]>
Commit | Line | Data |
---|---|---|
2d18b16d | 1 | /* Statement simplification on GIMPLE. |
3aea1f79 | 2 | Copyright (C) 2010-2014 Free Software Foundation, Inc. |
2d18b16d | 3 | Split out from tree-ssa-ccp.c. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
9 | Free Software Foundation; either version 3, or (at your option) any | |
10 | later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
9ed99284 | 26 | #include "stringpool.h" |
27 | #include "expr.h" | |
28 | #include "stmt.h" | |
29 | #include "stor-layout.h" | |
2d18b16d | 30 | #include "flags.h" |
a3020f2f | 31 | #include "hashtab.h" |
32 | #include "hash-set.h" | |
33 | #include "vec.h" | |
34 | #include "machmode.h" | |
35 | #include "hard-reg-set.h" | |
36 | #include "input.h" | |
2d18b16d | 37 | #include "function.h" |
b9ed1410 | 38 | #include "dumpfile.h" |
073c1fd5 | 39 | #include "bitmap.h" |
bc61cadb | 40 | #include "basic-block.h" |
41 | #include "tree-ssa-alias.h" | |
42 | #include "internal-fn.h" | |
43 | #include "gimple-fold.h" | |
44 | #include "gimple-expr.h" | |
45 | #include "is-a.h" | |
073c1fd5 | 46 | #include "gimple.h" |
a8783bee | 47 | #include "gimplify.h" |
dcf1a1ec | 48 | #include "gimple-iterator.h" |
073c1fd5 | 49 | #include "gimple-ssa.h" |
50 | #include "tree-ssanames.h" | |
51 | #include "tree-into-ssa.h" | |
52 | #include "tree-dfa.h" | |
69ee5dbb | 53 | #include "tree-ssa.h" |
2d18b16d | 54 | #include "tree-ssa-propagate.h" |
2d18b16d | 55 | #include "target.h" |
10fba9c0 | 56 | #include "ipa-utils.h" |
57 | #include "gimple-pretty-print.h" | |
424a4a92 | 58 | #include "tree-ssa-address.h" |
0e80b01d | 59 | #include "langhooks.h" |
f6a34e3f | 60 | #include "gimplify-me.h" |
ceb49bba | 61 | #include "dbgcnt.h" |
f7715905 | 62 | #include "builtins.h" |
b9ea678c | 63 | #include "output.h" |
2d18b16d | 64 | |
551732eb | 65 | /* Return true when DECL can be referenced from current unit. |
a65b88bf | 66 | FROM_DECL (if non-null) specify constructor of variable DECL was taken from. |
67 | We can get declarations that are not possible to reference for various | |
68 | reasons: | |
f1c35659 | 69 | |
f1c35659 | 70 | 1) When analyzing C++ virtual tables. |
71 | C++ virtual tables do have known constructors even | |
72 | when they are keyed to other compilation unit. | |
73 | Those tables can contain pointers to methods and vars | |
74 | in other units. Those methods have both STATIC and EXTERNAL | |
75 | set. | |
76 | 2) In WHOPR mode devirtualization might lead to reference | |
77 | to method that was partitioned elsehwere. | |
78 | In this case we have static VAR_DECL or FUNCTION_DECL | |
79 | that has no corresponding callgraph/varpool node | |
551732eb | 80 | declaring the body. |
81 | 3) COMDAT functions referred by external vtables that | |
125ac0ea | 82 | we devirtualize only during final compilation stage. |
551732eb | 83 | At this time we already decided that we will not output |
84 | the function body and thus we can't reference the symbol | |
85 | directly. */ | |
86 | ||
f1c35659 | 87 | static bool |
a65b88bf | 88 | can_refer_decl_in_current_unit_p (tree decl, tree from_decl) |
f1c35659 | 89 | { |
098f44bc | 90 | varpool_node *vnode; |
f1c35659 | 91 | struct cgraph_node *node; |
452659af | 92 | symtab_node *snode; |
a65b88bf | 93 | |
16d41ae2 | 94 | if (DECL_ABSTRACT_P (decl)) |
2246b6a9 | 95 | return false; |
96 | ||
97 | /* We are concerned only about static/external vars and functions. */ | |
98 | if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) | |
99 | || (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)) | |
100 | return true; | |
101 | ||
102 | /* Static objects can be referred only if they was not optimized out yet. */ | |
103 | if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) | |
104 | { | |
59425620 | 105 | /* Before we start optimizing unreachable code we can be sure all |
106 | static objects are defined. */ | |
35ee1c66 | 107 | if (symtab->function_flags_ready) |
59425620 | 108 | return true; |
415d1b9a | 109 | snode = symtab_node::get (decl); |
59425620 | 110 | if (!snode || !snode->definition) |
2246b6a9 | 111 | return false; |
13cbeaac | 112 | node = dyn_cast <cgraph_node *> (snode); |
2246b6a9 | 113 | return !node || !node->global.inlined_to; |
114 | } | |
115 | ||
8f1c7d19 | 116 | /* We will later output the initializer, so we can refer to it. |
a65b88bf | 117 | So we are concerned only when DECL comes from initializer of |
59425620 | 118 | external var or var that has been optimized out. */ |
a65b88bf | 119 | if (!from_decl |
120 | || TREE_CODE (from_decl) != VAR_DECL | |
59425620 | 121 | || (!DECL_EXTERNAL (from_decl) |
97221fd7 | 122 | && (vnode = varpool_node::get (from_decl)) != NULL |
59425620 | 123 | && vnode->definition) |
8f1c7d19 | 124 | || (flag_ltrans |
97221fd7 | 125 | && (vnode = varpool_node::get (from_decl)) != NULL |
fb5bde82 | 126 | && vnode->in_other_partition)) |
a65b88bf | 127 | return true; |
a65b88bf | 128 | /* We are folding reference from external vtable. The vtable may reffer |
129 | to a symbol keyed to other compilation unit. The other compilation | |
130 | unit may be in separate DSO and the symbol may be hidden. */ | |
131 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
132 | && DECL_EXTERNAL (decl) | |
1bf69827 | 133 | && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT |
415d1b9a | 134 | && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition)) |
a65b88bf | 135 | return false; |
551732eb | 136 | /* When function is public, we always can introduce new reference. |
137 | Exception are the COMDAT functions where introducing a direct | |
138 | reference imply need to include function body in the curren tunit. */ | |
139 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
140 | return true; | |
59425620 | 141 | /* We have COMDAT. We are going to check if we still have definition |
142 | or if the definition is going to be output in other partition. | |
143 | Bypass this when gimplifying; all needed functions will be produced. | |
a65b88bf | 144 | |
145 | As observed in PR20991 for already optimized out comdat virtual functions | |
9d75589a | 146 | it may be tempting to not necessarily give up because the copy will be |
a65b88bf | 147 | output elsewhere when corresponding vtable is output. |
148 | This is however not possible - ABI specify that COMDATs are output in | |
149 | units where they are used and when the other unit was compiled with LTO | |
150 | it is possible that vtable was kept public while the function itself | |
151 | was privatized. */ | |
35ee1c66 | 152 | if (!symtab->function_flags_ready) |
551732eb | 153 | return true; |
a65b88bf | 154 | |
415d1b9a | 155 | snode = symtab_node::get (decl); |
59425620 | 156 | if (!snode |
157 | || ((!snode->definition || DECL_EXTERNAL (decl)) | |
158 | && (!snode->in_other_partition | |
159 | || (!snode->forced_by_abi && !snode->force_output)))) | |
160 | return false; | |
161 | node = dyn_cast <cgraph_node *> (snode); | |
162 | return !node || !node->global.inlined_to; | |
f1c35659 | 163 | } |
164 | ||
bb903e9c | 165 | /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into |
a65b88bf | 166 | acceptable form for is_gimple_min_invariant. |
167 | FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */ | |
15ba153a | 168 | |
169 | tree | |
a65b88bf | 170 | canonicalize_constructor_val (tree cval, tree from_decl) |
15ba153a | 171 | { |
cf649032 | 172 | tree orig_cval = cval; |
173 | STRIP_NOPS (cval); | |
704d7315 | 174 | if (TREE_CODE (cval) == POINTER_PLUS_EXPR |
175 | && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST) | |
15ba153a | 176 | { |
704d7315 | 177 | tree ptr = TREE_OPERAND (cval, 0); |
178 | if (is_gimple_min_invariant (ptr)) | |
179 | cval = build1_loc (EXPR_LOCATION (cval), | |
180 | ADDR_EXPR, TREE_TYPE (ptr), | |
181 | fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)), | |
182 | ptr, | |
183 | fold_convert (ptr_type_node, | |
184 | TREE_OPERAND (cval, 1)))); | |
15ba153a | 185 | } |
186 | if (TREE_CODE (cval) == ADDR_EXPR) | |
187 | { | |
8edd00b2 | 188 | tree base = NULL_TREE; |
189 | if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR) | |
7843e4bc | 190 | { |
191 | base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0)); | |
192 | if (base) | |
193 | TREE_OPERAND (cval, 0) = base; | |
194 | } | |
8edd00b2 | 195 | else |
196 | base = get_base_address (TREE_OPERAND (cval, 0)); | |
e25d4891 | 197 | if (!base) |
198 | return NULL_TREE; | |
551732eb | 199 | |
e25d4891 | 200 | if ((TREE_CODE (base) == VAR_DECL |
201 | || TREE_CODE (base) == FUNCTION_DECL) | |
a65b88bf | 202 | && !can_refer_decl_in_current_unit_p (base, from_decl)) |
f1c35659 | 203 | return NULL_TREE; |
e25d4891 | 204 | if (TREE_CODE (base) == VAR_DECL) |
b03e5397 | 205 | TREE_ADDRESSABLE (base) = 1; |
e25d4891 | 206 | else if (TREE_CODE (base) == FUNCTION_DECL) |
207 | { | |
208 | /* Make sure we create a cgraph node for functions we'll reference. | |
209 | They can be non-existent if the reference comes from an entry | |
210 | of an external vtable for example. */ | |
415d1b9a | 211 | cgraph_node::get_create (base); |
e25d4891 | 212 | } |
bb903e9c | 213 | /* Fixup types in global initializers. */ |
0554476d | 214 | if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0))) |
215 | cval = build_fold_addr_expr (TREE_OPERAND (cval, 0)); | |
cf649032 | 216 | |
217 | if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval))) | |
218 | cval = fold_convert (TREE_TYPE (orig_cval), cval); | |
219 | return cval; | |
15ba153a | 220 | } |
f5faab84 | 221 | if (TREE_OVERFLOW_P (cval)) |
222 | return drop_tree_overflow (cval); | |
cf649032 | 223 | return orig_cval; |
15ba153a | 224 | } |
2d18b16d | 225 | |
226 | /* If SYM is a constant variable with known value, return the value. | |
227 | NULL_TREE is returned otherwise. */ | |
228 | ||
229 | tree | |
230 | get_symbol_constant_value (tree sym) | |
231 | { | |
df8d3e89 | 232 | tree val = ctor_for_folding (sym); |
233 | if (val != error_mark_node) | |
2d18b16d | 234 | { |
2d18b16d | 235 | if (val) |
236 | { | |
8f266cd9 | 237 | val = canonicalize_constructor_val (unshare_expr (val), sym); |
f1c35659 | 238 | if (val && is_gimple_min_invariant (val)) |
15ba153a | 239 | return val; |
f1c35659 | 240 | else |
241 | return NULL_TREE; | |
2d18b16d | 242 | } |
243 | /* Variables declared 'const' without an initializer | |
244 | have zero as the initializer if they may not be | |
245 | overridden at link or run time. */ | |
246 | if (!val | |
2d18b16d | 247 | && (INTEGRAL_TYPE_P (TREE_TYPE (sym)) |
248 | || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym)))) | |
385f3f36 | 249 | return build_zero_cst (TREE_TYPE (sym)); |
2d18b16d | 250 | } |
251 | ||
252 | return NULL_TREE; | |
253 | } | |
254 | ||
255 | ||
2d18b16d | 256 | |
257 | /* Subroutine of fold_stmt. We perform several simplifications of the | |
258 | memory reference tree EXPR and make sure to re-gimplify them properly | |
259 | after propagation of constant addresses. IS_LHS is true if the | |
260 | reference is supposed to be an lvalue. */ | |
261 | ||
262 | static tree | |
263 | maybe_fold_reference (tree expr, bool is_lhs) | |
264 | { | |
15ba153a | 265 | tree result; |
2d18b16d | 266 | |
c701e5d5 | 267 | if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR |
268 | || TREE_CODE (expr) == REALPART_EXPR | |
269 | || TREE_CODE (expr) == IMAGPART_EXPR) | |
270 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
271 | return fold_unary_loc (EXPR_LOCATION (expr), | |
272 | TREE_CODE (expr), | |
273 | TREE_TYPE (expr), | |
274 | TREE_OPERAND (expr, 0)); | |
275 | else if (TREE_CODE (expr) == BIT_FIELD_REF | |
276 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
277 | return fold_ternary_loc (EXPR_LOCATION (expr), | |
278 | TREE_CODE (expr), | |
279 | TREE_TYPE (expr), | |
280 | TREE_OPERAND (expr, 0), | |
281 | TREE_OPERAND (expr, 1), | |
282 | TREE_OPERAND (expr, 2)); | |
283 | ||
c701e5d5 | 284 | if (!is_lhs |
285 | && (result = fold_const_aggregate_ref (expr)) | |
286 | && is_gimple_min_invariant (result)) | |
287 | return result; | |
2d18b16d | 288 | |
2d18b16d | 289 | return NULL_TREE; |
290 | } | |
291 | ||
292 | ||
293 | /* Attempt to fold an assignment statement pointed-to by SI. Returns a | |
294 | replacement rhs for the statement or NULL_TREE if no simplification | |
295 | could be made. It is assumed that the operands have been previously | |
296 | folded. */ | |
297 | ||
298 | static tree | |
299 | fold_gimple_assign (gimple_stmt_iterator *si) | |
300 | { | |
301 | gimple stmt = gsi_stmt (*si); | |
302 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
303 | location_t loc = gimple_location (stmt); | |
304 | ||
305 | tree result = NULL_TREE; | |
306 | ||
307 | switch (get_gimple_rhs_class (subcode)) | |
308 | { | |
309 | case GIMPLE_SINGLE_RHS: | |
310 | { | |
311 | tree rhs = gimple_assign_rhs1 (stmt); | |
312 | ||
8a2caf10 | 313 | if (REFERENCE_CLASS_P (rhs)) |
2d18b16d | 314 | return maybe_fold_reference (rhs, false); |
315 | ||
0329fcdb | 316 | else if (TREE_CODE (rhs) == OBJ_TYPE_REF) |
317 | { | |
318 | tree val = OBJ_TYPE_REF_EXPR (rhs); | |
319 | if (is_gimple_min_invariant (val)) | |
320 | return val; | |
722b5983 | 321 | else if (flag_devirtualize && virtual_method_call_p (rhs)) |
0329fcdb | 322 | { |
323 | bool final; | |
324 | vec <cgraph_node *>targets | |
722b5983 | 325 | = possible_polymorphic_call_targets (rhs, stmt, &final); |
ceb49bba | 326 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
0329fcdb | 327 | { |
ceb49bba | 328 | if (dump_enabled_p ()) |
329 | { | |
4c8041d7 | 330 | location_t loc = gimple_location_safe (stmt); |
ceb49bba | 331 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
332 | "resolving virtual function address " | |
333 | "reference to function %s\n", | |
334 | targets.length () == 1 | |
335 | ? targets[0]->name () | |
6f364420 | 336 | : "NULL"); |
ceb49bba | 337 | } |
6f364420 | 338 | if (targets.length () == 1) |
339 | { | |
340 | val = fold_convert (TREE_TYPE (val), | |
341 | build_fold_addr_expr_loc | |
342 | (loc, targets[0]->decl)); | |
343 | STRIP_USELESS_TYPE_CONVERSION (val); | |
344 | } | |
345 | else | |
346 | /* We can not use __builtin_unreachable here because it | |
347 | can not have address taken. */ | |
348 | val = build_int_cst (TREE_TYPE (val), 0); | |
0329fcdb | 349 | return val; |
350 | } | |
351 | } | |
352 | ||
353 | } | |
2d18b16d | 354 | else if (TREE_CODE (rhs) == ADDR_EXPR) |
355 | { | |
182cf5a9 | 356 | tree ref = TREE_OPERAND (rhs, 0); |
357 | tree tem = maybe_fold_reference (ref, true); | |
358 | if (tem | |
359 | && TREE_CODE (tem) == MEM_REF | |
360 | && integer_zerop (TREE_OPERAND (tem, 1))) | |
361 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0)); | |
362 | else if (tem) | |
2d18b16d | 363 | result = fold_convert (TREE_TYPE (rhs), |
364 | build_fold_addr_expr_loc (loc, tem)); | |
182cf5a9 | 365 | else if (TREE_CODE (ref) == MEM_REF |
366 | && integer_zerop (TREE_OPERAND (ref, 1))) | |
367 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0)); | |
2d18b16d | 368 | } |
369 | ||
370 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
371 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
372 | && (CONSTRUCTOR_NELTS (rhs) | |
373 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
374 | { | |
375 | /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */ | |
376 | unsigned i; | |
377 | tree val; | |
378 | ||
379 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) | |
380 | if (TREE_CODE (val) != INTEGER_CST | |
381 | && TREE_CODE (val) != REAL_CST | |
382 | && TREE_CODE (val) != FIXED_CST) | |
383 | return NULL_TREE; | |
384 | ||
385 | return build_vector_from_ctor (TREE_TYPE (rhs), | |
386 | CONSTRUCTOR_ELTS (rhs)); | |
387 | } | |
388 | ||
389 | else if (DECL_P (rhs)) | |
8f266cd9 | 390 | return get_symbol_constant_value (rhs); |
2d18b16d | 391 | |
392 | /* If we couldn't fold the RHS, hand over to the generic | |
393 | fold routines. */ | |
394 | if (result == NULL_TREE) | |
395 | result = fold (rhs); | |
396 | ||
397 | /* Strip away useless type conversions. Both the NON_LVALUE_EXPR | |
398 | that may have been added by fold, and "useless" type | |
399 | conversions that might now be apparent due to propagation. */ | |
400 | STRIP_USELESS_TYPE_CONVERSION (result); | |
401 | ||
402 | if (result != rhs && valid_gimple_rhs_p (result)) | |
403 | return result; | |
404 | ||
405 | return NULL_TREE; | |
406 | } | |
407 | break; | |
408 | ||
409 | case GIMPLE_UNARY_RHS: | |
410 | { | |
411 | tree rhs = gimple_assign_rhs1 (stmt); | |
412 | ||
413 | result = fold_unary_loc (loc, subcode, gimple_expr_type (stmt), rhs); | |
414 | if (result) | |
415 | { | |
416 | /* If the operation was a conversion do _not_ mark a | |
417 | resulting constant with TREE_OVERFLOW if the original | |
418 | constant was not. These conversions have implementation | |
419 | defined behavior and retaining the TREE_OVERFLOW flag | |
420 | here would confuse later passes such as VRP. */ | |
421 | if (CONVERT_EXPR_CODE_P (subcode) | |
422 | && TREE_CODE (result) == INTEGER_CST | |
423 | && TREE_CODE (rhs) == INTEGER_CST) | |
424 | TREE_OVERFLOW (result) = TREE_OVERFLOW (rhs); | |
425 | ||
426 | STRIP_USELESS_TYPE_CONVERSION (result); | |
427 | if (valid_gimple_rhs_p (result)) | |
428 | return result; | |
429 | } | |
2d18b16d | 430 | } |
431 | break; | |
432 | ||
433 | case GIMPLE_BINARY_RHS: | |
75200312 | 434 | /* Try to canonicalize for boolean-typed X the comparisons |
435 | X == 0, X == 1, X != 0, and X != 1. */ | |
704d7315 | 436 | if (gimple_assign_rhs_code (stmt) == EQ_EXPR |
437 | || gimple_assign_rhs_code (stmt) == NE_EXPR) | |
75200312 | 438 | { |
439 | tree lhs = gimple_assign_lhs (stmt); | |
440 | tree op1 = gimple_assign_rhs1 (stmt); | |
441 | tree op2 = gimple_assign_rhs2 (stmt); | |
442 | tree type = TREE_TYPE (op1); | |
443 | ||
444 | /* Check whether the comparison operands are of the same boolean | |
445 | type as the result type is. | |
446 | Check that second operand is an integer-constant with value | |
447 | one or zero. */ | |
448 | if (TREE_CODE (op2) == INTEGER_CST | |
449 | && (integer_zerop (op2) || integer_onep (op2)) | |
450 | && useless_type_conversion_p (TREE_TYPE (lhs), type)) | |
451 | { | |
452 | enum tree_code cmp_code = gimple_assign_rhs_code (stmt); | |
453 | bool is_logical_not = false; | |
454 | ||
455 | /* X == 0 and X != 1 is a logical-not.of X | |
456 | X == 1 and X != 0 is X */ | |
457 | if ((cmp_code == EQ_EXPR && integer_zerop (op2)) | |
458 | || (cmp_code == NE_EXPR && integer_onep (op2))) | |
459 | is_logical_not = true; | |
460 | ||
461 | if (is_logical_not == false) | |
462 | result = op1; | |
463 | /* Only for one-bit precision typed X the transformation | |
464 | !X -> ~X is valied. */ | |
465 | else if (TYPE_PRECISION (type) == 1) | |
466 | result = build1_loc (gimple_location (stmt), BIT_NOT_EXPR, | |
467 | type, op1); | |
468 | /* Otherwise we use !X -> X ^ 1. */ | |
469 | else | |
470 | result = build2_loc (gimple_location (stmt), BIT_XOR_EXPR, | |
471 | type, op1, build_int_cst (type, 1)); | |
472 | ||
473 | } | |
474 | } | |
2d18b16d | 475 | |
476 | if (!result) | |
477 | result = fold_binary_loc (loc, subcode, | |
fbd25d42 | 478 | TREE_TYPE (gimple_assign_lhs (stmt)), |
479 | gimple_assign_rhs1 (stmt), | |
480 | gimple_assign_rhs2 (stmt)); | |
2d18b16d | 481 | |
482 | if (result) | |
483 | { | |
484 | STRIP_USELESS_TYPE_CONVERSION (result); | |
485 | if (valid_gimple_rhs_p (result)) | |
486 | return result; | |
2d18b16d | 487 | } |
488 | break; | |
489 | ||
00f4f705 | 490 | case GIMPLE_TERNARY_RHS: |
8a2caf10 | 491 | /* Try to fold a conditional expression. */ |
492 | if (gimple_assign_rhs_code (stmt) == COND_EXPR) | |
493 | { | |
494 | tree op0 = gimple_assign_rhs1 (stmt); | |
495 | tree tem; | |
496 | bool set = false; | |
497 | location_t cond_loc = gimple_location (stmt); | |
498 | ||
499 | if (COMPARISON_CLASS_P (op0)) | |
500 | { | |
501 | fold_defer_overflow_warnings (); | |
502 | tem = fold_binary_loc (cond_loc, | |
503 | TREE_CODE (op0), TREE_TYPE (op0), | |
504 | TREE_OPERAND (op0, 0), | |
505 | TREE_OPERAND (op0, 1)); | |
506 | /* This is actually a conditional expression, not a GIMPLE | |
507 | conditional statement, however, the valid_gimple_rhs_p | |
508 | test still applies. */ | |
509 | set = (tem && is_gimple_condexpr (tem) | |
510 | && valid_gimple_rhs_p (tem)); | |
511 | fold_undefer_overflow_warnings (set, stmt, 0); | |
512 | } | |
513 | else if (is_gimple_min_invariant (op0)) | |
514 | { | |
515 | tem = op0; | |
516 | set = true; | |
517 | } | |
518 | else | |
519 | return NULL_TREE; | |
520 | ||
521 | if (set) | |
522 | result = fold_build3_loc (cond_loc, COND_EXPR, | |
523 | TREE_TYPE (gimple_assign_lhs (stmt)), tem, | |
524 | gimple_assign_rhs2 (stmt), | |
525 | gimple_assign_rhs3 (stmt)); | |
526 | } | |
527 | ||
528 | if (!result) | |
529 | result = fold_ternary_loc (loc, subcode, | |
530 | TREE_TYPE (gimple_assign_lhs (stmt)), | |
531 | gimple_assign_rhs1 (stmt), | |
532 | gimple_assign_rhs2 (stmt), | |
533 | gimple_assign_rhs3 (stmt)); | |
00f4f705 | 534 | |
535 | if (result) | |
536 | { | |
537 | STRIP_USELESS_TYPE_CONVERSION (result); | |
538 | if (valid_gimple_rhs_p (result)) | |
539 | return result; | |
00f4f705 | 540 | } |
541 | break; | |
542 | ||
2d18b16d | 543 | case GIMPLE_INVALID_RHS: |
544 | gcc_unreachable (); | |
545 | } | |
546 | ||
547 | return NULL_TREE; | |
548 | } | |
549 | ||
550 | /* Attempt to fold a conditional statement. Return true if any changes were | |
551 | made. We only attempt to fold the condition expression, and do not perform | |
552 | any transformation that would require alteration of the cfg. It is | |
553 | assumed that the operands have been previously folded. */ | |
554 | ||
555 | static bool | |
556 | fold_gimple_cond (gimple stmt) | |
557 | { | |
558 | tree result = fold_binary_loc (gimple_location (stmt), | |
559 | gimple_cond_code (stmt), | |
560 | boolean_type_node, | |
561 | gimple_cond_lhs (stmt), | |
562 | gimple_cond_rhs (stmt)); | |
563 | ||
564 | if (result) | |
565 | { | |
566 | STRIP_USELESS_TYPE_CONVERSION (result); | |
567 | if (is_gimple_condexpr (result) && valid_gimple_rhs_p (result)) | |
568 | { | |
569 | gimple_cond_set_condition_from_tree (stmt, result); | |
570 | return true; | |
571 | } | |
572 | } | |
573 | ||
574 | return false; | |
575 | } | |
576 | ||
b9ea678c | 577 | |
578 | /* Replace a statement at *SI_P with a sequence of statements in STMTS, | |
579 | adjusting the replacement stmts location and virtual operands. | |
580 | If the statement has a lhs the last stmt in the sequence is expected | |
581 | to assign to that lhs. */ | |
582 | ||
583 | static void | |
584 | gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts) | |
585 | { | |
586 | gimple stmt = gsi_stmt (*si_p); | |
587 | ||
588 | if (gimple_has_location (stmt)) | |
589 | annotate_all_with_location (stmts, gimple_location (stmt)); | |
590 | ||
591 | /* First iterate over the replacement statements backward, assigning | |
592 | virtual operands to their defining statements. */ | |
593 | gimple laststore = NULL; | |
594 | for (gimple_stmt_iterator i = gsi_last (stmts); | |
595 | !gsi_end_p (i); gsi_prev (&i)) | |
596 | { | |
597 | gimple new_stmt = gsi_stmt (i); | |
598 | if ((gimple_assign_single_p (new_stmt) | |
599 | && !is_gimple_reg (gimple_assign_lhs (new_stmt))) | |
600 | || (is_gimple_call (new_stmt) | |
601 | && (gimple_call_flags (new_stmt) | |
602 | & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0)) | |
603 | { | |
604 | tree vdef; | |
605 | if (!laststore) | |
606 | vdef = gimple_vdef (stmt); | |
607 | else | |
608 | vdef = make_ssa_name (gimple_vop (cfun), new_stmt); | |
609 | gimple_set_vdef (new_stmt, vdef); | |
610 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
611 | SSA_NAME_DEF_STMT (vdef) = new_stmt; | |
612 | laststore = new_stmt; | |
613 | } | |
614 | } | |
615 | ||
616 | /* Second iterate over the statements forward, assigning virtual | |
617 | operands to their uses. */ | |
618 | tree reaching_vuse = gimple_vuse (stmt); | |
619 | for (gimple_stmt_iterator i = gsi_start (stmts); | |
620 | !gsi_end_p (i); gsi_next (&i)) | |
621 | { | |
622 | gimple new_stmt = gsi_stmt (i); | |
623 | /* If the new statement possibly has a VUSE, update it with exact SSA | |
624 | name we know will reach this one. */ | |
625 | if (gimple_has_mem_ops (new_stmt)) | |
626 | gimple_set_vuse (new_stmt, reaching_vuse); | |
627 | gimple_set_modified (new_stmt, true); | |
628 | if (gimple_vdef (new_stmt)) | |
629 | reaching_vuse = gimple_vdef (new_stmt); | |
630 | } | |
631 | ||
632 | /* If the new sequence does not do a store release the virtual | |
633 | definition of the original statement. */ | |
634 | if (reaching_vuse | |
635 | && reaching_vuse == gimple_vuse (stmt)) | |
636 | { | |
637 | tree vdef = gimple_vdef (stmt); | |
638 | if (vdef | |
639 | && TREE_CODE (vdef) == SSA_NAME) | |
640 | { | |
641 | unlink_stmt_vdef (stmt); | |
642 | release_ssa_name (vdef); | |
643 | } | |
644 | } | |
645 | ||
646 | /* Finally replace the original statement with the sequence. */ | |
647 | gsi_replace_with_seq (si_p, stmts, false); | |
648 | } | |
649 | ||
2d18b16d | 650 | /* Convert EXPR into a GIMPLE value suitable for substitution on the |
651 | RHS of an assignment. Insert the necessary statements before | |
652 | iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL | |
653 | is replaced. If the call is expected to produces a result, then it | |
654 | is replaced by an assignment of the new RHS to the result variable. | |
655 | If the result is to be ignored, then the call is replaced by a | |
3aadae2d | 656 | GIMPLE_NOP. A proper VDEF chain is retained by making the first |
657 | VUSE and the last VDEF of the whole sequence be the same as the replaced | |
658 | statement and using new SSA names for stores in between. */ | |
2d18b16d | 659 | |
660 | void | |
661 | gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr) | |
662 | { | |
663 | tree lhs; | |
2d18b16d | 664 | gimple stmt, new_stmt; |
665 | gimple_stmt_iterator i; | |
e3a19533 | 666 | gimple_seq stmts = NULL; |
2d18b16d | 667 | |
668 | stmt = gsi_stmt (*si_p); | |
669 | ||
670 | gcc_assert (is_gimple_call (stmt)); | |
671 | ||
8a4a28a8 | 672 | push_gimplify_context (gimple_in_ssa_p (cfun)); |
2d18b16d | 673 | |
34e73149 | 674 | lhs = gimple_call_lhs (stmt); |
2d18b16d | 675 | if (lhs == NULL_TREE) |
a3087021 | 676 | { |
677 | gimplify_and_add (expr, &stmts); | |
678 | /* We can end up with folding a memcpy of an empty class assignment | |
679 | which gets optimized away by C++ gimplification. */ | |
680 | if (gimple_seq_empty_p (stmts)) | |
681 | { | |
ec4a428c | 682 | pop_gimplify_context (NULL); |
a3087021 | 683 | if (gimple_in_ssa_p (cfun)) |
684 | { | |
685 | unlink_stmt_vdef (stmt); | |
686 | release_defs (stmt); | |
687 | } | |
ce9bb3b3 | 688 | gsi_replace (si_p, gimple_build_nop (), true); |
a3087021 | 689 | return; |
690 | } | |
691 | } | |
2d18b16d | 692 | else |
34e73149 | 693 | { |
694 | tree tmp = get_initialized_tmp_var (expr, &stmts, NULL); | |
695 | new_stmt = gimple_build_assign (lhs, tmp); | |
696 | i = gsi_last (stmts); | |
697 | gsi_insert_after_without_update (&i, new_stmt, | |
698 | GSI_CONTINUE_LINKING); | |
699 | } | |
2d18b16d | 700 | |
701 | pop_gimplify_context (NULL); | |
702 | ||
b9ea678c | 703 | gsi_replace_with_seq_vops (si_p, stmts); |
704 | } | |
2d18b16d | 705 | |
b9ea678c | 706 | |
707 | /* Replace the call at *GSI with the gimple value VAL. */ | |
708 | ||
709 | static void | |
710 | replace_call_with_value (gimple_stmt_iterator *gsi, tree val) | |
711 | { | |
712 | gimple stmt = gsi_stmt (*gsi); | |
713 | tree lhs = gimple_call_lhs (stmt); | |
714 | gimple repl; | |
715 | if (lhs) | |
34e73149 | 716 | { |
b9ea678c | 717 | if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val))) |
718 | val = fold_convert (TREE_TYPE (lhs), val); | |
719 | repl = gimple_build_assign (lhs, val); | |
720 | } | |
721 | else | |
722 | repl = gimple_build_nop (); | |
723 | tree vdef = gimple_vdef (stmt); | |
724 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
725 | { | |
726 | unlink_stmt_vdef (stmt); | |
727 | release_ssa_name (vdef); | |
728 | } | |
729 | gsi_replace (gsi, repl, true); | |
730 | } | |
731 | ||
732 | /* Replace the call at *GSI with the new call REPL and fold that | |
733 | again. */ | |
734 | ||
735 | static void | |
736 | replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple repl) | |
737 | { | |
738 | gimple stmt = gsi_stmt (*gsi); | |
739 | gimple_call_set_lhs (repl, gimple_call_lhs (stmt)); | |
740 | gimple_set_location (repl, gimple_location (stmt)); | |
741 | if (gimple_vdef (stmt) | |
742 | && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) | |
743 | { | |
744 | gimple_set_vdef (repl, gimple_vdef (stmt)); | |
745 | gimple_set_vuse (repl, gimple_vuse (stmt)); | |
746 | SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl; | |
747 | } | |
748 | gsi_replace (gsi, repl, true); | |
749 | fold_stmt (gsi); | |
750 | } | |
751 | ||
752 | /* Return true if VAR is a VAR_DECL or a component thereof. */ | |
753 | ||
754 | static bool | |
755 | var_decl_component_p (tree var) | |
756 | { | |
757 | tree inner = var; | |
758 | while (handled_component_p (inner)) | |
759 | inner = TREE_OPERAND (inner, 0); | |
760 | return SSA_VAR_P (inner); | |
761 | } | |
762 | ||
763 | /* Fold function call to builtin mem{{,p}cpy,move}. Return | |
764 | NULL_TREE if no simplification can be made. | |
765 | If ENDP is 0, return DEST (like memcpy). | |
766 | If ENDP is 1, return DEST+LEN (like mempcpy). | |
767 | If ENDP is 2, return DEST+LEN-1 (like stpcpy). | |
768 | If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap | |
769 | (memmove). */ | |
770 | ||
771 | static bool | |
772 | gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, | |
773 | tree dest, tree src, int endp) | |
774 | { | |
775 | gimple stmt = gsi_stmt (*gsi); | |
776 | tree lhs = gimple_call_lhs (stmt); | |
777 | tree len = gimple_call_arg (stmt, 2); | |
778 | tree destvar, srcvar; | |
779 | location_t loc = gimple_location (stmt); | |
780 | ||
781 | /* If the LEN parameter is zero, return DEST. */ | |
782 | if (integer_zerop (len)) | |
783 | { | |
784 | gimple repl; | |
785 | if (gimple_call_lhs (stmt)) | |
786 | repl = gimple_build_assign (gimple_call_lhs (stmt), dest); | |
787 | else | |
788 | repl = gimple_build_nop (); | |
789 | tree vdef = gimple_vdef (stmt); | |
790 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
34e73149 | 791 | { |
b9ea678c | 792 | unlink_stmt_vdef (stmt); |
793 | release_ssa_name (vdef); | |
794 | } | |
795 | gsi_replace (gsi, repl, true); | |
796 | return true; | |
797 | } | |
798 | ||
799 | /* If SRC and DEST are the same (and not volatile), return | |
800 | DEST{,+LEN,+LEN-1}. */ | |
801 | if (operand_equal_p (src, dest, 0)) | |
802 | { | |
803 | unlink_stmt_vdef (stmt); | |
804 | if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) | |
805 | release_ssa_name (gimple_vdef (stmt)); | |
806 | if (!lhs) | |
807 | { | |
808 | gsi_replace (gsi, gimple_build_nop (), true); | |
809 | return true; | |
810 | } | |
811 | goto done; | |
812 | } | |
813 | else | |
814 | { | |
815 | tree srctype, desttype; | |
816 | unsigned int src_align, dest_align; | |
817 | tree off0; | |
818 | ||
819 | /* Build accesses at offset zero with a ref-all character type. */ | |
820 | off0 = build_int_cst (build_pointer_type_for_mode (char_type_node, | |
821 | ptr_mode, true), 0); | |
822 | ||
823 | /* If we can perform the copy efficiently with first doing all loads | |
824 | and then all stores inline it that way. Currently efficiently | |
825 | means that we can load all the memory into a single integer | |
826 | register which is what MOVE_MAX gives us. */ | |
827 | src_align = get_pointer_alignment (src); | |
828 | dest_align = get_pointer_alignment (dest); | |
829 | if (tree_fits_uhwi_p (len) | |
830 | && compare_tree_int (len, MOVE_MAX) <= 0 | |
831 | /* ??? Don't transform copies from strings with known length this | |
832 | confuses the tree-ssa-strlen.c. This doesn't handle | |
833 | the case in gcc.dg/strlenopt-8.c which is XFAILed for that | |
834 | reason. */ | |
835 | && !c_strlen (src, 2)) | |
836 | { | |
837 | unsigned ilen = tree_to_uhwi (len); | |
838 | if (exact_log2 (ilen) != -1) | |
839 | { | |
840 | tree type = lang_hooks.types.type_for_size (ilen * 8, 1); | |
841 | if (type | |
842 | && TYPE_MODE (type) != BLKmode | |
843 | && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT | |
844 | == ilen * 8) | |
845 | /* If the destination pointer is not aligned we must be able | |
846 | to emit an unaligned store. */ | |
847 | && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type)) | |
848 | || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align))) | |
849 | { | |
850 | tree srctype = type; | |
851 | tree desttype = type; | |
852 | if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))) | |
853 | srctype = build_aligned_type (type, src_align); | |
854 | tree srcmem = fold_build2 (MEM_REF, srctype, src, off0); | |
855 | tree tem = fold_const_aggregate_ref (srcmem); | |
856 | if (tem) | |
857 | srcmem = tem; | |
858 | else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)) | |
859 | && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), | |
860 | src_align)) | |
861 | srcmem = NULL_TREE; | |
862 | if (srcmem) | |
863 | { | |
864 | gimple new_stmt; | |
865 | if (is_gimple_reg_type (TREE_TYPE (srcmem))) | |
866 | { | |
867 | new_stmt = gimple_build_assign (NULL_TREE, srcmem); | |
868 | if (gimple_in_ssa_p (cfun)) | |
869 | srcmem = make_ssa_name (TREE_TYPE (srcmem), | |
870 | new_stmt); | |
871 | else | |
872 | srcmem = create_tmp_reg (TREE_TYPE (srcmem), | |
873 | NULL); | |
874 | gimple_assign_set_lhs (new_stmt, srcmem); | |
875 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
876 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
877 | } | |
878 | if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))) | |
879 | desttype = build_aligned_type (type, dest_align); | |
880 | new_stmt | |
881 | = gimple_build_assign (fold_build2 (MEM_REF, desttype, | |
882 | dest, off0), | |
883 | srcmem); | |
884 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
885 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
886 | if (gimple_vdef (new_stmt) | |
887 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
888 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
889 | if (!lhs) | |
890 | { | |
891 | gsi_replace (gsi, new_stmt, true); | |
892 | return true; | |
893 | } | |
894 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
895 | goto done; | |
896 | } | |
897 | } | |
898 | } | |
899 | } | |
900 | ||
901 | if (endp == 3) | |
902 | { | |
903 | /* Both DEST and SRC must be pointer types. | |
904 | ??? This is what old code did. Is the testing for pointer types | |
905 | really mandatory? | |
906 | ||
907 | If either SRC is readonly or length is 1, we can use memcpy. */ | |
908 | if (!dest_align || !src_align) | |
909 | return false; | |
910 | if (readonly_data_expr (src) | |
911 | || (tree_fits_uhwi_p (len) | |
912 | && (MIN (src_align, dest_align) / BITS_PER_UNIT | |
913 | >= tree_to_uhwi (len)))) | |
914 | { | |
915 | tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
916 | if (!fn) | |
917 | return false; | |
918 | gimple_call_set_fndecl (stmt, fn); | |
919 | gimple_call_set_arg (stmt, 0, dest); | |
920 | gimple_call_set_arg (stmt, 1, src); | |
921 | fold_stmt (gsi); | |
922 | return true; | |
923 | } | |
924 | ||
925 | /* If *src and *dest can't overlap, optimize into memcpy as well. */ | |
926 | if (TREE_CODE (src) == ADDR_EXPR | |
927 | && TREE_CODE (dest) == ADDR_EXPR) | |
928 | { | |
929 | tree src_base, dest_base, fn; | |
930 | HOST_WIDE_INT src_offset = 0, dest_offset = 0; | |
931 | HOST_WIDE_INT size = -1; | |
932 | HOST_WIDE_INT maxsize = -1; | |
933 | ||
934 | srcvar = TREE_OPERAND (src, 0); | |
935 | src_base = get_ref_base_and_extent (srcvar, &src_offset, | |
936 | &size, &maxsize); | |
937 | destvar = TREE_OPERAND (dest, 0); | |
938 | dest_base = get_ref_base_and_extent (destvar, &dest_offset, | |
939 | &size, &maxsize); | |
940 | if (tree_fits_uhwi_p (len)) | |
941 | maxsize = tree_to_uhwi (len); | |
942 | else | |
943 | maxsize = -1; | |
944 | src_offset /= BITS_PER_UNIT; | |
945 | dest_offset /= BITS_PER_UNIT; | |
946 | if (SSA_VAR_P (src_base) | |
947 | && SSA_VAR_P (dest_base)) | |
948 | { | |
949 | if (operand_equal_p (src_base, dest_base, 0) | |
950 | && ranges_overlap_p (src_offset, maxsize, | |
951 | dest_offset, maxsize)) | |
952 | return false; | |
953 | } | |
954 | else if (TREE_CODE (src_base) == MEM_REF | |
955 | && TREE_CODE (dest_base) == MEM_REF) | |
956 | { | |
957 | if (! operand_equal_p (TREE_OPERAND (src_base, 0), | |
958 | TREE_OPERAND (dest_base, 0), 0)) | |
959 | return false; | |
960 | offset_int off = mem_ref_offset (src_base) + src_offset; | |
961 | if (!wi::fits_shwi_p (off)) | |
962 | return false; | |
963 | src_offset = off.to_shwi (); | |
964 | ||
965 | off = mem_ref_offset (dest_base) + dest_offset; | |
966 | if (!wi::fits_shwi_p (off)) | |
967 | return false; | |
968 | dest_offset = off.to_shwi (); | |
969 | if (ranges_overlap_p (src_offset, maxsize, | |
970 | dest_offset, maxsize)) | |
971 | return false; | |
972 | } | |
973 | else | |
974 | return false; | |
975 | ||
976 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
977 | if (!fn) | |
978 | return false; | |
979 | gimple_call_set_fndecl (stmt, fn); | |
980 | gimple_call_set_arg (stmt, 0, dest); | |
981 | gimple_call_set_arg (stmt, 1, src); | |
982 | fold_stmt (gsi); | |
983 | return true; | |
984 | } | |
985 | ||
986 | /* If the destination and source do not alias optimize into | |
987 | memcpy as well. */ | |
988 | if ((is_gimple_min_invariant (dest) | |
989 | || TREE_CODE (dest) == SSA_NAME) | |
990 | && (is_gimple_min_invariant (src) | |
991 | || TREE_CODE (src) == SSA_NAME)) | |
992 | { | |
993 | ao_ref destr, srcr; | |
994 | ao_ref_init_from_ptr_and_size (&destr, dest, len); | |
995 | ao_ref_init_from_ptr_and_size (&srcr, src, len); | |
996 | if (!refs_may_alias_p_1 (&destr, &srcr, false)) | |
997 | { | |
998 | tree fn; | |
999 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1000 | if (!fn) | |
1001 | return false; | |
1002 | gimple_call_set_fndecl (stmt, fn); | |
1003 | gimple_call_set_arg (stmt, 0, dest); | |
1004 | gimple_call_set_arg (stmt, 1, src); | |
1005 | fold_stmt (gsi); | |
1006 | return true; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | return false; | |
1011 | } | |
1012 | ||
1013 | if (!tree_fits_shwi_p (len)) | |
1014 | return false; | |
1015 | /* FIXME: | |
1016 | This logic lose for arguments like (type *)malloc (sizeof (type)), | |
1017 | since we strip the casts of up to VOID return value from malloc. | |
1018 | Perhaps we ought to inherit type from non-VOID argument here? */ | |
1019 | STRIP_NOPS (src); | |
1020 | STRIP_NOPS (dest); | |
1021 | if (!POINTER_TYPE_P (TREE_TYPE (src)) | |
1022 | || !POINTER_TYPE_P (TREE_TYPE (dest))) | |
1023 | return false; | |
1024 | /* In the following try to find a type that is most natural to be | |
1025 | used for the memcpy source and destination and that allows | |
1026 | the most optimization when memcpy is turned into a plain assignment | |
1027 | using that type. In theory we could always use a char[len] type | |
1028 | but that only gains us that the destination and source possibly | |
1029 | no longer will have their address taken. */ | |
1030 | /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */ | |
1031 | if (TREE_CODE (src) == POINTER_PLUS_EXPR) | |
1032 | { | |
1033 | tree tem = TREE_OPERAND (src, 0); | |
1034 | STRIP_NOPS (tem); | |
1035 | if (tem != TREE_OPERAND (src, 0)) | |
1036 | src = build1 (NOP_EXPR, TREE_TYPE (tem), src); | |
1037 | } | |
1038 | if (TREE_CODE (dest) == POINTER_PLUS_EXPR) | |
1039 | { | |
1040 | tree tem = TREE_OPERAND (dest, 0); | |
1041 | STRIP_NOPS (tem); | |
1042 | if (tem != TREE_OPERAND (dest, 0)) | |
1043 | dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest); | |
1044 | } | |
1045 | srctype = TREE_TYPE (TREE_TYPE (src)); | |
1046 | if (TREE_CODE (srctype) == ARRAY_TYPE | |
1047 | && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) | |
1048 | { | |
1049 | srctype = TREE_TYPE (srctype); | |
1050 | STRIP_NOPS (src); | |
1051 | src = build1 (NOP_EXPR, build_pointer_type (srctype), src); | |
1052 | } | |
1053 | desttype = TREE_TYPE (TREE_TYPE (dest)); | |
1054 | if (TREE_CODE (desttype) == ARRAY_TYPE | |
1055 | && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
1056 | { | |
1057 | desttype = TREE_TYPE (desttype); | |
1058 | STRIP_NOPS (dest); | |
1059 | dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest); | |
1060 | } | |
1061 | if (TREE_ADDRESSABLE (srctype) | |
1062 | || TREE_ADDRESSABLE (desttype)) | |
1063 | return false; | |
1064 | ||
1065 | /* Make sure we are not copying using a floating-point mode or | |
1066 | a type whose size possibly does not match its precision. */ | |
1067 | if (FLOAT_MODE_P (TYPE_MODE (desttype)) | |
1068 | || TREE_CODE (desttype) == BOOLEAN_TYPE | |
1069 | || TREE_CODE (desttype) == ENUMERAL_TYPE) | |
1070 | desttype = bitwise_type_for_mode (TYPE_MODE (desttype)); | |
1071 | if (FLOAT_MODE_P (TYPE_MODE (srctype)) | |
1072 | || TREE_CODE (srctype) == BOOLEAN_TYPE | |
1073 | || TREE_CODE (srctype) == ENUMERAL_TYPE) | |
1074 | srctype = bitwise_type_for_mode (TYPE_MODE (srctype)); | |
1075 | if (!srctype) | |
1076 | srctype = desttype; | |
1077 | if (!desttype) | |
1078 | desttype = srctype; | |
1079 | if (!srctype) | |
1080 | return false; | |
1081 | ||
1082 | src_align = get_pointer_alignment (src); | |
1083 | dest_align = get_pointer_alignment (dest); | |
1084 | if (dest_align < TYPE_ALIGN (desttype) | |
1085 | || src_align < TYPE_ALIGN (srctype)) | |
1086 | return false; | |
1087 | ||
1088 | destvar = dest; | |
1089 | STRIP_NOPS (destvar); | |
1090 | if (TREE_CODE (destvar) == ADDR_EXPR | |
1091 | && var_decl_component_p (TREE_OPERAND (destvar, 0)) | |
1092 | && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
1093 | destvar = fold_build2 (MEM_REF, desttype, destvar, off0); | |
1094 | else | |
1095 | destvar = NULL_TREE; | |
1096 | ||
1097 | srcvar = src; | |
1098 | STRIP_NOPS (srcvar); | |
1099 | if (TREE_CODE (srcvar) == ADDR_EXPR | |
1100 | && var_decl_component_p (TREE_OPERAND (srcvar, 0)) | |
1101 | && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) | |
1102 | { | |
1103 | if (!destvar | |
1104 | || src_align >= TYPE_ALIGN (desttype)) | |
1105 | srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype, | |
1106 | srcvar, off0); | |
1107 | else if (!STRICT_ALIGNMENT) | |
1108 | { | |
1109 | srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype), | |
1110 | src_align); | |
1111 | srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0); | |
1112 | } | |
34e73149 | 1113 | else |
b9ea678c | 1114 | srcvar = NULL_TREE; |
1115 | } | |
1116 | else | |
1117 | srcvar = NULL_TREE; | |
1118 | ||
1119 | if (srcvar == NULL_TREE && destvar == NULL_TREE) | |
1120 | return false; | |
1121 | ||
1122 | if (srcvar == NULL_TREE) | |
1123 | { | |
1124 | STRIP_NOPS (src); | |
1125 | if (src_align >= TYPE_ALIGN (desttype)) | |
1126 | srcvar = fold_build2 (MEM_REF, desttype, src, off0); | |
1127 | else | |
1128 | { | |
1129 | if (STRICT_ALIGNMENT) | |
1130 | return false; | |
1131 | srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype), | |
1132 | src_align); | |
1133 | srcvar = fold_build2 (MEM_REF, srctype, src, off0); | |
1134 | } | |
1135 | } | |
1136 | else if (destvar == NULL_TREE) | |
1137 | { | |
1138 | STRIP_NOPS (dest); | |
1139 | if (dest_align >= TYPE_ALIGN (srctype)) | |
1140 | destvar = fold_build2 (MEM_REF, srctype, dest, off0); | |
1141 | else | |
1142 | { | |
1143 | if (STRICT_ALIGNMENT) | |
1144 | return false; | |
1145 | desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype), | |
1146 | dest_align); | |
1147 | destvar = fold_build2 (MEM_REF, desttype, dest, off0); | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | gimple new_stmt; | |
1152 | if (is_gimple_reg_type (TREE_TYPE (srcvar))) | |
1153 | { | |
1154 | new_stmt = gimple_build_assign (NULL_TREE, srcvar); | |
1155 | if (gimple_in_ssa_p (cfun)) | |
1156 | srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt); | |
1157 | else | |
1158 | srcvar = create_tmp_reg (TREE_TYPE (srcvar), NULL); | |
1159 | gimple_assign_set_lhs (new_stmt, srcvar); | |
1160 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
1161 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1162 | } | |
1163 | new_stmt = gimple_build_assign (destvar, srcvar); | |
1164 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
1165 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
1166 | if (gimple_vdef (new_stmt) | |
1167 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
1168 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
1169 | if (!lhs) | |
1170 | { | |
1171 | gsi_replace (gsi, new_stmt, true); | |
1172 | return true; | |
1173 | } | |
1174 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1175 | } | |
1176 | ||
1177 | done: | |
1178 | if (endp == 0 || endp == 3) | |
1179 | len = NULL_TREE; | |
1180 | else if (endp == 2) | |
1181 | len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len, | |
1182 | ssize_int (1)); | |
1183 | if (endp == 2 || endp == 1) | |
1184 | dest = fold_build_pointer_plus_loc (loc, dest, len); | |
1185 | ||
1186 | dest = force_gimple_operand_gsi (gsi, dest, false, NULL_TREE, true, | |
1187 | GSI_SAME_STMT); | |
1188 | gimple repl = gimple_build_assign (lhs, dest); | |
1189 | gsi_replace (gsi, repl, true); | |
1190 | return true; | |
1191 | } | |
1192 | ||
1193 | /* Fold function call to builtin memset or bzero at *GSI setting the | |
1194 | memory of size LEN to VAL. Return whether a simplification was made. */ | |
1195 | ||
1196 | static bool | |
1197 | gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len) | |
1198 | { | |
1199 | gimple stmt = gsi_stmt (*gsi); | |
1200 | tree etype; | |
1201 | unsigned HOST_WIDE_INT length, cval; | |
1202 | ||
1203 | /* If the LEN parameter is zero, return DEST. */ | |
1204 | if (integer_zerop (len)) | |
1205 | { | |
1206 | replace_call_with_value (gsi, gimple_call_arg (stmt, 0)); | |
1207 | return true; | |
1208 | } | |
1209 | ||
1210 | if (! tree_fits_uhwi_p (len)) | |
1211 | return false; | |
1212 | ||
1213 | if (TREE_CODE (c) != INTEGER_CST) | |
1214 | return false; | |
1215 | ||
1216 | tree dest = gimple_call_arg (stmt, 0); | |
1217 | tree var = dest; | |
1218 | if (TREE_CODE (var) != ADDR_EXPR) | |
1219 | return false; | |
1220 | ||
1221 | var = TREE_OPERAND (var, 0); | |
1222 | if (TREE_THIS_VOLATILE (var)) | |
1223 | return false; | |
1224 | ||
1225 | etype = TREE_TYPE (var); | |
1226 | if (TREE_CODE (etype) == ARRAY_TYPE) | |
1227 | etype = TREE_TYPE (etype); | |
1228 | ||
1229 | if (!INTEGRAL_TYPE_P (etype) | |
1230 | && !POINTER_TYPE_P (etype)) | |
1231 | return NULL_TREE; | |
1232 | ||
1233 | if (! var_decl_component_p (var)) | |
1234 | return NULL_TREE; | |
1235 | ||
1236 | length = tree_to_uhwi (len); | |
1237 | if (GET_MODE_SIZE (TYPE_MODE (etype)) != length | |
1238 | || get_pointer_alignment (dest) / BITS_PER_UNIT < length) | |
1239 | return NULL_TREE; | |
1240 | ||
1241 | if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) | |
1242 | return NULL_TREE; | |
1243 | ||
1244 | if (integer_zerop (c)) | |
1245 | cval = 0; | |
1246 | else | |
1247 | { | |
1248 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64) | |
1249 | return NULL_TREE; | |
1250 | ||
1251 | cval = TREE_INT_CST_LOW (c); | |
1252 | cval &= 0xff; | |
1253 | cval |= cval << 8; | |
1254 | cval |= cval << 16; | |
1255 | cval |= (cval << 31) << 1; | |
1256 | } | |
1257 | ||
1258 | var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0)); | |
1259 | gimple store = gimple_build_assign (var, build_int_cst_type (etype, cval)); | |
1260 | gimple_set_vuse (store, gimple_vuse (stmt)); | |
1261 | tree vdef = gimple_vdef (stmt); | |
1262 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
1263 | { | |
1264 | gimple_set_vdef (store, gimple_vdef (stmt)); | |
1265 | SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store; | |
1266 | } | |
1267 | gsi_insert_before (gsi, store, GSI_SAME_STMT); | |
1268 | if (gimple_call_lhs (stmt)) | |
1269 | { | |
1270 | gimple asgn = gimple_build_assign (gimple_call_lhs (stmt), dest); | |
1271 | gsi_replace (gsi, asgn, true); | |
1272 | } | |
1273 | else | |
1274 | { | |
1275 | gimple_stmt_iterator gsi2 = *gsi; | |
1276 | gsi_prev (gsi); | |
1277 | gsi_remove (&gsi2, true); | |
1278 | } | |
1279 | ||
1280 | return true; | |
1281 | } | |
1282 | ||
1283 | ||
1284 | /* Return the string length, maximum string length or maximum value of | |
1285 | ARG in LENGTH. | |
1286 | If ARG is an SSA name variable, follow its use-def chains. If LENGTH | |
1287 | is not NULL and, for TYPE == 0, its value is not equal to the length | |
1288 | we determine or if we are unable to determine the length or value, | |
1289 | return false. VISITED is a bitmap of visited variables. | |
1290 | TYPE is 0 if string length should be returned, 1 for maximum string | |
1291 | length and 2 for maximum value ARG can have. */ | |
1292 | ||
1293 | static bool | |
2ccb7428 | 1294 | get_maxval_strlen (tree arg, tree *length, bitmap *visited, int type) |
b9ea678c | 1295 | { |
1296 | tree var, val; | |
1297 | gimple def_stmt; | |
1298 | ||
1299 | if (TREE_CODE (arg) != SSA_NAME) | |
1300 | { | |
1301 | /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */ | |
1302 | if (TREE_CODE (arg) == ADDR_EXPR | |
1303 | && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF | |
1304 | && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1))) | |
1305 | { | |
1306 | tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
1307 | if (TREE_CODE (aop0) == INDIRECT_REF | |
1308 | && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME) | |
1309 | return get_maxval_strlen (TREE_OPERAND (aop0, 0), | |
1310 | length, visited, type); | |
1311 | } | |
1312 | ||
1313 | if (type == 2) | |
1314 | { | |
1315 | val = arg; | |
1316 | if (TREE_CODE (val) != INTEGER_CST | |
1317 | || tree_int_cst_sgn (val) < 0) | |
1318 | return false; | |
1319 | } | |
1320 | else | |
1321 | val = c_strlen (arg, 1); | |
1322 | if (!val) | |
1323 | return false; | |
1324 | ||
1325 | if (*length) | |
1326 | { | |
1327 | if (type > 0) | |
1328 | { | |
1329 | if (TREE_CODE (*length) != INTEGER_CST | |
1330 | || TREE_CODE (val) != INTEGER_CST) | |
1331 | return false; | |
1332 | ||
1333 | if (tree_int_cst_lt (*length, val)) | |
1334 | *length = val; | |
1335 | return true; | |
1336 | } | |
1337 | else if (simple_cst_equal (val, *length) != 1) | |
1338 | return false; | |
1339 | } | |
1340 | ||
1341 | *length = val; | |
1342 | return true; | |
1343 | } | |
1344 | ||
1345 | /* If ARG is registered for SSA update we cannot look at its defining | |
1346 | statement. */ | |
1347 | if (name_registered_for_update_p (arg)) | |
1348 | return false; | |
1349 | ||
1350 | /* If we were already here, break the infinite cycle. */ | |
2ccb7428 | 1351 | if (!*visited) |
1352 | *visited = BITMAP_ALLOC (NULL); | |
1353 | if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg))) | |
b9ea678c | 1354 | return true; |
1355 | ||
1356 | var = arg; | |
1357 | def_stmt = SSA_NAME_DEF_STMT (var); | |
1358 | ||
1359 | switch (gimple_code (def_stmt)) | |
1360 | { | |
1361 | case GIMPLE_ASSIGN: | |
1362 | /* The RHS of the statement defining VAR must either have a | |
1363 | constant length or come from another SSA_NAME with a constant | |
1364 | length. */ | |
1365 | if (gimple_assign_single_p (def_stmt) | |
1366 | || gimple_assign_unary_nop_p (def_stmt)) | |
1367 | { | |
1368 | tree rhs = gimple_assign_rhs1 (def_stmt); | |
1369 | return get_maxval_strlen (rhs, length, visited, type); | |
1370 | } | |
1371 | else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR) | |
1372 | { | |
1373 | tree op2 = gimple_assign_rhs2 (def_stmt); | |
1374 | tree op3 = gimple_assign_rhs3 (def_stmt); | |
1375 | return get_maxval_strlen (op2, length, visited, type) | |
1376 | && get_maxval_strlen (op3, length, visited, type); | |
1377 | } | |
1378 | return false; | |
1379 | ||
1380 | case GIMPLE_PHI: | |
1381 | { | |
1382 | /* All the arguments of the PHI node must have the same constant | |
1383 | length. */ | |
1384 | unsigned i; | |
1385 | ||
1386 | for (i = 0; i < gimple_phi_num_args (def_stmt); i++) | |
1387 | { | |
1388 | tree arg = gimple_phi_arg (def_stmt, i)->def; | |
1389 | ||
1390 | /* If this PHI has itself as an argument, we cannot | |
1391 | determine the string length of this argument. However, | |
1392 | if we can find a constant string length for the other | |
1393 | PHI args then we can still be sure that this is a | |
1394 | constant string length. So be optimistic and just | |
1395 | continue with the next argument. */ | |
1396 | if (arg == gimple_phi_result (def_stmt)) | |
1397 | continue; | |
1398 | ||
1399 | if (!get_maxval_strlen (arg, length, visited, type)) | |
1400 | return false; | |
1401 | } | |
1402 | } | |
1403 | return true; | |
1404 | ||
1405 | default: | |
1406 | return false; | |
1407 | } | |
1408 | } | |
1409 | ||
2ccb7428 | 1410 | tree |
1411 | get_maxval_strlen (tree arg, int type) | |
1412 | { | |
1413 | bitmap visited = NULL; | |
1414 | tree len = NULL_TREE; | |
1415 | if (!get_maxval_strlen (arg, &len, &visited, type)) | |
1416 | len = NULL_TREE; | |
1417 | if (visited) | |
1418 | BITMAP_FREE (visited); | |
1419 | ||
1420 | return len; | |
1421 | } | |
1422 | ||
b9ea678c | 1423 | |
1424 | /* Fold function call to builtin strcpy with arguments DEST and SRC. | |
1425 | If LEN is not NULL, it represents the length of the string to be | |
1426 | copied. Return NULL_TREE if no simplification can be made. */ | |
1427 | ||
1428 | static bool | |
1429 | gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi, | |
2ccb7428 | 1430 | tree dest, tree src) |
b9ea678c | 1431 | { |
2ccb7428 | 1432 | location_t loc = gimple_location (gsi_stmt (*gsi)); |
b9ea678c | 1433 | tree fn; |
1434 | ||
1435 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
1436 | if (operand_equal_p (src, dest, 0)) | |
1437 | { | |
1438 | replace_call_with_value (gsi, dest); | |
1439 | return true; | |
1440 | } | |
1441 | ||
1442 | if (optimize_function_for_size_p (cfun)) | |
1443 | return false; | |
1444 | ||
1445 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1446 | if (!fn) | |
1447 | return false; | |
1448 | ||
c1920e9d | 1449 | tree len = get_maxval_strlen (src, 0); |
b9ea678c | 1450 | if (!len) |
2ccb7428 | 1451 | return false; |
b9ea678c | 1452 | |
1453 | len = fold_convert_loc (loc, size_type_node, len); | |
1454 | len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1)); | |
1455 | len = force_gimple_operand_gsi (gsi, len, true, | |
1456 | NULL_TREE, true, GSI_SAME_STMT); | |
1457 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1458 | replace_call_with_call_and_fold (gsi, repl); | |
1459 | return true; | |
1460 | } | |
1461 | ||
1462 | /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN. | |
1463 | If SLEN is not NULL, it represents the length of the source string. | |
1464 | Return NULL_TREE if no simplification can be made. */ | |
1465 | ||
1466 | static bool | |
2ccb7428 | 1467 | gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi, |
1468 | tree dest, tree src, tree len) | |
b9ea678c | 1469 | { |
2ccb7428 | 1470 | location_t loc = gimple_location (gsi_stmt (*gsi)); |
b9ea678c | 1471 | tree fn; |
1472 | ||
1473 | /* If the LEN parameter is zero, return DEST. */ | |
1474 | if (integer_zerop (len)) | |
1475 | { | |
1476 | replace_call_with_value (gsi, dest); | |
1477 | return true; | |
1478 | } | |
1479 | ||
1480 | /* We can't compare slen with len as constants below if len is not a | |
1481 | constant. */ | |
2ccb7428 | 1482 | if (TREE_CODE (len) != INTEGER_CST) |
b9ea678c | 1483 | return false; |
1484 | ||
b9ea678c | 1485 | /* Now, we must be passed a constant src ptr parameter. */ |
c1920e9d | 1486 | tree slen = get_maxval_strlen (src, 0); |
2ccb7428 | 1487 | if (!slen || TREE_CODE (slen) != INTEGER_CST) |
b9ea678c | 1488 | return false; |
1489 | ||
1490 | slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1)); | |
1491 | ||
1492 | /* We do not support simplification of this case, though we do | |
1493 | support it when expanding trees into RTL. */ | |
1494 | /* FIXME: generate a call to __builtin_memset. */ | |
1495 | if (tree_int_cst_lt (slen, len)) | |
1496 | return false; | |
1497 | ||
1498 | /* OK transform into builtin memcpy. */ | |
1499 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1500 | if (!fn) | |
1501 | return false; | |
1502 | ||
1503 | len = fold_convert_loc (loc, size_type_node, len); | |
1504 | len = force_gimple_operand_gsi (gsi, len, true, | |
1505 | NULL_TREE, true, GSI_SAME_STMT); | |
1506 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1507 | replace_call_with_call_and_fold (gsi, repl); | |
1508 | return true; | |
1509 | } | |
1510 | ||
1511 | /* Simplify a call to the strcat builtin. DST and SRC are the arguments | |
1512 | to the call. | |
1513 | ||
1514 | Return NULL_TREE if no simplification was possible, otherwise return the | |
1515 | simplified form of the call as a tree. | |
1516 | ||
1517 | The simplified form may be a constant or other expression which | |
1518 | computes the same value, but in a more efficient manner (including | |
1519 | calls to other builtin functions). | |
1520 | ||
1521 | The call may contain arguments which need to be evaluated, but | |
1522 | which are not useful to determine the result of the call. In | |
1523 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
1524 | COMPOUND_EXPR will be an argument which must be evaluated. | |
1525 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
1526 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
1527 | form of the builtin function call. */ | |
1528 | ||
1529 | static bool | |
2ccb7428 | 1530 | gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src) |
b9ea678c | 1531 | { |
1532 | gimple stmt = gsi_stmt (*gsi); | |
2ccb7428 | 1533 | location_t loc = gimple_location (stmt); |
b9ea678c | 1534 | |
1535 | const char *p = c_getstr (src); | |
1536 | ||
1537 | /* If the string length is zero, return the dst parameter. */ | |
1538 | if (p && *p == '\0') | |
1539 | { | |
1540 | replace_call_with_value (gsi, dst); | |
1541 | return true; | |
1542 | } | |
1543 | ||
1544 | if (!optimize_bb_for_speed_p (gimple_bb (stmt))) | |
1545 | return false; | |
1546 | ||
1547 | /* See if we can store by pieces into (dst + strlen(dst)). */ | |
1548 | tree newdst; | |
1549 | tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN); | |
1550 | tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1551 | ||
1552 | if (!strlen_fn || !memcpy_fn) | |
1553 | return false; | |
1554 | ||
1555 | /* If the length of the source string isn't computable don't | |
1556 | split strcat into strlen and memcpy. */ | |
2ccb7428 | 1557 | tree len = get_maxval_strlen (src, 0); |
b9ea678c | 1558 | if (! len) |
b9ea678c | 1559 | return false; |
1560 | ||
1561 | /* Create strlen (dst). */ | |
1562 | gimple_seq stmts = NULL, stmts2; | |
1563 | gimple repl = gimple_build_call (strlen_fn, 1, dst); | |
1564 | gimple_set_location (repl, loc); | |
1565 | if (gimple_in_ssa_p (cfun)) | |
1566 | newdst = make_ssa_name (size_type_node, NULL); | |
1567 | else | |
1568 | newdst = create_tmp_reg (size_type_node, NULL); | |
1569 | gimple_call_set_lhs (repl, newdst); | |
1570 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1571 | ||
1572 | /* Create (dst p+ strlen (dst)). */ | |
1573 | newdst = fold_build_pointer_plus_loc (loc, dst, newdst); | |
1574 | newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE); | |
1575 | gimple_seq_add_seq_without_update (&stmts, stmts2); | |
1576 | ||
1577 | len = fold_convert_loc (loc, size_type_node, len); | |
1578 | len = size_binop_loc (loc, PLUS_EXPR, len, | |
1579 | build_int_cst (size_type_node, 1)); | |
1580 | len = force_gimple_operand (len, &stmts2, true, NULL_TREE); | |
1581 | gimple_seq_add_seq_without_update (&stmts, stmts2); | |
1582 | ||
1583 | repl = gimple_build_call (memcpy_fn, 3, newdst, src, len); | |
1584 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1585 | if (gimple_call_lhs (stmt)) | |
1586 | { | |
1587 | repl = gimple_build_assign (gimple_call_lhs (stmt), dst); | |
1588 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1589 | gsi_replace_with_seq_vops (gsi, stmts); | |
1590 | /* gsi now points at the assignment to the lhs, get a | |
1591 | stmt iterator to the memcpy call. | |
1592 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
1593 | CFG isn't built yet. */ | |
1594 | gimple_stmt_iterator gsi2 = *gsi; | |
1595 | gsi_prev (&gsi2); | |
1596 | fold_stmt (&gsi2); | |
1597 | } | |
1598 | else | |
1599 | { | |
1600 | gsi_replace_with_seq_vops (gsi, stmts); | |
1601 | fold_stmt (gsi); | |
1602 | } | |
1603 | return true; | |
1604 | } | |
1605 | ||
95f5ab8d | 1606 | /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE |
1607 | are the arguments to the call. */ | |
1608 | ||
1609 | static bool | |
1610 | gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi) | |
1611 | { | |
1612 | gimple stmt = gsi_stmt (*gsi); | |
1613 | tree dest = gimple_call_arg (stmt, 0); | |
1614 | tree src = gimple_call_arg (stmt, 1); | |
1615 | tree size = gimple_call_arg (stmt, 2); | |
1616 | tree fn; | |
1617 | const char *p; | |
1618 | ||
1619 | ||
1620 | p = c_getstr (src); | |
1621 | /* If the SRC parameter is "", return DEST. */ | |
1622 | if (p && *p == '\0') | |
1623 | { | |
1624 | replace_call_with_value (gsi, dest); | |
1625 | return true; | |
1626 | } | |
1627 | ||
1628 | if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size)) | |
1629 | return false; | |
1630 | ||
1631 | /* If __builtin_strcat_chk is used, assume strcat is available. */ | |
1632 | fn = builtin_decl_explicit (BUILT_IN_STRCAT); | |
1633 | if (!fn) | |
1634 | return false; | |
1635 | ||
1636 | gimple repl = gimple_build_call (fn, 2, dest, src); | |
1637 | replace_call_with_call_and_fold (gsi, repl); | |
1638 | return true; | |
1639 | } | |
1640 | ||
396b19bc | 1641 | /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC, |
1642 | LEN, and SIZE. */ | |
1643 | ||
1644 | static bool | |
1645 | gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi) | |
1646 | { | |
1647 | gimple stmt = gsi_stmt (*gsi); | |
1648 | tree dest = gimple_call_arg (stmt, 0); | |
1649 | tree src = gimple_call_arg (stmt, 1); | |
1650 | tree len = gimple_call_arg (stmt, 2); | |
1651 | tree size = gimple_call_arg (stmt, 3); | |
1652 | tree fn; | |
1653 | const char *p; | |
1654 | ||
1655 | p = c_getstr (src); | |
1656 | /* If the SRC parameter is "" or if LEN is 0, return DEST. */ | |
1657 | if ((p && *p == '\0') | |
1658 | || integer_zerop (len)) | |
1659 | { | |
1660 | replace_call_with_value (gsi, dest); | |
1661 | return true; | |
1662 | } | |
1663 | ||
1664 | if (! tree_fits_uhwi_p (size)) | |
1665 | return false; | |
1666 | ||
1667 | if (! integer_all_onesp (size)) | |
1668 | { | |
1669 | tree src_len = c_strlen (src, 1); | |
1670 | if (src_len | |
1671 | && tree_fits_uhwi_p (src_len) | |
1672 | && tree_fits_uhwi_p (len) | |
1673 | && ! tree_int_cst_lt (len, src_len)) | |
1674 | { | |
1675 | /* If LEN >= strlen (SRC), optimize into __strcat_chk. */ | |
1676 | fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK); | |
1677 | if (!fn) | |
1678 | return false; | |
1679 | ||
1680 | gimple repl = gimple_build_call (fn, 3, dest, src, size); | |
1681 | replace_call_with_call_and_fold (gsi, repl); | |
1682 | return true; | |
1683 | } | |
1684 | return false; | |
1685 | } | |
1686 | ||
1687 | /* If __builtin_strncat_chk is used, assume strncat is available. */ | |
1688 | fn = builtin_decl_explicit (BUILT_IN_STRNCAT); | |
1689 | if (!fn) | |
1690 | return false; | |
1691 | ||
1692 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1693 | replace_call_with_call_and_fold (gsi, repl); | |
1694 | return true; | |
1695 | } | |
1696 | ||
b9ea678c | 1697 | /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments |
1698 | to the call. IGNORE is true if the value returned | |
1699 | by the builtin will be ignored. UNLOCKED is true is true if this | |
1700 | actually a call to fputs_unlocked. If LEN in non-NULL, it represents | |
1701 | the known length of the string. Return NULL_TREE if no simplification | |
1702 | was possible. */ | |
1703 | ||
1704 | static bool | |
1705 | gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi, | |
b9ea678c | 1706 | tree arg0, tree arg1, |
2ccb7428 | 1707 | bool unlocked) |
b9ea678c | 1708 | { |
2ccb7428 | 1709 | gimple stmt = gsi_stmt (*gsi); |
1710 | ||
b9ea678c | 1711 | /* If we're using an unlocked function, assume the other unlocked |
1712 | functions exist explicitly. */ | |
1713 | tree const fn_fputc = (unlocked | |
1714 | ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED) | |
1715 | : builtin_decl_implicit (BUILT_IN_FPUTC)); | |
1716 | tree const fn_fwrite = (unlocked | |
1717 | ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED) | |
1718 | : builtin_decl_implicit (BUILT_IN_FWRITE)); | |
1719 | ||
1720 | /* If the return value is used, don't do the transformation. */ | |
2ccb7428 | 1721 | if (gimple_call_lhs (stmt)) |
b9ea678c | 1722 | return false; |
1723 | ||
b9ea678c | 1724 | /* Get the length of the string passed to fputs. If the length |
1725 | can't be determined, punt. */ | |
2ccb7428 | 1726 | tree len = get_maxval_strlen (arg0, 0); |
b9ea678c | 1727 | if (!len |
1728 | || TREE_CODE (len) != INTEGER_CST) | |
1729 | return false; | |
1730 | ||
1731 | switch (compare_tree_int (len, 1)) | |
1732 | { | |
1733 | case -1: /* length is 0, delete the call entirely . */ | |
1734 | replace_call_with_value (gsi, integer_zero_node); | |
1735 | return true; | |
1736 | ||
1737 | case 0: /* length is 1, call fputc. */ | |
1738 | { | |
1739 | const char *p = c_getstr (arg0); | |
1740 | if (p != NULL) | |
1741 | { | |
1742 | if (!fn_fputc) | |
1743 | return false; | |
1744 | ||
1745 | gimple repl = gimple_build_call (fn_fputc, 2, | |
1746 | build_int_cst | |
1747 | (integer_type_node, p[0]), arg1); | |
1748 | replace_call_with_call_and_fold (gsi, repl); | |
1749 | return true; | |
1750 | } | |
1751 | } | |
1752 | /* FALLTHROUGH */ | |
1753 | case 1: /* length is greater than 1, call fwrite. */ | |
1754 | { | |
1755 | /* If optimizing for size keep fputs. */ | |
1756 | if (optimize_function_for_size_p (cfun)) | |
1757 | return false; | |
1758 | /* New argument list transforming fputs(string, stream) to | |
1759 | fwrite(string, 1, len, stream). */ | |
1760 | if (!fn_fwrite) | |
1761 | return false; | |
1762 | ||
1763 | gimple repl = gimple_build_call (fn_fwrite, 4, arg0, | |
1764 | size_one_node, len, arg1); | |
1765 | replace_call_with_call_and_fold (gsi, repl); | |
1766 | return true; | |
1767 | } | |
1768 | default: | |
1769 | gcc_unreachable (); | |
1770 | } | |
1771 | return false; | |
1772 | } | |
1773 | ||
1774 | /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
1775 | DEST, SRC, LEN, and SIZE are the arguments to the call. | |
1776 | IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_* | |
1777 | code of the builtin. If MAXLEN is not NULL, it is maximum length | |
1778 | passed as third argument. */ | |
1779 | ||
1780 | static bool | |
1781 | gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, | |
b9ea678c | 1782 | tree dest, tree src, tree len, tree size, |
b9ea678c | 1783 | enum built_in_function fcode) |
1784 | { | |
2ccb7428 | 1785 | gimple stmt = gsi_stmt (*gsi); |
1786 | location_t loc = gimple_location (stmt); | |
1787 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
b9ea678c | 1788 | tree fn; |
1789 | ||
1790 | /* If SRC and DEST are the same (and not volatile), return DEST | |
1791 | (resp. DEST+LEN for __mempcpy_chk). */ | |
1792 | if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) | |
1793 | { | |
1794 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
1795 | { | |
1796 | replace_call_with_value (gsi, dest); | |
1797 | return true; | |
1798 | } | |
1799 | else | |
1800 | { | |
1801 | tree temp = fold_build_pointer_plus_loc (loc, dest, len); | |
1802 | temp = force_gimple_operand_gsi (gsi, temp, | |
1803 | false, NULL_TREE, true, | |
1804 | GSI_SAME_STMT); | |
1805 | replace_call_with_value (gsi, temp); | |
1806 | return true; | |
1807 | } | |
1808 | } | |
1809 | ||
1810 | if (! tree_fits_uhwi_p (size)) | |
1811 | return false; | |
1812 | ||
2ccb7428 | 1813 | tree maxlen = get_maxval_strlen (len, 2); |
b9ea678c | 1814 | if (! integer_all_onesp (size)) |
1815 | { | |
1816 | if (! tree_fits_uhwi_p (len)) | |
1817 | { | |
1818 | /* If LEN is not constant, try MAXLEN too. | |
1819 | For MAXLEN only allow optimizing into non-_ocs function | |
1820 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
1821 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
1822 | { | |
1823 | if (fcode == BUILT_IN_MEMPCPY_CHK && ignore) | |
1824 | { | |
1825 | /* (void) __mempcpy_chk () can be optimized into | |
1826 | (void) __memcpy_chk (). */ | |
1827 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); | |
1828 | if (!fn) | |
1829 | return false; | |
1830 | ||
1831 | gimple repl = gimple_build_call (fn, 4, dest, src, len, size); | |
1832 | replace_call_with_call_and_fold (gsi, repl); | |
1833 | return true; | |
1834 | } | |
1835 | return false; | |
1836 | } | |
1837 | } | |
1838 | else | |
1839 | maxlen = len; | |
1840 | ||
1841 | if (tree_int_cst_lt (size, maxlen)) | |
1842 | return false; | |
1843 | } | |
1844 | ||
1845 | fn = NULL_TREE; | |
1846 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
1847 | mem{cpy,pcpy,move,set} is available. */ | |
1848 | switch (fcode) | |
1849 | { | |
1850 | case BUILT_IN_MEMCPY_CHK: | |
1851 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY); | |
1852 | break; | |
1853 | case BUILT_IN_MEMPCPY_CHK: | |
1854 | fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); | |
1855 | break; | |
1856 | case BUILT_IN_MEMMOVE_CHK: | |
1857 | fn = builtin_decl_explicit (BUILT_IN_MEMMOVE); | |
1858 | break; | |
1859 | case BUILT_IN_MEMSET_CHK: | |
1860 | fn = builtin_decl_explicit (BUILT_IN_MEMSET); | |
1861 | break; | |
1862 | default: | |
1863 | break; | |
1864 | } | |
1865 | ||
1866 | if (!fn) | |
1867 | return false; | |
1868 | ||
1869 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
1870 | replace_call_with_call_and_fold (gsi, repl); | |
1871 | return true; | |
1872 | } | |
1873 | ||
1874 | /* Fold a call to the __st[rp]cpy_chk builtin. | |
1875 | DEST, SRC, and SIZE are the arguments to the call. | |
1876 | IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_* | |
1877 | code of the builtin. If MAXLEN is not NULL, it is maximum length of | |
1878 | strings passed as second argument. */ | |
1879 | ||
1880 | static bool | |
1881 | gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, | |
2ccb7428 | 1882 | tree dest, |
b9ea678c | 1883 | tree src, tree size, |
b9ea678c | 1884 | enum built_in_function fcode) |
1885 | { | |
2ccb7428 | 1886 | gimple stmt = gsi_stmt (*gsi); |
1887 | location_t loc = gimple_location (stmt); | |
1888 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
b9ea678c | 1889 | tree len, fn; |
1890 | ||
1891 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
1892 | if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) | |
1893 | { | |
1894 | replace_call_with_value (gsi, dest); | |
1895 | return true; | |
1896 | } | |
1897 | ||
1898 | if (! tree_fits_uhwi_p (size)) | |
1899 | return false; | |
1900 | ||
2ccb7428 | 1901 | tree maxlen = get_maxval_strlen (src, 1); |
b9ea678c | 1902 | if (! integer_all_onesp (size)) |
1903 | { | |
1904 | len = c_strlen (src, 1); | |
1905 | if (! len || ! tree_fits_uhwi_p (len)) | |
1906 | { | |
1907 | /* If LEN is not constant, try MAXLEN too. | |
1908 | For MAXLEN only allow optimizing into non-_ocs function | |
1909 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
1910 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
1911 | { | |
1912 | if (fcode == BUILT_IN_STPCPY_CHK) | |
1913 | { | |
1914 | if (! ignore) | |
1915 | return false; | |
1916 | ||
1917 | /* If return value of __stpcpy_chk is ignored, | |
1918 | optimize into __strcpy_chk. */ | |
1919 | fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK); | |
1920 | if (!fn) | |
1921 | return false; | |
1922 | ||
1923 | gimple repl = gimple_build_call (fn, 3, dest, src, size); | |
1924 | replace_call_with_call_and_fold (gsi, repl); | |
1925 | return true; | |
1926 | } | |
1927 | ||
1928 | if (! len || TREE_SIDE_EFFECTS (len)) | |
1929 | return false; | |
1930 | ||
1931 | /* If c_strlen returned something, but not a constant, | |
1932 | transform __strcpy_chk into __memcpy_chk. */ | |
1933 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); | |
1934 | if (!fn) | |
1935 | return false; | |
1936 | ||
1937 | len = fold_convert_loc (loc, size_type_node, len); | |
1938 | len = size_binop_loc (loc, PLUS_EXPR, len, | |
1939 | build_int_cst (size_type_node, 1)); | |
1940 | len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE, | |
1941 | true, GSI_SAME_STMT); | |
1942 | gimple repl = gimple_build_call (fn, 4, dest, src, len, size); | |
1943 | replace_call_with_call_and_fold (gsi, repl); | |
1944 | return true; | |
1945 | } | |
34e73149 | 1946 | } |
b9ea678c | 1947 | else |
1948 | maxlen = len; | |
1949 | ||
1950 | if (! tree_int_cst_lt (maxlen, size)) | |
1951 | return false; | |
34e73149 | 1952 | } |
1953 | ||
b9ea678c | 1954 | /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ |
1955 | fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK | |
1956 | ? BUILT_IN_STPCPY : BUILT_IN_STRCPY); | |
1957 | if (!fn) | |
1958 | return false; | |
1959 | ||
1960 | gimple repl = gimple_build_call (fn, 2, dest, src); | |
1961 | replace_call_with_call_and_fold (gsi, repl); | |
1962 | return true; | |
1963 | } | |
1964 | ||
1965 | /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE | |
1966 | are the arguments to the call. If MAXLEN is not NULL, it is maximum | |
1967 | length passed as third argument. IGNORE is true if return value can be | |
1968 | ignored. FCODE is the BUILT_IN_* code of the builtin. */ | |
1969 | ||
1970 | static bool | |
1971 | gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, | |
1972 | tree dest, tree src, | |
2ccb7428 | 1973 | tree len, tree size, |
b9ea678c | 1974 | enum built_in_function fcode) |
1975 | { | |
2ccb7428 | 1976 | gimple stmt = gsi_stmt (*gsi); |
1977 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
b9ea678c | 1978 | tree fn; |
1979 | ||
1980 | if (fcode == BUILT_IN_STPNCPY_CHK && ignore) | |
2d18b16d | 1981 | { |
b9ea678c | 1982 | /* If return value of __stpncpy_chk is ignored, |
1983 | optimize into __strncpy_chk. */ | |
1984 | fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); | |
1985 | if (fn) | |
1986 | { | |
1987 | gimple repl = gimple_build_call (fn, 4, dest, src, len, size); | |
1988 | replace_call_with_call_and_fold (gsi, repl); | |
1989 | return true; | |
1990 | } | |
2d18b16d | 1991 | } |
1992 | ||
b9ea678c | 1993 | if (! tree_fits_uhwi_p (size)) |
1994 | return false; | |
1995 | ||
2ccb7428 | 1996 | tree maxlen = get_maxval_strlen (len, 2); |
b9ea678c | 1997 | if (! integer_all_onesp (size)) |
2d18b16d | 1998 | { |
b9ea678c | 1999 | if (! tree_fits_uhwi_p (len)) |
3aadae2d | 2000 | { |
b9ea678c | 2001 | /* If LEN is not constant, try MAXLEN too. |
2002 | For MAXLEN only allow optimizing into non-_ocs function | |
2003 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2004 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
2005 | return false; | |
78a0527a | 2006 | } |
b9ea678c | 2007 | else |
2008 | maxlen = len; | |
2009 | ||
2010 | if (tree_int_cst_lt (size, maxlen)) | |
2011 | return false; | |
2d18b16d | 2012 | } |
2013 | ||
b9ea678c | 2014 | /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */ |
2015 | fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK | |
2016 | ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY); | |
2017 | if (!fn) | |
2018 | return false; | |
2019 | ||
2020 | gimple repl = gimple_build_call (fn, 3, dest, src, len); | |
2021 | replace_call_with_call_and_fold (gsi, repl); | |
2022 | return true; | |
2d18b16d | 2023 | } |
2024 | ||
b9ea678c | 2025 | /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return |
2026 | NULL_TREE if a normal call should be emitted rather than expanding | |
2027 | the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or | |
2028 | BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length | |
2029 | passed as second argument. */ | |
2d18b16d | 2030 | |
2031 | static bool | |
b9ea678c | 2032 | gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi, |
2ccb7428 | 2033 | enum built_in_function fcode) |
2d18b16d | 2034 | { |
b9ea678c | 2035 | gimple stmt = gsi_stmt (*gsi); |
2036 | tree dest, size, len, fn, fmt, flag; | |
2037 | const char *fmt_str; | |
2d18b16d | 2038 | |
b9ea678c | 2039 | /* Verify the required arguments in the original call. */ |
2040 | if (gimple_call_num_args (stmt) < 5) | |
2041 | return false; | |
2d18b16d | 2042 | |
b9ea678c | 2043 | dest = gimple_call_arg (stmt, 0); |
2044 | len = gimple_call_arg (stmt, 1); | |
2045 | flag = gimple_call_arg (stmt, 2); | |
2046 | size = gimple_call_arg (stmt, 3); | |
2047 | fmt = gimple_call_arg (stmt, 4); | |
2048 | ||
2049 | if (! tree_fits_uhwi_p (size)) | |
2050 | return false; | |
2051 | ||
2052 | if (! integer_all_onesp (size)) | |
2053 | { | |
2ccb7428 | 2054 | tree maxlen = get_maxval_strlen (len, 2); |
b9ea678c | 2055 | if (! tree_fits_uhwi_p (len)) |
2d18b16d | 2056 | { |
b9ea678c | 2057 | /* If LEN is not constant, try MAXLEN too. |
2058 | For MAXLEN only allow optimizing into non-_ocs function | |
2059 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2060 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
2d18b16d | 2061 | return false; |
2062 | } | |
2063 | else | |
b9ea678c | 2064 | maxlen = len; |
2d18b16d | 2065 | |
b9ea678c | 2066 | if (tree_int_cst_lt (size, maxlen)) |
2067 | return false; | |
2068 | } | |
2d18b16d | 2069 | |
b9ea678c | 2070 | if (!init_target_chars ()) |
2071 | return false; | |
2d18b16d | 2072 | |
b9ea678c | 2073 | /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0 |
2074 | or if format doesn't contain % chars or is "%s". */ | |
2075 | if (! integer_zerop (flag)) | |
2076 | { | |
2077 | fmt_str = c_getstr (fmt); | |
2078 | if (fmt_str == NULL) | |
2079 | return false; | |
2080 | if (strchr (fmt_str, target_percent) != NULL | |
2081 | && strcmp (fmt_str, target_percent_s)) | |
2082 | return false; | |
2d18b16d | 2083 | } |
2084 | ||
b9ea678c | 2085 | /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is |
2086 | available. */ | |
2087 | fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK | |
2088 | ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF); | |
2089 | if (!fn) | |
1a06b503 | 2090 | return false; |
2091 | ||
b9ea678c | 2092 | /* Replace the called function and the first 5 argument by 3 retaining |
2093 | trailing varargs. */ | |
2094 | gimple_call_set_fndecl (stmt, fn); | |
2095 | gimple_call_set_fntype (stmt, TREE_TYPE (fn)); | |
2096 | gimple_call_set_arg (stmt, 0, dest); | |
2097 | gimple_call_set_arg (stmt, 1, len); | |
2098 | gimple_call_set_arg (stmt, 2, fmt); | |
2099 | for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i) | |
2100 | gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2)); | |
2101 | gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2); | |
2102 | fold_stmt (gsi); | |
2103 | return true; | |
2104 | } | |
2d18b16d | 2105 | |
b9ea678c | 2106 | /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS. |
2107 | Return NULL_TREE if a normal call should be emitted rather than | |
2108 | expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK | |
2109 | or BUILT_IN_VSPRINTF_CHK. */ | |
2d18b16d | 2110 | |
b9ea678c | 2111 | static bool |
2112 | gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi, | |
2113 | enum built_in_function fcode) | |
2114 | { | |
2115 | gimple stmt = gsi_stmt (*gsi); | |
2116 | tree dest, size, len, fn, fmt, flag; | |
2117 | const char *fmt_str; | |
2118 | unsigned nargs = gimple_call_num_args (stmt); | |
2d18b16d | 2119 | |
b9ea678c | 2120 | /* Verify the required arguments in the original call. */ |
2121 | if (nargs < 4) | |
2122 | return false; | |
2123 | dest = gimple_call_arg (stmt, 0); | |
2124 | flag = gimple_call_arg (stmt, 1); | |
2125 | size = gimple_call_arg (stmt, 2); | |
2126 | fmt = gimple_call_arg (stmt, 3); | |
2127 | ||
2128 | if (! tree_fits_uhwi_p (size)) | |
2129 | return false; | |
2130 | ||
2131 | len = NULL_TREE; | |
2132 | ||
2133 | if (!init_target_chars ()) | |
2134 | return false; | |
2135 | ||
2136 | /* Check whether the format is a literal string constant. */ | |
2137 | fmt_str = c_getstr (fmt); | |
2138 | if (fmt_str != NULL) | |
2139 | { | |
2140 | /* If the format doesn't contain % args or %%, we know the size. */ | |
2141 | if (strchr (fmt_str, target_percent) == 0) | |
2d18b16d | 2142 | { |
b9ea678c | 2143 | if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4) |
2144 | len = build_int_cstu (size_type_node, strlen (fmt_str)); | |
2145 | } | |
2146 | /* If the format is "%s" and first ... argument is a string literal, | |
2147 | we know the size too. */ | |
2148 | else if (fcode == BUILT_IN_SPRINTF_CHK | |
2149 | && strcmp (fmt_str, target_percent_s) == 0) | |
2150 | { | |
2151 | tree arg; | |
2d18b16d | 2152 | |
b9ea678c | 2153 | if (nargs == 5) |
2154 | { | |
2155 | arg = gimple_call_arg (stmt, 4); | |
2156 | if (POINTER_TYPE_P (TREE_TYPE (arg))) | |
2157 | { | |
2158 | len = c_strlen (arg, 1); | |
2159 | if (! len || ! tree_fits_uhwi_p (len)) | |
2160 | len = NULL_TREE; | |
2161 | } | |
2162 | } | |
2163 | } | |
2164 | } | |
2d18b16d | 2165 | |
b9ea678c | 2166 | if (! integer_all_onesp (size)) |
2167 | { | |
2168 | if (! len || ! tree_int_cst_lt (len, size)) | |
2169 | return false; | |
2170 | } | |
2d18b16d | 2171 | |
b9ea678c | 2172 | /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0 |
2173 | or if format doesn't contain % chars or is "%s". */ | |
2174 | if (! integer_zerop (flag)) | |
2175 | { | |
2176 | if (fmt_str == NULL) | |
2177 | return false; | |
2178 | if (strchr (fmt_str, target_percent) != NULL | |
2179 | && strcmp (fmt_str, target_percent_s)) | |
2180 | return false; | |
2181 | } | |
2d18b16d | 2182 | |
b9ea678c | 2183 | /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */ |
2184 | fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK | |
2185 | ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF); | |
2186 | if (!fn) | |
2187 | return false; | |
2188 | ||
2189 | /* Replace the called function and the first 4 argument by 2 retaining | |
2190 | trailing varargs. */ | |
2191 | gimple_call_set_fndecl (stmt, fn); | |
2192 | gimple_call_set_fntype (stmt, TREE_TYPE (fn)); | |
2193 | gimple_call_set_arg (stmt, 0, dest); | |
2194 | gimple_call_set_arg (stmt, 1, fmt); | |
2195 | for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i) | |
2196 | gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2)); | |
2197 | gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2); | |
2198 | fold_stmt (gsi); | |
2199 | return true; | |
2200 | } | |
2201 | ||
1888f376 | 2202 | /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG. |
2203 | ORIG may be null if this is a 2-argument call. We don't attempt to | |
2204 | simplify calls with more than 3 arguments. | |
2205 | ||
2206 | Return NULL_TREE if no simplification was possible, otherwise return the | |
2207 | simplified form of the call as a tree. If IGNORED is true, it means that | |
2208 | the caller does not use the returned value of the function. */ | |
2209 | ||
2210 | static bool | |
2ccb7428 | 2211 | gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi) |
1888f376 | 2212 | { |
2213 | gimple stmt = gsi_stmt (*gsi); | |
2214 | tree dest = gimple_call_arg (stmt, 0); | |
2215 | tree fmt = gimple_call_arg (stmt, 1); | |
2216 | tree orig = NULL_TREE; | |
2217 | const char *fmt_str = NULL; | |
2218 | ||
2219 | /* Verify the required arguments in the original call. We deal with two | |
2220 | types of sprintf() calls: 'sprintf (str, fmt)' and | |
2221 | 'sprintf (dest, "%s", orig)'. */ | |
2222 | if (gimple_call_num_args (stmt) > 3) | |
2223 | return false; | |
2224 | ||
2225 | if (gimple_call_num_args (stmt) == 3) | |
2226 | orig = gimple_call_arg (stmt, 2); | |
2227 | ||
2228 | /* Check whether the format is a literal string constant. */ | |
2229 | fmt_str = c_getstr (fmt); | |
2230 | if (fmt_str == NULL) | |
2231 | return false; | |
2232 | ||
2233 | if (!init_target_chars ()) | |
2234 | return false; | |
2235 | ||
2236 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2237 | if (strchr (fmt_str, target_percent) == NULL) | |
2238 | { | |
2239 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2240 | ||
2241 | if (!fn) | |
2242 | return false; | |
2243 | ||
2244 | /* Don't optimize sprintf (buf, "abc", ptr++). */ | |
2245 | if (orig) | |
2246 | return false; | |
2247 | ||
2248 | /* Convert sprintf (str, fmt) into strcpy (str, fmt) when | |
2249 | 'format' is known to contain no % formats. */ | |
2250 | gimple_seq stmts = NULL; | |
2251 | gimple repl = gimple_build_call (fn, 2, dest, fmt); | |
2252 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2253 | if (gimple_call_lhs (stmt)) | |
2254 | { | |
2255 | repl = gimple_build_assign (gimple_call_lhs (stmt), | |
2256 | build_int_cst (integer_type_node, | |
2257 | strlen (fmt_str))); | |
2258 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2259 | gsi_replace_with_seq_vops (gsi, stmts); | |
2260 | /* gsi now points at the assignment to the lhs, get a | |
2261 | stmt iterator to the memcpy call. | |
2262 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2263 | CFG isn't built yet. */ | |
2264 | gimple_stmt_iterator gsi2 = *gsi; | |
2265 | gsi_prev (&gsi2); | |
2266 | fold_stmt (&gsi2); | |
2267 | } | |
2268 | else | |
2269 | { | |
2270 | gsi_replace_with_seq_vops (gsi, stmts); | |
2271 | fold_stmt (gsi); | |
2272 | } | |
2273 | return true; | |
2274 | } | |
2275 | ||
2276 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
2277 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) | |
2278 | { | |
2279 | tree fn; | |
2280 | fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2281 | ||
2282 | if (!fn) | |
2283 | return false; | |
2284 | ||
2285 | /* Don't crash on sprintf (str1, "%s"). */ | |
2286 | if (!orig) | |
2287 | return false; | |
2288 | ||
2ccb7428 | 2289 | tree orig_len = NULL_TREE; |
2290 | if (gimple_call_lhs (stmt)) | |
1888f376 | 2291 | { |
2ccb7428 | 2292 | orig_len = get_maxval_strlen (orig, 0); |
95e631b8 | 2293 | if (!orig_len) |
1888f376 | 2294 | return false; |
2295 | } | |
2296 | ||
2297 | /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */ | |
2298 | gimple_seq stmts = NULL; | |
2299 | gimple repl = gimple_build_call (fn, 2, dest, orig); | |
2300 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2301 | if (gimple_call_lhs (stmt)) | |
2302 | { | |
95e631b8 | 2303 | if (!useless_type_conversion_p (integer_type_node, |
2304 | TREE_TYPE (orig_len))) | |
2305 | orig_len = fold_convert (integer_type_node, orig_len); | |
2306 | repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); | |
1888f376 | 2307 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2308 | gsi_replace_with_seq_vops (gsi, stmts); | |
2309 | /* gsi now points at the assignment to the lhs, get a | |
2310 | stmt iterator to the memcpy call. | |
2311 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2312 | CFG isn't built yet. */ | |
2313 | gimple_stmt_iterator gsi2 = *gsi; | |
2314 | gsi_prev (&gsi2); | |
2315 | fold_stmt (&gsi2); | |
2316 | } | |
2317 | else | |
2318 | { | |
2319 | gsi_replace_with_seq_vops (gsi, stmts); | |
2320 | fold_stmt (gsi); | |
2321 | } | |
2322 | return true; | |
2323 | } | |
2324 | return false; | |
2325 | } | |
2326 | ||
95e631b8 | 2327 | /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE, |
2328 | FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't | |
2329 | attempt to simplify calls with more than 4 arguments. | |
1888f376 | 2330 | |
95e631b8 | 2331 | Return NULL_TREE if no simplification was possible, otherwise return the |
2332 | simplified form of the call as a tree. If IGNORED is true, it means that | |
2333 | the caller does not use the returned value of the function. */ | |
2334 | ||
2335 | static bool | |
2ccb7428 | 2336 | gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi) |
95e631b8 | 2337 | { |
2338 | gimple stmt = gsi_stmt (*gsi); | |
2339 | tree dest = gimple_call_arg (stmt, 0); | |
2340 | tree destsize = gimple_call_arg (stmt, 1); | |
2341 | tree fmt = gimple_call_arg (stmt, 2); | |
2342 | tree orig = NULL_TREE; | |
2343 | const char *fmt_str = NULL; | |
2344 | ||
2345 | if (gimple_call_num_args (stmt) > 4) | |
2346 | return false; | |
2347 | ||
2348 | if (gimple_call_num_args (stmt) == 4) | |
2349 | orig = gimple_call_arg (stmt, 3); | |
2350 | ||
2351 | if (!tree_fits_uhwi_p (destsize)) | |
2352 | return false; | |
2353 | unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize); | |
2354 | ||
2355 | /* Check whether the format is a literal string constant. */ | |
2356 | fmt_str = c_getstr (fmt); | |
2357 | if (fmt_str == NULL) | |
2358 | return false; | |
2359 | ||
2360 | if (!init_target_chars ()) | |
2361 | return false; | |
2362 | ||
2363 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2364 | if (strchr (fmt_str, target_percent) == NULL) | |
2365 | { | |
2366 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2367 | if (!fn) | |
2368 | return false; | |
2369 | ||
2370 | /* Don't optimize snprintf (buf, 4, "abc", ptr++). */ | |
2371 | if (orig) | |
2372 | return false; | |
2373 | ||
2374 | /* We could expand this as | |
2375 | memcpy (str, fmt, cst - 1); str[cst - 1] = '\0'; | |
2376 | or to | |
2377 | memcpy (str, fmt_with_nul_at_cstm1, cst); | |
2378 | but in the former case that might increase code size | |
2379 | and in the latter case grow .rodata section too much. | |
2380 | So punt for now. */ | |
2381 | size_t len = strlen (fmt_str); | |
2382 | if (len >= destlen) | |
2383 | return false; | |
2384 | ||
2385 | gimple_seq stmts = NULL; | |
2386 | gimple repl = gimple_build_call (fn, 2, dest, fmt); | |
2387 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2388 | if (gimple_call_lhs (stmt)) | |
2389 | { | |
2390 | repl = gimple_build_assign (gimple_call_lhs (stmt), | |
2391 | build_int_cst (integer_type_node, len)); | |
2392 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2393 | gsi_replace_with_seq_vops (gsi, stmts); | |
2394 | /* gsi now points at the assignment to the lhs, get a | |
2395 | stmt iterator to the memcpy call. | |
2396 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2397 | CFG isn't built yet. */ | |
2398 | gimple_stmt_iterator gsi2 = *gsi; | |
2399 | gsi_prev (&gsi2); | |
2400 | fold_stmt (&gsi2); | |
2401 | } | |
2402 | else | |
2403 | { | |
2404 | gsi_replace_with_seq_vops (gsi, stmts); | |
2405 | fold_stmt (gsi); | |
2406 | } | |
2407 | return true; | |
2408 | } | |
2409 | ||
2410 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
2411 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) | |
2412 | { | |
2413 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2414 | if (!fn) | |
2415 | return false; | |
2416 | ||
2417 | /* Don't crash on snprintf (str1, cst, "%s"). */ | |
2418 | if (!orig) | |
2419 | return false; | |
2420 | ||
2ccb7428 | 2421 | tree orig_len = get_maxval_strlen (orig, 0); |
95e631b8 | 2422 | if (!orig_len) |
2ccb7428 | 2423 | return false; |
95e631b8 | 2424 | |
2425 | /* We could expand this as | |
2426 | memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0'; | |
2427 | or to | |
2428 | memcpy (str1, str2_with_nul_at_cstm1, cst); | |
2429 | but in the former case that might increase code size | |
2430 | and in the latter case grow .rodata section too much. | |
2431 | So punt for now. */ | |
2432 | if (compare_tree_int (orig_len, destlen) >= 0) | |
2433 | return false; | |
2434 | ||
2435 | /* Convert snprintf (str1, cst, "%s", str2) into | |
2436 | strcpy (str1, str2) if strlen (str2) < cst. */ | |
2437 | gimple_seq stmts = NULL; | |
2438 | gimple repl = gimple_build_call (fn, 2, dest, orig); | |
2439 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2440 | if (gimple_call_lhs (stmt)) | |
2441 | { | |
2442 | if (!useless_type_conversion_p (integer_type_node, | |
2443 | TREE_TYPE (orig_len))) | |
2444 | orig_len = fold_convert (integer_type_node, orig_len); | |
2445 | repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); | |
2446 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2447 | gsi_replace_with_seq_vops (gsi, stmts); | |
2448 | /* gsi now points at the assignment to the lhs, get a | |
2449 | stmt iterator to the memcpy call. | |
2450 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2451 | CFG isn't built yet. */ | |
2452 | gimple_stmt_iterator gsi2 = *gsi; | |
2453 | gsi_prev (&gsi2); | |
2454 | fold_stmt (&gsi2); | |
2455 | } | |
2456 | else | |
2457 | { | |
2458 | gsi_replace_with_seq_vops (gsi, stmts); | |
2459 | fold_stmt (gsi); | |
2460 | } | |
2461 | return true; | |
2462 | } | |
2463 | return false; | |
2464 | } | |
1888f376 | 2465 | |
b9ea678c | 2466 | |
2467 | /* Fold a call to __builtin_strlen with known length LEN. */ | |
2468 | ||
2469 | static bool | |
2ccb7428 | 2470 | gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi) |
b9ea678c | 2471 | { |
2ccb7428 | 2472 | gimple stmt = gsi_stmt (*gsi); |
2473 | tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0); | |
b9ea678c | 2474 | if (!len) |
2475 | return false; | |
7f96f652 | 2476 | len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT); |
b9ea678c | 2477 | replace_call_with_value (gsi, len); |
2478 | return true; | |
2d18b16d | 2479 | } |
2480 | ||
2481 | ||
2ccb7428 | 2482 | /* Fold the non-target builtin at *GSI and return whether any simplification |
2483 | was made. */ | |
2d18b16d | 2484 | |
b9ea678c | 2485 | static bool |
2ccb7428 | 2486 | gimple_fold_builtin (gimple_stmt_iterator *gsi) |
2d18b16d | 2487 | { |
b9ea678c | 2488 | gimple stmt = gsi_stmt (*gsi); |
b9ea678c | 2489 | tree callee = gimple_call_fndecl (stmt); |
2d18b16d | 2490 | |
2ccb7428 | 2491 | /* Give up for always_inline inline builtins until they are |
2492 | inlined. */ | |
2493 | if (avoid_folding_inline_builtin (callee)) | |
2494 | return false; | |
2d18b16d | 2495 | |
2d18b16d | 2496 | switch (DECL_FUNCTION_CODE (callee)) |
2497 | { | |
2ccb7428 | 2498 | case BUILT_IN_BZERO: |
2499 | return gimple_fold_builtin_memset (gsi, integer_zero_node, | |
2500 | gimple_call_arg (stmt, 1)); | |
2501 | case BUILT_IN_MEMSET: | |
2502 | return gimple_fold_builtin_memset (gsi, | |
2503 | gimple_call_arg (stmt, 1), | |
2504 | gimple_call_arg (stmt, 2)); | |
2505 | case BUILT_IN_BCOPY: | |
2506 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 1), | |
2507 | gimple_call_arg (stmt, 0), 3); | |
2508 | case BUILT_IN_MEMCPY: | |
2509 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
2510 | gimple_call_arg (stmt, 1), 0); | |
2511 | case BUILT_IN_MEMPCPY: | |
2512 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
2513 | gimple_call_arg (stmt, 1), 1); | |
2514 | case BUILT_IN_MEMMOVE: | |
2515 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
2516 | gimple_call_arg (stmt, 1), 3); | |
2517 | case BUILT_IN_SPRINTF_CHK: | |
2518 | case BUILT_IN_VSPRINTF_CHK: | |
2519 | return gimple_fold_builtin_sprintf_chk (gsi, DECL_FUNCTION_CODE (callee)); | |
2520 | case BUILT_IN_STRCAT_CHK: | |
2521 | return gimple_fold_builtin_strcat_chk (gsi); | |
396b19bc | 2522 | case BUILT_IN_STRNCAT_CHK: |
2523 | return gimple_fold_builtin_strncat_chk (gsi); | |
2d18b16d | 2524 | case BUILT_IN_STRLEN: |
2ccb7428 | 2525 | return gimple_fold_builtin_strlen (gsi); |
2d18b16d | 2526 | case BUILT_IN_STRCPY: |
2ccb7428 | 2527 | return gimple_fold_builtin_strcpy (gsi, |
b9ea678c | 2528 | gimple_call_arg (stmt, 0), |
2ccb7428 | 2529 | gimple_call_arg (stmt, 1)); |
2d18b16d | 2530 | case BUILT_IN_STRNCPY: |
2ccb7428 | 2531 | return gimple_fold_builtin_strncpy (gsi, |
b9ea678c | 2532 | gimple_call_arg (stmt, 0), |
2533 | gimple_call_arg (stmt, 1), | |
2ccb7428 | 2534 | gimple_call_arg (stmt, 2)); |
b65555c1 | 2535 | case BUILT_IN_STRCAT: |
2ccb7428 | 2536 | return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0), |
2537 | gimple_call_arg (stmt, 1)); | |
2d18b16d | 2538 | case BUILT_IN_FPUTS: |
2ccb7428 | 2539 | return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0), |
2540 | gimple_call_arg (stmt, 1), false); | |
2d18b16d | 2541 | case BUILT_IN_FPUTS_UNLOCKED: |
2ccb7428 | 2542 | return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0), |
2543 | gimple_call_arg (stmt, 1), true); | |
2d18b16d | 2544 | case BUILT_IN_MEMCPY_CHK: |
2545 | case BUILT_IN_MEMPCPY_CHK: | |
2546 | case BUILT_IN_MEMMOVE_CHK: | |
2547 | case BUILT_IN_MEMSET_CHK: | |
2ccb7428 | 2548 | return gimple_fold_builtin_memory_chk (gsi, |
b9ea678c | 2549 | gimple_call_arg (stmt, 0), |
2550 | gimple_call_arg (stmt, 1), | |
2551 | gimple_call_arg (stmt, 2), | |
2552 | gimple_call_arg (stmt, 3), | |
b9ea678c | 2553 | DECL_FUNCTION_CODE (callee)); |
2d18b16d | 2554 | case BUILT_IN_STRCPY_CHK: |
2555 | case BUILT_IN_STPCPY_CHK: | |
2ccb7428 | 2556 | return gimple_fold_builtin_stxcpy_chk (gsi, |
b9ea678c | 2557 | gimple_call_arg (stmt, 0), |
2558 | gimple_call_arg (stmt, 1), | |
2559 | gimple_call_arg (stmt, 2), | |
b9ea678c | 2560 | DECL_FUNCTION_CODE (callee)); |
2d18b16d | 2561 | case BUILT_IN_STRNCPY_CHK: |
1063acde | 2562 | case BUILT_IN_STPNCPY_CHK: |
b9ea678c | 2563 | return gimple_fold_builtin_stxncpy_chk (gsi, |
2564 | gimple_call_arg (stmt, 0), | |
2565 | gimple_call_arg (stmt, 1), | |
2566 | gimple_call_arg (stmt, 2), | |
2567 | gimple_call_arg (stmt, 3), | |
b9ea678c | 2568 | DECL_FUNCTION_CODE (callee)); |
2d18b16d | 2569 | case BUILT_IN_SNPRINTF_CHK: |
2570 | case BUILT_IN_VSNPRINTF_CHK: | |
2ccb7428 | 2571 | return gimple_fold_builtin_snprintf_chk (gsi, |
b9ea678c | 2572 | DECL_FUNCTION_CODE (callee)); |
95e631b8 | 2573 | case BUILT_IN_SNPRINTF: |
2ccb7428 | 2574 | return gimple_fold_builtin_snprintf (gsi); |
95e631b8 | 2575 | case BUILT_IN_SPRINTF: |
2ccb7428 | 2576 | return gimple_fold_builtin_sprintf (gsi); |
b9ea678c | 2577 | default:; |
2578 | } | |
2579 | ||
2580 | /* Try the generic builtin folder. */ | |
2581 | bool ignore = (gimple_call_lhs (stmt) == NULL); | |
2582 | tree result = fold_call_stmt (stmt, ignore); | |
2583 | if (result) | |
2584 | { | |
2585 | if (ignore) | |
2586 | STRIP_NOPS (result); | |
2587 | else | |
2588 | result = fold_convert (gimple_call_return_type (stmt), result); | |
2589 | if (!update_call_from_tree (gsi, result)) | |
2590 | gimplify_and_update_call_from_tree (gsi, result); | |
2591 | return true; | |
2592 | } | |
2593 | ||
2594 | return false; | |
2595 | } | |
2596 | ||
2d18b16d | 2597 | /* Attempt to fold a call statement referenced by the statement iterator GSI. |
2598 | The statement may be replaced by another statement, e.g., if the call | |
2599 | simplifies to a constant value. Return true if any changes were made. | |
2600 | It is assumed that the operands have been previously folded. */ | |
2601 | ||
3aa6ac67 | 2602 | static bool |
3fd0ca33 | 2603 | gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace) |
2d18b16d | 2604 | { |
2605 | gimple stmt = gsi_stmt (*gsi); | |
6fec5449 | 2606 | tree callee; |
3aa6ac67 | 2607 | bool changed = false; |
2608 | unsigned i; | |
2d18b16d | 2609 | |
3aa6ac67 | 2610 | /* Fold *& in call arguments. */ |
2611 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
2612 | if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i))) | |
2613 | { | |
2614 | tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false); | |
2615 | if (tmp) | |
2616 | { | |
2617 | gimple_call_set_arg (stmt, i, tmp); | |
2618 | changed = true; | |
2619 | } | |
2620 | } | |
6fec5449 | 2621 | |
2622 | /* Check for virtual calls that became direct calls. */ | |
2623 | callee = gimple_call_fn (stmt); | |
fb049fba | 2624 | if (callee && TREE_CODE (callee) == OBJ_TYPE_REF) |
6fec5449 | 2625 | { |
3658fd1d | 2626 | if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE) |
2627 | { | |
10fba9c0 | 2628 | if (dump_file && virtual_method_call_p (callee) |
2629 | && !possible_polymorphic_call_target_p | |
379f6698 | 2630 | (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl |
2631 | (OBJ_TYPE_REF_EXPR (callee))))) | |
10fba9c0 | 2632 | { |
2633 | fprintf (dump_file, | |
f70f513f | 2634 | "Type inheritance inconsistent devirtualization of "); |
10fba9c0 | 2635 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
2636 | fprintf (dump_file, " to "); | |
2637 | print_generic_expr (dump_file, callee, TDF_SLIM); | |
2638 | fprintf (dump_file, "\n"); | |
2639 | } | |
2640 | ||
3658fd1d | 2641 | gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee)); |
3aa6ac67 | 2642 | changed = true; |
2643 | } | |
f70f513f | 2644 | else if (flag_devirtualize && !inplace && virtual_method_call_p (callee)) |
3aa6ac67 | 2645 | { |
76058579 | 2646 | bool final; |
2647 | vec <cgraph_node *>targets | |
1b613a0a | 2648 | = possible_polymorphic_call_targets (callee, stmt, &final); |
ceb49bba | 2649 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
3aa6ac67 | 2650 | { |
f70f513f | 2651 | tree lhs = gimple_call_lhs (stmt); |
ceb49bba | 2652 | if (dump_enabled_p ()) |
2653 | { | |
4c8041d7 | 2654 | location_t loc = gimple_location_safe (stmt); |
ceb49bba | 2655 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
2656 | "folding virtual function call to %s\n", | |
2657 | targets.length () == 1 | |
2658 | ? targets[0]->name () | |
2659 | : "__builtin_unreachable"); | |
2660 | } | |
76058579 | 2661 | if (targets.length () == 1) |
3aa6ac67 | 2662 | { |
68b0b56c | 2663 | gimple_call_set_fndecl (stmt, targets[0]->decl); |
3aa6ac67 | 2664 | changed = true; |
f70f513f | 2665 | /* If the call becomes noreturn, remove the lhs. */ |
2666 | if (lhs && (gimple_call_flags (stmt) & ECF_NORETURN)) | |
2667 | { | |
2668 | if (TREE_CODE (lhs) == SSA_NAME) | |
2669 | { | |
2670 | tree var = create_tmp_var (TREE_TYPE (lhs), NULL); | |
2671 | tree def = get_or_create_ssa_default_def (cfun, var); | |
2672 | gimple new_stmt = gimple_build_assign (lhs, def); | |
2673 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
2674 | } | |
2675 | gimple_call_set_lhs (stmt, NULL_TREE); | |
2676 | } | |
3aa6ac67 | 2677 | } |
f70f513f | 2678 | else |
68b0b56c | 2679 | { |
2680 | tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
2681 | gimple new_stmt = gimple_build_call (fndecl, 0); | |
2682 | gimple_set_location (new_stmt, gimple_location (stmt)); | |
f70f513f | 2683 | if (lhs && TREE_CODE (lhs) == SSA_NAME) |
2684 | { | |
2685 | tree var = create_tmp_var (TREE_TYPE (lhs), NULL); | |
2686 | tree def = get_or_create_ssa_default_def (cfun, var); | |
ac3cd7ce | 2687 | |
2688 | /* To satisfy condition for | |
2689 | cgraph_update_edges_for_call_stmt_node, | |
2690 | we need to preserve GIMPLE_CALL statement | |
2691 | at position of GSI iterator. */ | |
f70f513f | 2692 | update_call_from_tree (gsi, def); |
ac3cd7ce | 2693 | gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT); |
f70f513f | 2694 | } |
2695 | else | |
7f1b049e | 2696 | { |
2697 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
2698 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
2699 | gsi_replace (gsi, new_stmt, false); | |
2700 | } | |
68b0b56c | 2701 | return true; |
2702 | } | |
3aa6ac67 | 2703 | } |
3658fd1d | 2704 | } |
3aa6ac67 | 2705 | } |
3658fd1d | 2706 | |
3aa6ac67 | 2707 | if (inplace) |
2708 | return changed; | |
2709 | ||
2710 | /* Check for builtins that CCP can handle using information not | |
2711 | available in the generic fold routines. */ | |
b9ea678c | 2712 | if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) |
2713 | { | |
2714 | if (gimple_fold_builtin (gsi)) | |
2715 | changed = true; | |
2716 | } | |
2717 | else if (gimple_call_builtin_p (stmt, BUILT_IN_MD)) | |
3aa6ac67 | 2718 | { |
29cad3b6 | 2719 | changed |= targetm.gimple_fold_builtin (gsi); |
6fec5449 | 2720 | } |
ce8e6661 | 2721 | else if (gimple_call_internal_p (stmt)) |
c83059be | 2722 | { |
ce8e6661 | 2723 | enum tree_code subcode = ERROR_MARK; |
2724 | tree result = NULL_TREE; | |
2725 | switch (gimple_call_internal_fn (stmt)) | |
2726 | { | |
2727 | case IFN_BUILTIN_EXPECT: | |
2728 | result = fold_builtin_expect (gimple_location (stmt), | |
2729 | gimple_call_arg (stmt, 0), | |
2730 | gimple_call_arg (stmt, 1), | |
2731 | gimple_call_arg (stmt, 2)); | |
2732 | break; | |
0b45f2d1 | 2733 | case IFN_UBSAN_OBJECT_SIZE: |
2734 | if (integer_all_onesp (gimple_call_arg (stmt, 2)) | |
2735 | || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST | |
2736 | && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST | |
2737 | && tree_int_cst_le (gimple_call_arg (stmt, 1), | |
2738 | gimple_call_arg (stmt, 2)))) | |
2739 | { | |
2740 | gsi_replace (gsi, gimple_build_nop (), true); | |
2741 | unlink_stmt_vdef (stmt); | |
2742 | release_defs (stmt); | |
2743 | return true; | |
2744 | } | |
2745 | break; | |
ce8e6661 | 2746 | case IFN_UBSAN_CHECK_ADD: |
2747 | subcode = PLUS_EXPR; | |
2748 | break; | |
2749 | case IFN_UBSAN_CHECK_SUB: | |
2750 | subcode = MINUS_EXPR; | |
2751 | break; | |
2752 | case IFN_UBSAN_CHECK_MUL: | |
2753 | subcode = MULT_EXPR; | |
2754 | break; | |
2755 | default: | |
2756 | break; | |
2757 | } | |
2758 | if (subcode != ERROR_MARK) | |
2759 | { | |
2760 | tree arg0 = gimple_call_arg (stmt, 0); | |
2761 | tree arg1 = gimple_call_arg (stmt, 1); | |
2762 | /* x = y + 0; x = y - 0; x = y * 0; */ | |
2763 | if (integer_zerop (arg1)) | |
2764 | result = subcode == MULT_EXPR | |
2765 | ? build_zero_cst (TREE_TYPE (arg0)) | |
2766 | : arg0; | |
2767 | /* x = 0 + y; x = 0 * y; */ | |
2768 | else if (subcode != MINUS_EXPR && integer_zerop (arg0)) | |
2769 | result = subcode == MULT_EXPR | |
2770 | ? build_zero_cst (TREE_TYPE (arg0)) | |
2771 | : arg1; | |
2772 | /* x = y - y; */ | |
2773 | else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0)) | |
2774 | result = build_zero_cst (TREE_TYPE (arg0)); | |
2775 | /* x = y * 1; x = 1 * y; */ | |
2776 | else if (subcode == MULT_EXPR) | |
2777 | { | |
2778 | if (integer_onep (arg1)) | |
2779 | result = arg0; | |
2780 | else if (integer_onep (arg0)) | |
2781 | result = arg1; | |
2782 | } | |
2783 | } | |
c83059be | 2784 | if (result) |
2785 | { | |
2786 | if (!update_call_from_tree (gsi, result)) | |
2787 | gimplify_and_update_call_from_tree (gsi, result); | |
2788 | changed = true; | |
2789 | } | |
2790 | } | |
6fec5449 | 2791 | |
3aa6ac67 | 2792 | return changed; |
2d18b16d | 2793 | } |
2794 | ||
1bd2e4a0 | 2795 | /* Canonicalize MEM_REFs invariant address operand after propagation. */ |
2796 | ||
2797 | static bool | |
2798 | maybe_canonicalize_mem_ref_addr (tree *t) | |
2799 | { | |
2800 | bool res = false; | |
2801 | ||
2802 | if (TREE_CODE (*t) == ADDR_EXPR) | |
2803 | t = &TREE_OPERAND (*t, 0); | |
2804 | ||
2805 | while (handled_component_p (*t)) | |
2806 | t = &TREE_OPERAND (*t, 0); | |
2807 | ||
2808 | /* Canonicalize MEM [&foo.bar, 0] which appears after propagating | |
2809 | of invariant addresses into a SSA name MEM_REF address. */ | |
2810 | if (TREE_CODE (*t) == MEM_REF | |
2811 | || TREE_CODE (*t) == TARGET_MEM_REF) | |
2812 | { | |
2813 | tree addr = TREE_OPERAND (*t, 0); | |
2814 | if (TREE_CODE (addr) == ADDR_EXPR | |
2815 | && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF | |
2816 | || handled_component_p (TREE_OPERAND (addr, 0)))) | |
2817 | { | |
2818 | tree base; | |
2819 | HOST_WIDE_INT coffset; | |
2820 | base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), | |
2821 | &coffset); | |
2822 | if (!base) | |
2823 | gcc_unreachable (); | |
2824 | ||
2825 | TREE_OPERAND (*t, 0) = build_fold_addr_expr (base); | |
2826 | TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR, | |
2827 | TREE_OPERAND (*t, 1), | |
2828 | size_int (coffset)); | |
2829 | res = true; | |
2830 | } | |
2831 | gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL | |
2832 | || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0))); | |
2833 | } | |
2834 | ||
2835 | /* Canonicalize back MEM_REFs to plain reference trees if the object | |
2836 | accessed is a decl that has the same access semantics as the MEM_REF. */ | |
2837 | if (TREE_CODE (*t) == MEM_REF | |
2838 | && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR | |
2839 | && integer_zerop (TREE_OPERAND (*t, 1))) | |
2840 | { | |
2841 | tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
2842 | tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1)); | |
2843 | if (/* Same volatile qualification. */ | |
2844 | TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl) | |
2845 | /* Same TBAA behavior with -fstrict-aliasing. */ | |
2846 | && !TYPE_REF_CAN_ALIAS_ALL (alias_type) | |
2847 | && (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) | |
2848 | == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type))) | |
2849 | /* Same alignment. */ | |
2850 | && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t)) | |
2851 | /* We have to look out here to not drop a required conversion | |
2852 | from the rhs to the lhs if *t appears on the lhs or vice-versa | |
2853 | if it appears on the rhs. Thus require strict type | |
2854 | compatibility. */ | |
2855 | && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl))) | |
2856 | { | |
2857 | *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
2858 | res = true; | |
2859 | } | |
2860 | } | |
2861 | ||
2862 | /* Canonicalize TARGET_MEM_REF in particular with respect to | |
2863 | the indexes becoming constant. */ | |
2864 | else if (TREE_CODE (*t) == TARGET_MEM_REF) | |
2865 | { | |
2866 | tree tem = maybe_fold_tmr (*t); | |
2867 | if (tem) | |
2868 | { | |
2869 | *t = tem; | |
2870 | res = true; | |
2871 | } | |
2872 | } | |
2873 | ||
2874 | return res; | |
2875 | } | |
2876 | ||
2d18b16d | 2877 | /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument |
2878 | distinguishes both cases. */ | |
2879 | ||
2880 | static bool | |
2881 | fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace) | |
2882 | { | |
2883 | bool changed = false; | |
2884 | gimple stmt = gsi_stmt (*gsi); | |
2885 | unsigned i; | |
2886 | ||
1bd2e4a0 | 2887 | /* First do required canonicalization of [TARGET_]MEM_REF addresses |
2888 | after propagation. | |
2889 | ??? This shouldn't be done in generic folding but in the | |
2890 | propagation helpers which also know whether an address was | |
2891 | propagated. */ | |
2892 | switch (gimple_code (stmt)) | |
2893 | { | |
2894 | case GIMPLE_ASSIGN: | |
2895 | if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS) | |
2896 | { | |
2897 | tree *rhs = gimple_assign_rhs1_ptr (stmt); | |
2898 | if ((REFERENCE_CLASS_P (*rhs) | |
2899 | || TREE_CODE (*rhs) == ADDR_EXPR) | |
2900 | && maybe_canonicalize_mem_ref_addr (rhs)) | |
2901 | changed = true; | |
2902 | tree *lhs = gimple_assign_lhs_ptr (stmt); | |
2903 | if (REFERENCE_CLASS_P (*lhs) | |
2904 | && maybe_canonicalize_mem_ref_addr (lhs)) | |
2905 | changed = true; | |
2906 | } | |
2907 | break; | |
2908 | case GIMPLE_CALL: | |
2909 | { | |
2910 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
2911 | { | |
2912 | tree *arg = gimple_call_arg_ptr (stmt, i); | |
2913 | if (REFERENCE_CLASS_P (*arg) | |
2914 | && maybe_canonicalize_mem_ref_addr (arg)) | |
2915 | changed = true; | |
2916 | } | |
2917 | tree *lhs = gimple_call_lhs_ptr (stmt); | |
2918 | if (*lhs | |
2919 | && REFERENCE_CLASS_P (*lhs) | |
2920 | && maybe_canonicalize_mem_ref_addr (lhs)) | |
2921 | changed = true; | |
2922 | break; | |
2923 | } | |
2924 | case GIMPLE_ASM: | |
2925 | { | |
2926 | for (i = 0; i < gimple_asm_noutputs (stmt); ++i) | |
2927 | { | |
2928 | tree link = gimple_asm_output_op (stmt, i); | |
2929 | tree op = TREE_VALUE (link); | |
2930 | if (REFERENCE_CLASS_P (op) | |
2931 | && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link))) | |
2932 | changed = true; | |
2933 | } | |
2934 | for (i = 0; i < gimple_asm_ninputs (stmt); ++i) | |
2935 | { | |
2936 | tree link = gimple_asm_input_op (stmt, i); | |
2937 | tree op = TREE_VALUE (link); | |
2938 | if ((REFERENCE_CLASS_P (op) | |
2939 | || TREE_CODE (op) == ADDR_EXPR) | |
2940 | && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link))) | |
2941 | changed = true; | |
2942 | } | |
2943 | } | |
2944 | break; | |
2945 | case GIMPLE_DEBUG: | |
2946 | if (gimple_debug_bind_p (stmt)) | |
2947 | { | |
2948 | tree *val = gimple_debug_bind_get_value_ptr (stmt); | |
2949 | if (*val | |
2950 | && (REFERENCE_CLASS_P (*val) | |
2951 | || TREE_CODE (*val) == ADDR_EXPR) | |
2952 | && maybe_canonicalize_mem_ref_addr (val)) | |
2953 | changed = true; | |
2954 | } | |
2955 | break; | |
2956 | default:; | |
2957 | } | |
2958 | ||
2d18b16d | 2959 | /* Fold the main computation performed by the statement. */ |
2960 | switch (gimple_code (stmt)) | |
2961 | { | |
2962 | case GIMPLE_ASSIGN: | |
2963 | { | |
2964 | unsigned old_num_ops = gimple_num_ops (stmt); | |
fbd25d42 | 2965 | enum tree_code subcode = gimple_assign_rhs_code (stmt); |
2d18b16d | 2966 | tree lhs = gimple_assign_lhs (stmt); |
fbd25d42 | 2967 | tree new_rhs; |
2968 | /* First canonicalize operand order. This avoids building new | |
2969 | trees if this is the only thing fold would later do. */ | |
2970 | if ((commutative_tree_code (subcode) | |
2971 | || commutative_ternary_tree_code (subcode)) | |
2972 | && tree_swap_operands_p (gimple_assign_rhs1 (stmt), | |
2973 | gimple_assign_rhs2 (stmt), false)) | |
2974 | { | |
2975 | tree tem = gimple_assign_rhs1 (stmt); | |
2976 | gimple_assign_set_rhs1 (stmt, gimple_assign_rhs2 (stmt)); | |
2977 | gimple_assign_set_rhs2 (stmt, tem); | |
2978 | changed = true; | |
2979 | } | |
2980 | new_rhs = fold_gimple_assign (gsi); | |
2d18b16d | 2981 | if (new_rhs |
2982 | && !useless_type_conversion_p (TREE_TYPE (lhs), | |
2983 | TREE_TYPE (new_rhs))) | |
2984 | new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); | |
2985 | if (new_rhs | |
2986 | && (!inplace | |
2987 | || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) | |
2988 | { | |
2989 | gimple_assign_set_rhs_from_tree (gsi, new_rhs); | |
2990 | changed = true; | |
2991 | } | |
2992 | break; | |
2993 | } | |
2994 | ||
2995 | case GIMPLE_COND: | |
2996 | changed |= fold_gimple_cond (stmt); | |
2997 | break; | |
2998 | ||
2999 | case GIMPLE_CALL: | |
3fd0ca33 | 3000 | changed |= gimple_fold_call (gsi, inplace); |
2d18b16d | 3001 | break; |
3002 | ||
3003 | case GIMPLE_ASM: | |
3004 | /* Fold *& in asm operands. */ | |
a02dcaf7 | 3005 | { |
3006 | size_t noutputs; | |
3007 | const char **oconstraints; | |
3008 | const char *constraint; | |
3009 | bool allows_mem, allows_reg; | |
3010 | ||
3011 | noutputs = gimple_asm_noutputs (stmt); | |
3012 | oconstraints = XALLOCAVEC (const char *, noutputs); | |
3013 | ||
3014 | for (i = 0; i < gimple_asm_noutputs (stmt); ++i) | |
3015 | { | |
3016 | tree link = gimple_asm_output_op (stmt, i); | |
3017 | tree op = TREE_VALUE (link); | |
3018 | oconstraints[i] | |
3019 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
3020 | if (REFERENCE_CLASS_P (op) | |
3021 | && (op = maybe_fold_reference (op, true)) != NULL_TREE) | |
3022 | { | |
3023 | TREE_VALUE (link) = op; | |
3024 | changed = true; | |
3025 | } | |
3026 | } | |
3027 | for (i = 0; i < gimple_asm_ninputs (stmt); ++i) | |
3028 | { | |
3029 | tree link = gimple_asm_input_op (stmt, i); | |
3030 | tree op = TREE_VALUE (link); | |
3031 | constraint | |
3032 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
3033 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, | |
3034 | oconstraints, &allows_mem, &allows_reg); | |
3035 | if (REFERENCE_CLASS_P (op) | |
3036 | && (op = maybe_fold_reference (op, !allows_reg && allows_mem)) | |
3037 | != NULL_TREE) | |
3038 | { | |
3039 | TREE_VALUE (link) = op; | |
3040 | changed = true; | |
3041 | } | |
3042 | } | |
3043 | } | |
2d18b16d | 3044 | break; |
3045 | ||
69f01c7b | 3046 | case GIMPLE_DEBUG: |
3047 | if (gimple_debug_bind_p (stmt)) | |
3048 | { | |
3049 | tree val = gimple_debug_bind_get_value (stmt); | |
3050 | if (val | |
3051 | && REFERENCE_CLASS_P (val)) | |
3052 | { | |
3053 | tree tem = maybe_fold_reference (val, false); | |
3054 | if (tem) | |
3055 | { | |
3056 | gimple_debug_bind_set_value (stmt, tem); | |
3057 | changed = true; | |
3058 | } | |
3059 | } | |
48bcb852 | 3060 | else if (val |
3061 | && TREE_CODE (val) == ADDR_EXPR) | |
3062 | { | |
3063 | tree ref = TREE_OPERAND (val, 0); | |
3064 | tree tem = maybe_fold_reference (ref, false); | |
3065 | if (tem) | |
3066 | { | |
3067 | tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val)); | |
3068 | gimple_debug_bind_set_value (stmt, tem); | |
3069 | changed = true; | |
3070 | } | |
3071 | } | |
69f01c7b | 3072 | } |
3073 | break; | |
3074 | ||
2d18b16d | 3075 | default:; |
3076 | } | |
3077 | ||
3078 | stmt = gsi_stmt (*gsi); | |
3079 | ||
5352fc9b | 3080 | /* Fold *& on the lhs. */ |
3081 | if (gimple_has_lhs (stmt)) | |
2d18b16d | 3082 | { |
3083 | tree lhs = gimple_get_lhs (stmt); | |
3084 | if (lhs && REFERENCE_CLASS_P (lhs)) | |
3085 | { | |
3086 | tree new_lhs = maybe_fold_reference (lhs, true); | |
3087 | if (new_lhs) | |
3088 | { | |
3089 | gimple_set_lhs (stmt, new_lhs); | |
3090 | changed = true; | |
3091 | } | |
3092 | } | |
3093 | } | |
3094 | ||
3095 | return changed; | |
3096 | } | |
3097 | ||
3098 | /* Fold the statement pointed to by GSI. In some cases, this function may | |
3099 | replace the whole statement with a new one. Returns true iff folding | |
3100 | makes any changes. | |
3101 | The statement pointed to by GSI should be in valid gimple form but may | |
3102 | be in unfolded state as resulting from for example constant propagation | |
3103 | which can produce *&x = 0. */ | |
3104 | ||
3105 | bool | |
3106 | fold_stmt (gimple_stmt_iterator *gsi) | |
3107 | { | |
3108 | return fold_stmt_1 (gsi, false); | |
3109 | } | |
3110 | ||
50aacf4c | 3111 | /* Perform the minimal folding on statement *GSI. Only operations like |
2d18b16d | 3112 | *&x created by constant propagation are handled. The statement cannot |
3113 | be replaced with a new one. Return true if the statement was | |
3114 | changed, false otherwise. | |
50aacf4c | 3115 | The statement *GSI should be in valid gimple form but may |
2d18b16d | 3116 | be in unfolded state as resulting from for example constant propagation |
3117 | which can produce *&x = 0. */ | |
3118 | ||
3119 | bool | |
50aacf4c | 3120 | fold_stmt_inplace (gimple_stmt_iterator *gsi) |
2d18b16d | 3121 | { |
50aacf4c | 3122 | gimple stmt = gsi_stmt (*gsi); |
3123 | bool changed = fold_stmt_1 (gsi, true); | |
3124 | gcc_assert (gsi_stmt (*gsi) == stmt); | |
2d18b16d | 3125 | return changed; |
3126 | } | |
3127 | ||
c82d157a | 3128 | /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE |
3129 | if EXPR is null or we don't know how. | |
3130 | If non-null, the result always has boolean type. */ | |
3131 | ||
3132 | static tree | |
3133 | canonicalize_bool (tree expr, bool invert) | |
3134 | { | |
3135 | if (!expr) | |
3136 | return NULL_TREE; | |
3137 | else if (invert) | |
3138 | { | |
3139 | if (integer_nonzerop (expr)) | |
3140 | return boolean_false_node; | |
3141 | else if (integer_zerop (expr)) | |
3142 | return boolean_true_node; | |
3143 | else if (TREE_CODE (expr) == SSA_NAME) | |
3144 | return fold_build2 (EQ_EXPR, boolean_type_node, expr, | |
3145 | build_int_cst (TREE_TYPE (expr), 0)); | |
3146 | else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison) | |
3147 | return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false), | |
3148 | boolean_type_node, | |
3149 | TREE_OPERAND (expr, 0), | |
3150 | TREE_OPERAND (expr, 1)); | |
3151 | else | |
3152 | return NULL_TREE; | |
3153 | } | |
3154 | else | |
3155 | { | |
3156 | if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
3157 | return expr; | |
3158 | if (integer_nonzerop (expr)) | |
3159 | return boolean_true_node; | |
3160 | else if (integer_zerop (expr)) | |
3161 | return boolean_false_node; | |
3162 | else if (TREE_CODE (expr) == SSA_NAME) | |
3163 | return fold_build2 (NE_EXPR, boolean_type_node, expr, | |
3164 | build_int_cst (TREE_TYPE (expr), 0)); | |
3165 | else if (TREE_CODE_CLASS (TREE_CODE (expr)) == tcc_comparison) | |
3166 | return fold_build2 (TREE_CODE (expr), | |
3167 | boolean_type_node, | |
3168 | TREE_OPERAND (expr, 0), | |
3169 | TREE_OPERAND (expr, 1)); | |
3170 | else | |
3171 | return NULL_TREE; | |
3172 | } | |
3173 | } | |
3174 | ||
3175 | /* Check to see if a boolean expression EXPR is logically equivalent to the | |
3176 | comparison (OP1 CODE OP2). Check for various identities involving | |
3177 | SSA_NAMEs. */ | |
3178 | ||
3179 | static bool | |
3180 | same_bool_comparison_p (const_tree expr, enum tree_code code, | |
3181 | const_tree op1, const_tree op2) | |
3182 | { | |
3183 | gimple s; | |
3184 | ||
3185 | /* The obvious case. */ | |
3186 | if (TREE_CODE (expr) == code | |
3187 | && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0) | |
3188 | && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0)) | |
3189 | return true; | |
3190 | ||
3191 | /* Check for comparing (name, name != 0) and the case where expr | |
3192 | is an SSA_NAME with a definition matching the comparison. */ | |
3193 | if (TREE_CODE (expr) == SSA_NAME | |
3194 | && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
3195 | { | |
3196 | if (operand_equal_p (expr, op1, 0)) | |
3197 | return ((code == NE_EXPR && integer_zerop (op2)) | |
3198 | || (code == EQ_EXPR && integer_nonzerop (op2))); | |
3199 | s = SSA_NAME_DEF_STMT (expr); | |
3200 | if (is_gimple_assign (s) | |
3201 | && gimple_assign_rhs_code (s) == code | |
3202 | && operand_equal_p (gimple_assign_rhs1 (s), op1, 0) | |
3203 | && operand_equal_p (gimple_assign_rhs2 (s), op2, 0)) | |
3204 | return true; | |
3205 | } | |
3206 | ||
3207 | /* If op1 is of the form (name != 0) or (name == 0), and the definition | |
3208 | of name is a comparison, recurse. */ | |
3209 | if (TREE_CODE (op1) == SSA_NAME | |
3210 | && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE) | |
3211 | { | |
3212 | s = SSA_NAME_DEF_STMT (op1); | |
3213 | if (is_gimple_assign (s) | |
3214 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison) | |
3215 | { | |
3216 | enum tree_code c = gimple_assign_rhs_code (s); | |
3217 | if ((c == NE_EXPR && integer_zerop (op2)) | |
3218 | || (c == EQ_EXPR && integer_nonzerop (op2))) | |
3219 | return same_bool_comparison_p (expr, c, | |
3220 | gimple_assign_rhs1 (s), | |
3221 | gimple_assign_rhs2 (s)); | |
3222 | if ((c == EQ_EXPR && integer_zerop (op2)) | |
3223 | || (c == NE_EXPR && integer_nonzerop (op2))) | |
3224 | return same_bool_comparison_p (expr, | |
3225 | invert_tree_comparison (c, false), | |
3226 | gimple_assign_rhs1 (s), | |
3227 | gimple_assign_rhs2 (s)); | |
3228 | } | |
3229 | } | |
3230 | return false; | |
3231 | } | |
3232 | ||
3233 | /* Check to see if two boolean expressions OP1 and OP2 are logically | |
3234 | equivalent. */ | |
3235 | ||
3236 | static bool | |
3237 | same_bool_result_p (const_tree op1, const_tree op2) | |
3238 | { | |
3239 | /* Simple cases first. */ | |
3240 | if (operand_equal_p (op1, op2, 0)) | |
3241 | return true; | |
3242 | ||
3243 | /* Check the cases where at least one of the operands is a comparison. | |
3244 | These are a bit smarter than operand_equal_p in that they apply some | |
3245 | identifies on SSA_NAMEs. */ | |
3246 | if (TREE_CODE_CLASS (TREE_CODE (op2)) == tcc_comparison | |
3247 | && same_bool_comparison_p (op1, TREE_CODE (op2), | |
3248 | TREE_OPERAND (op2, 0), | |
3249 | TREE_OPERAND (op2, 1))) | |
3250 | return true; | |
3251 | if (TREE_CODE_CLASS (TREE_CODE (op1)) == tcc_comparison | |
3252 | && same_bool_comparison_p (op2, TREE_CODE (op1), | |
3253 | TREE_OPERAND (op1, 0), | |
3254 | TREE_OPERAND (op1, 1))) | |
3255 | return true; | |
3256 | ||
3257 | /* Default case. */ | |
3258 | return false; | |
3259 | } | |
3260 | ||
3261 | /* Forward declarations for some mutually recursive functions. */ | |
3262 | ||
3263 | static tree | |
3264 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
3265 | enum tree_code code2, tree op2a, tree op2b); | |
3266 | static tree | |
3267 | and_var_with_comparison (tree var, bool invert, | |
3268 | enum tree_code code2, tree op2a, tree op2b); | |
3269 | static tree | |
3270 | and_var_with_comparison_1 (gimple stmt, | |
3271 | enum tree_code code2, tree op2a, tree op2b); | |
3272 | static tree | |
3273 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
3274 | enum tree_code code2, tree op2a, tree op2b); | |
3275 | static tree | |
3276 | or_var_with_comparison (tree var, bool invert, | |
3277 | enum tree_code code2, tree op2a, tree op2b); | |
3278 | static tree | |
3279 | or_var_with_comparison_1 (gimple stmt, | |
3280 | enum tree_code code2, tree op2a, tree op2b); | |
3281 | ||
3282 | /* Helper function for and_comparisons_1: try to simplify the AND of the | |
3283 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
3284 | If INVERT is true, invert the value of the VAR before doing the AND. | |
3285 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
3286 | ||
3287 | static tree | |
3288 | and_var_with_comparison (tree var, bool invert, | |
3289 | enum tree_code code2, tree op2a, tree op2b) | |
3290 | { | |
3291 | tree t; | |
3292 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
3293 | ||
3294 | /* We can only deal with variables whose definitions are assignments. */ | |
3295 | if (!is_gimple_assign (stmt)) | |
3296 | return NULL_TREE; | |
3297 | ||
3298 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
3299 | !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b)) | |
3300 | Then we only have to consider the simpler non-inverted cases. */ | |
3301 | if (invert) | |
3302 | t = or_var_with_comparison_1 (stmt, | |
3303 | invert_tree_comparison (code2, false), | |
3304 | op2a, op2b); | |
3305 | else | |
3306 | t = and_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
3307 | return canonicalize_bool (t, invert); | |
3308 | } | |
3309 | ||
3310 | /* Try to simplify the AND of the ssa variable defined by the assignment | |
3311 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
3312 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
3313 | ||
3314 | static tree | |
3315 | and_var_with_comparison_1 (gimple stmt, | |
3316 | enum tree_code code2, tree op2a, tree op2b) | |
3317 | { | |
3318 | tree var = gimple_assign_lhs (stmt); | |
3319 | tree true_test_var = NULL_TREE; | |
3320 | tree false_test_var = NULL_TREE; | |
3321 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
3322 | ||
3323 | /* Check for identities like (var AND (var == 0)) => false. */ | |
3324 | if (TREE_CODE (op2a) == SSA_NAME | |
3325 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
3326 | { | |
3327 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
3328 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
3329 | { | |
3330 | true_test_var = op2a; | |
3331 | if (var == true_test_var) | |
3332 | return var; | |
3333 | } | |
3334 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
3335 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
3336 | { | |
3337 | false_test_var = op2a; | |
3338 | if (var == false_test_var) | |
3339 | return boolean_false_node; | |
3340 | } | |
3341 | } | |
3342 | ||
3343 | /* If the definition is a comparison, recurse on it. */ | |
3344 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
3345 | { | |
3346 | tree t = and_comparisons_1 (innercode, | |
3347 | gimple_assign_rhs1 (stmt), | |
3348 | gimple_assign_rhs2 (stmt), | |
3349 | code2, | |
3350 | op2a, | |
3351 | op2b); | |
3352 | if (t) | |
3353 | return t; | |
3354 | } | |
3355 | ||
3356 | /* If the definition is an AND or OR expression, we may be able to | |
3357 | simplify by reassociating. */ | |
08410c65 | 3358 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
3359 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
c82d157a | 3360 | { |
3361 | tree inner1 = gimple_assign_rhs1 (stmt); | |
3362 | tree inner2 = gimple_assign_rhs2 (stmt); | |
3363 | gimple s; | |
3364 | tree t; | |
3365 | tree partial = NULL_TREE; | |
08410c65 | 3366 | bool is_and = (innercode == BIT_AND_EXPR); |
c82d157a | 3367 | |
3368 | /* Check for boolean identities that don't require recursive examination | |
3369 | of inner1/inner2: | |
3370 | inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var | |
3371 | inner1 AND (inner1 OR inner2) => inner1 | |
3372 | !inner1 AND (inner1 AND inner2) => false | |
3373 | !inner1 AND (inner1 OR inner2) => !inner1 AND inner2 | |
3374 | Likewise for similar cases involving inner2. */ | |
3375 | if (inner1 == true_test_var) | |
3376 | return (is_and ? var : inner1); | |
3377 | else if (inner2 == true_test_var) | |
3378 | return (is_and ? var : inner2); | |
3379 | else if (inner1 == false_test_var) | |
3380 | return (is_and | |
3381 | ? boolean_false_node | |
3382 | : and_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
3383 | else if (inner2 == false_test_var) | |
3384 | return (is_and | |
3385 | ? boolean_false_node | |
3386 | : and_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
3387 | ||
3388 | /* Next, redistribute/reassociate the AND across the inner tests. | |
3389 | Compute the first partial result, (inner1 AND (op2a code op2b)) */ | |
3390 | if (TREE_CODE (inner1) == SSA_NAME | |
3391 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
3392 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
3393 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
3394 | gimple_assign_rhs1 (s), | |
3395 | gimple_assign_rhs2 (s), | |
3396 | code2, op2a, op2b))) | |
3397 | { | |
3398 | /* Handle the AND case, where we are reassociating: | |
3399 | (inner1 AND inner2) AND (op2a code2 op2b) | |
3400 | => (t AND inner2) | |
3401 | If the partial result t is a constant, we win. Otherwise | |
3402 | continue on to try reassociating with the other inner test. */ | |
3403 | if (is_and) | |
3404 | { | |
3405 | if (integer_onep (t)) | |
3406 | return inner2; | |
3407 | else if (integer_zerop (t)) | |
3408 | return boolean_false_node; | |
3409 | } | |
3410 | ||
3411 | /* Handle the OR case, where we are redistributing: | |
3412 | (inner1 OR inner2) AND (op2a code2 op2b) | |
3413 | => (t OR (inner2 AND (op2a code2 op2b))) */ | |
ad1e8581 | 3414 | else if (integer_onep (t)) |
3415 | return boolean_true_node; | |
3416 | ||
3417 | /* Save partial result for later. */ | |
3418 | partial = t; | |
c82d157a | 3419 | } |
3420 | ||
3421 | /* Compute the second partial result, (inner2 AND (op2a code op2b)) */ | |
3422 | if (TREE_CODE (inner2) == SSA_NAME | |
3423 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
3424 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
3425 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
3426 | gimple_assign_rhs1 (s), | |
3427 | gimple_assign_rhs2 (s), | |
3428 | code2, op2a, op2b))) | |
3429 | { | |
3430 | /* Handle the AND case, where we are reassociating: | |
3431 | (inner1 AND inner2) AND (op2a code2 op2b) | |
3432 | => (inner1 AND t) */ | |
3433 | if (is_and) | |
3434 | { | |
3435 | if (integer_onep (t)) | |
3436 | return inner1; | |
3437 | else if (integer_zerop (t)) | |
3438 | return boolean_false_node; | |
ad1e8581 | 3439 | /* If both are the same, we can apply the identity |
3440 | (x AND x) == x. */ | |
3441 | else if (partial && same_bool_result_p (t, partial)) | |
3442 | return t; | |
c82d157a | 3443 | } |
3444 | ||
3445 | /* Handle the OR case. where we are redistributing: | |
3446 | (inner1 OR inner2) AND (op2a code2 op2b) | |
3447 | => (t OR (inner1 AND (op2a code2 op2b))) | |
3448 | => (t OR partial) */ | |
3449 | else | |
3450 | { | |
3451 | if (integer_onep (t)) | |
3452 | return boolean_true_node; | |
3453 | else if (partial) | |
3454 | { | |
3455 | /* We already got a simplification for the other | |
3456 | operand to the redistributed OR expression. The | |
3457 | interesting case is when at least one is false. | |
3458 | Or, if both are the same, we can apply the identity | |
3459 | (x OR x) == x. */ | |
3460 | if (integer_zerop (partial)) | |
3461 | return t; | |
3462 | else if (integer_zerop (t)) | |
3463 | return partial; | |
3464 | else if (same_bool_result_p (t, partial)) | |
3465 | return t; | |
3466 | } | |
3467 | } | |
3468 | } | |
3469 | } | |
3470 | return NULL_TREE; | |
3471 | } | |
3472 | ||
3473 | /* Try to simplify the AND of two comparisons defined by | |
3474 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
3475 | If this can be done without constructing an intermediate value, | |
3476 | return the resulting tree; otherwise NULL_TREE is returned. | |
3477 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
3478 | in the first comparison but not the second. */ | |
3479 | ||
3480 | static tree | |
3481 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
3482 | enum tree_code code2, tree op2a, tree op2b) | |
3483 | { | |
f2c1848b | 3484 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
357d8e5d | 3485 | |
c82d157a | 3486 | /* First check for ((x CODE1 y) AND (x CODE2 y)). */ |
3487 | if (operand_equal_p (op1a, op2a, 0) | |
3488 | && operand_equal_p (op1b, op2b, 0)) | |
3489 | { | |
08410c65 | 3490 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 3491 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
3492 | TRUTH_ANDIF_EXPR, code1, code2, | |
357d8e5d | 3493 | truth_type, op1a, op1b); |
c82d157a | 3494 | if (t) |
3495 | return t; | |
3496 | } | |
3497 | ||
3498 | /* Likewise the swapped case of the above. */ | |
3499 | if (operand_equal_p (op1a, op2b, 0) | |
3500 | && operand_equal_p (op1b, op2a, 0)) | |
3501 | { | |
08410c65 | 3502 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 3503 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
3504 | TRUTH_ANDIF_EXPR, code1, | |
3505 | swap_tree_comparison (code2), | |
357d8e5d | 3506 | truth_type, op1a, op1b); |
c82d157a | 3507 | if (t) |
3508 | return t; | |
3509 | } | |
3510 | ||
3511 | /* If both comparisons are of the same value against constants, we might | |
3512 | be able to merge them. */ | |
3513 | if (operand_equal_p (op1a, op2a, 0) | |
3514 | && TREE_CODE (op1b) == INTEGER_CST | |
3515 | && TREE_CODE (op2b) == INTEGER_CST) | |
3516 | { | |
3517 | int cmp = tree_int_cst_compare (op1b, op2b); | |
3518 | ||
3519 | /* If we have (op1a == op1b), we should either be able to | |
3520 | return that or FALSE, depending on whether the constant op1b | |
3521 | also satisfies the other comparison against op2b. */ | |
3522 | if (code1 == EQ_EXPR) | |
3523 | { | |
3524 | bool done = true; | |
3525 | bool val; | |
3526 | switch (code2) | |
3527 | { | |
3528 | case EQ_EXPR: val = (cmp == 0); break; | |
3529 | case NE_EXPR: val = (cmp != 0); break; | |
3530 | case LT_EXPR: val = (cmp < 0); break; | |
3531 | case GT_EXPR: val = (cmp > 0); break; | |
3532 | case LE_EXPR: val = (cmp <= 0); break; | |
3533 | case GE_EXPR: val = (cmp >= 0); break; | |
3534 | default: done = false; | |
3535 | } | |
3536 | if (done) | |
3537 | { | |
3538 | if (val) | |
3539 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
3540 | else | |
3541 | return boolean_false_node; | |
3542 | } | |
3543 | } | |
3544 | /* Likewise if the second comparison is an == comparison. */ | |
3545 | else if (code2 == EQ_EXPR) | |
3546 | { | |
3547 | bool done = true; | |
3548 | bool val; | |
3549 | switch (code1) | |
3550 | { | |
3551 | case EQ_EXPR: val = (cmp == 0); break; | |
3552 | case NE_EXPR: val = (cmp != 0); break; | |
3553 | case LT_EXPR: val = (cmp > 0); break; | |
3554 | case GT_EXPR: val = (cmp < 0); break; | |
3555 | case LE_EXPR: val = (cmp >= 0); break; | |
3556 | case GE_EXPR: val = (cmp <= 0); break; | |
3557 | default: done = false; | |
3558 | } | |
3559 | if (done) | |
3560 | { | |
3561 | if (val) | |
3562 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
3563 | else | |
3564 | return boolean_false_node; | |
3565 | } | |
3566 | } | |
3567 | ||
3568 | /* Same business with inequality tests. */ | |
3569 | else if (code1 == NE_EXPR) | |
3570 | { | |
3571 | bool val; | |
3572 | switch (code2) | |
3573 | { | |
3574 | case EQ_EXPR: val = (cmp != 0); break; | |
3575 | case NE_EXPR: val = (cmp == 0); break; | |
3576 | case LT_EXPR: val = (cmp >= 0); break; | |
3577 | case GT_EXPR: val = (cmp <= 0); break; | |
3578 | case LE_EXPR: val = (cmp > 0); break; | |
3579 | case GE_EXPR: val = (cmp < 0); break; | |
3580 | default: | |
3581 | val = false; | |
3582 | } | |
3583 | if (val) | |
3584 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
3585 | } | |
3586 | else if (code2 == NE_EXPR) | |
3587 | { | |
3588 | bool val; | |
3589 | switch (code1) | |
3590 | { | |
3591 | case EQ_EXPR: val = (cmp == 0); break; | |
3592 | case NE_EXPR: val = (cmp != 0); break; | |
3593 | case LT_EXPR: val = (cmp <= 0); break; | |
3594 | case GT_EXPR: val = (cmp >= 0); break; | |
3595 | case LE_EXPR: val = (cmp < 0); break; | |
3596 | case GE_EXPR: val = (cmp > 0); break; | |
3597 | default: | |
3598 | val = false; | |
3599 | } | |
3600 | if (val) | |
3601 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
3602 | } | |
3603 | ||
3604 | /* Chose the more restrictive of two < or <= comparisons. */ | |
3605 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
3606 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
3607 | { | |
3608 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
3609 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
3610 | else | |
3611 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
3612 | } | |
3613 | ||
3614 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
3615 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
3616 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
3617 | { | |
3618 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
3619 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
3620 | else | |
3621 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
3622 | } | |
3623 | ||
3624 | /* Check for singleton ranges. */ | |
3625 | else if (cmp == 0 | |
3626 | && ((code1 == LE_EXPR && code2 == GE_EXPR) | |
3627 | || (code1 == GE_EXPR && code2 == LE_EXPR))) | |
3628 | return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b); | |
3629 | ||
3630 | /* Check for disjoint ranges. */ | |
3631 | else if (cmp <= 0 | |
3632 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
3633 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
3634 | return boolean_false_node; | |
3635 | else if (cmp >= 0 | |
3636 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
3637 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
3638 | return boolean_false_node; | |
3639 | } | |
3640 | ||
3641 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
3642 | NAME's definition is a truth value. See if there are any simplifications | |
3643 | that can be done against the NAME's definition. */ | |
3644 | if (TREE_CODE (op1a) == SSA_NAME | |
3645 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
3646 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
3647 | { | |
3648 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
3649 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
3650 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
3651 | switch (gimple_code (stmt)) | |
3652 | { | |
3653 | case GIMPLE_ASSIGN: | |
3654 | /* Try to simplify by copy-propagating the definition. */ | |
3655 | return and_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
3656 | ||
3657 | case GIMPLE_PHI: | |
3658 | /* If every argument to the PHI produces the same result when | |
3659 | ANDed with the second comparison, we win. | |
3660 | Do not do this unless the type is bool since we need a bool | |
3661 | result here anyway. */ | |
3662 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
3663 | { | |
3664 | tree result = NULL_TREE; | |
3665 | unsigned i; | |
3666 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
3667 | { | |
3668 | tree arg = gimple_phi_arg_def (stmt, i); | |
3669 | ||
3670 | /* If this PHI has itself as an argument, ignore it. | |
3671 | If all the other args produce the same result, | |
3672 | we're still OK. */ | |
3673 | if (arg == gimple_phi_result (stmt)) | |
3674 | continue; | |
3675 | else if (TREE_CODE (arg) == INTEGER_CST) | |
3676 | { | |
3677 | if (invert ? integer_nonzerop (arg) : integer_zerop (arg)) | |
3678 | { | |
3679 | if (!result) | |
3680 | result = boolean_false_node; | |
3681 | else if (!integer_zerop (result)) | |
3682 | return NULL_TREE; | |
3683 | } | |
3684 | else if (!result) | |
3685 | result = fold_build2 (code2, boolean_type_node, | |
3686 | op2a, op2b); | |
3687 | else if (!same_bool_comparison_p (result, | |
3688 | code2, op2a, op2b)) | |
3689 | return NULL_TREE; | |
3690 | } | |
ae4330d7 | 3691 | else if (TREE_CODE (arg) == SSA_NAME |
3692 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
c82d157a | 3693 | { |
8a245b9d | 3694 | tree temp; |
3695 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
3696 | /* In simple cases we can look through PHI nodes, | |
3697 | but we have to be careful with loops. | |
3698 | See PR49073. */ | |
3699 | if (! dom_info_available_p (CDI_DOMINATORS) | |
3700 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
3701 | || dominated_by_p (CDI_DOMINATORS, | |
3702 | gimple_bb (def_stmt), | |
3703 | gimple_bb (stmt))) | |
3704 | return NULL_TREE; | |
3705 | temp = and_var_with_comparison (arg, invert, code2, | |
3706 | op2a, op2b); | |
c82d157a | 3707 | if (!temp) |
3708 | return NULL_TREE; | |
3709 | else if (!result) | |
3710 | result = temp; | |
3711 | else if (!same_bool_result_p (result, temp)) | |
3712 | return NULL_TREE; | |
3713 | } | |
3714 | else | |
3715 | return NULL_TREE; | |
3716 | } | |
3717 | return result; | |
3718 | } | |
3719 | ||
3720 | default: | |
3721 | break; | |
3722 | } | |
3723 | } | |
3724 | return NULL_TREE; | |
3725 | } | |
3726 | ||
3727 | /* Try to simplify the AND of two comparisons, specified by | |
3728 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
3729 | If this can be simplified to a single expression (without requiring | |
3730 | introducing more SSA variables to hold intermediate values), | |
3731 | return the resulting tree. Otherwise return NULL_TREE. | |
3732 | If the result expression is non-null, it has boolean type. */ | |
3733 | ||
3734 | tree | |
3735 | maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
3736 | enum tree_code code2, tree op2a, tree op2b) | |
3737 | { | |
3738 | tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
3739 | if (t) | |
3740 | return t; | |
3741 | else | |
3742 | return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
3743 | } | |
3744 | ||
3745 | /* Helper function for or_comparisons_1: try to simplify the OR of the | |
3746 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
3747 | If INVERT is true, invert the value of VAR before doing the OR. | |
3748 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
3749 | ||
3750 | static tree | |
3751 | or_var_with_comparison (tree var, bool invert, | |
3752 | enum tree_code code2, tree op2a, tree op2b) | |
3753 | { | |
3754 | tree t; | |
3755 | gimple stmt = SSA_NAME_DEF_STMT (var); | |
3756 | ||
3757 | /* We can only deal with variables whose definitions are assignments. */ | |
3758 | if (!is_gimple_assign (stmt)) | |
3759 | return NULL_TREE; | |
3760 | ||
3761 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
3762 | !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b)) | |
3763 | Then we only have to consider the simpler non-inverted cases. */ | |
3764 | if (invert) | |
3765 | t = and_var_with_comparison_1 (stmt, | |
3766 | invert_tree_comparison (code2, false), | |
3767 | op2a, op2b); | |
3768 | else | |
3769 | t = or_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
3770 | return canonicalize_bool (t, invert); | |
3771 | } | |
3772 | ||
3773 | /* Try to simplify the OR of the ssa variable defined by the assignment | |
3774 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
3775 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
3776 | ||
3777 | static tree | |
3778 | or_var_with_comparison_1 (gimple stmt, | |
3779 | enum tree_code code2, tree op2a, tree op2b) | |
3780 | { | |
3781 | tree var = gimple_assign_lhs (stmt); | |
3782 | tree true_test_var = NULL_TREE; | |
3783 | tree false_test_var = NULL_TREE; | |
3784 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
3785 | ||
3786 | /* Check for identities like (var OR (var != 0)) => true . */ | |
3787 | if (TREE_CODE (op2a) == SSA_NAME | |
3788 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
3789 | { | |
3790 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
3791 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
3792 | { | |
3793 | true_test_var = op2a; | |
3794 | if (var == true_test_var) | |
3795 | return var; | |
3796 | } | |
3797 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
3798 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
3799 | { | |
3800 | false_test_var = op2a; | |
3801 | if (var == false_test_var) | |
3802 | return boolean_true_node; | |
3803 | } | |
3804 | } | |
3805 | ||
3806 | /* If the definition is a comparison, recurse on it. */ | |
3807 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
3808 | { | |
3809 | tree t = or_comparisons_1 (innercode, | |
3810 | gimple_assign_rhs1 (stmt), | |
3811 | gimple_assign_rhs2 (stmt), | |
3812 | code2, | |
3813 | op2a, | |
3814 | op2b); | |
3815 | if (t) | |
3816 | return t; | |
3817 | } | |
3818 | ||
3819 | /* If the definition is an AND or OR expression, we may be able to | |
3820 | simplify by reassociating. */ | |
08410c65 | 3821 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
3822 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
c82d157a | 3823 | { |
3824 | tree inner1 = gimple_assign_rhs1 (stmt); | |
3825 | tree inner2 = gimple_assign_rhs2 (stmt); | |
3826 | gimple s; | |
3827 | tree t; | |
3828 | tree partial = NULL_TREE; | |
08410c65 | 3829 | bool is_or = (innercode == BIT_IOR_EXPR); |
c82d157a | 3830 | |
3831 | /* Check for boolean identities that don't require recursive examination | |
3832 | of inner1/inner2: | |
3833 | inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var | |
3834 | inner1 OR (inner1 AND inner2) => inner1 | |
3835 | !inner1 OR (inner1 OR inner2) => true | |
3836 | !inner1 OR (inner1 AND inner2) => !inner1 OR inner2 | |
3837 | */ | |
3838 | if (inner1 == true_test_var) | |
3839 | return (is_or ? var : inner1); | |
3840 | else if (inner2 == true_test_var) | |
3841 | return (is_or ? var : inner2); | |
3842 | else if (inner1 == false_test_var) | |
3843 | return (is_or | |
3844 | ? boolean_true_node | |
3845 | : or_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
3846 | else if (inner2 == false_test_var) | |
3847 | return (is_or | |
3848 | ? boolean_true_node | |
3849 | : or_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
3850 | ||
3851 | /* Next, redistribute/reassociate the OR across the inner tests. | |
3852 | Compute the first partial result, (inner1 OR (op2a code op2b)) */ | |
3853 | if (TREE_CODE (inner1) == SSA_NAME | |
3854 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
3855 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
3856 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
3857 | gimple_assign_rhs1 (s), | |
3858 | gimple_assign_rhs2 (s), | |
3859 | code2, op2a, op2b))) | |
3860 | { | |
3861 | /* Handle the OR case, where we are reassociating: | |
3862 | (inner1 OR inner2) OR (op2a code2 op2b) | |
3863 | => (t OR inner2) | |
3864 | If the partial result t is a constant, we win. Otherwise | |
3865 | continue on to try reassociating with the other inner test. */ | |
ad1e8581 | 3866 | if (is_or) |
c82d157a | 3867 | { |
3868 | if (integer_onep (t)) | |
3869 | return boolean_true_node; | |
3870 | else if (integer_zerop (t)) | |
3871 | return inner2; | |
3872 | } | |
3873 | ||
3874 | /* Handle the AND case, where we are redistributing: | |
3875 | (inner1 AND inner2) OR (op2a code2 op2b) | |
3876 | => (t AND (inner2 OR (op2a code op2b))) */ | |
ad1e8581 | 3877 | else if (integer_zerop (t)) |
3878 | return boolean_false_node; | |
3879 | ||
3880 | /* Save partial result for later. */ | |
3881 | partial = t; | |
c82d157a | 3882 | } |
3883 | ||
3884 | /* Compute the second partial result, (inner2 OR (op2a code op2b)) */ | |
3885 | if (TREE_CODE (inner2) == SSA_NAME | |
3886 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
3887 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
3888 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
3889 | gimple_assign_rhs1 (s), | |
3890 | gimple_assign_rhs2 (s), | |
3891 | code2, op2a, op2b))) | |
3892 | { | |
3893 | /* Handle the OR case, where we are reassociating: | |
3894 | (inner1 OR inner2) OR (op2a code2 op2b) | |
ad1e8581 | 3895 | => (inner1 OR t) |
3896 | => (t OR partial) */ | |
3897 | if (is_or) | |
c82d157a | 3898 | { |
3899 | if (integer_zerop (t)) | |
3900 | return inner1; | |
3901 | else if (integer_onep (t)) | |
3902 | return boolean_true_node; | |
ad1e8581 | 3903 | /* If both are the same, we can apply the identity |
3904 | (x OR x) == x. */ | |
3905 | else if (partial && same_bool_result_p (t, partial)) | |
3906 | return t; | |
c82d157a | 3907 | } |
3908 | ||
3909 | /* Handle the AND case, where we are redistributing: | |
3910 | (inner1 AND inner2) OR (op2a code2 op2b) | |
3911 | => (t AND (inner1 OR (op2a code2 op2b))) | |
3912 | => (t AND partial) */ | |
3913 | else | |
3914 | { | |
3915 | if (integer_zerop (t)) | |
3916 | return boolean_false_node; | |
3917 | else if (partial) | |
3918 | { | |
3919 | /* We already got a simplification for the other | |
3920 | operand to the redistributed AND expression. The | |
3921 | interesting case is when at least one is true. | |
3922 | Or, if both are the same, we can apply the identity | |
ad1e8581 | 3923 | (x AND x) == x. */ |
c82d157a | 3924 | if (integer_onep (partial)) |
3925 | return t; | |
3926 | else if (integer_onep (t)) | |
3927 | return partial; | |
3928 | else if (same_bool_result_p (t, partial)) | |
ad1e8581 | 3929 | return t; |
c82d157a | 3930 | } |
3931 | } | |
3932 | } | |
3933 | } | |
3934 | return NULL_TREE; | |
3935 | } | |
3936 | ||
3937 | /* Try to simplify the OR of two comparisons defined by | |
3938 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
3939 | If this can be done without constructing an intermediate value, | |
3940 | return the resulting tree; otherwise NULL_TREE is returned. | |
3941 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
3942 | in the first comparison but not the second. */ | |
3943 | ||
3944 | static tree | |
3945 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
3946 | enum tree_code code2, tree op2a, tree op2b) | |
3947 | { | |
f2c1848b | 3948 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
357d8e5d | 3949 | |
c82d157a | 3950 | /* First check for ((x CODE1 y) OR (x CODE2 y)). */ |
3951 | if (operand_equal_p (op1a, op2a, 0) | |
3952 | && operand_equal_p (op1b, op2b, 0)) | |
3953 | { | |
08410c65 | 3954 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 3955 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
3956 | TRUTH_ORIF_EXPR, code1, code2, | |
357d8e5d | 3957 | truth_type, op1a, op1b); |
c82d157a | 3958 | if (t) |
3959 | return t; | |
3960 | } | |
3961 | ||
3962 | /* Likewise the swapped case of the above. */ | |
3963 | if (operand_equal_p (op1a, op2b, 0) | |
3964 | && operand_equal_p (op1b, op2a, 0)) | |
3965 | { | |
08410c65 | 3966 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 3967 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
3968 | TRUTH_ORIF_EXPR, code1, | |
3969 | swap_tree_comparison (code2), | |
357d8e5d | 3970 | truth_type, op1a, op1b); |
c82d157a | 3971 | if (t) |
3972 | return t; | |
3973 | } | |
3974 | ||
3975 | /* If both comparisons are of the same value against constants, we might | |
3976 | be able to merge them. */ | |
3977 | if (operand_equal_p (op1a, op2a, 0) | |
3978 | && TREE_CODE (op1b) == INTEGER_CST | |
3979 | && TREE_CODE (op2b) == INTEGER_CST) | |
3980 | { | |
3981 | int cmp = tree_int_cst_compare (op1b, op2b); | |
3982 | ||
3983 | /* If we have (op1a != op1b), we should either be able to | |
3984 | return that or TRUE, depending on whether the constant op1b | |
3985 | also satisfies the other comparison against op2b. */ | |
3986 | if (code1 == NE_EXPR) | |
3987 | { | |
3988 | bool done = true; | |
3989 | bool val; | |
3990 | switch (code2) | |
3991 | { | |
3992 | case EQ_EXPR: val = (cmp == 0); break; | |
3993 | case NE_EXPR: val = (cmp != 0); break; | |
3994 | case LT_EXPR: val = (cmp < 0); break; | |
3995 | case GT_EXPR: val = (cmp > 0); break; | |
3996 | case LE_EXPR: val = (cmp <= 0); break; | |
3997 | case GE_EXPR: val = (cmp >= 0); break; | |
3998 | default: done = false; | |
3999 | } | |
4000 | if (done) | |
4001 | { | |
4002 | if (val) | |
4003 | return boolean_true_node; | |
4004 | else | |
4005 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4006 | } | |
4007 | } | |
4008 | /* Likewise if the second comparison is a != comparison. */ | |
4009 | else if (code2 == NE_EXPR) | |
4010 | { | |
4011 | bool done = true; | |
4012 | bool val; | |
4013 | switch (code1) | |
4014 | { | |
4015 | case EQ_EXPR: val = (cmp == 0); break; | |
4016 | case NE_EXPR: val = (cmp != 0); break; | |
4017 | case LT_EXPR: val = (cmp > 0); break; | |
4018 | case GT_EXPR: val = (cmp < 0); break; | |
4019 | case LE_EXPR: val = (cmp >= 0); break; | |
4020 | case GE_EXPR: val = (cmp <= 0); break; | |
4021 | default: done = false; | |
4022 | } | |
4023 | if (done) | |
4024 | { | |
4025 | if (val) | |
4026 | return boolean_true_node; | |
4027 | else | |
4028 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4029 | } | |
4030 | } | |
4031 | ||
4032 | /* See if an equality test is redundant with the other comparison. */ | |
4033 | else if (code1 == EQ_EXPR) | |
4034 | { | |
4035 | bool val; | |
4036 | switch (code2) | |
4037 | { | |
4038 | case EQ_EXPR: val = (cmp == 0); break; | |
4039 | case NE_EXPR: val = (cmp != 0); break; | |
4040 | case LT_EXPR: val = (cmp < 0); break; | |
4041 | case GT_EXPR: val = (cmp > 0); break; | |
4042 | case LE_EXPR: val = (cmp <= 0); break; | |
4043 | case GE_EXPR: val = (cmp >= 0); break; | |
4044 | default: | |
4045 | val = false; | |
4046 | } | |
4047 | if (val) | |
4048 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4049 | } | |
4050 | else if (code2 == EQ_EXPR) | |
4051 | { | |
4052 | bool val; | |
4053 | switch (code1) | |
4054 | { | |
4055 | case EQ_EXPR: val = (cmp == 0); break; | |
4056 | case NE_EXPR: val = (cmp != 0); break; | |
4057 | case LT_EXPR: val = (cmp > 0); break; | |
4058 | case GT_EXPR: val = (cmp < 0); break; | |
4059 | case LE_EXPR: val = (cmp >= 0); break; | |
4060 | case GE_EXPR: val = (cmp <= 0); break; | |
4061 | default: | |
4062 | val = false; | |
4063 | } | |
4064 | if (val) | |
4065 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4066 | } | |
4067 | ||
4068 | /* Chose the less restrictive of two < or <= comparisons. */ | |
4069 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
4070 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
4071 | { | |
4072 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
4073 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4074 | else | |
4075 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4076 | } | |
4077 | ||
4078 | /* Likewise chose the less restrictive of two > or >= comparisons. */ | |
4079 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
4080 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
4081 | { | |
4082 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
4083 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
4084 | else | |
4085 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
4086 | } | |
4087 | ||
4088 | /* Check for singleton ranges. */ | |
4089 | else if (cmp == 0 | |
4090 | && ((code1 == LT_EXPR && code2 == GT_EXPR) | |
4091 | || (code1 == GT_EXPR && code2 == LT_EXPR))) | |
4092 | return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b); | |
4093 | ||
4094 | /* Check for less/greater pairs that don't restrict the range at all. */ | |
4095 | else if (cmp >= 0 | |
4096 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
4097 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
4098 | return boolean_true_node; | |
4099 | else if (cmp <= 0 | |
4100 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
4101 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
4102 | return boolean_true_node; | |
4103 | } | |
4104 | ||
4105 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
4106 | NAME's definition is a truth value. See if there are any simplifications | |
4107 | that can be done against the NAME's definition. */ | |
4108 | if (TREE_CODE (op1a) == SSA_NAME | |
4109 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
4110 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
4111 | { | |
4112 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
4113 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
4114 | gimple stmt = SSA_NAME_DEF_STMT (op1a); | |
4115 | switch (gimple_code (stmt)) | |
4116 | { | |
4117 | case GIMPLE_ASSIGN: | |
4118 | /* Try to simplify by copy-propagating the definition. */ | |
4119 | return or_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
4120 | ||
4121 | case GIMPLE_PHI: | |
4122 | /* If every argument to the PHI produces the same result when | |
4123 | ORed with the second comparison, we win. | |
4124 | Do not do this unless the type is bool since we need a bool | |
4125 | result here anyway. */ | |
4126 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
4127 | { | |
4128 | tree result = NULL_TREE; | |
4129 | unsigned i; | |
4130 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
4131 | { | |
4132 | tree arg = gimple_phi_arg_def (stmt, i); | |
4133 | ||
4134 | /* If this PHI has itself as an argument, ignore it. | |
4135 | If all the other args produce the same result, | |
4136 | we're still OK. */ | |
4137 | if (arg == gimple_phi_result (stmt)) | |
4138 | continue; | |
4139 | else if (TREE_CODE (arg) == INTEGER_CST) | |
4140 | { | |
4141 | if (invert ? integer_zerop (arg) : integer_nonzerop (arg)) | |
4142 | { | |
4143 | if (!result) | |
4144 | result = boolean_true_node; | |
4145 | else if (!integer_onep (result)) | |
4146 | return NULL_TREE; | |
4147 | } | |
4148 | else if (!result) | |
4149 | result = fold_build2 (code2, boolean_type_node, | |
4150 | op2a, op2b); | |
4151 | else if (!same_bool_comparison_p (result, | |
4152 | code2, op2a, op2b)) | |
4153 | return NULL_TREE; | |
4154 | } | |
ae4330d7 | 4155 | else if (TREE_CODE (arg) == SSA_NAME |
4156 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
c82d157a | 4157 | { |
8a245b9d | 4158 | tree temp; |
4159 | gimple def_stmt = SSA_NAME_DEF_STMT (arg); | |
4160 | /* In simple cases we can look through PHI nodes, | |
4161 | but we have to be careful with loops. | |
4162 | See PR49073. */ | |
4163 | if (! dom_info_available_p (CDI_DOMINATORS) | |
4164 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
4165 | || dominated_by_p (CDI_DOMINATORS, | |
4166 | gimple_bb (def_stmt), | |
4167 | gimple_bb (stmt))) | |
4168 | return NULL_TREE; | |
4169 | temp = or_var_with_comparison (arg, invert, code2, | |
4170 | op2a, op2b); | |
c82d157a | 4171 | if (!temp) |
4172 | return NULL_TREE; | |
4173 | else if (!result) | |
4174 | result = temp; | |
4175 | else if (!same_bool_result_p (result, temp)) | |
4176 | return NULL_TREE; | |
4177 | } | |
4178 | else | |
4179 | return NULL_TREE; | |
4180 | } | |
4181 | return result; | |
4182 | } | |
4183 | ||
4184 | default: | |
4185 | break; | |
4186 | } | |
4187 | } | |
4188 | return NULL_TREE; | |
4189 | } | |
4190 | ||
4191 | /* Try to simplify the OR of two comparisons, specified by | |
4192 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
4193 | If this can be simplified to a single expression (without requiring | |
4194 | introducing more SSA variables to hold intermediate values), | |
4195 | return the resulting tree. Otherwise return NULL_TREE. | |
4196 | If the result expression is non-null, it has boolean type. */ | |
4197 | ||
4198 | tree | |
4199 | maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
4200 | enum tree_code code2, tree op2a, tree op2b) | |
4201 | { | |
4202 | tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
4203 | if (t) | |
4204 | return t; | |
4205 | else | |
4206 | return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
4207 | } | |
1d0b727d | 4208 | |
4209 | ||
4210 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
4211 | ||
4212 | Either NULL_TREE, a simplified but non-constant or a constant | |
4213 | is returned. | |
4214 | ||
4215 | ??? This should go into a gimple-fold-inline.h file to be eventually | |
4216 | privatized with the single valueize function used in the various TUs | |
4217 | to avoid the indirect function call overhead. */ | |
4218 | ||
4219 | tree | |
4220 | gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree)) | |
4221 | { | |
4222 | location_t loc = gimple_location (stmt); | |
4223 | switch (gimple_code (stmt)) | |
4224 | { | |
4225 | case GIMPLE_ASSIGN: | |
4226 | { | |
4227 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
4228 | ||
4229 | switch (get_gimple_rhs_class (subcode)) | |
4230 | { | |
4231 | case GIMPLE_SINGLE_RHS: | |
4232 | { | |
4233 | tree rhs = gimple_assign_rhs1 (stmt); | |
4234 | enum tree_code_class kind = TREE_CODE_CLASS (subcode); | |
4235 | ||
4236 | if (TREE_CODE (rhs) == SSA_NAME) | |
4237 | { | |
4238 | /* If the RHS is an SSA_NAME, return its known constant value, | |
4239 | if any. */ | |
4240 | return (*valueize) (rhs); | |
4241 | } | |
4242 | /* Handle propagating invariant addresses into address | |
4243 | operations. */ | |
4244 | else if (TREE_CODE (rhs) == ADDR_EXPR | |
4245 | && !is_gimple_min_invariant (rhs)) | |
4246 | { | |
03231f32 | 4247 | HOST_WIDE_INT offset = 0; |
1d0b727d | 4248 | tree base; |
4249 | base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0), | |
4250 | &offset, | |
4251 | valueize); | |
4252 | if (base | |
4253 | && (CONSTANT_CLASS_P (base) | |
4254 | || decl_address_invariant_p (base))) | |
4255 | return build_invariant_address (TREE_TYPE (rhs), | |
4256 | base, offset); | |
4257 | } | |
4258 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
4259 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
4260 | && (CONSTRUCTOR_NELTS (rhs) | |
4261 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
4262 | { | |
4263 | unsigned i; | |
fadf62f4 | 4264 | tree val, *vec; |
1d0b727d | 4265 | |
fadf62f4 | 4266 | vec = XALLOCAVEC (tree, |
4267 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))); | |
1d0b727d | 4268 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) |
4269 | { | |
4270 | val = (*valueize) (val); | |
4271 | if (TREE_CODE (val) == INTEGER_CST | |
4272 | || TREE_CODE (val) == REAL_CST | |
4273 | || TREE_CODE (val) == FIXED_CST) | |
fadf62f4 | 4274 | vec[i] = val; |
1d0b727d | 4275 | else |
4276 | return NULL_TREE; | |
4277 | } | |
4278 | ||
fadf62f4 | 4279 | return build_vector (TREE_TYPE (rhs), vec); |
1d0b727d | 4280 | } |
0329fcdb | 4281 | if (subcode == OBJ_TYPE_REF) |
4282 | { | |
4283 | tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs)); | |
4284 | /* If callee is constant, we can fold away the wrapper. */ | |
4285 | if (is_gimple_min_invariant (val)) | |
4286 | return val; | |
4287 | } | |
1d0b727d | 4288 | |
4289 | if (kind == tcc_reference) | |
4290 | { | |
4291 | if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR | |
4292 | || TREE_CODE (rhs) == REALPART_EXPR | |
4293 | || TREE_CODE (rhs) == IMAGPART_EXPR) | |
4294 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
4295 | { | |
4296 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
4297 | return fold_unary_loc (EXPR_LOCATION (rhs), | |
4298 | TREE_CODE (rhs), | |
4299 | TREE_TYPE (rhs), val); | |
4300 | } | |
4301 | else if (TREE_CODE (rhs) == BIT_FIELD_REF | |
4302 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
4303 | { | |
4304 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
4305 | return fold_ternary_loc (EXPR_LOCATION (rhs), | |
4306 | TREE_CODE (rhs), | |
4307 | TREE_TYPE (rhs), val, | |
4308 | TREE_OPERAND (rhs, 1), | |
4309 | TREE_OPERAND (rhs, 2)); | |
4310 | } | |
4311 | else if (TREE_CODE (rhs) == MEM_REF | |
4312 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
4313 | { | |
4314 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
4315 | if (TREE_CODE (val) == ADDR_EXPR | |
4316 | && is_gimple_min_invariant (val)) | |
4317 | { | |
4318 | tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs), | |
4319 | unshare_expr (val), | |
4320 | TREE_OPERAND (rhs, 1)); | |
4321 | if (tem) | |
4322 | rhs = tem; | |
4323 | } | |
4324 | } | |
4325 | return fold_const_aggregate_ref_1 (rhs, valueize); | |
4326 | } | |
4327 | else if (kind == tcc_declaration) | |
4328 | return get_symbol_constant_value (rhs); | |
4329 | return rhs; | |
4330 | } | |
4331 | ||
4332 | case GIMPLE_UNARY_RHS: | |
4333 | { | |
4334 | /* Handle unary operators that can appear in GIMPLE form. | |
4335 | Note that we know the single operand must be a constant, | |
4336 | so this should almost always return a simplified RHS. */ | |
1d0b727d | 4337 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); |
4338 | ||
1d0b727d | 4339 | return |
4340 | fold_unary_ignore_overflow_loc (loc, subcode, | |
4341 | gimple_expr_type (stmt), op0); | |
4342 | } | |
4343 | ||
4344 | case GIMPLE_BINARY_RHS: | |
4345 | { | |
4346 | /* Handle binary operators that can appear in GIMPLE form. */ | |
4347 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
4348 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
4349 | ||
4350 | /* Translate &x + CST into an invariant form suitable for | |
4351 | further propagation. */ | |
4352 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR | |
4353 | && TREE_CODE (op0) == ADDR_EXPR | |
4354 | && TREE_CODE (op1) == INTEGER_CST) | |
4355 | { | |
4356 | tree off = fold_convert (ptr_type_node, op1); | |
3048a5c0 | 4357 | return build_fold_addr_expr_loc |
4358 | (loc, | |
4359 | fold_build2 (MEM_REF, | |
1d0b727d | 4360 | TREE_TYPE (TREE_TYPE (op0)), |
4361 | unshare_expr (op0), off)); | |
4362 | } | |
4363 | ||
4364 | return fold_binary_loc (loc, subcode, | |
4365 | gimple_expr_type (stmt), op0, op1); | |
4366 | } | |
4367 | ||
4368 | case GIMPLE_TERNARY_RHS: | |
4369 | { | |
4370 | /* Handle ternary operators that can appear in GIMPLE form. */ | |
4371 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
4372 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
4373 | tree op2 = (*valueize) (gimple_assign_rhs3 (stmt)); | |
4374 | ||
ce993cc2 | 4375 | /* Fold embedded expressions in ternary codes. */ |
4376 | if ((subcode == COND_EXPR | |
4377 | || subcode == VEC_COND_EXPR) | |
4378 | && COMPARISON_CLASS_P (op0)) | |
4379 | { | |
4380 | tree op00 = (*valueize) (TREE_OPERAND (op0, 0)); | |
4381 | tree op01 = (*valueize) (TREE_OPERAND (op0, 1)); | |
4382 | tree tem = fold_binary_loc (loc, TREE_CODE (op0), | |
4383 | TREE_TYPE (op0), op00, op01); | |
4384 | if (tem) | |
4385 | op0 = tem; | |
4386 | } | |
4387 | ||
1d0b727d | 4388 | return fold_ternary_loc (loc, subcode, |
4389 | gimple_expr_type (stmt), op0, op1, op2); | |
4390 | } | |
4391 | ||
4392 | default: | |
4393 | gcc_unreachable (); | |
4394 | } | |
4395 | } | |
4396 | ||
4397 | case GIMPLE_CALL: | |
4398 | { | |
fb049fba | 4399 | tree fn; |
4400 | ||
4401 | if (gimple_call_internal_p (stmt)) | |
137559b2 | 4402 | { |
4403 | enum tree_code subcode = ERROR_MARK; | |
4404 | switch (gimple_call_internal_fn (stmt)) | |
4405 | { | |
4406 | case IFN_UBSAN_CHECK_ADD: | |
4407 | subcode = PLUS_EXPR; | |
4408 | break; | |
4409 | case IFN_UBSAN_CHECK_SUB: | |
4410 | subcode = MINUS_EXPR; | |
4411 | break; | |
4412 | case IFN_UBSAN_CHECK_MUL: | |
4413 | subcode = MULT_EXPR; | |
4414 | break; | |
4415 | default: | |
4416 | return NULL_TREE; | |
4417 | } | |
ce8e6661 | 4418 | tree arg0 = gimple_call_arg (stmt, 0); |
4419 | tree arg1 = gimple_call_arg (stmt, 1); | |
4420 | tree op0 = (*valueize) (arg0); | |
4421 | tree op1 = (*valueize) (arg1); | |
137559b2 | 4422 | |
4423 | if (TREE_CODE (op0) != INTEGER_CST | |
4424 | || TREE_CODE (op1) != INTEGER_CST) | |
ce8e6661 | 4425 | { |
4426 | switch (subcode) | |
4427 | { | |
4428 | case MULT_EXPR: | |
4429 | /* x * 0 = 0 * x = 0 without overflow. */ | |
4430 | if (integer_zerop (op0) || integer_zerop (op1)) | |
4431 | return build_zero_cst (TREE_TYPE (arg0)); | |
4432 | break; | |
4433 | case MINUS_EXPR: | |
4434 | /* y - y = 0 without overflow. */ | |
4435 | if (operand_equal_p (op0, op1, 0)) | |
4436 | return build_zero_cst (TREE_TYPE (arg0)); | |
4437 | break; | |
4438 | default: | |
4439 | break; | |
4440 | } | |
4441 | } | |
4442 | tree res | |
4443 | = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1); | |
137559b2 | 4444 | if (res |
4445 | && TREE_CODE (res) == INTEGER_CST | |
4446 | && !TREE_OVERFLOW (res)) | |
4447 | return res; | |
4448 | return NULL_TREE; | |
4449 | } | |
fb049fba | 4450 | |
4451 | fn = (*valueize) (gimple_call_fn (stmt)); | |
1d0b727d | 4452 | if (TREE_CODE (fn) == ADDR_EXPR |
4453 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
8ded4352 | 4454 | && DECL_BUILT_IN (TREE_OPERAND (fn, 0)) |
4455 | && gimple_builtin_call_types_compatible_p (stmt, | |
4456 | TREE_OPERAND (fn, 0))) | |
1d0b727d | 4457 | { |
4458 | tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt)); | |
4459 | tree call, retval; | |
4460 | unsigned i; | |
4461 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
4462 | args[i] = (*valueize) (gimple_call_arg (stmt, i)); | |
4463 | call = build_call_array_loc (loc, | |
4464 | gimple_call_return_type (stmt), | |
4465 | fn, gimple_call_num_args (stmt), args); | |
4466 | retval = fold_call_expr (EXPR_LOCATION (call), call, false); | |
4467 | if (retval) | |
8ded4352 | 4468 | { |
4469 | /* fold_call_expr wraps the result inside a NOP_EXPR. */ | |
4470 | STRIP_NOPS (retval); | |
4471 | retval = fold_convert (gimple_call_return_type (stmt), retval); | |
4472 | } | |
1d0b727d | 4473 | return retval; |
4474 | } | |
4475 | return NULL_TREE; | |
4476 | } | |
4477 | ||
4478 | default: | |
4479 | return NULL_TREE; | |
4480 | } | |
4481 | } | |
4482 | ||
4483 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
4484 | Returns NULL_TREE if folding to a constant is not possible, otherwise | |
4485 | returns a constant according to is_gimple_min_invariant. */ | |
4486 | ||
4487 | tree | |
4488 | gimple_fold_stmt_to_constant (gimple stmt, tree (*valueize) (tree)) | |
4489 | { | |
4490 | tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize); | |
4491 | if (res && is_gimple_min_invariant (res)) | |
4492 | return res; | |
4493 | return NULL_TREE; | |
4494 | } | |
4495 | ||
4496 | ||
4497 | /* The following set of functions are supposed to fold references using | |
4498 | their constant initializers. */ | |
4499 | ||
4500 | static tree fold_ctor_reference (tree type, tree ctor, | |
4501 | unsigned HOST_WIDE_INT offset, | |
a65b88bf | 4502 | unsigned HOST_WIDE_INT size, tree); |
1d0b727d | 4503 | |
4504 | /* See if we can find constructor defining value of BASE. | |
4505 | When we know the consructor with constant offset (such as | |
4506 | base is array[40] and we do know constructor of array), then | |
4507 | BIT_OFFSET is adjusted accordingly. | |
4508 | ||
4509 | As a special case, return error_mark_node when constructor | |
4510 | is not explicitly available, but it is known to be zero | |
4511 | such as 'static const int a;'. */ | |
4512 | static tree | |
4513 | get_base_constructor (tree base, HOST_WIDE_INT *bit_offset, | |
4514 | tree (*valueize)(tree)) | |
4515 | { | |
4516 | HOST_WIDE_INT bit_offset2, size, max_size; | |
4517 | if (TREE_CODE (base) == MEM_REF) | |
4518 | { | |
4519 | if (!integer_zerop (TREE_OPERAND (base, 1))) | |
4520 | { | |
e913b5cd | 4521 | if (!tree_fits_shwi_p (TREE_OPERAND (base, 1))) |
1d0b727d | 4522 | return NULL_TREE; |
e913b5cd | 4523 | *bit_offset += (mem_ref_offset (base).to_short_addr () |
1d0b727d | 4524 | * BITS_PER_UNIT); |
4525 | } | |
4526 | ||
4527 | if (valueize | |
4528 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) | |
4529 | base = valueize (TREE_OPERAND (base, 0)); | |
4530 | if (!base || TREE_CODE (base) != ADDR_EXPR) | |
4531 | return NULL_TREE; | |
4532 | base = TREE_OPERAND (base, 0); | |
4533 | } | |
4534 | ||
4535 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
4536 | DECL_INITIAL. If BASE is a nested reference into another | |
4537 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
4538 | the inner reference. */ | |
4539 | switch (TREE_CODE (base)) | |
4540 | { | |
4541 | case VAR_DECL: | |
1d0b727d | 4542 | case CONST_DECL: |
df8d3e89 | 4543 | { |
4544 | tree init = ctor_for_folding (base); | |
4545 | ||
a04e8d62 | 4546 | /* Our semantic is exact opposite of ctor_for_folding; |
df8d3e89 | 4547 | NULL means unknown, while error_mark_node is 0. */ |
4548 | if (init == error_mark_node) | |
4549 | return NULL_TREE; | |
4550 | if (!init) | |
4551 | return error_mark_node; | |
4552 | return init; | |
4553 | } | |
1d0b727d | 4554 | |
4555 | case ARRAY_REF: | |
4556 | case COMPONENT_REF: | |
4557 | base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size); | |
4558 | if (max_size == -1 || size != max_size) | |
4559 | return NULL_TREE; | |
4560 | *bit_offset += bit_offset2; | |
4561 | return get_base_constructor (base, bit_offset, valueize); | |
4562 | ||
4563 | case STRING_CST: | |
4564 | case CONSTRUCTOR: | |
4565 | return base; | |
4566 | ||
4567 | default: | |
4568 | return NULL_TREE; | |
4569 | } | |
4570 | } | |
4571 | ||
1d0b727d | 4572 | /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size |
4573 | SIZE to the memory at bit OFFSET. */ | |
4574 | ||
4575 | static tree | |
4576 | fold_array_ctor_reference (tree type, tree ctor, | |
4577 | unsigned HOST_WIDE_INT offset, | |
a65b88bf | 4578 | unsigned HOST_WIDE_INT size, |
4579 | tree from_decl) | |
1d0b727d | 4580 | { |
4581 | unsigned HOST_WIDE_INT cnt; | |
4582 | tree cfield, cval; | |
5de9d3ed | 4583 | offset_int low_bound; |
4584 | offset_int elt_size; | |
4585 | offset_int index, max_index; | |
4586 | offset_int access_index; | |
96c3acd0 | 4587 | tree domain_type = NULL_TREE, index_type = NULL_TREE; |
1d0b727d | 4588 | HOST_WIDE_INT inner_offset; |
4589 | ||
4590 | /* Compute low bound and elt size. */ | |
415b0903 | 4591 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE) |
4592 | domain_type = TYPE_DOMAIN (TREE_TYPE (ctor)); | |
1d0b727d | 4593 | if (domain_type && TYPE_MIN_VALUE (domain_type)) |
4594 | { | |
4595 | /* Static constructors for variably sized objects makes no sense. */ | |
4596 | gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST); | |
96c3acd0 | 4597 | index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type)); |
5de9d3ed | 4598 | low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type)); |
1d0b727d | 4599 | } |
4600 | else | |
e913b5cd | 4601 | low_bound = 0; |
1d0b727d | 4602 | /* Static constructors for variably sized objects makes no sense. */ |
9af5ce0c | 4603 | gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) |
1d0b727d | 4604 | == INTEGER_CST); |
5de9d3ed | 4605 | elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))); |
1d0b727d | 4606 | |
4607 | /* We can handle only constantly sized accesses that are known to not | |
4608 | be larger than size of array element. */ | |
4609 | if (!TYPE_SIZE_UNIT (type) | |
4610 | || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST | |
fe34354e | 4611 | || wi::lts_p (elt_size, wi::to_offset (TYPE_SIZE_UNIT (type))) |
4612 | || elt_size == 0) | |
1d0b727d | 4613 | return NULL_TREE; |
4614 | ||
4615 | /* Compute the array index we look for. */ | |
5de9d3ed | 4616 | access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT), |
796b6678 | 4617 | elt_size); |
cf8f0e63 | 4618 | access_index += low_bound; |
96c3acd0 | 4619 | if (index_type) |
796b6678 | 4620 | access_index = wi::ext (access_index, TYPE_PRECISION (index_type), |
4621 | TYPE_SIGN (index_type)); | |
1d0b727d | 4622 | |
4623 | /* And offset within the access. */ | |
cf8f0e63 | 4624 | inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT); |
1d0b727d | 4625 | |
4626 | /* See if the array field is large enough to span whole access. We do not | |
4627 | care to fold accesses spanning multiple array indexes. */ | |
cf8f0e63 | 4628 | if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT) |
1d0b727d | 4629 | return NULL_TREE; |
4630 | ||
e913b5cd | 4631 | index = low_bound - 1; |
96c3acd0 | 4632 | if (index_type) |
796b6678 | 4633 | index = wi::ext (index, TYPE_PRECISION (index_type), |
4634 | TYPE_SIGN (index_type)); | |
96c3acd0 | 4635 | |
1d0b727d | 4636 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
4637 | { | |
4638 | /* Array constructor might explicitely set index, or specify range | |
4639 | or leave index NULL meaning that it is next index after previous | |
4640 | one. */ | |
4641 | if (cfield) | |
4642 | { | |
4643 | if (TREE_CODE (cfield) == INTEGER_CST) | |
5de9d3ed | 4644 | max_index = index = wi::to_offset (cfield); |
1d0b727d | 4645 | else |
4646 | { | |
4647 | gcc_assert (TREE_CODE (cfield) == RANGE_EXPR); | |
5de9d3ed | 4648 | index = wi::to_offset (TREE_OPERAND (cfield, 0)); |
4649 | max_index = wi::to_offset (TREE_OPERAND (cfield, 1)); | |
1d0b727d | 4650 | } |
4651 | } | |
4652 | else | |
96c3acd0 | 4653 | { |
e913b5cd | 4654 | index += 1; |
96c3acd0 | 4655 | if (index_type) |
796b6678 | 4656 | index = wi::ext (index, TYPE_PRECISION (index_type), |
4657 | TYPE_SIGN (index_type)); | |
96c3acd0 | 4658 | max_index = index; |
4659 | } | |
1d0b727d | 4660 | |
4661 | /* Do we have match? */ | |
796b6678 | 4662 | if (wi::cmpu (access_index, index) >= 0 |
4663 | && wi::cmpu (access_index, max_index) <= 0) | |
a65b88bf | 4664 | return fold_ctor_reference (type, cval, inner_offset, size, |
4665 | from_decl); | |
1d0b727d | 4666 | } |
4667 | /* When memory is not explicitely mentioned in constructor, | |
4668 | it is 0 (or out of range). */ | |
4669 | return build_zero_cst (type); | |
4670 | } | |
4671 | ||
4672 | /* CTOR is CONSTRUCTOR of an aggregate or vector. | |
4673 | Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */ | |
4674 | ||
4675 | static tree | |
4676 | fold_nonarray_ctor_reference (tree type, tree ctor, | |
4677 | unsigned HOST_WIDE_INT offset, | |
a65b88bf | 4678 | unsigned HOST_WIDE_INT size, |
4679 | tree from_decl) | |
1d0b727d | 4680 | { |
4681 | unsigned HOST_WIDE_INT cnt; | |
4682 | tree cfield, cval; | |
4683 | ||
4684 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, | |
4685 | cval) | |
4686 | { | |
4687 | tree byte_offset = DECL_FIELD_OFFSET (cfield); | |
4688 | tree field_offset = DECL_FIELD_BIT_OFFSET (cfield); | |
4689 | tree field_size = DECL_SIZE (cfield); | |
5de9d3ed | 4690 | offset_int bitoffset; |
5de9d3ed | 4691 | offset_int bitoffset_end, access_end; |
1d0b727d | 4692 | |
4693 | /* Variable sized objects in static constructors makes no sense, | |
4694 | but field_size can be NULL for flexible array members. */ | |
4695 | gcc_assert (TREE_CODE (field_offset) == INTEGER_CST | |
4696 | && TREE_CODE (byte_offset) == INTEGER_CST | |
4697 | && (field_size != NULL_TREE | |
4698 | ? TREE_CODE (field_size) == INTEGER_CST | |
4699 | : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE)); | |
4700 | ||
4701 | /* Compute bit offset of the field. */ | |
5de9d3ed | 4702 | bitoffset = (wi::to_offset (field_offset) |
2315e038 | 4703 | + wi::lshift (wi::to_offset (byte_offset), |
4704 | LOG2_BITS_PER_UNIT)); | |
1d0b727d | 4705 | /* Compute bit offset where the field ends. */ |
4706 | if (field_size != NULL_TREE) | |
5de9d3ed | 4707 | bitoffset_end = bitoffset + wi::to_offset (field_size); |
1d0b727d | 4708 | else |
e913b5cd | 4709 | bitoffset_end = 0; |
1d0b727d | 4710 | |
5de9d3ed | 4711 | access_end = offset_int (offset) + size; |
40a19864 | 4712 | |
4713 | /* Is there any overlap between [OFFSET, OFFSET+SIZE) and | |
4714 | [BITOFFSET, BITOFFSET_END)? */ | |
796b6678 | 4715 | if (wi::cmps (access_end, bitoffset) > 0 |
1d0b727d | 4716 | && (field_size == NULL_TREE |
796b6678 | 4717 | || wi::lts_p (offset, bitoffset_end))) |
1d0b727d | 4718 | { |
5de9d3ed | 4719 | offset_int inner_offset = offset_int (offset) - bitoffset; |
1d0b727d | 4720 | /* We do have overlap. Now see if field is large enough to |
4721 | cover the access. Give up for accesses spanning multiple | |
4722 | fields. */ | |
796b6678 | 4723 | if (wi::cmps (access_end, bitoffset_end) > 0) |
1d0b727d | 4724 | return NULL_TREE; |
796b6678 | 4725 | if (wi::lts_p (offset, bitoffset)) |
40a19864 | 4726 | return NULL_TREE; |
1d0b727d | 4727 | return fold_ctor_reference (type, cval, |
cf8f0e63 | 4728 | inner_offset.to_uhwi (), size, |
a65b88bf | 4729 | from_decl); |
1d0b727d | 4730 | } |
4731 | } | |
4732 | /* When memory is not explicitely mentioned in constructor, it is 0. */ | |
4733 | return build_zero_cst (type); | |
4734 | } | |
4735 | ||
4736 | /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE | |
4737 | to the memory at bit OFFSET. */ | |
4738 | ||
4739 | static tree | |
4740 | fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset, | |
a65b88bf | 4741 | unsigned HOST_WIDE_INT size, tree from_decl) |
1d0b727d | 4742 | { |
4743 | tree ret; | |
4744 | ||
4745 | /* We found the field with exact match. */ | |
4746 | if (useless_type_conversion_p (type, TREE_TYPE (ctor)) | |
4747 | && !offset) | |
8f266cd9 | 4748 | return canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
1d0b727d | 4749 | |
4750 | /* We are at the end of walk, see if we can view convert the | |
4751 | result. */ | |
4752 | if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset | |
4753 | /* VIEW_CONVERT_EXPR is defined only for matching sizes. */ | |
2eac3ab5 | 4754 | && !compare_tree_int (TYPE_SIZE (type), size) |
4755 | && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size)) | |
1d0b727d | 4756 | { |
8f266cd9 | 4757 | ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
1d0b727d | 4758 | ret = fold_unary (VIEW_CONVERT_EXPR, type, ret); |
4759 | if (ret) | |
4760 | STRIP_NOPS (ret); | |
4761 | return ret; | |
4762 | } | |
4237b215 | 4763 | /* For constants and byte-aligned/sized reads try to go through |
4764 | native_encode/interpret. */ | |
4765 | if (CONSTANT_CLASS_P (ctor) | |
4766 | && BITS_PER_UNIT == 8 | |
4767 | && offset % BITS_PER_UNIT == 0 | |
4768 | && size % BITS_PER_UNIT == 0 | |
4769 | && size <= MAX_BITSIZE_MODE_ANY_MODE) | |
4770 | { | |
4771 | unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; | |
4772 | if (native_encode_expr (ctor, buf, size / BITS_PER_UNIT, | |
4773 | offset / BITS_PER_UNIT) > 0) | |
4774 | return native_interpret_expr (type, buf, size / BITS_PER_UNIT); | |
4775 | } | |
1d0b727d | 4776 | if (TREE_CODE (ctor) == CONSTRUCTOR) |
4777 | { | |
4778 | ||
415b0903 | 4779 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE |
4780 | || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE) | |
a65b88bf | 4781 | return fold_array_ctor_reference (type, ctor, offset, size, |
4782 | from_decl); | |
1d0b727d | 4783 | else |
a65b88bf | 4784 | return fold_nonarray_ctor_reference (type, ctor, offset, size, |
4785 | from_decl); | |
1d0b727d | 4786 | } |
4787 | ||
4788 | return NULL_TREE; | |
4789 | } | |
4790 | ||
4791 | /* Return the tree representing the element referenced by T if T is an | |
4792 | ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA | |
4793 | names using VALUEIZE. Return NULL_TREE otherwise. */ | |
4794 | ||
4795 | tree | |
4796 | fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree)) | |
4797 | { | |
4798 | tree ctor, idx, base; | |
4799 | HOST_WIDE_INT offset, size, max_size; | |
4800 | tree tem; | |
4801 | ||
62df0ff9 | 4802 | if (TREE_THIS_VOLATILE (t)) |
4803 | return NULL_TREE; | |
4804 | ||
1d0b727d | 4805 | if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_declaration) |
4806 | return get_symbol_constant_value (t); | |
4807 | ||
4808 | tem = fold_read_from_constant_string (t); | |
4809 | if (tem) | |
4810 | return tem; | |
4811 | ||
4812 | switch (TREE_CODE (t)) | |
4813 | { | |
4814 | case ARRAY_REF: | |
4815 | case ARRAY_RANGE_REF: | |
4816 | /* Constant indexes are handled well by get_base_constructor. | |
4817 | Only special case variable offsets. | |
4818 | FIXME: This code can't handle nested references with variable indexes | |
4819 | (they will be handled only by iteration of ccp). Perhaps we can bring | |
4820 | get_ref_base_and_extent here and make it use a valueize callback. */ | |
4821 | if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME | |
4822 | && valueize | |
4823 | && (idx = (*valueize) (TREE_OPERAND (t, 1))) | |
96c3acd0 | 4824 | && TREE_CODE (idx) == INTEGER_CST) |
1d0b727d | 4825 | { |
936a705d | 4826 | tree low_bound, unit_size; |
1d0b727d | 4827 | |
4828 | /* If the resulting bit-offset is constant, track it. */ | |
936a705d | 4829 | if ((low_bound = array_ref_low_bound (t), |
4830 | TREE_CODE (low_bound) == INTEGER_CST) | |
4831 | && (unit_size = array_ref_element_size (t), | |
4832 | tree_fits_uhwi_p (unit_size))) | |
1d0b727d | 4833 | { |
5de9d3ed | 4834 | offset_int woffset |
4835 | = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound), | |
796b6678 | 4836 | TYPE_PRECISION (TREE_TYPE (idx))); |
ddb1be65 | 4837 | |
796b6678 | 4838 | if (wi::fits_shwi_p (woffset)) |
e913b5cd | 4839 | { |
4840 | offset = woffset.to_shwi (); | |
4841 | /* TODO: This code seems wrong, multiply then check | |
4842 | to see if it fits. */ | |
fe5ad926 | 4843 | offset *= tree_to_uhwi (unit_size); |
e913b5cd | 4844 | offset *= BITS_PER_UNIT; |
ddb1be65 | 4845 | |
e913b5cd | 4846 | base = TREE_OPERAND (t, 0); |
4847 | ctor = get_base_constructor (base, &offset, valueize); | |
4848 | /* Empty constructor. Always fold to 0. */ | |
4849 | if (ctor == error_mark_node) | |
4850 | return build_zero_cst (TREE_TYPE (t)); | |
4851 | /* Out of bound array access. Value is undefined, | |
4852 | but don't fold. */ | |
4853 | if (offset < 0) | |
4854 | return NULL_TREE; | |
4855 | /* We can not determine ctor. */ | |
4856 | if (!ctor) | |
4857 | return NULL_TREE; | |
4858 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, | |
4859 | tree_to_uhwi (unit_size) | |
4860 | * BITS_PER_UNIT, | |
4861 | base); | |
4862 | } | |
1d0b727d | 4863 | } |
4864 | } | |
4865 | /* Fallthru. */ | |
4866 | ||
4867 | case COMPONENT_REF: | |
4868 | case BIT_FIELD_REF: | |
4869 | case TARGET_MEM_REF: | |
4870 | case MEM_REF: | |
4871 | base = get_ref_base_and_extent (t, &offset, &size, &max_size); | |
4872 | ctor = get_base_constructor (base, &offset, valueize); | |
4873 | ||
4874 | /* Empty constructor. Always fold to 0. */ | |
4875 | if (ctor == error_mark_node) | |
4876 | return build_zero_cst (TREE_TYPE (t)); | |
4877 | /* We do not know precise address. */ | |
4878 | if (max_size == -1 || max_size != size) | |
4879 | return NULL_TREE; | |
4880 | /* We can not determine ctor. */ | |
4881 | if (!ctor) | |
4882 | return NULL_TREE; | |
4883 | ||
4884 | /* Out of bound array access. Value is undefined, but don't fold. */ | |
4885 | if (offset < 0) | |
4886 | return NULL_TREE; | |
4887 | ||
a65b88bf | 4888 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, |
4889 | base); | |
1d0b727d | 4890 | |
4891 | case REALPART_EXPR: | |
4892 | case IMAGPART_EXPR: | |
4893 | { | |
4894 | tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize); | |
4895 | if (c && TREE_CODE (c) == COMPLEX_CST) | |
4896 | return fold_build1_loc (EXPR_LOCATION (t), | |
4897 | TREE_CODE (t), TREE_TYPE (t), c); | |
4898 | break; | |
4899 | } | |
4900 | ||
4901 | default: | |
4902 | break; | |
4903 | } | |
4904 | ||
4905 | return NULL_TREE; | |
4906 | } | |
4907 | ||
4908 | tree | |
4909 | fold_const_aggregate_ref (tree t) | |
4910 | { | |
4911 | return fold_const_aggregate_ref_1 (t, NULL); | |
4912 | } | |
0b7ad900 | 4913 | |
02636da3 | 4914 | /* Lookup virtual method with index TOKEN in a virtual table V |
857c5a0b | 4915 | at OFFSET. |
4916 | Set CAN_REFER if non-NULL to false if method | |
4917 | is not referable or if the virtual table is ill-formed (such as rewriten | |
4918 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
d4e80e2b | 4919 | |
4920 | tree | |
02636da3 | 4921 | gimple_get_virt_method_for_vtable (HOST_WIDE_INT token, |
4922 | tree v, | |
857c5a0b | 4923 | unsigned HOST_WIDE_INT offset, |
4924 | bool *can_refer) | |
d4e80e2b | 4925 | { |
02636da3 | 4926 | tree vtable = v, init, fn; |
4927 | unsigned HOST_WIDE_INT size; | |
57b16855 | 4928 | unsigned HOST_WIDE_INT elt_size, access_index; |
4929 | tree domain_type; | |
d4e80e2b | 4930 | |
857c5a0b | 4931 | if (can_refer) |
4932 | *can_refer = true; | |
4933 | ||
6750de5f | 4934 | /* First of all double check we have virtual table. */ |
d4e80e2b | 4935 | if (TREE_CODE (v) != VAR_DECL |
9b3e908f | 4936 | || !DECL_VIRTUAL_P (v)) |
857c5a0b | 4937 | { |
4938 | gcc_assert (in_lto_p); | |
4939 | /* Pass down that we lost track of the target. */ | |
4940 | if (can_refer) | |
4941 | *can_refer = false; | |
4942 | return NULL_TREE; | |
4943 | } | |
6750de5f | 4944 | |
9b3e908f | 4945 | init = ctor_for_folding (v); |
4946 | ||
6750de5f | 4947 | /* The virtual tables should always be born with constructors |
9b3e908f | 4948 | and we always should assume that they are avaialble for |
4949 | folding. At the moment we do not stream them in all cases, | |
4950 | but it should never happen that ctor seem unreachable. */ | |
4951 | gcc_assert (init); | |
4952 | if (init == error_mark_node) | |
4953 | { | |
4954 | gcc_assert (in_lto_p); | |
857c5a0b | 4955 | /* Pass down that we lost track of the target. */ |
4956 | if (can_refer) | |
4957 | *can_refer = false; | |
9b3e908f | 4958 | return NULL_TREE; |
4959 | } | |
d4e80e2b | 4960 | gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE); |
e913b5cd | 4961 | size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v)))); |
02636da3 | 4962 | offset *= BITS_PER_UNIT; |
d4e80e2b | 4963 | offset += token * size; |
6750de5f | 4964 | |
57b16855 | 4965 | /* Lookup the value in the constructor that is assumed to be array. |
4966 | This is equivalent to | |
4967 | fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init, | |
4968 | offset, size, NULL); | |
4969 | but in a constant time. We expect that frontend produced a simple | |
4970 | array without indexed initializers. */ | |
4971 | ||
4972 | gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE); | |
4973 | domain_type = TYPE_DOMAIN (TREE_TYPE (init)); | |
4974 | gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type))); | |
4975 | elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init)))); | |
4976 | ||
4977 | access_index = offset / BITS_PER_UNIT / elt_size; | |
4978 | gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0); | |
4979 | ||
4980 | /* This code makes an assumption that there are no | |
4981 | indexed fileds produced by C++ FE, so we can directly index the array. */ | |
4982 | if (access_index < CONSTRUCTOR_NELTS (init)) | |
4983 | { | |
4984 | fn = CONSTRUCTOR_ELT (init, access_index)->value; | |
4985 | gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index); | |
4986 | STRIP_NOPS (fn); | |
4987 | } | |
4988 | else | |
4989 | fn = NULL; | |
6750de5f | 4990 | |
4991 | /* For type inconsistent program we may end up looking up virtual method | |
4992 | in virtual table that does not contain TOKEN entries. We may overrun | |
4993 | the virtual table and pick up a constant or RTTI info pointer. | |
4994 | In any case the call is undefined. */ | |
4995 | if (!fn | |
4996 | || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR) | |
4997 | || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL) | |
4998 | fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
4999 | else | |
5000 | { | |
5001 | fn = TREE_OPERAND (fn, 0); | |
5002 | ||
5003 | /* When cgraph node is missing and function is not public, we cannot | |
5004 | devirtualize. This can happen in WHOPR when the actual method | |
5005 | ends up in other partition, because we found devirtualization | |
5006 | possibility too late. */ | |
5007 | if (!can_refer_decl_in_current_unit_p (fn, vtable)) | |
857c5a0b | 5008 | { |
5009 | if (can_refer) | |
5010 | { | |
5011 | *can_refer = false; | |
5012 | return fn; | |
5013 | } | |
5014 | return NULL_TREE; | |
5015 | } | |
6750de5f | 5016 | } |
d4e80e2b | 5017 | |
e25d4891 | 5018 | /* Make sure we create a cgraph node for functions we'll reference. |
5019 | They can be non-existent if the reference comes from an entry | |
5020 | of an external vtable for example. */ | |
415d1b9a | 5021 | cgraph_node::get_create (fn); |
e25d4891 | 5022 | |
d4e80e2b | 5023 | return fn; |
5024 | } | |
5025 | ||
02636da3 | 5026 | /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN |
5027 | is integer form of OBJ_TYPE_REF_TOKEN of the reference expression. | |
5028 | KNOWN_BINFO carries the binfo describing the true type of | |
857c5a0b | 5029 | OBJ_TYPE_REF_OBJECT(REF). |
5030 | Set CAN_REFER if non-NULL to false if method | |
5031 | is not referable or if the virtual table is ill-formed (such as rewriten | |
5032 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
02636da3 | 5033 | |
5034 | tree | |
857c5a0b | 5035 | gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo, |
5036 | bool *can_refer) | |
02636da3 | 5037 | { |
5038 | unsigned HOST_WIDE_INT offset; | |
5039 | tree v; | |
5040 | ||
5041 | v = BINFO_VTABLE (known_binfo); | |
5042 | /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */ | |
5043 | if (!v) | |
5044 | return NULL_TREE; | |
5045 | ||
5046 | if (!vtable_pointer_value_to_vtable (v, &v, &offset)) | |
857c5a0b | 5047 | { |
5048 | if (can_refer) | |
5049 | *can_refer = false; | |
5050 | return NULL_TREE; | |
5051 | } | |
5052 | return gimple_get_virt_method_for_vtable (token, v, offset, can_refer); | |
02636da3 | 5053 | } |
5054 | ||
0b7ad900 | 5055 | /* Return true iff VAL is a gimple expression that is known to be |
5056 | non-negative. Restricted to floating-point inputs. */ | |
5057 | ||
5058 | bool | |
5059 | gimple_val_nonnegative_real_p (tree val) | |
5060 | { | |
5061 | gimple def_stmt; | |
5062 | ||
5063 | gcc_assert (val && SCALAR_FLOAT_TYPE_P (TREE_TYPE (val))); | |
5064 | ||
5065 | /* Use existing logic for non-gimple trees. */ | |
5066 | if (tree_expr_nonnegative_p (val)) | |
5067 | return true; | |
5068 | ||
5069 | if (TREE_CODE (val) != SSA_NAME) | |
5070 | return false; | |
5071 | ||
5072 | /* Currently we look only at the immediately defining statement | |
5073 | to make this determination, since recursion on defining | |
5074 | statements of operands can lead to quadratic behavior in the | |
5075 | worst case. This is expected to catch almost all occurrences | |
5076 | in practice. It would be possible to implement limited-depth | |
5077 | recursion if important cases are lost. Alternatively, passes | |
5078 | that need this information (such as the pow/powi lowering code | |
5079 | in the cse_sincos pass) could be revised to provide it through | |
5080 | dataflow propagation. */ | |
5081 | ||
5082 | def_stmt = SSA_NAME_DEF_STMT (val); | |
5083 | ||
5084 | if (is_gimple_assign (def_stmt)) | |
5085 | { | |
5086 | tree op0, op1; | |
5087 | ||
5088 | /* See fold-const.c:tree_expr_nonnegative_p for additional | |
5089 | cases that could be handled with recursion. */ | |
5090 | ||
5091 | switch (gimple_assign_rhs_code (def_stmt)) | |
5092 | { | |
5093 | case ABS_EXPR: | |
5094 | /* Always true for floating-point operands. */ | |
5095 | return true; | |
5096 | ||
5097 | case MULT_EXPR: | |
5098 | /* True if the two operands are identical (since we are | |
5099 | restricted to floating-point inputs). */ | |
5100 | op0 = gimple_assign_rhs1 (def_stmt); | |
5101 | op1 = gimple_assign_rhs2 (def_stmt); | |
5102 | ||
5103 | if (op0 == op1 | |
5104 | || operand_equal_p (op0, op1, 0)) | |
5105 | return true; | |
5106 | ||
5107 | default: | |
5108 | return false; | |
5109 | } | |
5110 | } | |
5111 | else if (is_gimple_call (def_stmt)) | |
5112 | { | |
5113 | tree fndecl = gimple_call_fndecl (def_stmt); | |
5114 | if (fndecl | |
5115 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
5116 | { | |
5117 | tree arg1; | |
5118 | ||
5119 | switch (DECL_FUNCTION_CODE (fndecl)) | |
5120 | { | |
5121 | CASE_FLT_FN (BUILT_IN_ACOS): | |
5122 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
5123 | CASE_FLT_FN (BUILT_IN_CABS): | |
5124 | CASE_FLT_FN (BUILT_IN_COSH): | |
5125 | CASE_FLT_FN (BUILT_IN_ERFC): | |
5126 | CASE_FLT_FN (BUILT_IN_EXP): | |
5127 | CASE_FLT_FN (BUILT_IN_EXP10): | |
5128 | CASE_FLT_FN (BUILT_IN_EXP2): | |
5129 | CASE_FLT_FN (BUILT_IN_FABS): | |
5130 | CASE_FLT_FN (BUILT_IN_FDIM): | |
5131 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
5132 | CASE_FLT_FN (BUILT_IN_POW10): | |
5133 | return true; | |
5134 | ||
5135 | CASE_FLT_FN (BUILT_IN_SQRT): | |
5136 | /* sqrt(-0.0) is -0.0, and sqrt is not defined over other | |
5137 | nonnegative inputs. */ | |
5138 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (val)))) | |
5139 | return true; | |
5140 | ||
5141 | break; | |
5142 | ||
5143 | CASE_FLT_FN (BUILT_IN_POWI): | |
5144 | /* True if the second argument is an even integer. */ | |
5145 | arg1 = gimple_call_arg (def_stmt, 1); | |
5146 | ||
5147 | if (TREE_CODE (arg1) == INTEGER_CST | |
f9ae6f95 | 5148 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) |
0b7ad900 | 5149 | return true; |
5150 | ||
5151 | break; | |
5152 | ||
5153 | CASE_FLT_FN (BUILT_IN_POW): | |
5154 | /* True if the second argument is an even integer-valued | |
5155 | real. */ | |
5156 | arg1 = gimple_call_arg (def_stmt, 1); | |
5157 | ||
5158 | if (TREE_CODE (arg1) == REAL_CST) | |
5159 | { | |
5160 | REAL_VALUE_TYPE c; | |
5161 | HOST_WIDE_INT n; | |
5162 | ||
5163 | c = TREE_REAL_CST (arg1); | |
5164 | n = real_to_integer (&c); | |
5165 | ||
5166 | if ((n & 1) == 0) | |
5167 | { | |
5168 | REAL_VALUE_TYPE cint; | |
e913b5cd | 5169 | real_from_integer (&cint, VOIDmode, n, SIGNED); |
0b7ad900 | 5170 | if (real_identical (&c, &cint)) |
5171 | return true; | |
5172 | } | |
5173 | } | |
5174 | ||
5175 | break; | |
5176 | ||
5177 | default: | |
5178 | return false; | |
5179 | } | |
5180 | } | |
5181 | } | |
5182 | ||
5183 | return false; | |
5184 | } | |
09c41ee4 | 5185 | |
5186 | /* Given a pointer value OP0, return a simplified version of an | |
5187 | indirection through OP0, or NULL_TREE if no simplification is | |
5188 | possible. Note that the resulting type may be different from | |
5189 | the type pointed to in the sense that it is still compatible | |
5190 | from the langhooks point of view. */ | |
5191 | ||
5192 | tree | |
5193 | gimple_fold_indirect_ref (tree t) | |
5194 | { | |
5195 | tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype); | |
5196 | tree sub = t; | |
5197 | tree subtype; | |
5198 | ||
5199 | STRIP_NOPS (sub); | |
5200 | subtype = TREE_TYPE (sub); | |
5201 | if (!POINTER_TYPE_P (subtype)) | |
5202 | return NULL_TREE; | |
5203 | ||
5204 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5205 | { | |
5206 | tree op = TREE_OPERAND (sub, 0); | |
5207 | tree optype = TREE_TYPE (op); | |
5208 | /* *&p => p */ | |
5209 | if (useless_type_conversion_p (type, optype)) | |
5210 | return op; | |
5211 | ||
5212 | /* *(foo *)&fooarray => fooarray[0] */ | |
5213 | if (TREE_CODE (optype) == ARRAY_TYPE | |
5214 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST | |
5215 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
5216 | { | |
5217 | tree type_domain = TYPE_DOMAIN (optype); | |
5218 | tree min_val = size_zero_node; | |
5219 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5220 | min_val = TYPE_MIN_VALUE (type_domain); | |
5221 | if (TREE_CODE (min_val) == INTEGER_CST) | |
5222 | return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE); | |
5223 | } | |
5224 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
5225 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
5226 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
5227 | return fold_build1 (REALPART_EXPR, type, op); | |
5228 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
5229 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
5230 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
5231 | { | |
5232 | tree part_width = TYPE_SIZE (type); | |
5233 | tree index = bitsize_int (0); | |
5234 | return fold_build3 (BIT_FIELD_REF, type, op, part_width, index); | |
5235 | } | |
5236 | } | |
5237 | ||
5238 | /* *(p + CST) -> ... */ | |
5239 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
5240 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
5241 | { | |
5242 | tree addr = TREE_OPERAND (sub, 0); | |
5243 | tree off = TREE_OPERAND (sub, 1); | |
5244 | tree addrtype; | |
5245 | ||
5246 | STRIP_NOPS (addr); | |
5247 | addrtype = TREE_TYPE (addr); | |
5248 | ||
5249 | /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */ | |
5250 | if (TREE_CODE (addr) == ADDR_EXPR | |
5251 | && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE | |
5252 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))) | |
6b409616 | 5253 | && tree_fits_uhwi_p (off)) |
09c41ee4 | 5254 | { |
6b409616 | 5255 | unsigned HOST_WIDE_INT offset = tree_to_uhwi (off); |
09c41ee4 | 5256 | tree part_width = TYPE_SIZE (type); |
5257 | unsigned HOST_WIDE_INT part_widthi | |
6b409616 | 5258 | = tree_to_shwi (part_width) / BITS_PER_UNIT; |
09c41ee4 | 5259 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; |
5260 | tree index = bitsize_int (indexi); | |
5261 | if (offset / part_widthi | |
2dda221d | 5262 | < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))) |
09c41ee4 | 5263 | return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0), |
5264 | part_width, index); | |
5265 | } | |
5266 | ||
5267 | /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */ | |
5268 | if (TREE_CODE (addr) == ADDR_EXPR | |
5269 | && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE | |
5270 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))) | |
5271 | { | |
5272 | tree size = TYPE_SIZE_UNIT (type); | |
5273 | if (tree_int_cst_equal (size, off)) | |
5274 | return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0)); | |
5275 | } | |
5276 | ||
5277 | /* *(p + CST) -> MEM_REF <p, CST>. */ | |
5278 | if (TREE_CODE (addr) != ADDR_EXPR | |
5279 | || DECL_P (TREE_OPERAND (addr, 0))) | |
5280 | return fold_build2 (MEM_REF, type, | |
5281 | addr, | |
6b409616 | 5282 | wide_int_to_tree (ptype, off)); |
09c41ee4 | 5283 | } |
5284 | ||
5285 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
5286 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
5287 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST | |
5288 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype)))) | |
5289 | { | |
5290 | tree type_domain; | |
5291 | tree min_val = size_zero_node; | |
5292 | tree osub = sub; | |
5293 | sub = gimple_fold_indirect_ref (sub); | |
5294 | if (! sub) | |
5295 | sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub); | |
5296 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5297 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5298 | min_val = TYPE_MIN_VALUE (type_domain); | |
5299 | if (TREE_CODE (min_val) == INTEGER_CST) | |
5300 | return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE); | |
5301 | } | |
5302 | ||
5303 | return NULL_TREE; | |
5304 | } | |
f6a34e3f | 5305 | |
5306 | /* Return true if CODE is an operation that when operating on signed | |
5307 | integer types involves undefined behavior on overflow and the | |
5308 | operation can be expressed with unsigned arithmetic. */ | |
5309 | ||
5310 | bool | |
5311 | arith_code_with_undefined_signed_overflow (tree_code code) | |
5312 | { | |
5313 | switch (code) | |
5314 | { | |
5315 | case PLUS_EXPR: | |
5316 | case MINUS_EXPR: | |
5317 | case MULT_EXPR: | |
5318 | case NEGATE_EXPR: | |
5319 | case POINTER_PLUS_EXPR: | |
5320 | return true; | |
5321 | default: | |
5322 | return false; | |
5323 | } | |
5324 | } | |
5325 | ||
5326 | /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic | |
5327 | operation that can be transformed to unsigned arithmetic by converting | |
5328 | its operand, carrying out the operation in the corresponding unsigned | |
5329 | type and converting the result back to the original type. | |
5330 | ||
5331 | Returns a sequence of statements that replace STMT and also contain | |
5332 | a modified form of STMT itself. */ | |
5333 | ||
5334 | gimple_seq | |
5335 | rewrite_to_defined_overflow (gimple stmt) | |
5336 | { | |
5337 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
5338 | { | |
5339 | fprintf (dump_file, "rewriting stmt with undefined signed " | |
5340 | "overflow "); | |
5341 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
5342 | } | |
5343 | ||
5344 | tree lhs = gimple_assign_lhs (stmt); | |
5345 | tree type = unsigned_type_for (TREE_TYPE (lhs)); | |
5346 | gimple_seq stmts = NULL; | |
5347 | for (unsigned i = 1; i < gimple_num_ops (stmt); ++i) | |
5348 | { | |
5349 | gimple_seq stmts2 = NULL; | |
5350 | gimple_set_op (stmt, i, | |
5351 | force_gimple_operand (fold_convert (type, | |
5352 | gimple_op (stmt, i)), | |
5353 | &stmts2, true, NULL_TREE)); | |
5354 | gimple_seq_add_seq (&stmts, stmts2); | |
5355 | } | |
5356 | gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt)); | |
5357 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) | |
5358 | gimple_assign_set_rhs_code (stmt, PLUS_EXPR); | |
5359 | gimple_seq_add_stmt (&stmts, stmt); | |
5360 | gimple cvt = gimple_build_assign_with_ops | |
5361 | (NOP_EXPR, lhs, gimple_assign_lhs (stmt), NULL_TREE); | |
5362 | gimple_seq_add_stmt (&stmts, cvt); | |
5363 | ||
5364 | return stmts; | |
5365 | } | |
65f73697 | 5366 | |
5367 | /* Return OP converted to TYPE by emitting a conversion statement on SEQ | |
5368 | if required using location LOC. Note that OP will be returned | |
5369 | unmodified if GIMPLE does not require an explicit conversion between | |
5370 | its type and TYPE. */ | |
5371 | ||
5372 | tree | |
5373 | gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op) | |
5374 | { | |
5375 | if (useless_type_conversion_p (type, TREE_TYPE (op))) | |
5376 | return op; | |
5377 | op = fold_convert_loc (loc, type, op); | |
5378 | gimple_seq stmts = NULL; | |
5379 | op = force_gimple_operand (op, &stmts, true, NULL_TREE); | |
5380 | gimple_seq_add_seq_without_update (seq, stmts); | |
5381 | return op; | |
5382 | } |