OSDN Git Service

gcc:
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-math-opts.c
1 /* Global, SSA-based optimizations using mathematical identities.
2    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* Currently, the only mini-pass in this file tries to CSE reciprocal
22    operations.  These are common in sequences such as this one:
23
24         modulus = sqrt(x*x + y*y + z*z);
25         x = x / modulus;
26         y = y / modulus;
27         z = z / modulus;
28
29    that can be optimized to
30
31         modulus = sqrt(x*x + y*y + z*z);
32         rmodulus = 1.0 / modulus;
33         x = x * rmodulus;
34         y = y * rmodulus;
35         z = z * rmodulus;
36
37    We do this for loop invariant divisors, and with this pass whenever
38    we notice that a division has the same divisor multiple times.
39
40    Of course, like in PRE, we don't insert a division if a dominator
41    already has one.  However, this cannot be done as an extension of
42    PRE for several reasons.
43
44    First of all, with some experiments it was found out that the
45    transformation is not always useful if there are only two divisions
46    hy the same divisor.  This is probably because modern processors
47    can pipeline the divisions; on older, in-order processors it should
48    still be effective to optimize two divisions by the same number.
49    We make this a param, and it shall be called N in the remainder of
50    this comment.
51
52    Second, if trapping math is active, we have less freedom on where
53    to insert divisions: we can only do so in basic blocks that already
54    contain one.  (If divisions don't trap, instead, we can insert
55    divisions elsewhere, which will be in blocks that are common dominators
56    of those that have the division).
57
58    We really don't want to compute the reciprocal unless a division will
59    be found.  To do this, we won't insert the division in a basic block
60    that has less than N divisions *post-dominating* it.
61
62    The algorithm constructs a subset of the dominator tree, holding the
63    blocks containing the divisions and the common dominators to them,
64    and walk it twice.  The first walk is in post-order, and it annotates
65    each block with the number of divisions that post-dominate it: this
66    gives information on where divisions can be inserted profitably.
67    The second walk is in pre-order, and it inserts divisions as explained
68    above, and replaces divisions by multiplications.
69
70    In the best case, the cost of the pass is O(n_statements).  In the
71    worst-case, the cost is due to creating the dominator tree subset,
72    with a cost of O(n_basic_blocks ^ 2); however this can only happen
73    for n_statements / n_basic_blocks statements.  So, the amortized cost
74    of creating the dominator tree subset is O(n_basic_blocks) and the
75    worst-case cost of the pass is O(n_statements * n_basic_blocks).
76
77    More practically, the cost will be small because there are few
78    divisions, and they tend to be in the same basic block, so insert_bb
79    is called very few times.
80
81    If we did this using domwalk.c, an efficient implementation would have
82    to work on all the variables in a single pass, because we could not
83    work on just a subset of the dominator tree, as we do now, and the
84    cost would also be something like O(n_statements * n_basic_blocks).
85    The data structures would be more complex in order to work on all the
86    variables in a single pass.  */
87
88 #include "config.h"
89 #include "system.h"
90 #include "coretypes.h"
91 #include "tm.h"
92 #include "flags.h"
93 #include "tree.h"
94 #include "tree-flow.h"
95 #include "timevar.h"
96 #include "tree-pass.h"
97 #include "alloc-pool.h"
98 #include "basic-block.h"
99 #include "target.h"
100 #include "gimple-pretty-print.h"
101
102 /* FIXME: RTL headers have to be included here for optabs.  */
103 #include "rtl.h"                /* Because optabs.h wants enum rtx_code.  */
104 #include "expr.h"               /* Because optabs.h wants sepops.  */
105 #include "optabs.h"
106
107 /* This structure represents one basic block that either computes a
108    division, or is a common dominator for basic block that compute a
109    division.  */
110 struct occurrence {
111   /* The basic block represented by this structure.  */
112   basic_block bb;
113
114   /* If non-NULL, the SSA_NAME holding the definition for a reciprocal
115      inserted in BB.  */
116   tree recip_def;
117
118   /* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that
119      was inserted in BB.  */
120   gimple recip_def_stmt;
121
122   /* Pointer to a list of "struct occurrence"s for blocks dominated
123      by BB.  */
124   struct occurrence *children;
125
126   /* Pointer to the next "struct occurrence"s in the list of blocks
127      sharing a common dominator.  */
128   struct occurrence *next;
129
130   /* The number of divisions that are in BB before compute_merit.  The
131      number of divisions that are in BB or post-dominate it after
132      compute_merit.  */
133   int num_divisions;
134
135   /* True if the basic block has a division, false if it is a common
136      dominator for basic blocks that do.  If it is false and trapping
137      math is active, BB is not a candidate for inserting a reciprocal.  */
138   bool bb_has_division;
139 };
140
141 static struct
142 {
143   /* Number of 1.0/X ops inserted.  */
144   int rdivs_inserted;
145
146   /* Number of 1.0/FUNC ops inserted.  */
147   int rfuncs_inserted;
148 } reciprocal_stats;
149
150 static struct
151 {
152   /* Number of cexpi calls inserted.  */
153   int inserted;
154 } sincos_stats;
155
156 static struct
157 {
158   /* Number of hand-written 32-bit bswaps found.  */
159   int found_32bit;
160
161   /* Number of hand-written 64-bit bswaps found.  */
162   int found_64bit;
163 } bswap_stats;
164
165 static struct
166 {
167   /* Number of widening multiplication ops inserted.  */
168   int widen_mults_inserted;
169
170   /* Number of integer multiply-and-accumulate ops inserted.  */
171   int maccs_inserted;
172
173   /* Number of fp fused multiply-add ops inserted.  */
174   int fmas_inserted;
175 } widen_mul_stats;
176
177 /* The instance of "struct occurrence" representing the highest
178    interesting block in the dominator tree.  */
179 static struct occurrence *occ_head;
180
181 /* Allocation pool for getting instances of "struct occurrence".  */
182 static alloc_pool occ_pool;
183
184
185
186 /* Allocate and return a new struct occurrence for basic block BB, and
187    whose children list is headed by CHILDREN.  */
188 static struct occurrence *
189 occ_new (basic_block bb, struct occurrence *children)
190 {
191   struct occurrence *occ;
192
193   bb->aux = occ = (struct occurrence *) pool_alloc (occ_pool);
194   memset (occ, 0, sizeof (struct occurrence));
195
196   occ->bb = bb;
197   occ->children = children;
198   return occ;
199 }
200
201
202 /* Insert NEW_OCC into our subset of the dominator tree.  P_HEAD points to a
203    list of "struct occurrence"s, one per basic block, having IDOM as
204    their common dominator.
205
206    We try to insert NEW_OCC as deep as possible in the tree, and we also
207    insert any other block that is a common dominator for BB and one
208    block already in the tree.  */
209
210 static void
211 insert_bb (struct occurrence *new_occ, basic_block idom,
212            struct occurrence **p_head)
213 {
214   struct occurrence *occ, **p_occ;
215
216   for (p_occ = p_head; (occ = *p_occ) != NULL; )
217     {
218       basic_block bb = new_occ->bb, occ_bb = occ->bb;
219       basic_block dom = nearest_common_dominator (CDI_DOMINATORS, occ_bb, bb);
220       if (dom == bb)
221         {
222           /* BB dominates OCC_BB.  OCC becomes NEW_OCC's child: remove OCC
223              from its list.  */
224           *p_occ = occ->next;
225           occ->next = new_occ->children;
226           new_occ->children = occ;
227
228           /* Try the next block (it may as well be dominated by BB).  */
229         }
230
231       else if (dom == occ_bb)
232         {
233           /* OCC_BB dominates BB.  Tail recurse to look deeper.  */
234           insert_bb (new_occ, dom, &occ->children);
235           return;
236         }
237
238       else if (dom != idom)
239         {
240           gcc_assert (!dom->aux);
241
242           /* There is a dominator between IDOM and BB, add it and make
243              two children out of NEW_OCC and OCC.  First, remove OCC from
244              its list.  */
245           *p_occ = occ->next;
246           new_occ->next = occ;
247           occ->next = NULL;
248
249           /* None of the previous blocks has DOM as a dominator: if we tail
250              recursed, we would reexamine them uselessly. Just switch BB with
251              DOM, and go on looking for blocks dominated by DOM.  */
252           new_occ = occ_new (dom, new_occ);
253         }
254
255       else
256         {
257           /* Nothing special, go on with the next element.  */
258           p_occ = &occ->next;
259         }
260     }
261
262   /* No place was found as a child of IDOM.  Make BB a sibling of IDOM.  */
263   new_occ->next = *p_head;
264   *p_head = new_occ;
265 }
266
267 /* Register that we found a division in BB.  */
268
269 static inline void
270 register_division_in (basic_block bb)
271 {
272   struct occurrence *occ;
273
274   occ = (struct occurrence *) bb->aux;
275   if (!occ)
276     {
277       occ = occ_new (bb, NULL);
278       insert_bb (occ, ENTRY_BLOCK_PTR, &occ_head);
279     }
280
281   occ->bb_has_division = true;
282   occ->num_divisions++;
283 }
284
285
286 /* Compute the number of divisions that postdominate each block in OCC and
287    its children.  */
288
289 static void
290 compute_merit (struct occurrence *occ)
291 {
292   struct occurrence *occ_child;
293   basic_block dom = occ->bb;
294
295   for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
296     {
297       basic_block bb;
298       if (occ_child->children)
299         compute_merit (occ_child);
300
301       if (flag_exceptions)
302         bb = single_noncomplex_succ (dom);
303       else
304         bb = dom;
305
306       if (dominated_by_p (CDI_POST_DOMINATORS, bb, occ_child->bb))
307         occ->num_divisions += occ_child->num_divisions;
308     }
309 }
310
311
312 /* Return whether USE_STMT is a floating-point division by DEF.  */
313 static inline bool
314 is_division_by (gimple use_stmt, tree def)
315 {
316   return is_gimple_assign (use_stmt)
317          && gimple_assign_rhs_code (use_stmt) == RDIV_EXPR
318          && gimple_assign_rhs2 (use_stmt) == def
319          /* Do not recognize x / x as valid division, as we are getting
320             confused later by replacing all immediate uses x in such
321             a stmt.  */
322          && gimple_assign_rhs1 (use_stmt) != def;
323 }
324
325 /* Walk the subset of the dominator tree rooted at OCC, setting the
326    RECIP_DEF field to a definition of 1.0 / DEF that can be used in
327    the given basic block.  The field may be left NULL, of course,
328    if it is not possible or profitable to do the optimization.
329
330    DEF_BSI is an iterator pointing at the statement defining DEF.
331    If RECIP_DEF is set, a dominator already has a computation that can
332    be used.  */
333
334 static void
335 insert_reciprocals (gimple_stmt_iterator *def_gsi, struct occurrence *occ,
336                     tree def, tree recip_def, int threshold)
337 {
338   tree type;
339   gimple new_stmt;
340   gimple_stmt_iterator gsi;
341   struct occurrence *occ_child;
342
343   if (!recip_def
344       && (occ->bb_has_division || !flag_trapping_math)
345       && occ->num_divisions >= threshold)
346     {
347       /* Make a variable with the replacement and substitute it.  */
348       type = TREE_TYPE (def);
349       recip_def = make_rename_temp (type, "reciptmp");
350       new_stmt = gimple_build_assign_with_ops (RDIV_EXPR, recip_def,
351                                                build_one_cst (type), def);
352
353       if (occ->bb_has_division)
354         {
355           /* Case 1: insert before an existing division.  */
356           gsi = gsi_after_labels (occ->bb);
357           while (!gsi_end_p (gsi) && !is_division_by (gsi_stmt (gsi), def))
358             gsi_next (&gsi);
359
360           gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
361         }
362       else if (def_gsi && occ->bb == def_gsi->bb)
363         {
364           /* Case 2: insert right after the definition.  Note that this will
365              never happen if the definition statement can throw, because in
366              that case the sole successor of the statement's basic block will
367              dominate all the uses as well.  */
368           gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT);
369         }
370       else
371         {
372           /* Case 3: insert in a basic block not containing defs/uses.  */
373           gsi = gsi_after_labels (occ->bb);
374           gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
375         }
376
377       reciprocal_stats.rdivs_inserted++;
378
379       occ->recip_def_stmt = new_stmt;
380     }
381
382   occ->recip_def = recip_def;
383   for (occ_child = occ->children; occ_child; occ_child = occ_child->next)
384     insert_reciprocals (def_gsi, occ_child, def, recip_def, threshold);
385 }
386
387
388 /* Replace the division at USE_P with a multiplication by the reciprocal, if
389    possible.  */
390
391 static inline void
392 replace_reciprocal (use_operand_p use_p)
393 {
394   gimple use_stmt = USE_STMT (use_p);
395   basic_block bb = gimple_bb (use_stmt);
396   struct occurrence *occ = (struct occurrence *) bb->aux;
397
398   if (optimize_bb_for_speed_p (bb)
399       && occ->recip_def && use_stmt != occ->recip_def_stmt)
400     {
401       gimple_assign_set_rhs_code (use_stmt, MULT_EXPR);
402       SET_USE (use_p, occ->recip_def);
403       fold_stmt_inplace (use_stmt);
404       update_stmt (use_stmt);
405     }
406 }
407
408
409 /* Free OCC and return one more "struct occurrence" to be freed.  */
410
411 static struct occurrence *
412 free_bb (struct occurrence *occ)
413 {
414   struct occurrence *child, *next;
415
416   /* First get the two pointers hanging off OCC.  */
417   next = occ->next;
418   child = occ->children;
419   occ->bb->aux = NULL;
420   pool_free (occ_pool, occ);
421
422   /* Now ensure that we don't recurse unless it is necessary.  */
423   if (!child)
424     return next;
425   else
426     {
427       while (next)
428         next = free_bb (next);
429
430       return child;
431     }
432 }
433
434
435 /* Look for floating-point divisions among DEF's uses, and try to
436    replace them by multiplications with the reciprocal.  Add
437    as many statements computing the reciprocal as needed.
438
439    DEF must be a GIMPLE register of a floating-point type.  */
440
441 static void
442 execute_cse_reciprocals_1 (gimple_stmt_iterator *def_gsi, tree def)
443 {
444   use_operand_p use_p;
445   imm_use_iterator use_iter;
446   struct occurrence *occ;
447   int count = 0, threshold;
448
449   gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def)) && is_gimple_reg (def));
450
451   FOR_EACH_IMM_USE_FAST (use_p, use_iter, def)
452     {
453       gimple use_stmt = USE_STMT (use_p);
454       if (is_division_by (use_stmt, def))
455         {
456           register_division_in (gimple_bb (use_stmt));
457           count++;
458         }
459     }
460
461   /* Do the expensive part only if we can hope to optimize something.  */
462   threshold = targetm.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def)));
463   if (count >= threshold)
464     {
465       gimple use_stmt;
466       for (occ = occ_head; occ; occ = occ->next)
467         {
468           compute_merit (occ);
469           insert_reciprocals (def_gsi, occ, def, NULL, threshold);
470         }
471
472       FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
473         {
474           if (is_division_by (use_stmt, def))
475             {
476               FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter)
477                 replace_reciprocal (use_p);
478             }
479         }
480     }
481
482   for (occ = occ_head; occ; )
483     occ = free_bb (occ);
484
485   occ_head = NULL;
486 }
487
488 static bool
489 gate_cse_reciprocals (void)
490 {
491   return optimize && flag_reciprocal_math;
492 }
493
494 /* Go through all the floating-point SSA_NAMEs, and call
495    execute_cse_reciprocals_1 on each of them.  */
496 static unsigned int
497 execute_cse_reciprocals (void)
498 {
499   basic_block bb;
500   tree arg;
501
502   occ_pool = create_alloc_pool ("dominators for recip",
503                                 sizeof (struct occurrence),
504                                 n_basic_blocks / 3 + 1);
505
506   memset (&reciprocal_stats, 0, sizeof (reciprocal_stats));
507   calculate_dominance_info (CDI_DOMINATORS);
508   calculate_dominance_info (CDI_POST_DOMINATORS);
509
510 #ifdef ENABLE_CHECKING
511   FOR_EACH_BB (bb)
512     gcc_assert (!bb->aux);
513 #endif
514
515   for (arg = DECL_ARGUMENTS (cfun->decl); arg; arg = DECL_CHAIN (arg))
516     if (gimple_default_def (cfun, arg)
517         && FLOAT_TYPE_P (TREE_TYPE (arg))
518         && is_gimple_reg (arg))
519       execute_cse_reciprocals_1 (NULL, gimple_default_def (cfun, arg));
520
521   FOR_EACH_BB (bb)
522     {
523       gimple_stmt_iterator gsi;
524       gimple phi;
525       tree def;
526
527       for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
528         {
529           phi = gsi_stmt (gsi);
530           def = PHI_RESULT (phi);
531           if (FLOAT_TYPE_P (TREE_TYPE (def))
532               && is_gimple_reg (def))
533             execute_cse_reciprocals_1 (NULL, def);
534         }
535
536       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
537         {
538           gimple stmt = gsi_stmt (gsi);
539
540           if (gimple_has_lhs (stmt)
541               && (def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF)) != NULL
542               && FLOAT_TYPE_P (TREE_TYPE (def))
543               && TREE_CODE (def) == SSA_NAME)
544             execute_cse_reciprocals_1 (&gsi, def);
545         }
546
547       if (optimize_bb_for_size_p (bb))
548         continue;
549
550       /* Scan for a/func(b) and convert it to reciprocal a*rfunc(b).  */
551       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
552         {
553           gimple stmt = gsi_stmt (gsi);
554           tree fndecl;
555
556           if (is_gimple_assign (stmt)
557               && gimple_assign_rhs_code (stmt) == RDIV_EXPR)
558             {
559               tree arg1 = gimple_assign_rhs2 (stmt);
560               gimple stmt1;
561
562               if (TREE_CODE (arg1) != SSA_NAME)
563                 continue;
564
565               stmt1 = SSA_NAME_DEF_STMT (arg1);
566
567               if (is_gimple_call (stmt1)
568                   && gimple_call_lhs (stmt1)
569                   && (fndecl = gimple_call_fndecl (stmt1))
570                   && (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
571                       || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD))
572                 {
573                   enum built_in_function code;
574                   bool md_code, fail;
575                   imm_use_iterator ui;
576                   use_operand_p use_p;
577
578                   code = DECL_FUNCTION_CODE (fndecl);
579                   md_code = DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD;
580
581                   fndecl = targetm.builtin_reciprocal (code, md_code, false);
582                   if (!fndecl)
583                     continue;
584
585                   /* Check that all uses of the SSA name are divisions,
586                      otherwise replacing the defining statement will do
587                      the wrong thing.  */
588                   fail = false;
589                   FOR_EACH_IMM_USE_FAST (use_p, ui, arg1)
590                     {
591                       gimple stmt2 = USE_STMT (use_p);
592                       if (is_gimple_debug (stmt2))
593                         continue;
594                       if (!is_gimple_assign (stmt2)
595                           || gimple_assign_rhs_code (stmt2) != RDIV_EXPR
596                           || gimple_assign_rhs1 (stmt2) == arg1
597                           || gimple_assign_rhs2 (stmt2) != arg1)
598                         {
599                           fail = true;
600                           break;
601                         }
602                     }
603                   if (fail)
604                     continue;
605
606                   gimple_replace_lhs (stmt1, arg1);
607                   gimple_call_set_fndecl (stmt1, fndecl);
608                   update_stmt (stmt1);
609                   reciprocal_stats.rfuncs_inserted++;
610
611                   FOR_EACH_IMM_USE_STMT (stmt, ui, arg1)
612                     {
613                       gimple_assign_set_rhs_code (stmt, MULT_EXPR);
614                       fold_stmt_inplace (stmt);
615                       update_stmt (stmt);
616                     }
617                 }
618             }
619         }
620     }
621
622   statistics_counter_event (cfun, "reciprocal divs inserted",
623                             reciprocal_stats.rdivs_inserted);
624   statistics_counter_event (cfun, "reciprocal functions inserted",
625                             reciprocal_stats.rfuncs_inserted);
626
627   free_dominance_info (CDI_DOMINATORS);
628   free_dominance_info (CDI_POST_DOMINATORS);
629   free_alloc_pool (occ_pool);
630   return 0;
631 }
632
633 struct gimple_opt_pass pass_cse_reciprocals =
634 {
635  {
636   GIMPLE_PASS,
637   "recip",                              /* name */
638   gate_cse_reciprocals,                 /* gate */
639   execute_cse_reciprocals,              /* execute */
640   NULL,                                 /* sub */
641   NULL,                                 /* next */
642   0,                                    /* static_pass_number */
643   TV_NONE,                              /* tv_id */
644   PROP_ssa,                             /* properties_required */
645   0,                                    /* properties_provided */
646   0,                                    /* properties_destroyed */
647   0,                                    /* todo_flags_start */
648   TODO_dump_func | TODO_update_ssa | TODO_verify_ssa
649     | TODO_verify_stmts                /* todo_flags_finish */
650  }
651 };
652
653 /* Records an occurrence at statement USE_STMT in the vector of trees
654    STMTS if it is dominated by *TOP_BB or dominates it or this basic block
655    is not yet initialized.  Returns true if the occurrence was pushed on
656    the vector.  Adjusts *TOP_BB to be the basic block dominating all
657    statements in the vector.  */
658
659 static bool
660 maybe_record_sincos (VEC(gimple, heap) **stmts,
661                      basic_block *top_bb, gimple use_stmt)
662 {
663   basic_block use_bb = gimple_bb (use_stmt);
664   if (*top_bb
665       && (*top_bb == use_bb
666           || dominated_by_p (CDI_DOMINATORS, use_bb, *top_bb)))
667     VEC_safe_push (gimple, heap, *stmts, use_stmt);
668   else if (!*top_bb
669            || dominated_by_p (CDI_DOMINATORS, *top_bb, use_bb))
670     {
671       VEC_safe_push (gimple, heap, *stmts, use_stmt);
672       *top_bb = use_bb;
673     }
674   else
675     return false;
676
677   return true;
678 }
679
680 /* Look for sin, cos and cexpi calls with the same argument NAME and
681    create a single call to cexpi CSEing the result in this case.
682    We first walk over all immediate uses of the argument collecting
683    statements that we can CSE in a vector and in a second pass replace
684    the statement rhs with a REALPART or IMAGPART expression on the
685    result of the cexpi call we insert before the use statement that
686    dominates all other candidates.  */
687
688 static bool
689 execute_cse_sincos_1 (tree name)
690 {
691   gimple_stmt_iterator gsi;
692   imm_use_iterator use_iter;
693   tree fndecl, res, type;
694   gimple def_stmt, use_stmt, stmt;
695   int seen_cos = 0, seen_sin = 0, seen_cexpi = 0;
696   VEC(gimple, heap) *stmts = NULL;
697   basic_block top_bb = NULL;
698   int i;
699   bool cfg_changed = false;
700
701   type = TREE_TYPE (name);
702   FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, name)
703     {
704       if (gimple_code (use_stmt) != GIMPLE_CALL
705           || !gimple_call_lhs (use_stmt)
706           || !(fndecl = gimple_call_fndecl (use_stmt))
707           || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
708         continue;
709
710       switch (DECL_FUNCTION_CODE (fndecl))
711         {
712         CASE_FLT_FN (BUILT_IN_COS):
713           seen_cos |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
714           break;
715
716         CASE_FLT_FN (BUILT_IN_SIN):
717           seen_sin |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
718           break;
719
720         CASE_FLT_FN (BUILT_IN_CEXPI):
721           seen_cexpi |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0;
722           break;
723
724         default:;
725         }
726     }
727
728   if (seen_cos + seen_sin + seen_cexpi <= 1)
729     {
730       VEC_free(gimple, heap, stmts);
731       return false;
732     }
733
734   /* Simply insert cexpi at the beginning of top_bb but not earlier than
735      the name def statement.  */
736   fndecl = mathfn_built_in (type, BUILT_IN_CEXPI);
737   if (!fndecl)
738     return false;
739   res = create_tmp_reg (TREE_TYPE (TREE_TYPE (fndecl)), "sincostmp");
740   stmt = gimple_build_call (fndecl, 1, name);
741   res = make_ssa_name (res, stmt);
742   gimple_call_set_lhs (stmt, res);
743
744   def_stmt = SSA_NAME_DEF_STMT (name);
745   if (!SSA_NAME_IS_DEFAULT_DEF (name)
746       && gimple_code (def_stmt) != GIMPLE_PHI
747       && gimple_bb (def_stmt) == top_bb)
748     {
749       gsi = gsi_for_stmt (def_stmt);
750       gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
751     }
752   else
753     {
754       gsi = gsi_after_labels (top_bb);
755       gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
756     }
757   update_stmt (stmt);
758   sincos_stats.inserted++;
759
760   /* And adjust the recorded old call sites.  */
761   for (i = 0; VEC_iterate(gimple, stmts, i, use_stmt); ++i)
762     {
763       tree rhs = NULL;
764       fndecl = gimple_call_fndecl (use_stmt);
765
766       switch (DECL_FUNCTION_CODE (fndecl))
767         {
768         CASE_FLT_FN (BUILT_IN_COS):
769           rhs = fold_build1 (REALPART_EXPR, type, res);
770           break;
771
772         CASE_FLT_FN (BUILT_IN_SIN):
773           rhs = fold_build1 (IMAGPART_EXPR, type, res);
774           break;
775
776         CASE_FLT_FN (BUILT_IN_CEXPI):
777           rhs = res;
778           break;
779
780         default:;
781           gcc_unreachable ();
782         }
783
784         /* Replace call with a copy.  */
785         stmt = gimple_build_assign (gimple_call_lhs (use_stmt), rhs);
786
787         gsi = gsi_for_stmt (use_stmt);
788         gsi_replace (&gsi, stmt, true);
789         if (gimple_purge_dead_eh_edges (gimple_bb (stmt)))
790           cfg_changed = true;
791     }
792
793   VEC_free(gimple, heap, stmts);
794
795   return cfg_changed;
796 }
797
798 /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1
799    on the SSA_NAME argument of each of them.  */
800
801 static unsigned int
802 execute_cse_sincos (void)
803 {
804   basic_block bb;
805   bool cfg_changed = false;
806
807   calculate_dominance_info (CDI_DOMINATORS);
808   memset (&sincos_stats, 0, sizeof (sincos_stats));
809
810   FOR_EACH_BB (bb)
811     {
812       gimple_stmt_iterator gsi;
813
814       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
815         {
816           gimple stmt = gsi_stmt (gsi);
817           tree fndecl;
818
819           if (is_gimple_call (stmt)
820               && gimple_call_lhs (stmt)
821               && (fndecl = gimple_call_fndecl (stmt))
822               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
823             {
824               tree arg;
825
826               switch (DECL_FUNCTION_CODE (fndecl))
827                 {
828                 CASE_FLT_FN (BUILT_IN_COS):
829                 CASE_FLT_FN (BUILT_IN_SIN):
830                 CASE_FLT_FN (BUILT_IN_CEXPI):
831                   arg = gimple_call_arg (stmt, 0);
832                   if (TREE_CODE (arg) == SSA_NAME)
833                     cfg_changed |= execute_cse_sincos_1 (arg);
834                   break;
835
836                 default:;
837                 }
838             }
839         }
840     }
841
842   statistics_counter_event (cfun, "sincos statements inserted",
843                             sincos_stats.inserted);
844
845   free_dominance_info (CDI_DOMINATORS);
846   return cfg_changed ? TODO_cleanup_cfg : 0;
847 }
848
849 static bool
850 gate_cse_sincos (void)
851 {
852   /* Make sure we have either sincos or cexp.  */
853   return (TARGET_HAS_SINCOS
854           || TARGET_C99_FUNCTIONS)
855          && optimize;
856 }
857
858 struct gimple_opt_pass pass_cse_sincos =
859 {
860  {
861   GIMPLE_PASS,
862   "sincos",                             /* name */
863   gate_cse_sincos,                      /* gate */
864   execute_cse_sincos,                   /* execute */
865   NULL,                                 /* sub */
866   NULL,                                 /* next */
867   0,                                    /* static_pass_number */
868   TV_NONE,                              /* tv_id */
869   PROP_ssa,                             /* properties_required */
870   0,                                    /* properties_provided */
871   0,                                    /* properties_destroyed */
872   0,                                    /* todo_flags_start */
873   TODO_dump_func | TODO_update_ssa | TODO_verify_ssa
874     | TODO_verify_stmts                 /* todo_flags_finish */
875  }
876 };
877
878 /* A symbolic number is used to detect byte permutation and selection
879    patterns.  Therefore the field N contains an artificial number
880    consisting of byte size markers:
881
882    0    - byte has the value 0
883    1..size - byte contains the content of the byte
884    number indexed with that value minus one  */
885
886 struct symbolic_number {
887   unsigned HOST_WIDEST_INT n;
888   int size;
889 };
890
891 /* Perform a SHIFT or ROTATE operation by COUNT bits on symbolic
892    number N.  Return false if the requested operation is not permitted
893    on a symbolic number.  */
894
895 static inline bool
896 do_shift_rotate (enum tree_code code,
897                  struct symbolic_number *n,
898                  int count)
899 {
900   if (count % 8 != 0)
901     return false;
902
903   /* Zero out the extra bits of N in order to avoid them being shifted
904      into the significant bits.  */
905   if (n->size < (int)sizeof (HOST_WIDEST_INT))
906     n->n &= ((unsigned HOST_WIDEST_INT)1 << (n->size * BITS_PER_UNIT)) - 1;
907
908   switch (code)
909     {
910     case LSHIFT_EXPR:
911       n->n <<= count;
912       break;
913     case RSHIFT_EXPR:
914       n->n >>= count;
915       break;
916     case LROTATE_EXPR:
917       n->n = (n->n << count) | (n->n >> ((n->size * BITS_PER_UNIT) - count));
918       break;
919     case RROTATE_EXPR:
920       n->n = (n->n >> count) | (n->n << ((n->size * BITS_PER_UNIT) - count));
921       break;
922     default:
923       return false;
924     }
925   return true;
926 }
927
928 /* Perform sanity checking for the symbolic number N and the gimple
929    statement STMT.  */
930
931 static inline bool
932 verify_symbolic_number_p (struct symbolic_number *n, gimple stmt)
933 {
934   tree lhs_type;
935
936   lhs_type = gimple_expr_type (stmt);
937
938   if (TREE_CODE (lhs_type) != INTEGER_TYPE)
939     return false;
940
941   if (TYPE_PRECISION (lhs_type) != n->size * BITS_PER_UNIT)
942     return false;
943
944   return true;
945 }
946
947 /* find_bswap_1 invokes itself recursively with N and tries to perform
948    the operation given by the rhs of STMT on the result.  If the
949    operation could successfully be executed the function returns the
950    tree expression of the source operand and NULL otherwise.  */
951
952 static tree
953 find_bswap_1 (gimple stmt, struct symbolic_number *n, int limit)
954 {
955   enum tree_code code;
956   tree rhs1, rhs2 = NULL;
957   gimple rhs1_stmt, rhs2_stmt;
958   tree source_expr1;
959   enum gimple_rhs_class rhs_class;
960
961   if (!limit || !is_gimple_assign (stmt))
962     return NULL_TREE;
963
964   rhs1 = gimple_assign_rhs1 (stmt);
965
966   if (TREE_CODE (rhs1) != SSA_NAME)
967     return NULL_TREE;
968
969   code = gimple_assign_rhs_code (stmt);
970   rhs_class = gimple_assign_rhs_class (stmt);
971   rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
972
973   if (rhs_class == GIMPLE_BINARY_RHS)
974     rhs2 = gimple_assign_rhs2 (stmt);
975
976   /* Handle unary rhs and binary rhs with integer constants as second
977      operand.  */
978
979   if (rhs_class == GIMPLE_UNARY_RHS
980       || (rhs_class == GIMPLE_BINARY_RHS
981           && TREE_CODE (rhs2) == INTEGER_CST))
982     {
983       if (code != BIT_AND_EXPR
984           && code != LSHIFT_EXPR
985           && code != RSHIFT_EXPR
986           && code != LROTATE_EXPR
987           && code != RROTATE_EXPR
988           && code != NOP_EXPR
989           && code != CONVERT_EXPR)
990         return NULL_TREE;
991
992       source_expr1 = find_bswap_1 (rhs1_stmt, n, limit - 1);
993
994       /* If find_bswap_1 returned NULL STMT is a leaf node and we have
995          to initialize the symbolic number.  */
996       if (!source_expr1)
997         {
998           /* Set up the symbolic number N by setting each byte to a
999              value between 1 and the byte size of rhs1.  The highest
1000              order byte is set to n->size and the lowest order
1001              byte to 1.  */
1002           n->size = TYPE_PRECISION (TREE_TYPE (rhs1));
1003           if (n->size % BITS_PER_UNIT != 0)
1004             return NULL_TREE;
1005           n->size /= BITS_PER_UNIT;
1006           n->n = (sizeof (HOST_WIDEST_INT) < 8 ? 0 :
1007                   (unsigned HOST_WIDEST_INT)0x08070605 << 32 | 0x04030201);
1008
1009           if (n->size < (int)sizeof (HOST_WIDEST_INT))
1010             n->n &= ((unsigned HOST_WIDEST_INT)1 <<
1011                      (n->size * BITS_PER_UNIT)) - 1;
1012
1013           source_expr1 = rhs1;
1014         }
1015
1016       switch (code)
1017         {
1018         case BIT_AND_EXPR:
1019           {
1020             int i;
1021             unsigned HOST_WIDEST_INT val = widest_int_cst_value (rhs2);
1022             unsigned HOST_WIDEST_INT tmp = val;
1023
1024             /* Only constants masking full bytes are allowed.  */
1025             for (i = 0; i < n->size; i++, tmp >>= BITS_PER_UNIT)
1026               if ((tmp & 0xff) != 0 && (tmp & 0xff) != 0xff)
1027                 return NULL_TREE;
1028
1029             n->n &= val;
1030           }
1031           break;
1032         case LSHIFT_EXPR:
1033         case RSHIFT_EXPR:
1034         case LROTATE_EXPR:
1035         case RROTATE_EXPR:
1036           if (!do_shift_rotate (code, n, (int)TREE_INT_CST_LOW (rhs2)))
1037             return NULL_TREE;
1038           break;
1039         CASE_CONVERT:
1040           {
1041             int type_size;
1042
1043             type_size = TYPE_PRECISION (gimple_expr_type (stmt));
1044             if (type_size % BITS_PER_UNIT != 0)
1045               return NULL_TREE;
1046
1047             if (type_size / BITS_PER_UNIT < (int)(sizeof (HOST_WIDEST_INT)))
1048               {
1049                 /* If STMT casts to a smaller type mask out the bits not
1050                    belonging to the target type.  */
1051                 n->n &= ((unsigned HOST_WIDEST_INT)1 << type_size) - 1;
1052               }
1053             n->size = type_size / BITS_PER_UNIT;
1054           }
1055           break;
1056         default:
1057           return NULL_TREE;
1058         };
1059       return verify_symbolic_number_p (n, stmt) ? source_expr1 : NULL;
1060     }
1061
1062   /* Handle binary rhs.  */
1063
1064   if (rhs_class == GIMPLE_BINARY_RHS)
1065     {
1066       struct symbolic_number n1, n2;
1067       tree source_expr2;
1068
1069       if (code != BIT_IOR_EXPR)
1070         return NULL_TREE;
1071
1072       if (TREE_CODE (rhs2) != SSA_NAME)
1073         return NULL_TREE;
1074
1075       rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
1076
1077       switch (code)
1078         {
1079         case BIT_IOR_EXPR:
1080           source_expr1 = find_bswap_1 (rhs1_stmt, &n1, limit - 1);
1081
1082           if (!source_expr1)
1083             return NULL_TREE;
1084
1085           source_expr2 = find_bswap_1 (rhs2_stmt, &n2, limit - 1);
1086
1087           if (source_expr1 != source_expr2
1088               || n1.size != n2.size)
1089             return NULL_TREE;
1090
1091           n->size = n1.size;
1092           n->n = n1.n | n2.n;
1093
1094           if (!verify_symbolic_number_p (n, stmt))
1095             return NULL_TREE;
1096
1097           break;
1098         default:
1099           return NULL_TREE;
1100         }
1101       return source_expr1;
1102     }
1103   return NULL_TREE;
1104 }
1105
1106 /* Check if STMT completes a bswap implementation consisting of ORs,
1107    SHIFTs and ANDs.  Return the source tree expression on which the
1108    byte swap is performed and NULL if no bswap was found.  */
1109
1110 static tree
1111 find_bswap (gimple stmt)
1112 {
1113 /* The number which the find_bswap result should match in order to
1114    have a full byte swap.  The number is shifted to the left according
1115    to the size of the symbolic number before using it.  */
1116   unsigned HOST_WIDEST_INT cmp =
1117     sizeof (HOST_WIDEST_INT) < 8 ? 0 :
1118     (unsigned HOST_WIDEST_INT)0x01020304 << 32 | 0x05060708;
1119
1120   struct symbolic_number n;
1121   tree source_expr;
1122
1123   /* The last parameter determines the depth search limit.  It usually
1124      correlates directly to the number of bytes to be touched.  We
1125      increase that number by one here in order to also cover signed ->
1126      unsigned conversions of the src operand as can be seen in
1127      libgcc.  */
1128   source_expr =  find_bswap_1 (stmt, &n,
1129                                TREE_INT_CST_LOW (
1130                                  TYPE_SIZE_UNIT (gimple_expr_type (stmt))) + 1);
1131
1132   if (!source_expr)
1133     return NULL_TREE;
1134
1135   /* Zero out the extra bits of N and CMP.  */
1136   if (n.size < (int)sizeof (HOST_WIDEST_INT))
1137     {
1138       unsigned HOST_WIDEST_INT mask =
1139         ((unsigned HOST_WIDEST_INT)1 << (n.size * BITS_PER_UNIT)) - 1;
1140
1141       n.n &= mask;
1142       cmp >>= (sizeof (HOST_WIDEST_INT) - n.size) * BITS_PER_UNIT;
1143     }
1144
1145   /* A complete byte swap should make the symbolic number to start
1146      with the largest digit in the highest order byte.  */
1147   if (cmp != n.n)
1148     return NULL_TREE;
1149
1150   return source_expr;
1151 }
1152
1153 /* Find manual byte swap implementations and turn them into a bswap
1154    builtin invokation.  */
1155
1156 static unsigned int
1157 execute_optimize_bswap (void)
1158 {
1159   basic_block bb;
1160   bool bswap32_p, bswap64_p;
1161   bool changed = false;
1162   tree bswap32_type = NULL_TREE, bswap64_type = NULL_TREE;
1163
1164   if (BITS_PER_UNIT != 8)
1165     return 0;
1166
1167   if (sizeof (HOST_WIDEST_INT) < 8)
1168     return 0;
1169
1170   bswap32_p = (built_in_decls[BUILT_IN_BSWAP32]
1171                && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing);
1172   bswap64_p = (built_in_decls[BUILT_IN_BSWAP64]
1173                && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing
1174                    || (bswap32_p && word_mode == SImode)));
1175
1176   if (!bswap32_p && !bswap64_p)
1177     return 0;
1178
1179   /* Determine the argument type of the builtins.  The code later on
1180      assumes that the return and argument type are the same.  */
1181   if (bswap32_p)
1182     {
1183       tree fndecl = built_in_decls[BUILT_IN_BSWAP32];
1184       bswap32_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1185     }
1186
1187   if (bswap64_p)
1188     {
1189       tree fndecl = built_in_decls[BUILT_IN_BSWAP64];
1190       bswap64_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1191     }
1192
1193   memset (&bswap_stats, 0, sizeof (bswap_stats));
1194
1195   FOR_EACH_BB (bb)
1196     {
1197       gimple_stmt_iterator gsi;
1198
1199       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1200         {
1201           gimple stmt = gsi_stmt (gsi);
1202           tree bswap_src, bswap_type;
1203           tree bswap_tmp;
1204           tree fndecl = NULL_TREE;
1205           int type_size;
1206           gimple call;
1207
1208           if (!is_gimple_assign (stmt)
1209               || gimple_assign_rhs_code (stmt) != BIT_IOR_EXPR)
1210             continue;
1211
1212           type_size = TYPE_PRECISION (gimple_expr_type (stmt));
1213
1214           switch (type_size)
1215             {
1216             case 32:
1217               if (bswap32_p)
1218                 {
1219                   fndecl = built_in_decls[BUILT_IN_BSWAP32];
1220                   bswap_type = bswap32_type;
1221                 }
1222               break;
1223             case 64:
1224               if (bswap64_p)
1225                 {
1226                   fndecl = built_in_decls[BUILT_IN_BSWAP64];
1227                   bswap_type = bswap64_type;
1228                 }
1229               break;
1230             default:
1231               continue;
1232             }
1233
1234           if (!fndecl)
1235             continue;
1236
1237           bswap_src = find_bswap (stmt);
1238
1239           if (!bswap_src)
1240             continue;
1241
1242           changed = true;
1243           if (type_size == 32)
1244             bswap_stats.found_32bit++;
1245           else
1246             bswap_stats.found_64bit++;
1247
1248           bswap_tmp = bswap_src;
1249
1250           /* Convert the src expression if necessary.  */
1251           if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
1252             {
1253               gimple convert_stmt;
1254
1255               bswap_tmp = create_tmp_var (bswap_type, "bswapsrc");
1256               add_referenced_var (bswap_tmp);
1257               bswap_tmp = make_ssa_name (bswap_tmp, NULL);
1258
1259               convert_stmt = gimple_build_assign_with_ops (
1260                                CONVERT_EXPR, bswap_tmp, bswap_src, NULL);
1261               gsi_insert_before (&gsi, convert_stmt, GSI_SAME_STMT);
1262             }
1263
1264           call = gimple_build_call (fndecl, 1, bswap_tmp);
1265
1266           bswap_tmp = gimple_assign_lhs (stmt);
1267
1268           /* Convert the result if necessary.  */
1269           if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
1270             {
1271               gimple convert_stmt;
1272
1273               bswap_tmp = create_tmp_var (bswap_type, "bswapdst");
1274               add_referenced_var (bswap_tmp);
1275               bswap_tmp = make_ssa_name (bswap_tmp, NULL);
1276               convert_stmt = gimple_build_assign_with_ops (
1277                                CONVERT_EXPR, gimple_assign_lhs (stmt), bswap_tmp, NULL);
1278               gsi_insert_after (&gsi, convert_stmt, GSI_SAME_STMT);
1279             }
1280
1281           gimple_call_set_lhs (call, bswap_tmp);
1282
1283           if (dump_file)
1284             {
1285               fprintf (dump_file, "%d bit bswap implementation found at: ",
1286                        (int)type_size);
1287               print_gimple_stmt (dump_file, stmt, 0, 0);
1288             }
1289
1290           gsi_insert_after (&gsi, call, GSI_SAME_STMT);
1291           gsi_remove (&gsi, true);
1292         }
1293     }
1294
1295   statistics_counter_event (cfun, "32-bit bswap implementations found",
1296                             bswap_stats.found_32bit);
1297   statistics_counter_event (cfun, "64-bit bswap implementations found",
1298                             bswap_stats.found_64bit);
1299
1300   return (changed ? TODO_dump_func | TODO_update_ssa | TODO_verify_ssa
1301           | TODO_verify_stmts : 0);
1302 }
1303
1304 static bool
1305 gate_optimize_bswap (void)
1306 {
1307   return flag_expensive_optimizations && optimize;
1308 }
1309
1310 struct gimple_opt_pass pass_optimize_bswap =
1311 {
1312  {
1313   GIMPLE_PASS,
1314   "bswap",                              /* name */
1315   gate_optimize_bswap,                  /* gate */
1316   execute_optimize_bswap,               /* execute */
1317   NULL,                                 /* sub */
1318   NULL,                                 /* next */
1319   0,                                    /* static_pass_number */
1320   TV_NONE,                              /* tv_id */
1321   PROP_ssa,                             /* properties_required */
1322   0,                                    /* properties_provided */
1323   0,                                    /* properties_destroyed */
1324   0,                                    /* todo_flags_start */
1325   0                                     /* todo_flags_finish */
1326  }
1327 };
1328
1329 /* Return true if RHS is a suitable operand for a widening multiplication.
1330    There are two cases:
1331
1332      - RHS makes some value twice as wide.  Store that value in *NEW_RHS_OUT
1333        if so, and store its type in *TYPE_OUT.
1334
1335      - RHS is an integer constant.  Store that value in *NEW_RHS_OUT if so,
1336        but leave *TYPE_OUT untouched.  */
1337
1338 static bool
1339 is_widening_mult_rhs_p (tree rhs, tree *type_out, tree *new_rhs_out)
1340 {
1341   gimple stmt;
1342   tree type, type1, rhs1;
1343   enum tree_code rhs_code;
1344
1345   if (TREE_CODE (rhs) == SSA_NAME)
1346     {
1347       type = TREE_TYPE (rhs);
1348       stmt = SSA_NAME_DEF_STMT (rhs);
1349       if (!is_gimple_assign (stmt))
1350         return false;
1351
1352       rhs_code = gimple_assign_rhs_code (stmt);
1353       if (TREE_CODE (type) == INTEGER_TYPE
1354           ? !CONVERT_EXPR_CODE_P (rhs_code)
1355           : rhs_code != FIXED_CONVERT_EXPR)
1356         return false;
1357
1358       rhs1 = gimple_assign_rhs1 (stmt);
1359       type1 = TREE_TYPE (rhs1);
1360       if (TREE_CODE (type1) != TREE_CODE (type)
1361           || TYPE_PRECISION (type1) * 2 != TYPE_PRECISION (type))
1362         return false;
1363
1364       *new_rhs_out = rhs1;
1365       *type_out = type1;
1366       return true;
1367     }
1368
1369   if (TREE_CODE (rhs) == INTEGER_CST)
1370     {
1371       *new_rhs_out = rhs;
1372       *type_out = NULL;
1373       return true;
1374     }
1375
1376   return false;
1377 }
1378
1379 /* Return true if STMT performs a widening multiplication.  If so,
1380    store the unwidened types of the operands in *TYPE1_OUT and *TYPE2_OUT
1381    respectively.  Also fill *RHS1_OUT and *RHS2_OUT such that converting
1382    those operands to types *TYPE1_OUT and *TYPE2_OUT would give the
1383    operands of the multiplication.  */
1384
1385 static bool
1386 is_widening_mult_p (gimple stmt,
1387                     tree *type1_out, tree *rhs1_out,
1388                     tree *type2_out, tree *rhs2_out)
1389 {
1390   tree type;
1391
1392   type = TREE_TYPE (gimple_assign_lhs (stmt));
1393   if (TREE_CODE (type) != INTEGER_TYPE
1394       && TREE_CODE (type) != FIXED_POINT_TYPE)
1395     return false;
1396
1397   if (!is_widening_mult_rhs_p (gimple_assign_rhs1 (stmt), type1_out, rhs1_out))
1398     return false;
1399
1400   if (!is_widening_mult_rhs_p (gimple_assign_rhs2 (stmt), type2_out, rhs2_out))
1401     return false;
1402
1403   if (*type1_out == NULL)
1404     {
1405       if (*type2_out == NULL || !int_fits_type_p (*rhs1_out, *type2_out))
1406         return false;
1407       *type1_out = *type2_out;
1408     }
1409
1410   if (*type2_out == NULL)
1411     {
1412       if (!int_fits_type_p (*rhs2_out, *type1_out))
1413         return false;
1414       *type2_out = *type1_out;
1415     }
1416
1417   return true;
1418 }
1419
1420 /* Process a single gimple statement STMT, which has a MULT_EXPR as
1421    its rhs, and try to convert it into a WIDEN_MULT_EXPR.  The return
1422    value is true iff we converted the statement.  */
1423
1424 static bool
1425 convert_mult_to_widen (gimple stmt)
1426 {
1427   tree lhs, rhs1, rhs2, type, type1, type2;
1428   enum insn_code handler;
1429
1430   lhs = gimple_assign_lhs (stmt);
1431   type = TREE_TYPE (lhs);
1432   if (TREE_CODE (type) != INTEGER_TYPE)
1433     return false;
1434
1435   if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2))
1436     return false;
1437
1438   if (TYPE_UNSIGNED (type1) && TYPE_UNSIGNED (type2))
1439     handler = optab_handler (umul_widen_optab, TYPE_MODE (type));
1440   else if (!TYPE_UNSIGNED (type1) && !TYPE_UNSIGNED (type2))
1441     handler = optab_handler (smul_widen_optab, TYPE_MODE (type));
1442   else
1443     handler = optab_handler (usmul_widen_optab, TYPE_MODE (type));
1444
1445   if (handler == CODE_FOR_nothing)
1446     return false;
1447
1448   gimple_assign_set_rhs1 (stmt, fold_convert (type1, rhs1));
1449   gimple_assign_set_rhs2 (stmt, fold_convert (type2, rhs2));
1450   gimple_assign_set_rhs_code (stmt, WIDEN_MULT_EXPR);
1451   update_stmt (stmt);
1452   widen_mul_stats.widen_mults_inserted++;
1453   return true;
1454 }
1455
1456 /* Process a single gimple statement STMT, which is found at the
1457    iterator GSI and has a either a PLUS_EXPR or a MINUS_EXPR as its
1458    rhs (given by CODE), and try to convert it into a
1459    WIDEN_MULT_PLUS_EXPR or a WIDEN_MULT_MINUS_EXPR.  The return value
1460    is true iff we converted the statement.  */
1461
1462 static bool
1463 convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple stmt,
1464                             enum tree_code code)
1465 {
1466   gimple rhs1_stmt = NULL, rhs2_stmt = NULL;
1467   tree type, type1, type2;
1468   tree lhs, rhs1, rhs2, mult_rhs1, mult_rhs2, add_rhs;
1469   enum tree_code rhs1_code = ERROR_MARK, rhs2_code = ERROR_MARK;
1470   optab this_optab;
1471   enum tree_code wmult_code;
1472
1473   lhs = gimple_assign_lhs (stmt);
1474   type = TREE_TYPE (lhs);
1475   if (TREE_CODE (type) != INTEGER_TYPE
1476       && TREE_CODE (type) != FIXED_POINT_TYPE)
1477     return false;
1478
1479   if (code == MINUS_EXPR)
1480     wmult_code = WIDEN_MULT_MINUS_EXPR;
1481   else
1482     wmult_code = WIDEN_MULT_PLUS_EXPR;
1483
1484   rhs1 = gimple_assign_rhs1 (stmt);
1485   rhs2 = gimple_assign_rhs2 (stmt);
1486
1487   if (TREE_CODE (rhs1) == SSA_NAME)
1488     {
1489       rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
1490       if (is_gimple_assign (rhs1_stmt))
1491         rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
1492     }
1493   else
1494     return false;
1495
1496   if (TREE_CODE (rhs2) == SSA_NAME)
1497     {
1498       rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
1499       if (is_gimple_assign (rhs2_stmt))
1500         rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
1501     }
1502   else
1503     return false;
1504
1505   if (code == PLUS_EXPR && rhs1_code == MULT_EXPR)
1506     {
1507       if (!is_widening_mult_p (rhs1_stmt, &type1, &mult_rhs1,
1508                                &type2, &mult_rhs2))
1509         return false;
1510       add_rhs = rhs2;
1511     }
1512   else if (rhs2_code == MULT_EXPR)
1513     {
1514       if (!is_widening_mult_p (rhs2_stmt, &type1, &mult_rhs1,
1515                                &type2, &mult_rhs2))
1516         return false;
1517       add_rhs = rhs1;
1518     }
1519   else if (code == PLUS_EXPR && rhs1_code == WIDEN_MULT_EXPR)
1520     {
1521       mult_rhs1 = gimple_assign_rhs1 (rhs1_stmt);
1522       mult_rhs2 = gimple_assign_rhs2 (rhs1_stmt);
1523       type1 = TREE_TYPE (mult_rhs1);
1524       type2 = TREE_TYPE (mult_rhs2);
1525       add_rhs = rhs2;
1526     }
1527   else if (rhs2_code == WIDEN_MULT_EXPR)
1528     {
1529       mult_rhs1 = gimple_assign_rhs1 (rhs2_stmt);
1530       mult_rhs2 = gimple_assign_rhs2 (rhs2_stmt);
1531       type1 = TREE_TYPE (mult_rhs1);
1532       type2 = TREE_TYPE (mult_rhs2);
1533       add_rhs = rhs1;
1534     }
1535   else
1536     return false;
1537
1538   if (TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2))
1539     return false;
1540
1541   /* Verify that the machine can perform a widening multiply
1542      accumulate in this mode/signedness combination, otherwise
1543      this transformation is likely to pessimize code.  */
1544   this_optab = optab_for_tree_code (wmult_code, type1, optab_default);
1545   if (optab_handler (this_optab, TYPE_MODE (type)) == CODE_FOR_nothing)
1546     return false;
1547
1548   /* ??? May need some type verification here?  */
1549
1550   gimple_assign_set_rhs_with_ops_1 (gsi, wmult_code,
1551                                     fold_convert (type1, mult_rhs1),
1552                                     fold_convert (type2, mult_rhs2),
1553                                     add_rhs);
1554   update_stmt (gsi_stmt (*gsi));
1555   widen_mul_stats.maccs_inserted++;
1556   return true;
1557 }
1558
1559 /* Combine the multiplication at MUL_STMT with operands MULOP1 and MULOP2
1560    with uses in additions and subtractions to form fused multiply-add
1561    operations.  Returns true if successful and MUL_STMT should be removed.  */
1562
1563 static bool
1564 convert_mult_to_fma (gimple mul_stmt, tree op1, tree op2)
1565 {
1566   tree mul_result = gimple_get_lhs (mul_stmt);
1567   tree type = TREE_TYPE (mul_result);
1568   gimple use_stmt, neguse_stmt, fma_stmt;
1569   use_operand_p use_p;
1570   imm_use_iterator imm_iter;
1571
1572   if (FLOAT_TYPE_P (type)
1573       && flag_fp_contract_mode == FP_CONTRACT_OFF)
1574     return false;
1575
1576   /* We don't want to do bitfield reduction ops.  */
1577   if (INTEGRAL_TYPE_P (type)
1578       && (TYPE_PRECISION (type)
1579           != GET_MODE_PRECISION (TYPE_MODE (type))))
1580     return false;
1581
1582   /* If the target doesn't support it, don't generate it.  We assume that
1583      if fma isn't available then fms, fnma or fnms are not either.  */
1584   if (optab_handler (fma_optab, TYPE_MODE (type)) == CODE_FOR_nothing)
1585     return false;
1586
1587   /* Make sure that the multiplication statement becomes dead after
1588      the transformation, thus that all uses are transformed to FMAs.
1589      This means we assume that an FMA operation has the same cost
1590      as an addition.  */
1591   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, mul_result)
1592     {
1593       enum tree_code use_code;
1594       tree result = mul_result;
1595       bool negate_p = false;
1596
1597       use_stmt = USE_STMT (use_p);
1598
1599       if (is_gimple_debug (use_stmt))
1600         continue;
1601
1602       /* For now restrict this operations to single basic blocks.  In theory
1603          we would want to support sinking the multiplication in
1604          m = a*b;
1605          if ()
1606            ma = m + c;
1607          else
1608            d = m;
1609          to form a fma in the then block and sink the multiplication to the
1610          else block.  */
1611       if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
1612         return false;
1613
1614       if (!is_gimple_assign (use_stmt))
1615         return false;
1616
1617       use_code = gimple_assign_rhs_code (use_stmt);
1618
1619       /* A negate on the multiplication leads to FNMA.  */
1620       if (use_code == NEGATE_EXPR)
1621         {
1622           ssa_op_iter iter;
1623           tree use;
1624
1625           result = gimple_assign_lhs (use_stmt);
1626
1627           /* Make sure the negate statement becomes dead with this
1628              single transformation.  */
1629           if (!single_imm_use (gimple_assign_lhs (use_stmt),
1630                                &use_p, &neguse_stmt))
1631             return false;
1632
1633           /* Make sure the multiplication isn't also used on that stmt.  */
1634           FOR_EACH_SSA_TREE_OPERAND (use, neguse_stmt, iter, SSA_OP_USE)
1635             if (use == mul_result)
1636               return false;
1637
1638           /* Re-validate.  */
1639           use_stmt = neguse_stmt;
1640           if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
1641             return false;
1642           if (!is_gimple_assign (use_stmt))
1643             return false;
1644
1645           use_code = gimple_assign_rhs_code (use_stmt);
1646           negate_p = true;
1647         }
1648
1649       switch (use_code)
1650         {
1651         case MINUS_EXPR:
1652           if (gimple_assign_rhs2 (use_stmt) == result)
1653             negate_p = !negate_p;
1654           break;
1655         case PLUS_EXPR:
1656           break;
1657         default:
1658           /* FMA can only be formed from PLUS and MINUS.  */
1659           return false;
1660         }
1661
1662       /* We can't handle a * b + a * b.  */
1663       if (gimple_assign_rhs1 (use_stmt) == gimple_assign_rhs2 (use_stmt))
1664         return false;
1665
1666       /* While it is possible to validate whether or not the exact form
1667          that we've recognized is available in the backend, the assumption
1668          is that the transformation is never a loss.  For instance, suppose
1669          the target only has the plain FMA pattern available.  Consider
1670          a*b-c -> fma(a,b,-c): we've exchanged MUL+SUB for FMA+NEG, which
1671          is still two operations.  Consider -(a*b)-c -> fma(-a,b,-c): we
1672          still have 3 operations, but in the FMA form the two NEGs are
1673          independant and could be run in parallel.  */
1674     }
1675
1676   FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, mul_result)
1677     {
1678       gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1679       enum tree_code use_code;
1680       tree addop, mulop1 = op1, result = mul_result;
1681       bool negate_p = false;
1682
1683       if (is_gimple_debug (use_stmt))
1684         continue;
1685
1686       use_code = gimple_assign_rhs_code (use_stmt);
1687       if (use_code == NEGATE_EXPR)
1688         {
1689           result = gimple_assign_lhs (use_stmt);
1690           single_imm_use (gimple_assign_lhs (use_stmt), &use_p, &neguse_stmt);
1691           gsi_remove (&gsi, true);
1692           release_defs (use_stmt);
1693
1694           use_stmt = neguse_stmt;
1695           gsi = gsi_for_stmt (use_stmt);
1696           use_code = gimple_assign_rhs_code (use_stmt);
1697           negate_p = true;
1698         }
1699
1700       if (gimple_assign_rhs1 (use_stmt) == result)
1701         {
1702           addop = gimple_assign_rhs2 (use_stmt);
1703           /* a * b - c -> a * b + (-c)  */
1704           if (gimple_assign_rhs_code (use_stmt) == MINUS_EXPR)
1705             addop = force_gimple_operand_gsi (&gsi,
1706                                               build1 (NEGATE_EXPR,
1707                                                       type, addop),
1708                                               true, NULL_TREE, true,
1709                                               GSI_SAME_STMT);
1710         }
1711       else
1712         {
1713           addop = gimple_assign_rhs1 (use_stmt);
1714           /* a - b * c -> (-b) * c + a */
1715           if (gimple_assign_rhs_code (use_stmt) == MINUS_EXPR)
1716             negate_p = !negate_p;
1717         }
1718
1719       if (negate_p)
1720         mulop1 = force_gimple_operand_gsi (&gsi,
1721                                            build1 (NEGATE_EXPR,
1722                                                    type, mulop1),
1723                                            true, NULL_TREE, true,
1724                                            GSI_SAME_STMT);
1725
1726       fma_stmt = gimple_build_assign_with_ops3 (FMA_EXPR,
1727                                                 gimple_assign_lhs (use_stmt),
1728                                                 mulop1, op2,
1729                                                 addop);
1730       gsi_replace (&gsi, fma_stmt, true);
1731       widen_mul_stats.fmas_inserted++;
1732     }
1733
1734   return true;
1735 }
1736
1737 /* Find integer multiplications where the operands are extended from
1738    smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR
1739    where appropriate.  */
1740
1741 static unsigned int
1742 execute_optimize_widening_mul (void)
1743 {
1744   basic_block bb;
1745   bool cfg_changed = false;
1746
1747   memset (&widen_mul_stats, 0, sizeof (widen_mul_stats));
1748
1749   FOR_EACH_BB (bb)
1750     {
1751       gimple_stmt_iterator gsi;
1752
1753       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
1754         {
1755           gimple stmt = gsi_stmt (gsi);
1756           enum tree_code code;
1757
1758           if (is_gimple_assign (stmt))
1759             {
1760               code = gimple_assign_rhs_code (stmt);
1761               switch (code)
1762                 {
1763                 case MULT_EXPR:
1764                   if (!convert_mult_to_widen (stmt)
1765                       && convert_mult_to_fma (stmt,
1766                                               gimple_assign_rhs1 (stmt),
1767                                               gimple_assign_rhs2 (stmt)))
1768                     {
1769                       gsi_remove (&gsi, true);
1770                       release_defs (stmt);
1771                       continue;
1772                     }
1773                   break;
1774
1775                 case PLUS_EXPR:
1776                 case MINUS_EXPR:
1777                   convert_plusminus_to_widen (&gsi, stmt, code);
1778                   break;
1779
1780                 default:;
1781                 }
1782             }
1783           else if (is_gimple_call (stmt)
1784                    && gimple_call_lhs (stmt))
1785             {
1786               tree fndecl = gimple_call_fndecl (stmt);
1787               if (fndecl
1788                   && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1789                 {
1790                   switch (DECL_FUNCTION_CODE (fndecl))
1791                     {
1792                       case BUILT_IN_POWF:
1793                       case BUILT_IN_POW:
1794                       case BUILT_IN_POWL:
1795                         if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
1796                             && REAL_VALUES_EQUAL
1797                                  (TREE_REAL_CST (gimple_call_arg (stmt, 1)),
1798                                   dconst2)
1799                             && convert_mult_to_fma (stmt,
1800                                                     gimple_call_arg (stmt, 0),
1801                                                     gimple_call_arg (stmt, 0)))
1802                           {
1803                             unlink_stmt_vdef (stmt);
1804                             gsi_remove (&gsi, true);
1805                             release_defs (stmt);
1806                             if (gimple_purge_dead_eh_edges (bb))
1807                               cfg_changed = true;
1808                             continue;
1809                           }
1810                           break;
1811
1812                       default:;
1813                     }
1814                 }
1815             }
1816           gsi_next (&gsi);
1817         }
1818     }
1819
1820   statistics_counter_event (cfun, "widening multiplications inserted",
1821                             widen_mul_stats.widen_mults_inserted);
1822   statistics_counter_event (cfun, "widening maccs inserted",
1823                             widen_mul_stats.maccs_inserted);
1824   statistics_counter_event (cfun, "fused multiply-adds inserted",
1825                             widen_mul_stats.fmas_inserted);
1826
1827   return cfg_changed ? TODO_cleanup_cfg : 0;
1828 }
1829
1830 static bool
1831 gate_optimize_widening_mul (void)
1832 {
1833   return flag_expensive_optimizations && optimize;
1834 }
1835
1836 struct gimple_opt_pass pass_optimize_widening_mul =
1837 {
1838  {
1839   GIMPLE_PASS,
1840   "widening_mul",                       /* name */
1841   gate_optimize_widening_mul,           /* gate */
1842   execute_optimize_widening_mul,        /* execute */
1843   NULL,                                 /* sub */
1844   NULL,                                 /* next */
1845   0,                                    /* static_pass_number */
1846   TV_NONE,                              /* tv_id */
1847   PROP_ssa,                             /* properties_required */
1848   0,                                    /* properties_provided */
1849   0,                                    /* properties_destroyed */
1850   0,                                    /* todo_flags_start */
1851   TODO_verify_ssa
1852   | TODO_verify_stmts
1853   | TODO_dump_func
1854   | TODO_update_ssa                     /* todo_flags_finish */
1855  }
1856 };