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