1 /* Reassociation for trees.
2 Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
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/>. */
23 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "diagnostic.h"
29 #include "tree-inline.h"
30 #include "tree-flow.h"
32 #include "tree-dump.h"
34 #include "tree-iterator.h"
35 #include "tree-pass.h"
36 #include "alloc-pool.h"
38 #include "langhooks.h"
39 #include "pointer-set.h"
43 /* This is a simple global reassociation pass. It is, in part, based
44 on the LLVM pass of the same name (They do some things more/less
45 than we do, in different orders, etc).
47 It consists of five steps:
49 1. Breaking up subtract operations into addition + negate, where
50 it would promote the reassociation of adds.
52 2. Left linearization of the expression trees, so that (A+B)+(C+D)
53 becomes (((A+B)+C)+D), which is easier for us to rewrite later.
54 During linearization, we place the operands of the binary
55 expressions into a vector of operand_entry_t
57 3. Optimization of the operand lists, eliminating things like a +
60 4. Rewrite the expression trees we linearized and optimized so
61 they are in proper rank order.
63 5. Repropagate negates, as nothing else will clean it up ATM.
65 A bit of theory on #4, since nobody seems to write anything down
66 about why it makes sense to do it the way they do it:
68 We could do this much nicer theoretically, but don't (for reasons
69 explained after how to do it theoretically nice :P).
71 In order to promote the most redundancy elimination, you want
72 binary expressions whose operands are the same rank (or
73 preferably, the same value) exposed to the redundancy eliminator,
74 for possible elimination.
76 So the way to do this if we really cared, is to build the new op
77 tree from the leaves to the roots, merging as you go, and putting the
78 new op on the end of the worklist, until you are left with one
79 thing on the worklist.
81 IE if you have to rewrite the following set of operands (listed with
82 rank in parentheses), with opcode PLUS_EXPR:
84 a (1), b (1), c (1), d (2), e (2)
87 We start with our merge worklist empty, and the ops list with all of
90 You want to first merge all leaves of the same rank, as much as
93 So first build a binary op of
95 mergetmp = a + b, and put "mergetmp" on the merge worklist.
97 Because there is no three operand form of PLUS_EXPR, c is not going to
98 be exposed to redundancy elimination as a rank 1 operand.
100 So you might as well throw it on the merge worklist (you could also
101 consider it to now be a rank two operand, and merge it with d and e,
102 but in this case, you then have evicted e from a binary op. So at
103 least in this situation, you can't win.)
105 Then build a binary op of d + e
108 and put mergetmp2 on the merge worklist.
110 so merge worklist = {mergetmp, c, mergetmp2}
112 Continue building binary ops of these operations until you have only
113 one operation left on the worklist.
118 mergetmp3 = mergetmp + c
120 worklist = {mergetmp2, mergetmp3}
122 mergetmp4 = mergetmp2 + mergetmp3
124 worklist = {mergetmp4}
126 because we have one operation left, we can now just set the original
127 statement equal to the result of that operation.
129 This will at least expose a + b and d + e to redundancy elimination
130 as binary operations.
132 For extra points, you can reuse the old statements to build the
133 mergetmps, since you shouldn't run out.
135 So why don't we do this?
137 Because it's expensive, and rarely will help. Most trees we are
138 reassociating have 3 or less ops. If they have 2 ops, they already
139 will be written into a nice single binary op. If you have 3 ops, a
140 single simple check suffices to tell you whether the first two are of the
141 same rank. If so, you know to order it
144 newstmt = mergetmp + op3
148 newstmt = mergetmp + op1
150 If all three are of the same rank, you can't expose them all in a
151 single binary operator anyway, so the above is *still* the best you
154 Thus, this is what we do. When we have three ops left, we check to see
155 what order to put them in, and call it a day. As a nod to vector sum
156 reduction, we check if any of the ops are really a phi node that is a
157 destructive update for the associating op, and keep the destructive
158 update together for vector sum reduction recognition. */
165 int constants_eliminated
;
170 /* Operator, rank pair. */
171 typedef struct operand_entry
177 static alloc_pool operand_entry_pool
;
180 /* Starting rank number for a given basic block, so that we can rank
181 operations using unmovable instructions in that BB based on the bb
183 static long *bb_rank
;
185 /* Operand->rank hashtable. */
186 static struct pointer_map_t
*operand_rank
;
189 /* Look up the operand rank structure for expression E. */
192 find_operand_rank (tree e
)
194 void **slot
= pointer_map_contains (operand_rank
, e
);
195 return slot
? (long) *slot
: -1;
198 /* Insert {E,RANK} into the operand rank hashtable. */
201 insert_operand_rank (tree e
, long rank
)
204 gcc_assert (rank
> 0);
205 slot
= pointer_map_insert (operand_rank
, e
);
207 *slot
= (void *) rank
;
210 /* Given an expression E, return the rank of the expression. */
215 /* Constants have rank 0. */
216 if (is_gimple_min_invariant (e
))
219 /* SSA_NAME's have the rank of the expression they are the result
221 For globals and uninitialized values, the rank is 0.
222 For function arguments, use the pre-setup rank.
223 For PHI nodes, stores, asm statements, etc, we use the rank of
225 For simple operations, the rank is the maximum rank of any of
226 its operands, or the bb_rank, whichever is less.
227 I make no claims that this is optimal, however, it gives good
230 if (TREE_CODE (e
) == SSA_NAME
)
236 if (TREE_CODE (SSA_NAME_VAR (e
)) == PARM_DECL
237 && SSA_NAME_IS_DEFAULT_DEF (e
))
238 return find_operand_rank (e
);
240 stmt
= SSA_NAME_DEF_STMT (e
);
241 if (gimple_bb (stmt
) == NULL
)
244 if (!is_gimple_assign (stmt
)
245 || !ZERO_SSA_OPERANDS (stmt
, SSA_OP_VIRTUAL_DEFS
))
246 return bb_rank
[gimple_bb (stmt
)->index
];
248 /* If we already have a rank for this expression, use that. */
249 rank
= find_operand_rank (e
);
253 /* Otherwise, find the maximum rank for the operands, or the bb
254 rank, whichever is less. */
256 maxrank
= bb_rank
[gimple_bb(stmt
)->index
];
257 if (gimple_assign_single_p (stmt
))
259 tree rhs
= gimple_assign_rhs1 (stmt
);
260 n
= TREE_OPERAND_LENGTH (rhs
);
262 rank
= MAX (rank
, get_rank (rhs
));
266 i
< n
&& TREE_OPERAND (rhs
, i
) && rank
!= maxrank
; i
++)
267 rank
= MAX(rank
, get_rank (TREE_OPERAND (rhs
, i
)));
272 n
= gimple_num_ops (stmt
);
273 for (i
= 1; i
< n
&& rank
!= maxrank
; i
++)
275 gcc_assert (gimple_op (stmt
, i
));
276 rank
= MAX(rank
, get_rank (gimple_op (stmt
, i
)));
280 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
282 fprintf (dump_file
, "Rank for ");
283 print_generic_expr (dump_file
, e
, 0);
284 fprintf (dump_file
, " is %ld\n", (rank
+ 1));
287 /* Note the rank in the hashtable so we don't recompute it. */
288 insert_operand_rank (e
, (rank
+ 1));
292 /* Globals, etc, are rank 0 */
296 DEF_VEC_P(operand_entry_t
);
297 DEF_VEC_ALLOC_P(operand_entry_t
, heap
);
299 /* We want integer ones to end up last no matter what, since they are
300 the ones we can do the most with. */
301 #define INTEGER_CONST_TYPE 1 << 3
302 #define FLOAT_CONST_TYPE 1 << 2
303 #define OTHER_CONST_TYPE 1 << 1
305 /* Classify an invariant tree into integer, float, or other, so that
306 we can sort them to be near other constants of the same type. */
308 constant_type (tree t
)
310 if (INTEGRAL_TYPE_P (TREE_TYPE (t
)))
311 return INTEGER_CONST_TYPE
;
312 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t
)))
313 return FLOAT_CONST_TYPE
;
315 return OTHER_CONST_TYPE
;
318 /* qsort comparison function to sort operand entries PA and PB by rank
319 so that the sorted array is ordered by rank in decreasing order. */
321 sort_by_operand_rank (const void *pa
, const void *pb
)
323 const operand_entry_t oea
= *(const operand_entry_t
*)pa
;
324 const operand_entry_t oeb
= *(const operand_entry_t
*)pb
;
326 /* It's nicer for optimize_expression if constants that are likely
327 to fold when added/multiplied//whatever are put next to each
328 other. Since all constants have rank 0, order them by type. */
329 if (oeb
->rank
== 0 && oea
->rank
== 0)
330 return constant_type (oeb
->op
) - constant_type (oea
->op
);
332 /* Lastly, make sure the versions that are the same go next to each
333 other. We use SSA_NAME_VERSION because it's stable. */
334 if ((oeb
->rank
- oea
->rank
== 0)
335 && TREE_CODE (oea
->op
) == SSA_NAME
336 && TREE_CODE (oeb
->op
) == SSA_NAME
)
337 return SSA_NAME_VERSION (oeb
->op
) - SSA_NAME_VERSION (oea
->op
);
339 return oeb
->rank
- oea
->rank
;
342 /* Add an operand entry to *OPS for the tree operand OP. */
345 add_to_ops_vec (VEC(operand_entry_t
, heap
) **ops
, tree op
)
347 operand_entry_t oe
= (operand_entry_t
) pool_alloc (operand_entry_pool
);
350 oe
->rank
= get_rank (op
);
351 VEC_safe_push (operand_entry_t
, heap
, *ops
, oe
);
354 /* Return true if STMT is reassociable operation containing a binary
355 operation with tree code CODE, and is inside LOOP. */
358 is_reassociable_op (gimple stmt
, enum tree_code code
, struct loop
*loop
)
360 basic_block bb
= gimple_bb (stmt
);
362 if (gimple_bb (stmt
) == NULL
)
365 if (!flow_bb_inside_loop_p (loop
, bb
))
368 if (is_gimple_assign (stmt
)
369 && gimple_assign_rhs_code (stmt
) == code
370 && has_single_use (gimple_assign_lhs (stmt
)))
377 /* Given NAME, if NAME is defined by a unary operation OPCODE, return the
378 operand of the negate operation. Otherwise, return NULL. */
381 get_unary_op (tree name
, enum tree_code opcode
)
383 gimple stmt
= SSA_NAME_DEF_STMT (name
);
385 if (!is_gimple_assign (stmt
))
388 if (gimple_assign_rhs_code (stmt
) == opcode
)
389 return gimple_assign_rhs1 (stmt
);
393 /* If CURR and LAST are a pair of ops that OPCODE allows us to
394 eliminate through equivalences, do so, remove them from OPS, and
395 return true. Otherwise, return false. */
398 eliminate_duplicate_pair (enum tree_code opcode
,
399 VEC (operand_entry_t
, heap
) **ops
,
402 operand_entry_t curr
,
403 operand_entry_t last
)
406 /* If we have two of the same op, and the opcode is & |, min, or max,
407 we can eliminate one of them.
408 If we have two of the same op, and the opcode is ^, we can
409 eliminate both of them. */
411 if (last
&& last
->op
== curr
->op
)
419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
421 fprintf (dump_file
, "Equivalence: ");
422 print_generic_expr (dump_file
, curr
->op
, 0);
423 fprintf (dump_file
, " [&|minmax] ");
424 print_generic_expr (dump_file
, last
->op
, 0);
425 fprintf (dump_file
, " -> ");
426 print_generic_stmt (dump_file
, last
->op
, 0);
429 VEC_ordered_remove (operand_entry_t
, *ops
, i
);
430 reassociate_stats
.ops_eliminated
++;
435 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
437 fprintf (dump_file
, "Equivalence: ");
438 print_generic_expr (dump_file
, curr
->op
, 0);
439 fprintf (dump_file
, " ^ ");
440 print_generic_expr (dump_file
, last
->op
, 0);
441 fprintf (dump_file
, " -> nothing\n");
444 reassociate_stats
.ops_eliminated
+= 2;
446 if (VEC_length (operand_entry_t
, *ops
) == 2)
448 VEC_free (operand_entry_t
, heap
, *ops
);
450 add_to_ops_vec (ops
, fold_convert (TREE_TYPE (last
->op
),
456 VEC_ordered_remove (operand_entry_t
, *ops
, i
-1);
457 VEC_ordered_remove (operand_entry_t
, *ops
, i
-1);
469 /* If OPCODE is PLUS_EXPR, CURR->OP is really a negate expression,
470 look in OPS for a corresponding positive operation to cancel it
471 out. If we find one, remove the other from OPS, replace
472 OPS[CURRINDEX] with 0, and return true. Otherwise, return
476 eliminate_plus_minus_pair (enum tree_code opcode
,
477 VEC (operand_entry_t
, heap
) **ops
,
478 unsigned int currindex
,
479 operand_entry_t curr
)
485 if (opcode
!= PLUS_EXPR
|| TREE_CODE (curr
->op
) != SSA_NAME
)
488 negateop
= get_unary_op (curr
->op
, NEGATE_EXPR
);
489 if (negateop
== NULL_TREE
)
492 /* Any non-negated version will have a rank that is one less than
493 the current rank. So once we hit those ranks, if we don't find
496 for (i
= currindex
+ 1;
497 VEC_iterate (operand_entry_t
, *ops
, i
, oe
)
498 && oe
->rank
>= curr
->rank
- 1 ;
501 if (oe
->op
== negateop
)
504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
506 fprintf (dump_file
, "Equivalence: ");
507 print_generic_expr (dump_file
, negateop
, 0);
508 fprintf (dump_file
, " + -");
509 print_generic_expr (dump_file
, oe
->op
, 0);
510 fprintf (dump_file
, " -> 0\n");
513 VEC_ordered_remove (operand_entry_t
, *ops
, i
);
514 add_to_ops_vec (ops
, fold_convert(TREE_TYPE (oe
->op
),
516 VEC_ordered_remove (operand_entry_t
, *ops
, currindex
);
517 reassociate_stats
.ops_eliminated
++;
526 /* If OPCODE is BIT_IOR_EXPR, BIT_AND_EXPR, and, CURR->OP is really a
527 bitwise not expression, look in OPS for a corresponding operand to
528 cancel it out. If we find one, remove the other from OPS, replace
529 OPS[CURRINDEX] with 0, and return true. Otherwise, return
533 eliminate_not_pairs (enum tree_code opcode
,
534 VEC (operand_entry_t
, heap
) **ops
,
535 unsigned int currindex
,
536 operand_entry_t curr
)
542 if ((opcode
!= BIT_IOR_EXPR
&& opcode
!= BIT_AND_EXPR
)
543 || TREE_CODE (curr
->op
) != SSA_NAME
)
546 notop
= get_unary_op (curr
->op
, BIT_NOT_EXPR
);
547 if (notop
== NULL_TREE
)
550 /* Any non-not version will have a rank that is one less than
551 the current rank. So once we hit those ranks, if we don't find
554 for (i
= currindex
+ 1;
555 VEC_iterate (operand_entry_t
, *ops
, i
, oe
)
556 && oe
->rank
>= curr
->rank
- 1;
561 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
563 fprintf (dump_file
, "Equivalence: ");
564 print_generic_expr (dump_file
, notop
, 0);
565 if (opcode
== BIT_AND_EXPR
)
566 fprintf (dump_file
, " & ~");
567 else if (opcode
== BIT_IOR_EXPR
)
568 fprintf (dump_file
, " | ~");
569 print_generic_expr (dump_file
, oe
->op
, 0);
570 if (opcode
== BIT_AND_EXPR
)
571 fprintf (dump_file
, " -> 0\n");
572 else if (opcode
== BIT_IOR_EXPR
)
573 fprintf (dump_file
, " -> -1\n");
576 if (opcode
== BIT_AND_EXPR
)
577 oe
->op
= fold_convert (TREE_TYPE (oe
->op
), integer_zero_node
);
578 else if (opcode
== BIT_IOR_EXPR
)
579 oe
->op
= build_low_bits_mask (TREE_TYPE (oe
->op
),
580 TYPE_PRECISION (TREE_TYPE (oe
->op
)));
582 reassociate_stats
.ops_eliminated
583 += VEC_length (operand_entry_t
, *ops
) - 1;
584 VEC_free (operand_entry_t
, heap
, *ops
);
586 VEC_safe_push (operand_entry_t
, heap
, *ops
, oe
);
594 /* Use constant value that may be present in OPS to try to eliminate
595 operands. Note that this function is only really used when we've
596 eliminated ops for other reasons, or merged constants. Across
597 single statements, fold already does all of this, plus more. There
598 is little point in duplicating logic, so I've only included the
599 identities that I could ever construct testcases to trigger. */
602 eliminate_using_constants (enum tree_code opcode
,
603 VEC(operand_entry_t
, heap
) **ops
)
605 operand_entry_t oelast
= VEC_last (operand_entry_t
, *ops
);
606 tree type
= TREE_TYPE (oelast
->op
);
608 if (oelast
->rank
== 0
609 && (INTEGRAL_TYPE_P (type
) || FLOAT_TYPE_P (type
)))
614 if (integer_zerop (oelast
->op
))
616 if (VEC_length (operand_entry_t
, *ops
) != 1)
618 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
619 fprintf (dump_file
, "Found & 0, removing all other ops\n");
621 reassociate_stats
.ops_eliminated
622 += VEC_length (operand_entry_t
, *ops
) - 1;
624 VEC_free (operand_entry_t
, heap
, *ops
);
626 VEC_safe_push (operand_entry_t
, heap
, *ops
, oelast
);
630 else if (integer_all_onesp (oelast
->op
))
632 if (VEC_length (operand_entry_t
, *ops
) != 1)
634 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
635 fprintf (dump_file
, "Found & -1, removing\n");
636 VEC_pop (operand_entry_t
, *ops
);
637 reassociate_stats
.ops_eliminated
++;
642 if (integer_all_onesp (oelast
->op
))
644 if (VEC_length (operand_entry_t
, *ops
) != 1)
646 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
647 fprintf (dump_file
, "Found | -1, removing all other ops\n");
649 reassociate_stats
.ops_eliminated
650 += VEC_length (operand_entry_t
, *ops
) - 1;
652 VEC_free (operand_entry_t
, heap
, *ops
);
654 VEC_safe_push (operand_entry_t
, heap
, *ops
, oelast
);
658 else if (integer_zerop (oelast
->op
))
660 if (VEC_length (operand_entry_t
, *ops
) != 1)
662 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
663 fprintf (dump_file
, "Found | 0, removing\n");
664 VEC_pop (operand_entry_t
, *ops
);
665 reassociate_stats
.ops_eliminated
++;
670 if (integer_zerop (oelast
->op
)
671 || (FLOAT_TYPE_P (type
)
672 && !HONOR_NANS (TYPE_MODE (type
))
673 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type
))
674 && real_zerop (oelast
->op
)))
676 if (VEC_length (operand_entry_t
, *ops
) != 1)
678 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
679 fprintf (dump_file
, "Found * 0, removing all other ops\n");
681 reassociate_stats
.ops_eliminated
682 += VEC_length (operand_entry_t
, *ops
) - 1;
683 VEC_free (operand_entry_t
, heap
, *ops
);
685 VEC_safe_push (operand_entry_t
, heap
, *ops
, oelast
);
689 else if (integer_onep (oelast
->op
)
690 || (FLOAT_TYPE_P (type
)
691 && !HONOR_SNANS (TYPE_MODE (type
))
692 && real_onep (oelast
->op
)))
694 if (VEC_length (operand_entry_t
, *ops
) != 1)
696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
697 fprintf (dump_file
, "Found * 1, removing\n");
698 VEC_pop (operand_entry_t
, *ops
);
699 reassociate_stats
.ops_eliminated
++;
707 if (integer_zerop (oelast
->op
)
708 || (FLOAT_TYPE_P (type
)
709 && (opcode
== PLUS_EXPR
|| opcode
== MINUS_EXPR
)
710 && fold_real_zero_addition_p (type
, oelast
->op
,
711 opcode
== MINUS_EXPR
)))
713 if (VEC_length (operand_entry_t
, *ops
) != 1)
715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
716 fprintf (dump_file
, "Found [|^+] 0, removing\n");
717 VEC_pop (operand_entry_t
, *ops
);
718 reassociate_stats
.ops_eliminated
++;
730 static void linearize_expr_tree (VEC(operand_entry_t
, heap
) **, gimple
,
733 /* Structure for tracking and counting operands. */
734 typedef struct oecount_s
{
736 enum tree_code oecode
;
741 DEF_VEC_ALLOC_O(oecount
,heap
);
743 /* The heap for the oecount hashtable and the sorted list of operands. */
744 static VEC (oecount
, heap
) *cvec
;
746 /* Hash function for oecount. */
749 oecount_hash (const void *p
)
751 const oecount
*c
= VEC_index (oecount
, cvec
, (size_t)p
- 42);
752 return htab_hash_pointer (c
->op
) ^ (hashval_t
)c
->oecode
;
755 /* Comparison function for oecount. */
758 oecount_eq (const void *p1
, const void *p2
)
760 const oecount
*c1
= VEC_index (oecount
, cvec
, (size_t)p1
- 42);
761 const oecount
*c2
= VEC_index (oecount
, cvec
, (size_t)p2
- 42);
762 return (c1
->oecode
== c2
->oecode
763 && c1
->op
== c2
->op
);
766 /* Comparison function for qsort sorting oecount elements by count. */
769 oecount_cmp (const void *p1
, const void *p2
)
771 const oecount
*c1
= (const oecount
*)p1
;
772 const oecount
*c2
= (const oecount
*)p2
;
773 return c1
->cnt
- c2
->cnt
;
776 /* Walks the linear chain with result *DEF searching for an operation
777 with operand OP and code OPCODE removing that from the chain. *DEF
778 is updated if there is only one operand but no operation left. */
781 zero_one_operation (tree
*def
, enum tree_code opcode
, tree op
)
783 gimple stmt
= SSA_NAME_DEF_STMT (*def
);
787 tree name
= gimple_assign_rhs1 (stmt
);
789 /* If this is the operation we look for and one of the operands
790 is ours simply propagate the other operand into the stmts
792 if (gimple_assign_rhs_code (stmt
) == opcode
794 || gimple_assign_rhs2 (stmt
) == op
))
798 gimple_stmt_iterator gsi
;
800 name
= gimple_assign_rhs2 (stmt
);
801 gcc_assert (has_single_use (gimple_assign_lhs (stmt
)));
802 single_imm_use (gimple_assign_lhs (stmt
), &use
, &use_stmt
);
803 if (gimple_assign_lhs (stmt
) == *def
)
806 if (TREE_CODE (name
) != SSA_NAME
)
807 update_stmt (use_stmt
);
808 gsi
= gsi_for_stmt (stmt
);
809 gsi_remove (&gsi
, true);
814 /* Continue walking the chain. */
815 gcc_assert (name
!= op
816 && TREE_CODE (name
) == SSA_NAME
);
817 stmt
= SSA_NAME_DEF_STMT (name
);
822 /* Builds one statement performing OP1 OPCODE OP2 using TMPVAR for
823 the result. Places the statement after the definition of either
824 OP1 or OP2. Returns the new statement. */
827 build_and_add_sum (tree tmpvar
, tree op1
, tree op2
, enum tree_code opcode
)
829 gimple op1def
= NULL
, op2def
= NULL
;
830 gimple_stmt_iterator gsi
;
834 /* Create the addition statement. */
835 sum
= gimple_build_assign_with_ops (opcode
, tmpvar
, op1
, op2
);
836 op
= make_ssa_name (tmpvar
, sum
);
837 gimple_assign_set_lhs (sum
, op
);
839 /* Find an insertion place and insert. */
840 if (TREE_CODE (op1
) == SSA_NAME
)
841 op1def
= SSA_NAME_DEF_STMT (op1
);
842 if (TREE_CODE (op2
) == SSA_NAME
)
843 op2def
= SSA_NAME_DEF_STMT (op2
);
844 if ((!op1def
|| gimple_nop_p (op1def
))
845 && (!op2def
|| gimple_nop_p (op2def
)))
847 gsi
= gsi_start_bb (single_succ (ENTRY_BLOCK_PTR
));
848 gsi_insert_before (&gsi
, sum
, GSI_NEW_STMT
);
850 else if ((!op1def
|| gimple_nop_p (op1def
))
851 || (op2def
&& !gimple_nop_p (op2def
)
852 && stmt_dominates_stmt_p (op1def
, op2def
)))
854 if (gimple_code (op2def
) == GIMPLE_PHI
)
856 gsi
= gsi_start_bb (gimple_bb (op2def
));
857 gsi_insert_before (&gsi
, sum
, GSI_NEW_STMT
);
861 if (!stmt_ends_bb_p (op2def
))
863 gsi
= gsi_for_stmt (op2def
);
864 gsi_insert_after (&gsi
, sum
, GSI_NEW_STMT
);
871 FOR_EACH_EDGE (e
, ei
, gimple_bb (op2def
)->succs
)
872 if (e
->flags
& EDGE_FALLTHRU
)
873 gsi_insert_on_edge_immediate (e
, sum
);
879 if (gimple_code (op1def
) == GIMPLE_PHI
)
881 gsi
= gsi_start_bb (gimple_bb (op1def
));
882 gsi_insert_before (&gsi
, sum
, GSI_NEW_STMT
);
886 if (!stmt_ends_bb_p (op1def
))
888 gsi
= gsi_for_stmt (op1def
);
889 gsi_insert_after (&gsi
, sum
, GSI_NEW_STMT
);
896 FOR_EACH_EDGE (e
, ei
, gimple_bb (op1def
)->succs
)
897 if (e
->flags
& EDGE_FALLTHRU
)
898 gsi_insert_on_edge_immediate (e
, sum
);
907 /* Perform un-distribution of divisions and multiplications.
908 A * X + B * X is transformed into (A + B) * X and A / X + B / X
909 to (A + B) / X for real X.
911 The algorithm is organized as follows.
913 - First we walk the addition chain *OPS looking for summands that
914 are defined by a multiplication or a real division. This results
915 in the candidates bitmap with relevant indices into *OPS.
917 - Second we build the chains of multiplications or divisions for
918 these candidates, counting the number of occurences of (operand, code)
919 pairs in all of the candidates chains.
921 - Third we sort the (operand, code) pairs by number of occurence and
922 process them starting with the pair with the most uses.
924 * For each such pair we walk the candidates again to build a
925 second candidate bitmap noting all multiplication/division chains
926 that have at least one occurence of (operand, code).
928 * We build an alternate addition chain only covering these
929 candidates with one (operand, code) operation removed from their
930 multiplication/division chain.
932 * The first candidate gets replaced by the alternate addition chain
933 multiplied/divided by the operand.
935 * All candidate chains get disabled for further processing and
936 processing of (operand, code) pairs continues.
938 The alternate addition chains built are re-processed by the main
939 reassociation algorithm which allows optimizing a * x * y + b * y * x
940 to (a + b ) * x * y in one invocation of the reassociation pass. */
943 undistribute_ops_list (enum tree_code opcode
,
944 VEC (operand_entry_t
, heap
) **ops
, struct loop
*loop
)
946 unsigned int length
= VEC_length (operand_entry_t
, *ops
);
949 sbitmap candidates
, candidates2
;
950 unsigned nr_candidates
, nr_candidates2
;
951 sbitmap_iterator sbi0
;
952 VEC (operand_entry_t
, heap
) **subops
;
954 bool changed
= false;
957 || opcode
!= PLUS_EXPR
)
960 /* Build a list of candidates to process. */
961 candidates
= sbitmap_alloc (length
);
962 sbitmap_zero (candidates
);
964 for (i
= 0; VEC_iterate (operand_entry_t
, *ops
, i
, oe1
); ++i
)
966 enum tree_code dcode
;
969 if (TREE_CODE (oe1
->op
) != SSA_NAME
)
971 oe1def
= SSA_NAME_DEF_STMT (oe1
->op
);
972 if (!is_gimple_assign (oe1def
))
974 dcode
= gimple_assign_rhs_code (oe1def
);
975 if ((dcode
!= MULT_EXPR
976 && dcode
!= RDIV_EXPR
)
977 || !is_reassociable_op (oe1def
, dcode
, loop
))
980 SET_BIT (candidates
, i
);
984 if (nr_candidates
< 2)
986 sbitmap_free (candidates
);
990 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
992 fprintf (dump_file
, "searching for un-distribute opportunities ");
993 print_generic_expr (dump_file
,
994 VEC_index (operand_entry_t
, *ops
,
995 sbitmap_first_set_bit (candidates
))->op
, 0);
996 fprintf (dump_file
, " %d\n", nr_candidates
);
999 /* Build linearized sub-operand lists and the counting table. */
1001 ctable
= htab_create (15, oecount_hash
, oecount_eq
, NULL
);
1002 subops
= XCNEWVEC (VEC (operand_entry_t
, heap
) *,
1003 VEC_length (operand_entry_t
, *ops
));
1004 EXECUTE_IF_SET_IN_SBITMAP (candidates
, 0, i
, sbi0
)
1007 enum tree_code oecode
;
1010 oedef
= SSA_NAME_DEF_STMT (VEC_index (operand_entry_t
, *ops
, i
)->op
);
1011 oecode
= gimple_assign_rhs_code (oedef
);
1012 linearize_expr_tree (&subops
[i
], oedef
,
1013 associative_tree_code (oecode
), false);
1015 for (j
= 0; VEC_iterate (operand_entry_t
, subops
[i
], j
, oe1
); ++j
)
1023 VEC_safe_push (oecount
, heap
, cvec
, &c
);
1024 idx
= VEC_length (oecount
, cvec
) + 41;
1025 slot
= htab_find_slot (ctable
, (void *)idx
, INSERT
);
1028 *slot
= (void *)idx
;
1032 VEC_pop (oecount
, cvec
);
1033 VEC_index (oecount
, cvec
, (size_t)*slot
- 42)->cnt
++;
1037 htab_delete (ctable
);
1039 /* Sort the counting table. */
1040 qsort (VEC_address (oecount
, cvec
), VEC_length (oecount
, cvec
),
1041 sizeof (oecount
), oecount_cmp
);
1043 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1046 fprintf (dump_file
, "Candidates:\n");
1047 for (j
= 0; VEC_iterate (oecount
, cvec
, j
, c
); ++j
)
1049 fprintf (dump_file
, " %u %s: ", c
->cnt
,
1050 c
->oecode
== MULT_EXPR
1051 ? "*" : c
->oecode
== RDIV_EXPR
? "/" : "?");
1052 print_generic_expr (dump_file
, c
->op
, 0);
1053 fprintf (dump_file
, "\n");
1057 /* Process the (operand, code) pairs in order of most occurence. */
1058 candidates2
= sbitmap_alloc (length
);
1059 while (!VEC_empty (oecount
, cvec
))
1061 oecount
*c
= VEC_last (oecount
, cvec
);
1065 /* Now collect the operands in the outer chain that contain
1066 the common operand in their inner chain. */
1067 sbitmap_zero (candidates2
);
1069 EXECUTE_IF_SET_IN_SBITMAP (candidates
, 0, i
, sbi0
)
1072 enum tree_code oecode
;
1074 tree op
= VEC_index (operand_entry_t
, *ops
, i
)->op
;
1076 /* If we undistributed in this chain already this may be
1078 if (TREE_CODE (op
) != SSA_NAME
)
1081 oedef
= SSA_NAME_DEF_STMT (op
);
1082 oecode
= gimple_assign_rhs_code (oedef
);
1083 if (oecode
!= c
->oecode
)
1086 for (j
= 0; VEC_iterate (operand_entry_t
, subops
[i
], j
, oe1
); ++j
)
1088 if (oe1
->op
== c
->op
)
1090 SET_BIT (candidates2
, i
);
1097 if (nr_candidates2
>= 2)
1099 operand_entry_t oe1
, oe2
;
1102 int first
= sbitmap_first_set_bit (candidates2
);
1104 /* Build the new addition chain. */
1105 oe1
= VEC_index (operand_entry_t
, *ops
, first
);
1106 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1108 fprintf (dump_file
, "Building (");
1109 print_generic_expr (dump_file
, oe1
->op
, 0);
1111 tmpvar
= create_tmp_var (TREE_TYPE (oe1
->op
), NULL
);
1112 add_referenced_var (tmpvar
);
1113 zero_one_operation (&oe1
->op
, c
->oecode
, c
->op
);
1114 EXECUTE_IF_SET_IN_SBITMAP (candidates2
, first
+1, i
, sbi0
)
1117 oe2
= VEC_index (operand_entry_t
, *ops
, i
);
1118 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1120 fprintf (dump_file
, " + ");
1121 print_generic_expr (dump_file
, oe2
->op
, 0);
1123 zero_one_operation (&oe2
->op
, c
->oecode
, c
->op
);
1124 sum
= build_and_add_sum (tmpvar
, oe1
->op
, oe2
->op
, opcode
);
1125 oe2
->op
= fold_convert (TREE_TYPE (oe2
->op
), integer_zero_node
);
1127 oe1
->op
= gimple_get_lhs (sum
);
1130 /* Apply the multiplication/division. */
1131 prod
= build_and_add_sum (tmpvar
, oe1
->op
, c
->op
, c
->oecode
);
1132 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1134 fprintf (dump_file
, ") %s ", c
->oecode
== MULT_EXPR
? "*" : "/");
1135 print_generic_expr (dump_file
, c
->op
, 0);
1136 fprintf (dump_file
, "\n");
1139 /* Record it in the addition chain and disable further
1140 undistribution with this op. */
1141 oe1
->op
= gimple_assign_lhs (prod
);
1142 oe1
->rank
= get_rank (oe1
->op
);
1143 VEC_free (operand_entry_t
, heap
, subops
[first
]);
1148 VEC_pop (oecount
, cvec
);
1151 for (i
= 0; i
< VEC_length (operand_entry_t
, *ops
); ++i
)
1152 VEC_free (operand_entry_t
, heap
, subops
[i
]);
1154 VEC_free (oecount
, heap
, cvec
);
1155 sbitmap_free (candidates
);
1156 sbitmap_free (candidates2
);
1162 /* Perform various identities and other optimizations on the list of
1163 operand entries, stored in OPS. The tree code for the binary
1164 operation between all the operands is OPCODE. */
1167 optimize_ops_list (enum tree_code opcode
,
1168 VEC (operand_entry_t
, heap
) **ops
)
1170 unsigned int length
= VEC_length (operand_entry_t
, *ops
);
1173 operand_entry_t oelast
= NULL
;
1174 bool iterate
= false;
1179 oelast
= VEC_last (operand_entry_t
, *ops
);
1181 /* If the last two are constants, pop the constants off, merge them
1182 and try the next two. */
1183 if (oelast
->rank
== 0 && is_gimple_min_invariant (oelast
->op
))
1185 operand_entry_t oelm1
= VEC_index (operand_entry_t
, *ops
, length
- 2);
1187 if (oelm1
->rank
== 0
1188 && is_gimple_min_invariant (oelm1
->op
)
1189 && useless_type_conversion_p (TREE_TYPE (oelm1
->op
),
1190 TREE_TYPE (oelast
->op
)))
1192 tree folded
= fold_binary (opcode
, TREE_TYPE (oelm1
->op
),
1193 oelm1
->op
, oelast
->op
);
1195 if (folded
&& is_gimple_min_invariant (folded
))
1197 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1198 fprintf (dump_file
, "Merging constants\n");
1200 VEC_pop (operand_entry_t
, *ops
);
1201 VEC_pop (operand_entry_t
, *ops
);
1203 add_to_ops_vec (ops
, folded
);
1204 reassociate_stats
.constants_eliminated
++;
1206 optimize_ops_list (opcode
, ops
);
1212 eliminate_using_constants (opcode
, ops
);
1215 for (i
= 0; VEC_iterate (operand_entry_t
, *ops
, i
, oe
);)
1219 if (eliminate_not_pairs (opcode
, ops
, i
, oe
))
1221 if (eliminate_duplicate_pair (opcode
, ops
, &done
, i
, oe
, oelast
)
1222 || (!done
&& eliminate_plus_minus_pair (opcode
, ops
, i
, oe
)))
1234 length
= VEC_length (operand_entry_t
, *ops
);
1235 oelast
= VEC_last (operand_entry_t
, *ops
);
1238 optimize_ops_list (opcode
, ops
);
1241 /* Return true if OPERAND is defined by a PHI node which uses the LHS
1242 of STMT in it's operands. This is also known as a "destructive
1243 update" operation. */
1246 is_phi_for_stmt (gimple stmt
, tree operand
)
1250 use_operand_p arg_p
;
1253 if (TREE_CODE (operand
) != SSA_NAME
)
1256 lhs
= gimple_assign_lhs (stmt
);
1258 def_stmt
= SSA_NAME_DEF_STMT (operand
);
1259 if (gimple_code (def_stmt
) != GIMPLE_PHI
)
1262 FOR_EACH_PHI_ARG (arg_p
, def_stmt
, i
, SSA_OP_USE
)
1263 if (lhs
== USE_FROM_PTR (arg_p
))
1268 /* Remove def stmt of VAR if VAR has zero uses and recurse
1269 on rhs1 operand if so. */
1272 remove_visited_stmt_chain (tree var
)
1275 gimple_stmt_iterator gsi
;
1279 if (TREE_CODE (var
) != SSA_NAME
|| !has_zero_uses (var
))
1281 stmt
= SSA_NAME_DEF_STMT (var
);
1282 if (!gimple_visited_p (stmt
))
1284 var
= gimple_assign_rhs1 (stmt
);
1285 gsi
= gsi_for_stmt (stmt
);
1286 gsi_remove (&gsi
, true);
1287 release_defs (stmt
);
1291 /* Recursively rewrite our linearized statements so that the operators
1292 match those in OPS[OPINDEX], putting the computation in rank
1296 rewrite_expr_tree (gimple stmt
, unsigned int opindex
,
1297 VEC(operand_entry_t
, heap
) * ops
, bool moved
)
1299 tree rhs1
= gimple_assign_rhs1 (stmt
);
1300 tree rhs2
= gimple_assign_rhs2 (stmt
);
1303 /* If we have three operands left, then we want to make sure the one
1304 that gets the double binary op are the ones with the same rank.
1306 The alternative we try is to see if this is a destructive
1307 update style statement, which is like:
1310 In that case, we want to use the destructive update form to
1311 expose the possible vectorizer sum reduction opportunity.
1312 In that case, the third operand will be the phi node.
1314 We could, of course, try to be better as noted above, and do a
1315 lot of work to try to find these opportunities in >3 operand
1316 cases, but it is unlikely to be worth it. */
1317 if (opindex
+ 3 == VEC_length (operand_entry_t
, ops
))
1319 operand_entry_t oe1
, oe2
, oe3
;
1321 oe1
= VEC_index (operand_entry_t
, ops
, opindex
);
1322 oe2
= VEC_index (operand_entry_t
, ops
, opindex
+ 1);
1323 oe3
= VEC_index (operand_entry_t
, ops
, opindex
+ 2);
1325 if ((oe1
->rank
== oe2
->rank
1326 && oe2
->rank
!= oe3
->rank
)
1327 || (is_phi_for_stmt (stmt
, oe3
->op
)
1328 && !is_phi_for_stmt (stmt
, oe1
->op
)
1329 && !is_phi_for_stmt (stmt
, oe2
->op
)))
1331 struct operand_entry temp
= *oe3
;
1333 oe3
->rank
= oe1
->rank
;
1335 oe1
->rank
= temp
.rank
;
1337 else if ((oe1
->rank
== oe3
->rank
1338 && oe2
->rank
!= oe3
->rank
)
1339 || (is_phi_for_stmt (stmt
, oe2
->op
)
1340 && !is_phi_for_stmt (stmt
, oe1
->op
)
1341 && !is_phi_for_stmt (stmt
, oe3
->op
)))
1343 struct operand_entry temp
= *oe2
;
1345 oe2
->rank
= oe1
->rank
;
1347 oe1
->rank
= temp
.rank
;
1351 /* The final recursion case for this function is that you have
1352 exactly two operations left.
1353 If we had one exactly one op in the entire list to start with, we
1354 would have never called this function, and the tail recursion
1355 rewrites them one at a time. */
1356 if (opindex
+ 2 == VEC_length (operand_entry_t
, ops
))
1358 operand_entry_t oe1
, oe2
;
1360 oe1
= VEC_index (operand_entry_t
, ops
, opindex
);
1361 oe2
= VEC_index (operand_entry_t
, ops
, opindex
+ 1);
1363 if (rhs1
!= oe1
->op
|| rhs2
!= oe2
->op
)
1365 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1367 fprintf (dump_file
, "Transforming ");
1368 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1371 gimple_assign_set_rhs1 (stmt
, oe1
->op
);
1372 gimple_assign_set_rhs2 (stmt
, oe2
->op
);
1374 if (rhs1
!= oe1
->op
&& rhs1
!= oe2
->op
)
1375 remove_visited_stmt_chain (rhs1
);
1377 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1379 fprintf (dump_file
, " into ");
1380 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1387 /* If we hit here, we should have 3 or more ops left. */
1388 gcc_assert (opindex
+ 2 < VEC_length (operand_entry_t
, ops
));
1390 /* Rewrite the next operator. */
1391 oe
= VEC_index (operand_entry_t
, ops
, opindex
);
1397 gimple_stmt_iterator gsinow
, gsirhs1
;
1398 gimple stmt1
= stmt
, stmt2
;
1401 gsinow
= gsi_for_stmt (stmt
);
1402 count
= VEC_length (operand_entry_t
, ops
) - opindex
- 2;
1403 while (count
-- != 0)
1405 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt1
));
1406 gsirhs1
= gsi_for_stmt (stmt2
);
1407 gsi_move_before (&gsirhs1
, &gsinow
);
1414 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1416 fprintf (dump_file
, "Transforming ");
1417 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1420 gimple_assign_set_rhs2 (stmt
, oe
->op
);
1423 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1425 fprintf (dump_file
, " into ");
1426 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1429 /* Recurse on the LHS of the binary operator, which is guaranteed to
1430 be the non-leaf side. */
1431 rewrite_expr_tree (SSA_NAME_DEF_STMT (rhs1
), opindex
+ 1, ops
, moved
);
1434 /* Transform STMT, which is really (A +B) + (C + D) into the left
1435 linear form, ((A+B)+C)+D.
1436 Recurse on D if necessary. */
1439 linearize_expr (gimple stmt
)
1441 gimple_stmt_iterator gsinow
, gsirhs
;
1442 gimple binlhs
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
1443 gimple binrhs
= SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt
));
1444 enum tree_code rhscode
= gimple_assign_rhs_code (stmt
);
1445 gimple newbinrhs
= NULL
;
1446 struct loop
*loop
= loop_containing_stmt (stmt
);
1448 gcc_assert (is_reassociable_op (binlhs
, rhscode
, loop
)
1449 && is_reassociable_op (binrhs
, rhscode
, loop
));
1451 gsinow
= gsi_for_stmt (stmt
);
1452 gsirhs
= gsi_for_stmt (binrhs
);
1453 gsi_move_before (&gsirhs
, &gsinow
);
1455 gimple_assign_set_rhs2 (stmt
, gimple_assign_rhs1 (binrhs
));
1456 gimple_assign_set_rhs1 (binrhs
, gimple_assign_lhs (binlhs
));
1457 gimple_assign_set_rhs1 (stmt
, gimple_assign_lhs (binrhs
));
1459 if (TREE_CODE (gimple_assign_rhs2 (stmt
)) == SSA_NAME
)
1460 newbinrhs
= SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt
));
1462 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1464 fprintf (dump_file
, "Linearized: ");
1465 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1468 reassociate_stats
.linearized
++;
1469 update_stmt (binrhs
);
1470 update_stmt (binlhs
);
1473 gimple_set_visited (stmt
, true);
1474 gimple_set_visited (binlhs
, true);
1475 gimple_set_visited (binrhs
, true);
1477 /* Tail recurse on the new rhs if it still needs reassociation. */
1478 if (newbinrhs
&& is_reassociable_op (newbinrhs
, rhscode
, loop
))
1479 /* ??? This should probably be linearize_expr (newbinrhs) but I don't
1480 want to change the algorithm while converting to tuples. */
1481 linearize_expr (stmt
);
1484 /* If LHS has a single immediate use that is a GIMPLE_ASSIGN statement, return
1485 it. Otherwise, return NULL. */
1488 get_single_immediate_use (tree lhs
)
1490 use_operand_p immuse
;
1493 if (TREE_CODE (lhs
) == SSA_NAME
1494 && single_imm_use (lhs
, &immuse
, &immusestmt
)
1495 && is_gimple_assign (immusestmt
))
1501 static VEC(tree
, heap
) *broken_up_subtracts
;
1503 /* Recursively negate the value of TONEGATE, and return the SSA_NAME
1504 representing the negated value. Insertions of any necessary
1505 instructions go before GSI.
1506 This function is recursive in that, if you hand it "a_5" as the
1507 value to negate, and a_5 is defined by "a_5 = b_3 + b_4", it will
1508 transform b_3 + b_4 into a_5 = -b_3 + -b_4. */
1511 negate_value (tree tonegate
, gimple_stmt_iterator
*gsi
)
1513 gimple negatedefstmt
= NULL
;
1514 tree resultofnegate
;
1516 /* If we are trying to negate a name, defined by an add, negate the
1517 add operands instead. */
1518 if (TREE_CODE (tonegate
) == SSA_NAME
)
1519 negatedefstmt
= SSA_NAME_DEF_STMT (tonegate
);
1520 if (TREE_CODE (tonegate
) == SSA_NAME
1521 && is_gimple_assign (negatedefstmt
)
1522 && TREE_CODE (gimple_assign_lhs (negatedefstmt
)) == SSA_NAME
1523 && has_single_use (gimple_assign_lhs (negatedefstmt
))
1524 && gimple_assign_rhs_code (negatedefstmt
) == PLUS_EXPR
)
1526 gimple_stmt_iterator gsi
;
1527 tree rhs1
= gimple_assign_rhs1 (negatedefstmt
);
1528 tree rhs2
= gimple_assign_rhs2 (negatedefstmt
);
1530 gsi
= gsi_for_stmt (negatedefstmt
);
1531 rhs1
= negate_value (rhs1
, &gsi
);
1532 gimple_assign_set_rhs1 (negatedefstmt
, rhs1
);
1534 gsi
= gsi_for_stmt (negatedefstmt
);
1535 rhs2
= negate_value (rhs2
, &gsi
);
1536 gimple_assign_set_rhs2 (negatedefstmt
, rhs2
);
1538 update_stmt (negatedefstmt
);
1539 return gimple_assign_lhs (negatedefstmt
);
1542 tonegate
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (tonegate
), tonegate
);
1543 resultofnegate
= force_gimple_operand_gsi (gsi
, tonegate
, true,
1544 NULL_TREE
, true, GSI_SAME_STMT
);
1545 VEC_safe_push (tree
, heap
, broken_up_subtracts
, resultofnegate
);
1546 return resultofnegate
;
1549 /* Return true if we should break up the subtract in STMT into an add
1550 with negate. This is true when we the subtract operands are really
1551 adds, or the subtract itself is used in an add expression. In
1552 either case, breaking up the subtract into an add with negate
1553 exposes the adds to reassociation. */
1556 should_break_up_subtract (gimple stmt
)
1558 tree lhs
= gimple_assign_lhs (stmt
);
1559 tree binlhs
= gimple_assign_rhs1 (stmt
);
1560 tree binrhs
= gimple_assign_rhs2 (stmt
);
1562 struct loop
*loop
= loop_containing_stmt (stmt
);
1564 if (TREE_CODE (binlhs
) == SSA_NAME
1565 && is_reassociable_op (SSA_NAME_DEF_STMT (binlhs
), PLUS_EXPR
, loop
))
1568 if (TREE_CODE (binrhs
) == SSA_NAME
1569 && is_reassociable_op (SSA_NAME_DEF_STMT (binrhs
), PLUS_EXPR
, loop
))
1572 if (TREE_CODE (lhs
) == SSA_NAME
1573 && (immusestmt
= get_single_immediate_use (lhs
))
1574 && is_gimple_assign (immusestmt
)
1575 && (gimple_assign_rhs_code (immusestmt
) == PLUS_EXPR
1576 || gimple_assign_rhs_code (immusestmt
) == MULT_EXPR
))
1581 /* Transform STMT from A - B into A + -B. */
1584 break_up_subtract (gimple stmt
, gimple_stmt_iterator
*gsip
)
1586 tree rhs1
= gimple_assign_rhs1 (stmt
);
1587 tree rhs2
= gimple_assign_rhs2 (stmt
);
1589 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1591 fprintf (dump_file
, "Breaking up subtract ");
1592 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1595 rhs2
= negate_value (rhs2
, gsip
);
1596 gimple_assign_set_rhs_with_ops (gsip
, PLUS_EXPR
, rhs1
, rhs2
);
1600 /* Recursively linearize a binary expression that is the RHS of STMT.
1601 Place the operands of the expression tree in the vector named OPS. */
1604 linearize_expr_tree (VEC(operand_entry_t
, heap
) **ops
, gimple stmt
,
1605 bool is_associative
, bool set_visited
)
1607 tree binlhs
= gimple_assign_rhs1 (stmt
);
1608 tree binrhs
= gimple_assign_rhs2 (stmt
);
1609 gimple binlhsdef
, binrhsdef
;
1610 bool binlhsisreassoc
= false;
1611 bool binrhsisreassoc
= false;
1612 enum tree_code rhscode
= gimple_assign_rhs_code (stmt
);
1613 struct loop
*loop
= loop_containing_stmt (stmt
);
1616 gimple_set_visited (stmt
, true);
1618 if (TREE_CODE (binlhs
) == SSA_NAME
)
1620 binlhsdef
= SSA_NAME_DEF_STMT (binlhs
);
1621 binlhsisreassoc
= is_reassociable_op (binlhsdef
, rhscode
, loop
);
1624 if (TREE_CODE (binrhs
) == SSA_NAME
)
1626 binrhsdef
= SSA_NAME_DEF_STMT (binrhs
);
1627 binrhsisreassoc
= is_reassociable_op (binrhsdef
, rhscode
, loop
);
1630 /* If the LHS is not reassociable, but the RHS is, we need to swap
1631 them. If neither is reassociable, there is nothing we can do, so
1632 just put them in the ops vector. If the LHS is reassociable,
1633 linearize it. If both are reassociable, then linearize the RHS
1636 if (!binlhsisreassoc
)
1640 /* If this is not a associative operation like division, give up. */
1641 if (!is_associative
)
1643 add_to_ops_vec (ops
, binrhs
);
1647 if (!binrhsisreassoc
)
1649 add_to_ops_vec (ops
, binrhs
);
1650 add_to_ops_vec (ops
, binlhs
);
1654 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1656 fprintf (dump_file
, "swapping operands of ");
1657 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1660 swap_tree_operands (stmt
,
1661 gimple_assign_rhs1_ptr (stmt
),
1662 gimple_assign_rhs2_ptr (stmt
));
1665 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1667 fprintf (dump_file
, " is now ");
1668 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1671 /* We want to make it so the lhs is always the reassociative op,
1677 else if (binrhsisreassoc
)
1679 linearize_expr (stmt
);
1680 binlhs
= gimple_assign_rhs1 (stmt
);
1681 binrhs
= gimple_assign_rhs2 (stmt
);
1684 gcc_assert (TREE_CODE (binrhs
) != SSA_NAME
1685 || !is_reassociable_op (SSA_NAME_DEF_STMT (binrhs
),
1687 linearize_expr_tree (ops
, SSA_NAME_DEF_STMT (binlhs
),
1688 is_associative
, set_visited
);
1689 add_to_ops_vec (ops
, binrhs
);
1692 /* Repropagate the negates back into subtracts, since no other pass
1693 currently does it. */
1696 repropagate_negates (void)
1701 for (i
= 0; VEC_iterate (tree
, broken_up_subtracts
, i
, negate
); i
++)
1703 gimple user
= get_single_immediate_use (negate
);
1705 /* The negate operand can be either operand of a PLUS_EXPR
1706 (it can be the LHS if the RHS is a constant for example).
1708 Force the negate operand to the RHS of the PLUS_EXPR, then
1709 transform the PLUS_EXPR into a MINUS_EXPR. */
1711 && is_gimple_assign (user
)
1712 && gimple_assign_rhs_code (user
) == PLUS_EXPR
)
1714 /* If the negated operand appears on the LHS of the
1715 PLUS_EXPR, exchange the operands of the PLUS_EXPR
1716 to force the negated operand to the RHS of the PLUS_EXPR. */
1717 if (gimple_assign_rhs1 (user
) == negate
)
1719 swap_tree_operands (user
,
1720 gimple_assign_rhs1_ptr (user
),
1721 gimple_assign_rhs2_ptr (user
));
1724 /* Now transform the PLUS_EXPR into a MINUS_EXPR and replace
1725 the RHS of the PLUS_EXPR with the operand of the NEGATE_EXPR. */
1726 if (gimple_assign_rhs2 (user
) == negate
)
1728 tree rhs1
= gimple_assign_rhs1 (user
);
1729 tree rhs2
= get_unary_op (negate
, NEGATE_EXPR
);
1730 gimple_stmt_iterator gsi
= gsi_for_stmt (user
);
1731 gimple_assign_set_rhs_with_ops (&gsi
, MINUS_EXPR
, rhs1
, rhs2
);
1738 /* Break up subtract operations in block BB.
1740 We do this top down because we don't know whether the subtract is
1741 part of a possible chain of reassociation except at the top.
1750 we want to break up k = t - q, but we won't until we've transformed q
1751 = b - r, which won't be broken up until we transform b = c - d.
1753 En passant, clear the GIMPLE visited flag on every statement. */
1756 break_up_subtract_bb (basic_block bb
)
1758 gimple_stmt_iterator gsi
;
1761 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1763 gimple stmt
= gsi_stmt (gsi
);
1764 gimple_set_visited (stmt
, false);
1766 /* Look for simple gimple subtract operations. */
1767 if (is_gimple_assign (stmt
)
1768 && gimple_assign_rhs_code (stmt
) == MINUS_EXPR
)
1770 tree lhs
= gimple_assign_lhs (stmt
);
1771 tree rhs1
= gimple_assign_rhs1 (stmt
);
1772 tree rhs2
= gimple_assign_rhs2 (stmt
);
1774 /* If associative-math we can do reassociation for
1775 non-integral types. Or, we can do reassociation for
1776 non-saturating fixed-point types. */
1777 if ((!INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
1778 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1779 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2
)))
1780 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs
))
1781 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE(rhs1
))
1782 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE(rhs2
))
1783 || !flag_associative_math
)
1784 && (!NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE (lhs
))
1785 || !NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE(rhs1
))
1786 || !NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE(rhs2
))))
1789 /* Check for a subtract used only in an addition. If this
1790 is the case, transform it into add of a negate for better
1791 reassociation. IE transform C = A-B into C = A + -B if C
1792 is only used in an addition. */
1793 if (should_break_up_subtract (stmt
))
1794 break_up_subtract (stmt
, &gsi
);
1797 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
1799 son
= next_dom_son (CDI_DOMINATORS
, son
))
1800 break_up_subtract_bb (son
);
1803 /* Reassociate expressions in basic block BB and its post-dominator as
1807 reassociate_bb (basic_block bb
)
1809 gimple_stmt_iterator gsi
;
1812 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1814 gimple stmt
= gsi_stmt (gsi
);
1816 if (is_gimple_assign (stmt
))
1818 tree lhs
, rhs1
, rhs2
;
1819 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
1821 /* If this is not a gimple binary expression, there is
1822 nothing for us to do with it. */
1823 if (get_gimple_rhs_class (rhs_code
) != GIMPLE_BINARY_RHS
)
1826 /* If this was part of an already processed statement,
1827 we don't need to touch it again. */
1828 if (gimple_visited_p (stmt
))
1830 /* This statement might have become dead because of previous
1832 if (has_zero_uses (gimple_get_lhs (stmt
)))
1834 gsi_remove (&gsi
, true);
1835 release_defs (stmt
);
1836 /* We might end up removing the last stmt above which
1837 places the iterator to the end of the sequence.
1838 Reset it to the last stmt in this case which might
1839 be the end of the sequence as well if we removed
1840 the last statement of the sequence. In which case
1841 we need to bail out. */
1842 if (gsi_end_p (gsi
))
1844 gsi
= gsi_last_bb (bb
);
1845 if (gsi_end_p (gsi
))
1852 lhs
= gimple_assign_lhs (stmt
);
1853 rhs1
= gimple_assign_rhs1 (stmt
);
1854 rhs2
= gimple_assign_rhs2 (stmt
);
1856 /* If associative-math we can do reassociation for
1857 non-integral types. Or, we can do reassociation for
1858 non-saturating fixed-point types. */
1859 if ((!INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
1860 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1861 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs2
)))
1862 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs
))
1863 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE(rhs1
))
1864 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE(rhs2
))
1865 || !flag_associative_math
)
1866 && (!NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE (lhs
))
1867 || !NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE(rhs1
))
1868 || !NON_SAT_FIXED_POINT_TYPE_P (TREE_TYPE(rhs2
))))
1871 if (associative_tree_code (rhs_code
))
1873 VEC(operand_entry_t
, heap
) *ops
= NULL
;
1875 /* There may be no immediate uses left by the time we
1876 get here because we may have eliminated them all. */
1877 if (TREE_CODE (lhs
) == SSA_NAME
&& has_zero_uses (lhs
))
1880 gimple_set_visited (stmt
, true);
1881 linearize_expr_tree (&ops
, stmt
, true, true);
1882 qsort (VEC_address (operand_entry_t
, ops
),
1883 VEC_length (operand_entry_t
, ops
),
1884 sizeof (operand_entry_t
),
1885 sort_by_operand_rank
);
1886 optimize_ops_list (rhs_code
, &ops
);
1887 if (undistribute_ops_list (rhs_code
, &ops
,
1888 loop_containing_stmt (stmt
)))
1890 qsort (VEC_address (operand_entry_t
, ops
),
1891 VEC_length (operand_entry_t
, ops
),
1892 sizeof (operand_entry_t
),
1893 sort_by_operand_rank
);
1894 optimize_ops_list (rhs_code
, &ops
);
1897 if (VEC_length (operand_entry_t
, ops
) == 1)
1899 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1901 fprintf (dump_file
, "Transforming ");
1902 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1905 rhs1
= gimple_assign_rhs1 (stmt
);
1906 gimple_assign_set_rhs_from_tree (&gsi
,
1907 VEC_last (operand_entry_t
,
1910 remove_visited_stmt_chain (rhs1
);
1912 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1914 fprintf (dump_file
, " into ");
1915 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1919 rewrite_expr_tree (stmt
, 0, ops
, false);
1921 VEC_free (operand_entry_t
, heap
, ops
);
1925 for (son
= first_dom_son (CDI_POST_DOMINATORS
, bb
);
1927 son
= next_dom_son (CDI_POST_DOMINATORS
, son
))
1928 reassociate_bb (son
);
1931 void dump_ops_vector (FILE *file
, VEC (operand_entry_t
, heap
) *ops
);
1932 void debug_ops_vector (VEC (operand_entry_t
, heap
) *ops
);
1934 /* Dump the operand entry vector OPS to FILE. */
1937 dump_ops_vector (FILE *file
, VEC (operand_entry_t
, heap
) *ops
)
1942 for (i
= 0; VEC_iterate (operand_entry_t
, ops
, i
, oe
); i
++)
1944 fprintf (file
, "Op %d -> rank: %d, tree: ", i
, oe
->rank
);
1945 print_generic_expr (file
, oe
->op
, 0);
1949 /* Dump the operand entry vector OPS to STDERR. */
1952 debug_ops_vector (VEC (operand_entry_t
, heap
) *ops
)
1954 dump_ops_vector (stderr
, ops
);
1960 break_up_subtract_bb (ENTRY_BLOCK_PTR
);
1961 reassociate_bb (EXIT_BLOCK_PTR
);
1964 /* Initialize the reassociation pass. */
1972 int *bbs
= XNEWVEC (int, last_basic_block
+ 1);
1974 /* Find the loops, so that we can prevent moving calculations in
1976 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
);
1978 memset (&reassociate_stats
, 0, sizeof (reassociate_stats
));
1980 operand_entry_pool
= create_alloc_pool ("operand entry pool",
1981 sizeof (struct operand_entry
), 30);
1983 /* Reverse RPO (Reverse Post Order) will give us something where
1984 deeper loops come later. */
1985 pre_and_rev_post_order_compute (NULL
, bbs
, false);
1986 bb_rank
= XCNEWVEC (long, last_basic_block
+ 1);
1987 operand_rank
= pointer_map_create ();
1989 /* Give each argument a distinct rank. */
1990 for (param
= DECL_ARGUMENTS (current_function_decl
);
1992 param
= TREE_CHAIN (param
))
1994 if (gimple_default_def (cfun
, param
) != NULL
)
1996 tree def
= gimple_default_def (cfun
, param
);
1997 insert_operand_rank (def
, ++rank
);
2001 /* Give the chain decl a distinct rank. */
2002 if (cfun
->static_chain_decl
!= NULL
)
2004 tree def
= gimple_default_def (cfun
, cfun
->static_chain_decl
);
2006 insert_operand_rank (def
, ++rank
);
2009 /* Set up rank for each BB */
2010 for (i
= 0; i
< n_basic_blocks
- NUM_FIXED_BLOCKS
; i
++)
2011 bb_rank
[bbs
[i
]] = ++rank
<< 16;
2014 calculate_dominance_info (CDI_POST_DOMINATORS
);
2015 broken_up_subtracts
= NULL
;
2018 /* Cleanup after the reassociation pass, and print stats if
2024 statistics_counter_event (cfun
, "Linearized",
2025 reassociate_stats
.linearized
);
2026 statistics_counter_event (cfun
, "Constants eliminated",
2027 reassociate_stats
.constants_eliminated
);
2028 statistics_counter_event (cfun
, "Ops eliminated",
2029 reassociate_stats
.ops_eliminated
);
2030 statistics_counter_event (cfun
, "Statements rewritten",
2031 reassociate_stats
.rewritten
);
2033 pointer_map_destroy (operand_rank
);
2034 free_alloc_pool (operand_entry_pool
);
2036 VEC_free (tree
, heap
, broken_up_subtracts
);
2037 free_dominance_info (CDI_POST_DOMINATORS
);
2038 loop_optimizer_finalize ();
2041 /* Gate and execute functions for Reassociation. */
2044 execute_reassoc (void)
2049 repropagate_negates ();
2056 gate_tree_ssa_reassoc (void)
2058 return flag_tree_reassoc
!= 0;
2061 struct gimple_opt_pass pass_reassoc
=
2065 "reassoc", /* name */
2066 gate_tree_ssa_reassoc
, /* gate */
2067 execute_reassoc
, /* execute */
2070 0, /* static_pass_number */
2071 TV_TREE_REASSOC
, /* tv_id */
2072 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
2073 0, /* properties_provided */
2074 0, /* properties_destroyed */
2075 0, /* todo_flags_start */
2076 TODO_dump_func
| TODO_ggc_collect
| TODO_verify_ssa
/* todo_flags_finish */