1 /* Global, SSA-based optimizations using mathematical identities.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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
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
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/>. */
21 /* Currently, the only mini-pass in this file tries to CSE reciprocal
22 operations. These are common in sequences such as this one:
24 modulus = sqrt(x*x + y*y + z*z);
29 that can be optimized to
31 modulus = sqrt(x*x + y*y + z*z);
32 rmodulus = 1.0 / modulus;
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.
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.
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
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).
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.
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.
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).
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.
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. */
90 #include "coretypes.h"
94 #include "tree-flow.h"
96 #include "tree-pass.h"
97 #include "alloc-pool.h"
98 #include "basic-block.h"
100 #include "diagnostic.h"
101 #include "gimple-pretty-print.h"
103 /* FIXME: RTL headers have to be included here for optabs. */
104 #include "rtl.h" /* Because optabs.h wants enum rtx_code. */
105 #include "expr.h" /* Because optabs.h wants sepops. */
108 /* This structure represents one basic block that either computes a
109 division, or is a common dominator for basic block that compute a
112 /* The basic block represented by this structure. */
115 /* If non-NULL, the SSA_NAME holding the definition for a reciprocal
119 /* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that
120 was inserted in BB. */
121 gimple recip_def_stmt
;
123 /* Pointer to a list of "struct occurrence"s for blocks dominated
125 struct occurrence
*children
;
127 /* Pointer to the next "struct occurrence"s in the list of blocks
128 sharing a common dominator. */
129 struct occurrence
*next
;
131 /* The number of divisions that are in BB before compute_merit. The
132 number of divisions that are in BB or post-dominate it after
136 /* True if the basic block has a division, false if it is a common
137 dominator for basic blocks that do. If it is false and trapping
138 math is active, BB is not a candidate for inserting a reciprocal. */
139 bool bb_has_division
;
143 /* The instance of "struct occurrence" representing the highest
144 interesting block in the dominator tree. */
145 static struct occurrence
*occ_head
;
147 /* Allocation pool for getting instances of "struct occurrence". */
148 static alloc_pool occ_pool
;
152 /* Allocate and return a new struct occurrence for basic block BB, and
153 whose children list is headed by CHILDREN. */
154 static struct occurrence
*
155 occ_new (basic_block bb
, struct occurrence
*children
)
157 struct occurrence
*occ
;
159 bb
->aux
= occ
= (struct occurrence
*) pool_alloc (occ_pool
);
160 memset (occ
, 0, sizeof (struct occurrence
));
163 occ
->children
= children
;
168 /* Insert NEW_OCC into our subset of the dominator tree. P_HEAD points to a
169 list of "struct occurrence"s, one per basic block, having IDOM as
170 their common dominator.
172 We try to insert NEW_OCC as deep as possible in the tree, and we also
173 insert any other block that is a common dominator for BB and one
174 block already in the tree. */
177 insert_bb (struct occurrence
*new_occ
, basic_block idom
,
178 struct occurrence
**p_head
)
180 struct occurrence
*occ
, **p_occ
;
182 for (p_occ
= p_head
; (occ
= *p_occ
) != NULL
; )
184 basic_block bb
= new_occ
->bb
, occ_bb
= occ
->bb
;
185 basic_block dom
= nearest_common_dominator (CDI_DOMINATORS
, occ_bb
, bb
);
188 /* BB dominates OCC_BB. OCC becomes NEW_OCC's child: remove OCC
191 occ
->next
= new_occ
->children
;
192 new_occ
->children
= occ
;
194 /* Try the next block (it may as well be dominated by BB). */
197 else if (dom
== occ_bb
)
199 /* OCC_BB dominates BB. Tail recurse to look deeper. */
200 insert_bb (new_occ
, dom
, &occ
->children
);
204 else if (dom
!= idom
)
206 gcc_assert (!dom
->aux
);
208 /* There is a dominator between IDOM and BB, add it and make
209 two children out of NEW_OCC and OCC. First, remove OCC from
215 /* None of the previous blocks has DOM as a dominator: if we tail
216 recursed, we would reexamine them uselessly. Just switch BB with
217 DOM, and go on looking for blocks dominated by DOM. */
218 new_occ
= occ_new (dom
, new_occ
);
223 /* Nothing special, go on with the next element. */
228 /* No place was found as a child of IDOM. Make BB a sibling of IDOM. */
229 new_occ
->next
= *p_head
;
233 /* Register that we found a division in BB. */
236 register_division_in (basic_block bb
)
238 struct occurrence
*occ
;
240 occ
= (struct occurrence
*) bb
->aux
;
243 occ
= occ_new (bb
, NULL
);
244 insert_bb (occ
, ENTRY_BLOCK_PTR
, &occ_head
);
247 occ
->bb_has_division
= true;
248 occ
->num_divisions
++;
252 /* Compute the number of divisions that postdominate each block in OCC and
256 compute_merit (struct occurrence
*occ
)
258 struct occurrence
*occ_child
;
259 basic_block dom
= occ
->bb
;
261 for (occ_child
= occ
->children
; occ_child
; occ_child
= occ_child
->next
)
264 if (occ_child
->children
)
265 compute_merit (occ_child
);
268 bb
= single_noncomplex_succ (dom
);
272 if (dominated_by_p (CDI_POST_DOMINATORS
, bb
, occ_child
->bb
))
273 occ
->num_divisions
+= occ_child
->num_divisions
;
278 /* Return whether USE_STMT is a floating-point division by DEF. */
280 is_division_by (gimple use_stmt
, tree def
)
282 return is_gimple_assign (use_stmt
)
283 && gimple_assign_rhs_code (use_stmt
) == RDIV_EXPR
284 && gimple_assign_rhs2 (use_stmt
) == def
285 /* Do not recognize x / x as valid division, as we are getting
286 confused later by replacing all immediate uses x in such
288 && gimple_assign_rhs1 (use_stmt
) != def
;
291 /* Walk the subset of the dominator tree rooted at OCC, setting the
292 RECIP_DEF field to a definition of 1.0 / DEF that can be used in
293 the given basic block. The field may be left NULL, of course,
294 if it is not possible or profitable to do the optimization.
296 DEF_BSI is an iterator pointing at the statement defining DEF.
297 If RECIP_DEF is set, a dominator already has a computation that can
301 insert_reciprocals (gimple_stmt_iterator
*def_gsi
, struct occurrence
*occ
,
302 tree def
, tree recip_def
, int threshold
)
306 gimple_stmt_iterator gsi
;
307 struct occurrence
*occ_child
;
310 && (occ
->bb_has_division
|| !flag_trapping_math
)
311 && occ
->num_divisions
>= threshold
)
313 /* Make a variable with the replacement and substitute it. */
314 type
= TREE_TYPE (def
);
315 recip_def
= make_rename_temp (type
, "reciptmp");
316 new_stmt
= gimple_build_assign_with_ops (RDIV_EXPR
, recip_def
,
317 build_one_cst (type
), def
);
319 if (occ
->bb_has_division
)
321 /* Case 1: insert before an existing division. */
322 gsi
= gsi_after_labels (occ
->bb
);
323 while (!gsi_end_p (gsi
) && !is_division_by (gsi_stmt (gsi
), def
))
326 gsi_insert_before (&gsi
, new_stmt
, GSI_SAME_STMT
);
328 else if (def_gsi
&& occ
->bb
== def_gsi
->bb
)
330 /* Case 2: insert right after the definition. Note that this will
331 never happen if the definition statement can throw, because in
332 that case the sole successor of the statement's basic block will
333 dominate all the uses as well. */
334 gsi_insert_after (def_gsi
, new_stmt
, GSI_NEW_STMT
);
338 /* Case 3: insert in a basic block not containing defs/uses. */
339 gsi
= gsi_after_labels (occ
->bb
);
340 gsi_insert_before (&gsi
, new_stmt
, GSI_SAME_STMT
);
343 occ
->recip_def_stmt
= new_stmt
;
346 occ
->recip_def
= recip_def
;
347 for (occ_child
= occ
->children
; occ_child
; occ_child
= occ_child
->next
)
348 insert_reciprocals (def_gsi
, occ_child
, def
, recip_def
, threshold
);
352 /* Replace the division at USE_P with a multiplication by the reciprocal, if
356 replace_reciprocal (use_operand_p use_p
)
358 gimple use_stmt
= USE_STMT (use_p
);
359 basic_block bb
= gimple_bb (use_stmt
);
360 struct occurrence
*occ
= (struct occurrence
*) bb
->aux
;
362 if (optimize_bb_for_speed_p (bb
)
363 && occ
->recip_def
&& use_stmt
!= occ
->recip_def_stmt
)
365 gimple_assign_set_rhs_code (use_stmt
, MULT_EXPR
);
366 SET_USE (use_p
, occ
->recip_def
);
367 fold_stmt_inplace (use_stmt
);
368 update_stmt (use_stmt
);
373 /* Free OCC and return one more "struct occurrence" to be freed. */
375 static struct occurrence
*
376 free_bb (struct occurrence
*occ
)
378 struct occurrence
*child
, *next
;
380 /* First get the two pointers hanging off OCC. */
382 child
= occ
->children
;
384 pool_free (occ_pool
, occ
);
386 /* Now ensure that we don't recurse unless it is necessary. */
392 next
= free_bb (next
);
399 /* Look for floating-point divisions among DEF's uses, and try to
400 replace them by multiplications with the reciprocal. Add
401 as many statements computing the reciprocal as needed.
403 DEF must be a GIMPLE register of a floating-point type. */
406 execute_cse_reciprocals_1 (gimple_stmt_iterator
*def_gsi
, tree def
)
409 imm_use_iterator use_iter
;
410 struct occurrence
*occ
;
411 int count
= 0, threshold
;
413 gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def
)) && is_gimple_reg (def
));
415 FOR_EACH_IMM_USE_FAST (use_p
, use_iter
, def
)
417 gimple use_stmt
= USE_STMT (use_p
);
418 if (is_division_by (use_stmt
, def
))
420 register_division_in (gimple_bb (use_stmt
));
425 /* Do the expensive part only if we can hope to optimize something. */
426 threshold
= targetm
.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def
)));
427 if (count
>= threshold
)
430 for (occ
= occ_head
; occ
; occ
= occ
->next
)
433 insert_reciprocals (def_gsi
, occ
, def
, NULL
, threshold
);
436 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, def
)
438 if (is_division_by (use_stmt
, def
))
440 FOR_EACH_IMM_USE_ON_STMT (use_p
, use_iter
)
441 replace_reciprocal (use_p
);
446 for (occ
= occ_head
; occ
; )
453 gate_cse_reciprocals (void)
455 return optimize
&& flag_reciprocal_math
;
458 /* Go through all the floating-point SSA_NAMEs, and call
459 execute_cse_reciprocals_1 on each of them. */
461 execute_cse_reciprocals (void)
466 occ_pool
= create_alloc_pool ("dominators for recip",
467 sizeof (struct occurrence
),
468 n_basic_blocks
/ 3 + 1);
470 calculate_dominance_info (CDI_DOMINATORS
);
471 calculate_dominance_info (CDI_POST_DOMINATORS
);
473 #ifdef ENABLE_CHECKING
475 gcc_assert (!bb
->aux
);
478 for (arg
= DECL_ARGUMENTS (cfun
->decl
); arg
; arg
= TREE_CHAIN (arg
))
479 if (gimple_default_def (cfun
, arg
)
480 && FLOAT_TYPE_P (TREE_TYPE (arg
))
481 && is_gimple_reg (arg
))
482 execute_cse_reciprocals_1 (NULL
, gimple_default_def (cfun
, arg
));
486 gimple_stmt_iterator gsi
;
490 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
492 phi
= gsi_stmt (gsi
);
493 def
= PHI_RESULT (phi
);
494 if (FLOAT_TYPE_P (TREE_TYPE (def
))
495 && is_gimple_reg (def
))
496 execute_cse_reciprocals_1 (NULL
, def
);
499 for (gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
501 gimple stmt
= gsi_stmt (gsi
);
503 if (gimple_has_lhs (stmt
)
504 && (def
= SINGLE_SSA_TREE_OPERAND (stmt
, SSA_OP_DEF
)) != NULL
505 && FLOAT_TYPE_P (TREE_TYPE (def
))
506 && TREE_CODE (def
) == SSA_NAME
)
507 execute_cse_reciprocals_1 (&gsi
, def
);
510 if (optimize_bb_for_size_p (bb
))
513 /* Scan for a/func(b) and convert it to reciprocal a*rfunc(b). */
514 for (gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
516 gimple stmt
= gsi_stmt (gsi
);
519 if (is_gimple_assign (stmt
)
520 && gimple_assign_rhs_code (stmt
) == RDIV_EXPR
)
522 tree arg1
= gimple_assign_rhs2 (stmt
);
525 if (TREE_CODE (arg1
) != SSA_NAME
)
528 stmt1
= SSA_NAME_DEF_STMT (arg1
);
530 if (is_gimple_call (stmt1
)
531 && gimple_call_lhs (stmt1
)
532 && (fndecl
= gimple_call_fndecl (stmt1
))
533 && (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
534 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
))
536 enum built_in_function code
;
541 code
= DECL_FUNCTION_CODE (fndecl
);
542 md_code
= DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
;
544 fndecl
= targetm
.builtin_reciprocal (code
, md_code
, false);
548 /* Check that all uses of the SSA name are divisions,
549 otherwise replacing the defining statement will do
552 FOR_EACH_IMM_USE_FAST (use_p
, ui
, arg1
)
554 gimple stmt2
= USE_STMT (use_p
);
555 if (is_gimple_debug (stmt2
))
557 if (!is_gimple_assign (stmt2
)
558 || gimple_assign_rhs_code (stmt2
) != RDIV_EXPR
559 || gimple_assign_rhs1 (stmt2
) == arg1
560 || gimple_assign_rhs2 (stmt2
) != arg1
)
569 gimple_replace_lhs (stmt1
, arg1
);
570 gimple_call_set_fndecl (stmt1
, fndecl
);
573 FOR_EACH_IMM_USE_STMT (stmt
, ui
, arg1
)
575 gimple_assign_set_rhs_code (stmt
, MULT_EXPR
);
576 fold_stmt_inplace (stmt
);
584 free_dominance_info (CDI_DOMINATORS
);
585 free_dominance_info (CDI_POST_DOMINATORS
);
586 free_alloc_pool (occ_pool
);
590 struct gimple_opt_pass pass_cse_reciprocals
=
595 gate_cse_reciprocals
, /* gate */
596 execute_cse_reciprocals
, /* execute */
599 0, /* static_pass_number */
601 PROP_ssa
, /* properties_required */
602 0, /* properties_provided */
603 0, /* properties_destroyed */
604 0, /* todo_flags_start */
605 TODO_dump_func
| TODO_update_ssa
| TODO_verify_ssa
606 | TODO_verify_stmts
/* todo_flags_finish */
610 /* Records an occurrence at statement USE_STMT in the vector of trees
611 STMTS if it is dominated by *TOP_BB or dominates it or this basic block
612 is not yet initialized. Returns true if the occurrence was pushed on
613 the vector. Adjusts *TOP_BB to be the basic block dominating all
614 statements in the vector. */
617 maybe_record_sincos (VEC(gimple
, heap
) **stmts
,
618 basic_block
*top_bb
, gimple use_stmt
)
620 basic_block use_bb
= gimple_bb (use_stmt
);
622 && (*top_bb
== use_bb
623 || dominated_by_p (CDI_DOMINATORS
, use_bb
, *top_bb
)))
624 VEC_safe_push (gimple
, heap
, *stmts
, use_stmt
);
626 || dominated_by_p (CDI_DOMINATORS
, *top_bb
, use_bb
))
628 VEC_safe_push (gimple
, heap
, *stmts
, use_stmt
);
637 /* Look for sin, cos and cexpi calls with the same argument NAME and
638 create a single call to cexpi CSEing the result in this case.
639 We first walk over all immediate uses of the argument collecting
640 statements that we can CSE in a vector and in a second pass replace
641 the statement rhs with a REALPART or IMAGPART expression on the
642 result of the cexpi call we insert before the use statement that
643 dominates all other candidates. */
646 execute_cse_sincos_1 (tree name
)
648 gimple_stmt_iterator gsi
;
649 imm_use_iterator use_iter
;
650 tree fndecl
, res
, type
;
651 gimple def_stmt
, use_stmt
, stmt
;
652 int seen_cos
= 0, seen_sin
= 0, seen_cexpi
= 0;
653 VEC(gimple
, heap
) *stmts
= NULL
;
654 basic_block top_bb
= NULL
;
657 type
= TREE_TYPE (name
);
658 FOR_EACH_IMM_USE_STMT (use_stmt
, use_iter
, name
)
660 if (gimple_code (use_stmt
) != GIMPLE_CALL
661 || !gimple_call_lhs (use_stmt
)
662 || !(fndecl
= gimple_call_fndecl (use_stmt
))
663 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
666 switch (DECL_FUNCTION_CODE (fndecl
))
668 CASE_FLT_FN (BUILT_IN_COS
):
669 seen_cos
|= maybe_record_sincos (&stmts
, &top_bb
, use_stmt
) ? 1 : 0;
672 CASE_FLT_FN (BUILT_IN_SIN
):
673 seen_sin
|= maybe_record_sincos (&stmts
, &top_bb
, use_stmt
) ? 1 : 0;
676 CASE_FLT_FN (BUILT_IN_CEXPI
):
677 seen_cexpi
|= maybe_record_sincos (&stmts
, &top_bb
, use_stmt
) ? 1 : 0;
684 if (seen_cos
+ seen_sin
+ seen_cexpi
<= 1)
686 VEC_free(gimple
, heap
, stmts
);
690 /* Simply insert cexpi at the beginning of top_bb but not earlier than
691 the name def statement. */
692 fndecl
= mathfn_built_in (type
, BUILT_IN_CEXPI
);
695 res
= make_rename_temp (TREE_TYPE (TREE_TYPE (fndecl
)), "sincostmp");
696 stmt
= gimple_build_call (fndecl
, 1, name
);
697 gimple_call_set_lhs (stmt
, res
);
699 def_stmt
= SSA_NAME_DEF_STMT (name
);
700 if (!SSA_NAME_IS_DEFAULT_DEF (name
)
701 && gimple_code (def_stmt
) != GIMPLE_PHI
702 && gimple_bb (def_stmt
) == top_bb
)
704 gsi
= gsi_for_stmt (def_stmt
);
705 gsi_insert_after (&gsi
, stmt
, GSI_SAME_STMT
);
709 gsi
= gsi_after_labels (top_bb
);
710 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
714 /* And adjust the recorded old call sites. */
715 for (i
= 0; VEC_iterate(gimple
, stmts
, i
, use_stmt
); ++i
)
718 fndecl
= gimple_call_fndecl (use_stmt
);
720 switch (DECL_FUNCTION_CODE (fndecl
))
722 CASE_FLT_FN (BUILT_IN_COS
):
723 rhs
= fold_build1 (REALPART_EXPR
, type
, res
);
726 CASE_FLT_FN (BUILT_IN_SIN
):
727 rhs
= fold_build1 (IMAGPART_EXPR
, type
, res
);
730 CASE_FLT_FN (BUILT_IN_CEXPI
):
738 /* Replace call with a copy. */
739 stmt
= gimple_build_assign (gimple_call_lhs (use_stmt
), rhs
);
741 gsi
= gsi_for_stmt (use_stmt
);
742 gsi_insert_after (&gsi
, stmt
, GSI_SAME_STMT
);
743 gsi_remove (&gsi
, true);
746 VEC_free(gimple
, heap
, stmts
);
749 /* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1
750 on the SSA_NAME argument of each of them. */
753 execute_cse_sincos (void)
757 calculate_dominance_info (CDI_DOMINATORS
);
761 gimple_stmt_iterator gsi
;
763 for (gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
765 gimple stmt
= gsi_stmt (gsi
);
768 if (is_gimple_call (stmt
)
769 && gimple_call_lhs (stmt
)
770 && (fndecl
= gimple_call_fndecl (stmt
))
771 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
775 switch (DECL_FUNCTION_CODE (fndecl
))
777 CASE_FLT_FN (BUILT_IN_COS
):
778 CASE_FLT_FN (BUILT_IN_SIN
):
779 CASE_FLT_FN (BUILT_IN_CEXPI
):
780 arg
= gimple_call_arg (stmt
, 0);
781 if (TREE_CODE (arg
) == SSA_NAME
)
782 execute_cse_sincos_1 (arg
);
791 free_dominance_info (CDI_DOMINATORS
);
796 gate_cse_sincos (void)
798 /* Make sure we have either sincos or cexp. */
799 return (TARGET_HAS_SINCOS
800 || TARGET_C99_FUNCTIONS
)
804 struct gimple_opt_pass pass_cse_sincos
=
809 gate_cse_sincos
, /* gate */
810 execute_cse_sincos
, /* execute */
813 0, /* static_pass_number */
815 PROP_ssa
, /* properties_required */
816 0, /* properties_provided */
817 0, /* properties_destroyed */
818 0, /* todo_flags_start */
819 TODO_dump_func
| TODO_update_ssa
| TODO_verify_ssa
820 | TODO_verify_stmts
/* todo_flags_finish */
824 /* A symbolic number is used to detect byte permutation and selection
825 patterns. Therefore the field N contains an artificial number
826 consisting of byte size markers:
828 0 - byte has the value 0
829 1..size - byte contains the content of the byte
830 number indexed with that value minus one */
832 struct symbolic_number
{
833 unsigned HOST_WIDEST_INT n
;
837 /* Perform a SHIFT or ROTATE operation by COUNT bits on symbolic
838 number N. Return false if the requested operation is not permitted
839 on a symbolic number. */
842 do_shift_rotate (enum tree_code code
,
843 struct symbolic_number
*n
,
849 /* Zero out the extra bits of N in order to avoid them being shifted
850 into the significant bits. */
851 if (n
->size
< (int)sizeof (HOST_WIDEST_INT
))
852 n
->n
&= ((unsigned HOST_WIDEST_INT
)1 << (n
->size
* BITS_PER_UNIT
)) - 1;
863 n
->n
= (n
->n
<< count
) | (n
->n
>> ((n
->size
* BITS_PER_UNIT
) - count
));
866 n
->n
= (n
->n
>> count
) | (n
->n
<< ((n
->size
* BITS_PER_UNIT
) - count
));
874 /* Perform sanity checking for the symbolic number N and the gimple
878 verify_symbolic_number_p (struct symbolic_number
*n
, gimple stmt
)
882 lhs_type
= gimple_expr_type (stmt
);
884 if (TREE_CODE (lhs_type
) != INTEGER_TYPE
)
887 if (TYPE_PRECISION (lhs_type
) != n
->size
* BITS_PER_UNIT
)
893 /* find_bswap_1 invokes itself recursively with N and tries to perform
894 the operation given by the rhs of STMT on the result. If the
895 operation could successfully be executed the function returns the
896 tree expression of the source operand and NULL otherwise. */
899 find_bswap_1 (gimple stmt
, struct symbolic_number
*n
, int limit
)
902 tree rhs1
, rhs2
= NULL
;
903 gimple rhs1_stmt
, rhs2_stmt
;
905 enum gimple_rhs_class rhs_class
;
907 if (!limit
|| !is_gimple_assign (stmt
))
910 rhs1
= gimple_assign_rhs1 (stmt
);
912 if (TREE_CODE (rhs1
) != SSA_NAME
)
915 code
= gimple_assign_rhs_code (stmt
);
916 rhs_class
= gimple_assign_rhs_class (stmt
);
917 rhs1_stmt
= SSA_NAME_DEF_STMT (rhs1
);
919 if (rhs_class
== GIMPLE_BINARY_RHS
)
920 rhs2
= gimple_assign_rhs2 (stmt
);
922 /* Handle unary rhs and binary rhs with integer constants as second
925 if (rhs_class
== GIMPLE_UNARY_RHS
926 || (rhs_class
== GIMPLE_BINARY_RHS
927 && TREE_CODE (rhs2
) == INTEGER_CST
))
929 if (code
!= BIT_AND_EXPR
930 && code
!= LSHIFT_EXPR
931 && code
!= RSHIFT_EXPR
932 && code
!= LROTATE_EXPR
933 && code
!= RROTATE_EXPR
935 && code
!= CONVERT_EXPR
)
938 source_expr1
= find_bswap_1 (rhs1_stmt
, n
, limit
- 1);
940 /* If find_bswap_1 returned NULL STMT is a leaf node and we have
941 to initialize the symbolic number. */
944 /* Set up the symbolic number N by setting each byte to a
945 value between 1 and the byte size of rhs1. The highest
946 order byte is set to n->size and the lowest order
948 n
->size
= TYPE_PRECISION (TREE_TYPE (rhs1
));
949 if (n
->size
% BITS_PER_UNIT
!= 0)
951 n
->size
/= BITS_PER_UNIT
;
952 n
->n
= (sizeof (HOST_WIDEST_INT
) < 8 ? 0 :
953 (unsigned HOST_WIDEST_INT
)0x08070605 << 32 | 0x04030201);
955 if (n
->size
< (int)sizeof (HOST_WIDEST_INT
))
956 n
->n
&= ((unsigned HOST_WIDEST_INT
)1 <<
957 (n
->size
* BITS_PER_UNIT
)) - 1;
967 unsigned HOST_WIDEST_INT val
= widest_int_cst_value (rhs2
);
968 unsigned HOST_WIDEST_INT tmp
= val
;
970 /* Only constants masking full bytes are allowed. */
971 for (i
= 0; i
< n
->size
; i
++, tmp
>>= BITS_PER_UNIT
)
972 if ((tmp
& 0xff) != 0 && (tmp
& 0xff) != 0xff)
982 if (!do_shift_rotate (code
, n
, (int)TREE_INT_CST_LOW (rhs2
)))
989 type_size
= TYPE_PRECISION (gimple_expr_type (stmt
));
990 if (type_size
% BITS_PER_UNIT
!= 0)
993 if (type_size
/ BITS_PER_UNIT
< (int)(sizeof (HOST_WIDEST_INT
)))
995 /* If STMT casts to a smaller type mask out the bits not
996 belonging to the target type. */
997 n
->n
&= ((unsigned HOST_WIDEST_INT
)1 << type_size
) - 1;
999 n
->size
= type_size
/ BITS_PER_UNIT
;
1005 return verify_symbolic_number_p (n
, stmt
) ? source_expr1
: NULL
;
1008 /* Handle binary rhs. */
1010 if (rhs_class
== GIMPLE_BINARY_RHS
)
1012 struct symbolic_number n1
, n2
;
1015 if (code
!= BIT_IOR_EXPR
)
1018 if (TREE_CODE (rhs2
) != SSA_NAME
)
1021 rhs2_stmt
= SSA_NAME_DEF_STMT (rhs2
);
1026 source_expr1
= find_bswap_1 (rhs1_stmt
, &n1
, limit
- 1);
1031 source_expr2
= find_bswap_1 (rhs2_stmt
, &n2
, limit
- 1);
1033 if (source_expr1
!= source_expr2
1034 || n1
.size
!= n2
.size
)
1040 if (!verify_symbolic_number_p (n
, stmt
))
1047 return source_expr1
;
1052 /* Check if STMT completes a bswap implementation consisting of ORs,
1053 SHIFTs and ANDs. Return the source tree expression on which the
1054 byte swap is performed and NULL if no bswap was found. */
1057 find_bswap (gimple stmt
)
1059 /* The number which the find_bswap result should match in order to
1060 have a full byte swap. The number is shifted to the left according
1061 to the size of the symbolic number before using it. */
1062 unsigned HOST_WIDEST_INT cmp
=
1063 sizeof (HOST_WIDEST_INT
) < 8 ? 0 :
1064 (unsigned HOST_WIDEST_INT
)0x01020304 << 32 | 0x05060708;
1066 struct symbolic_number n
;
1069 /* The last parameter determines the depth search limit. It usually
1070 correlates directly to the number of bytes to be touched. We
1071 increase that number by one here in order to also cover signed ->
1072 unsigned conversions of the src operand as can be seen in
1074 source_expr
= find_bswap_1 (stmt
, &n
,
1076 TYPE_SIZE_UNIT (gimple_expr_type (stmt
))) + 1);
1081 /* Zero out the extra bits of N and CMP. */
1082 if (n
.size
< (int)sizeof (HOST_WIDEST_INT
))
1084 unsigned HOST_WIDEST_INT mask
=
1085 ((unsigned HOST_WIDEST_INT
)1 << (n
.size
* BITS_PER_UNIT
)) - 1;
1088 cmp
>>= (sizeof (HOST_WIDEST_INT
) - n
.size
) * BITS_PER_UNIT
;
1091 /* A complete byte swap should make the symbolic number to start
1092 with the largest digit in the highest order byte. */
1099 /* Find manual byte swap implementations and turn them into a bswap
1100 builtin invokation. */
1103 execute_optimize_bswap (void)
1106 bool bswap32_p
, bswap64_p
;
1107 bool changed
= false;
1108 tree bswap32_type
= NULL_TREE
, bswap64_type
= NULL_TREE
;
1110 if (BITS_PER_UNIT
!= 8)
1113 if (sizeof (HOST_WIDEST_INT
) < 8)
1116 bswap32_p
= (built_in_decls
[BUILT_IN_BSWAP32
]
1117 && optab_handler (bswap_optab
, SImode
)->insn_code
!=
1119 bswap64_p
= (built_in_decls
[BUILT_IN_BSWAP64
]
1120 && (optab_handler (bswap_optab
, DImode
)->insn_code
!=
1122 || (bswap32_p
&& word_mode
== SImode
)));
1124 if (!bswap32_p
&& !bswap64_p
)
1127 /* Determine the argument type of the builtins. The code later on
1128 assumes that the return and argument type are the same. */
1131 tree fndecl
= built_in_decls
[BUILT_IN_BSWAP32
];
1132 bswap32_type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1137 tree fndecl
= built_in_decls
[BUILT_IN_BSWAP64
];
1138 bswap64_type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1143 gimple_stmt_iterator gsi
;
1145 for (gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1147 gimple stmt
= gsi_stmt (gsi
);
1148 tree bswap_src
, bswap_type
;
1150 tree fndecl
= NULL_TREE
;
1154 if (!is_gimple_assign (stmt
)
1155 || gimple_assign_rhs_code (stmt
) != BIT_IOR_EXPR
)
1158 type_size
= TYPE_PRECISION (gimple_expr_type (stmt
));
1165 fndecl
= built_in_decls
[BUILT_IN_BSWAP32
];
1166 bswap_type
= bswap32_type
;
1172 fndecl
= built_in_decls
[BUILT_IN_BSWAP64
];
1173 bswap_type
= bswap64_type
;
1183 bswap_src
= find_bswap (stmt
);
1190 bswap_tmp
= bswap_src
;
1192 /* Convert the src expression if necessary. */
1193 if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp
), bswap_type
))
1195 gimple convert_stmt
;
1197 bswap_tmp
= create_tmp_var (bswap_type
, "bswapsrc");
1198 add_referenced_var (bswap_tmp
);
1199 bswap_tmp
= make_ssa_name (bswap_tmp
, NULL
);
1201 convert_stmt
= gimple_build_assign_with_ops (
1202 CONVERT_EXPR
, bswap_tmp
, bswap_src
, NULL
);
1203 gsi_insert_before (&gsi
, convert_stmt
, GSI_SAME_STMT
);
1206 call
= gimple_build_call (fndecl
, 1, bswap_tmp
);
1208 bswap_tmp
= gimple_assign_lhs (stmt
);
1210 /* Convert the result if necessary. */
1211 if (!useless_type_conversion_p (TREE_TYPE (bswap_tmp
), bswap_type
))
1213 gimple convert_stmt
;
1215 bswap_tmp
= create_tmp_var (bswap_type
, "bswapdst");
1216 add_referenced_var (bswap_tmp
);
1217 bswap_tmp
= make_ssa_name (bswap_tmp
, NULL
);
1218 convert_stmt
= gimple_build_assign_with_ops (
1219 CONVERT_EXPR
, gimple_assign_lhs (stmt
), bswap_tmp
, NULL
);
1220 gsi_insert_after (&gsi
, convert_stmt
, GSI_SAME_STMT
);
1223 gimple_call_set_lhs (call
, bswap_tmp
);
1227 fprintf (dump_file
, "%d bit bswap implementation found at: ",
1229 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1232 gsi_insert_after (&gsi
, call
, GSI_SAME_STMT
);
1233 gsi_remove (&gsi
, true);
1237 return (changed
? TODO_dump_func
| TODO_update_ssa
| TODO_verify_ssa
1238 | TODO_verify_stmts
: 0);
1242 gate_optimize_bswap (void)
1244 return flag_expensive_optimizations
&& optimize
;
1247 struct gimple_opt_pass pass_optimize_bswap
=
1252 gate_optimize_bswap
, /* gate */
1253 execute_optimize_bswap
, /* execute */
1256 0, /* static_pass_number */
1257 TV_NONE
, /* tv_id */
1258 PROP_ssa
, /* properties_required */
1259 0, /* properties_provided */
1260 0, /* properties_destroyed */
1261 0, /* todo_flags_start */
1262 0 /* todo_flags_finish */
1266 /* Find integer multiplications where the operands are extended from
1267 smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR
1268 where appropriate. */
1271 execute_optimize_widening_mul (void)
1273 bool changed
= false;
1278 gimple_stmt_iterator gsi
;
1280 for (gsi
= gsi_after_labels (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1282 gimple stmt
= gsi_stmt (gsi
);
1283 gimple rhs1_stmt
= NULL
, rhs2_stmt
= NULL
;
1284 tree type
, type1
= NULL
, type2
= NULL
;
1285 tree rhs1
, rhs2
, rhs1_convop
= NULL
, rhs2_convop
= NULL
;
1286 enum tree_code rhs1_code
, rhs2_code
;
1288 if (!is_gimple_assign (stmt
)
1289 || gimple_assign_rhs_code (stmt
) != MULT_EXPR
)
1292 type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1294 if (TREE_CODE (type
) != INTEGER_TYPE
)
1297 rhs1
= gimple_assign_rhs1 (stmt
);
1298 rhs2
= gimple_assign_rhs2 (stmt
);
1300 if (TREE_CODE (rhs1
) == SSA_NAME
)
1302 rhs1_stmt
= SSA_NAME_DEF_STMT (rhs1
);
1303 if (!is_gimple_assign (rhs1_stmt
))
1305 rhs1_code
= gimple_assign_rhs_code (rhs1_stmt
);
1306 if (!CONVERT_EXPR_CODE_P (rhs1_code
))
1308 rhs1_convop
= gimple_assign_rhs1 (rhs1_stmt
);
1309 type1
= TREE_TYPE (rhs1_convop
);
1310 if (TYPE_PRECISION (type1
) * 2 != TYPE_PRECISION (type
))
1313 else if (TREE_CODE (rhs1
) != INTEGER_CST
)
1316 if (TREE_CODE (rhs2
) == SSA_NAME
)
1318 rhs2_stmt
= SSA_NAME_DEF_STMT (rhs2
);
1319 if (!is_gimple_assign (rhs2_stmt
))
1321 rhs2_code
= gimple_assign_rhs_code (rhs2_stmt
);
1322 if (!CONVERT_EXPR_CODE_P (rhs2_code
))
1324 rhs2_convop
= gimple_assign_rhs1 (rhs2_stmt
);
1325 type2
= TREE_TYPE (rhs2_convop
);
1326 if (TYPE_PRECISION (type2
) * 2 != TYPE_PRECISION (type
))
1329 else if (TREE_CODE (rhs2
) != INTEGER_CST
)
1332 if (rhs1_stmt
== NULL
&& rhs2_stmt
== NULL
)
1335 /* Verify that the machine can perform a widening multiply in this
1336 mode/signedness combination, otherwise this transformation is
1337 likely to pessimize code. */
1338 if ((rhs1_stmt
== NULL
|| TYPE_UNSIGNED (type1
))
1339 && (rhs2_stmt
== NULL
|| TYPE_UNSIGNED (type2
))
1340 && (optab_handler (umul_widen_optab
, TYPE_MODE (type
))
1341 ->insn_code
== CODE_FOR_nothing
))
1343 else if ((rhs1_stmt
== NULL
|| !TYPE_UNSIGNED (type1
))
1344 && (rhs2_stmt
== NULL
|| !TYPE_UNSIGNED (type2
))
1345 && (optab_handler (smul_widen_optab
, TYPE_MODE (type
))
1346 ->insn_code
== CODE_FOR_nothing
))
1348 else if (rhs1_stmt
!= NULL
&& rhs2_stmt
!= 0
1349 && (TYPE_UNSIGNED (type1
) != TYPE_UNSIGNED (type2
))
1350 && (optab_handler (usmul_widen_optab
, TYPE_MODE (type
))
1351 ->insn_code
== CODE_FOR_nothing
))
1354 if ((rhs1_stmt
== NULL
&& !int_fits_type_p (rhs1
, type2
))
1355 || (rhs2_stmt
== NULL
&& !int_fits_type_p (rhs2
, type1
)))
1358 if (rhs1_stmt
== NULL
)
1359 gimple_assign_set_rhs1 (stmt
, fold_convert (type2
, rhs1
));
1361 gimple_assign_set_rhs1 (stmt
, rhs1_convop
);
1362 if (rhs2_stmt
== NULL
)
1363 gimple_assign_set_rhs2 (stmt
, fold_convert (type1
, rhs2
));
1365 gimple_assign_set_rhs2 (stmt
, rhs2_convop
);
1366 gimple_assign_set_rhs_code (stmt
, WIDEN_MULT_EXPR
);
1371 return (changed
? TODO_dump_func
| TODO_update_ssa
| TODO_verify_ssa
1372 | TODO_verify_stmts
: 0);
1376 gate_optimize_widening_mul (void)
1378 return flag_expensive_optimizations
&& optimize
;
1381 struct gimple_opt_pass pass_optimize_widening_mul
=
1385 "widening_mul", /* name */
1386 gate_optimize_widening_mul
, /* gate */
1387 execute_optimize_widening_mul
, /* execute */
1390 0, /* static_pass_number */
1391 TV_NONE
, /* tv_id */
1392 PROP_ssa
, /* properties_required */
1393 0, /* properties_provided */
1394 0, /* properties_destroyed */
1395 0, /* todo_flags_start */
1396 0 /* todo_flags_finish */