Line data Source code
1 : /* Statement simplification on GIMPLE.
2 : Copyright (C) 2010-2026 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 "tree-pass.h"
42 : #include "gimple-fold.h"
43 : #include "gimplify.h"
44 : #include "tree-into-ssa.h"
45 : #include "tree-dfa.h"
46 : #include "tree-object-size.h"
47 : #include "tree-ssa.h"
48 : #include "tree-ssa-propagate.h"
49 : #include "ipa-utils.h"
50 : #include "tree-ssa-address.h"
51 : #include "langhooks.h"
52 : #include "gimplify-me.h"
53 : #include "dbgcnt.h"
54 : #include "builtins.h"
55 : #include "tree-eh.h"
56 : #include "gimple-match.h"
57 : #include "gomp-constants.h"
58 : #include "optabs-query.h"
59 : #include "omp-general.h"
60 : #include "tree-cfg.h"
61 : #include "fold-const-call.h"
62 : #include "stringpool.h"
63 : #include "attribs.h"
64 : #include "asan.h"
65 : #include "diagnostic-core.h"
66 : #include "intl.h"
67 : #include "calls.h"
68 : #include "tree-vector-builder.h"
69 : #include "tree-ssa-strlen.h"
70 : #include "varasm.h"
71 : #include "internal-fn.h"
72 : #include "gimple-range.h"
73 :
74 : enum strlen_range_kind {
75 : /* Compute the exact constant string length. */
76 : SRK_STRLEN,
77 : /* Compute the maximum constant string length. */
78 : SRK_STRLENMAX,
79 : /* Compute a range of string lengths bounded by object sizes. When
80 : the length of a string cannot be determined, consider as the upper
81 : bound the size of the enclosing object the string may be a member
82 : or element of. Also determine the size of the largest character
83 : array the string may refer to. */
84 : SRK_LENRANGE,
85 : /* Determine the integer value of the argument (not string length). */
86 : SRK_INT_VALUE
87 : };
88 :
89 : static bool
90 : get_range_strlen (tree, bitmap, strlen_range_kind, c_strlen_data *, unsigned);
91 :
92 : /* Return true when DECL can be referenced from current unit.
93 : FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
94 : We can get declarations that are not possible to reference for various
95 : reasons:
96 :
97 : 1) When analyzing C++ virtual tables.
98 : C++ virtual tables do have known constructors even
99 : when they are keyed to other compilation unit.
100 : Those tables can contain pointers to methods and vars
101 : in other units. Those methods have both STATIC and EXTERNAL
102 : set.
103 : 2) In WHOPR mode devirtualization might lead to reference
104 : to method that was partitioned elsewhere.
105 : In this case we have static VAR_DECL or FUNCTION_DECL
106 : that has no corresponding callgraph/varpool node
107 : declaring the body.
108 : 3) COMDAT functions referred by external vtables that
109 : we devirtualize only during final compilation stage.
110 : At this time we already decided that we will not output
111 : the function body and thus we can't reference the symbol
112 : directly. */
113 :
114 : static bool
115 4413811 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
116 : {
117 4413811 : varpool_node *vnode;
118 4413811 : struct cgraph_node *node;
119 4413811 : symtab_node *snode;
120 :
121 4413811 : if (DECL_ABSTRACT_P (decl))
122 : return false;
123 :
124 : /* We are concerned only about static/external vars and functions. */
125 1498662 : if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
126 5508369 : || !VAR_OR_FUNCTION_DECL_P (decl))
127 : return true;
128 :
129 : /* Static objects can be referred only if they are defined and not optimized
130 : out yet. */
131 4009707 : if (!TREE_PUBLIC (decl))
132 : {
133 1098125 : if (DECL_EXTERNAL (decl))
134 : return false;
135 : /* Before we start optimizing unreachable code we can be sure all
136 : static objects are defined. */
137 1098080 : if (symtab->function_flags_ready)
138 : return true;
139 1062670 : snode = symtab_node::get (decl);
140 1062670 : if (!snode || !snode->definition)
141 : return false;
142 1062615 : node = dyn_cast <cgraph_node *> (snode);
143 1071268 : return !node || !node->inlined_to;
144 : }
145 :
146 : /* We will later output the initializer, so we can refer to it.
147 : So we are concerned only when DECL comes from initializer of
148 : external var or var that has been optimized out. */
149 2911582 : if (!from_decl
150 461851 : || !VAR_P (from_decl)
151 460722 : || (!DECL_EXTERNAL (from_decl)
152 205678 : && (vnode = varpool_node::get (from_decl)) != NULL
153 138954 : && vnode->definition)
154 3233356 : || (flag_ltrans
155 3 : && (vnode = varpool_node::get (from_decl)) != NULL
156 3 : && vnode->in_other_partition))
157 : return true;
158 : /* We are folding reference from external vtable. The vtable may refer
159 : to a symbol keyed to other compilation unit. The other compilation
160 : unit may be in separate DSO and the symbol may be hidden. */
161 321771 : if (DECL_VISIBILITY_SPECIFIED (decl)
162 315028 : && DECL_EXTERNAL (decl)
163 247215 : && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
164 485226 : && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
165 : return false;
166 : /* When function is public, we always can introduce new reference.
167 : Exception are the COMDAT functions where introducing a direct
168 : reference imply need to include function body in the current unit. */
169 158316 : if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
170 : return true;
171 : /* We have COMDAT. We are going to check if we still have definition
172 : or if the definition is going to be output in other partition.
173 : Bypass this when gimplifying; all needed functions will be produced.
174 :
175 : As observed in PR20991 for already optimized out comdat virtual functions
176 : it may be tempting to not necessarily give up because the copy will be
177 : output elsewhere when corresponding vtable is output.
178 : This is however not possible - ABI specify that COMDATs are output in
179 : units where they are used and when the other unit was compiled with LTO
180 : it is possible that vtable was kept public while the function itself
181 : was privatized. */
182 112064 : if (!symtab->function_flags_ready)
183 : return true;
184 :
185 99528 : snode = symtab_node::get (decl);
186 99528 : if (!snode
187 99528 : || ((!snode->definition || DECL_EXTERNAL (decl))
188 11829 : && (!snode->in_other_partition
189 0 : || (!snode->forced_by_abi && !snode->force_output))))
190 : return false;
191 62661 : node = dyn_cast <cgraph_node *> (snode);
192 62661 : return !node || !node->inlined_to;
193 : }
194 :
195 : /* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
196 : acceptable form for is_gimple_min_invariant.
197 : FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
198 :
199 : tree
200 15452338 : canonicalize_constructor_val (tree cval, tree from_decl)
201 : {
202 15452338 : if (CONSTANT_CLASS_P (cval))
203 : return cval;
204 :
205 9367132 : tree orig_cval = cval;
206 9367132 : STRIP_NOPS (cval);
207 9367132 : if (TREE_CODE (cval) == POINTER_PLUS_EXPR
208 9367132 : && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
209 : {
210 70271 : tree ptr = TREE_OPERAND (cval, 0);
211 70271 : if (is_gimple_min_invariant (ptr))
212 210075 : cval = build1_loc (EXPR_LOCATION (cval),
213 70025 : ADDR_EXPR, TREE_TYPE (ptr),
214 140050 : fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
215 : ptr,
216 : fold_convert (ptr_type_node,
217 : TREE_OPERAND (cval, 1))));
218 : }
219 9367132 : if (TREE_CODE (cval) == ADDR_EXPR)
220 : {
221 5125096 : tree base = NULL_TREE;
222 5125096 : if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
223 : {
224 193 : base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
225 193 : if (base)
226 193 : TREE_OPERAND (cval, 0) = base;
227 : }
228 : else
229 5124903 : base = get_base_address (TREE_OPERAND (cval, 0));
230 5125096 : if (!base)
231 0 : return NULL_TREE;
232 :
233 2277319 : if (VAR_OR_FUNCTION_DECL_P (base)
234 6417805 : && !can_refer_decl_in_current_unit_p (base, from_decl))
235 : return NULL_TREE;
236 4960861 : if (TREE_TYPE (base) == error_mark_node)
237 : return NULL_TREE;
238 4960861 : if (VAR_P (base))
239 : /* ??? We should be able to assert that TREE_ADDRESSABLE is set,
240 : but since the use can be in a debug stmt we can't. */
241 : ;
242 2276434 : else if (TREE_CODE (base) == FUNCTION_DECL)
243 : {
244 : /* Make sure we create a cgraph node for functions we'll reference.
245 : They can be non-existent if the reference comes from an entry
246 : of an external vtable for example. */
247 1291824 : cgraph_node::get_create (base);
248 : }
249 : /* Fixup types in global initializers. */
250 4960861 : if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
251 37974 : cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
252 :
253 4960861 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
254 212133 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
255 4960861 : return cval;
256 : }
257 : /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0. */
258 4242036 : if (TREE_CODE (cval) == INTEGER_CST)
259 : {
260 64065 : if (TREE_OVERFLOW_P (cval))
261 0 : cval = drop_tree_overflow (cval);
262 64065 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
263 60864 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
264 64065 : return cval;
265 : }
266 : return orig_cval;
267 : }
268 :
269 : /* If SYM is a constant variable with known value, return the value.
270 : NULL_TREE is returned otherwise. */
271 :
272 : tree
273 19951532 : get_symbol_constant_value (tree sym)
274 : {
275 19951532 : tree val = ctor_for_folding (sym);
276 19951532 : if (val != error_mark_node)
277 : {
278 39590 : if (val)
279 : {
280 37311 : val = canonicalize_constructor_val (unshare_expr (val), sym);
281 37311 : if (val
282 37311 : && is_gimple_min_invariant (val)
283 65886 : && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
284 : return val;
285 : else
286 8842 : return NULL_TREE;
287 : }
288 : /* Variables declared 'const' without an initializer
289 : have zero as the initializer if they may not be
290 : overridden at link or run time. */
291 2279 : if (!val
292 2279 : && is_gimple_reg_type (TREE_TYPE (sym)))
293 1942 : return build_zero_cst (TREE_TYPE (sym));
294 : }
295 :
296 : return NULL_TREE;
297 : }
298 :
299 :
300 :
301 : /* Subroutine of fold_stmt. We perform constant folding of the
302 : memory reference tree EXPR. */
303 :
304 : static tree
305 63006496 : maybe_fold_reference (tree expr)
306 : {
307 63006496 : tree result = NULL_TREE;
308 :
309 : /* Avoid expensive fold_const_aggregate_ref early on aggregate loads
310 : and esp. replacing STRING_CSTs inline. */
311 63006496 : if (!is_gimple_reg_type (TREE_TYPE (expr)))
312 : return NULL_TREE;
313 :
314 59937099 : if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
315 58725164 : || TREE_CODE (expr) == REALPART_EXPR
316 58047059 : || TREE_CODE (expr) == IMAGPART_EXPR)
317 61428184 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
318 3021 : result = fold_unary_loc (EXPR_LOCATION (expr),
319 : TREE_CODE (expr),
320 3021 : TREE_TYPE (expr),
321 3021 : TREE_OPERAND (expr, 0));
322 59934078 : else if (TREE_CODE (expr) == BIT_FIELD_REF
323 59934078 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
324 43 : result = fold_ternary_loc (EXPR_LOCATION (expr),
325 : TREE_CODE (expr),
326 43 : TREE_TYPE (expr),
327 43 : TREE_OPERAND (expr, 0),
328 43 : TREE_OPERAND (expr, 1),
329 43 : TREE_OPERAND (expr, 2));
330 : else
331 59934035 : result = fold_const_aggregate_ref (expr);
332 :
333 59937099 : if (result && is_gimple_min_invariant (result))
334 : return result;
335 :
336 : return NULL_TREE;
337 : }
338 :
339 : /* Return true if EXPR is an acceptable right-hand-side for a
340 : GIMPLE assignment. We validate the entire tree, not just
341 : the root node, thus catching expressions that embed complex
342 : operands that are not permitted in GIMPLE. This function
343 : is needed because the folding routines in fold-const.cc
344 : may return such expressions in some cases, e.g., an array
345 : access with an embedded index addition. It may make more
346 : sense to have folding routines that are sensitive to the
347 : constraints on GIMPLE operands, rather than abandoning any
348 : any attempt to fold if the usual folding turns out to be too
349 : aggressive. */
350 :
351 : bool
352 0 : valid_gimple_rhs_p (tree expr)
353 : {
354 0 : enum tree_code code = TREE_CODE (expr);
355 :
356 0 : switch (TREE_CODE_CLASS (code))
357 : {
358 0 : case tcc_declaration:
359 0 : if (!is_gimple_variable (expr))
360 : return false;
361 : break;
362 :
363 : case tcc_constant:
364 : /* All constants are ok. */
365 : break;
366 :
367 0 : case tcc_comparison:
368 : /* GENERIC allows comparisons with non-boolean types, reject
369 : those for GIMPLE. Let vector-typed comparisons pass - rules
370 : for GENERIC and GIMPLE are the same here. */
371 0 : if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
372 0 : && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
373 0 : || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
374 0 : && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
375 : return false;
376 :
377 : /* Fallthru. */
378 0 : case tcc_binary:
379 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0))
380 0 : || !is_gimple_val (TREE_OPERAND (expr, 1)))
381 0 : return false;
382 : break;
383 :
384 0 : case tcc_unary:
385 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0)))
386 : return false;
387 : break;
388 :
389 0 : case tcc_expression:
390 0 : switch (code)
391 : {
392 0 : case ADDR_EXPR:
393 0 : {
394 0 : tree t;
395 0 : if (is_gimple_min_invariant (expr))
396 : return true;
397 0 : t = TREE_OPERAND (expr, 0);
398 0 : while (handled_component_p (t))
399 : {
400 : /* ??? More checks needed, see the GIMPLE verifier. */
401 0 : if ((TREE_CODE (t) == ARRAY_REF
402 0 : || TREE_CODE (t) == ARRAY_RANGE_REF)
403 0 : && !is_gimple_val (TREE_OPERAND (t, 1)))
404 : return false;
405 0 : t = TREE_OPERAND (t, 0);
406 : }
407 0 : if (!is_gimple_id (t))
408 : return false;
409 : }
410 : break;
411 :
412 0 : default:
413 0 : if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
414 : {
415 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0))
416 0 : || !is_gimple_val (TREE_OPERAND (expr, 1))
417 0 : || !is_gimple_val (TREE_OPERAND (expr, 2)))
418 0 : return false;
419 : break;
420 : }
421 : return false;
422 : }
423 : break;
424 :
425 : case tcc_vl_exp:
426 : return false;
427 :
428 0 : case tcc_exceptional:
429 0 : if (code == CONSTRUCTOR)
430 : {
431 : unsigned i;
432 : tree elt;
433 0 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
434 0 : if (!is_gimple_val (elt))
435 : return false;
436 : return true;
437 : }
438 0 : if (code != SSA_NAME)
439 : return false;
440 : break;
441 :
442 0 : case tcc_reference:
443 0 : if (code == BIT_FIELD_REF)
444 0 : return is_gimple_val (TREE_OPERAND (expr, 0));
445 : return false;
446 :
447 : default:
448 : return false;
449 : }
450 :
451 : return true;
452 : }
453 :
454 :
455 : /* Attempt to fold an assignment statement pointed-to by SI. Returns a
456 : replacement rhs for the statement or NULL_TREE if no simplification
457 : could be made. It is assumed that the operands have been previously
458 : folded. */
459 :
460 : static tree
461 251881662 : fold_gimple_assign (gimple_stmt_iterator *si)
462 : {
463 251881662 : gimple *stmt = gsi_stmt (*si);
464 251881662 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
465 251881662 : location_t loc = gimple_location (stmt);
466 :
467 251881662 : tree result = NULL_TREE;
468 :
469 251881662 : switch (get_gimple_rhs_class (subcode))
470 : {
471 166658948 : case GIMPLE_SINGLE_RHS:
472 166658948 : {
473 166658948 : tree rhs = gimple_assign_rhs1 (stmt);
474 :
475 166658948 : if (TREE_CLOBBER_P (rhs))
476 : return NULL_TREE;
477 :
478 153580996 : if (REFERENCE_CLASS_P (rhs))
479 60518633 : return maybe_fold_reference (rhs);
480 :
481 93062363 : else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
482 : {
483 157520 : tree val = OBJ_TYPE_REF_EXPR (rhs);
484 157520 : if (is_gimple_min_invariant (val))
485 : return val;
486 157498 : else if (flag_devirtualize && virtual_method_call_p (rhs))
487 : {
488 157458 : bool final;
489 157458 : vec <cgraph_node *>targets
490 157458 : = possible_polymorphic_call_targets (rhs, stmt, &final);
491 157732 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
492 : {
493 44 : if (dump_enabled_p ())
494 : {
495 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
496 : "resolving virtual function address "
497 : "reference to function %s\n",
498 0 : targets.length () == 1
499 0 : ? targets[0]->name ()
500 : : "NULL");
501 : }
502 44 : if (targets.length () == 1)
503 : {
504 33 : val = fold_convert (TREE_TYPE (val),
505 : build_fold_addr_expr_loc
506 : (loc, targets[0]->decl));
507 33 : STRIP_USELESS_TYPE_CONVERSION (val);
508 : }
509 : else
510 : /* We cannot use __builtin_unreachable here because it
511 : cannot have address taken. */
512 11 : val = build_int_cst (TREE_TYPE (val), 0);
513 44 : return val;
514 : }
515 : }
516 : }
517 :
518 92904843 : else if (TREE_CODE (rhs) == ADDR_EXPR)
519 : {
520 14538545 : tree ref = TREE_OPERAND (rhs, 0);
521 14538545 : if (TREE_CODE (ref) == MEM_REF
522 14538545 : && integer_zerop (TREE_OPERAND (ref, 1)))
523 : {
524 2273 : result = TREE_OPERAND (ref, 0);
525 2273 : if (!useless_type_conversion_p (TREE_TYPE (rhs),
526 2273 : TREE_TYPE (result)))
527 0 : result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
528 2273 : return result;
529 : }
530 : }
531 :
532 78366298 : else if (TREE_CODE (rhs) == CONSTRUCTOR
533 78366298 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
534 : {
535 : /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
536 : unsigned i;
537 : tree val;
538 :
539 426686 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
540 422401 : if (! CONSTANT_CLASS_P (val))
541 : return NULL_TREE;
542 :
543 4285 : return build_vector_from_ctor (TREE_TYPE (rhs),
544 8570 : CONSTRUCTOR_ELTS (rhs));
545 : }
546 :
547 78030808 : else if (DECL_P (rhs)
548 78030808 : && is_gimple_reg_type (TREE_TYPE (rhs)))
549 12042072 : return get_symbol_constant_value (rhs);
550 : }
551 : break;
552 :
553 : case GIMPLE_UNARY_RHS:
554 : break;
555 :
556 : case GIMPLE_BINARY_RHS:
557 : break;
558 :
559 513027 : case GIMPLE_TERNARY_RHS:
560 1026054 : result = fold_ternary_loc (loc, subcode,
561 513027 : TREE_TYPE (gimple_assign_lhs (stmt)),
562 : gimple_assign_rhs1 (stmt),
563 : gimple_assign_rhs2 (stmt),
564 : gimple_assign_rhs3 (stmt));
565 :
566 513027 : if (result)
567 : {
568 0 : STRIP_USELESS_TYPE_CONVERSION (result);
569 0 : if (valid_gimple_rhs_p (result))
570 : return result;
571 : }
572 : break;
573 :
574 0 : case GIMPLE_INVALID_RHS:
575 0 : gcc_unreachable ();
576 : }
577 :
578 : return NULL_TREE;
579 : }
580 :
581 :
582 : /* Replace a statement at *SI_P with a sequence of statements in STMTS,
583 : adjusting the replacement stmts location and virtual operands.
584 :
585 : If the statements has an lhs, either:
586 :
587 : - the last statement of the new sequence must assign to the same lhs or
588 :
589 : - the caller must ensure that all uses of the old lhs have been
590 : removed before calling this function. This includes removing
591 : all debug uses. */
592 :
593 : void
594 123849 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
595 : {
596 123849 : gimple *stmt = gsi_stmt (*si_p);
597 :
598 123849 : if (gimple_has_location (stmt))
599 101741 : annotate_all_with_location (stmts, gimple_location (stmt));
600 :
601 : /* First iterate over the replacement statements backward, assigning
602 : virtual operands to their defining statements. */
603 123849 : gimple *laststore = NULL;
604 247698 : for (gimple_stmt_iterator i = gsi_last (stmts);
605 512307 : !gsi_end_p (i); gsi_prev (&i))
606 : {
607 194229 : gimple *new_stmt = gsi_stmt (i);
608 194229 : if ((gimple_assign_single_p (new_stmt)
609 119882 : && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
610 313841 : || (is_gimple_call (new_stmt)
611 14982 : && (gimple_call_flags (new_stmt)
612 14982 : & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
613 : {
614 2386 : tree vdef;
615 2386 : if (!laststore)
616 2380 : vdef = gimple_vdef (stmt);
617 : else
618 6 : vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
619 2386 : gimple_set_vdef (new_stmt, vdef);
620 2386 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
621 1383 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
622 : laststore = new_stmt;
623 : }
624 : }
625 :
626 : /* Second iterate over the statements forward, assigning virtual
627 : operands to their uses. */
628 123849 : tree reaching_vuse = gimple_vuse (stmt);
629 123849 : for (gimple_stmt_iterator i = gsi_start (stmts);
630 318078 : !gsi_end_p (i); gsi_next (&i))
631 : {
632 194229 : gimple *new_stmt = gsi_stmt (i);
633 : /* If the new statement possibly has a VUSE, update it with exact SSA
634 : name we know will reach this one. */
635 194229 : if (gimple_has_mem_ops (new_stmt))
636 194227 : gimple_set_vuse (new_stmt, reaching_vuse);
637 194229 : gimple_set_modified (new_stmt, true);
638 581296 : if (gimple_vdef (new_stmt))
639 194229 : reaching_vuse = gimple_vdef (new_stmt);
640 : }
641 :
642 : /* If the new sequence does not do a store release the virtual
643 : definition of the original statement. */
644 123849 : if (reaching_vuse
645 204312 : && reaching_vuse == gimple_vuse (stmt))
646 : {
647 79086 : tree vdef = gimple_vdef (stmt);
648 79086 : if (vdef
649 1469 : && TREE_CODE (vdef) == SSA_NAME)
650 : {
651 1413 : unlink_stmt_vdef (stmt);
652 1413 : release_ssa_name (vdef);
653 : }
654 : }
655 :
656 : /* Finally replace the original statement with the sequence. */
657 123849 : gsi_replace_with_seq (si_p, stmts, false);
658 123849 : }
659 :
660 : /* Helper function for update_gimple_call and
661 : gimplify_and_update_call_from_tree. A GIMPLE_CALL STMT is being replaced
662 : with GIMPLE_CALL NEW_STMT. */
663 :
664 : static void
665 2441 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
666 : gimple *stmt)
667 : {
668 2441 : tree lhs = gimple_call_lhs (stmt);
669 2441 : gimple_call_set_lhs (new_stmt, lhs);
670 2441 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
671 794 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
672 2441 : gimple_move_vops (new_stmt, stmt);
673 2441 : gimple_set_location (new_stmt, gimple_location (stmt));
674 2441 : if (gimple_block (new_stmt) == NULL_TREE)
675 1 : gimple_set_block (new_stmt, gimple_block (stmt));
676 2441 : gsi_replace (si_p, new_stmt, false);
677 2441 : }
678 :
679 : /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
680 : with number of arguments NARGS, where the arguments in GIMPLE form
681 : follow NARGS argument. */
682 :
683 : bool
684 2438 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
685 : {
686 2438 : va_list ap;
687 2438 : gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
688 :
689 2438 : gcc_assert (is_gimple_call (stmt));
690 2438 : va_start (ap, nargs);
691 2438 : new_stmt = gimple_build_call_valist (fn, nargs, ap);
692 2438 : finish_update_gimple_call (si_p, new_stmt, stmt);
693 2438 : va_end (ap);
694 2438 : return true;
695 : }
696 :
697 : /* Return true if EXPR is a CALL_EXPR suitable for representation
698 : as a single GIMPLE_CALL statement. If the arguments require
699 : further gimplification, return false. */
700 :
701 : static bool
702 59003 : valid_gimple_call_p (tree expr)
703 : {
704 59003 : unsigned i, nargs;
705 :
706 59003 : if (TREE_CODE (expr) != CALL_EXPR)
707 : return false;
708 :
709 3 : nargs = call_expr_nargs (expr);
710 6 : for (i = 0; i < nargs; i++)
711 : {
712 3 : tree arg = CALL_EXPR_ARG (expr, i);
713 3 : if (is_gimple_reg_type (TREE_TYPE (arg)))
714 : {
715 3 : if (!is_gimple_val (arg))
716 : return false;
717 : }
718 : else
719 0 : if (!is_gimple_lvalue (arg))
720 : return false;
721 : }
722 :
723 : return true;
724 : }
725 :
726 : /* Convert EXPR into a GIMPLE value suitable for substitution on the
727 : RHS of an assignment. Insert the necessary statements before
728 : iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
729 : is replaced. If the call is expected to produces a result, then it
730 : is replaced by an assignment of the new RHS to the result variable.
731 : If the result is to be ignored, then the call is replaced by a
732 : GIMPLE_NOP. A proper VDEF chain is retained by making the first
733 : VUSE and the last VDEF of the whole sequence be the same as the replaced
734 : statement and using new SSA names for stores in between. */
735 :
736 : void
737 59003 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
738 : {
739 59003 : tree lhs;
740 59003 : gimple *stmt, *new_stmt;
741 59003 : gimple_stmt_iterator i;
742 59003 : gimple_seq stmts = NULL;
743 :
744 59003 : stmt = gsi_stmt (*si_p);
745 :
746 59003 : gcc_assert (is_gimple_call (stmt));
747 :
748 59003 : if (valid_gimple_call_p (expr))
749 : {
750 : /* The call has simplified to another call. */
751 3 : tree fn = CALL_EXPR_FN (expr);
752 3 : unsigned i;
753 3 : unsigned nargs = call_expr_nargs (expr);
754 3 : vec<tree> args = vNULL;
755 3 : gcall *new_stmt;
756 :
757 3 : if (nargs > 0)
758 : {
759 3 : args.create (nargs);
760 3 : args.safe_grow_cleared (nargs, true);
761 :
762 9 : for (i = 0; i < nargs; i++)
763 3 : args[i] = CALL_EXPR_ARG (expr, i);
764 : }
765 :
766 3 : new_stmt = gimple_build_call_vec (fn, args);
767 3 : finish_update_gimple_call (si_p, new_stmt, stmt);
768 3 : args.release ();
769 3 : return;
770 : }
771 :
772 59000 : lhs = gimple_call_lhs (stmt);
773 59000 : if (lhs == NULL_TREE)
774 : {
775 2466 : push_gimplify_context (gimple_in_ssa_p (cfun));
776 1233 : gimplify_and_add (expr, &stmts);
777 1233 : pop_gimplify_context (NULL);
778 :
779 : /* We can end up with folding a memcpy of an empty class assignment
780 : which gets optimized away by C++ gimplification. */
781 1233 : if (gimple_seq_empty_p (stmts))
782 : {
783 1098 : if (gimple_in_ssa_p (cfun))
784 : {
785 1098 : unlink_stmt_vdef (stmt);
786 1098 : release_defs (stmt);
787 : }
788 1098 : gsi_replace (si_p, gimple_build_nop (), false);
789 1098 : return;
790 : }
791 : }
792 : else
793 : {
794 57767 : tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
795 57767 : new_stmt = gimple_build_assign (lhs, tmp);
796 57767 : i = gsi_last (stmts);
797 57767 : gsi_insert_after_without_update (&i, new_stmt,
798 : GSI_CONTINUE_LINKING);
799 : }
800 :
801 57902 : gsi_replace_with_seq_vops (si_p, stmts);
802 : }
803 :
804 : /* Print a message in the dump file recording transformation of FROM to TO. */
805 :
806 : static void
807 40566 : dump_transformation (gcall *from, gcall *to)
808 : {
809 40566 : if (dump_enabled_p ())
810 12 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n",
811 : gimple_call_fn (from), gimple_call_fn (to));
812 40566 : }
813 :
814 : /* Replace the call at *GSI with the gimple value VAL. */
815 :
816 : void
817 85006 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
818 : {
819 85006 : gimple *stmt = gsi_stmt (*gsi);
820 85006 : tree lhs = gimple_call_lhs (stmt);
821 85006 : gimple *repl;
822 85006 : if (lhs)
823 : {
824 79993 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
825 2108 : val = fold_convert (TREE_TYPE (lhs), val);
826 79993 : repl = gimple_build_assign (lhs, val);
827 : }
828 : else
829 5013 : repl = gimple_build_nop ();
830 85006 : tree vdef = gimple_vdef (stmt);
831 85006 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
832 : {
833 5587 : unlink_stmt_vdef (stmt);
834 5587 : release_ssa_name (vdef);
835 : }
836 85006 : gsi_replace (gsi, repl, false);
837 85006 : }
838 :
839 : /* Replace the call at *GSI with the new call REPL and fold that
840 : again. */
841 :
842 : static void
843 40566 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
844 : {
845 40566 : gimple *stmt = gsi_stmt (*gsi);
846 40566 : dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
847 40566 : gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
848 40566 : gimple_set_location (repl, gimple_location (stmt));
849 40566 : gimple_move_vops (repl, stmt);
850 40566 : gsi_replace (gsi, repl, false);
851 40566 : fold_stmt (gsi);
852 40566 : }
853 :
854 : /* Return true if VAR is a VAR_DECL or a component thereof. */
855 :
856 : static bool
857 386679 : var_decl_component_p (tree var)
858 : {
859 386679 : tree inner = var;
860 551081 : while (handled_component_p (inner))
861 164402 : inner = TREE_OPERAND (inner, 0);
862 386679 : return (DECL_P (inner)
863 386679 : || (TREE_CODE (inner) == MEM_REF
864 39559 : && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR));
865 : }
866 :
867 : /* Return TRUE if the SIZE argument, representing the size of an
868 : object, is in a range of values of which exactly zero is valid. */
869 :
870 : static bool
871 982587 : size_must_be_zero_p (tree size)
872 : {
873 982587 : if (integer_zerop (size))
874 : return true;
875 :
876 980022 : if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
877 : return false;
878 :
879 590626 : tree type = TREE_TYPE (size);
880 590626 : int prec = TYPE_PRECISION (type);
881 :
882 : /* Compute the value of SSIZE_MAX, the largest positive value that
883 : can be stored in ssize_t, the signed counterpart of size_t. */
884 590626 : wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
885 590626 : wide_int zero = wi::zero (TYPE_PRECISION (type));
886 590626 : int_range_max valid_range (type, zero, ssize_max);
887 590626 : int_range_max vr;
888 1181252 : get_range_query (cfun)->range_of_expr (vr, size);
889 :
890 590626 : if (vr.undefined_p ())
891 87 : vr.set_varying (TREE_TYPE (size));
892 590626 : vr.intersect (valid_range);
893 590626 : return vr.zero_p ();
894 590626 : }
895 :
896 : /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
897 : diagnose (otherwise undefined) overlapping copies without preventing
898 : folding. When folded, GCC guarantees that overlapping memcpy has
899 : the same semantics as memmove. Call to the library memcpy need not
900 : provide the same guarantee. Return false if no simplification can
901 : be made. */
902 :
903 : static bool
904 982587 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
905 : tree dest, tree src, enum built_in_function code)
906 : {
907 982587 : gimple *stmt = gsi_stmt (*gsi);
908 982587 : tree lhs = gimple_call_lhs (stmt);
909 982587 : tree len = gimple_call_arg (stmt, 2);
910 982587 : location_t loc = gimple_location (stmt);
911 :
912 : /* If the LEN parameter is a constant zero or in range where
913 : the only valid value is zero, return DEST. */
914 982587 : if (size_must_be_zero_p (len))
915 : {
916 2597 : gimple *repl;
917 2597 : if (gimple_call_lhs (stmt))
918 58 : repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
919 : else
920 2539 : repl = gimple_build_nop ();
921 2597 : tree vdef = gimple_vdef (stmt);
922 2597 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
923 : {
924 441 : unlink_stmt_vdef (stmt);
925 441 : release_ssa_name (vdef);
926 : }
927 2597 : gsi_replace (gsi, repl, false);
928 2597 : return true;
929 : }
930 :
931 : /* If SRC and DEST are the same (and not volatile), return
932 : DEST{,+LEN,+LEN-1}. */
933 979990 : if (operand_equal_p (src, dest, 0))
934 : {
935 : /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
936 : It's safe and may even be emitted by GCC itself (see bug
937 : 32667). */
938 73 : unlink_stmt_vdef (stmt);
939 146 : if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
940 33 : release_ssa_name (gimple_vdef (stmt));
941 73 : if (!lhs)
942 : {
943 52 : gsi_replace (gsi, gimple_build_nop (), false);
944 52 : return true;
945 : }
946 21 : goto done;
947 : }
948 1959834 : else if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
949 : return false;
950 : else
951 : {
952 : /* We cannot (easily) change the type of the copy if it is a storage
953 : order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
954 : modify the storage order of objects (see storage_order_barrier_p). */
955 979917 : tree srctype
956 993198 : = POINTER_TYPE_P (TREE_TYPE (src))
957 993198 : ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
958 979917 : tree desttype
959 1000141 : = POINTER_TYPE_P (TREE_TYPE (dest))
960 1000141 : ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
961 979917 : tree destvar, srcvar, srcoff;
962 979917 : unsigned int src_align, dest_align;
963 979917 : unsigned HOST_WIDE_INT tmp_len;
964 979917 : const char *tmp_str;
965 :
966 : /* Build accesses at offset zero with a ref-all character type. */
967 979917 : tree off0
968 979917 : = build_int_cst (build_pointer_type_for_mode (char_type_node,
969 : ptr_mode, true), 0);
970 :
971 : /* If we can perform the copy efficiently with first doing all loads
972 : and then all stores inline it that way. Currently efficiently
973 : means that we can load all the memory into a single integer
974 : register which is what MOVE_MAX gives us. */
975 979917 : src_align = get_pointer_alignment (src);
976 979917 : dest_align = get_pointer_alignment (dest);
977 979917 : if (tree_fits_uhwi_p (len)
978 379463 : && compare_tree_int (len, MOVE_MAX) <= 0
979 : /* FIXME: Don't transform copies from strings with known length.
980 : Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
981 : from being handled, and the case was XFAILed for that reason.
982 : Now that it is handled and the XFAIL removed, as soon as other
983 : strlenopt tests that rely on it for passing are adjusted, this
984 : hack can be removed. */
985 282884 : && !c_strlen (src, 1)
986 190809 : && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
987 80871 : && memchr (tmp_str, 0, tmp_len) == NULL)
988 123445 : && !(srctype
989 123445 : && AGGREGATE_TYPE_P (srctype)
990 59435 : && TYPE_REVERSE_STORAGE_ORDER (srctype))
991 1103229 : && !(desttype
992 123312 : && AGGREGATE_TYPE_P (desttype)
993 67959 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
994 : {
995 123279 : unsigned ilen = tree_to_uhwi (len);
996 123279 : if (pow2p_hwi (ilen))
997 : {
998 : /* Detect out-of-bounds accesses without issuing warnings.
999 : Avoid folding out-of-bounds copies but to avoid false
1000 : positives for unreachable code defer warning until after
1001 : DCE has worked its magic.
1002 : -Wrestrict is still diagnosed. */
1003 24782 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
1004 : dest, src, len, len,
1005 24782 : false, false))
1006 981 : if (warning != OPT_Wrestrict)
1007 21422 : return false;
1008 :
1009 23860 : scalar_int_mode imode;
1010 23860 : machine_mode mode;
1011 23860 : if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
1012 23860 : && bitwise_mode_for_size (ilen
1013 23860 : * BITS_PER_UNIT).exists (&mode)
1014 47720 : && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT)
1015 : /* If the destination pointer is not aligned we must be able
1016 : to emit an unaligned store. */
1017 23860 : && (dest_align >= GET_MODE_ALIGNMENT (mode)
1018 12974 : || !targetm.slow_unaligned_access (mode, dest_align)
1019 0 : || (optab_handler (movmisalign_optab, mode)
1020 : != CODE_FOR_nothing)))
1021 : {
1022 23860 : tree type = bitwise_type_for_mode (mode);
1023 23860 : tree srctype = type;
1024 23860 : tree desttype = type;
1025 23860 : if (src_align < GET_MODE_ALIGNMENT (mode))
1026 12507 : srctype = build_aligned_type (type, src_align);
1027 23860 : tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
1028 23860 : tree tem = fold_const_aggregate_ref (srcmem);
1029 23860 : if (tem)
1030 : srcmem = tem;
1031 22810 : else if (src_align < GET_MODE_ALIGNMENT (mode)
1032 12250 : && targetm.slow_unaligned_access (mode, src_align)
1033 22810 : && (optab_handler (movmisalign_optab, mode)
1034 : == CODE_FOR_nothing))
1035 : srcmem = NULL_TREE;
1036 22810 : if (srcmem)
1037 : {
1038 23860 : gimple *new_stmt;
1039 23860 : if (is_gimple_reg_type (TREE_TYPE (srcmem)))
1040 : {
1041 23860 : new_stmt = gimple_build_assign (NULL_TREE, srcmem);
1042 23860 : srcmem
1043 23860 : = make_ssa_name (TREE_TYPE (srcmem), new_stmt);
1044 23860 : gimple_assign_set_lhs (new_stmt, srcmem);
1045 47720 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1046 23860 : gimple_set_location (new_stmt, loc);
1047 23860 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1048 : }
1049 23860 : if (dest_align < GET_MODE_ALIGNMENT (mode))
1050 12974 : desttype = build_aligned_type (type, dest_align);
1051 23860 : new_stmt
1052 23860 : = gimple_build_assign (fold_build2 (MEM_REF, desttype,
1053 : dest, off0),
1054 : srcmem);
1055 23860 : gimple_move_vops (new_stmt, stmt);
1056 23860 : if (!lhs)
1057 : {
1058 20500 : gsi_replace (gsi, new_stmt, false);
1059 20500 : return true;
1060 : }
1061 3360 : gimple_set_location (new_stmt, loc);
1062 3360 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1063 3360 : goto done;
1064 : }
1065 : }
1066 : }
1067 : }
1068 :
1069 955135 : if (code == BUILT_IN_MEMMOVE)
1070 : {
1071 : /* Both DEST and SRC must be pointer types.
1072 : ??? This is what old code did. Is the testing for pointer types
1073 : really mandatory?
1074 :
1075 : If either SRC is readonly or length is 1, we can use memcpy. */
1076 200895 : if (!dest_align || !src_align)
1077 : return false;
1078 200895 : if (readonly_data_expr (src)
1079 200895 : || (tree_fits_uhwi_p (len)
1080 32896 : && (MIN (src_align, dest_align) / BITS_PER_UNIT
1081 32896 : >= tree_to_uhwi (len))))
1082 : {
1083 921603 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1084 20037 : if (!fn)
1085 : return false;
1086 20037 : gimple_call_set_fndecl (stmt, fn);
1087 20037 : gimple_call_set_arg (stmt, 0, dest);
1088 20037 : gimple_call_set_arg (stmt, 1, src);
1089 20037 : fold_stmt (gsi);
1090 20037 : return true;
1091 : }
1092 :
1093 : /* If *src and *dest can't overlap, optimize into memcpy as well. */
1094 180858 : if (TREE_CODE (src) == ADDR_EXPR
1095 5468 : && TREE_CODE (dest) == ADDR_EXPR)
1096 : {
1097 1798 : tree src_base, dest_base, fn;
1098 1798 : poly_int64 src_offset = 0, dest_offset = 0;
1099 1798 : poly_uint64 maxsize;
1100 :
1101 1798 : srcvar = TREE_OPERAND (src, 0);
1102 1798 : src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
1103 1798 : if (src_base == NULL)
1104 0 : src_base = srcvar;
1105 1798 : destvar = TREE_OPERAND (dest, 0);
1106 1798 : dest_base = get_addr_base_and_unit_offset (destvar,
1107 : &dest_offset);
1108 1798 : if (dest_base == NULL)
1109 0 : dest_base = destvar;
1110 1798 : if (!poly_int_tree_p (len, &maxsize))
1111 229 : maxsize = -1;
1112 1798 : if (SSA_VAR_P (src_base)
1113 1788 : && SSA_VAR_P (dest_base))
1114 : {
1115 1788 : if (operand_equal_p (src_base, dest_base, 0)
1116 1788 : && ranges_maybe_overlap_p (src_offset, maxsize,
1117 : dest_offset, maxsize))
1118 : return false;
1119 : }
1120 10 : else if (TREE_CODE (src_base) == MEM_REF
1121 0 : && TREE_CODE (dest_base) == MEM_REF)
1122 : {
1123 0 : if (! operand_equal_p (TREE_OPERAND (src_base, 0),
1124 0 : TREE_OPERAND (dest_base, 0), 0))
1125 0 : return false;
1126 0 : poly_offset_int full_src_offset
1127 0 : = mem_ref_offset (src_base) + src_offset;
1128 0 : poly_offset_int full_dest_offset
1129 0 : = mem_ref_offset (dest_base) + dest_offset;
1130 0 : if (ranges_maybe_overlap_p (full_src_offset, maxsize,
1131 : full_dest_offset, maxsize))
1132 : return false;
1133 0 : }
1134 : else
1135 : return false;
1136 :
1137 1798 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1138 1382 : if (!fn)
1139 : return false;
1140 1382 : gimple_call_set_fndecl (stmt, fn);
1141 1382 : gimple_call_set_arg (stmt, 0, dest);
1142 1382 : gimple_call_set_arg (stmt, 1, src);
1143 1382 : fold_stmt (gsi);
1144 1382 : return true;
1145 : }
1146 :
1147 : /* If the destination and source do not alias optimize into
1148 : memcpy as well. */
1149 179060 : if ((is_gimple_min_invariant (dest)
1150 175383 : || TREE_CODE (dest) == SSA_NAME)
1151 336122 : && (is_gimple_min_invariant (src)
1152 157090 : || TREE_CODE (src) == SSA_NAME))
1153 : {
1154 160296 : ao_ref destr, srcr;
1155 160296 : ao_ref_init_from_ptr_and_size (&destr, dest, len);
1156 160296 : ao_ref_init_from_ptr_and_size (&srcr, src, len);
1157 160296 : if (!refs_may_alias_p_1 (&destr, &srcr, false))
1158 : {
1159 10409 : tree fn;
1160 10409 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1161 10409 : if (!fn)
1162 10409 : return false;
1163 10409 : gimple_call_set_fndecl (stmt, fn);
1164 10409 : gimple_call_set_arg (stmt, 0, dest);
1165 10409 : gimple_call_set_arg (stmt, 1, src);
1166 10409 : fold_stmt (gsi);
1167 10409 : return true;
1168 : }
1169 : }
1170 :
1171 168651 : return false;
1172 : }
1173 :
1174 754240 : if (!tree_fits_shwi_p (len))
1175 : return false;
1176 304768 : if (!srctype
1177 304768 : || (AGGREGATE_TYPE_P (srctype)
1178 185537 : && TYPE_REVERSE_STORAGE_ORDER (srctype)))
1179 : return false;
1180 304635 : if (!desttype
1181 304635 : || (AGGREGATE_TYPE_P (desttype)
1182 180413 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
1183 : return false;
1184 : /* In the following try to find a type that is most natural to be
1185 : used for the memcpy source and destination and that allows
1186 : the most optimization when memcpy is turned into a plain assignment
1187 : using that type. In theory we could always use a char[len] type
1188 : but that only gains us that the destination and source possibly
1189 : no longer will have their address taken. */
1190 304602 : if (TREE_CODE (srctype) == ARRAY_TYPE
1191 304602 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1192 111529 : srctype = TREE_TYPE (srctype);
1193 304602 : if (TREE_CODE (desttype) == ARRAY_TYPE
1194 304602 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
1195 93382 : desttype = TREE_TYPE (desttype);
1196 304602 : if (TREE_ADDRESSABLE (srctype)
1197 304571 : || TREE_ADDRESSABLE (desttype))
1198 : return false;
1199 :
1200 : /* Make sure we are not copying using a floating-point mode or
1201 : a type whose size possibly does not match its precision. */
1202 608525 : if (FLOAT_MODE_P (TYPE_MODE (desttype))
1203 303718 : || TREE_CODE (desttype) == BOOLEAN_TYPE
1204 608240 : || TREE_CODE (desttype) == ENUMERAL_TYPE)
1205 856 : desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
1206 608655 : if (FLOAT_MODE_P (TYPE_MODE (srctype))
1207 303999 : || TREE_CODE (srctype) == BOOLEAN_TYPE
1208 608525 : || TREE_CODE (srctype) == ENUMERAL_TYPE)
1209 571 : srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
1210 304544 : if (!srctype)
1211 120 : srctype = desttype;
1212 304544 : if (!desttype)
1213 0 : desttype = srctype;
1214 304544 : if (!srctype)
1215 : return false;
1216 :
1217 304544 : src_align = get_pointer_alignment (src);
1218 304544 : dest_align = get_pointer_alignment (dest);
1219 :
1220 : /* Choose between src and destination type for the access based
1221 : on alignment, whether the access constitutes a register access
1222 : and whether it may actually expose a declaration for SSA rewrite
1223 : or SRA decomposition. Also try to expose a string constant, we
1224 : might be able to concatenate several of them later into a single
1225 : string store. */
1226 304544 : destvar = NULL_TREE;
1227 304544 : srcvar = NULL_TREE;
1228 304544 : if (TREE_CODE (dest) == ADDR_EXPR
1229 104520 : && var_decl_component_p (TREE_OPERAND (dest, 0))
1230 104516 : && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
1231 24591 : && dest_align >= TYPE_ALIGN (desttype)
1232 329135 : && (is_gimple_reg_type (desttype)
1233 24169 : || src_align >= TYPE_ALIGN (desttype)))
1234 19477 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1235 285067 : else if (TREE_CODE (src) == ADDR_EXPR
1236 221024 : && var_decl_component_p (TREE_OPERAND (src, 0))
1237 44577 : && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
1238 8603 : && src_align >= TYPE_ALIGN (srctype)
1239 293654 : && (is_gimple_reg_type (srctype)
1240 8424 : || dest_align >= TYPE_ALIGN (srctype)))
1241 3100 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1242 : /* FIXME: Don't transform copies from strings with known original length.
1243 : As soon as strlenopt tests that rely on it for passing are adjusted,
1244 : this hack can be removed. */
1245 281967 : else if (gimple_call_alloca_for_var_p (stmt)
1246 115 : && (srcvar = string_constant (src, &srcoff, NULL, NULL))
1247 3 : && integer_zerop (srcoff)
1248 3 : && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar)), len)
1249 281970 : && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
1250 3 : srctype = TREE_TYPE (srcvar);
1251 : else
1252 281964 : return false;
1253 :
1254 : /* Now that we chose an access type express the other side in
1255 : terms of it if the target allows that with respect to alignment
1256 : constraints. */
1257 22580 : if (srcvar == NULL_TREE)
1258 : {
1259 19477 : if (src_align >= TYPE_ALIGN (desttype))
1260 19461 : srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1261 : else
1262 : {
1263 16 : enum machine_mode mode = TYPE_MODE (desttype);
1264 16 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1265 16 : || (targetm.slow_unaligned_access (mode, src_align)
1266 16 : && (optab_handler (movmisalign_optab, mode)
1267 : == CODE_FOR_nothing)))
1268 : return false;
1269 16 : srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1270 : src_align);
1271 16 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1272 : }
1273 : }
1274 3103 : else if (destvar == NULL_TREE)
1275 : {
1276 3103 : if (dest_align >= TYPE_ALIGN (srctype))
1277 3103 : destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1278 : else
1279 : {
1280 0 : enum machine_mode mode = TYPE_MODE (srctype);
1281 0 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1282 0 : || (targetm.slow_unaligned_access (mode, dest_align)
1283 0 : && (optab_handler (movmisalign_optab, mode)
1284 : == CODE_FOR_nothing)))
1285 : return false;
1286 0 : desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1287 : dest_align);
1288 0 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1289 : }
1290 : }
1291 :
1292 : /* Same as above, detect out-of-bounds accesses without issuing
1293 : warnings. Avoid folding out-of-bounds copies but to avoid
1294 : false positives for unreachable code defer warning until
1295 : after DCE has worked its magic.
1296 : -Wrestrict is still diagnosed. */
1297 22580 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
1298 : dest, src, len, len,
1299 22580 : false, false))
1300 1263 : if (warning != OPT_Wrestrict)
1301 : return false;
1302 :
1303 21325 : gimple *new_stmt;
1304 21325 : if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1305 : {
1306 545 : tree tem = fold_const_aggregate_ref (srcvar);
1307 545 : if (tem)
1308 526 : srcvar = tem;
1309 545 : if (! is_gimple_min_invariant (srcvar))
1310 : {
1311 19 : new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1312 19 : srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
1313 19 : gimple_assign_set_lhs (new_stmt, srcvar);
1314 38 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1315 19 : gimple_set_location (new_stmt, loc);
1316 19 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1317 : }
1318 545 : new_stmt = gimple_build_assign (destvar, srcvar);
1319 545 : goto set_vop_and_replace;
1320 : }
1321 :
1322 : /* We get an aggregate copy. If the source is a STRING_CST, then
1323 : directly use its type to perform the copy. */
1324 20780 : if (TREE_CODE (srcvar) == STRING_CST)
1325 : desttype = srctype;
1326 :
1327 : /* Or else, use an unsigned char[] type to perform the copy in order
1328 : to preserve padding and to avoid any issues with TREE_ADDRESSABLE
1329 : types or float modes behavior on copying. */
1330 : else
1331 : {
1332 41554 : desttype = build_array_type_nelts (unsigned_char_type_node,
1333 20777 : tree_to_uhwi (len));
1334 20777 : srctype = desttype;
1335 20777 : if (src_align > TYPE_ALIGN (srctype))
1336 12469 : srctype = build_aligned_type (srctype, src_align);
1337 20777 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1338 : }
1339 :
1340 20780 : if (dest_align > TYPE_ALIGN (desttype))
1341 13063 : desttype = build_aligned_type (desttype, dest_align);
1342 20780 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1343 20780 : new_stmt = gimple_build_assign (destvar, srcvar);
1344 :
1345 21325 : set_vop_and_replace:
1346 21325 : gimple_move_vops (new_stmt, stmt);
1347 21325 : if (!lhs)
1348 : {
1349 21141 : gsi_replace (gsi, new_stmt, false);
1350 21141 : return true;
1351 : }
1352 184 : gimple_set_location (new_stmt, loc);
1353 184 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1354 : }
1355 :
1356 3565 : done:
1357 3565 : gimple_seq stmts = NULL;
1358 3565 : if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
1359 3565 : len = NULL_TREE;
1360 198 : else if (code == BUILT_IN_MEMPCPY)
1361 : {
1362 198 : len = gimple_convert_to_ptrofftype (&stmts, loc, len);
1363 198 : dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
1364 198 : TREE_TYPE (dest), dest, len);
1365 : }
1366 : else
1367 0 : gcc_unreachable ();
1368 :
1369 3565 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1370 3565 : gimple *repl = gimple_build_assign (lhs, dest);
1371 3565 : gsi_replace (gsi, repl, false);
1372 3565 : return true;
1373 : }
1374 :
1375 : /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1376 : to built-in memcmp (a, b, len). */
1377 :
1378 : static bool
1379 148 : gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
1380 : {
1381 148 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
1382 :
1383 148 : if (!fn)
1384 : return false;
1385 :
1386 : /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1387 :
1388 148 : gimple *stmt = gsi_stmt (*gsi);
1389 296 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
1390 : return false;
1391 148 : tree a = gimple_call_arg (stmt, 0);
1392 148 : tree b = gimple_call_arg (stmt, 1);
1393 148 : tree len = gimple_call_arg (stmt, 2);
1394 :
1395 148 : gimple *repl = gimple_build_call (fn, 3, a, b, len);
1396 148 : replace_call_with_call_and_fold (gsi, repl);
1397 :
1398 148 : return true;
1399 : }
1400 :
1401 : /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1402 : to built-in memmove (dest, src, len). */
1403 :
1404 : static bool
1405 367 : gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
1406 : {
1407 367 : tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
1408 :
1409 367 : if (!fn)
1410 : return false;
1411 :
1412 : /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1413 : it's equivalent to memmove (not memcpy). Transform bcopy (src, dest,
1414 : len) into memmove (dest, src, len). */
1415 :
1416 367 : gimple *stmt = gsi_stmt (*gsi);
1417 734 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1418 : return false;
1419 367 : tree src = gimple_call_arg (stmt, 0);
1420 367 : tree dest = gimple_call_arg (stmt, 1);
1421 367 : tree len = gimple_call_arg (stmt, 2);
1422 :
1423 367 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1424 367 : gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
1425 367 : replace_call_with_call_and_fold (gsi, repl);
1426 :
1427 367 : return true;
1428 : }
1429 :
1430 : /* Transform a call to built-in bzero (dest, len) at *GSI into one
1431 : to built-in memset (dest, 0, len). */
1432 :
1433 : static bool
1434 250 : gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
1435 : {
1436 250 : tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
1437 :
1438 250 : if (!fn)
1439 : return false;
1440 :
1441 : /* Transform bzero (dest, len) into memset (dest, 0, len). */
1442 :
1443 250 : gimple *stmt = gsi_stmt (*gsi);
1444 500 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1445 : return false;
1446 250 : tree dest = gimple_call_arg (stmt, 0);
1447 250 : tree len = gimple_call_arg (stmt, 1);
1448 :
1449 250 : gimple_seq seq = NULL;
1450 250 : gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
1451 250 : gimple_seq_add_stmt_without_update (&seq, repl);
1452 250 : gsi_replace_with_seq_vops (gsi, seq);
1453 250 : fold_stmt (gsi);
1454 :
1455 250 : return true;
1456 : }
1457 :
1458 : /* Fold function call to builtin memset or bzero at *GSI setting the
1459 : memory of size LEN to VAL. Return whether a simplification was made. */
1460 :
1461 : static bool
1462 307619 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1463 : {
1464 307619 : gimple *stmt = gsi_stmt (*gsi);
1465 307619 : tree etype;
1466 307619 : unsigned HOST_WIDE_INT length, cval;
1467 :
1468 : /* If the LEN parameter is zero, return DEST. */
1469 307619 : if (integer_zerop (len))
1470 : {
1471 836 : replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1472 836 : return true;
1473 : }
1474 :
1475 918591 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1476 : return false;
1477 :
1478 306705 : if (! tree_fits_uhwi_p (len))
1479 : return false;
1480 :
1481 203248 : if (TREE_CODE (c) != INTEGER_CST)
1482 : return false;
1483 :
1484 197464 : tree dest = gimple_call_arg (stmt, 0);
1485 197464 : tree var = dest;
1486 197464 : if (TREE_CODE (var) != ADDR_EXPR)
1487 : return false;
1488 :
1489 158045 : var = TREE_OPERAND (var, 0);
1490 158045 : if (TREE_THIS_VOLATILE (var))
1491 : return false;
1492 :
1493 158002 : etype = TREE_TYPE (var);
1494 158002 : if (TREE_CODE (etype) == ARRAY_TYPE)
1495 82203 : etype = TREE_TYPE (etype);
1496 :
1497 158002 : if ((!INTEGRAL_TYPE_P (etype)
1498 96091 : && !POINTER_TYPE_P (etype))
1499 158530 : || BITINT_TYPE_P (etype))
1500 : return false;
1501 :
1502 61135 : if (! var_decl_component_p (var))
1503 : return false;
1504 :
1505 61135 : length = tree_to_uhwi (len);
1506 61135 : if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
1507 1758 : || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
1508 3516 : != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
1509 62893 : || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1510 59377 : return false;
1511 :
1512 1758 : if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1513 : return false;
1514 :
1515 1758 : if (!type_has_mode_precision_p (etype))
1516 7 : etype = lang_hooks.types.type_for_mode (SCALAR_INT_TYPE_MODE (etype),
1517 7 : TYPE_UNSIGNED (etype));
1518 :
1519 1758 : if (integer_zerop (c))
1520 : cval = 0;
1521 : else
1522 : {
1523 337 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1524 : return NULL_TREE;
1525 :
1526 337 : cval = TREE_INT_CST_LOW (c);
1527 337 : cval &= 0xff;
1528 337 : cval |= cval << 8;
1529 337 : cval |= cval << 16;
1530 337 : cval |= (cval << 31) << 1;
1531 : }
1532 :
1533 1758 : var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1534 1758 : gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1535 1758 : gimple_move_vops (store, stmt);
1536 1758 : gimple_set_location (store, gimple_location (stmt));
1537 1758 : gsi_insert_before (gsi, store, GSI_SAME_STMT);
1538 1758 : if (gimple_call_lhs (stmt))
1539 : {
1540 2 : gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1541 2 : gsi_replace (gsi, asgn, false);
1542 : }
1543 : else
1544 : {
1545 1756 : gimple_stmt_iterator gsi2 = *gsi;
1546 1756 : gsi_prev (gsi);
1547 1756 : gsi_remove (&gsi2, true);
1548 : }
1549 :
1550 : return true;
1551 : }
1552 :
1553 : /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1554 :
1555 : static bool
1556 429489 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
1557 : c_strlen_data *pdata, unsigned eltsize)
1558 : {
1559 429489 : gcc_assert (TREE_CODE (arg) != SSA_NAME);
1560 :
1561 : /* The length computed by this invocation of the function. */
1562 429489 : tree val = NULL_TREE;
1563 :
1564 : /* True if VAL is an optimistic (tight) bound determined from
1565 : the size of the character array in which the string may be
1566 : stored. In that case, the computed VAL is used to set
1567 : PDATA->MAXBOUND. */
1568 429489 : bool tight_bound = false;
1569 :
1570 : /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1571 429489 : if (TREE_CODE (arg) == ADDR_EXPR
1572 429489 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
1573 : {
1574 28846 : tree op = TREE_OPERAND (arg, 0);
1575 28846 : if (integer_zerop (TREE_OPERAND (op, 1)))
1576 : {
1577 12576 : tree aop0 = TREE_OPERAND (op, 0);
1578 12576 : if (TREE_CODE (aop0) == INDIRECT_REF
1579 12576 : && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1580 0 : return get_range_strlen (TREE_OPERAND (aop0, 0), visited, rkind,
1581 0 : pdata, eltsize);
1582 : }
1583 16270 : else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
1584 16270 : && rkind == SRK_LENRANGE)
1585 : {
1586 : /* Fail if an array is the last member of a struct object
1587 : since it could be treated as a (fake) flexible array
1588 : member. */
1589 4800 : tree idx = TREE_OPERAND (op, 1);
1590 :
1591 4800 : arg = TREE_OPERAND (op, 0);
1592 4800 : tree optype = TREE_TYPE (arg);
1593 4800 : if (tree dom = TYPE_DOMAIN (optype))
1594 4800 : if (tree bound = TYPE_MAX_VALUE (dom))
1595 4800 : if (TREE_CODE (bound) == INTEGER_CST
1596 4800 : && TREE_CODE (idx) == INTEGER_CST
1597 8030 : && tree_int_cst_lt (bound, idx))
1598 : return false;
1599 : }
1600 : }
1601 :
1602 429281 : if (rkind == SRK_INT_VALUE)
1603 : {
1604 : /* We are computing the maximum value (not string length). */
1605 3083 : val = arg;
1606 3083 : if (TREE_CODE (val) != INTEGER_CST
1607 3083 : || tree_int_cst_sgn (val) < 0)
1608 2573 : return false;
1609 : }
1610 : else
1611 : {
1612 426198 : c_strlen_data lendata = { };
1613 426198 : val = c_strlen (arg, 1, &lendata, eltsize);
1614 :
1615 426198 : if (!val && lendata.decl)
1616 : {
1617 : /* ARG refers to an unterminated const character array.
1618 : DATA.DECL with size DATA.LEN. */
1619 4193 : val = lendata.minlen;
1620 4193 : pdata->decl = lendata.decl;
1621 : }
1622 : }
1623 :
1624 : /* Set if VAL represents the maximum length based on array size (set
1625 : when exact length cannot be determined). */
1626 426708 : bool maxbound = false;
1627 :
1628 426708 : if (!val && rkind == SRK_LENRANGE)
1629 : {
1630 225795 : if (TREE_CODE (arg) == ADDR_EXPR)
1631 80224 : return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
1632 80224 : pdata, eltsize);
1633 :
1634 145571 : if (TREE_CODE (arg) == ARRAY_REF)
1635 : {
1636 18094 : tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
1637 :
1638 : /* Determine the "innermost" array type. */
1639 18094 : while (TREE_CODE (optype) == ARRAY_TYPE
1640 25040 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1641 6946 : optype = TREE_TYPE (optype);
1642 :
1643 : /* Avoid arrays of pointers. */
1644 18094 : tree eltype = TREE_TYPE (optype);
1645 18094 : if (TREE_CODE (optype) != ARRAY_TYPE
1646 18094 : || !INTEGRAL_TYPE_P (eltype))
1647 : return false;
1648 :
1649 : /* Fail when the array bound is unknown or zero. */
1650 13620 : val = TYPE_SIZE_UNIT (optype);
1651 13620 : if (!val
1652 13548 : || TREE_CODE (val) != INTEGER_CST
1653 27140 : || integer_zerop (val))
1654 105 : return false;
1655 :
1656 13515 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1657 : integer_one_node);
1658 :
1659 : /* Set the minimum size to zero since the string in
1660 : the array could have zero length. */
1661 13515 : pdata->minlen = ssize_int (0);
1662 :
1663 13515 : tight_bound = true;
1664 : }
1665 127477 : else if (TREE_CODE (arg) == COMPONENT_REF
1666 127477 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
1667 : == ARRAY_TYPE))
1668 : {
1669 : /* Use the type of the member array to determine the upper
1670 : bound on the length of the array. This may be overly
1671 : optimistic if the array itself isn't NUL-terminated and
1672 : the caller relies on the subsequent member to contain
1673 : the NUL but that would only be considered valid if
1674 : the array were the last member of a struct. */
1675 :
1676 9648 : tree fld = TREE_OPERAND (arg, 1);
1677 :
1678 9648 : tree optype = TREE_TYPE (fld);
1679 :
1680 : /* Determine the "innermost" array type. */
1681 9648 : while (TREE_CODE (optype) == ARRAY_TYPE
1682 10210 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1683 562 : optype = TREE_TYPE (optype);
1684 :
1685 : /* Fail when the array bound is unknown or zero. */
1686 9648 : val = TYPE_SIZE_UNIT (optype);
1687 9648 : if (!val
1688 9412 : || TREE_CODE (val) != INTEGER_CST
1689 19025 : || integer_zerop (val))
1690 350 : return false;
1691 9298 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1692 : integer_one_node);
1693 :
1694 : /* Set the minimum size to zero since the string in
1695 : the array could have zero length. */
1696 9298 : pdata->minlen = ssize_int (0);
1697 :
1698 : /* The array size determined above is an optimistic bound
1699 : on the length. If the array isn't nul-terminated the
1700 : length computed by the library function would be greater.
1701 : Even though using strlen to cross the subobject boundary
1702 : is undefined, avoid drawing conclusions from the member
1703 : type about the length here. */
1704 9298 : tight_bound = true;
1705 : }
1706 117829 : else if (TREE_CODE (arg) == MEM_REF
1707 27154 : && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
1708 3911 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
1709 121302 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
1710 : {
1711 : /* Handle a MEM_REF into a DECL accessing an array of integers,
1712 : being conservative about references to extern structures with
1713 : flexible array members that can be initialized to arbitrary
1714 : numbers of elements as an extension (static structs are okay). */
1715 3473 : tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1716 3473 : if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
1717 6933 : && (decl_binds_to_current_def_p (ref)
1718 438 : || !array_ref_flexible_size_p (arg)))
1719 : {
1720 : /* Fail if the offset is out of bounds. Such accesses
1721 : should be diagnosed at some point. */
1722 3347 : val = DECL_SIZE_UNIT (ref);
1723 3347 : if (!val
1724 3175 : || TREE_CODE (val) != INTEGER_CST
1725 6522 : || integer_zerop (val))
1726 371 : return false;
1727 :
1728 3173 : poly_offset_int psiz = wi::to_offset (val);
1729 3173 : poly_offset_int poff = mem_ref_offset (arg);
1730 3173 : if (known_le (psiz, poff))
1731 : return false;
1732 :
1733 2976 : pdata->minlen = ssize_int (0);
1734 :
1735 : /* Subtract the offset and one for the terminating nul. */
1736 2976 : psiz -= poff;
1737 2976 : psiz -= 1;
1738 2976 : val = wide_int_to_tree (TREE_TYPE (val), psiz);
1739 : /* Since VAL reflects the size of a declared object
1740 : rather the type of the access it is not a tight bound. */
1741 : }
1742 : }
1743 114356 : else if (TREE_CODE (arg) == PARM_DECL || VAR_P (arg))
1744 : {
1745 : /* Avoid handling pointers to arrays. GCC might misuse
1746 : a pointer to an array of one bound to point to an array
1747 : object of a greater bound. */
1748 70455 : tree argtype = TREE_TYPE (arg);
1749 70455 : if (TREE_CODE (argtype) == ARRAY_TYPE)
1750 : {
1751 42382 : val = TYPE_SIZE_UNIT (argtype);
1752 42382 : if (!val
1753 41616 : || TREE_CODE (val) != INTEGER_CST
1754 83998 : || integer_zerop (val))
1755 881 : return false;
1756 41501 : val = wide_int_to_tree (TREE_TYPE (val),
1757 41501 : wi::sub (wi::to_wide (val), 1));
1758 :
1759 : /* Set the minimum size to zero since the string in
1760 : the array could have zero length. */
1761 41501 : pdata->minlen = ssize_int (0);
1762 : }
1763 : }
1764 : maxbound = true;
1765 : }
1766 :
1767 340303 : if (!val)
1768 : return false;
1769 :
1770 : /* Adjust the lower bound on the string length as necessary. */
1771 243728 : if (!pdata->minlen
1772 243728 : || (rkind != SRK_STRLEN
1773 71766 : && TREE_CODE (pdata->minlen) == INTEGER_CST
1774 71766 : && TREE_CODE (val) == INTEGER_CST
1775 71761 : && tree_int_cst_lt (val, pdata->minlen)))
1776 172054 : pdata->minlen = val;
1777 :
1778 243728 : if (pdata->maxbound && TREE_CODE (pdata->maxbound) == INTEGER_CST)
1779 : {
1780 : /* Adjust the tighter (more optimistic) string length bound
1781 : if necessary and proceed to adjust the more conservative
1782 : bound. */
1783 1492 : if (TREE_CODE (val) == INTEGER_CST)
1784 : {
1785 1492 : if (tree_int_cst_lt (pdata->maxbound, val))
1786 657 : pdata->maxbound = val;
1787 : }
1788 : else
1789 0 : pdata->maxbound = val;
1790 : }
1791 242236 : else if (pdata->maxbound || maxbound)
1792 : /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
1793 : if VAL corresponds to the maximum length determined based
1794 : on the type of the object. */
1795 70550 : pdata->maxbound = val;
1796 :
1797 243728 : if (tight_bound)
1798 : {
1799 : /* VAL computed above represents an optimistically tight bound
1800 : on the length of the string based on the referenced object's
1801 : or subobject's type. Determine the conservative upper bound
1802 : based on the enclosing object's size if possible. */
1803 22813 : if (rkind == SRK_LENRANGE)
1804 : {
1805 22813 : poly_int64 offset;
1806 22813 : tree base = get_addr_base_and_unit_offset (arg, &offset);
1807 22813 : if (!base)
1808 : {
1809 : /* When the call above fails due to a non-constant offset
1810 : assume the offset is zero and use the size of the whole
1811 : enclosing object instead. */
1812 7837 : base = get_base_address (arg);
1813 7837 : offset = 0;
1814 : }
1815 : /* If the base object is a pointer no upper bound on the length
1816 : can be determined. Otherwise the maximum length is equal to
1817 : the size of the enclosing object minus the offset of
1818 : the referenced subobject minus 1 (for the terminating nul). */
1819 22813 : tree type = TREE_TYPE (base);
1820 22813 : if (POINTER_TYPE_P (type)
1821 22809 : || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
1822 41295 : || !(val = DECL_SIZE_UNIT (base)))
1823 5578 : val = build_all_ones_cst (size_type_node);
1824 : else
1825 : {
1826 17235 : val = DECL_SIZE_UNIT (base);
1827 17235 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1828 : size_int (offset + 1));
1829 : }
1830 : }
1831 : else
1832 : return false;
1833 : }
1834 :
1835 243728 : if (pdata->maxlen)
1836 : {
1837 : /* Adjust the more conservative bound if possible/necessary
1838 : and fail otherwise. */
1839 7923 : if (rkind != SRK_STRLEN)
1840 : {
1841 6992 : if (TREE_CODE (pdata->maxlen) != INTEGER_CST
1842 6992 : || TREE_CODE (val) != INTEGER_CST)
1843 : return false;
1844 :
1845 6987 : if (tree_int_cst_lt (pdata->maxlen, val))
1846 1376 : pdata->maxlen = val;
1847 6987 : return true;
1848 : }
1849 931 : else if (simple_cst_equal (val, pdata->maxlen) != 1)
1850 : {
1851 : /* Fail if the length of this ARG is different from that
1852 : previously determined from another ARG. */
1853 : return false;
1854 : }
1855 : }
1856 :
1857 235929 : pdata->maxlen = val;
1858 235929 : return rkind == SRK_LENRANGE || !integer_all_onesp (val);
1859 : }
1860 :
1861 : /* For an ARG referencing one or more strings, try to obtain the range
1862 : of their lengths, or the size of the largest array ARG refers to if
1863 : the range of lengths cannot be determined, and store all in *PDATA.
1864 : For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1865 : the maximum constant value.
1866 : If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1867 : SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1868 : length or if we are unable to determine the length, return false.
1869 : VISITED is a bitmap of visited variables.
1870 : RKIND determines the kind of value or range to obtain (see
1871 : strlen_range_kind).
1872 : Set PDATA->DECL if ARG refers to an unterminated constant array.
1873 : On input, set ELTSIZE to 1 for normal single byte character strings,
1874 : and either 2 or 4 for wide character strings (the size of wchar_t).
1875 : Return true if *PDATA was successfully populated and false otherwise. */
1876 :
1877 : static bool
1878 1330494 : get_range_strlen (tree arg, bitmap visited,
1879 : strlen_range_kind rkind,
1880 : c_strlen_data *pdata, unsigned eltsize)
1881 : {
1882 :
1883 1407065 : if (TREE_CODE (arg) != SSA_NAME)
1884 429489 : return get_range_strlen_tree (arg, visited, rkind, pdata, eltsize);
1885 :
1886 : /* If ARG is registered for SSA update we cannot look at its defining
1887 : statement. */
1888 977576 : if (name_registered_for_update_p (arg))
1889 : return false;
1890 :
1891 : /* If we were already here, break the infinite cycle. */
1892 977576 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
1893 : return true;
1894 :
1895 973003 : tree var = arg;
1896 973003 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
1897 :
1898 973003 : switch (gimple_code (def_stmt))
1899 : {
1900 117283 : case GIMPLE_ASSIGN:
1901 : /* The RHS of the statement defining VAR must either have a
1902 : constant length or come from another SSA_NAME with a constant
1903 : length. */
1904 117283 : if (gimple_assign_single_p (def_stmt)
1905 117283 : || gimple_assign_unary_nop_p (def_stmt))
1906 : {
1907 76571 : tree rhs = gimple_assign_rhs1 (def_stmt);
1908 76571 : return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
1909 : }
1910 40712 : else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1911 : {
1912 246 : tree ops[2] = { gimple_assign_rhs2 (def_stmt),
1913 246 : gimple_assign_rhs3 (def_stmt) };
1914 :
1915 738 : for (unsigned int i = 0; i < 2; i++)
1916 492 : if (!get_range_strlen (ops[i], visited, rkind, pdata, eltsize))
1917 : {
1918 28 : if (rkind != SRK_LENRANGE)
1919 : return false;
1920 : /* Set the upper bound to the maximum to prevent
1921 : it from being adjusted in the next iteration but
1922 : leave MINLEN and the more conservative MAXBOUND
1923 : determined so far alone (or leave them null if
1924 : they haven't been set yet). That the MINLEN is
1925 : in fact zero can be determined from MAXLEN being
1926 : unbounded but the discovered minimum is used for
1927 : diagnostics. */
1928 28 : pdata->maxlen = build_all_ones_cst (size_type_node);
1929 : }
1930 : return true;
1931 : }
1932 : return false;
1933 :
1934 : case GIMPLE_PHI:
1935 : /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1936 : must have a constant length. */
1937 62595 : for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
1938 : {
1939 44517 : tree arg = gimple_phi_arg (def_stmt, i)->def;
1940 :
1941 : /* If this PHI has itself as an argument, we cannot
1942 : determine the string length of this argument. However,
1943 : if we can find a constant string length for the other
1944 : PHI args then we can still be sure that this is a
1945 : constant string length. So be optimistic and just
1946 : continue with the next argument. */
1947 44517 : if (arg == gimple_phi_result (def_stmt))
1948 0 : continue;
1949 :
1950 44517 : if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
1951 : {
1952 22502 : if (rkind != SRK_LENRANGE)
1953 : return false;
1954 : /* Set the upper bound to the maximum to prevent
1955 : it from being adjusted in the next iteration but
1956 : leave MINLEN and the more conservative MAXBOUND
1957 : determined so far alone (or leave them null if
1958 : they haven't been set yet). That the MINLEN is
1959 : in fact zero can be determined from MAXLEN being
1960 : unbounded but the discovered minimum is used for
1961 : diagnostics. */
1962 20768 : pdata->maxlen = build_all_ones_cst (size_type_node);
1963 : }
1964 : }
1965 : return true;
1966 :
1967 : default:
1968 : return false;
1969 : }
1970 : }
1971 :
1972 : /* Try to obtain the range of the lengths of the string(s) referenced
1973 : by ARG, or the size of the largest array ARG refers to if the range
1974 : of lengths cannot be determined, and store all in *PDATA which must
1975 : be zero-initialized on input except PDATA->MAXBOUND may be set to
1976 : a non-null tree node other than INTEGER_CST to request to have it
1977 : set to the length of the longest string in a PHI. ELTSIZE is
1978 : the expected size of the string element in bytes: 1 for char and
1979 : some power of 2 for wide characters.
1980 : Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
1981 : for optimization. Returning false means that a nonzero PDATA->MINLEN
1982 : doesn't reflect the true lower bound of the range when PDATA->MAXLEN
1983 : is -1 (in that case, the actual range is indeterminate, i.e.,
1984 : [0, PTRDIFF_MAX - 2]. */
1985 :
1986 : bool
1987 1132165 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
1988 : {
1989 1132165 : auto_bitmap visited;
1990 1132165 : tree maxbound = pdata->maxbound;
1991 :
1992 1132165 : if (!get_range_strlen (arg, visited, SRK_LENRANGE, pdata, eltsize))
1993 : {
1994 : /* On failure extend the length range to an impossible maximum
1995 : (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1996 : members can stay unchanged regardless. */
1997 912043 : pdata->minlen = ssize_int (0);
1998 912043 : pdata->maxlen = build_all_ones_cst (size_type_node);
1999 : }
2000 220122 : else if (!pdata->minlen)
2001 6430 : pdata->minlen = ssize_int (0);
2002 :
2003 : /* If it's unchanged from it initial non-null value, set the conservative
2004 : MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
2005 1132165 : if (maxbound && pdata->maxbound == maxbound)
2006 650885 : pdata->maxbound = build_all_ones_cst (size_type_node);
2007 :
2008 1132165 : return !integer_all_onesp (pdata->maxlen);
2009 1132165 : }
2010 :
2011 : /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
2012 : For ARG of pointer types, NONSTR indicates if the caller is prepared
2013 : to handle unterminated strings. For integer ARG and when RKIND ==
2014 : SRK_INT_VALUE, NONSTR must be null.
2015 :
2016 : If an unterminated array is discovered and our caller handles
2017 : unterminated arrays, then bubble up the offending DECL and
2018 : return the maximum size. Otherwise return NULL. */
2019 :
2020 : static tree
2021 95924 : get_maxval_strlen (tree arg, strlen_range_kind rkind, tree *nonstr = NULL)
2022 : {
2023 : /* A non-null NONSTR is meaningless when determining the maximum
2024 : value of an integer ARG. */
2025 95924 : gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
2026 :
2027 : // If arg is already a constant, simply return it.
2028 95924 : if (TREE_CODE (arg) == INTEGER_CST && rkind == SRK_INT_VALUE)
2029 : return arg;
2030 :
2031 : /* ARG must have an integral type when RKIND says so. */
2032 72997 : gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
2033 :
2034 73096 : auto_bitmap visited;
2035 :
2036 : /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
2037 : is unbounded. */
2038 73096 : c_strlen_data lendata = { };
2039 73096 : if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
2040 49884 : lendata.maxlen = NULL_TREE;
2041 23212 : else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
2042 0 : lendata.maxlen = NULL_TREE;
2043 :
2044 73096 : if (nonstr)
2045 : {
2046 : /* For callers prepared to handle unterminated arrays set
2047 : *NONSTR to point to the declaration of the array and return
2048 : the maximum length/size. */
2049 24028 : *nonstr = lendata.decl;
2050 24028 : return lendata.maxlen;
2051 : }
2052 :
2053 : /* Fail if the constant array isn't nul-terminated. */
2054 49068 : return lendata.decl ? NULL_TREE : lendata.maxlen;
2055 73096 : }
2056 :
2057 : /* Return true if LEN is known to be less than or equal to (or if STRICT is
2058 : true, strictly less than) the lower bound of SIZE at compile time and false
2059 : otherwise. */
2060 :
2061 : static bool
2062 63164 : known_lower (gimple *stmt, tree len, tree size, bool strict = false)
2063 : {
2064 63164 : if (len == NULL_TREE)
2065 : return false;
2066 :
2067 236250 : wide_int size_range[2];
2068 236250 : wide_int len_range[2];
2069 47250 : if (get_range (len, stmt, len_range) && get_range (size, stmt, size_range))
2070 : {
2071 16547 : if (strict)
2072 1859 : return wi::ltu_p (len_range[1], size_range[0]);
2073 : else
2074 14688 : return wi::leu_p (len_range[1], size_range[0]);
2075 : }
2076 :
2077 : return false;
2078 283500 : }
2079 :
2080 : /* Fold function call to builtin strcpy with arguments DEST and SRC.
2081 : If LEN is not NULL, it represents the length of the string to be
2082 : copied. Return NULL_TREE if no simplification can be made. */
2083 :
2084 : static bool
2085 26090 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
2086 : tree dest, tree src)
2087 : {
2088 26090 : gimple *stmt = gsi_stmt (*gsi);
2089 26090 : location_t loc = gimple_location (stmt);
2090 26090 : tree fn;
2091 :
2092 : /* If SRC and DEST are the same (and not volatile), return DEST. */
2093 26090 : if (operand_equal_p (src, dest, 0))
2094 : {
2095 : /* Issue -Wrestrict unless the pointers are null (those do
2096 : not point to objects and so do not indicate an overlap;
2097 : such calls could be the result of sanitization and jump
2098 : threading). */
2099 86 : if (!integer_zerop (dest) && !warning_suppressed_p (stmt, OPT_Wrestrict))
2100 : {
2101 51 : tree func = gimple_call_fndecl (stmt);
2102 :
2103 51 : warning_at (loc, OPT_Wrestrict,
2104 : "%qD source argument is the same as destination",
2105 : func);
2106 : }
2107 :
2108 86 : replace_call_with_value (gsi, dest);
2109 86 : return true;
2110 : }
2111 :
2112 26004 : if (optimize_function_for_size_p (cfun))
2113 : return false;
2114 :
2115 24028 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2116 24028 : if (!fn)
2117 : return false;
2118 :
2119 : /* Set to non-null if ARG refers to an unterminated array. */
2120 24028 : tree nonstr = NULL;
2121 24028 : tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
2122 :
2123 24028 : if (nonstr)
2124 : {
2125 : /* Avoid folding calls with unterminated arrays. */
2126 531 : if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
2127 69 : warn_string_no_nul (loc, stmt, "strcpy", src, nonstr);
2128 531 : suppress_warning (stmt, OPT_Wstringop_overread);
2129 531 : return false;
2130 : }
2131 :
2132 28893 : if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2133 : return false;
2134 :
2135 2889 : len = fold_convert_loc (loc, size_type_node, len);
2136 2889 : len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
2137 2889 : len = force_gimple_operand_gsi (gsi, len, true,
2138 : NULL_TREE, true, GSI_SAME_STMT);
2139 2889 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2140 2889 : replace_call_with_call_and_fold (gsi, repl);
2141 2889 : return true;
2142 : }
2143 :
2144 : /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
2145 : If SLEN is not NULL, it represents the length of the source string.
2146 : Return NULL_TREE if no simplification can be made. */
2147 :
2148 : static bool
2149 17274 : gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
2150 : tree dest, tree src, tree len)
2151 : {
2152 17274 : gimple *stmt = gsi_stmt (*gsi);
2153 17274 : location_t loc = gimple_location (stmt);
2154 17274 : bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
2155 :
2156 : /* If the LEN parameter is zero, return DEST. */
2157 17274 : if (integer_zerop (len))
2158 : {
2159 : /* Avoid warning if the destination refers to an array/pointer
2160 : decorate with attribute nonstring. */
2161 167 : if (!nonstring)
2162 : {
2163 155 : tree fndecl = gimple_call_fndecl (stmt);
2164 :
2165 : /* Warn about the lack of nul termination: the result is not
2166 : a (nul-terminated) string. */
2167 155 : tree slen = get_maxval_strlen (src, SRK_STRLEN);
2168 155 : if (slen && !integer_zerop (slen))
2169 24 : warning_at (loc, OPT_Wstringop_truncation,
2170 : "%qD destination unchanged after copying no bytes "
2171 : "from a string of length %E",
2172 : fndecl, slen);
2173 : else
2174 131 : warning_at (loc, OPT_Wstringop_truncation,
2175 : "%qD destination unchanged after copying no bytes",
2176 : fndecl);
2177 : }
2178 :
2179 167 : replace_call_with_value (gsi, dest);
2180 167 : return true;
2181 : }
2182 :
2183 : /* We can't compare slen with len as constants below if len is not a
2184 : constant. */
2185 17107 : if (TREE_CODE (len) != INTEGER_CST)
2186 : return false;
2187 :
2188 : /* Now, we must be passed a constant src ptr parameter. */
2189 10748 : tree slen = get_maxval_strlen (src, SRK_STRLEN);
2190 10748 : if (!slen || TREE_CODE (slen) != INTEGER_CST)
2191 : return false;
2192 :
2193 : /* The size of the source string including the terminating nul. */
2194 1780 : tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
2195 :
2196 : /* We do not support simplification of this case, though we do
2197 : support it when expanding trees into RTL. */
2198 : /* FIXME: generate a call to __builtin_memset. */
2199 1780 : if (tree_int_cst_lt (ssize, len))
2200 : return false;
2201 :
2202 : /* Diagnose truncation that leaves the copy unterminated. */
2203 695 : maybe_diag_stxncpy_trunc (*gsi, src, len);
2204 :
2205 : /* OK transform into builtin memcpy. */
2206 695 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2207 17802 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2208 : return false;
2209 :
2210 695 : len = fold_convert_loc (loc, size_type_node, len);
2211 695 : len = force_gimple_operand_gsi (gsi, len, true,
2212 : NULL_TREE, true, GSI_SAME_STMT);
2213 695 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2214 695 : replace_call_with_call_and_fold (gsi, repl);
2215 :
2216 695 : return true;
2217 : }
2218 :
2219 : /* Fold function call to builtin strchr or strrchr.
2220 : If both arguments are constant, evaluate and fold the result,
2221 : otherwise simplify str(r)chr (str, 0) into str + strlen (str).
2222 : In general strlen is significantly faster than strchr
2223 : due to being a simpler operation. */
2224 : static bool
2225 5473 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
2226 : {
2227 5473 : gimple *stmt = gsi_stmt (*gsi);
2228 5473 : tree str = gimple_call_arg (stmt, 0);
2229 5473 : tree c = gimple_call_arg (stmt, 1);
2230 5473 : location_t loc = gimple_location (stmt);
2231 5473 : const char *p;
2232 5473 : char ch;
2233 :
2234 5473 : if (!gimple_call_lhs (stmt))
2235 : return false;
2236 :
2237 : /* Avoid folding if the first argument is not a nul-terminated array.
2238 : Defer warning until later. */
2239 5463 : if (!check_nul_terminated_array (NULL_TREE, str))
2240 : return false;
2241 :
2242 5379 : if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
2243 : {
2244 41 : const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
2245 :
2246 41 : if (p1 == NULL)
2247 : {
2248 1 : replace_call_with_value (gsi, integer_zero_node);
2249 1 : return true;
2250 : }
2251 :
2252 40 : tree len = build_int_cst (size_type_node, p1 - p);
2253 40 : gimple_seq stmts = NULL;
2254 40 : gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
2255 : POINTER_PLUS_EXPR, str, len);
2256 40 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2257 40 : gsi_replace_with_seq_vops (gsi, stmts);
2258 40 : return true;
2259 : }
2260 :
2261 5420 : if (!integer_zerop (c) || (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun)))
2262 : return false;
2263 :
2264 : /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
2265 82 : if (is_strrchr && optimize_function_for_size_p (cfun))
2266 : {
2267 3 : tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
2268 :
2269 3 : if (strchr_fn)
2270 : {
2271 3 : gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
2272 3 : replace_call_with_call_and_fold (gsi, repl);
2273 3 : return true;
2274 : }
2275 :
2276 : return false;
2277 : }
2278 :
2279 79 : tree len;
2280 5429 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2281 :
2282 79 : if (!strlen_fn)
2283 : return false;
2284 :
2285 : /* Create newstr = strlen (str). */
2286 79 : gimple_seq stmts = NULL;
2287 79 : gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
2288 79 : gimple_set_location (new_stmt, loc);
2289 79 : len = make_ssa_name (size_type_node);
2290 79 : gimple_call_set_lhs (new_stmt, len);
2291 79 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2292 :
2293 : /* Create (str p+ strlen (str)). */
2294 79 : new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
2295 : POINTER_PLUS_EXPR, str, len);
2296 79 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2297 79 : gsi_replace_with_seq_vops (gsi, stmts);
2298 : /* gsi now points at the assignment to the lhs, get a
2299 : stmt iterator to the strlen.
2300 : ??? We can't use gsi_for_stmt as that doesn't work when the
2301 : CFG isn't built yet. */
2302 79 : gimple_stmt_iterator gsi2 = *gsi;
2303 79 : gsi_prev (&gsi2);
2304 79 : fold_stmt (&gsi2);
2305 79 : return true;
2306 : }
2307 :
2308 : /* Fold function call to builtin strstr.
2309 : If both arguments are constant, evaluate and fold the result,
2310 : additionally fold strstr (x, "") into x and strstr (x, "c")
2311 : into strchr (x, 'c'). */
2312 : static bool
2313 4126 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
2314 : {
2315 4126 : gimple *stmt = gsi_stmt (*gsi);
2316 4126 : if (!gimple_call_lhs (stmt))
2317 : return false;
2318 :
2319 4123 : tree haystack = gimple_call_arg (stmt, 0);
2320 4123 : tree needle = gimple_call_arg (stmt, 1);
2321 :
2322 : /* Avoid folding if either argument is not a nul-terminated array.
2323 : Defer warning until later. */
2324 4123 : if (!check_nul_terminated_array (NULL_TREE, haystack)
2325 4123 : || !check_nul_terminated_array (NULL_TREE, needle))
2326 19 : return false;
2327 :
2328 4104 : const char *q = c_getstr (needle);
2329 4104 : if (q == NULL)
2330 : return false;
2331 :
2332 2987 : if (const char *p = c_getstr (haystack))
2333 : {
2334 14 : const char *r = strstr (p, q);
2335 :
2336 14 : if (r == NULL)
2337 : {
2338 1 : replace_call_with_value (gsi, integer_zero_node);
2339 1 : return true;
2340 : }
2341 :
2342 13 : tree len = build_int_cst (size_type_node, r - p);
2343 13 : gimple_seq stmts = NULL;
2344 13 : gimple *new_stmt
2345 13 : = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
2346 : haystack, len);
2347 13 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2348 13 : gsi_replace_with_seq_vops (gsi, stmts);
2349 13 : return true;
2350 : }
2351 :
2352 : /* For strstr (x, "") return x. */
2353 2973 : if (q[0] == '\0')
2354 : {
2355 6 : replace_call_with_value (gsi, haystack);
2356 6 : return true;
2357 : }
2358 :
2359 10018 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2360 : return false;
2361 :
2362 : /* Transform strstr (x, "c") into strchr (x, 'c'). */
2363 2967 : if (q[1] == '\0')
2364 : {
2365 22 : tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
2366 22 : if (strchr_fn)
2367 : {
2368 22 : tree c = build_int_cst (integer_type_node, q[0]);
2369 22 : gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
2370 22 : replace_call_with_call_and_fold (gsi, repl);
2371 22 : return true;
2372 : }
2373 : }
2374 :
2375 : return false;
2376 : }
2377 :
2378 : /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2379 : to the call.
2380 :
2381 : Return NULL_TREE if no simplification was possible, otherwise return the
2382 : simplified form of the call as a tree.
2383 :
2384 : The simplified form may be a constant or other expression which
2385 : computes the same value, but in a more efficient manner (including
2386 : calls to other builtin functions).
2387 :
2388 : The call may contain arguments which need to be evaluated, but
2389 : which are not useful to determine the result of the call. In
2390 : this case we return a chain of COMPOUND_EXPRs. The LHS of each
2391 : COMPOUND_EXPR will be an argument which must be evaluated.
2392 : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2393 : COMPOUND_EXPR in the chain will contain the tree for the simplified
2394 : form of the builtin function call. */
2395 :
2396 : static bool
2397 7458 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
2398 : {
2399 7458 : gimple *stmt = gsi_stmt (*gsi);
2400 7458 : location_t loc = gimple_location (stmt);
2401 :
2402 7458 : const char *p = c_getstr (src);
2403 :
2404 : /* If the string length is zero, return the dst parameter. */
2405 7458 : if (p && *p == '\0')
2406 : {
2407 72 : replace_call_with_value (gsi, dst);
2408 72 : return true;
2409 : }
2410 :
2411 7386 : if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
2412 : return false;
2413 :
2414 20166 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2415 : return false;
2416 :
2417 : /* See if we can store by pieces into (dst + strlen(dst)). */
2418 6799 : tree newdst;
2419 6799 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2420 6799 : tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2421 :
2422 6799 : if (!strlen_fn || !memcpy_fn)
2423 : return false;
2424 :
2425 : /* If the length of the source string isn't computable don't
2426 : split strcat into strlen and memcpy. */
2427 6799 : tree len = get_maxval_strlen (src, SRK_STRLEN);
2428 6799 : if (! len)
2429 : return false;
2430 :
2431 : /* Create strlen (dst). */
2432 818 : gimple_seq stmts = NULL, stmts2;
2433 818 : gimple *repl = gimple_build_call (strlen_fn, 1, dst);
2434 818 : gimple_set_location (repl, loc);
2435 818 : newdst = make_ssa_name (size_type_node);
2436 818 : gimple_call_set_lhs (repl, newdst);
2437 818 : gimple_seq_add_stmt_without_update (&stmts, repl);
2438 :
2439 : /* Create (dst p+ strlen (dst)). */
2440 818 : newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
2441 818 : newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
2442 818 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2443 :
2444 818 : len = fold_convert_loc (loc, size_type_node, len);
2445 818 : len = size_binop_loc (loc, PLUS_EXPR, len,
2446 : build_int_cst (size_type_node, 1));
2447 818 : len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
2448 818 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2449 :
2450 818 : repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
2451 818 : gimple_seq_add_stmt_without_update (&stmts, repl);
2452 818 : if (gimple_call_lhs (stmt))
2453 : {
2454 165 : repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
2455 165 : gimple_seq_add_stmt_without_update (&stmts, repl);
2456 165 : gsi_replace_with_seq_vops (gsi, stmts);
2457 : /* gsi now points at the assignment to the lhs, get a
2458 : stmt iterator to the memcpy call.
2459 : ??? We can't use gsi_for_stmt as that doesn't work when the
2460 : CFG isn't built yet. */
2461 165 : gimple_stmt_iterator gsi2 = *gsi;
2462 165 : gsi_prev (&gsi2);
2463 165 : fold_stmt (&gsi2);
2464 : }
2465 : else
2466 : {
2467 653 : gsi_replace_with_seq_vops (gsi, stmts);
2468 653 : fold_stmt (gsi);
2469 : }
2470 : return true;
2471 : }
2472 :
2473 : /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2474 : are the arguments to the call. */
2475 :
2476 : static bool
2477 1493 : gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
2478 : {
2479 1493 : gimple *stmt = gsi_stmt (*gsi);
2480 1493 : tree dest = gimple_call_arg (stmt, 0);
2481 1493 : tree src = gimple_call_arg (stmt, 1);
2482 1493 : tree size = gimple_call_arg (stmt, 2);
2483 1493 : tree fn;
2484 1493 : const char *p;
2485 :
2486 1493 : p = c_getstr (src);
2487 : /* If the SRC parameter is "", return DEST. */
2488 1493 : if (p && *p == '\0')
2489 : {
2490 60 : replace_call_with_value (gsi, dest);
2491 60 : return true;
2492 : }
2493 :
2494 1433 : if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
2495 1351 : return false;
2496 :
2497 1515 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2498 : return false;
2499 :
2500 : /* If __builtin_strcat_chk is used, assume strcat is available. */
2501 82 : fn = builtin_decl_explicit (BUILT_IN_STRCAT);
2502 82 : if (!fn)
2503 : return false;
2504 :
2505 82 : gimple *repl = gimple_build_call (fn, 2, dest, src);
2506 82 : replace_call_with_call_and_fold (gsi, repl);
2507 82 : return true;
2508 : }
2509 :
2510 : /* Simplify a call to the strncat builtin. */
2511 :
2512 : static bool
2513 6786 : gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
2514 : {
2515 6786 : gimple *stmt = gsi_stmt (*gsi);
2516 6786 : tree dst = gimple_call_arg (stmt, 0);
2517 6786 : tree src = gimple_call_arg (stmt, 1);
2518 6786 : tree len = gimple_call_arg (stmt, 2);
2519 6786 : tree src_len = c_strlen (src, 1);
2520 :
2521 : /* If the requested length is zero, or the src parameter string
2522 : length is zero, return the dst parameter. */
2523 6786 : if (integer_zerop (len) || (src_len && integer_zerop (src_len)))
2524 : {
2525 119 : replace_call_with_value (gsi, dst);
2526 119 : return true;
2527 : }
2528 :
2529 : /* Return early if the requested len is less than the string length.
2530 : Warnings will be issued elsewhere later. */
2531 6667 : if (!src_len || known_lower (stmt, len, src_len, true))
2532 6099 : return false;
2533 :
2534 : /* Warn on constant LEN. */
2535 568 : if (TREE_CODE (len) == INTEGER_CST)
2536 : {
2537 131 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstringop_overflow_);
2538 131 : tree dstsize;
2539 :
2540 131 : if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize)
2541 175 : && TREE_CODE (dstsize) == INTEGER_CST)
2542 : {
2543 44 : int cmpdst = tree_int_cst_compare (len, dstsize);
2544 :
2545 44 : if (cmpdst >= 0)
2546 : {
2547 19 : tree fndecl = gimple_call_fndecl (stmt);
2548 :
2549 : /* Strncat copies (at most) LEN bytes and always appends
2550 : the terminating NUL so the specified bound should never
2551 : be equal to (or greater than) the size of the destination.
2552 : If it is, the copy could overflow. */
2553 19 : location_t loc = gimple_location (stmt);
2554 37 : nowarn = warning_at (loc, OPT_Wstringop_overflow_,
2555 : cmpdst == 0
2556 : ? G_("%qD specified bound %E equals "
2557 : "destination size")
2558 : : G_("%qD specified bound %E exceeds "
2559 : "destination size %E"),
2560 : fndecl, len, dstsize);
2561 19 : if (nowarn)
2562 0 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2563 : }
2564 : }
2565 :
2566 131 : if (!nowarn && TREE_CODE (src_len) == INTEGER_CST
2567 243 : && tree_int_cst_compare (src_len, len) == 0)
2568 : {
2569 20 : tree fndecl = gimple_call_fndecl (stmt);
2570 20 : location_t loc = gimple_location (stmt);
2571 :
2572 : /* To avoid possible overflow the specified bound should also
2573 : not be equal to the length of the source, even when the size
2574 : of the destination is unknown (it's not an uncommon mistake
2575 : to specify as the bound to strncpy the length of the source). */
2576 20 : if (warning_at (loc, OPT_Wstringop_overflow_,
2577 : "%qD specified bound %E equals source length",
2578 : fndecl, len))
2579 6 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2580 : }
2581 : }
2582 :
2583 568 : if (!known_lower (stmt, src_len, len))
2584 : return false;
2585 :
2586 136 : tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
2587 :
2588 : /* If the replacement _DECL isn't initialized, don't do the
2589 : transformation. */
2590 6803 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2591 : return false;
2592 :
2593 : /* Otherwise, emit a call to strcat. */
2594 136 : gcall *repl = gimple_build_call (fn, 2, dst, src);
2595 136 : replace_call_with_call_and_fold (gsi, repl);
2596 136 : return true;
2597 : }
2598 :
2599 : /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2600 : LEN, and SIZE. */
2601 :
2602 : static bool
2603 1116 : gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
2604 : {
2605 1116 : gimple *stmt = gsi_stmt (*gsi);
2606 1116 : tree dest = gimple_call_arg (stmt, 0);
2607 1116 : tree src = gimple_call_arg (stmt, 1);
2608 1116 : tree len = gimple_call_arg (stmt, 2);
2609 1116 : tree size = gimple_call_arg (stmt, 3);
2610 1116 : tree fn;
2611 1116 : const char *p;
2612 :
2613 1116 : p = c_getstr (src);
2614 : /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2615 276 : if ((p && *p == '\0')
2616 1341 : || integer_zerop (len))
2617 : {
2618 78 : replace_call_with_value (gsi, dest);
2619 78 : return true;
2620 : }
2621 :
2622 2988 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2623 : return false;
2624 :
2625 1038 : if (! integer_all_onesp (size))
2626 : {
2627 951 : tree src_len = c_strlen (src, 1);
2628 951 : if (known_lower (stmt, src_len, len))
2629 : {
2630 : /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2631 39 : fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
2632 39 : if (!fn)
2633 : return false;
2634 :
2635 39 : gimple *repl = gimple_build_call (fn, 3, dest, src, size);
2636 39 : replace_call_with_call_and_fold (gsi, repl);
2637 39 : return true;
2638 : }
2639 : return false;
2640 : }
2641 :
2642 : /* If __builtin_strncat_chk is used, assume strncat is available. */
2643 87 : fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
2644 87 : if (!fn)
2645 : return false;
2646 :
2647 87 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2648 87 : replace_call_with_call_and_fold (gsi, repl);
2649 87 : return true;
2650 : }
2651 :
2652 : /* Build and append gimple statements to STMTS that would load a first
2653 : character of a memory location identified by STR. LOC is location
2654 : of the statement. */
2655 :
2656 : static tree
2657 469 : gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
2658 : {
2659 469 : tree var;
2660 :
2661 469 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2662 469 : tree cst_uchar_ptr_node
2663 469 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
2664 469 : tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
2665 :
2666 469 : tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
2667 469 : gassign *stmt = gimple_build_assign (NULL_TREE, temp);
2668 469 : var = make_ssa_name (cst_uchar_node, stmt);
2669 :
2670 469 : gimple_assign_set_lhs (stmt, var);
2671 469 : gimple_seq_add_stmt_without_update (stmts, stmt);
2672 :
2673 469 : return var;
2674 : }
2675 :
2676 : /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. */
2677 :
2678 : static bool
2679 1250674 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
2680 : {
2681 1250674 : gimple *stmt = gsi_stmt (*gsi);
2682 1250674 : tree callee = gimple_call_fndecl (stmt);
2683 1250674 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
2684 :
2685 1250674 : tree type = integer_type_node;
2686 1250674 : tree str1 = gimple_call_arg (stmt, 0);
2687 1250674 : tree str2 = gimple_call_arg (stmt, 1);
2688 1250674 : tree lhs = gimple_call_lhs (stmt);
2689 :
2690 1250674 : tree bound_node = NULL_TREE;
2691 1250674 : unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
2692 :
2693 : /* Handle strncmp and strncasecmp functions. */
2694 1250674 : if (gimple_call_num_args (stmt) == 3)
2695 : {
2696 22993 : bound_node = gimple_call_arg (stmt, 2);
2697 22993 : if (tree_fits_uhwi_p (bound_node))
2698 17269 : bound = tree_to_uhwi (bound_node);
2699 : }
2700 :
2701 : /* If the BOUND parameter is zero, return zero. */
2702 17269 : if (bound == 0)
2703 : {
2704 4 : replace_call_with_value (gsi, integer_zero_node);
2705 4 : return true;
2706 : }
2707 :
2708 : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2709 1250670 : if (operand_equal_p (str1, str2, 0))
2710 : {
2711 41 : replace_call_with_value (gsi, integer_zero_node);
2712 41 : return true;
2713 : }
2714 :
2715 2501258 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2716 : return false;
2717 :
2718 : /* Initially set to the number of characters, including the terminating
2719 : nul if each array has one. LENx == strnlen (Sx, LENx) implies that
2720 : the array Sx is not terminated by a nul.
2721 : For nul-terminated strings then adjusted to their length so that
2722 : LENx == NULPOSx holds. */
2723 1250629 : unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
2724 1250629 : const char *p1 = getbyterep (str1, &len1);
2725 1250629 : const char *p2 = getbyterep (str2, &len2);
2726 :
2727 : /* The position of the terminating nul character if one exists, otherwise
2728 : a value greater than LENx. */
2729 1250629 : unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
2730 :
2731 1250629 : if (p1)
2732 : {
2733 42839 : size_t n = strnlen (p1, len1);
2734 42839 : if (n < len1)
2735 42732 : len1 = nulpos1 = n;
2736 : }
2737 :
2738 1250629 : if (p2)
2739 : {
2740 1219622 : size_t n = strnlen (p2, len2);
2741 1219622 : if (n < len2)
2742 1219563 : len2 = nulpos2 = n;
2743 : }
2744 :
2745 : /* For known strings, return an immediate value. */
2746 1250629 : if (p1 && p2)
2747 : {
2748 39245 : int r = 0;
2749 39245 : bool known_result = false;
2750 :
2751 39245 : switch (fcode)
2752 : {
2753 38169 : case BUILT_IN_STRCMP:
2754 38169 : case BUILT_IN_STRCMP_EQ:
2755 38169 : if (len1 != nulpos1 || len2 != nulpos2)
2756 : break;
2757 :
2758 38144 : r = strcmp (p1, p2);
2759 38144 : known_result = true;
2760 38144 : break;
2761 :
2762 1002 : case BUILT_IN_STRNCMP:
2763 1002 : case BUILT_IN_STRNCMP_EQ:
2764 1002 : {
2765 1002 : if (bound == HOST_WIDE_INT_M1U)
2766 : break;
2767 :
2768 : /* Reduce the bound to be no more than the length
2769 : of the shorter of the two strings, or the sizes
2770 : of the unterminated arrays. */
2771 38 : unsigned HOST_WIDE_INT n = bound;
2772 :
2773 38 : if (len1 == nulpos1 && len1 < n)
2774 4 : n = len1 + 1;
2775 38 : if (len2 == nulpos2 && len2 < n)
2776 11 : n = len2 + 1;
2777 :
2778 38 : if (MIN (nulpos1, nulpos2) + 1 < n)
2779 : break;
2780 :
2781 38 : r = strncmp (p1, p2, n);
2782 38 : known_result = true;
2783 38 : break;
2784 : }
2785 : /* Only handleable situation is where the string are equal (result 0),
2786 : which is already handled by operand_equal_p case. */
2787 : case BUILT_IN_STRCASECMP:
2788 : break;
2789 37 : case BUILT_IN_STRNCASECMP:
2790 37 : {
2791 37 : if (bound == HOST_WIDE_INT_M1U)
2792 : break;
2793 37 : r = strncmp (p1, p2, bound);
2794 37 : if (r == 0)
2795 : known_result = true;
2796 : break;
2797 : }
2798 0 : default:
2799 0 : gcc_unreachable ();
2800 : }
2801 :
2802 38182 : if (known_result)
2803 : {
2804 38182 : replace_call_with_value (gsi, build_cmp_result (type, r));
2805 38182 : return true;
2806 : }
2807 : }
2808 :
2809 2424894 : bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
2810 1195322 : || fcode == BUILT_IN_STRCMP
2811 1195322 : || fcode == BUILT_IN_STRCMP_EQ
2812 1218349 : || fcode == BUILT_IN_STRCASECMP;
2813 :
2814 1212447 : location_t loc = gimple_location (stmt);
2815 :
2816 : /* If the second arg is "", return *(const unsigned char*)arg1. */
2817 1212447 : if (p2 && *p2 == '\0' && nonzero_bound)
2818 : {
2819 150 : gimple_seq stmts = NULL;
2820 150 : tree var = gimple_load_first_char (loc, str1, &stmts);
2821 150 : if (lhs)
2822 : {
2823 150 : stmt = gimple_build_assign (lhs, NOP_EXPR, var);
2824 150 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2825 : }
2826 :
2827 150 : gsi_replace_with_seq_vops (gsi, stmts);
2828 150 : return true;
2829 : }
2830 :
2831 : /* If the first arg is "", return -*(const unsigned char*)arg2. */
2832 1212297 : if (p1 && *p1 == '\0' && nonzero_bound)
2833 : {
2834 99 : gimple_seq stmts = NULL;
2835 99 : tree var = gimple_load_first_char (loc, str2, &stmts);
2836 :
2837 99 : if (lhs)
2838 : {
2839 99 : tree c = make_ssa_name (integer_type_node);
2840 99 : stmt = gimple_build_assign (c, NOP_EXPR, var);
2841 99 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2842 :
2843 99 : stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
2844 99 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2845 : }
2846 :
2847 99 : gsi_replace_with_seq_vops (gsi, stmts);
2848 99 : return true;
2849 : }
2850 :
2851 : /* If BOUND is one, return an expression corresponding to
2852 : (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2853 1212198 : if (fcode == BUILT_IN_STRNCMP && bound == 1)
2854 : {
2855 110 : gimple_seq stmts = NULL;
2856 110 : tree temp1 = gimple_load_first_char (loc, str1, &stmts);
2857 110 : tree temp2 = gimple_load_first_char (loc, str2, &stmts);
2858 :
2859 110 : if (lhs)
2860 : {
2861 107 : tree c1 = make_ssa_name (integer_type_node);
2862 107 : gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
2863 107 : gimple_seq_add_stmt_without_update (&stmts, convert1);
2864 :
2865 107 : tree c2 = make_ssa_name (integer_type_node);
2866 107 : gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
2867 107 : gimple_seq_add_stmt_without_update (&stmts, convert2);
2868 :
2869 107 : stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
2870 107 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2871 : }
2872 :
2873 110 : gsi_replace_with_seq_vops (gsi, stmts);
2874 110 : return true;
2875 : }
2876 :
2877 : /* If BOUND is greater than the length of one constant string,
2878 : and the other argument is also a nul-terminated string, replace
2879 : strncmp with strcmp. */
2880 1212088 : if (fcode == BUILT_IN_STRNCMP
2881 17731 : && bound > 0 && bound < HOST_WIDE_INT_M1U
2882 12139 : && ((p2 && len2 < bound && len2 == nulpos2)
2883 11907 : || (p1 && len1 < bound && len1 == nulpos1)))
2884 : {
2885 1212088 : tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
2886 310 : if (!fn)
2887 : return false;
2888 310 : gimple *repl = gimple_build_call (fn, 2, str1, str2);
2889 310 : replace_call_with_call_and_fold (gsi, repl);
2890 310 : return true;
2891 : }
2892 :
2893 : return false;
2894 : }
2895 :
2896 : /* Fold a call to the memchr pointed by GSI iterator. */
2897 :
2898 : static bool
2899 30420 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2900 : {
2901 30420 : gimple *stmt = gsi_stmt (*gsi);
2902 30420 : tree lhs = gimple_call_lhs (stmt);
2903 30420 : tree arg1 = gimple_call_arg (stmt, 0);
2904 30420 : tree arg2 = gimple_call_arg (stmt, 1);
2905 30420 : tree len = gimple_call_arg (stmt, 2);
2906 :
2907 : /* If the LEN parameter is zero, return zero. */
2908 30420 : if (integer_zerop (len))
2909 : {
2910 1 : replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2911 1 : return true;
2912 : }
2913 :
2914 30419 : char c;
2915 30419 : if (TREE_CODE (arg2) != INTEGER_CST
2916 16540 : || !tree_fits_uhwi_p (len)
2917 31133 : || !target_char_cst_p (arg2, &c))
2918 29705 : return false;
2919 :
2920 714 : unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
2921 714 : unsigned HOST_WIDE_INT string_length;
2922 714 : const char *p1 = getbyterep (arg1, &string_length);
2923 :
2924 714 : if (p1)
2925 : {
2926 96 : const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
2927 96 : if (r == NULL)
2928 : {
2929 14 : tree mem_size, offset_node;
2930 14 : byte_representation (arg1, &offset_node, &mem_size, NULL);
2931 14 : unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE)
2932 14 : ? 0 : tree_to_uhwi (offset_node);
2933 : /* MEM_SIZE is the size of the array the string literal
2934 : is stored in. */
2935 14 : unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset;
2936 14 : gcc_checking_assert (string_length <= string_size);
2937 14 : if (length <= string_size)
2938 : {
2939 4 : replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2940 4 : return true;
2941 : }
2942 : }
2943 : else
2944 : {
2945 82 : unsigned HOST_WIDE_INT offset = r - p1;
2946 82 : gimple_seq stmts = NULL;
2947 82 : if (lhs != NULL_TREE)
2948 : {
2949 80 : tree offset_cst = build_int_cst (sizetype, offset);
2950 80 : gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
2951 : arg1, offset_cst);
2952 80 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2953 : }
2954 : else
2955 2 : gimple_seq_add_stmt_without_update (&stmts,
2956 : gimple_build_nop ());
2957 :
2958 82 : gsi_replace_with_seq_vops (gsi, stmts);
2959 82 : return true;
2960 : }
2961 : }
2962 :
2963 : return false;
2964 : }
2965 :
2966 : /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2967 : to the call. IGNORE is true if the value returned
2968 : by the builtin will be ignored. UNLOCKED is true is true if this
2969 : actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2970 : the known length of the string. Return NULL_TREE if no simplification
2971 : was possible. */
2972 :
2973 : static bool
2974 20740 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
2975 : tree arg0, tree arg1,
2976 : bool unlocked)
2977 : {
2978 20740 : gimple *stmt = gsi_stmt (*gsi);
2979 :
2980 : /* If we're using an unlocked function, assume the other unlocked
2981 : functions exist explicitly. */
2982 20740 : tree const fn_fputc = (unlocked
2983 20740 : ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2984 20697 : : builtin_decl_implicit (BUILT_IN_FPUTC));
2985 20697 : tree const fn_fwrite = (unlocked
2986 43 : ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2987 20740 : : builtin_decl_implicit (BUILT_IN_FWRITE));
2988 :
2989 : /* If the return value is used, don't do the transformation. */
2990 20740 : if (gimple_call_lhs (stmt))
2991 : return false;
2992 :
2993 : /* Get the length of the string passed to fputs. If the length
2994 : can't be determined, punt. */
2995 20669 : tree len = get_maxval_strlen (arg0, SRK_STRLEN);
2996 20669 : if (!len || TREE_CODE (len) != INTEGER_CST)
2997 : return false;
2998 :
2999 16265 : switch (compare_tree_int (len, 1))
3000 : {
3001 91 : case -1: /* length is 0, delete the call entirely . */
3002 91 : replace_call_with_value (gsi, integer_zero_node);
3003 91 : return true;
3004 :
3005 1060 : case 0: /* length is 1, call fputc. */
3006 1060 : {
3007 1060 : const char *p = c_getstr (arg0);
3008 1060 : if (p != NULL)
3009 : {
3010 2092 : if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3011 : return false;
3012 :
3013 1046 : gimple *repl
3014 1046 : = gimple_build_call (fn_fputc, 2,
3015 1046 : build_int_cst (integer_type_node, p[0]),
3016 : arg1);
3017 1046 : replace_call_with_call_and_fold (gsi, repl);
3018 1046 : return true;
3019 : }
3020 : }
3021 : /* FALLTHROUGH */
3022 15128 : case 1: /* length is greater than 1, call fwrite. */
3023 15128 : {
3024 : /* If optimizing for size keep fputs. */
3025 15128 : if (optimize_function_for_size_p (cfun))
3026 : return false;
3027 : /* New argument list transforming fputs(string, stream) to
3028 : fwrite(string, 1, len, stream). */
3029 27888 : if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3030 : return false;
3031 :
3032 8285 : gimple *repl
3033 8285 : = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
3034 : fold_convert (size_type_node, len), arg1);
3035 8285 : replace_call_with_call_and_fold (gsi, repl);
3036 8285 : return true;
3037 : }
3038 0 : default:
3039 0 : gcc_unreachable ();
3040 : }
3041 : }
3042 :
3043 : /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
3044 : DEST, SRC, LEN, and SIZE are the arguments to the call.
3045 : IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
3046 : code of the builtin. If MAXLEN is not NULL, it is maximum length
3047 : passed as third argument. */
3048 :
3049 : static bool
3050 25701 : gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
3051 : tree dest, tree src, tree len, tree size,
3052 : enum built_in_function fcode)
3053 : {
3054 25701 : gimple *stmt = gsi_stmt (*gsi);
3055 25701 : location_t loc = gimple_location (stmt);
3056 25701 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3057 25701 : tree fn;
3058 :
3059 : /* If SRC and DEST are the same (and not volatile), return DEST
3060 : (resp. DEST+LEN for __mempcpy_chk). */
3061 25701 : if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
3062 : {
3063 13 : if (fcode != BUILT_IN_MEMPCPY_CHK)
3064 : {
3065 7 : replace_call_with_value (gsi, dest);
3066 7 : return true;
3067 : }
3068 : else
3069 : {
3070 6 : gimple_seq stmts = NULL;
3071 6 : len = gimple_convert_to_ptrofftype (&stmts, loc, len);
3072 6 : tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
3073 6 : TREE_TYPE (dest), dest, len);
3074 6 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3075 6 : replace_call_with_value (gsi, temp);
3076 6 : return true;
3077 : }
3078 : }
3079 :
3080 68495 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3081 : return false;
3082 :
3083 25688 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3084 25688 : if (! integer_all_onesp (size)
3085 24689 : && !known_lower (stmt, len, size)
3086 42927 : && !known_lower (stmt, maxlen, size))
3087 : {
3088 : /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
3089 : least try to optimize (void) __mempcpy_chk () into
3090 : (void) __memcpy_chk () */
3091 17162 : if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
3092 : {
3093 43 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3094 43 : if (!fn)
3095 : return false;
3096 :
3097 43 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3098 43 : replace_call_with_call_and_fold (gsi, repl);
3099 43 : return true;
3100 : }
3101 : return false;
3102 : }
3103 :
3104 8526 : fn = NULL_TREE;
3105 : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
3106 : mem{cpy,pcpy,move,set} is available. */
3107 8526 : switch (fcode)
3108 : {
3109 1781 : case BUILT_IN_MEMCPY_CHK:
3110 1781 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
3111 1781 : break;
3112 1068 : case BUILT_IN_MEMPCPY_CHK:
3113 1068 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
3114 1068 : break;
3115 1652 : case BUILT_IN_MEMMOVE_CHK:
3116 1652 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
3117 1652 : break;
3118 4025 : case BUILT_IN_MEMSET_CHK:
3119 4025 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
3120 4025 : break;
3121 : default:
3122 : break;
3123 : }
3124 :
3125 8526 : if (!fn)
3126 : return false;
3127 :
3128 8526 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
3129 8526 : replace_call_with_call_and_fold (gsi, repl);
3130 8526 : return true;
3131 : }
3132 :
3133 : /* Fold a call to the __st[rp]cpy_chk builtin.
3134 : DEST, SRC, and SIZE are the arguments to the call.
3135 : IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
3136 : code of the builtin. If MAXLEN is not NULL, it is maximum length of
3137 : strings passed as second argument. */
3138 :
3139 : static bool
3140 2566 : gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
3141 : tree dest,
3142 : tree src, tree size,
3143 : enum built_in_function fcode)
3144 : {
3145 2566 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3146 2566 : location_t loc = gimple_location (stmt);
3147 2566 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3148 2566 : tree len, fn;
3149 :
3150 : /* If SRC and DEST are the same (and not volatile), return DEST. */
3151 2566 : if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
3152 : {
3153 : /* Issue -Wrestrict unless the pointers are null (those do
3154 : not point to objects and so do not indicate an overlap;
3155 : such calls could be the result of sanitization and jump
3156 : threading). */
3157 0 : if (!integer_zerop (dest)
3158 0 : && !warning_suppressed_p (stmt, OPT_Wrestrict))
3159 : {
3160 0 : tree func = gimple_call_fndecl (stmt);
3161 :
3162 0 : warning_at (loc, OPT_Wrestrict,
3163 : "%qD source argument is the same as destination",
3164 : func);
3165 : }
3166 :
3167 0 : replace_call_with_value (gsi, dest);
3168 0 : return true;
3169 : }
3170 :
3171 5132 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3172 : return false;
3173 :
3174 2566 : tree maxlen = get_maxval_strlen (src, SRK_STRLENMAX);
3175 2566 : if (! integer_all_onesp (size))
3176 : {
3177 2497 : len = c_strlen (src, 1);
3178 2497 : if (!known_lower (stmt, len, size, true)
3179 2497 : && !known_lower (stmt, maxlen, size, true))
3180 : {
3181 2185 : if (fcode == BUILT_IN_STPCPY_CHK)
3182 : {
3183 1077 : if (! ignore)
3184 : return false;
3185 :
3186 : /* If return value of __stpcpy_chk is ignored,
3187 : optimize into __strcpy_chk. */
3188 35 : fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
3189 35 : if (!fn)
3190 : return false;
3191 :
3192 35 : gimple *repl = gimple_build_call (fn, 3, dest, src, size);
3193 35 : replace_call_with_call_and_fold (gsi, repl);
3194 35 : return true;
3195 : }
3196 :
3197 1108 : if (! len || TREE_SIDE_EFFECTS (len))
3198 : return false;
3199 :
3200 : /* If c_strlen returned something, but not provably less than size,
3201 : transform __strcpy_chk into __memcpy_chk. */
3202 106 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3203 106 : if (!fn)
3204 : return false;
3205 :
3206 106 : gimple_seq stmts = NULL;
3207 106 : len = force_gimple_operand (len, &stmts, true, NULL_TREE);
3208 106 : len = gimple_convert (&stmts, loc, size_type_node, len);
3209 106 : len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
3210 : build_int_cst (size_type_node, 1));
3211 106 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3212 106 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3213 106 : replace_call_with_call_and_fold (gsi, repl);
3214 106 : return true;
3215 : }
3216 : }
3217 :
3218 : /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
3219 618 : fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore
3220 : ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
3221 381 : if (!fn)
3222 : return false;
3223 :
3224 381 : gcall *repl = gimple_build_call (fn, 2, dest, src);
3225 381 : replace_call_with_call_and_fold (gsi, repl);
3226 381 : return true;
3227 : }
3228 :
3229 : /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
3230 : are the arguments to the call. If MAXLEN is not NULL, it is maximum
3231 : length passed as third argument. IGNORE is true if return value can be
3232 : ignored. FCODE is the BUILT_IN_* code of the builtin. */
3233 :
3234 : static bool
3235 2721 : gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
3236 : tree dest, tree src,
3237 : tree len, tree size,
3238 : enum built_in_function fcode)
3239 : {
3240 2721 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3241 2721 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3242 2721 : tree fn;
3243 :
3244 2721 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3245 2721 : if (! integer_all_onesp (size)
3246 2721 : && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
3247 : {
3248 2264 : if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
3249 : {
3250 : /* If return value of __stpncpy_chk is ignored,
3251 : optimize into __strncpy_chk. */
3252 39 : fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
3253 39 : if (fn)
3254 : {
3255 39 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3256 39 : replace_call_with_call_and_fold (gsi, repl);
3257 39 : return true;
3258 : }
3259 : }
3260 : return false;
3261 : }
3262 :
3263 : /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
3264 717 : fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore
3265 : ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
3266 3139 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3267 : return false;
3268 :
3269 457 : gcall *repl = gimple_build_call (fn, 3, dest, src, len);
3270 457 : replace_call_with_call_and_fold (gsi, repl);
3271 457 : return true;
3272 : }
3273 :
3274 : /* Fold function call to builtin stpcpy with arguments DEST and SRC.
3275 : Return NULL_TREE if no simplification can be made. */
3276 :
3277 : static bool
3278 3674 : gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
3279 : {
3280 3674 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3281 3674 : location_t loc = gimple_location (stmt);
3282 3674 : tree dest = gimple_call_arg (stmt, 0);
3283 3674 : tree src = gimple_call_arg (stmt, 1);
3284 3674 : tree fn, lenp1;
3285 :
3286 7348 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3287 : return false;
3288 :
3289 : /* If the result is unused, replace stpcpy with strcpy. */
3290 3674 : if (gimple_call_lhs (stmt) == NULL_TREE)
3291 : {
3292 29 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3293 29 : if (!fn)
3294 : return false;
3295 29 : gimple_call_set_fndecl (stmt, fn);
3296 29 : fold_stmt (gsi);
3297 29 : return true;
3298 : }
3299 :
3300 : /* Set to non-null if ARG refers to an unterminated array. */
3301 3645 : c_strlen_data data = { };
3302 : /* The size of the unterminated array if SRC refers to one. */
3303 3645 : tree size;
3304 : /* True if the size is exact/constant, false if it's the lower bound
3305 : of a range. */
3306 3645 : bool exact;
3307 3645 : tree len = c_strlen (src, 1, &data, 1);
3308 3645 : if (!len
3309 703 : || TREE_CODE (len) != INTEGER_CST)
3310 : {
3311 3174 : data.decl = unterminated_array (src, &size, &exact);
3312 3174 : if (!data.decl)
3313 : return false;
3314 : }
3315 :
3316 1076 : if (data.decl)
3317 : {
3318 : /* Avoid folding calls with unterminated arrays. */
3319 605 : if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
3320 75 : warn_string_no_nul (loc, stmt, "stpcpy", src, data.decl, size,
3321 : exact);
3322 605 : suppress_warning (stmt, OPT_Wstringop_overread);
3323 605 : return false;
3324 : }
3325 :
3326 471 : if (optimize_function_for_size_p (cfun)
3327 : /* If length is zero it's small enough. */
3328 471 : && !integer_zerop (len))
3329 : return false;
3330 :
3331 : /* If the source has a known length replace stpcpy with memcpy. */
3332 3645 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3333 287 : if (!fn)
3334 : return false;
3335 :
3336 287 : gimple_seq stmts = NULL;
3337 287 : tree tem = gimple_convert (&stmts, loc, size_type_node, len);
3338 287 : lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
3339 : tem, build_int_cst (size_type_node, 1));
3340 287 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3341 287 : gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
3342 287 : gimple_move_vops (repl, stmt);
3343 287 : gsi_insert_before (gsi, repl, GSI_SAME_STMT);
3344 : /* Replace the result with dest + len. */
3345 287 : stmts = NULL;
3346 287 : tem = gimple_convert (&stmts, loc, sizetype, len);
3347 287 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3348 287 : gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
3349 : POINTER_PLUS_EXPR, dest, tem);
3350 287 : gsi_replace (gsi, ret, false);
3351 : /* Finally fold the memcpy call. */
3352 287 : gimple_stmt_iterator gsi2 = *gsi;
3353 287 : gsi_prev (&gsi2);
3354 287 : fold_stmt (&gsi2);
3355 287 : return true;
3356 : }
3357 :
3358 : /* Simplify mempcpy call stmt at GSI, returning true if simplified.
3359 : Currently only handling mempcpy -> memcpy when the return value
3360 : is ignored. */
3361 :
3362 : static bool
3363 9536 : gimple_fold_builtin_mempcpy (gimple_stmt_iterator *gsi)
3364 : {
3365 9536 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3366 :
3367 9536 : if (gimple_call_lhs (stmt) != NULL_TREE)
3368 : return false;
3369 :
3370 385 : tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
3371 385 : if (!fn)
3372 : return false;
3373 :
3374 385 : tree dest = gimple_call_arg (stmt, 0);
3375 385 : tree src = gimple_call_arg (stmt, 1);
3376 385 : tree n = gimple_call_arg (stmt, 2);
3377 :
3378 385 : gcall *repl = gimple_build_call (fn, 3, dest, src, n);
3379 385 : replace_call_with_call_and_fold (gsi, repl);
3380 :
3381 385 : return true;
3382 : }
3383 :
3384 : /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3385 : NULL_TREE if a normal call should be emitted rather than expanding
3386 : the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3387 : BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3388 : passed as second argument. */
3389 :
3390 : static bool
3391 2359 : gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
3392 : enum built_in_function fcode)
3393 : {
3394 2359 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3395 2359 : tree dest, size, len, fn, fmt, flag;
3396 2359 : const char *fmt_str;
3397 :
3398 : /* Verify the required arguments in the original call. */
3399 2359 : if (gimple_call_num_args (stmt) < 5)
3400 : return false;
3401 :
3402 2359 : dest = gimple_call_arg (stmt, 0);
3403 2359 : len = gimple_call_arg (stmt, 1);
3404 2359 : flag = gimple_call_arg (stmt, 2);
3405 2359 : size = gimple_call_arg (stmt, 3);
3406 2359 : fmt = gimple_call_arg (stmt, 4);
3407 :
3408 2359 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3409 2359 : if (! integer_all_onesp (size)
3410 2359 : && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
3411 : return false;
3412 :
3413 308 : if (!init_target_chars ())
3414 : return false;
3415 :
3416 : /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3417 : or if format doesn't contain % chars or is "%s". */
3418 308 : if (! integer_zerop (flag))
3419 : {
3420 52 : fmt_str = c_getstr (fmt);
3421 52 : if (fmt_str == NULL)
3422 : return false;
3423 52 : if (strchr (fmt_str, target_percent) != NULL
3424 51 : && strcmp (fmt_str, target_percent_s))
3425 : return false;
3426 : }
3427 :
3428 : /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3429 : available. */
3430 415 : fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
3431 : ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
3432 2618 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3433 : return false;
3434 :
3435 : /* Replace the called function and the first 5 argument by 3 retaining
3436 : trailing varargs. */
3437 259 : gimple_call_set_fndecl (stmt, fn);
3438 259 : gimple_call_set_fntype (stmt, TREE_TYPE (fn));
3439 259 : gimple_call_set_arg (stmt, 0, dest);
3440 259 : gimple_call_set_arg (stmt, 1, len);
3441 259 : gimple_call_set_arg (stmt, 2, fmt);
3442 546 : for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
3443 287 : gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
3444 259 : gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
3445 259 : fold_stmt (gsi);
3446 259 : return true;
3447 : }
3448 :
3449 : /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3450 : Return NULL_TREE if a normal call should be emitted rather than
3451 : expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3452 : or BUILT_IN_VSPRINTF_CHK. */
3453 :
3454 : static bool
3455 4459 : gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
3456 : enum built_in_function fcode)
3457 : {
3458 4459 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3459 4459 : tree dest, size, len, fn, fmt, flag;
3460 4459 : const char *fmt_str;
3461 4459 : unsigned nargs = gimple_call_num_args (stmt);
3462 :
3463 : /* Verify the required arguments in the original call. */
3464 4459 : if (nargs < 4)
3465 : return false;
3466 4459 : dest = gimple_call_arg (stmt, 0);
3467 4459 : flag = gimple_call_arg (stmt, 1);
3468 4459 : size = gimple_call_arg (stmt, 2);
3469 4459 : fmt = gimple_call_arg (stmt, 3);
3470 :
3471 4459 : len = NULL_TREE;
3472 :
3473 4459 : if (!init_target_chars ())
3474 : return false;
3475 :
3476 : /* Check whether the format is a literal string constant. */
3477 4459 : fmt_str = c_getstr (fmt);
3478 4459 : if (fmt_str != NULL)
3479 : {
3480 : /* If the format doesn't contain % args or %%, we know the size. */
3481 4069 : if (strchr (fmt_str, target_percent) == 0)
3482 : {
3483 251 : if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
3484 251 : len = build_int_cstu (size_type_node, strlen (fmt_str));
3485 : }
3486 : /* If the format is "%s" and first ... argument is a string literal,
3487 : we know the size too. */
3488 3818 : else if (fcode == BUILT_IN_SPRINTF_CHK
3489 2962 : && strcmp (fmt_str, target_percent_s) == 0)
3490 : {
3491 395 : tree arg;
3492 :
3493 395 : if (nargs == 5)
3494 : {
3495 395 : arg = gimple_call_arg (stmt, 4);
3496 395 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
3497 363 : len = c_strlen (arg, 1);
3498 : }
3499 : }
3500 : }
3501 :
3502 4459 : if (! integer_all_onesp (size) && !known_lower (stmt, len, size, true))
3503 : return false;
3504 :
3505 : /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3506 : or if format doesn't contain % chars or is "%s". */
3507 202 : if (! integer_zerop (flag))
3508 : {
3509 1 : if (fmt_str == NULL)
3510 : return false;
3511 1 : if (strchr (fmt_str, target_percent) != NULL
3512 0 : && strcmp (fmt_str, target_percent_s))
3513 : return false;
3514 : }
3515 :
3516 : /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3517 347 : fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
3518 : ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
3519 4661 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3520 : return false;
3521 :
3522 : /* Replace the called function and the first 4 argument by 2 retaining
3523 : trailing varargs. */
3524 202 : gimple_call_set_fndecl (stmt, fn);
3525 202 : gimple_call_set_fntype (stmt, TREE_TYPE (fn));
3526 202 : gimple_call_set_arg (stmt, 0, dest);
3527 202 : gimple_call_set_arg (stmt, 1, fmt);
3528 400 : for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
3529 198 : gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
3530 202 : gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
3531 202 : fold_stmt (gsi);
3532 202 : return true;
3533 : }
3534 :
3535 : /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3536 : ORIG may be null if this is a 2-argument call. We don't attempt to
3537 : simplify calls with more than 3 arguments.
3538 :
3539 : Return true if simplification was possible, otherwise false. */
3540 :
3541 : bool
3542 2284 : gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
3543 : {
3544 2284 : gimple *stmt = gsi_stmt (*gsi);
3545 :
3546 : /* Verify the required arguments in the original call. We deal with two
3547 : types of sprintf() calls: 'sprintf (str, fmt)' and
3548 : 'sprintf (dest, "%s", orig)'. */
3549 2284 : if (gimple_call_num_args (stmt) > 3)
3550 : return false;
3551 :
3552 1883 : tree orig = NULL_TREE;
3553 1883 : if (gimple_call_num_args (stmt) == 3)
3554 1785 : orig = gimple_call_arg (stmt, 2);
3555 :
3556 : /* Check whether the format is a literal string constant. */
3557 1883 : tree fmt = gimple_call_arg (stmt, 1);
3558 1883 : const char *fmt_str = c_getstr (fmt);
3559 1883 : if (fmt_str == NULL)
3560 : return false;
3561 :
3562 1883 : tree dest = gimple_call_arg (stmt, 0);
3563 :
3564 1883 : if (!init_target_chars ())
3565 : return false;
3566 :
3567 1883 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3568 5192 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3569 : return false;
3570 :
3571 : /* If the format doesn't contain % args or %%, use strcpy. */
3572 1883 : if (strchr (fmt_str, target_percent) == NULL)
3573 : {
3574 : /* Don't optimize sprintf (buf, "abc", ptr++). */
3575 110 : if (orig)
3576 : return false;
3577 :
3578 : /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3579 : 'format' is known to contain no % formats. */
3580 97 : gimple_seq stmts = NULL;
3581 97 : gimple *repl = gimple_build_call (fn, 2, dest, fmt);
3582 :
3583 : /* Propagate the NO_WARNING bit to avoid issuing the same
3584 : warning more than once. */
3585 97 : copy_warning (repl, stmt);
3586 :
3587 97 : gimple_seq_add_stmt_without_update (&stmts, repl);
3588 97 : if (tree lhs = gimple_call_lhs (stmt))
3589 : {
3590 0 : repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs),
3591 0 : strlen (fmt_str)));
3592 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3593 0 : gsi_replace_with_seq_vops (gsi, stmts);
3594 : /* gsi now points at the assignment to the lhs, get a
3595 : stmt iterator to the memcpy call.
3596 : ??? We can't use gsi_for_stmt as that doesn't work when the
3597 : CFG isn't built yet. */
3598 0 : gimple_stmt_iterator gsi2 = *gsi;
3599 0 : gsi_prev (&gsi2);
3600 0 : fold_stmt (&gsi2);
3601 : }
3602 : else
3603 : {
3604 97 : gsi_replace_with_seq_vops (gsi, stmts);
3605 97 : fold_stmt (gsi);
3606 : }
3607 97 : return true;
3608 : }
3609 :
3610 : /* If the format is "%s", use strcpy if the result isn't used. */
3611 1773 : else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3612 : {
3613 : /* Don't crash on sprintf (str1, "%s"). */
3614 748 : if (!orig)
3615 : return false;
3616 :
3617 : /* Don't fold calls with source arguments of invalid (nonpointer)
3618 : types. */
3619 747 : if (!POINTER_TYPE_P (TREE_TYPE (orig)))
3620 : return false;
3621 :
3622 741 : tree orig_len = NULL_TREE;
3623 741 : if (gimple_call_lhs (stmt))
3624 : {
3625 17 : orig_len = get_maxval_strlen (orig, SRK_STRLEN);
3626 17 : if (!orig_len)
3627 : return false;
3628 : }
3629 :
3630 : /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3631 724 : gimple_seq stmts = NULL;
3632 724 : gimple *repl = gimple_build_call (fn, 2, dest, orig);
3633 :
3634 : /* Propagate the NO_WARNING bit to avoid issuing the same
3635 : warning more than once. */
3636 724 : copy_warning (repl, stmt);
3637 :
3638 724 : gimple_seq_add_stmt_without_update (&stmts, repl);
3639 724 : if (tree lhs = gimple_call_lhs (stmt))
3640 : {
3641 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
3642 0 : TREE_TYPE (orig_len)))
3643 0 : orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
3644 0 : repl = gimple_build_assign (lhs, orig_len);
3645 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3646 0 : gsi_replace_with_seq_vops (gsi, stmts);
3647 : /* gsi now points at the assignment to the lhs, get a
3648 : stmt iterator to the memcpy call.
3649 : ??? We can't use gsi_for_stmt as that doesn't work when the
3650 : CFG isn't built yet. */
3651 0 : gimple_stmt_iterator gsi2 = *gsi;
3652 0 : gsi_prev (&gsi2);
3653 0 : fold_stmt (&gsi2);
3654 : }
3655 : else
3656 : {
3657 724 : gsi_replace_with_seq_vops (gsi, stmts);
3658 724 : fold_stmt (gsi);
3659 : }
3660 724 : return true;
3661 : }
3662 : return false;
3663 : }
3664 :
3665 : /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3666 : FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3667 : attempt to simplify calls with more than 4 arguments.
3668 :
3669 : Return true if simplification was possible, otherwise false. */
3670 :
3671 : bool
3672 1733 : gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
3673 : {
3674 1733 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3675 1733 : tree dest = gimple_call_arg (stmt, 0);
3676 1733 : tree destsize = gimple_call_arg (stmt, 1);
3677 1733 : tree fmt = gimple_call_arg (stmt, 2);
3678 1733 : tree orig = NULL_TREE;
3679 1733 : const char *fmt_str = NULL;
3680 :
3681 1733 : if (gimple_call_num_args (stmt) > 4
3682 2991 : || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3683 : return false;
3684 :
3685 812 : if (gimple_call_num_args (stmt) == 4)
3686 613 : orig = gimple_call_arg (stmt, 3);
3687 :
3688 : /* Check whether the format is a literal string constant. */
3689 812 : fmt_str = c_getstr (fmt);
3690 812 : if (fmt_str == NULL)
3691 : return false;
3692 :
3693 812 : if (!init_target_chars ())
3694 : return false;
3695 :
3696 : /* If the format doesn't contain % args or %%, use strcpy. */
3697 812 : if (strchr (fmt_str, target_percent) == NULL)
3698 : {
3699 228 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3700 198 : if (!fn)
3701 : return false;
3702 :
3703 : /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3704 198 : if (orig)
3705 : return false;
3706 :
3707 198 : tree len = build_int_cstu (TREE_TYPE (destsize), strlen (fmt_str));
3708 :
3709 : /* We could expand this as
3710 : memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3711 : or to
3712 : memcpy (str, fmt_with_nul_at_cstm1, cst);
3713 : but in the former case that might increase code size
3714 : and in the latter case grow .rodata section too much.
3715 : So punt for now. */
3716 198 : if (!known_lower (stmt, len, destsize, true))
3717 : return false;
3718 :
3719 168 : gimple_seq stmts = NULL;
3720 168 : gimple *repl = gimple_build_call (fn, 2, dest, fmt);
3721 168 : gimple_seq_add_stmt_without_update (&stmts, repl);
3722 168 : if (tree lhs = gimple_call_lhs (stmt))
3723 : {
3724 0 : repl = gimple_build_assign (lhs,
3725 0 : fold_convert (TREE_TYPE (lhs), len));
3726 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3727 0 : gsi_replace_with_seq_vops (gsi, stmts);
3728 : /* gsi now points at the assignment to the lhs, get a
3729 : stmt iterator to the memcpy call.
3730 : ??? We can't use gsi_for_stmt as that doesn't work when the
3731 : CFG isn't built yet. */
3732 0 : gimple_stmt_iterator gsi2 = *gsi;
3733 0 : gsi_prev (&gsi2);
3734 0 : fold_stmt (&gsi2);
3735 : }
3736 : else
3737 : {
3738 168 : gsi_replace_with_seq_vops (gsi, stmts);
3739 168 : fold_stmt (gsi);
3740 : }
3741 168 : return true;
3742 : }
3743 :
3744 : /* If the format is "%s", use strcpy if the result isn't used. */
3745 614 : else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3746 : {
3747 292 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3748 174 : if (!fn)
3749 : return false;
3750 :
3751 : /* Don't crash on snprintf (str1, cst, "%s"). */
3752 174 : if (!orig)
3753 : return false;
3754 :
3755 174 : tree orig_len = get_maxval_strlen (orig, SRK_STRLEN);
3756 :
3757 : /* We could expand this as
3758 : memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3759 : or to
3760 : memcpy (str1, str2_with_nul_at_cstm1, cst);
3761 : but in the former case that might increase code size
3762 : and in the latter case grow .rodata section too much.
3763 : So punt for now. */
3764 174 : if (!known_lower (stmt, orig_len, destsize, true))
3765 : return false;
3766 :
3767 : /* Convert snprintf (str1, cst, "%s", str2) into
3768 : strcpy (str1, str2) if strlen (str2) < cst. */
3769 56 : gimple_seq stmts = NULL;
3770 56 : gimple *repl = gimple_build_call (fn, 2, dest, orig);
3771 56 : gimple_seq_add_stmt_without_update (&stmts, repl);
3772 56 : if (tree lhs = gimple_call_lhs (stmt))
3773 : {
3774 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
3775 0 : TREE_TYPE (orig_len)))
3776 0 : orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
3777 0 : repl = gimple_build_assign (lhs, orig_len);
3778 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3779 0 : gsi_replace_with_seq_vops (gsi, stmts);
3780 : /* gsi now points at the assignment to the lhs, get a
3781 : stmt iterator to the memcpy call.
3782 : ??? We can't use gsi_for_stmt as that doesn't work when the
3783 : CFG isn't built yet. */
3784 0 : gimple_stmt_iterator gsi2 = *gsi;
3785 0 : gsi_prev (&gsi2);
3786 0 : fold_stmt (&gsi2);
3787 : }
3788 : else
3789 : {
3790 56 : gsi_replace_with_seq_vops (gsi, stmts);
3791 56 : fold_stmt (gsi);
3792 : }
3793 56 : return true;
3794 : }
3795 : return false;
3796 : }
3797 :
3798 : /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3799 : FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3800 : more than 3 arguments, and ARG may be null in the 2-argument case.
3801 :
3802 : Return NULL_TREE if no simplification was possible, otherwise return the
3803 : simplified form of the call as a tree. FCODE is the BUILT_IN_*
3804 : code of the function to be simplified. */
3805 :
3806 : static bool
3807 54546 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
3808 : tree fp, tree fmt, tree arg,
3809 : enum built_in_function fcode)
3810 : {
3811 54546 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3812 54546 : tree fn_fputc, fn_fputs;
3813 54546 : const char *fmt_str = NULL;
3814 :
3815 : /* If the return value is used, don't do the transformation. */
3816 54546 : if (gimple_call_lhs (stmt) != NULL_TREE)
3817 : return false;
3818 :
3819 : /* Check whether the format is a literal string constant. */
3820 50369 : fmt_str = c_getstr (fmt);
3821 50369 : if (fmt_str == NULL)
3822 : return false;
3823 :
3824 50039 : if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
3825 : {
3826 : /* If we're using an unlocked function, assume the other
3827 : unlocked functions exist explicitly. */
3828 80 : fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
3829 80 : fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
3830 : }
3831 : else
3832 : {
3833 49959 : fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
3834 49959 : fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
3835 : }
3836 :
3837 50039 : if (!init_target_chars ())
3838 : return false;
3839 :
3840 144270 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3841 : return false;
3842 :
3843 : /* If the format doesn't contain % args or %%, use strcpy. */
3844 50039 : if (strchr (fmt_str, target_percent) == NULL)
3845 : {
3846 9666 : if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
3847 9596 : && arg)
3848 : return false;
3849 :
3850 : /* If the format specifier was "", fprintf does nothing. */
3851 9666 : if (fmt_str[0] == '\0')
3852 : {
3853 58 : replace_call_with_value (gsi, NULL_TREE);
3854 58 : return true;
3855 : }
3856 :
3857 : /* When "string" doesn't contain %, replace all cases of
3858 : fprintf (fp, string) with fputs (string, fp). The fputs
3859 : builtin will take care of special cases like length == 1. */
3860 9608 : if (fn_fputs)
3861 : {
3862 9608 : gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
3863 9608 : replace_call_with_call_and_fold (gsi, repl);
3864 9608 : return true;
3865 : }
3866 : }
3867 :
3868 : /* The other optimizations can be done only on the non-va_list variants. */
3869 40373 : else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
3870 : return false;
3871 :
3872 : /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3873 39324 : else if (strcmp (fmt_str, target_percent_s) == 0)
3874 : {
3875 639 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3876 : return false;
3877 639 : if (fn_fputs)
3878 : {
3879 639 : gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3880 639 : replace_call_with_call_and_fold (gsi, repl);
3881 639 : return true;
3882 : }
3883 : }
3884 :
3885 : /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3886 38685 : else if (strcmp (fmt_str, target_percent_c) == 0)
3887 : {
3888 49 : if (!arg
3889 49 : || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
3890 0 : return false;
3891 49 : if (fn_fputc)
3892 : {
3893 49 : gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
3894 49 : replace_call_with_call_and_fold (gsi, repl);
3895 49 : return true;
3896 : }
3897 : }
3898 :
3899 : return false;
3900 : }
3901 :
3902 : /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3903 : FMT and ARG are the arguments to the call; we don't fold cases with
3904 : more than 2 arguments, and ARG may be null if this is a 1-argument case.
3905 :
3906 : Return NULL_TREE if no simplification was possible, otherwise return the
3907 : simplified form of the call as a tree. FCODE is the BUILT_IN_*
3908 : code of the function to be simplified. */
3909 :
3910 : static bool
3911 123796 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3912 : tree arg, enum built_in_function fcode)
3913 : {
3914 123796 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3915 123796 : tree fn_putchar, fn_puts, newarg;
3916 123796 : const char *fmt_str = NULL;
3917 :
3918 : /* If the return value is used, don't do the transformation. */
3919 123796 : if (gimple_call_lhs (stmt) != NULL_TREE)
3920 : return false;
3921 :
3922 359962 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3923 : return false;
3924 :
3925 : /* Check whether the format is a literal string constant. */
3926 120538 : fmt_str = c_getstr (fmt);
3927 120538 : if (fmt_str == NULL)
3928 : return false;
3929 :
3930 117469 : if (fcode == BUILT_IN_PRINTF_UNLOCKED)
3931 : {
3932 : /* If we're using an unlocked function, assume the other
3933 : unlocked functions exist explicitly. */
3934 80 : fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
3935 80 : fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
3936 : }
3937 : else
3938 : {
3939 117389 : fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3940 117389 : fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3941 : }
3942 :
3943 117469 : if (!init_target_chars ())
3944 : return false;
3945 :
3946 117469 : if (strcmp (fmt_str, target_percent_s) == 0
3947 111083 : || strchr (fmt_str, target_percent) == NULL)
3948 : {
3949 14162 : const char *str;
3950 :
3951 14162 : if (strcmp (fmt_str, target_percent_s) == 0)
3952 : {
3953 6386 : if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3954 : return false;
3955 :
3956 6098 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3957 : return false;
3958 :
3959 6093 : str = c_getstr (arg);
3960 6093 : if (str == NULL)
3961 : return false;
3962 : }
3963 : else
3964 : {
3965 : /* The format specifier doesn't contain any '%' characters. */
3966 7776 : if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3967 7644 : && arg)
3968 : return false;
3969 : str = fmt_str;
3970 : }
3971 :
3972 : /* If the string was "", printf does nothing. */
3973 5776 : if (str[0] == '\0')
3974 : {
3975 109 : replace_call_with_value (gsi, NULL_TREE);
3976 109 : return true;
3977 : }
3978 :
3979 : /* If the string has length of 1, call putchar. */
3980 5667 : if (str[1] == '\0')
3981 : {
3982 : /* Given printf("c"), (where c is any one character,)
3983 : convert "c"[0] to an int and pass that to the replacement
3984 : function. */
3985 559 : newarg = build_int_cst (integer_type_node, str[0]);
3986 559 : if (fn_putchar)
3987 : {
3988 559 : gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
3989 559 : replace_call_with_call_and_fold (gsi, repl);
3990 559 : return true;
3991 : }
3992 : }
3993 : else
3994 : {
3995 : /* If the string was "string\n", call puts("string"). */
3996 5108 : size_t len = strlen (str);
3997 5108 : if ((unsigned char)str[len - 1] == target_newline
3998 4014 : && (size_t) (int) len == len
3999 4014 : && (int) len > 0)
4000 : {
4001 4014 : char *newstr;
4002 :
4003 : /* Create a NUL-terminated string that's one char shorter
4004 : than the original, stripping off the trailing '\n'. */
4005 4014 : newstr = xstrdup (str);
4006 4014 : newstr[len - 1] = '\0';
4007 4014 : newarg = build_string_literal (len, newstr);
4008 4014 : free (newstr);
4009 4014 : if (fn_puts)
4010 : {
4011 4014 : gcall *repl = gimple_build_call (fn_puts, 1, newarg);
4012 4014 : replace_call_with_call_and_fold (gsi, repl);
4013 4014 : return true;
4014 : }
4015 : }
4016 : else
4017 : /* We'd like to arrange to call fputs(string,stdout) here,
4018 : but we need stdout and don't have a way to get it yet. */
4019 : return false;
4020 : }
4021 : }
4022 :
4023 : /* The other optimizations can be done only on the non-va_list variants. */
4024 103307 : else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
4025 : return false;
4026 :
4027 : /* If the format specifier was "%s\n", call __builtin_puts(arg). */
4028 103057 : else if (strcmp (fmt_str, target_percent_s_newline) == 0)
4029 : {
4030 181 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
4031 : return false;
4032 181 : if (fn_puts)
4033 : {
4034 181 : gcall *repl = gimple_build_call (fn_puts, 1, arg);
4035 181 : replace_call_with_call_and_fold (gsi, repl);
4036 181 : return true;
4037 : }
4038 : }
4039 :
4040 : /* If the format specifier was "%c", call __builtin_putchar(arg). */
4041 102876 : else if (strcmp (fmt_str, target_percent_c) == 0)
4042 : {
4043 94 : if (!arg || ! useless_type_conversion_p (integer_type_node,
4044 47 : TREE_TYPE (arg)))
4045 0 : return false;
4046 47 : if (fn_putchar)
4047 : {
4048 47 : gcall *repl = gimple_build_call (fn_putchar, 1, arg);
4049 47 : replace_call_with_call_and_fold (gsi, repl);
4050 47 : return true;
4051 : }
4052 : }
4053 :
4054 : return false;
4055 : }
4056 :
4057 :
4058 :
4059 : /* Fold a call to __builtin_strlen with known length LEN. */
4060 :
4061 : static bool
4062 136437 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
4063 : {
4064 136437 : gimple *stmt = gsi_stmt (*gsi);
4065 136437 : tree arg = gimple_call_arg (stmt, 0);
4066 :
4067 136437 : wide_int minlen;
4068 136437 : wide_int maxlen;
4069 :
4070 136437 : c_strlen_data lendata = { };
4071 136437 : if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
4072 34679 : && !lendata.decl
4073 31716 : && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
4074 168048 : && lendata.maxlen && TREE_CODE (lendata.maxlen) == INTEGER_CST)
4075 : {
4076 : /* The range of lengths refers to either a single constant
4077 : string or to the longest and shortest constant string
4078 : referenced by the argument of the strlen() call, or to
4079 : the strings that can possibly be stored in the arrays
4080 : the argument refers to. */
4081 31611 : minlen = wi::to_wide (lendata.minlen);
4082 31611 : maxlen = wi::to_wide (lendata.maxlen);
4083 : }
4084 : else
4085 : {
4086 104826 : unsigned prec = TYPE_PRECISION (sizetype);
4087 :
4088 104826 : minlen = wi::shwi (0, prec);
4089 104826 : maxlen = wi::to_wide (max_object_size (), prec) - 2;
4090 : }
4091 :
4092 : /* For -fsanitize=address, don't optimize the upper bound of the
4093 : length to be able to diagnose UB on non-zero terminated arrays. */
4094 136437 : if (sanitize_flags_p (SANITIZE_ADDRESS))
4095 278 : maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
4096 :
4097 136437 : if (minlen == maxlen)
4098 : {
4099 : /* Fold the strlen call to a constant. */
4100 1552 : tree type = TREE_TYPE (lendata.minlen);
4101 3104 : tree len = force_gimple_operand_gsi (gsi,
4102 1552 : wide_int_to_tree (type, minlen),
4103 : true, NULL, true, GSI_SAME_STMT);
4104 1552 : replace_call_with_value (gsi, len);
4105 1552 : return true;
4106 : }
4107 :
4108 : /* Set the strlen() range to [0, MAXLEN]. */
4109 134885 : if (tree lhs = gimple_call_lhs (stmt))
4110 134880 : set_strlen_range (lhs, minlen, maxlen);
4111 :
4112 : return false;
4113 136437 : }
4114 :
4115 : static bool
4116 234 : gimple_fold_builtin_omp_is_initial_device (gimple_stmt_iterator *gsi)
4117 : {
4118 : #if ACCEL_COMPILER
4119 : replace_call_with_value (gsi, integer_zero_node);
4120 : return true;
4121 : #else
4122 234 : if (!ENABLE_OFFLOADING || symtab->state == EXPANSION)
4123 : {
4124 0 : replace_call_with_value (gsi, integer_one_node);
4125 234 : return true;
4126 : }
4127 : #endif
4128 : return false;
4129 : }
4130 :
4131 : /* omp_get_initial_device was in OpenMP 5.0/5.1 explicitly and in
4132 : 5.0 implicitly the same as omp_get_num_devices; since 6.0 it is
4133 : unspecified whether -1 or omp_get_num_devices() is returned. For
4134 : better backward compatibility, use omp_get_num_devices() on the
4135 : host - and -1 on the device (where the result is unspecified). */
4136 :
4137 : static bool
4138 103 : gimple_fold_builtin_omp_get_initial_device (gimple_stmt_iterator *gsi)
4139 : {
4140 : #if ACCEL_COMPILER
4141 : replace_call_with_value (gsi, build_int_cst (integer_type_node, -1));
4142 : #else
4143 103 : if (!ENABLE_OFFLOADING)
4144 0 : replace_call_with_value (gsi, integer_zero_node);
4145 : else
4146 : {
4147 : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
4148 : gcall *repl = gimple_build_call (fn, 0);
4149 : replace_call_with_call_and_fold (gsi, repl);
4150 : }
4151 : #endif
4152 103 : return true;
4153 : }
4154 :
4155 : static bool
4156 309 : gimple_fold_builtin_omp_get_num_devices (gimple_stmt_iterator *gsi)
4157 : {
4158 309 : if (!ENABLE_OFFLOADING)
4159 : {
4160 0 : replace_call_with_value (gsi, integer_zero_node);
4161 309 : return true;
4162 : }
4163 : return false;
4164 : }
4165 :
4166 : /* Fold a call to __builtin_acc_on_device. */
4167 :
4168 : static bool
4169 2866 : gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
4170 : {
4171 : /* Defer folding until we know which compiler we're in. */
4172 2866 : if (symtab->state != EXPANSION)
4173 : return false;
4174 :
4175 554 : unsigned val_host = GOMP_DEVICE_HOST;
4176 554 : unsigned val_dev = GOMP_DEVICE_NONE;
4177 :
4178 : #ifdef ACCEL_COMPILER
4179 : val_host = GOMP_DEVICE_NOT_HOST;
4180 : val_dev = ACCEL_COMPILER_acc_device;
4181 : #endif
4182 :
4183 554 : location_t loc = gimple_location (gsi_stmt (*gsi));
4184 :
4185 554 : tree host_eq = make_ssa_name (boolean_type_node);
4186 554 : gimple *host_ass = gimple_build_assign
4187 554 : (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
4188 554 : gimple_set_location (host_ass, loc);
4189 554 : gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
4190 :
4191 554 : tree dev_eq = make_ssa_name (boolean_type_node);
4192 554 : gimple *dev_ass = gimple_build_assign
4193 554 : (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
4194 554 : gimple_set_location (dev_ass, loc);
4195 554 : gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
4196 :
4197 554 : tree result = make_ssa_name (boolean_type_node);
4198 554 : gimple *result_ass = gimple_build_assign
4199 554 : (result, BIT_IOR_EXPR, host_eq, dev_eq);
4200 554 : gimple_set_location (result_ass, loc);
4201 554 : gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
4202 :
4203 554 : replace_call_with_value (gsi, result);
4204 :
4205 554 : return true;
4206 : }
4207 :
4208 : /* Fold realloc (0, n) -> malloc (n). */
4209 :
4210 : static bool
4211 49353 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
4212 : {
4213 49353 : gimple *stmt = gsi_stmt (*gsi);
4214 49353 : tree arg = gimple_call_arg (stmt, 0);
4215 49353 : tree size = gimple_call_arg (stmt, 1);
4216 :
4217 146671 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
4218 : return false;
4219 :
4220 49353 : if (operand_equal_p (arg, null_pointer_node, 0))
4221 : {
4222 1388 : tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
4223 1388 : if (fn_malloc)
4224 : {
4225 1388 : gcall *repl = gimple_build_call (fn_malloc, 1, size);
4226 1388 : replace_call_with_call_and_fold (gsi, repl);
4227 1388 : return true;
4228 : }
4229 : }
4230 : return false;
4231 : }
4232 :
4233 : /* Number of bytes into which any type but aggregate, vector or
4234 : _BitInt types should fit. */
4235 : static constexpr size_t clear_padding_unit
4236 : = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT;
4237 : /* Buffer size on which __builtin_clear_padding folding code works. */
4238 : static const size_t clear_padding_buf_size = 32 * clear_padding_unit;
4239 :
4240 : /* Data passed through __builtin_clear_padding folding. */
4241 : struct clear_padding_struct {
4242 : location_t loc;
4243 : /* 0 during __builtin_clear_padding folding, nonzero during
4244 : clear_type_padding_in_mask. In that case, instead of clearing the
4245 : non-padding bits in union_ptr array clear the padding bits in there. */
4246 : bool clear_in_mask;
4247 : tree base;
4248 : tree alias_type;
4249 : gimple_stmt_iterator *gsi;
4250 : /* Alignment of buf->base + 0. */
4251 : unsigned align;
4252 : /* Offset from buf->base. Should be always a multiple of UNITS_PER_WORD. */
4253 : HOST_WIDE_INT off;
4254 : /* Number of padding bytes before buf->off that don't have padding clear
4255 : code emitted yet. */
4256 : HOST_WIDE_INT padding_bytes;
4257 : /* The size of the whole object. Never emit code to touch
4258 : buf->base + buf->sz or following bytes. */
4259 : HOST_WIDE_INT sz;
4260 : /* Number of bytes recorded in buf->buf. */
4261 : size_t size;
4262 : /* When inside union, instead of emitting code we and bits inside of
4263 : the union_ptr array. */
4264 : unsigned char *union_ptr;
4265 : /* Set bits mean padding bits that need to be cleared by the builtin. */
4266 : unsigned char buf[clear_padding_buf_size + clear_padding_unit];
4267 : };
4268 :
4269 : /* Emit code to clear padding requested in BUF->buf - set bits
4270 : in there stand for padding that should be cleared. FULL is true
4271 : if everything from the buffer should be flushed, otherwise
4272 : it can leave up to 2 * clear_padding_unit bytes for further
4273 : processing. */
4274 :
4275 : static void
4276 35436 : clear_padding_flush (clear_padding_struct *buf, bool full)
4277 : {
4278 35436 : gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
4279 35436 : if (!full && buf->size < 2 * clear_padding_unit)
4280 35436 : return;
4281 36484 : gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
4282 35394 : size_t end = buf->size;
4283 35394 : if (!full)
4284 42 : end = ((end - clear_padding_unit - 1) / clear_padding_unit
4285 : * clear_padding_unit);
4286 35394 : size_t padding_bytes = buf->padding_bytes;
4287 35394 : if (buf->union_ptr)
4288 : {
4289 34628 : if (buf->clear_in_mask)
4290 : {
4291 : /* During clear_type_padding_in_mask, clear the padding
4292 : bits set in buf->buf in the buf->union_ptr mask. */
4293 239282 : for (size_t i = 0; i < end; i++)
4294 : {
4295 205047 : if (buf->buf[i] == (unsigned char) ~0)
4296 9036 : padding_bytes++;
4297 : else
4298 : {
4299 196011 : memset (&buf->union_ptr[buf->off + i - padding_bytes],
4300 : 0, padding_bytes);
4301 196011 : padding_bytes = 0;
4302 196011 : buf->union_ptr[buf->off + i] &= ~buf->buf[i];
4303 : }
4304 : }
4305 34235 : if (full)
4306 : {
4307 34235 : memset (&buf->union_ptr[buf->off + end - padding_bytes],
4308 : 0, padding_bytes);
4309 34235 : buf->off = 0;
4310 34235 : buf->size = 0;
4311 34235 : buf->padding_bytes = 0;
4312 : }
4313 : else
4314 : {
4315 0 : memmove (buf->buf, buf->buf + end, buf->size - end);
4316 0 : buf->off += end;
4317 0 : buf->size -= end;
4318 0 : buf->padding_bytes = padding_bytes;
4319 : }
4320 34235 : return;
4321 : }
4322 : /* Inside of a union, instead of emitting any code, instead
4323 : clear all bits in the union_ptr buffer that are clear
4324 : in buf. Whole padding bytes don't clear anything. */
4325 3017 : for (size_t i = 0; i < end; i++)
4326 : {
4327 2624 : if (buf->buf[i] == (unsigned char) ~0)
4328 1424 : padding_bytes++;
4329 : else
4330 : {
4331 1200 : padding_bytes = 0;
4332 1200 : buf->union_ptr[buf->off + i] &= buf->buf[i];
4333 : }
4334 : }
4335 393 : if (full)
4336 : {
4337 393 : buf->off = 0;
4338 393 : buf->size = 0;
4339 393 : buf->padding_bytes = 0;
4340 : }
4341 : else
4342 : {
4343 0 : memmove (buf->buf, buf->buf + end, buf->size - end);
4344 0 : buf->off += end;
4345 0 : buf->size -= end;
4346 0 : buf->padding_bytes = padding_bytes;
4347 : }
4348 393 : return;
4349 : }
4350 766 : size_t wordsize = UNITS_PER_WORD;
4351 23505 : for (size_t i = 0; i < end; i += wordsize)
4352 : {
4353 22739 : size_t nonzero_first = wordsize;
4354 22739 : size_t nonzero_last = 0;
4355 22739 : size_t zero_first = wordsize;
4356 22739 : size_t zero_last = 0;
4357 22739 : bool all_ones = true, bytes_only = true;
4358 23025 : if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
4359 22739 : > (unsigned HOST_WIDE_INT) buf->sz)
4360 : {
4361 286 : gcc_assert (wordsize > 1);
4362 286 : wordsize /= 2;
4363 286 : i -= wordsize;
4364 286 : continue;
4365 : }
4366 22453 : size_t endsize = end - i > wordsize ? wordsize : end - i;
4367 200832 : for (size_t j = i; j < i + endsize; j++)
4368 : {
4369 178379 : if (buf->buf[j])
4370 : {
4371 168378 : if (nonzero_first == wordsize)
4372 : {
4373 21511 : nonzero_first = j - i;
4374 21511 : nonzero_last = j - i;
4375 : }
4376 168378 : if (nonzero_last != j - i)
4377 158 : all_ones = false;
4378 168378 : nonzero_last = j + 1 - i;
4379 : }
4380 : else
4381 : {
4382 10001 : if (zero_first == wordsize)
4383 1938 : zero_first = j - i;
4384 10001 : zero_last = j + 1 - i;
4385 : }
4386 178379 : if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
4387 : {
4388 85 : all_ones = false;
4389 85 : bytes_only = false;
4390 : }
4391 : }
4392 22453 : size_t padding_end = i;
4393 22453 : if (padding_bytes)
4394 : {
4395 20849 : if (nonzero_first == 0
4396 20849 : && nonzero_last == endsize
4397 20400 : && all_ones)
4398 : {
4399 : /* All bits are padding and we had some padding
4400 : before too. Just extend it. */
4401 20400 : padding_bytes += endsize;
4402 20400 : continue;
4403 : }
4404 449 : if (all_ones && nonzero_first == 0)
4405 : {
4406 4 : padding_bytes += nonzero_last;
4407 4 : padding_end += nonzero_last;
4408 4 : nonzero_first = wordsize;
4409 4 : nonzero_last = 0;
4410 : }
4411 445 : else if (bytes_only && nonzero_first == 0)
4412 : {
4413 0 : gcc_assert (zero_first && zero_first != wordsize);
4414 0 : padding_bytes += zero_first;
4415 0 : padding_end += zero_first;
4416 : }
4417 449 : tree atype, src;
4418 449 : if (padding_bytes == 1)
4419 : {
4420 33 : atype = char_type_node;
4421 33 : src = build_zero_cst (char_type_node);
4422 : }
4423 : else
4424 : {
4425 416 : atype = build_array_type_nelts (char_type_node, padding_bytes);
4426 416 : src = build_constructor (atype, NULL);
4427 : }
4428 449 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4429 : build_int_cst (buf->alias_type,
4430 449 : buf->off + padding_end
4431 449 : - padding_bytes));
4432 449 : gimple *g = gimple_build_assign (dst, src);
4433 449 : gimple_set_location (g, buf->loc);
4434 449 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4435 449 : padding_bytes = 0;
4436 449 : buf->padding_bytes = 0;
4437 : }
4438 2053 : if (nonzero_first == wordsize)
4439 : /* All bits in a word are 0, there are no padding bits. */
4440 946 : continue;
4441 1107 : if (all_ones && nonzero_last == endsize)
4442 : {
4443 : /* All bits between nonzero_first and end of word are padding
4444 : bits, start counting padding_bytes. */
4445 841 : padding_bytes = nonzero_last - nonzero_first;
4446 841 : continue;
4447 : }
4448 266 : if (bytes_only)
4449 : {
4450 : /* If bitfields aren't involved in this word, prefer storing
4451 : individual bytes or groups of them over performing a RMW
4452 : operation on the whole word. */
4453 227 : gcc_assert (i + zero_last <= end);
4454 1117 : for (size_t j = padding_end; j < i + zero_last; j++)
4455 : {
4456 890 : if (buf->buf[j])
4457 : {
4458 : size_t k;
4459 606 : for (k = j; k < i + zero_last; k++)
4460 606 : if (buf->buf[k] == 0)
4461 : break;
4462 259 : HOST_WIDE_INT off = buf->off + j;
4463 259 : tree atype, src;
4464 259 : if (k - j == 1)
4465 : {
4466 215 : atype = char_type_node;
4467 215 : src = build_zero_cst (char_type_node);
4468 : }
4469 : else
4470 : {
4471 44 : atype = build_array_type_nelts (char_type_node, k - j);
4472 44 : src = build_constructor (atype, NULL);
4473 : }
4474 259 : tree dst = build2_loc (buf->loc, MEM_REF, atype,
4475 : buf->base,
4476 259 : build_int_cst (buf->alias_type, off));
4477 259 : gimple *g = gimple_build_assign (dst, src);
4478 259 : gimple_set_location (g, buf->loc);
4479 259 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4480 259 : j = k;
4481 : }
4482 : }
4483 227 : if (nonzero_last == endsize)
4484 98 : padding_bytes = nonzero_last - zero_last;
4485 227 : continue;
4486 227 : }
4487 126 : for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
4488 : {
4489 126 : if (nonzero_last - nonzero_first <= eltsz
4490 39 : && ((nonzero_first & ~(eltsz - 1))
4491 39 : == ((nonzero_last - 1) & ~(eltsz - 1))))
4492 : {
4493 39 : tree type;
4494 39 : if (eltsz == 1)
4495 2 : type = char_type_node;
4496 : else
4497 37 : type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
4498 : 0);
4499 39 : size_t start = nonzero_first & ~(eltsz - 1);
4500 39 : HOST_WIDE_INT off = buf->off + i + start;
4501 39 : tree atype = type;
4502 39 : if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
4503 0 : atype = build_aligned_type (type, buf->align);
4504 39 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4505 39 : build_int_cst (buf->alias_type, off));
4506 39 : tree src;
4507 39 : gimple *g;
4508 39 : if (all_ones
4509 39 : && nonzero_first == start
4510 0 : && nonzero_last == start + eltsz)
4511 0 : src = build_zero_cst (type);
4512 : else
4513 : {
4514 39 : src = make_ssa_name (type);
4515 39 : tree tmp_dst = unshare_expr (dst);
4516 : /* The folding introduces a read from the tmp_dst, we should
4517 : prevent uninitialized warning analysis from issuing warning
4518 : for such fake read. In order to suppress warning only for
4519 : this expr, we should set the location of tmp_dst to
4520 : UNKNOWN_LOCATION first, then suppress_warning will call
4521 : set_no_warning_bit to set the no_warning flag only for
4522 : tmp_dst. */
4523 39 : SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
4524 39 : suppress_warning (tmp_dst, OPT_Wuninitialized);
4525 39 : g = gimple_build_assign (src, tmp_dst);
4526 39 : gimple_set_location (g, buf->loc);
4527 39 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4528 78 : tree mask = native_interpret_expr (type,
4529 39 : buf->buf + i + start,
4530 : eltsz);
4531 39 : gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
4532 39 : mask = fold_build1 (BIT_NOT_EXPR, type, mask);
4533 39 : tree src_masked = make_ssa_name (type);
4534 39 : g = gimple_build_assign (src_masked, BIT_AND_EXPR,
4535 : src, mask);
4536 39 : gimple_set_location (g, buf->loc);
4537 39 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4538 39 : src = src_masked;
4539 : }
4540 39 : g = gimple_build_assign (dst, src);
4541 39 : gimple_set_location (g, buf->loc);
4542 39 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4543 39 : break;
4544 : }
4545 : }
4546 : }
4547 766 : if (full)
4548 : {
4549 724 : if (padding_bytes)
4550 : {
4551 490 : tree atype, src;
4552 490 : if (padding_bytes == 1)
4553 : {
4554 110 : atype = char_type_node;
4555 110 : src = build_zero_cst (char_type_node);
4556 : }
4557 : else
4558 : {
4559 380 : atype = build_array_type_nelts (char_type_node, padding_bytes);
4560 380 : src = build_constructor (atype, NULL);
4561 : }
4562 490 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4563 : build_int_cst (buf->alias_type,
4564 490 : buf->off + end
4565 490 : - padding_bytes));
4566 490 : gimple *g = gimple_build_assign (dst, src);
4567 490 : gimple_set_location (g, buf->loc);
4568 490 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4569 : }
4570 724 : size_t end_rem = end % UNITS_PER_WORD;
4571 724 : buf->off += end - end_rem;
4572 724 : buf->size = end_rem;
4573 724 : memset (buf->buf, 0, buf->size);
4574 724 : buf->padding_bytes = 0;
4575 : }
4576 : else
4577 : {
4578 42 : memmove (buf->buf, buf->buf + end, buf->size - end);
4579 42 : buf->off += end;
4580 42 : buf->size -= end;
4581 42 : buf->padding_bytes = padding_bytes;
4582 : }
4583 : }
4584 :
4585 : /* Append PADDING_BYTES padding bytes. */
4586 :
4587 : static void
4588 5215 : clear_padding_add_padding (clear_padding_struct *buf,
4589 : HOST_WIDE_INT padding_bytes)
4590 : {
4591 5215 : if (padding_bytes == 0)
4592 : return;
4593 1679 : if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
4594 : > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
4595 42 : clear_padding_flush (buf, false);
4596 1679 : if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
4597 : > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
4598 : {
4599 42 : memset (buf->buf + buf->size, ~0, clear_padding_buf_size - buf->size);
4600 42 : padding_bytes -= clear_padding_buf_size - buf->size;
4601 42 : buf->size = clear_padding_buf_size;
4602 42 : clear_padding_flush (buf, false);
4603 42 : gcc_assert (buf->padding_bytes);
4604 : /* At this point buf->buf[0] through buf->buf[buf->size - 1]
4605 : is guaranteed to be all ones. */
4606 42 : padding_bytes += buf->size;
4607 42 : buf->size = padding_bytes % UNITS_PER_WORD;
4608 42 : memset (buf->buf, ~0, buf->size);
4609 42 : buf->off += padding_bytes - buf->size;
4610 42 : buf->padding_bytes += padding_bytes - buf->size;
4611 : }
4612 : else
4613 : {
4614 1637 : memset (buf->buf + buf->size, ~0, padding_bytes);
4615 1637 : buf->size += padding_bytes;
4616 : }
4617 : }
4618 :
4619 : static void clear_padding_type (clear_padding_struct *, tree,
4620 : HOST_WIDE_INT, bool);
4621 :
4622 : /* Clear padding bits of union type TYPE. */
4623 :
4624 : static void
4625 128 : clear_padding_union (clear_padding_struct *buf, tree type,
4626 : HOST_WIDE_INT sz, bool for_auto_init)
4627 : {
4628 128 : clear_padding_struct *union_buf;
4629 128 : HOST_WIDE_INT start_off = 0, next_off = 0;
4630 128 : size_t start_size = 0;
4631 128 : if (buf->union_ptr)
4632 : {
4633 42 : start_off = buf->off + buf->size;
4634 42 : next_off = start_off + sz;
4635 42 : start_size = start_off % UNITS_PER_WORD;
4636 42 : start_off -= start_size;
4637 42 : clear_padding_flush (buf, true);
4638 42 : union_buf = buf;
4639 : }
4640 : else
4641 : {
4642 86 : if (sz + buf->size > clear_padding_buf_size)
4643 0 : clear_padding_flush (buf, false);
4644 86 : union_buf = XALLOCA (clear_padding_struct);
4645 86 : union_buf->loc = buf->loc;
4646 86 : union_buf->clear_in_mask = buf->clear_in_mask;
4647 86 : union_buf->base = NULL_TREE;
4648 86 : union_buf->alias_type = NULL_TREE;
4649 86 : union_buf->gsi = NULL;
4650 86 : union_buf->align = 0;
4651 86 : union_buf->off = 0;
4652 86 : union_buf->padding_bytes = 0;
4653 86 : union_buf->sz = sz;
4654 86 : union_buf->size = 0;
4655 86 : if (sz + buf->size <= clear_padding_buf_size)
4656 86 : union_buf->union_ptr = buf->buf + buf->size;
4657 : else
4658 0 : union_buf->union_ptr = XNEWVEC (unsigned char, sz);
4659 86 : memset (union_buf->union_ptr, ~0, sz);
4660 : }
4661 :
4662 1193 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4663 1065 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4664 : {
4665 359 : if (DECL_SIZE_UNIT (field) == NULL_TREE)
4666 : {
4667 8 : if (TREE_TYPE (field) == error_mark_node)
4668 0 : continue;
4669 8 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4670 : && !COMPLETE_TYPE_P (TREE_TYPE (field)));
4671 8 : if (!buf->clear_in_mask && !for_auto_init)
4672 8 : error_at (buf->loc, "flexible array member %qD does not have "
4673 : "well defined padding bits for %qs",
4674 : field, "__builtin_clear_padding");
4675 8 : continue;
4676 : }
4677 351 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4678 351 : gcc_assert (union_buf->size == 0);
4679 351 : union_buf->off = start_off;
4680 351 : union_buf->size = start_size;
4681 351 : memset (union_buf->buf, ~0, start_size);
4682 351 : clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init);
4683 351 : clear_padding_add_padding (union_buf, sz - fldsz);
4684 351 : clear_padding_flush (union_buf, true);
4685 : }
4686 :
4687 128 : if (buf == union_buf)
4688 : {
4689 42 : buf->off = next_off;
4690 42 : buf->size = next_off % UNITS_PER_WORD;
4691 42 : buf->off -= buf->size;
4692 42 : memset (buf->buf, ~0, buf->size);
4693 : }
4694 86 : else if (sz + buf->size <= clear_padding_buf_size)
4695 86 : buf->size += sz;
4696 : else
4697 : {
4698 0 : unsigned char *union_ptr = union_buf->union_ptr;
4699 0 : while (sz)
4700 : {
4701 0 : clear_padding_flush (buf, false);
4702 0 : HOST_WIDE_INT this_sz
4703 0 : = MIN ((unsigned HOST_WIDE_INT) sz,
4704 : clear_padding_buf_size - buf->size);
4705 0 : memcpy (buf->buf + buf->size, union_ptr, this_sz);
4706 0 : buf->size += this_sz;
4707 0 : union_ptr += this_sz;
4708 0 : sz -= this_sz;
4709 : }
4710 0 : XDELETE (union_buf->union_ptr);
4711 : }
4712 128 : }
4713 :
4714 : /* The only known floating point formats with padding bits are the
4715 : IEEE extended ones. */
4716 :
4717 : static bool
4718 36020 : clear_padding_real_needs_padding_p (tree type)
4719 : {
4720 36020 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
4721 36020 : return (fmt->b == 2
4722 35409 : && fmt->signbit_ro == fmt->signbit_rw
4723 71429 : && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95));
4724 : }
4725 :
4726 : /* _BitInt has padding bits if it isn't extended in the ABI and has smaller
4727 : precision than bits in limb or corresponding number of limbs. */
4728 :
4729 : static bool
4730 54 : clear_padding_bitint_needs_padding_p (tree type)
4731 : {
4732 54 : struct bitint_info info;
4733 54 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
4734 54 : gcc_assert (ok);
4735 54 : if (info.extended)
4736 : return false;
4737 54 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.abi_limb_mode);
4738 54 : if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
4739 : return true;
4740 4 : else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode))
4741 : return false;
4742 : else
4743 4 : return (((unsigned) TYPE_PRECISION (type))
4744 4 : % GET_MODE_PRECISION (limb_mode)) != 0;
4745 : }
4746 :
4747 : /* Return true if TYPE might contain any padding bits. */
4748 :
4749 : bool
4750 916845 : clear_padding_type_may_have_padding_p (tree type)
4751 : {
4752 1053244 : switch (TREE_CODE (type))
4753 : {
4754 : case RECORD_TYPE:
4755 : case UNION_TYPE:
4756 : return true;
4757 136399 : case ARRAY_TYPE:
4758 136399 : case COMPLEX_TYPE:
4759 136399 : case VECTOR_TYPE:
4760 136399 : return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
4761 1799 : case REAL_TYPE:
4762 1799 : return clear_padding_real_needs_padding_p (type);
4763 61 : case ENUMERAL_TYPE:
4764 61 : if (BITINT_TYPE_P (type))
4765 0 : return clear_padding_bitint_needs_padding_p (type);
4766 : return false;
4767 54 : case BITINT_TYPE:
4768 54 : return clear_padding_bitint_needs_padding_p (type);
4769 : default:
4770 : return false;
4771 : }
4772 : }
4773 :
4774 : /* Return true if TYPE has padding bits aside from those in fields,
4775 : elements, etc. */
4776 :
4777 : bool
4778 1174324 : type_has_padding_at_level_p (tree type)
4779 : {
4780 1174324 : switch (TREE_CODE (type))
4781 : {
4782 1035770 : case RECORD_TYPE:
4783 1035770 : {
4784 1035770 : tree bitpos = size_zero_node;
4785 : /* Expect fields to be sorted by bit position. */
4786 7667979 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4787 6636919 : if (TREE_CODE (f) == FIELD_DECL)
4788 : {
4789 2270493 : if (DECL_PADDING_P (f))
4790 : return true;
4791 2270490 : tree pos = bit_position (f);
4792 2270490 : if (simple_cst_equal (bitpos, pos) != 1)
4793 : return true;
4794 2265806 : if (!DECL_SIZE (f))
4795 : return true;
4796 2265783 : bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
4797 : }
4798 1031060 : if (simple_cst_equal (bitpos, TYPE_SIZE (type)) != 1)
4799 : return true;
4800 : return false;
4801 : }
4802 3 : case UNION_TYPE:
4803 3 : case QUAL_UNION_TYPE:
4804 3 : bool any_fields;
4805 3 : any_fields = false;
4806 : /* If any of the fields is smaller than the whole, there is padding. */
4807 6 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4808 3 : if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node)
4809 3 : continue;
4810 0 : else if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
4811 0 : TYPE_SIZE (type)) != 1)
4812 : return true;
4813 : else
4814 : any_fields = true;
4815 : /* If the union doesn't have any fields and still has non-zero size,
4816 : all of it is padding. */
4817 3 : if (!any_fields && !integer_zerop (TYPE_SIZE (type)))
4818 : return true;
4819 : return false;
4820 : case ARRAY_TYPE:
4821 : case COMPLEX_TYPE:
4822 : case VECTOR_TYPE:
4823 : /* No recursing here, no padding at this level. */
4824 : return false;
4825 0 : case REAL_TYPE:
4826 0 : return clear_padding_real_needs_padding_p (type);
4827 0 : case ENUMERAL_TYPE:
4828 0 : if (BITINT_TYPE_P (type))
4829 0 : return clear_padding_bitint_needs_padding_p (type);
4830 : return false;
4831 0 : case BITINT_TYPE:
4832 0 : return clear_padding_bitint_needs_padding_p (type);
4833 : default:
4834 : return false;
4835 : }
4836 : }
4837 :
4838 : /* Emit a runtime loop:
4839 : for (; buf.base != end; buf.base += sz)
4840 : __builtin_clear_padding (buf.base); */
4841 :
4842 : static void
4843 114 : clear_padding_emit_loop (clear_padding_struct *buf, tree type,
4844 : tree end, bool for_auto_init)
4845 : {
4846 114 : tree l1 = create_artificial_label (buf->loc);
4847 114 : tree l2 = create_artificial_label (buf->loc);
4848 114 : tree l3 = create_artificial_label (buf->loc);
4849 114 : gimple *g = gimple_build_goto (l2);
4850 114 : gimple_set_location (g, buf->loc);
4851 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4852 114 : g = gimple_build_label (l1);
4853 114 : gimple_set_location (g, buf->loc);
4854 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4855 114 : clear_padding_type (buf, type, buf->sz, for_auto_init);
4856 114 : clear_padding_flush (buf, true);
4857 114 : g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR, buf->base,
4858 114 : size_int (buf->sz));
4859 114 : gimple_set_location (g, buf->loc);
4860 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4861 114 : g = gimple_build_label (l2);
4862 114 : gimple_set_location (g, buf->loc);
4863 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4864 114 : g = gimple_build_cond (NE_EXPR, buf->base, end, l1, l3);
4865 114 : gimple_set_location (g, buf->loc);
4866 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4867 114 : g = gimple_build_label (l3);
4868 114 : gimple_set_location (g, buf->loc);
4869 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4870 114 : }
4871 :
4872 : /* Clear padding bits for TYPE. Called recursively from
4873 : gimple_fold_builtin_clear_padding. If FOR_AUTO_INIT is true,
4874 : the __builtin_clear_padding is not called by the end user,
4875 : instead, it's inserted by the compiler to initialize the
4876 : paddings of automatic variable. Therefore, we should not
4877 : emit the error messages for flexible array members to confuse
4878 : the end user. */
4879 :
4880 : static void
4881 39505 : clear_padding_type (clear_padding_struct *buf, tree type,
4882 : HOST_WIDE_INT sz, bool for_auto_init)
4883 : {
4884 39505 : switch (TREE_CODE (type))
4885 : {
4886 1363 : case RECORD_TYPE:
4887 1363 : HOST_WIDE_INT cur_pos;
4888 1363 : cur_pos = 0;
4889 23099 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4890 21736 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4891 : {
4892 3814 : tree ftype = TREE_TYPE (field);
4893 3814 : if (DECL_BIT_FIELD (field))
4894 : {
4895 260 : HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
4896 260 : if (fldsz == 0)
4897 0 : continue;
4898 260 : HOST_WIDE_INT pos = int_byte_position (field);
4899 260 : if (pos >= sz)
4900 0 : continue;
4901 260 : HOST_WIDE_INT bpos
4902 260 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
4903 260 : bpos %= BITS_PER_UNIT;
4904 260 : HOST_WIDE_INT end
4905 260 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4906 260 : if (pos + end > cur_pos)
4907 : {
4908 199 : clear_padding_add_padding (buf, pos + end - cur_pos);
4909 199 : cur_pos = pos + end;
4910 : }
4911 260 : gcc_assert (cur_pos > pos
4912 : && ((unsigned HOST_WIDE_INT) buf->size
4913 : >= (unsigned HOST_WIDE_INT) cur_pos - pos));
4914 260 : unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
4915 260 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4916 : sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
4917 : " in %qs", "__builtin_clear_padding");
4918 260 : else if (BYTES_BIG_ENDIAN)
4919 : {
4920 : /* Big endian. */
4921 : if (bpos + fldsz <= BITS_PER_UNIT)
4922 : *p &= ~(((1 << fldsz) - 1)
4923 : << (BITS_PER_UNIT - bpos - fldsz));
4924 : else
4925 : {
4926 : if (bpos)
4927 : {
4928 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4929 : p++;
4930 : fldsz -= BITS_PER_UNIT - bpos;
4931 : }
4932 : memset (p, 0, fldsz / BITS_PER_UNIT);
4933 : p += fldsz / BITS_PER_UNIT;
4934 : fldsz %= BITS_PER_UNIT;
4935 : if (fldsz)
4936 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4937 : }
4938 : }
4939 : else
4940 : {
4941 : /* Little endian. */
4942 260 : if (bpos + fldsz <= BITS_PER_UNIT)
4943 159 : *p &= ~(((1 << fldsz) - 1) << bpos);
4944 : else
4945 : {
4946 101 : if (bpos)
4947 : {
4948 33 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4949 33 : p++;
4950 33 : fldsz -= BITS_PER_UNIT - bpos;
4951 : }
4952 101 : memset (p, 0, fldsz / BITS_PER_UNIT);
4953 101 : p += fldsz / BITS_PER_UNIT;
4954 101 : fldsz %= BITS_PER_UNIT;
4955 101 : if (fldsz)
4956 56 : *p &= ~((1 << fldsz) - 1);
4957 : }
4958 : }
4959 : }
4960 3554 : else if (DECL_SIZE_UNIT (field) == NULL_TREE)
4961 : {
4962 32 : if (ftype == error_mark_node)
4963 0 : continue;
4964 32 : gcc_assert (TREE_CODE (ftype) == ARRAY_TYPE
4965 : && !COMPLETE_TYPE_P (ftype));
4966 32 : if (!buf->clear_in_mask && !for_auto_init)
4967 24 : error_at (buf->loc, "flexible array member %qD does not "
4968 : "have well defined padding bits for %qs",
4969 : field, "__builtin_clear_padding");
4970 : }
4971 3522 : else if (is_empty_type (ftype))
4972 220 : continue;
4973 : else
4974 : {
4975 3302 : HOST_WIDE_INT pos = int_byte_position (field);
4976 3302 : if (pos >= sz)
4977 0 : continue;
4978 3302 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4979 3302 : gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
4980 3302 : clear_padding_add_padding (buf, pos - cur_pos);
4981 3302 : cur_pos = pos;
4982 3302 : if (tree asbase = lang_hooks.types.classtype_as_base (field))
4983 240 : ftype = asbase;
4984 3302 : clear_padding_type (buf, ftype, fldsz, for_auto_init);
4985 3302 : cur_pos += fldsz;
4986 : }
4987 : }
4988 1363 : gcc_assert (sz >= cur_pos);
4989 1363 : clear_padding_add_padding (buf, sz - cur_pos);
4990 1363 : break;
4991 325 : case ARRAY_TYPE:
4992 325 : HOST_WIDE_INT nelts, fldsz;
4993 325 : fldsz = int_size_in_bytes (TREE_TYPE (type));
4994 325 : if (fldsz == 0)
4995 : break;
4996 311 : nelts = sz / fldsz;
4997 311 : if (nelts > 1
4998 304 : && sz > 8 * UNITS_PER_WORD
4999 78 : && buf->union_ptr == NULL
5000 389 : && clear_padding_type_may_have_padding_p (TREE_TYPE (type)))
5001 : {
5002 : /* For sufficiently large array of more than one elements,
5003 : emit a runtime loop to keep code size manageable. */
5004 66 : tree base = buf->base;
5005 66 : unsigned int prev_align = buf->align;
5006 66 : HOST_WIDE_INT off = buf->off + buf->size;
5007 66 : HOST_WIDE_INT prev_sz = buf->sz;
5008 66 : clear_padding_flush (buf, true);
5009 66 : tree elttype = TREE_TYPE (type);
5010 66 : buf->base = create_tmp_var (build_pointer_type (elttype));
5011 66 : tree end = make_ssa_name (TREE_TYPE (buf->base));
5012 66 : gimple *g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR,
5013 66 : base, size_int (off));
5014 66 : gimple_set_location (g, buf->loc);
5015 66 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
5016 66 : g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf->base,
5017 66 : size_int (sz));
5018 66 : gimple_set_location (g, buf->loc);
5019 66 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
5020 66 : buf->sz = fldsz;
5021 66 : buf->align = TYPE_ALIGN (elttype);
5022 66 : buf->off = 0;
5023 66 : buf->size = 0;
5024 66 : clear_padding_emit_loop (buf, elttype, end, for_auto_init);
5025 66 : off += sz;
5026 66 : buf->base = base;
5027 66 : buf->sz = prev_sz;
5028 66 : buf->align = prev_align;
5029 66 : buf->size = off % UNITS_PER_WORD;
5030 66 : buf->off = off - buf->size;
5031 66 : memset (buf->buf, 0, buf->size);
5032 66 : break;
5033 : }
5034 1163 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5035 918 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5036 : break;
5037 128 : case UNION_TYPE:
5038 128 : clear_padding_union (buf, type, sz, for_auto_init);
5039 128 : break;
5040 34221 : case REAL_TYPE:
5041 34221 : gcc_assert ((size_t) sz <= clear_padding_unit);
5042 34221 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5043 0 : clear_padding_flush (buf, false);
5044 34221 : if (clear_padding_real_needs_padding_p (type))
5045 : {
5046 : /* Use native_interpret_real + native_encode_expr to figure out
5047 : which bits are padding. */
5048 1587 : memset (buf->buf + buf->size, ~0, sz);
5049 1587 : tree cst = native_interpret_real (type, buf->buf + buf->size, sz);
5050 1587 : gcc_assert (cst && TREE_CODE (cst) == REAL_CST);
5051 1587 : int len = native_encode_expr (cst, buf->buf + buf->size, sz);
5052 1587 : gcc_assert (len > 0 && (size_t) len == (size_t) sz);
5053 26979 : for (size_t i = 0; i < (size_t) sz; i++)
5054 25392 : buf->buf[buf->size + i] ^= ~0;
5055 : }
5056 : else
5057 32634 : memset (buf->buf + buf->size, 0, sz);
5058 34221 : buf->size += sz;
5059 34221 : break;
5060 0 : case COMPLEX_TYPE:
5061 0 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5062 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5063 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5064 0 : break;
5065 8 : case VECTOR_TYPE:
5066 8 : nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
5067 8 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5068 40 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5069 32 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5070 : break;
5071 7 : case NULLPTR_TYPE:
5072 7 : gcc_assert ((size_t) sz <= clear_padding_unit);
5073 7 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5074 0 : clear_padding_flush (buf, false);
5075 7 : memset (buf->buf + buf->size, ~0, sz);
5076 7 : buf->size += sz;
5077 7 : break;
5078 4 : case BITINT_TYPE:
5079 4 : do_bitint:
5080 4 : {
5081 4 : struct bitint_info info;
5082 4 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
5083 4 : gcc_assert (ok);
5084 4 : scalar_int_mode limb_mode
5085 4 : = as_a <scalar_int_mode> (info.abi_limb_mode);
5086 4 : if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
5087 : {
5088 2 : gcc_assert ((size_t) sz <= clear_padding_unit);
5089 2 : if ((unsigned HOST_WIDE_INT) sz + buf->size
5090 : > clear_padding_buf_size)
5091 0 : clear_padding_flush (buf, false);
5092 2 : if (!info.extended
5093 2 : && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
5094 : {
5095 2 : int tprec = GET_MODE_PRECISION (limb_mode);
5096 2 : int prec = TYPE_PRECISION (type);
5097 2 : tree t = build_nonstandard_integer_type (tprec, 1);
5098 2 : tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec));
5099 2 : int len = native_encode_expr (cst, buf->buf + buf->size, sz);
5100 2 : gcc_assert (len > 0 && (size_t) len == (size_t) sz);
5101 : }
5102 : else
5103 0 : memset (buf->buf + buf->size, 0, sz);
5104 2 : buf->size += sz;
5105 2 : break;
5106 : }
5107 2 : tree limbtype
5108 2 : = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1);
5109 2 : fldsz = int_size_in_bytes (limbtype);
5110 2 : nelts = int_size_in_bytes (type) / fldsz;
5111 13 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5112 : {
5113 11 : if (!info.extended
5114 11 : && i == (info.big_endian ? 0 : nelts - 1)
5115 13 : && (((unsigned) TYPE_PRECISION (type))
5116 2 : % TYPE_PRECISION (limbtype)) != 0)
5117 : {
5118 2 : int tprec = GET_MODE_PRECISION (limb_mode);
5119 2 : int prec = (((unsigned) TYPE_PRECISION (type)) % tprec);
5120 2 : tree cst = wide_int_to_tree (limbtype,
5121 2 : wi::mask (prec, true, tprec));
5122 2 : int len = native_encode_expr (cst, buf->buf + buf->size,
5123 : fldsz);
5124 2 : gcc_assert (len > 0 && (size_t) len == (size_t) fldsz);
5125 2 : buf->size += fldsz;
5126 : }
5127 : else
5128 9 : clear_padding_type (buf, limbtype, fldsz, for_auto_init);
5129 : }
5130 : break;
5131 : }
5132 0 : case ENUMERAL_TYPE:
5133 0 : if (BITINT_TYPE_P (type))
5134 0 : goto do_bitint;
5135 : /* FALLTHRU */
5136 3449 : default:
5137 3449 : gcc_assert ((size_t) sz <= clear_padding_unit);
5138 3449 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5139 0 : clear_padding_flush (buf, false);
5140 3449 : memset (buf->buf + buf->size, 0, sz);
5141 3449 : buf->size += sz;
5142 3449 : break;
5143 : }
5144 39505 : }
5145 :
5146 : /* Clear padding bits of TYPE in MASK. */
5147 :
5148 : void
5149 34235 : clear_type_padding_in_mask (tree type, unsigned char *mask)
5150 : {
5151 34235 : clear_padding_struct buf;
5152 34235 : buf.loc = UNKNOWN_LOCATION;
5153 34235 : buf.clear_in_mask = true;
5154 34235 : buf.base = NULL_TREE;
5155 34235 : buf.alias_type = NULL_TREE;
5156 34235 : buf.gsi = NULL;
5157 34235 : buf.align = 0;
5158 34235 : buf.off = 0;
5159 34235 : buf.padding_bytes = 0;
5160 34235 : buf.sz = int_size_in_bytes (type);
5161 34235 : buf.size = 0;
5162 34235 : buf.union_ptr = mask;
5163 34235 : clear_padding_type (&buf, type, buf.sz, false);
5164 34235 : clear_padding_flush (&buf, true);
5165 34235 : }
5166 :
5167 : /* Fold __builtin_clear_padding builtin. */
5168 :
5169 : static bool
5170 630 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
5171 : {
5172 630 : gimple *stmt = gsi_stmt (*gsi);
5173 630 : gcc_assert (gimple_call_num_args (stmt) == 2);
5174 630 : tree ptr = gimple_call_arg (stmt, 0);
5175 630 : tree typearg = gimple_call_arg (stmt, 1);
5176 : /* The 2nd argument of __builtin_clear_padding's value is used to
5177 : distinguish whether this call is made by the user or by the compiler
5178 : for automatic variable initialization. */
5179 630 : bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
5180 630 : tree type = TREE_TYPE (TREE_TYPE (typearg));
5181 630 : location_t loc = gimple_location (stmt);
5182 630 : clear_padding_struct buf;
5183 630 : gimple_stmt_iterator gsiprev = *gsi;
5184 : /* This should be folded during the lower pass. */
5185 1260 : gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
5186 630 : gcc_assert (COMPLETE_TYPE_P (type));
5187 630 : gsi_prev (&gsiprev);
5188 :
5189 630 : buf.loc = loc;
5190 630 : buf.clear_in_mask = false;
5191 630 : buf.base = ptr;
5192 630 : buf.alias_type = NULL_TREE;
5193 630 : buf.gsi = gsi;
5194 630 : buf.align = get_pointer_alignment (ptr);
5195 630 : unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
5196 630 : buf.align = MAX (buf.align, talign);
5197 630 : buf.off = 0;
5198 630 : buf.padding_bytes = 0;
5199 630 : buf.size = 0;
5200 630 : buf.sz = int_size_in_bytes (type);
5201 630 : buf.union_ptr = NULL;
5202 630 : if (buf.sz < 0 && int_size_in_bytes (strip_array_types (type)) < 0)
5203 1 : sorry_at (loc, "%s not supported for variable length aggregates",
5204 : "__builtin_clear_padding");
5205 : /* The implementation currently assumes 8-bit host and target
5206 : chars which is the case for all currently supported targets
5207 : and hosts and is required e.g. for native_{encode,interpret}* APIs. */
5208 629 : else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
5209 : sorry_at (loc, "%s not supported on this target",
5210 : "__builtin_clear_padding");
5211 629 : else if (!clear_padding_type_may_have_padding_p (type))
5212 : ;
5213 592 : else if (TREE_CODE (type) == ARRAY_TYPE && buf.sz < 0)
5214 : {
5215 48 : tree sz = TYPE_SIZE_UNIT (type);
5216 48 : tree elttype = type;
5217 : /* Only supports C/C++ VLAs and flattens all the VLA levels. */
5218 48 : while (TREE_CODE (elttype) == ARRAY_TYPE
5219 144 : && int_size_in_bytes (elttype) < 0)
5220 96 : elttype = TREE_TYPE (elttype);
5221 48 : HOST_WIDE_INT eltsz = int_size_in_bytes (elttype);
5222 48 : gcc_assert (eltsz >= 0);
5223 48 : if (eltsz)
5224 : {
5225 48 : buf.base = create_tmp_var (build_pointer_type (elttype));
5226 48 : tree end = make_ssa_name (TREE_TYPE (buf.base));
5227 48 : gimple *g = gimple_build_assign (buf.base, ptr);
5228 48 : gimple_set_location (g, loc);
5229 48 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5230 48 : g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf.base, sz);
5231 48 : gimple_set_location (g, loc);
5232 48 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5233 48 : buf.sz = eltsz;
5234 48 : buf.align = TYPE_ALIGN (elttype);
5235 48 : buf.alias_type = build_pointer_type (elttype);
5236 48 : clear_padding_emit_loop (&buf, elttype, end, for_auto_init);
5237 : }
5238 : }
5239 : else
5240 : {
5241 544 : if (!is_gimple_mem_ref_addr (buf.base))
5242 : {
5243 28 : buf.base = make_ssa_name (TREE_TYPE (ptr));
5244 28 : gimple *g = gimple_build_assign (buf.base, ptr);
5245 28 : gimple_set_location (g, loc);
5246 28 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5247 : }
5248 544 : buf.alias_type = build_pointer_type (type);
5249 544 : clear_padding_type (&buf, type, buf.sz, for_auto_init);
5250 544 : clear_padding_flush (&buf, true);
5251 : }
5252 :
5253 630 : gimple_stmt_iterator gsiprev2 = *gsi;
5254 630 : gsi_prev (&gsiprev2);
5255 630 : if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
5256 126 : gsi_replace (gsi, gimple_build_nop (), true);
5257 : else
5258 : {
5259 504 : gsi_remove (gsi, true);
5260 504 : *gsi = gsiprev2;
5261 : }
5262 630 : return true;
5263 : }
5264 :
5265 : /* Fold __builtin_constant_p builtin. */
5266 :
5267 : static bool
5268 95035 : gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
5269 : {
5270 95035 : gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
5271 :
5272 95035 : if (gimple_call_num_args (call) != 1)
5273 : return false;
5274 :
5275 95030 : tree arg = gimple_call_arg (call, 0);
5276 95030 : tree result = fold_builtin_constant_p (arg);
5277 :
5278 : /* Resolve __builtin_constant_p. If it hasn't been
5279 : folded to integer_one_node by now, it's fairly
5280 : certain that the value simply isn't constant. */
5281 188990 : if (!result && fold_before_rtl_expansion_p ())
5282 3 : result = integer_zero_node;
5283 :
5284 95030 : if (!result)
5285 : return false;
5286 :
5287 1073 : gimplify_and_update_call_from_tree (gsi, result);
5288 1073 : return true;
5289 : }
5290 :
5291 : /* If va_list type is a simple pointer and nothing special is needed,
5292 : optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
5293 : __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
5294 : pointer assignment. Returns true if a change happened. */
5295 :
5296 : static bool
5297 115065 : gimple_fold_builtin_stdarg (gimple_stmt_iterator *gsi, gcall *call)
5298 : {
5299 : /* These shouldn't be folded before pass_stdarg. */
5300 115065 : if (!fold_before_rtl_expansion_p ())
5301 : return false;
5302 :
5303 10829 : tree callee, lhs, rhs, cfun_va_list;
5304 10829 : bool va_list_simple_ptr;
5305 10829 : location_t loc = gimple_location (call);
5306 10829 : gimple *nstmt0, *nstmt;
5307 10829 : tree tlhs, oldvdef, newvdef;
5308 :
5309 10829 : callee = gimple_call_fndecl (call);
5310 :
5311 10829 : cfun_va_list = targetm.fn_abi_va_list (callee);
5312 21658 : va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
5313 10829 : && (TREE_TYPE (cfun_va_list) == void_type_node
5314 436 : || TREE_TYPE (cfun_va_list) == char_type_node);
5315 :
5316 10829 : switch (DECL_FUNCTION_CODE (callee))
5317 : {
5318 7006 : case BUILT_IN_VA_START:
5319 7006 : if (!va_list_simple_ptr
5320 172 : || targetm.expand_builtin_va_start != NULL
5321 7162 : || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
5322 : return false;
5323 :
5324 156 : if (gimple_call_num_args (call) != 2)
5325 : return false;
5326 :
5327 156 : lhs = gimple_call_arg (call, 0);
5328 156 : if (!POINTER_TYPE_P (TREE_TYPE (lhs))
5329 156 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
5330 156 : != TYPE_MAIN_VARIANT (cfun_va_list))
5331 : return false;
5332 : /* Create `tlhs = __builtin_next_arg(0);`. */
5333 156 : tlhs = make_ssa_name (cfun_va_list);
5334 156 : nstmt0 = gimple_build_call (builtin_decl_explicit (BUILT_IN_NEXT_ARG), 1, integer_zero_node);
5335 156 : lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
5336 156 : gimple_call_set_lhs (nstmt0, tlhs);
5337 156 : gimple_set_location (nstmt0, loc);
5338 156 : gimple_move_vops (nstmt0, call);
5339 156 : gsi_replace (gsi, nstmt0, false);
5340 156 : oldvdef = gimple_vdef (nstmt0);
5341 156 : newvdef = make_ssa_name (gimple_vop (cfun), nstmt0);
5342 156 : gimple_set_vdef (nstmt0, newvdef);
5343 :
5344 : /* Create `*lhs = tlhs;`. */
5345 156 : nstmt = gimple_build_assign (lhs, tlhs);
5346 156 : gimple_set_location (nstmt, loc);
5347 156 : gimple_set_vuse (nstmt, newvdef);
5348 156 : gimple_set_vdef (nstmt, oldvdef);
5349 156 : SSA_NAME_DEF_STMT (oldvdef) = nstmt;
5350 156 : gsi_insert_after (gsi, nstmt, GSI_NEW_STMT);
5351 :
5352 156 : if (dump_file && (dump_flags & TDF_DETAILS))
5353 : {
5354 0 : fprintf (dump_file, "Simplified\n ");
5355 0 : print_gimple_stmt (dump_file, call, 0, dump_flags);
5356 0 : fprintf (dump_file, "into\n ");
5357 0 : print_gimple_stmt (dump_file, nstmt0, 0, dump_flags);
5358 0 : fprintf (dump_file, " ");
5359 0 : print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
5360 : }
5361 : return true;
5362 :
5363 244 : case BUILT_IN_VA_COPY:
5364 244 : if (!va_list_simple_ptr)
5365 : return false;
5366 :
5367 47 : if (gimple_call_num_args (call) != 2)
5368 : return false;
5369 :
5370 47 : lhs = gimple_call_arg (call, 0);
5371 47 : if (!POINTER_TYPE_P (TREE_TYPE (lhs))
5372 47 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
5373 47 : != TYPE_MAIN_VARIANT (cfun_va_list))
5374 : return false;
5375 47 : rhs = gimple_call_arg (call, 1);
5376 47 : if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
5377 47 : != TYPE_MAIN_VARIANT (cfun_va_list))
5378 : return false;
5379 :
5380 47 : lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
5381 47 : nstmt = gimple_build_assign (lhs, rhs);
5382 47 : gimple_set_location (nstmt, loc);
5383 47 : gimple_move_vops (nstmt, call);
5384 47 : gsi_replace (gsi, nstmt, false);
5385 :
5386 47 : if (dump_file && (dump_flags & TDF_DETAILS))
5387 : {
5388 0 : fprintf (dump_file, "Simplified\n ");
5389 0 : print_gimple_stmt (dump_file, call, 0, dump_flags);
5390 0 : fprintf (dump_file, "into\n ");
5391 0 : print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
5392 : }
5393 : return true;
5394 :
5395 3579 : case BUILT_IN_VA_END:
5396 : /* No effect, so the statement will be deleted. */
5397 3579 : if (dump_file && (dump_flags & TDF_DETAILS))
5398 : {
5399 0 : fprintf (dump_file, "Removed\n ");
5400 0 : print_gimple_stmt (dump_file, call, 0, dump_flags);
5401 : }
5402 3579 : unlink_stmt_vdef (call);
5403 3579 : release_defs (call);
5404 3579 : gsi_replace (gsi, gimple_build_nop (), false);
5405 3579 : return true;
5406 :
5407 0 : default:
5408 0 : gcc_unreachable ();
5409 : }
5410 : }
5411 :
5412 : /* Fold the non-target builtin at *GSI and return whether any simplification
5413 : was made. */
5414 :
5415 : static bool
5416 9338987 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
5417 : {
5418 9338987 : gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
5419 9338987 : tree callee = gimple_call_fndecl (stmt);
5420 :
5421 : /* Give up for always_inline inline builtins until they are
5422 : inlined. */
5423 9338987 : if (avoid_folding_inline_builtin (callee))
5424 : return false;
5425 :
5426 9337811 : unsigned n = gimple_call_num_args (stmt);
5427 9337811 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
5428 9337811 : switch (fcode)
5429 : {
5430 115065 : case BUILT_IN_VA_START:
5431 115065 : case BUILT_IN_VA_END:
5432 115065 : case BUILT_IN_VA_COPY:
5433 115065 : return gimple_fold_builtin_stdarg (gsi, stmt);
5434 148 : case BUILT_IN_BCMP:
5435 148 : return gimple_fold_builtin_bcmp (gsi);
5436 367 : case BUILT_IN_BCOPY:
5437 367 : return gimple_fold_builtin_bcopy (gsi);
5438 250 : case BUILT_IN_BZERO:
5439 250 : return gimple_fold_builtin_bzero (gsi);
5440 :
5441 307619 : case BUILT_IN_MEMSET:
5442 307619 : return gimple_fold_builtin_memset (gsi,
5443 : gimple_call_arg (stmt, 1),
5444 307619 : gimple_call_arg (stmt, 2));
5445 10276 : case BUILT_IN_MEMPCPY:
5446 10276 : if (gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
5447 : gimple_call_arg (stmt, 1), fcode))
5448 : return true;
5449 9536 : return gimple_fold_builtin_mempcpy (gsi);
5450 972311 : case BUILT_IN_MEMCPY:
5451 972311 : case BUILT_IN_MEMMOVE:
5452 972311 : return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
5453 972311 : gimple_call_arg (stmt, 1), fcode);
5454 4459 : case BUILT_IN_SPRINTF_CHK:
5455 4459 : case BUILT_IN_VSPRINTF_CHK:
5456 4459 : return gimple_fold_builtin_sprintf_chk (gsi, fcode);
5457 1493 : case BUILT_IN_STRCAT_CHK:
5458 1493 : return gimple_fold_builtin_strcat_chk (gsi);
5459 1116 : case BUILT_IN_STRNCAT_CHK:
5460 1116 : return gimple_fold_builtin_strncat_chk (gsi);
5461 136437 : case BUILT_IN_STRLEN:
5462 136437 : return gimple_fold_builtin_strlen (gsi);
5463 26090 : case BUILT_IN_STRCPY:
5464 26090 : return gimple_fold_builtin_strcpy (gsi,
5465 : gimple_call_arg (stmt, 0),
5466 26090 : gimple_call_arg (stmt, 1));
5467 17274 : case BUILT_IN_STRNCPY:
5468 17274 : return gimple_fold_builtin_strncpy (gsi,
5469 : gimple_call_arg (stmt, 0),
5470 : gimple_call_arg (stmt, 1),
5471 17274 : gimple_call_arg (stmt, 2));
5472 7458 : case BUILT_IN_STRCAT:
5473 7458 : return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
5474 7458 : gimple_call_arg (stmt, 1));
5475 6786 : case BUILT_IN_STRNCAT:
5476 6786 : return gimple_fold_builtin_strncat (gsi);
5477 4743 : case BUILT_IN_INDEX:
5478 4743 : case BUILT_IN_STRCHR:
5479 4743 : return gimple_fold_builtin_strchr (gsi, false);
5480 730 : case BUILT_IN_RINDEX:
5481 730 : case BUILT_IN_STRRCHR:
5482 730 : return gimple_fold_builtin_strchr (gsi, true);
5483 4126 : case BUILT_IN_STRSTR:
5484 4126 : return gimple_fold_builtin_strstr (gsi);
5485 1250674 : case BUILT_IN_STRCMP:
5486 1250674 : case BUILT_IN_STRCMP_EQ:
5487 1250674 : case BUILT_IN_STRCASECMP:
5488 1250674 : case BUILT_IN_STRNCMP:
5489 1250674 : case BUILT_IN_STRNCMP_EQ:
5490 1250674 : case BUILT_IN_STRNCASECMP:
5491 1250674 : return gimple_fold_builtin_string_compare (gsi);
5492 30420 : case BUILT_IN_MEMCHR:
5493 30420 : return gimple_fold_builtin_memchr (gsi);
5494 20697 : case BUILT_IN_FPUTS:
5495 20697 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5496 20697 : gimple_call_arg (stmt, 1), false);
5497 43 : case BUILT_IN_FPUTS_UNLOCKED:
5498 43 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5499 43 : gimple_call_arg (stmt, 1), true);
5500 25701 : case BUILT_IN_MEMCPY_CHK:
5501 25701 : case BUILT_IN_MEMPCPY_CHK:
5502 25701 : case BUILT_IN_MEMMOVE_CHK:
5503 25701 : case BUILT_IN_MEMSET_CHK:
5504 25701 : return gimple_fold_builtin_memory_chk (gsi,
5505 : gimple_call_arg (stmt, 0),
5506 : gimple_call_arg (stmt, 1),
5507 : gimple_call_arg (stmt, 2),
5508 : gimple_call_arg (stmt, 3),
5509 25701 : fcode);
5510 3674 : case BUILT_IN_STPCPY:
5511 3674 : return gimple_fold_builtin_stpcpy (gsi);
5512 2566 : case BUILT_IN_STRCPY_CHK:
5513 2566 : case BUILT_IN_STPCPY_CHK:
5514 2566 : return gimple_fold_builtin_stxcpy_chk (gsi,
5515 : gimple_call_arg (stmt, 0),
5516 : gimple_call_arg (stmt, 1),
5517 : gimple_call_arg (stmt, 2),
5518 2566 : fcode);
5519 2721 : case BUILT_IN_STRNCPY_CHK:
5520 2721 : case BUILT_IN_STPNCPY_CHK:
5521 2721 : return gimple_fold_builtin_stxncpy_chk (gsi,
5522 : gimple_call_arg (stmt, 0),
5523 : gimple_call_arg (stmt, 1),
5524 : gimple_call_arg (stmt, 2),
5525 : gimple_call_arg (stmt, 3),
5526 2721 : fcode);
5527 2359 : case BUILT_IN_SNPRINTF_CHK:
5528 2359 : case BUILT_IN_VSNPRINTF_CHK:
5529 2359 : return gimple_fold_builtin_snprintf_chk (gsi, fcode);
5530 :
5531 798721 : case BUILT_IN_FPRINTF:
5532 798721 : case BUILT_IN_FPRINTF_UNLOCKED:
5533 798721 : case BUILT_IN_VFPRINTF:
5534 798721 : if (n == 2 || n == 3)
5535 94979 : return gimple_fold_builtin_fprintf (gsi,
5536 : gimple_call_arg (stmt, 0),
5537 : gimple_call_arg (stmt, 1),
5538 : n == 3
5539 42503 : ? gimple_call_arg (stmt, 2)
5540 : : NULL_TREE,
5541 52476 : fcode);
5542 : break;
5543 2229 : case BUILT_IN_FPRINTF_CHK:
5544 2229 : case BUILT_IN_VFPRINTF_CHK:
5545 2229 : if (n == 3 || n == 4)
5546 3814 : return gimple_fold_builtin_fprintf (gsi,
5547 : gimple_call_arg (stmt, 0),
5548 : gimple_call_arg (stmt, 2),
5549 : n == 4
5550 1744 : ? gimple_call_arg (stmt, 3)
5551 : : NULL_TREE,
5552 2070 : fcode);
5553 : break;
5554 199577 : case BUILT_IN_PRINTF:
5555 199577 : case BUILT_IN_PRINTF_UNLOCKED:
5556 199577 : case BUILT_IN_VPRINTF:
5557 199577 : if (n == 1 || n == 2)
5558 234214 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
5559 : n == 2
5560 112540 : ? gimple_call_arg (stmt, 1)
5561 121674 : : NULL_TREE, fcode);
5562 : break;
5563 2273 : case BUILT_IN_PRINTF_CHK:
5564 2273 : case BUILT_IN_VPRINTF_CHK:
5565 2273 : if (n == 2 || n == 3)
5566 3893 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
5567 : n == 3
5568 1771 : ? gimple_call_arg (stmt, 2)
5569 2122 : : NULL_TREE, fcode);
5570 : break;
5571 2866 : case BUILT_IN_ACC_ON_DEVICE:
5572 2866 : return gimple_fold_builtin_acc_on_device (gsi,
5573 2866 : gimple_call_arg (stmt, 0));
5574 234 : case BUILT_IN_OMP_IS_INITIAL_DEVICE:
5575 234 : return gimple_fold_builtin_omp_is_initial_device (gsi);
5576 :
5577 103 : case BUILT_IN_OMP_GET_INITIAL_DEVICE:
5578 103 : return gimple_fold_builtin_omp_get_initial_device (gsi);
5579 :
5580 309 : case BUILT_IN_OMP_GET_NUM_DEVICES:
5581 309 : return gimple_fold_builtin_omp_get_num_devices (gsi);
5582 :
5583 49353 : case BUILT_IN_REALLOC:
5584 49353 : return gimple_fold_builtin_realloc (gsi);
5585 :
5586 630 : case BUILT_IN_CLEAR_PADDING:
5587 630 : return gimple_fold_builtin_clear_padding (gsi);
5588 :
5589 95035 : case BUILT_IN_CONSTANT_P:
5590 95035 : return gimple_fold_builtin_constant_p (gsi);
5591 :
5592 6055336 : default:;
5593 : }
5594 :
5595 : /* Try the generic builtin folder. */
5596 6055336 : bool ignore = (gimple_call_lhs (stmt) == NULL);
5597 6055336 : tree result = fold_call_stmt (stmt, ignore);
5598 6055336 : if (result)
5599 : {
5600 4973 : if (ignore)
5601 1236 : STRIP_NOPS (result);
5602 : else
5603 3737 : result = fold_convert (gimple_call_return_type (stmt), result);
5604 4973 : gimplify_and_update_call_from_tree (gsi, result);
5605 4973 : return true;
5606 : }
5607 :
5608 : return false;
5609 : }
5610 :
5611 : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
5612 : function calls to constants, where possible. */
5613 :
5614 : static tree
5615 20589 : fold_internal_goacc_dim (const gimple *call)
5616 : {
5617 20589 : int axis = oacc_get_ifn_dim_arg (call);
5618 20589 : int size = oacc_get_fn_dim_size (current_function_decl, axis);
5619 20589 : tree result = NULL_TREE;
5620 20589 : tree type = TREE_TYPE (gimple_call_lhs (call));
5621 :
5622 20589 : switch (gimple_call_internal_fn (call))
5623 : {
5624 8915 : case IFN_GOACC_DIM_POS:
5625 : /* If the size is 1, we know the answer. */
5626 8915 : if (size == 1)
5627 8915 : result = build_int_cst (type, 0);
5628 : break;
5629 11674 : case IFN_GOACC_DIM_SIZE:
5630 : /* If the size is not dynamic, we know the answer. */
5631 11674 : if (size)
5632 11674 : result = build_int_cst (type, size);
5633 : break;
5634 : default:
5635 : break;
5636 : }
5637 :
5638 20589 : return result;
5639 : }
5640 :
5641 : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
5642 : for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
5643 : &var where var is only addressable because of such calls. */
5644 :
5645 : bool
5646 58260666 : optimize_atomic_compare_exchange_p (gimple *stmt)
5647 : {
5648 58260666 : if (gimple_call_num_args (stmt) != 6
5649 1582575 : || !flag_inline_atomics
5650 1582575 : || !optimize
5651 1582575 : || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
5652 1582494 : || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
5653 1043886 : || !gimple_vdef (stmt)
5654 59191632 : || !gimple_vuse (stmt))
5655 57329700 : return false;
5656 :
5657 930966 : tree fndecl = gimple_call_fndecl (stmt);
5658 930966 : switch (DECL_FUNCTION_CODE (fndecl))
5659 : {
5660 51771 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
5661 51771 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
5662 51771 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
5663 51771 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
5664 51771 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
5665 51771 : break;
5666 : default:
5667 : return false;
5668 : }
5669 :
5670 51771 : tree expected = gimple_call_arg (stmt, 1);
5671 51771 : if (TREE_CODE (expected) != ADDR_EXPR
5672 51771 : || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
5673 : return false;
5674 :
5675 49430 : tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
5676 49430 : if (!is_gimple_reg_type (etype)
5677 49003 : || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
5678 46605 : || TREE_THIS_VOLATILE (etype)
5679 46605 : || VECTOR_TYPE_P (etype)
5680 : || TREE_CODE (etype) == COMPLEX_TYPE
5681 : /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
5682 : might not preserve all the bits. See PR71716. */
5683 : || SCALAR_FLOAT_TYPE_P (etype)
5684 67404 : || maybe_ne (TYPE_PRECISION (etype),
5685 35948 : GET_MODE_BITSIZE (TYPE_MODE (etype))))
5686 37864 : return false;
5687 :
5688 11566 : tree weak = gimple_call_arg (stmt, 3);
5689 11566 : if (!integer_zerop (weak) && !integer_onep (weak))
5690 : return false;
5691 :
5692 11566 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5693 11566 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5694 11566 : machine_mode mode = TYPE_MODE (itype);
5695 :
5696 11566 : if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
5697 : == CODE_FOR_nothing
5698 11566 : && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
5699 : return false;
5700 :
5701 23132 : if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
5702 : return false;
5703 :
5704 : return true;
5705 : }
5706 :
5707 : /* Fold
5708 : r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
5709 : into
5710 : _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
5711 : i = IMAGPART_EXPR <t>;
5712 : r = (_Bool) i;
5713 : e = REALPART_EXPR <t>; */
5714 :
5715 : void
5716 5708 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
5717 : {
5718 5708 : gimple *stmt = gsi_stmt (*gsi);
5719 5708 : tree fndecl = gimple_call_fndecl (stmt);
5720 5708 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5721 5708 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5722 5708 : tree ctype = build_complex_type (itype);
5723 5708 : tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
5724 5708 : bool throws = false;
5725 5708 : edge e = NULL;
5726 5708 : gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5727 : expected);
5728 5708 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5729 5708 : gimple_stmt_iterator gsiret = gsi_for_stmt (g);
5730 5708 : if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
5731 : {
5732 2628 : g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
5733 : build1 (VIEW_CONVERT_EXPR, itype,
5734 : gimple_assign_lhs (g)));
5735 2628 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5736 : }
5737 5708 : int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
5738 11051 : + int_size_in_bytes (itype);
5739 5708 : g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
5740 : gimple_call_arg (stmt, 0),
5741 : gimple_assign_lhs (g),
5742 : gimple_call_arg (stmt, 2),
5743 5708 : build_int_cst (integer_type_node, flag),
5744 : gimple_call_arg (stmt, 4),
5745 : gimple_call_arg (stmt, 5));
5746 5708 : tree lhs = make_ssa_name (ctype);
5747 5708 : gimple_call_set_lhs (g, lhs);
5748 5708 : gimple_move_vops (g, stmt);
5749 5708 : tree oldlhs = gimple_call_lhs (stmt);
5750 5708 : if (stmt_can_throw_internal (cfun, stmt))
5751 : {
5752 203 : throws = true;
5753 203 : e = find_fallthru_edge (gsi_bb (*gsi)->succs);
5754 : }
5755 5708 : gimple_call_set_nothrow (as_a <gcall *> (g),
5756 5708 : gimple_call_nothrow_p (as_a <gcall *> (stmt)));
5757 5708 : gimple_call_set_lhs (stmt, NULL_TREE);
5758 5708 : gsi_replace (gsi, g, true);
5759 5708 : if (oldlhs)
5760 : {
5761 5661 : g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
5762 : build1 (IMAGPART_EXPR, itype, lhs));
5763 5661 : if (throws)
5764 : {
5765 197 : gsi_insert_on_edge_immediate (e, g);
5766 197 : *gsi = gsi_for_stmt (g);
5767 : }
5768 : else
5769 5464 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5770 5661 : g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
5771 5661 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5772 : }
5773 5708 : g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
5774 : build1 (REALPART_EXPR, itype, lhs));
5775 5708 : if (throws && oldlhs == NULL_TREE)
5776 : {
5777 6 : gsi_insert_on_edge_immediate (e, g);
5778 6 : *gsi = gsi_for_stmt (g);
5779 : }
5780 : else
5781 5702 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5782 5708 : if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
5783 : {
5784 5256 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5785 : VIEW_CONVERT_EXPR,
5786 2628 : build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
5787 : gimple_assign_lhs (g)));
5788 2628 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5789 : }
5790 5708 : g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
5791 5708 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5792 5708 : *gsi = gsiret;
5793 5708 : }
5794 :
5795 : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
5796 : doesn't fit into TYPE. The test for overflow should be regardless of
5797 : -fwrapv, and even for unsigned types. */
5798 :
5799 : bool
5800 416901 : arith_overflowed_p (enum tree_code code, const_tree type,
5801 : const_tree arg0, const_tree arg1)
5802 : {
5803 416901 : widest2_int warg0 = widest2_int_cst (arg0);
5804 416901 : widest2_int warg1 = widest2_int_cst (arg1);
5805 416901 : widest2_int wres;
5806 416901 : switch (code)
5807 : {
5808 96169 : case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
5809 115582 : case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
5810 206545 : case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
5811 0 : default: gcc_unreachable ();
5812 : }
5813 416901 : signop sign = TYPE_SIGN (type);
5814 416901 : if (sign == UNSIGNED && wi::neg_p (wres))
5815 : return true;
5816 345001 : return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
5817 417041 : }
5818 :
5819 : /* Mask state for partial load/store operations (mask and length). */
5820 : enum mask_load_store_state {
5821 : MASK_ALL_INACTIVE, /* All lanes/elements are inactive (can be elided). */
5822 : MASK_ALL_ACTIVE, /* All lanes/elements are active (unconditional). */
5823 : MASK_UNKNOWN
5824 : };
5825 :
5826 : /* Check the mask/length state of IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL.
5827 : Returns whether all elements are active, all inactive, or mixed.
5828 : VECTYPE is the vector type of the operation. */
5829 :
5830 : static enum mask_load_store_state
5831 8091 : partial_load_store_mask_state (gcall *call, tree vectype)
5832 : {
5833 8091 : internal_fn ifn = gimple_call_internal_fn (call);
5834 8091 : int mask_index = internal_fn_mask_index (ifn);
5835 8091 : int len_index = internal_fn_len_index (ifn);
5836 :
5837 : /* Extract length and mask arguments up front. */
5838 8091 : tree len = len_index != -1 ? gimple_call_arg (call, len_index) : NULL_TREE;
5839 0 : tree bias = len ? gimple_call_arg (call, len_index + 1) : NULL_TREE;
5840 8091 : tree mask = mask_index != -1 ? gimple_call_arg (call, mask_index) : NULL_TREE;
5841 :
5842 16182 : poly_int64 nelts = GET_MODE_NUNITS (TYPE_MODE (vectype));
5843 :
5844 8091 : poly_widest_int wlen = -1;
5845 8091 : bool full_length_p = !len; /* No length means full length. */
5846 :
5847 : /* Compute effective length. */
5848 8091 : if (len && poly_int_tree_p (len))
5849 : {
5850 0 : gcc_assert (TREE_CODE (bias) == INTEGER_CST);
5851 0 : wlen = wi::to_poly_widest (len) + wi::to_widest (bias);
5852 :
5853 0 : if (known_eq (wlen, 0))
5854 : return MASK_ALL_INACTIVE;
5855 :
5856 0 : if (known_eq (wlen, nelts))
5857 : full_length_p = true;
5858 : else
5859 : full_length_p = false;
5860 : }
5861 :
5862 : /* Check mask for early return cases. */
5863 8091 : if (mask)
5864 : {
5865 8091 : if (integer_zerop (mask))
5866 : return MASK_ALL_INACTIVE;
5867 :
5868 8076 : if (full_length_p && integer_all_onesp (mask))
5869 : return MASK_ALL_ACTIVE;
5870 : }
5871 0 : else if (full_length_p)
5872 : /* No mask and full length means all active. */
5873 : return MASK_ALL_ACTIVE;
5874 :
5875 : /* For VLA vectors, we can't do much more. */
5876 7934 : if (!nelts.is_constant ())
5877 : return MASK_UNKNOWN;
5878 :
5879 : /* Same for VLS vectors with non-constant mask. */
5880 7934 : if (mask && TREE_CODE (mask) != VECTOR_CST)
5881 : return MASK_UNKNOWN;
5882 :
5883 : /* Check VLS vector elements. */
5884 480 : gcc_assert (wlen.is_constant ());
5885 :
5886 480 : HOST_WIDE_INT active_len = wlen.to_constant ().to_shwi ();
5887 480 : if (active_len == -1)
5888 480 : active_len = nelts.to_constant ();
5889 :
5890 : /* Check if all elements in the active range match the mask. */
5891 996 : for (HOST_WIDE_INT i = 0; i < active_len; i++)
5892 : {
5893 996 : bool elt_active = !mask || !integer_zerop (vector_cst_elt (mask, i));
5894 516 : if (!elt_active)
5895 : {
5896 : /* Found an inactive element. Check if all are inactive. */
5897 972 : for (HOST_WIDE_INT j = 0; j < active_len; j++)
5898 972 : if (!mask || !integer_zerop (vector_cst_elt (mask, j)))
5899 : return MASK_UNKNOWN; /* Mixed state. */
5900 : return MASK_ALL_INACTIVE;
5901 : }
5902 : }
5903 :
5904 : /* All elements in active range are active. */
5905 0 : return full_length_p ? MASK_ALL_ACTIVE : MASK_UNKNOWN;
5906 8091 : }
5907 :
5908 :
5909 : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional
5910 : (all lanes active), return a MEM_REF for the memory it references.
5911 : Otherwise return NULL_TREE. VECTYPE is the type of the memory vector. */
5912 :
5913 : static tree
5914 4038 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype)
5915 : {
5916 : /* Only fold if all lanes are active (unconditional). */
5917 4038 : if (partial_load_store_mask_state (call, vectype) != MASK_ALL_ACTIVE)
5918 : return NULL_TREE;
5919 :
5920 71 : tree ptr = gimple_call_arg (call, 0);
5921 71 : tree alias_align = gimple_call_arg (call, 1);
5922 71 : if (!tree_fits_uhwi_p (alias_align))
5923 : return NULL_TREE;
5924 :
5925 71 : unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
5926 71 : if (TYPE_ALIGN (vectype) != align)
5927 14 : vectype = build_aligned_type (vectype, align);
5928 71 : tree offset = build_zero_cst (TREE_TYPE (alias_align));
5929 71 : return fold_build2 (MEM_REF, vectype, ptr, offset);
5930 : }
5931 :
5932 : /* Try to fold IFN_{MASK,LEN}_LOAD/STORE call CALL. Return true on success. */
5933 :
5934 : static bool
5935 4053 : gimple_fold_partial_load_store (gimple_stmt_iterator *gsi, gcall *call)
5936 : {
5937 4053 : internal_fn ifn = gimple_call_internal_fn (call);
5938 4053 : tree lhs = gimple_call_lhs (call);
5939 4053 : bool is_load = (lhs != NULL_TREE);
5940 4053 : tree vectype;
5941 :
5942 4053 : if (is_load)
5943 1979 : vectype = TREE_TYPE (lhs);
5944 : else
5945 : {
5946 2074 : tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
5947 2074 : vectype = TREE_TYPE (rhs);
5948 : }
5949 :
5950 4053 : enum mask_load_store_state state
5951 4053 : = partial_load_store_mask_state (call, vectype);
5952 :
5953 : /* Handle all-inactive case. */
5954 4053 : if (state == MASK_ALL_INACTIVE)
5955 : {
5956 15 : if (is_load)
5957 : {
5958 : /* Replace load with else value. */
5959 15 : int else_index = internal_fn_else_index (ifn);
5960 15 : tree else_value = gimple_call_arg (call, else_index);
5961 15 : if (!is_gimple_reg (lhs))
5962 : {
5963 0 : if (!zerop (else_value))
5964 : return false;
5965 0 : else_value = build_constructor (TREE_TYPE (lhs), NULL);
5966 : }
5967 15 : gassign *new_stmt = gimple_build_assign (lhs, else_value);
5968 15 : gimple_set_location (new_stmt, gimple_location (call));
5969 : /* When the lhs is an array for LANES version, then there is still
5970 : a store, move the vops from the old stmt to the new one. */
5971 15 : if (!is_gimple_reg (lhs))
5972 0 : gimple_move_vops (new_stmt, call);
5973 15 : gsi_replace (gsi, new_stmt, false);
5974 15 : return true;
5975 : }
5976 : else
5977 : {
5978 : /* Remove inactive store altogether. */
5979 0 : unlink_stmt_vdef (call);
5980 0 : release_defs (call);
5981 0 : gsi_replace (gsi, gimple_build_nop (), true);
5982 0 : return true;
5983 : }
5984 : }
5985 :
5986 : /* We cannot simplify a gather/scatter or load/store lanes further. */
5987 4038 : if (internal_gather_scatter_fn_p (ifn)
5988 4038 : || TREE_CODE (vectype) == ARRAY_TYPE)
5989 : return false;
5990 :
5991 : /* Handle all-active case by folding to regular memory operation. */
5992 4038 : if (tree mem_ref = gimple_fold_partial_load_store_mem_ref (call, vectype))
5993 : {
5994 71 : gassign *new_stmt;
5995 71 : if (is_load)
5996 17 : new_stmt = gimple_build_assign (lhs, mem_ref);
5997 : else
5998 : {
5999 54 : tree rhs
6000 54 : = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
6001 54 : new_stmt = gimple_build_assign (mem_ref, rhs);
6002 : }
6003 :
6004 71 : gimple_set_location (new_stmt, gimple_location (call));
6005 71 : gimple_move_vops (new_stmt, call);
6006 71 : gsi_replace (gsi, new_stmt, false);
6007 71 : return true;
6008 : }
6009 : return false;
6010 : }
6011 :
6012 : /* Attempt to fold a call statement referenced by the statement iterator GSI.
6013 : The statement may be replaced by another statement, e.g., if the call
6014 : simplifies to a constant value. Return true if any changes were made.
6015 : It is assumed that the operands have been previously folded. */
6016 :
6017 : static bool
6018 55569283 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
6019 : {
6020 55569283 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
6021 55569283 : tree callee;
6022 55569283 : bool changed = false;
6023 :
6024 : /* Check for virtual calls that became direct calls. */
6025 55569283 : callee = gimple_call_fn (stmt);
6026 55569283 : if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
6027 : {
6028 437541 : if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
6029 : {
6030 6 : if (dump_file && virtual_method_call_p (callee)
6031 376 : && !possible_polymorphic_call_target_p
6032 6 : (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
6033 6 : (OBJ_TYPE_REF_EXPR (callee)))))
6034 : {
6035 0 : fprintf (dump_file,
6036 : "Type inheritance inconsistent devirtualization of ");
6037 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6038 0 : fprintf (dump_file, " to ");
6039 0 : print_generic_expr (dump_file, callee, TDF_SLIM);
6040 0 : fprintf (dump_file, "\n");
6041 : }
6042 :
6043 370 : gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
6044 370 : changed = true;
6045 : }
6046 437171 : else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
6047 : {
6048 432201 : bool final;
6049 432201 : vec <cgraph_node *>targets
6050 432201 : = possible_polymorphic_call_targets (callee, stmt, &final);
6051 434861 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
6052 : {
6053 2027 : tree lhs = gimple_call_lhs (stmt);
6054 2027 : if (dump_enabled_p ())
6055 : {
6056 34 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
6057 : "folding virtual function call to %s\n",
6058 34 : targets.length () == 1
6059 17 : ? targets[0]->name ()
6060 : : "__builtin_unreachable");
6061 : }
6062 2027 : if (targets.length () == 1)
6063 : {
6064 1988 : tree fndecl = targets[0]->decl;
6065 1988 : gimple_call_set_fndecl (stmt, fndecl);
6066 1988 : changed = true;
6067 : /* If changing the call to __cxa_pure_virtual
6068 : or similar noreturn function, adjust gimple_call_fntype
6069 : too. */
6070 1988 : if (gimple_call_noreturn_p (stmt)
6071 19 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
6072 13 : && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
6073 2001 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6074 13 : == void_type_node))
6075 13 : gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
6076 : /* If the call becomes noreturn, remove the lhs. */
6077 1988 : if (lhs
6078 1668 : && gimple_call_noreturn_p (stmt)
6079 2003 : && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
6080 6 : || should_remove_lhs_p (lhs)))
6081 : {
6082 12 : if (TREE_CODE (lhs) == SSA_NAME)
6083 : {
6084 0 : tree var = create_tmp_var (TREE_TYPE (lhs));
6085 0 : tree def = get_or_create_ssa_default_def (cfun, var);
6086 0 : gimple *new_stmt = gimple_build_assign (lhs, def);
6087 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6088 : }
6089 12 : gimple_call_set_lhs (stmt, NULL_TREE);
6090 : }
6091 1988 : maybe_remove_unused_call_args (cfun, stmt);
6092 : }
6093 : else
6094 : {
6095 39 : location_t loc = gimple_location (stmt);
6096 39 : gimple *new_stmt = gimple_build_builtin_unreachable (loc);
6097 39 : gimple_call_set_ctrl_altering (new_stmt, false);
6098 : /* If the call had a SSA name as lhs morph that into
6099 : an uninitialized value. */
6100 39 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6101 : {
6102 12 : tree var = create_tmp_var (TREE_TYPE (lhs));
6103 12 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
6104 12 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
6105 12 : set_ssa_default_def (cfun, var, lhs);
6106 : }
6107 39 : gimple_move_vops (new_stmt, stmt);
6108 39 : gsi_replace (gsi, new_stmt, false);
6109 39 : return true;
6110 : }
6111 : }
6112 : }
6113 : }
6114 :
6115 : /* Check for indirect calls that became direct calls, and then
6116 : no longer require a static chain. */
6117 55569244 : if (gimple_call_chain (stmt))
6118 : {
6119 246591 : tree fn = gimple_call_fndecl (stmt);
6120 295643 : if (fn && !DECL_STATIC_CHAIN (fn))
6121 : {
6122 2054 : gimple_call_set_chain (stmt, NULL);
6123 2054 : changed = true;
6124 : }
6125 : }
6126 :
6127 55569244 : if (inplace)
6128 : return changed;
6129 :
6130 : /* Don't constant fold functions which can change the control. */
6131 55566549 : if (gimple_call_ctrl_altering_p (stmt))
6132 : return changed;
6133 :
6134 : /* Check for builtins that CCP can handle using information not
6135 : available in the generic fold routines. */
6136 47944986 : if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
6137 : {
6138 9338987 : if (gimple_fold_builtin (gsi))
6139 208242 : changed = true;
6140 : }
6141 38605999 : else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
6142 : {
6143 1131057 : changed |= targetm.gimple_fold_builtin (gsi);
6144 : }
6145 37474942 : else if (gimple_call_internal_p (stmt))
6146 : {
6147 1784111 : enum tree_code subcode = ERROR_MARK;
6148 1784111 : tree result = NULL_TREE;
6149 1784111 : bool cplx_result = false;
6150 1784111 : bool uaddc_usubc = false;
6151 1784111 : tree overflow = NULL_TREE;
6152 1784111 : switch (gimple_call_internal_fn (stmt))
6153 : {
6154 852 : case IFN_ASSUME:
6155 : /* Remove .ASSUME calls during the last fold since it is no
6156 : longer needed. */
6157 852 : if (fold_before_rtl_expansion_p ())
6158 116 : replace_call_with_value (gsi, NULL_TREE);
6159 : break;
6160 162966 : case IFN_BUILTIN_EXPECT:
6161 162966 : result = fold_builtin_expect (gimple_location (stmt),
6162 : gimple_call_arg (stmt, 0),
6163 : gimple_call_arg (stmt, 1),
6164 : gimple_call_arg (stmt, 2),
6165 : NULL_TREE);
6166 162966 : break;
6167 8660 : case IFN_UBSAN_OBJECT_SIZE:
6168 8660 : {
6169 8660 : tree offset = gimple_call_arg (stmt, 1);
6170 8660 : tree objsize = gimple_call_arg (stmt, 2);
6171 8660 : if (integer_all_onesp (objsize)
6172 8660 : || (TREE_CODE (offset) == INTEGER_CST
6173 4787 : && TREE_CODE (objsize) == INTEGER_CST
6174 1126 : && tree_int_cst_le (offset, objsize)))
6175 : {
6176 1539 : replace_call_with_value (gsi, NULL_TREE);
6177 1539 : return true;
6178 : }
6179 : }
6180 : break;
6181 11385 : case IFN_UBSAN_PTR:
6182 11385 : if (integer_zerop (gimple_call_arg (stmt, 1)))
6183 : {
6184 30 : replace_call_with_value (gsi, NULL_TREE);
6185 30 : return true;
6186 : }
6187 : break;
6188 8493 : case IFN_UBSAN_BOUNDS:
6189 8493 : {
6190 8493 : tree index = gimple_call_arg (stmt, 1);
6191 8493 : tree bound = gimple_call_arg (stmt, 2);
6192 8493 : if (TREE_CODE (index) == INTEGER_CST
6193 5471 : && TREE_CODE (bound) == INTEGER_CST)
6194 : {
6195 4340 : index = fold_convert (TREE_TYPE (bound), index);
6196 4340 : if (TREE_CODE (index) == INTEGER_CST
6197 4340 : && tree_int_cst_lt (index, bound))
6198 : {
6199 286 : replace_call_with_value (gsi, NULL_TREE);
6200 286 : return true;
6201 : }
6202 : }
6203 : }
6204 : break;
6205 20589 : case IFN_GOACC_DIM_SIZE:
6206 20589 : case IFN_GOACC_DIM_POS:
6207 20589 : result = fold_internal_goacc_dim (stmt);
6208 20589 : break;
6209 : case IFN_UBSAN_CHECK_ADD:
6210 : subcode = PLUS_EXPR;
6211 : break;
6212 : case IFN_UBSAN_CHECK_SUB:
6213 : subcode = MINUS_EXPR;
6214 : break;
6215 : case IFN_UBSAN_CHECK_MUL:
6216 : subcode = MULT_EXPR;
6217 : break;
6218 : case IFN_ADD_OVERFLOW:
6219 : subcode = PLUS_EXPR;
6220 : cplx_result = true;
6221 : break;
6222 : case IFN_SUB_OVERFLOW:
6223 : subcode = MINUS_EXPR;
6224 : cplx_result = true;
6225 : break;
6226 : case IFN_MUL_OVERFLOW:
6227 : subcode = MULT_EXPR;
6228 : cplx_result = true;
6229 : break;
6230 : case IFN_UADDC:
6231 : subcode = PLUS_EXPR;
6232 : cplx_result = true;
6233 : uaddc_usubc = true;
6234 : break;
6235 : case IFN_USUBC:
6236 : subcode = MINUS_EXPR;
6237 : cplx_result = true;
6238 : uaddc_usubc = true;
6239 : break;
6240 4053 : case IFN_LEN_LOAD:
6241 4053 : case IFN_MASK_LOAD:
6242 4053 : case IFN_MASK_LEN_LOAD:
6243 4053 : case IFN_MASK_GATHER_LOAD:
6244 4053 : case IFN_MASK_LEN_GATHER_LOAD:
6245 4053 : case IFN_MASK_LOAD_LANES:
6246 4053 : case IFN_MASK_LEN_LOAD_LANES:
6247 4053 : case IFN_LEN_STORE:
6248 4053 : case IFN_MASK_STORE:
6249 4053 : case IFN_MASK_LEN_STORE:
6250 4053 : case IFN_MASK_SCATTER_STORE:
6251 4053 : case IFN_MASK_LEN_SCATTER_STORE:
6252 4053 : case IFN_MASK_STORE_LANES:
6253 4053 : case IFN_MASK_LEN_STORE_LANES:
6254 4053 : changed |= gimple_fold_partial_load_store (gsi, stmt);
6255 4053 : break;
6256 : default:
6257 : break;
6258 : }
6259 187724 : if (subcode != ERROR_MARK)
6260 : {
6261 493292 : tree arg0 = gimple_call_arg (stmt, 0);
6262 493292 : tree arg1 = gimple_call_arg (stmt, 1);
6263 493292 : tree arg2 = NULL_TREE;
6264 493292 : tree type = TREE_TYPE (arg0);
6265 493292 : if (cplx_result)
6266 : {
6267 474206 : tree lhs = gimple_call_lhs (stmt);
6268 474206 : if (lhs == NULL_TREE)
6269 : type = NULL_TREE;
6270 : else
6271 474206 : type = TREE_TYPE (TREE_TYPE (lhs));
6272 474206 : if (uaddc_usubc)
6273 31855 : arg2 = gimple_call_arg (stmt, 2);
6274 : }
6275 493292 : if (type == NULL_TREE)
6276 : ;
6277 493292 : else if (uaddc_usubc)
6278 : {
6279 31855 : if (!integer_zerop (arg2))
6280 : ;
6281 : /* x = y + 0 + 0; x = y - 0 - 0; */
6282 4825 : else if (integer_zerop (arg1))
6283 : result = arg0;
6284 : /* x = 0 + y + 0; */
6285 4201 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
6286 : result = arg1;
6287 : /* x = y - y - 0; */
6288 4201 : else if (subcode == MINUS_EXPR
6289 4201 : && operand_equal_p (arg0, arg1, 0))
6290 0 : result = integer_zero_node;
6291 : }
6292 : /* x = y + 0; x = y - 0; x = y * 0; */
6293 461437 : else if (integer_zerop (arg1))
6294 10107 : result = subcode == MULT_EXPR ? integer_zero_node : arg0;
6295 : /* x = 0 + y; x = 0 * y; */
6296 451330 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
6297 0 : result = subcode == MULT_EXPR ? integer_zero_node : arg1;
6298 : /* x = y - y; */
6299 451330 : else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
6300 13 : result = integer_zero_node;
6301 : /* x = y * 1; x = 1 * y; */
6302 451317 : else if (subcode == MULT_EXPR && integer_onep (arg1))
6303 : result = arg0;
6304 446196 : else if (subcode == MULT_EXPR && integer_onep (arg0))
6305 : result = arg1;
6306 493292 : if (result)
6307 : {
6308 15865 : if (result == integer_zero_node)
6309 2146 : result = build_zero_cst (type);
6310 13719 : else if (cplx_result && TREE_TYPE (result) != type)
6311 : {
6312 9638 : if (TREE_CODE (result) == INTEGER_CST)
6313 : {
6314 0 : if (arith_overflowed_p (PLUS_EXPR, type, result,
6315 : integer_zero_node))
6316 0 : overflow = build_one_cst (type);
6317 : }
6318 9638 : else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
6319 6920 : && TYPE_UNSIGNED (type))
6320 9783 : || (TYPE_PRECISION (type)
6321 2863 : < (TYPE_PRECISION (TREE_TYPE (result))
6322 2863 : + (TYPE_UNSIGNED (TREE_TYPE (result))
6323 3219 : && !TYPE_UNSIGNED (type)))))
6324 : result = NULL_TREE;
6325 62 : if (result)
6326 62 : result = fold_convert (type, result);
6327 : }
6328 : }
6329 : }
6330 :
6331 1295253 : if (result)
6332 : {
6333 28784 : if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
6334 0 : result = drop_tree_overflow (result);
6335 28784 : if (cplx_result)
6336 : {
6337 6278 : if (overflow == NULL_TREE)
6338 6278 : overflow = build_zero_cst (TREE_TYPE (result));
6339 6278 : tree ctype = build_complex_type (TREE_TYPE (result));
6340 6278 : if (TREE_CODE (result) == INTEGER_CST
6341 2146 : && TREE_CODE (overflow) == INTEGER_CST)
6342 2146 : result = build_complex (ctype, result, overflow);
6343 : else
6344 4132 : result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
6345 : ctype, result, overflow);
6346 : }
6347 28784 : gimplify_and_update_call_from_tree (gsi, result);
6348 28784 : changed = true;
6349 : }
6350 : }
6351 :
6352 : return changed;
6353 : }
6354 :
6355 :
6356 : /* Return true whether NAME has a use on STMT. Note this can return
6357 : false even though there's a use on STMT if SSA operands are not
6358 : up-to-date. */
6359 :
6360 : static bool
6361 1674 : has_use_on_stmt (tree name, gimple *stmt)
6362 : {
6363 1674 : ssa_op_iter iter;
6364 1674 : tree op;
6365 3373 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6366 1745 : if (op == name)
6367 : return true;
6368 : return false;
6369 : }
6370 :
6371 : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
6372 :
6373 : void
6374 4613868 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
6375 : {
6376 4613868 : if (!dce_worklist)
6377 : return;
6378 :
6379 1608552 : for (gimple_stmt_iterator i = gsi_start (seq);
6380 1880066 : !gsi_end_p (i); gsi_next (&i))
6381 : {
6382 271514 : gimple *stmt = gsi_stmt (i);
6383 271514 : tree name = gimple_get_lhs (stmt);
6384 271514 : if (name && TREE_CODE (name) == SSA_NAME)
6385 271514 : bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
6386 : }
6387 : }
6388 :
6389 : /* Worker for fold_stmt_1 dispatch to pattern based folding with
6390 : gimple_simplify.
6391 :
6392 : Replaces *GSI with the simplification result in RCODE and OPS
6393 : and the associated statements in *SEQ. Does the replacement
6394 : according to INPLACE and returns true if the operation succeeded. */
6395 :
6396 : static bool
6397 8949139 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
6398 : gimple_match_op *res_op,
6399 : gimple_seq *seq, bool inplace,
6400 : bitmap dce_worklist)
6401 : {
6402 8949139 : gimple *stmt = gsi_stmt (*gsi);
6403 8949139 : tree *ops = res_op->ops;
6404 8949139 : unsigned int num_ops = res_op->num_ops;
6405 :
6406 : /* Play safe and do not allow abnormals to be mentioned in
6407 : newly created statements. See also maybe_push_res_to_seq.
6408 : As an exception allow such uses if there was a use of the
6409 : same SSA name on the old stmt. */
6410 19848553 : for (unsigned int i = 0; i < num_ops; ++i)
6411 10901042 : if (TREE_CODE (ops[i]) == SSA_NAME
6412 6640044 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
6413 10902716 : && !has_use_on_stmt (ops[i], stmt))
6414 : return false;
6415 :
6416 8947511 : if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
6417 0 : for (unsigned int i = 0; i < 2; ++i)
6418 0 : if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
6419 0 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
6420 0 : && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
6421 : return false;
6422 :
6423 : /* Don't insert new statements when INPLACE is true, even if we could
6424 : reuse STMT for the final statement. */
6425 8947511 : if (inplace && !gimple_seq_empty_p (*seq))
6426 : return false;
6427 :
6428 8947511 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6429 : {
6430 6738372 : gcc_assert (res_op->code.is_tree_code ());
6431 6738372 : auto code = tree_code (res_op->code);
6432 6738372 : if (TREE_CODE_CLASS (code) == tcc_comparison
6433 : /* GIMPLE_CONDs condition may not throw. */
6434 6738372 : && ((cfun
6435 1118765 : && (!flag_exceptions
6436 757488 : || !cfun->can_throw_non_call_exceptions))
6437 291735 : || !operation_could_trap_p (code,
6438 291735 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6439 : false, NULL_TREE)))
6440 1107488 : gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
6441 5630884 : else if (code == SSA_NAME)
6442 : {
6443 : /* If setting the gimple cond to the same thing,
6444 : return false as nothing changed. */
6445 3917373 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6446 3893151 : && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
6447 7807982 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6448 : return false;
6449 26764 : gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
6450 26764 : build_zero_cst (TREE_TYPE (ops[0])));
6451 : }
6452 1713511 : else if (code == INTEGER_CST)
6453 : {
6454 : /* Make into the canonical form `1 != 0` and `0 != 0`.
6455 : If already in the canonical form return false
6456 : saying nothing has been done. */
6457 1184904 : if (integer_zerop (ops[0]))
6458 : {
6459 5242725 : if (gimple_cond_false_canonical_p (cond_stmt))
6460 : return false;
6461 477868 : gimple_cond_make_false (cond_stmt);
6462 : }
6463 : else
6464 : {
6465 362376 : if (gimple_cond_true_canonical_p (cond_stmt))
6466 : return false;
6467 191336 : gimple_cond_make_true (cond_stmt);
6468 : }
6469 : }
6470 528607 : else if (!inplace)
6471 : {
6472 : /* For throwing comparisons, see if the GIMPLE_COND is the same as
6473 : the comparison would be.
6474 : This can happen due to the match pattern for
6475 : `(ne (cmp @0 @1) integer_zerop)` which creates a new expression
6476 : for the comparison. */
6477 528607 : if (TREE_CODE_CLASS (code) == tcc_comparison
6478 11277 : && (!cfun
6479 11277 : || (flag_exceptions
6480 11277 : && cfun->can_throw_non_call_exceptions))
6481 539884 : && operation_could_trap_p (code,
6482 11277 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6483 : false, NULL_TREE))
6484 : {
6485 11277 : tree lhs = gimple_cond_lhs (cond_stmt);
6486 11277 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6487 11277 : && TREE_CODE (lhs) == SSA_NAME
6488 11277 : && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6489 22554 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6490 : {
6491 11277 : gimple *s = SSA_NAME_DEF_STMT (lhs);
6492 11277 : if (is_gimple_assign (s)
6493 11277 : && gimple_assign_rhs_code (s) == code
6494 11277 : && operand_equal_p (gimple_assign_rhs1 (s), ops[0])
6495 22554 : && operand_equal_p (gimple_assign_rhs2 (s), ops[1]))
6496 : return false;
6497 : }
6498 : }
6499 517330 : tree res = maybe_push_res_to_seq (res_op, seq);
6500 517330 : if (!res)
6501 : return false;
6502 517330 : gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
6503 517330 : build_zero_cst (TREE_TYPE (res)));
6504 : }
6505 : else
6506 : return false;
6507 2320786 : if (dump_file && (dump_flags & TDF_DETAILS))
6508 : {
6509 857 : fprintf (dump_file, "gimple_simplified to ");
6510 857 : if (!gimple_seq_empty_p (*seq))
6511 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6512 857 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6513 : 0, TDF_SLIM);
6514 : }
6515 : // Mark the lhs of the new statements maybe for dce
6516 2320786 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6517 2320786 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6518 2320786 : return true;
6519 : }
6520 2209139 : else if (is_gimple_assign (stmt)
6521 2209139 : && res_op->code.is_tree_code ())
6522 : {
6523 2137080 : auto code = tree_code (res_op->code);
6524 2137080 : if (!inplace
6525 2137080 : || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
6526 : {
6527 2137080 : maybe_build_generic_op (res_op);
6528 5049916 : gimple_assign_set_rhs_with_ops (gsi, code,
6529 : res_op->op_or_null (0),
6530 : res_op->op_or_null (1),
6531 : res_op->op_or_null (2));
6532 2137080 : if (dump_file && (dump_flags & TDF_DETAILS))
6533 : {
6534 16612 : fprintf (dump_file, "gimple_simplified to ");
6535 16612 : if (!gimple_seq_empty_p (*seq))
6536 48 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6537 16612 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6538 : 0, TDF_SLIM);
6539 : }
6540 : // Mark the lhs of the new statements maybe for dce
6541 2137080 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6542 2137080 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6543 2137080 : return true;
6544 : }
6545 : }
6546 72059 : else if (res_op->code.is_fn_code ()
6547 72059 : && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
6548 : {
6549 8071 : gcc_assert (num_ops == gimple_call_num_args (stmt));
6550 23888 : for (unsigned int i = 0; i < num_ops; ++i)
6551 15817 : gimple_call_set_arg (stmt, i, ops[i]);
6552 8071 : if (dump_file && (dump_flags & TDF_DETAILS))
6553 : {
6554 0 : fprintf (dump_file, "gimple_simplified to ");
6555 0 : if (!gimple_seq_empty_p (*seq))
6556 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6557 0 : print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
6558 : }
6559 : // Mark the lhs of the new statements maybe for dce
6560 8071 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6561 8071 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6562 8071 : return true;
6563 : }
6564 63988 : else if (!inplace)
6565 : {
6566 125974 : if (gimple_has_lhs (stmt))
6567 : {
6568 63988 : tree lhs = gimple_get_lhs (stmt);
6569 63988 : if (!maybe_push_res_to_seq (res_op, seq, lhs))
6570 : return false;
6571 63005 : if (dump_file && (dump_flags & TDF_DETAILS))
6572 : {
6573 10 : fprintf (dump_file, "gimple_simplified to ");
6574 10 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6575 : }
6576 : // Mark the lhs of the new statements maybe for dce
6577 63005 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6578 63005 : gsi_replace_with_seq_vops (gsi, *seq);
6579 63005 : return true;
6580 : }
6581 : else
6582 0 : gcc_unreachable ();
6583 : }
6584 :
6585 : return false;
6586 : }
6587 :
6588 : /* Canonicalize MEM_REFs invariant address operand after propagation. */
6589 :
6590 : static bool
6591 186844830 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
6592 : {
6593 186844830 : bool res = false;
6594 186844830 : tree *orig_t = t;
6595 :
6596 186844830 : if (TREE_CODE (*t) == ADDR_EXPR)
6597 61234436 : t = &TREE_OPERAND (*t, 0);
6598 :
6599 : /* The C and C++ frontends use an ARRAY_REF for indexing with their
6600 : generic vector extension. The actual vector referenced is
6601 : view-converted to an array type for this purpose. If the index
6602 : is constant the canonical representation in the middle-end is a
6603 : BIT_FIELD_REF so re-write the former to the latter here. */
6604 186844830 : if (TREE_CODE (*t) == ARRAY_REF
6605 10704209 : && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
6606 160536 : && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
6607 186894141 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
6608 : {
6609 20030 : tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
6610 20030 : if (VECTOR_TYPE_P (vtype))
6611 : {
6612 20030 : tree low = array_ref_low_bound (*t);
6613 20030 : if (TREE_CODE (low) == INTEGER_CST)
6614 : {
6615 20030 : if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
6616 : {
6617 40016 : widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
6618 40016 : wi::to_widest (low));
6619 20008 : idx = wi::mul (idx, wi::to_widest
6620 40016 : (TYPE_SIZE (TREE_TYPE (*t))));
6621 20008 : widest_int ext
6622 20008 : = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
6623 20008 : if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
6624 : {
6625 39120 : *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
6626 19560 : TREE_TYPE (*t),
6627 19560 : TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
6628 19560 : TYPE_SIZE (TREE_TYPE (*t)),
6629 19560 : wide_int_to_tree (bitsizetype, idx));
6630 19560 : res = true;
6631 : }
6632 20008 : }
6633 : }
6634 : }
6635 : }
6636 :
6637 353100396 : while (handled_component_p (*t))
6638 166255566 : t = &TREE_OPERAND (*t, 0);
6639 :
6640 : /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
6641 : of invariant addresses into a SSA name MEM_REF address. */
6642 186844830 : if (TREE_CODE (*t) == MEM_REF
6643 186844830 : || TREE_CODE (*t) == TARGET_MEM_REF)
6644 : {
6645 98889527 : tree addr = TREE_OPERAND (*t, 0);
6646 98889527 : if (TREE_CODE (addr) == ADDR_EXPR
6647 98889527 : && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
6648 29951989 : || handled_component_p (TREE_OPERAND (addr, 0))))
6649 : {
6650 508970 : tree base;
6651 508970 : poly_int64 coffset;
6652 508970 : base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
6653 : &coffset);
6654 508970 : if (!base)
6655 : {
6656 18 : if (is_debug)
6657 18 : return false;
6658 0 : gcc_unreachable ();
6659 : }
6660 :
6661 508952 : TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
6662 508952 : TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
6663 508952 : TREE_OPERAND (*t, 1),
6664 508952 : size_int (coffset));
6665 508952 : res = true;
6666 : }
6667 98889509 : gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
6668 : || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
6669 : }
6670 :
6671 : /* Canonicalize back MEM_REFs to plain reference trees if the object
6672 : accessed is a decl that has the same access semantics as the MEM_REF. */
6673 186844812 : if (TREE_CODE (*t) == MEM_REF
6674 96928962 : && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
6675 29626118 : && integer_zerop (TREE_OPERAND (*t, 1))
6676 202965898 : && MR_DEPENDENCE_CLIQUE (*t) == 0)
6677 : {
6678 11410248 : tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6679 11410248 : tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
6680 11410248 : if (/* Same volatile qualification. */
6681 11410248 : TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
6682 : /* Same TBAA behavior with -fstrict-aliasing. */
6683 11407226 : && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
6684 11062405 : && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
6685 11062405 : == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
6686 : /* Same alignment. */
6687 4542049 : && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
6688 : /* We have to look out here to not drop a required conversion
6689 : from the rhs to the lhs if *t appears on the lhs or vice-versa
6690 : if it appears on the rhs. Thus require strict type
6691 : compatibility. */
6692 15672498 : && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
6693 : {
6694 2308945 : *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6695 2308945 : res = true;
6696 : }
6697 : }
6698 :
6699 175434564 : else if (TREE_CODE (*orig_t) == ADDR_EXPR
6700 58746393 : && TREE_CODE (*t) == MEM_REF
6701 195999262 : && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
6702 : {
6703 865 : tree base;
6704 865 : poly_int64 coffset;
6705 865 : base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
6706 : &coffset);
6707 865 : if (base)
6708 : {
6709 744 : gcc_assert (TREE_CODE (base) == MEM_REF);
6710 744 : poly_int64 moffset;
6711 744 : if (mem_ref_offset (base).to_shwi (&moffset))
6712 : {
6713 744 : coffset += moffset;
6714 744 : if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
6715 : {
6716 744 : coffset += moffset;
6717 744 : *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
6718 744 : return true;
6719 : }
6720 : }
6721 : }
6722 : }
6723 :
6724 : /* Canonicalize TARGET_MEM_REF in particular with respect to
6725 : the indexes becoming constant. */
6726 175433699 : else if (TREE_CODE (*t) == TARGET_MEM_REF)
6727 : {
6728 1960547 : tree tem = maybe_fold_tmr (*t);
6729 1960547 : if (tem)
6730 : {
6731 1796 : *t = tem;
6732 1796 : if (TREE_CODE (*orig_t) == ADDR_EXPR)
6733 0 : recompute_tree_invariant_for_addr_expr (*orig_t);
6734 : res = true;
6735 : }
6736 : }
6737 :
6738 : return res;
6739 : }
6740 :
6741 : /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
6742 : distinguishes both cases. */
6743 :
6744 : static bool
6745 746043115 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
6746 : bitmap dce_worklist = nullptr)
6747 : {
6748 746043115 : bool changed = false;
6749 746043115 : gimple *stmt = gsi_stmt (*gsi);
6750 746043115 : unsigned i;
6751 :
6752 : /* First do required canonicalization of [TARGET_]MEM_REF addresses
6753 : after propagation.
6754 : ??? This shouldn't be done in generic folding but in the
6755 : propagation helpers which also know whether an address was
6756 : propagated.
6757 : Also canonicalize operand order. */
6758 746043115 : switch (gimple_code (stmt))
6759 : {
6760 251836209 : case GIMPLE_ASSIGN:
6761 251836209 : if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
6762 : {
6763 165852372 : tree *rhs = gimple_assign_rhs1_ptr (stmt);
6764 165852372 : if ((REFERENCE_CLASS_P (*rhs)
6765 104906887 : || TREE_CODE (*rhs) == ADDR_EXPR)
6766 180447547 : && maybe_canonicalize_mem_ref_addr (rhs))
6767 : changed = true;
6768 165852372 : tree *lhs = gimple_assign_lhs_ptr (stmt);
6769 165852372 : if (REFERENCE_CLASS_P (*lhs)
6770 165852372 : && maybe_canonicalize_mem_ref_addr (lhs))
6771 : changed = true;
6772 : /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
6773 : This cannot be done in maybe_canonicalize_mem_ref_addr
6774 : as the gimple now has two operands rather than one.
6775 : The same reason why this can't be done in
6776 : maybe_canonicalize_mem_ref_addr is the same reason why
6777 : this can't be done inplace. */
6778 165852372 : if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
6779 : {
6780 14405383 : tree inner = TREE_OPERAND (*rhs, 0);
6781 14405383 : if (TREE_CODE (inner) == MEM_REF
6782 993741 : && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
6783 14468099 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6784 : {
6785 62716 : tree ptr = TREE_OPERAND (inner, 0);
6786 62716 : tree addon = TREE_OPERAND (inner, 1);
6787 62716 : addon = fold_convert (sizetype, addon);
6788 62716 : gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
6789 : ptr, addon);
6790 62716 : changed = true;
6791 62716 : stmt = gsi_stmt (*gsi);
6792 : }
6793 : }
6794 : }
6795 : else
6796 : {
6797 : /* Canonicalize operand order. */
6798 85983837 : enum tree_code code = gimple_assign_rhs_code (stmt);
6799 85983837 : if (TREE_CODE_CLASS (code) == tcc_comparison
6800 79965585 : || commutative_tree_code (code)
6801 129058750 : || commutative_ternary_tree_code (code))
6802 : {
6803 42909902 : tree rhs1 = gimple_assign_rhs1 (stmt);
6804 42909902 : tree rhs2 = gimple_assign_rhs2 (stmt);
6805 42909902 : if (tree_swap_operands_p (rhs1, rhs2))
6806 : {
6807 2564513 : gimple_assign_set_rhs1 (stmt, rhs2);
6808 2564513 : gimple_assign_set_rhs2 (stmt, rhs1);
6809 2564513 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6810 301774 : gimple_assign_set_rhs_code (stmt,
6811 : swap_tree_comparison (code));
6812 : changed = true;
6813 : }
6814 : }
6815 : }
6816 : break;
6817 55619385 : case GIMPLE_CALL:
6818 55619385 : {
6819 55619385 : gcall *call = as_a<gcall *> (stmt);
6820 166740510 : for (i = 0; i < gimple_call_num_args (call); ++i)
6821 : {
6822 111121125 : tree *arg = gimple_call_arg_ptr (call, i);
6823 111121125 : if (REFERENCE_CLASS_P (*arg)
6824 111121125 : && maybe_canonicalize_mem_ref_addr (arg))
6825 : changed = true;
6826 : }
6827 55619385 : tree *lhs = gimple_call_lhs_ptr (call);
6828 55619385 : if (*lhs
6829 22463356 : && REFERENCE_CLASS_P (*lhs)
6830 55736780 : && maybe_canonicalize_mem_ref_addr (lhs))
6831 : changed = true;
6832 55619385 : if (*lhs)
6833 : {
6834 22463356 : combined_fn cfn = gimple_call_combined_fn (call);
6835 22463356 : internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
6836 22463356 : int opno = first_commutative_argument (ifn);
6837 22463356 : if (opno >= 0)
6838 : {
6839 354390 : tree arg1 = gimple_call_arg (call, opno);
6840 354390 : tree arg2 = gimple_call_arg (call, opno + 1);
6841 354390 : if (tree_swap_operands_p (arg1, arg2))
6842 : {
6843 22414 : gimple_call_set_arg (call, opno, arg2);
6844 22414 : gimple_call_set_arg (call, opno + 1, arg1);
6845 22414 : changed = true;
6846 : }
6847 : }
6848 : }
6849 : break;
6850 : }
6851 587041 : case GIMPLE_ASM:
6852 587041 : {
6853 587041 : gasm *asm_stmt = as_a <gasm *> (stmt);
6854 1232184 : for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
6855 : {
6856 645143 : tree link = gimple_asm_output_op (asm_stmt, i);
6857 645143 : tree op = TREE_VALUE (link);
6858 645143 : if (REFERENCE_CLASS_P (op)
6859 645143 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6860 : changed = true;
6861 : }
6862 987505 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
6863 : {
6864 400464 : tree link = gimple_asm_input_op (asm_stmt, i);
6865 400464 : tree op = TREE_VALUE (link);
6866 400464 : if ((REFERENCE_CLASS_P (op)
6867 385962 : || TREE_CODE (op) == ADDR_EXPR)
6868 434109 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6869 : changed = true;
6870 : }
6871 : }
6872 : break;
6873 373766264 : case GIMPLE_DEBUG:
6874 373766264 : if (gimple_debug_bind_p (stmt))
6875 : {
6876 287739952 : tree *val = gimple_debug_bind_get_value_ptr (stmt);
6877 287739952 : if (*val
6878 164180487 : && (REFERENCE_CLASS_P (*val)
6879 161690792 : || TREE_CODE (*val) == ADDR_EXPR)
6880 336835263 : && maybe_canonicalize_mem_ref_addr (val, true))
6881 : changed = true;
6882 : }
6883 : break;
6884 44292288 : case GIMPLE_COND:
6885 44292288 : {
6886 : /* Canonicalize operand order. */
6887 44292288 : tree lhs = gimple_cond_lhs (stmt);
6888 44292288 : tree rhs = gimple_cond_rhs (stmt);
6889 44292288 : if (tree_swap_operands_p (lhs, rhs))
6890 : {
6891 1391268 : gcond *gc = as_a <gcond *> (stmt);
6892 1391268 : gimple_cond_set_lhs (gc, rhs);
6893 1391268 : gimple_cond_set_rhs (gc, lhs);
6894 1391268 : gimple_cond_set_code (gc,
6895 : swap_tree_comparison (gimple_cond_code (gc)));
6896 1391268 : changed = true;
6897 : }
6898 : }
6899 744565921 : default:;
6900 : }
6901 :
6902 : /* Dispatch to pattern-based folding. */
6903 744565921 : if (!inplace
6904 3103288 : || is_gimple_assign (stmt)
6905 745439623 : || gimple_code (stmt) == GIMPLE_COND)
6906 : {
6907 745169413 : gimple_seq seq = NULL;
6908 745169413 : gimple_match_op res_op;
6909 1488109240 : if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
6910 : valueize, valueize)
6911 745169413 : && replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
6912 : dce_worklist))
6913 : changed = true;
6914 : else
6915 740640471 : gimple_seq_discard (seq);
6916 : }
6917 :
6918 746043115 : stmt = gsi_stmt (*gsi);
6919 :
6920 : /* Fold the main computation performed by the statement. */
6921 746043115 : switch (gimple_code (stmt))
6922 : {
6923 251886311 : case GIMPLE_ASSIGN:
6924 251886311 : {
6925 : /* Try to canonicalize for boolean-typed X the comparisons
6926 : X == 0, X == 1, X != 0, and X != 1. */
6927 251886311 : if (gimple_assign_rhs_code (stmt) == EQ_EXPR
6928 251886311 : || gimple_assign_rhs_code (stmt) == NE_EXPR)
6929 : {
6930 3275676 : tree lhs = gimple_assign_lhs (stmt);
6931 3275676 : tree op1 = gimple_assign_rhs1 (stmt);
6932 3275676 : tree op2 = gimple_assign_rhs2 (stmt);
6933 3275676 : tree type = TREE_TYPE (op1);
6934 :
6935 : /* Check whether the comparison operands are of the same boolean
6936 : type as the result type is.
6937 : Check that second operand is an integer-constant with value
6938 : one or zero. */
6939 3275676 : if (TREE_CODE (op2) == INTEGER_CST
6940 2248750 : && (integer_zerop (op2) || integer_onep (op2))
6941 4976700 : && useless_type_conversion_p (TREE_TYPE (lhs), type))
6942 : {
6943 4649 : enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
6944 4649 : bool is_logical_not = false;
6945 :
6946 : /* X == 0 and X != 1 is a logical-not.of X
6947 : X == 1 and X != 0 is X */
6948 3920 : if ((cmp_code == EQ_EXPR && integer_zerop (op2))
6949 4649 : || (cmp_code == NE_EXPR && integer_onep (op2)))
6950 4607 : is_logical_not = true;
6951 :
6952 4649 : if (is_logical_not == false)
6953 42 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
6954 : /* Only for one-bit precision typed X the transformation
6955 : !X -> ~X is valid. */
6956 4607 : else if (TYPE_PRECISION (type) == 1)
6957 4607 : gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
6958 : /* Otherwise we use !X -> X ^ 1. */
6959 : else
6960 0 : gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
6961 : build_int_cst (type, 1));
6962 : changed = true;
6963 : break;
6964 : }
6965 : }
6966 :
6967 251881662 : unsigned old_num_ops = gimple_num_ops (stmt);
6968 251881662 : tree lhs = gimple_assign_lhs (stmt);
6969 251881662 : tree new_rhs = fold_gimple_assign (gsi);
6970 251881662 : if (new_rhs
6971 251995215 : && !useless_type_conversion_p (TREE_TYPE (lhs),
6972 113553 : TREE_TYPE (new_rhs)))
6973 0 : new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
6974 251881662 : if (new_rhs
6975 251881662 : && (!inplace
6976 987 : || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
6977 : {
6978 113553 : gimple_assign_set_rhs_from_tree (gsi, new_rhs);
6979 113553 : changed = true;
6980 : }
6981 : break;
6982 : }
6983 :
6984 55569283 : case GIMPLE_CALL:
6985 55569283 : changed |= gimple_fold_call (gsi, inplace);
6986 55569283 : break;
6987 :
6988 373766264 : case GIMPLE_DEBUG:
6989 373766264 : if (gimple_debug_bind_p (stmt))
6990 : {
6991 287739952 : tree val = gimple_debug_bind_get_value (stmt);
6992 287739952 : if (val && REFERENCE_CLASS_P (val))
6993 : {
6994 2487863 : tree tem = maybe_fold_reference (val);
6995 2487863 : if (tem)
6996 : {
6997 1601 : gimple_debug_bind_set_value (stmt, tem);
6998 1601 : changed = true;
6999 : }
7000 : }
7001 : }
7002 : break;
7003 :
7004 10612227 : case GIMPLE_RETURN:
7005 10612227 : {
7006 10612227 : greturn *ret_stmt = as_a<greturn *> (stmt);
7007 10612227 : tree ret = gimple_return_retval(ret_stmt);
7008 :
7009 10612227 : if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
7010 : {
7011 4453993 : tree val = valueize (ret);
7012 4453993 : if (val && val != ret
7013 4453993 : && may_propagate_copy (ret, val))
7014 : {
7015 0 : gimple_return_set_retval (ret_stmt, val);
7016 0 : changed = true;
7017 : }
7018 : }
7019 : }
7020 : break;
7021 :
7022 746043115 : default:;
7023 : }
7024 :
7025 746043115 : return changed;
7026 : }
7027 :
7028 : /* Valueziation callback that ends up not following SSA edges. */
7029 :
7030 : tree
7031 5715648100 : no_follow_ssa_edges (tree)
7032 : {
7033 5715648100 : return NULL_TREE;
7034 : }
7035 :
7036 : /* Valueization callback that ends up following single-use SSA edges only. */
7037 :
7038 : tree
7039 877922567 : follow_single_use_edges (tree val)
7040 : {
7041 877922567 : if (TREE_CODE (val) == SSA_NAME
7042 877922567 : && !has_single_use (val))
7043 448988950 : return NULL_TREE;
7044 : return val;
7045 : }
7046 :
7047 : /* Valueization callback that follows all SSA edges. */
7048 :
7049 : tree
7050 197580895 : follow_all_ssa_edges (tree val)
7051 : {
7052 197580895 : return val;
7053 : }
7054 :
7055 : /* Fold the statement pointed to by GSI. In some cases, this function may
7056 : replace the whole statement with a new one. Returns true iff folding
7057 : makes any changes.
7058 : The statement pointed to by GSI should be in valid gimple form but may
7059 : be in unfolded state as resulting from for example constant propagation
7060 : which can produce *&x = 0. */
7061 :
7062 : bool
7063 148880504 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
7064 : {
7065 148880504 : return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
7066 : }
7067 :
7068 : bool
7069 594059323 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
7070 : {
7071 594059323 : return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
7072 : }
7073 :
7074 : /* Perform the minimal folding on statement *GSI. Only operations like
7075 : *&x created by constant propagation are handled. The statement cannot
7076 : be replaced with a new one. Return true if the statement was
7077 : changed, false otherwise.
7078 : The statement *GSI should be in valid gimple form but may
7079 : be in unfolded state as resulting from for example constant propagation
7080 : which can produce *&x = 0. */
7081 :
7082 : bool
7083 3103288 : fold_stmt_inplace (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
7084 : {
7085 3103288 : gimple *stmt = gsi_stmt (*gsi);
7086 3103288 : bool changed = fold_stmt_1 (gsi, true, valueize);
7087 3103288 : gcc_assert (gsi_stmt (*gsi) == stmt);
7088 3103288 : return changed;
7089 : }
7090 :
7091 : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
7092 : if EXPR is null or we don't know how.
7093 : If non-null, the result always has boolean type. */
7094 :
7095 : static tree
7096 263418 : canonicalize_bool (tree expr, bool invert)
7097 : {
7098 263418 : if (!expr)
7099 : return NULL_TREE;
7100 50 : else if (invert)
7101 : {
7102 36 : if (integer_nonzerop (expr))
7103 0 : return boolean_false_node;
7104 36 : else if (integer_zerop (expr))
7105 0 : return boolean_true_node;
7106 36 : else if (TREE_CODE (expr) == SSA_NAME)
7107 0 : return fold_build2 (EQ_EXPR, boolean_type_node, expr,
7108 : build_int_cst (TREE_TYPE (expr), 0));
7109 36 : else if (COMPARISON_CLASS_P (expr))
7110 36 : return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
7111 : boolean_type_node,
7112 : TREE_OPERAND (expr, 0),
7113 : TREE_OPERAND (expr, 1));
7114 : else
7115 : return NULL_TREE;
7116 : }
7117 : else
7118 : {
7119 14 : if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
7120 : return expr;
7121 0 : if (integer_nonzerop (expr))
7122 0 : return boolean_true_node;
7123 0 : else if (integer_zerop (expr))
7124 0 : return boolean_false_node;
7125 0 : else if (TREE_CODE (expr) == SSA_NAME)
7126 0 : return fold_build2 (NE_EXPR, boolean_type_node, expr,
7127 : build_int_cst (TREE_TYPE (expr), 0));
7128 0 : else if (COMPARISON_CLASS_P (expr))
7129 0 : return fold_build2 (TREE_CODE (expr),
7130 : boolean_type_node,
7131 : TREE_OPERAND (expr, 0),
7132 : TREE_OPERAND (expr, 1));
7133 : else
7134 : return NULL_TREE;
7135 : }
7136 : }
7137 :
7138 : /* Check to see if a boolean expression EXPR is logically equivalent to the
7139 : comparison (OP1 CODE OP2). Check for various identities involving
7140 : SSA_NAMEs. */
7141 :
7142 : static bool
7143 1418 : same_bool_comparison_p (const_tree expr, enum tree_code code,
7144 : const_tree op1, const_tree op2)
7145 : {
7146 1418 : gimple *s;
7147 :
7148 : /* The obvious case. */
7149 1418 : if (TREE_CODE (expr) == code
7150 33 : && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
7151 1451 : && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
7152 : return true;
7153 :
7154 : /* Check for comparing (name, name != 0) and the case where expr
7155 : is an SSA_NAME with a definition matching the comparison. */
7156 1401 : if (TREE_CODE (expr) == SSA_NAME
7157 1401 : && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
7158 : {
7159 0 : if (operand_equal_p (expr, op1, 0))
7160 0 : return ((code == NE_EXPR && integer_zerop (op2))
7161 0 : || (code == EQ_EXPR && integer_nonzerop (op2)));
7162 0 : s = SSA_NAME_DEF_STMT (expr);
7163 0 : if (is_gimple_assign (s)
7164 0 : && gimple_assign_rhs_code (s) == code
7165 0 : && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
7166 0 : && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
7167 : return true;
7168 : }
7169 :
7170 : /* If op1 is of the form (name != 0) or (name == 0), and the definition
7171 : of name is a comparison, recurse. */
7172 1401 : if (TREE_CODE (op1) == SSA_NAME
7173 1401 : && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
7174 : {
7175 448 : s = SSA_NAME_DEF_STMT (op1);
7176 448 : if (is_gimple_assign (s)
7177 448 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
7178 : {
7179 0 : enum tree_code c = gimple_assign_rhs_code (s);
7180 0 : if ((c == NE_EXPR && integer_zerop (op2))
7181 0 : || (c == EQ_EXPR && integer_nonzerop (op2)))
7182 0 : return same_bool_comparison_p (expr, c,
7183 0 : gimple_assign_rhs1 (s),
7184 0 : gimple_assign_rhs2 (s));
7185 0 : if ((c == EQ_EXPR && integer_zerop (op2))
7186 0 : || (c == NE_EXPR && integer_nonzerop (op2)))
7187 0 : return same_bool_comparison_p (expr,
7188 : invert_tree_comparison (c, false),
7189 0 : gimple_assign_rhs1 (s),
7190 0 : gimple_assign_rhs2 (s));
7191 : }
7192 : }
7193 : return false;
7194 : }
7195 :
7196 : /* Check to see if two boolean expressions OP1 and OP2 are logically
7197 : equivalent. */
7198 :
7199 : static bool
7200 15 : same_bool_result_p (const_tree op1, const_tree op2)
7201 : {
7202 : /* Simple cases first. */
7203 15 : if (operand_equal_p (op1, op2, 0))
7204 : return true;
7205 :
7206 : /* Check the cases where at least one of the operands is a comparison.
7207 : These are a bit smarter than operand_equal_p in that they apply some
7208 : identifies on SSA_NAMEs. */
7209 8 : if (COMPARISON_CLASS_P (op2)
7210 16 : && same_bool_comparison_p (op1, TREE_CODE (op2),
7211 8 : TREE_OPERAND (op2, 0),
7212 8 : TREE_OPERAND (op2, 1)))
7213 : return true;
7214 8 : if (COMPARISON_CLASS_P (op1)
7215 16 : && same_bool_comparison_p (op2, TREE_CODE (op1),
7216 8 : TREE_OPERAND (op1, 0),
7217 8 : TREE_OPERAND (op1, 1)))
7218 : return true;
7219 :
7220 : /* Default case. */
7221 : return false;
7222 : }
7223 :
7224 : /* Forward declarations for some mutually recursive functions. */
7225 :
7226 : static tree
7227 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7228 : enum tree_code code2, tree op2a, tree op2b, basic_block);
7229 : static tree
7230 : and_var_with_comparison (tree type, tree var, bool invert,
7231 : enum tree_code code2, tree op2a, tree op2b,
7232 : basic_block);
7233 : static tree
7234 : and_var_with_comparison_1 (tree type, gimple *stmt,
7235 : enum tree_code code2, tree op2a, tree op2b,
7236 : basic_block);
7237 : static tree
7238 : or_comparisons_1 (tree, enum tree_code code1, tree op1a, tree op1b,
7239 : enum tree_code code2, tree op2a, tree op2b,
7240 : basic_block);
7241 : static tree
7242 : or_var_with_comparison (tree, tree var, bool invert,
7243 : enum tree_code code2, tree op2a, tree op2b,
7244 : basic_block);
7245 : static tree
7246 : or_var_with_comparison_1 (tree, gimple *stmt,
7247 : enum tree_code code2, tree op2a, tree op2b,
7248 : basic_block);
7249 :
7250 : /* Helper function for and_comparisons_1: try to simplify the AND of the
7251 : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
7252 : If INVERT is true, invert the value of the VAR before doing the AND.
7253 : Return NULL_EXPR if we can't simplify this to a single expression. */
7254 :
7255 : static tree
7256 224190 : and_var_with_comparison (tree type, tree var, bool invert,
7257 : enum tree_code code2, tree op2a, tree op2b,
7258 : basic_block outer_cond_bb)
7259 : {
7260 224190 : tree t;
7261 224190 : gimple *stmt = SSA_NAME_DEF_STMT (var);
7262 :
7263 : /* We can only deal with variables whose definitions are assignments. */
7264 224190 : if (!is_gimple_assign (stmt))
7265 : return NULL_TREE;
7266 :
7267 : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
7268 : !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
7269 : Then we only have to consider the simpler non-inverted cases. */
7270 223788 : if (invert)
7271 83146 : t = or_var_with_comparison_1 (type, stmt,
7272 : invert_tree_comparison (code2, false),
7273 : op2a, op2b, outer_cond_bb);
7274 : else
7275 140642 : t = and_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
7276 : outer_cond_bb);
7277 223788 : return canonicalize_bool (t, invert);
7278 : }
7279 :
7280 : /* Try to simplify the AND of the ssa variable defined by the assignment
7281 : STMT with the comparison specified by (OP2A CODE2 OP2B).
7282 : Return NULL_EXPR if we can't simplify this to a single expression. */
7283 :
7284 : static tree
7285 161160 : and_var_with_comparison_1 (tree type, gimple *stmt,
7286 : enum tree_code code2, tree op2a, tree op2b,
7287 : basic_block outer_cond_bb)
7288 : {
7289 161160 : tree var = gimple_assign_lhs (stmt);
7290 161160 : tree true_test_var = NULL_TREE;
7291 161160 : tree false_test_var = NULL_TREE;
7292 161160 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
7293 :
7294 : /* Check for identities like (var AND (var == 0)) => false. */
7295 161160 : if (TREE_CODE (op2a) == SSA_NAME
7296 161160 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
7297 : {
7298 8677 : if ((code2 == NE_EXPR && integer_zerop (op2b))
7299 22680 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
7300 : {
7301 8072 : true_test_var = op2a;
7302 8072 : if (var == true_test_var)
7303 : return var;
7304 : }
7305 3871 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
7306 14898 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
7307 : {
7308 3098 : false_test_var = op2a;
7309 3098 : if (var == false_test_var)
7310 0 : return boolean_false_node;
7311 : }
7312 : }
7313 :
7314 : /* If the definition is a comparison, recurse on it. */
7315 161160 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
7316 : {
7317 1942 : tree t = and_comparisons_1 (type, innercode,
7318 : gimple_assign_rhs1 (stmt),
7319 : gimple_assign_rhs2 (stmt),
7320 : code2,
7321 : op2a,
7322 : op2b, outer_cond_bb);
7323 1942 : if (t)
7324 : return t;
7325 : }
7326 :
7327 : /* If the definition is an AND or OR expression, we may be able to
7328 : simplify by reassociating. */
7329 161154 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
7330 161154 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
7331 : {
7332 14448 : tree inner1 = gimple_assign_rhs1 (stmt);
7333 14448 : tree inner2 = gimple_assign_rhs2 (stmt);
7334 14448 : gimple *s;
7335 14448 : tree t;
7336 14448 : tree partial = NULL_TREE;
7337 14448 : bool is_and = (innercode == BIT_AND_EXPR);
7338 :
7339 : /* Check for boolean identities that don't require recursive examination
7340 : of inner1/inner2:
7341 : inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
7342 : inner1 AND (inner1 OR inner2) => inner1
7343 : !inner1 AND (inner1 AND inner2) => false
7344 : !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
7345 : Likewise for similar cases involving inner2. */
7346 14448 : if (inner1 == true_test_var)
7347 0 : return (is_and ? var : inner1);
7348 14448 : else if (inner2 == true_test_var)
7349 0 : return (is_and ? var : inner2);
7350 14448 : else if (inner1 == false_test_var)
7351 0 : return (is_and
7352 0 : ? boolean_false_node
7353 0 : : and_var_with_comparison (type, inner2, false, code2, op2a,
7354 0 : op2b, outer_cond_bb));
7355 14448 : else if (inner2 == false_test_var)
7356 0 : return (is_and
7357 0 : ? boolean_false_node
7358 0 : : and_var_with_comparison (type, inner1, false, code2, op2a,
7359 0 : op2b, outer_cond_bb));
7360 :
7361 : /* Next, redistribute/reassociate the AND across the inner tests.
7362 : Compute the first partial result, (inner1 AND (op2a code op2b)) */
7363 14448 : if (TREE_CODE (inner1) == SSA_NAME
7364 14448 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
7365 13656 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7366 26946 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7367 : gimple_assign_rhs1 (s),
7368 : gimple_assign_rhs2 (s),
7369 : code2, op2a, op2b,
7370 : outer_cond_bb)))
7371 : {
7372 : /* Handle the AND case, where we are reassociating:
7373 : (inner1 AND inner2) AND (op2a code2 op2b)
7374 : => (t AND inner2)
7375 : If the partial result t is a constant, we win. Otherwise
7376 : continue on to try reassociating with the other inner test. */
7377 34 : if (is_and)
7378 : {
7379 5 : if (integer_onep (t))
7380 : return inner2;
7381 5 : else if (integer_zerop (t))
7382 0 : return boolean_false_node;
7383 : }
7384 :
7385 : /* Handle the OR case, where we are redistributing:
7386 : (inner1 OR inner2) AND (op2a code2 op2b)
7387 : => (t OR (inner2 AND (op2a code2 op2b))) */
7388 29 : else if (integer_onep (t))
7389 0 : return boolean_true_node;
7390 :
7391 : /* Save partial result for later. */
7392 : partial = t;
7393 : }
7394 :
7395 : /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
7396 14448 : if (TREE_CODE (inner2) == SSA_NAME
7397 14448 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
7398 14068 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7399 27612 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7400 : gimple_assign_rhs1 (s),
7401 : gimple_assign_rhs2 (s),
7402 : code2, op2a, op2b,
7403 : outer_cond_bb)))
7404 : {
7405 : /* Handle the AND case, where we are reassociating:
7406 : (inner1 AND inner2) AND (op2a code2 op2b)
7407 : => (inner1 AND t) */
7408 480 : if (is_and)
7409 : {
7410 25 : if (integer_onep (t))
7411 : return inner1;
7412 25 : else if (integer_zerop (t))
7413 1 : return boolean_false_node;
7414 : /* If both are the same, we can apply the identity
7415 : (x AND x) == x. */
7416 24 : else if (partial && same_bool_result_p (t, partial))
7417 : return t;
7418 : }
7419 :
7420 : /* Handle the OR case. where we are redistributing:
7421 : (inner1 OR inner2) AND (op2a code2 op2b)
7422 : => (t OR (inner1 AND (op2a code2 op2b)))
7423 : => (t OR partial) */
7424 : else
7425 : {
7426 455 : if (integer_onep (t))
7427 0 : return boolean_true_node;
7428 455 : else if (partial)
7429 : {
7430 : /* We already got a simplification for the other
7431 : operand to the redistributed OR expression. The
7432 : interesting case is when at least one is false.
7433 : Or, if both are the same, we can apply the identity
7434 : (x OR x) == x. */
7435 6 : if (integer_zerop (partial))
7436 : return t;
7437 6 : else if (integer_zerop (t))
7438 : return partial;
7439 4 : else if (same_bool_result_p (t, partial))
7440 : return t;
7441 : }
7442 : }
7443 : }
7444 : }
7445 : return NULL_TREE;
7446 : }
7447 :
7448 : /* Try to simplify the AND of two comparisons defined by
7449 : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
7450 : If this can be done without constructing an intermediate value,
7451 : return the resulting tree; otherwise NULL_TREE is returned.
7452 : This function is deliberately asymmetric as it recurses on SSA_DEFs
7453 : in the first comparison but not the second. */
7454 :
7455 : static tree
7456 823413 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7457 : enum tree_code code2, tree op2a, tree op2b,
7458 : basic_block outer_cond_bb)
7459 : {
7460 823413 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
7461 :
7462 : /* First check for ((x CODE1 y) AND (x CODE2 y)). */
7463 823413 : if (operand_equal_p (op1a, op2a, 0)
7464 823413 : && operand_equal_p (op1b, op2b, 0))
7465 : {
7466 : /* Result will be either NULL_TREE, or a combined comparison. */
7467 4115 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7468 : TRUTH_ANDIF_EXPR, code1, code2,
7469 : truth_type, op1a, op1b);
7470 4115 : if (t)
7471 : return t;
7472 : }
7473 :
7474 : /* Likewise the swapped case of the above. */
7475 822142 : if (operand_equal_p (op1a, op2b, 0)
7476 822142 : && operand_equal_p (op1b, op2a, 0))
7477 : {
7478 : /* Result will be either NULL_TREE, or a combined comparison. */
7479 29 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7480 : TRUTH_ANDIF_EXPR, code1,
7481 : swap_tree_comparison (code2),
7482 : truth_type, op1a, op1b);
7483 29 : if (t)
7484 : return t;
7485 : }
7486 :
7487 : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
7488 : NAME's definition is a truth value. See if there are any simplifications
7489 : that can be done against the NAME's definition. */
7490 822141 : if (TREE_CODE (op1a) == SSA_NAME
7491 822117 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
7492 1399961 : && (integer_zerop (op1b) || integer_onep (op1b)))
7493 : {
7494 118378 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
7495 301455 : || (code1 == NE_EXPR && integer_onep (op1b)));
7496 277057 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
7497 277057 : switch (gimple_code (stmt))
7498 : {
7499 221619 : case GIMPLE_ASSIGN:
7500 : /* Try to simplify by copy-propagating the definition. */
7501 221619 : return and_var_with_comparison (type, op1a, invert, code2, op2a,
7502 221619 : op2b, outer_cond_bb);
7503 :
7504 26285 : case GIMPLE_PHI:
7505 : /* If every argument to the PHI produces the same result when
7506 : ANDed with the second comparison, we win.
7507 : Do not do this unless the type is bool since we need a bool
7508 : result here anyway. */
7509 26285 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
7510 : {
7511 : tree result = NULL_TREE;
7512 : unsigned i;
7513 10236 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
7514 : {
7515 10236 : tree arg = gimple_phi_arg_def (stmt, i);
7516 :
7517 : /* If this PHI has itself as an argument, ignore it.
7518 : If all the other args produce the same result,
7519 : we're still OK. */
7520 10236 : if (arg == gimple_phi_result (stmt))
7521 0 : continue;
7522 10236 : else if (TREE_CODE (arg) == INTEGER_CST)
7523 : {
7524 6764 : if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
7525 : {
7526 3777 : if (!result)
7527 1738 : result = boolean_false_node;
7528 2039 : else if (!integer_zerop (result))
7529 : return NULL_TREE;
7530 : }
7531 2987 : else if (!result)
7532 1791 : result = fold_build2 (code2, boolean_type_node,
7533 : op2a, op2b);
7534 1196 : else if (!same_bool_comparison_p (result,
7535 : code2, op2a, op2b))
7536 : return NULL_TREE;
7537 : }
7538 3472 : else if (TREE_CODE (arg) == SSA_NAME
7539 3472 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
7540 : {
7541 3469 : tree temp;
7542 3469 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
7543 : /* In simple cases we can look through PHI nodes,
7544 : but we have to be careful with loops.
7545 : See PR49073. */
7546 3469 : if (! dom_info_available_p (CDI_DOMINATORS)
7547 3469 : || gimple_bb (def_stmt) == gimple_bb (stmt)
7548 6938 : || dominated_by_p (CDI_DOMINATORS,
7549 3469 : gimple_bb (def_stmt),
7550 3469 : gimple_bb (stmt)))
7551 898 : return NULL_TREE;
7552 2571 : temp = and_var_with_comparison (type, arg, invert, code2,
7553 : op2a, op2b,
7554 : outer_cond_bb);
7555 2571 : if (!temp)
7556 : return NULL_TREE;
7557 0 : else if (!result)
7558 : result = temp;
7559 0 : else if (!same_bool_result_p (result, temp))
7560 : return NULL_TREE;
7561 : }
7562 : else
7563 : return NULL_TREE;
7564 : }
7565 : return result;
7566 : }
7567 :
7568 : default:
7569 : break;
7570 : }
7571 : }
7572 : return NULL_TREE;
7573 : }
7574 :
7575 : static basic_block fosa_bb;
7576 : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
7577 : static tree
7578 32220692 : follow_outer_ssa_edges (tree val)
7579 : {
7580 32220692 : if (TREE_CODE (val) == SSA_NAME
7581 32220692 : && !SSA_NAME_IS_DEFAULT_DEF (val))
7582 : {
7583 31741153 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
7584 31741153 : if (!def_bb
7585 9357440 : || def_bb == fosa_bb
7586 36877636 : || (dom_info_available_p (CDI_DOMINATORS)
7587 5136483 : && (def_bb == fosa_bb
7588 5136483 : || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
7589 29029932 : return val;
7590 : /* We cannot temporarily rewrite stmts with undefined overflow
7591 : behavior, so avoid expanding them. */
7592 5403915 : if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
7593 258747 : || POINTER_TYPE_P (TREE_TYPE (val)))
7594 5299309 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
7595 : return NULL_TREE;
7596 1085324 : flow_sensitive_info_storage storage;
7597 1085324 : storage.save_and_clear (val);
7598 : /* If the definition does not dominate fosa_bb temporarily reset
7599 : flow-sensitive info. */
7600 1085324 : fosa_unwind->safe_push (std::make_pair (val, storage));
7601 1085324 : return val;
7602 : }
7603 : return val;
7604 : }
7605 :
7606 : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
7607 : : try to simplify the AND/OR of the ssa variable VAR with the comparison
7608 : specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
7609 : simplify this to a single expression. As we are going to lower the cost
7610 : of building SSA names / gimple stmts significantly, we need to allocate
7611 : them ont the stack. This will cause the code to be a bit ugly. */
7612 :
7613 : static tree
7614 889998 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
7615 : enum tree_code code1,
7616 : tree op1a, tree op1b,
7617 : enum tree_code code2, tree op2a,
7618 : tree op2b,
7619 : basic_block outer_cond_bb)
7620 : {
7621 : /* Allocate gimple stmt1 on the stack. */
7622 889998 : gassign *stmt1
7623 889998 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7624 889998 : gimple_init (stmt1, GIMPLE_ASSIGN, 3);
7625 889998 : gimple_assign_set_rhs_code (stmt1, code1);
7626 889998 : gimple_assign_set_rhs1 (stmt1, op1a);
7627 889998 : gimple_assign_set_rhs2 (stmt1, op1b);
7628 889998 : gimple_set_bb (stmt1, NULL);
7629 :
7630 : /* Allocate gimple stmt2 on the stack. */
7631 889998 : gassign *stmt2
7632 889998 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7633 889998 : gimple_init (stmt2, GIMPLE_ASSIGN, 3);
7634 889998 : gimple_assign_set_rhs_code (stmt2, code2);
7635 889998 : gimple_assign_set_rhs1 (stmt2, op2a);
7636 889998 : gimple_assign_set_rhs2 (stmt2, op2b);
7637 889998 : gimple_set_bb (stmt2, NULL);
7638 :
7639 : /* Allocate SSA names(lhs1) on the stack. */
7640 889998 : alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
7641 889998 : tree lhs1 = (tree) &lhs1buf[0];
7642 889998 : memset (lhs1, 0, sizeof (tree_ssa_name));
7643 889998 : TREE_SET_CODE (lhs1, SSA_NAME);
7644 889998 : TREE_TYPE (lhs1) = type;
7645 889998 : init_ssa_name_imm_use (lhs1);
7646 :
7647 : /* Allocate SSA names(lhs2) on the stack. */
7648 889998 : alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
7649 889998 : tree lhs2 = (tree) &lhs2buf[0];
7650 889998 : memset (lhs2, 0, sizeof (tree_ssa_name));
7651 889998 : TREE_SET_CODE (lhs2, SSA_NAME);
7652 889998 : TREE_TYPE (lhs2) = type;
7653 889998 : init_ssa_name_imm_use (lhs2);
7654 :
7655 889998 : gimple_assign_set_lhs (stmt1, lhs1);
7656 889998 : gimple_assign_set_lhs (stmt2, lhs2);
7657 :
7658 889998 : gimple_match_op op (gimple_match_cond::UNCOND, code,
7659 : type, gimple_assign_lhs (stmt1),
7660 889998 : gimple_assign_lhs (stmt2));
7661 889998 : fosa_bb = outer_cond_bb;
7662 889998 : auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
7663 889998 : fosa_unwind = &unwind_stack;
7664 1234553 : if (op.resimplify (NULL, (!outer_cond_bb
7665 : ? follow_all_ssa_edges : follow_outer_ssa_edges)))
7666 : {
7667 2437 : fosa_unwind = NULL;
7668 8710 : for (auto p : unwind_stack)
7669 1399 : p.second.restore (p.first);
7670 2437 : if (gimple_simplified_result_is_gimple_val (&op))
7671 : {
7672 1832 : tree res = op.ops[0];
7673 1832 : if (res == lhs1)
7674 1464 : return build2 (code1, type, op1a, op1b);
7675 368 : else if (res == lhs2)
7676 326 : return build2 (code2, type, op2a, op2b);
7677 : else
7678 : return res;
7679 : }
7680 605 : else if (op.code.is_tree_code ()
7681 605 : && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
7682 : {
7683 605 : tree op0 = op.ops[0];
7684 605 : tree op1 = op.ops[1];
7685 605 : if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
7686 : return NULL_TREE; /* not simple */
7687 :
7688 605 : return build2 ((enum tree_code)op.code, op.type, op0, op1);
7689 : }
7690 : }
7691 887561 : fosa_unwind = NULL;
7692 3746608 : for (auto p : unwind_stack)
7693 1083925 : p.second.restore (p.first);
7694 :
7695 887561 : return NULL_TREE;
7696 889998 : }
7697 :
7698 : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
7699 : conversion. Reject loads if LOAD is NULL, otherwise set *LOAD if a
7700 : converting load is found. */
7701 :
7702 : static bool
7703 1785290 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
7704 : {
7705 1785290 : bool ret = false;
7706 :
7707 1785290 : if (TREE_CODE (t) == SSA_NAME
7708 1785290 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7709 987331 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7710 : {
7711 919401 : bool load_p = gimple_assign_load_p (def);
7712 919401 : if (load_p && !load)
7713 : return false;
7714 904017 : switch (gimple_assign_rhs_code (def))
7715 : {
7716 9030 : CASE_CONVERT:
7717 9030 : op[0] = gimple_assign_rhs1 (def);
7718 9030 : ret = true;
7719 9030 : break;
7720 :
7721 2289 : case VIEW_CONVERT_EXPR:
7722 2289 : op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
7723 2289 : ret = true;
7724 2289 : break;
7725 :
7726 : default:
7727 : break;
7728 : }
7729 :
7730 11319 : if (ret && load_p)
7731 0 : *load = def;
7732 : }
7733 :
7734 : return ret;
7735 : }
7736 :
7737 : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
7738 : binary expression with code CODE. */
7739 :
7740 : static bool
7741 1807204 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
7742 : {
7743 1807204 : if (TREE_CODE (t) == SSA_NAME
7744 1807204 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7745 1008212 : if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7746 938541 : if (gimple_assign_rhs_code (def) == code)
7747 : {
7748 36882 : op[0] = gimple_assign_rhs1 (def);
7749 36882 : op[1] = gimple_assign_rhs2 (def);
7750 36882 : return true;
7751 : }
7752 : return false;
7753 : }
7754 : /* Subroutine for fold_truth_andor_1: decode a field reference.
7755 :
7756 : If *PEXP is a comparison reference, we return the innermost reference.
7757 :
7758 : *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
7759 : set to the starting bit number.
7760 :
7761 : *PVOLATILEP is set to 1 if the any expression encountered is volatile;
7762 : otherwise it is not changed.
7763 :
7764 : *PUNSIGNEDP is set to the signedness of the field.
7765 :
7766 : *PREVERSEP is set to the storage order of the field.
7767 :
7768 : *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. If
7769 : *PAND_MASK is initially set to a mask with nonzero precision, that mask is
7770 : combined with the found mask, or adjusted in precision to match.
7771 :
7772 : *PSIGNBIT is set to TRUE if, before clipping to *PBITSIZE, the mask
7773 : encompassed bits that corresponded to extensions of the sign bit.
7774 :
7775 : *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
7776 : case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
7777 : *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
7778 : and the left-hand operand of the XOR will be decoded. If *PXORP is TRUE,
7779 : PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
7780 : right-hand operand of the XOR will be decoded.
7781 :
7782 : *LOAD is set to the load stmt of the innermost reference, if any,
7783 : *and NULL otherwise.
7784 :
7785 : LOC[0..3] are filled in as conversion, masking, shifting and loading
7786 : operations are located.
7787 :
7788 : Return 0 if this is not a component reference or is one that we can't
7789 : do anything with. */
7790 :
7791 : static tree
7792 637692 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
7793 : HOST_WIDE_INT *pbitpos,
7794 : bool *punsignedp, bool *preversep, bool *pvolatilep,
7795 : wide_int *pand_mask, bool *psignbit,
7796 : bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
7797 : gimple **pload, location_t loc[4])
7798 : {
7799 637692 : tree exp = *pexp;
7800 637692 : tree outer_type = 0;
7801 637692 : wide_int and_mask;
7802 637692 : tree inner, offset;
7803 637692 : int shiftrt = 0;
7804 637692 : tree res_ops[2];
7805 637692 : machine_mode mode;
7806 637692 : bool convert_before_shift = false;
7807 637692 : bool signbit = false;
7808 637692 : bool xorp = false;
7809 637692 : tree xor_cmp_op;
7810 637692 : wide_int xor_and_mask;
7811 637692 : gimple *load = NULL;
7812 :
7813 : /* All the optimizations using this function assume integer fields.
7814 : There are problems with FP fields since the type_for_size call
7815 : below can fail for, e.g., XFmode. */
7816 637692 : if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
7817 : return NULL_TREE;
7818 :
7819 : /* Drop casts, saving only the outermost type, effectively used in
7820 : the compare. We can deal with at most one conversion, and it may
7821 : appear at various points in the chain of recognized preparation
7822 : statements. Earlier optimizers will often have already dropped
7823 : unneeded extensions, but they may survive, as in PR118046. ???
7824 : Can we do better and allow multiple conversions, perhaps taking
7825 : note of the narrowest intermediate type, sign extensions and
7826 : whatnot? */
7827 602402 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7828 : {
7829 10603 : outer_type = TREE_TYPE (exp);
7830 10603 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7831 10603 : exp = res_ops[0];
7832 : }
7833 :
7834 : /* Recognize and save a masking operation. Combine it with an
7835 : incoming mask. */
7836 602402 : if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
7837 602402 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7838 : {
7839 22595 : loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
7840 22595 : exp = res_ops[0];
7841 22595 : and_mask = wi::to_wide (res_ops[1]);
7842 22595 : unsigned prec_in = pand_mask->get_precision ();
7843 22595 : if (prec_in)
7844 : {
7845 52 : unsigned prec_op = and_mask.get_precision ();
7846 52 : if (prec_in >= prec_op)
7847 : {
7848 52 : if (prec_in > prec_op)
7849 0 : and_mask = wide_int::from (and_mask, prec_in, UNSIGNED);
7850 52 : and_mask &= *pand_mask;
7851 : }
7852 : else
7853 0 : and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
7854 : }
7855 : }
7856 : else
7857 579807 : and_mask = *pand_mask;
7858 :
7859 : /* Turn (a ^ b) [!]= 0 into a [!]= b. */
7860 602402 : if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
7861 : {
7862 : /* No location recorded for this one, it's entirely subsumed by the
7863 : compare. */
7864 8348 : if (*pxorp)
7865 : {
7866 4171 : exp = res_ops[1];
7867 4171 : gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
7868 : }
7869 4177 : else if (!pxor_cmp_op)
7870 : /* Not much we can do when xor appears in the right-hand compare
7871 : operand. */
7872 : return NULL_TREE;
7873 4175 : else if (integer_zerop (*pxor_cmp_op))
7874 : {
7875 4171 : xorp = true;
7876 4171 : exp = res_ops[0];
7877 4171 : xor_cmp_op = *pexp;
7878 4171 : xor_and_mask = *pand_mask;
7879 : }
7880 : }
7881 :
7882 : /* Another chance to drop conversions. */
7883 602400 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7884 : {
7885 706 : outer_type = TREE_TYPE (exp);
7886 706 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7887 706 : exp = res_ops[0];
7888 : }
7889 :
7890 : /* Take note of shifts. */
7891 602400 : if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
7892 602400 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7893 : {
7894 258 : loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
7895 258 : exp = res_ops[0];
7896 258 : if (!tree_fits_shwi_p (res_ops[1]))
7897 : return NULL_TREE;
7898 258 : shiftrt = tree_to_shwi (res_ops[1]);
7899 258 : if (shiftrt <= 0)
7900 : return NULL_TREE;
7901 : }
7902 :
7903 : /* Yet another chance to drop conversions. This one is allowed to
7904 : match a converting load, subsuming the load identification block
7905 : below. */
7906 602400 : if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
7907 : {
7908 10 : outer_type = TREE_TYPE (exp);
7909 10 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7910 10 : if (load)
7911 0 : loc[3] = gimple_location (load);
7912 10 : exp = res_ops[0];
7913 : /* This looks backwards, but we're going back the def chain, so if we
7914 : find the conversion here, after finding a shift, that's because the
7915 : convert appears before the shift, and we should thus adjust the bit
7916 : pos and size because of the shift after adjusting it due to type
7917 : conversion. */
7918 10 : convert_before_shift = true;
7919 : }
7920 :
7921 : /* Identify the load, if there is one. */
7922 602400 : if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
7923 : {
7924 335481 : gimple *def = SSA_NAME_DEF_STMT (exp);
7925 335481 : if (gimple_assign_load_p (def))
7926 : {
7927 213593 : loc[3] = gimple_location (def);
7928 213593 : load = def;
7929 213593 : exp = gimple_assign_rhs1 (def);
7930 : }
7931 : }
7932 :
7933 : /* Identify the relevant bits. */
7934 602400 : poly_int64 poly_bitsize, poly_bitpos;
7935 602400 : int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
7936 602400 : inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
7937 : &mode, &unsignedp, &reversep, &volatilep);
7938 :
7939 602400 : HOST_WIDE_INT bs, bp;
7940 602400 : if (!poly_bitsize.is_constant (&bs)
7941 602400 : || !poly_bitpos.is_constant (&bp)
7942 602400 : || bs <= shiftrt
7943 602400 : || offset != 0
7944 601165 : || TREE_CODE (inner) == PLACEHOLDER_EXPR
7945 : /* Reject out-of-bound accesses (PR79731, PR118514). */
7946 601165 : || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
7947 601139 : || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
7948 413304 : && !type_has_mode_precision_p (TREE_TYPE (inner))))
7949 28848 : return NULL_TREE;
7950 :
7951 : /* Adjust shifts... */
7952 573552 : if (convert_before_shift
7953 573552 : && outer_type && bs > TYPE_PRECISION (outer_type))
7954 : {
7955 3 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7956 3 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7957 0 : bp += excess;
7958 : bs -= excess;
7959 : }
7960 :
7961 573552 : if (shiftrt)
7962 : {
7963 : /* Punt if we're shifting by more than the loaded bitfield (after
7964 : adjustment), or if there's a shift after a change of signedness, punt.
7965 : When comparing this field with a constant, we'll check that the
7966 : constant is a proper sign- or zero-extension (depending on signedness)
7967 : of a value that would fit in the selected portion of the bitfield. A
7968 : shift after a change of signedness would make the extension
7969 : non-uniform, and we can't deal with that (yet ???). See
7970 : gcc.dg/field-merge-22.c for a test that would go wrong. */
7971 258 : if (bs <= shiftrt
7972 258 : || (convert_before_shift
7973 10 : && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
7974 : return NULL_TREE;
7975 250 : if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7976 250 : bp += shiftrt;
7977 250 : bs -= shiftrt;
7978 : }
7979 :
7980 : /* ... and bit position. */
7981 573544 : if (!convert_before_shift
7982 573544 : && outer_type && bs > TYPE_PRECISION (outer_type))
7983 : {
7984 5198 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7985 5198 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7986 0 : bp += excess;
7987 : bs -= excess;
7988 : }
7989 :
7990 : /* If the number of bits in the reference is the same as the bitsize of
7991 : the outer type, then the outer type gives the signedness. Otherwise
7992 : (in case of a small bitfield) the signedness is unchanged. */
7993 573544 : if (outer_type && bs == TYPE_PRECISION (outer_type))
7994 8822 : unsignedp = TYPE_UNSIGNED (outer_type);
7995 :
7996 : /* Make the mask the expected width. */
7997 573544 : if (and_mask.get_precision () != 0)
7998 : {
7999 : /* If the AND_MASK encompasses bits that would be extensions of
8000 : the sign bit, set SIGNBIT. */
8001 26290 : if (!unsignedp
8002 2633 : && and_mask.get_precision () > bs
8003 28965 : && (and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
8004 : signbit = true;
8005 26290 : and_mask = wide_int::from (and_mask, bs, UNSIGNED);
8006 : }
8007 :
8008 573544 : *pexp = exp;
8009 573544 : *pload = load;
8010 573544 : *pbitsize = bs;
8011 573544 : *pbitpos = bp;
8012 573544 : *punsignedp = unsignedp;
8013 573544 : *preversep = reversep;
8014 573544 : *pvolatilep = volatilep;
8015 573544 : *psignbit = signbit;
8016 573544 : *pand_mask = and_mask;
8017 573544 : if (xorp)
8018 : {
8019 4171 : *pxorp = xorp;
8020 4171 : *pxor_cmp_op = xor_cmp_op;
8021 4171 : *pxor_and_mask = xor_and_mask;
8022 : }
8023 :
8024 : return inner;
8025 637692 : }
8026 :
8027 : /* Return the one bitpos within bit extents L or R that is at an
8028 : ALIGN-bit alignment boundary, or -1 if there is more than one such
8029 : boundary, if there isn't any, or if there is any such boundary
8030 : between the extents. L and R are given by bitpos and bitsize. If
8031 : it doesn't return -1, there are two consecutive ALIGN-bit words
8032 : that contain both extents, and at least one of the extents
8033 : straddles across the returned alignment boundary. */
8034 :
8035 : static inline HOST_WIDE_INT
8036 28195 : compute_split_boundary_from_align (HOST_WIDE_INT align,
8037 : HOST_WIDE_INT l_bitpos,
8038 : HOST_WIDE_INT l_bitsize,
8039 : HOST_WIDE_INT r_bitpos,
8040 : HOST_WIDE_INT r_bitsize)
8041 : {
8042 28195 : HOST_WIDE_INT amask = ~(align - 1);
8043 :
8044 28195 : HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
8045 28195 : HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
8046 :
8047 28195 : HOST_WIDE_INT boundary = (end_bit - 1) & amask;
8048 :
8049 : /* Make sure we're crossing no more than one alignment boundary.
8050 :
8051 : ??? We don't have logic to recombine loads of two adjacent
8052 : fields that each crosses a different alignment boundary, so
8053 : as to load the middle word only once, if other words can't be
8054 : otherwise recombined. */
8055 28195 : if (boundary - first_bit > align)
8056 : return -1;
8057 :
8058 10891 : HOST_WIDE_INT l_start_word = l_bitpos & amask;
8059 10891 : HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
8060 :
8061 10891 : HOST_WIDE_INT r_start_word = r_bitpos & amask;
8062 10891 : HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
8063 :
8064 : /* If neither field straddles across an alignment boundary, it's no
8065 : use to even try to merge them. */
8066 10891 : if (l_start_word == l_end_word && r_start_word == r_end_word)
8067 10584 : return -1;
8068 :
8069 : return boundary;
8070 : }
8071 :
8072 : /* Make a bit_field_ref. If POINT is NULL, return the BIT_FIELD_REF.
8073 : Otherwise, build and insert a load stmt before POINT, and return
8074 : the SSA_NAME. ??? Rewrite LOAD in terms of the bitfield? */
8075 :
8076 : static tree
8077 4515 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
8078 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
8079 : bool unsignedp, bool reversep, gimple *point)
8080 : {
8081 4515 : if (point && loc == UNKNOWN_LOCATION)
8082 18 : loc = gimple_location (point);
8083 :
8084 4515 : tree ref = make_bit_field_ref (loc, unshare_expr (inner),
8085 : unshare_expr (orig_inner),
8086 : type, bitsize, bitpos,
8087 : unsignedp, reversep);
8088 4515 : if (!point)
8089 : return ref;
8090 :
8091 : /* If we're remaking the same load, reuse the SSA NAME it is already loaded
8092 : into. */
8093 4364 : if (gimple_assign_load_p (point)
8094 4364 : && operand_equal_p (ref, gimple_assign_rhs1 (point)))
8095 : {
8096 1657 : gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
8097 : return gimple_assign_lhs (point);
8098 : }
8099 :
8100 2707 : gimple_seq stmts = NULL;
8101 2707 : tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
8102 :
8103 : /* We know the vuse is supposed to end up being the same as that at the
8104 : original load at the insertion point, but if we don't set it, it will be a
8105 : generic placeholder that only the global SSA update at the end of the pass
8106 : would make equal, too late for us to use in further combinations. So go
8107 : ahead and copy the vuse. */
8108 :
8109 2707 : tree reaching_vuse = gimple_vuse (point);
8110 2707 : for (gimple_stmt_iterator i = gsi_start (stmts);
8111 5824 : !gsi_end_p (i); gsi_next (&i))
8112 : {
8113 3117 : gimple *new_stmt = gsi_stmt (i);
8114 6234 : if (gimple_has_mem_ops (new_stmt))
8115 3117 : gimple_set_vuse (new_stmt, reaching_vuse);
8116 : }
8117 :
8118 2707 : gimple_stmt_iterator gsi = gsi_for_stmt (point);
8119 2707 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
8120 2707 : return ret;
8121 : }
8122 :
8123 : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
8124 : LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
8125 : respectively, starting at BIT_POS, using reversed endianness if
8126 : REVERSEP. Also initialize BITPOS (the starting position of each
8127 : part into INNER), BITSIZ (the bit count starting at BITPOS),
8128 : TOSHIFT[1] (the amount by which the part and its mask are to be
8129 : shifted right to bring its least-significant bit to bit zero) and
8130 : SHIFTED (the amount by which the part, by separate loading, has
8131 : already been shifted right, but that the mask needs shifting to
8132 : match). */
8133 :
8134 : static inline void
8135 307 : build_split_load (tree /* out */ ln_arg[2],
8136 : HOST_WIDE_INT /* out */ bitpos[2],
8137 : HOST_WIDE_INT /* out */ bitsiz[2],
8138 : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
8139 : HOST_WIDE_INT /* out */ shifted[2],
8140 : location_t loc, tree inner, tree orig_inner,
8141 : scalar_int_mode mode, scalar_int_mode mode2,
8142 : HOST_WIDE_INT bit_pos, bool reversep,
8143 : gimple *point[2])
8144 : {
8145 307 : scalar_int_mode modes[2] = { mode, mode2 };
8146 307 : bitsiz[0] = GET_MODE_BITSIZE (mode);
8147 307 : bitsiz[1] = GET_MODE_BITSIZE (mode2);
8148 :
8149 921 : for (int i = 0; i < 2; i++)
8150 : {
8151 614 : tree type = lang_hooks.types.type_for_mode (modes[i], 1);
8152 614 : if (!type)
8153 : {
8154 0 : type = build_nonstandard_integer_type (bitsiz[0], 1);
8155 0 : gcc_assert (type);
8156 : }
8157 614 : bitpos[i] = bit_pos;
8158 1228 : ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
8159 614 : type, bitsiz[i],
8160 614 : bit_pos, 1, reversep, point[i]);
8161 614 : bit_pos += bitsiz[i];
8162 : }
8163 :
8164 307 : toshift[1] = toshift[0];
8165 307 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8166 : {
8167 3 : shifted[0] = bitsiz[1];
8168 3 : shifted[1] = 0;
8169 3 : toshift[0] = 0;
8170 : }
8171 : else
8172 : {
8173 304 : shifted[1] = bitsiz[0];
8174 304 : shifted[0] = 0;
8175 304 : toshift[1] = 0;
8176 : }
8177 307 : }
8178 :
8179 : /* Make arrangements to split at bit BOUNDARY a single loaded word
8180 : (with REVERSEP bit order) LN_ARG[0], to be shifted right by
8181 : TOSHIFT[0] to bring the field of interest to the least-significant
8182 : bit. The expectation is that the same loaded word will be
8183 : propagated from part 0 to part 1, with just different shifting and
8184 : masking to extract both parts. MASK is not expected to do more
8185 : than masking out the bits that belong to the other part. See
8186 : build_split_load for more information on the other fields. */
8187 :
8188 : static inline void
8189 51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
8190 : HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
8191 : HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
8192 : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
8193 : HOST_WIDE_INT /* out */ shifted[2],
8194 : wide_int /* out */ mask[2],
8195 : HOST_WIDE_INT boundary, bool reversep)
8196 : {
8197 51 : unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
8198 :
8199 51 : ln_arg[1] = ln_arg[0];
8200 51 : bitpos[1] = bitpos[0];
8201 51 : bitsiz[1] = bitsiz[0];
8202 51 : shifted[1] = shifted[0] = 0;
8203 :
8204 51 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8205 : {
8206 3 : toshift[1] = toshift[0];
8207 3 : toshift[0] = bitpos[0] + bitsiz[0] - boundary;
8208 3 : mask[0] = wi::mask (toshift[0], true, prec);
8209 3 : mask[1] = wi::mask (toshift[0], false, prec);
8210 : }
8211 : else
8212 : {
8213 48 : toshift[1] = boundary - bitpos[1];
8214 48 : mask[1] = wi::mask (toshift[1], true, prec);
8215 48 : mask[0] = wi::mask (toshift[1], false, prec);
8216 : }
8217 51 : }
8218 :
8219 : /* Find ways of folding logical expressions of LHS and RHS:
8220 :
8221 : Try to merge two comparisons to nearby fields.
8222 :
8223 : For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
8224 : at once, we can do this with a comparison against the object ANDed with the
8225 : a mask.
8226 :
8227 : If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
8228 : operations to do this with one comparison, loading both fields from P at
8229 : once, and likewise from Q.
8230 :
8231 : Herein, loading at once means loading from within the same alignment
8232 : boundary for the enclosing object. If (packed) fields cross such alignment
8233 : boundaries, we may still recombine the compares, so that loads do not cross
8234 : the boundaries.
8235 :
8236 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
8237 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
8238 :
8239 : TRUTH_TYPE is the type of the logical operand.
8240 :
8241 : LHS is denoted as LL_ARG LCODE LR_ARG.
8242 :
8243 : RHS is denoted as RL_ARG RCODE RR_ARG.
8244 :
8245 : LHS is assumed to dominate RHS.
8246 :
8247 : Combined loads are inserted next to preexisting loads, once we determine
8248 : that the combination is viable, and the combined condition references new
8249 : SSA_NAMEs that hold the loaded values. Since the original loads are
8250 : verified to have the same gimple_vuse, the insertion point doesn't matter
8251 : for correctness. ??? The loads may be a lot earlier than the compares, and
8252 : it's conceivable that one or two loads for RHS appear before those for LHS.
8253 : It could be advantageous to try to place the loads optimally, taking
8254 : advantage of knowing whether RHS is accessed before LHS, or that both are
8255 : accessed before both compares, but we don't do that (yet?).
8256 :
8257 : SEPARATEP should be NULL if the combined condition must be returned as a
8258 : single expression, even if it is a compound condition. This must only be
8259 : done if LHS and RHS are adjacent, without intervening conditions, and the
8260 : combined condition is to replace RHS, while LHS is dropped altogether.
8261 :
8262 : Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
8263 : replaced by a part of the compound condition that could replace RHS, while
8264 : the returned expression replaces LHS. This works whether or not LHS and RHS
8265 : are adjacent, as long as there aren't VDEFs or other side effects between
8266 : them.
8267 :
8268 : If the "words" accessed by RHS are already accessed by LHS, this won't
8269 : matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
8270 : be set to the compares that should take RHS's place. By "words" we mean
8271 : contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
8272 : object's type.
8273 :
8274 : We return the simplified tree or 0 if no optimization is possible. */
8275 :
8276 : tree
8277 257452 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
8278 : location_t lloc, enum tree_code lcode,
8279 : tree ll_arg, tree lr_arg,
8280 : location_t rloc, enum tree_code rcode,
8281 : tree rl_arg, tree rr_arg,
8282 : tree *separatep)
8283 : {
8284 : /* If this is the "or" of two comparisons, we can do something if
8285 : the comparisons are NE_EXPR. If this is the "and", we can do something
8286 : if the comparisons are EQ_EXPR. I.e.,
8287 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
8288 :
8289 : WANTED_CODE is this operation code. For single bit fields, we can
8290 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
8291 : comparison for one-bit fields. */
8292 :
8293 257452 : enum tree_code orig_code = code;
8294 257452 : enum tree_code wanted_code;
8295 257452 : tree ll_inner, lr_inner, rl_inner, rr_inner;
8296 257452 : gimple *ll_load, *lr_load, *rl_load, *rr_load;
8297 257452 : HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
8298 257452 : HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
8299 257452 : HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
8300 257452 : HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
8301 257452 : HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
8302 257452 : bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
8303 257452 : bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
8304 257452 : bool ll_signbit, lr_signbit, rl_signbit, rr_signbit;
8305 257452 : scalar_int_mode lnmode, lnmode2, rnmode;
8306 257452 : wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
8307 257452 : wide_int l_const, r_const;
8308 257452 : tree lntype, rntype, result;
8309 257452 : HOST_WIDE_INT first_bit, end_bit;
8310 257452 : bool volatilep;
8311 257452 : bool l_split_load;
8312 :
8313 : /* These are indexed by: conv, mask, shft, load. */
8314 257452 : location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
8315 257452 : location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
8316 257452 : location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8317 257452 : location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8318 :
8319 257452 : gcc_checking_assert (!separatep || !*separatep);
8320 :
8321 : /* Start by getting the comparison codes. Fail if anything is volatile.
8322 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
8323 : it were surrounded with a NE_EXPR. */
8324 :
8325 257452 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
8326 257452 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
8327 : return 0;
8328 :
8329 : /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
8330 : given by our caller to denote conditions from different blocks. */
8331 257452 : switch (code)
8332 : {
8333 : case TRUTH_AND_EXPR:
8334 : case TRUTH_ANDIF_EXPR:
8335 : code = TRUTH_AND_EXPR;
8336 : break;
8337 :
8338 0 : case TRUTH_OR_EXPR:
8339 0 : case TRUTH_ORIF_EXPR:
8340 0 : code = TRUTH_OR_EXPR;
8341 0 : break;
8342 :
8343 : default:
8344 : return 0;
8345 : }
8346 :
8347 : /* Prepare to turn compares of signed quantities with zero into sign-bit
8348 : tests. We need not worry about *_reversep here for these compare
8349 : rewrites: loads will have already been reversed before compares. Save the
8350 : precision, because [lr]l_arg may change and we won't be able to tell how
8351 : wide it was originally. */
8352 257452 : unsigned lsignbit = 0, rsignbit = 0;
8353 257452 : if ((lcode == LT_EXPR || lcode == GE_EXPR)
8354 11460 : && integer_zerop (lr_arg)
8355 3204 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8356 260656 : && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
8357 : {
8358 3204 : lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
8359 3204 : lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8360 : }
8361 : /* Turn compares of unsigned quantities with powers of two into
8362 : equality tests of masks. */
8363 254248 : else if ((lcode == LT_EXPR || lcode == GE_EXPR)
8364 8256 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8365 7591 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8366 5663 : && TREE_CODE (lr_arg) == INTEGER_CST
8367 254248 : && wi::popcount (wi::to_wide (lr_arg)) == 1)
8368 : {
8369 0 : ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
8370 0 : lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8371 0 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8372 : }
8373 : /* Turn compares of unsigned quantities with powers of two minus one
8374 : into equality tests of masks. */
8375 508496 : else if ((lcode == LE_EXPR || lcode == GT_EXPR)
8376 26918 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8377 26769 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8378 21892 : && TREE_CODE (lr_arg) == INTEGER_CST
8379 535414 : && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
8380 : {
8381 3118 : ll_and_mask = ~wi::to_wide (lr_arg);
8382 3118 : lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8383 3118 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8384 : }
8385 : /* Likewise for the second compare. */
8386 257452 : if ((rcode == LT_EXPR || rcode == GE_EXPR)
8387 18357 : && integer_zerop (rr_arg)
8388 1733 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8389 259185 : && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
8390 : {
8391 1733 : rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
8392 1733 : rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8393 : }
8394 255719 : else if ((rcode == LT_EXPR || rcode == GE_EXPR)
8395 16624 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8396 15656 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8397 2664 : && TREE_CODE (rr_arg) == INTEGER_CST
8398 255719 : && wi::popcount (wi::to_wide (rr_arg)) == 1)
8399 : {
8400 0 : rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
8401 0 : rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8402 0 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8403 : }
8404 511438 : else if ((rcode == LE_EXPR || rcode == GT_EXPR)
8405 35195 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8406 34971 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8407 25363 : && TREE_CODE (rr_arg) == INTEGER_CST
8408 546633 : && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
8409 : {
8410 2751 : rl_and_mask = ~wi::to_wide (rr_arg);
8411 2751 : rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8412 2751 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8413 : }
8414 :
8415 : /* See if the comparisons can be merged. Then get all the parameters for
8416 : each side. */
8417 :
8418 257452 : if ((lcode != EQ_EXPR && lcode != NE_EXPR)
8419 224606 : || (rcode != EQ_EXPR && rcode != NE_EXPR))
8420 : return 0;
8421 :
8422 201511 : ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
8423 201511 : volatilep = 0;
8424 201511 : bool l_xor = false, r_xor = false;
8425 201511 : ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
8426 : &ll_unsignedp, &ll_reversep, &volatilep,
8427 : &ll_and_mask, &ll_signbit,
8428 : &l_xor, &lr_arg, &lr_and_mask,
8429 : &ll_load, ll_loc);
8430 201511 : if (!ll_inner)
8431 : return 0;
8432 150465 : lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
8433 : &lr_unsignedp, &lr_reversep, &volatilep,
8434 : &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
8435 : &lr_load, lr_loc);
8436 150465 : if (!lr_inner)
8437 : return 0;
8438 147606 : rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
8439 : &rl_unsignedp, &rl_reversep, &volatilep,
8440 : &rl_and_mask, &rl_signbit,
8441 : &r_xor, &rr_arg, &rr_and_mask,
8442 : &rl_load, rl_loc);
8443 147606 : if (!rl_inner)
8444 : return 0;
8445 138110 : rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
8446 : &rr_unsignedp, &rr_reversep, &volatilep,
8447 : &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
8448 : &rr_load, rr_loc);
8449 138110 : if (!rr_inner)
8450 : return 0;
8451 :
8452 : /* It must be true that the inner operation on the lhs of each
8453 : comparison must be the same if we are to be able to do anything.
8454 : Then see if we have constants. If not, the same must be true for
8455 : the rhs's. If one is a load and the other isn't, we have to be
8456 : conservative and avoid the optimization, otherwise we could get
8457 : SRAed fields wrong. */
8458 137363 : if (volatilep)
8459 : return 0;
8460 :
8461 137363 : if (ll_reversep != rl_reversep
8462 137363 : || ! operand_equal_p (ll_inner, rl_inner, 0))
8463 : {
8464 : /* Try swapping the operands. */
8465 102343 : if (ll_reversep != rr_reversep || rsignbit
8466 204223 : || !operand_equal_p (ll_inner, rr_inner, 0))
8467 101110 : return 0;
8468 :
8469 1260 : rcode = swap_tree_comparison (rcode);
8470 1260 : std::swap (rl_arg, rr_arg);
8471 1260 : std::swap (rl_inner, rr_inner);
8472 1260 : std::swap (rl_bitsize, rr_bitsize);
8473 1260 : std::swap (rl_bitpos, rr_bitpos);
8474 1260 : std::swap (rl_unsignedp, rr_unsignedp);
8475 1260 : std::swap (rl_reversep, rr_reversep);
8476 1260 : std::swap (rl_and_mask, rr_and_mask);
8477 1260 : std::swap (rl_signbit, rr_signbit);
8478 1260 : std::swap (rl_load, rr_load);
8479 1260 : std::swap (rl_loc, rr_loc);
8480 : }
8481 :
8482 70428 : if ((ll_load && rl_load)
8483 138778 : ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
8484 2078 : : (!ll_load != !rl_load))
8485 : return 0;
8486 :
8487 : /* ??? Can we do anything with these? */
8488 35845 : if (lr_signbit || rr_signbit)
8489 : return 0;
8490 :
8491 : /* If the mask encompassed extensions of the sign bit before
8492 : clipping, try to include the sign bit in the test. If we're not
8493 : comparing with zero, don't even try to deal with it (for now?).
8494 : If we've already committed to a sign test, the extended (before
8495 : clipping) mask could already be messing with it. */
8496 35845 : if (ll_signbit)
8497 : {
8498 4 : if (!integer_zerop (lr_arg) || lsignbit)
8499 0 : return 0;
8500 4 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8501 4 : if (!ll_and_mask.get_precision ())
8502 0 : ll_and_mask = sign;
8503 : else
8504 4 : ll_and_mask |= sign;
8505 4 : }
8506 :
8507 35845 : if (rl_signbit)
8508 : {
8509 4 : if (!integer_zerop (rr_arg) || rsignbit)
8510 1 : return 0;
8511 3 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8512 3 : if (!rl_and_mask.get_precision ())
8513 0 : rl_and_mask = sign;
8514 : else
8515 3 : rl_and_mask |= sign;
8516 3 : }
8517 :
8518 35844 : if (TREE_CODE (lr_arg) == INTEGER_CST
8519 29301 : && TREE_CODE (rr_arg) == INTEGER_CST)
8520 : {
8521 28893 : l_const = wi::to_wide (lr_arg);
8522 : /* We don't expect masks on constants, but if there are any, apply
8523 : them now. */
8524 28893 : if (lr_and_mask.get_precision ())
8525 0 : l_const &= wide_int::from (lr_and_mask,
8526 0 : l_const.get_precision (), UNSIGNED);
8527 28893 : r_const = wi::to_wide (rr_arg);
8528 28893 : if (rr_and_mask.get_precision ())
8529 0 : r_const &= wide_int::from (rr_and_mask,
8530 0 : r_const.get_precision (), UNSIGNED);
8531 28893 : lr_reversep = ll_reversep;
8532 : }
8533 6951 : else if (lr_reversep != rr_reversep
8534 6951 : || ! operand_equal_p (lr_inner, rr_inner, 0)
8535 12734 : || ((lr_load && rr_load)
8536 17268 : ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
8537 27 : : (!lr_load != !rr_load)))
8538 1198 : return 0;
8539 :
8540 : /* If we found sign tests, finish turning them into bit tests. */
8541 :
8542 34646 : if (lsignbit)
8543 : {
8544 44 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8545 : /* If ll_arg is zero-extended and we're testing the sign bit, we know
8546 : what the result should be. Shifting the sign bit out of sign will get
8547 : us to mask the entire field out, yielding zero, i.e., the sign bit of
8548 : the zero-extended value. We know the masked value is being compared
8549 : with zero, so the compare will get us the result we're looking
8550 : for: TRUE if EQ_EXPR, FALSE if NE_EXPR. */
8551 44 : if (lsignbit > ll_bitsize && ll_unsignedp)
8552 1 : sign <<= 1;
8553 44 : if (!ll_and_mask.get_precision ())
8554 43 : ll_and_mask = sign;
8555 : else
8556 1 : ll_and_mask &= sign;
8557 44 : if (l_xor)
8558 : {
8559 1 : if (ll_bitsize != lr_bitsize)
8560 1 : return 0;
8561 0 : if (!lr_and_mask.get_precision ())
8562 0 : lr_and_mask = sign;
8563 : else
8564 0 : lr_and_mask &= sign;
8565 0 : if (l_const.get_precision ())
8566 0 : l_const &= wide_int::from (lr_and_mask,
8567 0 : l_const.get_precision (), UNSIGNED);
8568 : }
8569 44 : }
8570 :
8571 34645 : if (rsignbit)
8572 : {
8573 166 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8574 166 : if (rsignbit > rl_bitsize && rl_unsignedp)
8575 0 : sign <<= 1;
8576 166 : if (!rl_and_mask.get_precision ())
8577 166 : rl_and_mask = sign;
8578 : else
8579 0 : rl_and_mask &= sign;
8580 166 : if (r_xor)
8581 : {
8582 16 : if (rl_bitsize != rr_bitsize)
8583 0 : return 0;
8584 16 : if (!rr_and_mask.get_precision ())
8585 16 : rr_and_mask = sign;
8586 : else
8587 0 : rr_and_mask &= sign;
8588 16 : if (r_const.get_precision ())
8589 24 : r_const &= wide_int::from (rr_and_mask,
8590 12 : r_const.get_precision (), UNSIGNED);
8591 : }
8592 166 : }
8593 :
8594 : /* If either comparison code is not correct for our logical operation,
8595 : fail. However, we can convert a one-bit comparison against zero into
8596 : the opposite comparison against that bit being set in the field. */
8597 :
8598 34645 : wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
8599 34645 : if (lcode != wanted_code)
8600 : {
8601 4534 : if (l_const.get_precision ()
8602 4488 : && l_const == 0
8603 1517 : && ll_and_mask.get_precision ()
8604 4947 : && wi::popcount (ll_and_mask) == 1)
8605 : {
8606 : /* Make the left operand unsigned, since we are only interested
8607 : in the value of one bit. Otherwise we are doing the wrong
8608 : thing below. */
8609 289 : ll_unsignedp = 1;
8610 289 : l_const = ll_and_mask;
8611 : }
8612 : else
8613 4245 : return 0;
8614 : }
8615 :
8616 : /* This is analogous to the code for l_const above. */
8617 30400 : if (rcode != wanted_code)
8618 : {
8619 854 : if (r_const.get_precision ()
8620 854 : && r_const == 0
8621 829 : && rl_and_mask.get_precision ()
8622 1617 : && wi::popcount (rl_and_mask) == 1)
8623 : {
8624 601 : rl_unsignedp = 1;
8625 601 : r_const = rl_and_mask;
8626 : }
8627 : else
8628 253 : return 0;
8629 : }
8630 :
8631 : /* This will be bumped to 2 if any of the field pairs crosses an
8632 : alignment boundary, so the merged compare has to be done in two
8633 : parts. */
8634 90441 : int parts = 1;
8635 : /* Set to true if the second combined compare should come first,
8636 : e.g., because the second original compare accesses a word that
8637 : the first one doesn't, and the combined compares access those in
8638 : cmp[0]. */
8639 90441 : bool first1 = false;
8640 : /* Set to true if the first original compare is not the one being
8641 : split. */
8642 90441 : bool maybe_separate = false;
8643 :
8644 : /* The following 2-dimensional arrays use the first index to
8645 : identify left(0)- vs right(1)-hand compare operands, and the
8646 : second one to identify merged compare parts. */
8647 : /* The memory loads or constants to be compared. */
8648 : tree ld_arg[2][2];
8649 : /* The first bit of the corresponding inner object that the
8650 : corresponding LD_ARG covers. */
8651 : HOST_WIDE_INT bitpos[2][2];
8652 : /* The bit count starting at BITPOS that the corresponding LD_ARG
8653 : covers. */
8654 : HOST_WIDE_INT bitsiz[2][2];
8655 : /* The number of bits by which LD_ARG has already been shifted
8656 : right, WRT mask. */
8657 : HOST_WIDE_INT shifted[2][2];
8658 : /* The number of bits by which both LD_ARG and MASK need shifting to
8659 : bring its least-significant bit to bit zero. */
8660 : HOST_WIDE_INT toshift[2][2];
8661 : /* An additional mask to be applied to LD_ARG, to remove any bits
8662 : that may have been loaded for use in another compare, but that
8663 : don't belong in the corresponding compare. */
8664 361764 : wide_int xmask[2][2] = {};
8665 :
8666 : /* The combined compare or compares. */
8667 30147 : tree cmp[2];
8668 :
8669 : /* Consider we're comparing two non-contiguous fields of packed
8670 : structs, both aligned at 32-bit boundaries:
8671 :
8672 : ll_arg: an 8-bit field at offset 0
8673 : lr_arg: a 16-bit field at offset 2
8674 :
8675 : rl_arg: an 8-bit field at offset 1
8676 : rr_arg: a 16-bit field at offset 3
8677 :
8678 : We'll have r_split_load, because rr_arg straddles across an
8679 : alignment boundary.
8680 :
8681 : We'll want to have:
8682 :
8683 : bitpos = { { 0, 0 }, { 0, 32 } }
8684 : bitsiz = { { 32, 32 }, { 32, 8 } }
8685 :
8686 : And, for little-endian:
8687 :
8688 : shifted = { { 0, 0 }, { 0, 32 } }
8689 : toshift = { { 0, 24 }, { 0, 0 } }
8690 :
8691 : Or, for big-endian:
8692 :
8693 : shifted = { { 0, 0 }, { 8, 0 } }
8694 : toshift = { { 8, 0 }, { 0, 0 } }
8695 : */
8696 :
8697 : /* See if we can find a mode that contains both fields being compared on
8698 : the left. If we can't, fail. Otherwise, update all constants and masks
8699 : to be relative to a field of that size. */
8700 30147 : first_bit = MIN (ll_bitpos, rl_bitpos);
8701 30147 : end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
8702 30147 : HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
8703 30147 : poly_uint64 ll_end_region = 0;
8704 30147 : if (TYPE_SIZE (TREE_TYPE (ll_inner))
8705 30147 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
8706 30147 : ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
8707 30147 : if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
8708 30147 : ll_align, BITS_PER_WORD, volatilep, &lnmode))
8709 : l_split_load = false;
8710 : /* ??? If ll and rl share the same load, reuse that?
8711 : See PR 118206 -> gcc.dg/field-merge-18.c */
8712 : else
8713 : {
8714 : /* Consider the possibility of recombining loads if any of the
8715 : fields straddles across an alignment boundary, so that either
8716 : part can be loaded along with the other field. Since we
8717 : limit access modes to BITS_PER_WORD, don't exceed that,
8718 : otherwise on a 32-bit host and a 64-bit-aligned data
8719 : structure, we'll fail the above for a field that straddles
8720 : across two words, and would fail here for not even trying to
8721 : split it at between 32-bit words. */
8722 26808 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8723 28390 : (MIN (ll_align, BITS_PER_WORD),
8724 : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8725 :
8726 26808 : if (boundary < 0
8727 219 : || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
8728 : ll_align, BITS_PER_WORD, volatilep, &lnmode)
8729 26985 : || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
8730 177 : ll_align, BITS_PER_WORD, volatilep, &lnmode2))
8731 : {
8732 28171 : if (ll_align <= BITS_PER_WORD)
8733 : return 0;
8734 :
8735 : /* As a last resort, try double-word access modes. This
8736 : enables us to deal with misaligned double-word fields
8737 : that straddle across 3 separate words. */
8738 1253 : boundary = compute_split_boundary_from_align
8739 1343 : (MIN (ll_align, 2 * BITS_PER_WORD),
8740 : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8741 1253 : if (boundary < 0
8742 0 : || !get_best_mode (boundary - first_bit, first_bit,
8743 : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8744 : volatilep, &lnmode)
8745 1253 : || !get_best_mode (end_bit - boundary, boundary,
8746 0 : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8747 : volatilep, &lnmode2))
8748 1253 : return 0;
8749 : }
8750 :
8751 : /* If we can't have a single load, but can with two, figure out whether
8752 : the two compares can be separated, i.e., whether the entirety of the
8753 : first original compare is encompassed by the entirety of the first
8754 : combined compare. If the first original compare is past the alignment
8755 : boundary, arrange to compare that range first, by setting first1
8756 : (meaning make cmp[1] first, instead of cmp[0]). */
8757 177 : l_split_load = true;
8758 177 : parts = 2;
8759 177 : if (ll_bitpos >= boundary)
8760 : maybe_separate = first1 = true;
8761 132 : else if (ll_bitpos + ll_bitsize <= boundary)
8762 32 : maybe_separate = true;
8763 : }
8764 :
8765 3516 : lnbitsize = GET_MODE_BITSIZE (lnmode);
8766 3516 : lnbitpos = first_bit & ~ (lnbitsize - 1);
8767 : /* Avoid situations that the code below can't handle. */
8768 3516 : if (lnbitpos < 0)
8769 : return 0;
8770 :
8771 : /* Choose the type for the combined compare. Even if we're splitting loads,
8772 : make it wide enough to hold both. */
8773 3516 : if (l_split_load)
8774 354 : lnbitsize += GET_MODE_BITSIZE (lnmode2);
8775 3516 : lntype = build_nonstandard_integer_type (lnbitsize, 1);
8776 3516 : if (!lntype)
8777 : return NULL_TREE;
8778 3516 : lnprec = TYPE_PRECISION (lntype);
8779 3516 : xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
8780 :
8781 : /* Adjust bit ranges for reverse endianness. */
8782 3516 : if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8783 : {
8784 6 : xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
8785 6 : xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
8786 : }
8787 :
8788 : /* Adjust masks to match the positions in the combined lntype. */
8789 7032 : wide_int ll_mask, rl_mask, r_mask;
8790 3516 : if (ll_and_mask.get_precision ())
8791 4278 : ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
8792 2139 : xll_bitpos);
8793 : else
8794 1377 : ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
8795 3516 : if (rl_and_mask.get_precision ())
8796 4070 : rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
8797 2035 : xrl_bitpos);
8798 : else
8799 1481 : rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
8800 :
8801 : /* When we set l_const, we also set r_const. */
8802 3516 : gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
8803 :
8804 : /* Adjust right-hand constants in both original comparisons to match width
8805 : and bit position. */
8806 3516 : if (l_const.get_precision ())
8807 : {
8808 : /* Before clipping upper bits of the right-hand operand of the compare,
8809 : check that they're sign or zero extensions, depending on how the
8810 : left-hand operand would be extended. If it is unsigned, or if there's
8811 : a mask that zeroes out extension bits, whether because we've checked
8812 : for upper bits in the mask and did not set ll_signbit, or because the
8813 : sign bit itself is masked out, check that the right-hand operand is
8814 : zero-extended. */
8815 1894 : bool l_non_ext_bits = false;
8816 1894 : if (ll_bitsize < lr_bitsize)
8817 : {
8818 40 : wide_int zext = wi::zext (l_const, ll_bitsize);
8819 80 : if ((ll_unsignedp
8820 32 : || (ll_and_mask.get_precision ()
8821 4 : && (!ll_signbit
8822 48 : || ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
8823 8 : == 0)))
8824 152 : ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
8825 40 : l_const = zext;
8826 : else
8827 : l_non_ext_bits = true;
8828 40 : }
8829 : /* We're doing bitwise equality tests, so don't bother with sign
8830 : extensions. */
8831 1894 : l_const = wide_int::from (l_const, lnprec, UNSIGNED);
8832 1894 : if (ll_and_mask.get_precision ())
8833 1152 : l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
8834 1894 : l_const <<= xll_bitpos;
8835 5682 : if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
8836 : {
8837 0 : warning_at (lloc, OPT_Wtautological_compare,
8838 : "comparison is always %d", wanted_code == NE_EXPR);
8839 :
8840 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8841 : }
8842 :
8843 : /* Before clipping upper bits of the right-hand operand of the compare,
8844 : check that they're sign or zero extensions, depending on how the
8845 : left-hand operand would be extended. */
8846 1894 : bool r_non_ext_bits = false;
8847 1894 : if (rl_bitsize < rr_bitsize)
8848 : {
8849 18 : wide_int zext = wi::zext (r_const, rl_bitsize);
8850 36 : if ((rl_unsignedp
8851 17 : || (rl_and_mask.get_precision ()
8852 10 : && (!rl_signbit
8853 24 : || ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
8854 6 : == 0)))
8855 71 : ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
8856 18 : r_const = zext;
8857 : else
8858 : r_non_ext_bits = true;
8859 18 : }
8860 1894 : r_const = wide_int::from (r_const, lnprec, UNSIGNED);
8861 1894 : if (rl_and_mask.get_precision ())
8862 1092 : r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
8863 1894 : r_const <<= xrl_bitpos;
8864 5682 : if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
8865 : {
8866 0 : warning_at (rloc, OPT_Wtautological_compare,
8867 : "comparison is always %d", wanted_code == NE_EXPR);
8868 :
8869 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8870 : }
8871 :
8872 : /* If there is something in common between the masks, those bits of the
8873 : constants must be the same. If not, the combined condition cannot be
8874 : met, and the result is known. Test for this to avoid generating
8875 : incorrect code below. */
8876 1894 : wide_int mask = ll_mask & rl_mask;
8877 1894 : if (mask != 0
8878 1939 : && (l_const & mask) != (r_const & mask))
8879 : {
8880 0 : if (wanted_code == NE_EXPR)
8881 0 : return constant_boolean_node (true, truth_type);
8882 : else
8883 0 : return constant_boolean_node (false, truth_type);
8884 : }
8885 :
8886 : /* The constants are combined so as to line up with the loaded field, so
8887 : tentatively use the same parameters for the second combined
8888 : compare. */
8889 1894 : ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
8890 1894 : toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
8891 1894 : shifted[1][0] = 0;
8892 1894 : bitpos[1][0] = lnbitpos;
8893 1894 : bitsiz[1][0] = lnbitsize;
8894 :
8895 1894 : if (parts > 1)
8896 49 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8897 : shifted[1], xmask[1],
8898 49 : lnbitpos + GET_MODE_BITSIZE (lnmode),
8899 : lr_reversep);
8900 :
8901 : /* No masking needed, we know the full constants. */
8902 1894 : r_mask = wi::mask (0, true, lnprec);
8903 :
8904 : /* If the compiler thinks this is used uninitialized below, it's
8905 : because it can't realize that parts can only be 2 when
8906 : comparing with constants if l_split_load is also true. This
8907 : just silences the warning. */
8908 1894 : rnbitpos = 0;
8909 1894 : }
8910 :
8911 : /* Likewise, if the right sides are not constant, align them for the combined
8912 : compare. Also, disallow this optimization if a size, signedness or
8913 : storage order mismatch occurs between the left and right sides. */
8914 : else
8915 : {
8916 1622 : if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
8917 1561 : || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
8918 1561 : || ll_reversep != lr_reversep
8919 : /* Make sure the two fields on the right
8920 : correspond to the left without being swapped. */
8921 1561 : || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
8922 465 : return 0;
8923 :
8924 1161 : bool r_split_load;
8925 1161 : scalar_int_mode rnmode2;
8926 :
8927 : /* Figure out how to load the bits for the right-hand size of the
8928 : combined compare. As in the left-hand size, we may have to split it,
8929 : and then we use two separate compares. */
8930 1161 : first_bit = MIN (lr_bitpos, rr_bitpos);
8931 1161 : end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
8932 1161 : HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
8933 1161 : poly_uint64 lr_end_region = 0;
8934 1161 : if (TYPE_SIZE (TREE_TYPE (lr_inner))
8935 1161 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
8936 1161 : lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
8937 1161 : if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
8938 1161 : lr_align, BITS_PER_WORD, volatilep, &rnmode))
8939 : {
8940 : /* Consider the possibility of recombining loads if any of the
8941 : fields straddles across an alignment boundary, so that either
8942 : part can be loaded along with the other field. */
8943 134 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8944 134 : (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
8945 :
8946 134 : if (boundary < 0
8947 : /* If we're to split both, make sure the split point is
8948 : the same. */
8949 130 : || (l_split_load
8950 128 : && (boundary - lr_bitpos
8951 128 : != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
8952 130 : || !get_best_mode (boundary - first_bit, first_bit,
8953 : 0, lr_end_region,
8954 130 : lr_align, BITS_PER_WORD, volatilep, &rnmode)
8955 264 : || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
8956 130 : lr_align, BITS_PER_WORD, volatilep, &rnmode2))
8957 4 : return 0;
8958 :
8959 130 : r_split_load = true;
8960 130 : parts = 2;
8961 130 : if (lr_bitpos >= boundary)
8962 : maybe_separate = first1 = true;
8963 88 : else if (lr_bitpos + lr_bitsize <= boundary)
8964 29 : maybe_separate = true;
8965 : }
8966 : else
8967 : r_split_load = false;
8968 :
8969 : /* Find a type that can hold the entire right-hand operand. */
8970 1157 : rnbitsize = GET_MODE_BITSIZE (rnmode);
8971 1157 : rnbitpos = first_bit & ~ (rnbitsize - 1);
8972 1157 : if (r_split_load)
8973 260 : rnbitsize += GET_MODE_BITSIZE (rnmode2);
8974 1157 : rntype = build_nonstandard_integer_type (rnbitsize, 1);
8975 1157 : if (!rntype)
8976 : return 0;
8977 1157 : rnprec = TYPE_PRECISION (rntype);
8978 1157 : xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
8979 :
8980 : /* Adjust for reversed endianness. */
8981 1157 : if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8982 : {
8983 0 : xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
8984 0 : xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
8985 : }
8986 :
8987 : /* Adjust the masks to match the combined type, and combine them. */
8988 1157 : wide_int lr_mask, rr_mask;
8989 1157 : if (lr_and_mask.get_precision ())
8990 1972 : lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
8991 986 : xlr_bitpos);
8992 : else
8993 171 : lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
8994 1157 : if (rr_and_mask.get_precision ())
8995 1884 : rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
8996 942 : xrr_bitpos);
8997 : else
8998 215 : rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
8999 1157 : r_mask = lr_mask | rr_mask;
9000 :
9001 : /* Load the right-hand operand of the combined compare. */
9002 1157 : toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
9003 1157 : shifted[1][0] = 0;
9004 :
9005 1157 : if (!r_split_load)
9006 : {
9007 1027 : bitpos[1][0] = rnbitpos;
9008 1027 : bitsiz[1][0] = rnbitsize;
9009 1027 : ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
9010 1027 : rntype, rnbitsize, rnbitpos,
9011 1027 : lr_unsignedp || rr_unsignedp,
9012 : lr_reversep, lr_load);
9013 : }
9014 :
9015 : /* ... and the second part of the right-hand operand if needed. */
9016 1157 : if (parts > 1)
9017 : {
9018 130 : if (r_split_load)
9019 : {
9020 130 : gimple *point[2];
9021 130 : point[0] = lr_load;
9022 130 : point[1] = rr_load;
9023 130 : build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
9024 : shifted[1], rl_loc[3], lr_inner, lr_arg,
9025 : rnmode, rnmode2, rnbitpos, lr_reversep, point);
9026 : }
9027 : else
9028 0 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
9029 : shifted[1], xmask[1],
9030 0 : lnbitpos + GET_MODE_BITSIZE (lnmode)
9031 0 : - ll_bitpos + lr_bitpos, lr_reversep);
9032 : }
9033 1157 : }
9034 :
9035 : /* Now issue the loads for the left-hand combined operand/s. */
9036 6102 : wide_int l_mask = ll_mask | rl_mask;
9037 3051 : toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
9038 3051 : shifted[0][0] = 0;
9039 :
9040 3051 : if (!l_split_load)
9041 : {
9042 2874 : bitpos[0][0] = lnbitpos;
9043 2874 : bitsiz[0][0] = lnbitsize;
9044 2874 : ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
9045 2874 : lntype, lnbitsize, lnbitpos,
9046 2874 : ll_unsignedp || rl_unsignedp,
9047 : ll_reversep, ll_load);
9048 : }
9049 :
9050 3051 : if (parts > 1)
9051 : {
9052 179 : if (l_split_load)
9053 : {
9054 177 : gimple *point[2];
9055 177 : point[0] = ll_load;
9056 177 : point[1] = rl_load;
9057 177 : build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
9058 : shifted[0], rl_loc[3], ll_inner, ll_arg,
9059 : lnmode, lnmode2, lnbitpos, ll_reversep, point);
9060 : }
9061 : else
9062 2 : reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
9063 : shifted[0], xmask[0],
9064 2 : rnbitpos + GET_MODE_BITSIZE (rnmode)
9065 2 : - lr_bitpos + ll_bitpos, ll_reversep);
9066 : }
9067 :
9068 : /* Compute the compares. */
9069 6281 : for (int i = 0; i < parts; i++)
9070 : {
9071 3230 : tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
9072 9690 : wide_int mask[2] = { l_mask, r_mask };
9073 3230 : location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
9074 :
9075 : /* Figure out the masks, and unshare the original operands. */
9076 9690 : for (int j = 0; j < 2; j++)
9077 : {
9078 6460 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
9079 6460 : op[j] = unshare_expr (op[j]);
9080 :
9081 : /* Mask out the bits belonging to the other part. */
9082 6460 : if (xmask[j][i].get_precision ())
9083 102 : mask[j] &= xmask[j][i];
9084 :
9085 6460 : if (shifted[j][i])
9086 : {
9087 307 : wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
9088 307 : mask[j] = wi::lrshift (mask[j], shift);
9089 307 : }
9090 6460 : mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
9091 : }
9092 :
9093 : /* Line up the operands for a compare. */
9094 3230 : HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
9095 :
9096 3230 : if (shift)
9097 : {
9098 54 : int j;
9099 54 : if (shift > 0)
9100 : j = 0;
9101 : else
9102 : {
9103 52 : j = 1;
9104 52 : shift = -shift;
9105 : }
9106 :
9107 54 : tree shiftsz = bitsize_int (shift);
9108 54 : op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
9109 : op[j], shiftsz);
9110 54 : mask[j] = wi::lrshift (mask[j], shift);
9111 : }
9112 :
9113 : /* Convert to the smaller type before masking out unwanted
9114 : bits. */
9115 3230 : tree type = TREE_TYPE (op[0]);
9116 3230 : if (type != TREE_TYPE (op[1]))
9117 : {
9118 188 : int j = (TYPE_PRECISION (type)
9119 188 : < TYPE_PRECISION (TREE_TYPE (op[1])));
9120 188 : if (!j)
9121 89 : type = TREE_TYPE (op[1]);
9122 188 : op[j] = fold_convert_loc (locs[j][0], type, op[j]);
9123 188 : mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
9124 : }
9125 :
9126 : /* Apply masks. */
9127 9690 : for (int j = 0; j < 2; j++)
9128 6460 : if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
9129 2598 : op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
9130 5196 : op[j], wide_int_to_tree (type, mask[j]));
9131 :
9132 6281 : cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
9133 : op[0], op[1]);
9134 9690 : }
9135 :
9136 : /* Reorder the compares if needed. */
9137 3051 : if (first1)
9138 45 : std::swap (cmp[0], cmp[1]);
9139 :
9140 : /* Prepare to return the resulting compares. Combine two parts if
9141 : needed. */
9142 3051 : if (parts == 1)
9143 2872 : result = cmp[0];
9144 179 : else if (!separatep || !maybe_separate)
9145 : {
9146 : /* Only fold if any of the cmp is known, otherwise we may lose the
9147 : sequence point, and that may prevent further optimizations. */
9148 173 : if (TREE_CODE (cmp[0]) == INTEGER_CST
9149 137 : || TREE_CODE (cmp[1]) == INTEGER_CST)
9150 37 : result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
9151 : else
9152 136 : result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
9153 : }
9154 : else
9155 : {
9156 6 : result = cmp[0];
9157 6 : *separatep = cmp[1];
9158 : }
9159 :
9160 3051 : return result;
9161 257452 : }
9162 :
9163 : /* Try to simplify the AND of two comparisons, specified by
9164 : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9165 : If this can be simplified to a single expression (without requiring
9166 : introducing more SSA variables to hold intermediate values),
9167 : return the resulting tree. Otherwise return NULL_TREE.
9168 : If the result expression is non-null, it has boolean type. */
9169 :
9170 : tree
9171 411383 : maybe_fold_and_comparisons (tree type,
9172 : enum tree_code code1, tree op1a, tree op1b,
9173 : enum tree_code code2, tree op2a, tree op2b,
9174 : basic_block outer_cond_bb)
9175 : {
9176 411383 : if (tree t = and_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9177 : outer_cond_bb))
9178 : return t;
9179 :
9180 410088 : if (tree t = and_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9181 : outer_cond_bb))
9182 : return t;
9183 :
9184 410072 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
9185 : op1a, op1b, code2, op2a,
9186 : op2b, outer_cond_bb))
9187 : return t;
9188 :
9189 : return NULL_TREE;
9190 : }
9191 :
9192 : /* Helper function for or_comparisons_1: try to simplify the OR of the
9193 : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
9194 : If INVERT is true, invert the value of VAR before doing the OR.
9195 : Return NULL_EXPR if we can't simplify this to a single expression. */
9196 :
9197 : static tree
9198 39750 : or_var_with_comparison (tree type, tree var, bool invert,
9199 : enum tree_code code2, tree op2a, tree op2b,
9200 : basic_block outer_cond_bb)
9201 : {
9202 39750 : tree t;
9203 39750 : gimple *stmt = SSA_NAME_DEF_STMT (var);
9204 :
9205 : /* We can only deal with variables whose definitions are assignments. */
9206 39750 : if (!is_gimple_assign (stmt))
9207 : return NULL_TREE;
9208 :
9209 : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
9210 : !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
9211 : Then we only have to consider the simpler non-inverted cases. */
9212 39630 : if (invert)
9213 20518 : t = and_var_with_comparison_1 (type, stmt,
9214 : invert_tree_comparison (code2, false),
9215 : op2a, op2b, outer_cond_bb);
9216 : else
9217 19112 : t = or_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
9218 : outer_cond_bb);
9219 39630 : return canonicalize_bool (t, invert);
9220 : }
9221 :
9222 : /* Try to simplify the OR of the ssa variable defined by the assignment
9223 : STMT with the comparison specified by (OP2A CODE2 OP2B).
9224 : Return NULL_EXPR if we can't simplify this to a single expression. */
9225 :
9226 : static tree
9227 102258 : or_var_with_comparison_1 (tree type, gimple *stmt,
9228 : enum tree_code code2, tree op2a, tree op2b,
9229 : basic_block outer_cond_bb)
9230 : {
9231 102258 : tree var = gimple_assign_lhs (stmt);
9232 102258 : tree true_test_var = NULL_TREE;
9233 102258 : tree false_test_var = NULL_TREE;
9234 102258 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
9235 :
9236 : /* Check for identities like (var OR (var != 0)) => true . */
9237 102258 : if (TREE_CODE (op2a) == SSA_NAME
9238 102258 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
9239 : {
9240 15843 : if ((code2 == NE_EXPR && integer_zerop (op2b))
9241 50840 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
9242 : {
9243 14085 : true_test_var = op2a;
9244 14085 : if (var == true_test_var)
9245 : return var;
9246 : }
9247 2984 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
9248 30365 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
9249 : {
9250 7278 : false_test_var = op2a;
9251 7278 : if (var == false_test_var)
9252 0 : return boolean_true_node;
9253 : }
9254 : }
9255 :
9256 : /* If the definition is a comparison, recurse on it. */
9257 102258 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
9258 : {
9259 866 : tree t = or_comparisons_1 (type, innercode,
9260 : gimple_assign_rhs1 (stmt),
9261 : gimple_assign_rhs2 (stmt),
9262 : code2, op2a, op2b, outer_cond_bb);
9263 866 : if (t)
9264 : return t;
9265 : }
9266 :
9267 : /* If the definition is an AND or OR expression, we may be able to
9268 : simplify by reassociating. */
9269 102235 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
9270 102235 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
9271 : {
9272 39706 : tree inner1 = gimple_assign_rhs1 (stmt);
9273 39706 : tree inner2 = gimple_assign_rhs2 (stmt);
9274 39706 : gimple *s;
9275 39706 : tree t;
9276 39706 : tree partial = NULL_TREE;
9277 39706 : bool is_or = (innercode == BIT_IOR_EXPR);
9278 :
9279 : /* Check for boolean identities that don't require recursive examination
9280 : of inner1/inner2:
9281 : inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
9282 : inner1 OR (inner1 AND inner2) => inner1
9283 : !inner1 OR (inner1 OR inner2) => true
9284 : !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
9285 : */
9286 39706 : if (inner1 == true_test_var)
9287 0 : return (is_or ? var : inner1);
9288 39706 : else if (inner2 == true_test_var)
9289 0 : return (is_or ? var : inner2);
9290 39706 : else if (inner1 == false_test_var)
9291 0 : return (is_or
9292 0 : ? boolean_true_node
9293 0 : : or_var_with_comparison (type, inner2, false, code2, op2a,
9294 0 : op2b, outer_cond_bb));
9295 39706 : else if (inner2 == false_test_var)
9296 0 : return (is_or
9297 0 : ? boolean_true_node
9298 0 : : or_var_with_comparison (type, inner1, false, code2, op2a,
9299 0 : op2b, outer_cond_bb));
9300 :
9301 : /* Next, redistribute/reassociate the OR across the inner tests.
9302 : Compute the first partial result, (inner1 OR (op2a code op2b)) */
9303 39706 : if (TREE_CODE (inner1) == SSA_NAME
9304 39706 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
9305 38717 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9306 63915 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9307 : gimple_assign_rhs1 (s),
9308 : gimple_assign_rhs2 (s),
9309 : code2, op2a, op2b,
9310 : outer_cond_bb)))
9311 : {
9312 : /* Handle the OR case, where we are reassociating:
9313 : (inner1 OR inner2) OR (op2a code2 op2b)
9314 : => (t OR inner2)
9315 : If the partial result t is a constant, we win. Otherwise
9316 : continue on to try reassociating with the other inner test. */
9317 741 : if (is_or)
9318 : {
9319 31 : if (integer_onep (t))
9320 0 : return boolean_true_node;
9321 31 : else if (integer_zerop (t))
9322 : return inner2;
9323 : }
9324 :
9325 : /* Handle the AND case, where we are redistributing:
9326 : (inner1 AND inner2) OR (op2a code2 op2b)
9327 : => (t AND (inner2 OR (op2a code op2b))) */
9328 710 : else if (integer_zerop (t))
9329 0 : return boolean_false_node;
9330 :
9331 : /* Save partial result for later. */
9332 : partial = t;
9333 : }
9334 :
9335 : /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
9336 39706 : if (TREE_CODE (inner2) == SSA_NAME
9337 39706 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
9338 38997 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9339 76903 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9340 : gimple_assign_rhs1 (s),
9341 : gimple_assign_rhs2 (s),
9342 : code2, op2a, op2b,
9343 : outer_cond_bb)))
9344 : {
9345 : /* Handle the OR case, where we are reassociating:
9346 : (inner1 OR inner2) OR (op2a code2 op2b)
9347 : => (inner1 OR t)
9348 : => (t OR partial) */
9349 492 : if (is_or)
9350 : {
9351 60 : if (integer_zerop (t))
9352 : return inner1;
9353 60 : else if (integer_onep (t))
9354 1 : return boolean_true_node;
9355 : /* If both are the same, we can apply the identity
9356 : (x OR x) == x. */
9357 59 : else if (partial && same_bool_result_p (t, partial))
9358 : return t;
9359 : }
9360 :
9361 : /* Handle the AND case, where we are redistributing:
9362 : (inner1 AND inner2) OR (op2a code2 op2b)
9363 : => (t AND (inner1 OR (op2a code2 op2b)))
9364 : => (t AND partial) */
9365 : else
9366 : {
9367 432 : if (integer_zerop (t))
9368 0 : return boolean_false_node;
9369 432 : else if (partial)
9370 : {
9371 : /* We already got a simplification for the other
9372 : operand to the redistributed AND expression. The
9373 : interesting case is when at least one is true.
9374 : Or, if both are the same, we can apply the identity
9375 : (x AND x) == x. */
9376 14 : if (integer_onep (partial))
9377 : return t;
9378 14 : else if (integer_onep (t))
9379 : return partial;
9380 4 : else if (same_bool_result_p (t, partial))
9381 : return t;
9382 : }
9383 : }
9384 : }
9385 : }
9386 : return NULL_TREE;
9387 : }
9388 :
9389 : /* Try to simplify the OR of two comparisons defined by
9390 : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
9391 : If this can be done without constructing an intermediate value,
9392 : return the resulting tree; otherwise NULL_TREE is returned.
9393 : This function is deliberately asymmetric as it recurses on SSA_DEFs
9394 : in the first comparison but not the second. */
9395 :
9396 : static tree
9397 963777 : or_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
9398 : enum tree_code code2, tree op2a, tree op2b,
9399 : basic_block outer_cond_bb)
9400 : {
9401 963777 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
9402 :
9403 : /* First check for ((x CODE1 y) OR (x CODE2 y)). */
9404 963777 : if (operand_equal_p (op1a, op2a, 0)
9405 963777 : && operand_equal_p (op1b, op2b, 0))
9406 : {
9407 : /* Result will be either NULL_TREE, or a combined comparison. */
9408 3104 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9409 : TRUTH_ORIF_EXPR, code1, code2,
9410 : truth_type, op1a, op1b);
9411 3104 : if (t)
9412 : return t;
9413 : }
9414 :
9415 : /* Likewise the swapped case of the above. */
9416 960705 : if (operand_equal_p (op1a, op2b, 0)
9417 960705 : && operand_equal_p (op1b, op2a, 0))
9418 : {
9419 : /* Result will be either NULL_TREE, or a combined comparison. */
9420 0 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9421 : TRUTH_ORIF_EXPR, code1,
9422 : swap_tree_comparison (code2),
9423 : truth_type, op1a, op1b);
9424 0 : if (t)
9425 : return t;
9426 : }
9427 :
9428 : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
9429 : NAME's definition is a truth value. See if there are any simplifications
9430 : that can be done against the NAME's definition. */
9431 960705 : if (TREE_CODE (op1a) == SSA_NAME
9432 960702 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
9433 1232980 : && (integer_zerop (op1b) || integer_onep (op1b)))
9434 : {
9435 35727 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
9436 72242 : || (code1 == NE_EXPR && integer_onep (op1b)));
9437 68412 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
9438 68412 : switch (gimple_code (stmt))
9439 : {
9440 39630 : case GIMPLE_ASSIGN:
9441 : /* Try to simplify by copy-propagating the definition. */
9442 39630 : return or_var_with_comparison (type, op1a, invert, code2, op2a,
9443 39630 : op2b, outer_cond_bb);
9444 :
9445 15661 : case GIMPLE_PHI:
9446 : /* If every argument to the PHI produces the same result when
9447 : ORed with the second comparison, we win.
9448 : Do not do this unless the type is bool since we need a bool
9449 : result here anyway. */
9450 15661 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
9451 : {
9452 : tree result = NULL_TREE;
9453 : unsigned i;
9454 878 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
9455 : {
9456 878 : tree arg = gimple_phi_arg_def (stmt, i);
9457 :
9458 : /* If this PHI has itself as an argument, ignore it.
9459 : If all the other args produce the same result,
9460 : we're still OK. */
9461 878 : if (arg == gimple_phi_result (stmt))
9462 0 : continue;
9463 878 : else if (TREE_CODE (arg) == INTEGER_CST)
9464 : {
9465 737 : if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
9466 : {
9467 339 : if (!result)
9468 199 : result = boolean_true_node;
9469 140 : else if (!integer_onep (result))
9470 : return NULL_TREE;
9471 : }
9472 398 : else if (!result)
9473 192 : result = fold_build2 (code2, boolean_type_node,
9474 : op2a, op2b);
9475 206 : else if (!same_bool_comparison_p (result,
9476 : code2, op2a, op2b))
9477 : return NULL_TREE;
9478 : }
9479 141 : else if (TREE_CODE (arg) == SSA_NAME
9480 141 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
9481 : {
9482 141 : tree temp;
9483 141 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
9484 : /* In simple cases we can look through PHI nodes,
9485 : but we have to be careful with loops.
9486 : See PR49073. */
9487 141 : if (! dom_info_available_p (CDI_DOMINATORS)
9488 141 : || gimple_bb (def_stmt) == gimple_bb (stmt)
9489 282 : || dominated_by_p (CDI_DOMINATORS,
9490 141 : gimple_bb (def_stmt),
9491 141 : gimple_bb (stmt)))
9492 21 : return NULL_TREE;
9493 120 : temp = or_var_with_comparison (type, arg, invert, code2,
9494 : op2a, op2b, outer_cond_bb);
9495 120 : if (!temp)
9496 : return NULL_TREE;
9497 0 : else if (!result)
9498 : result = temp;
9499 0 : else if (!same_bool_result_p (result, temp))
9500 : return NULL_TREE;
9501 : }
9502 : else
9503 : return NULL_TREE;
9504 : }
9505 : return result;
9506 : }
9507 :
9508 : default:
9509 : break;
9510 : }
9511 : }
9512 : return NULL_TREE;
9513 : }
9514 :
9515 : /* Try to simplify the OR of two comparisons, specified by
9516 : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9517 : If this can be simplified to a single expression (without requiring
9518 : introducing more SSA variables to hold intermediate values),
9519 : return the resulting tree. Otherwise return NULL_TREE.
9520 : If the result expression is non-null, it has boolean type. */
9521 :
9522 : tree
9523 482980 : maybe_fold_or_comparisons (tree type,
9524 : enum tree_code code1, tree op1a, tree op1b,
9525 : enum tree_code code2, tree op2a, tree op2b,
9526 : basic_block outer_cond_bb)
9527 : {
9528 482980 : if (tree t = or_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9529 : outer_cond_bb))
9530 : return t;
9531 :
9532 479931 : if (tree t = or_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9533 : outer_cond_bb))
9534 : return t;
9535 :
9536 479926 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
9537 : op1a, op1b, code2, op2a,
9538 : op2b, outer_cond_bb))
9539 : return t;
9540 :
9541 : return NULL_TREE;
9542 : }
9543 :
9544 : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9545 :
9546 : Either NULL_TREE, a simplified but non-constant or a constant
9547 : is returned.
9548 :
9549 : ??? This should go into a gimple-fold-inline.h file to be eventually
9550 : privatized with the single valueize function used in the various TUs
9551 : to avoid the indirect function call overhead. */
9552 :
9553 : tree
9554 413851804 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
9555 : tree (*gvalueize) (tree))
9556 : {
9557 413851804 : gimple_match_op res_op;
9558 : /* ??? The SSA propagators do not correctly deal with following SSA use-def
9559 : edges if there are intermediate VARYING defs. For this reason
9560 : do not follow SSA edges here even though SCCVN can technically
9561 : just deal fine with that. */
9562 413851804 : if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
9563 : {
9564 55114353 : tree res = NULL_TREE;
9565 55114353 : if (gimple_simplified_result_is_gimple_val (&res_op))
9566 33640641 : res = res_op.ops[0];
9567 21473712 : else if (mprts_hook)
9568 7518072 : res = mprts_hook (&res_op);
9569 41158713 : if (res)
9570 : {
9571 35488219 : if (dump_file && dump_flags & TDF_DETAILS)
9572 : {
9573 9461 : fprintf (dump_file, "Match-and-simplified ");
9574 9461 : print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
9575 9461 : fprintf (dump_file, " to ");
9576 9461 : print_generic_expr (dump_file, res);
9577 9461 : fprintf (dump_file, "\n");
9578 : }
9579 35488219 : return res;
9580 : }
9581 : }
9582 :
9583 378363585 : location_t loc = gimple_location (stmt);
9584 378363585 : switch (gimple_code (stmt))
9585 : {
9586 326706432 : case GIMPLE_ASSIGN:
9587 326706432 : {
9588 326706432 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
9589 :
9590 326706432 : switch (get_gimple_rhs_class (subcode))
9591 : {
9592 110984941 : case GIMPLE_SINGLE_RHS:
9593 110984941 : {
9594 110984941 : tree rhs = gimple_assign_rhs1 (stmt);
9595 110984941 : enum tree_code_class kind = TREE_CODE_CLASS (subcode);
9596 :
9597 110984941 : if (TREE_CODE (rhs) == SSA_NAME)
9598 : {
9599 : /* If the RHS is an SSA_NAME, return its known constant value,
9600 : if any. */
9601 9528674 : return (*valueize) (rhs);
9602 : }
9603 : /* Handle propagating invariant addresses into address
9604 : operations. */
9605 101456267 : else if (TREE_CODE (rhs) == ADDR_EXPR
9606 101456267 : && !is_gimple_min_invariant (rhs))
9607 : {
9608 6029730 : poly_int64 offset = 0;
9609 6029730 : tree base;
9610 6029730 : base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
9611 : &offset,
9612 : valueize);
9613 6029730 : if (base
9614 6029730 : && (CONSTANT_CLASS_P (base)
9615 5377377 : || decl_address_invariant_p (base)))
9616 195160 : return build_invariant_address (TREE_TYPE (rhs),
9617 195160 : base, offset);
9618 : }
9619 95426537 : else if (TREE_CODE (rhs) == CONSTRUCTOR
9620 1059697 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
9621 96908998 : && known_eq (CONSTRUCTOR_NELTS (rhs),
9622 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
9623 : {
9624 405992 : unsigned i, nelts;
9625 405992 : tree val;
9626 :
9627 405992 : nelts = CONSTRUCTOR_NELTS (rhs);
9628 405992 : tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
9629 905158 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
9630 : {
9631 487879 : val = (*valueize) (val);
9632 487879 : if (TREE_CODE (val) == INTEGER_CST
9633 415153 : || TREE_CODE (val) == REAL_CST
9634 394705 : || TREE_CODE (val) == FIXED_CST)
9635 93174 : vec.quick_push (val);
9636 : else
9637 : return NULL_TREE;
9638 : }
9639 :
9640 11287 : return vec.build ();
9641 405992 : }
9642 100855115 : if (subcode == OBJ_TYPE_REF)
9643 : {
9644 330323 : tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
9645 : /* If callee is constant, we can fold away the wrapper. */
9646 330323 : if (is_gimple_min_invariant (val))
9647 : return val;
9648 : }
9649 :
9650 100854933 : if (kind == tcc_reference)
9651 : {
9652 66910915 : if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
9653 64702119 : || TREE_CODE (rhs) == REALPART_EXPR
9654 63850999 : || TREE_CODE (rhs) == IMAGPART_EXPR)
9655 68766284 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9656 : {
9657 3139268 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9658 3139268 : return fold_unary_loc (EXPR_LOCATION (rhs),
9659 3139268 : TREE_CODE (rhs),
9660 6278536 : TREE_TYPE (rhs), val);
9661 : }
9662 63771647 : else if (TREE_CODE (rhs) == BIT_FIELD_REF
9663 63771647 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9664 : {
9665 581975 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9666 581975 : return fold_ternary_loc (EXPR_LOCATION (rhs),
9667 581975 : TREE_CODE (rhs),
9668 581975 : TREE_TYPE (rhs), val,
9669 581975 : TREE_OPERAND (rhs, 1),
9670 1163950 : TREE_OPERAND (rhs, 2));
9671 : }
9672 63189672 : else if (TREE_CODE (rhs) == MEM_REF
9673 63189672 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9674 : {
9675 13153516 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9676 13153516 : if (TREE_CODE (val) == ADDR_EXPR
9677 13153516 : && is_gimple_min_invariant (val))
9678 : {
9679 942091 : tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
9680 : unshare_expr (val),
9681 : TREE_OPERAND (rhs, 1));
9682 942091 : if (tem)
9683 63189672 : rhs = tem;
9684 : }
9685 : }
9686 63189672 : return fold_const_aggregate_ref_1 (rhs, valueize);
9687 : }
9688 33944018 : else if (kind == tcc_declaration)
9689 7637432 : return get_symbol_constant_value (rhs);
9690 : return rhs;
9691 : }
9692 :
9693 : case GIMPLE_UNARY_RHS:
9694 : return NULL_TREE;
9695 :
9696 161758823 : case GIMPLE_BINARY_RHS:
9697 : /* Translate &x + CST into an invariant form suitable for
9698 : further propagation. */
9699 161758823 : if (subcode == POINTER_PLUS_EXPR)
9700 : {
9701 21481237 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9702 21481237 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9703 21481237 : if (TREE_CODE (op0) == ADDR_EXPR
9704 5489626 : && TREE_CODE (op1) == INTEGER_CST)
9705 : {
9706 516838 : tree off = fold_convert (ptr_type_node, op1);
9707 516838 : return build1_loc
9708 1033676 : (loc, ADDR_EXPR, TREE_TYPE (op0),
9709 516838 : fold_build2 (MEM_REF,
9710 : TREE_TYPE (TREE_TYPE (op0)),
9711 516838 : unshare_expr (op0), off));
9712 : }
9713 : }
9714 : /* Canonicalize bool != 0 and bool == 0 appearing after
9715 : valueization. While gimple_simplify handles this
9716 : it can get confused by the ~X == 1 -> X == 0 transform
9717 : which we cant reduce to a SSA name or a constant
9718 : (and we have no way to tell gimple_simplify to not
9719 : consider those transforms in the first place). */
9720 140277586 : else if (subcode == EQ_EXPR
9721 140277586 : || subcode == NE_EXPR)
9722 : {
9723 3209850 : tree lhs = gimple_assign_lhs (stmt);
9724 3209850 : tree op0 = gimple_assign_rhs1 (stmt);
9725 3209850 : if (useless_type_conversion_p (TREE_TYPE (lhs),
9726 3209850 : TREE_TYPE (op0)))
9727 : {
9728 24525 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9729 24525 : op0 = (*valueize) (op0);
9730 24525 : if (TREE_CODE (op0) == INTEGER_CST)
9731 705 : std::swap (op0, op1);
9732 24525 : if (TREE_CODE (op1) == INTEGER_CST
9733 24525 : && ((subcode == NE_EXPR && integer_zerop (op1))
9734 2310 : || (subcode == EQ_EXPR && integer_onep (op1))))
9735 282 : return op0;
9736 : }
9737 : }
9738 : return NULL_TREE;
9739 :
9740 712815 : case GIMPLE_TERNARY_RHS:
9741 712815 : {
9742 : /* Handle ternary operators that can appear in GIMPLE form. */
9743 712815 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9744 712815 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9745 712815 : tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
9746 712815 : return fold_ternary_loc (loc, subcode,
9747 712815 : TREE_TYPE (gimple_assign_lhs (stmt)),
9748 712815 : op0, op1, op2);
9749 : }
9750 :
9751 0 : default:
9752 0 : gcc_unreachable ();
9753 : }
9754 : }
9755 :
9756 14232519 : case GIMPLE_CALL:
9757 14232519 : {
9758 14232519 : tree fn;
9759 14232519 : gcall *call_stmt = as_a <gcall *> (stmt);
9760 :
9761 14232519 : if (gimple_call_internal_p (stmt))
9762 : {
9763 1302342 : enum tree_code subcode = ERROR_MARK;
9764 1302342 : switch (gimple_call_internal_fn (stmt))
9765 : {
9766 : case IFN_UBSAN_CHECK_ADD:
9767 : subcode = PLUS_EXPR;
9768 : break;
9769 7991 : case IFN_UBSAN_CHECK_SUB:
9770 7991 : subcode = MINUS_EXPR;
9771 7991 : break;
9772 6815 : case IFN_UBSAN_CHECK_MUL:
9773 6815 : subcode = MULT_EXPR;
9774 6815 : break;
9775 142819 : case IFN_BUILTIN_EXPECT:
9776 142819 : {
9777 142819 : tree arg0 = gimple_call_arg (stmt, 0);
9778 142819 : tree op0 = (*valueize) (arg0);
9779 142819 : if (TREE_CODE (op0) == INTEGER_CST)
9780 : return op0;
9781 : return NULL_TREE;
9782 : }
9783 : default:
9784 : return NULL_TREE;
9785 : }
9786 22932 : tree arg0 = gimple_call_arg (stmt, 0);
9787 22932 : tree arg1 = gimple_call_arg (stmt, 1);
9788 22932 : tree op0 = (*valueize) (arg0);
9789 22932 : tree op1 = (*valueize) (arg1);
9790 :
9791 22932 : if (TREE_CODE (op0) != INTEGER_CST
9792 2499 : || TREE_CODE (op1) != INTEGER_CST)
9793 : {
9794 22410 : switch (subcode)
9795 : {
9796 6715 : case MULT_EXPR:
9797 : /* x * 0 = 0 * x = 0 without overflow. */
9798 6715 : if (integer_zerop (op0) || integer_zerop (op1))
9799 20 : return build_zero_cst (TREE_TYPE (arg0));
9800 : break;
9801 7649 : case MINUS_EXPR:
9802 : /* y - y = 0 without overflow. */
9803 7649 : if (operand_equal_p (op0, op1, 0))
9804 0 : return build_zero_cst (TREE_TYPE (arg0));
9805 : break;
9806 : default:
9807 : break;
9808 : }
9809 : }
9810 22912 : tree res
9811 22912 : = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
9812 22912 : if (res
9813 2870 : && TREE_CODE (res) == INTEGER_CST
9814 23434 : && !TREE_OVERFLOW (res))
9815 : return res;
9816 : return NULL_TREE;
9817 : }
9818 :
9819 12930177 : fn = (*valueize) (gimple_call_fn (stmt));
9820 12930177 : if (TREE_CODE (fn) == ADDR_EXPR
9821 12303419 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
9822 12303355 : && fndecl_built_in_p (TREE_OPERAND (fn, 0))
9823 18934207 : && gimple_builtin_call_types_compatible_p (stmt,
9824 6004030 : TREE_OPERAND (fn, 0)))
9825 : {
9826 5902241 : tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
9827 5902241 : tree retval;
9828 5902241 : unsigned i;
9829 18291899 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
9830 12389658 : args[i] = (*valueize) (gimple_call_arg (stmt, i));
9831 5902241 : retval = fold_builtin_call_array (loc,
9832 : gimple_call_return_type (call_stmt),
9833 : fn, gimple_call_num_args (stmt), args);
9834 5902241 : if (retval)
9835 : {
9836 : /* fold_call_expr wraps the result inside a NOP_EXPR. */
9837 52283 : STRIP_NOPS (retval);
9838 52283 : retval = fold_convert (gimple_call_return_type (call_stmt),
9839 : retval);
9840 : }
9841 5902241 : return retval;
9842 : }
9843 : return NULL_TREE;
9844 : }
9845 :
9846 : default:
9847 : return NULL_TREE;
9848 : }
9849 : }
9850 :
9851 : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9852 : Returns NULL_TREE if folding to a constant is not possible, otherwise
9853 : returns a constant according to is_gimple_min_invariant. */
9854 :
9855 : tree
9856 4367 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
9857 : {
9858 4367 : tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
9859 4367 : if (res && is_gimple_min_invariant (res))
9860 : return res;
9861 : return NULL_TREE;
9862 : }
9863 :
9864 :
9865 : /* The following set of functions are supposed to fold references using
9866 : their constant initializers. */
9867 :
9868 : /* See if we can find constructor defining value of BASE.
9869 : When we know the constructor with constant offset (such as
9870 : base is array[40] and we do know constructor of array), then
9871 : BIT_OFFSET is adjusted accordingly.
9872 :
9873 : As a special case, return error_mark_node when constructor
9874 : is not explicitly available, but it is known to be zero
9875 : such as 'static const int a;'. */
9876 : static tree
9877 120063184 : get_base_constructor (tree base, poly_int64 *bit_offset,
9878 : tree (*valueize)(tree))
9879 : {
9880 120159266 : poly_int64 bit_offset2, size, max_size;
9881 120159266 : bool reverse;
9882 :
9883 120159266 : if (TREE_CODE (base) == MEM_REF)
9884 : {
9885 127156822 : poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
9886 63578411 : if (!boff.to_shwi (bit_offset))
9887 63248011 : return NULL_TREE;
9888 :
9889 63578064 : if (valueize
9890 63578064 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
9891 33364966 : base = valueize (TREE_OPERAND (base, 0));
9892 63578064 : if (!base || TREE_CODE (base) != ADDR_EXPR)
9893 : return NULL_TREE;
9894 330400 : base = TREE_OPERAND (base, 0);
9895 : }
9896 56580855 : else if (valueize
9897 29429937 : && TREE_CODE (base) == SSA_NAME)
9898 0 : base = valueize (base);
9899 :
9900 : /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
9901 : DECL_INITIAL. If BASE is a nested reference into another
9902 : ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
9903 : the inner reference. */
9904 56911255 : switch (TREE_CODE (base))
9905 : {
9906 49644243 : case VAR_DECL:
9907 49644243 : case CONST_DECL:
9908 49644243 : {
9909 49644243 : tree init = ctor_for_folding (base);
9910 :
9911 : /* Our semantic is exact opposite of ctor_for_folding;
9912 : NULL means unknown, while error_mark_node is 0. */
9913 49644243 : if (init == error_mark_node)
9914 : return NULL_TREE;
9915 1251460 : if (!init)
9916 1173 : return error_mark_node;
9917 : return init;
9918 : }
9919 :
9920 96082 : case VIEW_CONVERT_EXPR:
9921 96082 : return get_base_constructor (TREE_OPERAND (base, 0),
9922 96082 : bit_offset, valueize);
9923 :
9924 329460 : case ARRAY_REF:
9925 329460 : case COMPONENT_REF:
9926 329460 : base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
9927 : &reverse);
9928 329460 : if (!known_size_p (max_size) || maybe_ne (size, max_size))
9929 : return NULL_TREE;
9930 268870 : *bit_offset += bit_offset2;
9931 268870 : return get_base_constructor (base, bit_offset, valueize);
9932 :
9933 : case CONSTRUCTOR:
9934 : return base;
9935 :
9936 6841470 : default:
9937 6841470 : if (CONSTANT_CLASS_P (base))
9938 : return base;
9939 :
9940 : return NULL_TREE;
9941 : }
9942 : }
9943 :
9944 : /* CTOR is a CONSTRUCTOR of an array or vector type. Fold a reference of SIZE
9945 : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
9946 : the reference; otherwise the type of the referenced element is used instead.
9947 : When SIZE is zero, attempt to fold a reference to the entire element OFFSET
9948 : refers to. Increment *SUBOFF by the bit offset of the accessed element. */
9949 :
9950 : static tree
9951 660867 : fold_array_ctor_reference (tree type, tree ctor,
9952 : unsigned HOST_WIDE_INT offset,
9953 : unsigned HOST_WIDE_INT size,
9954 : tree from_decl,
9955 : unsigned HOST_WIDE_INT *suboff)
9956 : {
9957 660867 : offset_int low_bound;
9958 660867 : offset_int elt_size;
9959 660867 : offset_int access_index;
9960 660867 : tree domain_type = NULL_TREE;
9961 660867 : HOST_WIDE_INT inner_offset;
9962 :
9963 : /* Compute low bound and elt size. */
9964 660867 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
9965 660867 : domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
9966 660867 : if (domain_type && TYPE_MIN_VALUE (domain_type))
9967 : {
9968 : /* Static constructors for variably sized objects make no sense. */
9969 660867 : if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
9970 : return NULL_TREE;
9971 660867 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
9972 : }
9973 : else
9974 0 : low_bound = 0;
9975 : /* Static constructors for variably sized objects make no sense. */
9976 660867 : if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
9977 : return NULL_TREE;
9978 660867 : elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
9979 :
9980 : /* When TYPE is non-null, verify that it specifies a constant-sized
9981 : access of a multiple of the array element size. Avoid division
9982 : by zero below when ELT_SIZE is zero, such as with the result of
9983 : an initializer for a zero-length array or an empty struct. */
9984 660867 : if (elt_size == 0
9985 660867 : || (type
9986 660831 : && (!TYPE_SIZE_UNIT (type)
9987 660831 : || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
9988 36 : return NULL_TREE;
9989 :
9990 : /* Compute the array index we look for. */
9991 660831 : access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
9992 : elt_size);
9993 660831 : access_index += low_bound;
9994 :
9995 : /* And offset within the access. */
9996 660831 : inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
9997 :
9998 660831 : unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
9999 660831 : if (size > elt_sz * BITS_PER_UNIT)
10000 : {
10001 : /* native_encode_expr constraints. */
10002 21015 : if (size > MAX_BITSIZE_MODE_ANY_MODE
10003 17042 : || size % BITS_PER_UNIT != 0
10004 17042 : || inner_offset % BITS_PER_UNIT != 0
10005 17042 : || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
10006 : return NULL_TREE;
10007 :
10008 17042 : unsigned ctor_idx;
10009 17042 : tree val = get_array_ctor_element_at_index (ctor, access_index,
10010 : &ctor_idx);
10011 17066 : if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
10012 23 : return build_zero_cst (type);
10013 :
10014 : /* native-encode adjacent ctor elements. */
10015 17019 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10016 17019 : unsigned bufoff = 0;
10017 17019 : offset_int index = 0;
10018 17019 : offset_int max_index = access_index;
10019 17019 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
10020 17019 : if (!val)
10021 1 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
10022 17018 : else if (!CONSTANT_CLASS_P (val))
10023 : return NULL_TREE;
10024 16857 : if (!elt->index)
10025 : ;
10026 14414 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
10027 : {
10028 18 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
10029 18 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
10030 : }
10031 : else
10032 14396 : index = max_index = wi::to_offset (elt->index);
10033 16857 : index = wi::umax (index, access_index);
10034 108833 : do
10035 : {
10036 108833 : if (bufoff + elt_sz > sizeof (buf))
10037 0 : elt_sz = sizeof (buf) - bufoff;
10038 108833 : int len;
10039 108833 : if (TREE_CODE (val) == RAW_DATA_CST)
10040 : {
10041 20 : gcc_assert (inner_offset == 0);
10042 20 : if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
10043 : return NULL_TREE;
10044 40 : inner_offset = (access_index
10045 20 : - wi::to_offset (elt->index)).to_uhwi ();
10046 20 : len = MIN (sizeof (buf) - bufoff,
10047 : (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
10048 20 : memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
10049 : len);
10050 20 : access_index += len - 1;
10051 : }
10052 : else
10053 : {
10054 217626 : len = native_encode_expr (val, buf + bufoff, elt_sz,
10055 108813 : inner_offset / BITS_PER_UNIT);
10056 108813 : if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
10057 : return NULL_TREE;
10058 : }
10059 108833 : inner_offset = 0;
10060 108833 : bufoff += len;
10061 :
10062 108833 : access_index += 1;
10063 108833 : if (wi::cmpu (access_index, index) == 0)
10064 2 : val = elt->value;
10065 108831 : else if (wi::cmpu (access_index, max_index) > 0)
10066 : {
10067 108609 : ctor_idx++;
10068 108609 : if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
10069 : {
10070 15035 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
10071 15035 : ++max_index;
10072 : }
10073 : else
10074 : {
10075 93574 : elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
10076 93574 : index = 0;
10077 93574 : max_index = access_index;
10078 93574 : if (!elt->index)
10079 : ;
10080 92766 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
10081 : {
10082 0 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
10083 0 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
10084 : }
10085 : else
10086 92766 : index = max_index = wi::to_offset (elt->index);
10087 93574 : index = wi::umax (index, access_index);
10088 93574 : if (wi::cmpu (access_index, index) == 0)
10089 93569 : val = elt->value;
10090 : else
10091 5 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
10092 : }
10093 : }
10094 : }
10095 108833 : while (bufoff < size / BITS_PER_UNIT);
10096 16857 : *suboff += size;
10097 16857 : return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
10098 : }
10099 :
10100 639816 : unsigned ctor_idx;
10101 639816 : if (tree val = get_array_ctor_element_at_index (ctor, access_index,
10102 : &ctor_idx))
10103 : {
10104 638695 : if (TREE_CODE (val) == RAW_DATA_CST)
10105 : {
10106 2578 : if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
10107 : return NULL_TREE;
10108 2570 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
10109 2570 : if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
10110 : return NULL_TREE;
10111 2570 : unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
10112 2570 : val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
10113 : }
10114 638687 : if (!size && TREE_CODE (val) != CONSTRUCTOR)
10115 : {
10116 : /* For the final reference to the entire accessed element
10117 : (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
10118 : may be null) in favor of the type of the element, and set
10119 : SIZE to the size of the accessed element. */
10120 22903 : inner_offset = 0;
10121 22903 : type = TREE_TYPE (val);
10122 22903 : size = elt_sz * BITS_PER_UNIT;
10123 : }
10124 1636847 : else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
10125 443394 : && TREE_CODE (val) == CONSTRUCTOR
10126 15453 : && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
10127 : /* If this isn't the last element in the CTOR and a CTOR itself
10128 : and it does not cover the whole object we are requesting give up
10129 : since we're not set up for combining from multiple CTORs. */
10130 26 : return NULL_TREE;
10131 :
10132 638661 : *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
10133 638661 : return fold_ctor_reference (type, val, inner_offset, size, from_decl,
10134 : suboff);
10135 : }
10136 :
10137 : /* Memory not explicitly mentioned in constructor is 0 (or
10138 : the reference is out of range). */
10139 1121 : return type ? build_zero_cst (type) : NULL_TREE;
10140 : }
10141 :
10142 : /* CTOR is a CONSTRUCTOR of a record or union type. Fold a reference of SIZE
10143 : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
10144 : the reference; otherwise the type of the referenced member is used instead.
10145 : When SIZE is zero, attempt to fold a reference to the entire member OFFSET
10146 : refers to. Increment *SUBOFF by the bit offset of the accessed member. */
10147 :
10148 : static tree
10149 75043 : fold_nonarray_ctor_reference (tree type, tree ctor,
10150 : unsigned HOST_WIDE_INT offset,
10151 : unsigned HOST_WIDE_INT size,
10152 : tree from_decl,
10153 : unsigned HOST_WIDE_INT *suboff)
10154 : {
10155 75043 : unsigned HOST_WIDE_INT cnt;
10156 75043 : tree cfield, cval;
10157 :
10158 114728 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
10159 : {
10160 105769 : tree byte_offset = DECL_FIELD_OFFSET (cfield);
10161 105769 : tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
10162 105769 : tree field_size = DECL_SIZE (cfield);
10163 :
10164 105769 : if (!field_size)
10165 : {
10166 : /* Determine the size of the flexible array member from
10167 : the size of the initializer provided for it. */
10168 847 : field_size = TYPE_SIZE (TREE_TYPE (cval));
10169 : }
10170 :
10171 : /* Variable sized objects in static constructors makes no sense,
10172 : but field_size can be NULL for flexible array members. */
10173 105769 : gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
10174 : && TREE_CODE (byte_offset) == INTEGER_CST
10175 : && (field_size != NULL_TREE
10176 : ? TREE_CODE (field_size) == INTEGER_CST
10177 : : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
10178 :
10179 : /* Compute bit offset of the field. */
10180 105769 : offset_int bitoffset
10181 105769 : = (wi::to_offset (field_offset)
10182 105769 : + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
10183 : /* Compute bit offset where the field ends. */
10184 105769 : offset_int bitoffset_end;
10185 105769 : if (field_size != NULL_TREE)
10186 105769 : bitoffset_end = bitoffset + wi::to_offset (field_size);
10187 : else
10188 0 : bitoffset_end = 0;
10189 :
10190 : /* Compute the bit offset of the end of the desired access.
10191 : As a special case, if the size of the desired access is
10192 : zero, assume the access is to the entire field (and let
10193 : the caller make any necessary adjustments by storing
10194 : the actual bounds of the field in FIELDBOUNDS). */
10195 105769 : offset_int access_end = offset_int (offset);
10196 105769 : if (size)
10197 64341 : access_end += size;
10198 : else
10199 41428 : access_end = bitoffset_end;
10200 :
10201 : /* Is there any overlap between the desired access at
10202 : [OFFSET, OFFSET+SIZE) and the offset of the field within
10203 : the object at [BITOFFSET, BITOFFSET_END)? */
10204 105769 : if (wi::cmps (access_end, bitoffset) > 0
10205 105769 : && (field_size == NULL_TREE
10206 103438 : || wi::lts_p (offset, bitoffset_end)))
10207 : {
10208 66084 : *suboff += bitoffset.to_uhwi ();
10209 :
10210 66084 : if (!size && TREE_CODE (cval) != CONSTRUCTOR)
10211 : {
10212 : /* For the final reference to the entire accessed member
10213 : (SIZE is zero), reset OFFSET, disegard TYPE (which may
10214 : be null) in favor of the type of the member, and set
10215 : SIZE to the size of the accessed member. */
10216 19305 : offset = bitoffset.to_uhwi ();
10217 19305 : type = TREE_TYPE (cval);
10218 19305 : size = (bitoffset_end - bitoffset).to_uhwi ();
10219 : }
10220 :
10221 : /* We do have overlap. Now see if the field is large enough
10222 : to cover the access. Give up for accesses that extend
10223 : beyond the end of the object or that span multiple fields. */
10224 66084 : if (wi::cmps (access_end, bitoffset_end) > 0)
10225 : return NULL_TREE;
10226 65461 : if (offset < bitoffset)
10227 : return NULL_TREE;
10228 :
10229 65461 : offset_int inner_offset = offset_int (offset) - bitoffset;
10230 :
10231 : /* Integral bit-fields are left-justified on big-endian targets, so
10232 : we must arrange for native_encode_int to start at their MSB. */
10233 65461 : if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
10234 : {
10235 65461 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
10236 : return NULL_TREE;
10237 65461 : if (BYTES_BIG_ENDIAN)
10238 : {
10239 : tree ctype = TREE_TYPE (cfield);
10240 : unsigned int encoding_size;
10241 : if (TYPE_MODE (ctype) != BLKmode)
10242 : encoding_size
10243 : = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (ctype));
10244 : else
10245 : encoding_size = TREE_INT_CST_LOW (TYPE_SIZE (ctype));
10246 : inner_offset += encoding_size - wi::to_offset (field_size);
10247 : }
10248 : }
10249 :
10250 65461 : return fold_ctor_reference (type, cval,
10251 65461 : inner_offset.to_uhwi (), size,
10252 : from_decl, suboff);
10253 : }
10254 : }
10255 :
10256 8959 : if (!type)
10257 : return NULL_TREE;
10258 :
10259 8959 : return build_zero_cst (type);
10260 : }
10261 :
10262 : /* CTOR is a value initializing memory. Fold a reference of TYPE and
10263 : bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
10264 : is zero, attempt to fold a reference to the entire subobject
10265 : which OFFSET refers to. This is used when folding accesses to
10266 : string members of aggregates. When non-null, set *SUBOFF to
10267 : the bit offset of the accessed subobject. */
10268 :
10269 : tree
10270 1532530 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
10271 : const poly_uint64 &poly_size, tree from_decl,
10272 : unsigned HOST_WIDE_INT *suboff /* = NULL */)
10273 : {
10274 1532530 : tree ret;
10275 :
10276 : /* We found the field with exact match. */
10277 1532530 : if (type
10278 1532530 : && useless_type_conversion_p (type, TREE_TYPE (ctor))
10279 2192485 : && known_eq (poly_offset, 0U))
10280 658705 : return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
10281 :
10282 : /* The remaining optimizations need a constant size and offset. */
10283 873825 : unsigned HOST_WIDE_INT size, offset;
10284 873825 : if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
10285 : return NULL_TREE;
10286 :
10287 : /* We are at the end of walk, see if we can view convert the
10288 : result. */
10289 873825 : if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
10290 : /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
10291 23175 : && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
10292 23068 : && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
10293 : {
10294 14348 : ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
10295 14348 : if (ret)
10296 : {
10297 14348 : ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
10298 14348 : if (ret)
10299 14328 : STRIP_USELESS_TYPE_CONVERSION (ret);
10300 : }
10301 14348 : return ret;
10302 : }
10303 :
10304 : /* For constants and byte-aligned/sized reads, try to go through
10305 : native_encode/interpret. */
10306 859477 : if (CONSTANT_CLASS_P (ctor)
10307 : && BITS_PER_UNIT == 8
10308 115303 : && offset % BITS_PER_UNIT == 0
10309 115299 : && offset / BITS_PER_UNIT <= INT_MAX
10310 115267 : && size % BITS_PER_UNIT == 0
10311 115258 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10312 974437 : && can_native_interpret_type_p (type))
10313 : {
10314 99935 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10315 199870 : int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
10316 99935 : offset / BITS_PER_UNIT);
10317 99935 : if (len > 0)
10318 99217 : return native_interpret_expr (type, buf, len);
10319 : }
10320 :
10321 : /* For constructors, try first a recursive local processing, but in any case
10322 : this requires the native storage order. */
10323 760260 : if (TREE_CODE (ctor) == CONSTRUCTOR
10324 760260 : && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
10325 736140 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
10326 : {
10327 735910 : unsigned HOST_WIDE_INT dummy = 0;
10328 735910 : if (!suboff)
10329 611822 : suboff = &dummy;
10330 :
10331 735910 : tree ret;
10332 735910 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
10333 735910 : || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
10334 660867 : ret = fold_array_ctor_reference (type, ctor, offset, size,
10335 : from_decl, suboff);
10336 : else
10337 75043 : ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
10338 : from_decl, suboff);
10339 :
10340 : /* Otherwise fall back to native_encode_initializer. This may be done
10341 : only from the outermost fold_ctor_reference call (because it itself
10342 : recurses into CONSTRUCTORs and doesn't update suboff). */
10343 735910 : if (ret == NULL_TREE
10344 198569 : && suboff == &dummy
10345 : && BITS_PER_UNIT == 8
10346 190214 : && offset % BITS_PER_UNIT == 0
10347 190212 : && offset / BITS_PER_UNIT <= INT_MAX
10348 190212 : && size % BITS_PER_UNIT == 0
10349 190203 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10350 922140 : && can_native_interpret_type_p (type))
10351 : {
10352 172952 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10353 345904 : int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
10354 172952 : offset / BITS_PER_UNIT);
10355 172952 : if (len > 0)
10356 1302 : return native_interpret_expr (type, buf, len);
10357 : }
10358 :
10359 734608 : return ret;
10360 : }
10361 :
10362 : return NULL_TREE;
10363 : }
10364 :
10365 : /* Return the tree representing the element referenced by T if T is an
10366 : ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
10367 : names using VALUEIZE. Return NULL_TREE otherwise. */
10368 :
10369 : tree
10370 124759381 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
10371 : {
10372 124759381 : tree ctor, idx, base;
10373 124759381 : poly_int64 offset, size, max_size;
10374 124759381 : tree tem;
10375 124759381 : bool reverse;
10376 :
10377 124759381 : if (TREE_THIS_VOLATILE (t))
10378 : return NULL_TREE;
10379 :
10380 124443066 : if (DECL_P (t))
10381 259548 : return get_symbol_constant_value (t);
10382 :
10383 124183518 : tem = fold_read_from_constant_string (t);
10384 124183518 : if (tem)
10385 : return tem;
10386 :
10387 124181672 : switch (TREE_CODE (t))
10388 : {
10389 10359806 : case ARRAY_REF:
10390 10359806 : case ARRAY_RANGE_REF:
10391 : /* Constant indexes are handled well by get_base_constructor.
10392 : Only special case variable offsets.
10393 : FIXME: This code can't handle nested references with variable indexes
10394 : (they will be handled only by iteration of ccp). Perhaps we can bring
10395 : get_ref_base_and_extent here and make it use a valueize callback. */
10396 10359806 : if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
10397 6100106 : && valueize
10398 4087774 : && (idx = (*valueize) (TREE_OPERAND (t, 1)))
10399 14447580 : && poly_int_tree_p (idx))
10400 : {
10401 1638534 : tree low_bound, unit_size;
10402 :
10403 : /* If the resulting bit-offset is constant, track it. */
10404 1638534 : if ((low_bound = array_ref_low_bound (t),
10405 1638534 : poly_int_tree_p (low_bound))
10406 1638534 : && (unit_size = array_ref_element_size (t),
10407 1638534 : tree_fits_uhwi_p (unit_size)))
10408 : {
10409 1638534 : poly_offset_int woffset
10410 1638534 : = wi::sext (wi::to_poly_offset (idx)
10411 3277068 : - wi::to_poly_offset (low_bound),
10412 1638534 : TYPE_PRECISION (sizetype));
10413 1638534 : woffset *= tree_to_uhwi (unit_size);
10414 1638534 : woffset *= BITS_PER_UNIT;
10415 1638534 : if (woffset.to_shwi (&offset))
10416 : {
10417 1638403 : base = TREE_OPERAND (t, 0);
10418 1638403 : ctor = get_base_constructor (base, &offset, valueize);
10419 : /* Empty constructor. Always fold to 0. */
10420 1638403 : if (ctor == error_mark_node)
10421 1638403 : return build_zero_cst (TREE_TYPE (t));
10422 : /* Out of bound array access. Value is undefined,
10423 : but don't fold. */
10424 1638325 : if (maybe_lt (offset, 0))
10425 : return NULL_TREE;
10426 : /* We cannot determine ctor. */
10427 1637855 : if (!ctor)
10428 : return NULL_TREE;
10429 171174 : return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
10430 171174 : tree_to_uhwi (unit_size)
10431 342348 : * BITS_PER_UNIT,
10432 : base);
10433 : }
10434 : }
10435 : }
10436 : /* Fallthru. */
10437 :
10438 118155911 : case COMPONENT_REF:
10439 118155911 : case BIT_FIELD_REF:
10440 118155911 : case TARGET_MEM_REF:
10441 118155911 : case MEM_REF:
10442 118155911 : base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
10443 118155911 : ctor = get_base_constructor (base, &offset, valueize);
10444 :
10445 : /* We cannot determine ctor. */
10446 118155911 : if (!ctor)
10447 : return NULL_TREE;
10448 : /* Empty constructor. Always fold to 0. */
10449 1172395 : if (ctor == error_mark_node)
10450 1095 : return build_zero_cst (TREE_TYPE (t));
10451 : /* We do not know precise access. */
10452 1171300 : if (!known_size_p (max_size) || maybe_ne (max_size, size))
10453 : return NULL_TREE;
10454 : /* Out of bound array access. Value is undefined, but don't fold. */
10455 478777 : if (maybe_lt (offset, 0))
10456 : return NULL_TREE;
10457 : /* Access with reverse storage order. */
10458 478396 : if (reverse)
10459 : return NULL_TREE;
10460 :
10461 478396 : tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
10462 478396 : if (tem)
10463 : return tem;
10464 :
10465 : /* For bit field reads try to read the representative and
10466 : adjust. */
10467 164828 : if (TREE_CODE (t) == COMPONENT_REF
10468 5149 : && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
10469 164912 : && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
10470 : {
10471 84 : HOST_WIDE_INT csize, coffset;
10472 84 : tree field = TREE_OPERAND (t, 1);
10473 84 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
10474 168 : if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
10475 83 : && size.is_constant (&csize)
10476 83 : && offset.is_constant (&coffset)
10477 83 : && (coffset % BITS_PER_UNIT != 0
10478 81 : || csize % BITS_PER_UNIT != 0)
10479 84 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
10480 : {
10481 10 : poly_int64 bitoffset;
10482 10 : poly_uint64 field_offset, repr_offset;
10483 10 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
10484 20 : && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
10485 10 : bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
10486 : else
10487 : bitoffset = 0;
10488 10 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
10489 10 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
10490 10 : HOST_WIDE_INT bitoff;
10491 10 : int diff = (TYPE_PRECISION (TREE_TYPE (repr))
10492 10 : - TYPE_PRECISION (TREE_TYPE (field)));
10493 10 : if (bitoffset.is_constant (&bitoff)
10494 10 : && bitoff >= 0
10495 10 : && bitoff <= diff)
10496 : {
10497 10 : offset -= bitoff;
10498 10 : size = tree_to_uhwi (DECL_SIZE (repr));
10499 :
10500 10 : tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
10501 10 : size, base);
10502 10 : if (tem && TREE_CODE (tem) == INTEGER_CST)
10503 : {
10504 10 : if (!BYTES_BIG_ENDIAN)
10505 10 : tem = wide_int_to_tree (TREE_TYPE (field),
10506 10 : wi::lrshift (wi::to_wide (tem),
10507 : bitoff));
10508 : else
10509 : tem = wide_int_to_tree (TREE_TYPE (field),
10510 : wi::lrshift (wi::to_wide (tem),
10511 : diff - bitoff));
10512 10 : return tem;
10513 : }
10514 : }
10515 : }
10516 : }
10517 : break;
10518 :
10519 1573341 : case REALPART_EXPR:
10520 1573341 : case IMAGPART_EXPR:
10521 1573341 : {
10522 1573341 : tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
10523 1573341 : if (c && TREE_CODE (c) == COMPLEX_CST)
10524 2774 : return fold_build1_loc (EXPR_LOCATION (t),
10525 5548 : TREE_CODE (t), TREE_TYPE (t), c);
10526 : break;
10527 : }
10528 :
10529 : default:
10530 : break;
10531 : }
10532 :
10533 : return NULL_TREE;
10534 : }
10535 :
10536 : tree
10537 59996368 : fold_const_aggregate_ref (tree t)
10538 : {
10539 59996368 : return fold_const_aggregate_ref_1 (t, NULL);
10540 : }
10541 :
10542 : /* Lookup virtual method with index TOKEN in a virtual table V
10543 : at OFFSET.
10544 : Set CAN_REFER if non-NULL to false if method
10545 : is not referable or if the virtual table is ill-formed (such as rewritten
10546 : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10547 :
10548 : tree
10549 279410 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
10550 : tree v,
10551 : unsigned HOST_WIDE_INT offset,
10552 : bool *can_refer)
10553 : {
10554 279410 : tree vtable = v, init, fn;
10555 279410 : unsigned HOST_WIDE_INT size;
10556 279410 : unsigned HOST_WIDE_INT elt_size, access_index;
10557 279410 : tree domain_type;
10558 :
10559 279410 : if (can_refer)
10560 279410 : *can_refer = true;
10561 :
10562 : /* First of all double check we have virtual table. */
10563 279410 : if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
10564 : {
10565 : /* Pass down that we lost track of the target. */
10566 0 : if (can_refer)
10567 0 : *can_refer = false;
10568 0 : return NULL_TREE;
10569 : }
10570 :
10571 279410 : init = ctor_for_folding (v);
10572 :
10573 : /* The virtual tables should always be born with constructors
10574 : and we always should assume that they are available for
10575 : folding. At the moment we do not stream them in all cases,
10576 : but it should never happen that ctor seem unreachable. */
10577 279410 : gcc_assert (init);
10578 279410 : if (init == error_mark_node)
10579 : {
10580 : /* Pass down that we lost track of the target. */
10581 209 : if (can_refer)
10582 209 : *can_refer = false;
10583 209 : return NULL_TREE;
10584 : }
10585 279201 : gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
10586 279201 : size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
10587 279201 : offset *= BITS_PER_UNIT;
10588 279201 : offset += token * size;
10589 :
10590 : /* Lookup the value in the constructor that is assumed to be array.
10591 : This is equivalent to
10592 : fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
10593 : offset, size, NULL);
10594 : but in a constant time. We expect that frontend produced a simple
10595 : array without indexed initializers. */
10596 :
10597 279201 : gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
10598 279201 : domain_type = TYPE_DOMAIN (TREE_TYPE (init));
10599 279201 : gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
10600 279201 : elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
10601 :
10602 279201 : access_index = offset / BITS_PER_UNIT / elt_size;
10603 279201 : gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
10604 :
10605 : /* This code makes an assumption that there are no
10606 : indexed fields produced by C++ FE, so we can directly index the array. */
10607 279201 : if (access_index < CONSTRUCTOR_NELTS (init))
10608 : {
10609 279200 : fn = CONSTRUCTOR_ELT (init, access_index)->value;
10610 279200 : gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
10611 279200 : STRIP_NOPS (fn);
10612 : }
10613 : else
10614 : fn = NULL;
10615 :
10616 : /* For type inconsistent program we may end up looking up virtual method
10617 : in virtual table that does not contain TOKEN entries. We may overrun
10618 : the virtual table and pick up a constant or RTTI info pointer.
10619 : In any case the call is undefined. */
10620 279200 : if (!fn
10621 279200 : || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
10622 552525 : || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
10623 5876 : fn = builtin_decl_unreachable ();
10624 : else
10625 : {
10626 273325 : fn = TREE_OPERAND (fn, 0);
10627 :
10628 : /* When cgraph node is missing and function is not public, we cannot
10629 : devirtualize. This can happen in WHOPR when the actual method
10630 : ends up in other partition, because we found devirtualization
10631 : possibility too late. */
10632 273325 : if (!can_refer_decl_in_current_unit_p (fn, vtable))
10633 : {
10634 36191 : if (can_refer)
10635 : {
10636 36191 : *can_refer = false;
10637 36191 : return fn;
10638 : }
10639 : return NULL_TREE;
10640 : }
10641 : }
10642 :
10643 : /* Make sure we create a cgraph node for functions we'll reference.
10644 : They can be non-existent if the reference comes from an entry
10645 : of an external vtable for example. */
10646 243010 : cgraph_node::get_create (fn);
10647 :
10648 243010 : return fn;
10649 : }
10650 :
10651 : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
10652 : is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
10653 : KNOWN_BINFO carries the binfo describing the true type of
10654 : OBJ_TYPE_REF_OBJECT(REF).
10655 : Set CAN_REFER if non-NULL to false if method
10656 : is not referable or if the virtual table is ill-formed (such as rewritten
10657 : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10658 :
10659 : tree
10660 270742 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
10661 : bool *can_refer)
10662 : {
10663 270742 : unsigned HOST_WIDE_INT offset;
10664 270742 : tree v;
10665 :
10666 270742 : v = BINFO_VTABLE (known_binfo);
10667 : /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
10668 270742 : if (!v)
10669 : return NULL_TREE;
10670 :
10671 270742 : if (!vtable_pointer_value_to_vtable (v, &v, &offset))
10672 : {
10673 0 : if (can_refer)
10674 0 : *can_refer = false;
10675 0 : return NULL_TREE;
10676 : }
10677 270742 : return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
10678 : }
10679 :
10680 : /* Given a pointer value T, return a simplified version of an
10681 : indirection through T, or NULL_TREE if no simplification is
10682 : possible. Note that the resulting type may be different from
10683 : the type pointed to in the sense that it is still compatible
10684 : from the langhooks point of view. */
10685 :
10686 : tree
10687 2356121 : gimple_fold_indirect_ref (tree t)
10688 : {
10689 2356121 : tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
10690 2356121 : tree sub = t;
10691 2356121 : tree subtype;
10692 :
10693 2356121 : STRIP_NOPS (sub);
10694 2356121 : subtype = TREE_TYPE (sub);
10695 2356121 : if (!POINTER_TYPE_P (subtype)
10696 2356121 : || TYPE_REF_CAN_ALIAS_ALL (ptype))
10697 : return NULL_TREE;
10698 :
10699 2354416 : if (TREE_CODE (sub) == ADDR_EXPR)
10700 : {
10701 87915 : tree op = TREE_OPERAND (sub, 0);
10702 87915 : tree optype = TREE_TYPE (op);
10703 : /* *&p => p */
10704 87915 : if (useless_type_conversion_p (type, optype))
10705 : return op;
10706 :
10707 : /* *(foo *)&fooarray => fooarray[0] */
10708 992 : if (TREE_CODE (optype) == ARRAY_TYPE
10709 307 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
10710 1299 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10711 : {
10712 54 : tree type_domain = TYPE_DOMAIN (optype);
10713 54 : tree min_val = size_zero_node;
10714 54 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10715 54 : min_val = TYPE_MIN_VALUE (type_domain);
10716 54 : if (TREE_CODE (min_val) == INTEGER_CST)
10717 54 : return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
10718 : }
10719 : /* *(foo *)&complexfoo => __real__ complexfoo */
10720 938 : else if (TREE_CODE (optype) == COMPLEX_TYPE
10721 938 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10722 4 : return fold_build1 (REALPART_EXPR, type, op);
10723 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
10724 934 : else if (TREE_CODE (optype) == VECTOR_TYPE
10725 934 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10726 : {
10727 26 : tree part_width = TYPE_SIZE (type);
10728 26 : tree index = bitsize_int (0);
10729 26 : return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
10730 : }
10731 : }
10732 :
10733 : /* *(p + CST) -> ... */
10734 2267409 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
10735 2267409 : && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
10736 : {
10737 33710 : tree addr = TREE_OPERAND (sub, 0);
10738 33710 : tree off = TREE_OPERAND (sub, 1);
10739 33710 : tree addrtype;
10740 :
10741 33710 : STRIP_NOPS (addr);
10742 33710 : addrtype = TREE_TYPE (addr);
10743 :
10744 : /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
10745 33710 : if (TREE_CODE (addr) == ADDR_EXPR
10746 92 : && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
10747 39 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
10748 33738 : && tree_fits_uhwi_p (off))
10749 : {
10750 28 : unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
10751 28 : tree part_width = TYPE_SIZE (type);
10752 28 : unsigned HOST_WIDE_INT part_widthi
10753 28 : = tree_to_shwi (part_width) / BITS_PER_UNIT;
10754 28 : unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
10755 28 : tree index = bitsize_int (indexi);
10756 28 : if (known_lt (offset / part_widthi,
10757 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
10758 28 : return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
10759 : part_width, index);
10760 : }
10761 :
10762 : /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
10763 33682 : if (TREE_CODE (addr) == ADDR_EXPR
10764 64 : && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
10765 33683 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
10766 : {
10767 1 : tree size = TYPE_SIZE_UNIT (type);
10768 1 : if (tree_int_cst_equal (size, off))
10769 1 : return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
10770 : }
10771 :
10772 : /* *(p + CST) -> MEM_REF <p, CST>. */
10773 33681 : if (TREE_CODE (addr) != ADDR_EXPR
10774 33681 : || DECL_P (TREE_OPERAND (addr, 0)))
10775 33663 : return fold_build2 (MEM_REF, type,
10776 : addr,
10777 : wide_int_to_tree (ptype, wi::to_wide (off)));
10778 : }
10779 :
10780 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10781 2233717 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10782 2507 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
10783 2236194 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10784 : {
10785 1 : tree type_domain;
10786 1 : tree min_val = size_zero_node;
10787 1 : tree osub = sub;
10788 1 : sub = gimple_fold_indirect_ref (sub);
10789 1 : if (! sub)
10790 1 : sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
10791 1 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
10792 1 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10793 1 : min_val = TYPE_MIN_VALUE (type_domain);
10794 1 : if (TREE_CODE (min_val) == INTEGER_CST)
10795 1 : return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
10796 : }
10797 :
10798 : return NULL_TREE;
10799 : }
10800 :
10801 : /* Return true if CODE is an operation that when operating on signed
10802 : integer types involves undefined behavior on overflow and the
10803 : operation can be expressed with unsigned arithmetic. */
10804 :
10805 : bool
10806 506202 : arith_code_with_undefined_signed_overflow (tree_code code)
10807 : {
10808 506202 : switch (code)
10809 : {
10810 : case ABS_EXPR:
10811 : case PLUS_EXPR:
10812 : case MINUS_EXPR:
10813 : case MULT_EXPR:
10814 : case NEGATE_EXPR:
10815 : case POINTER_PLUS_EXPR:
10816 : return true;
10817 188801 : default:
10818 188801 : return false;
10819 : }
10820 : }
10821 :
10822 : /* Return true if STMT has an operation that operates on a signed
10823 : integer types involves undefined behavior on overflow and the
10824 : operation can be expressed with unsigned arithmetic.
10825 : Also returns true if STMT is a VCE that needs to be rewritten
10826 : if moved to be executed unconditionally. */
10827 :
10828 : bool
10829 1209781 : gimple_needing_rewrite_undefined (gimple *stmt)
10830 : {
10831 1209781 : if (!is_gimple_assign (stmt))
10832 : return false;
10833 1052135 : tree lhs = gimple_assign_lhs (stmt);
10834 1052135 : if (!lhs)
10835 : return false;
10836 1052135 : tree lhs_type = TREE_TYPE (lhs);
10837 1052135 : if (!INTEGRAL_TYPE_P (lhs_type)
10838 122131 : && !POINTER_TYPE_P (lhs_type))
10839 : return false;
10840 1015062 : tree rhs = gimple_assign_rhs1 (stmt);
10841 : /* Boolean loads need special handling as they are treated as a full MODE load
10842 : and don't mask off the bits for the precision. */
10843 1015062 : if (gimple_assign_load_p (stmt)
10844 : /* Booleans are the integral type which has this non-masking issue. */
10845 96173 : && TREE_CODE (lhs_type) == BOOLEAN_TYPE
10846 : /* Only non mode precision booleans are need the masking. */
10847 405 : && !type_has_mode_precision_p (lhs_type)
10848 : /* BFR should be the correct thing and just grab the precision. */
10849 405 : && TREE_CODE (rhs) != BIT_FIELD_REF
10850 : /* Bit-fields loads don't need a rewrite as the masking
10851 : happens for them. */
10852 1015467 : && (TREE_CODE (rhs) != COMPONENT_REF
10853 139 : || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1))))
10854 : return true;
10855 : /* VCE from integral types to a integral types but with
10856 : a smaller precision need to be changed into casts
10857 : to be well defined. */
10858 1014657 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
10859 197 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
10860 170 : && is_gimple_val (TREE_OPERAND (rhs, 0))
10861 1014827 : && TYPE_PRECISION (lhs_type)
10862 170 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
10863 : return true;
10864 1014491 : if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
10865 : return false;
10866 386110 : if (!arith_code_with_undefined_signed_overflow
10867 386110 : (gimple_assign_rhs_code (stmt)))
10868 : return false;
10869 : return true;
10870 : }
10871 :
10872 : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
10873 : operation that can be transformed to unsigned arithmetic by converting
10874 : its operand, carrying out the operation in the corresponding unsigned
10875 : type and converting the result back to the original type.
10876 :
10877 : If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
10878 : return NULL.
10879 : Otherwise returns a sequence of statements that replace STMT and also
10880 : contain a modified form of STMT itself. */
10881 :
10882 : static gimple_seq
10883 66378 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
10884 : bool in_place)
10885 : {
10886 66378 : gcc_assert (gimple_needing_rewrite_undefined (stmt));
10887 66378 : if (dump_file && (dump_flags & TDF_DETAILS))
10888 : {
10889 21 : fprintf (dump_file, "rewriting stmt for being unconditional defined");
10890 21 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
10891 : }
10892 66378 : gimple_seq stmts = NULL;
10893 66378 : tree lhs = gimple_assign_lhs (stmt);
10894 :
10895 : /* Boolean loads need to be rewritten to be a load from the same mode
10896 : and then a cast to the other type so the other bits are masked off
10897 : correctly since the load was done conditionally. It is similar to the VCE
10898 : case below. */
10899 66378 : if (gimple_assign_load_p (stmt)
10900 66378 : && TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE)
10901 : {
10902 113 : tree rhs = gimple_assign_rhs1 (stmt);
10903 :
10904 : /* Double check that gimple_needing_rewrite_undefined was called. */
10905 : /* Bit-fields loads will do the masking so don't need the rewriting. */
10906 113 : gcc_assert (TREE_CODE (rhs) != COMPONENT_REF
10907 : || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1)));
10908 : /* BFR is like a bit field load and will do the correct thing. */
10909 113 : gcc_assert (TREE_CODE (lhs) != BIT_FIELD_REF);
10910 : /* Complex boolean types are not valid so REAL/IMAG part will
10911 : never show up. */
10912 113 : gcc_assert (TREE_CODE (rhs) != REALPART_EXPR
10913 : && TREE_CODE (lhs) != IMAGPART_EXPR);
10914 :
10915 113 : auto bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (rhs)));
10916 113 : tree new_type = build_nonstandard_integer_type (bits, true);
10917 113 : location_t loc = gimple_location (stmt);
10918 113 : tree mem_ref = fold_build1_loc (loc, VIEW_CONVERT_EXPR, new_type, rhs);
10919 : /* Replace the original load with a new load and a new lhs. */
10920 113 : tree new_lhs = make_ssa_name (new_type);
10921 113 : gimple_assign_set_rhs1 (stmt, mem_ref);
10922 113 : gimple_assign_set_lhs (stmt, new_lhs);
10923 :
10924 113 : if (in_place)
10925 49 : update_stmt (stmt);
10926 : else
10927 : {
10928 64 : gimple_set_modified (stmt, true);
10929 64 : gimple_seq_add_stmt (&stmts, stmt);
10930 : }
10931 :
10932 : /* Build the conversion statement. */
10933 113 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
10934 113 : if (in_place)
10935 : {
10936 49 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
10937 49 : update_stmt (stmt);
10938 : }
10939 : else
10940 64 : gimple_seq_add_stmt (&stmts, cvt);
10941 113 : return stmts;
10942 : }
10943 :
10944 : /* VCE from integral types to another integral types but with
10945 : smaller precisions need to be changed into casts
10946 : to be well defined. */
10947 66265 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
10948 : {
10949 60 : tree rhs = gimple_assign_rhs1 (stmt);
10950 60 : tree new_rhs = TREE_OPERAND (rhs, 0);
10951 60 : gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
10952 : < TYPE_PRECISION (TREE_TYPE (new_rhs)));
10953 60 : gcc_assert (is_gimple_val (new_rhs));
10954 60 : gimple_assign_set_rhs_code (stmt, NOP_EXPR);
10955 60 : gimple_assign_set_rhs1 (stmt, new_rhs);
10956 60 : if (in_place)
10957 51 : update_stmt (stmt);
10958 : else
10959 : {
10960 9 : gimple_set_modified (stmt, true);
10961 9 : gimple_seq_add_stmt (&stmts, stmt);
10962 : }
10963 60 : return stmts;
10964 : }
10965 66205 : tree type = unsigned_type_for (TREE_TYPE (lhs));
10966 66205 : if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
10967 24 : gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
10968 : else
10969 198071 : for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
10970 : {
10971 131890 : tree op = gimple_op (stmt, i);
10972 131890 : op = gimple_convert (&stmts, type, op);
10973 131890 : gimple_set_op (stmt, i, op);
10974 : }
10975 66205 : gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
10976 66205 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
10977 11014 : gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
10978 66205 : gimple_set_modified (stmt, true);
10979 66205 : if (in_place)
10980 : {
10981 46149 : if (stmts)
10982 45789 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
10983 46149 : stmts = NULL;
10984 : }
10985 : else
10986 20056 : gimple_seq_add_stmt (&stmts, stmt);
10987 66205 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
10988 66205 : if (in_place)
10989 : {
10990 46149 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
10991 46149 : update_stmt (stmt);
10992 : }
10993 : else
10994 20056 : gimple_seq_add_stmt (&stmts, cvt);
10995 :
10996 66205 : return stmts;
10997 : }
10998 :
10999 : void
11000 46249 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
11001 : {
11002 46249 : rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
11003 46249 : }
11004 :
11005 : gimple_seq
11006 20129 : rewrite_to_defined_unconditional (gimple *stmt)
11007 : {
11008 20129 : return rewrite_to_defined_unconditional (nullptr, stmt, false);
11009 : }
11010 :
11011 : /* The valueization hook we use for the gimple_build API simplification.
11012 : This makes us match fold_buildN behavior by only combining with
11013 : statements in the sequence(s) we are currently building. */
11014 :
11015 : static tree
11016 20153029 : gimple_build_valueize (tree op)
11017 : {
11018 20153029 : if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
11019 4335745 : return op;
11020 : return NULL_TREE;
11021 : }
11022 :
11023 : /* Helper for gimple_build to perform the final insertion of stmts on SEQ. */
11024 :
11025 : static inline void
11026 1317093 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
11027 : bool before, gsi_iterator_update update,
11028 : gimple_seq seq)
11029 : {
11030 1317093 : if (before)
11031 : {
11032 95792 : if (gsi->bb)
11033 95792 : gsi_insert_seq_before (gsi, seq, update);
11034 : else
11035 0 : gsi_insert_seq_before_without_update (gsi, seq, update);
11036 : }
11037 : else
11038 : {
11039 1221301 : if (gsi->bb)
11040 163 : gsi_insert_seq_after (gsi, seq, update);
11041 : else
11042 1221138 : gsi_insert_seq_after_without_update (gsi, seq, update);
11043 : }
11044 1317093 : }
11045 :
11046 : /* Build the expression CODE OP0 of type TYPE with location LOC,
11047 : simplifying it first if possible. Returns the built
11048 : expression value and inserts statements possibly defining it
11049 : before GSI if BEFORE is true or after GSI if false and advance
11050 : the iterator accordingly.
11051 : If gsi refers to a basic block simplifying is allowed to look
11052 : at all SSA defs while when it does not it is restricted to
11053 : SSA defs that are not associated with a basic block yet,
11054 : indicating they belong to the currently building sequence. */
11055 :
11056 : tree
11057 338645 : gimple_build (gimple_stmt_iterator *gsi,
11058 : bool before, gsi_iterator_update update,
11059 : location_t loc, enum tree_code code, tree type, tree op0)
11060 : {
11061 338645 : gimple_seq seq = NULL;
11062 338645 : tree res
11063 338645 : = gimple_simplify (code, type, op0, &seq,
11064 338645 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
11065 338645 : if (!res)
11066 : {
11067 298368 : res = make_ssa_name (type);
11068 298368 : gimple *stmt;
11069 298368 : if (code == REALPART_EXPR
11070 : || code == IMAGPART_EXPR
11071 298368 : || code == VIEW_CONVERT_EXPR)
11072 15181 : stmt = gimple_build_assign (res, code, build1 (code, type, op0));
11073 : else
11074 283187 : stmt = gimple_build_assign (res, code, op0);
11075 298368 : gimple_set_location (stmt, loc);
11076 298368 : gimple_seq_add_stmt_without_update (&seq, stmt);
11077 : }
11078 338645 : gimple_build_insert_seq (gsi, before, update, seq);
11079 338645 : return res;
11080 : }
11081 :
11082 : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
11083 : simplifying it first if possible. Returns the built
11084 : expression value inserting any new statements at GSI honoring BEFORE
11085 : and UPDATE. */
11086 :
11087 : tree
11088 768927 : gimple_build (gimple_stmt_iterator *gsi,
11089 : bool before, gsi_iterator_update update,
11090 : location_t loc, enum tree_code code, tree type,
11091 : tree op0, tree op1)
11092 : {
11093 768927 : gimple_seq seq = NULL;
11094 768927 : tree res
11095 768927 : = gimple_simplify (code, type, op0, op1, &seq,
11096 768927 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
11097 768927 : if (!res)
11098 : {
11099 678143 : res = make_ssa_name (type);
11100 678143 : gimple *stmt = gimple_build_assign (res, code, op0, op1);
11101 678143 : gimple_set_location (stmt, loc);
11102 678143 : gimple_seq_add_stmt_without_update (&seq, stmt);
11103 : }
11104 768927 : gimple_build_insert_seq (gsi, before, update, seq);
11105 768927 : return res;
11106 : }
11107 :
11108 : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
11109 : simplifying it first if possible. Returns the built
11110 : expression value inserting any new statements at GSI honoring BEFORE
11111 : and UPDATE. */
11112 :
11113 : tree
11114 44818 : gimple_build (gimple_stmt_iterator *gsi,
11115 : bool before, gsi_iterator_update update,
11116 : location_t loc, enum tree_code code, tree type,
11117 : tree op0, tree op1, tree op2)
11118 : {
11119 :
11120 44818 : gimple_seq seq = NULL;
11121 44818 : tree res
11122 44818 : = gimple_simplify (code, type, op0, op1, op2, &seq,
11123 44818 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
11124 44818 : if (!res)
11125 : {
11126 31690 : res = make_ssa_name (type);
11127 31690 : gimple *stmt;
11128 31690 : if (code == BIT_FIELD_REF)
11129 24305 : stmt = gimple_build_assign (res, code,
11130 : build3 (code, type, op0, op1, op2));
11131 : else
11132 7385 : stmt = gimple_build_assign (res, code, op0, op1, op2);
11133 31690 : gimple_set_location (stmt, loc);
11134 31690 : gimple_seq_add_stmt_without_update (&seq, stmt);
11135 : }
11136 44818 : gimple_build_insert_seq (gsi, before, update, seq);
11137 44818 : return res;
11138 : }
11139 :
11140 : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
11141 : void) with a location LOC. Returns the built expression value (or NULL_TREE
11142 : if TYPE is void) inserting any new statements at GSI honoring BEFORE
11143 : and UPDATE. */
11144 :
11145 : tree
11146 0 : gimple_build (gimple_stmt_iterator *gsi,
11147 : bool before, gsi_iterator_update update,
11148 : location_t loc, combined_fn fn, tree type)
11149 : {
11150 0 : tree res = NULL_TREE;
11151 0 : gimple_seq seq = NULL;
11152 0 : gcall *stmt;
11153 0 : if (internal_fn_p (fn))
11154 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
11155 : else
11156 : {
11157 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11158 0 : stmt = gimple_build_call (decl, 0);
11159 : }
11160 0 : if (!VOID_TYPE_P (type))
11161 : {
11162 0 : res = make_ssa_name (type);
11163 0 : gimple_call_set_lhs (stmt, res);
11164 : }
11165 0 : gimple_set_location (stmt, loc);
11166 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11167 0 : gimple_build_insert_seq (gsi, before, update, seq);
11168 0 : return res;
11169 : }
11170 :
11171 : /* Build the call FN (ARG0) with a result of type TYPE
11172 : (or no result if TYPE is void) with location LOC,
11173 : simplifying it first if possible. Returns the built
11174 : expression value (or NULL_TREE if TYPE is void) inserting any new
11175 : statements at GSI honoring BEFORE and UPDATE. */
11176 :
11177 : tree
11178 24604 : gimple_build (gimple_stmt_iterator *gsi,
11179 : bool before, gsi_iterator_update update,
11180 : location_t loc, combined_fn fn,
11181 : tree type, tree arg0)
11182 : {
11183 24604 : gimple_seq seq = NULL;
11184 24604 : tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
11185 24604 : if (!res)
11186 : {
11187 24604 : gcall *stmt;
11188 24604 : if (internal_fn_p (fn))
11189 24224 : stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
11190 : else
11191 : {
11192 380 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11193 380 : stmt = gimple_build_call (decl, 1, arg0);
11194 : }
11195 24604 : if (!VOID_TYPE_P (type))
11196 : {
11197 24224 : res = make_ssa_name (type);
11198 24224 : gimple_call_set_lhs (stmt, res);
11199 : }
11200 24604 : gimple_set_location (stmt, loc);
11201 24604 : gimple_seq_add_stmt_without_update (&seq, stmt);
11202 : }
11203 24604 : gimple_build_insert_seq (gsi, before, update, seq);
11204 24604 : return res;
11205 : }
11206 :
11207 : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
11208 : (or no result if TYPE is void) with location LOC,
11209 : simplifying it first if possible. Returns the built
11210 : expression value (or NULL_TREE if TYPE is void) inserting any new
11211 : statements at GSI honoring BEFORE and UPDATE. */
11212 :
11213 : tree
11214 0 : gimple_build (gimple_stmt_iterator *gsi,
11215 : bool before, gsi_iterator_update update,
11216 : location_t loc, combined_fn fn,
11217 : tree type, tree arg0, tree arg1)
11218 : {
11219 0 : gimple_seq seq = NULL;
11220 0 : tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
11221 : gimple_build_valueize);
11222 0 : if (!res)
11223 : {
11224 0 : gcall *stmt;
11225 0 : if (internal_fn_p (fn))
11226 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
11227 : else
11228 : {
11229 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11230 0 : stmt = gimple_build_call (decl, 2, arg0, arg1);
11231 : }
11232 0 : if (!VOID_TYPE_P (type))
11233 : {
11234 0 : res = make_ssa_name (type);
11235 0 : gimple_call_set_lhs (stmt, res);
11236 : }
11237 0 : gimple_set_location (stmt, loc);
11238 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11239 : }
11240 0 : gimple_build_insert_seq (gsi, before, update, seq);
11241 0 : return res;
11242 : }
11243 :
11244 : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
11245 : (or no result if TYPE is void) with location LOC,
11246 : simplifying it first if possible. Returns the built
11247 : expression value (or NULL_TREE if TYPE is void) inserting any new
11248 : statements at GSI honoring BEFORE and UPDATE. */
11249 :
11250 : tree
11251 0 : gimple_build (gimple_stmt_iterator *gsi,
11252 : bool before, gsi_iterator_update update,
11253 : location_t loc, combined_fn fn,
11254 : tree type, tree arg0, tree arg1, tree arg2)
11255 : {
11256 0 : gimple_seq seq = NULL;
11257 0 : tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
11258 : &seq, gimple_build_valueize);
11259 0 : if (!res)
11260 : {
11261 0 : gcall *stmt;
11262 0 : if (internal_fn_p (fn))
11263 0 : stmt = gimple_build_call_internal (as_internal_fn (fn),
11264 : 3, arg0, arg1, arg2);
11265 : else
11266 : {
11267 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11268 0 : stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
11269 : }
11270 0 : if (!VOID_TYPE_P (type))
11271 : {
11272 0 : res = make_ssa_name (type);
11273 0 : gimple_call_set_lhs (stmt, res);
11274 : }
11275 0 : gimple_set_location (stmt, loc);
11276 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11277 : }
11278 0 : gimple_build_insert_seq (gsi, before, update, seq);
11279 0 : return res;
11280 : }
11281 :
11282 : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
11283 : void) with location LOC, simplifying it first if possible. Returns the
11284 : built expression value (or NULL_TREE if TYPE is void) inserting any new
11285 : statements at GSI honoring BEFORE and UPDATE. */
11286 :
11287 : tree
11288 21 : gimple_build (gimple_stmt_iterator *gsi,
11289 : bool before, gsi_iterator_update update,
11290 : location_t loc, code_helper code, tree type, tree op0)
11291 : {
11292 21 : if (code.is_tree_code ())
11293 0 : return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
11294 21 : return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
11295 : }
11296 :
11297 : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
11298 : void) with location LOC, simplifying it first if possible. Returns the
11299 : built expression value (or NULL_TREE if TYPE is void) inserting any new
11300 : statements at GSI honoring BEFORE and UPDATE. */
11301 :
11302 : tree
11303 24158 : gimple_build (gimple_stmt_iterator *gsi,
11304 : bool before, gsi_iterator_update update,
11305 : location_t loc, code_helper code, tree type, tree op0, tree op1)
11306 : {
11307 24158 : if (code.is_tree_code ())
11308 24158 : return gimple_build (gsi, before, update,
11309 24158 : loc, tree_code (code), type, op0, op1);
11310 0 : return gimple_build (gsi, before, update,
11311 0 : loc, combined_fn (code), type, op0, op1);
11312 : }
11313 :
11314 : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
11315 : is void) with location LOC, simplifying it first if possible. Returns the
11316 : built expression value (or NULL_TREE if TYPE is void) inserting any new
11317 : statements at GSI honoring BEFORE and UPDATE. */
11318 :
11319 : tree
11320 0 : gimple_build (gimple_stmt_iterator *gsi,
11321 : bool before, gsi_iterator_update update,
11322 : location_t loc, code_helper code,
11323 : tree type, tree op0, tree op1, tree op2)
11324 : {
11325 0 : if (code.is_tree_code ())
11326 0 : return gimple_build (gsi, before, update,
11327 0 : loc, tree_code (code), type, op0, op1, op2);
11328 0 : return gimple_build (gsi, before, update,
11329 0 : loc, combined_fn (code), type, op0, op1, op2);
11330 : }
11331 :
11332 : /* Build the conversion (TYPE) OP with a result of type TYPE
11333 : with location LOC if such conversion is necessary in GIMPLE,
11334 : simplifying it first.
11335 : Returns the built expression inserting any new statements
11336 : at GSI honoring BEFORE and UPDATE. */
11337 :
11338 : tree
11339 1999799 : gimple_convert (gimple_stmt_iterator *gsi,
11340 : bool before, gsi_iterator_update update,
11341 : location_t loc, tree type, tree op)
11342 : {
11343 1999799 : if (useless_type_conversion_p (type, TREE_TYPE (op)))
11344 : return op;
11345 185667 : return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
11346 : }
11347 :
11348 : /* Build the conversion (ptrofftype) OP with a result of a type
11349 : compatible with ptrofftype with location LOC if such conversion
11350 : is necessary in GIMPLE, simplifying it first.
11351 : Returns the built expression value inserting any new statements
11352 : at GSI honoring BEFORE and UPDATE. */
11353 :
11354 : tree
11355 208 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
11356 : bool before, gsi_iterator_update update,
11357 : location_t loc, tree op)
11358 : {
11359 208 : if (ptrofftype_p (TREE_TYPE (op)))
11360 : return op;
11361 0 : return gimple_convert (gsi, before, update, loc, sizetype, op);
11362 : }
11363 :
11364 : /* Build a vector of type TYPE in which each element has the value OP.
11365 : Return a gimple value for the result, inserting any new statements
11366 : at GSI honoring BEFORE and UPDATE. */
11367 :
11368 : tree
11369 324195 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
11370 : bool before, gsi_iterator_update update,
11371 : location_t loc, tree type, tree op)
11372 : {
11373 324195 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
11374 : && !CONSTANT_CLASS_P (op))
11375 : return gimple_build (gsi, before, update,
11376 : loc, VEC_DUPLICATE_EXPR, type, op);
11377 :
11378 324195 : tree res, vec = build_vector_from_val (type, op);
11379 324195 : if (is_gimple_val (vec))
11380 : return vec;
11381 27573 : if (gimple_in_ssa_p (cfun))
11382 27573 : res = make_ssa_name (type);
11383 : else
11384 0 : res = create_tmp_reg (type);
11385 27573 : gimple_seq seq = NULL;
11386 27573 : gimple *stmt = gimple_build_assign (res, vec);
11387 27573 : gimple_set_location (stmt, loc);
11388 27573 : gimple_seq_add_stmt_without_update (&seq, stmt);
11389 27573 : gimple_build_insert_seq (gsi, before, update, seq);
11390 27573 : return res;
11391 : }
11392 :
11393 : /* Build a vector from BUILDER, handling the case in which some elements
11394 : are non-constant. Return a gimple value for the result, inserting
11395 : any new instructions to GSI honoring BEFORE and UPDATE.
11396 :
11397 : BUILDER must not have a stepped encoding on entry. This is because
11398 : the function is not geared up to handle the arithmetic that would
11399 : be needed in the variable case, and any code building a vector that
11400 : is known to be constant should use BUILDER->build () directly. */
11401 :
11402 : tree
11403 367757 : gimple_build_vector (gimple_stmt_iterator *gsi,
11404 : bool before, gsi_iterator_update update,
11405 : location_t loc, tree_vector_builder *builder)
11406 : {
11407 367757 : gcc_assert (builder->nelts_per_pattern () <= 2);
11408 367757 : unsigned int encoded_nelts = builder->encoded_nelts ();
11409 1302443 : for (unsigned int i = 0; i < encoded_nelts; ++i)
11410 1047212 : if (!CONSTANT_CLASS_P ((*builder)[i]))
11411 : {
11412 112526 : gimple_seq seq = NULL;
11413 112526 : tree type = builder->type ();
11414 112526 : unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11415 112526 : vec<constructor_elt, va_gc> *v;
11416 112526 : vec_alloc (v, nelts);
11417 358954 : for (i = 0; i < nelts; ++i)
11418 246428 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
11419 :
11420 112526 : tree res;
11421 112526 : if (gimple_in_ssa_p (cfun))
11422 112526 : res = make_ssa_name (type);
11423 : else
11424 0 : res = create_tmp_reg (type);
11425 112526 : gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
11426 112526 : gimple_set_location (stmt, loc);
11427 112526 : gimple_seq_add_stmt_without_update (&seq, stmt);
11428 112526 : gimple_build_insert_seq (gsi, before, update, seq);
11429 112526 : return res;
11430 : }
11431 255231 : return builder->build ();
11432 : }
11433 :
11434 : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
11435 : and generate a value guaranteed to be rounded upwards to ALIGN.
11436 :
11437 : Return the tree node representing this size, it is of TREE_TYPE TYPE. */
11438 :
11439 : tree
11440 0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
11441 : bool before, gsi_iterator_update update,
11442 : location_t loc, tree type,
11443 : tree old_size, unsigned HOST_WIDE_INT align)
11444 : {
11445 0 : unsigned HOST_WIDE_INT tg_mask = align - 1;
11446 : /* tree new_size = (old_size + tg_mask) & ~tg_mask; */
11447 0 : gcc_assert (INTEGRAL_TYPE_P (type));
11448 0 : tree tree_mask = build_int_cst (type, tg_mask);
11449 0 : tree oversize = gimple_build (gsi, before, update,
11450 : loc, PLUS_EXPR, type, old_size, tree_mask);
11451 :
11452 0 : tree mask = build_int_cst (type, -align);
11453 0 : return gimple_build (gsi, before, update,
11454 0 : loc, BIT_AND_EXPR, type, oversize, mask);
11455 : }
11456 :
11457 : /* Return true if the result of assignment STMT is known to be non-negative.
11458 : DEPTH is the current nesting depth of the query. */
11459 :
11460 : static bool
11461 6320863 : gimple_assign_nonnegative_p (gimple *stmt, int depth)
11462 : {
11463 6320863 : enum tree_code code = gimple_assign_rhs_code (stmt);
11464 6320863 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
11465 6320863 : switch (get_gimple_rhs_class (code))
11466 : {
11467 503041 : case GIMPLE_UNARY_RHS:
11468 503041 : return tree_unary_nonnegative_p (gimple_assign_rhs_code (stmt),
11469 : type,
11470 : gimple_assign_rhs1 (stmt),
11471 503041 : depth);
11472 3708516 : case GIMPLE_BINARY_RHS:
11473 3708516 : return tree_binary_nonnegative_p (gimple_assign_rhs_code (stmt),
11474 : type,
11475 : gimple_assign_rhs1 (stmt),
11476 : gimple_assign_rhs2 (stmt),
11477 3708516 : depth);
11478 : case GIMPLE_TERNARY_RHS:
11479 : return false;
11480 2094938 : case GIMPLE_SINGLE_RHS:
11481 2094938 : return tree_single_nonnegative_p (gimple_assign_rhs1 (stmt),
11482 2094938 : depth);
11483 : case GIMPLE_INVALID_RHS:
11484 : break;
11485 : }
11486 0 : gcc_unreachable ();
11487 : }
11488 :
11489 : /* Return true if return value of call STMT is known to be non-negative.
11490 : DEPTH is the current nesting depth of the query. */
11491 :
11492 : static bool
11493 13022554 : gimple_call_nonnegative_p (gimple *stmt, int depth)
11494 : {
11495 13022554 : tree arg0
11496 13022554 : = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
11497 13022554 : tree arg1
11498 13022554 : = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
11499 13022554 : tree lhs = gimple_call_lhs (stmt);
11500 13022554 : return (lhs
11501 13022554 : && tree_call_nonnegative_p (TREE_TYPE (lhs),
11502 : gimple_call_combined_fn (stmt),
11503 13022554 : arg0, arg1, depth));
11504 : }
11505 :
11506 : /* Return true if return value of call STMT is known to be non-negative.
11507 : DEPTH is the current nesting depth of the query. */
11508 :
11509 : static bool
11510 1208393 : gimple_phi_nonnegative_p (gimple *stmt, int depth)
11511 : {
11512 1636808 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11513 : {
11514 1624353 : tree arg = gimple_phi_arg_def (stmt, i);
11515 1624353 : if (!tree_single_nonnegative_p (arg, depth + 1))
11516 : return false;
11517 : }
11518 : return true;
11519 : }
11520 :
11521 : /* Return true if STMT is known to compute a non-negative value.
11522 : DEPTH is the current nesting depth of the query. */
11523 :
11524 : bool
11525 21185555 : gimple_stmt_nonnegative_p (gimple *stmt, int depth)
11526 : {
11527 21185555 : tree type = gimple_range_type (stmt);
11528 21185555 : if (type && frange::supports_p (type))
11529 : {
11530 1458980 : frange r;
11531 1458980 : bool sign;
11532 1458980 : if (get_global_range_query ()->range_of_stmt (r, stmt)
11533 1458980 : && r.signbit_p (sign))
11534 27203 : return !sign;
11535 1458980 : }
11536 21158352 : switch (gimple_code (stmt))
11537 : {
11538 6320863 : case GIMPLE_ASSIGN:
11539 6320863 : return gimple_assign_nonnegative_p (stmt, depth);
11540 13022554 : case GIMPLE_CALL:
11541 13022554 : return gimple_call_nonnegative_p (stmt, depth);
11542 1208393 : case GIMPLE_PHI:
11543 1208393 : return gimple_phi_nonnegative_p (stmt, depth);
11544 : default:
11545 : return false;
11546 : }
11547 : }
11548 :
11549 : /* Return true if the floating-point value computed by assignment STMT
11550 : is known to have an integer value. We also allow +Inf, -Inf and NaN
11551 : to be considered integer values. Return false for signaling NaN.
11552 :
11553 : DEPTH is the current nesting depth of the query. */
11554 :
11555 : static bool
11556 58561 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
11557 : {
11558 58561 : enum tree_code code = gimple_assign_rhs_code (stmt);
11559 58561 : switch (get_gimple_rhs_class (code))
11560 : {
11561 15024 : case GIMPLE_UNARY_RHS:
11562 15024 : return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
11563 15024 : gimple_assign_rhs1 (stmt), depth);
11564 13267 : case GIMPLE_BINARY_RHS:
11565 13267 : return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
11566 : gimple_assign_rhs1 (stmt),
11567 13267 : gimple_assign_rhs2 (stmt), depth);
11568 : case GIMPLE_TERNARY_RHS:
11569 : return false;
11570 29115 : case GIMPLE_SINGLE_RHS:
11571 29115 : return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
11572 : case GIMPLE_INVALID_RHS:
11573 : break;
11574 : }
11575 0 : gcc_unreachable ();
11576 : }
11577 :
11578 : /* Return true if the floating-point value computed by call STMT is known
11579 : to have an integer value. We also allow +Inf, -Inf and NaN to be
11580 : considered integer values. Return false for signaling NaN.
11581 :
11582 : DEPTH is the current nesting depth of the query. */
11583 :
11584 : static bool
11585 1089 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
11586 : {
11587 1089 : tree arg0 = (gimple_call_num_args (stmt) > 0
11588 1089 : ? gimple_call_arg (stmt, 0)
11589 : : NULL_TREE);
11590 1089 : tree arg1 = (gimple_call_num_args (stmt) > 1
11591 1089 : ? gimple_call_arg (stmt, 1)
11592 : : NULL_TREE);
11593 1089 : return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
11594 1089 : arg0, arg1, depth);
11595 : }
11596 :
11597 : /* Return true if the floating-point result of phi STMT is known to have
11598 : an integer value. We also allow +Inf, -Inf and NaN to be considered
11599 : integer values. Return false for signaling NaN.
11600 :
11601 : DEPTH is the current nesting depth of the query. */
11602 :
11603 : static bool
11604 1489 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
11605 : {
11606 1652 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11607 : {
11608 1643 : tree arg = gimple_phi_arg_def (stmt, i);
11609 1643 : if (!integer_valued_real_single_p (arg, depth + 1))
11610 : return false;
11611 : }
11612 : return true;
11613 : }
11614 :
11615 : /* Return true if the floating-point value computed by STMT is known
11616 : to have an integer value. We also allow +Inf, -Inf and NaN to be
11617 : considered integer values. Return false for signaling NaN.
11618 :
11619 : DEPTH is the current nesting depth of the query. */
11620 :
11621 : bool
11622 88516 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
11623 : {
11624 88516 : switch (gimple_code (stmt))
11625 : {
11626 58561 : case GIMPLE_ASSIGN:
11627 58561 : return gimple_assign_integer_valued_real_p (stmt, depth);
11628 1089 : case GIMPLE_CALL:
11629 1089 : return gimple_call_integer_valued_real_p (stmt, depth);
11630 1489 : case GIMPLE_PHI:
11631 1489 : return gimple_phi_integer_valued_real_p (stmt, depth);
11632 : default:
11633 : return false;
11634 : }
11635 : }
|