Branch data Line data Source code
1 : : /* Statement simplification on GIMPLE.
2 : : Copyright (C) 2010-2025 Free Software Foundation, Inc.
3 : : Split out from tree-ssa-ccp.cc.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by the
9 : : Free Software Foundation; either version 3, or (at your option) any
10 : : later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT
13 : : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "target.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "predict.h"
30 : : #include "ssa.h"
31 : : #include "cgraph.h"
32 : : #include "gimple-pretty-print.h"
33 : : #include "gimple-ssa-warn-access.h"
34 : : #include "gimple-ssa-warn-restrict.h"
35 : : #include "fold-const.h"
36 : : #include "stmt.h"
37 : : #include "expr.h"
38 : : #include "stor-layout.h"
39 : : #include "dumpfile.h"
40 : : #include "gimple-iterator.h"
41 : : #include "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 elsehwere.
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 : 4572570 : can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
116 : : {
117 : 4572570 : varpool_node *vnode;
118 : 4572570 : struct cgraph_node *node;
119 : 4572570 : symtab_node *snode;
120 : :
121 : 4572570 : if (DECL_ABSTRACT_P (decl))
122 : : return false;
123 : :
124 : : /* We are concerned only about static/external vars and functions. */
125 : 1513634 : if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
126 : 5682289 : || !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 : 4168655 : if (!TREE_PUBLIC (decl))
132 : : {
133 : 1092069 : 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 : 1092024 : if (symtab->function_flags_ready)
138 : : return true;
139 : 1055957 : snode = symtab_node::get (decl);
140 : 1055957 : if (!snode || !snode->definition)
141 : : return false;
142 : 1055916 : node = dyn_cast <cgraph_node *> (snode);
143 : 1064966 : 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 : 3076586 : if (!from_decl
150 : 618557 : || !VAR_P (from_decl)
151 : 617436 : || (!DECL_EXTERNAL (from_decl)
152 : 304989 : && (vnode = varpool_node::get (from_decl)) != NULL
153 : 192950 : && vnode->definition)
154 : 3501111 : || (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 reffer
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 : 424522 : if (DECL_VISIBILITY_SPECIFIED (decl)
162 : 417555 : && DECL_EXTERNAL (decl)
163 : 308495 : && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
164 : 631985 : && (!(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 curren tunit. */
169 : 217059 : 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 : 164787 : if (!symtab->function_flags_ready)
183 : : return true;
184 : :
185 : 149886 : snode = symtab_node::get (decl);
186 : 149886 : if (!snode
187 : 149886 : || ((!snode->definition || DECL_EXTERNAL (decl))
188 : 15218 : && (!snode->in_other_partition
189 : 0 : || (!snode->forced_by_abi && !snode->force_output))))
190 : : return false;
191 : 100076 : node = dyn_cast <cgraph_node *> (snode);
192 : 100076 : 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 : 15669622 : canonicalize_constructor_val (tree cval, tree from_decl)
201 : : {
202 : 15669622 : if (CONSTANT_CLASS_P (cval))
203 : : return cval;
204 : :
205 : 9380371 : tree orig_cval = cval;
206 : 9380371 : STRIP_NOPS (cval);
207 : 9380371 : if (TREE_CODE (cval) == POINTER_PLUS_EXPR
208 : 9380371 : && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
209 : : {
210 : 71523 : tree ptr = TREE_OPERAND (cval, 0);
211 : 71523 : if (is_gimple_min_invariant (ptr))
212 : 213441 : cval = build1_loc (EXPR_LOCATION (cval),
213 : 71147 : ADDR_EXPR, TREE_TYPE (ptr),
214 : 142294 : fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
215 : : ptr,
216 : : fold_convert (ptr_type_node,
217 : : TREE_OPERAND (cval, 1))));
218 : : }
219 : 9380371 : if (TREE_CODE (cval) == ADDR_EXPR)
220 : : {
221 : 5160307 : tree base = NULL_TREE;
222 : 5160307 : 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 : 5160114 : base = get_base_address (TREE_OPERAND (cval, 0));
230 : 5160307 : if (!base)
231 : 0 : return NULL_TREE;
232 : :
233 : 2272761 : if (VAR_OR_FUNCTION_DECL_P (base)
234 : 6459746 : && !can_refer_decl_in_current_unit_p (base, from_decl))
235 : : return NULL_TREE;
236 : 4951943 : if (TREE_TYPE (base) == error_mark_node)
237 : : return NULL_TREE;
238 : 4951943 : 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 : 2271758 : 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 : 1298436 : cgraph_node::get_create (base);
248 : : }
249 : : /* Fixup types in global initializers. */
250 : 4951943 : if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
251 : 38177 : cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
252 : :
253 : 4951943 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
254 : 213392 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
255 : 4951943 : return cval;
256 : : }
257 : : /* In CONSTRUCTORs we may see unfolded constants like (int (*) ()) 0. */
258 : 4220064 : if (TREE_CODE (cval) == INTEGER_CST)
259 : : {
260 : 65502 : if (TREE_OVERFLOW_P (cval))
261 : 0 : cval = drop_tree_overflow (cval);
262 : 65502 : if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
263 : 62301 : cval = fold_convert (TREE_TYPE (orig_cval), cval);
264 : 65502 : 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 : 21403333 : get_symbol_constant_value (tree sym)
274 : : {
275 : 21403333 : tree val = ctor_for_folding (sym);
276 : 21403333 : if (val != error_mark_node)
277 : : {
278 : 41465 : if (val)
279 : : {
280 : 39205 : val = canonicalize_constructor_val (unshare_expr (val), sym);
281 : 39205 : if (val
282 : 39205 : && is_gimple_min_invariant (val)
283 : 68789 : && useless_type_conversion_p (TREE_TYPE (sym), TREE_TYPE (val)))
284 : : return val;
285 : : else
286 : 9727 : 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 : 2260 : if (!val
292 : 2260 : && is_gimple_reg_type (TREE_TYPE (sym)))
293 : 1921 : 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 : 63812957 : maybe_fold_reference (tree expr)
306 : : {
307 : 63812957 : tree result = NULL_TREE;
308 : :
309 : 63812957 : if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
310 : 61838137 : || TREE_CODE (expr) == REALPART_EXPR
311 : 61138369 : || TREE_CODE (expr) == IMAGPART_EXPR)
312 : 65346805 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
313 : 800 : result = fold_unary_loc (EXPR_LOCATION (expr),
314 : : TREE_CODE (expr),
315 : 800 : TREE_TYPE (expr),
316 : 800 : TREE_OPERAND (expr, 0));
317 : 63812157 : else if (TREE_CODE (expr) == BIT_FIELD_REF
318 : 63812157 : && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
319 : 4 : result = fold_ternary_loc (EXPR_LOCATION (expr),
320 : : TREE_CODE (expr),
321 : 4 : TREE_TYPE (expr),
322 : 4 : TREE_OPERAND (expr, 0),
323 : 4 : TREE_OPERAND (expr, 1),
324 : 4 : TREE_OPERAND (expr, 2));
325 : : else
326 : 63812153 : result = fold_const_aggregate_ref (expr);
327 : :
328 : 63812957 : if (result && is_gimple_min_invariant (result))
329 : : return result;
330 : :
331 : : return NULL_TREE;
332 : : }
333 : :
334 : : /* Return true if EXPR is an acceptable right-hand-side for a
335 : : GIMPLE assignment. We validate the entire tree, not just
336 : : the root node, thus catching expressions that embed complex
337 : : operands that are not permitted in GIMPLE. This function
338 : : is needed because the folding routines in fold-const.cc
339 : : may return such expressions in some cases, e.g., an array
340 : : access with an embedded index addition. It may make more
341 : : sense to have folding routines that are sensitive to the
342 : : constraints on GIMPLE operands, rather than abandoning any
343 : : any attempt to fold if the usual folding turns out to be too
344 : : aggressive. */
345 : :
346 : : bool
347 : 0 : valid_gimple_rhs_p (tree expr)
348 : : {
349 : 0 : enum tree_code code = TREE_CODE (expr);
350 : :
351 : 0 : switch (TREE_CODE_CLASS (code))
352 : : {
353 : 0 : case tcc_declaration:
354 : 0 : if (!is_gimple_variable (expr))
355 : : return false;
356 : : break;
357 : :
358 : : case tcc_constant:
359 : : /* All constants are ok. */
360 : : break;
361 : :
362 : 0 : case tcc_comparison:
363 : : /* GENERIC allows comparisons with non-boolean types, reject
364 : : those for GIMPLE. Let vector-typed comparisons pass - rules
365 : : for GENERIC and GIMPLE are the same here. */
366 : 0 : if (!(INTEGRAL_TYPE_P (TREE_TYPE (expr))
367 : 0 : && (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
368 : 0 : || TYPE_PRECISION (TREE_TYPE (expr)) == 1))
369 : 0 : && ! VECTOR_TYPE_P (TREE_TYPE (expr)))
370 : : return false;
371 : :
372 : : /* Fallthru. */
373 : 0 : case tcc_binary:
374 : 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0))
375 : 0 : || !is_gimple_val (TREE_OPERAND (expr, 1)))
376 : 0 : return false;
377 : : break;
378 : :
379 : 0 : case tcc_unary:
380 : 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0)))
381 : : return false;
382 : : break;
383 : :
384 : 0 : case tcc_expression:
385 : 0 : switch (code)
386 : : {
387 : 0 : case ADDR_EXPR:
388 : 0 : {
389 : 0 : tree t;
390 : 0 : if (is_gimple_min_invariant (expr))
391 : : return true;
392 : 0 : t = TREE_OPERAND (expr, 0);
393 : 0 : while (handled_component_p (t))
394 : : {
395 : : /* ??? More checks needed, see the GIMPLE verifier. */
396 : 0 : if ((TREE_CODE (t) == ARRAY_REF
397 : 0 : || TREE_CODE (t) == ARRAY_RANGE_REF)
398 : 0 : && !is_gimple_val (TREE_OPERAND (t, 1)))
399 : : return false;
400 : 0 : t = TREE_OPERAND (t, 0);
401 : : }
402 : 0 : if (!is_gimple_id (t))
403 : : return false;
404 : : }
405 : : break;
406 : :
407 : 0 : default:
408 : 0 : if (get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS)
409 : : {
410 : 0 : if (!is_gimple_val (TREE_OPERAND (expr, 0))
411 : 0 : || !is_gimple_val (TREE_OPERAND (expr, 1))
412 : 0 : || !is_gimple_val (TREE_OPERAND (expr, 2)))
413 : 0 : return false;
414 : : break;
415 : : }
416 : : return false;
417 : : }
418 : : break;
419 : :
420 : : case tcc_vl_exp:
421 : : return false;
422 : :
423 : 0 : case tcc_exceptional:
424 : 0 : if (code == CONSTRUCTOR)
425 : : {
426 : : unsigned i;
427 : : tree elt;
428 : 0 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
429 : 0 : if (!is_gimple_val (elt))
430 : : return false;
431 : : return true;
432 : : }
433 : 0 : if (code != SSA_NAME)
434 : : return false;
435 : : break;
436 : :
437 : 0 : case tcc_reference:
438 : 0 : if (code == BIT_FIELD_REF)
439 : 0 : return is_gimple_val (TREE_OPERAND (expr, 0));
440 : : return false;
441 : :
442 : : default:
443 : : return false;
444 : : }
445 : :
446 : : return true;
447 : : }
448 : :
449 : :
450 : : /* Attempt to fold an assignment statement pointed-to by SI. Returns a
451 : : replacement rhs for the statement or NULL_TREE if no simplification
452 : : could be made. It is assumed that the operands have been previously
453 : : folded. */
454 : :
455 : : static tree
456 : 254044569 : fold_gimple_assign (gimple_stmt_iterator *si)
457 : : {
458 : 254044569 : gimple *stmt = gsi_stmt (*si);
459 : 254044569 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
460 : 254044569 : location_t loc = gimple_location (stmt);
461 : :
462 : 254044569 : tree result = NULL_TREE;
463 : :
464 : 254044569 : switch (get_gimple_rhs_class (subcode))
465 : : {
466 : 168729096 : case GIMPLE_SINGLE_RHS:
467 : 168729096 : {
468 : 168729096 : tree rhs = gimple_assign_rhs1 (stmt);
469 : :
470 : 168729096 : if (TREE_CLOBBER_P (rhs))
471 : : return NULL_TREE;
472 : :
473 : 155474026 : if (REFERENCE_CLASS_P (rhs))
474 : 61366953 : return maybe_fold_reference (rhs);
475 : :
476 : 94107073 : else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
477 : : {
478 : 165174 : tree val = OBJ_TYPE_REF_EXPR (rhs);
479 : 165174 : if (is_gimple_min_invariant (val))
480 : : return val;
481 : 165152 : else if (flag_devirtualize && virtual_method_call_p (rhs))
482 : : {
483 : 165112 : bool final;
484 : 165112 : vec <cgraph_node *>targets
485 : 165112 : = possible_polymorphic_call_targets (rhs, stmt, &final);
486 : 165569 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
487 : : {
488 : 57 : if (dump_enabled_p ())
489 : : {
490 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
491 : : "resolving virtual function address "
492 : : "reference to function %s\n",
493 : 0 : targets.length () == 1
494 : 0 : ? targets[0]->name ()
495 : : : "NULL");
496 : : }
497 : 57 : if (targets.length () == 1)
498 : : {
499 : 46 : val = fold_convert (TREE_TYPE (val),
500 : : build_fold_addr_expr_loc
501 : : (loc, targets[0]->decl));
502 : 46 : STRIP_USELESS_TYPE_CONVERSION (val);
503 : : }
504 : : else
505 : : /* We cannot use __builtin_unreachable here because it
506 : : cannot have address taken. */
507 : 11 : val = build_int_cst (TREE_TYPE (val), 0);
508 : 57 : return val;
509 : : }
510 : : }
511 : : }
512 : :
513 : 93941899 : else if (TREE_CODE (rhs) == ADDR_EXPR)
514 : : {
515 : 14886065 : tree ref = TREE_OPERAND (rhs, 0);
516 : 14886065 : if (TREE_CODE (ref) == MEM_REF
517 : 14886065 : && integer_zerop (TREE_OPERAND (ref, 1)))
518 : : {
519 : 2231 : result = TREE_OPERAND (ref, 0);
520 : 2231 : if (!useless_type_conversion_p (TREE_TYPE (rhs),
521 : 2231 : TREE_TYPE (result)))
522 : 0 : result = build1 (NOP_EXPR, TREE_TYPE (rhs), result);
523 : 2231 : return result;
524 : : }
525 : : }
526 : :
527 : 79055834 : else if (TREE_CODE (rhs) == CONSTRUCTOR
528 : 79055834 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
529 : : {
530 : : /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
531 : : unsigned i;
532 : : tree val;
533 : :
534 : 415392 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
535 : 411154 : if (! CONSTANT_CLASS_P (val))
536 : : return NULL_TREE;
537 : :
538 : 4238 : return build_vector_from_ctor (TREE_TYPE (rhs),
539 : 8476 : CONSTRUCTOR_ELTS (rhs));
540 : : }
541 : :
542 : 78730982 : else if (DECL_P (rhs)
543 : 78730982 : && is_gimple_reg_type (TREE_TYPE (rhs)))
544 : 12257403 : return get_symbol_constant_value (rhs);
545 : : }
546 : : break;
547 : :
548 : : case GIMPLE_UNARY_RHS:
549 : : break;
550 : :
551 : : case GIMPLE_BINARY_RHS:
552 : : break;
553 : :
554 : 462415 : case GIMPLE_TERNARY_RHS:
555 : 924830 : result = fold_ternary_loc (loc, subcode,
556 : 462415 : TREE_TYPE (gimple_assign_lhs (stmt)),
557 : : gimple_assign_rhs1 (stmt),
558 : : gimple_assign_rhs2 (stmt),
559 : : gimple_assign_rhs3 (stmt));
560 : :
561 : 462415 : if (result)
562 : : {
563 : 0 : STRIP_USELESS_TYPE_CONVERSION (result);
564 : 0 : if (valid_gimple_rhs_p (result))
565 : : return result;
566 : : }
567 : : break;
568 : :
569 : 0 : case GIMPLE_INVALID_RHS:
570 : 0 : gcc_unreachable ();
571 : : }
572 : :
573 : : return NULL_TREE;
574 : : }
575 : :
576 : :
577 : : /* Replace a statement at *SI_P with a sequence of statements in STMTS,
578 : : adjusting the replacement stmts location and virtual operands.
579 : : If the statement has a lhs the last stmt in the sequence is expected
580 : : to assign to that lhs. */
581 : :
582 : : void
583 : 123365 : gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
584 : : {
585 : 123365 : gimple *stmt = gsi_stmt (*si_p);
586 : :
587 : 123365 : if (gimple_has_location (stmt))
588 : 101994 : annotate_all_with_location (stmts, gimple_location (stmt));
589 : :
590 : : /* First iterate over the replacement statements backward, assigning
591 : : virtual operands to their defining statements. */
592 : 123365 : gimple *laststore = NULL;
593 : 246730 : for (gimple_stmt_iterator i = gsi_last (stmts);
594 : 507659 : !gsi_end_p (i); gsi_prev (&i))
595 : : {
596 : 192147 : gimple *new_stmt = gsi_stmt (i);
597 : 192147 : if ((gimple_assign_single_p (new_stmt)
598 : 122189 : && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
599 : 314066 : || (is_gimple_call (new_stmt)
600 : 14906 : && (gimple_call_flags (new_stmt)
601 : 14906 : & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
602 : : {
603 : 2307 : tree vdef;
604 : 2307 : if (!laststore)
605 : 2301 : vdef = gimple_vdef (stmt);
606 : : else
607 : 6 : vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
608 : 2307 : gimple_set_vdef (new_stmt, vdef);
609 : 2307 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
610 : 1296 : SSA_NAME_DEF_STMT (vdef) = new_stmt;
611 : : laststore = new_stmt;
612 : : }
613 : : }
614 : :
615 : : /* Second iterate over the statements forward, assigning virtual
616 : : operands to their uses. */
617 : 123365 : tree reaching_vuse = gimple_vuse (stmt);
618 : 123365 : for (gimple_stmt_iterator i = gsi_start (stmts);
619 : 315512 : !gsi_end_p (i); gsi_next (&i))
620 : : {
621 : 192147 : gimple *new_stmt = gsi_stmt (i);
622 : : /* If the new statement possibly has a VUSE, update it with exact SSA
623 : : name we know will reach this one. */
624 : 192147 : if (gimple_has_mem_ops (new_stmt))
625 : 192145 : gimple_set_vuse (new_stmt, reaching_vuse);
626 : 192147 : gimple_set_modified (new_stmt, true);
627 : 575137 : if (gimple_vdef (new_stmt))
628 : 192147 : reaching_vuse = gimple_vdef (new_stmt);
629 : : }
630 : :
631 : : /* If the new sequence does not do a store release the virtual
632 : : definition of the original statement. */
633 : 123365 : if (reaching_vuse
634 : 206066 : && reaching_vuse == gimple_vuse (stmt))
635 : : {
636 : 81411 : tree vdef = gimple_vdef (stmt);
637 : 81411 : if (vdef
638 : 1501 : && TREE_CODE (vdef) == SSA_NAME)
639 : : {
640 : 1445 : unlink_stmt_vdef (stmt);
641 : 1445 : release_ssa_name (vdef);
642 : : }
643 : : }
644 : :
645 : : /* Finally replace the original statement with the sequence. */
646 : 123365 : gsi_replace_with_seq (si_p, stmts, false);
647 : 123365 : }
648 : :
649 : : /* Helper function for update_gimple_call and
650 : : gimplify_and_update_call_from_tree. A GIMPLE_CALL STMT is being replaced
651 : : with GIMPLE_CALL NEW_STMT. */
652 : :
653 : : static void
654 : 2435 : finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple *new_stmt,
655 : : gimple *stmt)
656 : : {
657 : 2435 : tree lhs = gimple_call_lhs (stmt);
658 : 2435 : gimple_call_set_lhs (new_stmt, lhs);
659 : 2435 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
660 : 802 : SSA_NAME_DEF_STMT (lhs) = new_stmt;
661 : 2435 : gimple_move_vops (new_stmt, stmt);
662 : 2435 : gimple_set_location (new_stmt, gimple_location (stmt));
663 : 2435 : if (gimple_block (new_stmt) == NULL_TREE)
664 : 1 : gimple_set_block (new_stmt, gimple_block (stmt));
665 : 2435 : gsi_replace (si_p, new_stmt, false);
666 : 2435 : }
667 : :
668 : : /* Update a GIMPLE_CALL statement at iterator *SI_P to call to FN
669 : : with number of arguments NARGS, where the arguments in GIMPLE form
670 : : follow NARGS argument. */
671 : :
672 : : bool
673 : 2430 : update_gimple_call (gimple_stmt_iterator *si_p, tree fn, int nargs, ...)
674 : : {
675 : 2430 : va_list ap;
676 : 2430 : gcall *new_stmt, *stmt = as_a <gcall *> (gsi_stmt (*si_p));
677 : :
678 : 2430 : gcc_assert (is_gimple_call (stmt));
679 : 2430 : va_start (ap, nargs);
680 : 2430 : new_stmt = gimple_build_call_valist (fn, nargs, ap);
681 : 2430 : finish_update_gimple_call (si_p, new_stmt, stmt);
682 : 2430 : va_end (ap);
683 : 2430 : return true;
684 : : }
685 : :
686 : : /* Return true if EXPR is a CALL_EXPR suitable for representation
687 : : as a single GIMPLE_CALL statement. If the arguments require
688 : : further gimplification, return false. */
689 : :
690 : : static bool
691 : 55897 : valid_gimple_call_p (tree expr)
692 : : {
693 : 55897 : unsigned i, nargs;
694 : :
695 : 55897 : if (TREE_CODE (expr) != CALL_EXPR)
696 : : return false;
697 : :
698 : 5 : nargs = call_expr_nargs (expr);
699 : 10 : for (i = 0; i < nargs; i++)
700 : : {
701 : 5 : tree arg = CALL_EXPR_ARG (expr, i);
702 : 5 : if (is_gimple_reg_type (TREE_TYPE (arg)))
703 : : {
704 : 5 : if (!is_gimple_val (arg))
705 : : return false;
706 : : }
707 : : else
708 : 0 : if (!is_gimple_lvalue (arg))
709 : : return false;
710 : : }
711 : :
712 : : return true;
713 : : }
714 : :
715 : : /* Convert EXPR into a GIMPLE value suitable for substitution on the
716 : : RHS of an assignment. Insert the necessary statements before
717 : : iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
718 : : is replaced. If the call is expected to produces a result, then it
719 : : is replaced by an assignment of the new RHS to the result variable.
720 : : If the result is to be ignored, then the call is replaced by a
721 : : GIMPLE_NOP. A proper VDEF chain is retained by making the first
722 : : VUSE and the last VDEF of the whole sequence be the same as the replaced
723 : : statement and using new SSA names for stores in between. */
724 : :
725 : : void
726 : 55897 : gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
727 : : {
728 : 55897 : tree lhs;
729 : 55897 : gimple *stmt, *new_stmt;
730 : 55897 : gimple_stmt_iterator i;
731 : 55897 : gimple_seq stmts = NULL;
732 : :
733 : 55897 : stmt = gsi_stmt (*si_p);
734 : :
735 : 55897 : gcc_assert (is_gimple_call (stmt));
736 : :
737 : 55897 : if (valid_gimple_call_p (expr))
738 : : {
739 : : /* The call has simplified to another call. */
740 : 5 : tree fn = CALL_EXPR_FN (expr);
741 : 5 : unsigned i;
742 : 5 : unsigned nargs = call_expr_nargs (expr);
743 : 5 : vec<tree> args = vNULL;
744 : 5 : gcall *new_stmt;
745 : :
746 : 5 : if (nargs > 0)
747 : : {
748 : 5 : args.create (nargs);
749 : 5 : args.safe_grow_cleared (nargs, true);
750 : :
751 : 15 : for (i = 0; i < nargs; i++)
752 : 5 : args[i] = CALL_EXPR_ARG (expr, i);
753 : : }
754 : :
755 : 5 : new_stmt = gimple_build_call_vec (fn, args);
756 : 5 : finish_update_gimple_call (si_p, new_stmt, stmt);
757 : 5 : args.release ();
758 : 5 : return;
759 : : }
760 : :
761 : 55892 : lhs = gimple_call_lhs (stmt);
762 : 55892 : if (lhs == NULL_TREE)
763 : : {
764 : 2400 : push_gimplify_context (gimple_in_ssa_p (cfun));
765 : 1200 : gimplify_and_add (expr, &stmts);
766 : 1200 : pop_gimplify_context (NULL);
767 : :
768 : : /* We can end up with folding a memcpy of an empty class assignment
769 : : which gets optimized away by C++ gimplification. */
770 : 1200 : if (gimple_seq_empty_p (stmts))
771 : : {
772 : 1065 : if (gimple_in_ssa_p (cfun))
773 : : {
774 : 1065 : unlink_stmt_vdef (stmt);
775 : 1065 : release_defs (stmt);
776 : : }
777 : 1065 : gsi_replace (si_p, gimple_build_nop (), false);
778 : 1065 : return;
779 : : }
780 : : }
781 : : else
782 : : {
783 : 54692 : tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
784 : 54692 : new_stmt = gimple_build_assign (lhs, tmp);
785 : 54692 : i = gsi_last (stmts);
786 : 54692 : gsi_insert_after_without_update (&i, new_stmt,
787 : : GSI_CONTINUE_LINKING);
788 : : }
789 : :
790 : 54827 : gsi_replace_with_seq_vops (si_p, stmts);
791 : : }
792 : :
793 : : /* Print a message in the dump file recording transformation of FROM to TO. */
794 : :
795 : : static void
796 : 39766 : dump_transformation (gcall *from, gcall *to)
797 : : {
798 : 39766 : if (dump_enabled_p ())
799 : 11 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, from, "simplified %T to %T\n",
800 : : gimple_call_fn (from), gimple_call_fn (to));
801 : 39766 : }
802 : :
803 : : /* Replace the call at *GSI with the gimple value VAL. */
804 : :
805 : : void
806 : 81083 : replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
807 : : {
808 : 81083 : gimple *stmt = gsi_stmt (*gsi);
809 : 81083 : tree lhs = gimple_call_lhs (stmt);
810 : 81083 : gimple *repl;
811 : 81083 : if (lhs)
812 : : {
813 : 76133 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
814 : 3895 : val = fold_convert (TREE_TYPE (lhs), val);
815 : 76133 : repl = gimple_build_assign (lhs, val);
816 : : }
817 : : else
818 : 4950 : repl = gimple_build_nop ();
819 : 81083 : tree vdef = gimple_vdef (stmt);
820 : 81083 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
821 : : {
822 : 5495 : unlink_stmt_vdef (stmt);
823 : 5495 : release_ssa_name (vdef);
824 : : }
825 : 81083 : gsi_replace (gsi, repl, false);
826 : 81083 : }
827 : :
828 : : /* Replace the call at *GSI with the new call REPL and fold that
829 : : again. */
830 : :
831 : : static void
832 : 39766 : replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
833 : : {
834 : 39766 : gimple *stmt = gsi_stmt (*gsi);
835 : 39766 : dump_transformation (as_a <gcall *> (stmt), as_a <gcall *> (repl));
836 : 39766 : gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
837 : 39766 : gimple_set_location (repl, gimple_location (stmt));
838 : 39766 : gimple_move_vops (repl, stmt);
839 : 39766 : gsi_replace (gsi, repl, false);
840 : 39766 : fold_stmt (gsi);
841 : 39766 : }
842 : :
843 : : /* Return true if VAR is a VAR_DECL or a component thereof. */
844 : :
845 : : static bool
846 : 384449 : var_decl_component_p (tree var)
847 : : {
848 : 384449 : tree inner = var;
849 : 554194 : while (handled_component_p (inner))
850 : 169745 : inner = TREE_OPERAND (inner, 0);
851 : 384449 : return (DECL_P (inner)
852 : 384449 : || (TREE_CODE (inner) == MEM_REF
853 : 44402 : && TREE_CODE (TREE_OPERAND (inner, 0)) == ADDR_EXPR));
854 : : }
855 : :
856 : : /* Return TRUE if the SIZE argument, representing the size of an
857 : : object, is in a range of values of which exactly zero is valid. */
858 : :
859 : : static bool
860 : 897363 : size_must_be_zero_p (tree size)
861 : : {
862 : 897363 : if (integer_zerop (size))
863 : : return true;
864 : :
865 : 894761 : if (TREE_CODE (size) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (size)))
866 : : return false;
867 : :
868 : 526616 : tree type = TREE_TYPE (size);
869 : 526616 : int prec = TYPE_PRECISION (type);
870 : :
871 : : /* Compute the value of SSIZE_MAX, the largest positive value that
872 : : can be stored in ssize_t, the signed counterpart of size_t. */
873 : 526616 : wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
874 : 526616 : wide_int zero = wi::zero (TYPE_PRECISION (type));
875 : 526616 : int_range_max valid_range (type, zero, ssize_max);
876 : 526616 : int_range_max vr;
877 : 1053232 : get_range_query (cfun)->range_of_expr (vr, size);
878 : :
879 : 526616 : if (vr.undefined_p ())
880 : 93 : vr.set_varying (TREE_TYPE (size));
881 : 526616 : vr.intersect (valid_range);
882 : 526616 : return vr.zero_p ();
883 : 526616 : }
884 : :
885 : : /* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
886 : : diagnose (otherwise undefined) overlapping copies without preventing
887 : : folding. When folded, GCC guarantees that overlapping memcpy has
888 : : the same semantics as memmove. Call to the library memcpy need not
889 : : provide the same guarantee. Return false if no simplification can
890 : : be made. */
891 : :
892 : : static bool
893 : 897363 : gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
894 : : tree dest, tree src, enum built_in_function code)
895 : : {
896 : 897363 : gimple *stmt = gsi_stmt (*gsi);
897 : 897363 : tree lhs = gimple_call_lhs (stmt);
898 : 897363 : tree len = gimple_call_arg (stmt, 2);
899 : 897363 : location_t loc = gimple_location (stmt);
900 : :
901 : : /* If the LEN parameter is a constant zero or in range where
902 : : the only valid value is zero, return DEST. */
903 : 897363 : if (size_must_be_zero_p (len))
904 : : {
905 : 2634 : gimple *repl;
906 : 2634 : if (gimple_call_lhs (stmt))
907 : 58 : repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
908 : : else
909 : 2576 : repl = gimple_build_nop ();
910 : 2634 : tree vdef = gimple_vdef (stmt);
911 : 2634 : if (vdef && TREE_CODE (vdef) == SSA_NAME)
912 : : {
913 : 547 : unlink_stmt_vdef (stmt);
914 : 547 : release_ssa_name (vdef);
915 : : }
916 : 2634 : gsi_replace (gsi, repl, false);
917 : 2634 : return true;
918 : : }
919 : :
920 : : /* If SRC and DEST are the same (and not volatile), return
921 : : DEST{,+LEN,+LEN-1}. */
922 : 894729 : if (operand_equal_p (src, dest, 0))
923 : : {
924 : : /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
925 : : It's safe and may even be emitted by GCC itself (see bug
926 : : 32667). */
927 : 69 : unlink_stmt_vdef (stmt);
928 : 138 : if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
929 : 29 : release_ssa_name (gimple_vdef (stmt));
930 : 69 : if (!lhs)
931 : : {
932 : 48 : gsi_replace (gsi, gimple_build_nop (), false);
933 : 48 : return true;
934 : : }
935 : 21 : goto done;
936 : : }
937 : 1789320 : else if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
938 : : return false;
939 : : else
940 : : {
941 : : /* We cannot (easily) change the type of the copy if it is a storage
942 : : order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that can
943 : : modify the storage order of objects (see storage_order_barrier_p). */
944 : 894660 : tree srctype
945 : 907661 : = POINTER_TYPE_P (TREE_TYPE (src))
946 : 907661 : ? TREE_TYPE (TREE_TYPE (src)) : NULL_TREE;
947 : 894660 : tree desttype
948 : 914080 : = POINTER_TYPE_P (TREE_TYPE (dest))
949 : 914080 : ? TREE_TYPE (TREE_TYPE (dest)) : NULL_TREE;
950 : 894660 : tree destvar, srcvar, srcoff;
951 : 894660 : unsigned int src_align, dest_align;
952 : 894660 : unsigned HOST_WIDE_INT tmp_len;
953 : 894660 : const char *tmp_str;
954 : :
955 : : /* Build accesses at offset zero with a ref-all character type. */
956 : 894660 : tree off0
957 : 894660 : = build_int_cst (build_pointer_type_for_mode (char_type_node,
958 : : ptr_mode, true), 0);
959 : :
960 : : /* If we can perform the copy efficiently with first doing all loads
961 : : and then all stores inline it that way. Currently efficiently
962 : : means that we can load all the memory into a single integer
963 : : register which is what MOVE_MAX gives us. */
964 : 894660 : src_align = get_pointer_alignment (src);
965 : 894660 : dest_align = get_pointer_alignment (dest);
966 : 894660 : if (tree_fits_uhwi_p (len)
967 : 359353 : && compare_tree_int (len, MOVE_MAX) <= 0
968 : : /* FIXME: Don't transform copies from strings with known length.
969 : : Until GCC 9 this prevented a case in gcc.dg/strlenopt-8.c
970 : : from being handled, and the case was XFAILed for that reason.
971 : : Now that it is handled and the XFAIL removed, as soon as other
972 : : strlenopt tests that rely on it for passing are adjusted, this
973 : : hack can be removed. */
974 : 271776 : && !c_strlen (src, 1)
975 : 171740 : && !((tmp_str = getbyterep (src, &tmp_len)) != NULL
976 : 72553 : && memchr (tmp_str, 0, tmp_len) == NULL)
977 : 110082 : && !(srctype
978 : 110082 : && AGGREGATE_TYPE_P (srctype)
979 : 53143 : && TYPE_REVERSE_STORAGE_ORDER (srctype))
980 : 1004609 : && !(desttype
981 : 109949 : && AGGREGATE_TYPE_P (desttype)
982 : 61360 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
983 : : {
984 : 109916 : unsigned ilen = tree_to_uhwi (len);
985 : 109916 : if (pow2p_hwi (ilen))
986 : : {
987 : : /* Detect out-of-bounds accesses without issuing warnings.
988 : : Avoid folding out-of-bounds copies but to avoid false
989 : : positives for unreachable code defer warning until after
990 : : DCE has worked its magic.
991 : : -Wrestrict is still diagnosed. */
992 : 22153 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
993 : : dest, src, len, len,
994 : 22153 : false, false))
995 : 953 : if (warning != OPT_Wrestrict)
996 : 18273 : return false;
997 : :
998 : 21258 : scalar_int_mode imode;
999 : 21258 : machine_mode mode;
1000 : 21258 : if (int_mode_for_size (ilen * BITS_PER_UNIT, 0).exists (&imode)
1001 : 21258 : && bitwise_mode_for_size (ilen
1002 : 21258 : * BITS_PER_UNIT).exists (&mode)
1003 : 42516 : && known_eq (GET_MODE_BITSIZE (mode), ilen * BITS_PER_UNIT)
1004 : : /* If the destination pointer is not aligned we must be able
1005 : : to emit an unaligned store. */
1006 : 21258 : && (dest_align >= GET_MODE_ALIGNMENT (mode)
1007 : 11162 : || !targetm.slow_unaligned_access (mode, dest_align)
1008 : 0 : || (optab_handler (movmisalign_optab, mode)
1009 : : != CODE_FOR_nothing)))
1010 : : {
1011 : 21258 : tree type = bitwise_type_for_mode (mode);
1012 : 21258 : tree srctype = type;
1013 : 21258 : tree desttype = type;
1014 : 21258 : if (src_align < GET_MODE_ALIGNMENT (mode))
1015 : 11006 : srctype = build_aligned_type (type, src_align);
1016 : 21258 : tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
1017 : 21258 : tree tem = fold_const_aggregate_ref (srcmem);
1018 : 21258 : if (tem)
1019 : : srcmem = tem;
1020 : 20407 : else if (src_align < GET_MODE_ALIGNMENT (mode)
1021 : 10754 : && targetm.slow_unaligned_access (mode, src_align)
1022 : 20407 : && (optab_handler (movmisalign_optab, mode)
1023 : : == CODE_FOR_nothing))
1024 : : srcmem = NULL_TREE;
1025 : 20407 : if (srcmem)
1026 : : {
1027 : 21258 : gimple *new_stmt;
1028 : 21258 : if (is_gimple_reg_type (TREE_TYPE (srcmem)))
1029 : : {
1030 : 21258 : new_stmt = gimple_build_assign (NULL_TREE, srcmem);
1031 : 21258 : srcmem
1032 : 21258 : = make_ssa_name (TREE_TYPE (srcmem), new_stmt);
1033 : 21258 : gimple_assign_set_lhs (new_stmt, srcmem);
1034 : 42516 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1035 : 21258 : gimple_set_location (new_stmt, loc);
1036 : 21258 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1037 : : }
1038 : 21258 : if (dest_align < GET_MODE_ALIGNMENT (mode))
1039 : 11162 : desttype = build_aligned_type (type, dest_align);
1040 : 21258 : new_stmt
1041 : 21258 : = gimple_build_assign (fold_build2 (MEM_REF, desttype,
1042 : : dest, off0),
1043 : : srcmem);
1044 : 21258 : gimple_move_vops (new_stmt, stmt);
1045 : 21258 : if (!lhs)
1046 : : {
1047 : 17378 : gsi_replace (gsi, new_stmt, false);
1048 : 17378 : return true;
1049 : : }
1050 : 3880 : gimple_set_location (new_stmt, loc);
1051 : 3880 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1052 : 3880 : goto done;
1053 : : }
1054 : : }
1055 : : }
1056 : : }
1057 : :
1058 : 872507 : if (code == BUILT_IN_MEMMOVE)
1059 : : {
1060 : : /* Both DEST and SRC must be pointer types.
1061 : : ??? This is what old code did. Is the testing for pointer types
1062 : : really mandatory?
1063 : :
1064 : : If either SRC is readonly or length is 1, we can use memcpy. */
1065 : 185042 : if (!dest_align || !src_align)
1066 : : return false;
1067 : 185042 : if (readonly_data_expr (src)
1068 : 185042 : || (tree_fits_uhwi_p (len)
1069 : 31640 : && (MIN (src_align, dest_align) / BITS_PER_UNIT
1070 : 31640 : >= tree_to_uhwi (len))))
1071 : : {
1072 : 840818 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1073 : 18388 : if (!fn)
1074 : : return false;
1075 : 18388 : gimple_call_set_fndecl (stmt, fn);
1076 : 18388 : gimple_call_set_arg (stmt, 0, dest);
1077 : 18388 : gimple_call_set_arg (stmt, 1, src);
1078 : 18388 : fold_stmt (gsi);
1079 : 18388 : return true;
1080 : : }
1081 : :
1082 : : /* If *src and *dest can't overlap, optimize into memcpy as well. */
1083 : 166654 : if (TREE_CODE (src) == ADDR_EXPR
1084 : 5324 : && TREE_CODE (dest) == ADDR_EXPR)
1085 : : {
1086 : 1687 : tree src_base, dest_base, fn;
1087 : 1687 : poly_int64 src_offset = 0, dest_offset = 0;
1088 : 1687 : poly_uint64 maxsize;
1089 : :
1090 : 1687 : srcvar = TREE_OPERAND (src, 0);
1091 : 1687 : src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
1092 : 1687 : if (src_base == NULL)
1093 : 0 : src_base = srcvar;
1094 : 1687 : destvar = TREE_OPERAND (dest, 0);
1095 : 1687 : dest_base = get_addr_base_and_unit_offset (destvar,
1096 : : &dest_offset);
1097 : 1687 : if (dest_base == NULL)
1098 : 0 : dest_base = destvar;
1099 : 1687 : if (!poly_int_tree_p (len, &maxsize))
1100 : 216 : maxsize = -1;
1101 : 1687 : if (SSA_VAR_P (src_base)
1102 : 1677 : && SSA_VAR_P (dest_base))
1103 : : {
1104 : 1677 : if (operand_equal_p (src_base, dest_base, 0)
1105 : 1677 : && ranges_maybe_overlap_p (src_offset, maxsize,
1106 : : dest_offset, maxsize))
1107 : : return false;
1108 : : }
1109 : 10 : else if (TREE_CODE (src_base) == MEM_REF
1110 : 0 : && TREE_CODE (dest_base) == MEM_REF)
1111 : : {
1112 : 0 : if (! operand_equal_p (TREE_OPERAND (src_base, 0),
1113 : 0 : TREE_OPERAND (dest_base, 0), 0))
1114 : 0 : return false;
1115 : 0 : poly_offset_int full_src_offset
1116 : 0 : = mem_ref_offset (src_base) + src_offset;
1117 : 0 : poly_offset_int full_dest_offset
1118 : 0 : = mem_ref_offset (dest_base) + dest_offset;
1119 : 0 : if (ranges_maybe_overlap_p (full_src_offset, maxsize,
1120 : : full_dest_offset, maxsize))
1121 : : return false;
1122 : 0 : }
1123 : : else
1124 : : return false;
1125 : :
1126 : 1687 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1127 : 1312 : if (!fn)
1128 : : return false;
1129 : 1312 : gimple_call_set_fndecl (stmt, fn);
1130 : 1312 : gimple_call_set_arg (stmt, 0, dest);
1131 : 1312 : gimple_call_set_arg (stmt, 1, src);
1132 : 1312 : fold_stmt (gsi);
1133 : 1312 : return true;
1134 : : }
1135 : :
1136 : : /* If the destination and source do not alias optimize into
1137 : : memcpy as well. */
1138 : 164967 : if ((is_gimple_min_invariant (dest)
1139 : 161263 : || TREE_CODE (dest) == SSA_NAME)
1140 : 309249 : && (is_gimple_min_invariant (src)
1141 : 144370 : || TREE_CODE (src) == SSA_NAME))
1142 : : {
1143 : 147563 : ao_ref destr, srcr;
1144 : 147563 : ao_ref_init_from_ptr_and_size (&destr, dest, len);
1145 : 147563 : ao_ref_init_from_ptr_and_size (&srcr, src, len);
1146 : 147563 : if (!refs_may_alias_p_1 (&destr, &srcr, false))
1147 : : {
1148 : 9979 : tree fn;
1149 : 9979 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1150 : 9979 : if (!fn)
1151 : 9979 : return false;
1152 : 9979 : gimple_call_set_fndecl (stmt, fn);
1153 : 9979 : gimple_call_set_arg (stmt, 0, dest);
1154 : 9979 : gimple_call_set_arg (stmt, 1, src);
1155 : 9979 : fold_stmt (gsi);
1156 : 9979 : return true;
1157 : : }
1158 : : }
1159 : :
1160 : 154988 : return false;
1161 : : }
1162 : :
1163 : 687465 : if (!tree_fits_shwi_p (len))
1164 : : return false;
1165 : 290135 : if (!srctype
1166 : 290135 : || (AGGREGATE_TYPE_P (srctype)
1167 : 185262 : && TYPE_REVERSE_STORAGE_ORDER (srctype)))
1168 : : return false;
1169 : 290002 : if (!desttype
1170 : 290002 : || (AGGREGATE_TYPE_P (desttype)
1171 : 178009 : && TYPE_REVERSE_STORAGE_ORDER (desttype)))
1172 : : return false;
1173 : : /* In the following try to find a type that is most natural to be
1174 : : used for the memcpy source and destination and that allows
1175 : : the most optimization when memcpy is turned into a plain assignment
1176 : : using that type. In theory we could always use a char[len] type
1177 : : but that only gains us that the destination and source possibly
1178 : : no longer will have their address taken. */
1179 : 289969 : if (TREE_CODE (srctype) == ARRAY_TYPE
1180 : 289969 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
1181 : 114537 : srctype = TREE_TYPE (srctype);
1182 : 289969 : if (TREE_CODE (desttype) == ARRAY_TYPE
1183 : 289969 : && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
1184 : 102021 : desttype = TREE_TYPE (desttype);
1185 : 289969 : if (TREE_ADDRESSABLE (srctype)
1186 : 289944 : || TREE_ADDRESSABLE (desttype))
1187 : : return false;
1188 : :
1189 : : /* Make sure we are not copying using a floating-point mode or
1190 : : a type whose size possibly does not match its precision. */
1191 : 579209 : if (FLOAT_MODE_P (TYPE_MODE (desttype))
1192 : 289052 : || TREE_CODE (desttype) == BOOLEAN_TYPE
1193 : 578941 : || TREE_CODE (desttype) == ENUMERAL_TYPE)
1194 : 881 : desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
1195 : 579387 : if (FLOAT_MODE_P (TYPE_MODE (srctype))
1196 : 289364 : || TREE_CODE (srctype) == BOOLEAN_TYPE
1197 : 579257 : || TREE_CODE (srctype) == ENUMERAL_TYPE)
1198 : 565 : srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
1199 : 289911 : if (!srctype)
1200 : 120 : srctype = desttype;
1201 : 289911 : if (!desttype)
1202 : 0 : desttype = srctype;
1203 : 289911 : if (!srctype)
1204 : : return false;
1205 : :
1206 : 289911 : src_align = get_pointer_alignment (src);
1207 : 289911 : dest_align = get_pointer_alignment (dest);
1208 : :
1209 : : /* Choose between src and destination type for the access based
1210 : : on alignment, whether the access constitutes a register access
1211 : : and whether it may actually expose a declaration for SSA rewrite
1212 : : or SRA decomposition. Also try to expose a string constant, we
1213 : : might be able to concatenate several of them later into a single
1214 : : string store. */
1215 : 289911 : destvar = NULL_TREE;
1216 : 289911 : srcvar = NULL_TREE;
1217 : 289911 : if (TREE_CODE (dest) == ADDR_EXPR
1218 : 110803 : && var_decl_component_p (TREE_OPERAND (dest, 0))
1219 : 110799 : && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len)
1220 : 21811 : && dest_align >= TYPE_ALIGN (desttype)
1221 : 311722 : && (is_gimple_reg_type (desttype)
1222 : 21400 : || src_align >= TYPE_ALIGN (desttype)))
1223 : 17256 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1224 : 272655 : else if (TREE_CODE (src) == ADDR_EXPR
1225 : 213322 : && var_decl_component_p (TREE_OPERAND (src, 0))
1226 : 39280 : && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len)
1227 : 8328 : && src_align >= TYPE_ALIGN (srctype)
1228 : 280967 : && (is_gimple_reg_type (srctype)
1229 : 8149 : || dest_align >= TYPE_ALIGN (srctype)))
1230 : 2871 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1231 : : /* FIXME: Don't transform copies from strings with known original length.
1232 : : As soon as strlenopt tests that rely on it for passing are adjusted,
1233 : : this hack can be removed. */
1234 : 269784 : else if (gimple_call_alloca_for_var_p (stmt)
1235 : 115 : && (srcvar = string_constant (src, &srcoff, NULL, NULL))
1236 : 3 : && integer_zerop (srcoff)
1237 : 3 : && tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (srcvar)), len)
1238 : 269787 : && dest_align >= TYPE_ALIGN (TREE_TYPE (srcvar)))
1239 : 3 : srctype = TREE_TYPE (srcvar);
1240 : : else
1241 : 269781 : return false;
1242 : :
1243 : : /* Now that we chose an access type express the other side in
1244 : : terms of it if the target allows that with respect to alignment
1245 : : constraints. */
1246 : 20130 : if (srcvar == NULL_TREE)
1247 : : {
1248 : 17256 : if (src_align >= TYPE_ALIGN (desttype))
1249 : 17242 : srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1250 : : else
1251 : : {
1252 : 14 : enum machine_mode mode = TYPE_MODE (desttype);
1253 : 14 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1254 : 14 : || (targetm.slow_unaligned_access (mode, src_align)
1255 : 14 : && (optab_handler (movmisalign_optab, mode)
1256 : : == CODE_FOR_nothing)))
1257 : : return false;
1258 : 14 : srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1259 : : src_align);
1260 : 14 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1261 : : }
1262 : : }
1263 : 2874 : else if (destvar == NULL_TREE)
1264 : : {
1265 : 2874 : if (dest_align >= TYPE_ALIGN (srctype))
1266 : 2874 : destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1267 : : else
1268 : : {
1269 : 0 : enum machine_mode mode = TYPE_MODE (srctype);
1270 : 0 : if ((mode == BLKmode && STRICT_ALIGNMENT)
1271 : 0 : || (targetm.slow_unaligned_access (mode, dest_align)
1272 : 0 : && (optab_handler (movmisalign_optab, mode)
1273 : : == CODE_FOR_nothing)))
1274 : : return false;
1275 : 0 : desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1276 : : dest_align);
1277 : 0 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1278 : : }
1279 : : }
1280 : :
1281 : : /* Same as above, detect out-of-bounds accesses without issuing
1282 : : warnings. Avoid folding out-of-bounds copies but to avoid
1283 : : false positives for unreachable code defer warning until
1284 : : after DCE has worked its magic.
1285 : : -Wrestrict is still diagnosed. */
1286 : 20130 : if (int warning = check_bounds_or_overlap (as_a <gcall *>(stmt),
1287 : : dest, src, len, len,
1288 : 20130 : false, false))
1289 : 115 : if (warning != OPT_Wrestrict)
1290 : : return false;
1291 : :
1292 : 20023 : gimple *new_stmt;
1293 : 20023 : if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1294 : : {
1295 : 534 : tree tem = fold_const_aggregate_ref (srcvar);
1296 : 534 : if (tem)
1297 : 517 : srcvar = tem;
1298 : 534 : if (! is_gimple_min_invariant (srcvar))
1299 : : {
1300 : 17 : new_stmt = gimple_build_assign (NULL_TREE, srcvar);
1301 : 17 : srcvar = make_ssa_name (TREE_TYPE (srcvar), new_stmt);
1302 : 17 : gimple_assign_set_lhs (new_stmt, srcvar);
1303 : 34 : gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1304 : 17 : gimple_set_location (new_stmt, loc);
1305 : 17 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1306 : : }
1307 : 534 : new_stmt = gimple_build_assign (destvar, srcvar);
1308 : 534 : goto set_vop_and_replace;
1309 : : }
1310 : :
1311 : : /* We get an aggregate copy. If the source is a STRING_CST, then
1312 : : directly use its type to perform the copy. */
1313 : 19489 : if (TREE_CODE (srcvar) == STRING_CST)
1314 : : desttype = srctype;
1315 : :
1316 : : /* Or else, use an unsigned char[] type to perform the copy in order
1317 : : to preserve padding and to avoid any issues with TREE_ADDRESSABLE
1318 : : types or float modes behavior on copying. */
1319 : : else
1320 : : {
1321 : 38972 : desttype = build_array_type_nelts (unsigned_char_type_node,
1322 : 19486 : tree_to_uhwi (len));
1323 : 19486 : srctype = desttype;
1324 : 19486 : if (src_align > TYPE_ALIGN (srctype))
1325 : 12047 : srctype = build_aligned_type (srctype, src_align);
1326 : 19486 : srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1327 : : }
1328 : :
1329 : 19489 : if (dest_align > TYPE_ALIGN (desttype))
1330 : 12697 : desttype = build_aligned_type (desttype, dest_align);
1331 : 19489 : destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1332 : 19489 : new_stmt = gimple_build_assign (destvar, srcvar);
1333 : :
1334 : 20023 : set_vop_and_replace:
1335 : 20023 : gimple_move_vops (new_stmt, stmt);
1336 : 20023 : if (!lhs)
1337 : : {
1338 : 19548 : gsi_replace (gsi, new_stmt, false);
1339 : 19548 : return true;
1340 : : }
1341 : 475 : gimple_set_location (new_stmt, loc);
1342 : 475 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1343 : : }
1344 : :
1345 : 4376 : done:
1346 : 4376 : gimple_seq stmts = NULL;
1347 : 4376 : if (code == BUILT_IN_MEMCPY || code == BUILT_IN_MEMMOVE)
1348 : 4376 : len = NULL_TREE;
1349 : 197 : else if (code == BUILT_IN_MEMPCPY)
1350 : : {
1351 : 197 : len = gimple_convert_to_ptrofftype (&stmts, loc, len);
1352 : 197 : dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
1353 : 197 : TREE_TYPE (dest), dest, len);
1354 : : }
1355 : : else
1356 : 0 : gcc_unreachable ();
1357 : :
1358 : 4376 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
1359 : 4376 : gimple *repl = gimple_build_assign (lhs, dest);
1360 : 4376 : gsi_replace (gsi, repl, false);
1361 : 4376 : return true;
1362 : : }
1363 : :
1364 : : /* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1365 : : to built-in memcmp (a, b, len). */
1366 : :
1367 : : static bool
1368 : 148 : gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
1369 : : {
1370 : 148 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
1371 : :
1372 : 148 : if (!fn)
1373 : : return false;
1374 : :
1375 : : /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1376 : :
1377 : 148 : gimple *stmt = gsi_stmt (*gsi);
1378 : 296 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
1379 : : return false;
1380 : 148 : tree a = gimple_call_arg (stmt, 0);
1381 : 148 : tree b = gimple_call_arg (stmt, 1);
1382 : 148 : tree len = gimple_call_arg (stmt, 2);
1383 : :
1384 : 148 : gimple *repl = gimple_build_call (fn, 3, a, b, len);
1385 : 148 : replace_call_with_call_and_fold (gsi, repl);
1386 : :
1387 : 148 : return true;
1388 : : }
1389 : :
1390 : : /* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1391 : : to built-in memmove (dest, src, len). */
1392 : :
1393 : : static bool
1394 : 367 : gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
1395 : : {
1396 : 367 : tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
1397 : :
1398 : 367 : if (!fn)
1399 : : return false;
1400 : :
1401 : : /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1402 : : it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1403 : : len) into memmove (dest, src, len). */
1404 : :
1405 : 367 : gimple *stmt = gsi_stmt (*gsi);
1406 : 734 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1407 : : return false;
1408 : 367 : tree src = gimple_call_arg (stmt, 0);
1409 : 367 : tree dest = gimple_call_arg (stmt, 1);
1410 : 367 : tree len = gimple_call_arg (stmt, 2);
1411 : :
1412 : 367 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1413 : 367 : gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
1414 : 367 : replace_call_with_call_and_fold (gsi, repl);
1415 : :
1416 : 367 : return true;
1417 : : }
1418 : :
1419 : : /* Transform a call to built-in bzero (dest, len) at *GSI into one
1420 : : to built-in memset (dest, 0, len). */
1421 : :
1422 : : static bool
1423 : 250 : gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
1424 : : {
1425 : 250 : tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
1426 : :
1427 : 250 : if (!fn)
1428 : : return false;
1429 : :
1430 : : /* Transform bzero (dest, len) into memset (dest, 0, len). */
1431 : :
1432 : 250 : gimple *stmt = gsi_stmt (*gsi);
1433 : 500 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1434 : : return false;
1435 : 250 : tree dest = gimple_call_arg (stmt, 0);
1436 : 250 : tree len = gimple_call_arg (stmt, 1);
1437 : :
1438 : 250 : gimple_seq seq = NULL;
1439 : 250 : gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
1440 : 250 : gimple_seq_add_stmt_without_update (&seq, repl);
1441 : 250 : gsi_replace_with_seq_vops (gsi, seq);
1442 : 250 : fold_stmt (gsi);
1443 : :
1444 : 250 : return true;
1445 : : }
1446 : :
1447 : : /* Fold function call to builtin memset or bzero at *GSI setting the
1448 : : memory of size LEN to VAL. Return whether a simplification was made. */
1449 : :
1450 : : static bool
1451 : 310359 : gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1452 : : {
1453 : 310359 : gimple *stmt = gsi_stmt (*gsi);
1454 : 310359 : tree etype;
1455 : 310359 : unsigned HOST_WIDE_INT length, cval;
1456 : :
1457 : : /* If the LEN parameter is zero, return DEST. */
1458 : 310359 : if (integer_zerop (len))
1459 : : {
1460 : 811 : replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1461 : 811 : return true;
1462 : : }
1463 : :
1464 : 926889 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
1465 : : return false;
1466 : :
1467 : 309548 : if (! tree_fits_uhwi_p (len))
1468 : : return false;
1469 : :
1470 : 199051 : if (TREE_CODE (c) != INTEGER_CST)
1471 : : return false;
1472 : :
1473 : 193334 : tree dest = gimple_call_arg (stmt, 0);
1474 : 193334 : tree var = dest;
1475 : 193334 : if (TREE_CODE (var) != ADDR_EXPR)
1476 : : return false;
1477 : :
1478 : 155691 : var = TREE_OPERAND (var, 0);
1479 : 155691 : if (TREE_THIS_VOLATILE (var))
1480 : : return false;
1481 : :
1482 : 155648 : etype = TREE_TYPE (var);
1483 : 155648 : if (TREE_CODE (etype) == ARRAY_TYPE)
1484 : 81489 : etype = TREE_TYPE (etype);
1485 : :
1486 : 155648 : if ((!INTEGRAL_TYPE_P (etype)
1487 : 95824 : && !POINTER_TYPE_P (etype))
1488 : 60361 : || TREE_CODE (etype) == BITINT_TYPE)
1489 : : return false;
1490 : :
1491 : 60324 : if (! var_decl_component_p (var))
1492 : : return false;
1493 : :
1494 : 60324 : length = tree_to_uhwi (len);
1495 : 60324 : if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
1496 : 1755 : || (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (etype))
1497 : 3510 : != GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (etype)))
1498 : 62079 : || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1499 : 58569 : return false;
1500 : :
1501 : 1755 : if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1502 : : return false;
1503 : :
1504 : 1755 : if (!type_has_mode_precision_p (etype))
1505 : 7 : etype = lang_hooks.types.type_for_mode (SCALAR_INT_TYPE_MODE (etype),
1506 : 7 : TYPE_UNSIGNED (etype));
1507 : :
1508 : 1755 : if (integer_zerop (c))
1509 : : cval = 0;
1510 : : else
1511 : : {
1512 : 339 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1513 : : return NULL_TREE;
1514 : :
1515 : 339 : cval = TREE_INT_CST_LOW (c);
1516 : 339 : cval &= 0xff;
1517 : 339 : cval |= cval << 8;
1518 : 339 : cval |= cval << 16;
1519 : 339 : cval |= (cval << 31) << 1;
1520 : : }
1521 : :
1522 : 1755 : var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
1523 : 1755 : gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
1524 : 1755 : gimple_move_vops (store, stmt);
1525 : 1755 : gimple_set_location (store, gimple_location (stmt));
1526 : 1755 : gsi_insert_before (gsi, store, GSI_SAME_STMT);
1527 : 1755 : if (gimple_call_lhs (stmt))
1528 : : {
1529 : 2 : gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
1530 : 2 : gsi_replace (gsi, asgn, false);
1531 : : }
1532 : : else
1533 : : {
1534 : 1753 : gimple_stmt_iterator gsi2 = *gsi;
1535 : 1753 : gsi_prev (gsi);
1536 : 1753 : gsi_remove (&gsi2, true);
1537 : : }
1538 : :
1539 : : return true;
1540 : : }
1541 : :
1542 : : /* Helper of get_range_strlen for ARG that is not an SSA_NAME. */
1543 : :
1544 : : static bool
1545 : 455777 : get_range_strlen_tree (tree arg, bitmap visited, strlen_range_kind rkind,
1546 : : c_strlen_data *pdata, unsigned eltsize)
1547 : : {
1548 : 455777 : gcc_assert (TREE_CODE (arg) != SSA_NAME);
1549 : :
1550 : : /* The length computed by this invocation of the function. */
1551 : 455777 : tree val = NULL_TREE;
1552 : :
1553 : : /* True if VAL is an optimistic (tight) bound determined from
1554 : : the size of the character array in which the string may be
1555 : : stored. In that case, the computed VAL is used to set
1556 : : PDATA->MAXBOUND. */
1557 : 455777 : bool tight_bound = false;
1558 : :
1559 : : /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1560 : 455777 : if (TREE_CODE (arg) == ADDR_EXPR
1561 : 455777 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
1562 : : {
1563 : 28173 : tree op = TREE_OPERAND (arg, 0);
1564 : 28173 : if (integer_zerop (TREE_OPERAND (op, 1)))
1565 : : {
1566 : 12045 : tree aop0 = TREE_OPERAND (op, 0);
1567 : 12045 : if (TREE_CODE (aop0) == INDIRECT_REF
1568 : 12045 : && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1569 : 0 : return get_range_strlen (TREE_OPERAND (aop0, 0), visited, rkind,
1570 : 0 : pdata, eltsize);
1571 : : }
1572 : 16128 : else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF
1573 : 16128 : && rkind == SRK_LENRANGE)
1574 : : {
1575 : : /* Fail if an array is the last member of a struct object
1576 : : since it could be treated as a (fake) flexible array
1577 : : member. */
1578 : 4794 : tree idx = TREE_OPERAND (op, 1);
1579 : :
1580 : 4794 : arg = TREE_OPERAND (op, 0);
1581 : 4794 : tree optype = TREE_TYPE (arg);
1582 : 4794 : if (tree dom = TYPE_DOMAIN (optype))
1583 : 4794 : if (tree bound = TYPE_MAX_VALUE (dom))
1584 : 4794 : if (TREE_CODE (bound) == INTEGER_CST
1585 : 4794 : && TREE_CODE (idx) == INTEGER_CST
1586 : 8018 : && tree_int_cst_lt (bound, idx))
1587 : : return false;
1588 : : }
1589 : : }
1590 : :
1591 : 455569 : if (rkind == SRK_INT_VALUE)
1592 : : {
1593 : : /* We are computing the maximum value (not string length). */
1594 : 25829 : val = arg;
1595 : 25829 : if (TREE_CODE (val) != INTEGER_CST
1596 : 25829 : || tree_int_cst_sgn (val) < 0)
1597 : 2572 : return false;
1598 : : }
1599 : : else
1600 : : {
1601 : 429740 : c_strlen_data lendata = { };
1602 : 429740 : val = c_strlen (arg, 1, &lendata, eltsize);
1603 : :
1604 : 429740 : if (!val && lendata.decl)
1605 : : {
1606 : : /* ARG refers to an unterminated const character array.
1607 : : DATA.DECL with size DATA.LEN. */
1608 : 4211 : val = lendata.minlen;
1609 : 4211 : pdata->decl = lendata.decl;
1610 : : }
1611 : : }
1612 : :
1613 : : /* Set if VAL represents the maximum length based on array size (set
1614 : : when exact length cannot be determined). */
1615 : 452997 : bool maxbound = false;
1616 : :
1617 : 452997 : if (!val && rkind == SRK_LENRANGE)
1618 : : {
1619 : 226886 : if (TREE_CODE (arg) == ADDR_EXPR)
1620 : 80667 : return get_range_strlen (TREE_OPERAND (arg, 0), visited, rkind,
1621 : 80667 : pdata, eltsize);
1622 : :
1623 : 146219 : if (TREE_CODE (arg) == ARRAY_REF)
1624 : : {
1625 : 18819 : tree optype = TREE_TYPE (TREE_OPERAND (arg, 0));
1626 : :
1627 : : /* Determine the "innermost" array type. */
1628 : 18819 : while (TREE_CODE (optype) == ARRAY_TYPE
1629 : 25657 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1630 : 6838 : optype = TREE_TYPE (optype);
1631 : :
1632 : : /* Avoid arrays of pointers. */
1633 : 18819 : tree eltype = TREE_TYPE (optype);
1634 : 18819 : if (TREE_CODE (optype) != ARRAY_TYPE
1635 : 18819 : || !INTEGRAL_TYPE_P (eltype))
1636 : : return false;
1637 : :
1638 : : /* Fail when the array bound is unknown or zero. */
1639 : 13412 : val = TYPE_SIZE_UNIT (optype);
1640 : 13412 : if (!val
1641 : 13340 : || TREE_CODE (val) != INTEGER_CST
1642 : 26724 : || integer_zerop (val))
1643 : 105 : return false;
1644 : :
1645 : 13307 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1646 : : integer_one_node);
1647 : :
1648 : : /* Set the minimum size to zero since the string in
1649 : : the array could have zero length. */
1650 : 13307 : pdata->minlen = ssize_int (0);
1651 : :
1652 : 13307 : tight_bound = true;
1653 : : }
1654 : 127400 : else if (TREE_CODE (arg) == COMPONENT_REF
1655 : 127400 : && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
1656 : : == ARRAY_TYPE))
1657 : : {
1658 : : /* Use the type of the member array to determine the upper
1659 : : bound on the length of the array. This may be overly
1660 : : optimistic if the array itself isn't NUL-terminated and
1661 : : the caller relies on the subsequent member to contain
1662 : : the NUL but that would only be considered valid if
1663 : : the array were the last member of a struct. */
1664 : :
1665 : 9607 : tree fld = TREE_OPERAND (arg, 1);
1666 : :
1667 : 9607 : tree optype = TREE_TYPE (fld);
1668 : :
1669 : : /* Determine the "innermost" array type. */
1670 : 9607 : while (TREE_CODE (optype) == ARRAY_TYPE
1671 : 10154 : && TREE_CODE (TREE_TYPE (optype)) == ARRAY_TYPE)
1672 : 547 : optype = TREE_TYPE (optype);
1673 : :
1674 : : /* Fail when the array bound is unknown or zero. */
1675 : 9607 : val = TYPE_SIZE_UNIT (optype);
1676 : 9607 : if (!val
1677 : 9371 : || TREE_CODE (val) != INTEGER_CST
1678 : 18943 : || integer_zerop (val))
1679 : 350 : return false;
1680 : 9257 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1681 : : integer_one_node);
1682 : :
1683 : : /* Set the minimum size to zero since the string in
1684 : : the array could have zero length. */
1685 : 9257 : pdata->minlen = ssize_int (0);
1686 : :
1687 : : /* The array size determined above is an optimistic bound
1688 : : on the length. If the array isn't nul-terminated the
1689 : : length computed by the library function would be greater.
1690 : : Even though using strlen to cross the subobject boundary
1691 : : is undefined, avoid drawing conclusions from the member
1692 : : type about the length here. */
1693 : 9257 : tight_bound = true;
1694 : : }
1695 : 117793 : else if (TREE_CODE (arg) == MEM_REF
1696 : 28198 : && TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
1697 : 5068 : && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == INTEGER_TYPE
1698 : 122423 : && TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
1699 : : {
1700 : : /* Handle a MEM_REF into a DECL accessing an array of integers,
1701 : : being conservative about references to extern structures with
1702 : : flexible array members that can be initialized to arbitrary
1703 : : numbers of elements as an extension (static structs are okay). */
1704 : 4630 : tree ref = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
1705 : 4630 : if ((TREE_CODE (ref) == PARM_DECL || VAR_P (ref))
1706 : 9247 : && (decl_binds_to_current_def_p (ref)
1707 : 438 : || !array_ref_flexible_size_p (arg)))
1708 : : {
1709 : : /* Fail if the offset is out of bounds. Such accesses
1710 : : should be diagnosed at some point. */
1711 : 4504 : val = DECL_SIZE_UNIT (ref);
1712 : 4504 : if (!val
1713 : 4332 : || TREE_CODE (val) != INTEGER_CST
1714 : 8836 : || integer_zerop (val))
1715 : 371 : return false;
1716 : :
1717 : 4330 : poly_offset_int psiz = wi::to_offset (val);
1718 : 4330 : poly_offset_int poff = mem_ref_offset (arg);
1719 : 4330 : if (known_le (psiz, poff))
1720 : : return false;
1721 : :
1722 : 4133 : pdata->minlen = ssize_int (0);
1723 : :
1724 : : /* Subtract the offset and one for the terminating nul. */
1725 : 4133 : psiz -= poff;
1726 : 4133 : psiz -= 1;
1727 : 4133 : val = wide_int_to_tree (TREE_TYPE (val), psiz);
1728 : : /* Since VAL reflects the size of a declared object
1729 : : rather the type of the access it is not a tight bound. */
1730 : : }
1731 : : }
1732 : 113163 : else if (TREE_CODE (arg) == PARM_DECL || VAR_P (arg))
1733 : : {
1734 : : /* Avoid handling pointers to arrays. GCC might misuse
1735 : : a pointer to an array of one bound to point to an array
1736 : : object of a greater bound. */
1737 : 69642 : tree argtype = TREE_TYPE (arg);
1738 : 69642 : if (TREE_CODE (argtype) == ARRAY_TYPE)
1739 : : {
1740 : 41910 : val = TYPE_SIZE_UNIT (argtype);
1741 : 41910 : if (!val
1742 : 41150 : || TREE_CODE (val) != INTEGER_CST
1743 : 83060 : || integer_zerop (val))
1744 : 875 : return false;
1745 : 41035 : val = wide_int_to_tree (TREE_TYPE (val),
1746 : 41035 : wi::sub (wi::to_wide (val), 1));
1747 : :
1748 : : /* Set the minimum size to zero since the string in
1749 : : the array could have zero length. */
1750 : 41035 : pdata->minlen = ssize_int (0);
1751 : : }
1752 : : }
1753 : : maxbound = true;
1754 : : }
1755 : :
1756 : 365222 : if (!val)
1757 : : return false;
1758 : :
1759 : : /* Adjust the lower bound on the string length as necessary. */
1760 : 269912 : if (!pdata->minlen
1761 : 269912 : || (rkind != SRK_STRLEN
1762 : 74447 : && TREE_CODE (pdata->minlen) == INTEGER_CST
1763 : 74447 : && TREE_CODE (val) == INTEGER_CST
1764 : 74442 : && tree_int_cst_lt (val, pdata->minlen)))
1765 : 195848 : pdata->minlen = val;
1766 : :
1767 : 269912 : if (pdata->maxbound && TREE_CODE (pdata->maxbound) == INTEGER_CST)
1768 : : {
1769 : : /* Adjust the tighter (more optimistic) string length bound
1770 : : if necessary and proceed to adjust the more conservative
1771 : : bound. */
1772 : 2050 : if (TREE_CODE (val) == INTEGER_CST)
1773 : : {
1774 : 2050 : if (tree_int_cst_lt (pdata->maxbound, val))
1775 : 651 : pdata->maxbound = val;
1776 : : }
1777 : : else
1778 : 0 : pdata->maxbound = val;
1779 : : }
1780 : 267862 : else if (pdata->maxbound || maxbound)
1781 : : /* Set PDATA->MAXBOUND only if it either isn't INTEGER_CST or
1782 : : if VAL corresponds to the maximum length determined based
1783 : : on the type of the object. */
1784 : 70433 : pdata->maxbound = val;
1785 : :
1786 : 269912 : if (tight_bound)
1787 : : {
1788 : : /* VAL computed above represents an optimistically tight bound
1789 : : on the length of the string based on the referenced object's
1790 : : or subobject's type. Determine the conservative upper bound
1791 : : based on the enclosing object's size if possible. */
1792 : 22564 : if (rkind == SRK_LENRANGE)
1793 : : {
1794 : 22564 : poly_int64 offset;
1795 : 22564 : tree base = get_addr_base_and_unit_offset (arg, &offset);
1796 : 22564 : if (!base)
1797 : : {
1798 : : /* When the call above fails due to a non-constant offset
1799 : : assume the offset is zero and use the size of the whole
1800 : : enclosing object instead. */
1801 : 7832 : base = get_base_address (arg);
1802 : 7832 : offset = 0;
1803 : : }
1804 : : /* If the base object is a pointer no upper bound on the length
1805 : : can be determined. Otherwise the maximum length is equal to
1806 : : the size of the enclosing object minus the offset of
1807 : : the referenced subobject minus 1 (for the terminating nul). */
1808 : 22564 : tree type = TREE_TYPE (base);
1809 : 22564 : if (TREE_CODE (type) == POINTER_TYPE
1810 : 22560 : || (TREE_CODE (base) != PARM_DECL && !VAR_P (base))
1811 : 40964 : || !(val = DECL_SIZE_UNIT (base)))
1812 : 5411 : val = build_all_ones_cst (size_type_node);
1813 : : else
1814 : : {
1815 : 17153 : val = DECL_SIZE_UNIT (base);
1816 : 17153 : val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1817 : : size_int (offset + 1));
1818 : : }
1819 : : }
1820 : : else
1821 : : return false;
1822 : : }
1823 : :
1824 : 269912 : if (pdata->maxlen)
1825 : : {
1826 : : /* Adjust the more conservative bound if possible/necessary
1827 : : and fail otherwise. */
1828 : 10010 : if (rkind != SRK_STRLEN)
1829 : : {
1830 : 9082 : if (TREE_CODE (pdata->maxlen) != INTEGER_CST
1831 : 9082 : || TREE_CODE (val) != INTEGER_CST)
1832 : : return false;
1833 : :
1834 : 9077 : if (tree_int_cst_lt (pdata->maxlen, val))
1835 : 1554 : pdata->maxlen = val;
1836 : 9077 : return true;
1837 : : }
1838 : 928 : else if (simple_cst_equal (val, pdata->maxlen) != 1)
1839 : : {
1840 : : /* Fail if the length of this ARG is different from that
1841 : : previously determined from another ARG. */
1842 : : return false;
1843 : : }
1844 : : }
1845 : :
1846 : 260026 : pdata->maxlen = val;
1847 : 260026 : return rkind == SRK_LENRANGE || !integer_all_onesp (val);
1848 : : }
1849 : :
1850 : : /* For an ARG referencing one or more strings, try to obtain the range
1851 : : of their lengths, or the size of the largest array ARG referes to if
1852 : : the range of lengths cannot be determined, and store all in *PDATA.
1853 : : For an integer ARG (when RKIND == SRK_INT_VALUE), try to determine
1854 : : the maximum constant value.
1855 : : If ARG is an SSA_NAME, follow its use-def chains. When RKIND ==
1856 : : SRK_STRLEN, then if PDATA->MAXLEN is not equal to the determined
1857 : : length or if we are unable to determine the length, return false.
1858 : : VISITED is a bitmap of visited variables.
1859 : : RKIND determines the kind of value or range to obtain (see
1860 : : strlen_range_kind).
1861 : : Set PDATA->DECL if ARG refers to an unterminated constant array.
1862 : : On input, set ELTSIZE to 1 for normal single byte character strings,
1863 : : and either 2 or 4 for wide characer strings (the size of wchar_t).
1864 : : Return true if *PDATA was successfully populated and false otherwise. */
1865 : :
1866 : : static bool
1867 : 1365713 : get_range_strlen (tree arg, bitmap visited,
1868 : : strlen_range_kind rkind,
1869 : : c_strlen_data *pdata, unsigned eltsize)
1870 : : {
1871 : :
1872 : 1446149 : if (TREE_CODE (arg) != SSA_NAME)
1873 : 455777 : return get_range_strlen_tree (arg, visited, rkind, pdata, eltsize);
1874 : :
1875 : : /* If ARG is registered for SSA update we cannot look at its defining
1876 : : statement. */
1877 : 990372 : if (name_registered_for_update_p (arg))
1878 : : return false;
1879 : :
1880 : : /* If we were already here, break the infinite cycle. */
1881 : 990372 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (arg)))
1882 : : return true;
1883 : :
1884 : 984974 : tree var = arg;
1885 : 984974 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
1886 : :
1887 : 984974 : switch (gimple_code (def_stmt))
1888 : : {
1889 : 126433 : case GIMPLE_ASSIGN:
1890 : : /* The RHS of the statement defining VAR must either have a
1891 : : constant length or come from another SSA_NAME with a constant
1892 : : length. */
1893 : 126433 : if (gimple_assign_single_p (def_stmt)
1894 : 126433 : || gimple_assign_unary_nop_p (def_stmt))
1895 : : {
1896 : 80436 : tree rhs = gimple_assign_rhs1 (def_stmt);
1897 : 80436 : return get_range_strlen (rhs, visited, rkind, pdata, eltsize);
1898 : : }
1899 : 45997 : else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1900 : : {
1901 : 246 : tree ops[2] = { gimple_assign_rhs2 (def_stmt),
1902 : 246 : gimple_assign_rhs3 (def_stmt) };
1903 : :
1904 : 738 : for (unsigned int i = 0; i < 2; i++)
1905 : 492 : if (!get_range_strlen (ops[i], visited, rkind, pdata, eltsize))
1906 : : {
1907 : 28 : if (rkind != SRK_LENRANGE)
1908 : : return false;
1909 : : /* Set the upper bound to the maximum to prevent
1910 : : it from being adjusted in the next iteration but
1911 : : leave MINLEN and the more conservative MAXBOUND
1912 : : determined so far alone (or leave them null if
1913 : : they haven't been set yet). That the MINLEN is
1914 : : in fact zero can be determined from MAXLEN being
1915 : : unbounded but the discovered minimum is used for
1916 : : diagnostics. */
1917 : 28 : pdata->maxlen = build_all_ones_cst (size_type_node);
1918 : : }
1919 : : return true;
1920 : : }
1921 : : return false;
1922 : :
1923 : : case GIMPLE_PHI:
1924 : : /* Unless RKIND == SRK_LENRANGE, all arguments of the PHI node
1925 : : must have a constant length. */
1926 : 74344 : for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
1927 : : {
1928 : 52819 : tree arg = gimple_phi_arg (def_stmt, i)->def;
1929 : :
1930 : : /* If this PHI has itself as an argument, we cannot
1931 : : determine the string length of this argument. However,
1932 : : if we can find a constant string length for the other
1933 : : PHI args then we can still be sure that this is a
1934 : : constant string length. So be optimistic and just
1935 : : continue with the next argument. */
1936 : 52819 : if (arg == gimple_phi_result (def_stmt))
1937 : 0 : continue;
1938 : :
1939 : 52819 : if (!get_range_strlen (arg, visited, rkind, pdata, eltsize))
1940 : : {
1941 : 24474 : if (rkind != SRK_LENRANGE)
1942 : : return false;
1943 : : /* Set the upper bound to the maximum to prevent
1944 : : it from being adjusted in the next iteration but
1945 : : leave MINLEN and the more conservative MAXBOUND
1946 : : determined so far alone (or leave them null if
1947 : : they haven't been set yet). That the MINLEN is
1948 : : in fact zero can be determined from MAXLEN being
1949 : : unbounded but the discovered minimum is used for
1950 : : diagnostics. */
1951 : 22729 : pdata->maxlen = build_all_ones_cst (size_type_node);
1952 : : }
1953 : : }
1954 : : return true;
1955 : :
1956 : : default:
1957 : : return false;
1958 : : }
1959 : : }
1960 : :
1961 : : /* Try to obtain the range of the lengths of the string(s) referenced
1962 : : by ARG, or the size of the largest array ARG refers to if the range
1963 : : of lengths cannot be determined, and store all in *PDATA which must
1964 : : be zero-initialized on input except PDATA->MAXBOUND may be set to
1965 : : a non-null tree node other than INTEGER_CST to request to have it
1966 : : set to the length of the longest string in a PHI. ELTSIZE is
1967 : : the expected size of the string element in bytes: 1 for char and
1968 : : some power of 2 for wide characters.
1969 : : Return true if the range [PDATA->MINLEN, PDATA->MAXLEN] is suitable
1970 : : for optimization. Returning false means that a nonzero PDATA->MINLEN
1971 : : doesn't reflect the true lower bound of the range when PDATA->MAXLEN
1972 : : is -1 (in that case, the actual range is indeterminate, i.e.,
1973 : : [0, PTRDIFF_MAX - 2]. */
1974 : :
1975 : : bool
1976 : 1135848 : get_range_strlen (tree arg, c_strlen_data *pdata, unsigned eltsize)
1977 : : {
1978 : 1135848 : auto_bitmap visited;
1979 : 1135848 : tree maxbound = pdata->maxbound;
1980 : :
1981 : 1135848 : if (!get_range_strlen (arg, visited, SRK_LENRANGE, pdata, eltsize))
1982 : : {
1983 : : /* On failure extend the length range to an impossible maximum
1984 : : (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1985 : : members can stay unchanged regardless. */
1986 : 914181 : pdata->minlen = ssize_int (0);
1987 : 914181 : pdata->maxlen = build_all_ones_cst (size_type_node);
1988 : : }
1989 : 221667 : else if (!pdata->minlen)
1990 : 7178 : pdata->minlen = ssize_int (0);
1991 : :
1992 : : /* If it's unchanged from it initial non-null value, set the conservative
1993 : : MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
1994 : 1135848 : if (maxbound && pdata->maxbound == maxbound)
1995 : 650803 : pdata->maxbound = build_all_ones_cst (size_type_node);
1996 : :
1997 : 1135848 : return !integer_all_onesp (pdata->maxlen);
1998 : 1135848 : }
1999 : :
2000 : : /* Return the maximum value for ARG given RKIND (see strlen_range_kind).
2001 : : For ARG of pointer types, NONSTR indicates if the caller is prepared
2002 : : to handle unterminated strings. For integer ARG and when RKIND ==
2003 : : SRK_INT_VALUE, NONSTR must be null.
2004 : :
2005 : : If an unterminated array is discovered and our caller handles
2006 : : unterminated arrays, then bubble up the offending DECL and
2007 : : return the maximum size. Otherwise return NULL. */
2008 : :
2009 : : static tree
2010 : 95887 : get_maxval_strlen (tree arg, strlen_range_kind rkind, tree *nonstr = NULL)
2011 : : {
2012 : : /* A non-null NONSTR is meaningless when determining the maximum
2013 : : value of an integer ARG. */
2014 : 95887 : gcc_assert (rkind != SRK_INT_VALUE || nonstr == NULL);
2015 : : /* ARG must have an integral type when RKIND says so. */
2016 : 95887 : gcc_assert (rkind != SRK_INT_VALUE || INTEGRAL_TYPE_P (TREE_TYPE (arg)));
2017 : :
2018 : 95887 : auto_bitmap visited;
2019 : :
2020 : : /* Reset DATA.MAXLEN if the call fails or when DATA.MAXLEN
2021 : : is unbounded. */
2022 : 95887 : c_strlen_data lendata = { };
2023 : 95887 : if (!get_range_strlen (arg, visited, rkind, &lendata, /* eltsize = */1))
2024 : 50093 : lendata.maxlen = NULL_TREE;
2025 : 45794 : else if (lendata.maxlen && integer_all_onesp (lendata.maxlen))
2026 : 0 : lendata.maxlen = NULL_TREE;
2027 : :
2028 : 95887 : if (nonstr)
2029 : : {
2030 : : /* For callers prepared to handle unterminated arrays set
2031 : : *NONSTR to point to the declaration of the array and return
2032 : : the maximum length/size. */
2033 : 23935 : *nonstr = lendata.decl;
2034 : 23935 : return lendata.maxlen;
2035 : : }
2036 : :
2037 : : /* Fail if the constant array isn't nul-terminated. */
2038 : 71952 : return lendata.decl ? NULL_TREE : lendata.maxlen;
2039 : 95887 : }
2040 : :
2041 : : /* Return true if LEN is known to be less than or equal to (or if STRICT is
2042 : : true, strictly less than) the lower bound of SIZE at compile time and false
2043 : : otherwise. */
2044 : :
2045 : : static bool
2046 : 63803 : known_lower (gimple *stmt, tree len, tree size, bool strict = false)
2047 : : {
2048 : 63803 : if (len == NULL_TREE)
2049 : : return false;
2050 : :
2051 : 237235 : wide_int size_range[2];
2052 : 237235 : wide_int len_range[2];
2053 : 47447 : if (get_range (len, stmt, len_range) && get_range (size, stmt, size_range))
2054 : : {
2055 : 16863 : if (strict)
2056 : 1913 : return wi::ltu_p (len_range[1], size_range[0]);
2057 : : else
2058 : 14950 : return wi::leu_p (len_range[1], size_range[0]);
2059 : : }
2060 : :
2061 : : return false;
2062 : 284682 : }
2063 : :
2064 : : /* Fold function call to builtin strcpy with arguments DEST and SRC.
2065 : : If LEN is not NULL, it represents the length of the string to be
2066 : : copied. Return NULL_TREE if no simplification can be made. */
2067 : :
2068 : : static bool
2069 : 26019 : gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
2070 : : tree dest, tree src)
2071 : : {
2072 : 26019 : gimple *stmt = gsi_stmt (*gsi);
2073 : 26019 : location_t loc = gimple_location (stmt);
2074 : 26019 : tree fn;
2075 : :
2076 : : /* If SRC and DEST are the same (and not volatile), return DEST. */
2077 : 26019 : if (operand_equal_p (src, dest, 0))
2078 : : {
2079 : : /* Issue -Wrestrict unless the pointers are null (those do
2080 : : not point to objects and so do not indicate an overlap;
2081 : : such calls could be the result of sanitization and jump
2082 : : threading). */
2083 : 86 : if (!integer_zerop (dest) && !warning_suppressed_p (stmt, OPT_Wrestrict))
2084 : : {
2085 : 51 : tree func = gimple_call_fndecl (stmt);
2086 : :
2087 : 51 : warning_at (loc, OPT_Wrestrict,
2088 : : "%qD source argument is the same as destination",
2089 : : func);
2090 : : }
2091 : :
2092 : 86 : replace_call_with_value (gsi, dest);
2093 : 86 : return true;
2094 : : }
2095 : :
2096 : 25933 : if (optimize_function_for_size_p (cfun))
2097 : : return false;
2098 : :
2099 : 23935 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2100 : 23935 : if (!fn)
2101 : : return false;
2102 : :
2103 : : /* Set to non-null if ARG refers to an unterminated array. */
2104 : 23935 : tree nonstr = NULL;
2105 : 23935 : tree len = get_maxval_strlen (src, SRK_STRLEN, &nonstr);
2106 : :
2107 : 23935 : if (nonstr)
2108 : : {
2109 : : /* Avoid folding calls with unterminated arrays. */
2110 : 531 : if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
2111 : 69 : warn_string_no_nul (loc, stmt, "strcpy", src, nonstr);
2112 : 531 : suppress_warning (stmt, OPT_Wstringop_overread);
2113 : 531 : return false;
2114 : : }
2115 : :
2116 : 28748 : if (!len || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2117 : : return false;
2118 : :
2119 : 2815 : len = fold_convert_loc (loc, size_type_node, len);
2120 : 2815 : len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
2121 : 2815 : len = force_gimple_operand_gsi (gsi, len, true,
2122 : : NULL_TREE, true, GSI_SAME_STMT);
2123 : 2815 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2124 : 2815 : replace_call_with_call_and_fold (gsi, repl);
2125 : 2815 : return true;
2126 : : }
2127 : :
2128 : : /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
2129 : : If SLEN is not NULL, it represents the length of the source string.
2130 : : Return NULL_TREE if no simplification can be made. */
2131 : :
2132 : : static bool
2133 : 17199 : gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
2134 : : tree dest, tree src, tree len)
2135 : : {
2136 : 17199 : gimple *stmt = gsi_stmt (*gsi);
2137 : 17199 : location_t loc = gimple_location (stmt);
2138 : 17199 : bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
2139 : :
2140 : : /* If the LEN parameter is zero, return DEST. */
2141 : 17199 : if (integer_zerop (len))
2142 : : {
2143 : : /* Avoid warning if the destination refers to an array/pointer
2144 : : decorate with attribute nonstring. */
2145 : 167 : if (!nonstring)
2146 : : {
2147 : 155 : tree fndecl = gimple_call_fndecl (stmt);
2148 : :
2149 : : /* Warn about the lack of nul termination: the result is not
2150 : : a (nul-terminated) string. */
2151 : 155 : tree slen = get_maxval_strlen (src, SRK_STRLEN);
2152 : 155 : if (slen && !integer_zerop (slen))
2153 : 24 : warning_at (loc, OPT_Wstringop_truncation,
2154 : : "%qD destination unchanged after copying no bytes "
2155 : : "from a string of length %E",
2156 : : fndecl, slen);
2157 : : else
2158 : 131 : warning_at (loc, OPT_Wstringop_truncation,
2159 : : "%qD destination unchanged after copying no bytes",
2160 : : fndecl);
2161 : : }
2162 : :
2163 : 167 : replace_call_with_value (gsi, dest);
2164 : 167 : return true;
2165 : : }
2166 : :
2167 : : /* We can't compare slen with len as constants below if len is not a
2168 : : constant. */
2169 : 17032 : if (TREE_CODE (len) != INTEGER_CST)
2170 : : return false;
2171 : :
2172 : : /* Now, we must be passed a constant src ptr parameter. */
2173 : 10677 : tree slen = get_maxval_strlen (src, SRK_STRLEN);
2174 : 10677 : if (!slen || TREE_CODE (slen) != INTEGER_CST)
2175 : : return false;
2176 : :
2177 : : /* The size of the source string including the terminating nul. */
2178 : 1780 : tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
2179 : :
2180 : : /* We do not support simplification of this case, though we do
2181 : : support it when expanding trees into RTL. */
2182 : : /* FIXME: generate a call to __builtin_memset. */
2183 : 1780 : if (tree_int_cst_lt (ssize, len))
2184 : : return false;
2185 : :
2186 : : /* Diagnose truncation that leaves the copy unterminated. */
2187 : 695 : maybe_diag_stxncpy_trunc (*gsi, src, len);
2188 : :
2189 : : /* OK transform into builtin memcpy. */
2190 : 695 : tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2191 : 17727 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2192 : : return false;
2193 : :
2194 : 695 : len = fold_convert_loc (loc, size_type_node, len);
2195 : 695 : len = force_gimple_operand_gsi (gsi, len, true,
2196 : : NULL_TREE, true, GSI_SAME_STMT);
2197 : 695 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2198 : 695 : replace_call_with_call_and_fold (gsi, repl);
2199 : :
2200 : 695 : return true;
2201 : : }
2202 : :
2203 : : /* Fold function call to builtin strchr or strrchr.
2204 : : If both arguments are constant, evaluate and fold the result,
2205 : : otherwise simplify str(r)chr (str, 0) into str + strlen (str).
2206 : : In general strlen is significantly faster than strchr
2207 : : due to being a simpler operation. */
2208 : : static bool
2209 : 5731 : gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
2210 : : {
2211 : 5731 : gimple *stmt = gsi_stmt (*gsi);
2212 : 5731 : tree str = gimple_call_arg (stmt, 0);
2213 : 5731 : tree c = gimple_call_arg (stmt, 1);
2214 : 5731 : location_t loc = gimple_location (stmt);
2215 : 5731 : const char *p;
2216 : 5731 : char ch;
2217 : :
2218 : 5731 : if (!gimple_call_lhs (stmt))
2219 : : return false;
2220 : :
2221 : : /* Avoid folding if the first argument is not a nul-terminated array.
2222 : : Defer warning until later. */
2223 : 5721 : if (!check_nul_terminated_array (NULL_TREE, str))
2224 : : return false;
2225 : :
2226 : 5637 : if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
2227 : : {
2228 : 41 : const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
2229 : :
2230 : 41 : if (p1 == NULL)
2231 : : {
2232 : 1 : replace_call_with_value (gsi, integer_zero_node);
2233 : 1 : return true;
2234 : : }
2235 : :
2236 : 40 : tree len = build_int_cst (size_type_node, p1 - p);
2237 : 40 : gimple_seq stmts = NULL;
2238 : 40 : gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
2239 : : POINTER_PLUS_EXPR, str, len);
2240 : 40 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2241 : 40 : gsi_replace_with_seq_vops (gsi, stmts);
2242 : 40 : return true;
2243 : : }
2244 : :
2245 : 5678 : if (!integer_zerop (c) || (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun)))
2246 : : return false;
2247 : :
2248 : : /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
2249 : 82 : if (is_strrchr && optimize_function_for_size_p (cfun))
2250 : : {
2251 : 3 : tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
2252 : :
2253 : 3 : if (strchr_fn)
2254 : : {
2255 : 3 : gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
2256 : 3 : replace_call_with_call_and_fold (gsi, repl);
2257 : 3 : return true;
2258 : : }
2259 : :
2260 : : return false;
2261 : : }
2262 : :
2263 : 79 : tree len;
2264 : 5687 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2265 : :
2266 : 79 : if (!strlen_fn)
2267 : : return false;
2268 : :
2269 : : /* Create newstr = strlen (str). */
2270 : 79 : gimple_seq stmts = NULL;
2271 : 79 : gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
2272 : 79 : gimple_set_location (new_stmt, loc);
2273 : 79 : len = make_ssa_name (size_type_node);
2274 : 79 : gimple_call_set_lhs (new_stmt, len);
2275 : 79 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2276 : :
2277 : : /* Create (str p+ strlen (str)). */
2278 : 79 : new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
2279 : : POINTER_PLUS_EXPR, str, len);
2280 : 79 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2281 : 79 : gsi_replace_with_seq_vops (gsi, stmts);
2282 : : /* gsi now points at the assignment to the lhs, get a
2283 : : stmt iterator to the strlen.
2284 : : ??? We can't use gsi_for_stmt as that doesn't work when the
2285 : : CFG isn't built yet. */
2286 : 79 : gimple_stmt_iterator gsi2 = *gsi;
2287 : 79 : gsi_prev (&gsi2);
2288 : 79 : fold_stmt (&gsi2);
2289 : 79 : return true;
2290 : : }
2291 : :
2292 : : /* Fold function call to builtin strstr.
2293 : : If both arguments are constant, evaluate and fold the result,
2294 : : additionally fold strstr (x, "") into x and strstr (x, "c")
2295 : : into strchr (x, 'c'). */
2296 : : static bool
2297 : 4325 : gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
2298 : : {
2299 : 4325 : gimple *stmt = gsi_stmt (*gsi);
2300 : 4325 : if (!gimple_call_lhs (stmt))
2301 : : return false;
2302 : :
2303 : 4322 : tree haystack = gimple_call_arg (stmt, 0);
2304 : 4322 : tree needle = gimple_call_arg (stmt, 1);
2305 : :
2306 : : /* Avoid folding if either argument is not a nul-terminated array.
2307 : : Defer warning until later. */
2308 : 4322 : if (!check_nul_terminated_array (NULL_TREE, haystack)
2309 : 4322 : || !check_nul_terminated_array (NULL_TREE, needle))
2310 : 19 : return false;
2311 : :
2312 : 4303 : const char *q = c_getstr (needle);
2313 : 4303 : if (q == NULL)
2314 : : return false;
2315 : :
2316 : 3143 : if (const char *p = c_getstr (haystack))
2317 : : {
2318 : 14 : const char *r = strstr (p, q);
2319 : :
2320 : 14 : if (r == NULL)
2321 : : {
2322 : 1 : replace_call_with_value (gsi, integer_zero_node);
2323 : 1 : return true;
2324 : : }
2325 : :
2326 : 13 : tree len = build_int_cst (size_type_node, r - p);
2327 : 13 : gimple_seq stmts = NULL;
2328 : 13 : gimple *new_stmt
2329 : 13 : = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
2330 : : haystack, len);
2331 : 13 : gimple_seq_add_stmt_without_update (&stmts, new_stmt);
2332 : 13 : gsi_replace_with_seq_vops (gsi, stmts);
2333 : 13 : return true;
2334 : : }
2335 : :
2336 : : /* For strstr (x, "") return x. */
2337 : 3129 : if (q[0] == '\0')
2338 : : {
2339 : 6 : replace_call_with_value (gsi, haystack);
2340 : 6 : return true;
2341 : : }
2342 : :
2343 : 10529 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2344 : : return false;
2345 : :
2346 : : /* Transform strstr (x, "c") into strchr (x, 'c'). */
2347 : 3123 : if (q[1] == '\0')
2348 : : {
2349 : 22 : tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
2350 : 22 : if (strchr_fn)
2351 : : {
2352 : 22 : tree c = build_int_cst (integer_type_node, q[0]);
2353 : 22 : gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
2354 : 22 : replace_call_with_call_and_fold (gsi, repl);
2355 : 22 : return true;
2356 : : }
2357 : : }
2358 : :
2359 : : return false;
2360 : : }
2361 : :
2362 : : /* Simplify a call to the strcat builtin. DST and SRC are the arguments
2363 : : to the call.
2364 : :
2365 : : Return NULL_TREE if no simplification was possible, otherwise return the
2366 : : simplified form of the call as a tree.
2367 : :
2368 : : The simplified form may be a constant or other expression which
2369 : : computes the same value, but in a more efficient manner (including
2370 : : calls to other builtin functions).
2371 : :
2372 : : The call may contain arguments which need to be evaluated, but
2373 : : which are not useful to determine the result of the call. In
2374 : : this case we return a chain of COMPOUND_EXPRs. The LHS of each
2375 : : COMPOUND_EXPR will be an argument which must be evaluated.
2376 : : COMPOUND_EXPRs are chained through their RHS. The RHS of the last
2377 : : COMPOUND_EXPR in the chain will contain the tree for the simplified
2378 : : form of the builtin function call. */
2379 : :
2380 : : static bool
2381 : 7457 : gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
2382 : : {
2383 : 7457 : gimple *stmt = gsi_stmt (*gsi);
2384 : 7457 : location_t loc = gimple_location (stmt);
2385 : :
2386 : 7457 : const char *p = c_getstr (src);
2387 : :
2388 : : /* If the string length is zero, return the dst parameter. */
2389 : 7457 : if (p && *p == '\0')
2390 : : {
2391 : 72 : replace_call_with_value (gsi, dst);
2392 : 72 : return true;
2393 : : }
2394 : :
2395 : 7385 : if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
2396 : : return false;
2397 : :
2398 : 20163 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2399 : : return false;
2400 : :
2401 : : /* See if we can store by pieces into (dst + strlen(dst)). */
2402 : 6802 : tree newdst;
2403 : 6802 : tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
2404 : 6802 : tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2405 : :
2406 : 6802 : if (!strlen_fn || !memcpy_fn)
2407 : : return false;
2408 : :
2409 : : /* If the length of the source string isn't computable don't
2410 : : split strcat into strlen and memcpy. */
2411 : 6802 : tree len = get_maxval_strlen (src, SRK_STRLEN);
2412 : 6802 : if (! len)
2413 : : return false;
2414 : :
2415 : : /* Create strlen (dst). */
2416 : 826 : gimple_seq stmts = NULL, stmts2;
2417 : 826 : gimple *repl = gimple_build_call (strlen_fn, 1, dst);
2418 : 826 : gimple_set_location (repl, loc);
2419 : 826 : newdst = make_ssa_name (size_type_node);
2420 : 826 : gimple_call_set_lhs (repl, newdst);
2421 : 826 : gimple_seq_add_stmt_without_update (&stmts, repl);
2422 : :
2423 : : /* Create (dst p+ strlen (dst)). */
2424 : 826 : newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
2425 : 826 : newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
2426 : 826 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2427 : :
2428 : 826 : len = fold_convert_loc (loc, size_type_node, len);
2429 : 826 : len = size_binop_loc (loc, PLUS_EXPR, len,
2430 : : build_int_cst (size_type_node, 1));
2431 : 826 : len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
2432 : 826 : gimple_seq_add_seq_without_update (&stmts, stmts2);
2433 : :
2434 : 826 : repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
2435 : 826 : gimple_seq_add_stmt_without_update (&stmts, repl);
2436 : 826 : if (gimple_call_lhs (stmt))
2437 : : {
2438 : 165 : repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
2439 : 165 : gimple_seq_add_stmt_without_update (&stmts, repl);
2440 : 165 : gsi_replace_with_seq_vops (gsi, stmts);
2441 : : /* gsi now points at the assignment to the lhs, get a
2442 : : stmt iterator to the memcpy call.
2443 : : ??? We can't use gsi_for_stmt as that doesn't work when the
2444 : : CFG isn't built yet. */
2445 : 165 : gimple_stmt_iterator gsi2 = *gsi;
2446 : 165 : gsi_prev (&gsi2);
2447 : 165 : fold_stmt (&gsi2);
2448 : : }
2449 : : else
2450 : : {
2451 : 661 : gsi_replace_with_seq_vops (gsi, stmts);
2452 : 661 : fold_stmt (gsi);
2453 : : }
2454 : : return true;
2455 : : }
2456 : :
2457 : : /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
2458 : : are the arguments to the call. */
2459 : :
2460 : : static bool
2461 : 1702 : gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
2462 : : {
2463 : 1702 : gimple *stmt = gsi_stmt (*gsi);
2464 : 1702 : tree dest = gimple_call_arg (stmt, 0);
2465 : 1702 : tree src = gimple_call_arg (stmt, 1);
2466 : 1702 : tree size = gimple_call_arg (stmt, 2);
2467 : 1702 : tree fn;
2468 : 1702 : const char *p;
2469 : :
2470 : 1702 : p = c_getstr (src);
2471 : : /* If the SRC parameter is "", return DEST. */
2472 : 1702 : if (p && *p == '\0')
2473 : : {
2474 : 60 : replace_call_with_value (gsi, dest);
2475 : 60 : return true;
2476 : : }
2477 : :
2478 : 1642 : if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
2479 : 1560 : return false;
2480 : :
2481 : 1724 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2482 : : return false;
2483 : :
2484 : : /* If __builtin_strcat_chk is used, assume strcat is available. */
2485 : 82 : fn = builtin_decl_explicit (BUILT_IN_STRCAT);
2486 : 82 : if (!fn)
2487 : : return false;
2488 : :
2489 : 82 : gimple *repl = gimple_build_call (fn, 2, dest, src);
2490 : 82 : replace_call_with_call_and_fold (gsi, repl);
2491 : 82 : return true;
2492 : : }
2493 : :
2494 : : /* Simplify a call to the strncat builtin. */
2495 : :
2496 : : static bool
2497 : 6786 : gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
2498 : : {
2499 : 6786 : gimple *stmt = gsi_stmt (*gsi);
2500 : 6786 : tree dst = gimple_call_arg (stmt, 0);
2501 : 6786 : tree src = gimple_call_arg (stmt, 1);
2502 : 6786 : tree len = gimple_call_arg (stmt, 2);
2503 : 6786 : tree src_len = c_strlen (src, 1);
2504 : :
2505 : : /* If the requested length is zero, or the src parameter string
2506 : : length is zero, return the dst parameter. */
2507 : 6786 : if (integer_zerop (len) || (src_len && integer_zerop (src_len)))
2508 : : {
2509 : 119 : replace_call_with_value (gsi, dst);
2510 : 119 : return true;
2511 : : }
2512 : :
2513 : : /* Return early if the requested len is less than the string length.
2514 : : Warnings will be issued elsewhere later. */
2515 : 6667 : if (!src_len || known_lower (stmt, len, src_len, true))
2516 : 6099 : return false;
2517 : :
2518 : : /* Warn on constant LEN. */
2519 : 568 : if (TREE_CODE (len) == INTEGER_CST)
2520 : : {
2521 : 131 : bool nowarn = warning_suppressed_p (stmt, OPT_Wstringop_overflow_);
2522 : 131 : tree dstsize;
2523 : :
2524 : 131 : if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize)
2525 : 175 : && TREE_CODE (dstsize) == INTEGER_CST)
2526 : : {
2527 : 44 : int cmpdst = tree_int_cst_compare (len, dstsize);
2528 : :
2529 : 44 : if (cmpdst >= 0)
2530 : : {
2531 : 19 : tree fndecl = gimple_call_fndecl (stmt);
2532 : :
2533 : : /* Strncat copies (at most) LEN bytes and always appends
2534 : : the terminating NUL so the specified bound should never
2535 : : be equal to (or greater than) the size of the destination.
2536 : : If it is, the copy could overflow. */
2537 : 19 : location_t loc = gimple_location (stmt);
2538 : 37 : nowarn = warning_at (loc, OPT_Wstringop_overflow_,
2539 : : cmpdst == 0
2540 : : ? G_("%qD specified bound %E equals "
2541 : : "destination size")
2542 : : : G_("%qD specified bound %E exceeds "
2543 : : "destination size %E"),
2544 : : fndecl, len, dstsize);
2545 : 19 : if (nowarn)
2546 : 0 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2547 : : }
2548 : : }
2549 : :
2550 : 131 : if (!nowarn && TREE_CODE (src_len) == INTEGER_CST
2551 : 243 : && tree_int_cst_compare (src_len, len) == 0)
2552 : : {
2553 : 20 : tree fndecl = gimple_call_fndecl (stmt);
2554 : 20 : location_t loc = gimple_location (stmt);
2555 : :
2556 : : /* To avoid possible overflow the specified bound should also
2557 : : not be equal to the length of the source, even when the size
2558 : : of the destination is unknown (it's not an uncommon mistake
2559 : : to specify as the bound to strncpy the length of the source). */
2560 : 20 : if (warning_at (loc, OPT_Wstringop_overflow_,
2561 : : "%qD specified bound %E equals source length",
2562 : : fndecl, len))
2563 : 6 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2564 : : }
2565 : : }
2566 : :
2567 : 568 : if (!known_lower (stmt, src_len, len))
2568 : : return false;
2569 : :
2570 : 136 : tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
2571 : :
2572 : : /* If the replacement _DECL isn't initialized, don't do the
2573 : : transformation. */
2574 : 6803 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2575 : : return false;
2576 : :
2577 : : /* Otherwise, emit a call to strcat. */
2578 : 136 : gcall *repl = gimple_build_call (fn, 2, dst, src);
2579 : 136 : replace_call_with_call_and_fold (gsi, repl);
2580 : 136 : return true;
2581 : : }
2582 : :
2583 : : /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2584 : : LEN, and SIZE. */
2585 : :
2586 : : static bool
2587 : 1143 : gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
2588 : : {
2589 : 1143 : gimple *stmt = gsi_stmt (*gsi);
2590 : 1143 : tree dest = gimple_call_arg (stmt, 0);
2591 : 1143 : tree src = gimple_call_arg (stmt, 1);
2592 : 1143 : tree len = gimple_call_arg (stmt, 2);
2593 : 1143 : tree size = gimple_call_arg (stmt, 3);
2594 : 1143 : tree fn;
2595 : 1143 : const char *p;
2596 : :
2597 : 1143 : p = c_getstr (src);
2598 : : /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2599 : 302 : if ((p && *p == '\0')
2600 : 1394 : || integer_zerop (len))
2601 : : {
2602 : 78 : replace_call_with_value (gsi, dest);
2603 : 78 : return true;
2604 : : }
2605 : :
2606 : 3043 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
2607 : : return false;
2608 : :
2609 : 1065 : if (! integer_all_onesp (size))
2610 : : {
2611 : 978 : tree src_len = c_strlen (src, 1);
2612 : 978 : if (known_lower (stmt, src_len, len))
2613 : : {
2614 : : /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2615 : 65 : fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
2616 : 65 : if (!fn)
2617 : : return false;
2618 : :
2619 : 65 : gimple *repl = gimple_build_call (fn, 3, dest, src, size);
2620 : 65 : replace_call_with_call_and_fold (gsi, repl);
2621 : 65 : return true;
2622 : : }
2623 : : return false;
2624 : : }
2625 : :
2626 : : /* If __builtin_strncat_chk is used, assume strncat is available. */
2627 : 87 : fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
2628 : 87 : if (!fn)
2629 : : return false;
2630 : :
2631 : 87 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
2632 : 87 : replace_call_with_call_and_fold (gsi, repl);
2633 : 87 : return true;
2634 : : }
2635 : :
2636 : : /* Build and append gimple statements to STMTS that would load a first
2637 : : character of a memory location identified by STR. LOC is location
2638 : : of the statement. */
2639 : :
2640 : : static tree
2641 : 461 : gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
2642 : : {
2643 : 461 : tree var;
2644 : :
2645 : 461 : tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2646 : 461 : tree cst_uchar_ptr_node
2647 : 461 : = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
2648 : 461 : tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
2649 : :
2650 : 461 : tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
2651 : 461 : gassign *stmt = gimple_build_assign (NULL_TREE, temp);
2652 : 461 : var = make_ssa_name (cst_uchar_node, stmt);
2653 : :
2654 : 461 : gimple_assign_set_lhs (stmt, var);
2655 : 461 : gimple_seq_add_stmt_without_update (stmts, stmt);
2656 : :
2657 : 461 : return var;
2658 : : }
2659 : :
2660 : : /* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. */
2661 : :
2662 : : static bool
2663 : 1247530 : gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
2664 : : {
2665 : 1247530 : gimple *stmt = gsi_stmt (*gsi);
2666 : 1247530 : tree callee = gimple_call_fndecl (stmt);
2667 : 1247530 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
2668 : :
2669 : 1247530 : tree type = integer_type_node;
2670 : 1247530 : tree str1 = gimple_call_arg (stmt, 0);
2671 : 1247530 : tree str2 = gimple_call_arg (stmt, 1);
2672 : 1247530 : tree lhs = gimple_call_lhs (stmt);
2673 : :
2674 : 1247530 : tree bound_node = NULL_TREE;
2675 : 1247530 : unsigned HOST_WIDE_INT bound = HOST_WIDE_INT_M1U;
2676 : :
2677 : : /* Handle strncmp and strncasecmp functions. */
2678 : 1247530 : if (gimple_call_num_args (stmt) == 3)
2679 : : {
2680 : 22753 : bound_node = gimple_call_arg (stmt, 2);
2681 : 22753 : if (tree_fits_uhwi_p (bound_node))
2682 : 17037 : bound = tree_to_uhwi (bound_node);
2683 : : }
2684 : :
2685 : : /* If the BOUND parameter is zero, return zero. */
2686 : 17037 : if (bound == 0)
2687 : : {
2688 : 4 : replace_call_with_value (gsi, integer_zero_node);
2689 : 4 : return true;
2690 : : }
2691 : :
2692 : : /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2693 : 1247526 : if (operand_equal_p (str1, str2, 0))
2694 : : {
2695 : 41 : replace_call_with_value (gsi, integer_zero_node);
2696 : 41 : return true;
2697 : : }
2698 : :
2699 : 2494970 : if (!gimple_vuse (stmt) && gimple_in_ssa_p (cfun))
2700 : : return false;
2701 : :
2702 : : /* Initially set to the number of characters, including the terminating
2703 : : nul if each array has one. LENx == strnlen (Sx, LENx) implies that
2704 : : the array Sx is not terminated by a nul.
2705 : : For nul-terminated strings then adjusted to their length so that
2706 : : LENx == NULPOSx holds. */
2707 : 1247485 : unsigned HOST_WIDE_INT len1 = HOST_WIDE_INT_MAX, len2 = len1;
2708 : 1247485 : const char *p1 = getbyterep (str1, &len1);
2709 : 1247485 : const char *p2 = getbyterep (str2, &len2);
2710 : :
2711 : : /* The position of the terminating nul character if one exists, otherwise
2712 : : a value greater than LENx. */
2713 : 1247485 : unsigned HOST_WIDE_INT nulpos1 = HOST_WIDE_INT_MAX, nulpos2 = nulpos1;
2714 : :
2715 : 1247485 : if (p1)
2716 : : {
2717 : 42798 : size_t n = strnlen (p1, len1);
2718 : 42798 : if (n < len1)
2719 : 42691 : len1 = nulpos1 = n;
2720 : : }
2721 : :
2722 : 1247485 : if (p2)
2723 : : {
2724 : 1218342 : size_t n = strnlen (p2, len2);
2725 : 1218342 : if (n < len2)
2726 : 1218283 : len2 = nulpos2 = n;
2727 : : }
2728 : :
2729 : : /* For known strings, return an immediate value. */
2730 : 1247485 : if (p1 && p2)
2731 : : {
2732 : 39244 : int r = 0;
2733 : 39244 : bool known_result = false;
2734 : :
2735 : 39244 : switch (fcode)
2736 : : {
2737 : 38169 : case BUILT_IN_STRCMP:
2738 : 38169 : case BUILT_IN_STRCMP_EQ:
2739 : 38169 : if (len1 != nulpos1 || len2 != nulpos2)
2740 : : break;
2741 : :
2742 : 38144 : r = strcmp (p1, p2);
2743 : 38144 : known_result = true;
2744 : 38144 : break;
2745 : :
2746 : 1002 : case BUILT_IN_STRNCMP:
2747 : 1002 : case BUILT_IN_STRNCMP_EQ:
2748 : 1002 : {
2749 : 1002 : if (bound == HOST_WIDE_INT_M1U)
2750 : : break;
2751 : :
2752 : : /* Reduce the bound to be no more than the length
2753 : : of the shorter of the two strings, or the sizes
2754 : : of the unterminated arrays. */
2755 : 38 : unsigned HOST_WIDE_INT n = bound;
2756 : :
2757 : 38 : if (len1 == nulpos1 && len1 < n)
2758 : 4 : n = len1 + 1;
2759 : 38 : if (len2 == nulpos2 && len2 < n)
2760 : 11 : n = len2 + 1;
2761 : :
2762 : 38 : if (MIN (nulpos1, nulpos2) + 1 < n)
2763 : : break;
2764 : :
2765 : 38 : r = strncmp (p1, p2, n);
2766 : 38 : known_result = true;
2767 : 38 : break;
2768 : : }
2769 : : /* Only handleable situation is where the string are equal (result 0),
2770 : : which is already handled by operand_equal_p case. */
2771 : : case BUILT_IN_STRCASECMP:
2772 : : break;
2773 : 37 : case BUILT_IN_STRNCASECMP:
2774 : 37 : {
2775 : 37 : if (bound == HOST_WIDE_INT_M1U)
2776 : : break;
2777 : 37 : r = strncmp (p1, p2, bound);
2778 : 37 : if (r == 0)
2779 : : known_result = true;
2780 : : break;
2781 : : }
2782 : 0 : default:
2783 : 0 : gcc_unreachable ();
2784 : : }
2785 : :
2786 : 38182 : if (known_result)
2787 : : {
2788 : 38182 : replace_call_with_value (gsi, build_cmp_result (type, r));
2789 : 38182 : return true;
2790 : : }
2791 : : }
2792 : :
2793 : 2418606 : bool nonzero_bound = (bound >= 1 && bound < HOST_WIDE_INT_M1U)
2794 : 1192410 : || fcode == BUILT_IN_STRCMP
2795 : 1192410 : || fcode == BUILT_IN_STRCMP_EQ
2796 : 1215191 : || fcode == BUILT_IN_STRCASECMP;
2797 : :
2798 : 1209303 : location_t loc = gimple_location (stmt);
2799 : :
2800 : : /* If the second arg is "", return *(const unsigned char*)arg1. */
2801 : 1209303 : if (p2 && *p2 == '\0' && nonzero_bound)
2802 : : {
2803 : 150 : gimple_seq stmts = NULL;
2804 : 150 : tree var = gimple_load_first_char (loc, str1, &stmts);
2805 : 150 : if (lhs)
2806 : : {
2807 : 150 : stmt = gimple_build_assign (lhs, NOP_EXPR, var);
2808 : 150 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2809 : : }
2810 : :
2811 : 150 : gsi_replace_with_seq_vops (gsi, stmts);
2812 : 150 : return true;
2813 : : }
2814 : :
2815 : : /* If the first arg is "", return -*(const unsigned char*)arg2. */
2816 : 1209153 : if (p1 && *p1 == '\0' && nonzero_bound)
2817 : : {
2818 : 91 : gimple_seq stmts = NULL;
2819 : 91 : tree var = gimple_load_first_char (loc, str2, &stmts);
2820 : :
2821 : 91 : if (lhs)
2822 : : {
2823 : 91 : tree c = make_ssa_name (integer_type_node);
2824 : 91 : stmt = gimple_build_assign (c, NOP_EXPR, var);
2825 : 91 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2826 : :
2827 : 91 : stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
2828 : 91 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2829 : : }
2830 : :
2831 : 91 : gsi_replace_with_seq_vops (gsi, stmts);
2832 : 91 : return true;
2833 : : }
2834 : :
2835 : : /* If BOUND is one, return an expression corresponding to
2836 : : (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2837 : 1209062 : if (fcode == BUILT_IN_STRNCMP && bound == 1)
2838 : : {
2839 : 110 : gimple_seq stmts = NULL;
2840 : 110 : tree temp1 = gimple_load_first_char (loc, str1, &stmts);
2841 : 110 : tree temp2 = gimple_load_first_char (loc, str2, &stmts);
2842 : :
2843 : 110 : if (lhs)
2844 : : {
2845 : 107 : tree c1 = make_ssa_name (integer_type_node);
2846 : 107 : gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
2847 : 107 : gimple_seq_add_stmt_without_update (&stmts, convert1);
2848 : :
2849 : 107 : tree c2 = make_ssa_name (integer_type_node);
2850 : 107 : gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
2851 : 107 : gimple_seq_add_stmt_without_update (&stmts, convert2);
2852 : :
2853 : 107 : stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
2854 : 107 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2855 : : }
2856 : :
2857 : 110 : gsi_replace_with_seq_vops (gsi, stmts);
2858 : 110 : return true;
2859 : : }
2860 : :
2861 : : /* If BOUND is greater than the length of one constant string,
2862 : : and the other argument is also a nul-terminated string, replace
2863 : : strncmp with strcmp. */
2864 : 1208952 : if (fcode == BUILT_IN_STRNCMP
2865 : 17605 : && bound > 0 && bound < HOST_WIDE_INT_M1U
2866 : 12017 : && ((p2 && len2 < bound && len2 == nulpos2)
2867 : 11787 : || (p1 && len1 < bound && len1 == nulpos1)))
2868 : : {
2869 : 1208952 : tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
2870 : 308 : if (!fn)
2871 : : return false;
2872 : 308 : gimple *repl = gimple_build_call (fn, 2, str1, str2);
2873 : 308 : replace_call_with_call_and_fold (gsi, repl);
2874 : 308 : return true;
2875 : : }
2876 : :
2877 : : return false;
2878 : : }
2879 : :
2880 : : /* Fold a call to the memchr pointed by GSI iterator. */
2881 : :
2882 : : static bool
2883 : 29142 : gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2884 : : {
2885 : 29142 : gimple *stmt = gsi_stmt (*gsi);
2886 : 29142 : tree lhs = gimple_call_lhs (stmt);
2887 : 29142 : tree arg1 = gimple_call_arg (stmt, 0);
2888 : 29142 : tree arg2 = gimple_call_arg (stmt, 1);
2889 : 29142 : tree len = gimple_call_arg (stmt, 2);
2890 : :
2891 : : /* If the LEN parameter is zero, return zero. */
2892 : 29142 : if (integer_zerop (len))
2893 : : {
2894 : 1 : replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2895 : 1 : return true;
2896 : : }
2897 : :
2898 : 29141 : char c;
2899 : 29141 : if (TREE_CODE (arg2) != INTEGER_CST
2900 : 15861 : || !tree_fits_uhwi_p (len)
2901 : 29834 : || !target_char_cst_p (arg2, &c))
2902 : 28448 : return false;
2903 : :
2904 : 693 : unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
2905 : 693 : unsigned HOST_WIDE_INT string_length;
2906 : 693 : const char *p1 = getbyterep (arg1, &string_length);
2907 : :
2908 : 693 : if (p1)
2909 : : {
2910 : 106 : const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
2911 : 106 : if (r == NULL)
2912 : : {
2913 : 15 : tree mem_size, offset_node;
2914 : 15 : byte_representation (arg1, &offset_node, &mem_size, NULL);
2915 : 15 : unsigned HOST_WIDE_INT offset = (offset_node == NULL_TREE)
2916 : 15 : ? 0 : tree_to_uhwi (offset_node);
2917 : : /* MEM_SIZE is the size of the array the string literal
2918 : : is stored in. */
2919 : 15 : unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size) - offset;
2920 : 15 : gcc_checking_assert (string_length <= string_size);
2921 : 15 : if (length <= string_size)
2922 : : {
2923 : 5 : replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2924 : 5 : return true;
2925 : : }
2926 : : }
2927 : : else
2928 : : {
2929 : 91 : unsigned HOST_WIDE_INT offset = r - p1;
2930 : 91 : gimple_seq stmts = NULL;
2931 : 91 : if (lhs != NULL_TREE)
2932 : : {
2933 : 89 : tree offset_cst = build_int_cst (sizetype, offset);
2934 : 89 : gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
2935 : : arg1, offset_cst);
2936 : 89 : gimple_seq_add_stmt_without_update (&stmts, stmt);
2937 : : }
2938 : : else
2939 : 2 : gimple_seq_add_stmt_without_update (&stmts,
2940 : : gimple_build_nop ());
2941 : :
2942 : 91 : gsi_replace_with_seq_vops (gsi, stmts);
2943 : 91 : return true;
2944 : : }
2945 : : }
2946 : :
2947 : : return false;
2948 : : }
2949 : :
2950 : : /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2951 : : to the call. IGNORE is true if the value returned
2952 : : by the builtin will be ignored. UNLOCKED is true is true if this
2953 : : actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2954 : : the known length of the string. Return NULL_TREE if no simplification
2955 : : was possible. */
2956 : :
2957 : : static bool
2958 : 20617 : gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
2959 : : tree arg0, tree arg1,
2960 : : bool unlocked)
2961 : : {
2962 : 20617 : gimple *stmt = gsi_stmt (*gsi);
2963 : :
2964 : : /* If we're using an unlocked function, assume the other unlocked
2965 : : functions exist explicitly. */
2966 : 20617 : tree const fn_fputc = (unlocked
2967 : 20617 : ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2968 : 20574 : : builtin_decl_implicit (BUILT_IN_FPUTC));
2969 : 20574 : tree const fn_fwrite = (unlocked
2970 : 43 : ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2971 : 20617 : : builtin_decl_implicit (BUILT_IN_FWRITE));
2972 : :
2973 : : /* If the return value is used, don't do the transformation. */
2974 : 20617 : if (gimple_call_lhs (stmt))
2975 : : return false;
2976 : :
2977 : : /* Get the length of the string passed to fputs. If the length
2978 : : can't be determined, punt. */
2979 : 20546 : tree len = get_maxval_strlen (arg0, SRK_STRLEN);
2980 : 20546 : if (!len || TREE_CODE (len) != INTEGER_CST)
2981 : : return false;
2982 : :
2983 : 16142 : switch (compare_tree_int (len, 1))
2984 : : {
2985 : 91 : case -1: /* length is 0, delete the call entirely . */
2986 : 91 : replace_call_with_value (gsi, integer_zero_node);
2987 : 91 : return true;
2988 : :
2989 : 1044 : case 0: /* length is 1, call fputc. */
2990 : 1044 : {
2991 : 1044 : const char *p = c_getstr (arg0);
2992 : 1044 : if (p != NULL)
2993 : : {
2994 : 2060 : if (!fn_fputc || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
2995 : : return false;
2996 : :
2997 : 1030 : gimple *repl
2998 : 1030 : = gimple_build_call (fn_fputc, 2,
2999 : 1030 : build_int_cst (integer_type_node, p[0]),
3000 : : arg1);
3001 : 1030 : replace_call_with_call_and_fold (gsi, repl);
3002 : 1030 : return true;
3003 : : }
3004 : : }
3005 : : /* FALLTHROUGH */
3006 : 15021 : case 1: /* length is greater than 1, call fwrite. */
3007 : 15021 : {
3008 : : /* If optimizing for size keep fputs. */
3009 : 15021 : if (optimize_function_for_size_p (cfun))
3010 : : return false;
3011 : : /* New argument list transforming fputs(string, stream) to
3012 : : fwrite(string, 1, len, stream). */
3013 : 27674 : if (!fn_fwrite || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3014 : : return false;
3015 : :
3016 : 8178 : gimple *repl
3017 : 8178 : = gimple_build_call (fn_fwrite, 4, arg0, size_one_node,
3018 : : fold_convert (size_type_node, len), arg1);
3019 : 8178 : replace_call_with_call_and_fold (gsi, repl);
3020 : 8178 : return true;
3021 : : }
3022 : 0 : default:
3023 : 0 : gcc_unreachable ();
3024 : : }
3025 : : }
3026 : :
3027 : : /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
3028 : : DEST, SRC, LEN, and SIZE are the arguments to the call.
3029 : : IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
3030 : : code of the builtin. If MAXLEN is not NULL, it is maximum length
3031 : : passed as third argument. */
3032 : :
3033 : : static bool
3034 : 25598 : gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
3035 : : tree dest, tree src, tree len, tree size,
3036 : : enum built_in_function fcode)
3037 : : {
3038 : 25598 : gimple *stmt = gsi_stmt (*gsi);
3039 : 25598 : location_t loc = gimple_location (stmt);
3040 : 25598 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3041 : 25598 : tree fn;
3042 : :
3043 : : /* If SRC and DEST are the same (and not volatile), return DEST
3044 : : (resp. DEST+LEN for __mempcpy_chk). */
3045 : 25598 : if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
3046 : : {
3047 : 13 : if (fcode != BUILT_IN_MEMPCPY_CHK)
3048 : : {
3049 : 7 : replace_call_with_value (gsi, dest);
3050 : 7 : return true;
3051 : : }
3052 : : else
3053 : : {
3054 : 6 : gimple_seq stmts = NULL;
3055 : 6 : len = gimple_convert_to_ptrofftype (&stmts, loc, len);
3056 : 6 : tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
3057 : 6 : TREE_TYPE (dest), dest, len);
3058 : 6 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3059 : 6 : replace_call_with_value (gsi, temp);
3060 : 6 : return true;
3061 : : }
3062 : : }
3063 : :
3064 : 68201 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3065 : : return false;
3066 : :
3067 : 25585 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3068 : 25585 : if (! integer_all_onesp (size)
3069 : 24519 : && !known_lower (stmt, len, size)
3070 : 42733 : && !known_lower (stmt, maxlen, size))
3071 : : {
3072 : : /* MAXLEN and LEN both cannot be proved to be less than SIZE, at
3073 : : least try to optimize (void) __mempcpy_chk () into
3074 : : (void) __memcpy_chk () */
3075 : 17071 : if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
3076 : : {
3077 : 40 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3078 : 40 : if (!fn)
3079 : : return false;
3080 : :
3081 : 40 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3082 : 40 : replace_call_with_call_and_fold (gsi, repl);
3083 : 40 : return true;
3084 : : }
3085 : : return false;
3086 : : }
3087 : :
3088 : 8514 : fn = NULL_TREE;
3089 : : /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
3090 : : mem{cpy,pcpy,move,set} is available. */
3091 : 8514 : switch (fcode)
3092 : : {
3093 : 1765 : case BUILT_IN_MEMCPY_CHK:
3094 : 1765 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
3095 : 1765 : break;
3096 : 1071 : case BUILT_IN_MEMPCPY_CHK:
3097 : 1071 : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
3098 : 1071 : break;
3099 : 1657 : case BUILT_IN_MEMMOVE_CHK:
3100 : 1657 : fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
3101 : 1657 : break;
3102 : 4021 : case BUILT_IN_MEMSET_CHK:
3103 : 4021 : fn = builtin_decl_explicit (BUILT_IN_MEMSET);
3104 : 4021 : break;
3105 : : default:
3106 : : break;
3107 : : }
3108 : :
3109 : 8514 : if (!fn)
3110 : : return false;
3111 : :
3112 : 8514 : gimple *repl = gimple_build_call (fn, 3, dest, src, len);
3113 : 8514 : replace_call_with_call_and_fold (gsi, repl);
3114 : 8514 : return true;
3115 : : }
3116 : :
3117 : : /* Fold a call to the __st[rp]cpy_chk builtin.
3118 : : DEST, SRC, and SIZE are the arguments to the call.
3119 : : IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
3120 : : code of the builtin. If MAXLEN is not NULL, it is maximum length of
3121 : : strings passed as second argument. */
3122 : :
3123 : : static bool
3124 : 2595 : gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
3125 : : tree dest,
3126 : : tree src, tree size,
3127 : : enum built_in_function fcode)
3128 : : {
3129 : 2595 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3130 : 2595 : location_t loc = gimple_location (stmt);
3131 : 2595 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3132 : 2595 : tree len, fn;
3133 : :
3134 : : /* If SRC and DEST are the same (and not volatile), return DEST. */
3135 : 2595 : if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
3136 : : {
3137 : : /* Issue -Wrestrict unless the pointers are null (those do
3138 : : not point to objects and so do not indicate an overlap;
3139 : : such calls could be the result of sanitization and jump
3140 : : threading). */
3141 : 0 : if (!integer_zerop (dest)
3142 : 0 : && !warning_suppressed_p (stmt, OPT_Wrestrict))
3143 : : {
3144 : 0 : tree func = gimple_call_fndecl (stmt);
3145 : :
3146 : 0 : warning_at (loc, OPT_Wrestrict,
3147 : : "%qD source argument is the same as destination",
3148 : : func);
3149 : : }
3150 : :
3151 : 0 : replace_call_with_value (gsi, dest);
3152 : 0 : return true;
3153 : : }
3154 : :
3155 : 5190 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3156 : : return false;
3157 : :
3158 : 2595 : tree maxlen = get_maxval_strlen (src, SRK_STRLENMAX);
3159 : 2595 : if (! integer_all_onesp (size))
3160 : : {
3161 : 2526 : len = c_strlen (src, 1);
3162 : 2526 : if (!known_lower (stmt, len, size, true)
3163 : 2526 : && !known_lower (stmt, maxlen, size, true))
3164 : : {
3165 : 2190 : if (fcode == BUILT_IN_STPCPY_CHK)
3166 : : {
3167 : 1097 : if (! ignore)
3168 : : return false;
3169 : :
3170 : : /* If return value of __stpcpy_chk is ignored,
3171 : : optimize into __strcpy_chk. */
3172 : 37 : fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
3173 : 37 : if (!fn)
3174 : : return false;
3175 : :
3176 : 37 : gimple *repl = gimple_build_call (fn, 3, dest, src, size);
3177 : 37 : replace_call_with_call_and_fold (gsi, repl);
3178 : 37 : return true;
3179 : : }
3180 : :
3181 : 1093 : if (! len || TREE_SIDE_EFFECTS (len))
3182 : : return false;
3183 : :
3184 : : /* If c_strlen returned something, but not provably less than size,
3185 : : transform __strcpy_chk into __memcpy_chk. */
3186 : 106 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3187 : 106 : if (!fn)
3188 : : return false;
3189 : :
3190 : 106 : gimple_seq stmts = NULL;
3191 : 106 : len = force_gimple_operand (len, &stmts, true, NULL_TREE);
3192 : 106 : len = gimple_convert (&stmts, loc, size_type_node, len);
3193 : 106 : len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
3194 : : build_int_cst (size_type_node, 1));
3195 : 106 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3196 : 106 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3197 : 106 : replace_call_with_call_and_fold (gsi, repl);
3198 : 106 : return true;
3199 : : }
3200 : : }
3201 : :
3202 : : /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
3203 : 662 : fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK && !ignore
3204 : : ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
3205 : 405 : if (!fn)
3206 : : return false;
3207 : :
3208 : 405 : gcall *repl = gimple_build_call (fn, 2, dest, src);
3209 : 405 : replace_call_with_call_and_fold (gsi, repl);
3210 : 405 : return true;
3211 : : }
3212 : :
3213 : : /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
3214 : : are the arguments to the call. If MAXLEN is not NULL, it is maximum
3215 : : length passed as third argument. IGNORE is true if return value can be
3216 : : ignored. FCODE is the BUILT_IN_* code of the builtin. */
3217 : :
3218 : : static bool
3219 : 2724 : gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
3220 : : tree dest, tree src,
3221 : : tree len, tree size,
3222 : : enum built_in_function fcode)
3223 : : {
3224 : 2724 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3225 : 2724 : bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
3226 : 2724 : tree fn;
3227 : :
3228 : 2724 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3229 : 2724 : if (! integer_all_onesp (size)
3230 : 2724 : && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
3231 : : {
3232 : 2267 : if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
3233 : : {
3234 : : /* If return value of __stpncpy_chk is ignored,
3235 : : optimize into __strncpy_chk. */
3236 : 39 : fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
3237 : 39 : if (fn)
3238 : : {
3239 : 39 : gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
3240 : 39 : replace_call_with_call_and_fold (gsi, repl);
3241 : 39 : return true;
3242 : : }
3243 : : }
3244 : : return false;
3245 : : }
3246 : :
3247 : : /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
3248 : 717 : fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK && !ignore
3249 : : ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
3250 : 3142 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3251 : : return false;
3252 : :
3253 : 457 : gcall *repl = gimple_build_call (fn, 3, dest, src, len);
3254 : 457 : replace_call_with_call_and_fold (gsi, repl);
3255 : 457 : return true;
3256 : : }
3257 : :
3258 : : /* Fold function call to builtin stpcpy with arguments DEST and SRC.
3259 : : Return NULL_TREE if no simplification can be made. */
3260 : :
3261 : : static bool
3262 : 3683 : gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
3263 : : {
3264 : 3683 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3265 : 3683 : location_t loc = gimple_location (stmt);
3266 : 3683 : tree dest = gimple_call_arg (stmt, 0);
3267 : 3683 : tree src = gimple_call_arg (stmt, 1);
3268 : 3683 : tree fn, lenp1;
3269 : :
3270 : 7366 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3271 : : return false;
3272 : :
3273 : : /* If the result is unused, replace stpcpy with strcpy. */
3274 : 3683 : if (gimple_call_lhs (stmt) == NULL_TREE)
3275 : : {
3276 : 30 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3277 : 30 : if (!fn)
3278 : : return false;
3279 : 30 : gimple_call_set_fndecl (stmt, fn);
3280 : 30 : fold_stmt (gsi);
3281 : 30 : return true;
3282 : : }
3283 : :
3284 : : /* Set to non-null if ARG refers to an unterminated array. */
3285 : 3653 : c_strlen_data data = { };
3286 : : /* The size of the unterminated array if SRC referes to one. */
3287 : 3653 : tree size;
3288 : : /* True if the size is exact/constant, false if it's the lower bound
3289 : : of a range. */
3290 : 3653 : bool exact;
3291 : 3653 : tree len = c_strlen (src, 1, &data, 1);
3292 : 3653 : if (!len
3293 : 706 : || TREE_CODE (len) != INTEGER_CST)
3294 : : {
3295 : 3179 : data.decl = unterminated_array (src, &size, &exact);
3296 : 3179 : if (!data.decl)
3297 : : return false;
3298 : : }
3299 : :
3300 : 1079 : if (data.decl)
3301 : : {
3302 : : /* Avoid folding calls with unterminated arrays. */
3303 : 605 : if (!warning_suppressed_p (stmt, OPT_Wstringop_overread))
3304 : 75 : warn_string_no_nul (loc, stmt, "stpcpy", src, data.decl, size,
3305 : : exact);
3306 : 605 : suppress_warning (stmt, OPT_Wstringop_overread);
3307 : 605 : return false;
3308 : : }
3309 : :
3310 : 474 : if (optimize_function_for_size_p (cfun)
3311 : : /* If length is zero it's small enough. */
3312 : 474 : && !integer_zerop (len))
3313 : : return false;
3314 : :
3315 : : /* If the source has a known length replace stpcpy with memcpy. */
3316 : 3653 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3317 : 290 : if (!fn)
3318 : : return false;
3319 : :
3320 : 290 : gimple_seq stmts = NULL;
3321 : 290 : tree tem = gimple_convert (&stmts, loc, size_type_node, len);
3322 : 290 : lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
3323 : : tem, build_int_cst (size_type_node, 1));
3324 : 290 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3325 : 290 : gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
3326 : 290 : gimple_move_vops (repl, stmt);
3327 : 290 : gsi_insert_before (gsi, repl, GSI_SAME_STMT);
3328 : : /* Replace the result with dest + len. */
3329 : 290 : stmts = NULL;
3330 : 290 : tem = gimple_convert (&stmts, loc, sizetype, len);
3331 : 290 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
3332 : 290 : gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
3333 : : POINTER_PLUS_EXPR, dest, tem);
3334 : 290 : gsi_replace (gsi, ret, false);
3335 : : /* Finally fold the memcpy call. */
3336 : 290 : gimple_stmt_iterator gsi2 = *gsi;
3337 : 290 : gsi_prev (&gsi2);
3338 : 290 : fold_stmt (&gsi2);
3339 : 290 : return true;
3340 : : }
3341 : :
3342 : : /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
3343 : : NULL_TREE if a normal call should be emitted rather than expanding
3344 : : the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
3345 : : BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
3346 : : passed as second argument. */
3347 : :
3348 : : static bool
3349 : 2677 : gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
3350 : : enum built_in_function fcode)
3351 : : {
3352 : 2677 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3353 : 2677 : tree dest, size, len, fn, fmt, flag;
3354 : 2677 : const char *fmt_str;
3355 : :
3356 : : /* Verify the required arguments in the original call. */
3357 : 2677 : if (gimple_call_num_args (stmt) < 5)
3358 : : return false;
3359 : :
3360 : 2677 : dest = gimple_call_arg (stmt, 0);
3361 : 2677 : len = gimple_call_arg (stmt, 1);
3362 : 2677 : flag = gimple_call_arg (stmt, 2);
3363 : 2677 : size = gimple_call_arg (stmt, 3);
3364 : 2677 : fmt = gimple_call_arg (stmt, 4);
3365 : :
3366 : 2677 : tree maxlen = get_maxval_strlen (len, SRK_INT_VALUE);
3367 : 2677 : if (! integer_all_onesp (size)
3368 : 2677 : && !known_lower (stmt, len, size) && !known_lower (stmt, maxlen, size))
3369 : : return false;
3370 : :
3371 : 308 : if (!init_target_chars ())
3372 : : return false;
3373 : :
3374 : : /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
3375 : : or if format doesn't contain % chars or is "%s". */
3376 : 308 : if (! integer_zerop (flag))
3377 : : {
3378 : 52 : fmt_str = c_getstr (fmt);
3379 : 52 : if (fmt_str == NULL)
3380 : : return false;
3381 : 52 : if (strchr (fmt_str, target_percent) != NULL
3382 : 51 : && strcmp (fmt_str, target_percent_s))
3383 : : return false;
3384 : : }
3385 : :
3386 : : /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
3387 : : available. */
3388 : 415 : fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
3389 : : ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
3390 : 2936 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3391 : : return false;
3392 : :
3393 : : /* Replace the called function and the first 5 argument by 3 retaining
3394 : : trailing varargs. */
3395 : 259 : gimple_call_set_fndecl (stmt, fn);
3396 : 259 : gimple_call_set_fntype (stmt, TREE_TYPE (fn));
3397 : 259 : gimple_call_set_arg (stmt, 0, dest);
3398 : 259 : gimple_call_set_arg (stmt, 1, len);
3399 : 259 : gimple_call_set_arg (stmt, 2, fmt);
3400 : 546 : for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
3401 : 287 : gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
3402 : 259 : gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
3403 : 259 : fold_stmt (gsi);
3404 : 259 : return true;
3405 : : }
3406 : :
3407 : : /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
3408 : : Return NULL_TREE if a normal call should be emitted rather than
3409 : : expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
3410 : : or BUILT_IN_VSPRINTF_CHK. */
3411 : :
3412 : : static bool
3413 : 4736 : gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
3414 : : enum built_in_function fcode)
3415 : : {
3416 : 4736 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3417 : 4736 : tree dest, size, len, fn, fmt, flag;
3418 : 4736 : const char *fmt_str;
3419 : 4736 : unsigned nargs = gimple_call_num_args (stmt);
3420 : :
3421 : : /* Verify the required arguments in the original call. */
3422 : 4736 : if (nargs < 4)
3423 : : return false;
3424 : 4736 : dest = gimple_call_arg (stmt, 0);
3425 : 4736 : flag = gimple_call_arg (stmt, 1);
3426 : 4736 : size = gimple_call_arg (stmt, 2);
3427 : 4736 : fmt = gimple_call_arg (stmt, 3);
3428 : :
3429 : 4736 : len = NULL_TREE;
3430 : :
3431 : 4736 : if (!init_target_chars ())
3432 : : return false;
3433 : :
3434 : : /* Check whether the format is a literal string constant. */
3435 : 4736 : fmt_str = c_getstr (fmt);
3436 : 4736 : if (fmt_str != NULL)
3437 : : {
3438 : : /* If the format doesn't contain % args or %%, we know the size. */
3439 : 4346 : if (strchr (fmt_str, target_percent) == 0)
3440 : : {
3441 : 304 : if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
3442 : 304 : len = build_int_cstu (size_type_node, strlen (fmt_str));
3443 : : }
3444 : : /* If the format is "%s" and first ... argument is a string literal,
3445 : : we know the size too. */
3446 : 4042 : else if (fcode == BUILT_IN_SPRINTF_CHK
3447 : 3174 : && strcmp (fmt_str, target_percent_s) == 0)
3448 : : {
3449 : 448 : tree arg;
3450 : :
3451 : 448 : if (nargs == 5)
3452 : : {
3453 : 448 : arg = gimple_call_arg (stmt, 4);
3454 : 448 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
3455 : 416 : len = c_strlen (arg, 1);
3456 : : }
3457 : : }
3458 : : }
3459 : :
3460 : 4736 : if (! integer_all_onesp (size) && !known_lower (stmt, len, size, true))
3461 : : return false;
3462 : :
3463 : : /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
3464 : : or if format doesn't contain % chars or is "%s". */
3465 : 202 : if (! integer_zerop (flag))
3466 : : {
3467 : 1 : if (fmt_str == NULL)
3468 : : return false;
3469 : 1 : if (strchr (fmt_str, target_percent) != NULL
3470 : 0 : && strcmp (fmt_str, target_percent_s))
3471 : : return false;
3472 : : }
3473 : :
3474 : : /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
3475 : 347 : fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
3476 : : ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
3477 : 4938 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3478 : : return false;
3479 : :
3480 : : /* Replace the called function and the first 4 argument by 2 retaining
3481 : : trailing varargs. */
3482 : 202 : gimple_call_set_fndecl (stmt, fn);
3483 : 202 : gimple_call_set_fntype (stmt, TREE_TYPE (fn));
3484 : 202 : gimple_call_set_arg (stmt, 0, dest);
3485 : 202 : gimple_call_set_arg (stmt, 1, fmt);
3486 : 400 : for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
3487 : 198 : gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
3488 : 202 : gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
3489 : 202 : fold_stmt (gsi);
3490 : 202 : return true;
3491 : : }
3492 : :
3493 : : /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
3494 : : ORIG may be null if this is a 2-argument call. We don't attempt to
3495 : : simplify calls with more than 3 arguments.
3496 : :
3497 : : Return true if simplification was possible, otherwise false. */
3498 : :
3499 : : bool
3500 : 2263 : gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
3501 : : {
3502 : 2263 : gimple *stmt = gsi_stmt (*gsi);
3503 : :
3504 : : /* Verify the required arguments in the original call. We deal with two
3505 : : types of sprintf() calls: 'sprintf (str, fmt)' and
3506 : : 'sprintf (dest, "%s", orig)'. */
3507 : 2263 : if (gimple_call_num_args (stmt) > 3)
3508 : : return false;
3509 : :
3510 : 1862 : tree orig = NULL_TREE;
3511 : 1862 : if (gimple_call_num_args (stmt) == 3)
3512 : 1766 : orig = gimple_call_arg (stmt, 2);
3513 : :
3514 : : /* Check whether the format is a literal string constant. */
3515 : 1862 : tree fmt = gimple_call_arg (stmt, 1);
3516 : 1862 : const char *fmt_str = c_getstr (fmt);
3517 : 1862 : if (fmt_str == NULL)
3518 : : return false;
3519 : :
3520 : 1862 : tree dest = gimple_call_arg (stmt, 0);
3521 : :
3522 : 1862 : if (!init_target_chars ())
3523 : : return false;
3524 : :
3525 : 1862 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3526 : 5133 : if (!fn || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3527 : : return false;
3528 : :
3529 : : /* If the format doesn't contain % args or %%, use strcpy. */
3530 : 1862 : if (strchr (fmt_str, target_percent) == NULL)
3531 : : {
3532 : : /* Don't optimize sprintf (buf, "abc", ptr++). */
3533 : 108 : if (orig)
3534 : : return false;
3535 : :
3536 : : /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3537 : : 'format' is known to contain no % formats. */
3538 : 95 : gimple_seq stmts = NULL;
3539 : 95 : gimple *repl = gimple_build_call (fn, 2, dest, fmt);
3540 : :
3541 : : /* Propagate the NO_WARNING bit to avoid issuing the same
3542 : : warning more than once. */
3543 : 95 : copy_warning (repl, stmt);
3544 : :
3545 : 95 : gimple_seq_add_stmt_without_update (&stmts, repl);
3546 : 95 : if (tree lhs = gimple_call_lhs (stmt))
3547 : : {
3548 : 0 : repl = gimple_build_assign (lhs, build_int_cst (TREE_TYPE (lhs),
3549 : 0 : strlen (fmt_str)));
3550 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3551 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3552 : : /* gsi now points at the assignment to the lhs, get a
3553 : : stmt iterator to the memcpy call.
3554 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3555 : : CFG isn't built yet. */
3556 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3557 : 0 : gsi_prev (&gsi2);
3558 : 0 : fold_stmt (&gsi2);
3559 : : }
3560 : : else
3561 : : {
3562 : 95 : gsi_replace_with_seq_vops (gsi, stmts);
3563 : 95 : fold_stmt (gsi);
3564 : : }
3565 : 95 : return true;
3566 : : }
3567 : :
3568 : : /* If the format is "%s", use strcpy if the result isn't used. */
3569 : 1754 : else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3570 : : {
3571 : : /* Don't crash on sprintf (str1, "%s"). */
3572 : 746 : if (!orig)
3573 : : return false;
3574 : :
3575 : : /* Don't fold calls with source arguments of invalid (nonpointer)
3576 : : types. */
3577 : 745 : if (!POINTER_TYPE_P (TREE_TYPE (orig)))
3578 : : return false;
3579 : :
3580 : 739 : tree orig_len = NULL_TREE;
3581 : 739 : if (gimple_call_lhs (stmt))
3582 : : {
3583 : 17 : orig_len = get_maxval_strlen (orig, SRK_STRLEN);
3584 : 17 : if (!orig_len)
3585 : : return false;
3586 : : }
3587 : :
3588 : : /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3589 : 722 : gimple_seq stmts = NULL;
3590 : 722 : gimple *repl = gimple_build_call (fn, 2, dest, orig);
3591 : :
3592 : : /* Propagate the NO_WARNING bit to avoid issuing the same
3593 : : warning more than once. */
3594 : 722 : copy_warning (repl, stmt);
3595 : :
3596 : 722 : gimple_seq_add_stmt_without_update (&stmts, repl);
3597 : 722 : if (tree lhs = gimple_call_lhs (stmt))
3598 : : {
3599 : 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
3600 : 0 : TREE_TYPE (orig_len)))
3601 : 0 : orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
3602 : 0 : repl = gimple_build_assign (lhs, orig_len);
3603 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3604 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3605 : : /* gsi now points at the assignment to the lhs, get a
3606 : : stmt iterator to the memcpy call.
3607 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3608 : : CFG isn't built yet. */
3609 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3610 : 0 : gsi_prev (&gsi2);
3611 : 0 : fold_stmt (&gsi2);
3612 : : }
3613 : : else
3614 : : {
3615 : 722 : gsi_replace_with_seq_vops (gsi, stmts);
3616 : 722 : fold_stmt (gsi);
3617 : : }
3618 : 722 : return true;
3619 : : }
3620 : : return false;
3621 : : }
3622 : :
3623 : : /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3624 : : FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3625 : : attempt to simplify calls with more than 4 arguments.
3626 : :
3627 : : Return true if simplification was possible, otherwise false. */
3628 : :
3629 : : bool
3630 : 1581 : gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
3631 : : {
3632 : 1581 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3633 : 1581 : tree dest = gimple_call_arg (stmt, 0);
3634 : 1581 : tree destsize = gimple_call_arg (stmt, 1);
3635 : 1581 : tree fmt = gimple_call_arg (stmt, 2);
3636 : 1581 : tree orig = NULL_TREE;
3637 : 1581 : const char *fmt_str = NULL;
3638 : :
3639 : 1581 : if (gimple_call_num_args (stmt) > 4
3640 : 2711 : || (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun)))
3641 : : return false;
3642 : :
3643 : 708 : if (gimple_call_num_args (stmt) == 4)
3644 : 589 : orig = gimple_call_arg (stmt, 3);
3645 : :
3646 : : /* Check whether the format is a literal string constant. */
3647 : 708 : fmt_str = c_getstr (fmt);
3648 : 708 : if (fmt_str == NULL)
3649 : : return false;
3650 : :
3651 : 708 : if (!init_target_chars ())
3652 : : return false;
3653 : :
3654 : : /* If the format doesn't contain % args or %%, use strcpy. */
3655 : 708 : if (strchr (fmt_str, target_percent) == NULL)
3656 : : {
3657 : 148 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3658 : 118 : if (!fn)
3659 : : return false;
3660 : :
3661 : : /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3662 : 118 : if (orig)
3663 : : return false;
3664 : :
3665 : 118 : tree len = build_int_cstu (TREE_TYPE (destsize), strlen (fmt_str));
3666 : :
3667 : : /* We could expand this as
3668 : : memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3669 : : or to
3670 : : memcpy (str, fmt_with_nul_at_cstm1, cst);
3671 : : but in the former case that might increase code size
3672 : : and in the latter case grow .rodata section too much.
3673 : : So punt for now. */
3674 : 118 : if (!known_lower (stmt, len, destsize, true))
3675 : : return false;
3676 : :
3677 : 88 : gimple_seq stmts = NULL;
3678 : 88 : gimple *repl = gimple_build_call (fn, 2, dest, fmt);
3679 : 88 : gimple_seq_add_stmt_without_update (&stmts, repl);
3680 : 88 : if (tree lhs = gimple_call_lhs (stmt))
3681 : : {
3682 : 0 : repl = gimple_build_assign (lhs,
3683 : 0 : fold_convert (TREE_TYPE (lhs), len));
3684 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3685 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3686 : : /* gsi now points at the assignment to the lhs, get a
3687 : : stmt iterator to the memcpy call.
3688 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3689 : : CFG isn't built yet. */
3690 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3691 : 0 : gsi_prev (&gsi2);
3692 : 0 : fold_stmt (&gsi2);
3693 : : }
3694 : : else
3695 : : {
3696 : 88 : gsi_replace_with_seq_vops (gsi, stmts);
3697 : 88 : fold_stmt (gsi);
3698 : : }
3699 : 88 : return true;
3700 : : }
3701 : :
3702 : : /* If the format is "%s", use strcpy if the result isn't used. */
3703 : 590 : else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3704 : : {
3705 : 292 : tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3706 : 174 : if (!fn)
3707 : : return false;
3708 : :
3709 : : /* Don't crash on snprintf (str1, cst, "%s"). */
3710 : 174 : if (!orig)
3711 : : return false;
3712 : :
3713 : 174 : tree orig_len = get_maxval_strlen (orig, SRK_STRLEN);
3714 : :
3715 : : /* We could expand this as
3716 : : memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3717 : : or to
3718 : : memcpy (str1, str2_with_nul_at_cstm1, cst);
3719 : : but in the former case that might increase code size
3720 : : and in the latter case grow .rodata section too much.
3721 : : So punt for now. */
3722 : 174 : if (!known_lower (stmt, orig_len, destsize, true))
3723 : : return false;
3724 : :
3725 : : /* Convert snprintf (str1, cst, "%s", str2) into
3726 : : strcpy (str1, str2) if strlen (str2) < cst. */
3727 : 56 : gimple_seq stmts = NULL;
3728 : 56 : gimple *repl = gimple_build_call (fn, 2, dest, orig);
3729 : 56 : gimple_seq_add_stmt_without_update (&stmts, repl);
3730 : 56 : if (tree lhs = gimple_call_lhs (stmt))
3731 : : {
3732 : 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
3733 : 0 : TREE_TYPE (orig_len)))
3734 : 0 : orig_len = fold_convert (TREE_TYPE (lhs), orig_len);
3735 : 0 : repl = gimple_build_assign (lhs, orig_len);
3736 : 0 : gimple_seq_add_stmt_without_update (&stmts, repl);
3737 : 0 : gsi_replace_with_seq_vops (gsi, stmts);
3738 : : /* gsi now points at the assignment to the lhs, get a
3739 : : stmt iterator to the memcpy call.
3740 : : ??? We can't use gsi_for_stmt as that doesn't work when the
3741 : : CFG isn't built yet. */
3742 : 0 : gimple_stmt_iterator gsi2 = *gsi;
3743 : 0 : gsi_prev (&gsi2);
3744 : 0 : fold_stmt (&gsi2);
3745 : : }
3746 : : else
3747 : : {
3748 : 56 : gsi_replace_with_seq_vops (gsi, stmts);
3749 : 56 : fold_stmt (gsi);
3750 : : }
3751 : 56 : return true;
3752 : : }
3753 : : return false;
3754 : : }
3755 : :
3756 : : /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3757 : : FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3758 : : more than 3 arguments, and ARG may be null in the 2-argument case.
3759 : :
3760 : : Return NULL_TREE if no simplification was possible, otherwise return the
3761 : : simplified form of the call as a tree. FCODE is the BUILT_IN_*
3762 : : code of the function to be simplified. */
3763 : :
3764 : : static bool
3765 : 54217 : gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
3766 : : tree fp, tree fmt, tree arg,
3767 : : enum built_in_function fcode)
3768 : : {
3769 : 54217 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3770 : 54217 : tree fn_fputc, fn_fputs;
3771 : 54217 : const char *fmt_str = NULL;
3772 : :
3773 : : /* If the return value is used, don't do the transformation. */
3774 : 54217 : if (gimple_call_lhs (stmt) != NULL_TREE)
3775 : : return false;
3776 : :
3777 : : /* Check whether the format is a literal string constant. */
3778 : 50049 : fmt_str = c_getstr (fmt);
3779 : 50049 : if (fmt_str == NULL)
3780 : : return false;
3781 : :
3782 : 49803 : if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
3783 : : {
3784 : : /* If we're using an unlocked function, assume the other
3785 : : unlocked functions exist explicitly. */
3786 : 80 : fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
3787 : 80 : fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
3788 : : }
3789 : : else
3790 : : {
3791 : 49723 : fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
3792 : 49723 : fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
3793 : : }
3794 : :
3795 : 49803 : if (!init_target_chars ())
3796 : : return false;
3797 : :
3798 : 143572 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3799 : : return false;
3800 : :
3801 : : /* If the format doesn't contain % args or %%, use strcpy. */
3802 : 49803 : if (strchr (fmt_str, target_percent) == NULL)
3803 : : {
3804 : 9563 : if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
3805 : 9493 : && arg)
3806 : : return false;
3807 : :
3808 : : /* If the format specifier was "", fprintf does nothing. */
3809 : 9563 : if (fmt_str[0] == '\0')
3810 : : {
3811 : 58 : replace_call_with_value (gsi, NULL_TREE);
3812 : 58 : return true;
3813 : : }
3814 : :
3815 : : /* When "string" doesn't contain %, replace all cases of
3816 : : fprintf (fp, string) with fputs (string, fp). The fputs
3817 : : builtin will take care of special cases like length == 1. */
3818 : 9505 : if (fn_fputs)
3819 : : {
3820 : 9505 : gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
3821 : 9505 : replace_call_with_call_and_fold (gsi, repl);
3822 : 9505 : return true;
3823 : : }
3824 : : }
3825 : :
3826 : : /* The other optimizations can be done only on the non-va_list variants. */
3827 : 40240 : else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
3828 : : return false;
3829 : :
3830 : : /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3831 : 39191 : else if (strcmp (fmt_str, target_percent_s) == 0)
3832 : : {
3833 : 639 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3834 : : return false;
3835 : 639 : if (fn_fputs)
3836 : : {
3837 : 639 : gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3838 : 639 : replace_call_with_call_and_fold (gsi, repl);
3839 : 639 : return true;
3840 : : }
3841 : : }
3842 : :
3843 : : /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3844 : 38552 : else if (strcmp (fmt_str, target_percent_c) == 0)
3845 : : {
3846 : 49 : if (!arg
3847 : 49 : || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
3848 : 0 : return false;
3849 : 49 : if (fn_fputc)
3850 : : {
3851 : 49 : gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
3852 : 49 : replace_call_with_call_and_fold (gsi, repl);
3853 : 49 : return true;
3854 : : }
3855 : : }
3856 : :
3857 : : return false;
3858 : : }
3859 : :
3860 : : /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3861 : : FMT and ARG are the arguments to the call; we don't fold cases with
3862 : : more than 2 arguments, and ARG may be null if this is a 1-argument case.
3863 : :
3864 : : Return NULL_TREE if no simplification was possible, otherwise return the
3865 : : simplified form of the call as a tree. FCODE is the BUILT_IN_*
3866 : : code of the function to be simplified. */
3867 : :
3868 : : static bool
3869 : 113359 : gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3870 : : tree arg, enum built_in_function fcode)
3871 : : {
3872 : 113359 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3873 : 113359 : tree fn_putchar, fn_puts, newarg;
3874 : 113359 : const char *fmt_str = NULL;
3875 : :
3876 : : /* If the return value is used, don't do the transformation. */
3877 : 113359 : if (gimple_call_lhs (stmt) != NULL_TREE)
3878 : : return false;
3879 : :
3880 : 328715 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
3881 : : return false;
3882 : :
3883 : : /* Check whether the format is a literal string constant. */
3884 : 110117 : fmt_str = c_getstr (fmt);
3885 : 110117 : if (fmt_str == NULL)
3886 : : return false;
3887 : :
3888 : 107056 : if (fcode == BUILT_IN_PRINTF_UNLOCKED)
3889 : : {
3890 : : /* If we're using an unlocked function, assume the other
3891 : : unlocked functions exist explicitly. */
3892 : 80 : fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
3893 : 80 : fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
3894 : : }
3895 : : else
3896 : : {
3897 : 106976 : fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3898 : 106976 : fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3899 : : }
3900 : :
3901 : 107056 : if (!init_target_chars ())
3902 : : return false;
3903 : :
3904 : 107056 : if (strcmp (fmt_str, target_percent_s) == 0
3905 : 100670 : || strchr (fmt_str, target_percent) == NULL)
3906 : : {
3907 : 14094 : const char *str;
3908 : :
3909 : 14094 : if (strcmp (fmt_str, target_percent_s) == 0)
3910 : : {
3911 : 6386 : if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3912 : : return false;
3913 : :
3914 : 6098 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3915 : : return false;
3916 : :
3917 : 6093 : str = c_getstr (arg);
3918 : 6093 : if (str == NULL)
3919 : : return false;
3920 : : }
3921 : : else
3922 : : {
3923 : : /* The format specifier doesn't contain any '%' characters. */
3924 : 7708 : if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3925 : 7576 : && arg)
3926 : : return false;
3927 : : str = fmt_str;
3928 : : }
3929 : :
3930 : : /* If the string was "", printf does nothing. */
3931 : 5744 : if (str[0] == '\0')
3932 : : {
3933 : 109 : replace_call_with_value (gsi, NULL_TREE);
3934 : 109 : return true;
3935 : : }
3936 : :
3937 : : /* If the string has length of 1, call putchar. */
3938 : 5635 : if (str[1] == '\0')
3939 : : {
3940 : : /* Given printf("c"), (where c is any one character,)
3941 : : convert "c"[0] to an int and pass that to the replacement
3942 : : function. */
3943 : 559 : newarg = build_int_cst (integer_type_node, str[0]);
3944 : 559 : if (fn_putchar)
3945 : : {
3946 : 559 : gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
3947 : 559 : replace_call_with_call_and_fold (gsi, repl);
3948 : 559 : return true;
3949 : : }
3950 : : }
3951 : : else
3952 : : {
3953 : : /* If the string was "string\n", call puts("string"). */
3954 : 5076 : size_t len = strlen (str);
3955 : 5076 : if ((unsigned char)str[len - 1] == target_newline
3956 : 3984 : && (size_t) (int) len == len
3957 : 3984 : && (int) len > 0)
3958 : : {
3959 : 3984 : char *newstr;
3960 : :
3961 : : /* Create a NUL-terminated string that's one char shorter
3962 : : than the original, stripping off the trailing '\n'. */
3963 : 3984 : newstr = xstrdup (str);
3964 : 3984 : newstr[len - 1] = '\0';
3965 : 3984 : newarg = build_string_literal (len, newstr);
3966 : 3984 : free (newstr);
3967 : 3984 : if (fn_puts)
3968 : : {
3969 : 3984 : gcall *repl = gimple_build_call (fn_puts, 1, newarg);
3970 : 3984 : replace_call_with_call_and_fold (gsi, repl);
3971 : 3984 : return true;
3972 : : }
3973 : : }
3974 : : else
3975 : : /* We'd like to arrange to call fputs(string,stdout) here,
3976 : : but we need stdout and don't have a way to get it yet. */
3977 : : return false;
3978 : : }
3979 : : }
3980 : :
3981 : : /* The other optimizations can be done only on the non-va_list variants. */
3982 : 92962 : else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3983 : : return false;
3984 : :
3985 : : /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3986 : 92712 : else if (strcmp (fmt_str, target_percent_s_newline) == 0)
3987 : : {
3988 : 179 : if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3989 : : return false;
3990 : 179 : if (fn_puts)
3991 : : {
3992 : 179 : gcall *repl = gimple_build_call (fn_puts, 1, arg);
3993 : 179 : replace_call_with_call_and_fold (gsi, repl);
3994 : 179 : return true;
3995 : : }
3996 : : }
3997 : :
3998 : : /* If the format specifier was "%c", call __builtin_putchar(arg). */
3999 : 92533 : else if (strcmp (fmt_str, target_percent_c) == 0)
4000 : : {
4001 : 94 : if (!arg || ! useless_type_conversion_p (integer_type_node,
4002 : 47 : TREE_TYPE (arg)))
4003 : 0 : return false;
4004 : 47 : if (fn_putchar)
4005 : : {
4006 : 47 : gcall *repl = gimple_build_call (fn_putchar, 1, arg);
4007 : 47 : replace_call_with_call_and_fold (gsi, repl);
4008 : 47 : return true;
4009 : : }
4010 : : }
4011 : :
4012 : : return false;
4013 : : }
4014 : :
4015 : :
4016 : :
4017 : : /* Fold a call to __builtin_strlen with known length LEN. */
4018 : :
4019 : : static bool
4020 : 140816 : gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
4021 : : {
4022 : 140816 : gimple *stmt = gsi_stmt (*gsi);
4023 : 140816 : tree arg = gimple_call_arg (stmt, 0);
4024 : :
4025 : 140816 : wide_int minlen;
4026 : 140816 : wide_int maxlen;
4027 : :
4028 : 140816 : c_strlen_data lendata = { };
4029 : 140816 : if (get_range_strlen (arg, &lendata, /* eltsize = */ 1)
4030 : 36728 : && !lendata.decl
4031 : 33747 : && lendata.minlen && TREE_CODE (lendata.minlen) == INTEGER_CST
4032 : 174458 : && lendata.maxlen && TREE_CODE (lendata.maxlen) == INTEGER_CST)
4033 : : {
4034 : : /* The range of lengths refers to either a single constant
4035 : : string or to the longest and shortest constant string
4036 : : referenced by the argument of the strlen() call, or to
4037 : : the strings that can possibly be stored in the arrays
4038 : : the argument refers to. */
4039 : 33642 : minlen = wi::to_wide (lendata.minlen);
4040 : 33642 : maxlen = wi::to_wide (lendata.maxlen);
4041 : : }
4042 : : else
4043 : : {
4044 : 107174 : unsigned prec = TYPE_PRECISION (sizetype);
4045 : :
4046 : 107174 : minlen = wi::shwi (0, prec);
4047 : 107174 : maxlen = wi::to_wide (max_object_size (), prec) - 2;
4048 : : }
4049 : :
4050 : : /* For -fsanitize=address, don't optimize the upper bound of the
4051 : : length to be able to diagnose UB on non-zero terminated arrays. */
4052 : 140816 : if (sanitize_flags_p (SANITIZE_ADDRESS))
4053 : 260 : maxlen = wi::max_value (TYPE_PRECISION (sizetype), UNSIGNED);
4054 : :
4055 : 140816 : if (minlen == maxlen)
4056 : : {
4057 : : /* Fold the strlen call to a constant. */
4058 : 3339 : tree type = TREE_TYPE (lendata.minlen);
4059 : 6678 : tree len = force_gimple_operand_gsi (gsi,
4060 : 3339 : wide_int_to_tree (type, minlen),
4061 : : true, NULL, true, GSI_SAME_STMT);
4062 : 3339 : replace_call_with_value (gsi, len);
4063 : 3339 : return true;
4064 : : }
4065 : :
4066 : : /* Set the strlen() range to [0, MAXLEN]. */
4067 : 137477 : if (tree lhs = gimple_call_lhs (stmt))
4068 : 137472 : set_strlen_range (lhs, minlen, maxlen);
4069 : :
4070 : : return false;
4071 : 140816 : }
4072 : :
4073 : : static bool
4074 : 210 : gimple_fold_builtin_omp_is_initial_device (gimple_stmt_iterator *gsi)
4075 : : {
4076 : : #if ACCEL_COMPILER
4077 : : replace_call_with_value (gsi, integer_zero_node);
4078 : : return true;
4079 : : #else
4080 : 210 : if (!ENABLE_OFFLOADING || symtab->state == EXPANSION)
4081 : : {
4082 : 0 : replace_call_with_value (gsi, integer_one_node);
4083 : 210 : return true;
4084 : : }
4085 : : #endif
4086 : : return false;
4087 : : }
4088 : :
4089 : : /* omp_get_initial_device was in OpenMP 5.0/5.1 explicitly and in
4090 : : 5.0 implicitly the same as omp_get_num_devices; since 6.0 it is
4091 : : unspecified whether -1 or omp_get_num_devices() is returned. For
4092 : : better backward compatibility, use omp_get_num_devices() on the
4093 : : host - and -1 on the device (where the result is unspecified). */
4094 : :
4095 : : static bool
4096 : 98 : gimple_fold_builtin_omp_get_initial_device (gimple_stmt_iterator *gsi)
4097 : : {
4098 : : #if ACCEL_COMPILER
4099 : : replace_call_with_value (gsi, build_int_cst (integer_type_node, -1));
4100 : : #else
4101 : 98 : if (!ENABLE_OFFLOADING)
4102 : 0 : replace_call_with_value (gsi, integer_zero_node);
4103 : : else
4104 : : {
4105 : : tree fn = builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
4106 : : gcall *repl = gimple_build_call (fn, 0);
4107 : : replace_call_with_call_and_fold (gsi, repl);
4108 : : }
4109 : : #endif
4110 : 98 : return true;
4111 : : }
4112 : :
4113 : : static bool
4114 : 265 : gimple_fold_builtin_omp_get_num_devices (gimple_stmt_iterator *gsi)
4115 : : {
4116 : 265 : if (!ENABLE_OFFLOADING)
4117 : : {
4118 : 0 : replace_call_with_value (gsi, integer_zero_node);
4119 : 265 : return true;
4120 : : }
4121 : : return false;
4122 : : }
4123 : :
4124 : : /* Fold a call to __builtin_acc_on_device. */
4125 : :
4126 : : static bool
4127 : 2866 : gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
4128 : : {
4129 : : /* Defer folding until we know which compiler we're in. */
4130 : 2866 : if (symtab->state != EXPANSION)
4131 : : return false;
4132 : :
4133 : 554 : unsigned val_host = GOMP_DEVICE_HOST;
4134 : 554 : unsigned val_dev = GOMP_DEVICE_NONE;
4135 : :
4136 : : #ifdef ACCEL_COMPILER
4137 : : val_host = GOMP_DEVICE_NOT_HOST;
4138 : : val_dev = ACCEL_COMPILER_acc_device;
4139 : : #endif
4140 : :
4141 : 554 : location_t loc = gimple_location (gsi_stmt (*gsi));
4142 : :
4143 : 554 : tree host_eq = make_ssa_name (boolean_type_node);
4144 : 554 : gimple *host_ass = gimple_build_assign
4145 : 554 : (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
4146 : 554 : gimple_set_location (host_ass, loc);
4147 : 554 : gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
4148 : :
4149 : 554 : tree dev_eq = make_ssa_name (boolean_type_node);
4150 : 554 : gimple *dev_ass = gimple_build_assign
4151 : 554 : (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
4152 : 554 : gimple_set_location (dev_ass, loc);
4153 : 554 : gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
4154 : :
4155 : 554 : tree result = make_ssa_name (boolean_type_node);
4156 : 554 : gimple *result_ass = gimple_build_assign
4157 : 554 : (result, BIT_IOR_EXPR, host_eq, dev_eq);
4158 : 554 : gimple_set_location (result_ass, loc);
4159 : 554 : gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
4160 : :
4161 : 554 : replace_call_with_value (gsi, result);
4162 : :
4163 : 554 : return true;
4164 : : }
4165 : :
4166 : : /* Fold realloc (0, n) -> malloc (n). */
4167 : :
4168 : : static bool
4169 : 44636 : gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
4170 : : {
4171 : 44636 : gimple *stmt = gsi_stmt (*gsi);
4172 : 44636 : tree arg = gimple_call_arg (stmt, 0);
4173 : 44636 : tree size = gimple_call_arg (stmt, 1);
4174 : :
4175 : 132638 : if (!gimple_vdef (stmt) && gimple_in_ssa_p (cfun))
4176 : : return false;
4177 : :
4178 : 44636 : if (operand_equal_p (arg, null_pointer_node, 0))
4179 : : {
4180 : 1270 : tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
4181 : 1270 : if (fn_malloc)
4182 : : {
4183 : 1270 : gcall *repl = gimple_build_call (fn_malloc, 1, size);
4184 : 1270 : replace_call_with_call_and_fold (gsi, repl);
4185 : 1270 : return true;
4186 : : }
4187 : : }
4188 : : return false;
4189 : : }
4190 : :
4191 : : /* Number of bytes into which any type but aggregate, vector or
4192 : : _BitInt types should fit. */
4193 : : static constexpr size_t clear_padding_unit
4194 : : = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT;
4195 : : /* Buffer size on which __builtin_clear_padding folding code works. */
4196 : : static const size_t clear_padding_buf_size = 32 * clear_padding_unit;
4197 : :
4198 : : /* Data passed through __builtin_clear_padding folding. */
4199 : : struct clear_padding_struct {
4200 : : location_t loc;
4201 : : /* 0 during __builtin_clear_padding folding, nonzero during
4202 : : clear_type_padding_in_mask. In that case, instead of clearing the
4203 : : non-padding bits in union_ptr array clear the padding bits in there. */
4204 : : bool clear_in_mask;
4205 : : tree base;
4206 : : tree alias_type;
4207 : : gimple_stmt_iterator *gsi;
4208 : : /* Alignment of buf->base + 0. */
4209 : : unsigned align;
4210 : : /* Offset from buf->base. Should be always a multiple of UNITS_PER_WORD. */
4211 : : HOST_WIDE_INT off;
4212 : : /* Number of padding bytes before buf->off that don't have padding clear
4213 : : code emitted yet. */
4214 : : HOST_WIDE_INT padding_bytes;
4215 : : /* The size of the whole object. Never emit code to touch
4216 : : buf->base + buf->sz or following bytes. */
4217 : : HOST_WIDE_INT sz;
4218 : : /* Number of bytes recorded in buf->buf. */
4219 : : size_t size;
4220 : : /* When inside union, instead of emitting code we and bits inside of
4221 : : the union_ptr array. */
4222 : : unsigned char *union_ptr;
4223 : : /* Set bits mean padding bits that need to be cleared by the builtin. */
4224 : : unsigned char buf[clear_padding_buf_size + clear_padding_unit];
4225 : : };
4226 : :
4227 : : /* Emit code to clear padding requested in BUF->buf - set bits
4228 : : in there stand for padding that should be cleared. FULL is true
4229 : : if everything from the buffer should be flushed, otherwise
4230 : : it can leave up to 2 * clear_padding_unit bytes for further
4231 : : processing. */
4232 : :
4233 : : static void
4234 : 31748 : clear_padding_flush (clear_padding_struct *buf, bool full)
4235 : : {
4236 : 31748 : gcc_assert ((clear_padding_unit % UNITS_PER_WORD) == 0);
4237 : 31748 : if (!full && buf->size < 2 * clear_padding_unit)
4238 : 31748 : return;
4239 : 32798 : gcc_assert ((buf->off % UNITS_PER_WORD) == 0);
4240 : 31706 : size_t end = buf->size;
4241 : 31706 : if (!full)
4242 : 42 : end = ((end - clear_padding_unit - 1) / clear_padding_unit
4243 : : * clear_padding_unit);
4244 : 31706 : size_t padding_bytes = buf->padding_bytes;
4245 : 31706 : if (buf->union_ptr)
4246 : : {
4247 : 30947 : if (buf->clear_in_mask)
4248 : : {
4249 : : /* During clear_type_padding_in_mask, clear the padding
4250 : : bits set in buf->buf in the buf->union_ptr mask. */
4251 : 210327 : for (size_t i = 0; i < end; i++)
4252 : : {
4253 : 179773 : if (buf->buf[i] == (unsigned char) ~0)
4254 : 6816 : padding_bytes++;
4255 : : else
4256 : : {
4257 : 172957 : memset (&buf->union_ptr[buf->off + i - padding_bytes],
4258 : : 0, padding_bytes);
4259 : 172957 : padding_bytes = 0;
4260 : 172957 : buf->union_ptr[buf->off + i] &= ~buf->buf[i];
4261 : : }
4262 : : }
4263 : 30554 : if (full)
4264 : : {
4265 : 30554 : memset (&buf->union_ptr[buf->off + end - padding_bytes],
4266 : : 0, padding_bytes);
4267 : 30554 : buf->off = 0;
4268 : 30554 : buf->size = 0;
4269 : 30554 : buf->padding_bytes = 0;
4270 : : }
4271 : : else
4272 : : {
4273 : 0 : memmove (buf->buf, buf->buf + end, buf->size - end);
4274 : 0 : buf->off += end;
4275 : 0 : buf->size -= end;
4276 : 0 : buf->padding_bytes = padding_bytes;
4277 : : }
4278 : 30554 : return;
4279 : : }
4280 : : /* Inside of a union, instead of emitting any code, instead
4281 : : clear all bits in the union_ptr buffer that are clear
4282 : : in buf. Whole padding bytes don't clear anything. */
4283 : 3017 : for (size_t i = 0; i < end; i++)
4284 : : {
4285 : 2624 : if (buf->buf[i] == (unsigned char) ~0)
4286 : 1424 : padding_bytes++;
4287 : : else
4288 : : {
4289 : 1200 : padding_bytes = 0;
4290 : 1200 : buf->union_ptr[buf->off + i] &= buf->buf[i];
4291 : : }
4292 : : }
4293 : 393 : if (full)
4294 : : {
4295 : 393 : buf->off = 0;
4296 : 393 : buf->size = 0;
4297 : 393 : buf->padding_bytes = 0;
4298 : : }
4299 : : else
4300 : : {
4301 : 0 : memmove (buf->buf, buf->buf + end, buf->size - end);
4302 : 0 : buf->off += end;
4303 : 0 : buf->size -= end;
4304 : 0 : buf->padding_bytes = padding_bytes;
4305 : : }
4306 : 393 : return;
4307 : : }
4308 : 759 : size_t wordsize = UNITS_PER_WORD;
4309 : 23482 : for (size_t i = 0; i < end; i += wordsize)
4310 : : {
4311 : 22723 : size_t nonzero_first = wordsize;
4312 : 22723 : size_t nonzero_last = 0;
4313 : 22723 : size_t zero_first = wordsize;
4314 : 22723 : size_t zero_last = 0;
4315 : 22723 : bool all_ones = true, bytes_only = true;
4316 : 23004 : if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
4317 : 22723 : > (unsigned HOST_WIDE_INT) buf->sz)
4318 : : {
4319 : 281 : gcc_assert (wordsize > 1);
4320 : 281 : wordsize /= 2;
4321 : 281 : i -= wordsize;
4322 : 281 : continue;
4323 : : }
4324 : 22442 : size_t endsize = end - i > wordsize ? wordsize : end - i;
4325 : 200753 : for (size_t j = i; j < i + endsize; j++)
4326 : : {
4327 : 178311 : if (buf->buf[j])
4328 : : {
4329 : 168362 : if (nonzero_first == wordsize)
4330 : : {
4331 : 21504 : nonzero_first = j - i;
4332 : 21504 : nonzero_last = j - i;
4333 : : }
4334 : 168362 : if (nonzero_last != j - i)
4335 : 158 : all_ones = false;
4336 : 168362 : nonzero_last = j + 1 - i;
4337 : : }
4338 : : else
4339 : : {
4340 : 9949 : if (zero_first == wordsize)
4341 : 1929 : zero_first = j - i;
4342 : 9949 : zero_last = j + 1 - i;
4343 : : }
4344 : 178311 : if (buf->buf[j] != 0 && buf->buf[j] != (unsigned char) ~0)
4345 : : {
4346 : 79 : all_ones = false;
4347 : 79 : bytes_only = false;
4348 : : }
4349 : : }
4350 : 22442 : size_t padding_end = i;
4351 : 22442 : if (padding_bytes)
4352 : : {
4353 : 20848 : if (nonzero_first == 0
4354 : 20848 : && nonzero_last == endsize
4355 : 20400 : && all_ones)
4356 : : {
4357 : : /* All bits are padding and we had some padding
4358 : : before too. Just extend it. */
4359 : 20400 : padding_bytes += endsize;
4360 : 20400 : continue;
4361 : : }
4362 : 448 : if (all_ones && nonzero_first == 0)
4363 : : {
4364 : 4 : padding_bytes += nonzero_last;
4365 : 4 : padding_end += nonzero_last;
4366 : 4 : nonzero_first = wordsize;
4367 : 4 : nonzero_last = 0;
4368 : : }
4369 : 444 : else if (bytes_only && nonzero_first == 0)
4370 : : {
4371 : 0 : gcc_assert (zero_first && zero_first != wordsize);
4372 : 0 : padding_bytes += zero_first;
4373 : 0 : padding_end += zero_first;
4374 : : }
4375 : 448 : tree atype, src;
4376 : 448 : if (padding_bytes == 1)
4377 : : {
4378 : 33 : atype = char_type_node;
4379 : 33 : src = build_zero_cst (char_type_node);
4380 : : }
4381 : : else
4382 : : {
4383 : 415 : atype = build_array_type_nelts (char_type_node, padding_bytes);
4384 : 415 : src = build_constructor (atype, NULL);
4385 : : }
4386 : 448 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4387 : : build_int_cst (buf->alias_type,
4388 : 448 : buf->off + padding_end
4389 : 448 : - padding_bytes));
4390 : 448 : gimple *g = gimple_build_assign (dst, src);
4391 : 448 : gimple_set_location (g, buf->loc);
4392 : 448 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4393 : 448 : padding_bytes = 0;
4394 : 448 : buf->padding_bytes = 0;
4395 : : }
4396 : 2042 : if (nonzero_first == wordsize)
4397 : : /* All bits in a word are 0, there are no padding bits. */
4398 : 942 : continue;
4399 : 1100 : if (all_ones && nonzero_last == endsize)
4400 : : {
4401 : : /* All bits between nonzero_first and end of word are padding
4402 : : bits, start counting padding_bytes. */
4403 : 837 : padding_bytes = nonzero_last - nonzero_first;
4404 : 837 : continue;
4405 : : }
4406 : 263 : if (bytes_only)
4407 : : {
4408 : : /* If bitfields aren't involved in this word, prefer storing
4409 : : individual bytes or groups of them over performing a RMW
4410 : : operation on the whole word. */
4411 : 226 : gcc_assert (i + zero_last <= end);
4412 : 1113 : for (size_t j = padding_end; j < i + zero_last; j++)
4413 : : {
4414 : 887 : if (buf->buf[j])
4415 : : {
4416 : : size_t k;
4417 : 604 : for (k = j; k < i + zero_last; k++)
4418 : 604 : if (buf->buf[k] == 0)
4419 : : break;
4420 : 258 : HOST_WIDE_INT off = buf->off + j;
4421 : 258 : tree atype, src;
4422 : 258 : if (k - j == 1)
4423 : : {
4424 : 214 : atype = char_type_node;
4425 : 214 : src = build_zero_cst (char_type_node);
4426 : : }
4427 : : else
4428 : : {
4429 : 44 : atype = build_array_type_nelts (char_type_node, k - j);
4430 : 44 : src = build_constructor (atype, NULL);
4431 : : }
4432 : 258 : tree dst = build2_loc (buf->loc, MEM_REF, atype,
4433 : : buf->base,
4434 : 258 : build_int_cst (buf->alias_type, off));
4435 : 258 : gimple *g = gimple_build_assign (dst, src);
4436 : 258 : gimple_set_location (g, buf->loc);
4437 : 258 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4438 : 258 : j = k;
4439 : : }
4440 : : }
4441 : 226 : if (nonzero_last == endsize)
4442 : 98 : padding_bytes = nonzero_last - zero_last;
4443 : 226 : continue;
4444 : 226 : }
4445 : 120 : for (size_t eltsz = 1; eltsz <= wordsize; eltsz <<= 1)
4446 : : {
4447 : 120 : if (nonzero_last - nonzero_first <= eltsz
4448 : 37 : && ((nonzero_first & ~(eltsz - 1))
4449 : 37 : == ((nonzero_last - 1) & ~(eltsz - 1))))
4450 : : {
4451 : 37 : tree type;
4452 : 37 : if (eltsz == 1)
4453 : 2 : type = char_type_node;
4454 : : else
4455 : 35 : type = lang_hooks.types.type_for_size (eltsz * BITS_PER_UNIT,
4456 : : 0);
4457 : 37 : size_t start = nonzero_first & ~(eltsz - 1);
4458 : 37 : HOST_WIDE_INT off = buf->off + i + start;
4459 : 37 : tree atype = type;
4460 : 37 : if (eltsz > 1 && buf->align < TYPE_ALIGN (type))
4461 : 0 : atype = build_aligned_type (type, buf->align);
4462 : 37 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4463 : 37 : build_int_cst (buf->alias_type, off));
4464 : 37 : tree src;
4465 : 37 : gimple *g;
4466 : 37 : if (all_ones
4467 : 37 : && nonzero_first == start
4468 : 0 : && nonzero_last == start + eltsz)
4469 : 0 : src = build_zero_cst (type);
4470 : : else
4471 : : {
4472 : 37 : src = make_ssa_name (type);
4473 : 37 : tree tmp_dst = unshare_expr (dst);
4474 : : /* The folding introduces a read from the tmp_dst, we should
4475 : : prevent uninitialized warning analysis from issuing warning
4476 : : for such fake read. In order to suppress warning only for
4477 : : this expr, we should set the location of tmp_dst to
4478 : : UNKNOWN_LOCATION first, then suppress_warning will call
4479 : : set_no_warning_bit to set the no_warning flag only for
4480 : : tmp_dst. */
4481 : 37 : SET_EXPR_LOCATION (tmp_dst, UNKNOWN_LOCATION);
4482 : 37 : suppress_warning (tmp_dst, OPT_Wuninitialized);
4483 : 37 : g = gimple_build_assign (src, tmp_dst);
4484 : 37 : gimple_set_location (g, buf->loc);
4485 : 37 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4486 : 74 : tree mask = native_interpret_expr (type,
4487 : 37 : buf->buf + i + start,
4488 : : eltsz);
4489 : 37 : gcc_assert (mask && TREE_CODE (mask) == INTEGER_CST);
4490 : 37 : mask = fold_build1 (BIT_NOT_EXPR, type, mask);
4491 : 37 : tree src_masked = make_ssa_name (type);
4492 : 37 : g = gimple_build_assign (src_masked, BIT_AND_EXPR,
4493 : : src, mask);
4494 : 37 : gimple_set_location (g, buf->loc);
4495 : 37 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4496 : 37 : src = src_masked;
4497 : : }
4498 : 37 : g = gimple_build_assign (dst, src);
4499 : 37 : gimple_set_location (g, buf->loc);
4500 : 37 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4501 : 37 : break;
4502 : : }
4503 : : }
4504 : : }
4505 : 759 : if (full)
4506 : : {
4507 : 717 : if (padding_bytes)
4508 : : {
4509 : 487 : tree atype, src;
4510 : 487 : if (padding_bytes == 1)
4511 : : {
4512 : 108 : atype = char_type_node;
4513 : 108 : src = build_zero_cst (char_type_node);
4514 : : }
4515 : : else
4516 : : {
4517 : 379 : atype = build_array_type_nelts (char_type_node, padding_bytes);
4518 : 379 : src = build_constructor (atype, NULL);
4519 : : }
4520 : 487 : tree dst = build2_loc (buf->loc, MEM_REF, atype, buf->base,
4521 : : build_int_cst (buf->alias_type,
4522 : 487 : buf->off + end
4523 : 487 : - padding_bytes));
4524 : 487 : gimple *g = gimple_build_assign (dst, src);
4525 : 487 : gimple_set_location (g, buf->loc);
4526 : 487 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4527 : : }
4528 : 717 : size_t end_rem = end % UNITS_PER_WORD;
4529 : 717 : buf->off += end - end_rem;
4530 : 717 : buf->size = end_rem;
4531 : 717 : memset (buf->buf, 0, buf->size);
4532 : 717 : buf->padding_bytes = 0;
4533 : : }
4534 : : else
4535 : : {
4536 : 42 : memmove (buf->buf, buf->buf + end, buf->size - end);
4537 : 42 : buf->off += end;
4538 : 42 : buf->size -= end;
4539 : 42 : buf->padding_bytes = padding_bytes;
4540 : : }
4541 : : }
4542 : :
4543 : : /* Append PADDING_BYTES padding bytes. */
4544 : :
4545 : : static void
4546 : 5004 : clear_padding_add_padding (clear_padding_struct *buf,
4547 : : HOST_WIDE_INT padding_bytes)
4548 : : {
4549 : 5004 : if (padding_bytes == 0)
4550 : : return;
4551 : 1667 : if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
4552 : : > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
4553 : 42 : clear_padding_flush (buf, false);
4554 : 1667 : if ((unsigned HOST_WIDE_INT) padding_bytes + buf->size
4555 : : > (unsigned HOST_WIDE_INT) clear_padding_buf_size)
4556 : : {
4557 : 42 : memset (buf->buf + buf->size, ~0, clear_padding_buf_size - buf->size);
4558 : 42 : padding_bytes -= clear_padding_buf_size - buf->size;
4559 : 42 : buf->size = clear_padding_buf_size;
4560 : 42 : clear_padding_flush (buf, false);
4561 : 42 : gcc_assert (buf->padding_bytes);
4562 : : /* At this point buf->buf[0] through buf->buf[buf->size - 1]
4563 : : is guaranteed to be all ones. */
4564 : 42 : padding_bytes += buf->size;
4565 : 42 : buf->size = padding_bytes % UNITS_PER_WORD;
4566 : 42 : memset (buf->buf, ~0, buf->size);
4567 : 42 : buf->off += padding_bytes - buf->size;
4568 : 42 : buf->padding_bytes += padding_bytes - buf->size;
4569 : : }
4570 : : else
4571 : : {
4572 : 1625 : memset (buf->buf + buf->size, ~0, padding_bytes);
4573 : 1625 : buf->size += padding_bytes;
4574 : : }
4575 : : }
4576 : :
4577 : : static void clear_padding_type (clear_padding_struct *, tree,
4578 : : HOST_WIDE_INT, bool);
4579 : :
4580 : : /* Clear padding bits of union type TYPE. */
4581 : :
4582 : : static void
4583 : 128 : clear_padding_union (clear_padding_struct *buf, tree type,
4584 : : HOST_WIDE_INT sz, bool for_auto_init)
4585 : : {
4586 : 128 : clear_padding_struct *union_buf;
4587 : 128 : HOST_WIDE_INT start_off = 0, next_off = 0;
4588 : 128 : size_t start_size = 0;
4589 : 128 : if (buf->union_ptr)
4590 : : {
4591 : 42 : start_off = buf->off + buf->size;
4592 : 42 : next_off = start_off + sz;
4593 : 42 : start_size = start_off % UNITS_PER_WORD;
4594 : 42 : start_off -= start_size;
4595 : 42 : clear_padding_flush (buf, true);
4596 : 42 : union_buf = buf;
4597 : : }
4598 : : else
4599 : : {
4600 : 86 : if (sz + buf->size > clear_padding_buf_size)
4601 : 0 : clear_padding_flush (buf, false);
4602 : 86 : union_buf = XALLOCA (clear_padding_struct);
4603 : 86 : union_buf->loc = buf->loc;
4604 : 86 : union_buf->clear_in_mask = buf->clear_in_mask;
4605 : 86 : union_buf->base = NULL_TREE;
4606 : 86 : union_buf->alias_type = NULL_TREE;
4607 : 86 : union_buf->gsi = NULL;
4608 : 86 : union_buf->align = 0;
4609 : 86 : union_buf->off = 0;
4610 : 86 : union_buf->padding_bytes = 0;
4611 : 86 : union_buf->sz = sz;
4612 : 86 : union_buf->size = 0;
4613 : 86 : if (sz + buf->size <= clear_padding_buf_size)
4614 : 86 : union_buf->union_ptr = buf->buf + buf->size;
4615 : : else
4616 : 0 : union_buf->union_ptr = XNEWVEC (unsigned char, sz);
4617 : 86 : memset (union_buf->union_ptr, ~0, sz);
4618 : : }
4619 : :
4620 : 1193 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4621 : 1065 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4622 : : {
4623 : 359 : if (DECL_SIZE_UNIT (field) == NULL_TREE)
4624 : : {
4625 : 8 : if (TREE_TYPE (field) == error_mark_node)
4626 : 0 : continue;
4627 : 8 : gcc_assert (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4628 : : && !COMPLETE_TYPE_P (TREE_TYPE (field)));
4629 : 8 : if (!buf->clear_in_mask && !for_auto_init)
4630 : 8 : error_at (buf->loc, "flexible array member %qD does not have "
4631 : : "well defined padding bits for %qs",
4632 : : field, "__builtin_clear_padding");
4633 : 8 : continue;
4634 : : }
4635 : 351 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4636 : 351 : gcc_assert (union_buf->size == 0);
4637 : 351 : union_buf->off = start_off;
4638 : 351 : union_buf->size = start_size;
4639 : 351 : memset (union_buf->buf, ~0, start_size);
4640 : 351 : clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init);
4641 : 351 : clear_padding_add_padding (union_buf, sz - fldsz);
4642 : 351 : clear_padding_flush (union_buf, true);
4643 : : }
4644 : :
4645 : 128 : if (buf == union_buf)
4646 : : {
4647 : 42 : buf->off = next_off;
4648 : 42 : buf->size = next_off % UNITS_PER_WORD;
4649 : 42 : buf->off -= buf->size;
4650 : 42 : memset (buf->buf, ~0, buf->size);
4651 : : }
4652 : 86 : else if (sz + buf->size <= clear_padding_buf_size)
4653 : 86 : buf->size += sz;
4654 : : else
4655 : : {
4656 : 0 : unsigned char *union_ptr = union_buf->union_ptr;
4657 : 0 : while (sz)
4658 : : {
4659 : 0 : clear_padding_flush (buf, false);
4660 : 0 : HOST_WIDE_INT this_sz
4661 : 0 : = MIN ((unsigned HOST_WIDE_INT) sz,
4662 : : clear_padding_buf_size - buf->size);
4663 : 0 : memcpy (buf->buf + buf->size, union_ptr, this_sz);
4664 : 0 : buf->size += this_sz;
4665 : 0 : union_ptr += this_sz;
4666 : 0 : sz -= this_sz;
4667 : : }
4668 : 0 : XDELETE (union_buf->union_ptr);
4669 : : }
4670 : 128 : }
4671 : :
4672 : : /* The only known floating point formats with padding bits are the
4673 : : IEEE extended ones. */
4674 : :
4675 : : static bool
4676 : 32205 : clear_padding_real_needs_padding_p (tree type)
4677 : : {
4678 : 32205 : const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
4679 : 32205 : return (fmt->b == 2
4680 : 31746 : && fmt->signbit_ro == fmt->signbit_rw
4681 : 63951 : && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95));
4682 : : }
4683 : :
4684 : : /* _BitInt has padding bits if it isn't extended in the ABI and has smaller
4685 : : precision than bits in limb or corresponding number of limbs. */
4686 : :
4687 : : static bool
4688 : 6 : clear_padding_bitint_needs_padding_p (tree type)
4689 : : {
4690 : 6 : struct bitint_info info;
4691 : 6 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
4692 : 6 : gcc_assert (ok);
4693 : 6 : if (info.extended)
4694 : : return false;
4695 : 6 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.abi_limb_mode);
4696 : 6 : if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
4697 : : return true;
4698 : 4 : else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode))
4699 : : return false;
4700 : : else
4701 : 4 : return (((unsigned) TYPE_PRECISION (type))
4702 : 4 : % GET_MODE_PRECISION (limb_mode)) != 0;
4703 : : }
4704 : :
4705 : : /* Return true if TYPE might contain any padding bits. */
4706 : :
4707 : : bool
4708 : 921918 : clear_padding_type_may_have_padding_p (tree type)
4709 : : {
4710 : 1056370 : switch (TREE_CODE (type))
4711 : : {
4712 : : case RECORD_TYPE:
4713 : : case UNION_TYPE:
4714 : : return true;
4715 : 134452 : case ARRAY_TYPE:
4716 : 134452 : case COMPLEX_TYPE:
4717 : 134452 : case VECTOR_TYPE:
4718 : 134452 : return clear_padding_type_may_have_padding_p (TREE_TYPE (type));
4719 : 1670 : case REAL_TYPE:
4720 : 1670 : return clear_padding_real_needs_padding_p (type);
4721 : 6 : case BITINT_TYPE:
4722 : 6 : return clear_padding_bitint_needs_padding_p (type);
4723 : 38190 : default:
4724 : 38190 : return false;
4725 : : }
4726 : : }
4727 : :
4728 : : /* Return true if TYPE has padding bits aside from those in fields,
4729 : : elements, etc. */
4730 : :
4731 : : bool
4732 : 1184847 : type_has_padding_at_level_p (tree type)
4733 : : {
4734 : 1184847 : switch (TREE_CODE (type))
4735 : : {
4736 : 1047421 : case RECORD_TYPE:
4737 : 1047421 : {
4738 : 1047421 : tree bitpos = size_zero_node;
4739 : : /* Expect fields to be sorted by bit position. */
4740 : 8589571 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4741 : 7546695 : if (TREE_CODE (f) == FIELD_DECL)
4742 : : {
4743 : 2291681 : if (DECL_PADDING_P (f))
4744 : : return true;
4745 : 2291678 : tree pos = bit_position (f);
4746 : 2291678 : if (simple_cst_equal (bitpos, pos) != 1)
4747 : : return true;
4748 : 2287156 : if (!DECL_SIZE (f))
4749 : : return true;
4750 : 2287136 : bitpos = int_const_binop (PLUS_EXPR, pos, DECL_SIZE (f));
4751 : : }
4752 : 1042876 : if (simple_cst_equal (bitpos, TYPE_SIZE (type)) != 1)
4753 : : return true;
4754 : : return false;
4755 : : }
4756 : 3 : case UNION_TYPE:
4757 : 3 : case QUAL_UNION_TYPE:
4758 : 3 : bool any_fields;
4759 : 3 : any_fields = false;
4760 : : /* If any of the fields is smaller than the whole, there is padding. */
4761 : 6 : for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
4762 : 3 : if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node)
4763 : 3 : continue;
4764 : 0 : else if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
4765 : 0 : TYPE_SIZE (type)) != 1)
4766 : : return true;
4767 : : else
4768 : : any_fields = true;
4769 : : /* If the union doesn't have any fields and still has non-zero size,
4770 : : all of it is padding. */
4771 : 3 : if (!any_fields && !integer_zerop (TYPE_SIZE (type)))
4772 : : return true;
4773 : : return false;
4774 : : case ARRAY_TYPE:
4775 : : case COMPLEX_TYPE:
4776 : : case VECTOR_TYPE:
4777 : : /* No recursing here, no padding at this level. */
4778 : : return false;
4779 : 0 : case REAL_TYPE:
4780 : 0 : return clear_padding_real_needs_padding_p (type);
4781 : 0 : case BITINT_TYPE:
4782 : 0 : return clear_padding_bitint_needs_padding_p (type);
4783 : : default:
4784 : : return false;
4785 : : }
4786 : : }
4787 : :
4788 : : /* Emit a runtime loop:
4789 : : for (; buf.base != end; buf.base += sz)
4790 : : __builtin_clear_padding (buf.base); */
4791 : :
4792 : : static void
4793 : 114 : clear_padding_emit_loop (clear_padding_struct *buf, tree type,
4794 : : tree end, bool for_auto_init)
4795 : : {
4796 : 114 : tree l1 = create_artificial_label (buf->loc);
4797 : 114 : tree l2 = create_artificial_label (buf->loc);
4798 : 114 : tree l3 = create_artificial_label (buf->loc);
4799 : 114 : gimple *g = gimple_build_goto (l2);
4800 : 114 : gimple_set_location (g, buf->loc);
4801 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4802 : 114 : g = gimple_build_label (l1);
4803 : 114 : gimple_set_location (g, buf->loc);
4804 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4805 : 114 : clear_padding_type (buf, type, buf->sz, for_auto_init);
4806 : 114 : clear_padding_flush (buf, true);
4807 : 114 : g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR, buf->base,
4808 : 114 : size_int (buf->sz));
4809 : 114 : gimple_set_location (g, buf->loc);
4810 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4811 : 114 : g = gimple_build_label (l2);
4812 : 114 : gimple_set_location (g, buf->loc);
4813 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4814 : 114 : g = gimple_build_cond (NE_EXPR, buf->base, end, l1, l3);
4815 : 114 : gimple_set_location (g, buf->loc);
4816 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4817 : 114 : g = gimple_build_label (l3);
4818 : 114 : gimple_set_location (g, buf->loc);
4819 : 114 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4820 : 114 : }
4821 : :
4822 : : /* Clear padding bits for TYPE. Called recursively from
4823 : : gimple_fold_builtin_clear_padding. If FOR_AUTO_INIT is true,
4824 : : the __builtin_clear_padding is not called by the end user,
4825 : : instead, it's inserted by the compiler to initialize the
4826 : : paddings of automatic variable. Therefore, we should not
4827 : : emit the error messages for flexible array members to confuse
4828 : : the end user. */
4829 : :
4830 : : static void
4831 : 35685 : clear_padding_type (clear_padding_struct *buf, tree type,
4832 : : HOST_WIDE_INT sz, bool for_auto_init)
4833 : : {
4834 : 35685 : switch (TREE_CODE (type))
4835 : : {
4836 : 1256 : case RECORD_TYPE:
4837 : 1256 : HOST_WIDE_INT cur_pos;
4838 : 1256 : cur_pos = 0;
4839 : 16143 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4840 : 14887 : if (TREE_CODE (field) == FIELD_DECL && !DECL_PADDING_P (field))
4841 : : {
4842 : 3702 : tree ftype = TREE_TYPE (field);
4843 : 3702 : if (DECL_BIT_FIELD (field))
4844 : : {
4845 : 256 : HOST_WIDE_INT fldsz = TYPE_PRECISION (ftype);
4846 : 256 : if (fldsz == 0)
4847 : 0 : continue;
4848 : 256 : HOST_WIDE_INT pos = int_byte_position (field);
4849 : 256 : if (pos >= sz)
4850 : 0 : continue;
4851 : 256 : HOST_WIDE_INT bpos
4852 : 256 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
4853 : 256 : bpos %= BITS_PER_UNIT;
4854 : 256 : HOST_WIDE_INT end
4855 : 256 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4856 : 256 : if (pos + end > cur_pos)
4857 : : {
4858 : 195 : clear_padding_add_padding (buf, pos + end - cur_pos);
4859 : 195 : cur_pos = pos + end;
4860 : : }
4861 : 256 : gcc_assert (cur_pos > pos
4862 : : && ((unsigned HOST_WIDE_INT) buf->size
4863 : : >= (unsigned HOST_WIDE_INT) cur_pos - pos));
4864 : 256 : unsigned char *p = buf->buf + buf->size - (cur_pos - pos);
4865 : 256 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4866 : : sorry_at (buf->loc, "PDP11 bit-field handling unsupported"
4867 : : " in %qs", "__builtin_clear_padding");
4868 : 256 : else if (BYTES_BIG_ENDIAN)
4869 : : {
4870 : : /* Big endian. */
4871 : : if (bpos + fldsz <= BITS_PER_UNIT)
4872 : : *p &= ~(((1 << fldsz) - 1)
4873 : : << (BITS_PER_UNIT - bpos - fldsz));
4874 : : else
4875 : : {
4876 : : if (bpos)
4877 : : {
4878 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4879 : : p++;
4880 : : fldsz -= BITS_PER_UNIT - bpos;
4881 : : }
4882 : : memset (p, 0, fldsz / BITS_PER_UNIT);
4883 : : p += fldsz / BITS_PER_UNIT;
4884 : : fldsz %= BITS_PER_UNIT;
4885 : : if (fldsz)
4886 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4887 : : }
4888 : : }
4889 : : else
4890 : : {
4891 : : /* Little endian. */
4892 : 256 : if (bpos + fldsz <= BITS_PER_UNIT)
4893 : 159 : *p &= ~(((1 << fldsz) - 1) << bpos);
4894 : : else
4895 : : {
4896 : 97 : if (bpos)
4897 : : {
4898 : 29 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4899 : 29 : p++;
4900 : 29 : fldsz -= BITS_PER_UNIT - bpos;
4901 : : }
4902 : 97 : memset (p, 0, fldsz / BITS_PER_UNIT);
4903 : 97 : p += fldsz / BITS_PER_UNIT;
4904 : 97 : fldsz %= BITS_PER_UNIT;
4905 : 97 : if (fldsz)
4906 : 52 : *p &= ~((1 << fldsz) - 1);
4907 : : }
4908 : : }
4909 : : }
4910 : 3446 : else if (DECL_SIZE_UNIT (field) == NULL_TREE)
4911 : : {
4912 : 32 : if (ftype == error_mark_node)
4913 : 0 : continue;
4914 : 32 : gcc_assert (TREE_CODE (ftype) == ARRAY_TYPE
4915 : : && !COMPLETE_TYPE_P (ftype));
4916 : 32 : if (!buf->clear_in_mask && !for_auto_init)
4917 : 24 : error_at (buf->loc, "flexible array member %qD does not "
4918 : : "have well defined padding bits for %qs",
4919 : : field, "__builtin_clear_padding");
4920 : : }
4921 : 3414 : else if (is_empty_type (ftype))
4922 : 212 : continue;
4923 : : else
4924 : : {
4925 : 3202 : HOST_WIDE_INT pos = int_byte_position (field);
4926 : 3202 : if (pos >= sz)
4927 : 0 : continue;
4928 : 3202 : HOST_WIDE_INT fldsz = tree_to_shwi (DECL_SIZE_UNIT (field));
4929 : 3202 : gcc_assert (pos >= 0 && fldsz >= 0 && pos >= cur_pos);
4930 : 3202 : clear_padding_add_padding (buf, pos - cur_pos);
4931 : 3202 : cur_pos = pos;
4932 : 3202 : if (tree asbase = lang_hooks.types.classtype_as_base (field))
4933 : 188 : ftype = asbase;
4934 : 3202 : clear_padding_type (buf, ftype, fldsz, for_auto_init);
4935 : 3202 : cur_pos += fldsz;
4936 : : }
4937 : : }
4938 : 1256 : gcc_assert (sz >= cur_pos);
4939 : 1256 : clear_padding_add_padding (buf, sz - cur_pos);
4940 : 1256 : break;
4941 : 325 : case ARRAY_TYPE:
4942 : 325 : HOST_WIDE_INT nelts, fldsz;
4943 : 325 : fldsz = int_size_in_bytes (TREE_TYPE (type));
4944 : 325 : if (fldsz == 0)
4945 : : break;
4946 : 311 : nelts = sz / fldsz;
4947 : 311 : if (nelts > 1
4948 : 304 : && sz > 8 * UNITS_PER_WORD
4949 : 78 : && buf->union_ptr == NULL
4950 : 389 : && clear_padding_type_may_have_padding_p (TREE_TYPE (type)))
4951 : : {
4952 : : /* For sufficiently large array of more than one elements,
4953 : : emit a runtime loop to keep code size manageable. */
4954 : 66 : tree base = buf->base;
4955 : 66 : unsigned int prev_align = buf->align;
4956 : 66 : HOST_WIDE_INT off = buf->off + buf->size;
4957 : 66 : HOST_WIDE_INT prev_sz = buf->sz;
4958 : 66 : clear_padding_flush (buf, true);
4959 : 66 : tree elttype = TREE_TYPE (type);
4960 : 66 : buf->base = create_tmp_var (build_pointer_type (elttype));
4961 : 66 : tree end = make_ssa_name (TREE_TYPE (buf->base));
4962 : 66 : gimple *g = gimple_build_assign (buf->base, POINTER_PLUS_EXPR,
4963 : 66 : base, size_int (off));
4964 : 66 : gimple_set_location (g, buf->loc);
4965 : 66 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4966 : 66 : g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf->base,
4967 : 66 : size_int (sz));
4968 : 66 : gimple_set_location (g, buf->loc);
4969 : 66 : gsi_insert_before (buf->gsi, g, GSI_SAME_STMT);
4970 : 66 : buf->sz = fldsz;
4971 : 66 : buf->align = TYPE_ALIGN (elttype);
4972 : 66 : buf->off = 0;
4973 : 66 : buf->size = 0;
4974 : 66 : clear_padding_emit_loop (buf, elttype, end, for_auto_init);
4975 : 66 : off += sz;
4976 : 66 : buf->base = base;
4977 : 66 : buf->sz = prev_sz;
4978 : 66 : buf->align = prev_align;
4979 : 66 : buf->size = off % UNITS_PER_WORD;
4980 : 66 : buf->off = off - buf->size;
4981 : 66 : memset (buf->buf, 0, buf->size);
4982 : 66 : break;
4983 : : }
4984 : 1163 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
4985 : 918 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
4986 : : break;
4987 : 128 : case UNION_TYPE:
4988 : 128 : clear_padding_union (buf, type, sz, for_auto_init);
4989 : 128 : break;
4990 : 30535 : case REAL_TYPE:
4991 : 30535 : gcc_assert ((size_t) sz <= clear_padding_unit);
4992 : 30535 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
4993 : 0 : clear_padding_flush (buf, false);
4994 : 30535 : if (clear_padding_real_needs_padding_p (type))
4995 : : {
4996 : : /* Use native_interpret_real + native_encode_expr to figure out
4997 : : which bits are padding. */
4998 : 1218 : memset (buf->buf + buf->size, ~0, sz);
4999 : 1218 : tree cst = native_interpret_real (type, buf->buf + buf->size, sz);
5000 : 1218 : gcc_assert (cst && TREE_CODE (cst) == REAL_CST);
5001 : 1218 : int len = native_encode_expr (cst, buf->buf + buf->size, sz);
5002 : 1218 : gcc_assert (len > 0 && (size_t) len == (size_t) sz);
5003 : 20706 : for (size_t i = 0; i < (size_t) sz; i++)
5004 : 19488 : buf->buf[buf->size + i] ^= ~0;
5005 : : }
5006 : : else
5007 : 29317 : memset (buf->buf + buf->size, 0, sz);
5008 : 30535 : buf->size += sz;
5009 : 30535 : break;
5010 : 0 : case COMPLEX_TYPE:
5011 : 0 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5012 : 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5013 : 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5014 : 0 : break;
5015 : 0 : case VECTOR_TYPE:
5016 : 0 : nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
5017 : 0 : fldsz = int_size_in_bytes (TREE_TYPE (type));
5018 : 0 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5019 : 0 : clear_padding_type (buf, TREE_TYPE (type), fldsz, for_auto_init);
5020 : : break;
5021 : 7 : case NULLPTR_TYPE:
5022 : 7 : gcc_assert ((size_t) sz <= clear_padding_unit);
5023 : 7 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5024 : 0 : clear_padding_flush (buf, false);
5025 : 7 : memset (buf->buf + buf->size, ~0, sz);
5026 : 7 : buf->size += sz;
5027 : 7 : break;
5028 : 4 : case BITINT_TYPE:
5029 : 4 : {
5030 : 4 : struct bitint_info info;
5031 : 4 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
5032 : 4 : gcc_assert (ok);
5033 : 4 : scalar_int_mode limb_mode
5034 : 4 : = as_a <scalar_int_mode> (info.abi_limb_mode);
5035 : 4 : if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
5036 : : {
5037 : 2 : gcc_assert ((size_t) sz <= clear_padding_unit);
5038 : 2 : if ((unsigned HOST_WIDE_INT) sz + buf->size
5039 : : > clear_padding_buf_size)
5040 : 0 : clear_padding_flush (buf, false);
5041 : 2 : if (!info.extended
5042 : 2 : && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode))
5043 : : {
5044 : 2 : int tprec = GET_MODE_PRECISION (limb_mode);
5045 : 2 : int prec = TYPE_PRECISION (type);
5046 : 2 : tree t = build_nonstandard_integer_type (tprec, 1);
5047 : 2 : tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec));
5048 : 2 : int len = native_encode_expr (cst, buf->buf + buf->size, sz);
5049 : 2 : gcc_assert (len > 0 && (size_t) len == (size_t) sz);
5050 : : }
5051 : : else
5052 : 0 : memset (buf->buf + buf->size, 0, sz);
5053 : 2 : buf->size += sz;
5054 : 2 : break;
5055 : : }
5056 : 2 : tree limbtype
5057 : 2 : = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1);
5058 : 2 : fldsz = int_size_in_bytes (limbtype);
5059 : 2 : nelts = int_size_in_bytes (type) / fldsz;
5060 : 13 : for (HOST_WIDE_INT i = 0; i < nelts; i++)
5061 : : {
5062 : 11 : if (!info.extended
5063 : 11 : && i == (info.big_endian ? 0 : nelts - 1)
5064 : 13 : && (((unsigned) TYPE_PRECISION (type))
5065 : 2 : % TYPE_PRECISION (limbtype)) != 0)
5066 : : {
5067 : 2 : int tprec = GET_MODE_PRECISION (limb_mode);
5068 : 2 : int prec = (((unsigned) TYPE_PRECISION (type)) % tprec);
5069 : 2 : tree cst = wide_int_to_tree (limbtype,
5070 : 2 : wi::mask (prec, true, tprec));
5071 : 2 : int len = native_encode_expr (cst, buf->buf + buf->size,
5072 : : fldsz);
5073 : 2 : gcc_assert (len > 0 && (size_t) len == (size_t) fldsz);
5074 : 2 : buf->size += fldsz;
5075 : : }
5076 : : else
5077 : 9 : clear_padding_type (buf, limbtype, fldsz, for_auto_init);
5078 : : }
5079 : : break;
5080 : : }
5081 : 3430 : default:
5082 : 3430 : gcc_assert ((size_t) sz <= clear_padding_unit);
5083 : 3430 : if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size)
5084 : 0 : clear_padding_flush (buf, false);
5085 : 3430 : memset (buf->buf + buf->size, 0, sz);
5086 : 3430 : buf->size += sz;
5087 : 3430 : break;
5088 : : }
5089 : 35685 : }
5090 : :
5091 : : /* Clear padding bits of TYPE in MASK. */
5092 : :
5093 : : void
5094 : 30554 : clear_type_padding_in_mask (tree type, unsigned char *mask)
5095 : : {
5096 : 30554 : clear_padding_struct buf;
5097 : 30554 : buf.loc = UNKNOWN_LOCATION;
5098 : 30554 : buf.clear_in_mask = true;
5099 : 30554 : buf.base = NULL_TREE;
5100 : 30554 : buf.alias_type = NULL_TREE;
5101 : 30554 : buf.gsi = NULL;
5102 : 30554 : buf.align = 0;
5103 : 30554 : buf.off = 0;
5104 : 30554 : buf.padding_bytes = 0;
5105 : 30554 : buf.sz = int_size_in_bytes (type);
5106 : 30554 : buf.size = 0;
5107 : 30554 : buf.union_ptr = mask;
5108 : 30554 : clear_padding_type (&buf, type, buf.sz, false);
5109 : 30554 : clear_padding_flush (&buf, true);
5110 : 30554 : }
5111 : :
5112 : : /* Fold __builtin_clear_padding builtin. */
5113 : :
5114 : : static bool
5115 : 623 : gimple_fold_builtin_clear_padding (gimple_stmt_iterator *gsi)
5116 : : {
5117 : 623 : gimple *stmt = gsi_stmt (*gsi);
5118 : 623 : gcc_assert (gimple_call_num_args (stmt) == 2);
5119 : 623 : tree ptr = gimple_call_arg (stmt, 0);
5120 : 623 : tree typearg = gimple_call_arg (stmt, 1);
5121 : : /* The 2nd argument of __builtin_clear_padding's value is used to
5122 : : distinguish whether this call is made by the user or by the compiler
5123 : : for automatic variable initialization. */
5124 : 623 : bool for_auto_init = (bool) TREE_INT_CST_LOW (typearg);
5125 : 623 : tree type = TREE_TYPE (TREE_TYPE (typearg));
5126 : 623 : location_t loc = gimple_location (stmt);
5127 : 623 : clear_padding_struct buf;
5128 : 623 : gimple_stmt_iterator gsiprev = *gsi;
5129 : : /* This should be folded during the lower pass. */
5130 : 1246 : gcc_assert (!gimple_in_ssa_p (cfun) && cfun->cfg == NULL);
5131 : 623 : gcc_assert (COMPLETE_TYPE_P (type));
5132 : 623 : gsi_prev (&gsiprev);
5133 : :
5134 : 623 : buf.loc = loc;
5135 : 623 : buf.clear_in_mask = false;
5136 : 623 : buf.base = ptr;
5137 : 623 : buf.alias_type = NULL_TREE;
5138 : 623 : buf.gsi = gsi;
5139 : 623 : buf.align = get_pointer_alignment (ptr);
5140 : 623 : unsigned int talign = min_align_of_type (type) * BITS_PER_UNIT;
5141 : 623 : buf.align = MAX (buf.align, talign);
5142 : 623 : buf.off = 0;
5143 : 623 : buf.padding_bytes = 0;
5144 : 623 : buf.size = 0;
5145 : 623 : buf.sz = int_size_in_bytes (type);
5146 : 623 : buf.union_ptr = NULL;
5147 : 623 : if (buf.sz < 0 && int_size_in_bytes (strip_array_types (type)) < 0)
5148 : 1 : sorry_at (loc, "%s not supported for variable length aggregates",
5149 : : "__builtin_clear_padding");
5150 : : /* The implementation currently assumes 8-bit host and target
5151 : : chars which is the case for all currently supported targets
5152 : : and hosts and is required e.g. for native_{encode,interpret}* APIs. */
5153 : 622 : else if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
5154 : : sorry_at (loc, "%s not supported on this target",
5155 : : "__builtin_clear_padding");
5156 : 622 : else if (!clear_padding_type_may_have_padding_p (type))
5157 : : ;
5158 : 585 : else if (TREE_CODE (type) == ARRAY_TYPE && buf.sz < 0)
5159 : : {
5160 : 48 : tree sz = TYPE_SIZE_UNIT (type);
5161 : 48 : tree elttype = type;
5162 : : /* Only supports C/C++ VLAs and flattens all the VLA levels. */
5163 : 48 : while (TREE_CODE (elttype) == ARRAY_TYPE
5164 : 144 : && int_size_in_bytes (elttype) < 0)
5165 : 96 : elttype = TREE_TYPE (elttype);
5166 : 48 : HOST_WIDE_INT eltsz = int_size_in_bytes (elttype);
5167 : 48 : gcc_assert (eltsz >= 0);
5168 : 48 : if (eltsz)
5169 : : {
5170 : 48 : buf.base = create_tmp_var (build_pointer_type (elttype));
5171 : 48 : tree end = make_ssa_name (TREE_TYPE (buf.base));
5172 : 48 : gimple *g = gimple_build_assign (buf.base, ptr);
5173 : 48 : gimple_set_location (g, loc);
5174 : 48 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5175 : 48 : g = gimple_build_assign (end, POINTER_PLUS_EXPR, buf.base, sz);
5176 : 48 : gimple_set_location (g, loc);
5177 : 48 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5178 : 48 : buf.sz = eltsz;
5179 : 48 : buf.align = TYPE_ALIGN (elttype);
5180 : 48 : buf.alias_type = build_pointer_type (elttype);
5181 : 48 : clear_padding_emit_loop (&buf, elttype, end, for_auto_init);
5182 : : }
5183 : : }
5184 : : else
5185 : : {
5186 : 537 : if (!is_gimple_mem_ref_addr (buf.base))
5187 : : {
5188 : 28 : buf.base = make_ssa_name (TREE_TYPE (ptr));
5189 : 28 : gimple *g = gimple_build_assign (buf.base, ptr);
5190 : 28 : gimple_set_location (g, loc);
5191 : 28 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5192 : : }
5193 : 537 : buf.alias_type = build_pointer_type (type);
5194 : 537 : clear_padding_type (&buf, type, buf.sz, for_auto_init);
5195 : 537 : clear_padding_flush (&buf, true);
5196 : : }
5197 : :
5198 : 623 : gimple_stmt_iterator gsiprev2 = *gsi;
5199 : 623 : gsi_prev (&gsiprev2);
5200 : 623 : if (gsi_stmt (gsiprev) == gsi_stmt (gsiprev2))
5201 : 125 : gsi_replace (gsi, gimple_build_nop (), true);
5202 : : else
5203 : : {
5204 : 498 : gsi_remove (gsi, true);
5205 : 498 : *gsi = gsiprev2;
5206 : : }
5207 : 623 : return true;
5208 : : }
5209 : :
5210 : : /* Fold __builtin_constant_p builtin. */
5211 : :
5212 : : static bool
5213 : 29161 : gimple_fold_builtin_constant_p (gimple_stmt_iterator *gsi)
5214 : : {
5215 : 29161 : gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
5216 : :
5217 : 29161 : if (gimple_call_num_args (call) != 1)
5218 : : return false;
5219 : :
5220 : 29161 : tree arg = gimple_call_arg (call, 0);
5221 : 29161 : tree result = fold_builtin_constant_p (arg);
5222 : :
5223 : : /* Resolve __builtin_constant_p. If it hasn't been
5224 : : folded to integer_one_node by now, it's fairly
5225 : : certain that the value simply isn't constant. */
5226 : 57614 : if (!result && fold_before_rtl_expansion_p ())
5227 : 3 : result = integer_zero_node;
5228 : :
5229 : 29161 : if (!result)
5230 : : return false;
5231 : :
5232 : 711 : gimplify_and_update_call_from_tree (gsi, result);
5233 : 711 : return true;
5234 : : }
5235 : :
5236 : : /* Fold __builtin_assume_aligned builtin. */
5237 : :
5238 : : static bool
5239 : 4577 : gimple_fold_builtin_assume_aligned (gimple_stmt_iterator *gsi)
5240 : : {
5241 : 4577 : if (!fold_before_rtl_expansion_p ())
5242 : : return false;
5243 : :
5244 : 585 : gcall *call = as_a<gcall*>(gsi_stmt (*gsi));
5245 : :
5246 : 585 : if (gimple_call_num_args (call) < 2)
5247 : : return false;
5248 : :
5249 : 585 : gimplify_and_update_call_from_tree (gsi, gimple_call_arg (call, 0));
5250 : :
5251 : 585 : return true;
5252 : : }
5253 : :
5254 : : /* If va_list type is a simple pointer and nothing special is needed,
5255 : : optimize __builtin_va_start (&ap, 0) into ap = __builtin_next_arg (0),
5256 : : __builtin_va_end (&ap) out as NOP and __builtin_va_copy into a simple
5257 : : pointer assignment. Returns true if a change happened. */
5258 : :
5259 : : static bool
5260 : 113485 : gimple_fold_builtin_stdarg (gimple_stmt_iterator *gsi, gcall *call)
5261 : : {
5262 : : /* These shouldn't be folded before pass_stdarg. */
5263 : 113485 : if (!fold_before_rtl_expansion_p ())
5264 : : return false;
5265 : :
5266 : 10704 : tree callee, lhs, rhs, cfun_va_list;
5267 : 10704 : bool va_list_simple_ptr;
5268 : 10704 : location_t loc = gimple_location (call);
5269 : 10704 : gimple *nstmt0, *nstmt;
5270 : 10704 : tree tlhs, oldvdef, newvdef;
5271 : :
5272 : 10704 : callee = gimple_call_fndecl (call);
5273 : :
5274 : 10704 : cfun_va_list = targetm.fn_abi_va_list (callee);
5275 : 21408 : va_list_simple_ptr = POINTER_TYPE_P (cfun_va_list)
5276 : 10704 : && (TREE_TYPE (cfun_va_list) == void_type_node
5277 : 424 : || TREE_TYPE (cfun_va_list) == char_type_node);
5278 : :
5279 : 10704 : switch (DECL_FUNCTION_CODE (callee))
5280 : : {
5281 : 6946 : case BUILT_IN_VA_START:
5282 : 6946 : if (!va_list_simple_ptr
5283 : 164 : || targetm.expand_builtin_va_start != NULL
5284 : 7094 : || !builtin_decl_explicit_p (BUILT_IN_NEXT_ARG))
5285 : : return false;
5286 : :
5287 : 148 : if (gimple_call_num_args (call) != 2)
5288 : : return false;
5289 : :
5290 : 148 : lhs = gimple_call_arg (call, 0);
5291 : 148 : if (!POINTER_TYPE_P (TREE_TYPE (lhs))
5292 : 148 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
5293 : 148 : != TYPE_MAIN_VARIANT (cfun_va_list))
5294 : : return false;
5295 : : /* Create `tlhs = __builtin_next_arg(0);`. */
5296 : 148 : tlhs = make_ssa_name (cfun_va_list);
5297 : 148 : nstmt0 = gimple_build_call (builtin_decl_explicit (BUILT_IN_NEXT_ARG), 1, integer_zero_node);
5298 : 148 : lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
5299 : 148 : gimple_call_set_lhs (nstmt0, tlhs);
5300 : 148 : gimple_set_location (nstmt0, loc);
5301 : 148 : gimple_move_vops (nstmt0, call);
5302 : 148 : gsi_replace (gsi, nstmt0, false);
5303 : 148 : oldvdef = gimple_vdef (nstmt0);
5304 : 148 : newvdef = make_ssa_name (gimple_vop (cfun), nstmt0);
5305 : 148 : gimple_set_vdef (nstmt0, newvdef);
5306 : :
5307 : : /* Create `*lhs = tlhs;`. */
5308 : 148 : nstmt = gimple_build_assign (lhs, tlhs);
5309 : 148 : gimple_set_location (nstmt, loc);
5310 : 148 : gimple_set_vuse (nstmt, newvdef);
5311 : 148 : gimple_set_vdef (nstmt, oldvdef);
5312 : 148 : SSA_NAME_DEF_STMT (oldvdef) = nstmt;
5313 : 148 : gsi_insert_after (gsi, nstmt, GSI_NEW_STMT);
5314 : :
5315 : 148 : if (dump_file && (dump_flags & TDF_DETAILS))
5316 : : {
5317 : 0 : fprintf (dump_file, "Simplified\n ");
5318 : 0 : print_gimple_stmt (dump_file, call, 0, dump_flags);
5319 : 0 : fprintf (dump_file, "into\n ");
5320 : 0 : print_gimple_stmt (dump_file, nstmt0, 0, dump_flags);
5321 : 0 : fprintf (dump_file, " ");
5322 : 0 : print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
5323 : : }
5324 : : return true;
5325 : :
5326 : 246 : case BUILT_IN_VA_COPY:
5327 : 246 : if (!va_list_simple_ptr)
5328 : : return false;
5329 : :
5330 : 47 : if (gimple_call_num_args (call) != 2)
5331 : : return false;
5332 : :
5333 : 47 : lhs = gimple_call_arg (call, 0);
5334 : 47 : if (!POINTER_TYPE_P (TREE_TYPE (lhs))
5335 : 47 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (lhs)))
5336 : 47 : != TYPE_MAIN_VARIANT (cfun_va_list))
5337 : : return false;
5338 : 47 : rhs = gimple_call_arg (call, 1);
5339 : 47 : if (TYPE_MAIN_VARIANT (TREE_TYPE (rhs))
5340 : 47 : != TYPE_MAIN_VARIANT (cfun_va_list))
5341 : : return false;
5342 : :
5343 : 47 : lhs = fold_build2 (MEM_REF, cfun_va_list, lhs, build_zero_cst (TREE_TYPE (lhs)));
5344 : 47 : nstmt = gimple_build_assign (lhs, rhs);
5345 : 47 : gimple_set_location (nstmt, loc);
5346 : 47 : gimple_move_vops (nstmt, call);
5347 : 47 : gsi_replace (gsi, nstmt, false);
5348 : :
5349 : 47 : if (dump_file && (dump_flags & TDF_DETAILS))
5350 : : {
5351 : 0 : fprintf (dump_file, "Simplified\n ");
5352 : 0 : print_gimple_stmt (dump_file, call, 0, dump_flags);
5353 : 0 : fprintf (dump_file, "into\n ");
5354 : 0 : print_gimple_stmt (dump_file, nstmt, 0, dump_flags);
5355 : : }
5356 : : return true;
5357 : :
5358 : 3512 : case BUILT_IN_VA_END:
5359 : : /* No effect, so the statement will be deleted. */
5360 : 3512 : if (dump_file && (dump_flags & TDF_DETAILS))
5361 : : {
5362 : 0 : fprintf (dump_file, "Removed\n ");
5363 : 0 : print_gimple_stmt (dump_file, call, 0, dump_flags);
5364 : : }
5365 : 3512 : unlink_stmt_vdef (call);
5366 : 3512 : release_defs (call);
5367 : 3512 : gsi_replace (gsi, gimple_build_nop (), true);
5368 : 3512 : return true;
5369 : :
5370 : 0 : default:
5371 : 0 : gcc_unreachable ();
5372 : : }
5373 : : }
5374 : :
5375 : : /* Fold the non-target builtin at *GSI and return whether any simplification
5376 : : was made. */
5377 : :
5378 : : static bool
5379 : 12541678 : gimple_fold_builtin (gimple_stmt_iterator *gsi)
5380 : : {
5381 : 12541678 : gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
5382 : 12541678 : tree callee = gimple_call_fndecl (stmt);
5383 : :
5384 : : /* Give up for always_inline inline builtins until they are
5385 : : inlined. */
5386 : 12541678 : if (avoid_folding_inline_builtin (callee))
5387 : : return false;
5388 : :
5389 : 12540502 : unsigned n = gimple_call_num_args (stmt);
5390 : 12540502 : enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
5391 : 12540502 : switch (fcode)
5392 : : {
5393 : 113485 : case BUILT_IN_VA_START:
5394 : 113485 : case BUILT_IN_VA_END:
5395 : 113485 : case BUILT_IN_VA_COPY:
5396 : 113485 : return gimple_fold_builtin_stdarg (gsi, stmt);
5397 : 148 : case BUILT_IN_BCMP:
5398 : 148 : return gimple_fold_builtin_bcmp (gsi);
5399 : 367 : case BUILT_IN_BCOPY:
5400 : 367 : return gimple_fold_builtin_bcopy (gsi);
5401 : 250 : case BUILT_IN_BZERO:
5402 : 250 : return gimple_fold_builtin_bzero (gsi);
5403 : :
5404 : 310359 : case BUILT_IN_MEMSET:
5405 : 310359 : return gimple_fold_builtin_memset (gsi,
5406 : : gimple_call_arg (stmt, 1),
5407 : 310359 : gimple_call_arg (stmt, 2));
5408 : 897363 : case BUILT_IN_MEMCPY:
5409 : 897363 : case BUILT_IN_MEMPCPY:
5410 : 897363 : case BUILT_IN_MEMMOVE:
5411 : 897363 : return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
5412 : 897363 : gimple_call_arg (stmt, 1), fcode);
5413 : 4736 : case BUILT_IN_SPRINTF_CHK:
5414 : 4736 : case BUILT_IN_VSPRINTF_CHK:
5415 : 4736 : return gimple_fold_builtin_sprintf_chk (gsi, fcode);
5416 : 1702 : case BUILT_IN_STRCAT_CHK:
5417 : 1702 : return gimple_fold_builtin_strcat_chk (gsi);
5418 : 1143 : case BUILT_IN_STRNCAT_CHK:
5419 : 1143 : return gimple_fold_builtin_strncat_chk (gsi);
5420 : 140816 : case BUILT_IN_STRLEN:
5421 : 140816 : return gimple_fold_builtin_strlen (gsi);
5422 : 26019 : case BUILT_IN_STRCPY:
5423 : 26019 : return gimple_fold_builtin_strcpy (gsi,
5424 : : gimple_call_arg (stmt, 0),
5425 : 26019 : gimple_call_arg (stmt, 1));
5426 : 17199 : case BUILT_IN_STRNCPY:
5427 : 17199 : return gimple_fold_builtin_strncpy (gsi,
5428 : : gimple_call_arg (stmt, 0),
5429 : : gimple_call_arg (stmt, 1),
5430 : 17199 : gimple_call_arg (stmt, 2));
5431 : 7457 : case BUILT_IN_STRCAT:
5432 : 7457 : return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
5433 : 7457 : gimple_call_arg (stmt, 1));
5434 : 6786 : case BUILT_IN_STRNCAT:
5435 : 6786 : return gimple_fold_builtin_strncat (gsi);
5436 : 5010 : case BUILT_IN_INDEX:
5437 : 5010 : case BUILT_IN_STRCHR:
5438 : 5010 : return gimple_fold_builtin_strchr (gsi, false);
5439 : 721 : case BUILT_IN_RINDEX:
5440 : 721 : case BUILT_IN_STRRCHR:
5441 : 721 : return gimple_fold_builtin_strchr (gsi, true);
5442 : 4325 : case BUILT_IN_STRSTR:
5443 : 4325 : return gimple_fold_builtin_strstr (gsi);
5444 : 1247530 : case BUILT_IN_STRCMP:
5445 : 1247530 : case BUILT_IN_STRCMP_EQ:
5446 : 1247530 : case BUILT_IN_STRCASECMP:
5447 : 1247530 : case BUILT_IN_STRNCMP:
5448 : 1247530 : case BUILT_IN_STRNCMP_EQ:
5449 : 1247530 : case BUILT_IN_STRNCASECMP:
5450 : 1247530 : return gimple_fold_builtin_string_compare (gsi);
5451 : 29142 : case BUILT_IN_MEMCHR:
5452 : 29142 : return gimple_fold_builtin_memchr (gsi);
5453 : 20574 : case BUILT_IN_FPUTS:
5454 : 20574 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5455 : 20574 : gimple_call_arg (stmt, 1), false);
5456 : 43 : case BUILT_IN_FPUTS_UNLOCKED:
5457 : 43 : return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
5458 : 43 : gimple_call_arg (stmt, 1), true);
5459 : 25598 : case BUILT_IN_MEMCPY_CHK:
5460 : 25598 : case BUILT_IN_MEMPCPY_CHK:
5461 : 25598 : case BUILT_IN_MEMMOVE_CHK:
5462 : 25598 : case BUILT_IN_MEMSET_CHK:
5463 : 25598 : return gimple_fold_builtin_memory_chk (gsi,
5464 : : gimple_call_arg (stmt, 0),
5465 : : gimple_call_arg (stmt, 1),
5466 : : gimple_call_arg (stmt, 2),
5467 : : gimple_call_arg (stmt, 3),
5468 : 25598 : fcode);
5469 : 3683 : case BUILT_IN_STPCPY:
5470 : 3683 : return gimple_fold_builtin_stpcpy (gsi);
5471 : 2595 : case BUILT_IN_STRCPY_CHK:
5472 : 2595 : case BUILT_IN_STPCPY_CHK:
5473 : 2595 : return gimple_fold_builtin_stxcpy_chk (gsi,
5474 : : gimple_call_arg (stmt, 0),
5475 : : gimple_call_arg (stmt, 1),
5476 : : gimple_call_arg (stmt, 2),
5477 : 2595 : fcode);
5478 : 2724 : case BUILT_IN_STRNCPY_CHK:
5479 : 2724 : case BUILT_IN_STPNCPY_CHK:
5480 : 2724 : return gimple_fold_builtin_stxncpy_chk (gsi,
5481 : : gimple_call_arg (stmt, 0),
5482 : : gimple_call_arg (stmt, 1),
5483 : : gimple_call_arg (stmt, 2),
5484 : : gimple_call_arg (stmt, 3),
5485 : 2724 : fcode);
5486 : 2677 : case BUILT_IN_SNPRINTF_CHK:
5487 : 2677 : case BUILT_IN_VSNPRINTF_CHK:
5488 : 2677 : return gimple_fold_builtin_snprintf_chk (gsi, fcode);
5489 : :
5490 : 839592 : case BUILT_IN_FPRINTF:
5491 : 839592 : case BUILT_IN_FPRINTF_UNLOCKED:
5492 : 839592 : case BUILT_IN_VFPRINTF:
5493 : 839592 : if (n == 2 || n == 3)
5494 : 94426 : return gimple_fold_builtin_fprintf (gsi,
5495 : : gimple_call_arg (stmt, 0),
5496 : : gimple_call_arg (stmt, 1),
5497 : : n == 3
5498 : 42278 : ? gimple_call_arg (stmt, 2)
5499 : : : NULL_TREE,
5500 : 52148 : fcode);
5501 : : break;
5502 : 2228 : case BUILT_IN_FPRINTF_CHK:
5503 : 2228 : case BUILT_IN_VFPRINTF_CHK:
5504 : 2228 : if (n == 3 || n == 4)
5505 : 3813 : return gimple_fold_builtin_fprintf (gsi,
5506 : : gimple_call_arg (stmt, 0),
5507 : : gimple_call_arg (stmt, 2),
5508 : : n == 4
5509 : 1744 : ? gimple_call_arg (stmt, 3)
5510 : : : NULL_TREE,
5511 : 2069 : fcode);
5512 : : break;
5513 : 188345 : case BUILT_IN_PRINTF:
5514 : 188345 : case BUILT_IN_PRINTF_UNLOCKED:
5515 : 188345 : case BUILT_IN_VPRINTF:
5516 : 188345 : if (n == 1 || n == 2)
5517 : 213387 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
5518 : : n == 2
5519 : 102150 : ? gimple_call_arg (stmt, 1)
5520 : 111237 : : NULL_TREE, fcode);
5521 : : break;
5522 : 2273 : case BUILT_IN_PRINTF_CHK:
5523 : 2273 : case BUILT_IN_VPRINTF_CHK:
5524 : 2273 : if (n == 2 || n == 3)
5525 : 3893 : return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
5526 : : n == 3
5527 : 1771 : ? gimple_call_arg (stmt, 2)
5528 : 2122 : : NULL_TREE, fcode);
5529 : : break;
5530 : 2866 : case BUILT_IN_ACC_ON_DEVICE:
5531 : 2866 : return gimple_fold_builtin_acc_on_device (gsi,
5532 : 2866 : gimple_call_arg (stmt, 0));
5533 : 210 : case BUILT_IN_OMP_IS_INITIAL_DEVICE:
5534 : 210 : return gimple_fold_builtin_omp_is_initial_device (gsi);
5535 : :
5536 : 98 : case BUILT_IN_OMP_GET_INITIAL_DEVICE:
5537 : 98 : return gimple_fold_builtin_omp_get_initial_device (gsi);
5538 : :
5539 : 265 : case BUILT_IN_OMP_GET_NUM_DEVICES:
5540 : 265 : return gimple_fold_builtin_omp_get_num_devices (gsi);
5541 : :
5542 : 44636 : case BUILT_IN_REALLOC:
5543 : 44636 : return gimple_fold_builtin_realloc (gsi);
5544 : :
5545 : 623 : case BUILT_IN_CLEAR_PADDING:
5546 : 623 : return gimple_fold_builtin_clear_padding (gsi);
5547 : :
5548 : 29161 : case BUILT_IN_CONSTANT_P:
5549 : 29161 : return gimple_fold_builtin_constant_p (gsi);
5550 : :
5551 : 4577 : case BUILT_IN_ASSUME_ALIGNED:
5552 : 4577 : return gimple_fold_builtin_assume_aligned (gsi);
5553 : :
5554 : 9418038 : default:;
5555 : : }
5556 : :
5557 : : /* Try the generic builtin folder. */
5558 : 9418038 : bool ignore = (gimple_call_lhs (stmt) == NULL);
5559 : 9418038 : tree result = fold_call_stmt (stmt, ignore);
5560 : 9418038 : if (result)
5561 : : {
5562 : 5030 : if (ignore)
5563 : 1205 : STRIP_NOPS (result);
5564 : : else
5565 : 3825 : result = fold_convert (gimple_call_return_type (stmt), result);
5566 : 5030 : gimplify_and_update_call_from_tree (gsi, result);
5567 : 5030 : return true;
5568 : : }
5569 : :
5570 : : return false;
5571 : : }
5572 : :
5573 : : /* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
5574 : : function calls to constants, where possible. */
5575 : :
5576 : : static tree
5577 : 19729 : fold_internal_goacc_dim (const gimple *call)
5578 : : {
5579 : 19729 : int axis = oacc_get_ifn_dim_arg (call);
5580 : 19729 : int size = oacc_get_fn_dim_size (current_function_decl, axis);
5581 : 19729 : tree result = NULL_TREE;
5582 : 19729 : tree type = TREE_TYPE (gimple_call_lhs (call));
5583 : :
5584 : 19729 : switch (gimple_call_internal_fn (call))
5585 : : {
5586 : 8571 : case IFN_GOACC_DIM_POS:
5587 : : /* If the size is 1, we know the answer. */
5588 : 8571 : if (size == 1)
5589 : 8571 : result = build_int_cst (type, 0);
5590 : : break;
5591 : 11158 : case IFN_GOACC_DIM_SIZE:
5592 : : /* If the size is not dynamic, we know the answer. */
5593 : 11158 : if (size)
5594 : 11158 : result = build_int_cst (type, size);
5595 : : break;
5596 : : default:
5597 : : break;
5598 : : }
5599 : :
5600 : 19729 : return result;
5601 : : }
5602 : :
5603 : : /* Return true if stmt is __atomic_compare_exchange_N call which is suitable
5604 : : for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
5605 : : &var where var is only addressable because of such calls. */
5606 : :
5607 : : bool
5608 : 58978879 : optimize_atomic_compare_exchange_p (gimple *stmt)
5609 : : {
5610 : 58978879 : if (gimple_call_num_args (stmt) != 6
5611 : 1635752 : || !flag_inline_atomics
5612 : 1635752 : || !optimize
5613 : 1635752 : || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
5614 : 1635691 : || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
5615 : 1110017 : || !gimple_vdef (stmt)
5616 : 59971321 : || !gimple_vuse (stmt))
5617 : 57986437 : return false;
5618 : :
5619 : 992442 : tree fndecl = gimple_call_fndecl (stmt);
5620 : 992442 : switch (DECL_FUNCTION_CODE (fndecl))
5621 : : {
5622 : 53610 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
5623 : 53610 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
5624 : 53610 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
5625 : 53610 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
5626 : 53610 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
5627 : 53610 : break;
5628 : : default:
5629 : : return false;
5630 : : }
5631 : :
5632 : 53610 : tree expected = gimple_call_arg (stmt, 1);
5633 : 53610 : if (TREE_CODE (expected) != ADDR_EXPR
5634 : 53610 : || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
5635 : : return false;
5636 : :
5637 : 50844 : tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
5638 : 50844 : if (!is_gimple_reg_type (etype)
5639 : 50410 : || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
5640 : 48012 : || TREE_THIS_VOLATILE (etype)
5641 : 48012 : || VECTOR_TYPE_P (etype)
5642 : : || TREE_CODE (etype) == COMPLEX_TYPE
5643 : : /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
5644 : : might not preserve all the bits. See PR71716. */
5645 : : || SCALAR_FLOAT_TYPE_P (etype)
5646 : 69332 : || maybe_ne (TYPE_PRECISION (etype),
5647 : 36976 : GET_MODE_BITSIZE (TYPE_MODE (etype))))
5648 : 38814 : return false;
5649 : :
5650 : 12030 : tree weak = gimple_call_arg (stmt, 3);
5651 : 12030 : if (!integer_zerop (weak) && !integer_onep (weak))
5652 : : return false;
5653 : :
5654 : 12030 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5655 : 12030 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5656 : 12030 : machine_mode mode = TYPE_MODE (itype);
5657 : :
5658 : 12030 : if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
5659 : : == CODE_FOR_nothing
5660 : 12030 : && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
5661 : : return false;
5662 : :
5663 : 24060 : if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
5664 : : return false;
5665 : :
5666 : : return true;
5667 : : }
5668 : :
5669 : : /* Fold
5670 : : r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
5671 : : into
5672 : : _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
5673 : : i = IMAGPART_EXPR <t>;
5674 : : r = (_Bool) i;
5675 : : e = REALPART_EXPR <t>; */
5676 : :
5677 : : void
5678 : 5908 : fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
5679 : : {
5680 : 5908 : gimple *stmt = gsi_stmt (*gsi);
5681 : 5908 : tree fndecl = gimple_call_fndecl (stmt);
5682 : 5908 : tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5683 : 5908 : tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
5684 : 5908 : tree ctype = build_complex_type (itype);
5685 : 5908 : tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
5686 : 5908 : bool throws = false;
5687 : 5908 : edge e = NULL;
5688 : 5908 : gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5689 : : expected);
5690 : 5908 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5691 : 5908 : gimple_stmt_iterator gsiret = gsi_for_stmt (g);
5692 : 5908 : if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
5693 : : {
5694 : 2689 : g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
5695 : : build1 (VIEW_CONVERT_EXPR, itype,
5696 : : gimple_assign_lhs (g)));
5697 : 2689 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
5698 : : }
5699 : 5908 : int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
5700 : 11356 : + int_size_in_bytes (itype);
5701 : 5908 : g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
5702 : : gimple_call_arg (stmt, 0),
5703 : : gimple_assign_lhs (g),
5704 : : gimple_call_arg (stmt, 2),
5705 : 5908 : build_int_cst (integer_type_node, flag),
5706 : : gimple_call_arg (stmt, 4),
5707 : : gimple_call_arg (stmt, 5));
5708 : 5908 : tree lhs = make_ssa_name (ctype);
5709 : 5908 : gimple_call_set_lhs (g, lhs);
5710 : 5908 : gimple_move_vops (g, stmt);
5711 : 5908 : tree oldlhs = gimple_call_lhs (stmt);
5712 : 5908 : if (stmt_can_throw_internal (cfun, stmt))
5713 : : {
5714 : 203 : throws = true;
5715 : 203 : e = find_fallthru_edge (gsi_bb (*gsi)->succs);
5716 : : }
5717 : 5908 : gimple_call_set_nothrow (as_a <gcall *> (g),
5718 : 5908 : gimple_call_nothrow_p (as_a <gcall *> (stmt)));
5719 : 5908 : gimple_call_set_lhs (stmt, NULL_TREE);
5720 : 5908 : gsi_replace (gsi, g, true);
5721 : 5908 : if (oldlhs)
5722 : : {
5723 : 5861 : g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
5724 : : build1 (IMAGPART_EXPR, itype, lhs));
5725 : 5861 : if (throws)
5726 : : {
5727 : 197 : gsi_insert_on_edge_immediate (e, g);
5728 : 197 : *gsi = gsi_for_stmt (g);
5729 : : }
5730 : : else
5731 : 5664 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5732 : 5861 : g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
5733 : 5861 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5734 : : }
5735 : 5908 : g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
5736 : : build1 (REALPART_EXPR, itype, lhs));
5737 : 5908 : if (throws && oldlhs == NULL_TREE)
5738 : : {
5739 : 6 : gsi_insert_on_edge_immediate (e, g);
5740 : 6 : *gsi = gsi_for_stmt (g);
5741 : : }
5742 : : else
5743 : 5902 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5744 : 5908 : if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
5745 : : {
5746 : 5378 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
5747 : : VIEW_CONVERT_EXPR,
5748 : 2689 : build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
5749 : : gimple_assign_lhs (g)));
5750 : 2689 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5751 : : }
5752 : 5908 : g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
5753 : 5908 : gsi_insert_after (gsi, g, GSI_NEW_STMT);
5754 : 5908 : *gsi = gsiret;
5755 : 5908 : }
5756 : :
5757 : : /* Return true if ARG0 CODE ARG1 in infinite signed precision operation
5758 : : doesn't fit into TYPE. The test for overflow should be regardless of
5759 : : -fwrapv, and even for unsigned types. */
5760 : :
5761 : : bool
5762 : 374910 : arith_overflowed_p (enum tree_code code, const_tree type,
5763 : : const_tree arg0, const_tree arg1)
5764 : : {
5765 : 374910 : widest2_int warg0 = widest2_int_cst (arg0);
5766 : 374910 : widest2_int warg1 = widest2_int_cst (arg1);
5767 : 374910 : widest2_int wres;
5768 : 374910 : switch (code)
5769 : : {
5770 : 97938 : case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
5771 : 115283 : case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
5772 : 162868 : case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
5773 : 0 : default: gcc_unreachable ();
5774 : : }
5775 : 374910 : signop sign = TYPE_SIGN (type);
5776 : 374910 : if (sign == UNSIGNED && wi::neg_p (wres))
5777 : : return true;
5778 : 303053 : return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
5779 : 374922 : }
5780 : :
5781 : : /* If IFN_{MASK,LEN,MASK_LEN}_LOAD/STORE call CALL is unconditional,
5782 : : return a MEM_REF for the memory it references, otherwise return null.
5783 : : VECTYPE is the type of the memory vector. MASK_P indicates it's for
5784 : : MASK if true, otherwise it's for LEN. */
5785 : :
5786 : : static tree
5787 : 3570 : gimple_fold_partial_load_store_mem_ref (gcall *call, tree vectype, bool mask_p)
5788 : : {
5789 : 3570 : tree ptr = gimple_call_arg (call, 0);
5790 : 3570 : tree alias_align = gimple_call_arg (call, 1);
5791 : 3570 : if (!tree_fits_uhwi_p (alias_align))
5792 : : return NULL_TREE;
5793 : :
5794 : 3570 : if (mask_p)
5795 : : {
5796 : 3570 : tree mask = gimple_call_arg (call, 2);
5797 : 3570 : if (!integer_all_onesp (mask))
5798 : : return NULL_TREE;
5799 : : }
5800 : : else
5801 : : {
5802 : 0 : internal_fn ifn = gimple_call_internal_fn (call);
5803 : 0 : int len_index = internal_fn_len_index (ifn);
5804 : 0 : tree basic_len = gimple_call_arg (call, len_index);
5805 : 0 : if (!poly_int_tree_p (basic_len))
5806 : : return NULL_TREE;
5807 : 0 : tree bias = gimple_call_arg (call, len_index + 1);
5808 : 0 : gcc_assert (TREE_CODE (bias) == INTEGER_CST);
5809 : : /* For LEN_LOAD/LEN_STORE/MASK_LEN_LOAD/MASK_LEN_STORE,
5810 : : we don't fold when (bias + len) != VF. */
5811 : 0 : if (maybe_ne (wi::to_poly_widest (basic_len) + wi::to_widest (bias),
5812 : 0 : GET_MODE_NUNITS (TYPE_MODE (vectype))))
5813 : : return NULL_TREE;
5814 : :
5815 : : /* For MASK_LEN_{LOAD,STORE}, we should also check whether
5816 : : the mask is all ones mask. */
5817 : 0 : if (ifn == IFN_MASK_LEN_LOAD || ifn == IFN_MASK_LEN_STORE)
5818 : : {
5819 : 0 : tree mask = gimple_call_arg (call, internal_fn_mask_index (ifn));
5820 : 0 : if (!integer_all_onesp (mask))
5821 : : return NULL_TREE;
5822 : : }
5823 : : }
5824 : :
5825 : 71 : unsigned HOST_WIDE_INT align = tree_to_uhwi (alias_align);
5826 : 71 : if (TYPE_ALIGN (vectype) != align)
5827 : 14 : vectype = build_aligned_type (vectype, align);
5828 : 71 : tree offset = build_zero_cst (TREE_TYPE (alias_align));
5829 : 71 : return fold_build2 (MEM_REF, vectype, ptr, offset);
5830 : : }
5831 : :
5832 : : /* Try to fold IFN_{MASK,LEN}_LOAD call CALL. Return true on success.
5833 : : MASK_P indicates it's for MASK if true, otherwise it's for LEN. */
5834 : :
5835 : : static bool
5836 : 1680 : gimple_fold_partial_load (gimple_stmt_iterator *gsi, gcall *call, bool mask_p)
5837 : : {
5838 : 1680 : tree lhs = gimple_call_lhs (call);
5839 : 1680 : if (!lhs)
5840 : : return false;
5841 : :
5842 : 3360 : if (tree rhs
5843 : 1680 : = gimple_fold_partial_load_store_mem_ref (call, TREE_TYPE (lhs), mask_p))
5844 : : {
5845 : 17 : gassign *new_stmt = gimple_build_assign (lhs, rhs);
5846 : 17 : gimple_set_location (new_stmt, gimple_location (call));
5847 : 17 : gimple_move_vops (new_stmt, call);
5848 : 17 : gsi_replace (gsi, new_stmt, false);
5849 : 17 : return true;
5850 : : }
5851 : : return false;
5852 : : }
5853 : :
5854 : : /* Try to fold IFN_{MASK,LEN}_STORE call CALL. Return true on success.
5855 : : MASK_P indicates it's for MASK if true, otherwise it's for LEN. */
5856 : :
5857 : : static bool
5858 : 1890 : gimple_fold_partial_store (gimple_stmt_iterator *gsi, gcall *call,
5859 : : bool mask_p)
5860 : : {
5861 : 1890 : internal_fn ifn = gimple_call_internal_fn (call);
5862 : 1890 : tree rhs = gimple_call_arg (call, internal_fn_stored_value_index (ifn));
5863 : 3780 : if (tree lhs
5864 : 1890 : = gimple_fold_partial_load_store_mem_ref (call, TREE_TYPE (rhs), mask_p))
5865 : : {
5866 : 54 : gassign *new_stmt = gimple_build_assign (lhs, rhs);
5867 : 54 : gimple_set_location (new_stmt, gimple_location (call));
5868 : 54 : gimple_move_vops (new_stmt, call);
5869 : 54 : gsi_replace (gsi, new_stmt, false);
5870 : 54 : return true;
5871 : : }
5872 : : return false;
5873 : : }
5874 : :
5875 : : /* Attempt to fold a call statement referenced by the statement iterator GSI.
5876 : : The statement may be replaced by another statement, e.g., if the call
5877 : : simplifies to a constant value. Return true if any changes were made.
5878 : : It is assumed that the operands have been previously folded. */
5879 : :
5880 : : static bool
5881 : 55578142 : gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
5882 : : {
5883 : 55578142 : gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
5884 : 55578142 : tree callee;
5885 : 55578142 : bool changed = false;
5886 : :
5887 : : /* Check for virtual calls that became direct calls. */
5888 : 55578142 : callee = gimple_call_fn (stmt);
5889 : 55578142 : if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
5890 : : {
5891 : 519639 : if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
5892 : : {
5893 : 6 : if (dump_file && virtual_method_call_p (callee)
5894 : 337 : && !possible_polymorphic_call_target_p
5895 : 6 : (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
5896 : 6 : (OBJ_TYPE_REF_EXPR (callee)))))
5897 : : {
5898 : 0 : fprintf (dump_file,
5899 : : "Type inheritance inconsistent devirtualization of ");
5900 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5901 : 0 : fprintf (dump_file, " to ");
5902 : 0 : print_generic_expr (dump_file, callee, TDF_SLIM);
5903 : 0 : fprintf (dump_file, "\n");
5904 : : }
5905 : :
5906 : 331 : gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
5907 : 331 : changed = true;
5908 : : }
5909 : 519308 : else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
5910 : : {
5911 : 514519 : bool final;
5912 : 514519 : vec <cgraph_node *>targets
5913 : 514519 : = possible_polymorphic_call_targets (callee, stmt, &final);
5914 : 517124 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
5915 : : {
5916 : 2052 : tree lhs = gimple_call_lhs (stmt);
5917 : 2052 : if (dump_enabled_p ())
5918 : : {
5919 : 34 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
5920 : : "folding virtual function call to %s\n",
5921 : 34 : targets.length () == 1
5922 : 17 : ? targets[0]->name ()
5923 : : : "__builtin_unreachable");
5924 : : }
5925 : 2052 : if (targets.length () == 1)
5926 : : {
5927 : 2015 : tree fndecl = targets[0]->decl;
5928 : 2015 : gimple_call_set_fndecl (stmt, fndecl);
5929 : 2015 : changed = true;
5930 : : /* If changing the call to __cxa_pure_virtual
5931 : : or similar noreturn function, adjust gimple_call_fntype
5932 : : too. */
5933 : 2015 : if (gimple_call_noreturn_p (stmt)
5934 : 28 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
5935 : 13 : && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
5936 : 2028 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5937 : 13 : == void_type_node))
5938 : 13 : gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
5939 : : /* If the call becomes noreturn, remove the lhs. */
5940 : 2015 : if (lhs
5941 : 1678 : && gimple_call_noreturn_p (stmt)
5942 : 2030 : && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
5943 : 6 : || should_remove_lhs_p (lhs)))
5944 : : {
5945 : 12 : if (TREE_CODE (lhs) == SSA_NAME)
5946 : : {
5947 : 0 : tree var = create_tmp_var (TREE_TYPE (lhs));
5948 : 0 : tree def = get_or_create_ssa_default_def (cfun, var);
5949 : 0 : gimple *new_stmt = gimple_build_assign (lhs, def);
5950 : 0 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5951 : : }
5952 : 12 : gimple_call_set_lhs (stmt, NULL_TREE);
5953 : : }
5954 : 2015 : maybe_remove_unused_call_args (cfun, stmt);
5955 : : }
5956 : : else
5957 : : {
5958 : 37 : location_t loc = gimple_location (stmt);
5959 : 37 : gimple *new_stmt = gimple_build_builtin_unreachable (loc);
5960 : 37 : gimple_call_set_ctrl_altering (new_stmt, false);
5961 : : /* If the call had a SSA name as lhs morph that into
5962 : : an uninitialized value. */
5963 : 37 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
5964 : : {
5965 : 12 : tree var = create_tmp_var (TREE_TYPE (lhs));
5966 : 12 : SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
5967 : 12 : SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
5968 : 12 : set_ssa_default_def (cfun, var, lhs);
5969 : : }
5970 : 37 : gimple_move_vops (new_stmt, stmt);
5971 : 37 : gsi_replace (gsi, new_stmt, false);
5972 : 37 : return true;
5973 : : }
5974 : : }
5975 : : }
5976 : : }
5977 : :
5978 : : /* Check for indirect calls that became direct calls, and then
5979 : : no longer require a static chain. */
5980 : 55578105 : if (gimple_call_chain (stmt))
5981 : : {
5982 : 245745 : tree fn = gimple_call_fndecl (stmt);
5983 : 294035 : if (fn && !DECL_STATIC_CHAIN (fn))
5984 : : {
5985 : 2024 : gimple_call_set_chain (stmt, NULL);
5986 : 2024 : changed = true;
5987 : : }
5988 : : }
5989 : :
5990 : 55578105 : if (inplace)
5991 : : return changed;
5992 : :
5993 : : /* Check for builtins that CCP can handle using information not
5994 : : available in the generic fold routines. */
5995 : 55575642 : if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
5996 : : {
5997 : 12541678 : if (gimple_fold_builtin (gsi))
5998 : 200557 : changed = true;
5999 : : }
6000 : 43033964 : else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
6001 : : {
6002 : 1078365 : changed |= targetm.gimple_fold_builtin (gsi);
6003 : : }
6004 : 41955599 : else if (gimple_call_internal_p (stmt))
6005 : : {
6006 : 2127471 : enum tree_code subcode = ERROR_MARK;
6007 : 2127471 : tree result = NULL_TREE;
6008 : 2127471 : bool cplx_result = false;
6009 : 2127471 : bool uaddc_usubc = false;
6010 : 2127471 : tree overflow = NULL_TREE;
6011 : 2127471 : switch (gimple_call_internal_fn (stmt))
6012 : : {
6013 : 832 : case IFN_ASSUME:
6014 : : /* Remove .ASSUME calls during the last fold since it is no
6015 : : longer needed. */
6016 : 832 : if (fold_before_rtl_expansion_p ())
6017 : 114 : replace_call_with_value (gsi, NULL_TREE);
6018 : : break;
6019 : 157672 : case IFN_BUILTIN_EXPECT:
6020 : 157672 : result = fold_builtin_expect (gimple_location (stmt),
6021 : : gimple_call_arg (stmt, 0),
6022 : : gimple_call_arg (stmt, 1),
6023 : : gimple_call_arg (stmt, 2),
6024 : : NULL_TREE);
6025 : 157672 : break;
6026 : 8660 : case IFN_UBSAN_OBJECT_SIZE:
6027 : 8660 : {
6028 : 8660 : tree offset = gimple_call_arg (stmt, 1);
6029 : 8660 : tree objsize = gimple_call_arg (stmt, 2);
6030 : 8660 : if (integer_all_onesp (objsize)
6031 : 8660 : || (TREE_CODE (offset) == INTEGER_CST
6032 : 4787 : && TREE_CODE (objsize) == INTEGER_CST
6033 : 1126 : && tree_int_cst_le (offset, objsize)))
6034 : : {
6035 : 1539 : replace_call_with_value (gsi, NULL_TREE);
6036 : 1539 : return true;
6037 : : }
6038 : : }
6039 : : break;
6040 : 11349 : case IFN_UBSAN_PTR:
6041 : 11349 : if (integer_zerop (gimple_call_arg (stmt, 1)))
6042 : : {
6043 : 30 : replace_call_with_value (gsi, NULL_TREE);
6044 : 30 : return true;
6045 : : }
6046 : : break;
6047 : 7358 : case IFN_UBSAN_BOUNDS:
6048 : 7358 : {
6049 : 7358 : tree index = gimple_call_arg (stmt, 1);
6050 : 7358 : tree bound = gimple_call_arg (stmt, 2);
6051 : 7358 : if (TREE_CODE (index) == INTEGER_CST
6052 : 4338 : && TREE_CODE (bound) == INTEGER_CST)
6053 : : {
6054 : 4078 : index = fold_convert (TREE_TYPE (bound), index);
6055 : 4078 : if (TREE_CODE (index) == INTEGER_CST
6056 : 4078 : && tree_int_cst_lt (index, bound))
6057 : : {
6058 : 284 : replace_call_with_value (gsi, NULL_TREE);
6059 : 284 : return true;
6060 : : }
6061 : : }
6062 : : }
6063 : : break;
6064 : 19729 : case IFN_GOACC_DIM_SIZE:
6065 : 19729 : case IFN_GOACC_DIM_POS:
6066 : 19729 : result = fold_internal_goacc_dim (stmt);
6067 : 19729 : break;
6068 : : case IFN_UBSAN_CHECK_ADD:
6069 : : subcode = PLUS_EXPR;
6070 : : break;
6071 : : case IFN_UBSAN_CHECK_SUB:
6072 : : subcode = MINUS_EXPR;
6073 : : break;
6074 : : case IFN_UBSAN_CHECK_MUL:
6075 : : subcode = MULT_EXPR;
6076 : : break;
6077 : : case IFN_ADD_OVERFLOW:
6078 : : subcode = PLUS_EXPR;
6079 : : cplx_result = true;
6080 : : break;
6081 : : case IFN_SUB_OVERFLOW:
6082 : : subcode = MINUS_EXPR;
6083 : : cplx_result = true;
6084 : : break;
6085 : : case IFN_MUL_OVERFLOW:
6086 : : subcode = MULT_EXPR;
6087 : : cplx_result = true;
6088 : : break;
6089 : : case IFN_UADDC:
6090 : : subcode = PLUS_EXPR;
6091 : : cplx_result = true;
6092 : : uaddc_usubc = true;
6093 : : break;
6094 : : case IFN_USUBC:
6095 : : subcode = MINUS_EXPR;
6096 : : cplx_result = true;
6097 : : uaddc_usubc = true;
6098 : : break;
6099 : 1680 : case IFN_MASK_LOAD:
6100 : 1680 : changed |= gimple_fold_partial_load (gsi, stmt, true);
6101 : 1680 : break;
6102 : 1890 : case IFN_MASK_STORE:
6103 : 1890 : changed |= gimple_fold_partial_store (gsi, stmt, true);
6104 : 1890 : break;
6105 : 0 : case IFN_LEN_LOAD:
6106 : 0 : case IFN_MASK_LEN_LOAD:
6107 : 0 : changed |= gimple_fold_partial_load (gsi, stmt, false);
6108 : 0 : break;
6109 : 0 : case IFN_LEN_STORE:
6110 : 0 : case IFN_MASK_LEN_STORE:
6111 : 0 : changed |= gimple_fold_partial_store (gsi, stmt, false);
6112 : 0 : break;
6113 : : default:
6114 : : break;
6115 : : }
6116 : 181085 : if (subcode != ERROR_MARK)
6117 : : {
6118 : 490670 : tree arg0 = gimple_call_arg (stmt, 0);
6119 : 490670 : tree arg1 = gimple_call_arg (stmt, 1);
6120 : 490670 : tree arg2 = NULL_TREE;
6121 : 490670 : tree type = TREE_TYPE (arg0);
6122 : 490670 : if (cplx_result)
6123 : : {
6124 : 471636 : tree lhs = gimple_call_lhs (stmt);
6125 : 471636 : if (lhs == NULL_TREE)
6126 : : type = NULL_TREE;
6127 : : else
6128 : 471636 : type = TREE_TYPE (TREE_TYPE (lhs));
6129 : 471636 : if (uaddc_usubc)
6130 : 31081 : arg2 = gimple_call_arg (stmt, 2);
6131 : : }
6132 : 490670 : if (type == NULL_TREE)
6133 : : ;
6134 : 490670 : else if (uaddc_usubc)
6135 : : {
6136 : 31081 : if (!integer_zerop (arg2))
6137 : : ;
6138 : : /* x = y + 0 + 0; x = y - 0 - 0; */
6139 : 4861 : else if (integer_zerop (arg1))
6140 : : result = arg0;
6141 : : /* x = 0 + y + 0; */
6142 : 4237 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
6143 : : result = arg1;
6144 : : /* x = y - y - 0; */
6145 : 4237 : else if (subcode == MINUS_EXPR
6146 : 4237 : && operand_equal_p (arg0, arg1, 0))
6147 : 0 : result = integer_zero_node;
6148 : : }
6149 : : /* x = y + 0; x = y - 0; x = y * 0; */
6150 : 459589 : else if (integer_zerop (arg1))
6151 : 10106 : result = subcode == MULT_EXPR ? integer_zero_node : arg0;
6152 : : /* x = 0 + y; x = 0 * y; */
6153 : 449483 : else if (subcode != MINUS_EXPR && integer_zerop (arg0))
6154 : 0 : result = subcode == MULT_EXPR ? integer_zero_node : arg1;
6155 : : /* x = y - y; */
6156 : 449483 : else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
6157 : 7 : result = integer_zero_node;
6158 : : /* x = y * 1; x = 1 * y; */
6159 : 449476 : else if (subcode == MULT_EXPR && integer_onep (arg1))
6160 : : result = arg0;
6161 : 446145 : else if (subcode == MULT_EXPR && integer_onep (arg0))
6162 : : result = arg1;
6163 : 490670 : if (result)
6164 : : {
6165 : 14068 : if (result == integer_zero_node)
6166 : 2140 : result = build_zero_cst (type);
6167 : 11928 : else if (cplx_result && TREE_TYPE (result) != type)
6168 : : {
6169 : 9610 : if (TREE_CODE (result) == INTEGER_CST)
6170 : : {
6171 : 0 : if (arith_overflowed_p (PLUS_EXPR, type, result,
6172 : : integer_zero_node))
6173 : 0 : overflow = build_one_cst (type);
6174 : : }
6175 : 9610 : else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
6176 : 6920 : && TYPE_UNSIGNED (type))
6177 : 9755 : || (TYPE_PRECISION (type)
6178 : 2835 : < (TYPE_PRECISION (TREE_TYPE (result))
6179 : 2835 : + (TYPE_UNSIGNED (TREE_TYPE (result))
6180 : 3163 : && !TYPE_UNSIGNED (type)))))
6181 : : result = NULL_TREE;
6182 : 66 : if (result)
6183 : 66 : result = fold_convert (type, result);
6184 : : }
6185 : : }
6186 : : }
6187 : :
6188 : 1639472 : if (result)
6189 : : {
6190 : 26025 : if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
6191 : 0 : result = drop_tree_overflow (result);
6192 : 26025 : if (cplx_result)
6193 : : {
6194 : 4513 : if (overflow == NULL_TREE)
6195 : 4513 : overflow = build_zero_cst (TREE_TYPE (result));
6196 : 4513 : tree ctype = build_complex_type (TREE_TYPE (result));
6197 : 4513 : if (TREE_CODE (result) == INTEGER_CST
6198 : 2140 : && TREE_CODE (overflow) == INTEGER_CST)
6199 : 2140 : result = build_complex (ctype, result, overflow);
6200 : : else
6201 : 2373 : result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
6202 : : ctype, result, overflow);
6203 : : }
6204 : 26025 : gimplify_and_update_call_from_tree (gsi, result);
6205 : 26025 : changed = true;
6206 : : }
6207 : : }
6208 : :
6209 : : return changed;
6210 : : }
6211 : :
6212 : :
6213 : : /* Return true whether NAME has a use on STMT. Note this can return
6214 : : false even though there's a use on STMT if SSA operands are not
6215 : : up-to-date. */
6216 : :
6217 : : static bool
6218 : 1599 : has_use_on_stmt (tree name, gimple *stmt)
6219 : : {
6220 : 1599 : ssa_op_iter iter;
6221 : 1599 : tree op;
6222 : 3223 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6223 : 1670 : if (op == name)
6224 : : return true;
6225 : : return false;
6226 : : }
6227 : :
6228 : : /* Add the lhs of each statement of SEQ to DCE_WORKLIST. */
6229 : :
6230 : : void
6231 : 4443162 : mark_lhs_in_seq_for_dce (bitmap dce_worklist, gimple_seq seq)
6232 : : {
6233 : 4443162 : if (!dce_worklist)
6234 : : return;
6235 : :
6236 : 1583162 : for (gimple_stmt_iterator i = gsi_start (seq);
6237 : 1842443 : !gsi_end_p (i); gsi_next (&i))
6238 : : {
6239 : 259281 : gimple *stmt = gsi_stmt (i);
6240 : 259281 : tree name = gimple_get_lhs (stmt);
6241 : 259281 : if (name && TREE_CODE (name) == SSA_NAME)
6242 : 259281 : bitmap_set_bit (dce_worklist, SSA_NAME_VERSION (name));
6243 : : }
6244 : : }
6245 : :
6246 : : /* Worker for fold_stmt_1 dispatch to pattern based folding with
6247 : : gimple_simplify.
6248 : :
6249 : : Replaces *GSI with the simplification result in RCODE and OPS
6250 : : and the associated statements in *SEQ. Does the replacement
6251 : : according to INPLACE and returns true if the operation succeeded. */
6252 : :
6253 : : static bool
6254 : 8859198 : replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
6255 : : gimple_match_op *res_op,
6256 : : gimple_seq *seq, bool inplace,
6257 : : bitmap dce_worklist)
6258 : : {
6259 : 8859198 : gimple *stmt = gsi_stmt (*gsi);
6260 : 8859198 : tree *ops = res_op->ops;
6261 : 8859198 : unsigned int num_ops = res_op->num_ops;
6262 : :
6263 : : /* Play safe and do not allow abnormals to be mentioned in
6264 : : newly created statements. See also maybe_push_res_to_seq.
6265 : : As an exception allow such uses if there was a use of the
6266 : : same SSA name on the old stmt. */
6267 : 19578609 : for (unsigned int i = 0; i < num_ops; ++i)
6268 : 10720964 : if (TREE_CODE (ops[i]) == SSA_NAME
6269 : 6653530 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
6270 : 10722563 : && !has_use_on_stmt (ops[i], stmt))
6271 : : return false;
6272 : :
6273 : 8857645 : if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
6274 : 0 : for (unsigned int i = 0; i < 2; ++i)
6275 : 0 : if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
6276 : 0 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
6277 : 0 : && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
6278 : : return false;
6279 : :
6280 : : /* Don't insert new statements when INPLACE is true, even if we could
6281 : : reuse STMT for the final statement. */
6282 : 8857645 : if (inplace && !gimple_seq_empty_p (*seq))
6283 : : return false;
6284 : :
6285 : 8857645 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
6286 : : {
6287 : 6753089 : gcc_assert (res_op->code.is_tree_code ());
6288 : 6753089 : auto code = tree_code (res_op->code);
6289 : 6753089 : if (TREE_CODE_CLASS (code) == tcc_comparison
6290 : : /* GIMPLE_CONDs condition may not throw. */
6291 : 6753089 : && ((cfun
6292 : 1086707 : && (!flag_exceptions
6293 : 737657 : || !cfun->can_throw_non_call_exceptions))
6294 : 286335 : || !operation_could_trap_p (code,
6295 : 286335 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6296 : : false, NULL_TREE)))
6297 : 1075453 : gimple_cond_set_condition (cond_stmt, code, ops[0], ops[1]);
6298 : 5677636 : else if (code == SSA_NAME)
6299 : : {
6300 : : /* If setting the gimple cond to the same thing,
6301 : : return false as nothing changed. */
6302 : 4030204 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6303 : 4005378 : && operand_equal_p (gimple_cond_lhs (cond_stmt), ops[0])
6304 : 8033023 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6305 : : return false;
6306 : 27385 : gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
6307 : 27385 : build_zero_cst (TREE_TYPE (ops[0])));
6308 : : }
6309 : 1647432 : else if (code == INTEGER_CST)
6310 : : {
6311 : : /* Make into the canonical form `1 != 0` and `0 != 0`.
6312 : : If already in the canonical form return false
6313 : : saying nothing has been done. */
6314 : 1114606 : if (integer_zerop (ops[0]))
6315 : : {
6316 : 5267452 : if (gimple_cond_false_canonical_p (cond_stmt))
6317 : : return false;
6318 : 463341 : gimple_cond_make_false (cond_stmt);
6319 : : }
6320 : : else
6321 : : {
6322 : 338812 : if (gimple_cond_true_canonical_p (cond_stmt))
6323 : : return false;
6324 : 176062 : gimple_cond_make_true (cond_stmt);
6325 : : }
6326 : : }
6327 : 532826 : else if (!inplace)
6328 : : {
6329 : : /* For throwing comparisons, see if the GIMPLE_COND is the same as
6330 : : the comparison would be.
6331 : : This can happen due to the match pattern for
6332 : : `(ne (cmp @0 @1) integer_zerop)` which creates a new expression
6333 : : for the comparison. */
6334 : 532826 : if (TREE_CODE_CLASS (code) == tcc_comparison
6335 : 11254 : && (!cfun
6336 : 11254 : || (flag_exceptions
6337 : 11254 : && cfun->can_throw_non_call_exceptions))
6338 : 544080 : && operation_could_trap_p (code,
6339 : 11254 : FLOAT_TYPE_P (TREE_TYPE (ops[0])),
6340 : : false, NULL_TREE))
6341 : : {
6342 : 11254 : tree lhs = gimple_cond_lhs (cond_stmt);
6343 : 11254 : if (gimple_cond_code (cond_stmt) == NE_EXPR
6344 : 11254 : && TREE_CODE (lhs) == SSA_NAME
6345 : 11254 : && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6346 : 22508 : && integer_zerop (gimple_cond_rhs (cond_stmt)))
6347 : : {
6348 : 11254 : gimple *s = SSA_NAME_DEF_STMT (lhs);
6349 : 11254 : if (is_gimple_assign (s)
6350 : 11254 : && gimple_assign_rhs_code (s) == code
6351 : 11254 : && operand_equal_p (gimple_assign_rhs1 (s), ops[0])
6352 : 22508 : && operand_equal_p (gimple_assign_rhs2 (s), ops[1]))
6353 : : return false;
6354 : : }
6355 : : }
6356 : 521572 : tree res = maybe_push_res_to_seq (res_op, seq);
6357 : 521572 : if (!res)
6358 : : return false;
6359 : 521572 : gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
6360 : 521572 : build_zero_cst (TREE_TYPE (res)));
6361 : : }
6362 : : else
6363 : : return false;
6364 : 2263813 : if (dump_file && (dump_flags & TDF_DETAILS))
6365 : : {
6366 : 858 : fprintf (dump_file, "gimple_simplified to ");
6367 : 858 : if (!gimple_seq_empty_p (*seq))
6368 : 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6369 : 858 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6370 : : 0, TDF_SLIM);
6371 : : }
6372 : : // Mark the lhs of the new statements maybe for dce
6373 : 2263813 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6374 : 2263813 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6375 : 2263813 : return true;
6376 : : }
6377 : 2104556 : else if (is_gimple_assign (stmt)
6378 : 2104556 : && res_op->code.is_tree_code ())
6379 : : {
6380 : 2030031 : auto code = tree_code (res_op->code);
6381 : 2030031 : if (!inplace
6382 : 2030031 : || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (code))
6383 : : {
6384 : 2030031 : maybe_build_generic_op (res_op);
6385 : 4787059 : gimple_assign_set_rhs_with_ops (gsi, code,
6386 : : res_op->op_or_null (0),
6387 : : res_op->op_or_null (1),
6388 : : res_op->op_or_null (2));
6389 : 2030031 : if (dump_file && (dump_flags & TDF_DETAILS))
6390 : : {
6391 : 15187 : fprintf (dump_file, "gimple_simplified to ");
6392 : 15187 : if (!gimple_seq_empty_p (*seq))
6393 : 46 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6394 : 15187 : print_gimple_stmt (dump_file, gsi_stmt (*gsi),
6395 : : 0, TDF_SLIM);
6396 : : }
6397 : : // Mark the lhs of the new statements maybe for dce
6398 : 2030031 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6399 : 2030031 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6400 : 2030031 : return true;
6401 : : }
6402 : : }
6403 : 74525 : else if (res_op->code.is_fn_code ()
6404 : 74525 : && gimple_call_combined_fn (stmt) == combined_fn (res_op->code))
6405 : : {
6406 : 7769 : gcc_assert (num_ops == gimple_call_num_args (stmt));
6407 : 23115 : for (unsigned int i = 0; i < num_ops; ++i)
6408 : 15346 : gimple_call_set_arg (stmt, i, ops[i]);
6409 : 7769 : if (dump_file && (dump_flags & TDF_DETAILS))
6410 : : {
6411 : 0 : fprintf (dump_file, "gimple_simplified to ");
6412 : 0 : if (!gimple_seq_empty_p (*seq))
6413 : 0 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6414 : 0 : print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
6415 : : }
6416 : : // Mark the lhs of the new statements maybe for dce
6417 : 7769 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6418 : 7769 : gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
6419 : 7769 : return true;
6420 : : }
6421 : 66756 : else if (!inplace)
6422 : : {
6423 : 131360 : if (gimple_has_lhs (stmt))
6424 : : {
6425 : 66756 : tree lhs = gimple_get_lhs (stmt);
6426 : 66756 : if (!maybe_push_res_to_seq (res_op, seq, lhs))
6427 : : return false;
6428 : 65927 : if (dump_file && (dump_flags & TDF_DETAILS))
6429 : : {
6430 : 10 : fprintf (dump_file, "gimple_simplified to ");
6431 : 10 : print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
6432 : : }
6433 : : // Mark the lhs of the new statements maybe for dce
6434 : 65927 : mark_lhs_in_seq_for_dce (dce_worklist, *seq);
6435 : 65927 : gsi_replace_with_seq_vops (gsi, *seq);
6436 : 65927 : return true;
6437 : : }
6438 : : else
6439 : 0 : gcc_unreachable ();
6440 : : }
6441 : :
6442 : : return false;
6443 : : }
6444 : :
6445 : : /* Canonicalize MEM_REFs invariant address operand after propagation. */
6446 : :
6447 : : static bool
6448 : 191056698 : maybe_canonicalize_mem_ref_addr (tree *t, bool is_debug = false)
6449 : : {
6450 : 191056698 : bool res = false;
6451 : 191056698 : tree *orig_t = t;
6452 : :
6453 : 191056698 : if (TREE_CODE (*t) == ADDR_EXPR)
6454 : 64171157 : t = &TREE_OPERAND (*t, 0);
6455 : :
6456 : : /* The C and C++ frontends use an ARRAY_REF for indexing with their
6457 : : generic vector extension. The actual vector referenced is
6458 : : view-converted to an array type for this purpose. If the index
6459 : : is constant the canonical representation in the middle-end is a
6460 : : BIT_FIELD_REF so re-write the former to the latter here. */
6461 : 191056698 : if (TREE_CODE (*t) == ARRAY_REF
6462 : 10597402 : && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
6463 : 149163 : && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
6464 : 191102165 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
6465 : : {
6466 : 16586 : tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
6467 : 16586 : if (VECTOR_TYPE_P (vtype))
6468 : : {
6469 : 16586 : tree low = array_ref_low_bound (*t);
6470 : 16586 : if (TREE_CODE (low) == INTEGER_CST)
6471 : : {
6472 : 16586 : if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
6473 : : {
6474 : 33128 : widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
6475 : 33128 : wi::to_widest (low));
6476 : 16564 : idx = wi::mul (idx, wi::to_widest
6477 : 33128 : (TYPE_SIZE (TREE_TYPE (*t))));
6478 : 16564 : widest_int ext
6479 : 16564 : = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
6480 : 16564 : if (maybe_le (ext, wi::to_poly_widest (TYPE_SIZE (vtype))))
6481 : : {
6482 : 32532 : *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
6483 : 16266 : TREE_TYPE (*t),
6484 : 16266 : TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
6485 : 16266 : TYPE_SIZE (TREE_TYPE (*t)),
6486 : 16266 : wide_int_to_tree (bitsizetype, idx));
6487 : 16266 : res = true;
6488 : : }
6489 : 16564 : }
6490 : : }
6491 : : }
6492 : : }
6493 : :
6494 : 358585678 : while (handled_component_p (*t))
6495 : 167528980 : t = &TREE_OPERAND (*t, 0);
6496 : :
6497 : : /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
6498 : : of invariant addresses into a SSA name MEM_REF address. */
6499 : 191056698 : if (TREE_CODE (*t) == MEM_REF
6500 : 191056698 : || TREE_CODE (*t) == TARGET_MEM_REF)
6501 : : {
6502 : 101381839 : tree addr = TREE_OPERAND (*t, 0);
6503 : 101381839 : if (TREE_CODE (addr) == ADDR_EXPR
6504 : 101381839 : && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
6505 : 31598083 : || handled_component_p (TREE_OPERAND (addr, 0))))
6506 : : {
6507 : 543357 : tree base;
6508 : 543357 : poly_int64 coffset;
6509 : 543357 : base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
6510 : : &coffset);
6511 : 543357 : if (!base)
6512 : : {
6513 : 18 : if (is_debug)
6514 : 18 : return false;
6515 : 0 : gcc_unreachable ();
6516 : : }
6517 : :
6518 : 543339 : TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
6519 : 543339 : TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
6520 : 543339 : TREE_OPERAND (*t, 1),
6521 : 543339 : size_int (coffset));
6522 : 543339 : res = true;
6523 : : }
6524 : 101381821 : gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
6525 : : || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
6526 : : }
6527 : :
6528 : : /* Canonicalize back MEM_REFs to plain reference trees if the object
6529 : : accessed is a decl that has the same access semantics as the MEM_REF. */
6530 : 191056680 : if (TREE_CODE (*t) == MEM_REF
6531 : 99471250 : && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
6532 : 31290786 : && integer_zerop (TREE_OPERAND (*t, 1))
6533 : 207794657 : && MR_DEPENDENCE_CLIQUE (*t) == 0)
6534 : : {
6535 : 11666142 : tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6536 : 11666142 : tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
6537 : 11666142 : if (/* Same volatile qualification. */
6538 : 11666142 : TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
6539 : : /* Same TBAA behavior with -fstrict-aliasing. */
6540 : 11663050 : && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
6541 : 11343237 : && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
6542 : 11343237 : == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
6543 : : /* Same alignment. */
6544 : 4949801 : && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
6545 : : /* We have to look out here to not drop a required conversion
6546 : : from the rhs to the lhs if *t appears on the lhs or vice-versa
6547 : : if it appears on the rhs. Thus require strict type
6548 : : compatibility. */
6549 : 16220242 : && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
6550 : : {
6551 : 2544050 : *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
6552 : 2544050 : res = true;
6553 : : }
6554 : : }
6555 : :
6556 : 179390538 : else if (TREE_CODE (*orig_t) == ADDR_EXPR
6557 : 61634138 : && TREE_CODE (*t) == MEM_REF
6558 : 200852309 : && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
6559 : : {
6560 : 782 : tree base;
6561 : 782 : poly_int64 coffset;
6562 : 782 : base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
6563 : : &coffset);
6564 : 782 : if (base)
6565 : : {
6566 : 687 : gcc_assert (TREE_CODE (base) == MEM_REF);
6567 : 687 : poly_int64 moffset;
6568 : 687 : if (mem_ref_offset (base).to_shwi (&moffset))
6569 : : {
6570 : 687 : coffset += moffset;
6571 : 687 : if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
6572 : : {
6573 : 687 : coffset += moffset;
6574 : 687 : *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
6575 : 687 : return true;
6576 : : }
6577 : : }
6578 : : }
6579 : : }
6580 : :
6581 : : /* Canonicalize TARGET_MEM_REF in particular with respect to
6582 : : the indexes becoming constant. */
6583 : 179389756 : else if (TREE_CODE (*t) == TARGET_MEM_REF)
6584 : : {
6585 : 1910571 : tree tem = maybe_fold_tmr (*t);
6586 : 1910571 : if (tem)
6587 : : {
6588 : 1647 : *t = tem;
6589 : 1647 : if (TREE_CODE (*orig_t) == ADDR_EXPR)
6590 : 0 : recompute_tree_invariant_for_addr_expr (*orig_t);
6591 : : res = true;
6592 : : }
6593 : : }
6594 : :
6595 : : return res;
6596 : : }
6597 : :
6598 : : /* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
6599 : : distinguishes both cases. */
6600 : :
6601 : : static bool
6602 : 747828870 : fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree),
6603 : : bitmap dce_worklist = nullptr)
6604 : : {
6605 : 747828870 : bool changed = false;
6606 : 747828870 : gimple *stmt = gsi_stmt (*gsi);
6607 : 747828870 : bool nowarning = warning_suppressed_p (stmt, OPT_Wstrict_overflow);
6608 : 747828870 : unsigned i;
6609 : 747828870 : fold_defer_overflow_warnings ();
6610 : :
6611 : : /* First do required canonicalization of [TARGET_]MEM_REF addresses
6612 : : after propagation.
6613 : : ??? This shouldn't be done in generic folding but in the
6614 : : propagation helpers which also know whether an address was
6615 : : propagated.
6616 : : Also canonicalize operand order. */
6617 : 747828870 : switch (gimple_code (stmt))
6618 : : {
6619 : 253996388 : case GIMPLE_ASSIGN:
6620 : 253996388 : if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
6621 : : {
6622 : 167974690 : tree *rhs = gimple_assign_rhs1_ptr (stmt);
6623 : 167974690 : if ((REFERENCE_CLASS_P (*rhs)
6624 : 106147768 : || TREE_CODE (*rhs) == ADDR_EXPR)
6625 : 182913190 : && maybe_canonicalize_mem_ref_addr (rhs))
6626 : : changed = true;
6627 : 167974690 : tree *lhs = gimple_assign_lhs_ptr (stmt);
6628 : 167974690 : if (REFERENCE_CLASS_P (*lhs)
6629 : 167974690 : && maybe_canonicalize_mem_ref_addr (lhs))
6630 : : changed = true;
6631 : : /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
6632 : : This cannot be done in maybe_canonicalize_mem_ref_addr
6633 : : as the gimple now has two operands rather than one.
6634 : : The same reason why this can't be done in
6635 : : maybe_canonicalize_mem_ref_addr is the same reason why
6636 : : this can't be done inplace. */
6637 : 167974690 : if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
6638 : : {
6639 : 14735374 : tree inner = TREE_OPERAND (*rhs, 0);
6640 : 14735374 : if (TREE_CODE (inner) == MEM_REF
6641 : 1087006 : && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
6642 : 14792403 : && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6643 : : {
6644 : 57029 : tree ptr = TREE_OPERAND (inner, 0);
6645 : 57029 : tree addon = TREE_OPERAND (inner, 1);
6646 : 57029 : addon = fold_convert (sizetype, addon);
6647 : 57029 : gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
6648 : : ptr, addon);
6649 : 57029 : changed = true;
6650 : 57029 : stmt = gsi_stmt (*gsi);
6651 : : }
6652 : : }
6653 : : }
6654 : : else
6655 : : {
6656 : : /* Canonicalize operand order. */
6657 : 86021698 : enum tree_code code = gimple_assign_rhs_code (stmt);
6658 : 86021698 : if (TREE_CODE_CLASS (code) == tcc_comparison
6659 : 80075251 : || commutative_tree_code (code)
6660 : 129323507 : || commutative_ternary_tree_code (code))
6661 : : {
6662 : 42720756 : tree rhs1 = gimple_assign_rhs1 (stmt);
6663 : 42720756 : tree rhs2 = gimple_assign_rhs2 (stmt);
6664 : 42720756 : if (tree_swap_operands_p (rhs1, rhs2))
6665 : : {
6666 : 3258916 : gimple_assign_set_rhs1 (stmt, rhs2);
6667 : 3258916 : gimple_assign_set_rhs2 (stmt, rhs1);
6668 : 3258916 : if (TREE_CODE_CLASS (code) == tcc_comparison)
6669 : 310773 : gimple_assign_set_rhs_code (stmt,
6670 : : swap_tree_comparison (code));
6671 : : changed = true;
6672 : : }
6673 : : }
6674 : : }
6675 : : break;
6676 : 55630975 : case GIMPLE_CALL:
6677 : 55630975 : {
6678 : 55630975 : gcall *call = as_a<gcall *> (stmt);
6679 : 167965421 : for (i = 0; i < gimple_call_num_args (call); ++i)
6680 : : {
6681 : 112334446 : tree *arg = gimple_call_arg_ptr (call, i);
6682 : 112334446 : if (REFERENCE_CLASS_P (*arg)
6683 : 112334446 : && maybe_canonicalize_mem_ref_addr (arg))
6684 : : changed = true;
6685 : : }
6686 : 55630975 : tree *lhs = gimple_call_lhs_ptr (call);
6687 : 55630975 : if (*lhs
6688 : 22716896 : && REFERENCE_CLASS_P (*lhs)
6689 : 55765891 : && maybe_canonicalize_mem_ref_addr (lhs))
6690 : : changed = true;
6691 : 55630975 : if (*lhs)
6692 : : {
6693 : 22716896 : combined_fn cfn = gimple_call_combined_fn (call);
6694 : 22716896 : internal_fn ifn = associated_internal_fn (cfn, TREE_TYPE (*lhs));
6695 : 22716896 : int opno = first_commutative_argument (ifn);
6696 : 22716896 : if (opno >= 0)
6697 : : {
6698 : 351490 : tree arg1 = gimple_call_arg (call, opno);
6699 : 351490 : tree arg2 = gimple_call_arg (call, opno + 1);
6700 : 351490 : if (tree_swap_operands_p (arg1, arg2))
6701 : : {
6702 : 23163 : gimple_call_set_arg (call, opno, arg2);
6703 : 23163 : gimple_call_set_arg (call, opno + 1, arg1);
6704 : 23163 : changed = true;
6705 : : }
6706 : : }
6707 : : }
6708 : : break;
6709 : : }
6710 : 567576 : case GIMPLE_ASM:
6711 : 567576 : {
6712 : 567576 : gasm *asm_stmt = as_a <gasm *> (stmt);
6713 : 1182977 : for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
6714 : : {
6715 : 615401 : tree link = gimple_asm_output_op (asm_stmt, i);
6716 : 615401 : tree op = TREE_VALUE (link);
6717 : 615401 : if (REFERENCE_CLASS_P (op)
6718 : 615401 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6719 : : changed = true;
6720 : : }
6721 : 945290 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
6722 : : {
6723 : 377714 : tree link = gimple_asm_input_op (asm_stmt, i);
6724 : 377714 : tree op = TREE_VALUE (link);
6725 : 377714 : if ((REFERENCE_CLASS_P (op)
6726 : 369354 : || TREE_CODE (op) == ADDR_EXPR)
6727 : 410133 : && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
6728 : : changed = true;
6729 : : }
6730 : : }
6731 : : break;
6732 : 372755837 : case GIMPLE_DEBUG:
6733 : 372755837 : if (gimple_debug_bind_p (stmt))
6734 : : {
6735 : 284505557 : tree *val = gimple_debug_bind_get_value_ptr (stmt);
6736 : 284505557 : if (*val
6737 : 162651921 : && (REFERENCE_CLASS_P (*val)
6738 : 160204007 : || TREE_CODE (*val) == ADDR_EXPR)
6739 : 336153709 : && maybe_canonicalize_mem_ref_addr (val, true))
6740 : : changed = true;
6741 : : }
6742 : : break;
6743 : 44282336 : case GIMPLE_COND:
6744 : 44282336 : {
6745 : : /* Canonicalize operand order. */
6746 : 44282336 : tree lhs = gimple_cond_lhs (stmt);
6747 : 44282336 : tree rhs = gimple_cond_rhs (stmt);
6748 : 44282336 : if (tree_swap_operands_p (lhs, rhs))
6749 : : {
6750 : 1411949 : gcond *gc = as_a <gcond *> (stmt);
6751 : 1411949 : gimple_cond_set_lhs (gc, rhs);
6752 : 1411949 : gimple_cond_set_rhs (gc, lhs);
6753 : 1411949 : gimple_cond_set_code (gc,
6754 : : swap_tree_comparison (gimple_cond_code (gc)));
6755 : 1411949 : changed = true;
6756 : : }
6757 : : }
6758 : 746303764 : default:;
6759 : : }
6760 : :
6761 : : /* Dispatch to pattern-based folding. */
6762 : 746303764 : if (!inplace
6763 : 3224464 : || is_gimple_assign (stmt)
6764 : 747202726 : || gimple_code (stmt) == GIMPLE_COND)
6765 : : {
6766 : 746929908 : gimple_seq seq = NULL;
6767 : 746929908 : gimple_match_op res_op;
6768 : 1491534314 : if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
6769 : : valueize, valueize))
6770 : : {
6771 : 8859198 : if (replace_stmt_with_simplification (gsi, &res_op, &seq, inplace,
6772 : : dce_worklist))
6773 : : changed = true;
6774 : : else
6775 : 4491658 : gimple_seq_discard (seq);
6776 : : }
6777 : : }
6778 : :
6779 : 747828870 : stmt = gsi_stmt (*gsi);
6780 : :
6781 : : /* Fold the main computation performed by the statement. */
6782 : 747828870 : switch (gimple_code (stmt))
6783 : : {
6784 : 254049221 : case GIMPLE_ASSIGN:
6785 : 254049221 : {
6786 : : /* Try to canonicalize for boolean-typed X the comparisons
6787 : : X == 0, X == 1, X != 0, and X != 1. */
6788 : 254049221 : if (gimple_assign_rhs_code (stmt) == EQ_EXPR
6789 : 254049221 : || gimple_assign_rhs_code (stmt) == NE_EXPR)
6790 : : {
6791 : 3255430 : tree lhs = gimple_assign_lhs (stmt);
6792 : 3255430 : tree op1 = gimple_assign_rhs1 (stmt);
6793 : 3255430 : tree op2 = gimple_assign_rhs2 (stmt);
6794 : 3255430 : tree type = TREE_TYPE (op1);
6795 : :
6796 : : /* Check whether the comparison operands are of the same boolean
6797 : : type as the result type is.
6798 : : Check that second operand is an integer-constant with value
6799 : : one or zero. */
6800 : 3255430 : if (TREE_CODE (op2) == INTEGER_CST
6801 : 2201048 : && (integer_zerop (op2) || integer_onep (op2))
6802 : 4887167 : && useless_type_conversion_p (TREE_TYPE (lhs), type))
6803 : : {
6804 : 4652 : enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
6805 : 4652 : bool is_logical_not = false;
6806 : :
6807 : : /* X == 0 and X != 1 is a logical-not.of X
6808 : : X == 1 and X != 0 is X */
6809 : 3924 : if ((cmp_code == EQ_EXPR && integer_zerop (op2))
6810 : 4652 : || (cmp_code == NE_EXPR && integer_onep (op2)))
6811 : 4610 : is_logical_not = true;
6812 : :
6813 : 4652 : if (is_logical_not == false)
6814 : 42 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
6815 : : /* Only for one-bit precision typed X the transformation
6816 : : !X -> ~X is valied. */
6817 : 4610 : else if (TYPE_PRECISION (type) == 1)
6818 : 4610 : gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
6819 : : /* Otherwise we use !X -> X ^ 1. */
6820 : : else
6821 : 0 : gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
6822 : : build_int_cst (type, 1));
6823 : : changed = true;
6824 : : break;
6825 : : }
6826 : : }
6827 : :
6828 : 254044569 : unsigned old_num_ops = gimple_num_ops (stmt);
6829 : 254044569 : tree lhs = gimple_assign_lhs (stmt);
6830 : 254044569 : tree new_rhs = fold_gimple_assign (gsi);
6831 : 254044569 : if (new_rhs
6832 : 254161512 : && !useless_type_conversion_p (TREE_TYPE (lhs),
6833 : 116943 : TREE_TYPE (new_rhs)))
6834 : 0 : new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
6835 : 254044569 : if (new_rhs
6836 : 254044569 : && (!inplace
6837 : 895 : || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
6838 : : {
6839 : 116943 : gimple_assign_set_rhs_from_tree (gsi, new_rhs);
6840 : 116943 : changed = true;
6841 : : }
6842 : : break;
6843 : : }
6844 : :
6845 : 55578142 : case GIMPLE_CALL:
6846 : 55578142 : changed |= gimple_fold_call (gsi, inplace);
6847 : 55578142 : break;
6848 : :
6849 : 372755837 : case GIMPLE_DEBUG:
6850 : 372755837 : if (gimple_debug_bind_p (stmt))
6851 : : {
6852 : 284505557 : tree val = gimple_debug_bind_get_value (stmt);
6853 : 284505557 : if (val && REFERENCE_CLASS_P (val))
6854 : : {
6855 : 2446004 : tree tem = maybe_fold_reference (val);
6856 : 2446004 : if (tem)
6857 : : {
6858 : 951 : gimple_debug_bind_set_value (stmt, tem);
6859 : 951 : changed = true;
6860 : : }
6861 : : }
6862 : : }
6863 : : break;
6864 : :
6865 : 10640844 : case GIMPLE_RETURN:
6866 : 10640844 : {
6867 : 10640844 : greturn *ret_stmt = as_a<greturn *> (stmt);
6868 : 10640844 : tree ret = gimple_return_retval(ret_stmt);
6869 : :
6870 : 10640844 : if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
6871 : : {
6872 : 4445565 : tree val = valueize (ret);
6873 : 4445565 : if (val && val != ret
6874 : 4445565 : && may_propagate_copy (ret, val))
6875 : : {
6876 : 0 : gimple_return_set_retval (ret_stmt, val);
6877 : 0 : changed = true;
6878 : : }
6879 : : }
6880 : : }
6881 : : break;
6882 : :
6883 : 747828870 : default:;
6884 : : }
6885 : :
6886 : 747828870 : stmt = gsi_stmt (*gsi);
6887 : :
6888 : 747828870 : fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
6889 : 747828870 : return changed;
6890 : : }
6891 : :
6892 : : /* Valueziation callback that ends up not following SSA edges. */
6893 : :
6894 : : tree
6895 : 5167551016 : no_follow_ssa_edges (tree)
6896 : : {
6897 : 5167551016 : return NULL_TREE;
6898 : : }
6899 : :
6900 : : /* Valueization callback that ends up following single-use SSA edges only. */
6901 : :
6902 : : tree
6903 : 803292696 : follow_single_use_edges (tree val)
6904 : : {
6905 : 803292696 : if (TREE_CODE (val) == SSA_NAME
6906 : 803292696 : && !has_single_use (val))
6907 : 409090354 : return NULL_TREE;
6908 : : return val;
6909 : : }
6910 : :
6911 : : /* Valueization callback that follows all SSA edges. */
6912 : :
6913 : : tree
6914 : 176513378 : follow_all_ssa_edges (tree val)
6915 : : {
6916 : 176513378 : return val;
6917 : : }
6918 : :
6919 : : /* Fold the statement pointed to by GSI. In some cases, this function may
6920 : : replace the whole statement with a new one. Returns true iff folding
6921 : : makes any changes.
6922 : : The statement pointed to by GSI should be in valid gimple form but may
6923 : : be in unfolded state as resulting from for example constant propagation
6924 : : which can produce *&x = 0. */
6925 : :
6926 : : bool
6927 : 145626853 : fold_stmt (gimple_stmt_iterator *gsi, bitmap dce_bitmap)
6928 : : {
6929 : 145626853 : return fold_stmt_1 (gsi, false, no_follow_ssa_edges, dce_bitmap);
6930 : : }
6931 : :
6932 : : bool
6933 : 598977553 : fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree), bitmap dce_bitmap)
6934 : : {
6935 : 598977553 : return fold_stmt_1 (gsi, false, valueize, dce_bitmap);
6936 : : }
6937 : :
6938 : : /* Perform the minimal folding on statement *GSI. Only operations like
6939 : : *&x created by constant propagation are handled. The statement cannot
6940 : : be replaced with a new one. Return true if the statement was
6941 : : changed, false otherwise.
6942 : : The statement *GSI should be in valid gimple form but may
6943 : : be in unfolded state as resulting from for example constant propagation
6944 : : which can produce *&x = 0. */
6945 : :
6946 : : bool
6947 : 3224464 : fold_stmt_inplace (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
6948 : : {
6949 : 3224464 : gimple *stmt = gsi_stmt (*gsi);
6950 : 3224464 : bool changed = fold_stmt_1 (gsi, true, valueize);
6951 : 3224464 : gcc_assert (gsi_stmt (*gsi) == stmt);
6952 : 3224464 : return changed;
6953 : : }
6954 : :
6955 : : /* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
6956 : : if EXPR is null or we don't know how.
6957 : : If non-null, the result always has boolean type. */
6958 : :
6959 : : static tree
6960 : 227399 : canonicalize_bool (tree expr, bool invert)
6961 : : {
6962 : 227399 : if (!expr)
6963 : : return NULL_TREE;
6964 : 56 : else if (invert)
6965 : : {
6966 : 42 : if (integer_nonzerop (expr))
6967 : 0 : return boolean_false_node;
6968 : 42 : else if (integer_zerop (expr))
6969 : 0 : return boolean_true_node;
6970 : 42 : else if (TREE_CODE (expr) == SSA_NAME)
6971 : 0 : return fold_build2 (EQ_EXPR, boolean_type_node, expr,
6972 : : build_int_cst (TREE_TYPE (expr), 0));
6973 : 42 : else if (COMPARISON_CLASS_P (expr))
6974 : 42 : return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
6975 : : boolean_type_node,
6976 : : TREE_OPERAND (expr, 0),
6977 : : TREE_OPERAND (expr, 1));
6978 : : else
6979 : : return NULL_TREE;
6980 : : }
6981 : : else
6982 : : {
6983 : 14 : if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
6984 : : return expr;
6985 : 0 : if (integer_nonzerop (expr))
6986 : 0 : return boolean_true_node;
6987 : 0 : else if (integer_zerop (expr))
6988 : 0 : return boolean_false_node;
6989 : 0 : else if (TREE_CODE (expr) == SSA_NAME)
6990 : 0 : return fold_build2 (NE_EXPR, boolean_type_node, expr,
6991 : : build_int_cst (TREE_TYPE (expr), 0));
6992 : 0 : else if (COMPARISON_CLASS_P (expr))
6993 : 0 : return fold_build2 (TREE_CODE (expr),
6994 : : boolean_type_node,
6995 : : TREE_OPERAND (expr, 0),
6996 : : TREE_OPERAND (expr, 1));
6997 : : else
6998 : : return NULL_TREE;
6999 : : }
7000 : : }
7001 : :
7002 : : /* Check to see if a boolean expression EXPR is logically equivalent to the
7003 : : comparison (OP1 CODE OP2). Check for various identities involving
7004 : : SSA_NAMEs. */
7005 : :
7006 : : static bool
7007 : 2120 : same_bool_comparison_p (const_tree expr, enum tree_code code,
7008 : : const_tree op1, const_tree op2)
7009 : : {
7010 : 2120 : gimple *s;
7011 : :
7012 : : /* The obvious case. */
7013 : 2120 : if (TREE_CODE (expr) == code
7014 : 49 : && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
7015 : 2169 : && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
7016 : : return true;
7017 : :
7018 : : /* Check for comparing (name, name != 0) and the case where expr
7019 : : is an SSA_NAME with a definition matching the comparison. */
7020 : 2103 : if (TREE_CODE (expr) == SSA_NAME
7021 : 2103 : && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
7022 : : {
7023 : 0 : if (operand_equal_p (expr, op1, 0))
7024 : 0 : return ((code == NE_EXPR && integer_zerop (op2))
7025 : 0 : || (code == EQ_EXPR && integer_nonzerop (op2)));
7026 : 0 : s = SSA_NAME_DEF_STMT (expr);
7027 : 0 : if (is_gimple_assign (s)
7028 : 0 : && gimple_assign_rhs_code (s) == code
7029 : 0 : && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
7030 : 0 : && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
7031 : : return true;
7032 : : }
7033 : :
7034 : : /* If op1 is of the form (name != 0) or (name == 0), and the definition
7035 : : of name is a comparison, recurse. */
7036 : 2103 : if (TREE_CODE (op1) == SSA_NAME
7037 : 2103 : && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
7038 : : {
7039 : 466 : s = SSA_NAME_DEF_STMT (op1);
7040 : 466 : if (is_gimple_assign (s)
7041 : 466 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
7042 : : {
7043 : 0 : enum tree_code c = gimple_assign_rhs_code (s);
7044 : 0 : if ((c == NE_EXPR && integer_zerop (op2))
7045 : 0 : || (c == EQ_EXPR && integer_nonzerop (op2)))
7046 : 0 : return same_bool_comparison_p (expr, c,
7047 : 0 : gimple_assign_rhs1 (s),
7048 : 0 : gimple_assign_rhs2 (s));
7049 : 0 : if ((c == EQ_EXPR && integer_zerop (op2))
7050 : 0 : || (c == NE_EXPR && integer_nonzerop (op2)))
7051 : 0 : return same_bool_comparison_p (expr,
7052 : : invert_tree_comparison (c, false),
7053 : 0 : gimple_assign_rhs1 (s),
7054 : 0 : gimple_assign_rhs2 (s));
7055 : : }
7056 : : }
7057 : : return false;
7058 : : }
7059 : :
7060 : : /* Check to see if two boolean expressions OP1 and OP2 are logically
7061 : : equivalent. */
7062 : :
7063 : : static bool
7064 : 23 : same_bool_result_p (const_tree op1, const_tree op2)
7065 : : {
7066 : : /* Simple cases first. */
7067 : 23 : if (operand_equal_p (op1, op2, 0))
7068 : : return true;
7069 : :
7070 : : /* Check the cases where at least one of the operands is a comparison.
7071 : : These are a bit smarter than operand_equal_p in that they apply some
7072 : : identifies on SSA_NAMEs. */
7073 : 16 : if (COMPARISON_CLASS_P (op2)
7074 : 32 : && same_bool_comparison_p (op1, TREE_CODE (op2),
7075 : 16 : TREE_OPERAND (op2, 0),
7076 : 16 : TREE_OPERAND (op2, 1)))
7077 : : return true;
7078 : 16 : if (COMPARISON_CLASS_P (op1)
7079 : 32 : && same_bool_comparison_p (op2, TREE_CODE (op1),
7080 : 16 : TREE_OPERAND (op1, 0),
7081 : 16 : TREE_OPERAND (op1, 1)))
7082 : : return true;
7083 : :
7084 : : /* Default case. */
7085 : : return false;
7086 : : }
7087 : :
7088 : : /* Forward declarations for some mutually recursive functions. */
7089 : :
7090 : : static tree
7091 : : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7092 : : enum tree_code code2, tree op2a, tree op2b, basic_block);
7093 : : static tree
7094 : : and_var_with_comparison (tree type, tree var, bool invert,
7095 : : enum tree_code code2, tree op2a, tree op2b,
7096 : : basic_block);
7097 : : static tree
7098 : : and_var_with_comparison_1 (tree type, gimple *stmt,
7099 : : enum tree_code code2, tree op2a, tree op2b,
7100 : : basic_block);
7101 : : static tree
7102 : : or_comparisons_1 (tree, enum tree_code code1, tree op1a, tree op1b,
7103 : : enum tree_code code2, tree op2a, tree op2b,
7104 : : basic_block);
7105 : : static tree
7106 : : or_var_with_comparison (tree, tree var, bool invert,
7107 : : enum tree_code code2, tree op2a, tree op2b,
7108 : : basic_block);
7109 : : static tree
7110 : : or_var_with_comparison_1 (tree, gimple *stmt,
7111 : : enum tree_code code2, tree op2a, tree op2b,
7112 : : basic_block);
7113 : :
7114 : : /* Helper function for and_comparisons_1: try to simplify the AND of the
7115 : : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
7116 : : If INVERT is true, invert the value of the VAR before doing the AND.
7117 : : Return NULL_EXPR if we can't simplify this to a single expression. */
7118 : :
7119 : : static tree
7120 : 187232 : and_var_with_comparison (tree type, tree var, bool invert,
7121 : : enum tree_code code2, tree op2a, tree op2b,
7122 : : basic_block outer_cond_bb)
7123 : : {
7124 : 187232 : tree t;
7125 : 187232 : gimple *stmt = SSA_NAME_DEF_STMT (var);
7126 : :
7127 : : /* We can only deal with variables whose definitions are assignments. */
7128 : 187232 : if (!is_gimple_assign (stmt))
7129 : : return NULL_TREE;
7130 : :
7131 : : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
7132 : : !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
7133 : : Then we only have to consider the simpler non-inverted cases. */
7134 : 186588 : if (invert)
7135 : 87429 : t = or_var_with_comparison_1 (type, stmt,
7136 : : invert_tree_comparison (code2, false),
7137 : : op2a, op2b, outer_cond_bb);
7138 : : else
7139 : 99159 : t = and_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
7140 : : outer_cond_bb);
7141 : 186588 : return canonicalize_bool (t, invert);
7142 : : }
7143 : :
7144 : : /* Try to simplify the AND of the ssa variable defined by the assignment
7145 : : STMT with the comparison specified by (OP2A CODE2 OP2B).
7146 : : Return NULL_EXPR if we can't simplify this to a single expression. */
7147 : :
7148 : : static tree
7149 : 120820 : and_var_with_comparison_1 (tree type, gimple *stmt,
7150 : : enum tree_code code2, tree op2a, tree op2b,
7151 : : basic_block outer_cond_bb)
7152 : : {
7153 : 120820 : tree var = gimple_assign_lhs (stmt);
7154 : 120820 : tree true_test_var = NULL_TREE;
7155 : 120820 : tree false_test_var = NULL_TREE;
7156 : 120820 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
7157 : :
7158 : : /* Check for identities like (var AND (var == 0)) => false. */
7159 : 120820 : if (TREE_CODE (op2a) == SSA_NAME
7160 : 120820 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
7161 : : {
7162 : 9206 : if ((code2 == NE_EXPR && integer_zerop (op2b))
7163 : 25976 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
7164 : : {
7165 : 8076 : true_test_var = op2a;
7166 : 8076 : if (var == true_test_var)
7167 : : return var;
7168 : : }
7169 : 6099 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
7170 : 19926 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
7171 : : {
7172 : 3622 : false_test_var = op2a;
7173 : 3622 : if (var == false_test_var)
7174 : 0 : return boolean_false_node;
7175 : : }
7176 : : }
7177 : :
7178 : : /* If the definition is a comparison, recurse on it. */
7179 : 120820 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
7180 : : {
7181 : 4043 : tree t = and_comparisons_1 (type, innercode,
7182 : : gimple_assign_rhs1 (stmt),
7183 : : gimple_assign_rhs2 (stmt),
7184 : : code2,
7185 : : op2a,
7186 : : op2b, outer_cond_bb);
7187 : 4043 : if (t)
7188 : : return t;
7189 : : }
7190 : :
7191 : : /* If the definition is an AND or OR expression, we may be able to
7192 : : simplify by reassociating. */
7193 : 120814 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
7194 : 120814 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
7195 : : {
7196 : 15332 : tree inner1 = gimple_assign_rhs1 (stmt);
7197 : 15332 : tree inner2 = gimple_assign_rhs2 (stmt);
7198 : 15332 : gimple *s;
7199 : 15332 : tree t;
7200 : 15332 : tree partial = NULL_TREE;
7201 : 15332 : bool is_and = (innercode == BIT_AND_EXPR);
7202 : :
7203 : : /* Check for boolean identities that don't require recursive examination
7204 : : of inner1/inner2:
7205 : : inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
7206 : : inner1 AND (inner1 OR inner2) => inner1
7207 : : !inner1 AND (inner1 AND inner2) => false
7208 : : !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
7209 : : Likewise for similar cases involving inner2. */
7210 : 15332 : if (inner1 == true_test_var)
7211 : 0 : return (is_and ? var : inner1);
7212 : 15332 : else if (inner2 == true_test_var)
7213 : 0 : return (is_and ? var : inner2);
7214 : 15332 : else if (inner1 == false_test_var)
7215 : 0 : return (is_and
7216 : 0 : ? boolean_false_node
7217 : 0 : : and_var_with_comparison (type, inner2, false, code2, op2a,
7218 : 0 : op2b, outer_cond_bb));
7219 : 15332 : else if (inner2 == false_test_var)
7220 : 0 : return (is_and
7221 : 0 : ? boolean_false_node
7222 : 0 : : and_var_with_comparison (type, inner1, false, code2, op2a,
7223 : 0 : op2b, outer_cond_bb));
7224 : :
7225 : : /* Next, redistribute/reassociate the AND across the inner tests.
7226 : : Compute the first partial result, (inner1 AND (op2a code op2b)) */
7227 : 15332 : if (TREE_CODE (inner1) == SSA_NAME
7228 : 15332 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
7229 : 14418 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7230 : 28767 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7231 : : gimple_assign_rhs1 (s),
7232 : : gimple_assign_rhs2 (s),
7233 : : code2, op2a, op2b,
7234 : : outer_cond_bb)))
7235 : : {
7236 : : /* Handle the AND case, where we are reassociating:
7237 : : (inner1 AND inner2) AND (op2a code2 op2b)
7238 : : => (t AND inner2)
7239 : : If the partial result t is a constant, we win. Otherwise
7240 : : continue on to try reassociating with the other inner test. */
7241 : 63 : if (is_and)
7242 : : {
7243 : 6 : if (integer_onep (t))
7244 : : return inner2;
7245 : 6 : else if (integer_zerop (t))
7246 : 0 : return boolean_false_node;
7247 : : }
7248 : :
7249 : : /* Handle the OR case, where we are redistributing:
7250 : : (inner1 OR inner2) AND (op2a code2 op2b)
7251 : : => (t OR (inner2 AND (op2a code2 op2b))) */
7252 : 57 : else if (integer_onep (t))
7253 : 0 : return boolean_true_node;
7254 : :
7255 : : /* Save partial result for later. */
7256 : : partial = t;
7257 : : }
7258 : :
7259 : : /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
7260 : 15332 : if (TREE_CODE (inner2) == SSA_NAME
7261 : 15332 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
7262 : 14956 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
7263 : 29077 : && (t = maybe_fold_and_comparisons (type, gimple_assign_rhs_code (s),
7264 : : gimple_assign_rhs1 (s),
7265 : : gimple_assign_rhs2 (s),
7266 : : code2, op2a, op2b,
7267 : : outer_cond_bb)))
7268 : : {
7269 : : /* Handle the AND case, where we are reassociating:
7270 : : (inner1 AND inner2) AND (op2a code2 op2b)
7271 : : => (inner1 AND t) */
7272 : 446 : if (is_and)
7273 : : {
7274 : 19 : if (integer_onep (t))
7275 : : return inner1;
7276 : 19 : else if (integer_zerop (t))
7277 : 1 : return boolean_false_node;
7278 : : /* If both are the same, we can apply the identity
7279 : : (x AND x) == x. */
7280 : 18 : else if (partial && same_bool_result_p (t, partial))
7281 : : return t;
7282 : : }
7283 : :
7284 : : /* Handle the OR case. where we are redistributing:
7285 : : (inner1 OR inner2) AND (op2a code2 op2b)
7286 : : => (t OR (inner1 AND (op2a code2 op2b)))
7287 : : => (t OR partial) */
7288 : : else
7289 : : {
7290 : 427 : if (integer_onep (t))
7291 : 0 : return boolean_true_node;
7292 : 427 : else if (partial)
7293 : : {
7294 : : /* We already got a simplification for the other
7295 : : operand to the redistributed OR expression. The
7296 : : interesting case is when at least one is false.
7297 : : Or, if both are the same, we can apply the identity
7298 : : (x OR x) == x. */
7299 : 14 : if (integer_zerop (partial))
7300 : : return t;
7301 : 14 : else if (integer_zerop (t))
7302 : : return partial;
7303 : 12 : else if (same_bool_result_p (t, partial))
7304 : : return t;
7305 : : }
7306 : : }
7307 : : }
7308 : : }
7309 : : return NULL_TREE;
7310 : : }
7311 : :
7312 : : /* Try to simplify the AND of two comparisons defined by
7313 : : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
7314 : : If this can be done without constructing an intermediate value,
7315 : : return the resulting tree; otherwise NULL_TREE is returned.
7316 : : This function is deliberately asymmetric as it recurses on SSA_DEFs
7317 : : in the first comparison but not the second. */
7318 : :
7319 : : static tree
7320 : 859143 : and_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
7321 : : enum tree_code code2, tree op2a, tree op2b,
7322 : : basic_block outer_cond_bb)
7323 : : {
7324 : 859143 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
7325 : :
7326 : : /* First check for ((x CODE1 y) AND (x CODE2 y)). */
7327 : 859143 : if (operand_equal_p (op1a, op2a, 0)
7328 : 859143 : && operand_equal_p (op1b, op2b, 0))
7329 : : {
7330 : : /* Result will be either NULL_TREE, or a combined comparison. */
7331 : 3775 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7332 : : TRUTH_ANDIF_EXPR, code1, code2,
7333 : : truth_type, op1a, op1b);
7334 : 3775 : if (t)
7335 : : return t;
7336 : : }
7337 : :
7338 : : /* Likewise the swapped case of the above. */
7339 : 858184 : if (operand_equal_p (op1a, op2b, 0)
7340 : 858184 : && operand_equal_p (op1b, op2a, 0))
7341 : : {
7342 : : /* Result will be either NULL_TREE, or a combined comparison. */
7343 : 0 : tree t = combine_comparisons (UNKNOWN_LOCATION,
7344 : : TRUTH_ANDIF_EXPR, code1,
7345 : : swap_tree_comparison (code2),
7346 : : truth_type, op1a, op1b);
7347 : 0 : if (t)
7348 : : return t;
7349 : : }
7350 : :
7351 : : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
7352 : : NAME's definition is a truth value. See if there are any simplifications
7353 : : that can be done against the NAME's definition. */
7354 : 858184 : if (TREE_CODE (op1a) == SSA_NAME
7355 : 858165 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
7356 : 1446663 : && (integer_zerop (op1b) || integer_onep (op1b)))
7357 : : {
7358 : 122571 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
7359 : 272422 : || (code1 == NE_EXPR && integer_onep (op1b)));
7360 : 248014 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
7361 : 248014 : switch (gimple_code (stmt))
7362 : : {
7363 : 182370 : case GIMPLE_ASSIGN:
7364 : : /* Try to simplify by copy-propagating the definition. */
7365 : 182370 : return and_var_with_comparison (type, op1a, invert, code2, op2a,
7366 : 182370 : op2b, outer_cond_bb);
7367 : :
7368 : 32965 : case GIMPLE_PHI:
7369 : : /* If every argument to the PHI produces the same result when
7370 : : ANDed with the second comparison, we win.
7371 : : Do not do this unless the type is bool since we need a bool
7372 : : result here anyway. */
7373 : 32965 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
7374 : : {
7375 : : tree result = NULL_TREE;
7376 : : unsigned i;
7377 : 15837 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
7378 : : {
7379 : 15837 : tree arg = gimple_phi_arg_def (stmt, i);
7380 : :
7381 : : /* If this PHI has itself as an argument, ignore it.
7382 : : If all the other args produce the same result,
7383 : : we're still OK. */
7384 : 15837 : if (arg == gimple_phi_result (stmt))
7385 : 0 : continue;
7386 : 15837 : else if (TREE_CODE (arg) == INTEGER_CST)
7387 : : {
7388 : 9879 : if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
7389 : : {
7390 : 5823 : if (!result)
7391 : 2394 : result = boolean_false_node;
7392 : 3429 : else if (!integer_zerop (result))
7393 : : return NULL_TREE;
7394 : : }
7395 : 4056 : else if (!result)
7396 : 2210 : result = fold_build2 (code2, boolean_type_node,
7397 : : op2a, op2b);
7398 : 1846 : else if (!same_bool_comparison_p (result,
7399 : : code2, op2a, op2b))
7400 : : return NULL_TREE;
7401 : : }
7402 : 5958 : else if (TREE_CODE (arg) == SSA_NAME
7403 : 5958 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
7404 : : {
7405 : 5955 : tree temp;
7406 : 5955 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
7407 : : /* In simple cases we can look through PHI nodes,
7408 : : but we have to be careful with loops.
7409 : : See PR49073. */
7410 : 5955 : if (! dom_info_available_p (CDI_DOMINATORS)
7411 : 5955 : || gimple_bb (def_stmt) == gimple_bb (stmt)
7412 : 11910 : || dominated_by_p (CDI_DOMINATORS,
7413 : 5955 : gimple_bb (def_stmt),
7414 : 5955 : gimple_bb (stmt)))
7415 : 1093 : return NULL_TREE;
7416 : 4862 : temp = and_var_with_comparison (type, arg, invert, code2,
7417 : : op2a, op2b,
7418 : : outer_cond_bb);
7419 : 4862 : if (!temp)
7420 : : return NULL_TREE;
7421 : 0 : else if (!result)
7422 : : result = temp;
7423 : 0 : else if (!same_bool_result_p (result, temp))
7424 : : return NULL_TREE;
7425 : : }
7426 : : else
7427 : : return NULL_TREE;
7428 : : }
7429 : : return result;
7430 : : }
7431 : :
7432 : : default:
7433 : : break;
7434 : : }
7435 : : }
7436 : : return NULL_TREE;
7437 : : }
7438 : :
7439 : : static basic_block fosa_bb;
7440 : : static vec<std::pair<tree, flow_sensitive_info_storage> > *fosa_unwind;
7441 : : static tree
7442 : 29043695 : follow_outer_ssa_edges (tree val)
7443 : : {
7444 : 29043695 : if (TREE_CODE (val) == SSA_NAME
7445 : 29043695 : && !SSA_NAME_IS_DEFAULT_DEF (val))
7446 : : {
7447 : 28554116 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (val));
7448 : 28554116 : if (!def_bb
7449 : 8845104 : || def_bb == fosa_bb
7450 : 33451391 : || (dom_info_available_p (CDI_DOMINATORS)
7451 : 4897275 : && (def_bb == fosa_bb
7452 : 4897275 : || dominated_by_p (CDI_DOMINATORS, fosa_bb, def_bb))))
7453 : 26199295 : return val;
7454 : : /* We cannot temporarily rewrite stmts with undefined overflow
7455 : : behavior, so avoid expanding them. */
7456 : 4683515 : if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (val))
7457 : 275049 : || POINTER_TYPE_P (TREE_TYPE (val)))
7458 : 4580125 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
7459 : : return NULL_TREE;
7460 : 1158263 : flow_sensitive_info_storage storage;
7461 : 1158263 : storage.save_and_clear (val);
7462 : : /* If the definition does not dominate fosa_bb temporarily reset
7463 : : flow-sensitive info. */
7464 : 1158263 : fosa_unwind->safe_push (std::make_pair (val, storage));
7465 : 1158263 : return val;
7466 : : }
7467 : : return val;
7468 : : }
7469 : :
7470 : : /* Helper function for maybe_fold_and_comparisons and maybe_fold_or_comparisons
7471 : : : try to simplify the AND/OR of the ssa variable VAR with the comparison
7472 : : specified by (OP2A CODE2 OP2B) from match.pd. Return NULL_EXPR if we can't
7473 : : simplify this to a single expression. As we are going to lower the cost
7474 : : of building SSA names / gimple stmts significantly, we need to allocate
7475 : : them ont the stack. This will cause the code to be a bit ugly. */
7476 : :
7477 : : static tree
7478 : 917012 : maybe_fold_comparisons_from_match_pd (tree type, enum tree_code code,
7479 : : enum tree_code code1,
7480 : : tree op1a, tree op1b,
7481 : : enum tree_code code2, tree op2a,
7482 : : tree op2b,
7483 : : basic_block outer_cond_bb)
7484 : : {
7485 : : /* Allocate gimple stmt1 on the stack. */
7486 : 917012 : gassign *stmt1
7487 : 917012 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7488 : 917012 : gimple_init (stmt1, GIMPLE_ASSIGN, 3);
7489 : 917012 : gimple_assign_set_rhs_code (stmt1, code1);
7490 : 917012 : gimple_assign_set_rhs1 (stmt1, op1a);
7491 : 917012 : gimple_assign_set_rhs2 (stmt1, op1b);
7492 : 917012 : gimple_set_bb (stmt1, NULL);
7493 : :
7494 : : /* Allocate gimple stmt2 on the stack. */
7495 : 917012 : gassign *stmt2
7496 : 917012 : = (gassign *) XALLOCAVEC (char, gimple_size (GIMPLE_ASSIGN, 3));
7497 : 917012 : gimple_init (stmt2, GIMPLE_ASSIGN, 3);
7498 : 917012 : gimple_assign_set_rhs_code (stmt2, code2);
7499 : 917012 : gimple_assign_set_rhs1 (stmt2, op2a);
7500 : 917012 : gimple_assign_set_rhs2 (stmt2, op2b);
7501 : 917012 : gimple_set_bb (stmt2, NULL);
7502 : :
7503 : : /* Allocate SSA names(lhs1) on the stack. */
7504 : 917012 : alignas (tree_node) unsigned char lhs1buf[sizeof (tree_ssa_name)];
7505 : 917012 : tree lhs1 = (tree) &lhs1buf[0];
7506 : 917012 : memset (lhs1, 0, sizeof (tree_ssa_name));
7507 : 917012 : TREE_SET_CODE (lhs1, SSA_NAME);
7508 : 917012 : TREE_TYPE (lhs1) = type;
7509 : 917012 : init_ssa_name_imm_use (lhs1);
7510 : :
7511 : : /* Allocate SSA names(lhs2) on the stack. */
7512 : 917012 : alignas (tree_node) unsigned char lhs2buf[sizeof (tree_ssa_name)];
7513 : 917012 : tree lhs2 = (tree) &lhs2buf[0];
7514 : 917012 : memset (lhs2, 0, sizeof (tree_ssa_name));
7515 : 917012 : TREE_SET_CODE (lhs2, SSA_NAME);
7516 : 917012 : TREE_TYPE (lhs2) = type;
7517 : 917012 : init_ssa_name_imm_use (lhs2);
7518 : :
7519 : 917012 : gimple_assign_set_lhs (stmt1, lhs1);
7520 : 917012 : gimple_assign_set_lhs (stmt2, lhs2);
7521 : :
7522 : 917012 : gimple_match_op op (gimple_match_cond::UNCOND, code,
7523 : : type, gimple_assign_lhs (stmt1),
7524 : 917012 : gimple_assign_lhs (stmt2));
7525 : 917012 : fosa_bb = outer_cond_bb;
7526 : 917012 : auto_vec<std::pair<tree, flow_sensitive_info_storage>, 8> unwind_stack;
7527 : 917012 : fosa_unwind = &unwind_stack;
7528 : 1262844 : if (op.resimplify (NULL, (!outer_cond_bb
7529 : : ? follow_all_ssa_edges : follow_outer_ssa_edges)))
7530 : : {
7531 : 2448 : fosa_unwind = NULL;
7532 : 8635 : for (auto p : unwind_stack)
7533 : 1291 : p.second.restore (p.first);
7534 : 2448 : if (gimple_simplified_result_is_gimple_val (&op))
7535 : : {
7536 : 1851 : tree res = op.ops[0];
7537 : 1851 : if (res == lhs1)
7538 : 1495 : return build2 (code1, type, op1a, op1b);
7539 : 356 : else if (res == lhs2)
7540 : 328 : return build2 (code2, type, op2a, op2b);
7541 : : else
7542 : : return res;
7543 : : }
7544 : 597 : else if (op.code.is_tree_code ()
7545 : 597 : && TREE_CODE_CLASS ((tree_code)op.code) == tcc_comparison)
7546 : : {
7547 : 597 : tree op0 = op.ops[0];
7548 : 597 : tree op1 = op.ops[1];
7549 : 597 : if (op0 == lhs1 || op0 == lhs2 || op1 == lhs1 || op1 == lhs2)
7550 : : return NULL_TREE; /* not simple */
7551 : :
7552 : 597 : return build2 ((enum tree_code)op.code, op.type, op0, op1);
7553 : : }
7554 : : }
7555 : 914564 : fosa_unwind = NULL;
7556 : 3900664 : for (auto p : unwind_stack)
7557 : 1156972 : p.second.restore (p.first);
7558 : :
7559 : 914564 : return NULL_TREE;
7560 : 917012 : }
7561 : :
7562 : : /* Return TRUE and set op[0] if T, following all SSA edges, is a type
7563 : : conversion. Reject loads if LOAD is NULL, otherwise set *LOAD if a
7564 : : converting load is found. */
7565 : :
7566 : : static bool
7567 : 1536505 : gimple_convert_def_p (tree t, tree op[1], gimple **load = NULL)
7568 : : {
7569 : 1536505 : bool ret = false;
7570 : :
7571 : 1536505 : if (TREE_CODE (t) == SSA_NAME
7572 : 1536505 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7573 : 872730 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7574 : : {
7575 : 785272 : bool load_p = gimple_assign_load_p (def);
7576 : 785272 : if (load_p && !load)
7577 : : return false;
7578 : 601138 : switch (gimple_assign_rhs_code (def))
7579 : : {
7580 : 10150 : CASE_CONVERT:
7581 : 10150 : op[0] = gimple_assign_rhs1 (def);
7582 : 10150 : ret = true;
7583 : 10150 : break;
7584 : :
7585 : 1779 : case VIEW_CONVERT_EXPR:
7586 : 1779 : op[0] = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
7587 : 1779 : ret = true;
7588 : 1779 : break;
7589 : :
7590 : : default:
7591 : : break;
7592 : : }
7593 : :
7594 : 11929 : if (ret && load_p)
7595 : 0 : *load = def;
7596 : : }
7597 : :
7598 : : return ret;
7599 : : }
7600 : :
7601 : : /* Return TRUE and set op[*] if T, following all SSA edges, resolves to a
7602 : : binary expression with code CODE. */
7603 : :
7604 : : static bool
7605 : 1560099 : gimple_binop_def_p (enum tree_code code, tree t, tree op[2])
7606 : : {
7607 : 1560099 : if (TREE_CODE (t) == SSA_NAME
7608 : 1560099 : && !SSA_NAME_IS_DEFAULT_DEF (t))
7609 : 895566 : if (gimple *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (t)))
7610 : 806264 : if (gimple_assign_rhs_code (def) == code)
7611 : : {
7612 : 40940 : op[0] = gimple_assign_rhs1 (def);
7613 : 40940 : op[1] = gimple_assign_rhs2 (def);
7614 : 40940 : return true;
7615 : : }
7616 : : return false;
7617 : : }
7618 : : /* Subroutine for fold_truth_andor_1: decode a field reference.
7619 : :
7620 : : If *PEXP is a comparison reference, we return the innermost reference.
7621 : :
7622 : : *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
7623 : : set to the starting bit number.
7624 : :
7625 : : *PVOLATILEP is set to 1 if the any expression encountered is volatile;
7626 : : otherwise it is not changed.
7627 : :
7628 : : *PUNSIGNEDP is set to the signedness of the field.
7629 : :
7630 : : *PREVERSEP is set to the storage order of the field.
7631 : :
7632 : : *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. If
7633 : : *PAND_MASK is initially set to a mask with nonzero precision, that mask is
7634 : : combined with the found mask, or adjusted in precision to match.
7635 : :
7636 : : *PSIGNBIT is set to TRUE if, before clipping to *PBITSIZE, the mask
7637 : : encompassed bits that corresponded to extensions of the sign bit.
7638 : :
7639 : : *PXORP is to be FALSE if EXP might be a XOR used in a compare, in which
7640 : : case, if PXOR_CMP_OP is a zero constant, it will be overridden with *PEXP,
7641 : : *PXORP will be set to TRUE, *PXOR_AND_MASK will be copied from *PAND_MASK,
7642 : : and the left-hand operand of the XOR will be decoded. If *PXORP is TRUE,
7643 : : PXOR_CMP_OP and PXOR_AND_MASK are supposed to be NULL, and then the
7644 : : right-hand operand of the XOR will be decoded.
7645 : :
7646 : : *LOAD is set to the load stmt of the innermost reference, if any,
7647 : : *and NULL otherwise.
7648 : :
7649 : : LOC[0..3] are filled in as conversion, masking, shifting and loading
7650 : : operations are located.
7651 : :
7652 : : Return 0 if this is not a component reference or is one that we can't
7653 : : do anything with. */
7654 : :
7655 : : static tree
7656 : 561940 : decode_field_reference (tree *pexp, HOST_WIDE_INT *pbitsize,
7657 : : HOST_WIDE_INT *pbitpos,
7658 : : bool *punsignedp, bool *preversep, bool *pvolatilep,
7659 : : wide_int *pand_mask, bool *psignbit,
7660 : : bool *pxorp, tree *pxor_cmp_op, wide_int *pxor_and_mask,
7661 : : gimple **pload, location_t loc[4])
7662 : : {
7663 : 561940 : tree exp = *pexp;
7664 : 561940 : tree outer_type = 0;
7665 : 561940 : wide_int and_mask;
7666 : 561940 : tree inner, offset;
7667 : 561940 : int shiftrt = 0;
7668 : 561940 : tree res_ops[2];
7669 : 561940 : machine_mode mode;
7670 : 561940 : bool convert_before_shift = false;
7671 : 561940 : bool signbit = false;
7672 : 561940 : bool xorp = false;
7673 : 561940 : tree xor_cmp_op;
7674 : 561940 : wide_int xor_and_mask;
7675 : 561940 : gimple *load = NULL;
7676 : :
7677 : : /* All the optimizations using this function assume integer fields.
7678 : : There are problems with FP fields since the type_for_size call
7679 : : below can fail for, e.g., XFmode. */
7680 : 561940 : if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
7681 : : return NULL_TREE;
7682 : :
7683 : : /* Drop casts, saving only the outermost type, effectively used in
7684 : : the compare. We can deal with at most one conversion, and it may
7685 : : appear at various points in the chain of recognized preparation
7686 : : statements. Earlier optimizers will often have already dropped
7687 : : unneeded extensions, but they may survive, as in PR118046. ???
7688 : : Can we do better and allow multiple conversions, perhaps taking
7689 : : note of the narrowest intermediate type, sign extensions and
7690 : : whatnot? */
7691 : 520037 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7692 : : {
7693 : 11665 : outer_type = TREE_TYPE (exp);
7694 : 11665 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7695 : 11665 : exp = res_ops[0];
7696 : : }
7697 : :
7698 : : /* Recognize and save a masking operation. Combine it with an
7699 : : incoming mask. */
7700 : 520037 : if (gimple_binop_def_p (BIT_AND_EXPR, exp, res_ops)
7701 : 520037 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7702 : : {
7703 : 25656 : loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
7704 : 25656 : exp = res_ops[0];
7705 : 25656 : and_mask = wi::to_wide (res_ops[1]);
7706 : 25656 : unsigned prec_in = pand_mask->get_precision ();
7707 : 25656 : if (prec_in)
7708 : : {
7709 : 52 : unsigned prec_op = and_mask.get_precision ();
7710 : 52 : if (prec_in >= prec_op)
7711 : : {
7712 : 52 : if (prec_in > prec_op)
7713 : 0 : and_mask = wide_int::from (and_mask, prec_in, UNSIGNED);
7714 : 52 : and_mask &= *pand_mask;
7715 : : }
7716 : : else
7717 : 0 : and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
7718 : : }
7719 : : }
7720 : : else
7721 : 494381 : and_mask = *pand_mask;
7722 : :
7723 : : /* Turn (a ^ b) [!]= 0 into a [!]= b. */
7724 : 520037 : if (pxorp && gimple_binop_def_p (BIT_XOR_EXPR, exp, res_ops))
7725 : : {
7726 : : /* No location recorded for this one, it's entirely subsumed by the
7727 : : compare. */
7728 : 8606 : if (*pxorp)
7729 : : {
7730 : 4294 : exp = res_ops[1];
7731 : 4294 : gcc_checking_assert (!pxor_cmp_op && !pxor_and_mask);
7732 : : }
7733 : 4312 : else if (!pxor_cmp_op)
7734 : : /* Not much we can do when xor appears in the right-hand compare
7735 : : operand. */
7736 : : return NULL_TREE;
7737 : 4300 : else if (integer_zerop (*pxor_cmp_op))
7738 : : {
7739 : 4294 : xorp = true;
7740 : 4294 : exp = res_ops[0];
7741 : 4294 : xor_cmp_op = *pexp;
7742 : 4294 : xor_and_mask = *pand_mask;
7743 : : }
7744 : : }
7745 : :
7746 : : /* Another chance to drop conversions. */
7747 : 520025 : if (!outer_type && gimple_convert_def_p (exp, res_ops))
7748 : : {
7749 : 252 : outer_type = TREE_TYPE (exp);
7750 : 252 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7751 : 252 : exp = res_ops[0];
7752 : : }
7753 : :
7754 : : /* Take note of shifts. */
7755 : 520025 : if (gimple_binop_def_p (RSHIFT_EXPR, exp, res_ops)
7756 : 520025 : && TREE_CODE (res_ops[1]) == INTEGER_CST)
7757 : : {
7758 : 322 : loc[2] = gimple_location (SSA_NAME_DEF_STMT (exp));
7759 : 322 : exp = res_ops[0];
7760 : 322 : if (!tree_fits_shwi_p (res_ops[1]))
7761 : : return NULL_TREE;
7762 : 322 : shiftrt = tree_to_shwi (res_ops[1]);
7763 : 322 : if (shiftrt <= 0)
7764 : : return NULL_TREE;
7765 : : }
7766 : :
7767 : : /* Yet another chance to drop conversions. This one is allowed to
7768 : : match a converting load, subsuming the load identification block
7769 : : below. */
7770 : 520025 : if (!outer_type && gimple_convert_def_p (exp, res_ops, &load))
7771 : : {
7772 : 12 : outer_type = TREE_TYPE (exp);
7773 : 12 : loc[0] = gimple_location (SSA_NAME_DEF_STMT (exp));
7774 : 12 : if (load)
7775 : 0 : loc[3] = gimple_location (load);
7776 : 12 : exp = res_ops[0];
7777 : : /* This looks backwards, but we're going back the def chain, so if we
7778 : : find the conversion here, after finding a shift, that's because the
7779 : : convert appears before the shift, and we should thus adjust the bit
7780 : : pos and size because of the shift after adjusting it due to type
7781 : : conversion. */
7782 : 12 : convert_before_shift = true;
7783 : : }
7784 : :
7785 : : /* Identify the load, if there is one. */
7786 : 520025 : if (!load && TREE_CODE (exp) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (exp))
7787 : : {
7788 : 297818 : gimple *def = SSA_NAME_DEF_STMT (exp);
7789 : 297818 : if (gimple_assign_load_p (def))
7790 : : {
7791 : 220615 : loc[3] = gimple_location (def);
7792 : 220615 : load = def;
7793 : 220615 : exp = gimple_assign_rhs1 (def);
7794 : : }
7795 : : }
7796 : :
7797 : : /* Identify the relevant bits. */
7798 : 520025 : poly_int64 poly_bitsize, poly_bitpos;
7799 : 520025 : int unsignedp, reversep = *preversep, volatilep = *pvolatilep;
7800 : 520025 : inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
7801 : : &mode, &unsignedp, &reversep, &volatilep);
7802 : :
7803 : 520025 : HOST_WIDE_INT bs, bp;
7804 : 520025 : if (!poly_bitsize.is_constant (&bs)
7805 : 520025 : || !poly_bitpos.is_constant (&bp)
7806 : 520025 : || bs <= shiftrt
7807 : 520025 : || offset != 0
7808 : 518725 : || TREE_CODE (inner) == PLACEHOLDER_EXPR
7809 : : /* Reject out-of-bound accesses (PR79731, PR118514). */
7810 : 518725 : || !access_in_bounds_of_type_p (TREE_TYPE (inner), bs, bp)
7811 : 518704 : || (INTEGRAL_TYPE_P (TREE_TYPE (inner))
7812 : 325637 : && !type_has_mode_precision_p (TREE_TYPE (inner))))
7813 : 34228 : return NULL_TREE;
7814 : :
7815 : : /* Adjust shifts... */
7816 : 485797 : if (convert_before_shift
7817 : 485797 : && outer_type && bs > TYPE_PRECISION (outer_type))
7818 : : {
7819 : 3 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7820 : 3 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7821 : 0 : bp += excess;
7822 : : bs -= excess;
7823 : : }
7824 : :
7825 : 485797 : if (shiftrt)
7826 : : {
7827 : : /* Punt if we're shifting by more than the loaded bitfield (after
7828 : : adjustment), or if there's a shift after a change of signedness, punt.
7829 : : When comparing this field with a constant, we'll check that the
7830 : : constant is a proper sign- or zero-extension (depending on signedness)
7831 : : of a value that would fit in the selected portion of the bitfield. A
7832 : : shift after a change of signedness would make the extension
7833 : : non-uniform, and we can't deal with that (yet ???). See
7834 : : gcc.dg/field-merge-22.c for a test that would go wrong. */
7835 : 316 : if (bs <= shiftrt
7836 : 316 : || (convert_before_shift
7837 : 12 : && outer_type && unsignedp != TYPE_UNSIGNED (outer_type)))
7838 : : return NULL_TREE;
7839 : 306 : if (!reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7840 : 306 : bp += shiftrt;
7841 : 306 : bs -= shiftrt;
7842 : : }
7843 : :
7844 : : /* ... and bit position. */
7845 : 485787 : if (!convert_before_shift
7846 : 485787 : && outer_type && bs > TYPE_PRECISION (outer_type))
7847 : : {
7848 : 5678 : HOST_WIDE_INT excess = bs - TYPE_PRECISION (outer_type);
7849 : 5678 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7850 : 0 : bp += excess;
7851 : : bs -= excess;
7852 : : }
7853 : :
7854 : : /* If the number of bits in the reference is the same as the bitsize of
7855 : : the outer type, then the outer type gives the signedness. Otherwise
7856 : : (in case of a small bitfield) the signedness is unchanged. */
7857 : 485787 : if (outer_type && bs == TYPE_PRECISION (outer_type))
7858 : 8749 : unsignedp = TYPE_UNSIGNED (outer_type);
7859 : :
7860 : : /* Make the mask the expected width. */
7861 : 485787 : if (and_mask.get_precision () != 0)
7862 : : {
7863 : : /* If the AND_MASK encompasses bits that would be extensions of
7864 : : the sign bit, set SIGNBIT. */
7865 : 29436 : if (!unsignedp
7866 : 2089 : && and_mask.get_precision () > bs
7867 : 31567 : && (and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
7868 : : signbit = true;
7869 : 29436 : and_mask = wide_int::from (and_mask, bs, UNSIGNED);
7870 : : }
7871 : :
7872 : 485787 : *pexp = exp;
7873 : 485787 : *pload = load;
7874 : 485787 : *pbitsize = bs;
7875 : 485787 : *pbitpos = bp;
7876 : 485787 : *punsignedp = unsignedp;
7877 : 485787 : *preversep = reversep;
7878 : 485787 : *pvolatilep = volatilep;
7879 : 485787 : *psignbit = signbit;
7880 : 485787 : *pand_mask = and_mask;
7881 : 485787 : if (xorp)
7882 : : {
7883 : 4294 : *pxorp = xorp;
7884 : 4294 : *pxor_cmp_op = xor_cmp_op;
7885 : 4294 : *pxor_and_mask = xor_and_mask;
7886 : : }
7887 : :
7888 : : return inner;
7889 : 561940 : }
7890 : :
7891 : : /* Return the one bitpos within bit extents L or R that is at an
7892 : : ALIGN-bit alignment boundary, or -1 if there is more than one such
7893 : : boundary, if there isn't any, or if there is any such boundary
7894 : : between the extents. L and R are given by bitpos and bitsize. If
7895 : : it doesn't return -1, there are two consecutive ALIGN-bit words
7896 : : that contain both extents, and at least one of the extents
7897 : : straddles across the returned alignment boundary. */
7898 : :
7899 : : static inline HOST_WIDE_INT
7900 : 28995 : compute_split_boundary_from_align (HOST_WIDE_INT align,
7901 : : HOST_WIDE_INT l_bitpos,
7902 : : HOST_WIDE_INT l_bitsize,
7903 : : HOST_WIDE_INT r_bitpos,
7904 : : HOST_WIDE_INT r_bitsize)
7905 : : {
7906 : 28995 : HOST_WIDE_INT amask = ~(align - 1);
7907 : :
7908 : 28995 : HOST_WIDE_INT first_bit = MIN (l_bitpos, r_bitpos);
7909 : 28995 : HOST_WIDE_INT end_bit = MAX (l_bitpos + l_bitsize, r_bitpos + r_bitsize);
7910 : :
7911 : 28995 : HOST_WIDE_INT boundary = (end_bit - 1) & amask;
7912 : :
7913 : : /* Make sure we're crossing no more than one alignment boundary.
7914 : :
7915 : : ??? We don't have logic to recombine loads of two adjacent
7916 : : fields that each crosses a different alignment boundary, so
7917 : : as to load the middle word only once, if other words can't be
7918 : : otherwise recombined. */
7919 : 28995 : if (boundary - first_bit > align)
7920 : : return -1;
7921 : :
7922 : 11339 : HOST_WIDE_INT l_start_word = l_bitpos & amask;
7923 : 11339 : HOST_WIDE_INT l_end_word = (l_bitpos + l_bitsize - 1) & amask;
7924 : :
7925 : 11339 : HOST_WIDE_INT r_start_word = r_bitpos & amask;
7926 : 11339 : HOST_WIDE_INT r_end_word = (r_bitpos + r_bitsize - 1) & amask;
7927 : :
7928 : : /* If neither field straddles across an alignment boundary, it's no
7929 : : use to even try to merge them. */
7930 : 11339 : if (l_start_word == l_end_word && r_start_word == r_end_word)
7931 : 11032 : return -1;
7932 : :
7933 : : return boundary;
7934 : : }
7935 : :
7936 : : /* Make a bit_field_ref. If POINT is NULL, return the BIT_FIELD_REF.
7937 : : Otherwise, build and insert a load stmt before POINT, and return
7938 : : the SSA_NAME. ??? Rewrite LOAD in terms of the bitfield? */
7939 : :
7940 : : static tree
7941 : 4719 : make_bit_field_load (location_t loc, tree inner, tree orig_inner, tree type,
7942 : : HOST_WIDE_INT bitsize, poly_int64 bitpos,
7943 : : bool unsignedp, bool reversep, gimple *point)
7944 : : {
7945 : 4719 : if (point && loc == UNKNOWN_LOCATION)
7946 : 18 : loc = gimple_location (point);
7947 : :
7948 : 4719 : tree ref = make_bit_field_ref (loc, unshare_expr (inner),
7949 : : unshare_expr (orig_inner),
7950 : : type, bitsize, bitpos,
7951 : : unsignedp, reversep);
7952 : 4719 : if (!point)
7953 : : return ref;
7954 : :
7955 : : /* If we're remaking the same load, reuse the SSA NAME it is already loaded
7956 : : into. */
7957 : 4574 : if (gimple_assign_load_p (point)
7958 : 4574 : && operand_equal_p (ref, gimple_assign_rhs1 (point)))
7959 : : {
7960 : 1723 : gcc_checking_assert (TREE_CODE (gimple_assign_lhs (point)) == SSA_NAME);
7961 : : return gimple_assign_lhs (point);
7962 : : }
7963 : :
7964 : 2851 : gimple_seq stmts = NULL;
7965 : 2851 : tree ret = force_gimple_operand (ref, &stmts, true, NULL_TREE);
7966 : :
7967 : : /* We know the vuse is supposed to end up being the same as that at the
7968 : : original load at the insertion point, but if we don't set it, it will be a
7969 : : generic placeholder that only the global SSA update at the end of the pass
7970 : : would make equal, too late for us to use in further combinations. So go
7971 : : ahead and copy the vuse. */
7972 : :
7973 : 2851 : tree reaching_vuse = gimple_vuse (point);
7974 : 2851 : for (gimple_stmt_iterator i = gsi_start (stmts);
7975 : 6106 : !gsi_end_p (i); gsi_next (&i))
7976 : : {
7977 : 3255 : gimple *new_stmt = gsi_stmt (i);
7978 : 6510 : if (gimple_has_mem_ops (new_stmt))
7979 : 3255 : gimple_set_vuse (new_stmt, reaching_vuse);
7980 : : }
7981 : :
7982 : 2851 : gimple_stmt_iterator gsi = gsi_for_stmt (point);
7983 : 2851 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
7984 : 2851 : return ret;
7985 : : }
7986 : :
7987 : : /* Initialize ln_arg[0] and ln_arg[1] to a pair of newly-created (at
7988 : : LOC) loads from INNER (from ORIG_INNER), of modes MODE and MODE2,
7989 : : respectively, starting at BIT_POS, using reversed endianness if
7990 : : REVERSEP. Also initialize BITPOS (the starting position of each
7991 : : part into INNER), BITSIZ (the bit count starting at BITPOS),
7992 : : TOSHIFT[1] (the amount by which the part and its mask are to be
7993 : : shifted right to bring its least-significant bit to bit zero) and
7994 : : SHIFTED (the amount by which the part, by separate loading, has
7995 : : already been shifted right, but that the mask needs shifting to
7996 : : match). */
7997 : :
7998 : : static inline void
7999 : 307 : build_split_load (tree /* out */ ln_arg[2],
8000 : : HOST_WIDE_INT /* out */ bitpos[2],
8001 : : HOST_WIDE_INT /* out */ bitsiz[2],
8002 : : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
8003 : : HOST_WIDE_INT /* out */ shifted[2],
8004 : : location_t loc, tree inner, tree orig_inner,
8005 : : scalar_int_mode mode, scalar_int_mode mode2,
8006 : : HOST_WIDE_INT bit_pos, bool reversep,
8007 : : gimple *point[2])
8008 : : {
8009 : 307 : scalar_int_mode modes[2] = { mode, mode2 };
8010 : 307 : bitsiz[0] = GET_MODE_BITSIZE (mode);
8011 : 307 : bitsiz[1] = GET_MODE_BITSIZE (mode2);
8012 : :
8013 : 921 : for (int i = 0; i < 2; i++)
8014 : : {
8015 : 614 : tree type = lang_hooks.types.type_for_mode (modes[i], 1);
8016 : 614 : if (!type)
8017 : : {
8018 : 0 : type = build_nonstandard_integer_type (bitsiz[0], 1);
8019 : 0 : gcc_assert (type);
8020 : : }
8021 : 614 : bitpos[i] = bit_pos;
8022 : 1228 : ln_arg[i] = make_bit_field_load (loc, inner, orig_inner,
8023 : 614 : type, bitsiz[i],
8024 : 614 : bit_pos, 1, reversep, point[i]);
8025 : 614 : bit_pos += bitsiz[i];
8026 : : }
8027 : :
8028 : 307 : toshift[1] = toshift[0];
8029 : 307 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8030 : : {
8031 : 3 : shifted[0] = bitsiz[1];
8032 : 3 : shifted[1] = 0;
8033 : 3 : toshift[0] = 0;
8034 : : }
8035 : : else
8036 : : {
8037 : 304 : shifted[1] = bitsiz[0];
8038 : 304 : shifted[0] = 0;
8039 : 304 : toshift[1] = 0;
8040 : : }
8041 : 307 : }
8042 : :
8043 : : /* Make arrangements to split at bit BOUNDARY a single loaded word
8044 : : (with REVERSEP bit order) LN_ARG[0], to be shifted right by
8045 : : TOSHIFT[0] to bring the field of interest to the least-significant
8046 : : bit. The expectation is that the same loaded word will be
8047 : : propagated from part 0 to part 1, with just different shifting and
8048 : : masking to extract both parts. MASK is not expected to do more
8049 : : than masking out the bits that belong to the other part. See
8050 : : build_split_load for more information on the other fields. */
8051 : :
8052 : : static inline void
8053 : 51 : reuse_split_load (tree /* in[0] out[1] */ ln_arg[2],
8054 : : HOST_WIDE_INT /* in[0] out[1] */ bitpos[2],
8055 : : HOST_WIDE_INT /* in[0] out[1] */ bitsiz[2],
8056 : : HOST_WIDE_INT /* in[0] out[0..1] */ toshift[2],
8057 : : HOST_WIDE_INT /* out */ shifted[2],
8058 : : wide_int /* out */ mask[2],
8059 : : HOST_WIDE_INT boundary, bool reversep)
8060 : : {
8061 : 51 : unsigned prec = TYPE_PRECISION (TREE_TYPE (ln_arg[0]));
8062 : :
8063 : 51 : ln_arg[1] = ln_arg[0];
8064 : 51 : bitpos[1] = bitpos[0];
8065 : 51 : bitsiz[1] = bitsiz[0];
8066 : 51 : shifted[1] = shifted[0] = 0;
8067 : :
8068 : 51 : if (reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8069 : : {
8070 : 3 : toshift[1] = toshift[0];
8071 : 3 : toshift[0] = bitpos[0] + bitsiz[0] - boundary;
8072 : 3 : mask[0] = wi::mask (toshift[0], true, prec);
8073 : 3 : mask[1] = wi::mask (toshift[0], false, prec);
8074 : : }
8075 : : else
8076 : : {
8077 : 48 : toshift[1] = boundary - bitpos[1];
8078 : 48 : mask[1] = wi::mask (toshift[1], true, prec);
8079 : 48 : mask[0] = wi::mask (toshift[1], false, prec);
8080 : : }
8081 : 51 : }
8082 : :
8083 : : /* Find ways of folding logical expressions of LHS and RHS:
8084 : :
8085 : : Try to merge two comparisons to nearby fields.
8086 : :
8087 : : For example, if we have p->a == 2 && p->b == 4 and we can load both A and B
8088 : : at once, we can do this with a comparison against the object ANDed with the
8089 : : a mask.
8090 : :
8091 : : If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
8092 : : operations to do this with one comparison, loading both fields from P at
8093 : : once, and likewise from Q.
8094 : :
8095 : : Herein, loading at once means loading from within the same alignment
8096 : : boundary for the enclosing object. If (packed) fields cross such alignment
8097 : : boundaries, we may still recombine the compares, so that loads do not cross
8098 : : the boundaries.
8099 : :
8100 : : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
8101 : : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
8102 : :
8103 : : TRUTH_TYPE is the type of the logical operand.
8104 : :
8105 : : LHS is denoted as LL_ARG LCODE LR_ARG.
8106 : :
8107 : : RHS is denoted as RL_ARG RCODE RR_ARG.
8108 : :
8109 : : LHS is assumed to dominate RHS.
8110 : :
8111 : : Combined loads are inserted next to preexisting loads, once we determine
8112 : : that the combination is viable, and the combined condition references new
8113 : : SSA_NAMEs that hold the loaded values. Since the original loads are
8114 : : verified to have the same gimple_vuse, the insertion point doesn't matter
8115 : : for correctness. ??? The loads may be a lot earlier than the compares, and
8116 : : it's conceivable that one or two loads for RHS appear before those for LHS.
8117 : : It could be advantageous to try to place the loads optimally, taking
8118 : : advantage of knowing whether RHS is accessed before LHS, or that both are
8119 : : accessed before both compares, but we don't do that (yet?).
8120 : :
8121 : : SEPARATEP should be NULL if the combined condition must be returned as a
8122 : : single expression, even if it is a compound condition. This must only be
8123 : : done if LHS and RHS are adjacent, without intervening conditions, and the
8124 : : combined condition is to replace RHS, while LHS is dropped altogether.
8125 : :
8126 : : Otherwise, SEPARATEP must be a non-NULL pointer to a NULL_TREE, that may be
8127 : : replaced by a part of the compound condition that could replace RHS, while
8128 : : the returned expression replaces LHS. This works whether or not LHS and RHS
8129 : : are adjacent, as long as there aren't VDEFs or other side effects between
8130 : : them.
8131 : :
8132 : : If the "words" accessed by RHS are already accessed by LHS, this won't
8133 : : matter, but if RHS accesses "words" that LHS doesn't, then *SEPARATEP will
8134 : : be set to the compares that should take RHS's place. By "words" we mean
8135 : : contiguous bits that do not cross a an TYPE_ALIGN boundary of the accessed
8136 : : object's type.
8137 : :
8138 : : We return the simplified tree or 0 if no optimization is possible. */
8139 : :
8140 : : tree
8141 : 254937 : fold_truth_andor_for_ifcombine (enum tree_code code, tree truth_type,
8142 : : location_t lloc, enum tree_code lcode,
8143 : : tree ll_arg, tree lr_arg,
8144 : : location_t rloc, enum tree_code rcode,
8145 : : tree rl_arg, tree rr_arg,
8146 : : tree *separatep)
8147 : : {
8148 : : /* If this is the "or" of two comparisons, we can do something if
8149 : : the comparisons are NE_EXPR. If this is the "and", we can do something
8150 : : if the comparisons are EQ_EXPR. I.e.,
8151 : : (a->b == 2 && a->c == 4) can become (a->new == NEW).
8152 : :
8153 : : WANTED_CODE is this operation code. For single bit fields, we can
8154 : : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
8155 : : comparison for one-bit fields. */
8156 : :
8157 : 254937 : enum tree_code orig_code = code;
8158 : 254937 : enum tree_code wanted_code;
8159 : 254937 : tree ll_inner, lr_inner, rl_inner, rr_inner;
8160 : 254937 : gimple *ll_load, *lr_load, *rl_load, *rr_load;
8161 : 254937 : HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
8162 : 254937 : HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
8163 : 254937 : HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
8164 : 254937 : HOST_WIDE_INT lnbitsize, lnbitpos, lnprec;
8165 : 254937 : HOST_WIDE_INT rnbitsize, rnbitpos, rnprec;
8166 : 254937 : bool ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
8167 : 254937 : bool ll_reversep, lr_reversep, rl_reversep, rr_reversep;
8168 : 254937 : bool ll_signbit, lr_signbit, rl_signbit, rr_signbit;
8169 : 254937 : scalar_int_mode lnmode, lnmode2, rnmode;
8170 : 254937 : wide_int ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
8171 : 254937 : wide_int l_const, r_const;
8172 : 254937 : tree lntype, rntype, result;
8173 : 254937 : HOST_WIDE_INT first_bit, end_bit;
8174 : 254937 : bool volatilep;
8175 : 254937 : bool l_split_load;
8176 : :
8177 : : /* These are indexed by: conv, mask, shft, load. */
8178 : 254937 : location_t ll_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
8179 : 254937 : location_t lr_loc[4] = { lloc, lloc, lloc, UNKNOWN_LOCATION };
8180 : 254937 : location_t rl_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8181 : 254937 : location_t rr_loc[4] = { rloc, rloc, rloc, UNKNOWN_LOCATION };
8182 : :
8183 : 254937 : gcc_checking_assert (!separatep || !*separatep);
8184 : :
8185 : : /* Start by getting the comparison codes. Fail if anything is volatile.
8186 : : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
8187 : : it were surrounded with a NE_EXPR. */
8188 : :
8189 : 254937 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
8190 : 254937 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
8191 : : return 0;
8192 : :
8193 : : /* We don't normally find TRUTH_*IF_EXPR in gimple, but these codes may be
8194 : : given by our caller to denote conditions from different blocks. */
8195 : 254937 : switch (code)
8196 : : {
8197 : : case TRUTH_AND_EXPR:
8198 : : case TRUTH_ANDIF_EXPR:
8199 : : code = TRUTH_AND_EXPR;
8200 : : break;
8201 : :
8202 : 0 : case TRUTH_OR_EXPR:
8203 : 0 : case TRUTH_ORIF_EXPR:
8204 : 0 : code = TRUTH_OR_EXPR;
8205 : 0 : break;
8206 : :
8207 : : default:
8208 : : return 0;
8209 : : }
8210 : :
8211 : : /* Prepare to turn compares of signed quantities with zero into sign-bit
8212 : : tests. We need not worry about *_reversep here for these compare
8213 : : rewrites: loads will have already been reversed before compares. Save the
8214 : : precision, because [lr]l_arg may change and we won't be able to tell how
8215 : : wide it was originally. */
8216 : 254937 : unsigned lsignbit = 0, rsignbit = 0;
8217 : 254937 : if ((lcode == LT_EXPR || lcode == GE_EXPR)
8218 : 14265 : && integer_zerop (lr_arg)
8219 : 3800 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8220 : 258737 : && !TYPE_UNSIGNED (TREE_TYPE (ll_arg)))
8221 : : {
8222 : 3800 : lsignbit = TYPE_PRECISION (TREE_TYPE (ll_arg));
8223 : 3800 : lcode = (lcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8224 : : }
8225 : : /* Turn compares of unsigned quantities with powers of two into
8226 : : equality tests of masks. */
8227 : 251137 : else if ((lcode == LT_EXPR || lcode == GE_EXPR)
8228 : 10465 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8229 : 7922 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8230 : 5374 : && TREE_CODE (lr_arg) == INTEGER_CST
8231 : 251137 : && wi::popcount (wi::to_wide (lr_arg)) == 1)
8232 : : {
8233 : 0 : ll_and_mask = ~(wi::to_wide (lr_arg) - 1);
8234 : 0 : lcode = (lcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8235 : 0 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8236 : : }
8237 : : /* Turn compares of unsigned quantities with powers of two minus one
8238 : : into equality tests of masks. */
8239 : 502274 : else if ((lcode == LE_EXPR || lcode == GT_EXPR)
8240 : 30083 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))
8241 : 29895 : && TYPE_UNSIGNED (TREE_TYPE (ll_arg))
8242 : 24224 : && TREE_CODE (lr_arg) == INTEGER_CST
8243 : 532357 : && wi::popcount (wi::to_wide (lr_arg) + 1) == 1)
8244 : : {
8245 : 3917 : ll_and_mask = ~wi::to_wide (lr_arg);
8246 : 3917 : lcode = (lcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8247 : 3917 : lr_arg = wide_int_to_tree (TREE_TYPE (ll_arg), ll_and_mask * 0);
8248 : : }
8249 : : /* Likewise for the second compare. */
8250 : 254937 : if ((rcode == LT_EXPR || rcode == GE_EXPR)
8251 : 22190 : && integer_zerop (rr_arg)
8252 : 1886 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8253 : 256823 : && !TYPE_UNSIGNED (TREE_TYPE (rl_arg)))
8254 : : {
8255 : 1886 : rsignbit = TYPE_PRECISION (TREE_TYPE (rl_arg));
8256 : 1886 : rcode = (rcode == LT_EXPR ? NE_EXPR : EQ_EXPR);
8257 : : }
8258 : 253051 : else if ((rcode == LT_EXPR || rcode == GE_EXPR)
8259 : 20304 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8260 : 17584 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8261 : 3753 : && TREE_CODE (rr_arg) == INTEGER_CST
8262 : 253051 : && wi::popcount (wi::to_wide (rr_arg)) == 1)
8263 : : {
8264 : 0 : rl_and_mask = ~(wi::to_wide (rr_arg) - 1);
8265 : 0 : rcode = (rcode == GE_EXPR ? NE_EXPR : EQ_EXPR);
8266 : 0 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8267 : : }
8268 : 506102 : else if ((rcode == LE_EXPR || rcode == GT_EXPR)
8269 : 39681 : && INTEGRAL_TYPE_P (TREE_TYPE (rl_arg))
8270 : 39197 : && TYPE_UNSIGNED (TREE_TYPE (rl_arg))
8271 : 28315 : && TREE_CODE (rr_arg) == INTEGER_CST
8272 : 545783 : && wi::popcount (wi::to_wide (rr_arg) + 1) == 1)
8273 : : {
8274 : 4342 : rl_and_mask = ~wi::to_wide (rr_arg);
8275 : 4342 : rcode = (rcode == GT_EXPR ? NE_EXPR : EQ_EXPR);
8276 : 4342 : rr_arg = wide_int_to_tree (TREE_TYPE (rl_arg), rl_and_mask * 0);
8277 : : }
8278 : :
8279 : : /* See if the comparisons can be merged. Then get all the parameters for
8280 : : each side. */
8281 : :
8282 : 254937 : if ((lcode != EQ_EXPR && lcode != NE_EXPR)
8283 : 217536 : || (rcode != EQ_EXPR && rcode != NE_EXPR))
8284 : : return 0;
8285 : :
8286 : 190870 : ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
8287 : 190870 : volatilep = 0;
8288 : 190870 : bool l_xor = false, r_xor = false;
8289 : 190870 : ll_inner = decode_field_reference (&ll_arg, &ll_bitsize, &ll_bitpos,
8290 : : &ll_unsignedp, &ll_reversep, &volatilep,
8291 : : &ll_and_mask, &ll_signbit,
8292 : : &l_xor, &lr_arg, &lr_and_mask,
8293 : : &ll_load, ll_loc);
8294 : 190870 : if (!ll_inner)
8295 : : return 0;
8296 : 129241 : lr_inner = decode_field_reference (&lr_arg, &lr_bitsize, &lr_bitpos,
8297 : : &lr_unsignedp, &lr_reversep, &volatilep,
8298 : : &lr_and_mask, &lr_signbit, &l_xor, 0, 0,
8299 : : &lr_load, lr_loc);
8300 : 129241 : if (!lr_inner)
8301 : : return 0;
8302 : 126255 : rl_inner = decode_field_reference (&rl_arg, &rl_bitsize, &rl_bitpos,
8303 : : &rl_unsignedp, &rl_reversep, &volatilep,
8304 : : &rl_and_mask, &rl_signbit,
8305 : : &r_xor, &rr_arg, &rr_and_mask,
8306 : : &rl_load, rl_loc);
8307 : 126255 : if (!rl_inner)
8308 : : return 0;
8309 : 115574 : rr_inner = decode_field_reference (&rr_arg, &rr_bitsize, &rr_bitpos,
8310 : : &rr_unsignedp, &rr_reversep, &volatilep,
8311 : : &rr_and_mask, &rr_signbit, &r_xor, 0, 0,
8312 : : &rr_load, rr_loc);
8313 : 115574 : if (!rr_inner)
8314 : : return 0;
8315 : :
8316 : : /* It must be true that the inner operation on the lhs of each
8317 : : comparison must be the same if we are to be able to do anything.
8318 : : Then see if we have constants. If not, the same must be true for
8319 : : the rhs's. If one is a load and the other isn't, we have to be
8320 : : conservative and avoid the optimization, otherwise we could get
8321 : : SRAed fields wrong. */
8322 : 114717 : if (volatilep)
8323 : : return 0;
8324 : :
8325 : 114717 : if (ll_reversep != rl_reversep
8326 : 114717 : || ! operand_equal_p (ll_inner, rl_inner, 0))
8327 : : {
8328 : : /* Try swapping the operands. */
8329 : 78187 : if (ll_reversep != rr_reversep || rsignbit
8330 : 155904 : || !operand_equal_p (ll_inner, rr_inner, 0))
8331 : 77035 : return 0;
8332 : :
8333 : 1179 : rcode = swap_tree_comparison (rcode);
8334 : 1179 : std::swap (rl_arg, rr_arg);
8335 : 1179 : std::swap (rl_inner, rr_inner);
8336 : 1179 : std::swap (rl_bitsize, rr_bitsize);
8337 : 1179 : std::swap (rl_bitpos, rr_bitpos);
8338 : 1179 : std::swap (rl_unsignedp, rr_unsignedp);
8339 : 1179 : std::swap (rl_reversep, rr_reversep);
8340 : 1179 : std::swap (rl_and_mask, rr_and_mask);
8341 : 1179 : std::swap (rl_signbit, rr_signbit);
8342 : 1179 : std::swap (rl_load, rr_load);
8343 : 1179 : std::swap (rl_loc, rr_loc);
8344 : : }
8345 : :
8346 : 73077 : if ((ll_load && rl_load)
8347 : 143867 : ? gimple_vuse (ll_load) != gimple_vuse (rl_load)
8348 : 2287 : : (!ll_load != !rl_load))
8349 : : return 0;
8350 : :
8351 : : /* ??? Can we do anything with these? */
8352 : 37254 : if (lr_signbit || rr_signbit)
8353 : : return 0;
8354 : :
8355 : : /* If the mask encompassed extensions of the sign bit before
8356 : : clipping, try to include the sign bit in the test. If we're not
8357 : : comparing with zero, don't even try to deal with it (for now?).
8358 : : If we've already commited to a sign test, the extended (before
8359 : : clipping) mask could already be messing with it. */
8360 : 37254 : if (ll_signbit)
8361 : : {
8362 : 4 : if (!integer_zerop (lr_arg) || lsignbit)
8363 : 0 : return 0;
8364 : 4 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8365 : 4 : if (!ll_and_mask.get_precision ())
8366 : 0 : ll_and_mask = sign;
8367 : : else
8368 : 4 : ll_and_mask |= sign;
8369 : 4 : }
8370 : :
8371 : 37254 : if (rl_signbit)
8372 : : {
8373 : 4 : if (!integer_zerop (rr_arg) || rsignbit)
8374 : 1 : return 0;
8375 : 3 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8376 : 3 : if (!rl_and_mask.get_precision ())
8377 : 0 : rl_and_mask = sign;
8378 : : else
8379 : 3 : rl_and_mask |= sign;
8380 : 3 : }
8381 : :
8382 : 37253 : if (TREE_CODE (lr_arg) == INTEGER_CST
8383 : 30454 : && TREE_CODE (rr_arg) == INTEGER_CST)
8384 : : {
8385 : 29989 : l_const = wi::to_wide (lr_arg);
8386 : : /* We don't expect masks on constants, but if there are any, apply
8387 : : them now. */
8388 : 29989 : if (lr_and_mask.get_precision ())
8389 : 0 : l_const &= wide_int::from (lr_and_mask,
8390 : 0 : l_const.get_precision (), UNSIGNED);
8391 : 29989 : r_const = wi::to_wide (rr_arg);
8392 : 29989 : if (rr_and_mask.get_precision ())
8393 : 0 : r_const &= wide_int::from (rr_and_mask,
8394 : 0 : r_const.get_precision (), UNSIGNED);
8395 : 29989 : lr_reversep = ll_reversep;
8396 : : }
8397 : 7264 : else if (lr_reversep != rr_reversep
8398 : 7264 : || ! operand_equal_p (lr_inner, rr_inner, 0)
8399 : 13288 : || ((lr_load && rr_load)
8400 : 17991 : ? gimple_vuse (lr_load) != gimple_vuse (rr_load)
8401 : 27 : : (!lr_load != !rr_load)))
8402 : 1270 : return 0;
8403 : :
8404 : : /* If we found sign tests, finish turning them into bit tests. */
8405 : :
8406 : 35983 : if (lsignbit)
8407 : : {
8408 : 60 : wide_int sign = wi::mask (ll_bitsize - 1, true, ll_bitsize);
8409 : : /* If ll_arg is zero-extended and we're testing the sign bit, we know
8410 : : what the result should be. Shifting the sign bit out of sign will get
8411 : : us to mask the entire field out, yielding zero, i.e., the sign bit of
8412 : : the zero-extended value. We know the masked value is being compared
8413 : : with zero, so the compare will get us the result we're looking
8414 : : for: TRUE if EQ_EXPR, FALSE if NE_EXPR. */
8415 : 60 : if (lsignbit > ll_bitsize && ll_unsignedp)
8416 : 1 : sign <<= 1;
8417 : 60 : if (!ll_and_mask.get_precision ())
8418 : 59 : ll_and_mask = sign;
8419 : : else
8420 : 1 : ll_and_mask &= sign;
8421 : 60 : if (l_xor)
8422 : : {
8423 : 1 : if (ll_bitsize != lr_bitsize)
8424 : 1 : return 0;
8425 : 0 : if (!lr_and_mask.get_precision ())
8426 : 0 : lr_and_mask = sign;
8427 : : else
8428 : 0 : lr_and_mask &= sign;
8429 : 0 : if (l_const.get_precision ())
8430 : 0 : l_const &= wide_int::from (lr_and_mask,
8431 : 0 : l_const.get_precision (), UNSIGNED);
8432 : : }
8433 : 60 : }
8434 : :
8435 : 35982 : if (rsignbit)
8436 : : {
8437 : 198 : wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
8438 : 198 : if (rsignbit > rl_bitsize && rl_unsignedp)
8439 : 0 : sign <<= 1;
8440 : 198 : if (!rl_and_mask.get_precision ())
8441 : 198 : rl_and_mask = sign;
8442 : : else
8443 : 0 : rl_and_mask &= sign;
8444 : 198 : if (r_xor)
8445 : : {
8446 : 16 : if (rl_bitsize != rr_bitsize)
8447 : 0 : return 0;
8448 : 16 : if (!rr_and_mask.get_precision ())
8449 : 16 : rr_and_mask = sign;
8450 : : else
8451 : 0 : rr_and_mask &= sign;
8452 : 16 : if (r_const.get_precision ())
8453 : 24 : r_const &= wide_int::from (rr_and_mask,
8454 : 12 : r_const.get_precision (), UNSIGNED);
8455 : : }
8456 : 198 : }
8457 : :
8458 : : /* If either comparison code is not correct for our logical operation,
8459 : : fail. However, we can convert a one-bit comparison against zero into
8460 : : the opposite comparison against that bit being set in the field. */
8461 : :
8462 : 35982 : wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
8463 : 35982 : if (lcode != wanted_code)
8464 : : {
8465 : 4987 : if (l_const.get_precision ()
8466 : 4869 : && l_const == 0
8467 : 1608 : && ll_and_mask.get_precision ()
8468 : 5604 : && wi::popcount (ll_and_mask) == 1)
8469 : : {
8470 : : /* Make the left operand unsigned, since we are only interested
8471 : : in the value of one bit. Otherwise we are doing the wrong
8472 : : thing below. */
8473 : 440 : ll_unsignedp = 1;
8474 : 440 : l_const = ll_and_mask;
8475 : : }
8476 : : else
8477 : 4547 : return 0;
8478 : : }
8479 : :
8480 : : /* This is analogous to the code for l_const above. */
8481 : 31435 : if (rcode != wanted_code)
8482 : : {
8483 : 992 : if (r_const.get_precision ()
8484 : 992 : && r_const == 0
8485 : 957 : && rl_and_mask.get_precision ()
8486 : 1883 : && wi::popcount (rl_and_mask) == 1)
8487 : : {
8488 : 724 : rl_unsignedp = 1;
8489 : 724 : r_const = rl_and_mask;
8490 : : }
8491 : : else
8492 : 268 : return 0;
8493 : : }
8494 : :
8495 : : /* This will be bumped to 2 if any of the field pairs crosses an
8496 : : alignment boundary, so the merged compare has to be done in two
8497 : : parts. */
8498 : 93501 : int parts = 1;
8499 : : /* Set to true if the second combined compare should come first,
8500 : : e.g., because the second original compare accesses a word that
8501 : : the first one doesn't, and the combined compares access those in
8502 : : cmp[0]. */
8503 : 93501 : bool first1 = false;
8504 : : /* Set to true if the first original compare is not the one being
8505 : : split. */
8506 : 93501 : bool maybe_separate = false;
8507 : :
8508 : : /* The following 2-dimensional arrays use the first index to
8509 : : identify left(0)- vs right(1)-hand compare operands, and the
8510 : : second one to identify merged compare parts. */
8511 : : /* The memory loads or constants to be compared. */
8512 : : tree ld_arg[2][2];
8513 : : /* The first bit of the corresponding inner object that the
8514 : : corresponding LD_ARG covers. */
8515 : : HOST_WIDE_INT bitpos[2][2];
8516 : : /* The bit count starting at BITPOS that the corresponding LD_ARG
8517 : : covers. */
8518 : : HOST_WIDE_INT bitsiz[2][2];
8519 : : /* The number of bits by which LD_ARG has already been shifted
8520 : : right, WRT mask. */
8521 : : HOST_WIDE_INT shifted[2][2];
8522 : : /* The number of bits by which both LD_ARG and MASK need shifting to
8523 : : bring its least-significant bit to bit zero. */
8524 : : HOST_WIDE_INT toshift[2][2];
8525 : : /* An additional mask to be applied to LD_ARG, to remove any bits
8526 : : that may have been loaded for use in another compare, but that
8527 : : don't belong in the corresponding compare. */
8528 : 374004 : wide_int xmask[2][2] = {};
8529 : :
8530 : : /* The combined compare or compares. */
8531 : 31167 : tree cmp[2];
8532 : :
8533 : : /* Consider we're comparing two non-contiguous fields of packed
8534 : : structs, both aligned at 32-bit boundaries:
8535 : :
8536 : : ll_arg: an 8-bit field at offset 0
8537 : : lr_arg: a 16-bit field at offset 2
8538 : :
8539 : : rl_arg: an 8-bit field at offset 1
8540 : : rr_arg: a 16-bit field at offset 3
8541 : :
8542 : : We'll have r_split_load, because rr_arg straddles across an
8543 : : alignment boundary.
8544 : :
8545 : : We'll want to have:
8546 : :
8547 : : bitpos = { { 0, 0 }, { 0, 32 } }
8548 : : bitsiz = { { 32, 32 }, { 32, 8 } }
8549 : :
8550 : : And, for little-endian:
8551 : :
8552 : : shifted = { { 0, 0 }, { 0, 32 } }
8553 : : toshift = { { 0, 24 }, { 0, 0 } }
8554 : :
8555 : : Or, for big-endian:
8556 : :
8557 : : shifted = { { 0, 0 }, { 8, 0 } }
8558 : : toshift = { { 8, 0 }, { 0, 0 } }
8559 : : */
8560 : :
8561 : : /* See if we can find a mode that contains both fields being compared on
8562 : : the left. If we can't, fail. Otherwise, update all constants and masks
8563 : : to be relative to a field of that size. */
8564 : 31167 : first_bit = MIN (ll_bitpos, rl_bitpos);
8565 : 31167 : end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
8566 : 31167 : HOST_WIDE_INT ll_align = TYPE_ALIGN (TREE_TYPE (ll_inner));
8567 : 31167 : poly_uint64 ll_end_region = 0;
8568 : 31167 : if (TYPE_SIZE (TREE_TYPE (ll_inner))
8569 : 31167 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (ll_inner))))
8570 : 31167 : ll_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (ll_inner)));
8571 : 31167 : if (get_best_mode (end_bit - first_bit, first_bit, 0, ll_end_region,
8572 : 31167 : ll_align, BITS_PER_WORD, volatilep, &lnmode))
8573 : : l_split_load = false;
8574 : : /* ??? If ll and rl share the same load, reuse that?
8575 : : See PR 118206 -> gcc.dg/field-merge-18.c */
8576 : : else
8577 : : {
8578 : : /* Consider the possibility of recombining loads if any of the
8579 : : fields straddles across an alignment boundary, so that either
8580 : : part can be loaded along with the other field. Since we
8581 : : limit access modes to BITS_PER_WORD, don't exceed that,
8582 : : otherwise on a 32-bit host and a 64-bit-aligned data
8583 : : structure, we'll fail the above for a field that straddles
8584 : : across two words, and would fail here for not even trying to
8585 : : split it at between 32-bit words. */
8586 : 27628 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8587 : 29287 : (MIN (ll_align, BITS_PER_WORD),
8588 : : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8589 : :
8590 : 27628 : if (boundary < 0
8591 : 219 : || !get_best_mode (boundary - first_bit, first_bit, 0, ll_end_region,
8592 : : ll_align, BITS_PER_WORD, volatilep, &lnmode)
8593 : 27805 : || !get_best_mode (end_bit - boundary, boundary, 0, ll_end_region,
8594 : 177 : ll_align, BITS_PER_WORD, volatilep, &lnmode2))
8595 : : {
8596 : 29068 : if (ll_align <= BITS_PER_WORD)
8597 : : return 0;
8598 : :
8599 : : /* As a last resort, try double-word access modes. This
8600 : : enables us to deal with misaligned double-word fields
8601 : : that straddle across 3 separate words. */
8602 : 1235 : boundary = compute_split_boundary_from_align
8603 : 1323 : (MIN (ll_align, 2 * BITS_PER_WORD),
8604 : : ll_bitpos, ll_bitsize, rl_bitpos, rl_bitsize);
8605 : 1235 : if (boundary < 0
8606 : 0 : || !get_best_mode (boundary - first_bit, first_bit,
8607 : : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8608 : : volatilep, &lnmode)
8609 : 1235 : || !get_best_mode (end_bit - boundary, boundary,
8610 : 0 : 0, ll_end_region, ll_align, 2 * BITS_PER_WORD,
8611 : : volatilep, &lnmode2))
8612 : 1235 : return 0;
8613 : : }
8614 : :
8615 : : /* If we can't have a single load, but can with two, figure out whether
8616 : : the two compares can be separated, i.e., whether the entirety of the
8617 : : first original compare is encompassed by the entirety of the first
8618 : : combined compare. If the first original compare is past the alignment
8619 : : boundary, arrange to compare that range first, by setting first1
8620 : : (meaning make cmp[1] first, instead of cmp[0]). */
8621 : 177 : l_split_load = true;
8622 : 177 : parts = 2;
8623 : 177 : if (ll_bitpos >= boundary)
8624 : : maybe_separate = first1 = true;
8625 : 132 : else if (ll_bitpos + ll_bitsize <= boundary)
8626 : 32 : maybe_separate = true;
8627 : : }
8628 : :
8629 : 3716 : lnbitsize = GET_MODE_BITSIZE (lnmode);
8630 : 3716 : lnbitpos = first_bit & ~ (lnbitsize - 1);
8631 : : /* Avoid situations that the code below can't handle. */
8632 : 3716 : if (lnbitpos < 0)
8633 : : return 0;
8634 : :
8635 : : /* Choose the type for the combined compare. Even if we're splitting loads,
8636 : : make it wide enough to hold both. */
8637 : 3716 : if (l_split_load)
8638 : 354 : lnbitsize += GET_MODE_BITSIZE (lnmode2);
8639 : 3716 : lntype = build_nonstandard_integer_type (lnbitsize, 1);
8640 : 3716 : if (!lntype)
8641 : : return NULL_TREE;
8642 : 3716 : lnprec = TYPE_PRECISION (lntype);
8643 : 3716 : xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
8644 : :
8645 : : /* Adjust bit ranges for reverse endianness. */
8646 : 3716 : if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8647 : : {
8648 : 6 : xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
8649 : 6 : xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
8650 : : }
8651 : :
8652 : : /* Adjust masks to match the positions in the combined lntype. */
8653 : 7432 : wide_int ll_mask, rl_mask, r_mask;
8654 : 3716 : if (ll_and_mask.get_precision ())
8655 : 4692 : ll_mask = wi::lshift (wide_int::from (ll_and_mask, lnprec, UNSIGNED),
8656 : 2346 : xll_bitpos);
8657 : : else
8658 : 1370 : ll_mask = wi::shifted_mask (xll_bitpos, ll_bitsize, false, lnprec);
8659 : 3716 : if (rl_and_mask.get_precision ())
8660 : 4436 : rl_mask = wi::lshift (wide_int::from (rl_and_mask, lnprec, UNSIGNED),
8661 : 2218 : xrl_bitpos);
8662 : : else
8663 : 1498 : rl_mask = wi::shifted_mask (xrl_bitpos, rl_bitsize, false, lnprec);
8664 : :
8665 : : /* When we set l_const, we also set r_const. */
8666 : 3716 : gcc_checking_assert (!l_const.get_precision () == !r_const.get_precision ());
8667 : :
8668 : : /* Adjust right-hand constants in both original comparisons to match width
8669 : : and bit position. */
8670 : 3716 : if (l_const.get_precision ())
8671 : : {
8672 : : /* Before clipping upper bits of the right-hand operand of the compare,
8673 : : check that they're sign or zero extensions, depending on how the
8674 : : left-hand operand would be extended. If it is unsigned, or if there's
8675 : : a mask that zeroes out extension bits, whether because we've checked
8676 : : for upper bits in the mask and did not set ll_signbit, or because the
8677 : : sign bit itself is masked out, check that the right-hand operand is
8678 : : zero-extended. */
8679 : 2086 : bool l_non_ext_bits = false;
8680 : 2086 : if (ll_bitsize < lr_bitsize)
8681 : : {
8682 : 34 : wide_int zext = wi::zext (l_const, ll_bitsize);
8683 : 68 : if ((ll_unsignedp
8684 : 26 : || (ll_and_mask.get_precision ()
8685 : 4 : && (!ll_signbit
8686 : 42 : || ((ll_and_mask & wi::mask (ll_bitsize - 1, true, ll_bitsize))
8687 : 8 : == 0)))
8688 : 128 : ? zext : wi::sext (l_const, ll_bitsize)) == l_const)
8689 : 34 : l_const = zext;
8690 : : else
8691 : : l_non_ext_bits = true;
8692 : 34 : }
8693 : : /* We're doing bitwise equality tests, so don't bother with sign
8694 : : extensions. */
8695 : 2086 : l_const = wide_int::from (l_const, lnprec, UNSIGNED);
8696 : 2086 : if (ll_and_mask.get_precision ())
8697 : 1351 : l_const &= wide_int::from (ll_and_mask, lnprec, UNSIGNED);
8698 : 2086 : l_const <<= xll_bitpos;
8699 : 6258 : if (l_non_ext_bits || (l_const & ~ll_mask) != 0)
8700 : : {
8701 : 0 : warning_at (lloc, OPT_Wtautological_compare,
8702 : : "comparison is always %d", wanted_code == NE_EXPR);
8703 : :
8704 : 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8705 : : }
8706 : :
8707 : : /* Before clipping upper bits of the right-hand operand of the compare,
8708 : : check that they're sign or zero extensions, depending on how the
8709 : : left-hand operand would be extended. */
8710 : 2086 : bool r_non_ext_bits = false;
8711 : 2086 : if (rl_bitsize < rr_bitsize)
8712 : : {
8713 : 34 : wide_int zext = wi::zext (r_const, rl_bitsize);
8714 : 68 : if ((rl_unsignedp
8715 : 17 : || (rl_and_mask.get_precision ()
8716 : 10 : && (!rl_signbit
8717 : 40 : || ((rl_and_mask & wi::mask (rl_bitsize - 1, true, rl_bitsize))
8718 : 6 : == 0)))
8719 : 119 : ? zext : wi::sext (r_const, rl_bitsize)) == r_const)
8720 : 34 : r_const = zext;
8721 : : else
8722 : : r_non_ext_bits = true;
8723 : 34 : }
8724 : 2086 : r_const = wide_int::from (r_const, lnprec, UNSIGNED);
8725 : 2086 : if (rl_and_mask.get_precision ())
8726 : 1271 : r_const &= wide_int::from (rl_and_mask, lnprec, UNSIGNED);
8727 : 2086 : r_const <<= xrl_bitpos;
8728 : 6258 : if (r_non_ext_bits || (r_const & ~rl_mask) != 0)
8729 : : {
8730 : 0 : warning_at (rloc, OPT_Wtautological_compare,
8731 : : "comparison is always %d", wanted_code == NE_EXPR);
8732 : :
8733 : 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
8734 : : }
8735 : :
8736 : : /* If there is something in common between the masks, those bits of the
8737 : : constants must be the same. If not, the combined condition cannot be
8738 : : met, and the result is known. Test for this to avoid generating
8739 : : incorrect code below. */
8740 : 2086 : wide_int mask = ll_mask & rl_mask;
8741 : 2086 : if (mask != 0
8742 : 2146 : && (l_const & mask) != (r_const & mask))
8743 : : {
8744 : 0 : if (wanted_code == NE_EXPR)
8745 : 0 : return constant_boolean_node (true, truth_type);
8746 : : else
8747 : 0 : return constant_boolean_node (false, truth_type);
8748 : : }
8749 : :
8750 : : /* The constants are combined so as to line up with the loaded field, so
8751 : : tentatively use the same parameters for the second combined
8752 : : compare. */
8753 : 2086 : ld_arg[1][0] = wide_int_to_tree (lntype, l_const | r_const);
8754 : 2086 : toshift[1][0] = MIN (xll_bitpos, xrl_bitpos);
8755 : 2086 : shifted[1][0] = 0;
8756 : 2086 : bitpos[1][0] = lnbitpos;
8757 : 2086 : bitsiz[1][0] = lnbitsize;
8758 : :
8759 : 2086 : if (parts > 1)
8760 : 49 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8761 : : shifted[1], xmask[1],
8762 : 49 : lnbitpos + GET_MODE_BITSIZE (lnmode),
8763 : : lr_reversep);
8764 : :
8765 : : /* No masking needed, we know the full constants. */
8766 : 2086 : r_mask = wi::mask (0, true, lnprec);
8767 : :
8768 : : /* If the compiler thinks this is used uninitialized below, it's
8769 : : because it can't realize that parts can only be 2 when
8770 : : comparing with constants if l_split_load is also true. This
8771 : : just silences the warning. */
8772 : 2086 : rnbitpos = 0;
8773 : 2086 : }
8774 : :
8775 : : /* Likewise, if the right sides are not constant, align them for the combined
8776 : : compare. Also, disallow this optimization if a size, signedness or
8777 : : storage order mismatch occurs between the left and right sides. */
8778 : : else
8779 : : {
8780 : 1630 : if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
8781 : 1569 : || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
8782 : 1569 : || ll_reversep != lr_reversep
8783 : : /* Make sure the two fields on the right
8784 : : correspond to the left without being swapped. */
8785 : 1569 : || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
8786 : 467 : return 0;
8787 : :
8788 : 1165 : bool r_split_load;
8789 : 1165 : scalar_int_mode rnmode2;
8790 : :
8791 : : /* Figure out how to load the bits for the right-hand size of the
8792 : : combined compare. As in the left-hand size, we may have to split it,
8793 : : and then we use two separate compares. */
8794 : 1165 : first_bit = MIN (lr_bitpos, rr_bitpos);
8795 : 1165 : end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
8796 : 1165 : HOST_WIDE_INT lr_align = TYPE_ALIGN (TREE_TYPE (lr_inner));
8797 : 1165 : poly_uint64 lr_end_region = 0;
8798 : 1165 : if (TYPE_SIZE (TREE_TYPE (lr_inner))
8799 : 1165 : && tree_fits_poly_uint64_p (TYPE_SIZE (TREE_TYPE (lr_inner))))
8800 : 1165 : lr_end_region = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (lr_inner)));
8801 : 1165 : if (!get_best_mode (end_bit - first_bit, first_bit, 0, lr_end_region,
8802 : 1165 : lr_align, BITS_PER_WORD, volatilep, &rnmode))
8803 : : {
8804 : : /* Consider the possibility of recombining loads if any of the
8805 : : fields straddles across an alignment boundary, so that either
8806 : : part can be loaded along with the other field. */
8807 : 132 : HOST_WIDE_INT boundary = compute_split_boundary_from_align
8808 : 132 : (lr_align, lr_bitpos, lr_bitsize, rr_bitpos, rr_bitsize);
8809 : :
8810 : 132 : if (boundary < 0
8811 : : /* If we're to split both, make sure the split point is
8812 : : the same. */
8813 : 130 : || (l_split_load
8814 : 128 : && (boundary - lr_bitpos
8815 : 128 : != (lnbitpos + GET_MODE_BITSIZE (lnmode)) - ll_bitpos))
8816 : 130 : || !get_best_mode (boundary - first_bit, first_bit,
8817 : : 0, lr_end_region,
8818 : 130 : lr_align, BITS_PER_WORD, volatilep, &rnmode)
8819 : 262 : || !get_best_mode (end_bit - boundary, boundary, 0, lr_end_region,
8820 : 130 : lr_align, BITS_PER_WORD, volatilep, &rnmode2))
8821 : 2 : return 0;
8822 : :
8823 : 130 : r_split_load = true;
8824 : 130 : parts = 2;
8825 : 130 : if (lr_bitpos >= boundary)
8826 : : maybe_separate = first1 = true;
8827 : 88 : else if (lr_bitpos + lr_bitsize <= boundary)
8828 : 29 : maybe_separate = true;
8829 : : }
8830 : : else
8831 : : r_split_load = false;
8832 : :
8833 : : /* Find a type that can hold the entire right-hand operand. */
8834 : 1163 : rnbitsize = GET_MODE_BITSIZE (rnmode);
8835 : 1163 : rnbitpos = first_bit & ~ (rnbitsize - 1);
8836 : 1163 : if (r_split_load)
8837 : 260 : rnbitsize += GET_MODE_BITSIZE (rnmode2);
8838 : 1163 : rntype = build_nonstandard_integer_type (rnbitsize, 1);
8839 : 1163 : if (!rntype)
8840 : : return 0;
8841 : 1163 : rnprec = TYPE_PRECISION (rntype);
8842 : 1163 : xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
8843 : :
8844 : : /* Adjust for reversed endianness. */
8845 : 1163 : if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
8846 : : {
8847 : 0 : xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
8848 : 0 : xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
8849 : : }
8850 : :
8851 : : /* Adjust the masks to match the combined type, and combine them. */
8852 : 1163 : wide_int lr_mask, rr_mask;
8853 : 1163 : if (lr_and_mask.get_precision ())
8854 : 1980 : lr_mask = wi::lshift (wide_int::from (lr_and_mask, rnprec, UNSIGNED),
8855 : 990 : xlr_bitpos);
8856 : : else
8857 : 173 : lr_mask = wi::shifted_mask (xlr_bitpos, lr_bitsize, false, rnprec);
8858 : 1163 : if (rr_and_mask.get_precision ())
8859 : 1884 : rr_mask = wi::lshift (wide_int::from (rr_and_mask, rnprec, UNSIGNED),
8860 : 942 : xrr_bitpos);
8861 : : else
8862 : 221 : rr_mask = wi::shifted_mask (xrr_bitpos, rr_bitsize, false, rnprec);
8863 : 1163 : r_mask = lr_mask | rr_mask;
8864 : :
8865 : : /* Load the right-hand operand of the combined compare. */
8866 : 1163 : toshift[1][0] = MIN (xlr_bitpos, xrr_bitpos);
8867 : 1163 : shifted[1][0] = 0;
8868 : :
8869 : 1163 : if (!r_split_load)
8870 : : {
8871 : 1033 : bitpos[1][0] = rnbitpos;
8872 : 1033 : bitsiz[1][0] = rnbitsize;
8873 : 1033 : ld_arg[1][0] = make_bit_field_load (ll_loc[3], lr_inner, lr_arg,
8874 : 1033 : rntype, rnbitsize, rnbitpos,
8875 : 1033 : lr_unsignedp || rr_unsignedp,
8876 : : lr_reversep, lr_load);
8877 : : }
8878 : :
8879 : : /* ... and the second part of the right-hand operand if needed. */
8880 : 1163 : if (parts > 1)
8881 : : {
8882 : 130 : if (r_split_load)
8883 : : {
8884 : 130 : gimple *point[2];
8885 : 130 : point[0] = lr_load;
8886 : 130 : point[1] = rr_load;
8887 : 130 : build_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8888 : : shifted[1], rl_loc[3], lr_inner, lr_arg,
8889 : : rnmode, rnmode2, rnbitpos, lr_reversep, point);
8890 : : }
8891 : : else
8892 : 0 : reuse_split_load (ld_arg[1], bitpos[1], bitsiz[1], toshift[1],
8893 : : shifted[1], xmask[1],
8894 : 0 : lnbitpos + GET_MODE_BITSIZE (lnmode)
8895 : 0 : - ll_bitpos + lr_bitpos, lr_reversep);
8896 : : }
8897 : 1163 : }
8898 : :
8899 : : /* Now issue the loads for the left-hand combined operand/s. */
8900 : 6498 : wide_int l_mask = ll_mask | rl_mask;
8901 : 3249 : toshift[0][0] = MIN (xll_bitpos, xrl_bitpos);
8902 : 3249 : shifted[0][0] = 0;
8903 : :
8904 : 3249 : if (!l_split_load)
8905 : : {
8906 : 3072 : bitpos[0][0] = lnbitpos;
8907 : 3072 : bitsiz[0][0] = lnbitsize;
8908 : 3072 : ld_arg[0][0] = make_bit_field_load (ll_loc[3], ll_inner, ll_arg,
8909 : 3072 : lntype, lnbitsize, lnbitpos,
8910 : 3072 : ll_unsignedp || rl_unsignedp,
8911 : : ll_reversep, ll_load);
8912 : : }
8913 : :
8914 : 3249 : if (parts > 1)
8915 : : {
8916 : 179 : if (l_split_load)
8917 : : {
8918 : 177 : gimple *point[2];
8919 : 177 : point[0] = ll_load;
8920 : 177 : point[1] = rl_load;
8921 : 177 : build_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
8922 : : shifted[0], rl_loc[3], ll_inner, ll_arg,
8923 : : lnmode, lnmode2, lnbitpos, ll_reversep, point);
8924 : : }
8925 : : else
8926 : 2 : reuse_split_load (ld_arg[0], bitpos[0], bitsiz[0], toshift[0],
8927 : : shifted[0], xmask[0],
8928 : 2 : rnbitpos + GET_MODE_BITSIZE (rnmode)
8929 : 2 : - lr_bitpos + ll_bitpos, ll_reversep);
8930 : : }
8931 : :
8932 : : /* Compute the compares. */
8933 : 6677 : for (int i = 0; i < parts; i++)
8934 : : {
8935 : 3428 : tree op[2] = { ld_arg[0][i], ld_arg[1][i] };
8936 : 10284 : wide_int mask[2] = { l_mask, r_mask };
8937 : 3428 : location_t *locs[2] = { i ? rl_loc : ll_loc, i ? rr_loc : lr_loc };
8938 : :
8939 : : /* Figure out the masks, and unshare the original operands. */
8940 : 10284 : for (int j = 0; j < 2; j++)
8941 : : {
8942 : 6856 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op[j]));
8943 : 6856 : op[j] = unshare_expr (op[j]);
8944 : :
8945 : : /* Mask out the bits belonging to the other part. */
8946 : 6856 : if (xmask[j][i].get_precision ())
8947 : 102 : mask[j] &= xmask[j][i];
8948 : :
8949 : 6856 : if (shifted[j][i])
8950 : : {
8951 : 307 : wide_int shift = wide_int::from (shifted[j][i], prec, UNSIGNED);
8952 : 307 : mask[j] = wi::lrshift (mask[j], shift);
8953 : 307 : }
8954 : 6856 : mask[j] = wide_int::from (mask[j], prec, UNSIGNED);
8955 : : }
8956 : :
8957 : : /* Line up the operands for a compare. */
8958 : 3428 : HOST_WIDE_INT shift = (toshift[0][i] - toshift[1][i]);
8959 : :
8960 : 3428 : if (shift)
8961 : : {
8962 : 54 : int j;
8963 : 54 : if (shift > 0)
8964 : : j = 0;
8965 : : else
8966 : : {
8967 : 52 : j = 1;
8968 : 52 : shift = -shift;
8969 : : }
8970 : :
8971 : 54 : tree shiftsz = bitsize_int (shift);
8972 : 54 : op[j] = fold_build2_loc (locs[j][1], RSHIFT_EXPR, TREE_TYPE (op[j]),
8973 : : op[j], shiftsz);
8974 : 54 : mask[j] = wi::lrshift (mask[j], shift);
8975 : : }
8976 : :
8977 : : /* Convert to the smaller type before masking out unwanted
8978 : : bits. */
8979 : 3428 : tree type = TREE_TYPE (op[0]);
8980 : 3428 : if (type != TREE_TYPE (op[1]))
8981 : : {
8982 : 236 : int j = (TYPE_PRECISION (type)
8983 : 236 : < TYPE_PRECISION (TREE_TYPE (op[1])));
8984 : 236 : if (!j)
8985 : 137 : type = TREE_TYPE (op[1]);
8986 : 236 : op[j] = fold_convert_loc (locs[j][0], type, op[j]);
8987 : 236 : mask[j] = wide_int::from (mask[j], TYPE_PRECISION (type), UNSIGNED);
8988 : : }
8989 : :
8990 : : /* Apply masks. */
8991 : 10284 : for (int j = 0; j < 2; j++)
8992 : 6856 : if (mask[j] != wi::mask (0, true, mask[j].get_precision ()))
8993 : 2809 : op[j] = fold_build2_loc (locs[j][2], BIT_AND_EXPR, type,
8994 : 5618 : op[j], wide_int_to_tree (type, mask[j]));
8995 : :
8996 : 6677 : cmp[i] = fold_build2_loc (i ? rloc : lloc, wanted_code, truth_type,
8997 : : op[0], op[1]);
8998 : 10284 : }
8999 : :
9000 : : /* Reorder the compares if needed. */
9001 : 3249 : if (first1)
9002 : 45 : std::swap (cmp[0], cmp[1]);
9003 : :
9004 : : /* Prepare to return the resulting compares. Combine two parts if
9005 : : needed. */
9006 : 3249 : if (parts == 1)
9007 : 3070 : result = cmp[0];
9008 : 179 : else if (!separatep || !maybe_separate)
9009 : : {
9010 : : /* Only fold if any of the cmp is known, otherwise we may lose the
9011 : : sequence point, and that may prevent further optimizations. */
9012 : 173 : if (TREE_CODE (cmp[0]) == INTEGER_CST
9013 : 137 : || TREE_CODE (cmp[1]) == INTEGER_CST)
9014 : 37 : result = fold_build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
9015 : : else
9016 : 136 : result = build2_loc (rloc, orig_code, truth_type, cmp[0], cmp[1]);
9017 : : }
9018 : : else
9019 : : {
9020 : 6 : result = cmp[0];
9021 : 6 : *separatep = cmp[1];
9022 : : }
9023 : :
9024 : 3249 : return result;
9025 : 254937 : }
9026 : :
9027 : : /* Try to simplify the AND of two comparisons, specified by
9028 : : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9029 : : If this can be simplified to a single expression (without requiring
9030 : : introducing more SSA variables to hold intermediate values),
9031 : : return the resulting tree. Otherwise return NULL_TREE.
9032 : : If the result expression is non-null, it has boolean type. */
9033 : :
9034 : : tree
9035 : 428044 : maybe_fold_and_comparisons (tree type,
9036 : : enum tree_code code1, tree op1a, tree op1b,
9037 : : enum tree_code code2, tree op2a, tree op2b,
9038 : : basic_block outer_cond_bb)
9039 : : {
9040 : 428044 : if (tree t = and_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9041 : : outer_cond_bb))
9042 : : return t;
9043 : :
9044 : 427056 : if (tree t = and_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9045 : : outer_cond_bb))
9046 : : return t;
9047 : :
9048 : 427040 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_AND_EXPR, code1,
9049 : : op1a, op1b, code2, op2a,
9050 : : op2b, outer_cond_bb))
9051 : : return t;
9052 : :
9053 : : return NULL_TREE;
9054 : : }
9055 : :
9056 : : /* Helper function for or_comparisons_1: try to simplify the OR of the
9057 : : ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
9058 : : If INVERT is true, invert the value of VAR before doing the OR.
9059 : : Return NULL_EXPR if we can't simplify this to a single expression. */
9060 : :
9061 : : static tree
9062 : 40930 : or_var_with_comparison (tree type, tree var, bool invert,
9063 : : enum tree_code code2, tree op2a, tree op2b,
9064 : : basic_block outer_cond_bb)
9065 : : {
9066 : 40930 : tree t;
9067 : 40930 : gimple *stmt = SSA_NAME_DEF_STMT (var);
9068 : :
9069 : : /* We can only deal with variables whose definitions are assignments. */
9070 : 40930 : if (!is_gimple_assign (stmt))
9071 : : return NULL_TREE;
9072 : :
9073 : : /* If we have an inverted comparison, apply DeMorgan's law and rewrite
9074 : : !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
9075 : : Then we only have to consider the simpler non-inverted cases. */
9076 : 40811 : if (invert)
9077 : 21661 : t = and_var_with_comparison_1 (type, stmt,
9078 : : invert_tree_comparison (code2, false),
9079 : : op2a, op2b, outer_cond_bb);
9080 : : else
9081 : 19150 : t = or_var_with_comparison_1 (type, stmt, code2, op2a, op2b,
9082 : : outer_cond_bb);
9083 : 40811 : return canonicalize_bool (t, invert);
9084 : : }
9085 : :
9086 : : /* Try to simplify the OR of the ssa variable defined by the assignment
9087 : : STMT with the comparison specified by (OP2A CODE2 OP2B).
9088 : : Return NULL_EXPR if we can't simplify this to a single expression. */
9089 : :
9090 : : static tree
9091 : 106579 : or_var_with_comparison_1 (tree type, gimple *stmt,
9092 : : enum tree_code code2, tree op2a, tree op2b,
9093 : : basic_block outer_cond_bb)
9094 : : {
9095 : 106579 : tree var = gimple_assign_lhs (stmt);
9096 : 106579 : tree true_test_var = NULL_TREE;
9097 : 106579 : tree false_test_var = NULL_TREE;
9098 : 106579 : enum tree_code innercode = gimple_assign_rhs_code (stmt);
9099 : :
9100 : : /* Check for identities like (var OR (var != 0)) => true . */
9101 : 106579 : if (TREE_CODE (op2a) == SSA_NAME
9102 : 106579 : && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
9103 : : {
9104 : 16065 : if ((code2 == NE_EXPR && integer_zerop (op2b))
9105 : 52449 : || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
9106 : : {
9107 : 14492 : true_test_var = op2a;
9108 : 14492 : if (var == true_test_var)
9109 : : return var;
9110 : : }
9111 : 3076 : else if ((code2 == EQ_EXPR && integer_zerop (op2b))
9112 : 31503 : || (code2 == NE_EXPR && integer_nonzerop (op2b)))
9113 : : {
9114 : 7442 : false_test_var = op2a;
9115 : 7442 : if (var == false_test_var)
9116 : 0 : return boolean_true_node;
9117 : : }
9118 : : }
9119 : :
9120 : : /* If the definition is a comparison, recurse on it. */
9121 : 106579 : if (TREE_CODE_CLASS (innercode) == tcc_comparison)
9122 : : {
9123 : 836 : tree t = or_comparisons_1 (type, innercode,
9124 : : gimple_assign_rhs1 (stmt),
9125 : : gimple_assign_rhs2 (stmt),
9126 : : code2, op2a, op2b, outer_cond_bb);
9127 : 836 : if (t)
9128 : : return t;
9129 : : }
9130 : :
9131 : : /* If the definition is an AND or OR expression, we may be able to
9132 : : simplify by reassociating. */
9133 : 106550 : if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
9134 : 106550 : && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
9135 : : {
9136 : 41003 : tree inner1 = gimple_assign_rhs1 (stmt);
9137 : 41003 : tree inner2 = gimple_assign_rhs2 (stmt);
9138 : 41003 : gimple *s;
9139 : 41003 : tree t;
9140 : 41003 : tree partial = NULL_TREE;
9141 : 41003 : bool is_or = (innercode == BIT_IOR_EXPR);
9142 : :
9143 : : /* Check for boolean identities that don't require recursive examination
9144 : : of inner1/inner2:
9145 : : inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
9146 : : inner1 OR (inner1 AND inner2) => inner1
9147 : : !inner1 OR (inner1 OR inner2) => true
9148 : : !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
9149 : : */
9150 : 41003 : if (inner1 == true_test_var)
9151 : 0 : return (is_or ? var : inner1);
9152 : 41003 : else if (inner2 == true_test_var)
9153 : 0 : return (is_or ? var : inner2);
9154 : 41003 : else if (inner1 == false_test_var)
9155 : 0 : return (is_or
9156 : 0 : ? boolean_true_node
9157 : 0 : : or_var_with_comparison (type, inner2, false, code2, op2a,
9158 : 0 : op2b, outer_cond_bb));
9159 : 41003 : else if (inner2 == false_test_var)
9160 : 0 : return (is_or
9161 : 0 : ? boolean_true_node
9162 : 0 : : or_var_with_comparison (type, inner1, false, code2, op2a,
9163 : 0 : op2b, outer_cond_bb));
9164 : :
9165 : : /* Next, redistribute/reassociate the OR across the inner tests.
9166 : : Compute the first partial result, (inner1 OR (op2a code op2b)) */
9167 : 41003 : if (TREE_CODE (inner1) == SSA_NAME
9168 : 41003 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
9169 : 39998 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9170 : 66080 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9171 : : gimple_assign_rhs1 (s),
9172 : : gimple_assign_rhs2 (s),
9173 : : code2, op2a, op2b,
9174 : : outer_cond_bb)))
9175 : : {
9176 : : /* Handle the OR case, where we are reassociating:
9177 : : (inner1 OR inner2) OR (op2a code2 op2b)
9178 : : => (t OR inner2)
9179 : : If the partial result t is a constant, we win. Otherwise
9180 : : continue on to try reassociating with the other inner test. */
9181 : 744 : if (is_or)
9182 : : {
9183 : 31 : if (integer_onep (t))
9184 : 0 : return boolean_true_node;
9185 : 31 : else if (integer_zerop (t))
9186 : : return inner2;
9187 : : }
9188 : :
9189 : : /* Handle the AND case, where we are redistributing:
9190 : : (inner1 AND inner2) OR (op2a code2 op2b)
9191 : : => (t AND (inner2 OR (op2a code op2b))) */
9192 : 713 : else if (integer_zerop (t))
9193 : 0 : return boolean_false_node;
9194 : :
9195 : : /* Save partial result for later. */
9196 : : partial = t;
9197 : : }
9198 : :
9199 : : /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
9200 : 41003 : if (TREE_CODE (inner2) == SSA_NAME
9201 : 41003 : && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
9202 : 40306 : && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
9203 : 79557 : && (t = maybe_fold_or_comparisons (type, gimple_assign_rhs_code (s),
9204 : : gimple_assign_rhs1 (s),
9205 : : gimple_assign_rhs2 (s),
9206 : : code2, op2a, op2b,
9207 : : outer_cond_bb)))
9208 : : {
9209 : : /* Handle the OR case, where we are reassociating:
9210 : : (inner1 OR inner2) OR (op2a code2 op2b)
9211 : : => (inner1 OR t)
9212 : : => (t OR partial) */
9213 : 488 : if (is_or)
9214 : : {
9215 : 59 : if (integer_zerop (t))
9216 : : return inner1;
9217 : 59 : else if (integer_onep (t))
9218 : 1 : return boolean_true_node;
9219 : : /* If both are the same, we can apply the identity
9220 : : (x OR x) == x. */
9221 : 58 : else if (partial && same_bool_result_p (t, partial))
9222 : : return t;
9223 : : }
9224 : :
9225 : : /* Handle the AND case, where we are redistributing:
9226 : : (inner1 AND inner2) OR (op2a code2 op2b)
9227 : : => (t AND (inner1 OR (op2a code2 op2b)))
9228 : : => (t AND partial) */
9229 : : else
9230 : : {
9231 : 429 : if (integer_zerop (t))
9232 : 0 : return boolean_false_node;
9233 : 429 : else if (partial)
9234 : : {
9235 : : /* We already got a simplification for the other
9236 : : operand to the redistributed AND expression. The
9237 : : interesting case is when at least one is true.
9238 : : Or, if both are the same, we can apply the identity
9239 : : (x AND x) == x. */
9240 : 14 : if (integer_onep (partial))
9241 : : return t;
9242 : 14 : else if (integer_onep (t))
9243 : : return partial;
9244 : 4 : else if (same_bool_result_p (t, partial))
9245 : : return t;
9246 : : }
9247 : : }
9248 : : }
9249 : : }
9250 : : return NULL_TREE;
9251 : : }
9252 : :
9253 : : /* Try to simplify the OR of two comparisons defined by
9254 : : (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
9255 : : If this can be done without constructing an intermediate value,
9256 : : return the resulting tree; otherwise NULL_TREE is returned.
9257 : : This function is deliberately asymmetric as it recurses on SSA_DEFs
9258 : : in the first comparison but not the second. */
9259 : :
9260 : : static tree
9261 : 983731 : or_comparisons_1 (tree type, enum tree_code code1, tree op1a, tree op1b,
9262 : : enum tree_code code2, tree op2a, tree op2b,
9263 : : basic_block outer_cond_bb)
9264 : : {
9265 : 983731 : tree truth_type = truth_type_for (TREE_TYPE (op1a));
9266 : :
9267 : : /* First check for ((x CODE1 y) OR (x CODE2 y)). */
9268 : 983731 : if (operand_equal_p (op1a, op2a, 0)
9269 : 983731 : && operand_equal_p (op1b, op2b, 0))
9270 : : {
9271 : : /* Result will be either NULL_TREE, or a combined comparison. */
9272 : 3002 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9273 : : TRUTH_ORIF_EXPR, code1, code2,
9274 : : truth_type, op1a, op1b);
9275 : 3002 : if (t)
9276 : : return t;
9277 : : }
9278 : :
9279 : : /* Likewise the swapped case of the above. */
9280 : 980761 : if (operand_equal_p (op1a, op2b, 0)
9281 : 980761 : && operand_equal_p (op1b, op2a, 0))
9282 : : {
9283 : : /* Result will be either NULL_TREE, or a combined comparison. */
9284 : 0 : tree t = combine_comparisons (UNKNOWN_LOCATION,
9285 : : TRUTH_ORIF_EXPR, code1,
9286 : : swap_tree_comparison (code2),
9287 : : truth_type, op1a, op1b);
9288 : 0 : if (t)
9289 : : return t;
9290 : : }
9291 : :
9292 : : /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
9293 : : NAME's definition is a truth value. See if there are any simplifications
9294 : : that can be done against the NAME's definition. */
9295 : 980761 : if (TREE_CODE (op1a) == SSA_NAME
9296 : 980758 : && (code1 == NE_EXPR || code1 == EQ_EXPR)
9297 : 1263205 : && (integer_zerop (op1b) || integer_onep (op1b)))
9298 : : {
9299 : 37143 : bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
9300 : 75463 : || (code1 == NE_EXPR && integer_onep (op1b)));
9301 : 71306 : gimple *stmt = SSA_NAME_DEF_STMT (op1a);
9302 : 71306 : switch (gimple_code (stmt))
9303 : : {
9304 : 40810 : case GIMPLE_ASSIGN:
9305 : : /* Try to simplify by copy-propagating the definition. */
9306 : 40810 : return or_var_with_comparison (type, op1a, invert, code2, op2a,
9307 : 40810 : op2b, outer_cond_bb);
9308 : :
9309 : 17138 : case GIMPLE_PHI:
9310 : : /* If every argument to the PHI produces the same result when
9311 : : ORed with the second comparison, we win.
9312 : : Do not do this unless the type is bool since we need a bool
9313 : : result here anyway. */
9314 : 17138 : if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
9315 : : {
9316 : : tree result = NULL_TREE;
9317 : : unsigned i;
9318 : 967 : for (i = 0; i < gimple_phi_num_args (stmt); i++)
9319 : : {
9320 : 967 : tree arg = gimple_phi_arg_def (stmt, i);
9321 : :
9322 : : /* If this PHI has itself as an argument, ignore it.
9323 : : If all the other args produce the same result,
9324 : : we're still OK. */
9325 : 967 : if (arg == gimple_phi_result (stmt))
9326 : 0 : continue;
9327 : 967 : else if (TREE_CODE (arg) == INTEGER_CST)
9328 : : {
9329 : 817 : if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
9330 : : {
9331 : 375 : if (!result)
9332 : 235 : result = boolean_true_node;
9333 : 140 : else if (!integer_onep (result))
9334 : : return NULL_TREE;
9335 : : }
9336 : 442 : else if (!result)
9337 : 200 : result = fold_build2 (code2, boolean_type_node,
9338 : : op2a, op2b);
9339 : 242 : else if (!same_bool_comparison_p (result,
9340 : : code2, op2a, op2b))
9341 : : return NULL_TREE;
9342 : : }
9343 : 150 : else if (TREE_CODE (arg) == SSA_NAME
9344 : 150 : && !SSA_NAME_IS_DEFAULT_DEF (arg))
9345 : : {
9346 : 150 : tree temp;
9347 : 150 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
9348 : : /* In simple cases we can look through PHI nodes,
9349 : : but we have to be careful with loops.
9350 : : See PR49073. */
9351 : 150 : if (! dom_info_available_p (CDI_DOMINATORS)
9352 : 150 : || gimple_bb (def_stmt) == gimple_bb (stmt)
9353 : 300 : || dominated_by_p (CDI_DOMINATORS,
9354 : 150 : gimple_bb (def_stmt),
9355 : 150 : gimple_bb (stmt)))
9356 : 30 : return NULL_TREE;
9357 : 120 : temp = or_var_with_comparison (type, arg, invert, code2,
9358 : : op2a, op2b, outer_cond_bb);
9359 : 120 : if (!temp)
9360 : : return NULL_TREE;
9361 : 0 : else if (!result)
9362 : : result = temp;
9363 : 0 : else if (!same_bool_result_p (result, temp))
9364 : : return NULL_TREE;
9365 : : }
9366 : : else
9367 : : return NULL_TREE;
9368 : : }
9369 : : return result;
9370 : : }
9371 : :
9372 : : default:
9373 : : break;
9374 : : }
9375 : : }
9376 : : return NULL_TREE;
9377 : : }
9378 : :
9379 : : /* Try to simplify the OR of two comparisons, specified by
9380 : : (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
9381 : : If this can be simplified to a single expression (without requiring
9382 : : introducing more SSA variables to hold intermediate values),
9383 : : return the resulting tree. Otherwise return NULL_TREE.
9384 : : If the result expression is non-null, it has boolean type. */
9385 : :
9386 : : tree
9387 : 492918 : maybe_fold_or_comparisons (tree type,
9388 : : enum tree_code code1, tree op1a, tree op1b,
9389 : : enum tree_code code2, tree op2a, tree op2b,
9390 : : basic_block outer_cond_bb)
9391 : : {
9392 : 492918 : if (tree t = or_comparisons_1 (type, code1, op1a, op1b, code2, op2a, op2b,
9393 : : outer_cond_bb))
9394 : : return t;
9395 : :
9396 : 489977 : if (tree t = or_comparisons_1 (type, code2, op2a, op2b, code1, op1a, op1b,
9397 : : outer_cond_bb))
9398 : : return t;
9399 : :
9400 : 489972 : if (tree t = maybe_fold_comparisons_from_match_pd (type, BIT_IOR_EXPR, code1,
9401 : : op1a, op1b, code2, op2a,
9402 : : op2b, outer_cond_bb))
9403 : : return t;
9404 : :
9405 : : return NULL_TREE;
9406 : : }
9407 : :
9408 : : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9409 : :
9410 : : Either NULL_TREE, a simplified but non-constant or a constant
9411 : : is returned.
9412 : :
9413 : : ??? This should go into a gimple-fold-inline.h file to be eventually
9414 : : privatized with the single valueize function used in the various TUs
9415 : : to avoid the indirect function call overhead. */
9416 : :
9417 : : tree
9418 : 418080724 : gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
9419 : : tree (*gvalueize) (tree))
9420 : : {
9421 : 418080724 : gimple_match_op res_op;
9422 : : /* ??? The SSA propagators do not correctly deal with following SSA use-def
9423 : : edges if there are intermediate VARYING defs. For this reason
9424 : : do not follow SSA edges here even though SCCVN can technically
9425 : : just deal fine with that. */
9426 : 418080724 : if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
9427 : : {
9428 : 55611205 : tree res = NULL_TREE;
9429 : 55611205 : if (gimple_simplified_result_is_gimple_val (&res_op))
9430 : 33371200 : res = res_op.ops[0];
9431 : 22240005 : else if (mprts_hook)
9432 : 7784062 : res = mprts_hook (&res_op);
9433 : 41155262 : if (res)
9434 : : {
9435 : 35162905 : if (dump_file && dump_flags & TDF_DETAILS)
9436 : : {
9437 : 9034 : fprintf (dump_file, "Match-and-simplified ");
9438 : 9034 : print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
9439 : 9034 : fprintf (dump_file, " to ");
9440 : 9034 : print_generic_expr (dump_file, res);
9441 : 9034 : fprintf (dump_file, "\n");
9442 : : }
9443 : 35162905 : return res;
9444 : : }
9445 : : }
9446 : :
9447 : 382917819 : location_t loc = gimple_location (stmt);
9448 : 382917819 : switch (gimple_code (stmt))
9449 : : {
9450 : 331262061 : case GIMPLE_ASSIGN:
9451 : 331262061 : {
9452 : 331262061 : enum tree_code subcode = gimple_assign_rhs_code (stmt);
9453 : :
9454 : 331262061 : switch (get_gimple_rhs_class (subcode))
9455 : : {
9456 : 121532529 : case GIMPLE_SINGLE_RHS:
9457 : 121532529 : {
9458 : 121532529 : tree rhs = gimple_assign_rhs1 (stmt);
9459 : 121532529 : enum tree_code_class kind = TREE_CODE_CLASS (subcode);
9460 : :
9461 : 121532529 : if (TREE_CODE (rhs) == SSA_NAME)
9462 : : {
9463 : : /* If the RHS is an SSA_NAME, return its known constant value,
9464 : : if any. */
9465 : 9750367 : return (*valueize) (rhs);
9466 : : }
9467 : : /* Handle propagating invariant addresses into address
9468 : : operations. */
9469 : 111782162 : else if (TREE_CODE (rhs) == ADDR_EXPR
9470 : 111782162 : && !is_gimple_min_invariant (rhs))
9471 : : {
9472 : 7618701 : poly_int64 offset = 0;
9473 : 7618701 : tree base;
9474 : 7618701 : base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
9475 : : &offset,
9476 : : valueize);
9477 : 7618701 : if (base
9478 : 7618701 : && (CONSTANT_CLASS_P (base)
9479 : 6924356 : || decl_address_invariant_p (base)))
9480 : 189881 : return build_invariant_address (TREE_TYPE (rhs),
9481 : 189881 : base, offset);
9482 : : }
9483 : 104163461 : else if (TREE_CODE (rhs) == CONSTRUCTOR
9484 : 1054718 : && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
9485 : 105626708 : && known_eq (CONSTRUCTOR_NELTS (rhs),
9486 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
9487 : : {
9488 : 398597 : unsigned i, nelts;
9489 : 398597 : tree val;
9490 : :
9491 : 398597 : nelts = CONSTRUCTOR_NELTS (rhs);
9492 : 398597 : tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
9493 : 883506 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
9494 : : {
9495 : 475087 : val = (*valueize) (val);
9496 : 475087 : if (TREE_CODE (val) == INTEGER_CST
9497 : 408493 : || TREE_CODE (val) == REAL_CST
9498 : 388775 : || TREE_CODE (val) == FIXED_CST)
9499 : 86312 : vec.quick_push (val);
9500 : : else
9501 : : return NULL_TREE;
9502 : : }
9503 : :
9504 : 9822 : return vec.build ();
9505 : 398597 : }
9506 : 111193684 : if (subcode == OBJ_TYPE_REF)
9507 : : {
9508 : 294547 : tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
9509 : : /* If callee is constant, we can fold away the wrapper. */
9510 : 294547 : if (is_gimple_min_invariant (val))
9511 : : return val;
9512 : : }
9513 : :
9514 : 111193500 : if (kind == tcc_reference)
9515 : : {
9516 : 74232464 : if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
9517 : 72092986 : || TREE_CODE (rhs) == REALPART_EXPR
9518 : 71239073 : || TREE_CODE (rhs) == IMAGPART_EXPR)
9519 : 76096027 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9520 : : {
9521 : 3079767 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9522 : 3079767 : return fold_unary_loc (EXPR_LOCATION (rhs),
9523 : 3079767 : TREE_CODE (rhs),
9524 : 6159534 : TREE_TYPE (rhs), val);
9525 : : }
9526 : 71152697 : else if (TREE_CODE (rhs) == BIT_FIELD_REF
9527 : 71152697 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9528 : : {
9529 : 469075 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9530 : 469075 : return fold_ternary_loc (EXPR_LOCATION (rhs),
9531 : 469075 : TREE_CODE (rhs),
9532 : 469075 : TREE_TYPE (rhs), val,
9533 : 469075 : TREE_OPERAND (rhs, 1),
9534 : 938150 : TREE_OPERAND (rhs, 2));
9535 : : }
9536 : 70683622 : else if (TREE_CODE (rhs) == MEM_REF
9537 : 70683622 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
9538 : : {
9539 : 14351574 : tree val = (*valueize) (TREE_OPERAND (rhs, 0));
9540 : 14351574 : if (TREE_CODE (val) == ADDR_EXPR
9541 : 14351574 : && is_gimple_min_invariant (val))
9542 : : {
9543 : 955846 : tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
9544 : : unshare_expr (val),
9545 : : TREE_OPERAND (rhs, 1));
9546 : 955846 : if (tem)
9547 : 70683622 : rhs = tem;
9548 : : }
9549 : : }
9550 : 70683622 : return fold_const_aggregate_ref_1 (rhs, valueize);
9551 : : }
9552 : 36961036 : else if (kind == tcc_declaration)
9553 : 8867834 : return get_symbol_constant_value (rhs);
9554 : : return rhs;
9555 : : }
9556 : :
9557 : : case GIMPLE_UNARY_RHS:
9558 : : return NULL_TREE;
9559 : :
9560 : 156984422 : case GIMPLE_BINARY_RHS:
9561 : : /* Translate &x + CST into an invariant form suitable for
9562 : : further propagation. */
9563 : 156984422 : if (subcode == POINTER_PLUS_EXPR)
9564 : : {
9565 : 18851792 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9566 : 18851792 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9567 : 18851792 : if (TREE_CODE (op0) == ADDR_EXPR
9568 : 5494336 : && TREE_CODE (op1) == INTEGER_CST)
9569 : : {
9570 : 582318 : tree off = fold_convert (ptr_type_node, op1);
9571 : 582318 : return build1_loc
9572 : 1164636 : (loc, ADDR_EXPR, TREE_TYPE (op0),
9573 : 582318 : fold_build2 (MEM_REF,
9574 : : TREE_TYPE (TREE_TYPE (op0)),
9575 : 582318 : unshare_expr (op0), off));
9576 : : }
9577 : : }
9578 : : /* Canonicalize bool != 0 and bool == 0 appearing after
9579 : : valueization. While gimple_simplify handles this
9580 : : it can get confused by the ~X == 1 -> X == 0 transform
9581 : : which we cant reduce to a SSA name or a constant
9582 : : (and we have no way to tell gimple_simplify to not
9583 : : consider those transforms in the first place). */
9584 : 138132630 : else if (subcode == EQ_EXPR
9585 : 138132630 : || subcode == NE_EXPR)
9586 : : {
9587 : 3315974 : tree lhs = gimple_assign_lhs (stmt);
9588 : 3315974 : tree op0 = gimple_assign_rhs1 (stmt);
9589 : 3315974 : if (useless_type_conversion_p (TREE_TYPE (lhs),
9590 : 3315974 : TREE_TYPE (op0)))
9591 : : {
9592 : 25983 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9593 : 25983 : op0 = (*valueize) (op0);
9594 : 25983 : if (TREE_CODE (op0) == INTEGER_CST)
9595 : 888 : std::swap (op0, op1);
9596 : 25983 : if (TREE_CODE (op1) == INTEGER_CST
9597 : 25983 : && ((subcode == NE_EXPR && integer_zerop (op1))
9598 : 2564 : || (subcode == EQ_EXPR && integer_onep (op1))))
9599 : 265 : return op0;
9600 : : }
9601 : : }
9602 : : return NULL_TREE;
9603 : :
9604 : 661413 : case GIMPLE_TERNARY_RHS:
9605 : 661413 : {
9606 : : /* Handle ternary operators that can appear in GIMPLE form. */
9607 : 661413 : tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
9608 : 661413 : tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
9609 : 661413 : tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
9610 : 661413 : return fold_ternary_loc (loc, subcode,
9611 : 661413 : TREE_TYPE (gimple_assign_lhs (stmt)),
9612 : 661413 : op0, op1, op2);
9613 : : }
9614 : :
9615 : 0 : default:
9616 : 0 : gcc_unreachable ();
9617 : : }
9618 : : }
9619 : :
9620 : 14164382 : case GIMPLE_CALL:
9621 : 14164382 : {
9622 : 14164382 : tree fn;
9623 : 14164382 : gcall *call_stmt = as_a <gcall *> (stmt);
9624 : :
9625 : 14164382 : if (gimple_call_internal_p (stmt))
9626 : : {
9627 : 1338512 : enum tree_code subcode = ERROR_MARK;
9628 : 1338512 : switch (gimple_call_internal_fn (stmt))
9629 : : {
9630 : : case IFN_UBSAN_CHECK_ADD:
9631 : : subcode = PLUS_EXPR;
9632 : : break;
9633 : 7955 : case IFN_UBSAN_CHECK_SUB:
9634 : 7955 : subcode = MINUS_EXPR;
9635 : 7955 : break;
9636 : 6815 : case IFN_UBSAN_CHECK_MUL:
9637 : 6815 : subcode = MULT_EXPR;
9638 : 6815 : break;
9639 : 138031 : case IFN_BUILTIN_EXPECT:
9640 : 138031 : {
9641 : 138031 : tree arg0 = gimple_call_arg (stmt, 0);
9642 : 138031 : tree op0 = (*valueize) (arg0);
9643 : 138031 : if (TREE_CODE (op0) == INTEGER_CST)
9644 : : return op0;
9645 : : return NULL_TREE;
9646 : : }
9647 : : default:
9648 : : return NULL_TREE;
9649 : : }
9650 : 22884 : tree arg0 = gimple_call_arg (stmt, 0);
9651 : 22884 : tree arg1 = gimple_call_arg (stmt, 1);
9652 : 22884 : tree op0 = (*valueize) (arg0);
9653 : 22884 : tree op1 = (*valueize) (arg1);
9654 : :
9655 : 22884 : if (TREE_CODE (op0) != INTEGER_CST
9656 : 2487 : || TREE_CODE (op1) != INTEGER_CST)
9657 : : {
9658 : 22374 : switch (subcode)
9659 : : {
9660 : 6715 : case MULT_EXPR:
9661 : : /* x * 0 = 0 * x = 0 without overflow. */
9662 : 6715 : if (integer_zerop (op0) || integer_zerop (op1))
9663 : 20 : return build_zero_cst (TREE_TYPE (arg0));
9664 : : break;
9665 : 7613 : case MINUS_EXPR:
9666 : : /* y - y = 0 without overflow. */
9667 : 7613 : if (operand_equal_p (op0, op1, 0))
9668 : 0 : return build_zero_cst (TREE_TYPE (arg0));
9669 : : break;
9670 : : default:
9671 : : break;
9672 : : }
9673 : : }
9674 : 22864 : tree res
9675 : 22864 : = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
9676 : 22864 : if (res
9677 : 2858 : && TREE_CODE (res) == INTEGER_CST
9678 : 23374 : && !TREE_OVERFLOW (res))
9679 : : return res;
9680 : : return NULL_TREE;
9681 : : }
9682 : :
9683 : 12825870 : fn = (*valueize) (gimple_call_fn (stmt));
9684 : 12825870 : if (TREE_CODE (fn) == ADDR_EXPR
9685 : 12196683 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
9686 : 12196619 : && fndecl_built_in_p (TREE_OPERAND (fn, 0))
9687 : 18737722 : && gimple_builtin_call_types_compatible_p (stmt,
9688 : 5911852 : TREE_OPERAND (fn, 0)))
9689 : : {
9690 : 5795672 : tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
9691 : 5795672 : tree retval;
9692 : 5795672 : unsigned i;
9693 : 17997593 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
9694 : 12201921 : args[i] = (*valueize) (gimple_call_arg (stmt, i));
9695 : 5795672 : retval = fold_builtin_call_array (loc,
9696 : : gimple_call_return_type (call_stmt),
9697 : : fn, gimple_call_num_args (stmt), args);
9698 : 5795672 : if (retval)
9699 : : {
9700 : : /* fold_call_expr wraps the result inside a NOP_EXPR. */
9701 : 47829 : STRIP_NOPS (retval);
9702 : 47829 : retval = fold_convert (gimple_call_return_type (call_stmt),
9703 : : retval);
9704 : : }
9705 : 5795672 : return retval;
9706 : : }
9707 : : return NULL_TREE;
9708 : : }
9709 : :
9710 : : default:
9711 : : return NULL_TREE;
9712 : : }
9713 : : }
9714 : :
9715 : : /* Fold STMT to a constant using VALUEIZE to valueize SSA names.
9716 : : Returns NULL_TREE if folding to a constant is not possible, otherwise
9717 : : returns a constant according to is_gimple_min_invariant. */
9718 : :
9719 : : tree
9720 : 4417 : gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
9721 : : {
9722 : 4417 : tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
9723 : 4417 : if (res && is_gimple_min_invariant (res))
9724 : : return res;
9725 : : return NULL_TREE;
9726 : : }
9727 : :
9728 : :
9729 : : /* The following set of functions are supposed to fold references using
9730 : : their constant initializers. */
9731 : :
9732 : : /* See if we can find constructor defining value of BASE.
9733 : : When we know the consructor with constant offset (such as
9734 : : base is array[40] and we do know constructor of array), then
9735 : : BIT_OFFSET is adjusted accordingly.
9736 : :
9737 : : As a special case, return error_mark_node when constructor
9738 : : is not explicitly available, but it is known to be zero
9739 : : such as 'static const int a;'. */
9740 : : static tree
9741 : 130638160 : get_base_constructor (tree base, poly_int64 *bit_offset,
9742 : : tree (*valueize)(tree))
9743 : : {
9744 : 130731007 : poly_int64 bit_offset2, size, max_size;
9745 : 130731007 : bool reverse;
9746 : :
9747 : 130731007 : if (TREE_CODE (base) == MEM_REF)
9748 : : {
9749 : 141090378 : poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
9750 : 70545189 : if (!boff.to_shwi (bit_offset))
9751 : 70215026 : return NULL_TREE;
9752 : :
9753 : 70544855 : if (valueize
9754 : 70544855 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
9755 : 38101616 : base = valueize (TREE_OPERAND (base, 0));
9756 : 70544855 : if (!base || TREE_CODE (base) != ADDR_EXPR)
9757 : : return NULL_TREE;
9758 : 330163 : base = TREE_OPERAND (base, 0);
9759 : : }
9760 : 60185818 : else if (valueize
9761 : 32166438 : && TREE_CODE (base) == SSA_NAME)
9762 : 0 : base = valueize (base);
9763 : :
9764 : : /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
9765 : : DECL_INITIAL. If BASE is a nested reference into another
9766 : : ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
9767 : : the inner reference. */
9768 : 60515981 : switch (TREE_CODE (base))
9769 : : {
9770 : 52922466 : case VAR_DECL:
9771 : 52922466 : case CONST_DECL:
9772 : 52922466 : {
9773 : 52922466 : tree init = ctor_for_folding (base);
9774 : :
9775 : : /* Our semantic is exact opposite of ctor_for_folding;
9776 : : NULL means unknown, while error_mark_node is 0. */
9777 : 52922466 : if (init == error_mark_node)
9778 : : return NULL_TREE;
9779 : 1140165 : if (!init)
9780 : 1131 : return error_mark_node;
9781 : : return init;
9782 : : }
9783 : :
9784 : 92847 : case VIEW_CONVERT_EXPR:
9785 : 92847 : return get_base_constructor (TREE_OPERAND (base, 0),
9786 : 92847 : bit_offset, valueize);
9787 : :
9788 : 317355 : case ARRAY_REF:
9789 : 317355 : case COMPONENT_REF:
9790 : 317355 : base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
9791 : : &reverse);
9792 : 317355 : if (!known_size_p (max_size) || maybe_ne (size, max_size))
9793 : : return NULL_TREE;
9794 : 256510 : *bit_offset += bit_offset2;
9795 : 256510 : return get_base_constructor (base, bit_offset, valueize);
9796 : :
9797 : : case CONSTRUCTOR:
9798 : : return base;
9799 : :
9800 : 7183313 : default:
9801 : 7183313 : if (CONSTANT_CLASS_P (base))
9802 : : return base;
9803 : :
9804 : : return NULL_TREE;
9805 : : }
9806 : : }
9807 : :
9808 : : /* CTOR is a CONSTRUCTOR of an array or vector type. Fold a reference of SIZE
9809 : : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
9810 : : the reference; otherwise the type of the referenced element is used instead.
9811 : : When SIZE is zero, attempt to fold a reference to the entire element OFFSET
9812 : : refers to. Increment *SUBOFF by the bit offset of the accessed element. */
9813 : :
9814 : : static tree
9815 : 726937 : fold_array_ctor_reference (tree type, tree ctor,
9816 : : unsigned HOST_WIDE_INT offset,
9817 : : unsigned HOST_WIDE_INT size,
9818 : : tree from_decl,
9819 : : unsigned HOST_WIDE_INT *suboff)
9820 : : {
9821 : 726937 : offset_int low_bound;
9822 : 726937 : offset_int elt_size;
9823 : 726937 : offset_int access_index;
9824 : 726937 : tree domain_type = NULL_TREE;
9825 : 726937 : HOST_WIDE_INT inner_offset;
9826 : :
9827 : : /* Compute low bound and elt size. */
9828 : 726937 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
9829 : 726937 : domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
9830 : 726937 : if (domain_type && TYPE_MIN_VALUE (domain_type))
9831 : : {
9832 : : /* Static constructors for variably sized objects make no sense. */
9833 : 726937 : if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
9834 : : return NULL_TREE;
9835 : 726937 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
9836 : : }
9837 : : else
9838 : 0 : low_bound = 0;
9839 : : /* Static constructors for variably sized objects make no sense. */
9840 : 726937 : if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
9841 : : return NULL_TREE;
9842 : 726937 : elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
9843 : :
9844 : : /* When TYPE is non-null, verify that it specifies a constant-sized
9845 : : access of a multiple of the array element size. Avoid division
9846 : : by zero below when ELT_SIZE is zero, such as with the result of
9847 : : an initializer for a zero-length array or an empty struct. */
9848 : 726937 : if (elt_size == 0
9849 : 726937 : || (type
9850 : 726901 : && (!TYPE_SIZE_UNIT (type)
9851 : 726901 : || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)))
9852 : 36 : return NULL_TREE;
9853 : :
9854 : : /* Compute the array index we look for. */
9855 : 726901 : access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
9856 : : elt_size);
9857 : 726901 : access_index += low_bound;
9858 : :
9859 : : /* And offset within the access. */
9860 : 726901 : inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
9861 : :
9862 : 726901 : unsigned HOST_WIDE_INT elt_sz = elt_size.to_uhwi ();
9863 : 726901 : if (size > elt_sz * BITS_PER_UNIT)
9864 : : {
9865 : : /* native_encode_expr constraints. */
9866 : 48554 : if (size > MAX_BITSIZE_MODE_ANY_MODE
9867 : 40555 : || size % BITS_PER_UNIT != 0
9868 : 40555 : || inner_offset % BITS_PER_UNIT != 0
9869 : 40555 : || elt_sz > MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT)
9870 : : return NULL_TREE;
9871 : :
9872 : 40555 : unsigned ctor_idx;
9873 : 40555 : tree val = get_array_ctor_element_at_index (ctor, access_index,
9874 : : &ctor_idx);
9875 : 40579 : if (!val && ctor_idx >= CONSTRUCTOR_NELTS (ctor))
9876 : 23 : return build_zero_cst (type);
9877 : :
9878 : : /* native-encode adjacent ctor elements. */
9879 : 40532 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
9880 : 40532 : unsigned bufoff = 0;
9881 : 40532 : offset_int index = 0;
9882 : 40532 : offset_int max_index = access_index;
9883 : 40532 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
9884 : 40532 : if (!val)
9885 : 1 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
9886 : 40531 : else if (!CONSTANT_CLASS_P (val))
9887 : : return NULL_TREE;
9888 : 40283 : if (!elt->index)
9889 : : ;
9890 : 33749 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
9891 : : {
9892 : 20 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
9893 : 20 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
9894 : : }
9895 : : else
9896 : 33729 : index = max_index = wi::to_offset (elt->index);
9897 : 40283 : index = wi::umax (index, access_index);
9898 : 263470 : do
9899 : : {
9900 : 263470 : if (bufoff + elt_sz > sizeof (buf))
9901 : 0 : elt_sz = sizeof (buf) - bufoff;
9902 : 263470 : int len;
9903 : 263470 : if (TREE_CODE (val) == RAW_DATA_CST)
9904 : : {
9905 : 20 : gcc_assert (inner_offset == 0);
9906 : 20 : if (!elt->index || TREE_CODE (elt->index) != INTEGER_CST)
9907 : : return NULL_TREE;
9908 : 40 : inner_offset = (access_index
9909 : 20 : - wi::to_offset (elt->index)).to_uhwi ();
9910 : 20 : len = MIN (sizeof (buf) - bufoff,
9911 : : (unsigned) (RAW_DATA_LENGTH (val) - inner_offset));
9912 : 20 : memcpy (buf + bufoff, RAW_DATA_POINTER (val) + inner_offset,
9913 : : len);
9914 : 20 : access_index += len - 1;
9915 : : }
9916 : : else
9917 : : {
9918 : 526900 : len = native_encode_expr (val, buf + bufoff, elt_sz,
9919 : 263450 : inner_offset / BITS_PER_UNIT);
9920 : 263450 : if (len != (int) elt_sz - inner_offset / BITS_PER_UNIT)
9921 : : return NULL_TREE;
9922 : : }
9923 : 263470 : inner_offset = 0;
9924 : 263470 : bufoff += len;
9925 : :
9926 : 263470 : access_index += 1;
9927 : 263470 : if (wi::cmpu (access_index, index) == 0)
9928 : 2 : val = elt->value;
9929 : 263468 : else if (wi::cmpu (access_index, max_index) > 0)
9930 : : {
9931 : 263228 : ctor_idx++;
9932 : 263228 : if (ctor_idx >= CONSTRUCTOR_NELTS (ctor))
9933 : : {
9934 : 36395 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
9935 : 36395 : ++max_index;
9936 : : }
9937 : : else
9938 : : {
9939 : 226833 : elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
9940 : 226833 : index = 0;
9941 : 226833 : max_index = access_index;
9942 : 226833 : if (!elt->index)
9943 : : ;
9944 : 226025 : else if (TREE_CODE (elt->index) == RANGE_EXPR)
9945 : : {
9946 : 0 : index = wi::to_offset (TREE_OPERAND (elt->index, 0));
9947 : 0 : max_index = wi::to_offset (TREE_OPERAND (elt->index, 1));
9948 : : }
9949 : : else
9950 : 226025 : index = max_index = wi::to_offset (elt->index);
9951 : 226833 : index = wi::umax (index, access_index);
9952 : 226833 : if (wi::cmpu (access_index, index) == 0)
9953 : 226828 : val = elt->value;
9954 : : else
9955 : 5 : val = build_zero_cst (TREE_TYPE (TREE_TYPE (ctor)));
9956 : : }
9957 : : }
9958 : : }
9959 : 263470 : while (bufoff < size / BITS_PER_UNIT);
9960 : 40283 : *suboff += size;
9961 : 40283 : return native_interpret_expr (type, buf, size / BITS_PER_UNIT);
9962 : : }
9963 : :
9964 : 678347 : unsigned ctor_idx;
9965 : 678347 : if (tree val = get_array_ctor_element_at_index (ctor, access_index,
9966 : : &ctor_idx))
9967 : : {
9968 : 676853 : if (TREE_CODE (val) == RAW_DATA_CST)
9969 : : {
9970 : 1802 : if (size != BITS_PER_UNIT || elt_sz != 1 || inner_offset != 0)
9971 : : return NULL_TREE;
9972 : 1794 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
9973 : 1794 : if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
9974 : : return NULL_TREE;
9975 : 1794 : unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
9976 : 1794 : val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
9977 : : }
9978 : 676845 : if (!size && TREE_CODE (val) != CONSTRUCTOR)
9979 : : {
9980 : : /* For the final reference to the entire accessed element
9981 : : (SIZE is zero), reset INNER_OFFSET, disegard TYPE (which
9982 : : may be null) in favor of the type of the element, and set
9983 : : SIZE to the size of the accessed element. */
9984 : 22513 : inner_offset = 0;
9985 : 22513 : type = TREE_TYPE (val);
9986 : 22513 : size = elt_sz * BITS_PER_UNIT;
9987 : : }
9988 : 1758988 : else if (size && access_index < CONSTRUCTOR_NELTS (ctor) - 1
9989 : 488416 : && TREE_CODE (val) == CONSTRUCTOR
9990 : 18119 : && (elt_sz * BITS_PER_UNIT - inner_offset) < size)
9991 : : /* If this isn't the last element in the CTOR and a CTOR itself
9992 : : and it does not cover the whole object we are requesting give up
9993 : : since we're not set up for combining from multiple CTORs. */
9994 : 26 : return NULL_TREE;
9995 : :
9996 : 676819 : *suboff += access_index.to_uhwi () * elt_sz * BITS_PER_UNIT;
9997 : 676819 : return fold_ctor_reference (type, val, inner_offset, size, from_decl,
9998 : : suboff);
9999 : : }
10000 : :
10001 : : /* Memory not explicitly mentioned in constructor is 0 (or
10002 : : the reference is out of range). */
10003 : 1494 : return type ? build_zero_cst (type) : NULL_TREE;
10004 : : }
10005 : :
10006 : : /* CTOR is a CONSTRUCTOR of a record or union type. Fold a reference of SIZE
10007 : : bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of
10008 : : the reference; otherwise the type of the referenced member is used instead.
10009 : : When SIZE is zero, attempt to fold a reference to the entire member OFFSET
10010 : : refers to. Increment *SUBOFF by the bit offset of the accessed member. */
10011 : :
10012 : : static tree
10013 : 75424 : fold_nonarray_ctor_reference (tree type, tree ctor,
10014 : : unsigned HOST_WIDE_INT offset,
10015 : : unsigned HOST_WIDE_INT size,
10016 : : tree from_decl,
10017 : : unsigned HOST_WIDE_INT *suboff)
10018 : : {
10019 : 75424 : unsigned HOST_WIDE_INT cnt;
10020 : 75424 : tree cfield, cval;
10021 : :
10022 : 115219 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
10023 : : {
10024 : 106257 : tree byte_offset = DECL_FIELD_OFFSET (cfield);
10025 : 106257 : tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
10026 : 106257 : tree field_size = DECL_SIZE (cfield);
10027 : :
10028 : 106257 : if (!field_size)
10029 : : {
10030 : : /* Determine the size of the flexible array member from
10031 : : the size of the initializer provided for it. */
10032 : 847 : field_size = TYPE_SIZE (TREE_TYPE (cval));
10033 : : }
10034 : :
10035 : : /* Variable sized objects in static constructors makes no sense,
10036 : : but field_size can be NULL for flexible array members. */
10037 : 106257 : gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
10038 : : && TREE_CODE (byte_offset) == INTEGER_CST
10039 : : && (field_size != NULL_TREE
10040 : : ? TREE_CODE (field_size) == INTEGER_CST
10041 : : : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
10042 : :
10043 : : /* Compute bit offset of the field. */
10044 : 106257 : offset_int bitoffset
10045 : 106257 : = (wi::to_offset (field_offset)
10046 : 106257 : + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
10047 : : /* Compute bit offset where the field ends. */
10048 : 106257 : offset_int bitoffset_end;
10049 : 106257 : if (field_size != NULL_TREE)
10050 : 106257 : bitoffset_end = bitoffset + wi::to_offset (field_size);
10051 : : else
10052 : 0 : bitoffset_end = 0;
10053 : :
10054 : : /* Compute the bit offset of the end of the desired access.
10055 : : As a special case, if the size of the desired access is
10056 : : zero, assume the access is to the entire field (and let
10057 : : the caller make any necessary adjustments by storing
10058 : : the actual bounds of the field in FIELDBOUNDS). */
10059 : 106257 : offset_int access_end = offset_int (offset);
10060 : 106257 : if (size)
10061 : 64826 : access_end += size;
10062 : : else
10063 : 41431 : access_end = bitoffset_end;
10064 : :
10065 : : /* Is there any overlap between the desired access at
10066 : : [OFFSET, OFFSET+SIZE) and the offset of the field within
10067 : : the object at [BITOFFSET, BITOFFSET_END)? */
10068 : 106257 : if (wi::cmps (access_end, bitoffset) > 0
10069 : 106257 : && (field_size == NULL_TREE
10070 : 103926 : || wi::lts_p (offset, bitoffset_end)))
10071 : : {
10072 : 66462 : *suboff += bitoffset.to_uhwi ();
10073 : :
10074 : 66462 : if (!size && TREE_CODE (cval) != CONSTRUCTOR)
10075 : : {
10076 : : /* For the final reference to the entire accessed member
10077 : : (SIZE is zero), reset OFFSET, disegard TYPE (which may
10078 : : be null) in favor of the type of the member, and set
10079 : : SIZE to the size of the accessed member. */
10080 : 19282 : offset = bitoffset.to_uhwi ();
10081 : 19282 : type = TREE_TYPE (cval);
10082 : 19282 : size = (bitoffset_end - bitoffset).to_uhwi ();
10083 : : }
10084 : :
10085 : : /* We do have overlap. Now see if the field is large enough
10086 : : to cover the access. Give up for accesses that extend
10087 : : beyond the end of the object or that span multiple fields. */
10088 : 66462 : if (wi::cmps (access_end, bitoffset_end) > 0)
10089 : : return NULL_TREE;
10090 : 65829 : if (offset < bitoffset)
10091 : : return NULL_TREE;
10092 : :
10093 : 65829 : offset_int inner_offset = offset_int (offset) - bitoffset;
10094 : :
10095 : : /* Integral bit-fields are left-justified on big-endian targets, so
10096 : : we must arrange for native_encode_int to start at their MSB. */
10097 : 65829 : if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield)))
10098 : : {
10099 : 65829 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
10100 : : return NULL_TREE;
10101 : 65829 : if (BYTES_BIG_ENDIAN)
10102 : : {
10103 : : tree ctype = TREE_TYPE (cfield);
10104 : : unsigned int encoding_size;
10105 : : if (TYPE_MODE (ctype) != BLKmode)
10106 : : encoding_size
10107 : : = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (ctype));
10108 : : else
10109 : : encoding_size = TREE_INT_CST_LOW (TYPE_SIZE (ctype));
10110 : : inner_offset += encoding_size - wi::to_offset (field_size);
10111 : : }
10112 : : }
10113 : :
10114 : 65829 : return fold_ctor_reference (type, cval,
10115 : 65829 : inner_offset.to_uhwi (), size,
10116 : : from_decl, suboff);
10117 : : }
10118 : : }
10119 : :
10120 : 8962 : if (!type)
10121 : : return NULL_TREE;
10122 : :
10123 : 8962 : return build_zero_cst (type);
10124 : : }
10125 : :
10126 : : /* CTOR is a value initializing memory. Fold a reference of TYPE and
10127 : : bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE
10128 : : is zero, attempt to fold a reference to the entire subobject
10129 : : which OFFSET refers to. This is used when folding accesses to
10130 : : string members of aggregates. When non-null, set *SUBOFF to
10131 : : the bit offset of the accessed subobject. */
10132 : :
10133 : : tree
10134 : 1658051 : fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset,
10135 : : const poly_uint64 &poly_size, tree from_decl,
10136 : : unsigned HOST_WIDE_INT *suboff /* = NULL */)
10137 : : {
10138 : 1658051 : tree ret;
10139 : :
10140 : : /* We found the field with exact match. */
10141 : 1658051 : if (type
10142 : 1658051 : && useless_type_conversion_p (type, TREE_TYPE (ctor))
10143 : 2351699 : && known_eq (poly_offset, 0U))
10144 : 692188 : return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
10145 : :
10146 : : /* The remaining optimizations need a constant size and offset. */
10147 : 965863 : unsigned HOST_WIDE_INT size, offset;
10148 : 965863 : if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
10149 : : return NULL_TREE;
10150 : :
10151 : : /* We are at the end of walk, see if we can view convert the
10152 : : result. */
10153 : 965863 : if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
10154 : : /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
10155 : 22976 : && known_eq (wi::to_poly_widest (TYPE_SIZE (type)), size)
10156 : 22869 : && known_eq (wi::to_poly_widest (TYPE_SIZE (TREE_TYPE (ctor))), size))
10157 : : {
10158 : 14564 : ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
10159 : 14564 : if (ret)
10160 : : {
10161 : 14564 : ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
10162 : 14564 : if (ret)
10163 : 14504 : STRIP_USELESS_TYPE_CONVERSION (ret);
10164 : : }
10165 : 14564 : return ret;
10166 : : }
10167 : :
10168 : : /* For constants and byte-aligned/sized reads, try to go through
10169 : : native_encode/interpret. */
10170 : 951299 : if (CONSTANT_CLASS_P (ctor)
10171 : : && BITS_PER_UNIT == 8
10172 : 139582 : && offset % BITS_PER_UNIT == 0
10173 : 139578 : && offset / BITS_PER_UNIT <= INT_MAX
10174 : 139546 : && size % BITS_PER_UNIT == 0
10175 : 139537 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10176 : 1090069 : && can_native_interpret_type_p (type))
10177 : : {
10178 : 96411 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10179 : 192822 : int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
10180 : 96411 : offset / BITS_PER_UNIT);
10181 : 96411 : if (len > 0)
10182 : 95703 : return native_interpret_expr (type, buf, len);
10183 : : }
10184 : :
10185 : : /* For constructors, try first a recursive local processing, but in any case
10186 : : this requires the native storage order. */
10187 : 855596 : if (TREE_CODE (ctor) == CONSTRUCTOR
10188 : 855596 : && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor))
10189 : 802591 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor))))
10190 : : {
10191 : 802361 : unsigned HOST_WIDE_INT dummy = 0;
10192 : 802361 : if (!suboff)
10193 : 679332 : suboff = &dummy;
10194 : :
10195 : 802361 : tree ret;
10196 : 802361 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
10197 : 802361 : || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
10198 : 726937 : ret = fold_array_ctor_reference (type, ctor, offset, size,
10199 : : from_decl, suboff);
10200 : : else
10201 : 75424 : ret = fold_nonarray_ctor_reference (type, ctor, offset, size,
10202 : : from_decl, suboff);
10203 : :
10204 : : /* Otherwise fall back to native_encode_initializer. This may be done
10205 : : only from the outermost fold_ctor_reference call (because it itself
10206 : : recurses into CONSTRUCTORs and doesn't update suboff). */
10207 : 802361 : if (ret == NULL_TREE
10208 : 270564 : && suboff == &dummy
10209 : : && BITS_PER_UNIT == 8
10210 : 261072 : && offset % BITS_PER_UNIT == 0
10211 : 261070 : && offset / BITS_PER_UNIT <= INT_MAX
10212 : 261070 : && size % BITS_PER_UNIT == 0
10213 : 261061 : && size <= MAX_BITSIZE_MODE_ANY_MODE
10214 : 1055423 : && can_native_interpret_type_p (type))
10215 : : {
10216 : 218185 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
10217 : 436370 : int len = native_encode_initializer (ctor, buf, size / BITS_PER_UNIT,
10218 : 218185 : offset / BITS_PER_UNIT);
10219 : 218185 : if (len > 0)
10220 : 1262 : return native_interpret_expr (type, buf, len);
10221 : : }
10222 : :
10223 : 801099 : return ret;
10224 : : }
10225 : :
10226 : : return NULL_TREE;
10227 : : }
10228 : :
10229 : : /* Return the tree representing the element referenced by T if T is an
10230 : : ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
10231 : : names using VALUEIZE. Return NULL_TREE otherwise. */
10232 : :
10233 : : tree
10234 : 136166632 : fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
10235 : : {
10236 : 136166632 : tree ctor, idx, base;
10237 : 136166632 : poly_int64 offset, size, max_size;
10238 : 136166632 : tree tem;
10239 : 136166632 : bool reverse;
10240 : :
10241 : 136166632 : if (TREE_THIS_VOLATILE (t))
10242 : : return NULL_TREE;
10243 : :
10244 : 135850896 : if (DECL_P (t))
10245 : 257153 : return get_symbol_constant_value (t);
10246 : :
10247 : 135593743 : tem = fold_read_from_constant_string (t);
10248 : 135593743 : if (tem)
10249 : : return tem;
10250 : :
10251 : 135591515 : switch (TREE_CODE (t))
10252 : : {
10253 : 10302234 : case ARRAY_REF:
10254 : 10302234 : case ARRAY_RANGE_REF:
10255 : : /* Constant indexes are handled well by get_base_constructor.
10256 : : Only special case variable offsets.
10257 : : FIXME: This code can't handle nested references with variable indexes
10258 : : (they will be handled only by iteration of ccp). Perhaps we can bring
10259 : : get_ref_base_and_extent here and make it use a valueize callback. */
10260 : 10302234 : if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
10261 : 6067682 : && valueize
10262 : 4004821 : && (idx = (*valueize) (TREE_OPERAND (t, 1)))
10263 : 14307055 : && poly_int_tree_p (idx))
10264 : : {
10265 : 1606523 : tree low_bound, unit_size;
10266 : :
10267 : : /* If the resulting bit-offset is constant, track it. */
10268 : 1606523 : if ((low_bound = array_ref_low_bound (t),
10269 : 1606523 : poly_int_tree_p (low_bound))
10270 : 1606523 : && (unit_size = array_ref_element_size (t),
10271 : 1606523 : tree_fits_uhwi_p (unit_size)))
10272 : : {
10273 : 1606523 : poly_offset_int woffset
10274 : 1606523 : = wi::sext (wi::to_poly_offset (idx)
10275 : 3213046 : - wi::to_poly_offset (low_bound),
10276 : 1606523 : TYPE_PRECISION (sizetype));
10277 : 1606523 : woffset *= tree_to_uhwi (unit_size);
10278 : 1606523 : woffset *= BITS_PER_UNIT;
10279 : 1606523 : if (woffset.to_shwi (&offset))
10280 : : {
10281 : 1606421 : base = TREE_OPERAND (t, 0);
10282 : 1606421 : ctor = get_base_constructor (base, &offset, valueize);
10283 : : /* Empty constructor. Always fold to 0. */
10284 : 1606421 : if (ctor == error_mark_node)
10285 : 1606421 : return build_zero_cst (TREE_TYPE (t));
10286 : : /* Out of bound array access. Value is undefined,
10287 : : but don't fold. */
10288 : 1606344 : if (maybe_lt (offset, 0))
10289 : : return NULL_TREE;
10290 : : /* We cannot determine ctor. */
10291 : 1605840 : if (!ctor)
10292 : : return NULL_TREE;
10293 : 170557 : return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
10294 : 170557 : tree_to_uhwi (unit_size)
10295 : 341114 : * BITS_PER_UNIT,
10296 : : base);
10297 : : }
10298 : : }
10299 : : }
10300 : : /* Fallthru. */
10301 : :
10302 : 128775229 : case COMPONENT_REF:
10303 : 128775229 : case BIT_FIELD_REF:
10304 : 128775229 : case TARGET_MEM_REF:
10305 : 128775229 : case MEM_REF:
10306 : 128775229 : base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
10307 : 128775229 : ctor = get_base_constructor (base, &offset, valueize);
10308 : :
10309 : : /* We cannot determine ctor. */
10310 : 128775229 : if (!ctor)
10311 : : return NULL_TREE;
10312 : : /* Empty constructor. Always fold to 0. */
10313 : 1081834 : if (ctor == error_mark_node)
10314 : 1054 : return build_zero_cst (TREE_TYPE (t));
10315 : : /* We do not know precise access. */
10316 : 1080780 : if (!known_size_p (max_size) || maybe_ne (max_size, size))
10317 : : return NULL_TREE;
10318 : : /* Out of bound array access. Value is undefined, but don't fold. */
10319 : 568781 : if (maybe_lt (offset, 0))
10320 : : return NULL_TREE;
10321 : : /* Access with reverse storage order. */
10322 : 565788 : if (reverse)
10323 : : return NULL_TREE;
10324 : :
10325 : 565788 : tem = fold_ctor_reference (TREE_TYPE (t), ctor, offset, size, base);
10326 : 565788 : if (tem)
10327 : : return tem;
10328 : :
10329 : : /* For bit field reads try to read the representative and
10330 : : adjust. */
10331 : 257761 : if (TREE_CODE (t) == COMPONENT_REF
10332 : 6353 : && DECL_BIT_FIELD (TREE_OPERAND (t, 1))
10333 : 257845 : && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)))
10334 : : {
10335 : 84 : HOST_WIDE_INT csize, coffset;
10336 : 84 : tree field = TREE_OPERAND (t, 1);
10337 : 84 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
10338 : 168 : if (INTEGRAL_TYPE_P (TREE_TYPE (repr))
10339 : 83 : && size.is_constant (&csize)
10340 : 83 : && offset.is_constant (&coffset)
10341 : 83 : && (coffset % BITS_PER_UNIT != 0
10342 : 81 : || csize % BITS_PER_UNIT != 0)
10343 : 84 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN)
10344 : : {
10345 : 10 : poly_int64 bitoffset;
10346 : 10 : poly_uint64 field_offset, repr_offset;
10347 : 10 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
10348 : 20 : && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
10349 : 10 : bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
10350 : : else
10351 : : bitoffset = 0;
10352 : 10 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
10353 : 10 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
10354 : 10 : HOST_WIDE_INT bitoff;
10355 : 10 : int diff = (TYPE_PRECISION (TREE_TYPE (repr))
10356 : 10 : - TYPE_PRECISION (TREE_TYPE (field)));
10357 : 10 : if (bitoffset.is_constant (&bitoff)
10358 : 10 : && bitoff >= 0
10359 : 10 : && bitoff <= diff)
10360 : : {
10361 : 10 : offset -= bitoff;
10362 : 10 : size = tree_to_uhwi (DECL_SIZE (repr));
10363 : :
10364 : 10 : tem = fold_ctor_reference (TREE_TYPE (repr), ctor, offset,
10365 : 10 : size, base);
10366 : 10 : if (tem && TREE_CODE (tem) == INTEGER_CST)
10367 : : {
10368 : 10 : if (!BYTES_BIG_ENDIAN)
10369 : 10 : tem = wide_int_to_tree (TREE_TYPE (field),
10370 : 10 : wi::lrshift (wi::to_wide (tem),
10371 : : bitoff));
10372 : : else
10373 : : tem = wide_int_to_tree (TREE_TYPE (field),
10374 : : wi::lrshift (wi::to_wide (tem),
10375 : : diff - bitoff));
10376 : 10 : return tem;
10377 : : }
10378 : : }
10379 : : }
10380 : : }
10381 : : break;
10382 : :
10383 : 1611857 : case REALPART_EXPR:
10384 : 1611857 : case IMAGPART_EXPR:
10385 : 1611857 : {
10386 : 1611857 : tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
10387 : 1611857 : if (c && TREE_CODE (c) == COMPLEX_CST)
10388 : 2718 : return fold_build1_loc (EXPR_LOCATION (t),
10389 : 5436 : TREE_CODE (t), TREE_TYPE (t), c);
10390 : : break;
10391 : : }
10392 : :
10393 : : default:
10394 : : break;
10395 : : }
10396 : :
10397 : : return NULL_TREE;
10398 : : }
10399 : :
10400 : : tree
10401 : 63871153 : fold_const_aggregate_ref (tree t)
10402 : : {
10403 : 63871153 : return fold_const_aggregate_ref_1 (t, NULL);
10404 : : }
10405 : :
10406 : : /* Lookup virtual method with index TOKEN in a virtual table V
10407 : : at OFFSET.
10408 : : Set CAN_REFER if non-NULL to false if method
10409 : : is not referable or if the virtual table is ill-formed (such as rewriten
10410 : : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10411 : :
10412 : : tree
10413 : 392653 : gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
10414 : : tree v,
10415 : : unsigned HOST_WIDE_INT offset,
10416 : : bool *can_refer)
10417 : : {
10418 : 392653 : tree vtable = v, init, fn;
10419 : 392653 : unsigned HOST_WIDE_INT size;
10420 : 392653 : unsigned HOST_WIDE_INT elt_size, access_index;
10421 : 392653 : tree domain_type;
10422 : :
10423 : 392653 : if (can_refer)
10424 : 392653 : *can_refer = true;
10425 : :
10426 : : /* First of all double check we have virtual table. */
10427 : 392653 : if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
10428 : : {
10429 : : /* Pass down that we lost track of the target. */
10430 : 0 : if (can_refer)
10431 : 0 : *can_refer = false;
10432 : 0 : return NULL_TREE;
10433 : : }
10434 : :
10435 : 392653 : init = ctor_for_folding (v);
10436 : :
10437 : : /* The virtual tables should always be born with constructors
10438 : : and we always should assume that they are avaialble for
10439 : : folding. At the moment we do not stream them in all cases,
10440 : : but it should never happen that ctor seem unreachable. */
10441 : 392653 : gcc_assert (init);
10442 : 392653 : if (init == error_mark_node)
10443 : : {
10444 : : /* Pass down that we lost track of the target. */
10445 : 180 : if (can_refer)
10446 : 180 : *can_refer = false;
10447 : 180 : return NULL_TREE;
10448 : : }
10449 : 392473 : gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
10450 : 392473 : size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
10451 : 392473 : offset *= BITS_PER_UNIT;
10452 : 392473 : offset += token * size;
10453 : :
10454 : : /* Lookup the value in the constructor that is assumed to be array.
10455 : : This is equivalent to
10456 : : fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
10457 : : offset, size, NULL);
10458 : : but in a constant time. We expect that frontend produced a simple
10459 : : array without indexed initializers. */
10460 : :
10461 : 392473 : gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
10462 : 392473 : domain_type = TYPE_DOMAIN (TREE_TYPE (init));
10463 : 392473 : gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
10464 : 392473 : elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
10465 : :
10466 : 392473 : access_index = offset / BITS_PER_UNIT / elt_size;
10467 : 392473 : gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
10468 : :
10469 : : /* This code makes an assumption that there are no
10470 : : indexed fileds produced by C++ FE, so we can directly index the array. */
10471 : 392473 : if (access_index < CONSTRUCTOR_NELTS (init))
10472 : : {
10473 : 392472 : fn = CONSTRUCTOR_ELT (init, access_index)->value;
10474 : 392472 : gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
10475 : 392472 : STRIP_NOPS (fn);
10476 : : }
10477 : : else
10478 : : fn = NULL;
10479 : :
10480 : : /* For type inconsistent program we may end up looking up virtual method
10481 : : in virtual table that does not contain TOKEN entries. We may overrun
10482 : : the virtual table and pick up a constant or RTTI info pointer.
10483 : : In any case the call is undefined. */
10484 : 392472 : if (!fn
10485 : 392472 : || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
10486 : 778057 : || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
10487 : 6888 : fn = builtin_decl_unreachable ();
10488 : : else
10489 : : {
10490 : 385585 : fn = TREE_OPERAND (fn, 0);
10491 : :
10492 : : /* When cgraph node is missing and function is not public, we cannot
10493 : : devirtualize. This can happen in WHOPR when the actual method
10494 : : ends up in other partition, because we found devirtualization
10495 : : possibility too late. */
10496 : 385585 : if (!can_refer_decl_in_current_unit_p (fn, vtable))
10497 : : {
10498 : 48999 : if (can_refer)
10499 : : {
10500 : 48999 : *can_refer = false;
10501 : 48999 : return fn;
10502 : : }
10503 : : return NULL_TREE;
10504 : : }
10505 : : }
10506 : :
10507 : : /* Make sure we create a cgraph node for functions we'll reference.
10508 : : They can be non-existent if the reference comes from an entry
10509 : : of an external vtable for example. */
10510 : 343474 : cgraph_node::get_create (fn);
10511 : :
10512 : 343474 : return fn;
10513 : : }
10514 : :
10515 : : /* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
10516 : : is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
10517 : : KNOWN_BINFO carries the binfo describing the true type of
10518 : : OBJ_TYPE_REF_OBJECT(REF).
10519 : : Set CAN_REFER if non-NULL to false if method
10520 : : is not referable or if the virtual table is ill-formed (such as rewriten
10521 : : by non-C++ produced symbol). Otherwise just return NULL in that calse. */
10522 : :
10523 : : tree
10524 : 364543 : gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
10525 : : bool *can_refer)
10526 : : {
10527 : 364543 : unsigned HOST_WIDE_INT offset;
10528 : 364543 : tree v;
10529 : :
10530 : 364543 : v = BINFO_VTABLE (known_binfo);
10531 : : /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
10532 : 364543 : if (!v)
10533 : : return NULL_TREE;
10534 : :
10535 : 364543 : if (!vtable_pointer_value_to_vtable (v, &v, &offset))
10536 : : {
10537 : 0 : if (can_refer)
10538 : 0 : *can_refer = false;
10539 : 0 : return NULL_TREE;
10540 : : }
10541 : 364543 : return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
10542 : : }
10543 : :
10544 : : /* Given a pointer value T, return a simplified version of an
10545 : : indirection through T, or NULL_TREE if no simplification is
10546 : : possible. Note that the resulting type may be different from
10547 : : the type pointed to in the sense that it is still compatible
10548 : : from the langhooks point of view. */
10549 : :
10550 : : tree
10551 : 2010924 : gimple_fold_indirect_ref (tree t)
10552 : : {
10553 : 2010924 : tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
10554 : 2010924 : tree sub = t;
10555 : 2010924 : tree subtype;
10556 : :
10557 : 2010924 : STRIP_NOPS (sub);
10558 : 2010924 : subtype = TREE_TYPE (sub);
10559 : 2010924 : if (!POINTER_TYPE_P (subtype)
10560 : 2010924 : || TYPE_REF_CAN_ALIAS_ALL (ptype))
10561 : : return NULL_TREE;
10562 : :
10563 : 2009203 : if (TREE_CODE (sub) == ADDR_EXPR)
10564 : : {
10565 : 108375 : tree op = TREE_OPERAND (sub, 0);
10566 : 108375 : tree optype = TREE_TYPE (op);
10567 : : /* *&p => p */
10568 : 108375 : if (useless_type_conversion_p (type, optype))
10569 : : return op;
10570 : :
10571 : : /* *(foo *)&fooarray => fooarray[0] */
10572 : 895 : if (TREE_CODE (optype) == ARRAY_TYPE
10573 : 307 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
10574 : 1202 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10575 : : {
10576 : 54 : tree type_domain = TYPE_DOMAIN (optype);
10577 : 54 : tree min_val = size_zero_node;
10578 : 54 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10579 : 54 : min_val = TYPE_MIN_VALUE (type_domain);
10580 : 54 : if (TREE_CODE (min_val) == INTEGER_CST)
10581 : 54 : return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
10582 : : }
10583 : : /* *(foo *)&complexfoo => __real__ complexfoo */
10584 : 841 : else if (TREE_CODE (optype) == COMPLEX_TYPE
10585 : 841 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10586 : 4 : return fold_build1 (REALPART_EXPR, type, op);
10587 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
10588 : 837 : else if (TREE_CODE (optype) == VECTOR_TYPE
10589 : 837 : && useless_type_conversion_p (type, TREE_TYPE (optype)))
10590 : : {
10591 : 26 : tree part_width = TYPE_SIZE (type);
10592 : 26 : tree index = bitsize_int (0);
10593 : 26 : return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
10594 : : }
10595 : : }
10596 : :
10597 : : /* *(p + CST) -> ... */
10598 : 1901639 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
10599 : 1901639 : && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
10600 : : {
10601 : 34222 : tree addr = TREE_OPERAND (sub, 0);
10602 : 34222 : tree off = TREE_OPERAND (sub, 1);
10603 : 34222 : tree addrtype;
10604 : :
10605 : 34222 : STRIP_NOPS (addr);
10606 : 34222 : addrtype = TREE_TYPE (addr);
10607 : :
10608 : : /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
10609 : 34222 : if (TREE_CODE (addr) == ADDR_EXPR
10610 : 92 : && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
10611 : 39 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
10612 : 34250 : && tree_fits_uhwi_p (off))
10613 : : {
10614 : 28 : unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
10615 : 28 : tree part_width = TYPE_SIZE (type);
10616 : 28 : unsigned HOST_WIDE_INT part_widthi
10617 : 28 : = tree_to_shwi (part_width) / BITS_PER_UNIT;
10618 : 28 : unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
10619 : 28 : tree index = bitsize_int (indexi);
10620 : 28 : if (known_lt (offset / part_widthi,
10621 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
10622 : 28 : return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
10623 : : part_width, index);
10624 : : }
10625 : :
10626 : : /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
10627 : 34194 : if (TREE_CODE (addr) == ADDR_EXPR
10628 : 64 : && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
10629 : 34195 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
10630 : : {
10631 : 1 : tree size = TYPE_SIZE_UNIT (type);
10632 : 1 : if (tree_int_cst_equal (size, off))
10633 : 1 : return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
10634 : : }
10635 : :
10636 : : /* *(p + CST) -> MEM_REF <p, CST>. */
10637 : 34193 : if (TREE_CODE (addr) != ADDR_EXPR
10638 : 34193 : || DECL_P (TREE_OPERAND (addr, 0)))
10639 : 34175 : return fold_build2 (MEM_REF, type,
10640 : : addr,
10641 : : wide_int_to_tree (ptype, wi::to_wide (off)));
10642 : : }
10643 : :
10644 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10645 : 1867435 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10646 : 2500 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
10647 : 1869917 : && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10648 : : {
10649 : 1 : tree type_domain;
10650 : 1 : tree min_val = size_zero_node;
10651 : 1 : tree osub = sub;
10652 : 1 : sub = gimple_fold_indirect_ref (sub);
10653 : 1 : if (! sub)
10654 : 1 : sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
10655 : 1 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
10656 : 1 : if (type_domain && TYPE_MIN_VALUE (type_domain))
10657 : 1 : min_val = TYPE_MIN_VALUE (type_domain);
10658 : 1 : if (TREE_CODE (min_val) == INTEGER_CST)
10659 : 1 : return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
10660 : : }
10661 : :
10662 : : return NULL_TREE;
10663 : : }
10664 : :
10665 : : /* Return true if CODE is an operation that when operating on signed
10666 : : integer types involves undefined behavior on overflow and the
10667 : : operation can be expressed with unsigned arithmetic. */
10668 : :
10669 : : bool
10670 : 428970 : arith_code_with_undefined_signed_overflow (tree_code code)
10671 : : {
10672 : 428970 : switch (code)
10673 : : {
10674 : : case ABS_EXPR:
10675 : : case PLUS_EXPR:
10676 : : case MINUS_EXPR:
10677 : : case MULT_EXPR:
10678 : : case NEGATE_EXPR:
10679 : : case POINTER_PLUS_EXPR:
10680 : : return true;
10681 : 176363 : default:
10682 : 176363 : return false;
10683 : : }
10684 : : }
10685 : :
10686 : : /* Return true if STMT has an operation that operates on a signed
10687 : : integer types involves undefined behavior on overflow and the
10688 : : operation can be expressed with unsigned arithmetic.
10689 : : Also returns true if STMT is a VCE that needs to be rewritten
10690 : : if moved to be executed unconditionally. */
10691 : :
10692 : : bool
10693 : 1165948 : gimple_needing_rewrite_undefined (gimple *stmt)
10694 : : {
10695 : 1165948 : if (!is_gimple_assign (stmt))
10696 : : return false;
10697 : 997428 : tree lhs = gimple_assign_lhs (stmt);
10698 : 997428 : if (!lhs)
10699 : : return false;
10700 : 997428 : tree lhs_type = TREE_TYPE (lhs);
10701 : 997428 : if (!INTEGRAL_TYPE_P (lhs_type)
10702 : 106298 : && !POINTER_TYPE_P (lhs_type))
10703 : : return false;
10704 : 961088 : tree rhs = gimple_assign_rhs1 (stmt);
10705 : : /* Boolean loads need special handling as they are treated as a full MODE load
10706 : : and don't mask off the bits for the precision. */
10707 : 961088 : if (gimple_assign_load_p (stmt)
10708 : : /* Booleans are the integral type which has this non-masking issue. */
10709 : 94118 : && TREE_CODE (lhs_type) == BOOLEAN_TYPE
10710 : : /* Only non mode precision booleans are need the masking. */
10711 : 352 : && !type_has_mode_precision_p (lhs_type)
10712 : : /* BFR should be the correct thing and just grab the precision. */
10713 : 352 : && TREE_CODE (rhs) != BIT_FIELD_REF
10714 : : /* Bit-fields loads don't need a rewrite as the masking
10715 : : happens for them. */
10716 : 961440 : && (TREE_CODE (rhs) != COMPONENT_REF
10717 : 155 : || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1))))
10718 : : return true;
10719 : : /* VCE from integral types to a integral types but with
10720 : : a smaller precision need to be changed into casts
10721 : : to be well defined. */
10722 : 960736 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
10723 : 195 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
10724 : 167 : && is_gimple_val (TREE_OPERAND (rhs, 0))
10725 : 960903 : && TYPE_PRECISION (lhs_type)
10726 : 167 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (rhs, 0))))
10727 : : return true;
10728 : 960569 : if (!TYPE_OVERFLOW_UNDEFINED (lhs_type))
10729 : : return false;
10730 : 317381 : if (!arith_code_with_undefined_signed_overflow
10731 : 317381 : (gimple_assign_rhs_code (stmt)))
10732 : : return false;
10733 : : return true;
10734 : : }
10735 : :
10736 : : /* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
10737 : : operation that can be transformed to unsigned arithmetic by converting
10738 : : its operand, carrying out the operation in the corresponding unsigned
10739 : : type and converting the result back to the original type.
10740 : :
10741 : : If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and
10742 : : return NULL.
10743 : : Otherwise returns a sequence of statements that replace STMT and also
10744 : : contain a modified form of STMT itself. */
10745 : :
10746 : : static gimple_seq
10747 : 42248 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi, gimple *stmt,
10748 : : bool in_place)
10749 : : {
10750 : 42248 : gcc_assert (gimple_needing_rewrite_undefined (stmt));
10751 : 42248 : if (dump_file && (dump_flags & TDF_DETAILS))
10752 : : {
10753 : 21 : fprintf (dump_file, "rewriting stmt for being uncondtional defined");
10754 : 21 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
10755 : : }
10756 : 42248 : gimple_seq stmts = NULL;
10757 : 42248 : tree lhs = gimple_assign_lhs (stmt);
10758 : :
10759 : : /* Boolean loads need to be rewritten to be a load from the same mode
10760 : : and then a cast to the other type so the other bits are masked off
10761 : : correctly since the load was done conditionally. It is similar to the VCE
10762 : : case below. */
10763 : 42248 : if (gimple_assign_load_p (stmt)
10764 : 42248 : && TREE_CODE (TREE_TYPE (lhs)) == BOOLEAN_TYPE)
10765 : : {
10766 : 90 : tree rhs = gimple_assign_rhs1 (stmt);
10767 : :
10768 : : /* Double check that gimple_needing_rewrite_undefined was called. */
10769 : : /* Bit-fields loads will do the masking so don't need the rewriting. */
10770 : 90 : gcc_assert (TREE_CODE (rhs) != COMPONENT_REF
10771 : : || !DECL_BIT_FIELD (TREE_OPERAND (rhs, 1)));
10772 : : /* BFR is like a bit field load and will do the correct thing. */
10773 : 90 : gcc_assert (TREE_CODE (lhs) != BIT_FIELD_REF);
10774 : : /* Complex boolean types are not valid so REAL/IMAG part will
10775 : : never show up. */
10776 : 90 : gcc_assert (TREE_CODE (rhs) != REALPART_EXPR
10777 : : && TREE_CODE (lhs) != IMAGPART_EXPR);
10778 : :
10779 : 90 : auto bits = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (rhs)));
10780 : 90 : tree new_type = build_nonstandard_integer_type (bits, true);
10781 : 90 : location_t loc = gimple_location (stmt);
10782 : 90 : tree mem_ref = fold_build1_loc (loc, VIEW_CONVERT_EXPR, new_type, rhs);
10783 : : /* Replace the original load with a new load and a new lhs. */
10784 : 90 : tree new_lhs = make_ssa_name (new_type);
10785 : 90 : gimple_assign_set_rhs1 (stmt, mem_ref);
10786 : 90 : gimple_assign_set_lhs (stmt, new_lhs);
10787 : :
10788 : 90 : if (in_place)
10789 : 49 : update_stmt (stmt);
10790 : : else
10791 : : {
10792 : 41 : gimple_set_modified (stmt, true);
10793 : 41 : gimple_seq_add_stmt (&stmts, stmt);
10794 : : }
10795 : :
10796 : : /* Build the conversion statement. */
10797 : 90 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, new_lhs);
10798 : 90 : if (in_place)
10799 : : {
10800 : 49 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
10801 : 49 : update_stmt (stmt);
10802 : : }
10803 : : else
10804 : 41 : gimple_seq_add_stmt (&stmts, cvt);
10805 : 90 : return stmts;
10806 : : }
10807 : :
10808 : : /* VCE from integral types to another integral types but with
10809 : : smaller precisions need to be changed into casts
10810 : : to be well defined. */
10811 : 42158 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
10812 : : {
10813 : 60 : tree rhs = gimple_assign_rhs1 (stmt);
10814 : 60 : tree new_rhs = TREE_OPERAND (rhs, 0);
10815 : 60 : gcc_assert (TYPE_PRECISION (TREE_TYPE (rhs))
10816 : : < TYPE_PRECISION (TREE_TYPE (new_rhs)));
10817 : 60 : gcc_assert (is_gimple_val (new_rhs));
10818 : 60 : gimple_assign_set_rhs_code (stmt, NOP_EXPR);
10819 : 60 : gimple_assign_set_rhs1 (stmt, new_rhs);
10820 : 60 : if (in_place)
10821 : 51 : update_stmt (stmt);
10822 : : else
10823 : : {
10824 : 9 : gimple_set_modified (stmt, true);
10825 : 9 : gimple_seq_add_stmt (&stmts, stmt);
10826 : : }
10827 : 60 : return stmts;
10828 : : }
10829 : 42098 : tree type = unsigned_type_for (TREE_TYPE (lhs));
10830 : 42098 : if (gimple_assign_rhs_code (stmt) == ABS_EXPR)
10831 : 17 : gimple_assign_set_rhs_code (stmt, ABSU_EXPR);
10832 : : else
10833 : 124813 : for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
10834 : : {
10835 : 82732 : tree op = gimple_op (stmt, i);
10836 : 82732 : op = gimple_convert (&stmts, type, op);
10837 : 82732 : gimple_set_op (stmt, i, op);
10838 : : }
10839 : 42098 : gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
10840 : 42098 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
10841 : 6489 : gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
10842 : 42098 : gimple_set_modified (stmt, true);
10843 : 42098 : if (in_place)
10844 : : {
10845 : 22279 : if (stmts)
10846 : 21890 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
10847 : 22279 : stmts = NULL;
10848 : : }
10849 : : else
10850 : 19819 : gimple_seq_add_stmt (&stmts, stmt);
10851 : 42098 : gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
10852 : 42098 : if (in_place)
10853 : : {
10854 : 22279 : gsi_insert_after (gsi, cvt, GSI_SAME_STMT);
10855 : 22279 : update_stmt (stmt);
10856 : : }
10857 : : else
10858 : 19819 : gimple_seq_add_stmt (&stmts, cvt);
10859 : :
10860 : 42098 : return stmts;
10861 : : }
10862 : :
10863 : : void
10864 : 22379 : rewrite_to_defined_unconditional (gimple_stmt_iterator *gsi)
10865 : : {
10866 : 22379 : rewrite_to_defined_unconditional (gsi, gsi_stmt (*gsi), true);
10867 : 22379 : }
10868 : :
10869 : : gimple_seq
10870 : 19869 : rewrite_to_defined_unconditional (gimple *stmt)
10871 : : {
10872 : 19869 : return rewrite_to_defined_unconditional (nullptr, stmt, false);
10873 : : }
10874 : :
10875 : : /* The valueization hook we use for the gimple_build API simplification.
10876 : : This makes us match fold_buildN behavior by only combining with
10877 : : statements in the sequence(s) we are currently building. */
10878 : :
10879 : : static tree
10880 : 18729318 : gimple_build_valueize (tree op)
10881 : : {
10882 : 18729318 : if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
10883 : 4276398 : return op;
10884 : : return NULL_TREE;
10885 : : }
10886 : :
10887 : : /* Helper for gimple_build to perform the final insertion of stmts on SEQ. */
10888 : :
10889 : : static inline void
10890 : 1289132 : gimple_build_insert_seq (gimple_stmt_iterator *gsi,
10891 : : bool before, gsi_iterator_update update,
10892 : : gimple_seq seq)
10893 : : {
10894 : 1289132 : if (before)
10895 : : {
10896 : 75533 : if (gsi->bb)
10897 : 75533 : gsi_insert_seq_before (gsi, seq, update);
10898 : : else
10899 : 0 : gsi_insert_seq_before_without_update (gsi, seq, update);
10900 : : }
10901 : : else
10902 : : {
10903 : 1213599 : if (gsi->bb)
10904 : 117 : gsi_insert_seq_after (gsi, seq, update);
10905 : : else
10906 : 1213482 : gsi_insert_seq_after_without_update (gsi, seq, update);
10907 : : }
10908 : 1289132 : }
10909 : :
10910 : : /* Build the expression CODE OP0 of type TYPE with location LOC,
10911 : : simplifying it first if possible. Returns the built
10912 : : expression value and inserts statements possibly defining it
10913 : : before GSI if BEFORE is true or after GSI if false and advance
10914 : : the iterator accordingly.
10915 : : If gsi refers to a basic block simplifying is allowed to look
10916 : : at all SSA defs while when it does not it is restricted to
10917 : : SSA defs that are not associated with a basic block yet,
10918 : : indicating they belong to the currently building sequence. */
10919 : :
10920 : : tree
10921 : 298034 : gimple_build (gimple_stmt_iterator *gsi,
10922 : : bool before, gsi_iterator_update update,
10923 : : location_t loc, enum tree_code code, tree type, tree op0)
10924 : : {
10925 : 298034 : gimple_seq seq = NULL;
10926 : 298034 : tree res
10927 : 298034 : = gimple_simplify (code, type, op0, &seq,
10928 : 298034 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
10929 : 298034 : if (!res)
10930 : : {
10931 : 267613 : res = make_ssa_name (type);
10932 : 267613 : gimple *stmt;
10933 : 267613 : if (code == REALPART_EXPR
10934 : : || code == IMAGPART_EXPR
10935 : 267613 : || code == VIEW_CONVERT_EXPR)
10936 : 12669 : stmt = gimple_build_assign (res, code, build1 (code, type, op0));
10937 : : else
10938 : 254944 : stmt = gimple_build_assign (res, code, op0);
10939 : 267613 : gimple_set_location (stmt, loc);
10940 : 267613 : gimple_seq_add_stmt_without_update (&seq, stmt);
10941 : : }
10942 : 298034 : gimple_build_insert_seq (gsi, before, update, seq);
10943 : 298034 : return res;
10944 : : }
10945 : :
10946 : : /* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
10947 : : simplifying it first if possible. Returns the built
10948 : : expression value inserting any new statements at GSI honoring BEFORE
10949 : : and UPDATE. */
10950 : :
10951 : : tree
10952 : 777945 : gimple_build (gimple_stmt_iterator *gsi,
10953 : : bool before, gsi_iterator_update update,
10954 : : location_t loc, enum tree_code code, tree type,
10955 : : tree op0, tree op1)
10956 : : {
10957 : 777945 : gimple_seq seq = NULL;
10958 : 777945 : tree res
10959 : 777945 : = gimple_simplify (code, type, op0, op1, &seq,
10960 : 777945 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
10961 : 777945 : if (!res)
10962 : : {
10963 : 685660 : res = make_ssa_name (type);
10964 : 685660 : gimple *stmt = gimple_build_assign (res, code, op0, op1);
10965 : 685660 : gimple_set_location (stmt, loc);
10966 : 685660 : gimple_seq_add_stmt_without_update (&seq, stmt);
10967 : : }
10968 : 777945 : gimple_build_insert_seq (gsi, before, update, seq);
10969 : 777945 : return res;
10970 : : }
10971 : :
10972 : : /* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
10973 : : simplifying it first if possible. Returns the built
10974 : : expression value inserting any new statements at GSI honoring BEFORE
10975 : : and UPDATE. */
10976 : :
10977 : : tree
10978 : 43966 : gimple_build (gimple_stmt_iterator *gsi,
10979 : : bool before, gsi_iterator_update update,
10980 : : location_t loc, enum tree_code code, tree type,
10981 : : tree op0, tree op1, tree op2)
10982 : : {
10983 : :
10984 : 43966 : gimple_seq seq = NULL;
10985 : 43966 : tree res
10986 : 43966 : = gimple_simplify (code, type, op0, op1, op2, &seq,
10987 : 43966 : gsi->bb ? follow_all_ssa_edges : gimple_build_valueize);
10988 : 43966 : if (!res)
10989 : : {
10990 : 32553 : res = make_ssa_name (type);
10991 : 32553 : gimple *stmt;
10992 : 32553 : if (code == BIT_FIELD_REF)
10993 : 25974 : stmt = gimple_build_assign (res, code,
10994 : : build3 (code, type, op0, op1, op2));
10995 : : else
10996 : 6579 : stmt = gimple_build_assign (res, code, op0, op1, op2);
10997 : 32553 : gimple_set_location (stmt, loc);
10998 : 32553 : gimple_seq_add_stmt_without_update (&seq, stmt);
10999 : : }
11000 : 43966 : gimple_build_insert_seq (gsi, before, update, seq);
11001 : 43966 : return res;
11002 : : }
11003 : :
11004 : : /* Build the call FN () with a result of type TYPE (or no result if TYPE is
11005 : : void) with a location LOC. Returns the built expression value (or NULL_TREE
11006 : : if TYPE is void) inserting any new statements at GSI honoring BEFORE
11007 : : and UPDATE. */
11008 : :
11009 : : tree
11010 : 0 : gimple_build (gimple_stmt_iterator *gsi,
11011 : : bool before, gsi_iterator_update update,
11012 : : location_t loc, combined_fn fn, tree type)
11013 : : {
11014 : 0 : tree res = NULL_TREE;
11015 : 0 : gimple_seq seq = NULL;
11016 : 0 : gcall *stmt;
11017 : 0 : if (internal_fn_p (fn))
11018 : 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 0);
11019 : : else
11020 : : {
11021 : 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11022 : 0 : stmt = gimple_build_call (decl, 0);
11023 : : }
11024 : 0 : if (!VOID_TYPE_P (type))
11025 : : {
11026 : 0 : res = make_ssa_name (type);
11027 : 0 : gimple_call_set_lhs (stmt, res);
11028 : : }
11029 : 0 : gimple_set_location (stmt, loc);
11030 : 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11031 : 0 : gimple_build_insert_seq (gsi, before, update, seq);
11032 : 0 : return res;
11033 : : }
11034 : :
11035 : : /* Build the call FN (ARG0) with a result of type TYPE
11036 : : (or no result if TYPE is void) with location LOC,
11037 : : simplifying it first if possible. Returns the built
11038 : : expression value (or NULL_TREE if TYPE is void) inserting any new
11039 : : statements at GSI honoring BEFORE and UPDATE. */
11040 : :
11041 : : tree
11042 : 24251 : gimple_build (gimple_stmt_iterator *gsi,
11043 : : bool before, gsi_iterator_update update,
11044 : : location_t loc, combined_fn fn,
11045 : : tree type, tree arg0)
11046 : : {
11047 : 24251 : gimple_seq seq = NULL;
11048 : 24251 : tree res = gimple_simplify (fn, type, arg0, &seq, gimple_build_valueize);
11049 : 24251 : if (!res)
11050 : : {
11051 : 24251 : gcall *stmt;
11052 : 24251 : if (internal_fn_p (fn))
11053 : 23874 : stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
11054 : : else
11055 : : {
11056 : 377 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11057 : 377 : stmt = gimple_build_call (decl, 1, arg0);
11058 : : }
11059 : 24251 : if (!VOID_TYPE_P (type))
11060 : : {
11061 : 23874 : res = make_ssa_name (type);
11062 : 23874 : gimple_call_set_lhs (stmt, res);
11063 : : }
11064 : 24251 : gimple_set_location (stmt, loc);
11065 : 24251 : gimple_seq_add_stmt_without_update (&seq, stmt);
11066 : : }
11067 : 24251 : gimple_build_insert_seq (gsi, before, update, seq);
11068 : 24251 : return res;
11069 : : }
11070 : :
11071 : : /* Build the call FN (ARG0, ARG1) with a result of type TYPE
11072 : : (or no result if TYPE is void) with location LOC,
11073 : : simplifying it first if possible. Returns the built
11074 : : expression value (or NULL_TREE if TYPE is void) inserting any new
11075 : : statements at GSI honoring BEFORE and UPDATE. */
11076 : :
11077 : : tree
11078 : 0 : gimple_build (gimple_stmt_iterator *gsi,
11079 : : bool before, gsi_iterator_update update,
11080 : : location_t loc, combined_fn fn,
11081 : : tree type, tree arg0, tree arg1)
11082 : : {
11083 : 0 : gimple_seq seq = NULL;
11084 : 0 : tree res = gimple_simplify (fn, type, arg0, arg1, &seq,
11085 : : gimple_build_valueize);
11086 : 0 : if (!res)
11087 : : {
11088 : 0 : gcall *stmt;
11089 : 0 : if (internal_fn_p (fn))
11090 : 0 : stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
11091 : : else
11092 : : {
11093 : 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11094 : 0 : stmt = gimple_build_call (decl, 2, arg0, arg1);
11095 : : }
11096 : 0 : if (!VOID_TYPE_P (type))
11097 : : {
11098 : 0 : res = make_ssa_name (type);
11099 : 0 : gimple_call_set_lhs (stmt, res);
11100 : : }
11101 : 0 : gimple_set_location (stmt, loc);
11102 : 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11103 : : }
11104 : 0 : gimple_build_insert_seq (gsi, before, update, seq);
11105 : 0 : return res;
11106 : : }
11107 : :
11108 : : /* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
11109 : : (or no result if TYPE is void) with location LOC,
11110 : : simplifying it first if possible. Returns the built
11111 : : expression value (or NULL_TREE if TYPE is void) inserting any new
11112 : : statements at GSI honoring BEFORE and UPDATE. */
11113 : :
11114 : : tree
11115 : 0 : gimple_build (gimple_stmt_iterator *gsi,
11116 : : bool before, gsi_iterator_update update,
11117 : : location_t loc, combined_fn fn,
11118 : : tree type, tree arg0, tree arg1, tree arg2)
11119 : : {
11120 : 0 : gimple_seq seq = NULL;
11121 : 0 : tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
11122 : : &seq, gimple_build_valueize);
11123 : 0 : if (!res)
11124 : : {
11125 : 0 : gcall *stmt;
11126 : 0 : if (internal_fn_p (fn))
11127 : 0 : stmt = gimple_build_call_internal (as_internal_fn (fn),
11128 : : 3, arg0, arg1, arg2);
11129 : : else
11130 : : {
11131 : 0 : tree decl = builtin_decl_implicit (as_builtin_fn (fn));
11132 : 0 : stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
11133 : : }
11134 : 0 : if (!VOID_TYPE_P (type))
11135 : : {
11136 : 0 : res = make_ssa_name (type);
11137 : 0 : gimple_call_set_lhs (stmt, res);
11138 : : }
11139 : 0 : gimple_set_location (stmt, loc);
11140 : 0 : gimple_seq_add_stmt_without_update (&seq, stmt);
11141 : : }
11142 : 0 : gimple_build_insert_seq (gsi, before, update, seq);
11143 : 0 : return res;
11144 : : }
11145 : :
11146 : : /* Build CODE (OP0) with a result of type TYPE (or no result if TYPE is
11147 : : void) with location LOC, simplifying it first if possible. Returns the
11148 : : built expression value (or NULL_TREE if TYPE is void) inserting any new
11149 : : statements at GSI honoring BEFORE and UPDATE. */
11150 : :
11151 : : tree
11152 : 21 : gimple_build (gimple_stmt_iterator *gsi,
11153 : : bool before, gsi_iterator_update update,
11154 : : location_t loc, code_helper code, tree type, tree op0)
11155 : : {
11156 : 21 : if (code.is_tree_code ())
11157 : 0 : return gimple_build (gsi, before, update, loc, tree_code (code), type, op0);
11158 : 21 : return gimple_build (gsi, before, update, loc, combined_fn (code), type, op0);
11159 : : }
11160 : :
11161 : : /* Build CODE (OP0, OP1) with a result of type TYPE (or no result if TYPE is
11162 : : void) with location LOC, simplifying it first if possible. Returns the
11163 : : built expression value (or NULL_TREE if TYPE is void) inserting any new
11164 : : statements at GSI honoring BEFORE and UPDATE. */
11165 : :
11166 : : tree
11167 : 24415 : gimple_build (gimple_stmt_iterator *gsi,
11168 : : bool before, gsi_iterator_update update,
11169 : : location_t loc, code_helper code, tree type, tree op0, tree op1)
11170 : : {
11171 : 24415 : if (code.is_tree_code ())
11172 : 24415 : return gimple_build (gsi, before, update,
11173 : 24415 : loc, tree_code (code), type, op0, op1);
11174 : 0 : return gimple_build (gsi, before, update,
11175 : 0 : loc, combined_fn (code), type, op0, op1);
11176 : : }
11177 : :
11178 : : /* Build CODE (OP0, OP1, OP2) with a result of type TYPE (or no result if TYPE
11179 : : is void) with location LOC, simplifying it first if possible. Returns the
11180 : : built expression value (or NULL_TREE if TYPE is void) inserting any new
11181 : : statements at GSI honoring BEFORE and UPDATE. */
11182 : :
11183 : : tree
11184 : 0 : gimple_build (gimple_stmt_iterator *gsi,
11185 : : bool before, gsi_iterator_update update,
11186 : : location_t loc, code_helper code,
11187 : : tree type, tree op0, tree op1, tree op2)
11188 : : {
11189 : 0 : if (code.is_tree_code ())
11190 : 0 : return gimple_build (gsi, before, update,
11191 : 0 : loc, tree_code (code), type, op0, op1, op2);
11192 : 0 : return gimple_build (gsi, before, update,
11193 : 0 : loc, combined_fn (code), type, op0, op1, op2);
11194 : : }
11195 : :
11196 : : /* Build the conversion (TYPE) OP with a result of type TYPE
11197 : : with location LOC if such conversion is neccesary in GIMPLE,
11198 : : simplifying it first.
11199 : : Returns the built expression inserting any new statements
11200 : : at GSI honoring BEFORE and UPDATE. */
11201 : :
11202 : : tree
11203 : 1907125 : gimple_convert (gimple_stmt_iterator *gsi,
11204 : : bool before, gsi_iterator_update update,
11205 : : location_t loc, tree type, tree op)
11206 : : {
11207 : 1907125 : if (useless_type_conversion_p (type, TREE_TYPE (op)))
11208 : : return op;
11209 : 136388 : return gimple_build (gsi, before, update, loc, NOP_EXPR, type, op);
11210 : : }
11211 : :
11212 : : /* Build the conversion (ptrofftype) OP with a result of a type
11213 : : compatible with ptrofftype with location LOC if such conversion
11214 : : is neccesary in GIMPLE, simplifying it first.
11215 : : Returns the built expression value inserting any new statements
11216 : : at GSI honoring BEFORE and UPDATE. */
11217 : :
11218 : : tree
11219 : 203 : gimple_convert_to_ptrofftype (gimple_stmt_iterator *gsi,
11220 : : bool before, gsi_iterator_update update,
11221 : : location_t loc, tree op)
11222 : : {
11223 : 203 : if (ptrofftype_p (TREE_TYPE (op)))
11224 : : return op;
11225 : 0 : return gimple_convert (gsi, before, update, loc, sizetype, op);
11226 : : }
11227 : :
11228 : : /* Build a vector of type TYPE in which each element has the value OP.
11229 : : Return a gimple value for the result, inserting any new statements
11230 : : at GSI honoring BEFORE and UPDATE. */
11231 : :
11232 : : tree
11233 : 324389 : gimple_build_vector_from_val (gimple_stmt_iterator *gsi,
11234 : : bool before, gsi_iterator_update update,
11235 : : location_t loc, tree type, tree op)
11236 : : {
11237 : 324389 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
11238 : : && !CONSTANT_CLASS_P (op))
11239 : : return gimple_build (gsi, before, update,
11240 : : loc, VEC_DUPLICATE_EXPR, type, op);
11241 : :
11242 : 324389 : tree res, vec = build_vector_from_val (type, op);
11243 : 324389 : if (is_gimple_val (vec))
11244 : : return vec;
11245 : 26538 : if (gimple_in_ssa_p (cfun))
11246 : 26538 : res = make_ssa_name (type);
11247 : : else
11248 : 0 : res = create_tmp_reg (type);
11249 : 26538 : gimple_seq seq = NULL;
11250 : 26538 : gimple *stmt = gimple_build_assign (res, vec);
11251 : 26538 : gimple_set_location (stmt, loc);
11252 : 26538 : gimple_seq_add_stmt_without_update (&seq, stmt);
11253 : 26538 : gimple_build_insert_seq (gsi, before, update, seq);
11254 : 26538 : return res;
11255 : : }
11256 : :
11257 : : /* Build a vector from BUILDER, handling the case in which some elements
11258 : : are non-constant. Return a gimple value for the result, inserting
11259 : : any new instructions to GSI honoring BEFORE and UPDATE.
11260 : :
11261 : : BUILDER must not have a stepped encoding on entry. This is because
11262 : : the function is not geared up to handle the arithmetic that would
11263 : : be needed in the variable case, and any code building a vector that
11264 : : is known to be constant should use BUILDER->build () directly. */
11265 : :
11266 : : tree
11267 : 374099 : gimple_build_vector (gimple_stmt_iterator *gsi,
11268 : : bool before, gsi_iterator_update update,
11269 : : location_t loc, tree_vector_builder *builder)
11270 : : {
11271 : 374099 : gcc_assert (builder->nelts_per_pattern () <= 2);
11272 : 374099 : unsigned int encoded_nelts = builder->encoded_nelts ();
11273 : 1335137 : for (unsigned int i = 0; i < encoded_nelts; ++i)
11274 : 1079436 : if (!CONSTANT_CLASS_P ((*builder)[i]))
11275 : : {
11276 : 118398 : gimple_seq seq = NULL;
11277 : 118398 : tree type = builder->type ();
11278 : 118398 : unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11279 : 118398 : vec<constructor_elt, va_gc> *v;
11280 : 118398 : vec_alloc (v, nelts);
11281 : 377728 : for (i = 0; i < nelts; ++i)
11282 : 259330 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
11283 : :
11284 : 118398 : tree res;
11285 : 118398 : if (gimple_in_ssa_p (cfun))
11286 : 118398 : res = make_ssa_name (type);
11287 : : else
11288 : 0 : res = create_tmp_reg (type);
11289 : 118398 : gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
11290 : 118398 : gimple_set_location (stmt, loc);
11291 : 118398 : gimple_seq_add_stmt_without_update (&seq, stmt);
11292 : 118398 : gimple_build_insert_seq (gsi, before, update, seq);
11293 : 118398 : return res;
11294 : : }
11295 : 255701 : return builder->build ();
11296 : : }
11297 : :
11298 : : /* Emit gimple statements into &stmts that take a value given in OLD_SIZE
11299 : : and generate a value guaranteed to be rounded upwards to ALIGN.
11300 : :
11301 : : Return the tree node representing this size, it is of TREE_TYPE TYPE. */
11302 : :
11303 : : tree
11304 : 0 : gimple_build_round_up (gimple_stmt_iterator *gsi,
11305 : : bool before, gsi_iterator_update update,
11306 : : location_t loc, tree type,
11307 : : tree old_size, unsigned HOST_WIDE_INT align)
11308 : : {
11309 : 0 : unsigned HOST_WIDE_INT tg_mask = align - 1;
11310 : : /* tree new_size = (old_size + tg_mask) & ~tg_mask; */
11311 : 0 : gcc_assert (INTEGRAL_TYPE_P (type));
11312 : 0 : tree tree_mask = build_int_cst (type, tg_mask);
11313 : 0 : tree oversize = gimple_build (gsi, before, update,
11314 : : loc, PLUS_EXPR, type, old_size, tree_mask);
11315 : :
11316 : 0 : tree mask = build_int_cst (type, -align);
11317 : 0 : return gimple_build (gsi, before, update,
11318 : 0 : loc, BIT_AND_EXPR, type, oversize, mask);
11319 : : }
11320 : :
11321 : : /* Return true if the result of assignment STMT is known to be non-negative.
11322 : : If the return value is based on the assumption that signed overflow is
11323 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11324 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11325 : :
11326 : : static bool
11327 : 55966630 : gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11328 : : int depth)
11329 : : {
11330 : 55966630 : enum tree_code code = gimple_assign_rhs_code (stmt);
11331 : 55966630 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
11332 : 55966630 : switch (get_gimple_rhs_class (code))
11333 : : {
11334 : 12009140 : case GIMPLE_UNARY_RHS:
11335 : 12009140 : return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
11336 : : type,
11337 : : gimple_assign_rhs1 (stmt),
11338 : 12009140 : strict_overflow_p, depth);
11339 : 38666257 : case GIMPLE_BINARY_RHS:
11340 : 38666257 : return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
11341 : : type,
11342 : : gimple_assign_rhs1 (stmt),
11343 : : gimple_assign_rhs2 (stmt),
11344 : 38666257 : strict_overflow_p, depth);
11345 : : case GIMPLE_TERNARY_RHS:
11346 : : return false;
11347 : 5252056 : case GIMPLE_SINGLE_RHS:
11348 : 5252056 : return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
11349 : 5252056 : strict_overflow_p, depth);
11350 : : case GIMPLE_INVALID_RHS:
11351 : : break;
11352 : : }
11353 : 0 : gcc_unreachable ();
11354 : : }
11355 : :
11356 : : /* Return true if return value of call STMT is known to be non-negative.
11357 : : If the return value is based on the assumption that signed overflow is
11358 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11359 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11360 : :
11361 : : static bool
11362 : 21105853 : gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11363 : : int depth)
11364 : : {
11365 : 21105853 : tree arg0
11366 : 21105853 : = gimple_call_num_args (stmt) > 0 ? gimple_call_arg (stmt, 0) : NULL_TREE;
11367 : 21105853 : tree arg1
11368 : 21105853 : = gimple_call_num_args (stmt) > 1 ? gimple_call_arg (stmt, 1) : NULL_TREE;
11369 : 21105853 : tree lhs = gimple_call_lhs (stmt);
11370 : 21105853 : return (lhs
11371 : 21105853 : && tree_call_nonnegative_warnv_p (TREE_TYPE (lhs),
11372 : : gimple_call_combined_fn (stmt),
11373 : : arg0, arg1,
11374 : 21105853 : strict_overflow_p, depth));
11375 : : }
11376 : :
11377 : : /* Return true if return value of call STMT is known to be non-negative.
11378 : : If the return value is based on the assumption that signed overflow is
11379 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11380 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11381 : :
11382 : : static bool
11383 : 13038456 : gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11384 : : int depth)
11385 : : {
11386 : 26641401 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11387 : : {
11388 : 21429649 : tree arg = gimple_phi_arg_def (stmt, i);
11389 : 21429649 : if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1))
11390 : : return false;
11391 : : }
11392 : : return true;
11393 : : }
11394 : :
11395 : : /* Return true if STMT is known to compute a non-negative value.
11396 : : If the return value is based on the assumption that signed overflow is
11397 : : undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
11398 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
11399 : :
11400 : : bool
11401 : 144870035 : gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
11402 : : int depth)
11403 : : {
11404 : 144870035 : tree type = gimple_range_type (stmt);
11405 : 144870035 : if (type && frange::supports_p (type))
11406 : : {
11407 : 1650073 : frange r;
11408 : 1650073 : bool sign;
11409 : 1650073 : if (get_global_range_query ()->range_of_stmt (r, stmt)
11410 : 1650073 : && r.signbit_p (sign))
11411 : 28738 : return !sign;
11412 : 1650073 : }
11413 : 144841297 : switch (gimple_code (stmt))
11414 : : {
11415 : 55966630 : case GIMPLE_ASSIGN:
11416 : 55966630 : return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p,
11417 : 55966630 : depth);
11418 : 21105853 : case GIMPLE_CALL:
11419 : 21105853 : return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p,
11420 : 21105853 : depth);
11421 : 13038456 : case GIMPLE_PHI:
11422 : 13038456 : return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p,
11423 : 13038456 : depth);
11424 : : default:
11425 : : return false;
11426 : : }
11427 : : }
11428 : :
11429 : : /* Return true if the floating-point value computed by assignment STMT
11430 : : is known to have an integer value. We also allow +Inf, -Inf and NaN
11431 : : to be considered integer values. Return false for signaling NaN.
11432 : :
11433 : : DEPTH is the current nesting depth of the query. */
11434 : :
11435 : : static bool
11436 : 57968 : gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
11437 : : {
11438 : 57968 : enum tree_code code = gimple_assign_rhs_code (stmt);
11439 : 57968 : switch (get_gimple_rhs_class (code))
11440 : : {
11441 : 14871 : case GIMPLE_UNARY_RHS:
11442 : 14871 : return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
11443 : 14871 : gimple_assign_rhs1 (stmt), depth);
11444 : 13082 : case GIMPLE_BINARY_RHS:
11445 : 13082 : return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
11446 : : gimple_assign_rhs1 (stmt),
11447 : 13082 : gimple_assign_rhs2 (stmt), depth);
11448 : : case GIMPLE_TERNARY_RHS:
11449 : : return false;
11450 : 29013 : case GIMPLE_SINGLE_RHS:
11451 : 29013 : return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
11452 : : case GIMPLE_INVALID_RHS:
11453 : : break;
11454 : : }
11455 : 0 : gcc_unreachable ();
11456 : : }
11457 : :
11458 : : /* Return true if the floating-point value computed by call STMT is known
11459 : : to have an integer value. We also allow +Inf, -Inf and NaN to be
11460 : : considered integer values. Return false for signaling NaN.
11461 : :
11462 : : DEPTH is the current nesting depth of the query. */
11463 : :
11464 : : static bool
11465 : 851 : gimple_call_integer_valued_real_p (gimple *stmt, int depth)
11466 : : {
11467 : 851 : tree arg0 = (gimple_call_num_args (stmt) > 0
11468 : 851 : ? gimple_call_arg (stmt, 0)
11469 : : : NULL_TREE);
11470 : 851 : tree arg1 = (gimple_call_num_args (stmt) > 1
11471 : 851 : ? gimple_call_arg (stmt, 1)
11472 : : : NULL_TREE);
11473 : 851 : return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
11474 : 851 : arg0, arg1, depth);
11475 : : }
11476 : :
11477 : : /* Return true if the floating-point result of phi STMT is known to have
11478 : : an integer value. We also allow +Inf, -Inf and NaN to be considered
11479 : : integer values. Return false for signaling NaN.
11480 : :
11481 : : DEPTH is the current nesting depth of the query. */
11482 : :
11483 : : static bool
11484 : 1465 : gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
11485 : : {
11486 : 1630 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
11487 : : {
11488 : 1625 : tree arg = gimple_phi_arg_def (stmt, i);
11489 : 1625 : if (!integer_valued_real_single_p (arg, depth + 1))
11490 : : return false;
11491 : : }
11492 : : return true;
11493 : : }
11494 : :
11495 : : /* Return true if the floating-point value computed by STMT is known
11496 : : to have an integer value. We also allow +Inf, -Inf and NaN to be
11497 : : considered integer values. Return false for signaling NaN.
11498 : :
11499 : : DEPTH is the current nesting depth of the query. */
11500 : :
11501 : : bool
11502 : 87231 : gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
11503 : : {
11504 : 87231 : switch (gimple_code (stmt))
11505 : : {
11506 : 57968 : case GIMPLE_ASSIGN:
11507 : 57968 : return gimple_assign_integer_valued_real_p (stmt, depth);
11508 : 851 : case GIMPLE_CALL:
11509 : 851 : return gimple_call_integer_valued_real_p (stmt, depth);
11510 : 1465 : case GIMPLE_PHI:
11511 : 1465 : return gimple_phi_integer_valued_real_p (stmt, depth);
11512 : : default:
11513 : : return false;
11514 : : }
11515 : : }
|