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 : 4608720 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
115 : : {
116 : 4608720 : varpool_node *vnode;
117 : 4608720 : struct cgraph_node *node;
118 : 4608720 : symtab_node *snode;
119 : :
120 : 4608720 : if (DECL_ABSTRACT_P (decl))
121 : : return false;
122 : :
123 : : /* We are concerned only about static/external vars and functions. */
124 : 1511785 : if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
125 : 5716816 : || !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 : 4205031 : if (!TREE_PUBLIC (decl))
131 : : {
132 : 1091057 : 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 : 1091012 : if (symtab->function_flags_ready)
137 : : return true;
138 : 1055451 : snode = symtab_node::get (decl);
139 : 1055451 : if (!snode || !snode->definition)
140 : : return false;
141 : 1055410 : node = dyn_cast <cgraph_node *> (snode);
142 : 1064347 : 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 : 3113974 : if (!from_decl
149 : 663806 : || !VAR_P (from_decl)
150 : 662563 : || (!DECL_EXTERNAL (from_decl)
151 : 302722 : && (vnode = varpool_node::get (from_decl)) != NULL
152 : 183519 : && vnode->definition)
153 : 3593057 : || (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 : 479081 : if (DECL_VISIBILITY_SPECIFIED (decl)
161 : 472071 : && DECL_EXTERNAL (decl)
162 : 355978 : && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
163 : 735135 : && (!(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 : 223027 : 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 : 171012 : if (!symtab->function_flags_ready)
182 : : return true;
183 : :
184 : 156383 : snode = symtab_node::get (decl);
185 : 156383 : if (!snode
186 : 156383 : || ((!snode->definition || DECL_EXTERNAL (decl))
187 : 18585 : && (!snode->in_other_partition
188 : 0 : || (!snode->forced_by_abi && !snode->force_output))))
189 : : return false;
190 : 101368 : node = dyn_cast <cgraph_node *> (snode);
191 : 101368 : return !node || !node->inlined_to;
192 : : }
193 : :
194 : : /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
195 : : acceptable form for is_gimple_min_invariant.
196 : : FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
197 : :
198 : : tree
199 : 15612807 : canonicalize_constructor_val (tree cval, tree from_decl)
200 : : {
201 : 15612807 : if (CONSTANT_CLASS_P (cval))
202 : : return cval;
203 : :
204 : 9400186 : tree orig_cval = cval;
205 : 9400186 : STRIP_NOPS (cval);
206 : 9400186 : if (TREE_CODE (cval) == POINTER_PLUS_EXPR
207 : 9400186 : && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
208 : : {
209 : 69765 : tree ptr = TREE_OPERAND (cval, 0);
210 : 69765 : if (is_gimple_min_invariant (ptr))
211 : 208329 : cval = build1_loc (EXPR_LOCATION (cval),
212 : 69443 : ADDR_EXPR, TREE_TYPE (ptr),
213 : 138886 : fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
214 : : ptr,
215 : : fold_convert (ptr_type_node,
216 : : TREE_OPERAND (cval, 1))));
217 : : }
218 : 9400186 : if (TREE_CODE (cval) == ADDR_EXPR)
219 : : {
220 : 5195140 : tree base = NULL_TREE;
221 : 5195140 : if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
222 : : {
223 : 189 : base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
224 : 189 : if (base)
225 : 189 : TREE_OPERAND (cval, 0) = base;
226 : : }
227 : : else
228 : 5194951 : base = get_base_address (TREE_OPERAND (cval, 0));
229 : 5195140 : if (!base)
230 : 0 : return NULL_TREE;
231 : :
232 : 2265460 : if (VAR_OR_FUNCTION_DECL_P (base)
233 : 6491575 : && !can_refer_decl_in_current_unit_p (base, from_decl))
234 : : return NULL_TREE;
235 : 4938181 : if (TREE_TYPE (base) == error_mark_node)
236 : : return NULL_TREE;
237 : 4938181 : if (VAR_P (base))
238 : : /* ??? We should be able to assert that TREE_ADDRESSABLE is set,
239 : : but since the use can be in a debug stmt we can't. */
240 : : ;
241 : 2264457 : else if (TREE_CODE (base) == FUNCTION_DECL)
242 : : {
243 : : /* Make sure we create a cgraph node for functions we'll reference.
244 : : They can be non-existent if the reference comes from an entry
245 : : of an external vtable for example. */
246 : 1295432 : cgraph_node::get_create (base);
247 : : }
248 : : /* Fixup types in global initializers. */
249 : 4938181 : if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
250 : 37715 : cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
251 : :
252 : 4938181 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
253 : 212200 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
254 : 4938181 : return cval;
255 : : }
256 : : /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0. */
257 : 4205046 : if (TREE_CODE (cval) == INTEGER_CST)
258 : : {
259 : 61245 : if (TREE_OVERFLOW_P (cval))
260 : 0 : cval = drop_tree_overflow (cval);
261 : 61245 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
262 : 61244 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
263 : 61245 : return cval;
264 : : }
265 : : return orig_cval;
266 : : }
267 : :
268 : : /* If SYM is a constant variable with known value, return the value.
269 : : NULL_TREE is returned otherwise. */
270 : :
271 : : tree
272 : 21289018 : get_symbol_constant_value (tree sym)
273 : : {
274 : 21289018 : tree val = ctor_for_folding (sym);
275 : 21289018 : if (val != error_mark_node)
276 : : {
277 : 39867 : if (val)
278 : : {
279 : 37602 : val = canonicalize_constructor_val (unshare_expr (val), sym);
280 : 37602 : if (val
281 : 37602 : && is_gimple_min_invariant (val)
282 : 67083 : && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
283 : : return val;
284 : : else
285 : 8228 : return NULL_TREE;
286 : : }
287 : : /* Variables declared 'const' without an initializer
288 : : have zero as the initializer if they may not be
289 : : overridden at link or run time. */
290 : 2265 : if (!val
291 : 2265 : && is_gimple_reg_type (TREE_TYPE (sym)))
292 : 1917 : return build_zero_cst (TREE_TYPE (sym));
293 : : }
294 : :
295 : : return NULL_TREE;
296 : : }
297 : :
298 : :
299 : :
300 : : /* Subroutine of fold_stmt. We perform constant folding of the
301 : : memory reference tree EXPR. */
302 : :
303 : : static tree
304 : 63518653 : maybe_fold_reference (tree expr)
305 : : {
306 : 63518653 : tree result = NULL_TREE;
307 : :
308 : 63518653 : if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
309 : 61581465 : || TREE_CODE (expr) == REALPART_EXPR
310 : 60886385 : || TREE_CODE (expr) == IMAGPART_EXPR)
311 : 65043399 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
312 : 334 : result = fold_unary_loc (EXPR_LOCATION (expr),
313 : : TREE_CODE (expr),
314 : 334 : TREE_TYPE (expr),
315 : 334 : TREE_OPERAND (expr, 0));
316 : 63518319 : else if (TREE_CODE (expr) == BIT_FIELD_REF
317 : 63518319 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
318 : 4 : result = fold_ternary_loc (EXPR_LOCATION (expr),
319 : : TREE_CODE (expr),
320 : 4 : TREE_TYPE (expr),
321 : 4 : TREE_OPERAND (expr, 0),
322 : 4 : TREE_OPERAND (expr, 1),
323 : 4 : TREE_OPERAND (expr, 2));
324 : : else
325 : 63518315 : result = fold_const_aggregate_ref (expr);
326 : :
327 : 63518653 : if (result && is_gimple_min_invariant (result))
328 : : return result;
329 : :
330 : : return NULL_TREE;
331 : : }
332 : :
333 : : /* Return true if EXPR is an acceptable right-hand-side for a
334 : : GIMPLE assignment. We validate the entire tree, not just
335 : : the root node, thus catching expressions that embed complex
336 : : operands that are not permitted in GIMPLE. This function
337 : : is needed because the folding routines in fold-const.cc
338 : : may return such expressions in some cases, e.g., an array
339 : : access with an embedded index addition. It may make more
340 : : sense to have folding routines that are sensitive to the
341 : : constraints on GIMPLE operands, rather than abandoning any
342 : : any attempt to fold if the usual folding turns out to be too
343 : : aggressive. */
344 : :
345 : : bool
346 : 0 : valid_gimple_rhs_p (tree expr)
347 : : {
348 : 0 : enum tree_code code = TREE_CODE (expr);
349 : :
350 : 0 : switch (TREE_CODE_CLASS (code))
351 : : {
352 : 0 : case tcc_declaration:
353 : 0 : if (!is_gimple_variable (expr))
354 : : return false;
355 : : break;
356 : :
357 : : case tcc_constant:
358 : : /* All constants are ok. */
359 : : break;
360 : :
361 : 0 : case tcc_comparison:
362 : : /* GENERIC allows comparisons with non-boolean types, reject
363 : : those for GIMPLE. Let vector-typed comparisons pass - rules
364 : : for GENERIC and GIMPLE are the same here. */
365 : 0 : if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
366 : 0 : && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
367 : 0 : || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
368 : 0 : && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
369 : : return false;
370 : :
371 : : /* Fallthru. */
372 : 0 : case tcc_binary:
373 : 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0))
374 : 0 : || !is_gimple_val (TREE_OPERAND (expr, 1)))
375 : 0 : return false;
376 : : break;
377 : :
378 : 0 : case tcc_unary:
379 : 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0)))
380 : : return false;
381 : : break;
382 : :
383 : 0 : case tcc_expression:
384 : 0 : switch (code)
385 : : {
386 : 0 : case ADDR_EXPR:
387 : 0 : {
388 : 0 : tree t;
389 : 0 : if (is_gimple_min_invariant (expr))
390 : : return true;
391 : 0 : t = TREE_OPERAND (expr, 0);
392 : 0 : while (handled_component_p (t))
393 : : {
394 : : /* ??? More checks needed, see the GIMPLE verifier. */
395 : 0 : if ((TREE_CODE (t) == ARRAY_REF
396 : 0 : || TREE_CODE (t) == ARRAY_RANGE_REF)
397 : 0 : && !is_gimple_val (TREE_OPERAND (t, 1)))
398 : : return false;
399 : 0 : t = TREE_OPERAND (t, 0);
400 : : }
401 : 0 : if (!is_gimple_id (t))
402 : : return false;
403 : : }
404 : : break;
405 : :
406 : 0 : default:
407 : 0 : if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
408 : : {
409 : 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0))
410 : 0 : || !is_gimple_val (TREE_OPERAND (expr, 1))
411 : 0 : || !is_gimple_val (TREE_OPERAND (expr, 2)))
412 : 0 : return false;
413 : : break;
414 : : }
415 : : return false;
416 : : }
417 : : break;
418 : :
419 : : case tcc_vl_exp:
420 : : return false;
421 : :
422 : 0 : case tcc_exceptional:
423 : 0 : if (code == CONSTRUCTOR)
424 : : {
425 : : unsigned i;
426 : : tree elt;
427 : 0 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
428 : 0 : if (!is_gimple_val (elt))
429 : : return false;
430 : : return true;
431 : : }
432 : 0 : if (code != SSA_NAME)
433 : : return false;
434 : : break;
435 : :
436 : 0 : case tcc_reference:
437 : 0 : if (code == BIT_FIELD_REF)
438 : 0 : return is_gimple_val (TREE_OPERAND (expr, 0));
439 : : return false;
440 : :
441 : : default:
442 : : return false;
443 : : }
444 : :
445 : : return true;
446 : : }
447 : :
448 : :
449 : : /* Attempt to fold an assignment statement pointed-to by SI. Returns a
450 : : replacement rhs for the statement or NULL_TREE if no simplification
451 : : could be made. It is assumed that the operands have been previously
452 : : folded. */
453 : :
454 : : static tree
455 : 256545663 : fold_gimple_assign (gimple_stmt_iterator *si)
456 : : {
457 : 256545663 : gimple *stmt = gsi_stmt (*si);
458 : 256545663 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
459 : 256545663 : location_t loc = gimple_location (stmt);
460 : :
461 : 256545663 : tree result = NULL_TREE;
462 : :
463 : 256545663 : switch (get_gimple_rhs_class (subcode))
464 : : {
465 : 171123393 : case GIMPLE_SINGLE_RHS:
466 : 171123393 : {
467 : 171123393 : tree rhs = gimple_assign_rhs1 (stmt);
468 : :
469 : 171123393 : if (TREE_CLOBBER_P (rhs))
470 : : return NULL_TREE;
471 : :
472 : 154776791 : if (REFERENCE_CLASS_P (rhs))
473 : 61110606 : return maybe_fold_reference (rhs);
474 : :
475 : 93666185 : else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
476 : : {
477 : 68028 : tree val = OBJ_TYPE_REF_EXPR (rhs);
478 : 68028 : if (is_gimple_min_invariant (val))
479 : : return val;
480 : 68021 : else if (flag_devirtualize && virtual_method_call_p (rhs))
481 : : {
482 : 67981 : bool final;
483 : 67981 : vec <cgraph_node *>targets
484 : 67981 : = possible_polymorphic_call_targets (rhs, stmt, &final);
485 : 68004 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
486 : : {
487 : 23 : if (dump_enabled_p ())
488 : : {
489 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
490 : : "resolving virtual function address "
491 : : "reference to function %s\n",
492 : 0 : targets.length () == 1
493 : 0 : ? targets[0]->name ()
494 : : : "NULL");
495 : : }
496 : 23 : if (targets.length () == 1)
497 : : {
498 : 16 : val = fold_convert (TREE_TYPE (val),
499 : : build_fold_addr_expr_loc
500 : : (loc, targets[0]->decl));
501 : 16 : STRIP_USELESS_TYPE_CONVERSION (val);
502 : : }
503 : : else
504 : : /* We cannot use __builtin_unreachable here because it
505 : : cannot have address taken. */
506 : 7 : val = build_int_cst (TREE_TYPE (val), 0);
507 : 23 : return val;
508 : : }
509 : : }
510 : : }
511 : :
512 : 93598157 : else if (TREE_CODE (rhs) == ADDR_EXPR)
513 : : {
514 : 14779304 : tree ref = TREE_OPERAND (rhs, 0);
515 : 14779304 : if (TREE_CODE (ref) == MEM_REF
516 : 14779304 : && integer_zerop (TREE_OPERAND (ref, 1)))
517 : : {
518 : 2125 : result = TREE_OPERAND (ref, 0);
519 : 2125 : if (!useless_type_conversion_p (TREE_TYPE (rhs),
520 : 2125 : TREE_TYPE (result)))
521 : 0 : result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
522 : 2125 : return result;
523 : : }
524 : : }
525 : :
526 : 78818853 : else if (TREE_CODE (rhs) == CONSTRUCTOR
527 : 78818853 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
528 : : {
529 : : /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
530 : : unsigned i;
531 : : tree val;
532 : :
533 : 413186 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
534 : 408969 : if (! CONSTANT_CLASS_P (val))
535 : : return NULL_TREE;
536 : :
537 : 4217 : return build_vector_from_ctor (TREE_TYPE (rhs),
538 : 8434 : CONSTRUCTOR_ELTS (rhs));
539 : : }
540 : :
541 : 78496761 : else if (DECL_P (rhs)
542 : 78496761 : && is_gimple_reg_type (TREE_TYPE (rhs)))
543 : 12117954 : return get_symbol_constant_value (rhs);
544 : : }
545 : : break;
546 : :
547 : : case GIMPLE_UNARY_RHS:
548 : : break;
549 : :
550 : : case GIMPLE_BINARY_RHS:
551 : : break;
552 : :
553 : 437944 : case GIMPLE_TERNARY_RHS:
554 : 875888 : result = fold_ternary_loc (loc, subcode,
555 : 437944 : TREE_TYPE (gimple_assign_lhs (stmt)),
556 : : gimple_assign_rhs1 (stmt),
557 : : gimple_assign_rhs2 (stmt),
558 : : gimple_assign_rhs3 (stmt));
559 : :
560 : 437944 : if (result)
561 : : {
562 : 0 : STRIP_USELESS_TYPE_CONVERSION (result);
563 : 0 : if (valid_gimple_rhs_p (result))
564 : : return result;
565 : : }
566 : : break;
567 : :
568 : 0 : case GIMPLE_INVALID_RHS:
569 : 0 : gcc_unreachable ();
570 : : }
571 : :
572 : : return NULL_TREE;
573 : : }
574 : :
575 : :
576 : : /* Replace a statement at *SI_P with a sequence of statements in STMTS,
577 : : adjusting the replacement stmts location and virtual operands.
578 : : If the statement has a lhs the last stmt in the sequence is expected
579 : : to assign to that lhs. */
580 : :
581 : : void
582 : 114319 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
583 : : {
584 : 114319 : gimple *stmt = gsi_stmt (*si_p);
585 : :
586 : 114319 : if (gimple_has_location (stmt))
587 : 92959 : annotate_all_with_location (stmts, gimple_location (stmt));
588 : :
589 : : /* First iterate over the replacement statements backward, assigning
590 : : virtual operands to their defining statements. */
591 : 114319 : gimple *laststore = NULL;
592 : 228638 : for (gimple_stmt_iterator i = gsi_last (stmts);
593 : 426731 : !gsi_end_p (i); gsi_prev (&i))
594 : : {
595 : 156206 : gimple *new_stmt = gsi_stmt (i);
596 : 156206 : if ((gimple_assign_single_p (new_stmt)
597 : 103934 : && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
598 : 259677 : || (is_gimple_call (new_stmt)
599 : 15513 : && (gimple_call_flags (new_stmt)
600 : 15513 : & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
601 : : {
602 : 2636 : tree vdef;
603 : 2636 : if (!laststore)
604 : 2483 : vdef = gimple_vdef (stmt);
605 : : else
606 : 153 : vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
607 : 2636 : gimple_set_vdef (new_stmt, vdef);
608 : 2636 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
609 : 1638 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
610 : : laststore = new_stmt;
611 : : }
612 : : }
613 : :
614 : : /* Second iterate over the statements forward, assigning virtual
615 : : operands to their uses. */
616 : 114319 : tree reaching_vuse = gimple_vuse (stmt);
617 : 114319 : for (gimple_stmt_iterator i = gsi_start (stmts);
618 : 270525 : !gsi_end_p (i); gsi_next (&i))
619 : : {
620 : 156206 : gimple *new_stmt = gsi_stmt (i);
621 : : /* If the new statement possibly has a VUSE, update it with exact SSA
622 : : name we know will reach this one. */
623 : 156206 : if (gimple_has_mem_ops (new_stmt))
624 : 156204 : gimple_set_vuse (new_stmt, reaching_vuse);
625 : 156206 : gimple_set_modified (new_stmt, true);
626 : 466973 : if (gimple_vdef (new_stmt))
627 : 156206 : reaching_vuse = gimple_vdef (new_stmt);
628 : : }
629 : :
630 : : /* If the new sequence does not do a store release the virtual
631 : : definition of the original statement. */
632 : 114319 : if (reaching_vuse
633 : 187606 : && reaching_vuse == gimple_vuse (stmt))
634 : : {
635 : 71802 : tree vdef = gimple_vdef (stmt);
636 : 71802 : if (vdef
637 : 1478 : && TREE_CODE (vdef) == SSA_NAME)
638 : : {
639 : 1423 : unlink_stmt_vdef (stmt);
640 : 1423 : release_ssa_name (vdef);
641 : : }
642 : : }
643 : :
644 : : /* Finally replace the original statement with the sequence. */
645 : 114319 : gsi_replace_with_seq (si_p, stmts, false);
646 : 114319 : }
647 : :
648 : : /* Helper function for update_gimple_call and
649 : : gimplify_and_update_call_from_tree. A GIMPLE_CALL STMT is being replaced
650 : : with GIMPLE_CALL NEW_STMT. */
651 : :
652 : : static void
653 : 2462 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
654 : : gimple *stmt)
655 : : {
656 : 2462 : tree lhs = gimple_call_lhs (stmt);
657 : 2462 : gimple_call_set_lhs (new_stmt, lhs);
658 : 2462 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
659 : 836 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
660 : 2462 : gimple_move_vops (new_stmt, stmt);
661 : 2462 : gimple_set_location (new_stmt, gimple_location (stmt));
662 : 2462 : if (gimple_block (new_stmt) == NULL_TREE)
663 : 1 : gimple_set_block (new_stmt, gimple_block (stmt));
664 : 2462 : gsi_replace (si_p, new_stmt, false);
665 : 2462 : }
666 : :
667 : : /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
668 : : with number of arguments NARGS, where the arguments in GIMPLE form
669 : : follow NARGS argument. */
670 : :
671 : : bool
672 : 2457 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
673 : : {
674 : 2457 : va_list ap;
675 : 2457 : gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
676 : :
677 : 2457 : gcc_assert (is_gimple_call (stmt));
678 : 2457 : va_start (ap, nargs);
679 : 2457 : new_stmt = gimple_build_call_valist (fn, nargs, ap);
680 : 2457 : finish_update_gimple_call (si_p, new_stmt, stmt);
681 : 2457 : va_end (ap);
682 : 2457 : return true;
683 : : }
684 : :
685 : : /* Return true if EXPR is a CALL_EXPR suitable for representation
686 : : as a single GIMPLE_CALL statement. If the arguments require
687 : : further gimplification, return false. */
688 : :
689 : : static bool
690 : 52082 : valid_gimple_call_p (tree expr)
691 : : {
692 : 52082 : unsigned i, nargs;
693 : :
694 : 52082 : if (TREE_CODE (expr) != CALL_EXPR)
695 : : return false;
696 : :
697 : 5 : nargs = call_expr_nargs (expr);
698 : 10 : for (i = 0; i < nargs; i++)
699 : : {
700 : 5 : tree arg = CALL_EXPR_ARG (expr, i);
701 : 5 : if (is_gimple_reg_type (TREE_TYPE (arg)))
702 : : {
703 : 5 : if (!is_gimple_val (arg))
704 : : return false;
705 : : }
706 : : else
707 : 0 : if (!is_gimple_lvalue (arg))
708 : : return false;
709 : : }
710 : :
711 : : return true;
712 : : }
713 : :
714 : : /* Convert EXPR into a GIMPLE value suitable for substitution on the
715 : : RHS of an assignment. Insert the necessary statements before
716 : : iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
717 : : is replaced. If the call is expected to produces a result, then it
718 : : is replaced by an assignment of the new RHS to the result variable.
719 : : If the result is to be ignored, then the call is replaced by a
720 : : GIMPLE_NOP. A proper VDEF chain is retained by making the first
721 : : VUSE and the last VDEF of the whole sequence be the same as the replaced
722 : : statement and using new SSA names for stores in between. */
723 : :
724 : : void
725 : 52082 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
726 : : {
727 : 52082 : tree lhs;
728 : 52082 : gimple *stmt, *new_stmt;
729 : 52082 : gimple_stmt_iterator i;
730 : 52082 : gimple_seq stmts = NULL;
731 : :
732 : 52082 : stmt = gsi_stmt (*si_p);
733 : :
734 : 52082 : gcc_assert (is_gimple_call (stmt));
735 : :
736 : 52082 : if (valid_gimple_call_p (expr))
737 : : {
738 : : /* The call has simplified to another call. */
739 : 5 : tree fn = CALL_EXPR_FN (expr);
740 : 5 : unsigned i;
741 : 5 : unsigned nargs = call_expr_nargs (expr);
742 : 5 : vec<tree> args = vNULL;
743 : 5 : gcall *new_stmt;
744 : :
745 : 5 : if (nargs > 0)
746 : : {
747 : 5 : args.create (nargs);
748 : 5 : args.safe_grow_cleared (nargs, true);
749 : :
750 : 15 : for (i = 0; i < nargs; i++)
751 : 5 : args[i] = CALL_EXPR_ARG (expr, i);
752 : : }
753 : :
754 : 5 : new_stmt = gimple_build_call_vec (fn, args);
755 : 5 : finish_update_gimple_call (si_p, new_stmt, stmt);
756 : 5 : args.release ();
757 : 5 : return;
758 : : }
759 : :
760 : 52077 : lhs = gimple_call_lhs (stmt);
761 : 52077 : if (lhs == NULL_TREE)
762 : : {
763 : 13124 : push_gimplify_context (gimple_in_ssa_p (cfun));
764 : 6562 : gimplify_and_add (expr, &stmts);
765 : 6562 : pop_gimplify_context (NULL);
766 : :
767 : : /* We can end up with folding a memcpy of an empty class assignment
768 : : which gets optimized away by C++ gimplification. */
769 : 6562 : if (gimple_seq_empty_p (stmts))
770 : : {
771 : 6233 : if (gimple_in_ssa_p (cfun))
772 : : {
773 : 6233 : unlink_stmt_vdef (stmt);
774 : 6233 : release_defs (stmt);
775 : : }
776 : 6233 : gsi_replace (si_p, gimple_build_nop (), false);
777 : 6233 : return;
778 : : }
779 : : }
780 : : else
781 : : {
782 : 45515 : tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
783 : 45515 : new_stmt = gimple_build_assign (lhs, tmp);
784 : 45515 : i = gsi_last (stmts);
785 : 45515 : gsi_insert_after_without_update (&i, new_stmt,
786 : : GSI_CONTINUE_LINKING);
787 : : }
788 : :
789 : 45844 : gsi_replace_with_seq_vops (si_p, stmts);
790 : : }
791 : :
792 : : /* Print a message in the dump file recording transformation of FROM to TO. */
793 : :
794 : : static void
795 : 39643 : dump_transformation (gcall *from, gcall *to)
796 : : {
797 : 39643 : if (dump_enabled_p ())
798 : 11 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n",
799 : : gimple_call_fn (from), gimple_call_fn (to));
800 : 39643 : }
801 : :
802 : : /* Replace the call at *GSI with the gimple value VAL. */
803 : :
804 : : void
805 : 75162 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
806 : : {
807 : 75162 : gimple *stmt = gsi_stmt (*gsi);
808 : 75162 : tree lhs = gimple_call_lhs (stmt);
809 : 75162 : gimple *repl;
810 : 75162 : if (lhs)
811 : : {
812 : 72059 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
813 : 3913 : val = fold_convert (TREE_TYPE (lhs), val);
814 : 72059 : repl = gimple_build_assign (lhs, val);
815 : : }
816 : : else
817 : 3103 : repl = gimple_build_nop ();
818 : 75162 : tree vdef = gimple_vdef (stmt);
819 : 75162 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
820 : : {
821 : 3766 : unlink_stmt_vdef (stmt);
822 : 3766 : release_ssa_name (vdef);
823 : : }
824 : 75162 : gsi_replace (gsi, repl, false);
825 : 75162 : }
826 : :
827 : : /* Replace the call at *GSI with the new call REPL and fold that
828 : : again. */
829 : :
830 : : static void
831 : 39643 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
832 : : {
833 : 39643 : gimple *stmt = gsi_stmt (*gsi);
834 : 39643 : dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
835 : 39643 : gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
836 : 39643 : gimple_set_location (repl, gimple_location (stmt));
837 : 39643 : gimple_move_vops (repl, stmt);
838 : 39643 : gsi_replace (gsi, repl, false);
839 : 39643 : fold_stmt (gsi);
840 : 39643 : }
841 : :
842 : : /* Return true if VAR is a VAR_DECL or a component thereof. */
843 : :
844 : : static bool
845 : 431109 : var_decl_component_p (tree var)
846 : : {
847 : 431109 : tree inner = var;
848 : 625693 : while (handled_component_p (inner))
849 : 194584 : inner = TREE_OPERAND (inner, 0);
850 : 431109 : return (DECL_P (inner)
851 : 431109 : || (TREE_CODE (inner) == MEM_REF
852 : 51133 : && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR));
853 : : }
854 : :
855 : : /* Return TRUE if the SIZE argument, representing the size of an
856 : : object, is in a range of values of which exactly zero is valid. */
857 : :
858 : : static bool
859 : 975359 : size_must_be_zero_p (tree size)
860 : : {
861 : 975359 : if (integer_zerop (size))
862 : : return true;
863 : :
864 : 972759 : if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
865 : : return false;
866 : :
867 : 571168 : tree type = TREE_TYPE (size);
868 : 571168 : int prec = TYPE_PRECISION (type);
869 : :
870 : : /* Compute the value of SSIZE_MAX, the largest positive value that
871 : : can be stored in ssize_t, the signed counterpart of size_t. */
872 : 571168 : wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
873 : 571168 : wide_int zero = wi::zero (TYPE_PRECISION (type));
874 : 571168 : int_range_max valid_range (type, zero, ssize_max);
875 : 571168 : int_range_max vr;
876 : 1142336 : get_range_query (cfun)->range_of_expr (vr, size);
877 : :
878 : 571168 : if (vr.undefined_p ())
879 : 37 : vr.set_varying (TREE_TYPE (size));
880 : 571168 : vr.intersect (valid_range);
881 : 571168 : return vr.zero_p ();
882 : 571168 : }
883 : :
884 : : /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
885 : : diagnose (otherwise undefined) overlapping copies without preventing
886 : : folding. When folded, GCC guarantees that overlapping memcpy has
887 : : the same semantics as memmove. Call to the library memcpy need not
888 : : provide the same guarantee. Return false if no simplification can
889 : : be made. */
890 : :
891 : : static bool
892 : 975359 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
893 : : tree dest, tree src, enum built_in_function code)
894 : : {
895 : 975359 : gimple *stmt = gsi_stmt (*gsi);
896 : 975359 : tree lhs = gimple_call_lhs (stmt);
897 : 975359 : tree len = gimple_call_arg (stmt, 2);
898 : 975359 : location_t loc = gimple_location (stmt);
899 : :
900 : : /* If the LEN parameter is a constant zero or in range where
901 : : the only valid value is zero, return DEST. */
902 : 975359 : if (size_must_be_zero_p (len))
903 : : {
904 : 2632 : gimple *repl;
905 : 2632 : if (gimple_call_lhs (stmt))
906 : 58 : repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
907 : : else
908 : 2574 : repl = gimple_build_nop ();
909 : 2632 : tree vdef = gimple_vdef (stmt);
910 : 2632 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
911 : : {
912 : 547 : unlink_stmt_vdef (stmt);
913 : 547 : release_ssa_name (vdef);
914 : : }
915 : 2632 : gsi_replace (gsi, repl, false);
916 : 2632 : return true;
917 : : }
918 : :
919 : : /* If SRC and DEST are the same (and not volatile), return
920 : : DEST{,+LEN,+LEN-1}. */
921 : 972727 : if (operand_equal_p (src, dest, 0))
922 : : {
923 : : /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
924 : : It's safe and may even be emitted by GCC itself (see bug
925 : : 32667). */
926 : 68 : unlink_stmt_vdef (stmt);
927 : 136 : if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
928 : 28 : release_ssa_name (gimple_vdef (stmt));
929 : 68 : if (!lhs)
930 : : {
931 : 47 : gsi_replace (gsi, gimple_build_nop (), false);
932 : 47 : return true;
933 : : }
934 : 21 : goto done;
935 : : }
936 : 1945318 : else if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
937 : : return false;
938 : : else
939 : : {
940 : : /* We cannot (easily) change the type of the copy if it is a storage
941 : : order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
942 : : modify the storage order of objects (see storage_order_barrier_p). */
943 : 972659 : tree srctype
944 : 986538 : = POINTER_TYPE_P (TREE_TYPE (src))
945 : 986538 : ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
946 : 972659 : tree desttype
947 : 993928 : = POINTER_TYPE_P (TREE_TYPE (dest))
948 : 993928 : ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
949 : 972659 : tree destvar, srcvar, srcoff;
950 : 972659 : unsigned int src_align, dest_align;
951 : 972659 : unsigned HOST_WIDE_INT tmp_len;
952 : 972659 : const char *tmp_str;
953 : :
954 : : /* Build accesses at offset zero with a ref-all character type. */
955 : 972659 : tree off0
956 : 972659 : = build_int_cst (build_pointer_type_for_mode (char_type_node,
957 : : ptr_mode, true), 0);
958 : :
959 : : /* If we can perform the copy efficiently with first doing all loads
960 : : and then all stores inline it that way. Currently efficiently
961 : : means that we can load all the memory into a single integer
962 : : register which is what MOVE_MAX gives us. */
963 : 972659 : src_align = get_pointer_alignment (src);
964 : 972659 : dest_align = get_pointer_alignment (dest);
965 : 972659 : if (tree_fits_uhwi_p (len)
966 : 392841 : && compare_tree_int (len, MOVE_MAX) <= 0
967 : : /* FIXME: Don't transform copies from strings with known length.
968 : : Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
969 : : from being handled, and the case was XFAILed for that reason.
970 : : Now that it is handled and the XFAIL removed, as soon as other
971 : : strlenopt tests that rely on it for passing are adjusted, this
972 : : hack can be removed. */
973 : 296260 : && !c_strlen (src, 1)
974 : 183309 : && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
975 : 78654 : && memchr (tmp_str, 0, tmp_len) == NULL)
976 : 116846 : && !(srctype
977 : 116846 : && AGGREGATE_TYPE_P (srctype)
978 : 57490 : && TYPE_REVERSE_STORAGE_ORDER (srctype))
979 : 1089356 : && !(desttype
980 : 116697 : && AGGREGATE_TYPE_P (desttype)
981 : 66525 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
982 : : {
983 : 116660 : unsigned ilen = tree_to_uhwi (len);
984 : 116660 : if (pow2p_hwi (ilen))
985 : : {
986 : : /* Detect out-of-bounds accesses without issuing warnings.
987 : : Avoid folding out-of-bounds copies but to avoid false
988 : : positives for unreachable code defer warning until after
989 : : DCE has worked its magic.
990 : : -Wrestrict is still diagnosed. */
991 : 20465 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
992 : : dest, src, len, len,
993 : 20465 : false, false))
994 : 1066 : if (warning != OPT_Wrestrict)
995 : 18217 : return false;
996 : :
997 : 19457 : scalar_int_mode imode;
998 : 19457 : machine_mode mode;
999 : 19457 : if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
1000 : 19457 : && bitwise_mode_for_size (ilen
1001 : 19457 : * BITS_PER_UNIT).exists (&mode)
1002 : 38914 : && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT)
1003 : : /* If the destination pointer is not aligned we must be able
1004 : : to emit an unaligned store. */
1005 : 19457 : && (dest_align >= GET_MODE_ALIGNMENT (mode)
1006 : 11071 : || !targetm.slow_unaligned_access (mode, dest_align)
1007 : 0 : || (optab_handler (movmisalign_optab, mode)
1008 : : != CODE_FOR_nothing)))
1009 : : {
1010 : 19457 : tree type = bitwise_type_for_mode (mode);
1011 : 19457 : tree srctype = type;
1012 : 19457 : tree desttype = type;
1013 : 19457 : if (src_align < GET_MODE_ALIGNMENT (mode))
1014 : 10846 : srctype = build_aligned_type (type, src_align);
1015 : 19457 : tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
1016 : 19457 : tree tem = fold_const_aggregate_ref (srcmem);
1017 : 19457 : if (tem)
1018 : : srcmem = tem;
1019 : 18606 : else if (src_align < GET_MODE_ALIGNMENT (mode)
1020 : 10594 : && targetm.slow_unaligned_access (mode, src_align)
1021 : 18606 : && (optab_handler (movmisalign_optab, mode)
1022 : : == CODE_FOR_nothing))
1023 : : srcmem = NULL_TREE;
1024 : 18606 : if (srcmem)
1025 : : {
1026 : 19457 : gimple *new_stmt;
1027 : 19457 : if (is_gimple_reg_type (TREE_TYPE (srcmem)))
1028 : : {
1029 : 19457 : new_stmt = gimple_build_assign (NULL_TREE, srcmem);
1030 : 19457 : srcmem
1031 : 19457 : = make_ssa_name (TREE_TYPE (srcmem), new_stmt);
1032 : 19457 : gimple_assign_set_lhs (new_stmt, srcmem);
1033 : 38914 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1034 : 19457 : gimple_set_location (new_stmt, loc);
1035 : 19457 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1036 : : }
1037 : 19457 : if (dest_align < GET_MODE_ALIGNMENT (mode))
1038 : 11071 : desttype = build_aligned_type (type, dest_align);
1039 : 19457 : new_stmt
1040 : 19457 : = gimple_build_assign (fold_build2 (MEM_REF, desttype,
1041 : : dest, off0),
1042 : : srcmem);
1043 : 19457 : gimple_move_vops (new_stmt, stmt);
1044 : 19457 : if (!lhs)
1045 : : {
1046 : 17209 : gsi_replace (gsi, new_stmt, false);
1047 : 17209 : return true;
1048 : : }
1049 : 2248 : gimple_set_location (new_stmt, loc);
1050 : 2248 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1051 : 2248 : goto done;
1052 : : }
1053 : : }
1054 : : }
1055 : : }
1056 : :
1057 : 952194 : if (code == BUILT_IN_MEMMOVE)
1058 : : {
1059 : : /* Both DEST and SRC must be pointer types.
1060 : : ??? This is what old code did. Is the testing for pointer types
1061 : : really mandatory?
1062 : :
1063 : : If either SRC is readonly or length is 1, we can use memcpy. */
1064 : 195378 : if (!dest_align || !src_align)
1065 : : return false;
1066 : 195378 : if (readonly_data_expr (src)
1067 : 195378 : || (tree_fits_uhwi_p (len)
1068 : 32415 : && (MIN (src_align, dest_align) / BITS_PER_UNIT
1069 : 32415 : >= tree_to_uhwi (len))))
1070 : : {
1071 : 920815 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1072 : 18248 : if (!fn)
1073 : : return false;
1074 : 18248 : gimple_call_set_fndecl (stmt, fn);
1075 : 18248 : gimple_call_set_arg (stmt, 0, dest);
1076 : 18248 : gimple_call_set_arg (stmt, 1, src);
1077 : 18248 : fold_stmt (gsi);
1078 : 18248 : return true;
1079 : : }
1080 : :
1081 : : /* If *src and *dest can't overlap, optimize into memcpy as well. */
1082 : 177130 : if (TREE_CODE (src) == ADDR_EXPR
1083 : 5544 : && TREE_CODE (dest) == ADDR_EXPR)
1084 : : {
1085 : 1752 : tree src_base, dest_base, fn;
1086 : 1752 : poly_int64 src_offset = 0, dest_offset = 0;
1087 : 1752 : poly_uint64 maxsize;
1088 : :
1089 : 1752 : srcvar = TREE_OPERAND (src, 0);
1090 : 1752 : src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
1091 : 1752 : if (src_base == NULL)
1092 : 0 : src_base = srcvar;
1093 : 1752 : destvar = TREE_OPERAND (dest, 0);
1094 : 1752 : dest_base = get_addr_base_and_unit_offset (destvar,
1095 : : &dest_offset);
1096 : 1752 : if (dest_base == NULL)
1097 : 0 : dest_base = destvar;
1098 : 1752 : if (!poly_int_tree_p (len, &maxsize))
1099 : 224 : maxsize = -1;
1100 : 1752 : if (SSA_VAR_P (src_base)
1101 : 1742 : && SSA_VAR_P (dest_base))
1102 : : {
1103 : 1742 : if (operand_equal_p (src_base, dest_base, 0)
1104 : 1742 : && ranges_maybe_overlap_p (src_offset, maxsize,
1105 : : dest_offset, maxsize))
1106 : : return false;
1107 : : }
1108 : 10 : else if (TREE_CODE (src_base) == MEM_REF
1109 : 0 : && TREE_CODE (dest_base) == MEM_REF)
1110 : : {
1111 : 0 : if (! operand_equal_p (TREE_OPERAND (src_base, 0),
1112 : 0 : TREE_OPERAND (dest_base, 0), 0))
1113 : 0 : return false;
1114 : 0 : poly_offset_int full_src_offset
1115 : 0 : = mem_ref_offset (src_base) + src_offset;
1116 : 0 : poly_offset_int full_dest_offset
1117 : 0 : = mem_ref_offset (dest_base) + dest_offset;
1118 : 0 : if (ranges_maybe_overlap_p (full_src_offset, maxsize,
1119 : : full_dest_offset, maxsize))
1120 : : return false;
1121 : 0 : }
1122 : : else
1123 : : return false;
1124 : :
1125 : 1752 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1126 : 1307 : if (!fn)
1127 : : return false;
1128 : 1307 : gimple_call_set_fndecl (stmt, fn);
1129 : 1307 : gimple_call_set_arg (stmt, 0, dest);
1130 : 1307 : gimple_call_set_arg (stmt, 1, src);
1131 : 1307 : fold_stmt (gsi);
1132 : 1307 : return true;
1133 : : }
1134 : :
1135 : : /* If the destination and source do not alias optimize into
1136 : : memcpy as well. */
1137 : 175378 : if ((is_gimple_min_invariant (dest)
1138 : 171792 : || TREE_CODE (dest) == SSA_NAME)
1139 : 330433 : && (is_gimple_min_invariant (src)
1140 : 154861 : || TREE_CODE (src) == SSA_NAME))
1141 : : {
1142 : 158230 : ao_ref destr, srcr;
1143 : 158230 : ao_ref_init_from_ptr_and_size (&destr, dest, len);
1144 : 158230 : ao_ref_init_from_ptr_and_size (&srcr, src, len);
1145 : 158230 : if (!refs_may_alias_p_1 (&destr, &srcr, false))
1146 : : {
1147 : 9805 : tree fn;
1148 : 9805 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1149 : 9805 : if (!fn)
1150 : 9805 : return false;
1151 : 9805 : gimple_call_set_fndecl (stmt, fn);
1152 : 9805 : gimple_call_set_arg (stmt, 0, dest);
1153 : 9805 : gimple_call_set_arg (stmt, 1, src);
1154 : 9805 : fold_stmt (gsi);
1155 : 9805 : return true;
1156 : : }
1157 : : }
1158 : :
1159 : 165573 : return false;
1160 : : }
1161 : :
1162 : 756816 : if (!tree_fits_shwi_p (len))
1163 : : return false;
1164 : 324626 : if (!srctype
1165 : 324626 : || (AGGREGATE_TYPE_P (srctype)
1166 : 207998 : && TYPE_REVERSE_STORAGE_ORDER (srctype)))
1167 : : return false;
1168 : 324477 : if (!desttype
1169 : 324477 : || (AGGREGATE_TYPE_P (desttype)
1170 : 199827 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
1171 : : return false;
1172 : : /* In the following try to find a type that is most natural to be
1173 : : used for the memcpy source and destination and that allows
1174 : : the most optimization when memcpy is turned into a plain assignment
1175 : : using that type. In theory we could always use a char[len] type
1176 : : but that only gains us that the destination and source possibly
1177 : : no longer will have their address taken. */
1178 : 324440 : if (TREE_CODE (srctype) == ARRAY_TYPE
1179 : 324440 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1180 : 130552 : srctype = TREE_TYPE (srctype);
1181 : 324440 : if (TREE_CODE (desttype) == ARRAY_TYPE
1182 : 324440 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
1183 : 116734 : desttype = TREE_TYPE (desttype);
1184 : 324440 : if (TREE_ADDRESSABLE (srctype)
1185 : 324428 : || TREE_ADDRESSABLE (desttype))
1186 : : return false;
1187 : :
1188 : : /* Make sure we are not copying using a floating-point mode or
1189 : : a type whose size possibly does not match its precision. */
1190 : 648116 : if (FLOAT_MODE_P (TYPE_MODE (desttype))
1191 : 323422 : || TREE_CODE (desttype) == BOOLEAN_TYPE
1192 : 647819 : || TREE_CODE (desttype) == ENUMERAL_TYPE)
1193 : 1023 : desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
1194 : 648348 : if (FLOAT_MODE_P (TYPE_MODE (srctype))
1195 : 323799 : || TREE_CODE (srctype) == BOOLEAN_TYPE
1196 : 648200 : || TREE_CODE (srctype) == ENUMERAL_TYPE)
1197 : 642 : srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
1198 : 324421 : if (!srctype)
1199 : 137 : srctype = desttype;
1200 : 324421 : if (!desttype)
1201 : 0 : desttype = srctype;
1202 : 324421 : if (!srctype)
1203 : : return false;
1204 : :
1205 : 324421 : src_align = get_pointer_alignment (src);
1206 : 324421 : dest_align = get_pointer_alignment (dest);
1207 : :
1208 : : /* Choose between src and destination type for the access based
1209 : : on alignment, whether the access constitutes a register access
1210 : : and whether it may actually expose a declaration for SSA rewrite
1211 : : or SRA decomposition. Also try to expose a string constant, we
1212 : : might be able to concatenate several of them later into a single
1213 : : string store. */
1214 : 324421 : destvar = NULL_TREE;
1215 : 324421 : srcvar = NULL_TREE;
1216 : 324421 : if (TREE_CODE (dest) == ADDR_EXPR
1217 : 124485 : && var_decl_component_p (TREE_OPERAND (dest, 0))
1218 : 124481 : && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
1219 : 22126 : && dest_align >= TYPE_ALIGN (desttype)
1220 : 346547 : && (is_gimple_reg_type (desttype)
1221 : 21703 : || src_align >= TYPE_ALIGN (desttype)))
1222 : 17128 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1223 : 307293 : else if (TREE_CODE (src) == ADDR_EXPR
1224 : 240111 : && var_decl_component_p (TREE_OPERAND (src, 0))
1225 : 44810 : && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
1226 : 8930 : && src_align >= TYPE_ALIGN (srctype)
1227 : 316205 : && (is_gimple_reg_type (srctype)
1228 : 8744 : || dest_align >= TYPE_ALIGN (srctype)))
1229 : 2816 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1230 : : /* FIXME: Don't transform copies from strings with known original length.
1231 : : As soon as strlenopt tests that rely on it for passing are adjusted,
1232 : : this hack can be removed. */
1233 : 304477 : else if (gimple_call_alloca_for_var_p (stmt)
1234 : 3 : && (srcvar = string_constant (src, &srcoff, NULL, NULL))
1235 : 3 : && integer_zerop (srcoff)
1236 : 3 : && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar)), len)
1237 : 304480 : && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
1238 : 3 : srctype = TREE_TYPE (srcvar);
1239 : : else
1240 : 304474 : return false;
1241 : :
1242 : : /* Now that we chose an access type express the other side in
1243 : : terms of it if the target allows that with respect to alignment
1244 : : constraints. */
1245 : 19947 : if (srcvar == NULL_TREE)
1246 : : {
1247 : 17128 : if (src_align >= TYPE_ALIGN (desttype))
1248 : 17114 : srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1249 : : else
1250 : : {
1251 : 14 : enum machine_mode mode = TYPE_MODE (desttype);
1252 : 14 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1253 : 14 : || (targetm.slow_unaligned_access (mode, src_align)
1254 : 14 : && (optab_handler (movmisalign_optab, mode)
1255 : : == CODE_FOR_nothing)))
1256 : : return false;
1257 : 14 : srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1258 : : src_align);
1259 : 14 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1260 : : }
1261 : : }
1262 : 2819 : else if (destvar == NULL_TREE)
1263 : : {
1264 : 2819 : if (dest_align >= TYPE_ALIGN (srctype))
1265 : 2819 : destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1266 : : else
1267 : : {
1268 : 0 : enum machine_mode mode = TYPE_MODE (srctype);
1269 : 0 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1270 : 0 : || (targetm.slow_unaligned_access (mode, dest_align)
1271 : 0 : && (optab_handler (movmisalign_optab, mode)
1272 : : == CODE_FOR_nothing)))
1273 : : return false;
1274 : 0 : desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1275 : : dest_align);
1276 : 0 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1277 : : }
1278 : : }
1279 : :
1280 : : /* Same as above, detect out-of-bounds accesses without issuing
1281 : : warnings. Avoid folding out-of-bounds copies but to avoid
1282 : : false positives for unreachable code defer warning until
1283 : : after DCE has worked its magic.
1284 : : -Wrestrict is still diagnosed. */
1285 : 19947 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
1286 : : dest, src, len, len,
1287 : 19947 : false, false))
1288 : 133 : if (warning != OPT_Wrestrict)
1289 : : return false;
1290 : :
1291 : 19822 : gimple *new_stmt;
1292 : 19822 : if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1293 : : {
1294 : 546 : tree tem = fold_const_aggregate_ref (srcvar);
1295 : 546 : if (tem)
1296 : 529 : srcvar = tem;
1297 : 546 : if (! is_gimple_min_invariant (srcvar))
1298 : : {
1299 : 17 : new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1300 : 17 : srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
1301 : 17 : gimple_assign_set_lhs (new_stmt, srcvar);
1302 : 34 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1303 : 17 : gimple_set_location (new_stmt, loc);
1304 : 17 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1305 : : }
1306 : 546 : new_stmt = gimple_build_assign (destvar, srcvar);
1307 : 546 : goto set_vop_and_replace;
1308 : : }
1309 : :
1310 : : /* We get an aggregate copy. If the source is a STRING_CST, then
1311 : : directly use its type to perform the copy. */
1312 : 19276 : if (TREE_CODE (srcvar) == STRING_CST)
1313 : : desttype = srctype;
1314 : :
1315 : : /* Or else, use an unsigned char[] type to perform the copy in order
1316 : : to preserve padding and to avoid any issues with TREE_ADDRESSABLE
1317 : : types or float modes behavior on copying. */
1318 : : else
1319 : : {
1320 : 38546 : desttype = build_array_type_nelts (unsigned_char_type_node,
1321 : 19273 : tree_to_uhwi (len));
1322 : 19273 : srctype = desttype;
1323 : 19273 : if (src_align > TYPE_ALIGN (srctype))
1324 : 11915 : srctype = build_aligned_type (srctype, src_align);
1325 : 19273 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1326 : : }
1327 : :
1328 : 19276 : if (dest_align > TYPE_ALIGN (desttype))
1329 : 12556 : desttype = build_aligned_type (desttype, dest_align);
1330 : 19276 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1331 : 19276 : new_stmt = gimple_build_assign (destvar, srcvar);
1332 : :
1333 : 19822 : set_vop_and_replace:
1334 : 19822 : gimple_move_vops (new_stmt, stmt);
1335 : 19822 : if (!lhs)
1336 : : {
1337 : 19347 : gsi_replace (gsi, new_stmt, false);
1338 : 19347 : return true;
1339 : : }
1340 : 475 : gimple_set_location (new_stmt, loc);
1341 : 475 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1342 : : }
1343 : :
1344 : 2744 : done:
1345 : 2744 : gimple_seq stmts = NULL;
1346 : 2744 : if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
1347 : 2744 : len = NULL_TREE;
1348 : 197 : else if (code == BUILT_IN_MEMPCPY)
1349 : : {
1350 : 197 : len = gimple_convert_to_ptrofftype (&stmts, loc, len);
1351 : 197 : dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
1352 : 197 : TREE_TYPE (dest), dest, len);
1353 : : }
1354 : : else
1355 : 0 : gcc_unreachable ();
1356 : :
1357 : 2744 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1358 : 2744 : gimple *repl = gimple_build_assign (lhs, dest);
1359 : 2744 : gsi_replace (gsi, repl, false);
1360 : 2744 : return true;
1361 : : }
1362 : :
1363 : : /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1364 : : to built-in memcmp (a, b, len). */
1365 : :
1366 : : static bool
1367 : 148 : gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
1368 : : {
1369 : 148 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
1370 : :
1371 : 148 : if (!fn)
1372 : : return false;
1373 : :
1374 : : /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1375 : :
1376 : 148 : gimple *stmt = gsi_stmt (*gsi);
1377 : 296 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
1378 : : return false;
1379 : 148 : tree a = gimple_call_arg (stmt, 0);
1380 : 148 : tree b = gimple_call_arg (stmt, 1);
1381 : 148 : tree len = gimple_call_arg (stmt, 2);
1382 : :
1383 : 148 : gimple *repl = gimple_build_call (fn, 3, a, b, len);
1384 : 148 : replace_call_with_call_and_fold (gsi, repl);
1385 : :
1386 : 148 : return true;
1387 : : }
1388 : :
1389 : : /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1390 : : to built-in memmove (dest, src, len). */
1391 : :
1392 : : static bool
1393 : 367 : gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
1394 : : {
1395 : 367 : tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
1396 : :
1397 : 367 : if (!fn)
1398 : : return false;
1399 : :
1400 : : /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1401 : : it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1402 : : len) into memmove (dest, src, len). */
1403 : :
1404 : 367 : gimple *stmt = gsi_stmt (*gsi);
1405 : 734 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1406 : : return false;
1407 : 367 : tree src = gimple_call_arg (stmt, 0);
1408 : 367 : tree dest = gimple_call_arg (stmt, 1);
1409 : 367 : tree len = gimple_call_arg (stmt, 2);
1410 : :
1411 : 367 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1412 : 367 : gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
1413 : 367 : replace_call_with_call_and_fold (gsi, repl);
1414 : :
1415 : 367 : return true;
1416 : : }
1417 : :
1418 : : /* Transform a call to built-in bzero (dest, len) at *GSI into one
1419 : : to built-in memset (dest, 0, len). */
1420 : :
1421 : : static bool
1422 : 250 : gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
1423 : : {
1424 : 250 : tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
1425 : :
1426 : 250 : if (!fn)
1427 : : return false;
1428 : :
1429 : : /* Transform bzero (dest, len) into memset (dest, 0, len). */
1430 : :
1431 : 250 : gimple *stmt = gsi_stmt (*gsi);
1432 : 500 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1433 : : return false;
1434 : 250 : tree dest = gimple_call_arg (stmt, 0);
1435 : 250 : tree len = gimple_call_arg (stmt, 1);
1436 : :
1437 : 250 : gimple_seq seq = NULL;
1438 : 250 : gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
1439 : 250 : gimple_seq_add_stmt_without_update (&seq, repl);
1440 : 250 : gsi_replace_with_seq_vops (gsi, seq);
1441 : 250 : fold_stmt (gsi);
1442 : :
1443 : 250 : return true;
1444 : : }
1445 : :
1446 : : /* Fold function call to builtin memset or bzero at *GSI setting the
1447 : : memory of size LEN to VAL. Return whether a simplification was made. */
1448 : :
1449 : : static bool
1450 : 334881 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1451 : : {
1452 : 334881 : gimple *stmt = gsi_stmt (*gsi);
1453 : 334881 : tree etype;
1454 : 334881 : unsigned HOST_WIDE_INT length, cval;
1455 : :
1456 : : /* If the LEN parameter is zero, return DEST. */
1457 : 334881 : if (integer_zerop (len))
1458 : : {
1459 : 811 : replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1460 : 811 : return true;
1461 : : }
1462 : :
1463 : 1000454 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1464 : : return false;
1465 : :
1466 : 334070 : if (! tree_fits_uhwi_p (len))
1467 : : return false;
1468 : :
1469 : 213884 : if (TREE_CODE (c) != INTEGER_CST)
1470 : : return false;
1471 : :
1472 : 207735 : tree dest = gimple_call_arg (stmt, 0);
1473 : 207735 : tree var = dest;
1474 : 207735 : if (TREE_CODE (var) != ADDR_EXPR)
1475 : : return false;
1476 : :
1477 : 166913 : var = TREE_OPERAND (var, 0);
1478 : 166913 : if (TREE_THIS_VOLATILE (var))
1479 : : return false;
1480 : :
1481 : 166870 : etype = TREE_TYPE (var);
1482 : 166870 : if (TREE_CODE (etype) == ARRAY_TYPE)
1483 : 89175 : etype = TREE_TYPE (etype);
1484 : :
1485 : 166870 : if ((!INTEGRAL_TYPE_P (etype)
1486 : 100934 : && !POINTER_TYPE_P (etype))
1487 : 66558 : || TREE_CODE (etype) == BITINT_TYPE)
1488 : : return false;
1489 : :
1490 : 66513 : if (! var_decl_component_p (var))
1491 : : return false;
1492 : :
1493 : 66513 : length = tree_to_uhwi (len);
1494 : 66513 : if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
1495 : 1756 : || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
1496 : 3512 : != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
1497 : 68269 : || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1498 : 64757 : return false;
1499 : :
1500 : 1756 : if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1501 : : return false;
1502 : :
1503 : 1756 : if (!type_has_mode_precision_p (etype))
1504 : 7 : etype = lang_hooks.types.type_for_mode (SCALAR_INT_TYPE_MODE (etype),
1505 : 7 : TYPE_UNSIGNED (etype));
1506 : :
1507 : 1756 : if (integer_zerop (c))
1508 : : cval = 0;
1509 : : else
1510 : : {
1511 : 334 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1512 : : return NULL_TREE;
1513 : :
1514 : 334 : cval = TREE_INT_CST_LOW (c);
1515 : 334 : cval &= 0xff;
1516 : 334 : cval |= cval << 8;
1517 : 334 : cval |= cval << 16;
1518 : 334 : cval |= (cval << 31) << 1;
1519 : : }
1520 : :
1521 : 1756 : var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1522 : 1756 : gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1523 : 1756 : gimple_move_vops (store, stmt);
1524 : 1756 : gimple_set_location (store, gimple_location (stmt));
1525 : 1756 : gsi_insert_before (gsi, store, GSI_SAME_STMT);
1526 : 1756 : if (gimple_call_lhs (stmt))
1527 : : {
1528 : 2 : gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1529 : 2 : gsi_replace (gsi, asgn, false);
1530 : : }
1531 : : else
1532 : : {
1533 : 1754 : gimple_stmt_iterator gsi2 = *gsi;
1534 : 1754 : gsi_prev (gsi);
1535 : 1754 : gsi_remove (&gsi2, true);
1536 : : }
1537 : :
1538 : : return true;
1539 : : }
1540 : :
1541 : : /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1542 : :
1543 : : static bool
1544 : 621670 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
1545 : : c_strlen_data *pdata, unsigned eltsize)
1546 : : {
1547 : 621670 : gcc_assert (TREE_CODE (arg) != SSA_NAME);
1548 : :
1549 : : /* The length computed by this invocation of the function. */
1550 : 621670 : tree val = NULL_TREE;
1551 : :
1552 : : /* True if VAL is an optimistic (tight) bound determined from
1553 : : the size of the character array in which the string may be
1554 : : stored. In that case, the computed VAL is used to set
1555 : : PDATA->MAXBOUND. */
1556 : 621670 : bool tight_bound = false;
1557 : :
1558 : : /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1559 : 621670 : if (TREE_CODE (arg) == ADDR_EXPR
1560 : 621670 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
1561 : : {
1562 : 34219 : tree op = TREE_OPERAND (arg, 0);
1563 : 34219 : if (integer_zerop (TREE_OPERAND (op, 1)))
1564 : : {
1565 : 13150 : tree aop0 = TREE_OPERAND (op, 0);
1566 : 13150 : if (TREE_CODE (aop0) == INDIRECT_REF
1567 : 13150 : && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1568 : 0 : return get_range_strlen (TREE_OPERAND (aop0, 0), visited, rkind,
1569 : 0 : pdata, eltsize);
1570 : : }
1571 : 21069 : else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
1572 : 21069 : && rkind == SRK_LENRANGE)
1573 : : {
1574 : : /* Fail if an array is the last member of a struct object
1575 : : since it could be treated as a (fake) flexible array
1576 : : member. */
1577 : 5082 : tree idx = TREE_OPERAND (op, 1);
1578 : :
1579 : 5082 : arg = TREE_OPERAND (op, 0);
1580 : 5082 : tree optype = TREE_TYPE (arg);
1581 : 5082 : if (tree dom = TYPE_DOMAIN (optype))
1582 : 5082 : if (tree bound = TYPE_MAX_VALUE (dom))
1583 : 5082 : if (TREE_CODE (bound) == INTEGER_CST
1584 : 5082 : && TREE_CODE (idx) == INTEGER_CST
1585 : 8594 : && tree_int_cst_lt (bound, idx))
1586 : : return false;
1587 : : }
1588 : : }
1589 : :
1590 : 621454 : if (rkind == SRK_INT_VALUE)
1591 : : {
1592 : : /* We are computing the maximum value (not string length). */
1593 : 26295 : val = arg;
1594 : 26295 : if (TREE_CODE (val) != INTEGER_CST
1595 : 26295 : || tree_int_cst_sgn (val) < 0)
1596 : 2787 : return false;
1597 : : }
1598 : : else
1599 : : {
1600 : 595159 : c_strlen_data lendata = { };
1601 : 595159 : val = c_strlen (arg, 1, &lendata, eltsize);
1602 : :
1603 : 595159 : if (!val && lendata.decl)
1604 : : {
1605 : : /* ARG refers to an unterminated const character array.
1606 : : DATA.DECL with size DATA.LEN. */
1607 : 4559 : val = lendata.minlen;
1608 : 4559 : pdata->decl = lendata.decl;
1609 : : }
1610 : : }
1611 : :
1612 : : /* Set if VAL represents the maximum length based on array size (set
1613 : : when exact length cannot be determined). */
1614 : 618667 : bool maxbound = false;
1615 : :
1616 : 618667 : if (!val && rkind == SRK_LENRANGE)
1617 : : {
1618 : 388653 : if (TREE_CODE (arg) == ADDR_EXPR)
1619 : 156675 : return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
1620 : 156675 : pdata, eltsize);
1621 : :
1622 : 231978 : if (TREE_CODE (arg) == ARRAY_REF)
1623 : : {
1624 : 28754 : tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
1625 : :
1626 : : /* Determine the "innermost" array type. */
1627 : 28754 : while (TREE_CODE (optype) == ARRAY_TYPE
1628 : 36151 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1629 : 7397 : optype = TREE_TYPE (optype);
1630 : :
1631 : : /* Avoid arrays of pointers. */
1632 : 28754 : tree eltype = TREE_TYPE (optype);
1633 : 28754 : if (TREE_CODE (optype) != ARRAY_TYPE
1634 : 28754 : || !INTEGRAL_TYPE_P (eltype))
1635 : : return false;
1636 : :
1637 : : /* Fail when the array bound is unknown or zero. */
1638 : 16136 : val = TYPE_SIZE_UNIT (optype);
1639 : 16136 : if (!val
1640 : 16064 : || TREE_CODE (val) != INTEGER_CST
1641 : 32168 : || integer_zerop (val))
1642 : 113 : return false;
1643 : :
1644 : 16023 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1645 : : integer_one_node);
1646 : :
1647 : : /* Set the minimum size to zero since the string in
1648 : : the array could have zero length. */
1649 : 16023 : pdata->minlen = ssize_int (0);
1650 : :
1651 : 16023 : tight_bound = true;
1652 : : }
1653 : 203224 : else if (TREE_CODE (arg) == COMPONENT_REF
1654 : 203224 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
1655 : : == ARRAY_TYPE))
1656 : : {
1657 : : /* Use the type of the member array to determine the upper
1658 : : bound on the length of the array. This may be overly
1659 : : optimistic if the array itself isn't NUL-terminated and
1660 : : the caller relies on the subsequent member to contain
1661 : : the NUL but that would only be considered valid if
1662 : : the array were the last member of a struct. */
1663 : :
1664 : 10492 : tree fld = TREE_OPERAND (arg, 1);
1665 : :
1666 : 10492 : tree optype = TREE_TYPE (fld);
1667 : :
1668 : : /* Determine the "innermost" array type. */
1669 : 10492 : while (TREE_CODE (optype) == ARRAY_TYPE
1670 : 11140 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1671 : 648 : optype = TREE_TYPE (optype);
1672 : :
1673 : : /* Fail when the array bound is unknown or zero. */
1674 : 10492 : val = TYPE_SIZE_UNIT (optype);
1675 : 10492 : if (!val
1676 : 10236 : || TREE_CODE (val) != INTEGER_CST
1677 : 20693 : || integer_zerop (val))
1678 : 376 : return false;
1679 : 10116 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1680 : : integer_one_node);
1681 : :
1682 : : /* Set the minimum size to zero since the string in
1683 : : the array could have zero length. */
1684 : 10116 : pdata->minlen = ssize_int (0);
1685 : :
1686 : : /* The array size determined above is an optimistic bound
1687 : : on the length. If the array isn't nul-terminated the
1688 : : length computed by the library function would be greater.
1689 : : Even though using strlen to cross the subobject boundary
1690 : : is undefined, avoid drawing conclusions from the member
1691 : : type about the length here. */
1692 : 10116 : tight_bound = true;
1693 : : }
1694 : 192732 : else if (TREE_CODE (arg) == MEM_REF
1695 : 29265 : && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
1696 : 4822 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
1697 : 197032 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
1698 : : {
1699 : : /* Handle a MEM_REF into a DECL accessing an array of integers,
1700 : : being conservative about references to extern structures with
1701 : : flexible array members that can be initialized to arbitrary
1702 : : numbers of elements as an extension (static structs are okay). */
1703 : 4300 : tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1704 : 4300 : if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
1705 : 8586 : && (decl_binds_to_current_def_p (ref)
1706 : 457 : || !array_ref_flexible_size_p (arg)))
1707 : : {
1708 : : /* Fail if the offset is out of bounds. Such accesses
1709 : : should be diagnosed at some point. */
1710 : 4161 : val = DECL_SIZE_UNIT (ref);
1711 : 4161 : if (!val
1712 : 3989 : || TREE_CODE (val) != INTEGER_CST
1713 : 8150 : || integer_zerop (val))
1714 : 386 : return false;
1715 : :
1716 : 3987 : poly_offset_int psiz = wi::to_offset (val);
1717 : 3987 : poly_offset_int poff = mem_ref_offset (arg);
1718 : 3987 : if (known_le (psiz, poff))
1719 : : return false;
1720 : :
1721 : 3775 : pdata->minlen = ssize_int (0);
1722 : :
1723 : : /* Subtract the offset and one for the terminating nul. */
1724 : 3775 : psiz -= poff;
1725 : 3775 : psiz -= 1;
1726 : 3775 : val = wide_int_to_tree (TREE_TYPE (val), psiz);
1727 : : /* Since VAL reflects the size of a declared object
1728 : : rather the type of the access it is not a tight bound. */
1729 : : }
1730 : : }
1731 : 188432 : else if (TREE_CODE (arg) == PARM_DECL || VAR_P (arg))
1732 : : {
1733 : : /* Avoid handling pointers to arrays. GCC might misuse
1734 : : a pointer to an array of one bound to point to an array
1735 : : object of a greater bound. */
1736 : 136068 : tree argtype = TREE_TYPE (arg);
1737 : 136068 : if (TREE_CODE (argtype) == ARRAY_TYPE)
1738 : : {
1739 : 46607 : val = TYPE_SIZE_UNIT (argtype);
1740 : 46607 : if (!val
1741 : 45813 : || TREE_CODE (val) != INTEGER_CST
1742 : 92420 : || integer_zerop (val))
1743 : 911 : return false;
1744 : 45696 : val = wide_int_to_tree (TREE_TYPE (val),
1745 : 45696 : wi::sub (wi::to_wide (val), 1));
1746 : :
1747 : : /* Set the minimum size to zero since the string in
1748 : : the array could have zero length. */
1749 : 45696 : pdata->minlen = ssize_int (0);
1750 : : }
1751 : : }
1752 : : maxbound = true;
1753 : : }
1754 : :
1755 : 447588 : if (!val)
1756 : : return false;
1757 : :
1758 : : /* Adjust the lower bound on the string length as necessary. */
1759 : 279735 : if (!pdata->minlen
1760 : 279735 : || (rkind != SRK_STRLEN
1761 : 81682 : && TREE_CODE (pdata->minlen) == INTEGER_CST
1762 : 81682 : && TREE_CODE (val) == INTEGER_CST
1763 : 81677 : && tree_int_cst_lt (val, pdata->minlen)))
1764 : 197772 : pdata->minlen = val;
1765 : :
1766 : 279735 : if (pdata->maxbound && TREE_CODE (pdata->maxbound) == INTEGER_CST)
1767 : : {
1768 : : /* Adjust the tighter (more optimistic) string length bound
1769 : : if necessary and proceed to adjust the more conservative
1770 : : bound. */
1771 : 1804 : if (TREE_CODE (val) == INTEGER_CST)
1772 : : {
1773 : 1804 : if (tree_int_cst_lt (pdata->maxbound, val))
1774 : 612 : pdata->maxbound = val;
1775 : : }
1776 : : else
1777 : 0 : pdata->maxbound = val;
1778 : : }
1779 : 277931 : else if (pdata->maxbound || maxbound)
1780 : : /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
1781 : : if VAL corresponds to the maximum length determined based
1782 : : on the type of the object. */
1783 : 78612 : pdata->maxbound = val;
1784 : :
1785 : 279735 : if (tight_bound)
1786 : : {
1787 : : /* VAL computed above represents an optimistically tight bound
1788 : : on the length of the string based on the referenced object's
1789 : : or subobject's type. Determine the conservative upper bound
1790 : : based on the enclosing object's size if possible. */
1791 : 26139 : if (rkind == SRK_LENRANGE)
1792 : : {
1793 : 26139 : poly_int64 offset;
1794 : 26139 : tree base = get_addr_base_and_unit_offset (arg, &offset);
1795 : 26139 : if (!base)
1796 : : {
1797 : : /* When the call above fails due to a non-constant offset
1798 : : assume the offset is zero and use the size of the whole
1799 : : enclosing object instead. */
1800 : 7832 : base = get_base_address (arg);
1801 : 7832 : offset = 0;
1802 : : }
1803 : : /* If the base object is a pointer no upper bound on the length
1804 : : can be determined. Otherwise the maximum length is equal to
1805 : : the size of the enclosing object minus the offset of
1806 : : the referenced subobject minus 1 (for the terminating nul). */
1807 : 26139 : tree type = TREE_TYPE (base);
1808 : 26139 : if (TREE_CODE (type) == POINTER_TYPE
1809 : 26135 : || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
1810 : 45904 : || !(val = DECL_SIZE_UNIT (base)))
1811 : 7627 : val = build_all_ones_cst (size_type_node);
1812 : : else
1813 : : {
1814 : 18512 : val = DECL_SIZE_UNIT (base);
1815 : 18512 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1816 : : size_int (offset + 1));
1817 : : }
1818 : : }
1819 : : else
1820 : : return false;
1821 : : }
1822 : :
1823 : 279735 : if (pdata->maxlen)
1824 : : {
1825 : : /* Adjust the more conservative bound if possible/necessary
1826 : : and fail otherwise. */
1827 : 10089 : if (rkind != SRK_STRLEN)
1828 : : {
1829 : 9052 : if (TREE_CODE (pdata->maxlen) != INTEGER_CST
1830 : 9052 : || TREE_CODE (val) != INTEGER_CST)
1831 : : return false;
1832 : :
1833 : 9047 : if (tree_int_cst_lt (pdata->maxlen, val))
1834 : 1437 : pdata->maxlen = val;
1835 : 9047 : return true;
1836 : : }
1837 : 1037 : else if (simple_cst_equal (val, pdata->maxlen) != 1)
1838 : : {
1839 : : /* Fail if the length of this ARG is different from that
1840 : : previously determined from another ARG. */
1841 : : return false;
1842 : : }
1843 : : }
1844 : :
1845 : 269772 : pdata->maxlen = val;
1846 : 269772 : return rkind == SRK_LENRANGE || !integer_all_onesp (val);
1847 : : }
1848 : :
1849 : : /* For an ARG referencing one or more strings, try to obtain the range
1850 : : of their lengths, or the size of the largest array ARG referes to if
1851 : : the range of lengths cannot be determined, and store all in *PDATA.
1852 : : For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1853 : : the maximum constant value.
1854 : : If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1855 : : SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1856 : : length or if we are unable to determine the length, return false.
1857 : : VISITED is a bitmap of visited variables.
1858 : : RKIND determines the kind of value or range to obtain (see
1859 : : strlen_range_kind).
1860 : : Set PDATA->DECL if ARG refers to an unterminated constant array.
1861 : : On input, set ELTSIZE to 1 for normal single byte character strings,
1862 : : and either 2 or 4 for wide characer strings (the size of wchar_t).
1863 : : Return true if *PDATA was successfully populated and false otherwise. */
1864 : :
1865 : : static bool
1866 : 1543332 : get_range_strlen (tree arg, bitmap visited,
1867 : : strlen_range_kind rkind,
1868 : : c_strlen_data *pdata, unsigned eltsize)
1869 : : {
1870 : :
1871 : 1637766 : if (TREE_CODE (arg) != SSA_NAME)
1872 : 621670 : return get_range_strlen_tree (arg, visited, rkind, pdata, eltsize);
1873 : :
1874 : : /* If ARG is registered for SSA update we cannot look at its defining
1875 : : statement. */
1876 : 1016096 : if (name_registered_for_update_p (arg))
1877 : : return false;
1878 : :
1879 : : /* If we were already here, break the infinite cycle. */
1880 : 1016096 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
1881 : : return true;
1882 : :
1883 : 1013279 : tree var = arg;
1884 : 1013279 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
1885 : :
1886 : 1013279 : switch (gimple_code (def_stmt))
1887 : : {
1888 : 148513 : case GIMPLE_ASSIGN:
1889 : : /* The RHS of the statement defining VAR must either have a
1890 : : constant length or come from another SSA_NAME with a constant
1891 : : length. */
1892 : 148513 : if (gimple_assign_single_p (def_stmt)
1893 : 148513 : || gimple_assign_unary_nop_p (def_stmt))
1894 : : {
1895 : 94434 : tree rhs = gimple_assign_rhs1 (def_stmt);
1896 : 94434 : return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
1897 : : }
1898 : 54079 : else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1899 : : {
1900 : 246 : tree ops[2] = { gimple_assign_rhs2 (def_stmt),
1901 : 246 : gimple_assign_rhs3 (def_stmt) };
1902 : :
1903 : 738 : for (unsigned int i = 0; i < 2; i++)
1904 : 492 : if (!get_range_strlen (ops[i], visited, rkind, pdata, eltsize))
1905 : : {
1906 : 28 : if (rkind != SRK_LENRANGE)
1907 : : return false;
1908 : : /* Set the upper bound to the maximum to prevent
1909 : : it from being adjusted in the next iteration but
1910 : : leave MINLEN and the more conservative MAXBOUND
1911 : : determined so far alone (or leave them null if
1912 : : they haven't been set yet). That the MINLEN is
1913 : : in fact zero can be determined from MAXLEN being
1914 : : unbounded but the discovered minimum is used for
1915 : : diagnostics. */
1916 : 28 : pdata->maxlen = build_all_ones_cst (size_type_node);
1917 : : }
1918 : : return true;
1919 : : }
1920 : : return false;
1921 : :
1922 : : case GIMPLE_PHI:
1923 : : /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1924 : : must have a constant length. */
1925 : 76306 : for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
1926 : : {
1927 : 53199 : tree arg = gimple_phi_arg (def_stmt, i)->def;
1928 : :
1929 : : /* If this PHI has itself as an argument, we cannot
1930 : : determine the string length of this argument. However,
1931 : : if we can find a constant string length for the other
1932 : : PHI args then we can still be sure that this is a
1933 : : constant string length. So be optimistic and just
1934 : : continue with the next argument. */
1935 : 53199 : if (arg == gimple_phi_result (def_stmt))
1936 : 0 : continue;
1937 : :
1938 : 53199 : if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
1939 : : {
1940 : 28131 : if (rkind != SRK_LENRANGE)
1941 : : return false;
1942 : : /* Set the upper bound to the maximum to prevent
1943 : : it from being adjusted in the next iteration but
1944 : : leave MINLEN and the more conservative MAXBOUND
1945 : : determined so far alone (or leave them null if
1946 : : they haven't been set yet). That the MINLEN is
1947 : : in fact zero can be determined from MAXLEN being
1948 : : unbounded but the discovered minimum is used for
1949 : : diagnostics. */
1950 : 26166 : pdata->maxlen = build_all_ones_cst (size_type_node);
1951 : : }
1952 : : }
1953 : : return true;
1954 : :
1955 : : default:
1956 : : return false;
1957 : : }
1958 : : }
1959 : :
1960 : : /* Try to obtain the range of the lengths of the string(s) referenced
1961 : : by ARG, or the size of the largest array ARG refers to if the range
1962 : : of lengths cannot be determined, and store all in *PDATA which must
1963 : : be zero-initialized on input except PDATA->MAXBOUND may be set to
1964 : : a non-null tree node other than INTEGER_CST to request to have it
1965 : : set to the length of the longest string in a PHI. ELTSIZE is
1966 : : the expected size of the string element in bytes: 1 for char and
1967 : : some power of 2 for wide characters.
1968 : : Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
1969 : : for optimization. Returning false means that a nonzero PDATA->MINLEN
1970 : : doesn't reflect the true lower bound of the range when PDATA->MAXLEN
1971 : : is -1 (in that case, the actual range is indeterminate, i.e.,
1972 : : [0, PTRDIFF_MAX - 2]. */
1973 : :
1974 : : bool
1975 : 1231407 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
1976 : : {
1977 : 1231407 : auto_bitmap visited;
1978 : 1231407 : tree maxbound = pdata->maxbound;
1979 : :
1980 : 1231407 : if (!get_range_strlen (arg, visited, SRK_LENRANGE, pdata, eltsize))
1981 : : {
1982 : : /* On failure extend the length range to an impossible maximum
1983 : : (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1984 : : members can stay unchanged regardless. */
1985 : 998985 : pdata->minlen = ssize_int (0);
1986 : 998985 : pdata->maxlen = build_all_ones_cst (size_type_node);
1987 : : }
1988 : 232422 : else if (!pdata->minlen)
1989 : 8557 : pdata->minlen = ssize_int (0);
1990 : :
1991 : : /* If it's unchanged from it initial non-null value, set the conservative
1992 : : MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
1993 : 1231407 : if (maxbound && pdata->maxbound == maxbound)
1994 : 650545 : pdata->maxbound = build_all_ones_cst (size_type_node);
1995 : :
1996 : 1231407 : return !integer_all_onesp (pdata->maxlen);
1997 : 1231407 : }
1998 : :
1999 : : /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
2000 : : For ARG of pointer types, NONSTR indicates if the caller is prepared
2001 : : to handle unterminated strings. For integer ARG and when RKIND ==
2002 : : SRK_INT_VALUE, NONSTR must be null.
2003 : :
2004 : : If an unterminated array is discovered and our caller handles
2005 : : unterminated arrays, then bubble up the offending DECL and
2006 : : return the maximum size. Otherwise return NULL. */
2007 : :
2008 : : static tree
2009 : 101559 : get_maxval_strlen (tree arg, strlen_range_kind rkind, tree *nonstr = NULL)
2010 : : {
2011 : : /* A non-null NONSTR is meaningless when determining the maximum
2012 : : value of an integer ARG. */
2013 : 101559 : gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
2014 : : /* ARG must have an integral type when RKIND says so. */
2015 : 101559 : gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
2016 : :
2017 : 101559 : auto_bitmap visited;
2018 : :
2019 : : /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
2020 : : is unbounded. */
2021 : 101559 : c_strlen_data lendata = { };
2022 : 101559 : if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
2023 : 54526 : lendata.maxlen = NULL_TREE;
2024 : 47033 : else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
2025 : 0 : lendata.maxlen = NULL_TREE;
2026 : :
2027 : 101559 : if (nonstr)
2028 : : {
2029 : : /* For callers prepared to handle unterminated arrays set
2030 : : *NONSTR to point to the declaration of the array and return
2031 : : the maximum length/size. */
2032 : 25430 : *nonstr = lendata.decl;
2033 : 25430 : return lendata.maxlen;
2034 : : }
2035 : :
2036 : : /* Fail if the constant array isn't nul-terminated. */
2037 : 76129 : return lendata.decl ? NULL_TREE : lendata.maxlen;
2038 : 101559 : }
2039 : :
2040 : : /* Return true if LEN is known to be less than or equal to (or if STRICT is
2041 : : true, strictly less than) the lower bound of SIZE at compile time and false
2042 : : otherwise. */
2043 : :
2044 : : static bool
2045 : 66522 : known_lower (gimple *stmt, tree len, tree size, bool strict = false)
2046 : : {
2047 : 66522 : if (len == NULL_TREE)
2048 : : return false;
2049 : :
2050 : 243780 : wide_int size_range[2];
2051 : 243780 : wide_int len_range[2];
2052 : 48756 : if (get_range (len, stmt, len_range) && get_range (size, stmt, size_range))
2053 : : {
2054 : 17631 : if (strict)
2055 : 2053 : return wi::ltu_p (len_range[1], size_range[0]);
2056 : : else
2057 : 15578 : return wi::leu_p (len_range[1], size_range[0]);
2058 : : }
2059 : :
2060 : : return false;
2061 : 292536 : }
2062 : :
2063 : : /* Fold function call to builtin strcpy with arguments DEST and SRC.
2064 : : If LEN is not NULL, it represents the length of the string to be
2065 : : copied. Return NULL_TREE if no simplification can be made. */
2066 : :
2067 : : static bool
2068 : 27758 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
2069 : : tree dest, tree src)
2070 : : {
2071 : 27758 : gimple *stmt = gsi_stmt (*gsi);
2072 : 27758 : location_t loc = gimple_location (stmt);
2073 : 27758 : tree fn;
2074 : :
2075 : : /* If SRC and DEST are the same (and not volatile), return DEST. */
2076 : 27758 : if (operand_equal_p (src, dest, 0))
2077 : : {
2078 : : /* Issue -Wrestrict unless the pointers are null (those do
2079 : : not point to objects and so do not indicate an overlap;
2080 : : such calls could be the result of sanitization and jump
2081 : : threading). */
2082 : 86 : if (!integer_zerop (dest) && !warning_suppressed_p (stmt, OPT_Wrestrict))
2083 : : {
2084 : 51 : tree func = gimple_call_fndecl (stmt);
2085 : :
2086 : 51 : warning_at (loc, OPT_Wrestrict,
2087 : : "%qD source argument is the same as destination",
2088 : : func);
2089 : : }
2090 : :
2091 : 86 : replace_call_with_value (gsi, dest);
2092 : 86 : return true;
2093 : : }
2094 : :
2095 : 27672 : if (optimize_function_for_size_p (cfun))
2096 : : return false;
2097 : :
2098 : 25430 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2099 : 25430 : if (!fn)
2100 : : return false;
2101 : :
2102 : : /* Set to non-null if ARG refers to an unterminated array. */
2103 : 25430 : tree nonstr = NULL;
2104 : 25430 : tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
2105 : :
2106 : 25430 : if (nonstr)
2107 : : {
2108 : : /* Avoid folding calls with unterminated arrays. */
2109 : 596 : if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
2110 : 69 : warn_string_no_nul (loc, stmt, "strcpy", src, nonstr);
2111 : 596 : suppress_warning (stmt, OPT_Wstringop_overread);
2112 : 596 : return false;
2113 : : }
2114 : :
2115 : 30488 : if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2116 : : return false;
2117 : :
2118 : 2816 : len = fold_convert_loc (loc, size_type_node, len);
2119 : 2816 : len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
2120 : 2816 : len = force_gimple_operand_gsi (gsi, len, true,
2121 : : NULL_TREE, true, GSI_SAME_STMT);
2122 : 2816 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2123 : 2816 : replace_call_with_call_and_fold (gsi, repl);
2124 : 2816 : return true;
2125 : : }
2126 : :
2127 : : /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
2128 : : If SLEN is not NULL, it represents the length of the source string.
2129 : : Return NULL_TREE if no simplification can be made. */
2130 : :
2131 : : static bool
2132 : 19137 : gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
2133 : : tree dest, tree src, tree len)
2134 : : {
2135 : 19137 : gimple *stmt = gsi_stmt (*gsi);
2136 : 19137 : location_t loc = gimple_location (stmt);
2137 : 19137 : bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
2138 : :
2139 : : /* If the LEN parameter is zero, return DEST. */
2140 : 19137 : if (integer_zerop (len))
2141 : : {
2142 : : /* Avoid warning if the destination refers to an array/pointer
2143 : : decorate with attribute nonstring. */
2144 : 167 : if (!nonstring)
2145 : : {
2146 : 155 : tree fndecl = gimple_call_fndecl (stmt);
2147 : :
2148 : : /* Warn about the lack of nul termination: the result is not
2149 : : a (nul-terminated) string. */
2150 : 155 : tree slen = get_maxval_strlen (src, SRK_STRLEN);
2151 : 155 : if (slen && !integer_zerop (slen))
2152 : 24 : warning_at (loc, OPT_Wstringop_truncation,
2153 : : "%qD destination unchanged after copying no bytes "
2154 : : "from a string of length %E",
2155 : : fndecl, slen);
2156 : : else
2157 : 131 : warning_at (loc, OPT_Wstringop_truncation,
2158 : : "%qD destination unchanged after copying no bytes",
2159 : : fndecl);
2160 : : }
2161 : :
2162 : 167 : replace_call_with_value (gsi, dest);
2163 : 167 : return true;
2164 : : }
2165 : :
2166 : : /* We can't compare slen with len as constants below if len is not a
2167 : : constant. */
2168 : 18970 : if (TREE_CODE (len) != INTEGER_CST)
2169 : : return false;
2170 : :
2171 : : /* Now, we must be passed a constant src ptr parameter. */
2172 : 11901 : tree slen = get_maxval_strlen (src, SRK_STRLEN);
2173 : 11901 : if (!slen || TREE_CODE (slen) != INTEGER_CST)
2174 : : return false;
2175 : :
2176 : : /* The size of the source string including the terminating nul. */
2177 : 1917 : tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
2178 : :
2179 : : /* We do not support simplification of this case, though we do
2180 : : support it when expanding trees into RTL. */
2181 : : /* FIXME: generate a call to __builtin_memset. */
2182 : 1917 : if (tree_int_cst_lt (ssize, len))
2183 : : return false;
2184 : :
2185 : : /* Diagnose truncation that leaves the copy unterminated. */
2186 : 695 : maybe_diag_stxncpy_trunc (*gsi, src, len);
2187 : :
2188 : : /* OK transform into builtin memcpy. */
2189 : 695 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2190 : 19665 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2191 : : return false;
2192 : :
2193 : 695 : len = fold_convert_loc (loc, size_type_node, len);
2194 : 695 : len = force_gimple_operand_gsi (gsi, len, true,
2195 : : NULL_TREE, true, GSI_SAME_STMT);
2196 : 695 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2197 : 695 : replace_call_with_call_and_fold (gsi, repl);
2198 : :
2199 : 695 : return true;
2200 : : }
2201 : :
2202 : : /* Fold function call to builtin strchr or strrchr.
2203 : : If both arguments are constant, evaluate and fold the result,
2204 : : otherwise simplify str(r)chr (str, 0) into str + strlen (str).
2205 : : In general strlen is significantly faster than strchr
2206 : : due to being a simpler operation. */
2207 : : static bool
2208 : 6450 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
2209 : : {
2210 : 6450 : gimple *stmt = gsi_stmt (*gsi);
2211 : 6450 : tree str = gimple_call_arg (stmt, 0);
2212 : 6450 : tree c = gimple_call_arg (stmt, 1);
2213 : 6450 : location_t loc = gimple_location (stmt);
2214 : 6450 : const char *p;
2215 : 6450 : char ch;
2216 : :
2217 : 6450 : if (!gimple_call_lhs (stmt))
2218 : : return false;
2219 : :
2220 : : /* Avoid folding if the first argument is not a nul-terminated array.
2221 : : Defer warning until later. */
2222 : 6440 : if (!check_nul_terminated_array (NULL_TREE, str))
2223 : : return false;
2224 : :
2225 : 6346 : if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
2226 : : {
2227 : 41 : const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
2228 : :
2229 : 41 : if (p1 == NULL)
2230 : : {
2231 : 1 : replace_call_with_value (gsi, integer_zero_node);
2232 : 1 : return true;
2233 : : }
2234 : :
2235 : 40 : tree len = build_int_cst (size_type_node, p1 - p);
2236 : 40 : gimple_seq stmts = NULL;
2237 : 40 : gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
2238 : : POINTER_PLUS_EXPR, str, len);
2239 : 40 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2240 : 40 : gsi_replace_with_seq_vops (gsi, stmts);
2241 : 40 : return true;
2242 : : }
2243 : :
2244 : 6387 : if (!integer_zerop (c) || (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun)))
2245 : : return false;
2246 : :
2247 : : /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
2248 : 82 : if (is_strrchr && optimize_function_for_size_p (cfun))
2249 : : {
2250 : 3 : tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
2251 : :
2252 : 3 : if (strchr_fn)
2253 : : {
2254 : 3 : gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
2255 : 3 : replace_call_with_call_and_fold (gsi, repl);
2256 : 3 : return true;
2257 : : }
2258 : :
2259 : : return false;
2260 : : }
2261 : :
2262 : 79 : tree len;
2263 : 6406 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2264 : :
2265 : 79 : if (!strlen_fn)
2266 : : return false;
2267 : :
2268 : : /* Create newstr = strlen (str). */
2269 : 79 : gimple_seq stmts = NULL;
2270 : 79 : gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
2271 : 79 : gimple_set_location (new_stmt, loc);
2272 : 79 : len = make_ssa_name (size_type_node);
2273 : 79 : gimple_call_set_lhs (new_stmt, len);
2274 : 79 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2275 : :
2276 : : /* Create (str p+ strlen (str)). */
2277 : 79 : new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
2278 : : POINTER_PLUS_EXPR, str, len);
2279 : 79 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2280 : 79 : gsi_replace_with_seq_vops (gsi, stmts);
2281 : : /* gsi now points at the assignment to the lhs, get a
2282 : : stmt iterator to the strlen.
2283 : : ??? We can't use gsi_for_stmt as that doesn't work when the
2284 : : CFG isn't built yet. */
2285 : 79 : gimple_stmt_iterator gsi2 = *gsi;
2286 : 79 : gsi_prev (&gsi2);
2287 : 79 : fold_stmt (&gsi2);
2288 : 79 : return true;
2289 : : }
2290 : :
2291 : : /* Fold function call to builtin strstr.
2292 : : If both arguments are constant, evaluate and fold the result,
2293 : : additionally fold strstr (x, "") into x and strstr (x, "c")
2294 : : into strchr (x, 'c'). */
2295 : : static bool
2296 : 4631 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
2297 : : {
2298 : 4631 : gimple *stmt = gsi_stmt (*gsi);
2299 : 4631 : if (!gimple_call_lhs (stmt))
2300 : : return false;
2301 : :
2302 : 4628 : tree haystack = gimple_call_arg (stmt, 0);
2303 : 4628 : tree needle = gimple_call_arg (stmt, 1);
2304 : :
2305 : : /* Avoid folding if either argument is not a nul-terminated array.
2306 : : Defer warning until later. */
2307 : 4628 : if (!check_nul_terminated_array (NULL_TREE, haystack)
2308 : 4628 : || !check_nul_terminated_array (NULL_TREE, needle))
2309 : 21 : return false;
2310 : :
2311 : 4607 : const char *q = c_getstr (needle);
2312 : 4607 : if (q == NULL)
2313 : : return false;
2314 : :
2315 : 3434 : if (const char *p = c_getstr (haystack))
2316 : : {
2317 : 14 : const char *r = strstr (p, q);
2318 : :
2319 : 14 : if (r == NULL)
2320 : : {
2321 : 1 : replace_call_with_value (gsi, integer_zero_node);
2322 : 1 : return true;
2323 : : }
2324 : :
2325 : 13 : tree len = build_int_cst (size_type_node, r - p);
2326 : 13 : gimple_seq stmts = NULL;
2327 : 13 : gimple *new_stmt
2328 : 13 : = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
2329 : : haystack, len);
2330 : 13 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2331 : 13 : gsi_replace_with_seq_vops (gsi, stmts);
2332 : 13 : return true;
2333 : : }
2334 : :
2335 : : /* For strstr (x, "") return x. */
2336 : 3420 : if (q[0] == '\0')
2337 : : {
2338 : 6 : replace_call_with_value (gsi, haystack);
2339 : 6 : return true;
2340 : : }
2341 : :
2342 : 11417 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2343 : : return false;
2344 : :
2345 : : /* Transform strstr (x, "c") into strchr (x, 'c'). */
2346 : 3414 : if (q[1] == '\0')
2347 : : {
2348 : 22 : tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
2349 : 22 : if (strchr_fn)
2350 : : {
2351 : 22 : tree c = build_int_cst (integer_type_node, q[0]);
2352 : 22 : gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
2353 : 22 : replace_call_with_call_and_fold (gsi, repl);
2354 : 22 : return true;
2355 : : }
2356 : : }
2357 : :
2358 : : return false;
2359 : : }
2360 : :
2361 : : /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2362 : : to the call.
2363 : :
2364 : : Return NULL_TREE if no simplification was possible, otherwise return the
2365 : : simplified form of the call as a tree.
2366 : :
2367 : : The simplified form may be a constant or other expression which
2368 : : computes the same value, but in a more efficient manner (including
2369 : : calls to other builtin functions).
2370 : :
2371 : : The call may contain arguments which need to be evaluated, but
2372 : : which are not useful to determine the result of the call. In
2373 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
2374 : : COMPOUND_EXPR will be an argument which must be evaluated.
2375 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2376 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
2377 : : form of the builtin function call. */
2378 : :
2379 : : static bool
2380 : 8040 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
2381 : : {
2382 : 8040 : gimple *stmt = gsi_stmt (*gsi);
2383 : 8040 : location_t loc = gimple_location (stmt);
2384 : :
2385 : 8040 : const char *p = c_getstr (src);
2386 : :
2387 : : /* If the string length is zero, return the dst parameter. */
2388 : 8040 : if (p && *p == '\0')
2389 : : {
2390 : 72 : replace_call_with_value (gsi, dst);
2391 : 72 : return true;
2392 : : }
2393 : :
2394 : 7968 : if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
2395 : : return false;
2396 : :
2397 : 21800 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2398 : : return false;
2399 : :
2400 : : /* See if we can store by pieces into (dst + strlen(dst)). */
2401 : 7323 : tree newdst;
2402 : 7323 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2403 : 7323 : tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2404 : :
2405 : 7323 : if (!strlen_fn || !memcpy_fn)
2406 : : return false;
2407 : :
2408 : : /* If the length of the source string isn't computable don't
2409 : : split strcat into strlen and memcpy. */
2410 : 7323 : tree len = get_maxval_strlen (src, SRK_STRLEN);
2411 : 7323 : if (! len)
2412 : : return false;
2413 : :
2414 : : /* Create strlen (dst). */
2415 : 814 : gimple_seq stmts = NULL, stmts2;
2416 : 814 : gimple *repl = gimple_build_call (strlen_fn, 1, dst);
2417 : 814 : gimple_set_location (repl, loc);
2418 : 814 : newdst = make_ssa_name (size_type_node);
2419 : 814 : gimple_call_set_lhs (repl, newdst);
2420 : 814 : gimple_seq_add_stmt_without_update (&stmts, repl);
2421 : :
2422 : : /* Create (dst p+ strlen (dst)). */
2423 : 814 : newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
2424 : 814 : newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
2425 : 814 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2426 : :
2427 : 814 : len = fold_convert_loc (loc, size_type_node, len);
2428 : 814 : len = size_binop_loc (loc, PLUS_EXPR, len,
2429 : : build_int_cst (size_type_node, 1));
2430 : 814 : len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
2431 : 814 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2432 : :
2433 : 814 : repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
2434 : 814 : gimple_seq_add_stmt_without_update (&stmts, repl);
2435 : 814 : if (gimple_call_lhs (stmt))
2436 : : {
2437 : 165 : repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
2438 : 165 : gimple_seq_add_stmt_without_update (&stmts, repl);
2439 : 165 : gsi_replace_with_seq_vops (gsi, stmts);
2440 : : /* gsi now points at the assignment to the lhs, get a
2441 : : stmt iterator to the memcpy call.
2442 : : ??? We can't use gsi_for_stmt as that doesn't work when the
2443 : : CFG isn't built yet. */
2444 : 165 : gimple_stmt_iterator gsi2 = *gsi;
2445 : 165 : gsi_prev (&gsi2);
2446 : 165 : fold_stmt (&gsi2);
2447 : : }
2448 : : else
2449 : : {
2450 : 649 : gsi_replace_with_seq_vops (gsi, stmts);
2451 : 649 : fold_stmt (gsi);
2452 : : }
2453 : : return true;
2454 : : }
2455 : :
2456 : : /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2457 : : are the arguments to the call. */
2458 : :
2459 : : static bool
2460 : 1854 : gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
2461 : : {
2462 : 1854 : gimple *stmt = gsi_stmt (*gsi);
2463 : 1854 : tree dest = gimple_call_arg (stmt, 0);
2464 : 1854 : tree src = gimple_call_arg (stmt, 1);
2465 : 1854 : tree size = gimple_call_arg (stmt, 2);
2466 : 1854 : tree fn;
2467 : 1854 : const char *p;
2468 : :
2469 : 1854 : p = c_getstr (src);
2470 : : /* If the SRC parameter is "", return DEST. */
2471 : 1854 : if (p && *p == '\0')
2472 : : {
2473 : 60 : replace_call_with_value (gsi, dest);
2474 : 60 : return true;
2475 : : }
2476 : :
2477 : 1794 : if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
2478 : 1712 : return false;
2479 : :
2480 : 1876 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2481 : : return false;
2482 : :
2483 : : /* If __builtin_strcat_chk is used, assume strcat is available. */
2484 : 82 : fn = builtin_decl_explicit (BUILT_IN_STRCAT);
2485 : 82 : if (!fn)
2486 : : return false;
2487 : :
2488 : 82 : gimple *repl = gimple_build_call (fn, 2, dest, src);
2489 : 82 : replace_call_with_call_and_fold (gsi, repl);
2490 : 82 : return true;
2491 : : }
2492 : :
2493 : : /* Simplify a call to the strncat builtin. */
2494 : :
2495 : : static bool
2496 : 7557 : gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
2497 : : {
2498 : 7557 : gimple *stmt = gsi_stmt (*gsi);
2499 : 7557 : tree dst = gimple_call_arg (stmt, 0);
2500 : 7557 : tree src = gimple_call_arg (stmt, 1);
2501 : 7557 : tree len = gimple_call_arg (stmt, 2);
2502 : 7557 : tree src_len = c_strlen (src, 1);
2503 : :
2504 : : /* If the requested length is zero, or the src parameter string
2505 : : length is zero, return the dst parameter. */
2506 : 7557 : if (integer_zerop (len) || (src_len && integer_zerop (src_len)))
2507 : : {
2508 : 119 : replace_call_with_value (gsi, dst);
2509 : 119 : return true;
2510 : : }
2511 : :
2512 : : /* Return early if the requested len is less than the string length.
2513 : : Warnings will be issued elsewhere later. */
2514 : 7438 : if (!src_len || known_lower (stmt, len, src_len, true))
2515 : 6833 : return false;
2516 : :
2517 : : /* Warn on constant LEN. */
2518 : 605 : if (TREE_CODE (len) == INTEGER_CST)
2519 : : {
2520 : 131 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstringop_overflow_);
2521 : 131 : tree dstsize;
2522 : :
2523 : 131 : if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize)
2524 : 175 : && TREE_CODE (dstsize) == INTEGER_CST)
2525 : : {
2526 : 44 : int cmpdst = tree_int_cst_compare (len, dstsize);
2527 : :
2528 : 44 : if (cmpdst >= 0)
2529 : : {
2530 : 19 : tree fndecl = gimple_call_fndecl (stmt);
2531 : :
2532 : : /* Strncat copies (at most) LEN bytes and always appends
2533 : : the terminating NUL so the specified bound should never
2534 : : be equal to (or greater than) the size of the destination.
2535 : : If it is, the copy could overflow. */
2536 : 19 : location_t loc = gimple_location (stmt);
2537 : 37 : nowarn = warning_at (loc, OPT_Wstringop_overflow_,
2538 : : cmpdst == 0
2539 : : ? G_("%qD specified bound %E equals "
2540 : : "destination size")
2541 : : : G_("%qD specified bound %E exceeds "
2542 : : "destination size %E"),
2543 : : fndecl, len, dstsize);
2544 : 19 : if (nowarn)
2545 : 0 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2546 : : }
2547 : : }
2548 : :
2549 : 131 : if (!nowarn && TREE_CODE (src_len) == INTEGER_CST
2550 : 243 : && tree_int_cst_compare (src_len, len) == 0)
2551 : : {
2552 : 20 : tree fndecl = gimple_call_fndecl (stmt);
2553 : 20 : location_t loc = gimple_location (stmt);
2554 : :
2555 : : /* To avoid possible overflow the specified bound should also
2556 : : not be equal to the length of the source, even when the size
2557 : : of the destination is unknown (it's not an uncommon mistake
2558 : : to specify as the bound to strncpy the length of the source). */
2559 : 20 : if (warning_at (loc, OPT_Wstringop_overflow_,
2560 : : "%qD specified bound %E equals source length",
2561 : : fndecl, len))
2562 : 6 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2563 : : }
2564 : : }
2565 : :
2566 : 605 : if (!known_lower (stmt, src_len, len))
2567 : : return false;
2568 : :
2569 : 136 : tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
2570 : :
2571 : : /* If the replacement _DECL isn't initialized, don't do the
2572 : : transformation. */
2573 : 7574 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2574 : : return false;
2575 : :
2576 : : /* Otherwise, emit a call to strcat. */
2577 : 136 : gcall *repl = gimple_build_call (fn, 2, dst, src);
2578 : 136 : replace_call_with_call_and_fold (gsi, repl);
2579 : 136 : return true;
2580 : : }
2581 : :
2582 : : /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2583 : : LEN, and SIZE. */
2584 : :
2585 : : static bool
2586 : 1225 : gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
2587 : : {
2588 : 1225 : gimple *stmt = gsi_stmt (*gsi);
2589 : 1225 : tree dest = gimple_call_arg (stmt, 0);
2590 : 1225 : tree src = gimple_call_arg (stmt, 1);
2591 : 1225 : tree len = gimple_call_arg (stmt, 2);
2592 : 1225 : tree size = gimple_call_arg (stmt, 3);
2593 : 1225 : tree fn;
2594 : 1225 : const char *p;
2595 : :
2596 : 1225 : p = c_getstr (src);
2597 : : /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2598 : 312 : if ((p && *p == '\0')
2599 : 1486 : || integer_zerop (len))
2600 : : {
2601 : 78 : replace_call_with_value (gsi, dest);
2602 : 78 : return true;
2603 : : }
2604 : :
2605 : 3289 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2606 : : return false;
2607 : :
2608 : 1147 : if (! integer_all_onesp (size))
2609 : : {
2610 : 1060 : tree src_len = c_strlen (src, 1);
2611 : 1060 : if (known_lower (stmt, src_len, len))
2612 : : {
2613 : : /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2614 : 65 : fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
2615 : 65 : if (!fn)
2616 : : return false;
2617 : :
2618 : 65 : gimple *repl = gimple_build_call (fn, 3, dest, src, size);
2619 : 65 : replace_call_with_call_and_fold (gsi, repl);
2620 : 65 : return true;
2621 : : }
2622 : : return false;
2623 : : }
2624 : :
2625 : : /* If __builtin_strncat_chk is used, assume strncat is available. */
2626 : 87 : fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
2627 : 87 : if (!fn)
2628 : : return false;
2629 : :
2630 : 87 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2631 : 87 : replace_call_with_call_and_fold (gsi, repl);
2632 : 87 : return true;
2633 : : }
2634 : :
2635 : : /* Build and append gimple statements to STMTS that would load a first
2636 : : character of a memory location identified by STR. LOC is location
2637 : : of the statement. */
2638 : :
2639 : : static tree
2640 : 461 : gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
2641 : : {
2642 : 461 : tree var;
2643 : :
2644 : 461 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2645 : 461 : tree cst_uchar_ptr_node
2646 : 461 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
2647 : 461 : tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
2648 : :
2649 : 461 : tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
2650 : 461 : gassign *stmt = gimple_build_assign (NULL_TREE, temp);
2651 : 461 : var = make_ssa_name (cst_uchar_node, stmt);
2652 : :
2653 : 461 : gimple_assign_set_lhs (stmt, var);
2654 : 461 : gimple_seq_add_stmt_without_update (stmts, stmt);
2655 : :
2656 : 461 : return var;
2657 : : }
2658 : :
2659 : : /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. */
2660 : :
2661 : : static bool
2662 : 1377599 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
2663 : : {
2664 : 1377599 : gimple *stmt = gsi_stmt (*gsi);
2665 : 1377599 : tree callee = gimple_call_fndecl (stmt);
2666 : 1377599 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
2667 : :
2668 : 1377599 : tree type = integer_type_node;
2669 : 1377599 : tree str1 = gimple_call_arg (stmt, 0);
2670 : 1377599 : tree str2 = gimple_call_arg (stmt, 1);
2671 : 1377599 : tree lhs = gimple_call_lhs (stmt);
2672 : :
2673 : 1377599 : tree bound_node = NULL_TREE;
2674 : 1377599 : unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
2675 : :
2676 : : /* Handle strncmp and strncasecmp functions. */
2677 : 1377599 : if (gimple_call_num_args (stmt) == 3)
2678 : : {
2679 : 25569 : bound_node = gimple_call_arg (stmt, 2);
2680 : 25569 : if (tree_fits_uhwi_p (bound_node))
2681 : 19320 : bound = tree_to_uhwi (bound_node);
2682 : : }
2683 : :
2684 : : /* If the BOUND parameter is zero, return zero. */
2685 : 19320 : if (bound == 0)
2686 : : {
2687 : 4 : replace_call_with_value (gsi, integer_zero_node);
2688 : 4 : return true;
2689 : : }
2690 : :
2691 : : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2692 : 1377595 : if (operand_equal_p (str1, str2, 0))
2693 : : {
2694 : 41 : replace_call_with_value (gsi, integer_zero_node);
2695 : 41 : return true;
2696 : : }
2697 : :
2698 : 2755108 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2699 : : return false;
2700 : :
2701 : : /* Initially set to the number of characters, including the terminating
2702 : : nul if each array has one. LENx == strnlen (Sx, LENx) implies that
2703 : : the array Sx is not terminated by a nul.
2704 : : For nul-terminated strings then adjusted to their length so that
2705 : : LENx == NULPOSx holds. */
2706 : 1377554 : unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
2707 : 1377554 : const char *p1 = getbyterep (str1, &len1);
2708 : 1377554 : const char *p2 = getbyterep (str2, &len2);
2709 : :
2710 : : /* The position of the terminating nul character if one exists, otherwise
2711 : : a value greater than LENx. */
2712 : 1377554 : unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
2713 : :
2714 : 1377554 : if (p1)
2715 : : {
2716 : 43321 : size_t n = strnlen (p1, len1);
2717 : 43321 : if (n < len1)
2718 : 43206 : len1 = nulpos1 = n;
2719 : : }
2720 : :
2721 : 1377554 : if (p2)
2722 : : {
2723 : 1345697 : size_t n = strnlen (p2, len2);
2724 : 1345697 : if (n < len2)
2725 : 1345633 : len2 = nulpos2 = n;
2726 : : }
2727 : :
2728 : : /* For known strings, return an immediate value. */
2729 : 1377554 : if (p1 && p2)
2730 : : {
2731 : 39371 : int r = 0;
2732 : 39371 : bool known_result = false;
2733 : :
2734 : 39371 : switch (fcode)
2735 : : {
2736 : 38172 : case BUILT_IN_STRCMP:
2737 : 38172 : case BUILT_IN_STRCMP_EQ:
2738 : 38172 : if (len1 != nulpos1 || len2 != nulpos2)
2739 : : break;
2740 : :
2741 : 38144 : r = strcmp (p1, p2);
2742 : 38144 : known_result = true;
2743 : 38144 : break;
2744 : :
2745 : 1118 : case BUILT_IN_STRNCMP:
2746 : 1118 : case BUILT_IN_STRNCMP_EQ:
2747 : 1118 : {
2748 : 1118 : if (bound == HOST_WIDE_INT_M1U)
2749 : : break;
2750 : :
2751 : : /* Reduce the bound to be no more than the length
2752 : : of the shorter of the two strings, or the sizes
2753 : : of the unterminated arrays. */
2754 : 38 : unsigned HOST_WIDE_INT n = bound;
2755 : :
2756 : 38 : if (len1 == nulpos1 && len1 < n)
2757 : 4 : n = len1 + 1;
2758 : 38 : if (len2 == nulpos2 && len2 < n)
2759 : 11 : n = len2 + 1;
2760 : :
2761 : 38 : if (MIN (nulpos1, nulpos2) + 1 < n)
2762 : : break;
2763 : :
2764 : 38 : r = strncmp (p1, p2, n);
2765 : 38 : known_result = true;
2766 : 38 : break;
2767 : : }
2768 : : /* Only handleable situation is where the string are equal (result 0),
2769 : : which is already handled by operand_equal_p case. */
2770 : : case BUILT_IN_STRCASECMP:
2771 : : break;
2772 : 41 : case BUILT_IN_STRNCASECMP:
2773 : 41 : {
2774 : 41 : if (bound == HOST_WIDE_INT_M1U)
2775 : : break;
2776 : 41 : r = strncmp (p1, p2, bound);
2777 : 41 : if (r == 0)
2778 : : known_result = true;
2779 : : break;
2780 : : }
2781 : 0 : default:
2782 : 0 : gcc_unreachable ();
2783 : : }
2784 : :
2785 : 38182 : if (known_result)
2786 : : {
2787 : 38182 : replace_call_with_value (gsi, build_cmp_result (type, r));
2788 : 38182 : return true;
2789 : : }
2790 : : }
2791 : :
2792 : 2678744 : bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
2793 : 1320207 : || fcode == BUILT_IN_STRCMP
2794 : 1320207 : || fcode == BUILT_IN_STRCMP_EQ
2795 : 1345813 : || fcode == BUILT_IN_STRCASECMP;
2796 : :
2797 : 1339372 : location_t loc = gimple_location (stmt);
2798 : :
2799 : : /* If the second arg is "", return *(const unsigned char*)arg1. */
2800 : 1339372 : if (p2 && *p2 == '\0' && nonzero_bound)
2801 : : {
2802 : 150 : gimple_seq stmts = NULL;
2803 : 150 : tree var = gimple_load_first_char (loc, str1, &stmts);
2804 : 150 : if (lhs)
2805 : : {
2806 : 150 : stmt = gimple_build_assign (lhs, NOP_EXPR, var);
2807 : 150 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2808 : : }
2809 : :
2810 : 150 : gsi_replace_with_seq_vops (gsi, stmts);
2811 : 150 : return true;
2812 : : }
2813 : :
2814 : : /* If the first arg is "", return -*(const unsigned char*)arg2. */
2815 : 1339222 : if (p1 && *p1 == '\0' && nonzero_bound)
2816 : : {
2817 : 91 : gimple_seq stmts = NULL;
2818 : 91 : tree var = gimple_load_first_char (loc, str2, &stmts);
2819 : :
2820 : 91 : if (lhs)
2821 : : {
2822 : 91 : tree c = make_ssa_name (integer_type_node);
2823 : 91 : stmt = gimple_build_assign (c, NOP_EXPR, var);
2824 : 91 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2825 : :
2826 : 91 : stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
2827 : 91 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2828 : : }
2829 : :
2830 : 91 : gsi_replace_with_seq_vops (gsi, stmts);
2831 : 91 : return true;
2832 : : }
2833 : :
2834 : : /* If BOUND is one, return an expression corresponding to
2835 : : (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2836 : 1339131 : if (fcode == BUILT_IN_STRNCMP && bound == 1)
2837 : : {
2838 : 110 : gimple_seq stmts = NULL;
2839 : 110 : tree temp1 = gimple_load_first_char (loc, str1, &stmts);
2840 : 110 : tree temp2 = gimple_load_first_char (loc, str2, &stmts);
2841 : :
2842 : 110 : if (lhs)
2843 : : {
2844 : 107 : tree c1 = make_ssa_name (integer_type_node);
2845 : 107 : gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
2846 : 107 : gimple_seq_add_stmt_without_update (&stmts, convert1);
2847 : :
2848 : 107 : tree c2 = make_ssa_name (integer_type_node);
2849 : 107 : gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
2850 : 107 : gimple_seq_add_stmt_without_update (&stmts, convert2);
2851 : :
2852 : 107 : stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
2853 : 107 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2854 : : }
2855 : :
2856 : 110 : gsi_replace_with_seq_vops (gsi, stmts);
2857 : 110 : return true;
2858 : : }
2859 : :
2860 : : /* If BOUND is greater than the length of one constant string,
2861 : : and the other argument is also a nul-terminated string, replace
2862 : : strncmp with strcmp. */
2863 : 1339021 : if (fcode == BUILT_IN_STRNCMP
2864 : 19568 : && bound > 0 && bound < HOST_WIDE_INT_M1U
2865 : 13453 : && ((p2 && len2 < bound && len2 == nulpos2)
2866 : 13223 : || (p1 && len1 < bound && len1 == nulpos1)))
2867 : : {
2868 : 1339021 : tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
2869 : 308 : if (!fn)
2870 : : return false;
2871 : 308 : gimple *repl = gimple_build_call (fn, 2, str1, str2);
2872 : 308 : replace_call_with_call_and_fold (gsi, repl);
2873 : 308 : return true;
2874 : : }
2875 : :
2876 : : return false;
2877 : : }
2878 : :
2879 : : /* Fold a call to the memchr pointed by GSI iterator. */
2880 : :
2881 : : static bool
2882 : 31265 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2883 : : {
2884 : 31265 : gimple *stmt = gsi_stmt (*gsi);
2885 : 31265 : tree lhs = gimple_call_lhs (stmt);
2886 : 31265 : tree arg1 = gimple_call_arg (stmt, 0);
2887 : 31265 : tree arg2 = gimple_call_arg (stmt, 1);
2888 : 31265 : tree len = gimple_call_arg (stmt, 2);
2889 : :
2890 : : /* If the LEN parameter is zero, return zero. */
2891 : 31265 : if (integer_zerop (len))
2892 : : {
2893 : 1 : replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2894 : 1 : return true;
2895 : : }
2896 : :
2897 : 31264 : char c;
2898 : 31264 : if (TREE_CODE (arg2) != INTEGER_CST
2899 : 18487 : || !tree_fits_uhwi_p (len)
2900 : 32000 : || !target_char_cst_p (arg2, &c))
2901 : 30528 : return false;
2902 : :
2903 : 736 : unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
2904 : 736 : unsigned HOST_WIDE_INT string_length;
2905 : 736 : const char *p1 = getbyterep (arg1, &string_length);
2906 : :
2907 : 736 : if (p1)
2908 : : {
2909 : 100 : const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
2910 : 100 : if (r == NULL)
2911 : : {
2912 : 17 : tree mem_size, offset_node;
2913 : 17 : byte_representation (arg1, &offset_node, &mem_size, NULL);
2914 : 17 : unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE)
2915 : 17 : ? 0 : tree_to_uhwi (offset_node);
2916 : : /* MEM_SIZE is the size of the array the string literal
2917 : : is stored in. */
2918 : 17 : unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset;
2919 : 17 : gcc_checking_assert (string_length <= string_size);
2920 : 17 : if (length <= string_size)
2921 : : {
2922 : 5 : replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2923 : 5 : return true;
2924 : : }
2925 : : }
2926 : : else
2927 : : {
2928 : 83 : unsigned HOST_WIDE_INT offset = r - p1;
2929 : 83 : gimple_seq stmts = NULL;
2930 : 83 : if (lhs != NULL_TREE)
2931 : : {
2932 : 81 : tree offset_cst = build_int_cst (sizetype, offset);
2933 : 81 : gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
2934 : : arg1, offset_cst);
2935 : 81 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2936 : : }
2937 : : else
2938 : 2 : gimple_seq_add_stmt_without_update (&stmts,
2939 : : gimple_build_nop ());
2940 : :
2941 : 83 : gsi_replace_with_seq_vops (gsi, stmts);
2942 : 83 : return true;
2943 : : }
2944 : : }
2945 : :
2946 : : return false;
2947 : : }
2948 : :
2949 : : /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2950 : : to the call. IGNORE is true if the value returned
2951 : : by the builtin will be ignored. UNLOCKED is true is true if this
2952 : : actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2953 : : the known length of the string. Return NULL_TREE if no simplification
2954 : : was possible. */
2955 : :
2956 : : static bool
2957 : 21997 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
2958 : : tree arg0, tree arg1,
2959 : : bool unlocked)
2960 : : {
2961 : 21997 : gimple *stmt = gsi_stmt (*gsi);
2962 : :
2963 : : /* If we're using an unlocked function, assume the other unlocked
2964 : : functions exist explicitly. */
2965 : 21997 : tree const fn_fputc = (unlocked
2966 : 21997 : ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2967 : 21953 : : builtin_decl_implicit (BUILT_IN_FPUTC));
2968 : 21953 : tree const fn_fwrite = (unlocked
2969 : 44 : ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2970 : 21997 : : builtin_decl_implicit (BUILT_IN_FWRITE));
2971 : :
2972 : : /* If the return value is used, don't do the transformation. */
2973 : 21997 : if (gimple_call_lhs (stmt))
2974 : : return false;
2975 : :
2976 : : /* Get the length of the string passed to fputs. If the length
2977 : : can't be determined, punt. */
2978 : 21917 : tree len = get_maxval_strlen (arg0, SRK_STRLEN);
2979 : 21917 : if (!len || TREE_CODE (len) != INTEGER_CST)
2980 : : return false;
2981 : :
2982 : 16935 : switch (compare_tree_int (len, 1))
2983 : : {
2984 : 91 : case -1: /* length is 0, delete the call entirely . */
2985 : 91 : replace_call_with_value (gsi, integer_zero_node);
2986 : 91 : return true;
2987 : :
2988 : 1045 : case 0: /* length is 1, call fputc. */
2989 : 1045 : {
2990 : 1045 : const char *p = c_getstr (arg0);
2991 : 1045 : if (p != NULL)
2992 : : {
2993 : 2060 : if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2994 : : return false;
2995 : :
2996 : 1030 : gimple *repl
2997 : 1030 : = gimple_build_call (fn_fputc, 2,
2998 : 1030 : build_int_cst (integer_type_node, p[0]),
2999 : : arg1);
3000 : 1030 : replace_call_with_call_and_fold (gsi, repl);
3001 : 1030 : return true;
3002 : : }
3003 : : }
3004 : : /* FALLTHROUGH */
3005 : 15814 : case 1: /* length is greater than 1, call fwrite. */
3006 : 15814 : {
3007 : : /* If optimizing for size keep fputs. */
3008 : 15814 : if (optimize_function_for_size_p (cfun))
3009 : : return false;
3010 : : /* New argument list transforming fputs(string, stream) to
3011 : : fwrite(string, 1, len, stream). */
3012 : 29027 : if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3013 : : return false;
3014 : :
3015 : 8151 : gimple *repl
3016 : 8151 : = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
3017 : : fold_convert (size_type_node, len), arg1);
3018 : 8151 : replace_call_with_call_and_fold (gsi, repl);
3019 : 8151 : return true;
3020 : : }
3021 : 0 : default:
3022 : 0 : gcc_unreachable ();
3023 : : }
3024 : : }
3025 : :
3026 : : /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
3027 : : DEST, SRC, LEN, and SIZE are the arguments to the call.
3028 : : IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
3029 : : code of the builtin. If MAXLEN is not NULL, it is maximum length
3030 : : passed as third argument. */
3031 : :
3032 : : static bool
3033 : 26060 : gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
3034 : : tree dest, tree src, tree len, tree size,
3035 : : enum built_in_function fcode)
3036 : : {
3037 : 26060 : gimple *stmt = gsi_stmt (*gsi);
3038 : 26060 : location_t loc = gimple_location (stmt);
3039 : 26060 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3040 : 26060 : tree fn;
3041 : :
3042 : : /* If SRC and DEST are the same (and not volatile), return DEST
3043 : : (resp. DEST+LEN for __mempcpy_chk). */
3044 : 26060 : if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
3045 : : {
3046 : 13 : if (fcode != BUILT_IN_MEMPCPY_CHK)
3047 : : {
3048 : 7 : replace_call_with_value (gsi, dest);
3049 : 7 : return true;
3050 : : }
3051 : : else
3052 : : {
3053 : 6 : gimple_seq stmts = NULL;
3054 : 6 : len = gimple_convert_to_ptrofftype (&stmts, loc, len);
3055 : 6 : tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
3056 : 6 : TREE_TYPE (dest), dest, len);
3057 : 6 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3058 : 6 : replace_call_with_value (gsi, temp);
3059 : 6 : return true;
3060 : : }
3061 : : }
3062 : :
3063 : 69589 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3064 : : return false;
3065 : :
3066 : 26047 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3067 : 26047 : if (! integer_all_onesp (size)
3068 : 24983 : && !known_lower (stmt, len, size)
3069 : 43659 : && !known_lower (stmt, maxlen, size))
3070 : : {
3071 : : /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
3072 : : least try to optimize (void) __mempcpy_chk () into
3073 : : (void) __memcpy_chk () */
3074 : 17535 : if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
3075 : : {
3076 : 40 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3077 : 40 : if (!fn)
3078 : : return false;
3079 : :
3080 : 40 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3081 : 40 : replace_call_with_call_and_fold (gsi, repl);
3082 : 40 : return true;
3083 : : }
3084 : : return false;
3085 : : }
3086 : :
3087 : 8512 : fn = NULL_TREE;
3088 : : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
3089 : : mem{cpy,pcpy,move,set} is available. */
3090 : 8512 : switch (fcode)
3091 : : {
3092 : 1764 : case BUILT_IN_MEMCPY_CHK:
3093 : 1764 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
3094 : 1764 : break;
3095 : 1071 : case BUILT_IN_MEMPCPY_CHK:
3096 : 1071 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
3097 : 1071 : break;
3098 : 1657 : case BUILT_IN_MEMMOVE_CHK:
3099 : 1657 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
3100 : 1657 : break;
3101 : 4020 : case BUILT_IN_MEMSET_CHK:
3102 : 4020 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
3103 : 4020 : break;
3104 : : default:
3105 : : break;
3106 : : }
3107 : :
3108 : 8512 : if (!fn)
3109 : : return false;
3110 : :
3111 : 8512 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
3112 : 8512 : replace_call_with_call_and_fold (gsi, repl);
3113 : 8512 : return true;
3114 : : }
3115 : :
3116 : : /* Fold a call to the __st[rp]cpy_chk builtin.
3117 : : DEST, SRC, and SIZE are the arguments to the call.
3118 : : IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
3119 : : code of the builtin. If MAXLEN is not NULL, it is maximum length of
3120 : : strings passed as second argument. */
3121 : :
3122 : : static bool
3123 : 2797 : gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
3124 : : tree dest,
3125 : : tree src, tree size,
3126 : : enum built_in_function fcode)
3127 : : {
3128 : 2797 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3129 : 2797 : location_t loc = gimple_location (stmt);
3130 : 2797 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3131 : 2797 : tree len, fn;
3132 : :
3133 : : /* If SRC and DEST are the same (and not volatile), return DEST. */
3134 : 2797 : if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
3135 : : {
3136 : : /* Issue -Wrestrict unless the pointers are null (those do
3137 : : not point to objects and so do not indicate an overlap;
3138 : : such calls could be the result of sanitization and jump
3139 : : threading). */
3140 : 0 : if (!integer_zerop (dest)
3141 : 0 : && !warning_suppressed_p (stmt, OPT_Wrestrict))
3142 : : {
3143 : 0 : tree func = gimple_call_fndecl (stmt);
3144 : :
3145 : 0 : warning_at (loc, OPT_Wrestrict,
3146 : : "%qD source argument is the same as destination",
3147 : : func);
3148 : : }
3149 : :
3150 : 0 : replace_call_with_value (gsi, dest);
3151 : 0 : return true;
3152 : : }
3153 : :
3154 : 5594 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3155 : : return false;
3156 : :
3157 : 2797 : tree maxlen = get_maxval_strlen (src, SRK_STRLENMAX);
3158 : 2797 : if (! integer_all_onesp (size))
3159 : : {
3160 : 2728 : len = c_strlen (src, 1);
3161 : 2728 : if (!known_lower (stmt, len, size, true)
3162 : 2728 : && !known_lower (stmt, maxlen, size, true))
3163 : : {
3164 : 2392 : if (fcode == BUILT_IN_STPCPY_CHK)
3165 : : {
3166 : 1193 : if (! ignore)
3167 : : return false;
3168 : :
3169 : : /* If return value of __stpcpy_chk is ignored,
3170 : : optimize into __strcpy_chk. */
3171 : 37 : fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
3172 : 37 : if (!fn)
3173 : : return false;
3174 : :
3175 : 37 : gimple *repl = gimple_build_call (fn, 3, dest, src, size);
3176 : 37 : replace_call_with_call_and_fold (gsi, repl);
3177 : 37 : return true;
3178 : : }
3179 : :
3180 : 1199 : if (! len || TREE_SIDE_EFFECTS (len))
3181 : : return false;
3182 : :
3183 : : /* If c_strlen returned something, but not provably less than size,
3184 : : transform __strcpy_chk into __memcpy_chk. */
3185 : 106 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3186 : 106 : if (!fn)
3187 : : return false;
3188 : :
3189 : 106 : gimple_seq stmts = NULL;
3190 : 106 : len = force_gimple_operand (len, &stmts, true, NULL_TREE);
3191 : 106 : len = gimple_convert (&stmts, loc, size_type_node, len);
3192 : 106 : len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
3193 : : build_int_cst (size_type_node, 1));
3194 : 106 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3195 : 106 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3196 : 106 : replace_call_with_call_and_fold (gsi, repl);
3197 : 106 : return true;
3198 : : }
3199 : : }
3200 : :
3201 : : /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
3202 : 662 : fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore
3203 : : ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
3204 : 405 : if (!fn)
3205 : : return false;
3206 : :
3207 : 405 : gcall *repl = gimple_build_call (fn, 2, dest, src);
3208 : 405 : replace_call_with_call_and_fold (gsi, repl);
3209 : 405 : return true;
3210 : : }
3211 : :
3212 : : /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
3213 : : are the arguments to the call. If MAXLEN is not NULL, it is maximum
3214 : : length passed as third argument. IGNORE is true if return value can be
3215 : : ignored. FCODE is the BUILT_IN_* code of the builtin. */
3216 : :
3217 : : static bool
3218 : 2881 : gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
3219 : : tree dest, tree src,
3220 : : tree len, tree size,
3221 : : enum built_in_function fcode)
3222 : : {
3223 : 2881 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3224 : 2881 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3225 : 2881 : tree fn;
3226 : :
3227 : 2881 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3228 : 2881 : if (! integer_all_onesp (size)
3229 : 2881 : && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
3230 : : {
3231 : 2424 : if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
3232 : : {
3233 : : /* If return value of __stpncpy_chk is ignored,
3234 : : optimize into __strncpy_chk. */
3235 : 39 : fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
3236 : 39 : if (fn)
3237 : : {
3238 : 39 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3239 : 39 : replace_call_with_call_and_fold (gsi, repl);
3240 : 39 : return true;
3241 : : }
3242 : : }
3243 : : return false;
3244 : : }
3245 : :
3246 : : /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
3247 : 717 : fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore
3248 : : ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
3249 : 3299 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3250 : : return false;
3251 : :
3252 : 457 : gcall *repl = gimple_build_call (fn, 3, dest, src, len);
3253 : 457 : replace_call_with_call_and_fold (gsi, repl);
3254 : 457 : return true;
3255 : : }
3256 : :
3257 : : /* Fold function call to builtin stpcpy with arguments DEST and SRC.
3258 : : Return NULL_TREE if no simplification can be made. */
3259 : :
3260 : : static bool
3261 : 4111 : gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
3262 : : {
3263 : 4111 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3264 : 4111 : location_t loc = gimple_location (stmt);
3265 : 4111 : tree dest = gimple_call_arg (stmt, 0);
3266 : 4111 : tree src = gimple_call_arg (stmt, 1);
3267 : 4111 : tree fn, lenp1;
3268 : :
3269 : 8222 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3270 : : return false;
3271 : :
3272 : : /* If the result is unused, replace stpcpy with strcpy. */
3273 : 4111 : if (gimple_call_lhs (stmt) == NULL_TREE)
3274 : : {
3275 : 30 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3276 : 30 : if (!fn)
3277 : : return false;
3278 : 30 : gimple_call_set_fndecl (stmt, fn);
3279 : 30 : fold_stmt (gsi);
3280 : 30 : return true;
3281 : : }
3282 : :
3283 : : /* Set to non-null if ARG refers to an unterminated array. */
3284 : 4081 : c_strlen_data data = { };
3285 : : /* The size of the unterminated array if SRC referes to one. */
3286 : 4081 : tree size;
3287 : : /* True if the size is exact/constant, false if it's the lower bound
3288 : : of a range. */
3289 : 4081 : bool exact;
3290 : 4081 : tree len = c_strlen (src, 1, &data, 1);
3291 : 4081 : if (!len
3292 : 757 : || TREE_CODE (len) != INTEGER_CST)
3293 : : {
3294 : 3584 : data.decl = unterminated_array (src, &size, &exact);
3295 : 3584 : if (!data.decl)
3296 : : return false;
3297 : : }
3298 : :
3299 : 1172 : if (data.decl)
3300 : : {
3301 : : /* Avoid folding calls with unterminated arrays. */
3302 : 675 : if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
3303 : 75 : warn_string_no_nul (loc, stmt, "stpcpy", src, data.decl, size,
3304 : : exact);
3305 : 675 : suppress_warning (stmt, OPT_Wstringop_overread);
3306 : 675 : return false;
3307 : : }
3308 : :
3309 : 497 : if (optimize_function_for_size_p (cfun)
3310 : : /* If length is zero it's small enough. */
3311 : 497 : && !integer_zerop (len))
3312 : : return false;
3313 : :
3314 : : /* If the source has a known length replace stpcpy with memcpy. */
3315 : 4081 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3316 : 290 : if (!fn)
3317 : : return false;
3318 : :
3319 : 290 : gimple_seq stmts = NULL;
3320 : 290 : tree tem = gimple_convert (&stmts, loc, size_type_node, len);
3321 : 290 : lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
3322 : : tem, build_int_cst (size_type_node, 1));
3323 : 290 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3324 : 290 : gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
3325 : 290 : gimple_move_vops (repl, stmt);
3326 : 290 : gsi_insert_before (gsi, repl, GSI_SAME_STMT);
3327 : : /* Replace the result with dest + len. */
3328 : 290 : stmts = NULL;
3329 : 290 : tem = gimple_convert (&stmts, loc, sizetype, len);
3330 : 290 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3331 : 290 : gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
3332 : : POINTER_PLUS_EXPR, dest, tem);
3333 : 290 : gsi_replace (gsi, ret, false);
3334 : : /* Finally fold the memcpy call. */
3335 : 290 : gimple_stmt_iterator gsi2 = *gsi;
3336 : 290 : gsi_prev (&gsi2);
3337 : 290 : fold_stmt (&gsi2);
3338 : 290 : return true;
3339 : : }
3340 : :
3341 : : /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3342 : : NULL_TREE if a normal call should be emitted rather than expanding
3343 : : the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3344 : : BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3345 : : passed as second argument. */
3346 : :
3347 : : static bool
3348 : 2917 : gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
3349 : : enum built_in_function fcode)
3350 : : {
3351 : 2917 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3352 : 2917 : tree dest, size, len, fn, fmt, flag;
3353 : 2917 : const char *fmt_str;
3354 : :
3355 : : /* Verify the required arguments in the original call. */
3356 : 2917 : if (gimple_call_num_args (stmt) < 5)
3357 : : return false;
3358 : :
3359 : 2917 : dest = gimple_call_arg (stmt, 0);
3360 : 2917 : len = gimple_call_arg (stmt, 1);
3361 : 2917 : flag = gimple_call_arg (stmt, 2);
3362 : 2917 : size = gimple_call_arg (stmt, 3);
3363 : 2917 : fmt = gimple_call_arg (stmt, 4);
3364 : :
3365 : 2917 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3366 : 2917 : if (! integer_all_onesp (size)
3367 : 2917 : && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
3368 : : return false;
3369 : :
3370 : 314 : if (!init_target_chars ())
3371 : : return false;
3372 : :
3373 : : /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3374 : : or if format doesn't contain % chars or is "%s". */
3375 : 314 : if (! integer_zerop (flag))
3376 : : {
3377 : 58 : fmt_str = c_getstr (fmt);
3378 : 58 : if (fmt_str == NULL)
3379 : : return false;
3380 : 58 : if (strchr (fmt_str, target_percent) != NULL
3381 : 57 : && strcmp (fmt_str, target_percent_s))
3382 : : return false;
3383 : : }
3384 : :
3385 : : /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3386 : : available. */
3387 : 415 : fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
3388 : : ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
3389 : 3176 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3390 : : return false;
3391 : :
3392 : : /* Replace the called function and the first 5 argument by 3 retaining
3393 : : trailing varargs. */
3394 : 259 : gimple_call_set_fndecl (stmt, fn);
3395 : 259 : gimple_call_set_fntype (stmt, TREE_TYPE (fn));
3396 : 259 : gimple_call_set_arg (stmt, 0, dest);
3397 : 259 : gimple_call_set_arg (stmt, 1, len);
3398 : 259 : gimple_call_set_arg (stmt, 2, fmt);
3399 : 546 : for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
3400 : 287 : gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
3401 : 259 : gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
3402 : 259 : fold_stmt (gsi);
3403 : 259 : return true;
3404 : : }
3405 : :
3406 : : /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3407 : : Return NULL_TREE if a normal call should be emitted rather than
3408 : : expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3409 : : or BUILT_IN_VSPRINTF_CHK. */
3410 : :
3411 : : static bool
3412 : 5105 : gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
3413 : : enum built_in_function fcode)
3414 : : {
3415 : 5105 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3416 : 5105 : tree dest, size, len, fn, fmt, flag;
3417 : 5105 : const char *fmt_str;
3418 : 5105 : unsigned nargs = gimple_call_num_args (stmt);
3419 : :
3420 : : /* Verify the required arguments in the original call. */
3421 : 5105 : if (nargs < 4)
3422 : : return false;
3423 : 5105 : dest = gimple_call_arg (stmt, 0);
3424 : 5105 : flag = gimple_call_arg (stmt, 1);
3425 : 5105 : size = gimple_call_arg (stmt, 2);
3426 : 5105 : fmt = gimple_call_arg (stmt, 3);
3427 : :
3428 : 5105 : len = NULL_TREE;
3429 : :
3430 : 5105 : if (!init_target_chars ())
3431 : : return false;
3432 : :
3433 : : /* Check whether the format is a literal string constant. */
3434 : 5105 : fmt_str = c_getstr (fmt);
3435 : 5105 : if (fmt_str != NULL)
3436 : : {
3437 : : /* If the format doesn't contain % args or %%, we know the size. */
3438 : 4681 : if (strchr (fmt_str, target_percent) == 0)
3439 : : {
3440 : 322 : if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
3441 : 322 : len = build_int_cstu (size_type_node, strlen (fmt_str));
3442 : : }
3443 : : /* If the format is "%s" and first ... argument is a string literal,
3444 : : we know the size too. */
3445 : 4359 : else if (fcode == BUILT_IN_SPRINTF_CHK
3446 : 3400 : && strcmp (fmt_str, target_percent_s) == 0)
3447 : : {
3448 : 490 : tree arg;
3449 : :
3450 : 490 : if (nargs == 5)
3451 : : {
3452 : 490 : arg = gimple_call_arg (stmt, 4);
3453 : 490 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
3454 : 454 : len = c_strlen (arg, 1);
3455 : : }
3456 : : }
3457 : : }
3458 : :
3459 : 5105 : if (! integer_all_onesp (size) && !known_lower (stmt, len, size, true))
3460 : : return false;
3461 : :
3462 : : /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3463 : : or if format doesn't contain % chars or is "%s". */
3464 : 202 : if (! integer_zerop (flag))
3465 : : {
3466 : 1 : if (fmt_str == NULL)
3467 : : return false;
3468 : 1 : if (strchr (fmt_str, target_percent) != NULL
3469 : 0 : && strcmp (fmt_str, target_percent_s))
3470 : : return false;
3471 : : }
3472 : :
3473 : : /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3474 : 347 : fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
3475 : : ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
3476 : 5307 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3477 : : return false;
3478 : :
3479 : : /* Replace the called function and the first 4 argument by 2 retaining
3480 : : trailing varargs. */
3481 : 202 : gimple_call_set_fndecl (stmt, fn);
3482 : 202 : gimple_call_set_fntype (stmt, TREE_TYPE (fn));
3483 : 202 : gimple_call_set_arg (stmt, 0, dest);
3484 : 202 : gimple_call_set_arg (stmt, 1, fmt);
3485 : 400 : for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
3486 : 198 : gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
3487 : 202 : gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
3488 : 202 : fold_stmt (gsi);
3489 : 202 : return true;
3490 : : }
3491 : :
3492 : : /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3493 : : ORIG may be null if this is a 2-argument call. We don't attempt to
3494 : : simplify calls with more than 3 arguments.
3495 : :
3496 : : Return true if simplification was possible, otherwise false. */
3497 : :
3498 : : bool
3499 : 2263 : gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
3500 : : {
3501 : 2263 : gimple *stmt = gsi_stmt (*gsi);
3502 : :
3503 : : /* Verify the required arguments in the original call. We deal with two
3504 : : types of sprintf() calls: 'sprintf (str, fmt)' and
3505 : : 'sprintf (dest, "%s", orig)'. */
3506 : 2263 : if (gimple_call_num_args (stmt) > 3)
3507 : : return false;
3508 : :
3509 : 1862 : tree orig = NULL_TREE;
3510 : 1862 : if (gimple_call_num_args (stmt) == 3)
3511 : 1766 : orig = gimple_call_arg (stmt, 2);
3512 : :
3513 : : /* Check whether the format is a literal string constant. */
3514 : 1862 : tree fmt = gimple_call_arg (stmt, 1);
3515 : 1862 : const char *fmt_str = c_getstr (fmt);
3516 : 1862 : if (fmt_str == NULL)
3517 : : return false;
3518 : :
3519 : 1862 : tree dest = gimple_call_arg (stmt, 0);
3520 : :
3521 : 1862 : if (!init_target_chars ())
3522 : : return false;
3523 : :
3524 : 1862 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3525 : 5133 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3526 : : return false;
3527 : :
3528 : : /* If the format doesn't contain % args or %%, use strcpy. */
3529 : 1862 : if (strchr (fmt_str, target_percent) == NULL)
3530 : : {
3531 : : /* Don't optimize sprintf (buf, "abc", ptr++). */
3532 : 108 : if (orig)
3533 : : return false;
3534 : :
3535 : : /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3536 : : 'format' is known to contain no % formats. */
3537 : 95 : gimple_seq stmts = NULL;
3538 : 95 : gimple *repl = gimple_build_call (fn, 2, dest, fmt);
3539 : :
3540 : : /* Propagate the NO_WARNING bit to avoid issuing the same
3541 : : warning more than once. */
3542 : 95 : copy_warning (repl, stmt);
3543 : :
3544 : 95 : gimple_seq_add_stmt_without_update (&stmts, repl);
3545 : 95 : if (tree lhs = gimple_call_lhs (stmt))
3546 : : {
3547 : 0 : repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs),
3548 : 0 : strlen (fmt_str)));
3549 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3550 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3551 : : /* gsi now points at the assignment to the lhs, get a
3552 : : stmt iterator to the memcpy call.
3553 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3554 : : CFG isn't built yet. */
3555 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3556 : 0 : gsi_prev (&gsi2);
3557 : 0 : fold_stmt (&gsi2);
3558 : : }
3559 : : else
3560 : : {
3561 : 95 : gsi_replace_with_seq_vops (gsi, stmts);
3562 : 95 : fold_stmt (gsi);
3563 : : }
3564 : 95 : return true;
3565 : : }
3566 : :
3567 : : /* If the format is "%s", use strcpy if the result isn't used. */
3568 : 1754 : else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3569 : : {
3570 : : /* Don't crash on sprintf (str1, "%s"). */
3571 : 746 : if (!orig)
3572 : : return false;
3573 : :
3574 : : /* Don't fold calls with source arguments of invalid (nonpointer)
3575 : : types. */
3576 : 745 : if (!POINTER_TYPE_P (TREE_TYPE (orig)))
3577 : : return false;
3578 : :
3579 : 739 : tree orig_len = NULL_TREE;
3580 : 739 : if (gimple_call_lhs (stmt))
3581 : : {
3582 : 17 : orig_len = get_maxval_strlen (orig, SRK_STRLEN);
3583 : 17 : if (!orig_len)
3584 : : return false;
3585 : : }
3586 : :
3587 : : /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3588 : 722 : gimple_seq stmts = NULL;
3589 : 722 : gimple *repl = gimple_build_call (fn, 2, dest, orig);
3590 : :
3591 : : /* Propagate the NO_WARNING bit to avoid issuing the same
3592 : : warning more than once. */
3593 : 722 : copy_warning (repl, stmt);
3594 : :
3595 : 722 : gimple_seq_add_stmt_without_update (&stmts, repl);
3596 : 722 : if (tree lhs = gimple_call_lhs (stmt))
3597 : : {
3598 : 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
3599 : 0 : TREE_TYPE (orig_len)))
3600 : 0 : orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
3601 : 0 : repl = gimple_build_assign (lhs, orig_len);
3602 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3603 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3604 : : /* gsi now points at the assignment to the lhs, get a
3605 : : stmt iterator to the memcpy call.
3606 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3607 : : CFG isn't built yet. */
3608 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3609 : 0 : gsi_prev (&gsi2);
3610 : 0 : fold_stmt (&gsi2);
3611 : : }
3612 : : else
3613 : : {
3614 : 722 : gsi_replace_with_seq_vops (gsi, stmts);
3615 : 722 : fold_stmt (gsi);
3616 : : }
3617 : 722 : return true;
3618 : : }
3619 : : return false;
3620 : : }
3621 : :
3622 : : /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3623 : : FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3624 : : attempt to simplify calls with more than 4 arguments.
3625 : :
3626 : : Return true if simplification was possible, otherwise false. */
3627 : :
3628 : : bool
3629 : 1580 : gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
3630 : : {
3631 : 1580 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3632 : 1580 : tree dest = gimple_call_arg (stmt, 0);
3633 : 1580 : tree destsize = gimple_call_arg (stmt, 1);
3634 : 1580 : tree fmt = gimple_call_arg (stmt, 2);
3635 : 1580 : tree orig = NULL_TREE;
3636 : 1580 : const char *fmt_str = NULL;
3637 : :
3638 : 1580 : if (gimple_call_num_args (stmt) > 4
3639 : 2708 : || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3640 : : return false;
3641 : :
3642 : 707 : if (gimple_call_num_args (stmt) == 4)
3643 : 588 : orig = gimple_call_arg (stmt, 3);
3644 : :
3645 : : /* Check whether the format is a literal string constant. */
3646 : 707 : fmt_str = c_getstr (fmt);
3647 : 707 : if (fmt_str == NULL)
3648 : : return false;
3649 : :
3650 : 707 : if (!init_target_chars ())
3651 : : return false;
3652 : :
3653 : : /* If the format doesn't contain % args or %%, use strcpy. */
3654 : 707 : if (strchr (fmt_str, target_percent) == NULL)
3655 : : {
3656 : 148 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3657 : 118 : if (!fn)
3658 : : return false;
3659 : :
3660 : : /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3661 : 118 : if (orig)
3662 : : return false;
3663 : :
3664 : 118 : tree len = build_int_cstu (TREE_TYPE (destsize), strlen (fmt_str));
3665 : :
3666 : : /* We could expand this as
3667 : : memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3668 : : or to
3669 : : memcpy (str, fmt_with_nul_at_cstm1, cst);
3670 : : but in the former case that might increase code size
3671 : : and in the latter case grow .rodata section too much.
3672 : : So punt for now. */
3673 : 118 : if (!known_lower (stmt, len, destsize, true))
3674 : : return false;
3675 : :
3676 : 88 : gimple_seq stmts = NULL;
3677 : 88 : gimple *repl = gimple_build_call (fn, 2, dest, fmt);
3678 : 88 : gimple_seq_add_stmt_without_update (&stmts, repl);
3679 : 88 : if (tree lhs = gimple_call_lhs (stmt))
3680 : : {
3681 : 0 : repl = gimple_build_assign (lhs,
3682 : 0 : fold_convert (TREE_TYPE (lhs), len));
3683 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3684 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3685 : : /* gsi now points at the assignment to the lhs, get a
3686 : : stmt iterator to the memcpy call.
3687 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3688 : : CFG isn't built yet. */
3689 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3690 : 0 : gsi_prev (&gsi2);
3691 : 0 : fold_stmt (&gsi2);
3692 : : }
3693 : : else
3694 : : {
3695 : 88 : gsi_replace_with_seq_vops (gsi, stmts);
3696 : 88 : fold_stmt (gsi);
3697 : : }
3698 : 88 : return true;
3699 : : }
3700 : :
3701 : : /* If the format is "%s", use strcpy if the result isn't used. */
3702 : 589 : else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3703 : : {
3704 : 292 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3705 : 174 : if (!fn)
3706 : : return false;
3707 : :
3708 : : /* Don't crash on snprintf (str1, cst, "%s"). */
3709 : 174 : if (!orig)
3710 : : return false;
3711 : :
3712 : 174 : tree orig_len = get_maxval_strlen (orig, SRK_STRLEN);
3713 : :
3714 : : /* We could expand this as
3715 : : memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3716 : : or to
3717 : : memcpy (str1, str2_with_nul_at_cstm1, cst);
3718 : : but in the former case that might increase code size
3719 : : and in the latter case grow .rodata section too much.
3720 : : So punt for now. */
3721 : 174 : if (!known_lower (stmt, orig_len, destsize, true))
3722 : : return false;
3723 : :
3724 : : /* Convert snprintf (str1, cst, "%s", str2) into
3725 : : strcpy (str1, str2) if strlen (str2) < cst. */
3726 : 56 : gimple_seq stmts = NULL;
3727 : 56 : gimple *repl = gimple_build_call (fn, 2, dest, orig);
3728 : 56 : gimple_seq_add_stmt_without_update (&stmts, repl);
3729 : 56 : if (tree lhs = gimple_call_lhs (stmt))
3730 : : {
3731 : 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
3732 : 0 : TREE_TYPE (orig_len)))
3733 : 0 : orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
3734 : 0 : repl = gimple_build_assign (lhs, orig_len);
3735 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3736 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3737 : : /* gsi now points at the assignment to the lhs, get a
3738 : : stmt iterator to the memcpy call.
3739 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3740 : : CFG isn't built yet. */
3741 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3742 : 0 : gsi_prev (&gsi2);
3743 : 0 : fold_stmt (&gsi2);
3744 : : }
3745 : : else
3746 : : {
3747 : 56 : gsi_replace_with_seq_vops (gsi, stmts);
3748 : 56 : fold_stmt (gsi);
3749 : : }
3750 : 56 : return true;
3751 : : }
3752 : : return false;
3753 : : }
3754 : :
3755 : : /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3756 : : FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3757 : : more than 3 arguments, and ARG may be null in the 2-argument case.
3758 : :
3759 : : Return NULL_TREE if no simplification was possible, otherwise return the
3760 : : simplified form of the call as a tree. FCODE is the BUILT_IN_*
3761 : : code of the function to be simplified. */
3762 : :
3763 : : static bool
3764 : 59478 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
3765 : : tree fp, tree fmt, tree arg,
3766 : : enum built_in_function fcode)
3767 : : {
3768 : 59478 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3769 : 59478 : tree fn_fputc, fn_fputs;
3770 : 59478 : const char *fmt_str = NULL;
3771 : :
3772 : : /* If the return value is used, don't do the transformation. */
3773 : 59478 : if (gimple_call_lhs (stmt) != NULL_TREE)
3774 : : return false;
3775 : :
3776 : : /* Check whether the format is a literal string constant. */
3777 : 54784 : fmt_str = c_getstr (fmt);
3778 : 54784 : if (fmt_str == NULL)
3779 : : return false;
3780 : :
3781 : 54504 : if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
3782 : : {
3783 : : /* If we're using an unlocked function, assume the other
3784 : : unlocked functions exist explicitly. */
3785 : 86 : fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
3786 : 86 : fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
3787 : : }
3788 : : else
3789 : : {
3790 : 54418 : fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
3791 : 54418 : fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
3792 : : }
3793 : :
3794 : 54504 : if (!init_target_chars ())
3795 : : return false;
3796 : :
3797 : 158262 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3798 : : return false;
3799 : :
3800 : : /* If the format doesn't contain % args or %%, use strcpy. */
3801 : 54504 : if (strchr (fmt_str, target_percent) == NULL)
3802 : : {
3803 : 9536 : if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
3804 : 9466 : && arg)
3805 : : return false;
3806 : :
3807 : : /* If the format specifier was "", fprintf does nothing. */
3808 : 9536 : if (fmt_str[0] == '\0')
3809 : : {
3810 : 58 : replace_call_with_value (gsi, NULL_TREE);
3811 : 58 : return true;
3812 : : }
3813 : :
3814 : : /* When "string" doesn't contain %, replace all cases of
3815 : : fprintf (fp, string) with fputs (string, fp). The fputs
3816 : : builtin will take care of special cases like length == 1. */
3817 : 9478 : if (fn_fputs)
3818 : : {
3819 : 9478 : gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
3820 : 9478 : replace_call_with_call_and_fold (gsi, repl);
3821 : 9478 : return true;
3822 : : }
3823 : : }
3824 : :
3825 : : /* The other optimizations can be done only on the non-va_list variants. */
3826 : 44968 : else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
3827 : : return false;
3828 : :
3829 : : /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3830 : 43761 : else if (strcmp (fmt_str, target_percent_s) == 0)
3831 : : {
3832 : 639 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3833 : : return false;
3834 : 639 : if (fn_fputs)
3835 : : {
3836 : 639 : gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3837 : 639 : replace_call_with_call_and_fold (gsi, repl);
3838 : 639 : return true;
3839 : : }
3840 : : }
3841 : :
3842 : : /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3843 : 43122 : else if (strcmp (fmt_str, target_percent_c) == 0)
3844 : : {
3845 : 49 : if (!arg
3846 : 49 : || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
3847 : 0 : return false;
3848 : 49 : if (fn_fputc)
3849 : : {
3850 : 49 : gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
3851 : 49 : replace_call_with_call_and_fold (gsi, repl);
3852 : 49 : return true;
3853 : : }
3854 : : }
3855 : :
3856 : : return false;
3857 : : }
3858 : :
3859 : : /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3860 : : FMT and ARG are the arguments to the call; we don't fold cases with
3861 : : more than 2 arguments, and ARG may be null if this is a 1-argument case.
3862 : :
3863 : : Return NULL_TREE if no simplification was possible, otherwise return the
3864 : : simplified form of the call as a tree. FCODE is the BUILT_IN_*
3865 : : code of the function to be simplified. */
3866 : :
3867 : : static bool
3868 : 121857 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3869 : : tree arg, enum built_in_function fcode)
3870 : : {
3871 : 121857 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3872 : 121857 : tree fn_putchar, fn_puts, newarg;
3873 : 121857 : const char *fmt_str = NULL;
3874 : :
3875 : : /* If the return value is used, don't do the transformation. */
3876 : 121857 : if (gimple_call_lhs (stmt) != NULL_TREE)
3877 : : return false;
3878 : :
3879 : 353423 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3880 : : return false;
3881 : :
3882 : : /* Check whether the format is a literal string constant. */
3883 : 118208 : fmt_str = c_getstr (fmt);
3884 : 118208 : if (fmt_str == NULL)
3885 : : return false;
3886 : :
3887 : 114749 : if (fcode == BUILT_IN_PRINTF_UNLOCKED)
3888 : : {
3889 : : /* If we're using an unlocked function, assume the other
3890 : : unlocked functions exist explicitly. */
3891 : 86 : fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
3892 : 86 : fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
3893 : : }
3894 : : else
3895 : : {
3896 : 114663 : fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3897 : 114663 : fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3898 : : }
3899 : :
3900 : 114749 : if (!init_target_chars ())
3901 : : return false;
3902 : :
3903 : 114749 : if (strcmp (fmt_str, target_percent_s) == 0
3904 : 107656 : || strchr (fmt_str, target_percent) == NULL)
3905 : : {
3906 : 15023 : const char *str;
3907 : :
3908 : 15023 : if (strcmp (fmt_str, target_percent_s) == 0)
3909 : : {
3910 : 7093 : if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3911 : : return false;
3912 : :
3913 : 6769 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3914 : : return false;
3915 : :
3916 : 6764 : str = c_getstr (arg);
3917 : 6764 : if (str == NULL)
3918 : : return false;
3919 : : }
3920 : : else
3921 : : {
3922 : : /* The format specifier doesn't contain any '%' characters. */
3923 : 7930 : if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3924 : 7788 : && arg)
3925 : : return false;
3926 : : str = fmt_str;
3927 : : }
3928 : :
3929 : : /* If the string was "", printf does nothing. */
3930 : 5722 : if (str[0] == '\0')
3931 : : {
3932 : 109 : replace_call_with_value (gsi, NULL_TREE);
3933 : 109 : return true;
3934 : : }
3935 : :
3936 : : /* If the string has length of 1, call putchar. */
3937 : 5613 : if (str[1] == '\0')
3938 : : {
3939 : : /* Given printf("c"), (where c is any one character,)
3940 : : convert "c"[0] to an int and pass that to the replacement
3941 : : function. */
3942 : 559 : newarg = build_int_cst (integer_type_node, str[0]);
3943 : 559 : if (fn_putchar)
3944 : : {
3945 : 559 : gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
3946 : 559 : replace_call_with_call_and_fold (gsi, repl);
3947 : 559 : return true;
3948 : : }
3949 : : }
3950 : : else
3951 : : {
3952 : : /* If the string was "string\n", call puts("string"). */
3953 : 5054 : size_t len = strlen (str);
3954 : 5054 : if ((unsigned char)str[len - 1] == target_newline
3955 : 3956 : && (size_t) (int) len == len
3956 : 3956 : && (int) len > 0)
3957 : : {
3958 : 3956 : char *newstr;
3959 : :
3960 : : /* Create a NUL-terminated string that's one char shorter
3961 : : than the original, stripping off the trailing '\n'. */
3962 : 3956 : newstr = xstrdup (str);
3963 : 3956 : newstr[len - 1] = '\0';
3964 : 3956 : newarg = build_string_literal (len, newstr);
3965 : 3956 : free (newstr);
3966 : 3956 : if (fn_puts)
3967 : : {
3968 : 3956 : gcall *repl = gimple_build_call (fn_puts, 1, newarg);
3969 : 3956 : replace_call_with_call_and_fold (gsi, repl);
3970 : 3956 : return true;
3971 : : }
3972 : : }
3973 : : else
3974 : : /* We'd like to arrange to call fputs(string,stdout) here,
3975 : : but we need stdout and don't have a way to get it yet. */
3976 : : return false;
3977 : : }
3978 : : }
3979 : :
3980 : : /* The other optimizations can be done only on the non-va_list variants. */
3981 : 99726 : else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3982 : : return false;
3983 : :
3984 : : /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3985 : 99448 : else if (strcmp (fmt_str, target_percent_s_newline) == 0)
3986 : : {
3987 : 179 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3988 : : return false;
3989 : 179 : if (fn_puts)
3990 : : {
3991 : 179 : gcall *repl = gimple_build_call (fn_puts, 1, arg);
3992 : 179 : replace_call_with_call_and_fold (gsi, repl);
3993 : 179 : return true;
3994 : : }
3995 : : }
3996 : :
3997 : : /* If the format specifier was "%c", call __builtin_putchar(arg). */
3998 : 99269 : else if (strcmp (fmt_str, target_percent_c) == 0)
3999 : : {
4000 : 94 : if (!arg || ! useless_type_conversion_p (integer_type_node,
4001 : 47 : TREE_TYPE (arg)))
4002 : 0 : return false;
4003 : 47 : if (fn_putchar)
4004 : : {
4005 : 47 : gcall *repl = gimple_build_call (fn_putchar, 1, arg);
4006 : 47 : replace_call_with_call_and_fold (gsi, repl);
4007 : 47 : return true;
4008 : : }
4009 : : }
4010 : :
4011 : : return false;
4012 : : }
4013 : :
4014 : :
4015 : :
4016 : : /* Fold a call to __builtin_strlen with known length LEN. */
4017 : :
4018 : : static bool
4019 : 158558 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
4020 : : {
4021 : 158558 : gimple *stmt = gsi_stmt (*gsi);
4022 : 158558 : tree arg = gimple_call_arg (stmt, 0);
4023 : :
4024 : 158558 : wide_int minlen;
4025 : 158558 : wide_int maxlen;
4026 : :
4027 : 158558 : c_strlen_data lendata = { };
4028 : 158558 : if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
4029 : 38841 : && !lendata.decl
4030 : 35588 : && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
4031 : 194041 : && lendata.maxlen && TREE_CODE (lendata.maxlen) == INTEGER_CST)
4032 : : {
4033 : : /* The range of lengths refers to either a single constant
4034 : : string or to the longest and shortest constant string
4035 : : referenced by the argument of the strlen() call, or to
4036 : : the strings that can possibly be stored in the arrays
4037 : : the argument refers to. */
4038 : 35483 : minlen = wi::to_wide (lendata.minlen);
4039 : 35483 : maxlen = wi::to_wide (lendata.maxlen);
4040 : : }
4041 : : else
4042 : : {
4043 : 123075 : unsigned prec = TYPE_PRECISION (sizetype);
4044 : :
4045 : 123075 : minlen = wi::shwi (0, prec);
4046 : 123075 : maxlen = wi::to_wide (max_object_size (), prec) - 2;
4047 : : }
4048 : :
4049 : : /* For -fsanitize=address, don't optimize the upper bound of the
4050 : : length to be able to diagnose UB on non-zero terminated arrays. */
4051 : 158558 : if (sanitize_flags_p (SANITIZE_ADDRESS))
4052 : 287 : maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
4053 : :
4054 : 158558 : if (minlen == maxlen)
4055 : : {
4056 : : /* Fold the strlen call to a constant. */
4057 : 3357 : tree type = TREE_TYPE (lendata.minlen);
4058 : 6714 : tree len = force_gimple_operand_gsi (gsi,
4059 : 3357 : wide_int_to_tree (type, minlen),
4060 : : true, NULL, true, GSI_SAME_STMT);
4061 : 3357 : replace_call_with_value (gsi, len);
4062 : 3357 : return true;
4063 : : }
4064 : :
4065 : : /* Set the strlen() range to [0, MAXLEN]. */
4066 : 155201 : if (tree lhs = gimple_call_lhs (stmt))
4067 : 155196 : set_strlen_range (lhs, minlen, maxlen);
4068 : :
4069 : : return false;
4070 : 158558 : }
4071 : :
4072 : : static bool
4073 : 206 : gimple_fold_builtin_omp_is_initial_device (gimple_stmt_iterator *gsi)
4074 : : {
4075 : : #if ACCEL_COMPILER
4076 : : replace_call_with_value (gsi, integer_zero_node);
4077 : : return true;
4078 : : #else
4079 : 206 : if (!ENABLE_OFFLOADING || symtab->state == EXPANSION)
4080 : : {
4081 : 0 : replace_call_with_value (gsi, integer_one_node);
4082 : 206 : return true;
4083 : : }
4084 : : #endif
4085 : : return false;
4086 : : }
4087 : :
4088 : : /* omp_get_initial_device was in OpenMP 5.0/5.1 explicitly and in
4089 : : 5.0 implicitly the same as omp_get_num_devices; since 6.0 it is
4090 : : unspecified whether -1 or omp_get_num_devices() is returned. For
4091 : : better backward compatibility, use omp_get_num_devices() on the
4092 : : host - and -1 on the device (where the result is unspecified). */
4093 : :
4094 : : static bool
4095 : 98 : gimple_fold_builtin_omp_get_initial_device (gimple_stmt_iterator *gsi)
4096 : : {
4097 : : #if ACCEL_COMPILER
4098 : : replace_call_with_value (gsi, build_int_cst (integer_type_node, -1));
4099 : : #else
4100 : 98 : if (!ENABLE_OFFLOADING)
4101 : 0 : replace_call_with_value (gsi, integer_zero_node);
4102 : : else
4103 : : {
4104 : : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
4105 : : gcall *repl = gimple_build_call (fn, 0);
4106 : : replace_call_with_call_and_fold (gsi, repl);
4107 : : }
4108 : : #endif
4109 : 98 : return true;
4110 : : }
4111 : :
4112 : : static bool
4113 : 265 : gimple_fold_builtin_omp_get_num_devices (gimple_stmt_iterator *gsi)
4114 : : {
4115 : 265 : if (!ENABLE_OFFLOADING)
4116 : : {
4117 : 0 : replace_call_with_value (gsi, integer_zero_node);
4118 : 265 : return true;
4119 : : }
4120 : : return false;
4121 : : }
4122 : :
4123 : : /* Fold a call to __builtin_acc_on_device. */
4124 : :
4125 : : static bool
4126 : 2866 : gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
4127 : : {
4128 : : /* Defer folding until we know which compiler we're in. */
4129 : 2866 : if (symtab->state != EXPANSION)
4130 : : return false;
4131 : :
4132 : 554 : unsigned val_host = GOMP_DEVICE_HOST;
4133 : 554 : unsigned val_dev = GOMP_DEVICE_NONE;
4134 : :
4135 : : #ifdef ACCEL_COMPILER
4136 : : val_host = GOMP_DEVICE_NOT_HOST;
4137 : : val_dev = ACCEL_COMPILER_acc_device;
4138 : : #endif
4139 : :
4140 : 554 : location_t loc = gimple_location (gsi_stmt (*gsi));
4141 : :
4142 : 554 : tree host_eq = make_ssa_name (boolean_type_node);
4143 : 554 : gimple *host_ass = gimple_build_assign
4144 : 554 : (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
4145 : 554 : gimple_set_location (host_ass, loc);
4146 : 554 : gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
4147 : :
4148 : 554 : tree dev_eq = make_ssa_name (boolean_type_node);
4149 : 554 : gimple *dev_ass = gimple_build_assign
4150 : 554 : (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
4151 : 554 : gimple_set_location (dev_ass, loc);
4152 : 554 : gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
4153 : :
4154 : 554 : tree result = make_ssa_name (boolean_type_node);
4155 : 554 : gimple *result_ass = gimple_build_assign
4156 : 554 : (result, BIT_IOR_EXPR, host_eq, dev_eq);
4157 : 554 : gimple_set_location (result_ass, loc);
4158 : 554 : gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
4159 : :
4160 : 554 : replace_call_with_value (gsi, result);
4161 : :
4162 : 554 : return true;
4163 : : }
4164 : :
4165 : : /* Fold realloc (0, n) -> malloc (n). */
4166 : :
4167 : : static bool
4168 : 48134 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
4169 : : {
4170 : 48134 : gimple *stmt = gsi_stmt (*gsi);
4171 : 48134 : tree arg = gimple_call_arg (stmt, 0);
4172 : 48134 : tree size = gimple_call_arg (stmt, 1);
4173 : :
4174 : 143172 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
4175 : : return false;
4176 : :
4177 : 48134 : if (operand_equal_p (arg, null_pointer_node, 0))
4178 : : {
4179 : 1230 : tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
4180 : 1230 : if (fn_malloc)
4181 : : {
4182 : 1230 : gcall *repl = gimple_build_call (fn_malloc, 1, size);
4183 : 1230 : replace_call_with_call_and_fold (gsi, repl);
4184 : 1230 : return true;
4185 : : }
4186 : : }
4187 : : return false;
4188 : : }
4189 : :
4190 : : /* Number of bytes into which any type but aggregate, vector or
4191 : : _BitInt types should fit. */
4192 : : static constexpr size_t clear_padding_unit
4193 : : = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT;
4194 : : /* Buffer size on which __builtin_clear_padding folding code works. */
4195 : : static const size_t clear_padding_buf_size = 32 * clear_padding_unit;
4196 : :
4197 : : /* Data passed through __builtin_clear_padding folding. */
4198 : : struct clear_padding_struct {
4199 : : location_t loc;
4200 : : /* 0 during __builtin_clear_padding folding, nonzero during
4201 : : clear_type_padding_in_mask. In that case, instead of clearing the
4202 : : non-padding bits in union_ptr array clear the padding bits in there. */
4203 : : bool clear_in_mask;
4204 : : tree base;
4205 : : tree alias_type;
4206 : : gimple_stmt_iterator *gsi;
4207 : : /* Alignment of buf->base + 0. */
4208 : : unsigned align;
4209 : : /* Offset from buf->base. Should be always a multiple of UNITS_PER_WORD. */
4210 : : HOST_WIDE_INT off;
4211 : : /* Number of padding bytes before buf->off that don't have padding clear
4212 : : code emitted yet. */
4213 : : HOST_WIDE_INT padding_bytes;
4214 : : /* The size of the whole object. Never emit code to touch
4215 : : buf->base + buf->sz or following bytes. */
4216 : : HOST_WIDE_INT sz;
4217 : : /* Number of bytes recorded in buf->buf. */
4218 : : size_t size;
4219 : : /* When inside union, instead of emitting code we and bits inside of
4220 : : the union_ptr array. */
4221 : : unsigned char *union_ptr;
4222 : : /* Set bits mean padding bits that need to be cleared by the builtin. */
4223 : : unsigned char buf[clear_padding_buf_size + clear_padding_unit];
4224 : : };
4225 : :
4226 : : /* Emit code to clear padding requested in BUF->buf - set bits
4227 : : in there stand for padding that should be cleared. FULL is true
4228 : : if everything from the buffer should be flushed, otherwise
4229 : : it can leave up to 2 * clear_padding_unit bytes for further
4230 : : processing. */
4231 : :
4232 : : static void
4233 : 31212 : clear_padding_flush (clear_padding_struct *buf, bool full)
4234 : : {
4235 : 31212 : gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
4236 : 31212 : if (!full && buf->size < 2 * clear_padding_unit)
4237 : 31212 : return;
4238 : 32264 : gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
4239 : 31170 : size_t end = buf->size;
4240 : 31170 : if (!full)
4241 : 42 : end = ((end - clear_padding_unit - 1) / clear_padding_unit
4242 : : * clear_padding_unit);
4243 : 31170 : size_t padding_bytes = buf->padding_bytes;
4244 : 31170 : if (buf->union_ptr)
4245 : : {
4246 : 30418 : if (buf->clear_in_mask)
4247 : : {
4248 : : /* During clear_type_padding_in_mask, clear the padding
4249 : : bits set in buf->buf in the buf->union_ptr mask. */
4250 : 207086 : for (size_t i = 0; i < end; i++)
4251 : : {
4252 : 177061 : if (buf->buf[i] == (unsigned char) ~0)
4253 : 6816 : padding_bytes++;
4254 : : else
4255 : : {
4256 : 170245 : memset (&buf->union_ptr[buf->off + i - padding_bytes],
4257 : : 0, padding_bytes);
4258 : 170245 : padding_bytes = 0;
4259 : 170245 : buf->union_ptr[buf->off + i] &= ~buf->buf[i];
4260 : : }
4261 : : }
4262 : 30025 : if (full)
4263 : : {
4264 : 30025 : memset (&buf->union_ptr[buf->off + end - padding_bytes],
4265 : : 0, padding_bytes);
4266 : 30025 : buf->off = 0;
4267 : 30025 : buf->size = 0;
4268 : 30025 : buf->padding_bytes = 0;
4269 : : }
4270 : : else
4271 : : {
4272 : 0 : memmove (buf->buf, buf->buf + end, buf->size - end);
4273 : 0 : buf->off += end;
4274 : 0 : buf->size -= end;
4275 : 0 : buf->padding_bytes = padding_bytes;
4276 : : }
4277 : 30025 : return;
4278 : : }
4279 : : /* Inside of a union, instead of emitting any code, instead
4280 : : clear all bits in the union_ptr buffer that are clear
4281 : : in buf. Whole padding bytes don't clear anything. */
4282 : 3017 : for (size_t i = 0; i < end; i++)
4283 : : {
4284 : 2624 : if (buf->buf[i] == (unsigned char) ~0)
4285 : 1424 : padding_bytes++;
4286 : : else
4287 : : {
4288 : 1200 : padding_bytes = 0;
4289 : 1200 : buf->union_ptr[buf->off + i] &= buf->buf[i];
4290 : : }
4291 : : }
4292 : 393 : if (full)
4293 : : {
4294 : 393 : buf->off = 0;
4295 : 393 : buf->size = 0;
4296 : 393 : buf->padding_bytes = 0;
4297 : : }
4298 : : else
4299 : : {
4300 : 0 : memmove (buf->buf, buf->buf + end, buf->size - end);
4301 : 0 : buf->off += end;
4302 : 0 : buf->size -= end;
4303 : 0 : buf->padding_bytes = padding_bytes;
4304 : : }
4305 : 393 : return;
4306 : : }
4307 : 752 : size_t wordsize = UNITS_PER_WORD;
4308 : 43621 : for (size_t i = 0; i < end; i += wordsize)
4309 : : {
4310 : 42869 : size_t nonzero_first = wordsize;
4311 : 42869 : size_t nonzero_last = 0;
4312 : 42869 : size_t zero_first = wordsize;
4313 : 42869 : size_t zero_last = 0;
4314 : 42869 : bool all_ones = true, bytes_only = true;
4315 : 43150 : if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
4316 : 42869 : > (unsigned HOST_WIDE_INT) buf->sz)
4317 : : {
4318 : 281 : gcc_assert (wordsize > 1);
4319 : 281 : wordsize /= 2;
4320 : 281 : i -= wordsize;
4321 : 281 : continue;
4322 : : }
4323 : 42588 : size_t endsize = end - i > wordsize ? wordsize : end - i;
4324 : 382067 : for (size_t j = i; j < i + endsize; j++)
4325 : : {
4326 : 339479 : if (buf->buf[j])
4327 : : {
4328 : 329618 : if (nonzero_first == wordsize)
4329 : : {
4330 : 41660 : nonzero_first = j - i;
4331 : 41660 : nonzero_last = j - i;
4332 : : }
4333 : 329618 : if (nonzero_last != j - i)
4334 : 158 : all_ones = false;
4335 : 329618 : nonzero_last = j + 1 - i;
4336 : : }
4337 : : else
4338 : : {
4339 : 9861 : if (zero_first == wordsize)
4340 : 1915 : zero_first = j - i;
4341 : 9861 : zero_last = j + 1 - i;
4342 : : }
4343 : 339479 : if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
4344 : : {
4345 : 79 : all_ones = false;
4346 : 79 : bytes_only = false;
4347 : : }
4348 : : }
4349 : 42588 : size_t padding_end = i;
4350 : 42588 : if (padding_bytes)
4351 : : {
4352 : 41008 : if (nonzero_first == 0
4353 : 41008 : && nonzero_last == endsize
4354 : 40560 : && all_ones)
4355 : : {
4356 : : /* All bits are padding and we had some padding
4357 : : before too. Just extend it. */
4358 : 40560 : padding_bytes += endsize;
4359 : 40560 : continue;
4360 : : }
4361 : 448 : if (all_ones && nonzero_first == 0)
4362 : : {
4363 : 4 : padding_bytes += nonzero_last;
4364 : 4 : padding_end += nonzero_last;
4365 : 4 : nonzero_first = wordsize;
4366 : 4 : nonzero_last = 0;
4367 : : }
4368 : 444 : else if (bytes_only && nonzero_first == 0)
4369 : : {
4370 : 0 : gcc_assert (zero_first && zero_first != wordsize);
4371 : 0 : padding_bytes += zero_first;
4372 : 0 : padding_end += zero_first;
4373 : : }
4374 : 448 : tree atype, src;
4375 : 448 : if (padding_bytes == 1)
4376 : : {
4377 : 33 : atype = char_type_node;
4378 : 33 : src = build_zero_cst (char_type_node);
4379 : : }
4380 : : else
4381 : : {
4382 : 415 : atype = build_array_type_nelts (char_type_node, padding_bytes);
4383 : 415 : src = build_constructor (atype, NULL);
4384 : : }
4385 : 448 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4386 : : build_int_cst (buf->alias_type,
4387 : 448 : buf->off + padding_end
4388 : 448 : - padding_bytes));
4389 : 448 : gimple *g = gimple_build_assign (dst, src);
4390 : 448 : gimple_set_location (g, buf->loc);
4391 : 448 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4392 : 448 : padding_bytes = 0;
4393 : 448 : buf->padding_bytes = 0;
4394 : : }
4395 : 2028 : if (nonzero_first == wordsize)
4396 : : /* All bits in a word are 0, there are no padding bits. */
4397 : 932 : continue;
4398 : 1096 : if (all_ones && nonzero_last == endsize)
4399 : : {
4400 : : /* All bits between nonzero_first and end of word are padding
4401 : : bits, start counting padding_bytes. */
4402 : 833 : padding_bytes = nonzero_last - nonzero_first;
4403 : 833 : continue;
4404 : : }
4405 : 263 : if (bytes_only)
4406 : : {
4407 : : /* If bitfields aren't involved in this word, prefer storing
4408 : : individual bytes or groups of them over performing a RMW
4409 : : operation on the whole word. */
4410 : 226 : gcc_assert (i + zero_last <= end);
4411 : 1113 : for (size_t j = padding_end; j < i + zero_last; j++)
4412 : : {
4413 : 887 : if (buf->buf[j])
4414 : : {
4415 : : size_t k;
4416 : 604 : for (k = j; k < i + zero_last; k++)
4417 : 604 : if (buf->buf[k] == 0)
4418 : : break;
4419 : 258 : HOST_WIDE_INT off = buf->off + j;
4420 : 258 : tree atype, src;
4421 : 258 : if (k - j == 1)
4422 : : {
4423 : 214 : atype = char_type_node;
4424 : 214 : src = build_zero_cst (char_type_node);
4425 : : }
4426 : : else
4427 : : {
4428 : 44 : atype = build_array_type_nelts (char_type_node, k - j);
4429 : 44 : src = build_constructor (atype, NULL);
4430 : : }
4431 : 258 : tree dst = build2_loc (buf->loc, MEM_REF, atype,
4432 : : buf->base,
4433 : 258 : build_int_cst (buf->alias_type, off));
4434 : 258 : gimple *g = gimple_build_assign (dst, src);
4435 : 258 : gimple_set_location (g, buf->loc);
4436 : 258 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4437 : 258 : j = k;
4438 : : }
4439 : : }
4440 : 226 : if (nonzero_last == endsize)
4441 : 98 : padding_bytes = nonzero_last - zero_last;
4442 : 226 : continue;
4443 : 226 : }
4444 : 120 : for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
4445 : : {
4446 : 120 : if (nonzero_last - nonzero_first <= eltsz
4447 : 37 : && ((nonzero_first & ~(eltsz - 1))
4448 : 37 : == ((nonzero_last - 1) & ~(eltsz - 1))))
4449 : : {
4450 : 37 : tree type;
4451 : 37 : if (eltsz == 1)
4452 : 2 : type = char_type_node;
4453 : : else
4454 : 35 : type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
4455 : : 0);
4456 : 37 : size_t start = nonzero_first & ~(eltsz - 1);
4457 : 37 : HOST_WIDE_INT off = buf->off + i + start;
4458 : 37 : tree atype = type;
4459 : 37 : if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
4460 : 0 : atype = build_aligned_type (type, buf->align);
4461 : 37 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4462 : 37 : build_int_cst (buf->alias_type, off));
4463 : 37 : tree src;
4464 : 37 : gimple *g;
4465 : 37 : if (all_ones
4466 : 37 : && nonzero_first == start
4467 : 0 : && nonzero_last == start + eltsz)
4468 : 0 : src = build_zero_cst (type);
4469 : : else
4470 : : {
4471 : 37 : src = make_ssa_name (type);
4472 : 37 : tree tmp_dst = unshare_expr (dst);
4473 : : /* The folding introduces a read from the tmp_dst, we should
4474 : : prevent uninitialized warning analysis from issuing warning
4475 : : for such fake read. In order to suppress warning only for
4476 : : this expr, we should set the location of tmp_dst to
4477 : : UNKNOWN_LOCATION first, then suppress_warning will call
4478 : : set_no_warning_bit to set the no_warning flag only for
4479 : : tmp_dst. */
4480 : 37 : SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
4481 : 37 : suppress_warning (tmp_dst, OPT_Wuninitialized);
4482 : 37 : g = gimple_build_assign (src, tmp_dst);
4483 : 37 : gimple_set_location (g, buf->loc);
4484 : 37 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4485 : 74 : tree mask = native_interpret_expr (type,
4486 : 37 : buf->buf + i + start,
4487 : : eltsz);
4488 : 37 : gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
4489 : 37 : mask = fold_build1 (BIT_NOT_EXPR, type, mask);
4490 : 37 : tree src_masked = make_ssa_name (type);
4491 : 37 : g = gimple_build_assign (src_masked, BIT_AND_EXPR,
4492 : : src, mask);
4493 : 37 : gimple_set_location (g, buf->loc);
4494 : 37 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4495 : 37 : src = src_masked;
4496 : : }
4497 : 37 : g = gimple_build_assign (dst, src);
4498 : 37 : gimple_set_location (g, buf->loc);
4499 : 37 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4500 : 37 : break;
4501 : : }
4502 : : }
4503 : : }
4504 : 752 : if (full)
4505 : : {
4506 : 710 : if (padding_bytes)
4507 : : {
4508 : 483 : tree atype, src;
4509 : 483 : if (padding_bytes == 1)
4510 : : {
4511 : 108 : atype = char_type_node;
4512 : 108 : src = build_zero_cst (char_type_node);
4513 : : }
4514 : : else
4515 : : {
4516 : 375 : atype = build_array_type_nelts (char_type_node, padding_bytes);
4517 : 375 : src = build_constructor (atype, NULL);
4518 : : }
4519 : 483 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4520 : : build_int_cst (buf->alias_type,
4521 : 483 : buf->off + end
4522 : 483 : - padding_bytes));
4523 : 483 : gimple *g = gimple_build_assign (dst, src);
4524 : 483 : gimple_set_location (g, buf->loc);
4525 : 483 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4526 : : }
4527 : 710 : size_t end_rem = end % UNITS_PER_WORD;
4528 : 710 : buf->off += end - end_rem;
4529 : 710 : buf->size = end_rem;
4530 : 710 : memset (buf->buf, 0, buf->size);
4531 : 710 : buf->padding_bytes = 0;
4532 : : }
4533 : : else
4534 : : {
4535 : 42 : memmove (buf->buf, buf->buf + end, buf->size - end);
4536 : 42 : buf->off += end;
4537 : 42 : buf->size -= end;
4538 : 42 : buf->padding_bytes = padding_bytes;
4539 : : }
4540 : : }
4541 : :
4542 : : /* Append PADDING_BYTES padding bytes. */
4543 : :
4544 : : static void
4545 : 4977 : clear_padding_add_padding (clear_padding_struct *buf,
4546 : : HOST_WIDE_INT padding_bytes)
4547 : : {
4548 : 4977 : if (padding_bytes == 0)
4549 : : return;
4550 : 1667 : if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
4551 : : > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
4552 : 42 : clear_padding_flush (buf, false);
4553 : 1667 : if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
4554 : : > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
4555 : : {
4556 : 42 : memset (buf->buf + buf->size, ~0, clear_padding_buf_size - buf->size);
4557 : 42 : padding_bytes -= clear_padding_buf_size - buf->size;
4558 : 42 : buf->size = clear_padding_buf_size;
4559 : 42 : clear_padding_flush (buf, false);
4560 : 42 : gcc_assert (buf->padding_bytes);
4561 : : /* At this point buf->buf[0] through buf->buf[buf->size - 1]
4562 : : is guaranteed to be all ones. */
4563 : 42 : padding_bytes += buf->size;
4564 : 42 : buf->size = padding_bytes % UNITS_PER_WORD;
4565 : 42 : memset (buf->buf, ~0, buf->size);
4566 : 42 : buf->off += padding_bytes - buf->size;
4567 : 42 : buf->padding_bytes += padding_bytes - buf->size;
4568 : : }
4569 : : else
4570 : : {
4571 : 1625 : memset (buf->buf + buf->size, ~0, padding_bytes);
4572 : 1625 : buf->size += padding_bytes;
4573 : : }
4574 : : }
4575 : :
4576 : : static void clear_padding_type (clear_padding_struct *, tree,
4577 : : HOST_WIDE_INT, bool);
4578 : :
4579 : : /* Clear padding bits of union type TYPE. */
4580 : :
4581 : : static void
4582 : 128 : clear_padding_union (clear_padding_struct *buf, tree type,
4583 : : HOST_WIDE_INT sz, bool for_auto_init)
4584 : : {
4585 : 128 : clear_padding_struct *union_buf;
4586 : 128 : HOST_WIDE_INT start_off = 0, next_off = 0;
4587 : 128 : size_t start_size = 0;
4588 : 128 : if (buf->union_ptr)
4589 : : {
4590 : 42 : start_off = buf->off + buf->size;
4591 : 42 : next_off = start_off + sz;
4592 : 42 : start_size = start_off % UNITS_PER_WORD;
4593 : 42 : start_off -= start_size;
4594 : 42 : clear_padding_flush (buf, true);
4595 : 42 : union_buf = buf;
4596 : : }
4597 : : else
4598 : : {
4599 : 86 : if (sz + buf->size > clear_padding_buf_size)
4600 : 0 : clear_padding_flush (buf, false);
4601 : 86 : union_buf = XALLOCA (clear_padding_struct);
4602 : 86 : union_buf->loc = buf->loc;
4603 : 86 : union_buf->clear_in_mask = buf->clear_in_mask;
4604 : 86 : union_buf->base = NULL_TREE;
4605 : 86 : union_buf->alias_type = NULL_TREE;
4606 : 86 : union_buf->gsi = NULL;
4607 : 86 : union_buf->align = 0;
4608 : 86 : union_buf->off = 0;
4609 : 86 : union_buf->padding_bytes = 0;
4610 : 86 : union_buf->sz = sz;
4611 : 86 : union_buf->size = 0;
4612 : 86 : if (sz + buf->size <= clear_padding_buf_size)
4613 : 86 : union_buf->union_ptr = buf->buf + buf->size;
4614 : : else
4615 : 0 : union_buf->union_ptr = XNEWVEC (unsigned char, sz);
4616 : 86 : memset (union_buf->union_ptr, ~0, sz);
4617 : : }
4618 : :
4619 : 1193 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4620 : 1065 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4621 : : {
4622 : 359 : if (DECL_SIZE_UNIT (field) == NULL_TREE)
4623 : : {
4624 : 8 : if (TREE_TYPE (field) == error_mark_node)
4625 : 0 : continue;
4626 : 8 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4627 : : && !COMPLETE_TYPE_P (TREE_TYPE (field)));
4628 : 8 : if (!buf->clear_in_mask && !for_auto_init)
4629 : 8 : error_at (buf->loc, "flexible array member %qD does not have "
4630 : : "well defined padding bits for %qs",
4631 : : field, "__builtin_clear_padding");
4632 : 8 : continue;
4633 : : }
4634 : 351 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4635 : 351 : gcc_assert (union_buf->size == 0);
4636 : 351 : union_buf->off = start_off;
4637 : 351 : union_buf->size = start_size;
4638 : 351 : memset (union_buf->buf, ~0, start_size);
4639 : 351 : clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init);
4640 : 351 : clear_padding_add_padding (union_buf, sz - fldsz);
4641 : 351 : clear_padding_flush (union_buf, true);
4642 : : }
4643 : :
4644 : 128 : if (buf == union_buf)
4645 : : {
4646 : 42 : buf->off = next_off;
4647 : 42 : buf->size = next_off % UNITS_PER_WORD;
4648 : 42 : buf->off -= buf->size;
4649 : 42 : memset (buf->buf, ~0, buf->size);
4650 : : }
4651 : 86 : else if (sz + buf->size <= clear_padding_buf_size)
4652 : 86 : buf->size += sz;
4653 : : else
4654 : : {
4655 : 0 : unsigned char *union_ptr = union_buf->union_ptr;
4656 : 0 : while (sz)
4657 : : {
4658 : 0 : clear_padding_flush (buf, false);
4659 : 0 : HOST_WIDE_INT this_sz
4660 : 0 : = MIN ((unsigned HOST_WIDE_INT) sz,
4661 : : clear_padding_buf_size - buf->size);
4662 : 0 : memcpy (buf->buf + buf->size, union_ptr, this_sz);
4663 : 0 : buf->size += this_sz;
4664 : 0 : union_ptr += this_sz;
4665 : 0 : sz -= this_sz;
4666 : : }
4667 : 0 : XDELETE (union_buf->union_ptr);
4668 : : }
4669 : 128 : }
4670 : :
4671 : : /* The only known floating point formats with padding bits are the
4672 : : IEEE extended ones. */
4673 : :
4674 : : static bool
4675 : 31667 : clear_padding_real_needs_padding_p (tree type)
4676 : : {
4677 : 31667 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
4678 : 31667 : return (fmt->b == 2
4679 : 31348 : && fmt->signbit_ro == fmt->signbit_rw
4680 : 63015 : && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95));
4681 : : }
4682 : :
4683 : : /* _BitInt has padding bits if it isn't extended in the ABI and has smaller
4684 : : precision than bits in limb or corresponding number of limbs. */
4685 : :
4686 : : static bool
4687 : 6 : clear_padding_bitint_needs_padding_p (tree type)
4688 : : {
4689 : 6 : struct bitint_info info;
4690 : 6 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
4691 : 6 : gcc_assert (ok);
4692 : 6 : if (info.extended)
4693 : : return false;
4694 : 6 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.abi_limb_mode);
4695 : 6 : if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
4696 : : return true;
4697 : 4 : else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode))
4698 : : return false;
4699 : : else
4700 : 4 : return (((unsigned) TYPE_PRECISION (type))
4701 : 4 : % GET_MODE_PRECISION (limb_mode)) != 0;
4702 : : }
4703 : :
4704 : : /* Return true if TYPE might contain any padding bits. */
4705 : :
4706 : : bool
4707 : 921273 : clear_padding_type_may_have_padding_p (tree type)
4708 : : {
4709 : 1053949 : switch (TREE_CODE (type))
4710 : : {
4711 : : case RECORD_TYPE:
4712 : : case UNION_TYPE:
4713 : : return true;
4714 : 132676 : case ARRAY_TYPE:
4715 : 132676 : case COMPLEX_TYPE:
4716 : 132676 : case VECTOR_TYPE:
4717 : 132676 : return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
4718 : 1659 : case REAL_TYPE:
4719 : 1659 : return clear_padding_real_needs_padding_p (type);
4720 : 6 : case BITINT_TYPE:
4721 : 6 : return clear_padding_bitint_needs_padding_p (type);
4722 : 37053 : default:
4723 : 37053 : return false;
4724 : : }
4725 : : }
4726 : :
4727 : : /* Return true if TYPE has padding bits aside from those in fields,
4728 : : elements, etc. */
4729 : :
4730 : : bool
4731 : 1161569 : type_has_padding_at_level_p (tree type)
4732 : : {
4733 : 1161569 : switch (TREE_CODE (type))
4734 : : {
4735 : 1027037 : case RECORD_TYPE:
4736 : 1027037 : {
4737 : 1027037 : tree bitpos = size_zero_node;
4738 : : /* Expect fields to be sorted by bit position. */
4739 : 8028213 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4740 : 7004934 : if (TREE_CODE (f) == FIELD_DECL)
4741 : : {
4742 : 2269322 : if (DECL_PADDING_P (f))
4743 : : return true;
4744 : 2269319 : tree pos = bit_position (f);
4745 : 2269319 : if (simple_cst_equal (bitpos, pos) != 1)
4746 : : return true;
4747 : 2265584 : if (!DECL_SIZE (f))
4748 : : return true;
4749 : 2265564 : bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
4750 : : }
4751 : 1023279 : if (simple_cst_equal (bitpos, TYPE_SIZE (type)) != 1)
4752 : : return true;
4753 : : return false;
4754 : : }
4755 : 3 : case UNION_TYPE:
4756 : 3 : case QUAL_UNION_TYPE:
4757 : 3 : bool any_fields;
4758 : 3 : any_fields = false;
4759 : : /* If any of the fields is smaller than the whole, there is padding. */
4760 : 6 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4761 : 3 : if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node)
4762 : 3 : continue;
4763 : 0 : else if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
4764 : 0 : TYPE_SIZE (type)) != 1)
4765 : : return true;
4766 : : else
4767 : : any_fields = true;
4768 : : /* If the union doesn't have any fields and still has non-zero size,
4769 : : all of it is padding. */
4770 : 3 : if (!any_fields && !integer_zerop (TYPE_SIZE (type)))
4771 : : return true;
4772 : : return false;
4773 : : case ARRAY_TYPE:
4774 : : case COMPLEX_TYPE:
4775 : : case VECTOR_TYPE:
4776 : : /* No recursing here, no padding at this level. */
4777 : : return false;
4778 : 0 : case REAL_TYPE:
4779 : 0 : return clear_padding_real_needs_padding_p (type);
4780 : 0 : case BITINT_TYPE:
4781 : 0 : return clear_padding_bitint_needs_padding_p (type);
4782 : : default:
4783 : : return false;
4784 : : }
4785 : : }
4786 : :
4787 : : /* Emit a runtime loop:
4788 : : for (; buf.base != end; buf.base += sz)
4789 : : __builtin_clear_padding (buf.base); */
4790 : :
4791 : : static void
4792 : 114 : clear_padding_emit_loop (clear_padding_struct *buf, tree type,
4793 : : tree end, bool for_auto_init)
4794 : : {
4795 : 114 : tree l1 = create_artificial_label (buf->loc);
4796 : 114 : tree l2 = create_artificial_label (buf->loc);
4797 : 114 : tree l3 = create_artificial_label (buf->loc);
4798 : 114 : gimple *g = gimple_build_goto (l2);
4799 : 114 : gimple_set_location (g, buf->loc);
4800 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4801 : 114 : g = gimple_build_label (l1);
4802 : 114 : gimple_set_location (g, buf->loc);
4803 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4804 : 114 : clear_padding_type (buf, type, buf->sz, for_auto_init);
4805 : 114 : clear_padding_flush (buf, true);
4806 : 114 : g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR, buf->base,
4807 : 114 : size_int (buf->sz));
4808 : 114 : gimple_set_location (g, buf->loc);
4809 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4810 : 114 : g = gimple_build_label (l2);
4811 : 114 : gimple_set_location (g, buf->loc);
4812 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4813 : 114 : g = gimple_build_cond (NE_EXPR, buf->base, end, l1, l3);
4814 : 114 : gimple_set_location (g, buf->loc);
4815 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4816 : 114 : g = gimple_build_label (l3);
4817 : 114 : gimple_set_location (g, buf->loc);
4818 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4819 : 114 : }
4820 : :
4821 : : /* Clear padding bits for TYPE. Called recursively from
4822 : : gimple_fold_builtin_clear_padding. If FOR_AUTO_INIT is true,
4823 : : the __builtin_clear_padding is not called by the end user,
4824 : : instead, it's inserted by the compiler to initialize the
4825 : : paddings of automatic variable. Therefore, we should not
4826 : : emit the error messages for flexible array members to confuse
4827 : : the end user. */
4828 : :
4829 : : static void
4830 : 35131 : clear_padding_type (clear_padding_struct *buf, tree type,
4831 : : HOST_WIDE_INT sz, bool for_auto_init)
4832 : : {
4833 : 35131 : switch (TREE_CODE (type))
4834 : : {
4835 : 1247 : case RECORD_TYPE:
4836 : 1247 : HOST_WIDE_INT cur_pos;
4837 : 1247 : cur_pos = 0;
4838 : 16011 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4839 : 14764 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4840 : : {
4841 : 3684 : tree ftype = TREE_TYPE (field);
4842 : 3684 : if (DECL_BIT_FIELD (field))
4843 : : {
4844 : 256 : HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
4845 : 256 : if (fldsz == 0)
4846 : 0 : continue;
4847 : 256 : HOST_WIDE_INT pos = int_byte_position (field);
4848 : 256 : if (pos >= sz)
4849 : 0 : continue;
4850 : 256 : HOST_WIDE_INT bpos
4851 : 256 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
4852 : 256 : bpos %= BITS_PER_UNIT;
4853 : 256 : HOST_WIDE_INT end
4854 : 256 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4855 : 256 : if (pos + end > cur_pos)
4856 : : {
4857 : 195 : clear_padding_add_padding (buf, pos + end - cur_pos);
4858 : 195 : cur_pos = pos + end;
4859 : : }
4860 : 256 : gcc_assert (cur_pos > pos
4861 : : && ((unsigned HOST_WIDE_INT) buf->size
4862 : : >= (unsigned HOST_WIDE_INT) cur_pos - pos));
4863 : 256 : unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
4864 : 256 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4865 : : sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
4866 : : " in %qs", "__builtin_clear_padding");
4867 : 256 : else if (BYTES_BIG_ENDIAN)
4868 : : {
4869 : : /* Big endian. */
4870 : : if (bpos + fldsz <= BITS_PER_UNIT)
4871 : : *p &= ~(((1 << fldsz) - 1)
4872 : : << (BITS_PER_UNIT - bpos - fldsz));
4873 : : else
4874 : : {
4875 : : if (bpos)
4876 : : {
4877 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4878 : : p++;
4879 : : fldsz -= BITS_PER_UNIT - bpos;
4880 : : }
4881 : : memset (p, 0, fldsz / BITS_PER_UNIT);
4882 : : p += fldsz / BITS_PER_UNIT;
4883 : : fldsz %= BITS_PER_UNIT;
4884 : : if (fldsz)
4885 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4886 : : }
4887 : : }
4888 : : else
4889 : : {
4890 : : /* Little endian. */
4891 : 256 : if (bpos + fldsz <= BITS_PER_UNIT)
4892 : 159 : *p &= ~(((1 << fldsz) - 1) << bpos);
4893 : : else
4894 : : {
4895 : 97 : if (bpos)
4896 : : {
4897 : 29 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4898 : 29 : p++;
4899 : 29 : fldsz -= BITS_PER_UNIT - bpos;
4900 : : }
4901 : 97 : memset (p, 0, fldsz / BITS_PER_UNIT);
4902 : 97 : p += fldsz / BITS_PER_UNIT;
4903 : 97 : fldsz %= BITS_PER_UNIT;
4904 : 97 : if (fldsz)
4905 : 52 : *p &= ~((1 << fldsz) - 1);
4906 : : }
4907 : : }
4908 : : }
4909 : 3428 : else if (DECL_SIZE_UNIT (field) == NULL_TREE)
4910 : : {
4911 : 32 : if (ftype == error_mark_node)
4912 : 0 : continue;
4913 : 32 : gcc_assert (TREE_CODE (ftype) == ARRAY_TYPE
4914 : : && !COMPLETE_TYPE_P (ftype));
4915 : 32 : if (!buf->clear_in_mask && !for_auto_init)
4916 : 24 : error_at (buf->loc, "flexible array member %qD does not "
4917 : : "have well defined padding bits for %qs",
4918 : : field, "__builtin_clear_padding");
4919 : : }
4920 : 3396 : else if (is_empty_type (ftype))
4921 : 212 : continue;
4922 : : else
4923 : : {
4924 : 3184 : HOST_WIDE_INT pos = int_byte_position (field);
4925 : 3184 : if (pos >= sz)
4926 : 0 : continue;
4927 : 3184 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4928 : 3184 : gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
4929 : 3184 : clear_padding_add_padding (buf, pos - cur_pos);
4930 : 3184 : cur_pos = pos;
4931 : 3184 : if (tree asbase = lang_hooks.types.classtype_as_base (field))
4932 : 188 : ftype = asbase;
4933 : 3184 : clear_padding_type (buf, ftype, fldsz, for_auto_init);
4934 : 3184 : cur_pos += fldsz;
4935 : : }
4936 : : }
4937 : 1247 : gcc_assert (sz >= cur_pos);
4938 : 1247 : clear_padding_add_padding (buf, sz - cur_pos);
4939 : 1247 : break;
4940 : 325 : case ARRAY_TYPE:
4941 : 325 : HOST_WIDE_INT nelts, fldsz;
4942 : 325 : fldsz = int_size_in_bytes (TREE_TYPE (type));
4943 : 325 : if (fldsz == 0)
4944 : : break;
4945 : 311 : nelts = sz / fldsz;
4946 : 311 : if (nelts > 1
4947 : 304 : && sz > 8 * UNITS_PER_WORD
4948 : 78 : && buf->union_ptr == NULL
4949 : 389 : && clear_padding_type_may_have_padding_p (TREE_TYPE (type)))
4950 : : {
4951 : : /* For sufficiently large array of more than one elements,
4952 : : emit a runtime loop to keep code size manageable. */
4953 : 66 : tree base = buf->base;
4954 : 66 : unsigned int prev_align = buf->align;
4955 : 66 : HOST_WIDE_INT off = buf->off + buf->size;
4956 : 66 : HOST_WIDE_INT prev_sz = buf->sz;
4957 : 66 : clear_padding_flush (buf, true);
4958 : 66 : tree elttype = TREE_TYPE (type);
4959 : 66 : buf->base = create_tmp_var (build_pointer_type (elttype));
4960 : 66 : tree end = make_ssa_name (TREE_TYPE (buf->base));
4961 : 66 : gimple *g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR,
4962 : 66 : base, size_int (off));
4963 : 66 : gimple_set_location (g, buf->loc);
4964 : 66 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4965 : 66 : g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf->base,
4966 : 66 : size_int (sz));
4967 : 66 : gimple_set_location (g, buf->loc);
4968 : 66 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4969 : 66 : buf->sz = fldsz;
4970 : 66 : buf->align = TYPE_ALIGN (elttype);
4971 : 66 : buf->off = 0;
4972 : 66 : buf->size = 0;
4973 : 66 : clear_padding_emit_loop (buf, elttype, end, for_auto_init);
4974 : 66 : off += sz;
4975 : 66 : buf->base = base;
4976 : 66 : buf->sz = prev_sz;
4977 : 66 : buf->align = prev_align;
4978 : 66 : buf->size = off % UNITS_PER_WORD;
4979 : 66 : buf->off = off - buf->size;
4980 : 66 : memset (buf->buf, 0, buf->size);
4981 : 66 : break;
4982 : : }
4983 : 1163 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
4984 : 918 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
4985 : : break;
4986 : 128 : case UNION_TYPE:
4987 : 128 : clear_padding_union (buf, type, sz, for_auto_init);
4988 : 128 : break;
4989 : 30008 : case REAL_TYPE:
4990 : 30008 : gcc_assert ((size_t) sz <= clear_padding_unit);
4991 : 30008 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
4992 : 0 : clear_padding_flush (buf, false);
4993 : 30008 : if (clear_padding_real_needs_padding_p (type))
4994 : : {
4995 : : /* Use native_interpret_real + native_encode_expr to figure out
4996 : : which bits are padding. */
4997 : 1214 : memset (buf->buf + buf->size, ~0, sz);
4998 : 1214 : tree cst = native_interpret_real (type, buf->buf + buf->size, sz);
4999 : 1214 : gcc_assert (cst && TREE_CODE (cst) == REAL_CST);
5000 : 1214 : int len = native_encode_expr (cst, buf->buf + buf->size, sz);
5001 : 1214 : gcc_assert (len > 0 && (size_t) len == (size_t) sz);
5002 : 20638 : for (size_t i = 0; i < (size_t) sz; i++)
5003 : 19424 : buf->buf[buf->size + i] ^= ~0;
5004 : : }
5005 : : else
5006 : 28794 : memset (buf->buf + buf->size, 0, sz);
5007 : 30008 : buf->size += sz;
5008 : 30008 : break;
5009 : 0 : case COMPLEX_TYPE:
5010 : 0 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5011 : 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5012 : 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5013 : 0 : break;
5014 : 0 : case VECTOR_TYPE:
5015 : 0 : nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
5016 : 0 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5017 : 0 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5018 : 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5019 : : break;
5020 : 7 : case NULLPTR_TYPE:
5021 : 7 : gcc_assert ((size_t) sz <= clear_padding_unit);
5022 : 7 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5023 : 0 : clear_padding_flush (buf, false);
5024 : 7 : memset (buf->buf + buf->size, ~0, sz);
5025 : 7 : buf->size += sz;
5026 : 7 : break;
5027 : 4 : case BITINT_TYPE:
5028 : 4 : {
5029 : 4 : struct bitint_info info;
5030 : 4 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
5031 : 4 : gcc_assert (ok);
5032 : 4 : scalar_int_mode limb_mode
5033 : 4 : = as_a <scalar_int_mode> (info.abi_limb_mode);
5034 : 4 : if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
5035 : : {
5036 : 2 : gcc_assert ((size_t) sz <= clear_padding_unit);
5037 : 2 : if ((unsigned HOST_WIDE_INT) sz + buf->size
5038 : : > clear_padding_buf_size)
5039 : 0 : clear_padding_flush (buf, false);
5040 : 2 : if (!info.extended
5041 : 2 : && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
5042 : : {
5043 : 2 : int tprec = GET_MODE_PRECISION (limb_mode);
5044 : 2 : int prec = TYPE_PRECISION (type);
5045 : 2 : tree t = build_nonstandard_integer_type (tprec, 1);
5046 : 2 : tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec));
5047 : 2 : int len = native_encode_expr (cst, buf->buf + buf->size, sz);
5048 : 2 : gcc_assert (len > 0 && (size_t) len == (size_t) sz);
5049 : : }
5050 : : else
5051 : 0 : memset (buf->buf + buf->size, 0, sz);
5052 : 2 : buf->size += sz;
5053 : 2 : break;
5054 : : }
5055 : 2 : tree limbtype
5056 : 2 : = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1);
5057 : 2 : fldsz = int_size_in_bytes (limbtype);
5058 : 2 : nelts = int_size_in_bytes (type) / fldsz;
5059 : 13 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5060 : : {
5061 : 11 : if (!info.extended
5062 : 11 : && i == (info.big_endian ? 0 : nelts - 1)
5063 : 13 : && (((unsigned) TYPE_PRECISION (type))
5064 : 2 : % TYPE_PRECISION (limbtype)) != 0)
5065 : : {
5066 : 2 : int tprec = GET_MODE_PRECISION (limb_mode);
5067 : 2 : int prec = (((unsigned) TYPE_PRECISION (type)) % tprec);
5068 : 2 : tree cst = wide_int_to_tree (limbtype,
5069 : 2 : wi::mask (prec, true, tprec));
5070 : 2 : int len = native_encode_expr (cst, buf->buf + buf->size,
5071 : : fldsz);
5072 : 2 : gcc_assert (len > 0 && (size_t) len == (size_t) fldsz);
5073 : 2 : buf->size += fldsz;
5074 : : }
5075 : : else
5076 : 9 : clear_padding_type (buf, limbtype, fldsz, for_auto_init);
5077 : : }
5078 : : break;
5079 : : }
5080 : 3412 : default:
5081 : 3412 : gcc_assert ((size_t) sz <= clear_padding_unit);
5082 : 3412 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5083 : 0 : clear_padding_flush (buf, false);
5084 : 3412 : memset (buf->buf + buf->size, 0, sz);
5085 : 3412 : buf->size += sz;
5086 : 3412 : break;
5087 : : }
5088 : 35131 : }
5089 : :
5090 : : /* Clear padding bits of TYPE in MASK. */
5091 : :
5092 : : void
5093 : 30025 : clear_type_padding_in_mask (tree type, unsigned char *mask)
5094 : : {
5095 : 30025 : clear_padding_struct buf;
5096 : 30025 : buf.loc = UNKNOWN_LOCATION;
5097 : 30025 : buf.clear_in_mask = true;
5098 : 30025 : buf.base = NULL_TREE;
5099 : 30025 : buf.alias_type = NULL_TREE;
5100 : 30025 : buf.gsi = NULL;
5101 : 30025 : buf.align = 0;
5102 : 30025 : buf.off = 0;
5103 : 30025 : buf.padding_bytes = 0;
5104 : 30025 : buf.sz = int_size_in_bytes (type);
5105 : 30025 : buf.size = 0;
5106 : 30025 : buf.union_ptr = mask;
5107 : 30025 : clear_padding_type (&buf, type, buf.sz, false);
5108 : 30025 : clear_padding_flush (&buf, true);
5109 : 30025 : }
5110 : :
5111 : : /* Fold __builtin_clear_padding builtin. */
5112 : :
5113 : : static bool
5114 : 616 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
5115 : : {
5116 : 616 : gimple *stmt = gsi_stmt (*gsi);
5117 : 616 : gcc_assert (gimple_call_num_args (stmt) == 2);
5118 : 616 : tree ptr = gimple_call_arg (stmt, 0);
5119 : 616 : tree typearg = gimple_call_arg (stmt, 1);
5120 : : /* The 2nd argument of __builtin_clear_padding's value is used to
5121 : : distinguish whether this call is made by the user or by the compiler
5122 : : for automatic variable initialization. */
5123 : 616 : bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
5124 : 616 : tree type = TREE_TYPE (TREE_TYPE (typearg));
5125 : 616 : location_t loc = gimple_location (stmt);
5126 : 616 : clear_padding_struct buf;
5127 : 616 : gimple_stmt_iterator gsiprev = *gsi;
5128 : : /* This should be folded during the lower pass. */
5129 : 1232 : gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
5130 : 616 : gcc_assert (COMPLETE_TYPE_P (type));
5131 : 616 : gsi_prev (&gsiprev);
5132 : :
5133 : 616 : buf.loc = loc;
5134 : 616 : buf.clear_in_mask = false;
5135 : 616 : buf.base = ptr;
5136 : 616 : buf.alias_type = NULL_TREE;
5137 : 616 : buf.gsi = gsi;
5138 : 616 : buf.align = get_pointer_alignment (ptr);
5139 : 616 : unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
5140 : 616 : buf.align = MAX (buf.align, talign);
5141 : 616 : buf.off = 0;
5142 : 616 : buf.padding_bytes = 0;
5143 : 616 : buf.size = 0;
5144 : 616 : buf.sz = int_size_in_bytes (type);
5145 : 616 : buf.union_ptr = NULL;
5146 : 616 : if (buf.sz < 0 && int_size_in_bytes (strip_array_types (type)) < 0)
5147 : 1 : sorry_at (loc, "%s not supported for variable length aggregates",
5148 : : "__builtin_clear_padding");
5149 : : /* The implementation currently assumes 8-bit host and target
5150 : : chars which is the case for all currently supported targets
5151 : : and hosts and is required e.g. for native_{encode,interpret}* APIs. */
5152 : 615 : else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
5153 : : sorry_at (loc, "%s not supported on this target",
5154 : : "__builtin_clear_padding");
5155 : 615 : else if (!clear_padding_type_may_have_padding_p (type))
5156 : : ;
5157 : 578 : else if (TREE_CODE (type) == ARRAY_TYPE && buf.sz < 0)
5158 : : {
5159 : 48 : tree sz = TYPE_SIZE_UNIT (type);
5160 : 48 : tree elttype = type;
5161 : : /* Only supports C/C++ VLAs and flattens all the VLA levels. */
5162 : 48 : while (TREE_CODE (elttype) == ARRAY_TYPE
5163 : 144 : && int_size_in_bytes (elttype) < 0)
5164 : 96 : elttype = TREE_TYPE (elttype);
5165 : 48 : HOST_WIDE_INT eltsz = int_size_in_bytes (elttype);
5166 : 48 : gcc_assert (eltsz >= 0);
5167 : 48 : if (eltsz)
5168 : : {
5169 : 48 : buf.base = create_tmp_var (build_pointer_type (elttype));
5170 : 48 : tree end = make_ssa_name (TREE_TYPE (buf.base));
5171 : 48 : gimple *g = gimple_build_assign (buf.base, ptr);
5172 : 48 : gimple_set_location (g, loc);
5173 : 48 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5174 : 48 : g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf.base, sz);
5175 : 48 : gimple_set_location (g, loc);
5176 : 48 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5177 : 48 : buf.sz = eltsz;
5178 : 48 : buf.align = TYPE_ALIGN (elttype);
5179 : 48 : buf.alias_type = build_pointer_type (elttype);
5180 : 48 : clear_padding_emit_loop (&buf, elttype, end, for_auto_init);
5181 : : }
5182 : : }
5183 : : else
5184 : : {
5185 : 530 : if (!is_gimple_mem_ref_addr (buf.base))
5186 : : {
5187 : 28 : buf.base = make_ssa_name (TREE_TYPE (ptr));
5188 : 28 : gimple *g = gimple_build_assign (buf.base, ptr);
5189 : 28 : gimple_set_location (g, loc);
5190 : 28 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5191 : : }
5192 : 530 : buf.alias_type = build_pointer_type (type);
5193 : 530 : clear_padding_type (&buf, type, buf.sz, for_auto_init);
5194 : 530 : clear_padding_flush (&buf, true);
5195 : : }
5196 : :
5197 : 616 : gimple_stmt_iterator gsiprev2 = *gsi;
5198 : 616 : gsi_prev (&gsiprev2);
5199 : 616 : if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
5200 : 122 : gsi_replace (gsi, gimple_build_nop (), true);
5201 : : else
5202 : : {
5203 : 494 : gsi_remove (gsi, true);
5204 : 494 : *gsi = gsiprev2;
5205 : : }
5206 : 616 : return true;
5207 : : }
5208 : :
5209 : : /* Fold the non-target builtin at *GSI and return whether any simplification
5210 : : was made. */
5211 : :
5212 : : static bool
5213 : 13493480 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
5214 : : {
5215 : 13493480 : gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
5216 : 13493480 : tree callee = gimple_call_fndecl (stmt);
5217 : :
5218 : : /* Give up for always_inline inline builtins until they are
5219 : : inlined. */
5220 : 13493480 : if (avoid_folding_inline_builtin (callee))
5221 : : return false;
5222 : :
5223 : 13492304 : unsigned n = gimple_call_num_args (stmt);
5224 : 13492304 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
5225 : 13492304 : switch (fcode)
5226 : : {
5227 : 148 : case BUILT_IN_BCMP:
5228 : 148 : return gimple_fold_builtin_bcmp (gsi);
5229 : 367 : case BUILT_IN_BCOPY:
5230 : 367 : return gimple_fold_builtin_bcopy (gsi);
5231 : 250 : case BUILT_IN_BZERO:
5232 : 250 : return gimple_fold_builtin_bzero (gsi);
5233 : :
5234 : 334881 : case BUILT_IN_MEMSET:
5235 : 334881 : return gimple_fold_builtin_memset (gsi,
5236 : : gimple_call_arg (stmt, 1),
5237 : 334881 : gimple_call_arg (stmt, 2));
5238 : 975359 : case BUILT_IN_MEMCPY:
5239 : 975359 : case BUILT_IN_MEMPCPY:
5240 : 975359 : case BUILT_IN_MEMMOVE:
5241 : 975359 : return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
5242 : 975359 : gimple_call_arg (stmt, 1), fcode);
5243 : 5105 : case BUILT_IN_SPRINTF_CHK:
5244 : 5105 : case BUILT_IN_VSPRINTF_CHK:
5245 : 5105 : return gimple_fold_builtin_sprintf_chk (gsi, fcode);
5246 : 1854 : case BUILT_IN_STRCAT_CHK:
5247 : 1854 : return gimple_fold_builtin_strcat_chk (gsi);
5248 : 1225 : case BUILT_IN_STRNCAT_CHK:
5249 : 1225 : return gimple_fold_builtin_strncat_chk (gsi);
5250 : 158558 : case BUILT_IN_STRLEN:
5251 : 158558 : return gimple_fold_builtin_strlen (gsi);
5252 : 27758 : case BUILT_IN_STRCPY:
5253 : 27758 : return gimple_fold_builtin_strcpy (gsi,
5254 : : gimple_call_arg (stmt, 0),
5255 : 27758 : gimple_call_arg (stmt, 1));
5256 : 19137 : case BUILT_IN_STRNCPY:
5257 : 19137 : return gimple_fold_builtin_strncpy (gsi,
5258 : : gimple_call_arg (stmt, 0),
5259 : : gimple_call_arg (stmt, 1),
5260 : 19137 : gimple_call_arg (stmt, 2));
5261 : 8040 : case BUILT_IN_STRCAT:
5262 : 8040 : return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
5263 : 8040 : gimple_call_arg (stmt, 1));
5264 : 7557 : case BUILT_IN_STRNCAT:
5265 : 7557 : return gimple_fold_builtin_strncat (gsi);
5266 : 5658 : case BUILT_IN_INDEX:
5267 : 5658 : case BUILT_IN_STRCHR:
5268 : 5658 : return gimple_fold_builtin_strchr (gsi, false);
5269 : 792 : case BUILT_IN_RINDEX:
5270 : 792 : case BUILT_IN_STRRCHR:
5271 : 792 : return gimple_fold_builtin_strchr (gsi, true);
5272 : 4631 : case BUILT_IN_STRSTR:
5273 : 4631 : return gimple_fold_builtin_strstr (gsi);
5274 : 1377599 : case BUILT_IN_STRCMP:
5275 : 1377599 : case BUILT_IN_STRCMP_EQ:
5276 : 1377599 : case BUILT_IN_STRCASECMP:
5277 : 1377599 : case BUILT_IN_STRNCMP:
5278 : 1377599 : case BUILT_IN_STRNCMP_EQ:
5279 : 1377599 : case BUILT_IN_STRNCASECMP:
5280 : 1377599 : return gimple_fold_builtin_string_compare (gsi);
5281 : 31265 : case BUILT_IN_MEMCHR:
5282 : 31265 : return gimple_fold_builtin_memchr (gsi);
5283 : 21953 : case BUILT_IN_FPUTS:
5284 : 21953 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5285 : 21953 : gimple_call_arg (stmt, 1), false);
5286 : 44 : case BUILT_IN_FPUTS_UNLOCKED:
5287 : 44 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5288 : 44 : gimple_call_arg (stmt, 1), true);
5289 : 26060 : case BUILT_IN_MEMCPY_CHK:
5290 : 26060 : case BUILT_IN_MEMPCPY_CHK:
5291 : 26060 : case BUILT_IN_MEMMOVE_CHK:
5292 : 26060 : case BUILT_IN_MEMSET_CHK:
5293 : 26060 : return gimple_fold_builtin_memory_chk (gsi,
5294 : : gimple_call_arg (stmt, 0),
5295 : : gimple_call_arg (stmt, 1),
5296 : : gimple_call_arg (stmt, 2),
5297 : : gimple_call_arg (stmt, 3),
5298 : 26060 : fcode);
5299 : 4111 : case BUILT_IN_STPCPY:
5300 : 4111 : return gimple_fold_builtin_stpcpy (gsi);
5301 : 2797 : case BUILT_IN_STRCPY_CHK:
5302 : 2797 : case BUILT_IN_STPCPY_CHK:
5303 : 2797 : return gimple_fold_builtin_stxcpy_chk (gsi,
5304 : : gimple_call_arg (stmt, 0),
5305 : : gimple_call_arg (stmt, 1),
5306 : : gimple_call_arg (stmt, 2),
5307 : 2797 : fcode);
5308 : 2881 : case BUILT_IN_STRNCPY_CHK:
5309 : 2881 : case BUILT_IN_STPNCPY_CHK:
5310 : 2881 : return gimple_fold_builtin_stxncpy_chk (gsi,
5311 : : gimple_call_arg (stmt, 0),
5312 : : gimple_call_arg (stmt, 1),
5313 : : gimple_call_arg (stmt, 2),
5314 : : gimple_call_arg (stmt, 3),
5315 : 2881 : fcode);
5316 : 2917 : case BUILT_IN_SNPRINTF_CHK:
5317 : 2917 : case BUILT_IN_VSNPRINTF_CHK:
5318 : 2917 : return gimple_fold_builtin_snprintf_chk (gsi, fcode);
5319 : :
5320 : 902530 : case BUILT_IN_FPRINTF:
5321 : 902530 : case BUILT_IN_FPRINTF_UNLOCKED:
5322 : 902530 : case BUILT_IN_VFPRINTF:
5323 : 902530 : if (n == 2 || n == 3)
5324 : 104394 : return gimple_fold_builtin_fprintf (gsi,
5325 : : gimple_call_arg (stmt, 0),
5326 : : gimple_call_arg (stmt, 1),
5327 : : n == 3
5328 : 47248 : ? gimple_call_arg (stmt, 2)
5329 : : : NULL_TREE,
5330 : 57146 : fcode);
5331 : : break;
5332 : 2514 : case BUILT_IN_FPRINTF_CHK:
5333 : 2514 : case BUILT_IN_VFPRINTF_CHK:
5334 : 2514 : if (n == 3 || n == 4)
5335 : 4303 : return gimple_fold_builtin_fprintf (gsi,
5336 : : gimple_call_arg (stmt, 0),
5337 : : gimple_call_arg (stmt, 2),
5338 : : n == 4
5339 : 1971 : ? gimple_call_arg (stmt, 3)
5340 : : : NULL_TREE,
5341 : 2332 : fcode);
5342 : : break;
5343 : 203923 : case BUILT_IN_PRINTF:
5344 : 203923 : case BUILT_IN_PRINTF_UNLOCKED:
5345 : 203923 : case BUILT_IN_VPRINTF:
5346 : 203923 : if (n == 1 || n == 2)
5347 : 229388 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
5348 : : n == 2
5349 : 109929 : ? gimple_call_arg (stmt, 1)
5350 : 119459 : : NULL_TREE, fcode);
5351 : : break;
5352 : 2571 : case BUILT_IN_PRINTF_CHK:
5353 : 2571 : case BUILT_IN_VPRINTF_CHK:
5354 : 2571 : if (n == 2 || n == 3)
5355 : 4401 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
5356 : : n == 3
5357 : 2003 : ? gimple_call_arg (stmt, 2)
5358 : 2398 : : NULL_TREE, fcode);
5359 : : break;
5360 : 2866 : case BUILT_IN_ACC_ON_DEVICE:
5361 : 2866 : return gimple_fold_builtin_acc_on_device (gsi,
5362 : 2866 : gimple_call_arg (stmt, 0));
5363 : 206 : case BUILT_IN_OMP_IS_INITIAL_DEVICE:
5364 : 206 : return gimple_fold_builtin_omp_is_initial_device (gsi);
5365 : :
5366 : 98 : case BUILT_IN_OMP_GET_INITIAL_DEVICE:
5367 : 98 : return gimple_fold_builtin_omp_get_initial_device (gsi);
5368 : :
5369 : 265 : case BUILT_IN_OMP_GET_NUM_DEVICES:
5370 : 265 : return gimple_fold_builtin_omp_get_num_devices (gsi);
5371 : :
5372 : 48134 : case BUILT_IN_REALLOC:
5373 : 48134 : return gimple_fold_builtin_realloc (gsi);
5374 : :
5375 : 616 : case BUILT_IN_CLEAR_PADDING:
5376 : 616 : return gimple_fold_builtin_clear_padding (gsi);
5377 : :
5378 : 10237837 : default:;
5379 : : }
5380 : :
5381 : : /* Try the generic builtin folder. */
5382 : 10237837 : bool ignore = (gimple_call_lhs (stmt) == NULL);
5383 : 10237837 : tree result = fold_call_stmt (stmt, ignore);
5384 : 10237837 : if (result)
5385 : : {
5386 : 5730 : if (ignore)
5387 : 1204 : STRIP_NOPS (result);
5388 : : else
5389 : 4526 : result = fold_convert (gimple_call_return_type (stmt), result);
5390 : 5730 : gimplify_and_update_call_from_tree (gsi, result);
5391 : 5730 : return true;
5392 : : }
5393 : :
5394 : : return false;
5395 : : }
5396 : :
5397 : : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
5398 : : function calls to constants, where possible. */
5399 : :
5400 : : static tree
5401 : 19736 : fold_internal_goacc_dim (const gimple *call)
5402 : : {
5403 : 19736 : int axis = oacc_get_ifn_dim_arg (call);
5404 : 19736 : int size = oacc_get_fn_dim_size (current_function_decl, axis);
5405 : 19736 : tree result = NULL_TREE;
5406 : 19736 : tree type = TREE_TYPE (gimple_call_lhs (call));
5407 : :
5408 : 19736 : switch (gimple_call_internal_fn (call))
5409 : : {
5410 : 8576 : case IFN_GOACC_DIM_POS:
5411 : : /* If the size is 1, we know the answer. */
5412 : 8576 : if (size == 1)
5413 : 8576 : result = build_int_cst (type, 0);
5414 : : break;
5415 : 11160 : case IFN_GOACC_DIM_SIZE:
5416 : : /* If the size is not dynamic, we know the answer. */
5417 : 11160 : if (size)
5418 : 11160 : result = build_int_cst (type, size);
5419 : : break;
5420 : : default:
5421 : : break;
5422 : : }
5423 : :
5424 : 19736 : return result;
5425 : : }
5426 : :
5427 : : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
5428 : : for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
5429 : : &var where var is only addressable because of such calls. */
5430 : :
5431 : : bool
5432 : 58236607 : optimize_atomic_compare_exchange_p (gimple *stmt)
5433 : : {
5434 : 58236607 : if (gimple_call_num_args (stmt) != 6
5435 : 1637899 : || !flag_inline_atomics
5436 : 1637899 : || !optimize
5437 : 1637899 : || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
5438 : 1637838 : || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
5439 : 1069816 : || !gimple_vdef (stmt)
5440 : 59192785 : || !gimple_vuse (stmt))
5441 : 57280429 : return false;
5442 : :
5443 : 956178 : tree fndecl = gimple_call_fndecl (stmt);
5444 : 956178 : switch (DECL_FUNCTION_CODE (fndecl))
5445 : : {
5446 : 52964 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
5447 : 52964 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
5448 : 52964 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
5449 : 52964 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
5450 : 52964 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
5451 : 52964 : break;
5452 : : default:
5453 : : return false;
5454 : : }
5455 : :
5456 : 52964 : tree expected = gimple_call_arg (stmt, 1);
5457 : 52964 : if (TREE_CODE (expected) != ADDR_EXPR
5458 : 52964 : || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
5459 : : return false;
5460 : :
5461 : 50460 : tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
5462 : 50460 : if (!is_gimple_reg_type (etype)
5463 : 50074 : || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
5464 : 47690 : || TREE_THIS_VOLATILE (etype)
5465 : 47690 : || VECTOR_TYPE_P (etype)
5466 : : || TREE_CODE (etype) == COMPLEX_TYPE
5467 : : /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
5468 : : might not preserve all the bits. See PR71716. */
5469 : : || SCALAR_FLOAT_TYPE_P (etype)
5470 : 68674 : || maybe_ne (TYPE_PRECISION (etype),
5471 : 36428 : GET_MODE_BITSIZE (TYPE_MODE (etype))))
5472 : 38596 : return false;
5473 : :
5474 : 11864 : tree weak = gimple_call_arg (stmt, 3);
5475 : 11864 : if (!integer_zerop (weak) && !integer_onep (weak))
5476 : : return false;
5477 : :
5478 : 11864 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5479 : 11864 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5480 : 11864 : machine_mode mode = TYPE_MODE (itype);
5481 : :
5482 : 11864 : if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
5483 : : == CODE_FOR_nothing
5484 : 11864 : && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
5485 : : return false;
5486 : :
5487 : 23728 : if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
5488 : : return false;
5489 : :
5490 : : return true;
5491 : : }
5492 : :
5493 : : /* Fold
5494 : : r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
5495 : : into
5496 : : _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
5497 : : i = IMAGPART_EXPR <t>;
5498 : : r = (_Bool) i;
5499 : : e = REALPART_EXPR <t>; */
5500 : :
5501 : : void
5502 : 5825 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
5503 : : {
5504 : 5825 : gimple *stmt = gsi_stmt (*gsi);
5505 : 5825 : tree fndecl = gimple_call_fndecl (stmt);
5506 : 5825 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5507 : 5825 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5508 : 5825 : tree ctype = build_complex_type (itype);
5509 : 5825 : tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
5510 : 5825 : bool throws = false;
5511 : 5825 : edge e = NULL;
5512 : 5825 : gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5513 : : expected);
5514 : 5825 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5515 : 5825 : gimple_stmt_iterator gsiret = gsi_for_stmt (g);
5516 : 5825 : if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
5517 : : {
5518 : 2634 : g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
5519 : : build1 (VIEW_CONVERT_EXPR, itype,
5520 : : gimple_assign_lhs (g)));
5521 : 2634 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5522 : : }
5523 : 5825 : int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
5524 : 11193 : + int_size_in_bytes (itype);
5525 : 5825 : g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
5526 : : gimple_call_arg (stmt, 0),
5527 : : gimple_assign_lhs (g),
5528 : : gimple_call_arg (stmt, 2),
5529 : 5825 : build_int_cst (integer_type_node, flag),
5530 : : gimple_call_arg (stmt, 4),
5531 : : gimple_call_arg (stmt, 5));
5532 : 5825 : tree lhs = make_ssa_name (ctype);
5533 : 5825 : gimple_call_set_lhs (g, lhs);
5534 : 5825 : gimple_move_vops (g, stmt);
5535 : 5825 : tree oldlhs = gimple_call_lhs (stmt);
5536 : 5825 : if (stmt_can_throw_internal (cfun, stmt))
5537 : : {
5538 : 203 : throws = true;
5539 : 203 : e = find_fallthru_edge (gsi_bb (*gsi)->succs);
5540 : : }
5541 : 5825 : gimple_call_set_nothrow (as_a <gcall *> (g),
5542 : 5825 : gimple_call_nothrow_p (as_a <gcall *> (stmt)));
5543 : 5825 : gimple_call_set_lhs (stmt, NULL_TREE);
5544 : 5825 : gsi_replace (gsi, g, true);
5545 : 5825 : if (oldlhs)
5546 : : {
5547 : 5778 : g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
5548 : : build1 (IMAGPART_EXPR, itype, lhs));
5549 : 5778 : if (throws)
5550 : : {
5551 : 197 : gsi_insert_on_edge_immediate (e, g);
5552 : 197 : *gsi = gsi_for_stmt (g);
5553 : : }
5554 : : else
5555 : 5581 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5556 : 5778 : g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
5557 : 5778 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5558 : : }
5559 : 5825 : g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
5560 : : build1 (REALPART_EXPR, itype, lhs));
5561 : 5825 : if (throws && oldlhs == NULL_TREE)
5562 : : {
5563 : 6 : gsi_insert_on_edge_immediate (e, g);
5564 : 6 : *gsi = gsi_for_stmt (g);
5565 : : }
5566 : : else
5567 : 5819 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5568 : 5825 : if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
5569 : : {
5570 : 5268 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5571 : : VIEW_CONVERT_EXPR,
5572 : 2634 : build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
5573 : : gimple_assign_lhs (g)));
5574 : 2634 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5575 : : }
5576 : 5825 : g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
5577 : 5825 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5578 : 5825 : *gsi = gsiret;
5579 : 5825 : }
5580 : :
5581 : : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
5582 : : doesn't fit into TYPE. The test for overflow should be regardless of
5583 : : -fwrapv, and even for unsigned types. */
5584 : :
5585 : : bool
5586 : 370460 : arith_overflowed_p (enum tree_code code, const_tree type,
5587 : : const_tree arg0, const_tree arg1)
5588 : : {
5589 : 370460 : widest2_int warg0 = widest2_int_cst (arg0);
5590 : 370460 : widest2_int warg1 = widest2_int_cst (arg1);
5591 : 370460 : widest2_int wres;
5592 : 370460 : switch (code)
5593 : : {
5594 : 97344 : case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
5595 : 115075 : case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
5596 : 159220 : case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
5597 : 0 : default: gcc_unreachable ();
5598 : : }
5599 : 370460 : signop sign = TYPE_SIGN (type);
5600 : 370460 : if (sign == UNSIGNED && wi::neg_p (wres))
5601 : : return true;
5602 : 298634 : return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
5603 : 370472 : }
5604 : :
5605 : : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional,
5606 : : return a MEM_REF for the memory it references, otherwise return null.
5607 : : VECTYPE is the type of the memory vector. MASK_P indicates it's for
5608 : : MASK if true, otherwise it's for LEN. */
5609 : :
5610 : : static tree
5611 : 2964 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype, bool mask_p)
5612 : : {
5613 : 2964 : tree ptr = gimple_call_arg (call, 0);
5614 : 2964 : tree alias_align = gimple_call_arg (call, 1);
5615 : 2964 : if (!tree_fits_uhwi_p (alias_align))
5616 : : return NULL_TREE;
5617 : :
5618 : 2964 : if (mask_p)
5619 : : {
5620 : 2964 : tree mask = gimple_call_arg (call, 2);
5621 : 2964 : if (!integer_all_onesp (mask))
5622 : : return NULL_TREE;
5623 : : }
5624 : : else
5625 : : {
5626 : 0 : internal_fn ifn = gimple_call_internal_fn (call);
5627 : 0 : int len_index = internal_fn_len_index (ifn);
5628 : 0 : tree basic_len = gimple_call_arg (call, len_index);
5629 : 0 : if (!poly_int_tree_p (basic_len))
5630 : : return NULL_TREE;
5631 : 0 : tree bias = gimple_call_arg (call, len_index + 1);
5632 : 0 : gcc_assert (TREE_CODE (bias) == INTEGER_CST);
5633 : : /* For LEN_LOAD/LEN_STORE/MASK_LEN_LOAD/MASK_LEN_STORE,
5634 : : we don't fold when (bias + len) != VF. */
5635 : 0 : if (maybe_ne (wi::to_poly_widest (basic_len) + wi::to_widest (bias),
5636 : 0 : GET_MODE_NUNITS (TYPE_MODE (vectype))))
5637 : : return NULL_TREE;
5638 : :
5639 : : /* For MASK_LEN_{LOAD,STORE}, we should also check whether
5640 : : the mask is all ones mask. */
5641 : 0 : if (ifn == IFN_MASK_LEN_LOAD || ifn == IFN_MASK_LEN_STORE)
5642 : : {
5643 : 0 : tree mask = gimple_call_arg (call, internal_fn_mask_index (ifn));
5644 : 0 : if (!integer_all_onesp (mask))
5645 : : return NULL_TREE;
5646 : : }
5647 : : }
5648 : :
5649 : 31 : unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
5650 : 31 : if (TYPE_ALIGN (vectype) != align)
5651 : 14 : vectype = build_aligned_type (vectype, align);
5652 : 31 : tree offset = build_zero_cst (TREE_TYPE (alias_align));
5653 : 31 : return fold_build2 (MEM_REF, vectype, ptr, offset);
5654 : : }
5655 : :
5656 : : /* Try to fold IFN_{MASK,LEN}_LOAD call CALL. Return true on success.
5657 : : MASK_P indicates it's for MASK if true, otherwise it's for LEN. */
5658 : :
5659 : : static bool
5660 : 1530 : gimple_fold_partial_load (gimple_stmt_iterator *gsi, gcall *call, bool mask_p)
5661 : : {
5662 : 1530 : tree lhs = gimple_call_lhs (call);
5663 : 1530 : if (!lhs)
5664 : : return false;
5665 : :
5666 : 3060 : if (tree rhs
5667 : 1530 : = gimple_fold_partial_load_store_mem_ref (call, TREE_TYPE (lhs), mask_p))
5668 : : {
5669 : 17 : gassign *new_stmt = gimple_build_assign (lhs, rhs);
5670 : 17 : gimple_set_location (new_stmt, gimple_location (call));
5671 : 17 : gimple_move_vops (new_stmt, call);
5672 : 17 : gsi_replace (gsi, new_stmt, false);
5673 : 17 : return true;
5674 : : }
5675 : : return false;
5676 : : }
5677 : :
5678 : : /* Try to fold IFN_{MASK,LEN}_STORE call CALL. Return true on success.
5679 : : MASK_P indicates it's for MASK if true, otherwise it's for LEN. */
5680 : :
5681 : : static bool
5682 : 1434 : gimple_fold_partial_store (gimple_stmt_iterator *gsi, gcall *call,
5683 : : bool mask_p)
5684 : : {
5685 : 1434 : internal_fn ifn = gimple_call_internal_fn (call);
5686 : 1434 : tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
5687 : 2868 : if (tree lhs
5688 : 1434 : = gimple_fold_partial_load_store_mem_ref (call, TREE_TYPE (rhs), mask_p))
5689 : : {
5690 : 14 : gassign *new_stmt = gimple_build_assign (lhs, rhs);
5691 : 14 : gimple_set_location (new_stmt, gimple_location (call));
5692 : 14 : gimple_move_vops (new_stmt, call);
5693 : 14 : gsi_replace (gsi, new_stmt, false);
5694 : 14 : return true;
5695 : : }
5696 : : return false;
5697 : : }
5698 : :
5699 : : /* Attempt to fold a call statement referenced by the statement iterator GSI.
5700 : : The statement may be replaced by another statement, e.g., if the call
5701 : : simplifies to a constant value. Return true if any changes were made.
5702 : : It is assumed that the operands have been previously folded. */
5703 : :
5704 : : static bool
5705 : 55858548 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
5706 : : {
5707 : 55858548 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
5708 : 55858548 : tree callee;
5709 : 55858548 : bool changed = false;
5710 : :
5711 : : /* Check for virtual calls that became direct calls. */
5712 : 55858548 : callee = gimple_call_fn (stmt);
5713 : 55858548 : if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
5714 : : {
5715 : 492317 : if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
5716 : : {
5717 : 6 : if (dump_file && virtual_method_call_p (callee)
5718 : 377 : && !possible_polymorphic_call_target_p
5719 : 6 : (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
5720 : 6 : (OBJ_TYPE_REF_EXPR (callee)))))
5721 : : {
5722 : 0 : fprintf (dump_file,
5723 : : "Type inheritance inconsistent devirtualization of ");
5724 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5725 : 0 : fprintf (dump_file, " to ");
5726 : 0 : print_generic_expr (dump_file, callee, TDF_SLIM);
5727 : 0 : fprintf (dump_file, "\n");
5728 : : }
5729 : :
5730 : 371 : gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
5731 : 371 : changed = true;
5732 : : }
5733 : 491946 : else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
5734 : : {
5735 : 487169 : bool final;
5736 : 487169 : vec <cgraph_node *>targets
5737 : 487169 : = possible_polymorphic_call_targets (callee, stmt, &final);
5738 : 489795 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
5739 : : {
5740 : 2030 : tree lhs = gimple_call_lhs (stmt);
5741 : 2030 : if (dump_enabled_p ())
5742 : : {
5743 : 34 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
5744 : : "folding virtual function call to %s\n",
5745 : 34 : targets.length () == 1
5746 : 17 : ? targets[0]->name ()
5747 : : : "__builtin_unreachable");
5748 : : }
5749 : 2030 : if (targets.length () == 1)
5750 : : {
5751 : 1987 : tree fndecl = targets[0]->decl;
5752 : 1987 : gimple_call_set_fndecl (stmt, fndecl);
5753 : 1987 : changed = true;
5754 : : /* If changing the call to __cxa_pure_virtual
5755 : : or similar noreturn function, adjust gimple_call_fntype
5756 : : too. */
5757 : 1987 : if (gimple_call_noreturn_p (stmt)
5758 : 28 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
5759 : 13 : && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
5760 : 2000 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5761 : 13 : == void_type_node))
5762 : 13 : gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
5763 : : /* If the call becomes noreturn, remove the lhs. */
5764 : 1987 : if (lhs
5765 : 1682 : && gimple_call_noreturn_p (stmt)
5766 : 2002 : && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
5767 : 6 : || should_remove_lhs_p (lhs)))
5768 : : {
5769 : 12 : if (TREE_CODE (lhs) == SSA_NAME)
5770 : : {
5771 : 0 : tree var = create_tmp_var (TREE_TYPE (lhs));
5772 : 0 : tree def = get_or_create_ssa_default_def (cfun, var);
5773 : 0 : gimple *new_stmt = gimple_build_assign (lhs, def);
5774 : 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5775 : : }
5776 : 12 : gimple_call_set_lhs (stmt, NULL_TREE);
5777 : : }
5778 : 1987 : maybe_remove_unused_call_args (cfun, stmt);
5779 : : }
5780 : : else
5781 : : {
5782 : 43 : location_t loc = gimple_location (stmt);
5783 : 43 : gimple *new_stmt = gimple_build_builtin_unreachable (loc);
5784 : 43 : gimple_call_set_ctrl_altering (new_stmt, false);
5785 : : /* If the call had a SSA name as lhs morph that into
5786 : : an uninitialized value. */
5787 : 43 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
5788 : : {
5789 : 13 : tree var = create_tmp_var (TREE_TYPE (lhs));
5790 : 13 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
5791 : 13 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
5792 : 13 : set_ssa_default_def (cfun, var, lhs);
5793 : : }
5794 : 43 : gimple_move_vops (new_stmt, stmt);
5795 : 43 : gsi_replace (gsi, new_stmt, false);
5796 : 43 : return true;
5797 : : }
5798 : : }
5799 : : }
5800 : : }
5801 : :
5802 : : /* Check for indirect calls that became direct calls, and then
5803 : : no longer require a static chain. */
5804 : 55858505 : if (gimple_call_chain (stmt))
5805 : : {
5806 : 245271 : tree fn = gimple_call_fndecl (stmt);
5807 : 293631 : if (fn && !DECL_STATIC_CHAIN (fn))
5808 : : {
5809 : 2024 : gimple_call_set_chain (stmt, NULL);
5810 : 2024 : changed = true;
5811 : : }
5812 : : }
5813 : :
5814 : 55858505 : if (inplace)
5815 : : return changed;
5816 : :
5817 : : /* Check for builtins that CCP can handle using information not
5818 : : available in the generic fold routines. */
5819 : 55856053 : if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
5820 : : {
5821 : 13493480 : if (gimple_fold_builtin (gsi))
5822 : 193725 : changed = true;
5823 : : }
5824 : 42362573 : else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
5825 : : {
5826 : 1077640 : changed |= targetm.gimple_fold_builtin (gsi);
5827 : : }
5828 : 41284933 : else if (gimple_call_internal_p (stmt))
5829 : : {
5830 : 1393556 : enum tree_code subcode = ERROR_MARK;
5831 : 1393556 : tree result = NULL_TREE;
5832 : 1393556 : bool cplx_result = false;
5833 : 1393556 : bool uaddc_usubc = false;
5834 : 1393556 : tree overflow = NULL_TREE;
5835 : 1393556 : switch (gimple_call_internal_fn (stmt))
5836 : : {
5837 : 156162 : case IFN_BUILTIN_EXPECT:
5838 : 156162 : result = fold_builtin_expect (gimple_location (stmt),
5839 : : gimple_call_arg (stmt, 0),
5840 : : gimple_call_arg (stmt, 1),
5841 : : gimple_call_arg (stmt, 2),
5842 : : NULL_TREE);
5843 : 156162 : break;
5844 : 8450 : case IFN_UBSAN_OBJECT_SIZE:
5845 : 8450 : {
5846 : 8450 : tree offset = gimple_call_arg (stmt, 1);
5847 : 8450 : tree objsize = gimple_call_arg (stmt, 2);
5848 : 8450 : if (integer_all_onesp (objsize)
5849 : 8450 : || (TREE_CODE (offset) == INTEGER_CST
5850 : 4648 : && TREE_CODE (objsize) == INTEGER_CST
5851 : 1163 : && tree_int_cst_le (offset, objsize)))
5852 : : {
5853 : 1502 : replace_call_with_value (gsi, NULL_TREE);
5854 : 1502 : return true;
5855 : : }
5856 : : }
5857 : : break;
5858 : 11211 : case IFN_UBSAN_PTR:
5859 : 11211 : if (integer_zerop (gimple_call_arg (stmt, 1)))
5860 : : {
5861 : 31 : replace_call_with_value (gsi, NULL_TREE);
5862 : 31 : return true;
5863 : : }
5864 : : break;
5865 : 6770 : case IFN_UBSAN_BOUNDS:
5866 : 6770 : {
5867 : 6770 : tree index = gimple_call_arg (stmt, 1);
5868 : 6770 : tree bound = gimple_call_arg (stmt, 2);
5869 : 6770 : if (TREE_CODE (index) == INTEGER_CST
5870 : 4225 : && TREE_CODE (bound) == INTEGER_CST)
5871 : : {
5872 : 4053 : index = fold_convert (TREE_TYPE (bound), index);
5873 : 4053 : if (TREE_CODE (index) == INTEGER_CST
5874 : 4053 : && tree_int_cst_lt (index, bound))
5875 : : {
5876 : 283 : replace_call_with_value (gsi, NULL_TREE);
5877 : 283 : return true;
5878 : : }
5879 : : }
5880 : : }
5881 : : break;
5882 : 19736 : case IFN_GOACC_DIM_SIZE:
5883 : 19736 : case IFN_GOACC_DIM_POS:
5884 : 19736 : result = fold_internal_goacc_dim (stmt);
5885 : 19736 : break;
5886 : : case IFN_UBSAN_CHECK_ADD:
5887 : : subcode = PLUS_EXPR;
5888 : : break;
5889 : : case IFN_UBSAN_CHECK_SUB:
5890 : : subcode = MINUS_EXPR;
5891 : : break;
5892 : : case IFN_UBSAN_CHECK_MUL:
5893 : : subcode = MULT_EXPR;
5894 : : break;
5895 : : case IFN_ADD_OVERFLOW:
5896 : : subcode = PLUS_EXPR;
5897 : : cplx_result = true;
5898 : : break;
5899 : : case IFN_SUB_OVERFLOW:
5900 : : subcode = MINUS_EXPR;
5901 : : cplx_result = true;
5902 : : break;
5903 : : case IFN_MUL_OVERFLOW:
5904 : : subcode = MULT_EXPR;
5905 : : cplx_result = true;
5906 : : break;
5907 : : case IFN_UADDC:
5908 : : subcode = PLUS_EXPR;
5909 : : cplx_result = true;
5910 : : uaddc_usubc = true;
5911 : : break;
5912 : : case IFN_USUBC:
5913 : : subcode = MINUS_EXPR;
5914 : : cplx_result = true;
5915 : : uaddc_usubc = true;
5916 : : break;
5917 : 1530 : case IFN_MASK_LOAD:
5918 : 1530 : changed |= gimple_fold_partial_load (gsi, stmt, true);
5919 : 1530 : break;
5920 : 1434 : case IFN_MASK_STORE:
5921 : 1434 : changed |= gimple_fold_partial_store (gsi, stmt, true);
5922 : 1434 : break;
5923 : 0 : case IFN_LEN_LOAD:
5924 : 0 : case IFN_MASK_LEN_LOAD:
5925 : 0 : changed |= gimple_fold_partial_load (gsi, stmt, false);
5926 : 0 : break;
5927 : 0 : case IFN_LEN_STORE:
5928 : 0 : case IFN_MASK_LEN_STORE:
5929 : 0 : changed |= gimple_fold_partial_store (gsi, stmt, false);
5930 : 0 : break;
5931 : : default:
5932 : : break;
5933 : : }
5934 : 178862 : if (subcode != ERROR_MARK)
5935 : : {
5936 : 488445 : tree arg0 = gimple_call_arg (stmt, 0);
5937 : 488445 : tree arg1 = gimple_call_arg (stmt, 1);
5938 : 488445 : tree arg2 = NULL_TREE;
5939 : 488445 : tree type = TREE_TYPE (arg0);
5940 : 488445 : if (cplx_result)
5941 : : {
5942 : 469464 : tree lhs = gimple_call_lhs (stmt);
5943 : 469464 : if (lhs == NULL_TREE)
5944 : : type = NULL_TREE;
5945 : : else
5946 : 469464 : type = TREE_TYPE (TREE_TYPE (lhs));
5947 : 469464 : if (uaddc_usubc)
5948 : 30992 : arg2 = gimple_call_arg (stmt, 2);
5949 : : }
5950 : 488445 : if (type == NULL_TREE)
5951 : : ;
5952 : 488445 : else if (uaddc_usubc)
5953 : : {
5954 : 30992 : if (!integer_zerop (arg2))
5955 : : ;
5956 : : /* x = y + 0 + 0; x = y - 0 - 0; */
5957 : 4838 : else if (integer_zerop (arg1))
5958 : : result = arg0;
5959 : : /* x = 0 + y + 0; */
5960 : 4217 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
5961 : : result = arg1;
5962 : : /* x = y - y - 0; */
5963 : 4217 : else if (subcode == MINUS_EXPR
5964 : 4217 : && operand_equal_p (arg0, arg1, 0))
5965 : 0 : result = integer_zero_node;
5966 : : }
5967 : : /* x = y + 0; x = y - 0; x = y * 0; */
5968 : 457453 : else if (integer_zerop (arg1))
5969 : 10096 : result = subcode == MULT_EXPR ? integer_zero_node : arg0;
5970 : : /* x = 0 + y; x = 0 * y; */
5971 : 447357 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
5972 : 0 : result = subcode == MULT_EXPR ? integer_zero_node : arg1;
5973 : : /* x = y - y; */
5974 : 447357 : else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
5975 : 6 : result = integer_zero_node;
5976 : : /* x = y * 1; x = 1 * y; */
5977 : 447351 : else if (subcode == MULT_EXPR && integer_onep (arg1))
5978 : : result = arg0;
5979 : 444025 : else if (subcode == MULT_EXPR && integer_onep (arg0))
5980 : : result = arg1;
5981 : 488445 : if (result)
5982 : : {
5983 : 14049 : if (result == integer_zero_node)
5984 : 2139 : result = build_zero_cst (type);
5985 : 11910 : else if (cplx_result && TREE_TYPE (result) != type)
5986 : : {
5987 : 9600 : if (TREE_CODE (result) == INTEGER_CST)
5988 : : {
5989 : 0 : if (arith_overflowed_p (PLUS_EXPR, type, result,
5990 : : integer_zero_node))
5991 : 0 : overflow = build_one_cst (type);
5992 : : }
5993 : 9600 : else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
5994 : 6919 : && TYPE_UNSIGNED (type))
5995 : 9745 : || (TYPE_PRECISION (type)
5996 : 2826 : < (TYPE_PRECISION (TREE_TYPE (result))
5997 : 2826 : + (TYPE_UNSIGNED (TREE_TYPE (result))
5998 : 3145 : && !TYPE_UNSIGNED (type)))))
5999 : : result = NULL_TREE;
6000 : 58 : if (result)
6001 : 58 : result = fold_convert (type, result);
6002 : : }
6003 : : }
6004 : : }
6005 : :
6006 : 907802 : if (result)
6007 : : {
6008 : 25986 : if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
6009 : 0 : result = drop_tree_overflow (result);
6010 : 25986 : if (cplx_result)
6011 : : {
6012 : 4492 : if (overflow == NULL_TREE)
6013 : 4492 : overflow = build_zero_cst (TREE_TYPE (result));
6014 : 4492 : tree ctype = build_complex_type (TREE_TYPE (result));
6015 : 4492 : if (TREE_CODE (result) == INTEGER_CST
6016 : 2139 : && TREE_CODE (overflow) == INTEGER_CST)
6017 : 2139 : result = build_complex (ctype, result, overflow);
6018 : : else
6019 : 2353 : result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
6020 : : ctype, result, overflow);
6021 : : }
6022 : 25986 : gimplify_and_update_call_from_tree (gsi, result);
6023 : 25986 : changed = true;
6024 : : }
6025 : : }
6026 : :
6027 : : return changed;
6028 : : }
6029 : :
6030 : :
6031 : : /* Return true whether NAME has a use on STMT. Note this can return
6032 : : false even though there's a use on STMT if SSA operands are not
6033 : : up-to-date. */
6034 : :
6035 : : static bool
6036 : 1605 : has_use_on_stmt (tree name, gimple *stmt)
6037 : : {
6038 : 1605 : ssa_op_iter iter;
6039 : 1605 : tree op;
6040 : 3245 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6041 : 1686 : if (op == name)
6042 : : return true;
6043 : : return false;
6044 : : }
6045 : :
6046 : : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
6047 : :
6048 : : void
6049 : 4527957 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
6050 : : {
6051 : 4527957 : if (!dce_worklist)
6052 : : return;
6053 : :
6054 : 1645824 : for (gimple_stmt_iterator i = gsi_start (seq);
6055 : 1925876 : !gsi_end_p (i); gsi_next (&i))
6056 : : {
6057 : 280052 : gimple *stmt = gsi_stmt (i);
6058 : 280052 : tree name = gimple_get_lhs (stmt);
6059 : 280052 : if (name && TREE_CODE (name) == SSA_NAME)
6060 : 280052 : bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
6061 : : }
6062 : : }
6063 : :
6064 : : /* Worker for fold_stmt_1 dispatch to pattern based folding with
6065 : : gimple_simplify.
6066 : :
6067 : : Replaces *GSI with the simplification result in RCODE and OPS
6068 : : and the associated statements in *SEQ. Does the replacement
6069 : : according to INPLACE and returns true if the operation succeeded. */
6070 : :
6071 : : static bool
6072 : 8894777 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
6073 : : gimple_match_op *res_op,
6074 : : gimple_seq *seq, bool inplace,
6075 : : bitmap dce_worklist)
6076 : : {
6077 : 8894777 : gimple *stmt = gsi_stmt (*gsi);
6078 : 8894777 : tree *ops = res_op->ops;
6079 : 8894777 : unsigned int num_ops = res_op->num_ops;
6080 : :
6081 : : /* Play safe and do not allow abnormals to be mentioned in
6082 : : newly created statements. See also maybe_push_res_to_seq.
6083 : : As an exception allow such uses if there was a use of the
6084 : : same SSA name on the old stmt. */
6085 : 19679556 : for (unsigned int i = 0; i < num_ops; ++i)
6086 : 10786338 : if (TREE_CODE (ops[i]) == SSA_NAME
6087 : 6645123 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
6088 : 10787943 : && !has_use_on_stmt (ops[i], stmt))
6089 : : return false;
6090 : :
6091 : 8893218 : if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
6092 : 0 : for (unsigned int i = 0; i < 2; ++i)
6093 : 0 : if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
6094 : 0 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
6095 : 0 : && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
6096 : : return false;
6097 : :
6098 : : /* Don't insert new statements when INPLACE is true, even if we could
6099 : : reuse STMT for the final statement. */
6100 : 8893218 : if (inplace && !gimple_seq_empty_p (*seq))
6101 : : return false;
6102 : :
6103 : 8893218 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6104 : : {
6105 : 6798652 : gcc_assert (res_op->code.is_tree_code ());
6106 : 6798652 : auto code = tree_code (res_op->code);
6107 : 6798652 : if (TREE_CODE_CLASS (code) == tcc_comparison
6108 : : /* GIMPLE_CONDs condition may not throw. */
6109 : 6798652 : && ((cfun
6110 : 1126706 : && (!flag_exceptions
6111 : 778968 : || !cfun->can_throw_non_call_exceptions))
6112 : 288994 : || !operation_could_trap_p (code,
6113 : 288994 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6114 : : false, NULL_TREE)))
6115 : 1115468 : gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
6116 : 5683184 : else if (code == SSA_NAME)
6117 : : {
6118 : : /* If setting the gimple cond to the same thing,
6119 : : return false as nothing changed. */
6120 : 3982209 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6121 : 3957882 : && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
6122 : 7937626 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6123 : : return false;
6124 : 26792 : gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
6125 : 26792 : build_zero_cst (TREE_TYPE (ops[0])));
6126 : : }
6127 : 1700975 : else if (code == INTEGER_CST)
6128 : : {
6129 : : /* Make into the canonical form `1 != 0` and `0 != 0`.
6130 : : If already in the canonical form return false
6131 : : saying nothing has been done. */
6132 : 1094209 : if (integer_zerop (ops[0]))
6133 : : {
6134 : 5219279 : if (gimple_cond_false_canonical_p (cond_stmt))
6135 : : return false;
6136 : 458287 : gimple_cond_make_false (cond_stmt);
6137 : : }
6138 : : else
6139 : : {
6140 : 316490 : if (gimple_cond_true_canonical_p (cond_stmt))
6141 : : return false;
6142 : 163403 : gimple_cond_make_true (cond_stmt);
6143 : : }
6144 : : }
6145 : 606766 : else if (!inplace)
6146 : : {
6147 : : /* For throwing comparisons, see if the GIMPLE_COND is the same as
6148 : : the comparison would be.
6149 : : This can happen due to the match pattern for
6150 : : `(ne (cmp @0 @1) integer_zerop)` which creates a new expression
6151 : : for the comparison. */
6152 : 606766 : if (TREE_CODE_CLASS (code) == tcc_comparison
6153 : 11238 : && (!cfun
6154 : 11238 : || (flag_exceptions
6155 : 11238 : && cfun->can_throw_non_call_exceptions))
6156 : 618004 : && operation_could_trap_p (code,
6157 : 11238 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6158 : : false, NULL_TREE))
6159 : : {
6160 : 11238 : tree lhs = gimple_cond_lhs (cond_stmt);
6161 : 11238 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6162 : 11238 : && TREE_CODE (lhs) == SSA_NAME
6163 : 11238 : && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6164 : 22476 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6165 : : {
6166 : 11238 : gimple *s = SSA_NAME_DEF_STMT (lhs);
6167 : 11238 : if (is_gimple_assign (s)
6168 : 11238 : && gimple_assign_rhs_code (s) == code
6169 : 11238 : && operand_equal_p (gimple_assign_rhs1 (s), ops[0])
6170 : 22476 : && operand_equal_p (gimple_assign_rhs2 (s), ops[1]))
6171 : : return false;
6172 : : }
6173 : : }
6174 : 595528 : tree res = maybe_push_res_to_seq (res_op, seq);
6175 : 595528 : if (!res)
6176 : : return false;
6177 : 595528 : gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
6178 : 595528 : build_zero_cst (TREE_TYPE (res)));
6179 : : }
6180 : : else
6181 : : return false;
6182 : 2359478 : if (dump_file && (dump_flags & TDF_DETAILS))
6183 : : {
6184 : 858 : fprintf (dump_file, "gimple_simplified to ");
6185 : 858 : if (!gimple_seq_empty_p (*seq))
6186 : 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6187 : 858 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6188 : : 0, TDF_SLIM);
6189 : : }
6190 : : // Mark the lhs of the new statements maybe for dce
6191 : 2359478 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6192 : 2359478 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6193 : 2359478 : return true;
6194 : : }
6195 : 2094566 : else if (is_gimple_assign (stmt)
6196 : 2094566 : && res_op->code.is_tree_code ())
6197 : : {
6198 : 2020092 : auto code = tree_code (res_op->code);
6199 : 2020092 : if (!inplace
6200 : 2020092 : || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
6201 : : {
6202 : 2020092 : maybe_build_generic_op (res_op);
6203 : 4756075 : gimple_assign_set_rhs_with_ops (gsi, code,
6204 : : res_op->op_or_null (0),
6205 : : res_op->op_or_null (1),
6206 : : res_op->op_or_null (2));
6207 : 2020092 : if (dump_file && (dump_flags & TDF_DETAILS))
6208 : : {
6209 : 11393 : fprintf (dump_file, "gimple_simplified to ");
6210 : 11393 : if (!gimple_seq_empty_p (*seq))
6211 : 41 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6212 : 11393 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6213 : : 0, TDF_SLIM);
6214 : : }
6215 : : // Mark the lhs of the new statements maybe for dce
6216 : 2020092 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6217 : 2020092 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6218 : 2020092 : return true;
6219 : : }
6220 : : }
6221 : 74474 : else if (res_op->code.is_fn_code ()
6222 : 74474 : && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
6223 : : {
6224 : 7763 : gcc_assert (num_ops == gimple_call_num_args (stmt));
6225 : 23097 : for (unsigned int i = 0; i < num_ops; ++i)
6226 : 15334 : gimple_call_set_arg (stmt, i, ops[i]);
6227 : 7763 : if (dump_file && (dump_flags & TDF_DETAILS))
6228 : : {
6229 : 0 : fprintf (dump_file, "gimple_simplified to ");
6230 : 0 : if (!gimple_seq_empty_p (*seq))
6231 : 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6232 : 0 : print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
6233 : : }
6234 : : // Mark the lhs of the new statements maybe for dce
6235 : 7763 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6236 : 7763 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6237 : 7763 : return true;
6238 : : }
6239 : 66711 : else if (!inplace)
6240 : : {
6241 : 131273 : if (gimple_has_lhs (stmt))
6242 : : {
6243 : 66711 : tree lhs = gimple_get_lhs (stmt);
6244 : 66711 : if (!maybe_push_res_to_seq (res_op, seq, lhs))
6245 : : return false;
6246 : 65884 : if (dump_file && (dump_flags & TDF_DETAILS))
6247 : : {
6248 : 10 : fprintf (dump_file, "gimple_simplified to ");
6249 : 10 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6250 : : }
6251 : : // Mark the lhs of the new statements maybe for dce
6252 : 65884 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6253 : 65884 : gsi_replace_with_seq_vops (gsi, *seq);
6254 : 65884 : return true;
6255 : : }
6256 : : else
6257 : 0 : gcc_unreachable ();
6258 : : }
6259 : :
6260 : : return false;
6261 : : }
6262 : :
6263 : : /* Canonicalize MEM_REFs invariant address operand after propagation. */
6264 : :
6265 : : static bool
6266 : 192423569 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
6267 : : {
6268 : 192423569 : bool res = false;
6269 : 192423569 : tree *orig_t = t;
6270 : :
6271 : 192423569 : if (TREE_CODE (*t) == ADDR_EXPR)
6272 : 62937433 : t = &TREE_OPERAND (*t, 0);
6273 : :
6274 : : /* The C and C++ frontends use an ARRAY_REF for indexing with their
6275 : : generic vector extension. The actual vector referenced is
6276 : : view-converted to an array type for this purpose. If the index
6277 : : is constant the canonical representation in the middle-end is a
6278 : : BIT_FIELD_REF so re-write the former to the latter here. */
6279 : 192423569 : if (TREE_CODE (*t) == ARRAY_REF
6280 : 10306530 : && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
6281 : 148136 : && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
6282 : 192467996 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
6283 : : {
6284 : 15555 : tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
6285 : 15555 : if (VECTOR_TYPE_P (vtype))
6286 : : {
6287 : 15555 : tree low = array_ref_low_bound (*t);
6288 : 15555 : if (TREE_CODE (low) == INTEGER_CST)
6289 : : {
6290 : 15555 : if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
6291 : : {
6292 : 31066 : widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
6293 : 31066 : wi::to_widest (low));
6294 : 15533 : idx = wi::mul (idx, wi::to_widest
6295 : 31066 : (TYPE_SIZE (TREE_TYPE (*t))));
6296 : 15533 : widest_int ext
6297 : 15533 : = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
6298 : 15533 : if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
6299 : : {
6300 : 30470 : *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
6301 : 15235 : TREE_TYPE (*t),
6302 : 15235 : TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
6303 : 15235 : TYPE_SIZE (TREE_TYPE (*t)),
6304 : 15235 : wide_int_to_tree (bitsizetype, idx));
6305 : 15235 : res = true;
6306 : : }
6307 : 15533 : }
6308 : : }
6309 : : }
6310 : : }
6311 : :
6312 : 358141263 : while (handled_component_p (*t))
6313 : 165717694 : t = &TREE_OPERAND (*t, 0);
6314 : :
6315 : : /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
6316 : : of invariant addresses into a SSA name MEM_REF address. */
6317 : 192423569 : if (TREE_CODE (*t) == MEM_REF
6318 : 192423569 : || TREE_CODE (*t) == TARGET_MEM_REF)
6319 : : {
6320 : 104132626 : tree addr = TREE_OPERAND (*t, 0);
6321 : 104132626 : if (TREE_CODE (addr) == ADDR_EXPR
6322 : 104132626 : && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
6323 : 33784496 : || handled_component_p (TREE_OPERAND (addr, 0))))
6324 : : {
6325 : 565070 : tree base;
6326 : 565070 : poly_int64 coffset;
6327 : 565070 : base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
6328 : : &coffset);
6329 : 565070 : if (!base)
6330 : : {
6331 : 18 : if (is_debug)
6332 : 18 : return false;
6333 : 0 : gcc_unreachable ();
6334 : : }
6335 : :
6336 : 565052 : TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
6337 : 565052 : TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
6338 : 565052 : TREE_OPERAND (*t, 1),
6339 : 565052 : size_int (coffset));
6340 : 565052 : res = true;
6341 : : }
6342 : 104132608 : gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
6343 : : || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
6344 : : }
6345 : :
6346 : : /* Canonicalize back MEM_REFs to plain reference trees if the object
6347 : : accessed is a decl that has the same access semantics as the MEM_REF. */
6348 : 192423551 : if (TREE_CODE (*t) == MEM_REF
6349 : 102186306 : && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
6350 : 33476494 : && integer_zerop (TREE_OPERAND (*t, 1))
6351 : 210902171 : && MR_DEPENDENCE_CLIQUE (*t) == 0)
6352 : : {
6353 : 11425792 : tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6354 : 11425792 : tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
6355 : 11425792 : if (/* Same volatile qualification. */
6356 : 11425792 : TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
6357 : : /* Same TBAA behavior with -fstrict-aliasing. */
6358 : 11422690 : && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
6359 : 11194251 : && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
6360 : 11194251 : == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
6361 : : /* Same alignment. */
6362 : 4910602 : && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
6363 : : /* We have to look out here to not drop a required conversion
6364 : : from the rhs to the lhs if *t appears on the lhs or vice-versa
6365 : : if it appears on the rhs. Thus require strict type
6366 : : compatibility. */
6367 : 15945110 : && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
6368 : : {
6369 : 2504178 : *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6370 : 2504178 : res = true;
6371 : : }
6372 : : }
6373 : :
6374 : 180997759 : else if (TREE_CODE (*orig_t) == ADDR_EXPR
6375 : 60421453 : && TREE_CODE (*t) == MEM_REF
6376 : 202203503 : && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
6377 : : {
6378 : 811 : tree base;
6379 : 811 : poly_int64 coffset;
6380 : 811 : base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
6381 : : &coffset);
6382 : 811 : if (base)
6383 : : {
6384 : 705 : gcc_assert (TREE_CODE (base) == MEM_REF);
6385 : 705 : poly_int64 moffset;
6386 : 705 : if (mem_ref_offset (base).to_shwi (&moffset))
6387 : : {
6388 : 705 : coffset += moffset;
6389 : 705 : if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
6390 : : {
6391 : 705 : coffset += moffset;
6392 : 705 : *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
6393 : 705 : return true;
6394 : : }
6395 : : }
6396 : : }
6397 : : }
6398 : :
6399 : : /* Canonicalize TARGET_MEM_REF in particular with respect to
6400 : : the indexes becoming constant. */
6401 : 180996948 : else if (TREE_CODE (*t) == TARGET_MEM_REF)
6402 : : {
6403 : 1946302 : tree tem = maybe_fold_tmr (*t);
6404 : 1946302 : if (tem)
6405 : : {
6406 : 1629 : *t = tem;
6407 : 1629 : if (TREE_CODE (*orig_t) == ADDR_EXPR)
6408 : 0 : recompute_tree_invariant_for_addr_expr (*orig_t);
6409 : : res = true;
6410 : : }
6411 : : }
6412 : :
6413 : : return res;
6414 : : }
6415 : :
6416 : : /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
6417 : : distinguishes both cases. */
6418 : :
6419 : : static bool
6420 : 748996449 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
6421 : : bitmap dce_worklist = nullptr)
6422 : : {
6423 : 748996449 : bool changed = false;
6424 : 748996449 : gimple *stmt = gsi_stmt (*gsi);
6425 : 748996449 : bool nowarning = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
6426 : 748996449 : unsigned i;
6427 : 748996449 : fold_defer_overflow_warnings ();
6428 : :
6429 : : /* First do required canonicalization of [TARGET_]MEM_REF addresses
6430 : : after propagation.
6431 : : ??? This shouldn't be done in generic folding but in the
6432 : : propagation helpers which also know whether an address was
6433 : : propagated.
6434 : : Also canonicalize operand order. */
6435 : 748996449 : switch (gimple_code (stmt))
6436 : : {
6437 : 256498092 : case GIMPLE_ASSIGN:
6438 : 256498092 : if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
6439 : : {
6440 : 170379430 : tree *rhs = gimple_assign_rhs1_ptr (stmt);
6441 : 170379430 : if ((REFERENCE_CLASS_P (*rhs)
6442 : 108802078 : || TREE_CODE (*rhs) == ADDR_EXPR)
6443 : 185208633 : && maybe_canonicalize_mem_ref_addr (rhs))
6444 : : changed = true;
6445 : 170379430 : tree *lhs = gimple_assign_lhs_ptr (stmt);
6446 : 170379430 : if (REFERENCE_CLASS_P (*lhs)
6447 : 170379430 : && maybe_canonicalize_mem_ref_addr (lhs))
6448 : : changed = true;
6449 : : /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
6450 : : This cannot be done in maybe_canonicalize_mem_ref_addr
6451 : : as the gimple now has two operands rather than one.
6452 : : The same reason why this can't be done in
6453 : : maybe_canonicalize_mem_ref_addr is the same reason why
6454 : : this can't be done inplace. */
6455 : 170379430 : if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
6456 : : {
6457 : 14629625 : tree inner = TREE_OPERAND (*rhs, 0);
6458 : 14629625 : if (TREE_CODE (inner) == MEM_REF
6459 : 1088539 : && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
6460 : 14684278 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6461 : : {
6462 : 54653 : tree ptr = TREE_OPERAND (inner, 0);
6463 : 54653 : tree addon = TREE_OPERAND (inner, 1);
6464 : 54653 : addon = fold_convert (sizetype, addon);
6465 : 54653 : gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
6466 : : ptr, addon);
6467 : 54653 : changed = true;
6468 : 54653 : stmt = gsi_stmt (*gsi);
6469 : : }
6470 : : }
6471 : : }
6472 : : else
6473 : : {
6474 : : /* Canonicalize operand order. */
6475 : 86118662 : enum tree_code code = gimple_assign_rhs_code (stmt);
6476 : 86118662 : if (TREE_CODE_CLASS (code) == tcc_comparison
6477 : 80131414 : || commutative_tree_code (code)
6478 : 129584446 : || commutative_ternary_tree_code (code))
6479 : : {
6480 : 42653425 : tree rhs1 = gimple_assign_rhs1 (stmt);
6481 : 42653425 : tree rhs2 = gimple_assign_rhs2 (stmt);
6482 : 42653425 : if (tree_swap_operands_p (rhs1, rhs2))
6483 : : {
6484 : 3217837 : gimple_assign_set_rhs1 (stmt, rhs2);
6485 : 3217837 : gimple_assign_set_rhs2 (stmt, rhs1);
6486 : 3217837 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6487 : 306120 : gimple_assign_set_rhs_code (stmt,
6488 : : swap_tree_comparison (code));
6489 : : changed = true;
6490 : : }
6491 : : }
6492 : : }
6493 : : break;
6494 : 55910857 : case GIMPLE_CALL:
6495 : 55910857 : {
6496 : 55910857 : gcall *call = as_a<gcall *> (stmt);
6497 : 168308721 : for (i = 0; i < gimple_call_num_args (call); ++i)
6498 : : {
6499 : 112397864 : tree *arg = gimple_call_arg_ptr (call, i);
6500 : 112397864 : if (REFERENCE_CLASS_P (*arg)
6501 : 112397864 : && maybe_canonicalize_mem_ref_addr (arg))
6502 : : changed = true;
6503 : : }
6504 : 55910857 : tree *lhs = gimple_call_lhs_ptr (call);
6505 : 55910857 : if (*lhs
6506 : 22661413 : && REFERENCE_CLASS_P (*lhs)
6507 : 56058079 : && maybe_canonicalize_mem_ref_addr (lhs))
6508 : : changed = true;
6509 : 55910857 : if (*lhs)
6510 : : {
6511 : 22661413 : combined_fn cfn = gimple_call_combined_fn (call);
6512 : 22661413 : internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
6513 : 22661413 : int opno = first_commutative_argument (ifn);
6514 : 22661413 : if (opno >= 0)
6515 : : {
6516 : 350935 : tree arg1 = gimple_call_arg (call, opno);
6517 : 350935 : tree arg2 = gimple_call_arg (call, opno + 1);
6518 : 350935 : if (tree_swap_operands_p (arg1, arg2))
6519 : : {
6520 : 23228 : gimple_call_set_arg (call, opno, arg2);
6521 : 23228 : gimple_call_set_arg (call, opno + 1, arg1);
6522 : 23228 : changed = true;
6523 : : }
6524 : : }
6525 : : }
6526 : : break;
6527 : : }
6528 : 556187 : case GIMPLE_ASM:
6529 : 556187 : {
6530 : 556187 : gasm *asm_stmt = as_a <gasm *> (stmt);
6531 : 1150423 : for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
6532 : : {
6533 : 594236 : tree link = gimple_asm_output_op (asm_stmt, i);
6534 : 594236 : tree op = TREE_VALUE (link);
6535 : 594236 : if (REFERENCE_CLASS_P (op)
6536 : 594236 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6537 : : changed = true;
6538 : : }
6539 : 921793 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
6540 : : {
6541 : 365606 : tree link = gimple_asm_input_op (asm_stmt, i);
6542 : 365606 : tree op = TREE_VALUE (link);
6543 : 365606 : if ((REFERENCE_CLASS_P (op)
6544 : 359841 : || TREE_CODE (op) == ADDR_EXPR)
6545 : 396364 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6546 : : changed = true;
6547 : : }
6548 : : }
6549 : : break;
6550 : 371890419 : case GIMPLE_DEBUG:
6551 : 371890419 : if (gimple_debug_bind_p (stmt))
6552 : : {
6553 : 281152567 : tree *val = gimple_debug_bind_get_value_ptr (stmt);
6554 : 281152567 : if (*val
6555 : 162366694 : && (REFERENCE_CLASS_P (*val)
6556 : 159956844 : || TREE_CODE (*val) == ADDR_EXPR)
6557 : 331639889 : && maybe_canonicalize_mem_ref_addr (val, true))
6558 : : changed = true;
6559 : : }
6560 : : break;
6561 : 43732776 : case GIMPLE_COND:
6562 : 43732776 : {
6563 : : /* Canonicalize operand order. */
6564 : 43732776 : tree lhs = gimple_cond_lhs (stmt);
6565 : 43732776 : tree rhs = gimple_cond_rhs (stmt);
6566 : 43732776 : if (tree_swap_operands_p (lhs, rhs))
6567 : : {
6568 : 1358570 : gcond *gc = as_a <gcond *> (stmt);
6569 : 1358570 : gimple_cond_set_lhs (gc, rhs);
6570 : 1358570 : gimple_cond_set_rhs (gc, lhs);
6571 : 1358570 : gimple_cond_set_code (gc,
6572 : : swap_tree_comparison (gimple_cond_code (gc)));
6573 : 1358570 : changed = true;
6574 : : }
6575 : : }
6576 : 747497264 : default:;
6577 : : }
6578 : :
6579 : : /* Dispatch to pattern-based folding. */
6580 : 747497264 : if (!inplace
6581 : 3214179 : || is_gimple_assign (stmt)
6582 : 748395193 : || gimple_code (stmt) == GIMPLE_COND)
6583 : : {
6584 : 748098520 : gimple_seq seq = NULL;
6585 : 748098520 : gimple_match_op res_op;
6586 : 1493880790 : if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
6587 : : valueize, valueize))
6588 : : {
6589 : 8894777 : if (replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
6590 : : dce_worklist))
6591 : : changed = true;
6592 : : else
6593 : 4441560 : gimple_seq_discard (seq);
6594 : : }
6595 : : }
6596 : :
6597 : 748996449 : stmt = gsi_stmt (*gsi);
6598 : :
6599 : : /* Fold the main computation performed by the statement. */
6600 : 748996449 : switch (gimple_code (stmt))
6601 : : {
6602 : 256550401 : case GIMPLE_ASSIGN:
6603 : 256550401 : {
6604 : : /* Try to canonicalize for boolean-typed X the comparisons
6605 : : X == 0, X == 1, X != 0, and X != 1. */
6606 : 256550401 : if (gimple_assign_rhs_code (stmt) == EQ_EXPR
6607 : 256550401 : || gimple_assign_rhs_code (stmt) == NE_EXPR)
6608 : : {
6609 : 3304254 : tree lhs = gimple_assign_lhs (stmt);
6610 : 3304254 : tree op1 = gimple_assign_rhs1 (stmt);
6611 : 3304254 : tree op2 = gimple_assign_rhs2 (stmt);
6612 : 3304254 : tree type = TREE_TYPE (op1);
6613 : :
6614 : : /* Check whether the comparison operands are of the same boolean
6615 : : type as the result type is.
6616 : : Check that second operand is an integer-constant with value
6617 : : one or zero. */
6618 : 3304254 : if (TREE_CODE (op2) == INTEGER_CST
6619 : 2256168 : && (integer_zerop (op2) || integer_onep (op2))
6620 : 4941960 : && useless_type_conversion_p (TREE_TYPE (lhs), type))
6621 : : {
6622 : 4738 : enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
6623 : 4738 : bool is_logical_not = false;
6624 : :
6625 : : /* X == 0 and X != 1 is a logical-not.of X
6626 : : X == 1 and X != 0 is X */
6627 : 3934 : if ((cmp_code == EQ_EXPR && integer_zerop (op2))
6628 : 4738 : || (cmp_code == NE_EXPR && integer_onep (op2)))
6629 : 4696 : is_logical_not = true;
6630 : :
6631 : 4738 : if (is_logical_not == false)
6632 : 42 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
6633 : : /* Only for one-bit precision typed X the transformation
6634 : : !X -> ~X is valied. */
6635 : 4696 : else if (TYPE_PRECISION (type) == 1)
6636 : 4696 : gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
6637 : : /* Otherwise we use !X -> X ^ 1. */
6638 : : else
6639 : 0 : gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
6640 : : build_int_cst (type, 1));
6641 : : changed = true;
6642 : : break;
6643 : : }
6644 : : }
6645 : :
6646 : 256545663 : unsigned old_num_ops = gimple_num_ops (stmt);
6647 : 256545663 : tree lhs = gimple_assign_lhs (stmt);
6648 : 256545663 : tree new_rhs = fold_gimple_assign (gsi);
6649 : 256545663 : if (new_rhs
6650 : 256666425 : && !useless_type_conversion_p (TREE_TYPE (lhs),
6651 : 120762 : TREE_TYPE (new_rhs)))
6652 : 0 : new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
6653 : 256545663 : if (new_rhs
6654 : 256545663 : && (!inplace
6655 : 770 : || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
6656 : : {
6657 : 120762 : gimple_assign_set_rhs_from_tree (gsi, new_rhs);
6658 : 120762 : changed = true;
6659 : : }
6660 : : break;
6661 : : }
6662 : :
6663 : 55858548 : case GIMPLE_CALL:
6664 : 55858548 : changed |= gimple_fold_call (gsi, inplace);
6665 : 55858548 : break;
6666 : :
6667 : 371890419 : case GIMPLE_DEBUG:
6668 : 371890419 : if (gimple_debug_bind_p (stmt))
6669 : : {
6670 : 281152567 : tree val = gimple_debug_bind_get_value (stmt);
6671 : 281152567 : if (val && REFERENCE_CLASS_P (val))
6672 : : {
6673 : 2408047 : tree tem = maybe_fold_reference (val);
6674 : 2408047 : if (tem)
6675 : : {
6676 : 332 : gimple_debug_bind_set_value (stmt, tem);
6677 : 332 : changed = true;
6678 : : }
6679 : : }
6680 : : }
6681 : : break;
6682 : :
6683 : 10491857 : case GIMPLE_RETURN:
6684 : 10491857 : {
6685 : 10491857 : greturn *ret_stmt = as_a<greturn *> (stmt);
6686 : 10491857 : tree ret = gimple_return_retval(ret_stmt);
6687 : :
6688 : 10491857 : if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
6689 : : {
6690 : 4371358 : tree val = valueize (ret);
6691 : 4371358 : if (val && val != ret
6692 : 4371358 : && may_propagate_copy (ret, val))
6693 : : {
6694 : 0 : gimple_return_set_retval (ret_stmt, val);
6695 : 0 : changed = true;
6696 : : }
6697 : : }
6698 : : }
6699 : : break;
6700 : :
6701 : 748996449 : default:;
6702 : : }
6703 : :
6704 : 748996449 : stmt = gsi_stmt (*gsi);
6705 : :
6706 : 748996449 : fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
6707 : 748996449 : return changed;
6708 : : }
6709 : :
6710 : : /* Valueziation callback that ends up not following SSA edges. */
6711 : :
6712 : : tree
6713 : 5099342844 : no_follow_ssa_edges (tree)
6714 : : {
6715 : 5099342844 : return NULL_TREE;
6716 : : }
6717 : :
6718 : : /* Valueization callback that ends up following single-use SSA edges only. */
6719 : :
6720 : : tree
6721 : 796497570 : follow_single_use_edges (tree val)
6722 : : {
6723 : 796497570 : if (TREE_CODE (val) == SSA_NAME
6724 : 796497570 : && !has_single_use (val))
6725 : 403992508 : return NULL_TREE;
6726 : : return val;
6727 : : }
6728 : :
6729 : : /* Valueization callback that follows all SSA edges. */
6730 : :
6731 : : tree
6732 : 173546327 : follow_all_ssa_edges (tree val)
6733 : : {
6734 : 173546327 : return val;
6735 : : }
6736 : :
6737 : : /* Fold the statement pointed to by GSI. In some cases, this function may
6738 : : replace the whole statement with a new one. Returns true iff folding
6739 : : makes any changes.
6740 : : The statement pointed to by GSI should be in valid gimple form but may
6741 : : be in unfolded state as resulting from for example constant propagation
6742 : : which can produce *&x = 0. */
6743 : :
6744 : : bool
6745 : 146561629 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
6746 : : {
6747 : 146561629 : return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
6748 : : }
6749 : :
6750 : : bool
6751 : 599220641 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
6752 : : {
6753 : 599220641 : return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
6754 : : }
6755 : :
6756 : : /* Perform the minimal folding on statement *GSI. Only operations like
6757 : : *&x created by constant propagation are handled. The statement cannot
6758 : : be replaced with a new one. Return true if the statement was
6759 : : changed, false otherwise.
6760 : : The statement *GSI should be in valid gimple form but may
6761 : : be in unfolded state as resulting from for example constant propagation
6762 : : which can produce *&x = 0. */
6763 : :
6764 : : bool
6765 : 3214179 : fold_stmt_inplace (gimple_stmt_iterator *gsi)
6766 : : {
6767 : 3214179 : gimple *stmt = gsi_stmt (*gsi);
6768 : 3214179 : bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
6769 : 3214179 : gcc_assert (gsi_stmt (*gsi) == stmt);
6770 : 3214179 : return changed;
6771 : : }
6772 : :
6773 : : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
6774 : : if EXPR is null or we don't know how.
6775 : : If non-null, the result always has boolean type. */
6776 : :
6777 : : static tree
6778 : 291407 : canonicalize_bool (tree expr, bool invert)
6779 : : {
6780 : 291407 : if (!expr)
6781 : : return NULL_TREE;
6782 : 56 : else if (invert)
6783 : : {
6784 : 42 : if (integer_nonzerop (expr))
6785 : 0 : return boolean_false_node;
6786 : 42 : else if (integer_zerop (expr))
6787 : 0 : return boolean_true_node;
6788 : 42 : else if (TREE_CODE (expr) == SSA_NAME)
6789 : 0 : return fold_build2 (EQ_EXPR, boolean_type_node, expr,
6790 : : build_int_cst (TREE_TYPE (expr), 0));
6791 : 42 : else if (COMPARISON_CLASS_P (expr))
6792 : 42 : return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
6793 : : boolean_type_node,
6794 : : TREE_OPERAND (expr, 0),
6795 : : TREE_OPERAND (expr, 1));
6796 : : else
6797 : : return NULL_TREE;
6798 : : }
6799 : : else
6800 : : {
6801 : 14 : if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
6802 : : return expr;
6803 : 0 : if (integer_nonzerop (expr))
6804 : 0 : return boolean_true_node;
6805 : 0 : else if (integer_zerop (expr))
6806 : 0 : return boolean_false_node;
6807 : 0 : else if (TREE_CODE (expr) == SSA_NAME)
6808 : 0 : return fold_build2 (NE_EXPR, boolean_type_node, expr,
6809 : : build_int_cst (TREE_TYPE (expr), 0));
6810 : 0 : else if (COMPARISON_CLASS_P (expr))
6811 : 0 : return fold_build2 (TREE_CODE (expr),
6812 : : boolean_type_node,
6813 : : TREE_OPERAND (expr, 0),
6814 : : TREE_OPERAND (expr, 1));
6815 : : else
6816 : : return NULL_TREE;
6817 : : }
6818 : : }
6819 : :
6820 : : /* Check to see if a boolean expression EXPR is logically equivalent to the
6821 : : comparison (OP1 CODE OP2). Check for various identities involving
6822 : : SSA_NAMEs. */
6823 : :
6824 : : static bool
6825 : 2077 : same_bool_comparison_p (const_tree expr, enum tree_code code,
6826 : : const_tree op1, const_tree op2)
6827 : : {
6828 : 2077 : gimple *s;
6829 : :
6830 : : /* The obvious case. */
6831 : 2077 : if (TREE_CODE (expr) == code
6832 : 49 : && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
6833 : 2126 : && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
6834 : : return true;
6835 : :
6836 : : /* Check for comparing (name, name != 0) and the case where expr
6837 : : is an SSA_NAME with a definition matching the comparison. */
6838 : 2060 : if (TREE_CODE (expr) == SSA_NAME
6839 : 2060 : && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
6840 : : {
6841 : 0 : if (operand_equal_p (expr, op1, 0))
6842 : 0 : return ((code == NE_EXPR && integer_zerop (op2))
6843 : 0 : || (code == EQ_EXPR && integer_nonzerop (op2)));
6844 : 0 : s = SSA_NAME_DEF_STMT (expr);
6845 : 0 : if (is_gimple_assign (s)
6846 : 0 : && gimple_assign_rhs_code (s) == code
6847 : 0 : && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
6848 : 0 : && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
6849 : : return true;
6850 : : }
6851 : :
6852 : : /* If op1 is of the form (name != 0) or (name == 0), and the definition
6853 : : of name is a comparison, recurse. */
6854 : 2060 : if (TREE_CODE (op1) == SSA_NAME
6855 : 2060 : && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
6856 : : {
6857 : 468 : s = SSA_NAME_DEF_STMT (op1);
6858 : 468 : if (is_gimple_assign (s)
6859 : 468 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
6860 : : {
6861 : 0 : enum tree_code c = gimple_assign_rhs_code (s);
6862 : 0 : if ((c == NE_EXPR && integer_zerop (op2))
6863 : 0 : || (c == EQ_EXPR && integer_nonzerop (op2)))
6864 : 0 : return same_bool_comparison_p (expr, c,
6865 : 0 : gimple_assign_rhs1 (s),
6866 : 0 : gimple_assign_rhs2 (s));
6867 : 0 : if ((c == EQ_EXPR && integer_zerop (op2))
6868 : 0 : || (c == NE_EXPR && integer_nonzerop (op2)))
6869 : 0 : return same_bool_comparison_p (expr,
6870 : : invert_tree_comparison (c, false),
6871 : 0 : gimple_assign_rhs1 (s),
6872 : 0 : gimple_assign_rhs2 (s));
6873 : : }
6874 : : }
6875 : : return false;
6876 : : }
6877 : :
6878 : : /* Check to see if two boolean expressions OP1 and OP2 are logically
6879 : : equivalent. */
6880 : :
6881 : : static bool
6882 : 23 : same_bool_result_p (const_tree op1, const_tree op2)
6883 : : {
6884 : : /* Simple cases first. */
6885 : 23 : if (operand_equal_p (op1, op2, 0))
6886 : : return true;
6887 : :
6888 : : /* Check the cases where at least one of the operands is a comparison.
6889 : : These are a bit smarter than operand_equal_p in that they apply some
6890 : : identifies on SSA_NAMEs. */
6891 : 16 : if (COMPARISON_CLASS_P (op2)
6892 : 32 : && same_bool_comparison_p (op1, TREE_CODE (op2),
6893 : 16 : TREE_OPERAND (op2, 0),
6894 : 16 : TREE_OPERAND (op2, 1)))
6895 : : return true;
6896 : 16 : if (COMPARISON_CLASS_P (op1)
6897 : 32 : && same_bool_comparison_p (op2, TREE_CODE (op1),
6898 : 16 : TREE_OPERAND (op1, 0),
6899 : 16 : TREE_OPERAND (op1, 1)))
6900 : : return true;
6901 : :
6902 : : /* Default case. */
6903 : : return false;
6904 : : }
6905 : :
6906 : : /* Forward declarations for some mutually recursive functions. */
6907 : :
6908 : : static tree
6909 : : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
6910 : : enum tree_code code2, tree op2a, tree op2b, basic_block);
6911 : : static tree
6912 : : and_var_with_comparison (tree type, tree var, bool invert,
6913 : : enum tree_code code2, tree op2a, tree op2b,
6914 : : basic_block);
6915 : : static tree
6916 : : and_var_with_comparison_1 (tree type, gimple *stmt,
6917 : : enum tree_code code2, tree op2a, tree op2b,
6918 : : basic_block);
6919 : : static tree
6920 : : or_comparisons_1 (tree, enum tree_code code1, tree op1a, tree op1b,
6921 : : enum tree_code code2, tree op2a, tree op2b,
6922 : : basic_block);
6923 : : static tree
6924 : : or_var_with_comparison (tree, tree var, bool invert,
6925 : : enum tree_code code2, tree op2a, tree op2b,
6926 : : basic_block);
6927 : : static tree
6928 : : or_var_with_comparison_1 (tree, gimple *stmt,
6929 : : enum tree_code code2, tree op2a, tree op2b,
6930 : : basic_block);
6931 : :
6932 : : /* Helper function for and_comparisons_1: try to simplify the AND of the
6933 : : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
6934 : : If INVERT is true, invert the value of the VAR before doing the AND.
6935 : : Return NULL_EXPR if we can't simplify this to a single expression. */
6936 : :
6937 : : static tree
6938 : 248444 : and_var_with_comparison (tree type, tree var, bool invert,
6939 : : enum tree_code code2, tree op2a, tree op2b,
6940 : : basic_block outer_cond_bb)
6941 : : {
6942 : 248444 : tree t;
6943 : 248444 : gimple *stmt = SSA_NAME_DEF_STMT (var);
6944 : :
6945 : : /* We can only deal with variables whose definitions are assignments. */
6946 : 248444 : if (!is_gimple_assign (stmt))
6947 : : return NULL_TREE;
6948 : :
6949 : : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
6950 : : !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
6951 : : Then we only have to consider the simpler non-inverted cases. */
6952 : 247790 : if (invert)
6953 : 150983 : t = or_var_with_comparison_1 (type, stmt,
6954 : : invert_tree_comparison (code2, false),
6955 : : op2a, op2b, outer_cond_bb);
6956 : : else
6957 : 96807 : t = and_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
6958 : : outer_cond_bb);
6959 : 247790 : return canonicalize_bool (t, invert);
6960 : : }
6961 : :
6962 : : /* Try to simplify the AND of the ssa variable defined by the assignment
6963 : : STMT with the comparison specified by (OP2A CODE2 OP2B).
6964 : : Return NULL_EXPR if we can't simplify this to a single expression. */
6965 : :
6966 : : static tree
6967 : 121340 : and_var_with_comparison_1 (tree type, gimple *stmt,
6968 : : enum tree_code code2, tree op2a, tree op2b,
6969 : : basic_block outer_cond_bb)
6970 : : {
6971 : 121340 : tree var = gimple_assign_lhs (stmt);
6972 : 121340 : tree true_test_var = NULL_TREE;
6973 : 121340 : tree false_test_var = NULL_TREE;
6974 : 121340 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
6975 : :
6976 : : /* Check for identities like (var AND (var == 0)) => false. */
6977 : 121340 : if (TREE_CODE (op2a) == SSA_NAME
6978 : 121340 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
6979 : : {
6980 : 8756 : if ((code2 == NE_EXPR && integer_zerop (op2b))
6981 : 25555 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
6982 : : {
6983 : 7916 : true_test_var = op2a;
6984 : 7916 : if (var == true_test_var)
6985 : : return var;
6986 : : }
6987 : 5885 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
6988 : 19597 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
6989 : : {
6990 : 3466 : false_test_var = op2a;
6991 : 3466 : if (var == false_test_var)
6992 : 0 : return boolean_false_node;
6993 : : }
6994 : : }
6995 : :
6996 : : /* If the definition is a comparison, recurse on it. */
6997 : 121340 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
6998 : : {
6999 : 4043 : tree t = and_comparisons_1 (type, innercode,
7000 : : gimple_assign_rhs1 (stmt),
7001 : : gimple_assign_rhs2 (stmt),
7002 : : code2,
7003 : : op2a,
7004 : : op2b, outer_cond_bb);
7005 : 4043 : if (t)
7006 : : return t;
7007 : : }
7008 : :
7009 : : /* If the definition is an AND or OR expression, we may be able to
7010 : : simplify by reassociating. */
7011 : 121334 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
7012 : 121334 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
7013 : : {
7014 : 15010 : tree inner1 = gimple_assign_rhs1 (stmt);
7015 : 15010 : tree inner2 = gimple_assign_rhs2 (stmt);
7016 : 15010 : gimple *s;
7017 : 15010 : tree t;
7018 : 15010 : tree partial = NULL_TREE;
7019 : 15010 : bool is_and = (innercode == BIT_AND_EXPR);
7020 : :
7021 : : /* Check for boolean identities that don't require recursive examination
7022 : : of inner1/inner2:
7023 : : inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
7024 : : inner1 AND (inner1 OR inner2) => inner1
7025 : : !inner1 AND (inner1 AND inner2) => false
7026 : : !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
7027 : : Likewise for similar cases involving inner2. */
7028 : 15010 : if (inner1 == true_test_var)
7029 : 0 : return (is_and ? var : inner1);
7030 : 15010 : else if (inner2 == true_test_var)
7031 : 0 : return (is_and ? var : inner2);
7032 : 15010 : else if (inner1 == false_test_var)
7033 : 0 : return (is_and
7034 : 0 : ? boolean_false_node
7035 : 0 : : and_var_with_comparison (type, inner2, false, code2, op2a,
7036 : 0 : op2b, outer_cond_bb));
7037 : 15010 : else if (inner2 == false_test_var)
7038 : 0 : return (is_and
7039 : 0 : ? boolean_false_node
7040 : 0 : : and_var_with_comparison (type, inner1, false, code2, op2a,
7041 : 0 : op2b, outer_cond_bb));
7042 : :
7043 : : /* Next, redistribute/reassociate the AND across the inner tests.
7044 : : Compute the first partial result, (inner1 AND (op2a code op2b)) */
7045 : 15010 : if (TREE_CODE (inner1) == SSA_NAME
7046 : 15010 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
7047 : 14140 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7048 : 28357 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7049 : : gimple_assign_rhs1 (s),
7050 : : gimple_assign_rhs2 (s),
7051 : : code2, op2a, op2b,
7052 : : outer_cond_bb)))
7053 : : {
7054 : : /* Handle the AND case, where we are reassociating:
7055 : : (inner1 AND inner2) AND (op2a code2 op2b)
7056 : : => (t AND inner2)
7057 : : If the partial result t is a constant, we win. Otherwise
7058 : : continue on to try reassociating with the other inner test. */
7059 : 60 : if (is_and)
7060 : : {
7061 : 3 : if (integer_onep (t))
7062 : : return inner2;
7063 : 3 : else if (integer_zerop (t))
7064 : 0 : return boolean_false_node;
7065 : : }
7066 : :
7067 : : /* Handle the OR case, where we are redistributing:
7068 : : (inner1 OR inner2) AND (op2a code2 op2b)
7069 : : => (t OR (inner2 AND (op2a code2 op2b))) */
7070 : 57 : else if (integer_onep (t))
7071 : 0 : return boolean_true_node;
7072 : :
7073 : : /* Save partial result for later. */
7074 : : partial = t;
7075 : : }
7076 : :
7077 : : /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
7078 : 15010 : if (TREE_CODE (inner2) == SSA_NAME
7079 : 15010 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
7080 : 14682 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7081 : 28468 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7082 : : gimple_assign_rhs1 (s),
7083 : : gimple_assign_rhs2 (s),
7084 : : code2, op2a, op2b,
7085 : : outer_cond_bb)))
7086 : : {
7087 : : /* Handle the AND case, where we are reassociating:
7088 : : (inner1 AND inner2) AND (op2a code2 op2b)
7089 : : => (inner1 AND t) */
7090 : 88 : if (is_and)
7091 : : {
7092 : 14 : if (integer_onep (t))
7093 : : return inner1;
7094 : 14 : else if (integer_zerop (t))
7095 : 1 : return boolean_false_node;
7096 : : /* If both are the same, we can apply the identity
7097 : : (x AND x) == x. */
7098 : 13 : else if (partial && same_bool_result_p (t, partial))
7099 : : return t;
7100 : : }
7101 : :
7102 : : /* Handle the OR case. where we are redistributing:
7103 : : (inner1 OR inner2) AND (op2a code2 op2b)
7104 : : => (t OR (inner1 AND (op2a code2 op2b)))
7105 : : => (t OR partial) */
7106 : : else
7107 : : {
7108 : 74 : if (integer_onep (t))
7109 : 0 : return boolean_true_node;
7110 : 74 : else if (partial)
7111 : : {
7112 : : /* We already got a simplification for the other
7113 : : operand to the redistributed OR expression. The
7114 : : interesting case is when at least one is false.
7115 : : Or, if both are the same, we can apply the identity
7116 : : (x OR x) == x. */
7117 : 14 : if (integer_zerop (partial))
7118 : : return t;
7119 : 14 : else if (integer_zerop (t))
7120 : : return partial;
7121 : 12 : else if (same_bool_result_p (t, partial))
7122 : : return t;
7123 : : }
7124 : : }
7125 : : }
7126 : : }
7127 : : return NULL_TREE;
7128 : : }
7129 : :
7130 : : /* Try to simplify the AND of two comparisons defined by
7131 : : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
7132 : : If this can be done without constructing an intermediate value,
7133 : : return the resulting tree; otherwise NULL_TREE is returned.
7134 : : This function is deliberately asymmetric as it recurses on SSA_DEFs
7135 : : in the first comparison but not the second. */
7136 : :
7137 : : static tree
7138 : 906135 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7139 : : enum tree_code code2, tree op2a, tree op2b,
7140 : : basic_block outer_cond_bb)
7141 : : {
7142 : 906135 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
7143 : :
7144 : : /* First check for ((x CODE1 y) AND (x CODE2 y)). */
7145 : 906135 : if (operand_equal_p (op1a, op2a, 0)
7146 : 906135 : && operand_equal_p (op1b, op2b, 0))
7147 : : {
7148 : : /* Result will be either NULL_TREE, or a combined comparison. */
7149 : 3713 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7150 : : TRUTH_ANDIF_EXPR, code1, code2,
7151 : : truth_type, op1a, op1b);
7152 : 3713 : if (t)
7153 : : return t;
7154 : : }
7155 : :
7156 : : /* Likewise the swapped case of the above. */
7157 : 905238 : if (operand_equal_p (op1a, op2b, 0)
7158 : 905238 : && operand_equal_p (op1b, op2a, 0))
7159 : : {
7160 : : /* Result will be either NULL_TREE, or a combined comparison. */
7161 : 0 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7162 : : TRUTH_ANDIF_EXPR, code1,
7163 : : swap_tree_comparison (code2),
7164 : : truth_type, op1a, op1b);
7165 : 0 : if (t)
7166 : : return t;
7167 : : }
7168 : :
7169 : : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
7170 : : NAME's definition is a truth value. See if there are any simplifications
7171 : : that can be done against the NAME's definition. */
7172 : 905238 : if (TREE_CODE (op1a) == SSA_NAME
7173 : 904901 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
7174 : 1553204 : && (integer_zerop (op1b) || integer_onep (op1b)))
7175 : : {
7176 : 187554 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
7177 : 332288 : || (code1 == NE_EXPR && integer_onep (op1b)));
7178 : 307127 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
7179 : 307127 : switch (gimple_code (stmt))
7180 : : {
7181 : 243582 : case GIMPLE_ASSIGN:
7182 : : /* Try to simplify by copy-propagating the definition. */
7183 : 243582 : return and_var_with_comparison (type, op1a, invert, code2, op2a,
7184 : 243582 : op2b, outer_cond_bb);
7185 : :
7186 : 31998 : case GIMPLE_PHI:
7187 : : /* If every argument to the PHI produces the same result when
7188 : : ANDed with the second comparison, we win.
7189 : : Do not do this unless the type is bool since we need a bool
7190 : : result here anyway. */
7191 : 31998 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
7192 : : {
7193 : : tree result = NULL_TREE;
7194 : : unsigned i;
7195 : 15484 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
7196 : : {
7197 : 15484 : tree arg = gimple_phi_arg_def (stmt, i);
7198 : :
7199 : : /* If this PHI has itself as an argument, ignore it.
7200 : : If all the other args produce the same result,
7201 : : we're still OK. */
7202 : 15484 : if (arg == gimple_phi_result (stmt))
7203 : 0 : continue;
7204 : 15484 : else if (TREE_CODE (arg) == INTEGER_CST)
7205 : : {
7206 : 9554 : if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
7207 : : {
7208 : 5624 : if (!result)
7209 : 2348 : result = boolean_false_node;
7210 : 3276 : else if (!integer_zerop (result))
7211 : : return NULL_TREE;
7212 : : }
7213 : 3930 : else if (!result)
7214 : 2131 : result = fold_build2 (code2, boolean_type_node,
7215 : : op2a, op2b);
7216 : 1799 : else if (!same_bool_comparison_p (result,
7217 : : code2, op2a, op2b))
7218 : : return NULL_TREE;
7219 : : }
7220 : 5930 : else if (TREE_CODE (arg) == SSA_NAME
7221 : 5930 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
7222 : : {
7223 : 5927 : tree temp;
7224 : 5927 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
7225 : : /* In simple cases we can look through PHI nodes,
7226 : : but we have to be careful with loops.
7227 : : See PR49073. */
7228 : 5927 : if (! dom_info_available_p (CDI_DOMINATORS)
7229 : 5927 : || gimple_bb (def_stmt) == gimple_bb (stmt)
7230 : 11854 : || dominated_by_p (CDI_DOMINATORS,
7231 : 5927 : gimple_bb (def_stmt),
7232 : 5927 : gimple_bb (stmt)))
7233 : 1065 : return NULL_TREE;
7234 : 4862 : temp = and_var_with_comparison (type, arg, invert, code2,
7235 : : op2a, op2b,
7236 : : outer_cond_bb);
7237 : 4862 : if (!temp)
7238 : : return NULL_TREE;
7239 : 0 : else if (!result)
7240 : : result = temp;
7241 : 0 : else if (!same_bool_result_p (result, temp))
7242 : : return NULL_TREE;
7243 : : }
7244 : : else
7245 : : return NULL_TREE;
7246 : : }
7247 : : return result;
7248 : : }
7249 : :
7250 : : default:
7251 : : break;
7252 : : }
7253 : : }
7254 : : return NULL_TREE;
7255 : : }
7256 : :
7257 : : static basic_block fosa_bb;
7258 : : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
7259 : : static tree
7260 : 31506162 : follow_outer_ssa_edges (tree val)
7261 : : {
7262 : 31506162 : if (TREE_CODE (val) == SSA_NAME
7263 : 31506162 : && !SSA_NAME_IS_DEFAULT_DEF (val))
7264 : : {
7265 : 31078237 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
7266 : 31078237 : if (!def_bb
7267 : 9989478 : || def_bb == fosa_bb
7268 : 36433057 : || (dom_info_available_p (CDI_DOMINATORS)
7269 : 5354820 : && (def_bb == fosa_bb
7270 : 5354820 : || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
7271 : 28079508 : return val;
7272 : : /* We cannot temporarily rewrite stmts with undefined overflow
7273 : : behavior, so avoid expanding them. */
7274 : 5987016 : if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
7275 : 222029 : || POINTER_TYPE_P (TREE_TYPE (val)))
7276 : 5901064 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
7277 : : return NULL_TREE;
7278 : 2004650 : flow_sensitive_info_storage storage;
7279 : 2004650 : storage.save_and_clear (val);
7280 : : /* If the definition does not dominate fosa_bb temporarily reset
7281 : : flow-sensitive info. */
7282 : 2004650 : fosa_unwind->safe_push (std::make_pair (val, storage));
7283 : 2004650 : return val;
7284 : : }
7285 : : return val;
7286 : : }
7287 : :
7288 : : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
7289 : : : try to simplify the AND/OR of the ssa variable VAR with the comparison
7290 : : specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
7291 : : simplify this to a single expression. As we are going to lower the cost
7292 : : of building SSA names / gimple stmts significantly, we need to allocate
7293 : : them ont the stack. This will cause the code to be a bit ugly. */
7294 : :
7295 : : static tree
7296 : 943526 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
7297 : : enum tree_code code1,
7298 : : tree op1a, tree op1b,
7299 : : enum tree_code code2, tree op2a,
7300 : : tree op2b,
7301 : : basic_block outer_cond_bb)
7302 : : {
7303 : : /* Allocate gimple stmt1 on the stack. */
7304 : 943526 : gassign *stmt1
7305 : 943526 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7306 : 943526 : gimple_init (stmt1, GIMPLE_ASSIGN, 3);
7307 : 943526 : gimple_assign_set_rhs_code (stmt1, code1);
7308 : 943526 : gimple_assign_set_rhs1 (stmt1, op1a);
7309 : 943526 : gimple_assign_set_rhs2 (stmt1, op1b);
7310 : 943526 : gimple_set_bb (stmt1, NULL);
7311 : :
7312 : : /* Allocate gimple stmt2 on the stack. */
7313 : 943526 : gassign *stmt2
7314 : 943526 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7315 : 943526 : gimple_init (stmt2, GIMPLE_ASSIGN, 3);
7316 : 943526 : gimple_assign_set_rhs_code (stmt2, code2);
7317 : 943526 : gimple_assign_set_rhs1 (stmt2, op2a);
7318 : 943526 : gimple_assign_set_rhs2 (stmt2, op2b);
7319 : 943526 : gimple_set_bb (stmt2, NULL);
7320 : :
7321 : : /* Allocate SSA names(lhs1) on the stack. */
7322 : 943526 : alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
7323 : 943526 : tree lhs1 = (tree) &lhs1buf[0];
7324 : 943526 : memset (lhs1, 0, sizeof (tree_ssa_name));
7325 : 943526 : TREE_SET_CODE (lhs1, SSA_NAME);
7326 : 943526 : TREE_TYPE (lhs1) = type;
7327 : 943526 : init_ssa_name_imm_use (lhs1);
7328 : :
7329 : : /* Allocate SSA names(lhs2) on the stack. */
7330 : 943526 : alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
7331 : 943526 : tree lhs2 = (tree) &lhs2buf[0];
7332 : 943526 : memset (lhs2, 0, sizeof (tree_ssa_name));
7333 : 943526 : TREE_SET_CODE (lhs2, SSA_NAME);
7334 : 943526 : TREE_TYPE (lhs2) = type;
7335 : 943526 : init_ssa_name_imm_use (lhs2);
7336 : :
7337 : 943526 : gimple_assign_set_lhs (stmt1, lhs1);
7338 : 943526 : gimple_assign_set_lhs (stmt2, lhs2);
7339 : :
7340 : 943526 : gimple_match_op op (gimple_match_cond::UNCOND, code,
7341 : : type, gimple_assign_lhs (stmt1),
7342 : 943526 : gimple_assign_lhs (stmt2));
7343 : 943526 : fosa_bb = outer_cond_bb;
7344 : 943526 : auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
7345 : 943526 : fosa_unwind = &unwind_stack;
7346 : 1317511 : if (op.resimplify (NULL, (!outer_cond_bb
7347 : : ? follow_all_ssa_edges : follow_outer_ssa_edges)))
7348 : : {
7349 : 2077 : fosa_unwind = NULL;
7350 : 7522 : for (auto p : unwind_stack)
7351 : 1291 : p.second.restore (p.first);
7352 : 2077 : if (gimple_simplified_result_is_gimple_val (&op))
7353 : : {
7354 : 1480 : tree res = op.ops[0];
7355 : 1480 : if (res == lhs1)
7356 : 1122 : return build2 (code1, type, op1a, op1b);
7357 : 358 : else if (res == lhs2)
7358 : 330 : return build2 (code2, type, op2a, op2b);
7359 : : else
7360 : : return res;
7361 : : }
7362 : 597 : else if (op.code.is_tree_code ()
7363 : 597 : && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
7364 : : {
7365 : 597 : tree op0 = op.ops[0];
7366 : 597 : tree op1 = op.ops[1];
7367 : 597 : if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
7368 : : return NULL_TREE; /* not simple */
7369 : :
7370 : 597 : return build2 ((enum tree_code)op.code, op.type, op0, op1);
7371 : : }
7372 : : }
7373 : 941449 : fosa_unwind = NULL;
7374 : 4827706 : for (auto p : unwind_stack)
7375 : 2003359 : p.second.restore (p.first);
7376 : :
7377 : 941449 : return NULL_TREE;
7378 : 943526 : }
7379 : :
7380 : : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
7381 : : conversion. Reject loads if LOAD is NULL, otherwise set *LOAD if a
7382 : : converting load is found. */
7383 : :
7384 : : static bool
7385 : 1971405 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
7386 : : {
7387 : 1971405 : bool ret = false;
7388 : :
7389 : 1971405 : if (TREE_CODE (t) == SSA_NAME
7390 : 1971405 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7391 : 1116180 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7392 : : {
7393 : 1032710 : bool load_p = gimple_assign_load_p (def);
7394 : 1032710 : if (load_p && !load)
7395 : : return false;
7396 : 763510 : switch (gimple_assign_rhs_code (def))
7397 : : {
7398 : 11407 : CASE_CONVERT:
7399 : 11407 : op[0] = gimple_assign_rhs1 (def);
7400 : 11407 : ret = true;
7401 : 11407 : break;
7402 : :
7403 : 1632 : case VIEW_CONVERT_EXPR:
7404 : 1632 : op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
7405 : 1632 : ret = true;
7406 : 1632 : break;
7407 : :
7408 : : default:
7409 : : break;
7410 : : }
7411 : :
7412 : 13039 : if (ret && load_p)
7413 : 0 : *load = def;
7414 : : }
7415 : :
7416 : : return ret;
7417 : : }
7418 : :
7419 : : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
7420 : : binary expression with code CODE. */
7421 : :
7422 : : static bool
7423 : 1997223 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
7424 : : {
7425 : 1997223 : if (TREE_CODE (t) == SSA_NAME
7426 : 1997223 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7427 : 1140988 : if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7428 : 1053163 : if (gimple_assign_rhs_code (def) == code)
7429 : : {
7430 : 38563 : op[0] = gimple_assign_rhs1 (def);
7431 : 38563 : op[1] = gimple_assign_rhs2 (def);
7432 : 38563 : return true;
7433 : : }
7434 : : return false;
7435 : : }
7436 : : /* Subroutine for fold_truth_andor_1: decode a field reference.
7437 : :
7438 : : If *PEXP is a comparison reference, we return the innermost reference.
7439 : :
7440 : : *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
7441 : : set to the starting bit number.
7442 : :
7443 : : *PVOLATILEP is set to 1 if the any expression encountered is volatile;
7444 : : otherwise it is not changed.
7445 : :
7446 : : *PUNSIGNEDP is set to the signedness of the field.
7447 : :
7448 : : *PREVERSEP is set to the storage order of the field.
7449 : :
7450 : : *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. If
7451 : : *PAND_MASK is initially set to a mask with nonzero precision, that mask is
7452 : : combined with the found mask, or adjusted in precision to match.
7453 : :
7454 : : *PSIGNBIT is set to TRUE if, before clipping to *PBITSIZE, the mask
7455 : : encompassed bits that corresponded to extensions of the sign bit.
7456 : :
7457 : : *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
7458 : : case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
7459 : : *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
7460 : : and the left-hand operand of the XOR will be decoded. If *PXORP is TRUE,
7461 : : PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
7462 : : right-hand operand of the XOR will be decoded.
7463 : :
7464 : : *LOAD is set to the load stmt of the innermost reference, if any,
7465 : : *and NULL otherwise.
7466 : :
7467 : : LOC[0..3] are filled in as conversion, masking, shifting and loading
7468 : : operations are located.
7469 : :
7470 : : Return 0 if this is not a component reference or is one that we can't
7471 : : do anything with. */
7472 : :
7473 : : static tree
7474 : 705018 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
7475 : : HOST_WIDE_INT *pbitpos,
7476 : : bool *punsignedp, bool *preversep, bool *pvolatilep,
7477 : : wide_int *pand_mask, bool *psignbit,
7478 : : bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
7479 : : gimple **pload, location_t loc[4])
7480 : : {
7481 : 705018 : tree exp = *pexp;
7482 : 705018 : tree outer_type = 0;
7483 : 705018 : wide_int and_mask;
7484 : 705018 : tree inner, offset;
7485 : 705018 : int shiftrt = 0;
7486 : 705018 : tree res_ops[2];
7487 : 705018 : machine_mode mode;
7488 : 705018 : bool convert_before_shift = false;
7489 : 705018 : bool signbit = false;
7490 : 705018 : bool xorp = false;
7491 : 705018 : tree xor_cmp_op;
7492 : 705018 : wide_int xor_and_mask;
7493 : 705018 : gimple *load = NULL;
7494 : :
7495 : : /* All the optimizations using this function assume integer fields.
7496 : : There are problems with FP fields since the type_for_size call
7497 : : below can fail for, e.g., XFmode. */
7498 : 705018 : if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
7499 : : return NULL_TREE;
7500 : :
7501 : : /* Drop casts, saving only the outermost type, effectively used in
7502 : : the compare. We can deal with at most one conversion, and it may
7503 : : appear at various points in the chain of recognized preparation
7504 : : statements. Earlier optimizers will often have already dropped
7505 : : unneeded extensions, but they may survive, as in PR118046. ???
7506 : : Can we do better and allow multiple conversions, perhaps taking
7507 : : note of the narrowest intermediate type, sign extensions and
7508 : : whatnot? */
7509 : 665745 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7510 : : {
7511 : 12779 : outer_type = TREE_TYPE (exp);
7512 : 12779 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7513 : 12779 : exp = res_ops[0];
7514 : : }
7515 : :
7516 : : /* Recognize and save a masking operation. Combine it with an
7517 : : incoming mask. */
7518 : 665745 : if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
7519 : 665745 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7520 : : {
7521 : 23654 : loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
7522 : 23654 : exp = res_ops[0];
7523 : 23654 : and_mask = wi::to_wide (res_ops[1]);
7524 : 23654 : unsigned prec_in = pand_mask->get_precision ();
7525 : 23654 : if (prec_in)
7526 : : {
7527 : 52 : unsigned prec_op = and_mask.get_precision ();
7528 : 52 : if (prec_in >= prec_op)
7529 : : {
7530 : 52 : if (prec_in > prec_op)
7531 : 0 : and_mask = wide_int::from (and_mask, prec_in, UNSIGNED);
7532 : 52 : and_mask &= *pand_mask;
7533 : : }
7534 : : else
7535 : 0 : and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
7536 : : }
7537 : : }
7538 : : else
7539 : 642091 : and_mask = *pand_mask;
7540 : :
7541 : : /* Turn (a ^ b) [!]= 0 into a [!]= b. */
7542 : 665745 : if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
7543 : : {
7544 : : /* No location recorded for this one, it's entirely subsumed by the
7545 : : compare. */
7546 : 8012 : if (*pxorp)
7547 : : {
7548 : 3997 : exp = res_ops[1];
7549 : 3997 : gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
7550 : : }
7551 : 4015 : else if (!pxor_cmp_op)
7552 : : /* Not much we can do when xor appears in the right-hand compare
7553 : : operand. */
7554 : : return NULL_TREE;
7555 : 4003 : else if (integer_zerop (*pxor_cmp_op))
7556 : : {
7557 : 3997 : xorp = true;
7558 : 3997 : exp = res_ops[0];
7559 : 3997 : xor_cmp_op = *pexp;
7560 : 3997 : xor_and_mask = *pand_mask;
7561 : : }
7562 : : }
7563 : :
7564 : : /* Another chance to drop conversions. */
7565 : 665733 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7566 : : {
7567 : 248 : outer_type = TREE_TYPE (exp);
7568 : 248 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7569 : 248 : exp = res_ops[0];
7570 : : }
7571 : :
7572 : : /* Take note of shifts. */
7573 : 665733 : if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
7574 : 665733 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7575 : : {
7576 : 324 : loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
7577 : 324 : exp = res_ops[0];
7578 : 324 : if (!tree_fits_shwi_p (res_ops[1]))
7579 : : return NULL_TREE;
7580 : 324 : shiftrt = tree_to_shwi (res_ops[1]);
7581 : 324 : if (shiftrt <= 0)
7582 : : return NULL_TREE;
7583 : : }
7584 : :
7585 : : /* Yet another chance to drop conversions. This one is allowed to
7586 : : match a converting load, subsuming the load identification block
7587 : : below. */
7588 : 665733 : if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
7589 : : {
7590 : 12 : outer_type = TREE_TYPE (exp);
7591 : 12 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7592 : 12 : if (load)
7593 : 0 : loc[3] = gimple_location (load);
7594 : 12 : exp = res_ops[0];
7595 : : /* This looks backwards, but we're going back the def chain, so if we
7596 : : find the conversion here, after finding a shift, that's because the
7597 : : convert appears before the shift, and we should thus adjust the bit
7598 : : pos and size because of the shift after adjusting it due to type
7599 : : conversion. */
7600 : 12 : convert_before_shift = true;
7601 : : }
7602 : :
7603 : : /* Identify the load, if there is one. */
7604 : 665733 : if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
7605 : : {
7606 : 379841 : gimple *def = SSA_NAME_DEF_STMT (exp);
7607 : 379841 : if (gimple_assign_load_p (def))
7608 : : {
7609 : 303996 : loc[3] = gimple_location (def);
7610 : 303996 : load = def;
7611 : 303996 : exp = gimple_assign_rhs1 (def);
7612 : : }
7613 : : }
7614 : :
7615 : : /* Identify the relevant bits. */
7616 : 665733 : poly_int64 poly_bitsize, poly_bitpos;
7617 : 665733 : int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
7618 : 665733 : inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
7619 : : &mode, &unsignedp, &reversep, &volatilep);
7620 : :
7621 : 665733 : HOST_WIDE_INT bs, bp;
7622 : 665733 : if (!poly_bitsize.is_constant (&bs)
7623 : 665733 : || !poly_bitpos.is_constant (&bp)
7624 : 665733 : || bs <= shiftrt
7625 : 665733 : || offset != 0
7626 : 664431 : || TREE_CODE (inner) == PLACEHOLDER_EXPR
7627 : : /* Reject out-of-bound accesses (PR79731, PR118514). */
7628 : 664431 : || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
7629 : 664414 : || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
7630 : 387382 : && !type_has_mode_precision_p (TREE_TYPE (inner))))
7631 : 34004 : return NULL_TREE;
7632 : :
7633 : : /* Adjust shifts... */
7634 : 631729 : if (convert_before_shift
7635 : 631729 : && outer_type && bs > TYPE_PRECISION (outer_type))
7636 : : {
7637 : 3 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7638 : 3 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7639 : 0 : bp += excess;
7640 : : bs -= excess;
7641 : : }
7642 : :
7643 : 631729 : if (shiftrt)
7644 : : {
7645 : : /* Punt if we're shifting by more than the loaded bitfield (after
7646 : : adjustment), or if there's a shift after a change of signedness, punt.
7647 : : When comparing this field with a constant, we'll check that the
7648 : : constant is a proper sign- or zero-extension (depending on signedness)
7649 : : of a value that would fit in the selected portion of the bitfield. A
7650 : : shift after a change of signedness would make the extension
7651 : : non-uniform, and we can't deal with that (yet ???). See
7652 : : gcc.dg/field-merge-22.c for a test that would go wrong. */
7653 : 318 : if (bs <= shiftrt
7654 : 318 : || (convert_before_shift
7655 : 12 : && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
7656 : : return NULL_TREE;
7657 : 308 : if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7658 : 308 : bp += shiftrt;
7659 : 308 : bs -= shiftrt;
7660 : : }
7661 : :
7662 : : /* ... and bit position. */
7663 : 631719 : if (!convert_before_shift
7664 : 631719 : && outer_type && bs > TYPE_PRECISION (outer_type))
7665 : : {
7666 : 6486 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7667 : 6486 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7668 : 0 : bp += excess;
7669 : : bs -= excess;
7670 : : }
7671 : :
7672 : : /* If the number of bits in the reference is the same as the bitsize of
7673 : : the outer type, then the outer type gives the signedness. Otherwise
7674 : : (in case of a small bitfield) the signedness is unchanged. */
7675 : 631719 : if (outer_type && bs == TYPE_PRECISION (outer_type))
7676 : 9204 : unsignedp = TYPE_UNSIGNED (outer_type);
7677 : :
7678 : : /* Make the mask the expected width. */
7679 : 631719 : if (and_mask.get_precision () != 0)
7680 : : {
7681 : : /* If the AND_MASK encompasses bits that would be extensions of
7682 : : the sign bit, set SIGNBIT. */
7683 : 27277 : if (!unsignedp
7684 : 2062 : && and_mask.get_precision () > bs
7685 : 29381 : && (and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
7686 : : signbit = true;
7687 : 27277 : and_mask = wide_int::from (and_mask, bs, UNSIGNED);
7688 : : }
7689 : :
7690 : 631719 : *pexp = exp;
7691 : 631719 : *pload = load;
7692 : 631719 : *pbitsize = bs;
7693 : 631719 : *pbitpos = bp;
7694 : 631719 : *punsignedp = unsignedp;
7695 : 631719 : *preversep = reversep;
7696 : 631719 : *pvolatilep = volatilep;
7697 : 631719 : *psignbit = signbit;
7698 : 631719 : *pand_mask = and_mask;
7699 : 631719 : if (xorp)
7700 : : {
7701 : 3997 : *pxorp = xorp;
7702 : 3997 : *pxor_cmp_op = xor_cmp_op;
7703 : 3997 : *pxor_and_mask = xor_and_mask;
7704 : : }
7705 : :
7706 : : return inner;
7707 : 705018 : }
7708 : :
7709 : : /* Return the one bitpos within bit extents L or R that is at an
7710 : : ALIGN-bit alignment boundary, or -1 if there is more than one such
7711 : : boundary, if there isn't any, or if there is any such boundary
7712 : : between the extents. L and R are given by bitpos and bitsize. If
7713 : : it doesn't return -1, there are two consecutive ALIGN-bit words
7714 : : that contain both extents, and at least one of the extents
7715 : : straddles across the returned alignment boundary. */
7716 : :
7717 : : static inline HOST_WIDE_INT
7718 : 68048 : compute_split_boundary_from_align (HOST_WIDE_INT align,
7719 : : HOST_WIDE_INT l_bitpos,
7720 : : HOST_WIDE_INT l_bitsize,
7721 : : HOST_WIDE_INT r_bitpos,
7722 : : HOST_WIDE_INT r_bitsize)
7723 : : {
7724 : 68048 : HOST_WIDE_INT amask = ~(align - 1);
7725 : :
7726 : 68048 : HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
7727 : 68048 : HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
7728 : :
7729 : 68048 : HOST_WIDE_INT boundary = (end_bit - 1) & amask;
7730 : :
7731 : : /* Make sure we're crossing no more than one alignment boundary.
7732 : :
7733 : : ??? We don't have logic to recombine loads of two adjacent
7734 : : fields that each crosses a different alignment boundary, so
7735 : : as to load the middle word only once, if other words can't be
7736 : : otherwise recombined. */
7737 : 68048 : if (boundary - first_bit > align)
7738 : : return -1;
7739 : :
7740 : 13798 : HOST_WIDE_INT l_start_word = l_bitpos & amask;
7741 : 13798 : HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
7742 : :
7743 : 13798 : HOST_WIDE_INT r_start_word = r_bitpos & amask;
7744 : 13798 : HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
7745 : :
7746 : : /* If neither field straddles across an alignment boundary, it's no
7747 : : use to even try to merge them. */
7748 : 13798 : if (l_start_word == l_end_word && r_start_word == r_end_word)
7749 : 13503 : return -1;
7750 : :
7751 : : return boundary;
7752 : : }
7753 : :
7754 : : /* Make a bit_field_ref. If POINT is NULL, return the BIT_FIELD_REF.
7755 : : Otherwise, build and insert a load stmt before POINT, and return
7756 : : the SSA_NAME. ??? Rewrite LOAD in terms of the bitfield? */
7757 : :
7758 : : static tree
7759 : 4350 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
7760 : : HOST_WIDE_INT bitsize, poly_int64 bitpos,
7761 : : bool unsignedp, bool reversep, gimple *point)
7762 : : {
7763 : 4350 : if (point && loc == UNKNOWN_LOCATION)
7764 : 18 : loc = gimple_location (point);
7765 : :
7766 : 4350 : tree ref = make_bit_field_ref (loc, unshare_expr (inner),
7767 : : unshare_expr (orig_inner),
7768 : : type, bitsize, bitpos,
7769 : : unsignedp, reversep);
7770 : 4350 : if (!point)
7771 : : return ref;
7772 : :
7773 : : /* If we're remaking the same load, reuse the SSA NAME it is already loaded
7774 : : into. */
7775 : 4211 : if (gimple_assign_load_p (point)
7776 : 4211 : && operand_equal_p (ref, gimple_assign_rhs1 (point)))
7777 : : {
7778 : 1717 : gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
7779 : : return gimple_assign_lhs (point);
7780 : : }
7781 : :
7782 : 2494 : gimple_seq stmts = NULL;
7783 : 2494 : tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
7784 : :
7785 : : /* We know the vuse is supposed to end up being the same as that at the
7786 : : original load at the insertion point, but if we don't set it, it will be a
7787 : : generic placeholder that only the global SSA update at the end of the pass
7788 : : would make equal, too late for us to use in further combinations. So go
7789 : : ahead and copy the vuse. */
7790 : :
7791 : 2494 : tree reaching_vuse = gimple_vuse (point);
7792 : 2494 : for (gimple_stmt_iterator i = gsi_start (stmts);
7793 : 5371 : !gsi_end_p (i); gsi_next (&i))
7794 : : {
7795 : 2877 : gimple *new_stmt = gsi_stmt (i);
7796 : 5754 : if (gimple_has_mem_ops (new_stmt))
7797 : 2877 : gimple_set_vuse (new_stmt, reaching_vuse);
7798 : : }
7799 : :
7800 : 2494 : gimple_stmt_iterator gsi = gsi_for_stmt (point);
7801 : 2494 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
7802 : 2494 : return ret;
7803 : : }
7804 : :
7805 : : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
7806 : : LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
7807 : : respectively, starting at BIT_POS, using reversed endianness if
7808 : : REVERSEP. Also initialize BITPOS (the starting position of each
7809 : : part into INNER), BITSIZ (the bit count starting at BITPOS),
7810 : : TOSHIFT[1] (the amount by which the part and its mask are to be
7811 : : shifted right to bring its least-significant bit to bit zero) and
7812 : : SHIFTED (the amount by which the part, by separate loading, has
7813 : : already been shifted right, but that the mask needs shifting to
7814 : : match). */
7815 : :
7816 : : static inline void
7817 : 295 : build_split_load (tree /* out */ ln_arg[2],
7818 : : HOST_WIDE_INT /* out */ bitpos[2],
7819 : : HOST_WIDE_INT /* out */ bitsiz[2],
7820 : : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
7821 : : HOST_WIDE_INT /* out */ shifted[2],
7822 : : location_t loc, tree inner, tree orig_inner,
7823 : : scalar_int_mode mode, scalar_int_mode mode2,
7824 : : HOST_WIDE_INT bit_pos, bool reversep,
7825 : : gimple *point[2])
7826 : : {
7827 : 295 : scalar_int_mode modes[2] = { mode, mode2 };
7828 : 295 : bitsiz[0] = GET_MODE_BITSIZE (mode);
7829 : 295 : bitsiz[1] = GET_MODE_BITSIZE (mode2);
7830 : :
7831 : 885 : for (int i = 0; i < 2; i++)
7832 : : {
7833 : 590 : tree type = lang_hooks.types.type_for_mode (modes[i], 1);
7834 : 590 : if (!type)
7835 : : {
7836 : 0 : type = build_nonstandard_integer_type (bitsiz[0], 1);
7837 : 0 : gcc_assert (type);
7838 : : }
7839 : 590 : bitpos[i] = bit_pos;
7840 : 1180 : ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
7841 : 590 : type, bitsiz[i],
7842 : 590 : bit_pos, 1, reversep, point[i]);
7843 : 590 : bit_pos += bitsiz[i];
7844 : : }
7845 : :
7846 : 295 : toshift[1] = toshift[0];
7847 : 295 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7848 : : {
7849 : 3 : shifted[0] = bitsiz[1];
7850 : 3 : shifted[1] = 0;
7851 : 3 : toshift[0] = 0;
7852 : : }
7853 : : else
7854 : : {
7855 : 292 : shifted[1] = bitsiz[0];
7856 : 292 : shifted[0] = 0;
7857 : 292 : toshift[1] = 0;
7858 : : }
7859 : 295 : }
7860 : :
7861 : : /* Make arrangements to split at bit BOUNDARY a single loaded word
7862 : : (with REVERSEP bit order) LN_ARG[0], to be shifted right by
7863 : : TOSHIFT[0] to bring the field of interest to the least-significant
7864 : : bit. The expectation is that the same loaded word will be
7865 : : propagated from part 0 to part 1, with just different shifting and
7866 : : masking to extract both parts. MASK is not expected to do more
7867 : : than masking out the bits that belong to the other part. See
7868 : : build_split_load for more information on the other fields. */
7869 : :
7870 : : static inline void
7871 : 51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
7872 : : HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
7873 : : HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
7874 : : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
7875 : : HOST_WIDE_INT /* out */ shifted[2],
7876 : : wide_int /* out */ mask[2],
7877 : : HOST_WIDE_INT boundary, bool reversep)
7878 : : {
7879 : 51 : unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
7880 : :
7881 : 51 : ln_arg[1] = ln_arg[0];
7882 : 51 : bitpos[1] = bitpos[0];
7883 : 51 : bitsiz[1] = bitsiz[0];
7884 : 51 : shifted[1] = shifted[0] = 0;
7885 : :
7886 : 51 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7887 : : {
7888 : 3 : toshift[1] = toshift[0];
7889 : 3 : toshift[0] = bitpos[0] + bitsiz[0] - boundary;
7890 : 3 : mask[0] = wi::mask (toshift[0], true, prec);
7891 : 3 : mask[1] = wi::mask (toshift[0], false, prec);
7892 : : }
7893 : : else
7894 : : {
7895 : 48 : toshift[1] = boundary - bitpos[1];
7896 : 48 : mask[1] = wi::mask (toshift[1], true, prec);
7897 : 48 : mask[0] = wi::mask (toshift[1], false, prec);
7898 : : }
7899 : 51 : }
7900 : :
7901 : : /* Find ways of folding logical expressions of LHS and RHS:
7902 : :
7903 : : Try to merge two comparisons to nearby fields.
7904 : :
7905 : : For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
7906 : : at once, we can do this with a comparison against the object ANDed with the
7907 : : a mask.
7908 : :
7909 : : If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
7910 : : operations to do this with one comparison, loading both fields from P at
7911 : : once, and likewise from Q.
7912 : :
7913 : : Herein, loading at once means loading from within the same alignment
7914 : : boundary for the enclosing object. If (packed) fields cross such alignment
7915 : : boundaries, we may still recombine the compares, so that loads do not cross
7916 : : the boundaries.
7917 : :
7918 : : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
7919 : : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
7920 : :
7921 : : TRUTH_TYPE is the type of the logical operand.
7922 : :
7923 : : LHS is denoted as LL_ARG LCODE LR_ARG.
7924 : :
7925 : : RHS is denoted as RL_ARG RCODE RR_ARG.
7926 : :
7927 : : LHS is assumed to dominate RHS.
7928 : :
7929 : : Combined loads are inserted next to preexisting loads, once we determine
7930 : : that the combination is viable, and the combined condition references new
7931 : : SSA_NAMEs that hold the loaded values. Since the original loads are
7932 : : verified to have the same gimple_vuse, the insertion point doesn't matter
7933 : : for correctness. ??? The loads may be a lot earlier than the compares, and
7934 : : it's conceivable that one or two loads for RHS appear before those for LHS.
7935 : : It could be advantageous to try to place the loads optimally, taking
7936 : : advantage of knowing whether RHS is accessed before LHS, or that both are
7937 : : accessed before both compares, but we don't do that (yet?).
7938 : :
7939 : : SEPARATEP should be NULL if the combined condition must be returned as a
7940 : : single expression, even if it is a compound condition. This must only be
7941 : : done if LHS and RHS are adjacent, without intervening conditions, and the
7942 : : combined condition is to replace RHS, while LHS is dropped altogether.
7943 : :
7944 : : Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
7945 : : replaced by a part of the compound condition that could replace RHS, while
7946 : : the returned expression replaces LHS. This works whether or not LHS and RHS
7947 : : are adjacent, as long as there aren't VDEFs or other side effects between
7948 : : them.
7949 : :
7950 : : If the "words" accessed by RHS are already accessed by LHS, this won't
7951 : : matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
7952 : : be set to the compares that should take RHS's place. By "words" we mean
7953 : : contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
7954 : : object's type.
7955 : :
7956 : : We return the simplified tree or 0 if no optimization is possible. */
7957 : :
7958 : : tree
7959 : 284603 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
7960 : : location_t lloc, enum tree_code lcode,
7961 : : tree ll_arg, tree lr_arg,
7962 : : location_t rloc, enum tree_code rcode,
7963 : : tree rl_arg, tree rr_arg,
7964 : : tree *separatep)
7965 : : {
7966 : : /* If this is the "or" of two comparisons, we can do something if
7967 : : the comparisons are NE_EXPR. If this is the "and", we can do something
7968 : : if the comparisons are EQ_EXPR. I.e.,
7969 : : (a->b == 2 && a->c == 4) can become (a->new == NEW).
7970 : :
7971 : : WANTED_CODE is this operation code. For single bit fields, we can
7972 : : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
7973 : : comparison for one-bit fields. */
7974 : :
7975 : 284603 : enum tree_code orig_code = code;
7976 : 284603 : enum tree_code wanted_code;
7977 : 284603 : tree ll_inner, lr_inner, rl_inner, rr_inner;
7978 : 284603 : gimple *ll_load, *lr_load, *rl_load, *rr_load;
7979 : 284603 : HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
7980 : 284603 : HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
7981 : 284603 : HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
7982 : 284603 : HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
7983 : 284603 : HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
7984 : 284603 : bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
7985 : 284603 : bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
7986 : 284603 : bool ll_signbit, lr_signbit, rl_signbit, rr_signbit;
7987 : 284603 : scalar_int_mode lnmode, lnmode2, rnmode;
7988 : 284603 : wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
7989 : 284603 : wide_int l_const, r_const;
7990 : 284603 : tree lntype, rntype, result;
7991 : 284603 : HOST_WIDE_INT first_bit, end_bit;
7992 : 284603 : bool volatilep;
7993 : 284603 : bool l_split_load;
7994 : :
7995 : : /* These are indexed by: conv, mask, shft, load. */
7996 : 284603 : location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
7997 : 284603 : location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
7998 : 284603 : location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
7999 : 284603 : location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8000 : :
8001 : 284603 : gcc_checking_assert (!separatep || !*separatep);
8002 : :
8003 : : /* Start by getting the comparison codes. Fail if anything is volatile.
8004 : : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
8005 : : it were surrounded with a NE_EXPR. */
8006 : :
8007 : 284603 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
8008 : 284603 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
8009 : : return 0;
8010 : :
8011 : : /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
8012 : : given by our caller to denote conditions from different blocks. */
8013 : 284603 : switch (code)
8014 : : {
8015 : : case TRUTH_AND_EXPR:
8016 : : case TRUTH_ANDIF_EXPR:
8017 : : code = TRUTH_AND_EXPR;
8018 : : break;
8019 : :
8020 : 0 : case TRUTH_OR_EXPR:
8021 : 0 : case TRUTH_ORIF_EXPR:
8022 : 0 : code = TRUTH_OR_EXPR;
8023 : 0 : break;
8024 : :
8025 : : default:
8026 : : return 0;
8027 : : }
8028 : :
8029 : : /* Prepare to turn compares of signed quantities with zero into sign-bit
8030 : : tests. We need not worry about *_reversep here for these compare
8031 : : rewrites: loads will have already been reversed before compares. Save the
8032 : : precision, because [lr]l_arg may change and we won't be able to tell how
8033 : : wide it was originally. */
8034 : 284603 : unsigned lsignbit = 0, rsignbit = 0;
8035 : 284603 : if ((lcode == LT_EXPR || lcode == GE_EXPR)
8036 : 12590 : && integer_zerop (lr_arg)
8037 : 3649 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8038 : 288252 : && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
8039 : : {
8040 : 3649 : lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
8041 : 3649 : lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8042 : : }
8043 : : /* Turn compares of unsigned quantities with powers of two into
8044 : : equality tests of masks. */
8045 : 280954 : else if ((lcode == LT_EXPR || lcode == GE_EXPR)
8046 : 8941 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8047 : 6477 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8048 : 4542 : && TREE_CODE (lr_arg) == INTEGER_CST
8049 : 280954 : && wi::popcount (wi::to_wide (lr_arg)) == 1)
8050 : : {
8051 : 0 : ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
8052 : 0 : lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8053 : 0 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8054 : : }
8055 : : /* Turn compares of unsigned quantities with powers of two minus one
8056 : : into equality tests of masks. */
8057 : 561908 : else if ((lcode == LE_EXPR || lcode == GT_EXPR)
8058 : 28116 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8059 : 27921 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8060 : 22326 : && TREE_CODE (lr_arg) == INTEGER_CST
8061 : 590024 : && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
8062 : : {
8063 : 3807 : ll_and_mask = ~wi::to_wide (lr_arg);
8064 : 3807 : lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8065 : 3807 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8066 : : }
8067 : : /* Likewise for the second compare. */
8068 : 284603 : if ((rcode == LT_EXPR || rcode == GE_EXPR)
8069 : 21582 : && integer_zerop (rr_arg)
8070 : 1740 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8071 : 286343 : && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
8072 : : {
8073 : 1740 : rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
8074 : 1740 : rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8075 : : }
8076 : 282863 : else if ((rcode == LT_EXPR || rcode == GE_EXPR)
8077 : 19842 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8078 : 17203 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8079 : 3527 : && TREE_CODE (rr_arg) == INTEGER_CST
8080 : 282863 : && wi::popcount (wi::to_wide (rr_arg)) == 1)
8081 : : {
8082 : 0 : rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
8083 : 0 : rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8084 : 0 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8085 : : }
8086 : 565726 : else if ((rcode == LE_EXPR || rcode == GT_EXPR)
8087 : 37869 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8088 : 37382 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8089 : 26626 : && TREE_CODE (rr_arg) == INTEGER_CST
8090 : 603595 : && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
8091 : : {
8092 : 4477 : rl_and_mask = ~wi::to_wide (rr_arg);
8093 : 4477 : rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8094 : 4477 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8095 : : }
8096 : :
8097 : : /* See if the comparisons can be merged. Then get all the parameters for
8098 : : each side. */
8099 : :
8100 : 284603 : if ((lcode != EQ_EXPR && lcode != NE_EXPR)
8101 : 250583 : || (rcode != EQ_EXPR && rcode != NE_EXPR))
8102 : : return 0;
8103 : :
8104 : 224583 : ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
8105 : 224583 : volatilep = 0;
8106 : 224583 : bool l_xor = false, r_xor = false;
8107 : 224583 : ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
8108 : : &ll_unsignedp, &ll_reversep, &volatilep,
8109 : : &ll_and_mask, &ll_signbit,
8110 : : &l_xor, &lr_arg, &lr_and_mask,
8111 : : &ll_load, ll_loc);
8112 : 224583 : if (!ll_inner)
8113 : : return 0;
8114 : 165771 : lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
8115 : : &lr_unsignedp, &lr_reversep, &volatilep,
8116 : : &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
8117 : : &lr_load, lr_loc);
8118 : 165771 : if (!lr_inner)
8119 : : return 0;
8120 : 162737 : rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
8121 : : &rl_unsignedp, &rl_reversep, &volatilep,
8122 : : &rl_and_mask, &rl_signbit,
8123 : : &r_xor, &rr_arg, &rr_and_mask,
8124 : : &rl_load, rl_loc);
8125 : 162737 : if (!rl_inner)
8126 : : return 0;
8127 : 151927 : rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
8128 : : &rr_unsignedp, &rr_reversep, &volatilep,
8129 : : &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
8130 : : &rr_load, rr_loc);
8131 : 151927 : if (!rr_inner)
8132 : : return 0;
8133 : :
8134 : : /* It must be true that the inner operation on the lhs of each
8135 : : comparison must be the same if we are to be able to do anything.
8136 : : Then see if we have constants. If not, the same must be true for
8137 : : the rhs's. If one is a load and the other isn't, we have to be
8138 : : conservative and avoid the optimization, otherwise we could get
8139 : : SRAed fields wrong. */
8140 : 151284 : if (volatilep)
8141 : : return 0;
8142 : :
8143 : 151284 : if (ll_reversep != rl_reversep
8144 : 151284 : || ! operand_equal_p (ll_inner, rl_inner, 0))
8145 : : {
8146 : : /* Try swapping the operands. */
8147 : 76387 : if (ll_reversep != rr_reversep || rsignbit
8148 : 152432 : || !operand_equal_p (ll_inner, rr_inner, 0))
8149 : 75387 : return 0;
8150 : :
8151 : 1027 : rcode = swap_tree_comparison (rcode);
8152 : 1027 : std::swap (rl_arg, rr_arg);
8153 : 1027 : std::swap (rl_inner, rr_inner);
8154 : 1027 : std::swap (rl_bitsize, rr_bitsize);
8155 : 1027 : std::swap (rl_bitpos, rr_bitpos);
8156 : 1027 : std::swap (rl_unsignedp, rr_unsignedp);
8157 : 1027 : std::swap (rl_reversep, rr_reversep);
8158 : 1027 : std::swap (rl_and_mask, rr_and_mask);
8159 : 1027 : std::swap (rl_signbit, rr_signbit);
8160 : 1027 : std::swap (rl_load, rr_load);
8161 : 1027 : std::swap (rl_loc, rr_loc);
8162 : : }
8163 : :
8164 : 149788 : if ((ll_load && rl_load)
8165 : 297570 : ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
8166 : 2006 : : (!ll_load != !rl_load))
8167 : : return 0;
8168 : :
8169 : : /* ??? Can we do anything with these? */
8170 : 75546 : if (lr_signbit || rr_signbit)
8171 : : return 0;
8172 : :
8173 : : /* If the mask encompassed extensions of the sign bit before
8174 : : clipping, try to include the sign bit in the test. If we're not
8175 : : comparing with zero, don't even try to deal with it (for now?).
8176 : : If we've already commited to a sign test, the extended (before
8177 : : clipping) mask could already be messing with it. */
8178 : 75546 : if (ll_signbit)
8179 : : {
8180 : 4 : if (!integer_zerop (lr_arg) || lsignbit)
8181 : 0 : return 0;
8182 : 4 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8183 : 4 : if (!ll_and_mask.get_precision ())
8184 : 0 : ll_and_mask = sign;
8185 : : else
8186 : 4 : ll_and_mask |= sign;
8187 : 4 : }
8188 : :
8189 : 75546 : if (rl_signbit)
8190 : : {
8191 : 4 : if (!integer_zerop (rr_arg) || rsignbit)
8192 : 1 : return 0;
8193 : 3 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8194 : 3 : if (!rl_and_mask.get_precision ())
8195 : 0 : rl_and_mask = sign;
8196 : : else
8197 : 3 : rl_and_mask |= sign;
8198 : 3 : }
8199 : :
8200 : 75545 : if (TREE_CODE (lr_arg) == INTEGER_CST
8201 : 64392 : && TREE_CODE (rr_arg) == INTEGER_CST)
8202 : : {
8203 : 63986 : l_const = wi::to_wide (lr_arg);
8204 : : /* We don't expect masks on constants, but if there are any, apply
8205 : : them now. */
8206 : 63986 : if (lr_and_mask.get_precision ())
8207 : 0 : l_const &= wide_int::from (lr_and_mask,
8208 : 0 : l_const.get_precision (), UNSIGNED);
8209 : 63986 : r_const = wi::to_wide (rr_arg);
8210 : 63986 : if (rr_and_mask.get_precision ())
8211 : 0 : r_const &= wide_int::from (rr_and_mask,
8212 : 0 : r_const.get_precision (), UNSIGNED);
8213 : 63986 : lr_reversep = ll_reversep;
8214 : : }
8215 : 11559 : else if (lr_reversep != rr_reversep
8216 : 11559 : || ! operand_equal_p (lr_inner, rr_inner, 0)
8217 : 21908 : || ((lr_load && rr_load)
8218 : 30966 : ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
8219 : 27 : : (!lr_load != !rr_load)))
8220 : 1240 : return 0;
8221 : :
8222 : : /* If we found sign tests, finish turning them into bit tests. */
8223 : :
8224 : 74305 : if (lsignbit)
8225 : : {
8226 : 59 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8227 : : /* If ll_arg is zero-extended and we're testing the sign bit, we know
8228 : : what the result should be. Shifting the sign bit out of sign will get
8229 : : us to mask the entire field out, yielding zero, i.e., the sign bit of
8230 : : the zero-extended value. We know the masked value is being compared
8231 : : with zero, so the compare will get us the result we're looking
8232 : : for: TRUE if EQ_EXPR, FALSE if NE_EXPR. */
8233 : 59 : if (lsignbit > ll_bitsize && ll_unsignedp)
8234 : 1 : sign <<= 1;
8235 : 59 : if (!ll_and_mask.get_precision ())
8236 : 58 : ll_and_mask = sign;
8237 : : else
8238 : 1 : ll_and_mask &= sign;
8239 : 59 : if (l_xor)
8240 : : {
8241 : 1 : if (ll_bitsize != lr_bitsize)
8242 : 1 : return 0;
8243 : 0 : if (!lr_and_mask.get_precision ())
8244 : 0 : lr_and_mask = sign;
8245 : : else
8246 : 0 : lr_and_mask &= sign;
8247 : 0 : if (l_const.get_precision ())
8248 : 0 : l_const &= wide_int::from (lr_and_mask,
8249 : 0 : l_const.get_precision (), UNSIGNED);
8250 : : }
8251 : 59 : }
8252 : :
8253 : 74304 : if (rsignbit)
8254 : : {
8255 : 186 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8256 : 186 : if (rsignbit > rl_bitsize && rl_unsignedp)
8257 : 0 : sign <<= 1;
8258 : 186 : if (!rl_and_mask.get_precision ())
8259 : 186 : rl_and_mask = sign;
8260 : : else
8261 : 0 : rl_and_mask &= sign;
8262 : 186 : if (r_xor)
8263 : : {
8264 : 4 : if (rl_bitsize != rr_bitsize)
8265 : 0 : return 0;
8266 : 4 : if (!rr_and_mask.get_precision ())
8267 : 4 : rr_and_mask = sign;
8268 : : else
8269 : 0 : rr_and_mask &= sign;
8270 : 4 : if (r_const.get_precision ())
8271 : 0 : r_const &= wide_int::from (rr_and_mask,
8272 : 0 : r_const.get_precision (), UNSIGNED);
8273 : : }
8274 : 186 : }
8275 : :
8276 : : /* If either comparison code is not correct for our logical operation,
8277 : : fail. However, we can convert a one-bit comparison against zero into
8278 : : the opposite comparison against that bit being set in the field. */
8279 : :
8280 : 74304 : wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
8281 : 74304 : if (lcode != wanted_code)
8282 : : {
8283 : 4466 : if (l_const.get_precision ()
8284 : 4348 : && l_const == 0
8285 : 1195 : && ll_and_mask.get_precision ()
8286 : 4751 : && wi::popcount (ll_and_mask) == 1)
8287 : : {
8288 : : /* Make the left operand unsigned, since we are only interested
8289 : : in the value of one bit. Otherwise we are doing the wrong
8290 : : thing below. */
8291 : 160 : ll_unsignedp = 1;
8292 : 160 : l_const = ll_and_mask;
8293 : : }
8294 : : else
8295 : 4306 : return 0;
8296 : : }
8297 : :
8298 : : /* This is analogous to the code for l_const above. */
8299 : 69998 : if (rcode != wanted_code)
8300 : : {
8301 : 707 : if (r_const.get_precision ()
8302 : 707 : && r_const == 0
8303 : 680 : && rl_and_mask.get_precision ()
8304 : 1321 : && wi::popcount (rl_and_mask) == 1)
8305 : : {
8306 : 447 : rl_unsignedp = 1;
8307 : 447 : r_const = rl_and_mask;
8308 : : }
8309 : : else
8310 : 260 : return 0;
8311 : : }
8312 : :
8313 : : /* This will be bumped to 2 if any of the field pairs crosses an
8314 : : alignment boundary, so the merged compare has to be done in two
8315 : : parts. */
8316 : 209214 : int parts = 1;
8317 : : /* Set to true if the second combined compare should come first,
8318 : : e.g., because the second original compare accesses a word that
8319 : : the first one doesn't, and the combined compares access those in
8320 : : cmp[0]. */
8321 : 209214 : bool first1 = false;
8322 : : /* Set to true if the first original compare is not the one being
8323 : : split. */
8324 : 209214 : bool maybe_separate = false;
8325 : :
8326 : : /* The following 2-dimensional arrays use the first index to
8327 : : identify left(0)- vs right(1)-hand compare operands, and the
8328 : : second one to identify merged compare parts. */
8329 : : /* The memory loads or constants to be compared. */
8330 : : tree ld_arg[2][2];
8331 : : /* The first bit of the corresponding inner object that the
8332 : : corresponding LD_ARG covers. */
8333 : : HOST_WIDE_INT bitpos[2][2];
8334 : : /* The bit count starting at BITPOS that the corresponding LD_ARG
8335 : : covers. */
8336 : : HOST_WIDE_INT bitsiz[2][2];
8337 : : /* The number of bits by which LD_ARG has already been shifted
8338 : : right, WRT mask. */
8339 : : HOST_WIDE_INT shifted[2][2];
8340 : : /* The number of bits by which both LD_ARG and MASK need shifting to
8341 : : bring its least-significant bit to bit zero. */
8342 : : HOST_WIDE_INT toshift[2][2];
8343 : : /* An additional mask to be applied to LD_ARG, to remove any bits
8344 : : that may have been loaded for use in another compare, but that
8345 : : don't belong in the corresponding compare. */
8346 : 836856 : wide_int xmask[2][2] = {};
8347 : :
8348 : : /* The combined compare or compares. */
8349 : 69738 : tree cmp[2];
8350 : :
8351 : : /* Consider we're comparing two non-contiguous fields of packed
8352 : : structs, both aligned at 32-bit boundaries:
8353 : :
8354 : : ll_arg: an 8-bit field at offset 0
8355 : : lr_arg: a 16-bit field at offset 2
8356 : :
8357 : : rl_arg: an 8-bit field at offset 1
8358 : : rr_arg: a 16-bit field at offset 3
8359 : :
8360 : : We'll have r_split_load, because rr_arg straddles across an
8361 : : alignment boundary.
8362 : :
8363 : : We'll want to have:
8364 : :
8365 : : bitpos = { { 0, 0 }, { 0, 32 } }
8366 : : bitsiz = { { 32, 32 }, { 32, 8 } }
8367 : :
8368 : : And, for little-endian:
8369 : :
8370 : : shifted = { { 0, 0 }, { 0, 32 } }
8371 : : toshift = { { 0, 24 }, { 0, 0 } }
8372 : :
8373 : : Or, for big-endian:
8374 : :
8375 : : shifted = { { 0, 0 }, { 8, 0 } }
8376 : : toshift = { { 8, 0 }, { 0, 0 } }
8377 : : */
8378 : :
8379 : : /* See if we can find a mode that contains both fields being compared on
8380 : : the left. If we can't, fail. Otherwise, update all constants and masks
8381 : : to be relative to a field of that size. */
8382 : 69738 : first_bit = MIN (ll_bitpos, rl_bitpos);
8383 : 69738 : end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
8384 : 69738 : HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
8385 : 69738 : poly_uint64 ll_end_region = 0;
8386 : 69738 : if (TYPE_SIZE (TREE_TYPE (ll_inner))
8387 : 69738 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
8388 : 69738 : ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
8389 : 69738 : if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
8390 : 69738 : ll_align, BITS_PER_WORD, volatilep, &lnmode))
8391 : : l_split_load = false;
8392 : : /* ??? If ll and rl share the same load, reuse that?
8393 : : See PR 118206 -> gcc.dg/field-merge-18.c */
8394 : : else
8395 : : {
8396 : : /* Consider the possibility of recombining loads if any of the
8397 : : fields straddles across an alignment boundary, so that either
8398 : : part can be loaded along with the other field. Since we
8399 : : limit access modes to BITS_PER_WORD, don't exceed that,
8400 : : otherwise on a 32-bit host and a 64-bit-aligned data
8401 : : structure, we'll fail the above for a field that straddles
8402 : : across two words, and would fail here for not even trying to
8403 : : split it at between 32-bit words. */
8404 : 66703 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8405 : 68184 : (MIN (ll_align, BITS_PER_WORD),
8406 : : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8407 : :
8408 : 66703 : if (boundary < 0
8409 : 210 : || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
8410 : : ll_align, BITS_PER_WORD, volatilep, &lnmode)
8411 : 66874 : || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
8412 : 171 : ll_align, BITS_PER_WORD, volatilep, &lnmode2))
8413 : : {
8414 : 67974 : if (ll_align <= BITS_PER_WORD)
8415 : : return 0;
8416 : :
8417 : : /* As a last resort, try double-word access modes. This
8418 : : enables us to deal with misaligned double-word fields
8419 : : that straddle across 3 separate words. */
8420 : 1219 : boundary = compute_split_boundary_from_align
8421 : 1307 : (MIN (ll_align, 2 * BITS_PER_WORD),
8422 : : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8423 : 1219 : if (boundary < 0
8424 : 0 : || !get_best_mode (boundary - first_bit, first_bit,
8425 : : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8426 : : volatilep, &lnmode)
8427 : 1219 : || !get_best_mode (end_bit - boundary, boundary,
8428 : 0 : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8429 : : volatilep, &lnmode2))
8430 : 1219 : return 0;
8431 : : }
8432 : :
8433 : : /* If we can't have a single load, but can with two, figure out whether
8434 : : the two compares can be separated, i.e., whether the entirety of the
8435 : : first original compare is encompassed by the entirety of the first
8436 : : combined compare. If the first original compare is past the alignment
8437 : : boundary, arrange to compare that range first, by setting first1
8438 : : (meaning make cmp[1] first, instead of cmp[0]). */
8439 : 171 : l_split_load = true;
8440 : 171 : parts = 2;
8441 : 171 : if (ll_bitpos >= boundary)
8442 : : maybe_separate = first1 = true;
8443 : 126 : else if (ll_bitpos + ll_bitsize <= boundary)
8444 : 26 : maybe_separate = true;
8445 : : }
8446 : :
8447 : 3206 : lnbitsize = GET_MODE_BITSIZE (lnmode);
8448 : 3206 : lnbitpos = first_bit & ~ (lnbitsize - 1);
8449 : : /* Avoid situations that the code below can't handle. */
8450 : 3206 : if (lnbitpos < 0)
8451 : : return 0;
8452 : :
8453 : : /* Choose the type for the combined compare. Even if we're splitting loads,
8454 : : make it wide enough to hold both. */
8455 : 3206 : if (l_split_load)
8456 : 342 : lnbitsize += GET_MODE_BITSIZE (lnmode2);
8457 : 3206 : lntype = build_nonstandard_integer_type (lnbitsize, 1);
8458 : 3206 : if (!lntype)
8459 : : return NULL_TREE;
8460 : 3206 : lnprec = TYPE_PRECISION (lntype);
8461 : 3206 : xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
8462 : :
8463 : : /* Adjust bit ranges for reverse endianness. */
8464 : 3206 : if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8465 : : {
8466 : 6 : xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
8467 : 6 : xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
8468 : : }
8469 : :
8470 : : /* Adjust masks to match the positions in the combined lntype. */
8471 : 6412 : wide_int ll_mask, rl_mask, r_mask;
8472 : 3206 : if (ll_and_mask.get_precision ())
8473 : 4126 : ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
8474 : 2063 : xll_bitpos);
8475 : : else
8476 : 1143 : ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
8477 : 3206 : if (rl_and_mask.get_precision ())
8478 : 3876 : rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
8479 : 1938 : xrl_bitpos);
8480 : : else
8481 : 1268 : rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
8482 : :
8483 : : /* When we set l_const, we also set r_const. */
8484 : 3206 : gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
8485 : :
8486 : : /* Adjust right-hand constants in both original comparisons to match width
8487 : : and bit position. */
8488 : 3206 : if (l_const.get_precision ())
8489 : : {
8490 : : /* Before clipping upper bits of the right-hand operand of the compare,
8491 : : check that they're sign or zero extensions, depending on how the
8492 : : left-hand operand would be extended. If it is unsigned, or if there's
8493 : : a mask that zeroes out extension bits, whether because we've checked
8494 : : for upper bits in the mask and did not set ll_signbit, or because the
8495 : : sign bit itself is masked out, check that the right-hand operand is
8496 : : zero-extended. */
8497 : 1791 : bool l_non_ext_bits = false;
8498 : 1791 : if (ll_bitsize < lr_bitsize)
8499 : : {
8500 : 34 : wide_int zext = wi::zext (l_const, ll_bitsize);
8501 : 68 : if ((ll_unsignedp
8502 : 26 : || (ll_and_mask.get_precision ()
8503 : 4 : && (!ll_signbit
8504 : 42 : || ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
8505 : 8 : == 0)))
8506 : 128 : ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
8507 : 34 : l_const = zext;
8508 : : else
8509 : : l_non_ext_bits = true;
8510 : 34 : }
8511 : : /* We're doing bitwise equality tests, so don't bother with sign
8512 : : extensions. */
8513 : 1791 : l_const = wide_int::from (l_const, lnprec, UNSIGNED);
8514 : 1791 : if (ll_and_mask.get_precision ())
8515 : 1071 : l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
8516 : 1791 : l_const <<= xll_bitpos;
8517 : 5373 : if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
8518 : : {
8519 : 0 : warning_at (lloc, OPT_Wtautological_compare,
8520 : : "comparison is always %d", wanted_code == NE_EXPR);
8521 : :
8522 : 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8523 : : }
8524 : :
8525 : : /* Before clipping upper bits of the right-hand operand of the compare,
8526 : : check that they're sign or zero extensions, depending on how the
8527 : : left-hand operand would be extended. */
8528 : 1791 : bool r_non_ext_bits = false;
8529 : 1791 : if (rl_bitsize < rr_bitsize)
8530 : : {
8531 : 34 : wide_int zext = wi::zext (r_const, rl_bitsize);
8532 : 68 : if ((rl_unsignedp
8533 : 17 : || (rl_and_mask.get_precision ()
8534 : 10 : && (!rl_signbit
8535 : 40 : || ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
8536 : 6 : == 0)))
8537 : 119 : ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
8538 : 34 : r_const = zext;
8539 : : else
8540 : : r_non_ext_bits = true;
8541 : 34 : }
8542 : 1791 : r_const = wide_int::from (r_const, lnprec, UNSIGNED);
8543 : 1791 : if (rl_and_mask.get_precision ())
8544 : 991 : r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
8545 : 1791 : r_const <<= xrl_bitpos;
8546 : 5373 : if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
8547 : : {
8548 : 0 : warning_at (rloc, OPT_Wtautological_compare,
8549 : : "comparison is always %d", wanted_code == NE_EXPR);
8550 : :
8551 : 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8552 : : }
8553 : :
8554 : : /* If there is something in common between the masks, those bits of the
8555 : : constants must be the same. If not, the combined condition cannot be
8556 : : met, and the result is known. Test for this to avoid generating
8557 : : incorrect code below. */
8558 : 1791 : wide_int mask = ll_mask & rl_mask;
8559 : 1791 : if (mask != 0
8560 : 1851 : && (l_const & mask) != (r_const & mask))
8561 : : {
8562 : 0 : if (wanted_code == NE_EXPR)
8563 : 0 : return constant_boolean_node (true, truth_type);
8564 : : else
8565 : 0 : return constant_boolean_node (false, truth_type);
8566 : : }
8567 : :
8568 : : /* The constants are combined so as to line up with the loaded field, so
8569 : : tentatively use the same parameters for the second combined
8570 : : compare. */
8571 : 1791 : ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
8572 : 1791 : toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
8573 : 1791 : shifted[1][0] = 0;
8574 : 1791 : bitpos[1][0] = lnbitpos;
8575 : 1791 : bitsiz[1][0] = lnbitsize;
8576 : :
8577 : 1791 : if (parts > 1)
8578 : 49 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8579 : : shifted[1], xmask[1],
8580 : 49 : lnbitpos + GET_MODE_BITSIZE (lnmode),
8581 : : lr_reversep);
8582 : :
8583 : : /* No masking needed, we know the full constants. */
8584 : 1791 : r_mask = wi::mask (0, true, lnprec);
8585 : :
8586 : : /* If the compiler thinks this is used uninitialized below, it's
8587 : : because it can't realize that parts can only be 2 when
8588 : : comparing with constants if l_split_load is also true. This
8589 : : just silences the warning. */
8590 : 1791 : rnbitpos = 0;
8591 : 1791 : }
8592 : :
8593 : : /* Likewise, if the right sides are not constant, align them for the combined
8594 : : compare. Also, disallow this optimization if a size, signedness or
8595 : : storage order mismatch occurs between the left and right sides. */
8596 : : else
8597 : : {
8598 : 1415 : if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
8599 : 1366 : || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
8600 : 1366 : || ll_reversep != lr_reversep
8601 : : /* Make sure the two fields on the right
8602 : : correspond to the left without being swapped. */
8603 : 1366 : || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
8604 : 283 : return 0;
8605 : :
8606 : 1134 : bool r_split_load;
8607 : 1134 : scalar_int_mode rnmode2;
8608 : :
8609 : : /* Figure out how to load the bits for the right-hand size of the
8610 : : combined compare. As in the left-hand size, we may have to split it,
8611 : : and then we use two separate compares. */
8612 : 1134 : first_bit = MIN (lr_bitpos, rr_bitpos);
8613 : 1134 : end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
8614 : 1134 : HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
8615 : 1134 : poly_uint64 lr_end_region = 0;
8616 : 1134 : if (TYPE_SIZE (TREE_TYPE (lr_inner))
8617 : 1134 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
8618 : 1134 : lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
8619 : 1134 : if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
8620 : 1134 : lr_align, BITS_PER_WORD, volatilep, &rnmode))
8621 : : {
8622 : : /* Consider the possibility of recombining loads if any of the
8623 : : fields straddles across an alignment boundary, so that either
8624 : : part can be loaded along with the other field. */
8625 : 126 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8626 : 126 : (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
8627 : :
8628 : 126 : if (boundary < 0
8629 : : /* If we're to split both, make sure the split point is
8630 : : the same. */
8631 : 124 : || (l_split_load
8632 : 122 : && (boundary - lr_bitpos
8633 : 122 : != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
8634 : 124 : || !get_best_mode (boundary - first_bit, first_bit,
8635 : : 0, lr_end_region,
8636 : 124 : lr_align, BITS_PER_WORD, volatilep, &rnmode)
8637 : 250 : || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
8638 : 124 : lr_align, BITS_PER_WORD, volatilep, &rnmode2))
8639 : 2 : return 0;
8640 : :
8641 : 124 : r_split_load = true;
8642 : 124 : parts = 2;
8643 : 124 : if (lr_bitpos >= boundary)
8644 : : maybe_separate = first1 = true;
8645 : 82 : else if (lr_bitpos + lr_bitsize <= boundary)
8646 : 23 : maybe_separate = true;
8647 : : }
8648 : : else
8649 : : r_split_load = false;
8650 : :
8651 : : /* Find a type that can hold the entire right-hand operand. */
8652 : 1132 : rnbitsize = GET_MODE_BITSIZE (rnmode);
8653 : 1132 : rnbitpos = first_bit & ~ (rnbitsize - 1);
8654 : 1132 : if (r_split_load)
8655 : 248 : rnbitsize += GET_MODE_BITSIZE (rnmode2);
8656 : 1132 : rntype = build_nonstandard_integer_type (rnbitsize, 1);
8657 : 1132 : if (!rntype)
8658 : : return 0;
8659 : 1132 : rnprec = TYPE_PRECISION (rntype);
8660 : 1132 : xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
8661 : :
8662 : : /* Adjust for reversed endianness. */
8663 : 1132 : if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8664 : : {
8665 : 0 : xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
8666 : 0 : xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
8667 : : }
8668 : :
8669 : : /* Adjust the masks to match the combined type, and combine them. */
8670 : 1132 : wide_int lr_mask, rr_mask;
8671 : 1132 : if (lr_and_mask.get_precision ())
8672 : 1974 : lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
8673 : 987 : xlr_bitpos);
8674 : : else
8675 : 145 : lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
8676 : 1132 : if (rr_and_mask.get_precision ())
8677 : 1884 : rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
8678 : 942 : xrr_bitpos);
8679 : : else
8680 : 190 : rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
8681 : 1132 : r_mask = lr_mask | rr_mask;
8682 : :
8683 : : /* Load the right-hand operand of the combined compare. */
8684 : 1132 : toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
8685 : 1132 : shifted[1][0] = 0;
8686 : :
8687 : 1132 : if (!r_split_load)
8688 : : {
8689 : 1008 : bitpos[1][0] = rnbitpos;
8690 : 1008 : bitsiz[1][0] = rnbitsize;
8691 : 1008 : ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
8692 : 1008 : rntype, rnbitsize, rnbitpos,
8693 : 1008 : lr_unsignedp || rr_unsignedp,
8694 : : lr_reversep, lr_load);
8695 : : }
8696 : :
8697 : : /* ... and the second part of the right-hand operand if needed. */
8698 : 1132 : if (parts > 1)
8699 : : {
8700 : 124 : if (r_split_load)
8701 : : {
8702 : 124 : gimple *point[2];
8703 : 124 : point[0] = lr_load;
8704 : 124 : point[1] = rr_load;
8705 : 124 : build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8706 : : shifted[1], rl_loc[3], lr_inner, lr_arg,
8707 : : rnmode, rnmode2, rnbitpos, lr_reversep, point);
8708 : : }
8709 : : else
8710 : 0 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8711 : : shifted[1], xmask[1],
8712 : 0 : lnbitpos + GET_MODE_BITSIZE (lnmode)
8713 : 0 : - ll_bitpos + lr_bitpos, lr_reversep);
8714 : : }
8715 : 1132 : }
8716 : :
8717 : : /* Now issue the loads for the left-hand combined operand/s. */
8718 : 5846 : wide_int l_mask = ll_mask | rl_mask;
8719 : 2923 : toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
8720 : 2923 : shifted[0][0] = 0;
8721 : :
8722 : 2923 : if (!l_split_load)
8723 : : {
8724 : 2752 : bitpos[0][0] = lnbitpos;
8725 : 2752 : bitsiz[0][0] = lnbitsize;
8726 : 2752 : ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
8727 : 2752 : lntype, lnbitsize, lnbitpos,
8728 : 2752 : ll_unsignedp || rl_unsignedp,
8729 : : ll_reversep, ll_load);
8730 : : }
8731 : :
8732 : 2923 : if (parts > 1)
8733 : : {
8734 : 173 : if (l_split_load)
8735 : : {
8736 : 171 : gimple *point[2];
8737 : 171 : point[0] = ll_load;
8738 : 171 : point[1] = rl_load;
8739 : 171 : build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
8740 : : shifted[0], rl_loc[3], ll_inner, ll_arg,
8741 : : lnmode, lnmode2, lnbitpos, ll_reversep, point);
8742 : : }
8743 : : else
8744 : 2 : reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
8745 : : shifted[0], xmask[0],
8746 : 2 : rnbitpos + GET_MODE_BITSIZE (rnmode)
8747 : 2 : - lr_bitpos + ll_bitpos, ll_reversep);
8748 : : }
8749 : :
8750 : : /* Compute the compares. */
8751 : 6019 : for (int i = 0; i < parts; i++)
8752 : : {
8753 : 3096 : tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
8754 : 9288 : wide_int mask[2] = { l_mask, r_mask };
8755 : 3096 : location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
8756 : :
8757 : : /* Figure out the masks, and unshare the original operands. */
8758 : 9288 : for (int j = 0; j < 2; j++)
8759 : : {
8760 : 6192 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
8761 : 6192 : op[j] = unshare_expr (op[j]);
8762 : :
8763 : : /* Mask out the bits belonging to the other part. */
8764 : 6192 : if (xmask[j][i].get_precision ())
8765 : 102 : mask[j] &= xmask[j][i];
8766 : :
8767 : 6192 : if (shifted[j][i])
8768 : : {
8769 : 295 : wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
8770 : 295 : mask[j] = wi::lrshift (mask[j], shift);
8771 : 295 : }
8772 : 6192 : mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
8773 : : }
8774 : :
8775 : : /* Line up the operands for a compare. */
8776 : 3096 : HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
8777 : :
8778 : 3096 : if (shift)
8779 : : {
8780 : 54 : int j;
8781 : 54 : if (shift > 0)
8782 : : j = 0;
8783 : : else
8784 : : {
8785 : 52 : j = 1;
8786 : 52 : shift = -shift;
8787 : : }
8788 : :
8789 : 54 : tree shiftsz = bitsize_int (shift);
8790 : 54 : op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
8791 : : op[j], shiftsz);
8792 : 54 : mask[j] = wi::lrshift (mask[j], shift);
8793 : : }
8794 : :
8795 : : /* Convert to the smaller type before masking out unwanted
8796 : : bits. */
8797 : 3096 : tree type = TREE_TYPE (op[0]);
8798 : 3096 : if (type != TREE_TYPE (op[1]))
8799 : : {
8800 : 236 : int j = (TYPE_PRECISION (type)
8801 : 236 : < TYPE_PRECISION (TREE_TYPE (op[1])));
8802 : 236 : if (!j)
8803 : 137 : type = TREE_TYPE (op[1]);
8804 : 236 : op[j] = fold_convert_loc (locs[j][0], type, op[j]);
8805 : 236 : mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
8806 : : }
8807 : :
8808 : : /* Apply masks. */
8809 : 9288 : for (int j = 0; j < 2; j++)
8810 : 6192 : if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
8811 : 2519 : op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
8812 : 5038 : op[j], wide_int_to_tree (type, mask[j]));
8813 : :
8814 : 6019 : cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
8815 : : op[0], op[1]);
8816 : 9288 : }
8817 : :
8818 : : /* Reorder the compares if needed. */
8819 : 2923 : if (first1)
8820 : 45 : std::swap (cmp[0], cmp[1]);
8821 : :
8822 : : /* Prepare to return the resulting compares. Combine two parts if
8823 : : needed. */
8824 : 2923 : if (parts == 1)
8825 : 2750 : result = cmp[0];
8826 : 173 : else if (!separatep || !maybe_separate)
8827 : : {
8828 : : /* Only fold if any of the cmp is known, otherwise we may lose the
8829 : : sequence point, and that may prevent further optimizations. */
8830 : 167 : if (TREE_CODE (cmp[0]) == INTEGER_CST
8831 : 131 : || TREE_CODE (cmp[1]) == INTEGER_CST)
8832 : 37 : result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
8833 : : else
8834 : 130 : result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
8835 : : }
8836 : : else
8837 : : {
8838 : 6 : result = cmp[0];
8839 : 6 : *separatep = cmp[1];
8840 : : }
8841 : :
8842 : 2923 : return result;
8843 : 284603 : }
8844 : :
8845 : : /* Try to simplify the AND of two comparisons, specified by
8846 : : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
8847 : : If this can be simplified to a single expression (without requiring
8848 : : introducing more SSA variables to hold intermediate values),
8849 : : return the resulting tree. Otherwise return NULL_TREE.
8850 : : If the result expression is non-null, it has boolean type. */
8851 : :
8852 : : tree
8853 : 451509 : maybe_fold_and_comparisons (tree type,
8854 : : enum tree_code code1, tree op1a, tree op1b,
8855 : : enum tree_code code2, tree op2a, tree op2b,
8856 : : basic_block outer_cond_bb)
8857 : : {
8858 : 451509 : if (tree t = and_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
8859 : : outer_cond_bb))
8860 : : return t;
8861 : :
8862 : 450583 : if (tree t = and_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
8863 : : outer_cond_bb))
8864 : : return t;
8865 : :
8866 : 450567 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
8867 : : op1a, op1b, code2, op2a,
8868 : : op2b, outer_cond_bb))
8869 : : return t;
8870 : :
8871 : : return NULL_TREE;
8872 : : }
8873 : :
8874 : : /* Helper function for or_comparisons_1: try to simplify the OR of the
8875 : : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
8876 : : If INVERT is true, invert the value of VAR before doing the OR.
8877 : : Return NULL_EXPR if we can't simplify this to a single expression. */
8878 : :
8879 : : static tree
8880 : 43736 : or_var_with_comparison (tree type, tree var, bool invert,
8881 : : enum tree_code code2, tree op2a, tree op2b,
8882 : : basic_block outer_cond_bb)
8883 : : {
8884 : 43736 : tree t;
8885 : 43736 : gimple *stmt = SSA_NAME_DEF_STMT (var);
8886 : :
8887 : : /* We can only deal with variables whose definitions are assignments. */
8888 : 43736 : if (!is_gimple_assign (stmt))
8889 : : return NULL_TREE;
8890 : :
8891 : : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
8892 : : !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
8893 : : Then we only have to consider the simpler non-inverted cases. */
8894 : 43617 : if (invert)
8895 : 24533 : t = and_var_with_comparison_1 (type, stmt,
8896 : : invert_tree_comparison (code2, false),
8897 : : op2a, op2b, outer_cond_bb);
8898 : : else
8899 : 19084 : t = or_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
8900 : : outer_cond_bb);
8901 : 43617 : return canonicalize_bool (t, invert);
8902 : : }
8903 : :
8904 : : /* Try to simplify the OR of the ssa variable defined by the assignment
8905 : : STMT with the comparison specified by (OP2A CODE2 OP2B).
8906 : : Return NULL_EXPR if we can't simplify this to a single expression. */
8907 : :
8908 : : static tree
8909 : 170067 : or_var_with_comparison_1 (tree type, gimple *stmt,
8910 : : enum tree_code code2, tree op2a, tree op2b,
8911 : : basic_block outer_cond_bb)
8912 : : {
8913 : 170067 : tree var = gimple_assign_lhs (stmt);
8914 : 170067 : tree true_test_var = NULL_TREE;
8915 : 170067 : tree false_test_var = NULL_TREE;
8916 : 170067 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
8917 : :
8918 : : /* Check for identities like (var OR (var != 0)) => true . */
8919 : 170067 : if (TREE_CODE (op2a) == SSA_NAME
8920 : 170067 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
8921 : : {
8922 : 15955 : if ((code2 == NE_EXPR && integer_zerop (op2b))
8923 : 51721 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
8924 : : {
8925 : 14366 : true_test_var = op2a;
8926 : 14366 : if (var == true_test_var)
8927 : : return var;
8928 : : }
8929 : 3037 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
8930 : 30926 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
8931 : : {
8932 : 7375 : false_test_var = op2a;
8933 : 7375 : if (var == false_test_var)
8934 : 0 : return boolean_true_node;
8935 : : }
8936 : : }
8937 : :
8938 : : /* If the definition is a comparison, recurse on it. */
8939 : 170067 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
8940 : : {
8941 : 846 : tree t = or_comparisons_1 (type, innercode,
8942 : : gimple_assign_rhs1 (stmt),
8943 : : gimple_assign_rhs2 (stmt),
8944 : : code2, op2a, op2b, outer_cond_bb);
8945 : 846 : if (t)
8946 : : return t;
8947 : : }
8948 : :
8949 : : /* If the definition is an AND or OR expression, we may be able to
8950 : : simplify by reassociating. */
8951 : 170038 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
8952 : 170038 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
8953 : : {
8954 : 40428 : tree inner1 = gimple_assign_rhs1 (stmt);
8955 : 40428 : tree inner2 = gimple_assign_rhs2 (stmt);
8956 : 40428 : gimple *s;
8957 : 40428 : tree t;
8958 : 40428 : tree partial = NULL_TREE;
8959 : 40428 : bool is_or = (innercode == BIT_IOR_EXPR);
8960 : :
8961 : : /* Check for boolean identities that don't require recursive examination
8962 : : of inner1/inner2:
8963 : : inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
8964 : : inner1 OR (inner1 AND inner2) => inner1
8965 : : !inner1 OR (inner1 OR inner2) => true
8966 : : !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
8967 : : */
8968 : 40428 : if (inner1 == true_test_var)
8969 : 0 : return (is_or ? var : inner1);
8970 : 40428 : else if (inner2 == true_test_var)
8971 : 0 : return (is_or ? var : inner2);
8972 : 40428 : else if (inner1 == false_test_var)
8973 : 0 : return (is_or
8974 : 0 : ? boolean_true_node
8975 : 0 : : or_var_with_comparison (type, inner2, false, code2, op2a,
8976 : 0 : op2b, outer_cond_bb));
8977 : 40428 : else if (inner2 == false_test_var)
8978 : 0 : return (is_or
8979 : 0 : ? boolean_true_node
8980 : 0 : : or_var_with_comparison (type, inner1, false, code2, op2a,
8981 : 0 : op2b, outer_cond_bb));
8982 : :
8983 : : /* Next, redistribute/reassociate the OR across the inner tests.
8984 : : Compute the first partial result, (inner1 OR (op2a code op2b)) */
8985 : 40428 : if (TREE_CODE (inner1) == SSA_NAME
8986 : 40428 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
8987 : 39434 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
8988 : 64979 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
8989 : : gimple_assign_rhs1 (s),
8990 : : gimple_assign_rhs2 (s),
8991 : : code2, op2a, op2b,
8992 : : outer_cond_bb)))
8993 : : {
8994 : : /* Handle the OR case, where we are reassociating:
8995 : : (inner1 OR inner2) OR (op2a code2 op2b)
8996 : : => (t OR inner2)
8997 : : If the partial result t is a constant, we win. Otherwise
8998 : : continue on to try reassociating with the other inner test. */
8999 : 746 : if (is_or)
9000 : : {
9001 : 35 : if (integer_onep (t))
9002 : 0 : return boolean_true_node;
9003 : 35 : else if (integer_zerop (t))
9004 : : return inner2;
9005 : : }
9006 : :
9007 : : /* Handle the AND case, where we are redistributing:
9008 : : (inner1 AND inner2) OR (op2a code2 op2b)
9009 : : => (t AND (inner2 OR (op2a code op2b))) */
9010 : 711 : else if (integer_zerop (t))
9011 : 0 : return boolean_false_node;
9012 : :
9013 : : /* Save partial result for later. */
9014 : : partial = t;
9015 : : }
9016 : :
9017 : : /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
9018 : 40428 : if (TREE_CODE (inner2) == SSA_NAME
9019 : 40428 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
9020 : 39738 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9021 : 78396 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9022 : : gimple_assign_rhs1 (s),
9023 : : gimple_assign_rhs2 (s),
9024 : : code2, op2a, op2b,
9025 : : outer_cond_bb)))
9026 : : {
9027 : : /* Handle the OR case, where we are reassociating:
9028 : : (inner1 OR inner2) OR (op2a code2 op2b)
9029 : : => (inner1 OR t)
9030 : : => (t OR partial) */
9031 : 500 : if (is_or)
9032 : : {
9033 : 69 : if (integer_zerop (t))
9034 : : return inner1;
9035 : 69 : else if (integer_onep (t))
9036 : 1 : return boolean_true_node;
9037 : : /* If both are the same, we can apply the identity
9038 : : (x OR x) == x. */
9039 : 68 : else if (partial && same_bool_result_p (t, partial))
9040 : : return t;
9041 : : }
9042 : :
9043 : : /* Handle the AND case, where we are redistributing:
9044 : : (inner1 AND inner2) OR (op2a code2 op2b)
9045 : : => (t AND (inner1 OR (op2a code2 op2b)))
9046 : : => (t AND partial) */
9047 : : else
9048 : : {
9049 : 431 : if (integer_zerop (t))
9050 : 0 : return boolean_false_node;
9051 : 431 : else if (partial)
9052 : : {
9053 : : /* We already got a simplification for the other
9054 : : operand to the redistributed AND expression. The
9055 : : interesting case is when at least one is true.
9056 : : Or, if both are the same, we can apply the identity
9057 : : (x AND x) == x. */
9058 : 14 : if (integer_onep (partial))
9059 : : return t;
9060 : 14 : else if (integer_onep (t))
9061 : : return partial;
9062 : 4 : else if (same_bool_result_p (t, partial))
9063 : : return t;
9064 : : }
9065 : : }
9066 : : }
9067 : : }
9068 : : return NULL_TREE;
9069 : : }
9070 : :
9071 : : /* Try to simplify the OR of two comparisons defined by
9072 : : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
9073 : : If this can be done without constructing an intermediate value,
9074 : : return the resulting tree; otherwise NULL_TREE is returned.
9075 : : This function is deliberately asymmetric as it recurses on SSA_DEFs
9076 : : in the first comparison but not the second. */
9077 : :
9078 : : static tree
9079 : 989848 : or_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
9080 : : enum tree_code code2, tree op2a, tree op2b,
9081 : : basic_block outer_cond_bb)
9082 : : {
9083 : 989848 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
9084 : :
9085 : : /* First check for ((x CODE1 y) OR (x CODE2 y)). */
9086 : 989848 : if (operand_equal_p (op1a, op2a, 0)
9087 : 989848 : && operand_equal_p (op1b, op2b, 0))
9088 : : {
9089 : : /* Result will be either NULL_TREE, or a combined comparison. */
9090 : 3135 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9091 : : TRUTH_ORIF_EXPR, code1, code2,
9092 : : truth_type, op1a, op1b);
9093 : 3135 : if (t)
9094 : : return t;
9095 : : }
9096 : :
9097 : : /* Likewise the swapped case of the above. */
9098 : 986745 : if (operand_equal_p (op1a, op2b, 0)
9099 : 986745 : && operand_equal_p (op1b, op2a, 0))
9100 : : {
9101 : : /* Result will be either NULL_TREE, or a combined comparison. */
9102 : 0 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9103 : : TRUTH_ORIF_EXPR, code1,
9104 : : swap_tree_comparison (code2),
9105 : : truth_type, op1a, op1b);
9106 : 0 : if (t)
9107 : : return t;
9108 : : }
9109 : :
9110 : : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
9111 : : NAME's definition is a truth value. See if there are any simplifications
9112 : : that can be done against the NAME's definition. */
9113 : 986745 : if (TREE_CODE (op1a) == SSA_NAME
9114 : 986212 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
9115 : 1271185 : && (integer_zerop (op1b) || integer_onep (op1b)))
9116 : : {
9117 : 38044 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
9118 : 76139 : || (code1 == NE_EXPR && integer_onep (op1b)));
9119 : 72006 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
9120 : 72006 : switch (gimple_code (stmt))
9121 : : {
9122 : 43616 : case GIMPLE_ASSIGN:
9123 : : /* Try to simplify by copy-propagating the definition. */
9124 : 43616 : return or_var_with_comparison (type, op1a, invert, code2, op2a,
9125 : 43616 : op2b, outer_cond_bb);
9126 : :
9127 : 14844 : case GIMPLE_PHI:
9128 : : /* If every argument to the PHI produces the same result when
9129 : : ORed with the second comparison, we win.
9130 : : Do not do this unless the type is bool since we need a bool
9131 : : result here anyway. */
9132 : 14844 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
9133 : : {
9134 : : tree result = NULL_TREE;
9135 : : unsigned i;
9136 : 975 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
9137 : : {
9138 : 975 : tree arg = gimple_phi_arg_def (stmt, i);
9139 : :
9140 : : /* If this PHI has itself as an argument, ignore it.
9141 : : If all the other args produce the same result,
9142 : : we're still OK. */
9143 : 975 : if (arg == gimple_phi_result (stmt))
9144 : 0 : continue;
9145 : 975 : else if (TREE_CODE (arg) == INTEGER_CST)
9146 : : {
9147 : 825 : if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
9148 : : {
9149 : 379 : if (!result)
9150 : 239 : result = boolean_true_node;
9151 : 140 : else if (!integer_onep (result))
9152 : : return NULL_TREE;
9153 : : }
9154 : 446 : else if (!result)
9155 : 200 : result = fold_build2 (code2, boolean_type_node,
9156 : : op2a, op2b);
9157 : 246 : else if (!same_bool_comparison_p (result,
9158 : : code2, op2a, op2b))
9159 : : return NULL_TREE;
9160 : : }
9161 : 150 : else if (TREE_CODE (arg) == SSA_NAME
9162 : 150 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
9163 : : {
9164 : 150 : tree temp;
9165 : 150 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
9166 : : /* In simple cases we can look through PHI nodes,
9167 : : but we have to be careful with loops.
9168 : : See PR49073. */
9169 : 150 : if (! dom_info_available_p (CDI_DOMINATORS)
9170 : 150 : || gimple_bb (def_stmt) == gimple_bb (stmt)
9171 : 300 : || dominated_by_p (CDI_DOMINATORS,
9172 : 150 : gimple_bb (def_stmt),
9173 : 150 : gimple_bb (stmt)))
9174 : 30 : return NULL_TREE;
9175 : 120 : temp = or_var_with_comparison (type, arg, invert, code2,
9176 : : op2a, op2b, outer_cond_bb);
9177 : 120 : if (!temp)
9178 : : return NULL_TREE;
9179 : 0 : else if (!result)
9180 : : result = temp;
9181 : 0 : else if (!same_bool_result_p (result, temp))
9182 : : return NULL_TREE;
9183 : : }
9184 : : else
9185 : : return NULL_TREE;
9186 : : }
9187 : : return result;
9188 : : }
9189 : :
9190 : : default:
9191 : : break;
9192 : : }
9193 : : }
9194 : : return NULL_TREE;
9195 : : }
9196 : :
9197 : : /* Try to simplify the OR of two comparisons, specified by
9198 : : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9199 : : If this can be simplified to a single expression (without requiring
9200 : : introducing more SSA variables to hold intermediate values),
9201 : : return the resulting tree. Otherwise return NULL_TREE.
9202 : : If the result expression is non-null, it has boolean type. */
9203 : :
9204 : : tree
9205 : 496038 : maybe_fold_or_comparisons (tree type,
9206 : : enum tree_code code1, tree op1a, tree op1b,
9207 : : enum tree_code code2, tree op2a, tree op2b,
9208 : : basic_block outer_cond_bb)
9209 : : {
9210 : 496038 : if (tree t = or_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9211 : : outer_cond_bb))
9212 : : return t;
9213 : :
9214 : 492964 : if (tree t = or_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9215 : : outer_cond_bb))
9216 : : return t;
9217 : :
9218 : 492959 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
9219 : : op1a, op1b, code2, op2a,
9220 : : op2b, outer_cond_bb))
9221 : : return t;
9222 : :
9223 : : return NULL_TREE;
9224 : : }
9225 : :
9226 : : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9227 : :
9228 : : Either NULL_TREE, a simplified but non-constant or a constant
9229 : : is returned.
9230 : :
9231 : : ??? This should go into a gimple-fold-inline.h file to be eventually
9232 : : privatized with the single valueize function used in the various TUs
9233 : : to avoid the indirect function call overhead. */
9234 : :
9235 : : tree
9236 : 414924620 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
9237 : : tree (*gvalueize) (tree))
9238 : : {
9239 : 414924620 : gimple_match_op res_op;
9240 : : /* ??? The SSA propagators do not correctly deal with following SSA use-def
9241 : : edges if there are intermediate VARYING defs. For this reason
9242 : : do not follow SSA edges here even though SCCVN can technically
9243 : : just deal fine with that. */
9244 : 414924620 : if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
9245 : : {
9246 : 55141317 : tree res = NULL_TREE;
9247 : 55141317 : if (gimple_simplified_result_is_gimple_val (&res_op))
9248 : 33032297 : res = res_op.ops[0];
9249 : 22109020 : else if (mprts_hook)
9250 : 7725264 : res = mprts_hook (&res_op);
9251 : 40757561 : if (res)
9252 : : {
9253 : 34811267 : if (dump_file && dump_flags & TDF_DETAILS)
9254 : : {
9255 : 8775 : fprintf (dump_file, "Match-and-simplified ");
9256 : 8775 : print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
9257 : 8775 : fprintf (dump_file, " to ");
9258 : 8775 : print_generic_expr (dump_file, res);
9259 : 8775 : fprintf (dump_file, "\n");
9260 : : }
9261 : 34811267 : return res;
9262 : : }
9263 : : }
9264 : :
9265 : 380113353 : location_t loc = gimple_location (stmt);
9266 : 380113353 : switch (gimple_code (stmt))
9267 : : {
9268 : 329075539 : case GIMPLE_ASSIGN:
9269 : 329075539 : {
9270 : 329075539 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
9271 : :
9272 : 329075539 : switch (get_gimple_rhs_class (subcode))
9273 : : {
9274 : 120129396 : case GIMPLE_SINGLE_RHS:
9275 : 120129396 : {
9276 : 120129396 : tree rhs = gimple_assign_rhs1 (stmt);
9277 : 120129396 : enum tree_code_class kind = TREE_CODE_CLASS (subcode);
9278 : :
9279 : 120129396 : if (TREE_CODE (rhs) == SSA_NAME)
9280 : : {
9281 : : /* If the RHS is an SSA_NAME, return its known constant value,
9282 : : if any. */
9283 : 9378736 : return (*valueize) (rhs);
9284 : : }
9285 : : /* Handle propagating invariant addresses into address
9286 : : operations. */
9287 : 110750660 : else if (TREE_CODE (rhs) == ADDR_EXPR
9288 : 110750660 : && !is_gimple_min_invariant (rhs))
9289 : : {
9290 : 7538712 : poly_int64 offset = 0;
9291 : 7538712 : tree base;
9292 : 7538712 : base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
9293 : : &offset,
9294 : : valueize);
9295 : 7538712 : if (base
9296 : 7538712 : && (CONSTANT_CLASS_P (base)
9297 : 6875638 : || decl_address_invariant_p (base)))
9298 : 188892 : return build_invariant_address (TREE_TYPE (rhs),
9299 : 188892 : base, offset);
9300 : : }
9301 : 103211948 : else if (TREE_CODE (rhs) == CONSTRUCTOR
9302 : 1041535 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
9303 : 104656227 : && known_eq (CONSTRUCTOR_NELTS (rhs),
9304 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
9305 : : {
9306 : 393016 : unsigned i, nelts;
9307 : 393016 : tree val;
9308 : :
9309 : 393016 : nelts = CONSTRUCTOR_NELTS (rhs);
9310 : 393016 : tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
9311 : 870486 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
9312 : : {
9313 : 468225 : val = (*valueize) (val);
9314 : 468225 : if (TREE_CODE (val) == INTEGER_CST
9315 : 403566 : || TREE_CODE (val) == REAL_CST
9316 : 383771 : || TREE_CODE (val) == FIXED_CST)
9317 : 84454 : vec.quick_push (val);
9318 : : else
9319 : : return NULL_TREE;
9320 : : }
9321 : :
9322 : 9245 : return vec.build ();
9323 : 393016 : }
9324 : 110168752 : if (subcode == OBJ_TYPE_REF)
9325 : : {
9326 : 113981 : tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
9327 : : /* If callee is constant, we can fold away the wrapper. */
9328 : 113981 : if (is_gimple_min_invariant (val))
9329 : : return val;
9330 : : }
9331 : :
9332 : 110168675 : if (kind == tcc_reference)
9333 : : {
9334 : 73653266 : if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
9335 : 71564808 : || TREE_CODE (rhs) == REALPART_EXPR
9336 : 70716529 : || TREE_CODE (rhs) == IMAGPART_EXPR)
9337 : 75505282 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9338 : : {
9339 : 3025850 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9340 : 3025850 : return fold_unary_loc (EXPR_LOCATION (rhs),
9341 : 3025850 : TREE_CODE (rhs),
9342 : 6051700 : TREE_TYPE (rhs), val);
9343 : : }
9344 : 70627416 : else if (TREE_CODE (rhs) == BIT_FIELD_REF
9345 : 70627416 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9346 : : {
9347 : 478818 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9348 : 478818 : return fold_ternary_loc (EXPR_LOCATION (rhs),
9349 : 478818 : TREE_CODE (rhs),
9350 : 478818 : TREE_TYPE (rhs), val,
9351 : 478818 : TREE_OPERAND (rhs, 1),
9352 : 957636 : TREE_OPERAND (rhs, 2));
9353 : : }
9354 : 70148598 : else if (TREE_CODE (rhs) == MEM_REF
9355 : 70148598 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9356 : : {
9357 : 14090094 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9358 : 14090094 : if (TREE_CODE (val) == ADDR_EXPR
9359 : 14090094 : && is_gimple_min_invariant (val))
9360 : : {
9361 : 955570 : tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
9362 : : unshare_expr (val),
9363 : : TREE_OPERAND (rhs, 1));
9364 : 955570 : if (tem)
9365 : 70148598 : rhs = tem;
9366 : : }
9367 : : }
9368 : 70148598 : return fold_const_aggregate_ref_1 (rhs, valueize);
9369 : : }
9370 : 36515409 : else if (kind == tcc_declaration)
9371 : 8893748 : return get_symbol_constant_value (rhs);
9372 : : return rhs;
9373 : : }
9374 : :
9375 : : case GIMPLE_UNARY_RHS:
9376 : : return NULL_TREE;
9377 : :
9378 : 156501641 : case GIMPLE_BINARY_RHS:
9379 : : /* Translate &x + CST into an invariant form suitable for
9380 : : further propagation. */
9381 : 156501641 : if (subcode == POINTER_PLUS_EXPR)
9382 : : {
9383 : 18730058 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9384 : 18730058 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9385 : 18730058 : if (TREE_CODE (op0) == ADDR_EXPR
9386 : 5538451 : && TREE_CODE (op1) == INTEGER_CST)
9387 : : {
9388 : 573737 : tree off = fold_convert (ptr_type_node, op1);
9389 : 573737 : return build1_loc
9390 : 1147474 : (loc, ADDR_EXPR, TREE_TYPE (op0),
9391 : 573737 : fold_build2 (MEM_REF,
9392 : : TREE_TYPE (TREE_TYPE (op0)),
9393 : 573737 : unshare_expr (op0), off));
9394 : : }
9395 : : }
9396 : : /* Canonicalize bool != 0 and bool == 0 appearing after
9397 : : valueization. While gimple_simplify handles this
9398 : : it can get confused by the ~X == 1 -> X == 0 transform
9399 : : which we cant reduce to a SSA name or a constant
9400 : : (and we have no way to tell gimple_simplify to not
9401 : : consider those transforms in the first place). */
9402 : 137771583 : else if (subcode == EQ_EXPR
9403 : 137771583 : || subcode == NE_EXPR)
9404 : : {
9405 : 3365423 : tree lhs = gimple_assign_lhs (stmt);
9406 : 3365423 : tree op0 = gimple_assign_rhs1 (stmt);
9407 : 3365423 : if (useless_type_conversion_p (TREE_TYPE (lhs),
9408 : 3365423 : TREE_TYPE (op0)))
9409 : : {
9410 : 26229 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9411 : 26229 : op0 = (*valueize) (op0);
9412 : 26229 : if (TREE_CODE (op0) == INTEGER_CST)
9413 : 950 : std::swap (op0, op1);
9414 : 26229 : if (TREE_CODE (op1) == INTEGER_CST
9415 : 26229 : && ((subcode == NE_EXPR && integer_zerop (op1))
9416 : 2593 : || (subcode == EQ_EXPR && integer_onep (op1))))
9417 : 265 : return op0;
9418 : : }
9419 : : }
9420 : : return NULL_TREE;
9421 : :
9422 : 626753 : case GIMPLE_TERNARY_RHS:
9423 : 626753 : {
9424 : : /* Handle ternary operators that can appear in GIMPLE form. */
9425 : 626753 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9426 : 626753 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9427 : 626753 : tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
9428 : 626753 : return fold_ternary_loc (loc, subcode,
9429 : 626753 : TREE_TYPE (gimple_assign_lhs (stmt)),
9430 : 626753 : op0, op1, op2);
9431 : : }
9432 : :
9433 : 0 : default:
9434 : 0 : gcc_unreachable ();
9435 : : }
9436 : : }
9437 : :
9438 : 14014812 : case GIMPLE_CALL:
9439 : 14014812 : {
9440 : 14014812 : tree fn;
9441 : 14014812 : gcall *call_stmt = as_a <gcall *> (stmt);
9442 : :
9443 : 14014812 : if (gimple_call_internal_p (stmt))
9444 : : {
9445 : 1187158 : enum tree_code subcode = ERROR_MARK;
9446 : 1187158 : switch (gimple_call_internal_fn (stmt))
9447 : : {
9448 : : case IFN_UBSAN_CHECK_ADD:
9449 : : subcode = PLUS_EXPR;
9450 : : break;
9451 : 7911 : case IFN_UBSAN_CHECK_SUB:
9452 : 7911 : subcode = MINUS_EXPR;
9453 : 7911 : break;
9454 : 6815 : case IFN_UBSAN_CHECK_MUL:
9455 : 6815 : subcode = MULT_EXPR;
9456 : 6815 : break;
9457 : 136840 : case IFN_BUILTIN_EXPECT:
9458 : 136840 : {
9459 : 136840 : tree arg0 = gimple_call_arg (stmt, 0);
9460 : 136840 : tree op0 = (*valueize) (arg0);
9461 : 136840 : if (TREE_CODE (op0) == INTEGER_CST)
9462 : : return op0;
9463 : : return NULL_TREE;
9464 : : }
9465 : : default:
9466 : : return NULL_TREE;
9467 : : }
9468 : 22803 : tree arg0 = gimple_call_arg (stmt, 0);
9469 : 22803 : tree arg1 = gimple_call_arg (stmt, 1);
9470 : 22803 : tree op0 = (*valueize) (arg0);
9471 : 22803 : tree op1 = (*valueize) (arg1);
9472 : :
9473 : 22803 : if (TREE_CODE (op0) != INTEGER_CST
9474 : 2482 : || TREE_CODE (op1) != INTEGER_CST)
9475 : : {
9476 : 22293 : switch (subcode)
9477 : : {
9478 : 6715 : case MULT_EXPR:
9479 : : /* x * 0 = 0 * x = 0 without overflow. */
9480 : 6715 : if (integer_zerop (op0) || integer_zerop (op1))
9481 : 20 : return build_zero_cst (TREE_TYPE (arg0));
9482 : : break;
9483 : 7569 : case MINUS_EXPR:
9484 : : /* y - y = 0 without overflow. */
9485 : 7569 : if (operand_equal_p (op0, op1, 0))
9486 : 0 : return build_zero_cst (TREE_TYPE (arg0));
9487 : : break;
9488 : : default:
9489 : : break;
9490 : : }
9491 : : }
9492 : 22783 : tree res
9493 : 22783 : = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
9494 : 22783 : if (res
9495 : 2861 : && TREE_CODE (res) == INTEGER_CST
9496 : 23293 : && !TREE_OVERFLOW (res))
9497 : : return res;
9498 : : return NULL_TREE;
9499 : : }
9500 : :
9501 : 12827654 : fn = (*valueize) (gimple_call_fn (stmt));
9502 : 12827654 : if (TREE_CODE (fn) == ADDR_EXPR
9503 : 12197487 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
9504 : 12197423 : && fndecl_built_in_p (TREE_OPERAND (fn, 0))
9505 : 18788382 : && gimple_builtin_call_types_compatible_p (stmt,
9506 : 5960728 : TREE_OPERAND (fn, 0)))
9507 : : {
9508 : 5868059 : tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
9509 : 5868059 : tree retval;
9510 : 5868059 : unsigned i;
9511 : 18325621 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
9512 : 12457562 : args[i] = (*valueize) (gimple_call_arg (stmt, i));
9513 : 5868059 : retval = fold_builtin_call_array (loc,
9514 : : gimple_call_return_type (call_stmt),
9515 : : fn, gimple_call_num_args (stmt), args);
9516 : 5868059 : if (retval)
9517 : : {
9518 : : /* fold_call_expr wraps the result inside a NOP_EXPR. */
9519 : 51551 : STRIP_NOPS (retval);
9520 : 51551 : retval = fold_convert (gimple_call_return_type (call_stmt),
9521 : : retval);
9522 : : }
9523 : 5868059 : return retval;
9524 : : }
9525 : : return NULL_TREE;
9526 : : }
9527 : :
9528 : : default:
9529 : : return NULL_TREE;
9530 : : }
9531 : : }
9532 : :
9533 : : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9534 : : Returns NULL_TREE if folding to a constant is not possible, otherwise
9535 : : returns a constant according to is_gimple_min_invariant. */
9536 : :
9537 : : tree
9538 : 4334 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
9539 : : {
9540 : 4334 : tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
9541 : 4334 : if (res && is_gimple_min_invariant (res))
9542 : : return res;
9543 : : return NULL_TREE;
9544 : : }
9545 : :
9546 : :
9547 : : /* The following set of functions are supposed to fold references using
9548 : : their constant initializers. */
9549 : :
9550 : : /* See if we can find constructor defining value of BASE.
9551 : : When we know the consructor with constant offset (such as
9552 : : base is array[40] and we do know constructor of array), then
9553 : : BIT_OFFSET is adjusted accordingly.
9554 : :
9555 : : As a special case, return error_mark_node when constructor
9556 : : is not explicitly available, but it is known to be zero
9557 : : such as 'static const int a;'. */
9558 : : static tree
9559 : 129829747 : get_base_constructor (tree base, poly_int64 *bit_offset,
9560 : : tree (*valueize)(tree))
9561 : : {
9562 : 129922585 : poly_int64 bit_offset2, size, max_size;
9563 : 129922585 : bool reverse;
9564 : :
9565 : 129922585 : if (TREE_CODE (base) == MEM_REF)
9566 : : {
9567 : 141408478 : poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
9568 : 70704239 : if (!boff.to_shwi (bit_offset))
9569 : 70380612 : return NULL_TREE;
9570 : :
9571 : 70703905 : if (valueize
9572 : 70703905 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
9573 : 38131090 : base = valueize (TREE_OPERAND (base, 0));
9574 : 70703905 : if (!base || TREE_CODE (base) != ADDR_EXPR)
9575 : : return NULL_TREE;
9576 : 323627 : base = TREE_OPERAND (base, 0);
9577 : : }
9578 : 59218346 : else if (valueize
9579 : 31597805 : && TREE_CODE (base) == SSA_NAME)
9580 : 0 : base = valueize (base);
9581 : :
9582 : : /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
9583 : : DECL_INITIAL. If BASE is a nested reference into another
9584 : : ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
9585 : : the inner reference. */
9586 : 59541973 : switch (TREE_CODE (base))
9587 : : {
9588 : 52020430 : case VAR_DECL:
9589 : 52020430 : case CONST_DECL:
9590 : 52020430 : {
9591 : 52020430 : tree init = ctor_for_folding (base);
9592 : :
9593 : : /* Our semantic is exact opposite of ctor_for_folding;
9594 : : NULL means unknown, while error_mark_node is 0. */
9595 : 52020430 : if (init == error_mark_node)
9596 : : return NULL_TREE;
9597 : 1151731 : if (!init)
9598 : 1131 : return error_mark_node;
9599 : : return init;
9600 : : }
9601 : :
9602 : 92838 : case VIEW_CONVERT_EXPR:
9603 : 92838 : return get_base_constructor (TREE_OPERAND (base, 0),
9604 : 92838 : bit_offset, valueize);
9605 : :
9606 : 304666 : case ARRAY_REF:
9607 : 304666 : case COMPONENT_REF:
9608 : 304666 : base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
9609 : : &reverse);
9610 : 304666 : if (!known_size_p (max_size) || maybe_ne (size, max_size))
9611 : : return NULL_TREE;
9612 : 243812 : *bit_offset += bit_offset2;
9613 : 243812 : return get_base_constructor (base, bit_offset, valueize);
9614 : :
9615 : : case CONSTRUCTOR:
9616 : : return base;
9617 : :
9618 : 7124039 : default:
9619 : 7124039 : if (CONSTANT_CLASS_P (base))
9620 : : return base;
9621 : :
9622 : : return NULL_TREE;
9623 : : }
9624 : : }
9625 : :
9626 : : /* CTOR is a CONSTRUCTOR of an array or vector type. Fold a reference of SIZE
9627 : : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
9628 : : the reference; otherwise the type of the referenced element is used instead.
9629 : : When SIZE is zero, attempt to fold a reference to the entire element OFFSET
9630 : : refers to. Increment *SUBOFF by the bit offset of the accessed element. */
9631 : :
9632 : : static tree
9633 : 767565 : fold_array_ctor_reference (tree type, tree ctor,
9634 : : unsigned HOST_WIDE_INT offset,
9635 : : unsigned HOST_WIDE_INT size,
9636 : : tree from_decl,
9637 : : unsigned HOST_WIDE_INT *suboff)
9638 : : {
9639 : 767565 : offset_int low_bound;
9640 : 767565 : offset_int elt_size;
9641 : 767565 : offset_int access_index;
9642 : 767565 : tree domain_type = NULL_TREE;
9643 : 767565 : HOST_WIDE_INT inner_offset;
9644 : :
9645 : : /* Compute low bound and elt size. */
9646 : 767565 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
9647 : 767565 : domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
9648 : 767565 : if (domain_type && TYPE_MIN_VALUE (domain_type))
9649 : : {
9650 : : /* Static constructors for variably sized objects make no sense. */
9651 : 767565 : if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
9652 : : return NULL_TREE;
9653 : 767565 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
9654 : : }
9655 : : else
9656 : 0 : low_bound = 0;
9657 : : /* Static constructors for variably sized objects make no sense. */
9658 : 767565 : if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
9659 : : return NULL_TREE;
9660 : 767565 : elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
9661 : :
9662 : : /* When TYPE is non-null, verify that it specifies a constant-sized
9663 : : access of a multiple of the array element size. Avoid division
9664 : : by zero below when ELT_SIZE is zero, such as with the result of
9665 : : an initializer for a zero-length array or an empty struct. */
9666 : 767565 : if (elt_size == 0
9667 : 767565 : || (type
9668 : 767526 : && (!TYPE_SIZE_UNIT (type)
9669 : 767526 : || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
9670 : 39 : return NULL_TREE;
9671 : :
9672 : : /* Compute the array index we look for. */
9673 : 767526 : access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
9674 : : elt_size);
9675 : 767526 : access_index += low_bound;
9676 : :
9677 : : /* And offset within the access. */
9678 : 767526 : inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
9679 : :
9680 : 767526 : unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
9681 : 767526 : if (size > elt_sz * BITS_PER_UNIT)
9682 : : {
9683 : : /* native_encode_expr constraints. */
9684 : 47724 : if (size > MAX_BITSIZE_MODE_ANY_MODE
9685 : 46233 : || size % BITS_PER_UNIT != 0
9686 : 46233 : || inner_offset % BITS_PER_UNIT != 0
9687 : 46233 : || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
9688 : : return NULL_TREE;
9689 : :
9690 : 46233 : unsigned ctor_idx;
9691 : 46233 : tree val = get_array_ctor_element_at_index (ctor, access_index,
9692 : : &ctor_idx);
9693 : 46257 : if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
9694 : 23 : return build_zero_cst (type);
9695 : :
9696 : : /* native-encode adjacent ctor elements. */
9697 : 46210 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
9698 : 46210 : unsigned bufoff = 0;
9699 : 46210 : offset_int index = 0;
9700 : 46210 : offset_int max_index = access_index;
9701 : 46210 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
9702 : 46210 : if (!val)
9703 : 1 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
9704 : 46209 : else if (!CONSTANT_CLASS_P (val))
9705 : : return NULL_TREE;
9706 : 45552 : if (!elt->index)
9707 : : ;
9708 : 38746 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
9709 : : {
9710 : 20 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
9711 : 20 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
9712 : : }
9713 : : else
9714 : 38726 : index = max_index = wi::to_offset (elt->index);
9715 : 45552 : index = wi::umax (index, access_index);
9716 : 521686 : do
9717 : : {
9718 : 521686 : if (bufoff + elt_sz > sizeof (buf))
9719 : 0 : elt_sz = sizeof (buf) - bufoff;
9720 : 521686 : int len;
9721 : 521686 : if (TREE_CODE (val) == RAW_DATA_CST)
9722 : : {
9723 : 20 : gcc_assert (inner_offset == 0);
9724 : 20 : if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
9725 : : return NULL_TREE;
9726 : 40 : inner_offset = (access_index
9727 : 20 : - wi::to_offset (elt->index)).to_uhwi ();
9728 : 20 : len = MIN (sizeof (buf) - bufoff,
9729 : : (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
9730 : 20 : memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
9731 : : len);
9732 : 20 : access_index += len - 1;
9733 : : }
9734 : : else
9735 : : {
9736 : 1043332 : len = native_encode_expr (val, buf + bufoff, elt_sz,
9737 : 521666 : inner_offset / BITS_PER_UNIT);
9738 : 521666 : if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
9739 : : return NULL_TREE;
9740 : : }
9741 : 521686 : inner_offset = 0;
9742 : 521686 : bufoff += len;
9743 : :
9744 : 521686 : access_index += 1;
9745 : 521686 : if (wi::cmpu (access_index, index) == 0)
9746 : 2 : val = elt->value;
9747 : 521684 : else if (wi::cmpu (access_index, max_index) > 0)
9748 : : {
9749 : 521444 : ctor_idx++;
9750 : 521444 : if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
9751 : : {
9752 : 41800 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
9753 : 41800 : ++max_index;
9754 : : }
9755 : : else
9756 : : {
9757 : 479644 : elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
9758 : 479644 : index = 0;
9759 : 479644 : max_index = access_index;
9760 : 479644 : if (!elt->index)
9761 : : ;
9762 : 478836 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
9763 : : {
9764 : 0 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
9765 : 0 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
9766 : : }
9767 : : else
9768 : 478836 : index = max_index = wi::to_offset (elt->index);
9769 : 479644 : index = wi::umax (index, access_index);
9770 : 479644 : if (wi::cmpu (access_index, index) == 0)
9771 : 479643 : val = elt->value;
9772 : : else
9773 : 1 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
9774 : : }
9775 : : }
9776 : : }
9777 : 521686 : while (bufoff < size / BITS_PER_UNIT);
9778 : 45552 : *suboff += size;
9779 : 45552 : return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
9780 : : }
9781 : :
9782 : 719802 : unsigned ctor_idx;
9783 : 719802 : if (tree val = get_array_ctor_element_at_index (ctor, access_index,
9784 : : &ctor_idx))
9785 : : {
9786 : 718231 : if (TREE_CODE (val) == RAW_DATA_CST)
9787 : : {
9788 : 1802 : if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
9789 : : return NULL_TREE;
9790 : 1794 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
9791 : 1794 : if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
9792 : : return NULL_TREE;
9793 : 1794 : unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
9794 : 1794 : val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
9795 : : }
9796 : 718223 : if (!size && TREE_CODE (val) != CONSTRUCTOR)
9797 : : {
9798 : : /* For the final reference to the entire accessed element
9799 : : (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
9800 : : may be null) in favor of the type of the element, and set
9801 : : SIZE to the size of the accessed element. */
9802 : 23026 : inner_offset = 0;
9803 : 23026 : type = TREE_TYPE (val);
9804 : 23026 : size = elt_sz * BITS_PER_UNIT;
9805 : : }
9806 : 1885371 : else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
9807 : 533715 : && TREE_CODE (val) == CONSTRUCTOR
9808 : 18183 : && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
9809 : : /* If this isn't the last element in the CTOR and a CTOR itself
9810 : : and it does not cover the whole object we are requesting give up
9811 : : since we're not set up for combining from multiple CTORs. */
9812 : 29 : return NULL_TREE;
9813 : :
9814 : 718194 : *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
9815 : 718194 : return fold_ctor_reference (type, val, inner_offset, size, from_decl,
9816 : : suboff);
9817 : : }
9818 : :
9819 : : /* Memory not explicitly mentioned in constructor is 0 (or
9820 : : the reference is out of range). */
9821 : 1571 : return type ? build_zero_cst (type) : NULL_TREE;
9822 : : }
9823 : :
9824 : : /* CTOR is a CONSTRUCTOR of a record or union type. Fold a reference of SIZE
9825 : : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
9826 : : the reference; otherwise the type of the referenced member is used instead.
9827 : : When SIZE is zero, attempt to fold a reference to the entire member OFFSET
9828 : : refers to. Increment *SUBOFF by the bit offset of the accessed member. */
9829 : :
9830 : : static tree
9831 : 76290 : fold_nonarray_ctor_reference (tree type, tree ctor,
9832 : : unsigned HOST_WIDE_INT offset,
9833 : : unsigned HOST_WIDE_INT size,
9834 : : tree from_decl,
9835 : : unsigned HOST_WIDE_INT *suboff)
9836 : : {
9837 : 76290 : unsigned HOST_WIDE_INT cnt;
9838 : 76290 : tree cfield, cval;
9839 : :
9840 : 116973 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
9841 : : {
9842 : 108017 : tree byte_offset = DECL_FIELD_OFFSET (cfield);
9843 : 108017 : tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
9844 : 108017 : tree field_size = DECL_SIZE (cfield);
9845 : :
9846 : 108017 : if (!field_size)
9847 : : {
9848 : : /* Determine the size of the flexible array member from
9849 : : the size of the initializer provided for it. */
9850 : 847 : field_size = TYPE_SIZE (TREE_TYPE (cval));
9851 : : }
9852 : :
9853 : : /* Variable sized objects in static constructors makes no sense,
9854 : : but field_size can be NULL for flexible array members. */
9855 : 108017 : gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
9856 : : && TREE_CODE (byte_offset) == INTEGER_CST
9857 : : && (field_size != NULL_TREE
9858 : : ? TREE_CODE (field_size) == INTEGER_CST
9859 : : : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
9860 : :
9861 : : /* Compute bit offset of the field. */
9862 : 108017 : offset_int bitoffset
9863 : 108017 : = (wi::to_offset (field_offset)
9864 : 108017 : + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
9865 : : /* Compute bit offset where the field ends. */
9866 : 108017 : offset_int bitoffset_end;
9867 : 108017 : if (field_size != NULL_TREE)
9868 : 108017 : bitoffset_end = bitoffset + wi::to_offset (field_size);
9869 : : else
9870 : 0 : bitoffset_end = 0;
9871 : :
9872 : : /* Compute the bit offset of the end of the desired access.
9873 : : As a special case, if the size of the desired access is
9874 : : zero, assume the access is to the entire field (and let
9875 : : the caller make any necessary adjustments by storing
9876 : : the actual bounds of the field in FIELDBOUNDS). */
9877 : 108017 : offset_int access_end = offset_int (offset);
9878 : 108017 : if (size)
9879 : 65598 : access_end += size;
9880 : : else
9881 : 42419 : access_end = bitoffset_end;
9882 : :
9883 : : /* Is there any overlap between the desired access at
9884 : : [OFFSET, OFFSET+SIZE) and the offset of the field within
9885 : : the object at [BITOFFSET, BITOFFSET_END)? */
9886 : 108017 : if (wi::cmps (access_end, bitoffset) > 0
9887 : 108017 : && (field_size == NULL_TREE
9888 : 105686 : || wi::lts_p (offset, bitoffset_end)))
9889 : : {
9890 : 67334 : *suboff += bitoffset.to_uhwi ();
9891 : :
9892 : 67334 : if (!size && TREE_CODE (cval) != CONSTRUCTOR)
9893 : : {
9894 : : /* For the final reference to the entire accessed member
9895 : : (SIZE is zero), reset OFFSET, disegard TYPE (which may
9896 : : be null) in favor of the type of the member, and set
9897 : : SIZE to the size of the accessed member. */
9898 : 19759 : offset = bitoffset.to_uhwi ();
9899 : 19759 : type = TREE_TYPE (cval);
9900 : 19759 : size = (bitoffset_end - bitoffset).to_uhwi ();
9901 : : }
9902 : :
9903 : : /* We do have overlap. Now see if the field is large enough
9904 : : to cover the access. Give up for accesses that extend
9905 : : beyond the end of the object or that span multiple fields. */
9906 : 67334 : if (wi::cmps (access_end, bitoffset_end) > 0)
9907 : : return NULL_TREE;
9908 : 66706 : if (offset < bitoffset)
9909 : : return NULL_TREE;
9910 : :
9911 : 66706 : offset_int inner_offset = offset_int (offset) - bitoffset;
9912 : :
9913 : : /* Integral bit-fields are left-justified on big-endian targets, so
9914 : : we must arrange for native_encode_int to start at their MSB. */
9915 : 66706 : if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
9916 : : {
9917 : 1115 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9918 : : return NULL_TREE;
9919 : 1115 : const unsigned int encoding_size
9920 : 1115 : = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (cfield)));
9921 : 1115 : if (BYTES_BIG_ENDIAN)
9922 : : inner_offset += encoding_size - wi::to_offset (field_size);
9923 : : }
9924 : :
9925 : 66706 : return fold_ctor_reference (type, cval,
9926 : 66706 : inner_offset.to_uhwi (), size,
9927 : : from_decl, suboff);
9928 : : }
9929 : : }
9930 : :
9931 : 8956 : if (!type)
9932 : : return NULL_TREE;
9933 : :
9934 : 8956 : return build_zero_cst (type);
9935 : : }
9936 : :
9937 : : /* CTOR is a value initializing memory. Fold a reference of TYPE and
9938 : : bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
9939 : : is zero, attempt to fold a reference to the entire subobject
9940 : : which OFFSET refers to. This is used when folding accesses to
9941 : : string members of aggregates. When non-null, set *SUBOFF to
9942 : : the bit offset of the accessed subobject. */
9943 : :
9944 : : tree
9945 : 1737995 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
9946 : : const poly_uint64 &poly_size, tree from_decl,
9947 : : unsigned HOST_WIDE_INT *suboff /* = NULL */)
9948 : : {
9949 : 1737995 : tree ret;
9950 : :
9951 : : /* We found the field with exact match. */
9952 : 1737995 : if (type
9953 : 1737995 : && useless_type_conversion_p (type, TREE_TYPE (ctor))
9954 : 2473894 : && known_eq (poly_offset, 0U))
9955 : 734456 : return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
9956 : :
9957 : : /* The remaining optimizations need a constant size and offset. */
9958 : 1003539 : unsigned HOST_WIDE_INT size, offset;
9959 : 1003539 : if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
9960 : : return NULL_TREE;
9961 : :
9962 : : /* We are at the end of walk, see if we can view convert the
9963 : : result. */
9964 : 1003539 : if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
9965 : : /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
9966 : 22623 : && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
9967 : 22516 : && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
9968 : : {
9969 : 14201 : ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
9970 : 14201 : if (ret)
9971 : : {
9972 : 14201 : ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
9973 : 14201 : if (ret)
9974 : 14141 : STRIP_USELESS_TYPE_CONVERSION (ret);
9975 : : }
9976 : 14201 : return ret;
9977 : : }
9978 : :
9979 : : /* For constants and byte-aligned/sized reads, try to go through
9980 : : native_encode/interpret. */
9981 : 989338 : if (CONSTANT_CLASS_P (ctor)
9982 : : && BITS_PER_UNIT == 8
9983 : 136041 : && offset % BITS_PER_UNIT == 0
9984 : 136037 : && offset / BITS_PER_UNIT <= INT_MAX
9985 : 136005 : && size % BITS_PER_UNIT == 0
9986 : 135996 : && size <= MAX_BITSIZE_MODE_ANY_MODE
9987 : 1125051 : && can_native_interpret_type_p (type))
9988 : : {
9989 : 92794 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
9990 : 185588 : int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
9991 : 92794 : offset / BITS_PER_UNIT);
9992 : 92794 : if (len > 0)
9993 : 92086 : return native_interpret_expr (type, buf, len);
9994 : : }
9995 : :
9996 : : /* For constructors, try first a recursive local processing, but in any case
9997 : : this requires the native storage order. */
9998 : 897252 : if (TREE_CODE (ctor) == CONSTRUCTOR
9999 : 897252 : && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
10000 : 844085 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
10001 : : {
10002 : 843855 : unsigned HOST_WIDE_INT dummy = 0;
10003 : 843855 : if (!suboff)
10004 : 719589 : suboff = &dummy;
10005 : :
10006 : 843855 : tree ret;
10007 : 843855 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
10008 : 843855 : || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
10009 : 767565 : ret = fold_array_ctor_reference (type, ctor, offset, size,
10010 : : from_decl, suboff);
10011 : : else
10012 : 76290 : ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
10013 : : from_decl, suboff);
10014 : :
10015 : : /* Otherwise fall back to native_encode_initializer. This may be done
10016 : : only from the outermost fold_ctor_reference call (because it itself
10017 : : recurses into CONSTRUCTORs and doesn't update suboff). */
10018 : 843855 : if (ret == NULL_TREE
10019 : 318601 : && suboff == &dummy
10020 : : && BITS_PER_UNIT == 8
10021 : 309026 : && offset % BITS_PER_UNIT == 0
10022 : 309024 : && offset / BITS_PER_UNIT <= INT_MAX
10023 : 309024 : && size % BITS_PER_UNIT == 0
10024 : 309015 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10025 : 1151379 : && can_native_interpret_type_p (type))
10026 : : {
10027 : 266860 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10028 : 533720 : int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
10029 : 266860 : offset / BITS_PER_UNIT);
10030 : 266860 : if (len > 0)
10031 : 1317 : return native_interpret_expr (type, buf, len);
10032 : : }
10033 : :
10034 : 842538 : return ret;
10035 : : }
10036 : :
10037 : : return NULL_TREE;
10038 : : }
10039 : :
10040 : : /* Return the tree representing the element referenced by T if T is an
10041 : : ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
10042 : : names using VALUEIZE. Return NULL_TREE otherwise. */
10043 : :
10044 : : tree
10045 : 135307391 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
10046 : : {
10047 : 135307391 : tree ctor, idx, base;
10048 : 135307391 : poly_int64 offset, size, max_size;
10049 : 135307391 : tree tem;
10050 : 135307391 : bool reverse;
10051 : :
10052 : 135307391 : if (TREE_THIS_VOLATILE (t))
10053 : : return NULL_TREE;
10054 : :
10055 : 134995073 : if (DECL_P (t))
10056 : 256604 : return get_symbol_constant_value (t);
10057 : :
10058 : 134738469 : tem = fold_read_from_constant_string (t);
10059 : 134738469 : if (tem)
10060 : : return tem;
10061 : :
10062 : 134733920 : switch (TREE_CODE (t))
10063 : : {
10064 : 10282010 : case ARRAY_REF:
10065 : 10282010 : case ARRAY_RANGE_REF:
10066 : : /* Constant indexes are handled well by get_base_constructor.
10067 : : Only special case variable offsets.
10068 : : FIXME: This code can't handle nested references with variable indexes
10069 : : (they will be handled only by iteration of ccp). Perhaps we can bring
10070 : : get_ref_base_and_extent here and make it use a valueize callback. */
10071 : 10282010 : if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
10072 : 6089886 : && valueize
10073 : 4012532 : && (idx = (*valueize) (TREE_OPERAND (t, 1)))
10074 : 14294542 : && poly_int_tree_p (idx))
10075 : : {
10076 : 1623782 : tree low_bound, unit_size;
10077 : :
10078 : : /* If the resulting bit-offset is constant, track it. */
10079 : 1623782 : if ((low_bound = array_ref_low_bound (t),
10080 : 1623782 : poly_int_tree_p (low_bound))
10081 : 1623782 : && (unit_size = array_ref_element_size (t),
10082 : 1623782 : tree_fits_uhwi_p (unit_size)))
10083 : : {
10084 : 1623782 : poly_offset_int woffset
10085 : 1623782 : = wi::sext (wi::to_poly_offset (idx)
10086 : 3247564 : - wi::to_poly_offset (low_bound),
10087 : 1623782 : TYPE_PRECISION (sizetype));
10088 : 1623782 : woffset *= tree_to_uhwi (unit_size);
10089 : 1623782 : woffset *= BITS_PER_UNIT;
10090 : 1623782 : if (woffset.to_shwi (&offset))
10091 : : {
10092 : 1623680 : base = TREE_OPERAND (t, 0);
10093 : 1623680 : ctor = get_base_constructor (base, &offset, valueize);
10094 : : /* Empty constructor. Always fold to 0. */
10095 : 1623680 : if (ctor == error_mark_node)
10096 : 1623680 : return build_zero_cst (TREE_TYPE (t));
10097 : : /* Out of bound array access. Value is undefined,
10098 : : but don't fold. */
10099 : 1623603 : if (maybe_lt (offset, 0))
10100 : : return NULL_TREE;
10101 : : /* We cannot determine ctor. */
10102 : 1623099 : if (!ctor)
10103 : : return NULL_TREE;
10104 : 169027 : return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
10105 : 169027 : tree_to_uhwi (unit_size)
10106 : 338054 : * BITS_PER_UNIT,
10107 : : base);
10108 : : }
10109 : : }
10110 : : }
10111 : : /* Fallthru. */
10112 : :
10113 : 127962255 : case COMPONENT_REF:
10114 : 127962255 : case BIT_FIELD_REF:
10115 : 127962255 : case TARGET_MEM_REF:
10116 : 127962255 : case MEM_REF:
10117 : 127962255 : base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
10118 : 127962255 : ctor = get_base_constructor (base, &offset, valueize);
10119 : :
10120 : : /* Empty constructor. Always fold to 0. */
10121 : 127962255 : if (ctor == error_mark_node)
10122 : 1054 : return build_zero_cst (TREE_TYPE (t));
10123 : : /* We do not know precise address. */
10124 : 127961201 : if (!known_size_p (max_size) || maybe_ne (max_size, size))
10125 : : return NULL_TREE;
10126 : : /* We cannot determine ctor. */
10127 : 120310614 : if (!ctor)
10128 : : return NULL_TREE;
10129 : :
10130 : : /* Out of bound array access. Value is undefined, but don't fold. */
10131 : 596230 : if (maybe_lt (offset, 0))
10132 : : return NULL_TREE;
10133 : :
10134 : 593375 : tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
10135 : 593375 : if (tem)
10136 : : return tem;
10137 : :
10138 : : /* For bit field reads try to read the representative and
10139 : : adjust. */
10140 : 293913 : if (TREE_CODE (t) == COMPONENT_REF
10141 : 6353 : && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
10142 : 293997 : && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
10143 : : {
10144 : 84 : HOST_WIDE_INT csize, coffset;
10145 : 84 : tree field = TREE_OPERAND (t, 1);
10146 : 84 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
10147 : 168 : if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
10148 : 83 : && size.is_constant (&csize)
10149 : 83 : && offset.is_constant (&coffset)
10150 : 83 : && (coffset % BITS_PER_UNIT != 0
10151 : 81 : || csize % BITS_PER_UNIT != 0)
10152 : 10 : && !reverse
10153 : 84 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
10154 : : {
10155 : 10 : poly_int64 bitoffset;
10156 : 10 : poly_uint64 field_offset, repr_offset;
10157 : 10 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
10158 : 20 : && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
10159 : 10 : bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
10160 : : else
10161 : : bitoffset = 0;
10162 : 10 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
10163 : 10 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
10164 : 10 : HOST_WIDE_INT bitoff;
10165 : 10 : int diff = (TYPE_PRECISION (TREE_TYPE (repr))
10166 : 10 : - TYPE_PRECISION (TREE_TYPE (field)));
10167 : 10 : if (bitoffset.is_constant (&bitoff)
10168 : 10 : && bitoff >= 0
10169 : 10 : && bitoff <= diff)
10170 : : {
10171 : 10 : offset -= bitoff;
10172 : 10 : size = tree_to_uhwi (DECL_SIZE (repr));
10173 : :
10174 : 10 : tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
10175 : 10 : size, base);
10176 : 10 : if (tem && TREE_CODE (tem) == INTEGER_CST)
10177 : : {
10178 : 10 : if (!BYTES_BIG_ENDIAN)
10179 : 10 : tem = wide_int_to_tree (TREE_TYPE (field),
10180 : 10 : wi::lrshift (wi::to_wide (tem),
10181 : : bitoff));
10182 : : else
10183 : : tem = wide_int_to_tree (TREE_TYPE (field),
10184 : : wi::lrshift (wi::to_wide (tem),
10185 : : diff - bitoff));
10186 : 10 : return tem;
10187 : : }
10188 : : }
10189 : : }
10190 : : }
10191 : : break;
10192 : :
10193 : 1601429 : case REALPART_EXPR:
10194 : 1601429 : case IMAGPART_EXPR:
10195 : 1601429 : {
10196 : 1601429 : tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
10197 : 1601429 : if (c && TREE_CODE (c) == COMPLEX_CST)
10198 : 2696 : return fold_build1_loc (EXPR_LOCATION (t),
10199 : 5392 : TREE_CODE (t), TREE_TYPE (t), c);
10200 : : break;
10201 : : }
10202 : :
10203 : : default:
10204 : : break;
10205 : : }
10206 : :
10207 : : return NULL_TREE;
10208 : : }
10209 : :
10210 : : tree
10211 : 63557364 : fold_const_aggregate_ref (tree t)
10212 : : {
10213 : 63557364 : return fold_const_aggregate_ref_1 (t, NULL);
10214 : : }
10215 : :
10216 : : /* Lookup virtual method with index TOKEN in a virtual table V
10217 : : at OFFSET.
10218 : : Set CAN_REFER if non-NULL to false if method
10219 : : is not referable or if the virtual table is ill-formed (such as rewriten
10220 : : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10221 : :
10222 : : tree
10223 : 389250 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
10224 : : tree v,
10225 : : unsigned HOST_WIDE_INT offset,
10226 : : bool *can_refer)
10227 : : {
10228 : 389250 : tree vtable = v, init, fn;
10229 : 389250 : unsigned HOST_WIDE_INT size;
10230 : 389250 : unsigned HOST_WIDE_INT elt_size, access_index;
10231 : 389250 : tree domain_type;
10232 : :
10233 : 389250 : if (can_refer)
10234 : 389250 : *can_refer = true;
10235 : :
10236 : : /* First of all double check we have virtual table. */
10237 : 389250 : if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
10238 : : {
10239 : : /* Pass down that we lost track of the target. */
10240 : 0 : if (can_refer)
10241 : 0 : *can_refer = false;
10242 : 0 : return NULL_TREE;
10243 : : }
10244 : :
10245 : 389250 : init = ctor_for_folding (v);
10246 : :
10247 : : /* The virtual tables should always be born with constructors
10248 : : and we always should assume that they are avaialble for
10249 : : folding. At the moment we do not stream them in all cases,
10250 : : but it should never happen that ctor seem unreachable. */
10251 : 389250 : gcc_assert (init);
10252 : 389250 : if (init == error_mark_node)
10253 : : {
10254 : : /* Pass down that we lost track of the target. */
10255 : 176 : if (can_refer)
10256 : 176 : *can_refer = false;
10257 : 176 : return NULL_TREE;
10258 : : }
10259 : 389074 : gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
10260 : 389074 : size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
10261 : 389074 : offset *= BITS_PER_UNIT;
10262 : 389074 : offset += token * size;
10263 : :
10264 : : /* Lookup the value in the constructor that is assumed to be array.
10265 : : This is equivalent to
10266 : : fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
10267 : : offset, size, NULL);
10268 : : but in a constant time. We expect that frontend produced a simple
10269 : : array without indexed initializers. */
10270 : :
10271 : 389074 : gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
10272 : 389074 : domain_type = TYPE_DOMAIN (TREE_TYPE (init));
10273 : 389074 : gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
10274 : 389074 : elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
10275 : :
10276 : 389074 : access_index = offset / BITS_PER_UNIT / elt_size;
10277 : 389074 : gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
10278 : :
10279 : : /* The C++ FE can now produce indexed fields, and we check if the indexes
10280 : : match. */
10281 : 389074 : if (access_index < CONSTRUCTOR_NELTS (init))
10282 : : {
10283 : 389073 : fn = CONSTRUCTOR_ELT (init, access_index)->value;
10284 : 389073 : tree idx = CONSTRUCTOR_ELT (init, access_index)->index;
10285 : 389073 : gcc_checking_assert (!idx || tree_to_uhwi (idx) == access_index);
10286 : 389073 : STRIP_NOPS (fn);
10287 : : }
10288 : : else
10289 : : fn = NULL;
10290 : :
10291 : : /* For type inconsistent program we may end up looking up virtual method
10292 : : in virtual table that does not contain TOKEN entries. We may overrun
10293 : : the virtual table and pick up a constant or RTTI info pointer.
10294 : : In any case the call is undefined. */
10295 : 389073 : if (!fn
10296 : 389073 : || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
10297 : 771678 : || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
10298 : 6469 : fn = builtin_decl_unreachable ();
10299 : : else
10300 : : {
10301 : 382605 : fn = TREE_OPERAND (fn, 0);
10302 : :
10303 : : /* When cgraph node is missing and function is not public, we cannot
10304 : : devirtualize. This can happen in WHOPR when the actual method
10305 : : ends up in other partition, because we found devirtualization
10306 : : possibility too late. */
10307 : 382605 : if (!can_refer_decl_in_current_unit_p (fn, vtable))
10308 : : {
10309 : 54200 : if (can_refer)
10310 : : {
10311 : 54200 : *can_refer = false;
10312 : 54200 : return fn;
10313 : : }
10314 : : return NULL_TREE;
10315 : : }
10316 : : }
10317 : :
10318 : : /* Make sure we create a cgraph node for functions we'll reference.
10319 : : They can be non-existent if the reference comes from an entry
10320 : : of an external vtable for example. */
10321 : 334874 : cgraph_node::get_create (fn);
10322 : :
10323 : 334874 : return fn;
10324 : : }
10325 : :
10326 : : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
10327 : : is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
10328 : : KNOWN_BINFO carries the binfo describing the true type of
10329 : : OBJ_TYPE_REF_OBJECT(REF).
10330 : : Set CAN_REFER if non-NULL to false if method
10331 : : is not referable or if the virtual table is ill-formed (such as rewriten
10332 : : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10333 : :
10334 : : tree
10335 : 366882 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
10336 : : bool *can_refer)
10337 : : {
10338 : 366882 : unsigned HOST_WIDE_INT offset;
10339 : 366882 : tree v;
10340 : :
10341 : 366882 : v = BINFO_VTABLE (known_binfo);
10342 : : /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
10343 : 366882 : if (!v)
10344 : : return NULL_TREE;
10345 : :
10346 : 366882 : if (!vtable_pointer_value_to_vtable (v, &v, &offset))
10347 : : {
10348 : 0 : if (can_refer)
10349 : 0 : *can_refer = false;
10350 : 0 : return NULL_TREE;
10351 : : }
10352 : 366882 : return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
10353 : : }
10354 : :
10355 : : /* Given a pointer value T, return a simplified version of an
10356 : : indirection through T, or NULL_TREE if no simplification is
10357 : : possible. Note that the resulting type may be different from
10358 : : the type pointed to in the sense that it is still compatible
10359 : : from the langhooks point of view. */
10360 : :
10361 : : tree
10362 : 1991147 : gimple_fold_indirect_ref (tree t)
10363 : : {
10364 : 1991147 : tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
10365 : 1991147 : tree sub = t;
10366 : 1991147 : tree subtype;
10367 : :
10368 : 1991147 : STRIP_NOPS (sub);
10369 : 1991147 : subtype = TREE_TYPE (sub);
10370 : 1991147 : if (!POINTER_TYPE_P (subtype)
10371 : 1991147 : || TYPE_REF_CAN_ALIAS_ALL (ptype))
10372 : : return NULL_TREE;
10373 : :
10374 : 1989425 : if (TREE_CODE (sub) == ADDR_EXPR)
10375 : : {
10376 : 127545 : tree op = TREE_OPERAND (sub, 0);
10377 : 127545 : tree optype = TREE_TYPE (op);
10378 : : /* *&p => p */
10379 : 127545 : if (useless_type_conversion_p (type, optype))
10380 : : return op;
10381 : :
10382 : : /* *(foo *)&fooarray => fooarray[0] */
10383 : 912 : if (TREE_CODE (optype) == ARRAY_TYPE
10384 : 321 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
10385 : 1233 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10386 : : {
10387 : 52 : tree type_domain = TYPE_DOMAIN (optype);
10388 : 52 : tree min_val = size_zero_node;
10389 : 52 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10390 : 52 : min_val = TYPE_MIN_VALUE (type_domain);
10391 : 52 : if (TREE_CODE (min_val) == INTEGER_CST)
10392 : 52 : return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
10393 : : }
10394 : : /* *(foo *)&complexfoo => __real__ complexfoo */
10395 : 860 : else if (TREE_CODE (optype) == COMPLEX_TYPE
10396 : 860 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10397 : 4 : return fold_build1 (REALPART_EXPR, type, op);
10398 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
10399 : 856 : else if (TREE_CODE (optype) == VECTOR_TYPE
10400 : 856 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10401 : : {
10402 : 26 : tree part_width = TYPE_SIZE (type);
10403 : 26 : tree index = bitsize_int (0);
10404 : 26 : return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
10405 : : }
10406 : : }
10407 : :
10408 : : /* *(p + CST) -> ... */
10409 : 1862710 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
10410 : 1862710 : && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
10411 : : {
10412 : 34323 : tree addr = TREE_OPERAND (sub, 0);
10413 : 34323 : tree off = TREE_OPERAND (sub, 1);
10414 : 34323 : tree addrtype;
10415 : :
10416 : 34323 : STRIP_NOPS (addr);
10417 : 34323 : addrtype = TREE_TYPE (addr);
10418 : :
10419 : : /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
10420 : 34323 : if (TREE_CODE (addr) == ADDR_EXPR
10421 : 92 : && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
10422 : 39 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
10423 : 34351 : && tree_fits_uhwi_p (off))
10424 : : {
10425 : 28 : unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
10426 : 28 : tree part_width = TYPE_SIZE (type);
10427 : 28 : unsigned HOST_WIDE_INT part_widthi
10428 : 28 : = tree_to_shwi (part_width) / BITS_PER_UNIT;
10429 : 28 : unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
10430 : 28 : tree index = bitsize_int (indexi);
10431 : 28 : if (known_lt (offset / part_widthi,
10432 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
10433 : 28 : return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
10434 : : part_width, index);
10435 : : }
10436 : :
10437 : : /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
10438 : 34295 : if (TREE_CODE (addr) == ADDR_EXPR
10439 : 64 : && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
10440 : 34296 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
10441 : : {
10442 : 1 : tree size = TYPE_SIZE_UNIT (type);
10443 : 1 : if (tree_int_cst_equal (size, off))
10444 : 1 : return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
10445 : : }
10446 : :
10447 : : /* *(p + CST) -> MEM_REF <p, CST>. */
10448 : 34294 : if (TREE_CODE (addr) != ADDR_EXPR
10449 : 34294 : || DECL_P (TREE_OPERAND (addr, 0)))
10450 : 34276 : return fold_build2 (MEM_REF, type,
10451 : : addr,
10452 : : wide_int_to_tree (ptype, wi::to_wide (off)));
10453 : : }
10454 : :
10455 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10456 : 1828405 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10457 : 1958 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
10458 : 1830345 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10459 : : {
10460 : 1 : tree type_domain;
10461 : 1 : tree min_val = size_zero_node;
10462 : 1 : tree osub = sub;
10463 : 1 : sub = gimple_fold_indirect_ref (sub);
10464 : 1 : if (! sub)
10465 : 1 : sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
10466 : 1 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
10467 : 1 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10468 : 1 : min_val = TYPE_MIN_VALUE (type_domain);
10469 : 1 : if (TREE_CODE (min_val) == INTEGER_CST)
10470 : 1 : return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
10471 : : }
10472 : :
10473 : : return NULL_TREE;
10474 : : }
10475 : :
10476 : : /* Return true if CODE is an operation that when operating on signed
10477 : : integer types involves undefined behavior on overflow and the
10478 : : operation can be expressed with unsigned arithmetic. */
10479 : :
10480 : : static bool
10481 : 283481 : arith_code_with_undefined_signed_overflow (tree_code code)
10482 : : {
10483 : 0 : switch (code)
10484 : : {
10485 : : case ABS_EXPR:
10486 : : case PLUS_EXPR:
10487 : : case MINUS_EXPR:
10488 : : case MULT_EXPR:
10489 : : case NEGATE_EXPR:
10490 : : case POINTER_PLUS_EXPR:
10491 : : return true;
10492 : 0 : default:
10493 : 0 : return false;
10494 : : }
10495 : : }
10496 : :
10497 : : /* Return true if STMT has an operation that operates on a signed
10498 : : integer types involves undefined behavior on overflow and the
10499 : : operation can be expressed with unsigned arithmetic.
10500 : : Also returns true if STMT is a VCE that needs to be rewritten
10501 : : if moved to be executed unconditionally. */
10502 : :
10503 : : bool
10504 : 1039774 : gimple_needing_rewrite_undefined (gimple *stmt)
10505 : : {
10506 : 1039774 : if (!is_gimple_assign (stmt))
10507 : : return false;
10508 : 873778 : tree lhs = gimple_assign_lhs (stmt);
10509 : 873778 : if (!lhs)
10510 : : return false;
10511 : 873778 : tree lhs_type = TREE_TYPE (lhs);
10512 : 873778 : if (!INTEGRAL_TYPE_P (lhs_type)
10513 : 104986 : && !POINTER_TYPE_P (lhs_type))
10514 : : return false;
10515 : 837484 : tree rhs = gimple_assign_rhs1 (stmt);
10516 : : /* VCE from integral types to a integral types but with
10517 : : a smaller precision need to be changed into casts
10518 : : to be well defined. */
10519 : 837484 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
10520 : 186 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
10521 : 163 : && is_gimple_val (TREE_OPERAND (rhs, 0))
10522 : 837647 : && TYPE_PRECISION (lhs_type)
10523 : 163 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
10524 : : return true;
10525 : 837321 : if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
10526 : : return false;
10527 : 283481 : if (!arith_code_with_undefined_signed_overflow
10528 : 1039611 : (gimple_assign_rhs_code (stmt)))
10529 : : return false;
10530 : : return true;
10531 : : }
10532 : :
10533 : : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
10534 : : operation that can be transformed to unsigned arithmetic by converting
10535 : : its operand, carrying out the operation in the corresponding unsigned
10536 : : type and converting the result back to the original type.
10537 : :
10538 : : If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
10539 : : return NULL.
10540 : : Otherwise returns a sequence of statements that replace STMT and also
10541 : : contain a modified form of STMT itself. */
10542 : :
10543 : : static gimple_seq
10544 : 33053 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
10545 : : bool in_place)
10546 : : {
10547 : 33053 : gcc_assert (gimple_needing_rewrite_undefined (stmt));
10548 : 33053 : if (dump_file && (dump_flags & TDF_DETAILS))
10549 : : {
10550 : 23 : fprintf (dump_file, "rewriting stmt for being uncondtional defined");
10551 : 23 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
10552 : : }
10553 : 33053 : gimple_seq stmts = NULL;
10554 : : /* VCE from integral types to another integral types but with
10555 : : smaller precisions need to be changed into casts
10556 : : to be well defined. */
10557 : 33053 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
10558 : : {
10559 : 58 : tree rhs = gimple_assign_rhs1 (stmt);
10560 : 58 : tree new_rhs = TREE_OPERAND (rhs, 0);
10561 : 58 : gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
10562 : : < TYPE_PRECISION (TREE_TYPE (new_rhs)));
10563 : 58 : gcc_assert (is_gimple_val (new_rhs));
10564 : 58 : gimple_assign_set_rhs_code (stmt, NOP_EXPR);
10565 : 58 : gimple_assign_set_rhs1 (stmt, new_rhs);
10566 : 58 : if (in_place)
10567 : 49 : update_stmt (stmt);
10568 : : else
10569 : : {
10570 : 9 : gimple_set_modified (stmt, true);
10571 : 9 : gimple_seq_add_stmt (&stmts, stmt);
10572 : : }
10573 : 58 : return stmts;
10574 : : }
10575 : 32995 : tree lhs = gimple_assign_lhs (stmt);
10576 : 32995 : tree type = unsigned_type_for (TREE_TYPE (lhs));
10577 : 32995 : if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
10578 : 17 : gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
10579 : : else
10580 : 97786 : for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
10581 : : {
10582 : 64808 : tree op = gimple_op (stmt, i);
10583 : 64808 : op = gimple_convert (&stmts, type, op);
10584 : 64808 : gimple_set_op (stmt, i, op);
10585 : : }
10586 : 32995 : gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
10587 : 32995 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
10588 : 6765 : gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
10589 : 32995 : gimple_set_modified (stmt, true);
10590 : 32995 : if (in_place)
10591 : : {
10592 : 11039 : if (stmts)
10593 : 11024 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
10594 : 11039 : stmts = NULL;
10595 : : }
10596 : : else
10597 : 21956 : gimple_seq_add_stmt (&stmts, stmt);
10598 : 32995 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
10599 : 32995 : if (in_place)
10600 : : {
10601 : 11039 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
10602 : 11039 : update_stmt (stmt);
10603 : : }
10604 : : else
10605 : 21956 : gimple_seq_add_stmt (&stmts, cvt);
10606 : :
10607 : 32995 : return stmts;
10608 : : }
10609 : :
10610 : : void
10611 : 11088 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
10612 : : {
10613 : 11088 : rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
10614 : 11088 : }
10615 : :
10616 : : gimple_seq
10617 : 21965 : rewrite_to_defined_unconditional (gimple *stmt)
10618 : : {
10619 : 21965 : return rewrite_to_defined_unconditional (nullptr, stmt, false);
10620 : : }
10621 : :
10622 : : /* The valueization hook we use for the gimple_build API simplification.
10623 : : This makes us match fold_buildN behavior by only combining with
10624 : : statements in the sequence(s) we are currently building. */
10625 : :
10626 : : static tree
10627 : 18376514 : gimple_build_valueize (tree op)
10628 : : {
10629 : 18376514 : if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
10630 : 4234762 : return op;
10631 : : return NULL_TREE;
10632 : : }
10633 : :
10634 : : /* Helper for gimple_build to perform the final insertion of stmts on SEQ. */
10635 : :
10636 : : static inline void
10637 : 1230169 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
10638 : : bool before, gsi_iterator_update update,
10639 : : gimple_seq seq)
10640 : : {
10641 : 1230169 : if (before)
10642 : : {
10643 : 57706 : if (gsi->bb)
10644 : 57706 : gsi_insert_seq_before (gsi, seq, update);
10645 : : else
10646 : 0 : gsi_insert_seq_before_without_update (gsi, seq, update);
10647 : : }
10648 : : else
10649 : : {
10650 : 1172463 : if (gsi->bb)
10651 : 34 : gsi_insert_seq_after (gsi, seq, update);
10652 : : else
10653 : 1172429 : gsi_insert_seq_after_without_update (gsi, seq, update);
10654 : : }
10655 : 1230169 : }
10656 : :
10657 : : /* Build the expression CODE OP0 of type TYPE with location LOC,
10658 : : simplifying it first if possible. Returns the built
10659 : : expression value and inserts statements possibly defining it
10660 : : before GSI if BEFORE is true or after GSI if false and advance
10661 : : the iterator accordingly.
10662 : : If gsi refers to a basic block simplifying is allowed to look
10663 : : at all SSA defs while when it does not it is restricted to
10664 : : SSA defs that are not associated with a basic block yet,
10665 : : indicating they belong to the currently building sequence. */
10666 : :
10667 : : tree
10668 : 256986 : gimple_build (gimple_stmt_iterator *gsi,
10669 : : bool before, gsi_iterator_update update,
10670 : : location_t loc, enum tree_code code, tree type, tree op0)
10671 : : {
10672 : 256986 : gimple_seq seq = NULL;
10673 : 256986 : tree res
10674 : 256986 : = gimple_simplify (code, type, op0, &seq,
10675 : 256986 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
10676 : 256986 : if (!res)
10677 : : {
10678 : 235872 : res = make_ssa_name (type);
10679 : 235872 : gimple *stmt;
10680 : 235872 : if (code == REALPART_EXPR
10681 : : || code == IMAGPART_EXPR
10682 : 235872 : || code == VIEW_CONVERT_EXPR)
10683 : 12205 : stmt = gimple_build_assign (res, code, build1 (code, type, op0));
10684 : : else
10685 : 223667 : stmt = gimple_build_assign (res, code, op0);
10686 : 235872 : gimple_set_location (stmt, loc);
10687 : 235872 : gimple_seq_add_stmt_without_update (&seq, stmt);
10688 : : }
10689 : 256986 : gimple_build_insert_seq (gsi, before, update, seq);
10690 : 256986 : return res;
10691 : : }
10692 : :
10693 : : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
10694 : : simplifying it first if possible. Returns the built
10695 : : expression value inserting any new statements at GSI honoring BEFORE
10696 : : and UPDATE. */
10697 : :
10698 : : tree
10699 : 764420 : gimple_build (gimple_stmt_iterator *gsi,
10700 : : bool before, gsi_iterator_update update,
10701 : : location_t loc, enum tree_code code, tree type,
10702 : : tree op0, tree op1)
10703 : : {
10704 : 764420 : gimple_seq seq = NULL;
10705 : 764420 : tree res
10706 : 764420 : = gimple_simplify (code, type, op0, op1, &seq,
10707 : 764420 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
10708 : 764420 : if (!res)
10709 : : {
10710 : 677391 : res = make_ssa_name (type);
10711 : 677391 : gimple *stmt = gimple_build_assign (res, code, op0, op1);
10712 : 677391 : gimple_set_location (stmt, loc);
10713 : 677391 : gimple_seq_add_stmt_without_update (&seq, stmt);
10714 : : }
10715 : 764420 : gimple_build_insert_seq (gsi, before, update, seq);
10716 : 764420 : return res;
10717 : : }
10718 : :
10719 : : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
10720 : : simplifying it first if possible. Returns the built
10721 : : expression value inserting any new statements at GSI honoring BEFORE
10722 : : and UPDATE. */
10723 : :
10724 : : tree
10725 : 41848 : gimple_build (gimple_stmt_iterator *gsi,
10726 : : bool before, gsi_iterator_update update,
10727 : : location_t loc, enum tree_code code, tree type,
10728 : : tree op0, tree op1, tree op2)
10729 : : {
10730 : :
10731 : 41848 : gimple_seq seq = NULL;
10732 : 41848 : tree res
10733 : 41848 : = gimple_simplify (code, type, op0, op1, op2, &seq,
10734 : 41848 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
10735 : 41848 : if (!res)
10736 : : {
10737 : 30426 : res = make_ssa_name (type);
10738 : 30426 : gimple *stmt;
10739 : 30426 : if (code == BIT_FIELD_REF)
10740 : 24015 : stmt = gimple_build_assign (res, code,
10741 : : build3 (code, type, op0, op1, op2));
10742 : : else
10743 : 6411 : stmt = gimple_build_assign (res, code, op0, op1, op2);
10744 : 30426 : gimple_set_location (stmt, loc);
10745 : 30426 : gimple_seq_add_stmt_without_update (&seq, stmt);
10746 : : }
10747 : 41848 : gimple_build_insert_seq (gsi, before, update, seq);
10748 : 41848 : return res;
10749 : : }
10750 : :
10751 : : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
10752 : : void) with a location LOC. Returns the built expression value (or NULL_TREE
10753 : : if TYPE is void) inserting any new statements at GSI honoring BEFORE
10754 : : and UPDATE. */
10755 : :
10756 : : tree
10757 : 0 : gimple_build (gimple_stmt_iterator *gsi,
10758 : : bool before, gsi_iterator_update update,
10759 : : location_t loc, combined_fn fn, tree type)
10760 : : {
10761 : 0 : tree res = NULL_TREE;
10762 : 0 : gimple_seq seq = NULL;
10763 : 0 : gcall *stmt;
10764 : 0 : if (internal_fn_p (fn))
10765 : 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
10766 : : else
10767 : : {
10768 : 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
10769 : 0 : stmt = gimple_build_call (decl, 0);
10770 : : }
10771 : 0 : if (!VOID_TYPE_P (type))
10772 : : {
10773 : 0 : res = make_ssa_name (type);
10774 : 0 : gimple_call_set_lhs (stmt, res);
10775 : : }
10776 : 0 : gimple_set_location (stmt, loc);
10777 : 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
10778 : 0 : gimple_build_insert_seq (gsi, before, update, seq);
10779 : 0 : return res;
10780 : : }
10781 : :
10782 : : /* Build the call FN (ARG0) with a result of type TYPE
10783 : : (or no result if TYPE is void) with location LOC,
10784 : : simplifying it first if possible. Returns the built
10785 : : expression value (or NULL_TREE if TYPE is void) inserting any new
10786 : : statements at GSI honoring BEFORE and UPDATE. */
10787 : :
10788 : : tree
10789 : 23900 : gimple_build (gimple_stmt_iterator *gsi,
10790 : : bool before, gsi_iterator_update update,
10791 : : location_t loc, combined_fn fn,
10792 : : tree type, tree arg0)
10793 : : {
10794 : 23900 : gimple_seq seq = NULL;
10795 : 23900 : tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
10796 : 23900 : if (!res)
10797 : : {
10798 : 23900 : gcall *stmt;
10799 : 23900 : if (internal_fn_p (fn))
10800 : 23529 : stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
10801 : : else
10802 : : {
10803 : 371 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
10804 : 371 : stmt = gimple_build_call (decl, 1, arg0);
10805 : : }
10806 : 23900 : if (!VOID_TYPE_P (type))
10807 : : {
10808 : 23529 : res = make_ssa_name (type);
10809 : 23529 : gimple_call_set_lhs (stmt, res);
10810 : : }
10811 : 23900 : gimple_set_location (stmt, loc);
10812 : 23900 : gimple_seq_add_stmt_without_update (&seq, stmt);
10813 : : }
10814 : 23900 : gimple_build_insert_seq (gsi, before, update, seq);
10815 : 23900 : return res;
10816 : : }
10817 : :
10818 : : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
10819 : : (or no result if TYPE is void) with location LOC,
10820 : : simplifying it first if possible. Returns the built
10821 : : expression value (or NULL_TREE if TYPE is void) inserting any new
10822 : : statements at GSI honoring BEFORE and UPDATE. */
10823 : :
10824 : : tree
10825 : 0 : gimple_build (gimple_stmt_iterator *gsi,
10826 : : bool before, gsi_iterator_update update,
10827 : : location_t loc, combined_fn fn,
10828 : : tree type, tree arg0, tree arg1)
10829 : : {
10830 : 0 : gimple_seq seq = NULL;
10831 : 0 : tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
10832 : : gimple_build_valueize);
10833 : 0 : if (!res)
10834 : : {
10835 : 0 : gcall *stmt;
10836 : 0 : if (internal_fn_p (fn))
10837 : 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
10838 : : else
10839 : : {
10840 : 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
10841 : 0 : stmt = gimple_build_call (decl, 2, arg0, arg1);
10842 : : }
10843 : 0 : if (!VOID_TYPE_P (type))
10844 : : {
10845 : 0 : res = make_ssa_name (type);
10846 : 0 : gimple_call_set_lhs (stmt, res);
10847 : : }
10848 : 0 : gimple_set_location (stmt, loc);
10849 : 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
10850 : : }
10851 : 0 : gimple_build_insert_seq (gsi, before, update, seq);
10852 : 0 : return res;
10853 : : }
10854 : :
10855 : : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
10856 : : (or no result if TYPE is void) with location LOC,
10857 : : simplifying it first if possible. Returns the built
10858 : : expression value (or NULL_TREE if TYPE is void) inserting any new
10859 : : statements at GSI honoring BEFORE and UPDATE. */
10860 : :
10861 : : tree
10862 : 0 : gimple_build (gimple_stmt_iterator *gsi,
10863 : : bool before, gsi_iterator_update update,
10864 : : location_t loc, combined_fn fn,
10865 : : tree type, tree arg0, tree arg1, tree arg2)
10866 : : {
10867 : 0 : gimple_seq seq = NULL;
10868 : 0 : tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
10869 : : &seq, gimple_build_valueize);
10870 : 0 : if (!res)
10871 : : {
10872 : 0 : gcall *stmt;
10873 : 0 : if (internal_fn_p (fn))
10874 : 0 : stmt = gimple_build_call_internal (as_internal_fn (fn),
10875 : : 3, arg0, arg1, arg2);
10876 : : else
10877 : : {
10878 : 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
10879 : 0 : stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
10880 : : }
10881 : 0 : if (!VOID_TYPE_P (type))
10882 : : {
10883 : 0 : res = make_ssa_name (type);
10884 : 0 : gimple_call_set_lhs (stmt, res);
10885 : : }
10886 : 0 : gimple_set_location (stmt, loc);
10887 : 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
10888 : : }
10889 : 0 : gimple_build_insert_seq (gsi, before, update, seq);
10890 : 0 : return res;
10891 : : }
10892 : :
10893 : : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
10894 : : void) with location LOC, simplifying it first if possible. Returns the
10895 : : built expression value (or NULL_TREE if TYPE is void) inserting any new
10896 : : statements at GSI honoring BEFORE and UPDATE. */
10897 : :
10898 : : tree
10899 : 21 : gimple_build (gimple_stmt_iterator *gsi,
10900 : : bool before, gsi_iterator_update update,
10901 : : location_t loc, code_helper code, tree type, tree op0)
10902 : : {
10903 : 21 : if (code.is_tree_code ())
10904 : 0 : return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
10905 : 21 : return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
10906 : : }
10907 : :
10908 : : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
10909 : : void) with location LOC, simplifying it first if possible. Returns the
10910 : : built expression value (or NULL_TREE if TYPE is void) inserting any new
10911 : : statements at GSI honoring BEFORE and UPDATE. */
10912 : :
10913 : : tree
10914 : 22330 : gimple_build (gimple_stmt_iterator *gsi,
10915 : : bool before, gsi_iterator_update update,
10916 : : location_t loc, code_helper code, tree type, tree op0, tree op1)
10917 : : {
10918 : 22330 : if (code.is_tree_code ())
10919 : 22330 : return gimple_build (gsi, before, update,
10920 : 22330 : loc, tree_code (code), type, op0, op1);
10921 : 0 : return gimple_build (gsi, before, update,
10922 : 0 : loc, combined_fn (code), type, op0, op1);
10923 : : }
10924 : :
10925 : : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
10926 : : is void) with location LOC, simplifying it first if possible. Returns the
10927 : : built expression value (or NULL_TREE if TYPE is void) inserting any new
10928 : : statements at GSI honoring BEFORE and UPDATE. */
10929 : :
10930 : : tree
10931 : 0 : gimple_build (gimple_stmt_iterator *gsi,
10932 : : bool before, gsi_iterator_update update,
10933 : : location_t loc, code_helper code,
10934 : : tree type, tree op0, tree op1, tree op2)
10935 : : {
10936 : 0 : if (code.is_tree_code ())
10937 : 0 : return gimple_build (gsi, before, update,
10938 : 0 : loc, tree_code (code), type, op0, op1, op2);
10939 : 0 : return gimple_build (gsi, before, update,
10940 : 0 : loc, combined_fn (code), type, op0, op1, op2);
10941 : : }
10942 : :
10943 : : /* Build the conversion (TYPE) OP with a result of type TYPE
10944 : : with location LOC if such conversion is neccesary in GIMPLE,
10945 : : simplifying it first.
10946 : : Returns the built expression inserting any new statements
10947 : : at GSI honoring BEFORE and UPDATE. */
10948 : :
10949 : : tree
10950 : 1835733 : gimple_convert (gimple_stmt_iterator *gsi,
10951 : : bool before, gsi_iterator_update update,
10952 : : location_t loc, tree type, tree op)
10953 : : {
10954 : 1835733 : if (useless_type_conversion_p (type, TREE_TYPE (op)))
10955 : : return op;
10956 : 100081 : return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
10957 : : }
10958 : :
10959 : : /* Build the conversion (ptrofftype) OP with a result of a type
10960 : : compatible with ptrofftype with location LOC if such conversion
10961 : : is neccesary in GIMPLE, simplifying it first.
10962 : : Returns the built expression value inserting any new statements
10963 : : at GSI honoring BEFORE and UPDATE. */
10964 : :
10965 : : tree
10966 : 203 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
10967 : : bool before, gsi_iterator_update update,
10968 : : location_t loc, tree op)
10969 : : {
10970 : 203 : if (ptrofftype_p (TREE_TYPE (op)))
10971 : : return op;
10972 : 0 : return gimple_convert (gsi, before, update, loc, sizetype, op);
10973 : : }
10974 : :
10975 : : /* Build a vector of type TYPE in which each element has the value OP.
10976 : : Return a gimple value for the result, inserting any new statements
10977 : : at GSI honoring BEFORE and UPDATE. */
10978 : :
10979 : : tree
10980 : 306948 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
10981 : : bool before, gsi_iterator_update update,
10982 : : location_t loc, tree type, tree op)
10983 : : {
10984 : 306948 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
10985 : : && !CONSTANT_CLASS_P (op))
10986 : : return gimple_build (gsi, before, update,
10987 : : loc, VEC_DUPLICATE_EXPR, type, op);
10988 : :
10989 : 306948 : tree res, vec = build_vector_from_val (type, op);
10990 : 306948 : if (is_gimple_val (vec))
10991 : : return vec;
10992 : 25347 : if (gimple_in_ssa_p (cfun))
10993 : 25347 : res = make_ssa_name (type);
10994 : : else
10995 : 0 : res = create_tmp_reg (type);
10996 : 25347 : gimple_seq seq = NULL;
10997 : 25347 : gimple *stmt = gimple_build_assign (res, vec);
10998 : 25347 : gimple_set_location (stmt, loc);
10999 : 25347 : gimple_seq_add_stmt_without_update (&seq, stmt);
11000 : 25347 : gimple_build_insert_seq (gsi, before, update, seq);
11001 : 25347 : return res;
11002 : : }
11003 : :
11004 : : /* Build a vector from BUILDER, handling the case in which some elements
11005 : : are non-constant. Return a gimple value for the result, inserting
11006 : : any new instructions to GSI honoring BEFORE and UPDATE.
11007 : :
11008 : : BUILDER must not have a stepped encoding on entry. This is because
11009 : : the function is not geared up to handle the arithmetic that would
11010 : : be needed in the variable case, and any code building a vector that
11011 : : is known to be constant should use BUILDER->build () directly. */
11012 : :
11013 : : tree
11014 : 364274 : gimple_build_vector (gimple_stmt_iterator *gsi,
11015 : : bool before, gsi_iterator_update update,
11016 : : location_t loc, tree_vector_builder *builder)
11017 : : {
11018 : 364274 : gcc_assert (builder->nelts_per_pattern () <= 2);
11019 : 364274 : unsigned int encoded_nelts = builder->encoded_nelts ();
11020 : 1265011 : for (unsigned int i = 0; i < encoded_nelts; ++i)
11021 : 1018405 : if (!CONSTANT_CLASS_P ((*builder)[i]))
11022 : : {
11023 : 117668 : gimple_seq seq = NULL;
11024 : 117668 : tree type = builder->type ();
11025 : 117668 : unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11026 : 117668 : vec<constructor_elt, va_gc> *v;
11027 : 117668 : vec_alloc (v, nelts);
11028 : 373346 : for (i = 0; i < nelts; ++i)
11029 : 255678 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
11030 : :
11031 : 117668 : tree res;
11032 : 117668 : if (gimple_in_ssa_p (cfun))
11033 : 117668 : res = make_ssa_name (type);
11034 : : else
11035 : 0 : res = create_tmp_reg (type);
11036 : 117668 : gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
11037 : 117668 : gimple_set_location (stmt, loc);
11038 : 117668 : gimple_seq_add_stmt_without_update (&seq, stmt);
11039 : 117668 : gimple_build_insert_seq (gsi, before, update, seq);
11040 : 117668 : return res;
11041 : : }
11042 : 246606 : return builder->build ();
11043 : : }
11044 : :
11045 : : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
11046 : : and generate a value guaranteed to be rounded upwards to ALIGN.
11047 : :
11048 : : Return the tree node representing this size, it is of TREE_TYPE TYPE. */
11049 : :
11050 : : tree
11051 : 0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
11052 : : bool before, gsi_iterator_update update,
11053 : : location_t loc, tree type,
11054 : : tree old_size, unsigned HOST_WIDE_INT align)
11055 : : {
11056 : 0 : unsigned HOST_WIDE_INT tg_mask = align - 1;
11057 : : /* tree new_size = (old_size + tg_mask) & ~tg_mask; */
11058 : 0 : gcc_assert (INTEGRAL_TYPE_P (type));
11059 : 0 : tree tree_mask = build_int_cst (type, tg_mask);
11060 : 0 : tree oversize = gimple_build (gsi, before, update,
11061 : : loc, PLUS_EXPR, type, old_size, tree_mask);
11062 : :
11063 : 0 : tree mask = build_int_cst (type, -align);
11064 : 0 : return gimple_build (gsi, before, update,
11065 : 0 : loc, BIT_AND_EXPR, type, oversize, mask);
11066 : : }
11067 : :
11068 : : /* Return true if the result of assignment STMT is known to be non-negative.
11069 : : If the return value is based on the assumption that signed overflow is
11070 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11071 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11072 : :
11073 : : static bool
11074 : 55684269 : gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11075 : : int depth)
11076 : : {
11077 : 55684269 : enum tree_code code = gimple_assign_rhs_code (stmt);
11078 : 55684269 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
11079 : 55684269 : switch (get_gimple_rhs_class (code))
11080 : : {
11081 : 11994385 : case GIMPLE_UNARY_RHS:
11082 : 11994385 : return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
11083 : : type,
11084 : : gimple_assign_rhs1 (stmt),
11085 : 11994385 : strict_overflow_p, depth);
11086 : 38434922 : case GIMPLE_BINARY_RHS:
11087 : 38434922 : return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
11088 : : type,
11089 : : gimple_assign_rhs1 (stmt),
11090 : : gimple_assign_rhs2 (stmt),
11091 : 38434922 : strict_overflow_p, depth);
11092 : : case GIMPLE_TERNARY_RHS:
11093 : : return false;
11094 : 5217014 : case GIMPLE_SINGLE_RHS:
11095 : 5217014 : return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
11096 : 5217014 : strict_overflow_p, depth);
11097 : : case GIMPLE_INVALID_RHS:
11098 : : break;
11099 : : }
11100 : 0 : gcc_unreachable ();
11101 : : }
11102 : :
11103 : : /* Return true if return value of call STMT is known to be non-negative.
11104 : : If the return value is based on the assumption that signed overflow is
11105 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11106 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11107 : :
11108 : : static bool
11109 : 20690154 : gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11110 : : int depth)
11111 : : {
11112 : 20690154 : tree arg0
11113 : 20690154 : = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
11114 : 20690154 : tree arg1
11115 : 20690154 : = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
11116 : 20690154 : tree lhs = gimple_call_lhs (stmt);
11117 : 20690154 : return (lhs
11118 : 20690154 : && tree_call_nonnegative_warnv_p (TREE_TYPE (lhs),
11119 : : gimple_call_combined_fn (stmt),
11120 : : arg0, arg1,
11121 : 20690154 : strict_overflow_p, depth));
11122 : : }
11123 : :
11124 : : /* Return true if return value of call STMT is known to be non-negative.
11125 : : If the return value is based on the assumption that signed overflow is
11126 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11127 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11128 : :
11129 : : static bool
11130 : 12888226 : gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11131 : : int depth)
11132 : : {
11133 : 26212241 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11134 : : {
11135 : 21129661 : tree arg = gimple_phi_arg_def (stmt, i);
11136 : 21129661 : if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1))
11137 : : return false;
11138 : : }
11139 : : return true;
11140 : : }
11141 : :
11142 : : /* Return true if STMT is known to compute a non-negative value.
11143 : : If the return value is based on the assumption that signed overflow is
11144 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11145 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11146 : :
11147 : : bool
11148 : 143271656 : gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11149 : : int depth)
11150 : : {
11151 : 143271656 : tree type = gimple_range_type (stmt);
11152 : 143271656 : if (type && frange::supports_p (type))
11153 : : {
11154 : 1679955 : frange r;
11155 : 1679955 : bool sign;
11156 : 1679955 : if (get_global_range_query ()->range_of_stmt (r, stmt)
11157 : 1679955 : && r.signbit_p (sign))
11158 : 28837 : return !sign;
11159 : 1679955 : }
11160 : 143242819 : switch (gimple_code (stmt))
11161 : : {
11162 : 55684269 : case GIMPLE_ASSIGN:
11163 : 55684269 : return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p,
11164 : 55684269 : depth);
11165 : 20690154 : case GIMPLE_CALL:
11166 : 20690154 : return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p,
11167 : 20690154 : depth);
11168 : 12888226 : case GIMPLE_PHI:
11169 : 12888226 : return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p,
11170 : 12888226 : depth);
11171 : : default:
11172 : : return false;
11173 : : }
11174 : : }
11175 : :
11176 : : /* Return true if the floating-point value computed by assignment STMT
11177 : : is known to have an integer value. We also allow +Inf, -Inf and NaN
11178 : : to be considered integer values. Return false for signaling NaN.
11179 : :
11180 : : DEPTH is the current nesting depth of the query. */
11181 : :
11182 : : static bool
11183 : 59843 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
11184 : : {
11185 : 59843 : enum tree_code code = gimple_assign_rhs_code (stmt);
11186 : 59843 : switch (get_gimple_rhs_class (code))
11187 : : {
11188 : 15030 : case GIMPLE_UNARY_RHS:
11189 : 15030 : return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
11190 : 15030 : gimple_assign_rhs1 (stmt), depth);
11191 : 13641 : case GIMPLE_BINARY_RHS:
11192 : 13641 : return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
11193 : : gimple_assign_rhs1 (stmt),
11194 : 13641 : gimple_assign_rhs2 (stmt), depth);
11195 : : case GIMPLE_TERNARY_RHS:
11196 : : return false;
11197 : 30091 : case GIMPLE_SINGLE_RHS:
11198 : 30091 : return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
11199 : : case GIMPLE_INVALID_RHS:
11200 : : break;
11201 : : }
11202 : 0 : gcc_unreachable ();
11203 : : }
11204 : :
11205 : : /* Return true if the floating-point value computed by call STMT is known
11206 : : to have an integer value. We also allow +Inf, -Inf and NaN to be
11207 : : considered integer values. Return false for signaling NaN.
11208 : :
11209 : : DEPTH is the current nesting depth of the query. */
11210 : :
11211 : : static bool
11212 : 874 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
11213 : : {
11214 : 874 : tree arg0 = (gimple_call_num_args (stmt) > 0
11215 : 874 : ? gimple_call_arg (stmt, 0)
11216 : : : NULL_TREE);
11217 : 874 : tree arg1 = (gimple_call_num_args (stmt) > 1
11218 : 874 : ? gimple_call_arg (stmt, 1)
11219 : : : NULL_TREE);
11220 : 874 : return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
11221 : 874 : arg0, arg1, depth);
11222 : : }
11223 : :
11224 : : /* Return true if the floating-point result of phi STMT is known to have
11225 : : an integer value. We also allow +Inf, -Inf and NaN to be considered
11226 : : integer values. Return false for signaling NaN.
11227 : :
11228 : : DEPTH is the current nesting depth of the query. */
11229 : :
11230 : : static bool
11231 : 1527 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
11232 : : {
11233 : 1707 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11234 : : {
11235 : 1701 : tree arg = gimple_phi_arg_def (stmt, i);
11236 : 1701 : if (!integer_valued_real_single_p (arg, depth + 1))
11237 : : return false;
11238 : : }
11239 : : return true;
11240 : : }
11241 : :
11242 : : /* Return true if the floating-point value computed by STMT is known
11243 : : to have an integer value. We also allow +Inf, -Inf and NaN to be
11244 : : considered integer values. Return false for signaling NaN.
11245 : :
11246 : : DEPTH is the current nesting depth of the query. */
11247 : :
11248 : : bool
11249 : 89763 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
11250 : : {
11251 : 89763 : switch (gimple_code (stmt))
11252 : : {
11253 : 59843 : case GIMPLE_ASSIGN:
11254 : 59843 : return gimple_assign_integer_valued_real_p (stmt, depth);
11255 : 874 : case GIMPLE_CALL:
11256 : 874 : return gimple_call_integer_valued_real_p (stmt, depth);
11257 : 1527 : case GIMPLE_PHI:
11258 : 1527 : return gimple_phi_integer_valued_real_p (stmt, depth);
11259 : : default:
11260 : : return false;
11261 : : }
11262 : : }
|