OSDN Git Service

2011-05-27 Alexander Monakov <amonakov@ispras.ru>
[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, 2011
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 /* To evaluate powi(x,n), the floating point value x raised to the
799    constant integer exponent n, we use a hybrid algorithm that
800    combines the "window method" with look-up tables.  For an
801    introduction to exponentiation algorithms and "addition chains",
802    see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
803    "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
804    3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation
805    Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998.  */
806
807 /* Provide a default value for POWI_MAX_MULTS, the maximum number of
808    multiplications to inline before calling the system library's pow
809    function.  powi(x,n) requires at worst 2*bits(n)-2 multiplications,
810    so this default never requires calling pow, powf or powl.  */
811
812 #ifndef POWI_MAX_MULTS
813 #define POWI_MAX_MULTS  (2*HOST_BITS_PER_WIDE_INT-2)
814 #endif
815
816 /* The size of the "optimal power tree" lookup table.  All
817    exponents less than this value are simply looked up in the
818    powi_table below.  This threshold is also used to size the
819    cache of pseudo registers that hold intermediate results.  */
820 #define POWI_TABLE_SIZE 256
821
822 /* The size, in bits of the window, used in the "window method"
823    exponentiation algorithm.  This is equivalent to a radix of
824    (1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method".  */
825 #define POWI_WINDOW_SIZE 3
826
827 /* The following table is an efficient representation of an
828    "optimal power tree".  For each value, i, the corresponding
829    value, j, in the table states than an optimal evaluation
830    sequence for calculating pow(x,i) can be found by evaluating
831    pow(x,j)*pow(x,i-j).  An optimal power tree for the first
832    100 integers is given in Knuth's "Seminumerical algorithms".  */
833
834 static const unsigned char powi_table[POWI_TABLE_SIZE] =
835   {
836       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
837       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
838       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
839      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
840      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
841      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
842      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
843      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
844      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
845      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
846      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
847      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
848      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
849      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
850      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
851      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
852      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
853      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
854      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
855      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
856      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
857      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
858      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
859      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
860      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
861     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
862     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
863     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
864     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
865     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
866     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
867     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
868   };
869
870
871 /* Return the number of multiplications required to calculate
872    powi(x,n) where n is less than POWI_TABLE_SIZE.  This is a
873    subroutine of powi_cost.  CACHE is an array indicating
874    which exponents have already been calculated.  */
875
876 static int
877 powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache)
878 {
879   /* If we've already calculated this exponent, then this evaluation
880      doesn't require any additional multiplications.  */
881   if (cache[n])
882     return 0;
883
884   cache[n] = true;
885   return powi_lookup_cost (n - powi_table[n], cache)
886          + powi_lookup_cost (powi_table[n], cache) + 1;
887 }
888
889 /* Return the number of multiplications required to calculate
890    powi(x,n) for an arbitrary x, given the exponent N.  This
891    function needs to be kept in sync with powi_as_mults below.  */
892
893 static int
894 powi_cost (HOST_WIDE_INT n)
895 {
896   bool cache[POWI_TABLE_SIZE];
897   unsigned HOST_WIDE_INT digit;
898   unsigned HOST_WIDE_INT val;
899   int result;
900
901   if (n == 0)
902     return 0;
903
904   /* Ignore the reciprocal when calculating the cost.  */
905   val = (n < 0) ? -n : n;
906
907   /* Initialize the exponent cache.  */
908   memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool));
909   cache[1] = true;
910
911   result = 0;
912
913   while (val >= POWI_TABLE_SIZE)
914     {
915       if (val & 1)
916         {
917           digit = val & ((1 << POWI_WINDOW_SIZE) - 1);
918           result += powi_lookup_cost (digit, cache)
919                     + POWI_WINDOW_SIZE + 1;
920           val >>= POWI_WINDOW_SIZE;
921         }
922       else
923         {
924           val >>= 1;
925           result++;
926         }
927     }
928
929   return result + powi_lookup_cost (val, cache);
930 }
931
932 /* Recursive subroutine of powi_as_mults.  This function takes the
933    array, CACHE, of already calculated exponents and an exponent N and
934    returns a tree that corresponds to CACHE[1]**N, with type TYPE.  */
935
936 static tree
937 powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type,
938                  HOST_WIDE_INT n, tree *cache, tree target)
939 {
940   tree op0, op1, ssa_target;
941   unsigned HOST_WIDE_INT digit;
942   gimple mult_stmt;
943
944   if (n < POWI_TABLE_SIZE && cache[n])
945     return cache[n];
946
947   ssa_target = make_ssa_name (target, NULL);
948
949   if (n < POWI_TABLE_SIZE)
950     {
951       cache[n] = ssa_target;
952       op0 = powi_as_mults_1 (gsi, loc, type, n - powi_table[n], cache, target);
953       op1 = powi_as_mults_1 (gsi, loc, type, powi_table[n], cache, target);
954     }
955   else if (n & 1)
956     {
957       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
958       op0 = powi_as_mults_1 (gsi, loc, type, n - digit, cache, target);
959       op1 = powi_as_mults_1 (gsi, loc, type, digit, cache, target);
960     }
961   else
962     {
963       op0 = powi_as_mults_1 (gsi, loc, type, n >> 1, cache, target);
964       op1 = op0;
965     }
966
967   mult_stmt = gimple_build_assign_with_ops (MULT_EXPR, ssa_target, op0, op1);
968   gsi_insert_before (gsi, mult_stmt, GSI_SAME_STMT);
969
970   return ssa_target;
971 }
972
973 /* Convert ARG0**N to a tree of multiplications of ARG0 with itself.
974    This function needs to be kept in sync with powi_cost above.  */
975
976 static tree
977 powi_as_mults (gimple_stmt_iterator *gsi, location_t loc,
978                tree arg0, HOST_WIDE_INT n)
979 {
980   tree cache[POWI_TABLE_SIZE], result, type = TREE_TYPE (arg0), target;
981   gimple div_stmt;
982
983   if (n == 0)
984     return build_real (type, dconst1);
985
986   memset (cache, 0,  sizeof (cache));
987   cache[1] = arg0;
988
989   target = create_tmp_var (type, "powmult");
990   add_referenced_var (target);
991
992   result = powi_as_mults_1 (gsi, loc, type, (n < 0) ? -n : n, cache, target);
993
994   if (n >= 0)
995     return result;
996
997   /* If the original exponent was negative, reciprocate the result.  */
998   target = make_ssa_name (target, NULL);
999   div_stmt = gimple_build_assign_with_ops (RDIV_EXPR, target, 
1000                                            build_real (type, dconst1),
1001                                            result);
1002   gsi_insert_before (gsi, div_stmt, GSI_SAME_STMT);
1003
1004   return target;
1005 }
1006
1007 /* ARG0 and N are the two arguments to a powi builtin in GSI with
1008    location info LOC.  If the arguments are appropriate, create an
1009    equivalent sequence of statements prior to GSI using an optimal
1010    number of multiplications, and return an expession holding the
1011    result.  */
1012
1013 static tree
1014 gimple_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc, 
1015                             tree arg0, HOST_WIDE_INT n)
1016 {
1017   /* Avoid largest negative number.  */
1018   if (n != -n
1019       && ((n >= -1 && n <= 2)
1020           || (optimize_function_for_speed_p (cfun)
1021               && powi_cost (n) <= POWI_MAX_MULTS)))
1022     return powi_as_mults (gsi, loc, arg0, n);
1023
1024   return NULL_TREE;
1025 }
1026
1027 /* ARG0 and ARG1 are the two arguments to a pow builtin call in GSI
1028    with location info LOC.  If possible, create an equivalent and
1029    less expensive sequence of statements prior to GSI, and return an
1030    expession holding the result.  */
1031
1032 static tree
1033 gimple_expand_builtin_pow (gimple_stmt_iterator *gsi, location_t loc, 
1034                            tree arg0, tree arg1)
1035 {
1036   REAL_VALUE_TYPE c, cint;
1037   HOST_WIDE_INT n;
1038
1039   /* If the exponent isn't a constant, there's nothing of interest
1040      to be done.  */
1041   if (TREE_CODE (arg1) != REAL_CST)
1042     return NULL_TREE;
1043
1044   /* If the exponent is equivalent to an integer, expand it into
1045      multiplies when profitable.  */
1046   c = TREE_REAL_CST (arg1);
1047   n = real_to_integer (&c);
1048   real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
1049
1050   if (real_identical (&c, &cint)
1051       && ((n >= -1 && n <= 2)
1052           || (flag_unsafe_math_optimizations
1053               && optimize_insn_for_speed_p ()
1054               && powi_cost (n) <= POWI_MAX_MULTS)))
1055     return gimple_expand_builtin_powi (gsi, loc, arg0, n);
1056
1057   return NULL_TREE;
1058 }
1059
1060 /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1
1061    on the SSA_NAME argument of each of them.  Also expand powi(x,n) into
1062    an optimal number of multiplies, when n is a constant.  */
1063
1064 static unsigned int
1065 execute_cse_sincos (void)
1066 {
1067   basic_block bb;
1068   bool cfg_changed = false;
1069
1070   calculate_dominance_info (CDI_DOMINATORS);
1071   memset (&sincos_stats, 0, sizeof (sincos_stats));
1072
1073   FOR_EACH_BB (bb)
1074     {
1075       gimple_stmt_iterator gsi;
1076
1077       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1078         {
1079           gimple stmt = gsi_stmt (gsi);
1080           tree fndecl;
1081
1082           if (is_gimple_call (stmt)
1083               && gimple_call_lhs (stmt)
1084               && (fndecl = gimple_call_fndecl (stmt))
1085               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1086             {
1087               tree arg, arg0, arg1, result;
1088               HOST_WIDE_INT n;
1089               location_t loc;
1090
1091               switch (DECL_FUNCTION_CODE (fndecl))
1092                 {
1093                 CASE_FLT_FN (BUILT_IN_COS):
1094                 CASE_FLT_FN (BUILT_IN_SIN):
1095                 CASE_FLT_FN (BUILT_IN_CEXPI):
1096                   /* Make sure we have either sincos or cexp.  */
1097                   if (!TARGET_HAS_SINCOS && !TARGET_C99_FUNCTIONS)
1098                     break;
1099
1100                   arg = gimple_call_arg (stmt, 0);
1101                   if (TREE_CODE (arg) == SSA_NAME)
1102                     cfg_changed |= execute_cse_sincos_1 (arg);
1103                   break;
1104
1105                 CASE_FLT_FN (BUILT_IN_POW):
1106                   arg0 = gimple_call_arg (stmt, 0);
1107                   arg1 = gimple_call_arg (stmt, 1);
1108
1109                   loc = gimple_location (stmt);
1110                   result = gimple_expand_builtin_pow (&gsi, loc, arg0, arg1);
1111
1112                   if (result)
1113                     {
1114                       tree lhs = gimple_get_lhs (stmt);
1115                       gimple new_stmt = gimple_build_assign (lhs, result);
1116                       gimple_set_location (new_stmt, loc);
1117                       unlink_stmt_vdef (stmt);
1118                       gsi_replace (&gsi, new_stmt, true);
1119                     }
1120                   break;
1121
1122                 CASE_FLT_FN (BUILT_IN_POWI):
1123                   arg0 = gimple_call_arg (stmt, 0);
1124                   arg1 = gimple_call_arg (stmt, 1);
1125                   if (!host_integerp (arg1, 0))
1126                     break;
1127
1128                   n = TREE_INT_CST_LOW (arg1);
1129                   loc = gimple_location (stmt);
1130                   result = gimple_expand_builtin_powi (&gsi, loc, arg0, n);
1131
1132                   if (result)
1133                     {
1134                       tree lhs = gimple_get_lhs (stmt);
1135                       gimple new_stmt = gimple_build_assign (lhs, result);
1136                       gimple_set_location (new_stmt, loc);
1137                       unlink_stmt_vdef (stmt);
1138                       gsi_replace (&gsi, new_stmt, true);
1139                     }
1140                   break;
1141
1142                 default:;
1143                 }
1144             }
1145         }
1146     }
1147
1148   statistics_counter_event (cfun, "sincos statements inserted",
1149                             sincos_stats.inserted);
1150
1151   free_dominance_info (CDI_DOMINATORS);
1152   return cfg_changed ? TODO_cleanup_cfg : 0;
1153 }
1154
1155 static bool
1156 gate_cse_sincos (void)
1157 {
1158   /* We no longer require either sincos or cexp, since powi expansion
1159      piggybacks on this pass.  */
1160   return optimize;
1161 }
1162
1163 struct gimple_opt_pass pass_cse_sincos =
1164 {
1165  {
1166   GIMPLE_PASS,
1167   "sincos",                             /* name */
1168   gate_cse_sincos,                      /* gate */
1169   execute_cse_sincos,                   /* execute */
1170   NULL,                                 /* sub */
1171   NULL,                                 /* next */
1172   0,                                    /* static_pass_number */
1173   TV_NONE,                              /* tv_id */
1174   PROP_ssa,                             /* properties_required */
1175   0,                                    /* properties_provided */
1176   0,                                    /* properties_destroyed */
1177   0,                                    /* todo_flags_start */
1178   TODO_dump_func | TODO_update_ssa | TODO_verify_ssa
1179     | TODO_verify_stmts                 /* todo_flags_finish */
1180  }
1181 };
1182
1183 /* A symbolic number is used to detect byte permutation and selection
1184    patterns.  Therefore the field N contains an artificial number
1185    consisting of byte size markers:
1186
1187    0    - byte has the value 0
1188    1..size - byte contains the content of the byte
1189    number indexed with that value minus one  */
1190
1191 struct symbolic_number {
1192   unsigned HOST_WIDEST_INT n;
1193   int size;
1194 };
1195
1196 /* Perform a SHIFT or ROTATE operation by COUNT bits on symbolic
1197    number N.  Return false if the requested operation is not permitted
1198    on a symbolic number.  */
1199
1200 static inline bool
1201 do_shift_rotate (enum tree_code code,
1202                  struct symbolic_number *n,
1203                  int count)
1204 {
1205   if (count % 8 != 0)
1206     return false;
1207
1208   /* Zero out the extra bits of N in order to avoid them being shifted
1209      into the significant bits.  */
1210   if (n->size < (int)sizeof (HOST_WIDEST_INT))
1211     n->n &= ((unsigned HOST_WIDEST_INT)1 << (n->size * BITS_PER_UNIT)) - 1;
1212
1213   switch (code)
1214     {
1215     case LSHIFT_EXPR:
1216       n->n <<= count;
1217       break;
1218     case RSHIFT_EXPR:
1219       n->n >>= count;
1220       break;
1221     case LROTATE_EXPR:
1222       n->n = (n->n << count) | (n->n >> ((n->size * BITS_PER_UNIT) - count));
1223       break;
1224     case RROTATE_EXPR:
1225       n->n = (n->n >> count) | (n->n << ((n->size * BITS_PER_UNIT) - count));
1226       break;
1227     default:
1228       return false;
1229     }
1230   return true;
1231 }
1232
1233 /* Perform sanity checking for the symbolic number N and the gimple
1234    statement STMT.  */
1235
1236 static inline bool
1237 verify_symbolic_number_p (struct symbolic_number *n, gimple stmt)
1238 {
1239   tree lhs_type;
1240
1241   lhs_type = gimple_expr_type (stmt);
1242
1243   if (TREE_CODE (lhs_type) != INTEGER_TYPE)
1244     return false;
1245
1246   if (TYPE_PRECISION (lhs_type) != n->size * BITS_PER_UNIT)
1247     return false;
1248
1249   return true;
1250 }
1251
1252 /* find_bswap_1 invokes itself recursively with N and tries to perform
1253    the operation given by the rhs of STMT on the result.  If the
1254    operation could successfully be executed the function returns the
1255    tree expression of the source operand and NULL otherwise.  */
1256
1257 static tree
1258 find_bswap_1 (gimple stmt, struct symbolic_number *n, int limit)
1259 {
1260   enum tree_code code;
1261   tree rhs1, rhs2 = NULL;
1262   gimple rhs1_stmt, rhs2_stmt;
1263   tree source_expr1;
1264   enum gimple_rhs_class rhs_class;
1265
1266   if (!limit || !is_gimple_assign (stmt))
1267     return NULL_TREE;
1268
1269   rhs1 = gimple_assign_rhs1 (stmt);
1270
1271   if (TREE_CODE (rhs1) != SSA_NAME)
1272     return NULL_TREE;
1273
1274   code = gimple_assign_rhs_code (stmt);
1275   rhs_class = gimple_assign_rhs_class (stmt);
1276   rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
1277
1278   if (rhs_class == GIMPLE_BINARY_RHS)
1279     rhs2 = gimple_assign_rhs2 (stmt);
1280
1281   /* Handle unary rhs and binary rhs with integer constants as second
1282      operand.  */
1283
1284   if (rhs_class == GIMPLE_UNARY_RHS
1285       || (rhs_class == GIMPLE_BINARY_RHS
1286           && TREE_CODE (rhs2) == INTEGER_CST))
1287     {
1288       if (code != BIT_AND_EXPR
1289           && code != LSHIFT_EXPR
1290           && code != RSHIFT_EXPR
1291           && code != LROTATE_EXPR
1292           && code != RROTATE_EXPR
1293           && code != NOP_EXPR
1294           && code != CONVERT_EXPR)
1295         return NULL_TREE;
1296
1297       source_expr1 = find_bswap_1 (rhs1_stmt, n, limit - 1);
1298
1299       /* If find_bswap_1 returned NULL STMT is a leaf node and we have
1300          to initialize the symbolic number.  */
1301       if (!source_expr1)
1302         {
1303           /* Set up the symbolic number N by setting each byte to a
1304              value between 1 and the byte size of rhs1.  The highest
1305              order byte is set to n->size and the lowest order
1306              byte to 1.  */
1307           n->size = TYPE_PRECISION (TREE_TYPE (rhs1));
1308           if (n->size % BITS_PER_UNIT != 0)
1309             return NULL_TREE;
1310           n->size /= BITS_PER_UNIT;
1311           n->n = (sizeof (HOST_WIDEST_INT) < 8 ? 0 :
1312                   (unsigned HOST_WIDEST_INT)0x08070605 << 32 | 0x04030201);
1313
1314           if (n->size < (int)sizeof (HOST_WIDEST_INT))
1315             n->n &= ((unsigned HOST_WIDEST_INT)1 <<
1316                      (n->size * BITS_PER_UNIT)) - 1;
1317
1318           source_expr1 = rhs1;
1319         }
1320
1321       switch (code)
1322         {
1323         case BIT_AND_EXPR:
1324           {
1325             int i;
1326             unsigned HOST_WIDEST_INT val = widest_int_cst_value (rhs2);
1327             unsigned HOST_WIDEST_INT tmp = val;
1328
1329             /* Only constants masking full bytes are allowed.  */
1330             for (i = 0; i < n->size; i++, tmp >>= BITS_PER_UNIT)
1331               if ((tmp & 0xff) != 0 && (tmp & 0xff) != 0xff)
1332                 return NULL_TREE;
1333
1334             n->n &= val;
1335           }
1336           break;
1337         case LSHIFT_EXPR:
1338         case RSHIFT_EXPR:
1339         case LROTATE_EXPR:
1340         case RROTATE_EXPR:
1341           if (!do_shift_rotate (code, n, (int)TREE_INT_CST_LOW (rhs2)))
1342             return NULL_TREE;
1343           break;
1344         CASE_CONVERT:
1345           {
1346             int type_size;
1347
1348             type_size = TYPE_PRECISION (gimple_expr_type (stmt));
1349             if (type_size % BITS_PER_UNIT != 0)
1350               return NULL_TREE;
1351
1352             if (type_size / BITS_PER_UNIT < (int)(sizeof (HOST_WIDEST_INT)))
1353               {
1354                 /* If STMT casts to a smaller type mask out the bits not
1355                    belonging to the target type.  */
1356                 n->n &= ((unsigned HOST_WIDEST_INT)1 << type_size) - 1;
1357               }
1358             n->size = type_size / BITS_PER_UNIT;
1359           }
1360           break;
1361         default:
1362           return NULL_TREE;
1363         };
1364       return verify_symbolic_number_p (n, stmt) ? source_expr1 : NULL;
1365     }
1366
1367   /* Handle binary rhs.  */
1368
1369   if (rhs_class == GIMPLE_BINARY_RHS)
1370     {
1371       struct symbolic_number n1, n2;
1372       tree source_expr2;
1373
1374       if (code != BIT_IOR_EXPR)
1375         return NULL_TREE;
1376
1377       if (TREE_CODE (rhs2) != SSA_NAME)
1378         return NULL_TREE;
1379
1380       rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
1381
1382       switch (code)
1383         {
1384         case BIT_IOR_EXPR:
1385           source_expr1 = find_bswap_1 (rhs1_stmt, &n1, limit - 1);
1386
1387           if (!source_expr1)
1388             return NULL_TREE;
1389
1390           source_expr2 = find_bswap_1 (rhs2_stmt, &n2, limit - 1);
1391
1392           if (source_expr1 != source_expr2
1393               || n1.size != n2.size)
1394             return NULL_TREE;
1395
1396           n->size = n1.size;
1397           n->n = n1.n | n2.n;
1398
1399           if (!verify_symbolic_number_p (n, stmt))
1400             return NULL_TREE;
1401
1402           break;
1403         default:
1404           return NULL_TREE;
1405         }
1406       return source_expr1;
1407     }
1408   return NULL_TREE;
1409 }
1410
1411 /* Check if STMT completes a bswap implementation consisting of ORs,
1412    SHIFTs and ANDs.  Return the source tree expression on which the
1413    byte swap is performed and NULL if no bswap was found.  */
1414
1415 static tree
1416 find_bswap (gimple stmt)
1417 {
1418 /* The number which the find_bswap result should match in order to
1419    have a full byte swap.  The number is shifted to the left according
1420    to the size of the symbolic number before using it.  */
1421   unsigned HOST_WIDEST_INT cmp =
1422     sizeof (HOST_WIDEST_INT) < 8 ? 0 :
1423     (unsigned HOST_WIDEST_INT)0x01020304 << 32 | 0x05060708;
1424
1425   struct symbolic_number n;
1426   tree source_expr;
1427
1428   /* The last parameter determines the depth search limit.  It usually
1429      correlates directly to the number of bytes to be touched.  We
1430      increase that number by one here in order to also cover signed ->
1431      unsigned conversions of the src operand as can be seen in
1432      libgcc.  */
1433   source_expr =  find_bswap_1 (stmt, &n,
1434                                TREE_INT_CST_LOW (
1435                                  TYPE_SIZE_UNIT (gimple_expr_type (stmt))) + 1);
1436
1437   if (!source_expr)
1438     return NULL_TREE;
1439
1440   /* Zero out the extra bits of N and CMP.  */
1441   if (n.size < (int)sizeof (HOST_WIDEST_INT))
1442     {
1443       unsigned HOST_WIDEST_INT mask =
1444         ((unsigned HOST_WIDEST_INT)1 << (n.size * BITS_PER_UNIT)) - 1;
1445
1446       n.n &= mask;
1447       cmp >>= (sizeof (HOST_WIDEST_INT) - n.size) * BITS_PER_UNIT;
1448     }
1449
1450   /* A complete byte swap should make the symbolic number to start
1451      with the largest digit in the highest order byte.  */
1452   if (cmp != n.n)
1453     return NULL_TREE;
1454
1455   return source_expr;
1456 }
1457
1458 /* Find manual byte swap implementations and turn them into a bswap
1459    builtin invokation.  */
1460
1461 static unsigned int
1462 execute_optimize_bswap (void)
1463 {
1464   basic_block bb;
1465   bool bswap32_p, bswap64_p;
1466   bool changed = false;
1467   tree bswap32_type = NULL_TREE, bswap64_type = NULL_TREE;
1468
1469   if (BITS_PER_UNIT != 8)
1470     return 0;
1471
1472   if (sizeof (HOST_WIDEST_INT) < 8)
1473     return 0;
1474
1475   bswap32_p = (built_in_decls[BUILT_IN_BSWAP32]
1476                && optab_handler (bswap_optab, SImode) != CODE_FOR_nothing);
1477   bswap64_p = (built_in_decls[BUILT_IN_BSWAP64]
1478                && (optab_handler (bswap_optab, DImode) != CODE_FOR_nothing
1479                    || (bswap32_p && word_mode == SImode)));
1480
1481   if (!bswap32_p && !bswap64_p)
1482     return 0;
1483
1484   /* Determine the argument type of the builtins.  The code later on
1485      assumes that the return and argument type are the same.  */
1486   if (bswap32_p)
1487     {
1488       tree fndecl = built_in_decls[BUILT_IN_BSWAP32];
1489       bswap32_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1490     }
1491
1492   if (bswap64_p)
1493     {
1494       tree fndecl = built_in_decls[BUILT_IN_BSWAP64];
1495       bswap64_type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1496     }
1497
1498   memset (&bswap_stats, 0, sizeof (bswap_stats));
1499
1500   FOR_EACH_BB (bb)
1501     {
1502       gimple_stmt_iterator gsi;
1503
1504       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1505         {
1506           gimple stmt = gsi_stmt (gsi);
1507           tree bswap_src, bswap_type;
1508           tree bswap_tmp;
1509           tree fndecl = NULL_TREE;
1510           int type_size;
1511           gimple call;
1512
1513           if (!is_gimple_assign (stmt)
1514               || gimple_assign_rhs_code (stmt) != BIT_IOR_EXPR)
1515             continue;
1516
1517           type_size = TYPE_PRECISION (gimple_expr_type (stmt));
1518
1519           switch (type_size)
1520             {
1521             case 32:
1522               if (bswap32_p)
1523                 {
1524                   fndecl = built_in_decls[BUILT_IN_BSWAP32];
1525                   bswap_type = bswap32_type;
1526                 }
1527               break;
1528             case 64:
1529               if (bswap64_p)
1530                 {
1531                   fndecl = built_in_decls[BUILT_IN_BSWAP64];
1532                   bswap_type = bswap64_type;
1533                 }
1534               break;
1535             default:
1536               continue;
1537             }
1538
1539           if (!fndecl)
1540             continue;
1541
1542           bswap_src = find_bswap (stmt);
1543
1544           if (!bswap_src)
1545             continue;
1546
1547           changed = true;
1548           if (type_size == 32)
1549             bswap_stats.found_32bit++;
1550           else
1551             bswap_stats.found_64bit++;
1552
1553           bswap_tmp = bswap_src;
1554
1555           /* Convert the src expression if necessary.  */
1556           if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
1557             {
1558               gimple convert_stmt;
1559
1560               bswap_tmp = create_tmp_var (bswap_type, "bswapsrc");
1561               add_referenced_var (bswap_tmp);
1562               bswap_tmp = make_ssa_name (bswap_tmp, NULL);
1563
1564               convert_stmt = gimple_build_assign_with_ops (
1565                                CONVERT_EXPR, bswap_tmp, bswap_src, NULL);
1566               gsi_insert_before (&gsi, convert_stmt, GSI_SAME_STMT);
1567             }
1568
1569           call = gimple_build_call (fndecl, 1, bswap_tmp);
1570
1571           bswap_tmp = gimple_assign_lhs (stmt);
1572
1573           /* Convert the result if necessary.  */
1574           if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp), bswap_type))
1575             {
1576               gimple convert_stmt;
1577
1578               bswap_tmp = create_tmp_var (bswap_type, "bswapdst");
1579               add_referenced_var (bswap_tmp);
1580               bswap_tmp = make_ssa_name (bswap_tmp, NULL);
1581               convert_stmt = gimple_build_assign_with_ops (
1582                                CONVERT_EXPR, gimple_assign_lhs (stmt), bswap_tmp, NULL);
1583               gsi_insert_after (&gsi, convert_stmt, GSI_SAME_STMT);
1584             }
1585
1586           gimple_call_set_lhs (call, bswap_tmp);
1587
1588           if (dump_file)
1589             {
1590               fprintf (dump_file, "%d bit bswap implementation found at: ",
1591                        (int)type_size);
1592               print_gimple_stmt (dump_file, stmt, 0, 0);
1593             }
1594
1595           gsi_insert_after (&gsi, call, GSI_SAME_STMT);
1596           gsi_remove (&gsi, true);
1597         }
1598     }
1599
1600   statistics_counter_event (cfun, "32-bit bswap implementations found",
1601                             bswap_stats.found_32bit);
1602   statistics_counter_event (cfun, "64-bit bswap implementations found",
1603                             bswap_stats.found_64bit);
1604
1605   return (changed ? TODO_dump_func | TODO_update_ssa | TODO_verify_ssa
1606           | TODO_verify_stmts : 0);
1607 }
1608
1609 static bool
1610 gate_optimize_bswap (void)
1611 {
1612   return flag_expensive_optimizations && optimize;
1613 }
1614
1615 struct gimple_opt_pass pass_optimize_bswap =
1616 {
1617  {
1618   GIMPLE_PASS,
1619   "bswap",                              /* name */
1620   gate_optimize_bswap,                  /* gate */
1621   execute_optimize_bswap,               /* execute */
1622   NULL,                                 /* sub */
1623   NULL,                                 /* next */
1624   0,                                    /* static_pass_number */
1625   TV_NONE,                              /* tv_id */
1626   PROP_ssa,                             /* properties_required */
1627   0,                                    /* properties_provided */
1628   0,                                    /* properties_destroyed */
1629   0,                                    /* todo_flags_start */
1630   0                                     /* todo_flags_finish */
1631  }
1632 };
1633
1634 /* Return true if RHS is a suitable operand for a widening multiplication.
1635    There are two cases:
1636
1637      - RHS makes some value twice as wide.  Store that value in *NEW_RHS_OUT
1638        if so, and store its type in *TYPE_OUT.
1639
1640      - RHS is an integer constant.  Store that value in *NEW_RHS_OUT if so,
1641        but leave *TYPE_OUT untouched.  */
1642
1643 static bool
1644 is_widening_mult_rhs_p (tree rhs, tree *type_out, tree *new_rhs_out)
1645 {
1646   gimple stmt;
1647   tree type, type1, rhs1;
1648   enum tree_code rhs_code;
1649
1650   if (TREE_CODE (rhs) == SSA_NAME)
1651     {
1652       type = TREE_TYPE (rhs);
1653       stmt = SSA_NAME_DEF_STMT (rhs);
1654       if (!is_gimple_assign (stmt))
1655         return false;
1656
1657       rhs_code = gimple_assign_rhs_code (stmt);
1658       if (TREE_CODE (type) == INTEGER_TYPE
1659           ? !CONVERT_EXPR_CODE_P (rhs_code)
1660           : rhs_code != FIXED_CONVERT_EXPR)
1661         return false;
1662
1663       rhs1 = gimple_assign_rhs1 (stmt);
1664       type1 = TREE_TYPE (rhs1);
1665       if (TREE_CODE (type1) != TREE_CODE (type)
1666           || TYPE_PRECISION (type1) * 2 != TYPE_PRECISION (type))
1667         return false;
1668
1669       *new_rhs_out = rhs1;
1670       *type_out = type1;
1671       return true;
1672     }
1673
1674   if (TREE_CODE (rhs) == INTEGER_CST)
1675     {
1676       *new_rhs_out = rhs;
1677       *type_out = NULL;
1678       return true;
1679     }
1680
1681   return false;
1682 }
1683
1684 /* Return true if STMT performs a widening multiplication.  If so,
1685    store the unwidened types of the operands in *TYPE1_OUT and *TYPE2_OUT
1686    respectively.  Also fill *RHS1_OUT and *RHS2_OUT such that converting
1687    those operands to types *TYPE1_OUT and *TYPE2_OUT would give the
1688    operands of the multiplication.  */
1689
1690 static bool
1691 is_widening_mult_p (gimple stmt,
1692                     tree *type1_out, tree *rhs1_out,
1693                     tree *type2_out, tree *rhs2_out)
1694 {
1695   tree type;
1696
1697   type = TREE_TYPE (gimple_assign_lhs (stmt));
1698   if (TREE_CODE (type) != INTEGER_TYPE
1699       && TREE_CODE (type) != FIXED_POINT_TYPE)
1700     return false;
1701
1702   if (!is_widening_mult_rhs_p (gimple_assign_rhs1 (stmt), type1_out, rhs1_out))
1703     return false;
1704
1705   if (!is_widening_mult_rhs_p (gimple_assign_rhs2 (stmt), type2_out, rhs2_out))
1706     return false;
1707
1708   if (*type1_out == NULL)
1709     {
1710       if (*type2_out == NULL || !int_fits_type_p (*rhs1_out, *type2_out))
1711         return false;
1712       *type1_out = *type2_out;
1713     }
1714
1715   if (*type2_out == NULL)
1716     {
1717       if (!int_fits_type_p (*rhs2_out, *type1_out))
1718         return false;
1719       *type2_out = *type1_out;
1720     }
1721
1722   return true;
1723 }
1724
1725 /* Process a single gimple statement STMT, which has a MULT_EXPR as
1726    its rhs, and try to convert it into a WIDEN_MULT_EXPR.  The return
1727    value is true iff we converted the statement.  */
1728
1729 static bool
1730 convert_mult_to_widen (gimple stmt)
1731 {
1732   tree lhs, rhs1, rhs2, type, type1, type2;
1733   enum insn_code handler;
1734
1735   lhs = gimple_assign_lhs (stmt);
1736   type = TREE_TYPE (lhs);
1737   if (TREE_CODE (type) != INTEGER_TYPE)
1738     return false;
1739
1740   if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2))
1741     return false;
1742
1743   if (TYPE_UNSIGNED (type1) && TYPE_UNSIGNED (type2))
1744     handler = optab_handler (umul_widen_optab, TYPE_MODE (type));
1745   else if (!TYPE_UNSIGNED (type1) && !TYPE_UNSIGNED (type2))
1746     handler = optab_handler (smul_widen_optab, TYPE_MODE (type));
1747   else
1748     handler = optab_handler (usmul_widen_optab, TYPE_MODE (type));
1749
1750   if (handler == CODE_FOR_nothing)
1751     return false;
1752
1753   gimple_assign_set_rhs1 (stmt, fold_convert (type1, rhs1));
1754   gimple_assign_set_rhs2 (stmt, fold_convert (type2, rhs2));
1755   gimple_assign_set_rhs_code (stmt, WIDEN_MULT_EXPR);
1756   update_stmt (stmt);
1757   widen_mul_stats.widen_mults_inserted++;
1758   return true;
1759 }
1760
1761 /* Process a single gimple statement STMT, which is found at the
1762    iterator GSI and has a either a PLUS_EXPR or a MINUS_EXPR as its
1763    rhs (given by CODE), and try to convert it into a
1764    WIDEN_MULT_PLUS_EXPR or a WIDEN_MULT_MINUS_EXPR.  The return value
1765    is true iff we converted the statement.  */
1766
1767 static bool
1768 convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple stmt,
1769                             enum tree_code code)
1770 {
1771   gimple rhs1_stmt = NULL, rhs2_stmt = NULL;
1772   tree type, type1, type2;
1773   tree lhs, rhs1, rhs2, mult_rhs1, mult_rhs2, add_rhs;
1774   enum tree_code rhs1_code = ERROR_MARK, rhs2_code = ERROR_MARK;
1775   optab this_optab;
1776   enum tree_code wmult_code;
1777
1778   lhs = gimple_assign_lhs (stmt);
1779   type = TREE_TYPE (lhs);
1780   if (TREE_CODE (type) != INTEGER_TYPE
1781       && TREE_CODE (type) != FIXED_POINT_TYPE)
1782     return false;
1783
1784   if (code == MINUS_EXPR)
1785     wmult_code = WIDEN_MULT_MINUS_EXPR;
1786   else
1787     wmult_code = WIDEN_MULT_PLUS_EXPR;
1788
1789   rhs1 = gimple_assign_rhs1 (stmt);
1790   rhs2 = gimple_assign_rhs2 (stmt);
1791
1792   if (TREE_CODE (rhs1) == SSA_NAME)
1793     {
1794       rhs1_stmt = SSA_NAME_DEF_STMT (rhs1);
1795       if (is_gimple_assign (rhs1_stmt))
1796         rhs1_code = gimple_assign_rhs_code (rhs1_stmt);
1797     }
1798   else
1799     return false;
1800
1801   if (TREE_CODE (rhs2) == SSA_NAME)
1802     {
1803       rhs2_stmt = SSA_NAME_DEF_STMT (rhs2);
1804       if (is_gimple_assign (rhs2_stmt))
1805         rhs2_code = gimple_assign_rhs_code (rhs2_stmt);
1806     }
1807   else
1808     return false;
1809
1810   if (code == PLUS_EXPR && rhs1_code == MULT_EXPR)
1811     {
1812       if (!is_widening_mult_p (rhs1_stmt, &type1, &mult_rhs1,
1813                                &type2, &mult_rhs2))
1814         return false;
1815       add_rhs = rhs2;
1816     }
1817   else if (rhs2_code == MULT_EXPR)
1818     {
1819       if (!is_widening_mult_p (rhs2_stmt, &type1, &mult_rhs1,
1820                                &type2, &mult_rhs2))
1821         return false;
1822       add_rhs = rhs1;
1823     }
1824   else if (code == PLUS_EXPR && rhs1_code == WIDEN_MULT_EXPR)
1825     {
1826       mult_rhs1 = gimple_assign_rhs1 (rhs1_stmt);
1827       mult_rhs2 = gimple_assign_rhs2 (rhs1_stmt);
1828       type1 = TREE_TYPE (mult_rhs1);
1829       type2 = TREE_TYPE (mult_rhs2);
1830       add_rhs = rhs2;
1831     }
1832   else if (rhs2_code == WIDEN_MULT_EXPR)
1833     {
1834       mult_rhs1 = gimple_assign_rhs1 (rhs2_stmt);
1835       mult_rhs2 = gimple_assign_rhs2 (rhs2_stmt);
1836       type1 = TREE_TYPE (mult_rhs1);
1837       type2 = TREE_TYPE (mult_rhs2);
1838       add_rhs = rhs1;
1839     }
1840   else
1841     return false;
1842
1843   if (TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2))
1844     return false;
1845
1846   /* Verify that the machine can perform a widening multiply
1847      accumulate in this mode/signedness combination, otherwise
1848      this transformation is likely to pessimize code.  */
1849   this_optab = optab_for_tree_code (wmult_code, type1, optab_default);
1850   if (optab_handler (this_optab, TYPE_MODE (type)) == CODE_FOR_nothing)
1851     return false;
1852
1853   /* ??? May need some type verification here?  */
1854
1855   gimple_assign_set_rhs_with_ops_1 (gsi, wmult_code,
1856                                     fold_convert (type1, mult_rhs1),
1857                                     fold_convert (type2, mult_rhs2),
1858                                     add_rhs);
1859   update_stmt (gsi_stmt (*gsi));
1860   widen_mul_stats.maccs_inserted++;
1861   return true;
1862 }
1863
1864 /* Combine the multiplication at MUL_STMT with operands MULOP1 and MULOP2
1865    with uses in additions and subtractions to form fused multiply-add
1866    operations.  Returns true if successful and MUL_STMT should be removed.  */
1867
1868 static bool
1869 convert_mult_to_fma (gimple mul_stmt, tree op1, tree op2)
1870 {
1871   tree mul_result = gimple_get_lhs (mul_stmt);
1872   tree type = TREE_TYPE (mul_result);
1873   gimple use_stmt, neguse_stmt, fma_stmt;
1874   use_operand_p use_p;
1875   imm_use_iterator imm_iter;
1876
1877   if (FLOAT_TYPE_P (type)
1878       && flag_fp_contract_mode == FP_CONTRACT_OFF)
1879     return false;
1880
1881   /* We don't want to do bitfield reduction ops.  */
1882   if (INTEGRAL_TYPE_P (type)
1883       && (TYPE_PRECISION (type)
1884           != GET_MODE_PRECISION (TYPE_MODE (type))))
1885     return false;
1886
1887   /* If the target doesn't support it, don't generate it.  We assume that
1888      if fma isn't available then fms, fnma or fnms are not either.  */
1889   if (optab_handler (fma_optab, TYPE_MODE (type)) == CODE_FOR_nothing)
1890     return false;
1891
1892   /* Make sure that the multiplication statement becomes dead after
1893      the transformation, thus that all uses are transformed to FMAs.
1894      This means we assume that an FMA operation has the same cost
1895      as an addition.  */
1896   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, mul_result)
1897     {
1898       enum tree_code use_code;
1899       tree result = mul_result;
1900       bool negate_p = false;
1901
1902       use_stmt = USE_STMT (use_p);
1903
1904       if (is_gimple_debug (use_stmt))
1905         continue;
1906
1907       /* For now restrict this operations to single basic blocks.  In theory
1908          we would want to support sinking the multiplication in
1909          m = a*b;
1910          if ()
1911            ma = m + c;
1912          else
1913            d = m;
1914          to form a fma in the then block and sink the multiplication to the
1915          else block.  */
1916       if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
1917         return false;
1918
1919       if (!is_gimple_assign (use_stmt))
1920         return false;
1921
1922       use_code = gimple_assign_rhs_code (use_stmt);
1923
1924       /* A negate on the multiplication leads to FNMA.  */
1925       if (use_code == NEGATE_EXPR)
1926         {
1927           ssa_op_iter iter;
1928           tree use;
1929
1930           result = gimple_assign_lhs (use_stmt);
1931
1932           /* Make sure the negate statement becomes dead with this
1933              single transformation.  */
1934           if (!single_imm_use (gimple_assign_lhs (use_stmt),
1935                                &use_p, &neguse_stmt))
1936             return false;
1937
1938           /* Make sure the multiplication isn't also used on that stmt.  */
1939           FOR_EACH_SSA_TREE_OPERAND (use, neguse_stmt, iter, SSA_OP_USE)
1940             if (use == mul_result)
1941               return false;
1942
1943           /* Re-validate.  */
1944           use_stmt = neguse_stmt;
1945           if (gimple_bb (use_stmt) != gimple_bb (mul_stmt))
1946             return false;
1947           if (!is_gimple_assign (use_stmt))
1948             return false;
1949
1950           use_code = gimple_assign_rhs_code (use_stmt);
1951           negate_p = true;
1952         }
1953
1954       switch (use_code)
1955         {
1956         case MINUS_EXPR:
1957           if (gimple_assign_rhs2 (use_stmt) == result)
1958             negate_p = !negate_p;
1959           break;
1960         case PLUS_EXPR:
1961           break;
1962         default:
1963           /* FMA can only be formed from PLUS and MINUS.  */
1964           return false;
1965         }
1966
1967       /* We can't handle a * b + a * b.  */
1968       if (gimple_assign_rhs1 (use_stmt) == gimple_assign_rhs2 (use_stmt))
1969         return false;
1970
1971       /* While it is possible to validate whether or not the exact form
1972          that we've recognized is available in the backend, the assumption
1973          is that the transformation is never a loss.  For instance, suppose
1974          the target only has the plain FMA pattern available.  Consider
1975          a*b-c -> fma(a,b,-c): we've exchanged MUL+SUB for FMA+NEG, which
1976          is still two operations.  Consider -(a*b)-c -> fma(-a,b,-c): we
1977          still have 3 operations, but in the FMA form the two NEGs are
1978          independant and could be run in parallel.  */
1979     }
1980
1981   FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, mul_result)
1982     {
1983       gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1984       enum tree_code use_code;
1985       tree addop, mulop1 = op1, result = mul_result;
1986       bool negate_p = false;
1987
1988       if (is_gimple_debug (use_stmt))
1989         continue;
1990
1991       use_code = gimple_assign_rhs_code (use_stmt);
1992       if (use_code == NEGATE_EXPR)
1993         {
1994           result = gimple_assign_lhs (use_stmt);
1995           single_imm_use (gimple_assign_lhs (use_stmt), &use_p, &neguse_stmt);
1996           gsi_remove (&gsi, true);
1997           release_defs (use_stmt);
1998
1999           use_stmt = neguse_stmt;
2000           gsi = gsi_for_stmt (use_stmt);
2001           use_code = gimple_assign_rhs_code (use_stmt);
2002           negate_p = true;
2003         }
2004
2005       if (gimple_assign_rhs1 (use_stmt) == result)
2006         {
2007           addop = gimple_assign_rhs2 (use_stmt);
2008           /* a * b - c -> a * b + (-c)  */
2009           if (gimple_assign_rhs_code (use_stmt) == MINUS_EXPR)
2010             addop = force_gimple_operand_gsi (&gsi,
2011                                               build1 (NEGATE_EXPR,
2012                                                       type, addop),
2013                                               true, NULL_TREE, true,
2014                                               GSI_SAME_STMT);
2015         }
2016       else
2017         {
2018           addop = gimple_assign_rhs1 (use_stmt);
2019           /* a - b * c -> (-b) * c + a */
2020           if (gimple_assign_rhs_code (use_stmt) == MINUS_EXPR)
2021             negate_p = !negate_p;
2022         }
2023
2024       if (negate_p)
2025         mulop1 = force_gimple_operand_gsi (&gsi,
2026                                            build1 (NEGATE_EXPR,
2027                                                    type, mulop1),
2028                                            true, NULL_TREE, true,
2029                                            GSI_SAME_STMT);
2030
2031       fma_stmt = gimple_build_assign_with_ops3 (FMA_EXPR,
2032                                                 gimple_assign_lhs (use_stmt),
2033                                                 mulop1, op2,
2034                                                 addop);
2035       gsi_replace (&gsi, fma_stmt, true);
2036       widen_mul_stats.fmas_inserted++;
2037     }
2038
2039   return true;
2040 }
2041
2042 /* Find integer multiplications where the operands are extended from
2043    smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR
2044    where appropriate.  */
2045
2046 static unsigned int
2047 execute_optimize_widening_mul (void)
2048 {
2049   basic_block bb;
2050   bool cfg_changed = false;
2051
2052   memset (&widen_mul_stats, 0, sizeof (widen_mul_stats));
2053
2054   FOR_EACH_BB (bb)
2055     {
2056       gimple_stmt_iterator gsi;
2057
2058       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
2059         {
2060           gimple stmt = gsi_stmt (gsi);
2061           enum tree_code code;
2062
2063           if (is_gimple_assign (stmt))
2064             {
2065               code = gimple_assign_rhs_code (stmt);
2066               switch (code)
2067                 {
2068                 case MULT_EXPR:
2069                   if (!convert_mult_to_widen (stmt)
2070                       && convert_mult_to_fma (stmt,
2071                                               gimple_assign_rhs1 (stmt),
2072                                               gimple_assign_rhs2 (stmt)))
2073                     {
2074                       gsi_remove (&gsi, true);
2075                       release_defs (stmt);
2076                       continue;
2077                     }
2078                   break;
2079
2080                 case PLUS_EXPR:
2081                 case MINUS_EXPR:
2082                   convert_plusminus_to_widen (&gsi, stmt, code);
2083                   break;
2084
2085                 default:;
2086                 }
2087             }
2088           else if (is_gimple_call (stmt)
2089                    && gimple_call_lhs (stmt))
2090             {
2091               tree fndecl = gimple_call_fndecl (stmt);
2092               if (fndecl
2093                   && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2094                 {
2095                   switch (DECL_FUNCTION_CODE (fndecl))
2096                     {
2097                       case BUILT_IN_POWF:
2098                       case BUILT_IN_POW:
2099                       case BUILT_IN_POWL:
2100                         if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
2101                             && REAL_VALUES_EQUAL
2102                                  (TREE_REAL_CST (gimple_call_arg (stmt, 1)),
2103                                   dconst2)
2104                             && convert_mult_to_fma (stmt,
2105                                                     gimple_call_arg (stmt, 0),
2106                                                     gimple_call_arg (stmt, 0)))
2107                           {
2108                             unlink_stmt_vdef (stmt);
2109                             gsi_remove (&gsi, true);
2110                             release_defs (stmt);
2111                             if (gimple_purge_dead_eh_edges (bb))
2112                               cfg_changed = true;
2113                             continue;
2114                           }
2115                           break;
2116
2117                       default:;
2118                     }
2119                 }
2120             }
2121           gsi_next (&gsi);
2122         }
2123     }
2124
2125   statistics_counter_event (cfun, "widening multiplications inserted",
2126                             widen_mul_stats.widen_mults_inserted);
2127   statistics_counter_event (cfun, "widening maccs inserted",
2128                             widen_mul_stats.maccs_inserted);
2129   statistics_counter_event (cfun, "fused multiply-adds inserted",
2130                             widen_mul_stats.fmas_inserted);
2131
2132   return cfg_changed ? TODO_cleanup_cfg : 0;
2133 }
2134
2135 static bool
2136 gate_optimize_widening_mul (void)
2137 {
2138   return flag_expensive_optimizations && optimize;
2139 }
2140
2141 struct gimple_opt_pass pass_optimize_widening_mul =
2142 {
2143  {
2144   GIMPLE_PASS,
2145   "widening_mul",                       /* name */
2146   gate_optimize_widening_mul,           /* gate */
2147   execute_optimize_widening_mul,        /* execute */
2148   NULL,                                 /* sub */
2149   NULL,                                 /* next */
2150   0,                                    /* static_pass_number */
2151   TV_NONE,                              /* tv_id */
2152   PROP_ssa,                             /* properties_required */
2153   0,                                    /* properties_provided */
2154   0,                                    /* properties_destroyed */
2155   0,                                    /* todo_flags_start */
2156   TODO_verify_ssa
2157   | TODO_verify_stmts
2158   | TODO_dump_func
2159   | TODO_update_ssa                     /* todo_flags_finish */
2160  }
2161 };