re PR c++/54538 (Getting assembler messages when compiling)
[gcc.git] / gcc / tree-ssa-forwprop.c
1 /* Forward propagation of expressions for single use variables.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
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)
10 any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "gimple.h"
34 #include "expr.h"
35 #include "cfgloop.h"
36
37 /* This pass propagates the RHS of assignment statements into use
38 sites of the LHS of the assignment. It's basically a specialized
39 form of tree combination. It is hoped all of this can disappear
40 when we have a generalized tree combiner.
41
42 One class of common cases we handle is forward propagating a single use
43 variable into a COND_EXPR.
44
45 bb0:
46 x = a COND b;
47 if (x) goto ... else goto ...
48
49 Will be transformed into:
50
51 bb0:
52 if (a COND b) goto ... else goto ...
53
54 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
55
56 Or (assuming c1 and c2 are constants):
57
58 bb0:
59 x = a + c1;
60 if (x EQ/NEQ c2) goto ... else goto ...
61
62 Will be transformed into:
63
64 bb0:
65 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
66
67 Similarly for x = a - c1.
68
69 Or
70
71 bb0:
72 x = !a
73 if (x) goto ... else goto ...
74
75 Will be transformed into:
76
77 bb0:
78 if (a == 0) goto ... else goto ...
79
80 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
81 For these cases, we propagate A into all, possibly more than one,
82 COND_EXPRs that use X.
83
84 Or
85
86 bb0:
87 x = (typecast) a
88 if (x) goto ... else goto ...
89
90 Will be transformed into:
91
92 bb0:
93 if (a != 0) goto ... else goto ...
94
95 (Assuming a is an integral type and x is a boolean or x is an
96 integral and a is a boolean.)
97
98 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
99 For these cases, we propagate A into all, possibly more than one,
100 COND_EXPRs that use X.
101
102 In addition to eliminating the variable and the statement which assigns
103 a value to the variable, we may be able to later thread the jump without
104 adding insane complexity in the dominator optimizer.
105
106 Also note these transformations can cascade. We handle this by having
107 a worklist of COND_EXPR statements to examine. As we make a change to
108 a statement, we put it back on the worklist to examine on the next
109 iteration of the main loop.
110
111 A second class of propagation opportunities arises for ADDR_EXPR
112 nodes.
113
114 ptr = &x->y->z;
115 res = *ptr;
116
117 Will get turned into
118
119 res = x->y->z;
120
121 Or
122 ptr = (type1*)&type2var;
123 res = *ptr
124
125 Will get turned into (if type1 and type2 are the same size
126 and neither have volatile on them):
127 res = VIEW_CONVERT_EXPR<type1>(type2var)
128
129 Or
130
131 ptr = &x[0];
132 ptr2 = ptr + <constant>;
133
134 Will get turned into
135
136 ptr2 = &x[constant/elementsize];
137
138 Or
139
140 ptr = &x[0];
141 offset = index * element_size;
142 offset_p = (pointer) offset;
143 ptr2 = ptr + offset_p
144
145 Will get turned into:
146
147 ptr2 = &x[index];
148
149 Or
150 ssa = (int) decl
151 res = ssa & 1
152
153 Provided that decl has known alignment >= 2, will get turned into
154
155 res = 0
156
157 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
158 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
159 {NOT_EXPR,NEG_EXPR}.
160
161 This will (of course) be extended as other needs arise. */
162
163 static bool forward_propagate_addr_expr (tree name, tree rhs);
164
165 /* Set to true if we delete dead edges during the optimization. */
166 static bool cfg_changed;
167
168 static tree rhs_to_tree (tree type, gimple stmt);
169
170 /* Get the next statement we can propagate NAME's value into skipping
171 trivial copies. Returns the statement that is suitable as a
172 propagation destination or NULL_TREE if there is no such one.
173 This only returns destinations in a single-use chain. FINAL_NAME_P
174 if non-NULL is written to the ssa name that represents the use. */
175
176 static gimple
177 get_prop_dest_stmt (tree name, tree *final_name_p)
178 {
179 use_operand_p use;
180 gimple use_stmt;
181
182 do {
183 /* If name has multiple uses, bail out. */
184 if (!single_imm_use (name, &use, &use_stmt))
185 return NULL;
186
187 /* If this is not a trivial copy, we found it. */
188 if (!gimple_assign_ssa_name_copy_p (use_stmt)
189 || gimple_assign_rhs1 (use_stmt) != name)
190 break;
191
192 /* Continue searching uses of the copy destination. */
193 name = gimple_assign_lhs (use_stmt);
194 } while (1);
195
196 if (final_name_p)
197 *final_name_p = name;
198
199 return use_stmt;
200 }
201
202 /* Get the statement we can propagate from into NAME skipping
203 trivial copies. Returns the statement which defines the
204 propagation source or NULL_TREE if there is no such one.
205 If SINGLE_USE_ONLY is set considers only sources which have
206 a single use chain up to NAME. If SINGLE_USE_P is non-null,
207 it is set to whether the chain to NAME is a single use chain
208 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
209
210 static gimple
211 get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
212 {
213 bool single_use = true;
214
215 do {
216 gimple def_stmt = SSA_NAME_DEF_STMT (name);
217
218 if (!has_single_use (name))
219 {
220 single_use = false;
221 if (single_use_only)
222 return NULL;
223 }
224
225 /* If name is defined by a PHI node or is the default def, bail out. */
226 if (!is_gimple_assign (def_stmt))
227 return NULL;
228
229 /* If def_stmt is not a simple copy, we possibly found it. */
230 if (!gimple_assign_ssa_name_copy_p (def_stmt))
231 {
232 tree rhs;
233
234 if (!single_use_only && single_use_p)
235 *single_use_p = single_use;
236
237 /* We can look through pointer conversions in the search
238 for a useful stmt for the comparison folding. */
239 rhs = gimple_assign_rhs1 (def_stmt);
240 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
241 && TREE_CODE (rhs) == SSA_NAME
242 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (def_stmt)))
243 && POINTER_TYPE_P (TREE_TYPE (rhs)))
244 name = rhs;
245 else
246 return def_stmt;
247 }
248 else
249 {
250 /* Continue searching the def of the copy source name. */
251 name = gimple_assign_rhs1 (def_stmt);
252 }
253 } while (1);
254 }
255
256 /* Checks if the destination ssa name in DEF_STMT can be used as
257 propagation source. Returns true if so, otherwise false. */
258
259 static bool
260 can_propagate_from (gimple def_stmt)
261 {
262 gcc_assert (is_gimple_assign (def_stmt));
263
264 /* If the rhs has side-effects we cannot propagate from it. */
265 if (gimple_has_volatile_ops (def_stmt))
266 return false;
267
268 /* If the rhs is a load we cannot propagate from it. */
269 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
270 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
271 return false;
272
273 /* Constants can be always propagated. */
274 if (gimple_assign_single_p (def_stmt)
275 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
276 return true;
277
278 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
279 if (stmt_references_abnormal_ssa_name (def_stmt))
280 return false;
281
282 /* If the definition is a conversion of a pointer to a function type,
283 then we can not apply optimizations as some targets require
284 function pointers to be canonicalized and in this case this
285 optimization could eliminate a necessary canonicalization. */
286 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
287 {
288 tree rhs = gimple_assign_rhs1 (def_stmt);
289 if (POINTER_TYPE_P (TREE_TYPE (rhs))
290 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs))) == FUNCTION_TYPE)
291 return false;
292 }
293
294 return true;
295 }
296
297 /* Remove a chain of dead statements starting at the definition of
298 NAME. The chain is linked via the first operand of the defining statements.
299 If NAME was replaced in its only use then this function can be used
300 to clean up dead stmts. The function handles already released SSA
301 names gracefully.
302 Returns true if cleanup-cfg has to run. */
303
304 static bool
305 remove_prop_source_from_use (tree name)
306 {
307 gimple_stmt_iterator gsi;
308 gimple stmt;
309 bool cfg_changed = false;
310
311 do {
312 basic_block bb;
313
314 if (SSA_NAME_IN_FREE_LIST (name)
315 || SSA_NAME_IS_DEFAULT_DEF (name)
316 || !has_zero_uses (name))
317 return cfg_changed;
318
319 stmt = SSA_NAME_DEF_STMT (name);
320 if (gimple_code (stmt) == GIMPLE_PHI
321 || gimple_has_side_effects (stmt))
322 return cfg_changed;
323
324 bb = gimple_bb (stmt);
325 gsi = gsi_for_stmt (stmt);
326 unlink_stmt_vdef (stmt);
327 if (gsi_remove (&gsi, true))
328 cfg_changed |= gimple_purge_dead_eh_edges (bb);
329 release_defs (stmt);
330
331 name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
332 } while (name && TREE_CODE (name) == SSA_NAME);
333
334 return cfg_changed;
335 }
336
337 /* Return the rhs of a gimple_assign STMT in a form of a single tree,
338 converted to type TYPE.
339
340 This should disappear, but is needed so we can combine expressions and use
341 the fold() interfaces. Long term, we need to develop folding and combine
342 routines that deal with gimple exclusively . */
343
344 static tree
345 rhs_to_tree (tree type, gimple stmt)
346 {
347 location_t loc = gimple_location (stmt);
348 enum tree_code code = gimple_assign_rhs_code (stmt);
349 if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
350 return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
351 gimple_assign_rhs2 (stmt),
352 gimple_assign_rhs3 (stmt));
353 else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
354 return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
355 gimple_assign_rhs2 (stmt));
356 else if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
357 return build1 (code, type, gimple_assign_rhs1 (stmt));
358 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
359 return gimple_assign_rhs1 (stmt);
360 else
361 gcc_unreachable ();
362 }
363
364 /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
365 the folded result in a form suitable for COND_EXPR_COND or
366 NULL_TREE, if there is no suitable simplified form. If
367 INVARIANT_ONLY is true only gimple_min_invariant results are
368 considered simplified. */
369
370 static tree
371 combine_cond_expr_cond (gimple stmt, enum tree_code code, tree type,
372 tree op0, tree op1, bool invariant_only)
373 {
374 tree t;
375
376 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
377
378 fold_defer_overflow_warnings ();
379 t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
380 if (!t)
381 {
382 fold_undefer_overflow_warnings (false, NULL, 0);
383 return NULL_TREE;
384 }
385
386 /* Require that we got a boolean type out if we put one in. */
387 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
388
389 /* Canonicalize the combined condition for use in a COND_EXPR. */
390 t = canonicalize_cond_expr_cond (t);
391
392 /* Bail out if we required an invariant but didn't get one. */
393 if (!t || (invariant_only && !is_gimple_min_invariant (t)))
394 {
395 fold_undefer_overflow_warnings (false, NULL, 0);
396 return NULL_TREE;
397 }
398
399 fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt), stmt, 0);
400
401 return t;
402 }
403
404 /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
405 of its operand. Return a new comparison tree or NULL_TREE if there
406 were no simplifying combines. */
407
408 static tree
409 forward_propagate_into_comparison_1 (gimple stmt,
410 enum tree_code code, tree type,
411 tree op0, tree op1)
412 {
413 tree tmp = NULL_TREE;
414 tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
415 bool single_use0_p = false, single_use1_p = false;
416
417 /* For comparisons use the first operand, that is likely to
418 simplify comparisons against constants. */
419 if (TREE_CODE (op0) == SSA_NAME)
420 {
421 gimple def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
422 if (def_stmt && can_propagate_from (def_stmt))
423 {
424 rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
425 tmp = combine_cond_expr_cond (stmt, code, type,
426 rhs0, op1, !single_use0_p);
427 if (tmp)
428 return tmp;
429 }
430 }
431
432 /* If that wasn't successful, try the second operand. */
433 if (TREE_CODE (op1) == SSA_NAME)
434 {
435 gimple def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
436 if (def_stmt && can_propagate_from (def_stmt))
437 {
438 rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
439 tmp = combine_cond_expr_cond (stmt, code, type,
440 op0, rhs1, !single_use1_p);
441 if (tmp)
442 return tmp;
443 }
444 }
445
446 /* If that wasn't successful either, try both operands. */
447 if (rhs0 != NULL_TREE
448 && rhs1 != NULL_TREE)
449 tmp = combine_cond_expr_cond (stmt, code, type,
450 rhs0, rhs1,
451 !(single_use0_p && single_use1_p));
452
453 return tmp;
454 }
455
456 /* Propagate from the ssa name definition statements of the assignment
457 from a comparison at *GSI into the conditional if that simplifies it.
458 Returns 1 if the stmt was modified and 2 if the CFG needs cleanup,
459 otherwise returns 0. */
460
461 static int
462 forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
463 {
464 gimple stmt = gsi_stmt (*gsi);
465 tree tmp;
466 bool cfg_changed = false;
467 tree type = TREE_TYPE (gimple_assign_lhs (stmt));
468 tree rhs1 = gimple_assign_rhs1 (stmt);
469 tree rhs2 = gimple_assign_rhs2 (stmt);
470
471 /* Combine the comparison with defining statements. */
472 tmp = forward_propagate_into_comparison_1 (stmt,
473 gimple_assign_rhs_code (stmt),
474 type, rhs1, rhs2);
475 if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
476 {
477 gimple_assign_set_rhs_from_tree (gsi, tmp);
478 fold_stmt (gsi);
479 update_stmt (gsi_stmt (*gsi));
480
481 if (TREE_CODE (rhs1) == SSA_NAME)
482 cfg_changed |= remove_prop_source_from_use (rhs1);
483 if (TREE_CODE (rhs2) == SSA_NAME)
484 cfg_changed |= remove_prop_source_from_use (rhs2);
485 return cfg_changed ? 2 : 1;
486 }
487
488 return 0;
489 }
490
491 /* Propagate from the ssa name definition statements of COND_EXPR
492 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
493 Returns zero if no statement was changed, one if there were
494 changes and two if cfg_cleanup needs to run.
495
496 This must be kept in sync with forward_propagate_into_cond. */
497
498 static int
499 forward_propagate_into_gimple_cond (gimple stmt)
500 {
501 tree tmp;
502 enum tree_code code = gimple_cond_code (stmt);
503 bool cfg_changed = false;
504 tree rhs1 = gimple_cond_lhs (stmt);
505 tree rhs2 = gimple_cond_rhs (stmt);
506
507 /* We can do tree combining on SSA_NAME and comparison expressions. */
508 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
509 return 0;
510
511 tmp = forward_propagate_into_comparison_1 (stmt, code,
512 boolean_type_node,
513 rhs1, rhs2);
514 if (tmp)
515 {
516 if (dump_file && tmp)
517 {
518 fprintf (dump_file, " Replaced '");
519 print_gimple_expr (dump_file, stmt, 0, 0);
520 fprintf (dump_file, "' with '");
521 print_generic_expr (dump_file, tmp, 0);
522 fprintf (dump_file, "'\n");
523 }
524
525 gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
526 update_stmt (stmt);
527
528 if (TREE_CODE (rhs1) == SSA_NAME)
529 cfg_changed |= remove_prop_source_from_use (rhs1);
530 if (TREE_CODE (rhs2) == SSA_NAME)
531 cfg_changed |= remove_prop_source_from_use (rhs2);
532 return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1;
533 }
534
535 /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */
536 if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE
537 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
538 && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
539 && ((code == EQ_EXPR
540 && integer_zerop (rhs2))
541 || (code == NE_EXPR
542 && integer_onep (rhs2))))
543 {
544 basic_block bb = gimple_bb (stmt);
545 gimple_cond_set_code (stmt, NE_EXPR);
546 gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1)));
547 EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
548 EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
549 return 1;
550 }
551
552 return 0;
553 }
554
555
556 /* Propagate from the ssa name definition statements of COND_EXPR
557 in the rhs of statement STMT into the conditional if that simplifies it.
558 Returns true zero if the stmt was changed. */
559
560 static bool
561 forward_propagate_into_cond (gimple_stmt_iterator *gsi_p)
562 {
563 gimple stmt = gsi_stmt (*gsi_p);
564 tree tmp = NULL_TREE;
565 tree cond = gimple_assign_rhs1 (stmt);
566 bool swap = false;
567
568 /* We can do tree combining on SSA_NAME and comparison expressions. */
569 if (COMPARISON_CLASS_P (cond))
570 tmp = forward_propagate_into_comparison_1 (stmt, TREE_CODE (cond),
571 boolean_type_node,
572 TREE_OPERAND (cond, 0),
573 TREE_OPERAND (cond, 1));
574 else if (TREE_CODE (cond) == SSA_NAME)
575 {
576 enum tree_code code;
577 tree name = cond;
578 gimple def_stmt = get_prop_source_stmt (name, true, NULL);
579 if (!def_stmt || !can_propagate_from (def_stmt))
580 return 0;
581
582 code = gimple_assign_rhs_code (def_stmt);
583 if (TREE_CODE_CLASS (code) == tcc_comparison)
584 tmp = fold_build2_loc (gimple_location (def_stmt),
585 code,
586 boolean_type_node,
587 gimple_assign_rhs1 (def_stmt),
588 gimple_assign_rhs2 (def_stmt));
589 else if ((code == BIT_NOT_EXPR
590 && TYPE_PRECISION (TREE_TYPE (cond)) == 1)
591 || (code == BIT_XOR_EXPR
592 && integer_onep (gimple_assign_rhs2 (def_stmt))))
593 {
594 tmp = gimple_assign_rhs1 (def_stmt);
595 swap = true;
596 }
597 }
598
599 if (tmp
600 && is_gimple_condexpr (tmp))
601 {
602 if (dump_file && tmp)
603 {
604 fprintf (dump_file, " Replaced '");
605 print_generic_expr (dump_file, cond, 0);
606 fprintf (dump_file, "' with '");
607 print_generic_expr (dump_file, tmp, 0);
608 fprintf (dump_file, "'\n");
609 }
610
611 if (integer_onep (tmp))
612 gimple_assign_set_rhs_from_tree (gsi_p, gimple_assign_rhs2 (stmt));
613 else if (integer_zerop (tmp))
614 gimple_assign_set_rhs_from_tree (gsi_p, gimple_assign_rhs3 (stmt));
615 else
616 {
617 gimple_assign_set_rhs1 (stmt, unshare_expr (tmp));
618 if (swap)
619 {
620 tree t = gimple_assign_rhs2 (stmt);
621 gimple_assign_set_rhs2 (stmt, gimple_assign_rhs3 (stmt));
622 gimple_assign_set_rhs3 (stmt, t);
623 }
624 }
625 stmt = gsi_stmt (*gsi_p);
626 update_stmt (stmt);
627
628 return true;
629 }
630
631 return 0;
632 }
633
634 /* Propagate from the ssa name definition statements of COND_EXPR
635 values in the rhs of statement STMT into the conditional arms
636 if that simplifies it.
637 Returns true if the stmt was changed. */
638
639 static bool
640 combine_cond_exprs (gimple_stmt_iterator *gsi_p)
641 {
642 gimple stmt = gsi_stmt (*gsi_p);
643 tree cond, val1, val2;
644 bool changed = false;
645
646 cond = gimple_assign_rhs1 (stmt);
647 val1 = gimple_assign_rhs2 (stmt);
648 if (TREE_CODE (val1) == SSA_NAME)
649 {
650 gimple def_stmt = SSA_NAME_DEF_STMT (val1);
651 if (is_gimple_assign (def_stmt)
652 && gimple_assign_rhs_code (def_stmt) == gimple_assign_rhs_code (stmt)
653 && operand_equal_p (gimple_assign_rhs1 (def_stmt), cond, 0))
654 {
655 val1 = unshare_expr (gimple_assign_rhs2 (def_stmt));
656 gimple_assign_set_rhs2 (stmt, val1);
657 changed = true;
658 }
659 }
660 val2 = gimple_assign_rhs3 (stmt);
661 if (TREE_CODE (val2) == SSA_NAME)
662 {
663 gimple def_stmt = SSA_NAME_DEF_STMT (val2);
664 if (is_gimple_assign (def_stmt)
665 && gimple_assign_rhs_code (def_stmt) == gimple_assign_rhs_code (stmt)
666 && operand_equal_p (gimple_assign_rhs1 (def_stmt), cond, 0))
667 {
668 val2 = unshare_expr (gimple_assign_rhs3 (def_stmt));
669 gimple_assign_set_rhs3 (stmt, val2);
670 changed = true;
671 }
672 }
673 if (operand_equal_p (val1, val2, 0))
674 {
675 gimple_assign_set_rhs_from_tree (gsi_p, val1);
676 stmt = gsi_stmt (*gsi_p);
677 changed = true;
678 }
679
680 if (changed)
681 update_stmt (stmt);
682
683 return changed;
684 }
685
686 /* We've just substituted an ADDR_EXPR into stmt. Update all the
687 relevant data structures to match. */
688
689 static void
690 tidy_after_forward_propagate_addr (gimple stmt)
691 {
692 /* We may have turned a trapping insn into a non-trapping insn. */
693 if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
694 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
695 cfg_changed = true;
696
697 if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
698 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
699 }
700
701 /* NAME is a SSA_NAME representing DEF_RHS which is of the form
702 ADDR_EXPR <whatever>.
703
704 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
705 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
706 node or for recovery of array indexing from pointer arithmetic.
707
708 Return true if the propagation was successful (the propagation can
709 be not totally successful, yet things may have been changed). */
710
711 static bool
712 forward_propagate_addr_expr_1 (tree name, tree def_rhs,
713 gimple_stmt_iterator *use_stmt_gsi,
714 bool single_use_p)
715 {
716 tree lhs, rhs, rhs2, array_ref;
717 gimple use_stmt = gsi_stmt (*use_stmt_gsi);
718 enum tree_code rhs_code;
719 bool res = true;
720
721 gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
722
723 lhs = gimple_assign_lhs (use_stmt);
724 rhs_code = gimple_assign_rhs_code (use_stmt);
725 rhs = gimple_assign_rhs1 (use_stmt);
726
727 /* Trivial cases. The use statement could be a trivial copy or a
728 useless conversion. Recurse to the uses of the lhs as copyprop does
729 not copy through different variant pointers and FRE does not catch
730 all useless conversions. Treat the case of a single-use name and
731 a conversion to def_rhs type separate, though. */
732 if (TREE_CODE (lhs) == SSA_NAME
733 && ((rhs_code == SSA_NAME && rhs == name)
734 || CONVERT_EXPR_CODE_P (rhs_code)))
735 {
736 /* Only recurse if we don't deal with a single use or we cannot
737 do the propagation to the current statement. In particular
738 we can end up with a conversion needed for a non-invariant
739 address which we cannot do in a single statement. */
740 if (!single_use_p
741 || (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))
742 && (!is_gimple_min_invariant (def_rhs)
743 || (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
744 && POINTER_TYPE_P (TREE_TYPE (def_rhs))
745 && (TYPE_PRECISION (TREE_TYPE (lhs))
746 > TYPE_PRECISION (TREE_TYPE (def_rhs)))))))
747 return forward_propagate_addr_expr (lhs, def_rhs);
748
749 gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
750 if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
751 gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
752 else
753 gimple_assign_set_rhs_code (use_stmt, NOP_EXPR);
754 return true;
755 }
756
757 /* Propagate through constant pointer adjustments. */
758 if (TREE_CODE (lhs) == SSA_NAME
759 && rhs_code == POINTER_PLUS_EXPR
760 && rhs == name
761 && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
762 {
763 tree new_def_rhs;
764 /* As we come here with non-invariant addresses in def_rhs we need
765 to make sure we can build a valid constant offsetted address
766 for further propagation. Simply rely on fold building that
767 and check after the fact. */
768 new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
769 def_rhs,
770 fold_convert (ptr_type_node,
771 gimple_assign_rhs2 (use_stmt)));
772 if (TREE_CODE (new_def_rhs) == MEM_REF
773 && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
774 return false;
775 new_def_rhs = build_fold_addr_expr_with_type (new_def_rhs,
776 TREE_TYPE (rhs));
777
778 /* Recurse. If we could propagate into all uses of lhs do not
779 bother to replace into the current use but just pretend we did. */
780 if (TREE_CODE (new_def_rhs) == ADDR_EXPR
781 && forward_propagate_addr_expr (lhs, new_def_rhs))
782 return true;
783
784 if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_def_rhs)))
785 gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
786 new_def_rhs, NULL_TREE);
787 else if (is_gimple_min_invariant (new_def_rhs))
788 gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR,
789 new_def_rhs, NULL_TREE);
790 else
791 return false;
792 gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
793 update_stmt (use_stmt);
794 return true;
795 }
796
797 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
798 ADDR_EXPR will not appear on the LHS. */
799 lhs = gimple_assign_lhs (use_stmt);
800 while (handled_component_p (lhs))
801 lhs = TREE_OPERAND (lhs, 0);
802
803 /* Now see if the LHS node is a MEM_REF using NAME. If so,
804 propagate the ADDR_EXPR into the use of NAME and fold the result. */
805 if (TREE_CODE (lhs) == MEM_REF
806 && TREE_OPERAND (lhs, 0) == name)
807 {
808 tree def_rhs_base;
809 HOST_WIDE_INT def_rhs_offset;
810 /* If the address is invariant we can always fold it. */
811 if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
812 &def_rhs_offset)))
813 {
814 double_int off = mem_ref_offset (lhs);
815 tree new_ptr;
816 off += double_int::from_shwi (def_rhs_offset);
817 if (TREE_CODE (def_rhs_base) == MEM_REF)
818 {
819 off += mem_ref_offset (def_rhs_base);
820 new_ptr = TREE_OPERAND (def_rhs_base, 0);
821 }
822 else
823 new_ptr = build_fold_addr_expr (def_rhs_base);
824 TREE_OPERAND (lhs, 0) = new_ptr;
825 TREE_OPERAND (lhs, 1)
826 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
827 tidy_after_forward_propagate_addr (use_stmt);
828 /* Continue propagating into the RHS if this was not the only use. */
829 if (single_use_p)
830 return true;
831 }
832 /* If the LHS is a plain dereference and the value type is the same as
833 that of the pointed-to type of the address we can put the
834 dereferenced address on the LHS preserving the original alias-type. */
835 else if (gimple_assign_lhs (use_stmt) == lhs
836 && integer_zerop (TREE_OPERAND (lhs, 1))
837 && useless_type_conversion_p
838 (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
839 TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
840 {
841 tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
842 tree new_offset, new_base, saved, new_lhs;
843 while (handled_component_p (*def_rhs_basep))
844 def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
845 saved = *def_rhs_basep;
846 if (TREE_CODE (*def_rhs_basep) == MEM_REF)
847 {
848 new_base = TREE_OPERAND (*def_rhs_basep, 0);
849 new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
850 TREE_OPERAND (*def_rhs_basep, 1));
851 }
852 else
853 {
854 new_base = build_fold_addr_expr (*def_rhs_basep);
855 new_offset = TREE_OPERAND (lhs, 1);
856 }
857 *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
858 new_base, new_offset);
859 TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
860 TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
861 TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
862 new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
863 gimple_assign_set_lhs (use_stmt, new_lhs);
864 TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
865 TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
866 *def_rhs_basep = saved;
867 tidy_after_forward_propagate_addr (use_stmt);
868 /* Continue propagating into the RHS if this was not the
869 only use. */
870 if (single_use_p)
871 return true;
872 }
873 else
874 /* We can have a struct assignment dereferencing our name twice.
875 Note that we didn't propagate into the lhs to not falsely
876 claim we did when propagating into the rhs. */
877 res = false;
878 }
879
880 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
881 nodes from the RHS. */
882 rhs = gimple_assign_rhs1 (use_stmt);
883 if (TREE_CODE (rhs) == ADDR_EXPR)
884 rhs = TREE_OPERAND (rhs, 0);
885 while (handled_component_p (rhs))
886 rhs = TREE_OPERAND (rhs, 0);
887
888 /* Now see if the RHS node is a MEM_REF using NAME. If so,
889 propagate the ADDR_EXPR into the use of NAME and fold the result. */
890 if (TREE_CODE (rhs) == MEM_REF
891 && TREE_OPERAND (rhs, 0) == name)
892 {
893 tree def_rhs_base;
894 HOST_WIDE_INT def_rhs_offset;
895 if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
896 &def_rhs_offset)))
897 {
898 double_int off = mem_ref_offset (rhs);
899 tree new_ptr;
900 off += double_int::from_shwi (def_rhs_offset);
901 if (TREE_CODE (def_rhs_base) == MEM_REF)
902 {
903 off += mem_ref_offset (def_rhs_base);
904 new_ptr = TREE_OPERAND (def_rhs_base, 0);
905 }
906 else
907 new_ptr = build_fold_addr_expr (def_rhs_base);
908 TREE_OPERAND (rhs, 0) = new_ptr;
909 TREE_OPERAND (rhs, 1)
910 = double_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
911 fold_stmt_inplace (use_stmt_gsi);
912 tidy_after_forward_propagate_addr (use_stmt);
913 return res;
914 }
915 /* If the RHS is a plain dereference and the value type is the same as
916 that of the pointed-to type of the address we can put the
917 dereferenced address on the RHS preserving the original alias-type. */
918 else if (gimple_assign_rhs1 (use_stmt) == rhs
919 && integer_zerop (TREE_OPERAND (rhs, 1))
920 && useless_type_conversion_p
921 (TREE_TYPE (gimple_assign_lhs (use_stmt)),
922 TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
923 {
924 tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
925 tree new_offset, new_base, saved, new_rhs;
926 while (handled_component_p (*def_rhs_basep))
927 def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
928 saved = *def_rhs_basep;
929 if (TREE_CODE (*def_rhs_basep) == MEM_REF)
930 {
931 new_base = TREE_OPERAND (*def_rhs_basep, 0);
932 new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
933 TREE_OPERAND (*def_rhs_basep, 1));
934 }
935 else
936 {
937 new_base = build_fold_addr_expr (*def_rhs_basep);
938 new_offset = TREE_OPERAND (rhs, 1);
939 }
940 *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep),
941 new_base, new_offset);
942 TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
943 TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
944 TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
945 new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
946 gimple_assign_set_rhs1 (use_stmt, new_rhs);
947 TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
948 TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
949 *def_rhs_basep = saved;
950 fold_stmt_inplace (use_stmt_gsi);
951 tidy_after_forward_propagate_addr (use_stmt);
952 return res;
953 }
954 }
955
956 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
957 is nothing to do. */
958 if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
959 || gimple_assign_rhs1 (use_stmt) != name)
960 return false;
961
962 /* The remaining cases are all for turning pointer arithmetic into
963 array indexing. They only apply when we have the address of
964 element zero in an array. If that is not the case then there
965 is nothing to do. */
966 array_ref = TREE_OPERAND (def_rhs, 0);
967 if ((TREE_CODE (array_ref) != ARRAY_REF
968 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
969 || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
970 && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
971 return false;
972
973 rhs2 = gimple_assign_rhs2 (use_stmt);
974 /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
975 if (TREE_CODE (rhs2) == INTEGER_CST)
976 {
977 tree new_rhs = build1_loc (gimple_location (use_stmt),
978 ADDR_EXPR, TREE_TYPE (def_rhs),
979 fold_build2 (MEM_REF,
980 TREE_TYPE (TREE_TYPE (def_rhs)),
981 unshare_expr (def_rhs),
982 fold_convert (ptr_type_node,
983 rhs2)));
984 gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
985 use_stmt = gsi_stmt (*use_stmt_gsi);
986 update_stmt (use_stmt);
987 tidy_after_forward_propagate_addr (use_stmt);
988 return true;
989 }
990
991 return false;
992 }
993
994 /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
995
996 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
997 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
998 node or for recovery of array indexing from pointer arithmetic.
999 Returns true, if all uses have been propagated into. */
1000
1001 static bool
1002 forward_propagate_addr_expr (tree name, tree rhs)
1003 {
1004 int stmt_loop_depth = bb_loop_depth (gimple_bb (SSA_NAME_DEF_STMT (name)));
1005 imm_use_iterator iter;
1006 gimple use_stmt;
1007 bool all = true;
1008 bool single_use_p = has_single_use (name);
1009
1010 FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
1011 {
1012 bool result;
1013 tree use_rhs;
1014
1015 /* If the use is not in a simple assignment statement, then
1016 there is nothing we can do. */
1017 if (gimple_code (use_stmt) != GIMPLE_ASSIGN)
1018 {
1019 if (!is_gimple_debug (use_stmt))
1020 all = false;
1021 continue;
1022 }
1023
1024 /* If the use is in a deeper loop nest, then we do not want
1025 to propagate non-invariant ADDR_EXPRs into the loop as that
1026 is likely adding expression evaluations into the loop. */
1027 if (bb_loop_depth (gimple_bb (use_stmt)) > stmt_loop_depth
1028 && !is_gimple_min_invariant (rhs))
1029 {
1030 all = false;
1031 continue;
1032 }
1033
1034 {
1035 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1036 result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
1037 single_use_p);
1038 /* If the use has moved to a different statement adjust
1039 the update machinery for the old statement too. */
1040 if (use_stmt != gsi_stmt (gsi))
1041 {
1042 update_stmt (use_stmt);
1043 use_stmt = gsi_stmt (gsi);
1044 }
1045
1046 update_stmt (use_stmt);
1047 }
1048 all &= result;
1049
1050 /* Remove intermediate now unused copy and conversion chains. */
1051 use_rhs = gimple_assign_rhs1 (use_stmt);
1052 if (result
1053 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
1054 && TREE_CODE (use_rhs) == SSA_NAME
1055 && has_zero_uses (gimple_assign_lhs (use_stmt)))
1056 {
1057 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1058 release_defs (use_stmt);
1059 gsi_remove (&gsi, true);
1060 }
1061 }
1062
1063 return all && has_zero_uses (name);
1064 }
1065
1066
1067 /* Forward propagate the comparison defined in *DEFGSI like
1068 cond_1 = x CMP y to uses of the form
1069 a_1 = (T')cond_1
1070 a_1 = !cond_1
1071 a_1 = cond_1 != 0
1072 Returns true if stmt is now unused. Advance DEFGSI to the next
1073 statement. */
1074
1075 static bool
1076 forward_propagate_comparison (gimple_stmt_iterator *defgsi)
1077 {
1078 gimple stmt = gsi_stmt (*defgsi);
1079 tree name = gimple_assign_lhs (stmt);
1080 gimple use_stmt;
1081 tree tmp = NULL_TREE;
1082 gimple_stmt_iterator gsi;
1083 enum tree_code code;
1084 tree lhs;
1085
1086 /* Don't propagate ssa names that occur in abnormal phis. */
1087 if ((TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1088 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
1089 || (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME
1090 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt))))
1091 goto bailout;
1092
1093 /* Do not un-cse comparisons. But propagate through copies. */
1094 use_stmt = get_prop_dest_stmt (name, &name);
1095 if (!use_stmt
1096 || !is_gimple_assign (use_stmt))
1097 goto bailout;
1098
1099 code = gimple_assign_rhs_code (use_stmt);
1100 lhs = gimple_assign_lhs (use_stmt);
1101 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
1102 goto bailout;
1103
1104 /* We can propagate the condition into a statement that
1105 computes the logical negation of the comparison result. */
1106 if ((code == BIT_NOT_EXPR
1107 && TYPE_PRECISION (TREE_TYPE (lhs)) == 1)
1108 || (code == BIT_XOR_EXPR
1109 && integer_onep (gimple_assign_rhs2 (use_stmt))))
1110 {
1111 tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
1112 bool nans = HONOR_NANS (TYPE_MODE (type));
1113 enum tree_code inv_code;
1114 inv_code = invert_tree_comparison (gimple_assign_rhs_code (stmt), nans);
1115 if (inv_code == ERROR_MARK)
1116 goto bailout;
1117
1118 tmp = build2 (inv_code, TREE_TYPE (lhs), gimple_assign_rhs1 (stmt),
1119 gimple_assign_rhs2 (stmt));
1120 }
1121 else
1122 goto bailout;
1123
1124 gsi = gsi_for_stmt (use_stmt);
1125 gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (tmp));
1126 use_stmt = gsi_stmt (gsi);
1127 update_stmt (use_stmt);
1128
1129 if (dump_file && (dump_flags & TDF_DETAILS))
1130 {
1131 fprintf (dump_file, " Replaced '");
1132 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1133 fprintf (dump_file, "' with '");
1134 print_gimple_expr (dump_file, use_stmt, 0, dump_flags);
1135 fprintf (dump_file, "'\n");
1136 }
1137
1138 /* When we remove stmt now the iterator defgsi goes off it's current
1139 sequence, hence advance it now. */
1140 gsi_next (defgsi);
1141
1142 /* Remove defining statements. */
1143 return remove_prop_source_from_use (name);
1144
1145 bailout:
1146 gsi_next (defgsi);
1147 return false;
1148 }
1149
1150
1151 /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1152 If so, we can change STMT into lhs = y which can later be copy
1153 propagated. Similarly for negation.
1154
1155 This could trivially be formulated as a forward propagation
1156 to immediate uses. However, we already had an implementation
1157 from DOM which used backward propagation via the use-def links.
1158
1159 It turns out that backward propagation is actually faster as
1160 there's less work to do for each NOT/NEG expression we find.
1161 Backwards propagation needs to look at the statement in a single
1162 backlink. Forward propagation needs to look at potentially more
1163 than one forward link.
1164
1165 Returns true when the statement was changed. */
1166
1167 static bool
1168 simplify_not_neg_expr (gimple_stmt_iterator *gsi_p)
1169 {
1170 gimple stmt = gsi_stmt (*gsi_p);
1171 tree rhs = gimple_assign_rhs1 (stmt);
1172 gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
1173
1174 /* See if the RHS_DEF_STMT has the same form as our statement. */
1175 if (is_gimple_assign (rhs_def_stmt)
1176 && gimple_assign_rhs_code (rhs_def_stmt) == gimple_assign_rhs_code (stmt))
1177 {
1178 tree rhs_def_operand = gimple_assign_rhs1 (rhs_def_stmt);
1179
1180 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
1181 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1182 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1183 {
1184 gimple_assign_set_rhs_from_tree (gsi_p, rhs_def_operand);
1185 stmt = gsi_stmt (*gsi_p);
1186 update_stmt (stmt);
1187 return true;
1188 }
1189 }
1190
1191 return false;
1192 }
1193
1194 /* Helper function for simplify_gimple_switch. Remove case labels that
1195 have values outside the range of the new type. */
1196
1197 static void
1198 simplify_gimple_switch_label_vec (gimple stmt, tree index_type)
1199 {
1200 unsigned int branch_num = gimple_switch_num_labels (stmt);
1201 VEC(tree, heap) *labels = VEC_alloc (tree, heap, branch_num);
1202 unsigned int i, len;
1203
1204 /* Collect the existing case labels in a VEC, and preprocess it as if
1205 we are gimplifying a GENERIC SWITCH_EXPR. */
1206 for (i = 1; i < branch_num; i++)
1207 VEC_quick_push (tree, labels, gimple_switch_label (stmt, i));
1208 preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
1209
1210 /* If any labels were removed, replace the existing case labels
1211 in the GIMPLE_SWITCH statement with the correct ones.
1212 Note that the type updates were done in-place on the case labels,
1213 so we only have to replace the case labels in the GIMPLE_SWITCH
1214 if the number of labels changed. */
1215 len = VEC_length (tree, labels);
1216 if (len < branch_num - 1)
1217 {
1218 bitmap target_blocks;
1219 edge_iterator ei;
1220 edge e;
1221
1222 /* Corner case: *all* case labels have been removed as being
1223 out-of-range for INDEX_TYPE. Push one label and let the
1224 CFG cleanups deal with this further. */
1225 if (len == 0)
1226 {
1227 tree label, elt;
1228
1229 label = CASE_LABEL (gimple_switch_default_label (stmt));
1230 elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1231 VEC_quick_push (tree, labels, elt);
1232 len = 1;
1233 }
1234
1235 for (i = 0; i < VEC_length (tree, labels); i++)
1236 gimple_switch_set_label (stmt, i + 1, VEC_index (tree, labels, i));
1237 for (i++ ; i < branch_num; i++)
1238 gimple_switch_set_label (stmt, i, NULL_TREE);
1239 gimple_switch_set_num_labels (stmt, len + 1);
1240
1241 /* Cleanup any edges that are now dead. */
1242 target_blocks = BITMAP_ALLOC (NULL);
1243 for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1244 {
1245 tree elt = gimple_switch_label (stmt, i);
1246 basic_block target = label_to_block (CASE_LABEL (elt));
1247 bitmap_set_bit (target_blocks, target->index);
1248 }
1249 for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1250 {
1251 if (! bitmap_bit_p (target_blocks, e->dest->index))
1252 {
1253 remove_edge (e);
1254 cfg_changed = true;
1255 free_dominance_info (CDI_DOMINATORS);
1256 }
1257 else
1258 ei_next (&ei);
1259 }
1260 BITMAP_FREE (target_blocks);
1261 }
1262
1263 VEC_free (tree, heap, labels);
1264 }
1265
1266 /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1267 the condition which we may be able to optimize better. */
1268
1269 static bool
1270 simplify_gimple_switch (gimple stmt)
1271 {
1272 tree cond = gimple_switch_index (stmt);
1273 tree def, to, ti;
1274 gimple def_stmt;
1275
1276 /* The optimization that we really care about is removing unnecessary
1277 casts. That will let us do much better in propagating the inferred
1278 constant at the switch target. */
1279 if (TREE_CODE (cond) == SSA_NAME)
1280 {
1281 def_stmt = SSA_NAME_DEF_STMT (cond);
1282 if (is_gimple_assign (def_stmt))
1283 {
1284 if (gimple_assign_rhs_code (def_stmt) == NOP_EXPR)
1285 {
1286 int need_precision;
1287 bool fail;
1288
1289 def = gimple_assign_rhs1 (def_stmt);
1290
1291 to = TREE_TYPE (cond);
1292 ti = TREE_TYPE (def);
1293
1294 /* If we have an extension that preserves value, then we
1295 can copy the source value into the switch. */
1296
1297 need_precision = TYPE_PRECISION (ti);
1298 fail = false;
1299 if (! INTEGRAL_TYPE_P (ti))
1300 fail = true;
1301 else if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
1302 fail = true;
1303 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
1304 need_precision += 1;
1305 if (TYPE_PRECISION (to) < need_precision)
1306 fail = true;
1307
1308 if (!fail)
1309 {
1310 gimple_switch_set_index (stmt, def);
1311 simplify_gimple_switch_label_vec (stmt, ti);
1312 update_stmt (stmt);
1313 return true;
1314 }
1315 }
1316 }
1317 }
1318
1319 return false;
1320 }
1321
1322 /* For pointers p2 and p1 return p2 - p1 if the
1323 difference is known and constant, otherwise return NULL. */
1324
1325 static tree
1326 constant_pointer_difference (tree p1, tree p2)
1327 {
1328 int i, j;
1329 #define CPD_ITERATIONS 5
1330 tree exps[2][CPD_ITERATIONS];
1331 tree offs[2][CPD_ITERATIONS];
1332 int cnt[2];
1333
1334 for (i = 0; i < 2; i++)
1335 {
1336 tree p = i ? p1 : p2;
1337 tree off = size_zero_node;
1338 gimple stmt;
1339 enum tree_code code;
1340
1341 /* For each of p1 and p2 we need to iterate at least
1342 twice, to handle ADDR_EXPR directly in p1/p2,
1343 SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1344 on definition's stmt RHS. Iterate a few extra times. */
1345 j = 0;
1346 do
1347 {
1348 if (!POINTER_TYPE_P (TREE_TYPE (p)))
1349 break;
1350 if (TREE_CODE (p) == ADDR_EXPR)
1351 {
1352 tree q = TREE_OPERAND (p, 0);
1353 HOST_WIDE_INT offset;
1354 tree base = get_addr_base_and_unit_offset (q, &offset);
1355 if (base)
1356 {
1357 q = base;
1358 if (offset)
1359 off = size_binop (PLUS_EXPR, off, size_int (offset));
1360 }
1361 if (TREE_CODE (q) == MEM_REF
1362 && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1363 {
1364 p = TREE_OPERAND (q, 0);
1365 off = size_binop (PLUS_EXPR, off,
1366 double_int_to_tree (sizetype,
1367 mem_ref_offset (q)));
1368 }
1369 else
1370 {
1371 exps[i][j] = q;
1372 offs[i][j++] = off;
1373 break;
1374 }
1375 }
1376 if (TREE_CODE (p) != SSA_NAME)
1377 break;
1378 exps[i][j] = p;
1379 offs[i][j++] = off;
1380 if (j == CPD_ITERATIONS)
1381 break;
1382 stmt = SSA_NAME_DEF_STMT (p);
1383 if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1384 break;
1385 code = gimple_assign_rhs_code (stmt);
1386 if (code == POINTER_PLUS_EXPR)
1387 {
1388 if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1389 break;
1390 off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1391 p = gimple_assign_rhs1 (stmt);
1392 }
1393 else if (code == ADDR_EXPR || code == NOP_EXPR)
1394 p = gimple_assign_rhs1 (stmt);
1395 else
1396 break;
1397 }
1398 while (1);
1399 cnt[i] = j;
1400 }
1401
1402 for (i = 0; i < cnt[0]; i++)
1403 for (j = 0; j < cnt[1]; j++)
1404 if (exps[0][i] == exps[1][j])
1405 return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1406
1407 return NULL_TREE;
1408 }
1409
1410 /* *GSI_P is a GIMPLE_CALL to a builtin function.
1411 Optimize
1412 memcpy (p, "abcd", 4);
1413 memset (p + 4, ' ', 3);
1414 into
1415 memcpy (p, "abcd ", 7);
1416 call if the latter can be stored by pieces during expansion. */
1417
1418 static bool
1419 simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2)
1420 {
1421 gimple stmt1, stmt2 = gsi_stmt (*gsi_p);
1422 tree vuse = gimple_vuse (stmt2);
1423 if (vuse == NULL)
1424 return false;
1425 stmt1 = SSA_NAME_DEF_STMT (vuse);
1426
1427 switch (DECL_FUNCTION_CODE (callee2))
1428 {
1429 case BUILT_IN_MEMSET:
1430 if (gimple_call_num_args (stmt2) != 3
1431 || gimple_call_lhs (stmt2)
1432 || CHAR_BIT != 8
1433 || BITS_PER_UNIT != 8)
1434 break;
1435 else
1436 {
1437 tree callee1;
1438 tree ptr1, src1, str1, off1, len1, lhs1;
1439 tree ptr2 = gimple_call_arg (stmt2, 0);
1440 tree val2 = gimple_call_arg (stmt2, 1);
1441 tree len2 = gimple_call_arg (stmt2, 2);
1442 tree diff, vdef, new_str_cst;
1443 gimple use_stmt;
1444 unsigned int ptr1_align;
1445 unsigned HOST_WIDE_INT src_len;
1446 char *src_buf;
1447 use_operand_p use_p;
1448
1449 if (!host_integerp (val2, 0)
1450 || !host_integerp (len2, 1))
1451 break;
1452 if (is_gimple_call (stmt1))
1453 {
1454 /* If first stmt is a call, it needs to be memcpy
1455 or mempcpy, with string literal as second argument and
1456 constant length. */
1457 callee1 = gimple_call_fndecl (stmt1);
1458 if (callee1 == NULL_TREE
1459 || DECL_BUILT_IN_CLASS (callee1) != BUILT_IN_NORMAL
1460 || gimple_call_num_args (stmt1) != 3)
1461 break;
1462 if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
1463 && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
1464 break;
1465 ptr1 = gimple_call_arg (stmt1, 0);
1466 src1 = gimple_call_arg (stmt1, 1);
1467 len1 = gimple_call_arg (stmt1, 2);
1468 lhs1 = gimple_call_lhs (stmt1);
1469 if (!host_integerp (len1, 1))
1470 break;
1471 str1 = string_constant (src1, &off1);
1472 if (str1 == NULL_TREE)
1473 break;
1474 if (!host_integerp (off1, 1)
1475 || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
1476 || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
1477 - tree_low_cst (off1, 1)) > 0
1478 || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
1479 || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
1480 != TYPE_MODE (char_type_node))
1481 break;
1482 }
1483 else if (gimple_assign_single_p (stmt1))
1484 {
1485 /* Otherwise look for length 1 memcpy optimized into
1486 assignment. */
1487 ptr1 = gimple_assign_lhs (stmt1);
1488 src1 = gimple_assign_rhs1 (stmt1);
1489 if (TREE_CODE (ptr1) != MEM_REF
1490 || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
1491 || !host_integerp (src1, 0))
1492 break;
1493 ptr1 = build_fold_addr_expr (ptr1);
1494 callee1 = NULL_TREE;
1495 len1 = size_one_node;
1496 lhs1 = NULL_TREE;
1497 off1 = size_zero_node;
1498 str1 = NULL_TREE;
1499 }
1500 else
1501 break;
1502
1503 diff = constant_pointer_difference (ptr1, ptr2);
1504 if (diff == NULL && lhs1 != NULL)
1505 {
1506 diff = constant_pointer_difference (lhs1, ptr2);
1507 if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1508 && diff != NULL)
1509 diff = size_binop (PLUS_EXPR, diff,
1510 fold_convert (sizetype, len1));
1511 }
1512 /* If the difference between the second and first destination pointer
1513 is not constant, or is bigger than memcpy length, bail out. */
1514 if (diff == NULL
1515 || !host_integerp (diff, 1)
1516 || tree_int_cst_lt (len1, diff))
1517 break;
1518
1519 /* Use maximum of difference plus memset length and memcpy length
1520 as the new memcpy length, if it is too big, bail out. */
1521 src_len = tree_low_cst (diff, 1);
1522 src_len += tree_low_cst (len2, 1);
1523 if (src_len < (unsigned HOST_WIDE_INT) tree_low_cst (len1, 1))
1524 src_len = tree_low_cst (len1, 1);
1525 if (src_len > 1024)
1526 break;
1527
1528 /* If mempcpy value is used elsewhere, bail out, as mempcpy
1529 with bigger length will return different result. */
1530 if (lhs1 != NULL_TREE
1531 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
1532 && (TREE_CODE (lhs1) != SSA_NAME
1533 || !single_imm_use (lhs1, &use_p, &use_stmt)
1534 || use_stmt != stmt2))
1535 break;
1536
1537 /* If anything reads memory in between memcpy and memset
1538 call, the modified memcpy call might change it. */
1539 vdef = gimple_vdef (stmt1);
1540 if (vdef != NULL
1541 && (!single_imm_use (vdef, &use_p, &use_stmt)
1542 || use_stmt != stmt2))
1543 break;
1544
1545 ptr1_align = get_pointer_alignment (ptr1);
1546 /* Construct the new source string literal. */
1547 src_buf = XALLOCAVEC (char, src_len + 1);
1548 if (callee1)
1549 memcpy (src_buf,
1550 TREE_STRING_POINTER (str1) + tree_low_cst (off1, 1),
1551 tree_low_cst (len1, 1));
1552 else
1553 src_buf[0] = tree_low_cst (src1, 0);
1554 memset (src_buf + tree_low_cst (diff, 1),
1555 tree_low_cst (val2, 0), tree_low_cst (len2, 1));
1556 src_buf[src_len] = '\0';
1557 /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
1558 handle embedded '\0's. */
1559 if (strlen (src_buf) != src_len)
1560 break;
1561 rtl_profile_for_bb (gimple_bb (stmt2));
1562 /* If the new memcpy wouldn't be emitted by storing the literal
1563 by pieces, this optimization might enlarge .rodata too much,
1564 as commonly used string literals couldn't be shared any
1565 longer. */
1566 if (!can_store_by_pieces (src_len,
1567 builtin_strncpy_read_str,
1568 src_buf, ptr1_align, false))
1569 break;
1570
1571 new_str_cst = build_string_literal (src_len, src_buf);
1572 if (callee1)
1573 {
1574 /* If STMT1 is a mem{,p}cpy call, adjust it and remove
1575 memset call. */
1576 if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1577 gimple_call_set_lhs (stmt1, NULL_TREE);
1578 gimple_call_set_arg (stmt1, 1, new_str_cst);
1579 gimple_call_set_arg (stmt1, 2,
1580 build_int_cst (TREE_TYPE (len1), src_len));
1581 update_stmt (stmt1);
1582 unlink_stmt_vdef (stmt2);
1583 gsi_remove (gsi_p, true);
1584 release_defs (stmt2);
1585 if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
1586 release_ssa_name (lhs1);
1587 return true;
1588 }
1589 else
1590 {
1591 /* Otherwise, if STMT1 is length 1 memcpy optimized into
1592 assignment, remove STMT1 and change memset call into
1593 memcpy call. */
1594 gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
1595
1596 if (!is_gimple_val (ptr1))
1597 ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
1598 true, GSI_SAME_STMT);
1599 gimple_call_set_fndecl (stmt2,
1600 builtin_decl_explicit (BUILT_IN_MEMCPY));
1601 gimple_call_set_arg (stmt2, 0, ptr1);
1602 gimple_call_set_arg (stmt2, 1, new_str_cst);
1603 gimple_call_set_arg (stmt2, 2,
1604 build_int_cst (TREE_TYPE (len2), src_len));
1605 unlink_stmt_vdef (stmt1);
1606 gsi_remove (&gsi, true);
1607 release_defs (stmt1);
1608 update_stmt (stmt2);
1609 return false;
1610 }
1611 }
1612 break;
1613 default:
1614 break;
1615 }
1616 return false;
1617 }
1618
1619 /* Checks if expression has type of one-bit precision, or is a known
1620 truth-valued expression. */
1621 static bool
1622 truth_valued_ssa_name (tree name)
1623 {
1624 gimple def;
1625 tree type = TREE_TYPE (name);
1626
1627 if (!INTEGRAL_TYPE_P (type))
1628 return false;
1629 /* Don't check here for BOOLEAN_TYPE as the precision isn't
1630 necessarily one and so ~X is not equal to !X. */
1631 if (TYPE_PRECISION (type) == 1)
1632 return true;
1633 def = SSA_NAME_DEF_STMT (name);
1634 if (is_gimple_assign (def))
1635 return truth_value_p (gimple_assign_rhs_code (def));
1636 return false;
1637 }
1638
1639 /* Helper routine for simplify_bitwise_binary_1 function.
1640 Return for the SSA name NAME the expression X if it mets condition
1641 NAME = !X. Otherwise return NULL_TREE.
1642 Detected patterns for NAME = !X are:
1643 !X and X == 0 for X with integral type.
1644 X ^ 1, X != 1,or ~X for X with integral type with precision of one. */
1645 static tree
1646 lookup_logical_inverted_value (tree name)
1647 {
1648 tree op1, op2;
1649 enum tree_code code;
1650 gimple def;
1651
1652 /* If name has none-intergal type, or isn't a SSA_NAME, then
1653 return. */
1654 if (TREE_CODE (name) != SSA_NAME
1655 || !INTEGRAL_TYPE_P (TREE_TYPE (name)))
1656 return NULL_TREE;
1657 def = SSA_NAME_DEF_STMT (name);
1658 if (!is_gimple_assign (def))
1659 return NULL_TREE;
1660
1661 code = gimple_assign_rhs_code (def);
1662 op1 = gimple_assign_rhs1 (def);
1663 op2 = NULL_TREE;
1664
1665 /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand.
1666 If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return. */
1667 if (code == EQ_EXPR || code == NE_EXPR
1668 || code == BIT_XOR_EXPR)
1669 op2 = gimple_assign_rhs2 (def);
1670
1671 switch (code)
1672 {
1673 case BIT_NOT_EXPR:
1674 if (truth_valued_ssa_name (name))
1675 return op1;
1676 break;
1677 case EQ_EXPR:
1678 /* Check if we have X == 0 and X has an integral type. */
1679 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1)))
1680 break;
1681 if (integer_zerop (op2))
1682 return op1;
1683 break;
1684 case NE_EXPR:
1685 /* Check if we have X != 1 and X is a truth-valued. */
1686 if (!INTEGRAL_TYPE_P (TREE_TYPE (op1)))
1687 break;
1688 if (integer_onep (op2) && truth_valued_ssa_name (op1))
1689 return op1;
1690 break;
1691 case BIT_XOR_EXPR:
1692 /* Check if we have X ^ 1 and X is truth valued. */
1693 if (integer_onep (op2) && truth_valued_ssa_name (op1))
1694 return op1;
1695 break;
1696 default:
1697 break;
1698 }
1699
1700 return NULL_TREE;
1701 }
1702
1703 /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary
1704 operations CODE, if one operand has the logically inverted
1705 value of the other. */
1706 static tree
1707 simplify_bitwise_binary_1 (enum tree_code code, tree type,
1708 tree arg1, tree arg2)
1709 {
1710 tree anot;
1711
1712 /* If CODE isn't a bitwise binary operation, return NULL_TREE. */
1713 if (code != BIT_AND_EXPR && code != BIT_IOR_EXPR
1714 && code != BIT_XOR_EXPR)
1715 return NULL_TREE;
1716
1717 /* First check if operands ARG1 and ARG2 are equal. If so
1718 return NULL_TREE as this optimization is handled fold_stmt. */
1719 if (arg1 == arg2)
1720 return NULL_TREE;
1721 /* See if we have in arguments logical-not patterns. */
1722 if (((anot = lookup_logical_inverted_value (arg1)) == NULL_TREE
1723 || anot != arg2)
1724 && ((anot = lookup_logical_inverted_value (arg2)) == NULL_TREE
1725 || anot != arg1))
1726 return NULL_TREE;
1727
1728 /* X & !X -> 0. */
1729 if (code == BIT_AND_EXPR)
1730 return fold_convert (type, integer_zero_node);
1731 /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued. */
1732 if (truth_valued_ssa_name (anot))
1733 return fold_convert (type, integer_one_node);
1734
1735 /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */
1736 return NULL_TREE;
1737 }
1738
1739 /* Given a ssa_name in NAME see if it was defined by an assignment and
1740 set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
1741 to the second operand on the rhs. */
1742
1743 static inline void
1744 defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
1745 {
1746 gimple def;
1747 enum tree_code code1;
1748 tree arg11;
1749 tree arg21;
1750 tree arg31;
1751 enum gimple_rhs_class grhs_class;
1752
1753 code1 = TREE_CODE (name);
1754 arg11 = name;
1755 arg21 = NULL_TREE;
1756 grhs_class = get_gimple_rhs_class (code1);
1757
1758 if (code1 == SSA_NAME)
1759 {
1760 def = SSA_NAME_DEF_STMT (name);
1761
1762 if (def && is_gimple_assign (def)
1763 && can_propagate_from (def))
1764 {
1765 code1 = gimple_assign_rhs_code (def);
1766 arg11 = gimple_assign_rhs1 (def);
1767 arg21 = gimple_assign_rhs2 (def);
1768 arg31 = gimple_assign_rhs2 (def);
1769 }
1770 }
1771 else if (grhs_class == GIMPLE_TERNARY_RHS
1772 || GIMPLE_BINARY_RHS
1773 || GIMPLE_UNARY_RHS
1774 || GIMPLE_SINGLE_RHS)
1775 extract_ops_from_tree_1 (name, &code1, &arg11, &arg21, &arg31);
1776
1777 *code = code1;
1778 *arg1 = arg11;
1779 if (arg2)
1780 *arg2 = arg21;
1781 /* Ignore arg3 currently. */
1782 }
1783
1784 /* Simplify bitwise binary operations.
1785 Return true if a transformation applied, otherwise return false. */
1786
1787 static bool
1788 simplify_bitwise_binary (gimple_stmt_iterator *gsi)
1789 {
1790 gimple stmt = gsi_stmt (*gsi);
1791 tree arg1 = gimple_assign_rhs1 (stmt);
1792 tree arg2 = gimple_assign_rhs2 (stmt);
1793 enum tree_code code = gimple_assign_rhs_code (stmt);
1794 tree res;
1795 tree def1_arg1, def1_arg2, def2_arg1, def2_arg2;
1796 enum tree_code def1_code, def2_code;
1797
1798 defcodefor_name (arg1, &def1_code, &def1_arg1, &def1_arg2);
1799 defcodefor_name (arg2, &def2_code, &def2_arg1, &def2_arg2);
1800
1801 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)). */
1802 if (TREE_CODE (arg2) == INTEGER_CST
1803 && CONVERT_EXPR_CODE_P (def1_code)
1804 && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1))
1805 && int_fits_type_p (arg2, TREE_TYPE (def1_arg1)))
1806 {
1807 gimple newop;
1808 tree tem = make_ssa_name (TREE_TYPE (def1_arg1), NULL);
1809 newop =
1810 gimple_build_assign_with_ops (code, tem, def1_arg1,
1811 fold_convert_loc (gimple_location (stmt),
1812 TREE_TYPE (def1_arg1),
1813 arg2));
1814 gimple_set_location (newop, gimple_location (stmt));
1815 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
1816 gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR,
1817 tem, NULL_TREE, NULL_TREE);
1818 update_stmt (gsi_stmt (*gsi));
1819 return true;
1820 }
1821
1822 /* For bitwise binary operations apply operand conversions to the
1823 binary operation result instead of to the operands. This allows
1824 to combine successive conversions and bitwise binary operations. */
1825 if (CONVERT_EXPR_CODE_P (def1_code)
1826 && CONVERT_EXPR_CODE_P (def2_code)
1827 && types_compatible_p (TREE_TYPE (def1_arg1), TREE_TYPE (def2_arg1))
1828 /* Make sure that the conversion widens the operands, or has same
1829 precision, or that it changes the operation to a bitfield
1830 precision. */
1831 && ((TYPE_PRECISION (TREE_TYPE (def1_arg1))
1832 <= TYPE_PRECISION (TREE_TYPE (arg1)))
1833 || (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (arg1)))
1834 != MODE_INT)
1835 || (TYPE_PRECISION (TREE_TYPE (arg1))
1836 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg1))))))
1837 {
1838 gimple newop;
1839 tree tem = make_ssa_name (TREE_TYPE (def1_arg1), NULL);
1840 newop = gimple_build_assign_with_ops (code, tem, def1_arg1, def2_arg1);
1841 gimple_set_location (newop, gimple_location (stmt));
1842 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
1843 gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR,
1844 tem, NULL_TREE, NULL_TREE);
1845 update_stmt (gsi_stmt (*gsi));
1846 return true;
1847 }
1848
1849
1850 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
1851 if (def1_code == def2_code
1852 && def1_code == BIT_AND_EXPR
1853 && operand_equal_for_phi_arg_p (def1_arg2,
1854 def2_arg2))
1855 {
1856 tree b = def1_arg2;
1857 tree a = def1_arg1;
1858 tree c = def2_arg1;
1859 tree inner = fold_build2 (code, TREE_TYPE (arg2), a, c);
1860 /* If A OP0 C (this usually means C is the same as A) is 0
1861 then fold it down correctly. */
1862 if (integer_zerop (inner))
1863 {
1864 gimple_assign_set_rhs_from_tree (gsi, inner);
1865 update_stmt (stmt);
1866 return true;
1867 }
1868 /* If A OP0 C (this usually means C is the same as A) is a ssa_name
1869 then fold it down correctly. */
1870 else if (TREE_CODE (inner) == SSA_NAME)
1871 {
1872 tree outer = fold_build2 (def1_code, TREE_TYPE (inner),
1873 inner, b);
1874 gimple_assign_set_rhs_from_tree (gsi, outer);
1875 update_stmt (stmt);
1876 return true;
1877 }
1878 else
1879 {
1880 gimple newop;
1881 tree tem;
1882 tem = make_ssa_name (TREE_TYPE (arg2), NULL);
1883 newop = gimple_build_assign_with_ops (code, tem, a, c);
1884 gimple_set_location (newop, gimple_location (stmt));
1885 /* Make sure to re-process the new stmt as it's walking upwards. */
1886 gsi_insert_before (gsi, newop, GSI_NEW_STMT);
1887 gimple_assign_set_rhs1 (stmt, tem);
1888 gimple_assign_set_rhs2 (stmt, b);
1889 gimple_assign_set_rhs_code (stmt, def1_code);
1890 update_stmt (stmt);
1891 return true;
1892 }
1893 }
1894
1895 /* (a | CST1) & CST2 -> (a & CST2) | (CST1 & CST2). */
1896 if (code == BIT_AND_EXPR
1897 && def1_code == BIT_IOR_EXPR
1898 && TREE_CODE (arg2) == INTEGER_CST
1899 && TREE_CODE (def1_arg2) == INTEGER_CST)
1900 {
1901 tree cst = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg2),
1902 arg2, def1_arg2);
1903 tree tem;
1904 gimple newop;
1905 if (integer_zerop (cst))
1906 {
1907 gimple_assign_set_rhs1 (stmt, def1_arg1);
1908 update_stmt (stmt);
1909 return true;
1910 }
1911 tem = make_ssa_name (TREE_TYPE (arg2), NULL);
1912 newop = gimple_build_assign_with_ops (BIT_AND_EXPR,
1913 tem, def1_arg1, arg2);
1914 gimple_set_location (newop, gimple_location (stmt));
1915 /* Make sure to re-process the new stmt as it's walking upwards. */
1916 gsi_insert_before (gsi, newop, GSI_NEW_STMT);
1917 gimple_assign_set_rhs1 (stmt, tem);
1918 gimple_assign_set_rhs2 (stmt, cst);
1919 gimple_assign_set_rhs_code (stmt, BIT_IOR_EXPR);
1920 update_stmt (stmt);
1921 return true;
1922 }
1923
1924 /* Combine successive equal operations with constants. */
1925 if ((code == BIT_AND_EXPR
1926 || code == BIT_IOR_EXPR
1927 || code == BIT_XOR_EXPR)
1928 && def1_code == code
1929 && TREE_CODE (arg2) == INTEGER_CST
1930 && TREE_CODE (def1_arg2) == INTEGER_CST)
1931 {
1932 tree cst = fold_build2 (code, TREE_TYPE (arg2),
1933 arg2, def1_arg2);
1934 gimple_assign_set_rhs1 (stmt, def1_arg1);
1935 gimple_assign_set_rhs2 (stmt, cst);
1936 update_stmt (stmt);
1937 return true;
1938 }
1939
1940 /* Canonicalize X ^ ~0 to ~X. */
1941 if (code == BIT_XOR_EXPR
1942 && TREE_CODE (arg2) == INTEGER_CST
1943 && integer_all_onesp (arg2))
1944 {
1945 gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, arg1, NULL_TREE);
1946 gcc_assert (gsi_stmt (*gsi) == stmt);
1947 update_stmt (stmt);
1948 return true;
1949 }
1950
1951 /* Try simple folding for X op !X, and X op X. */
1952 res = simplify_bitwise_binary_1 (code, TREE_TYPE (arg1), arg1, arg2);
1953 if (res != NULL_TREE)
1954 {
1955 gimple_assign_set_rhs_from_tree (gsi, res);
1956 update_stmt (gsi_stmt (*gsi));
1957 return true;
1958 }
1959
1960 if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
1961 {
1962 enum tree_code ocode = code == BIT_AND_EXPR ? BIT_IOR_EXPR : BIT_AND_EXPR;
1963 if (def1_code == ocode)
1964 {
1965 tree x = arg2;
1966 enum tree_code coden;
1967 tree a1, a2;
1968 /* ( X | Y) & X -> X */
1969 /* ( X & Y) | X -> X */
1970 if (x == def1_arg1
1971 || x == def1_arg2)
1972 {
1973 gimple_assign_set_rhs_from_tree (gsi, x);
1974 update_stmt (gsi_stmt (*gsi));
1975 return true;
1976 }
1977
1978 defcodefor_name (def1_arg1, &coden, &a1, &a2);
1979 /* (~X | Y) & X -> X & Y */
1980 /* (~X & Y) | X -> X | Y */
1981 if (coden == BIT_NOT_EXPR && a1 == x)
1982 {
1983 gimple_assign_set_rhs_with_ops (gsi, code,
1984 x, def1_arg2);
1985 gcc_assert (gsi_stmt (*gsi) == stmt);
1986 update_stmt (stmt);
1987 return true;
1988 }
1989 defcodefor_name (def1_arg2, &coden, &a1, &a2);
1990 /* (Y | ~X) & X -> X & Y */
1991 /* (Y & ~X) | X -> X | Y */
1992 if (coden == BIT_NOT_EXPR && a1 == x)
1993 {
1994 gimple_assign_set_rhs_with_ops (gsi, code,
1995 x, def1_arg1);
1996 gcc_assert (gsi_stmt (*gsi) == stmt);
1997 update_stmt (stmt);
1998 return true;
1999 }
2000 }
2001 if (def2_code == ocode)
2002 {
2003 enum tree_code coden;
2004 tree a1;
2005 tree x = arg1;
2006 /* X & ( X | Y) -> X */
2007 /* X | ( X & Y) -> X */
2008 if (x == def2_arg1
2009 || x == def2_arg2)
2010 {
2011 gimple_assign_set_rhs_from_tree (gsi, x);
2012 update_stmt (gsi_stmt (*gsi));
2013 return true;
2014 }
2015 defcodefor_name (def2_arg1, &coden, &a1, NULL);
2016 /* (~X | Y) & X -> X & Y */
2017 /* (~X & Y) | X -> X | Y */
2018 if (coden == BIT_NOT_EXPR && a1 == x)
2019 {
2020 gimple_assign_set_rhs_with_ops (gsi, code,
2021 x, def2_arg2);
2022 gcc_assert (gsi_stmt (*gsi) == stmt);
2023 update_stmt (stmt);
2024 return true;
2025 }
2026 defcodefor_name (def2_arg2, &coden, &a1, NULL);
2027 /* (Y | ~X) & X -> X & Y */
2028 /* (Y & ~X) | X -> X | Y */
2029 if (coden == BIT_NOT_EXPR && a1 == x)
2030 {
2031 gimple_assign_set_rhs_with_ops (gsi, code,
2032 x, def2_arg1);
2033 gcc_assert (gsi_stmt (*gsi) == stmt);
2034 update_stmt (stmt);
2035 return true;
2036 }
2037 }
2038 }
2039
2040 return false;
2041 }
2042
2043
2044 /* Perform re-associations of the plus or minus statement STMT that are
2045 always permitted. Returns true if the CFG was changed. */
2046
2047 static bool
2048 associate_plusminus (gimple_stmt_iterator *gsi)
2049 {
2050 gimple stmt = gsi_stmt (*gsi);
2051 tree rhs1 = gimple_assign_rhs1 (stmt);
2052 tree rhs2 = gimple_assign_rhs2 (stmt);
2053 enum tree_code code = gimple_assign_rhs_code (stmt);
2054 bool changed;
2055
2056 /* We can't reassociate at all for saturating types. */
2057 if (TYPE_SATURATING (TREE_TYPE (rhs1)))
2058 return false;
2059
2060 /* First contract negates. */
2061 do
2062 {
2063 changed = false;
2064
2065 /* A +- (-B) -> A -+ B. */
2066 if (TREE_CODE (rhs2) == SSA_NAME)
2067 {
2068 gimple def_stmt = SSA_NAME_DEF_STMT (rhs2);
2069 if (is_gimple_assign (def_stmt)
2070 && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
2071 && can_propagate_from (def_stmt))
2072 {
2073 code = (code == MINUS_EXPR) ? PLUS_EXPR : MINUS_EXPR;
2074 gimple_assign_set_rhs_code (stmt, code);
2075 rhs2 = gimple_assign_rhs1 (def_stmt);
2076 gimple_assign_set_rhs2 (stmt, rhs2);
2077 gimple_set_modified (stmt, true);
2078 changed = true;
2079 }
2080 }
2081
2082 /* (-A) + B -> B - A. */
2083 if (TREE_CODE (rhs1) == SSA_NAME
2084 && code == PLUS_EXPR)
2085 {
2086 gimple def_stmt = SSA_NAME_DEF_STMT (rhs1);
2087 if (is_gimple_assign (def_stmt)
2088 && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
2089 && can_propagate_from (def_stmt))
2090 {
2091 code = MINUS_EXPR;
2092 gimple_assign_set_rhs_code (stmt, code);
2093 rhs1 = rhs2;
2094 gimple_assign_set_rhs1 (stmt, rhs1);
2095 rhs2 = gimple_assign_rhs1 (def_stmt);
2096 gimple_assign_set_rhs2 (stmt, rhs2);
2097 gimple_set_modified (stmt, true);
2098 changed = true;
2099 }
2100 }
2101 }
2102 while (changed);
2103
2104 /* We can't reassociate floating-point or fixed-point plus or minus
2105 because of saturation to +-Inf. */
2106 if (FLOAT_TYPE_P (TREE_TYPE (rhs1))
2107 || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1)))
2108 goto out;
2109
2110 /* Second match patterns that allow contracting a plus-minus pair
2111 irrespective of overflow issues.
2112
2113 (A +- B) - A -> +- B
2114 (A +- B) -+ B -> A
2115 (CST +- A) +- CST -> CST +- A
2116 (A + CST) +- CST -> A + CST
2117 ~A + A -> -1
2118 ~A + 1 -> -A
2119 A - (A +- B) -> -+ B
2120 A +- (B +- A) -> +- B
2121 CST +- (CST +- A) -> CST +- A
2122 CST +- (A +- CST) -> CST +- A
2123 A + ~A -> -1
2124
2125 via commutating the addition and contracting operations to zero
2126 by reassociation. */
2127
2128 if (TREE_CODE (rhs1) == SSA_NAME)
2129 {
2130 gimple def_stmt = SSA_NAME_DEF_STMT (rhs1);
2131 if (is_gimple_assign (def_stmt) && can_propagate_from (def_stmt))
2132 {
2133 enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
2134 if (def_code == PLUS_EXPR
2135 || def_code == MINUS_EXPR)
2136 {
2137 tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2138 tree def_rhs2 = gimple_assign_rhs2 (def_stmt);
2139 if (operand_equal_p (def_rhs1, rhs2, 0)
2140 && code == MINUS_EXPR)
2141 {
2142 /* (A +- B) - A -> +- B. */
2143 code = ((def_code == PLUS_EXPR)
2144 ? TREE_CODE (def_rhs2) : NEGATE_EXPR);
2145 rhs1 = def_rhs2;
2146 rhs2 = NULL_TREE;
2147 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2148 gcc_assert (gsi_stmt (*gsi) == stmt);
2149 gimple_set_modified (stmt, true);
2150 }
2151 else if (operand_equal_p (def_rhs2, rhs2, 0)
2152 && code != def_code)
2153 {
2154 /* (A +- B) -+ B -> A. */
2155 code = TREE_CODE (def_rhs1);
2156 rhs1 = def_rhs1;
2157 rhs2 = NULL_TREE;
2158 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2159 gcc_assert (gsi_stmt (*gsi) == stmt);
2160 gimple_set_modified (stmt, true);
2161 }
2162 else if (TREE_CODE (rhs2) == INTEGER_CST
2163 && TREE_CODE (def_rhs1) == INTEGER_CST)
2164 {
2165 /* (CST +- A) +- CST -> CST +- A. */
2166 tree cst = fold_binary (code, TREE_TYPE (rhs1),
2167 def_rhs1, rhs2);
2168 if (cst && !TREE_OVERFLOW (cst))
2169 {
2170 code = def_code;
2171 gimple_assign_set_rhs_code (stmt, code);
2172 rhs1 = cst;
2173 gimple_assign_set_rhs1 (stmt, rhs1);
2174 rhs2 = def_rhs2;
2175 gimple_assign_set_rhs2 (stmt, rhs2);
2176 gimple_set_modified (stmt, true);
2177 }
2178 }
2179 else if (TREE_CODE (rhs2) == INTEGER_CST
2180 && TREE_CODE (def_rhs2) == INTEGER_CST
2181 && def_code == PLUS_EXPR)
2182 {
2183 /* (A + CST) +- CST -> A + CST. */
2184 tree cst = fold_binary (code, TREE_TYPE (rhs1),
2185 def_rhs2, rhs2);
2186 if (cst && !TREE_OVERFLOW (cst))
2187 {
2188 code = PLUS_EXPR;
2189 gimple_assign_set_rhs_code (stmt, code);
2190 rhs1 = def_rhs1;
2191 gimple_assign_set_rhs1 (stmt, rhs1);
2192 rhs2 = cst;
2193 gimple_assign_set_rhs2 (stmt, rhs2);
2194 gimple_set_modified (stmt, true);
2195 }
2196 }
2197 }
2198 else if (def_code == BIT_NOT_EXPR
2199 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
2200 {
2201 tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2202 if (code == PLUS_EXPR
2203 && operand_equal_p (def_rhs1, rhs2, 0))
2204 {
2205 /* ~A + A -> -1. */
2206 code = INTEGER_CST;
2207 rhs1 = build_int_cst_type (TREE_TYPE (rhs2), -1);
2208 rhs2 = NULL_TREE;
2209 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2210 gcc_assert (gsi_stmt (*gsi) == stmt);
2211 gimple_set_modified (stmt, true);
2212 }
2213 else if (code == PLUS_EXPR
2214 && integer_onep (rhs1))
2215 {
2216 /* ~A + 1 -> -A. */
2217 code = NEGATE_EXPR;
2218 rhs1 = def_rhs1;
2219 rhs2 = NULL_TREE;
2220 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2221 gcc_assert (gsi_stmt (*gsi) == stmt);
2222 gimple_set_modified (stmt, true);
2223 }
2224 }
2225 }
2226 }
2227
2228 if (rhs2 && TREE_CODE (rhs2) == SSA_NAME)
2229 {
2230 gimple def_stmt = SSA_NAME_DEF_STMT (rhs2);
2231 if (is_gimple_assign (def_stmt) && can_propagate_from (def_stmt))
2232 {
2233 enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
2234 if (def_code == PLUS_EXPR
2235 || def_code == MINUS_EXPR)
2236 {
2237 tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2238 tree def_rhs2 = gimple_assign_rhs2 (def_stmt);
2239 if (operand_equal_p (def_rhs1, rhs1, 0)
2240 && code == MINUS_EXPR)
2241 {
2242 /* A - (A +- B) -> -+ B. */
2243 code = ((def_code == PLUS_EXPR)
2244 ? NEGATE_EXPR : TREE_CODE (def_rhs2));
2245 rhs1 = def_rhs2;
2246 rhs2 = NULL_TREE;
2247 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2248 gcc_assert (gsi_stmt (*gsi) == stmt);
2249 gimple_set_modified (stmt, true);
2250 }
2251 else if (operand_equal_p (def_rhs2, rhs1, 0)
2252 && code != def_code)
2253 {
2254 /* A +- (B +- A) -> +- B. */
2255 code = ((code == PLUS_EXPR)
2256 ? TREE_CODE (def_rhs1) : NEGATE_EXPR);
2257 rhs1 = def_rhs1;
2258 rhs2 = NULL_TREE;
2259 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2260 gcc_assert (gsi_stmt (*gsi) == stmt);
2261 gimple_set_modified (stmt, true);
2262 }
2263 else if (TREE_CODE (rhs1) == INTEGER_CST
2264 && TREE_CODE (def_rhs1) == INTEGER_CST)
2265 {
2266 /* CST +- (CST +- A) -> CST +- A. */
2267 tree cst = fold_binary (code, TREE_TYPE (rhs2),
2268 rhs1, def_rhs1);
2269 if (cst && !TREE_OVERFLOW (cst))
2270 {
2271 code = (code == def_code ? PLUS_EXPR : MINUS_EXPR);
2272 gimple_assign_set_rhs_code (stmt, code);
2273 rhs1 = cst;
2274 gimple_assign_set_rhs1 (stmt, rhs1);
2275 rhs2 = def_rhs2;
2276 gimple_assign_set_rhs2 (stmt, rhs2);
2277 gimple_set_modified (stmt, true);
2278 }
2279 }
2280 else if (TREE_CODE (rhs1) == INTEGER_CST
2281 && TREE_CODE (def_rhs2) == INTEGER_CST)
2282 {
2283 /* CST +- (A +- CST) -> CST +- A. */
2284 tree cst = fold_binary (def_code == code
2285 ? PLUS_EXPR : MINUS_EXPR,
2286 TREE_TYPE (rhs2),
2287 rhs1, def_rhs2);
2288 if (cst && !TREE_OVERFLOW (cst))
2289 {
2290 rhs1 = cst;
2291 gimple_assign_set_rhs1 (stmt, rhs1);
2292 rhs2 = def_rhs1;
2293 gimple_assign_set_rhs2 (stmt, rhs2);
2294 gimple_set_modified (stmt, true);
2295 }
2296 }
2297 }
2298 else if (def_code == BIT_NOT_EXPR
2299 && INTEGRAL_TYPE_P (TREE_TYPE (rhs2)))
2300 {
2301 tree def_rhs1 = gimple_assign_rhs1 (def_stmt);
2302 if (code == PLUS_EXPR
2303 && operand_equal_p (def_rhs1, rhs1, 0))
2304 {
2305 /* A + ~A -> -1. */
2306 code = INTEGER_CST;
2307 rhs1 = build_int_cst_type (TREE_TYPE (rhs1), -1);
2308 rhs2 = NULL_TREE;
2309 gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE);
2310 gcc_assert (gsi_stmt (*gsi) == stmt);
2311 gimple_set_modified (stmt, true);
2312 }
2313 }
2314 }
2315 }
2316
2317 out:
2318 if (gimple_modified_p (stmt))
2319 {
2320 fold_stmt_inplace (gsi);
2321 update_stmt (stmt);
2322 if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
2323 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
2324 return true;
2325 }
2326
2327 return false;
2328 }
2329
2330 /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns
2331 true if anything changed, false otherwise. */
2332
2333 static bool
2334 associate_pointerplus (gimple_stmt_iterator *gsi)
2335 {
2336 gimple stmt = gsi_stmt (*gsi);
2337 gimple def_stmt;
2338 tree ptr, rhs, algn;
2339
2340 /* Pattern match
2341 tem = (sizetype) ptr;
2342 tem = tem & algn;
2343 tem = -tem;
2344 ... = ptr p+ tem;
2345 and produce the simpler and easier to analyze with respect to alignment
2346 ... = ptr & ~algn; */
2347 ptr = gimple_assign_rhs1 (stmt);
2348 rhs = gimple_assign_rhs2 (stmt);
2349 if (TREE_CODE (rhs) != SSA_NAME)
2350 return false;
2351 def_stmt = SSA_NAME_DEF_STMT (rhs);
2352 if (!is_gimple_assign (def_stmt)
2353 || gimple_assign_rhs_code (def_stmt) != NEGATE_EXPR)
2354 return false;
2355 rhs = gimple_assign_rhs1 (def_stmt);
2356 if (TREE_CODE (rhs) != SSA_NAME)
2357 return false;
2358 def_stmt = SSA_NAME_DEF_STMT (rhs);
2359 if (!is_gimple_assign (def_stmt)
2360 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
2361 return false;
2362 rhs = gimple_assign_rhs1 (def_stmt);
2363 algn = gimple_assign_rhs2 (def_stmt);
2364 if (TREE_CODE (rhs) != SSA_NAME
2365 || TREE_CODE (algn) != INTEGER_CST)
2366 return false;
2367 def_stmt = SSA_NAME_DEF_STMT (rhs);
2368 if (!is_gimple_assign (def_stmt)
2369 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
2370 return false;
2371 if (gimple_assign_rhs1 (def_stmt) != ptr)
2372 return false;
2373
2374 algn = double_int_to_tree (TREE_TYPE (ptr), ~tree_to_double_int (algn));
2375 gimple_assign_set_rhs_with_ops (gsi, BIT_AND_EXPR, ptr, algn);
2376 fold_stmt_inplace (gsi);
2377 update_stmt (stmt);
2378
2379 return true;
2380 }
2381
2382 /* Combine two conversions in a row for the second conversion at *GSI.
2383 Returns 1 if there were any changes made, 2 if cfg-cleanup needs to
2384 run. Else it returns 0. */
2385
2386 static int
2387 combine_conversions (gimple_stmt_iterator *gsi)
2388 {
2389 gimple stmt = gsi_stmt (*gsi);
2390 gimple def_stmt;
2391 tree op0, lhs;
2392 enum tree_code code = gimple_assign_rhs_code (stmt);
2393 enum tree_code code2;
2394
2395 gcc_checking_assert (CONVERT_EXPR_CODE_P (code)
2396 || code == FLOAT_EXPR
2397 || code == FIX_TRUNC_EXPR);
2398
2399 lhs = gimple_assign_lhs (stmt);
2400 op0 = gimple_assign_rhs1 (stmt);
2401 if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0)))
2402 {
2403 gimple_assign_set_rhs_code (stmt, TREE_CODE (op0));
2404 return 1;
2405 }
2406
2407 if (TREE_CODE (op0) != SSA_NAME)
2408 return 0;
2409
2410 def_stmt = SSA_NAME_DEF_STMT (op0);
2411 if (!is_gimple_assign (def_stmt))
2412 return 0;
2413
2414 code2 = gimple_assign_rhs_code (def_stmt);
2415
2416 if (CONVERT_EXPR_CODE_P (code2) || code2 == FLOAT_EXPR)
2417 {
2418 tree defop0 = gimple_assign_rhs1 (def_stmt);
2419 tree type = TREE_TYPE (lhs);
2420 tree inside_type = TREE_TYPE (defop0);
2421 tree inter_type = TREE_TYPE (op0);
2422 int inside_int = INTEGRAL_TYPE_P (inside_type);
2423 int inside_ptr = POINTER_TYPE_P (inside_type);
2424 int inside_float = FLOAT_TYPE_P (inside_type);
2425 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
2426 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2427 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2428 int inter_int = INTEGRAL_TYPE_P (inter_type);
2429 int inter_ptr = POINTER_TYPE_P (inter_type);
2430 int inter_float = FLOAT_TYPE_P (inter_type);
2431 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
2432 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2433 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2434 int final_int = INTEGRAL_TYPE_P (type);
2435 int final_ptr = POINTER_TYPE_P (type);
2436 int final_float = FLOAT_TYPE_P (type);
2437 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
2438 unsigned int final_prec = TYPE_PRECISION (type);
2439 int final_unsignedp = TYPE_UNSIGNED (type);
2440
2441 /* Don't propagate ssa names that occur in abnormal phis. */
2442 if (TREE_CODE (defop0) == SSA_NAME
2443 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defop0))
2444 return 0;
2445
2446 /* In addition to the cases of two conversions in a row
2447 handled below, if we are converting something to its own
2448 type via an object of identical or wider precision, neither
2449 conversion is needed. */
2450 if (useless_type_conversion_p (type, inside_type)
2451 && (((inter_int || inter_ptr) && final_int)
2452 || (inter_float && final_float))
2453 && inter_prec >= final_prec)
2454 {
2455 gimple_assign_set_rhs1 (stmt, unshare_expr (defop0));
2456 gimple_assign_set_rhs_code (stmt, TREE_CODE (defop0));
2457 update_stmt (stmt);
2458 return remove_prop_source_from_use (op0) ? 2 : 1;
2459 }
2460
2461 /* Likewise, if the intermediate and initial types are either both
2462 float or both integer, we don't need the middle conversion if the
2463 former is wider than the latter and doesn't change the signedness
2464 (for integers). Avoid this if the final type is a pointer since
2465 then we sometimes need the middle conversion. Likewise if the
2466 final type has a precision not equal to the size of its mode. */
2467 if (((inter_int && inside_int)
2468 || (inter_float && inside_float)
2469 || (inter_vec && inside_vec))
2470 && inter_prec >= inside_prec
2471 && (inter_float || inter_vec
2472 || inter_unsignedp == inside_unsignedp)
2473 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
2474 && TYPE_MODE (type) == TYPE_MODE (inter_type))
2475 && ! final_ptr
2476 && (! final_vec || inter_prec == inside_prec))
2477 {
2478 gimple_assign_set_rhs1 (stmt, defop0);
2479 update_stmt (stmt);
2480 return remove_prop_source_from_use (op0) ? 2 : 1;
2481 }
2482
2483 /* If we have a sign-extension of a zero-extended value, we can
2484 replace that by a single zero-extension. Likewise if the
2485 final conversion does not change precision we can drop the
2486 intermediate conversion. */
2487 if (inside_int && inter_int && final_int
2488 && ((inside_prec < inter_prec && inter_prec < final_prec
2489 && inside_unsignedp && !inter_unsignedp)
2490 || final_prec == inter_prec))
2491 {
2492 gimple_assign_set_rhs1 (stmt, defop0);
2493 update_stmt (stmt);
2494 return remove_prop_source_from_use (op0) ? 2 : 1;
2495 }
2496
2497 /* Two conversions in a row are not needed unless:
2498 - some conversion is floating-point (overstrict for now), or
2499 - some conversion is a vector (overstrict for now), or
2500 - the intermediate type is narrower than both initial and
2501 final, or
2502 - the intermediate type and innermost type differ in signedness,
2503 and the outermost type is wider than the intermediate, or
2504 - the initial type is a pointer type and the precisions of the
2505 intermediate and final types differ, or
2506 - the final type is a pointer type and the precisions of the
2507 initial and intermediate types differ. */
2508 if (! inside_float && ! inter_float && ! final_float
2509 && ! inside_vec && ! inter_vec && ! final_vec
2510 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2511 && ! (inside_int && inter_int
2512 && inter_unsignedp != inside_unsignedp
2513 && inter_prec < final_prec)
2514 && ((inter_unsignedp && inter_prec > inside_prec)
2515 == (final_unsignedp && final_prec > inter_prec))
2516 && ! (inside_ptr && inter_prec != final_prec)
2517 && ! (final_ptr && inside_prec != inter_prec)
2518 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
2519 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
2520 {
2521 gimple_assign_set_rhs1 (stmt, defop0);
2522 update_stmt (stmt);
2523 return remove_prop_source_from_use (op0) ? 2 : 1;
2524 }
2525
2526 /* A truncation to an unsigned type should be canonicalized as
2527 bitwise and of a mask. */
2528 if (final_int && inter_int && inside_int
2529 && final_prec == inside_prec
2530 && final_prec > inter_prec
2531 && inter_unsignedp)
2532 {
2533 tree tem;
2534 tem = fold_build2 (BIT_AND_EXPR, inside_type,
2535 defop0,
2536 double_int_to_tree
2537 (inside_type, double_int::mask (inter_prec)));
2538 if (!useless_type_conversion_p (type, inside_type))
2539 {
2540 tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE, true,
2541 GSI_SAME_STMT);
2542 gimple_assign_set_rhs1 (stmt, tem);
2543 }
2544 else
2545 gimple_assign_set_rhs_from_tree (gsi, tem);
2546 update_stmt (gsi_stmt (*gsi));
2547 return 1;
2548 }
2549
2550 /* If we are converting an integer to a floating-point that can
2551 represent it exactly and back to an integer, we can skip the
2552 floating-point conversion. */
2553 if (inside_int && inter_float && final_int &&
2554 (unsigned) significand_size (TYPE_MODE (inter_type))
2555 >= inside_prec - !inside_unsignedp)
2556 {
2557 if (useless_type_conversion_p (type, inside_type))
2558 {
2559 gimple_assign_set_rhs1 (stmt, unshare_expr (defop0));
2560 gimple_assign_set_rhs_code (stmt, TREE_CODE (defop0));
2561 update_stmt (stmt);
2562 return remove_prop_source_from_use (op0) ? 2 : 1;
2563 }
2564 else
2565 {
2566 gimple_assign_set_rhs1 (stmt, defop0);
2567 gimple_assign_set_rhs_code (stmt, CONVERT_EXPR);
2568 update_stmt (stmt);
2569 return remove_prop_source_from_use (op0) ? 2 : 1;
2570 }
2571 }
2572 }
2573
2574 return 0;
2575 }
2576
2577 /* Combine an element access with a shuffle. Returns true if there were
2578 any changes made, else it returns false. */
2579
2580 static bool
2581 simplify_bitfield_ref (gimple_stmt_iterator *gsi)
2582 {
2583 gimple stmt = gsi_stmt (*gsi);
2584 gimple def_stmt;
2585 tree op, op0, op1, op2;
2586 tree elem_type;
2587 unsigned idx, n, size;
2588 enum tree_code code;
2589
2590 op = gimple_assign_rhs1 (stmt);
2591 gcc_checking_assert (TREE_CODE (op) == BIT_FIELD_REF);
2592
2593 op0 = TREE_OPERAND (op, 0);
2594 if (TREE_CODE (op0) != SSA_NAME
2595 || TREE_CODE (TREE_TYPE (op0)) != VECTOR_TYPE)
2596 return false;
2597
2598 elem_type = TREE_TYPE (TREE_TYPE (op0));
2599 if (TREE_TYPE (op) != elem_type)
2600 return false;
2601
2602 size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
2603 op1 = TREE_OPERAND (op, 1);
2604 n = TREE_INT_CST_LOW (op1) / size;
2605 if (n != 1)
2606 return false;
2607
2608 def_stmt = SSA_NAME_DEF_STMT (op0);
2609 if (!def_stmt || !is_gimple_assign (def_stmt)
2610 || !can_propagate_from (def_stmt))
2611 return false;
2612
2613 op2 = TREE_OPERAND (op, 2);
2614 idx = TREE_INT_CST_LOW (op2) / size;
2615
2616 code = gimple_assign_rhs_code (def_stmt);
2617
2618 if (code == VEC_PERM_EXPR)
2619 {
2620 tree p, m, index, tem;
2621 unsigned nelts;
2622 m = gimple_assign_rhs3 (def_stmt);
2623 if (TREE_CODE (m) != VECTOR_CST)
2624 return false;
2625 nelts = VECTOR_CST_NELTS (m);
2626 idx = TREE_INT_CST_LOW (VECTOR_CST_ELT (m, idx));
2627 idx %= 2 * nelts;
2628 if (idx < nelts)
2629 {
2630 p = gimple_assign_rhs1 (def_stmt);
2631 }
2632 else
2633 {
2634 p = gimple_assign_rhs2 (def_stmt);
2635 idx -= nelts;
2636 }
2637 index = build_int_cst (TREE_TYPE (TREE_TYPE (m)), idx * size);
2638 tem = build3 (BIT_FIELD_REF, TREE_TYPE (op),
2639 unshare_expr (p), op1, index);
2640 gimple_assign_set_rhs1 (stmt, tem);
2641 fold_stmt (gsi);
2642 update_stmt (gsi_stmt (*gsi));
2643 return true;
2644 }
2645
2646 return false;
2647 }
2648
2649 /* Determine whether applying the 2 permutations (mask1 then mask2)
2650 gives back one of the input. */
2651
2652 static int
2653 is_combined_permutation_identity (tree mask1, tree mask2)
2654 {
2655 tree mask;
2656 unsigned int nelts, i, j;
2657 bool maybe_identity1 = true;
2658 bool maybe_identity2 = true;
2659
2660 gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
2661 && TREE_CODE (mask2) == VECTOR_CST);
2662 mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
2663 gcc_assert (TREE_CODE (mask) == VECTOR_CST);
2664
2665 nelts = VECTOR_CST_NELTS (mask);
2666 for (i = 0; i < nelts; i++)
2667 {
2668 tree val = VECTOR_CST_ELT (mask, i);
2669 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2670 j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
2671 if (j == i)
2672 maybe_identity2 = false;
2673 else if (j == i + nelts)
2674 maybe_identity1 = false;
2675 else
2676 return 0;
2677 }
2678 return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
2679 }
2680
2681 /* Combine a shuffle with its arguments. Returns 1 if there were any
2682 changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */
2683
2684 static int
2685 simplify_permutation (gimple_stmt_iterator *gsi)
2686 {
2687 gimple stmt = gsi_stmt (*gsi);
2688 gimple def_stmt;
2689 tree op0, op1, op2, op3, arg0, arg1;
2690 enum tree_code code;
2691
2692 gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
2693
2694 op0 = gimple_assign_rhs1 (stmt);
2695 op1 = gimple_assign_rhs2 (stmt);
2696 op2 = gimple_assign_rhs3 (stmt);
2697
2698 if (TREE_CODE (op2) != VECTOR_CST)
2699 return 0;
2700
2701 if (TREE_CODE (op0) == VECTOR_CST)
2702 {
2703 code = VECTOR_CST;
2704 arg0 = op0;
2705 }
2706 else if (TREE_CODE (op0) == SSA_NAME)
2707 {
2708 def_stmt = SSA_NAME_DEF_STMT (op0);
2709 if (!def_stmt || !is_gimple_assign (def_stmt)
2710 || !can_propagate_from (def_stmt))
2711 return 0;
2712
2713 code = gimple_assign_rhs_code (def_stmt);
2714 arg0 = gimple_assign_rhs1 (def_stmt);
2715 }
2716 else
2717 return 0;
2718
2719 /* Two consecutive shuffles. */
2720 if (code == VEC_PERM_EXPR)
2721 {
2722 tree orig;
2723 int ident;
2724
2725 if (op0 != op1)
2726 return 0;
2727 op3 = gimple_assign_rhs3 (def_stmt);
2728 if (TREE_CODE (op3) != VECTOR_CST)
2729 return 0;
2730 ident = is_combined_permutation_identity (op3, op2);
2731 if (!ident)
2732 return 0;
2733 orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
2734 : gimple_assign_rhs2 (def_stmt);
2735 gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
2736 gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
2737 gimple_set_num_ops (stmt, 2);
2738 update_stmt (stmt);
2739 return remove_prop_source_from_use (op0) ? 2 : 1;
2740 }
2741
2742 /* Shuffle of a constructor. */
2743 else if (code == CONSTRUCTOR || code == VECTOR_CST)
2744 {
2745 tree opt;
2746 bool ret = false;
2747 if (op0 != op1)
2748 {
2749 if (TREE_CODE (op0) == SSA_NAME && !has_single_use (op0))
2750 return 0;
2751
2752 if (TREE_CODE (op1) == VECTOR_CST)
2753 arg1 = op1;
2754 else if (TREE_CODE (op1) == SSA_NAME)
2755 {
2756 enum tree_code code2;
2757
2758 if (!has_single_use (op1))
2759 return 0;
2760
2761 gimple def_stmt2 = SSA_NAME_DEF_STMT (op1);
2762 if (!def_stmt2 || !is_gimple_assign (def_stmt2)
2763 || !can_propagate_from (def_stmt2))
2764 return 0;
2765
2766 code2 = gimple_assign_rhs_code (def_stmt2);
2767 if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
2768 return 0;
2769 arg1 = gimple_assign_rhs1 (def_stmt2);
2770 }
2771 else
2772 return 0;
2773 }
2774 else
2775 {
2776 /* Already used twice in this statement. */
2777 if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
2778 return 0;
2779 arg1 = arg0;
2780 }
2781 opt = fold_ternary (VEC_PERM_EXPR, TREE_TYPE(op0), arg0, arg1, op2);
2782 if (!opt
2783 || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE(opt) != VECTOR_CST))
2784 return 0;
2785 gimple_assign_set_rhs_from_tree (gsi, opt);
2786 update_stmt (gsi_stmt (*gsi));
2787 if (TREE_CODE (op0) == SSA_NAME)
2788 ret = remove_prop_source_from_use (op0);
2789 if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
2790 ret |= remove_prop_source_from_use (op1);
2791 return ret ? 2 : 1;
2792 }
2793
2794 return 0;
2795 }
2796
2797 /* Main entry point for the forward propagation and statement combine
2798 optimizer. */
2799
2800 static unsigned int
2801 ssa_forward_propagate_and_combine (void)
2802 {
2803 basic_block bb;
2804 unsigned int todoflags = 0;
2805
2806 cfg_changed = false;
2807
2808 FOR_EACH_BB (bb)
2809 {
2810 gimple_stmt_iterator gsi;
2811
2812 /* Apply forward propagation to all stmts in the basic-block.
2813 Note we update GSI within the loop as necessary. */
2814 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2815 {
2816 gimple stmt = gsi_stmt (gsi);
2817 tree lhs, rhs;
2818 enum tree_code code;
2819
2820 if (!is_gimple_assign (stmt))
2821 {
2822 gsi_next (&gsi);
2823 continue;
2824 }
2825
2826 lhs = gimple_assign_lhs (stmt);
2827 rhs = gimple_assign_rhs1 (stmt);
2828 code = gimple_assign_rhs_code (stmt);
2829 if (TREE_CODE (lhs) != SSA_NAME
2830 || has_zero_uses (lhs))
2831 {
2832 gsi_next (&gsi);
2833 continue;
2834 }
2835
2836 /* If this statement sets an SSA_NAME to an address,
2837 try to propagate the address into the uses of the SSA_NAME. */
2838 if (code == ADDR_EXPR
2839 /* Handle pointer conversions on invariant addresses
2840 as well, as this is valid gimple. */
2841 || (CONVERT_EXPR_CODE_P (code)
2842 && TREE_CODE (rhs) == ADDR_EXPR
2843 && POINTER_TYPE_P (TREE_TYPE (lhs))))
2844 {
2845 tree base = get_base_address (TREE_OPERAND (rhs, 0));
2846 if ((!base
2847 || !DECL_P (base)
2848 || decl_address_invariant_p (base))
2849 && !stmt_references_abnormal_ssa_name (stmt)
2850 && forward_propagate_addr_expr (lhs, rhs))
2851 {
2852 release_defs (stmt);
2853 todoflags |= TODO_remove_unused_locals;
2854 gsi_remove (&gsi, true);
2855 }
2856 else
2857 gsi_next (&gsi);
2858 }
2859 else if (code == POINTER_PLUS_EXPR)
2860 {
2861 tree off = gimple_assign_rhs2 (stmt);
2862 if (TREE_CODE (off) == INTEGER_CST
2863 && can_propagate_from (stmt)
2864 && !simple_iv_increment_p (stmt)
2865 /* ??? Better adjust the interface to that function
2866 instead of building new trees here. */
2867 && forward_propagate_addr_expr
2868 (lhs,
2869 build1_loc (gimple_location (stmt),
2870 ADDR_EXPR, TREE_TYPE (rhs),
2871 fold_build2 (MEM_REF,
2872 TREE_TYPE (TREE_TYPE (rhs)),
2873 rhs,
2874 fold_convert (ptr_type_node,
2875 off)))))
2876 {
2877 release_defs (stmt);
2878 todoflags |= TODO_remove_unused_locals;
2879 gsi_remove (&gsi, true);
2880 }
2881 else if (is_gimple_min_invariant (rhs))
2882 {
2883 /* Make sure to fold &a[0] + off_1 here. */
2884 fold_stmt_inplace (&gsi);
2885 update_stmt (stmt);
2886 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
2887 gsi_next (&gsi);
2888 }
2889 else
2890 gsi_next (&gsi);
2891 }
2892 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2893 {
2894 if (forward_propagate_comparison (&gsi))
2895 cfg_changed = true;
2896 }
2897 else
2898 gsi_next (&gsi);
2899 }
2900
2901 /* Combine stmts with the stmts defining their operands.
2902 Note we update GSI within the loop as necessary. */
2903 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
2904 {
2905 gimple stmt = gsi_stmt (gsi);
2906 bool changed = false;
2907
2908 /* Mark stmt as potentially needing revisiting. */
2909 gimple_set_plf (stmt, GF_PLF_1, false);
2910
2911 switch (gimple_code (stmt))
2912 {
2913 case GIMPLE_ASSIGN:
2914 {
2915 tree rhs1 = gimple_assign_rhs1 (stmt);
2916 enum tree_code code = gimple_assign_rhs_code (stmt);
2917
2918 if ((code == BIT_NOT_EXPR
2919 || code == NEGATE_EXPR)
2920 && TREE_CODE (rhs1) == SSA_NAME)
2921 changed = simplify_not_neg_expr (&gsi);
2922 else if (code == COND_EXPR
2923 || code == VEC_COND_EXPR)
2924 {
2925 /* In this case the entire COND_EXPR is in rhs1. */
2926 if (forward_propagate_into_cond (&gsi)
2927 || combine_cond_exprs (&gsi))
2928 {
2929 changed = true;
2930 stmt = gsi_stmt (gsi);
2931 }
2932 }
2933 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2934 {
2935 int did_something;
2936 did_something = forward_propagate_into_comparison (&gsi);
2937 if (did_something == 2)
2938 cfg_changed = true;
2939 changed = did_something != 0;
2940 }
2941 else if (code == BIT_AND_EXPR
2942 || code == BIT_IOR_EXPR
2943 || code == BIT_XOR_EXPR)
2944 changed = simplify_bitwise_binary (&gsi);
2945 else if (code == PLUS_EXPR
2946 || code == MINUS_EXPR)
2947 changed = associate_plusminus (&gsi);
2948 else if (code == POINTER_PLUS_EXPR)
2949 changed = associate_pointerplus (&gsi);
2950 else if (CONVERT_EXPR_CODE_P (code)
2951 || code == FLOAT_EXPR
2952 || code == FIX_TRUNC_EXPR)
2953 {
2954 int did_something = combine_conversions (&gsi);
2955 if (did_something == 2)
2956 cfg_changed = true;
2957 changed = did_something != 0;
2958 }
2959 else if (code == VEC_PERM_EXPR)
2960 {
2961 int did_something = simplify_permutation (&gsi);
2962 if (did_something == 2)
2963 cfg_changed = true;
2964 changed = did_something != 0;
2965 }
2966 else if (code == BIT_FIELD_REF)
2967 changed = simplify_bitfield_ref (&gsi);
2968 break;
2969 }
2970
2971 case GIMPLE_SWITCH:
2972 changed = simplify_gimple_switch (stmt);
2973 break;
2974
2975 case GIMPLE_COND:
2976 {
2977 int did_something;
2978 did_something = forward_propagate_into_gimple_cond (stmt);
2979 if (did_something == 2)
2980 cfg_changed = true;
2981 changed = did_something != 0;
2982 break;
2983 }
2984
2985 case GIMPLE_CALL:
2986 {
2987 tree callee = gimple_call_fndecl (stmt);
2988 if (callee != NULL_TREE
2989 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2990 changed = simplify_builtin_call (&gsi, callee);
2991 break;
2992 }
2993
2994 default:;
2995 }
2996
2997 if (changed)
2998 {
2999 /* If the stmt changed then re-visit it and the statements
3000 inserted before it. */
3001 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3002 if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
3003 break;
3004 if (gsi_end_p (gsi))
3005 gsi = gsi_start_bb (bb);
3006 else
3007 gsi_next (&gsi);
3008 }
3009 else
3010 {
3011 /* Stmt no longer needs to be revisited. */
3012 gimple_set_plf (stmt, GF_PLF_1, true);
3013 gsi_next (&gsi);
3014 }
3015 }
3016 }
3017
3018 if (cfg_changed)
3019 todoflags |= TODO_cleanup_cfg;
3020
3021 return todoflags;
3022 }
3023
3024
3025 static bool
3026 gate_forwprop (void)
3027 {
3028 return flag_tree_forwprop;
3029 }
3030
3031 struct gimple_opt_pass pass_forwprop =
3032 {
3033 {
3034 GIMPLE_PASS,
3035 "forwprop", /* name */
3036 gate_forwprop, /* gate */
3037 ssa_forward_propagate_and_combine, /* execute */
3038 NULL, /* sub */
3039 NULL, /* next */
3040 0, /* static_pass_number */
3041 TV_TREE_FORWPROP, /* tv_id */
3042 PROP_cfg | PROP_ssa, /* properties_required */
3043 0, /* properties_provided */
3044 0, /* properties_destroyed */
3045 0, /* todo_flags_start */
3046 TODO_ggc_collect
3047 | TODO_update_ssa
3048 | TODO_verify_ssa /* todo_flags_finish */
3049 }
3050 };