tree-ssa-phiopt.c (abs_replacement): New function.
[gcc.git] / gcc / tree-ssa-phiopt.c
1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "errors.h"
26 #include "ggc.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "flags.h"
30 #include "tm_p.h"
31 #include "basic-block.h"
32 #include "timevar.h"
33 #include "diagnostic.h"
34 #include "tree-flow.h"
35 #include "tree-pass.h"
36 #include "tree-dump.h"
37 #include "langhooks.h"
38
39 static void tree_ssa_phiopt (void);
40 static bool conditional_replacement (basic_block, tree, tree, tree);
41 static bool value_replacement (basic_block, tree, tree, tree);
42 static bool abs_replacement (basic_block, tree, tree, tree);
43 static void replace_phi_with_stmt (block_stmt_iterator, basic_block,
44 basic_block, tree, tree);
45 static bool candidate_bb_for_phi_optimization (basic_block,
46 basic_block *,
47 basic_block *);
48 static bool empty_block_p (basic_block);
49
50 /* This pass eliminates PHI nodes which can be trivially implemented as
51 an assignment from a conditional expression. ie if we have something
52 like:
53
54 bb0:
55 if (cond) goto bb2; else goto bb1;
56 bb1:
57 bb2:
58 x = PHI (0 (bb1), 1 (bb0)
59
60 We can rewrite that as:
61
62 bb0:
63 bb1:
64 bb2:
65 x = cond;
66
67 bb1 will become unreachable and bb0 and bb2 will almost always
68 be merged into a single block. This occurs often due to gimplification
69 of conditionals.
70
71 Also done is the following optimization:
72
73 bb0:
74 if (a != b) goto bb2; else goto bb1;
75 bb1:
76 bb2:
77 x = PHI (a (bb1), b (bb0))
78
79 We can rewrite that as:
80
81 bb0:
82 bb1:
83 bb2:
84 x = b;
85
86 This can sometimes occur as a result of other optimizations. A
87 similar transformation is done by the ifcvt RTL optimizer.
88
89 This pass also eliminates PHI nodes which are really absolute
90 values. i.e. if we have something like:
91
92 bb0:
93 if (a >= 0) goto bb2; else goto bb1;
94 bb1:
95 x = -a;
96 bb2:
97 x = PHI (x (bb1), a (bb0));
98
99 We can rewrite that as:
100
101 bb0:
102 bb1:
103 bb2:
104 x = ABS_EXPR< a >;
105
106 bb1 will become unreachable and bb0 and bb2 will almost always be merged
107 into a single block. Similar transformations are done by the ifcvt
108 RTL optimizer. */
109
110 static void
111 tree_ssa_phiopt (void)
112 {
113 basic_block bb;
114 bool removed_phis = false;
115
116 /* Search every basic block for PHI nodes we may be able to optimize. */
117 FOR_EACH_BB (bb)
118 {
119 tree arg0, arg1, phi;
120
121 /* We're searching for blocks with one PHI node which has two
122 arguments. */
123 phi = phi_nodes (bb);
124 if (phi && TREE_CHAIN (phi) == NULL
125 && PHI_NUM_ARGS (phi) == 2)
126 {
127 arg0 = PHI_ARG_DEF (phi, 0);
128 arg1 = PHI_ARG_DEF (phi, 1);
129
130 /* Do the replacement of conditional if it can be done. */
131 if (conditional_replacement (bb, phi, arg0, arg1)
132 || value_replacement (bb, phi, arg0, arg1)
133 || abs_replacement (bb, phi, arg0, arg1))
134 {
135 /* We have done the replacement so we need to rebuild the
136 cfg when this pass is complete. */
137 removed_phis = true;
138 }
139 }
140 }
141
142 /* If we removed any PHIs, then we have unreachable blocks and blocks
143 which need to be merged in the CFG. */
144 if (removed_phis)
145 cleanup_tree_cfg ();
146 }
147
148 /* Return TRUE if block BB has no executable statements, otherwise return
149 FALSE. */
150 static bool
151 empty_block_p (basic_block bb)
152 {
153 block_stmt_iterator bsi;
154
155 /* BB must have no executable statements. */
156 bsi = bsi_start (bb);
157 while (!bsi_end_p (bsi)
158 && (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR
159 || IS_EMPTY_STMT (bsi_stmt (bsi))))
160 bsi_next (&bsi);
161
162 if (!bsi_end_p (bsi))
163 return false;
164
165 return true;
166 }
167
168 /* BB is a basic block which has only one PHI node with precisely two
169 arguments.
170
171 Examine both of BB's predecessors to see if one ends with a
172 COND_EXPR and the other is a successor of the COND_EXPR. If so, then
173 we may be able to optimize PHI nodes at the start of BB.
174
175 If so, mark store the block with the COND_EXPR into COND_BLOCK_P
176 and the other block into OTHER_BLOCK_P and return true, otherwise
177 return false. */
178
179 static bool
180 candidate_bb_for_phi_optimization (basic_block bb,
181 basic_block *cond_block_p,
182 basic_block *other_block_p)
183 {
184 tree last0, last1;
185 basic_block cond_block, other_block;
186
187 /* One of the alternatives must come from a block ending with
188 a COND_EXPR. */
189 last0 = last_stmt (bb->pred->src);
190 last1 = last_stmt (bb->pred->pred_next->src);
191 if (last0 && TREE_CODE (last0) == COND_EXPR)
192 {
193 cond_block = bb->pred->src;
194 other_block = bb->pred->pred_next->src;
195 }
196 else if (last1 && TREE_CODE (last1) == COND_EXPR)
197 {
198 other_block = bb->pred->src;
199 cond_block = bb->pred->pred_next->src;
200 }
201 else
202 return false;
203
204 /* COND_BLOCK must have precisely two successors. We indirectly
205 verify that those successors are BB and OTHER_BLOCK. */
206 if (!cond_block->succ
207 || !cond_block->succ->succ_next
208 || cond_block->succ->succ_next->succ_next
209 || (cond_block->succ->flags & EDGE_ABNORMAL) != 0
210 || (cond_block->succ->succ_next->flags & EDGE_ABNORMAL) != 0)
211 return false;
212
213 /* OTHER_BLOCK must have a single predecessor which is COND_BLOCK,
214 OTHER_BLOCK must have a single successor which is BB and
215 OTHER_BLOCK must have no PHI nodes. */
216 if (!other_block->pred
217 || other_block->pred->src != cond_block
218 || other_block->pred->pred_next
219 || !other_block->succ
220 || other_block->succ->dest != bb
221 || other_block->succ->succ_next
222 || phi_nodes (other_block))
223 return false;
224
225 *cond_block_p = cond_block;
226 *other_block_p = other_block;
227 /* Everything looks OK. */
228 return true;
229 }
230
231 /* Replace PHI in block BB with statement NEW. NEW is inserted after
232 BSI. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
233 is known to have two edges, one of which must reach BB). */
234
235 static void
236 replace_phi_with_stmt (block_stmt_iterator bsi, basic_block bb,
237 basic_block cond_block, tree phi, tree new)
238 {
239 /* Insert our new statement at the head of our block. */
240 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
241
242 /* Register our new statement as the defining statement for
243 the result. */
244 SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = new;
245
246 /* Remove the now useless PHI node.
247
248 We do not want to use remove_phi_node since that releases the
249 SSA_NAME as well and the SSA_NAME is still being used. */
250 release_phi_node (phi);
251 bb_ann (bb)->phi_nodes = NULL;
252
253 /* Disconnect the edge leading into the empty block. That will
254 make the empty block unreachable and it will be removed later. */
255 if (cond_block->succ->dest == bb)
256 {
257 cond_block->succ->flags |= EDGE_FALLTHRU;
258 cond_block->succ->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
259 ssa_remove_edge (cond_block->succ->succ_next);
260 }
261 else
262 {
263 cond_block->succ->succ_next->flags |= EDGE_FALLTHRU;
264 cond_block->succ->succ_next->flags
265 &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
266 ssa_remove_edge (cond_block->succ);
267 }
268
269 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
270 bsi = bsi_last (cond_block);
271 bsi_remove (&bsi);
272
273 if (dump_file && (dump_flags & TDF_DETAILS))
274 fprintf (dump_file,
275 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
276 cond_block->index,
277 bb->index);
278 }
279
280 /* The function conditional_replacement does the main work of doing the
281 conditional replacement. Return true if the replacement is done.
282 Otherwise return false.
283 BB is the basic block where the replacement is going to be done on. ARG0
284 is argument 0 from PHI. Likewise for ARG1. */
285
286 static bool
287 conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
288 {
289 tree result;
290 tree old_result = NULL;
291 basic_block other_block = NULL;
292 basic_block cond_block = NULL;
293 tree new, cond;
294 block_stmt_iterator bsi;
295 edge true_edge, false_edge;
296 tree new_var = NULL;
297
298 /* The PHI arguments have the constants 0 and 1, then convert
299 it to the conditional. */
300 if ((integer_zerop (arg0) && integer_onep (arg1))
301 || (integer_zerop (arg1) && integer_onep (arg0)))
302 ;
303 else
304 return false;
305
306 if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block)
307 || !empty_block_p (other_block))
308 return false;
309
310 /* If the condition is not a naked SSA_NAME and its type does not
311 match the type of the result, then we have to create a new
312 variable to optimize this case as it would likely create
313 non-gimple code when the condition was converted to the
314 result's type. */
315 cond = COND_EXPR_COND (last_stmt (cond_block));
316 result = PHI_RESULT (phi);
317 if (TREE_CODE (cond) != SSA_NAME
318 && !lang_hooks.types_compatible_p (TREE_TYPE (cond), TREE_TYPE (result)))
319 {
320 new_var = make_rename_temp (TREE_TYPE (cond), NULL);
321 old_result = cond;
322 cond = new_var;
323 }
324
325 /* If the condition was a naked SSA_NAME and the type is not the
326 same as the type of the result, then convert the type of the
327 condition. */
328 if (!lang_hooks.types_compatible_p (TREE_TYPE (cond), TREE_TYPE (result)))
329 cond = fold_convert (TREE_TYPE (result), cond);
330
331 /* We need to know which is the true edge and which is the false
332 edge so that we know when to invert the condition below. */
333 extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge);
334
335 /* Insert our new statement at the head of our block. */
336 bsi = bsi_start (bb);
337
338 if (old_result)
339 {
340 tree new1;
341 if (TREE_CODE_CLASS (TREE_CODE (old_result)) != '<')
342 return false;
343
344 new1 = build (TREE_CODE (old_result), TREE_TYPE (result),
345 TREE_OPERAND (old_result, 0),
346 TREE_OPERAND (old_result, 1));
347
348 new1 = build (MODIFY_EXPR, TREE_TYPE (result), new_var, new1);
349 bsi_insert_after (&bsi, new1, BSI_NEW_STMT);
350 }
351
352 /* At this point we know we have a COND_EXPR with two successors.
353 One successor is BB, the other successor is an empty block which
354 falls through into BB.
355
356 There is a single PHI node at the join point (BB) and its arguments
357 are constants (0, 1).
358
359 So, given the condition COND, and the two PHI arguments, we can
360 rewrite this PHI into non-branching code:
361
362 dest = (COND) or dest = COND'
363
364 We use the condition as-is if the argument associated with the
365 true edge has the value one or the argument associated with the
366 false edge as the value zero. Note that those conditions are not
367 the same since only one of the outgoing edges from the COND_EXPR
368 will directly reach BB and thus be associated with an argument. */
369 if ((PHI_ARG_EDGE (phi, 0) == true_edge && integer_onep (arg0))
370 || (PHI_ARG_EDGE (phi, 0) == false_edge && integer_zerop (arg0))
371 || (PHI_ARG_EDGE (phi, 1) == true_edge && integer_onep (arg1))
372 || (PHI_ARG_EDGE (phi, 1) == false_edge && integer_zerop (arg1)))
373 {
374 new = build (MODIFY_EXPR, TREE_TYPE (PHI_RESULT (phi)),
375 PHI_RESULT (phi), cond);
376 }
377 else
378 {
379 tree cond1 = invert_truthvalue (cond);
380
381 cond = cond1;
382 /* If what we get back is a conditional expression, there is no
383 way that it can be gimple. */
384 if (TREE_CODE (cond) == COND_EXPR)
385 return false;
386
387 /* If what we get back is not gimple try to create it as gimple by
388 using a temporary variable. */
389 if (is_gimple_cast (cond)
390 && !is_gimple_val (TREE_OPERAND (cond, 0)))
391 {
392 tree temp = TREE_OPERAND (cond, 0);
393 tree new_var_1 = make_rename_temp (TREE_TYPE (temp), NULL);
394 new = build (MODIFY_EXPR, TREE_TYPE (new_var_1), new_var_1, temp);
395 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
396 cond = fold_convert (TREE_TYPE (result), new_var_1);
397 }
398
399 if (TREE_CODE (cond) == TRUTH_NOT_EXPR
400 && !is_gimple_val (TREE_OPERAND (cond, 0)))
401 return false;
402
403 new = build (MODIFY_EXPR, TREE_TYPE (PHI_RESULT (phi)),
404 PHI_RESULT (phi), cond);
405 }
406
407 replace_phi_with_stmt (bsi, bb, cond_block, phi, new);
408
409 /* Note that we optimized this PHI. */
410 return true;
411 }
412
413 /* The function value_replacement does the main work of doing the value
414 replacement. Return true if the replacement is done. Otherwise return
415 false.
416 BB is the basic block where the replacement is going to be done on. ARG0
417 is argument 0 from the PHI. Likewise for ARG1. */
418
419 static bool
420 value_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
421 {
422 tree result;
423 basic_block other_block = NULL;
424 basic_block cond_block = NULL;
425 tree new, cond;
426 edge true_edge, false_edge;
427
428 /* If the type says honor signed zeros we cannot do this
429 optimization. */
430 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
431 return false;
432
433 if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block)
434 || !empty_block_p (other_block))
435 return false;
436
437 cond = COND_EXPR_COND (last_stmt (cond_block));
438 result = PHI_RESULT (phi);
439
440 /* This transformation is only valid for equality comparisons. */
441 if (TREE_CODE (cond) != NE_EXPR && TREE_CODE (cond) != EQ_EXPR)
442 return false;
443
444 /* We need to know which is the true edge and which is the false
445 edge so that we know if have abs or negative abs. */
446 extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge);
447
448 /* At this point we know we have a COND_EXPR with two successors.
449 One successor is BB, the other successor is an empty block which
450 falls through into BB.
451
452 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
453
454 There is a single PHI node at the join point (BB) with two arguments.
455
456 We now need to verify that the two arguments in the PHI node match
457 the two arguments to the equality comparison. */
458
459 if ((operand_equal_p (arg0, TREE_OPERAND (cond, 0), 0)
460 && operand_equal_p (arg1, TREE_OPERAND (cond, 1), 0))
461 || (operand_equal_p (arg1, TREE_OPERAND (cond, 0), 0)
462 && operand_equal_p (arg0, TREE_OPERAND (cond, 1), 0)))
463 {
464 edge e;
465 tree arg;
466
467 e = (TREE_CODE (cond) == NE_EXPR ? true_edge : false_edge);
468 if (PHI_ARG_EDGE (phi, 0) == e)
469 arg = arg0;
470 else
471 arg = arg1;
472
473 /* Build the new assignment. */
474 new = build (MODIFY_EXPR, TREE_TYPE (result), result, arg);
475
476 replace_phi_with_stmt (bsi_start (bb), bb, cond_block, phi, new);
477
478 /* Note that we optimized this PHI. */
479 return true;
480 }
481 return false;
482 }
483
484 /* The function absolute_replacement does the main work of doing the absolute
485 replacement. Return true if the replacement is done. Otherwise return
486 false.
487 bb is the basic block where the replacement is going to be done on. arg0
488 is argument 0 from the phi. Likewise for arg1. */
489 static bool
490 abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
491 {
492 tree result;
493 basic_block other_block = NULL;
494 basic_block cond_block = NULL;
495 tree new, cond;
496 block_stmt_iterator bsi;
497 edge true_edge, false_edge;
498 tree assign = NULL;
499 edge e;
500 tree rhs = NULL, lhs = NULL;
501 bool negate;
502 enum tree_code cond_code;
503
504 /* If the type says honor signed zeros we cannot do this
505 optimization. */
506 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
507 return false;
508
509 if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block))
510 return false;
511
512 /* OTHER_BLOCK must have only one executable statement which must have the
513 form arg0 = -arg1 or arg1 = -arg0. */
514 bsi = bsi_start (other_block);
515 while (!bsi_end_p (bsi))
516 {
517 tree stmt = bsi_stmt (bsi);
518
519 /* Empty statements and labels are uninteresting. */
520 if (TREE_CODE (stmt) == LABEL_EXPR
521 || IS_EMPTY_STMT (stmt))
522 {
523 bsi_next (&bsi);
524 continue;
525 }
526
527 /* If we found the assignment, but it was not the only executable
528 statement in OTHER_BLOCK, then we can not optimize. */
529 if (assign)
530 return false;
531
532 /* If we got here, then we have found the first executable statement
533 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
534 arg1 = -arg0, then we can not optimize. */
535 if (TREE_CODE (stmt) == MODIFY_EXPR)
536 {
537 lhs = TREE_OPERAND (stmt, 0);
538 rhs = TREE_OPERAND (stmt, 1);
539
540 if (TREE_CODE (rhs) == NEGATE_EXPR)
541 {
542 rhs = TREE_OPERAND (rhs, 0);
543
544 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
545 if ((lhs == arg0 && rhs == arg1)
546 || (lhs == arg1 && rhs == arg0))
547 {
548 assign = stmt;
549 bsi_next (&bsi);
550 }
551 else
552 return false;
553 }
554 else
555 return false;
556 }
557 else
558 return false;
559 }
560
561 /* If we did not find the proper negation assignment, then we can not
562 optimize. */
563 if (assign == NULL)
564 return false;
565
566 cond = COND_EXPR_COND (last_stmt (cond_block));
567 result = PHI_RESULT (phi);
568
569 /* Only relationals comparing arg[01] against zero are interesting. */
570 cond_code = TREE_CODE (cond);
571 if (cond_code != GT_EXPR && cond_code != GE_EXPR
572 && cond_code != LT_EXPR && cond_code != LE_EXPR)
573 return false;
574
575 /* Make sure the conditional is arg[01] OP y. */
576 if (TREE_OPERAND (cond, 0) != rhs)
577 return false;
578
579 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (cond, 1)))
580 ? real_zerop (TREE_OPERAND (cond, 1))
581 : integer_zerop (TREE_OPERAND (cond, 1)))
582 ;
583 else
584 return false;
585
586 /* We need to know which is the true edge and which is the false
587 edge so that we know if have abs or negative abs. */
588 extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge);
589
590 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
591 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
592 the false edge goes to OTHER_BLOCK. */
593 if (cond_code == GT_EXPR || cond_code == GE_EXPR)
594 e = true_edge;
595 else
596 e = false_edge;
597
598 if (e->dest == other_block)
599 negate = true;
600 else
601 negate = false;
602
603 if (negate)
604 lhs = make_rename_temp (TREE_TYPE (result), NULL);
605 else
606 lhs = result;
607
608 /* Build the modify expression with abs expression. */
609 new = build (MODIFY_EXPR, TREE_TYPE (lhs),
610 lhs, build1 (ABS_EXPR, TREE_TYPE (lhs), rhs));
611
612 replace_phi_with_stmt (bsi_start (bb), bb, cond_block, phi, new);
613
614 if (negate)
615 {
616
617 /* Get the right BSI. We want to insert after the recently
618 added ABS_EXPR statement (which we know is the first statement
619 in the block. */
620 bsi = bsi_start (bb);
621 bsi_next (&bsi);
622 new = build (MODIFY_EXPR, TREE_TYPE (result),
623 result, build1 (NEGATE_EXPR, TREE_TYPE (lhs), lhs));
624
625 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
626
627 /* Register the new statement as defining the temporary -- this is
628 normally done by replace_phi_with_stmt, but the link will be wrong
629 if we had to negate the resulting value. */
630 SSA_NAME_DEF_STMT (result) = new;
631 }
632
633 /* Note that we optimized this PHI. */
634 return true;
635 }
636
637
638 /* Always do these optimizations if we have SSA
639 trees to work on. */
640 static bool
641 gate_phiopt (void)
642 {
643 return 1;
644 }
645
646 struct tree_opt_pass pass_phiopt =
647 {
648 "phiopt", /* name */
649 gate_phiopt, /* gate */
650 tree_ssa_phiopt, /* execute */
651 NULL, /* sub */
652 NULL, /* next */
653 0, /* static_pass_number */
654 TV_TREE_PHIOPT, /* tv_id */
655 PROP_cfg | PROP_ssa, /* properties_required */
656 0, /* properties_provided */
657 0, /* properties_destroyed */
658 0, /* todo_flags_start */
659 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */
660 | TODO_verify_ssa | TODO_rename_vars
661 | TODO_verify_flow
662 };
663
664