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