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