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 4443557 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
116 : {
117 4443557 : varpool_node *vnode;
118 4443557 : struct cgraph_node *node;
119 4443557 : symtab_node *snode;
120 :
121 4443557 : if (DECL_ABSTRACT_P (decl))
122 : return false;
123 :
124 : /* We are concerned only about static/external vars and functions. */
125 1503256 : if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
126 5542292 : || !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 4039036 : if (!TREE_PUBLIC (decl))
132 : {
133 1098526 : 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 1098481 : if (symtab->function_flags_ready)
138 : return true;
139 1062830 : snode = symtab_node::get (decl);
140 1062830 : if (!snode || !snode->definition)
141 : return false;
142 1062775 : node = dyn_cast <cgraph_node *> (snode);
143 1071444 : 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 2940510 : if (!from_decl
150 476882 : || !VAR_P (from_decl)
151 475753 : || (!DECL_EXTERNAL (from_decl)
152 201833 : && (vnode = varpool_node::get (from_decl)) != NULL
153 139055 : && vnode->definition)
154 3277214 : || (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 336701 : if (DECL_VISIBILITY_SPECIFIED (decl)
162 329981 : && DECL_EXTERNAL (decl)
163 265131 : && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
164 518459 : && (!(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 154943 : 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 109321 : if (!symtab->function_flags_ready)
183 : return true;
184 :
185 96403 : snode = symtab_node::get (decl);
186 96403 : if (!snode
187 96403 : || ((!snode->definition || DECL_EXTERNAL (decl))
188 11887 : && (!snode->in_other_partition
189 0 : || (!snode->forced_by_abi && !snode->force_output))))
190 : return false;
191 59404 : node = dyn_cast <cgraph_node *> (snode);
192 59404 : 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 15553389 : canonicalize_constructor_val (tree cval, tree from_decl)
201 : {
202 15553389 : if (CONSTANT_CLASS_P (cval))
203 : return cval;
204 :
205 9426899 : tree orig_cval = cval;
206 9426899 : STRIP_NOPS (cval);
207 9426899 : if (TREE_CODE (cval) == POINTER_PLUS_EXPR
208 9426899 : && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
209 : {
210 71972 : tree ptr = TREE_OPERAND (cval, 0);
211 71972 : if (is_gimple_min_invariant (ptr))
212 215169 : cval = build1_loc (EXPR_LOCATION (cval),
213 71723 : ADDR_EXPR, TREE_TYPE (ptr),
214 143446 : fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
215 : ptr,
216 : fold_convert (ptr_type_node,
217 : TREE_OPERAND (cval, 1))));
218 : }
219 9426899 : if (TREE_CODE (cval) == ADDR_EXPR)
220 : {
221 5170105 : tree base = NULL_TREE;
222 5170105 : 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 5169912 : base = get_base_address (TREE_OPERAND (cval, 0));
230 5170105 : if (!base)
231 0 : return NULL_TREE;
232 :
233 2297028 : if (VAR_OR_FUNCTION_DECL_P (base)
234 6470946 : && !can_refer_decl_in_current_unit_p (base, from_decl))
235 : return NULL_TREE;
236 4987565 : if (TREE_TYPE (base) == error_mark_node)
237 : return NULL_TREE;
238 4987565 : 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 2296143 : 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 1299956 : cgraph_node::get_create (base);
248 : }
249 : /* Fixup types in global initializers. */
250 4987565 : if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
251 39219 : cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
252 :
253 4987565 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
254 214531 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
255 4987565 : return cval;
256 : }
257 : /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0. */
258 4256794 : if (TREE_CODE (cval) == INTEGER_CST)
259 : {
260 66327 : if (TREE_OVERFLOW_P (cval))
261 0 : cval = drop_tree_overflow (cval);
262 66327 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
263 63126 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
264 66327 : 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 20383781 : get_symbol_constant_value (tree sym)
274 : {
275 20383781 : tree val = ctor_for_folding (sym);
276 20383781 : if (val != error_mark_node)
277 : {
278 39741 : if (val)
279 : {
280 37460 : val = canonicalize_constructor_val (unshare_expr (val), sym);
281 37460 : if (val
282 37460 : && is_gimple_min_invariant (val)
283 66054 : && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
284 : return val;
285 : else
286 8972 : 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 2281 : if (!val
292 2281 : && is_gimple_reg_type (TREE_TYPE (sym)))
293 1944 : 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 65682825 : maybe_fold_reference (tree expr)
306 : {
307 65682825 : tree result = NULL_TREE;
308 :
309 : /* Avoid expensive fold_const_aggregate_ref early on aggregate loads
310 : and esp. replacing STRING_CSTs inline. */
311 65682825 : if (!is_gimple_reg_type (TREE_TYPE (expr)))
312 : return NULL_TREE;
313 :
314 62562586 : if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
315 61229194 : || TREE_CODE (expr) == REALPART_EXPR
316 60438016 : || TREE_CODE (expr) == IMAGPART_EXPR)
317 64281752 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
318 5795 : result = fold_unary_loc (EXPR_LOCATION (expr),
319 : TREE_CODE (expr),
320 5795 : TREE_TYPE (expr),
321 5795 : TREE_OPERAND (expr, 0));
322 62556791 : else if (TREE_CODE (expr) == BIT_FIELD_REF
323 62556791 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
324 49 : result = fold_ternary_loc (EXPR_LOCATION (expr),
325 : TREE_CODE (expr),
326 49 : TREE_TYPE (expr),
327 49 : TREE_OPERAND (expr, 0),
328 49 : TREE_OPERAND (expr, 1),
329 49 : TREE_OPERAND (expr, 2));
330 : else
331 62556742 : result = fold_const_aggregate_ref (expr);
332 :
333 62562586 : 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 261227548 : fold_gimple_assign (gimple_stmt_iterator *si)
462 : {
463 261227548 : gimple *stmt = gsi_stmt (*si);
464 261227548 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
465 261227548 : location_t loc = gimple_location (stmt);
466 :
467 261227548 : tree result = NULL_TREE;
468 :
469 261227548 : switch (get_gimple_rhs_class (subcode))
470 : {
471 173279561 : case GIMPLE_SINGLE_RHS:
472 173279561 : {
473 173279561 : tree rhs = gimple_assign_rhs1 (stmt);
474 :
475 173279561 : if (TREE_CLOBBER_P (rhs))
476 : return NULL_TREE;
477 :
478 159158909 : if (REFERENCE_CLASS_P (rhs))
479 63002786 : return maybe_fold_reference (rhs);
480 :
481 96156123 : else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
482 : {
483 174880 : tree val = OBJ_TYPE_REF_EXPR (rhs);
484 174880 : if (is_gimple_min_invariant (val))
485 : return val;
486 174849 : else if (flag_devirtualize && virtual_method_call_p (rhs))
487 : {
488 174809 : bool final;
489 174809 : vec <cgraph_node *>targets
490 174809 : = possible_polymorphic_call_targets (rhs, stmt, &final);
491 175081 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
492 : {
493 42 : 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 42 : 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 9 : val = build_int_cst (TREE_TYPE (val), 0);
513 42 : return val;
514 : }
515 : }
516 : }
517 :
518 95981243 : else if (TREE_CODE (rhs) == ADDR_EXPR)
519 : {
520 15233827 : tree ref = TREE_OPERAND (rhs, 0);
521 15233827 : if (TREE_CODE (ref) == MEM_REF
522 15233827 : && integer_zerop (TREE_OPERAND (ref, 1)))
523 : {
524 2659 : result = TREE_OPERAND (ref, 0);
525 2659 : if (!useless_type_conversion_p (TREE_TYPE (rhs),
526 2659 : TREE_TYPE (result)))
527 0 : result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
528 2659 : return result;
529 : }
530 : }
531 :
532 80747416 : else if (TREE_CODE (rhs) == CONSTRUCTOR
533 80747416 : && 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 458949 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
540 454534 : if (! CONSTANT_CLASS_P (val))
541 : return NULL_TREE;
542 :
543 4415 : return build_vector_from_ctor (TREE_TYPE (rhs),
544 8830 : CONSTRUCTOR_ELTS (rhs));
545 : }
546 :
547 80380766 : else if (DECL_P (rhs)
548 80380766 : && is_gimple_reg_type (TREE_TYPE (rhs)))
549 12250180 : 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 561894 : case GIMPLE_TERNARY_RHS:
560 1123788 : result = fold_ternary_loc (loc, subcode,
561 561894 : TREE_TYPE (gimple_assign_lhs (stmt)),
562 : gimple_assign_rhs1 (stmt),
563 : gimple_assign_rhs2 (stmt),
564 : gimple_assign_rhs3 (stmt));
565 :
566 561894 : 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 133772 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
595 : {
596 133772 : gimple *stmt = gsi_stmt (*si_p);
597 :
598 133772 : if (gimple_has_location (stmt))
599 111664 : 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 133772 : gimple *laststore = NULL;
604 267544 : for (gimple_stmt_iterator i = gsi_last (stmts);
605 543484 : !gsi_end_p (i); gsi_prev (&i))
606 : {
607 204856 : gimple *new_stmt = gsi_stmt (i);
608 204856 : if ((gimple_assign_single_p (new_stmt)
609 129652 : && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
610 334238 : || (is_gimple_call (new_stmt)
611 14837 : && (gimple_call_flags (new_stmt)
612 14837 : & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
613 : {
614 2313 : tree vdef;
615 2313 : if (!laststore)
616 2307 : vdef = gimple_vdef (stmt);
617 : else
618 6 : vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
619 2313 : gimple_set_vdef (new_stmt, vdef);
620 2313 : 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 133772 : tree reaching_vuse = gimple_vuse (stmt);
629 133772 : for (gimple_stmt_iterator i = gsi_start (stmts);
630 338628 : !gsi_end_p (i); gsi_next (&i))
631 : {
632 204856 : 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 204856 : if (gimple_has_mem_ops (new_stmt))
636 204854 : gimple_set_vuse (new_stmt, reaching_vuse);
637 204856 : gimple_set_modified (new_stmt, true);
638 613177 : if (gimple_vdef (new_stmt))
639 204856 : 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 133772 : if (reaching_vuse
645 223115 : && reaching_vuse == gimple_vuse (stmt))
646 : {
647 87966 : tree vdef = gimple_vdef (stmt);
648 87966 : 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 133772 : gsi_replace_with_seq (si_p, stmts, false);
658 133772 : }
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 2459 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
666 : gimple *stmt)
667 : {
668 2459 : tree lhs = gimple_call_lhs (stmt);
669 2459 : gimple_call_set_lhs (new_stmt, lhs);
670 2459 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
671 794 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
672 2459 : gimple_move_vops (new_stmt, stmt);
673 2459 : gimple_set_location (new_stmt, gimple_location (stmt));
674 2459 : if (gimple_block (new_stmt) == NULL_TREE)
675 1 : gimple_set_block (new_stmt, gimple_block (stmt));
676 2459 : gsi_replace (si_p, new_stmt, false);
677 2459 : }
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 2456 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
685 : {
686 2456 : va_list ap;
687 2456 : gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
688 :
689 2456 : gcc_assert (is_gimple_call (stmt));
690 2456 : va_start (ap, nargs);
691 2456 : new_stmt = gimple_build_call_valist (fn, nargs, ap);
692 2456 : finish_update_gimple_call (si_p, new_stmt, stmt);
693 2456 : va_end (ap);
694 2456 : 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 60294 : valid_gimple_call_p (tree expr)
703 : {
704 60294 : unsigned i, nargs;
705 :
706 60294 : 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 60294 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
738 : {
739 60294 : tree lhs;
740 60294 : gimple *stmt, *new_stmt;
741 60294 : gimple_stmt_iterator i;
742 60294 : gimple_seq stmts = NULL;
743 :
744 60294 : stmt = gsi_stmt (*si_p);
745 :
746 60294 : gcc_assert (is_gimple_call (stmt));
747 :
748 60294 : 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 60291 : lhs = gimple_call_lhs (stmt);
773 60291 : if (lhs == NULL_TREE)
774 : {
775 2464 : push_gimplify_context (gimple_in_ssa_p (cfun));
776 1232 : gimplify_and_add (expr, &stmts);
777 1232 : 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 1232 : if (gimple_seq_empty_p (stmts))
782 : {
783 1097 : if (gimple_in_ssa_p (cfun))
784 : {
785 1097 : unlink_stmt_vdef (stmt);
786 1097 : release_defs (stmt);
787 : }
788 1097 : gsi_replace (si_p, gimple_build_nop (), false);
789 1097 : return;
790 : }
791 : }
792 : else
793 : {
794 59059 : tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
795 59059 : new_stmt = gimple_build_assign (lhs, tmp);
796 59059 : i = gsi_last (stmts);
797 59059 : gsi_insert_after_without_update (&i, new_stmt,
798 : GSI_CONTINUE_LINKING);
799 : }
800 :
801 59194 : 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 40508 : dump_transformation (gcall *from, gcall *to)
808 : {
809 40508 : 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 40508 : }
813 :
814 : /* Replace the call at *GSI with the gimple value VAL. */
815 :
816 : void
817 90537 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
818 : {
819 90537 : gimple *stmt = gsi_stmt (*gsi);
820 90537 : tree lhs = gimple_call_lhs (stmt);
821 90537 : gimple *repl;
822 90537 : if (lhs)
823 : {
824 85529 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
825 2176 : val = fold_convert (TREE_TYPE (lhs), val);
826 85529 : repl = gimple_build_assign (lhs, val);
827 : }
828 : else
829 5008 : repl = gimple_build_nop ();
830 90537 : tree vdef = gimple_vdef (stmt);
831 90537 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
832 : {
833 5582 : unlink_stmt_vdef (stmt);
834 5582 : release_ssa_name (vdef);
835 : }
836 90537 : gsi_replace (gsi, repl, false);
837 90537 : }
838 :
839 : /* Replace the call at *GSI with the new call REPL and fold that
840 : again. */
841 :
842 : static void
843 40508 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
844 : {
845 40508 : gimple *stmt = gsi_stmt (*gsi);
846 40508 : dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
847 40508 : gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
848 40508 : gimple_set_location (repl, gimple_location (stmt));
849 40508 : gimple_move_vops (repl, stmt);
850 40508 : gsi_replace (gsi, repl, false);
851 40508 : fold_stmt (gsi);
852 40508 : }
853 :
854 : /* Return true if VAR is a VAR_DECL or a component thereof. */
855 :
856 : static bool
857 419237 : var_decl_component_p (tree var)
858 : {
859 419237 : tree inner = var;
860 602188 : while (handled_component_p (inner))
861 182951 : inner = TREE_OPERAND (inner, 0);
862 419237 : return (DECL_P (inner)
863 419237 : || (TREE_CODE (inner) == MEM_REF
864 43102 : && 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 1041071 : size_must_be_zero_p (tree size)
872 : {
873 1041071 : if (integer_zerop (size))
874 : return true;
875 :
876 1038497 : if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
877 : return false;
878 :
879 628652 : tree type = TREE_TYPE (size);
880 628652 : 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 628652 : wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
885 628652 : wide_int zero = wi::zero (TYPE_PRECISION (type));
886 628652 : int_range_max valid_range (type, zero, ssize_max);
887 628652 : int_range_max vr;
888 1257304 : get_range_query (cfun)->range_of_expr (vr, size);
889 :
890 628652 : if (vr.undefined_p ())
891 87 : vr.set_varying (TREE_TYPE (size));
892 628652 : vr.intersect (valid_range);
893 628652 : return vr.zero_p ();
894 628652 : }
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 1041071 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
905 : tree dest, tree src, enum built_in_function code)
906 : {
907 1041071 : gimple *stmt = gsi_stmt (*gsi);
908 1041071 : tree lhs = gimple_call_lhs (stmt);
909 1041071 : tree len = gimple_call_arg (stmt, 2);
910 1041071 : 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 1041071 : if (size_must_be_zero_p (len))
915 : {
916 2619 : gimple *repl;
917 2619 : if (gimple_call_lhs (stmt))
918 60 : repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
919 : else
920 2559 : repl = gimple_build_nop ();
921 2619 : tree vdef = gimple_vdef (stmt);
922 2619 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
923 : {
924 462 : unlink_stmt_vdef (stmt);
925 462 : release_ssa_name (vdef);
926 : }
927 2619 : gsi_replace (gsi, repl, false);
928 2619 : return true;
929 : }
930 :
931 : /* If SRC and DEST are the same (and not volatile), return
932 : DEST{,+LEN,+LEN-1}. */
933 1038452 : 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 83 : unlink_stmt_vdef (stmt);
939 166 : if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
940 43 : release_ssa_name (gimple_vdef (stmt));
941 83 : if (!lhs)
942 : {
943 62 : gsi_replace (gsi, gimple_build_nop (), false);
944 62 : return true;
945 : }
946 21 : goto done;
947 : }
948 2076738 : 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 1038369 : tree srctype
956 1052003 : = POINTER_TYPE_P (TREE_TYPE (src))
957 1052003 : ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
958 1038369 : tree desttype
959 1058693 : = POINTER_TYPE_P (TREE_TYPE (dest))
960 1058693 : ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
961 1038369 : tree destvar, srcvar, srcoff;
962 1038369 : unsigned int src_align, dest_align;
963 1038369 : unsigned HOST_WIDE_INT tmp_len;
964 1038369 : const char *tmp_str;
965 :
966 : /* Build accesses at offset zero with a ref-all character type. */
967 1038369 : tree off0
968 1038369 : = 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 1038369 : src_align = get_pointer_alignment (src);
976 1038369 : dest_align = get_pointer_alignment (dest);
977 1038369 : if (tree_fits_uhwi_p (len)
978 399406 : && 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 299706 : && !c_strlen (src, 1)
986 193088 : && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
987 80943 : && memchr (tmp_str, 0, tmp_len) == NULL)
988 125762 : && !(srctype
989 125762 : && AGGREGATE_TYPE_P (srctype)
990 60280 : && TYPE_REVERSE_STORAGE_ORDER (srctype))
991 1163998 : && !(desttype
992 125629 : && AGGREGATE_TYPE_P (desttype)
993 69558 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
994 : {
995 125596 : unsigned ilen = tree_to_uhwi (len);
996 125596 : 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 25166 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
1004 : dest, src, len, len,
1005 25166 : false, false))
1006 981 : if (warning != OPT_Wrestrict)
1007 21805 : return false;
1008 :
1009 24244 : scalar_int_mode imode;
1010 24244 : machine_mode mode;
1011 24244 : if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
1012 24244 : && bitwise_mode_for_size (ilen
1013 24244 : * BITS_PER_UNIT).exists (&mode)
1014 48488 : && 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 24244 : && (dest_align >= GET_MODE_ALIGNMENT (mode)
1018 13118 : || !targetm.slow_unaligned_access (mode, dest_align)
1019 0 : || (optab_handler (movmisalign_optab, mode)
1020 : != CODE_FOR_nothing)))
1021 : {
1022 24244 : tree type = bitwise_type_for_mode (mode);
1023 24244 : tree srctype = type;
1024 24244 : tree desttype = type;
1025 24244 : if (src_align < GET_MODE_ALIGNMENT (mode))
1026 12503 : srctype = build_aligned_type (type, src_align);
1027 24244 : tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
1028 24244 : tree tem = fold_const_aggregate_ref (srcmem);
1029 24244 : if (tem)
1030 : srcmem = tem;
1031 23182 : else if (src_align < GET_MODE_ALIGNMENT (mode)
1032 12245 : && targetm.slow_unaligned_access (mode, src_align)
1033 23182 : && (optab_handler (movmisalign_optab, mode)
1034 : == CODE_FOR_nothing))
1035 : srcmem = NULL_TREE;
1036 23182 : if (srcmem)
1037 : {
1038 24244 : gimple *new_stmt;
1039 24244 : if (is_gimple_reg_type (TREE_TYPE (srcmem)))
1040 : {
1041 24244 : new_stmt = gimple_build_assign (NULL_TREE, srcmem);
1042 24244 : srcmem
1043 24244 : = make_ssa_name (TREE_TYPE (srcmem), new_stmt);
1044 24244 : gimple_assign_set_lhs (new_stmt, srcmem);
1045 48488 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1046 24244 : gimple_set_location (new_stmt, loc);
1047 24244 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1048 : }
1049 24244 : if (dest_align < GET_MODE_ALIGNMENT (mode))
1050 13118 : desttype = build_aligned_type (type, dest_align);
1051 24244 : new_stmt
1052 24244 : = gimple_build_assign (fold_build2 (MEM_REF, desttype,
1053 : dest, off0),
1054 : srcmem);
1055 24244 : gimple_move_vops (new_stmt, stmt);
1056 24244 : if (!lhs)
1057 : {
1058 20883 : gsi_replace (gsi, new_stmt, false);
1059 20883 : return true;
1060 : }
1061 3361 : gimple_set_location (new_stmt, loc);
1062 3361 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1063 3361 : goto done;
1064 : }
1065 : }
1066 : }
1067 : }
1068 :
1069 1013203 : 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 206623 : if (!dest_align || !src_align)
1077 : return false;
1078 206623 : if (readonly_data_expr (src)
1079 206623 : || (tree_fits_uhwi_p (len)
1080 32925 : && (MIN (src_align, dest_align) / BITS_PER_UNIT
1081 32925 : >= tree_to_uhwi (len))))
1082 : {
1083 979645 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1084 20469 : if (!fn)
1085 : return false;
1086 20469 : gimple_call_set_fndecl (stmt, fn);
1087 20469 : gimple_call_set_arg (stmt, 0, dest);
1088 20469 : gimple_call_set_arg (stmt, 1, src);
1089 20469 : fold_stmt (gsi);
1090 20469 : return true;
1091 : }
1092 :
1093 : /* If *src and *dest can't overlap, optimize into memcpy as well. */
1094 186154 : if (TREE_CODE (src) == ADDR_EXPR
1095 5491 : && TREE_CODE (dest) == ADDR_EXPR)
1096 : {
1097 1821 : tree src_base, dest_base, fn;
1098 1821 : poly_int64 src_offset = 0, dest_offset = 0;
1099 1821 : poly_uint64 maxsize;
1100 :
1101 1821 : srcvar = TREE_OPERAND (src, 0);
1102 1821 : src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
1103 1821 : if (src_base == NULL)
1104 0 : src_base = srcvar;
1105 1821 : destvar = TREE_OPERAND (dest, 0);
1106 1821 : dest_base = get_addr_base_and_unit_offset (destvar,
1107 : &dest_offset);
1108 1821 : if (dest_base == NULL)
1109 0 : dest_base = destvar;
1110 1821 : if (!poly_int_tree_p (len, &maxsize))
1111 229 : maxsize = -1;
1112 1821 : if (SSA_VAR_P (src_base)
1113 1811 : && SSA_VAR_P (dest_base))
1114 : {
1115 1811 : if (operand_equal_p (src_base, dest_base, 0)
1116 1811 : && 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 1821 : 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 184333 : if ((is_gimple_min_invariant (dest)
1150 180648 : || TREE_CODE (dest) == SSA_NAME)
1151 346513 : && (is_gimple_min_invariant (src)
1152 162216 : || TREE_CODE (src) == SSA_NAME))
1153 : {
1154 165422 : ao_ref destr, srcr;
1155 165422 : ao_ref_init_from_ptr_and_size (&destr, dest, len);
1156 165422 : ao_ref_init_from_ptr_and_size (&srcr, src, len);
1157 165422 : if (!refs_may_alias_p_1 (&destr, &srcr, false))
1158 : {
1159 10291 : tree fn;
1160 10291 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1161 10291 : if (!fn)
1162 10291 : return false;
1163 10291 : gimple_call_set_fndecl (stmt, fn);
1164 10291 : gimple_call_set_arg (stmt, 0, dest);
1165 10291 : gimple_call_set_arg (stmt, 1, src);
1166 10291 : fold_stmt (gsi);
1167 10291 : return true;
1168 : }
1169 : }
1170 :
1171 174042 : return false;
1172 : }
1173 :
1174 806580 : if (!tree_fits_shwi_p (len))
1175 : return false;
1176 324291 : if (!srctype
1177 324291 : || (AGGREGATE_TYPE_P (srctype)
1178 203633 : && TYPE_REVERSE_STORAGE_ORDER (srctype)))
1179 : return false;
1180 324158 : if (!desttype
1181 324158 : || (AGGREGATE_TYPE_P (desttype)
1182 191253 : && 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 324125 : if (TREE_CODE (srctype) == ARRAY_TYPE
1191 324125 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1192 129078 : srctype = TREE_TYPE (srctype);
1193 324125 : if (TREE_CODE (desttype) == ARRAY_TYPE
1194 324125 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
1195 103959 : desttype = TREE_TYPE (desttype);
1196 324125 : if (TREE_ADDRESSABLE (srctype)
1197 324078 : || 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 647497 : if (FLOAT_MODE_P (TYPE_MODE (desttype))
1203 323204 : || TREE_CODE (desttype) == BOOLEAN_TYPE
1204 647212 : || TREE_CODE (desttype) == ENUMERAL_TYPE)
1205 868 : desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
1206 647627 : if (FLOAT_MODE_P (TYPE_MODE (srctype))
1207 323485 : || TREE_CODE (srctype) == BOOLEAN_TYPE
1208 647497 : || TREE_CODE (srctype) == ENUMERAL_TYPE)
1209 583 : srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
1210 324030 : if (!srctype)
1211 120 : srctype = desttype;
1212 324030 : if (!desttype)
1213 0 : desttype = srctype;
1214 324030 : if (!srctype)
1215 : return false;
1216 :
1217 324030 : src_align = get_pointer_alignment (src);
1218 324030 : 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 324030 : destvar = NULL_TREE;
1227 324030 : srcvar = NULL_TREE;
1228 324030 : if (TREE_CODE (dest) == ADDR_EXPR
1229 115337 : && var_decl_component_p (TREE_OPERAND (dest, 0))
1230 115333 : && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
1231 24838 : && dest_align >= TYPE_ALIGN (desttype)
1232 348868 : && (is_gimple_reg_type (desttype)
1233 24414 : || src_align >= TYPE_ALIGN (desttype)))
1234 19572 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1235 304458 : else if (TREE_CODE (src) == ADDR_EXPR
1236 239642 : && var_decl_component_p (TREE_OPERAND (src, 0))
1237 46879 : && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
1238 8799 : && src_align >= TYPE_ALIGN (srctype)
1239 313241 : && (is_gimple_reg_type (srctype)
1240 8620 : || dest_align >= TYPE_ALIGN (srctype)))
1241 3126 : 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 301332 : 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 301335 : && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
1250 3 : srctype = TREE_TYPE (srcvar);
1251 : else
1252 301329 : 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 22701 : if (srcvar == NULL_TREE)
1258 : {
1259 19572 : if (src_align >= TYPE_ALIGN (desttype))
1260 19554 : srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1261 : else
1262 : {
1263 18 : enum machine_mode mode = TYPE_MODE (desttype);
1264 18 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1265 18 : || (targetm.slow_unaligned_access (mode, src_align)
1266 18 : && (optab_handler (movmisalign_optab, mode)
1267 : == CODE_FOR_nothing)))
1268 : return false;
1269 18 : srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1270 : src_align);
1271 18 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1272 : }
1273 : }
1274 3129 : else if (destvar == NULL_TREE)
1275 : {
1276 3129 : if (dest_align >= TYPE_ALIGN (srctype))
1277 3129 : 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 22701 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
1298 : dest, src, len, len,
1299 22701 : false, false))
1300 1263 : if (warning != OPT_Wrestrict)
1301 : return false;
1302 :
1303 21446 : gimple *new_stmt;
1304 21446 : if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1305 : {
1306 547 : tree tem = fold_const_aggregate_ref (srcvar);
1307 547 : if (tem)
1308 526 : srcvar = tem;
1309 547 : if (! is_gimple_min_invariant (srcvar))
1310 : {
1311 21 : new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1312 21 : srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
1313 21 : gimple_assign_set_lhs (new_stmt, srcvar);
1314 42 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1315 21 : gimple_set_location (new_stmt, loc);
1316 21 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1317 : }
1318 547 : new_stmt = gimple_build_assign (destvar, srcvar);
1319 547 : 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 20899 : 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 41792 : desttype = build_array_type_nelts (unsigned_char_type_node,
1333 20896 : tree_to_uhwi (len));
1334 20896 : srctype = desttype;
1335 20896 : if (src_align > TYPE_ALIGN (srctype))
1336 12580 : srctype = build_aligned_type (srctype, src_align);
1337 20896 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1338 : }
1339 :
1340 20899 : if (dest_align > TYPE_ALIGN (desttype))
1341 13169 : desttype = build_aligned_type (desttype, dest_align);
1342 20899 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1343 20899 : new_stmt = gimple_build_assign (destvar, srcvar);
1344 :
1345 21446 : set_vop_and_replace:
1346 21446 : gimple_move_vops (new_stmt, stmt);
1347 21446 : if (!lhs)
1348 : {
1349 21262 : gsi_replace (gsi, new_stmt, false);
1350 21262 : 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 3566 : done:
1357 3566 : gimple_seq stmts = NULL;
1358 3566 : if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
1359 3566 : 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 3566 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1370 3566 : gimple *repl = gimple_build_assign (lhs, dest);
1371 3566 : gsi_replace (gsi, repl, false);
1372 3566 : 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 314235 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1463 : {
1464 314235 : gimple *stmt = gsi_stmt (*gsi);
1465 314235 : tree etype;
1466 314235 : unsigned HOST_WIDE_INT length, cval;
1467 :
1468 : /* If the LEN parameter is zero, return DEST. */
1469 314235 : if (integer_zerop (len))
1470 : {
1471 833 : replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1472 833 : return true;
1473 : }
1474 :
1475 938450 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1476 : return false;
1477 :
1478 313324 : if (! tree_fits_uhwi_p (len))
1479 : return false;
1480 :
1481 207302 : if (TREE_CODE (c) != INTEGER_CST)
1482 : return false;
1483 :
1484 201366 : tree dest = gimple_call_arg (stmt, 0);
1485 201366 : tree var = dest;
1486 201366 : if (TREE_CODE (var) != ADDR_EXPR)
1487 : return false;
1488 :
1489 161308 : var = TREE_OPERAND (var, 0);
1490 161308 : if (TREE_THIS_VOLATILE (var))
1491 : return false;
1492 :
1493 161265 : etype = TREE_TYPE (var);
1494 161265 : if (TREE_CODE (etype) == ARRAY_TYPE)
1495 85042 : etype = TREE_TYPE (etype);
1496 :
1497 161265 : if ((!INTEGRAL_TYPE_P (etype)
1498 96231 : && !POINTER_TYPE_P (etype))
1499 161793 : || BITINT_TYPE_P (etype))
1500 : return false;
1501 :
1502 64258 : if (! var_decl_component_p (var))
1503 : return false;
1504 :
1505 64258 : length = tree_to_uhwi (len);
1506 64258 : if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
1507 1756 : || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
1508 3512 : != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
1509 66014 : || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1510 62502 : return false;
1511 :
1512 1756 : if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1513 : return false;
1514 :
1515 1756 : 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 1756 : 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 1756 : var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1534 1756 : gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1535 1756 : gimple_move_vops (store, stmt);
1536 1756 : gimple_set_location (store, gimple_location (stmt));
1537 1756 : gsi_insert_before (gsi, store, GSI_SAME_STMT);
1538 1756 : 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 1754 : gimple_stmt_iterator gsi2 = *gsi;
1546 1754 : gsi_prev (gsi);
1547 1754 : 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 450597 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
1557 : c_strlen_data *pdata, unsigned eltsize)
1558 : {
1559 450597 : gcc_assert (TREE_CODE (arg) != SSA_NAME);
1560 :
1561 : /* The length computed by this invocation of the function. */
1562 450597 : 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 450597 : bool tight_bound = false;
1569 :
1570 : /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1571 450597 : if (TREE_CODE (arg) == ADDR_EXPR
1572 450597 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
1573 : {
1574 28861 : tree op = TREE_OPERAND (arg, 0);
1575 28861 : if (integer_zerop (TREE_OPERAND (op, 1)))
1576 : {
1577 12585 : tree aop0 = TREE_OPERAND (op, 0);
1578 12585 : if (TREE_CODE (aop0) == INDIRECT_REF
1579 12585 : && 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 16276 : else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
1584 16276 : && 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 450389 : 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 447306 : c_strlen_data lendata = { };
1613 447306 : val = c_strlen (arg, 1, &lendata, eltsize);
1614 :
1615 447306 : if (!val && lendata.decl)
1616 : {
1617 : /* ARG refers to an unterminated const character array.
1618 : DATA.DECL with size DATA.LEN. */
1619 4214 : val = lendata.minlen;
1620 4214 : 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 447816 : bool maxbound = false;
1627 :
1628 447816 : if (!val && rkind == SRK_LENRANGE)
1629 : {
1630 245128 : if (TREE_CODE (arg) == ADDR_EXPR)
1631 87053 : return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
1632 87053 : pdata, eltsize);
1633 :
1634 158075 : if (TREE_CODE (arg) == ARRAY_REF)
1635 : {
1636 18664 : tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
1637 :
1638 : /* Determine the "innermost" array type. */
1639 18664 : while (TREE_CODE (optype) == ARRAY_TYPE
1640 25610 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1641 6946 : optype = TREE_TYPE (optype);
1642 :
1643 : /* Avoid arrays of pointers. */
1644 18664 : tree eltype = TREE_TYPE (optype);
1645 18664 : if (TREE_CODE (optype) != ARRAY_TYPE
1646 18664 : || !INTEGRAL_TYPE_P (eltype))
1647 : return false;
1648 :
1649 : /* Fail when the array bound is unknown or zero. */
1650 13626 : val = TYPE_SIZE_UNIT (optype);
1651 13626 : if (!val
1652 13554 : || TREE_CODE (val) != INTEGER_CST
1653 27152 : || integer_zerop (val))
1654 105 : return false;
1655 :
1656 13521 : 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 13521 : pdata->minlen = ssize_int (0);
1662 :
1663 13521 : tight_bound = true;
1664 : }
1665 139411 : else if (TREE_CODE (arg) == COMPONENT_REF
1666 139411 : && (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 9890 : tree fld = TREE_OPERAND (arg, 1);
1677 :
1678 9890 : tree optype = TREE_TYPE (fld);
1679 :
1680 : /* Determine the "innermost" array type. */
1681 9890 : while (TREE_CODE (optype) == ARRAY_TYPE
1682 10452 : && 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 9890 : val = TYPE_SIZE_UNIT (optype);
1687 9890 : if (!val
1688 9654 : || TREE_CODE (val) != INTEGER_CST
1689 19509 : || integer_zerop (val))
1690 350 : return false;
1691 9540 : 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 9540 : 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 9540 : tight_bound = true;
1705 : }
1706 129521 : else if (TREE_CODE (arg) == MEM_REF
1707 35254 : && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
1708 10393 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
1709 139476 : && 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 9955 : tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1716 9955 : if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
1717 19897 : && (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 9829 : val = DECL_SIZE_UNIT (ref);
1723 9829 : if (!val
1724 9657 : || TREE_CODE (val) != INTEGER_CST
1725 19486 : || integer_zerop (val))
1726 371 : return false;
1727 :
1728 9655 : poly_offset_int psiz = wi::to_offset (val);
1729 9655 : poly_offset_int poff = mem_ref_offset (arg);
1730 9655 : if (known_le (psiz, poff))
1731 : return false;
1732 :
1733 9458 : pdata->minlen = ssize_int (0);
1734 :
1735 : /* Subtract the offset and one for the terminating nul. */
1736 9458 : psiz -= poff;
1737 9458 : psiz -= 1;
1738 9458 : 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 119566 : 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 70957 : tree argtype = TREE_TYPE (arg);
1749 70957 : if (TREE_CODE (argtype) == ARRAY_TYPE)
1750 : {
1751 42481 : val = TYPE_SIZE_UNIT (argtype);
1752 42481 : if (!val
1753 41715 : || TREE_CODE (val) != INTEGER_CST
1754 84196 : || integer_zerop (val))
1755 881 : return false;
1756 41600 : val = wide_int_to_tree (TREE_TYPE (val),
1757 41600 : 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 41600 : pdata->minlen = ssize_int (0);
1762 : }
1763 : }
1764 : maxbound = true;
1765 : }
1766 :
1767 354018 : if (!val)
1768 : return false;
1769 :
1770 : /* Adjust the lower bound on the string length as necessary. */
1771 252450 : if (!pdata->minlen
1772 252450 : || (rkind != SRK_STRLEN
1773 78692 : && TREE_CODE (pdata->minlen) == INTEGER_CST
1774 78692 : && TREE_CODE (val) == INTEGER_CST
1775 78687 : && tree_int_cst_lt (val, pdata->minlen)))
1776 173858 : pdata->minlen = val;
1777 :
1778 252450 : 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 7985 : if (TREE_CODE (val) == INTEGER_CST)
1784 : {
1785 7985 : if (tree_int_cst_lt (pdata->maxbound, val))
1786 740 : pdata->maxbound = val;
1787 : }
1788 : else
1789 0 : pdata->maxbound = val;
1790 : }
1791 244465 : 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 70886 : pdata->maxbound = val;
1796 :
1797 252450 : 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 23061 : if (rkind == SRK_LENRANGE)
1804 : {
1805 23061 : poly_int64 offset;
1806 23061 : tree base = get_addr_base_and_unit_offset (arg, &offset);
1807 23061 : 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 23061 : tree type = TREE_TYPE (base);
1820 23061 : if (POINTER_TYPE_P (type)
1821 23057 : || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
1822 41769 : || !(val = DECL_SIZE_UNIT (base)))
1823 5600 : val = build_all_ones_cst (size_type_node);
1824 : else
1825 : {
1826 17461 : val = DECL_SIZE_UNIT (base);
1827 17461 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1828 : size_int (offset + 1));
1829 : }
1830 : }
1831 : else
1832 : return false;
1833 : }
1834 :
1835 252450 : if (pdata->maxlen)
1836 : {
1837 : /* Adjust the more conservative bound if possible/necessary
1838 : and fail otherwise. */
1839 15586 : if (rkind != SRK_STRLEN)
1840 : {
1841 14655 : if (TREE_CODE (pdata->maxlen) != INTEGER_CST
1842 14655 : || TREE_CODE (val) != INTEGER_CST)
1843 : return false;
1844 :
1845 14650 : if (tree_int_cst_lt (pdata->maxlen, val))
1846 1463 : pdata->maxlen = val;
1847 14650 : 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 236988 : pdata->maxlen = val;
1858 236988 : 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 1370162 : get_range_strlen (tree arg, bitmap visited,
1879 : strlen_range_kind rkind,
1880 : c_strlen_data *pdata, unsigned eltsize)
1881 : {
1882 :
1883 1452049 : if (TREE_CODE (arg) != SSA_NAME)
1884 450597 : 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 1001452 : if (name_registered_for_update_p (arg))
1889 : return false;
1890 :
1891 : /* If we were already here, break the infinite cycle. */
1892 1001452 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
1893 : return true;
1894 :
1895 995795 : tree var = arg;
1896 995795 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
1897 :
1898 995795 : switch (gimple_code (def_stmt))
1899 : {
1900 125464 : 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 125464 : if (gimple_assign_single_p (def_stmt)
1905 125464 : || gimple_assign_unary_nop_p (def_stmt))
1906 : {
1907 81887 : tree rhs = gimple_assign_rhs1 (def_stmt);
1908 81887 : return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
1909 : }
1910 43577 : 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 86340 : for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
1938 : {
1939 62774 : 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 62774 : if (arg == gimple_phi_result (def_stmt))
1948 0 : continue;
1949 :
1950 62774 : if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
1951 : {
1952 30253 : 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 28519 : 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 1146829 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
1988 : {
1989 1146829 : auto_bitmap visited;
1990 1146829 : tree maxbound = pdata->maxbound;
1991 :
1992 1146829 : 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 921809 : pdata->minlen = ssize_int (0);
1998 921809 : pdata->maxlen = build_all_ones_cst (size_type_node);
1999 : }
2000 225020 : else if (!pdata->minlen)
2001 9086 : 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 1146829 : if (maxbound && pdata->maxbound == maxbound)
2006 650797 : pdata->maxbound = build_all_ones_cst (size_type_node);
2007 :
2008 1146829 : return !integer_all_onesp (pdata->maxlen);
2009 1146829 : }
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 95842 : 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 95842 : gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
2026 :
2027 : // If arg is already a constant, simply return it.
2028 95842 : 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 72915 : gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
2033 :
2034 73014 : auto_bitmap visited;
2035 :
2036 : /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
2037 : is unbounded. */
2038 73014 : c_strlen_data lendata = { };
2039 73014 : if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
2040 49912 : lendata.maxlen = NULL_TREE;
2041 23102 : else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
2042 0 : lendata.maxlen = NULL_TREE;
2043 :
2044 73014 : 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 24074 : *nonstr = lendata.decl;
2050 24074 : return lendata.maxlen;
2051 : }
2052 :
2053 : /* Fail if the constant array isn't nul-terminated. */
2054 48940 : return lendata.decl ? NULL_TREE : lendata.maxlen;
2055 73014 : }
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 26136 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
2086 : tree dest, tree src)
2087 : {
2088 26136 : gimple *stmt = gsi_stmt (*gsi);
2089 26136 : location_t loc = gimple_location (stmt);
2090 26136 : tree fn;
2091 :
2092 : /* If SRC and DEST are the same (and not volatile), return DEST. */
2093 26136 : 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 26050 : if (optimize_function_for_size_p (cfun))
2113 : return false;
2114 :
2115 24074 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2116 24074 : if (!fn)
2117 : return false;
2118 :
2119 : /* Set to non-null if ARG refers to an unterminated array. */
2120 24074 : tree nonstr = NULL;
2121 24074 : tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
2122 :
2123 24074 : 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 28942 : if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2133 : return false;
2134 :
2135 2892 : len = fold_convert_loc (loc, size_type_node, len);
2136 2892 : len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
2137 2892 : len = force_gimple_operand_gsi (gsi, len, true,
2138 : NULL_TREE, true, GSI_SAME_STMT);
2139 2892 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2140 2892 : replace_call_with_call_and_fold (gsi, repl);
2141 2892 : 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 5472 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
2226 : {
2227 5472 : gimple *stmt = gsi_stmt (*gsi);
2228 5472 : tree str = gimple_call_arg (stmt, 0);
2229 5472 : tree c = gimple_call_arg (stmt, 1);
2230 5472 : location_t loc = gimple_location (stmt);
2231 5472 : const char *p;
2232 5472 : char ch;
2233 :
2234 5472 : 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 5462 : if (!check_nul_terminated_array (NULL_TREE, str))
2240 : return false;
2241 :
2242 5378 : 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 5419 : 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 5428 : 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 4384 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
2314 : {
2315 4384 : gimple *stmt = gsi_stmt (*gsi);
2316 4384 : if (!gimple_call_lhs (stmt))
2317 : return false;
2318 :
2319 4381 : tree haystack = gimple_call_arg (stmt, 0);
2320 4381 : 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 4381 : if (!check_nul_terminated_array (NULL_TREE, haystack)
2325 4381 : || !check_nul_terminated_array (NULL_TREE, needle))
2326 19 : return false;
2327 :
2328 4362 : const char *q = c_getstr (needle);
2329 4362 : if (q == NULL)
2330 : return false;
2331 :
2332 3182 : 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 3168 : if (q[0] == '\0')
2354 : {
2355 6 : replace_call_with_value (gsi, haystack);
2356 6 : return true;
2357 : }
2358 :
2359 10666 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2360 : return false;
2361 :
2362 : /* Transform strstr (x, "c") into strchr (x, 'c'). */
2363 3162 : 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 7338 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
2398 : {
2399 7338 : gimple *stmt = gsi_stmt (*gsi);
2400 7338 : location_t loc = gimple_location (stmt);
2401 :
2402 7338 : const char *p = c_getstr (src);
2403 :
2404 : /* If the string length is zero, return the dst parameter. */
2405 7338 : if (p && *p == '\0')
2406 : {
2407 72 : replace_call_with_value (gsi, dst);
2408 72 : return true;
2409 : }
2410 :
2411 7266 : if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
2412 : return false;
2413 :
2414 19879 : 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 6679 : tree newdst;
2419 6679 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2420 6679 : tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2421 :
2422 6679 : 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 6679 : tree len = get_maxval_strlen (src, SRK_STRLEN);
2428 6679 : if (! len)
2429 : return false;
2430 :
2431 : /* Create strlen (dst). */
2432 745 : gimple_seq stmts = NULL, stmts2;
2433 745 : gimple *repl = gimple_build_call (strlen_fn, 1, dst);
2434 745 : gimple_set_location (repl, loc);
2435 745 : newdst = make_ssa_name (size_type_node);
2436 745 : gimple_call_set_lhs (repl, newdst);
2437 745 : gimple_seq_add_stmt_without_update (&stmts, repl);
2438 :
2439 : /* Create (dst p+ strlen (dst)). */
2440 745 : newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
2441 745 : newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
2442 745 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2443 :
2444 745 : len = fold_convert_loc (loc, size_type_node, len);
2445 745 : len = size_binop_loc (loc, PLUS_EXPR, len,
2446 : build_int_cst (size_type_node, 1));
2447 745 : len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
2448 745 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2449 :
2450 745 : repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
2451 745 : gimple_seq_add_stmt_without_update (&stmts, repl);
2452 745 : 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 580 : gsi_replace_with_seq_vops (gsi, stmts);
2468 580 : 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 1250714 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
2680 : {
2681 1250714 : gimple *stmt = gsi_stmt (*gsi);
2682 1250714 : tree callee = gimple_call_fndecl (stmt);
2683 1250714 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
2684 :
2685 1250714 : tree type = integer_type_node;
2686 1250714 : tree str1 = gimple_call_arg (stmt, 0);
2687 1250714 : tree str2 = gimple_call_arg (stmt, 1);
2688 1250714 : tree lhs = gimple_call_lhs (stmt);
2689 :
2690 1250714 : tree bound_node = NULL_TREE;
2691 1250714 : unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
2692 :
2693 : /* Handle strncmp and strncasecmp functions. */
2694 1250714 : if (gimple_call_num_args (stmt) == 3)
2695 : {
2696 22999 : bound_node = gimple_call_arg (stmt, 2);
2697 22999 : if (tree_fits_uhwi_p (bound_node))
2698 17275 : bound = tree_to_uhwi (bound_node);
2699 : }
2700 :
2701 : /* If the BOUND parameter is zero, return zero. */
2702 17275 : 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 1250710 : 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 2501338 : 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 1250669 : unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
2724 1250669 : const char *p1 = getbyterep (str1, &len1);
2725 1250669 : 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 1250669 : unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
2730 :
2731 1250669 : if (p1)
2732 : {
2733 42839 : size_t n = strnlen (p1, len1);
2734 42839 : if (n < len1)
2735 42732 : len1 = nulpos1 = n;
2736 : }
2737 :
2738 1250669 : if (p2)
2739 : {
2740 1219586 : size_t n = strnlen (p2, len2);
2741 1219586 : if (n < len2)
2742 1219527 : len2 = nulpos2 = n;
2743 : }
2744 :
2745 : /* For known strings, return an immediate value. */
2746 1250669 : 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 2424974 : bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
2810 1195356 : || fcode == BUILT_IN_STRCMP
2811 1195356 : || fcode == BUILT_IN_STRCMP_EQ
2812 1218389 : || fcode == BUILT_IN_STRCASECMP;
2813 :
2814 1212487 : location_t loc = gimple_location (stmt);
2815 :
2816 : /* If the second arg is "", return *(const unsigned char*)arg1. */
2817 1212487 : 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 1212337 : 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 1212238 : 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 1212128 : if (fcode == BUILT_IN_STRNCMP
2881 17737 : && bound > 0 && bound < HOST_WIDE_INT_M1U
2882 12145 : && ((p2 && len2 < bound && len2 == nulpos2)
2883 11913 : || (p1 && len1 < bound && len1 == nulpos1)))
2884 : {
2885 1212128 : 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 37487 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2900 : {
2901 37487 : gimple *stmt = gsi_stmt (*gsi);
2902 37487 : tree lhs = gimple_call_lhs (stmt);
2903 37487 : tree arg1 = gimple_call_arg (stmt, 0);
2904 37487 : tree arg2 = gimple_call_arg (stmt, 1);
2905 37487 : tree len = gimple_call_arg (stmt, 2);
2906 :
2907 : /* If the LEN parameter is zero, return zero. */
2908 37487 : 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 37486 : char c;
2915 37486 : if (TREE_CODE (arg2) != INTEGER_CST
2916 21863 : || !tree_fits_uhwi_p (len)
2917 38200 : || !target_char_cst_p (arg2, &c))
2918 36772 : 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 20732 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
2975 : tree arg0, tree arg1,
2976 : bool unlocked)
2977 : {
2978 20732 : gimple *stmt = gsi_stmt (*gsi);
2979 :
2980 : /* If we're using an unlocked function, assume the other unlocked
2981 : functions exist explicitly. */
2982 20732 : tree const fn_fputc = (unlocked
2983 20732 : ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2984 20689 : : builtin_decl_implicit (BUILT_IN_FPUTC));
2985 20689 : tree const fn_fwrite = (unlocked
2986 43 : ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2987 20732 : : builtin_decl_implicit (BUILT_IN_FWRITE));
2988 :
2989 : /* If the return value is used, don't do the transformation. */
2990 20732 : 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 20661 : tree len = get_maxval_strlen (arg0, SRK_STRLEN);
2996 20661 : if (!len || TREE_CODE (len) != INTEGER_CST)
2997 : return false;
2998 :
2999 16225 : 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 1064 : case 0: /* length is 1, call fputc. */
3006 1064 : {
3007 1064 : const char *p = c_getstr (arg0);
3008 1064 : if (p != NULL)
3009 : {
3010 2100 : if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3011 : return false;
3012 :
3013 1050 : gimple *repl
3014 1050 : = gimple_build_call (fn_fputc, 2,
3015 1050 : build_int_cst (integer_type_node, p[0]),
3016 : arg1);
3017 1050 : replace_call_with_call_and_fold (gsi, repl);
3018 1050 : return true;
3019 : }
3020 : }
3021 : /* FALLTHROUGH */
3022 15084 : case 1: /* length is greater than 1, call fwrite. */
3023 15084 : {
3024 : /* If optimizing for size keep fputs. */
3025 15084 : 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 27832 : if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3030 : return false;
3031 :
3032 8241 : gimple *repl
3033 8241 : = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
3034 : fold_convert (size_type_node, len), arg1);
3035 8241 : replace_call_with_call_and_fold (gsi, repl);
3036 8241 : 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 54589 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
3808 : tree fp, tree fmt, tree arg,
3809 : enum built_in_function fcode)
3810 : {
3811 54589 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3812 54589 : tree fn_fputc, fn_fputs;
3813 54589 : const char *fmt_str = NULL;
3814 :
3815 : /* If the return value is used, don't do the transformation. */
3816 54589 : if (gimple_call_lhs (stmt) != NULL_TREE)
3817 : return false;
3818 :
3819 : /* Check whether the format is a literal string constant. */
3820 50412 : fmt_str = c_getstr (fmt);
3821 50412 : if (fmt_str == NULL)
3822 : return false;
3823 :
3824 50082 : 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 50002 : fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
3834 50002 : fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
3835 : }
3836 :
3837 50082 : if (!init_target_chars ())
3838 : return false;
3839 :
3840 144435 : 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 50082 : if (strchr (fmt_str, target_percent) == NULL)
3845 : {
3846 9626 : if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
3847 9556 : && arg)
3848 : return false;
3849 :
3850 : /* If the format specifier was "", fprintf does nothing. */
3851 9626 : 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 9568 : if (fn_fputs)
3861 : {
3862 9568 : gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
3863 9568 : replace_call_with_call_and_fold (gsi, repl);
3864 9568 : return true;
3865 : }
3866 : }
3867 :
3868 : /* The other optimizations can be done only on the non-va_list variants. */
3869 40456 : 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 39407 : else if (strcmp (fmt_str, target_percent_s) == 0)
3874 : {
3875 643 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3876 : return false;
3877 643 : if (fn_fputs)
3878 : {
3879 643 : gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3880 643 : replace_call_with_call_and_fold (gsi, repl);
3881 643 : return true;
3882 : }
3883 : }
3884 :
3885 : /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3886 38764 : 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 124068 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3912 : tree arg, enum built_in_function fcode)
3913 : {
3914 124068 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3915 124068 : tree fn_putchar, fn_puts, newarg;
3916 124068 : const char *fmt_str = NULL;
3917 :
3918 : /* If the return value is used, don't do the transformation. */
3919 124068 : if (gimple_call_lhs (stmt) != NULL_TREE)
3920 : return false;
3921 :
3922 360768 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3923 : return false;
3924 :
3925 : /* Check whether the format is a literal string constant. */
3926 120810 : fmt_str = c_getstr (fmt);
3927 120810 : if (fmt_str == NULL)
3928 : return false;
3929 :
3930 117773 : 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 117693 : fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3940 117693 : fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3941 : }
3942 :
3943 117773 : if (!init_target_chars ())
3944 : return false;
3945 :
3946 117773 : if (strcmp (fmt_str, target_percent_s) == 0
3947 111387 : || strchr (fmt_str, target_percent) == NULL)
3948 : {
3949 14225 : const char *str;
3950 :
3951 14225 : 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 7839 : if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3967 7707 : && arg)
3968 : return false;
3969 : str = fmt_str;
3970 : }
3971 :
3972 : /* If the string was "", printf does nothing. */
3973 5790 : 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 5681 : 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 5122 : size_t len = strlen (str);
3997 5122 : if ((unsigned char)str[len - 1] == target_newline
3998 4022 : && (size_t) (int) len == len
3999 4022 : && (int) len > 0)
4000 : {
4001 4022 : char *newstr;
4002 :
4003 : /* Create a NUL-terminated string that's one char shorter
4004 : than the original, stripping off the trailing '\n'. */
4005 4022 : newstr = xstrdup (str);
4006 4022 : newstr[len - 1] = '\0';
4007 4022 : newarg = build_string_literal (len, newstr);
4008 4022 : free (newstr);
4009 4022 : if (fn_puts)
4010 : {
4011 4022 : gcall *repl = gimple_build_call (fn_puts, 1, newarg);
4012 4022 : replace_call_with_call_and_fold (gsi, repl);
4013 4022 : 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 103548 : 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 103298 : else if (strcmp (fmt_str, target_percent_s_newline) == 0)
4029 : {
4030 183 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
4031 : return false;
4032 183 : if (fn_puts)
4033 : {
4034 183 : gcall *repl = gimple_build_call (fn_puts, 1, arg);
4035 183 : replace_call_with_call_and_fold (gsi, repl);
4036 183 : return true;
4037 : }
4038 : }
4039 :
4040 : /* If the format specifier was "%c", call __builtin_putchar(arg). */
4041 103115 : 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 148789 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
4063 : {
4064 148789 : gimple *stmt = gsi_stmt (*gsi);
4065 148789 : tree arg = gimple_call_arg (stmt, 0);
4066 :
4067 148789 : wide_int minlen;
4068 148789 : wide_int maxlen;
4069 :
4070 148789 : c_strlen_data lendata = { };
4071 148789 : if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
4072 35021 : && !lendata.decl
4073 32058 : && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
4074 180742 : && 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 31953 : minlen = wi::to_wide (lendata.minlen);
4082 31953 : maxlen = wi::to_wide (lendata.maxlen);
4083 : }
4084 : else
4085 : {
4086 116836 : unsigned prec = TYPE_PRECISION (sizetype);
4087 :
4088 116836 : minlen = wi::shwi (0, prec);
4089 116836 : 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 148789 : if (sanitize_flags_p (SANITIZE_ADDRESS))
4095 278 : maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
4096 :
4097 148789 : if (minlen == maxlen)
4098 : {
4099 : /* Fold the strlen call to a constant. */
4100 1620 : tree type = TREE_TYPE (lendata.minlen);
4101 3240 : tree len = force_gimple_operand_gsi (gsi,
4102 1620 : wide_int_to_tree (type, minlen),
4103 : true, NULL, true, GSI_SAME_STMT);
4104 1620 : replace_call_with_value (gsi, len);
4105 1620 : return true;
4106 : }
4107 :
4108 : /* Set the strlen() range to [0, MAXLEN]. */
4109 147169 : if (tree lhs = gimple_call_lhs (stmt))
4110 147164 : set_strlen_range (lhs, minlen, maxlen);
4111 :
4112 : return false;
4113 148789 : }
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 321 : gimple_fold_builtin_omp_get_num_devices (gimple_stmt_iterator *gsi)
4157 : {
4158 321 : if (!ENABLE_OFFLOADING)
4159 : {
4160 0 : replace_call_with_value (gsi, integer_zero_node);
4161 321 : 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 49380 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
4212 : {
4213 49380 : gimple *stmt = gsi_stmt (*gsi);
4214 49380 : tree arg = gimple_call_arg (stmt, 0);
4215 49380 : tree size = gimple_call_arg (stmt, 1);
4216 :
4217 146747 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
4218 : return false;
4219 :
4220 49380 : if (operand_equal_p (arg, null_pointer_node, 0))
4221 : {
4222 1393 : tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
4223 1393 : if (fn_malloc)
4224 : {
4225 1393 : gcall *repl = gimple_build_call (fn_malloc, 1, size);
4226 1393 : replace_call_with_call_and_fold (gsi, repl);
4227 1393 : 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 38867 : clear_padding_flush (clear_padding_struct *buf, bool full)
4277 : {
4278 38867 : gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
4279 38867 : if (!full && buf->size < 2 * clear_padding_unit)
4280 38867 : return;
4281 39915 : gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
4282 38825 : size_t end = buf->size;
4283 38825 : if (!full)
4284 42 : end = ((end - clear_padding_unit - 1) / clear_padding_unit
4285 : * clear_padding_unit);
4286 38825 : size_t padding_bytes = buf->padding_bytes;
4287 38825 : if (buf->union_ptr)
4288 : {
4289 38058 : 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 333272 : for (size_t i = 0; i < end; i++)
4294 : {
4295 295607 : if (buf->buf[i] == (unsigned char) ~0)
4296 9052 : padding_bytes++;
4297 : else
4298 : {
4299 286555 : memset (&buf->union_ptr[buf->off + i - padding_bytes],
4300 : 0, padding_bytes);
4301 286555 : padding_bytes = 0;
4302 286555 : buf->union_ptr[buf->off + i] &= ~buf->buf[i];
4303 : }
4304 : }
4305 37665 : if (full)
4306 : {
4307 37665 : memset (&buf->union_ptr[buf->off + end - padding_bytes],
4308 : 0, padding_bytes);
4309 37665 : buf->off = 0;
4310 37665 : buf->size = 0;
4311 37665 : 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 37665 : 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 767 : size_t wordsize = UNITS_PER_WORD;
4351 23514 : for (size_t i = 0; i < end; i += wordsize)
4352 : {
4353 22747 : size_t nonzero_first = wordsize;
4354 22747 : size_t nonzero_last = 0;
4355 22747 : size_t zero_first = wordsize;
4356 22747 : size_t zero_last = 0;
4357 22747 : bool all_ones = true, bytes_only = true;
4358 23033 : if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
4359 22747 : > (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 22461 : size_t endsize = end - i > wordsize ? wordsize : end - i;
4367 200904 : for (size_t j = i; j < i + endsize; j++)
4368 : {
4369 178443 : if (buf->buf[j])
4370 : {
4371 168410 : if (nonzero_first == wordsize)
4372 : {
4373 21519 : nonzero_first = j - i;
4374 21519 : nonzero_last = j - i;
4375 : }
4376 168410 : if (nonzero_last != j - i)
4377 166 : all_ones = false;
4378 168410 : nonzero_last = j + 1 - i;
4379 : }
4380 : else
4381 : {
4382 10033 : if (zero_first == wordsize)
4383 1946 : zero_first = j - i;
4384 10033 : zero_last = j + 1 - i;
4385 : }
4386 178443 : if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
4387 : {
4388 101 : all_ones = false;
4389 101 : bytes_only = false;
4390 : }
4391 : }
4392 22461 : size_t padding_end = i;
4393 22461 : 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 2061 : if (nonzero_first == wordsize)
4439 : /* All bits in a word are 0, there are no padding bits. */
4440 946 : continue;
4441 1115 : 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 274 : 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 158 : for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
4488 : {
4489 158 : if (nonzero_last - nonzero_first <= eltsz
4490 47 : && ((nonzero_first & ~(eltsz - 1))
4491 47 : == ((nonzero_last - 1) & ~(eltsz - 1))))
4492 : {
4493 47 : tree type;
4494 47 : if (eltsz == 1)
4495 2 : type = char_type_node;
4496 : else
4497 45 : type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
4498 : 0);
4499 47 : size_t start = nonzero_first & ~(eltsz - 1);
4500 47 : HOST_WIDE_INT off = buf->off + i + start;
4501 47 : tree atype = type;
4502 47 : if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
4503 8 : atype = build_aligned_type (type, buf->align);
4504 47 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4505 47 : build_int_cst (buf->alias_type, off));
4506 47 : tree src;
4507 47 : gimple *g;
4508 47 : if (all_ones
4509 47 : && nonzero_first == start
4510 0 : && nonzero_last == start + eltsz)
4511 0 : src = build_zero_cst (type);
4512 : else
4513 : {
4514 47 : src = make_ssa_name (type);
4515 47 : 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 47 : SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
4524 47 : suppress_warning (tmp_dst, OPT_Wuninitialized);
4525 47 : g = gimple_build_assign (src, tmp_dst);
4526 47 : gimple_set_location (g, buf->loc);
4527 47 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4528 94 : tree mask = native_interpret_expr (type,
4529 47 : buf->buf + i + start,
4530 : eltsz);
4531 47 : gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
4532 47 : mask = fold_build1 (BIT_NOT_EXPR, type, mask);
4533 47 : tree src_masked = make_ssa_name (type);
4534 47 : g = gimple_build_assign (src_masked, BIT_AND_EXPR,
4535 : src, mask);
4536 47 : gimple_set_location (g, buf->loc);
4537 47 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4538 47 : src = src_masked;
4539 : }
4540 47 : g = gimple_build_assign (dst, src);
4541 47 : gimple_set_location (g, buf->loc);
4542 47 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4543 47 : break;
4544 : }
4545 : }
4546 : }
4547 767 : if (full)
4548 : {
4549 725 : 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 725 : size_t end_rem = end % UNITS_PER_WORD;
4571 725 : buf->off += end - end_rem;
4572 725 : buf->size = end_rem;
4573 725 : memset (buf->buf, 0, buf->size);
4574 725 : 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 26529 : clear_padding_add_padding (clear_padding_struct *buf,
4589 : HOST_WIDE_INT padding_bytes)
4590 : {
4591 26529 : if (padding_bytes == 0)
4592 : return;
4593 1713 : 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 1713 : 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 1671 : memset (buf->buf + buf->size, ~0, padding_bytes);
4615 1671 : 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 38017 : clear_padding_real_needs_padding_p (tree type)
4719 : {
4720 38017 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
4721 38017 : return (fmt->b == 2
4722 37406 : && fmt->signbit_ro == fmt->signbit_rw
4723 75423 : && (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 944956 : clear_padding_type_may_have_padding_p (tree type)
4751 : {
4752 1084191 : switch (TREE_CODE (type))
4753 : {
4754 : case RECORD_TYPE:
4755 : case UNION_TYPE:
4756 : return true;
4757 139235 : case ARRAY_TYPE:
4758 139235 : case COMPLEX_TYPE:
4759 139235 : case VECTOR_TYPE:
4760 139235 : return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
4761 1810 : case REAL_TYPE:
4762 1810 : return clear_padding_real_needs_padding_p (type);
4763 63 : case ENUMERAL_TYPE:
4764 63 : 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 1201550 : type_has_padding_at_level_p (tree type)
4779 : {
4780 1201550 : switch (TREE_CODE (type))
4781 : {
4782 1059260 : case RECORD_TYPE:
4783 1059260 : {
4784 1059260 : tree bitpos = size_zero_node;
4785 : /* Expect fields to be sorted by bit position. */
4786 8635501 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4787 7581659 : if (TREE_CODE (f) == FIELD_DECL)
4788 : {
4789 2304580 : if (DECL_PADDING_P (f))
4790 : return true;
4791 2304577 : tree pos = bit_position (f);
4792 2304577 : if (simple_cst_equal (bitpos, pos) != 1)
4793 : return true;
4794 2299185 : if (!DECL_SIZE (f))
4795 : return true;
4796 2299162 : bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
4797 : }
4798 1053842 : 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 73745 : clear_padding_type (clear_padding_struct *buf, tree type,
4882 : HOST_WIDE_INT sz, bool for_auto_init)
4883 : {
4884 73745 : switch (TREE_CODE (type))
4885 : {
4886 10304 : case RECORD_TYPE:
4887 10304 : HOST_WIDE_INT cur_pos;
4888 10304 : cur_pos = 0;
4889 793667 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4890 783363 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4891 : {
4892 24977 : tree ftype = TREE_TYPE (field);
4893 24977 : if (DECL_BIT_FIELD (field))
4894 : {
4895 292 : HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
4896 292 : if (fldsz == 0)
4897 0 : continue;
4898 292 : HOST_WIDE_INT pos = int_byte_position (field);
4899 292 : if (pos >= sz)
4900 0 : continue;
4901 292 : HOST_WIDE_INT bpos
4902 292 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
4903 292 : bpos %= BITS_PER_UNIT;
4904 292 : HOST_WIDE_INT end
4905 292 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4906 292 : if (pos + end > cur_pos)
4907 : {
4908 215 : clear_padding_add_padding (buf, pos + end - cur_pos);
4909 215 : cur_pos = pos + end;
4910 : }
4911 292 : gcc_assert (cur_pos > pos
4912 : && ((unsigned HOST_WIDE_INT) buf->size
4913 : >= (unsigned HOST_WIDE_INT) cur_pos - pos));
4914 292 : unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
4915 292 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4916 : sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
4917 : " in %qs", "__builtin_clear_padding");
4918 292 : 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 292 : if (bpos + fldsz <= BITS_PER_UNIT)
4943 175 : *p &= ~(((1 << fldsz) - 1) << bpos);
4944 : else
4945 : {
4946 117 : if (bpos)
4947 : {
4948 33 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4949 33 : p++;
4950 33 : fldsz -= BITS_PER_UNIT - bpos;
4951 : }
4952 117 : memset (p, 0, fldsz / BITS_PER_UNIT);
4953 117 : p += fldsz / BITS_PER_UNIT;
4954 117 : fldsz %= BITS_PER_UNIT;
4955 117 : if (fldsz)
4956 72 : *p &= ~((1 << fldsz) - 1);
4957 : }
4958 : }
4959 : }
4960 24685 : 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 24653 : else if (is_empty_type (ftype))
4972 8994 : continue;
4973 : else
4974 : {
4975 15659 : HOST_WIDE_INT pos = int_byte_position (field);
4976 15659 : if (pos >= sz)
4977 0 : continue;
4978 15659 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4979 15659 : gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
4980 15659 : clear_padding_add_padding (buf, pos - cur_pos);
4981 15659 : cur_pos = pos;
4982 15659 : if (tree asbase = lang_hooks.types.classtype_as_base (field))
4983 300 : ftype = asbase;
4984 15659 : clear_padding_type (buf, ftype, fldsz, for_auto_init);
4985 15659 : cur_pos += fldsz;
4986 : }
4987 : }
4988 10304 : gcc_assert (sz >= cur_pos);
4989 10304 : clear_padding_add_padding (buf, sz - cur_pos);
4990 10304 : break;
4991 326 : case ARRAY_TYPE:
4992 326 : HOST_WIDE_INT nelts, fldsz;
4993 326 : fldsz = int_size_in_bytes (TREE_TYPE (type));
4994 326 : if (fldsz == 0)
4995 : break;
4996 312 : nelts = sz / fldsz;
4997 312 : if (nelts > 1
4998 305 : && sz > 8 * UNITS_PER_WORD
4999 78 : && buf->union_ptr == NULL
5000 390 : && 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 1180 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5035 934 : 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 36207 : case REAL_TYPE:
5041 36207 : gcc_assert ((size_t) sz <= clear_padding_unit);
5042 36207 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5043 0 : clear_padding_flush (buf, false);
5044 36207 : 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 34620 : memset (buf->buf + buf->size, 0, sz);
5058 36207 : buf->size += sz;
5059 36207 : 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 5180 : case VECTOR_TYPE:
5066 5180 : nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
5067 5180 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5068 23648 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5069 18468 : 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 21589 : default:
5137 21589 : gcc_assert ((size_t) sz <= clear_padding_unit);
5138 21589 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5139 0 : clear_padding_flush (buf, false);
5140 21589 : memset (buf->buf + buf->size, 0, sz);
5141 21589 : buf->size += sz;
5142 21589 : break;
5143 : }
5144 73745 : }
5145 :
5146 : /* Clear padding bits of TYPE in MASK. */
5147 :
5148 : void
5149 37665 : clear_type_padding_in_mask (tree type, unsigned char *mask)
5150 : {
5151 37665 : clear_padding_struct buf;
5152 37665 : buf.loc = UNKNOWN_LOCATION;
5153 37665 : buf.clear_in_mask = true;
5154 37665 : buf.base = NULL_TREE;
5155 37665 : buf.alias_type = NULL_TREE;
5156 37665 : buf.gsi = NULL;
5157 37665 : buf.align = 0;
5158 37665 : buf.off = 0;
5159 37665 : buf.padding_bytes = 0;
5160 37665 : buf.sz = int_size_in_bytes (type);
5161 37665 : buf.size = 0;
5162 37665 : buf.union_ptr = mask;
5163 37665 : clear_padding_type (&buf, type, buf.sz, false);
5164 37665 : clear_padding_flush (&buf, true);
5165 37665 : }
5166 :
5167 : /* Fold __builtin_clear_padding builtin. */
5168 :
5169 : static bool
5170 631 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
5171 : {
5172 631 : gimple *stmt = gsi_stmt (*gsi);
5173 631 : gcc_assert (gimple_call_num_args (stmt) == 2);
5174 631 : tree ptr = gimple_call_arg (stmt, 0);
5175 631 : 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 631 : bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
5180 631 : tree type = TREE_TYPE (TREE_TYPE (typearg));
5181 631 : location_t loc = gimple_location (stmt);
5182 631 : clear_padding_struct buf;
5183 631 : gimple_stmt_iterator gsiprev = *gsi;
5184 : /* This should be folded during the lower pass. */
5185 1262 : gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
5186 631 : gcc_assert (COMPLETE_TYPE_P (type));
5187 631 : gsi_prev (&gsiprev);
5188 :
5189 631 : buf.loc = loc;
5190 631 : buf.clear_in_mask = false;
5191 631 : buf.base = ptr;
5192 631 : buf.alias_type = NULL_TREE;
5193 631 : buf.gsi = gsi;
5194 631 : buf.align = get_pointer_alignment (ptr);
5195 631 : unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
5196 631 : buf.align = MAX (buf.align, talign);
5197 631 : buf.off = 0;
5198 631 : buf.padding_bytes = 0;
5199 631 : buf.size = 0;
5200 631 : buf.sz = int_size_in_bytes (type);
5201 631 : buf.union_ptr = NULL;
5202 631 : 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 630 : else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
5209 : sorry_at (loc, "%s not supported on this target",
5210 : "__builtin_clear_padding");
5211 630 : else if (!clear_padding_type_may_have_padding_p (type))
5212 : ;
5213 593 : 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 545 : 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 545 : buf.alias_type = build_pointer_type (type);
5249 545 : clear_padding_type (&buf, type, buf.sz, for_auto_init);
5250 545 : clear_padding_flush (&buf, true);
5251 : }
5252 :
5253 631 : gimple_stmt_iterator gsiprev2 = *gsi;
5254 631 : gsi_prev (&gsiprev2);
5255 631 : if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
5256 126 : gsi_replace (gsi, gimple_build_nop (), true);
5257 : else
5258 : {
5259 505 : gsi_remove (gsi, true);
5260 505 : *gsi = gsiprev2;
5261 : }
5262 631 : return true;
5263 : }
5264 :
5265 : /* Fold __builtin_constant_p builtin. */
5266 :
5267 : static bool
5268 176578 : gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
5269 : {
5270 176578 : gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
5271 :
5272 176578 : if (gimple_call_num_args (call) != 1)
5273 : return false;
5274 :
5275 176573 : tree arg = gimple_call_arg (call, 0);
5276 176573 : 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 351315 : if (!result && fold_before_rtl_expansion_p ())
5282 3 : result = integer_zero_node;
5283 :
5284 176573 : if (!result)
5285 : return false;
5286 :
5287 1834 : gimplify_and_update_call_from_tree (gsi, result);
5288 1834 : 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 115125 : gimple_fold_builtin_stdarg (gimple_stmt_iterator *gsi, gcall *call)
5298 : {
5299 : /* These shouldn't be folded before pass_stdarg. */
5300 115125 : if (!fold_before_rtl_expansion_p ())
5301 : return false;
5302 :
5303 10819 : tree callee, lhs, rhs, cfun_va_list;
5304 10819 : bool va_list_simple_ptr;
5305 10819 : location_t loc = gimple_location (call);
5306 10819 : gimple *nstmt0, *nstmt;
5307 10819 : tree tlhs, oldvdef, newvdef;
5308 :
5309 10819 : callee = gimple_call_fndecl (call);
5310 :
5311 10819 : cfun_va_list = targetm.fn_abi_va_list (callee);
5312 21638 : va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
5313 10819 : && (TREE_TYPE (cfun_va_list) == void_type_node
5314 436 : || TREE_TYPE (cfun_va_list) == char_type_node);
5315 :
5316 10819 : 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 3569 : case BUILT_IN_VA_END:
5396 : /* No effect, so the statement will be deleted. */
5397 3569 : 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 3569 : unlink_stmt_vdef (call);
5403 3569 : release_defs (call);
5404 3569 : gsi_replace (gsi, gimple_build_nop (), false);
5405 3569 : return true;
5406 :
5407 0 : default:
5408 0 : gcc_unreachable ();
5409 : }
5410 : }
5411 :
5412 : /* Fold __builtin_call_{code_address,static_chain} builtins. This handles
5413 : only the trivial left-over cases not processed in tree-nested.cc. */
5414 :
5415 : static bool
5416 2 : gimple_fold_builtin_call_info (gimple_stmt_iterator *gsi,
5417 : enum built_in_function fcode)
5418 : {
5419 2 : gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
5420 2 : tree arg = gimple_call_arg (stmt, 0);
5421 :
5422 : /* The error will be emitted in builtins.cc. */
5423 2 : if (TREE_CODE (arg) != ADDR_EXPR
5424 2 : || FUNCTION_DECL != TREE_CODE (TREE_OPERAND (arg, 0)))
5425 : return false;
5426 :
5427 : /* The case with static chain is handled in tree-nested.cc. */
5428 2 : gcc_assert (!DECL_STATIC_CHAIN (TREE_OPERAND (arg, 0)));
5429 :
5430 2 : if (fcode == BUILT_IN_CALL_STATIC_CHAIN)
5431 1 : replace_call_with_value (gsi, null_pointer_node);
5432 : else
5433 1 : replace_call_with_value (gsi, arg);
5434 :
5435 : return true;
5436 : }
5437 :
5438 :
5439 : /* Fold the non-target builtin at *GSI and return whether any simplification
5440 : was made. */
5441 :
5442 : static bool
5443 9577850 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
5444 : {
5445 9577850 : gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
5446 9577850 : tree callee = gimple_call_fndecl (stmt);
5447 :
5448 : /* Give up for always_inline inline builtins until they are
5449 : inlined. */
5450 9577850 : if (avoid_folding_inline_builtin (callee))
5451 : return false;
5452 :
5453 9576674 : unsigned n = gimple_call_num_args (stmt);
5454 9576674 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
5455 9576674 : switch (fcode)
5456 : {
5457 115125 : case BUILT_IN_VA_START:
5458 115125 : case BUILT_IN_VA_END:
5459 115125 : case BUILT_IN_VA_COPY:
5460 115125 : return gimple_fold_builtin_stdarg (gsi, stmt);
5461 148 : case BUILT_IN_BCMP:
5462 148 : return gimple_fold_builtin_bcmp (gsi);
5463 367 : case BUILT_IN_BCOPY:
5464 367 : return gimple_fold_builtin_bcopy (gsi);
5465 250 : case BUILT_IN_BZERO:
5466 250 : return gimple_fold_builtin_bzero (gsi);
5467 :
5468 314235 : case BUILT_IN_MEMSET:
5469 314235 : return gimple_fold_builtin_memset (gsi,
5470 : gimple_call_arg (stmt, 1),
5471 314235 : gimple_call_arg (stmt, 2));
5472 10276 : case BUILT_IN_MEMPCPY:
5473 10276 : if (gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
5474 : gimple_call_arg (stmt, 1), fcode))
5475 : return true;
5476 9536 : return gimple_fold_builtin_mempcpy (gsi);
5477 1030795 : case BUILT_IN_MEMCPY:
5478 1030795 : case BUILT_IN_MEMMOVE:
5479 1030795 : return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
5480 1030795 : gimple_call_arg (stmt, 1), fcode);
5481 4459 : case BUILT_IN_SPRINTF_CHK:
5482 4459 : case BUILT_IN_VSPRINTF_CHK:
5483 4459 : return gimple_fold_builtin_sprintf_chk (gsi, fcode);
5484 1493 : case BUILT_IN_STRCAT_CHK:
5485 1493 : return gimple_fold_builtin_strcat_chk (gsi);
5486 1116 : case BUILT_IN_STRNCAT_CHK:
5487 1116 : return gimple_fold_builtin_strncat_chk (gsi);
5488 148789 : case BUILT_IN_STRLEN:
5489 148789 : return gimple_fold_builtin_strlen (gsi);
5490 26136 : case BUILT_IN_STRCPY:
5491 26136 : return gimple_fold_builtin_strcpy (gsi,
5492 : gimple_call_arg (stmt, 0),
5493 26136 : gimple_call_arg (stmt, 1));
5494 17274 : case BUILT_IN_STRNCPY:
5495 17274 : return gimple_fold_builtin_strncpy (gsi,
5496 : gimple_call_arg (stmt, 0),
5497 : gimple_call_arg (stmt, 1),
5498 17274 : gimple_call_arg (stmt, 2));
5499 7338 : case BUILT_IN_STRCAT:
5500 7338 : return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
5501 7338 : gimple_call_arg (stmt, 1));
5502 6786 : case BUILT_IN_STRNCAT:
5503 6786 : return gimple_fold_builtin_strncat (gsi);
5504 4743 : case BUILT_IN_INDEX:
5505 4743 : case BUILT_IN_STRCHR:
5506 4743 : return gimple_fold_builtin_strchr (gsi, false);
5507 729 : case BUILT_IN_RINDEX:
5508 729 : case BUILT_IN_STRRCHR:
5509 729 : return gimple_fold_builtin_strchr (gsi, true);
5510 4384 : case BUILT_IN_STRSTR:
5511 4384 : return gimple_fold_builtin_strstr (gsi);
5512 1250714 : case BUILT_IN_STRCMP:
5513 1250714 : case BUILT_IN_STRCMP_EQ:
5514 1250714 : case BUILT_IN_STRCASECMP:
5515 1250714 : case BUILT_IN_STRNCMP:
5516 1250714 : case BUILT_IN_STRNCMP_EQ:
5517 1250714 : case BUILT_IN_STRNCASECMP:
5518 1250714 : return gimple_fold_builtin_string_compare (gsi);
5519 37487 : case BUILT_IN_MEMCHR:
5520 37487 : return gimple_fold_builtin_memchr (gsi);
5521 20689 : case BUILT_IN_FPUTS:
5522 20689 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5523 20689 : gimple_call_arg (stmt, 1), false);
5524 43 : case BUILT_IN_FPUTS_UNLOCKED:
5525 43 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5526 43 : gimple_call_arg (stmt, 1), true);
5527 25701 : case BUILT_IN_MEMCPY_CHK:
5528 25701 : case BUILT_IN_MEMPCPY_CHK:
5529 25701 : case BUILT_IN_MEMMOVE_CHK:
5530 25701 : case BUILT_IN_MEMSET_CHK:
5531 25701 : return gimple_fold_builtin_memory_chk (gsi,
5532 : gimple_call_arg (stmt, 0),
5533 : gimple_call_arg (stmt, 1),
5534 : gimple_call_arg (stmt, 2),
5535 : gimple_call_arg (stmt, 3),
5536 25701 : fcode);
5537 3674 : case BUILT_IN_STPCPY:
5538 3674 : return gimple_fold_builtin_stpcpy (gsi);
5539 2566 : case BUILT_IN_STRCPY_CHK:
5540 2566 : case BUILT_IN_STPCPY_CHK:
5541 2566 : return gimple_fold_builtin_stxcpy_chk (gsi,
5542 : gimple_call_arg (stmt, 0),
5543 : gimple_call_arg (stmt, 1),
5544 : gimple_call_arg (stmt, 2),
5545 2566 : fcode);
5546 2721 : case BUILT_IN_STRNCPY_CHK:
5547 2721 : case BUILT_IN_STPNCPY_CHK:
5548 2721 : return gimple_fold_builtin_stxncpy_chk (gsi,
5549 : gimple_call_arg (stmt, 0),
5550 : gimple_call_arg (stmt, 1),
5551 : gimple_call_arg (stmt, 2),
5552 : gimple_call_arg (stmt, 3),
5553 2721 : fcode);
5554 2359 : case BUILT_IN_SNPRINTF_CHK:
5555 2359 : case BUILT_IN_VSNPRINTF_CHK:
5556 2359 : return gimple_fold_builtin_snprintf_chk (gsi, fcode);
5557 :
5558 842737 : case BUILT_IN_FPRINTF:
5559 842737 : case BUILT_IN_FPRINTF_UNLOCKED:
5560 842737 : case BUILT_IN_VFPRINTF:
5561 842737 : if (n == 2 || n == 3)
5562 95105 : return gimple_fold_builtin_fprintf (gsi,
5563 : gimple_call_arg (stmt, 0),
5564 : gimple_call_arg (stmt, 1),
5565 : n == 3
5566 42586 : ? gimple_call_arg (stmt, 2)
5567 : : NULL_TREE,
5568 52519 : fcode);
5569 : break;
5570 2229 : case BUILT_IN_FPRINTF_CHK:
5571 2229 : case BUILT_IN_VFPRINTF_CHK:
5572 2229 : if (n == 3 || n == 4)
5573 3814 : return gimple_fold_builtin_fprintf (gsi,
5574 : gimple_call_arg (stmt, 0),
5575 : gimple_call_arg (stmt, 2),
5576 : n == 4
5577 1744 : ? gimple_call_arg (stmt, 3)
5578 : : NULL_TREE,
5579 2070 : fcode);
5580 : break;
5581 199908 : case BUILT_IN_PRINTF:
5582 199908 : case BUILT_IN_PRINTF_UNLOCKED:
5583 199908 : case BUILT_IN_VPRINTF:
5584 199908 : if (n == 1 || n == 2)
5585 234776 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
5586 : n == 2
5587 112830 : ? gimple_call_arg (stmt, 1)
5588 121946 : : NULL_TREE, fcode);
5589 : break;
5590 2273 : case BUILT_IN_PRINTF_CHK:
5591 2273 : case BUILT_IN_VPRINTF_CHK:
5592 2273 : if (n == 2 || n == 3)
5593 3893 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
5594 : n == 3
5595 1771 : ? gimple_call_arg (stmt, 2)
5596 2122 : : NULL_TREE, fcode);
5597 : break;
5598 2866 : case BUILT_IN_ACC_ON_DEVICE:
5599 2866 : return gimple_fold_builtin_acc_on_device (gsi,
5600 2866 : gimple_call_arg (stmt, 0));
5601 234 : case BUILT_IN_OMP_IS_INITIAL_DEVICE:
5602 234 : return gimple_fold_builtin_omp_is_initial_device (gsi);
5603 :
5604 103 : case BUILT_IN_OMP_GET_INITIAL_DEVICE:
5605 103 : return gimple_fold_builtin_omp_get_initial_device (gsi);
5606 :
5607 321 : case BUILT_IN_OMP_GET_NUM_DEVICES:
5608 321 : return gimple_fold_builtin_omp_get_num_devices (gsi);
5609 :
5610 49380 : case BUILT_IN_REALLOC:
5611 49380 : return gimple_fold_builtin_realloc (gsi);
5612 :
5613 631 : case BUILT_IN_CLEAR_PADDING:
5614 631 : return gimple_fold_builtin_clear_padding (gsi);
5615 :
5616 176578 : case BUILT_IN_CONSTANT_P:
5617 176578 : return gimple_fold_builtin_constant_p (gsi);
5618 :
5619 2 : case BUILT_IN_CALL_CODE_ADDRESS:
5620 2 : case BUILT_IN_CALL_STATIC_CHAIN:
5621 2 : return gimple_fold_builtin_call_info (gsi, fcode);
5622 :
5623 6127505 : default:;
5624 : }
5625 :
5626 : /* Try the generic builtin folder. */
5627 6127505 : bool ignore = (gimple_call_lhs (stmt) == NULL);
5628 6127505 : tree result = fold_call_stmt (stmt, ignore);
5629 6127505 : if (result)
5630 : {
5631 4990 : if (ignore)
5632 1235 : STRIP_NOPS (result);
5633 : else
5634 3755 : result = fold_convert (gimple_call_return_type (stmt), result);
5635 4990 : gimplify_and_update_call_from_tree (gsi, result);
5636 4990 : return true;
5637 : }
5638 :
5639 : return false;
5640 : }
5641 :
5642 : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
5643 : function calls to constants, where possible. */
5644 :
5645 : static tree
5646 20589 : fold_internal_goacc_dim (const gimple *call)
5647 : {
5648 20589 : int axis = oacc_get_ifn_dim_arg (call);
5649 20589 : int size = oacc_get_fn_dim_size (current_function_decl, axis);
5650 20589 : tree result = NULL_TREE;
5651 20589 : tree type = TREE_TYPE (gimple_call_lhs (call));
5652 :
5653 20589 : switch (gimple_call_internal_fn (call))
5654 : {
5655 8915 : case IFN_GOACC_DIM_POS:
5656 : /* If the size is 1, we know the answer. */
5657 8915 : if (size == 1)
5658 8915 : result = build_int_cst (type, 0);
5659 : break;
5660 11674 : case IFN_GOACC_DIM_SIZE:
5661 : /* If the size is not dynamic, we know the answer. */
5662 11674 : if (size)
5663 11674 : result = build_int_cst (type, size);
5664 : break;
5665 : default:
5666 : break;
5667 : }
5668 :
5669 20589 : return result;
5670 : }
5671 :
5672 : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
5673 : for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
5674 : &var where var is only addressable because of such calls. */
5675 :
5676 : bool
5677 60483637 : optimize_atomic_compare_exchange_p (gimple *stmt)
5678 : {
5679 60483637 : if (gimple_call_num_args (stmt) != 6
5680 1647320 : || !flag_inline_atomics
5681 1647320 : || !optimize
5682 1647320 : || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
5683 1647239 : || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
5684 1103953 : || !gimple_vdef (stmt)
5685 61468450 : || !gimple_vuse (stmt))
5686 59498824 : return false;
5687 :
5688 984813 : tree fndecl = gimple_call_fndecl (stmt);
5689 984813 : switch (DECL_FUNCTION_CODE (fndecl))
5690 : {
5691 51816 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
5692 51816 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
5693 51816 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
5694 51816 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
5695 51816 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
5696 51816 : break;
5697 : default:
5698 : return false;
5699 : }
5700 :
5701 51816 : tree expected = gimple_call_arg (stmt, 1);
5702 51816 : if (TREE_CODE (expected) != ADDR_EXPR
5703 51816 : || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
5704 : return false;
5705 :
5706 49435 : tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
5707 49435 : if (!is_gimple_reg_type (etype)
5708 49008 : || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
5709 46617 : || TREE_THIS_VOLATILE (etype)
5710 46617 : || VECTOR_TYPE_P (etype)
5711 : || TREE_CODE (etype) == COMPLEX_TYPE
5712 : /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
5713 : might not preserve all the bits. See PR71716. */
5714 : || SCALAR_FLOAT_TYPE_P (etype)
5715 67445 : || maybe_ne (TYPE_PRECISION (etype),
5716 36020 : GET_MODE_BITSIZE (TYPE_MODE (etype))))
5717 37833 : return false;
5718 :
5719 11602 : tree weak = gimple_call_arg (stmt, 3);
5720 11602 : if (!integer_zerop (weak) && !integer_onep (weak))
5721 : return false;
5722 :
5723 11602 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5724 11602 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5725 11602 : machine_mode mode = TYPE_MODE (itype);
5726 :
5727 11602 : if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
5728 : == CODE_FOR_nothing
5729 11602 : && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
5730 : return false;
5731 :
5732 23204 : if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
5733 : return false;
5734 :
5735 : return true;
5736 : }
5737 :
5738 : /* Fold
5739 : r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
5740 : into
5741 : _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
5742 : i = IMAGPART_EXPR <t>;
5743 : r = (_Bool) i;
5744 : e = REALPART_EXPR <t>; */
5745 :
5746 : void
5747 5726 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
5748 : {
5749 5726 : gimple *stmt = gsi_stmt (*gsi);
5750 5726 : tree fndecl = gimple_call_fndecl (stmt);
5751 5726 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5752 5726 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5753 5726 : tree ctype = build_complex_type (itype);
5754 5726 : tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
5755 5726 : bool throws = false;
5756 5726 : edge e = NULL;
5757 5726 : gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5758 : expected);
5759 5726 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5760 5726 : gimple_stmt_iterator gsiret = gsi_for_stmt (g);
5761 5726 : if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
5762 : {
5763 2644 : g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
5764 : build1 (VIEW_CONVERT_EXPR, itype,
5765 : gimple_assign_lhs (g)));
5766 2644 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5767 : }
5768 5726 : int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
5769 11069 : + int_size_in_bytes (itype);
5770 5726 : g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
5771 : gimple_call_arg (stmt, 0),
5772 : gimple_assign_lhs (g),
5773 : gimple_call_arg (stmt, 2),
5774 5726 : build_int_cst (integer_type_node, flag),
5775 : gimple_call_arg (stmt, 4),
5776 : gimple_call_arg (stmt, 5));
5777 5726 : tree lhs = make_ssa_name (ctype);
5778 5726 : gimple_call_set_lhs (g, lhs);
5779 5726 : gimple_move_vops (g, stmt);
5780 5726 : tree oldlhs = gimple_call_lhs (stmt);
5781 5726 : if (stmt_can_throw_internal (cfun, stmt))
5782 : {
5783 203 : throws = true;
5784 203 : e = find_fallthru_edge (gsi_bb (*gsi)->succs);
5785 : }
5786 5726 : gimple_call_set_nothrow (as_a <gcall *> (g),
5787 5726 : gimple_call_nothrow_p (as_a <gcall *> (stmt)));
5788 5726 : gimple_call_set_lhs (stmt, NULL_TREE);
5789 5726 : gsi_replace (gsi, g, true);
5790 5726 : if (oldlhs)
5791 : {
5792 5679 : g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
5793 : build1 (IMAGPART_EXPR, itype, lhs));
5794 5679 : if (throws)
5795 : {
5796 197 : gsi_insert_on_edge_immediate (e, g);
5797 197 : *gsi = gsi_for_stmt (g);
5798 : }
5799 : else
5800 5482 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5801 5679 : g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
5802 5679 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5803 : }
5804 5726 : g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
5805 : build1 (REALPART_EXPR, itype, lhs));
5806 5726 : if (throws && oldlhs == NULL_TREE)
5807 : {
5808 6 : gsi_insert_on_edge_immediate (e, g);
5809 6 : *gsi = gsi_for_stmt (g);
5810 : }
5811 : else
5812 5720 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5813 5726 : if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
5814 : {
5815 5288 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5816 : VIEW_CONVERT_EXPR,
5817 2644 : build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
5818 : gimple_assign_lhs (g)));
5819 2644 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5820 : }
5821 5726 : g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
5822 5726 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5823 5726 : *gsi = gsiret;
5824 5726 : }
5825 :
5826 : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
5827 : doesn't fit into TYPE. The test for overflow should be regardless of
5828 : -fwrapv, and even for unsigned types. */
5829 :
5830 : bool
5831 446701 : arith_overflowed_p (enum tree_code code, const_tree type,
5832 : const_tree arg0, const_tree arg1)
5833 : {
5834 446701 : widest2_int warg0 = widest2_int_cst (arg0);
5835 446701 : widest2_int warg1 = widest2_int_cst (arg1);
5836 446701 : widest2_int wres;
5837 446701 : switch (code)
5838 : {
5839 96290 : case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
5840 115657 : case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
5841 236149 : case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
5842 0 : default: gcc_unreachable ();
5843 : }
5844 446701 : signop sign = TYPE_SIGN (type);
5845 446701 : if (sign == UNSIGNED && wi::neg_p (wres))
5846 : return true;
5847 374785 : return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
5848 446841 : }
5849 :
5850 : /* Mask state for partial load/store operations (mask and length). */
5851 : enum mask_load_store_state {
5852 : MASK_ALL_INACTIVE, /* All lanes/elements are inactive (can be elided). */
5853 : MASK_ALL_ACTIVE, /* All lanes/elements are active (unconditional). */
5854 : MASK_UNKNOWN
5855 : };
5856 :
5857 : /* Check the mask/length state of IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL.
5858 : Returns whether all elements are active, all inactive, or mixed.
5859 : VECTYPE is the vector type of the operation. */
5860 :
5861 : static enum mask_load_store_state
5862 7991 : partial_load_store_mask_state (gcall *call, tree vectype)
5863 : {
5864 7991 : internal_fn ifn = gimple_call_internal_fn (call);
5865 7991 : int mask_index = internal_fn_mask_index (ifn);
5866 7991 : int len_index = internal_fn_len_index (ifn);
5867 :
5868 : /* Extract length and mask arguments up front. */
5869 7991 : tree len = len_index != -1 ? gimple_call_arg (call, len_index) : NULL_TREE;
5870 0 : tree bias = len ? gimple_call_arg (call, len_index + 1) : NULL_TREE;
5871 7991 : tree mask = mask_index != -1 ? gimple_call_arg (call, mask_index) : NULL_TREE;
5872 :
5873 15982 : poly_int64 nelts = GET_MODE_NUNITS (TYPE_MODE (vectype));
5874 :
5875 7991 : poly_widest_int wlen = -1;
5876 7991 : bool full_length_p = !len; /* No length means full length. */
5877 :
5878 : /* Compute effective length. */
5879 7991 : if (len && poly_int_tree_p (len))
5880 : {
5881 0 : gcc_assert (TREE_CODE (bias) == INTEGER_CST);
5882 0 : wlen = wi::to_poly_widest (len) + wi::to_widest (bias);
5883 :
5884 0 : if (known_eq (wlen, 0))
5885 : return MASK_ALL_INACTIVE;
5886 :
5887 0 : if (known_eq (wlen, nelts))
5888 : full_length_p = true;
5889 : else
5890 : full_length_p = false;
5891 : }
5892 :
5893 : /* Check mask for early return cases. */
5894 7991 : if (mask)
5895 : {
5896 7991 : if (integer_zerop (mask))
5897 : return MASK_ALL_INACTIVE;
5898 :
5899 7976 : if (full_length_p && integer_all_onesp (mask))
5900 : return MASK_ALL_ACTIVE;
5901 : }
5902 0 : else if (full_length_p)
5903 : /* No mask and full length means all active. */
5904 : return MASK_ALL_ACTIVE;
5905 :
5906 : /* For VLA vectors, we can't do much more. */
5907 7834 : if (!nelts.is_constant ())
5908 : return MASK_UNKNOWN;
5909 :
5910 : /* Same for VLS vectors with non-constant mask. */
5911 7834 : if (mask && TREE_CODE (mask) != VECTOR_CST)
5912 : return MASK_UNKNOWN;
5913 :
5914 : /* Check VLS vector elements. */
5915 480 : gcc_assert (wlen.is_constant ());
5916 :
5917 480 : HOST_WIDE_INT active_len = wlen.to_constant ().to_shwi ();
5918 480 : if (active_len == -1)
5919 480 : active_len = nelts.to_constant ();
5920 :
5921 : /* Check if all elements in the active range match the mask. */
5922 996 : for (HOST_WIDE_INT i = 0; i < active_len; i++)
5923 : {
5924 996 : bool elt_active = !mask || !integer_zerop (vector_cst_elt (mask, i));
5925 516 : if (!elt_active)
5926 : {
5927 : /* Found an inactive element. Check if all are inactive. */
5928 972 : for (HOST_WIDE_INT j = 0; j < active_len; j++)
5929 972 : if (!mask || !integer_zerop (vector_cst_elt (mask, j)))
5930 : return MASK_UNKNOWN; /* Mixed state. */
5931 : return MASK_ALL_INACTIVE;
5932 : }
5933 : }
5934 :
5935 : /* All elements in active range are active. */
5936 0 : return full_length_p ? MASK_ALL_ACTIVE : MASK_UNKNOWN;
5937 7991 : }
5938 :
5939 :
5940 : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional
5941 : (all lanes active), return a MEM_REF for the memory it references.
5942 : Otherwise return NULL_TREE. VECTYPE is the type of the memory vector. */
5943 :
5944 : static tree
5945 3988 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype)
5946 : {
5947 : /* Only fold if all lanes are active (unconditional). */
5948 3988 : if (partial_load_store_mask_state (call, vectype) != MASK_ALL_ACTIVE)
5949 : return NULL_TREE;
5950 :
5951 71 : tree ptr = gimple_call_arg (call, 0);
5952 71 : tree alias_align = gimple_call_arg (call, 1);
5953 71 : if (!tree_fits_uhwi_p (alias_align))
5954 : return NULL_TREE;
5955 :
5956 71 : unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
5957 71 : if (TYPE_ALIGN (vectype) != align)
5958 14 : vectype = build_aligned_type (vectype, align);
5959 71 : tree offset = build_zero_cst (TREE_TYPE (alias_align));
5960 71 : return fold_build2 (MEM_REF, vectype, ptr, offset);
5961 : }
5962 :
5963 : /* Try to fold IFN_{MASK,LEN}_LOAD/STORE call CALL. Return true on success. */
5964 :
5965 : static bool
5966 4003 : gimple_fold_partial_load_store (gimple_stmt_iterator *gsi, gcall *call)
5967 : {
5968 4003 : internal_fn ifn = gimple_call_internal_fn (call);
5969 4003 : tree lhs = gimple_call_lhs (call);
5970 4003 : bool is_load = (lhs != NULL_TREE);
5971 4003 : tree vectype;
5972 :
5973 4003 : if (is_load)
5974 1979 : vectype = TREE_TYPE (lhs);
5975 : else
5976 : {
5977 2024 : tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
5978 2024 : vectype = TREE_TYPE (rhs);
5979 : }
5980 :
5981 4003 : enum mask_load_store_state state
5982 4003 : = partial_load_store_mask_state (call, vectype);
5983 :
5984 : /* Handle all-inactive case. */
5985 4003 : if (state == MASK_ALL_INACTIVE)
5986 : {
5987 15 : if (is_load)
5988 : {
5989 : /* Replace load with else value. */
5990 15 : int else_index = internal_fn_else_index (ifn);
5991 15 : tree else_value = gimple_call_arg (call, else_index);
5992 15 : if (!is_gimple_reg (lhs))
5993 : {
5994 0 : if (!zerop (else_value))
5995 : return false;
5996 0 : else_value = build_constructor (TREE_TYPE (lhs), NULL);
5997 : }
5998 15 : gassign *new_stmt = gimple_build_assign (lhs, else_value);
5999 15 : gimple_set_location (new_stmt, gimple_location (call));
6000 : /* When the lhs is an array for LANES version, then there is still
6001 : a store, move the vops from the old stmt to the new one. */
6002 15 : if (!is_gimple_reg (lhs))
6003 0 : gimple_move_vops (new_stmt, call);
6004 15 : gsi_replace (gsi, new_stmt, false);
6005 15 : return true;
6006 : }
6007 : else
6008 : {
6009 : /* Remove inactive store altogether. */
6010 0 : unlink_stmt_vdef (call);
6011 0 : release_defs (call);
6012 0 : gsi_replace (gsi, gimple_build_nop (), true);
6013 0 : return true;
6014 : }
6015 : }
6016 :
6017 : /* We cannot simplify a gather/scatter or load/store lanes further. */
6018 3988 : if (internal_gather_scatter_fn_p (ifn)
6019 3988 : || TREE_CODE (vectype) == ARRAY_TYPE)
6020 : return false;
6021 :
6022 : /* Handle all-active case by folding to regular memory operation. */
6023 3988 : if (tree mem_ref = gimple_fold_partial_load_store_mem_ref (call, vectype))
6024 : {
6025 71 : gassign *new_stmt;
6026 71 : if (is_load)
6027 17 : new_stmt = gimple_build_assign (lhs, mem_ref);
6028 : else
6029 : {
6030 54 : tree rhs
6031 54 : = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
6032 54 : new_stmt = gimple_build_assign (mem_ref, rhs);
6033 : }
6034 :
6035 71 : gimple_set_location (new_stmt, gimple_location (call));
6036 71 : gimple_move_vops (new_stmt, call);
6037 71 : gsi_replace (gsi, new_stmt, false);
6038 71 : return true;
6039 : }
6040 : return false;
6041 : }
6042 :
6043 : /* Attempt to fold a call statement referenced by the statement iterator GSI.
6044 : The statement may be replaced by another statement, e.g., if the call
6045 : simplifies to a constant value. Return true if any changes were made.
6046 : It is assumed that the operands have been previously folded. */
6047 :
6048 : static bool
6049 57761045 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
6050 : {
6051 57761045 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
6052 57761045 : tree callee;
6053 57761045 : bool changed = false;
6054 :
6055 : /* Check for virtual calls that became direct calls. */
6056 57761045 : callee = gimple_call_fn (stmt);
6057 57761045 : if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
6058 : {
6059 461342 : if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
6060 : {
6061 6 : if (dump_file && virtual_method_call_p (callee)
6062 408 : && !possible_polymorphic_call_target_p
6063 6 : (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
6064 6 : (OBJ_TYPE_REF_EXPR (callee)))))
6065 : {
6066 0 : fprintf (dump_file,
6067 : "Type inheritance inconsistent devirtualization of ");
6068 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
6069 0 : fprintf (dump_file, " to ");
6070 0 : print_generic_expr (dump_file, callee, TDF_SLIM);
6071 0 : fprintf (dump_file, "\n");
6072 : }
6073 :
6074 402 : gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
6075 402 : changed = true;
6076 : }
6077 460940 : else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
6078 : {
6079 455870 : bool final;
6080 455870 : vec <cgraph_node *>targets
6081 455870 : = possible_polymorphic_call_targets (callee, stmt, &final);
6082 458534 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
6083 : {
6084 2028 : tree lhs = gimple_call_lhs (stmt);
6085 2028 : if (dump_enabled_p ())
6086 : {
6087 34 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
6088 : "folding virtual function call to %s\n",
6089 34 : targets.length () == 1
6090 17 : ? targets[0]->name ()
6091 : : "__builtin_unreachable");
6092 : }
6093 2028 : if (targets.length () == 1)
6094 : {
6095 1989 : tree fndecl = targets[0]->decl;
6096 1989 : gimple_call_set_fndecl (stmt, fndecl);
6097 1989 : changed = true;
6098 : /* If changing the call to __cxa_pure_virtual
6099 : or similar noreturn function, adjust gimple_call_fntype
6100 : too. */
6101 1989 : if (gimple_call_noreturn_p (stmt)
6102 19 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
6103 13 : && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
6104 2002 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6105 13 : == void_type_node))
6106 13 : gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
6107 : /* If the call becomes noreturn, remove the lhs. */
6108 1989 : if (lhs
6109 1668 : && gimple_call_noreturn_p (stmt)
6110 2004 : && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
6111 6 : || should_remove_lhs_p (lhs)))
6112 : {
6113 12 : if (TREE_CODE (lhs) == SSA_NAME)
6114 : {
6115 0 : tree var = create_tmp_var (TREE_TYPE (lhs));
6116 0 : tree def = get_or_create_ssa_default_def (cfun, var);
6117 0 : gimple *new_stmt = gimple_build_assign (lhs, def);
6118 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
6119 : }
6120 12 : gimple_call_set_lhs (stmt, NULL_TREE);
6121 : }
6122 1989 : maybe_remove_unused_call_args (cfun, stmt);
6123 : }
6124 : else
6125 : {
6126 39 : location_t loc = gimple_location (stmt);
6127 39 : gimple *new_stmt = gimple_build_builtin_unreachable (loc);
6128 39 : gimple_call_set_ctrl_altering (new_stmt, false);
6129 : /* If the call had a SSA name as lhs morph that into
6130 : an uninitialized value. */
6131 39 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6132 : {
6133 12 : tree var = create_tmp_var (TREE_TYPE (lhs));
6134 12 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
6135 12 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
6136 12 : set_ssa_default_def (cfun, var, lhs);
6137 : }
6138 39 : gimple_move_vops (new_stmt, stmt);
6139 39 : gsi_replace (gsi, new_stmt, false);
6140 39 : return true;
6141 : }
6142 : }
6143 : }
6144 : }
6145 :
6146 : /* Check for indirect calls that became direct calls, and then
6147 : no longer require a static chain. */
6148 57761006 : if (gimple_call_chain (stmt))
6149 : {
6150 246566 : tree fn = gimple_call_fndecl (stmt);
6151 295600 : if (fn && !DECL_STATIC_CHAIN (fn))
6152 : {
6153 2054 : gimple_call_set_chain (stmt, NULL);
6154 2054 : changed = true;
6155 : }
6156 : }
6157 :
6158 57761006 : if (inplace)
6159 : return changed;
6160 :
6161 : /* Don't constant fold functions which can change the control. */
6162 57757860 : if (gimple_call_ctrl_altering_p (stmt))
6163 : return changed;
6164 :
6165 : /* Check for builtins that CCP can handle using information not
6166 : available in the generic fold routines. */
6167 49846704 : if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
6168 : {
6169 9577850 : if (gimple_fold_builtin (gsi))
6170 210012 : changed = true;
6171 : }
6172 40268854 : else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
6173 : {
6174 1153759 : changed |= targetm.gimple_fold_builtin (gsi);
6175 : }
6176 39115095 : else if (gimple_call_internal_p (stmt))
6177 : {
6178 1940370 : enum tree_code subcode = ERROR_MARK;
6179 1940370 : tree result = NULL_TREE;
6180 1940370 : bool cplx_result = false;
6181 1940370 : bool uaddc_usubc = false;
6182 1940370 : tree overflow = NULL_TREE;
6183 1940370 : switch (gimple_call_internal_fn (stmt))
6184 : {
6185 852 : case IFN_ASSUME:
6186 : /* Remove .ASSUME calls during the last fold since it is no
6187 : longer needed. */
6188 852 : if (fold_before_rtl_expansion_p ())
6189 116 : replace_call_with_value (gsi, NULL_TREE);
6190 : break;
6191 163277 : case IFN_BUILTIN_EXPECT:
6192 163277 : result = fold_builtin_expect (gimple_location (stmt),
6193 : gimple_call_arg (stmt, 0),
6194 : gimple_call_arg (stmt, 1),
6195 : gimple_call_arg (stmt, 2),
6196 : NULL_TREE);
6197 163277 : break;
6198 8884 : case IFN_UBSAN_OBJECT_SIZE:
6199 8884 : {
6200 8884 : tree offset = gimple_call_arg (stmt, 1);
6201 8884 : tree objsize = gimple_call_arg (stmt, 2);
6202 8884 : if (integer_all_onesp (objsize)
6203 8884 : || (TREE_CODE (offset) == INTEGER_CST
6204 4825 : && TREE_CODE (objsize) == INTEGER_CST
6205 1126 : && tree_int_cst_le (offset, objsize)))
6206 : {
6207 1539 : replace_call_with_value (gsi, NULL_TREE);
6208 1539 : return true;
6209 : }
6210 : }
6211 : break;
6212 11656 : case IFN_UBSAN_PTR:
6213 11656 : if (integer_zerop (gimple_call_arg (stmt, 1)))
6214 : {
6215 30 : replace_call_with_value (gsi, NULL_TREE);
6216 30 : return true;
6217 : }
6218 : break;
6219 8493 : case IFN_UBSAN_BOUNDS:
6220 8493 : {
6221 8493 : tree index = gimple_call_arg (stmt, 1);
6222 8493 : tree bound = gimple_call_arg (stmt, 2);
6223 8493 : if (TREE_CODE (index) == INTEGER_CST
6224 5471 : && TREE_CODE (bound) == INTEGER_CST)
6225 : {
6226 4340 : index = fold_convert (TREE_TYPE (bound), index);
6227 4340 : if (TREE_CODE (index) == INTEGER_CST
6228 4340 : && tree_int_cst_lt (index, bound))
6229 : {
6230 286 : replace_call_with_value (gsi, NULL_TREE);
6231 286 : return true;
6232 : }
6233 : }
6234 : }
6235 : break;
6236 20589 : case IFN_GOACC_DIM_SIZE:
6237 20589 : case IFN_GOACC_DIM_POS:
6238 20589 : result = fold_internal_goacc_dim (stmt);
6239 20589 : break;
6240 : case IFN_UBSAN_CHECK_ADD:
6241 : subcode = PLUS_EXPR;
6242 : break;
6243 : case IFN_UBSAN_CHECK_SUB:
6244 : subcode = MINUS_EXPR;
6245 : break;
6246 : case IFN_UBSAN_CHECK_MUL:
6247 : subcode = MULT_EXPR;
6248 : break;
6249 : case IFN_ADD_OVERFLOW:
6250 : subcode = PLUS_EXPR;
6251 : cplx_result = true;
6252 : break;
6253 : case IFN_SUB_OVERFLOW:
6254 : subcode = MINUS_EXPR;
6255 : cplx_result = true;
6256 : break;
6257 : case IFN_MUL_OVERFLOW:
6258 : subcode = MULT_EXPR;
6259 : cplx_result = true;
6260 : break;
6261 : case IFN_UADDC:
6262 : subcode = PLUS_EXPR;
6263 : cplx_result = true;
6264 : uaddc_usubc = true;
6265 : break;
6266 : case IFN_USUBC:
6267 : subcode = MINUS_EXPR;
6268 : cplx_result = true;
6269 : uaddc_usubc = true;
6270 : break;
6271 4003 : case IFN_LEN_LOAD:
6272 4003 : case IFN_MASK_LOAD:
6273 4003 : case IFN_MASK_LEN_LOAD:
6274 4003 : case IFN_MASK_GATHER_LOAD:
6275 4003 : case IFN_MASK_LEN_GATHER_LOAD:
6276 4003 : case IFN_MASK_LOAD_LANES:
6277 4003 : case IFN_MASK_LEN_LOAD_LANES:
6278 4003 : case IFN_LEN_STORE:
6279 4003 : case IFN_MASK_STORE:
6280 4003 : case IFN_MASK_LEN_STORE:
6281 4003 : case IFN_MASK_SCATTER_STORE:
6282 4003 : case IFN_MASK_LEN_SCATTER_STORE:
6283 4003 : case IFN_MASK_STORE_LANES:
6284 4003 : case IFN_MASK_LEN_STORE_LANES:
6285 4003 : changed |= gimple_fold_partial_load_store (gsi, stmt);
6286 4003 : break;
6287 : default:
6288 : break;
6289 : }
6290 187985 : if (subcode != ERROR_MARK)
6291 : {
6292 494650 : tree arg0 = gimple_call_arg (stmt, 0);
6293 494650 : tree arg1 = gimple_call_arg (stmt, 1);
6294 494650 : tree arg2 = NULL_TREE;
6295 494650 : tree type = TREE_TYPE (arg0);
6296 494650 : if (cplx_result)
6297 : {
6298 475585 : tree lhs = gimple_call_lhs (stmt);
6299 475585 : if (lhs == NULL_TREE)
6300 : type = NULL_TREE;
6301 : else
6302 475585 : type = TREE_TYPE (TREE_TYPE (lhs));
6303 475585 : if (uaddc_usubc)
6304 31838 : arg2 = gimple_call_arg (stmt, 2);
6305 : }
6306 494650 : if (type == NULL_TREE)
6307 : ;
6308 494650 : else if (uaddc_usubc)
6309 : {
6310 31838 : if (!integer_zerop (arg2))
6311 : ;
6312 : /* x = y + 0 + 0; x = y - 0 - 0; */
6313 4825 : else if (integer_zerop (arg1))
6314 : result = arg0;
6315 : /* x = 0 + y + 0; */
6316 4201 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
6317 : result = arg1;
6318 : /* x = y - y - 0; */
6319 4201 : else if (subcode == MINUS_EXPR
6320 4201 : && operand_equal_p (arg0, arg1, 0))
6321 0 : result = integer_zero_node;
6322 : }
6323 : /* x = y + 0; x = y - 0; x = y * 0; */
6324 462812 : else if (integer_zerop (arg1))
6325 10107 : result = subcode == MULT_EXPR ? integer_zero_node : arg0;
6326 : /* x = 0 + y; x = 0 * y; */
6327 452705 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
6328 0 : result = subcode == MULT_EXPR ? integer_zero_node : arg1;
6329 : /* x = y - y; */
6330 452705 : else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
6331 13 : result = integer_zero_node;
6332 : /* x = y * 1; x = 1 * y; */
6333 452692 : else if (subcode == MULT_EXPR && integer_onep (arg1))
6334 : result = arg0;
6335 447367 : else if (subcode == MULT_EXPR && integer_onep (arg0))
6336 : result = arg1;
6337 494650 : if (result)
6338 : {
6339 16069 : if (result == integer_zero_node)
6340 2146 : result = build_zero_cst (type);
6341 13923 : else if (cplx_result && TREE_TYPE (result) != type)
6342 : {
6343 9638 : if (TREE_CODE (result) == INTEGER_CST)
6344 : {
6345 0 : if (arith_overflowed_p (PLUS_EXPR, type, result,
6346 : integer_zero_node))
6347 0 : overflow = build_one_cst (type);
6348 : }
6349 9638 : else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
6350 6920 : && TYPE_UNSIGNED (type))
6351 9783 : || (TYPE_PRECISION (type)
6352 2863 : < (TYPE_PRECISION (TREE_TYPE (result))
6353 2863 : + (TYPE_UNSIGNED (TREE_TYPE (result))
6354 3219 : && !TYPE_UNSIGNED (type)))))
6355 : result = NULL_TREE;
6356 62 : if (result)
6357 62 : result = fold_convert (type, result);
6358 : }
6359 : }
6360 : }
6361 :
6362 1450358 : if (result)
6363 : {
6364 28988 : if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
6365 0 : result = drop_tree_overflow (result);
6366 28988 : if (cplx_result)
6367 : {
6368 6482 : if (overflow == NULL_TREE)
6369 6482 : overflow = build_zero_cst (TREE_TYPE (result));
6370 6482 : tree ctype = build_complex_type (TREE_TYPE (result));
6371 6482 : if (TREE_CODE (result) == INTEGER_CST
6372 2146 : && TREE_CODE (overflow) == INTEGER_CST)
6373 2146 : result = build_complex (ctype, result, overflow);
6374 : else
6375 4336 : result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
6376 : ctype, result, overflow);
6377 : }
6378 28988 : gimplify_and_update_call_from_tree (gsi, result);
6379 28988 : changed = true;
6380 : }
6381 : }
6382 :
6383 : return changed;
6384 : }
6385 :
6386 :
6387 : /* Return true whether NAME has a use on STMT. Note this can return
6388 : false even though there's a use on STMT if SSA operands are not
6389 : up-to-date. */
6390 :
6391 : static bool
6392 1674 : has_use_on_stmt (tree name, gimple *stmt)
6393 : {
6394 1674 : ssa_op_iter iter;
6395 1674 : tree op;
6396 3373 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6397 1745 : if (op == name)
6398 : return true;
6399 : return false;
6400 : }
6401 :
6402 : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
6403 :
6404 : void
6405 4828588 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
6406 : {
6407 4828588 : if (!dce_worklist)
6408 : return;
6409 :
6410 1655918 : for (gimple_stmt_iterator i = gsi_start (seq);
6411 1934795 : !gsi_end_p (i); gsi_next (&i))
6412 : {
6413 278877 : gimple *stmt = gsi_stmt (i);
6414 278877 : tree name = gimple_get_lhs (stmt);
6415 278877 : if (name && TREE_CODE (name) == SSA_NAME)
6416 278877 : bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
6417 : }
6418 : }
6419 :
6420 : /* Worker for fold_stmt_1 dispatch to pattern based folding with
6421 : gimple_simplify.
6422 :
6423 : Replaces *GSI with the simplification result in RCODE and OPS
6424 : and the associated statements in *SEQ. Does the replacement
6425 : according to INPLACE and returns true if the operation succeeded. */
6426 :
6427 : static bool
6428 9320855 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
6429 : gimple_match_op *res_op,
6430 : gimple_seq *seq, bool inplace,
6431 : bitmap dce_worklist)
6432 : {
6433 9320855 : gimple *stmt = gsi_stmt (*gsi);
6434 9320855 : tree *ops = res_op->ops;
6435 9320855 : unsigned int num_ops = res_op->num_ops;
6436 :
6437 : /* Play safe and do not allow abnormals to be mentioned in
6438 : newly created statements. See also maybe_push_res_to_seq.
6439 : As an exception allow such uses if there was a use of the
6440 : same SSA name on the old stmt. */
6441 20658059 : for (unsigned int i = 0; i < num_ops; ++i)
6442 11338832 : if (TREE_CODE (ops[i]) == SSA_NAME
6443 6885948 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
6444 11340506 : && !has_use_on_stmt (ops[i], stmt))
6445 : return false;
6446 :
6447 9319227 : if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
6448 0 : for (unsigned int i = 0; i < 2; ++i)
6449 0 : if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
6450 0 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
6451 0 : && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
6452 : return false;
6453 :
6454 : /* Don't insert new statements when INPLACE is true, even if we could
6455 : reuse STMT for the final statement. */
6456 9319227 : if (inplace && !gimple_seq_empty_p (*seq))
6457 : return false;
6458 :
6459 9319227 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6460 : {
6461 7016349 : gcc_assert (res_op->code.is_tree_code ());
6462 7016349 : auto code = tree_code (res_op->code);
6463 7016349 : if (TREE_CODE_CLASS (code) == tcc_comparison
6464 : /* GIMPLE_CONDs condition may not throw. */
6465 7016349 : && ((cfun
6466 1164493 : && (!flag_exceptions
6467 790724 : || !cfun->can_throw_non_call_exceptions))
6468 292045 : || !operation_could_trap_p (code,
6469 292045 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6470 : false, NULL_TREE)))
6471 1153216 : gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
6472 5863133 : else if (code == SSA_NAME)
6473 : {
6474 : /* If setting the gimple cond to the same thing,
6475 : return false as nothing changed. */
6476 4056648 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6477 4032247 : && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
6478 8086350 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6479 : return false;
6480 26946 : gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
6481 26946 : build_zero_cst (TREE_TYPE (ops[0])));
6482 : }
6483 1806485 : else if (code == INTEGER_CST)
6484 : {
6485 : /* Make into the canonical form `1 != 0` and `0 != 0`.
6486 : If already in the canonical form return false
6487 : saying nothing has been done. */
6488 1258585 : if (integer_zerop (ops[0]))
6489 : {
6490 5454684 : if (gimple_cond_false_canonical_p (cond_stmt))
6491 : return false;
6492 514178 : gimple_cond_make_false (cond_stmt);
6493 : }
6494 : else
6495 : {
6496 383909 : if (gimple_cond_true_canonical_p (cond_stmt))
6497 : return false;
6498 207989 : gimple_cond_make_true (cond_stmt);
6499 : }
6500 : }
6501 547900 : else if (!inplace)
6502 : {
6503 : /* For throwing comparisons, see if the GIMPLE_COND is the same as
6504 : the comparison would be.
6505 : This can happen due to the match pattern for
6506 : `(ne (cmp @0 @1) integer_zerop)` which creates a new expression
6507 : for the comparison. */
6508 547900 : if (TREE_CODE_CLASS (code) == tcc_comparison
6509 11277 : && (!cfun
6510 11277 : || (flag_exceptions
6511 11277 : && cfun->can_throw_non_call_exceptions))
6512 559177 : && operation_could_trap_p (code,
6513 11277 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6514 : false, NULL_TREE))
6515 : {
6516 11277 : tree lhs = gimple_cond_lhs (cond_stmt);
6517 11277 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6518 11277 : && TREE_CODE (lhs) == SSA_NAME
6519 11277 : && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6520 22554 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6521 : {
6522 11277 : gimple *s = SSA_NAME_DEF_STMT (lhs);
6523 11277 : if (is_gimple_assign (s)
6524 11277 : && gimple_assign_rhs_code (s) == code
6525 11277 : && operand_equal_p (gimple_assign_rhs1 (s), ops[0])
6526 22554 : && operand_equal_p (gimple_assign_rhs2 (s), ops[1]))
6527 : return false;
6528 : }
6529 : }
6530 536623 : tree res = maybe_push_res_to_seq (res_op, seq);
6531 536623 : if (!res)
6532 : return false;
6533 536623 : gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
6534 536623 : build_zero_cst (TREE_TYPE (res)));
6535 : }
6536 : else
6537 : return false;
6538 2438952 : if (dump_file && (dump_flags & TDF_DETAILS))
6539 : {
6540 857 : fprintf (dump_file, "gimple_simplified to ");
6541 857 : if (!gimple_seq_empty_p (*seq))
6542 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6543 857 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6544 : 0, TDF_SLIM);
6545 : }
6546 : // Mark the lhs of the new statements maybe for dce
6547 2438952 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6548 2438952 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6549 2438952 : return true;
6550 : }
6551 2302878 : else if (is_gimple_assign (stmt)
6552 2302878 : && res_op->code.is_tree_code ())
6553 : {
6554 2222114 : auto code = tree_code (res_op->code);
6555 2222114 : if (!inplace
6556 2222114 : || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
6557 : {
6558 2222114 : maybe_build_generic_op (res_op);
6559 5239749 : gimple_assign_set_rhs_with_ops (gsi, code,
6560 : res_op->op_or_null (0),
6561 : res_op->op_or_null (1),
6562 : res_op->op_or_null (2));
6563 2222114 : if (dump_file && (dump_flags & TDF_DETAILS))
6564 : {
6565 16858 : fprintf (dump_file, "gimple_simplified to ");
6566 16858 : if (!gimple_seq_empty_p (*seq))
6567 327 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6568 16858 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6569 : 0, TDF_SLIM);
6570 : }
6571 : // Mark the lhs of the new statements maybe for dce
6572 2222114 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6573 2222114 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6574 2222114 : return true;
6575 : }
6576 : }
6577 80764 : else if (res_op->code.is_fn_code ()
6578 80764 : && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
6579 : {
6580 8072 : gcc_assert (num_ops == gimple_call_num_args (stmt));
6581 23890 : for (unsigned int i = 0; i < num_ops; ++i)
6582 15818 : gimple_call_set_arg (stmt, i, ops[i]);
6583 8072 : if (dump_file && (dump_flags & TDF_DETAILS))
6584 : {
6585 0 : fprintf (dump_file, "gimple_simplified to ");
6586 0 : if (!gimple_seq_empty_p (*seq))
6587 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6588 0 : print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
6589 : }
6590 : // Mark the lhs of the new statements maybe for dce
6591 8072 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6592 8072 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6593 8072 : return true;
6594 : }
6595 72692 : else if (!inplace)
6596 : {
6597 143381 : if (gimple_has_lhs (stmt))
6598 : {
6599 72692 : tree lhs = gimple_get_lhs (stmt);
6600 72692 : if (!maybe_push_res_to_seq (res_op, seq, lhs))
6601 : return false;
6602 71709 : if (dump_file && (dump_flags & TDF_DETAILS))
6603 : {
6604 10 : fprintf (dump_file, "gimple_simplified to ");
6605 10 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6606 : }
6607 : // Mark the lhs of the new statements maybe for dce
6608 71709 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6609 71709 : gsi_replace_with_seq_vops (gsi, *seq);
6610 71709 : return true;
6611 : }
6612 : else
6613 0 : gcc_unreachable ();
6614 : }
6615 :
6616 : return false;
6617 : }
6618 :
6619 : /* Canonicalize MEM_REFs invariant address operand after propagation. */
6620 :
6621 : static bool
6622 197645983 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
6623 : {
6624 197645983 : bool res = false;
6625 197645983 : tree *orig_t = t;
6626 :
6627 197645983 : if (TREE_CODE (*t) == ADDR_EXPR)
6628 67304665 : t = &TREE_OPERAND (*t, 0);
6629 :
6630 : /* The C and C++ frontends use an ARRAY_REF for indexing with their
6631 : generic vector extension. The actual vector referenced is
6632 : view-converted to an array type for this purpose. If the index
6633 : is constant the canonical representation in the middle-end is a
6634 : BIT_FIELD_REF so re-write the former to the latter here. */
6635 197645983 : if (TREE_CODE (*t) == ARRAY_REF
6636 11006498 : && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
6637 173835 : && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
6638 197699439 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
6639 : {
6640 24175 : tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
6641 24175 : if (VECTOR_TYPE_P (vtype))
6642 : {
6643 24175 : tree low = array_ref_low_bound (*t);
6644 24175 : if (TREE_CODE (low) == INTEGER_CST)
6645 : {
6646 24175 : if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
6647 : {
6648 48306 : widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
6649 48306 : wi::to_widest (low));
6650 24153 : idx = wi::mul (idx, wi::to_widest
6651 48306 : (TYPE_SIZE (TREE_TYPE (*t))));
6652 24153 : widest_int ext
6653 24153 : = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
6654 24153 : if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
6655 : {
6656 47154 : *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
6657 23577 : TREE_TYPE (*t),
6658 23577 : TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
6659 23577 : TYPE_SIZE (TREE_TYPE (*t)),
6660 23577 : wide_int_to_tree (bitsizetype, idx));
6661 23577 : res = true;
6662 : }
6663 24153 : }
6664 : }
6665 : }
6666 : }
6667 :
6668 372131791 : while (handled_component_p (*t))
6669 174485808 : t = &TREE_OPERAND (*t, 0);
6670 :
6671 : /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
6672 : of invariant addresses into a SSA name MEM_REF address. */
6673 197645983 : if (TREE_CODE (*t) == MEM_REF
6674 197645983 : || TREE_CODE (*t) == TARGET_MEM_REF)
6675 : {
6676 103897575 : tree addr = TREE_OPERAND (*t, 0);
6677 103897575 : if (TREE_CODE (addr) == ADDR_EXPR
6678 103897575 : && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
6679 31603607 : || handled_component_p (TREE_OPERAND (addr, 0))))
6680 : {
6681 553581 : tree base;
6682 553581 : poly_int64 coffset;
6683 553581 : base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
6684 : &coffset);
6685 553581 : if (!base)
6686 : {
6687 18 : if (is_debug)
6688 18 : return false;
6689 0 : gcc_unreachable ();
6690 : }
6691 :
6692 553563 : TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
6693 553563 : TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
6694 553563 : TREE_OPERAND (*t, 1),
6695 553563 : size_int (coffset));
6696 553563 : res = true;
6697 : }
6698 103897557 : gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
6699 : || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
6700 : }
6701 :
6702 : /* Canonicalize back MEM_REFs to plain reference trees if the object
6703 : accessed is a decl that has the same access semantics as the MEM_REF. */
6704 197645965 : if (TREE_CODE (*t) == MEM_REF
6705 101919557 : && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
6706 31293265 : && integer_zerop (TREE_OPERAND (*t, 1))
6707 214669824 : && MR_DEPENDENCE_CLIQUE (*t) == 0)
6708 : {
6709 12042412 : tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6710 12042412 : tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
6711 12042412 : if (/* Same volatile qualification. */
6712 12042412 : TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
6713 : /* Same TBAA behavior with -fstrict-aliasing. */
6714 12039392 : && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
6715 11691692 : && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
6716 11691692 : == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
6717 : /* Same alignment. */
6718 4851491 : && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
6719 : /* We have to look out here to not drop a required conversion
6720 : from the rhs to the lhs if *t appears on the lhs or vice-versa
6721 : if it appears on the rhs. Thus require strict type
6722 : compatibility. */
6723 16607374 : && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
6724 : {
6725 2528692 : *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6726 2528692 : res = true;
6727 : }
6728 : }
6729 :
6730 185603553 : else if (TREE_CODE (*orig_t) == ADDR_EXPR
6731 64541474 : && TREE_CODE (*t) == MEM_REF
6732 207711949 : && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
6733 : {
6734 854 : tree base;
6735 854 : poly_int64 coffset;
6736 854 : base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
6737 : &coffset);
6738 854 : if (base)
6739 : {
6740 760 : gcc_assert (TREE_CODE (base) == MEM_REF);
6741 760 : poly_int64 moffset;
6742 760 : if (mem_ref_offset (base).to_shwi (&moffset))
6743 : {
6744 760 : coffset += moffset;
6745 760 : if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
6746 : {
6747 760 : coffset += moffset;
6748 760 : *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
6749 760 : return true;
6750 : }
6751 : }
6752 : }
6753 : }
6754 :
6755 : /* Canonicalize TARGET_MEM_REF in particular with respect to
6756 : the indexes becoming constant. */
6757 185602699 : else if (TREE_CODE (*t) == TARGET_MEM_REF)
6758 : {
6759 1978000 : tree tem = maybe_fold_tmr (*t);
6760 1978000 : if (tem)
6761 : {
6762 1605 : *t = tem;
6763 1605 : if (TREE_CODE (*orig_t) == ADDR_EXPR)
6764 0 : recompute_tree_invariant_for_addr_expr (*orig_t);
6765 : res = true;
6766 : }
6767 : }
6768 :
6769 : return res;
6770 : }
6771 :
6772 : /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
6773 : distinguishes both cases. */
6774 :
6775 : static bool
6776 797760378 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
6777 : bitmap dce_worklist = nullptr)
6778 : {
6779 797760378 : bool changed = false;
6780 797760378 : gimple *stmt = gsi_stmt (*gsi);
6781 797760378 : unsigned i;
6782 :
6783 : /* First do required canonicalization of [TARGET_]MEM_REF addresses
6784 : after propagation.
6785 : ??? This shouldn't be done in generic folding but in the
6786 : propagation helpers which also know whether an address was
6787 : propagated.
6788 : Also canonicalize operand order. */
6789 797760378 : switch (gimple_code (stmt))
6790 : {
6791 261173540 : case GIMPLE_ASSIGN:
6792 261173540 : if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
6793 : {
6794 172422005 : tree *rhs = gimple_assign_rhs1_ptr (stmt);
6795 172422005 : if ((REFERENCE_CLASS_P (*rhs)
6796 108977910 : || TREE_CODE (*rhs) == ADDR_EXPR)
6797 187713657 : && maybe_canonicalize_mem_ref_addr (rhs))
6798 : changed = true;
6799 172422005 : tree *lhs = gimple_assign_lhs_ptr (stmt);
6800 172422005 : if (REFERENCE_CLASS_P (*lhs)
6801 172422005 : && maybe_canonicalize_mem_ref_addr (lhs))
6802 : changed = true;
6803 : /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
6804 : This cannot be done in maybe_canonicalize_mem_ref_addr
6805 : as the gimple now has two operands rather than one.
6806 : The same reason why this can't be done in
6807 : maybe_canonicalize_mem_ref_addr is the same reason why
6808 : this can't be done inplace. */
6809 172422005 : if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
6810 : {
6811 15086048 : tree inner = TREE_OPERAND (*rhs, 0);
6812 15086048 : if (TREE_CODE (inner) == MEM_REF
6813 1055279 : && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
6814 15150189 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6815 : {
6816 64141 : tree ptr = TREE_OPERAND (inner, 0);
6817 64141 : tree addon = TREE_OPERAND (inner, 1);
6818 64141 : addon = fold_convert (sizetype, addon);
6819 64141 : gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
6820 : ptr, addon);
6821 64141 : changed = true;
6822 64141 : stmt = gsi_stmt (*gsi);
6823 : }
6824 : }
6825 : }
6826 : else
6827 : {
6828 : /* Canonicalize operand order. */
6829 88751535 : enum tree_code code = gimple_assign_rhs_code (stmt);
6830 88751535 : if (TREE_CODE_CLASS (code) == tcc_comparison
6831 82408917 : || commutative_tree_code (code)
6832 133385591 : || commutative_ternary_tree_code (code))
6833 : {
6834 44118463 : tree rhs1 = gimple_assign_rhs1 (stmt);
6835 44118463 : tree rhs2 = gimple_assign_rhs2 (stmt);
6836 44118463 : if (tree_swap_operands_p (rhs1, rhs2))
6837 : {
6838 2632814 : gimple_assign_set_rhs1 (stmt, rhs2);
6839 2632814 : gimple_assign_set_rhs2 (stmt, rhs1);
6840 2632814 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6841 320703 : gimple_assign_set_rhs_code (stmt,
6842 : swap_tree_comparison (code));
6843 : changed = true;
6844 : }
6845 : }
6846 : }
6847 : break;
6848 57819849 : case GIMPLE_CALL:
6849 57819849 : {
6850 57819849 : gcall *call = as_a<gcall *> (stmt);
6851 173413555 : for (i = 0; i < gimple_call_num_args (call); ++i)
6852 : {
6853 115593706 : tree *arg = gimple_call_arg_ptr (call, i);
6854 115593706 : if (REFERENCE_CLASS_P (*arg)
6855 115593706 : && maybe_canonicalize_mem_ref_addr (arg))
6856 : changed = true;
6857 : }
6858 57819849 : tree *lhs = gimple_call_lhs_ptr (call);
6859 57819849 : if (*lhs
6860 23387651 : && REFERENCE_CLASS_P (*lhs)
6861 57940057 : && maybe_canonicalize_mem_ref_addr (lhs))
6862 : changed = true;
6863 57819849 : if (*lhs)
6864 : {
6865 23387651 : combined_fn cfn = gimple_call_combined_fn (call);
6866 23387651 : internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
6867 23387651 : int opno = first_commutative_argument (ifn);
6868 23387651 : if (opno >= 0)
6869 : {
6870 355729 : tree arg1 = gimple_call_arg (call, opno);
6871 355729 : tree arg2 = gimple_call_arg (call, opno + 1);
6872 355729 : if (tree_swap_operands_p (arg1, arg2))
6873 : {
6874 22424 : gimple_call_set_arg (call, opno, arg2);
6875 22424 : gimple_call_set_arg (call, opno + 1, arg1);
6876 22424 : changed = true;
6877 : }
6878 : }
6879 : }
6880 : break;
6881 : }
6882 600864 : case GIMPLE_ASM:
6883 600864 : {
6884 600864 : gasm *asm_stmt = as_a <gasm *> (stmt);
6885 1278581 : for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
6886 : {
6887 677717 : tree link = gimple_asm_output_op (asm_stmt, i);
6888 677717 : tree op = TREE_VALUE (link);
6889 677717 : if (REFERENCE_CLASS_P (op)
6890 677717 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6891 : changed = true;
6892 : }
6893 1031023 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
6894 : {
6895 430159 : tree link = gimple_asm_input_op (asm_stmt, i);
6896 430159 : tree op = TREE_VALUE (link);
6897 430159 : if ((REFERENCE_CLASS_P (op)
6898 401916 : || TREE_CODE (op) == ADDR_EXPR)
6899 465659 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6900 : changed = true;
6901 : }
6902 : }
6903 : break;
6904 411458731 : case GIMPLE_DEBUG:
6905 411458731 : if (gimple_debug_bind_p (stmt))
6906 : {
6907 317029962 : tree *val = gimple_debug_bind_get_value_ptr (stmt);
6908 317029962 : if (*val
6909 180090172 : && (REFERENCE_CLASS_P (*val)
6910 177408192 : || TREE_CODE (*val) == ADDR_EXPR)
6911 371689455 : && maybe_canonicalize_mem_ref_addr (val, true))
6912 : changed = true;
6913 : }
6914 : break;
6915 45885920 : case GIMPLE_COND:
6916 45885920 : {
6917 : /* Canonicalize operand order. */
6918 45885920 : tree lhs = gimple_cond_lhs (stmt);
6919 45885920 : tree rhs = gimple_cond_rhs (stmt);
6920 45885920 : if (tree_swap_operands_p (lhs, rhs))
6921 : {
6922 1433497 : gcond *gc = as_a <gcond *> (stmt);
6923 1433497 : gimple_cond_set_lhs (gc, rhs);
6924 1433497 : gimple_cond_set_rhs (gc, lhs);
6925 1433497 : gimple_cond_set_code (gc,
6926 : swap_tree_comparison (gimple_cond_code (gc)));
6927 1433497 : changed = true;
6928 : }
6929 : }
6930 796209897 : default:;
6931 : }
6932 :
6933 : /* Dispatch to pattern-based folding. */
6934 796209897 : if (!inplace
6935 3252038 : || is_gimple_assign (stmt)
6936 797131165 : || gimple_code (stmt) == GIMPLE_COND)
6937 : {
6938 796839110 : gimple_seq seq = NULL;
6939 796839110 : gimple_match_op res_op;
6940 1591347450 : if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
6941 : valueize, valueize)
6942 796839110 : && replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
6943 : dce_worklist))
6944 : changed = true;
6945 : else
6946 792098263 : gimple_seq_discard (seq);
6947 : }
6948 :
6949 797760378 : stmt = gsi_stmt (*gsi);
6950 :
6951 : /* Fold the main computation performed by the statement. */
6952 797760378 : switch (gimple_code (stmt))
6953 : {
6954 261232344 : case GIMPLE_ASSIGN:
6955 261232344 : {
6956 : /* Try to canonicalize for boolean-typed X the comparisons
6957 : X == 0, X == 1, X != 0, and X != 1. */
6958 261232344 : if (gimple_assign_rhs_code (stmt) == EQ_EXPR
6959 261232344 : || gimple_assign_rhs_code (stmt) == NE_EXPR)
6960 : {
6961 3520652 : tree lhs = gimple_assign_lhs (stmt);
6962 3520652 : tree op1 = gimple_assign_rhs1 (stmt);
6963 3520652 : tree op2 = gimple_assign_rhs2 (stmt);
6964 3520652 : tree type = TREE_TYPE (op1);
6965 :
6966 : /* Check whether the comparison operands are of the same boolean
6967 : type as the result type is.
6968 : Check that second operand is an integer-constant with value
6969 : one or zero. */
6970 3520652 : if (TREE_CODE (op2) == INTEGER_CST
6971 2417016 : && (integer_zerop (op2) || integer_onep (op2))
6972 5317051 : && useless_type_conversion_p (TREE_TYPE (lhs), type))
6973 : {
6974 4796 : enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
6975 4796 : bool is_logical_not = false;
6976 :
6977 : /* X == 0 and X != 1 is a logical-not.of X
6978 : X == 1 and X != 0 is X */
6979 4038 : if ((cmp_code == EQ_EXPR && integer_zerop (op2))
6980 4796 : || (cmp_code == NE_EXPR && integer_onep (op2)))
6981 4754 : is_logical_not = true;
6982 :
6983 4796 : if (is_logical_not == false)
6984 42 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
6985 : /* Only for one-bit precision typed X the transformation
6986 : !X -> ~X is valid. */
6987 4754 : else if (TYPE_PRECISION (type) == 1)
6988 4754 : gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
6989 : /* Otherwise we use !X -> X ^ 1. */
6990 : else
6991 0 : gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
6992 : build_int_cst (type, 1));
6993 : changed = true;
6994 : break;
6995 : }
6996 : }
6997 :
6998 261227548 : unsigned old_num_ops = gimple_num_ops (stmt);
6999 261227548 : tree lhs = gimple_assign_lhs (stmt);
7000 261227548 : tree new_rhs = fold_gimple_assign (gsi);
7001 261227548 : if (new_rhs
7002 261347136 : && !useless_type_conversion_p (TREE_TYPE (lhs),
7003 119588 : TREE_TYPE (new_rhs)))
7004 0 : new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
7005 261227548 : if (new_rhs
7006 261227548 : && (!inplace
7007 1036 : || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
7008 : {
7009 119588 : gimple_assign_set_rhs_from_tree (gsi, new_rhs);
7010 119588 : changed = true;
7011 : }
7012 : break;
7013 : }
7014 :
7015 57761045 : case GIMPLE_CALL:
7016 57761045 : changed |= gimple_fold_call (gsi, inplace);
7017 57761045 : break;
7018 :
7019 411458731 : case GIMPLE_DEBUG:
7020 411458731 : if (gimple_debug_bind_p (stmt))
7021 : {
7022 317029962 : tree val = gimple_debug_bind_get_value (stmt);
7023 317029962 : if (val && REFERENCE_CLASS_P (val))
7024 : {
7025 2680039 : tree tem = maybe_fold_reference (val);
7026 2680039 : if (tem)
7027 : {
7028 4387 : gimple_debug_bind_set_value (stmt, tem);
7029 4387 : changed = true;
7030 : }
7031 : }
7032 : }
7033 : break;
7034 :
7035 10988342 : case GIMPLE_RETURN:
7036 10988342 : {
7037 10988342 : greturn *ret_stmt = as_a<greturn *> (stmt);
7038 10988342 : tree ret = gimple_return_retval(ret_stmt);
7039 :
7040 10988342 : if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
7041 : {
7042 4617644 : tree val = valueize (ret);
7043 4617644 : if (val && val != ret
7044 4617644 : && may_propagate_copy (ret, val))
7045 : {
7046 0 : gimple_return_set_retval (ret_stmt, val);
7047 0 : changed = true;
7048 : }
7049 : }
7050 : }
7051 : break;
7052 :
7053 797760378 : default:;
7054 : }
7055 :
7056 797760378 : return changed;
7057 : }
7058 :
7059 : /* Valueziation callback that ends up not following SSA edges. */
7060 :
7061 : tree
7062 5917100648 : no_follow_ssa_edges (tree)
7063 : {
7064 5917100648 : return NULL_TREE;
7065 : }
7066 :
7067 : /* Valueization callback that ends up following single-use SSA edges only. */
7068 :
7069 : tree
7070 908787669 : follow_single_use_edges (tree val)
7071 : {
7072 908787669 : if (TREE_CODE (val) == SSA_NAME
7073 908787669 : && !has_single_use (val))
7074 465465407 : return NULL_TREE;
7075 : return val;
7076 : }
7077 :
7078 : /* Valueization callback that follows all SSA edges. */
7079 :
7080 : tree
7081 211325788 : follow_all_ssa_edges (tree val)
7082 : {
7083 211325788 : return val;
7084 : }
7085 :
7086 : /* Fold the statement pointed to by GSI. In some cases, this function may
7087 : replace the whole statement with a new one. Returns true iff folding
7088 : makes any changes.
7089 : The statement pointed to by GSI should be in valid gimple form but may
7090 : be in unfolded state as resulting from for example constant propagation
7091 : which can produce *&x = 0. */
7092 :
7093 : bool
7094 159639933 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
7095 : {
7096 159639933 : return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
7097 : }
7098 :
7099 : bool
7100 634868407 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
7101 : {
7102 634868407 : return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
7103 : }
7104 :
7105 : /* Perform the minimal folding on statement *GSI. Only operations like
7106 : *&x created by constant propagation are handled. The statement cannot
7107 : be replaced with a new one. Return true if the statement was
7108 : changed, false otherwise.
7109 : The statement *GSI should be in valid gimple form but may
7110 : be in unfolded state as resulting from for example constant propagation
7111 : which can produce *&x = 0. */
7112 :
7113 : bool
7114 3252038 : fold_stmt_inplace (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
7115 : {
7116 3252038 : gimple *stmt = gsi_stmt (*gsi);
7117 3252038 : bool changed = fold_stmt_1 (gsi, true, valueize);
7118 3252038 : gcc_assert (gsi_stmt (*gsi) == stmt);
7119 3252038 : return changed;
7120 : }
7121 :
7122 : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
7123 : if EXPR is null or we don't know how.
7124 : If non-null, the result always has boolean type. */
7125 :
7126 : static tree
7127 428163 : canonicalize_bool (tree expr, bool invert)
7128 : {
7129 428163 : if (!expr)
7130 : return NULL_TREE;
7131 60 : else if (invert)
7132 : {
7133 41 : if (integer_nonzerop (expr))
7134 0 : return boolean_false_node;
7135 41 : else if (integer_zerop (expr))
7136 0 : return boolean_true_node;
7137 41 : else if (TREE_CODE (expr) == SSA_NAME)
7138 0 : return fold_build2 (EQ_EXPR, boolean_type_node, expr,
7139 : build_int_cst (TREE_TYPE (expr), 0));
7140 41 : else if (COMPARISON_CLASS_P (expr))
7141 41 : return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
7142 : boolean_type_node,
7143 : TREE_OPERAND (expr, 0),
7144 : TREE_OPERAND (expr, 1));
7145 : else
7146 : return NULL_TREE;
7147 : }
7148 : else
7149 : {
7150 19 : if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
7151 : return expr;
7152 0 : if (integer_nonzerop (expr))
7153 0 : return boolean_true_node;
7154 0 : else if (integer_zerop (expr))
7155 0 : return boolean_false_node;
7156 0 : else if (TREE_CODE (expr) == SSA_NAME)
7157 0 : return fold_build2 (NE_EXPR, boolean_type_node, expr,
7158 : build_int_cst (TREE_TYPE (expr), 0));
7159 0 : else if (COMPARISON_CLASS_P (expr))
7160 0 : return fold_build2 (TREE_CODE (expr),
7161 : boolean_type_node,
7162 : TREE_OPERAND (expr, 0),
7163 : TREE_OPERAND (expr, 1));
7164 : else
7165 : return NULL_TREE;
7166 : }
7167 : }
7168 :
7169 : /* Check to see if a boolean expression EXPR is logically equivalent to the
7170 : comparison (OP1 CODE OP2). Check for various identities involving
7171 : SSA_NAMEs. */
7172 :
7173 : static bool
7174 1388 : same_bool_comparison_p (const_tree expr, enum tree_code code,
7175 : const_tree op1, const_tree op2)
7176 : {
7177 1388 : gimple *s;
7178 :
7179 : /* The obvious case. */
7180 1388 : if (TREE_CODE (expr) == code
7181 33 : && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
7182 1421 : && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
7183 : return true;
7184 :
7185 : /* Check for comparing (name, name != 0) and the case where expr
7186 : is an SSA_NAME with a definition matching the comparison. */
7187 1371 : if (TREE_CODE (expr) == SSA_NAME
7188 1371 : && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
7189 : {
7190 0 : if (operand_equal_p (expr, op1, 0))
7191 0 : return ((code == NE_EXPR && integer_zerop (op2))
7192 0 : || (code == EQ_EXPR && integer_nonzerop (op2)));
7193 0 : s = SSA_NAME_DEF_STMT (expr);
7194 0 : if (is_gimple_assign (s)
7195 0 : && gimple_assign_rhs_code (s) == code
7196 0 : && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
7197 0 : && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
7198 : return true;
7199 : }
7200 :
7201 : /* If op1 is of the form (name != 0) or (name == 0), and the definition
7202 : of name is a comparison, recurse. */
7203 1371 : if (TREE_CODE (op1) == SSA_NAME
7204 1371 : && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
7205 : {
7206 460 : s = SSA_NAME_DEF_STMT (op1);
7207 460 : if (is_gimple_assign (s)
7208 460 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
7209 : {
7210 0 : enum tree_code c = gimple_assign_rhs_code (s);
7211 0 : if ((c == NE_EXPR && integer_zerop (op2))
7212 0 : || (c == EQ_EXPR && integer_nonzerop (op2)))
7213 0 : return same_bool_comparison_p (expr, c,
7214 0 : gimple_assign_rhs1 (s),
7215 0 : gimple_assign_rhs2 (s));
7216 0 : if ((c == EQ_EXPR && integer_zerop (op2))
7217 0 : || (c == NE_EXPR && integer_nonzerop (op2)))
7218 0 : return same_bool_comparison_p (expr,
7219 : invert_tree_comparison (c, false),
7220 0 : gimple_assign_rhs1 (s),
7221 0 : gimple_assign_rhs2 (s));
7222 : }
7223 : }
7224 : return false;
7225 : }
7226 :
7227 : /* Check to see if two boolean expressions OP1 and OP2 are logically
7228 : equivalent. */
7229 :
7230 : static bool
7231 15 : same_bool_result_p (const_tree op1, const_tree op2)
7232 : {
7233 : /* Simple cases first. */
7234 15 : if (operand_equal_p (op1, op2, 0))
7235 : return true;
7236 :
7237 : /* Check the cases where at least one of the operands is a comparison.
7238 : These are a bit smarter than operand_equal_p in that they apply some
7239 : identifies on SSA_NAMEs. */
7240 8 : if (COMPARISON_CLASS_P (op2)
7241 16 : && same_bool_comparison_p (op1, TREE_CODE (op2),
7242 8 : TREE_OPERAND (op2, 0),
7243 8 : TREE_OPERAND (op2, 1)))
7244 : return true;
7245 8 : if (COMPARISON_CLASS_P (op1)
7246 16 : && same_bool_comparison_p (op2, TREE_CODE (op1),
7247 8 : TREE_OPERAND (op1, 0),
7248 8 : TREE_OPERAND (op1, 1)))
7249 : return true;
7250 :
7251 : /* Default case. */
7252 : return false;
7253 : }
7254 :
7255 : /* Forward declarations for some mutually recursive functions. */
7256 :
7257 : static tree
7258 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7259 : enum tree_code code2, tree op2a, tree op2b, basic_block);
7260 : static tree
7261 : and_var_with_comparison (tree type, tree var, bool invert,
7262 : enum tree_code code2, tree op2a, tree op2b,
7263 : basic_block);
7264 : static tree
7265 : and_var_with_comparison_1 (tree type, gimple *stmt,
7266 : enum tree_code code2, tree op2a, tree op2b,
7267 : basic_block);
7268 : static tree
7269 : or_comparisons_1 (tree, enum tree_code code1, tree op1a, tree op1b,
7270 : enum tree_code code2, tree op2a, tree op2b,
7271 : basic_block);
7272 : static tree
7273 : or_var_with_comparison (tree, tree var, bool invert,
7274 : enum tree_code code2, tree op2a, tree op2b,
7275 : basic_block);
7276 : static tree
7277 : or_var_with_comparison_1 (tree, gimple *stmt,
7278 : enum tree_code code2, tree op2a, tree op2b,
7279 : basic_block);
7280 :
7281 : /* Helper function for and_comparisons_1: try to simplify the AND of the
7282 : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
7283 : If INVERT is true, invert the value of the VAR before doing the AND.
7284 : Return NULL_EXPR if we can't simplify this to a single expression. */
7285 :
7286 : static tree
7287 365419 : and_var_with_comparison (tree type, tree var, bool invert,
7288 : enum tree_code code2, tree op2a, tree op2b,
7289 : basic_block outer_cond_bb)
7290 : {
7291 365419 : tree t;
7292 365419 : gimple *stmt = SSA_NAME_DEF_STMT (var);
7293 :
7294 : /* We can only deal with variables whose definitions are assignments. */
7295 365419 : if (!is_gimple_assign (stmt))
7296 : return NULL_TREE;
7297 :
7298 : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
7299 : !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
7300 : Then we only have to consider the simpler non-inverted cases. */
7301 364865 : if (invert)
7302 124357 : t = or_var_with_comparison_1 (type, stmt,
7303 : invert_tree_comparison (code2, false),
7304 : op2a, op2b, outer_cond_bb);
7305 : else
7306 240508 : t = and_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
7307 : outer_cond_bb);
7308 364865 : return canonicalize_bool (t, invert);
7309 : }
7310 :
7311 : /* Try to simplify the AND of the ssa variable defined by the assignment
7312 : STMT with the comparison specified by (OP2A CODE2 OP2B).
7313 : Return NULL_EXPR if we can't simplify this to a single expression. */
7314 :
7315 : static tree
7316 264719 : and_var_with_comparison_1 (tree type, gimple *stmt,
7317 : enum tree_code code2, tree op2a, tree op2b,
7318 : basic_block outer_cond_bb)
7319 : {
7320 264719 : tree var = gimple_assign_lhs (stmt);
7321 264719 : tree true_test_var = NULL_TREE;
7322 264719 : tree false_test_var = NULL_TREE;
7323 264719 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
7324 :
7325 : /* Check for identities like (var AND (var == 0)) => false. */
7326 264719 : if (TREE_CODE (op2a) == SSA_NAME
7327 264719 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
7328 : {
7329 13835 : if ((code2 == NE_EXPR && integer_zerop (op2b))
7330 37052 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
7331 : {
7332 14282 : true_test_var = op2a;
7333 14282 : if (var == true_test_var)
7334 : return var;
7335 : }
7336 5863 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
7337 23602 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
7338 : {
7339 4036 : false_test_var = op2a;
7340 4036 : if (var == false_test_var)
7341 0 : return boolean_false_node;
7342 : }
7343 : }
7344 :
7345 : /* If the definition is a comparison, recurse on it. */
7346 264719 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
7347 : {
7348 1960 : tree t = and_comparisons_1 (type, innercode,
7349 : gimple_assign_rhs1 (stmt),
7350 : gimple_assign_rhs2 (stmt),
7351 : code2,
7352 : op2a,
7353 : op2b, outer_cond_bb);
7354 1960 : if (t)
7355 : return t;
7356 : }
7357 :
7358 : /* If the definition is an AND or OR expression, we may be able to
7359 : simplify by reassociating. */
7360 264713 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
7361 264713 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
7362 : {
7363 26335 : tree inner1 = gimple_assign_rhs1 (stmt);
7364 26335 : tree inner2 = gimple_assign_rhs2 (stmt);
7365 26335 : gimple *s;
7366 26335 : tree t;
7367 26335 : tree partial = NULL_TREE;
7368 26335 : bool is_and = (innercode == BIT_AND_EXPR);
7369 :
7370 : /* Check for boolean identities that don't require recursive examination
7371 : of inner1/inner2:
7372 : inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
7373 : inner1 AND (inner1 OR inner2) => inner1
7374 : !inner1 AND (inner1 AND inner2) => false
7375 : !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
7376 : Likewise for similar cases involving inner2. */
7377 26335 : if (inner1 == true_test_var)
7378 0 : return (is_and ? var : inner1);
7379 26335 : else if (inner2 == true_test_var)
7380 0 : return (is_and ? var : inner2);
7381 26335 : else if (inner1 == false_test_var)
7382 0 : return (is_and
7383 0 : ? boolean_false_node
7384 0 : : and_var_with_comparison (type, inner2, false, code2, op2a,
7385 0 : op2b, outer_cond_bb));
7386 26335 : else if (inner2 == false_test_var)
7387 0 : return (is_and
7388 0 : ? boolean_false_node
7389 0 : : and_var_with_comparison (type, inner1, false, code2, op2a,
7390 0 : op2b, outer_cond_bb));
7391 :
7392 : /* Next, redistribute/reassociate the AND across the inner tests.
7393 : Compute the first partial result, (inner1 AND (op2a code op2b)) */
7394 26335 : if (TREE_CODE (inner1) == SSA_NAME
7395 26335 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
7396 25361 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7397 49961 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7398 : gimple_assign_rhs1 (s),
7399 : gimple_assign_rhs2 (s),
7400 : code2, op2a, op2b,
7401 : outer_cond_bb)))
7402 : {
7403 : /* Handle the AND case, where we are reassociating:
7404 : (inner1 AND inner2) AND (op2a code2 op2b)
7405 : => (t AND inner2)
7406 : If the partial result t is a constant, we win. Otherwise
7407 : continue on to try reassociating with the other inner test. */
7408 31 : if (is_and)
7409 : {
7410 1 : if (integer_onep (t))
7411 : return inner2;
7412 1 : else if (integer_zerop (t))
7413 0 : return boolean_false_node;
7414 : }
7415 :
7416 : /* Handle the OR case, where we are redistributing:
7417 : (inner1 OR inner2) AND (op2a code2 op2b)
7418 : => (t OR (inner2 AND (op2a code2 op2b))) */
7419 30 : else if (integer_onep (t))
7420 0 : return boolean_true_node;
7421 :
7422 : /* Save partial result for later. */
7423 : partial = t;
7424 : }
7425 :
7426 : /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
7427 26335 : if (TREE_CODE (inner2) == SSA_NAME
7428 26335 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
7429 25875 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7430 50289 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7431 : gimple_assign_rhs1 (s),
7432 : gimple_assign_rhs2 (s),
7433 : code2, op2a, op2b,
7434 : outer_cond_bb)))
7435 : {
7436 : /* Handle the AND case, where we are reassociating:
7437 : (inner1 AND inner2) AND (op2a code2 op2b)
7438 : => (inner1 AND t) */
7439 879 : if (is_and)
7440 : {
7441 27 : if (integer_onep (t))
7442 : return inner1;
7443 27 : else if (integer_zerop (t))
7444 1 : return boolean_false_node;
7445 : /* If both are the same, we can apply the identity
7446 : (x AND x) == x. */
7447 26 : else if (partial && same_bool_result_p (t, partial))
7448 : return t;
7449 : }
7450 :
7451 : /* Handle the OR case. where we are redistributing:
7452 : (inner1 OR inner2) AND (op2a code2 op2b)
7453 : => (t OR (inner1 AND (op2a code2 op2b)))
7454 : => (t OR partial) */
7455 : else
7456 : {
7457 852 : if (integer_onep (t))
7458 0 : return boolean_true_node;
7459 852 : else if (partial)
7460 : {
7461 : /* We already got a simplification for the other
7462 : operand to the redistributed OR expression. The
7463 : interesting case is when at least one is false.
7464 : Or, if both are the same, we can apply the identity
7465 : (x OR x) == x. */
7466 11 : if (integer_zerop (partial))
7467 : return t;
7468 6 : else if (integer_zerop (t))
7469 : return partial;
7470 4 : else if (same_bool_result_p (t, partial))
7471 : return t;
7472 : }
7473 : }
7474 : }
7475 : }
7476 : return NULL_TREE;
7477 : }
7478 :
7479 : /* Try to simplify the AND of two comparisons defined by
7480 : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
7481 : If this can be done without constructing an intermediate value,
7482 : return the resulting tree; otherwise NULL_TREE is returned.
7483 : This function is deliberately asymmetric as it recurses on SSA_DEFs
7484 : in the first comparison but not the second. */
7485 :
7486 : static tree
7487 1266158 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7488 : enum tree_code code2, tree op2a, tree op2b,
7489 : basic_block outer_cond_bb)
7490 : {
7491 1266158 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
7492 :
7493 : /* First check for ((x CODE1 y) AND (x CODE2 y)). */
7494 1266158 : if (operand_equal_p (op1a, op2a, 0)
7495 1266158 : && operand_equal_p (op1b, op2b, 0))
7496 : {
7497 : /* Result will be either NULL_TREE, or a combined comparison. */
7498 4570 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7499 : TRUTH_ANDIF_EXPR, code1, code2,
7500 : truth_type, op1a, op1b);
7501 4570 : if (t)
7502 : return t;
7503 : }
7504 :
7505 : /* Likewise the swapped case of the above. */
7506 1264468 : if (operand_equal_p (op1a, op2b, 0)
7507 1264468 : && operand_equal_p (op1b, op2a, 0))
7508 : {
7509 : /* Result will be either NULL_TREE, or a combined comparison. */
7510 65 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7511 : TRUTH_ANDIF_EXPR, code1,
7512 : swap_tree_comparison (code2),
7513 : truth_type, op1a, op1b);
7514 65 : if (t)
7515 : return t;
7516 : }
7517 :
7518 : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
7519 : NAME's definition is a truth value. See if there are any simplifications
7520 : that can be done against the NAME's definition. */
7521 1264467 : if (TREE_CODE (op1a) == SSA_NAME
7522 1264416 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
7523 2230699 : && (integer_zerop (op1b) || integer_onep (op1b)))
7524 : {
7525 177305 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
7526 468421 : || (code1 == NE_EXPR && integer_onep (op1b)));
7527 428283 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
7528 428283 : switch (gimple_code (stmt))
7529 : {
7530 362592 : case GIMPLE_ASSIGN:
7531 : /* Try to simplify by copy-propagating the definition. */
7532 362592 : return and_var_with_comparison (type, op1a, invert, code2, op2a,
7533 362592 : op2b, outer_cond_bb);
7534 :
7535 31037 : case GIMPLE_PHI:
7536 : /* If every argument to the PHI produces the same result when
7537 : ANDed with the second comparison, we win.
7538 : Do not do this unless the type is bool since we need a bool
7539 : result here anyway. */
7540 31037 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
7541 : {
7542 : tree result = NULL_TREE;
7543 : unsigned i;
7544 10834 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
7545 : {
7546 10834 : tree arg = gimple_phi_arg_def (stmt, i);
7547 :
7548 : /* If this PHI has itself as an argument, ignore it.
7549 : If all the other args produce the same result,
7550 : we're still OK. */
7551 10834 : if (arg == gimple_phi_result (stmt))
7552 0 : continue;
7553 10834 : else if (TREE_CODE (arg) == INTEGER_CST)
7554 : {
7555 7103 : if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
7556 : {
7557 4111 : if (!result)
7558 1717 : result = boolean_false_node;
7559 2394 : else if (!integer_zerop (result))
7560 : return NULL_TREE;
7561 : }
7562 2992 : else if (!result)
7563 1842 : result = fold_build2 (code2, boolean_type_node,
7564 : op2a, op2b);
7565 1150 : else if (!same_bool_comparison_p (result,
7566 : code2, op2a, op2b))
7567 : return NULL_TREE;
7568 : }
7569 3731 : else if (TREE_CODE (arg) == SSA_NAME
7570 3731 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
7571 : {
7572 3728 : tree temp;
7573 3728 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
7574 : /* In simple cases we can look through PHI nodes,
7575 : but we have to be careful with loops.
7576 : See PR49073. */
7577 3728 : if (! dom_info_available_p (CDI_DOMINATORS)
7578 3728 : || gimple_bb (def_stmt) == gimple_bb (stmt)
7579 7456 : || dominated_by_p (CDI_DOMINATORS,
7580 3728 : gimple_bb (def_stmt),
7581 3728 : gimple_bb (stmt)))
7582 901 : return NULL_TREE;
7583 2827 : temp = and_var_with_comparison (type, arg, invert, code2,
7584 : op2a, op2b,
7585 : outer_cond_bb);
7586 2827 : if (!temp)
7587 : return NULL_TREE;
7588 0 : else if (!result)
7589 : result = temp;
7590 0 : else if (!same_bool_result_p (result, temp))
7591 : return NULL_TREE;
7592 : }
7593 : else
7594 : return NULL_TREE;
7595 : }
7596 : return result;
7597 : }
7598 :
7599 : default:
7600 : break;
7601 : }
7602 : }
7603 : return NULL_TREE;
7604 : }
7605 :
7606 : static basic_block fosa_bb;
7607 : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
7608 : static tree
7609 60235278 : follow_outer_ssa_edges (tree val)
7610 : {
7611 60235278 : if (TREE_CODE (val) == SSA_NAME
7612 60235278 : && !SSA_NAME_IS_DEFAULT_DEF (val))
7613 : {
7614 59089790 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
7615 59089790 : if (!def_bb
7616 19500753 : || def_bb == fosa_bb
7617 69543508 : || (dom_info_available_p (CDI_DOMINATORS)
7618 10453718 : && (def_bb == fosa_bb
7619 10453718 : || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
7620 52642873 : return val;
7621 : /* We cannot temporarily rewrite stmts with undefined overflow
7622 : behavior, so avoid expanding them. */
7623 12854210 : if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
7624 699638 : || POINTER_TYPE_P (TREE_TYPE (val)))
7625 12485981 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
7626 : return NULL_TREE;
7627 2775339 : flow_sensitive_info_storage storage;
7628 2775339 : storage.save_and_clear (val);
7629 : /* If the definition does not dominate fosa_bb temporarily reset
7630 : flow-sensitive info. */
7631 2775339 : fosa_unwind->safe_push (std::make_pair (val, storage));
7632 2775339 : return val;
7633 : }
7634 : return val;
7635 : }
7636 :
7637 : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
7638 : : try to simplify the AND/OR of the ssa variable VAR with the comparison
7639 : specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
7640 : simplify this to a single expression. As we are going to lower the cost
7641 : of building SSA names / gimple stmts significantly, we need to allocate
7642 : them ont the stack. This will cause the code to be a bit ugly. */
7643 :
7644 : static tree
7645 1200268 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
7646 : enum tree_code code1,
7647 : tree op1a, tree op1b,
7648 : enum tree_code code2, tree op2a,
7649 : tree op2b,
7650 : basic_block outer_cond_bb)
7651 : {
7652 : /* Allocate gimple stmt1 on the stack. */
7653 1200268 : gassign *stmt1
7654 1200268 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7655 1200268 : gimple_init (stmt1, GIMPLE_ASSIGN, 3);
7656 1200268 : gimple_assign_set_rhs_code (stmt1, code1);
7657 1200268 : gimple_assign_set_rhs1 (stmt1, op1a);
7658 1200268 : gimple_assign_set_rhs2 (stmt1, op1b);
7659 1200268 : gimple_set_bb (stmt1, NULL);
7660 :
7661 : /* Allocate gimple stmt2 on the stack. */
7662 1200268 : gassign *stmt2
7663 1200268 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7664 1200268 : gimple_init (stmt2, GIMPLE_ASSIGN, 3);
7665 1200268 : gimple_assign_set_rhs_code (stmt2, code2);
7666 1200268 : gimple_assign_set_rhs1 (stmt2, op2a);
7667 1200268 : gimple_assign_set_rhs2 (stmt2, op2b);
7668 1200268 : gimple_set_bb (stmt2, NULL);
7669 :
7670 : /* Allocate SSA names(lhs1) on the stack. */
7671 1200268 : alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
7672 1200268 : tree lhs1 = (tree) &lhs1buf[0];
7673 1200268 : memset (lhs1, 0, sizeof (tree_ssa_name));
7674 1200268 : TREE_SET_CODE (lhs1, SSA_NAME);
7675 1200268 : TREE_TYPE (lhs1) = type;
7676 1200268 : init_ssa_name_imm_use (lhs1);
7677 :
7678 : /* Allocate SSA names(lhs2) on the stack. */
7679 1200268 : alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
7680 1200268 : tree lhs2 = (tree) &lhs2buf[0];
7681 1200268 : memset (lhs2, 0, sizeof (tree_ssa_name));
7682 1200268 : TREE_SET_CODE (lhs2, SSA_NAME);
7683 1200268 : TREE_TYPE (lhs2) = type;
7684 1200268 : init_ssa_name_imm_use (lhs2);
7685 :
7686 1200268 : gimple_assign_set_lhs (stmt1, lhs1);
7687 1200268 : gimple_assign_set_lhs (stmt2, lhs2);
7688 :
7689 1200268 : gimple_match_op op (gimple_match_cond::UNCOND, code,
7690 : type, gimple_assign_lhs (stmt1),
7691 1200268 : gimple_assign_lhs (stmt2));
7692 1200268 : fosa_bb = outer_cond_bb;
7693 1200268 : auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
7694 1200268 : fosa_unwind = &unwind_stack;
7695 1799900 : if (op.resimplify (NULL, (!outer_cond_bb
7696 : ? follow_all_ssa_edges : follow_outer_ssa_edges)))
7697 : {
7698 3235 : fosa_unwind = NULL;
7699 11076 : for (auto p : unwind_stack)
7700 1371 : p.second.restore (p.first);
7701 3235 : if (gimple_simplified_result_is_gimple_val (&op))
7702 : {
7703 2601 : tree res = op.ops[0];
7704 2601 : if (res == lhs1)
7705 2098 : return build2 (code1, type, op1a, op1b);
7706 503 : else if (res == lhs2)
7707 432 : return build2 (code2, type, op2a, op2b);
7708 : else
7709 : return res;
7710 : }
7711 634 : else if (op.code.is_tree_code ()
7712 634 : && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
7713 : {
7714 634 : tree op0 = op.ops[0];
7715 634 : tree op1 = op.ops[1];
7716 634 : if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
7717 : return NULL_TREE; /* not simple */
7718 :
7719 634 : return build2 ((enum tree_code)op.code, op.type, op0, op1);
7720 : }
7721 : }
7722 1197033 : fosa_unwind = NULL;
7723 6365067 : for (auto p : unwind_stack)
7724 2773968 : p.second.restore (p.first);
7725 :
7726 1197033 : return NULL_TREE;
7727 1200268 : }
7728 :
7729 : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
7730 : conversion. Reject loads if LOAD is NULL, otherwise set *LOAD if a
7731 : converting load is found. */
7732 :
7733 : static bool
7734 3291099 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
7735 : {
7736 3291099 : bool ret = false;
7737 :
7738 3291099 : if (TREE_CODE (t) == SSA_NAME
7739 3291099 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7740 1817769 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7741 : {
7742 1724189 : bool load_p = gimple_assign_load_p (def);
7743 1724189 : if (load_p && !load)
7744 : return false;
7745 1739928 : switch (gimple_assign_rhs_code (def))
7746 : {
7747 13786 : CASE_CONVERT:
7748 13786 : op[0] = gimple_assign_rhs1 (def);
7749 13786 : ret = true;
7750 13786 : break;
7751 :
7752 3950 : case VIEW_CONVERT_EXPR:
7753 3950 : op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
7754 3950 : ret = true;
7755 3950 : break;
7756 :
7757 : default:
7758 : break;
7759 : }
7760 :
7761 17736 : if (ret && load_p)
7762 0 : *load = def;
7763 : }
7764 :
7765 : return ret;
7766 : }
7767 :
7768 : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
7769 : binary expression with code CODE. */
7770 :
7771 : static bool
7772 3325254 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
7773 : {
7774 3325254 : if (TREE_CODE (t) == SSA_NAME
7775 3325254 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7776 1849657 : if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7777 1753669 : if (gimple_assign_rhs_code (def) == code)
7778 : {
7779 56854 : op[0] = gimple_assign_rhs1 (def);
7780 56854 : op[1] = gimple_assign_rhs2 (def);
7781 56854 : return true;
7782 : }
7783 : return false;
7784 : }
7785 : /* Subroutine for fold_truth_andor_1: decode a field reference.
7786 :
7787 : If *PEXP is a comparison reference, we return the innermost reference.
7788 :
7789 : *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
7790 : set to the starting bit number.
7791 :
7792 : *PVOLATILEP is set to 1 if the any expression encountered is volatile;
7793 : otherwise it is not changed.
7794 :
7795 : *PUNSIGNEDP is set to the signedness of the field.
7796 :
7797 : *PREVERSEP is set to the storage order of the field.
7798 :
7799 : *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. If
7800 : *PAND_MASK is initially set to a mask with nonzero precision, that mask is
7801 : combined with the found mask, or adjusted in precision to match.
7802 :
7803 : *PSIGNBIT is set to TRUE if, before clipping to *PBITSIZE, the mask
7804 : encompassed bits that corresponded to extensions of the sign bit.
7805 :
7806 : *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
7807 : case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
7808 : *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
7809 : and the left-hand operand of the XOR will be decoded. If *PXORP is TRUE,
7810 : PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
7811 : right-hand operand of the XOR will be decoded.
7812 :
7813 : *LOAD is set to the load stmt of the innermost reference, if any,
7814 : *and NULL otherwise.
7815 :
7816 : LOC[0..3] are filled in as conversion, masking, shifting and loading
7817 : operations are located.
7818 :
7819 : Return 0 if this is not a component reference or is one that we can't
7820 : do anything with. */
7821 :
7822 : static tree
7823 1174145 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
7824 : HOST_WIDE_INT *pbitpos,
7825 : bool *punsignedp, bool *preversep, bool *pvolatilep,
7826 : wide_int *pand_mask, bool *psignbit,
7827 : bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
7828 : gimple **pload, location_t loc[4])
7829 : {
7830 1174145 : tree exp = *pexp;
7831 1174145 : tree outer_type = 0;
7832 1174145 : wide_int and_mask;
7833 1174145 : tree inner, offset;
7834 1174145 : int shiftrt = 0;
7835 1174145 : tree res_ops[2];
7836 1174145 : machine_mode mode;
7837 1174145 : bool convert_before_shift = false;
7838 1174145 : bool signbit = false;
7839 1174145 : bool xorp = false;
7840 1174145 : tree xor_cmp_op;
7841 1174145 : wide_int xor_and_mask;
7842 1174145 : gimple *load = NULL;
7843 :
7844 : /* All the optimizations using this function assume integer fields.
7845 : There are problems with FP fields since the type_for_size call
7846 : below can fail for, e.g., XFmode. */
7847 1174145 : if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
7848 : return NULL_TREE;
7849 :
7850 : /* Drop casts, saving only the outermost type, effectively used in
7851 : the compare. We can deal with at most one conversion, and it may
7852 : appear at various points in the chain of recognized preparation
7853 : statements. Earlier optimizers will often have already dropped
7854 : unneeded extensions, but they may survive, as in PR118046. ???
7855 : Can we do better and allow multiple conversions, perhaps taking
7856 : note of the narrowest intermediate type, sign extensions and
7857 : whatnot? */
7858 1108436 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7859 : {
7860 16385 : outer_type = TREE_TYPE (exp);
7861 16385 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7862 16385 : exp = res_ops[0];
7863 : }
7864 :
7865 : /* Recognize and save a masking operation. Combine it with an
7866 : incoming mask. */
7867 1108436 : if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
7868 1108436 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7869 : {
7870 33516 : loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
7871 33516 : exp = res_ops[0];
7872 33516 : and_mask = wi::to_wide (res_ops[1]);
7873 33516 : unsigned prec_in = pand_mask->get_precision ();
7874 33516 : if (prec_in)
7875 : {
7876 52 : unsigned prec_op = and_mask.get_precision ();
7877 52 : if (prec_in >= prec_op)
7878 : {
7879 52 : if (prec_in > prec_op)
7880 0 : and_mask = wide_int::from (and_mask, prec_in, UNSIGNED);
7881 52 : and_mask &= *pand_mask;
7882 : }
7883 : else
7884 0 : and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
7885 : }
7886 : }
7887 : else
7888 1074920 : and_mask = *pand_mask;
7889 :
7890 : /* Turn (a ^ b) [!]= 0 into a [!]= b. */
7891 1108436 : if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
7892 : {
7893 : /* No location recorded for this one, it's entirely subsumed by the
7894 : compare. */
7895 10998 : if (*pxorp)
7896 : {
7897 5471 : exp = res_ops[1];
7898 5471 : gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
7899 : }
7900 5527 : else if (!pxor_cmp_op)
7901 : /* Not much we can do when xor appears in the right-hand compare
7902 : operand. */
7903 : return NULL_TREE;
7904 5473 : else if (integer_zerop (*pxor_cmp_op))
7905 : {
7906 5471 : xorp = true;
7907 5471 : exp = res_ops[0];
7908 5471 : xor_cmp_op = *pexp;
7909 5471 : xor_and_mask = *pand_mask;
7910 : }
7911 : }
7912 :
7913 : /* Another chance to drop conversions. */
7914 1108382 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7915 : {
7916 1331 : outer_type = TREE_TYPE (exp);
7917 1331 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7918 1331 : exp = res_ops[0];
7919 : }
7920 :
7921 : /* Take note of shifts. */
7922 1108382 : if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
7923 1108382 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7924 : {
7925 568 : loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
7926 568 : exp = res_ops[0];
7927 568 : if (!tree_fits_shwi_p (res_ops[1]))
7928 : return NULL_TREE;
7929 568 : shiftrt = tree_to_shwi (res_ops[1]);
7930 568 : if (shiftrt <= 0)
7931 : return NULL_TREE;
7932 : }
7933 :
7934 : /* Yet another chance to drop conversions. This one is allowed to
7935 : match a converting load, subsuming the load identification block
7936 : below. */
7937 1108382 : if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
7938 : {
7939 20 : outer_type = TREE_TYPE (exp);
7940 20 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7941 20 : if (load)
7942 0 : loc[3] = gimple_location (load);
7943 20 : exp = res_ops[0];
7944 : /* This looks backwards, but we're going back the def chain, so if we
7945 : find the conversion here, after finding a shift, that's because the
7946 : convert appears before the shift, and we should thus adjust the bit
7947 : pos and size because of the shift after adjusting it due to type
7948 : conversion. */
7949 20 : convert_before_shift = true;
7950 : }
7951 :
7952 : /* Identify the load, if there is one. */
7953 1108382 : if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
7954 : {
7955 615732 : gimple *def = SSA_NAME_DEF_STMT (exp);
7956 615732 : if (gimple_assign_load_p (def))
7957 : {
7958 389146 : loc[3] = gimple_location (def);
7959 389146 : load = def;
7960 389146 : exp = gimple_assign_rhs1 (def);
7961 : }
7962 : }
7963 :
7964 : /* Identify the relevant bits. */
7965 1108382 : poly_int64 poly_bitsize, poly_bitpos;
7966 1108382 : int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
7967 1108382 : inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
7968 : &mode, &unsignedp, &reversep, &volatilep);
7969 :
7970 1108382 : HOST_WIDE_INT bs, bp;
7971 1108382 : if (!poly_bitsize.is_constant (&bs)
7972 1108382 : || !poly_bitpos.is_constant (&bp)
7973 1108382 : || bs <= shiftrt
7974 1108382 : || offset != 0
7975 1107148 : || TREE_CODE (inner) == PLACEHOLDER_EXPR
7976 : /* Reject out-of-bound accesses (PR79731, PR118514). */
7977 1107148 : || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
7978 1107114 : || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
7979 759954 : && !type_has_mode_precision_p (TREE_TYPE (inner))))
7980 48489 : return NULL_TREE;
7981 :
7982 : /* Adjust shifts... */
7983 1059893 : if (convert_before_shift
7984 1059893 : && outer_type && bs > TYPE_PRECISION (outer_type))
7985 : {
7986 2 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7987 2 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7988 0 : bp += excess;
7989 : bs -= excess;
7990 : }
7991 :
7992 1059893 : if (shiftrt)
7993 : {
7994 : /* Punt if we're shifting by more than the loaded bitfield (after
7995 : adjustment), or if there's a shift after a change of signedness, punt.
7996 : When comparing this field with a constant, we'll check that the
7997 : constant is a proper sign- or zero-extension (depending on signedness)
7998 : of a value that would fit in the selected portion of the bitfield. A
7999 : shift after a change of signedness would make the extension
8000 : non-uniform, and we can't deal with that (yet ???). See
8001 : gcc.dg/field-merge-22.c for a test that would go wrong. */
8002 568 : if (bs <= shiftrt
8003 568 : || (convert_before_shift
8004 20 : && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
8005 : return NULL_TREE;
8006 549 : if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8007 549 : bp += shiftrt;
8008 549 : bs -= shiftrt;
8009 : }
8010 :
8011 : /* ... and bit position. */
8012 1059874 : if (!convert_before_shift
8013 1059874 : && outer_type && bs > TYPE_PRECISION (outer_type))
8014 : {
8015 7749 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
8016 7749 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8017 0 : bp += excess;
8018 : bs -= excess;
8019 : }
8020 :
8021 : /* If the number of bits in the reference is the same as the bitsize of
8022 : the outer type, then the outer type gives the signedness. Otherwise
8023 : (in case of a small bitfield) the signedness is unchanged. */
8024 1059874 : if (outer_type && bs == TYPE_PRECISION (outer_type))
8025 12917 : unsignedp = TYPE_UNSIGNED (outer_type);
8026 :
8027 : /* Make the mask the expected width. */
8028 1059874 : if (and_mask.get_precision () != 0)
8029 : {
8030 : /* If the AND_MASK encompasses bits that would be extensions of
8031 : the sign bit, set SIGNBIT. */
8032 38738 : if (!unsignedp
8033 2978 : && and_mask.get_precision () > bs
8034 41764 : && (and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
8035 : signbit = true;
8036 38738 : and_mask = wide_int::from (and_mask, bs, UNSIGNED);
8037 : }
8038 :
8039 1059874 : *pexp = exp;
8040 1059874 : *pload = load;
8041 1059874 : *pbitsize = bs;
8042 1059874 : *pbitpos = bp;
8043 1059874 : *punsignedp = unsignedp;
8044 1059874 : *preversep = reversep;
8045 1059874 : *pvolatilep = volatilep;
8046 1059874 : *psignbit = signbit;
8047 1059874 : *pand_mask = and_mask;
8048 1059874 : if (xorp)
8049 : {
8050 5471 : *pxorp = xorp;
8051 5471 : *pxor_cmp_op = xor_cmp_op;
8052 5471 : *pxor_and_mask = xor_and_mask;
8053 : }
8054 :
8055 : return inner;
8056 1174145 : }
8057 :
8058 : /* Return the one bitpos within bit extents L or R that is at an
8059 : ALIGN-bit alignment boundary, or -1 if there is more than one such
8060 : boundary, if there isn't any, or if there is any such boundary
8061 : between the extents. L and R are given by bitpos and bitsize. If
8062 : it doesn't return -1, there are two consecutive ALIGN-bit words
8063 : that contain both extents, and at least one of the extents
8064 : straddles across the returned alignment boundary. */
8065 :
8066 : static inline HOST_WIDE_INT
8067 48301 : compute_split_boundary_from_align (HOST_WIDE_INT align,
8068 : HOST_WIDE_INT l_bitpos,
8069 : HOST_WIDE_INT l_bitsize,
8070 : HOST_WIDE_INT r_bitpos,
8071 : HOST_WIDE_INT r_bitsize)
8072 : {
8073 48301 : HOST_WIDE_INT amask = ~(align - 1);
8074 :
8075 48301 : HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
8076 48301 : HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
8077 :
8078 48301 : HOST_WIDE_INT boundary = (end_bit - 1) & amask;
8079 :
8080 : /* Make sure we're crossing no more than one alignment boundary.
8081 :
8082 : ??? We don't have logic to recombine loads of two adjacent
8083 : fields that each crosses a different alignment boundary, so
8084 : as to load the middle word only once, if other words can't be
8085 : otherwise recombined. */
8086 48301 : if (boundary - first_bit > align)
8087 : return -1;
8088 :
8089 18708 : HOST_WIDE_INT l_start_word = l_bitpos & amask;
8090 18708 : HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
8091 :
8092 18708 : HOST_WIDE_INT r_start_word = r_bitpos & amask;
8093 18708 : HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
8094 :
8095 : /* If neither field straddles across an alignment boundary, it's no
8096 : use to even try to merge them. */
8097 18708 : if (l_start_word == l_end_word && r_start_word == r_end_word)
8098 18401 : return -1;
8099 :
8100 : return boundary;
8101 : }
8102 :
8103 : /* Make a bit_field_ref. If POINT is NULL, return the BIT_FIELD_REF.
8104 : Otherwise, build and insert a load stmt before POINT, and return
8105 : the SSA_NAME. ??? Rewrite LOAD in terms of the bitfield? */
8106 :
8107 : static tree
8108 4635 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
8109 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
8110 : bool unsignedp, bool reversep, gimple *point)
8111 : {
8112 4635 : if (point && loc == UNKNOWN_LOCATION)
8113 18 : loc = gimple_location (point);
8114 :
8115 4635 : tree ref = make_bit_field_ref (loc, unshare_expr (inner),
8116 : unshare_expr (orig_inner),
8117 : type, bitsize, bitpos,
8118 : unsignedp, reversep);
8119 4635 : if (!point)
8120 : return ref;
8121 :
8122 : /* If we're remaking the same load, reuse the SSA NAME it is already loaded
8123 : into. */
8124 4480 : if (gimple_assign_load_p (point)
8125 4480 : && operand_equal_p (ref, gimple_assign_rhs1 (point)))
8126 : {
8127 1662 : gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
8128 : return gimple_assign_lhs (point);
8129 : }
8130 :
8131 2818 : gimple_seq stmts = NULL;
8132 2818 : tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
8133 :
8134 : /* We know the vuse is supposed to end up being the same as that at the
8135 : original load at the insertion point, but if we don't set it, it will be a
8136 : generic placeholder that only the global SSA update at the end of the pass
8137 : would make equal, too late for us to use in further combinations. So go
8138 : ahead and copy the vuse. */
8139 :
8140 2818 : tree reaching_vuse = gimple_vuse (point);
8141 2818 : for (gimple_stmt_iterator i = gsi_start (stmts);
8142 6146 : !gsi_end_p (i); gsi_next (&i))
8143 : {
8144 3328 : gimple *new_stmt = gsi_stmt (i);
8145 6656 : if (gimple_has_mem_ops (new_stmt))
8146 3328 : gimple_set_vuse (new_stmt, reaching_vuse);
8147 : }
8148 :
8149 2818 : gimple_stmt_iterator gsi = gsi_for_stmt (point);
8150 2818 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
8151 2818 : return ret;
8152 : }
8153 :
8154 : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
8155 : LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
8156 : respectively, starting at BIT_POS, using reversed endianness if
8157 : REVERSEP. Also initialize BITPOS (the starting position of each
8158 : part into INNER), BITSIZ (the bit count starting at BITPOS),
8159 : TOSHIFT[1] (the amount by which the part and its mask are to be
8160 : shifted right to bring its least-significant bit to bit zero) and
8161 : SHIFTED (the amount by which the part, by separate loading, has
8162 : already been shifted right, but that the mask needs shifting to
8163 : match). */
8164 :
8165 : static inline void
8166 307 : build_split_load (tree /* out */ ln_arg[2],
8167 : HOST_WIDE_INT /* out */ bitpos[2],
8168 : HOST_WIDE_INT /* out */ bitsiz[2],
8169 : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
8170 : HOST_WIDE_INT /* out */ shifted[2],
8171 : location_t loc, tree inner, tree orig_inner,
8172 : scalar_int_mode mode, scalar_int_mode mode2,
8173 : HOST_WIDE_INT bit_pos, bool reversep,
8174 : gimple *point[2])
8175 : {
8176 307 : scalar_int_mode modes[2] = { mode, mode2 };
8177 307 : bitsiz[0] = GET_MODE_BITSIZE (mode);
8178 307 : bitsiz[1] = GET_MODE_BITSIZE (mode2);
8179 :
8180 921 : for (int i = 0; i < 2; i++)
8181 : {
8182 614 : tree type = lang_hooks.types.type_for_mode (modes[i], 1);
8183 614 : if (!type)
8184 : {
8185 0 : type = build_nonstandard_integer_type (bitsiz[0], 1);
8186 0 : gcc_assert (type);
8187 : }
8188 614 : bitpos[i] = bit_pos;
8189 1228 : ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
8190 614 : type, bitsiz[i],
8191 614 : bit_pos, 1, reversep, point[i]);
8192 614 : bit_pos += bitsiz[i];
8193 : }
8194 :
8195 307 : toshift[1] = toshift[0];
8196 307 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8197 : {
8198 3 : shifted[0] = bitsiz[1];
8199 3 : shifted[1] = 0;
8200 3 : toshift[0] = 0;
8201 : }
8202 : else
8203 : {
8204 304 : shifted[1] = bitsiz[0];
8205 304 : shifted[0] = 0;
8206 304 : toshift[1] = 0;
8207 : }
8208 307 : }
8209 :
8210 : /* Make arrangements to split at bit BOUNDARY a single loaded word
8211 : (with REVERSEP bit order) LN_ARG[0], to be shifted right by
8212 : TOSHIFT[0] to bring the field of interest to the least-significant
8213 : bit. The expectation is that the same loaded word will be
8214 : propagated from part 0 to part 1, with just different shifting and
8215 : masking to extract both parts. MASK is not expected to do more
8216 : than masking out the bits that belong to the other part. See
8217 : build_split_load for more information on the other fields. */
8218 :
8219 : static inline void
8220 51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
8221 : HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
8222 : HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
8223 : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
8224 : HOST_WIDE_INT /* out */ shifted[2],
8225 : wide_int /* out */ mask[2],
8226 : HOST_WIDE_INT boundary, bool reversep)
8227 : {
8228 51 : unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
8229 :
8230 51 : ln_arg[1] = ln_arg[0];
8231 51 : bitpos[1] = bitpos[0];
8232 51 : bitsiz[1] = bitsiz[0];
8233 51 : shifted[1] = shifted[0] = 0;
8234 :
8235 51 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8236 : {
8237 3 : toshift[1] = toshift[0];
8238 3 : toshift[0] = bitpos[0] + bitsiz[0] - boundary;
8239 3 : mask[0] = wi::mask (toshift[0], true, prec);
8240 3 : mask[1] = wi::mask (toshift[0], false, prec);
8241 : }
8242 : else
8243 : {
8244 48 : toshift[1] = boundary - bitpos[1];
8245 48 : mask[1] = wi::mask (toshift[1], true, prec);
8246 48 : mask[0] = wi::mask (toshift[1], false, prec);
8247 : }
8248 51 : }
8249 :
8250 : /* Find ways of folding logical expressions of LHS and RHS:
8251 :
8252 : Try to merge two comparisons to nearby fields.
8253 :
8254 : For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
8255 : at once, we can do this with a comparison against the object ANDed with the
8256 : a mask.
8257 :
8258 : If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
8259 : operations to do this with one comparison, loading both fields from P at
8260 : once, and likewise from Q.
8261 :
8262 : Herein, loading at once means loading from within the same alignment
8263 : boundary for the enclosing object. If (packed) fields cross such alignment
8264 : boundaries, we may still recombine the compares, so that loads do not cross
8265 : the boundaries.
8266 :
8267 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
8268 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
8269 :
8270 : TRUTH_TYPE is the type of the logical operand.
8271 :
8272 : LHS is denoted as LL_ARG LCODE LR_ARG.
8273 :
8274 : RHS is denoted as RL_ARG RCODE RR_ARG.
8275 :
8276 : LHS is assumed to dominate RHS.
8277 :
8278 : Combined loads are inserted next to preexisting loads, once we determine
8279 : that the combination is viable, and the combined condition references new
8280 : SSA_NAMEs that hold the loaded values. Since the original loads are
8281 : verified to have the same gimple_vuse, the insertion point doesn't matter
8282 : for correctness. ??? The loads may be a lot earlier than the compares, and
8283 : it's conceivable that one or two loads for RHS appear before those for LHS.
8284 : It could be advantageous to try to place the loads optimally, taking
8285 : advantage of knowing whether RHS is accessed before LHS, or that both are
8286 : accessed before both compares, but we don't do that (yet?).
8287 :
8288 : SEPARATEP should be NULL if the combined condition must be returned as a
8289 : single expression, even if it is a compound condition. This must only be
8290 : done if LHS and RHS are adjacent, without intervening conditions, and the
8291 : combined condition is to replace RHS, while LHS is dropped altogether.
8292 :
8293 : Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
8294 : replaced by a part of the compound condition that could replace RHS, while
8295 : the returned expression replaces LHS. This works whether or not LHS and RHS
8296 : are adjacent, as long as there aren't VDEFs or other side effects between
8297 : them.
8298 :
8299 : If the "words" accessed by RHS are already accessed by LHS, this won't
8300 : matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
8301 : be set to the compares that should take RHS's place. By "words" we mean
8302 : contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
8303 : object's type.
8304 :
8305 : We return the simplified tree or 0 if no optimization is possible. */
8306 :
8307 : tree
8308 438503 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
8309 : location_t lloc, enum tree_code lcode,
8310 : tree ll_arg, tree lr_arg,
8311 : location_t rloc, enum tree_code rcode,
8312 : tree rl_arg, tree rr_arg,
8313 : tree *separatep)
8314 : {
8315 : /* If this is the "or" of two comparisons, we can do something if
8316 : the comparisons are NE_EXPR. If this is the "and", we can do something
8317 : if the comparisons are EQ_EXPR. I.e.,
8318 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
8319 :
8320 : WANTED_CODE is this operation code. For single bit fields, we can
8321 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
8322 : comparison for one-bit fields. */
8323 :
8324 438503 : enum tree_code orig_code = code;
8325 438503 : enum tree_code wanted_code;
8326 438503 : tree ll_inner, lr_inner, rl_inner, rr_inner;
8327 438503 : gimple *ll_load, *lr_load, *rl_load, *rr_load;
8328 438503 : HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
8329 438503 : HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
8330 438503 : HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
8331 438503 : HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
8332 438503 : HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
8333 438503 : bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
8334 438503 : bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
8335 438503 : bool ll_signbit, lr_signbit, rl_signbit, rr_signbit;
8336 438503 : scalar_int_mode lnmode, lnmode2, rnmode;
8337 438503 : wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
8338 438503 : wide_int l_const, r_const;
8339 438503 : tree lntype, rntype, result;
8340 438503 : HOST_WIDE_INT first_bit, end_bit;
8341 438503 : bool volatilep;
8342 438503 : bool l_split_load;
8343 :
8344 : /* These are indexed by: conv, mask, shft, load. */
8345 438503 : location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
8346 438503 : location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
8347 438503 : location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8348 438503 : location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8349 :
8350 438503 : gcc_checking_assert (!separatep || !*separatep);
8351 :
8352 : /* Start by getting the comparison codes. Fail if anything is volatile.
8353 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
8354 : it were surrounded with a NE_EXPR. */
8355 :
8356 438503 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
8357 438503 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
8358 : return 0;
8359 :
8360 : /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
8361 : given by our caller to denote conditions from different blocks. */
8362 438503 : switch (code)
8363 : {
8364 : case TRUTH_AND_EXPR:
8365 : case TRUTH_ANDIF_EXPR:
8366 : code = TRUTH_AND_EXPR;
8367 : break;
8368 :
8369 0 : case TRUTH_OR_EXPR:
8370 0 : case TRUTH_ORIF_EXPR:
8371 0 : code = TRUTH_OR_EXPR;
8372 0 : break;
8373 :
8374 : default:
8375 : return 0;
8376 : }
8377 :
8378 : /* Prepare to turn compares of signed quantities with zero into sign-bit
8379 : tests. We need not worry about *_reversep here for these compare
8380 : rewrites: loads will have already been reversed before compares. Save the
8381 : precision, because [lr]l_arg may change and we won't be able to tell how
8382 : wide it was originally. */
8383 438503 : unsigned lsignbit = 0, rsignbit = 0;
8384 438503 : if ((lcode == LT_EXPR || lcode == GE_EXPR)
8385 15931 : && integer_zerop (lr_arg)
8386 3690 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8387 442193 : && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
8388 : {
8389 3690 : lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
8390 3690 : lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8391 : }
8392 : /* Turn compares of unsigned quantities with powers of two into
8393 : equality tests of masks. */
8394 434813 : else if ((lcode == LT_EXPR || lcode == GE_EXPR)
8395 12241 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8396 11483 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8397 8345 : && TREE_CODE (lr_arg) == INTEGER_CST
8398 434813 : && wi::popcount (wi::to_wide (lr_arg)) == 1)
8399 : {
8400 0 : ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
8401 0 : lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8402 0 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8403 : }
8404 : /* Turn compares of unsigned quantities with powers of two minus one
8405 : into equality tests of masks. */
8406 869626 : else if ((lcode == LE_EXPR || lcode == GT_EXPR)
8407 34585 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8408 34354 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8409 27766 : && TREE_CODE (lr_arg) == INTEGER_CST
8410 904211 : && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
8411 : {
8412 4086 : ll_and_mask = ~wi::to_wide (lr_arg);
8413 4086 : lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8414 4086 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8415 : }
8416 : /* Likewise for the second compare. */
8417 438503 : if ((rcode == LT_EXPR || rcode == GE_EXPR)
8418 22200 : && integer_zerop (rr_arg)
8419 2421 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8420 440924 : && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
8421 : {
8422 2421 : rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
8423 2421 : rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8424 : }
8425 436082 : else if ((rcode == LT_EXPR || rcode == GE_EXPR)
8426 19779 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8427 18648 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8428 3835 : && TREE_CODE (rr_arg) == INTEGER_CST
8429 436082 : && wi::popcount (wi::to_wide (rr_arg)) == 1)
8430 : {
8431 0 : rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
8432 0 : rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8433 0 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8434 : }
8435 872164 : else if ((rcode == LE_EXPR || rcode == GT_EXPR)
8436 44609 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8437 44326 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8438 32496 : && TREE_CODE (rr_arg) == INTEGER_CST
8439 916773 : && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
8440 : {
8441 3457 : rl_and_mask = ~wi::to_wide (rr_arg);
8442 3457 : rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8443 3457 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8444 : }
8445 :
8446 : /* See if the comparisons can be merged. Then get all the parameters for
8447 : each side. */
8448 :
8449 438503 : if ((lcode != EQ_EXPR && lcode != NE_EXPR)
8450 394674 : || (rcode != EQ_EXPR && rcode != NE_EXPR))
8451 : return 0;
8452 :
8453 367320 : ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
8454 367320 : volatilep = 0;
8455 367320 : bool l_xor = false, r_xor = false;
8456 367320 : ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
8457 : &ll_unsignedp, &ll_reversep, &volatilep,
8458 : &ll_and_mask, &ll_signbit,
8459 : &l_xor, &lr_arg, &lr_and_mask,
8460 : &ll_load, ll_loc);
8461 367320 : if (!ll_inner)
8462 : return 0;
8463 277874 : lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
8464 : &lr_unsignedp, &lr_reversep, &volatilep,
8465 : &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
8466 : &lr_load, lr_loc);
8467 277874 : if (!lr_inner)
8468 : return 0;
8469 273541 : rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
8470 : &rl_unsignedp, &rl_reversep, &volatilep,
8471 : &rl_and_mask, &rl_signbit,
8472 : &r_xor, &rr_arg, &rr_and_mask,
8473 : &rl_load, rl_loc);
8474 273541 : if (!rl_inner)
8475 : return 0;
8476 255410 : rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
8477 : &rr_unsignedp, &rr_reversep, &volatilep,
8478 : &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
8479 : &rr_load, rr_loc);
8480 255410 : if (!rr_inner)
8481 : return 0;
8482 :
8483 : /* It must be true that the inner operation on the lhs of each
8484 : comparison must be the same if we are to be able to do anything.
8485 : Then see if we have constants. If not, the same must be true for
8486 : the rhs's. If one is a load and the other isn't, we have to be
8487 : conservative and avoid the optimization, otherwise we could get
8488 : SRAed fields wrong. */
8489 253049 : if (volatilep)
8490 : return 0;
8491 :
8492 253048 : if (ll_reversep != rl_reversep
8493 253048 : || ! operand_equal_p (ll_inner, rl_inner, 0))
8494 : {
8495 : /* Try swapping the operands. */
8496 195820 : if (ll_reversep != rr_reversep || rsignbit
8497 390946 : || !operand_equal_p (ll_inner, rr_inner, 0))
8498 193984 : return 0;
8499 :
8500 1937 : rcode = swap_tree_comparison (rcode);
8501 1937 : std::swap (rl_arg, rr_arg);
8502 1937 : std::swap (rl_inner, rr_inner);
8503 1937 : std::swap (rl_bitsize, rr_bitsize);
8504 1937 : std::swap (rl_bitpos, rr_bitpos);
8505 1937 : std::swap (rl_unsignedp, rr_unsignedp);
8506 1937 : std::swap (rl_reversep, rr_reversep);
8507 1937 : std::swap (rl_and_mask, rr_and_mask);
8508 1937 : std::swap (rl_signbit, rr_signbit);
8509 1937 : std::swap (rl_load, rr_load);
8510 1937 : std::swap (rl_loc, rr_loc);
8511 : }
8512 :
8513 115910 : if ((ll_load && rl_load)
8514 229602 : ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
8515 2218 : : (!ll_load != !rl_load))
8516 : return 0;
8517 :
8518 : /* ??? Can we do anything with these? */
8519 58304 : if (lr_signbit || rr_signbit)
8520 : return 0;
8521 :
8522 : /* If the mask encompassed extensions of the sign bit before
8523 : clipping, try to include the sign bit in the test. If we're not
8524 : comparing with zero, don't even try to deal with it (for now?).
8525 : If we've already committed to a sign test, the extended (before
8526 : clipping) mask could already be messing with it. */
8527 58304 : if (ll_signbit)
8528 : {
8529 4 : if (!integer_zerop (lr_arg) || lsignbit)
8530 0 : return 0;
8531 4 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8532 4 : if (!ll_and_mask.get_precision ())
8533 0 : ll_and_mask = sign;
8534 : else
8535 4 : ll_and_mask |= sign;
8536 4 : }
8537 :
8538 58304 : if (rl_signbit)
8539 : {
8540 4 : if (!integer_zerop (rr_arg) || rsignbit)
8541 1 : return 0;
8542 3 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8543 3 : if (!rl_and_mask.get_precision ())
8544 0 : rl_and_mask = sign;
8545 : else
8546 3 : rl_and_mask |= sign;
8547 3 : }
8548 :
8549 58303 : if (TREE_CODE (lr_arg) == INTEGER_CST
8550 46227 : && TREE_CODE (rr_arg) == INTEGER_CST)
8551 : {
8552 45655 : l_const = wi::to_wide (lr_arg);
8553 : /* We don't expect masks on constants, but if there are any, apply
8554 : them now. */
8555 45655 : if (lr_and_mask.get_precision ())
8556 0 : l_const &= wide_int::from (lr_and_mask,
8557 0 : l_const.get_precision (), UNSIGNED);
8558 45655 : r_const = wi::to_wide (rr_arg);
8559 45655 : if (rr_and_mask.get_precision ())
8560 0 : r_const &= wide_int::from (rr_and_mask,
8561 0 : r_const.get_precision (), UNSIGNED);
8562 45655 : lr_reversep = ll_reversep;
8563 : }
8564 12648 : else if (lr_reversep != rr_reversep
8565 12640 : || ! operand_equal_p (lr_inner, rr_inner, 0)
8566 22017 : || ((lr_load && rr_load)
8567 27933 : ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
8568 58 : : (!lr_load != !rr_load)))
8569 3518 : return 0;
8570 :
8571 : /* If we found sign tests, finish turning them into bit tests. */
8572 :
8573 54785 : if (lsignbit)
8574 : {
8575 54 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8576 : /* If ll_arg is zero-extended and we're testing the sign bit, we know
8577 : what the result should be. Shifting the sign bit out of sign will get
8578 : us to mask the entire field out, yielding zero, i.e., the sign bit of
8579 : the zero-extended value. We know the masked value is being compared
8580 : with zero, so the compare will get us the result we're looking
8581 : for: TRUE if EQ_EXPR, FALSE if NE_EXPR. */
8582 54 : if (lsignbit > ll_bitsize && ll_unsignedp)
8583 1 : sign <<= 1;
8584 54 : if (!ll_and_mask.get_precision ())
8585 53 : ll_and_mask = sign;
8586 : else
8587 1 : ll_and_mask &= sign;
8588 54 : if (l_xor)
8589 : {
8590 1 : if (ll_bitsize != lr_bitsize)
8591 1 : return 0;
8592 0 : if (!lr_and_mask.get_precision ())
8593 0 : lr_and_mask = sign;
8594 : else
8595 0 : lr_and_mask &= sign;
8596 0 : if (l_const.get_precision ())
8597 0 : l_const &= wide_int::from (lr_and_mask,
8598 0 : l_const.get_precision (), UNSIGNED);
8599 : }
8600 54 : }
8601 :
8602 54784 : if (rsignbit)
8603 : {
8604 171 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8605 171 : if (rsignbit > rl_bitsize && rl_unsignedp)
8606 0 : sign <<= 1;
8607 171 : if (!rl_and_mask.get_precision ())
8608 171 : rl_and_mask = sign;
8609 : else
8610 0 : rl_and_mask &= sign;
8611 171 : if (r_xor)
8612 : {
8613 16 : if (rl_bitsize != rr_bitsize)
8614 0 : return 0;
8615 16 : if (!rr_and_mask.get_precision ())
8616 16 : rr_and_mask = sign;
8617 : else
8618 0 : rr_and_mask &= sign;
8619 16 : if (r_const.get_precision ())
8620 24 : r_const &= wide_int::from (rr_and_mask,
8621 12 : r_const.get_precision (), UNSIGNED);
8622 : }
8623 171 : }
8624 :
8625 : /* If either comparison code is not correct for our logical operation,
8626 : fail. However, we can convert a one-bit comparison against zero into
8627 : the opposite comparison against that bit being set in the field. */
8628 :
8629 54784 : wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
8630 54784 : if (lcode != wanted_code)
8631 : {
8632 5074 : if (l_const.get_precision ()
8633 4900 : && l_const == 0
8634 1661 : && ll_and_mask.get_precision ()
8635 5466 : && wi::popcount (ll_and_mask) == 1)
8636 : {
8637 : /* Make the left operand unsigned, since we are only interested
8638 : in the value of one bit. Otherwise we are doing the wrong
8639 : thing below. */
8640 235 : ll_unsignedp = 1;
8641 235 : l_const = ll_and_mask;
8642 : }
8643 : else
8644 4839 : return 0;
8645 : }
8646 :
8647 : /* This is analogous to the code for l_const above. */
8648 49945 : if (rcode != wanted_code)
8649 : {
8650 982 : if (r_const.get_precision ()
8651 982 : && r_const == 0
8652 943 : && rl_and_mask.get_precision ()
8653 1849 : && wi::popcount (rl_and_mask) == 1)
8654 : {
8655 534 : rl_unsignedp = 1;
8656 534 : r_const = rl_and_mask;
8657 : }
8658 : else
8659 448 : return 0;
8660 : }
8661 :
8662 : /* This will be bumped to 2 if any of the field pairs crosses an
8663 : alignment boundary, so the merged compare has to be done in two
8664 : parts. */
8665 148491 : int parts = 1;
8666 : /* Set to true if the second combined compare should come first,
8667 : e.g., because the second original compare accesses a word that
8668 : the first one doesn't, and the combined compares access those in
8669 : cmp[0]. */
8670 148491 : bool first1 = false;
8671 : /* Set to true if the first original compare is not the one being
8672 : split. */
8673 148491 : bool maybe_separate = false;
8674 :
8675 : /* The following 2-dimensional arrays use the first index to
8676 : identify left(0)- vs right(1)-hand compare operands, and the
8677 : second one to identify merged compare parts. */
8678 : /* The memory loads or constants to be compared. */
8679 : tree ld_arg[2][2];
8680 : /* The first bit of the corresponding inner object that the
8681 : corresponding LD_ARG covers. */
8682 : HOST_WIDE_INT bitpos[2][2];
8683 : /* The bit count starting at BITPOS that the corresponding LD_ARG
8684 : covers. */
8685 : HOST_WIDE_INT bitsiz[2][2];
8686 : /* The number of bits by which LD_ARG has already been shifted
8687 : right, WRT mask. */
8688 : HOST_WIDE_INT shifted[2][2];
8689 : /* The number of bits by which both LD_ARG and MASK need shifting to
8690 : bring its least-significant bit to bit zero. */
8691 : HOST_WIDE_INT toshift[2][2];
8692 : /* An additional mask to be applied to LD_ARG, to remove any bits
8693 : that may have been loaded for use in another compare, but that
8694 : don't belong in the corresponding compare. */
8695 593964 : wide_int xmask[2][2] = {};
8696 :
8697 : /* The combined compare or compares. */
8698 49497 : tree cmp[2];
8699 :
8700 : /* Consider we're comparing two non-contiguous fields of packed
8701 : structs, both aligned at 32-bit boundaries:
8702 :
8703 : ll_arg: an 8-bit field at offset 0
8704 : lr_arg: a 16-bit field at offset 2
8705 :
8706 : rl_arg: an 8-bit field at offset 1
8707 : rr_arg: a 16-bit field at offset 3
8708 :
8709 : We'll have r_split_load, because rr_arg straddles across an
8710 : alignment boundary.
8711 :
8712 : We'll want to have:
8713 :
8714 : bitpos = { { 0, 0 }, { 0, 32 } }
8715 : bitsiz = { { 32, 32 }, { 32, 8 } }
8716 :
8717 : And, for little-endian:
8718 :
8719 : shifted = { { 0, 0 }, { 0, 32 } }
8720 : toshift = { { 0, 24 }, { 0, 0 } }
8721 :
8722 : Or, for big-endian:
8723 :
8724 : shifted = { { 0, 0 }, { 8, 0 } }
8725 : toshift = { { 8, 0 }, { 0, 0 } }
8726 : */
8727 :
8728 : /* See if we can find a mode that contains both fields being compared on
8729 : the left. If we can't, fail. Otherwise, update all constants and masks
8730 : to be relative to a field of that size. */
8731 49497 : first_bit = MIN (ll_bitpos, rl_bitpos);
8732 49497 : end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
8733 49497 : HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
8734 49497 : poly_uint64 ll_end_region = 0;
8735 49497 : if (TYPE_SIZE (TREE_TYPE (ll_inner))
8736 49497 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
8737 49497 : ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
8738 49497 : if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
8739 49497 : ll_align, BITS_PER_WORD, volatilep, &lnmode))
8740 : l_split_load = false;
8741 : /* ??? If ll and rl share the same load, reuse that?
8742 : See PR 118206 -> gcc.dg/field-merge-18.c */
8743 : else
8744 : {
8745 : /* Consider the possibility of recombining loads if any of the
8746 : fields straddles across an alignment boundary, so that either
8747 : part can be loaded along with the other field. Since we
8748 : limit access modes to BITS_PER_WORD, don't exceed that,
8749 : otherwise on a 32-bit host and a 64-bit-aligned data
8750 : structure, we'll fail the above for a field that straddles
8751 : across two words, and would fail here for not even trying to
8752 : split it at between 32-bit words. */
8753 46304 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8754 49154 : (MIN (ll_align, BITS_PER_WORD),
8755 : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8756 :
8757 46304 : if (boundary < 0
8758 219 : || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
8759 : ll_align, BITS_PER_WORD, volatilep, &lnmode)
8760 46481 : || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
8761 177 : ll_align, BITS_PER_WORD, volatilep, &lnmode2))
8762 : {
8763 48935 : if (ll_align <= BITS_PER_WORD)
8764 : return 0;
8765 :
8766 : /* As a last resort, try double-word access modes. This
8767 : enables us to deal with misaligned double-word fields
8768 : that straddle across 3 separate words. */
8769 1859 : boundary = compute_split_boundary_from_align
8770 1951 : (MIN (ll_align, 2 * BITS_PER_WORD),
8771 : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8772 1859 : if (boundary < 0
8773 0 : || !get_best_mode (boundary - first_bit, first_bit,
8774 : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8775 : volatilep, &lnmode)
8776 1859 : || !get_best_mode (end_bit - boundary, boundary,
8777 0 : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8778 : volatilep, &lnmode2))
8779 1859 : return 0;
8780 : }
8781 :
8782 : /* If we can't have a single load, but can with two, figure out whether
8783 : the two compares can be separated, i.e., whether the entirety of the
8784 : first original compare is encompassed by the entirety of the first
8785 : combined compare. If the first original compare is past the alignment
8786 : boundary, arrange to compare that range first, by setting first1
8787 : (meaning make cmp[1] first, instead of cmp[0]). */
8788 177 : l_split_load = true;
8789 177 : parts = 2;
8790 177 : if (ll_bitpos >= boundary)
8791 : maybe_separate = first1 = true;
8792 132 : else if (ll_bitpos + ll_bitsize <= boundary)
8793 32 : maybe_separate = true;
8794 : }
8795 :
8796 3370 : lnbitsize = GET_MODE_BITSIZE (lnmode);
8797 3370 : lnbitpos = first_bit & ~ (lnbitsize - 1);
8798 : /* Avoid situations that the code below can't handle. */
8799 3370 : if (lnbitpos < 0)
8800 : return 0;
8801 :
8802 : /* Choose the type for the combined compare. Even if we're splitting loads,
8803 : make it wide enough to hold both. */
8804 3370 : if (l_split_load)
8805 354 : lnbitsize += GET_MODE_BITSIZE (lnmode2);
8806 3370 : lntype = build_nonstandard_integer_type (lnbitsize, 1);
8807 3370 : if (!lntype)
8808 : return NULL_TREE;
8809 3370 : lnprec = TYPE_PRECISION (lntype);
8810 3370 : xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
8811 :
8812 : /* Adjust bit ranges for reverse endianness. */
8813 3370 : if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8814 : {
8815 6 : xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
8816 6 : xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
8817 : }
8818 :
8819 : /* Adjust masks to match the positions in the combined lntype. */
8820 6740 : wide_int ll_mask, rl_mask, r_mask;
8821 3370 : if (ll_and_mask.get_precision ())
8822 4300 : ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
8823 2150 : xll_bitpos);
8824 : else
8825 1220 : ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
8826 3370 : if (rl_and_mask.get_precision ())
8827 3968 : rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
8828 1984 : xrl_bitpos);
8829 : else
8830 1386 : rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
8831 :
8832 : /* When we set l_const, we also set r_const. */
8833 3370 : gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
8834 :
8835 : /* Adjust right-hand constants in both original comparisons to match width
8836 : and bit position. */
8837 3370 : if (l_const.get_precision ())
8838 : {
8839 : /* Before clipping upper bits of the right-hand operand of the compare,
8840 : check that they're sign or zero extensions, depending on how the
8841 : left-hand operand would be extended. If it is unsigned, or if there's
8842 : a mask that zeroes out extension bits, whether because we've checked
8843 : for upper bits in the mask and did not set ll_signbit, or because the
8844 : sign bit itself is masked out, check that the right-hand operand is
8845 : zero-extended. */
8846 1978 : bool l_non_ext_bits = false;
8847 1978 : if (ll_bitsize < lr_bitsize)
8848 : {
8849 44 : wide_int zext = wi::zext (l_const, ll_bitsize);
8850 88 : if ((ll_unsignedp
8851 32 : || (ll_and_mask.get_precision ()
8852 4 : && (!ll_signbit
8853 52 : || ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
8854 8 : == 0)))
8855 164 : ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
8856 44 : l_const = zext;
8857 : else
8858 : l_non_ext_bits = true;
8859 44 : }
8860 : /* We're doing bitwise equality tests, so don't bother with sign
8861 : extensions. */
8862 1978 : l_const = wide_int::from (l_const, lnprec, UNSIGNED);
8863 1978 : if (ll_and_mask.get_precision ())
8864 1163 : l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
8865 1978 : l_const <<= xll_bitpos;
8866 5934 : if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
8867 : {
8868 0 : warning_at (lloc, OPT_Wtautological_compare,
8869 : "comparison is always %d", wanted_code == NE_EXPR);
8870 :
8871 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8872 : }
8873 :
8874 : /* Before clipping upper bits of the right-hand operand of the compare,
8875 : check that they're sign or zero extensions, depending on how the
8876 : left-hand operand would be extended. */
8877 1978 : bool r_non_ext_bits = false;
8878 1978 : if (rl_bitsize < rr_bitsize)
8879 : {
8880 15 : wide_int zext = wi::zext (r_const, rl_bitsize);
8881 30 : if ((rl_unsignedp
8882 11 : || (rl_and_mask.get_precision ()
8883 4 : && (!rl_signbit
8884 21 : || ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
8885 6 : == 0)))
8886 56 : ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
8887 15 : r_const = zext;
8888 : else
8889 : r_non_ext_bits = true;
8890 15 : }
8891 1978 : r_const = wide_int::from (r_const, lnprec, UNSIGNED);
8892 1978 : if (rl_and_mask.get_precision ())
8893 1038 : r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
8894 1978 : r_const <<= xrl_bitpos;
8895 5934 : if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
8896 : {
8897 0 : warning_at (rloc, OPT_Wtautological_compare,
8898 : "comparison is always %d", wanted_code == NE_EXPR);
8899 :
8900 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8901 : }
8902 :
8903 : /* If there is something in common between the masks, those bits of the
8904 : constants must be the same. If not, the combined condition cannot be
8905 : met, and the result is known. Test for this to avoid generating
8906 : incorrect code below. */
8907 1978 : wide_int mask = ll_mask & rl_mask;
8908 1978 : if (mask != 0
8909 2026 : && (l_const & mask) != (r_const & mask))
8910 : {
8911 0 : if (wanted_code == NE_EXPR)
8912 0 : return constant_boolean_node (true, truth_type);
8913 : else
8914 0 : return constant_boolean_node (false, truth_type);
8915 : }
8916 :
8917 : /* The constants are combined so as to line up with the loaded field, so
8918 : tentatively use the same parameters for the second combined
8919 : compare. */
8920 1978 : ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
8921 1978 : toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
8922 1978 : shifted[1][0] = 0;
8923 1978 : bitpos[1][0] = lnbitpos;
8924 1978 : bitsiz[1][0] = lnbitsize;
8925 :
8926 1978 : if (parts > 1)
8927 49 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8928 : shifted[1], xmask[1],
8929 49 : lnbitpos + GET_MODE_BITSIZE (lnmode),
8930 : lr_reversep);
8931 :
8932 : /* No masking needed, we know the full constants. */
8933 1978 : r_mask = wi::mask (0, true, lnprec);
8934 :
8935 : /* If the compiler thinks this is used uninitialized below, it's
8936 : because it can't realize that parts can only be 2 when
8937 : comparing with constants if l_split_load is also true. This
8938 : just silences the warning. */
8939 1978 : rnbitpos = 0;
8940 1978 : }
8941 :
8942 : /* Likewise, if the right sides are not constant, align them for the combined
8943 : compare. Also, disallow this optimization if a size, signedness or
8944 : storage order mismatch occurs between the left and right sides. */
8945 : else
8946 : {
8947 1392 : if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
8948 1306 : || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
8949 1306 : || ll_reversep != lr_reversep
8950 : /* Make sure the two fields on the right
8951 : correspond to the left without being swapped. */
8952 1306 : || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
8953 217 : return 0;
8954 :
8955 1183 : bool r_split_load;
8956 1183 : scalar_int_mode rnmode2;
8957 :
8958 : /* Figure out how to load the bits for the right-hand size of the
8959 : combined compare. As in the left-hand size, we may have to split it,
8960 : and then we use two separate compares. */
8961 1183 : first_bit = MIN (lr_bitpos, rr_bitpos);
8962 1183 : end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
8963 1183 : HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
8964 1183 : poly_uint64 lr_end_region = 0;
8965 1183 : if (TYPE_SIZE (TREE_TYPE (lr_inner))
8966 1183 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
8967 1183 : lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
8968 1183 : if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
8969 1183 : lr_align, BITS_PER_WORD, volatilep, &rnmode))
8970 : {
8971 : /* Consider the possibility of recombining loads if any of the
8972 : fields straddles across an alignment boundary, so that either
8973 : part can be loaded along with the other field. */
8974 138 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8975 138 : (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
8976 :
8977 138 : if (boundary < 0
8978 : /* If we're to split both, make sure the split point is
8979 : the same. */
8980 130 : || (l_split_load
8981 128 : && (boundary - lr_bitpos
8982 128 : != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
8983 130 : || !get_best_mode (boundary - first_bit, first_bit,
8984 : 0, lr_end_region,
8985 130 : lr_align, BITS_PER_WORD, volatilep, &rnmode)
8986 268 : || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
8987 130 : lr_align, BITS_PER_WORD, volatilep, &rnmode2))
8988 8 : return 0;
8989 :
8990 130 : r_split_load = true;
8991 130 : parts = 2;
8992 130 : if (lr_bitpos >= boundary)
8993 : maybe_separate = first1 = true;
8994 88 : else if (lr_bitpos + lr_bitsize <= boundary)
8995 29 : maybe_separate = true;
8996 : }
8997 : else
8998 : r_split_load = false;
8999 :
9000 : /* Find a type that can hold the entire right-hand operand. */
9001 1175 : rnbitsize = GET_MODE_BITSIZE (rnmode);
9002 1175 : rnbitpos = first_bit & ~ (rnbitsize - 1);
9003 1175 : if (r_split_load)
9004 260 : rnbitsize += GET_MODE_BITSIZE (rnmode2);
9005 1175 : rntype = build_nonstandard_integer_type (rnbitsize, 1);
9006 1175 : if (!rntype)
9007 : return 0;
9008 1175 : rnprec = TYPE_PRECISION (rntype);
9009 1175 : xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
9010 :
9011 : /* Adjust for reversed endianness. */
9012 1175 : if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
9013 : {
9014 0 : xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
9015 0 : xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
9016 : }
9017 :
9018 : /* Adjust the masks to match the combined type, and combine them. */
9019 1175 : wide_int lr_mask, rr_mask;
9020 1175 : if (lr_and_mask.get_precision ())
9021 1972 : lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
9022 986 : xlr_bitpos);
9023 : else
9024 189 : lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
9025 1175 : if (rr_and_mask.get_precision ())
9026 1890 : rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
9027 945 : xrr_bitpos);
9028 : else
9029 230 : rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
9030 1175 : r_mask = lr_mask | rr_mask;
9031 :
9032 : /* Load the right-hand operand of the combined compare. */
9033 1175 : toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
9034 1175 : shifted[1][0] = 0;
9035 :
9036 1175 : if (!r_split_load)
9037 : {
9038 1045 : bitpos[1][0] = rnbitpos;
9039 1045 : bitsiz[1][0] = rnbitsize;
9040 1045 : ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
9041 1045 : rntype, rnbitsize, rnbitpos,
9042 1045 : lr_unsignedp || rr_unsignedp,
9043 : lr_reversep, lr_load);
9044 : }
9045 :
9046 : /* ... and the second part of the right-hand operand if needed. */
9047 1175 : if (parts > 1)
9048 : {
9049 130 : if (r_split_load)
9050 : {
9051 130 : gimple *point[2];
9052 130 : point[0] = lr_load;
9053 130 : point[1] = rr_load;
9054 130 : build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
9055 : shifted[1], rl_loc[3], lr_inner, lr_arg,
9056 : rnmode, rnmode2, rnbitpos, lr_reversep, point);
9057 : }
9058 : else
9059 0 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
9060 : shifted[1], xmask[1],
9061 0 : lnbitpos + GET_MODE_BITSIZE (lnmode)
9062 0 : - ll_bitpos + lr_bitpos, lr_reversep);
9063 : }
9064 1175 : }
9065 :
9066 : /* Now issue the loads for the left-hand combined operand/s. */
9067 6306 : wide_int l_mask = ll_mask | rl_mask;
9068 3153 : toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
9069 3153 : shifted[0][0] = 0;
9070 :
9071 3153 : if (!l_split_load)
9072 : {
9073 2976 : bitpos[0][0] = lnbitpos;
9074 2976 : bitsiz[0][0] = lnbitsize;
9075 2976 : ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
9076 2976 : lntype, lnbitsize, lnbitpos,
9077 2976 : ll_unsignedp || rl_unsignedp,
9078 : ll_reversep, ll_load);
9079 : }
9080 :
9081 3153 : if (parts > 1)
9082 : {
9083 179 : if (l_split_load)
9084 : {
9085 177 : gimple *point[2];
9086 177 : point[0] = ll_load;
9087 177 : point[1] = rl_load;
9088 177 : build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
9089 : shifted[0], rl_loc[3], ll_inner, ll_arg,
9090 : lnmode, lnmode2, lnbitpos, ll_reversep, point);
9091 : }
9092 : else
9093 2 : reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
9094 : shifted[0], xmask[0],
9095 2 : rnbitpos + GET_MODE_BITSIZE (rnmode)
9096 2 : - lr_bitpos + ll_bitpos, ll_reversep);
9097 : }
9098 :
9099 : /* Compute the compares. */
9100 6485 : for (int i = 0; i < parts; i++)
9101 : {
9102 3332 : tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
9103 9996 : wide_int mask[2] = { l_mask, r_mask };
9104 3332 : location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
9105 :
9106 : /* Figure out the masks, and unshare the original operands. */
9107 9996 : for (int j = 0; j < 2; j++)
9108 : {
9109 6664 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
9110 6664 : op[j] = unshare_expr (op[j]);
9111 :
9112 : /* Mask out the bits belonging to the other part. */
9113 6664 : if (xmask[j][i].get_precision ())
9114 102 : mask[j] &= xmask[j][i];
9115 :
9116 6664 : if (shifted[j][i])
9117 : {
9118 307 : wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
9119 307 : mask[j] = wi::lrshift (mask[j], shift);
9120 307 : }
9121 6664 : mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
9122 : }
9123 :
9124 : /* Line up the operands for a compare. */
9125 3332 : HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
9126 :
9127 3332 : if (shift)
9128 : {
9129 54 : int j;
9130 54 : if (shift > 0)
9131 : j = 0;
9132 : else
9133 : {
9134 52 : j = 1;
9135 52 : shift = -shift;
9136 : }
9137 :
9138 54 : tree shiftsz = bitsize_int (shift);
9139 54 : op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
9140 : op[j], shiftsz);
9141 54 : mask[j] = wi::lrshift (mask[j], shift);
9142 : }
9143 :
9144 : /* Convert to the smaller type before masking out unwanted
9145 : bits. */
9146 3332 : tree type = TREE_TYPE (op[0]);
9147 3332 : if (type != TREE_TYPE (op[1]))
9148 : {
9149 188 : int j = (TYPE_PRECISION (type)
9150 188 : < TYPE_PRECISION (TREE_TYPE (op[1])));
9151 188 : if (!j)
9152 89 : type = TREE_TYPE (op[1]);
9153 188 : op[j] = fold_convert_loc (locs[j][0], type, op[j]);
9154 188 : mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
9155 : }
9156 :
9157 : /* Apply masks. */
9158 9996 : for (int j = 0; j < 2; j++)
9159 6664 : if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
9160 2688 : op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
9161 5376 : op[j], wide_int_to_tree (type, mask[j]));
9162 :
9163 6485 : cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
9164 : op[0], op[1]);
9165 9996 : }
9166 :
9167 : /* Reorder the compares if needed. */
9168 3153 : if (first1)
9169 45 : std::swap (cmp[0], cmp[1]);
9170 :
9171 : /* Prepare to return the resulting compares. Combine two parts if
9172 : needed. */
9173 3153 : if (parts == 1)
9174 2974 : result = cmp[0];
9175 179 : else if (!separatep || !maybe_separate)
9176 : {
9177 : /* Only fold if any of the cmp is known, otherwise we may lose the
9178 : sequence point, and that may prevent further optimizations. */
9179 173 : if (TREE_CODE (cmp[0]) == INTEGER_CST
9180 137 : || TREE_CODE (cmp[1]) == INTEGER_CST)
9181 37 : result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
9182 : else
9183 136 : result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
9184 : }
9185 : else
9186 : {
9187 6 : result = cmp[0];
9188 6 : *separatep = cmp[1];
9189 : }
9190 :
9191 3153 : return result;
9192 438503 : }
9193 :
9194 : /* Try to simplify the AND of two comparisons, specified by
9195 : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9196 : If this can be simplified to a single expression (without requiring
9197 : introducing more SSA variables to hold intermediate values),
9198 : return the resulting tree. Otherwise return NULL_TREE.
9199 : If the result expression is non-null, it has boolean type. */
9200 :
9201 : tree
9202 632956 : maybe_fold_and_comparisons (tree type,
9203 : enum tree_code code1, tree op1a, tree op1b,
9204 : enum tree_code code2, tree op2a, tree op2b,
9205 : basic_block outer_cond_bb)
9206 : {
9207 632956 : if (tree t = and_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9208 : outer_cond_bb))
9209 : return t;
9210 :
9211 631242 : if (tree t = and_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9212 : outer_cond_bb))
9213 : return t;
9214 :
9215 631216 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
9216 : op1a, op1b, code2, op2a,
9217 : op2b, outer_cond_bb))
9218 : return t;
9219 :
9220 : return NULL_TREE;
9221 : }
9222 :
9223 : /* Helper function for or_comparisons_1: try to simplify the OR of the
9224 : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
9225 : If INVERT is true, invert the value of VAR before doing the OR.
9226 : Return NULL_EXPR if we can't simplify this to a single expression. */
9227 :
9228 : static tree
9229 63478 : or_var_with_comparison (tree type, tree var, bool invert,
9230 : enum tree_code code2, tree op2a, tree op2b,
9231 : basic_block outer_cond_bb)
9232 : {
9233 63478 : tree t;
9234 63478 : gimple *stmt = SSA_NAME_DEF_STMT (var);
9235 :
9236 : /* We can only deal with variables whose definitions are assignments. */
9237 63478 : if (!is_gimple_assign (stmt))
9238 : return NULL_TREE;
9239 :
9240 : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
9241 : !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
9242 : Then we only have to consider the simpler non-inverted cases. */
9243 63298 : if (invert)
9244 24211 : t = and_var_with_comparison_1 (type, stmt,
9245 : invert_tree_comparison (code2, false),
9246 : op2a, op2b, outer_cond_bb);
9247 : else
9248 39087 : t = or_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
9249 : outer_cond_bb);
9250 63298 : return canonicalize_bool (t, invert);
9251 : }
9252 :
9253 : /* Try to simplify the OR of the ssa variable defined by the assignment
9254 : STMT with the comparison specified by (OP2A CODE2 OP2B).
9255 : Return NULL_EXPR if we can't simplify this to a single expression. */
9256 :
9257 : static tree
9258 163444 : or_var_with_comparison_1 (tree type, gimple *stmt,
9259 : enum tree_code code2, tree op2a, tree op2b,
9260 : basic_block outer_cond_bb)
9261 : {
9262 163444 : tree var = gimple_assign_lhs (stmt);
9263 163444 : tree true_test_var = NULL_TREE;
9264 163444 : tree false_test_var = NULL_TREE;
9265 163444 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
9266 :
9267 : /* Check for identities like (var OR (var != 0)) => true . */
9268 163444 : if (TREE_CODE (op2a) == SSA_NAME
9269 163444 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
9270 : {
9271 34923 : if ((code2 == NE_EXPR && integer_zerop (op2b))
9272 93091 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
9273 : {
9274 28446 : true_test_var = op2a;
9275 28446 : if (var == true_test_var)
9276 : return var;
9277 : }
9278 6427 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
9279 52755 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
9280 : {
9281 13069 : false_test_var = op2a;
9282 13069 : if (var == false_test_var)
9283 0 : return boolean_true_node;
9284 : }
9285 : }
9286 :
9287 : /* If the definition is a comparison, recurse on it. */
9288 163444 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
9289 : {
9290 973 : tree t = or_comparisons_1 (type, innercode,
9291 : gimple_assign_rhs1 (stmt),
9292 : gimple_assign_rhs2 (stmt),
9293 : code2, op2a, op2b, outer_cond_bb);
9294 973 : if (t)
9295 : return t;
9296 : }
9297 :
9298 : /* If the definition is an AND or OR expression, we may be able to
9299 : simplify by reassociating. */
9300 163421 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
9301 163421 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
9302 : {
9303 73676 : tree inner1 = gimple_assign_rhs1 (stmt);
9304 73676 : tree inner2 = gimple_assign_rhs2 (stmt);
9305 73676 : gimple *s;
9306 73676 : tree t;
9307 73676 : tree partial = NULL_TREE;
9308 73676 : bool is_or = (innercode == BIT_IOR_EXPR);
9309 :
9310 : /* Check for boolean identities that don't require recursive examination
9311 : of inner1/inner2:
9312 : inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
9313 : inner1 OR (inner1 AND inner2) => inner1
9314 : !inner1 OR (inner1 OR inner2) => true
9315 : !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
9316 : */
9317 73676 : if (inner1 == true_test_var)
9318 0 : return (is_or ? var : inner1);
9319 73676 : else if (inner2 == true_test_var)
9320 0 : return (is_or ? var : inner2);
9321 73676 : else if (inner1 == false_test_var)
9322 0 : return (is_or
9323 0 : ? boolean_true_node
9324 0 : : or_var_with_comparison (type, inner2, false, code2, op2a,
9325 0 : op2b, outer_cond_bb));
9326 73676 : else if (inner2 == false_test_var)
9327 0 : return (is_or
9328 0 : ? boolean_true_node
9329 0 : : or_var_with_comparison (type, inner1, false, code2, op2a,
9330 0 : op2b, outer_cond_bb));
9331 :
9332 : /* Next, redistribute/reassociate the OR across the inner tests.
9333 : Compute the first partial result, (inner1 OR (op2a code op2b)) */
9334 73676 : if (TREE_CODE (inner1) == SSA_NAME
9335 73676 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
9336 72405 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9337 127853 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9338 : gimple_assign_rhs1 (s),
9339 : gimple_assign_rhs2 (s),
9340 : code2, op2a, op2b,
9341 : outer_cond_bb)))
9342 : {
9343 : /* Handle the OR case, where we are reassociating:
9344 : (inner1 OR inner2) OR (op2a code2 op2b)
9345 : => (t OR inner2)
9346 : If the partial result t is a constant, we win. Otherwise
9347 : continue on to try reassociating with the other inner test. */
9348 5989 : if (is_or)
9349 : {
9350 94 : if (integer_onep (t))
9351 0 : return boolean_true_node;
9352 94 : else if (integer_zerop (t))
9353 : return inner2;
9354 : }
9355 :
9356 : /* Handle the AND case, where we are redistributing:
9357 : (inner1 AND inner2) OR (op2a code2 op2b)
9358 : => (t AND (inner2 OR (op2a code op2b))) */
9359 5895 : else if (integer_zerop (t))
9360 0 : return boolean_false_node;
9361 :
9362 : /* Save partial result for later. */
9363 : partial = t;
9364 : }
9365 :
9366 : /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
9367 73676 : if (TREE_CODE (inner2) == SSA_NAME
9368 73676 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
9369 72777 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9370 142934 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9371 : gimple_assign_rhs1 (s),
9372 : gimple_assign_rhs2 (s),
9373 : code2, op2a, op2b,
9374 : outer_cond_bb)))
9375 : {
9376 : /* Handle the OR case, where we are reassociating:
9377 : (inner1 OR inner2) OR (op2a code2 op2b)
9378 : => (inner1 OR t)
9379 : => (t OR partial) */
9380 5567 : if (is_or)
9381 : {
9382 75 : if (integer_zerop (t))
9383 : return inner1;
9384 75 : else if (integer_onep (t))
9385 1 : return boolean_true_node;
9386 : /* If both are the same, we can apply the identity
9387 : (x OR x) == x. */
9388 74 : else if (partial && same_bool_result_p (t, partial))
9389 : return t;
9390 : }
9391 :
9392 : /* Handle the AND case, where we are redistributing:
9393 : (inner1 AND inner2) OR (op2a code2 op2b)
9394 : => (t AND (inner1 OR (op2a code2 op2b)))
9395 : => (t AND partial) */
9396 : else
9397 : {
9398 5492 : if (integer_zerop (t))
9399 0 : return boolean_false_node;
9400 5492 : else if (partial)
9401 : {
9402 : /* We already got a simplification for the other
9403 : operand to the redistributed AND expression. The
9404 : interesting case is when at least one is true.
9405 : Or, if both are the same, we can apply the identity
9406 : (x AND x) == x. */
9407 19 : if (integer_onep (partial))
9408 : return t;
9409 18 : else if (integer_onep (t))
9410 : return partial;
9411 4 : else if (same_bool_result_p (t, partial))
9412 : return t;
9413 : }
9414 : }
9415 : }
9416 : }
9417 : return NULL_TREE;
9418 : }
9419 :
9420 : /* Try to simplify the OR of two comparisons defined by
9421 : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
9422 : If this can be done without constructing an intermediate value,
9423 : return the resulting tree; otherwise NULL_TREE is returned.
9424 : This function is deliberately asymmetric as it recurses on SSA_DEFs
9425 : in the first comparison but not the second. */
9426 :
9427 : static tree
9428 1152209 : or_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
9429 : enum tree_code code2, tree op2a, tree op2b,
9430 : basic_block outer_cond_bb)
9431 : {
9432 1152209 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
9433 :
9434 : /* First check for ((x CODE1 y) OR (x CODE2 y)). */
9435 1152209 : if (operand_equal_p (op1a, op2a, 0)
9436 1152209 : && operand_equal_p (op1b, op2b, 0))
9437 : {
9438 : /* Result will be either NULL_TREE, or a combined comparison. */
9439 13177 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9440 : TRUTH_ORIF_EXPR, code1, code2,
9441 : truth_type, op1a, op1b);
9442 13177 : if (t)
9443 : return t;
9444 : }
9445 :
9446 : /* Likewise the swapped case of the above. */
9447 1139064 : if (operand_equal_p (op1a, op2b, 0)
9448 1139064 : && operand_equal_p (op1b, op2a, 0))
9449 : {
9450 : /* Result will be either NULL_TREE, or a combined comparison. */
9451 0 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9452 : TRUTH_ORIF_EXPR, code1,
9453 : swap_tree_comparison (code2),
9454 : truth_type, op1a, op1b);
9455 0 : if (t)
9456 : return t;
9457 : }
9458 :
9459 : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
9460 : NAME's definition is a truth value. See if there are any simplifications
9461 : that can be done against the NAME's definition. */
9462 1139064 : if (TREE_CODE (op1a) == SSA_NAME
9463 1138645 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
9464 1486025 : && (integer_zerop (op1b) || integer_onep (op1b)))
9465 : {
9466 45951 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
9467 107118 : || (code1 == NE_EXPR && integer_onep (op1b)));
9468 102935 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
9469 102935 : switch (gimple_code (stmt))
9470 : {
9471 63271 : case GIMPLE_ASSIGN:
9472 : /* Try to simplify by copy-propagating the definition. */
9473 63271 : return or_var_with_comparison (type, op1a, invert, code2, op2a,
9474 63271 : op2b, outer_cond_bb);
9475 :
9476 15229 : case GIMPLE_PHI:
9477 : /* If every argument to the PHI produces the same result when
9478 : ORed with the second comparison, we win.
9479 : Do not do this unless the type is bool since we need a bool
9480 : result here anyway. */
9481 15229 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
9482 : {
9483 : tree result = NULL_TREE;
9484 : unsigned i;
9485 1003 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
9486 : {
9487 1003 : tree arg = gimple_phi_arg_def (stmt, i);
9488 :
9489 : /* If this PHI has itself as an argument, ignore it.
9490 : If all the other args produce the same result,
9491 : we're still OK. */
9492 1003 : if (arg == gimple_phi_result (stmt))
9493 0 : continue;
9494 1003 : else if (TREE_CODE (arg) == INTEGER_CST)
9495 : {
9496 773 : if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
9497 : {
9498 355 : if (!result)
9499 215 : result = boolean_true_node;
9500 140 : else if (!integer_onep (result))
9501 : return NULL_TREE;
9502 : }
9503 418 : else if (!result)
9504 196 : result = fold_build2 (code2, boolean_type_node,
9505 : op2a, op2b);
9506 222 : else if (!same_bool_comparison_p (result,
9507 : code2, op2a, op2b))
9508 : return NULL_TREE;
9509 : }
9510 230 : else if (TREE_CODE (arg) == SSA_NAME
9511 230 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
9512 : {
9513 230 : tree temp;
9514 230 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
9515 : /* In simple cases we can look through PHI nodes,
9516 : but we have to be careful with loops.
9517 : See PR49073. */
9518 230 : if (! dom_info_available_p (CDI_DOMINATORS)
9519 230 : || gimple_bb (def_stmt) == gimple_bb (stmt)
9520 460 : || dominated_by_p (CDI_DOMINATORS,
9521 230 : gimple_bb (def_stmt),
9522 230 : gimple_bb (stmt)))
9523 23 : return NULL_TREE;
9524 207 : temp = or_var_with_comparison (type, arg, invert, code2,
9525 : op2a, op2b, outer_cond_bb);
9526 207 : if (!temp)
9527 : return NULL_TREE;
9528 0 : else if (!result)
9529 : result = temp;
9530 0 : else if (!same_bool_result_p (result, temp))
9531 : return NULL_TREE;
9532 : }
9533 : else
9534 : return NULL_TREE;
9535 : }
9536 : return result;
9537 : }
9538 :
9539 : default:
9540 : break;
9541 : }
9542 : }
9543 : return NULL_TREE;
9544 : }
9545 :
9546 : /* Try to simplify the OR of two comparisons, specified by
9547 : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9548 : If this can be simplified to a single expression (without requiring
9549 : introducing more SSA variables to hold intermediate values),
9550 : return the resulting tree. Otherwise return NULL_TREE.
9551 : If the result expression is non-null, it has boolean type. */
9552 :
9553 : tree
9554 582179 : maybe_fold_or_comparisons (tree type,
9555 : enum tree_code code1, tree op1a, tree op1b,
9556 : enum tree_code code2, tree op2a, tree op2b,
9557 : basic_block outer_cond_bb)
9558 : {
9559 582179 : if (tree t = or_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9560 : outer_cond_bb))
9561 : return t;
9562 :
9563 569057 : if (tree t = or_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9564 : outer_cond_bb))
9565 : return t;
9566 :
9567 569052 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
9568 : op1a, op1b, code2, op2a,
9569 : op2b, outer_cond_bb))
9570 : return t;
9571 :
9572 : return NULL_TREE;
9573 : }
9574 :
9575 : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9576 :
9577 : Either NULL_TREE, a simplified but non-constant or a constant
9578 : is returned.
9579 :
9580 : ??? This should go into a gimple-fold-inline.h file to be eventually
9581 : privatized with the single valueize function used in the various TUs
9582 : to avoid the indirect function call overhead. */
9583 :
9584 : tree
9585 424626634 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
9586 : tree (*gvalueize) (tree))
9587 : {
9588 424626634 : gimple_match_op res_op;
9589 : /* ??? The SSA propagators do not correctly deal with following SSA use-def
9590 : edges if there are intermediate VARYING defs. For this reason
9591 : do not follow SSA edges here even though SCCVN can technically
9592 : just deal fine with that. */
9593 424626634 : if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
9594 : {
9595 56619075 : tree res = NULL_TREE;
9596 56619075 : if (gimple_simplified_result_is_gimple_val (&res_op))
9597 34632010 : res = res_op.ops[0];
9598 21987065 : else if (mprts_hook)
9599 7691313 : res = mprts_hook (&res_op);
9600 42323323 : if (res)
9601 : {
9602 36530783 : if (dump_file && dump_flags & TDF_DETAILS)
9603 : {
9604 9454 : fprintf (dump_file, "Match-and-simplified ");
9605 9454 : print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
9606 9454 : fprintf (dump_file, " to ");
9607 9454 : print_generic_expr (dump_file, res);
9608 9454 : fprintf (dump_file, "\n");
9609 : }
9610 36530783 : return res;
9611 : }
9612 : }
9613 :
9614 388095851 : location_t loc = gimple_location (stmt);
9615 388095851 : switch (gimple_code (stmt))
9616 : {
9617 334885602 : case GIMPLE_ASSIGN:
9618 334885602 : {
9619 334885602 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
9620 :
9621 334885602 : switch (get_gimple_rhs_class (subcode))
9622 : {
9623 115128854 : case GIMPLE_SINGLE_RHS:
9624 115128854 : {
9625 115128854 : tree rhs = gimple_assign_rhs1 (stmt);
9626 115128854 : enum tree_code_class kind = TREE_CODE_CLASS (subcode);
9627 :
9628 115128854 : if (TREE_CODE (rhs) == SSA_NAME)
9629 : {
9630 : /* If the RHS is an SSA_NAME, return its known constant value,
9631 : if any. */
9632 9851830 : return (*valueize) (rhs);
9633 : }
9634 : /* Handle propagating invariant addresses into address
9635 : operations. */
9636 105277024 : else if (TREE_CODE (rhs) == ADDR_EXPR
9637 105277024 : && !is_gimple_min_invariant (rhs))
9638 : {
9639 6340452 : poly_int64 offset = 0;
9640 6340452 : tree base;
9641 6340452 : base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
9642 : &offset,
9643 : valueize);
9644 6340452 : if (base
9645 6340452 : && (CONSTANT_CLASS_P (base)
9646 5679255 : || decl_address_invariant_p (base)))
9647 199093 : return build_invariant_address (TREE_TYPE (rhs),
9648 199093 : base, offset);
9649 : }
9650 98936572 : else if (TREE_CODE (rhs) == CONSTRUCTOR
9651 1103563 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
9652 100491791 : && known_eq (CONSTRUCTOR_NELTS (rhs),
9653 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
9654 : {
9655 428743 : unsigned i, nelts;
9656 428743 : tree val;
9657 :
9658 428743 : nelts = CONSTRUCTOR_NELTS (rhs);
9659 428743 : tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
9660 954809 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
9661 : {
9662 513960 : val = (*valueize) (val);
9663 513960 : if (TREE_CODE (val) == INTEGER_CST
9664 437418 : || TREE_CODE (val) == REAL_CST
9665 416637 : || TREE_CODE (val) == FIXED_CST)
9666 97323 : vec.quick_push (val);
9667 : else
9668 : return NULL_TREE;
9669 : }
9670 :
9671 12106 : return vec.build ();
9672 428743 : }
9673 104649188 : if (subcode == OBJ_TYPE_REF)
9674 : {
9675 362278 : tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
9676 : /* If callee is constant, we can fold away the wrapper. */
9677 362278 : if (is_gimple_min_invariant (val))
9678 : return val;
9679 : }
9680 :
9681 104648938 : if (kind == tcc_reference)
9682 : {
9683 69547433 : if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
9684 67268531 : || TREE_CODE (rhs) == REALPART_EXPR
9685 66257516 : || TREE_CODE (rhs) == IMAGPART_EXPR)
9686 71727136 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9687 : {
9688 3523690 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9689 3523690 : return fold_unary_loc (EXPR_LOCATION (rhs),
9690 3523690 : TREE_CODE (rhs),
9691 7047380 : TREE_TYPE (rhs), val);
9692 : }
9693 66023743 : else if (TREE_CODE (rhs) == BIT_FIELD_REF
9694 66023743 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9695 : {
9696 704009 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9697 704009 : return fold_ternary_loc (EXPR_LOCATION (rhs),
9698 704009 : TREE_CODE (rhs),
9699 704009 : TREE_TYPE (rhs), val,
9700 704009 : TREE_OPERAND (rhs, 1),
9701 1408018 : TREE_OPERAND (rhs, 2));
9702 : }
9703 65319734 : else if (TREE_CODE (rhs) == MEM_REF
9704 65319734 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9705 : {
9706 13533349 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9707 13533349 : if (TREE_CODE (val) == ADDR_EXPR
9708 13533349 : && is_gimple_min_invariant (val))
9709 : {
9710 971480 : tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
9711 : unshare_expr (val),
9712 : TREE_OPERAND (rhs, 1));
9713 971480 : if (tem)
9714 65319734 : rhs = tem;
9715 : }
9716 : }
9717 65319734 : return fold_const_aggregate_ref_1 (rhs, valueize);
9718 : }
9719 35101505 : else if (kind == tcc_declaration)
9720 7820547 : return get_symbol_constant_value (rhs);
9721 : return rhs;
9722 : }
9723 :
9724 : case GIMPLE_UNARY_RHS:
9725 : return NULL_TREE;
9726 :
9727 164441133 : case GIMPLE_BINARY_RHS:
9728 : /* Translate &x + CST into an invariant form suitable for
9729 : further propagation. */
9730 164441133 : if (subcode == POINTER_PLUS_EXPR)
9731 : {
9732 21581440 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9733 21581440 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9734 21581440 : if (TREE_CODE (op0) == ADDR_EXPR
9735 5631125 : && TREE_CODE (op1) == INTEGER_CST)
9736 : {
9737 564522 : tree off = fold_convert (ptr_type_node, op1);
9738 564522 : return build1_loc
9739 1129044 : (loc, ADDR_EXPR, TREE_TYPE (op0),
9740 564522 : fold_build2 (MEM_REF,
9741 : TREE_TYPE (TREE_TYPE (op0)),
9742 564522 : unshare_expr (op0), off));
9743 : }
9744 : }
9745 : /* Canonicalize bool != 0 and bool == 0 appearing after
9746 : valueization. While gimple_simplify handles this
9747 : it can get confused by the ~X == 1 -> X == 0 transform
9748 : which we cant reduce to a SSA name or a constant
9749 : (and we have no way to tell gimple_simplify to not
9750 : consider those transforms in the first place). */
9751 142859693 : else if (subcode == EQ_EXPR
9752 142859693 : || subcode == NE_EXPR)
9753 : {
9754 3449283 : tree lhs = gimple_assign_lhs (stmt);
9755 3449283 : tree op0 = gimple_assign_rhs1 (stmt);
9756 3449283 : if (useless_type_conversion_p (TREE_TYPE (lhs),
9757 3449283 : TREE_TYPE (op0)))
9758 : {
9759 26551 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9760 26551 : op0 = (*valueize) (op0);
9761 26551 : if (TREE_CODE (op0) == INTEGER_CST)
9762 707 : std::swap (op0, op1);
9763 26551 : if (TREE_CODE (op1) == INTEGER_CST
9764 26551 : && ((subcode == NE_EXPR && integer_zerop (op1))
9765 2342 : || (subcode == EQ_EXPR && integer_onep (op1))))
9766 323 : return op0;
9767 : }
9768 : }
9769 : return NULL_TREE;
9770 :
9771 758628 : case GIMPLE_TERNARY_RHS:
9772 758628 : {
9773 : /* Handle ternary operators that can appear in GIMPLE form. */
9774 758628 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9775 758628 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9776 758628 : tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
9777 758628 : return fold_ternary_loc (loc, subcode,
9778 758628 : TREE_TYPE (gimple_assign_lhs (stmt)),
9779 758628 : op0, op1, op2);
9780 : }
9781 :
9782 0 : default:
9783 0 : gcc_unreachable ();
9784 : }
9785 : }
9786 :
9787 14670816 : case GIMPLE_CALL:
9788 14670816 : {
9789 14670816 : tree fn;
9790 14670816 : gcall *call_stmt = as_a <gcall *> (stmt);
9791 :
9792 14670816 : if (gimple_call_internal_p (stmt))
9793 : {
9794 1338902 : enum tree_code subcode = ERROR_MARK;
9795 1338902 : switch (gimple_call_internal_fn (stmt))
9796 : {
9797 : case IFN_UBSAN_CHECK_ADD:
9798 : subcode = PLUS_EXPR;
9799 : break;
9800 7986 : case IFN_UBSAN_CHECK_SUB:
9801 7986 : subcode = MINUS_EXPR;
9802 7986 : break;
9803 6809 : case IFN_UBSAN_CHECK_MUL:
9804 6809 : subcode = MULT_EXPR;
9805 6809 : break;
9806 143079 : case IFN_BUILTIN_EXPECT:
9807 143079 : {
9808 143079 : tree arg0 = gimple_call_arg (stmt, 0);
9809 143079 : tree op0 = (*valueize) (arg0);
9810 143079 : if (TREE_CODE (op0) == INTEGER_CST)
9811 : return op0;
9812 : return NULL_TREE;
9813 : }
9814 : default:
9815 : return NULL_TREE;
9816 : }
9817 22916 : tree arg0 = gimple_call_arg (stmt, 0);
9818 22916 : tree arg1 = gimple_call_arg (stmt, 1);
9819 22916 : tree op0 = (*valueize) (arg0);
9820 22916 : tree op1 = (*valueize) (arg1);
9821 :
9822 22916 : if (TREE_CODE (op0) != INTEGER_CST
9823 2494 : || TREE_CODE (op1) != INTEGER_CST)
9824 : {
9825 22394 : switch (subcode)
9826 : {
9827 6709 : case MULT_EXPR:
9828 : /* x * 0 = 0 * x = 0 without overflow. */
9829 6709 : if (integer_zerop (op0) || integer_zerop (op1))
9830 20 : return build_zero_cst (TREE_TYPE (arg0));
9831 : break;
9832 7644 : case MINUS_EXPR:
9833 : /* y - y = 0 without overflow. */
9834 7644 : if (operand_equal_p (op0, op1, 0))
9835 0 : return build_zero_cst (TREE_TYPE (arg0));
9836 : break;
9837 : default:
9838 : break;
9839 : }
9840 : }
9841 22896 : tree res
9842 22896 : = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
9843 22896 : if (res
9844 2870 : && TREE_CODE (res) == INTEGER_CST
9845 23418 : && !TREE_OVERFLOW (res))
9846 : return res;
9847 : return NULL_TREE;
9848 : }
9849 :
9850 13331914 : fn = (*valueize) (gimple_call_fn (stmt));
9851 13331914 : if (TREE_CODE (fn) == ADDR_EXPR
9852 12685935 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
9853 12685871 : && fndecl_built_in_p (TREE_OPERAND (fn, 0))
9854 19519084 : && gimple_builtin_call_types_compatible_p (stmt,
9855 6187170 : TREE_OPERAND (fn, 0)))
9856 : {
9857 6087247 : tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
9858 6087247 : tree retval;
9859 6087247 : unsigned i;
9860 18877772 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
9861 12790525 : args[i] = (*valueize) (gimple_call_arg (stmt, i));
9862 6087247 : retval = fold_builtin_call_array (loc,
9863 : gimple_call_return_type (call_stmt),
9864 : fn, gimple_call_num_args (stmt), args);
9865 6087247 : if (retval)
9866 : {
9867 : /* fold_call_expr wraps the result inside a NOP_EXPR. */
9868 61332 : STRIP_NOPS (retval);
9869 61332 : retval = fold_convert (gimple_call_return_type (call_stmt),
9870 : retval);
9871 : }
9872 6087247 : return retval;
9873 : }
9874 : return NULL_TREE;
9875 : }
9876 :
9877 : default:
9878 : return NULL_TREE;
9879 : }
9880 : }
9881 :
9882 : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9883 : Returns NULL_TREE if folding to a constant is not possible, otherwise
9884 : returns a constant according to is_gimple_min_invariant. */
9885 :
9886 : tree
9887 4367 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
9888 : {
9889 4367 : tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
9890 4367 : if (res && is_gimple_min_invariant (res))
9891 : return res;
9892 : return NULL_TREE;
9893 : }
9894 :
9895 :
9896 : /* The following set of functions are supposed to fold references using
9897 : their constant initializers. */
9898 :
9899 : /* See if we can find constructor defining value of BASE.
9900 : When we know the constructor with constant offset (such as
9901 : base is array[40] and we do know constructor of array), then
9902 : BIT_OFFSET is adjusted accordingly.
9903 :
9904 : As a special case, return error_mark_node when constructor
9905 : is not explicitly available, but it is known to be zero
9906 : such as 'static const int a;'. */
9907 : static tree
9908 124499650 : get_base_constructor (tree base, poly_int64 *bit_offset,
9909 : tree (*valueize)(tree))
9910 : {
9911 124598335 : poly_int64 bit_offset2, size, max_size;
9912 124598335 : bool reverse;
9913 :
9914 124598335 : if (TREE_CODE (base) == MEM_REF)
9915 : {
9916 132339320 : poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
9917 66169660 : if (!boff.to_shwi (bit_offset))
9918 65808462 : return NULL_TREE;
9919 :
9920 66169313 : if (valueize
9921 66169313 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
9922 34657985 : base = valueize (TREE_OPERAND (base, 0));
9923 66169313 : if (!base || TREE_CODE (base) != ADDR_EXPR)
9924 : return NULL_TREE;
9925 361198 : base = TREE_OPERAND (base, 0);
9926 : }
9927 58428675 : else if (valueize
9928 30290879 : && TREE_CODE (base) == SSA_NAME)
9929 0 : base = valueize (base);
9930 :
9931 : /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
9932 : DECL_INITIAL. If BASE is a nested reference into another
9933 : ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
9934 : the inner reference. */
9935 58789873 : switch (TREE_CODE (base))
9936 : {
9937 51138279 : case VAR_DECL:
9938 51138279 : case CONST_DECL:
9939 51138279 : {
9940 51138279 : tree init = ctor_for_folding (base);
9941 :
9942 : /* Our semantic is exact opposite of ctor_for_folding;
9943 : NULL means unknown, while error_mark_node is 0. */
9944 51138279 : if (init == error_mark_node)
9945 : return NULL_TREE;
9946 1268758 : if (!init)
9947 1173 : return error_mark_node;
9948 : return init;
9949 : }
9950 :
9951 98685 : case VIEW_CONVERT_EXPR:
9952 98685 : return get_base_constructor (TREE_OPERAND (base, 0),
9953 98685 : bit_offset, valueize);
9954 :
9955 354070 : case ARRAY_REF:
9956 354070 : case COMPONENT_REF:
9957 354070 : base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
9958 : &reverse);
9959 354070 : if (!known_size_p (max_size) || maybe_ne (size, max_size))
9960 : return NULL_TREE;
9961 293577 : *bit_offset += bit_offset2;
9962 293577 : return get_base_constructor (base, bit_offset, valueize);
9963 :
9964 : case CONSTRUCTOR:
9965 : return base;
9966 :
9967 7198839 : default:
9968 7198839 : if (CONSTANT_CLASS_P (base))
9969 : return base;
9970 :
9971 : return NULL_TREE;
9972 : }
9973 : }
9974 :
9975 : /* CTOR is a CONSTRUCTOR of an array or vector type. Fold a reference of SIZE
9976 : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
9977 : the reference; otherwise the type of the referenced element is used instead.
9978 : When SIZE is zero, attempt to fold a reference to the entire element OFFSET
9979 : refers to. Increment *SUBOFF by the bit offset of the accessed element. */
9980 :
9981 : static tree
9982 685400 : fold_array_ctor_reference (tree type, tree ctor,
9983 : unsigned HOST_WIDE_INT offset,
9984 : unsigned HOST_WIDE_INT size,
9985 : tree from_decl,
9986 : unsigned HOST_WIDE_INT *suboff)
9987 : {
9988 685400 : offset_int low_bound;
9989 685400 : offset_int elt_size;
9990 685400 : offset_int access_index;
9991 685400 : tree domain_type = NULL_TREE;
9992 685400 : HOST_WIDE_INT inner_offset;
9993 :
9994 : /* Compute low bound and elt size. */
9995 685400 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
9996 685400 : domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
9997 685400 : if (domain_type && TYPE_MIN_VALUE (domain_type))
9998 : {
9999 : /* Static constructors for variably sized objects make no sense. */
10000 685400 : if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
10001 : return NULL_TREE;
10002 685400 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
10003 : }
10004 : else
10005 0 : low_bound = 0;
10006 : /* Static constructors for variably sized objects make no sense. */
10007 685400 : if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
10008 : return NULL_TREE;
10009 685400 : elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
10010 :
10011 : /* When TYPE is non-null, verify that it specifies a constant-sized
10012 : access of a multiple of the array element size. Avoid division
10013 : by zero below when ELT_SIZE is zero, such as with the result of
10014 : an initializer for a zero-length array or an empty struct. */
10015 685400 : if (elt_size == 0
10016 685400 : || (type
10017 685364 : && (!TYPE_SIZE_UNIT (type)
10018 685364 : || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
10019 36 : return NULL_TREE;
10020 :
10021 : /* Compute the array index we look for. */
10022 685364 : access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
10023 : elt_size);
10024 685364 : access_index += low_bound;
10025 :
10026 : /* And offset within the access. */
10027 685364 : inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
10028 :
10029 685364 : unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
10030 685364 : if (size > elt_sz * BITS_PER_UNIT)
10031 : {
10032 : /* native_encode_expr constraints. */
10033 21467 : if (size > MAX_BITSIZE_MODE_ANY_MODE
10034 17494 : || size % BITS_PER_UNIT != 0
10035 17494 : || inner_offset % BITS_PER_UNIT != 0
10036 17494 : || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
10037 : return NULL_TREE;
10038 :
10039 17494 : unsigned ctor_idx;
10040 17494 : tree val = get_array_ctor_element_at_index (ctor, access_index,
10041 : &ctor_idx);
10042 17518 : if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
10043 23 : return build_zero_cst (type);
10044 :
10045 : /* native-encode adjacent ctor elements. */
10046 17471 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10047 17471 : unsigned bufoff = 0;
10048 17471 : offset_int index = 0;
10049 17471 : offset_int max_index = access_index;
10050 17471 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
10051 17471 : if (!val)
10052 1 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
10053 17470 : else if (!CONSTANT_CLASS_P (val))
10054 : return NULL_TREE;
10055 17297 : if (!elt->index)
10056 : ;
10057 17162 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
10058 : {
10059 18 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
10060 18 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
10061 : }
10062 : else
10063 17144 : index = max_index = wi::to_offset (elt->index);
10064 17297 : index = wi::umax (index, access_index);
10065 112426 : do
10066 : {
10067 112426 : if (bufoff + elt_sz > sizeof (buf))
10068 0 : elt_sz = sizeof (buf) - bufoff;
10069 112426 : int len;
10070 112426 : if (TREE_CODE (val) == RAW_DATA_CST)
10071 : {
10072 20 : gcc_assert (inner_offset == 0);
10073 20 : if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
10074 : return NULL_TREE;
10075 40 : inner_offset = (access_index
10076 20 : - wi::to_offset (elt->index)).to_uhwi ();
10077 20 : len = MIN (sizeof (buf) - bufoff,
10078 : (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
10079 20 : memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
10080 : len);
10081 20 : access_index += len - 1;
10082 : }
10083 : else
10084 : {
10085 224812 : len = native_encode_expr (val, buf + bufoff, elt_sz,
10086 112406 : inner_offset / BITS_PER_UNIT);
10087 112406 : if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
10088 : return NULL_TREE;
10089 : }
10090 112426 : inner_offset = 0;
10091 112426 : bufoff += len;
10092 :
10093 112426 : access_index += 1;
10094 112426 : if (wi::cmpu (access_index, index) == 0)
10095 2 : val = elt->value;
10096 112424 : else if (wi::cmpu (access_index, max_index) > 0)
10097 : {
10098 112202 : ctor_idx++;
10099 112202 : if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
10100 : {
10101 15370 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
10102 15370 : ++max_index;
10103 : }
10104 : else
10105 : {
10106 96832 : elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
10107 96832 : index = 0;
10108 96832 : max_index = access_index;
10109 96832 : if (!elt->index)
10110 : ;
10111 96024 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
10112 : {
10113 0 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
10114 0 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
10115 : }
10116 : else
10117 96024 : index = max_index = wi::to_offset (elt->index);
10118 96832 : index = wi::umax (index, access_index);
10119 96832 : if (wi::cmpu (access_index, index) == 0)
10120 96827 : val = elt->value;
10121 : else
10122 5 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
10123 : }
10124 : }
10125 : }
10126 112426 : while (bufoff < size / BITS_PER_UNIT);
10127 17297 : *suboff += size;
10128 17297 : return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
10129 : }
10130 :
10131 663897 : unsigned ctor_idx;
10132 663897 : if (tree val = get_array_ctor_element_at_index (ctor, access_index,
10133 : &ctor_idx))
10134 : {
10135 662770 : if (TREE_CODE (val) == RAW_DATA_CST)
10136 : {
10137 2590 : if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
10138 : return NULL_TREE;
10139 2582 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
10140 2582 : if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
10141 : return NULL_TREE;
10142 2582 : unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
10143 2582 : val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
10144 : }
10145 662762 : if (!size && TREE_CODE (val) != CONSTRUCTOR)
10146 : {
10147 : /* For the final reference to the entire accessed element
10148 : (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
10149 : may be null) in favor of the type of the element, and set
10150 : SIZE to the size of the accessed element. */
10151 22915 : inner_offset = 0;
10152 22915 : type = TREE_TYPE (val);
10153 22915 : size = elt_sz * BITS_PER_UNIT;
10154 : }
10155 1708523 : else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
10156 466949 : && TREE_CODE (val) == CONSTRUCTOR
10157 15669 : && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
10158 : /* If this isn't the last element in the CTOR and a CTOR itself
10159 : and it does not cover the whole object we are requesting give up
10160 : since we're not set up for combining from multiple CTORs. */
10161 26 : return NULL_TREE;
10162 :
10163 662736 : *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
10164 662736 : return fold_ctor_reference (type, val, inner_offset, size, from_decl,
10165 : suboff);
10166 : }
10167 :
10168 : /* Memory not explicitly mentioned in constructor is 0 (or
10169 : the reference is out of range). */
10170 1127 : return type ? build_zero_cst (type) : NULL_TREE;
10171 : }
10172 :
10173 : /* CTOR is a CONSTRUCTOR of a record or union type. Fold a reference of SIZE
10174 : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
10175 : the reference; otherwise the type of the referenced member is used instead.
10176 : When SIZE is zero, attempt to fold a reference to the entire member OFFSET
10177 : refers to. Increment *SUBOFF by the bit offset of the accessed member. */
10178 :
10179 : static tree
10180 79649 : fold_nonarray_ctor_reference (tree type, tree ctor,
10181 : unsigned HOST_WIDE_INT offset,
10182 : unsigned HOST_WIDE_INT size,
10183 : tree from_decl,
10184 : unsigned HOST_WIDE_INT *suboff)
10185 : {
10186 79649 : unsigned HOST_WIDE_INT cnt;
10187 79649 : tree cfield, cval;
10188 :
10189 120401 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
10190 : {
10191 111442 : tree byte_offset = DECL_FIELD_OFFSET (cfield);
10192 111442 : tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
10193 111442 : tree field_size = DECL_SIZE (cfield);
10194 :
10195 111442 : if (!field_size)
10196 : {
10197 : /* Determine the size of the flexible array member from
10198 : the size of the initializer provided for it. */
10199 847 : field_size = TYPE_SIZE (TREE_TYPE (cval));
10200 : }
10201 :
10202 : /* Variable sized objects in static constructors makes no sense,
10203 : but field_size can be NULL for flexible array members. */
10204 111442 : gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
10205 : && TREE_CODE (byte_offset) == INTEGER_CST
10206 : && (field_size != NULL_TREE
10207 : ? TREE_CODE (field_size) == INTEGER_CST
10208 : : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
10209 :
10210 : /* Compute bit offset of the field. */
10211 111442 : offset_int bitoffset
10212 111442 : = (wi::to_offset (field_offset)
10213 111442 : + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
10214 : /* Compute bit offset where the field ends. */
10215 111442 : offset_int bitoffset_end;
10216 111442 : if (field_size != NULL_TREE)
10217 111442 : bitoffset_end = bitoffset + wi::to_offset (field_size);
10218 : else
10219 0 : bitoffset_end = 0;
10220 :
10221 : /* Compute the bit offset of the end of the desired access.
10222 : As a special case, if the size of the desired access is
10223 : zero, assume the access is to the entire field (and let
10224 : the caller make any necessary adjustments by storing
10225 : the actual bounds of the field in FIELDBOUNDS). */
10226 111442 : offset_int access_end = offset_int (offset);
10227 111442 : if (size)
10228 69617 : access_end += size;
10229 : else
10230 41825 : access_end = bitoffset_end;
10231 :
10232 : /* Is there any overlap between the desired access at
10233 : [OFFSET, OFFSET+SIZE) and the offset of the field within
10234 : the object at [BITOFFSET, BITOFFSET_END)? */
10235 111442 : if (wi::cmps (access_end, bitoffset) > 0
10236 111442 : && (field_size == NULL_TREE
10237 108737 : || wi::lts_p (offset, bitoffset_end)))
10238 : {
10239 70690 : *suboff += bitoffset.to_uhwi ();
10240 :
10241 70690 : if (!size && TREE_CODE (cval) != CONSTRUCTOR)
10242 : {
10243 : /* For the final reference to the entire accessed member
10244 : (SIZE is zero), reset OFFSET, disegard TYPE (which may
10245 : be null) in favor of the type of the member, and set
10246 : SIZE to the size of the accessed member. */
10247 19310 : offset = bitoffset.to_uhwi ();
10248 19310 : type = TREE_TYPE (cval);
10249 19310 : size = (bitoffset_end - bitoffset).to_uhwi ();
10250 : }
10251 :
10252 : /* We do have overlap. Now see if the field is large enough
10253 : to cover the access. Give up for accesses that extend
10254 : beyond the end of the object or that span multiple fields. */
10255 70690 : if (wi::cmps (access_end, bitoffset_end) > 0)
10256 : return NULL_TREE;
10257 70063 : if (offset < bitoffset)
10258 : return NULL_TREE;
10259 :
10260 70063 : offset_int inner_offset = offset_int (offset) - bitoffset;
10261 :
10262 : /* Integral bit-fields are left-justified on big-endian targets, so
10263 : we must arrange for native_encode_int to start at their MSB. */
10264 70063 : if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
10265 : {
10266 70063 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
10267 : return NULL_TREE;
10268 70063 : if (BYTES_BIG_ENDIAN)
10269 : {
10270 : tree ctype = TREE_TYPE (cfield);
10271 : unsigned int encoding_size;
10272 : if (TYPE_MODE (ctype) != BLKmode)
10273 : encoding_size
10274 : = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (ctype));
10275 : else
10276 : encoding_size = TREE_INT_CST_LOW (TYPE_SIZE (ctype));
10277 : inner_offset += encoding_size - wi::to_offset (field_size);
10278 : }
10279 : }
10280 :
10281 70063 : return fold_ctor_reference (type, cval,
10282 70063 : inner_offset.to_uhwi (), size,
10283 : from_decl, suboff);
10284 : }
10285 : }
10286 :
10287 8959 : if (!type)
10288 : return NULL_TREE;
10289 :
10290 8959 : return build_zero_cst (type);
10291 : }
10292 :
10293 : /* CTOR is a value initializing memory. Fold a reference of TYPE and
10294 : bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
10295 : is zero, attempt to fold a reference to the entire subobject
10296 : which OFFSET refers to. This is used when folding accesses to
10297 : string members of aggregates. When non-null, set *SUBOFF to
10298 : the bit offset of the accessed subobject. */
10299 :
10300 : tree
10301 1590717 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
10302 : const poly_uint64 &poly_size, tree from_decl,
10303 : unsigned HOST_WIDE_INT *suboff /* = NULL */)
10304 : {
10305 1590717 : tree ret;
10306 :
10307 : /* We found the field with exact match. */
10308 1590717 : if (type
10309 1590717 : && useless_type_conversion_p (type, TREE_TYPE (ctor))
10310 2276197 : && known_eq (poly_offset, 0U))
10311 684216 : return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
10312 :
10313 : /* The remaining optimizations need a constant size and offset. */
10314 906501 : unsigned HOST_WIDE_INT size, offset;
10315 906501 : if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
10316 : return NULL_TREE;
10317 :
10318 : /* We are at the end of walk, see if we can view convert the
10319 : result. */
10320 906501 : if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
10321 : /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
10322 23268 : && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
10323 23161 : && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
10324 : {
10325 14441 : ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
10326 14441 : if (ret)
10327 : {
10328 14441 : ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
10329 14441 : if (ret)
10330 14421 : STRIP_USELESS_TYPE_CONVERSION (ret);
10331 : }
10332 14441 : return ret;
10333 : }
10334 :
10335 : /* For constants and byte-aligned/sized reads, try to go through
10336 : native_encode/interpret. */
10337 892060 : if (CONSTANT_CLASS_P (ctor)
10338 : && BITS_PER_UNIT == 8
10339 118747 : && offset % BITS_PER_UNIT == 0
10340 118743 : && offset / BITS_PER_UNIT <= INT_MAX
10341 118711 : && size % BITS_PER_UNIT == 0
10342 118702 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10343 1010464 : && can_native_interpret_type_p (type))
10344 : {
10345 103354 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10346 206708 : int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
10347 103354 : offset / BITS_PER_UNIT);
10348 103354 : if (len > 0)
10349 102636 : return native_interpret_expr (type, buf, len);
10350 : }
10351 :
10352 : /* For constructors, try first a recursive local processing, but in any case
10353 : this requires the native storage order. */
10354 789424 : if (TREE_CODE (ctor) == CONSTRUCTOR
10355 789424 : && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
10356 765279 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
10357 : {
10358 765049 : unsigned HOST_WIDE_INT dummy = 0;
10359 765049 : if (!suboff)
10360 637233 : suboff = &dummy;
10361 :
10362 765049 : tree ret;
10363 765049 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
10364 765049 : || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
10365 685400 : ret = fold_array_ctor_reference (type, ctor, offset, size,
10366 : from_decl, suboff);
10367 : else
10368 79649 : ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
10369 : from_decl, suboff);
10370 :
10371 : /* Otherwise fall back to native_encode_initializer. This may be done
10372 : only from the outermost fold_ctor_reference call (because it itself
10373 : recurses into CONSTRUCTORs and doesn't update suboff). */
10374 765049 : if (ret == NULL_TREE
10375 217189 : && suboff == &dummy
10376 : && BITS_PER_UNIT == 8
10377 208834 : && offset % BITS_PER_UNIT == 0
10378 208832 : && offset / BITS_PER_UNIT <= INT_MAX
10379 208832 : && size % BITS_PER_UNIT == 0
10380 208823 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10381 969899 : && can_native_interpret_type_p (type))
10382 : {
10383 191265 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10384 382530 : int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
10385 191265 : offset / BITS_PER_UNIT);
10386 191265 : if (len > 0)
10387 1310 : return native_interpret_expr (type, buf, len);
10388 : }
10389 :
10390 763739 : return ret;
10391 : }
10392 :
10393 : return NULL_TREE;
10394 : }
10395 :
10396 : /* Return the tree representing the element referenced by T if T is an
10397 : ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
10398 : names using VALUEIZE. Return NULL_TREE otherwise. */
10399 :
10400 : tree
10401 129747793 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
10402 : {
10403 129747793 : tree ctor, idx, base;
10404 129747793 : poly_int64 offset, size, max_size;
10405 129747793 : tree tem;
10406 129747793 : bool reverse;
10407 :
10408 129747793 : if (TREE_THIS_VOLATILE (t))
10409 : return NULL_TREE;
10410 :
10411 129431467 : if (DECL_P (t))
10412 300233 : return get_symbol_constant_value (t);
10413 :
10414 129131234 : tem = fold_read_from_constant_string (t);
10415 129131234 : if (tem)
10416 : return tem;
10417 :
10418 129129379 : switch (TREE_CODE (t))
10419 : {
10420 10653902 : case ARRAY_REF:
10421 10653902 : case ARRAY_RANGE_REF:
10422 : /* Constant indexes are handled well by get_base_constructor.
10423 : Only special case variable offsets.
10424 : FIXME: This code can't handle nested references with variable indexes
10425 : (they will be handled only by iteration of ccp). Perhaps we can bring
10426 : get_ref_base_and_extent here and make it use a valueize callback. */
10427 10653902 : if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
10428 6207842 : && valueize
10429 4164408 : && (idx = (*valueize) (TREE_OPERAND (t, 1)))
10430 14818310 : && poly_int_tree_p (idx))
10431 : {
10432 1682673 : tree low_bound, unit_size;
10433 :
10434 : /* If the resulting bit-offset is constant, track it. */
10435 1682673 : if ((low_bound = array_ref_low_bound (t),
10436 1682673 : poly_int_tree_p (low_bound))
10437 1682673 : && (unit_size = array_ref_element_size (t),
10438 1682673 : tree_fits_uhwi_p (unit_size)))
10439 : {
10440 1682673 : poly_offset_int woffset
10441 1682673 : = wi::sext (wi::to_poly_offset (idx)
10442 3365346 : - wi::to_poly_offset (low_bound),
10443 1682673 : TYPE_PRECISION (sizetype));
10444 1682673 : woffset *= tree_to_uhwi (unit_size);
10445 1682673 : woffset *= BITS_PER_UNIT;
10446 1682673 : if (woffset.to_shwi (&offset))
10447 : {
10448 1682542 : base = TREE_OPERAND (t, 0);
10449 1682542 : ctor = get_base_constructor (base, &offset, valueize);
10450 : /* Empty constructor. Always fold to 0. */
10451 1682542 : if (ctor == error_mark_node)
10452 1682542 : return build_zero_cst (TREE_TYPE (t));
10453 : /* Out of bound array access. Value is undefined,
10454 : but don't fold. */
10455 1682464 : if (maybe_lt (offset, 0))
10456 : return NULL_TREE;
10457 : /* We cannot determine ctor. */
10458 1681990 : if (!ctor)
10459 : return NULL_TREE;
10460 173304 : return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
10461 173304 : tree_to_uhwi (unit_size)
10462 346608 : * BITS_PER_UNIT,
10463 : base);
10464 : }
10465 : }
10466 : }
10467 : /* Fallthru. */
10468 :
10469 122523531 : case COMPONENT_REF:
10470 122523531 : case BIT_FIELD_REF:
10471 122523531 : case TARGET_MEM_REF:
10472 122523531 : case MEM_REF:
10473 122523531 : base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
10474 122523531 : ctor = get_base_constructor (base, &offset, valueize);
10475 :
10476 : /* We cannot determine ctor. */
10477 122523531 : if (!ctor)
10478 : return NULL_TREE;
10479 : /* Empty constructor. Always fold to 0. */
10480 1190685 : if (ctor == error_mark_node)
10481 1095 : return build_zero_cst (TREE_TYPE (t));
10482 : /* We do not know precise access. */
10483 1189590 : if (!known_size_p (max_size) || maybe_ne (max_size, size))
10484 : return NULL_TREE;
10485 : /* Out of bound array access. Value is undefined, but don't fold. */
10486 500465 : if (maybe_lt (offset, 0))
10487 : return NULL_TREE;
10488 : /* Access with reverse storage order. */
10489 500084 : if (reverse)
10490 : return NULL_TREE;
10491 :
10492 500084 : tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
10493 500084 : if (tem)
10494 : return tem;
10495 :
10496 : /* For bit field reads try to read the representative and
10497 : adjust. */
10498 179200 : if (TREE_CODE (t) == COMPONENT_REF
10499 5149 : && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
10500 179284 : && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
10501 : {
10502 84 : HOST_WIDE_INT csize, coffset;
10503 84 : tree field = TREE_OPERAND (t, 1);
10504 84 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
10505 168 : if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
10506 83 : && size.is_constant (&csize)
10507 83 : && offset.is_constant (&coffset)
10508 83 : && (coffset % BITS_PER_UNIT != 0
10509 81 : || csize % BITS_PER_UNIT != 0)
10510 84 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
10511 : {
10512 10 : poly_int64 bitoffset;
10513 10 : poly_uint64 field_offset, repr_offset;
10514 10 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
10515 20 : && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
10516 10 : bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
10517 : else
10518 : bitoffset = 0;
10519 10 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
10520 10 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
10521 10 : HOST_WIDE_INT bitoff;
10522 10 : int diff = (TYPE_PRECISION (TREE_TYPE (repr))
10523 10 : - TYPE_PRECISION (TREE_TYPE (field)));
10524 10 : if (bitoffset.is_constant (&bitoff)
10525 10 : && bitoff >= 0
10526 10 : && bitoff <= diff)
10527 : {
10528 10 : offset -= bitoff;
10529 10 : size = tree_to_uhwi (DECL_SIZE (repr));
10530 :
10531 10 : tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
10532 10 : size, base);
10533 10 : if (tem && TREE_CODE (tem) == INTEGER_CST)
10534 : {
10535 10 : if (!BYTES_BIG_ENDIAN)
10536 10 : tem = wide_int_to_tree (TREE_TYPE (field),
10537 10 : wi::lrshift (wi::to_wide (tem),
10538 : bitoff));
10539 : else
10540 : tem = wide_int_to_tree (TREE_TYPE (field),
10541 : wi::lrshift (wi::to_wide (tem),
10542 : diff - bitoff));
10543 10 : return tem;
10544 : }
10545 : }
10546 : }
10547 : }
10548 : break;
10549 :
10550 1808160 : case REALPART_EXPR:
10551 1808160 : case IMAGPART_EXPR:
10552 1808160 : {
10553 1808160 : tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
10554 1808160 : if (c && TREE_CODE (c) == COMPLEX_CST)
10555 2774 : return fold_build1_loc (EXPR_LOCATION (t),
10556 5548 : TREE_CODE (t), TREE_TYPE (t), c);
10557 : break;
10558 : }
10559 :
10560 : default:
10561 : break;
10562 : }
10563 :
10564 : return NULL_TREE;
10565 : }
10566 :
10567 : tree
10568 62619899 : fold_const_aggregate_ref (tree t)
10569 : {
10570 62619899 : return fold_const_aggregate_ref_1 (t, NULL);
10571 : }
10572 :
10573 : /* Lookup virtual method with index TOKEN in a virtual table V
10574 : at OFFSET.
10575 : Set CAN_REFER if non-NULL to false if method
10576 : is not referable or if the virtual table is ill-formed (such as rewritten
10577 : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10578 :
10579 : tree
10580 275611 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
10581 : tree v,
10582 : unsigned HOST_WIDE_INT offset,
10583 : bool *can_refer)
10584 : {
10585 275611 : tree vtable = v, init, fn;
10586 275611 : unsigned HOST_WIDE_INT size;
10587 275611 : unsigned HOST_WIDE_INT elt_size, access_index;
10588 275611 : tree domain_type;
10589 :
10590 275611 : if (can_refer)
10591 275611 : *can_refer = true;
10592 :
10593 : /* First of all double check we have virtual table. */
10594 275611 : if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
10595 : {
10596 : /* Pass down that we lost track of the target. */
10597 0 : if (can_refer)
10598 0 : *can_refer = false;
10599 0 : return NULL_TREE;
10600 : }
10601 :
10602 275611 : init = ctor_for_folding (v);
10603 :
10604 : /* The virtual tables should always be born with constructors
10605 : and we always should assume that they are available for
10606 : folding. At the moment we do not stream them in all cases,
10607 : but it should never happen that ctor seem unreachable. */
10608 275611 : gcc_assert (init);
10609 275611 : if (init == error_mark_node)
10610 : {
10611 : /* Pass down that we lost track of the target. */
10612 205 : if (can_refer)
10613 205 : *can_refer = false;
10614 205 : return NULL_TREE;
10615 : }
10616 275406 : gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
10617 275406 : size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
10618 275406 : offset *= BITS_PER_UNIT;
10619 275406 : offset += token * size;
10620 :
10621 : /* Lookup the value in the constructor that is assumed to be array.
10622 : This is equivalent to
10623 : fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
10624 : offset, size, NULL);
10625 : but in a constant time. We expect that frontend produced a simple
10626 : array without indexed initializers. */
10627 :
10628 275406 : gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
10629 275406 : domain_type = TYPE_DOMAIN (TREE_TYPE (init));
10630 275406 : gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
10631 275406 : elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
10632 :
10633 275406 : access_index = offset / BITS_PER_UNIT / elt_size;
10634 275406 : gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
10635 :
10636 : /* This code makes an assumption that there are no
10637 : indexed fields produced by C++ FE, so we can directly index the array. */
10638 275406 : if (access_index < CONSTRUCTOR_NELTS (init))
10639 : {
10640 275405 : fn = CONSTRUCTOR_ELT (init, access_index)->value;
10641 275405 : gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
10642 275405 : STRIP_NOPS (fn);
10643 : }
10644 : else
10645 : fn = NULL;
10646 :
10647 : /* For type inconsistent program we may end up looking up virtual method
10648 : in virtual table that does not contain TOKEN entries. We may overrun
10649 : the virtual table and pick up a constant or RTTI info pointer.
10650 : In any case the call is undefined. */
10651 275405 : if (!fn
10652 275405 : || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
10653 545044 : || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
10654 5767 : fn = builtin_decl_unreachable ();
10655 : else
10656 : {
10657 269639 : fn = TREE_OPERAND (fn, 0);
10658 :
10659 : /* When cgraph node is missing and function is not public, we cannot
10660 : devirtualize. This can happen in WHOPR when the actual method
10661 : ends up in other partition, because we found devirtualization
10662 : possibility too late. */
10663 269639 : if (!can_refer_decl_in_current_unit_p (fn, vtable))
10664 : {
10665 36321 : if (can_refer)
10666 : {
10667 36321 : *can_refer = false;
10668 36321 : return fn;
10669 : }
10670 : return NULL_TREE;
10671 : }
10672 : }
10673 :
10674 : /* Make sure we create a cgraph node for functions we'll reference.
10675 : They can be non-existent if the reference comes from an entry
10676 : of an external vtable for example. */
10677 239085 : cgraph_node::get_create (fn);
10678 :
10679 239085 : return fn;
10680 : }
10681 :
10682 : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
10683 : is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
10684 : KNOWN_BINFO carries the binfo describing the true type of
10685 : OBJ_TYPE_REF_OBJECT(REF).
10686 : Set CAN_REFER if non-NULL to false if method
10687 : is not referable or if the virtual table is ill-formed (such as rewritten
10688 : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10689 :
10690 : tree
10691 267062 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
10692 : bool *can_refer)
10693 : {
10694 267062 : unsigned HOST_WIDE_INT offset;
10695 267062 : tree v;
10696 :
10697 267062 : v = BINFO_VTABLE (known_binfo);
10698 : /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
10699 267062 : if (!v)
10700 : return NULL_TREE;
10701 :
10702 267062 : if (!vtable_pointer_value_to_vtable (v, &v, &offset))
10703 : {
10704 0 : if (can_refer)
10705 0 : *can_refer = false;
10706 0 : return NULL_TREE;
10707 : }
10708 267062 : return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
10709 : }
10710 :
10711 : /* Given a pointer value T, return a simplified version of an
10712 : indirection through T, or NULL_TREE if no simplification is
10713 : possible. Note that the resulting type may be different from
10714 : the type pointed to in the sense that it is still compatible
10715 : from the langhooks point of view. */
10716 :
10717 : tree
10718 2526362 : gimple_fold_indirect_ref (tree t)
10719 : {
10720 2526362 : tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
10721 2526362 : tree sub = t;
10722 2526362 : tree subtype;
10723 :
10724 2526362 : STRIP_NOPS (sub);
10725 2526362 : subtype = TREE_TYPE (sub);
10726 2526362 : if (!POINTER_TYPE_P (subtype)
10727 2526362 : || TYPE_REF_CAN_ALIAS_ALL (ptype))
10728 : return NULL_TREE;
10729 :
10730 2524653 : if (TREE_CODE (sub) == ADDR_EXPR)
10731 : {
10732 94766 : tree op = TREE_OPERAND (sub, 0);
10733 94766 : tree optype = TREE_TYPE (op);
10734 : /* *&p => p */
10735 94766 : if (useless_type_conversion_p (type, optype))
10736 : return op;
10737 :
10738 : /* *(foo *)&fooarray => fooarray[0] */
10739 993 : if (TREE_CODE (optype) == ARRAY_TYPE
10740 307 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
10741 1300 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10742 : {
10743 54 : tree type_domain = TYPE_DOMAIN (optype);
10744 54 : tree min_val = size_zero_node;
10745 54 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10746 54 : min_val = TYPE_MIN_VALUE (type_domain);
10747 54 : if (TREE_CODE (min_val) == INTEGER_CST)
10748 54 : return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
10749 : }
10750 : /* *(foo *)&complexfoo => __real__ complexfoo */
10751 939 : else if (TREE_CODE (optype) == COMPLEX_TYPE
10752 939 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10753 4 : return fold_build1 (REALPART_EXPR, type, op);
10754 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
10755 935 : else if (TREE_CODE (optype) == VECTOR_TYPE
10756 935 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10757 : {
10758 26 : tree part_width = TYPE_SIZE (type);
10759 26 : tree index = bitsize_int (0);
10760 26 : return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
10761 : }
10762 : }
10763 :
10764 : /* *(p + CST) -> ... */
10765 2430796 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
10766 2430796 : && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
10767 : {
10768 34111 : tree addr = TREE_OPERAND (sub, 0);
10769 34111 : tree off = TREE_OPERAND (sub, 1);
10770 34111 : tree addrtype;
10771 :
10772 34111 : STRIP_NOPS (addr);
10773 34111 : addrtype = TREE_TYPE (addr);
10774 :
10775 : /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
10776 34111 : if (TREE_CODE (addr) == ADDR_EXPR
10777 92 : && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
10778 39 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
10779 34139 : && tree_fits_uhwi_p (off))
10780 : {
10781 28 : unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
10782 28 : tree part_width = TYPE_SIZE (type);
10783 28 : unsigned HOST_WIDE_INT part_widthi
10784 28 : = tree_to_shwi (part_width) / BITS_PER_UNIT;
10785 28 : unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
10786 28 : tree index = bitsize_int (indexi);
10787 28 : if (known_lt (offset / part_widthi,
10788 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
10789 28 : return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
10790 : part_width, index);
10791 : }
10792 :
10793 : /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
10794 34083 : if (TREE_CODE (addr) == ADDR_EXPR
10795 64 : && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
10796 34084 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
10797 : {
10798 1 : tree size = TYPE_SIZE_UNIT (type);
10799 1 : if (tree_int_cst_equal (size, off))
10800 1 : return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
10801 : }
10802 :
10803 : /* *(p + CST) -> MEM_REF <p, CST>. */
10804 34082 : if (TREE_CODE (addr) != ADDR_EXPR
10805 34082 : || DECL_P (TREE_OPERAND (addr, 0)))
10806 34064 : return fold_build2 (MEM_REF, type,
10807 : addr,
10808 : wide_int_to_tree (ptype, wi::to_wide (off)));
10809 : }
10810 :
10811 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10812 2396703 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10813 2507 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
10814 2399180 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10815 : {
10816 1 : tree type_domain;
10817 1 : tree min_val = size_zero_node;
10818 1 : tree osub = sub;
10819 1 : sub = gimple_fold_indirect_ref (sub);
10820 1 : if (! sub)
10821 1 : sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
10822 1 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
10823 1 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10824 1 : min_val = TYPE_MIN_VALUE (type_domain);
10825 1 : if (TREE_CODE (min_val) == INTEGER_CST)
10826 1 : return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
10827 : }
10828 :
10829 : return NULL_TREE;
10830 : }
10831 :
10832 : /* Return true if CODE is an operation that when operating on signed
10833 : integer types involves undefined behavior on overflow and the
10834 : operation can be expressed with unsigned arithmetic. */
10835 :
10836 : bool
10837 510385 : arith_code_with_undefined_signed_overflow (tree_code code)
10838 : {
10839 510385 : switch (code)
10840 : {
10841 : case ABS_EXPR:
10842 : case PLUS_EXPR:
10843 : case MINUS_EXPR:
10844 : case MULT_EXPR:
10845 : case NEGATE_EXPR:
10846 : case POINTER_PLUS_EXPR:
10847 : return true;
10848 202564 : default:
10849 202564 : return false;
10850 : }
10851 : }
10852 :
10853 : /* Return true if STMT has an operation that operates on a signed
10854 : integer types involves undefined behavior on overflow and the
10855 : operation can be expressed with unsigned arithmetic.
10856 : Also returns true if STMT is a VCE that needs to be rewritten
10857 : if moved to be executed unconditionally. */
10858 :
10859 : bool
10860 1321413 : gimple_needing_rewrite_undefined (gimple *stmt)
10861 : {
10862 1321413 : if (!is_gimple_assign (stmt))
10863 : return false;
10864 1123095 : tree lhs = gimple_assign_lhs (stmt);
10865 1123095 : if (!lhs)
10866 : return false;
10867 1123095 : tree lhs_type = TREE_TYPE (lhs);
10868 1123095 : if (!INTEGRAL_TYPE_P (lhs_type)
10869 121553 : && !POINTER_TYPE_P (lhs_type))
10870 : return false;
10871 1085926 : tree rhs = gimple_assign_rhs1 (stmt);
10872 : /* Boolean loads need special handling as they are treated as a full MODE load
10873 : and don't mask off the bits for the precision. */
10874 1085926 : if (gimple_assign_load_p (stmt)
10875 : /* Booleans are the integral type which has this non-masking issue. */
10876 95161 : && TREE_CODE (lhs_type) == BOOLEAN_TYPE
10877 : /* Only non mode precision booleans are need the masking. */
10878 425 : && !type_has_mode_precision_p (lhs_type)
10879 : /* BFR should be the correct thing and just grab the precision. */
10880 425 : && TREE_CODE (rhs) != BIT_FIELD_REF
10881 : /* Bit-fields loads don't need a rewrite as the masking
10882 : happens for them. */
10883 1086351 : && (TREE_CODE (rhs) != COMPONENT_REF
10884 139 : || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1))))
10885 : return true;
10886 : /* VCE from integral types to a integral types but with
10887 : a smaller precision need to be changed into casts
10888 : to be well defined. */
10889 1085501 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
10890 199 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
10891 172 : && is_gimple_val (TREE_OPERAND (rhs, 0))
10892 1085673 : && TYPE_PRECISION (lhs_type)
10893 172 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
10894 : return true;
10895 1085333 : if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
10896 : return false;
10897 389972 : if (!arith_code_with_undefined_signed_overflow
10898 389972 : (gimple_assign_rhs_code (stmt)))
10899 : return false;
10900 : return true;
10901 : }
10902 :
10903 : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
10904 : operation that can be transformed to unsigned arithmetic by converting
10905 : its operand, carrying out the operation in the corresponding unsigned
10906 : type and converting the result back to the original type.
10907 :
10908 : If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
10909 : return NULL.
10910 : Otherwise returns a sequence of statements that replace STMT and also
10911 : contain a modified form of STMT itself. */
10912 :
10913 : static gimple_seq
10914 65409 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
10915 : bool in_place)
10916 : {
10917 65409 : gcc_assert (gimple_needing_rewrite_undefined (stmt));
10918 65409 : if (dump_file && (dump_flags & TDF_DETAILS))
10919 : {
10920 21 : fprintf (dump_file, "rewriting stmt for being unconditional defined");
10921 21 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
10922 : }
10923 65409 : gimple_seq stmts = NULL;
10924 65409 : tree lhs = gimple_assign_lhs (stmt);
10925 :
10926 : /* Boolean loads need to be rewritten to be a load from the same mode
10927 : and then a cast to the other type so the other bits are masked off
10928 : correctly since the load was done conditionally. It is similar to the VCE
10929 : case below. */
10930 65409 : if (gimple_assign_load_p (stmt)
10931 65409 : && TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE)
10932 : {
10933 123 : tree rhs = gimple_assign_rhs1 (stmt);
10934 :
10935 : /* Double check that gimple_needing_rewrite_undefined was called. */
10936 : /* Bit-fields loads will do the masking so don't need the rewriting. */
10937 123 : gcc_assert (TREE_CODE (rhs) != COMPONENT_REF
10938 : || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1)));
10939 : /* BFR is like a bit field load and will do the correct thing. */
10940 123 : gcc_assert (TREE_CODE (lhs) != BIT_FIELD_REF);
10941 : /* Complex boolean types are not valid so REAL/IMAG part will
10942 : never show up. */
10943 123 : gcc_assert (TREE_CODE (rhs) != REALPART_EXPR
10944 : && TREE_CODE (lhs) != IMAGPART_EXPR);
10945 :
10946 123 : auto bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (rhs)));
10947 123 : tree new_type = build_nonstandard_integer_type (bits, true);
10948 123 : location_t loc = gimple_location (stmt);
10949 123 : tree mem_ref = fold_build1_loc (loc, VIEW_CONVERT_EXPR, new_type, rhs);
10950 : /* Replace the original load with a new load and a new lhs. */
10951 123 : tree new_lhs = make_ssa_name (new_type);
10952 123 : gimple_assign_set_rhs1 (stmt, mem_ref);
10953 123 : gimple_assign_set_lhs (stmt, new_lhs);
10954 :
10955 123 : if (in_place)
10956 49 : update_stmt (stmt);
10957 : else
10958 : {
10959 74 : gimple_set_modified (stmt, true);
10960 74 : gimple_seq_add_stmt (&stmts, stmt);
10961 : }
10962 :
10963 : /* Build the conversion statement. */
10964 123 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
10965 123 : if (in_place)
10966 : {
10967 49 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
10968 49 : update_stmt (stmt);
10969 : }
10970 : else
10971 74 : gimple_seq_add_stmt (&stmts, cvt);
10972 123 : return stmts;
10973 : }
10974 :
10975 : /* VCE from integral types to another integral types but with
10976 : smaller precisions need to be changed into casts
10977 : to be well defined. */
10978 65286 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
10979 : {
10980 60 : tree rhs = gimple_assign_rhs1 (stmt);
10981 60 : tree new_rhs = TREE_OPERAND (rhs, 0);
10982 60 : gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
10983 : < TYPE_PRECISION (TREE_TYPE (new_rhs)));
10984 60 : gcc_assert (is_gimple_val (new_rhs));
10985 60 : gimple_assign_set_rhs_code (stmt, NOP_EXPR);
10986 60 : gimple_assign_set_rhs1 (stmt, new_rhs);
10987 60 : if (in_place)
10988 51 : update_stmt (stmt);
10989 : else
10990 : {
10991 9 : gimple_set_modified (stmt, true);
10992 9 : gimple_seq_add_stmt (&stmts, stmt);
10993 : }
10994 60 : return stmts;
10995 : }
10996 65226 : tree type = unsigned_type_for (TREE_TYPE (lhs));
10997 65226 : if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
10998 24 : gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
10999 : else
11000 195116 : for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
11001 : {
11002 129914 : tree op = gimple_op (stmt, i);
11003 129914 : op = gimple_convert (&stmts, type, op);
11004 129914 : gimple_set_op (stmt, i, op);
11005 : }
11006 65226 : gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
11007 65226 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
11008 8868 : gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
11009 65226 : gimple_set_modified (stmt, true);
11010 65226 : if (in_place)
11011 : {
11012 47319 : if (stmts)
11013 46959 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
11014 47319 : stmts = NULL;
11015 : }
11016 : else
11017 17907 : gimple_seq_add_stmt (&stmts, stmt);
11018 65226 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
11019 65226 : if (in_place)
11020 : {
11021 47319 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
11022 47319 : update_stmt (stmt);
11023 : }
11024 : else
11025 17907 : gimple_seq_add_stmt (&stmts, cvt);
11026 :
11027 65226 : return stmts;
11028 : }
11029 :
11030 : void
11031 47419 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
11032 : {
11033 47419 : rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
11034 47419 : }
11035 :
11036 : gimple_seq
11037 17990 : rewrite_to_defined_unconditional (gimple *stmt)
11038 : {
11039 17990 : return rewrite_to_defined_unconditional (nullptr, stmt, false);
11040 : }
11041 :
11042 : /* The valueization hook we use for the gimple_build API simplification.
11043 : This makes us match fold_buildN behavior by only combining with
11044 : statements in the sequence(s) we are currently building. */
11045 :
11046 : static tree
11047 20684244 : gimple_build_valueize (tree op)
11048 : {
11049 20684244 : if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
11050 4391109 : return op;
11051 : return NULL_TREE;
11052 : }
11053 :
11054 : /* Helper for gimple_build to perform the final insertion of stmts on SEQ. */
11055 :
11056 : static inline void
11057 1344242 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
11058 : bool before, gsi_iterator_update update,
11059 : gimple_seq seq)
11060 : {
11061 1344242 : if (before)
11062 : {
11063 98062 : if (gsi->bb)
11064 98062 : gsi_insert_seq_before (gsi, seq, update);
11065 : else
11066 0 : gsi_insert_seq_before_without_update (gsi, seq, update);
11067 : }
11068 : else
11069 : {
11070 1246180 : if (gsi->bb)
11071 163 : gsi_insert_seq_after (gsi, seq, update);
11072 : else
11073 1246017 : gsi_insert_seq_after_without_update (gsi, seq, update);
11074 : }
11075 1344242 : }
11076 :
11077 : /* Build the expression CODE OP0 of type TYPE with location LOC,
11078 : simplifying it first if possible. Returns the built
11079 : expression value and inserts statements possibly defining it
11080 : before GSI if BEFORE is true or after GSI if false and advance
11081 : the iterator accordingly.
11082 : If gsi refers to a basic block simplifying is allowed to look
11083 : at all SSA defs while when it does not it is restricted to
11084 : SSA defs that are not associated with a basic block yet,
11085 : indicating they belong to the currently building sequence. */
11086 :
11087 : tree
11088 346281 : gimple_build (gimple_stmt_iterator *gsi,
11089 : bool before, gsi_iterator_update update,
11090 : location_t loc, enum tree_code code, tree type, tree op0)
11091 : {
11092 346281 : gimple_seq seq = NULL;
11093 346281 : tree res
11094 346281 : = gimple_simplify (code, type, op0, &seq,
11095 346281 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
11096 346281 : if (!res)
11097 : {
11098 304205 : res = make_ssa_name (type);
11099 304205 : gimple *stmt;
11100 304205 : if (code == REALPART_EXPR
11101 : || code == IMAGPART_EXPR
11102 304205 : || code == VIEW_CONVERT_EXPR)
11103 15740 : stmt = gimple_build_assign (res, code, build1 (code, type, op0));
11104 : else
11105 288465 : stmt = gimple_build_assign (res, code, op0);
11106 304205 : gimple_set_location (stmt, loc);
11107 304205 : gimple_seq_add_stmt_without_update (&seq, stmt);
11108 : }
11109 346281 : gimple_build_insert_seq (gsi, before, update, seq);
11110 346281 : return res;
11111 : }
11112 :
11113 : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
11114 : simplifying it first if possible. Returns the built
11115 : expression value inserting any new statements at GSI honoring BEFORE
11116 : and UPDATE. */
11117 :
11118 : tree
11119 785137 : gimple_build (gimple_stmt_iterator *gsi,
11120 : bool before, gsi_iterator_update update,
11121 : location_t loc, enum tree_code code, tree type,
11122 : tree op0, tree op1)
11123 : {
11124 785137 : gimple_seq seq = NULL;
11125 785137 : tree res
11126 785137 : = gimple_simplify (code, type, op0, op1, &seq,
11127 785137 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
11128 785137 : if (!res)
11129 : {
11130 691280 : res = make_ssa_name (type);
11131 691280 : gimple *stmt = gimple_build_assign (res, code, op0, op1);
11132 691280 : gimple_set_location (stmt, loc);
11133 691280 : gimple_seq_add_stmt_without_update (&seq, stmt);
11134 : }
11135 785137 : gimple_build_insert_seq (gsi, before, update, seq);
11136 785137 : return res;
11137 : }
11138 :
11139 : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
11140 : simplifying it first if possible. Returns the built
11141 : expression value inserting any new statements at GSI honoring BEFORE
11142 : and UPDATE. */
11143 :
11144 : tree
11145 45887 : gimple_build (gimple_stmt_iterator *gsi,
11146 : bool before, gsi_iterator_update update,
11147 : location_t loc, enum tree_code code, tree type,
11148 : tree op0, tree op1, tree op2)
11149 : {
11150 :
11151 45887 : gimple_seq seq = NULL;
11152 45887 : tree res
11153 45887 : = gimple_simplify (code, type, op0, op1, op2, &seq,
11154 45887 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
11155 45887 : if (!res)
11156 : {
11157 32542 : res = make_ssa_name (type);
11158 32542 : gimple *stmt;
11159 32542 : if (code == BIT_FIELD_REF)
11160 24927 : stmt = gimple_build_assign (res, code,
11161 : build3 (code, type, op0, op1, op2));
11162 : else
11163 7615 : stmt = gimple_build_assign (res, code, op0, op1, op2);
11164 32542 : gimple_set_location (stmt, loc);
11165 32542 : gimple_seq_add_stmt_without_update (&seq, stmt);
11166 : }
11167 45887 : gimple_build_insert_seq (gsi, before, update, seq);
11168 45887 : return res;
11169 : }
11170 :
11171 : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
11172 : void) with a location LOC. Returns the built expression value (or NULL_TREE
11173 : if TYPE is void) inserting any new statements at GSI honoring BEFORE
11174 : and UPDATE. */
11175 :
11176 : tree
11177 0 : gimple_build (gimple_stmt_iterator *gsi,
11178 : bool before, gsi_iterator_update update,
11179 : location_t loc, combined_fn fn, tree type)
11180 : {
11181 0 : tree res = NULL_TREE;
11182 0 : gimple_seq seq = NULL;
11183 0 : gcall *stmt;
11184 0 : if (internal_fn_p (fn))
11185 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
11186 : else
11187 : {
11188 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11189 0 : stmt = gimple_build_call (decl, 0);
11190 : }
11191 0 : if (!VOID_TYPE_P (type))
11192 : {
11193 0 : res = make_ssa_name (type);
11194 0 : gimple_call_set_lhs (stmt, res);
11195 : }
11196 0 : gimple_set_location (stmt, loc);
11197 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11198 0 : gimple_build_insert_seq (gsi, before, update, seq);
11199 0 : return res;
11200 : }
11201 :
11202 : /* Build the call FN (ARG0) with a result of type TYPE
11203 : (or no result if TYPE is void) with location LOC,
11204 : simplifying it first if possible. Returns the built
11205 : expression value (or NULL_TREE if TYPE is void) inserting any new
11206 : statements at GSI honoring BEFORE and UPDATE. */
11207 :
11208 : tree
11209 24663 : gimple_build (gimple_stmt_iterator *gsi,
11210 : bool before, gsi_iterator_update update,
11211 : location_t loc, combined_fn fn,
11212 : tree type, tree arg0)
11213 : {
11214 24663 : gimple_seq seq = NULL;
11215 24663 : tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
11216 24663 : if (!res)
11217 : {
11218 24663 : gcall *stmt;
11219 24663 : if (internal_fn_p (fn))
11220 24285 : stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
11221 : else
11222 : {
11223 378 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11224 378 : stmt = gimple_build_call (decl, 1, arg0);
11225 : }
11226 24663 : if (!VOID_TYPE_P (type))
11227 : {
11228 24285 : res = make_ssa_name (type);
11229 24285 : gimple_call_set_lhs (stmt, res);
11230 : }
11231 24663 : gimple_set_location (stmt, loc);
11232 24663 : gimple_seq_add_stmt_without_update (&seq, stmt);
11233 : }
11234 24663 : gimple_build_insert_seq (gsi, before, update, seq);
11235 24663 : return res;
11236 : }
11237 :
11238 : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
11239 : (or no result if TYPE is void) with location LOC,
11240 : simplifying it first if possible. Returns the built
11241 : expression value (or NULL_TREE if TYPE is void) inserting any new
11242 : statements at GSI honoring BEFORE and UPDATE. */
11243 :
11244 : tree
11245 0 : gimple_build (gimple_stmt_iterator *gsi,
11246 : bool before, gsi_iterator_update update,
11247 : location_t loc, combined_fn fn,
11248 : tree type, tree arg0, tree arg1)
11249 : {
11250 0 : gimple_seq seq = NULL;
11251 0 : tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
11252 : gimple_build_valueize);
11253 0 : if (!res)
11254 : {
11255 0 : gcall *stmt;
11256 0 : if (internal_fn_p (fn))
11257 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
11258 : else
11259 : {
11260 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11261 0 : stmt = gimple_build_call (decl, 2, arg0, arg1);
11262 : }
11263 0 : if (!VOID_TYPE_P (type))
11264 : {
11265 0 : res = make_ssa_name (type);
11266 0 : gimple_call_set_lhs (stmt, res);
11267 : }
11268 0 : gimple_set_location (stmt, loc);
11269 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11270 : }
11271 0 : gimple_build_insert_seq (gsi, before, update, seq);
11272 0 : return res;
11273 : }
11274 :
11275 : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
11276 : (or no result if TYPE is void) with location LOC,
11277 : simplifying it first if possible. Returns the built
11278 : expression value (or NULL_TREE if TYPE is void) inserting any new
11279 : statements at GSI honoring BEFORE and UPDATE. */
11280 :
11281 : tree
11282 0 : gimple_build (gimple_stmt_iterator *gsi,
11283 : bool before, gsi_iterator_update update,
11284 : location_t loc, combined_fn fn,
11285 : tree type, tree arg0, tree arg1, tree arg2)
11286 : {
11287 0 : gimple_seq seq = NULL;
11288 0 : tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
11289 : &seq, gimple_build_valueize);
11290 0 : if (!res)
11291 : {
11292 0 : gcall *stmt;
11293 0 : if (internal_fn_p (fn))
11294 0 : stmt = gimple_build_call_internal (as_internal_fn (fn),
11295 : 3, arg0, arg1, arg2);
11296 : else
11297 : {
11298 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11299 0 : stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
11300 : }
11301 0 : if (!VOID_TYPE_P (type))
11302 : {
11303 0 : res = make_ssa_name (type);
11304 0 : gimple_call_set_lhs (stmt, res);
11305 : }
11306 0 : gimple_set_location (stmt, loc);
11307 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11308 : }
11309 0 : gimple_build_insert_seq (gsi, before, update, seq);
11310 0 : return res;
11311 : }
11312 :
11313 : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
11314 : void) with location LOC, simplifying it first if possible. Returns the
11315 : built expression value (or NULL_TREE if TYPE is void) inserting any new
11316 : statements at GSI honoring BEFORE and UPDATE. */
11317 :
11318 : tree
11319 21 : gimple_build (gimple_stmt_iterator *gsi,
11320 : bool before, gsi_iterator_update update,
11321 : location_t loc, code_helper code, tree type, tree op0)
11322 : {
11323 21 : if (code.is_tree_code ())
11324 0 : return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
11325 21 : return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
11326 : }
11327 :
11328 : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
11329 : void) with location LOC, simplifying it first if possible. Returns the
11330 : built expression value (or NULL_TREE if TYPE is void) inserting any new
11331 : statements at GSI honoring BEFORE and UPDATE. */
11332 :
11333 : tree
11334 24179 : gimple_build (gimple_stmt_iterator *gsi,
11335 : bool before, gsi_iterator_update update,
11336 : location_t loc, code_helper code, tree type, tree op0, tree op1)
11337 : {
11338 24179 : if (code.is_tree_code ())
11339 24179 : return gimple_build (gsi, before, update,
11340 24179 : loc, tree_code (code), type, op0, op1);
11341 0 : return gimple_build (gsi, before, update,
11342 0 : loc, combined_fn (code), type, op0, op1);
11343 : }
11344 :
11345 : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
11346 : is void) with location LOC, simplifying it first if possible. Returns the
11347 : built expression value (or NULL_TREE if TYPE is void) inserting any new
11348 : statements at GSI honoring BEFORE and UPDATE. */
11349 :
11350 : tree
11351 0 : gimple_build (gimple_stmt_iterator *gsi,
11352 : bool before, gsi_iterator_update update,
11353 : location_t loc, code_helper code,
11354 : tree type, tree op0, tree op1, tree op2)
11355 : {
11356 0 : if (code.is_tree_code ())
11357 0 : return gimple_build (gsi, before, update,
11358 0 : loc, tree_code (code), type, op0, op1, op2);
11359 0 : return gimple_build (gsi, before, update,
11360 0 : loc, combined_fn (code), type, op0, op1, op2);
11361 : }
11362 :
11363 : /* Build the conversion (TYPE) OP with a result of type TYPE
11364 : with location LOC if such conversion is necessary in GIMPLE,
11365 : simplifying it first.
11366 : Returns the built expression inserting any new statements
11367 : at GSI honoring BEFORE and UPDATE. */
11368 :
11369 : tree
11370 2039559 : gimple_convert (gimple_stmt_iterator *gsi,
11371 : bool before, gsi_iterator_update update,
11372 : location_t loc, tree type, tree op)
11373 : {
11374 2039559 : if (useless_type_conversion_p (type, TREE_TYPE (op)))
11375 : return op;
11376 186717 : return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
11377 : }
11378 :
11379 : /* Build the conversion (ptrofftype) OP with a result of a type
11380 : compatible with ptrofftype with location LOC if such conversion
11381 : is necessary in GIMPLE, simplifying it first.
11382 : Returns the built expression value inserting any new statements
11383 : at GSI honoring BEFORE and UPDATE. */
11384 :
11385 : tree
11386 208 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
11387 : bool before, gsi_iterator_update update,
11388 : location_t loc, tree op)
11389 : {
11390 208 : if (ptrofftype_p (TREE_TYPE (op)))
11391 : return op;
11392 0 : return gimple_convert (gsi, before, update, loc, sizetype, op);
11393 : }
11394 :
11395 : /* Build a vector of type TYPE in which each element has the value OP.
11396 : Return a gimple value for the result, inserting any new statements
11397 : at GSI honoring BEFORE and UPDATE. */
11398 :
11399 : tree
11400 327580 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
11401 : bool before, gsi_iterator_update update,
11402 : location_t loc, tree type, tree op)
11403 : {
11404 327580 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
11405 : && !CONSTANT_CLASS_P (op))
11406 : return gimple_build (gsi, before, update,
11407 : loc, VEC_DUPLICATE_EXPR, type, op);
11408 :
11409 327580 : tree res, vec = build_vector_from_val (type, op);
11410 327580 : if (is_gimple_val (vec))
11411 : return vec;
11412 27768 : if (gimple_in_ssa_p (cfun))
11413 27768 : res = make_ssa_name (type);
11414 : else
11415 0 : res = create_tmp_reg (type);
11416 27768 : gimple_seq seq = NULL;
11417 27768 : gimple *stmt = gimple_build_assign (res, vec);
11418 27768 : gimple_set_location (stmt, loc);
11419 27768 : gimple_seq_add_stmt_without_update (&seq, stmt);
11420 27768 : gimple_build_insert_seq (gsi, before, update, seq);
11421 27768 : return res;
11422 : }
11423 :
11424 : /* Build a vector from BUILDER, handling the case in which some elements
11425 : are non-constant. Return a gimple value for the result, inserting
11426 : any new instructions to GSI honoring BEFORE and UPDATE.
11427 :
11428 : BUILDER must not have a stepped encoding on entry. This is because
11429 : the function is not geared up to handle the arithmetic that would
11430 : be needed in the variable case, and any code building a vector that
11431 : is known to be constant should use BUILDER->build () directly. */
11432 :
11433 : tree
11434 372329 : gimple_build_vector (gimple_stmt_iterator *gsi,
11435 : bool before, gsi_iterator_update update,
11436 : location_t loc, tree_vector_builder *builder)
11437 : {
11438 372329 : gcc_assert (builder->nelts_per_pattern () <= 2);
11439 372329 : unsigned int encoded_nelts = builder->encoded_nelts ();
11440 1313794 : for (unsigned int i = 0; i < encoded_nelts; ++i)
11441 1055971 : if (!CONSTANT_CLASS_P ((*builder)[i]))
11442 : {
11443 114506 : gimple_seq seq = NULL;
11444 114506 : tree type = builder->type ();
11445 114506 : unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11446 114506 : vec<constructor_elt, va_gc> *v;
11447 114506 : vec_alloc (v, nelts);
11448 365144 : for (i = 0; i < nelts; ++i)
11449 250638 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
11450 :
11451 114506 : tree res;
11452 114506 : if (gimple_in_ssa_p (cfun))
11453 114506 : res = make_ssa_name (type);
11454 : else
11455 0 : res = create_tmp_reg (type);
11456 114506 : gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
11457 114506 : gimple_set_location (stmt, loc);
11458 114506 : gimple_seq_add_stmt_without_update (&seq, stmt);
11459 114506 : gimple_build_insert_seq (gsi, before, update, seq);
11460 114506 : return res;
11461 : }
11462 257823 : return builder->build ();
11463 : }
11464 :
11465 : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
11466 : and generate a value guaranteed to be rounded upwards to ALIGN.
11467 :
11468 : Return the tree node representing this size, it is of TREE_TYPE TYPE. */
11469 :
11470 : tree
11471 0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
11472 : bool before, gsi_iterator_update update,
11473 : location_t loc, tree type,
11474 : tree old_size, unsigned HOST_WIDE_INT align)
11475 : {
11476 0 : unsigned HOST_WIDE_INT tg_mask = align - 1;
11477 : /* tree new_size = (old_size + tg_mask) & ~tg_mask; */
11478 0 : gcc_assert (INTEGRAL_TYPE_P (type));
11479 0 : tree tree_mask = build_int_cst (type, tg_mask);
11480 0 : tree oversize = gimple_build (gsi, before, update,
11481 : loc, PLUS_EXPR, type, old_size, tree_mask);
11482 :
11483 0 : tree mask = build_int_cst (type, -align);
11484 0 : return gimple_build (gsi, before, update,
11485 0 : loc, BIT_AND_EXPR, type, oversize, mask);
11486 : }
11487 :
11488 : /* Return true if the result of assignment STMT is known to be non-negative.
11489 : DEPTH is the current nesting depth of the query. */
11490 :
11491 : static bool
11492 6548978 : gimple_assign_nonnegative_p (gimple *stmt, int depth)
11493 : {
11494 6548978 : enum tree_code code = gimple_assign_rhs_code (stmt);
11495 6548978 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
11496 6548978 : switch (get_gimple_rhs_class (code))
11497 : {
11498 550716 : case GIMPLE_UNARY_RHS:
11499 550716 : return tree_unary_nonnegative_p (gimple_assign_rhs_code (stmt),
11500 : type,
11501 : gimple_assign_rhs1 (stmt),
11502 550716 : depth);
11503 3834522 : case GIMPLE_BINARY_RHS:
11504 3834522 : return tree_binary_nonnegative_p (gimple_assign_rhs_code (stmt),
11505 : type,
11506 : gimple_assign_rhs1 (stmt),
11507 : gimple_assign_rhs2 (stmt),
11508 3834522 : depth);
11509 : case GIMPLE_TERNARY_RHS:
11510 : return false;
11511 2146849 : case GIMPLE_SINGLE_RHS:
11512 2146849 : return tree_single_nonnegative_p (gimple_assign_rhs1 (stmt),
11513 2146849 : depth);
11514 : case GIMPLE_INVALID_RHS:
11515 : break;
11516 : }
11517 0 : gcc_unreachable ();
11518 : }
11519 :
11520 : /* Return true if return value of call STMT is known to be non-negative.
11521 : DEPTH is the current nesting depth of the query. */
11522 :
11523 : static bool
11524 12841124 : gimple_call_nonnegative_p (gimple *stmt, int depth)
11525 : {
11526 12841124 : tree arg0
11527 12841124 : = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
11528 12841124 : tree arg1
11529 12841124 : = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
11530 12841124 : tree lhs = gimple_call_lhs (stmt);
11531 12841124 : return (lhs
11532 12841124 : && tree_call_nonnegative_p (TREE_TYPE (lhs),
11533 : gimple_call_combined_fn (stmt),
11534 12841124 : arg0, arg1, depth));
11535 : }
11536 :
11537 : /* Return true if return value of call STMT is known to be non-negative.
11538 : DEPTH is the current nesting depth of the query. */
11539 :
11540 : static bool
11541 1221692 : gimple_phi_nonnegative_p (gimple *stmt, int depth)
11542 : {
11543 1651885 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11544 : {
11545 1639115 : tree arg = gimple_phi_arg_def (stmt, i);
11546 1639115 : if (!tree_single_nonnegative_p (arg, depth + 1))
11547 : return false;
11548 : }
11549 : return true;
11550 : }
11551 :
11552 : /* Return true if STMT is known to compute a non-negative value.
11553 : DEPTH is the current nesting depth of the query. */
11554 :
11555 : bool
11556 21268253 : gimple_stmt_nonnegative_p (gimple *stmt, int depth)
11557 : {
11558 21268253 : tree type = gimple_range_type (stmt);
11559 21268253 : if (type && frange::supports_p (type))
11560 : {
11561 1508051 : frange r;
11562 1508051 : bool sign;
11563 1508051 : if (get_global_range_query ()->range_of_stmt (r, stmt)
11564 1508051 : && r.signbit_p (sign))
11565 28261 : return !sign;
11566 1508051 : }
11567 21239992 : switch (gimple_code (stmt))
11568 : {
11569 6548978 : case GIMPLE_ASSIGN:
11570 6548978 : return gimple_assign_nonnegative_p (stmt, depth);
11571 12841124 : case GIMPLE_CALL:
11572 12841124 : return gimple_call_nonnegative_p (stmt, depth);
11573 1221692 : case GIMPLE_PHI:
11574 1221692 : return gimple_phi_nonnegative_p (stmt, depth);
11575 : default:
11576 : return false;
11577 : }
11578 : }
11579 :
11580 : /* Return true if the floating-point value computed by assignment STMT
11581 : is known to have an integer value. We also allow +Inf, -Inf and NaN
11582 : to be considered integer values. Return false for signaling NaN.
11583 :
11584 : DEPTH is the current nesting depth of the query. */
11585 :
11586 : static bool
11587 58569 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
11588 : {
11589 58569 : enum tree_code code = gimple_assign_rhs_code (stmt);
11590 58569 : switch (get_gimple_rhs_class (code))
11591 : {
11592 15024 : case GIMPLE_UNARY_RHS:
11593 15024 : return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
11594 15024 : gimple_assign_rhs1 (stmt), depth);
11595 13267 : case GIMPLE_BINARY_RHS:
11596 13267 : return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
11597 : gimple_assign_rhs1 (stmt),
11598 13267 : gimple_assign_rhs2 (stmt), depth);
11599 : case GIMPLE_TERNARY_RHS:
11600 : return false;
11601 29123 : case GIMPLE_SINGLE_RHS:
11602 29123 : return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
11603 : case GIMPLE_INVALID_RHS:
11604 : break;
11605 : }
11606 0 : gcc_unreachable ();
11607 : }
11608 :
11609 : /* Return true if the floating-point value computed by call STMT is known
11610 : to have an integer value. We also allow +Inf, -Inf and NaN to be
11611 : considered integer values. Return false for signaling NaN.
11612 :
11613 : DEPTH is the current nesting depth of the query. */
11614 :
11615 : static bool
11616 1089 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
11617 : {
11618 1089 : tree arg0 = (gimple_call_num_args (stmt) > 0
11619 1089 : ? gimple_call_arg (stmt, 0)
11620 : : NULL_TREE);
11621 1089 : tree arg1 = (gimple_call_num_args (stmt) > 1
11622 1089 : ? gimple_call_arg (stmt, 1)
11623 : : NULL_TREE);
11624 1089 : return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
11625 1089 : arg0, arg1, depth);
11626 : }
11627 :
11628 : /* Return true if the floating-point result of phi STMT is known to have
11629 : an integer value. We also allow +Inf, -Inf and NaN to be considered
11630 : integer values. Return false for signaling NaN.
11631 :
11632 : DEPTH is the current nesting depth of the query. */
11633 :
11634 : static bool
11635 1489 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
11636 : {
11637 1652 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11638 : {
11639 1643 : tree arg = gimple_phi_arg_def (stmt, i);
11640 1643 : if (!integer_valued_real_single_p (arg, depth + 1))
11641 : return false;
11642 : }
11643 : return true;
11644 : }
11645 :
11646 : /* Return true if the floating-point value computed by STMT is known
11647 : to have an integer value. We also allow +Inf, -Inf and NaN to be
11648 : considered integer values. Return false for signaling NaN.
11649 :
11650 : DEPTH is the current nesting depth of the query. */
11651 :
11652 : bool
11653 88524 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
11654 : {
11655 88524 : switch (gimple_code (stmt))
11656 : {
11657 58569 : case GIMPLE_ASSIGN:
11658 58569 : return gimple_assign_integer_valued_real_p (stmt, depth);
11659 1089 : case GIMPLE_CALL:
11660 1089 : return gimple_call_integer_valued_real_p (stmt, depth);
11661 1489 : case GIMPLE_PHI:
11662 1489 : return gimple_phi_integer_valued_real_p (stmt, depth);
11663 : default:
11664 : return false;
11665 : }
11666 : }
|