]>
Commit | Line | Data |
---|---|---|
2d18b16d | 1 | /* Statement simplification on GIMPLE. |
aad93da1 | 2 | Copyright (C) 2010-2017 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" | |
9ef16211 | 24 | #include "backend.h" |
7c29e30e | 25 | #include "target.h" |
26 | #include "rtl.h" | |
2d18b16d | 27 | #include "tree.h" |
9ef16211 | 28 | #include "gimple.h" |
7c29e30e | 29 | #include "predict.h" |
9ef16211 | 30 | #include "ssa.h" |
7c29e30e | 31 | #include "cgraph.h" |
32 | #include "gimple-pretty-print.h" | |
9ef16211 | 33 | #include "fold-const.h" |
d53441c8 | 34 | #include "stmt.h" |
35 | #include "expr.h" | |
36 | #include "stor-layout.h" | |
b9ed1410 | 37 | #include "dumpfile.h" |
bc61cadb | 38 | #include "gimple-fold.h" |
a8783bee | 39 | #include "gimplify.h" |
dcf1a1ec | 40 | #include "gimple-iterator.h" |
073c1fd5 | 41 | #include "tree-into-ssa.h" |
42 | #include "tree-dfa.h" | |
69ee5dbb | 43 | #include "tree-ssa.h" |
2d18b16d | 44 | #include "tree-ssa-propagate.h" |
10fba9c0 | 45 | #include "ipa-utils.h" |
424a4a92 | 46 | #include "tree-ssa-address.h" |
0e80b01d | 47 | #include "langhooks.h" |
f6a34e3f | 48 | #include "gimplify-me.h" |
ceb49bba | 49 | #include "dbgcnt.h" |
f7715905 | 50 | #include "builtins.h" |
55534d34 | 51 | #include "tree-eh.h" |
52 | #include "gimple-match.h" | |
7e3a76de | 53 | #include "gomp-constants.h" |
6b7a6f44 | 54 | #include "optabs-query.h" |
4954efd4 | 55 | #include "omp-general.h" |
57cbac18 | 56 | #include "ipa-chkp.h" |
0a6b484c | 57 | #include "tree-cfg.h" |
ac8a4f8e | 58 | #include "fold-const-call.h" |
30a86690 | 59 | #include "stringpool.h" |
60 | #include "attribs.h" | |
9917317a | 61 | #include "asan.h" |
2d18b16d | 62 | |
551732eb | 63 | /* Return true when DECL can be referenced from current unit. |
a65b88bf | 64 | FROM_DECL (if non-null) specify constructor of variable DECL was taken from. |
65 | We can get declarations that are not possible to reference for various | |
66 | reasons: | |
f1c35659 | 67 | |
f1c35659 | 68 | 1) When analyzing C++ virtual tables. |
69 | C++ virtual tables do have known constructors even | |
70 | when they are keyed to other compilation unit. | |
71 | Those tables can contain pointers to methods and vars | |
72 | in other units. Those methods have both STATIC and EXTERNAL | |
73 | set. | |
74 | 2) In WHOPR mode devirtualization might lead to reference | |
75 | to method that was partitioned elsehwere. | |
76 | In this case we have static VAR_DECL or FUNCTION_DECL | |
77 | that has no corresponding callgraph/varpool node | |
551732eb | 78 | declaring the body. |
79 | 3) COMDAT functions referred by external vtables that | |
125ac0ea | 80 | we devirtualize only during final compilation stage. |
551732eb | 81 | At this time we already decided that we will not output |
82 | the function body and thus we can't reference the symbol | |
83 | directly. */ | |
84 | ||
f1c35659 | 85 | static bool |
a65b88bf | 86 | can_refer_decl_in_current_unit_p (tree decl, tree from_decl) |
f1c35659 | 87 | { |
098f44bc | 88 | varpool_node *vnode; |
f1c35659 | 89 | struct cgraph_node *node; |
452659af | 90 | symtab_node *snode; |
a65b88bf | 91 | |
16d41ae2 | 92 | if (DECL_ABSTRACT_P (decl)) |
2246b6a9 | 93 | return false; |
94 | ||
95 | /* We are concerned only about static/external vars and functions. */ | |
96 | if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) | |
53e9c5c4 | 97 | || !VAR_OR_FUNCTION_DECL_P (decl)) |
2246b6a9 | 98 | return true; |
99 | ||
100 | /* Static objects can be referred only if they was not optimized out yet. */ | |
101 | if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) | |
102 | { | |
59425620 | 103 | /* Before we start optimizing unreachable code we can be sure all |
104 | static objects are defined. */ | |
35ee1c66 | 105 | if (symtab->function_flags_ready) |
59425620 | 106 | return true; |
415d1b9a | 107 | snode = symtab_node::get (decl); |
59425620 | 108 | if (!snode || !snode->definition) |
2246b6a9 | 109 | return false; |
13cbeaac | 110 | node = dyn_cast <cgraph_node *> (snode); |
2246b6a9 | 111 | return !node || !node->global.inlined_to; |
112 | } | |
113 | ||
8f1c7d19 | 114 | /* We will later output the initializer, so we can refer to it. |
a65b88bf | 115 | So we are concerned only when DECL comes from initializer of |
59425620 | 116 | external var or var that has been optimized out. */ |
a65b88bf | 117 | if (!from_decl |
53e9c5c4 | 118 | || !VAR_P (from_decl) |
59425620 | 119 | || (!DECL_EXTERNAL (from_decl) |
97221fd7 | 120 | && (vnode = varpool_node::get (from_decl)) != NULL |
59425620 | 121 | && vnode->definition) |
8f1c7d19 | 122 | || (flag_ltrans |
97221fd7 | 123 | && (vnode = varpool_node::get (from_decl)) != NULL |
fb5bde82 | 124 | && vnode->in_other_partition)) |
a65b88bf | 125 | return true; |
a65b88bf | 126 | /* We are folding reference from external vtable. The vtable may reffer |
127 | to a symbol keyed to other compilation unit. The other compilation | |
128 | unit may be in separate DSO and the symbol may be hidden. */ | |
129 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
130 | && DECL_EXTERNAL (decl) | |
1bf69827 | 131 | && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT |
415d1b9a | 132 | && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition)) |
a65b88bf | 133 | return false; |
551732eb | 134 | /* When function is public, we always can introduce new reference. |
135 | Exception are the COMDAT functions where introducing a direct | |
136 | reference imply need to include function body in the curren tunit. */ | |
137 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl)) | |
138 | return true; | |
59425620 | 139 | /* We have COMDAT. We are going to check if we still have definition |
140 | or if the definition is going to be output in other partition. | |
141 | Bypass this when gimplifying; all needed functions will be produced. | |
a65b88bf | 142 | |
143 | As observed in PR20991 for already optimized out comdat virtual functions | |
9d75589a | 144 | it may be tempting to not necessarily give up because the copy will be |
a65b88bf | 145 | output elsewhere when corresponding vtable is output. |
146 | This is however not possible - ABI specify that COMDATs are output in | |
147 | units where they are used and when the other unit was compiled with LTO | |
148 | it is possible that vtable was kept public while the function itself | |
149 | was privatized. */ | |
35ee1c66 | 150 | if (!symtab->function_flags_ready) |
551732eb | 151 | return true; |
a65b88bf | 152 | |
415d1b9a | 153 | snode = symtab_node::get (decl); |
59425620 | 154 | if (!snode |
155 | || ((!snode->definition || DECL_EXTERNAL (decl)) | |
156 | && (!snode->in_other_partition | |
157 | || (!snode->forced_by_abi && !snode->force_output)))) | |
158 | return false; | |
159 | node = dyn_cast <cgraph_node *> (snode); | |
160 | return !node || !node->global.inlined_to; | |
f1c35659 | 161 | } |
162 | ||
c985cfb4 | 163 | /* Create a temporary for TYPE for a statement STMT. If the current function |
164 | is in SSA form, a SSA name is created. Otherwise a temporary register | |
165 | is made. */ | |
166 | ||
ac0146c1 | 167 | tree |
168 | create_tmp_reg_or_ssa_name (tree type, gimple *stmt) | |
c985cfb4 | 169 | { |
170 | if (gimple_in_ssa_p (cfun)) | |
171 | return make_ssa_name (type, stmt); | |
172 | else | |
173 | return create_tmp_reg (type); | |
174 | } | |
175 | ||
bb903e9c | 176 | /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into |
a65b88bf | 177 | acceptable form for is_gimple_min_invariant. |
178 | FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */ | |
15ba153a | 179 | |
180 | tree | |
a65b88bf | 181 | canonicalize_constructor_val (tree cval, tree from_decl) |
15ba153a | 182 | { |
cf649032 | 183 | tree orig_cval = cval; |
184 | STRIP_NOPS (cval); | |
704d7315 | 185 | if (TREE_CODE (cval) == POINTER_PLUS_EXPR |
186 | && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST) | |
15ba153a | 187 | { |
704d7315 | 188 | tree ptr = TREE_OPERAND (cval, 0); |
189 | if (is_gimple_min_invariant (ptr)) | |
190 | cval = build1_loc (EXPR_LOCATION (cval), | |
191 | ADDR_EXPR, TREE_TYPE (ptr), | |
192 | fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)), | |
193 | ptr, | |
194 | fold_convert (ptr_type_node, | |
195 | TREE_OPERAND (cval, 1)))); | |
15ba153a | 196 | } |
197 | if (TREE_CODE (cval) == ADDR_EXPR) | |
198 | { | |
8edd00b2 | 199 | tree base = NULL_TREE; |
200 | if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR) | |
7843e4bc | 201 | { |
202 | base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0)); | |
203 | if (base) | |
204 | TREE_OPERAND (cval, 0) = base; | |
205 | } | |
8edd00b2 | 206 | else |
207 | base = get_base_address (TREE_OPERAND (cval, 0)); | |
e25d4891 | 208 | if (!base) |
209 | return NULL_TREE; | |
551732eb | 210 | |
53e9c5c4 | 211 | if (VAR_OR_FUNCTION_DECL_P (base) |
a65b88bf | 212 | && !can_refer_decl_in_current_unit_p (base, from_decl)) |
f1c35659 | 213 | return NULL_TREE; |
9bbfd060 | 214 | if (TREE_TYPE (base) == error_mark_node) |
215 | return NULL_TREE; | |
53e9c5c4 | 216 | if (VAR_P (base)) |
b03e5397 | 217 | TREE_ADDRESSABLE (base) = 1; |
e25d4891 | 218 | else if (TREE_CODE (base) == FUNCTION_DECL) |
219 | { | |
220 | /* Make sure we create a cgraph node for functions we'll reference. | |
221 | They can be non-existent if the reference comes from an entry | |
222 | of an external vtable for example. */ | |
415d1b9a | 223 | cgraph_node::get_create (base); |
e25d4891 | 224 | } |
bb903e9c | 225 | /* Fixup types in global initializers. */ |
0554476d | 226 | if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0))) |
227 | cval = build_fold_addr_expr (TREE_OPERAND (cval, 0)); | |
cf649032 | 228 | |
229 | if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval))) | |
230 | cval = fold_convert (TREE_TYPE (orig_cval), cval); | |
231 | return cval; | |
15ba153a | 232 | } |
f5faab84 | 233 | if (TREE_OVERFLOW_P (cval)) |
234 | return drop_tree_overflow (cval); | |
cf649032 | 235 | return orig_cval; |
15ba153a | 236 | } |
2d18b16d | 237 | |
238 | /* If SYM is a constant variable with known value, return the value. | |
239 | NULL_TREE is returned otherwise. */ | |
240 | ||
241 | tree | |
242 | get_symbol_constant_value (tree sym) | |
243 | { | |
df8d3e89 | 244 | tree val = ctor_for_folding (sym); |
245 | if (val != error_mark_node) | |
2d18b16d | 246 | { |
2d18b16d | 247 | if (val) |
248 | { | |
8f266cd9 | 249 | val = canonicalize_constructor_val (unshare_expr (val), sym); |
f1c35659 | 250 | if (val && is_gimple_min_invariant (val)) |
15ba153a | 251 | return val; |
f1c35659 | 252 | else |
253 | return NULL_TREE; | |
2d18b16d | 254 | } |
255 | /* Variables declared 'const' without an initializer | |
256 | have zero as the initializer if they may not be | |
257 | overridden at link or run time. */ | |
258 | if (!val | |
cde9e53d | 259 | && is_gimple_reg_type (TREE_TYPE (sym))) |
385f3f36 | 260 | return build_zero_cst (TREE_TYPE (sym)); |
2d18b16d | 261 | } |
262 | ||
263 | return NULL_TREE; | |
264 | } | |
265 | ||
266 | ||
2d18b16d | 267 | |
268 | /* Subroutine of fold_stmt. We perform several simplifications of the | |
269 | memory reference tree EXPR and make sure to re-gimplify them properly | |
270 | after propagation of constant addresses. IS_LHS is true if the | |
271 | reference is supposed to be an lvalue. */ | |
272 | ||
273 | static tree | |
274 | maybe_fold_reference (tree expr, bool is_lhs) | |
275 | { | |
15ba153a | 276 | tree result; |
2d18b16d | 277 | |
c701e5d5 | 278 | if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR |
279 | || TREE_CODE (expr) == REALPART_EXPR | |
280 | || TREE_CODE (expr) == IMAGPART_EXPR) | |
281 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
282 | return fold_unary_loc (EXPR_LOCATION (expr), | |
283 | TREE_CODE (expr), | |
284 | TREE_TYPE (expr), | |
285 | TREE_OPERAND (expr, 0)); | |
286 | else if (TREE_CODE (expr) == BIT_FIELD_REF | |
287 | && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0))) | |
288 | return fold_ternary_loc (EXPR_LOCATION (expr), | |
289 | TREE_CODE (expr), | |
290 | TREE_TYPE (expr), | |
291 | TREE_OPERAND (expr, 0), | |
292 | TREE_OPERAND (expr, 1), | |
293 | TREE_OPERAND (expr, 2)); | |
294 | ||
c701e5d5 | 295 | if (!is_lhs |
296 | && (result = fold_const_aggregate_ref (expr)) | |
297 | && is_gimple_min_invariant (result)) | |
298 | return result; | |
2d18b16d | 299 | |
2d18b16d | 300 | return NULL_TREE; |
301 | } | |
302 | ||
303 | ||
304 | /* Attempt to fold an assignment statement pointed-to by SI. Returns a | |
305 | replacement rhs for the statement or NULL_TREE if no simplification | |
306 | could be made. It is assumed that the operands have been previously | |
307 | folded. */ | |
308 | ||
309 | static tree | |
310 | fold_gimple_assign (gimple_stmt_iterator *si) | |
311 | { | |
42acab1c | 312 | gimple *stmt = gsi_stmt (*si); |
2d18b16d | 313 | enum tree_code subcode = gimple_assign_rhs_code (stmt); |
314 | location_t loc = gimple_location (stmt); | |
315 | ||
316 | tree result = NULL_TREE; | |
317 | ||
318 | switch (get_gimple_rhs_class (subcode)) | |
319 | { | |
320 | case GIMPLE_SINGLE_RHS: | |
321 | { | |
322 | tree rhs = gimple_assign_rhs1 (stmt); | |
323 | ||
b43b0cfd | 324 | if (TREE_CLOBBER_P (rhs)) |
325 | return NULL_TREE; | |
326 | ||
8a2caf10 | 327 | if (REFERENCE_CLASS_P (rhs)) |
2d18b16d | 328 | return maybe_fold_reference (rhs, false); |
329 | ||
0329fcdb | 330 | else if (TREE_CODE (rhs) == OBJ_TYPE_REF) |
331 | { | |
332 | tree val = OBJ_TYPE_REF_EXPR (rhs); | |
333 | if (is_gimple_min_invariant (val)) | |
334 | return val; | |
722b5983 | 335 | else if (flag_devirtualize && virtual_method_call_p (rhs)) |
0329fcdb | 336 | { |
337 | bool final; | |
338 | vec <cgraph_node *>targets | |
722b5983 | 339 | = possible_polymorphic_call_targets (rhs, stmt, &final); |
ceb49bba | 340 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
0329fcdb | 341 | { |
ceb49bba | 342 | if (dump_enabled_p ()) |
343 | { | |
4c8041d7 | 344 | location_t loc = gimple_location_safe (stmt); |
ceb49bba | 345 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
346 | "resolving virtual function address " | |
347 | "reference to function %s\n", | |
348 | targets.length () == 1 | |
349 | ? targets[0]->name () | |
6f364420 | 350 | : "NULL"); |
ceb49bba | 351 | } |
6f364420 | 352 | if (targets.length () == 1) |
353 | { | |
354 | val = fold_convert (TREE_TYPE (val), | |
355 | build_fold_addr_expr_loc | |
356 | (loc, targets[0]->decl)); | |
357 | STRIP_USELESS_TYPE_CONVERSION (val); | |
358 | } | |
359 | else | |
360 | /* We can not use __builtin_unreachable here because it | |
361 | can not have address taken. */ | |
362 | val = build_int_cst (TREE_TYPE (val), 0); | |
0329fcdb | 363 | return val; |
364 | } | |
365 | } | |
0329fcdb | 366 | } |
e1fff881 | 367 | |
2d18b16d | 368 | else if (TREE_CODE (rhs) == ADDR_EXPR) |
369 | { | |
182cf5a9 | 370 | tree ref = TREE_OPERAND (rhs, 0); |
371 | tree tem = maybe_fold_reference (ref, true); | |
372 | if (tem | |
373 | && TREE_CODE (tem) == MEM_REF | |
374 | && integer_zerop (TREE_OPERAND (tem, 1))) | |
375 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0)); | |
376 | else if (tem) | |
2d18b16d | 377 | result = fold_convert (TREE_TYPE (rhs), |
378 | build_fold_addr_expr_loc (loc, tem)); | |
182cf5a9 | 379 | else if (TREE_CODE (ref) == MEM_REF |
380 | && integer_zerop (TREE_OPERAND (ref, 1))) | |
381 | result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0)); | |
e1fff881 | 382 | |
383 | if (result) | |
384 | { | |
385 | /* Strip away useless type conversions. Both the | |
386 | NON_LVALUE_EXPR that may have been added by fold, and | |
387 | "useless" type conversions that might now be apparent | |
388 | due to propagation. */ | |
389 | STRIP_USELESS_TYPE_CONVERSION (result); | |
390 | ||
391 | if (result != rhs && valid_gimple_rhs_p (result)) | |
392 | return result; | |
393 | } | |
2d18b16d | 394 | } |
395 | ||
396 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
e1fff881 | 397 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE) |
2d18b16d | 398 | { |
399 | /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */ | |
400 | unsigned i; | |
401 | tree val; | |
402 | ||
403 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) | |
e1fff881 | 404 | if (! CONSTANT_CLASS_P (val)) |
2d18b16d | 405 | return NULL_TREE; |
406 | ||
407 | return build_vector_from_ctor (TREE_TYPE (rhs), | |
408 | CONSTRUCTOR_ELTS (rhs)); | |
409 | } | |
410 | ||
411 | else if (DECL_P (rhs)) | |
8f266cd9 | 412 | return get_symbol_constant_value (rhs); |
2d18b16d | 413 | } |
414 | break; | |
415 | ||
416 | case GIMPLE_UNARY_RHS: | |
2d18b16d | 417 | break; |
418 | ||
419 | case GIMPLE_BINARY_RHS: | |
2d18b16d | 420 | break; |
421 | ||
00f4f705 | 422 | case GIMPLE_TERNARY_RHS: |
f11d6df8 | 423 | result = fold_ternary_loc (loc, subcode, |
424 | TREE_TYPE (gimple_assign_lhs (stmt)), | |
425 | gimple_assign_rhs1 (stmt), | |
426 | gimple_assign_rhs2 (stmt), | |
427 | gimple_assign_rhs3 (stmt)); | |
00f4f705 | 428 | |
429 | if (result) | |
430 | { | |
431 | STRIP_USELESS_TYPE_CONVERSION (result); | |
432 | if (valid_gimple_rhs_p (result)) | |
433 | return result; | |
00f4f705 | 434 | } |
435 | break; | |
436 | ||
2d18b16d | 437 | case GIMPLE_INVALID_RHS: |
438 | gcc_unreachable (); | |
439 | } | |
440 | ||
441 | return NULL_TREE; | |
442 | } | |
443 | ||
b9ea678c | 444 | |
445 | /* Replace a statement at *SI_P with a sequence of statements in STMTS, | |
446 | adjusting the replacement stmts location and virtual operands. | |
447 | If the statement has a lhs the last stmt in the sequence is expected | |
448 | to assign to that lhs. */ | |
449 | ||
450 | static void | |
451 | gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts) | |
452 | { | |
42acab1c | 453 | gimple *stmt = gsi_stmt (*si_p); |
b9ea678c | 454 | |
455 | if (gimple_has_location (stmt)) | |
456 | annotate_all_with_location (stmts, gimple_location (stmt)); | |
457 | ||
458 | /* First iterate over the replacement statements backward, assigning | |
459 | virtual operands to their defining statements. */ | |
42acab1c | 460 | gimple *laststore = NULL; |
b9ea678c | 461 | for (gimple_stmt_iterator i = gsi_last (stmts); |
462 | !gsi_end_p (i); gsi_prev (&i)) | |
463 | { | |
42acab1c | 464 | gimple *new_stmt = gsi_stmt (i); |
b9ea678c | 465 | if ((gimple_assign_single_p (new_stmt) |
466 | && !is_gimple_reg (gimple_assign_lhs (new_stmt))) | |
467 | || (is_gimple_call (new_stmt) | |
468 | && (gimple_call_flags (new_stmt) | |
469 | & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0)) | |
470 | { | |
471 | tree vdef; | |
472 | if (!laststore) | |
473 | vdef = gimple_vdef (stmt); | |
474 | else | |
475 | vdef = make_ssa_name (gimple_vop (cfun), new_stmt); | |
476 | gimple_set_vdef (new_stmt, vdef); | |
477 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
478 | SSA_NAME_DEF_STMT (vdef) = new_stmt; | |
479 | laststore = new_stmt; | |
480 | } | |
481 | } | |
482 | ||
483 | /* Second iterate over the statements forward, assigning virtual | |
484 | operands to their uses. */ | |
485 | tree reaching_vuse = gimple_vuse (stmt); | |
486 | for (gimple_stmt_iterator i = gsi_start (stmts); | |
487 | !gsi_end_p (i); gsi_next (&i)) | |
488 | { | |
42acab1c | 489 | gimple *new_stmt = gsi_stmt (i); |
b9ea678c | 490 | /* If the new statement possibly has a VUSE, update it with exact SSA |
491 | name we know will reach this one. */ | |
492 | if (gimple_has_mem_ops (new_stmt)) | |
493 | gimple_set_vuse (new_stmt, reaching_vuse); | |
494 | gimple_set_modified (new_stmt, true); | |
495 | if (gimple_vdef (new_stmt)) | |
496 | reaching_vuse = gimple_vdef (new_stmt); | |
497 | } | |
498 | ||
499 | /* If the new sequence does not do a store release the virtual | |
500 | definition of the original statement. */ | |
501 | if (reaching_vuse | |
502 | && reaching_vuse == gimple_vuse (stmt)) | |
503 | { | |
504 | tree vdef = gimple_vdef (stmt); | |
505 | if (vdef | |
506 | && TREE_CODE (vdef) == SSA_NAME) | |
507 | { | |
508 | unlink_stmt_vdef (stmt); | |
509 | release_ssa_name (vdef); | |
510 | } | |
511 | } | |
512 | ||
513 | /* Finally replace the original statement with the sequence. */ | |
514 | gsi_replace_with_seq (si_p, stmts, false); | |
515 | } | |
516 | ||
2d18b16d | 517 | /* Convert EXPR into a GIMPLE value suitable for substitution on the |
518 | RHS of an assignment. Insert the necessary statements before | |
519 | iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL | |
520 | is replaced. If the call is expected to produces a result, then it | |
521 | is replaced by an assignment of the new RHS to the result variable. | |
522 | If the result is to be ignored, then the call is replaced by a | |
3aadae2d | 523 | GIMPLE_NOP. A proper VDEF chain is retained by making the first |
524 | VUSE and the last VDEF of the whole sequence be the same as the replaced | |
525 | statement and using new SSA names for stores in between. */ | |
2d18b16d | 526 | |
527 | void | |
528 | gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr) | |
529 | { | |
530 | tree lhs; | |
42acab1c | 531 | gimple *stmt, *new_stmt; |
2d18b16d | 532 | gimple_stmt_iterator i; |
e3a19533 | 533 | gimple_seq stmts = NULL; |
2d18b16d | 534 | |
535 | stmt = gsi_stmt (*si_p); | |
536 | ||
537 | gcc_assert (is_gimple_call (stmt)); | |
538 | ||
8a4a28a8 | 539 | push_gimplify_context (gimple_in_ssa_p (cfun)); |
2d18b16d | 540 | |
34e73149 | 541 | lhs = gimple_call_lhs (stmt); |
2d18b16d | 542 | if (lhs == NULL_TREE) |
a3087021 | 543 | { |
544 | gimplify_and_add (expr, &stmts); | |
545 | /* We can end up with folding a memcpy of an empty class assignment | |
546 | which gets optimized away by C++ gimplification. */ | |
547 | if (gimple_seq_empty_p (stmts)) | |
548 | { | |
ec4a428c | 549 | pop_gimplify_context (NULL); |
a3087021 | 550 | if (gimple_in_ssa_p (cfun)) |
551 | { | |
552 | unlink_stmt_vdef (stmt); | |
553 | release_defs (stmt); | |
554 | } | |
cfe45b40 | 555 | gsi_replace (si_p, gimple_build_nop (), false); |
a3087021 | 556 | return; |
557 | } | |
558 | } | |
2d18b16d | 559 | else |
34e73149 | 560 | { |
9ae1b28a | 561 | tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE); |
34e73149 | 562 | new_stmt = gimple_build_assign (lhs, tmp); |
563 | i = gsi_last (stmts); | |
564 | gsi_insert_after_without_update (&i, new_stmt, | |
565 | GSI_CONTINUE_LINKING); | |
566 | } | |
2d18b16d | 567 | |
568 | pop_gimplify_context (NULL); | |
569 | ||
b9ea678c | 570 | gsi_replace_with_seq_vops (si_p, stmts); |
571 | } | |
2d18b16d | 572 | |
b9ea678c | 573 | |
574 | /* Replace the call at *GSI with the gimple value VAL. */ | |
575 | ||
d08919a7 | 576 | void |
b9ea678c | 577 | replace_call_with_value (gimple_stmt_iterator *gsi, tree val) |
578 | { | |
42acab1c | 579 | gimple *stmt = gsi_stmt (*gsi); |
b9ea678c | 580 | tree lhs = gimple_call_lhs (stmt); |
42acab1c | 581 | gimple *repl; |
b9ea678c | 582 | if (lhs) |
34e73149 | 583 | { |
b9ea678c | 584 | if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val))) |
585 | val = fold_convert (TREE_TYPE (lhs), val); | |
586 | repl = gimple_build_assign (lhs, val); | |
587 | } | |
588 | else | |
589 | repl = gimple_build_nop (); | |
590 | tree vdef = gimple_vdef (stmt); | |
591 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
592 | { | |
593 | unlink_stmt_vdef (stmt); | |
594 | release_ssa_name (vdef); | |
595 | } | |
cfe45b40 | 596 | gsi_replace (gsi, repl, false); |
b9ea678c | 597 | } |
598 | ||
599 | /* Replace the call at *GSI with the new call REPL and fold that | |
600 | again. */ | |
601 | ||
602 | static void | |
42acab1c | 603 | replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl) |
b9ea678c | 604 | { |
42acab1c | 605 | gimple *stmt = gsi_stmt (*gsi); |
b9ea678c | 606 | gimple_call_set_lhs (repl, gimple_call_lhs (stmt)); |
607 | gimple_set_location (repl, gimple_location (stmt)); | |
608 | if (gimple_vdef (stmt) | |
609 | && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) | |
610 | { | |
611 | gimple_set_vdef (repl, gimple_vdef (stmt)); | |
b9ea678c | 612 | SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl; |
613 | } | |
983fdeb3 | 614 | if (gimple_vuse (stmt)) |
615 | gimple_set_vuse (repl, gimple_vuse (stmt)); | |
cfe45b40 | 616 | gsi_replace (gsi, repl, false); |
b9ea678c | 617 | fold_stmt (gsi); |
618 | } | |
619 | ||
620 | /* Return true if VAR is a VAR_DECL or a component thereof. */ | |
621 | ||
622 | static bool | |
623 | var_decl_component_p (tree var) | |
624 | { | |
625 | tree inner = var; | |
626 | while (handled_component_p (inner)) | |
627 | inner = TREE_OPERAND (inner, 0); | |
628 | return SSA_VAR_P (inner); | |
629 | } | |
630 | ||
631 | /* Fold function call to builtin mem{{,p}cpy,move}. Return | |
fbbdee2b | 632 | false if no simplification can be made. |
b9ea678c | 633 | If ENDP is 0, return DEST (like memcpy). |
634 | If ENDP is 1, return DEST+LEN (like mempcpy). | |
635 | If ENDP is 2, return DEST+LEN-1 (like stpcpy). | |
636 | If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap | |
637 | (memmove). */ | |
638 | ||
639 | static bool | |
640 | gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, | |
641 | tree dest, tree src, int endp) | |
642 | { | |
42acab1c | 643 | gimple *stmt = gsi_stmt (*gsi); |
b9ea678c | 644 | tree lhs = gimple_call_lhs (stmt); |
645 | tree len = gimple_call_arg (stmt, 2); | |
646 | tree destvar, srcvar; | |
647 | location_t loc = gimple_location (stmt); | |
648 | ||
649 | /* If the LEN parameter is zero, return DEST. */ | |
650 | if (integer_zerop (len)) | |
651 | { | |
42acab1c | 652 | gimple *repl; |
b9ea678c | 653 | if (gimple_call_lhs (stmt)) |
654 | repl = gimple_build_assign (gimple_call_lhs (stmt), dest); | |
655 | else | |
656 | repl = gimple_build_nop (); | |
657 | tree vdef = gimple_vdef (stmt); | |
658 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
34e73149 | 659 | { |
b9ea678c | 660 | unlink_stmt_vdef (stmt); |
661 | release_ssa_name (vdef); | |
662 | } | |
cfe45b40 | 663 | gsi_replace (gsi, repl, false); |
b9ea678c | 664 | return true; |
665 | } | |
666 | ||
667 | /* If SRC and DEST are the same (and not volatile), return | |
668 | DEST{,+LEN,+LEN-1}. */ | |
669 | if (operand_equal_p (src, dest, 0)) | |
670 | { | |
671 | unlink_stmt_vdef (stmt); | |
672 | if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) | |
673 | release_ssa_name (gimple_vdef (stmt)); | |
674 | if (!lhs) | |
675 | { | |
cfe45b40 | 676 | gsi_replace (gsi, gimple_build_nop (), false); |
b9ea678c | 677 | return true; |
678 | } | |
679 | goto done; | |
680 | } | |
681 | else | |
682 | { | |
683 | tree srctype, desttype; | |
684 | unsigned int src_align, dest_align; | |
685 | tree off0; | |
686 | ||
57cbac18 | 687 | /* Inlining of memcpy/memmove may cause bounds lost (if we copy |
688 | pointers as wide integer) and also may result in huge function | |
689 | size because of inlined bounds copy. Thus don't inline for | |
690 | functions we want to instrument. */ | |
691 | if (flag_check_pointer_bounds | |
692 | && chkp_instrumentable_p (cfun->decl) | |
693 | /* Even if data may contain pointers we can inline if copy | |
694 | less than a pointer size. */ | |
695 | && (!tree_fits_uhwi_p (len) | |
696 | || compare_tree_int (len, POINTER_SIZE_UNITS) >= 0)) | |
697 | return false; | |
698 | ||
b9ea678c | 699 | /* Build accesses at offset zero with a ref-all character type. */ |
700 | off0 = build_int_cst (build_pointer_type_for_mode (char_type_node, | |
701 | ptr_mode, true), 0); | |
702 | ||
703 | /* If we can perform the copy efficiently with first doing all loads | |
704 | and then all stores inline it that way. Currently efficiently | |
705 | means that we can load all the memory into a single integer | |
706 | register which is what MOVE_MAX gives us. */ | |
707 | src_align = get_pointer_alignment (src); | |
708 | dest_align = get_pointer_alignment (dest); | |
709 | if (tree_fits_uhwi_p (len) | |
710 | && compare_tree_int (len, MOVE_MAX) <= 0 | |
711 | /* ??? Don't transform copies from strings with known length this | |
712 | confuses the tree-ssa-strlen.c. This doesn't handle | |
713 | the case in gcc.dg/strlenopt-8.c which is XFAILed for that | |
714 | reason. */ | |
715 | && !c_strlen (src, 2)) | |
716 | { | |
717 | unsigned ilen = tree_to_uhwi (len); | |
ac29ece2 | 718 | if (pow2p_hwi (ilen)) |
b9ea678c | 719 | { |
720 | tree type = lang_hooks.types.type_for_size (ilen * 8, 1); | |
721 | if (type | |
722 | && TYPE_MODE (type) != BLKmode | |
723 | && (GET_MODE_SIZE (TYPE_MODE (type)) * BITS_PER_UNIT | |
724 | == ilen * 8) | |
725 | /* If the destination pointer is not aligned we must be able | |
726 | to emit an unaligned store. */ | |
727 | && (dest_align >= GET_MODE_ALIGNMENT (TYPE_MODE (type)) | |
6b7a6f44 | 728 | || !SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), dest_align) |
729 | || (optab_handler (movmisalign_optab, TYPE_MODE (type)) | |
730 | != CODE_FOR_nothing))) | |
b9ea678c | 731 | { |
732 | tree srctype = type; | |
733 | tree desttype = type; | |
734 | if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))) | |
735 | srctype = build_aligned_type (type, src_align); | |
736 | tree srcmem = fold_build2 (MEM_REF, srctype, src, off0); | |
737 | tree tem = fold_const_aggregate_ref (srcmem); | |
738 | if (tem) | |
739 | srcmem = tem; | |
740 | else if (src_align < GET_MODE_ALIGNMENT (TYPE_MODE (type)) | |
741 | && SLOW_UNALIGNED_ACCESS (TYPE_MODE (type), | |
6b7a6f44 | 742 | src_align) |
743 | && (optab_handler (movmisalign_optab, | |
744 | TYPE_MODE (type)) | |
745 | == CODE_FOR_nothing)) | |
b9ea678c | 746 | srcmem = NULL_TREE; |
747 | if (srcmem) | |
748 | { | |
42acab1c | 749 | gimple *new_stmt; |
b9ea678c | 750 | if (is_gimple_reg_type (TREE_TYPE (srcmem))) |
751 | { | |
752 | new_stmt = gimple_build_assign (NULL_TREE, srcmem); | |
c985cfb4 | 753 | srcmem |
754 | = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem), | |
755 | new_stmt); | |
b9ea678c | 756 | gimple_assign_set_lhs (new_stmt, srcmem); |
757 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
758 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
759 | } | |
760 | if (dest_align < GET_MODE_ALIGNMENT (TYPE_MODE (type))) | |
761 | desttype = build_aligned_type (type, dest_align); | |
762 | new_stmt | |
763 | = gimple_build_assign (fold_build2 (MEM_REF, desttype, | |
764 | dest, off0), | |
765 | srcmem); | |
766 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
767 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
768 | if (gimple_vdef (new_stmt) | |
769 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
770 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
771 | if (!lhs) | |
772 | { | |
cfe45b40 | 773 | gsi_replace (gsi, new_stmt, false); |
b9ea678c | 774 | return true; |
775 | } | |
776 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
777 | goto done; | |
778 | } | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
783 | if (endp == 3) | |
784 | { | |
785 | /* Both DEST and SRC must be pointer types. | |
786 | ??? This is what old code did. Is the testing for pointer types | |
787 | really mandatory? | |
788 | ||
789 | If either SRC is readonly or length is 1, we can use memcpy. */ | |
790 | if (!dest_align || !src_align) | |
791 | return false; | |
792 | if (readonly_data_expr (src) | |
793 | || (tree_fits_uhwi_p (len) | |
794 | && (MIN (src_align, dest_align) / BITS_PER_UNIT | |
795 | >= tree_to_uhwi (len)))) | |
796 | { | |
797 | tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
798 | if (!fn) | |
799 | return false; | |
800 | gimple_call_set_fndecl (stmt, fn); | |
801 | gimple_call_set_arg (stmt, 0, dest); | |
802 | gimple_call_set_arg (stmt, 1, src); | |
803 | fold_stmt (gsi); | |
804 | return true; | |
805 | } | |
806 | ||
807 | /* If *src and *dest can't overlap, optimize into memcpy as well. */ | |
808 | if (TREE_CODE (src) == ADDR_EXPR | |
809 | && TREE_CODE (dest) == ADDR_EXPR) | |
810 | { | |
811 | tree src_base, dest_base, fn; | |
812 | HOST_WIDE_INT src_offset = 0, dest_offset = 0; | |
54e95a17 | 813 | HOST_WIDE_INT maxsize; |
b9ea678c | 814 | |
815 | srcvar = TREE_OPERAND (src, 0); | |
54e95a17 | 816 | src_base = get_addr_base_and_unit_offset (srcvar, &src_offset); |
817 | if (src_base == NULL) | |
818 | src_base = srcvar; | |
b9ea678c | 819 | destvar = TREE_OPERAND (dest, 0); |
54e95a17 | 820 | dest_base = get_addr_base_and_unit_offset (destvar, |
821 | &dest_offset); | |
822 | if (dest_base == NULL) | |
823 | dest_base = destvar; | |
b9ea678c | 824 | if (tree_fits_uhwi_p (len)) |
825 | maxsize = tree_to_uhwi (len); | |
826 | else | |
827 | maxsize = -1; | |
b9ea678c | 828 | if (SSA_VAR_P (src_base) |
829 | && SSA_VAR_P (dest_base)) | |
830 | { | |
831 | if (operand_equal_p (src_base, dest_base, 0) | |
832 | && ranges_overlap_p (src_offset, maxsize, | |
833 | dest_offset, maxsize)) | |
834 | return false; | |
835 | } | |
836 | else if (TREE_CODE (src_base) == MEM_REF | |
837 | && TREE_CODE (dest_base) == MEM_REF) | |
838 | { | |
839 | if (! operand_equal_p (TREE_OPERAND (src_base, 0), | |
840 | TREE_OPERAND (dest_base, 0), 0)) | |
841 | return false; | |
842 | offset_int off = mem_ref_offset (src_base) + src_offset; | |
843 | if (!wi::fits_shwi_p (off)) | |
844 | return false; | |
845 | src_offset = off.to_shwi (); | |
846 | ||
847 | off = mem_ref_offset (dest_base) + dest_offset; | |
848 | if (!wi::fits_shwi_p (off)) | |
849 | return false; | |
850 | dest_offset = off.to_shwi (); | |
851 | if (ranges_overlap_p (src_offset, maxsize, | |
852 | dest_offset, maxsize)) | |
853 | return false; | |
854 | } | |
855 | else | |
856 | return false; | |
857 | ||
858 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
859 | if (!fn) | |
860 | return false; | |
861 | gimple_call_set_fndecl (stmt, fn); | |
862 | gimple_call_set_arg (stmt, 0, dest); | |
863 | gimple_call_set_arg (stmt, 1, src); | |
864 | fold_stmt (gsi); | |
865 | return true; | |
866 | } | |
867 | ||
868 | /* If the destination and source do not alias optimize into | |
869 | memcpy as well. */ | |
870 | if ((is_gimple_min_invariant (dest) | |
871 | || TREE_CODE (dest) == SSA_NAME) | |
872 | && (is_gimple_min_invariant (src) | |
873 | || TREE_CODE (src) == SSA_NAME)) | |
874 | { | |
875 | ao_ref destr, srcr; | |
876 | ao_ref_init_from_ptr_and_size (&destr, dest, len); | |
877 | ao_ref_init_from_ptr_and_size (&srcr, src, len); | |
878 | if (!refs_may_alias_p_1 (&destr, &srcr, false)) | |
879 | { | |
880 | tree fn; | |
881 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
882 | if (!fn) | |
883 | return false; | |
884 | gimple_call_set_fndecl (stmt, fn); | |
885 | gimple_call_set_arg (stmt, 0, dest); | |
886 | gimple_call_set_arg (stmt, 1, src); | |
887 | fold_stmt (gsi); | |
888 | return true; | |
889 | } | |
890 | } | |
891 | ||
892 | return false; | |
893 | } | |
894 | ||
895 | if (!tree_fits_shwi_p (len)) | |
896 | return false; | |
897 | /* FIXME: | |
898 | This logic lose for arguments like (type *)malloc (sizeof (type)), | |
899 | since we strip the casts of up to VOID return value from malloc. | |
900 | Perhaps we ought to inherit type from non-VOID argument here? */ | |
901 | STRIP_NOPS (src); | |
902 | STRIP_NOPS (dest); | |
903 | if (!POINTER_TYPE_P (TREE_TYPE (src)) | |
904 | || !POINTER_TYPE_P (TREE_TYPE (dest))) | |
905 | return false; | |
906 | /* In the following try to find a type that is most natural to be | |
907 | used for the memcpy source and destination and that allows | |
908 | the most optimization when memcpy is turned into a plain assignment | |
909 | using that type. In theory we could always use a char[len] type | |
910 | but that only gains us that the destination and source possibly | |
911 | no longer will have their address taken. */ | |
912 | /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */ | |
913 | if (TREE_CODE (src) == POINTER_PLUS_EXPR) | |
914 | { | |
915 | tree tem = TREE_OPERAND (src, 0); | |
916 | STRIP_NOPS (tem); | |
917 | if (tem != TREE_OPERAND (src, 0)) | |
918 | src = build1 (NOP_EXPR, TREE_TYPE (tem), src); | |
919 | } | |
920 | if (TREE_CODE (dest) == POINTER_PLUS_EXPR) | |
921 | { | |
922 | tree tem = TREE_OPERAND (dest, 0); | |
923 | STRIP_NOPS (tem); | |
924 | if (tem != TREE_OPERAND (dest, 0)) | |
925 | dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest); | |
926 | } | |
927 | srctype = TREE_TYPE (TREE_TYPE (src)); | |
928 | if (TREE_CODE (srctype) == ARRAY_TYPE | |
929 | && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) | |
930 | { | |
931 | srctype = TREE_TYPE (srctype); | |
932 | STRIP_NOPS (src); | |
933 | src = build1 (NOP_EXPR, build_pointer_type (srctype), src); | |
934 | } | |
935 | desttype = TREE_TYPE (TREE_TYPE (dest)); | |
936 | if (TREE_CODE (desttype) == ARRAY_TYPE | |
937 | && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
938 | { | |
939 | desttype = TREE_TYPE (desttype); | |
940 | STRIP_NOPS (dest); | |
941 | dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest); | |
942 | } | |
943 | if (TREE_ADDRESSABLE (srctype) | |
944 | || TREE_ADDRESSABLE (desttype)) | |
945 | return false; | |
946 | ||
947 | /* Make sure we are not copying using a floating-point mode or | |
948 | a type whose size possibly does not match its precision. */ | |
949 | if (FLOAT_MODE_P (TYPE_MODE (desttype)) | |
950 | || TREE_CODE (desttype) == BOOLEAN_TYPE | |
951 | || TREE_CODE (desttype) == ENUMERAL_TYPE) | |
952 | desttype = bitwise_type_for_mode (TYPE_MODE (desttype)); | |
953 | if (FLOAT_MODE_P (TYPE_MODE (srctype)) | |
954 | || TREE_CODE (srctype) == BOOLEAN_TYPE | |
955 | || TREE_CODE (srctype) == ENUMERAL_TYPE) | |
956 | srctype = bitwise_type_for_mode (TYPE_MODE (srctype)); | |
957 | if (!srctype) | |
958 | srctype = desttype; | |
959 | if (!desttype) | |
960 | desttype = srctype; | |
961 | if (!srctype) | |
962 | return false; | |
963 | ||
964 | src_align = get_pointer_alignment (src); | |
965 | dest_align = get_pointer_alignment (dest); | |
966 | if (dest_align < TYPE_ALIGN (desttype) | |
967 | || src_align < TYPE_ALIGN (srctype)) | |
968 | return false; | |
969 | ||
970 | destvar = dest; | |
971 | STRIP_NOPS (destvar); | |
972 | if (TREE_CODE (destvar) == ADDR_EXPR | |
973 | && var_decl_component_p (TREE_OPERAND (destvar, 0)) | |
974 | && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)) | |
975 | destvar = fold_build2 (MEM_REF, desttype, destvar, off0); | |
976 | else | |
977 | destvar = NULL_TREE; | |
978 | ||
979 | srcvar = src; | |
980 | STRIP_NOPS (srcvar); | |
981 | if (TREE_CODE (srcvar) == ADDR_EXPR | |
982 | && var_decl_component_p (TREE_OPERAND (srcvar, 0)) | |
983 | && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)) | |
984 | { | |
985 | if (!destvar | |
986 | || src_align >= TYPE_ALIGN (desttype)) | |
987 | srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype, | |
988 | srcvar, off0); | |
989 | else if (!STRICT_ALIGNMENT) | |
990 | { | |
991 | srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype), | |
992 | src_align); | |
993 | srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0); | |
994 | } | |
34e73149 | 995 | else |
b9ea678c | 996 | srcvar = NULL_TREE; |
997 | } | |
998 | else | |
999 | srcvar = NULL_TREE; | |
1000 | ||
1001 | if (srcvar == NULL_TREE && destvar == NULL_TREE) | |
1002 | return false; | |
1003 | ||
1004 | if (srcvar == NULL_TREE) | |
1005 | { | |
1006 | STRIP_NOPS (src); | |
1007 | if (src_align >= TYPE_ALIGN (desttype)) | |
1008 | srcvar = fold_build2 (MEM_REF, desttype, src, off0); | |
1009 | else | |
1010 | { | |
1011 | if (STRICT_ALIGNMENT) | |
1012 | return false; | |
1013 | srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype), | |
1014 | src_align); | |
1015 | srcvar = fold_build2 (MEM_REF, srctype, src, off0); | |
1016 | } | |
1017 | } | |
1018 | else if (destvar == NULL_TREE) | |
1019 | { | |
1020 | STRIP_NOPS (dest); | |
1021 | if (dest_align >= TYPE_ALIGN (srctype)) | |
1022 | destvar = fold_build2 (MEM_REF, srctype, dest, off0); | |
1023 | else | |
1024 | { | |
1025 | if (STRICT_ALIGNMENT) | |
1026 | return false; | |
1027 | desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype), | |
1028 | dest_align); | |
1029 | destvar = fold_build2 (MEM_REF, desttype, dest, off0); | |
1030 | } | |
1031 | } | |
1032 | ||
42acab1c | 1033 | gimple *new_stmt; |
b9ea678c | 1034 | if (is_gimple_reg_type (TREE_TYPE (srcvar))) |
1035 | { | |
dbf504ce | 1036 | tree tem = fold_const_aggregate_ref (srcvar); |
1037 | if (tem) | |
1038 | srcvar = tem; | |
1039 | if (! is_gimple_min_invariant (srcvar)) | |
1040 | { | |
1041 | new_stmt = gimple_build_assign (NULL_TREE, srcvar); | |
c985cfb4 | 1042 | srcvar = create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar), |
1043 | new_stmt); | |
dbf504ce | 1044 | gimple_assign_set_lhs (new_stmt, srcvar); |
1045 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
1046 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1047 | } | |
b9ea678c | 1048 | } |
1049 | new_stmt = gimple_build_assign (destvar, srcvar); | |
1050 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
1051 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
1052 | if (gimple_vdef (new_stmt) | |
1053 | && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME) | |
1054 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
1055 | if (!lhs) | |
1056 | { | |
cfe45b40 | 1057 | gsi_replace (gsi, new_stmt, false); |
b9ea678c | 1058 | return true; |
1059 | } | |
1060 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); | |
1061 | } | |
1062 | ||
1063 | done: | |
cba8396b | 1064 | gimple_seq stmts = NULL; |
b9ea678c | 1065 | if (endp == 0 || endp == 3) |
1066 | len = NULL_TREE; | |
1067 | else if (endp == 2) | |
cba8396b | 1068 | len = gimple_build (&stmts, loc, MINUS_EXPR, TREE_TYPE (len), len, |
1069 | ssize_int (1)); | |
b9ea678c | 1070 | if (endp == 2 || endp == 1) |
cba8396b | 1071 | { |
1072 | len = gimple_convert_to_ptrofftype (&stmts, loc, len); | |
1073 | dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR, | |
1074 | TREE_TYPE (dest), dest, len); | |
1075 | } | |
b9ea678c | 1076 | |
cba8396b | 1077 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
42acab1c | 1078 | gimple *repl = gimple_build_assign (lhs, dest); |
cfe45b40 | 1079 | gsi_replace (gsi, repl, false); |
b9ea678c | 1080 | return true; |
1081 | } | |
1082 | ||
9f5f711c | 1083 | /* Transform a call to built-in bcmp(a, b, len) at *GSI into one |
1084 | to built-in memcmp (a, b, len). */ | |
1085 | ||
1086 | static bool | |
1087 | gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi) | |
1088 | { | |
1089 | tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP); | |
1090 | ||
1091 | if (!fn) | |
1092 | return false; | |
1093 | ||
1094 | /* Transform bcmp (a, b, len) into memcmp (a, b, len). */ | |
1095 | ||
1096 | gimple *stmt = gsi_stmt (*gsi); | |
1097 | tree a = gimple_call_arg (stmt, 0); | |
1098 | tree b = gimple_call_arg (stmt, 1); | |
1099 | tree len = gimple_call_arg (stmt, 2); | |
1100 | ||
1101 | gimple *repl = gimple_build_call (fn, 3, a, b, len); | |
1102 | replace_call_with_call_and_fold (gsi, repl); | |
1103 | ||
1104 | return true; | |
1105 | } | |
1106 | ||
1107 | /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one | |
1108 | to built-in memmove (dest, src, len). */ | |
1109 | ||
1110 | static bool | |
1111 | gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi) | |
1112 | { | |
1113 | tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE); | |
1114 | ||
1115 | if (!fn) | |
1116 | return false; | |
1117 | ||
1118 | /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies | |
1119 | it's quivalent to memmove (not memcpy). Transform bcopy (src, dest, | |
1120 | len) into memmove (dest, src, len). */ | |
1121 | ||
1122 | gimple *stmt = gsi_stmt (*gsi); | |
1123 | tree src = gimple_call_arg (stmt, 0); | |
1124 | tree dest = gimple_call_arg (stmt, 1); | |
1125 | tree len = gimple_call_arg (stmt, 2); | |
1126 | ||
1127 | gimple *repl = gimple_build_call (fn, 3, dest, src, len); | |
1128 | gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn)); | |
1129 | replace_call_with_call_and_fold (gsi, repl); | |
1130 | ||
1131 | return true; | |
1132 | } | |
1133 | ||
1134 | /* Transform a call to built-in bzero (dest, len) at *GSI into one | |
1135 | to built-in memset (dest, 0, len). */ | |
1136 | ||
1137 | static bool | |
1138 | gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi) | |
1139 | { | |
1140 | tree fn = builtin_decl_implicit (BUILT_IN_MEMSET); | |
1141 | ||
1142 | if (!fn) | |
1143 | return false; | |
1144 | ||
1145 | /* Transform bzero (dest, len) into memset (dest, 0, len). */ | |
1146 | ||
1147 | gimple *stmt = gsi_stmt (*gsi); | |
1148 | tree dest = gimple_call_arg (stmt, 0); | |
1149 | tree len = gimple_call_arg (stmt, 1); | |
1150 | ||
1151 | gimple_seq seq = NULL; | |
1152 | gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len); | |
1153 | gimple_seq_add_stmt_without_update (&seq, repl); | |
1154 | gsi_replace_with_seq_vops (gsi, seq); | |
1155 | fold_stmt (gsi); | |
1156 | ||
1157 | return true; | |
1158 | } | |
1159 | ||
b9ea678c | 1160 | /* Fold function call to builtin memset or bzero at *GSI setting the |
1161 | memory of size LEN to VAL. Return whether a simplification was made. */ | |
1162 | ||
1163 | static bool | |
1164 | gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len) | |
1165 | { | |
42acab1c | 1166 | gimple *stmt = gsi_stmt (*gsi); |
b9ea678c | 1167 | tree etype; |
1168 | unsigned HOST_WIDE_INT length, cval; | |
1169 | ||
1170 | /* If the LEN parameter is zero, return DEST. */ | |
1171 | if (integer_zerop (len)) | |
1172 | { | |
1173 | replace_call_with_value (gsi, gimple_call_arg (stmt, 0)); | |
1174 | return true; | |
1175 | } | |
1176 | ||
1177 | if (! tree_fits_uhwi_p (len)) | |
1178 | return false; | |
1179 | ||
1180 | if (TREE_CODE (c) != INTEGER_CST) | |
1181 | return false; | |
1182 | ||
1183 | tree dest = gimple_call_arg (stmt, 0); | |
1184 | tree var = dest; | |
1185 | if (TREE_CODE (var) != ADDR_EXPR) | |
1186 | return false; | |
1187 | ||
1188 | var = TREE_OPERAND (var, 0); | |
1189 | if (TREE_THIS_VOLATILE (var)) | |
1190 | return false; | |
1191 | ||
1192 | etype = TREE_TYPE (var); | |
1193 | if (TREE_CODE (etype) == ARRAY_TYPE) | |
1194 | etype = TREE_TYPE (etype); | |
1195 | ||
1196 | if (!INTEGRAL_TYPE_P (etype) | |
1197 | && !POINTER_TYPE_P (etype)) | |
1198 | return NULL_TREE; | |
1199 | ||
1200 | if (! var_decl_component_p (var)) | |
1201 | return NULL_TREE; | |
1202 | ||
1203 | length = tree_to_uhwi (len); | |
1204 | if (GET_MODE_SIZE (TYPE_MODE (etype)) != length | |
1205 | || get_pointer_alignment (dest) / BITS_PER_UNIT < length) | |
1206 | return NULL_TREE; | |
1207 | ||
1208 | if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) | |
1209 | return NULL_TREE; | |
1210 | ||
1211 | if (integer_zerop (c)) | |
1212 | cval = 0; | |
1213 | else | |
1214 | { | |
1215 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64) | |
1216 | return NULL_TREE; | |
1217 | ||
1218 | cval = TREE_INT_CST_LOW (c); | |
1219 | cval &= 0xff; | |
1220 | cval |= cval << 8; | |
1221 | cval |= cval << 16; | |
1222 | cval |= (cval << 31) << 1; | |
1223 | } | |
1224 | ||
1225 | var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0)); | |
42acab1c | 1226 | gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval)); |
b9ea678c | 1227 | gimple_set_vuse (store, gimple_vuse (stmt)); |
1228 | tree vdef = gimple_vdef (stmt); | |
1229 | if (vdef && TREE_CODE (vdef) == SSA_NAME) | |
1230 | { | |
1231 | gimple_set_vdef (store, gimple_vdef (stmt)); | |
1232 | SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store; | |
1233 | } | |
1234 | gsi_insert_before (gsi, store, GSI_SAME_STMT); | |
1235 | if (gimple_call_lhs (stmt)) | |
1236 | { | |
42acab1c | 1237 | gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest); |
cfe45b40 | 1238 | gsi_replace (gsi, asgn, false); |
b9ea678c | 1239 | } |
1240 | else | |
1241 | { | |
1242 | gimple_stmt_iterator gsi2 = *gsi; | |
1243 | gsi_prev (gsi); | |
1244 | gsi_remove (&gsi2, true); | |
1245 | } | |
1246 | ||
1247 | return true; | |
1248 | } | |
1249 | ||
1250 | ||
b9833bfd | 1251 | /* Obtain the minimum and maximum string length or minimum and maximum |
1252 | value of ARG in LENGTH[0] and LENGTH[1], respectively. | |
1253 | If ARG is an SSA name variable, follow its use-def chains. When | |
1254 | TYPE == 0, if LENGTH[1] is not equal to the length we determine or | |
1255 | if we are unable to determine the length or value, return False. | |
1256 | VISITED is a bitmap of visited variables. | |
1257 | TYPE is 0 if string length should be obtained, 1 for maximum string | |
1258 | length and 2 for maximum value ARG can have. | |
1259 | When FUZZY is set and the length of a string cannot be determined, | |
1260 | the function instead considers as the maximum possible length the | |
035409c3 | 1261 | size of a character array it may refer to. |
1262 | Set *FLEXP to true if the range of the string lengths has been | |
1263 | obtained from the upper bound of an array at the end of a struct. | |
1264 | Such an array may hold a string that's longer than its upper bound | |
1265 | due to it being used as a poor-man's flexible array member. */ | |
b9ea678c | 1266 | |
1267 | static bool | |
b9833bfd | 1268 | get_range_strlen (tree arg, tree length[2], bitmap *visited, int type, |
035409c3 | 1269 | bool fuzzy, bool *flexp) |
b9ea678c | 1270 | { |
1271 | tree var, val; | |
42acab1c | 1272 | gimple *def_stmt; |
b9ea678c | 1273 | |
b9833bfd | 1274 | /* The minimum and maximum length. The MAXLEN pointer stays unchanged |
1275 | but MINLEN may be cleared during the execution of the function. */ | |
1276 | tree *minlen = length; | |
1277 | tree *const maxlen = length + 1; | |
1278 | ||
b9ea678c | 1279 | if (TREE_CODE (arg) != SSA_NAME) |
1280 | { | |
1281 | /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */ | |
1282 | if (TREE_CODE (arg) == ADDR_EXPR | |
1283 | && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF | |
1284 | && integer_zerop (TREE_OPERAND (TREE_OPERAND (arg, 0), 1))) | |
1285 | { | |
1286 | tree aop0 = TREE_OPERAND (TREE_OPERAND (arg, 0), 0); | |
1287 | if (TREE_CODE (aop0) == INDIRECT_REF | |
1288 | && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME) | |
b9833bfd | 1289 | return get_range_strlen (TREE_OPERAND (aop0, 0), |
035409c3 | 1290 | length, visited, type, fuzzy, flexp); |
b9ea678c | 1291 | } |
1292 | ||
1293 | if (type == 2) | |
1294 | { | |
1295 | val = arg; | |
1296 | if (TREE_CODE (val) != INTEGER_CST | |
1297 | || tree_int_cst_sgn (val) < 0) | |
1298 | return false; | |
1299 | } | |
1300 | else | |
1301 | val = c_strlen (arg, 1); | |
b9833bfd | 1302 | |
1303 | if (!val && fuzzy) | |
1304 | { | |
1305 | if (TREE_CODE (arg) == ADDR_EXPR) | |
1306 | return get_range_strlen (TREE_OPERAND (arg, 0), length, | |
035409c3 | 1307 | visited, type, fuzzy, flexp); |
b9833bfd | 1308 | |
1309 | if (TREE_CODE (arg) == COMPONENT_REF | |
1310 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1))) == ARRAY_TYPE) | |
1311 | { | |
1312 | /* Use the type of the member array to determine the upper | |
1313 | bound on the length of the array. This may be overly | |
1314 | optimistic if the array itself isn't NUL-terminated and | |
1315 | the caller relies on the subsequent member to contain | |
035409c3 | 1316 | the NUL. |
1317 | Set *FLEXP to true if the array whose bound is being | |
1318 | used is at the end of a struct. */ | |
07110764 | 1319 | if (array_at_struct_end_p (arg)) |
035409c3 | 1320 | *flexp = true; |
1321 | ||
b9833bfd | 1322 | arg = TREE_OPERAND (arg, 1); |
1323 | val = TYPE_SIZE_UNIT (TREE_TYPE (arg)); | |
1324 | if (!val || integer_zerop (val)) | |
1325 | return false; | |
1326 | val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val, | |
1327 | integer_one_node); | |
dd66e028 | 1328 | /* Set the minimum size to zero since the string in |
1329 | the array could have zero length. */ | |
1330 | *minlen = ssize_int (0); | |
b9833bfd | 1331 | } |
1332 | } | |
1333 | ||
b9ea678c | 1334 | if (!val) |
1335 | return false; | |
1336 | ||
b9833bfd | 1337 | if (minlen |
1338 | && (!*minlen | |
1339 | || (type > 0 | |
1340 | && TREE_CODE (*minlen) == INTEGER_CST | |
1341 | && TREE_CODE (val) == INTEGER_CST | |
1342 | && tree_int_cst_lt (val, *minlen)))) | |
1343 | *minlen = val; | |
1344 | ||
1345 | if (*maxlen) | |
b9ea678c | 1346 | { |
1347 | if (type > 0) | |
1348 | { | |
b9833bfd | 1349 | if (TREE_CODE (*maxlen) != INTEGER_CST |
b9ea678c | 1350 | || TREE_CODE (val) != INTEGER_CST) |
1351 | return false; | |
1352 | ||
b9833bfd | 1353 | if (tree_int_cst_lt (*maxlen, val)) |
1354 | *maxlen = val; | |
b9ea678c | 1355 | return true; |
1356 | } | |
b9833bfd | 1357 | else if (simple_cst_equal (val, *maxlen) != 1) |
b9ea678c | 1358 | return false; |
1359 | } | |
1360 | ||
b9833bfd | 1361 | *maxlen = val; |
b9ea678c | 1362 | return true; |
1363 | } | |
1364 | ||
1365 | /* If ARG is registered for SSA update we cannot look at its defining | |
1366 | statement. */ | |
1367 | if (name_registered_for_update_p (arg)) | |
1368 | return false; | |
1369 | ||
1370 | /* If we were already here, break the infinite cycle. */ | |
2ccb7428 | 1371 | if (!*visited) |
1372 | *visited = BITMAP_ALLOC (NULL); | |
1373 | if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg))) | |
b9ea678c | 1374 | return true; |
1375 | ||
1376 | var = arg; | |
1377 | def_stmt = SSA_NAME_DEF_STMT (var); | |
1378 | ||
1379 | switch (gimple_code (def_stmt)) | |
1380 | { | |
1381 | case GIMPLE_ASSIGN: | |
1382 | /* The RHS of the statement defining VAR must either have a | |
1383 | constant length or come from another SSA_NAME with a constant | |
1384 | length. */ | |
1385 | if (gimple_assign_single_p (def_stmt) | |
1386 | || gimple_assign_unary_nop_p (def_stmt)) | |
1387 | { | |
1388 | tree rhs = gimple_assign_rhs1 (def_stmt); | |
035409c3 | 1389 | return get_range_strlen (rhs, length, visited, type, fuzzy, flexp); |
b9ea678c | 1390 | } |
1391 | else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR) | |
1392 | { | |
1393 | tree op2 = gimple_assign_rhs2 (def_stmt); | |
1394 | tree op3 = gimple_assign_rhs3 (def_stmt); | |
035409c3 | 1395 | return get_range_strlen (op2, length, visited, type, fuzzy, flexp) |
1396 | && get_range_strlen (op3, length, visited, type, fuzzy, flexp); | |
b9ea678c | 1397 | } |
1398 | return false; | |
1399 | ||
1400 | case GIMPLE_PHI: | |
1401 | { | |
1402 | /* All the arguments of the PHI node must have the same constant | |
1403 | length. */ | |
1404 | unsigned i; | |
1405 | ||
1406 | for (i = 0; i < gimple_phi_num_args (def_stmt); i++) | |
1407 | { | |
1408 | tree arg = gimple_phi_arg (def_stmt, i)->def; | |
1409 | ||
1410 | /* If this PHI has itself as an argument, we cannot | |
1411 | determine the string length of this argument. However, | |
1412 | if we can find a constant string length for the other | |
1413 | PHI args then we can still be sure that this is a | |
1414 | constant string length. So be optimistic and just | |
1415 | continue with the next argument. */ | |
1416 | if (arg == gimple_phi_result (def_stmt)) | |
1417 | continue; | |
1418 | ||
035409c3 | 1419 | if (!get_range_strlen (arg, length, visited, type, fuzzy, flexp)) |
b9833bfd | 1420 | { |
1421 | if (fuzzy) | |
1422 | *maxlen = build_all_ones_cst (size_type_node); | |
1423 | else | |
1424 | return false; | |
1425 | } | |
b9ea678c | 1426 | } |
1427 | } | |
1428 | return true; | |
1429 | ||
1430 | default: | |
1431 | return false; | |
1432 | } | |
1433 | } | |
1434 | ||
b9833bfd | 1435 | /* Determine the minimum and maximum value or string length that ARG |
1436 | refers to and store each in the first two elements of MINMAXLEN. | |
1437 | For expressions that point to strings of unknown lengths that are | |
1438 | character arrays, use the upper bound of the array as the maximum | |
1439 | length. For example, given an expression like 'x ? array : "xyz"' | |
1440 | and array declared as 'char array[8]', MINMAXLEN[0] will be set | |
1441 | to 3 and MINMAXLEN[1] to 7, the longest string that could be | |
1442 | stored in array. | |
035409c3 | 1443 | Return true if the range of the string lengths has been obtained |
1444 | from the upper bound of an array at the end of a struct. Such | |
1445 | an array may hold a string that's longer than its upper bound | |
1446 | due to it being used as a poor-man's flexible array member. */ | |
b9833bfd | 1447 | |
035409c3 | 1448 | bool |
1449 | get_range_strlen (tree arg, tree minmaxlen[2]) | |
b9833bfd | 1450 | { |
1451 | bitmap visited = NULL; | |
1452 | ||
1453 | minmaxlen[0] = NULL_TREE; | |
1454 | minmaxlen[1] = NULL_TREE; | |
1455 | ||
035409c3 | 1456 | bool flexarray = false; |
1457 | get_range_strlen (arg, minmaxlen, &visited, 1, true, &flexarray); | |
b9833bfd | 1458 | |
1459 | if (visited) | |
1460 | BITMAP_FREE (visited); | |
035409c3 | 1461 | |
1462 | return flexarray; | |
b9833bfd | 1463 | } |
1464 | ||
2ccb7428 | 1465 | tree |
1466 | get_maxval_strlen (tree arg, int type) | |
1467 | { | |
1468 | bitmap visited = NULL; | |
b9833bfd | 1469 | tree len[2] = { NULL_TREE, NULL_TREE }; |
035409c3 | 1470 | |
1471 | bool dummy; | |
1472 | if (!get_range_strlen (arg, len, &visited, type, false, &dummy)) | |
b9833bfd | 1473 | len[1] = NULL_TREE; |
2ccb7428 | 1474 | if (visited) |
1475 | BITMAP_FREE (visited); | |
1476 | ||
b9833bfd | 1477 | return len[1]; |
2ccb7428 | 1478 | } |
1479 | ||
b9ea678c | 1480 | |
1481 | /* Fold function call to builtin strcpy with arguments DEST and SRC. | |
1482 | If LEN is not NULL, it represents the length of the string to be | |
1483 | copied. Return NULL_TREE if no simplification can be made. */ | |
1484 | ||
1485 | static bool | |
1486 | gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi, | |
2ccb7428 | 1487 | tree dest, tree src) |
b9ea678c | 1488 | { |
2ccb7428 | 1489 | location_t loc = gimple_location (gsi_stmt (*gsi)); |
b9ea678c | 1490 | tree fn; |
1491 | ||
1492 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
1493 | if (operand_equal_p (src, dest, 0)) | |
1494 | { | |
1495 | replace_call_with_value (gsi, dest); | |
1496 | return true; | |
1497 | } | |
1498 | ||
1499 | if (optimize_function_for_size_p (cfun)) | |
1500 | return false; | |
1501 | ||
1502 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1503 | if (!fn) | |
1504 | return false; | |
1505 | ||
c1920e9d | 1506 | tree len = get_maxval_strlen (src, 0); |
b9ea678c | 1507 | if (!len) |
2ccb7428 | 1508 | return false; |
b9ea678c | 1509 | |
1510 | len = fold_convert_loc (loc, size_type_node, len); | |
1511 | len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1)); | |
1512 | len = force_gimple_operand_gsi (gsi, len, true, | |
1513 | NULL_TREE, true, GSI_SAME_STMT); | |
42acab1c | 1514 | gimple *repl = gimple_build_call (fn, 3, dest, src, len); |
b9ea678c | 1515 | replace_call_with_call_and_fold (gsi, repl); |
1516 | return true; | |
1517 | } | |
1518 | ||
1519 | /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN. | |
1520 | If SLEN is not NULL, it represents the length of the source string. | |
1521 | Return NULL_TREE if no simplification can be made. */ | |
1522 | ||
1523 | static bool | |
2ccb7428 | 1524 | gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi, |
1525 | tree dest, tree src, tree len) | |
b9ea678c | 1526 | { |
2ccb7428 | 1527 | location_t loc = gimple_location (gsi_stmt (*gsi)); |
b9ea678c | 1528 | tree fn; |
1529 | ||
1530 | /* If the LEN parameter is zero, return DEST. */ | |
1531 | if (integer_zerop (len)) | |
1532 | { | |
1533 | replace_call_with_value (gsi, dest); | |
1534 | return true; | |
1535 | } | |
1536 | ||
1537 | /* We can't compare slen with len as constants below if len is not a | |
1538 | constant. */ | |
2ccb7428 | 1539 | if (TREE_CODE (len) != INTEGER_CST) |
b9ea678c | 1540 | return false; |
1541 | ||
b9ea678c | 1542 | /* Now, we must be passed a constant src ptr parameter. */ |
c1920e9d | 1543 | tree slen = get_maxval_strlen (src, 0); |
2ccb7428 | 1544 | if (!slen || TREE_CODE (slen) != INTEGER_CST) |
b9ea678c | 1545 | return false; |
1546 | ||
1547 | slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1)); | |
1548 | ||
1549 | /* We do not support simplification of this case, though we do | |
1550 | support it when expanding trees into RTL. */ | |
1551 | /* FIXME: generate a call to __builtin_memset. */ | |
1552 | if (tree_int_cst_lt (slen, len)) | |
1553 | return false; | |
1554 | ||
1555 | /* OK transform into builtin memcpy. */ | |
1556 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1557 | if (!fn) | |
1558 | return false; | |
1559 | ||
1560 | len = fold_convert_loc (loc, size_type_node, len); | |
1561 | len = force_gimple_operand_gsi (gsi, len, true, | |
1562 | NULL_TREE, true, GSI_SAME_STMT); | |
42acab1c | 1563 | gimple *repl = gimple_build_call (fn, 3, dest, src, len); |
b9ea678c | 1564 | replace_call_with_call_and_fold (gsi, repl); |
1565 | return true; | |
1566 | } | |
1567 | ||
77a1a89c | 1568 | /* Fold function call to builtin strchr or strrchr. |
1569 | If both arguments are constant, evaluate and fold the result, | |
1570 | otherwise simplify str(r)chr (str, 0) into str + strlen (str). | |
85dd8d9a | 1571 | In general strlen is significantly faster than strchr |
1572 | due to being a simpler operation. */ | |
1573 | static bool | |
77a1a89c | 1574 | gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr) |
85dd8d9a | 1575 | { |
1576 | gimple *stmt = gsi_stmt (*gsi); | |
1577 | tree str = gimple_call_arg (stmt, 0); | |
1578 | tree c = gimple_call_arg (stmt, 1); | |
1579 | location_t loc = gimple_location (stmt); | |
77a1a89c | 1580 | const char *p; |
1581 | char ch; | |
85dd8d9a | 1582 | |
77a1a89c | 1583 | if (!gimple_call_lhs (stmt)) |
85dd8d9a | 1584 | return false; |
1585 | ||
77a1a89c | 1586 | if ((p = c_getstr (str)) && target_char_cst_p (c, &ch)) |
1587 | { | |
1588 | const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch); | |
1589 | ||
1590 | if (p1 == NULL) | |
1591 | { | |
1592 | replace_call_with_value (gsi, integer_zero_node); | |
1593 | return true; | |
1594 | } | |
1595 | ||
1596 | tree len = build_int_cst (size_type_node, p1 - p); | |
1597 | gimple_seq stmts = NULL; | |
1598 | gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt), | |
1599 | POINTER_PLUS_EXPR, str, len); | |
1600 | gimple_seq_add_stmt_without_update (&stmts, new_stmt); | |
1601 | gsi_replace_with_seq_vops (gsi, stmts); | |
1602 | return true; | |
1603 | } | |
1604 | ||
1605 | if (!integer_zerop (c)) | |
85dd8d9a | 1606 | return false; |
1607 | ||
77a1a89c | 1608 | /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */ |
051acc87 | 1609 | if (is_strrchr && optimize_function_for_size_p (cfun)) |
77a1a89c | 1610 | { |
1611 | tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR); | |
1612 | ||
051acc87 | 1613 | if (strchr_fn) |
77a1a89c | 1614 | { |
1615 | gimple *repl = gimple_build_call (strchr_fn, 2, str, c); | |
1616 | replace_call_with_call_and_fold (gsi, repl); | |
1617 | return true; | |
1618 | } | |
1619 | ||
1620 | return false; | |
1621 | } | |
1622 | ||
85dd8d9a | 1623 | tree len; |
1624 | tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN); | |
1625 | ||
1626 | if (!strlen_fn) | |
1627 | return false; | |
1628 | ||
1629 | /* Create newstr = strlen (str). */ | |
1630 | gimple_seq stmts = NULL; | |
1631 | gimple *new_stmt = gimple_build_call (strlen_fn, 1, str); | |
1632 | gimple_set_location (new_stmt, loc); | |
c985cfb4 | 1633 | len = create_tmp_reg_or_ssa_name (size_type_node); |
85dd8d9a | 1634 | gimple_call_set_lhs (new_stmt, len); |
1635 | gimple_seq_add_stmt_without_update (&stmts, new_stmt); | |
1636 | ||
1637 | /* Create (str p+ strlen (str)). */ | |
1638 | new_stmt = gimple_build_assign (gimple_call_lhs (stmt), | |
1639 | POINTER_PLUS_EXPR, str, len); | |
1640 | gimple_seq_add_stmt_without_update (&stmts, new_stmt); | |
1641 | gsi_replace_with_seq_vops (gsi, stmts); | |
1642 | /* gsi now points at the assignment to the lhs, get a | |
1643 | stmt iterator to the strlen. | |
1644 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
1645 | CFG isn't built yet. */ | |
1646 | gimple_stmt_iterator gsi2 = *gsi; | |
1647 | gsi_prev (&gsi2); | |
1648 | fold_stmt (&gsi2); | |
1649 | return true; | |
1650 | } | |
1651 | ||
051acc87 | 1652 | /* Fold function call to builtin strstr. |
1653 | If both arguments are constant, evaluate and fold the result, | |
1654 | additionally fold strstr (x, "") into x and strstr (x, "c") | |
1655 | into strchr (x, 'c'). */ | |
1656 | static bool | |
1657 | gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi) | |
1658 | { | |
1659 | gimple *stmt = gsi_stmt (*gsi); | |
1660 | tree haystack = gimple_call_arg (stmt, 0); | |
1661 | tree needle = gimple_call_arg (stmt, 1); | |
1662 | const char *p, *q; | |
1663 | ||
1664 | if (!gimple_call_lhs (stmt)) | |
1665 | return false; | |
1666 | ||
1667 | q = c_getstr (needle); | |
1668 | if (q == NULL) | |
1669 | return false; | |
1670 | ||
1671 | if ((p = c_getstr (haystack))) | |
1672 | { | |
1673 | const char *r = strstr (p, q); | |
1674 | ||
1675 | if (r == NULL) | |
1676 | { | |
1677 | replace_call_with_value (gsi, integer_zero_node); | |
1678 | return true; | |
1679 | } | |
1680 | ||
1681 | tree len = build_int_cst (size_type_node, r - p); | |
1682 | gimple_seq stmts = NULL; | |
1683 | gimple *new_stmt | |
1684 | = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR, | |
1685 | haystack, len); | |
1686 | gimple_seq_add_stmt_without_update (&stmts, new_stmt); | |
1687 | gsi_replace_with_seq_vops (gsi, stmts); | |
1688 | return true; | |
1689 | } | |
1690 | ||
1691 | /* For strstr (x, "") return x. */ | |
1692 | if (q[0] == '\0') | |
1693 | { | |
1694 | replace_call_with_value (gsi, haystack); | |
1695 | return true; | |
1696 | } | |
1697 | ||
1698 | /* Transform strstr (x, "c") into strchr (x, 'c'). */ | |
1699 | if (q[1] == '\0') | |
1700 | { | |
1701 | tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR); | |
1702 | if (strchr_fn) | |
1703 | { | |
1704 | tree c = build_int_cst (integer_type_node, q[0]); | |
1705 | gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c); | |
1706 | replace_call_with_call_and_fold (gsi, repl); | |
1707 | return true; | |
1708 | } | |
1709 | } | |
1710 | ||
1711 | return false; | |
1712 | } | |
1713 | ||
b9ea678c | 1714 | /* Simplify a call to the strcat builtin. DST and SRC are the arguments |
1715 | to the call. | |
1716 | ||
1717 | Return NULL_TREE if no simplification was possible, otherwise return the | |
1718 | simplified form of the call as a tree. | |
1719 | ||
1720 | The simplified form may be a constant or other expression which | |
1721 | computes the same value, but in a more efficient manner (including | |
1722 | calls to other builtin functions). | |
1723 | ||
1724 | The call may contain arguments which need to be evaluated, but | |
1725 | which are not useful to determine the result of the call. In | |
1726 | this case we return a chain of COMPOUND_EXPRs. The LHS of each | |
1727 | COMPOUND_EXPR will be an argument which must be evaluated. | |
1728 | COMPOUND_EXPRs are chained through their RHS. The RHS of the last | |
1729 | COMPOUND_EXPR in the chain will contain the tree for the simplified | |
1730 | form of the builtin function call. */ | |
1731 | ||
1732 | static bool | |
2ccb7428 | 1733 | gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src) |
b9ea678c | 1734 | { |
42acab1c | 1735 | gimple *stmt = gsi_stmt (*gsi); |
2ccb7428 | 1736 | location_t loc = gimple_location (stmt); |
b9ea678c | 1737 | |
1738 | const char *p = c_getstr (src); | |
1739 | ||
1740 | /* If the string length is zero, return the dst parameter. */ | |
1741 | if (p && *p == '\0') | |
1742 | { | |
1743 | replace_call_with_value (gsi, dst); | |
1744 | return true; | |
1745 | } | |
1746 | ||
1747 | if (!optimize_bb_for_speed_p (gimple_bb (stmt))) | |
1748 | return false; | |
1749 | ||
1750 | /* See if we can store by pieces into (dst + strlen(dst)). */ | |
1751 | tree newdst; | |
1752 | tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN); | |
1753 | tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
1754 | ||
1755 | if (!strlen_fn || !memcpy_fn) | |
1756 | return false; | |
1757 | ||
1758 | /* If the length of the source string isn't computable don't | |
1759 | split strcat into strlen and memcpy. */ | |
2ccb7428 | 1760 | tree len = get_maxval_strlen (src, 0); |
b9ea678c | 1761 | if (! len) |
b9ea678c | 1762 | return false; |
1763 | ||
1764 | /* Create strlen (dst). */ | |
1765 | gimple_seq stmts = NULL, stmts2; | |
42acab1c | 1766 | gimple *repl = gimple_build_call (strlen_fn, 1, dst); |
b9ea678c | 1767 | gimple_set_location (repl, loc); |
c985cfb4 | 1768 | newdst = create_tmp_reg_or_ssa_name (size_type_node); |
b9ea678c | 1769 | gimple_call_set_lhs (repl, newdst); |
1770 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1771 | ||
1772 | /* Create (dst p+ strlen (dst)). */ | |
1773 | newdst = fold_build_pointer_plus_loc (loc, dst, newdst); | |
1774 | newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE); | |
1775 | gimple_seq_add_seq_without_update (&stmts, stmts2); | |
1776 | ||
1777 | len = fold_convert_loc (loc, size_type_node, len); | |
1778 | len = size_binop_loc (loc, PLUS_EXPR, len, | |
1779 | build_int_cst (size_type_node, 1)); | |
1780 | len = force_gimple_operand (len, &stmts2, true, NULL_TREE); | |
1781 | gimple_seq_add_seq_without_update (&stmts, stmts2); | |
1782 | ||
1783 | repl = gimple_build_call (memcpy_fn, 3, newdst, src, len); | |
1784 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1785 | if (gimple_call_lhs (stmt)) | |
1786 | { | |
1787 | repl = gimple_build_assign (gimple_call_lhs (stmt), dst); | |
1788 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
1789 | gsi_replace_with_seq_vops (gsi, stmts); | |
1790 | /* gsi now points at the assignment to the lhs, get a | |
1791 | stmt iterator to the memcpy call. | |
1792 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
1793 | CFG isn't built yet. */ | |
1794 | gimple_stmt_iterator gsi2 = *gsi; | |
1795 | gsi_prev (&gsi2); | |
1796 | fold_stmt (&gsi2); | |
1797 | } | |
1798 | else | |
1799 | { | |
1800 | gsi_replace_with_seq_vops (gsi, stmts); | |
1801 | fold_stmt (gsi); | |
1802 | } | |
1803 | return true; | |
1804 | } | |
1805 | ||
95f5ab8d | 1806 | /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE |
1807 | are the arguments to the call. */ | |
1808 | ||
1809 | static bool | |
1810 | gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi) | |
1811 | { | |
42acab1c | 1812 | gimple *stmt = gsi_stmt (*gsi); |
95f5ab8d | 1813 | tree dest = gimple_call_arg (stmt, 0); |
1814 | tree src = gimple_call_arg (stmt, 1); | |
1815 | tree size = gimple_call_arg (stmt, 2); | |
1816 | tree fn; | |
1817 | const char *p; | |
1818 | ||
1819 | ||
1820 | p = c_getstr (src); | |
1821 | /* If the SRC parameter is "", return DEST. */ | |
1822 | if (p && *p == '\0') | |
1823 | { | |
1824 | replace_call_with_value (gsi, dest); | |
1825 | return true; | |
1826 | } | |
1827 | ||
1828 | if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size)) | |
1829 | return false; | |
1830 | ||
1831 | /* If __builtin_strcat_chk is used, assume strcat is available. */ | |
1832 | fn = builtin_decl_explicit (BUILT_IN_STRCAT); | |
1833 | if (!fn) | |
1834 | return false; | |
1835 | ||
42acab1c | 1836 | gimple *repl = gimple_build_call (fn, 2, dest, src); |
95f5ab8d | 1837 | replace_call_with_call_and_fold (gsi, repl); |
1838 | return true; | |
1839 | } | |
1840 | ||
e788f202 | 1841 | /* Simplify a call to the strncat builtin. */ |
1842 | ||
1843 | static bool | |
1844 | gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi) | |
1845 | { | |
1846 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
1847 | tree dst = gimple_call_arg (stmt, 0); | |
1848 | tree src = gimple_call_arg (stmt, 1); | |
1849 | tree len = gimple_call_arg (stmt, 2); | |
1850 | ||
1851 | const char *p = c_getstr (src); | |
1852 | ||
1853 | /* If the requested length is zero, or the src parameter string | |
1854 | length is zero, return the dst parameter. */ | |
1855 | if (integer_zerop (len) || (p && *p == '\0')) | |
1856 | { | |
1857 | replace_call_with_value (gsi, dst); | |
1858 | return true; | |
1859 | } | |
1860 | ||
1861 | /* If the requested len is greater than or equal to the string | |
1862 | length, call strcat. */ | |
1863 | if (TREE_CODE (len) == INTEGER_CST && p | |
1864 | && compare_tree_int (len, strlen (p)) >= 0) | |
1865 | { | |
1866 | tree fn = builtin_decl_implicit (BUILT_IN_STRCAT); | |
1867 | ||
1868 | /* If the replacement _DECL isn't initialized, don't do the | |
1869 | transformation. */ | |
1870 | if (!fn) | |
1871 | return false; | |
1872 | ||
1873 | gcall *repl = gimple_build_call (fn, 2, dst, src); | |
1874 | replace_call_with_call_and_fold (gsi, repl); | |
1875 | return true; | |
1876 | } | |
1877 | ||
1878 | return false; | |
1879 | } | |
1880 | ||
396b19bc | 1881 | /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC, |
1882 | LEN, and SIZE. */ | |
1883 | ||
1884 | static bool | |
1885 | gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi) | |
1886 | { | |
42acab1c | 1887 | gimple *stmt = gsi_stmt (*gsi); |
396b19bc | 1888 | tree dest = gimple_call_arg (stmt, 0); |
1889 | tree src = gimple_call_arg (stmt, 1); | |
1890 | tree len = gimple_call_arg (stmt, 2); | |
1891 | tree size = gimple_call_arg (stmt, 3); | |
1892 | tree fn; | |
1893 | const char *p; | |
1894 | ||
1895 | p = c_getstr (src); | |
1896 | /* If the SRC parameter is "" or if LEN is 0, return DEST. */ | |
1897 | if ((p && *p == '\0') | |
1898 | || integer_zerop (len)) | |
1899 | { | |
1900 | replace_call_with_value (gsi, dest); | |
1901 | return true; | |
1902 | } | |
1903 | ||
1904 | if (! tree_fits_uhwi_p (size)) | |
1905 | return false; | |
1906 | ||
1907 | if (! integer_all_onesp (size)) | |
1908 | { | |
1909 | tree src_len = c_strlen (src, 1); | |
1910 | if (src_len | |
1911 | && tree_fits_uhwi_p (src_len) | |
1912 | && tree_fits_uhwi_p (len) | |
1913 | && ! tree_int_cst_lt (len, src_len)) | |
1914 | { | |
1915 | /* If LEN >= strlen (SRC), optimize into __strcat_chk. */ | |
1916 | fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK); | |
1917 | if (!fn) | |
1918 | return false; | |
1919 | ||
42acab1c | 1920 | gimple *repl = gimple_build_call (fn, 3, dest, src, size); |
396b19bc | 1921 | replace_call_with_call_and_fold (gsi, repl); |
1922 | return true; | |
1923 | } | |
1924 | return false; | |
1925 | } | |
1926 | ||
1927 | /* If __builtin_strncat_chk is used, assume strncat is available. */ | |
1928 | fn = builtin_decl_explicit (BUILT_IN_STRNCAT); | |
1929 | if (!fn) | |
1930 | return false; | |
1931 | ||
42acab1c | 1932 | gimple *repl = gimple_build_call (fn, 3, dest, src, len); |
396b19bc | 1933 | replace_call_with_call_and_fold (gsi, repl); |
1934 | return true; | |
1935 | } | |
1936 | ||
ac8a4f8e | 1937 | /* Build and append gimple statements to STMTS that would load a first |
1938 | character of a memory location identified by STR. LOC is location | |
1939 | of the statement. */ | |
1940 | ||
1941 | static tree | |
1942 | gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts) | |
1943 | { | |
1944 | tree var; | |
1945 | ||
1946 | tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0); | |
1947 | tree cst_uchar_ptr_node | |
1948 | = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true); | |
1949 | tree off0 = build_int_cst (cst_uchar_ptr_node, 0); | |
1950 | ||
1951 | tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0); | |
1952 | gassign *stmt = gimple_build_assign (NULL_TREE, temp); | |
1953 | var = create_tmp_reg_or_ssa_name (cst_uchar_node, stmt); | |
1954 | ||
1955 | gimple_assign_set_lhs (stmt, var); | |
1956 | gimple_seq_add_stmt_without_update (stmts, stmt); | |
1957 | ||
1958 | return var; | |
1959 | } | |
1960 | ||
1961 | /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. | |
1962 | FCODE is the name of the builtin. */ | |
1963 | ||
1964 | static bool | |
1965 | gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi) | |
1966 | { | |
1967 | gimple *stmt = gsi_stmt (*gsi); | |
1968 | tree callee = gimple_call_fndecl (stmt); | |
1969 | enum built_in_function fcode = DECL_FUNCTION_CODE (callee); | |
1970 | ||
1971 | tree type = integer_type_node; | |
1972 | tree str1 = gimple_call_arg (stmt, 0); | |
1973 | tree str2 = gimple_call_arg (stmt, 1); | |
1974 | tree lhs = gimple_call_lhs (stmt); | |
1975 | HOST_WIDE_INT length = -1; | |
1976 | ||
1977 | /* Handle strncmp and strncasecmp functions. */ | |
1978 | if (gimple_call_num_args (stmt) == 3) | |
1979 | { | |
1980 | tree len = gimple_call_arg (stmt, 2); | |
1981 | if (tree_fits_uhwi_p (len)) | |
1982 | length = tree_to_uhwi (len); | |
1983 | } | |
1984 | ||
1985 | /* If the LEN parameter is zero, return zero. */ | |
1986 | if (length == 0) | |
1987 | { | |
1988 | replace_call_with_value (gsi, integer_zero_node); | |
1989 | return true; | |
1990 | } | |
1991 | ||
1992 | /* If ARG1 and ARG2 are the same (and not volatile), return zero. */ | |
1993 | if (operand_equal_p (str1, str2, 0)) | |
1994 | { | |
1995 | replace_call_with_value (gsi, integer_zero_node); | |
1996 | return true; | |
1997 | } | |
1998 | ||
1999 | const char *p1 = c_getstr (str1); | |
2000 | const char *p2 = c_getstr (str2); | |
2001 | ||
2002 | /* For known strings, return an immediate value. */ | |
2003 | if (p1 && p2) | |
2004 | { | |
2005 | int r = 0; | |
2006 | bool known_result = false; | |
2007 | ||
2008 | switch (fcode) | |
2009 | { | |
2010 | case BUILT_IN_STRCMP: | |
2011 | { | |
2012 | r = strcmp (p1, p2); | |
2013 | known_result = true; | |
2014 | break; | |
2015 | } | |
2016 | case BUILT_IN_STRNCMP: | |
2017 | { | |
2018 | if (length == -1) | |
2019 | break; | |
2020 | r = strncmp (p1, p2, length); | |
2021 | known_result = true; | |
2022 | break; | |
2023 | } | |
2024 | /* Only handleable situation is where the string are equal (result 0), | |
2025 | which is already handled by operand_equal_p case. */ | |
2026 | case BUILT_IN_STRCASECMP: | |
2027 | break; | |
2028 | case BUILT_IN_STRNCASECMP: | |
2029 | { | |
2030 | if (length == -1) | |
2031 | break; | |
2032 | r = strncmp (p1, p2, length); | |
2033 | if (r == 0) | |
2034 | known_result = true; | |
2035 | break;; | |
2036 | } | |
2037 | default: | |
2038 | gcc_unreachable (); | |
2039 | } | |
2040 | ||
2041 | if (known_result) | |
2042 | { | |
2043 | replace_call_with_value (gsi, build_cmp_result (type, r)); | |
2044 | return true; | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | bool nonzero_length = length >= 1 | |
2049 | || fcode == BUILT_IN_STRCMP | |
2050 | || fcode == BUILT_IN_STRCASECMP; | |
2051 | ||
2052 | location_t loc = gimple_location (stmt); | |
2053 | ||
2054 | /* If the second arg is "", return *(const unsigned char*)arg1. */ | |
2055 | if (p2 && *p2 == '\0' && nonzero_length) | |
2056 | { | |
2057 | gimple_seq stmts = NULL; | |
2058 | tree var = gimple_load_first_char (loc, str1, &stmts); | |
2059 | if (lhs) | |
2060 | { | |
2061 | stmt = gimple_build_assign (lhs, NOP_EXPR, var); | |
2062 | gimple_seq_add_stmt_without_update (&stmts, stmt); | |
2063 | } | |
2064 | ||
2065 | gsi_replace_with_seq_vops (gsi, stmts); | |
2066 | return true; | |
2067 | } | |
2068 | ||
2069 | /* If the first arg is "", return -*(const unsigned char*)arg2. */ | |
2070 | if (p1 && *p1 == '\0' && nonzero_length) | |
2071 | { | |
2072 | gimple_seq stmts = NULL; | |
2073 | tree var = gimple_load_first_char (loc, str2, &stmts); | |
2074 | ||
2075 | if (lhs) | |
2076 | { | |
2077 | tree c = create_tmp_reg_or_ssa_name (integer_type_node); | |
2078 | stmt = gimple_build_assign (c, NOP_EXPR, var); | |
2079 | gimple_seq_add_stmt_without_update (&stmts, stmt); | |
2080 | ||
2081 | stmt = gimple_build_assign (lhs, NEGATE_EXPR, c); | |
2082 | gimple_seq_add_stmt_without_update (&stmts, stmt); | |
2083 | } | |
2084 | ||
2085 | gsi_replace_with_seq_vops (gsi, stmts); | |
2086 | return true; | |
2087 | } | |
2088 | ||
2089 | /* If len parameter is one, return an expression corresponding to | |
2090 | (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */ | |
2091 | if (fcode == BUILT_IN_STRNCMP && length == 1) | |
2092 | { | |
2093 | gimple_seq stmts = NULL; | |
2094 | tree temp1 = gimple_load_first_char (loc, str1, &stmts); | |
2095 | tree temp2 = gimple_load_first_char (loc, str2, &stmts); | |
2096 | ||
2097 | if (lhs) | |
2098 | { | |
2099 | tree c1 = create_tmp_reg_or_ssa_name (integer_type_node); | |
2100 | gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1); | |
2101 | gimple_seq_add_stmt_without_update (&stmts, convert1); | |
2102 | ||
2103 | tree c2 = create_tmp_reg_or_ssa_name (integer_type_node); | |
2104 | gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2); | |
2105 | gimple_seq_add_stmt_without_update (&stmts, convert2); | |
2106 | ||
2107 | stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2); | |
2108 | gimple_seq_add_stmt_without_update (&stmts, stmt); | |
2109 | } | |
2110 | ||
2111 | gsi_replace_with_seq_vops (gsi, stmts); | |
2112 | return true; | |
2113 | } | |
2114 | ||
2115 | return false; | |
2116 | } | |
2117 | ||
507a998e | 2118 | /* Fold a call to the memchr pointed by GSI iterator. */ |
2119 | ||
2120 | static bool | |
2121 | gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi) | |
2122 | { | |
2123 | gimple *stmt = gsi_stmt (*gsi); | |
2124 | tree lhs = gimple_call_lhs (stmt); | |
2125 | tree arg1 = gimple_call_arg (stmt, 0); | |
2126 | tree arg2 = gimple_call_arg (stmt, 1); | |
2127 | tree len = gimple_call_arg (stmt, 2); | |
2128 | ||
2129 | /* If the LEN parameter is zero, return zero. */ | |
2130 | if (integer_zerop (len)) | |
2131 | { | |
2132 | replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0)); | |
2133 | return true; | |
2134 | } | |
2135 | ||
2136 | char c; | |
2137 | if (TREE_CODE (arg2) != INTEGER_CST | |
2138 | || !tree_fits_uhwi_p (len) | |
2139 | || !target_char_cst_p (arg2, &c)) | |
2140 | return false; | |
2141 | ||
2142 | unsigned HOST_WIDE_INT length = tree_to_uhwi (len); | |
2143 | unsigned HOST_WIDE_INT string_length; | |
2144 | const char *p1 = c_getstr (arg1, &string_length); | |
2145 | ||
2146 | if (p1) | |
2147 | { | |
2148 | const char *r = (const char *)memchr (p1, c, MIN (length, string_length)); | |
2149 | if (r == NULL) | |
2150 | { | |
2151 | if (length <= string_length) | |
2152 | { | |
2153 | replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0)); | |
2154 | return true; | |
2155 | } | |
2156 | } | |
2157 | else | |
2158 | { | |
2159 | unsigned HOST_WIDE_INT offset = r - p1; | |
2160 | gimple_seq stmts = NULL; | |
2161 | if (lhs != NULL_TREE) | |
2162 | { | |
2163 | tree offset_cst = build_int_cst (TREE_TYPE (len), offset); | |
2164 | gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR, | |
2165 | arg1, offset_cst); | |
2166 | gimple_seq_add_stmt_without_update (&stmts, stmt); | |
2167 | } | |
2168 | else | |
2169 | gimple_seq_add_stmt_without_update (&stmts, | |
2170 | gimple_build_nop ()); | |
2171 | ||
2172 | gsi_replace_with_seq_vops (gsi, stmts); | |
2173 | return true; | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | return false; | |
2178 | } | |
ac8a4f8e | 2179 | |
b9ea678c | 2180 | /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments |
2181 | to the call. IGNORE is true if the value returned | |
2182 | by the builtin will be ignored. UNLOCKED is true is true if this | |
2183 | actually a call to fputs_unlocked. If LEN in non-NULL, it represents | |
2184 | the known length of the string. Return NULL_TREE if no simplification | |
2185 | was possible. */ | |
2186 | ||
2187 | static bool | |
2188 | gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi, | |
b9ea678c | 2189 | tree arg0, tree arg1, |
2ccb7428 | 2190 | bool unlocked) |
b9ea678c | 2191 | { |
42acab1c | 2192 | gimple *stmt = gsi_stmt (*gsi); |
2ccb7428 | 2193 | |
b9ea678c | 2194 | /* If we're using an unlocked function, assume the other unlocked |
2195 | functions exist explicitly. */ | |
2196 | tree const fn_fputc = (unlocked | |
2197 | ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED) | |
2198 | : builtin_decl_implicit (BUILT_IN_FPUTC)); | |
2199 | tree const fn_fwrite = (unlocked | |
2200 | ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED) | |
2201 | : builtin_decl_implicit (BUILT_IN_FWRITE)); | |
2202 | ||
2203 | /* If the return value is used, don't do the transformation. */ | |
2ccb7428 | 2204 | if (gimple_call_lhs (stmt)) |
b9ea678c | 2205 | return false; |
2206 | ||
b9ea678c | 2207 | /* Get the length of the string passed to fputs. If the length |
2208 | can't be determined, punt. */ | |
2ccb7428 | 2209 | tree len = get_maxval_strlen (arg0, 0); |
b9ea678c | 2210 | if (!len |
2211 | || TREE_CODE (len) != INTEGER_CST) | |
2212 | return false; | |
2213 | ||
2214 | switch (compare_tree_int (len, 1)) | |
2215 | { | |
2216 | case -1: /* length is 0, delete the call entirely . */ | |
2217 | replace_call_with_value (gsi, integer_zero_node); | |
2218 | return true; | |
2219 | ||
2220 | case 0: /* length is 1, call fputc. */ | |
2221 | { | |
2222 | const char *p = c_getstr (arg0); | |
2223 | if (p != NULL) | |
2224 | { | |
2225 | if (!fn_fputc) | |
2226 | return false; | |
2227 | ||
42acab1c | 2228 | gimple *repl = gimple_build_call (fn_fputc, 2, |
b9ea678c | 2229 | build_int_cst |
2230 | (integer_type_node, p[0]), arg1); | |
2231 | replace_call_with_call_and_fold (gsi, repl); | |
2232 | return true; | |
2233 | } | |
2234 | } | |
2235 | /* FALLTHROUGH */ | |
2236 | case 1: /* length is greater than 1, call fwrite. */ | |
2237 | { | |
2238 | /* If optimizing for size keep fputs. */ | |
2239 | if (optimize_function_for_size_p (cfun)) | |
2240 | return false; | |
2241 | /* New argument list transforming fputs(string, stream) to | |
2242 | fwrite(string, 1, len, stream). */ | |
2243 | if (!fn_fwrite) | |
2244 | return false; | |
2245 | ||
42acab1c | 2246 | gimple *repl = gimple_build_call (fn_fwrite, 4, arg0, |
b9ea678c | 2247 | size_one_node, len, arg1); |
2248 | replace_call_with_call_and_fold (gsi, repl); | |
2249 | return true; | |
2250 | } | |
2251 | default: | |
2252 | gcc_unreachable (); | |
2253 | } | |
2254 | return false; | |
2255 | } | |
2256 | ||
2257 | /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin. | |
2258 | DEST, SRC, LEN, and SIZE are the arguments to the call. | |
2259 | IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_* | |
2260 | code of the builtin. If MAXLEN is not NULL, it is maximum length | |
2261 | passed as third argument. */ | |
2262 | ||
2263 | static bool | |
2264 | gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, | |
b9ea678c | 2265 | tree dest, tree src, tree len, tree size, |
b9ea678c | 2266 | enum built_in_function fcode) |
2267 | { | |
42acab1c | 2268 | gimple *stmt = gsi_stmt (*gsi); |
2ccb7428 | 2269 | location_t loc = gimple_location (stmt); |
2270 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
b9ea678c | 2271 | tree fn; |
2272 | ||
2273 | /* If SRC and DEST are the same (and not volatile), return DEST | |
2274 | (resp. DEST+LEN for __mempcpy_chk). */ | |
2275 | if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) | |
2276 | { | |
2277 | if (fcode != BUILT_IN_MEMPCPY_CHK) | |
2278 | { | |
2279 | replace_call_with_value (gsi, dest); | |
2280 | return true; | |
2281 | } | |
2282 | else | |
2283 | { | |
cba8396b | 2284 | gimple_seq stmts = NULL; |
2285 | len = gimple_convert_to_ptrofftype (&stmts, loc, len); | |
c437d1e8 | 2286 | tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR, |
2287 | TREE_TYPE (dest), dest, len); | |
cba8396b | 2288 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); |
b9ea678c | 2289 | replace_call_with_value (gsi, temp); |
2290 | return true; | |
2291 | } | |
2292 | } | |
2293 | ||
2294 | if (! tree_fits_uhwi_p (size)) | |
2295 | return false; | |
2296 | ||
2ccb7428 | 2297 | tree maxlen = get_maxval_strlen (len, 2); |
b9ea678c | 2298 | if (! integer_all_onesp (size)) |
2299 | { | |
2300 | if (! tree_fits_uhwi_p (len)) | |
2301 | { | |
2302 | /* If LEN is not constant, try MAXLEN too. | |
2303 | For MAXLEN only allow optimizing into non-_ocs function | |
2304 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2305 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
2306 | { | |
2307 | if (fcode == BUILT_IN_MEMPCPY_CHK && ignore) | |
2308 | { | |
2309 | /* (void) __mempcpy_chk () can be optimized into | |
2310 | (void) __memcpy_chk (). */ | |
2311 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); | |
2312 | if (!fn) | |
2313 | return false; | |
2314 | ||
42acab1c | 2315 | gimple *repl = gimple_build_call (fn, 4, dest, src, len, size); |
b9ea678c | 2316 | replace_call_with_call_and_fold (gsi, repl); |
2317 | return true; | |
2318 | } | |
2319 | return false; | |
2320 | } | |
2321 | } | |
2322 | else | |
2323 | maxlen = len; | |
2324 | ||
2325 | if (tree_int_cst_lt (size, maxlen)) | |
2326 | return false; | |
2327 | } | |
2328 | ||
2329 | fn = NULL_TREE; | |
2330 | /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume | |
2331 | mem{cpy,pcpy,move,set} is available. */ | |
2332 | switch (fcode) | |
2333 | { | |
2334 | case BUILT_IN_MEMCPY_CHK: | |
2335 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY); | |
2336 | break; | |
2337 | case BUILT_IN_MEMPCPY_CHK: | |
2338 | fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); | |
2339 | break; | |
2340 | case BUILT_IN_MEMMOVE_CHK: | |
2341 | fn = builtin_decl_explicit (BUILT_IN_MEMMOVE); | |
2342 | break; | |
2343 | case BUILT_IN_MEMSET_CHK: | |
2344 | fn = builtin_decl_explicit (BUILT_IN_MEMSET); | |
2345 | break; | |
2346 | default: | |
2347 | break; | |
2348 | } | |
2349 | ||
2350 | if (!fn) | |
2351 | return false; | |
2352 | ||
42acab1c | 2353 | gimple *repl = gimple_build_call (fn, 3, dest, src, len); |
b9ea678c | 2354 | replace_call_with_call_and_fold (gsi, repl); |
2355 | return true; | |
2356 | } | |
2357 | ||
2358 | /* Fold a call to the __st[rp]cpy_chk builtin. | |
2359 | DEST, SRC, and SIZE are the arguments to the call. | |
2360 | IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_* | |
2361 | code of the builtin. If MAXLEN is not NULL, it is maximum length of | |
2362 | strings passed as second argument. */ | |
2363 | ||
2364 | static bool | |
2365 | gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, | |
2ccb7428 | 2366 | tree dest, |
b9ea678c | 2367 | tree src, tree size, |
b9ea678c | 2368 | enum built_in_function fcode) |
2369 | { | |
42acab1c | 2370 | gimple *stmt = gsi_stmt (*gsi); |
2ccb7428 | 2371 | location_t loc = gimple_location (stmt); |
2372 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; | |
b9ea678c | 2373 | tree len, fn; |
2374 | ||
2375 | /* If SRC and DEST are the same (and not volatile), return DEST. */ | |
2376 | if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) | |
2377 | { | |
2378 | replace_call_with_value (gsi, dest); | |
2379 | return true; | |
2380 | } | |
2381 | ||
2382 | if (! tree_fits_uhwi_p (size)) | |
2383 | return false; | |
2384 | ||
2ccb7428 | 2385 | tree maxlen = get_maxval_strlen (src, 1); |
b9ea678c | 2386 | if (! integer_all_onesp (size)) |
2387 | { | |
2388 | len = c_strlen (src, 1); | |
2389 | if (! len || ! tree_fits_uhwi_p (len)) | |
2390 | { | |
2391 | /* If LEN is not constant, try MAXLEN too. | |
2392 | For MAXLEN only allow optimizing into non-_ocs function | |
2393 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2394 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
2395 | { | |
2396 | if (fcode == BUILT_IN_STPCPY_CHK) | |
2397 | { | |
2398 | if (! ignore) | |
2399 | return false; | |
2400 | ||
2401 | /* If return value of __stpcpy_chk is ignored, | |
2402 | optimize into __strcpy_chk. */ | |
2403 | fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK); | |
2404 | if (!fn) | |
2405 | return false; | |
2406 | ||
42acab1c | 2407 | gimple *repl = gimple_build_call (fn, 3, dest, src, size); |
b9ea678c | 2408 | replace_call_with_call_and_fold (gsi, repl); |
2409 | return true; | |
2410 | } | |
2411 | ||
2412 | if (! len || TREE_SIDE_EFFECTS (len)) | |
2413 | return false; | |
2414 | ||
2415 | /* If c_strlen returned something, but not a constant, | |
2416 | transform __strcpy_chk into __memcpy_chk. */ | |
2417 | fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK); | |
2418 | if (!fn) | |
2419 | return false; | |
2420 | ||
cba8396b | 2421 | gimple_seq stmts = NULL; |
2422 | len = gimple_convert (&stmts, loc, size_type_node, len); | |
2423 | len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len, | |
2424 | build_int_cst (size_type_node, 1)); | |
2425 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); | |
42acab1c | 2426 | gimple *repl = gimple_build_call (fn, 4, dest, src, len, size); |
b9ea678c | 2427 | replace_call_with_call_and_fold (gsi, repl); |
2428 | return true; | |
2429 | } | |
34e73149 | 2430 | } |
b9ea678c | 2431 | else |
2432 | maxlen = len; | |
2433 | ||
2434 | if (! tree_int_cst_lt (maxlen, size)) | |
2435 | return false; | |
34e73149 | 2436 | } |
2437 | ||
b9ea678c | 2438 | /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */ |
2439 | fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK | |
2440 | ? BUILT_IN_STPCPY : BUILT_IN_STRCPY); | |
2441 | if (!fn) | |
2442 | return false; | |
2443 | ||
42acab1c | 2444 | gimple *repl = gimple_build_call (fn, 2, dest, src); |
b9ea678c | 2445 | replace_call_with_call_and_fold (gsi, repl); |
2446 | return true; | |
2447 | } | |
2448 | ||
2449 | /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE | |
2450 | are the arguments to the call. If MAXLEN is not NULL, it is maximum | |
2451 | length passed as third argument. IGNORE is true if return value can be | |
2452 | ignored. FCODE is the BUILT_IN_* code of the builtin. */ | |
2453 | ||
2454 | static bool | |
2455 | gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi, | |
2456 | tree dest, tree src, | |
2ccb7428 | 2457 | tree len, tree size, |
b9ea678c | 2458 | enum built_in_function fcode) |
2459 | { | |
42acab1c | 2460 | gimple *stmt = gsi_stmt (*gsi); |
2ccb7428 | 2461 | bool ignore = gimple_call_lhs (stmt) == NULL_TREE; |
b9ea678c | 2462 | tree fn; |
2463 | ||
2464 | if (fcode == BUILT_IN_STPNCPY_CHK && ignore) | |
2d18b16d | 2465 | { |
b9ea678c | 2466 | /* If return value of __stpncpy_chk is ignored, |
2467 | optimize into __strncpy_chk. */ | |
2468 | fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK); | |
2469 | if (fn) | |
2470 | { | |
42acab1c | 2471 | gimple *repl = gimple_build_call (fn, 4, dest, src, len, size); |
b9ea678c | 2472 | replace_call_with_call_and_fold (gsi, repl); |
2473 | return true; | |
2474 | } | |
2d18b16d | 2475 | } |
2476 | ||
b9ea678c | 2477 | if (! tree_fits_uhwi_p (size)) |
2478 | return false; | |
2479 | ||
2ccb7428 | 2480 | tree maxlen = get_maxval_strlen (len, 2); |
b9ea678c | 2481 | if (! integer_all_onesp (size)) |
2d18b16d | 2482 | { |
b9ea678c | 2483 | if (! tree_fits_uhwi_p (len)) |
3aadae2d | 2484 | { |
b9ea678c | 2485 | /* If LEN is not constant, try MAXLEN too. |
2486 | For MAXLEN only allow optimizing into non-_ocs function | |
2487 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2488 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
2489 | return false; | |
78a0527a | 2490 | } |
b9ea678c | 2491 | else |
2492 | maxlen = len; | |
2493 | ||
2494 | if (tree_int_cst_lt (size, maxlen)) | |
2495 | return false; | |
2d18b16d | 2496 | } |
2497 | ||
b9ea678c | 2498 | /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */ |
2499 | fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK | |
2500 | ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY); | |
2501 | if (!fn) | |
2502 | return false; | |
2503 | ||
42acab1c | 2504 | gimple *repl = gimple_build_call (fn, 3, dest, src, len); |
b9ea678c | 2505 | replace_call_with_call_and_fold (gsi, repl); |
2506 | return true; | |
2d18b16d | 2507 | } |
2508 | ||
e80cc485 | 2509 | /* Fold function call to builtin stpcpy with arguments DEST and SRC. |
2510 | Return NULL_TREE if no simplification can be made. */ | |
2511 | ||
2512 | static bool | |
2513 | gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi) | |
2514 | { | |
2515 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
2516 | location_t loc = gimple_location (stmt); | |
2517 | tree dest = gimple_call_arg (stmt, 0); | |
2518 | tree src = gimple_call_arg (stmt, 1); | |
2519 | tree fn, len, lenp1; | |
2520 | ||
2521 | /* If the result is unused, replace stpcpy with strcpy. */ | |
2522 | if (gimple_call_lhs (stmt) == NULL_TREE) | |
2523 | { | |
2524 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2525 | if (!fn) | |
2526 | return false; | |
2527 | gimple_call_set_fndecl (stmt, fn); | |
2528 | fold_stmt (gsi); | |
2529 | return true; | |
2530 | } | |
2531 | ||
2532 | len = c_strlen (src, 1); | |
2533 | if (!len | |
2534 | || TREE_CODE (len) != INTEGER_CST) | |
2535 | return false; | |
2536 | ||
2537 | if (optimize_function_for_size_p (cfun) | |
2538 | /* If length is zero it's small enough. */ | |
2539 | && !integer_zerop (len)) | |
2540 | return false; | |
2541 | ||
2542 | /* If the source has a known length replace stpcpy with memcpy. */ | |
2543 | fn = builtin_decl_implicit (BUILT_IN_MEMCPY); | |
2544 | if (!fn) | |
2545 | return false; | |
2546 | ||
2547 | gimple_seq stmts = NULL; | |
2548 | tree tem = gimple_convert (&stmts, loc, size_type_node, len); | |
2549 | lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, | |
2550 | tem, build_int_cst (size_type_node, 1)); | |
2551 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); | |
2552 | gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1); | |
2553 | gimple_set_vuse (repl, gimple_vuse (stmt)); | |
2554 | gimple_set_vdef (repl, gimple_vdef (stmt)); | |
2555 | if (gimple_vdef (repl) | |
2556 | && TREE_CODE (gimple_vdef (repl)) == SSA_NAME) | |
2557 | SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl; | |
2558 | gsi_insert_before (gsi, repl, GSI_SAME_STMT); | |
2559 | /* Replace the result with dest + len. */ | |
2560 | stmts = NULL; | |
2561 | tem = gimple_convert (&stmts, loc, sizetype, len); | |
2562 | gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); | |
2563 | gassign *ret = gimple_build_assign (gimple_call_lhs (stmt), | |
2564 | POINTER_PLUS_EXPR, dest, tem); | |
cfe45b40 | 2565 | gsi_replace (gsi, ret, false); |
e80cc485 | 2566 | /* Finally fold the memcpy call. */ |
2567 | gimple_stmt_iterator gsi2 = *gsi; | |
2568 | gsi_prev (&gsi2); | |
2569 | fold_stmt (&gsi2); | |
2570 | return true; | |
2571 | } | |
2572 | ||
b9ea678c | 2573 | /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return |
2574 | NULL_TREE if a normal call should be emitted rather than expanding | |
2575 | the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or | |
2576 | BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length | |
2577 | passed as second argument. */ | |
2d18b16d | 2578 | |
2579 | static bool | |
b9ea678c | 2580 | gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi, |
2ccb7428 | 2581 | enum built_in_function fcode) |
2d18b16d | 2582 | { |
1a91d914 | 2583 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
b9ea678c | 2584 | tree dest, size, len, fn, fmt, flag; |
2585 | const char *fmt_str; | |
2d18b16d | 2586 | |
b9ea678c | 2587 | /* Verify the required arguments in the original call. */ |
2588 | if (gimple_call_num_args (stmt) < 5) | |
2589 | return false; | |
2d18b16d | 2590 | |
b9ea678c | 2591 | dest = gimple_call_arg (stmt, 0); |
2592 | len = gimple_call_arg (stmt, 1); | |
2593 | flag = gimple_call_arg (stmt, 2); | |
2594 | size = gimple_call_arg (stmt, 3); | |
2595 | fmt = gimple_call_arg (stmt, 4); | |
2596 | ||
2597 | if (! tree_fits_uhwi_p (size)) | |
2598 | return false; | |
2599 | ||
2600 | if (! integer_all_onesp (size)) | |
2601 | { | |
2ccb7428 | 2602 | tree maxlen = get_maxval_strlen (len, 2); |
b9ea678c | 2603 | if (! tree_fits_uhwi_p (len)) |
2d18b16d | 2604 | { |
b9ea678c | 2605 | /* If LEN is not constant, try MAXLEN too. |
2606 | For MAXLEN only allow optimizing into non-_ocs function | |
2607 | if SIZE is >= MAXLEN, never convert to __ocs_fail (). */ | |
2608 | if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen)) | |
2d18b16d | 2609 | return false; |
2610 | } | |
2611 | else | |
b9ea678c | 2612 | maxlen = len; |
2d18b16d | 2613 | |
b9ea678c | 2614 | if (tree_int_cst_lt (size, maxlen)) |
2615 | return false; | |
2616 | } | |
2d18b16d | 2617 | |
b9ea678c | 2618 | if (!init_target_chars ()) |
2619 | return false; | |
2d18b16d | 2620 | |
b9ea678c | 2621 | /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0 |
2622 | or if format doesn't contain % chars or is "%s". */ | |
2623 | if (! integer_zerop (flag)) | |
2624 | { | |
2625 | fmt_str = c_getstr (fmt); | |
2626 | if (fmt_str == NULL) | |
2627 | return false; | |
2628 | if (strchr (fmt_str, target_percent) != NULL | |
2629 | && strcmp (fmt_str, target_percent_s)) | |
2630 | return false; | |
2d18b16d | 2631 | } |
2632 | ||
b9ea678c | 2633 | /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is |
2634 | available. */ | |
2635 | fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK | |
2636 | ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF); | |
2637 | if (!fn) | |
1a06b503 | 2638 | return false; |
2639 | ||
b9ea678c | 2640 | /* Replace the called function and the first 5 argument by 3 retaining |
2641 | trailing varargs. */ | |
2642 | gimple_call_set_fndecl (stmt, fn); | |
2643 | gimple_call_set_fntype (stmt, TREE_TYPE (fn)); | |
2644 | gimple_call_set_arg (stmt, 0, dest); | |
2645 | gimple_call_set_arg (stmt, 1, len); | |
2646 | gimple_call_set_arg (stmt, 2, fmt); | |
2647 | for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i) | |
2648 | gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2)); | |
2649 | gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2); | |
2650 | fold_stmt (gsi); | |
2651 | return true; | |
2652 | } | |
2d18b16d | 2653 | |
b9ea678c | 2654 | /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS. |
2655 | Return NULL_TREE if a normal call should be emitted rather than | |
2656 | expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK | |
2657 | or BUILT_IN_VSPRINTF_CHK. */ | |
2d18b16d | 2658 | |
b9ea678c | 2659 | static bool |
2660 | gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi, | |
2661 | enum built_in_function fcode) | |
2662 | { | |
1a91d914 | 2663 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
b9ea678c | 2664 | tree dest, size, len, fn, fmt, flag; |
2665 | const char *fmt_str; | |
2666 | unsigned nargs = gimple_call_num_args (stmt); | |
2d18b16d | 2667 | |
b9ea678c | 2668 | /* Verify the required arguments in the original call. */ |
2669 | if (nargs < 4) | |
2670 | return false; | |
2671 | dest = gimple_call_arg (stmt, 0); | |
2672 | flag = gimple_call_arg (stmt, 1); | |
2673 | size = gimple_call_arg (stmt, 2); | |
2674 | fmt = gimple_call_arg (stmt, 3); | |
2675 | ||
2676 | if (! tree_fits_uhwi_p (size)) | |
2677 | return false; | |
2678 | ||
2679 | len = NULL_TREE; | |
2680 | ||
2681 | if (!init_target_chars ()) | |
2682 | return false; | |
2683 | ||
2684 | /* Check whether the format is a literal string constant. */ | |
2685 | fmt_str = c_getstr (fmt); | |
2686 | if (fmt_str != NULL) | |
2687 | { | |
2688 | /* If the format doesn't contain % args or %%, we know the size. */ | |
2689 | if (strchr (fmt_str, target_percent) == 0) | |
2d18b16d | 2690 | { |
b9ea678c | 2691 | if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4) |
2692 | len = build_int_cstu (size_type_node, strlen (fmt_str)); | |
2693 | } | |
2694 | /* If the format is "%s" and first ... argument is a string literal, | |
2695 | we know the size too. */ | |
2696 | else if (fcode == BUILT_IN_SPRINTF_CHK | |
2697 | && strcmp (fmt_str, target_percent_s) == 0) | |
2698 | { | |
2699 | tree arg; | |
2d18b16d | 2700 | |
b9ea678c | 2701 | if (nargs == 5) |
2702 | { | |
2703 | arg = gimple_call_arg (stmt, 4); | |
2704 | if (POINTER_TYPE_P (TREE_TYPE (arg))) | |
2705 | { | |
2706 | len = c_strlen (arg, 1); | |
2707 | if (! len || ! tree_fits_uhwi_p (len)) | |
2708 | len = NULL_TREE; | |
2709 | } | |
2710 | } | |
2711 | } | |
2712 | } | |
2d18b16d | 2713 | |
b9ea678c | 2714 | if (! integer_all_onesp (size)) |
2715 | { | |
2716 | if (! len || ! tree_int_cst_lt (len, size)) | |
2717 | return false; | |
2718 | } | |
2d18b16d | 2719 | |
b9ea678c | 2720 | /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0 |
2721 | or if format doesn't contain % chars or is "%s". */ | |
2722 | if (! integer_zerop (flag)) | |
2723 | { | |
2724 | if (fmt_str == NULL) | |
2725 | return false; | |
2726 | if (strchr (fmt_str, target_percent) != NULL | |
2727 | && strcmp (fmt_str, target_percent_s)) | |
2728 | return false; | |
2729 | } | |
2d18b16d | 2730 | |
b9ea678c | 2731 | /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */ |
2732 | fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK | |
2733 | ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF); | |
2734 | if (!fn) | |
2735 | return false; | |
2736 | ||
2737 | /* Replace the called function and the first 4 argument by 2 retaining | |
2738 | trailing varargs. */ | |
2739 | gimple_call_set_fndecl (stmt, fn); | |
2740 | gimple_call_set_fntype (stmt, TREE_TYPE (fn)); | |
2741 | gimple_call_set_arg (stmt, 0, dest); | |
2742 | gimple_call_set_arg (stmt, 1, fmt); | |
2743 | for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i) | |
2744 | gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2)); | |
2745 | gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2); | |
2746 | fold_stmt (gsi); | |
2747 | return true; | |
2748 | } | |
2749 | ||
1888f376 | 2750 | /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG. |
2751 | ORIG may be null if this is a 2-argument call. We don't attempt to | |
2752 | simplify calls with more than 3 arguments. | |
2753 | ||
53e0530a | 2754 | Return true if simplification was possible, otherwise false. */ |
1888f376 | 2755 | |
53e0530a | 2756 | bool |
2ccb7428 | 2757 | gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi) |
1888f376 | 2758 | { |
42acab1c | 2759 | gimple *stmt = gsi_stmt (*gsi); |
1888f376 | 2760 | tree dest = gimple_call_arg (stmt, 0); |
2761 | tree fmt = gimple_call_arg (stmt, 1); | |
2762 | tree orig = NULL_TREE; | |
2763 | const char *fmt_str = NULL; | |
2764 | ||
2765 | /* Verify the required arguments in the original call. We deal with two | |
2766 | types of sprintf() calls: 'sprintf (str, fmt)' and | |
2767 | 'sprintf (dest, "%s", orig)'. */ | |
2768 | if (gimple_call_num_args (stmt) > 3) | |
2769 | return false; | |
2770 | ||
2771 | if (gimple_call_num_args (stmt) == 3) | |
2772 | orig = gimple_call_arg (stmt, 2); | |
2773 | ||
2774 | /* Check whether the format is a literal string constant. */ | |
2775 | fmt_str = c_getstr (fmt); | |
2776 | if (fmt_str == NULL) | |
2777 | return false; | |
2778 | ||
2779 | if (!init_target_chars ()) | |
2780 | return false; | |
2781 | ||
2782 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2783 | if (strchr (fmt_str, target_percent) == NULL) | |
2784 | { | |
2785 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2786 | ||
2787 | if (!fn) | |
2788 | return false; | |
2789 | ||
2790 | /* Don't optimize sprintf (buf, "abc", ptr++). */ | |
2791 | if (orig) | |
2792 | return false; | |
2793 | ||
2794 | /* Convert sprintf (str, fmt) into strcpy (str, fmt) when | |
2795 | 'format' is known to contain no % formats. */ | |
2796 | gimple_seq stmts = NULL; | |
42acab1c | 2797 | gimple *repl = gimple_build_call (fn, 2, dest, fmt); |
1888f376 | 2798 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2799 | if (gimple_call_lhs (stmt)) | |
2800 | { | |
2801 | repl = gimple_build_assign (gimple_call_lhs (stmt), | |
2802 | build_int_cst (integer_type_node, | |
2803 | strlen (fmt_str))); | |
2804 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2805 | gsi_replace_with_seq_vops (gsi, stmts); | |
2806 | /* gsi now points at the assignment to the lhs, get a | |
2807 | stmt iterator to the memcpy call. | |
2808 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2809 | CFG isn't built yet. */ | |
2810 | gimple_stmt_iterator gsi2 = *gsi; | |
2811 | gsi_prev (&gsi2); | |
2812 | fold_stmt (&gsi2); | |
2813 | } | |
2814 | else | |
2815 | { | |
2816 | gsi_replace_with_seq_vops (gsi, stmts); | |
2817 | fold_stmt (gsi); | |
2818 | } | |
2819 | return true; | |
2820 | } | |
2821 | ||
2822 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
2823 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) | |
2824 | { | |
2825 | tree fn; | |
2826 | fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2827 | ||
2828 | if (!fn) | |
2829 | return false; | |
2830 | ||
2831 | /* Don't crash on sprintf (str1, "%s"). */ | |
2832 | if (!orig) | |
2833 | return false; | |
2834 | ||
2ccb7428 | 2835 | tree orig_len = NULL_TREE; |
2836 | if (gimple_call_lhs (stmt)) | |
1888f376 | 2837 | { |
2ccb7428 | 2838 | orig_len = get_maxval_strlen (orig, 0); |
95e631b8 | 2839 | if (!orig_len) |
1888f376 | 2840 | return false; |
2841 | } | |
2842 | ||
2843 | /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */ | |
2844 | gimple_seq stmts = NULL; | |
42acab1c | 2845 | gimple *repl = gimple_build_call (fn, 2, dest, orig); |
1888f376 | 2846 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2847 | if (gimple_call_lhs (stmt)) | |
2848 | { | |
95e631b8 | 2849 | if (!useless_type_conversion_p (integer_type_node, |
2850 | TREE_TYPE (orig_len))) | |
2851 | orig_len = fold_convert (integer_type_node, orig_len); | |
2852 | repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); | |
1888f376 | 2853 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2854 | gsi_replace_with_seq_vops (gsi, stmts); | |
2855 | /* gsi now points at the assignment to the lhs, get a | |
2856 | stmt iterator to the memcpy call. | |
2857 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2858 | CFG isn't built yet. */ | |
2859 | gimple_stmt_iterator gsi2 = *gsi; | |
2860 | gsi_prev (&gsi2); | |
2861 | fold_stmt (&gsi2); | |
2862 | } | |
2863 | else | |
2864 | { | |
2865 | gsi_replace_with_seq_vops (gsi, stmts); | |
2866 | fold_stmt (gsi); | |
2867 | } | |
2868 | return true; | |
2869 | } | |
2870 | return false; | |
2871 | } | |
2872 | ||
95e631b8 | 2873 | /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE, |
2874 | FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't | |
2875 | attempt to simplify calls with more than 4 arguments. | |
1888f376 | 2876 | |
53e0530a | 2877 | Return true if simplification was possible, otherwise false. */ |
95e631b8 | 2878 | |
53e0530a | 2879 | bool |
2ccb7428 | 2880 | gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi) |
95e631b8 | 2881 | { |
1a91d914 | 2882 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
95e631b8 | 2883 | tree dest = gimple_call_arg (stmt, 0); |
2884 | tree destsize = gimple_call_arg (stmt, 1); | |
2885 | tree fmt = gimple_call_arg (stmt, 2); | |
2886 | tree orig = NULL_TREE; | |
2887 | const char *fmt_str = NULL; | |
2888 | ||
2889 | if (gimple_call_num_args (stmt) > 4) | |
2890 | return false; | |
2891 | ||
2892 | if (gimple_call_num_args (stmt) == 4) | |
2893 | orig = gimple_call_arg (stmt, 3); | |
2894 | ||
2895 | if (!tree_fits_uhwi_p (destsize)) | |
2896 | return false; | |
2897 | unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize); | |
2898 | ||
2899 | /* Check whether the format is a literal string constant. */ | |
2900 | fmt_str = c_getstr (fmt); | |
2901 | if (fmt_str == NULL) | |
2902 | return false; | |
2903 | ||
2904 | if (!init_target_chars ()) | |
2905 | return false; | |
2906 | ||
2907 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
2908 | if (strchr (fmt_str, target_percent) == NULL) | |
2909 | { | |
2910 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2911 | if (!fn) | |
2912 | return false; | |
2913 | ||
2914 | /* Don't optimize snprintf (buf, 4, "abc", ptr++). */ | |
2915 | if (orig) | |
2916 | return false; | |
2917 | ||
2918 | /* We could expand this as | |
2919 | memcpy (str, fmt, cst - 1); str[cst - 1] = '\0'; | |
2920 | or to | |
2921 | memcpy (str, fmt_with_nul_at_cstm1, cst); | |
2922 | but in the former case that might increase code size | |
2923 | and in the latter case grow .rodata section too much. | |
2924 | So punt for now. */ | |
2925 | size_t len = strlen (fmt_str); | |
2926 | if (len >= destlen) | |
2927 | return false; | |
2928 | ||
2929 | gimple_seq stmts = NULL; | |
42acab1c | 2930 | gimple *repl = gimple_build_call (fn, 2, dest, fmt); |
95e631b8 | 2931 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2932 | if (gimple_call_lhs (stmt)) | |
2933 | { | |
2934 | repl = gimple_build_assign (gimple_call_lhs (stmt), | |
2935 | build_int_cst (integer_type_node, len)); | |
2936 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2937 | gsi_replace_with_seq_vops (gsi, stmts); | |
2938 | /* gsi now points at the assignment to the lhs, get a | |
2939 | stmt iterator to the memcpy call. | |
2940 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2941 | CFG isn't built yet. */ | |
2942 | gimple_stmt_iterator gsi2 = *gsi; | |
2943 | gsi_prev (&gsi2); | |
2944 | fold_stmt (&gsi2); | |
2945 | } | |
2946 | else | |
2947 | { | |
2948 | gsi_replace_with_seq_vops (gsi, stmts); | |
2949 | fold_stmt (gsi); | |
2950 | } | |
2951 | return true; | |
2952 | } | |
2953 | ||
2954 | /* If the format is "%s", use strcpy if the result isn't used. */ | |
2955 | else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0) | |
2956 | { | |
2957 | tree fn = builtin_decl_implicit (BUILT_IN_STRCPY); | |
2958 | if (!fn) | |
2959 | return false; | |
2960 | ||
2961 | /* Don't crash on snprintf (str1, cst, "%s"). */ | |
2962 | if (!orig) | |
2963 | return false; | |
2964 | ||
2ccb7428 | 2965 | tree orig_len = get_maxval_strlen (orig, 0); |
baf8dfde | 2966 | if (!orig_len || TREE_CODE (orig_len) != INTEGER_CST) |
2ccb7428 | 2967 | return false; |
95e631b8 | 2968 | |
2969 | /* We could expand this as | |
2970 | memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0'; | |
2971 | or to | |
2972 | memcpy (str1, str2_with_nul_at_cstm1, cst); | |
2973 | but in the former case that might increase code size | |
2974 | and in the latter case grow .rodata section too much. | |
2975 | So punt for now. */ | |
2976 | if (compare_tree_int (orig_len, destlen) >= 0) | |
2977 | return false; | |
2978 | ||
2979 | /* Convert snprintf (str1, cst, "%s", str2) into | |
2980 | strcpy (str1, str2) if strlen (str2) < cst. */ | |
2981 | gimple_seq stmts = NULL; | |
42acab1c | 2982 | gimple *repl = gimple_build_call (fn, 2, dest, orig); |
95e631b8 | 2983 | gimple_seq_add_stmt_without_update (&stmts, repl); |
2984 | if (gimple_call_lhs (stmt)) | |
2985 | { | |
2986 | if (!useless_type_conversion_p (integer_type_node, | |
2987 | TREE_TYPE (orig_len))) | |
2988 | orig_len = fold_convert (integer_type_node, orig_len); | |
2989 | repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len); | |
2990 | gimple_seq_add_stmt_without_update (&stmts, repl); | |
2991 | gsi_replace_with_seq_vops (gsi, stmts); | |
2992 | /* gsi now points at the assignment to the lhs, get a | |
2993 | stmt iterator to the memcpy call. | |
2994 | ??? We can't use gsi_for_stmt as that doesn't work when the | |
2995 | CFG isn't built yet. */ | |
2996 | gimple_stmt_iterator gsi2 = *gsi; | |
2997 | gsi_prev (&gsi2); | |
2998 | fold_stmt (&gsi2); | |
2999 | } | |
3000 | else | |
3001 | { | |
3002 | gsi_replace_with_seq_vops (gsi, stmts); | |
3003 | fold_stmt (gsi); | |
3004 | } | |
3005 | return true; | |
3006 | } | |
3007 | return false; | |
3008 | } | |
1888f376 | 3009 | |
aea88c77 | 3010 | /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins. |
3011 | FP, FMT, and ARG are the arguments to the call. We don't fold calls with | |
3012 | more than 3 arguments, and ARG may be null in the 2-argument case. | |
3013 | ||
3014 | Return NULL_TREE if no simplification was possible, otherwise return the | |
3015 | simplified form of the call as a tree. FCODE is the BUILT_IN_* | |
3016 | code of the function to be simplified. */ | |
3017 | ||
3018 | static bool | |
3019 | gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi, | |
3020 | tree fp, tree fmt, tree arg, | |
3021 | enum built_in_function fcode) | |
3022 | { | |
3023 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
3024 | tree fn_fputc, fn_fputs; | |
3025 | const char *fmt_str = NULL; | |
3026 | ||
3027 | /* If the return value is used, don't do the transformation. */ | |
3028 | if (gimple_call_lhs (stmt) != NULL_TREE) | |
3029 | return false; | |
3030 | ||
3031 | /* Check whether the format is a literal string constant. */ | |
3032 | fmt_str = c_getstr (fmt); | |
3033 | if (fmt_str == NULL) | |
3034 | return false; | |
3035 | ||
3036 | if (fcode == BUILT_IN_FPRINTF_UNLOCKED) | |
3037 | { | |
3038 | /* If we're using an unlocked function, assume the other | |
3039 | unlocked functions exist explicitly. */ | |
3040 | fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED); | |
3041 | fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED); | |
3042 | } | |
3043 | else | |
3044 | { | |
3045 | fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC); | |
3046 | fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS); | |
3047 | } | |
3048 | ||
3049 | if (!init_target_chars ()) | |
3050 | return false; | |
3051 | ||
3052 | /* If the format doesn't contain % args or %%, use strcpy. */ | |
3053 | if (strchr (fmt_str, target_percent) == NULL) | |
3054 | { | |
3055 | if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK | |
3056 | && arg) | |
3057 | return false; | |
3058 | ||
3059 | /* If the format specifier was "", fprintf does nothing. */ | |
3060 | if (fmt_str[0] == '\0') | |
3061 | { | |
3062 | replace_call_with_value (gsi, NULL_TREE); | |
3063 | return true; | |
3064 | } | |
3065 | ||
3066 | /* When "string" doesn't contain %, replace all cases of | |
3067 | fprintf (fp, string) with fputs (string, fp). The fputs | |
3068 | builtin will take care of special cases like length == 1. */ | |
3069 | if (fn_fputs) | |
3070 | { | |
3071 | gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp); | |
3072 | replace_call_with_call_and_fold (gsi, repl); | |
3073 | return true; | |
3074 | } | |
3075 | } | |
3076 | ||
3077 | /* The other optimizations can be done only on the non-va_list variants. */ | |
3078 | else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK) | |
3079 | return false; | |
3080 | ||
3081 | /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */ | |
3082 | else if (strcmp (fmt_str, target_percent_s) == 0) | |
3083 | { | |
3084 | if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg))) | |
3085 | return false; | |
3086 | if (fn_fputs) | |
3087 | { | |
3088 | gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp); | |
3089 | replace_call_with_call_and_fold (gsi, repl); | |
3090 | return true; | |
3091 | } | |
3092 | } | |
3093 | ||
3094 | /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */ | |
3095 | else if (strcmp (fmt_str, target_percent_c) == 0) | |
3096 | { | |
3097 | if (!arg | |
3098 | || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg))) | |
3099 | return false; | |
3100 | if (fn_fputc) | |
3101 | { | |
3102 | gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp); | |
3103 | replace_call_with_call_and_fold (gsi, repl); | |
3104 | return true; | |
3105 | } | |
3106 | } | |
3107 | ||
3108 | return false; | |
3109 | } | |
3110 | ||
e788f202 | 3111 | /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins. |
3112 | FMT and ARG are the arguments to the call; we don't fold cases with | |
3113 | more than 2 arguments, and ARG may be null if this is a 1-argument case. | |
3114 | ||
3115 | Return NULL_TREE if no simplification was possible, otherwise return the | |
3116 | simplified form of the call as a tree. FCODE is the BUILT_IN_* | |
3117 | code of the function to be simplified. */ | |
3118 | ||
3119 | static bool | |
3120 | gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt, | |
3121 | tree arg, enum built_in_function fcode) | |
3122 | { | |
3123 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); | |
3124 | tree fn_putchar, fn_puts, newarg; | |
3125 | const char *fmt_str = NULL; | |
3126 | ||
3127 | /* If the return value is used, don't do the transformation. */ | |
3128 | if (gimple_call_lhs (stmt) != NULL_TREE) | |
3129 | return false; | |
3130 | ||
3131 | /* Check whether the format is a literal string constant. */ | |
3132 | fmt_str = c_getstr (fmt); | |
3133 | if (fmt_str == NULL) | |
3134 | return false; | |
3135 | ||
3136 | if (fcode == BUILT_IN_PRINTF_UNLOCKED) | |
3137 | { | |
3138 | /* If we're using an unlocked function, assume the other | |
3139 | unlocked functions exist explicitly. */ | |
3140 | fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED); | |
3141 | fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED); | |
3142 | } | |
3143 | else | |
3144 | { | |
3145 | fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR); | |
3146 | fn_puts = builtin_decl_implicit (BUILT_IN_PUTS); | |
3147 | } | |
3148 | ||
3149 | if (!init_target_chars ()) | |
3150 | return false; | |
3151 | ||
3152 | if (strcmp (fmt_str, target_percent_s) == 0 | |
3153 | || strchr (fmt_str, target_percent) == NULL) | |
3154 | { | |
3155 | const char *str; | |
3156 | ||
3157 | if (strcmp (fmt_str, target_percent_s) == 0) | |
3158 | { | |
3159 | if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
3160 | return false; | |
3161 | ||
3162 | if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg))) | |
3163 | return false; | |
3164 | ||
3165 | str = c_getstr (arg); | |
3166 | if (str == NULL) | |
3167 | return false; | |
3168 | } | |
3169 | else | |
3170 | { | |
3171 | /* The format specifier doesn't contain any '%' characters. */ | |
3172 | if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK | |
3173 | && arg) | |
3174 | return false; | |
3175 | str = fmt_str; | |
3176 | } | |
3177 | ||
3178 | /* If the string was "", printf does nothing. */ | |
3179 | if (str[0] == '\0') | |
3180 | { | |
3181 | replace_call_with_value (gsi, NULL_TREE); | |
3182 | return true; | |
3183 | } | |
3184 | ||
3185 | /* If the string has length of 1, call putchar. */ | |
3186 | if (str[1] == '\0') | |
3187 | { | |
3188 | /* Given printf("c"), (where c is any one character,) | |
3189 | convert "c"[0] to an int and pass that to the replacement | |
3190 | function. */ | |
3191 | newarg = build_int_cst (integer_type_node, str[0]); | |
3192 | if (fn_putchar) | |
3193 | { | |
3194 | gcall *repl = gimple_build_call (fn_putchar, 1, newarg); | |
3195 | replace_call_with_call_and_fold (gsi, repl); | |
3196 | return true; | |
3197 | } | |
3198 | } | |
3199 | else | |
3200 | { | |
3201 | /* If the string was "string\n", call puts("string"). */ | |
3202 | size_t len = strlen (str); | |
3203 | if ((unsigned char)str[len - 1] == target_newline | |
3204 | && (size_t) (int) len == len | |
3205 | && (int) len > 0) | |
3206 | { | |
3207 | char *newstr; | |
3208 | tree offset_node, string_cst; | |
3209 | ||
3210 | /* Create a NUL-terminated string that's one char shorter | |
3211 | than the original, stripping off the trailing '\n'. */ | |
3212 | newarg = build_string_literal (len, str); | |
3213 | string_cst = string_constant (newarg, &offset_node); | |
3214 | gcc_checking_assert (string_cst | |
3215 | && (TREE_STRING_LENGTH (string_cst) | |
3216 | == (int) len) | |
3217 | && integer_zerop (offset_node) | |
3218 | && (unsigned char) | |
3219 | TREE_STRING_POINTER (string_cst)[len - 1] | |
3220 | == target_newline); | |
3221 | /* build_string_literal creates a new STRING_CST, | |
3222 | modify it in place to avoid double copying. */ | |
3223 | newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst)); | |
3224 | newstr[len - 1] = '\0'; | |
3225 | if (fn_puts) | |
3226 | { | |
3227 | gcall *repl = gimple_build_call (fn_puts, 1, newarg); | |
3228 | replace_call_with_call_and_fold (gsi, repl); | |
3229 | return true; | |
3230 | } | |
3231 | } | |
3232 | else | |
3233 | /* We'd like to arrange to call fputs(string,stdout) here, | |
3234 | but we need stdout and don't have a way to get it yet. */ | |
3235 | return false; | |
3236 | } | |
3237 | } | |
3238 | ||
3239 | /* The other optimizations can be done only on the non-va_list variants. */ | |
3240 | else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK) | |
3241 | return false; | |
3242 | ||
3243 | /* If the format specifier was "%s\n", call __builtin_puts(arg). */ | |
3244 | else if (strcmp (fmt_str, target_percent_s_newline) == 0) | |
3245 | { | |
3246 | if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg))) | |
3247 | return false; | |
3248 | if (fn_puts) | |
3249 | { | |
3250 | gcall *repl = gimple_build_call (fn_puts, 1, arg); | |
3251 | replace_call_with_call_and_fold (gsi, repl); | |
3252 | return true; | |
3253 | } | |
3254 | } | |
3255 | ||
3256 | /* If the format specifier was "%c", call __builtin_putchar(arg). */ | |
3257 | else if (strcmp (fmt_str, target_percent_c) == 0) | |
3258 | { | |
3259 | if (!arg || ! useless_type_conversion_p (integer_type_node, | |
3260 | TREE_TYPE (arg))) | |
3261 | return false; | |
3262 | if (fn_putchar) | |
3263 | { | |
3264 | gcall *repl = gimple_build_call (fn_putchar, 1, arg); | |
3265 | replace_call_with_call_and_fold (gsi, repl); | |
3266 | return true; | |
3267 | } | |
3268 | } | |
3269 | ||
3270 | return false; | |
3271 | } | |
3272 | ||
aea88c77 | 3273 | |
b9ea678c | 3274 | |
3275 | /* Fold a call to __builtin_strlen with known length LEN. */ | |
3276 | ||
3277 | static bool | |
2ccb7428 | 3278 | gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi) |
b9ea678c | 3279 | { |
42acab1c | 3280 | gimple *stmt = gsi_stmt (*gsi); |
2ccb7428 | 3281 | tree len = get_maxval_strlen (gimple_call_arg (stmt, 0), 0); |
b9ea678c | 3282 | if (!len) |
3283 | return false; | |
7f96f652 | 3284 | len = force_gimple_operand_gsi (gsi, len, true, NULL, true, GSI_SAME_STMT); |
b9ea678c | 3285 | replace_call_with_value (gsi, len); |
3286 | return true; | |
2d18b16d | 3287 | } |
3288 | ||
7e3a76de | 3289 | /* Fold a call to __builtin_acc_on_device. */ |
3290 | ||
3291 | static bool | |
3292 | gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0) | |
3293 | { | |
3294 | /* Defer folding until we know which compiler we're in. */ | |
3295 | if (symtab->state != EXPANSION) | |
3296 | return false; | |
3297 | ||
3298 | unsigned val_host = GOMP_DEVICE_HOST; | |
3299 | unsigned val_dev = GOMP_DEVICE_NONE; | |
3300 | ||
3301 | #ifdef ACCEL_COMPILER | |
3302 | val_host = GOMP_DEVICE_NOT_HOST; | |
3303 | val_dev = ACCEL_COMPILER_acc_device; | |
3304 | #endif | |
3305 | ||
3306 | location_t loc = gimple_location (gsi_stmt (*gsi)); | |
3307 | ||
3308 | tree host_eq = make_ssa_name (boolean_type_node); | |
3309 | gimple *host_ass = gimple_build_assign | |
3310 | (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host)); | |
3311 | gimple_set_location (host_ass, loc); | |
3312 | gsi_insert_before (gsi, host_ass, GSI_SAME_STMT); | |
3313 | ||
3314 | tree dev_eq = make_ssa_name (boolean_type_node); | |
3315 | gimple *dev_ass = gimple_build_assign | |
3316 | (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev)); | |
3317 | gimple_set_location (dev_ass, loc); | |
3318 | gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT); | |
3319 | ||
3320 | tree result = make_ssa_name (boolean_type_node); | |
3321 | gimple *result_ass = gimple_build_assign | |
3322 | (result, BIT_IOR_EXPR, host_eq, dev_eq); | |
3323 | gimple_set_location (result_ass, loc); | |
3324 | gsi_insert_before (gsi, result_ass, GSI_SAME_STMT); | |
3325 | ||
3326 | replace_call_with_value (gsi, result); | |
3327 | ||
3328 | return true; | |
3329 | } | |
2d18b16d | 3330 | |
2c98f972 | 3331 | /* Fold realloc (0, n) -> malloc (n). */ |
3332 | ||
3333 | static bool | |
3334 | gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi) | |
3335 | { | |
3336 | gimple *stmt = gsi_stmt (*gsi); | |
3337 | tree arg = gimple_call_arg (stmt, 0); | |
3338 | tree size = gimple_call_arg (stmt, 1); | |
3339 | ||
3340 | if (operand_equal_p (arg, null_pointer_node, 0)) | |
3341 | { | |
3342 | tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC); | |
3343 | if (fn_malloc) | |
3344 | { | |
3345 | gcall *repl = gimple_build_call (fn_malloc, 1, size); | |
3346 | replace_call_with_call_and_fold (gsi, repl); | |
3347 | return true; | |
3348 | } | |
3349 | } | |
3350 | return false; | |
3351 | } | |
3352 | ||
2ccb7428 | 3353 | /* Fold the non-target builtin at *GSI and return whether any simplification |
3354 | was made. */ | |
2d18b16d | 3355 | |
b9ea678c | 3356 | static bool |
2ccb7428 | 3357 | gimple_fold_builtin (gimple_stmt_iterator *gsi) |
2d18b16d | 3358 | { |
1a91d914 | 3359 | gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi)); |
b9ea678c | 3360 | tree callee = gimple_call_fndecl (stmt); |
2d18b16d | 3361 | |
2ccb7428 | 3362 | /* Give up for always_inline inline builtins until they are |
3363 | inlined. */ | |
3364 | if (avoid_folding_inline_builtin (callee)) | |
3365 | return false; | |
2d18b16d | 3366 | |
aea88c77 | 3367 | unsigned n = gimple_call_num_args (stmt); |
3368 | enum built_in_function fcode = DECL_FUNCTION_CODE (callee); | |
3369 | switch (fcode) | |
2d18b16d | 3370 | { |
9f5f711c | 3371 | case BUILT_IN_BCMP: |
3372 | return gimple_fold_builtin_bcmp (gsi); | |
3373 | case BUILT_IN_BCOPY: | |
3374 | return gimple_fold_builtin_bcopy (gsi); | |
2ccb7428 | 3375 | case BUILT_IN_BZERO: |
9f5f711c | 3376 | return gimple_fold_builtin_bzero (gsi); |
3377 | ||
2ccb7428 | 3378 | case BUILT_IN_MEMSET: |
3379 | return gimple_fold_builtin_memset (gsi, | |
3380 | gimple_call_arg (stmt, 1), | |
3381 | gimple_call_arg (stmt, 2)); | |
2ccb7428 | 3382 | case BUILT_IN_MEMCPY: |
3383 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
3384 | gimple_call_arg (stmt, 1), 0); | |
3385 | case BUILT_IN_MEMPCPY: | |
3386 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
3387 | gimple_call_arg (stmt, 1), 1); | |
3388 | case BUILT_IN_MEMMOVE: | |
3389 | return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), | |
3390 | gimple_call_arg (stmt, 1), 3); | |
3391 | case BUILT_IN_SPRINTF_CHK: | |
3392 | case BUILT_IN_VSPRINTF_CHK: | |
aea88c77 | 3393 | return gimple_fold_builtin_sprintf_chk (gsi, fcode); |
2ccb7428 | 3394 | case BUILT_IN_STRCAT_CHK: |
3395 | return gimple_fold_builtin_strcat_chk (gsi); | |
396b19bc | 3396 | case BUILT_IN_STRNCAT_CHK: |
3397 | return gimple_fold_builtin_strncat_chk (gsi); | |
2d18b16d | 3398 | case BUILT_IN_STRLEN: |
2ccb7428 | 3399 | return gimple_fold_builtin_strlen (gsi); |
2d18b16d | 3400 | case BUILT_IN_STRCPY: |
2ccb7428 | 3401 | return gimple_fold_builtin_strcpy (gsi, |
b9ea678c | 3402 | gimple_call_arg (stmt, 0), |
2ccb7428 | 3403 | gimple_call_arg (stmt, 1)); |
2d18b16d | 3404 | case BUILT_IN_STRNCPY: |
2ccb7428 | 3405 | return gimple_fold_builtin_strncpy (gsi, |
b9ea678c | 3406 | gimple_call_arg (stmt, 0), |
3407 | gimple_call_arg (stmt, 1), | |
2ccb7428 | 3408 | gimple_call_arg (stmt, 2)); |
b65555c1 | 3409 | case BUILT_IN_STRCAT: |
2ccb7428 | 3410 | return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0), |
3411 | gimple_call_arg (stmt, 1)); | |
e788f202 | 3412 | case BUILT_IN_STRNCAT: |
3413 | return gimple_fold_builtin_strncat (gsi); | |
77a1a89c | 3414 | case BUILT_IN_INDEX: |
85dd8d9a | 3415 | case BUILT_IN_STRCHR: |
77a1a89c | 3416 | return gimple_fold_builtin_strchr (gsi, false); |
3417 | case BUILT_IN_RINDEX: | |
3418 | case BUILT_IN_STRRCHR: | |
3419 | return gimple_fold_builtin_strchr (gsi, true); | |
051acc87 | 3420 | case BUILT_IN_STRSTR: |
3421 | return gimple_fold_builtin_strstr (gsi); | |
ac8a4f8e | 3422 | case BUILT_IN_STRCMP: |
3423 | case BUILT_IN_STRCASECMP: | |
3424 | case BUILT_IN_STRNCMP: | |
3425 | case BUILT_IN_STRNCASECMP: | |
3426 | return gimple_fold_builtin_string_compare (gsi); | |
507a998e | 3427 | case BUILT_IN_MEMCHR: |
3428 | return gimple_fold_builtin_memchr (gsi); | |
2d18b16d | 3429 | case BUILT_IN_FPUTS: |
2ccb7428 | 3430 | return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0), |
3431 | gimple_call_arg (stmt, 1), false); | |
2d18b16d | 3432 | case BUILT_IN_FPUTS_UNLOCKED: |
2ccb7428 | 3433 | return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0), |
3434 | gimple_call_arg (stmt, 1), true); | |
2d18b16d | 3435 | case BUILT_IN_MEMCPY_CHK: |
3436 | case BUILT_IN_MEMPCPY_CHK: | |
3437 | case BUILT_IN_MEMMOVE_CHK: | |
3438 | case BUILT_IN_MEMSET_CHK: | |
2ccb7428 | 3439 | return gimple_fold_builtin_memory_chk (gsi, |
b9ea678c | 3440 | gimple_call_arg (stmt, 0), |
3441 | gimple_call_arg (stmt, 1), | |
3442 | gimple_call_arg (stmt, 2), | |
3443 | gimple_call_arg (stmt, 3), | |
aea88c77 | 3444 | fcode); |
e80cc485 | 3445 | case BUILT_IN_STPCPY: |
3446 | return gimple_fold_builtin_stpcpy (gsi); | |
2d18b16d | 3447 | case BUILT_IN_STRCPY_CHK: |
3448 | case BUILT_IN_STPCPY_CHK: | |
2ccb7428 | 3449 | return gimple_fold_builtin_stxcpy_chk (gsi, |
b9ea678c | 3450 | gimple_call_arg (stmt, 0), |
3451 | gimple_call_arg (stmt, 1), | |
3452 | gimple_call_arg (stmt, 2), | |
aea88c77 | 3453 | fcode); |
2d18b16d | 3454 | case BUILT_IN_STRNCPY_CHK: |
1063acde | 3455 | case BUILT_IN_STPNCPY_CHK: |
b9ea678c | 3456 | return gimple_fold_builtin_stxncpy_chk (gsi, |
3457 | gimple_call_arg (stmt, 0), | |
3458 | gimple_call_arg (stmt, 1), | |
3459 | gimple_call_arg (stmt, 2), | |
3460 | gimple_call_arg (stmt, 3), | |
aea88c77 | 3461 | fcode); |
2d18b16d | 3462 | case BUILT_IN_SNPRINTF_CHK: |
3463 | case BUILT_IN_VSNPRINTF_CHK: | |
aea88c77 | 3464 | return gimple_fold_builtin_snprintf_chk (gsi, fcode); |
53e0530a | 3465 | |
aea88c77 | 3466 | case BUILT_IN_FPRINTF: |
3467 | case BUILT_IN_FPRINTF_UNLOCKED: | |
3468 | case BUILT_IN_VFPRINTF: | |
3469 | if (n == 2 || n == 3) | |
3470 | return gimple_fold_builtin_fprintf (gsi, | |
3471 | gimple_call_arg (stmt, 0), | |
3472 | gimple_call_arg (stmt, 1), | |
3473 | n == 3 | |
3474 | ? gimple_call_arg (stmt, 2) | |
3475 | : NULL_TREE, | |
3476 | fcode); | |
3477 | break; | |
3478 | case BUILT_IN_FPRINTF_CHK: | |
3479 | case BUILT_IN_VFPRINTF_CHK: | |
3480 | if (n == 3 || n == 4) | |
3481 | return gimple_fold_builtin_fprintf (gsi, | |
3482 | gimple_call_arg (stmt, 0), | |
3483 | gimple_call_arg (stmt, 2), | |
3484 | n == 4 | |
3485 | ? gimple_call_arg (stmt, 3) | |
3486 | : NULL_TREE, | |
3487 | fcode); | |
3488 | break; | |
e788f202 | 3489 | case BUILT_IN_PRINTF: |
3490 | case BUILT_IN_PRINTF_UNLOCKED: | |
3491 | case BUILT_IN_VPRINTF: | |
3492 | if (n == 1 || n == 2) | |
3493 | return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0), | |
3494 | n == 2 | |
3495 | ? gimple_call_arg (stmt, 1) | |
3496 | : NULL_TREE, fcode); | |
3497 | break; | |
3498 | case BUILT_IN_PRINTF_CHK: | |
3499 | case BUILT_IN_VPRINTF_CHK: | |
3500 | if (n == 2 || n == 3) | |
3501 | return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1), | |
3502 | n == 3 | |
3503 | ? gimple_call_arg (stmt, 2) | |
3504 | : NULL_TREE, fcode); | |
7e18bd45 | 3505 | break; |
7e3a76de | 3506 | case BUILT_IN_ACC_ON_DEVICE: |
3507 | return gimple_fold_builtin_acc_on_device (gsi, | |
3508 | gimple_call_arg (stmt, 0)); | |
2c98f972 | 3509 | case BUILT_IN_REALLOC: |
3510 | return gimple_fold_builtin_realloc (gsi); | |
3511 | ||
b9ea678c | 3512 | default:; |
3513 | } | |
3514 | ||
3515 | /* Try the generic builtin folder. */ | |
3516 | bool ignore = (gimple_call_lhs (stmt) == NULL); | |
3517 | tree result = fold_call_stmt (stmt, ignore); | |
3518 | if (result) | |
3519 | { | |
3520 | if (ignore) | |
3521 | STRIP_NOPS (result); | |
3522 | else | |
3523 | result = fold_convert (gimple_call_return_type (stmt), result); | |
3524 | if (!update_call_from_tree (gsi, result)) | |
3525 | gimplify_and_update_call_from_tree (gsi, result); | |
3526 | return true; | |
3527 | } | |
3528 | ||
3529 | return false; | |
3530 | } | |
3531 | ||
b0ccb4e7 | 3532 | /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal |
3533 | function calls to constants, where possible. */ | |
3534 | ||
3535 | static tree | |
3536 | fold_internal_goacc_dim (const gimple *call) | |
3537 | { | |
4954efd4 | 3538 | int axis = oacc_get_ifn_dim_arg (call); |
3539 | int size = oacc_get_fn_dim_size (current_function_decl, axis); | |
b0ccb4e7 | 3540 | bool is_pos = gimple_call_internal_fn (call) == IFN_GOACC_DIM_POS; |
3541 | tree result = NULL_TREE; | |
3542 | ||
3543 | /* If the size is 1, or we only want the size and it is not dynamic, | |
3544 | we know the answer. */ | |
3545 | if (size == 1 || (!is_pos && size)) | |
3546 | { | |
3547 | tree type = TREE_TYPE (gimple_call_lhs (call)); | |
3548 | result = build_int_cst (type, size - is_pos); | |
3549 | } | |
3550 | ||
3551 | return result; | |
3552 | } | |
3553 | ||
5a5ef659 | 3554 | /* Return true if stmt is __atomic_compare_exchange_N call which is suitable |
3555 | for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is | |
3556 | &var where var is only addressable because of such calls. */ | |
3557 | ||
3558 | bool | |
3559 | optimize_atomic_compare_exchange_p (gimple *stmt) | |
3560 | { | |
3561 | if (gimple_call_num_args (stmt) != 6 | |
3562 | || !flag_inline_atomics | |
3563 | || !optimize | |
9917317a | 3564 | || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS) |
5a5ef659 | 3565 | || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL) |
3566 | || !gimple_vdef (stmt) | |
3567 | || !gimple_vuse (stmt)) | |
3568 | return false; | |
3569 | ||
3570 | tree fndecl = gimple_call_fndecl (stmt); | |
3571 | switch (DECL_FUNCTION_CODE (fndecl)) | |
3572 | { | |
3573 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1: | |
3574 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2: | |
3575 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4: | |
3576 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8: | |
3577 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16: | |
3578 | break; | |
3579 | default: | |
3580 | return false; | |
3581 | } | |
3582 | ||
3583 | tree expected = gimple_call_arg (stmt, 1); | |
3584 | if (TREE_CODE (expected) != ADDR_EXPR | |
1e12a5c8 | 3585 | || !SSA_VAR_P (TREE_OPERAND (expected, 0))) |
3586 | return false; | |
3587 | ||
3588 | tree etype = TREE_TYPE (TREE_OPERAND (expected, 0)); | |
3589 | if (!is_gimple_reg_type (etype) | |
5a5ef659 | 3590 | || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl) |
1e12a5c8 | 3591 | || TREE_THIS_VOLATILE (etype) |
3592 | || VECTOR_TYPE_P (etype) | |
3593 | || TREE_CODE (etype) == COMPLEX_TYPE | |
3594 | /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs | |
3595 | might not preserve all the bits. See PR71716. */ | |
3596 | || SCALAR_FLOAT_TYPE_P (etype) | |
3597 | || TYPE_PRECISION (etype) != GET_MODE_BITSIZE (TYPE_MODE (etype))) | |
5a5ef659 | 3598 | return false; |
3599 | ||
3600 | tree weak = gimple_call_arg (stmt, 3); | |
3601 | if (!integer_zerop (weak) && !integer_onep (weak)) | |
3602 | return false; | |
3603 | ||
3604 | tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
3605 | tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt))); | |
3606 | machine_mode mode = TYPE_MODE (itype); | |
3607 | ||
3608 | if (direct_optab_handler (atomic_compare_and_swap_optab, mode) | |
3609 | == CODE_FOR_nothing | |
3610 | && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing) | |
3611 | return false; | |
3612 | ||
1e12a5c8 | 3613 | if (int_size_in_bytes (etype) != GET_MODE_SIZE (mode)) |
5a5ef659 | 3614 | return false; |
3615 | ||
3616 | return true; | |
3617 | } | |
3618 | ||
3619 | /* Fold | |
3620 | r = __atomic_compare_exchange_N (p, &e, d, w, s, f); | |
3621 | into | |
3622 | _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f); | |
3623 | i = IMAGPART_EXPR <t>; | |
3624 | r = (_Bool) i; | |
3625 | e = REALPART_EXPR <t>; */ | |
3626 | ||
3627 | void | |
3628 | fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi) | |
3629 | { | |
3630 | gimple *stmt = gsi_stmt (*gsi); | |
3631 | tree fndecl = gimple_call_fndecl (stmt); | |
3632 | tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
3633 | tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt))); | |
3634 | tree ctype = build_complex_type (itype); | |
3635 | tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0); | |
c35e53b1 | 3636 | bool throws = false; |
3637 | edge e = NULL; | |
5a5ef659 | 3638 | gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)), |
3639 | expected); | |
3640 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
3641 | gimple_stmt_iterator gsiret = gsi_for_stmt (g); | |
3642 | if (!useless_type_conversion_p (itype, TREE_TYPE (expected))) | |
3643 | { | |
3644 | g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR, | |
3645 | build1 (VIEW_CONVERT_EXPR, itype, | |
3646 | gimple_assign_lhs (g))); | |
3647 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
3648 | } | |
3649 | int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0) | |
3650 | + int_size_in_bytes (itype); | |
3651 | g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6, | |
3652 | gimple_call_arg (stmt, 0), | |
3653 | gimple_assign_lhs (g), | |
3654 | gimple_call_arg (stmt, 2), | |
3655 | build_int_cst (integer_type_node, flag), | |
3656 | gimple_call_arg (stmt, 4), | |
3657 | gimple_call_arg (stmt, 5)); | |
3658 | tree lhs = make_ssa_name (ctype); | |
3659 | gimple_call_set_lhs (g, lhs); | |
3660 | gimple_set_vdef (g, gimple_vdef (stmt)); | |
3661 | gimple_set_vuse (g, gimple_vuse (stmt)); | |
3662 | SSA_NAME_DEF_STMT (gimple_vdef (g)) = g; | |
c35e53b1 | 3663 | tree oldlhs = gimple_call_lhs (stmt); |
3664 | if (stmt_can_throw_internal (stmt)) | |
3665 | { | |
3666 | throws = true; | |
3667 | e = find_fallthru_edge (gsi_bb (*gsi)->succs); | |
3668 | } | |
3669 | gimple_call_set_nothrow (as_a <gcall *> (g), | |
3670 | gimple_call_nothrow_p (as_a <gcall *> (stmt))); | |
3671 | gimple_call_set_lhs (stmt, NULL_TREE); | |
3672 | gsi_replace (gsi, g, true); | |
3673 | if (oldlhs) | |
5a5ef659 | 3674 | { |
5a5ef659 | 3675 | g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR, |
3676 | build1 (IMAGPART_EXPR, itype, lhs)); | |
c35e53b1 | 3677 | if (throws) |
3678 | { | |
3679 | gsi_insert_on_edge_immediate (e, g); | |
3680 | *gsi = gsi_for_stmt (g); | |
3681 | } | |
3682 | else | |
3683 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
3684 | g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g)); | |
3685 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
5a5ef659 | 3686 | } |
5a5ef659 | 3687 | g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR, |
3688 | build1 (REALPART_EXPR, itype, lhs)); | |
c35e53b1 | 3689 | if (throws && oldlhs == NULL_TREE) |
3690 | { | |
3691 | gsi_insert_on_edge_immediate (e, g); | |
3692 | *gsi = gsi_for_stmt (g); | |
3693 | } | |
3694 | else | |
3695 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
5a5ef659 | 3696 | if (!useless_type_conversion_p (TREE_TYPE (expected), itype)) |
3697 | { | |
3698 | g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)), | |
3699 | VIEW_CONVERT_EXPR, | |
3700 | build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected), | |
3701 | gimple_assign_lhs (g))); | |
3702 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
3703 | } | |
3704 | g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g)); | |
3705 | gsi_insert_after (gsi, g, GSI_NEW_STMT); | |
3706 | *gsi = gsiret; | |
3707 | } | |
3708 | ||
0c93c8a9 | 3709 | /* Return true if ARG0 CODE ARG1 in infinite signed precision operation |
3710 | doesn't fit into TYPE. The test for overflow should be regardless of | |
3711 | -fwrapv, and even for unsigned types. */ | |
3712 | ||
3713 | bool | |
3714 | arith_overflowed_p (enum tree_code code, const_tree type, | |
3715 | const_tree arg0, const_tree arg1) | |
3716 | { | |
3717 | typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int; | |
3718 | typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > | |
3719 | widest2_int_cst; | |
3720 | widest2_int warg0 = widest2_int_cst (arg0); | |
3721 | widest2_int warg1 = widest2_int_cst (arg1); | |
3722 | widest2_int wres; | |
3723 | switch (code) | |
3724 | { | |
3725 | case PLUS_EXPR: wres = wi::add (warg0, warg1); break; | |
3726 | case MINUS_EXPR: wres = wi::sub (warg0, warg1); break; | |
3727 | case MULT_EXPR: wres = wi::mul (warg0, warg1); break; | |
3728 | default: gcc_unreachable (); | |
3729 | } | |
3730 | signop sign = TYPE_SIGN (type); | |
3731 | if (sign == UNSIGNED && wi::neg_p (wres)) | |
3732 | return true; | |
3733 | return wi::min_precision (wres, sign) > TYPE_PRECISION (type); | |
3734 | } | |
3735 | ||
2d18b16d | 3736 | /* Attempt to fold a call statement referenced by the statement iterator GSI. |
3737 | The statement may be replaced by another statement, e.g., if the call | |
3738 | simplifies to a constant value. Return true if any changes were made. | |
3739 | It is assumed that the operands have been previously folded. */ | |
3740 | ||
3aa6ac67 | 3741 | static bool |
3fd0ca33 | 3742 | gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace) |
2d18b16d | 3743 | { |
1a91d914 | 3744 | gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); |
6fec5449 | 3745 | tree callee; |
3aa6ac67 | 3746 | bool changed = false; |
3747 | unsigned i; | |
2d18b16d | 3748 | |
3aa6ac67 | 3749 | /* Fold *& in call arguments. */ |
3750 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
3751 | if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i))) | |
3752 | { | |
3753 | tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false); | |
3754 | if (tmp) | |
3755 | { | |
3756 | gimple_call_set_arg (stmt, i, tmp); | |
3757 | changed = true; | |
3758 | } | |
3759 | } | |
6fec5449 | 3760 | |
3761 | /* Check for virtual calls that became direct calls. */ | |
3762 | callee = gimple_call_fn (stmt); | |
fb049fba | 3763 | if (callee && TREE_CODE (callee) == OBJ_TYPE_REF) |
6fec5449 | 3764 | { |
3658fd1d | 3765 | if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE) |
3766 | { | |
10fba9c0 | 3767 | if (dump_file && virtual_method_call_p (callee) |
3768 | && !possible_polymorphic_call_target_p | |
379f6698 | 3769 | (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl |
3770 | (OBJ_TYPE_REF_EXPR (callee))))) | |
10fba9c0 | 3771 | { |
3772 | fprintf (dump_file, | |
f70f513f | 3773 | "Type inheritance inconsistent devirtualization of "); |
10fba9c0 | 3774 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
3775 | fprintf (dump_file, " to "); | |
3776 | print_generic_expr (dump_file, callee, TDF_SLIM); | |
3777 | fprintf (dump_file, "\n"); | |
3778 | } | |
3779 | ||
3658fd1d | 3780 | gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee)); |
3aa6ac67 | 3781 | changed = true; |
3782 | } | |
f70f513f | 3783 | else if (flag_devirtualize && !inplace && virtual_method_call_p (callee)) |
3aa6ac67 | 3784 | { |
76058579 | 3785 | bool final; |
3786 | vec <cgraph_node *>targets | |
1b613a0a | 3787 | = possible_polymorphic_call_targets (callee, stmt, &final); |
ceb49bba | 3788 | if (final && targets.length () <= 1 && dbg_cnt (devirt)) |
3aa6ac67 | 3789 | { |
f70f513f | 3790 | tree lhs = gimple_call_lhs (stmt); |
ceb49bba | 3791 | if (dump_enabled_p ()) |
3792 | { | |
4c8041d7 | 3793 | location_t loc = gimple_location_safe (stmt); |
ceb49bba | 3794 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc, |
3795 | "folding virtual function call to %s\n", | |
3796 | targets.length () == 1 | |
3797 | ? targets[0]->name () | |
3798 | : "__builtin_unreachable"); | |
3799 | } | |
76058579 | 3800 | if (targets.length () == 1) |
3aa6ac67 | 3801 | { |
b9a1dcd5 | 3802 | tree fndecl = targets[0]->decl; |
3803 | gimple_call_set_fndecl (stmt, fndecl); | |
3aa6ac67 | 3804 | changed = true; |
b9a1dcd5 | 3805 | /* If changing the call to __cxa_pure_virtual |
3806 | or similar noreturn function, adjust gimple_call_fntype | |
3807 | too. */ | |
e10bc159 | 3808 | if (gimple_call_noreturn_p (stmt) |
b9a1dcd5 | 3809 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl))) |
3810 | && TYPE_ARG_TYPES (TREE_TYPE (fndecl)) | |
3811 | && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) | |
3812 | == void_type_node)) | |
3813 | gimple_call_set_fntype (stmt, TREE_TYPE (fndecl)); | |
f70f513f | 3814 | /* If the call becomes noreturn, remove the lhs. */ |
73ef73bd | 3815 | if (lhs |
3816 | && gimple_call_noreturn_p (stmt) | |
b9a1dcd5 | 3817 | && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt))) |
0a6b484c | 3818 | || should_remove_lhs_p (lhs))) |
f70f513f | 3819 | { |
3820 | if (TREE_CODE (lhs) == SSA_NAME) | |
3821 | { | |
f9e245b2 | 3822 | tree var = create_tmp_var (TREE_TYPE (lhs)); |
f70f513f | 3823 | tree def = get_or_create_ssa_default_def (cfun, var); |
42acab1c | 3824 | gimple *new_stmt = gimple_build_assign (lhs, def); |
f70f513f | 3825 | gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT); |
3826 | } | |
3827 | gimple_call_set_lhs (stmt, NULL_TREE); | |
3828 | } | |
aee8a3e8 | 3829 | maybe_remove_unused_call_args (cfun, stmt); |
3aa6ac67 | 3830 | } |
f70f513f | 3831 | else |
68b0b56c | 3832 | { |
3833 | tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
42acab1c | 3834 | gimple *new_stmt = gimple_build_call (fndecl, 0); |
68b0b56c | 3835 | gimple_set_location (new_stmt, gimple_location (stmt)); |
f70f513f | 3836 | if (lhs && TREE_CODE (lhs) == SSA_NAME) |
3837 | { | |
f9e245b2 | 3838 | tree var = create_tmp_var (TREE_TYPE (lhs)); |
f70f513f | 3839 | tree def = get_or_create_ssa_default_def (cfun, var); |
ac3cd7ce | 3840 | |
3841 | /* To satisfy condition for | |
3842 | cgraph_update_edges_for_call_stmt_node, | |
3843 | we need to preserve GIMPLE_CALL statement | |
3844 | at position of GSI iterator. */ | |
f70f513f | 3845 | update_call_from_tree (gsi, def); |
ac3cd7ce | 3846 | gsi_insert_before (gsi, new_stmt, GSI_NEW_STMT); |
f70f513f | 3847 | } |
3848 | else | |
7f1b049e | 3849 | { |
3850 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
3851 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
3852 | gsi_replace (gsi, new_stmt, false); | |
3853 | } | |
68b0b56c | 3854 | return true; |
3855 | } | |
3aa6ac67 | 3856 | } |
3658fd1d | 3857 | } |
3aa6ac67 | 3858 | } |
3658fd1d | 3859 | |
156cc902 | 3860 | /* Check for indirect calls that became direct calls, and then |
3861 | no longer require a static chain. */ | |
3862 | if (gimple_call_chain (stmt)) | |
3863 | { | |
3864 | tree fn = gimple_call_fndecl (stmt); | |
3865 | if (fn && !DECL_STATIC_CHAIN (fn)) | |
3866 | { | |
3867 | gimple_call_set_chain (stmt, NULL); | |
3868 | changed = true; | |
3869 | } | |
3870 | else | |
3871 | { | |
3872 | tree tmp = maybe_fold_reference (gimple_call_chain (stmt), false); | |
3873 | if (tmp) | |
3874 | { | |
3875 | gimple_call_set_chain (stmt, tmp); | |
3876 | changed = true; | |
3877 | } | |
3878 | } | |
3879 | } | |
3880 | ||
3aa6ac67 | 3881 | if (inplace) |
3882 | return changed; | |
3883 | ||
3884 | /* Check for builtins that CCP can handle using information not | |
3885 | available in the generic fold routines. */ | |
b9ea678c | 3886 | if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) |
3887 | { | |
3888 | if (gimple_fold_builtin (gsi)) | |
3889 | changed = true; | |
3890 | } | |
3891 | else if (gimple_call_builtin_p (stmt, BUILT_IN_MD)) | |
3aa6ac67 | 3892 | { |
29cad3b6 | 3893 | changed |= targetm.gimple_fold_builtin (gsi); |
6fec5449 | 3894 | } |
ce8e6661 | 3895 | else if (gimple_call_internal_p (stmt)) |
c83059be | 3896 | { |
ce8e6661 | 3897 | enum tree_code subcode = ERROR_MARK; |
3898 | tree result = NULL_TREE; | |
0c93c8a9 | 3899 | bool cplx_result = false; |
3900 | tree overflow = NULL_TREE; | |
ce8e6661 | 3901 | switch (gimple_call_internal_fn (stmt)) |
3902 | { | |
3903 | case IFN_BUILTIN_EXPECT: | |
3904 | result = fold_builtin_expect (gimple_location (stmt), | |
3905 | gimple_call_arg (stmt, 0), | |
3906 | gimple_call_arg (stmt, 1), | |
3907 | gimple_call_arg (stmt, 2)); | |
3908 | break; | |
0b45f2d1 | 3909 | case IFN_UBSAN_OBJECT_SIZE: |
3910 | if (integer_all_onesp (gimple_call_arg (stmt, 2)) | |
3911 | || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST | |
3912 | && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST | |
3913 | && tree_int_cst_le (gimple_call_arg (stmt, 1), | |
3914 | gimple_call_arg (stmt, 2)))) | |
3915 | { | |
cfe45b40 | 3916 | gsi_replace (gsi, gimple_build_nop (), false); |
0b45f2d1 | 3917 | unlink_stmt_vdef (stmt); |
3918 | release_defs (stmt); | |
3919 | return true; | |
3920 | } | |
3921 | break; | |
b0ccb4e7 | 3922 | case IFN_GOACC_DIM_SIZE: |
3923 | case IFN_GOACC_DIM_POS: | |
3924 | result = fold_internal_goacc_dim (stmt); | |
3925 | break; | |
ce8e6661 | 3926 | case IFN_UBSAN_CHECK_ADD: |
3927 | subcode = PLUS_EXPR; | |
3928 | break; | |
3929 | case IFN_UBSAN_CHECK_SUB: | |
3930 | subcode = MINUS_EXPR; | |
3931 | break; | |
3932 | case IFN_UBSAN_CHECK_MUL: | |
3933 | subcode = MULT_EXPR; | |
3934 | break; | |
0c93c8a9 | 3935 | case IFN_ADD_OVERFLOW: |
3936 | subcode = PLUS_EXPR; | |
3937 | cplx_result = true; | |
3938 | break; | |
3939 | case IFN_SUB_OVERFLOW: | |
3940 | subcode = MINUS_EXPR; | |
3941 | cplx_result = true; | |
3942 | break; | |
3943 | case IFN_MUL_OVERFLOW: | |
3944 | subcode = MULT_EXPR; | |
3945 | cplx_result = true; | |
3946 | break; | |
ce8e6661 | 3947 | default: |
3948 | break; | |
3949 | } | |
3950 | if (subcode != ERROR_MARK) | |
3951 | { | |
3952 | tree arg0 = gimple_call_arg (stmt, 0); | |
3953 | tree arg1 = gimple_call_arg (stmt, 1); | |
0c93c8a9 | 3954 | tree type = TREE_TYPE (arg0); |
3955 | if (cplx_result) | |
3956 | { | |
3957 | tree lhs = gimple_call_lhs (stmt); | |
3958 | if (lhs == NULL_TREE) | |
3959 | type = NULL_TREE; | |
3960 | else | |
3961 | type = TREE_TYPE (TREE_TYPE (lhs)); | |
3962 | } | |
3963 | if (type == NULL_TREE) | |
3964 | ; | |
ce8e6661 | 3965 | /* x = y + 0; x = y - 0; x = y * 0; */ |
0c93c8a9 | 3966 | else if (integer_zerop (arg1)) |
3967 | result = subcode == MULT_EXPR ? integer_zero_node : arg0; | |
ce8e6661 | 3968 | /* x = 0 + y; x = 0 * y; */ |
3969 | else if (subcode != MINUS_EXPR && integer_zerop (arg0)) | |
0c93c8a9 | 3970 | result = subcode == MULT_EXPR ? integer_zero_node : arg1; |
ce8e6661 | 3971 | /* x = y - y; */ |
3972 | else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0)) | |
0c93c8a9 | 3973 | result = integer_zero_node; |
ce8e6661 | 3974 | /* x = y * 1; x = 1 * y; */ |
0c93c8a9 | 3975 | else if (subcode == MULT_EXPR && integer_onep (arg1)) |
3976 | result = arg0; | |
3977 | else if (subcode == MULT_EXPR && integer_onep (arg0)) | |
3978 | result = arg1; | |
3979 | else if (TREE_CODE (arg0) == INTEGER_CST | |
3980 | && TREE_CODE (arg1) == INTEGER_CST) | |
ce8e6661 | 3981 | { |
0c93c8a9 | 3982 | if (cplx_result) |
3983 | result = int_const_binop (subcode, fold_convert (type, arg0), | |
3984 | fold_convert (type, arg1)); | |
3985 | else | |
3986 | result = int_const_binop (subcode, arg0, arg1); | |
3987 | if (result && arith_overflowed_p (subcode, type, arg0, arg1)) | |
3988 | { | |
3989 | if (cplx_result) | |
3990 | overflow = build_one_cst (type); | |
3991 | else | |
3992 | result = NULL_TREE; | |
3993 | } | |
3994 | } | |
3995 | if (result) | |
3996 | { | |
3997 | if (result == integer_zero_node) | |
3998 | result = build_zero_cst (type); | |
3999 | else if (cplx_result && TREE_TYPE (result) != type) | |
4000 | { | |
4001 | if (TREE_CODE (result) == INTEGER_CST) | |
4002 | { | |
4003 | if (arith_overflowed_p (PLUS_EXPR, type, result, | |
4004 | integer_zero_node)) | |
4005 | overflow = build_one_cst (type); | |
4006 | } | |
4007 | else if ((!TYPE_UNSIGNED (TREE_TYPE (result)) | |
4008 | && TYPE_UNSIGNED (type)) | |
4009 | || (TYPE_PRECISION (type) | |
4010 | < (TYPE_PRECISION (TREE_TYPE (result)) | |
4011 | + (TYPE_UNSIGNED (TREE_TYPE (result)) | |
4012 | && !TYPE_UNSIGNED (type))))) | |
4013 | result = NULL_TREE; | |
4014 | if (result) | |
4015 | result = fold_convert (type, result); | |
4016 | } | |
ce8e6661 | 4017 | } |
4018 | } | |
0c93c8a9 | 4019 | |
c83059be | 4020 | if (result) |
4021 | { | |
0c93c8a9 | 4022 | if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result)) |
4023 | result = drop_tree_overflow (result); | |
4024 | if (cplx_result) | |
4025 | { | |
4026 | if (overflow == NULL_TREE) | |
4027 | overflow = build_zero_cst (TREE_TYPE (result)); | |
4028 | tree ctype = build_complex_type (TREE_TYPE (result)); | |
4029 | if (TREE_CODE (result) == INTEGER_CST | |
4030 | && TREE_CODE (overflow) == INTEGER_CST) | |
4031 | result = build_complex (ctype, result, overflow); | |
4032 | else | |
4033 | result = build2_loc (gimple_location (stmt), COMPLEX_EXPR, | |
4034 | ctype, result, overflow); | |
4035 | } | |
c83059be | 4036 | if (!update_call_from_tree (gsi, result)) |
4037 | gimplify_and_update_call_from_tree (gsi, result); | |
4038 | changed = true; | |
4039 | } | |
4040 | } | |
6fec5449 | 4041 | |
3aa6ac67 | 4042 | return changed; |
2d18b16d | 4043 | } |
4044 | ||
55534d34 | 4045 | |
1ae7fdc9 | 4046 | /* Return true whether NAME has a use on STMT. */ |
4047 | ||
4048 | static bool | |
42acab1c | 4049 | has_use_on_stmt (tree name, gimple *stmt) |
1ae7fdc9 | 4050 | { |
4051 | imm_use_iterator iter; | |
4052 | use_operand_p use_p; | |
4053 | FOR_EACH_IMM_USE_FAST (use_p, iter, name) | |
4054 | if (USE_STMT (use_p) == stmt) | |
4055 | return true; | |
4056 | return false; | |
4057 | } | |
4058 | ||
55534d34 | 4059 | /* Worker for fold_stmt_1 dispatch to pattern based folding with |
4060 | gimple_simplify. | |
4061 | ||
4062 | Replaces *GSI with the simplification result in RCODE and OPS | |
4063 | and the associated statements in *SEQ. Does the replacement | |
4064 | according to INPLACE and returns true if the operation succeeded. */ | |
4065 | ||
4066 | static bool | |
4067 | replace_stmt_with_simplification (gimple_stmt_iterator *gsi, | |
4068 | code_helper rcode, tree *ops, | |
4069 | gimple_seq *seq, bool inplace) | |
4070 | { | |
42acab1c | 4071 | gimple *stmt = gsi_stmt (*gsi); |
55534d34 | 4072 | |
4073 | /* Play safe and do not allow abnormals to be mentioned in | |
1ae7fdc9 | 4074 | newly created statements. See also maybe_push_res_to_seq. |
4075 | As an exception allow such uses if there was a use of the | |
4076 | same SSA name on the old stmt. */ | |
55534d34 | 4077 | if ((TREE_CODE (ops[0]) == SSA_NAME |
1ae7fdc9 | 4078 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[0]) |
4079 | && !has_use_on_stmt (ops[0], stmt)) | |
55534d34 | 4080 | || (ops[1] |
4081 | && TREE_CODE (ops[1]) == SSA_NAME | |
1ae7fdc9 | 4082 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[1]) |
4083 | && !has_use_on_stmt (ops[1], stmt)) | |
55534d34 | 4084 | || (ops[2] |
4085 | && TREE_CODE (ops[2]) == SSA_NAME | |
1ae7fdc9 | 4086 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[2]) |
1943a4a2 | 4087 | && !has_use_on_stmt (ops[2], stmt)) |
4088 | || (COMPARISON_CLASS_P (ops[0]) | |
4089 | && ((TREE_CODE (TREE_OPERAND (ops[0], 0)) == SSA_NAME | |
4090 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], 0)) | |
4091 | && !has_use_on_stmt (TREE_OPERAND (ops[0], 0), stmt)) | |
4092 | || (TREE_CODE (TREE_OPERAND (ops[0], 1)) == SSA_NAME | |
4093 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], 1)) | |
4094 | && !has_use_on_stmt (TREE_OPERAND (ops[0], 1), stmt))))) | |
55534d34 | 4095 | return false; |
4096 | ||
433f3eed | 4097 | /* Don't insert new statements when INPLACE is true, even if we could |
4098 | reuse STMT for the final statement. */ | |
4099 | if (inplace && !gimple_seq_empty_p (*seq)) | |
4100 | return false; | |
4101 | ||
1a91d914 | 4102 | if (gcond *cond_stmt = dyn_cast <gcond *> (stmt)) |
55534d34 | 4103 | { |
4104 | gcc_assert (rcode.is_tree_code ()); | |
4105 | if (TREE_CODE_CLASS ((enum tree_code)rcode) == tcc_comparison | |
4106 | /* GIMPLE_CONDs condition may not throw. */ | |
4107 | && (!flag_exceptions | |
4108 | || !cfun->can_throw_non_call_exceptions | |
4109 | || !operation_could_trap_p (rcode, | |
4110 | FLOAT_TYPE_P (TREE_TYPE (ops[0])), | |
4111 | false, NULL_TREE))) | |
1a91d914 | 4112 | gimple_cond_set_condition (cond_stmt, rcode, ops[0], ops[1]); |
55534d34 | 4113 | else if (rcode == SSA_NAME) |
1a91d914 | 4114 | gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0], |
55534d34 | 4115 | build_zero_cst (TREE_TYPE (ops[0]))); |
4116 | else if (rcode == INTEGER_CST) | |
4117 | { | |
4118 | if (integer_zerop (ops[0])) | |
1a91d914 | 4119 | gimple_cond_make_false (cond_stmt); |
55534d34 | 4120 | else |
1a91d914 | 4121 | gimple_cond_make_true (cond_stmt); |
55534d34 | 4122 | } |
4123 | else if (!inplace) | |
4124 | { | |
4125 | tree res = maybe_push_res_to_seq (rcode, boolean_type_node, | |
4126 | ops, seq); | |
4127 | if (!res) | |
4128 | return false; | |
1a91d914 | 4129 | gimple_cond_set_condition (cond_stmt, NE_EXPR, res, |
55534d34 | 4130 | build_zero_cst (TREE_TYPE (res))); |
4131 | } | |
4132 | else | |
4133 | return false; | |
4134 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4135 | { | |
4136 | fprintf (dump_file, "gimple_simplified to "); | |
4137 | if (!gimple_seq_empty_p (*seq)) | |
4138 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
4139 | print_gimple_stmt (dump_file, gsi_stmt (*gsi), | |
4140 | 0, TDF_SLIM); | |
4141 | } | |
4142 | gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT); | |
4143 | return true; | |
4144 | } | |
4145 | else if (is_gimple_assign (stmt) | |
4146 | && rcode.is_tree_code ()) | |
4147 | { | |
4148 | if (!inplace | |
2ad7e37a | 4149 | || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (rcode)) |
55534d34 | 4150 | { |
4151 | maybe_build_generic_op (rcode, | |
eadb4d2f | 4152 | TREE_TYPE (gimple_assign_lhs (stmt)), ops); |
806413d2 | 4153 | gimple_assign_set_rhs_with_ops (gsi, rcode, ops[0], ops[1], ops[2]); |
55534d34 | 4154 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4155 | { | |
4156 | fprintf (dump_file, "gimple_simplified to "); | |
4157 | if (!gimple_seq_empty_p (*seq)) | |
4158 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
4159 | print_gimple_stmt (dump_file, gsi_stmt (*gsi), | |
4160 | 0, TDF_SLIM); | |
4161 | } | |
4162 | gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT); | |
4163 | return true; | |
4164 | } | |
4165 | } | |
1a191038 | 4166 | else if (rcode.is_fn_code () |
3a18d05c | 4167 | && gimple_call_combined_fn (stmt) == rcode) |
1a191038 | 4168 | { |
4169 | unsigned i; | |
4170 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
4171 | { | |
4172 | gcc_assert (ops[i] != NULL_TREE); | |
4173 | gimple_call_set_arg (stmt, i, ops[i]); | |
4174 | } | |
4175 | if (i < 3) | |
4176 | gcc_assert (ops[i] == NULL_TREE); | |
433f3eed | 4177 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4178 | { | |
4179 | fprintf (dump_file, "gimple_simplified to "); | |
4180 | if (!gimple_seq_empty_p (*seq)) | |
4181 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
4182 | print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM); | |
4183 | } | |
4184 | gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT); | |
1a191038 | 4185 | return true; |
4186 | } | |
55534d34 | 4187 | else if (!inplace) |
4188 | { | |
4189 | if (gimple_has_lhs (stmt)) | |
4190 | { | |
4191 | tree lhs = gimple_get_lhs (stmt); | |
2dad62cf | 4192 | if (!maybe_push_res_to_seq (rcode, TREE_TYPE (lhs), |
4193 | ops, seq, lhs)) | |
4194 | return false; | |
55534d34 | 4195 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4196 | { | |
4197 | fprintf (dump_file, "gimple_simplified to "); | |
4198 | print_gimple_seq (dump_file, *seq, 0, TDF_SLIM); | |
4199 | } | |
4200 | gsi_replace_with_seq_vops (gsi, *seq); | |
4201 | return true; | |
4202 | } | |
4203 | else | |
4204 | gcc_unreachable (); | |
4205 | } | |
4206 | ||
4207 | return false; | |
4208 | } | |
4209 | ||
1bd2e4a0 | 4210 | /* Canonicalize MEM_REFs invariant address operand after propagation. */ |
4211 | ||
4212 | static bool | |
4213 | maybe_canonicalize_mem_ref_addr (tree *t) | |
4214 | { | |
4215 | bool res = false; | |
4216 | ||
4217 | if (TREE_CODE (*t) == ADDR_EXPR) | |
4218 | t = &TREE_OPERAND (*t, 0); | |
4219 | ||
7345b977 | 4220 | /* The C and C++ frontends use an ARRAY_REF for indexing with their |
4221 | generic vector extension. The actual vector referenced is | |
4222 | view-converted to an array type for this purpose. If the index | |
4223 | is constant the canonical representation in the middle-end is a | |
4224 | BIT_FIELD_REF so re-write the former to the latter here. */ | |
4225 | if (TREE_CODE (*t) == ARRAY_REF | |
4226 | && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR | |
4227 | && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST | |
4228 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0)))) | |
4229 | { | |
4230 | tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0)); | |
4231 | if (VECTOR_TYPE_P (vtype)) | |
4232 | { | |
4233 | tree low = array_ref_low_bound (*t); | |
4234 | if (TREE_CODE (low) == INTEGER_CST) | |
4235 | { | |
4236 | if (tree_int_cst_le (low, TREE_OPERAND (*t, 1))) | |
4237 | { | |
4238 | widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)), | |
4239 | wi::to_widest (low)); | |
4240 | idx = wi::mul (idx, wi::to_widest | |
4241 | (TYPE_SIZE (TREE_TYPE (*t)))); | |
4242 | widest_int ext | |
4243 | = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t)))); | |
4244 | if (wi::les_p (ext, wi::to_widest (TYPE_SIZE (vtype)))) | |
4245 | { | |
4246 | *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF, | |
4247 | TREE_TYPE (*t), | |
4248 | TREE_OPERAND (TREE_OPERAND (*t, 0), 0), | |
4249 | TYPE_SIZE (TREE_TYPE (*t)), | |
a07b1b15 | 4250 | wide_int_to_tree (bitsizetype, idx)); |
7345b977 | 4251 | res = true; |
4252 | } | |
4253 | } | |
4254 | } | |
4255 | } | |
4256 | } | |
4257 | ||
1bd2e4a0 | 4258 | while (handled_component_p (*t)) |
4259 | t = &TREE_OPERAND (*t, 0); | |
4260 | ||
4261 | /* Canonicalize MEM [&foo.bar, 0] which appears after propagating | |
4262 | of invariant addresses into a SSA name MEM_REF address. */ | |
4263 | if (TREE_CODE (*t) == MEM_REF | |
4264 | || TREE_CODE (*t) == TARGET_MEM_REF) | |
4265 | { | |
4266 | tree addr = TREE_OPERAND (*t, 0); | |
4267 | if (TREE_CODE (addr) == ADDR_EXPR | |
4268 | && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF | |
4269 | || handled_component_p (TREE_OPERAND (addr, 0)))) | |
4270 | { | |
4271 | tree base; | |
4272 | HOST_WIDE_INT coffset; | |
4273 | base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), | |
4274 | &coffset); | |
4275 | if (!base) | |
4276 | gcc_unreachable (); | |
4277 | ||
4278 | TREE_OPERAND (*t, 0) = build_fold_addr_expr (base); | |
4279 | TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR, | |
4280 | TREE_OPERAND (*t, 1), | |
4281 | size_int (coffset)); | |
4282 | res = true; | |
4283 | } | |
4284 | gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL | |
4285 | || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0))); | |
4286 | } | |
4287 | ||
4288 | /* Canonicalize back MEM_REFs to plain reference trees if the object | |
4289 | accessed is a decl that has the same access semantics as the MEM_REF. */ | |
4290 | if (TREE_CODE (*t) == MEM_REF | |
4291 | && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR | |
62b0a610 | 4292 | && integer_zerop (TREE_OPERAND (*t, 1)) |
4293 | && MR_DEPENDENCE_CLIQUE (*t) == 0) | |
1bd2e4a0 | 4294 | { |
4295 | tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
4296 | tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1)); | |
4297 | if (/* Same volatile qualification. */ | |
4298 | TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl) | |
4299 | /* Same TBAA behavior with -fstrict-aliasing. */ | |
4300 | && !TYPE_REF_CAN_ALIAS_ALL (alias_type) | |
4301 | && (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) | |
4302 | == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type))) | |
4303 | /* Same alignment. */ | |
4304 | && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t)) | |
4305 | /* We have to look out here to not drop a required conversion | |
4306 | from the rhs to the lhs if *t appears on the lhs or vice-versa | |
4307 | if it appears on the rhs. Thus require strict type | |
4308 | compatibility. */ | |
4309 | && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl))) | |
4310 | { | |
4311 | *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0); | |
4312 | res = true; | |
4313 | } | |
4314 | } | |
4315 | ||
4316 | /* Canonicalize TARGET_MEM_REF in particular with respect to | |
4317 | the indexes becoming constant. */ | |
4318 | else if (TREE_CODE (*t) == TARGET_MEM_REF) | |
4319 | { | |
4320 | tree tem = maybe_fold_tmr (*t); | |
4321 | if (tem) | |
4322 | { | |
4323 | *t = tem; | |
4324 | res = true; | |
4325 | } | |
4326 | } | |
4327 | ||
4328 | return res; | |
4329 | } | |
4330 | ||
2d18b16d | 4331 | /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument |
4332 | distinguishes both cases. */ | |
4333 | ||
4334 | static bool | |
55534d34 | 4335 | fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree)) |
2d18b16d | 4336 | { |
4337 | bool changed = false; | |
42acab1c | 4338 | gimple *stmt = gsi_stmt (*gsi); |
eb1a077c | 4339 | bool nowarning = gimple_no_warning_p (stmt); |
2d18b16d | 4340 | unsigned i; |
eb1a077c | 4341 | fold_defer_overflow_warnings (); |
2d18b16d | 4342 | |
1bd2e4a0 | 4343 | /* First do required canonicalization of [TARGET_]MEM_REF addresses |
4344 | after propagation. | |
4345 | ??? This shouldn't be done in generic folding but in the | |
4346 | propagation helpers which also know whether an address was | |
1ae7fdc9 | 4347 | propagated. |
4348 | Also canonicalize operand order. */ | |
1bd2e4a0 | 4349 | switch (gimple_code (stmt)) |
4350 | { | |
4351 | case GIMPLE_ASSIGN: | |
4352 | if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS) | |
4353 | { | |
4354 | tree *rhs = gimple_assign_rhs1_ptr (stmt); | |
4355 | if ((REFERENCE_CLASS_P (*rhs) | |
4356 | || TREE_CODE (*rhs) == ADDR_EXPR) | |
4357 | && maybe_canonicalize_mem_ref_addr (rhs)) | |
4358 | changed = true; | |
4359 | tree *lhs = gimple_assign_lhs_ptr (stmt); | |
4360 | if (REFERENCE_CLASS_P (*lhs) | |
4361 | && maybe_canonicalize_mem_ref_addr (lhs)) | |
4362 | changed = true; | |
4363 | } | |
1ae7fdc9 | 4364 | else |
4365 | { | |
4366 | /* Canonicalize operand order. */ | |
4367 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
4368 | if (TREE_CODE_CLASS (code) == tcc_comparison | |
4369 | || commutative_tree_code (code) | |
4370 | || commutative_ternary_tree_code (code)) | |
4371 | { | |
4372 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
4373 | tree rhs2 = gimple_assign_rhs2 (stmt); | |
48baf518 | 4374 | if (tree_swap_operands_p (rhs1, rhs2)) |
1ae7fdc9 | 4375 | { |
4376 | gimple_assign_set_rhs1 (stmt, rhs2); | |
4377 | gimple_assign_set_rhs2 (stmt, rhs1); | |
4378 | if (TREE_CODE_CLASS (code) == tcc_comparison) | |
4379 | gimple_assign_set_rhs_code (stmt, | |
4380 | swap_tree_comparison (code)); | |
4381 | changed = true; | |
4382 | } | |
4383 | } | |
4384 | } | |
1bd2e4a0 | 4385 | break; |
4386 | case GIMPLE_CALL: | |
4387 | { | |
4388 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
4389 | { | |
4390 | tree *arg = gimple_call_arg_ptr (stmt, i); | |
4391 | if (REFERENCE_CLASS_P (*arg) | |
4392 | && maybe_canonicalize_mem_ref_addr (arg)) | |
4393 | changed = true; | |
4394 | } | |
4395 | tree *lhs = gimple_call_lhs_ptr (stmt); | |
4396 | if (*lhs | |
4397 | && REFERENCE_CLASS_P (*lhs) | |
4398 | && maybe_canonicalize_mem_ref_addr (lhs)) | |
4399 | changed = true; | |
4400 | break; | |
4401 | } | |
4402 | case GIMPLE_ASM: | |
4403 | { | |
1a91d914 | 4404 | gasm *asm_stmt = as_a <gasm *> (stmt); |
4405 | for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i) | |
1bd2e4a0 | 4406 | { |
1a91d914 | 4407 | tree link = gimple_asm_output_op (asm_stmt, i); |
1bd2e4a0 | 4408 | tree op = TREE_VALUE (link); |
4409 | if (REFERENCE_CLASS_P (op) | |
4410 | && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link))) | |
4411 | changed = true; | |
4412 | } | |
1a91d914 | 4413 | for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i) |
1bd2e4a0 | 4414 | { |
1a91d914 | 4415 | tree link = gimple_asm_input_op (asm_stmt, i); |
1bd2e4a0 | 4416 | tree op = TREE_VALUE (link); |
4417 | if ((REFERENCE_CLASS_P (op) | |
4418 | || TREE_CODE (op) == ADDR_EXPR) | |
4419 | && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link))) | |
4420 | changed = true; | |
4421 | } | |
4422 | } | |
4423 | break; | |
4424 | case GIMPLE_DEBUG: | |
4425 | if (gimple_debug_bind_p (stmt)) | |
4426 | { | |
4427 | tree *val = gimple_debug_bind_get_value_ptr (stmt); | |
4428 | if (*val | |
4429 | && (REFERENCE_CLASS_P (*val) | |
4430 | || TREE_CODE (*val) == ADDR_EXPR) | |
4431 | && maybe_canonicalize_mem_ref_addr (val)) | |
4432 | changed = true; | |
4433 | } | |
4434 | break; | |
1ae7fdc9 | 4435 | case GIMPLE_COND: |
4436 | { | |
4437 | /* Canonicalize operand order. */ | |
4438 | tree lhs = gimple_cond_lhs (stmt); | |
4439 | tree rhs = gimple_cond_rhs (stmt); | |
48baf518 | 4440 | if (tree_swap_operands_p (lhs, rhs)) |
1ae7fdc9 | 4441 | { |
4442 | gcond *gc = as_a <gcond *> (stmt); | |
4443 | gimple_cond_set_lhs (gc, rhs); | |
4444 | gimple_cond_set_rhs (gc, lhs); | |
4445 | gimple_cond_set_code (gc, | |
4446 | swap_tree_comparison (gimple_cond_code (gc))); | |
4447 | changed = true; | |
4448 | } | |
4449 | } | |
1bd2e4a0 | 4450 | default:; |
4451 | } | |
4452 | ||
55534d34 | 4453 | /* Dispatch to pattern-based folding. */ |
4454 | if (!inplace | |
4455 | || is_gimple_assign (stmt) | |
4456 | || gimple_code (stmt) == GIMPLE_COND) | |
4457 | { | |
4458 | gimple_seq seq = NULL; | |
4459 | code_helper rcode; | |
4460 | tree ops[3] = {}; | |
fc6cc27b | 4461 | if (gimple_simplify (stmt, &rcode, ops, inplace ? NULL : &seq, |
4462 | valueize, valueize)) | |
55534d34 | 4463 | { |
4464 | if (replace_stmt_with_simplification (gsi, rcode, ops, &seq, inplace)) | |
4465 | changed = true; | |
4466 | else | |
4467 | gimple_seq_discard (seq); | |
4468 | } | |
4469 | } | |
4470 | ||
4471 | stmt = gsi_stmt (*gsi); | |
4472 | ||
2d18b16d | 4473 | /* Fold the main computation performed by the statement. */ |
4474 | switch (gimple_code (stmt)) | |
4475 | { | |
4476 | case GIMPLE_ASSIGN: | |
4477 | { | |
de299e50 | 4478 | /* Try to canonicalize for boolean-typed X the comparisons |
4479 | X == 0, X == 1, X != 0, and X != 1. */ | |
4480 | if (gimple_assign_rhs_code (stmt) == EQ_EXPR | |
4481 | || gimple_assign_rhs_code (stmt) == NE_EXPR) | |
fbd25d42 | 4482 | { |
de299e50 | 4483 | tree lhs = gimple_assign_lhs (stmt); |
4484 | tree op1 = gimple_assign_rhs1 (stmt); | |
4485 | tree op2 = gimple_assign_rhs2 (stmt); | |
4486 | tree type = TREE_TYPE (op1); | |
4487 | ||
4488 | /* Check whether the comparison operands are of the same boolean | |
4489 | type as the result type is. | |
4490 | Check that second operand is an integer-constant with value | |
4491 | one or zero. */ | |
4492 | if (TREE_CODE (op2) == INTEGER_CST | |
4493 | && (integer_zerop (op2) || integer_onep (op2)) | |
4494 | && useless_type_conversion_p (TREE_TYPE (lhs), type)) | |
4495 | { | |
4496 | enum tree_code cmp_code = gimple_assign_rhs_code (stmt); | |
4497 | bool is_logical_not = false; | |
4498 | ||
4499 | /* X == 0 and X != 1 is a logical-not.of X | |
4500 | X == 1 and X != 0 is X */ | |
4501 | if ((cmp_code == EQ_EXPR && integer_zerop (op2)) | |
4502 | || (cmp_code == NE_EXPR && integer_onep (op2))) | |
4503 | is_logical_not = true; | |
4504 | ||
4505 | if (is_logical_not == false) | |
4506 | gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1); | |
4507 | /* Only for one-bit precision typed X the transformation | |
4508 | !X -> ~X is valied. */ | |
4509 | else if (TYPE_PRECISION (type) == 1) | |
4510 | gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1); | |
4511 | /* Otherwise we use !X -> X ^ 1. */ | |
4512 | else | |
4513 | gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1, | |
4514 | build_int_cst (type, 1)); | |
4515 | changed = true; | |
4516 | break; | |
4517 | } | |
fbd25d42 | 4518 | } |
de299e50 | 4519 | |
4520 | unsigned old_num_ops = gimple_num_ops (stmt); | |
4521 | tree lhs = gimple_assign_lhs (stmt); | |
4522 | tree new_rhs = fold_gimple_assign (gsi); | |
2d18b16d | 4523 | if (new_rhs |
4524 | && !useless_type_conversion_p (TREE_TYPE (lhs), | |
4525 | TREE_TYPE (new_rhs))) | |
4526 | new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs); | |
4527 | if (new_rhs | |
4528 | && (!inplace | |
4529 | || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops)) | |
4530 | { | |
4531 | gimple_assign_set_rhs_from_tree (gsi, new_rhs); | |
4532 | changed = true; | |
4533 | } | |
4534 | break; | |
4535 | } | |
4536 | ||
2d18b16d | 4537 | case GIMPLE_CALL: |
3fd0ca33 | 4538 | changed |= gimple_fold_call (gsi, inplace); |
2d18b16d | 4539 | break; |
4540 | ||
4541 | case GIMPLE_ASM: | |
4542 | /* Fold *& in asm operands. */ | |
a02dcaf7 | 4543 | { |
1a91d914 | 4544 | gasm *asm_stmt = as_a <gasm *> (stmt); |
a02dcaf7 | 4545 | size_t noutputs; |
4546 | const char **oconstraints; | |
4547 | const char *constraint; | |
4548 | bool allows_mem, allows_reg; | |
4549 | ||
1a91d914 | 4550 | noutputs = gimple_asm_noutputs (asm_stmt); |
a02dcaf7 | 4551 | oconstraints = XALLOCAVEC (const char *, noutputs); |
4552 | ||
1a91d914 | 4553 | for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i) |
a02dcaf7 | 4554 | { |
1a91d914 | 4555 | tree link = gimple_asm_output_op (asm_stmt, i); |
a02dcaf7 | 4556 | tree op = TREE_VALUE (link); |
4557 | oconstraints[i] | |
4558 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
4559 | if (REFERENCE_CLASS_P (op) | |
4560 | && (op = maybe_fold_reference (op, true)) != NULL_TREE) | |
4561 | { | |
4562 | TREE_VALUE (link) = op; | |
4563 | changed = true; | |
4564 | } | |
4565 | } | |
1a91d914 | 4566 | for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i) |
a02dcaf7 | 4567 | { |
1a91d914 | 4568 | tree link = gimple_asm_input_op (asm_stmt, i); |
a02dcaf7 | 4569 | tree op = TREE_VALUE (link); |
4570 | constraint | |
4571 | = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
4572 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, | |
4573 | oconstraints, &allows_mem, &allows_reg); | |
4574 | if (REFERENCE_CLASS_P (op) | |
4575 | && (op = maybe_fold_reference (op, !allows_reg && allows_mem)) | |
4576 | != NULL_TREE) | |
4577 | { | |
4578 | TREE_VALUE (link) = op; | |
4579 | changed = true; | |
4580 | } | |
4581 | } | |
4582 | } | |
2d18b16d | 4583 | break; |
4584 | ||
69f01c7b | 4585 | case GIMPLE_DEBUG: |
4586 | if (gimple_debug_bind_p (stmt)) | |
4587 | { | |
4588 | tree val = gimple_debug_bind_get_value (stmt); | |
4589 | if (val | |
4590 | && REFERENCE_CLASS_P (val)) | |
4591 | { | |
4592 | tree tem = maybe_fold_reference (val, false); | |
4593 | if (tem) | |
4594 | { | |
4595 | gimple_debug_bind_set_value (stmt, tem); | |
4596 | changed = true; | |
4597 | } | |
4598 | } | |
48bcb852 | 4599 | else if (val |
4600 | && TREE_CODE (val) == ADDR_EXPR) | |
4601 | { | |
4602 | tree ref = TREE_OPERAND (val, 0); | |
4603 | tree tem = maybe_fold_reference (ref, false); | |
4604 | if (tem) | |
4605 | { | |
4606 | tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val)); | |
4607 | gimple_debug_bind_set_value (stmt, tem); | |
4608 | changed = true; | |
4609 | } | |
4610 | } | |
69f01c7b | 4611 | } |
4612 | break; | |
4613 | ||
1f1a3de9 | 4614 | case GIMPLE_RETURN: |
4615 | { | |
4616 | greturn *ret_stmt = as_a<greturn *> (stmt); | |
4617 | tree ret = gimple_return_retval(ret_stmt); | |
4618 | ||
4619 | if (ret && TREE_CODE (ret) == SSA_NAME && valueize) | |
4620 | { | |
4621 | tree val = valueize (ret); | |
44c09708 | 4622 | if (val && val != ret |
4623 | && may_propagate_copy (ret, val)) | |
1f1a3de9 | 4624 | { |
4625 | gimple_return_set_retval (ret_stmt, val); | |
4626 | changed = true; | |
4627 | } | |
4628 | } | |
4629 | } | |
4630 | break; | |
4631 | ||
2d18b16d | 4632 | default:; |
4633 | } | |
4634 | ||
4635 | stmt = gsi_stmt (*gsi); | |
4636 | ||
5352fc9b | 4637 | /* Fold *& on the lhs. */ |
4638 | if (gimple_has_lhs (stmt)) | |
2d18b16d | 4639 | { |
4640 | tree lhs = gimple_get_lhs (stmt); | |
4641 | if (lhs && REFERENCE_CLASS_P (lhs)) | |
4642 | { | |
4643 | tree new_lhs = maybe_fold_reference (lhs, true); | |
4644 | if (new_lhs) | |
4645 | { | |
4646 | gimple_set_lhs (stmt, new_lhs); | |
4647 | changed = true; | |
4648 | } | |
4649 | } | |
4650 | } | |
4651 | ||
eb1a077c | 4652 | fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0); |
2d18b16d | 4653 | return changed; |
4654 | } | |
4655 | ||
55534d34 | 4656 | /* Valueziation callback that ends up not following SSA edges. */ |
4657 | ||
4658 | tree | |
4659 | no_follow_ssa_edges (tree) | |
4660 | { | |
4661 | return NULL_TREE; | |
4662 | } | |
4663 | ||
b768d5ee | 4664 | /* Valueization callback that ends up following single-use SSA edges only. */ |
4665 | ||
4666 | tree | |
4667 | follow_single_use_edges (tree val) | |
4668 | { | |
4669 | if (TREE_CODE (val) == SSA_NAME | |
4670 | && !has_single_use (val)) | |
4671 | return NULL_TREE; | |
4672 | return val; | |
4673 | } | |
4674 | ||
2d18b16d | 4675 | /* Fold the statement pointed to by GSI. In some cases, this function may |
4676 | replace the whole statement with a new one. Returns true iff folding | |
4677 | makes any changes. | |
4678 | The statement pointed to by GSI should be in valid gimple form but may | |
4679 | be in unfolded state as resulting from for example constant propagation | |
4680 | which can produce *&x = 0. */ | |
4681 | ||
4682 | bool | |
4683 | fold_stmt (gimple_stmt_iterator *gsi) | |
4684 | { | |
55534d34 | 4685 | return fold_stmt_1 (gsi, false, no_follow_ssa_edges); |
4686 | } | |
4687 | ||
4688 | bool | |
4689 | fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree)) | |
4690 | { | |
4691 | return fold_stmt_1 (gsi, false, valueize); | |
2d18b16d | 4692 | } |
4693 | ||
50aacf4c | 4694 | /* Perform the minimal folding on statement *GSI. Only operations like |
2d18b16d | 4695 | *&x created by constant propagation are handled. The statement cannot |
4696 | be replaced with a new one. Return true if the statement was | |
4697 | changed, false otherwise. | |
50aacf4c | 4698 | The statement *GSI should be in valid gimple form but may |
2d18b16d | 4699 | be in unfolded state as resulting from for example constant propagation |
4700 | which can produce *&x = 0. */ | |
4701 | ||
4702 | bool | |
50aacf4c | 4703 | fold_stmt_inplace (gimple_stmt_iterator *gsi) |
2d18b16d | 4704 | { |
42acab1c | 4705 | gimple *stmt = gsi_stmt (*gsi); |
55534d34 | 4706 | bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges); |
50aacf4c | 4707 | gcc_assert (gsi_stmt (*gsi) == stmt); |
2d18b16d | 4708 | return changed; |
4709 | } | |
4710 | ||
c82d157a | 4711 | /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE |
4712 | if EXPR is null or we don't know how. | |
4713 | If non-null, the result always has boolean type. */ | |
4714 | ||
4715 | static tree | |
4716 | canonicalize_bool (tree expr, bool invert) | |
4717 | { | |
4718 | if (!expr) | |
4719 | return NULL_TREE; | |
4720 | else if (invert) | |
4721 | { | |
4722 | if (integer_nonzerop (expr)) | |
4723 | return boolean_false_node; | |
4724 | else if (integer_zerop (expr)) | |
4725 | return boolean_true_node; | |
4726 | else if (TREE_CODE (expr) == SSA_NAME) | |
4727 | return fold_build2 (EQ_EXPR, boolean_type_node, expr, | |
4728 | build_int_cst (TREE_TYPE (expr), 0)); | |
21c8a0ab | 4729 | else if (COMPARISON_CLASS_P (expr)) |
c82d157a | 4730 | return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false), |
4731 | boolean_type_node, | |
4732 | TREE_OPERAND (expr, 0), | |
4733 | TREE_OPERAND (expr, 1)); | |
4734 | else | |
4735 | return NULL_TREE; | |
4736 | } | |
4737 | else | |
4738 | { | |
4739 | if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
4740 | return expr; | |
4741 | if (integer_nonzerop (expr)) | |
4742 | return boolean_true_node; | |
4743 | else if (integer_zerop (expr)) | |
4744 | return boolean_false_node; | |
4745 | else if (TREE_CODE (expr) == SSA_NAME) | |
4746 | return fold_build2 (NE_EXPR, boolean_type_node, expr, | |
4747 | build_int_cst (TREE_TYPE (expr), 0)); | |
21c8a0ab | 4748 | else if (COMPARISON_CLASS_P (expr)) |
c82d157a | 4749 | return fold_build2 (TREE_CODE (expr), |
4750 | boolean_type_node, | |
4751 | TREE_OPERAND (expr, 0), | |
4752 | TREE_OPERAND (expr, 1)); | |
4753 | else | |
4754 | return NULL_TREE; | |
4755 | } | |
4756 | } | |
4757 | ||
4758 | /* Check to see if a boolean expression EXPR is logically equivalent to the | |
4759 | comparison (OP1 CODE OP2). Check for various identities involving | |
4760 | SSA_NAMEs. */ | |
4761 | ||
4762 | static bool | |
4763 | same_bool_comparison_p (const_tree expr, enum tree_code code, | |
4764 | const_tree op1, const_tree op2) | |
4765 | { | |
42acab1c | 4766 | gimple *s; |
c82d157a | 4767 | |
4768 | /* The obvious case. */ | |
4769 | if (TREE_CODE (expr) == code | |
4770 | && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0) | |
4771 | && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0)) | |
4772 | return true; | |
4773 | ||
4774 | /* Check for comparing (name, name != 0) and the case where expr | |
4775 | is an SSA_NAME with a definition matching the comparison. */ | |
4776 | if (TREE_CODE (expr) == SSA_NAME | |
4777 | && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE) | |
4778 | { | |
4779 | if (operand_equal_p (expr, op1, 0)) | |
4780 | return ((code == NE_EXPR && integer_zerop (op2)) | |
4781 | || (code == EQ_EXPR && integer_nonzerop (op2))); | |
4782 | s = SSA_NAME_DEF_STMT (expr); | |
4783 | if (is_gimple_assign (s) | |
4784 | && gimple_assign_rhs_code (s) == code | |
4785 | && operand_equal_p (gimple_assign_rhs1 (s), op1, 0) | |
4786 | && operand_equal_p (gimple_assign_rhs2 (s), op2, 0)) | |
4787 | return true; | |
4788 | } | |
4789 | ||
4790 | /* If op1 is of the form (name != 0) or (name == 0), and the definition | |
4791 | of name is a comparison, recurse. */ | |
4792 | if (TREE_CODE (op1) == SSA_NAME | |
4793 | && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE) | |
4794 | { | |
4795 | s = SSA_NAME_DEF_STMT (op1); | |
4796 | if (is_gimple_assign (s) | |
4797 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison) | |
4798 | { | |
4799 | enum tree_code c = gimple_assign_rhs_code (s); | |
4800 | if ((c == NE_EXPR && integer_zerop (op2)) | |
4801 | || (c == EQ_EXPR && integer_nonzerop (op2))) | |
4802 | return same_bool_comparison_p (expr, c, | |
4803 | gimple_assign_rhs1 (s), | |
4804 | gimple_assign_rhs2 (s)); | |
4805 | if ((c == EQ_EXPR && integer_zerop (op2)) | |
4806 | || (c == NE_EXPR && integer_nonzerop (op2))) | |
4807 | return same_bool_comparison_p (expr, | |
4808 | invert_tree_comparison (c, false), | |
4809 | gimple_assign_rhs1 (s), | |
4810 | gimple_assign_rhs2 (s)); | |
4811 | } | |
4812 | } | |
4813 | return false; | |
4814 | } | |
4815 | ||
4816 | /* Check to see if two boolean expressions OP1 and OP2 are logically | |
4817 | equivalent. */ | |
4818 | ||
4819 | static bool | |
4820 | same_bool_result_p (const_tree op1, const_tree op2) | |
4821 | { | |
4822 | /* Simple cases first. */ | |
4823 | if (operand_equal_p (op1, op2, 0)) | |
4824 | return true; | |
4825 | ||
4826 | /* Check the cases where at least one of the operands is a comparison. | |
4827 | These are a bit smarter than operand_equal_p in that they apply some | |
4828 | identifies on SSA_NAMEs. */ | |
21c8a0ab | 4829 | if (COMPARISON_CLASS_P (op2) |
c82d157a | 4830 | && same_bool_comparison_p (op1, TREE_CODE (op2), |
4831 | TREE_OPERAND (op2, 0), | |
4832 | TREE_OPERAND (op2, 1))) | |
4833 | return true; | |
21c8a0ab | 4834 | if (COMPARISON_CLASS_P (op1) |
c82d157a | 4835 | && same_bool_comparison_p (op2, TREE_CODE (op1), |
4836 | TREE_OPERAND (op1, 0), | |
4837 | TREE_OPERAND (op1, 1))) | |
4838 | return true; | |
4839 | ||
4840 | /* Default case. */ | |
4841 | return false; | |
4842 | } | |
4843 | ||
4844 | /* Forward declarations for some mutually recursive functions. */ | |
4845 | ||
4846 | static tree | |
4847 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
4848 | enum tree_code code2, tree op2a, tree op2b); | |
4849 | static tree | |
4850 | and_var_with_comparison (tree var, bool invert, | |
4851 | enum tree_code code2, tree op2a, tree op2b); | |
4852 | static tree | |
42acab1c | 4853 | and_var_with_comparison_1 (gimple *stmt, |
c82d157a | 4854 | enum tree_code code2, tree op2a, tree op2b); |
4855 | static tree | |
4856 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
4857 | enum tree_code code2, tree op2a, tree op2b); | |
4858 | static tree | |
4859 | or_var_with_comparison (tree var, bool invert, | |
4860 | enum tree_code code2, tree op2a, tree op2b); | |
4861 | static tree | |
42acab1c | 4862 | or_var_with_comparison_1 (gimple *stmt, |
c82d157a | 4863 | enum tree_code code2, tree op2a, tree op2b); |
4864 | ||
4865 | /* Helper function for and_comparisons_1: try to simplify the AND of the | |
4866 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
4867 | If INVERT is true, invert the value of the VAR before doing the AND. | |
4868 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
4869 | ||
4870 | static tree | |
4871 | and_var_with_comparison (tree var, bool invert, | |
4872 | enum tree_code code2, tree op2a, tree op2b) | |
4873 | { | |
4874 | tree t; | |
42acab1c | 4875 | gimple *stmt = SSA_NAME_DEF_STMT (var); |
c82d157a | 4876 | |
4877 | /* We can only deal with variables whose definitions are assignments. */ | |
4878 | if (!is_gimple_assign (stmt)) | |
4879 | return NULL_TREE; | |
4880 | ||
4881 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
4882 | !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b)) | |
4883 | Then we only have to consider the simpler non-inverted cases. */ | |
4884 | if (invert) | |
4885 | t = or_var_with_comparison_1 (stmt, | |
4886 | invert_tree_comparison (code2, false), | |
4887 | op2a, op2b); | |
4888 | else | |
4889 | t = and_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
4890 | return canonicalize_bool (t, invert); | |
4891 | } | |
4892 | ||
4893 | /* Try to simplify the AND of the ssa variable defined by the assignment | |
4894 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
4895 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
4896 | ||
4897 | static tree | |
42acab1c | 4898 | and_var_with_comparison_1 (gimple *stmt, |
c82d157a | 4899 | enum tree_code code2, tree op2a, tree op2b) |
4900 | { | |
4901 | tree var = gimple_assign_lhs (stmt); | |
4902 | tree true_test_var = NULL_TREE; | |
4903 | tree false_test_var = NULL_TREE; | |
4904 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
4905 | ||
4906 | /* Check for identities like (var AND (var == 0)) => false. */ | |
4907 | if (TREE_CODE (op2a) == SSA_NAME | |
4908 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
4909 | { | |
4910 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
4911 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
4912 | { | |
4913 | true_test_var = op2a; | |
4914 | if (var == true_test_var) | |
4915 | return var; | |
4916 | } | |
4917 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
4918 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
4919 | { | |
4920 | false_test_var = op2a; | |
4921 | if (var == false_test_var) | |
4922 | return boolean_false_node; | |
4923 | } | |
4924 | } | |
4925 | ||
4926 | /* If the definition is a comparison, recurse on it. */ | |
4927 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
4928 | { | |
4929 | tree t = and_comparisons_1 (innercode, | |
4930 | gimple_assign_rhs1 (stmt), | |
4931 | gimple_assign_rhs2 (stmt), | |
4932 | code2, | |
4933 | op2a, | |
4934 | op2b); | |
4935 | if (t) | |
4936 | return t; | |
4937 | } | |
4938 | ||
4939 | /* If the definition is an AND or OR expression, we may be able to | |
4940 | simplify by reassociating. */ | |
08410c65 | 4941 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
4942 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
c82d157a | 4943 | { |
4944 | tree inner1 = gimple_assign_rhs1 (stmt); | |
4945 | tree inner2 = gimple_assign_rhs2 (stmt); | |
42acab1c | 4946 | gimple *s; |
c82d157a | 4947 | tree t; |
4948 | tree partial = NULL_TREE; | |
08410c65 | 4949 | bool is_and = (innercode == BIT_AND_EXPR); |
c82d157a | 4950 | |
4951 | /* Check for boolean identities that don't require recursive examination | |
4952 | of inner1/inner2: | |
4953 | inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var | |
4954 | inner1 AND (inner1 OR inner2) => inner1 | |
4955 | !inner1 AND (inner1 AND inner2) => false | |
4956 | !inner1 AND (inner1 OR inner2) => !inner1 AND inner2 | |
4957 | Likewise for similar cases involving inner2. */ | |
4958 | if (inner1 == true_test_var) | |
4959 | return (is_and ? var : inner1); | |
4960 | else if (inner2 == true_test_var) | |
4961 | return (is_and ? var : inner2); | |
4962 | else if (inner1 == false_test_var) | |
4963 | return (is_and | |
4964 | ? boolean_false_node | |
4965 | : and_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
4966 | else if (inner2 == false_test_var) | |
4967 | return (is_and | |
4968 | ? boolean_false_node | |
4969 | : and_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
4970 | ||
4971 | /* Next, redistribute/reassociate the AND across the inner tests. | |
4972 | Compute the first partial result, (inner1 AND (op2a code op2b)) */ | |
4973 | if (TREE_CODE (inner1) == SSA_NAME | |
4974 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
4975 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
4976 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
4977 | gimple_assign_rhs1 (s), | |
4978 | gimple_assign_rhs2 (s), | |
4979 | code2, op2a, op2b))) | |
4980 | { | |
4981 | /* Handle the AND case, where we are reassociating: | |
4982 | (inner1 AND inner2) AND (op2a code2 op2b) | |
4983 | => (t AND inner2) | |
4984 | If the partial result t is a constant, we win. Otherwise | |
4985 | continue on to try reassociating with the other inner test. */ | |
4986 | if (is_and) | |
4987 | { | |
4988 | if (integer_onep (t)) | |
4989 | return inner2; | |
4990 | else if (integer_zerop (t)) | |
4991 | return boolean_false_node; | |
4992 | } | |
4993 | ||
4994 | /* Handle the OR case, where we are redistributing: | |
4995 | (inner1 OR inner2) AND (op2a code2 op2b) | |
4996 | => (t OR (inner2 AND (op2a code2 op2b))) */ | |
ad1e8581 | 4997 | else if (integer_onep (t)) |
4998 | return boolean_true_node; | |
4999 | ||
5000 | /* Save partial result for later. */ | |
5001 | partial = t; | |
c82d157a | 5002 | } |
5003 | ||
5004 | /* Compute the second partial result, (inner2 AND (op2a code op2b)) */ | |
5005 | if (TREE_CODE (inner2) == SSA_NAME | |
5006 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
5007 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
5008 | && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s), | |
5009 | gimple_assign_rhs1 (s), | |
5010 | gimple_assign_rhs2 (s), | |
5011 | code2, op2a, op2b))) | |
5012 | { | |
5013 | /* Handle the AND case, where we are reassociating: | |
5014 | (inner1 AND inner2) AND (op2a code2 op2b) | |
5015 | => (inner1 AND t) */ | |
5016 | if (is_and) | |
5017 | { | |
5018 | if (integer_onep (t)) | |
5019 | return inner1; | |
5020 | else if (integer_zerop (t)) | |
5021 | return boolean_false_node; | |
ad1e8581 | 5022 | /* If both are the same, we can apply the identity |
5023 | (x AND x) == x. */ | |
5024 | else if (partial && same_bool_result_p (t, partial)) | |
5025 | return t; | |
c82d157a | 5026 | } |
5027 | ||
5028 | /* Handle the OR case. where we are redistributing: | |
5029 | (inner1 OR inner2) AND (op2a code2 op2b) | |
5030 | => (t OR (inner1 AND (op2a code2 op2b))) | |
5031 | => (t OR partial) */ | |
5032 | else | |
5033 | { | |
5034 | if (integer_onep (t)) | |
5035 | return boolean_true_node; | |
5036 | else if (partial) | |
5037 | { | |
5038 | /* We already got a simplification for the other | |
5039 | operand to the redistributed OR expression. The | |
5040 | interesting case is when at least one is false. | |
5041 | Or, if both are the same, we can apply the identity | |
5042 | (x OR x) == x. */ | |
5043 | if (integer_zerop (partial)) | |
5044 | return t; | |
5045 | else if (integer_zerop (t)) | |
5046 | return partial; | |
5047 | else if (same_bool_result_p (t, partial)) | |
5048 | return t; | |
5049 | } | |
5050 | } | |
5051 | } | |
5052 | } | |
5053 | return NULL_TREE; | |
5054 | } | |
5055 | ||
5056 | /* Try to simplify the AND of two comparisons defined by | |
5057 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
5058 | If this can be done without constructing an intermediate value, | |
5059 | return the resulting tree; otherwise NULL_TREE is returned. | |
5060 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
5061 | in the first comparison but not the second. */ | |
5062 | ||
5063 | static tree | |
5064 | and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
5065 | enum tree_code code2, tree op2a, tree op2b) | |
5066 | { | |
f2c1848b | 5067 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
357d8e5d | 5068 | |
c82d157a | 5069 | /* First check for ((x CODE1 y) AND (x CODE2 y)). */ |
5070 | if (operand_equal_p (op1a, op2a, 0) | |
5071 | && operand_equal_p (op1b, op2b, 0)) | |
5072 | { | |
08410c65 | 5073 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 5074 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
5075 | TRUTH_ANDIF_EXPR, code1, code2, | |
357d8e5d | 5076 | truth_type, op1a, op1b); |
c82d157a | 5077 | if (t) |
5078 | return t; | |
5079 | } | |
5080 | ||
5081 | /* Likewise the swapped case of the above. */ | |
5082 | if (operand_equal_p (op1a, op2b, 0) | |
5083 | && operand_equal_p (op1b, op2a, 0)) | |
5084 | { | |
08410c65 | 5085 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 5086 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
5087 | TRUTH_ANDIF_EXPR, code1, | |
5088 | swap_tree_comparison (code2), | |
357d8e5d | 5089 | truth_type, op1a, op1b); |
c82d157a | 5090 | if (t) |
5091 | return t; | |
5092 | } | |
5093 | ||
5094 | /* If both comparisons are of the same value against constants, we might | |
5095 | be able to merge them. */ | |
5096 | if (operand_equal_p (op1a, op2a, 0) | |
5097 | && TREE_CODE (op1b) == INTEGER_CST | |
5098 | && TREE_CODE (op2b) == INTEGER_CST) | |
5099 | { | |
5100 | int cmp = tree_int_cst_compare (op1b, op2b); | |
5101 | ||
5102 | /* If we have (op1a == op1b), we should either be able to | |
5103 | return that or FALSE, depending on whether the constant op1b | |
5104 | also satisfies the other comparison against op2b. */ | |
5105 | if (code1 == EQ_EXPR) | |
5106 | { | |
5107 | bool done = true; | |
5108 | bool val; | |
5109 | switch (code2) | |
5110 | { | |
5111 | case EQ_EXPR: val = (cmp == 0); break; | |
5112 | case NE_EXPR: val = (cmp != 0); break; | |
5113 | case LT_EXPR: val = (cmp < 0); break; | |
5114 | case GT_EXPR: val = (cmp > 0); break; | |
5115 | case LE_EXPR: val = (cmp <= 0); break; | |
5116 | case GE_EXPR: val = (cmp >= 0); break; | |
5117 | default: done = false; | |
5118 | } | |
5119 | if (done) | |
5120 | { | |
5121 | if (val) | |
5122 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5123 | else | |
5124 | return boolean_false_node; | |
5125 | } | |
5126 | } | |
5127 | /* Likewise if the second comparison is an == comparison. */ | |
5128 | else if (code2 == EQ_EXPR) | |
5129 | { | |
5130 | bool done = true; | |
5131 | bool val; | |
5132 | switch (code1) | |
5133 | { | |
5134 | case EQ_EXPR: val = (cmp == 0); break; | |
5135 | case NE_EXPR: val = (cmp != 0); break; | |
5136 | case LT_EXPR: val = (cmp > 0); break; | |
5137 | case GT_EXPR: val = (cmp < 0); break; | |
5138 | case LE_EXPR: val = (cmp >= 0); break; | |
5139 | case GE_EXPR: val = (cmp <= 0); break; | |
5140 | default: done = false; | |
5141 | } | |
5142 | if (done) | |
5143 | { | |
5144 | if (val) | |
5145 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5146 | else | |
5147 | return boolean_false_node; | |
5148 | } | |
5149 | } | |
5150 | ||
5151 | /* Same business with inequality tests. */ | |
5152 | else if (code1 == NE_EXPR) | |
5153 | { | |
5154 | bool val; | |
5155 | switch (code2) | |
5156 | { | |
5157 | case EQ_EXPR: val = (cmp != 0); break; | |
5158 | case NE_EXPR: val = (cmp == 0); break; | |
5159 | case LT_EXPR: val = (cmp >= 0); break; | |
5160 | case GT_EXPR: val = (cmp <= 0); break; | |
5161 | case LE_EXPR: val = (cmp > 0); break; | |
5162 | case GE_EXPR: val = (cmp < 0); break; | |
5163 | default: | |
5164 | val = false; | |
5165 | } | |
5166 | if (val) | |
5167 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5168 | } | |
5169 | else if (code2 == NE_EXPR) | |
5170 | { | |
5171 | bool val; | |
5172 | switch (code1) | |
5173 | { | |
5174 | case EQ_EXPR: val = (cmp == 0); break; | |
5175 | case NE_EXPR: val = (cmp != 0); break; | |
5176 | case LT_EXPR: val = (cmp <= 0); break; | |
5177 | case GT_EXPR: val = (cmp >= 0); break; | |
5178 | case LE_EXPR: val = (cmp < 0); break; | |
5179 | case GE_EXPR: val = (cmp > 0); break; | |
5180 | default: | |
5181 | val = false; | |
5182 | } | |
5183 | if (val) | |
5184 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5185 | } | |
5186 | ||
5187 | /* Chose the more restrictive of two < or <= comparisons. */ | |
5188 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
5189 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
5190 | { | |
5191 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
5192 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5193 | else | |
5194 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5195 | } | |
5196 | ||
5197 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
5198 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
5199 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
5200 | { | |
5201 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
5202 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5203 | else | |
5204 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5205 | } | |
5206 | ||
5207 | /* Check for singleton ranges. */ | |
5208 | else if (cmp == 0 | |
5209 | && ((code1 == LE_EXPR && code2 == GE_EXPR) | |
5210 | || (code1 == GE_EXPR && code2 == LE_EXPR))) | |
5211 | return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b); | |
5212 | ||
5213 | /* Check for disjoint ranges. */ | |
5214 | else if (cmp <= 0 | |
5215 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
5216 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
5217 | return boolean_false_node; | |
5218 | else if (cmp >= 0 | |
5219 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
5220 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
5221 | return boolean_false_node; | |
5222 | } | |
5223 | ||
5224 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
5225 | NAME's definition is a truth value. See if there are any simplifications | |
5226 | that can be done against the NAME's definition. */ | |
5227 | if (TREE_CODE (op1a) == SSA_NAME | |
5228 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
5229 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
5230 | { | |
5231 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
5232 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
42acab1c | 5233 | gimple *stmt = SSA_NAME_DEF_STMT (op1a); |
c82d157a | 5234 | switch (gimple_code (stmt)) |
5235 | { | |
5236 | case GIMPLE_ASSIGN: | |
5237 | /* Try to simplify by copy-propagating the definition. */ | |
5238 | return and_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
5239 | ||
5240 | case GIMPLE_PHI: | |
5241 | /* If every argument to the PHI produces the same result when | |
5242 | ANDed with the second comparison, we win. | |
5243 | Do not do this unless the type is bool since we need a bool | |
5244 | result here anyway. */ | |
5245 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
5246 | { | |
5247 | tree result = NULL_TREE; | |
5248 | unsigned i; | |
5249 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
5250 | { | |
5251 | tree arg = gimple_phi_arg_def (stmt, i); | |
5252 | ||
5253 | /* If this PHI has itself as an argument, ignore it. | |
5254 | If all the other args produce the same result, | |
5255 | we're still OK. */ | |
5256 | if (arg == gimple_phi_result (stmt)) | |
5257 | continue; | |
5258 | else if (TREE_CODE (arg) == INTEGER_CST) | |
5259 | { | |
5260 | if (invert ? integer_nonzerop (arg) : integer_zerop (arg)) | |
5261 | { | |
5262 | if (!result) | |
5263 | result = boolean_false_node; | |
5264 | else if (!integer_zerop (result)) | |
5265 | return NULL_TREE; | |
5266 | } | |
5267 | else if (!result) | |
5268 | result = fold_build2 (code2, boolean_type_node, | |
5269 | op2a, op2b); | |
5270 | else if (!same_bool_comparison_p (result, | |
5271 | code2, op2a, op2b)) | |
5272 | return NULL_TREE; | |
5273 | } | |
ae4330d7 | 5274 | else if (TREE_CODE (arg) == SSA_NAME |
5275 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
c82d157a | 5276 | { |
8a245b9d | 5277 | tree temp; |
42acab1c | 5278 | gimple *def_stmt = SSA_NAME_DEF_STMT (arg); |
8a245b9d | 5279 | /* In simple cases we can look through PHI nodes, |
5280 | but we have to be careful with loops. | |
5281 | See PR49073. */ | |
5282 | if (! dom_info_available_p (CDI_DOMINATORS) | |
5283 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
5284 | || dominated_by_p (CDI_DOMINATORS, | |
5285 | gimple_bb (def_stmt), | |
5286 | gimple_bb (stmt))) | |
5287 | return NULL_TREE; | |
5288 | temp = and_var_with_comparison (arg, invert, code2, | |
5289 | op2a, op2b); | |
c82d157a | 5290 | if (!temp) |
5291 | return NULL_TREE; | |
5292 | else if (!result) | |
5293 | result = temp; | |
5294 | else if (!same_bool_result_p (result, temp)) | |
5295 | return NULL_TREE; | |
5296 | } | |
5297 | else | |
5298 | return NULL_TREE; | |
5299 | } | |
5300 | return result; | |
5301 | } | |
5302 | ||
5303 | default: | |
5304 | break; | |
5305 | } | |
5306 | } | |
5307 | return NULL_TREE; | |
5308 | } | |
5309 | ||
5310 | /* Try to simplify the AND of two comparisons, specified by | |
5311 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
5312 | If this can be simplified to a single expression (without requiring | |
5313 | introducing more SSA variables to hold intermediate values), | |
5314 | return the resulting tree. Otherwise return NULL_TREE. | |
5315 | If the result expression is non-null, it has boolean type. */ | |
5316 | ||
5317 | tree | |
5318 | maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
5319 | enum tree_code code2, tree op2a, tree op2b) | |
5320 | { | |
5321 | tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
5322 | if (t) | |
5323 | return t; | |
5324 | else | |
5325 | return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
5326 | } | |
5327 | ||
5328 | /* Helper function for or_comparisons_1: try to simplify the OR of the | |
5329 | ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B). | |
5330 | If INVERT is true, invert the value of VAR before doing the OR. | |
5331 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
5332 | ||
5333 | static tree | |
5334 | or_var_with_comparison (tree var, bool invert, | |
5335 | enum tree_code code2, tree op2a, tree op2b) | |
5336 | { | |
5337 | tree t; | |
42acab1c | 5338 | gimple *stmt = SSA_NAME_DEF_STMT (var); |
c82d157a | 5339 | |
5340 | /* We can only deal with variables whose definitions are assignments. */ | |
5341 | if (!is_gimple_assign (stmt)) | |
5342 | return NULL_TREE; | |
5343 | ||
5344 | /* If we have an inverted comparison, apply DeMorgan's law and rewrite | |
5345 | !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b)) | |
5346 | Then we only have to consider the simpler non-inverted cases. */ | |
5347 | if (invert) | |
5348 | t = and_var_with_comparison_1 (stmt, | |
5349 | invert_tree_comparison (code2, false), | |
5350 | op2a, op2b); | |
5351 | else | |
5352 | t = or_var_with_comparison_1 (stmt, code2, op2a, op2b); | |
5353 | return canonicalize_bool (t, invert); | |
5354 | } | |
5355 | ||
5356 | /* Try to simplify the OR of the ssa variable defined by the assignment | |
5357 | STMT with the comparison specified by (OP2A CODE2 OP2B). | |
5358 | Return NULL_EXPR if we can't simplify this to a single expression. */ | |
5359 | ||
5360 | static tree | |
42acab1c | 5361 | or_var_with_comparison_1 (gimple *stmt, |
c82d157a | 5362 | enum tree_code code2, tree op2a, tree op2b) |
5363 | { | |
5364 | tree var = gimple_assign_lhs (stmt); | |
5365 | tree true_test_var = NULL_TREE; | |
5366 | tree false_test_var = NULL_TREE; | |
5367 | enum tree_code innercode = gimple_assign_rhs_code (stmt); | |
5368 | ||
5369 | /* Check for identities like (var OR (var != 0)) => true . */ | |
5370 | if (TREE_CODE (op2a) == SSA_NAME | |
5371 | && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) | |
5372 | { | |
5373 | if ((code2 == NE_EXPR && integer_zerop (op2b)) | |
5374 | || (code2 == EQ_EXPR && integer_nonzerop (op2b))) | |
5375 | { | |
5376 | true_test_var = op2a; | |
5377 | if (var == true_test_var) | |
5378 | return var; | |
5379 | } | |
5380 | else if ((code2 == EQ_EXPR && integer_zerop (op2b)) | |
5381 | || (code2 == NE_EXPR && integer_nonzerop (op2b))) | |
5382 | { | |
5383 | false_test_var = op2a; | |
5384 | if (var == false_test_var) | |
5385 | return boolean_true_node; | |
5386 | } | |
5387 | } | |
5388 | ||
5389 | /* If the definition is a comparison, recurse on it. */ | |
5390 | if (TREE_CODE_CLASS (innercode) == tcc_comparison) | |
5391 | { | |
5392 | tree t = or_comparisons_1 (innercode, | |
5393 | gimple_assign_rhs1 (stmt), | |
5394 | gimple_assign_rhs2 (stmt), | |
5395 | code2, | |
5396 | op2a, | |
5397 | op2b); | |
5398 | if (t) | |
5399 | return t; | |
5400 | } | |
5401 | ||
5402 | /* If the definition is an AND or OR expression, we may be able to | |
5403 | simplify by reassociating. */ | |
08410c65 | 5404 | if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
5405 | && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR)) | |
c82d157a | 5406 | { |
5407 | tree inner1 = gimple_assign_rhs1 (stmt); | |
5408 | tree inner2 = gimple_assign_rhs2 (stmt); | |
42acab1c | 5409 | gimple *s; |
c82d157a | 5410 | tree t; |
5411 | tree partial = NULL_TREE; | |
08410c65 | 5412 | bool is_or = (innercode == BIT_IOR_EXPR); |
c82d157a | 5413 | |
5414 | /* Check for boolean identities that don't require recursive examination | |
5415 | of inner1/inner2: | |
5416 | inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var | |
5417 | inner1 OR (inner1 AND inner2) => inner1 | |
5418 | !inner1 OR (inner1 OR inner2) => true | |
5419 | !inner1 OR (inner1 AND inner2) => !inner1 OR inner2 | |
5420 | */ | |
5421 | if (inner1 == true_test_var) | |
5422 | return (is_or ? var : inner1); | |
5423 | else if (inner2 == true_test_var) | |
5424 | return (is_or ? var : inner2); | |
5425 | else if (inner1 == false_test_var) | |
5426 | return (is_or | |
5427 | ? boolean_true_node | |
5428 | : or_var_with_comparison (inner2, false, code2, op2a, op2b)); | |
5429 | else if (inner2 == false_test_var) | |
5430 | return (is_or | |
5431 | ? boolean_true_node | |
5432 | : or_var_with_comparison (inner1, false, code2, op2a, op2b)); | |
5433 | ||
5434 | /* Next, redistribute/reassociate the OR across the inner tests. | |
5435 | Compute the first partial result, (inner1 OR (op2a code op2b)) */ | |
5436 | if (TREE_CODE (inner1) == SSA_NAME | |
5437 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1)) | |
5438 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
5439 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
5440 | gimple_assign_rhs1 (s), | |
5441 | gimple_assign_rhs2 (s), | |
5442 | code2, op2a, op2b))) | |
5443 | { | |
5444 | /* Handle the OR case, where we are reassociating: | |
5445 | (inner1 OR inner2) OR (op2a code2 op2b) | |
5446 | => (t OR inner2) | |
5447 | If the partial result t is a constant, we win. Otherwise | |
5448 | continue on to try reassociating with the other inner test. */ | |
ad1e8581 | 5449 | if (is_or) |
c82d157a | 5450 | { |
5451 | if (integer_onep (t)) | |
5452 | return boolean_true_node; | |
5453 | else if (integer_zerop (t)) | |
5454 | return inner2; | |
5455 | } | |
5456 | ||
5457 | /* Handle the AND case, where we are redistributing: | |
5458 | (inner1 AND inner2) OR (op2a code2 op2b) | |
5459 | => (t AND (inner2 OR (op2a code op2b))) */ | |
ad1e8581 | 5460 | else if (integer_zerop (t)) |
5461 | return boolean_false_node; | |
5462 | ||
5463 | /* Save partial result for later. */ | |
5464 | partial = t; | |
c82d157a | 5465 | } |
5466 | ||
5467 | /* Compute the second partial result, (inner2 OR (op2a code op2b)) */ | |
5468 | if (TREE_CODE (inner2) == SSA_NAME | |
5469 | && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2)) | |
5470 | && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison | |
5471 | && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s), | |
5472 | gimple_assign_rhs1 (s), | |
5473 | gimple_assign_rhs2 (s), | |
5474 | code2, op2a, op2b))) | |
5475 | { | |
5476 | /* Handle the OR case, where we are reassociating: | |
5477 | (inner1 OR inner2) OR (op2a code2 op2b) | |
ad1e8581 | 5478 | => (inner1 OR t) |
5479 | => (t OR partial) */ | |
5480 | if (is_or) | |
c82d157a | 5481 | { |
5482 | if (integer_zerop (t)) | |
5483 | return inner1; | |
5484 | else if (integer_onep (t)) | |
5485 | return boolean_true_node; | |
ad1e8581 | 5486 | /* If both are the same, we can apply the identity |
5487 | (x OR x) == x. */ | |
5488 | else if (partial && same_bool_result_p (t, partial)) | |
5489 | return t; | |
c82d157a | 5490 | } |
5491 | ||
5492 | /* Handle the AND case, where we are redistributing: | |
5493 | (inner1 AND inner2) OR (op2a code2 op2b) | |
5494 | => (t AND (inner1 OR (op2a code2 op2b))) | |
5495 | => (t AND partial) */ | |
5496 | else | |
5497 | { | |
5498 | if (integer_zerop (t)) | |
5499 | return boolean_false_node; | |
5500 | else if (partial) | |
5501 | { | |
5502 | /* We already got a simplification for the other | |
5503 | operand to the redistributed AND expression. The | |
5504 | interesting case is when at least one is true. | |
5505 | Or, if both are the same, we can apply the identity | |
ad1e8581 | 5506 | (x AND x) == x. */ |
c82d157a | 5507 | if (integer_onep (partial)) |
5508 | return t; | |
5509 | else if (integer_onep (t)) | |
5510 | return partial; | |
5511 | else if (same_bool_result_p (t, partial)) | |
ad1e8581 | 5512 | return t; |
c82d157a | 5513 | } |
5514 | } | |
5515 | } | |
5516 | } | |
5517 | return NULL_TREE; | |
5518 | } | |
5519 | ||
5520 | /* Try to simplify the OR of two comparisons defined by | |
5521 | (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively. | |
5522 | If this can be done without constructing an intermediate value, | |
5523 | return the resulting tree; otherwise NULL_TREE is returned. | |
5524 | This function is deliberately asymmetric as it recurses on SSA_DEFs | |
5525 | in the first comparison but not the second. */ | |
5526 | ||
5527 | static tree | |
5528 | or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b, | |
5529 | enum tree_code code2, tree op2a, tree op2b) | |
5530 | { | |
f2c1848b | 5531 | tree truth_type = truth_type_for (TREE_TYPE (op1a)); |
357d8e5d | 5532 | |
c82d157a | 5533 | /* First check for ((x CODE1 y) OR (x CODE2 y)). */ |
5534 | if (operand_equal_p (op1a, op2a, 0) | |
5535 | && operand_equal_p (op1b, op2b, 0)) | |
5536 | { | |
08410c65 | 5537 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 5538 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
5539 | TRUTH_ORIF_EXPR, code1, code2, | |
357d8e5d | 5540 | truth_type, op1a, op1b); |
c82d157a | 5541 | if (t) |
5542 | return t; | |
5543 | } | |
5544 | ||
5545 | /* Likewise the swapped case of the above. */ | |
5546 | if (operand_equal_p (op1a, op2b, 0) | |
5547 | && operand_equal_p (op1b, op2a, 0)) | |
5548 | { | |
08410c65 | 5549 | /* Result will be either NULL_TREE, or a combined comparison. */ |
c82d157a | 5550 | tree t = combine_comparisons (UNKNOWN_LOCATION, |
5551 | TRUTH_ORIF_EXPR, code1, | |
5552 | swap_tree_comparison (code2), | |
357d8e5d | 5553 | truth_type, op1a, op1b); |
c82d157a | 5554 | if (t) |
5555 | return t; | |
5556 | } | |
5557 | ||
5558 | /* If both comparisons are of the same value against constants, we might | |
5559 | be able to merge them. */ | |
5560 | if (operand_equal_p (op1a, op2a, 0) | |
5561 | && TREE_CODE (op1b) == INTEGER_CST | |
5562 | && TREE_CODE (op2b) == INTEGER_CST) | |
5563 | { | |
5564 | int cmp = tree_int_cst_compare (op1b, op2b); | |
5565 | ||
5566 | /* If we have (op1a != op1b), we should either be able to | |
5567 | return that or TRUE, depending on whether the constant op1b | |
5568 | also satisfies the other comparison against op2b. */ | |
5569 | if (code1 == NE_EXPR) | |
5570 | { | |
5571 | bool done = true; | |
5572 | bool val; | |
5573 | switch (code2) | |
5574 | { | |
5575 | case EQ_EXPR: val = (cmp == 0); break; | |
5576 | case NE_EXPR: val = (cmp != 0); break; | |
5577 | case LT_EXPR: val = (cmp < 0); break; | |
5578 | case GT_EXPR: val = (cmp > 0); break; | |
5579 | case LE_EXPR: val = (cmp <= 0); break; | |
5580 | case GE_EXPR: val = (cmp >= 0); break; | |
5581 | default: done = false; | |
5582 | } | |
5583 | if (done) | |
5584 | { | |
5585 | if (val) | |
5586 | return boolean_true_node; | |
5587 | else | |
5588 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5589 | } | |
5590 | } | |
5591 | /* Likewise if the second comparison is a != comparison. */ | |
5592 | else if (code2 == NE_EXPR) | |
5593 | { | |
5594 | bool done = true; | |
5595 | bool val; | |
5596 | switch (code1) | |
5597 | { | |
5598 | case EQ_EXPR: val = (cmp == 0); break; | |
5599 | case NE_EXPR: val = (cmp != 0); break; | |
5600 | case LT_EXPR: val = (cmp > 0); break; | |
5601 | case GT_EXPR: val = (cmp < 0); break; | |
5602 | case LE_EXPR: val = (cmp >= 0); break; | |
5603 | case GE_EXPR: val = (cmp <= 0); break; | |
5604 | default: done = false; | |
5605 | } | |
5606 | if (done) | |
5607 | { | |
5608 | if (val) | |
5609 | return boolean_true_node; | |
5610 | else | |
5611 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5612 | } | |
5613 | } | |
5614 | ||
5615 | /* See if an equality test is redundant with the other comparison. */ | |
5616 | else if (code1 == EQ_EXPR) | |
5617 | { | |
5618 | bool val; | |
5619 | switch (code2) | |
5620 | { | |
5621 | case EQ_EXPR: val = (cmp == 0); break; | |
5622 | case NE_EXPR: val = (cmp != 0); break; | |
5623 | case LT_EXPR: val = (cmp < 0); break; | |
5624 | case GT_EXPR: val = (cmp > 0); break; | |
5625 | case LE_EXPR: val = (cmp <= 0); break; | |
5626 | case GE_EXPR: val = (cmp >= 0); break; | |
5627 | default: | |
5628 | val = false; | |
5629 | } | |
5630 | if (val) | |
5631 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5632 | } | |
5633 | else if (code2 == EQ_EXPR) | |
5634 | { | |
5635 | bool val; | |
5636 | switch (code1) | |
5637 | { | |
5638 | case EQ_EXPR: val = (cmp == 0); break; | |
5639 | case NE_EXPR: val = (cmp != 0); break; | |
5640 | case LT_EXPR: val = (cmp > 0); break; | |
5641 | case GT_EXPR: val = (cmp < 0); break; | |
5642 | case LE_EXPR: val = (cmp >= 0); break; | |
5643 | case GE_EXPR: val = (cmp <= 0); break; | |
5644 | default: | |
5645 | val = false; | |
5646 | } | |
5647 | if (val) | |
5648 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5649 | } | |
5650 | ||
5651 | /* Chose the less restrictive of two < or <= comparisons. */ | |
5652 | else if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
5653 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
5654 | { | |
5655 | if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
5656 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5657 | else | |
5658 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5659 | } | |
5660 | ||
5661 | /* Likewise chose the less restrictive of two > or >= comparisons. */ | |
5662 | else if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
5663 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
5664 | { | |
5665 | if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
5666 | return fold_build2 (code2, boolean_type_node, op2a, op2b); | |
5667 | else | |
5668 | return fold_build2 (code1, boolean_type_node, op1a, op1b); | |
5669 | } | |
5670 | ||
5671 | /* Check for singleton ranges. */ | |
5672 | else if (cmp == 0 | |
5673 | && ((code1 == LT_EXPR && code2 == GT_EXPR) | |
5674 | || (code1 == GT_EXPR && code2 == LT_EXPR))) | |
5675 | return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b); | |
5676 | ||
5677 | /* Check for less/greater pairs that don't restrict the range at all. */ | |
5678 | else if (cmp >= 0 | |
5679 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
5680 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
5681 | return boolean_true_node; | |
5682 | else if (cmp <= 0 | |
5683 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
5684 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
5685 | return boolean_true_node; | |
5686 | } | |
5687 | ||
5688 | /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where | |
5689 | NAME's definition is a truth value. See if there are any simplifications | |
5690 | that can be done against the NAME's definition. */ | |
5691 | if (TREE_CODE (op1a) == SSA_NAME | |
5692 | && (code1 == NE_EXPR || code1 == EQ_EXPR) | |
5693 | && (integer_zerop (op1b) || integer_onep (op1b))) | |
5694 | { | |
5695 | bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b)) | |
5696 | || (code1 == NE_EXPR && integer_onep (op1b))); | |
42acab1c | 5697 | gimple *stmt = SSA_NAME_DEF_STMT (op1a); |
c82d157a | 5698 | switch (gimple_code (stmt)) |
5699 | { | |
5700 | case GIMPLE_ASSIGN: | |
5701 | /* Try to simplify by copy-propagating the definition. */ | |
5702 | return or_var_with_comparison (op1a, invert, code2, op2a, op2b); | |
5703 | ||
5704 | case GIMPLE_PHI: | |
5705 | /* If every argument to the PHI produces the same result when | |
5706 | ORed with the second comparison, we win. | |
5707 | Do not do this unless the type is bool since we need a bool | |
5708 | result here anyway. */ | |
5709 | if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE) | |
5710 | { | |
5711 | tree result = NULL_TREE; | |
5712 | unsigned i; | |
5713 | for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
5714 | { | |
5715 | tree arg = gimple_phi_arg_def (stmt, i); | |
5716 | ||
5717 | /* If this PHI has itself as an argument, ignore it. | |
5718 | If all the other args produce the same result, | |
5719 | we're still OK. */ | |
5720 | if (arg == gimple_phi_result (stmt)) | |
5721 | continue; | |
5722 | else if (TREE_CODE (arg) == INTEGER_CST) | |
5723 | { | |
5724 | if (invert ? integer_zerop (arg) : integer_nonzerop (arg)) | |
5725 | { | |
5726 | if (!result) | |
5727 | result = boolean_true_node; | |
5728 | else if (!integer_onep (result)) | |
5729 | return NULL_TREE; | |
5730 | } | |
5731 | else if (!result) | |
5732 | result = fold_build2 (code2, boolean_type_node, | |
5733 | op2a, op2b); | |
5734 | else if (!same_bool_comparison_p (result, | |
5735 | code2, op2a, op2b)) | |
5736 | return NULL_TREE; | |
5737 | } | |
ae4330d7 | 5738 | else if (TREE_CODE (arg) == SSA_NAME |
5739 | && !SSA_NAME_IS_DEFAULT_DEF (arg)) | |
c82d157a | 5740 | { |
8a245b9d | 5741 | tree temp; |
42acab1c | 5742 | gimple *def_stmt = SSA_NAME_DEF_STMT (arg); |
8a245b9d | 5743 | /* In simple cases we can look through PHI nodes, |
5744 | but we have to be careful with loops. | |
5745 | See PR49073. */ | |
5746 | if (! dom_info_available_p (CDI_DOMINATORS) | |
5747 | || gimple_bb (def_stmt) == gimple_bb (stmt) | |
5748 | || dominated_by_p (CDI_DOMINATORS, | |
5749 | gimple_bb (def_stmt), | |
5750 | gimple_bb (stmt))) | |
5751 | return NULL_TREE; | |
5752 | temp = or_var_with_comparison (arg, invert, code2, | |
5753 | op2a, op2b); | |
c82d157a | 5754 | if (!temp) |
5755 | return NULL_TREE; | |
5756 | else if (!result) | |
5757 | result = temp; | |
5758 | else if (!same_bool_result_p (result, temp)) | |
5759 | return NULL_TREE; | |
5760 | } | |
5761 | else | |
5762 | return NULL_TREE; | |
5763 | } | |
5764 | return result; | |
5765 | } | |
5766 | ||
5767 | default: | |
5768 | break; | |
5769 | } | |
5770 | } | |
5771 | return NULL_TREE; | |
5772 | } | |
5773 | ||
5774 | /* Try to simplify the OR of two comparisons, specified by | |
5775 | (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively. | |
5776 | If this can be simplified to a single expression (without requiring | |
5777 | introducing more SSA variables to hold intermediate values), | |
5778 | return the resulting tree. Otherwise return NULL_TREE. | |
5779 | If the result expression is non-null, it has boolean type. */ | |
5780 | ||
5781 | tree | |
5782 | maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b, | |
5783 | enum tree_code code2, tree op2a, tree op2b) | |
5784 | { | |
5785 | tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b); | |
5786 | if (t) | |
5787 | return t; | |
5788 | else | |
5789 | return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b); | |
5790 | } | |
1d0b727d | 5791 | |
5792 | ||
5793 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
5794 | ||
5795 | Either NULL_TREE, a simplified but non-constant or a constant | |
5796 | is returned. | |
5797 | ||
5798 | ??? This should go into a gimple-fold-inline.h file to be eventually | |
5799 | privatized with the single valueize function used in the various TUs | |
5800 | to avoid the indirect function call overhead. */ | |
5801 | ||
5802 | tree | |
42acab1c | 5803 | gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree), |
4e1b3545 | 5804 | tree (*gvalueize) (tree)) |
1d0b727d | 5805 | { |
b768d5ee | 5806 | code_helper rcode; |
5807 | tree ops[3] = {}; | |
5808 | /* ??? The SSA propagators do not correctly deal with following SSA use-def | |
5809 | edges if there are intermediate VARYING defs. For this reason | |
5810 | do not follow SSA edges here even though SCCVN can technically | |
5811 | just deal fine with that. */ | |
eb074ef3 | 5812 | if (gimple_simplify (stmt, &rcode, ops, NULL, gvalueize, valueize)) |
b768d5ee | 5813 | { |
eb074ef3 | 5814 | tree res = NULL_TREE; |
717ceeab | 5815 | if (gimple_simplified_result_is_gimple_val (rcode, ops)) |
eb074ef3 | 5816 | res = ops[0]; |
5817 | else if (mprts_hook) | |
5818 | res = mprts_hook (rcode, gimple_expr_type (stmt), ops); | |
5819 | if (res) | |
b768d5ee | 5820 | { |
eb074ef3 | 5821 | if (dump_file && dump_flags & TDF_DETAILS) |
5822 | { | |
5823 | fprintf (dump_file, "Match-and-simplified "); | |
5824 | print_gimple_expr (dump_file, stmt, 0, TDF_SLIM); | |
5825 | fprintf (dump_file, " to "); | |
1ffa4346 | 5826 | print_generic_expr (dump_file, res); |
eb074ef3 | 5827 | fprintf (dump_file, "\n"); |
5828 | } | |
5829 | return res; | |
b768d5ee | 5830 | } |
b768d5ee | 5831 | } |
5832 | ||
1d0b727d | 5833 | location_t loc = gimple_location (stmt); |
5834 | switch (gimple_code (stmt)) | |
5835 | { | |
5836 | case GIMPLE_ASSIGN: | |
5837 | { | |
5838 | enum tree_code subcode = gimple_assign_rhs_code (stmt); | |
5839 | ||
5840 | switch (get_gimple_rhs_class (subcode)) | |
5841 | { | |
5842 | case GIMPLE_SINGLE_RHS: | |
5843 | { | |
5844 | tree rhs = gimple_assign_rhs1 (stmt); | |
5845 | enum tree_code_class kind = TREE_CODE_CLASS (subcode); | |
5846 | ||
5847 | if (TREE_CODE (rhs) == SSA_NAME) | |
5848 | { | |
5849 | /* If the RHS is an SSA_NAME, return its known constant value, | |
5850 | if any. */ | |
5851 | return (*valueize) (rhs); | |
5852 | } | |
5853 | /* Handle propagating invariant addresses into address | |
5854 | operations. */ | |
5855 | else if (TREE_CODE (rhs) == ADDR_EXPR | |
5856 | && !is_gimple_min_invariant (rhs)) | |
5857 | { | |
03231f32 | 5858 | HOST_WIDE_INT offset = 0; |
1d0b727d | 5859 | tree base; |
5860 | base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0), | |
5861 | &offset, | |
5862 | valueize); | |
5863 | if (base | |
5864 | && (CONSTANT_CLASS_P (base) | |
5865 | || decl_address_invariant_p (base))) | |
5866 | return build_invariant_address (TREE_TYPE (rhs), | |
5867 | base, offset); | |
5868 | } | |
5869 | else if (TREE_CODE (rhs) == CONSTRUCTOR | |
5870 | && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE | |
5871 | && (CONSTRUCTOR_NELTS (rhs) | |
5872 | == TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs)))) | |
5873 | { | |
5874 | unsigned i; | |
fadf62f4 | 5875 | tree val, *vec; |
1d0b727d | 5876 | |
fadf62f4 | 5877 | vec = XALLOCAVEC (tree, |
5878 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))); | |
1d0b727d | 5879 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val) |
5880 | { | |
5881 | val = (*valueize) (val); | |
5882 | if (TREE_CODE (val) == INTEGER_CST | |
5883 | || TREE_CODE (val) == REAL_CST | |
5884 | || TREE_CODE (val) == FIXED_CST) | |
fadf62f4 | 5885 | vec[i] = val; |
1d0b727d | 5886 | else |
5887 | return NULL_TREE; | |
5888 | } | |
5889 | ||
fadf62f4 | 5890 | return build_vector (TREE_TYPE (rhs), vec); |
1d0b727d | 5891 | } |
0329fcdb | 5892 | if (subcode == OBJ_TYPE_REF) |
5893 | { | |
5894 | tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs)); | |
5895 | /* If callee is constant, we can fold away the wrapper. */ | |
5896 | if (is_gimple_min_invariant (val)) | |
5897 | return val; | |
5898 | } | |
1d0b727d | 5899 | |
5900 | if (kind == tcc_reference) | |
5901 | { | |
5902 | if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR | |
5903 | || TREE_CODE (rhs) == REALPART_EXPR | |
5904 | || TREE_CODE (rhs) == IMAGPART_EXPR) | |
5905 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
5906 | { | |
5907 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
5908 | return fold_unary_loc (EXPR_LOCATION (rhs), | |
5909 | TREE_CODE (rhs), | |
5910 | TREE_TYPE (rhs), val); | |
5911 | } | |
5912 | else if (TREE_CODE (rhs) == BIT_FIELD_REF | |
5913 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
5914 | { | |
5915 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
5916 | return fold_ternary_loc (EXPR_LOCATION (rhs), | |
5917 | TREE_CODE (rhs), | |
5918 | TREE_TYPE (rhs), val, | |
5919 | TREE_OPERAND (rhs, 1), | |
5920 | TREE_OPERAND (rhs, 2)); | |
5921 | } | |
5922 | else if (TREE_CODE (rhs) == MEM_REF | |
5923 | && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME) | |
5924 | { | |
5925 | tree val = (*valueize) (TREE_OPERAND (rhs, 0)); | |
5926 | if (TREE_CODE (val) == ADDR_EXPR | |
5927 | && is_gimple_min_invariant (val)) | |
5928 | { | |
5929 | tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs), | |
5930 | unshare_expr (val), | |
5931 | TREE_OPERAND (rhs, 1)); | |
5932 | if (tem) | |
5933 | rhs = tem; | |
5934 | } | |
5935 | } | |
5936 | return fold_const_aggregate_ref_1 (rhs, valueize); | |
5937 | } | |
5938 | else if (kind == tcc_declaration) | |
5939 | return get_symbol_constant_value (rhs); | |
5940 | return rhs; | |
5941 | } | |
5942 | ||
5943 | case GIMPLE_UNARY_RHS: | |
2ad7e37a | 5944 | return NULL_TREE; |
1d0b727d | 5945 | |
5946 | case GIMPLE_BINARY_RHS: | |
38758f38 | 5947 | /* Translate &x + CST into an invariant form suitable for |
5948 | further propagation. */ | |
5949 | if (subcode == POINTER_PLUS_EXPR) | |
5950 | { | |
38758f38 | 5951 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); |
5952 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
38758f38 | 5953 | if (TREE_CODE (op0) == ADDR_EXPR |
5954 | && TREE_CODE (op1) == INTEGER_CST) | |
5955 | { | |
5956 | tree off = fold_convert (ptr_type_node, op1); | |
5957 | return build_fold_addr_expr_loc | |
5958 | (loc, | |
5959 | fold_build2 (MEM_REF, | |
5960 | TREE_TYPE (TREE_TYPE (op0)), | |
5961 | unshare_expr (op0), off)); | |
5962 | } | |
5963 | } | |
373bcaaf | 5964 | /* Canonicalize bool != 0 and bool == 0 appearing after |
5965 | valueization. While gimple_simplify handles this | |
5966 | it can get confused by the ~X == 1 -> X == 0 transform | |
5967 | which we cant reduce to a SSA name or a constant | |
5968 | (and we have no way to tell gimple_simplify to not | |
5969 | consider those transforms in the first place). */ | |
5970 | else if (subcode == EQ_EXPR | |
5971 | || subcode == NE_EXPR) | |
5972 | { | |
5973 | tree lhs = gimple_assign_lhs (stmt); | |
5974 | tree op0 = gimple_assign_rhs1 (stmt); | |
5975 | if (useless_type_conversion_p (TREE_TYPE (lhs), | |
5976 | TREE_TYPE (op0))) | |
5977 | { | |
5978 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
5979 | op0 = (*valueize) (op0); | |
3ce024a1 | 5980 | if (TREE_CODE (op0) == INTEGER_CST) |
5981 | std::swap (op0, op1); | |
5982 | if (TREE_CODE (op1) == INTEGER_CST | |
5983 | && ((subcode == NE_EXPR && integer_zerop (op1)) | |
5984 | || (subcode == EQ_EXPR && integer_onep (op1)))) | |
5985 | return op0; | |
373bcaaf | 5986 | } |
5987 | } | |
38758f38 | 5988 | return NULL_TREE; |
1d0b727d | 5989 | |
5990 | case GIMPLE_TERNARY_RHS: | |
5991 | { | |
5992 | /* Handle ternary operators that can appear in GIMPLE form. */ | |
5993 | tree op0 = (*valueize) (gimple_assign_rhs1 (stmt)); | |
5994 | tree op1 = (*valueize) (gimple_assign_rhs2 (stmt)); | |
5995 | tree op2 = (*valueize) (gimple_assign_rhs3 (stmt)); | |
1d0b727d | 5996 | return fold_ternary_loc (loc, subcode, |
5997 | gimple_expr_type (stmt), op0, op1, op2); | |
5998 | } | |
5999 | ||
6000 | default: | |
6001 | gcc_unreachable (); | |
6002 | } | |
6003 | } | |
6004 | ||
6005 | case GIMPLE_CALL: | |
6006 | { | |
fb049fba | 6007 | tree fn; |
1a91d914 | 6008 | gcall *call_stmt = as_a <gcall *> (stmt); |
fb049fba | 6009 | |
6010 | if (gimple_call_internal_p (stmt)) | |
137559b2 | 6011 | { |
6012 | enum tree_code subcode = ERROR_MARK; | |
6013 | switch (gimple_call_internal_fn (stmt)) | |
6014 | { | |
6015 | case IFN_UBSAN_CHECK_ADD: | |
6016 | subcode = PLUS_EXPR; | |
6017 | break; | |
6018 | case IFN_UBSAN_CHECK_SUB: | |
6019 | subcode = MINUS_EXPR; | |
6020 | break; | |
6021 | case IFN_UBSAN_CHECK_MUL: | |
6022 | subcode = MULT_EXPR; | |
6023 | break; | |
db48d95b | 6024 | case IFN_BUILTIN_EXPECT: |
6025 | { | |
6026 | tree arg0 = gimple_call_arg (stmt, 0); | |
6027 | tree op0 = (*valueize) (arg0); | |
6028 | if (TREE_CODE (op0) == INTEGER_CST) | |
6029 | return op0; | |
6030 | return NULL_TREE; | |
6031 | } | |
137559b2 | 6032 | default: |
6033 | return NULL_TREE; | |
6034 | } | |
ce8e6661 | 6035 | tree arg0 = gimple_call_arg (stmt, 0); |
6036 | tree arg1 = gimple_call_arg (stmt, 1); | |
6037 | tree op0 = (*valueize) (arg0); | |
6038 | tree op1 = (*valueize) (arg1); | |
137559b2 | 6039 | |
6040 | if (TREE_CODE (op0) != INTEGER_CST | |
6041 | || TREE_CODE (op1) != INTEGER_CST) | |
ce8e6661 | 6042 | { |
6043 | switch (subcode) | |
6044 | { | |
6045 | case MULT_EXPR: | |
6046 | /* x * 0 = 0 * x = 0 without overflow. */ | |
6047 | if (integer_zerop (op0) || integer_zerop (op1)) | |
6048 | return build_zero_cst (TREE_TYPE (arg0)); | |
6049 | break; | |
6050 | case MINUS_EXPR: | |
6051 | /* y - y = 0 without overflow. */ | |
6052 | if (operand_equal_p (op0, op1, 0)) | |
6053 | return build_zero_cst (TREE_TYPE (arg0)); | |
6054 | break; | |
6055 | default: | |
6056 | break; | |
6057 | } | |
6058 | } | |
6059 | tree res | |
6060 | = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1); | |
137559b2 | 6061 | if (res |
6062 | && TREE_CODE (res) == INTEGER_CST | |
6063 | && !TREE_OVERFLOW (res)) | |
6064 | return res; | |
6065 | return NULL_TREE; | |
6066 | } | |
fb049fba | 6067 | |
6068 | fn = (*valueize) (gimple_call_fn (stmt)); | |
1d0b727d | 6069 | if (TREE_CODE (fn) == ADDR_EXPR |
6070 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
8ded4352 | 6071 | && DECL_BUILT_IN (TREE_OPERAND (fn, 0)) |
6072 | && gimple_builtin_call_types_compatible_p (stmt, | |
6073 | TREE_OPERAND (fn, 0))) | |
1d0b727d | 6074 | { |
6075 | tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt)); | |
9d884767 | 6076 | tree retval; |
1d0b727d | 6077 | unsigned i; |
6078 | for (i = 0; i < gimple_call_num_args (stmt); ++i) | |
6079 | args[i] = (*valueize) (gimple_call_arg (stmt, i)); | |
9d884767 | 6080 | retval = fold_builtin_call_array (loc, |
1a91d914 | 6081 | gimple_call_return_type (call_stmt), |
1d0b727d | 6082 | fn, gimple_call_num_args (stmt), args); |
1d0b727d | 6083 | if (retval) |
8ded4352 | 6084 | { |
6085 | /* fold_call_expr wraps the result inside a NOP_EXPR. */ | |
6086 | STRIP_NOPS (retval); | |
1a91d914 | 6087 | retval = fold_convert (gimple_call_return_type (call_stmt), |
6088 | retval); | |
8ded4352 | 6089 | } |
1d0b727d | 6090 | return retval; |
6091 | } | |
6092 | return NULL_TREE; | |
6093 | } | |
6094 | ||
6095 | default: | |
6096 | return NULL_TREE; | |
6097 | } | |
6098 | } | |
6099 | ||
6100 | /* Fold STMT to a constant using VALUEIZE to valueize SSA names. | |
6101 | Returns NULL_TREE if folding to a constant is not possible, otherwise | |
6102 | returns a constant according to is_gimple_min_invariant. */ | |
6103 | ||
6104 | tree | |
42acab1c | 6105 | gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree)) |
1d0b727d | 6106 | { |
6107 | tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize); | |
6108 | if (res && is_gimple_min_invariant (res)) | |
6109 | return res; | |
6110 | return NULL_TREE; | |
6111 | } | |
6112 | ||
6113 | ||
6114 | /* The following set of functions are supposed to fold references using | |
6115 | their constant initializers. */ | |
6116 | ||
1d0b727d | 6117 | /* See if we can find constructor defining value of BASE. |
6118 | When we know the consructor with constant offset (such as | |
6119 | base is array[40] and we do know constructor of array), then | |
6120 | BIT_OFFSET is adjusted accordingly. | |
6121 | ||
6122 | As a special case, return error_mark_node when constructor | |
6123 | is not explicitly available, but it is known to be zero | |
6124 | such as 'static const int a;'. */ | |
6125 | static tree | |
6126 | get_base_constructor (tree base, HOST_WIDE_INT *bit_offset, | |
6127 | tree (*valueize)(tree)) | |
6128 | { | |
6129 | HOST_WIDE_INT bit_offset2, size, max_size; | |
292237f3 | 6130 | bool reverse; |
6131 | ||
1d0b727d | 6132 | if (TREE_CODE (base) == MEM_REF) |
6133 | { | |
6134 | if (!integer_zerop (TREE_OPERAND (base, 1))) | |
6135 | { | |
e913b5cd | 6136 | if (!tree_fits_shwi_p (TREE_OPERAND (base, 1))) |
1d0b727d | 6137 | return NULL_TREE; |
e913b5cd | 6138 | *bit_offset += (mem_ref_offset (base).to_short_addr () |
1d0b727d | 6139 | * BITS_PER_UNIT); |
6140 | } | |
6141 | ||
6142 | if (valueize | |
6143 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) | |
6144 | base = valueize (TREE_OPERAND (base, 0)); | |
6145 | if (!base || TREE_CODE (base) != ADDR_EXPR) | |
6146 | return NULL_TREE; | |
6147 | base = TREE_OPERAND (base, 0); | |
6148 | } | |
2be13cd5 | 6149 | else if (valueize |
6150 | && TREE_CODE (base) == SSA_NAME) | |
6151 | base = valueize (base); | |
1d0b727d | 6152 | |
6153 | /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its | |
6154 | DECL_INITIAL. If BASE is a nested reference into another | |
6155 | ARRAY_REF or COMPONENT_REF, make a recursive call to resolve | |
6156 | the inner reference. */ | |
6157 | switch (TREE_CODE (base)) | |
6158 | { | |
6159 | case VAR_DECL: | |
1d0b727d | 6160 | case CONST_DECL: |
df8d3e89 | 6161 | { |
6162 | tree init = ctor_for_folding (base); | |
6163 | ||
a04e8d62 | 6164 | /* Our semantic is exact opposite of ctor_for_folding; |
df8d3e89 | 6165 | NULL means unknown, while error_mark_node is 0. */ |
6166 | if (init == error_mark_node) | |
6167 | return NULL_TREE; | |
6168 | if (!init) | |
6169 | return error_mark_node; | |
6170 | return init; | |
6171 | } | |
1d0b727d | 6172 | |
2be13cd5 | 6173 | case VIEW_CONVERT_EXPR: |
6174 | return get_base_constructor (TREE_OPERAND (base, 0), | |
6175 | bit_offset, valueize); | |
6176 | ||
1d0b727d | 6177 | case ARRAY_REF: |
6178 | case COMPONENT_REF: | |
292237f3 | 6179 | base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size, |
6180 | &reverse); | |
1d0b727d | 6181 | if (max_size == -1 || size != max_size) |
6182 | return NULL_TREE; | |
6183 | *bit_offset += bit_offset2; | |
6184 | return get_base_constructor (base, bit_offset, valueize); | |
6185 | ||
1d0b727d | 6186 | case CONSTRUCTOR: |
6187 | return base; | |
6188 | ||
6189 | default: | |
2be13cd5 | 6190 | if (CONSTANT_CLASS_P (base)) |
6191 | return base; | |
6192 | ||
1d0b727d | 6193 | return NULL_TREE; |
6194 | } | |
6195 | } | |
6196 | ||
1d0b727d | 6197 | /* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size |
6198 | SIZE to the memory at bit OFFSET. */ | |
6199 | ||
6200 | static tree | |
6201 | fold_array_ctor_reference (tree type, tree ctor, | |
6202 | unsigned HOST_WIDE_INT offset, | |
a65b88bf | 6203 | unsigned HOST_WIDE_INT size, |
6204 | tree from_decl) | |
1d0b727d | 6205 | { |
5de9d3ed | 6206 | offset_int low_bound; |
6207 | offset_int elt_size; | |
5de9d3ed | 6208 | offset_int access_index; |
ff943637 | 6209 | tree domain_type = NULL_TREE; |
1d0b727d | 6210 | HOST_WIDE_INT inner_offset; |
6211 | ||
6212 | /* Compute low bound and elt size. */ | |
415b0903 | 6213 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE) |
6214 | domain_type = TYPE_DOMAIN (TREE_TYPE (ctor)); | |
1d0b727d | 6215 | if (domain_type && TYPE_MIN_VALUE (domain_type)) |
6216 | { | |
6217 | /* Static constructors for variably sized objects makes no sense. */ | |
0a547de2 | 6218 | if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST) |
6219 | return NULL_TREE; | |
5de9d3ed | 6220 | low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type)); |
1d0b727d | 6221 | } |
6222 | else | |
e913b5cd | 6223 | low_bound = 0; |
1d0b727d | 6224 | /* Static constructors for variably sized objects makes no sense. */ |
0a547de2 | 6225 | if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST) |
6226 | return NULL_TREE; | |
5de9d3ed | 6227 | elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))); |
1d0b727d | 6228 | |
6229 | /* We can handle only constantly sized accesses that are known to not | |
6230 | be larger than size of array element. */ | |
6231 | if (!TYPE_SIZE_UNIT (type) | |
6232 | || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST | |
32115eac | 6233 | || elt_size < wi::to_offset (TYPE_SIZE_UNIT (type)) |
fe34354e | 6234 | || elt_size == 0) |
1d0b727d | 6235 | return NULL_TREE; |
6236 | ||
6237 | /* Compute the array index we look for. */ | |
5de9d3ed | 6238 | access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT), |
796b6678 | 6239 | elt_size); |
cf8f0e63 | 6240 | access_index += low_bound; |
1d0b727d | 6241 | |
6242 | /* And offset within the access. */ | |
cf8f0e63 | 6243 | inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT); |
1d0b727d | 6244 | |
6245 | /* See if the array field is large enough to span whole access. We do not | |
6246 | care to fold accesses spanning multiple array indexes. */ | |
cf8f0e63 | 6247 | if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT) |
1d0b727d | 6248 | return NULL_TREE; |
ff943637 | 6249 | if (tree val = get_array_ctor_element_at_index (ctor, access_index)) |
6250 | return fold_ctor_reference (type, val, inner_offset, size, from_decl); | |
1d0b727d | 6251 | |
1d0b727d | 6252 | /* When memory is not explicitely mentioned in constructor, |
6253 | it is 0 (or out of range). */ | |
6254 | return build_zero_cst (type); | |
6255 | } | |
6256 | ||
6257 | /* CTOR is CONSTRUCTOR of an aggregate or vector. | |
6258 | Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */ | |
6259 | ||
6260 | static tree | |
6261 | fold_nonarray_ctor_reference (tree type, tree ctor, | |
6262 | unsigned HOST_WIDE_INT offset, | |
a65b88bf | 6263 | unsigned HOST_WIDE_INT size, |
6264 | tree from_decl) | |
1d0b727d | 6265 | { |
6266 | unsigned HOST_WIDE_INT cnt; | |
6267 | tree cfield, cval; | |
6268 | ||
6269 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, | |
6270 | cval) | |
6271 | { | |
6272 | tree byte_offset = DECL_FIELD_OFFSET (cfield); | |
6273 | tree field_offset = DECL_FIELD_BIT_OFFSET (cfield); | |
6274 | tree field_size = DECL_SIZE (cfield); | |
5de9d3ed | 6275 | offset_int bitoffset; |
5de9d3ed | 6276 | offset_int bitoffset_end, access_end; |
1d0b727d | 6277 | |
6278 | /* Variable sized objects in static constructors makes no sense, | |
6279 | but field_size can be NULL for flexible array members. */ | |
6280 | gcc_assert (TREE_CODE (field_offset) == INTEGER_CST | |
6281 | && TREE_CODE (byte_offset) == INTEGER_CST | |
6282 | && (field_size != NULL_TREE | |
6283 | ? TREE_CODE (field_size) == INTEGER_CST | |
6284 | : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE)); | |
6285 | ||
6286 | /* Compute bit offset of the field. */ | |
5de9d3ed | 6287 | bitoffset = (wi::to_offset (field_offset) |
9fdc1ed4 | 6288 | + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT)); |
1d0b727d | 6289 | /* Compute bit offset where the field ends. */ |
6290 | if (field_size != NULL_TREE) | |
5de9d3ed | 6291 | bitoffset_end = bitoffset + wi::to_offset (field_size); |
1d0b727d | 6292 | else |
e913b5cd | 6293 | bitoffset_end = 0; |
1d0b727d | 6294 | |
5de9d3ed | 6295 | access_end = offset_int (offset) + size; |
40a19864 | 6296 | |
6297 | /* Is there any overlap between [OFFSET, OFFSET+SIZE) and | |
6298 | [BITOFFSET, BITOFFSET_END)? */ | |
796b6678 | 6299 | if (wi::cmps (access_end, bitoffset) > 0 |
1d0b727d | 6300 | && (field_size == NULL_TREE |
796b6678 | 6301 | || wi::lts_p (offset, bitoffset_end))) |
1d0b727d | 6302 | { |
5de9d3ed | 6303 | offset_int inner_offset = offset_int (offset) - bitoffset; |
1d0b727d | 6304 | /* We do have overlap. Now see if field is large enough to |
6305 | cover the access. Give up for accesses spanning multiple | |
6306 | fields. */ | |
796b6678 | 6307 | if (wi::cmps (access_end, bitoffset_end) > 0) |
1d0b727d | 6308 | return NULL_TREE; |
32115eac | 6309 | if (offset < bitoffset) |
40a19864 | 6310 | return NULL_TREE; |
1d0b727d | 6311 | return fold_ctor_reference (type, cval, |
cf8f0e63 | 6312 | inner_offset.to_uhwi (), size, |
a65b88bf | 6313 | from_decl); |
1d0b727d | 6314 | } |
6315 | } | |
6316 | /* When memory is not explicitely mentioned in constructor, it is 0. */ | |
6317 | return build_zero_cst (type); | |
6318 | } | |
6319 | ||
6320 | /* CTOR is value initializing memory, fold reference of type TYPE and size SIZE | |
6321 | to the memory at bit OFFSET. */ | |
6322 | ||
a5650c86 | 6323 | tree |
1d0b727d | 6324 | fold_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT offset, |
a65b88bf | 6325 | unsigned HOST_WIDE_INT size, tree from_decl) |
1d0b727d | 6326 | { |
6327 | tree ret; | |
6328 | ||
6329 | /* We found the field with exact match. */ | |
6330 | if (useless_type_conversion_p (type, TREE_TYPE (ctor)) | |
6331 | && !offset) | |
8f266cd9 | 6332 | return canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
1d0b727d | 6333 | |
6334 | /* We are at the end of walk, see if we can view convert the | |
6335 | result. */ | |
6336 | if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset | |
6337 | /* VIEW_CONVERT_EXPR is defined only for matching sizes. */ | |
2eac3ab5 | 6338 | && !compare_tree_int (TYPE_SIZE (type), size) |
6339 | && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size)) | |
1d0b727d | 6340 | { |
8f266cd9 | 6341 | ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl); |
1d0b727d | 6342 | if (ret) |
c71d23da | 6343 | { |
6344 | ret = fold_unary (VIEW_CONVERT_EXPR, type, ret); | |
6345 | if (ret) | |
6346 | STRIP_USELESS_TYPE_CONVERSION (ret); | |
6347 | } | |
1d0b727d | 6348 | return ret; |
6349 | } | |
4237b215 | 6350 | /* For constants and byte-aligned/sized reads try to go through |
6351 | native_encode/interpret. */ | |
6352 | if (CONSTANT_CLASS_P (ctor) | |
6353 | && BITS_PER_UNIT == 8 | |
6354 | && offset % BITS_PER_UNIT == 0 | |
6355 | && size % BITS_PER_UNIT == 0 | |
6356 | && size <= MAX_BITSIZE_MODE_ANY_MODE) | |
6357 | { | |
6358 | unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; | |
522ea93c | 6359 | int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT, |
6360 | offset / BITS_PER_UNIT); | |
6361 | if (len > 0) | |
6362 | return native_interpret_expr (type, buf, len); | |
4237b215 | 6363 | } |
1d0b727d | 6364 | if (TREE_CODE (ctor) == CONSTRUCTOR) |
6365 | { | |
6366 | ||
415b0903 | 6367 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE |
6368 | || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE) | |
a65b88bf | 6369 | return fold_array_ctor_reference (type, ctor, offset, size, |
6370 | from_decl); | |
1d0b727d | 6371 | else |
a65b88bf | 6372 | return fold_nonarray_ctor_reference (type, ctor, offset, size, |
6373 | from_decl); | |
1d0b727d | 6374 | } |
6375 | ||
6376 | return NULL_TREE; | |
6377 | } | |
6378 | ||
6379 | /* Return the tree representing the element referenced by T if T is an | |
6380 | ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA | |
6381 | names using VALUEIZE. Return NULL_TREE otherwise. */ | |
6382 | ||
6383 | tree | |
6384 | fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree)) | |
6385 | { | |
6386 | tree ctor, idx, base; | |
6387 | HOST_WIDE_INT offset, size, max_size; | |
6388 | tree tem; | |
292237f3 | 6389 | bool reverse; |
1d0b727d | 6390 | |
62df0ff9 | 6391 | if (TREE_THIS_VOLATILE (t)) |
6392 | return NULL_TREE; | |
6393 | ||
74efe522 | 6394 | if (DECL_P (t)) |
1d0b727d | 6395 | return get_symbol_constant_value (t); |
6396 | ||
6397 | tem = fold_read_from_constant_string (t); | |
6398 | if (tem) | |
6399 | return tem; | |
6400 | ||
6401 | switch (TREE_CODE (t)) | |
6402 | { | |
6403 | case ARRAY_REF: | |
6404 | case ARRAY_RANGE_REF: | |
6405 | /* Constant indexes are handled well by get_base_constructor. | |
6406 | Only special case variable offsets. | |
6407 | FIXME: This code can't handle nested references with variable indexes | |
6408 | (they will be handled only by iteration of ccp). Perhaps we can bring | |
6409 | get_ref_base_and_extent here and make it use a valueize callback. */ | |
6410 | if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME | |
6411 | && valueize | |
6412 | && (idx = (*valueize) (TREE_OPERAND (t, 1))) | |
96c3acd0 | 6413 | && TREE_CODE (idx) == INTEGER_CST) |
1d0b727d | 6414 | { |
936a705d | 6415 | tree low_bound, unit_size; |
1d0b727d | 6416 | |
6417 | /* If the resulting bit-offset is constant, track it. */ | |
936a705d | 6418 | if ((low_bound = array_ref_low_bound (t), |
6419 | TREE_CODE (low_bound) == INTEGER_CST) | |
6420 | && (unit_size = array_ref_element_size (t), | |
6421 | tree_fits_uhwi_p (unit_size))) | |
1d0b727d | 6422 | { |
5de9d3ed | 6423 | offset_int woffset |
6424 | = wi::sext (wi::to_offset (idx) - wi::to_offset (low_bound), | |
796b6678 | 6425 | TYPE_PRECISION (TREE_TYPE (idx))); |
ddb1be65 | 6426 | |
796b6678 | 6427 | if (wi::fits_shwi_p (woffset)) |
e913b5cd | 6428 | { |
6429 | offset = woffset.to_shwi (); | |
6430 | /* TODO: This code seems wrong, multiply then check | |
6431 | to see if it fits. */ | |
fe5ad926 | 6432 | offset *= tree_to_uhwi (unit_size); |
e913b5cd | 6433 | offset *= BITS_PER_UNIT; |
ddb1be65 | 6434 | |
e913b5cd | 6435 | base = TREE_OPERAND (t, 0); |
6436 | ctor = get_base_constructor (base, &offset, valueize); | |
6437 | /* Empty constructor. Always fold to 0. */ | |
6438 | if (ctor == error_mark_node) | |
6439 | return build_zero_cst (TREE_TYPE (t)); | |
6440 | /* Out of bound array access. Value is undefined, | |
6441 | but don't fold. */ | |
6442 | if (offset < 0) | |
6443 | return NULL_TREE; | |
6444 | /* We can not determine ctor. */ | |
6445 | if (!ctor) | |
6446 | return NULL_TREE; | |
6447 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, | |
6448 | tree_to_uhwi (unit_size) | |
6449 | * BITS_PER_UNIT, | |
6450 | base); | |
6451 | } | |
1d0b727d | 6452 | } |
6453 | } | |
6454 | /* Fallthru. */ | |
6455 | ||
6456 | case COMPONENT_REF: | |
6457 | case BIT_FIELD_REF: | |
6458 | case TARGET_MEM_REF: | |
6459 | case MEM_REF: | |
292237f3 | 6460 | base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse); |
1d0b727d | 6461 | ctor = get_base_constructor (base, &offset, valueize); |
6462 | ||
6463 | /* Empty constructor. Always fold to 0. */ | |
6464 | if (ctor == error_mark_node) | |
6465 | return build_zero_cst (TREE_TYPE (t)); | |
6466 | /* We do not know precise address. */ | |
6467 | if (max_size == -1 || max_size != size) | |
6468 | return NULL_TREE; | |
6469 | /* We can not determine ctor. */ | |
6470 | if (!ctor) | |
6471 | return NULL_TREE; | |
6472 | ||
6473 | /* Out of bound array access. Value is undefined, but don't fold. */ | |
6474 | if (offset < 0) | |
6475 | return NULL_TREE; | |
6476 | ||
a65b88bf | 6477 | return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, |
6478 | base); | |
1d0b727d | 6479 | |
6480 | case REALPART_EXPR: | |
6481 | case IMAGPART_EXPR: | |
6482 | { | |
6483 | tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize); | |
6484 | if (c && TREE_CODE (c) == COMPLEX_CST) | |
6485 | return fold_build1_loc (EXPR_LOCATION (t), | |
6486 | TREE_CODE (t), TREE_TYPE (t), c); | |
6487 | break; | |
6488 | } | |
6489 | ||
6490 | default: | |
6491 | break; | |
6492 | } | |
6493 | ||
6494 | return NULL_TREE; | |
6495 | } | |
6496 | ||
6497 | tree | |
6498 | fold_const_aggregate_ref (tree t) | |
6499 | { | |
6500 | return fold_const_aggregate_ref_1 (t, NULL); | |
6501 | } | |
0b7ad900 | 6502 | |
02636da3 | 6503 | /* Lookup virtual method with index TOKEN in a virtual table V |
857c5a0b | 6504 | at OFFSET. |
6505 | Set CAN_REFER if non-NULL to false if method | |
6506 | is not referable or if the virtual table is ill-formed (such as rewriten | |
6507 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
d4e80e2b | 6508 | |
6509 | tree | |
02636da3 | 6510 | gimple_get_virt_method_for_vtable (HOST_WIDE_INT token, |
6511 | tree v, | |
857c5a0b | 6512 | unsigned HOST_WIDE_INT offset, |
6513 | bool *can_refer) | |
d4e80e2b | 6514 | { |
02636da3 | 6515 | tree vtable = v, init, fn; |
6516 | unsigned HOST_WIDE_INT size; | |
57b16855 | 6517 | unsigned HOST_WIDE_INT elt_size, access_index; |
6518 | tree domain_type; | |
d4e80e2b | 6519 | |
857c5a0b | 6520 | if (can_refer) |
6521 | *can_refer = true; | |
6522 | ||
6750de5f | 6523 | /* First of all double check we have virtual table. */ |
53e9c5c4 | 6524 | if (!VAR_P (v) || !DECL_VIRTUAL_P (v)) |
857c5a0b | 6525 | { |
857c5a0b | 6526 | /* Pass down that we lost track of the target. */ |
6527 | if (can_refer) | |
6528 | *can_refer = false; | |
6529 | return NULL_TREE; | |
6530 | } | |
6750de5f | 6531 | |
9b3e908f | 6532 | init = ctor_for_folding (v); |
6533 | ||
6750de5f | 6534 | /* The virtual tables should always be born with constructors |
9b3e908f | 6535 | and we always should assume that they are avaialble for |
6536 | folding. At the moment we do not stream them in all cases, | |
6537 | but it should never happen that ctor seem unreachable. */ | |
6538 | gcc_assert (init); | |
6539 | if (init == error_mark_node) | |
6540 | { | |
6541 | gcc_assert (in_lto_p); | |
857c5a0b | 6542 | /* Pass down that we lost track of the target. */ |
6543 | if (can_refer) | |
6544 | *can_refer = false; | |
9b3e908f | 6545 | return NULL_TREE; |
6546 | } | |
d4e80e2b | 6547 | gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE); |
e913b5cd | 6548 | size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v)))); |
02636da3 | 6549 | offset *= BITS_PER_UNIT; |
d4e80e2b | 6550 | offset += token * size; |
6750de5f | 6551 | |
57b16855 | 6552 | /* Lookup the value in the constructor that is assumed to be array. |
6553 | This is equivalent to | |
6554 | fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init, | |
6555 | offset, size, NULL); | |
6556 | but in a constant time. We expect that frontend produced a simple | |
6557 | array without indexed initializers. */ | |
6558 | ||
6559 | gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE); | |
6560 | domain_type = TYPE_DOMAIN (TREE_TYPE (init)); | |
6561 | gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type))); | |
6562 | elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init)))); | |
6563 | ||
6564 | access_index = offset / BITS_PER_UNIT / elt_size; | |
6565 | gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0); | |
6566 | ||
6567 | /* This code makes an assumption that there are no | |
6568 | indexed fileds produced by C++ FE, so we can directly index the array. */ | |
6569 | if (access_index < CONSTRUCTOR_NELTS (init)) | |
6570 | { | |
6571 | fn = CONSTRUCTOR_ELT (init, access_index)->value; | |
6572 | gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index); | |
6573 | STRIP_NOPS (fn); | |
6574 | } | |
6575 | else | |
6576 | fn = NULL; | |
6750de5f | 6577 | |
6578 | /* For type inconsistent program we may end up looking up virtual method | |
6579 | in virtual table that does not contain TOKEN entries. We may overrun | |
6580 | the virtual table and pick up a constant or RTTI info pointer. | |
6581 | In any case the call is undefined. */ | |
6582 | if (!fn | |
6583 | || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR) | |
6584 | || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL) | |
6585 | fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
6586 | else | |
6587 | { | |
6588 | fn = TREE_OPERAND (fn, 0); | |
6589 | ||
6590 | /* When cgraph node is missing and function is not public, we cannot | |
6591 | devirtualize. This can happen in WHOPR when the actual method | |
6592 | ends up in other partition, because we found devirtualization | |
6593 | possibility too late. */ | |
6594 | if (!can_refer_decl_in_current_unit_p (fn, vtable)) | |
857c5a0b | 6595 | { |
6596 | if (can_refer) | |
6597 | { | |
6598 | *can_refer = false; | |
6599 | return fn; | |
6600 | } | |
6601 | return NULL_TREE; | |
6602 | } | |
6750de5f | 6603 | } |
d4e80e2b | 6604 | |
e25d4891 | 6605 | /* Make sure we create a cgraph node for functions we'll reference. |
6606 | They can be non-existent if the reference comes from an entry | |
6607 | of an external vtable for example. */ | |
415d1b9a | 6608 | cgraph_node::get_create (fn); |
e25d4891 | 6609 | |
d4e80e2b | 6610 | return fn; |
6611 | } | |
6612 | ||
02636da3 | 6613 | /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN |
6614 | is integer form of OBJ_TYPE_REF_TOKEN of the reference expression. | |
6615 | KNOWN_BINFO carries the binfo describing the true type of | |
857c5a0b | 6616 | OBJ_TYPE_REF_OBJECT(REF). |
6617 | Set CAN_REFER if non-NULL to false if method | |
6618 | is not referable or if the virtual table is ill-formed (such as rewriten | |
6619 | by non-C++ produced symbol). Otherwise just return NULL in that calse. */ | |
02636da3 | 6620 | |
6621 | tree | |
857c5a0b | 6622 | gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo, |
6623 | bool *can_refer) | |
02636da3 | 6624 | { |
6625 | unsigned HOST_WIDE_INT offset; | |
6626 | tree v; | |
6627 | ||
6628 | v = BINFO_VTABLE (known_binfo); | |
6629 | /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */ | |
6630 | if (!v) | |
6631 | return NULL_TREE; | |
6632 | ||
6633 | if (!vtable_pointer_value_to_vtable (v, &v, &offset)) | |
857c5a0b | 6634 | { |
6635 | if (can_refer) | |
6636 | *can_refer = false; | |
6637 | return NULL_TREE; | |
6638 | } | |
6639 | return gimple_get_virt_method_for_vtable (token, v, offset, can_refer); | |
02636da3 | 6640 | } |
6641 | ||
ba3fa3cb | 6642 | /* Given a pointer value T, return a simplified version of an |
6643 | indirection through T, or NULL_TREE if no simplification is | |
09c41ee4 | 6644 | possible. Note that the resulting type may be different from |
6645 | the type pointed to in the sense that it is still compatible | |
6646 | from the langhooks point of view. */ | |
6647 | ||
6648 | tree | |
6649 | gimple_fold_indirect_ref (tree t) | |
6650 | { | |
6651 | tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype); | |
6652 | tree sub = t; | |
6653 | tree subtype; | |
6654 | ||
6655 | STRIP_NOPS (sub); | |
6656 | subtype = TREE_TYPE (sub); | |
ba3fa3cb | 6657 | if (!POINTER_TYPE_P (subtype) |
6658 | || TYPE_REF_CAN_ALIAS_ALL (ptype)) | |
09c41ee4 | 6659 | return NULL_TREE; |
6660 | ||
6661 | if (TREE_CODE (sub) == ADDR_EXPR) | |
6662 | { | |
6663 | tree op = TREE_OPERAND (sub, 0); | |
6664 | tree optype = TREE_TYPE (op); | |
6665 | /* *&p => p */ | |
6666 | if (useless_type_conversion_p (type, optype)) | |
6667 | return op; | |
6668 | ||
6669 | /* *(foo *)&fooarray => fooarray[0] */ | |
6670 | if (TREE_CODE (optype) == ARRAY_TYPE | |
6671 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST | |
6672 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
6673 | { | |
6674 | tree type_domain = TYPE_DOMAIN (optype); | |
6675 | tree min_val = size_zero_node; | |
6676 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
6677 | min_val = TYPE_MIN_VALUE (type_domain); | |
6678 | if (TREE_CODE (min_val) == INTEGER_CST) | |
6679 | return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE); | |
6680 | } | |
6681 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
6682 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
6683 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
6684 | return fold_build1 (REALPART_EXPR, type, op); | |
6685 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
6686 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
6687 | && useless_type_conversion_p (type, TREE_TYPE (optype))) | |
6688 | { | |
6689 | tree part_width = TYPE_SIZE (type); | |
6690 | tree index = bitsize_int (0); | |
6691 | return fold_build3 (BIT_FIELD_REF, type, op, part_width, index); | |
6692 | } | |
6693 | } | |
6694 | ||
6695 | /* *(p + CST) -> ... */ | |
6696 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
6697 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
6698 | { | |
6699 | tree addr = TREE_OPERAND (sub, 0); | |
6700 | tree off = TREE_OPERAND (sub, 1); | |
6701 | tree addrtype; | |
6702 | ||
6703 | STRIP_NOPS (addr); | |
6704 | addrtype = TREE_TYPE (addr); | |
6705 | ||
6706 | /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */ | |
6707 | if (TREE_CODE (addr) == ADDR_EXPR | |
6708 | && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE | |
6709 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))) | |
6b409616 | 6710 | && tree_fits_uhwi_p (off)) |
09c41ee4 | 6711 | { |
6b409616 | 6712 | unsigned HOST_WIDE_INT offset = tree_to_uhwi (off); |
09c41ee4 | 6713 | tree part_width = TYPE_SIZE (type); |
6714 | unsigned HOST_WIDE_INT part_widthi | |
6b409616 | 6715 | = tree_to_shwi (part_width) / BITS_PER_UNIT; |
09c41ee4 | 6716 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; |
6717 | tree index = bitsize_int (indexi); | |
6718 | if (offset / part_widthi | |
2dda221d | 6719 | < TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))) |
09c41ee4 | 6720 | return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0), |
6721 | part_width, index); | |
6722 | } | |
6723 | ||
6724 | /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */ | |
6725 | if (TREE_CODE (addr) == ADDR_EXPR | |
6726 | && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE | |
6727 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))) | |
6728 | { | |
6729 | tree size = TYPE_SIZE_UNIT (type); | |
6730 | if (tree_int_cst_equal (size, off)) | |
6731 | return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0)); | |
6732 | } | |
6733 | ||
6734 | /* *(p + CST) -> MEM_REF <p, CST>. */ | |
6735 | if (TREE_CODE (addr) != ADDR_EXPR | |
6736 | || DECL_P (TREE_OPERAND (addr, 0))) | |
6737 | return fold_build2 (MEM_REF, type, | |
6738 | addr, | |
6b409616 | 6739 | wide_int_to_tree (ptype, off)); |
09c41ee4 | 6740 | } |
6741 | ||
6742 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
6743 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
6744 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST | |
6745 | && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype)))) | |
6746 | { | |
6747 | tree type_domain; | |
6748 | tree min_val = size_zero_node; | |
6749 | tree osub = sub; | |
6750 | sub = gimple_fold_indirect_ref (sub); | |
6751 | if (! sub) | |
6752 | sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub); | |
6753 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
6754 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
6755 | min_val = TYPE_MIN_VALUE (type_domain); | |
6756 | if (TREE_CODE (min_val) == INTEGER_CST) | |
6757 | return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE); | |
6758 | } | |
6759 | ||
6760 | return NULL_TREE; | |
6761 | } | |
f6a34e3f | 6762 | |
6763 | /* Return true if CODE is an operation that when operating on signed | |
6764 | integer types involves undefined behavior on overflow and the | |
6765 | operation can be expressed with unsigned arithmetic. */ | |
6766 | ||
6767 | bool | |
6768 | arith_code_with_undefined_signed_overflow (tree_code code) | |
6769 | { | |
6770 | switch (code) | |
6771 | { | |
6772 | case PLUS_EXPR: | |
6773 | case MINUS_EXPR: | |
6774 | case MULT_EXPR: | |
6775 | case NEGATE_EXPR: | |
6776 | case POINTER_PLUS_EXPR: | |
6777 | return true; | |
6778 | default: | |
6779 | return false; | |
6780 | } | |
6781 | } | |
6782 | ||
6783 | /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic | |
6784 | operation that can be transformed to unsigned arithmetic by converting | |
6785 | its operand, carrying out the operation in the corresponding unsigned | |
6786 | type and converting the result back to the original type. | |
6787 | ||
6788 | Returns a sequence of statements that replace STMT and also contain | |
6789 | a modified form of STMT itself. */ | |
6790 | ||
6791 | gimple_seq | |
42acab1c | 6792 | rewrite_to_defined_overflow (gimple *stmt) |
f6a34e3f | 6793 | { |
6794 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
6795 | { | |
6796 | fprintf (dump_file, "rewriting stmt with undefined signed " | |
6797 | "overflow "); | |
6798 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); | |
6799 | } | |
6800 | ||
6801 | tree lhs = gimple_assign_lhs (stmt); | |
6802 | tree type = unsigned_type_for (TREE_TYPE (lhs)); | |
6803 | gimple_seq stmts = NULL; | |
6804 | for (unsigned i = 1; i < gimple_num_ops (stmt); ++i) | |
6805 | { | |
cba8396b | 6806 | tree op = gimple_op (stmt, i); |
6807 | op = gimple_convert (&stmts, type, op); | |
6808 | gimple_set_op (stmt, i, op); | |
f6a34e3f | 6809 | } |
6810 | gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt)); | |
6811 | if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR) | |
6812 | gimple_assign_set_rhs_code (stmt, PLUS_EXPR); | |
6813 | gimple_seq_add_stmt (&stmts, stmt); | |
42acab1c | 6814 | gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt)); |
f6a34e3f | 6815 | gimple_seq_add_stmt (&stmts, cvt); |
6816 | ||
6817 | return stmts; | |
6818 | } | |
65f73697 | 6819 | |
2165588a | 6820 | |
85627475 | 6821 | /* The valueization hook we use for the gimple_build API simplification. |
6822 | This makes us match fold_buildN behavior by only combining with | |
6823 | statements in the sequence(s) we are currently building. */ | |
6824 | ||
6825 | static tree | |
6826 | gimple_build_valueize (tree op) | |
6827 | { | |
6828 | if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL) | |
6829 | return op; | |
6830 | return NULL_TREE; | |
6831 | } | |
6832 | ||
2165588a | 6833 | /* Build the expression CODE OP0 of type TYPE with location LOC, |
85627475 | 6834 | simplifying it first if possible. Returns the built |
2165588a | 6835 | expression value and appends statements possibly defining it |
6836 | to SEQ. */ | |
6837 | ||
6838 | tree | |
6839 | gimple_build (gimple_seq *seq, location_t loc, | |
85627475 | 6840 | enum tree_code code, tree type, tree op0) |
2165588a | 6841 | { |
85627475 | 6842 | tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize); |
2165588a | 6843 | if (!res) |
6844 | { | |
c985cfb4 | 6845 | res = create_tmp_reg_or_ssa_name (type); |
42acab1c | 6846 | gimple *stmt; |
2165588a | 6847 | if (code == REALPART_EXPR |
6848 | || code == IMAGPART_EXPR | |
6849 | || code == VIEW_CONVERT_EXPR) | |
e9cf809e | 6850 | stmt = gimple_build_assign (res, code, build1 (code, type, op0)); |
2165588a | 6851 | else |
e9cf809e | 6852 | stmt = gimple_build_assign (res, code, op0); |
2165588a | 6853 | gimple_set_location (stmt, loc); |
6854 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6855 | } | |
6856 | return res; | |
6857 | } | |
6858 | ||
6859 | /* Build the expression OP0 CODE OP1 of type TYPE with location LOC, | |
85627475 | 6860 | simplifying it first if possible. Returns the built |
2165588a | 6861 | expression value and appends statements possibly defining it |
6862 | to SEQ. */ | |
6863 | ||
6864 | tree | |
6865 | gimple_build (gimple_seq *seq, location_t loc, | |
85627475 | 6866 | enum tree_code code, tree type, tree op0, tree op1) |
2165588a | 6867 | { |
85627475 | 6868 | tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize); |
2165588a | 6869 | if (!res) |
6870 | { | |
c985cfb4 | 6871 | res = create_tmp_reg_or_ssa_name (type); |
42acab1c | 6872 | gimple *stmt = gimple_build_assign (res, code, op0, op1); |
2165588a | 6873 | gimple_set_location (stmt, loc); |
6874 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6875 | } | |
6876 | return res; | |
6877 | } | |
6878 | ||
6879 | /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC, | |
85627475 | 6880 | simplifying it first if possible. Returns the built |
2165588a | 6881 | expression value and appends statements possibly defining it |
6882 | to SEQ. */ | |
6883 | ||
6884 | tree | |
6885 | gimple_build (gimple_seq *seq, location_t loc, | |
85627475 | 6886 | enum tree_code code, tree type, tree op0, tree op1, tree op2) |
2165588a | 6887 | { |
6888 | tree res = gimple_simplify (code, type, op0, op1, op2, | |
85627475 | 6889 | seq, gimple_build_valueize); |
2165588a | 6890 | if (!res) |
6891 | { | |
c985cfb4 | 6892 | res = create_tmp_reg_or_ssa_name (type); |
42acab1c | 6893 | gimple *stmt; |
2165588a | 6894 | if (code == BIT_FIELD_REF) |
e9cf809e | 6895 | stmt = gimple_build_assign (res, code, |
6896 | build3 (code, type, op0, op1, op2)); | |
2165588a | 6897 | else |
e9cf809e | 6898 | stmt = gimple_build_assign (res, code, op0, op1, op2); |
2165588a | 6899 | gimple_set_location (stmt, loc); |
6900 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6901 | } | |
6902 | return res; | |
6903 | } | |
6904 | ||
6905 | /* Build the call FN (ARG0) with a result of type TYPE | |
6906 | (or no result if TYPE is void) with location LOC, | |
85627475 | 6907 | simplifying it first if possible. Returns the built |
2165588a | 6908 | expression value (or NULL_TREE if TYPE is void) and appends |
6909 | statements possibly defining it to SEQ. */ | |
6910 | ||
6911 | tree | |
6912 | gimple_build (gimple_seq *seq, location_t loc, | |
85627475 | 6913 | enum built_in_function fn, tree type, tree arg0) |
2165588a | 6914 | { |
85627475 | 6915 | tree res = gimple_simplify (fn, type, arg0, seq, gimple_build_valueize); |
2165588a | 6916 | if (!res) |
6917 | { | |
6918 | tree decl = builtin_decl_implicit (fn); | |
42acab1c | 6919 | gimple *stmt = gimple_build_call (decl, 1, arg0); |
2165588a | 6920 | if (!VOID_TYPE_P (type)) |
6921 | { | |
c985cfb4 | 6922 | res = create_tmp_reg_or_ssa_name (type); |
2165588a | 6923 | gimple_call_set_lhs (stmt, res); |
6924 | } | |
6925 | gimple_set_location (stmt, loc); | |
6926 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6927 | } | |
6928 | return res; | |
6929 | } | |
6930 | ||
6931 | /* Build the call FN (ARG0, ARG1) with a result of type TYPE | |
6932 | (or no result if TYPE is void) with location LOC, | |
85627475 | 6933 | simplifying it first if possible. Returns the built |
2165588a | 6934 | expression value (or NULL_TREE if TYPE is void) and appends |
6935 | statements possibly defining it to SEQ. */ | |
6936 | ||
6937 | tree | |
6938 | gimple_build (gimple_seq *seq, location_t loc, | |
85627475 | 6939 | enum built_in_function fn, tree type, tree arg0, tree arg1) |
2165588a | 6940 | { |
85627475 | 6941 | tree res = gimple_simplify (fn, type, arg0, arg1, seq, gimple_build_valueize); |
2165588a | 6942 | if (!res) |
6943 | { | |
6944 | tree decl = builtin_decl_implicit (fn); | |
42acab1c | 6945 | gimple *stmt = gimple_build_call (decl, 2, arg0, arg1); |
2165588a | 6946 | if (!VOID_TYPE_P (type)) |
6947 | { | |
c985cfb4 | 6948 | res = create_tmp_reg_or_ssa_name (type); |
2165588a | 6949 | gimple_call_set_lhs (stmt, res); |
6950 | } | |
6951 | gimple_set_location (stmt, loc); | |
6952 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6953 | } | |
6954 | return res; | |
6955 | } | |
6956 | ||
6957 | /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE | |
6958 | (or no result if TYPE is void) with location LOC, | |
85627475 | 6959 | simplifying it first if possible. Returns the built |
2165588a | 6960 | expression value (or NULL_TREE if TYPE is void) and appends |
6961 | statements possibly defining it to SEQ. */ | |
6962 | ||
6963 | tree | |
6964 | gimple_build (gimple_seq *seq, location_t loc, | |
6965 | enum built_in_function fn, tree type, | |
85627475 | 6966 | tree arg0, tree arg1, tree arg2) |
2165588a | 6967 | { |
85627475 | 6968 | tree res = gimple_simplify (fn, type, arg0, arg1, arg2, |
6969 | seq, gimple_build_valueize); | |
2165588a | 6970 | if (!res) |
6971 | { | |
6972 | tree decl = builtin_decl_implicit (fn); | |
42acab1c | 6973 | gimple *stmt = gimple_build_call (decl, 3, arg0, arg1, arg2); |
2165588a | 6974 | if (!VOID_TYPE_P (type)) |
6975 | { | |
c985cfb4 | 6976 | res = create_tmp_reg_or_ssa_name (type); |
2165588a | 6977 | gimple_call_set_lhs (stmt, res); |
6978 | } | |
6979 | gimple_set_location (stmt, loc); | |
6980 | gimple_seq_add_stmt_without_update (seq, stmt); | |
6981 | } | |
6982 | return res; | |
6983 | } | |
6984 | ||
6985 | /* Build the conversion (TYPE) OP with a result of type TYPE | |
6986 | with location LOC if such conversion is neccesary in GIMPLE, | |
6987 | simplifying it first. | |
6988 | Returns the built expression value and appends | |
6989 | statements possibly defining it to SEQ. */ | |
65f73697 | 6990 | |
6991 | tree | |
6992 | gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op) | |
6993 | { | |
6994 | if (useless_type_conversion_p (type, TREE_TYPE (op))) | |
6995 | return op; | |
2165588a | 6996 | return gimple_build (seq, loc, NOP_EXPR, type, op); |
65f73697 | 6997 | } |
ee230333 | 6998 | |
cba8396b | 6999 | /* Build the conversion (ptrofftype) OP with a result of a type |
7000 | compatible with ptrofftype with location LOC if such conversion | |
7001 | is neccesary in GIMPLE, simplifying it first. | |
7002 | Returns the built expression value and appends | |
7003 | statements possibly defining it to SEQ. */ | |
7004 | ||
7005 | tree | |
7006 | gimple_convert_to_ptrofftype (gimple_seq *seq, location_t loc, tree op) | |
7007 | { | |
7008 | if (ptrofftype_p (TREE_TYPE (op))) | |
7009 | return op; | |
7010 | return gimple_convert (seq, loc, sizetype, op); | |
7011 | } | |
7012 | ||
ee230333 | 7013 | /* Return true if the result of assignment STMT is known to be non-negative. |
7014 | If the return value is based on the assumption that signed overflow is | |
7015 | undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
7016 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ | |
7017 | ||
7018 | static bool | |
7019 | gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p, | |
7020 | int depth) | |
7021 | { | |
7022 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
7023 | switch (get_gimple_rhs_class (code)) | |
7024 | { | |
7025 | case GIMPLE_UNARY_RHS: | |
7026 | return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt), | |
7027 | gimple_expr_type (stmt), | |
7028 | gimple_assign_rhs1 (stmt), | |
7029 | strict_overflow_p, depth); | |
7030 | case GIMPLE_BINARY_RHS: | |
7031 | return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt), | |
7032 | gimple_expr_type (stmt), | |
7033 | gimple_assign_rhs1 (stmt), | |
7034 | gimple_assign_rhs2 (stmt), | |
7035 | strict_overflow_p, depth); | |
7036 | case GIMPLE_TERNARY_RHS: | |
7037 | return false; | |
7038 | case GIMPLE_SINGLE_RHS: | |
7039 | return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt), | |
7040 | strict_overflow_p, depth); | |
7041 | case GIMPLE_INVALID_RHS: | |
7042 | break; | |
7043 | } | |
7044 | gcc_unreachable (); | |
7045 | } | |
7046 | ||
7047 | /* Return true if return value of call STMT is known to be non-negative. | |
7048 | If the return value is based on the assumption that signed overflow is | |
7049 | undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
7050 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ | |
7051 | ||
7052 | static bool | |
7053 | gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p, | |
7054 | int depth) | |
7055 | { | |
7056 | tree arg0 = gimple_call_num_args (stmt) > 0 ? | |
7057 | gimple_call_arg (stmt, 0) : NULL_TREE; | |
7058 | tree arg1 = gimple_call_num_args (stmt) > 1 ? | |
7059 | gimple_call_arg (stmt, 1) : NULL_TREE; | |
7060 | ||
7061 | return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt), | |
7a366e88 | 7062 | gimple_call_combined_fn (stmt), |
ee230333 | 7063 | arg0, |
7064 | arg1, | |
7065 | strict_overflow_p, depth); | |
7066 | } | |
7067 | ||
ec11da34 | 7068 | /* Return true if return value of call STMT is known to be non-negative. |
7069 | If the return value is based on the assumption that signed overflow is | |
7070 | undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
7071 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ | |
7072 | ||
7073 | static bool | |
7074 | gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p, | |
7075 | int depth) | |
7076 | { | |
7077 | for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i) | |
7078 | { | |
7079 | tree arg = gimple_phi_arg_def (stmt, i); | |
7080 | if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1)) | |
7081 | return false; | |
7082 | } | |
7083 | return true; | |
7084 | } | |
7085 | ||
ee230333 | 7086 | /* Return true if STMT is known to compute a non-negative value. |
7087 | If the return value is based on the assumption that signed overflow is | |
7088 | undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
7089 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ | |
7090 | ||
7091 | bool | |
7092 | gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p, | |
7093 | int depth) | |
7094 | { | |
7095 | switch (gimple_code (stmt)) | |
7096 | { | |
7097 | case GIMPLE_ASSIGN: | |
7098 | return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p, | |
7099 | depth); | |
7100 | case GIMPLE_CALL: | |
7101 | return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p, | |
7102 | depth); | |
ec11da34 | 7103 | case GIMPLE_PHI: |
7104 | return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p, | |
7105 | depth); | |
ee230333 | 7106 | default: |
7107 | return false; | |
7108 | } | |
7109 | } | |
6f5f406a | 7110 | |
7111 | /* Return true if the floating-point value computed by assignment STMT | |
7112 | is known to have an integer value. We also allow +Inf, -Inf and NaN | |
9f27d92a | 7113 | to be considered integer values. Return false for signaling NaN. |
6f5f406a | 7114 | |
7115 | DEPTH is the current nesting depth of the query. */ | |
7116 | ||
7117 | static bool | |
7118 | gimple_assign_integer_valued_real_p (gimple *stmt, int depth) | |
7119 | { | |
7120 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
7121 | switch (get_gimple_rhs_class (code)) | |
7122 | { | |
7123 | case GIMPLE_UNARY_RHS: | |
7124 | return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt), | |
7125 | gimple_assign_rhs1 (stmt), depth); | |
7126 | case GIMPLE_BINARY_RHS: | |
7127 | return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt), | |
7128 | gimple_assign_rhs1 (stmt), | |
7129 | gimple_assign_rhs2 (stmt), depth); | |
7130 | case GIMPLE_TERNARY_RHS: | |
7131 | return false; | |
7132 | case GIMPLE_SINGLE_RHS: | |
7133 | return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth); | |
7134 | case GIMPLE_INVALID_RHS: | |
7135 | break; | |
7136 | } | |
7137 | gcc_unreachable (); | |
7138 | } | |
7139 | ||
7140 | /* Return true if the floating-point value computed by call STMT is known | |
7141 | to have an integer value. We also allow +Inf, -Inf and NaN to be | |
9f27d92a | 7142 | considered integer values. Return false for signaling NaN. |
6f5f406a | 7143 | |
7144 | DEPTH is the current nesting depth of the query. */ | |
7145 | ||
7146 | static bool | |
7147 | gimple_call_integer_valued_real_p (gimple *stmt, int depth) | |
7148 | { | |
7149 | tree arg0 = (gimple_call_num_args (stmt) > 0 | |
7150 | ? gimple_call_arg (stmt, 0) | |
7151 | : NULL_TREE); | |
7152 | tree arg1 = (gimple_call_num_args (stmt) > 1 | |
7153 | ? gimple_call_arg (stmt, 1) | |
7154 | : NULL_TREE); | |
7a366e88 | 7155 | return integer_valued_real_call_p (gimple_call_combined_fn (stmt), |
6f5f406a | 7156 | arg0, arg1, depth); |
7157 | } | |
7158 | ||
7159 | /* Return true if the floating-point result of phi STMT is known to have | |
7160 | an integer value. We also allow +Inf, -Inf and NaN to be considered | |
9f27d92a | 7161 | integer values. Return false for signaling NaN. |
6f5f406a | 7162 | |
7163 | DEPTH is the current nesting depth of the query. */ | |
7164 | ||
7165 | static bool | |
7166 | gimple_phi_integer_valued_real_p (gimple *stmt, int depth) | |
7167 | { | |
7168 | for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i) | |
7169 | { | |
7170 | tree arg = gimple_phi_arg_def (stmt, i); | |
7171 | if (!integer_valued_real_single_p (arg, depth + 1)) | |
7172 | return false; | |
7173 | } | |
7174 | return true; | |
7175 | } | |
7176 | ||
7177 | /* Return true if the floating-point value computed by STMT is known | |
7178 | to have an integer value. We also allow +Inf, -Inf and NaN to be | |
9f27d92a | 7179 | considered integer values. Return false for signaling NaN. |
6f5f406a | 7180 | |
7181 | DEPTH is the current nesting depth of the query. */ | |
7182 | ||
7183 | bool | |
7184 | gimple_stmt_integer_valued_real_p (gimple *stmt, int depth) | |
7185 | { | |
7186 | switch (gimple_code (stmt)) | |
7187 | { | |
7188 | case GIMPLE_ASSIGN: | |
7189 | return gimple_assign_integer_valued_real_p (stmt, depth); | |
7190 | case GIMPLE_CALL: | |
7191 | return gimple_call_integer_valued_real_p (stmt, depth); | |
7192 | case GIMPLE_PHI: | |
7193 | return gimple_phi_integer_valued_real_p (stmt, depth); | |
7194 | default: | |
7195 | return false; | |
7196 | } | |
7197 | } |