]>
Commit | Line | Data |
---|---|---|
291d763b | 1 | /* Forward propagation of expressions for single use variables. |
3aea1f79 | 2 | Copyright (C) 2004-2014 Free Software Foundation, Inc. |
4ee9c684 | 3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8c4c00c1 | 8 | the Free Software Foundation; either version 3, or (at your option) |
4ee9c684 | 9 | any later version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 17 | along 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 "tree.h" |
9ed99284 | 25 | #include "stor-layout.h" |
4ee9c684 | 26 | #include "tm_p.h" |
94ea8568 | 27 | #include "predict.h" |
28 | #include "vec.h" | |
29 | #include "hashtab.h" | |
30 | #include "hash-set.h" | |
31 | #include "machmode.h" | |
32 | #include "hard-reg-set.h" | |
33 | #include "input.h" | |
34 | #include "function.h" | |
35 | #include "dominance.h" | |
36 | #include "cfg.h" | |
4ee9c684 | 37 | #include "basic-block.h" |
e5b1e080 | 38 | #include "gimple-pretty-print.h" |
bc61cadb | 39 | #include "tree-ssa-alias.h" |
40 | #include "internal-fn.h" | |
41 | #include "gimple-fold.h" | |
42 | #include "tree-eh.h" | |
43 | #include "gimple-expr.h" | |
44 | #include "is-a.h" | |
073c1fd5 | 45 | #include "gimple.h" |
a8783bee | 46 | #include "gimplify.h" |
dcf1a1ec | 47 | #include "gimple-iterator.h" |
e795d6e1 | 48 | #include "gimplify-me.h" |
073c1fd5 | 49 | #include "gimple-ssa.h" |
50 | #include "tree-cfg.h" | |
51 | #include "tree-phinodes.h" | |
52 | #include "ssa-iterators.h" | |
9ed99284 | 53 | #include "stringpool.h" |
073c1fd5 | 54 | #include "tree-ssanames.h" |
9ed99284 | 55 | #include "expr.h" |
073c1fd5 | 56 | #include "tree-dfa.h" |
4ee9c684 | 57 | #include "tree-pass.h" |
291d763b | 58 | #include "langhooks.h" |
5adc1066 | 59 | #include "flags.h" |
8f79c655 | 60 | #include "diagnostic.h" |
27f931ff | 61 | #include "expr.h" |
6b42039a | 62 | #include "cfgloop.h" |
d1938a4b | 63 | #include "optabs.h" |
58bf5219 | 64 | #include "tree-ssa-propagate.h" |
424a4a92 | 65 | #include "tree-ssa-dom.h" |
f7715905 | 66 | #include "builtins.h" |
f619ecae | 67 | #include "tree-cfgcleanup.h" |
68 | #include "tree-into-ssa.h" | |
94ea8568 | 69 | #include "cfganal.h" |
4ee9c684 | 70 | |
291d763b | 71 | /* This pass propagates the RHS of assignment statements into use |
72 | sites of the LHS of the assignment. It's basically a specialized | |
8f628ee8 | 73 | form of tree combination. It is hoped all of this can disappear |
74 | when we have a generalized tree combiner. | |
4ee9c684 | 75 | |
291d763b | 76 | One class of common cases we handle is forward propagating a single use |
48e1416a | 77 | variable into a COND_EXPR. |
4ee9c684 | 78 | |
79 | bb0: | |
80 | x = a COND b; | |
81 | if (x) goto ... else goto ... | |
82 | ||
83 | Will be transformed into: | |
84 | ||
85 | bb0: | |
86 | if (a COND b) goto ... else goto ... | |
48e1416a | 87 | |
4ee9c684 | 88 | Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1). |
89 | ||
90 | Or (assuming c1 and c2 are constants): | |
91 | ||
92 | bb0: | |
48e1416a | 93 | x = a + c1; |
4ee9c684 | 94 | if (x EQ/NEQ c2) goto ... else goto ... |
95 | ||
96 | Will be transformed into: | |
97 | ||
98 | bb0: | |
99 | if (a EQ/NEQ (c2 - c1)) goto ... else goto ... | |
100 | ||
101 | Similarly for x = a - c1. | |
48e1416a | 102 | |
4ee9c684 | 103 | Or |
104 | ||
105 | bb0: | |
106 | x = !a | |
107 | if (x) goto ... else goto ... | |
108 | ||
109 | Will be transformed into: | |
110 | ||
111 | bb0: | |
112 | if (a == 0) goto ... else goto ... | |
113 | ||
114 | Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1). | |
115 | For these cases, we propagate A into all, possibly more than one, | |
116 | COND_EXPRs that use X. | |
117 | ||
f5c8cff5 | 118 | Or |
119 | ||
120 | bb0: | |
121 | x = (typecast) a | |
122 | if (x) goto ... else goto ... | |
123 | ||
124 | Will be transformed into: | |
125 | ||
126 | bb0: | |
127 | if (a != 0) goto ... else goto ... | |
128 | ||
129 | (Assuming a is an integral type and x is a boolean or x is an | |
130 | integral and a is a boolean.) | |
131 | ||
132 | Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1). | |
133 | For these cases, we propagate A into all, possibly more than one, | |
134 | COND_EXPRs that use X. | |
135 | ||
4ee9c684 | 136 | In addition to eliminating the variable and the statement which assigns |
137 | a value to the variable, we may be able to later thread the jump without | |
e6dfde59 | 138 | adding insane complexity in the dominator optimizer. |
4ee9c684 | 139 | |
f5c8cff5 | 140 | Also note these transformations can cascade. We handle this by having |
141 | a worklist of COND_EXPR statements to examine. As we make a change to | |
142 | a statement, we put it back on the worklist to examine on the next | |
143 | iteration of the main loop. | |
144 | ||
291d763b | 145 | A second class of propagation opportunities arises for ADDR_EXPR |
146 | nodes. | |
147 | ||
148 | ptr = &x->y->z; | |
149 | res = *ptr; | |
150 | ||
151 | Will get turned into | |
152 | ||
153 | res = x->y->z; | |
154 | ||
50f39ec6 | 155 | Or |
156 | ptr = (type1*)&type2var; | |
157 | res = *ptr | |
158 | ||
159 | Will get turned into (if type1 and type2 are the same size | |
160 | and neither have volatile on them): | |
161 | res = VIEW_CONVERT_EXPR<type1>(type2var) | |
162 | ||
291d763b | 163 | Or |
164 | ||
165 | ptr = &x[0]; | |
166 | ptr2 = ptr + <constant>; | |
167 | ||
168 | Will get turned into | |
169 | ||
170 | ptr2 = &x[constant/elementsize]; | |
171 | ||
172 | Or | |
173 | ||
174 | ptr = &x[0]; | |
175 | offset = index * element_size; | |
176 | offset_p = (pointer) offset; | |
177 | ptr2 = ptr + offset_p | |
178 | ||
179 | Will get turned into: | |
180 | ||
181 | ptr2 = &x[index]; | |
182 | ||
1c4607fd | 183 | Or |
184 | ssa = (int) decl | |
185 | res = ssa & 1 | |
186 | ||
187 | Provided that decl has known alignment >= 2, will get turned into | |
188 | ||
189 | res = 0 | |
190 | ||
8f628ee8 | 191 | We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to |
192 | allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent | |
193 | {NOT_EXPR,NEG_EXPR}. | |
291d763b | 194 | |
4ee9c684 | 195 | This will (of course) be extended as other needs arise. */ |
196 | ||
bfb89138 | 197 | static bool forward_propagate_addr_expr (tree, tree, bool); |
148aa112 | 198 | |
b59e1c90 | 199 | /* Set to true if we delete dead edges during the optimization. */ |
148aa112 | 200 | static bool cfg_changed; |
201 | ||
75a70cf9 | 202 | static tree rhs_to_tree (tree type, gimple stmt); |
148aa112 | 203 | |
83a20baf | 204 | /* Get the next statement we can propagate NAME's value into skipping |
5adc1066 | 205 | trivial copies. Returns the statement that is suitable as a |
206 | propagation destination or NULL_TREE if there is no such one. | |
207 | This only returns destinations in a single-use chain. FINAL_NAME_P | |
208 | if non-NULL is written to the ssa name that represents the use. */ | |
a3451973 | 209 | |
75a70cf9 | 210 | static gimple |
5adc1066 | 211 | get_prop_dest_stmt (tree name, tree *final_name_p) |
a3451973 | 212 | { |
5adc1066 | 213 | use_operand_p use; |
75a70cf9 | 214 | gimple use_stmt; |
a3451973 | 215 | |
5adc1066 | 216 | do { |
217 | /* If name has multiple uses, bail out. */ | |
218 | if (!single_imm_use (name, &use, &use_stmt)) | |
75a70cf9 | 219 | return NULL; |
a3451973 | 220 | |
5adc1066 | 221 | /* If this is not a trivial copy, we found it. */ |
8f0b877f | 222 | if (!gimple_assign_ssa_name_copy_p (use_stmt) |
75a70cf9 | 223 | || gimple_assign_rhs1 (use_stmt) != name) |
5adc1066 | 224 | break; |
225 | ||
226 | /* Continue searching uses of the copy destination. */ | |
75a70cf9 | 227 | name = gimple_assign_lhs (use_stmt); |
5adc1066 | 228 | } while (1); |
229 | ||
230 | if (final_name_p) | |
231 | *final_name_p = name; | |
232 | ||
233 | return use_stmt; | |
a3451973 | 234 | } |
235 | ||
5adc1066 | 236 | /* Get the statement we can propagate from into NAME skipping |
237 | trivial copies. Returns the statement which defines the | |
238 | propagation source or NULL_TREE if there is no such one. | |
239 | If SINGLE_USE_ONLY is set considers only sources which have | |
240 | a single use chain up to NAME. If SINGLE_USE_P is non-null, | |
241 | it is set to whether the chain to NAME is a single use chain | |
242 | or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */ | |
4ee9c684 | 243 | |
75a70cf9 | 244 | static gimple |
5adc1066 | 245 | get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p) |
f5c8cff5 | 246 | { |
5adc1066 | 247 | bool single_use = true; |
248 | ||
249 | do { | |
75a70cf9 | 250 | gimple def_stmt = SSA_NAME_DEF_STMT (name); |
5adc1066 | 251 | |
252 | if (!has_single_use (name)) | |
253 | { | |
254 | single_use = false; | |
255 | if (single_use_only) | |
75a70cf9 | 256 | return NULL; |
5adc1066 | 257 | } |
258 | ||
259 | /* If name is defined by a PHI node or is the default def, bail out. */ | |
8f0b877f | 260 | if (!is_gimple_assign (def_stmt)) |
75a70cf9 | 261 | return NULL; |
5adc1066 | 262 | |
ab31ca23 | 263 | /* If def_stmt is a simple copy, continue looking. */ |
264 | if (gimple_assign_rhs_code (def_stmt) == SSA_NAME) | |
265 | name = gimple_assign_rhs1 (def_stmt); | |
266 | else | |
5adc1066 | 267 | { |
268 | if (!single_use_only && single_use_p) | |
269 | *single_use_p = single_use; | |
270 | ||
ab31ca23 | 271 | return def_stmt; |
5adc1066 | 272 | } |
5adc1066 | 273 | } while (1); |
274 | } | |
e6dfde59 | 275 | |
5adc1066 | 276 | /* Checks if the destination ssa name in DEF_STMT can be used as |
277 | propagation source. Returns true if so, otherwise false. */ | |
e6dfde59 | 278 | |
5adc1066 | 279 | static bool |
75a70cf9 | 280 | can_propagate_from (gimple def_stmt) |
5adc1066 | 281 | { |
75a70cf9 | 282 | gcc_assert (is_gimple_assign (def_stmt)); |
8f0b877f | 283 | |
484b827b | 284 | /* If the rhs has side-effects we cannot propagate from it. */ |
75a70cf9 | 285 | if (gimple_has_volatile_ops (def_stmt)) |
484b827b | 286 | return false; |
287 | ||
288 | /* If the rhs is a load we cannot propagate from it. */ | |
75a70cf9 | 289 | if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference |
290 | || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration) | |
484b827b | 291 | return false; |
292 | ||
b9e98b8a | 293 | /* Constants can be always propagated. */ |
8f0b877f | 294 | if (gimple_assign_single_p (def_stmt) |
295 | && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))) | |
b9e98b8a | 296 | return true; |
297 | ||
75a70cf9 | 298 | /* We cannot propagate ssa names that occur in abnormal phi nodes. */ |
32cdcc42 | 299 | if (stmt_references_abnormal_ssa_name (def_stmt)) |
300 | return false; | |
4ee9c684 | 301 | |
5adc1066 | 302 | /* If the definition is a conversion of a pointer to a function type, |
75a70cf9 | 303 | then we can not apply optimizations as some targets require |
304 | function pointers to be canonicalized and in this case this | |
305 | optimization could eliminate a necessary canonicalization. */ | |
8f0b877f | 306 | if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) |
75a70cf9 | 307 | { |
308 | tree rhs = gimple_assign_rhs1 (def_stmt); | |
309 | if (POINTER_TYPE_P (TREE_TYPE (rhs)) | |
310 | && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs))) == FUNCTION_TYPE) | |
311 | return false; | |
312 | } | |
8f0b877f | 313 | |
5adc1066 | 314 | return true; |
e6dfde59 | 315 | } |
316 | ||
ff0739e0 | 317 | /* Remove a chain of dead statements starting at the definition of |
318 | NAME. The chain is linked via the first operand of the defining statements. | |
5d2361b0 | 319 | If NAME was replaced in its only use then this function can be used |
ff0739e0 | 320 | to clean up dead stmts. The function handles already released SSA |
321 | names gracefully. | |
322 | Returns true if cleanup-cfg has to run. */ | |
8f628ee8 | 323 | |
5adc1066 | 324 | static bool |
5d2361b0 | 325 | remove_prop_source_from_use (tree name) |
5adc1066 | 326 | { |
75a70cf9 | 327 | gimple_stmt_iterator gsi; |
328 | gimple stmt; | |
5d2361b0 | 329 | bool cfg_changed = false; |
8f628ee8 | 330 | |
5adc1066 | 331 | do { |
5d2361b0 | 332 | basic_block bb; |
333 | ||
ff0739e0 | 334 | if (SSA_NAME_IN_FREE_LIST (name) |
335 | || SSA_NAME_IS_DEFAULT_DEF (name) | |
336 | || !has_zero_uses (name)) | |
5d2361b0 | 337 | return cfg_changed; |
8f628ee8 | 338 | |
5adc1066 | 339 | stmt = SSA_NAME_DEF_STMT (name); |
ff0739e0 | 340 | if (gimple_code (stmt) == GIMPLE_PHI |
341 | || gimple_has_side_effects (stmt)) | |
6f9714b3 | 342 | return cfg_changed; |
ff0739e0 | 343 | |
344 | bb = gimple_bb (stmt); | |
6f9714b3 | 345 | gsi = gsi_for_stmt (stmt); |
ff0739e0 | 346 | unlink_stmt_vdef (stmt); |
13ff78a4 | 347 | if (gsi_remove (&gsi, true)) |
348 | cfg_changed |= gimple_purge_dead_eh_edges (bb); | |
ff0739e0 | 349 | release_defs (stmt); |
8f628ee8 | 350 | |
ff0739e0 | 351 | name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE; |
75a70cf9 | 352 | } while (name && TREE_CODE (name) == SSA_NAME); |
8f628ee8 | 353 | |
5d2361b0 | 354 | return cfg_changed; |
5adc1066 | 355 | } |
8f628ee8 | 356 | |
75a70cf9 | 357 | /* Return the rhs of a gimple_assign STMT in a form of a single tree, |
358 | converted to type TYPE. | |
48e1416a | 359 | |
75a70cf9 | 360 | This should disappear, but is needed so we can combine expressions and use |
361 | the fold() interfaces. Long term, we need to develop folding and combine | |
362 | routines that deal with gimple exclusively . */ | |
363 | ||
364 | static tree | |
365 | rhs_to_tree (tree type, gimple stmt) | |
366 | { | |
389dd41b | 367 | location_t loc = gimple_location (stmt); |
75a70cf9 | 368 | enum tree_code code = gimple_assign_rhs_code (stmt); |
57c45d70 | 369 | if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS) |
370 | return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt), | |
371 | gimple_assign_rhs2 (stmt), | |
372 | gimple_assign_rhs3 (stmt)); | |
373 | else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS) | |
389dd41b | 374 | return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt), |
fb8ed03f | 375 | gimple_assign_rhs2 (stmt)); |
75a70cf9 | 376 | else if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS) |
fb8ed03f | 377 | return build1 (code, type, gimple_assign_rhs1 (stmt)); |
75a70cf9 | 378 | else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) |
379 | return gimple_assign_rhs1 (stmt); | |
380 | else | |
381 | gcc_unreachable (); | |
382 | } | |
383 | ||
5adc1066 | 384 | /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns |
385 | the folded result in a form suitable for COND_EXPR_COND or | |
386 | NULL_TREE, if there is no suitable simplified form. If | |
387 | INVARIANT_ONLY is true only gimple_min_invariant results are | |
388 | considered simplified. */ | |
8f628ee8 | 389 | |
390 | static tree | |
c73fee76 | 391 | combine_cond_expr_cond (gimple stmt, enum tree_code code, tree type, |
5adc1066 | 392 | tree op0, tree op1, bool invariant_only) |
8f628ee8 | 393 | { |
5adc1066 | 394 | tree t; |
8f628ee8 | 395 | |
5adc1066 | 396 | gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison); |
8f628ee8 | 397 | |
c73fee76 | 398 | fold_defer_overflow_warnings (); |
399 | t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1); | |
5adc1066 | 400 | if (!t) |
c73fee76 | 401 | { |
402 | fold_undefer_overflow_warnings (false, NULL, 0); | |
403 | return NULL_TREE; | |
404 | } | |
8f628ee8 | 405 | |
5adc1066 | 406 | /* Require that we got a boolean type out if we put one in. */ |
407 | gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type)); | |
8f628ee8 | 408 | |
a7392604 | 409 | /* Canonicalize the combined condition for use in a COND_EXPR. */ |
410 | t = canonicalize_cond_expr_cond (t); | |
8f628ee8 | 411 | |
5adc1066 | 412 | /* Bail out if we required an invariant but didn't get one. */ |
75a70cf9 | 413 | if (!t || (invariant_only && !is_gimple_min_invariant (t))) |
c73fee76 | 414 | { |
415 | fold_undefer_overflow_warnings (false, NULL, 0); | |
416 | return NULL_TREE; | |
417 | } | |
418 | ||
419 | fold_undefer_overflow_warnings (!gimple_no_warning_p (stmt), stmt, 0); | |
8f628ee8 | 420 | |
a7392604 | 421 | return t; |
8f628ee8 | 422 | } |
423 | ||
c8126d25 | 424 | /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements |
425 | of its operand. Return a new comparison tree or NULL_TREE if there | |
426 | were no simplifying combines. */ | |
427 | ||
428 | static tree | |
c73fee76 | 429 | forward_propagate_into_comparison_1 (gimple stmt, |
678b2f5b | 430 | enum tree_code code, tree type, |
431 | tree op0, tree op1) | |
c8126d25 | 432 | { |
433 | tree tmp = NULL_TREE; | |
434 | tree rhs0 = NULL_TREE, rhs1 = NULL_TREE; | |
435 | bool single_use0_p = false, single_use1_p = false; | |
436 | ||
437 | /* For comparisons use the first operand, that is likely to | |
438 | simplify comparisons against constants. */ | |
439 | if (TREE_CODE (op0) == SSA_NAME) | |
440 | { | |
441 | gimple def_stmt = get_prop_source_stmt (op0, false, &single_use0_p); | |
442 | if (def_stmt && can_propagate_from (def_stmt)) | |
443 | { | |
444 | rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt); | |
c73fee76 | 445 | tmp = combine_cond_expr_cond (stmt, code, type, |
c8126d25 | 446 | rhs0, op1, !single_use0_p); |
447 | if (tmp) | |
448 | return tmp; | |
449 | } | |
450 | } | |
451 | ||
452 | /* If that wasn't successful, try the second operand. */ | |
453 | if (TREE_CODE (op1) == SSA_NAME) | |
454 | { | |
455 | gimple def_stmt = get_prop_source_stmt (op1, false, &single_use1_p); | |
456 | if (def_stmt && can_propagate_from (def_stmt)) | |
457 | { | |
458 | rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt); | |
c73fee76 | 459 | tmp = combine_cond_expr_cond (stmt, code, type, |
c8126d25 | 460 | op0, rhs1, !single_use1_p); |
461 | if (tmp) | |
462 | return tmp; | |
463 | } | |
464 | } | |
465 | ||
466 | /* If that wasn't successful either, try both operands. */ | |
467 | if (rhs0 != NULL_TREE | |
468 | && rhs1 != NULL_TREE) | |
c73fee76 | 469 | tmp = combine_cond_expr_cond (stmt, code, type, |
c8126d25 | 470 | rhs0, rhs1, |
471 | !(single_use0_p && single_use1_p)); | |
472 | ||
473 | return tmp; | |
474 | } | |
475 | ||
678b2f5b | 476 | /* Propagate from the ssa name definition statements of the assignment |
477 | from a comparison at *GSI into the conditional if that simplifies it. | |
6f9714b3 | 478 | Returns 1 if the stmt was modified and 2 if the CFG needs cleanup, |
479 | otherwise returns 0. */ | |
c8126d25 | 480 | |
6f9714b3 | 481 | static int |
678b2f5b | 482 | forward_propagate_into_comparison (gimple_stmt_iterator *gsi) |
c8126d25 | 483 | { |
678b2f5b | 484 | gimple stmt = gsi_stmt (*gsi); |
485 | tree tmp; | |
6f9714b3 | 486 | bool cfg_changed = false; |
56632de0 | 487 | tree type = TREE_TYPE (gimple_assign_lhs (stmt)); |
6f9714b3 | 488 | tree rhs1 = gimple_assign_rhs1 (stmt); |
489 | tree rhs2 = gimple_assign_rhs2 (stmt); | |
c8126d25 | 490 | |
491 | /* Combine the comparison with defining statements. */ | |
c73fee76 | 492 | tmp = forward_propagate_into_comparison_1 (stmt, |
678b2f5b | 493 | gimple_assign_rhs_code (stmt), |
56632de0 | 494 | type, rhs1, rhs2); |
495 | if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp))) | |
c8126d25 | 496 | { |
678b2f5b | 497 | gimple_assign_set_rhs_from_tree (gsi, tmp); |
50aacf4c | 498 | fold_stmt (gsi); |
499 | update_stmt (gsi_stmt (*gsi)); | |
75200312 | 500 | |
6f9714b3 | 501 | if (TREE_CODE (rhs1) == SSA_NAME) |
502 | cfg_changed |= remove_prop_source_from_use (rhs1); | |
503 | if (TREE_CODE (rhs2) == SSA_NAME) | |
504 | cfg_changed |= remove_prop_source_from_use (rhs2); | |
505 | return cfg_changed ? 2 : 1; | |
c8126d25 | 506 | } |
507 | ||
6f9714b3 | 508 | return 0; |
c8126d25 | 509 | } |
510 | ||
5adc1066 | 511 | /* Propagate from the ssa name definition statements of COND_EXPR |
75a70cf9 | 512 | in GIMPLE_COND statement STMT into the conditional if that simplifies it. |
513 | Returns zero if no statement was changed, one if there were | |
514 | changes and two if cfg_cleanup needs to run. | |
48e1416a | 515 | |
75a70cf9 | 516 | This must be kept in sync with forward_propagate_into_cond. */ |
517 | ||
518 | static int | |
519 | forward_propagate_into_gimple_cond (gimple stmt) | |
520 | { | |
678b2f5b | 521 | tree tmp; |
522 | enum tree_code code = gimple_cond_code (stmt); | |
6f9714b3 | 523 | bool cfg_changed = false; |
524 | tree rhs1 = gimple_cond_lhs (stmt); | |
525 | tree rhs2 = gimple_cond_rhs (stmt); | |
678b2f5b | 526 | |
527 | /* We can do tree combining on SSA_NAME and comparison expressions. */ | |
528 | if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison) | |
529 | return 0; | |
530 | ||
c73fee76 | 531 | tmp = forward_propagate_into_comparison_1 (stmt, code, |
678b2f5b | 532 | boolean_type_node, |
6f9714b3 | 533 | rhs1, rhs2); |
678b2f5b | 534 | if (tmp) |
535 | { | |
536 | if (dump_file && tmp) | |
537 | { | |
678b2f5b | 538 | fprintf (dump_file, " Replaced '"); |
6f9714b3 | 539 | print_gimple_expr (dump_file, stmt, 0, 0); |
678b2f5b | 540 | fprintf (dump_file, "' with '"); |
541 | print_generic_expr (dump_file, tmp, 0); | |
542 | fprintf (dump_file, "'\n"); | |
543 | } | |
75a70cf9 | 544 | |
678b2f5b | 545 | gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp)); |
546 | update_stmt (stmt); | |
75a70cf9 | 547 | |
6f9714b3 | 548 | if (TREE_CODE (rhs1) == SSA_NAME) |
549 | cfg_changed |= remove_prop_source_from_use (rhs1); | |
550 | if (TREE_CODE (rhs2) == SSA_NAME) | |
551 | cfg_changed |= remove_prop_source_from_use (rhs2); | |
552 | return (cfg_changed || is_gimple_min_invariant (tmp)) ? 2 : 1; | |
678b2f5b | 553 | } |
75a70cf9 | 554 | |
10a6edd6 | 555 | /* Canonicalize _Bool == 0 and _Bool != 1 to _Bool != 0 by swapping edges. */ |
556 | if ((TREE_CODE (TREE_TYPE (rhs1)) == BOOLEAN_TYPE | |
557 | || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) | |
558 | && TYPE_PRECISION (TREE_TYPE (rhs1)) == 1)) | |
559 | && ((code == EQ_EXPR | |
560 | && integer_zerop (rhs2)) | |
561 | || (code == NE_EXPR | |
562 | && integer_onep (rhs2)))) | |
563 | { | |
564 | basic_block bb = gimple_bb (stmt); | |
565 | gimple_cond_set_code (stmt, NE_EXPR); | |
566 | gimple_cond_set_rhs (stmt, build_zero_cst (TREE_TYPE (rhs1))); | |
567 | EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); | |
568 | EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE); | |
569 | return 1; | |
570 | } | |
571 | ||
6f9714b3 | 572 | return 0; |
75a70cf9 | 573 | } |
574 | ||
575 | ||
576 | /* Propagate from the ssa name definition statements of COND_EXPR | |
577 | in the rhs of statement STMT into the conditional if that simplifies it. | |
8a2caf10 | 578 | Returns true zero if the stmt was changed. */ |
4ee9c684 | 579 | |
8a2caf10 | 580 | static bool |
75a70cf9 | 581 | forward_propagate_into_cond (gimple_stmt_iterator *gsi_p) |
e6dfde59 | 582 | { |
75a70cf9 | 583 | gimple stmt = gsi_stmt (*gsi_p); |
678b2f5b | 584 | tree tmp = NULL_TREE; |
585 | tree cond = gimple_assign_rhs1 (stmt); | |
def3cb70 | 586 | enum tree_code code = gimple_assign_rhs_code (stmt); |
10a6edd6 | 587 | bool swap = false; |
d080be9e | 588 | |
678b2f5b | 589 | /* We can do tree combining on SSA_NAME and comparison expressions. */ |
590 | if (COMPARISON_CLASS_P (cond)) | |
c73fee76 | 591 | tmp = forward_propagate_into_comparison_1 (stmt, TREE_CODE (cond), |
f2c1848b | 592 | TREE_TYPE (cond), |
c8126d25 | 593 | TREE_OPERAND (cond, 0), |
594 | TREE_OPERAND (cond, 1)); | |
678b2f5b | 595 | else if (TREE_CODE (cond) == SSA_NAME) |
596 | { | |
def3cb70 | 597 | enum tree_code def_code; |
8a2caf10 | 598 | tree name = cond; |
678b2f5b | 599 | gimple def_stmt = get_prop_source_stmt (name, true, NULL); |
600 | if (!def_stmt || !can_propagate_from (def_stmt)) | |
6f9714b3 | 601 | return 0; |
5adc1066 | 602 | |
def3cb70 | 603 | def_code = gimple_assign_rhs_code (def_stmt); |
604 | if (TREE_CODE_CLASS (def_code) == tcc_comparison) | |
8a2caf10 | 605 | tmp = fold_build2_loc (gimple_location (def_stmt), |
def3cb70 | 606 | def_code, |
bc112f18 | 607 | TREE_TYPE (cond), |
8a2caf10 | 608 | gimple_assign_rhs1 (def_stmt), |
609 | gimple_assign_rhs2 (def_stmt)); | |
def3cb70 | 610 | else if (code == COND_EXPR |
611 | && ((def_code == BIT_NOT_EXPR | |
612 | && TYPE_PRECISION (TREE_TYPE (cond)) == 1) | |
613 | || (def_code == BIT_XOR_EXPR | |
614 | && integer_onep (gimple_assign_rhs2 (def_stmt))))) | |
10a6edd6 | 615 | { |
616 | tmp = gimple_assign_rhs1 (def_stmt); | |
617 | swap = true; | |
618 | } | |
678b2f5b | 619 | } |
5adc1066 | 620 | |
25f48be0 | 621 | if (tmp |
622 | && is_gimple_condexpr (tmp)) | |
678b2f5b | 623 | { |
624 | if (dump_file && tmp) | |
625 | { | |
626 | fprintf (dump_file, " Replaced '"); | |
627 | print_generic_expr (dump_file, cond, 0); | |
628 | fprintf (dump_file, "' with '"); | |
629 | print_generic_expr (dump_file, tmp, 0); | |
630 | fprintf (dump_file, "'\n"); | |
631 | } | |
d080be9e | 632 | |
def3cb70 | 633 | if ((code == VEC_COND_EXPR) ? integer_all_onesp (tmp) |
634 | : integer_onep (tmp)) | |
8a2caf10 | 635 | gimple_assign_set_rhs_from_tree (gsi_p, gimple_assign_rhs2 (stmt)); |
636 | else if (integer_zerop (tmp)) | |
637 | gimple_assign_set_rhs_from_tree (gsi_p, gimple_assign_rhs3 (stmt)); | |
638 | else | |
10a6edd6 | 639 | { |
640 | gimple_assign_set_rhs1 (stmt, unshare_expr (tmp)); | |
641 | if (swap) | |
642 | { | |
643 | tree t = gimple_assign_rhs2 (stmt); | |
644 | gimple_assign_set_rhs2 (stmt, gimple_assign_rhs3 (stmt)); | |
645 | gimple_assign_set_rhs3 (stmt, t); | |
646 | } | |
647 | } | |
678b2f5b | 648 | stmt = gsi_stmt (*gsi_p); |
649 | update_stmt (stmt); | |
5adc1066 | 650 | |
8a2caf10 | 651 | return true; |
678b2f5b | 652 | } |
d080be9e | 653 | |
6f9714b3 | 654 | return 0; |
4ee9c684 | 655 | } |
656 | ||
360b78f3 | 657 | /* Propagate from the ssa name definition statements of COND_EXPR |
658 | values in the rhs of statement STMT into the conditional arms | |
659 | if that simplifies it. | |
660 | Returns true if the stmt was changed. */ | |
661 | ||
662 | static bool | |
663 | combine_cond_exprs (gimple_stmt_iterator *gsi_p) | |
664 | { | |
665 | gimple stmt = gsi_stmt (*gsi_p); | |
666 | tree cond, val1, val2; | |
667 | bool changed = false; | |
668 | ||
669 | cond = gimple_assign_rhs1 (stmt); | |
670 | val1 = gimple_assign_rhs2 (stmt); | |
671 | if (TREE_CODE (val1) == SSA_NAME) | |
672 | { | |
673 | gimple def_stmt = SSA_NAME_DEF_STMT (val1); | |
674 | if (is_gimple_assign (def_stmt) | |
675 | && gimple_assign_rhs_code (def_stmt) == gimple_assign_rhs_code (stmt) | |
676 | && operand_equal_p (gimple_assign_rhs1 (def_stmt), cond, 0)) | |
677 | { | |
678 | val1 = unshare_expr (gimple_assign_rhs2 (def_stmt)); | |
679 | gimple_assign_set_rhs2 (stmt, val1); | |
680 | changed = true; | |
681 | } | |
682 | } | |
683 | val2 = gimple_assign_rhs3 (stmt); | |
684 | if (TREE_CODE (val2) == SSA_NAME) | |
685 | { | |
686 | gimple def_stmt = SSA_NAME_DEF_STMT (val2); | |
687 | if (is_gimple_assign (def_stmt) | |
688 | && gimple_assign_rhs_code (def_stmt) == gimple_assign_rhs_code (stmt) | |
689 | && operand_equal_p (gimple_assign_rhs1 (def_stmt), cond, 0)) | |
690 | { | |
691 | val2 = unshare_expr (gimple_assign_rhs3 (def_stmt)); | |
692 | gimple_assign_set_rhs3 (stmt, val2); | |
693 | changed = true; | |
694 | } | |
695 | } | |
696 | if (operand_equal_p (val1, val2, 0)) | |
697 | { | |
698 | gimple_assign_set_rhs_from_tree (gsi_p, val1); | |
699 | stmt = gsi_stmt (*gsi_p); | |
700 | changed = true; | |
701 | } | |
702 | ||
703 | if (changed) | |
704 | update_stmt (stmt); | |
705 | ||
706 | return changed; | |
707 | } | |
708 | ||
48e1416a | 709 | /* We've just substituted an ADDR_EXPR into stmt. Update all the |
148aa112 | 710 | relevant data structures to match. */ |
711 | ||
712 | static void | |
75a70cf9 | 713 | tidy_after_forward_propagate_addr (gimple stmt) |
148aa112 | 714 | { |
148aa112 | 715 | /* We may have turned a trapping insn into a non-trapping insn. */ |
716 | if (maybe_clean_or_replace_eh_stmt (stmt, stmt) | |
75a70cf9 | 717 | && gimple_purge_dead_eh_edges (gimple_bb (stmt))) |
148aa112 | 718 | cfg_changed = true; |
f2fae51f | 719 | |
75a70cf9 | 720 | if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR) |
721 | recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt)); | |
148aa112 | 722 | } |
723 | ||
15ec875c | 724 | /* NAME is a SSA_NAME representing DEF_RHS which is of the form |
725 | ADDR_EXPR <whatever>. | |
291d763b | 726 | |
3d5cfe81 | 727 | Try to forward propagate the ADDR_EXPR into the use USE_STMT. |
291d763b | 728 | Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF |
3d5cfe81 | 729 | node or for recovery of array indexing from pointer arithmetic. |
75a70cf9 | 730 | |
6b5a5c42 | 731 | Return true if the propagation was successful (the propagation can |
732 | be not totally successful, yet things may have been changed). */ | |
291d763b | 733 | |
734 | static bool | |
75a70cf9 | 735 | forward_propagate_addr_expr_1 (tree name, tree def_rhs, |
736 | gimple_stmt_iterator *use_stmt_gsi, | |
6776dec8 | 737 | bool single_use_p) |
291d763b | 738 | { |
75a70cf9 | 739 | tree lhs, rhs, rhs2, array_ref; |
75a70cf9 | 740 | gimple use_stmt = gsi_stmt (*use_stmt_gsi); |
741 | enum tree_code rhs_code; | |
9e019299 | 742 | bool res = true; |
291d763b | 743 | |
971c637a | 744 | gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR); |
291d763b | 745 | |
75a70cf9 | 746 | lhs = gimple_assign_lhs (use_stmt); |
747 | rhs_code = gimple_assign_rhs_code (use_stmt); | |
748 | rhs = gimple_assign_rhs1 (use_stmt); | |
15ec875c | 749 | |
bfb89138 | 750 | /* Do not perform copy-propagation but recurse through copy chains. */ |
751 | if (TREE_CODE (lhs) == SSA_NAME | |
752 | && rhs_code == SSA_NAME) | |
753 | return forward_propagate_addr_expr (lhs, def_rhs, single_use_p); | |
754 | ||
755 | /* The use statement could be a conversion. Recurse to the uses of the | |
756 | lhs as copyprop does not copy through pointer to integer to pointer | |
757 | conversions and FRE does not catch all cases either. | |
758 | Treat the case of a single-use name and | |
6776dec8 | 759 | a conversion to def_rhs type separate, though. */ |
971c637a | 760 | if (TREE_CODE (lhs) == SSA_NAME |
bfb89138 | 761 | && CONVERT_EXPR_CODE_P (rhs_code)) |
6776dec8 | 762 | { |
bfb89138 | 763 | /* If there is a point in a conversion chain where the types match |
764 | so we can remove a conversion re-materialize the address here | |
765 | and stop. */ | |
766 | if (single_use_p | |
767 | && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs))) | |
768 | { | |
769 | gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs)); | |
770 | gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs)); | |
771 | return true; | |
772 | } | |
773 | ||
774 | /* Else recurse if the conversion preserves the address value. */ | |
775 | if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs)) | |
776 | || POINTER_TYPE_P (TREE_TYPE (lhs))) | |
777 | && (TYPE_PRECISION (TREE_TYPE (lhs)) | |
778 | >= TYPE_PRECISION (TREE_TYPE (def_rhs)))) | |
779 | return forward_propagate_addr_expr (lhs, def_rhs, single_use_p); | |
780 | ||
781 | return false; | |
6776dec8 | 782 | } |
971c637a | 783 | |
bfb89138 | 784 | /* If this isn't a conversion chain from this on we only can propagate |
785 | into compatible pointer contexts. */ | |
786 | if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs))) | |
787 | return false; | |
788 | ||
182cf5a9 | 789 | /* Propagate through constant pointer adjustments. */ |
790 | if (TREE_CODE (lhs) == SSA_NAME | |
791 | && rhs_code == POINTER_PLUS_EXPR | |
792 | && rhs == name | |
793 | && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST) | |
794 | { | |
795 | tree new_def_rhs; | |
796 | /* As we come here with non-invariant addresses in def_rhs we need | |
797 | to make sure we can build a valid constant offsetted address | |
798 | for further propagation. Simply rely on fold building that | |
799 | and check after the fact. */ | |
800 | new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)), | |
801 | def_rhs, | |
802 | fold_convert (ptr_type_node, | |
803 | gimple_assign_rhs2 (use_stmt))); | |
804 | if (TREE_CODE (new_def_rhs) == MEM_REF | |
f5d03f27 | 805 | && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0))) |
182cf5a9 | 806 | return false; |
807 | new_def_rhs = build_fold_addr_expr_with_type (new_def_rhs, | |
808 | TREE_TYPE (rhs)); | |
809 | ||
810 | /* Recurse. If we could propagate into all uses of lhs do not | |
811 | bother to replace into the current use but just pretend we did. */ | |
812 | if (TREE_CODE (new_def_rhs) == ADDR_EXPR | |
bfb89138 | 813 | && forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p)) |
182cf5a9 | 814 | return true; |
815 | ||
816 | if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (new_def_rhs))) | |
817 | gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs), | |
818 | new_def_rhs, NULL_TREE); | |
819 | else if (is_gimple_min_invariant (new_def_rhs)) | |
820 | gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, | |
821 | new_def_rhs, NULL_TREE); | |
822 | else | |
823 | return false; | |
824 | gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); | |
825 | update_stmt (use_stmt); | |
826 | return true; | |
827 | } | |
828 | ||
48e1416a | 829 | /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS. |
971c637a | 830 | ADDR_EXPR will not appear on the LHS. */ |
d0d1ecb8 | 831 | tree *lhsp = gimple_assign_lhs_ptr (use_stmt); |
832 | while (handled_component_p (*lhsp)) | |
833 | lhsp = &TREE_OPERAND (*lhsp, 0); | |
834 | lhs = *lhsp; | |
971c637a | 835 | |
182cf5a9 | 836 | /* Now see if the LHS node is a MEM_REF using NAME. If so, |
971c637a | 837 | propagate the ADDR_EXPR into the use of NAME and fold the result. */ |
182cf5a9 | 838 | if (TREE_CODE (lhs) == MEM_REF |
9e019299 | 839 | && TREE_OPERAND (lhs, 0) == name) |
971c637a | 840 | { |
182cf5a9 | 841 | tree def_rhs_base; |
842 | HOST_WIDE_INT def_rhs_offset; | |
843 | /* If the address is invariant we can always fold it. */ | |
844 | if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), | |
845 | &def_rhs_offset))) | |
9e019299 | 846 | { |
5de9d3ed | 847 | offset_int off = mem_ref_offset (lhs); |
182cf5a9 | 848 | tree new_ptr; |
e913b5cd | 849 | off += def_rhs_offset; |
182cf5a9 | 850 | if (TREE_CODE (def_rhs_base) == MEM_REF) |
851 | { | |
cf8f0e63 | 852 | off += mem_ref_offset (def_rhs_base); |
182cf5a9 | 853 | new_ptr = TREE_OPERAND (def_rhs_base, 0); |
854 | } | |
855 | else | |
856 | new_ptr = build_fold_addr_expr (def_rhs_base); | |
857 | TREE_OPERAND (lhs, 0) = new_ptr; | |
858 | TREE_OPERAND (lhs, 1) | |
e913b5cd | 859 | = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off); |
9e019299 | 860 | tidy_after_forward_propagate_addr (use_stmt); |
9e019299 | 861 | /* Continue propagating into the RHS if this was not the only use. */ |
862 | if (single_use_p) | |
863 | return true; | |
864 | } | |
182cf5a9 | 865 | /* If the LHS is a plain dereference and the value type is the same as |
866 | that of the pointed-to type of the address we can put the | |
867 | dereferenced address on the LHS preserving the original alias-type. */ | |
d0d1ecb8 | 868 | else if (integer_zerop (TREE_OPERAND (lhs, 1)) |
869 | && ((gimple_assign_lhs (use_stmt) == lhs | |
870 | && useless_type_conversion_p | |
871 | (TREE_TYPE (TREE_OPERAND (def_rhs, 0)), | |
872 | TREE_TYPE (gimple_assign_rhs1 (use_stmt)))) | |
873 | || types_compatible_p (TREE_TYPE (lhs), | |
874 | TREE_TYPE (TREE_OPERAND (def_rhs, 0)))) | |
f6e2e4ff | 875 | /* Don't forward anything into clobber stmts if it would result |
876 | in the lhs no longer being a MEM_REF. */ | |
877 | && (!gimple_clobber_p (use_stmt) | |
878 | || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF)) | |
182cf5a9 | 879 | { |
880 | tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0); | |
98d96c6f | 881 | tree new_offset, new_base, saved, new_lhs; |
182cf5a9 | 882 | while (handled_component_p (*def_rhs_basep)) |
883 | def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0); | |
884 | saved = *def_rhs_basep; | |
885 | if (TREE_CODE (*def_rhs_basep) == MEM_REF) | |
886 | { | |
887 | new_base = TREE_OPERAND (*def_rhs_basep, 0); | |
b97e39a0 | 888 | new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)), |
889 | TREE_OPERAND (*def_rhs_basep, 1)); | |
182cf5a9 | 890 | } |
891 | else | |
892 | { | |
893 | new_base = build_fold_addr_expr (*def_rhs_basep); | |
894 | new_offset = TREE_OPERAND (lhs, 1); | |
895 | } | |
896 | *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep), | |
897 | new_base, new_offset); | |
2e5dc41c | 898 | TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs); |
31fa5b0d | 899 | TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs); |
2e5dc41c | 900 | TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs); |
98d96c6f | 901 | new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0)); |
d0d1ecb8 | 902 | *lhsp = new_lhs; |
98d96c6f | 903 | TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs); |
31fa5b0d | 904 | TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs); |
182cf5a9 | 905 | *def_rhs_basep = saved; |
906 | tidy_after_forward_propagate_addr (use_stmt); | |
907 | /* Continue propagating into the RHS if this was not the | |
908 | only use. */ | |
909 | if (single_use_p) | |
910 | return true; | |
911 | } | |
9e019299 | 912 | else |
913 | /* We can have a struct assignment dereferencing our name twice. | |
914 | Note that we didn't propagate into the lhs to not falsely | |
915 | claim we did when propagating into the rhs. */ | |
916 | res = false; | |
971c637a | 917 | } |
15ec875c | 918 | |
631d5db6 | 919 | /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR |
920 | nodes from the RHS. */ | |
d0d1ecb8 | 921 | tree *rhsp = gimple_assign_rhs1_ptr (use_stmt); |
922 | if (TREE_CODE (*rhsp) == ADDR_EXPR) | |
923 | rhsp = &TREE_OPERAND (*rhsp, 0); | |
924 | while (handled_component_p (*rhsp)) | |
925 | rhsp = &TREE_OPERAND (*rhsp, 0); | |
926 | rhs = *rhsp; | |
291d763b | 927 | |
182cf5a9 | 928 | /* Now see if the RHS node is a MEM_REF using NAME. If so, |
291d763b | 929 | propagate the ADDR_EXPR into the use of NAME and fold the result. */ |
182cf5a9 | 930 | if (TREE_CODE (rhs) == MEM_REF |
931 | && TREE_OPERAND (rhs, 0) == name) | |
291d763b | 932 | { |
182cf5a9 | 933 | tree def_rhs_base; |
934 | HOST_WIDE_INT def_rhs_offset; | |
935 | if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0), | |
936 | &def_rhs_offset))) | |
937 | { | |
5de9d3ed | 938 | offset_int off = mem_ref_offset (rhs); |
182cf5a9 | 939 | tree new_ptr; |
e913b5cd | 940 | off += def_rhs_offset; |
182cf5a9 | 941 | if (TREE_CODE (def_rhs_base) == MEM_REF) |
942 | { | |
cf8f0e63 | 943 | off += mem_ref_offset (def_rhs_base); |
182cf5a9 | 944 | new_ptr = TREE_OPERAND (def_rhs_base, 0); |
945 | } | |
946 | else | |
947 | new_ptr = build_fold_addr_expr (def_rhs_base); | |
948 | TREE_OPERAND (rhs, 0) = new_ptr; | |
949 | TREE_OPERAND (rhs, 1) | |
e913b5cd | 950 | = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off); |
50aacf4c | 951 | fold_stmt_inplace (use_stmt_gsi); |
182cf5a9 | 952 | tidy_after_forward_propagate_addr (use_stmt); |
953 | return res; | |
954 | } | |
2e5dc41c | 955 | /* If the RHS is a plain dereference and the value type is the same as |
182cf5a9 | 956 | that of the pointed-to type of the address we can put the |
2e5dc41c | 957 | dereferenced address on the RHS preserving the original alias-type. */ |
d0d1ecb8 | 958 | else if (integer_zerop (TREE_OPERAND (rhs, 1)) |
959 | && ((gimple_assign_rhs1 (use_stmt) == rhs | |
960 | && useless_type_conversion_p | |
961 | (TREE_TYPE (gimple_assign_lhs (use_stmt)), | |
962 | TREE_TYPE (TREE_OPERAND (def_rhs, 0)))) | |
963 | || types_compatible_p (TREE_TYPE (rhs), | |
964 | TREE_TYPE (TREE_OPERAND (def_rhs, 0))))) | |
182cf5a9 | 965 | { |
966 | tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0); | |
98d96c6f | 967 | tree new_offset, new_base, saved, new_rhs; |
182cf5a9 | 968 | while (handled_component_p (*def_rhs_basep)) |
969 | def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0); | |
970 | saved = *def_rhs_basep; | |
971 | if (TREE_CODE (*def_rhs_basep) == MEM_REF) | |
972 | { | |
973 | new_base = TREE_OPERAND (*def_rhs_basep, 0); | |
b97e39a0 | 974 | new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)), |
975 | TREE_OPERAND (*def_rhs_basep, 1)); | |
182cf5a9 | 976 | } |
977 | else | |
978 | { | |
979 | new_base = build_fold_addr_expr (*def_rhs_basep); | |
980 | new_offset = TREE_OPERAND (rhs, 1); | |
981 | } | |
982 | *def_rhs_basep = build2 (MEM_REF, TREE_TYPE (*def_rhs_basep), | |
983 | new_base, new_offset); | |
2e5dc41c | 984 | TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs); |
31fa5b0d | 985 | TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs); |
2e5dc41c | 986 | TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs); |
98d96c6f | 987 | new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0)); |
d0d1ecb8 | 988 | *rhsp = new_rhs; |
98d96c6f | 989 | TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs); |
31fa5b0d | 990 | TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs); |
182cf5a9 | 991 | *def_rhs_basep = saved; |
50aacf4c | 992 | fold_stmt_inplace (use_stmt_gsi); |
182cf5a9 | 993 | tidy_after_forward_propagate_addr (use_stmt); |
994 | return res; | |
995 | } | |
291d763b | 996 | } |
997 | ||
971c637a | 998 | /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there |
999 | is nothing to do. */ | |
75a70cf9 | 1000 | if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR |
1001 | || gimple_assign_rhs1 (use_stmt) != name) | |
971c637a | 1002 | return false; |
1003 | ||
291d763b | 1004 | /* The remaining cases are all for turning pointer arithmetic into |
1005 | array indexing. They only apply when we have the address of | |
1006 | element zero in an array. If that is not the case then there | |
1007 | is nothing to do. */ | |
15ec875c | 1008 | array_ref = TREE_OPERAND (def_rhs, 0); |
182cf5a9 | 1009 | if ((TREE_CODE (array_ref) != ARRAY_REF |
1010 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE | |
1011 | || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST) | |
1012 | && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE) | |
291d763b | 1013 | return false; |
1014 | ||
75a70cf9 | 1015 | rhs2 = gimple_assign_rhs2 (use_stmt); |
704d7315 | 1016 | /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */ |
75a70cf9 | 1017 | if (TREE_CODE (rhs2) == INTEGER_CST) |
291d763b | 1018 | { |
704d7315 | 1019 | tree new_rhs = build1_loc (gimple_location (use_stmt), |
1020 | ADDR_EXPR, TREE_TYPE (def_rhs), | |
1021 | fold_build2 (MEM_REF, | |
1022 | TREE_TYPE (TREE_TYPE (def_rhs)), | |
1023 | unshare_expr (def_rhs), | |
1024 | fold_convert (ptr_type_node, | |
1025 | rhs2))); | |
1026 | gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs); | |
1027 | use_stmt = gsi_stmt (*use_stmt_gsi); | |
1028 | update_stmt (use_stmt); | |
1029 | tidy_after_forward_propagate_addr (use_stmt); | |
1030 | return true; | |
291d763b | 1031 | } |
1032 | ||
291d763b | 1033 | return false; |
1034 | } | |
1035 | ||
3d5cfe81 | 1036 | /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>. |
1037 | ||
1038 | Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME. | |
1039 | Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF | |
1040 | node or for recovery of array indexing from pointer arithmetic. | |
bfb89138 | 1041 | |
1042 | PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was | |
1043 | the single use in the previous invocation. Pass true when calling | |
1044 | this as toplevel. | |
1045 | ||
3d5cfe81 | 1046 | Returns true, if all uses have been propagated into. */ |
1047 | ||
1048 | static bool | |
bfb89138 | 1049 | forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p) |
3d5cfe81 | 1050 | { |
3d5cfe81 | 1051 | imm_use_iterator iter; |
75a70cf9 | 1052 | gimple use_stmt; |
3d5cfe81 | 1053 | bool all = true; |
bfb89138 | 1054 | bool single_use_p = parent_single_use_p && has_single_use (name); |
3d5cfe81 | 1055 | |
09aca5bc | 1056 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, name) |
3d5cfe81 | 1057 | { |
c96420f8 | 1058 | bool result; |
9481f629 | 1059 | tree use_rhs; |
3d5cfe81 | 1060 | |
1061 | /* If the use is not in a simple assignment statement, then | |
1062 | there is nothing we can do. */ | |
162efce1 | 1063 | if (!is_gimple_assign (use_stmt)) |
3d5cfe81 | 1064 | { |
688ff29b | 1065 | if (!is_gimple_debug (use_stmt)) |
9845d120 | 1066 | all = false; |
3d5cfe81 | 1067 | continue; |
1068 | } | |
1069 | ||
162efce1 | 1070 | gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
1071 | result = forward_propagate_addr_expr_1 (name, rhs, &gsi, | |
1072 | single_use_p); | |
1073 | /* If the use has moved to a different statement adjust | |
1074 | the update machinery for the old statement too. */ | |
1075 | if (use_stmt != gsi_stmt (gsi)) | |
3d5cfe81 | 1076 | { |
162efce1 | 1077 | update_stmt (use_stmt); |
1078 | use_stmt = gsi_stmt (gsi); | |
3d5cfe81 | 1079 | } |
162efce1 | 1080 | update_stmt (use_stmt); |
c96420f8 | 1081 | all &= result; |
de6ed584 | 1082 | |
15ec875c | 1083 | /* Remove intermediate now unused copy and conversion chains. */ |
75a70cf9 | 1084 | use_rhs = gimple_assign_rhs1 (use_stmt); |
15ec875c | 1085 | if (result |
75a70cf9 | 1086 | && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME |
7b705d94 | 1087 | && TREE_CODE (use_rhs) == SSA_NAME |
1088 | && has_zero_uses (gimple_assign_lhs (use_stmt))) | |
15ec875c | 1089 | { |
75a70cf9 | 1090 | gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
15ec875c | 1091 | release_defs (use_stmt); |
75a70cf9 | 1092 | gsi_remove (&gsi, true); |
15ec875c | 1093 | } |
3d5cfe81 | 1094 | } |
1095 | ||
628ce22b | 1096 | return all && has_zero_uses (name); |
3d5cfe81 | 1097 | } |
1098 | ||
678b2f5b | 1099 | |
e3a19533 | 1100 | /* Forward propagate the comparison defined in *DEFGSI like |
678b2f5b | 1101 | cond_1 = x CMP y to uses of the form |
1102 | a_1 = (T')cond_1 | |
1103 | a_1 = !cond_1 | |
1104 | a_1 = cond_1 != 0 | |
e3a19533 | 1105 | Returns true if stmt is now unused. Advance DEFGSI to the next |
1106 | statement. */ | |
678b2f5b | 1107 | |
1108 | static bool | |
e3a19533 | 1109 | forward_propagate_comparison (gimple_stmt_iterator *defgsi) |
678b2f5b | 1110 | { |
e3a19533 | 1111 | gimple stmt = gsi_stmt (*defgsi); |
678b2f5b | 1112 | tree name = gimple_assign_lhs (stmt); |
1113 | gimple use_stmt; | |
1114 | tree tmp = NULL_TREE; | |
e5b1e080 | 1115 | gimple_stmt_iterator gsi; |
1116 | enum tree_code code; | |
1117 | tree lhs; | |
678b2f5b | 1118 | |
1119 | /* Don't propagate ssa names that occur in abnormal phis. */ | |
1120 | if ((TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME | |
1121 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt))) | |
1122 | || (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME | |
1123 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs2 (stmt)))) | |
e3a19533 | 1124 | goto bailout; |
678b2f5b | 1125 | |
1126 | /* Do not un-cse comparisons. But propagate through copies. */ | |
1127 | use_stmt = get_prop_dest_stmt (name, &name); | |
e5b1e080 | 1128 | if (!use_stmt |
1129 | || !is_gimple_assign (use_stmt)) | |
e3a19533 | 1130 | goto bailout; |
678b2f5b | 1131 | |
e5b1e080 | 1132 | code = gimple_assign_rhs_code (use_stmt); |
1133 | lhs = gimple_assign_lhs (use_stmt); | |
1134 | if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs))) | |
e3a19533 | 1135 | goto bailout; |
678b2f5b | 1136 | |
e5b1e080 | 1137 | /* We can propagate the condition into a statement that |
1138 | computes the logical negation of the comparison result. */ | |
4b5f1658 | 1139 | if ((code == BIT_NOT_EXPR |
1140 | && TYPE_PRECISION (TREE_TYPE (lhs)) == 1) | |
1141 | || (code == BIT_XOR_EXPR | |
1142 | && integer_onep (gimple_assign_rhs2 (use_stmt)))) | |
e5b1e080 | 1143 | { |
1144 | tree type = TREE_TYPE (gimple_assign_rhs1 (stmt)); | |
1145 | bool nans = HONOR_NANS (TYPE_MODE (type)); | |
1146 | enum tree_code inv_code; | |
1147 | inv_code = invert_tree_comparison (gimple_assign_rhs_code (stmt), nans); | |
1148 | if (inv_code == ERROR_MARK) | |
e3a19533 | 1149 | goto bailout; |
678b2f5b | 1150 | |
e5b1e080 | 1151 | tmp = build2 (inv_code, TREE_TYPE (lhs), gimple_assign_rhs1 (stmt), |
1152 | gimple_assign_rhs2 (stmt)); | |
1153 | } | |
1154 | else | |
e3a19533 | 1155 | goto bailout; |
678b2f5b | 1156 | |
e5b1e080 | 1157 | gsi = gsi_for_stmt (use_stmt); |
1158 | gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (tmp)); | |
1159 | use_stmt = gsi_stmt (gsi); | |
1160 | update_stmt (use_stmt); | |
678b2f5b | 1161 | |
e5b1e080 | 1162 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1163 | { | |
1164 | fprintf (dump_file, " Replaced '"); | |
1165 | print_gimple_expr (dump_file, stmt, 0, dump_flags); | |
1166 | fprintf (dump_file, "' with '"); | |
1167 | print_gimple_expr (dump_file, use_stmt, 0, dump_flags); | |
1168 | fprintf (dump_file, "'\n"); | |
678b2f5b | 1169 | } |
1170 | ||
e3a19533 | 1171 | /* When we remove stmt now the iterator defgsi goes off it's current |
1172 | sequence, hence advance it now. */ | |
1173 | gsi_next (defgsi); | |
1174 | ||
e5b1e080 | 1175 | /* Remove defining statements. */ |
1176 | return remove_prop_source_from_use (name); | |
e3a19533 | 1177 | |
1178 | bailout: | |
1179 | gsi_next (defgsi); | |
1180 | return false; | |
678b2f5b | 1181 | } |
1182 | ||
1183 | ||
d23e1965 | 1184 | /* GSI_P points to a statement which performs a narrowing integral |
1185 | conversion. | |
1186 | ||
1187 | Look for cases like: | |
1188 | ||
1189 | t = x & c; | |
1190 | y = (T) t; | |
1191 | ||
1192 | Turn them into: | |
1193 | ||
1194 | t = x & c; | |
1195 | y = (T) x; | |
1196 | ||
1197 | If T is narrower than X's type and C merely masks off bits outside | |
1198 | of (T) and nothing else. | |
1199 | ||
1200 | Normally we'd let DCE remove the dead statement. But no DCE runs | |
1201 | after the last forwprop/combine pass, so we remove the obviously | |
1202 | dead code ourselves. | |
1203 | ||
1204 | Return TRUE if a change was made, FALSE otherwise. */ | |
1205 | ||
1206 | static bool | |
1207 | simplify_conversion_from_bitmask (gimple_stmt_iterator *gsi_p) | |
1208 | { | |
1209 | gimple stmt = gsi_stmt (*gsi_p); | |
1210 | gimple rhs_def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)); | |
1211 | ||
1212 | /* See if the input for the conversion was set via a BIT_AND_EXPR and | |
1213 | the only use of the BIT_AND_EXPR result is the conversion. */ | |
1214 | if (is_gimple_assign (rhs_def_stmt) | |
1215 | && gimple_assign_rhs_code (rhs_def_stmt) == BIT_AND_EXPR | |
1216 | && has_single_use (gimple_assign_lhs (rhs_def_stmt))) | |
1217 | { | |
1218 | tree rhs_def_operand1 = gimple_assign_rhs1 (rhs_def_stmt); | |
1219 | tree rhs_def_operand2 = gimple_assign_rhs2 (rhs_def_stmt); | |
1220 | tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
1221 | ||
1222 | /* Now verify suitability of the BIT_AND_EXPR's operands. | |
1223 | The first must be an SSA_NAME that we can propagate and the | |
1224 | second must be an integer constant that masks out all the | |
1225 | bits outside the final result's type, but nothing else. */ | |
1226 | if (TREE_CODE (rhs_def_operand1) == SSA_NAME | |
1227 | && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand1) | |
1228 | && TREE_CODE (rhs_def_operand2) == INTEGER_CST | |
1229 | && operand_equal_p (rhs_def_operand2, | |
1230 | build_low_bits_mask (TREE_TYPE (rhs_def_operand2), | |
1231 | TYPE_PRECISION (lhs_type)), | |
1232 | 0)) | |
1233 | { | |
1234 | /* This is an optimizable case. Replace the source operand | |
1235 | in the conversion with the first source operand of the | |
1236 | BIT_AND_EXPR. */ | |
1237 | gimple_assign_set_rhs1 (stmt, rhs_def_operand1); | |
1238 | stmt = gsi_stmt (*gsi_p); | |
1239 | update_stmt (stmt); | |
1240 | ||
1241 | /* There is no DCE after the last forwprop pass. It's | |
1242 | easy to clean up the first order effects here. */ | |
1243 | gimple_stmt_iterator si; | |
1244 | si = gsi_for_stmt (rhs_def_stmt); | |
1245 | gsi_remove (&si, true); | |
1246 | release_defs (rhs_def_stmt); | |
1247 | return true; | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | return false; | |
1252 | } | |
1253 | ||
1254 | ||
3a938499 | 1255 | /* If we have lhs = ~x (STMT), look and see if earlier we had x = ~y. |
1256 | If so, we can change STMT into lhs = y which can later be copy | |
48e1416a | 1257 | propagated. Similarly for negation. |
3a938499 | 1258 | |
48e1416a | 1259 | This could trivially be formulated as a forward propagation |
3a938499 | 1260 | to immediate uses. However, we already had an implementation |
1261 | from DOM which used backward propagation via the use-def links. | |
1262 | ||
1263 | It turns out that backward propagation is actually faster as | |
1264 | there's less work to do for each NOT/NEG expression we find. | |
1265 | Backwards propagation needs to look at the statement in a single | |
1266 | backlink. Forward propagation needs to look at potentially more | |
678b2f5b | 1267 | than one forward link. |
3a938499 | 1268 | |
678b2f5b | 1269 | Returns true when the statement was changed. */ |
1270 | ||
1271 | static bool | |
75a70cf9 | 1272 | simplify_not_neg_expr (gimple_stmt_iterator *gsi_p) |
3a938499 | 1273 | { |
75a70cf9 | 1274 | gimple stmt = gsi_stmt (*gsi_p); |
1275 | tree rhs = gimple_assign_rhs1 (stmt); | |
1276 | gimple rhs_def_stmt = SSA_NAME_DEF_STMT (rhs); | |
3a938499 | 1277 | |
1278 | /* See if the RHS_DEF_STMT has the same form as our statement. */ | |
75a70cf9 | 1279 | if (is_gimple_assign (rhs_def_stmt) |
1280 | && gimple_assign_rhs_code (rhs_def_stmt) == gimple_assign_rhs_code (stmt)) | |
3a938499 | 1281 | { |
75a70cf9 | 1282 | tree rhs_def_operand = gimple_assign_rhs1 (rhs_def_stmt); |
3a938499 | 1283 | |
1284 | /* Verify that RHS_DEF_OPERAND is a suitable SSA_NAME. */ | |
1285 | if (TREE_CODE (rhs_def_operand) == SSA_NAME | |
1286 | && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand)) | |
1287 | { | |
75a70cf9 | 1288 | gimple_assign_set_rhs_from_tree (gsi_p, rhs_def_operand); |
1289 | stmt = gsi_stmt (*gsi_p); | |
3a938499 | 1290 | update_stmt (stmt); |
678b2f5b | 1291 | return true; |
3a938499 | 1292 | } |
1293 | } | |
678b2f5b | 1294 | |
1295 | return false; | |
3a938499 | 1296 | } |
3d5cfe81 | 1297 | |
b59e1c90 | 1298 | /* Helper function for simplify_gimple_switch. Remove case labels that |
1299 | have values outside the range of the new type. */ | |
1300 | ||
1301 | static void | |
1302 | simplify_gimple_switch_label_vec (gimple stmt, tree index_type) | |
1303 | { | |
1304 | unsigned int branch_num = gimple_switch_num_labels (stmt); | |
c2078b80 | 1305 | auto_vec<tree> labels (branch_num); |
b59e1c90 | 1306 | unsigned int i, len; |
1307 | ||
1308 | /* Collect the existing case labels in a VEC, and preprocess it as if | |
1309 | we are gimplifying a GENERIC SWITCH_EXPR. */ | |
1310 | for (i = 1; i < branch_num; i++) | |
f1f41a6c | 1311 | labels.quick_push (gimple_switch_label (stmt, i)); |
b59e1c90 | 1312 | preprocess_case_label_vec_for_gimple (labels, index_type, NULL); |
1313 | ||
1314 | /* If any labels were removed, replace the existing case labels | |
1315 | in the GIMPLE_SWITCH statement with the correct ones. | |
1316 | Note that the type updates were done in-place on the case labels, | |
1317 | so we only have to replace the case labels in the GIMPLE_SWITCH | |
1318 | if the number of labels changed. */ | |
f1f41a6c | 1319 | len = labels.length (); |
b59e1c90 | 1320 | if (len < branch_num - 1) |
1321 | { | |
1322 | bitmap target_blocks; | |
1323 | edge_iterator ei; | |
1324 | edge e; | |
1325 | ||
1326 | /* Corner case: *all* case labels have been removed as being | |
1327 | out-of-range for INDEX_TYPE. Push one label and let the | |
1328 | CFG cleanups deal with this further. */ | |
1329 | if (len == 0) | |
1330 | { | |
1331 | tree label, elt; | |
1332 | ||
1333 | label = CASE_LABEL (gimple_switch_default_label (stmt)); | |
1334 | elt = build_case_label (build_int_cst (index_type, 0), NULL, label); | |
f1f41a6c | 1335 | labels.quick_push (elt); |
b59e1c90 | 1336 | len = 1; |
1337 | } | |
1338 | ||
f1f41a6c | 1339 | for (i = 0; i < labels.length (); i++) |
1340 | gimple_switch_set_label (stmt, i + 1, labels[i]); | |
b59e1c90 | 1341 | for (i++ ; i < branch_num; i++) |
1342 | gimple_switch_set_label (stmt, i, NULL_TREE); | |
1343 | gimple_switch_set_num_labels (stmt, len + 1); | |
1344 | ||
1345 | /* Cleanup any edges that are now dead. */ | |
1346 | target_blocks = BITMAP_ALLOC (NULL); | |
1347 | for (i = 0; i < gimple_switch_num_labels (stmt); i++) | |
1348 | { | |
1349 | tree elt = gimple_switch_label (stmt, i); | |
1350 | basic_block target = label_to_block (CASE_LABEL (elt)); | |
1351 | bitmap_set_bit (target_blocks, target->index); | |
1352 | } | |
1353 | for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); ) | |
1354 | { | |
1355 | if (! bitmap_bit_p (target_blocks, e->dest->index)) | |
1356 | { | |
1357 | remove_edge (e); | |
1358 | cfg_changed = true; | |
1359 | free_dominance_info (CDI_DOMINATORS); | |
1360 | } | |
1361 | else | |
1362 | ei_next (&ei); | |
1363 | } | |
1364 | BITMAP_FREE (target_blocks); | |
1365 | } | |
b59e1c90 | 1366 | } |
1367 | ||
b5860aba | 1368 | /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of |
1369 | the condition which we may be able to optimize better. */ | |
1370 | ||
678b2f5b | 1371 | static bool |
75a70cf9 | 1372 | simplify_gimple_switch (gimple stmt) |
b5860aba | 1373 | { |
b5860aba | 1374 | /* The optimization that we really care about is removing unnecessary |
1375 | casts. That will let us do much better in propagating the inferred | |
1376 | constant at the switch target. */ | |
00bffa46 | 1377 | tree cond = gimple_switch_index (stmt); |
b5860aba | 1378 | if (TREE_CODE (cond) == SSA_NAME) |
1379 | { | |
00bffa46 | 1380 | gimple def_stmt = SSA_NAME_DEF_STMT (cond); |
1381 | if (gimple_assign_cast_p (def_stmt)) | |
b5860aba | 1382 | { |
00bffa46 | 1383 | tree def = gimple_assign_rhs1 (def_stmt); |
1384 | if (TREE_CODE (def) != SSA_NAME) | |
1385 | return false; | |
1386 | ||
1387 | /* If we have an extension or sign-change that preserves the | |
1388 | values we check against then we can copy the source value into | |
1389 | the switch. */ | |
1390 | tree ti = TREE_TYPE (def); | |
1391 | if (INTEGRAL_TYPE_P (ti) | |
1392 | && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond))) | |
b5860aba | 1393 | { |
00bffa46 | 1394 | size_t n = gimple_switch_num_labels (stmt); |
1395 | tree min = NULL_TREE, max = NULL_TREE; | |
1396 | if (n > 1) | |
1397 | { | |
1398 | min = CASE_LOW (gimple_switch_label (stmt, 1)); | |
1399 | if (CASE_HIGH (gimple_switch_label (stmt, n - 1))) | |
1400 | max = CASE_HIGH (gimple_switch_label (stmt, n - 1)); | |
1401 | else | |
1402 | max = CASE_LOW (gimple_switch_label (stmt, n - 1)); | |
1403 | } | |
1404 | if ((!min || int_fits_type_p (min, ti)) | |
1405 | && (!max || int_fits_type_p (max, ti))) | |
b5860aba | 1406 | { |
75a70cf9 | 1407 | gimple_switch_set_index (stmt, def); |
b59e1c90 | 1408 | simplify_gimple_switch_label_vec (stmt, ti); |
b5860aba | 1409 | update_stmt (stmt); |
678b2f5b | 1410 | return true; |
b5860aba | 1411 | } |
1412 | } | |
1413 | } | |
1414 | } | |
678b2f5b | 1415 | |
1416 | return false; | |
b5860aba | 1417 | } |
1418 | ||
27f931ff | 1419 | /* For pointers p2 and p1 return p2 - p1 if the |
1420 | difference is known and constant, otherwise return NULL. */ | |
1421 | ||
1422 | static tree | |
1423 | constant_pointer_difference (tree p1, tree p2) | |
1424 | { | |
1425 | int i, j; | |
1426 | #define CPD_ITERATIONS 5 | |
1427 | tree exps[2][CPD_ITERATIONS]; | |
1428 | tree offs[2][CPD_ITERATIONS]; | |
1429 | int cnt[2]; | |
1430 | ||
1431 | for (i = 0; i < 2; i++) | |
1432 | { | |
1433 | tree p = i ? p1 : p2; | |
1434 | tree off = size_zero_node; | |
1435 | gimple stmt; | |
1436 | enum tree_code code; | |
1437 | ||
1438 | /* For each of p1 and p2 we need to iterate at least | |
1439 | twice, to handle ADDR_EXPR directly in p1/p2, | |
1440 | SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc. | |
1441 | on definition's stmt RHS. Iterate a few extra times. */ | |
1442 | j = 0; | |
1443 | do | |
1444 | { | |
1445 | if (!POINTER_TYPE_P (TREE_TYPE (p))) | |
1446 | break; | |
1447 | if (TREE_CODE (p) == ADDR_EXPR) | |
1448 | { | |
1449 | tree q = TREE_OPERAND (p, 0); | |
1450 | HOST_WIDE_INT offset; | |
1451 | tree base = get_addr_base_and_unit_offset (q, &offset); | |
1452 | if (base) | |
1453 | { | |
1454 | q = base; | |
1455 | if (offset) | |
1456 | off = size_binop (PLUS_EXPR, off, size_int (offset)); | |
1457 | } | |
1458 | if (TREE_CODE (q) == MEM_REF | |
1459 | && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME) | |
1460 | { | |
1461 | p = TREE_OPERAND (q, 0); | |
1462 | off = size_binop (PLUS_EXPR, off, | |
e913b5cd | 1463 | wide_int_to_tree (sizetype, |
1464 | mem_ref_offset (q))); | |
27f931ff | 1465 | } |
1466 | else | |
1467 | { | |
1468 | exps[i][j] = q; | |
1469 | offs[i][j++] = off; | |
1470 | break; | |
1471 | } | |
1472 | } | |
1473 | if (TREE_CODE (p) != SSA_NAME) | |
1474 | break; | |
1475 | exps[i][j] = p; | |
1476 | offs[i][j++] = off; | |
1477 | if (j == CPD_ITERATIONS) | |
1478 | break; | |
1479 | stmt = SSA_NAME_DEF_STMT (p); | |
1480 | if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p) | |
1481 | break; | |
1482 | code = gimple_assign_rhs_code (stmt); | |
1483 | if (code == POINTER_PLUS_EXPR) | |
1484 | { | |
1485 | if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST) | |
1486 | break; | |
1487 | off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt)); | |
1488 | p = gimple_assign_rhs1 (stmt); | |
1489 | } | |
1490 | else if (code == ADDR_EXPR || code == NOP_EXPR) | |
1491 | p = gimple_assign_rhs1 (stmt); | |
1492 | else | |
1493 | break; | |
1494 | } | |
1495 | while (1); | |
1496 | cnt[i] = j; | |
1497 | } | |
1498 | ||
1499 | for (i = 0; i < cnt[0]; i++) | |
1500 | for (j = 0; j < cnt[1]; j++) | |
1501 | if (exps[0][i] == exps[1][j]) | |
1502 | return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]); | |
1503 | ||
1504 | return NULL_TREE; | |
1505 | } | |
1506 | ||
1507 | /* *GSI_P is a GIMPLE_CALL to a builtin function. | |
1508 | Optimize | |
1509 | memcpy (p, "abcd", 4); | |
1510 | memset (p + 4, ' ', 3); | |
1511 | into | |
1512 | memcpy (p, "abcd ", 7); | |
1513 | call if the latter can be stored by pieces during expansion. */ | |
1514 | ||
1515 | static bool | |
1516 | simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2) | |
1517 | { | |
1518 | gimple stmt1, stmt2 = gsi_stmt (*gsi_p); | |
1519 | tree vuse = gimple_vuse (stmt2); | |
1520 | if (vuse == NULL) | |
1521 | return false; | |
1522 | stmt1 = SSA_NAME_DEF_STMT (vuse); | |
1523 | ||
1524 | switch (DECL_FUNCTION_CODE (callee2)) | |
1525 | { | |
1526 | case BUILT_IN_MEMSET: | |
1527 | if (gimple_call_num_args (stmt2) != 3 | |
1528 | || gimple_call_lhs (stmt2) | |
1529 | || CHAR_BIT != 8 | |
1530 | || BITS_PER_UNIT != 8) | |
1531 | break; | |
1532 | else | |
1533 | { | |
1534 | tree callee1; | |
1535 | tree ptr1, src1, str1, off1, len1, lhs1; | |
1536 | tree ptr2 = gimple_call_arg (stmt2, 0); | |
1537 | tree val2 = gimple_call_arg (stmt2, 1); | |
1538 | tree len2 = gimple_call_arg (stmt2, 2); | |
1539 | tree diff, vdef, new_str_cst; | |
1540 | gimple use_stmt; | |
1541 | unsigned int ptr1_align; | |
1542 | unsigned HOST_WIDE_INT src_len; | |
1543 | char *src_buf; | |
1544 | use_operand_p use_p; | |
1545 | ||
e913b5cd | 1546 | if (!tree_fits_shwi_p (val2) |
1547 | || !tree_fits_uhwi_p (len2)) | |
27f931ff | 1548 | break; |
1549 | if (is_gimple_call (stmt1)) | |
1550 | { | |
1551 | /* If first stmt is a call, it needs to be memcpy | |
1552 | or mempcpy, with string literal as second argument and | |
1553 | constant length. */ | |
1554 | callee1 = gimple_call_fndecl (stmt1); | |
1555 | if (callee1 == NULL_TREE | |
1556 | || DECL_BUILT_IN_CLASS (callee1) != BUILT_IN_NORMAL | |
1557 | || gimple_call_num_args (stmt1) != 3) | |
1558 | break; | |
1559 | if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY | |
1560 | && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY) | |
1561 | break; | |
1562 | ptr1 = gimple_call_arg (stmt1, 0); | |
1563 | src1 = gimple_call_arg (stmt1, 1); | |
1564 | len1 = gimple_call_arg (stmt1, 2); | |
1565 | lhs1 = gimple_call_lhs (stmt1); | |
e913b5cd | 1566 | if (!tree_fits_uhwi_p (len1)) |
27f931ff | 1567 | break; |
1568 | str1 = string_constant (src1, &off1); | |
1569 | if (str1 == NULL_TREE) | |
1570 | break; | |
e913b5cd | 1571 | if (!tree_fits_uhwi_p (off1) |
27f931ff | 1572 | || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0 |
1573 | || compare_tree_int (len1, TREE_STRING_LENGTH (str1) | |
e913b5cd | 1574 | - tree_to_uhwi (off1)) > 0 |
27f931ff | 1575 | || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE |
1576 | || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1))) | |
1577 | != TYPE_MODE (char_type_node)) | |
1578 | break; | |
1579 | } | |
1580 | else if (gimple_assign_single_p (stmt1)) | |
1581 | { | |
1582 | /* Otherwise look for length 1 memcpy optimized into | |
1583 | assignment. */ | |
1584 | ptr1 = gimple_assign_lhs (stmt1); | |
1585 | src1 = gimple_assign_rhs1 (stmt1); | |
1586 | if (TREE_CODE (ptr1) != MEM_REF | |
1587 | || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node) | |
e913b5cd | 1588 | || !tree_fits_shwi_p (src1)) |
27f931ff | 1589 | break; |
1590 | ptr1 = build_fold_addr_expr (ptr1); | |
1591 | callee1 = NULL_TREE; | |
1592 | len1 = size_one_node; | |
1593 | lhs1 = NULL_TREE; | |
1594 | off1 = size_zero_node; | |
1595 | str1 = NULL_TREE; | |
1596 | } | |
1597 | else | |
1598 | break; | |
1599 | ||
1600 | diff = constant_pointer_difference (ptr1, ptr2); | |
1601 | if (diff == NULL && lhs1 != NULL) | |
1602 | { | |
1603 | diff = constant_pointer_difference (lhs1, ptr2); | |
1604 | if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY | |
1605 | && diff != NULL) | |
1606 | diff = size_binop (PLUS_EXPR, diff, | |
1607 | fold_convert (sizetype, len1)); | |
1608 | } | |
1609 | /* If the difference between the second and first destination pointer | |
1610 | is not constant, or is bigger than memcpy length, bail out. */ | |
1611 | if (diff == NULL | |
e913b5cd | 1612 | || !tree_fits_uhwi_p (diff) |
27f931ff | 1613 | || tree_int_cst_lt (len1, diff)) |
1614 | break; | |
1615 | ||
1616 | /* Use maximum of difference plus memset length and memcpy length | |
1617 | as the new memcpy length, if it is too big, bail out. */ | |
e913b5cd | 1618 | src_len = tree_to_uhwi (diff); |
1619 | src_len += tree_to_uhwi (len2); | |
aa59f000 | 1620 | if (src_len < tree_to_uhwi (len1)) |
e913b5cd | 1621 | src_len = tree_to_uhwi (len1); |
27f931ff | 1622 | if (src_len > 1024) |
1623 | break; | |
1624 | ||
1625 | /* If mempcpy value is used elsewhere, bail out, as mempcpy | |
1626 | with bigger length will return different result. */ | |
1627 | if (lhs1 != NULL_TREE | |
1628 | && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY | |
1629 | && (TREE_CODE (lhs1) != SSA_NAME | |
1630 | || !single_imm_use (lhs1, &use_p, &use_stmt) | |
1631 | || use_stmt != stmt2)) | |
1632 | break; | |
1633 | ||
1634 | /* If anything reads memory in between memcpy and memset | |
1635 | call, the modified memcpy call might change it. */ | |
1636 | vdef = gimple_vdef (stmt1); | |
1637 | if (vdef != NULL | |
1638 | && (!single_imm_use (vdef, &use_p, &use_stmt) | |
1639 | || use_stmt != stmt2)) | |
1640 | break; | |
1641 | ||
957d0361 | 1642 | ptr1_align = get_pointer_alignment (ptr1); |
27f931ff | 1643 | /* Construct the new source string literal. */ |
1644 | src_buf = XALLOCAVEC (char, src_len + 1); | |
1645 | if (callee1) | |
1646 | memcpy (src_buf, | |
e913b5cd | 1647 | TREE_STRING_POINTER (str1) + tree_to_uhwi (off1), |
1648 | tree_to_uhwi (len1)); | |
27f931ff | 1649 | else |
e913b5cd | 1650 | src_buf[0] = tree_to_shwi (src1); |
1651 | memset (src_buf + tree_to_uhwi (diff), | |
1652 | tree_to_shwi (val2), tree_to_uhwi (len2)); | |
27f931ff | 1653 | src_buf[src_len] = '\0'; |
1654 | /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str | |
1655 | handle embedded '\0's. */ | |
1656 | if (strlen (src_buf) != src_len) | |
1657 | break; | |
1658 | rtl_profile_for_bb (gimple_bb (stmt2)); | |
1659 | /* If the new memcpy wouldn't be emitted by storing the literal | |
1660 | by pieces, this optimization might enlarge .rodata too much, | |
1661 | as commonly used string literals couldn't be shared any | |
1662 | longer. */ | |
1663 | if (!can_store_by_pieces (src_len, | |
1664 | builtin_strncpy_read_str, | |
1665 | src_buf, ptr1_align, false)) | |
1666 | break; | |
1667 | ||
1668 | new_str_cst = build_string_literal (src_len, src_buf); | |
1669 | if (callee1) | |
1670 | { | |
1671 | /* If STMT1 is a mem{,p}cpy call, adjust it and remove | |
1672 | memset call. */ | |
1673 | if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY) | |
1674 | gimple_call_set_lhs (stmt1, NULL_TREE); | |
1675 | gimple_call_set_arg (stmt1, 1, new_str_cst); | |
1676 | gimple_call_set_arg (stmt1, 2, | |
1677 | build_int_cst (TREE_TYPE (len1), src_len)); | |
1678 | update_stmt (stmt1); | |
1679 | unlink_stmt_vdef (stmt2); | |
1680 | gsi_remove (gsi_p, true); | |
1681 | release_defs (stmt2); | |
1682 | if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY) | |
1683 | release_ssa_name (lhs1); | |
1684 | return true; | |
1685 | } | |
1686 | else | |
1687 | { | |
1688 | /* Otherwise, if STMT1 is length 1 memcpy optimized into | |
1689 | assignment, remove STMT1 and change memset call into | |
1690 | memcpy call. */ | |
1691 | gimple_stmt_iterator gsi = gsi_for_stmt (stmt1); | |
1692 | ||
7ecb2e7c | 1693 | if (!is_gimple_val (ptr1)) |
1694 | ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE, | |
1695 | true, GSI_SAME_STMT); | |
b9a16870 | 1696 | gimple_call_set_fndecl (stmt2, |
1697 | builtin_decl_explicit (BUILT_IN_MEMCPY)); | |
27f931ff | 1698 | gimple_call_set_arg (stmt2, 0, ptr1); |
1699 | gimple_call_set_arg (stmt2, 1, new_str_cst); | |
1700 | gimple_call_set_arg (stmt2, 2, | |
1701 | build_int_cst (TREE_TYPE (len2), src_len)); | |
1702 | unlink_stmt_vdef (stmt1); | |
1703 | gsi_remove (&gsi, true); | |
1704 | release_defs (stmt1); | |
1705 | update_stmt (stmt2); | |
1706 | return false; | |
1707 | } | |
1708 | } | |
1709 | break; | |
1710 | default: | |
1711 | break; | |
1712 | } | |
1713 | return false; | |
1714 | } | |
1715 | ||
41913fa9 | 1716 | /* Checks if expression has type of one-bit precision, or is a known |
1717 | truth-valued expression. */ | |
1718 | static bool | |
1719 | truth_valued_ssa_name (tree name) | |
1720 | { | |
1721 | gimple def; | |
1722 | tree type = TREE_TYPE (name); | |
1723 | ||
1724 | if (!INTEGRAL_TYPE_P (type)) | |
1725 | return false; | |
1726 | /* Don't check here for BOOLEAN_TYPE as the precision isn't | |
1727 | necessarily one and so ~X is not equal to !X. */ | |
1728 | if (TYPE_PRECISION (type) == 1) | |
1729 | return true; | |
1730 | def = SSA_NAME_DEF_STMT (name); | |
1731 | if (is_gimple_assign (def)) | |
1732 | return truth_value_p (gimple_assign_rhs_code (def)); | |
1733 | return false; | |
1734 | } | |
1735 | ||
1736 | /* Helper routine for simplify_bitwise_binary_1 function. | |
1737 | Return for the SSA name NAME the expression X if it mets condition | |
1738 | NAME = !X. Otherwise return NULL_TREE. | |
1739 | Detected patterns for NAME = !X are: | |
1740 | !X and X == 0 for X with integral type. | |
1741 | X ^ 1, X != 1,or ~X for X with integral type with precision of one. */ | |
1742 | static tree | |
1743 | lookup_logical_inverted_value (tree name) | |
1744 | { | |
1745 | tree op1, op2; | |
1746 | enum tree_code code; | |
1747 | gimple def; | |
1748 | ||
1749 | /* If name has none-intergal type, or isn't a SSA_NAME, then | |
1750 | return. */ | |
1751 | if (TREE_CODE (name) != SSA_NAME | |
1752 | || !INTEGRAL_TYPE_P (TREE_TYPE (name))) | |
1753 | return NULL_TREE; | |
1754 | def = SSA_NAME_DEF_STMT (name); | |
1755 | if (!is_gimple_assign (def)) | |
1756 | return NULL_TREE; | |
1757 | ||
1758 | code = gimple_assign_rhs_code (def); | |
1759 | op1 = gimple_assign_rhs1 (def); | |
1760 | op2 = NULL_TREE; | |
1761 | ||
1762 | /* Get for EQ_EXPR or BIT_XOR_EXPR operation the second operand. | |
8f4a7578 | 1763 | If CODE isn't an EQ_EXPR, BIT_XOR_EXPR, or BIT_NOT_EXPR, then return. */ |
41913fa9 | 1764 | if (code == EQ_EXPR || code == NE_EXPR |
1765 | || code == BIT_XOR_EXPR) | |
1766 | op2 = gimple_assign_rhs2 (def); | |
1767 | ||
1768 | switch (code) | |
1769 | { | |
41913fa9 | 1770 | case BIT_NOT_EXPR: |
1771 | if (truth_valued_ssa_name (name)) | |
1772 | return op1; | |
1773 | break; | |
1774 | case EQ_EXPR: | |
1775 | /* Check if we have X == 0 and X has an integral type. */ | |
1776 | if (!INTEGRAL_TYPE_P (TREE_TYPE (op1))) | |
1777 | break; | |
1778 | if (integer_zerop (op2)) | |
1779 | return op1; | |
1780 | break; | |
1781 | case NE_EXPR: | |
1782 | /* Check if we have X != 1 and X is a truth-valued. */ | |
1783 | if (!INTEGRAL_TYPE_P (TREE_TYPE (op1))) | |
1784 | break; | |
1785 | if (integer_onep (op2) && truth_valued_ssa_name (op1)) | |
1786 | return op1; | |
1787 | break; | |
1788 | case BIT_XOR_EXPR: | |
1789 | /* Check if we have X ^ 1 and X is truth valued. */ | |
1790 | if (integer_onep (op2) && truth_valued_ssa_name (op1)) | |
1791 | return op1; | |
1792 | break; | |
1793 | default: | |
1794 | break; | |
1795 | } | |
1796 | ||
1797 | return NULL_TREE; | |
1798 | } | |
1799 | ||
1800 | /* Optimize ARG1 CODE ARG2 to a constant for bitwise binary | |
1801 | operations CODE, if one operand has the logically inverted | |
1802 | value of the other. */ | |
1803 | static tree | |
1804 | simplify_bitwise_binary_1 (enum tree_code code, tree type, | |
1805 | tree arg1, tree arg2) | |
1806 | { | |
1807 | tree anot; | |
1808 | ||
1809 | /* If CODE isn't a bitwise binary operation, return NULL_TREE. */ | |
1810 | if (code != BIT_AND_EXPR && code != BIT_IOR_EXPR | |
1811 | && code != BIT_XOR_EXPR) | |
1812 | return NULL_TREE; | |
1813 | ||
1814 | /* First check if operands ARG1 and ARG2 are equal. If so | |
1815 | return NULL_TREE as this optimization is handled fold_stmt. */ | |
1816 | if (arg1 == arg2) | |
1817 | return NULL_TREE; | |
1818 | /* See if we have in arguments logical-not patterns. */ | |
1819 | if (((anot = lookup_logical_inverted_value (arg1)) == NULL_TREE | |
1820 | || anot != arg2) | |
1821 | && ((anot = lookup_logical_inverted_value (arg2)) == NULL_TREE | |
1822 | || anot != arg1)) | |
1823 | return NULL_TREE; | |
1824 | ||
1825 | /* X & !X -> 0. */ | |
1826 | if (code == BIT_AND_EXPR) | |
1827 | return fold_convert (type, integer_zero_node); | |
1828 | /* X | !X -> 1 and X ^ !X -> 1, if X is truth-valued. */ | |
1829 | if (truth_valued_ssa_name (anot)) | |
1830 | return fold_convert (type, integer_one_node); | |
1831 | ||
1832 | /* ??? Otherwise result is (X != 0 ? X : 1). not handled. */ | |
1833 | return NULL_TREE; | |
1834 | } | |
1835 | ||
10fbe63d | 1836 | /* Given a ssa_name in NAME see if it was defined by an assignment and |
1837 | set CODE to be the code and ARG1 to the first operand on the rhs and ARG2 | |
1838 | to the second operand on the rhs. */ | |
1839 | ||
1840 | static inline void | |
1841 | defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2) | |
1842 | { | |
1843 | gimple def; | |
1844 | enum tree_code code1; | |
1845 | tree arg11; | |
1846 | tree arg21; | |
1847 | tree arg31; | |
1848 | enum gimple_rhs_class grhs_class; | |
1849 | ||
1850 | code1 = TREE_CODE (name); | |
1851 | arg11 = name; | |
1852 | arg21 = NULL_TREE; | |
1853 | grhs_class = get_gimple_rhs_class (code1); | |
1854 | ||
1855 | if (code1 == SSA_NAME) | |
1856 | { | |
1857 | def = SSA_NAME_DEF_STMT (name); | |
1858 | ||
1859 | if (def && is_gimple_assign (def) | |
1860 | && can_propagate_from (def)) | |
1861 | { | |
1862 | code1 = gimple_assign_rhs_code (def); | |
1863 | arg11 = gimple_assign_rhs1 (def); | |
1864 | arg21 = gimple_assign_rhs2 (def); | |
1865 | arg31 = gimple_assign_rhs2 (def); | |
1866 | } | |
1867 | } | |
1868 | else if (grhs_class == GIMPLE_TERNARY_RHS | |
1869 | || GIMPLE_BINARY_RHS | |
1870 | || GIMPLE_UNARY_RHS | |
1871 | || GIMPLE_SINGLE_RHS) | |
1872 | extract_ops_from_tree_1 (name, &code1, &arg11, &arg21, &arg31); | |
1873 | ||
1874 | *code = code1; | |
1875 | *arg1 = arg11; | |
1876 | if (arg2) | |
1877 | *arg2 = arg21; | |
1878 | /* Ignore arg3 currently. */ | |
1879 | } | |
1880 | ||
750e47f5 | 1881 | /* Return true if a conversion of an operand from type FROM to type TO |
1882 | should be applied after performing the operation instead. */ | |
1883 | ||
1884 | static bool | |
1885 | hoist_conversion_for_bitop_p (tree to, tree from) | |
1886 | { | |
1887 | /* That's a good idea if the conversion widens the operand, thus | |
1888 | after hoisting the conversion the operation will be narrower. */ | |
1889 | if (TYPE_PRECISION (from) < TYPE_PRECISION (to)) | |
1890 | return true; | |
1891 | ||
1892 | /* It's also a good idea if the conversion is to a non-integer mode. */ | |
1893 | if (GET_MODE_CLASS (TYPE_MODE (to)) != MODE_INT) | |
1894 | return true; | |
1895 | ||
1896 | /* Or if the precision of TO is not the same as the precision | |
1897 | of its mode. */ | |
1898 | if (TYPE_PRECISION (to) != GET_MODE_PRECISION (TYPE_MODE (to))) | |
1899 | return true; | |
1900 | ||
1901 | return false; | |
1902 | } | |
1903 | ||
16bc66ec | 1904 | /* GSI points to a statement of the form |
1905 | ||
1906 | result = OP0 CODE OP1 | |
1907 | ||
1908 | Where OP0 and OP1 are single bit SSA_NAMEs and CODE is either | |
1909 | BIT_AND_EXPR or BIT_IOR_EXPR. | |
1910 | ||
1911 | If OP0 is fed by a bitwise negation of another single bit SSA_NAME, | |
1912 | then we can simplify the two statements into a single LT_EXPR or LE_EXPR | |
1913 | when code is BIT_AND_EXPR and BIT_IOR_EXPR respectively. | |
1914 | ||
040f64e5 | 1915 | If a simplification is made, return TRUE, else return FALSE. */ |
16bc66ec | 1916 | static bool |
1917 | simplify_bitwise_binary_boolean (gimple_stmt_iterator *gsi, | |
1918 | enum tree_code code, | |
1919 | tree op0, tree op1) | |
1920 | { | |
1921 | gimple op0_def_stmt = SSA_NAME_DEF_STMT (op0); | |
1922 | ||
1923 | if (!is_gimple_assign (op0_def_stmt) | |
1924 | || (gimple_assign_rhs_code (op0_def_stmt) != BIT_NOT_EXPR)) | |
1925 | return false; | |
1926 | ||
1927 | tree x = gimple_assign_rhs1 (op0_def_stmt); | |
1928 | if (TREE_CODE (x) == SSA_NAME | |
1929 | && INTEGRAL_TYPE_P (TREE_TYPE (x)) | |
1930 | && TYPE_PRECISION (TREE_TYPE (x)) == 1 | |
1931 | && TYPE_UNSIGNED (TREE_TYPE (x)) == TYPE_UNSIGNED (TREE_TYPE (op1))) | |
1932 | { | |
1933 | enum tree_code newcode; | |
1934 | ||
1935 | gimple stmt = gsi_stmt (*gsi); | |
1936 | gimple_assign_set_rhs1 (stmt, x); | |
1937 | gimple_assign_set_rhs2 (stmt, op1); | |
1938 | if (code == BIT_AND_EXPR) | |
1939 | newcode = TYPE_UNSIGNED (TREE_TYPE (x)) ? LT_EXPR : GT_EXPR; | |
1940 | else | |
1941 | newcode = TYPE_UNSIGNED (TREE_TYPE (x)) ? LE_EXPR : GE_EXPR; | |
1942 | gimple_assign_set_rhs_code (stmt, newcode); | |
1943 | update_stmt (stmt); | |
1944 | return true; | |
1945 | } | |
1946 | return false; | |
1947 | ||
1948 | } | |
1949 | ||
300da094 | 1950 | /* Simplify bitwise binary operations. |
1951 | Return true if a transformation applied, otherwise return false. */ | |
1c4607fd | 1952 | |
300da094 | 1953 | static bool |
1954 | simplify_bitwise_binary (gimple_stmt_iterator *gsi) | |
1c4607fd | 1955 | { |
300da094 | 1956 | gimple stmt = gsi_stmt (*gsi); |
1c4607fd | 1957 | tree arg1 = gimple_assign_rhs1 (stmt); |
1958 | tree arg2 = gimple_assign_rhs2 (stmt); | |
300da094 | 1959 | enum tree_code code = gimple_assign_rhs_code (stmt); |
1960 | tree res; | |
10fbe63d | 1961 | tree def1_arg1, def1_arg2, def2_arg1, def2_arg2; |
26f54bd0 | 1962 | enum tree_code def1_code, def2_code; |
1c4607fd | 1963 | |
10fbe63d | 1964 | defcodefor_name (arg1, &def1_code, &def1_arg1, &def1_arg2); |
1965 | defcodefor_name (arg2, &def2_code, &def2_arg1, &def2_arg2); | |
26f54bd0 | 1966 | |
750e47f5 | 1967 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
1968 | when profitable. */ | |
25ce0d90 | 1969 | if (TREE_CODE (arg2) == INTEGER_CST |
1970 | && CONVERT_EXPR_CODE_P (def1_code) | |
750e47f5 | 1971 | && hoist_conversion_for_bitop_p (TREE_TYPE (arg1), TREE_TYPE (def1_arg1)) |
105fc895 | 1972 | && INTEGRAL_TYPE_P (TREE_TYPE (def1_arg1)) |
25ce0d90 | 1973 | && int_fits_type_p (arg2, TREE_TYPE (def1_arg1))) |
1974 | { | |
1975 | gimple newop; | |
03d37e4e | 1976 | tree tem = make_ssa_name (TREE_TYPE (def1_arg1), NULL); |
25ce0d90 | 1977 | newop = |
1978 | gimple_build_assign_with_ops (code, tem, def1_arg1, | |
1979 | fold_convert_loc (gimple_location (stmt), | |
1980 | TREE_TYPE (def1_arg1), | |
1981 | arg2)); | |
4b5f1658 | 1982 | gimple_set_location (newop, gimple_location (stmt)); |
25ce0d90 | 1983 | gsi_insert_before (gsi, newop, GSI_SAME_STMT); |
1984 | gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR, | |
1985 | tem, NULL_TREE, NULL_TREE); | |
1986 | update_stmt (gsi_stmt (*gsi)); | |
1987 | return true; | |
1988 | } | |
1989 | ||
300da094 | 1990 | /* For bitwise binary operations apply operand conversions to the |
1991 | binary operation result instead of to the operands. This allows | |
1992 | to combine successive conversions and bitwise binary operations. */ | |
26f54bd0 | 1993 | if (CONVERT_EXPR_CODE_P (def1_code) |
1994 | && CONVERT_EXPR_CODE_P (def2_code) | |
1995 | && types_compatible_p (TREE_TYPE (def1_arg1), TREE_TYPE (def2_arg1)) | |
750e47f5 | 1996 | && hoist_conversion_for_bitop_p (TREE_TYPE (arg1), TREE_TYPE (def1_arg1))) |
1c4607fd | 1997 | { |
26f54bd0 | 1998 | gimple newop; |
03d37e4e | 1999 | tree tem = make_ssa_name (TREE_TYPE (def1_arg1), NULL); |
26f54bd0 | 2000 | newop = gimple_build_assign_with_ops (code, tem, def1_arg1, def2_arg1); |
4b5f1658 | 2001 | gimple_set_location (newop, gimple_location (stmt)); |
26f54bd0 | 2002 | gsi_insert_before (gsi, newop, GSI_SAME_STMT); |
2003 | gimple_assign_set_rhs_with_ops_1 (gsi, NOP_EXPR, | |
2004 | tem, NULL_TREE, NULL_TREE); | |
2005 | update_stmt (gsi_stmt (*gsi)); | |
2006 | return true; | |
2007 | } | |
2008 | ||
35967c0f | 2009 | |
2010 | /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */ | |
2011 | if (def1_code == def2_code | |
2012 | && def1_code == BIT_AND_EXPR | |
0a3f7203 | 2013 | && operand_equal_for_phi_arg_p (def1_arg2, |
2014 | def2_arg2)) | |
35967c0f | 2015 | { |
0a3f7203 | 2016 | tree b = def1_arg2; |
35967c0f | 2017 | tree a = def1_arg1; |
2018 | tree c = def2_arg1; | |
2019 | tree inner = fold_build2 (code, TREE_TYPE (arg2), a, c); | |
2020 | /* If A OP0 C (this usually means C is the same as A) is 0 | |
2021 | then fold it down correctly. */ | |
2022 | if (integer_zerop (inner)) | |
2023 | { | |
2024 | gimple_assign_set_rhs_from_tree (gsi, inner); | |
2025 | update_stmt (stmt); | |
2026 | return true; | |
2027 | } | |
2028 | /* If A OP0 C (this usually means C is the same as A) is a ssa_name | |
2029 | then fold it down correctly. */ | |
2030 | else if (TREE_CODE (inner) == SSA_NAME) | |
2031 | { | |
2032 | tree outer = fold_build2 (def1_code, TREE_TYPE (inner), | |
2033 | inner, b); | |
2034 | gimple_assign_set_rhs_from_tree (gsi, outer); | |
2035 | update_stmt (stmt); | |
2036 | return true; | |
2037 | } | |
2038 | else | |
2039 | { | |
2040 | gimple newop; | |
2041 | tree tem; | |
03d37e4e | 2042 | tem = make_ssa_name (TREE_TYPE (arg2), NULL); |
35967c0f | 2043 | newop = gimple_build_assign_with_ops (code, tem, a, c); |
35967c0f | 2044 | gimple_set_location (newop, gimple_location (stmt)); |
2045 | /* Make sure to re-process the new stmt as it's walking upwards. */ | |
2046 | gsi_insert_before (gsi, newop, GSI_NEW_STMT); | |
2047 | gimple_assign_set_rhs1 (stmt, tem); | |
2048 | gimple_assign_set_rhs2 (stmt, b); | |
2049 | gimple_assign_set_rhs_code (stmt, def1_code); | |
2050 | update_stmt (stmt); | |
2051 | return true; | |
2052 | } | |
2053 | } | |
2054 | ||
26f54bd0 | 2055 | /* (a | CST1) & CST2 -> (a & CST2) | (CST1 & CST2). */ |
2056 | if (code == BIT_AND_EXPR | |
2057 | && def1_code == BIT_IOR_EXPR | |
2c83a45e | 2058 | && CONSTANT_CLASS_P (arg2) |
2059 | && CONSTANT_CLASS_P (def1_arg2)) | |
26f54bd0 | 2060 | { |
2061 | tree cst = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg2), | |
10fbe63d | 2062 | arg2, def1_arg2); |
26f54bd0 | 2063 | tree tem; |
2064 | gimple newop; | |
2065 | if (integer_zerop (cst)) | |
300da094 | 2066 | { |
26f54bd0 | 2067 | gimple_assign_set_rhs1 (stmt, def1_arg1); |
2068 | update_stmt (stmt); | |
2069 | return true; | |
300da094 | 2070 | } |
03d37e4e | 2071 | tem = make_ssa_name (TREE_TYPE (arg2), NULL); |
26f54bd0 | 2072 | newop = gimple_build_assign_with_ops (BIT_AND_EXPR, |
2073 | tem, def1_arg1, arg2); | |
4b5f1658 | 2074 | gimple_set_location (newop, gimple_location (stmt)); |
26f54bd0 | 2075 | /* Make sure to re-process the new stmt as it's walking upwards. */ |
2076 | gsi_insert_before (gsi, newop, GSI_NEW_STMT); | |
2077 | gimple_assign_set_rhs1 (stmt, tem); | |
2078 | gimple_assign_set_rhs2 (stmt, cst); | |
2079 | gimple_assign_set_rhs_code (stmt, BIT_IOR_EXPR); | |
2080 | update_stmt (stmt); | |
2081 | return true; | |
2082 | } | |
2083 | ||
2084 | /* Combine successive equal operations with constants. */ | |
2085 | if ((code == BIT_AND_EXPR | |
2086 | || code == BIT_IOR_EXPR | |
2087 | || code == BIT_XOR_EXPR) | |
2088 | && def1_code == code | |
2c83a45e | 2089 | && CONSTANT_CLASS_P (arg2) |
2090 | && CONSTANT_CLASS_P (def1_arg2)) | |
26f54bd0 | 2091 | { |
2092 | tree cst = fold_build2 (code, TREE_TYPE (arg2), | |
10fbe63d | 2093 | arg2, def1_arg2); |
26f54bd0 | 2094 | gimple_assign_set_rhs1 (stmt, def1_arg1); |
2095 | gimple_assign_set_rhs2 (stmt, cst); | |
2096 | update_stmt (stmt); | |
2097 | return true; | |
1c4607fd | 2098 | } |
300da094 | 2099 | |
8a5f403f | 2100 | /* Canonicalize X ^ ~0 to ~X. */ |
2101 | if (code == BIT_XOR_EXPR | |
8a5f403f | 2102 | && integer_all_onesp (arg2)) |
2103 | { | |
2104 | gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, arg1, NULL_TREE); | |
2105 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
2106 | update_stmt (stmt); | |
2107 | return true; | |
2108 | } | |
2109 | ||
41913fa9 | 2110 | /* Try simple folding for X op !X, and X op X. */ |
2111 | res = simplify_bitwise_binary_1 (code, TREE_TYPE (arg1), arg1, arg2); | |
2112 | if (res != NULL_TREE) | |
2113 | { | |
2114 | gimple_assign_set_rhs_from_tree (gsi, res); | |
2115 | update_stmt (gsi_stmt (*gsi)); | |
2116 | return true; | |
2117 | } | |
2118 | ||
10fbe63d | 2119 | if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR) |
2120 | { | |
2121 | enum tree_code ocode = code == BIT_AND_EXPR ? BIT_IOR_EXPR : BIT_AND_EXPR; | |
2122 | if (def1_code == ocode) | |
2123 | { | |
2124 | tree x = arg2; | |
2125 | enum tree_code coden; | |
2126 | tree a1, a2; | |
2127 | /* ( X | Y) & X -> X */ | |
2128 | /* ( X & Y) | X -> X */ | |
2129 | if (x == def1_arg1 | |
2130 | || x == def1_arg2) | |
2131 | { | |
2132 | gimple_assign_set_rhs_from_tree (gsi, x); | |
2133 | update_stmt (gsi_stmt (*gsi)); | |
2134 | return true; | |
2135 | } | |
2136 | ||
2137 | defcodefor_name (def1_arg1, &coden, &a1, &a2); | |
2138 | /* (~X | Y) & X -> X & Y */ | |
2139 | /* (~X & Y) | X -> X | Y */ | |
2140 | if (coden == BIT_NOT_EXPR && a1 == x) | |
2141 | { | |
2142 | gimple_assign_set_rhs_with_ops (gsi, code, | |
2143 | x, def1_arg2); | |
2144 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
2145 | update_stmt (stmt); | |
2146 | return true; | |
2147 | } | |
2148 | defcodefor_name (def1_arg2, &coden, &a1, &a2); | |
2149 | /* (Y | ~X) & X -> X & Y */ | |
2150 | /* (Y & ~X) | X -> X | Y */ | |
2151 | if (coden == BIT_NOT_EXPR && a1 == x) | |
2152 | { | |
2153 | gimple_assign_set_rhs_with_ops (gsi, code, | |
2154 | x, def1_arg1); | |
2155 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
2156 | update_stmt (stmt); | |
2157 | return true; | |
2158 | } | |
2159 | } | |
2160 | if (def2_code == ocode) | |
2161 | { | |
2162 | enum tree_code coden; | |
2163 | tree a1; | |
2164 | tree x = arg1; | |
2165 | /* X & ( X | Y) -> X */ | |
2166 | /* X | ( X & Y) -> X */ | |
2167 | if (x == def2_arg1 | |
2168 | || x == def2_arg2) | |
2169 | { | |
2170 | gimple_assign_set_rhs_from_tree (gsi, x); | |
2171 | update_stmt (gsi_stmt (*gsi)); | |
2172 | return true; | |
2173 | } | |
2174 | defcodefor_name (def2_arg1, &coden, &a1, NULL); | |
2175 | /* (~X | Y) & X -> X & Y */ | |
2176 | /* (~X & Y) | X -> X | Y */ | |
2177 | if (coden == BIT_NOT_EXPR && a1 == x) | |
2178 | { | |
2179 | gimple_assign_set_rhs_with_ops (gsi, code, | |
2180 | x, def2_arg2); | |
2181 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
2182 | update_stmt (stmt); | |
2183 | return true; | |
2184 | } | |
2185 | defcodefor_name (def2_arg2, &coden, &a1, NULL); | |
2186 | /* (Y | ~X) & X -> X & Y */ | |
2187 | /* (Y & ~X) | X -> X | Y */ | |
2188 | if (coden == BIT_NOT_EXPR && a1 == x) | |
2189 | { | |
2190 | gimple_assign_set_rhs_with_ops (gsi, code, | |
2191 | x, def2_arg1); | |
2192 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
2193 | update_stmt (stmt); | |
2194 | return true; | |
2195 | } | |
2196 | } | |
10fbe63d | 2197 | |
16bc66ec | 2198 | /* If arg1 and arg2 are booleans (or any single bit type) |
2199 | then try to simplify: | |
2200 | ||
2201 | (~X & Y) -> X < Y | |
2202 | (X & ~Y) -> Y < X | |
2203 | (~X | Y) -> X <= Y | |
2204 | (X | ~Y) -> Y <= X | |
2205 | ||
2206 | But only do this if our result feeds into a comparison as | |
2207 | this transformation is not always a win, particularly on | |
2208 | targets with and-not instructions. */ | |
2209 | if (TREE_CODE (arg1) == SSA_NAME | |
2210 | && TREE_CODE (arg2) == SSA_NAME | |
2211 | && INTEGRAL_TYPE_P (TREE_TYPE (arg1)) | |
2212 | && TYPE_PRECISION (TREE_TYPE (arg1)) == 1 | |
2213 | && TYPE_PRECISION (TREE_TYPE (arg2)) == 1 | |
2214 | && (TYPE_UNSIGNED (TREE_TYPE (arg1)) | |
2215 | == TYPE_UNSIGNED (TREE_TYPE (arg2)))) | |
2216 | { | |
2217 | use_operand_p use_p; | |
2218 | gimple use_stmt; | |
2219 | ||
2220 | if (single_imm_use (gimple_assign_lhs (stmt), &use_p, &use_stmt)) | |
2221 | { | |
2222 | if (gimple_code (use_stmt) == GIMPLE_COND | |
2223 | && gimple_cond_lhs (use_stmt) == gimple_assign_lhs (stmt) | |
2224 | && integer_zerop (gimple_cond_rhs (use_stmt)) | |
2225 | && gimple_cond_code (use_stmt) == NE_EXPR) | |
2226 | { | |
2227 | if (simplify_bitwise_binary_boolean (gsi, code, arg1, arg2)) | |
2228 | return true; | |
2229 | if (simplify_bitwise_binary_boolean (gsi, code, arg2, arg1)) | |
2230 | return true; | |
2231 | } | |
2232 | } | |
2233 | } | |
2234 | } | |
300da094 | 2235 | return false; |
1c4607fd | 2236 | } |
2237 | ||
ca3c9092 | 2238 | |
3b8827a2 | 2239 | /* Recognize rotation patterns. Return true if a transformation |
2240 | applied, otherwise return false. | |
2241 | ||
2242 | We are looking for X with unsigned type T with bitsize B, OP being | |
2243 | +, | or ^, some type T2 wider than T and | |
2244 | (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B | |
2245 | ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B | |
2246 | (X << Y) OP (X >> (B - Y)) | |
2247 | (X << (int) Y) OP (X >> (int) (B - Y)) | |
2248 | ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y))) | |
2249 | ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y))) | |
043ce677 | 2250 | (X << Y) | (X >> ((-Y) & (B - 1))) |
2251 | (X << (int) Y) | (X >> (int) ((-Y) & (B - 1))) | |
2252 | ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1)))) | |
2253 | ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1)))) | |
3b8827a2 | 2254 | |
2255 | and transform these into: | |
2256 | X r<< CNT1 | |
2257 | X r<< Y | |
2258 | ||
2259 | Note, in the patterns with T2 type, the type of OP operands | |
2260 | might be even a signed type, but should have precision B. */ | |
2261 | ||
2262 | static bool | |
2263 | simplify_rotate (gimple_stmt_iterator *gsi) | |
2264 | { | |
2265 | gimple stmt = gsi_stmt (*gsi); | |
2266 | tree arg[2], rtype, rotcnt = NULL_TREE; | |
2267 | tree def_arg1[2], def_arg2[2]; | |
2268 | enum tree_code def_code[2]; | |
2269 | tree lhs; | |
2270 | int i; | |
2271 | bool swapped_p = false; | |
2272 | gimple g; | |
2273 | ||
2274 | arg[0] = gimple_assign_rhs1 (stmt); | |
2275 | arg[1] = gimple_assign_rhs2 (stmt); | |
2276 | rtype = TREE_TYPE (arg[0]); | |
2277 | ||
2278 | /* Only create rotates in complete modes. Other cases are not | |
2279 | expanded properly. */ | |
2280 | if (!INTEGRAL_TYPE_P (rtype) | |
2281 | || TYPE_PRECISION (rtype) != GET_MODE_PRECISION (TYPE_MODE (rtype))) | |
2282 | return false; | |
2283 | ||
2284 | for (i = 0; i < 2; i++) | |
2285 | defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]); | |
2286 | ||
2287 | /* Look through narrowing conversions. */ | |
2288 | if (CONVERT_EXPR_CODE_P (def_code[0]) | |
2289 | && CONVERT_EXPR_CODE_P (def_code[1]) | |
2290 | && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0])) | |
2291 | && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1])) | |
2292 | && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) | |
2293 | == TYPE_PRECISION (TREE_TYPE (def_arg1[1])) | |
2294 | && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) > TYPE_PRECISION (rtype) | |
2295 | && has_single_use (arg[0]) | |
2296 | && has_single_use (arg[1])) | |
2297 | { | |
2298 | for (i = 0; i < 2; i++) | |
2299 | { | |
2300 | arg[i] = def_arg1[i]; | |
2301 | defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]); | |
2302 | } | |
2303 | } | |
2304 | ||
2305 | /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */ | |
2306 | for (i = 0; i < 2; i++) | |
2307 | if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR) | |
2308 | return false; | |
2309 | else if (!has_single_use (arg[i])) | |
2310 | return false; | |
2311 | if (def_code[0] == def_code[1]) | |
2312 | return false; | |
2313 | ||
2314 | /* If we've looked through narrowing conversions before, look through | |
2315 | widening conversions from unsigned type with the same precision | |
2316 | as rtype here. */ | |
2317 | if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype)) | |
2318 | for (i = 0; i < 2; i++) | |
2319 | { | |
2320 | tree tem; | |
2321 | enum tree_code code; | |
2322 | defcodefor_name (def_arg1[i], &code, &tem, NULL); | |
2323 | if (!CONVERT_EXPR_CODE_P (code) | |
2324 | || !INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
2325 | || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype)) | |
2326 | return false; | |
2327 | def_arg1[i] = tem; | |
2328 | } | |
2329 | /* Both shifts have to use the same first operand. */ | |
2330 | if (TREE_CODE (def_arg1[0]) != SSA_NAME || def_arg1[0] != def_arg1[1]) | |
2331 | return false; | |
2332 | if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))) | |
2333 | return false; | |
2334 | ||
2335 | /* CNT1 + CNT2 == B case above. */ | |
e913b5cd | 2336 | if (tree_fits_uhwi_p (def_arg2[0]) |
2337 | && tree_fits_uhwi_p (def_arg2[1]) | |
aa59f000 | 2338 | && tree_to_uhwi (def_arg2[0]) |
e913b5cd | 2339 | + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype)) |
3b8827a2 | 2340 | rotcnt = def_arg2[0]; |
2341 | else if (TREE_CODE (def_arg2[0]) != SSA_NAME | |
2342 | || TREE_CODE (def_arg2[1]) != SSA_NAME) | |
2343 | return false; | |
2344 | else | |
2345 | { | |
2346 | tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2]; | |
2347 | enum tree_code cdef_code[2]; | |
2348 | /* Look through conversion of the shift count argument. | |
2349 | The C/C++ FE cast any shift count argument to integer_type_node. | |
2350 | The only problem might be if the shift count type maximum value | |
2351 | is equal or smaller than number of bits in rtype. */ | |
2352 | for (i = 0; i < 2; i++) | |
2353 | { | |
2354 | def_arg2_alt[i] = def_arg2[i]; | |
2355 | defcodefor_name (def_arg2[i], &cdef_code[i], | |
2356 | &cdef_arg1[i], &cdef_arg2[i]); | |
2357 | if (CONVERT_EXPR_CODE_P (cdef_code[i]) | |
2358 | && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i])) | |
2359 | && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i])) | |
2360 | > floor_log2 (TYPE_PRECISION (rtype)) | |
2361 | && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i])) | |
2362 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (cdef_arg1[i])))) | |
2363 | { | |
2364 | def_arg2_alt[i] = cdef_arg1[i]; | |
2365 | defcodefor_name (def_arg2_alt[i], &cdef_code[i], | |
2366 | &cdef_arg1[i], &cdef_arg2[i]); | |
2367 | } | |
2368 | } | |
2369 | for (i = 0; i < 2; i++) | |
2370 | /* Check for one shift count being Y and the other B - Y, | |
2371 | with optional casts. */ | |
2372 | if (cdef_code[i] == MINUS_EXPR | |
e913b5cd | 2373 | && tree_fits_shwi_p (cdef_arg1[i]) |
2374 | && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype) | |
3b8827a2 | 2375 | && TREE_CODE (cdef_arg2[i]) == SSA_NAME) |
2376 | { | |
2377 | tree tem; | |
2378 | enum tree_code code; | |
2379 | ||
2380 | if (cdef_arg2[i] == def_arg2[1 - i] | |
2381 | || cdef_arg2[i] == def_arg2_alt[1 - i]) | |
2382 | { | |
2383 | rotcnt = cdef_arg2[i]; | |
2384 | break; | |
2385 | } | |
2386 | defcodefor_name (cdef_arg2[i], &code, &tem, NULL); | |
2387 | if (CONVERT_EXPR_CODE_P (code) | |
2388 | && INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
2389 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
2390 | > floor_log2 (TYPE_PRECISION (rtype)) | |
2391 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
2392 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem))) | |
2393 | && (tem == def_arg2[1 - i] | |
2394 | || tem == def_arg2_alt[1 - i])) | |
2395 | { | |
2396 | rotcnt = tem; | |
2397 | break; | |
2398 | } | |
2399 | } | |
2400 | /* The above sequence isn't safe for Y being 0, | |
2401 | because then one of the shifts triggers undefined behavior. | |
2402 | This alternative is safe even for rotation count of 0. | |
2403 | One shift count is Y and the other (-Y) & (B - 1). */ | |
2404 | else if (cdef_code[i] == BIT_AND_EXPR | |
e913b5cd | 2405 | && tree_fits_shwi_p (cdef_arg2[i]) |
2406 | && tree_to_shwi (cdef_arg2[i]) | |
3b8827a2 | 2407 | == TYPE_PRECISION (rtype) - 1 |
043ce677 | 2408 | && TREE_CODE (cdef_arg1[i]) == SSA_NAME |
2409 | && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR) | |
3b8827a2 | 2410 | { |
2411 | tree tem; | |
2412 | enum tree_code code; | |
2413 | ||
2414 | defcodefor_name (cdef_arg1[i], &code, &tem, NULL); | |
2415 | if (CONVERT_EXPR_CODE_P (code) | |
2416 | && INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
2417 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
2418 | > floor_log2 (TYPE_PRECISION (rtype)) | |
2419 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
2420 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem)))) | |
2421 | defcodefor_name (tem, &code, &tem, NULL); | |
2422 | ||
2423 | if (code == NEGATE_EXPR) | |
2424 | { | |
2425 | if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i]) | |
2426 | { | |
2427 | rotcnt = tem; | |
2428 | break; | |
2429 | } | |
2430 | defcodefor_name (tem, &code, &tem, NULL); | |
2431 | if (CONVERT_EXPR_CODE_P (code) | |
2432 | && INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
2433 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
2434 | > floor_log2 (TYPE_PRECISION (rtype)) | |
2435 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
2436 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (tem))) | |
2437 | && (tem == def_arg2[1 - i] | |
2438 | || tem == def_arg2_alt[1 - i])) | |
2439 | { | |
2440 | rotcnt = tem; | |
2441 | break; | |
2442 | } | |
2443 | } | |
2444 | } | |
2445 | if (rotcnt == NULL_TREE) | |
2446 | return false; | |
2447 | swapped_p = i != 1; | |
2448 | } | |
2449 | ||
2450 | if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]), | |
2451 | TREE_TYPE (rotcnt))) | |
2452 | { | |
2453 | g = gimple_build_assign_with_ops (NOP_EXPR, | |
2454 | make_ssa_name (TREE_TYPE (def_arg2[0]), | |
2455 | NULL), | |
2456 | rotcnt, NULL_TREE); | |
2457 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
2458 | rotcnt = gimple_assign_lhs (g); | |
2459 | } | |
2460 | lhs = gimple_assign_lhs (stmt); | |
2461 | if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0]))) | |
2462 | lhs = make_ssa_name (TREE_TYPE (def_arg1[0]), NULL); | |
2463 | g = gimple_build_assign_with_ops (((def_code[0] == LSHIFT_EXPR) ^ swapped_p) | |
2464 | ? LROTATE_EXPR : RROTATE_EXPR, | |
2465 | lhs, def_arg1[0], rotcnt); | |
2466 | if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0]))) | |
2467 | { | |
2468 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
2469 | g = gimple_build_assign_with_ops (NOP_EXPR, gimple_assign_lhs (stmt), | |
2470 | lhs, NULL_TREE); | |
2471 | } | |
2472 | gsi_replace (gsi, g, false); | |
2473 | return true; | |
2474 | } | |
2475 | ||
ca3c9092 | 2476 | /* Perform re-associations of the plus or minus statement STMT that are |
b69d1cb6 | 2477 | always permitted. Returns true if the CFG was changed. */ |
ca3c9092 | 2478 | |
b69d1cb6 | 2479 | static bool |
50aacf4c | 2480 | associate_plusminus (gimple_stmt_iterator *gsi) |
ca3c9092 | 2481 | { |
50aacf4c | 2482 | gimple stmt = gsi_stmt (*gsi); |
ca3c9092 | 2483 | tree rhs1 = gimple_assign_rhs1 (stmt); |
2484 | tree rhs2 = gimple_assign_rhs2 (stmt); | |
2485 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
ca3c9092 | 2486 | bool changed; |
2487 | ||
2488 | /* We can't reassociate at all for saturating types. */ | |
2489 | if (TYPE_SATURATING (TREE_TYPE (rhs1))) | |
b69d1cb6 | 2490 | return false; |
ca3c9092 | 2491 | |
2492 | /* First contract negates. */ | |
2493 | do | |
2494 | { | |
2495 | changed = false; | |
2496 | ||
2497 | /* A +- (-B) -> A -+ B. */ | |
2498 | if (TREE_CODE (rhs2) == SSA_NAME) | |
2499 | { | |
2500 | gimple def_stmt = SSA_NAME_DEF_STMT (rhs2); | |
2501 | if (is_gimple_assign (def_stmt) | |
32cdcc42 | 2502 | && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR |
2503 | && can_propagate_from (def_stmt)) | |
ca3c9092 | 2504 | { |
2505 | code = (code == MINUS_EXPR) ? PLUS_EXPR : MINUS_EXPR; | |
2506 | gimple_assign_set_rhs_code (stmt, code); | |
2507 | rhs2 = gimple_assign_rhs1 (def_stmt); | |
2508 | gimple_assign_set_rhs2 (stmt, rhs2); | |
2509 | gimple_set_modified (stmt, true); | |
2510 | changed = true; | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | /* (-A) + B -> B - A. */ | |
2515 | if (TREE_CODE (rhs1) == SSA_NAME | |
2516 | && code == PLUS_EXPR) | |
2517 | { | |
2518 | gimple def_stmt = SSA_NAME_DEF_STMT (rhs1); | |
2519 | if (is_gimple_assign (def_stmt) | |
32cdcc42 | 2520 | && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR |
2521 | && can_propagate_from (def_stmt)) | |
ca3c9092 | 2522 | { |
2523 | code = MINUS_EXPR; | |
2524 | gimple_assign_set_rhs_code (stmt, code); | |
2525 | rhs1 = rhs2; | |
2526 | gimple_assign_set_rhs1 (stmt, rhs1); | |
2527 | rhs2 = gimple_assign_rhs1 (def_stmt); | |
2528 | gimple_assign_set_rhs2 (stmt, rhs2); | |
2529 | gimple_set_modified (stmt, true); | |
2530 | changed = true; | |
2531 | } | |
2532 | } | |
2533 | } | |
2534 | while (changed); | |
2535 | ||
2536 | /* We can't reassociate floating-point or fixed-point plus or minus | |
2537 | because of saturation to +-Inf. */ | |
2538 | if (FLOAT_TYPE_P (TREE_TYPE (rhs1)) | |
2539 | || FIXED_POINT_TYPE_P (TREE_TYPE (rhs1))) | |
2540 | goto out; | |
2541 | ||
2542 | /* Second match patterns that allow contracting a plus-minus pair | |
2543 | irrespective of overflow issues. | |
2544 | ||
2545 | (A +- B) - A -> +- B | |
2546 | (A +- B) -+ B -> A | |
2547 | (CST +- A) +- CST -> CST +- A | |
2c83a45e | 2548 | (A +- CST) +- CST -> A +- CST |
ca3c9092 | 2549 | ~A + A -> -1 |
2550 | ~A + 1 -> -A | |
2551 | A - (A +- B) -> -+ B | |
2552 | A +- (B +- A) -> +- B | |
2553 | CST +- (CST +- A) -> CST +- A | |
2554 | CST +- (A +- CST) -> CST +- A | |
2555 | A + ~A -> -1 | |
c223850c | 2556 | (T)(P + A) - (T)P -> (T)A |
ca3c9092 | 2557 | |
2558 | via commutating the addition and contracting operations to zero | |
2559 | by reassociation. */ | |
2560 | ||
ca3c9092 | 2561 | if (TREE_CODE (rhs1) == SSA_NAME) |
2562 | { | |
2563 | gimple def_stmt = SSA_NAME_DEF_STMT (rhs1); | |
32cdcc42 | 2564 | if (is_gimple_assign (def_stmt) && can_propagate_from (def_stmt)) |
ca3c9092 | 2565 | { |
2566 | enum tree_code def_code = gimple_assign_rhs_code (def_stmt); | |
2567 | if (def_code == PLUS_EXPR | |
2568 | || def_code == MINUS_EXPR) | |
2569 | { | |
2570 | tree def_rhs1 = gimple_assign_rhs1 (def_stmt); | |
2571 | tree def_rhs2 = gimple_assign_rhs2 (def_stmt); | |
2572 | if (operand_equal_p (def_rhs1, rhs2, 0) | |
2573 | && code == MINUS_EXPR) | |
2574 | { | |
2575 | /* (A +- B) - A -> +- B. */ | |
2576 | code = ((def_code == PLUS_EXPR) | |
2577 | ? TREE_CODE (def_rhs2) : NEGATE_EXPR); | |
2578 | rhs1 = def_rhs2; | |
2579 | rhs2 = NULL_TREE; | |
50aacf4c | 2580 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2581 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2582 | gimple_set_modified (stmt, true); |
2583 | } | |
2584 | else if (operand_equal_p (def_rhs2, rhs2, 0) | |
2585 | && code != def_code) | |
2586 | { | |
2587 | /* (A +- B) -+ B -> A. */ | |
2588 | code = TREE_CODE (def_rhs1); | |
2589 | rhs1 = def_rhs1; | |
2590 | rhs2 = NULL_TREE; | |
50aacf4c | 2591 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2592 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2593 | gimple_set_modified (stmt, true); |
2594 | } | |
2c83a45e | 2595 | else if (CONSTANT_CLASS_P (rhs2) |
2596 | && CONSTANT_CLASS_P (def_rhs1)) | |
ca3c9092 | 2597 | { |
2598 | /* (CST +- A) +- CST -> CST +- A. */ | |
2599 | tree cst = fold_binary (code, TREE_TYPE (rhs1), | |
2600 | def_rhs1, rhs2); | |
2601 | if (cst && !TREE_OVERFLOW (cst)) | |
2602 | { | |
2603 | code = def_code; | |
2604 | gimple_assign_set_rhs_code (stmt, code); | |
2605 | rhs1 = cst; | |
2606 | gimple_assign_set_rhs1 (stmt, rhs1); | |
2607 | rhs2 = def_rhs2; | |
2608 | gimple_assign_set_rhs2 (stmt, rhs2); | |
2609 | gimple_set_modified (stmt, true); | |
2610 | } | |
2611 | } | |
2c83a45e | 2612 | else if (CONSTANT_CLASS_P (rhs2) |
2613 | && CONSTANT_CLASS_P (def_rhs2)) | |
ca3c9092 | 2614 | { |
2c83a45e | 2615 | /* (A +- CST) +- CST -> A +- CST. */ |
2616 | enum tree_code mix = (code == def_code) | |
2617 | ? PLUS_EXPR : MINUS_EXPR; | |
2618 | tree cst = fold_binary (mix, TREE_TYPE (rhs1), | |
ca3c9092 | 2619 | def_rhs2, rhs2); |
2620 | if (cst && !TREE_OVERFLOW (cst)) | |
2621 | { | |
2c83a45e | 2622 | code = def_code; |
ca3c9092 | 2623 | gimple_assign_set_rhs_code (stmt, code); |
2624 | rhs1 = def_rhs1; | |
2625 | gimple_assign_set_rhs1 (stmt, rhs1); | |
2626 | rhs2 = cst; | |
2627 | gimple_assign_set_rhs2 (stmt, rhs2); | |
2628 | gimple_set_modified (stmt, true); | |
2629 | } | |
2630 | } | |
2631 | } | |
2c83a45e | 2632 | else if (def_code == BIT_NOT_EXPR && code == PLUS_EXPR) |
ca3c9092 | 2633 | { |
2634 | tree def_rhs1 = gimple_assign_rhs1 (def_stmt); | |
2c83a45e | 2635 | if (operand_equal_p (def_rhs1, rhs2, 0)) |
ca3c9092 | 2636 | { |
2637 | /* ~A + A -> -1. */ | |
2c83a45e | 2638 | rhs1 = build_all_ones_cst (TREE_TYPE (rhs2)); |
ca3c9092 | 2639 | rhs2 = NULL_TREE; |
2c83a45e | 2640 | code = TREE_CODE (rhs1); |
50aacf4c | 2641 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2642 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2643 | gimple_set_modified (stmt, true); |
2644 | } | |
2c83a45e | 2645 | else if ((TREE_CODE (TREE_TYPE (rhs2)) != COMPLEX_TYPE |
2646 | && integer_onep (rhs2)) | |
2647 | || (TREE_CODE (rhs2) == COMPLEX_CST | |
2648 | && integer_onep (TREE_REALPART (rhs2)) | |
2649 | && integer_onep (TREE_IMAGPART (rhs2)))) | |
ca3c9092 | 2650 | { |
2651 | /* ~A + 1 -> -A. */ | |
2652 | code = NEGATE_EXPR; | |
2653 | rhs1 = def_rhs1; | |
2654 | rhs2 = NULL_TREE; | |
50aacf4c | 2655 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2656 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2657 | gimple_set_modified (stmt, true); |
2658 | } | |
2659 | } | |
f0365515 | 2660 | else if (code == MINUS_EXPR |
2661 | && CONVERT_EXPR_CODE_P (def_code) | |
2662 | && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME | |
c223850c | 2663 | && TREE_CODE (rhs2) == SSA_NAME) |
2664 | { | |
f0365515 | 2665 | /* (T)(P + A) - (T)P -> (T)A. */ |
c223850c | 2666 | gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs2); |
f0365515 | 2667 | if (is_gimple_assign (def_stmt2) |
c223850c | 2668 | && can_propagate_from (def_stmt2) |
2669 | && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2)) | |
2670 | && TREE_CODE (gimple_assign_rhs1 (def_stmt2)) == SSA_NAME) | |
2671 | { | |
f0365515 | 2672 | /* Now we have (T)X - (T)P. */ |
2673 | tree p = gimple_assign_rhs1 (def_stmt2); | |
c223850c | 2674 | def_stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt)); |
2675 | if (is_gimple_assign (def_stmt2) | |
f0365515 | 2676 | && can_propagate_from (def_stmt2) |
2677 | && (gimple_assign_rhs_code (def_stmt2) == POINTER_PLUS_EXPR | |
2678 | || gimple_assign_rhs_code (def_stmt2) == PLUS_EXPR) | |
2679 | && gimple_assign_rhs1 (def_stmt2) == p) | |
c223850c | 2680 | { |
f0365515 | 2681 | /* And finally (T)(P + A) - (T)P. */ |
2682 | tree a = gimple_assign_rhs2 (def_stmt2); | |
8f79c655 | 2683 | if (TYPE_PRECISION (TREE_TYPE (rhs1)) |
2684 | <= TYPE_PRECISION (TREE_TYPE (a)) | |
2685 | /* For integer types, if A has a smaller type | |
2686 | than T the result depends on the possible | |
2687 | overflow in P + A. | |
2688 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
2689 | However, if an overflow in P + A would cause | |
2690 | undefined behavior, we can assume that there | |
2691 | is no overflow. */ | |
2692 | || (INTEGRAL_TYPE_P (TREE_TYPE (p)) | |
2693 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (p))) | |
2694 | /* For pointer types, if the conversion of A to the | |
2695 | final type requires a sign- or zero-extension, | |
2696 | then we have to punt - it is not defined which | |
2697 | one is correct. */ | |
2698 | || (POINTER_TYPE_P (TREE_TYPE (p)) | |
2699 | && TREE_CODE (a) == INTEGER_CST | |
f0365515 | 2700 | && tree_int_cst_sign_bit (a) == 0)) |
c223850c | 2701 | { |
8f79c655 | 2702 | if (issue_strict_overflow_warning |
2703 | (WARN_STRICT_OVERFLOW_MISC) | |
2704 | && TYPE_PRECISION (TREE_TYPE (rhs1)) | |
2705 | > TYPE_PRECISION (TREE_TYPE (a)) | |
2706 | && INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
2707 | warning_at (gimple_location (stmt), | |
2708 | OPT_Wstrict_overflow, | |
2709 | "assuming signed overflow does not " | |
2710 | "occur when assuming that " | |
2711 | "(T)(P + A) - (T)P is always (T)A"); | |
c223850c | 2712 | if (useless_type_conversion_p (TREE_TYPE (rhs1), |
f0365515 | 2713 | TREE_TYPE (a))) |
2714 | code = TREE_CODE (a); | |
c223850c | 2715 | else |
f0365515 | 2716 | code = NOP_EXPR; |
2717 | rhs1 = a; | |
c223850c | 2718 | rhs2 = NULL_TREE; |
2719 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, | |
f0365515 | 2720 | rhs2); |
c223850c | 2721 | gcc_assert (gsi_stmt (*gsi) == stmt); |
2722 | gimple_set_modified (stmt, true); | |
2723 | } | |
2724 | } | |
2725 | } | |
2726 | } | |
ca3c9092 | 2727 | } |
2728 | } | |
2729 | ||
2730 | if (rhs2 && TREE_CODE (rhs2) == SSA_NAME) | |
2731 | { | |
2732 | gimple def_stmt = SSA_NAME_DEF_STMT (rhs2); | |
32cdcc42 | 2733 | if (is_gimple_assign (def_stmt) && can_propagate_from (def_stmt)) |
ca3c9092 | 2734 | { |
2735 | enum tree_code def_code = gimple_assign_rhs_code (def_stmt); | |
2736 | if (def_code == PLUS_EXPR | |
2737 | || def_code == MINUS_EXPR) | |
2738 | { | |
2739 | tree def_rhs1 = gimple_assign_rhs1 (def_stmt); | |
2740 | tree def_rhs2 = gimple_assign_rhs2 (def_stmt); | |
2741 | if (operand_equal_p (def_rhs1, rhs1, 0) | |
2742 | && code == MINUS_EXPR) | |
2743 | { | |
2744 | /* A - (A +- B) -> -+ B. */ | |
2745 | code = ((def_code == PLUS_EXPR) | |
2746 | ? NEGATE_EXPR : TREE_CODE (def_rhs2)); | |
2747 | rhs1 = def_rhs2; | |
2748 | rhs2 = NULL_TREE; | |
50aacf4c | 2749 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2750 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2751 | gimple_set_modified (stmt, true); |
2752 | } | |
2753 | else if (operand_equal_p (def_rhs2, rhs1, 0) | |
2754 | && code != def_code) | |
2755 | { | |
2756 | /* A +- (B +- A) -> +- B. */ | |
2757 | code = ((code == PLUS_EXPR) | |
2758 | ? TREE_CODE (def_rhs1) : NEGATE_EXPR); | |
2759 | rhs1 = def_rhs1; | |
2760 | rhs2 = NULL_TREE; | |
50aacf4c | 2761 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2762 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2763 | gimple_set_modified (stmt, true); |
2764 | } | |
2c83a45e | 2765 | else if (CONSTANT_CLASS_P (rhs1) |
2766 | && CONSTANT_CLASS_P (def_rhs1)) | |
ca3c9092 | 2767 | { |
2768 | /* CST +- (CST +- A) -> CST +- A. */ | |
2769 | tree cst = fold_binary (code, TREE_TYPE (rhs2), | |
2770 | rhs1, def_rhs1); | |
2771 | if (cst && !TREE_OVERFLOW (cst)) | |
2772 | { | |
2773 | code = (code == def_code ? PLUS_EXPR : MINUS_EXPR); | |
2774 | gimple_assign_set_rhs_code (stmt, code); | |
2775 | rhs1 = cst; | |
2776 | gimple_assign_set_rhs1 (stmt, rhs1); | |
2777 | rhs2 = def_rhs2; | |
2778 | gimple_assign_set_rhs2 (stmt, rhs2); | |
2779 | gimple_set_modified (stmt, true); | |
2780 | } | |
2781 | } | |
2c83a45e | 2782 | else if (CONSTANT_CLASS_P (rhs1) |
2783 | && CONSTANT_CLASS_P (def_rhs2)) | |
ca3c9092 | 2784 | { |
2785 | /* CST +- (A +- CST) -> CST +- A. */ | |
2786 | tree cst = fold_binary (def_code == code | |
2787 | ? PLUS_EXPR : MINUS_EXPR, | |
2788 | TREE_TYPE (rhs2), | |
2789 | rhs1, def_rhs2); | |
2790 | if (cst && !TREE_OVERFLOW (cst)) | |
2791 | { | |
2792 | rhs1 = cst; | |
2793 | gimple_assign_set_rhs1 (stmt, rhs1); | |
2794 | rhs2 = def_rhs1; | |
2795 | gimple_assign_set_rhs2 (stmt, rhs2); | |
2796 | gimple_set_modified (stmt, true); | |
2797 | } | |
2798 | } | |
2799 | } | |
2c83a45e | 2800 | else if (def_code == BIT_NOT_EXPR) |
ca3c9092 | 2801 | { |
2802 | tree def_rhs1 = gimple_assign_rhs1 (def_stmt); | |
2803 | if (code == PLUS_EXPR | |
2804 | && operand_equal_p (def_rhs1, rhs1, 0)) | |
2805 | { | |
2806 | /* A + ~A -> -1. */ | |
2c83a45e | 2807 | rhs1 = build_all_ones_cst (TREE_TYPE (rhs1)); |
ca3c9092 | 2808 | rhs2 = NULL_TREE; |
2c83a45e | 2809 | code = TREE_CODE (rhs1); |
50aacf4c | 2810 | gimple_assign_set_rhs_with_ops (gsi, code, rhs1, NULL_TREE); |
2811 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
ca3c9092 | 2812 | gimple_set_modified (stmt, true); |
2813 | } | |
2814 | } | |
2815 | } | |
2816 | } | |
2817 | ||
2818 | out: | |
2819 | if (gimple_modified_p (stmt)) | |
2820 | { | |
50aacf4c | 2821 | fold_stmt_inplace (gsi); |
ca3c9092 | 2822 | update_stmt (stmt); |
5a423a75 | 2823 | return true; |
ca3c9092 | 2824 | } |
b69d1cb6 | 2825 | |
2826 | return false; | |
ca3c9092 | 2827 | } |
2828 | ||
c9c17332 | 2829 | /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns |
2830 | true if anything changed, false otherwise. */ | |
2831 | ||
2832 | static bool | |
b904104c | 2833 | associate_pointerplus_align (gimple_stmt_iterator *gsi) |
c9c17332 | 2834 | { |
2835 | gimple stmt = gsi_stmt (*gsi); | |
2836 | gimple def_stmt; | |
2837 | tree ptr, rhs, algn; | |
2838 | ||
2839 | /* Pattern match | |
2840 | tem = (sizetype) ptr; | |
2841 | tem = tem & algn; | |
2842 | tem = -tem; | |
2843 | ... = ptr p+ tem; | |
2844 | and produce the simpler and easier to analyze with respect to alignment | |
2845 | ... = ptr & ~algn; */ | |
2846 | ptr = gimple_assign_rhs1 (stmt); | |
2847 | rhs = gimple_assign_rhs2 (stmt); | |
2848 | if (TREE_CODE (rhs) != SSA_NAME) | |
2849 | return false; | |
2850 | def_stmt = SSA_NAME_DEF_STMT (rhs); | |
2851 | if (!is_gimple_assign (def_stmt) | |
2852 | || gimple_assign_rhs_code (def_stmt) != NEGATE_EXPR) | |
2853 | return false; | |
2854 | rhs = gimple_assign_rhs1 (def_stmt); | |
2855 | if (TREE_CODE (rhs) != SSA_NAME) | |
2856 | return false; | |
2857 | def_stmt = SSA_NAME_DEF_STMT (rhs); | |
2858 | if (!is_gimple_assign (def_stmt) | |
2859 | || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR) | |
2860 | return false; | |
2861 | rhs = gimple_assign_rhs1 (def_stmt); | |
2862 | algn = gimple_assign_rhs2 (def_stmt); | |
2863 | if (TREE_CODE (rhs) != SSA_NAME | |
2864 | || TREE_CODE (algn) != INTEGER_CST) | |
2865 | return false; | |
2866 | def_stmt = SSA_NAME_DEF_STMT (rhs); | |
2867 | if (!is_gimple_assign (def_stmt) | |
2868 | || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) | |
2869 | return false; | |
2870 | if (gimple_assign_rhs1 (def_stmt) != ptr) | |
2871 | return false; | |
2872 | ||
6da74b21 | 2873 | algn = wide_int_to_tree (TREE_TYPE (ptr), wi::bit_not (algn)); |
c9c17332 | 2874 | gimple_assign_set_rhs_with_ops (gsi, BIT_AND_EXPR, ptr, algn); |
2875 | fold_stmt_inplace (gsi); | |
2876 | update_stmt (stmt); | |
2877 | ||
2878 | return true; | |
2879 | } | |
2880 | ||
b904104c | 2881 | /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns |
2882 | true if anything changed, false otherwise. */ | |
2883 | ||
2884 | static bool | |
2885 | associate_pointerplus_diff (gimple_stmt_iterator *gsi) | |
2886 | { | |
2887 | gimple stmt = gsi_stmt (*gsi); | |
2888 | gimple def_stmt; | |
2889 | tree ptr1, rhs; | |
2890 | ||
2891 | /* Pattern match | |
2892 | tem1 = (long) ptr1; | |
2893 | tem2 = (long) ptr2; | |
2894 | tem3 = tem2 - tem1; | |
2895 | tem4 = (unsigned long) tem3; | |
2896 | tem5 = ptr1 + tem4; | |
2897 | and produce | |
2898 | tem5 = ptr2; */ | |
2899 | ptr1 = gimple_assign_rhs1 (stmt); | |
2900 | rhs = gimple_assign_rhs2 (stmt); | |
2901 | if (TREE_CODE (rhs) != SSA_NAME) | |
2902 | return false; | |
2903 | gimple minus = SSA_NAME_DEF_STMT (rhs); | |
2904 | /* Conditionally look through a sign-changing conversion. */ | |
2905 | if (is_gimple_assign (minus) | |
2906 | && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (minus)) | |
2907 | && (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (minus))) | |
2908 | == TYPE_PRECISION (TREE_TYPE (rhs))) | |
2909 | && TREE_CODE (gimple_assign_rhs1 (minus)) == SSA_NAME) | |
2910 | minus = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (minus)); | |
2911 | if (!is_gimple_assign (minus)) | |
2912 | return false; | |
2913 | if (gimple_assign_rhs_code (minus) != MINUS_EXPR) | |
2914 | return false; | |
2915 | rhs = gimple_assign_rhs2 (minus); | |
2916 | if (TREE_CODE (rhs) != SSA_NAME) | |
2917 | return false; | |
2918 | def_stmt = SSA_NAME_DEF_STMT (rhs); | |
2919 | if (!is_gimple_assign (def_stmt) | |
2920 | || ! CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)) | |
2921 | || gimple_assign_rhs1 (def_stmt) != ptr1) | |
2922 | return false; | |
2923 | rhs = gimple_assign_rhs1 (minus); | |
2924 | if (TREE_CODE (rhs) != SSA_NAME) | |
2925 | return false; | |
2926 | def_stmt = SSA_NAME_DEF_STMT (rhs); | |
2927 | if (!is_gimple_assign (def_stmt) | |
2928 | || ! CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) | |
2929 | return false; | |
2930 | rhs = gimple_assign_rhs1 (def_stmt); | |
2931 | if (! useless_type_conversion_p (TREE_TYPE (ptr1), TREE_TYPE (rhs))) | |
2932 | return false; | |
2933 | ||
2934 | gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (rhs), rhs, NULL_TREE); | |
2935 | update_stmt (stmt); | |
2936 | ||
2937 | return true; | |
2938 | } | |
2939 | ||
2940 | /* Associate operands of a POINTER_PLUS_EXPR assignmen at *GSI. Returns | |
2941 | true if anything changed, false otherwise. */ | |
2942 | ||
2943 | static bool | |
2944 | associate_pointerplus (gimple_stmt_iterator *gsi) | |
2945 | { | |
2946 | gimple stmt = gsi_stmt (*gsi); | |
2947 | gimple def_stmt; | |
2948 | tree ptr, off1, off2; | |
2949 | ||
2950 | if (associate_pointerplus_align (gsi) | |
2951 | || associate_pointerplus_diff (gsi)) | |
2952 | return true; | |
2953 | ||
2954 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ | |
2955 | ptr = gimple_assign_rhs1 (stmt); | |
2956 | off1 = gimple_assign_rhs2 (stmt); | |
135b982d | 2957 | if (TREE_CODE (ptr) != SSA_NAME |
2958 | || !has_single_use (ptr)) | |
b904104c | 2959 | return false; |
2960 | def_stmt = SSA_NAME_DEF_STMT (ptr); | |
2961 | if (!is_gimple_assign (def_stmt) | |
135b982d | 2962 | || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR |
2963 | || !can_propagate_from (def_stmt)) | |
b904104c | 2964 | return false; |
2965 | ptr = gimple_assign_rhs1 (def_stmt); | |
2966 | off2 = gimple_assign_rhs2 (def_stmt); | |
2967 | if (!types_compatible_p (TREE_TYPE (off1), TREE_TYPE (off2))) | |
2968 | return false; | |
2969 | ||
2970 | tree off = make_ssa_name (TREE_TYPE (off1), NULL); | |
2971 | gimple ostmt = gimple_build_assign_with_ops (PLUS_EXPR, off, off1, off2); | |
2972 | gsi_insert_before (gsi, ostmt, GSI_SAME_STMT); | |
2973 | ||
2974 | gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR, ptr, off); | |
2975 | update_stmt (stmt); | |
2976 | ||
2977 | return true; | |
2978 | } | |
2979 | ||
6afd0544 | 2980 | /* Combine two conversions in a row for the second conversion at *GSI. |
89c8f35a | 2981 | Returns 1 if there were any changes made, 2 if cfg-cleanup needs to |
2982 | run. Else it returns 0. */ | |
6afd0544 | 2983 | |
89c8f35a | 2984 | static int |
6afd0544 | 2985 | combine_conversions (gimple_stmt_iterator *gsi) |
2986 | { | |
2987 | gimple stmt = gsi_stmt (*gsi); | |
2988 | gimple def_stmt; | |
2989 | tree op0, lhs; | |
2990 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
487282d5 | 2991 | enum tree_code code2; |
6afd0544 | 2992 | |
2993 | gcc_checking_assert (CONVERT_EXPR_CODE_P (code) | |
2994 | || code == FLOAT_EXPR | |
2995 | || code == FIX_TRUNC_EXPR); | |
2996 | ||
2997 | lhs = gimple_assign_lhs (stmt); | |
2998 | op0 = gimple_assign_rhs1 (stmt); | |
2999 | if (useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0))) | |
3000 | { | |
3001 | gimple_assign_set_rhs_code (stmt, TREE_CODE (op0)); | |
89c8f35a | 3002 | return 1; |
6afd0544 | 3003 | } |
3004 | ||
3005 | if (TREE_CODE (op0) != SSA_NAME) | |
89c8f35a | 3006 | return 0; |
6afd0544 | 3007 | |
3008 | def_stmt = SSA_NAME_DEF_STMT (op0); | |
3009 | if (!is_gimple_assign (def_stmt)) | |
89c8f35a | 3010 | return 0; |
6afd0544 | 3011 | |
487282d5 | 3012 | code2 = gimple_assign_rhs_code (def_stmt); |
3013 | ||
3014 | if (CONVERT_EXPR_CODE_P (code2) || code2 == FLOAT_EXPR) | |
6afd0544 | 3015 | { |
3016 | tree defop0 = gimple_assign_rhs1 (def_stmt); | |
3017 | tree type = TREE_TYPE (lhs); | |
3018 | tree inside_type = TREE_TYPE (defop0); | |
3019 | tree inter_type = TREE_TYPE (op0); | |
3020 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
3021 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
3022 | int inside_float = FLOAT_TYPE_P (inside_type); | |
3023 | int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE; | |
3024 | unsigned int inside_prec = TYPE_PRECISION (inside_type); | |
3025 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
3026 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
3027 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
3028 | int inter_float = FLOAT_TYPE_P (inter_type); | |
3029 | int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE; | |
3030 | unsigned int inter_prec = TYPE_PRECISION (inter_type); | |
3031 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
3032 | int final_int = INTEGRAL_TYPE_P (type); | |
3033 | int final_ptr = POINTER_TYPE_P (type); | |
3034 | int final_float = FLOAT_TYPE_P (type); | |
3035 | int final_vec = TREE_CODE (type) == VECTOR_TYPE; | |
3036 | unsigned int final_prec = TYPE_PRECISION (type); | |
3037 | int final_unsignedp = TYPE_UNSIGNED (type); | |
3038 | ||
3aeff048 | 3039 | /* Don't propagate ssa names that occur in abnormal phis. */ |
3040 | if (TREE_CODE (defop0) == SSA_NAME | |
3041 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defop0)) | |
3042 | return 0; | |
3043 | ||
6afd0544 | 3044 | /* In addition to the cases of two conversions in a row |
3045 | handled below, if we are converting something to its own | |
3046 | type via an object of identical or wider precision, neither | |
3047 | conversion is needed. */ | |
3048 | if (useless_type_conversion_p (type, inside_type) | |
3049 | && (((inter_int || inter_ptr) && final_int) | |
3050 | || (inter_float && final_float)) | |
3051 | && inter_prec >= final_prec) | |
3052 | { | |
3053 | gimple_assign_set_rhs1 (stmt, unshare_expr (defop0)); | |
3054 | gimple_assign_set_rhs_code (stmt, TREE_CODE (defop0)); | |
3055 | update_stmt (stmt); | |
89c8f35a | 3056 | return remove_prop_source_from_use (op0) ? 2 : 1; |
6afd0544 | 3057 | } |
3058 | ||
3059 | /* Likewise, if the intermediate and initial types are either both | |
3060 | float or both integer, we don't need the middle conversion if the | |
3061 | former is wider than the latter and doesn't change the signedness | |
3062 | (for integers). Avoid this if the final type is a pointer since | |
3063 | then we sometimes need the middle conversion. Likewise if the | |
3064 | final type has a precision not equal to the size of its mode. */ | |
3065 | if (((inter_int && inside_int) | |
3066 | || (inter_float && inside_float) | |
3067 | || (inter_vec && inside_vec)) | |
3068 | && inter_prec >= inside_prec | |
3069 | && (inter_float || inter_vec | |
3070 | || inter_unsignedp == inside_unsignedp) | |
51dbf409 | 3071 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) |
6afd0544 | 3072 | && TYPE_MODE (type) == TYPE_MODE (inter_type)) |
3073 | && ! final_ptr | |
3074 | && (! final_vec || inter_prec == inside_prec)) | |
3075 | { | |
3076 | gimple_assign_set_rhs1 (stmt, defop0); | |
3077 | update_stmt (stmt); | |
89c8f35a | 3078 | return remove_prop_source_from_use (op0) ? 2 : 1; |
6afd0544 | 3079 | } |
3080 | ||
3081 | /* If we have a sign-extension of a zero-extended value, we can | |
a6476f88 | 3082 | replace that by a single zero-extension. Likewise if the |
3083 | final conversion does not change precision we can drop the | |
3084 | intermediate conversion. */ | |
6afd0544 | 3085 | if (inside_int && inter_int && final_int |
a6476f88 | 3086 | && ((inside_prec < inter_prec && inter_prec < final_prec |
3087 | && inside_unsignedp && !inter_unsignedp) | |
3088 | || final_prec == inter_prec)) | |
6afd0544 | 3089 | { |
3090 | gimple_assign_set_rhs1 (stmt, defop0); | |
3091 | update_stmt (stmt); | |
89c8f35a | 3092 | return remove_prop_source_from_use (op0) ? 2 : 1; |
6afd0544 | 3093 | } |
3094 | ||
3095 | /* Two conversions in a row are not needed unless: | |
3096 | - some conversion is floating-point (overstrict for now), or | |
3097 | - some conversion is a vector (overstrict for now), or | |
3098 | - the intermediate type is narrower than both initial and | |
3099 | final, or | |
3100 | - the intermediate type and innermost type differ in signedness, | |
3101 | and the outermost type is wider than the intermediate, or | |
3102 | - the initial type is a pointer type and the precisions of the | |
3103 | intermediate and final types differ, or | |
3104 | - the final type is a pointer type and the precisions of the | |
3105 | initial and intermediate types differ. */ | |
3106 | if (! inside_float && ! inter_float && ! final_float | |
3107 | && ! inside_vec && ! inter_vec && ! final_vec | |
3108 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
3109 | && ! (inside_int && inter_int | |
3110 | && inter_unsignedp != inside_unsignedp | |
3111 | && inter_prec < final_prec) | |
3112 | && ((inter_unsignedp && inter_prec > inside_prec) | |
3113 | == (final_unsignedp && final_prec > inter_prec)) | |
3114 | && ! (inside_ptr && inter_prec != final_prec) | |
3115 | && ! (final_ptr && inside_prec != inter_prec) | |
51dbf409 | 3116 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) |
6afd0544 | 3117 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) |
3118 | { | |
3119 | gimple_assign_set_rhs1 (stmt, defop0); | |
3120 | update_stmt (stmt); | |
89c8f35a | 3121 | return remove_prop_source_from_use (op0) ? 2 : 1; |
6afd0544 | 3122 | } |
3123 | ||
3124 | /* A truncation to an unsigned type should be canonicalized as | |
3125 | bitwise and of a mask. */ | |
3126 | if (final_int && inter_int && inside_int | |
3127 | && final_prec == inside_prec | |
3128 | && final_prec > inter_prec | |
3129 | && inter_unsignedp) | |
3130 | { | |
3131 | tree tem; | |
3132 | tem = fold_build2 (BIT_AND_EXPR, inside_type, | |
3133 | defop0, | |
e913b5cd | 3134 | wide_int_to_tree |
796b6678 | 3135 | (inside_type, |
3136 | wi::mask (inter_prec, false, | |
3137 | TYPE_PRECISION (inside_type)))); | |
6afd0544 | 3138 | if (!useless_type_conversion_p (type, inside_type)) |
3139 | { | |
3140 | tem = force_gimple_operand_gsi (gsi, tem, true, NULL_TREE, true, | |
3141 | GSI_SAME_STMT); | |
3142 | gimple_assign_set_rhs1 (stmt, tem); | |
3143 | } | |
3144 | else | |
3145 | gimple_assign_set_rhs_from_tree (gsi, tem); | |
3146 | update_stmt (gsi_stmt (*gsi)); | |
89c8f35a | 3147 | return 1; |
6afd0544 | 3148 | } |
487282d5 | 3149 | |
3150 | /* If we are converting an integer to a floating-point that can | |
3151 | represent it exactly and back to an integer, we can skip the | |
3152 | floating-point conversion. */ | |
3153 | if (inside_int && inter_float && final_int && | |
3154 | (unsigned) significand_size (TYPE_MODE (inter_type)) | |
3155 | >= inside_prec - !inside_unsignedp) | |
3156 | { | |
3157 | if (useless_type_conversion_p (type, inside_type)) | |
3158 | { | |
3159 | gimple_assign_set_rhs1 (stmt, unshare_expr (defop0)); | |
3160 | gimple_assign_set_rhs_code (stmt, TREE_CODE (defop0)); | |
3161 | update_stmt (stmt); | |
3162 | return remove_prop_source_from_use (op0) ? 2 : 1; | |
3163 | } | |
3164 | else | |
3165 | { | |
3166 | gimple_assign_set_rhs1 (stmt, defop0); | |
3167 | gimple_assign_set_rhs_code (stmt, CONVERT_EXPR); | |
3168 | update_stmt (stmt); | |
3169 | return remove_prop_source_from_use (op0) ? 2 : 1; | |
3170 | } | |
3171 | } | |
6afd0544 | 3172 | } |
3173 | ||
89c8f35a | 3174 | return 0; |
6afd0544 | 3175 | } |
3176 | ||
dbf51ba1 | 3177 | /* Combine VIEW_CONVERT_EXPRs with their defining statement. */ |
3178 | ||
3179 | static bool | |
3180 | simplify_vce (gimple_stmt_iterator *gsi) | |
3181 | { | |
3182 | gimple stmt = gsi_stmt (*gsi); | |
3183 | tree type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
3184 | ||
3185 | /* Drop useless VIEW_CONVERT_EXPRs. */ | |
3186 | tree op = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0); | |
3187 | if (useless_type_conversion_p (type, TREE_TYPE (op))) | |
3188 | { | |
3189 | gimple_assign_set_rhs1 (stmt, op); | |
3190 | update_stmt (stmt); | |
3191 | return true; | |
3192 | } | |
3193 | ||
3194 | if (TREE_CODE (op) != SSA_NAME) | |
3195 | return false; | |
3196 | ||
3197 | gimple def_stmt = SSA_NAME_DEF_STMT (op); | |
3198 | if (!is_gimple_assign (def_stmt)) | |
3199 | return false; | |
3200 | ||
3201 | tree def_op = gimple_assign_rhs1 (def_stmt); | |
3202 | switch (gimple_assign_rhs_code (def_stmt)) | |
3203 | { | |
3204 | CASE_CONVERT: | |
3205 | /* Strip integral conversions that do not change the precision. */ | |
3206 | if ((INTEGRAL_TYPE_P (TREE_TYPE (op)) | |
3207 | || POINTER_TYPE_P (TREE_TYPE (op))) | |
3208 | && (INTEGRAL_TYPE_P (TREE_TYPE (def_op)) | |
3209 | || POINTER_TYPE_P (TREE_TYPE (def_op))) | |
3210 | && (TYPE_PRECISION (TREE_TYPE (op)) | |
3211 | == TYPE_PRECISION (TREE_TYPE (def_op)))) | |
3212 | { | |
3213 | TREE_OPERAND (gimple_assign_rhs1 (stmt), 0) = def_op; | |
3214 | update_stmt (stmt); | |
3215 | return true; | |
3216 | } | |
3217 | break; | |
3218 | ||
3219 | case VIEW_CONVERT_EXPR: | |
3220 | /* Series of VIEW_CONVERT_EXPRs on register operands can | |
3221 | be contracted. */ | |
3222 | if (TREE_CODE (TREE_OPERAND (def_op, 0)) == SSA_NAME) | |
3223 | { | |
3224 | if (useless_type_conversion_p (type, | |
3225 | TREE_TYPE (TREE_OPERAND (def_op, 0)))) | |
3226 | gimple_assign_set_rhs1 (stmt, TREE_OPERAND (def_op, 0)); | |
3227 | else | |
3228 | TREE_OPERAND (gimple_assign_rhs1 (stmt), 0) | |
3229 | = TREE_OPERAND (def_op, 0); | |
3230 | update_stmt (stmt); | |
3231 | return true; | |
3232 | } | |
3233 | ||
3234 | default:; | |
3235 | } | |
3236 | ||
3237 | return false; | |
3238 | } | |
3239 | ||
173c91d9 | 3240 | /* Combine an element access with a shuffle. Returns true if there were |
3241 | any changes made, else it returns false. */ | |
3242 | ||
3243 | static bool | |
3244 | simplify_bitfield_ref (gimple_stmt_iterator *gsi) | |
3245 | { | |
3246 | gimple stmt = gsi_stmt (*gsi); | |
3247 | gimple def_stmt; | |
3248 | tree op, op0, op1, op2; | |
3249 | tree elem_type; | |
3250 | unsigned idx, n, size; | |
3251 | enum tree_code code; | |
3252 | ||
3253 | op = gimple_assign_rhs1 (stmt); | |
3254 | gcc_checking_assert (TREE_CODE (op) == BIT_FIELD_REF); | |
3255 | ||
3256 | op0 = TREE_OPERAND (op, 0); | |
3257 | if (TREE_CODE (op0) != SSA_NAME | |
3258 | || TREE_CODE (TREE_TYPE (op0)) != VECTOR_TYPE) | |
3259 | return false; | |
3260 | ||
58bf5219 | 3261 | def_stmt = get_prop_source_stmt (op0, false, NULL); |
3262 | if (!def_stmt || !can_propagate_from (def_stmt)) | |
3263 | return false; | |
3264 | ||
3265 | op1 = TREE_OPERAND (op, 1); | |
3266 | op2 = TREE_OPERAND (op, 2); | |
3267 | code = gimple_assign_rhs_code (def_stmt); | |
3268 | ||
3269 | if (code == CONSTRUCTOR) | |
3270 | { | |
3271 | tree tem = fold_ternary (BIT_FIELD_REF, TREE_TYPE (op), | |
3272 | gimple_assign_rhs1 (def_stmt), op1, op2); | |
3273 | if (!tem || !valid_gimple_rhs_p (tem)) | |
3274 | return false; | |
3275 | gimple_assign_set_rhs_from_tree (gsi, tem); | |
3276 | update_stmt (gsi_stmt (*gsi)); | |
3277 | return true; | |
3278 | } | |
3279 | ||
173c91d9 | 3280 | elem_type = TREE_TYPE (TREE_TYPE (op0)); |
3281 | if (TREE_TYPE (op) != elem_type) | |
3282 | return false; | |
3283 | ||
f9ae6f95 | 3284 | size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type)); |
3285 | n = TREE_INT_CST_LOW (op1) / size; | |
173c91d9 | 3286 | if (n != 1) |
3287 | return false; | |
f9ae6f95 | 3288 | idx = TREE_INT_CST_LOW (op2) / size; |
173c91d9 | 3289 | |
173c91d9 | 3290 | if (code == VEC_PERM_EXPR) |
3291 | { | |
3292 | tree p, m, index, tem; | |
3293 | unsigned nelts; | |
3294 | m = gimple_assign_rhs3 (def_stmt); | |
3295 | if (TREE_CODE (m) != VECTOR_CST) | |
3296 | return false; | |
3297 | nelts = VECTOR_CST_NELTS (m); | |
f9ae6f95 | 3298 | idx = TREE_INT_CST_LOW (VECTOR_CST_ELT (m, idx)); |
173c91d9 | 3299 | idx %= 2 * nelts; |
3300 | if (idx < nelts) | |
3301 | { | |
3302 | p = gimple_assign_rhs1 (def_stmt); | |
3303 | } | |
3304 | else | |
3305 | { | |
3306 | p = gimple_assign_rhs2 (def_stmt); | |
3307 | idx -= nelts; | |
3308 | } | |
3309 | index = build_int_cst (TREE_TYPE (TREE_TYPE (m)), idx * size); | |
3310 | tem = build3 (BIT_FIELD_REF, TREE_TYPE (op), | |
ab54bbbd | 3311 | unshare_expr (p), op1, index); |
173c91d9 | 3312 | gimple_assign_set_rhs1 (stmt, tem); |
3313 | fold_stmt (gsi); | |
3314 | update_stmt (gsi_stmt (*gsi)); | |
3315 | return true; | |
3316 | } | |
3317 | ||
3318 | return false; | |
3319 | } | |
3320 | ||
496ec2ad | 3321 | /* Determine whether applying the 2 permutations (mask1 then mask2) |
3322 | gives back one of the input. */ | |
3323 | ||
3324 | static int | |
3325 | is_combined_permutation_identity (tree mask1, tree mask2) | |
3326 | { | |
3327 | tree mask; | |
3328 | unsigned int nelts, i, j; | |
3329 | bool maybe_identity1 = true; | |
3330 | bool maybe_identity2 = true; | |
3331 | ||
3332 | gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST | |
3333 | && TREE_CODE (mask2) == VECTOR_CST); | |
3334 | mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2); | |
3335 | gcc_assert (TREE_CODE (mask) == VECTOR_CST); | |
3336 | ||
3337 | nelts = VECTOR_CST_NELTS (mask); | |
3338 | for (i = 0; i < nelts; i++) | |
3339 | { | |
3340 | tree val = VECTOR_CST_ELT (mask, i); | |
3341 | gcc_assert (TREE_CODE (val) == INTEGER_CST); | |
f9ae6f95 | 3342 | j = TREE_INT_CST_LOW (val) & (2 * nelts - 1); |
496ec2ad | 3343 | if (j == i) |
3344 | maybe_identity2 = false; | |
3345 | else if (j == i + nelts) | |
3346 | maybe_identity1 = false; | |
3347 | else | |
3348 | return 0; | |
3349 | } | |
3350 | return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0; | |
3351 | } | |
3352 | ||
2b9112d6 | 3353 | /* Combine a shuffle with its arguments. Returns 1 if there were any |
3354 | changes made, 2 if cfg-cleanup needs to run. Else it returns 0. */ | |
496ec2ad | 3355 | |
3356 | static int | |
3357 | simplify_permutation (gimple_stmt_iterator *gsi) | |
3358 | { | |
3359 | gimple stmt = gsi_stmt (*gsi); | |
3360 | gimple def_stmt; | |
2b9112d6 | 3361 | tree op0, op1, op2, op3, arg0, arg1; |
3362 | enum tree_code code; | |
ab54bbbd | 3363 | bool single_use_op0 = false; |
496ec2ad | 3364 | |
2b9112d6 | 3365 | gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR); |
496ec2ad | 3366 | |
3367 | op0 = gimple_assign_rhs1 (stmt); | |
3368 | op1 = gimple_assign_rhs2 (stmt); | |
3369 | op2 = gimple_assign_rhs3 (stmt); | |
3370 | ||
496ec2ad | 3371 | if (TREE_CODE (op2) != VECTOR_CST) |
3372 | return 0; | |
3373 | ||
2b9112d6 | 3374 | if (TREE_CODE (op0) == VECTOR_CST) |
3375 | { | |
3376 | code = VECTOR_CST; | |
3377 | arg0 = op0; | |
3378 | } | |
3379 | else if (TREE_CODE (op0) == SSA_NAME) | |
3380 | { | |
ab54bbbd | 3381 | def_stmt = get_prop_source_stmt (op0, false, &single_use_op0); |
3382 | if (!def_stmt || !can_propagate_from (def_stmt)) | |
2b9112d6 | 3383 | return 0; |
496ec2ad | 3384 | |
2b9112d6 | 3385 | code = gimple_assign_rhs_code (def_stmt); |
3386 | arg0 = gimple_assign_rhs1 (def_stmt); | |
3387 | } | |
3388 | else | |
496ec2ad | 3389 | return 0; |
3390 | ||
496ec2ad | 3391 | /* Two consecutive shuffles. */ |
2b9112d6 | 3392 | if (code == VEC_PERM_EXPR) |
496ec2ad | 3393 | { |
3394 | tree orig; | |
3395 | int ident; | |
2b9112d6 | 3396 | |
3397 | if (op0 != op1) | |
3398 | return 0; | |
496ec2ad | 3399 | op3 = gimple_assign_rhs3 (def_stmt); |
3400 | if (TREE_CODE (op3) != VECTOR_CST) | |
3401 | return 0; | |
3402 | ident = is_combined_permutation_identity (op3, op2); | |
3403 | if (!ident) | |
3404 | return 0; | |
3405 | orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt) | |
3406 | : gimple_assign_rhs2 (def_stmt); | |
3407 | gimple_assign_set_rhs1 (stmt, unshare_expr (orig)); | |
3408 | gimple_assign_set_rhs_code (stmt, TREE_CODE (orig)); | |
3409 | gimple_set_num_ops (stmt, 2); | |
3410 | update_stmt (stmt); | |
3411 | return remove_prop_source_from_use (op0) ? 2 : 1; | |
3412 | } | |
3413 | ||
2b9112d6 | 3414 | /* Shuffle of a constructor. */ |
3415 | else if (code == CONSTRUCTOR || code == VECTOR_CST) | |
3416 | { | |
3417 | tree opt; | |
3418 | bool ret = false; | |
3419 | if (op0 != op1) | |
3420 | { | |
ab54bbbd | 3421 | if (TREE_CODE (op0) == SSA_NAME && !single_use_op0) |
2b9112d6 | 3422 | return 0; |
3423 | ||
3424 | if (TREE_CODE (op1) == VECTOR_CST) | |
3425 | arg1 = op1; | |
3426 | else if (TREE_CODE (op1) == SSA_NAME) | |
3427 | { | |
3428 | enum tree_code code2; | |
3429 | ||
ab54bbbd | 3430 | gimple def_stmt2 = get_prop_source_stmt (op1, true, NULL); |
3431 | if (!def_stmt2 || !can_propagate_from (def_stmt2)) | |
2b9112d6 | 3432 | return 0; |
3433 | ||
3434 | code2 = gimple_assign_rhs_code (def_stmt2); | |
3435 | if (code2 != CONSTRUCTOR && code2 != VECTOR_CST) | |
3436 | return 0; | |
3437 | arg1 = gimple_assign_rhs1 (def_stmt2); | |
3438 | } | |
3439 | else | |
3440 | return 0; | |
3441 | } | |
3442 | else | |
3443 | { | |
3444 | /* Already used twice in this statement. */ | |
3445 | if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2) | |
3446 | return 0; | |
3447 | arg1 = arg0; | |
3448 | } | |
9af5ce0c | 3449 | opt = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (op0), arg0, arg1, op2); |
2b9112d6 | 3450 | if (!opt |
9af5ce0c | 3451 | || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST)) |
2b9112d6 | 3452 | return 0; |
3453 | gimple_assign_set_rhs_from_tree (gsi, opt); | |
3454 | update_stmt (gsi_stmt (*gsi)); | |
3455 | if (TREE_CODE (op0) == SSA_NAME) | |
3456 | ret = remove_prop_source_from_use (op0); | |
3457 | if (op0 != op1 && TREE_CODE (op1) == SSA_NAME) | |
3458 | ret |= remove_prop_source_from_use (op1); | |
3459 | return ret ? 2 : 1; | |
3460 | } | |
3461 | ||
3462 | return 0; | |
496ec2ad | 3463 | } |
3464 | ||
6a9e13a2 | 3465 | /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */ |
3466 | ||
3467 | static bool | |
3468 | simplify_vector_constructor (gimple_stmt_iterator *gsi) | |
3469 | { | |
3470 | gimple stmt = gsi_stmt (*gsi); | |
3471 | gimple def_stmt; | |
3472 | tree op, op2, orig, type, elem_type; | |
3473 | unsigned elem_size, nelts, i; | |
3474 | enum tree_code code; | |
3475 | constructor_elt *elt; | |
3476 | unsigned char *sel; | |
3477 | bool maybe_ident; | |
3478 | ||
3479 | gcc_checking_assert (gimple_assign_rhs_code (stmt) == CONSTRUCTOR); | |
3480 | ||
3481 | op = gimple_assign_rhs1 (stmt); | |
3482 | type = TREE_TYPE (op); | |
3483 | gcc_checking_assert (TREE_CODE (type) == VECTOR_TYPE); | |
3484 | ||
3485 | nelts = TYPE_VECTOR_SUBPARTS (type); | |
3486 | elem_type = TREE_TYPE (type); | |
f9ae6f95 | 3487 | elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type)); |
6a9e13a2 | 3488 | |
3489 | sel = XALLOCAVEC (unsigned char, nelts); | |
3490 | orig = NULL; | |
3491 | maybe_ident = true; | |
f1f41a6c | 3492 | FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt) |
6a9e13a2 | 3493 | { |
3494 | tree ref, op1; | |
3495 | ||
3496 | if (i >= nelts) | |
3497 | return false; | |
3498 | ||
3499 | if (TREE_CODE (elt->value) != SSA_NAME) | |
3500 | return false; | |
ab54bbbd | 3501 | def_stmt = get_prop_source_stmt (elt->value, false, NULL); |
3502 | if (!def_stmt) | |
6a9e13a2 | 3503 | return false; |
3504 | code = gimple_assign_rhs_code (def_stmt); | |
3505 | if (code != BIT_FIELD_REF) | |
3506 | return false; | |
3507 | op1 = gimple_assign_rhs1 (def_stmt); | |
3508 | ref = TREE_OPERAND (op1, 0); | |
3509 | if (orig) | |
3510 | { | |
3511 | if (ref != orig) | |
3512 | return false; | |
3513 | } | |
3514 | else | |
3515 | { | |
3516 | if (TREE_CODE (ref) != SSA_NAME) | |
3517 | return false; | |
8a13ba5e | 3518 | if (!useless_type_conversion_p (type, TREE_TYPE (ref))) |
3519 | return false; | |
6a9e13a2 | 3520 | orig = ref; |
3521 | } | |
f9ae6f95 | 3522 | if (TREE_INT_CST_LOW (TREE_OPERAND (op1, 1)) != elem_size) |
6a9e13a2 | 3523 | return false; |
f9ae6f95 | 3524 | sel[i] = TREE_INT_CST_LOW (TREE_OPERAND (op1, 2)) / elem_size; |
6a9e13a2 | 3525 | if (sel[i] != i) maybe_ident = false; |
3526 | } | |
3527 | if (i < nelts) | |
3528 | return false; | |
3529 | ||
3530 | if (maybe_ident) | |
d1938a4b | 3531 | gimple_assign_set_rhs_from_tree (gsi, orig); |
6a9e13a2 | 3532 | else |
3533 | { | |
d1938a4b | 3534 | tree mask_type, *mask_elts; |
3535 | ||
3536 | if (!can_vec_perm_p (TYPE_MODE (type), false, sel)) | |
3537 | return false; | |
3538 | mask_type | |
3539 | = build_vector_type (build_nonstandard_integer_type (elem_size, 1), | |
3540 | nelts); | |
3541 | if (GET_MODE_CLASS (TYPE_MODE (mask_type)) != MODE_VECTOR_INT | |
3542 | || GET_MODE_SIZE (TYPE_MODE (mask_type)) | |
3543 | != GET_MODE_SIZE (TYPE_MODE (type))) | |
6a9e13a2 | 3544 | return false; |
d1938a4b | 3545 | mask_elts = XALLOCAVEC (tree, nelts); |
3546 | for (i = 0; i < nelts; i++) | |
3547 | mask_elts[i] = build_int_cst (TREE_TYPE (mask_type), sel[i]); | |
3548 | op2 = build_vector (mask_type, mask_elts); | |
6a9e13a2 | 3549 | gimple_assign_set_rhs_with_ops_1 (gsi, VEC_PERM_EXPR, orig, orig, op2); |
3550 | } | |
3551 | update_stmt (gsi_stmt (*gsi)); | |
3552 | return true; | |
3553 | } | |
3554 | ||
5a423a75 | 3555 | /* Simplify multiplications. |
3556 | Return true if a transformation applied, otherwise return false. */ | |
3557 | ||
3558 | static bool | |
3559 | simplify_mult (gimple_stmt_iterator *gsi) | |
3560 | { | |
3561 | gimple stmt = gsi_stmt (*gsi); | |
3562 | tree arg1 = gimple_assign_rhs1 (stmt); | |
3563 | tree arg2 = gimple_assign_rhs2 (stmt); | |
3564 | ||
3565 | if (TREE_CODE (arg1) != SSA_NAME) | |
3566 | return false; | |
3567 | ||
3568 | gimple def_stmt = SSA_NAME_DEF_STMT (arg1); | |
3569 | if (!is_gimple_assign (def_stmt)) | |
3570 | return false; | |
3571 | ||
3572 | /* Look through a sign-changing conversion. */ | |
3573 | if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) | |
3574 | { | |
3575 | if (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (def_stmt))) | |
3576 | != TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) | |
3577 | || TREE_CODE (gimple_assign_rhs1 (def_stmt)) != SSA_NAME) | |
3578 | return false; | |
3579 | def_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt)); | |
3580 | if (!is_gimple_assign (def_stmt)) | |
3581 | return false; | |
3582 | } | |
3583 | ||
3584 | if (gimple_assign_rhs_code (def_stmt) == EXACT_DIV_EXPR) | |
3585 | { | |
3586 | if (operand_equal_p (gimple_assign_rhs2 (def_stmt), arg2, 0)) | |
3587 | { | |
3588 | tree res = gimple_assign_rhs1 (def_stmt); | |
3589 | if (useless_type_conversion_p (TREE_TYPE (arg1), TREE_TYPE (res))) | |
3590 | gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (res), res, | |
3591 | NULL_TREE); | |
3592 | else | |
3593 | gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, res, NULL_TREE); | |
3594 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
3595 | update_stmt (stmt); | |
3596 | return true; | |
3597 | } | |
3598 | } | |
3599 | ||
3600 | return false; | |
3601 | } | |
f619ecae | 3602 | |
3603 | ||
3604 | /* Const-and-copy lattice for fold_all_stmts. */ | |
3605 | static vec<tree> lattice; | |
3606 | ||
3607 | /* Primitive "lattice" function for gimple_simplify. */ | |
3608 | ||
3609 | static tree | |
3610 | fwprop_ssa_val (tree name) | |
3611 | { | |
3612 | /* First valueize NAME. */ | |
3613 | if (TREE_CODE (name) == SSA_NAME | |
3614 | && SSA_NAME_VERSION (name) < lattice.length ()) | |
3615 | { | |
3616 | tree val = lattice[SSA_NAME_VERSION (name)]; | |
3617 | if (val) | |
3618 | name = val; | |
3619 | } | |
3620 | /* If NAME is not the only use signal we don't want to continue | |
3621 | matching into its definition. */ | |
3622 | if (TREE_CODE (name) == SSA_NAME | |
3623 | && !has_single_use (name)) | |
3624 | return NULL_TREE; | |
3625 | return name; | |
3626 | } | |
3627 | ||
3628 | /* Fold all stmts using fold_stmt following only single-use chains | |
3629 | and using a simple const-and-copy lattice. */ | |
3630 | ||
3631 | static bool | |
3632 | fold_all_stmts (struct function *fun) | |
3633 | { | |
3634 | bool cfg_changed = false; | |
3635 | ||
3636 | /* Combine stmts with the stmts defining their operands. Do that | |
3637 | in an order that guarantees visiting SSA defs before SSA uses. */ | |
3638 | lattice.create (num_ssa_names); | |
3639 | lattice.quick_grow_cleared (num_ssa_names); | |
3640 | int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun)); | |
3641 | int postorder_num = inverted_post_order_compute (postorder); | |
3642 | for (int i = 0; i < postorder_num; ++i) | |
3643 | { | |
3644 | basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]); | |
3645 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); | |
3646 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
3647 | { | |
3648 | gimple stmt = gsi_stmt (gsi); | |
3649 | gimple orig_stmt = stmt; | |
3650 | ||
3651 | if (fold_stmt (&gsi, fwprop_ssa_val)) | |
3652 | { | |
3653 | stmt = gsi_stmt (gsi); | |
3654 | if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt) | |
3655 | && gimple_purge_dead_eh_edges (bb)) | |
3656 | cfg_changed = true; | |
3657 | /* Cleanup the CFG if we simplified a condition to | |
3658 | true or false. */ | |
3659 | if (gimple_code (stmt) == GIMPLE_COND | |
3660 | && (gimple_cond_true_p (stmt) | |
3661 | || gimple_cond_false_p (stmt))) | |
3662 | cfg_changed = true; | |
3663 | update_stmt (stmt); | |
3664 | } | |
3665 | ||
3666 | /* Fill up the lattice. */ | |
3667 | if (gimple_assign_single_p (stmt)) | |
3668 | { | |
3669 | tree lhs = gimple_assign_lhs (stmt); | |
3670 | tree rhs = gimple_assign_rhs1 (stmt); | |
3671 | if (TREE_CODE (lhs) == SSA_NAME) | |
3672 | { | |
3673 | if (TREE_CODE (rhs) == SSA_NAME) | |
3674 | lattice[SSA_NAME_VERSION (lhs)] = fwprop_ssa_val (rhs); | |
3675 | else if (is_gimple_min_invariant (rhs)) | |
3676 | lattice[SSA_NAME_VERSION (lhs)] = rhs; | |
3677 | else | |
3678 | lattice[SSA_NAME_VERSION (lhs)] = lhs; | |
3679 | } | |
3680 | } | |
3681 | } | |
3682 | } | |
3683 | free (postorder); | |
3684 | lattice.release (); | |
3685 | ||
3686 | return cfg_changed; | |
3687 | } | |
3688 | ||
678b2f5b | 3689 | /* Main entry point for the forward propagation and statement combine |
3690 | optimizer. */ | |
4ee9c684 | 3691 | |
65b0537f | 3692 | namespace { |
3693 | ||
3694 | const pass_data pass_data_forwprop = | |
3695 | { | |
3696 | GIMPLE_PASS, /* type */ | |
3697 | "forwprop", /* name */ | |
3698 | OPTGROUP_NONE, /* optinfo_flags */ | |
65b0537f | 3699 | TV_TREE_FORWPROP, /* tv_id */ |
3700 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
3701 | 0, /* properties_provided */ | |
3702 | 0, /* properties_destroyed */ | |
3703 | 0, /* todo_flags_start */ | |
8b88439e | 3704 | TODO_update_ssa, /* todo_flags_finish */ |
65b0537f | 3705 | }; |
3706 | ||
3707 | class pass_forwprop : public gimple_opt_pass | |
3708 | { | |
3709 | public: | |
3710 | pass_forwprop (gcc::context *ctxt) | |
3711 | : gimple_opt_pass (pass_data_forwprop, ctxt) | |
3712 | {} | |
3713 | ||
3714 | /* opt_pass methods: */ | |
3715 | opt_pass * clone () { return new pass_forwprop (m_ctxt); } | |
3716 | virtual bool gate (function *) { return flag_tree_forwprop; } | |
3717 | virtual unsigned int execute (function *); | |
3718 | ||
3719 | }; // class pass_forwprop | |
3720 | ||
3721 | unsigned int | |
3722 | pass_forwprop::execute (function *fun) | |
4ee9c684 | 3723 | { |
f5c8cff5 | 3724 | basic_block bb; |
c96420f8 | 3725 | unsigned int todoflags = 0; |
4ee9c684 | 3726 | |
148aa112 | 3727 | cfg_changed = false; |
3728 | ||
65b0537f | 3729 | FOR_EACH_BB_FN (bb, fun) |
f5c8cff5 | 3730 | { |
2f5a3c4a | 3731 | gimple_stmt_iterator gsi; |
291d763b | 3732 | |
678b2f5b | 3733 | /* Apply forward propagation to all stmts in the basic-block. |
3734 | Note we update GSI within the loop as necessary. */ | |
75a70cf9 | 3735 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); ) |
291d763b | 3736 | { |
75a70cf9 | 3737 | gimple stmt = gsi_stmt (gsi); |
678b2f5b | 3738 | tree lhs, rhs; |
3739 | enum tree_code code; | |
291d763b | 3740 | |
678b2f5b | 3741 | if (!is_gimple_assign (stmt)) |
291d763b | 3742 | { |
678b2f5b | 3743 | gsi_next (&gsi); |
3744 | continue; | |
3745 | } | |
3a938499 | 3746 | |
678b2f5b | 3747 | lhs = gimple_assign_lhs (stmt); |
3748 | rhs = gimple_assign_rhs1 (stmt); | |
3749 | code = gimple_assign_rhs_code (stmt); | |
3750 | if (TREE_CODE (lhs) != SSA_NAME | |
3751 | || has_zero_uses (lhs)) | |
3752 | { | |
3753 | gsi_next (&gsi); | |
3754 | continue; | |
3755 | } | |
3a938499 | 3756 | |
678b2f5b | 3757 | /* If this statement sets an SSA_NAME to an address, |
3758 | try to propagate the address into the uses of the SSA_NAME. */ | |
3759 | if (code == ADDR_EXPR | |
3760 | /* Handle pointer conversions on invariant addresses | |
3761 | as well, as this is valid gimple. */ | |
3762 | || (CONVERT_EXPR_CODE_P (code) | |
3763 | && TREE_CODE (rhs) == ADDR_EXPR | |
3764 | && POINTER_TYPE_P (TREE_TYPE (lhs)))) | |
3765 | { | |
3766 | tree base = get_base_address (TREE_OPERAND (rhs, 0)); | |
3767 | if ((!base | |
3768 | || !DECL_P (base) | |
3769 | || decl_address_invariant_p (base)) | |
3770 | && !stmt_references_abnormal_ssa_name (stmt) | |
bfb89138 | 3771 | && forward_propagate_addr_expr (lhs, rhs, true)) |
1c4607fd | 3772 | { |
678b2f5b | 3773 | release_defs (stmt); |
678b2f5b | 3774 | gsi_remove (&gsi, true); |
1c4607fd | 3775 | } |
678b2f5b | 3776 | else |
3777 | gsi_next (&gsi); | |
3778 | } | |
cd22a796 | 3779 | else if (code == POINTER_PLUS_EXPR) |
678b2f5b | 3780 | { |
cd22a796 | 3781 | tree off = gimple_assign_rhs2 (stmt); |
3782 | if (TREE_CODE (off) == INTEGER_CST | |
3783 | && can_propagate_from (stmt) | |
3784 | && !simple_iv_increment_p (stmt) | |
678b2f5b | 3785 | /* ??? Better adjust the interface to that function |
3786 | instead of building new trees here. */ | |
3787 | && forward_propagate_addr_expr | |
cd22a796 | 3788 | (lhs, |
3789 | build1_loc (gimple_location (stmt), | |
3790 | ADDR_EXPR, TREE_TYPE (rhs), | |
3791 | fold_build2 (MEM_REF, | |
3792 | TREE_TYPE (TREE_TYPE (rhs)), | |
3793 | rhs, | |
3794 | fold_convert (ptr_type_node, | |
bfb89138 | 3795 | off))), true)) |
ca3c9092 | 3796 | { |
678b2f5b | 3797 | release_defs (stmt); |
678b2f5b | 3798 | gsi_remove (&gsi, true); |
ca3c9092 | 3799 | } |
678b2f5b | 3800 | else if (is_gimple_min_invariant (rhs)) |
6afd0544 | 3801 | { |
678b2f5b | 3802 | /* Make sure to fold &a[0] + off_1 here. */ |
50aacf4c | 3803 | fold_stmt_inplace (&gsi); |
678b2f5b | 3804 | update_stmt (stmt); |
3805 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) | |
6afd0544 | 3806 | gsi_next (&gsi); |
3807 | } | |
291d763b | 3808 | else |
75a70cf9 | 3809 | gsi_next (&gsi); |
291d763b | 3810 | } |
678b2f5b | 3811 | else if (TREE_CODE_CLASS (code) == tcc_comparison) |
b5860aba | 3812 | { |
e3a19533 | 3813 | if (forward_propagate_comparison (&gsi)) |
65b0537f | 3814 | cfg_changed = true; |
b5860aba | 3815 | } |
291d763b | 3816 | else |
75a70cf9 | 3817 | gsi_next (&gsi); |
291d763b | 3818 | } |
678b2f5b | 3819 | |
3820 | /* Combine stmts with the stmts defining their operands. | |
3821 | Note we update GSI within the loop as necessary. */ | |
a7107e58 | 3822 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);) |
678b2f5b | 3823 | { |
3824 | gimple stmt = gsi_stmt (gsi); | |
3825 | bool changed = false; | |
3826 | ||
2f5a3c4a | 3827 | /* Mark stmt as potentially needing revisiting. */ |
3828 | gimple_set_plf (stmt, GF_PLF_1, false); | |
3829 | ||
678b2f5b | 3830 | switch (gimple_code (stmt)) |
3831 | { | |
3832 | case GIMPLE_ASSIGN: | |
3833 | { | |
3834 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
3835 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
3836 | ||
3837 | if ((code == BIT_NOT_EXPR | |
3838 | || code == NEGATE_EXPR) | |
3839 | && TREE_CODE (rhs1) == SSA_NAME) | |
3840 | changed = simplify_not_neg_expr (&gsi); | |
360b78f3 | 3841 | else if (code == COND_EXPR |
3842 | || code == VEC_COND_EXPR) | |
678b2f5b | 3843 | { |
3844 | /* In this case the entire COND_EXPR is in rhs1. */ | |
11b881f5 | 3845 | if (forward_propagate_into_cond (&gsi) |
3846 | || combine_cond_exprs (&gsi)) | |
3847 | { | |
3848 | changed = true; | |
3849 | stmt = gsi_stmt (gsi); | |
3850 | } | |
678b2f5b | 3851 | } |
3852 | else if (TREE_CODE_CLASS (code) == tcc_comparison) | |
3853 | { | |
6f9714b3 | 3854 | int did_something; |
6f9714b3 | 3855 | did_something = forward_propagate_into_comparison (&gsi); |
3856 | if (did_something == 2) | |
3857 | cfg_changed = true; | |
6f9714b3 | 3858 | changed = did_something != 0; |
678b2f5b | 3859 | } |
3b8827a2 | 3860 | else if ((code == PLUS_EXPR |
3861 | || code == BIT_IOR_EXPR | |
3862 | || code == BIT_XOR_EXPR) | |
3863 | && simplify_rotate (&gsi)) | |
3864 | changed = true; | |
678b2f5b | 3865 | else if (code == BIT_AND_EXPR |
3866 | || code == BIT_IOR_EXPR | |
3867 | || code == BIT_XOR_EXPR) | |
3868 | changed = simplify_bitwise_binary (&gsi); | |
5a423a75 | 3869 | else if (code == MULT_EXPR) |
3870 | { | |
3871 | changed = simplify_mult (&gsi); | |
3872 | if (changed | |
3873 | && maybe_clean_or_replace_eh_stmt (stmt, stmt) | |
3874 | && gimple_purge_dead_eh_edges (bb)) | |
3875 | cfg_changed = true; | |
3876 | } | |
678b2f5b | 3877 | else if (code == PLUS_EXPR |
3878 | || code == MINUS_EXPR) | |
5a423a75 | 3879 | { |
3880 | changed = associate_plusminus (&gsi); | |
3881 | if (changed | |
3882 | && maybe_clean_or_replace_eh_stmt (stmt, stmt) | |
3883 | && gimple_purge_dead_eh_edges (bb)) | |
3884 | cfg_changed = true; | |
3885 | } | |
c9c17332 | 3886 | else if (code == POINTER_PLUS_EXPR) |
3887 | changed = associate_pointerplus (&gsi); | |
678b2f5b | 3888 | else if (CONVERT_EXPR_CODE_P (code) |
3889 | || code == FLOAT_EXPR | |
3890 | || code == FIX_TRUNC_EXPR) | |
89c8f35a | 3891 | { |
3892 | int did_something = combine_conversions (&gsi); | |
3893 | if (did_something == 2) | |
3894 | cfg_changed = true; | |
d23e1965 | 3895 | |
3896 | /* If we have a narrowing conversion to an integral | |
3897 | type that is fed by a BIT_AND_EXPR, we might be | |
3898 | able to remove the BIT_AND_EXPR if it merely | |
3899 | masks off bits outside the final type (and nothing | |
3900 | else. */ | |
3901 | if (! did_something) | |
3902 | { | |
3903 | tree outer_type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
3904 | tree inner_type = TREE_TYPE (gimple_assign_rhs1 (stmt)); | |
4c0d6cf7 | 3905 | if (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME |
3906 | && INTEGRAL_TYPE_P (outer_type) | |
d23e1965 | 3907 | && INTEGRAL_TYPE_P (inner_type) |
3908 | && (TYPE_PRECISION (outer_type) | |
3909 | <= TYPE_PRECISION (inner_type))) | |
3910 | did_something = simplify_conversion_from_bitmask (&gsi); | |
3911 | } | |
3912 | ||
89c8f35a | 3913 | changed = did_something != 0; |
3914 | } | |
dbf51ba1 | 3915 | else if (code == VIEW_CONVERT_EXPR) |
3916 | changed = simplify_vce (&gsi); | |
496ec2ad | 3917 | else if (code == VEC_PERM_EXPR) |
3918 | { | |
3919 | int did_something = simplify_permutation (&gsi); | |
3920 | if (did_something == 2) | |
3921 | cfg_changed = true; | |
3922 | changed = did_something != 0; | |
3923 | } | |
173c91d9 | 3924 | else if (code == BIT_FIELD_REF) |
3925 | changed = simplify_bitfield_ref (&gsi); | |
6a9e13a2 | 3926 | else if (code == CONSTRUCTOR |
3927 | && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE) | |
3928 | changed = simplify_vector_constructor (&gsi); | |
678b2f5b | 3929 | break; |
3930 | } | |
3931 | ||
3932 | case GIMPLE_SWITCH: | |
3933 | changed = simplify_gimple_switch (stmt); | |
3934 | break; | |
3935 | ||
3936 | case GIMPLE_COND: | |
3937 | { | |
3938 | int did_something; | |
678b2f5b | 3939 | did_something = forward_propagate_into_gimple_cond (stmt); |
3940 | if (did_something == 2) | |
3941 | cfg_changed = true; | |
678b2f5b | 3942 | changed = did_something != 0; |
3943 | break; | |
3944 | } | |
3945 | ||
3946 | case GIMPLE_CALL: | |
3947 | { | |
3948 | tree callee = gimple_call_fndecl (stmt); | |
3949 | if (callee != NULL_TREE | |
3950 | && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) | |
3951 | changed = simplify_builtin_call (&gsi, callee); | |
3952 | break; | |
3953 | } | |
3954 | ||
3955 | default:; | |
3956 | } | |
3957 | ||
a7107e58 | 3958 | if (changed) |
3959 | { | |
3960 | /* If the stmt changed then re-visit it and the statements | |
3961 | inserted before it. */ | |
2f5a3c4a | 3962 | for (; !gsi_end_p (gsi); gsi_prev (&gsi)) |
3963 | if (gimple_plf (gsi_stmt (gsi), GF_PLF_1)) | |
3964 | break; | |
3965 | if (gsi_end_p (gsi)) | |
a7107e58 | 3966 | gsi = gsi_start_bb (bb); |
3967 | else | |
2f5a3c4a | 3968 | gsi_next (&gsi); |
a7107e58 | 3969 | } |
3970 | else | |
3971 | { | |
2f5a3c4a | 3972 | /* Stmt no longer needs to be revisited. */ |
3973 | gimple_set_plf (stmt, GF_PLF_1, true); | |
a7107e58 | 3974 | gsi_next (&gsi); |
3975 | } | |
678b2f5b | 3976 | } |
f5c8cff5 | 3977 | } |
148aa112 | 3978 | |
f619ecae | 3979 | /* At the end fold all statements. */ |
3980 | cfg_changed |= fold_all_stmts (fun); | |
3981 | ||
148aa112 | 3982 | if (cfg_changed) |
6fa78c7b | 3983 | todoflags |= TODO_cleanup_cfg; |
678b2f5b | 3984 | |
c96420f8 | 3985 | return todoflags; |
4ee9c684 | 3986 | } |
3987 | ||
cbe8bda8 | 3988 | } // anon namespace |
3989 | ||
3990 | gimple_opt_pass * | |
3991 | make_pass_forwprop (gcc::context *ctxt) | |
3992 | { | |
3993 | return new pass_forwprop (ctxt); | |
3994 | } |