]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-forwprop.c
PR debug/43516
[thirdparty/gcc.git] / gcc / tree-ssa-forwprop.c
CommitLineData
291d763b 1/* Forward propagation of expressions for single use variables.
cfaf579d 2 Copyright (C) 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4ee9c684 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8c4c00c1 8the Free Software Foundation; either version 3, or (at your option)
4ee9c684 9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
4ee9c684 19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "tm.h"
4ee9c684 24#include "ggc.h"
25#include "tree.h"
26#include "rtl.h"
27#include "tm_p.h"
28#include "basic-block.h"
29#include "timevar.h"
30#include "diagnostic.h"
31#include "tree-flow.h"
32#include "tree-pass.h"
33#include "tree-dump.h"
291d763b 34#include "langhooks.h"
5adc1066 35#include "flags.h"
75a70cf9 36#include "gimple.h"
4ee9c684 37
291d763b 38/* This pass propagates the RHS of assignment statements into use
39 sites of the LHS of the assignment. It's basically a specialized
8f628ee8 40 form of tree combination. It is hoped all of this can disappear
41 when we have a generalized tree combiner.
4ee9c684 42
291d763b 43 One class of common cases we handle is forward propagating a single use
48e1416a 44 variable into a COND_EXPR.
4ee9c684 45
46 bb0:
47 x = a COND b;
48 if (x) goto ... else goto ...
49
50 Will be transformed into:
51
52 bb0:
53 if (a COND b) goto ... else goto ...
48e1416a 54
4ee9c684 55 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
56
57 Or (assuming c1 and c2 are constants):
58
59 bb0:
48e1416a 60 x = a + c1;
4ee9c684 61 if (x EQ/NEQ c2) goto ... else goto ...
62
63 Will be transformed into:
64
65 bb0:
66 if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
67
68 Similarly for x = a - c1.
48e1416a 69
4ee9c684 70 Or
71
72 bb0:
73 x = !a
74 if (x) goto ... else goto ...
75
76 Will be transformed into:
77
78 bb0:
79 if (a == 0) goto ... else goto ...
80
81 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
82 For these cases, we propagate A into all, possibly more than one,
83 COND_EXPRs that use X.
84
f5c8cff5 85 Or
86
87 bb0:
88 x = (typecast) a
89 if (x) goto ... else goto ...
90
91 Will be transformed into:
92
93 bb0:
94 if (a != 0) goto ... else goto ...
95
96 (Assuming a is an integral type and x is a boolean or x is an
97 integral and a is a boolean.)
98
99 Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
100 For these cases, we propagate A into all, possibly more than one,
101 COND_EXPRs that use X.
102
4ee9c684 103 In addition to eliminating the variable and the statement which assigns
104 a value to the variable, we may be able to later thread the jump without
e6dfde59 105 adding insane complexity in the dominator optimizer.
4ee9c684 106
f5c8cff5 107 Also note these transformations can cascade. We handle this by having
108 a worklist of COND_EXPR statements to examine. As we make a change to
109 a statement, we put it back on the worklist to examine on the next
110 iteration of the main loop.
111
291d763b 112 A second class of propagation opportunities arises for ADDR_EXPR
113 nodes.
114
115 ptr = &x->y->z;
116 res = *ptr;
117
118 Will get turned into
119
120 res = x->y->z;
121
50f39ec6 122 Or
123 ptr = (type1*)&type2var;
124 res = *ptr
125
126 Will get turned into (if type1 and type2 are the same size
127 and neither have volatile on them):
128 res = VIEW_CONVERT_EXPR<type1>(type2var)
129
291d763b 130 Or
131
132 ptr = &x[0];
133 ptr2 = ptr + <constant>;
134
135 Will get turned into
136
137 ptr2 = &x[constant/elementsize];
138
139 Or
140
141 ptr = &x[0];
142 offset = index * element_size;
143 offset_p = (pointer) offset;
144 ptr2 = ptr + offset_p
145
146 Will get turned into:
147
148 ptr2 = &x[index];
149
1c4607fd 150 Or
151 ssa = (int) decl
152 res = ssa & 1
153
154 Provided that decl has known alignment >= 2, will get turned into
155
156 res = 0
157
8f628ee8 158 We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
159 allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
160 {NOT_EXPR,NEG_EXPR}.
291d763b 161
4ee9c684 162 This will (of course) be extended as other needs arise. */
163
15ec875c 164static bool forward_propagate_addr_expr (tree name, tree rhs);
148aa112 165
166/* Set to true if we delete EH edges during the optimization. */
167static bool cfg_changed;
168
75a70cf9 169static tree rhs_to_tree (tree type, gimple stmt);
148aa112 170
83a20baf 171/* Get the next statement we can propagate NAME's value into skipping
5adc1066 172 trivial copies. Returns the statement that is suitable as a
173 propagation destination or NULL_TREE if there is no such one.
174 This only returns destinations in a single-use chain. FINAL_NAME_P
175 if non-NULL is written to the ssa name that represents the use. */
a3451973 176
75a70cf9 177static gimple
5adc1066 178get_prop_dest_stmt (tree name, tree *final_name_p)
a3451973 179{
5adc1066 180 use_operand_p use;
75a70cf9 181 gimple use_stmt;
a3451973 182
5adc1066 183 do {
184 /* If name has multiple uses, bail out. */
185 if (!single_imm_use (name, &use, &use_stmt))
75a70cf9 186 return NULL;
a3451973 187
5adc1066 188 /* If this is not a trivial copy, we found it. */
8f0b877f 189 if (!gimple_assign_ssa_name_copy_p (use_stmt)
75a70cf9 190 || gimple_assign_rhs1 (use_stmt) != name)
5adc1066 191 break;
192
193 /* Continue searching uses of the copy destination. */
75a70cf9 194 name = gimple_assign_lhs (use_stmt);
5adc1066 195 } while (1);
196
197 if (final_name_p)
198 *final_name_p = name;
199
200 return use_stmt;
a3451973 201}
202
5adc1066 203/* Get the statement we can propagate from into NAME skipping
204 trivial copies. Returns the statement which defines the
205 propagation source or NULL_TREE if there is no such one.
206 If SINGLE_USE_ONLY is set considers only sources which have
207 a single use chain up to NAME. If SINGLE_USE_P is non-null,
208 it is set to whether the chain to NAME is a single use chain
209 or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
4ee9c684 210
75a70cf9 211static gimple
5adc1066 212get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
f5c8cff5 213{
5adc1066 214 bool single_use = true;
215
216 do {
75a70cf9 217 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5adc1066 218
219 if (!has_single_use (name))
220 {
221 single_use = false;
222 if (single_use_only)
75a70cf9 223 return NULL;
5adc1066 224 }
225
226 /* If name is defined by a PHI node or is the default def, bail out. */
8f0b877f 227 if (!is_gimple_assign (def_stmt))
75a70cf9 228 return NULL;
5adc1066 229
8f0b877f 230 /* If def_stmt is not a simple copy, we possibly found it. */
231 if (!gimple_assign_ssa_name_copy_p (def_stmt))
5adc1066 232 {
b9e98b8a 233 tree rhs;
234
5adc1066 235 if (!single_use_only && single_use_p)
236 *single_use_p = single_use;
237
b9e98b8a 238 /* We can look through pointer conversions in the search
239 for a useful stmt for the comparison folding. */
75a70cf9 240 rhs = gimple_assign_rhs1 (def_stmt);
d9659041 241 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
75a70cf9 242 && TREE_CODE (rhs) == SSA_NAME
243 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (def_stmt)))
244 && POINTER_TYPE_P (TREE_TYPE (rhs)))
245 name = rhs;
b9e98b8a 246 else
247 return def_stmt;
248 }
249 else
250 {
251 /* Continue searching the def of the copy source name. */
75a70cf9 252 name = gimple_assign_rhs1 (def_stmt);
5adc1066 253 }
5adc1066 254 } while (1);
255}
e6dfde59 256
5adc1066 257/* Checks if the destination ssa name in DEF_STMT can be used as
258 propagation source. Returns true if so, otherwise false. */
e6dfde59 259
5adc1066 260static bool
75a70cf9 261can_propagate_from (gimple def_stmt)
5adc1066 262{
cc5ef3f4 263 use_operand_p use_p;
264 ssa_op_iter iter;
e6dfde59 265
75a70cf9 266 gcc_assert (is_gimple_assign (def_stmt));
8f0b877f 267
484b827b 268 /* If the rhs has side-effects we cannot propagate from it. */
75a70cf9 269 if (gimple_has_volatile_ops (def_stmt))
484b827b 270 return false;
271
272 /* If the rhs is a load we cannot propagate from it. */
75a70cf9 273 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
274 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
484b827b 275 return false;
276
b9e98b8a 277 /* Constants can be always propagated. */
8f0b877f 278 if (gimple_assign_single_p (def_stmt)
279 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
b9e98b8a 280 return true;
281
75a70cf9 282 /* We cannot propagate ssa names that occur in abnormal phi nodes. */
cc5ef3f4 283 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
284 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
5adc1066 285 return false;
4ee9c684 286
5adc1066 287 /* If the definition is a conversion of a pointer to a function type,
75a70cf9 288 then we can not apply optimizations as some targets require
289 function pointers to be canonicalized and in this case this
290 optimization could eliminate a necessary canonicalization. */
8f0b877f 291 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
75a70cf9 292 {
293 tree rhs = gimple_assign_rhs1 (def_stmt);
294 if (POINTER_TYPE_P (TREE_TYPE (rhs))
295 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs))) == FUNCTION_TYPE)
296 return false;
297 }
8f0b877f 298
5adc1066 299 return true;
e6dfde59 300}
301
5adc1066 302/* Remove a copy chain ending in NAME along the defs but not
303 further or including UP_TO_STMT. If NAME was replaced in
304 its only use then this function can be used to clean up
305 dead stmts. Returns true if UP_TO_STMT can be removed
306 as well, otherwise false. */
8f628ee8 307
5adc1066 308static bool
75a70cf9 309remove_prop_source_from_use (tree name, gimple up_to_stmt)
5adc1066 310{
75a70cf9 311 gimple_stmt_iterator gsi;
312 gimple stmt;
8f628ee8 313
5adc1066 314 do {
315 if (!has_zero_uses (name))
316 return false;
8f628ee8 317
5adc1066 318 stmt = SSA_NAME_DEF_STMT (name);
319 if (stmt == up_to_stmt)
320 return true;
8f628ee8 321
75a70cf9 322 gsi = gsi_for_stmt (stmt);
5adc1066 323 release_defs (stmt);
75a70cf9 324 gsi_remove (&gsi, true);
8f628ee8 325
75a70cf9 326 name = (gimple_assign_copy_p (stmt)) ? gimple_assign_rhs1 (stmt) : NULL;
327 } while (name && TREE_CODE (name) == SSA_NAME);
8f628ee8 328
5adc1066 329 return false;
330}
8f628ee8 331
75a70cf9 332/* Return the rhs of a gimple_assign STMT in a form of a single tree,
333 converted to type TYPE.
48e1416a 334
75a70cf9 335 This should disappear, but is needed so we can combine expressions and use
336 the fold() interfaces. Long term, we need to develop folding and combine
337 routines that deal with gimple exclusively . */
338
339static tree
340rhs_to_tree (tree type, gimple stmt)
341{
389dd41b 342 location_t loc = gimple_location (stmt);
75a70cf9 343 enum tree_code code = gimple_assign_rhs_code (stmt);
344 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
389dd41b 345 return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
fb8ed03f 346 gimple_assign_rhs2 (stmt));
75a70cf9 347 else if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
fb8ed03f 348 return build1 (code, type, gimple_assign_rhs1 (stmt));
75a70cf9 349 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
350 return gimple_assign_rhs1 (stmt);
351 else
352 gcc_unreachable ();
353}
354
5adc1066 355/* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
356 the folded result in a form suitable for COND_EXPR_COND or
357 NULL_TREE, if there is no suitable simplified form. If
358 INVARIANT_ONLY is true only gimple_min_invariant results are
359 considered simplified. */
8f628ee8 360
361static tree
389dd41b 362combine_cond_expr_cond (location_t loc, enum tree_code code, tree type,
5adc1066 363 tree op0, tree op1, bool invariant_only)
8f628ee8 364{
5adc1066 365 tree t;
8f628ee8 366
5adc1066 367 gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
8f628ee8 368
389dd41b 369 t = fold_binary_loc (loc, code, type, op0, op1);
5adc1066 370 if (!t)
371 return NULL_TREE;
8f628ee8 372
5adc1066 373 /* Require that we got a boolean type out if we put one in. */
374 gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
8f628ee8 375
a7392604 376 /* Canonicalize the combined condition for use in a COND_EXPR. */
377 t = canonicalize_cond_expr_cond (t);
8f628ee8 378
5adc1066 379 /* Bail out if we required an invariant but didn't get one. */
75a70cf9 380 if (!t || (invariant_only && !is_gimple_min_invariant (t)))
5adc1066 381 return NULL_TREE;
8f628ee8 382
a7392604 383 return t;
8f628ee8 384}
385
5adc1066 386/* Propagate from the ssa name definition statements of COND_EXPR
75a70cf9 387 in GIMPLE_COND statement STMT into the conditional if that simplifies it.
388 Returns zero if no statement was changed, one if there were
389 changes and two if cfg_cleanup needs to run.
48e1416a 390
75a70cf9 391 This must be kept in sync with forward_propagate_into_cond. */
392
393static int
394forward_propagate_into_gimple_cond (gimple stmt)
395{
389dd41b 396 int did_something = 0;
48e1416a 397 location_t loc = gimple_location (stmt);
75a70cf9 398
399 do {
400 tree tmp = NULL_TREE;
401 tree name, rhs0 = NULL_TREE, rhs1 = NULL_TREE;
402 gimple def_stmt;
403 bool single_use0_p = false, single_use1_p = false;
404 enum tree_code code = gimple_cond_code (stmt);
405
406 /* We can do tree combining on SSA_NAME and comparison expressions. */
407 if (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison
408 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME)
409 {
410 /* For comparisons use the first operand, that is likely to
411 simplify comparisons against constants. */
412 name = gimple_cond_lhs (stmt);
413 def_stmt = get_prop_source_stmt (name, false, &single_use0_p);
414 if (def_stmt && can_propagate_from (def_stmt))
415 {
416 tree op1 = gimple_cond_rhs (stmt);
417 rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
389dd41b 418 tmp = combine_cond_expr_cond (loc, code, boolean_type_node, rhs0,
75a70cf9 419 op1, !single_use0_p);
420 }
421 /* If that wasn't successful, try the second operand. */
422 if (tmp == NULL_TREE
423 && TREE_CODE (gimple_cond_rhs (stmt)) == SSA_NAME)
424 {
425 tree op0 = gimple_cond_lhs (stmt);
426 name = gimple_cond_rhs (stmt);
427 def_stmt = get_prop_source_stmt (name, false, &single_use1_p);
428 if (!def_stmt || !can_propagate_from (def_stmt))
429 return did_something;
430
431 rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
389dd41b 432 tmp = combine_cond_expr_cond (loc, code, boolean_type_node, op0,
433 rhs1, !single_use1_p);
75a70cf9 434 }
435 /* If that wasn't successful either, try both operands. */
436 if (tmp == NULL_TREE
437 && rhs0 != NULL_TREE
438 && rhs1 != NULL_TREE)
389dd41b 439 tmp = combine_cond_expr_cond (loc, code, boolean_type_node, rhs0,
440 fold_convert_loc (loc,
441 TREE_TYPE (rhs0),
442 rhs1),
75a70cf9 443 !(single_use0_p && single_use1_p));
444 }
445
446 if (tmp)
447 {
448 if (dump_file && tmp)
449 {
450 tree cond = build2 (gimple_cond_code (stmt),
451 boolean_type_node,
452 gimple_cond_lhs (stmt),
453 gimple_cond_rhs (stmt));
454 fprintf (dump_file, " Replaced '");
455 print_generic_expr (dump_file, cond, 0);
456 fprintf (dump_file, "' with '");
457 print_generic_expr (dump_file, tmp, 0);
458 fprintf (dump_file, "'\n");
459 }
460
461 gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
462 update_stmt (stmt);
463
464 /* Remove defining statements. */
465 remove_prop_source_from_use (name, NULL);
466
467 if (is_gimple_min_invariant (tmp))
468 did_something = 2;
469 else if (did_something == 0)
470 did_something = 1;
471
472 /* Continue combining. */
473 continue;
474 }
475
476 break;
477 } while (1);
478
479 return did_something;
480}
481
482
483/* Propagate from the ssa name definition statements of COND_EXPR
484 in the rhs of statement STMT into the conditional if that simplifies it.
4c580c8c 485 Returns zero if no statement was changed, one if there were
75a70cf9 486 changes and two if cfg_cleanup needs to run.
487
488 This must be kept in sync with forward_propagate_into_gimple_cond. */
4ee9c684 489
4c580c8c 490static int
75a70cf9 491forward_propagate_into_cond (gimple_stmt_iterator *gsi_p)
e6dfde59 492{
75a70cf9 493 gimple stmt = gsi_stmt (*gsi_p);
389dd41b 494 location_t loc = gimple_location (stmt);
4c580c8c 495 int did_something = 0;
d080be9e 496
5adc1066 497 do {
498 tree tmp = NULL_TREE;
75a70cf9 499 tree cond = gimple_assign_rhs1 (stmt);
500 tree name, rhs0 = NULL_TREE, rhs1 = NULL_TREE;
501 gimple def_stmt;
f4628d45 502 bool single_use0_p = false, single_use1_p = false;
5adc1066 503
504 /* We can do tree combining on SSA_NAME and comparison expressions. */
505 if (COMPARISON_CLASS_P (cond)
506 && TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME)
507 {
508 /* For comparisons use the first operand, that is likely to
509 simplify comparisons against constants. */
510 name = TREE_OPERAND (cond, 0);
f4628d45 511 def_stmt = get_prop_source_stmt (name, false, &single_use0_p);
75a70cf9 512 if (def_stmt && can_propagate_from (def_stmt))
5adc1066 513 {
514 tree op1 = TREE_OPERAND (cond, 1);
75a70cf9 515 rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
389dd41b 516 tmp = combine_cond_expr_cond (loc, TREE_CODE (cond),
517 boolean_type_node,
75a70cf9 518 rhs0, op1, !single_use0_p);
5adc1066 519 }
520 /* If that wasn't successful, try the second operand. */
521 if (tmp == NULL_TREE
522 && TREE_CODE (TREE_OPERAND (cond, 1)) == SSA_NAME)
523 {
524 tree op0 = TREE_OPERAND (cond, 0);
525 name = TREE_OPERAND (cond, 1);
f4628d45 526 def_stmt = get_prop_source_stmt (name, false, &single_use1_p);
75a70cf9 527 if (!def_stmt || !can_propagate_from (def_stmt))
d080be9e 528 return did_something;
5adc1066 529
75a70cf9 530 rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
389dd41b 531 tmp = combine_cond_expr_cond (loc, TREE_CODE (cond),
532 boolean_type_node,
75a70cf9 533 op0, rhs1, !single_use1_p);
5adc1066 534 }
484b827b 535 /* If that wasn't successful either, try both operands. */
536 if (tmp == NULL_TREE
537 && rhs0 != NULL_TREE
538 && rhs1 != NULL_TREE)
389dd41b 539 tmp = combine_cond_expr_cond (loc, TREE_CODE (cond),
540 boolean_type_node,
541 rhs0,
542 fold_convert_loc (loc,
543 TREE_TYPE (rhs0),
544 rhs1),
f4628d45 545 !(single_use0_p && single_use1_p));
5adc1066 546 }
547 else if (TREE_CODE (cond) == SSA_NAME)
548 {
549 name = cond;
550 def_stmt = get_prop_source_stmt (name, true, NULL);
75a70cf9 551 if (def_stmt || !can_propagate_from (def_stmt))
d080be9e 552 return did_something;
5adc1066 553
75a70cf9 554 rhs0 = gimple_assign_rhs1 (def_stmt);
389dd41b 555 tmp = combine_cond_expr_cond (loc, NE_EXPR, boolean_type_node, rhs0,
484b827b 556 build_int_cst (TREE_TYPE (rhs0), 0),
5adc1066 557 false);
558 }
559
560 if (tmp)
561 {
562 if (dump_file && tmp)
563 {
564 fprintf (dump_file, " Replaced '");
565 print_generic_expr (dump_file, cond, 0);
566 fprintf (dump_file, "' with '");
567 print_generic_expr (dump_file, tmp, 0);
568 fprintf (dump_file, "'\n");
569 }
570
75a70cf9 571 gimple_assign_set_rhs_from_tree (gsi_p, unshare_expr (tmp));
572 stmt = gsi_stmt (*gsi_p);
5adc1066 573 update_stmt (stmt);
574
575 /* Remove defining statements. */
576 remove_prop_source_from_use (name, NULL);
577
4c580c8c 578 if (is_gimple_min_invariant (tmp))
579 did_something = 2;
580 else if (did_something == 0)
581 did_something = 1;
d080be9e 582
5adc1066 583 /* Continue combining. */
584 continue;
585 }
586
587 break;
588 } while (1);
d080be9e 589
590 return did_something;
4ee9c684 591}
592
48e1416a 593/* We've just substituted an ADDR_EXPR into stmt. Update all the
148aa112 594 relevant data structures to match. */
595
596static void
75a70cf9 597tidy_after_forward_propagate_addr (gimple stmt)
148aa112 598{
148aa112 599 /* We may have turned a trapping insn into a non-trapping insn. */
600 if (maybe_clean_or_replace_eh_stmt (stmt, stmt)
75a70cf9 601 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
148aa112 602 cfg_changed = true;
f2fae51f 603
75a70cf9 604 if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
605 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
148aa112 606}
607
75a70cf9 608/* DEF_RHS contains the address of the 0th element in an array.
6c01267c 609 USE_STMT uses type of DEF_RHS to compute the address of an
291d763b 610 arbitrary element within the array. The (variable) byte offset
611 of the element is contained in OFFSET.
612
613 We walk back through the use-def chains of OFFSET to verify that
614 it is indeed computing the offset of an element within the array
615 and extract the index corresponding to the given byte offset.
616
617 We then try to fold the entire address expression into a form
618 &array[index].
619
620 If we are successful, we replace the right hand side of USE_STMT
621 with the new address computation. */
622
623static bool
6c01267c 624forward_propagate_addr_into_variable_array_index (tree offset,
75a70cf9 625 tree def_rhs,
626 gimple_stmt_iterator *use_stmt_gsi)
291d763b 627{
401d1fb3 628 tree index, tunit;
75a70cf9 629 gimple offset_def, use_stmt = gsi_stmt (*use_stmt_gsi);
401d1fb3 630 tree tmp;
631
632 tunit = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (def_rhs)));
633 if (!host_integerp (tunit, 1))
634 return false;
291d763b 635
65c220cd 636 /* Get the offset's defining statement. */
637 offset_def = SSA_NAME_DEF_STMT (offset);
638
639 /* Try to find an expression for a proper index. This is either a
640 multiplication expression by the element size or just the ssa name we came
641 along in case the element size is one. In that case, however, we do not
642 allow multiplications because they can be computing index to a higher
643 level dimension (PR 37861). */
401d1fb3 644 if (integer_onep (tunit))
1a773ec5 645 {
65c220cd 646 if (is_gimple_assign (offset_def)
647 && gimple_assign_rhs_code (offset_def) == MULT_EXPR)
648 return false;
291d763b 649
65c220cd 650 index = offset;
651 }
652 else
653 {
0de36bdb 654 /* The statement which defines OFFSET before type conversion
75a70cf9 655 must be a simple GIMPLE_ASSIGN. */
65c220cd 656 if (!is_gimple_assign (offset_def))
1a773ec5 657 return false;
291d763b 658
0de36bdb 659 /* The RHS of the statement which defines OFFSET must be a
48e1416a 660 multiplication of an object by the size of the array elements.
0de36bdb 661 This implicitly verifies that the size of the array elements
662 is constant. */
401d1fb3 663 if (gimple_assign_rhs_code (offset_def) == MULT_EXPR
664 && TREE_CODE (gimple_assign_rhs2 (offset_def)) == INTEGER_CST
665 && tree_int_cst_equal (gimple_assign_rhs2 (offset_def), tunit))
666 {
667 /* The first operand to the MULT_EXPR is the desired index. */
668 index = gimple_assign_rhs1 (offset_def);
669 }
670 /* If we have idx * tunit + CST * tunit re-associate that. */
671 else if ((gimple_assign_rhs_code (offset_def) == PLUS_EXPR
672 || gimple_assign_rhs_code (offset_def) == MINUS_EXPR)
673 && TREE_CODE (gimple_assign_rhs1 (offset_def)) == SSA_NAME
674 && TREE_CODE (gimple_assign_rhs2 (offset_def)) == INTEGER_CST
675 && (tmp = div_if_zero_remainder (EXACT_DIV_EXPR,
676 gimple_assign_rhs2 (offset_def),
677 tunit)) != NULL_TREE)
678 {
679 gimple offset_def2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (offset_def));
507b89a4 680 if (is_gimple_assign (offset_def2)
681 && gimple_assign_rhs_code (offset_def2) == MULT_EXPR
401d1fb3 682 && TREE_CODE (gimple_assign_rhs2 (offset_def2)) == INTEGER_CST
683 && tree_int_cst_equal (gimple_assign_rhs2 (offset_def2), tunit))
684 {
685 index = fold_build2 (gimple_assign_rhs_code (offset_def),
686 TREE_TYPE (offset),
687 gimple_assign_rhs1 (offset_def2), tmp);
688 }
689 else
690 return false;
691 }
692 else
1a773ec5 693 return false;
1a773ec5 694 }
291d763b 695
696 /* Replace the pointer addition with array indexing. */
401d1fb3 697 index = force_gimple_operand_gsi (use_stmt_gsi, index, true, NULL_TREE,
698 true, GSI_SAME_STMT);
75a70cf9 699 gimple_assign_set_rhs_from_tree (use_stmt_gsi, unshare_expr (def_rhs));
700 use_stmt = gsi_stmt (*use_stmt_gsi);
701 TREE_OPERAND (TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0), 1)
35cc02b5 702 = index;
291d763b 703
704 /* That should have created gimple, so there is no need to
705 record information to undo the propagation. */
148aa112 706 fold_stmt_inplace (use_stmt);
707 tidy_after_forward_propagate_addr (use_stmt);
291d763b 708 return true;
709}
710
15ec875c 711/* NAME is a SSA_NAME representing DEF_RHS which is of the form
712 ADDR_EXPR <whatever>.
291d763b 713
3d5cfe81 714 Try to forward propagate the ADDR_EXPR into the use USE_STMT.
291d763b 715 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
3d5cfe81 716 node or for recovery of array indexing from pointer arithmetic.
75a70cf9 717
6b5a5c42 718 Return true if the propagation was successful (the propagation can
719 be not totally successful, yet things may have been changed). */
291d763b 720
721static bool
75a70cf9 722forward_propagate_addr_expr_1 (tree name, tree def_rhs,
723 gimple_stmt_iterator *use_stmt_gsi,
6776dec8 724 bool single_use_p)
291d763b 725{
75a70cf9 726 tree lhs, rhs, rhs2, array_ref;
971c637a 727 tree *rhsp, *lhsp;
75a70cf9 728 gimple use_stmt = gsi_stmt (*use_stmt_gsi);
729 enum tree_code rhs_code;
9e019299 730 bool res = true;
291d763b 731
971c637a 732 gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
291d763b 733
75a70cf9 734 lhs = gimple_assign_lhs (use_stmt);
735 rhs_code = gimple_assign_rhs_code (use_stmt);
736 rhs = gimple_assign_rhs1 (use_stmt);
15ec875c 737
6776dec8 738 /* Trivial cases. The use statement could be a trivial copy or a
15ec875c 739 useless conversion. Recurse to the uses of the lhs as copyprop does
971c637a 740 not copy through different variant pointers and FRE does not catch
6776dec8 741 all useless conversions. Treat the case of a single-use name and
742 a conversion to def_rhs type separate, though. */
971c637a 743 if (TREE_CODE (lhs) == SSA_NAME
75a70cf9 744 && ((rhs_code == SSA_NAME && rhs == name)
316616c9 745 || CONVERT_EXPR_CODE_P (rhs_code)))
6776dec8 746 {
316616c9 747 /* Only recurse if we don't deal with a single use or we cannot
748 do the propagation to the current statement. In particular
749 we can end up with a conversion needed for a non-invariant
750 address which we cannot do in a single statement. */
751 if (!single_use_p
752 || (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))
bd8d8d81 753 && (!is_gimple_min_invariant (def_rhs)
754 || (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
755 && POINTER_TYPE_P (TREE_TYPE (def_rhs))
756 && (TYPE_PRECISION (TREE_TYPE (lhs))
757 > TYPE_PRECISION (TREE_TYPE (def_rhs)))))))
971c637a 758 return forward_propagate_addr_expr (lhs, def_rhs);
759
75a70cf9 760 gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
316616c9 761 if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
762 gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
763 else
764 gimple_assign_set_rhs_code (use_stmt, NOP_EXPR);
6776dec8 765 return true;
766 }
971c637a 767
48e1416a 768 /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
971c637a 769 ADDR_EXPR will not appear on the LHS. */
75a70cf9 770 lhsp = gimple_assign_lhs_ptr (use_stmt);
971c637a 771 while (handled_component_p (*lhsp))
772 lhsp = &TREE_OPERAND (*lhsp, 0);
773 lhs = *lhsp;
774
48e1416a 775 /* Now see if the LHS node is an INDIRECT_REF using NAME. If so,
971c637a 776 propagate the ADDR_EXPR into the use of NAME and fold the result. */
777 if (TREE_CODE (lhs) == INDIRECT_REF
9e019299 778 && TREE_OPERAND (lhs, 0) == name)
971c637a 779 {
9e019299 780 if (may_propagate_address_into_dereference (def_rhs, lhs)
781 && (lhsp != gimple_assign_lhs_ptr (use_stmt)
782 || useless_type_conversion_p
783 (TREE_TYPE (TREE_OPERAND (def_rhs, 0)), TREE_TYPE (rhs))))
784 {
785 *lhsp = unshare_expr (TREE_OPERAND (def_rhs, 0));
786 fold_stmt_inplace (use_stmt);
787 tidy_after_forward_propagate_addr (use_stmt);
788
789 /* Continue propagating into the RHS if this was not the only use. */
790 if (single_use_p)
791 return true;
792 }
793 else
794 /* We can have a struct assignment dereferencing our name twice.
795 Note that we didn't propagate into the lhs to not falsely
796 claim we did when propagating into the rhs. */
797 res = false;
971c637a 798 }
15ec875c 799
631d5db6 800 /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
801 nodes from the RHS. */
75a70cf9 802 rhsp = gimple_assign_rhs1_ptr (use_stmt);
971c637a 803 while (handled_component_p (*rhsp)
804 || TREE_CODE (*rhsp) == ADDR_EXPR)
805 rhsp = &TREE_OPERAND (*rhsp, 0);
806 rhs = *rhsp;
291d763b 807
75a70cf9 808 /* Now see if the RHS node is an INDIRECT_REF using NAME. If so,
291d763b 809 propagate the ADDR_EXPR into the use of NAME and fold the result. */
971c637a 810 if (TREE_CODE (rhs) == INDIRECT_REF
811 && TREE_OPERAND (rhs, 0) == name
fb8ed03f 812 && may_propagate_address_into_dereference (def_rhs, rhs))
291d763b 813 {
971c637a 814 *rhsp = unshare_expr (TREE_OPERAND (def_rhs, 0));
148aa112 815 fold_stmt_inplace (use_stmt);
816 tidy_after_forward_propagate_addr (use_stmt);
9e019299 817 return res;
291d763b 818 }
819
48e1416a 820 /* Now see if the RHS node is an INDIRECT_REF using NAME. If so,
50f39ec6 821 propagate the ADDR_EXPR into the use of NAME and try to
822 create a VCE and fold the result. */
823 if (TREE_CODE (rhs) == INDIRECT_REF
824 && TREE_OPERAND (rhs, 0) == name
825 && TYPE_SIZE (TREE_TYPE (rhs))
826 && TYPE_SIZE (TREE_TYPE (TREE_OPERAND (def_rhs, 0)))
37d2e64d 827 /* Function decls should not be used for VCE either as it could be a
828 function descriptor that we want and not the actual function code. */
6ec63422 829 && TREE_CODE (TREE_OPERAND (def_rhs, 0)) != FUNCTION_DECL
50f39ec6 830 /* We should not convert volatile loads to non volatile loads. */
831 && !TYPE_VOLATILE (TREE_TYPE (rhs))
832 && !TYPE_VOLATILE (TREE_TYPE (TREE_OPERAND (def_rhs, 0)))
833 && operand_equal_p (TYPE_SIZE (TREE_TYPE (rhs)),
2164d9d3 834 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (def_rhs, 0))), 0)
835 /* Make sure we only do TBAA compatible replacements. */
836 && get_alias_set (TREE_OPERAND (def_rhs, 0)) == get_alias_set (rhs))
50f39ec6 837 {
c4d6ac81 838 tree def_rhs_base, new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
37d2e64d 839 new_rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), new_rhs);
840 if (TREE_CODE (new_rhs) != VIEW_CONVERT_EXPR)
841 {
842 /* If we have folded the VIEW_CONVERT_EXPR then the result is only
843 valid if we can replace the whole rhs of the use statement. */
844 if (rhs != gimple_assign_rhs1 (use_stmt))
845 return false;
846 new_rhs = force_gimple_operand_gsi (use_stmt_gsi, new_rhs, true, NULL,
847 true, GSI_NEW_STMT);
848 gimple_assign_set_rhs1 (use_stmt, new_rhs);
c4d6ac81 849 tidy_after_forward_propagate_addr (use_stmt);
9e019299 850 return res;
37d2e64d 851 }
c4d6ac81 852 /* If the defining rhs comes from an indirect reference, then do not
853 convert into a VIEW_CONVERT_EXPR. */
854 def_rhs_base = TREE_OPERAND (def_rhs, 0);
855 while (handled_component_p (def_rhs_base))
856 def_rhs_base = TREE_OPERAND (def_rhs_base, 0);
857 if (!INDIRECT_REF_P (def_rhs_base))
37d2e64d 858 {
859 /* We may have arbitrary VIEW_CONVERT_EXPRs in a nested component
860 reference. Place it there and fold the thing. */
861 *rhsp = new_rhs;
862 fold_stmt_inplace (use_stmt);
c4d6ac81 863 tidy_after_forward_propagate_addr (use_stmt);
9e019299 864 return res;
37d2e64d 865 }
50f39ec6 866 }
867
971c637a 868 /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
869 is nothing to do. */
75a70cf9 870 if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
871 || gimple_assign_rhs1 (use_stmt) != name)
971c637a 872 return false;
873
291d763b 874 /* The remaining cases are all for turning pointer arithmetic into
875 array indexing. They only apply when we have the address of
876 element zero in an array. If that is not the case then there
877 is nothing to do. */
15ec875c 878 array_ref = TREE_OPERAND (def_rhs, 0);
291d763b 879 if (TREE_CODE (array_ref) != ARRAY_REF
880 || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
088cc5d5 881 || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
291d763b 882 return false;
883
75a70cf9 884 rhs2 = gimple_assign_rhs2 (use_stmt);
088cc5d5 885 /* Try to optimize &x[C1] p+ C2 where C2 is a multiple of the size
886 of the elements in X into &x[C1 + C2/element size]. */
75a70cf9 887 if (TREE_CODE (rhs2) == INTEGER_CST)
291d763b 888 {
e60a6f7b 889 tree new_rhs = maybe_fold_stmt_addition (gimple_location (use_stmt),
890 TREE_TYPE (def_rhs),
088cc5d5 891 def_rhs, rhs2);
75a70cf9 892 if (new_rhs)
291d763b 893 {
7b705d94 894 tree type = TREE_TYPE (gimple_assign_lhs (use_stmt));
895 new_rhs = unshare_expr (new_rhs);
896 if (!useless_type_conversion_p (type, TREE_TYPE (new_rhs)))
897 {
898 if (!is_gimple_min_invariant (new_rhs))
899 new_rhs = force_gimple_operand_gsi (use_stmt_gsi, new_rhs,
900 true, NULL_TREE,
901 true, GSI_SAME_STMT);
902 new_rhs = fold_convert (type, new_rhs);
903 }
904 gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
75a70cf9 905 use_stmt = gsi_stmt (*use_stmt_gsi);
906 update_stmt (use_stmt);
148aa112 907 tidy_after_forward_propagate_addr (use_stmt);
291d763b 908 return true;
909 }
291d763b 910 }
911
0de36bdb 912 /* Try to optimize &x[0] p+ OFFSET where OFFSET is defined by
291d763b 913 converting a multiplication of an index by the size of the
914 array elements, then the result is converted into the proper
915 type for the arithmetic. */
75a70cf9 916 if (TREE_CODE (rhs2) == SSA_NAME
088cc5d5 917 && integer_zerop (TREE_OPERAND (array_ref, 1))
c019af4d 918 && useless_type_conversion_p (TREE_TYPE (name), TREE_TYPE (def_rhs))
291d763b 919 /* Avoid problems with IVopts creating PLUS_EXPRs with a
920 different type than their operands. */
83a99d39 921 && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
75a70cf9 922 return forward_propagate_addr_into_variable_array_index (rhs2, def_rhs,
923 use_stmt_gsi);
291d763b 924 return false;
925}
926
3d5cfe81 927/* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
928
929 Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
930 Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
931 node or for recovery of array indexing from pointer arithmetic.
932 Returns true, if all uses have been propagated into. */
933
934static bool
15ec875c 935forward_propagate_addr_expr (tree name, tree rhs)
3d5cfe81 936{
75a70cf9 937 int stmt_loop_depth = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_depth;
3d5cfe81 938 imm_use_iterator iter;
75a70cf9 939 gimple use_stmt;
3d5cfe81 940 bool all = true;
6776dec8 941 bool single_use_p = has_single_use (name);
3d5cfe81 942
09aca5bc 943 FOR_EACH_IMM_USE_STMT (use_stmt, iter, name)
3d5cfe81 944 {
c96420f8 945 bool result;
9481f629 946 tree use_rhs;
3d5cfe81 947
948 /* If the use is not in a simple assignment statement, then
949 there is nothing we can do. */
75a70cf9 950 if (gimple_code (use_stmt) != GIMPLE_ASSIGN)
3d5cfe81 951 {
688ff29b 952 if (!is_gimple_debug (use_stmt))
9845d120 953 all = false;
3d5cfe81 954 continue;
955 }
956
a540e2fe 957 /* If the use is in a deeper loop nest, then we do not want
75a70cf9 958 to propagate the ADDR_EXPR into the loop as that is likely
959 adding expression evaluations into the loop. */
960 if (gimple_bb (use_stmt)->loop_depth > stmt_loop_depth)
3d5cfe81 961 {
962 all = false;
963 continue;
964 }
a540e2fe 965
75a70cf9 966 {
967 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
968 result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
969 single_use_p);
dd277d48 970 /* If the use has moved to a different statement adjust
4c5fd53c 971 the update machinery for the old statement too. */
dd277d48 972 if (use_stmt != gsi_stmt (gsi))
973 {
dd277d48 974 update_stmt (use_stmt);
4c5fd53c 975 use_stmt = gsi_stmt (gsi);
dd277d48 976 }
4c5fd53c 977
978 update_stmt (use_stmt);
75a70cf9 979 }
c96420f8 980 all &= result;
de6ed584 981
15ec875c 982 /* Remove intermediate now unused copy and conversion chains. */
75a70cf9 983 use_rhs = gimple_assign_rhs1 (use_stmt);
15ec875c 984 if (result
75a70cf9 985 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
7b705d94 986 && TREE_CODE (use_rhs) == SSA_NAME
987 && has_zero_uses (gimple_assign_lhs (use_stmt)))
15ec875c 988 {
75a70cf9 989 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
15ec875c 990 release_defs (use_stmt);
75a70cf9 991 gsi_remove (&gsi, true);
15ec875c 992 }
3d5cfe81 993 }
994
995 return all;
996}
997
75a70cf9 998/* Forward propagate the comparison defined in STMT like
5adc1066 999 cond_1 = x CMP y to uses of the form
1000 a_1 = (T')cond_1
1001 a_1 = !cond_1
1002 a_1 = cond_1 != 0
1003 Returns true if stmt is now unused. */
1004
1005static bool
75a70cf9 1006forward_propagate_comparison (gimple stmt)
5adc1066 1007{
75a70cf9 1008 tree name = gimple_assign_lhs (stmt);
1009 gimple use_stmt;
1010 tree tmp = NULL_TREE;
5adc1066 1011
1012 /* Don't propagate ssa names that occur in abnormal phis. */
75a70cf9 1013 if ((TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1014 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
1015 || (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME
1016 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt))))
5adc1066 1017 return false;
1018
1019 /* Do not un-cse comparisons. But propagate through copies. */
1020 use_stmt = get_prop_dest_stmt (name, &name);
75a70cf9 1021 if (!use_stmt)
5adc1066 1022 return false;
1023
1024 /* Conversion of the condition result to another integral type. */
75a70cf9 1025 if (is_gimple_assign (use_stmt)
d9659041 1026 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt))
75a70cf9 1027 || TREE_CODE_CLASS (gimple_assign_rhs_code (use_stmt))
1028 == tcc_comparison
1029 || gimple_assign_rhs_code (use_stmt) == TRUTH_NOT_EXPR)
1030 && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
5adc1066 1031 {
75a70cf9 1032 tree lhs = gimple_assign_lhs (use_stmt);
5adc1066 1033
1034 /* We can propagate the condition into a conversion. */
d9659041 1035 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (use_stmt)))
5adc1066 1036 {
1037 /* Avoid using fold here as that may create a COND_EXPR with
1038 non-boolean condition as canonical form. */
75a70cf9 1039 tmp = build2 (gimple_assign_rhs_code (stmt), TREE_TYPE (lhs),
1040 gimple_assign_rhs1 (stmt), gimple_assign_rhs2 (stmt));
5adc1066 1041 }
1042 /* We can propagate the condition into X op CST where op
f0b5f617 1043 is EQ_EXPR or NE_EXPR and CST is either one or zero. */
75a70cf9 1044 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (use_stmt))
1045 == tcc_comparison
1046 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
1047 && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
1048 {
1049 enum tree_code code = gimple_assign_rhs_code (use_stmt);
1050 tree cst = gimple_assign_rhs2 (use_stmt);
1051 tree cond;
1052
1053 cond = build2 (gimple_assign_rhs_code (stmt),
1054 TREE_TYPE (cst),
1055 gimple_assign_rhs1 (stmt),
1056 gimple_assign_rhs2 (stmt));
1057
389dd41b 1058 tmp = combine_cond_expr_cond (gimple_location (use_stmt),
1059 code, TREE_TYPE (lhs),
1060 cond, cst, false);
75a70cf9 1061 if (tmp == NULL_TREE)
1062 return false;
1063 }
5adc1066 1064 /* We can propagate the condition into a statement that
1065 computes the logical negation of the comparison result. */
75a70cf9 1066 else if (gimple_assign_rhs_code (use_stmt) == TRUTH_NOT_EXPR)
5adc1066 1067 {
75a70cf9 1068 tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
5adc1066 1069 bool nans = HONOR_NANS (TYPE_MODE (type));
1070 enum tree_code code;
75a70cf9 1071 code = invert_tree_comparison (gimple_assign_rhs_code (stmt), nans);
5adc1066 1072 if (code == ERROR_MARK)
1073 return false;
1074
75a70cf9 1075 tmp = build2 (code, TREE_TYPE (lhs), gimple_assign_rhs1 (stmt),
1076 gimple_assign_rhs2 (stmt));
5adc1066 1077 }
1078 else
1079 return false;
1080
75a70cf9 1081 {
1082 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1083 gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (tmp));
1084 use_stmt = gsi_stmt (gsi);
1085 update_stmt (use_stmt);
1086 }
5adc1066 1087
1088 /* Remove defining statements. */
1089 remove_prop_source_from_use (name, stmt);
1090
1091 if (dump_file && (dump_flags & TDF_DETAILS))
1092 {
75a70cf9 1093 tree old_rhs = rhs_to_tree (TREE_TYPE (gimple_assign_lhs (stmt)),
1094 stmt);
5adc1066 1095 fprintf (dump_file, " Replaced '");
75a70cf9 1096 print_generic_expr (dump_file, old_rhs, dump_flags);
5adc1066 1097 fprintf (dump_file, "' with '");
1098 print_generic_expr (dump_file, tmp, dump_flags);
1099 fprintf (dump_file, "'\n");
1100 }
1101
1102 return true;
1103 }
1104
1105 return false;
1106}
1107
3a938499 1108/* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y.
1109 If so, we can change STMT into lhs = y which can later be copy
48e1416a 1110 propagated. Similarly for negation.
3a938499 1111
48e1416a 1112 This could trivially be formulated as a forward propagation
3a938499 1113 to immediate uses. However, we already had an implementation
1114 from DOM which used backward propagation via the use-def links.
1115
1116 It turns out that backward propagation is actually faster as
1117 there's less work to do for each NOT/NEG expression we find.
1118 Backwards propagation needs to look at the statement in a single
1119 backlink. Forward propagation needs to look at potentially more
1120 than one forward link. */
1121
1122static void
75a70cf9 1123simplify_not_neg_expr (gimple_stmt_iterator *gsi_p)
3a938499 1124{
75a70cf9 1125 gimple stmt = gsi_stmt (*gsi_p);
1126 tree rhs = gimple_assign_rhs1 (stmt);
1127 gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs);
3a938499 1128
1129 /* See if the RHS_DEF_STMT has the same form as our statement. */
75a70cf9 1130 if (is_gimple_assign (rhs_def_stmt)
1131 && gimple_assign_rhs_code (rhs_def_stmt) == gimple_assign_rhs_code (stmt))
3a938499 1132 {
75a70cf9 1133 tree rhs_def_operand = gimple_assign_rhs1 (rhs_def_stmt);
3a938499 1134
1135 /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */
1136 if (TREE_CODE (rhs_def_operand) == SSA_NAME
1137 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
1138 {
75a70cf9 1139 gimple_assign_set_rhs_from_tree (gsi_p, rhs_def_operand);
1140 stmt = gsi_stmt (*gsi_p);
3a938499 1141 update_stmt (stmt);
1142 }
1143 }
1144}
3d5cfe81 1145
b5860aba 1146/* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1147 the condition which we may be able to optimize better. */
1148
1149static void
75a70cf9 1150simplify_gimple_switch (gimple stmt)
b5860aba 1151{
75a70cf9 1152 tree cond = gimple_switch_index (stmt);
b5860aba 1153 tree def, to, ti;
75a70cf9 1154 gimple def_stmt;
b5860aba 1155
1156 /* The optimization that we really care about is removing unnecessary
1157 casts. That will let us do much better in propagating the inferred
1158 constant at the switch target. */
1159 if (TREE_CODE (cond) == SSA_NAME)
1160 {
75a70cf9 1161 def_stmt = SSA_NAME_DEF_STMT (cond);
1162 if (is_gimple_assign (def_stmt))
b5860aba 1163 {
75a70cf9 1164 if (gimple_assign_rhs_code (def_stmt) == NOP_EXPR)
b5860aba 1165 {
1166 int need_precision;
1167 bool fail;
1168
75a70cf9 1169 def = gimple_assign_rhs1 (def_stmt);
b5860aba 1170
1171#ifdef ENABLE_CHECKING
1172 /* ??? Why was Jeff testing this? We are gimple... */
1173 gcc_assert (is_gimple_val (def));
1174#endif
1175
1176 to = TREE_TYPE (cond);
1177 ti = TREE_TYPE (def);
1178
1179 /* If we have an extension that preserves value, then we
1180 can copy the source value into the switch. */
1181
1182 need_precision = TYPE_PRECISION (ti);
1183 fail = false;
c5237b8b 1184 if (! INTEGRAL_TYPE_P (ti))
1185 fail = true;
1186 else if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
b5860aba 1187 fail = true;
1188 else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
1189 need_precision += 1;
1190 if (TYPE_PRECISION (to) < need_precision)
1191 fail = true;
1192
1193 if (!fail)
1194 {
75a70cf9 1195 gimple_switch_set_index (stmt, def);
b5860aba 1196 update_stmt (stmt);
1197 }
1198 }
1199 }
1200 }
1201}
1202
1c4607fd 1203/* Run bitwise and assignments throug the folder. If the first argument is an
1204 ssa name that is itself a result of a typecast of an ADDR_EXPR to an
1205 integer, feed the ADDR_EXPR to the folder rather than the ssa name.
1206*/
1207
1208static void
1209simplify_bitwise_and (gimple_stmt_iterator *gsi, gimple stmt)
1210{
1211 tree res;
1212 tree arg1 = gimple_assign_rhs1 (stmt);
1213 tree arg2 = gimple_assign_rhs2 (stmt);
1214
1215 if (TREE_CODE (arg2) != INTEGER_CST)
1216 return;
1217
1218 if (TREE_CODE (arg1) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (arg1))
1219 {
1220 gimple def = SSA_NAME_DEF_STMT (arg1);
1221
1222 if (gimple_assign_cast_p (def)
1223 && INTEGRAL_TYPE_P (gimple_expr_type (def)))
1224 {
1225 tree op = gimple_assign_rhs1 (def);
1226
1227 if (TREE_CODE (op) == ADDR_EXPR)
1228 arg1 = op;
1229 }
1230 }
1231
389dd41b 1232 res = fold_binary_loc (gimple_location (stmt),
1233 BIT_AND_EXPR, TREE_TYPE (gimple_assign_lhs (stmt)),
1c4607fd 1234 arg1, arg2);
1235 if (res && is_gimple_min_invariant (res))
1236 {
1237 gimple_assign_set_rhs_from_tree (gsi, res);
1238 update_stmt (stmt);
1239 }
1240 return;
1241}
1242
4ee9c684 1243/* Main entry point for the forward propagation optimizer. */
1244
2a1990e9 1245static unsigned int
4ee9c684 1246tree_ssa_forward_propagate_single_use_vars (void)
1247{
f5c8cff5 1248 basic_block bb;
c96420f8 1249 unsigned int todoflags = 0;
4ee9c684 1250
148aa112 1251 cfg_changed = false;
1252
f5c8cff5 1253 FOR_EACH_BB (bb)
1254 {
75a70cf9 1255 gimple_stmt_iterator gsi;
291d763b 1256
75a70cf9 1257 /* Note we update GSI within the loop as necessary. */
1258 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
291d763b 1259 {
75a70cf9 1260 gimple stmt = gsi_stmt (gsi);
291d763b 1261
1262 /* If this statement sets an SSA_NAME to an address,
1263 try to propagate the address into the uses of the SSA_NAME. */
75a70cf9 1264 if (is_gimple_assign (stmt))
291d763b 1265 {
75a70cf9 1266 tree lhs = gimple_assign_lhs (stmt);
1267 tree rhs = gimple_assign_rhs1 (stmt);
3a938499 1268
1269 if (TREE_CODE (lhs) != SSA_NAME)
1270 {
75a70cf9 1271 gsi_next (&gsi);
3a938499 1272 continue;
1273 }
1274
75a70cf9 1275 if (gimple_assign_rhs_code (stmt) == ADDR_EXPR
971c637a 1276 /* Handle pointer conversions on invariant addresses
1277 as well, as this is valid gimple. */
d9659041 1278 || (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
75a70cf9 1279 && TREE_CODE (rhs) == ADDR_EXPR
1280 && POINTER_TYPE_P (TREE_TYPE (lhs))))
3a938499 1281 {
971c637a 1282 STRIP_NOPS (rhs);
b75537fb 1283 if (!stmt_references_abnormal_ssa_name (stmt)
1284 && forward_propagate_addr_expr (lhs, rhs))
24838d3f 1285 {
1286 release_defs (stmt);
ea0ab927 1287 todoflags |= TODO_remove_unused_locals;
75a70cf9 1288 gsi_remove (&gsi, true);
24838d3f 1289 }
3a938499 1290 else
75a70cf9 1291 gsi_next (&gsi);
3a938499 1292 }
87c5de3b 1293 else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1294 && is_gimple_min_invariant (rhs))
1295 {
1296 /* Make sure to fold &a[0] + off_1 here. */
1297 fold_stmt_inplace (stmt);
1298 update_stmt (stmt);
1299 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1300 gsi_next (&gsi);
1301 }
75a70cf9 1302 else if ((gimple_assign_rhs_code (stmt) == BIT_NOT_EXPR
1303 || gimple_assign_rhs_code (stmt) == NEGATE_EXPR)
1304 && TREE_CODE (rhs) == SSA_NAME)
3a938499 1305 {
75a70cf9 1306 simplify_not_neg_expr (&gsi);
1307 gsi_next (&gsi);
3a938499 1308 }
75a70cf9 1309 else if (gimple_assign_rhs_code (stmt) == COND_EXPR)
ec0fa513 1310 {
75a70cf9 1311 /* In this case the entire COND_EXPR is in rhs1. */
4c580c8c 1312 int did_something;
d080be9e 1313 fold_defer_overflow_warnings ();
75a70cf9 1314 did_something = forward_propagate_into_cond (&gsi);
1315 stmt = gsi_stmt (gsi);
4c580c8c 1316 if (did_something == 2)
1317 cfg_changed = true;
d080be9e 1318 fold_undefer_overflow_warnings (!TREE_NO_WARNING (rhs)
1319 && did_something, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL);
75a70cf9 1320 gsi_next (&gsi);
ec0fa513 1321 }
75a70cf9 1322 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
1323 == tcc_comparison)
5adc1066 1324 {
75a70cf9 1325 if (forward_propagate_comparison (stmt))
5adc1066 1326 {
1327 release_defs (stmt);
1328 todoflags |= TODO_remove_unused_locals;
75a70cf9 1329 gsi_remove (&gsi, true);
5adc1066 1330 }
1331 else
75a70cf9 1332 gsi_next (&gsi);
5adc1066 1333 }
1c4607fd 1334 else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
1335 {
1336 simplify_bitwise_and (&gsi, stmt);
1337 gsi_next (&gsi);
1338 }
291d763b 1339 else
75a70cf9 1340 gsi_next (&gsi);
291d763b 1341 }
75a70cf9 1342 else if (gimple_code (stmt) == GIMPLE_SWITCH)
b5860aba 1343 {
75a70cf9 1344 simplify_gimple_switch (stmt);
1345 gsi_next (&gsi);
b5860aba 1346 }
75a70cf9 1347 else if (gimple_code (stmt) == GIMPLE_COND)
291d763b 1348 {
4c580c8c 1349 int did_something;
d080be9e 1350 fold_defer_overflow_warnings ();
75a70cf9 1351 did_something = forward_propagate_into_gimple_cond (stmt);
4c580c8c 1352 if (did_something == 2)
1353 cfg_changed = true;
72c59a18 1354 fold_undefer_overflow_warnings (did_something, stmt,
d080be9e 1355 WARN_STRICT_OVERFLOW_CONDITIONAL);
75a70cf9 1356 gsi_next (&gsi);
291d763b 1357 }
1358 else
75a70cf9 1359 gsi_next (&gsi);
291d763b 1360 }
f5c8cff5 1361 }
148aa112 1362
1363 if (cfg_changed)
6fa78c7b 1364 todoflags |= TODO_cleanup_cfg;
c96420f8 1365 return todoflags;
4ee9c684 1366}
1367
1368
1369static bool
1370gate_forwprop (void)
1371{
408c3c77 1372 return flag_tree_forwprop;
4ee9c684 1373}
1374
48e1416a 1375struct gimple_opt_pass pass_forwprop =
20099e35 1376{
1377 {
1378 GIMPLE_PASS,
4ee9c684 1379 "forwprop", /* name */
1380 gate_forwprop, /* gate */
1381 tree_ssa_forward_propagate_single_use_vars, /* execute */
1382 NULL, /* sub */
1383 NULL, /* next */
1384 0, /* static_pass_number */
1385 TV_TREE_FORWPROP, /* tv_id */
49290934 1386 PROP_cfg | PROP_ssa, /* properties_required */
4ee9c684 1387 0, /* properties_provided */
b6246c40 1388 0, /* properties_destroyed */
4ee9c684 1389 0, /* todo_flags_start */
de6ed584 1390 TODO_dump_func
abd433a7 1391 | TODO_ggc_collect
de6ed584 1392 | TODO_update_ssa
20099e35 1393 | TODO_verify_ssa /* todo_flags_finish */
1394 }
4ee9c684 1395};
37361b38 1396