]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple-fold.c
c-ada-spec.c (dump_ada_declaration): Generate a forward declaration for a typedef...
[thirdparty/gcc.git] / gcc / gimple-fold.c
CommitLineData
cbdd87d4 1/* Statement simplification on GIMPLE.
85ec4feb 2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
cbdd87d4
RG
3 Split out from tree-ssa-ccp.c.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3, or (at your option) any
10later version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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"
c7131fb2 24#include "backend.h"
957060b5
AM
25#include "target.h"
26#include "rtl.h"
cbdd87d4 27#include "tree.h"
c7131fb2 28#include "gimple.h"
957060b5 29#include "predict.h"
c7131fb2 30#include "ssa.h"
957060b5
AM
31#include "cgraph.h"
32#include "gimple-pretty-print.h"
cc8bea0a 33#include "gimple-ssa-warn-restrict.h"
c7131fb2 34#include "fold-const.h"
36566b39
PK
35#include "stmt.h"
36#include "expr.h"
37#include "stor-layout.h"
7ee2468b 38#include "dumpfile.h"
2fb9a547 39#include "gimple-fold.h"
45b0be94 40#include "gimplify.h"
5be5c238 41#include "gimple-iterator.h"
442b4905
AM
42#include "tree-into-ssa.h"
43#include "tree-dfa.h"
025d57f0 44#include "tree-object-size.h"
7a300452 45#include "tree-ssa.h"
cbdd87d4 46#include "tree-ssa-propagate.h"
450ad0cd 47#include "ipa-utils.h"
4484a35a 48#include "tree-ssa-address.h"
862d0b35 49#include "langhooks.h"
19e51b40 50#include "gimplify-me.h"
2b5f0895 51#include "dbgcnt.h"
9b2b7279 52#include "builtins.h"
e0ee10ed
RB
53#include "tree-eh.h"
54#include "gimple-match.h"
48126138 55#include "gomp-constants.h"
f869c12f 56#include "optabs-query.h"
629b3d75 57#include "omp-general.h"
3de2a40e 58#include "ipa-chkp.h"
abd3a68c 59#include "tree-cfg.h"
a918bfbf 60#include "fold-const-call.h"
314e6352
ML
61#include "stringpool.h"
62#include "attribs.h"
45b2222a 63#include "asan.h"
025d57f0
MS
64#include "diagnostic-core.h"
65#include "intl.h"
6a33d0ff 66#include "calls.h"
5ebaa477 67#include "tree-vector-builder.h"
5d0d5d68 68#include "tree-ssa-strlen.h"
cbdd87d4 69
b3b9f3d0 70/* Return true when DECL can be referenced from current unit.
c44c2088
JH
71 FROM_DECL (if non-null) specify constructor of variable DECL was taken from.
72 We can get declarations that are not possible to reference for various
73 reasons:
1389294c 74
1389294c
JH
75 1) When analyzing C++ virtual tables.
76 C++ virtual tables do have known constructors even
77 when they are keyed to other compilation unit.
78 Those tables can contain pointers to methods and vars
79 in other units. Those methods have both STATIC and EXTERNAL
80 set.
81 2) In WHOPR mode devirtualization might lead to reference
82 to method that was partitioned elsehwere.
83 In this case we have static VAR_DECL or FUNCTION_DECL
84 that has no corresponding callgraph/varpool node
b3b9f3d0
JH
85 declaring the body.
86 3) COMDAT functions referred by external vtables that
3e89949e 87 we devirtualize only during final compilation stage.
b3b9f3d0
JH
88 At this time we already decided that we will not output
89 the function body and thus we can't reference the symbol
90 directly. */
91
1389294c 92static bool
c44c2088 93can_refer_decl_in_current_unit_p (tree decl, tree from_decl)
1389294c 94{
2c8326a5 95 varpool_node *vnode;
1389294c 96 struct cgraph_node *node;
5e20cdc9 97 symtab_node *snode;
c44c2088 98
00de328a 99 if (DECL_ABSTRACT_P (decl))
1632a686
JH
100 return false;
101
102 /* We are concerned only about static/external vars and functions. */
103 if ((!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
8813a647 104 || !VAR_OR_FUNCTION_DECL_P (decl))
1632a686
JH
105 return true;
106
107 /* Static objects can be referred only if they was not optimized out yet. */
108 if (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
109 {
3aaf0529
JH
110 /* Before we start optimizing unreachable code we can be sure all
111 static objects are defined. */
3dafb85c 112 if (symtab->function_flags_ready)
3aaf0529 113 return true;
d52f5295 114 snode = symtab_node::get (decl);
3aaf0529 115 if (!snode || !snode->definition)
1632a686 116 return false;
7de90a6c 117 node = dyn_cast <cgraph_node *> (snode);
1632a686
JH
118 return !node || !node->global.inlined_to;
119 }
120
6da8be89 121 /* We will later output the initializer, so we can refer to it.
c44c2088 122 So we are concerned only when DECL comes from initializer of
3aaf0529 123 external var or var that has been optimized out. */
c44c2088 124 if (!from_decl
8813a647 125 || !VAR_P (from_decl)
3aaf0529 126 || (!DECL_EXTERNAL (from_decl)
9041d2e6 127 && (vnode = varpool_node::get (from_decl)) != NULL
3aaf0529 128 && vnode->definition)
6da8be89 129 || (flag_ltrans
9041d2e6 130 && (vnode = varpool_node::get (from_decl)) != NULL
6adda80b 131 && vnode->in_other_partition))
c44c2088 132 return true;
c44c2088
JH
133 /* We are folding reference from external vtable. The vtable may reffer
134 to a symbol keyed to other compilation unit. The other compilation
135 unit may be in separate DSO and the symbol may be hidden. */
136 if (DECL_VISIBILITY_SPECIFIED (decl)
137 && DECL_EXTERNAL (decl)
a33a931b 138 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT
d52f5295 139 && (!(snode = symtab_node::get (decl)) || !snode->in_other_partition))
c44c2088 140 return false;
b3b9f3d0
JH
141 /* When function is public, we always can introduce new reference.
142 Exception are the COMDAT functions where introducing a direct
143 reference imply need to include function body in the curren tunit. */
144 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
145 return true;
3aaf0529
JH
146 /* We have COMDAT. We are going to check if we still have definition
147 or if the definition is going to be output in other partition.
148 Bypass this when gimplifying; all needed functions will be produced.
c44c2088
JH
149
150 As observed in PR20991 for already optimized out comdat virtual functions
073a8998 151 it may be tempting to not necessarily give up because the copy will be
c44c2088
JH
152 output elsewhere when corresponding vtable is output.
153 This is however not possible - ABI specify that COMDATs are output in
154 units where they are used and when the other unit was compiled with LTO
155 it is possible that vtable was kept public while the function itself
156 was privatized. */
3dafb85c 157 if (!symtab->function_flags_ready)
b3b9f3d0 158 return true;
c44c2088 159
d52f5295 160 snode = symtab_node::get (decl);
3aaf0529
JH
161 if (!snode
162 || ((!snode->definition || DECL_EXTERNAL (decl))
163 && (!snode->in_other_partition
164 || (!snode->forced_by_abi && !snode->force_output))))
165 return false;
166 node = dyn_cast <cgraph_node *> (snode);
167 return !node || !node->global.inlined_to;
1389294c
JH
168}
169
a15ebbcd
ML
170/* Create a temporary for TYPE for a statement STMT. If the current function
171 is in SSA form, a SSA name is created. Otherwise a temporary register
172 is made. */
173
edc19e03
WS
174tree
175create_tmp_reg_or_ssa_name (tree type, gimple *stmt)
a15ebbcd
ML
176{
177 if (gimple_in_ssa_p (cfun))
178 return make_ssa_name (type, stmt);
179 else
180 return create_tmp_reg (type);
181}
182
0038d4e0 183/* CVAL is value taken from DECL_INITIAL of variable. Try to transform it into
c44c2088
JH
184 acceptable form for is_gimple_min_invariant.
185 FROM_DECL (if non-NULL) specify variable whose constructor contains CVAL. */
17f39a39
JH
186
187tree
c44c2088 188canonicalize_constructor_val (tree cval, tree from_decl)
17f39a39 189{
50619002
EB
190 tree orig_cval = cval;
191 STRIP_NOPS (cval);
315f5f1b
RG
192 if (TREE_CODE (cval) == POINTER_PLUS_EXPR
193 && TREE_CODE (TREE_OPERAND (cval, 1)) == INTEGER_CST)
17f39a39 194 {
315f5f1b
RG
195 tree ptr = TREE_OPERAND (cval, 0);
196 if (is_gimple_min_invariant (ptr))
197 cval = build1_loc (EXPR_LOCATION (cval),
198 ADDR_EXPR, TREE_TYPE (ptr),
199 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (ptr)),
200 ptr,
201 fold_convert (ptr_type_node,
202 TREE_OPERAND (cval, 1))));
17f39a39
JH
203 }
204 if (TREE_CODE (cval) == ADDR_EXPR)
205 {
5a27a197
RG
206 tree base = NULL_TREE;
207 if (TREE_CODE (TREE_OPERAND (cval, 0)) == COMPOUND_LITERAL_EXPR)
ca5f4331
MM
208 {
209 base = COMPOUND_LITERAL_EXPR_DECL (TREE_OPERAND (cval, 0));
210 if (base)
211 TREE_OPERAND (cval, 0) = base;
212 }
5a27a197
RG
213 else
214 base = get_base_address (TREE_OPERAND (cval, 0));
7501ca28
RG
215 if (!base)
216 return NULL_TREE;
b3b9f3d0 217
8813a647 218 if (VAR_OR_FUNCTION_DECL_P (base)
c44c2088 219 && !can_refer_decl_in_current_unit_p (base, from_decl))
1389294c 220 return NULL_TREE;
13f92e8d
JJ
221 if (TREE_TYPE (base) == error_mark_node)
222 return NULL_TREE;
8813a647 223 if (VAR_P (base))
46eb666a 224 TREE_ADDRESSABLE (base) = 1;
7501ca28
RG
225 else if (TREE_CODE (base) == FUNCTION_DECL)
226 {
227 /* Make sure we create a cgraph node for functions we'll reference.
228 They can be non-existent if the reference comes from an entry
229 of an external vtable for example. */
d52f5295 230 cgraph_node::get_create (base);
7501ca28 231 }
0038d4e0 232 /* Fixup types in global initializers. */
73aef89e
RG
233 if (TREE_TYPE (TREE_TYPE (cval)) != TREE_TYPE (TREE_OPERAND (cval, 0)))
234 cval = build_fold_addr_expr (TREE_OPERAND (cval, 0));
50619002
EB
235
236 if (!useless_type_conversion_p (TREE_TYPE (orig_cval), TREE_TYPE (cval)))
237 cval = fold_convert (TREE_TYPE (orig_cval), cval);
238 return cval;
17f39a39 239 }
846abd0d
RB
240 if (TREE_OVERFLOW_P (cval))
241 return drop_tree_overflow (cval);
50619002 242 return orig_cval;
17f39a39 243}
cbdd87d4
RG
244
245/* If SYM is a constant variable with known value, return the value.
246 NULL_TREE is returned otherwise. */
247
248tree
249get_symbol_constant_value (tree sym)
250{
6a6dac52
JH
251 tree val = ctor_for_folding (sym);
252 if (val != error_mark_node)
cbdd87d4 253 {
cbdd87d4
RG
254 if (val)
255 {
9d60be38 256 val = canonicalize_constructor_val (unshare_expr (val), sym);
1389294c 257 if (val && is_gimple_min_invariant (val))
17f39a39 258 return val;
1389294c
JH
259 else
260 return NULL_TREE;
cbdd87d4
RG
261 }
262 /* Variables declared 'const' without an initializer
263 have zero as the initializer if they may not be
264 overridden at link or run time. */
265 if (!val
b8a8c472 266 && is_gimple_reg_type (TREE_TYPE (sym)))
e8160c9a 267 return build_zero_cst (TREE_TYPE (sym));
cbdd87d4
RG
268 }
269
270 return NULL_TREE;
271}
272
273
cbdd87d4
RG
274
275/* Subroutine of fold_stmt. We perform several simplifications of the
276 memory reference tree EXPR and make sure to re-gimplify them properly
277 after propagation of constant addresses. IS_LHS is true if the
278 reference is supposed to be an lvalue. */
279
280static tree
281maybe_fold_reference (tree expr, bool is_lhs)
282{
17f39a39 283 tree result;
cbdd87d4 284
f0eddb90
RG
285 if ((TREE_CODE (expr) == VIEW_CONVERT_EXPR
286 || TREE_CODE (expr) == REALPART_EXPR
287 || TREE_CODE (expr) == IMAGPART_EXPR)
288 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
289 return fold_unary_loc (EXPR_LOCATION (expr),
290 TREE_CODE (expr),
291 TREE_TYPE (expr),
292 TREE_OPERAND (expr, 0));
293 else if (TREE_CODE (expr) == BIT_FIELD_REF
294 && CONSTANT_CLASS_P (TREE_OPERAND (expr, 0)))
295 return fold_ternary_loc (EXPR_LOCATION (expr),
296 TREE_CODE (expr),
297 TREE_TYPE (expr),
298 TREE_OPERAND (expr, 0),
299 TREE_OPERAND (expr, 1),
300 TREE_OPERAND (expr, 2));
301
f0eddb90
RG
302 if (!is_lhs
303 && (result = fold_const_aggregate_ref (expr))
304 && is_gimple_min_invariant (result))
305 return result;
cbdd87d4 306
cbdd87d4
RG
307 return NULL_TREE;
308}
309
310
311/* Attempt to fold an assignment statement pointed-to by SI. Returns a
312 replacement rhs for the statement or NULL_TREE if no simplification
313 could be made. It is assumed that the operands have been previously
314 folded. */
315
316static tree
317fold_gimple_assign (gimple_stmt_iterator *si)
318{
355fe088 319 gimple *stmt = gsi_stmt (*si);
cbdd87d4
RG
320 enum tree_code subcode = gimple_assign_rhs_code (stmt);
321 location_t loc = gimple_location (stmt);
322
323 tree result = NULL_TREE;
324
325 switch (get_gimple_rhs_class (subcode))
326 {
327 case GIMPLE_SINGLE_RHS:
328 {
329 tree rhs = gimple_assign_rhs1 (stmt);
330
8c00ba08
JW
331 if (TREE_CLOBBER_P (rhs))
332 return NULL_TREE;
333
4e71066d 334 if (REFERENCE_CLASS_P (rhs))
cbdd87d4
RG
335 return maybe_fold_reference (rhs, false);
336
bdf37f7a
JH
337 else if (TREE_CODE (rhs) == OBJ_TYPE_REF)
338 {
339 tree val = OBJ_TYPE_REF_EXPR (rhs);
340 if (is_gimple_min_invariant (val))
341 return val;
f8a39967 342 else if (flag_devirtualize && virtual_method_call_p (rhs))
bdf37f7a
JH
343 {
344 bool final;
345 vec <cgraph_node *>targets
f8a39967 346 = possible_polymorphic_call_targets (rhs, stmt, &final);
2b5f0895 347 if (final && targets.length () <= 1 && dbg_cnt (devirt))
bdf37f7a 348 {
2b5f0895
XDL
349 if (dump_enabled_p ())
350 {
807b7d62 351 location_t loc = gimple_location_safe (stmt);
2b5f0895
XDL
352 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
353 "resolving virtual function address "
354 "reference to function %s\n",
355 targets.length () == 1
356 ? targets[0]->name ()
3ef276e4 357 : "NULL");
2b5f0895 358 }
3ef276e4
RB
359 if (targets.length () == 1)
360 {
361 val = fold_convert (TREE_TYPE (val),
362 build_fold_addr_expr_loc
363 (loc, targets[0]->decl));
364 STRIP_USELESS_TYPE_CONVERSION (val);
365 }
366 else
367 /* We can not use __builtin_unreachable here because it
368 can not have address taken. */
369 val = build_int_cst (TREE_TYPE (val), 0);
bdf37f7a
JH
370 return val;
371 }
372 }
bdf37f7a 373 }
7524f419 374
cbdd87d4
RG
375 else if (TREE_CODE (rhs) == ADDR_EXPR)
376 {
70f34814
RG
377 tree ref = TREE_OPERAND (rhs, 0);
378 tree tem = maybe_fold_reference (ref, true);
379 if (tem
380 && TREE_CODE (tem) == MEM_REF
381 && integer_zerop (TREE_OPERAND (tem, 1)))
382 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (tem, 0));
383 else if (tem)
cbdd87d4
RG
384 result = fold_convert (TREE_TYPE (rhs),
385 build_fold_addr_expr_loc (loc, tem));
70f34814
RG
386 else if (TREE_CODE (ref) == MEM_REF
387 && integer_zerop (TREE_OPERAND (ref, 1)))
388 result = fold_convert (TREE_TYPE (rhs), TREE_OPERAND (ref, 0));
7524f419
RB
389
390 if (result)
391 {
392 /* Strip away useless type conversions. Both the
393 NON_LVALUE_EXPR that may have been added by fold, and
394 "useless" type conversions that might now be apparent
395 due to propagation. */
396 STRIP_USELESS_TYPE_CONVERSION (result);
397
398 if (result != rhs && valid_gimple_rhs_p (result))
399 return result;
400 }
cbdd87d4
RG
401 }
402
403 else if (TREE_CODE (rhs) == CONSTRUCTOR
7524f419 404 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
cbdd87d4
RG
405 {
406 /* Fold a constant vector CONSTRUCTOR to VECTOR_CST. */
407 unsigned i;
408 tree val;
409
410 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
7524f419 411 if (! CONSTANT_CLASS_P (val))
cbdd87d4
RG
412 return NULL_TREE;
413
414 return build_vector_from_ctor (TREE_TYPE (rhs),
415 CONSTRUCTOR_ELTS (rhs));
416 }
417
418 else if (DECL_P (rhs))
9d60be38 419 return get_symbol_constant_value (rhs);
cbdd87d4
RG
420 }
421 break;
422
423 case GIMPLE_UNARY_RHS:
cbdd87d4
RG
424 break;
425
426 case GIMPLE_BINARY_RHS:
cbdd87d4
RG
427 break;
428
0354c0c7 429 case GIMPLE_TERNARY_RHS:
5c099d40
RB
430 result = fold_ternary_loc (loc, subcode,
431 TREE_TYPE (gimple_assign_lhs (stmt)),
432 gimple_assign_rhs1 (stmt),
433 gimple_assign_rhs2 (stmt),
434 gimple_assign_rhs3 (stmt));
0354c0c7
BS
435
436 if (result)
437 {
438 STRIP_USELESS_TYPE_CONVERSION (result);
439 if (valid_gimple_rhs_p (result))
440 return result;
0354c0c7
BS
441 }
442 break;
443
cbdd87d4
RG
444 case GIMPLE_INVALID_RHS:
445 gcc_unreachable ();
446 }
447
448 return NULL_TREE;
449}
450
fef5a0d9
RB
451
452/* Replace a statement at *SI_P with a sequence of statements in STMTS,
453 adjusting the replacement stmts location and virtual operands.
454 If the statement has a lhs the last stmt in the sequence is expected
455 to assign to that lhs. */
456
457static void
458gsi_replace_with_seq_vops (gimple_stmt_iterator *si_p, gimple_seq stmts)
459{
355fe088 460 gimple *stmt = gsi_stmt (*si_p);
fef5a0d9
RB
461
462 if (gimple_has_location (stmt))
463 annotate_all_with_location (stmts, gimple_location (stmt));
464
465 /* First iterate over the replacement statements backward, assigning
466 virtual operands to their defining statements. */
355fe088 467 gimple *laststore = NULL;
fef5a0d9
RB
468 for (gimple_stmt_iterator i = gsi_last (stmts);
469 !gsi_end_p (i); gsi_prev (&i))
470 {
355fe088 471 gimple *new_stmt = gsi_stmt (i);
fef5a0d9
RB
472 if ((gimple_assign_single_p (new_stmt)
473 && !is_gimple_reg (gimple_assign_lhs (new_stmt)))
474 || (is_gimple_call (new_stmt)
475 && (gimple_call_flags (new_stmt)
476 & (ECF_NOVOPS | ECF_PURE | ECF_CONST | ECF_NORETURN)) == 0))
477 {
478 tree vdef;
479 if (!laststore)
480 vdef = gimple_vdef (stmt);
481 else
482 vdef = make_ssa_name (gimple_vop (cfun), new_stmt);
483 gimple_set_vdef (new_stmt, vdef);
484 if (vdef && TREE_CODE (vdef) == SSA_NAME)
485 SSA_NAME_DEF_STMT (vdef) = new_stmt;
486 laststore = new_stmt;
487 }
488 }
489
490 /* Second iterate over the statements forward, assigning virtual
491 operands to their uses. */
492 tree reaching_vuse = gimple_vuse (stmt);
493 for (gimple_stmt_iterator i = gsi_start (stmts);
494 !gsi_end_p (i); gsi_next (&i))
495 {
355fe088 496 gimple *new_stmt = gsi_stmt (i);
fef5a0d9
RB
497 /* If the new statement possibly has a VUSE, update it with exact SSA
498 name we know will reach this one. */
499 if (gimple_has_mem_ops (new_stmt))
500 gimple_set_vuse (new_stmt, reaching_vuse);
501 gimple_set_modified (new_stmt, true);
502 if (gimple_vdef (new_stmt))
503 reaching_vuse = gimple_vdef (new_stmt);
504 }
505
506 /* If the new sequence does not do a store release the virtual
507 definition of the original statement. */
508 if (reaching_vuse
509 && reaching_vuse == gimple_vuse (stmt))
510 {
511 tree vdef = gimple_vdef (stmt);
512 if (vdef
513 && TREE_CODE (vdef) == SSA_NAME)
514 {
515 unlink_stmt_vdef (stmt);
516 release_ssa_name (vdef);
517 }
518 }
519
520 /* Finally replace the original statement with the sequence. */
521 gsi_replace_with_seq (si_p, stmts, false);
522}
523
cbdd87d4
RG
524/* Convert EXPR into a GIMPLE value suitable for substitution on the
525 RHS of an assignment. Insert the necessary statements before
526 iterator *SI_P. The statement at *SI_P, which must be a GIMPLE_CALL
527 is replaced. If the call is expected to produces a result, then it
528 is replaced by an assignment of the new RHS to the result variable.
529 If the result is to be ignored, then the call is replaced by a
fe2ef088
MM
530 GIMPLE_NOP. A proper VDEF chain is retained by making the first
531 VUSE and the last VDEF of the whole sequence be the same as the replaced
532 statement and using new SSA names for stores in between. */
cbdd87d4
RG
533
534void
535gimplify_and_update_call_from_tree (gimple_stmt_iterator *si_p, tree expr)
536{
537 tree lhs;
355fe088 538 gimple *stmt, *new_stmt;
cbdd87d4 539 gimple_stmt_iterator i;
355a7673 540 gimple_seq stmts = NULL;
cbdd87d4
RG
541
542 stmt = gsi_stmt (*si_p);
543
544 gcc_assert (is_gimple_call (stmt));
545
45852dcc 546 push_gimplify_context (gimple_in_ssa_p (cfun));
cbdd87d4 547
e256dfce 548 lhs = gimple_call_lhs (stmt);
cbdd87d4 549 if (lhs == NULL_TREE)
6e572326
RG
550 {
551 gimplify_and_add (expr, &stmts);
552 /* We can end up with folding a memcpy of an empty class assignment
553 which gets optimized away by C++ gimplification. */
554 if (gimple_seq_empty_p (stmts))
555 {
9fdc58de 556 pop_gimplify_context (NULL);
6e572326
RG
557 if (gimple_in_ssa_p (cfun))
558 {
559 unlink_stmt_vdef (stmt);
560 release_defs (stmt);
561 }
f6b4dc28 562 gsi_replace (si_p, gimple_build_nop (), false);
6e572326
RG
563 return;
564 }
565 }
cbdd87d4 566 else
e256dfce 567 {
381cdae4 568 tree tmp = force_gimple_operand (expr, &stmts, false, NULL_TREE);
e256dfce
RG
569 new_stmt = gimple_build_assign (lhs, tmp);
570 i = gsi_last (stmts);
571 gsi_insert_after_without_update (&i, new_stmt,
572 GSI_CONTINUE_LINKING);
573 }
cbdd87d4
RG
574
575 pop_gimplify_context (NULL);
576
fef5a0d9
RB
577 gsi_replace_with_seq_vops (si_p, stmts);
578}
cbdd87d4 579
fef5a0d9
RB
580
581/* Replace the call at *GSI with the gimple value VAL. */
582
e3174bdf 583void
fef5a0d9
RB
584replace_call_with_value (gimple_stmt_iterator *gsi, tree val)
585{
355fe088 586 gimple *stmt = gsi_stmt (*gsi);
fef5a0d9 587 tree lhs = gimple_call_lhs (stmt);
355fe088 588 gimple *repl;
fef5a0d9 589 if (lhs)
e256dfce 590 {
fef5a0d9
RB
591 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (val)))
592 val = fold_convert (TREE_TYPE (lhs), val);
593 repl = gimple_build_assign (lhs, val);
594 }
595 else
596 repl = gimple_build_nop ();
597 tree vdef = gimple_vdef (stmt);
598 if (vdef && TREE_CODE (vdef) == SSA_NAME)
599 {
600 unlink_stmt_vdef (stmt);
601 release_ssa_name (vdef);
602 }
f6b4dc28 603 gsi_replace (gsi, repl, false);
fef5a0d9
RB
604}
605
606/* Replace the call at *GSI with the new call REPL and fold that
607 again. */
608
609static void
355fe088 610replace_call_with_call_and_fold (gimple_stmt_iterator *gsi, gimple *repl)
fef5a0d9 611{
355fe088 612 gimple *stmt = gsi_stmt (*gsi);
fef5a0d9
RB
613 gimple_call_set_lhs (repl, gimple_call_lhs (stmt));
614 gimple_set_location (repl, gimple_location (stmt));
615 if (gimple_vdef (stmt)
616 && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
617 {
618 gimple_set_vdef (repl, gimple_vdef (stmt));
fef5a0d9
RB
619 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
620 }
00296d7f
JJ
621 if (gimple_vuse (stmt))
622 gimple_set_vuse (repl, gimple_vuse (stmt));
f6b4dc28 623 gsi_replace (gsi, repl, false);
fef5a0d9
RB
624 fold_stmt (gsi);
625}
626
627/* Return true if VAR is a VAR_DECL or a component thereof. */
628
629static bool
630var_decl_component_p (tree var)
631{
632 tree inner = var;
633 while (handled_component_p (inner))
634 inner = TREE_OPERAND (inner, 0);
635 return SSA_VAR_P (inner);
636}
637
6512c0f1
MS
638/* If the SIZE argument representing the size of an object is in a range
639 of values of which exactly one is valid (and that is zero), return
640 true, otherwise false. */
641
642static bool
643size_must_be_zero_p (tree size)
644{
645 if (integer_zerop (size))
646 return true;
647
648 if (TREE_CODE (size) != SSA_NAME)
649 return false;
650
651 wide_int min, max;
652 enum value_range_type rtype = get_range_info (size, &min, &max);
653 if (rtype != VR_ANTI_RANGE)
654 return false;
655
656 tree type = TREE_TYPE (size);
657 int prec = TYPE_PRECISION (type);
658
659 wide_int wone = wi::one (prec);
660
661 /* Compute the value of SSIZE_MAX, the largest positive value that
662 can be stored in ssize_t, the signed counterpart of size_t. */
663 wide_int ssize_max = wi::lshift (wi::one (prec), prec - 1) - 1;
664
665 return wi::eq_p (min, wone) && wi::geu_p (max, ssize_max);
666}
667
cc8bea0a
MS
668/* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and
669 diagnose (otherwise undefined) overlapping copies without preventing
670 folding. When folded, GCC guarantees that overlapping memcpy has
671 the same semantics as memmove. Call to the library memcpy need not
672 provide the same guarantee. Return false if no simplification can
673 be made. */
fef5a0d9
RB
674
675static bool
676gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi,
677 tree dest, tree src, int endp)
678{
355fe088 679 gimple *stmt = gsi_stmt (*gsi);
fef5a0d9
RB
680 tree lhs = gimple_call_lhs (stmt);
681 tree len = gimple_call_arg (stmt, 2);
682 tree destvar, srcvar;
683 location_t loc = gimple_location (stmt);
684
cc8bea0a 685 bool nowarn = gimple_no_warning_p (stmt);
cc8bea0a 686
6512c0f1
MS
687 /* If the LEN parameter is a constant zero or in range where
688 the only valid value is zero, return DEST. */
689 if (size_must_be_zero_p (len))
fef5a0d9 690 {
355fe088 691 gimple *repl;
fef5a0d9
RB
692 if (gimple_call_lhs (stmt))
693 repl = gimple_build_assign (gimple_call_lhs (stmt), dest);
694 else
695 repl = gimple_build_nop ();
696 tree vdef = gimple_vdef (stmt);
697 if (vdef && TREE_CODE (vdef) == SSA_NAME)
e256dfce 698 {
fef5a0d9
RB
699 unlink_stmt_vdef (stmt);
700 release_ssa_name (vdef);
701 }
f6b4dc28 702 gsi_replace (gsi, repl, false);
fef5a0d9
RB
703 return true;
704 }
705
706 /* If SRC and DEST are the same (and not volatile), return
707 DEST{,+LEN,+LEN-1}. */
708 if (operand_equal_p (src, dest, 0))
709 {
cc8bea0a
MS
710 /* Avoid diagnosing exact overlap in calls to __builtin_memcpy.
711 It's safe and may even be emitted by GCC itself (see bug
e9b9fa4c 712 32667). */
fef5a0d9
RB
713 unlink_stmt_vdef (stmt);
714 if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
715 release_ssa_name (gimple_vdef (stmt));
716 if (!lhs)
717 {
f6b4dc28 718 gsi_replace (gsi, gimple_build_nop (), false);
fef5a0d9
RB
719 return true;
720 }
721 goto done;
722 }
723 else
724 {
725 tree srctype, desttype;
726 unsigned int src_align, dest_align;
727 tree off0;
728
3de2a40e
IE
729 /* Inlining of memcpy/memmove may cause bounds lost (if we copy
730 pointers as wide integer) and also may result in huge function
731 size because of inlined bounds copy. Thus don't inline for
732 functions we want to instrument. */
733 if (flag_check_pointer_bounds
734 && chkp_instrumentable_p (cfun->decl)
735 /* Even if data may contain pointers we can inline if copy
736 less than a pointer size. */
737 && (!tree_fits_uhwi_p (len)
738 || compare_tree_int (len, POINTER_SIZE_UNITS) >= 0))
739 return false;
740
fef5a0d9
RB
741 /* Build accesses at offset zero with a ref-all character type. */
742 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
743 ptr_mode, true), 0);
744
745 /* If we can perform the copy efficiently with first doing all loads
746 and then all stores inline it that way. Currently efficiently
747 means that we can load all the memory into a single integer
748 register which is what MOVE_MAX gives us. */
749 src_align = get_pointer_alignment (src);
750 dest_align = get_pointer_alignment (dest);
751 if (tree_fits_uhwi_p (len)
752 && compare_tree_int (len, MOVE_MAX) <= 0
753 /* ??? Don't transform copies from strings with known length this
754 confuses the tree-ssa-strlen.c. This doesn't handle
755 the case in gcc.dg/strlenopt-8.c which is XFAILed for that
756 reason. */
757 && !c_strlen (src, 2))
758 {
759 unsigned ilen = tree_to_uhwi (len);
146ec50f 760 if (pow2p_hwi (ilen))
fef5a0d9 761 {
cc8bea0a
MS
762 /* Detect invalid bounds and overlapping copies and issue
763 either -Warray-bounds or -Wrestrict. */
764 if (!nowarn
765 && check_bounds_or_overlap (as_a <gcall *>(stmt),
766 dest, src, len, len))
767 gimple_set_no_warning (stmt, true);
768
64ab8765 769 scalar_int_mode mode;
fef5a0d9
RB
770 tree type = lang_hooks.types.type_for_size (ilen * 8, 1);
771 if (type
64ab8765
RS
772 && is_a <scalar_int_mode> (TYPE_MODE (type), &mode)
773 && GET_MODE_SIZE (mode) * BITS_PER_UNIT == ilen * 8
fef5a0d9
RB
774 /* If the destination pointer is not aligned we must be able
775 to emit an unaligned store. */
64ab8765 776 && (dest_align >= GET_MODE_ALIGNMENT (mode)
e0bd6c9f 777 || !targetm.slow_unaligned_access (mode, dest_align)
64ab8765 778 || (optab_handler (movmisalign_optab, mode)
f869c12f 779 != CODE_FOR_nothing)))
fef5a0d9
RB
780 {
781 tree srctype = type;
782 tree desttype = type;
64ab8765 783 if (src_align < GET_MODE_ALIGNMENT (mode))
fef5a0d9
RB
784 srctype = build_aligned_type (type, src_align);
785 tree srcmem = fold_build2 (MEM_REF, srctype, src, off0);
786 tree tem = fold_const_aggregate_ref (srcmem);
787 if (tem)
788 srcmem = tem;
64ab8765 789 else if (src_align < GET_MODE_ALIGNMENT (mode)
e0bd6c9f 790 && targetm.slow_unaligned_access (mode, src_align)
64ab8765 791 && (optab_handler (movmisalign_optab, mode)
f869c12f 792 == CODE_FOR_nothing))
fef5a0d9
RB
793 srcmem = NULL_TREE;
794 if (srcmem)
795 {
355fe088 796 gimple *new_stmt;
fef5a0d9
RB
797 if (is_gimple_reg_type (TREE_TYPE (srcmem)))
798 {
799 new_stmt = gimple_build_assign (NULL_TREE, srcmem);
a15ebbcd
ML
800 srcmem
801 = create_tmp_reg_or_ssa_name (TREE_TYPE (srcmem),
802 new_stmt);
fef5a0d9
RB
803 gimple_assign_set_lhs (new_stmt, srcmem);
804 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
805 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
806 }
64ab8765 807 if (dest_align < GET_MODE_ALIGNMENT (mode))
fef5a0d9
RB
808 desttype = build_aligned_type (type, dest_align);
809 new_stmt
810 = gimple_build_assign (fold_build2 (MEM_REF, desttype,
811 dest, off0),
812 srcmem);
813 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
814 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
815 if (gimple_vdef (new_stmt)
816 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
817 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
818 if (!lhs)
819 {
f6b4dc28 820 gsi_replace (gsi, new_stmt, false);
fef5a0d9
RB
821 return true;
822 }
823 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
824 goto done;
825 }
826 }
827 }
828 }
829
830 if (endp == 3)
831 {
832 /* Both DEST and SRC must be pointer types.
833 ??? This is what old code did. Is the testing for pointer types
834 really mandatory?
835
836 If either SRC is readonly or length is 1, we can use memcpy. */
837 if (!dest_align || !src_align)
838 return false;
839 if (readonly_data_expr (src)
840 || (tree_fits_uhwi_p (len)
841 && (MIN (src_align, dest_align) / BITS_PER_UNIT
842 >= tree_to_uhwi (len))))
843 {
844 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
845 if (!fn)
846 return false;
847 gimple_call_set_fndecl (stmt, fn);
848 gimple_call_set_arg (stmt, 0, dest);
849 gimple_call_set_arg (stmt, 1, src);
850 fold_stmt (gsi);
851 return true;
852 }
853
854 /* If *src and *dest can't overlap, optimize into memcpy as well. */
855 if (TREE_CODE (src) == ADDR_EXPR
856 && TREE_CODE (dest) == ADDR_EXPR)
857 {
858 tree src_base, dest_base, fn;
a90c8804
RS
859 poly_int64 src_offset = 0, dest_offset = 0;
860 poly_uint64 maxsize;
fef5a0d9
RB
861
862 srcvar = TREE_OPERAND (src, 0);
4fda19ef
JJ
863 src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
864 if (src_base == NULL)
865 src_base = srcvar;
fef5a0d9 866 destvar = TREE_OPERAND (dest, 0);
4fda19ef
JJ
867 dest_base = get_addr_base_and_unit_offset (destvar,
868 &dest_offset);
869 if (dest_base == NULL)
870 dest_base = destvar;
a90c8804 871 if (!poly_int_tree_p (len, &maxsize))
fef5a0d9 872 maxsize = -1;
fef5a0d9
RB
873 if (SSA_VAR_P (src_base)
874 && SSA_VAR_P (dest_base))
875 {
876 if (operand_equal_p (src_base, dest_base, 0)
a90c8804
RS
877 && ranges_maybe_overlap_p (src_offset, maxsize,
878 dest_offset, maxsize))
fef5a0d9
RB
879 return false;
880 }
881 else if (TREE_CODE (src_base) == MEM_REF
882 && TREE_CODE (dest_base) == MEM_REF)
883 {
884 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
885 TREE_OPERAND (dest_base, 0), 0))
886 return false;
a90c8804
RS
887 poly_offset_int full_src_offset
888 = mem_ref_offset (src_base) + src_offset;
889 poly_offset_int full_dest_offset
890 = mem_ref_offset (dest_base) + dest_offset;
891 if (ranges_maybe_overlap_p (full_src_offset, maxsize,
892 full_dest_offset, maxsize))
fef5a0d9
RB
893 return false;
894 }
895 else
896 return false;
897
898 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
899 if (!fn)
900 return false;
901 gimple_call_set_fndecl (stmt, fn);
902 gimple_call_set_arg (stmt, 0, dest);
903 gimple_call_set_arg (stmt, 1, src);
904 fold_stmt (gsi);
905 return true;
906 }
907
908 /* If the destination and source do not alias optimize into
909 memcpy as well. */
910 if ((is_gimple_min_invariant (dest)
911 || TREE_CODE (dest) == SSA_NAME)
912 && (is_gimple_min_invariant (src)
913 || TREE_CODE (src) == SSA_NAME))
914 {
915 ao_ref destr, srcr;
916 ao_ref_init_from_ptr_and_size (&destr, dest, len);
917 ao_ref_init_from_ptr_and_size (&srcr, src, len);
918 if (!refs_may_alias_p_1 (&destr, &srcr, false))
919 {
920 tree fn;
921 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
922 if (!fn)
923 return false;
924 gimple_call_set_fndecl (stmt, fn);
925 gimple_call_set_arg (stmt, 0, dest);
926 gimple_call_set_arg (stmt, 1, src);
927 fold_stmt (gsi);
928 return true;
929 }
930 }
931
932 return false;
933 }
934
935 if (!tree_fits_shwi_p (len))
936 return false;
fef5a0d9
RB
937 if (!POINTER_TYPE_P (TREE_TYPE (src))
938 || !POINTER_TYPE_P (TREE_TYPE (dest)))
939 return false;
940 /* In the following try to find a type that is most natural to be
941 used for the memcpy source and destination and that allows
942 the most optimization when memcpy is turned into a plain assignment
943 using that type. In theory we could always use a char[len] type
944 but that only gains us that the destination and source possibly
945 no longer will have their address taken. */
fef5a0d9
RB
946 srctype = TREE_TYPE (TREE_TYPE (src));
947 if (TREE_CODE (srctype) == ARRAY_TYPE
948 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
42f74245 949 srctype = TREE_TYPE (srctype);
fef5a0d9
RB
950 desttype = TREE_TYPE (TREE_TYPE (dest));
951 if (TREE_CODE (desttype) == ARRAY_TYPE
952 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
42f74245 953 desttype = TREE_TYPE (desttype);
fef5a0d9
RB
954 if (TREE_ADDRESSABLE (srctype)
955 || TREE_ADDRESSABLE (desttype))
956 return false;
957
958 /* Make sure we are not copying using a floating-point mode or
959 a type whose size possibly does not match its precision. */
960 if (FLOAT_MODE_P (TYPE_MODE (desttype))
961 || TREE_CODE (desttype) == BOOLEAN_TYPE
962 || TREE_CODE (desttype) == ENUMERAL_TYPE)
963 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
964 if (FLOAT_MODE_P (TYPE_MODE (srctype))
965 || TREE_CODE (srctype) == BOOLEAN_TYPE
966 || TREE_CODE (srctype) == ENUMERAL_TYPE)
967 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
968 if (!srctype)
969 srctype = desttype;
970 if (!desttype)
971 desttype = srctype;
972 if (!srctype)
973 return false;
974
975 src_align = get_pointer_alignment (src);
976 dest_align = get_pointer_alignment (dest);
977 if (dest_align < TYPE_ALIGN (desttype)
978 || src_align < TYPE_ALIGN (srctype))
979 return false;
980
42f74245
RB
981 destvar = NULL_TREE;
982 if (TREE_CODE (dest) == ADDR_EXPR
983 && var_decl_component_p (TREE_OPERAND (dest, 0))
fef5a0d9 984 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
42f74245 985 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
fef5a0d9 986
42f74245
RB
987 srcvar = NULL_TREE;
988 if (TREE_CODE (src) == ADDR_EXPR
989 && var_decl_component_p (TREE_OPERAND (src, 0))
fef5a0d9
RB
990 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
991 {
992 if (!destvar
993 || src_align >= TYPE_ALIGN (desttype))
994 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
42f74245 995 src, off0);
fef5a0d9
RB
996 else if (!STRICT_ALIGNMENT)
997 {
998 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
999 src_align);
42f74245 1000 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
fef5a0d9 1001 }
fef5a0d9 1002 }
fef5a0d9
RB
1003
1004 if (srcvar == NULL_TREE && destvar == NULL_TREE)
1005 return false;
1006
1007 if (srcvar == NULL_TREE)
1008 {
fef5a0d9
RB
1009 if (src_align >= TYPE_ALIGN (desttype))
1010 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
1011 else
1012 {
1013 if (STRICT_ALIGNMENT)
1014 return false;
1015 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
1016 src_align);
1017 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
1018 }
1019 }
1020 else if (destvar == NULL_TREE)
1021 {
fef5a0d9
RB
1022 if (dest_align >= TYPE_ALIGN (srctype))
1023 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
1024 else
1025 {
1026 if (STRICT_ALIGNMENT)
1027 return false;
1028 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
1029 dest_align);
1030 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
1031 }
1032 }
1033
cc8bea0a
MS
1034 /* Detect invalid bounds and overlapping copies and issue either
1035 -Warray-bounds or -Wrestrict. */
1036 if (!nowarn)
1037 check_bounds_or_overlap (as_a <gcall *>(stmt), dest, src, len, len);
1038
355fe088 1039 gimple *new_stmt;
fef5a0d9
RB
1040 if (is_gimple_reg_type (TREE_TYPE (srcvar)))
1041 {
921b13d0
RB
1042 tree tem = fold_const_aggregate_ref (srcvar);
1043 if (tem)
1044 srcvar = tem;
1045 if (! is_gimple_min_invariant (srcvar))
1046 {
1047 new_stmt = gimple_build_assign (NULL_TREE, srcvar);
a15ebbcd
ML
1048 srcvar = create_tmp_reg_or_ssa_name (TREE_TYPE (srcvar),
1049 new_stmt);
921b13d0
RB
1050 gimple_assign_set_lhs (new_stmt, srcvar);
1051 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1052 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1053 }
d7257171
RB
1054 new_stmt = gimple_build_assign (destvar, srcvar);
1055 goto set_vop_and_replace;
fef5a0d9 1056 }
d7257171
RB
1057
1058 /* We get an aggregate copy. Use an unsigned char[] type to
1059 perform the copying to preserve padding and to avoid any issues
1060 with TREE_ADDRESSABLE types or float modes behavior on copying. */
1061 desttype = build_array_type_nelts (unsigned_char_type_node,
1062 tree_to_uhwi (len));
1063 srctype = desttype;
1064 if (src_align > TYPE_ALIGN (srctype))
1065 srctype = build_aligned_type (srctype, src_align);
1066 if (dest_align > TYPE_ALIGN (desttype))
1067 desttype = build_aligned_type (desttype, dest_align);
1068 new_stmt
1069 = gimple_build_assign (fold_build2 (MEM_REF, desttype, dest, off0),
1070 fold_build2 (MEM_REF, srctype, src, off0));
1071set_vop_and_replace:
fef5a0d9
RB
1072 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
1073 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
1074 if (gimple_vdef (new_stmt)
1075 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1076 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1077 if (!lhs)
1078 {
f6b4dc28 1079 gsi_replace (gsi, new_stmt, false);
fef5a0d9
RB
1080 return true;
1081 }
1082 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1083 }
1084
1085done:
74e3c262 1086 gimple_seq stmts = NULL;
fef5a0d9
RB
1087 if (endp == 0 || endp == 3)
1088 len = NULL_TREE;
1089 else if (endp == 2)
74e3c262
RB
1090 len = gimple_build (&stmts, loc, MINUS_EXPR, TREE_TYPE (len), len,
1091 ssize_int (1));
fef5a0d9 1092 if (endp == 2 || endp == 1)
74e3c262
RB
1093 {
1094 len = gimple_convert_to_ptrofftype (&stmts, loc, len);
1095 dest = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
1096 TREE_TYPE (dest), dest, len);
1097 }
fef5a0d9 1098
74e3c262 1099 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
355fe088 1100 gimple *repl = gimple_build_assign (lhs, dest);
f6b4dc28 1101 gsi_replace (gsi, repl, false);
fef5a0d9
RB
1102 return true;
1103}
1104
b3d8d88e
MS
1105/* Transform a call to built-in bcmp(a, b, len) at *GSI into one
1106 to built-in memcmp (a, b, len). */
1107
1108static bool
1109gimple_fold_builtin_bcmp (gimple_stmt_iterator *gsi)
1110{
1111 tree fn = builtin_decl_implicit (BUILT_IN_MEMCMP);
1112
1113 if (!fn)
1114 return false;
1115
1116 /* Transform bcmp (a, b, len) into memcmp (a, b, len). */
1117
1118 gimple *stmt = gsi_stmt (*gsi);
1119 tree a = gimple_call_arg (stmt, 0);
1120 tree b = gimple_call_arg (stmt, 1);
1121 tree len = gimple_call_arg (stmt, 2);
1122
1123 gimple *repl = gimple_build_call (fn, 3, a, b, len);
1124 replace_call_with_call_and_fold (gsi, repl);
1125
1126 return true;
1127}
1128
1129/* Transform a call to built-in bcopy (src, dest, len) at *GSI into one
1130 to built-in memmove (dest, src, len). */
1131
1132static bool
1133gimple_fold_builtin_bcopy (gimple_stmt_iterator *gsi)
1134{
1135 tree fn = builtin_decl_implicit (BUILT_IN_MEMMOVE);
1136
1137 if (!fn)
1138 return false;
1139
1140 /* bcopy has been removed from POSIX in Issue 7 but Issue 6 specifies
1141 it's quivalent to memmove (not memcpy). Transform bcopy (src, dest,
1142 len) into memmove (dest, src, len). */
1143
1144 gimple *stmt = gsi_stmt (*gsi);
1145 tree src = gimple_call_arg (stmt, 0);
1146 tree dest = gimple_call_arg (stmt, 1);
1147 tree len = gimple_call_arg (stmt, 2);
1148
1149 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
1150 gimple_call_set_fntype (as_a <gcall *> (stmt), TREE_TYPE (fn));
1151 replace_call_with_call_and_fold (gsi, repl);
1152
1153 return true;
1154}
1155
1156/* Transform a call to built-in bzero (dest, len) at *GSI into one
1157 to built-in memset (dest, 0, len). */
1158
1159static bool
1160gimple_fold_builtin_bzero (gimple_stmt_iterator *gsi)
1161{
1162 tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
1163
1164 if (!fn)
1165 return false;
1166
1167 /* Transform bzero (dest, len) into memset (dest, 0, len). */
1168
1169 gimple *stmt = gsi_stmt (*gsi);
1170 tree dest = gimple_call_arg (stmt, 0);
1171 tree len = gimple_call_arg (stmt, 1);
1172
1173 gimple_seq seq = NULL;
1174 gimple *repl = gimple_build_call (fn, 3, dest, integer_zero_node, len);
1175 gimple_seq_add_stmt_without_update (&seq, repl);
1176 gsi_replace_with_seq_vops (gsi, seq);
1177 fold_stmt (gsi);
1178
1179 return true;
1180}
1181
fef5a0d9
RB
1182/* Fold function call to builtin memset or bzero at *GSI setting the
1183 memory of size LEN to VAL. Return whether a simplification was made. */
1184
1185static bool
1186gimple_fold_builtin_memset (gimple_stmt_iterator *gsi, tree c, tree len)
1187{
355fe088 1188 gimple *stmt = gsi_stmt (*gsi);
fef5a0d9
RB
1189 tree etype;
1190 unsigned HOST_WIDE_INT length, cval;
1191
1192 /* If the LEN parameter is zero, return DEST. */
1193 if (integer_zerop (len))
1194 {
1195 replace_call_with_value (gsi, gimple_call_arg (stmt, 0));
1196 return true;
1197 }
1198
1199 if (! tree_fits_uhwi_p (len))
1200 return false;
1201
1202 if (TREE_CODE (c) != INTEGER_CST)
1203 return false;
1204
1205 tree dest = gimple_call_arg (stmt, 0);
1206 tree var = dest;
1207 if (TREE_CODE (var) != ADDR_EXPR)
1208 return false;
1209
1210 var = TREE_OPERAND (var, 0);
1211 if (TREE_THIS_VOLATILE (var))
1212 return false;
1213
1214 etype = TREE_TYPE (var);
1215 if (TREE_CODE (etype) == ARRAY_TYPE)
1216 etype = TREE_TYPE (etype);
1217
1218 if (!INTEGRAL_TYPE_P (etype)
1219 && !POINTER_TYPE_P (etype))
1220 return NULL_TREE;
1221
1222 if (! var_decl_component_p (var))
1223 return NULL_TREE;
1224
1225 length = tree_to_uhwi (len);
7a504f33 1226 if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (etype)) != length
fef5a0d9
RB
1227 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
1228 return NULL_TREE;
1229
1230 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
1231 return NULL_TREE;
1232
1233 if (integer_zerop (c))
1234 cval = 0;
1235 else
1236 {
1237 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
1238 return NULL_TREE;
1239
1240 cval = TREE_INT_CST_LOW (c);
1241 cval &= 0xff;
1242 cval |= cval << 8;
1243 cval |= cval << 16;
1244 cval |= (cval << 31) << 1;
1245 }
1246
1247 var = fold_build2 (MEM_REF, etype, dest, build_int_cst (ptr_type_node, 0));
355fe088 1248 gimple *store = gimple_build_assign (var, build_int_cst_type (etype, cval));
fef5a0d9
RB
1249 gimple_set_vuse (store, gimple_vuse (stmt));
1250 tree vdef = gimple_vdef (stmt);
1251 if (vdef && TREE_CODE (vdef) == SSA_NAME)
1252 {
1253 gimple_set_vdef (store, gimple_vdef (stmt));
1254 SSA_NAME_DEF_STMT (gimple_vdef (stmt)) = store;
1255 }
1256 gsi_insert_before (gsi, store, GSI_SAME_STMT);
1257 if (gimple_call_lhs (stmt))
1258 {
355fe088 1259 gimple *asgn = gimple_build_assign (gimple_call_lhs (stmt), dest);
f6b4dc28 1260 gsi_replace (gsi, asgn, false);
fef5a0d9
RB
1261 }
1262 else
1263 {
1264 gimple_stmt_iterator gsi2 = *gsi;
1265 gsi_prev (gsi);
1266 gsi_remove (&gsi2, true);
1267 }
1268
1269 return true;
1270}
1271
1272
88d0c3f0
MS
1273/* Obtain the minimum and maximum string length or minimum and maximum
1274 value of ARG in LENGTH[0] and LENGTH[1], respectively.
1275 If ARG is an SSA name variable, follow its use-def chains. When
1276 TYPE == 0, if LENGTH[1] is not equal to the length we determine or
c8602fe6 1277 if we are unable to determine the length or value, return false.
88d0c3f0
MS
1278 VISITED is a bitmap of visited variables.
1279 TYPE is 0 if string length should be obtained, 1 for maximum string
1280 length and 2 for maximum value ARG can have.
c8602fe6 1281 When FUZZY is non-zero and the length of a string cannot be determined,
88d0c3f0 1282 the function instead considers as the maximum possible length the
c8602fe6
JJ
1283 size of a character array it may refer to. If FUZZY is 2, it will handle
1284 PHIs and COND_EXPRs optimistically, if we can determine string length
1285 minimum and maximum, it will use the minimum from the ones where it
1286 can be determined.
3f343040
MS
1287 Set *FLEXP to true if the range of the string lengths has been
1288 obtained from the upper bound of an array at the end of a struct.
1289 Such an array may hold a string that's longer than its upper bound
1290 due to it being used as a poor-man's flexible array member. */
fef5a0d9
RB
1291
1292static bool
88d0c3f0 1293get_range_strlen (tree arg, tree length[2], bitmap *visited, int type,
c8602fe6 1294 int fuzzy, bool *flexp)
fef5a0d9 1295{
c42d0aa0 1296 tree var, val = NULL_TREE;
355fe088 1297 gimple *def_stmt;
fef5a0d9 1298
c8602fe6
JJ
1299 /* The minimum and maximum length. */
1300 tree *const minlen = length;
88d0c3f0
MS
1301 tree *const maxlen = length + 1;
1302
fef5a0d9
RB
1303 if (TREE_CODE (arg) != SSA_NAME)
1304 {
1305 /* We can end up with &(*iftmp_1)[0] here as well, so handle it. */
1306 if (TREE_CODE (arg) == ADDR_EXPR
c42d0aa0 1307 && TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
fef5a0d9 1308 {
c42d0aa0
MS
1309 tree op = TREE_OPERAND (arg, 0);
1310 if (integer_zerop (TREE_OPERAND (op, 1)))
1311 {
1312 tree aop0 = TREE_OPERAND (op, 0);
1313 if (TREE_CODE (aop0) == INDIRECT_REF
1314 && TREE_CODE (TREE_OPERAND (aop0, 0)) == SSA_NAME)
1315 return get_range_strlen (TREE_OPERAND (aop0, 0),
1316 length, visited, type, fuzzy, flexp);
1317 }
1318 else if (TREE_CODE (TREE_OPERAND (op, 0)) == COMPONENT_REF && fuzzy)
1319 {
1320 /* Fail if an array is the last member of a struct object
1321 since it could be treated as a (fake) flexible array
1322 member. */
1323 tree idx = TREE_OPERAND (op, 1);
1324
1325 arg = TREE_OPERAND (op, 0);
1326 tree optype = TREE_TYPE (arg);
1327 if (tree dom = TYPE_DOMAIN (optype))
1328 if (tree bound = TYPE_MAX_VALUE (dom))
1329 if (TREE_CODE (bound) == INTEGER_CST
1330 && TREE_CODE (idx) == INTEGER_CST
1331 && tree_int_cst_lt (bound, idx))
1332 return false;
1333 }
fef5a0d9
RB
1334 }
1335
1336 if (type == 2)
1337 {
1338 val = arg;
1339 if (TREE_CODE (val) != INTEGER_CST
1340 || tree_int_cst_sgn (val) < 0)
1341 return false;
1342 }
1343 else
1344 val = c_strlen (arg, 1);
88d0c3f0
MS
1345
1346 if (!val && fuzzy)
1347 {
1348 if (TREE_CODE (arg) == ADDR_EXPR)
1349 return get_range_strlen (TREE_OPERAND (arg, 0), length,
3f343040 1350 visited, type, fuzzy, flexp);
88d0c3f0 1351
c42d0aa0
MS
1352 if (TREE_CODE (arg) == ARRAY_REF)
1353 {
1354 tree type = TREE_TYPE (TREE_OPERAND (arg, 0));
1355
1bfd6a00 1356 /* Determine the "innermost" array type. */
c42d0aa0
MS
1357 while (TREE_CODE (type) == ARRAY_TYPE
1358 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1359 type = TREE_TYPE (type);
1360
1bfd6a00
MS
1361 /* Avoid arrays of pointers. */
1362 tree eltype = TREE_TYPE (type);
1363 if (TREE_CODE (type) != ARRAY_TYPE
1364 || !INTEGRAL_TYPE_P (eltype))
1365 return false;
1366
c42d0aa0
MS
1367 val = TYPE_SIZE_UNIT (type);
1368 if (!val || integer_zerop (val))
1369 return false;
1370
1371 val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1372 integer_one_node);
1373 /* Set the minimum size to zero since the string in
1374 the array could have zero length. */
1375 *minlen = ssize_int (0);
204a7ecb
JJ
1376
1377 if (TREE_CODE (TREE_OPERAND (arg, 0)) == COMPONENT_REF
1378 && type == TREE_TYPE (TREE_OPERAND (arg, 0))
1379 && array_at_struct_end_p (TREE_OPERAND (arg, 0)))
1380 *flexp = true;
c42d0aa0
MS
1381 }
1382 else if (TREE_CODE (arg) == COMPONENT_REF
204a7ecb
JJ
1383 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 1)))
1384 == ARRAY_TYPE))
88d0c3f0
MS
1385 {
1386 /* Use the type of the member array to determine the upper
1387 bound on the length of the array. This may be overly
1388 optimistic if the array itself isn't NUL-terminated and
1389 the caller relies on the subsequent member to contain
c42d0aa0
MS
1390 the NUL but that would only be considered valid if
1391 the array were the last member of a struct.
3f343040
MS
1392 Set *FLEXP to true if the array whose bound is being
1393 used is at the end of a struct. */
c3e46927 1394 if (array_at_struct_end_p (arg))
3f343040
MS
1395 *flexp = true;
1396
88d0c3f0 1397 arg = TREE_OPERAND (arg, 1);
c42d0aa0
MS
1398
1399 tree type = TREE_TYPE (arg);
1400
1401 while (TREE_CODE (type) == ARRAY_TYPE
1402 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
1403 type = TREE_TYPE (type);
1404
1405 /* Fail when the array bound is unknown or zero. */
1406 val = TYPE_SIZE_UNIT (type);
88d0c3f0
MS
1407 if (!val || integer_zerop (val))
1408 return false;
1409 val = fold_build2 (MINUS_EXPR, TREE_TYPE (val), val,
1410 integer_one_node);
e495e31a
MS
1411 /* Set the minimum size to zero since the string in
1412 the array could have zero length. */
1413 *minlen = ssize_int (0);
88d0c3f0 1414 }
2004617a 1415
c42d0aa0 1416 if (VAR_P (arg))
2004617a 1417 {
c42d0aa0
MS
1418 tree type = TREE_TYPE (arg);
1419 if (POINTER_TYPE_P (type))
1420 type = TREE_TYPE (type);
1421
1422 if (TREE_CODE (type) == ARRAY_TYPE)
1423 {
1424 val = TYPE_SIZE_UNIT (type);
1425 if (!val
1426 || TREE_CODE (val) != INTEGER_CST
1427 || integer_zerop (val))
1428 return false;
1429 val = wide_int_to_tree (TREE_TYPE (val),
204a7ecb 1430 wi::sub (wi::to_wide (val), 1));
c42d0aa0
MS
1431 /* Set the minimum size to zero since the string in
1432 the array could have zero length. */
1433 *minlen = ssize_int (0);
1434 }
2004617a 1435 }
88d0c3f0
MS
1436 }
1437
fef5a0d9
RB
1438 if (!val)
1439 return false;
1440
c8602fe6
JJ
1441 if (!*minlen
1442 || (type > 0
1443 && TREE_CODE (*minlen) == INTEGER_CST
1444 && TREE_CODE (val) == INTEGER_CST
1445 && tree_int_cst_lt (val, *minlen)))
88d0c3f0
MS
1446 *minlen = val;
1447
1448 if (*maxlen)
fef5a0d9
RB
1449 {
1450 if (type > 0)
1451 {
88d0c3f0 1452 if (TREE_CODE (*maxlen) != INTEGER_CST
fef5a0d9
RB
1453 || TREE_CODE (val) != INTEGER_CST)
1454 return false;
1455
88d0c3f0
MS
1456 if (tree_int_cst_lt (*maxlen, val))
1457 *maxlen = val;
fef5a0d9
RB
1458 return true;
1459 }
88d0c3f0 1460 else if (simple_cst_equal (val, *maxlen) != 1)
fef5a0d9
RB
1461 return false;
1462 }
1463
88d0c3f0 1464 *maxlen = val;
fef5a0d9
RB
1465 return true;
1466 }
1467
1468 /* If ARG is registered for SSA update we cannot look at its defining
1469 statement. */
1470 if (name_registered_for_update_p (arg))
1471 return false;
1472
1473 /* If we were already here, break the infinite cycle. */
dcb7fae2
RB
1474 if (!*visited)
1475 *visited = BITMAP_ALLOC (NULL);
1476 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (arg)))
fef5a0d9
RB
1477 return true;
1478
1479 var = arg;
1480 def_stmt = SSA_NAME_DEF_STMT (var);
1481
1482 switch (gimple_code (def_stmt))
1483 {
1484 case GIMPLE_ASSIGN:
1485 /* The RHS of the statement defining VAR must either have a
1486 constant length or come from another SSA_NAME with a constant
1487 length. */
1488 if (gimple_assign_single_p (def_stmt)
1489 || gimple_assign_unary_nop_p (def_stmt))
1490 {
1491 tree rhs = gimple_assign_rhs1 (def_stmt);
3f343040 1492 return get_range_strlen (rhs, length, visited, type, fuzzy, flexp);
fef5a0d9
RB
1493 }
1494 else if (gimple_assign_rhs_code (def_stmt) == COND_EXPR)
1495 {
c8602fe6
JJ
1496 tree ops[2] = { gimple_assign_rhs2 (def_stmt),
1497 gimple_assign_rhs3 (def_stmt) };
1498
1499 for (unsigned int i = 0; i < 2; i++)
1500 if (!get_range_strlen (ops[i], length, visited, type, fuzzy,
1501 flexp))
1502 {
1503 if (fuzzy == 2)
1504 *maxlen = build_all_ones_cst (size_type_node);
1505 else
1506 return false;
1507 }
1508 return true;
cc8bea0a 1509 }
fef5a0d9
RB
1510 return false;
1511
1512 case GIMPLE_PHI:
c8602fe6
JJ
1513 /* All the arguments of the PHI node must have the same constant
1514 length. */
1515 for (unsigned i = 0; i < gimple_phi_num_args (def_stmt); i++)
fef5a0d9
RB
1516 {
1517 tree arg = gimple_phi_arg (def_stmt, i)->def;
1518
1519 /* If this PHI has itself as an argument, we cannot
1520 determine the string length of this argument. However,
1521 if we can find a constant string length for the other
1522 PHI args then we can still be sure that this is a
1523 constant string length. So be optimistic and just
1524 continue with the next argument. */
1525 if (arg == gimple_phi_result (def_stmt))
1526 continue;
1527
3f343040 1528 if (!get_range_strlen (arg, length, visited, type, fuzzy, flexp))
88d0c3f0 1529 {
c8602fe6 1530 if (fuzzy == 2)
88d0c3f0
MS
1531 *maxlen = build_all_ones_cst (size_type_node);
1532 else
1533 return false;
1534 }
fef5a0d9 1535 }
fef5a0d9
RB
1536 return true;
1537
1538 default:
1539 return false;
1540 }
1541}
1542
88d0c3f0
MS
1543/* Determine the minimum and maximum value or string length that ARG
1544 refers to and store each in the first two elements of MINMAXLEN.
1545 For expressions that point to strings of unknown lengths that are
1546 character arrays, use the upper bound of the array as the maximum
1547 length. For example, given an expression like 'x ? array : "xyz"'
1548 and array declared as 'char array[8]', MINMAXLEN[0] will be set
c8602fe6 1549 to 0 and MINMAXLEN[1] to 7, the longest string that could be
88d0c3f0 1550 stored in array.
3f343040
MS
1551 Return true if the range of the string lengths has been obtained
1552 from the upper bound of an array at the end of a struct. Such
1553 an array may hold a string that's longer than its upper bound
c8602fe6
JJ
1554 due to it being used as a poor-man's flexible array member.
1555
1556 STRICT is true if it will handle PHIs and COND_EXPRs conservatively
1557 and false if PHIs and COND_EXPRs are to be handled optimistically,
1558 if we can determine string length minimum and maximum; it will use
1559 the minimum from the ones where it can be determined.
1560 STRICT false should be only used for warning code. */
88d0c3f0 1561
3f343040 1562bool
c8602fe6 1563get_range_strlen (tree arg, tree minmaxlen[2], bool strict)
88d0c3f0
MS
1564{
1565 bitmap visited = NULL;
1566
1567 minmaxlen[0] = NULL_TREE;
1568 minmaxlen[1] = NULL_TREE;
1569
3f343040 1570 bool flexarray = false;
c8602fe6
JJ
1571 if (!get_range_strlen (arg, minmaxlen, &visited, 1, strict ? 1 : 2,
1572 &flexarray))
1573 {
1574 minmaxlen[0] = NULL_TREE;
1575 minmaxlen[1] = NULL_TREE;
1576 }
88d0c3f0
MS
1577
1578 if (visited)
1579 BITMAP_FREE (visited);
3f343040
MS
1580
1581 return flexarray;
88d0c3f0
MS
1582}
1583
dcb7fae2
RB
1584tree
1585get_maxval_strlen (tree arg, int type)
1586{
1587 bitmap visited = NULL;
88d0c3f0 1588 tree len[2] = { NULL_TREE, NULL_TREE };
3f343040
MS
1589
1590 bool dummy;
c8602fe6 1591 if (!get_range_strlen (arg, len, &visited, type, 0, &dummy))
88d0c3f0 1592 len[1] = NULL_TREE;
dcb7fae2
RB
1593 if (visited)
1594 BITMAP_FREE (visited);
1595
88d0c3f0 1596 return len[1];
dcb7fae2
RB
1597}
1598
fef5a0d9
RB
1599
1600/* Fold function call to builtin strcpy with arguments DEST and SRC.
1601 If LEN is not NULL, it represents the length of the string to be
1602 copied. Return NULL_TREE if no simplification can be made. */
1603
1604static bool
1605gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi,
dcb7fae2 1606 tree dest, tree src)
fef5a0d9 1607{
cc8bea0a
MS
1608 gimple *stmt = gsi_stmt (*gsi);
1609 location_t loc = gimple_location (stmt);
fef5a0d9
RB
1610 tree fn;
1611
1612 /* If SRC and DEST are the same (and not volatile), return DEST. */
1613 if (operand_equal_p (src, dest, 0))
1614 {
8cd95cec
MS
1615 /* Issue -Wrestrict unless the pointers are null (those do
1616 not point to objects and so do not indicate an overlap;
1617 such calls could be the result of sanitization and jump
1618 threading). */
1619 if (!integer_zerop (dest) && !gimple_no_warning_p (stmt))
e9b9fa4c
MS
1620 {
1621 tree func = gimple_call_fndecl (stmt);
cc8bea0a 1622
e9b9fa4c
MS
1623 warning_at (loc, OPT_Wrestrict,
1624 "%qD source argument is the same as destination",
1625 func);
1626 }
cc8bea0a 1627
fef5a0d9
RB
1628 replace_call_with_value (gsi, dest);
1629 return true;
1630 }
1631
1632 if (optimize_function_for_size_p (cfun))
1633 return false;
1634
1635 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1636 if (!fn)
1637 return false;
1638
1579e1f8 1639 tree len = get_maxval_strlen (src, 0);
fef5a0d9 1640 if (!len)
dcb7fae2 1641 return false;
fef5a0d9
RB
1642
1643 len = fold_convert_loc (loc, size_type_node, len);
1644 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
1645 len = force_gimple_operand_gsi (gsi, len, true,
1646 NULL_TREE, true, GSI_SAME_STMT);
355fe088 1647 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
fef5a0d9
RB
1648 replace_call_with_call_and_fold (gsi, repl);
1649 return true;
1650}
1651
1652/* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
1653 If SLEN is not NULL, it represents the length of the source string.
1654 Return NULL_TREE if no simplification can be made. */
1655
1656static bool
dcb7fae2
RB
1657gimple_fold_builtin_strncpy (gimple_stmt_iterator *gsi,
1658 tree dest, tree src, tree len)
fef5a0d9 1659{
025d57f0
MS
1660 gimple *stmt = gsi_stmt (*gsi);
1661 location_t loc = gimple_location (stmt);
6a33d0ff 1662 bool nonstring = get_attr_nonstring_decl (dest) != NULL_TREE;
fef5a0d9
RB
1663
1664 /* If the LEN parameter is zero, return DEST. */
1665 if (integer_zerop (len))
1666 {
6a33d0ff
MS
1667 /* Avoid warning if the destination refers to a an array/pointer
1668 decorate with attribute nonstring. */
1669 if (!nonstring)
1670 {
1671 tree fndecl = gimple_call_fndecl (stmt);
1672 gcall *call = as_a <gcall *> (stmt);
1673
1674 /* Warn about the lack of nul termination: the result is not
1675 a (nul-terminated) string. */
1676 tree slen = get_maxval_strlen (src, 0);
1677 if (slen && !integer_zerop (slen))
1678 warning_at (loc, OPT_Wstringop_truncation,
1679 "%G%qD destination unchanged after copying no bytes "
1680 "from a string of length %E",
1681 call, fndecl, slen);
1682 else
1683 warning_at (loc, OPT_Wstringop_truncation,
1684 "%G%qD destination unchanged after copying no bytes",
1685 call, fndecl);
1686 }
025d57f0 1687
fef5a0d9
RB
1688 replace_call_with_value (gsi, dest);
1689 return true;
1690 }
1691
1692 /* We can't compare slen with len as constants below if len is not a
1693 constant. */
dcb7fae2 1694 if (TREE_CODE (len) != INTEGER_CST)
fef5a0d9
RB
1695 return false;
1696
fef5a0d9 1697 /* Now, we must be passed a constant src ptr parameter. */
1579e1f8 1698 tree slen = get_maxval_strlen (src, 0);
dcb7fae2 1699 if (!slen || TREE_CODE (slen) != INTEGER_CST)
fef5a0d9
RB
1700 return false;
1701
025d57f0
MS
1702 /* The size of the source string including the terminating nul. */
1703 tree ssize = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
fef5a0d9
RB
1704
1705 /* We do not support simplification of this case, though we do
1706 support it when expanding trees into RTL. */
1707 /* FIXME: generate a call to __builtin_memset. */
025d57f0 1708 if (tree_int_cst_lt (ssize, len))
fef5a0d9
RB
1709 return false;
1710
5d0d5d68
MS
1711 /* Diagnose truncation that leaves the copy unterminated. */
1712 maybe_diag_stxncpy_trunc (*gsi, src, len);
025d57f0 1713
fef5a0d9 1714 /* OK transform into builtin memcpy. */
025d57f0 1715 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
fef5a0d9
RB
1716 if (!fn)
1717 return false;
1718
1719 len = fold_convert_loc (loc, size_type_node, len);
1720 len = force_gimple_operand_gsi (gsi, len, true,
1721 NULL_TREE, true, GSI_SAME_STMT);
355fe088 1722 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
fef5a0d9 1723 replace_call_with_call_and_fold (gsi, repl);
025d57f0 1724
fef5a0d9
RB
1725 return true;
1726}
1727
71dea1dd
WD
1728/* Fold function call to builtin strchr or strrchr.
1729 If both arguments are constant, evaluate and fold the result,
1730 otherwise simplify str(r)chr (str, 0) into str + strlen (str).
912d9ec3
WD
1731 In general strlen is significantly faster than strchr
1732 due to being a simpler operation. */
1733static bool
71dea1dd 1734gimple_fold_builtin_strchr (gimple_stmt_iterator *gsi, bool is_strrchr)
912d9ec3
WD
1735{
1736 gimple *stmt = gsi_stmt (*gsi);
1737 tree str = gimple_call_arg (stmt, 0);
1738 tree c = gimple_call_arg (stmt, 1);
1739 location_t loc = gimple_location (stmt);
71dea1dd
WD
1740 const char *p;
1741 char ch;
912d9ec3 1742
71dea1dd 1743 if (!gimple_call_lhs (stmt))
912d9ec3
WD
1744 return false;
1745
71dea1dd
WD
1746 if ((p = c_getstr (str)) && target_char_cst_p (c, &ch))
1747 {
1748 const char *p1 = is_strrchr ? strrchr (p, ch) : strchr (p, ch);
1749
1750 if (p1 == NULL)
1751 {
1752 replace_call_with_value (gsi, integer_zero_node);
1753 return true;
1754 }
1755
1756 tree len = build_int_cst (size_type_node, p1 - p);
1757 gimple_seq stmts = NULL;
1758 gimple *new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
1759 POINTER_PLUS_EXPR, str, len);
1760 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1761 gsi_replace_with_seq_vops (gsi, stmts);
1762 return true;
1763 }
1764
1765 if (!integer_zerop (c))
912d9ec3
WD
1766 return false;
1767
71dea1dd 1768 /* Transform strrchr (s, 0) to strchr (s, 0) when optimizing for size. */
c8952930 1769 if (is_strrchr && optimize_function_for_size_p (cfun))
71dea1dd
WD
1770 {
1771 tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
1772
c8952930 1773 if (strchr_fn)
71dea1dd
WD
1774 {
1775 gimple *repl = gimple_build_call (strchr_fn, 2, str, c);
1776 replace_call_with_call_and_fold (gsi, repl);
1777 return true;
1778 }
1779
1780 return false;
1781 }
1782
912d9ec3
WD
1783 tree len;
1784 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1785
1786 if (!strlen_fn)
1787 return false;
1788
1789 /* Create newstr = strlen (str). */
1790 gimple_seq stmts = NULL;
1791 gimple *new_stmt = gimple_build_call (strlen_fn, 1, str);
1792 gimple_set_location (new_stmt, loc);
a15ebbcd 1793 len = create_tmp_reg_or_ssa_name (size_type_node);
912d9ec3
WD
1794 gimple_call_set_lhs (new_stmt, len);
1795 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1796
1797 /* Create (str p+ strlen (str)). */
1798 new_stmt = gimple_build_assign (gimple_call_lhs (stmt),
1799 POINTER_PLUS_EXPR, str, len);
1800 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1801 gsi_replace_with_seq_vops (gsi, stmts);
1802 /* gsi now points at the assignment to the lhs, get a
1803 stmt iterator to the strlen.
1804 ??? We can't use gsi_for_stmt as that doesn't work when the
1805 CFG isn't built yet. */
1806 gimple_stmt_iterator gsi2 = *gsi;
1807 gsi_prev (&gsi2);
1808 fold_stmt (&gsi2);
1809 return true;
1810}
1811
c8952930
JJ
1812/* Fold function call to builtin strstr.
1813 If both arguments are constant, evaluate and fold the result,
1814 additionally fold strstr (x, "") into x and strstr (x, "c")
1815 into strchr (x, 'c'). */
1816static bool
1817gimple_fold_builtin_strstr (gimple_stmt_iterator *gsi)
1818{
1819 gimple *stmt = gsi_stmt (*gsi);
1820 tree haystack = gimple_call_arg (stmt, 0);
1821 tree needle = gimple_call_arg (stmt, 1);
1822 const char *p, *q;
1823
1824 if (!gimple_call_lhs (stmt))
1825 return false;
1826
1827 q = c_getstr (needle);
1828 if (q == NULL)
1829 return false;
1830
1831 if ((p = c_getstr (haystack)))
1832 {
1833 const char *r = strstr (p, q);
1834
1835 if (r == NULL)
1836 {
1837 replace_call_with_value (gsi, integer_zero_node);
1838 return true;
1839 }
1840
1841 tree len = build_int_cst (size_type_node, r - p);
1842 gimple_seq stmts = NULL;
1843 gimple *new_stmt
1844 = gimple_build_assign (gimple_call_lhs (stmt), POINTER_PLUS_EXPR,
1845 haystack, len);
1846 gimple_seq_add_stmt_without_update (&stmts, new_stmt);
1847 gsi_replace_with_seq_vops (gsi, stmts);
1848 return true;
1849 }
1850
1851 /* For strstr (x, "") return x. */
1852 if (q[0] == '\0')
1853 {
1854 replace_call_with_value (gsi, haystack);
1855 return true;
1856 }
1857
1858 /* Transform strstr (x, "c") into strchr (x, 'c'). */
1859 if (q[1] == '\0')
1860 {
1861 tree strchr_fn = builtin_decl_implicit (BUILT_IN_STRCHR);
1862 if (strchr_fn)
1863 {
1864 tree c = build_int_cst (integer_type_node, q[0]);
1865 gimple *repl = gimple_build_call (strchr_fn, 2, haystack, c);
1866 replace_call_with_call_and_fold (gsi, repl);
1867 return true;
1868 }
1869 }
1870
1871 return false;
1872}
1873
fef5a0d9
RB
1874/* Simplify a call to the strcat builtin. DST and SRC are the arguments
1875 to the call.
1876
1877 Return NULL_TREE if no simplification was possible, otherwise return the
1878 simplified form of the call as a tree.
1879
1880 The simplified form may be a constant or other expression which
1881 computes the same value, but in a more efficient manner (including
1882 calls to other builtin functions).
1883
1884 The call may contain arguments which need to be evaluated, but
1885 which are not useful to determine the result of the call. In
1886 this case we return a chain of COMPOUND_EXPRs. The LHS of each
1887 COMPOUND_EXPR will be an argument which must be evaluated.
1888 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
1889 COMPOUND_EXPR in the chain will contain the tree for the simplified
1890 form of the builtin function call. */
1891
1892static bool
dcb7fae2 1893gimple_fold_builtin_strcat (gimple_stmt_iterator *gsi, tree dst, tree src)
fef5a0d9 1894{
355fe088 1895 gimple *stmt = gsi_stmt (*gsi);
dcb7fae2 1896 location_t loc = gimple_location (stmt);
fef5a0d9
RB
1897
1898 const char *p = c_getstr (src);
1899
1900 /* If the string length is zero, return the dst parameter. */
1901 if (p && *p == '\0')
1902 {
1903 replace_call_with_value (gsi, dst);
1904 return true;
1905 }
1906
1907 if (!optimize_bb_for_speed_p (gimple_bb (stmt)))
1908 return false;
1909
1910 /* See if we can store by pieces into (dst + strlen(dst)). */
1911 tree newdst;
1912 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
1913 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1914
1915 if (!strlen_fn || !memcpy_fn)
1916 return false;
1917
1918 /* If the length of the source string isn't computable don't
1919 split strcat into strlen and memcpy. */
dcb7fae2 1920 tree len = get_maxval_strlen (src, 0);
fef5a0d9 1921 if (! len)
fef5a0d9
RB
1922 return false;
1923
1924 /* Create strlen (dst). */
1925 gimple_seq stmts = NULL, stmts2;
355fe088 1926 gimple *repl = gimple_build_call (strlen_fn, 1, dst);
fef5a0d9 1927 gimple_set_location (repl, loc);
a15ebbcd 1928 newdst = create_tmp_reg_or_ssa_name (size_type_node);
fef5a0d9
RB
1929 gimple_call_set_lhs (repl, newdst);
1930 gimple_seq_add_stmt_without_update (&stmts, repl);
1931
1932 /* Create (dst p+ strlen (dst)). */
1933 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
1934 newdst = force_gimple_operand (newdst, &stmts2, true, NULL_TREE);
1935 gimple_seq_add_seq_without_update (&stmts, stmts2);
1936
1937 len = fold_convert_loc (loc, size_type_node, len);
1938 len = size_binop_loc (loc, PLUS_EXPR, len,
1939 build_int_cst (size_type_node, 1));
1940 len = force_gimple_operand (len, &stmts2, true, NULL_TREE);
1941 gimple_seq_add_seq_without_update (&stmts, stmts2);
1942
1943 repl = gimple_build_call (memcpy_fn, 3, newdst, src, len);
1944 gimple_seq_add_stmt_without_update (&stmts, repl);
1945 if (gimple_call_lhs (stmt))
1946 {
1947 repl = gimple_build_assign (gimple_call_lhs (stmt), dst);
1948 gimple_seq_add_stmt_without_update (&stmts, repl);
1949 gsi_replace_with_seq_vops (gsi, stmts);
1950 /* gsi now points at the assignment to the lhs, get a
1951 stmt iterator to the memcpy call.
1952 ??? We can't use gsi_for_stmt as that doesn't work when the
1953 CFG isn't built yet. */
1954 gimple_stmt_iterator gsi2 = *gsi;
1955 gsi_prev (&gsi2);
1956 fold_stmt (&gsi2);
1957 }
1958 else
1959 {
1960 gsi_replace_with_seq_vops (gsi, stmts);
1961 fold_stmt (gsi);
1962 }
1963 return true;
1964}
1965
07f1cf56
RB
1966/* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
1967 are the arguments to the call. */
1968
1969static bool
1970gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
1971{
355fe088 1972 gimple *stmt = gsi_stmt (*gsi);
07f1cf56
RB
1973 tree dest = gimple_call_arg (stmt, 0);
1974 tree src = gimple_call_arg (stmt, 1);
1975 tree size = gimple_call_arg (stmt, 2);
1976 tree fn;
1977 const char *p;
1978
1979
1980 p = c_getstr (src);
1981 /* If the SRC parameter is "", return DEST. */
1982 if (p && *p == '\0')
1983 {
1984 replace_call_with_value (gsi, dest);
1985 return true;
1986 }
1987
1988 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
1989 return false;
1990
1991 /* If __builtin_strcat_chk is used, assume strcat is available. */
1992 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
1993 if (!fn)
1994 return false;
1995
355fe088 1996 gimple *repl = gimple_build_call (fn, 2, dest, src);
07f1cf56
RB
1997 replace_call_with_call_and_fold (gsi, repl);
1998 return true;
1999}
2000
ad03a744
RB
2001/* Simplify a call to the strncat builtin. */
2002
2003static bool
2004gimple_fold_builtin_strncat (gimple_stmt_iterator *gsi)
2005{
2006 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2007 tree dst = gimple_call_arg (stmt, 0);
2008 tree src = gimple_call_arg (stmt, 1);
2009 tree len = gimple_call_arg (stmt, 2);
2010
2011 const char *p = c_getstr (src);
2012
2013 /* If the requested length is zero, or the src parameter string
2014 length is zero, return the dst parameter. */
2015 if (integer_zerop (len) || (p && *p == '\0'))
2016 {
2017 replace_call_with_value (gsi, dst);
2018 return true;
2019 }
2020
025d57f0
MS
2021 if (TREE_CODE (len) != INTEGER_CST || !p)
2022 return false;
2023
2024 unsigned srclen = strlen (p);
2025
2026 int cmpsrc = compare_tree_int (len, srclen);
2027
2028 /* Return early if the requested len is less than the string length.
2029 Warnings will be issued elsewhere later. */
2030 if (cmpsrc < 0)
2031 return false;
2032
2033 unsigned HOST_WIDE_INT dstsize;
2034
2035 bool nowarn = gimple_no_warning_p (stmt);
2036
2037 if (!nowarn && compute_builtin_object_size (dst, 1, &dstsize))
ad03a744 2038 {
025d57f0 2039 int cmpdst = compare_tree_int (len, dstsize);
ad03a744 2040
025d57f0
MS
2041 if (cmpdst >= 0)
2042 {
2043 tree fndecl = gimple_call_fndecl (stmt);
2044
2045 /* Strncat copies (at most) LEN bytes and always appends
2046 the terminating NUL so the specified bound should never
2047 be equal to (or greater than) the size of the destination.
2048 If it is, the copy could overflow. */
2049 location_t loc = gimple_location (stmt);
2050 nowarn = warning_at (loc, OPT_Wstringop_overflow_,
2051 cmpdst == 0
2052 ? G_("%G%qD specified bound %E equals "
2053 "destination size")
2054 : G_("%G%qD specified bound %E exceeds "
2055 "destination size %wu"),
2056 stmt, fndecl, len, dstsize);
2057 if (nowarn)
2058 gimple_set_no_warning (stmt, true);
2059 }
2060 }
ad03a744 2061
025d57f0
MS
2062 if (!nowarn && cmpsrc == 0)
2063 {
2064 tree fndecl = gimple_call_fndecl (stmt);
2065
2066 /* To avoid certain truncation the specified bound should also
2067 not be equal to (or less than) the length of the source. */
2068 location_t loc = gimple_location (stmt);
2069 if (warning_at (loc, OPT_Wstringop_overflow_,
2070 "%G%qD specified bound %E equals source length",
2071 stmt, fndecl, len))
2072 gimple_set_no_warning (stmt, true);
ad03a744
RB
2073 }
2074
025d57f0
MS
2075 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
2076
2077 /* If the replacement _DECL isn't initialized, don't do the
2078 transformation. */
2079 if (!fn)
2080 return false;
2081
2082 /* Otherwise, emit a call to strcat. */
2083 gcall *repl = gimple_build_call (fn, 2, dst, src);
2084 replace_call_with_call_and_fold (gsi, repl);
2085 return true;
ad03a744
RB
2086}
2087
745583f9
RB
2088/* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
2089 LEN, and SIZE. */
2090
2091static bool
2092gimple_fold_builtin_strncat_chk (gimple_stmt_iterator *gsi)
2093{
355fe088 2094 gimple *stmt = gsi_stmt (*gsi);
745583f9
RB
2095 tree dest = gimple_call_arg (stmt, 0);
2096 tree src = gimple_call_arg (stmt, 1);
2097 tree len = gimple_call_arg (stmt, 2);
2098 tree size = gimple_call_arg (stmt, 3);
2099 tree fn;
2100 const char *p;
2101
2102 p = c_getstr (src);
2103 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
2104 if ((p && *p == '\0')
2105 || integer_zerop (len))
2106 {
2107 replace_call_with_value (gsi, dest);
2108 return true;
2109 }
2110
2111 if (! tree_fits_uhwi_p (size))
2112 return false;
2113
2114 if (! integer_all_onesp (size))
2115 {
2116 tree src_len = c_strlen (src, 1);
2117 if (src_len
2118 && tree_fits_uhwi_p (src_len)
2119 && tree_fits_uhwi_p (len)
2120 && ! tree_int_cst_lt (len, src_len))
2121 {
2122 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
2123 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
2124 if (!fn)
2125 return false;
2126
355fe088 2127 gimple *repl = gimple_build_call (fn, 3, dest, src, size);
745583f9
RB
2128 replace_call_with_call_and_fold (gsi, repl);
2129 return true;
2130 }
2131 return false;
2132 }
2133
2134 /* If __builtin_strncat_chk is used, assume strncat is available. */
2135 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
2136 if (!fn)
2137 return false;
2138
355fe088 2139 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
745583f9
RB
2140 replace_call_with_call_and_fold (gsi, repl);
2141 return true;
2142}
2143
a918bfbf
ML
2144/* Build and append gimple statements to STMTS that would load a first
2145 character of a memory location identified by STR. LOC is location
2146 of the statement. */
2147
2148static tree
2149gimple_load_first_char (location_t loc, tree str, gimple_seq *stmts)
2150{
2151 tree var;
2152
2153 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
2154 tree cst_uchar_ptr_node
2155 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
2156 tree off0 = build_int_cst (cst_uchar_ptr_node, 0);
2157
2158 tree temp = fold_build2_loc (loc, MEM_REF, cst_uchar_node, str, off0);
2159 gassign *stmt = gimple_build_assign (NULL_TREE, temp);
2160 var = create_tmp_reg_or_ssa_name (cst_uchar_node, stmt);
2161
2162 gimple_assign_set_lhs (stmt, var);
2163 gimple_seq_add_stmt_without_update (stmts, stmt);
2164
2165 return var;
2166}
2167
2168/* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
2169 FCODE is the name of the builtin. */
2170
2171static bool
2172gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
2173{
2174 gimple *stmt = gsi_stmt (*gsi);
2175 tree callee = gimple_call_fndecl (stmt);
2176 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
2177
2178 tree type = integer_type_node;
2179 tree str1 = gimple_call_arg (stmt, 0);
2180 tree str2 = gimple_call_arg (stmt, 1);
2181 tree lhs = gimple_call_lhs (stmt);
2182 HOST_WIDE_INT length = -1;
2183
2184 /* Handle strncmp and strncasecmp functions. */
2185 if (gimple_call_num_args (stmt) == 3)
2186 {
2187 tree len = gimple_call_arg (stmt, 2);
2188 if (tree_fits_uhwi_p (len))
2189 length = tree_to_uhwi (len);
2190 }
2191
2192 /* If the LEN parameter is zero, return zero. */
2193 if (length == 0)
2194 {
2195 replace_call_with_value (gsi, integer_zero_node);
2196 return true;
2197 }
2198
2199 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
2200 if (operand_equal_p (str1, str2, 0))
2201 {
2202 replace_call_with_value (gsi, integer_zero_node);
2203 return true;
2204 }
2205
2206 const char *p1 = c_getstr (str1);
2207 const char *p2 = c_getstr (str2);
2208
2209 /* For known strings, return an immediate value. */
2210 if (p1 && p2)
2211 {
2212 int r = 0;
2213 bool known_result = false;
2214
2215 switch (fcode)
2216 {
2217 case BUILT_IN_STRCMP:
8b0b334a 2218 case BUILT_IN_STRCMP_EQ:
a918bfbf
ML
2219 {
2220 r = strcmp (p1, p2);
2221 known_result = true;
2222 break;
2223 }
2224 case BUILT_IN_STRNCMP:
8b0b334a 2225 case BUILT_IN_STRNCMP_EQ:
a918bfbf
ML
2226 {
2227 if (length == -1)
2228 break;
2229 r = strncmp (p1, p2, length);
2230 known_result = true;
2231 break;
2232 }
2233 /* Only handleable situation is where the string are equal (result 0),
2234 which is already handled by operand_equal_p case. */
2235 case BUILT_IN_STRCASECMP:
2236 break;
2237 case BUILT_IN_STRNCASECMP:
2238 {
2239 if (length == -1)
2240 break;
2241 r = strncmp (p1, p2, length);
2242 if (r == 0)
2243 known_result = true;
5de73c05 2244 break;
a918bfbf
ML
2245 }
2246 default:
2247 gcc_unreachable ();
2248 }
2249
2250 if (known_result)
2251 {
2252 replace_call_with_value (gsi, build_cmp_result (type, r));
2253 return true;
2254 }
2255 }
2256
2257 bool nonzero_length = length >= 1
2258 || fcode == BUILT_IN_STRCMP
8b0b334a 2259 || fcode == BUILT_IN_STRCMP_EQ
a918bfbf
ML
2260 || fcode == BUILT_IN_STRCASECMP;
2261
2262 location_t loc = gimple_location (stmt);
2263
2264 /* If the second arg is "", return *(const unsigned char*)arg1. */
2265 if (p2 && *p2 == '\0' && nonzero_length)
2266 {
2267 gimple_seq stmts = NULL;
2268 tree var = gimple_load_first_char (loc, str1, &stmts);
2269 if (lhs)
2270 {
2271 stmt = gimple_build_assign (lhs, NOP_EXPR, var);
2272 gimple_seq_add_stmt_without_update (&stmts, stmt);
2273 }
2274
2275 gsi_replace_with_seq_vops (gsi, stmts);
2276 return true;
2277 }
2278
2279 /* If the first arg is "", return -*(const unsigned char*)arg2. */
2280 if (p1 && *p1 == '\0' && nonzero_length)
2281 {
2282 gimple_seq stmts = NULL;
2283 tree var = gimple_load_first_char (loc, str2, &stmts);
2284
2285 if (lhs)
2286 {
2287 tree c = create_tmp_reg_or_ssa_name (integer_type_node);
2288 stmt = gimple_build_assign (c, NOP_EXPR, var);
2289 gimple_seq_add_stmt_without_update (&stmts, stmt);
2290
2291 stmt = gimple_build_assign (lhs, NEGATE_EXPR, c);
2292 gimple_seq_add_stmt_without_update (&stmts, stmt);
2293 }
2294
2295 gsi_replace_with_seq_vops (gsi, stmts);
2296 return true;
2297 }
2298
2299 /* If len parameter is one, return an expression corresponding to
2300 (*(const unsigned char*)arg2 - *(const unsigned char*)arg1). */
2301 if (fcode == BUILT_IN_STRNCMP && length == 1)
2302 {
2303 gimple_seq stmts = NULL;
2304 tree temp1 = gimple_load_first_char (loc, str1, &stmts);
2305 tree temp2 = gimple_load_first_char (loc, str2, &stmts);
2306
2307 if (lhs)
2308 {
2309 tree c1 = create_tmp_reg_or_ssa_name (integer_type_node);
2310 gassign *convert1 = gimple_build_assign (c1, NOP_EXPR, temp1);
2311 gimple_seq_add_stmt_without_update (&stmts, convert1);
2312
2313 tree c2 = create_tmp_reg_or_ssa_name (integer_type_node);
2314 gassign *convert2 = gimple_build_assign (c2, NOP_EXPR, temp2);
2315 gimple_seq_add_stmt_without_update (&stmts, convert2);
2316
2317 stmt = gimple_build_assign (lhs, MINUS_EXPR, c1, c2);
2318 gimple_seq_add_stmt_without_update (&stmts, stmt);
2319 }
2320
2321 gsi_replace_with_seq_vops (gsi, stmts);
2322 return true;
2323 }
2324
caed5c92
QZ
2325 /* If length is larger than the length of one constant string,
2326 replace strncmp with corresponding strcmp */
2327 if (fcode == BUILT_IN_STRNCMP
2328 && length > 0
2329 && ((p2 && (size_t) length > strlen (p2))
2330 || (p1 && (size_t) length > strlen (p1))))
2331 {
2332 tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
2333 if (!fn)
2334 return false;
2335 gimple *repl = gimple_build_call (fn, 2, str1, str2);
2336 replace_call_with_call_and_fold (gsi, repl);
2337 return true;
2338 }
2339
a918bfbf
ML
2340 return false;
2341}
2342
488c6247
ML
2343/* Fold a call to the memchr pointed by GSI iterator. */
2344
2345static bool
2346gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
2347{
2348 gimple *stmt = gsi_stmt (*gsi);
2349 tree lhs = gimple_call_lhs (stmt);
2350 tree arg1 = gimple_call_arg (stmt, 0);
2351 tree arg2 = gimple_call_arg (stmt, 1);
2352 tree len = gimple_call_arg (stmt, 2);
2353
2354 /* If the LEN parameter is zero, return zero. */
2355 if (integer_zerop (len))
2356 {
2357 replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2358 return true;
2359 }
2360
2361 char c;
2362 if (TREE_CODE (arg2) != INTEGER_CST
2363 || !tree_fits_uhwi_p (len)
2364 || !target_char_cst_p (arg2, &c))
2365 return false;
2366
2367 unsigned HOST_WIDE_INT length = tree_to_uhwi (len);
2368 unsigned HOST_WIDE_INT string_length;
2369 const char *p1 = c_getstr (arg1, &string_length);
2370
2371 if (p1)
2372 {
2373 const char *r = (const char *)memchr (p1, c, MIN (length, string_length));
2374 if (r == NULL)
2375 {
2376 if (length <= string_length)
2377 {
2378 replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0));
2379 return true;
2380 }
2381 }
2382 else
2383 {
2384 unsigned HOST_WIDE_INT offset = r - p1;
2385 gimple_seq stmts = NULL;
2386 if (lhs != NULL_TREE)
2387 {
2388 tree offset_cst = build_int_cst (TREE_TYPE (len), offset);
2389 gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR,
2390 arg1, offset_cst);
2391 gimple_seq_add_stmt_without_update (&stmts, stmt);
2392 }
2393 else
2394 gimple_seq_add_stmt_without_update (&stmts,
2395 gimple_build_nop ());
2396
2397 gsi_replace_with_seq_vops (gsi, stmts);
2398 return true;
2399 }
2400 }
2401
2402 return false;
2403}
a918bfbf 2404
fef5a0d9
RB
2405/* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
2406 to the call. IGNORE is true if the value returned
2407 by the builtin will be ignored. UNLOCKED is true is true if this
2408 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
2409 the known length of the string. Return NULL_TREE if no simplification
2410 was possible. */
2411
2412static bool
2413gimple_fold_builtin_fputs (gimple_stmt_iterator *gsi,
fef5a0d9 2414 tree arg0, tree arg1,
dcb7fae2 2415 bool unlocked)
fef5a0d9 2416{
355fe088 2417 gimple *stmt = gsi_stmt (*gsi);
dcb7fae2 2418
fef5a0d9
RB
2419 /* If we're using an unlocked function, assume the other unlocked
2420 functions exist explicitly. */
2421 tree const fn_fputc = (unlocked
2422 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
2423 : builtin_decl_implicit (BUILT_IN_FPUTC));
2424 tree const fn_fwrite = (unlocked
2425 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
2426 : builtin_decl_implicit (BUILT_IN_FWRITE));
2427
2428 /* If the return value is used, don't do the transformation. */
dcb7fae2 2429 if (gimple_call_lhs (stmt))
fef5a0d9
RB
2430 return false;
2431
fef5a0d9
RB
2432 /* Get the length of the string passed to fputs. If the length
2433 can't be determined, punt. */
dcb7fae2 2434 tree len = get_maxval_strlen (arg0, 0);
fef5a0d9
RB
2435 if (!len
2436 || TREE_CODE (len) != INTEGER_CST)
2437 return false;
2438
2439 switch (compare_tree_int (len, 1))
2440 {
2441 case -1: /* length is 0, delete the call entirely . */
2442 replace_call_with_value (gsi, integer_zero_node);
2443 return true;
2444
2445 case 0: /* length is 1, call fputc. */
2446 {
2447 const char *p = c_getstr (arg0);
2448 if (p != NULL)
2449 {
2450 if (!fn_fputc)
2451 return false;
2452
355fe088 2453 gimple *repl = gimple_build_call (fn_fputc, 2,
fef5a0d9
RB
2454 build_int_cst
2455 (integer_type_node, p[0]), arg1);
2456 replace_call_with_call_and_fold (gsi, repl);
2457 return true;
2458 }
2459 }
2460 /* FALLTHROUGH */
2461 case 1: /* length is greater than 1, call fwrite. */
2462 {
2463 /* If optimizing for size keep fputs. */
2464 if (optimize_function_for_size_p (cfun))
2465 return false;
2466 /* New argument list transforming fputs(string, stream) to
2467 fwrite(string, 1, len, stream). */
2468 if (!fn_fwrite)
2469 return false;
2470
355fe088 2471 gimple *repl = gimple_build_call (fn_fwrite, 4, arg0,
fef5a0d9
RB
2472 size_one_node, len, arg1);
2473 replace_call_with_call_and_fold (gsi, repl);
2474 return true;
2475 }
2476 default:
2477 gcc_unreachable ();
2478 }
2479 return false;
2480}
2481
2482/* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
2483 DEST, SRC, LEN, and SIZE are the arguments to the call.
2484 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
2485 code of the builtin. If MAXLEN is not NULL, it is maximum length
2486 passed as third argument. */
2487
2488static bool
2489gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi,
fef5a0d9 2490 tree dest, tree src, tree len, tree size,
fef5a0d9
RB
2491 enum built_in_function fcode)
2492{
355fe088 2493 gimple *stmt = gsi_stmt (*gsi);
dcb7fae2
RB
2494 location_t loc = gimple_location (stmt);
2495 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
fef5a0d9
RB
2496 tree fn;
2497
2498 /* If SRC and DEST are the same (and not volatile), return DEST
2499 (resp. DEST+LEN for __mempcpy_chk). */
2500 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
2501 {
2502 if (fcode != BUILT_IN_MEMPCPY_CHK)
2503 {
2504 replace_call_with_value (gsi, dest);
2505 return true;
2506 }
2507 else
2508 {
74e3c262
RB
2509 gimple_seq stmts = NULL;
2510 len = gimple_convert_to_ptrofftype (&stmts, loc, len);
22518428
JJ
2511 tree temp = gimple_build (&stmts, loc, POINTER_PLUS_EXPR,
2512 TREE_TYPE (dest), dest, len);
74e3c262 2513 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
fef5a0d9
RB
2514 replace_call_with_value (gsi, temp);
2515 return true;
2516 }
2517 }
2518
2519 if (! tree_fits_uhwi_p (size))
2520 return false;
2521
dcb7fae2 2522 tree maxlen = get_maxval_strlen (len, 2);
fef5a0d9
RB
2523 if (! integer_all_onesp (size))
2524 {
2525 if (! tree_fits_uhwi_p (len))
2526 {
2527 /* If LEN is not constant, try MAXLEN too.
2528 For MAXLEN only allow optimizing into non-_ocs function
2529 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2530 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2531 {
2532 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
2533 {
2534 /* (void) __mempcpy_chk () can be optimized into
2535 (void) __memcpy_chk (). */
2536 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2537 if (!fn)
2538 return false;
2539
355fe088 2540 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
fef5a0d9
RB
2541 replace_call_with_call_and_fold (gsi, repl);
2542 return true;
2543 }
2544 return false;
2545 }
2546 }
2547 else
2548 maxlen = len;
2549
2550 if (tree_int_cst_lt (size, maxlen))
2551 return false;
2552 }
2553
2554 fn = NULL_TREE;
2555 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
2556 mem{cpy,pcpy,move,set} is available. */
2557 switch (fcode)
2558 {
2559 case BUILT_IN_MEMCPY_CHK:
2560 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
2561 break;
2562 case BUILT_IN_MEMPCPY_CHK:
2563 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
2564 break;
2565 case BUILT_IN_MEMMOVE_CHK:
2566 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
2567 break;
2568 case BUILT_IN_MEMSET_CHK:
2569 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
2570 break;
2571 default:
2572 break;
2573 }
2574
2575 if (!fn)
2576 return false;
2577
355fe088 2578 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
fef5a0d9
RB
2579 replace_call_with_call_and_fold (gsi, repl);
2580 return true;
2581}
2582
2583/* Fold a call to the __st[rp]cpy_chk builtin.
2584 DEST, SRC, and SIZE are the arguments to the call.
2585 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
2586 code of the builtin. If MAXLEN is not NULL, it is maximum length of
2587 strings passed as second argument. */
2588
2589static bool
2590gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi,
dcb7fae2 2591 tree dest,
fef5a0d9 2592 tree src, tree size,
fef5a0d9
RB
2593 enum built_in_function fcode)
2594{
355fe088 2595 gimple *stmt = gsi_stmt (*gsi);
dcb7fae2
RB
2596 location_t loc = gimple_location (stmt);
2597 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
fef5a0d9
RB
2598 tree len, fn;
2599
2600 /* If SRC and DEST are the same (and not volatile), return DEST. */
2601 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
2602 {
8cd95cec
MS
2603 /* Issue -Wrestrict unless the pointers are null (those do
2604 not point to objects and so do not indicate an overlap;
2605 such calls could be the result of sanitization and jump
2606 threading). */
2607 if (!integer_zerop (dest) && !gimple_no_warning_p (stmt))
e9b9fa4c
MS
2608 {
2609 tree func = gimple_call_fndecl (stmt);
cc8bea0a 2610
e9b9fa4c
MS
2611 warning_at (loc, OPT_Wrestrict,
2612 "%qD source argument is the same as destination",
2613 func);
2614 }
cc8bea0a 2615
fef5a0d9
RB
2616 replace_call_with_value (gsi, dest);
2617 return true;
2618 }
2619
2620 if (! tree_fits_uhwi_p (size))
2621 return false;
2622
dcb7fae2 2623 tree maxlen = get_maxval_strlen (src, 1);
fef5a0d9
RB
2624 if (! integer_all_onesp (size))
2625 {
2626 len = c_strlen (src, 1);
2627 if (! len || ! tree_fits_uhwi_p (len))
2628 {
2629 /* If LEN is not constant, try MAXLEN too.
2630 For MAXLEN only allow optimizing into non-_ocs function
2631 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2632 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2633 {
2634 if (fcode == BUILT_IN_STPCPY_CHK)
2635 {
2636 if (! ignore)
2637 return false;
2638
2639 /* If return value of __stpcpy_chk is ignored,
2640 optimize into __strcpy_chk. */
2641 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
2642 if (!fn)
2643 return false;
2644
355fe088 2645 gimple *repl = gimple_build_call (fn, 3, dest, src, size);
fef5a0d9
RB
2646 replace_call_with_call_and_fold (gsi, repl);
2647 return true;
2648 }
2649
2650 if (! len || TREE_SIDE_EFFECTS (len))
2651 return false;
2652
2653 /* If c_strlen returned something, but not a constant,
2654 transform __strcpy_chk into __memcpy_chk. */
2655 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2656 if (!fn)
2657 return false;
2658
74e3c262
RB
2659 gimple_seq stmts = NULL;
2660 len = gimple_convert (&stmts, loc, size_type_node, len);
2661 len = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node, len,
2662 build_int_cst (size_type_node, 1));
2663 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
355fe088 2664 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
fef5a0d9
RB
2665 replace_call_with_call_and_fold (gsi, repl);
2666 return true;
2667 }
e256dfce 2668 }
fef5a0d9
RB
2669 else
2670 maxlen = len;
2671
2672 if (! tree_int_cst_lt (maxlen, size))
2673 return false;
e256dfce
RG
2674 }
2675
fef5a0d9
RB
2676 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
2677 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
2678 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
2679 if (!fn)
2680 return false;
2681
355fe088 2682 gimple *repl = gimple_build_call (fn, 2, dest, src);
fef5a0d9
RB
2683 replace_call_with_call_and_fold (gsi, repl);
2684 return true;
2685}
2686
2687/* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
2688 are the arguments to the call. If MAXLEN is not NULL, it is maximum
2689 length passed as third argument. IGNORE is true if return value can be
2690 ignored. FCODE is the BUILT_IN_* code of the builtin. */
2691
2692static bool
2693gimple_fold_builtin_stxncpy_chk (gimple_stmt_iterator *gsi,
2694 tree dest, tree src,
dcb7fae2 2695 tree len, tree size,
fef5a0d9
RB
2696 enum built_in_function fcode)
2697{
355fe088 2698 gimple *stmt = gsi_stmt (*gsi);
dcb7fae2 2699 bool ignore = gimple_call_lhs (stmt) == NULL_TREE;
fef5a0d9
RB
2700 tree fn;
2701
2702 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
cbdd87d4 2703 {
fef5a0d9
RB
2704 /* If return value of __stpncpy_chk is ignored,
2705 optimize into __strncpy_chk. */
2706 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
2707 if (fn)
2708 {
355fe088 2709 gimple *repl = gimple_build_call (fn, 4, dest, src, len, size);
fef5a0d9
RB
2710 replace_call_with_call_and_fold (gsi, repl);
2711 return true;
2712 }
cbdd87d4
RG
2713 }
2714
fef5a0d9
RB
2715 if (! tree_fits_uhwi_p (size))
2716 return false;
2717
dcb7fae2 2718 tree maxlen = get_maxval_strlen (len, 2);
fef5a0d9 2719 if (! integer_all_onesp (size))
cbdd87d4 2720 {
fef5a0d9 2721 if (! tree_fits_uhwi_p (len))
fe2ef088 2722 {
fef5a0d9
RB
2723 /* If LEN is not constant, try MAXLEN too.
2724 For MAXLEN only allow optimizing into non-_ocs function
2725 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2726 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
2727 return false;
8a1561bc 2728 }
fef5a0d9
RB
2729 else
2730 maxlen = len;
2731
2732 if (tree_int_cst_lt (size, maxlen))
2733 return false;
cbdd87d4
RG
2734 }
2735
fef5a0d9
RB
2736 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
2737 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
2738 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
2739 if (!fn)
2740 return false;
2741
355fe088 2742 gimple *repl = gimple_build_call (fn, 3, dest, src, len);
fef5a0d9
RB
2743 replace_call_with_call_and_fold (gsi, repl);
2744 return true;
cbdd87d4
RG
2745}
2746
2625bb5d
RB
2747/* Fold function call to builtin stpcpy with arguments DEST and SRC.
2748 Return NULL_TREE if no simplification can be made. */
2749
2750static bool
2751gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
2752{
2753 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
2754 location_t loc = gimple_location (stmt);
2755 tree dest = gimple_call_arg (stmt, 0);
2756 tree src = gimple_call_arg (stmt, 1);
2757 tree fn, len, lenp1;
2758
2759 /* If the result is unused, replace stpcpy with strcpy. */
2760 if (gimple_call_lhs (stmt) == NULL_TREE)
2761 {
2762 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
2763 if (!fn)
2764 return false;
2765 gimple_call_set_fndecl (stmt, fn);
2766 fold_stmt (gsi);
2767 return true;
2768 }
2769
2770 len = c_strlen (src, 1);
2771 if (!len
2772 || TREE_CODE (len) != INTEGER_CST)
2773 return false;
2774
2775 if (optimize_function_for_size_p (cfun)
2776 /* If length is zero it's small enough. */
2777 && !integer_zerop (len))
2778 return false;
2779
2780 /* If the source has a known length replace stpcpy with memcpy. */
2781 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2782 if (!fn)
2783 return false;
2784
2785 gimple_seq stmts = NULL;
2786 tree tem = gimple_convert (&stmts, loc, size_type_node, len);
2787 lenp1 = gimple_build (&stmts, loc, PLUS_EXPR, size_type_node,
2788 tem, build_int_cst (size_type_node, 1));
2789 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2790 gcall *repl = gimple_build_call (fn, 3, dest, src, lenp1);
2791 gimple_set_vuse (repl, gimple_vuse (stmt));
2792 gimple_set_vdef (repl, gimple_vdef (stmt));
2793 if (gimple_vdef (repl)
2794 && TREE_CODE (gimple_vdef (repl)) == SSA_NAME)
2795 SSA_NAME_DEF_STMT (gimple_vdef (repl)) = repl;
2796 gsi_insert_before (gsi, repl, GSI_SAME_STMT);
2797 /* Replace the result with dest + len. */
2798 stmts = NULL;
2799 tem = gimple_convert (&stmts, loc, sizetype, len);
2800 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
2801 gassign *ret = gimple_build_assign (gimple_call_lhs (stmt),
2802 POINTER_PLUS_EXPR, dest, tem);
f6b4dc28 2803 gsi_replace (gsi, ret, false);
2625bb5d
RB
2804 /* Finally fold the memcpy call. */
2805 gimple_stmt_iterator gsi2 = *gsi;
2806 gsi_prev (&gsi2);
2807 fold_stmt (&gsi2);
2808 return true;
2809}
2810
fef5a0d9
RB
2811/* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
2812 NULL_TREE if a normal call should be emitted rather than expanding
2813 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
2814 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
2815 passed as second argument. */
cbdd87d4
RG
2816
2817static bool
fef5a0d9 2818gimple_fold_builtin_snprintf_chk (gimple_stmt_iterator *gsi,
dcb7fae2 2819 enum built_in_function fcode)
cbdd87d4 2820{
538dd0b7 2821 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
fef5a0d9
RB
2822 tree dest, size, len, fn, fmt, flag;
2823 const char *fmt_str;
cbdd87d4 2824
fef5a0d9
RB
2825 /* Verify the required arguments in the original call. */
2826 if (gimple_call_num_args (stmt) < 5)
2827 return false;
cbdd87d4 2828
fef5a0d9
RB
2829 dest = gimple_call_arg (stmt, 0);
2830 len = gimple_call_arg (stmt, 1);
2831 flag = gimple_call_arg (stmt, 2);
2832 size = gimple_call_arg (stmt, 3);
2833 fmt = gimple_call_arg (stmt, 4);
2834
2835 if (! tree_fits_uhwi_p (size))
2836 return false;
2837
2838 if (! integer_all_onesp (size))
2839 {
dcb7fae2 2840 tree maxlen = get_maxval_strlen (len, 2);
fef5a0d9 2841 if (! tree_fits_uhwi_p (len))
cbdd87d4 2842 {
fef5a0d9
RB
2843 /* If LEN is not constant, try MAXLEN too.
2844 For MAXLEN only allow optimizing into non-_ocs function
2845 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
2846 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
cbdd87d4
RG
2847 return false;
2848 }
2849 else
fef5a0d9 2850 maxlen = len;
cbdd87d4 2851
fef5a0d9
RB
2852 if (tree_int_cst_lt (size, maxlen))
2853 return false;
2854 }
cbdd87d4 2855
fef5a0d9
RB
2856 if (!init_target_chars ())
2857 return false;
cbdd87d4 2858
fef5a0d9
RB
2859 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
2860 or if format doesn't contain % chars or is "%s". */
2861 if (! integer_zerop (flag))
2862 {
2863 fmt_str = c_getstr (fmt);
2864 if (fmt_str == NULL)
2865 return false;
2866 if (strchr (fmt_str, target_percent) != NULL
2867 && strcmp (fmt_str, target_percent_s))
2868 return false;
cbdd87d4
RG
2869 }
2870
fef5a0d9
RB
2871 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
2872 available. */
2873 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
2874 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
2875 if (!fn)
491e0b9b
RG
2876 return false;
2877
fef5a0d9
RB
2878 /* Replace the called function and the first 5 argument by 3 retaining
2879 trailing varargs. */
2880 gimple_call_set_fndecl (stmt, fn);
2881 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2882 gimple_call_set_arg (stmt, 0, dest);
2883 gimple_call_set_arg (stmt, 1, len);
2884 gimple_call_set_arg (stmt, 2, fmt);
2885 for (unsigned i = 3; i < gimple_call_num_args (stmt) - 2; ++i)
2886 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2887 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2888 fold_stmt (gsi);
2889 return true;
2890}
cbdd87d4 2891
fef5a0d9
RB
2892/* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
2893 Return NULL_TREE if a normal call should be emitted rather than
2894 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
2895 or BUILT_IN_VSPRINTF_CHK. */
cbdd87d4 2896
fef5a0d9
RB
2897static bool
2898gimple_fold_builtin_sprintf_chk (gimple_stmt_iterator *gsi,
2899 enum built_in_function fcode)
2900{
538dd0b7 2901 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
fef5a0d9
RB
2902 tree dest, size, len, fn, fmt, flag;
2903 const char *fmt_str;
2904 unsigned nargs = gimple_call_num_args (stmt);
cbdd87d4 2905
fef5a0d9
RB
2906 /* Verify the required arguments in the original call. */
2907 if (nargs < 4)
2908 return false;
2909 dest = gimple_call_arg (stmt, 0);
2910 flag = gimple_call_arg (stmt, 1);
2911 size = gimple_call_arg (stmt, 2);
2912 fmt = gimple_call_arg (stmt, 3);
2913
2914 if (! tree_fits_uhwi_p (size))
2915 return false;
2916
2917 len = NULL_TREE;
2918
2919 if (!init_target_chars ())
2920 return false;
2921
2922 /* Check whether the format is a literal string constant. */
2923 fmt_str = c_getstr (fmt);
2924 if (fmt_str != NULL)
2925 {
2926 /* If the format doesn't contain % args or %%, we know the size. */
2927 if (strchr (fmt_str, target_percent) == 0)
cbdd87d4 2928 {
fef5a0d9
RB
2929 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
2930 len = build_int_cstu (size_type_node, strlen (fmt_str));
2931 }
2932 /* If the format is "%s" and first ... argument is a string literal,
2933 we know the size too. */
2934 else if (fcode == BUILT_IN_SPRINTF_CHK
2935 && strcmp (fmt_str, target_percent_s) == 0)
2936 {
2937 tree arg;
cbdd87d4 2938
fef5a0d9
RB
2939 if (nargs == 5)
2940 {
2941 arg = gimple_call_arg (stmt, 4);
2942 if (POINTER_TYPE_P (TREE_TYPE (arg)))
2943 {
2944 len = c_strlen (arg, 1);
2945 if (! len || ! tree_fits_uhwi_p (len))
2946 len = NULL_TREE;
2947 }
2948 }
2949 }
2950 }
cbdd87d4 2951
fef5a0d9
RB
2952 if (! integer_all_onesp (size))
2953 {
2954 if (! len || ! tree_int_cst_lt (len, size))
2955 return false;
2956 }
cbdd87d4 2957
fef5a0d9
RB
2958 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
2959 or if format doesn't contain % chars or is "%s". */
2960 if (! integer_zerop (flag))
2961 {
2962 if (fmt_str == NULL)
2963 return false;
2964 if (strchr (fmt_str, target_percent) != NULL
2965 && strcmp (fmt_str, target_percent_s))
2966 return false;
2967 }
cbdd87d4 2968
fef5a0d9
RB
2969 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
2970 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
2971 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
2972 if (!fn)
2973 return false;
2974
2975 /* Replace the called function and the first 4 argument by 2 retaining
2976 trailing varargs. */
2977 gimple_call_set_fndecl (stmt, fn);
2978 gimple_call_set_fntype (stmt, TREE_TYPE (fn));
2979 gimple_call_set_arg (stmt, 0, dest);
2980 gimple_call_set_arg (stmt, 1, fmt);
2981 for (unsigned i = 2; i < gimple_call_num_args (stmt) - 2; ++i)
2982 gimple_call_set_arg (stmt, i, gimple_call_arg (stmt, i + 2));
2983 gimple_set_num_ops (stmt, gimple_num_ops (stmt) - 2);
2984 fold_stmt (gsi);
2985 return true;
2986}
2987
35770bb2
RB
2988/* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
2989 ORIG may be null if this is a 2-argument call. We don't attempt to
2990 simplify calls with more than 3 arguments.
2991
a104bd88 2992 Return true if simplification was possible, otherwise false. */
35770bb2 2993
a104bd88 2994bool
dcb7fae2 2995gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
35770bb2 2996{
355fe088 2997 gimple *stmt = gsi_stmt (*gsi);
35770bb2
RB
2998 tree dest = gimple_call_arg (stmt, 0);
2999 tree fmt = gimple_call_arg (stmt, 1);
3000 tree orig = NULL_TREE;
3001 const char *fmt_str = NULL;
3002
3003 /* Verify the required arguments in the original call. We deal with two
3004 types of sprintf() calls: 'sprintf (str, fmt)' and
3005 'sprintf (dest, "%s", orig)'. */
3006 if (gimple_call_num_args (stmt) > 3)
3007 return false;
3008
3009 if (gimple_call_num_args (stmt) == 3)
3010 orig = gimple_call_arg (stmt, 2);
3011
3012 /* Check whether the format is a literal string constant. */
3013 fmt_str = c_getstr (fmt);
3014 if (fmt_str == NULL)
3015 return false;
3016
3017 if (!init_target_chars ())
3018 return false;
3019
3020 /* If the format doesn't contain % args or %%, use strcpy. */
3021 if (strchr (fmt_str, target_percent) == NULL)
3022 {
3023 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3024
3025 if (!fn)
3026 return false;
3027
3028 /* Don't optimize sprintf (buf, "abc", ptr++). */
3029 if (orig)
3030 return false;
3031
3032 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
3033 'format' is known to contain no % formats. */
3034 gimple_seq stmts = NULL;
355fe088 3035 gimple *repl = gimple_build_call (fn, 2, dest, fmt);
35770bb2
RB
3036 gimple_seq_add_stmt_without_update (&stmts, repl);
3037 if (gimple_call_lhs (stmt))
3038 {
3039 repl = gimple_build_assign (gimple_call_lhs (stmt),
3040 build_int_cst (integer_type_node,
3041 strlen (fmt_str)));
3042 gimple_seq_add_stmt_without_update (&stmts, repl);
3043 gsi_replace_with_seq_vops (gsi, stmts);
3044 /* gsi now points at the assignment to the lhs, get a
3045 stmt iterator to the memcpy call.
3046 ??? We can't use gsi_for_stmt as that doesn't work when the
3047 CFG isn't built yet. */
3048 gimple_stmt_iterator gsi2 = *gsi;
3049 gsi_prev (&gsi2);
3050 fold_stmt (&gsi2);
3051 }
3052 else
3053 {
3054 gsi_replace_with_seq_vops (gsi, stmts);
3055 fold_stmt (gsi);
3056 }
3057 return true;
3058 }
3059
3060 /* If the format is "%s", use strcpy if the result isn't used. */
3061 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3062 {
3063 tree fn;
3064 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3065
3066 if (!fn)
3067 return false;
3068
3069 /* Don't crash on sprintf (str1, "%s"). */
3070 if (!orig)
3071 return false;
3072
dcb7fae2
RB
3073 tree orig_len = NULL_TREE;
3074 if (gimple_call_lhs (stmt))
35770bb2 3075 {
dcb7fae2 3076 orig_len = get_maxval_strlen (orig, 0);
d7e78447 3077 if (!orig_len)
35770bb2
RB
3078 return false;
3079 }
3080
3081 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
3082 gimple_seq stmts = NULL;
355fe088 3083 gimple *repl = gimple_build_call (fn, 2, dest, orig);
35770bb2
RB
3084 gimple_seq_add_stmt_without_update (&stmts, repl);
3085 if (gimple_call_lhs (stmt))
3086 {
d7e78447
RB
3087 if (!useless_type_conversion_p (integer_type_node,
3088 TREE_TYPE (orig_len)))
3089 orig_len = fold_convert (integer_type_node, orig_len);
3090 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
35770bb2
RB
3091 gimple_seq_add_stmt_without_update (&stmts, repl);
3092 gsi_replace_with_seq_vops (gsi, stmts);
3093 /* gsi now points at the assignment to the lhs, get a
3094 stmt iterator to the memcpy call.
3095 ??? We can't use gsi_for_stmt as that doesn't work when the
3096 CFG isn't built yet. */
3097 gimple_stmt_iterator gsi2 = *gsi;
3098 gsi_prev (&gsi2);
3099 fold_stmt (&gsi2);
3100 }
3101 else
3102 {
3103 gsi_replace_with_seq_vops (gsi, stmts);
3104 fold_stmt (gsi);
3105 }
3106 return true;
3107 }
3108 return false;
3109}
3110
d7e78447
RB
3111/* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
3112 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
3113 attempt to simplify calls with more than 4 arguments.
35770bb2 3114
a104bd88 3115 Return true if simplification was possible, otherwise false. */
d7e78447 3116
a104bd88 3117bool
dcb7fae2 3118gimple_fold_builtin_snprintf (gimple_stmt_iterator *gsi)
d7e78447 3119{
538dd0b7 3120 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
d7e78447
RB
3121 tree dest = gimple_call_arg (stmt, 0);
3122 tree destsize = gimple_call_arg (stmt, 1);
3123 tree fmt = gimple_call_arg (stmt, 2);
3124 tree orig = NULL_TREE;
3125 const char *fmt_str = NULL;
3126
3127 if (gimple_call_num_args (stmt) > 4)
3128 return false;
3129
3130 if (gimple_call_num_args (stmt) == 4)
3131 orig = gimple_call_arg (stmt, 3);
3132
3133 if (!tree_fits_uhwi_p (destsize))
3134 return false;
3135 unsigned HOST_WIDE_INT destlen = tree_to_uhwi (destsize);
3136
3137 /* Check whether the format is a literal string constant. */
3138 fmt_str = c_getstr (fmt);
3139 if (fmt_str == NULL)
3140 return false;
3141
3142 if (!init_target_chars ())
3143 return false;
3144
3145 /* If the format doesn't contain % args or %%, use strcpy. */
3146 if (strchr (fmt_str, target_percent) == NULL)
3147 {
3148 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3149 if (!fn)
3150 return false;
3151
3152 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
3153 if (orig)
3154 return false;
3155
3156 /* We could expand this as
3157 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
3158 or to
3159 memcpy (str, fmt_with_nul_at_cstm1, cst);
3160 but in the former case that might increase code size
3161 and in the latter case grow .rodata section too much.
3162 So punt for now. */
3163 size_t len = strlen (fmt_str);
3164 if (len >= destlen)
3165 return false;
3166
3167 gimple_seq stmts = NULL;
355fe088 3168 gimple *repl = gimple_build_call (fn, 2, dest, fmt);
d7e78447
RB
3169 gimple_seq_add_stmt_without_update (&stmts, repl);
3170 if (gimple_call_lhs (stmt))
3171 {
3172 repl = gimple_build_assign (gimple_call_lhs (stmt),
3173 build_int_cst (integer_type_node, len));
3174 gimple_seq_add_stmt_without_update (&stmts, repl);
3175 gsi_replace_with_seq_vops (gsi, stmts);
3176 /* gsi now points at the assignment to the lhs, get a
3177 stmt iterator to the memcpy call.
3178 ??? We can't use gsi_for_stmt as that doesn't work when the
3179 CFG isn't built yet. */
3180 gimple_stmt_iterator gsi2 = *gsi;
3181 gsi_prev (&gsi2);
3182 fold_stmt (&gsi2);
3183 }
3184 else
3185 {
3186 gsi_replace_with_seq_vops (gsi, stmts);
3187 fold_stmt (gsi);
3188 }
3189 return true;
3190 }
3191
3192 /* If the format is "%s", use strcpy if the result isn't used. */
3193 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
3194 {
3195 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3196 if (!fn)
3197 return false;
3198
3199 /* Don't crash on snprintf (str1, cst, "%s"). */
3200 if (!orig)
3201 return false;
3202
dcb7fae2 3203 tree orig_len = get_maxval_strlen (orig, 0);
af9db3a7 3204 if (!orig_len || TREE_CODE (orig_len) != INTEGER_CST)
dcb7fae2 3205 return false;
d7e78447
RB
3206
3207 /* We could expand this as
3208 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
3209 or to
3210 memcpy (str1, str2_with_nul_at_cstm1, cst);
3211 but in the former case that might increase code size
3212 and in the latter case grow .rodata section too much.
3213 So punt for now. */
3214 if (compare_tree_int (orig_len, destlen) >= 0)
3215 return false;
3216
3217 /* Convert snprintf (str1, cst, "%s", str2) into
3218 strcpy (str1, str2) if strlen (str2) < cst. */
3219 gimple_seq stmts = NULL;
355fe088 3220 gimple *repl = gimple_build_call (fn, 2, dest, orig);
d7e78447
RB
3221 gimple_seq_add_stmt_without_update (&stmts, repl);
3222 if (gimple_call_lhs (stmt))
3223 {
3224 if (!useless_type_conversion_p (integer_type_node,
3225 TREE_TYPE (orig_len)))
3226 orig_len = fold_convert (integer_type_node, orig_len);
3227 repl = gimple_build_assign (gimple_call_lhs (stmt), orig_len);
3228 gimple_seq_add_stmt_without_update (&stmts, repl);
3229 gsi_replace_with_seq_vops (gsi, stmts);
3230 /* gsi now points at the assignment to the lhs, get a
3231 stmt iterator to the memcpy call.
3232 ??? We can't use gsi_for_stmt as that doesn't work when the
3233 CFG isn't built yet. */
3234 gimple_stmt_iterator gsi2 = *gsi;
3235 gsi_prev (&gsi2);
3236 fold_stmt (&gsi2);
3237 }
3238 else
3239 {
3240 gsi_replace_with_seq_vops (gsi, stmts);
3241 fold_stmt (gsi);
3242 }
3243 return true;
3244 }
3245 return false;
3246}
35770bb2 3247
edd7ae68
RB
3248/* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
3249 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
3250 more than 3 arguments, and ARG may be null in the 2-argument case.
3251
3252 Return NULL_TREE if no simplification was possible, otherwise return the
3253 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3254 code of the function to be simplified. */
3255
3256static bool
3257gimple_fold_builtin_fprintf (gimple_stmt_iterator *gsi,
3258 tree fp, tree fmt, tree arg,
3259 enum built_in_function fcode)
3260{
3261 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3262 tree fn_fputc, fn_fputs;
3263 const char *fmt_str = NULL;
3264
3265 /* If the return value is used, don't do the transformation. */
3266 if (gimple_call_lhs (stmt) != NULL_TREE)
3267 return false;
3268
3269 /* Check whether the format is a literal string constant. */
3270 fmt_str = c_getstr (fmt);
3271 if (fmt_str == NULL)
3272 return false;
3273
3274 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
3275 {
3276 /* If we're using an unlocked function, assume the other
3277 unlocked functions exist explicitly. */
3278 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
3279 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
3280 }
3281 else
3282 {
3283 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
3284 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
3285 }
3286
3287 if (!init_target_chars ())
3288 return false;
3289
3290 /* If the format doesn't contain % args or %%, use strcpy. */
3291 if (strchr (fmt_str, target_percent) == NULL)
3292 {
3293 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
3294 && arg)
3295 return false;
3296
3297 /* If the format specifier was "", fprintf does nothing. */
3298 if (fmt_str[0] == '\0')
3299 {
3300 replace_call_with_value (gsi, NULL_TREE);
3301 return true;
3302 }
3303
3304 /* When "string" doesn't contain %, replace all cases of
3305 fprintf (fp, string) with fputs (string, fp). The fputs
3306 builtin will take care of special cases like length == 1. */
3307 if (fn_fputs)
3308 {
3309 gcall *repl = gimple_build_call (fn_fputs, 2, fmt, fp);
3310 replace_call_with_call_and_fold (gsi, repl);
3311 return true;
3312 }
3313 }
3314
3315 /* The other optimizations can be done only on the non-va_list variants. */
3316 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
3317 return false;
3318
3319 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
3320 else if (strcmp (fmt_str, target_percent_s) == 0)
3321 {
3322 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3323 return false;
3324 if (fn_fputs)
3325 {
3326 gcall *repl = gimple_build_call (fn_fputs, 2, arg, fp);
3327 replace_call_with_call_and_fold (gsi, repl);
3328 return true;
3329 }
3330 }
3331
3332 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
3333 else if (strcmp (fmt_str, target_percent_c) == 0)
3334 {
3335 if (!arg
3336 || ! useless_type_conversion_p (integer_type_node, TREE_TYPE (arg)))
3337 return false;
3338 if (fn_fputc)
3339 {
3340 gcall *repl = gimple_build_call (fn_fputc, 2, arg, fp);
3341 replace_call_with_call_and_fold (gsi, repl);
3342 return true;
3343 }
3344 }
3345
3346 return false;
3347}
3348
ad03a744
RB
3349/* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
3350 FMT and ARG are the arguments to the call; we don't fold cases with
3351 more than 2 arguments, and ARG may be null if this is a 1-argument case.
3352
3353 Return NULL_TREE if no simplification was possible, otherwise return the
3354 simplified form of the call as a tree. FCODE is the BUILT_IN_*
3355 code of the function to be simplified. */
3356
3357static bool
3358gimple_fold_builtin_printf (gimple_stmt_iterator *gsi, tree fmt,
3359 tree arg, enum built_in_function fcode)
3360{
3361 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3362 tree fn_putchar, fn_puts, newarg;
3363 const char *fmt_str = NULL;
3364
3365 /* If the return value is used, don't do the transformation. */
3366 if (gimple_call_lhs (stmt) != NULL_TREE)
3367 return false;
3368
3369 /* Check whether the format is a literal string constant. */
3370 fmt_str = c_getstr (fmt);
3371 if (fmt_str == NULL)
3372 return false;
3373
3374 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
3375 {
3376 /* If we're using an unlocked function, assume the other
3377 unlocked functions exist explicitly. */
3378 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
3379 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
3380 }
3381 else
3382 {
3383 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
3384 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
3385 }
3386
3387 if (!init_target_chars ())
3388 return false;
3389
3390 if (strcmp (fmt_str, target_percent_s) == 0
3391 || strchr (fmt_str, target_percent) == NULL)
3392 {
3393 const char *str;
3394
3395 if (strcmp (fmt_str, target_percent_s) == 0)
3396 {
3397 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3398 return false;
3399
3400 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3401 return false;
3402
3403 str = c_getstr (arg);
3404 if (str == NULL)
3405 return false;
3406 }
3407 else
3408 {
3409 /* The format specifier doesn't contain any '%' characters. */
3410 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
3411 && arg)
3412 return false;
3413 str = fmt_str;
3414 }
3415
3416 /* If the string was "", printf does nothing. */
3417 if (str[0] == '\0')
3418 {
3419 replace_call_with_value (gsi, NULL_TREE);
3420 return true;
3421 }
3422
3423 /* If the string has length of 1, call putchar. */
3424 if (str[1] == '\0')
3425 {
3426 /* Given printf("c"), (where c is any one character,)
3427 convert "c"[0] to an int and pass that to the replacement
3428 function. */
3429 newarg = build_int_cst (integer_type_node, str[0]);
3430 if (fn_putchar)
3431 {
3432 gcall *repl = gimple_build_call (fn_putchar, 1, newarg);
3433 replace_call_with_call_and_fold (gsi, repl);
3434 return true;
3435 }
3436 }
3437 else
3438 {
3439 /* If the string was "string\n", call puts("string"). */
3440 size_t len = strlen (str);
3441 if ((unsigned char)str[len - 1] == target_newline
3442 && (size_t) (int) len == len
3443 && (int) len > 0)
3444 {
3445 char *newstr;
3446 tree offset_node, string_cst;
3447
3448 /* Create a NUL-terminated string that's one char shorter
3449 than the original, stripping off the trailing '\n'. */
3450 newarg = build_string_literal (len, str);
3451 string_cst = string_constant (newarg, &offset_node);
3452 gcc_checking_assert (string_cst
3453 && (TREE_STRING_LENGTH (string_cst)
3454 == (int) len)
3455 && integer_zerop (offset_node)
3456 && (unsigned char)
3457 TREE_STRING_POINTER (string_cst)[len - 1]
3458 == target_newline);
3459 /* build_string_literal creates a new STRING_CST,
3460 modify it in place to avoid double copying. */
3461 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
3462 newstr[len - 1] = '\0';
3463 if (fn_puts)
3464 {
3465 gcall *repl = gimple_build_call (fn_puts, 1, newarg);
3466 replace_call_with_call_and_fold (gsi, repl);
3467 return true;
3468 }
3469 }
3470 else
3471 /* We'd like to arrange to call fputs(string,stdout) here,
3472 but we need stdout and don't have a way to get it yet. */
3473 return false;
3474 }
3475 }
3476
3477 /* The other optimizations can be done only on the non-va_list variants. */
3478 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
3479 return false;
3480
3481 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
3482 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
3483 {
3484 if (!arg || ! POINTER_TYPE_P (TREE_TYPE (arg)))
3485 return false;
3486 if (fn_puts)
3487 {
3488 gcall *repl = gimple_build_call (fn_puts, 1, arg);
3489 replace_call_with_call_and_fold (gsi, repl);
3490 return true;
3491 }
3492 }
3493
3494 /* If the format specifier was "%c", call __builtin_putchar(arg). */
3495 else if (strcmp (fmt_str, target_percent_c) == 0)
3496 {
3497 if (!arg || ! useless_type_conversion_p (integer_type_node,
3498 TREE_TYPE (arg)))
3499 return false;
3500 if (fn_putchar)
3501 {
3502 gcall *repl = gimple_build_call (fn_putchar, 1, arg);
3503 replace_call_with_call_and_fold (gsi, repl);
3504 return true;
3505 }
3506 }
3507
3508 return false;
3509}
3510
edd7ae68 3511
fef5a0d9
RB
3512
3513/* Fold a call to __builtin_strlen with known length LEN. */
3514
3515static bool
dcb7fae2 3516gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi)
fef5a0d9 3517{
355fe088 3518 gimple *stmt = gsi_stmt (*gsi);
c42d0aa0
MS
3519
3520 wide_int minlen;
3521 wide_int maxlen;
3522
3523 tree lenrange[2];
c8602fe6 3524 if (!get_range_strlen (gimple_call_arg (stmt, 0), lenrange, true)
c42d0aa0
MS
3525 && lenrange[0] && TREE_CODE (lenrange[0]) == INTEGER_CST
3526 && lenrange[1] && TREE_CODE (lenrange[1]) == INTEGER_CST)
3527 {
3528 /* The range of lengths refers to either a single constant
3529 string or to the longest and shortest constant string
3530 referenced by the argument of the strlen() call, or to
3531 the strings that can possibly be stored in the arrays
3532 the argument refers to. */
3533 minlen = wi::to_wide (lenrange[0]);
3534 maxlen = wi::to_wide (lenrange[1]);
3535 }
3536 else
3537 {
3538 unsigned prec = TYPE_PRECISION (sizetype);
3539
3540 minlen = wi::shwi (0, prec);
3541 maxlen = wi::to_wide (max_object_size (), prec) - 2;
3542 }
3543
3544 if (minlen == maxlen)
3545 {
3546 lenrange[0] = force_gimple_operand_gsi (gsi, lenrange[0], true, NULL,
3547 true, GSI_SAME_STMT);
3548 replace_call_with_value (gsi, lenrange[0]);
3549 return true;
3550 }
3551
3552 tree lhs = gimple_call_lhs (stmt);
3553 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3554 set_range_info (lhs, VR_RANGE, minlen, maxlen);
3555
3556 return false;
cbdd87d4
RG
3557}
3558
48126138
NS
3559/* Fold a call to __builtin_acc_on_device. */
3560
3561static bool
3562gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
3563{
3564 /* Defer folding until we know which compiler we're in. */
3565 if (symtab->state != EXPANSION)
3566 return false;
3567
3568 unsigned val_host = GOMP_DEVICE_HOST;
3569 unsigned val_dev = GOMP_DEVICE_NONE;
3570
3571#ifdef ACCEL_COMPILER
3572 val_host = GOMP_DEVICE_NOT_HOST;
3573 val_dev = ACCEL_COMPILER_acc_device;
3574#endif
3575
3576 location_t loc = gimple_location (gsi_stmt (*gsi));
3577
3578 tree host_eq = make_ssa_name (boolean_type_node);
3579 gimple *host_ass = gimple_build_assign
3580 (host_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_host));
3581 gimple_set_location (host_ass, loc);
3582 gsi_insert_before (gsi, host_ass, GSI_SAME_STMT);
3583
3584 tree dev_eq = make_ssa_name (boolean_type_node);
3585 gimple *dev_ass = gimple_build_assign
3586 (dev_eq, EQ_EXPR, arg0, build_int_cst (TREE_TYPE (arg0), val_dev));
3587 gimple_set_location (dev_ass, loc);
3588 gsi_insert_before (gsi, dev_ass, GSI_SAME_STMT);
3589
3590 tree result = make_ssa_name (boolean_type_node);
3591 gimple *result_ass = gimple_build_assign
3592 (result, BIT_IOR_EXPR, host_eq, dev_eq);
3593 gimple_set_location (result_ass, loc);
3594 gsi_insert_before (gsi, result_ass, GSI_SAME_STMT);
3595
3596 replace_call_with_value (gsi, result);
3597
3598 return true;
3599}
cbdd87d4 3600
fe75f732
PK
3601/* Fold realloc (0, n) -> malloc (n). */
3602
3603static bool
3604gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
3605{
3606 gimple *stmt = gsi_stmt (*gsi);
3607 tree arg = gimple_call_arg (stmt, 0);
3608 tree size = gimple_call_arg (stmt, 1);
3609
3610 if (operand_equal_p (arg, null_pointer_node, 0))
3611 {
3612 tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
3613 if (fn_malloc)
3614 {
3615 gcall *repl = gimple_build_call (fn_malloc, 1, size);
3616 replace_call_with_call_and_fold (gsi, repl);
3617 return true;
3618 }
3619 }
3620 return false;
3621}
3622
dcb7fae2
RB
3623/* Fold the non-target builtin at *GSI and return whether any simplification
3624 was made. */
cbdd87d4 3625
fef5a0d9 3626static bool
dcb7fae2 3627gimple_fold_builtin (gimple_stmt_iterator *gsi)
cbdd87d4 3628{
538dd0b7 3629 gcall *stmt = as_a <gcall *>(gsi_stmt (*gsi));
fef5a0d9 3630 tree callee = gimple_call_fndecl (stmt);
cbdd87d4 3631
dcb7fae2
RB
3632 /* Give up for always_inline inline builtins until they are
3633 inlined. */
3634 if (avoid_folding_inline_builtin (callee))
3635 return false;
cbdd87d4 3636
edd7ae68
RB
3637 unsigned n = gimple_call_num_args (stmt);
3638 enum built_in_function fcode = DECL_FUNCTION_CODE (callee);
3639 switch (fcode)
cbdd87d4 3640 {
b3d8d88e
MS
3641 case BUILT_IN_BCMP:
3642 return gimple_fold_builtin_bcmp (gsi);
3643 case BUILT_IN_BCOPY:
3644 return gimple_fold_builtin_bcopy (gsi);
dcb7fae2 3645 case BUILT_IN_BZERO:
b3d8d88e
MS
3646 return gimple_fold_builtin_bzero (gsi);
3647
dcb7fae2
RB
3648 case BUILT_IN_MEMSET:
3649 return gimple_fold_builtin_memset (gsi,
3650 gimple_call_arg (stmt, 1),
3651 gimple_call_arg (stmt, 2));
dcb7fae2
RB
3652 case BUILT_IN_MEMCPY:
3653 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3654 gimple_call_arg (stmt, 1), 0);
3655 case BUILT_IN_MEMPCPY:
3656 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3657 gimple_call_arg (stmt, 1), 1);
3658 case BUILT_IN_MEMMOVE:
3659 return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0),
3660 gimple_call_arg (stmt, 1), 3);
3661 case BUILT_IN_SPRINTF_CHK:
3662 case BUILT_IN_VSPRINTF_CHK:
edd7ae68 3663 return gimple_fold_builtin_sprintf_chk (gsi, fcode);
dcb7fae2
RB
3664 case BUILT_IN_STRCAT_CHK:
3665 return gimple_fold_builtin_strcat_chk (gsi);
745583f9
RB
3666 case BUILT_IN_STRNCAT_CHK:
3667 return gimple_fold_builtin_strncat_chk (gsi);
cbdd87d4 3668 case BUILT_IN_STRLEN:
dcb7fae2 3669 return gimple_fold_builtin_strlen (gsi);
cbdd87d4 3670 case BUILT_IN_STRCPY:
dcb7fae2 3671 return gimple_fold_builtin_strcpy (gsi,
fef5a0d9 3672 gimple_call_arg (stmt, 0),
dcb7fae2 3673 gimple_call_arg (stmt, 1));
cbdd87d4 3674 case BUILT_IN_STRNCPY:
dcb7fae2 3675 return gimple_fold_builtin_strncpy (gsi,
fef5a0d9
RB
3676 gimple_call_arg (stmt, 0),
3677 gimple_call_arg (stmt, 1),
dcb7fae2 3678 gimple_call_arg (stmt, 2));
9a7eefec 3679 case BUILT_IN_STRCAT:
dcb7fae2
RB
3680 return gimple_fold_builtin_strcat (gsi, gimple_call_arg (stmt, 0),
3681 gimple_call_arg (stmt, 1));
ad03a744
RB
3682 case BUILT_IN_STRNCAT:
3683 return gimple_fold_builtin_strncat (gsi);
71dea1dd 3684 case BUILT_IN_INDEX:
912d9ec3 3685 case BUILT_IN_STRCHR:
71dea1dd
WD
3686 return gimple_fold_builtin_strchr (gsi, false);
3687 case BUILT_IN_RINDEX:
3688 case BUILT_IN_STRRCHR:
3689 return gimple_fold_builtin_strchr (gsi, true);
c8952930
JJ
3690 case BUILT_IN_STRSTR:
3691 return gimple_fold_builtin_strstr (gsi);
a918bfbf 3692 case BUILT_IN_STRCMP:
8b0b334a 3693 case BUILT_IN_STRCMP_EQ:
a918bfbf
ML
3694 case BUILT_IN_STRCASECMP:
3695 case BUILT_IN_STRNCMP:
8b0b334a 3696 case BUILT_IN_STRNCMP_EQ:
a918bfbf
ML
3697 case BUILT_IN_STRNCASECMP:
3698 return gimple_fold_builtin_string_compare (gsi);
488c6247
ML
3699 case BUILT_IN_MEMCHR:
3700 return gimple_fold_builtin_memchr (gsi);
cbdd87d4 3701 case BUILT_IN_FPUTS:
dcb7fae2
RB
3702 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
3703 gimple_call_arg (stmt, 1), false);
cbdd87d4 3704 case BUILT_IN_FPUTS_UNLOCKED:
dcb7fae2
RB
3705 return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
3706 gimple_call_arg (stmt, 1), true);
cbdd87d4
RG
3707 case BUILT_IN_MEMCPY_CHK:
3708 case BUILT_IN_MEMPCPY_CHK:
3709 case BUILT_IN_MEMMOVE_CHK:
3710 case BUILT_IN_MEMSET_CHK:
dcb7fae2 3711 return gimple_fold_builtin_memory_chk (gsi,
fef5a0d9
RB
3712 gimple_call_arg (stmt, 0),
3713 gimple_call_arg (stmt, 1),
3714 gimple_call_arg (stmt, 2),
3715 gimple_call_arg (stmt, 3),
edd7ae68 3716 fcode);
2625bb5d
RB
3717 case BUILT_IN_STPCPY:
3718 return gimple_fold_builtin_stpcpy (gsi);
cbdd87d4
RG
3719 case BUILT_IN_STRCPY_CHK:
3720 case BUILT_IN_STPCPY_CHK:
dcb7fae2 3721 return gimple_fold_builtin_stxcpy_chk (gsi,
fef5a0d9
RB
3722 gimple_call_arg (stmt, 0),
3723 gimple_call_arg (stmt, 1),
3724 gimple_call_arg (stmt, 2),
edd7ae68 3725 fcode);
cbdd87d4 3726 case BUILT_IN_STRNCPY_CHK:
f3fc9b80 3727 case BUILT_IN_STPNCPY_CHK:
fef5a0d9
RB
3728 return gimple_fold_builtin_stxncpy_chk (gsi,
3729 gimple_call_arg (stmt, 0),
3730 gimple_call_arg (stmt, 1),
3731 gimple_call_arg (stmt, 2),
3732 gimple_call_arg (stmt, 3),
edd7ae68 3733 fcode);
cbdd87d4
RG
3734 case BUILT_IN_SNPRINTF_CHK:
3735 case BUILT_IN_VSNPRINTF_CHK:
edd7ae68 3736 return gimple_fold_builtin_snprintf_chk (gsi, fcode);
a104bd88 3737
edd7ae68
RB
3738 case BUILT_IN_FPRINTF:
3739 case BUILT_IN_FPRINTF_UNLOCKED:
3740 case BUILT_IN_VFPRINTF:
3741 if (n == 2 || n == 3)
3742 return gimple_fold_builtin_fprintf (gsi,
3743 gimple_call_arg (stmt, 0),
3744 gimple_call_arg (stmt, 1),
3745 n == 3
3746 ? gimple_call_arg (stmt, 2)
3747 : NULL_TREE,
3748 fcode);
3749 break;
3750 case BUILT_IN_FPRINTF_CHK:
3751 case BUILT_IN_VFPRINTF_CHK:
3752 if (n == 3 || n == 4)
3753 return gimple_fold_builtin_fprintf (gsi,
3754 gimple_call_arg (stmt, 0),
3755 gimple_call_arg (stmt, 2),
3756 n == 4
3757 ? gimple_call_arg (stmt, 3)
3758 : NULL_TREE,
3759 fcode);
3760 break;
ad03a744
RB
3761 case BUILT_IN_PRINTF:
3762 case BUILT_IN_PRINTF_UNLOCKED:
3763 case BUILT_IN_VPRINTF:
3764 if (n == 1 || n == 2)
3765 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 0),
3766 n == 2
3767 ? gimple_call_arg (stmt, 1)
3768 : NULL_TREE, fcode);
3769 break;
3770 case BUILT_IN_PRINTF_CHK:
3771 case BUILT_IN_VPRINTF_CHK:
3772 if (n == 2 || n == 3)
3773 return gimple_fold_builtin_printf (gsi, gimple_call_arg (stmt, 1),
3774 n == 3
3775 ? gimple_call_arg (stmt, 2)
3776 : NULL_TREE, fcode);
242a37f1 3777 break;
48126138
NS
3778 case BUILT_IN_ACC_ON_DEVICE:
3779 return gimple_fold_builtin_acc_on_device (gsi,
3780 gimple_call_arg (stmt, 0));
fe75f732
PK
3781 case BUILT_IN_REALLOC:
3782 return gimple_fold_builtin_realloc (gsi);
3783
fef5a0d9
RB
3784 default:;
3785 }
3786
3787 /* Try the generic builtin folder. */
3788 bool ignore = (gimple_call_lhs (stmt) == NULL);
3789 tree result = fold_call_stmt (stmt, ignore);
3790 if (result)
3791 {
3792 if (ignore)
3793 STRIP_NOPS (result);
3794 else
3795 result = fold_convert (gimple_call_return_type (stmt), result);
3796 if (!update_call_from_tree (gsi, result))
3797 gimplify_and_update_call_from_tree (gsi, result);
3798 return true;
3799 }
3800
3801 return false;
3802}
3803
451e8dae
NS
3804/* Transform IFN_GOACC_DIM_SIZE and IFN_GOACC_DIM_POS internal
3805 function calls to constants, where possible. */
3806
3807static tree
3808fold_internal_goacc_dim (const gimple *call)
3809{
629b3d75
MJ
3810 int axis = oacc_get_ifn_dim_arg (call);
3811 int size = oacc_get_fn_dim_size (current_function_decl, axis);
451e8dae 3812 tree result = NULL_TREE;
67d2229e 3813 tree type = TREE_TYPE (gimple_call_lhs (call));
451e8dae 3814
67d2229e 3815 switch (gimple_call_internal_fn (call))
451e8dae 3816 {
67d2229e
TV
3817 case IFN_GOACC_DIM_POS:
3818 /* If the size is 1, we know the answer. */
3819 if (size == 1)
3820 result = build_int_cst (type, 0);
3821 break;
3822 case IFN_GOACC_DIM_SIZE:
3823 /* If the size is not dynamic, we know the answer. */
3824 if (size)
3825 result = build_int_cst (type, size);
3826 break;
3827 default:
3828 break;
451e8dae
NS
3829 }
3830
3831 return result;
3832}
3833
849a76a5
JJ
3834/* Return true if stmt is __atomic_compare_exchange_N call which is suitable
3835 for conversion into ATOMIC_COMPARE_EXCHANGE if the second argument is
3836 &var where var is only addressable because of such calls. */
3837
3838bool
3839optimize_atomic_compare_exchange_p (gimple *stmt)
3840{
3841 if (gimple_call_num_args (stmt) != 6
3842 || !flag_inline_atomics
3843 || !optimize
45b2222a 3844 || sanitize_flags_p (SANITIZE_THREAD | SANITIZE_ADDRESS)
849a76a5
JJ
3845 || !gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
3846 || !gimple_vdef (stmt)
3847 || !gimple_vuse (stmt))
3848 return false;
3849
3850 tree fndecl = gimple_call_fndecl (stmt);
3851 switch (DECL_FUNCTION_CODE (fndecl))
3852 {
3853 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
3854 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
3855 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
3856 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
3857 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
3858 break;
3859 default:
3860 return false;
3861 }
3862
3863 tree expected = gimple_call_arg (stmt, 1);
3864 if (TREE_CODE (expected) != ADDR_EXPR
1de3c940
JJ
3865 || !SSA_VAR_P (TREE_OPERAND (expected, 0)))
3866 return false;
3867
3868 tree etype = TREE_TYPE (TREE_OPERAND (expected, 0));
3869 if (!is_gimple_reg_type (etype)
849a76a5 3870 || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl)
1de3c940
JJ
3871 || TREE_THIS_VOLATILE (etype)
3872 || VECTOR_TYPE_P (etype)
3873 || TREE_CODE (etype) == COMPLEX_TYPE
3874 /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs
3875 might not preserve all the bits. See PR71716. */
3876 || SCALAR_FLOAT_TYPE_P (etype)
73a699ae
RS
3877 || maybe_ne (TYPE_PRECISION (etype),
3878 GET_MODE_BITSIZE (TYPE_MODE (etype))))
849a76a5
JJ
3879 return false;
3880
3881 tree weak = gimple_call_arg (stmt, 3);
3882 if (!integer_zerop (weak) && !integer_onep (weak))
3883 return false;
3884
3885 tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3886 tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
3887 machine_mode mode = TYPE_MODE (itype);
3888
3889 if (direct_optab_handler (atomic_compare_and_swap_optab, mode)
3890 == CODE_FOR_nothing
3891 && optab_handler (sync_compare_and_swap_optab, mode) == CODE_FOR_nothing)
3892 return false;
3893
cf098191 3894 if (maybe_ne (int_size_in_bytes (etype), GET_MODE_SIZE (mode)))
849a76a5
JJ
3895 return false;
3896
3897 return true;
3898}
3899
3900/* Fold
3901 r = __atomic_compare_exchange_N (p, &e, d, w, s, f);
3902 into
3903 _Complex uintN_t t = ATOMIC_COMPARE_EXCHANGE (p, e, d, w * 256 + N, s, f);
3904 i = IMAGPART_EXPR <t>;
3905 r = (_Bool) i;
3906 e = REALPART_EXPR <t>; */
3907
3908void
3909fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi)
3910{
3911 gimple *stmt = gsi_stmt (*gsi);
3912 tree fndecl = gimple_call_fndecl (stmt);
3913 tree parmt = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3914 tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt)));
3915 tree ctype = build_complex_type (itype);
3916 tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
cc195d46
JJ
3917 bool throws = false;
3918 edge e = NULL;
849a76a5
JJ
3919 gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
3920 expected);
3921 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3922 gimple_stmt_iterator gsiret = gsi_for_stmt (g);
3923 if (!useless_type_conversion_p (itype, TREE_TYPE (expected)))
3924 {
3925 g = gimple_build_assign (make_ssa_name (itype), VIEW_CONVERT_EXPR,
3926 build1 (VIEW_CONVERT_EXPR, itype,
3927 gimple_assign_lhs (g)));
3928 gsi_insert_before (gsi, g, GSI_SAME_STMT);
3929 }
3930 int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0)
3931 + int_size_in_bytes (itype);
3932 g = gimple_build_call_internal (IFN_ATOMIC_COMPARE_EXCHANGE, 6,
3933 gimple_call_arg (stmt, 0),
3934 gimple_assign_lhs (g),
3935 gimple_call_arg (stmt, 2),
3936 build_int_cst (integer_type_node, flag),
3937 gimple_call_arg (stmt, 4),
3938 gimple_call_arg (stmt, 5));
3939 tree lhs = make_ssa_name (ctype);
3940 gimple_call_set_lhs (g, lhs);
3941 gimple_set_vdef (g, gimple_vdef (stmt));
3942 gimple_set_vuse (g, gimple_vuse (stmt));
3943 SSA_NAME_DEF_STMT (gimple_vdef (g)) = g;
cc195d46
JJ
3944 tree oldlhs = gimple_call_lhs (stmt);
3945 if (stmt_can_throw_internal (stmt))
3946 {
3947 throws = true;
3948 e = find_fallthru_edge (gsi_bb (*gsi)->succs);
3949 }
3950 gimple_call_set_nothrow (as_a <gcall *> (g),
3951 gimple_call_nothrow_p (as_a <gcall *> (stmt)));
3952 gimple_call_set_lhs (stmt, NULL_TREE);
3953 gsi_replace (gsi, g, true);
3954 if (oldlhs)
849a76a5 3955 {
849a76a5
JJ
3956 g = gimple_build_assign (make_ssa_name (itype), IMAGPART_EXPR,
3957 build1 (IMAGPART_EXPR, itype, lhs));
cc195d46
JJ
3958 if (throws)
3959 {
3960 gsi_insert_on_edge_immediate (e, g);
3961 *gsi = gsi_for_stmt (g);
3962 }
3963 else
3964 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3965 g = gimple_build_assign (oldlhs, NOP_EXPR, gimple_assign_lhs (g));
3966 gsi_insert_after (gsi, g, GSI_NEW_STMT);
849a76a5 3967 }
849a76a5
JJ
3968 g = gimple_build_assign (make_ssa_name (itype), REALPART_EXPR,
3969 build1 (REALPART_EXPR, itype, lhs));
cc195d46
JJ
3970 if (throws && oldlhs == NULL_TREE)
3971 {
3972 gsi_insert_on_edge_immediate (e, g);
3973 *gsi = gsi_for_stmt (g);
3974 }
3975 else
3976 gsi_insert_after (gsi, g, GSI_NEW_STMT);
849a76a5
JJ
3977 if (!useless_type_conversion_p (TREE_TYPE (expected), itype))
3978 {
3979 g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)),
3980 VIEW_CONVERT_EXPR,
3981 build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),
3982 gimple_assign_lhs (g)));
3983 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3984 }
3985 g = gimple_build_assign (expected, SSA_NAME, gimple_assign_lhs (g));
3986 gsi_insert_after (gsi, g, GSI_NEW_STMT);
3987 *gsi = gsiret;
3988}
3989
1304953e
JJ
3990/* Return true if ARG0 CODE ARG1 in infinite signed precision operation
3991 doesn't fit into TYPE. The test for overflow should be regardless of
3992 -fwrapv, and even for unsigned types. */
3993
3994bool
3995arith_overflowed_p (enum tree_code code, const_tree type,
3996 const_tree arg0, const_tree arg1)
3997{
3998 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) widest2_int;
3999 typedef generic_wide_int <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> >
4000 widest2_int_cst;
4001 widest2_int warg0 = widest2_int_cst (arg0);
4002 widest2_int warg1 = widest2_int_cst (arg1);
4003 widest2_int wres;
4004 switch (code)
4005 {
4006 case PLUS_EXPR: wres = wi::add (warg0, warg1); break;
4007 case MINUS_EXPR: wres = wi::sub (warg0, warg1); break;
4008 case MULT_EXPR: wres = wi::mul (warg0, warg1); break;
4009 default: gcc_unreachable ();
4010 }
4011 signop sign = TYPE_SIGN (type);
4012 if (sign == UNSIGNED && wi::neg_p (wres))
4013 return true;
4014 return wi::min_precision (wres, sign) > TYPE_PRECISION (type);
4015}
4016
cbdd87d4
RG
4017/* Attempt to fold a call statement referenced by the statement iterator GSI.
4018 The statement may be replaced by another statement, e.g., if the call
4019 simplifies to a constant value. Return true if any changes were made.
4020 It is assumed that the operands have been previously folded. */
4021
e021c122 4022static bool
ceeffab0 4023gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace)
cbdd87d4 4024{
538dd0b7 4025 gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi));
3b45a007 4026 tree callee;
e021c122
RG
4027 bool changed = false;
4028 unsigned i;
cbdd87d4 4029
e021c122
RG
4030 /* Fold *& in call arguments. */
4031 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4032 if (REFERENCE_CLASS_P (gimple_call_arg (stmt, i)))
4033 {
4034 tree tmp = maybe_fold_reference (gimple_call_arg (stmt, i), false);
4035 if (tmp)
4036 {
4037 gimple_call_set_arg (stmt, i, tmp);
4038 changed = true;
4039 }
4040 }
3b45a007
RG
4041
4042 /* Check for virtual calls that became direct calls. */
4043 callee = gimple_call_fn (stmt);
25583c4f 4044 if (callee && TREE_CODE (callee) == OBJ_TYPE_REF)
3b45a007 4045 {
49c471e3
MJ
4046 if (gimple_call_addr_fndecl (OBJ_TYPE_REF_EXPR (callee)) != NULL_TREE)
4047 {
450ad0cd
JH
4048 if (dump_file && virtual_method_call_p (callee)
4049 && !possible_polymorphic_call_target_p
6f8091fc
JH
4050 (callee, stmt, cgraph_node::get (gimple_call_addr_fndecl
4051 (OBJ_TYPE_REF_EXPR (callee)))))
450ad0cd
JH
4052 {
4053 fprintf (dump_file,
a70e9985 4054 "Type inheritance inconsistent devirtualization of ");
450ad0cd
JH
4055 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
4056 fprintf (dump_file, " to ");
4057 print_generic_expr (dump_file, callee, TDF_SLIM);
4058 fprintf (dump_file, "\n");
4059 }
4060
49c471e3 4061 gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
e021c122
RG
4062 changed = true;
4063 }
a70e9985 4064 else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
e021c122 4065 {
61dd6a2e
JH
4066 bool final;
4067 vec <cgraph_node *>targets
058d0a90 4068 = possible_polymorphic_call_targets (callee, stmt, &final);
2b5f0895 4069 if (final && targets.length () <= 1 && dbg_cnt (devirt))
e021c122 4070 {
a70e9985 4071 tree lhs = gimple_call_lhs (stmt);
2b5f0895
XDL
4072 if (dump_enabled_p ())
4073 {
807b7d62 4074 location_t loc = gimple_location_safe (stmt);
2b5f0895
XDL
4075 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
4076 "folding virtual function call to %s\n",
4077 targets.length () == 1
4078 ? targets[0]->name ()
4079 : "__builtin_unreachable");
4080 }
61dd6a2e 4081 if (targets.length () == 1)
cf3e5a89 4082 {
18954840
JJ
4083 tree fndecl = targets[0]->decl;
4084 gimple_call_set_fndecl (stmt, fndecl);
cf3e5a89 4085 changed = true;
18954840
JJ
4086 /* If changing the call to __cxa_pure_virtual
4087 or similar noreturn function, adjust gimple_call_fntype
4088 too. */
865f7046 4089 if (gimple_call_noreturn_p (stmt)
18954840
JJ
4090 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
4091 && TYPE_ARG_TYPES (TREE_TYPE (fndecl))
4092 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
4093 == void_type_node))
4094 gimple_call_set_fntype (stmt, TREE_TYPE (fndecl));
a70e9985 4095 /* If the call becomes noreturn, remove the lhs. */
3cee7e4e
MP
4096 if (lhs
4097 && gimple_call_noreturn_p (stmt)
18954840 4098 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (stmt)))
abd3a68c 4099 || should_remove_lhs_p (lhs)))
a70e9985
JJ
4100 {
4101 if (TREE_CODE (lhs) == SSA_NAME)
4102 {
b731b390 4103 tree var = create_tmp_var (TREE_TYPE (lhs));
a70e9985 4104 tree def = get_or_create_ssa_default_def (cfun, var);
355fe088 4105 gimple *new_stmt = gimple_build_assign (lhs, def);
a70e9985
JJ
4106 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4107 }
4108 gimple_call_set_lhs (stmt, NULL_TREE);
4109 }
0b986c6a 4110 maybe_remove_unused_call_args (cfun, stmt);
cf3e5a89 4111 }
a70e9985 4112 else
cf3e5a89
JJ
4113 {
4114 tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
355fe088 4115 gimple *new_stmt = gimple_build_call (fndecl, 0);
cf3e5a89 4116 gimple_set_location (new_stmt, gimple_location (stmt));
2da6996c
RB
4117 /* If the call had a SSA name as lhs morph that into
4118 an uninitialized value. */
a70e9985
JJ
4119 if (lhs && TREE_CODE (lhs) == SSA_NAME)
4120 {
b731b390 4121 tree var = create_tmp_var (TREE_TYPE (lhs));
2da6996c
RB
4122 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
4123 SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
4124 set_ssa_default_def (cfun, var, lhs);
42e52a51 4125 }
2da6996c
RB
4126 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
4127 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
4128 gsi_replace (gsi, new_stmt, false);
cf3e5a89
JJ
4129 return true;
4130 }
e021c122 4131 }
49c471e3 4132 }
e021c122 4133 }
49c471e3 4134
f2d3d07e
RH
4135 /* Check for indirect calls that became direct calls, and then
4136 no longer require a static chain. */
4137 if (gimple_call_chain (stmt))
4138 {
4139 tree fn = gimple_call_fndecl (stmt);
4140 if (fn && !DECL_STATIC_CHAIN (fn))
4141 {
4142 gimple_call_set_chain (stmt, NULL);
4143 changed = true;
4144 }
4145 else
4146 {
4147 tree tmp = maybe_fold_reference (gimple_call_chain (stmt), false);
4148 if (tmp)
4149 {
4150 gimple_call_set_chain (stmt, tmp);
4151 changed = true;
4152 }
4153 }
4154 }
4155
e021c122
RG
4156 if (inplace)
4157 return changed;
4158
4159 /* Check for builtins that CCP can handle using information not
4160 available in the generic fold routines. */
fef5a0d9
RB
4161 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
4162 {
4163 if (gimple_fold_builtin (gsi))
4164 changed = true;
4165 }
4166 else if (gimple_call_builtin_p (stmt, BUILT_IN_MD))
e021c122 4167 {
ea679d55 4168 changed |= targetm.gimple_fold_builtin (gsi);
3b45a007 4169 }
368b454d 4170 else if (gimple_call_internal_p (stmt))
ed9c79e1 4171 {
368b454d
JJ
4172 enum tree_code subcode = ERROR_MARK;
4173 tree result = NULL_TREE;
1304953e
JJ
4174 bool cplx_result = false;
4175 tree overflow = NULL_TREE;
368b454d
JJ
4176 switch (gimple_call_internal_fn (stmt))
4177 {
4178 case IFN_BUILTIN_EXPECT:
4179 result = fold_builtin_expect (gimple_location (stmt),
4180 gimple_call_arg (stmt, 0),
4181 gimple_call_arg (stmt, 1),
4182 gimple_call_arg (stmt, 2));
4183 break;
0e82f089 4184 case IFN_UBSAN_OBJECT_SIZE:
ca1150f0
JJ
4185 {
4186 tree offset = gimple_call_arg (stmt, 1);
4187 tree objsize = gimple_call_arg (stmt, 2);
4188 if (integer_all_onesp (objsize)
4189 || (TREE_CODE (offset) == INTEGER_CST
4190 && TREE_CODE (objsize) == INTEGER_CST
4191 && tree_int_cst_le (offset, objsize)))
4192 {
4193 replace_call_with_value (gsi, NULL_TREE);
4194 return true;
4195 }
4196 }
4197 break;
4198 case IFN_UBSAN_PTR:
4199 if (integer_zerop (gimple_call_arg (stmt, 1)))
0e82f089 4200 {
ca1150f0 4201 replace_call_with_value (gsi, NULL_TREE);
0e82f089
MP
4202 return true;
4203 }
4204 break;
ca1150f0
JJ
4205 case IFN_UBSAN_BOUNDS:
4206 {
4207 tree index = gimple_call_arg (stmt, 1);
4208 tree bound = gimple_call_arg (stmt, 2);
4209 if (TREE_CODE (index) == INTEGER_CST
4210 && TREE_CODE (bound) == INTEGER_CST)
4211 {
4212 index = fold_convert (TREE_TYPE (bound), index);
4213 if (TREE_CODE (index) == INTEGER_CST
4214 && tree_int_cst_le (index, bound))
4215 {
4216 replace_call_with_value (gsi, NULL_TREE);
4217 return true;
4218 }
4219 }
4220 }
4221 break;
451e8dae
NS
4222 case IFN_GOACC_DIM_SIZE:
4223 case IFN_GOACC_DIM_POS:
4224 result = fold_internal_goacc_dim (stmt);
4225 break;
368b454d
JJ
4226 case IFN_UBSAN_CHECK_ADD:
4227 subcode = PLUS_EXPR;
4228 break;
4229 case IFN_UBSAN_CHECK_SUB:
4230 subcode = MINUS_EXPR;
4231 break;
4232 case IFN_UBSAN_CHECK_MUL:
4233 subcode = MULT_EXPR;
4234 break;
1304953e
JJ
4235 case IFN_ADD_OVERFLOW:
4236 subcode = PLUS_EXPR;
4237 cplx_result = true;
4238 break;
4239 case IFN_SUB_OVERFLOW:
4240 subcode = MINUS_EXPR;
4241 cplx_result = true;
4242 break;
4243 case IFN_MUL_OVERFLOW:
4244 subcode = MULT_EXPR;
4245 cplx_result = true;
4246 break;
368b454d
JJ
4247 default:
4248 break;
4249 }
4250 if (subcode != ERROR_MARK)
4251 {
4252 tree arg0 = gimple_call_arg (stmt, 0);
4253 tree arg1 = gimple_call_arg (stmt, 1);
1304953e
JJ
4254 tree type = TREE_TYPE (arg0);
4255 if (cplx_result)
4256 {
4257 tree lhs = gimple_call_lhs (stmt);
4258 if (lhs == NULL_TREE)
4259 type = NULL_TREE;
4260 else
4261 type = TREE_TYPE (TREE_TYPE (lhs));
4262 }
4263 if (type == NULL_TREE)
4264 ;
368b454d 4265 /* x = y + 0; x = y - 0; x = y * 0; */
1304953e
JJ
4266 else if (integer_zerop (arg1))
4267 result = subcode == MULT_EXPR ? integer_zero_node : arg0;
368b454d
JJ
4268 /* x = 0 + y; x = 0 * y; */
4269 else if (subcode != MINUS_EXPR && integer_zerop (arg0))
1304953e 4270 result = subcode == MULT_EXPR ? integer_zero_node : arg1;
368b454d
JJ
4271 /* x = y - y; */
4272 else if (subcode == MINUS_EXPR && operand_equal_p (arg0, arg1, 0))
1304953e 4273 result = integer_zero_node;
368b454d 4274 /* x = y * 1; x = 1 * y; */
1304953e
JJ
4275 else if (subcode == MULT_EXPR && integer_onep (arg1))
4276 result = arg0;
4277 else if (subcode == MULT_EXPR && integer_onep (arg0))
4278 result = arg1;
4279 else if (TREE_CODE (arg0) == INTEGER_CST
4280 && TREE_CODE (arg1) == INTEGER_CST)
368b454d 4281 {
1304953e
JJ
4282 if (cplx_result)
4283 result = int_const_binop (subcode, fold_convert (type, arg0),
4284 fold_convert (type, arg1));
4285 else
4286 result = int_const_binop (subcode, arg0, arg1);
4287 if (result && arith_overflowed_p (subcode, type, arg0, arg1))
4288 {
4289 if (cplx_result)
4290 overflow = build_one_cst (type);
4291 else
4292 result = NULL_TREE;
4293 }
4294 }
4295 if (result)
4296 {
4297 if (result == integer_zero_node)
4298 result = build_zero_cst (type);
4299 else if (cplx_result && TREE_TYPE (result) != type)
4300 {
4301 if (TREE_CODE (result) == INTEGER_CST)
4302 {
4303 if (arith_overflowed_p (PLUS_EXPR, type, result,
4304 integer_zero_node))
4305 overflow = build_one_cst (type);
4306 }
4307 else if ((!TYPE_UNSIGNED (TREE_TYPE (result))
4308 && TYPE_UNSIGNED (type))
4309 || (TYPE_PRECISION (type)
4310 < (TYPE_PRECISION (TREE_TYPE (result))
4311 + (TYPE_UNSIGNED (TREE_TYPE (result))
4312 && !TYPE_UNSIGNED (type)))))
4313 result = NULL_TREE;
4314 if (result)
4315 result = fold_convert (type, result);
4316 }
368b454d
JJ
4317 }
4318 }
1304953e 4319
ed9c79e1
JJ
4320 if (result)
4321 {
1304953e
JJ
4322 if (TREE_CODE (result) == INTEGER_CST && TREE_OVERFLOW (result))
4323 result = drop_tree_overflow (result);
4324 if (cplx_result)
4325 {
4326 if (overflow == NULL_TREE)
4327 overflow = build_zero_cst (TREE_TYPE (result));
4328 tree ctype = build_complex_type (TREE_TYPE (result));
4329 if (TREE_CODE (result) == INTEGER_CST
4330 && TREE_CODE (overflow) == INTEGER_CST)
4331 result = build_complex (ctype, result, overflow);
4332 else
4333 result = build2_loc (gimple_location (stmt), COMPLEX_EXPR,
4334 ctype, result, overflow);
4335 }
ed9c79e1
JJ
4336 if (!update_call_from_tree (gsi, result))
4337 gimplify_and_update_call_from_tree (gsi, result);
4338 changed = true;
4339 }
4340 }
3b45a007 4341
e021c122 4342 return changed;
cbdd87d4
RG
4343}
4344
e0ee10ed 4345
89a79e96
RB
4346/* Return true whether NAME has a use on STMT. */
4347
4348static bool
355fe088 4349has_use_on_stmt (tree name, gimple *stmt)
89a79e96
RB
4350{
4351 imm_use_iterator iter;
4352 use_operand_p use_p;
4353 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
4354 if (USE_STMT (use_p) == stmt)
4355 return true;
4356 return false;
4357}
4358
e0ee10ed
RB
4359/* Worker for fold_stmt_1 dispatch to pattern based folding with
4360 gimple_simplify.
4361
4362 Replaces *GSI with the simplification result in RCODE and OPS
4363 and the associated statements in *SEQ. Does the replacement
4364 according to INPLACE and returns true if the operation succeeded. */
4365
4366static bool
4367replace_stmt_with_simplification (gimple_stmt_iterator *gsi,
5d75ad95 4368 gimple_match_op *res_op,
e0ee10ed
RB
4369 gimple_seq *seq, bool inplace)
4370{
355fe088 4371 gimple *stmt = gsi_stmt (*gsi);
5d75ad95
RS
4372 tree *ops = res_op->ops;
4373 unsigned int num_ops = res_op->num_ops;
e0ee10ed
RB
4374
4375 /* Play safe and do not allow abnormals to be mentioned in
89a79e96
RB
4376 newly created statements. See also maybe_push_res_to_seq.
4377 As an exception allow such uses if there was a use of the
4378 same SSA name on the old stmt. */
5d75ad95
RS
4379 for (unsigned int i = 0; i < num_ops; ++i)
4380 if (TREE_CODE (ops[i]) == SSA_NAME
4381 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ops[i])
4382 && !has_use_on_stmt (ops[i], stmt))
4383 return false;
4384
4385 if (num_ops > 0 && COMPARISON_CLASS_P (ops[0]))
4386 for (unsigned int i = 0; i < 2; ++i)
4387 if (TREE_CODE (TREE_OPERAND (ops[0], i)) == SSA_NAME
4388 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (ops[0], i))
4389 && !has_use_on_stmt (TREE_OPERAND (ops[0], i), stmt))
4390 return false;
e0ee10ed 4391
fec40d06
RS
4392 /* Don't insert new statements when INPLACE is true, even if we could
4393 reuse STMT for the final statement. */
4394 if (inplace && !gimple_seq_empty_p (*seq))
4395 return false;
4396
538dd0b7 4397 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
e0ee10ed 4398 {
5d75ad95
RS
4399 gcc_assert (res_op->code.is_tree_code ());
4400 if (TREE_CODE_CLASS ((enum tree_code) res_op->code) == tcc_comparison
e0ee10ed
RB
4401 /* GIMPLE_CONDs condition may not throw. */
4402 && (!flag_exceptions
4403 || !cfun->can_throw_non_call_exceptions
5d75ad95 4404 || !operation_could_trap_p (res_op->code,
e0ee10ed
RB
4405 FLOAT_TYPE_P (TREE_TYPE (ops[0])),
4406 false, NULL_TREE)))
5d75ad95
RS
4407 gimple_cond_set_condition (cond_stmt, res_op->code, ops[0], ops[1]);
4408 else if (res_op->code == SSA_NAME)
538dd0b7 4409 gimple_cond_set_condition (cond_stmt, NE_EXPR, ops[0],
e0ee10ed 4410 build_zero_cst (TREE_TYPE (ops[0])));
5d75ad95 4411 else if (res_op->code == INTEGER_CST)
e0ee10ed
RB
4412 {
4413 if (integer_zerop (ops[0]))
538dd0b7 4414 gimple_cond_make_false (cond_stmt);
e0ee10ed 4415 else
538dd0b7 4416 gimple_cond_make_true (cond_stmt);
e0ee10ed
RB
4417 }
4418 else if (!inplace)
4419 {
5d75ad95 4420 tree res = maybe_push_res_to_seq (res_op, seq);
e0ee10ed
RB
4421 if (!res)
4422 return false;
538dd0b7 4423 gimple_cond_set_condition (cond_stmt, NE_EXPR, res,
e0ee10ed
RB
4424 build_zero_cst (TREE_TYPE (res)));
4425 }
4426 else
4427 return false;
4428 if (dump_file && (dump_flags & TDF_DETAILS))
4429 {
4430 fprintf (dump_file, "gimple_simplified to ");
4431 if (!gimple_seq_empty_p (*seq))
4432 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4433 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
4434 0, TDF_SLIM);
4435 }
4436 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4437 return true;
4438 }
4439 else if (is_gimple_assign (stmt)
5d75ad95 4440 && res_op->code.is_tree_code ())
e0ee10ed
RB
4441 {
4442 if (!inplace
5d75ad95 4443 || gimple_num_ops (stmt) > get_gimple_rhs_num_ops (res_op->code))
e0ee10ed 4444 {
5d75ad95
RS
4445 maybe_build_generic_op (res_op);
4446 gimple_assign_set_rhs_with_ops (gsi, res_op->code,
4447 res_op->op_or_null (0),
4448 res_op->op_or_null (1),
4449 res_op->op_or_null (2));
e0ee10ed
RB
4450 if (dump_file && (dump_flags & TDF_DETAILS))
4451 {
4452 fprintf (dump_file, "gimple_simplified to ");
4453 if (!gimple_seq_empty_p (*seq))
4454 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4455 print_gimple_stmt (dump_file, gsi_stmt (*gsi),
4456 0, TDF_SLIM);
4457 }
4458 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
4459 return true;
4460 }
4461 }
5d75ad95
RS
4462 else if (res_op->code.is_fn_code ()
4463 && gimple_call_combined_fn (stmt) == res_op->code)
37d486ab 4464 {
5d75ad95
RS
4465 gcc_assert (num_ops == gimple_call_num_args (stmt));
4466 for (unsigned int i = 0; i < num_ops; ++i)
4467 gimple_call_set_arg (stmt, i, ops[i]);
fec40d06
RS
4468 if (dump_file && (dump_flags & TDF_DETAILS))
4469 {
4470 fprintf (dump_file, "gimple_simplified to ");
4471 if (!gimple_seq_empty_p (*seq))
4472 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4473 print_gimple_stmt (dump_file, gsi_stmt (*gsi), 0, TDF_SLIM);
4474 }
4475 gsi_insert_seq_before (gsi, *seq, GSI_SAME_STMT);
37d486ab
RB
4476 return true;
4477 }
e0ee10ed
RB
4478 else if (!inplace)
4479 {
4480 if (gimple_has_lhs (stmt))
4481 {
4482 tree lhs = gimple_get_lhs (stmt);
5d75ad95 4483 if (!maybe_push_res_to_seq (res_op, seq, lhs))
de665bbd 4484 return false;
e0ee10ed
RB
4485 if (dump_file && (dump_flags & TDF_DETAILS))
4486 {
4487 fprintf (dump_file, "gimple_simplified to ");
4488 print_gimple_seq (dump_file, *seq, 0, TDF_SLIM);
4489 }
4490 gsi_replace_with_seq_vops (gsi, *seq);
4491 return true;
4492 }
4493 else
4494 gcc_unreachable ();
4495 }
4496
4497 return false;
4498}
4499
040292e7
RB
4500/* Canonicalize MEM_REFs invariant address operand after propagation. */
4501
4502static bool
4503maybe_canonicalize_mem_ref_addr (tree *t)
4504{
4505 bool res = false;
4506
4507 if (TREE_CODE (*t) == ADDR_EXPR)
4508 t = &TREE_OPERAND (*t, 0);
4509
f17a223d
RB
4510 /* The C and C++ frontends use an ARRAY_REF for indexing with their
4511 generic vector extension. The actual vector referenced is
4512 view-converted to an array type for this purpose. If the index
4513 is constant the canonical representation in the middle-end is a
4514 BIT_FIELD_REF so re-write the former to the latter here. */
4515 if (TREE_CODE (*t) == ARRAY_REF
4516 && TREE_CODE (TREE_OPERAND (*t, 0)) == VIEW_CONVERT_EXPR
4517 && TREE_CODE (TREE_OPERAND (*t, 1)) == INTEGER_CST
4518 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0))))
4519 {
4520 tree vtype = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*t, 0), 0));
4521 if (VECTOR_TYPE_P (vtype))
4522 {
4523 tree low = array_ref_low_bound (*t);
4524 if (TREE_CODE (low) == INTEGER_CST)
4525 {
4526 if (tree_int_cst_le (low, TREE_OPERAND (*t, 1)))
4527 {
4528 widest_int idx = wi::sub (wi::to_widest (TREE_OPERAND (*t, 1)),
4529 wi::to_widest (low));
4530 idx = wi::mul (idx, wi::to_widest
4531 (TYPE_SIZE (TREE_TYPE (*t))));
4532 widest_int ext
4533 = wi::add (idx, wi::to_widest (TYPE_SIZE (TREE_TYPE (*t))));
4534 if (wi::les_p (ext, wi::to_widest (TYPE_SIZE (vtype))))
4535 {
4536 *t = build3_loc (EXPR_LOCATION (*t), BIT_FIELD_REF,
4537 TREE_TYPE (*t),
4538 TREE_OPERAND (TREE_OPERAND (*t, 0), 0),
4539 TYPE_SIZE (TREE_TYPE (*t)),
92e29a5e 4540 wide_int_to_tree (bitsizetype, idx));
f17a223d
RB
4541 res = true;
4542 }
4543 }
4544 }
4545 }
4546 }
4547
040292e7
RB
4548 while (handled_component_p (*t))
4549 t = &TREE_OPERAND (*t, 0);
4550
4551 /* Canonicalize MEM [&foo.bar, 0] which appears after propagating
4552 of invariant addresses into a SSA name MEM_REF address. */
4553 if (TREE_CODE (*t) == MEM_REF
4554 || TREE_CODE (*t) == TARGET_MEM_REF)
4555 {
4556 tree addr = TREE_OPERAND (*t, 0);
4557 if (TREE_CODE (addr) == ADDR_EXPR
4558 && (TREE_CODE (TREE_OPERAND (addr, 0)) == MEM_REF
4559 || handled_component_p (TREE_OPERAND (addr, 0))))
4560 {
4561 tree base;
a90c8804 4562 poly_int64 coffset;
040292e7
RB
4563 base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
4564 &coffset);
4565 if (!base)
4566 gcc_unreachable ();
4567
4568 TREE_OPERAND (*t, 0) = build_fold_addr_expr (base);
4569 TREE_OPERAND (*t, 1) = int_const_binop (PLUS_EXPR,
4570 TREE_OPERAND (*t, 1),
4571 size_int (coffset));
4572 res = true;
4573 }
4574 gcc_checking_assert (TREE_CODE (TREE_OPERAND (*t, 0)) == DEBUG_EXPR_DECL
4575 || is_gimple_mem_ref_addr (TREE_OPERAND (*t, 0)));
4576 }
4577
4578 /* Canonicalize back MEM_REFs to plain reference trees if the object
4579 accessed is a decl that has the same access semantics as the MEM_REF. */
4580 if (TREE_CODE (*t) == MEM_REF
4581 && TREE_CODE (TREE_OPERAND (*t, 0)) == ADDR_EXPR
f3dccf50
RB
4582 && integer_zerop (TREE_OPERAND (*t, 1))
4583 && MR_DEPENDENCE_CLIQUE (*t) == 0)
040292e7
RB
4584 {
4585 tree decl = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
4586 tree alias_type = TREE_TYPE (TREE_OPERAND (*t, 1));
4587 if (/* Same volatile qualification. */
4588 TREE_THIS_VOLATILE (*t) == TREE_THIS_VOLATILE (decl)
4589 /* Same TBAA behavior with -fstrict-aliasing. */
4590 && !TYPE_REF_CAN_ALIAS_ALL (alias_type)
4591 && (TYPE_MAIN_VARIANT (TREE_TYPE (decl))
4592 == TYPE_MAIN_VARIANT (TREE_TYPE (alias_type)))
4593 /* Same alignment. */
4594 && TYPE_ALIGN (TREE_TYPE (decl)) == TYPE_ALIGN (TREE_TYPE (*t))
4595 /* We have to look out here to not drop a required conversion
4596 from the rhs to the lhs if *t appears on the lhs or vice-versa
4597 if it appears on the rhs. Thus require strict type
4598 compatibility. */
4599 && types_compatible_p (TREE_TYPE (*t), TREE_TYPE (decl)))
4600 {
4601 *t = TREE_OPERAND (TREE_OPERAND (*t, 0), 0);
4602 res = true;
4603 }
4604 }
4605
4606 /* Canonicalize TARGET_MEM_REF in particular with respect to
4607 the indexes becoming constant. */
4608 else if (TREE_CODE (*t) == TARGET_MEM_REF)
4609 {
4610 tree tem = maybe_fold_tmr (*t);
4611 if (tem)
4612 {
4613 *t = tem;
4614 res = true;
4615 }
4616 }
4617
4618 return res;
4619}
4620
cbdd87d4
RG
4621/* Worker for both fold_stmt and fold_stmt_inplace. The INPLACE argument
4622 distinguishes both cases. */
4623
4624static bool
e0ee10ed 4625fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
cbdd87d4
RG
4626{
4627 bool changed = false;
355fe088 4628 gimple *stmt = gsi_stmt (*gsi);
a8b85ce9 4629 bool nowarning = gimple_no_warning_p (stmt);
cbdd87d4 4630 unsigned i;
a8b85ce9 4631 fold_defer_overflow_warnings ();
cbdd87d4 4632
040292e7
RB
4633 /* First do required canonicalization of [TARGET_]MEM_REF addresses
4634 after propagation.
4635 ??? This shouldn't be done in generic folding but in the
4636 propagation helpers which also know whether an address was
89a79e96
RB
4637 propagated.
4638 Also canonicalize operand order. */
040292e7
RB
4639 switch (gimple_code (stmt))
4640 {
4641 case GIMPLE_ASSIGN:
4642 if (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
4643 {
4644 tree *rhs = gimple_assign_rhs1_ptr (stmt);
4645 if ((REFERENCE_CLASS_P (*rhs)
4646 || TREE_CODE (*rhs) == ADDR_EXPR)
4647 && maybe_canonicalize_mem_ref_addr (rhs))
4648 changed = true;
4649 tree *lhs = gimple_assign_lhs_ptr (stmt);
4650 if (REFERENCE_CLASS_P (*lhs)
4651 && maybe_canonicalize_mem_ref_addr (lhs))
4652 changed = true;
4653 }
89a79e96
RB
4654 else
4655 {
4656 /* Canonicalize operand order. */
4657 enum tree_code code = gimple_assign_rhs_code (stmt);
4658 if (TREE_CODE_CLASS (code) == tcc_comparison
4659 || commutative_tree_code (code)
4660 || commutative_ternary_tree_code (code))
4661 {
4662 tree rhs1 = gimple_assign_rhs1 (stmt);
4663 tree rhs2 = gimple_assign_rhs2 (stmt);
14e72812 4664 if (tree_swap_operands_p (rhs1, rhs2))
89a79e96
RB
4665 {
4666 gimple_assign_set_rhs1 (stmt, rhs2);
4667 gimple_assign_set_rhs2 (stmt, rhs1);
4668 if (TREE_CODE_CLASS (code) == tcc_comparison)
4669 gimple_assign_set_rhs_code (stmt,
4670 swap_tree_comparison (code));
4671 changed = true;
4672 }
4673 }
4674 }
040292e7
RB
4675 break;
4676 case GIMPLE_CALL:
4677 {
4678 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4679 {
4680 tree *arg = gimple_call_arg_ptr (stmt, i);
4681 if (REFERENCE_CLASS_P (*arg)
4682 && maybe_canonicalize_mem_ref_addr (arg))
4683 changed = true;
4684 }
4685 tree *lhs = gimple_call_lhs_ptr (stmt);
4686 if (*lhs
4687 && REFERENCE_CLASS_P (*lhs)
4688 && maybe_canonicalize_mem_ref_addr (lhs))
4689 changed = true;
4690 break;
4691 }
4692 case GIMPLE_ASM:
4693 {
538dd0b7
DM
4694 gasm *asm_stmt = as_a <gasm *> (stmt);
4695 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
040292e7 4696 {
538dd0b7 4697 tree link = gimple_asm_output_op (asm_stmt, i);
040292e7
RB
4698 tree op = TREE_VALUE (link);
4699 if (REFERENCE_CLASS_P (op)
4700 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
4701 changed = true;
4702 }
538dd0b7 4703 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
040292e7 4704 {
538dd0b7 4705 tree link = gimple_asm_input_op (asm_stmt, i);
040292e7
RB
4706 tree op = TREE_VALUE (link);
4707 if ((REFERENCE_CLASS_P (op)
4708 || TREE_CODE (op) == ADDR_EXPR)
4709 && maybe_canonicalize_mem_ref_addr (&TREE_VALUE (link)))
4710 changed = true;
4711 }
4712 }
4713 break;
4714 case GIMPLE_DEBUG:
4715 if (gimple_debug_bind_p (stmt))
4716 {
4717 tree *val = gimple_debug_bind_get_value_ptr (stmt);
4718 if (*val
4719 && (REFERENCE_CLASS_P (*val)
4720 || TREE_CODE (*val) == ADDR_EXPR)
4721 && maybe_canonicalize_mem_ref_addr (val))
4722 changed = true;
4723 }
4724 break;
89a79e96
RB
4725 case GIMPLE_COND:
4726 {
4727 /* Canonicalize operand order. */
4728 tree lhs = gimple_cond_lhs (stmt);
4729 tree rhs = gimple_cond_rhs (stmt);
14e72812 4730 if (tree_swap_operands_p (lhs, rhs))
89a79e96
RB
4731 {
4732 gcond *gc = as_a <gcond *> (stmt);
4733 gimple_cond_set_lhs (gc, rhs);
4734 gimple_cond_set_rhs (gc, lhs);
4735 gimple_cond_set_code (gc,
4736 swap_tree_comparison (gimple_cond_code (gc)));
4737 changed = true;
4738 }
4739 }
040292e7
RB
4740 default:;
4741 }
4742
e0ee10ed
RB
4743 /* Dispatch to pattern-based folding. */
4744 if (!inplace
4745 || is_gimple_assign (stmt)
4746 || gimple_code (stmt) == GIMPLE_COND)
4747 {
4748 gimple_seq seq = NULL;
5d75ad95
RS
4749 gimple_match_op res_op;
4750 if (gimple_simplify (stmt, &res_op, inplace ? NULL : &seq,
0ff093d8 4751 valueize, valueize))
e0ee10ed 4752 {
5d75ad95 4753 if (replace_stmt_with_simplification (gsi, &res_op, &seq, inplace))
e0ee10ed
RB
4754 changed = true;
4755 else
4756 gimple_seq_discard (seq);
4757 }
4758 }
4759
4760 stmt = gsi_stmt (*gsi);
4761
cbdd87d4
RG
4762 /* Fold the main computation performed by the statement. */
4763 switch (gimple_code (stmt))
4764 {
4765 case GIMPLE_ASSIGN:
4766 {
819ec64c
RB
4767 /* Try to canonicalize for boolean-typed X the comparisons
4768 X == 0, X == 1, X != 0, and X != 1. */
4769 if (gimple_assign_rhs_code (stmt) == EQ_EXPR
4770 || gimple_assign_rhs_code (stmt) == NE_EXPR)
5fbcc0ed 4771 {
819ec64c
RB
4772 tree lhs = gimple_assign_lhs (stmt);
4773 tree op1 = gimple_assign_rhs1 (stmt);
4774 tree op2 = gimple_assign_rhs2 (stmt);
4775 tree type = TREE_TYPE (op1);
4776
4777 /* Check whether the comparison operands are of the same boolean
4778 type as the result type is.
4779 Check that second operand is an integer-constant with value
4780 one or zero. */
4781 if (TREE_CODE (op2) == INTEGER_CST
4782 && (integer_zerop (op2) || integer_onep (op2))
4783 && useless_type_conversion_p (TREE_TYPE (lhs), type))
4784 {
4785 enum tree_code cmp_code = gimple_assign_rhs_code (stmt);
4786 bool is_logical_not = false;
4787
4788 /* X == 0 and X != 1 is a logical-not.of X
4789 X == 1 and X != 0 is X */
4790 if ((cmp_code == EQ_EXPR && integer_zerop (op2))
4791 || (cmp_code == NE_EXPR && integer_onep (op2)))
4792 is_logical_not = true;
4793
4794 if (is_logical_not == false)
4795 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op1), op1);
4796 /* Only for one-bit precision typed X the transformation
4797 !X -> ~X is valied. */
4798 else if (TYPE_PRECISION (type) == 1)
4799 gimple_assign_set_rhs_with_ops (gsi, BIT_NOT_EXPR, op1);
4800 /* Otherwise we use !X -> X ^ 1. */
4801 else
4802 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op1,
4803 build_int_cst (type, 1));
4804 changed = true;
4805 break;
4806 }
5fbcc0ed 4807 }
819ec64c
RB
4808
4809 unsigned old_num_ops = gimple_num_ops (stmt);
4810 tree lhs = gimple_assign_lhs (stmt);
4811 tree new_rhs = fold_gimple_assign (gsi);
cbdd87d4
RG
4812 if (new_rhs
4813 && !useless_type_conversion_p (TREE_TYPE (lhs),
4814 TREE_TYPE (new_rhs)))
4815 new_rhs = fold_convert (TREE_TYPE (lhs), new_rhs);
4816 if (new_rhs
4817 && (!inplace
4818 || get_gimple_rhs_num_ops (TREE_CODE (new_rhs)) < old_num_ops))
4819 {
4820 gimple_assign_set_rhs_from_tree (gsi, new_rhs);
4821 changed = true;
4822 }
4823 break;
4824 }
4825
cbdd87d4 4826 case GIMPLE_CALL:
ceeffab0 4827 changed |= gimple_fold_call (gsi, inplace);
cbdd87d4
RG
4828 break;
4829
4830 case GIMPLE_ASM:
4831 /* Fold *& in asm operands. */
38384150 4832 {
538dd0b7 4833 gasm *asm_stmt = as_a <gasm *> (stmt);
38384150
JJ
4834 size_t noutputs;
4835 const char **oconstraints;
4836 const char *constraint;
4837 bool allows_mem, allows_reg;
4838
538dd0b7 4839 noutputs = gimple_asm_noutputs (asm_stmt);
38384150
JJ
4840 oconstraints = XALLOCAVEC (const char *, noutputs);
4841
538dd0b7 4842 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
38384150 4843 {
538dd0b7 4844 tree link = gimple_asm_output_op (asm_stmt, i);
38384150
JJ
4845 tree op = TREE_VALUE (link);
4846 oconstraints[i]
4847 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4848 if (REFERENCE_CLASS_P (op)
4849 && (op = maybe_fold_reference (op, true)) != NULL_TREE)
4850 {
4851 TREE_VALUE (link) = op;
4852 changed = true;
4853 }
4854 }
538dd0b7 4855 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
38384150 4856 {
538dd0b7 4857 tree link = gimple_asm_input_op (asm_stmt, i);
38384150
JJ
4858 tree op = TREE_VALUE (link);
4859 constraint
4860 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4861 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
4862 oconstraints, &allows_mem, &allows_reg);
4863 if (REFERENCE_CLASS_P (op)
4864 && (op = maybe_fold_reference (op, !allows_reg && allows_mem))
4865 != NULL_TREE)
4866 {
4867 TREE_VALUE (link) = op;
4868 changed = true;
4869 }
4870 }
4871 }
cbdd87d4
RG
4872 break;
4873
bd422c4a
RG
4874 case GIMPLE_DEBUG:
4875 if (gimple_debug_bind_p (stmt))
4876 {
4877 tree val = gimple_debug_bind_get_value (stmt);
4878 if (val
4879 && REFERENCE_CLASS_P (val))
4880 {
4881 tree tem = maybe_fold_reference (val, false);
4882 if (tem)
4883 {
4884 gimple_debug_bind_set_value (stmt, tem);
4885 changed = true;
4886 }
4887 }
3e888a5e
RG
4888 else if (val
4889 && TREE_CODE (val) == ADDR_EXPR)
4890 {
4891 tree ref = TREE_OPERAND (val, 0);
4892 tree tem = maybe_fold_reference (ref, false);
4893 if (tem)
4894 {
4895 tem = build_fold_addr_expr_with_type (tem, TREE_TYPE (val));
4896 gimple_debug_bind_set_value (stmt, tem);
4897 changed = true;
4898 }
4899 }
bd422c4a
RG
4900 }
4901 break;
4902
cfe3d653
PK
4903 case GIMPLE_RETURN:
4904 {
4905 greturn *ret_stmt = as_a<greturn *> (stmt);
4906 tree ret = gimple_return_retval(ret_stmt);
4907
4908 if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
4909 {
4910 tree val = valueize (ret);
1af928db
RB
4911 if (val && val != ret
4912 && may_propagate_copy (ret, val))
cfe3d653
PK
4913 {
4914 gimple_return_set_retval (ret_stmt, val);
4915 changed = true;
4916 }
4917 }
4918 }
4919 break;
4920
cbdd87d4
RG
4921 default:;
4922 }
4923
4924 stmt = gsi_stmt (*gsi);
4925
37376165
RB
4926 /* Fold *& on the lhs. */
4927 if (gimple_has_lhs (stmt))
cbdd87d4
RG
4928 {
4929 tree lhs = gimple_get_lhs (stmt);
4930 if (lhs && REFERENCE_CLASS_P (lhs))
4931 {
4932 tree new_lhs = maybe_fold_reference (lhs, true);
4933 if (new_lhs)
4934 {
4935 gimple_set_lhs (stmt, new_lhs);
4936 changed = true;
4937 }
4938 }
4939 }
4940
a8b85ce9 4941 fold_undefer_overflow_warnings (changed && !nowarning, stmt, 0);
cbdd87d4
RG
4942 return changed;
4943}
4944
e0ee10ed
RB
4945/* Valueziation callback that ends up not following SSA edges. */
4946
4947tree
4948no_follow_ssa_edges (tree)
4949{
4950 return NULL_TREE;
4951}
4952
45cc9f96
RB
4953/* Valueization callback that ends up following single-use SSA edges only. */
4954
4955tree
4956follow_single_use_edges (tree val)
4957{
4958 if (TREE_CODE (val) == SSA_NAME
4959 && !has_single_use (val))
4960 return NULL_TREE;
4961 return val;
4962}
4963
c566cc9f
RS
4964/* Valueization callback that follows all SSA edges. */
4965
4966tree
4967follow_all_ssa_edges (tree val)
4968{
4969 return val;
4970}
4971
cbdd87d4
RG
4972/* Fold the statement pointed to by GSI. In some cases, this function may
4973 replace the whole statement with a new one. Returns true iff folding
4974 makes any changes.
4975 The statement pointed to by GSI should be in valid gimple form but may
4976 be in unfolded state as resulting from for example constant propagation
4977 which can produce *&x = 0. */
4978
4979bool
4980fold_stmt (gimple_stmt_iterator *gsi)
4981{
e0ee10ed
RB
4982 return fold_stmt_1 (gsi, false, no_follow_ssa_edges);
4983}
4984
4985bool
4986fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
4987{
4988 return fold_stmt_1 (gsi, false, valueize);
cbdd87d4
RG
4989}
4990
59401b92 4991/* Perform the minimal folding on statement *GSI. Only operations like
cbdd87d4
RG
4992 *&x created by constant propagation are handled. The statement cannot
4993 be replaced with a new one. Return true if the statement was
4994 changed, false otherwise.
59401b92 4995 The statement *GSI should be in valid gimple form but may
cbdd87d4
RG
4996 be in unfolded state as resulting from for example constant propagation
4997 which can produce *&x = 0. */
4998
4999bool
59401b92 5000fold_stmt_inplace (gimple_stmt_iterator *gsi)
cbdd87d4 5001{
355fe088 5002 gimple *stmt = gsi_stmt (*gsi);
e0ee10ed 5003 bool changed = fold_stmt_1 (gsi, true, no_follow_ssa_edges);
59401b92 5004 gcc_assert (gsi_stmt (*gsi) == stmt);
cbdd87d4
RG
5005 return changed;
5006}
5007
e89065a1
SL
5008/* Canonicalize and possibly invert the boolean EXPR; return NULL_TREE
5009 if EXPR is null or we don't know how.
5010 If non-null, the result always has boolean type. */
5011
5012static tree
5013canonicalize_bool (tree expr, bool invert)
5014{
5015 if (!expr)
5016 return NULL_TREE;
5017 else if (invert)
5018 {
5019 if (integer_nonzerop (expr))
5020 return boolean_false_node;
5021 else if (integer_zerop (expr))
5022 return boolean_true_node;
5023 else if (TREE_CODE (expr) == SSA_NAME)
5024 return fold_build2 (EQ_EXPR, boolean_type_node, expr,
5025 build_int_cst (TREE_TYPE (expr), 0));
98209db3 5026 else if (COMPARISON_CLASS_P (expr))
e89065a1
SL
5027 return fold_build2 (invert_tree_comparison (TREE_CODE (expr), false),
5028 boolean_type_node,
5029 TREE_OPERAND (expr, 0),
5030 TREE_OPERAND (expr, 1));
5031 else
5032 return NULL_TREE;
5033 }
5034 else
5035 {
5036 if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
5037 return expr;
5038 if (integer_nonzerop (expr))
5039 return boolean_true_node;
5040 else if (integer_zerop (expr))
5041 return boolean_false_node;
5042 else if (TREE_CODE (expr) == SSA_NAME)
5043 return fold_build2 (NE_EXPR, boolean_type_node, expr,
5044 build_int_cst (TREE_TYPE (expr), 0));
98209db3 5045 else if (COMPARISON_CLASS_P (expr))
e89065a1
SL
5046 return fold_build2 (TREE_CODE (expr),
5047 boolean_type_node,
5048 TREE_OPERAND (expr, 0),
5049 TREE_OPERAND (expr, 1));
5050 else
5051 return NULL_TREE;
5052 }
5053}
5054
5055/* Check to see if a boolean expression EXPR is logically equivalent to the
5056 comparison (OP1 CODE OP2). Check for various identities involving
5057 SSA_NAMEs. */
5058
5059static bool
5060same_bool_comparison_p (const_tree expr, enum tree_code code,
5061 const_tree op1, const_tree op2)
5062{
355fe088 5063 gimple *s;
e89065a1
SL
5064
5065 /* The obvious case. */
5066 if (TREE_CODE (expr) == code
5067 && operand_equal_p (TREE_OPERAND (expr, 0), op1, 0)
5068 && operand_equal_p (TREE_OPERAND (expr, 1), op2, 0))
5069 return true;
5070
5071 /* Check for comparing (name, name != 0) and the case where expr
5072 is an SSA_NAME with a definition matching the comparison. */
5073 if (TREE_CODE (expr) == SSA_NAME
5074 && TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE)
5075 {
5076 if (operand_equal_p (expr, op1, 0))
5077 return ((code == NE_EXPR && integer_zerop (op2))
5078 || (code == EQ_EXPR && integer_nonzerop (op2)));
5079 s = SSA_NAME_DEF_STMT (expr);
5080 if (is_gimple_assign (s)
5081 && gimple_assign_rhs_code (s) == code
5082 && operand_equal_p (gimple_assign_rhs1 (s), op1, 0)
5083 && operand_equal_p (gimple_assign_rhs2 (s), op2, 0))
5084 return true;
5085 }
5086
5087 /* If op1 is of the form (name != 0) or (name == 0), and the definition
5088 of name is a comparison, recurse. */
5089 if (TREE_CODE (op1) == SSA_NAME
5090 && TREE_CODE (TREE_TYPE (op1)) == BOOLEAN_TYPE)
5091 {
5092 s = SSA_NAME_DEF_STMT (op1);
5093 if (is_gimple_assign (s)
5094 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
5095 {
5096 enum tree_code c = gimple_assign_rhs_code (s);
5097 if ((c == NE_EXPR && integer_zerop (op2))
5098 || (c == EQ_EXPR && integer_nonzerop (op2)))
5099 return same_bool_comparison_p (expr, c,
5100 gimple_assign_rhs1 (s),
5101 gimple_assign_rhs2 (s));
5102 if ((c == EQ_EXPR && integer_zerop (op2))
5103 || (c == NE_EXPR && integer_nonzerop (op2)))
5104 return same_bool_comparison_p (expr,
5105 invert_tree_comparison (c, false),
5106 gimple_assign_rhs1 (s),
5107 gimple_assign_rhs2 (s));
5108 }
5109 }
5110 return false;
5111}
5112
5113/* Check to see if two boolean expressions OP1 and OP2 are logically
5114 equivalent. */
5115
5116static bool
5117same_bool_result_p (const_tree op1, const_tree op2)
5118{
5119 /* Simple cases first. */
5120 if (operand_equal_p (op1, op2, 0))
5121 return true;
5122
5123 /* Check the cases where at least one of the operands is a comparison.
5124 These are a bit smarter than operand_equal_p in that they apply some
5125 identifies on SSA_NAMEs. */
98209db3 5126 if (COMPARISON_CLASS_P (op2)
e89065a1
SL
5127 && same_bool_comparison_p (op1, TREE_CODE (op2),
5128 TREE_OPERAND (op2, 0),
5129 TREE_OPERAND (op2, 1)))
5130 return true;
98209db3 5131 if (COMPARISON_CLASS_P (op1)
e89065a1
SL
5132 && same_bool_comparison_p (op2, TREE_CODE (op1),
5133 TREE_OPERAND (op1, 0),
5134 TREE_OPERAND (op1, 1)))
5135 return true;
5136
5137 /* Default case. */
5138 return false;
5139}
5140
5141/* Forward declarations for some mutually recursive functions. */
5142
5143static tree
5144and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5145 enum tree_code code2, tree op2a, tree op2b);
5146static tree
5147and_var_with_comparison (tree var, bool invert,
5148 enum tree_code code2, tree op2a, tree op2b);
5149static tree
355fe088 5150and_var_with_comparison_1 (gimple *stmt,
e89065a1
SL
5151 enum tree_code code2, tree op2a, tree op2b);
5152static tree
5153or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5154 enum tree_code code2, tree op2a, tree op2b);
5155static tree
5156or_var_with_comparison (tree var, bool invert,
5157 enum tree_code code2, tree op2a, tree op2b);
5158static tree
355fe088 5159or_var_with_comparison_1 (gimple *stmt,
e89065a1
SL
5160 enum tree_code code2, tree op2a, tree op2b);
5161
5162/* Helper function for and_comparisons_1: try to simplify the AND of the
5163 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5164 If INVERT is true, invert the value of the VAR before doing the AND.
5165 Return NULL_EXPR if we can't simplify this to a single expression. */
5166
5167static tree
5168and_var_with_comparison (tree var, bool invert,
5169 enum tree_code code2, tree op2a, tree op2b)
5170{
5171 tree t;
355fe088 5172 gimple *stmt = SSA_NAME_DEF_STMT (var);
e89065a1
SL
5173
5174 /* We can only deal with variables whose definitions are assignments. */
5175 if (!is_gimple_assign (stmt))
5176 return NULL_TREE;
5177
5178 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5179 !var AND (op2a code2 op2b) => !(var OR !(op2a code2 op2b))
5180 Then we only have to consider the simpler non-inverted cases. */
5181 if (invert)
5182 t = or_var_with_comparison_1 (stmt,
5183 invert_tree_comparison (code2, false),
5184 op2a, op2b);
5185 else
5186 t = and_var_with_comparison_1 (stmt, code2, op2a, op2b);
5187 return canonicalize_bool (t, invert);
5188}
5189
5190/* Try to simplify the AND of the ssa variable defined by the assignment
5191 STMT with the comparison specified by (OP2A CODE2 OP2B).
5192 Return NULL_EXPR if we can't simplify this to a single expression. */
5193
5194static tree
355fe088 5195and_var_with_comparison_1 (gimple *stmt,
e89065a1
SL
5196 enum tree_code code2, tree op2a, tree op2b)
5197{
5198 tree var = gimple_assign_lhs (stmt);
5199 tree true_test_var = NULL_TREE;
5200 tree false_test_var = NULL_TREE;
5201 enum tree_code innercode = gimple_assign_rhs_code (stmt);
5202
5203 /* Check for identities like (var AND (var == 0)) => false. */
5204 if (TREE_CODE (op2a) == SSA_NAME
5205 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
5206 {
5207 if ((code2 == NE_EXPR && integer_zerop (op2b))
5208 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
5209 {
5210 true_test_var = op2a;
5211 if (var == true_test_var)
5212 return var;
5213 }
5214 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
5215 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
5216 {
5217 false_test_var = op2a;
5218 if (var == false_test_var)
5219 return boolean_false_node;
5220 }
5221 }
5222
5223 /* If the definition is a comparison, recurse on it. */
5224 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
5225 {
5226 tree t = and_comparisons_1 (innercode,
5227 gimple_assign_rhs1 (stmt),
5228 gimple_assign_rhs2 (stmt),
5229 code2,
5230 op2a,
5231 op2b);
5232 if (t)
5233 return t;
5234 }
5235
5236 /* If the definition is an AND or OR expression, we may be able to
5237 simplify by reassociating. */
eb9820c0
KT
5238 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
5239 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
e89065a1
SL
5240 {
5241 tree inner1 = gimple_assign_rhs1 (stmt);
5242 tree inner2 = gimple_assign_rhs2 (stmt);
355fe088 5243 gimple *s;
e89065a1
SL
5244 tree t;
5245 tree partial = NULL_TREE;
eb9820c0 5246 bool is_and = (innercode == BIT_AND_EXPR);
e89065a1
SL
5247
5248 /* Check for boolean identities that don't require recursive examination
5249 of inner1/inner2:
5250 inner1 AND (inner1 AND inner2) => inner1 AND inner2 => var
5251 inner1 AND (inner1 OR inner2) => inner1
5252 !inner1 AND (inner1 AND inner2) => false
5253 !inner1 AND (inner1 OR inner2) => !inner1 AND inner2
5254 Likewise for similar cases involving inner2. */
5255 if (inner1 == true_test_var)
5256 return (is_and ? var : inner1);
5257 else if (inner2 == true_test_var)
5258 return (is_and ? var : inner2);
5259 else if (inner1 == false_test_var)
5260 return (is_and
5261 ? boolean_false_node
5262 : and_var_with_comparison (inner2, false, code2, op2a, op2b));
5263 else if (inner2 == false_test_var)
5264 return (is_and
5265 ? boolean_false_node
5266 : and_var_with_comparison (inner1, false, code2, op2a, op2b));
5267
5268 /* Next, redistribute/reassociate the AND across the inner tests.
5269 Compute the first partial result, (inner1 AND (op2a code op2b)) */
5270 if (TREE_CODE (inner1) == SSA_NAME
5271 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
5272 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5273 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
5274 gimple_assign_rhs1 (s),
5275 gimple_assign_rhs2 (s),
5276 code2, op2a, op2b)))
5277 {
5278 /* Handle the AND case, where we are reassociating:
5279 (inner1 AND inner2) AND (op2a code2 op2b)
5280 => (t AND inner2)
5281 If the partial result t is a constant, we win. Otherwise
5282 continue on to try reassociating with the other inner test. */
5283 if (is_and)
5284 {
5285 if (integer_onep (t))
5286 return inner2;
5287 else if (integer_zerop (t))
5288 return boolean_false_node;
5289 }
5290
5291 /* Handle the OR case, where we are redistributing:
5292 (inner1 OR inner2) AND (op2a code2 op2b)
5293 => (t OR (inner2 AND (op2a code2 op2b))) */
8236c8eb
JJ
5294 else if (integer_onep (t))
5295 return boolean_true_node;
5296
5297 /* Save partial result for later. */
5298 partial = t;
e89065a1
SL
5299 }
5300
5301 /* Compute the second partial result, (inner2 AND (op2a code op2b)) */
5302 if (TREE_CODE (inner2) == SSA_NAME
5303 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
5304 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5305 && (t = maybe_fold_and_comparisons (gimple_assign_rhs_code (s),
5306 gimple_assign_rhs1 (s),
5307 gimple_assign_rhs2 (s),
5308 code2, op2a, op2b)))
5309 {
5310 /* Handle the AND case, where we are reassociating:
5311 (inner1 AND inner2) AND (op2a code2 op2b)
5312 => (inner1 AND t) */
5313 if (is_and)
5314 {
5315 if (integer_onep (t))
5316 return inner1;
5317 else if (integer_zerop (t))
5318 return boolean_false_node;
8236c8eb
JJ
5319 /* If both are the same, we can apply the identity
5320 (x AND x) == x. */
5321 else if (partial && same_bool_result_p (t, partial))
5322 return t;
e89065a1
SL
5323 }
5324
5325 /* Handle the OR case. where we are redistributing:
5326 (inner1 OR inner2) AND (op2a code2 op2b)
5327 => (t OR (inner1 AND (op2a code2 op2b)))
5328 => (t OR partial) */
5329 else
5330 {
5331 if (integer_onep (t))
5332 return boolean_true_node;
5333 else if (partial)
5334 {
5335 /* We already got a simplification for the other
5336 operand to the redistributed OR expression. The
5337 interesting case is when at least one is false.
5338 Or, if both are the same, we can apply the identity
5339 (x OR x) == x. */
5340 if (integer_zerop (partial))
5341 return t;
5342 else if (integer_zerop (t))
5343 return partial;
5344 else if (same_bool_result_p (t, partial))
5345 return t;
5346 }
5347 }
5348 }
5349 }
5350 return NULL_TREE;
5351}
5352
5353/* Try to simplify the AND of two comparisons defined by
5354 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5355 If this can be done without constructing an intermediate value,
5356 return the resulting tree; otherwise NULL_TREE is returned.
5357 This function is deliberately asymmetric as it recurses on SSA_DEFs
5358 in the first comparison but not the second. */
5359
5360static tree
5361and_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5362 enum tree_code code2, tree op2a, tree op2b)
5363{
ae22ac3c 5364 tree truth_type = truth_type_for (TREE_TYPE (op1a));
31ed6226 5365
e89065a1
SL
5366 /* First check for ((x CODE1 y) AND (x CODE2 y)). */
5367 if (operand_equal_p (op1a, op2a, 0)
5368 && operand_equal_p (op1b, op2b, 0))
5369 {
eb9820c0 5370 /* Result will be either NULL_TREE, or a combined comparison. */
e89065a1
SL
5371 tree t = combine_comparisons (UNKNOWN_LOCATION,
5372 TRUTH_ANDIF_EXPR, code1, code2,
31ed6226 5373 truth_type, op1a, op1b);
e89065a1
SL
5374 if (t)
5375 return t;
5376 }
5377
5378 /* Likewise the swapped case of the above. */
5379 if (operand_equal_p (op1a, op2b, 0)
5380 && operand_equal_p (op1b, op2a, 0))
5381 {
eb9820c0 5382 /* Result will be either NULL_TREE, or a combined comparison. */
e89065a1
SL
5383 tree t = combine_comparisons (UNKNOWN_LOCATION,
5384 TRUTH_ANDIF_EXPR, code1,
5385 swap_tree_comparison (code2),
31ed6226 5386 truth_type, op1a, op1b);
e89065a1
SL
5387 if (t)
5388 return t;
5389 }
5390
5391 /* If both comparisons are of the same value against constants, we might
5392 be able to merge them. */
5393 if (operand_equal_p (op1a, op2a, 0)
5394 && TREE_CODE (op1b) == INTEGER_CST
5395 && TREE_CODE (op2b) == INTEGER_CST)
5396 {
5397 int cmp = tree_int_cst_compare (op1b, op2b);
5398
5399 /* If we have (op1a == op1b), we should either be able to
5400 return that or FALSE, depending on whether the constant op1b
5401 also satisfies the other comparison against op2b. */
5402 if (code1 == EQ_EXPR)
5403 {
5404 bool done = true;
5405 bool val;
5406 switch (code2)
5407 {
5408 case EQ_EXPR: val = (cmp == 0); break;
5409 case NE_EXPR: val = (cmp != 0); break;
5410 case LT_EXPR: val = (cmp < 0); break;
5411 case GT_EXPR: val = (cmp > 0); break;
5412 case LE_EXPR: val = (cmp <= 0); break;
5413 case GE_EXPR: val = (cmp >= 0); break;
5414 default: done = false;
5415 }
5416 if (done)
5417 {
5418 if (val)
5419 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5420 else
5421 return boolean_false_node;
5422 }
5423 }
5424 /* Likewise if the second comparison is an == comparison. */
5425 else if (code2 == EQ_EXPR)
5426 {
5427 bool done = true;
5428 bool val;
5429 switch (code1)
5430 {
5431 case EQ_EXPR: val = (cmp == 0); break;
5432 case NE_EXPR: val = (cmp != 0); break;
5433 case LT_EXPR: val = (cmp > 0); break;
5434 case GT_EXPR: val = (cmp < 0); break;
5435 case LE_EXPR: val = (cmp >= 0); break;
5436 case GE_EXPR: val = (cmp <= 0); break;
5437 default: done = false;
5438 }
5439 if (done)
5440 {
5441 if (val)
5442 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5443 else
5444 return boolean_false_node;
5445 }
5446 }
5447
5448 /* Same business with inequality tests. */
5449 else if (code1 == NE_EXPR)
5450 {
5451 bool val;
5452 switch (code2)
5453 {
5454 case EQ_EXPR: val = (cmp != 0); break;
5455 case NE_EXPR: val = (cmp == 0); break;
5456 case LT_EXPR: val = (cmp >= 0); break;
5457 case GT_EXPR: val = (cmp <= 0); break;
5458 case LE_EXPR: val = (cmp > 0); break;
5459 case GE_EXPR: val = (cmp < 0); break;
5460 default:
5461 val = false;
5462 }
5463 if (val)
5464 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5465 }
5466 else if (code2 == NE_EXPR)
5467 {
5468 bool val;
5469 switch (code1)
5470 {
5471 case EQ_EXPR: val = (cmp == 0); break;
5472 case NE_EXPR: val = (cmp != 0); break;
5473 case LT_EXPR: val = (cmp <= 0); break;
5474 case GT_EXPR: val = (cmp >= 0); break;
5475 case LE_EXPR: val = (cmp < 0); break;
5476 case GE_EXPR: val = (cmp > 0); break;
5477 default:
5478 val = false;
5479 }
5480 if (val)
5481 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5482 }
5483
5484 /* Chose the more restrictive of two < or <= comparisons. */
5485 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
5486 && (code2 == LT_EXPR || code2 == LE_EXPR))
5487 {
5488 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
5489 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5490 else
5491 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5492 }
5493
5494 /* Likewise chose the more restrictive of two > or >= comparisons. */
5495 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
5496 && (code2 == GT_EXPR || code2 == GE_EXPR))
5497 {
5498 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
5499 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5500 else
5501 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5502 }
5503
5504 /* Check for singleton ranges. */
5505 else if (cmp == 0
5506 && ((code1 == LE_EXPR && code2 == GE_EXPR)
5507 || (code1 == GE_EXPR && code2 == LE_EXPR)))
5508 return fold_build2 (EQ_EXPR, boolean_type_node, op1a, op2b);
5509
5510 /* Check for disjoint ranges. */
5511 else if (cmp <= 0
5512 && (code1 == LT_EXPR || code1 == LE_EXPR)
5513 && (code2 == GT_EXPR || code2 == GE_EXPR))
5514 return boolean_false_node;
5515 else if (cmp >= 0
5516 && (code1 == GT_EXPR || code1 == GE_EXPR)
5517 && (code2 == LT_EXPR || code2 == LE_EXPR))
5518 return boolean_false_node;
5519 }
5520
5521 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5522 NAME's definition is a truth value. See if there are any simplifications
5523 that can be done against the NAME's definition. */
5524 if (TREE_CODE (op1a) == SSA_NAME
5525 && (code1 == NE_EXPR || code1 == EQ_EXPR)
5526 && (integer_zerop (op1b) || integer_onep (op1b)))
5527 {
5528 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
5529 || (code1 == NE_EXPR && integer_onep (op1b)));
355fe088 5530 gimple *stmt = SSA_NAME_DEF_STMT (op1a);
e89065a1
SL
5531 switch (gimple_code (stmt))
5532 {
5533 case GIMPLE_ASSIGN:
5534 /* Try to simplify by copy-propagating the definition. */
5535 return and_var_with_comparison (op1a, invert, code2, op2a, op2b);
5536
5537 case GIMPLE_PHI:
5538 /* If every argument to the PHI produces the same result when
5539 ANDed with the second comparison, we win.
5540 Do not do this unless the type is bool since we need a bool
5541 result here anyway. */
5542 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
5543 {
5544 tree result = NULL_TREE;
5545 unsigned i;
5546 for (i = 0; i < gimple_phi_num_args (stmt); i++)
5547 {
5548 tree arg = gimple_phi_arg_def (stmt, i);
5549
5550 /* If this PHI has itself as an argument, ignore it.
5551 If all the other args produce the same result,
5552 we're still OK. */
5553 if (arg == gimple_phi_result (stmt))
5554 continue;
5555 else if (TREE_CODE (arg) == INTEGER_CST)
5556 {
5557 if (invert ? integer_nonzerop (arg) : integer_zerop (arg))
5558 {
5559 if (!result)
5560 result = boolean_false_node;
5561 else if (!integer_zerop (result))
5562 return NULL_TREE;
5563 }
5564 else if (!result)
5565 result = fold_build2 (code2, boolean_type_node,
5566 op2a, op2b);
5567 else if (!same_bool_comparison_p (result,
5568 code2, op2a, op2b))
5569 return NULL_TREE;
5570 }
0e8b84ec
JJ
5571 else if (TREE_CODE (arg) == SSA_NAME
5572 && !SSA_NAME_IS_DEFAULT_DEF (arg))
e89065a1 5573 {
6c66f733 5574 tree temp;
355fe088 5575 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
6c66f733
JJ
5576 /* In simple cases we can look through PHI nodes,
5577 but we have to be careful with loops.
5578 See PR49073. */
5579 if (! dom_info_available_p (CDI_DOMINATORS)
5580 || gimple_bb (def_stmt) == gimple_bb (stmt)
5581 || dominated_by_p (CDI_DOMINATORS,
5582 gimple_bb (def_stmt),
5583 gimple_bb (stmt)))
5584 return NULL_TREE;
5585 temp = and_var_with_comparison (arg, invert, code2,
5586 op2a, op2b);
e89065a1
SL
5587 if (!temp)
5588 return NULL_TREE;
5589 else if (!result)
5590 result = temp;
5591 else if (!same_bool_result_p (result, temp))
5592 return NULL_TREE;
5593 }
5594 else
5595 return NULL_TREE;
5596 }
5597 return result;
5598 }
5599
5600 default:
5601 break;
5602 }
5603 }
5604 return NULL_TREE;
5605}
5606
5607/* Try to simplify the AND of two comparisons, specified by
5608 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
5609 If this can be simplified to a single expression (without requiring
5610 introducing more SSA variables to hold intermediate values),
5611 return the resulting tree. Otherwise return NULL_TREE.
5612 If the result expression is non-null, it has boolean type. */
5613
5614tree
5615maybe_fold_and_comparisons (enum tree_code code1, tree op1a, tree op1b,
5616 enum tree_code code2, tree op2a, tree op2b)
5617{
5618 tree t = and_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
5619 if (t)
5620 return t;
5621 else
5622 return and_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
5623}
5624
5625/* Helper function for or_comparisons_1: try to simplify the OR of the
5626 ssa variable VAR with the comparison specified by (OP2A CODE2 OP2B).
5627 If INVERT is true, invert the value of VAR before doing the OR.
5628 Return NULL_EXPR if we can't simplify this to a single expression. */
5629
5630static tree
5631or_var_with_comparison (tree var, bool invert,
5632 enum tree_code code2, tree op2a, tree op2b)
5633{
5634 tree t;
355fe088 5635 gimple *stmt = SSA_NAME_DEF_STMT (var);
e89065a1
SL
5636
5637 /* We can only deal with variables whose definitions are assignments. */
5638 if (!is_gimple_assign (stmt))
5639 return NULL_TREE;
5640
5641 /* If we have an inverted comparison, apply DeMorgan's law and rewrite
5642 !var OR (op2a code2 op2b) => !(var AND !(op2a code2 op2b))
5643 Then we only have to consider the simpler non-inverted cases. */
5644 if (invert)
5645 t = and_var_with_comparison_1 (stmt,
5646 invert_tree_comparison (code2, false),
5647 op2a, op2b);
5648 else
5649 t = or_var_with_comparison_1 (stmt, code2, op2a, op2b);
5650 return canonicalize_bool (t, invert);
5651}
5652
5653/* Try to simplify the OR of the ssa variable defined by the assignment
5654 STMT with the comparison specified by (OP2A CODE2 OP2B).
5655 Return NULL_EXPR if we can't simplify this to a single expression. */
5656
5657static tree
355fe088 5658or_var_with_comparison_1 (gimple *stmt,
e89065a1
SL
5659 enum tree_code code2, tree op2a, tree op2b)
5660{
5661 tree var = gimple_assign_lhs (stmt);
5662 tree true_test_var = NULL_TREE;
5663 tree false_test_var = NULL_TREE;
5664 enum tree_code innercode = gimple_assign_rhs_code (stmt);
5665
5666 /* Check for identities like (var OR (var != 0)) => true . */
5667 if (TREE_CODE (op2a) == SSA_NAME
5668 && TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE)
5669 {
5670 if ((code2 == NE_EXPR && integer_zerop (op2b))
5671 || (code2 == EQ_EXPR && integer_nonzerop (op2b)))
5672 {
5673 true_test_var = op2a;
5674 if (var == true_test_var)
5675 return var;
5676 }
5677 else if ((code2 == EQ_EXPR && integer_zerop (op2b))
5678 || (code2 == NE_EXPR && integer_nonzerop (op2b)))
5679 {
5680 false_test_var = op2a;
5681 if (var == false_test_var)
5682 return boolean_true_node;
5683 }
5684 }
5685
5686 /* If the definition is a comparison, recurse on it. */
5687 if (TREE_CODE_CLASS (innercode) == tcc_comparison)
5688 {
5689 tree t = or_comparisons_1 (innercode,
5690 gimple_assign_rhs1 (stmt),
5691 gimple_assign_rhs2 (stmt),
5692 code2,
5693 op2a,
5694 op2b);
5695 if (t)
5696 return t;
5697 }
5698
5699 /* If the definition is an AND or OR expression, we may be able to
5700 simplify by reassociating. */
eb9820c0
KT
5701 if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
5702 && (innercode == BIT_AND_EXPR || innercode == BIT_IOR_EXPR))
e89065a1
SL
5703 {
5704 tree inner1 = gimple_assign_rhs1 (stmt);
5705 tree inner2 = gimple_assign_rhs2 (stmt);
355fe088 5706 gimple *s;
e89065a1
SL
5707 tree t;
5708 tree partial = NULL_TREE;
eb9820c0 5709 bool is_or = (innercode == BIT_IOR_EXPR);
e89065a1
SL
5710
5711 /* Check for boolean identities that don't require recursive examination
5712 of inner1/inner2:
5713 inner1 OR (inner1 OR inner2) => inner1 OR inner2 => var
5714 inner1 OR (inner1 AND inner2) => inner1
5715 !inner1 OR (inner1 OR inner2) => true
5716 !inner1 OR (inner1 AND inner2) => !inner1 OR inner2
5717 */
5718 if (inner1 == true_test_var)
5719 return (is_or ? var : inner1);
5720 else if (inner2 == true_test_var)
5721 return (is_or ? var : inner2);
5722 else if (inner1 == false_test_var)
5723 return (is_or
5724 ? boolean_true_node
5725 : or_var_with_comparison (inner2, false, code2, op2a, op2b));
5726 else if (inner2 == false_test_var)
5727 return (is_or
5728 ? boolean_true_node
5729 : or_var_with_comparison (inner1, false, code2, op2a, op2b));
5730
5731 /* Next, redistribute/reassociate the OR across the inner tests.
5732 Compute the first partial result, (inner1 OR (op2a code op2b)) */
5733 if (TREE_CODE (inner1) == SSA_NAME
5734 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner1))
5735 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5736 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
5737 gimple_assign_rhs1 (s),
5738 gimple_assign_rhs2 (s),
5739 code2, op2a, op2b)))
5740 {
5741 /* Handle the OR case, where we are reassociating:
5742 (inner1 OR inner2) OR (op2a code2 op2b)
5743 => (t OR inner2)
5744 If the partial result t is a constant, we win. Otherwise
5745 continue on to try reassociating with the other inner test. */
8236c8eb 5746 if (is_or)
e89065a1
SL
5747 {
5748 if (integer_onep (t))
5749 return boolean_true_node;
5750 else if (integer_zerop (t))
5751 return inner2;
5752 }
5753
5754 /* Handle the AND case, where we are redistributing:
5755 (inner1 AND inner2) OR (op2a code2 op2b)
5756 => (t AND (inner2 OR (op2a code op2b))) */
8236c8eb
JJ
5757 else if (integer_zerop (t))
5758 return boolean_false_node;
5759
5760 /* Save partial result for later. */
5761 partial = t;
e89065a1
SL
5762 }
5763
5764 /* Compute the second partial result, (inner2 OR (op2a code op2b)) */
5765 if (TREE_CODE (inner2) == SSA_NAME
5766 && is_gimple_assign (s = SSA_NAME_DEF_STMT (inner2))
5767 && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison
5768 && (t = maybe_fold_or_comparisons (gimple_assign_rhs_code (s),
5769 gimple_assign_rhs1 (s),
5770 gimple_assign_rhs2 (s),
5771 code2, op2a, op2b)))
5772 {
5773 /* Handle the OR case, where we are reassociating:
5774 (inner1 OR inner2) OR (op2a code2 op2b)
8236c8eb
JJ
5775 => (inner1 OR t)
5776 => (t OR partial) */
5777 if (is_or)
e89065a1
SL
5778 {
5779 if (integer_zerop (t))
5780 return inner1;
5781 else if (integer_onep (t))
5782 return boolean_true_node;
8236c8eb
JJ
5783 /* If both are the same, we can apply the identity
5784 (x OR x) == x. */
5785 else if (partial && same_bool_result_p (t, partial))
5786 return t;
e89065a1
SL
5787 }
5788
5789 /* Handle the AND case, where we are redistributing:
5790 (inner1 AND inner2) OR (op2a code2 op2b)
5791 => (t AND (inner1 OR (op2a code2 op2b)))
5792 => (t AND partial) */
5793 else
5794 {
5795 if (integer_zerop (t))
5796 return boolean_false_node;
5797 else if (partial)
5798 {
5799 /* We already got a simplification for the other
5800 operand to the redistributed AND expression. The
5801 interesting case is when at least one is true.
5802 Or, if both are the same, we can apply the identity
8236c8eb 5803 (x AND x) == x. */
e89065a1
SL
5804 if (integer_onep (partial))
5805 return t;
5806 else if (integer_onep (t))
5807 return partial;
5808 else if (same_bool_result_p (t, partial))
8236c8eb 5809 return t;
e89065a1
SL
5810 }
5811 }
5812 }
5813 }
5814 return NULL_TREE;
5815}
5816
5817/* Try to simplify the OR of two comparisons defined by
5818 (OP1A CODE1 OP1B) and (OP2A CODE2 OP2B), respectively.
5819 If this can be done without constructing an intermediate value,
5820 return the resulting tree; otherwise NULL_TREE is returned.
5821 This function is deliberately asymmetric as it recurses on SSA_DEFs
5822 in the first comparison but not the second. */
5823
5824static tree
5825or_comparisons_1 (enum tree_code code1, tree op1a, tree op1b,
5826 enum tree_code code2, tree op2a, tree op2b)
5827{
ae22ac3c 5828 tree truth_type = truth_type_for (TREE_TYPE (op1a));
31ed6226 5829
e89065a1
SL
5830 /* First check for ((x CODE1 y) OR (x CODE2 y)). */
5831 if (operand_equal_p (op1a, op2a, 0)
5832 && operand_equal_p (op1b, op2b, 0))
5833 {
eb9820c0 5834 /* Result will be either NULL_TREE, or a combined comparison. */
e89065a1
SL
5835 tree t = combine_comparisons (UNKNOWN_LOCATION,
5836 TRUTH_ORIF_EXPR, code1, code2,
31ed6226 5837 truth_type, op1a, op1b);
e89065a1
SL
5838 if (t)
5839 return t;
5840 }
5841
5842 /* Likewise the swapped case of the above. */
5843 if (operand_equal_p (op1a, op2b, 0)
5844 && operand_equal_p (op1b, op2a, 0))
5845 {
eb9820c0 5846 /* Result will be either NULL_TREE, or a combined comparison. */
e89065a1
SL
5847 tree t = combine_comparisons (UNKNOWN_LOCATION,
5848 TRUTH_ORIF_EXPR, code1,
5849 swap_tree_comparison (code2),
31ed6226 5850 truth_type, op1a, op1b);
e89065a1
SL
5851 if (t)
5852 return t;
5853 }
5854
5855 /* If both comparisons are of the same value against constants, we might
5856 be able to merge them. */
5857 if (operand_equal_p (op1a, op2a, 0)
5858 && TREE_CODE (op1b) == INTEGER_CST
5859 && TREE_CODE (op2b) == INTEGER_CST)
5860 {
5861 int cmp = tree_int_cst_compare (op1b, op2b);
5862
5863 /* If we have (op1a != op1b), we should either be able to
5864 return that or TRUE, depending on whether the constant op1b
5865 also satisfies the other comparison against op2b. */
5866 if (code1 == NE_EXPR)
5867 {
5868 bool done = true;
5869 bool val;
5870 switch (code2)
5871 {
5872 case EQ_EXPR: val = (cmp == 0); break;
5873 case NE_EXPR: val = (cmp != 0); break;
5874 case LT_EXPR: val = (cmp < 0); break;
5875 case GT_EXPR: val = (cmp > 0); break;
5876 case LE_EXPR: val = (cmp <= 0); break;
5877 case GE_EXPR: val = (cmp >= 0); break;
5878 default: done = false;
5879 }
5880 if (done)
5881 {
5882 if (val)
5883 return boolean_true_node;
5884 else
5885 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5886 }
5887 }
5888 /* Likewise if the second comparison is a != comparison. */
5889 else if (code2 == NE_EXPR)
5890 {
5891 bool done = true;
5892 bool val;
5893 switch (code1)
5894 {
5895 case EQ_EXPR: val = (cmp == 0); break;
5896 case NE_EXPR: val = (cmp != 0); break;
5897 case LT_EXPR: val = (cmp > 0); break;
5898 case GT_EXPR: val = (cmp < 0); break;
5899 case LE_EXPR: val = (cmp >= 0); break;
5900 case GE_EXPR: val = (cmp <= 0); break;
5901 default: done = false;
5902 }
5903 if (done)
5904 {
5905 if (val)
5906 return boolean_true_node;
5907 else
5908 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5909 }
5910 }
5911
5912 /* See if an equality test is redundant with the other comparison. */
5913 else if (code1 == EQ_EXPR)
5914 {
5915 bool val;
5916 switch (code2)
5917 {
5918 case EQ_EXPR: val = (cmp == 0); break;
5919 case NE_EXPR: val = (cmp != 0); break;
5920 case LT_EXPR: val = (cmp < 0); break;
5921 case GT_EXPR: val = (cmp > 0); break;
5922 case LE_EXPR: val = (cmp <= 0); break;
5923 case GE_EXPR: val = (cmp >= 0); break;
5924 default:
5925 val = false;
5926 }
5927 if (val)
5928 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5929 }
5930 else if (code2 == EQ_EXPR)
5931 {
5932 bool val;
5933 switch (code1)
5934 {
5935 case EQ_EXPR: val = (cmp == 0); break;
5936 case NE_EXPR: val = (cmp != 0); break;
5937 case LT_EXPR: val = (cmp > 0); break;
5938 case GT_EXPR: val = (cmp < 0); break;
5939 case LE_EXPR: val = (cmp >= 0); break;
5940 case GE_EXPR: val = (cmp <= 0); break;
5941 default:
5942 val = false;
5943 }
5944 if (val)
5945 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5946 }
5947
5948 /* Chose the less restrictive of two < or <= comparisons. */
5949 else if ((code1 == LT_EXPR || code1 == LE_EXPR)
5950 && (code2 == LT_EXPR || code2 == LE_EXPR))
5951 {
5952 if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
5953 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5954 else
5955 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5956 }
5957
5958 /* Likewise chose the less restrictive of two > or >= comparisons. */
5959 else if ((code1 == GT_EXPR || code1 == GE_EXPR)
5960 && (code2 == GT_EXPR || code2 == GE_EXPR))
5961 {
5962 if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
5963 return fold_build2 (code2, boolean_type_node, op2a, op2b);
5964 else
5965 return fold_build2 (code1, boolean_type_node, op1a, op1b);
5966 }
5967
5968 /* Check for singleton ranges. */
5969 else if (cmp == 0
5970 && ((code1 == LT_EXPR && code2 == GT_EXPR)
5971 || (code1 == GT_EXPR && code2 == LT_EXPR)))
5972 return fold_build2 (NE_EXPR, boolean_type_node, op1a, op2b);
5973
5974 /* Check for less/greater pairs that don't restrict the range at all. */
5975 else if (cmp >= 0
5976 && (code1 == LT_EXPR || code1 == LE_EXPR)
5977 && (code2 == GT_EXPR || code2 == GE_EXPR))
5978 return boolean_true_node;
5979 else if (cmp <= 0
5980 && (code1 == GT_EXPR || code1 == GE_EXPR)
5981 && (code2 == LT_EXPR || code2 == LE_EXPR))
5982 return boolean_true_node;
5983 }
5984
5985 /* Perhaps the first comparison is (NAME != 0) or (NAME == 1) where
5986 NAME's definition is a truth value. See if there are any simplifications
5987 that can be done against the NAME's definition. */
5988 if (TREE_CODE (op1a) == SSA_NAME
5989 && (code1 == NE_EXPR || code1 == EQ_EXPR)
5990 && (integer_zerop (op1b) || integer_onep (op1b)))
5991 {
5992 bool invert = ((code1 == EQ_EXPR && integer_zerop (op1b))
5993 || (code1 == NE_EXPR && integer_onep (op1b)));
355fe088 5994 gimple *stmt = SSA_NAME_DEF_STMT (op1a);
e89065a1
SL
5995 switch (gimple_code (stmt))
5996 {
5997 case GIMPLE_ASSIGN:
5998 /* Try to simplify by copy-propagating the definition. */
5999 return or_var_with_comparison (op1a, invert, code2, op2a, op2b);
6000
6001 case GIMPLE_PHI:
6002 /* If every argument to the PHI produces the same result when
6003 ORed with the second comparison, we win.
6004 Do not do this unless the type is bool since we need a bool
6005 result here anyway. */
6006 if (TREE_CODE (TREE_TYPE (op1a)) == BOOLEAN_TYPE)
6007 {
6008 tree result = NULL_TREE;
6009 unsigned i;
6010 for (i = 0; i < gimple_phi_num_args (stmt); i++)
6011 {
6012 tree arg = gimple_phi_arg_def (stmt, i);
6013
6014 /* If this PHI has itself as an argument, ignore it.
6015 If all the other args produce the same result,
6016 we're still OK. */
6017 if (arg == gimple_phi_result (stmt))
6018 continue;
6019 else if (TREE_CODE (arg) == INTEGER_CST)
6020 {
6021 if (invert ? integer_zerop (arg) : integer_nonzerop (arg))
6022 {
6023 if (!result)
6024 result = boolean_true_node;
6025 else if (!integer_onep (result))
6026 return NULL_TREE;
6027 }
6028 else if (!result)
6029 result = fold_build2 (code2, boolean_type_node,
6030 op2a, op2b);
6031 else if (!same_bool_comparison_p (result,
6032 code2, op2a, op2b))
6033 return NULL_TREE;
6034 }
0e8b84ec
JJ
6035 else if (TREE_CODE (arg) == SSA_NAME
6036 && !SSA_NAME_IS_DEFAULT_DEF (arg))
e89065a1 6037 {
6c66f733 6038 tree temp;
355fe088 6039 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
6c66f733
JJ
6040 /* In simple cases we can look through PHI nodes,
6041 but we have to be careful with loops.
6042 See PR49073. */
6043 if (! dom_info_available_p (CDI_DOMINATORS)
6044 || gimple_bb (def_stmt) == gimple_bb (stmt)
6045 || dominated_by_p (CDI_DOMINATORS,
6046 gimple_bb (def_stmt),
6047 gimple_bb (stmt)))
6048 return NULL_TREE;
6049 temp = or_var_with_comparison (arg, invert, code2,
6050 op2a, op2b);
e89065a1
SL
6051 if (!temp)
6052 return NULL_TREE;
6053 else if (!result)
6054 result = temp;
6055 else if (!same_bool_result_p (result, temp))
6056 return NULL_TREE;
6057 }
6058 else
6059 return NULL_TREE;
6060 }
6061 return result;
6062 }
6063
6064 default:
6065 break;
6066 }
6067 }
6068 return NULL_TREE;
6069}
6070
6071/* Try to simplify the OR of two comparisons, specified by
6072 (OP1A CODE1 OP1B) and (OP2B CODE2 OP2B), respectively.
6073 If this can be simplified to a single expression (without requiring
6074 introducing more SSA variables to hold intermediate values),
6075 return the resulting tree. Otherwise return NULL_TREE.
6076 If the result expression is non-null, it has boolean type. */
6077
6078tree
6079maybe_fold_or_comparisons (enum tree_code code1, tree op1a, tree op1b,
6080 enum tree_code code2, tree op2a, tree op2b)
6081{
6082 tree t = or_comparisons_1 (code1, op1a, op1b, code2, op2a, op2b);
6083 if (t)
6084 return t;
6085 else
6086 return or_comparisons_1 (code2, op2a, op2b, code1, op1a, op1b);
6087}
cfef45c8
RG
6088
6089
6090/* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6091
6092 Either NULL_TREE, a simplified but non-constant or a constant
6093 is returned.
6094
6095 ??? This should go into a gimple-fold-inline.h file to be eventually
6096 privatized with the single valueize function used in the various TUs
6097 to avoid the indirect function call overhead. */
6098
6099tree
355fe088 6100gimple_fold_stmt_to_constant_1 (gimple *stmt, tree (*valueize) (tree),
d2a85801 6101 tree (*gvalueize) (tree))
cfef45c8 6102{
5d75ad95 6103 gimple_match_op res_op;
45cc9f96
RB
6104 /* ??? The SSA propagators do not correctly deal with following SSA use-def
6105 edges if there are intermediate VARYING defs. For this reason
6106 do not follow SSA edges here even though SCCVN can technically
6107 just deal fine with that. */
5d75ad95 6108 if (gimple_simplify (stmt, &res_op, NULL, gvalueize, valueize))
45cc9f96 6109 {
34050b6b 6110 tree res = NULL_TREE;
5d75ad95
RS
6111 if (gimple_simplified_result_is_gimple_val (&res_op))
6112 res = res_op.ops[0];
34050b6b 6113 else if (mprts_hook)
5d75ad95 6114 res = mprts_hook (&res_op);
34050b6b 6115 if (res)
45cc9f96 6116 {
34050b6b
RB
6117 if (dump_file && dump_flags & TDF_DETAILS)
6118 {
6119 fprintf (dump_file, "Match-and-simplified ");
6120 print_gimple_expr (dump_file, stmt, 0, TDF_SLIM);
6121 fprintf (dump_file, " to ");
ef6cb4c7 6122 print_generic_expr (dump_file, res);
34050b6b
RB
6123 fprintf (dump_file, "\n");
6124 }
6125 return res;
45cc9f96 6126 }
45cc9f96
RB
6127 }
6128
cfef45c8
RG
6129 location_t loc = gimple_location (stmt);
6130 switch (gimple_code (stmt))
6131 {
6132 case GIMPLE_ASSIGN:
6133 {
6134 enum tree_code subcode = gimple_assign_rhs_code (stmt);
6135
6136 switch (get_gimple_rhs_class (subcode))
6137 {
6138 case GIMPLE_SINGLE_RHS:
6139 {
6140 tree rhs = gimple_assign_rhs1 (stmt);
6141 enum tree_code_class kind = TREE_CODE_CLASS (subcode);
6142
6143 if (TREE_CODE (rhs) == SSA_NAME)
6144 {
6145 /* If the RHS is an SSA_NAME, return its known constant value,
6146 if any. */
6147 return (*valueize) (rhs);
6148 }
6149 /* Handle propagating invariant addresses into address
6150 operations. */
6151 else if (TREE_CODE (rhs) == ADDR_EXPR
6152 && !is_gimple_min_invariant (rhs))
6153 {
a90c8804 6154 poly_int64 offset = 0;
cfef45c8
RG
6155 tree base;
6156 base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (rhs, 0),
6157 &offset,
6158 valueize);
6159 if (base
6160 && (CONSTANT_CLASS_P (base)
6161 || decl_address_invariant_p (base)))
6162 return build_invariant_address (TREE_TYPE (rhs),
6163 base, offset);
6164 }
6165 else if (TREE_CODE (rhs) == CONSTRUCTOR
6166 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE
928686b1
RS
6167 && known_eq (CONSTRUCTOR_NELTS (rhs),
6168 TYPE_VECTOR_SUBPARTS (TREE_TYPE (rhs))))
cfef45c8 6169 {
794e3180
RS
6170 unsigned i, nelts;
6171 tree val;
cfef45c8 6172
928686b1 6173 nelts = CONSTRUCTOR_NELTS (rhs);
5ebaa477 6174 tree_vector_builder vec (TREE_TYPE (rhs), nelts, 1);
cfef45c8
RG
6175 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), i, val)
6176 {
6177 val = (*valueize) (val);
6178 if (TREE_CODE (val) == INTEGER_CST
6179 || TREE_CODE (val) == REAL_CST
6180 || TREE_CODE (val) == FIXED_CST)
794e3180 6181 vec.quick_push (val);
cfef45c8
RG
6182 else
6183 return NULL_TREE;
6184 }
6185
5ebaa477 6186 return vec.build ();
cfef45c8 6187 }
bdf37f7a
JH
6188 if (subcode == OBJ_TYPE_REF)
6189 {
6190 tree val = (*valueize) (OBJ_TYPE_REF_EXPR (rhs));
6191 /* If callee is constant, we can fold away the wrapper. */
6192 if (is_gimple_min_invariant (val))
6193 return val;
6194 }
cfef45c8
RG
6195
6196 if (kind == tcc_reference)
6197 {
6198 if ((TREE_CODE (rhs) == VIEW_CONVERT_EXPR
6199 || TREE_CODE (rhs) == REALPART_EXPR
6200 || TREE_CODE (rhs) == IMAGPART_EXPR)
6201 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
6202 {
6203 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
6204 return fold_unary_loc (EXPR_LOCATION (rhs),
6205 TREE_CODE (rhs),
6206 TREE_TYPE (rhs), val);
6207 }
6208 else if (TREE_CODE (rhs) == BIT_FIELD_REF
6209 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
6210 {
6211 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
6212 return fold_ternary_loc (EXPR_LOCATION (rhs),
6213 TREE_CODE (rhs),
6214 TREE_TYPE (rhs), val,
6215 TREE_OPERAND (rhs, 1),
6216 TREE_OPERAND (rhs, 2));
6217 }
6218 else if (TREE_CODE (rhs) == MEM_REF
6219 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
6220 {
6221 tree val = (*valueize) (TREE_OPERAND (rhs, 0));
6222 if (TREE_CODE (val) == ADDR_EXPR
6223 && is_gimple_min_invariant (val))
6224 {
6225 tree tem = fold_build2 (MEM_REF, TREE_TYPE (rhs),
6226 unshare_expr (val),
6227 TREE_OPERAND (rhs, 1));
6228 if (tem)
6229 rhs = tem;
6230 }
6231 }
6232 return fold_const_aggregate_ref_1 (rhs, valueize);
6233 }
6234 else if (kind == tcc_declaration)
6235 return get_symbol_constant_value (rhs);
6236 return rhs;
6237 }
6238
6239 case GIMPLE_UNARY_RHS:
f3582e54 6240 return NULL_TREE;
cfef45c8
RG
6241
6242 case GIMPLE_BINARY_RHS:
4b1b9e64
RB
6243 /* Translate &x + CST into an invariant form suitable for
6244 further propagation. */
6245 if (subcode == POINTER_PLUS_EXPR)
6246 {
4b1b9e64
RB
6247 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
6248 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
4b1b9e64
RB
6249 if (TREE_CODE (op0) == ADDR_EXPR
6250 && TREE_CODE (op1) == INTEGER_CST)
6251 {
6252 tree off = fold_convert (ptr_type_node, op1);
6253 return build_fold_addr_expr_loc
6254 (loc,
6255 fold_build2 (MEM_REF,
6256 TREE_TYPE (TREE_TYPE (op0)),
6257 unshare_expr (op0), off));
6258 }
6259 }
59c20dc7
RB
6260 /* Canonicalize bool != 0 and bool == 0 appearing after
6261 valueization. While gimple_simplify handles this
6262 it can get confused by the ~X == 1 -> X == 0 transform
6263 which we cant reduce to a SSA name or a constant
6264 (and we have no way to tell gimple_simplify to not
6265 consider those transforms in the first place). */
6266 else if (subcode == EQ_EXPR
6267 || subcode == NE_EXPR)
6268 {
6269 tree lhs = gimple_assign_lhs (stmt);
6270 tree op0 = gimple_assign_rhs1 (stmt);
6271 if (useless_type_conversion_p (TREE_TYPE (lhs),
6272 TREE_TYPE (op0)))
6273 {
6274 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
6275 op0 = (*valueize) (op0);
8861704d
RB
6276 if (TREE_CODE (op0) == INTEGER_CST)
6277 std::swap (op0, op1);
6278 if (TREE_CODE (op1) == INTEGER_CST
6279 && ((subcode == NE_EXPR && integer_zerop (op1))
6280 || (subcode == EQ_EXPR && integer_onep (op1))))
6281 return op0;
59c20dc7
RB
6282 }
6283 }
4b1b9e64 6284 return NULL_TREE;
cfef45c8
RG
6285
6286 case GIMPLE_TERNARY_RHS:
6287 {
6288 /* Handle ternary operators that can appear in GIMPLE form. */
6289 tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
6290 tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
6291 tree op2 = (*valueize) (gimple_assign_rhs3 (stmt));
cfef45c8
RG
6292 return fold_ternary_loc (loc, subcode,
6293 gimple_expr_type (stmt), op0, op1, op2);
6294 }
6295
6296 default:
6297 gcc_unreachable ();
6298 }
6299 }
6300
6301 case GIMPLE_CALL:
6302 {
25583c4f 6303 tree fn;
538dd0b7 6304 gcall *call_stmt = as_a <gcall *> (stmt);
25583c4f
RS
6305
6306 if (gimple_call_internal_p (stmt))
31e071ae
MP
6307 {
6308 enum tree_code subcode = ERROR_MARK;
6309 switch (gimple_call_internal_fn (stmt))
6310 {
6311 case IFN_UBSAN_CHECK_ADD:
6312 subcode = PLUS_EXPR;
6313 break;
6314 case IFN_UBSAN_CHECK_SUB:
6315 subcode = MINUS_EXPR;
6316 break;
6317 case IFN_UBSAN_CHECK_MUL:
6318 subcode = MULT_EXPR;
6319 break;
68fa96d6
ML
6320 case IFN_BUILTIN_EXPECT:
6321 {
6322 tree arg0 = gimple_call_arg (stmt, 0);
6323 tree op0 = (*valueize) (arg0);
6324 if (TREE_CODE (op0) == INTEGER_CST)
6325 return op0;
6326 return NULL_TREE;
6327 }
31e071ae
MP
6328 default:
6329 return NULL_TREE;
6330 }
368b454d
JJ
6331 tree arg0 = gimple_call_arg (stmt, 0);
6332 tree arg1 = gimple_call_arg (stmt, 1);
6333 tree op0 = (*valueize) (arg0);
6334 tree op1 = (*valueize) (arg1);
31e071ae
MP
6335
6336 if (TREE_CODE (op0) != INTEGER_CST
6337 || TREE_CODE (op1) != INTEGER_CST)
368b454d
JJ
6338 {
6339 switch (subcode)
6340 {
6341 case MULT_EXPR:
6342 /* x * 0 = 0 * x = 0 without overflow. */
6343 if (integer_zerop (op0) || integer_zerop (op1))
6344 return build_zero_cst (TREE_TYPE (arg0));
6345 break;
6346 case MINUS_EXPR:
6347 /* y - y = 0 without overflow. */
6348 if (operand_equal_p (op0, op1, 0))
6349 return build_zero_cst (TREE_TYPE (arg0));
6350 break;
6351 default:
6352 break;
6353 }
6354 }
6355 tree res
6356 = fold_binary_loc (loc, subcode, TREE_TYPE (arg0), op0, op1);
31e071ae
MP
6357 if (res
6358 && TREE_CODE (res) == INTEGER_CST
6359 && !TREE_OVERFLOW (res))
6360 return res;
6361 return NULL_TREE;
6362 }
25583c4f
RS
6363
6364 fn = (*valueize) (gimple_call_fn (stmt));
cfef45c8
RG
6365 if (TREE_CODE (fn) == ADDR_EXPR
6366 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5c944c6c
RB
6367 && DECL_BUILT_IN (TREE_OPERAND (fn, 0))
6368 && gimple_builtin_call_types_compatible_p (stmt,
6369 TREE_OPERAND (fn, 0)))
cfef45c8
RG
6370 {
6371 tree *args = XALLOCAVEC (tree, gimple_call_num_args (stmt));
a6a0570f 6372 tree retval;
cfef45c8
RG
6373 unsigned i;
6374 for (i = 0; i < gimple_call_num_args (stmt); ++i)
6375 args[i] = (*valueize) (gimple_call_arg (stmt, i));
a6a0570f 6376 retval = fold_builtin_call_array (loc,
538dd0b7 6377 gimple_call_return_type (call_stmt),
cfef45c8 6378 fn, gimple_call_num_args (stmt), args);
cfef45c8 6379 if (retval)
5c944c6c
RB
6380 {
6381 /* fold_call_expr wraps the result inside a NOP_EXPR. */
6382 STRIP_NOPS (retval);
538dd0b7
DM
6383 retval = fold_convert (gimple_call_return_type (call_stmt),
6384 retval);
5c944c6c 6385 }
cfef45c8
RG
6386 return retval;
6387 }
6388 return NULL_TREE;
6389 }
6390
6391 default:
6392 return NULL_TREE;
6393 }
6394}
6395
6396/* Fold STMT to a constant using VALUEIZE to valueize SSA names.
6397 Returns NULL_TREE if folding to a constant is not possible, otherwise
6398 returns a constant according to is_gimple_min_invariant. */
6399
6400tree
355fe088 6401gimple_fold_stmt_to_constant (gimple *stmt, tree (*valueize) (tree))
cfef45c8
RG
6402{
6403 tree res = gimple_fold_stmt_to_constant_1 (stmt, valueize);
6404 if (res && is_gimple_min_invariant (res))
6405 return res;
6406 return NULL_TREE;
6407}
6408
6409
6410/* The following set of functions are supposed to fold references using
6411 their constant initializers. */
6412
cfef45c8
RG
6413/* See if we can find constructor defining value of BASE.
6414 When we know the consructor with constant offset (such as
6415 base is array[40] and we do know constructor of array), then
6416 BIT_OFFSET is adjusted accordingly.
6417
6418 As a special case, return error_mark_node when constructor
6419 is not explicitly available, but it is known to be zero
6420 such as 'static const int a;'. */
6421static tree
588db50c 6422get_base_constructor (tree base, poly_int64_pod *bit_offset,
cfef45c8
RG
6423 tree (*valueize)(tree))
6424{
588db50c 6425 poly_int64 bit_offset2, size, max_size;
ee45a32d
EB
6426 bool reverse;
6427
cfef45c8
RG
6428 if (TREE_CODE (base) == MEM_REF)
6429 {
6a5aca53
ML
6430 poly_offset_int boff = *bit_offset + mem_ref_offset (base) * BITS_PER_UNIT;
6431 if (!boff.to_shwi (bit_offset))
6432 return NULL_TREE;
cfef45c8
RG
6433
6434 if (valueize
6435 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
6436 base = valueize (TREE_OPERAND (base, 0));
6437 if (!base || TREE_CODE (base) != ADDR_EXPR)
6438 return NULL_TREE;
6439 base = TREE_OPERAND (base, 0);
6440 }
13e88953
RB
6441 else if (valueize
6442 && TREE_CODE (base) == SSA_NAME)
6443 base = valueize (base);
cfef45c8
RG
6444
6445 /* Get a CONSTRUCTOR. If BASE is a VAR_DECL, get its
6446 DECL_INITIAL. If BASE is a nested reference into another
6447 ARRAY_REF or COMPONENT_REF, make a recursive call to resolve
6448 the inner reference. */
6449 switch (TREE_CODE (base))
6450 {
6451 case VAR_DECL:
cfef45c8 6452 case CONST_DECL:
6a6dac52
JH
6453 {
6454 tree init = ctor_for_folding (base);
6455
688010ba 6456 /* Our semantic is exact opposite of ctor_for_folding;
6a6dac52
JH
6457 NULL means unknown, while error_mark_node is 0. */
6458 if (init == error_mark_node)
6459 return NULL_TREE;
6460 if (!init)
6461 return error_mark_node;
6462 return init;
6463 }
cfef45c8 6464
13e88953
RB
6465 case VIEW_CONVERT_EXPR:
6466 return get_base_constructor (TREE_OPERAND (base, 0),
6467 bit_offset, valueize);
6468
cfef45c8
RG
6469 case ARRAY_REF:
6470 case COMPONENT_REF:
ee45a32d
EB
6471 base = get_ref_base_and_extent (base, &bit_offset2, &size, &max_size,
6472 &reverse);
588db50c 6473 if (!known_size_p (max_size) || maybe_ne (size, max_size))
cfef45c8
RG
6474 return NULL_TREE;
6475 *bit_offset += bit_offset2;
6476 return get_base_constructor (base, bit_offset, valueize);
6477
cfef45c8
RG
6478 case CONSTRUCTOR:
6479 return base;
6480
6481 default:
13e88953
RB
6482 if (CONSTANT_CLASS_P (base))
6483 return base;
6484
cfef45c8
RG
6485 return NULL_TREE;
6486 }
6487}
6488
cfef45c8
RG
6489/* CTOR is CONSTRUCTOR of an array type. Fold reference of type TYPE and size
6490 SIZE to the memory at bit OFFSET. */
6491
6492static tree
6493fold_array_ctor_reference (tree type, tree ctor,
6494 unsigned HOST_WIDE_INT offset,
c44c2088
JH
6495 unsigned HOST_WIDE_INT size,
6496 tree from_decl)
cfef45c8 6497{
807e902e
KZ
6498 offset_int low_bound;
6499 offset_int elt_size;
807e902e 6500 offset_int access_index;
6a636014 6501 tree domain_type = NULL_TREE;
cfef45c8
RG
6502 HOST_WIDE_INT inner_offset;
6503
6504 /* Compute low bound and elt size. */
eb8f1123
RG
6505 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
6506 domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
cfef45c8
RG
6507 if (domain_type && TYPE_MIN_VALUE (domain_type))
6508 {
6509 /* Static constructors for variably sized objects makes no sense. */
9ef2eff0
RB
6510 if (TREE_CODE (TYPE_MIN_VALUE (domain_type)) != INTEGER_CST)
6511 return NULL_TREE;
807e902e 6512 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
cfef45c8
RG
6513 }
6514 else
807e902e 6515 low_bound = 0;
cfef45c8 6516 /* Static constructors for variably sized objects makes no sense. */
9ef2eff0
RB
6517 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor)))) != INTEGER_CST)
6518 return NULL_TREE;
807e902e 6519 elt_size = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ctor))));
cfef45c8
RG
6520
6521 /* We can handle only constantly sized accesses that are known to not
6522 be larger than size of array element. */
6523 if (!TYPE_SIZE_UNIT (type)
6524 || TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
032c80e9 6525 || elt_size < wi::to_offset (TYPE_SIZE_UNIT (type))
807e902e 6526 || elt_size == 0)
cfef45c8
RG
6527 return NULL_TREE;
6528
6529 /* Compute the array index we look for. */
807e902e
KZ
6530 access_index = wi::udiv_trunc (offset_int (offset / BITS_PER_UNIT),
6531 elt_size);
27bcd47c 6532 access_index += low_bound;
cfef45c8
RG
6533
6534 /* And offset within the access. */
27bcd47c 6535 inner_offset = offset % (elt_size.to_uhwi () * BITS_PER_UNIT);
cfef45c8
RG
6536
6537 /* See if the array field is large enough to span whole access. We do not
6538 care to fold accesses spanning multiple array indexes. */
27bcd47c 6539 if (inner_offset + size > elt_size.to_uhwi () * BITS_PER_UNIT)
cfef45c8 6540 return NULL_TREE;
6a636014
AL
6541 if (tree val = get_array_ctor_element_at_index (ctor, access_index))
6542 return fold_ctor_reference (type, val, inner_offset, size, from_decl);
cfef45c8 6543
cfef45c8
RG
6544 /* When memory is not explicitely mentioned in constructor,
6545 it is 0 (or out of range). */
6546 return build_zero_cst (type);
6547}
6548
6549/* CTOR is CONSTRUCTOR of an aggregate or vector.
6550 Fold reference of type TYPE and size SIZE to the memory at bit OFFSET. */
6551
6552static tree
6553fold_nonarray_ctor_reference (tree type, tree ctor,
6554 unsigned HOST_WIDE_INT offset,
c44c2088
JH
6555 unsigned HOST_WIDE_INT size,
6556 tree from_decl)
cfef45c8
RG
6557{
6558 unsigned HOST_WIDE_INT cnt;
6559 tree cfield, cval;
6560
6561 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield,
6562 cval)
6563 {
6564 tree byte_offset = DECL_FIELD_OFFSET (cfield);
6565 tree field_offset = DECL_FIELD_BIT_OFFSET (cfield);
6566 tree field_size = DECL_SIZE (cfield);
807e902e
KZ
6567 offset_int bitoffset;
6568 offset_int bitoffset_end, access_end;
cfef45c8
RG
6569
6570 /* Variable sized objects in static constructors makes no sense,
6571 but field_size can be NULL for flexible array members. */
6572 gcc_assert (TREE_CODE (field_offset) == INTEGER_CST
6573 && TREE_CODE (byte_offset) == INTEGER_CST
6574 && (field_size != NULL_TREE
6575 ? TREE_CODE (field_size) == INTEGER_CST
6576 : TREE_CODE (TREE_TYPE (cfield)) == ARRAY_TYPE));
6577
6578 /* Compute bit offset of the field. */
807e902e 6579 bitoffset = (wi::to_offset (field_offset)
8de73453 6580 + (wi::to_offset (byte_offset) << LOG2_BITS_PER_UNIT));
cfef45c8
RG
6581 /* Compute bit offset where the field ends. */
6582 if (field_size != NULL_TREE)
807e902e 6583 bitoffset_end = bitoffset + wi::to_offset (field_size);
cfef45c8 6584 else
807e902e 6585 bitoffset_end = 0;
cfef45c8 6586
807e902e 6587 access_end = offset_int (offset) + size;
b8b2b009
JJ
6588
6589 /* Is there any overlap between [OFFSET, OFFSET+SIZE) and
6590 [BITOFFSET, BITOFFSET_END)? */
807e902e 6591 if (wi::cmps (access_end, bitoffset) > 0
cfef45c8 6592 && (field_size == NULL_TREE
807e902e 6593 || wi::lts_p (offset, bitoffset_end)))
cfef45c8 6594 {
807e902e 6595 offset_int inner_offset = offset_int (offset) - bitoffset;
cfef45c8
RG
6596 /* We do have overlap. Now see if field is large enough to
6597 cover the access. Give up for accesses spanning multiple
6598 fields. */
807e902e 6599 if (wi::cmps (access_end, bitoffset_end) > 0)
cfef45c8 6600 return NULL_TREE;
032c80e9 6601 if (offset < bitoffset)
b8b2b009 6602 return NULL_TREE;
cfef45c8 6603 return fold_ctor_reference (type, cval,
27bcd47c 6604 inner_offset.to_uhwi (), size,
c44c2088 6605 from_decl);
cfef45c8
RG
6606 }
6607 }
6608 /* When memory is not explicitely mentioned in constructor, it is 0. */
6609 return build_zero_cst (type);
6610}
6611
30acf282
RS
6612/* CTOR is value initializing memory, fold reference of type TYPE and
6613 size POLY_SIZE to the memory at bit POLY_OFFSET. */
cfef45c8 6614
8403c2cf 6615tree
30acf282
RS
6616fold_ctor_reference (tree type, tree ctor, poly_uint64 poly_offset,
6617 poly_uint64 poly_size, tree from_decl)
cfef45c8
RG
6618{
6619 tree ret;
6620
6621 /* We found the field with exact match. */
6622 if (useless_type_conversion_p (type, TREE_TYPE (ctor))
30acf282 6623 && known_eq (poly_offset, 0U))
9d60be38 6624 return canonicalize_constructor_val (unshare_expr (ctor), from_decl);
cfef45c8 6625
30acf282
RS
6626 /* The remaining optimizations need a constant size and offset. */
6627 unsigned HOST_WIDE_INT size, offset;
6628 if (!poly_size.is_constant (&size) || !poly_offset.is_constant (&offset))
6629 return NULL_TREE;
6630
cfef45c8
RG
6631 /* We are at the end of walk, see if we can view convert the
6632 result. */
6633 if (!AGGREGATE_TYPE_P (TREE_TYPE (ctor)) && !offset
6634 /* VIEW_CONVERT_EXPR is defined only for matching sizes. */
3d8208ce
TP
6635 && !compare_tree_int (TYPE_SIZE (type), size)
6636 && !compare_tree_int (TYPE_SIZE (TREE_TYPE (ctor)), size))
cfef45c8 6637 {
9d60be38 6638 ret = canonicalize_constructor_val (unshare_expr (ctor), from_decl);
cfef45c8 6639 if (ret)
672d9f8e
RB
6640 {
6641 ret = fold_unary (VIEW_CONVERT_EXPR, type, ret);
6642 if (ret)
6643 STRIP_USELESS_TYPE_CONVERSION (ret);
6644 }
cfef45c8
RG
6645 return ret;
6646 }
b2505143
RB
6647 /* For constants and byte-aligned/sized reads try to go through
6648 native_encode/interpret. */
6649 if (CONSTANT_CLASS_P (ctor)
6650 && BITS_PER_UNIT == 8
6651 && offset % BITS_PER_UNIT == 0
6652 && size % BITS_PER_UNIT == 0
6653 && size <= MAX_BITSIZE_MODE_ANY_MODE)
6654 {
6655 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1ff0a84c
JJ
6656 int len = native_encode_expr (ctor, buf, size / BITS_PER_UNIT,
6657 offset / BITS_PER_UNIT);
6658 if (len > 0)
6659 return native_interpret_expr (type, buf, len);
b2505143 6660 }
cfef45c8
RG
6661 if (TREE_CODE (ctor) == CONSTRUCTOR)
6662 {
6663
eb8f1123
RG
6664 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE
6665 || TREE_CODE (TREE_TYPE (ctor)) == VECTOR_TYPE)
c44c2088
JH
6666 return fold_array_ctor_reference (type, ctor, offset, size,
6667 from_decl);
cfef45c8 6668 else
c44c2088
JH
6669 return fold_nonarray_ctor_reference (type, ctor, offset, size,
6670 from_decl);
cfef45c8
RG
6671 }
6672
6673 return NULL_TREE;
6674}
6675
6676/* Return the tree representing the element referenced by T if T is an
6677 ARRAY_REF or COMPONENT_REF into constant aggregates valuezing SSA
6678 names using VALUEIZE. Return NULL_TREE otherwise. */
6679
6680tree
6681fold_const_aggregate_ref_1 (tree t, tree (*valueize) (tree))
6682{
6683 tree ctor, idx, base;
588db50c 6684 poly_int64 offset, size, max_size;
cfef45c8 6685 tree tem;
ee45a32d 6686 bool reverse;
cfef45c8 6687
f8a7df45
RG
6688 if (TREE_THIS_VOLATILE (t))
6689 return NULL_TREE;
6690
3a65ee74 6691 if (DECL_P (t))
cfef45c8
RG
6692 return get_symbol_constant_value (t);
6693
6694 tem = fold_read_from_constant_string (t);
6695 if (tem)
6696 return tem;
6697
6698 switch (TREE_CODE (t))
6699 {
6700 case ARRAY_REF:
6701 case ARRAY_RANGE_REF:
6702 /* Constant indexes are handled well by get_base_constructor.
6703 Only special case variable offsets.
6704 FIXME: This code can't handle nested references with variable indexes
6705 (they will be handled only by iteration of ccp). Perhaps we can bring
6706 get_ref_base_and_extent here and make it use a valueize callback. */
6707 if (TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME
6708 && valueize
6709 && (idx = (*valueize) (TREE_OPERAND (t, 1)))
588db50c 6710 && poly_int_tree_p (idx))
cfef45c8
RG
6711 {
6712 tree low_bound, unit_size;
6713
6714 /* If the resulting bit-offset is constant, track it. */
6715 if ((low_bound = array_ref_low_bound (t),
588db50c 6716 poly_int_tree_p (low_bound))
cfef45c8 6717 && (unit_size = array_ref_element_size (t),
807e902e 6718 tree_fits_uhwi_p (unit_size)))
cfef45c8 6719 {
588db50c
RS
6720 poly_offset_int woffset
6721 = wi::sext (wi::to_poly_offset (idx)
6722 - wi::to_poly_offset (low_bound),
807e902e
KZ
6723 TYPE_PRECISION (TREE_TYPE (idx)));
6724
588db50c 6725 if (woffset.to_shwi (&offset))
807e902e 6726 {
807e902e
KZ
6727 /* TODO: This code seems wrong, multiply then check
6728 to see if it fits. */
6729 offset *= tree_to_uhwi (unit_size);
6730 offset *= BITS_PER_UNIT;
6731
6732 base = TREE_OPERAND (t, 0);
6733 ctor = get_base_constructor (base, &offset, valueize);
6734 /* Empty constructor. Always fold to 0. */
6735 if (ctor == error_mark_node)
6736 return build_zero_cst (TREE_TYPE (t));
6737 /* Out of bound array access. Value is undefined,
6738 but don't fold. */
588db50c 6739 if (maybe_lt (offset, 0))
807e902e
KZ
6740 return NULL_TREE;
6741 /* We can not determine ctor. */
6742 if (!ctor)
6743 return NULL_TREE;
6744 return fold_ctor_reference (TREE_TYPE (t), ctor, offset,
6745 tree_to_uhwi (unit_size)
6746 * BITS_PER_UNIT,
6747 base);
6748 }
cfef45c8
RG
6749 }
6750 }
6751 /* Fallthru. */
6752
6753 case COMPONENT_REF:
6754 case BIT_FIELD_REF:
6755 case TARGET_MEM_REF:
6756 case MEM_REF:
ee45a32d 6757 base = get_ref_base_and_extent (t, &offset, &size, &max_size, &reverse);
cfef45c8
RG
6758 ctor = get_base_constructor (base, &offset, valueize);
6759
6760 /* Empty constructor. Always fold to 0. */
6761 if (ctor == error_mark_node)
6762 return build_zero_cst (TREE_TYPE (t));
6763 /* We do not know precise address. */
588db50c 6764 if (!known_size_p (max_size) || maybe_ne (max_size, size))
cfef45c8
RG
6765 return NULL_TREE;
6766 /* We can not determine ctor. */
6767 if (!ctor)
6768 return NULL_TREE;
6769
6770 /* Out of bound array access. Value is undefined, but don't fold. */
588db50c 6771 if (maybe_lt (offset, 0))
cfef45c8
RG
6772 return NULL_TREE;
6773
c44c2088
JH
6774 return fold_ctor_reference (TREE_TYPE (t), ctor, offset, size,
6775 base);
cfef45c8
RG
6776
6777 case REALPART_EXPR:
6778 case IMAGPART_EXPR:
6779 {
6780 tree c = fold_const_aggregate_ref_1 (TREE_OPERAND (t, 0), valueize);
6781 if (c && TREE_CODE (c) == COMPLEX_CST)
6782 return fold_build1_loc (EXPR_LOCATION (t),
6783 TREE_CODE (t), TREE_TYPE (t), c);
6784 break;
6785 }
6786
6787 default:
6788 break;
6789 }
6790
6791 return NULL_TREE;
6792}
6793
6794tree
6795fold_const_aggregate_ref (tree t)
6796{
6797 return fold_const_aggregate_ref_1 (t, NULL);
6798}
06bc3ec7 6799
85942f45 6800/* Lookup virtual method with index TOKEN in a virtual table V
ec77d61f
JH
6801 at OFFSET.
6802 Set CAN_REFER if non-NULL to false if method
6803 is not referable or if the virtual table is ill-formed (such as rewriten
6804 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
81fa35bd
MJ
6805
6806tree
85942f45
JH
6807gimple_get_virt_method_for_vtable (HOST_WIDE_INT token,
6808 tree v,
ec77d61f
JH
6809 unsigned HOST_WIDE_INT offset,
6810 bool *can_refer)
81fa35bd 6811{
85942f45
JH
6812 tree vtable = v, init, fn;
6813 unsigned HOST_WIDE_INT size;
8c311b50
JH
6814 unsigned HOST_WIDE_INT elt_size, access_index;
6815 tree domain_type;
81fa35bd 6816
ec77d61f
JH
6817 if (can_refer)
6818 *can_refer = true;
6819
9de2f554 6820 /* First of all double check we have virtual table. */
8813a647 6821 if (!VAR_P (v) || !DECL_VIRTUAL_P (v))
ec77d61f 6822 {
ec77d61f
JH
6823 /* Pass down that we lost track of the target. */
6824 if (can_refer)
6825 *can_refer = false;
6826 return NULL_TREE;
6827 }
9de2f554 6828
2aa3da06
JH
6829 init = ctor_for_folding (v);
6830
9de2f554 6831 /* The virtual tables should always be born with constructors
2aa3da06
JH
6832 and we always should assume that they are avaialble for
6833 folding. At the moment we do not stream them in all cases,
6834 but it should never happen that ctor seem unreachable. */
6835 gcc_assert (init);
6836 if (init == error_mark_node)
6837 {
ec77d61f
JH
6838 /* Pass down that we lost track of the target. */
6839 if (can_refer)
6840 *can_refer = false;
2aa3da06
JH
6841 return NULL_TREE;
6842 }
81fa35bd 6843 gcc_checking_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
ae7e9ddd 6844 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (v))));
85942f45 6845 offset *= BITS_PER_UNIT;
81fa35bd 6846 offset += token * size;
9de2f554 6847
8c311b50
JH
6848 /* Lookup the value in the constructor that is assumed to be array.
6849 This is equivalent to
6850 fn = fold_ctor_reference (TREE_TYPE (TREE_TYPE (v)), init,
6851 offset, size, NULL);
6852 but in a constant time. We expect that frontend produced a simple
6853 array without indexed initializers. */
6854
6855 gcc_checking_assert (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
6856 domain_type = TYPE_DOMAIN (TREE_TYPE (init));
6857 gcc_checking_assert (integer_zerop (TYPE_MIN_VALUE (domain_type)));
6858 elt_size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init))));
6859
6860 access_index = offset / BITS_PER_UNIT / elt_size;
6861 gcc_checking_assert (offset % (elt_size * BITS_PER_UNIT) == 0);
6862
6863 /* This code makes an assumption that there are no
6864 indexed fileds produced by C++ FE, so we can directly index the array. */
6865 if (access_index < CONSTRUCTOR_NELTS (init))
6866 {
6867 fn = CONSTRUCTOR_ELT (init, access_index)->value;
6868 gcc_checking_assert (!CONSTRUCTOR_ELT (init, access_index)->index);
6869 STRIP_NOPS (fn);
6870 }
6871 else
6872 fn = NULL;
9de2f554
JH
6873
6874 /* For type inconsistent program we may end up looking up virtual method
6875 in virtual table that does not contain TOKEN entries. We may overrun
6876 the virtual table and pick up a constant or RTTI info pointer.
6877 In any case the call is undefined. */
6878 if (!fn
6879 || (TREE_CODE (fn) != ADDR_EXPR && TREE_CODE (fn) != FDESC_EXPR)
6880 || TREE_CODE (TREE_OPERAND (fn, 0)) != FUNCTION_DECL)
6881 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
6882 else
6883 {
6884 fn = TREE_OPERAND (fn, 0);
6885
6886 /* When cgraph node is missing and function is not public, we cannot
6887 devirtualize. This can happen in WHOPR when the actual method
6888 ends up in other partition, because we found devirtualization
6889 possibility too late. */
6890 if (!can_refer_decl_in_current_unit_p (fn, vtable))
ec77d61f
JH
6891 {
6892 if (can_refer)
6893 {
6894 *can_refer = false;
6895 return fn;
6896 }
6897 return NULL_TREE;
6898 }
9de2f554 6899 }
81fa35bd 6900
7501ca28
RG
6901 /* Make sure we create a cgraph node for functions we'll reference.
6902 They can be non-existent if the reference comes from an entry
6903 of an external vtable for example. */
d52f5295 6904 cgraph_node::get_create (fn);
7501ca28 6905
81fa35bd
MJ
6906 return fn;
6907}
6908
85942f45
JH
6909/* Return a declaration of a function which an OBJ_TYPE_REF references. TOKEN
6910 is integer form of OBJ_TYPE_REF_TOKEN of the reference expression.
6911 KNOWN_BINFO carries the binfo describing the true type of
ec77d61f
JH
6912 OBJ_TYPE_REF_OBJECT(REF).
6913 Set CAN_REFER if non-NULL to false if method
6914 is not referable or if the virtual table is ill-formed (such as rewriten
6915 by non-C++ produced symbol). Otherwise just return NULL in that calse. */
85942f45
JH
6916
6917tree
ec77d61f
JH
6918gimple_get_virt_method_for_binfo (HOST_WIDE_INT token, tree known_binfo,
6919 bool *can_refer)
85942f45
JH
6920{
6921 unsigned HOST_WIDE_INT offset;
6922 tree v;
6923
6924 v = BINFO_VTABLE (known_binfo);
6925 /* If there is no virtual methods table, leave the OBJ_TYPE_REF alone. */
6926 if (!v)
6927 return NULL_TREE;
6928
6929 if (!vtable_pointer_value_to_vtable (v, &v, &offset))
ec77d61f
JH
6930 {
6931 if (can_refer)
6932 *can_refer = false;
6933 return NULL_TREE;
6934 }
6935 return gimple_get_virt_method_for_vtable (token, v, offset, can_refer);
85942f45
JH
6936}
6937
737f500a
RB
6938/* Given a pointer value T, return a simplified version of an
6939 indirection through T, or NULL_TREE if no simplification is
b184c8f1
AM
6940 possible. Note that the resulting type may be different from
6941 the type pointed to in the sense that it is still compatible
6942 from the langhooks point of view. */
6943
6944tree
6945gimple_fold_indirect_ref (tree t)
6946{
6947 tree ptype = TREE_TYPE (t), type = TREE_TYPE (ptype);
6948 tree sub = t;
6949 tree subtype;
6950
6951 STRIP_NOPS (sub);
6952 subtype = TREE_TYPE (sub);
737f500a
RB
6953 if (!POINTER_TYPE_P (subtype)
6954 || TYPE_REF_CAN_ALIAS_ALL (ptype))
b184c8f1
AM
6955 return NULL_TREE;
6956
6957 if (TREE_CODE (sub) == ADDR_EXPR)
6958 {
6959 tree op = TREE_OPERAND (sub, 0);
6960 tree optype = TREE_TYPE (op);
6961 /* *&p => p */
6962 if (useless_type_conversion_p (type, optype))
6963 return op;
6964
6965 /* *(foo *)&fooarray => fooarray[0] */
6966 if (TREE_CODE (optype) == ARRAY_TYPE
6967 && TREE_CODE (TYPE_SIZE (TREE_TYPE (optype))) == INTEGER_CST
6968 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6969 {
6970 tree type_domain = TYPE_DOMAIN (optype);
6971 tree min_val = size_zero_node;
6972 if (type_domain && TYPE_MIN_VALUE (type_domain))
6973 min_val = TYPE_MIN_VALUE (type_domain);
6974 if (TREE_CODE (min_val) == INTEGER_CST)
6975 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
6976 }
6977 /* *(foo *)&complexfoo => __real__ complexfoo */
6978 else if (TREE_CODE (optype) == COMPLEX_TYPE
6979 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6980 return fold_build1 (REALPART_EXPR, type, op);
6981 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
6982 else if (TREE_CODE (optype) == VECTOR_TYPE
6983 && useless_type_conversion_p (type, TREE_TYPE (optype)))
6984 {
6985 tree part_width = TYPE_SIZE (type);
6986 tree index = bitsize_int (0);
6987 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
6988 }
6989 }
6990
6991 /* *(p + CST) -> ... */
6992 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
6993 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
6994 {
6995 tree addr = TREE_OPERAND (sub, 0);
6996 tree off = TREE_OPERAND (sub, 1);
6997 tree addrtype;
6998
6999 STRIP_NOPS (addr);
7000 addrtype = TREE_TYPE (addr);
7001
7002 /* ((foo*)&vectorfoo)[1] -> BIT_FIELD_REF<vectorfoo,...> */
7003 if (TREE_CODE (addr) == ADDR_EXPR
7004 && TREE_CODE (TREE_TYPE (addrtype)) == VECTOR_TYPE
7005 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype)))
cc269bb6 7006 && tree_fits_uhwi_p (off))
b184c8f1 7007 {
ae7e9ddd 7008 unsigned HOST_WIDE_INT offset = tree_to_uhwi (off);
b184c8f1
AM
7009 tree part_width = TYPE_SIZE (type);
7010 unsigned HOST_WIDE_INT part_widthi
9439e9a1 7011 = tree_to_shwi (part_width) / BITS_PER_UNIT;
b184c8f1
AM
7012 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
7013 tree index = bitsize_int (indexi);
928686b1
RS
7014 if (known_lt (offset / part_widthi,
7015 TYPE_VECTOR_SUBPARTS (TREE_TYPE (addrtype))))
b184c8f1
AM
7016 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (addr, 0),
7017 part_width, index);
7018 }
7019
7020 /* ((foo*)&complexfoo)[1] -> __imag__ complexfoo */
7021 if (TREE_CODE (addr) == ADDR_EXPR
7022 && TREE_CODE (TREE_TYPE (addrtype)) == COMPLEX_TYPE
7023 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (addrtype))))
7024 {
7025 tree size = TYPE_SIZE_UNIT (type);
7026 if (tree_int_cst_equal (size, off))
7027 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (addr, 0));
7028 }
7029
7030 /* *(p + CST) -> MEM_REF <p, CST>. */
7031 if (TREE_CODE (addr) != ADDR_EXPR
7032 || DECL_P (TREE_OPERAND (addr, 0)))
7033 return fold_build2 (MEM_REF, type,
7034 addr,
8e6cdc90 7035 wide_int_to_tree (ptype, wi::to_wide (off)));
b184c8f1
AM
7036 }
7037
7038 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7039 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7040 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (subtype)))) == INTEGER_CST
7041 && useless_type_conversion_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7042 {
7043 tree type_domain;
7044 tree min_val = size_zero_node;
7045 tree osub = sub;
7046 sub = gimple_fold_indirect_ref (sub);
7047 if (! sub)
7048 sub = build1 (INDIRECT_REF, TREE_TYPE (subtype), osub);
7049 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7050 if (type_domain && TYPE_MIN_VALUE (type_domain))
7051 min_val = TYPE_MIN_VALUE (type_domain);
7052 if (TREE_CODE (min_val) == INTEGER_CST)
7053 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
7054 }
7055
7056 return NULL_TREE;
7057}
19e51b40
JJ
7058
7059/* Return true if CODE is an operation that when operating on signed
7060 integer types involves undefined behavior on overflow and the
7061 operation can be expressed with unsigned arithmetic. */
7062
7063bool
7064arith_code_with_undefined_signed_overflow (tree_code code)
7065{
7066 switch (code)
7067 {
7068 case PLUS_EXPR:
7069 case MINUS_EXPR:
7070 case MULT_EXPR:
7071 case NEGATE_EXPR:
7072 case POINTER_PLUS_EXPR:
7073 return true;
7074 default:
7075 return false;
7076 }
7077}
7078
7079/* Rewrite STMT, an assignment with a signed integer or pointer arithmetic
7080 operation that can be transformed to unsigned arithmetic by converting
7081 its operand, carrying out the operation in the corresponding unsigned
7082 type and converting the result back to the original type.
7083
7084 Returns a sequence of statements that replace STMT and also contain
7085 a modified form of STMT itself. */
7086
7087gimple_seq
355fe088 7088rewrite_to_defined_overflow (gimple *stmt)
19e51b40
JJ
7089{
7090 if (dump_file && (dump_flags & TDF_DETAILS))
7091 {
7092 fprintf (dump_file, "rewriting stmt with undefined signed "
7093 "overflow ");
7094 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
7095 }
7096
7097 tree lhs = gimple_assign_lhs (stmt);
7098 tree type = unsigned_type_for (TREE_TYPE (lhs));
7099 gimple_seq stmts = NULL;
7100 for (unsigned i = 1; i < gimple_num_ops (stmt); ++i)
7101 {
74e3c262
RB
7102 tree op = gimple_op (stmt, i);
7103 op = gimple_convert (&stmts, type, op);
7104 gimple_set_op (stmt, i, op);
19e51b40
JJ
7105 }
7106 gimple_assign_set_lhs (stmt, make_ssa_name (type, stmt));
7107 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
7108 gimple_assign_set_rhs_code (stmt, PLUS_EXPR);
7109 gimple_seq_add_stmt (&stmts, stmt);
355fe088 7110 gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt));
19e51b40
JJ
7111 gimple_seq_add_stmt (&stmts, cvt);
7112
7113 return stmts;
7114}
d4f5cd5e 7115
3d2cf79f 7116
c26de36d
RB
7117/* The valueization hook we use for the gimple_build API simplification.
7118 This makes us match fold_buildN behavior by only combining with
7119 statements in the sequence(s) we are currently building. */
7120
7121static tree
7122gimple_build_valueize (tree op)
7123{
7124 if (gimple_bb (SSA_NAME_DEF_STMT (op)) == NULL)
7125 return op;
7126 return NULL_TREE;
7127}
7128
3d2cf79f 7129/* Build the expression CODE OP0 of type TYPE with location LOC,
c26de36d 7130 simplifying it first if possible. Returns the built
3d2cf79f
RB
7131 expression value and appends statements possibly defining it
7132 to SEQ. */
7133
7134tree
7135gimple_build (gimple_seq *seq, location_t loc,
c26de36d 7136 enum tree_code code, tree type, tree op0)
3d2cf79f 7137{
c26de36d 7138 tree res = gimple_simplify (code, type, op0, seq, gimple_build_valueize);
3d2cf79f
RB
7139 if (!res)
7140 {
a15ebbcd 7141 res = create_tmp_reg_or_ssa_name (type);
355fe088 7142 gimple *stmt;
3d2cf79f
RB
7143 if (code == REALPART_EXPR
7144 || code == IMAGPART_EXPR
7145 || code == VIEW_CONVERT_EXPR)
0d0e4a03 7146 stmt = gimple_build_assign (res, code, build1 (code, type, op0));
3d2cf79f 7147 else
0d0e4a03 7148 stmt = gimple_build_assign (res, code, op0);
3d2cf79f
RB
7149 gimple_set_location (stmt, loc);
7150 gimple_seq_add_stmt_without_update (seq, stmt);
7151 }
7152 return res;
7153}
7154
7155/* Build the expression OP0 CODE OP1 of type TYPE with location LOC,
c26de36d 7156 simplifying it first if possible. Returns the built
3d2cf79f
RB
7157 expression value and appends statements possibly defining it
7158 to SEQ. */
7159
7160tree
7161gimple_build (gimple_seq *seq, location_t loc,
c26de36d 7162 enum tree_code code, tree type, tree op0, tree op1)
3d2cf79f 7163{
c26de36d 7164 tree res = gimple_simplify (code, type, op0, op1, seq, gimple_build_valueize);
3d2cf79f
RB
7165 if (!res)
7166 {
a15ebbcd 7167 res = create_tmp_reg_or_ssa_name (type);
355fe088 7168 gimple *stmt = gimple_build_assign (res, code, op0, op1);
3d2cf79f
RB
7169 gimple_set_location (stmt, loc);
7170 gimple_seq_add_stmt_without_update (seq, stmt);
7171 }
7172 return res;
7173}
7174
7175/* Build the expression (CODE OP0 OP1 OP2) of type TYPE with location LOC,
c26de36d 7176 simplifying it first if possible. Returns the built
3d2cf79f
RB
7177 expression value and appends statements possibly defining it
7178 to SEQ. */
7179
7180tree
7181gimple_build (gimple_seq *seq, location_t loc,
c26de36d 7182 enum tree_code code, tree type, tree op0, tree op1, tree op2)
3d2cf79f
RB
7183{
7184 tree res = gimple_simplify (code, type, op0, op1, op2,
c26de36d 7185 seq, gimple_build_valueize);
3d2cf79f
RB
7186 if (!res)
7187 {
a15ebbcd 7188 res = create_tmp_reg_or_ssa_name (type);
355fe088 7189 gimple *stmt;
3d2cf79f 7190 if (code == BIT_FIELD_REF)
0d0e4a03
JJ
7191 stmt = gimple_build_assign (res, code,
7192 build3 (code, type, op0, op1, op2));
3d2cf79f 7193 else
0d0e4a03 7194 stmt = gimple_build_assign (res, code, op0, op1, op2);
3d2cf79f
RB
7195 gimple_set_location (stmt, loc);
7196 gimple_seq_add_stmt_without_update (seq, stmt);
7197 }
7198 return res;
7199}
7200
7201/* Build the call FN (ARG0) with a result of type TYPE
7202 (or no result if TYPE is void) with location LOC,
c26de36d 7203 simplifying it first if possible. Returns the built
3d2cf79f
RB
7204 expression value (or NULL_TREE if TYPE is void) and appends
7205 statements possibly defining it to SEQ. */
7206
7207tree
eb69361d
RS
7208gimple_build (gimple_seq *seq, location_t loc, combined_fn fn,
7209 tree type, tree arg0)
3d2cf79f 7210{
c26de36d 7211 tree res = gimple_simplify (fn, type, arg0, seq, gimple_build_valueize);
3d2cf79f
RB
7212 if (!res)
7213 {
eb69361d
RS
7214 gcall *stmt;
7215 if (internal_fn_p (fn))
7216 stmt = gimple_build_call_internal (as_internal_fn (fn), 1, arg0);
7217 else
7218 {
7219 tree decl = builtin_decl_implicit (as_builtin_fn (fn));
7220 stmt = gimple_build_call (decl, 1, arg0);
7221 }
3d2cf79f
RB
7222 if (!VOID_TYPE_P (type))
7223 {
a15ebbcd 7224 res = create_tmp_reg_or_ssa_name (type);
3d2cf79f
RB
7225 gimple_call_set_lhs (stmt, res);
7226 }
7227 gimple_set_location (stmt, loc);
7228 gimple_seq_add_stmt_without_update (seq, stmt);
7229 }
7230 return res;
7231}
7232
7233/* Build the call FN (ARG0, ARG1) with a result of type TYPE
7234 (or no result if TYPE is void) with location LOC,
c26de36d 7235 simplifying it first if possible. Returns the built
3d2cf79f
RB
7236 expression value (or NULL_TREE if TYPE is void) and appends
7237 statements possibly defining it to SEQ. */
7238
7239tree
eb69361d
RS
7240gimple_build (gimple_seq *seq, location_t loc, combined_fn fn,
7241 tree type, tree arg0, tree arg1)
3d2cf79f 7242{
c26de36d 7243 tree res = gimple_simplify (fn, type, arg0, arg1, seq, gimple_build_valueize);
3d2cf79f
RB
7244 if (!res)
7245 {
eb69361d
RS
7246 gcall *stmt;
7247 if (internal_fn_p (fn))
7248 stmt = gimple_build_call_internal (as_internal_fn (fn), 2, arg0, arg1);
7249 else
7250 {
7251 tree decl = builtin_decl_implicit (as_builtin_fn (fn));
7252 stmt = gimple_build_call (decl, 2, arg0, arg1);
7253 }
3d2cf79f
RB
7254 if (!VOID_TYPE_P (type))
7255 {
a15ebbcd 7256 res = create_tmp_reg_or_ssa_name (type);
3d2cf79f
RB
7257 gimple_call_set_lhs (stmt, res);
7258 }
7259 gimple_set_location (stmt, loc);
7260 gimple_seq_add_stmt_without_update (seq, stmt);
7261 }
7262 return res;
7263}
7264
7265/* Build the call FN (ARG0, ARG1, ARG2) with a result of type TYPE
7266 (or no result if TYPE is void) with location LOC,
c26de36d 7267 simplifying it first if possible. Returns the built
3d2cf79f
RB
7268 expression value (or NULL_TREE if TYPE is void) and appends
7269 statements possibly defining it to SEQ. */
7270
7271tree
eb69361d
RS
7272gimple_build (gimple_seq *seq, location_t loc, combined_fn fn,
7273 tree type, tree arg0, tree arg1, tree arg2)
3d2cf79f 7274{
c26de36d
RB
7275 tree res = gimple_simplify (fn, type, arg0, arg1, arg2,
7276 seq, gimple_build_valueize);
3d2cf79f
RB
7277 if (!res)
7278 {
eb69361d
RS
7279 gcall *stmt;
7280 if (internal_fn_p (fn))
7281 stmt = gimple_build_call_internal (as_internal_fn (fn),
7282 3, arg0, arg1, arg2);
7283 else
7284 {
7285 tree decl = builtin_decl_implicit (as_builtin_fn (fn));
7286 stmt = gimple_build_call (decl, 3, arg0, arg1, arg2);
7287 }
3d2cf79f
RB
7288 if (!VOID_TYPE_P (type))
7289 {
a15ebbcd 7290 res = create_tmp_reg_or_ssa_name (type);
3d2cf79f
RB
7291 gimple_call_set_lhs (stmt, res);
7292 }
7293 gimple_set_location (stmt, loc);
7294 gimple_seq_add_stmt_without_update (seq, stmt);
7295 }
7296 return res;
7297}
7298
7299/* Build the conversion (TYPE) OP with a result of type TYPE
7300 with location LOC if such conversion is neccesary in GIMPLE,
7301 simplifying it first.
7302 Returns the built expression value and appends
7303 statements possibly defining it to SEQ. */
d4f5cd5e
RB
7304
7305tree
7306gimple_convert (gimple_seq *seq, location_t loc, tree type, tree op)
7307{
7308 if (useless_type_conversion_p (type, TREE_TYPE (op)))
7309 return op;
3d2cf79f 7310 return gimple_build (seq, loc, NOP_EXPR, type, op);
d4f5cd5e 7311}
68e57f04 7312
74e3c262
RB
7313/* Build the conversion (ptrofftype) OP with a result of a type
7314 compatible with ptrofftype with location LOC if such conversion
7315 is neccesary in GIMPLE, simplifying it first.
7316 Returns the built expression value and appends
7317 statements possibly defining it to SEQ. */
7318
7319tree
7320gimple_convert_to_ptrofftype (gimple_seq *seq, location_t loc, tree op)
7321{
7322 if (ptrofftype_p (TREE_TYPE (op)))
7323 return op;
7324 return gimple_convert (seq, loc, sizetype, op);
7325}
7326
e7c45b66
RS
7327/* Build a vector of type TYPE in which each element has the value OP.
7328 Return a gimple value for the result, appending any new statements
7329 to SEQ. */
7330
7331tree
7332gimple_build_vector_from_val (gimple_seq *seq, location_t loc, tree type,
7333 tree op)
7334{
928686b1
RS
7335 if (!TYPE_VECTOR_SUBPARTS (type).is_constant ()
7336 && !CONSTANT_CLASS_P (op))
7337 return gimple_build (seq, loc, VEC_DUPLICATE_EXPR, type, op);
7338
e7c45b66
RS
7339 tree res, vec = build_vector_from_val (type, op);
7340 if (is_gimple_val (vec))
7341 return vec;
7342 if (gimple_in_ssa_p (cfun))
7343 res = make_ssa_name (type);
7344 else
7345 res = create_tmp_reg (type);
7346 gimple *stmt = gimple_build_assign (res, vec);
7347 gimple_set_location (stmt, loc);
7348 gimple_seq_add_stmt_without_update (seq, stmt);
7349 return res;
7350}
7351
abe73c3d
RS
7352/* Build a vector from BUILDER, handling the case in which some elements
7353 are non-constant. Return a gimple value for the result, appending any
7354 new instructions to SEQ.
7355
7356 BUILDER must not have a stepped encoding on entry. This is because
7357 the function is not geared up to handle the arithmetic that would
7358 be needed in the variable case, and any code building a vector that
7359 is known to be constant should use BUILDER->build () directly. */
e7c45b66
RS
7360
7361tree
abe73c3d
RS
7362gimple_build_vector (gimple_seq *seq, location_t loc,
7363 tree_vector_builder *builder)
e7c45b66 7364{
abe73c3d
RS
7365 gcc_assert (builder->nelts_per_pattern () <= 2);
7366 unsigned int encoded_nelts = builder->encoded_nelts ();
7367 for (unsigned int i = 0; i < encoded_nelts; ++i)
7368 if (!TREE_CONSTANT ((*builder)[i]))
e7c45b66 7369 {
abe73c3d 7370 tree type = builder->type ();
928686b1 7371 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
e7c45b66
RS
7372 vec<constructor_elt, va_gc> *v;
7373 vec_alloc (v, nelts);
7374 for (i = 0; i < nelts; ++i)
abe73c3d 7375 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, builder->elt (i));
e7c45b66
RS
7376
7377 tree res;
7378 if (gimple_in_ssa_p (cfun))
7379 res = make_ssa_name (type);
7380 else
7381 res = create_tmp_reg (type);
7382 gimple *stmt = gimple_build_assign (res, build_constructor (type, v));
7383 gimple_set_location (stmt, loc);
7384 gimple_seq_add_stmt_without_update (seq, stmt);
7385 return res;
7386 }
abe73c3d 7387 return builder->build ();
e7c45b66
RS
7388}
7389
68e57f04
RS
7390/* Return true if the result of assignment STMT is known to be non-negative.
7391 If the return value is based on the assumption that signed overflow is
7392 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7393 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7394
7395static bool
7396gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7397 int depth)
7398{
7399 enum tree_code code = gimple_assign_rhs_code (stmt);
7400 switch (get_gimple_rhs_class (code))
7401 {
7402 case GIMPLE_UNARY_RHS:
7403 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
7404 gimple_expr_type (stmt),
7405 gimple_assign_rhs1 (stmt),
7406 strict_overflow_p, depth);
7407 case GIMPLE_BINARY_RHS:
7408 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
7409 gimple_expr_type (stmt),
7410 gimple_assign_rhs1 (stmt),
7411 gimple_assign_rhs2 (stmt),
7412 strict_overflow_p, depth);
7413 case GIMPLE_TERNARY_RHS:
7414 return false;
7415 case GIMPLE_SINGLE_RHS:
7416 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
7417 strict_overflow_p, depth);
7418 case GIMPLE_INVALID_RHS:
7419 break;
7420 }
7421 gcc_unreachable ();
7422}
7423
7424/* Return true if return value of call STMT is known to be non-negative.
7425 If the return value is based on the assumption that signed overflow is
7426 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7427 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7428
7429static bool
7430gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7431 int depth)
7432{
7433 tree arg0 = gimple_call_num_args (stmt) > 0 ?
7434 gimple_call_arg (stmt, 0) : NULL_TREE;
7435 tree arg1 = gimple_call_num_args (stmt) > 1 ?
7436 gimple_call_arg (stmt, 1) : NULL_TREE;
7437
7438 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1d9da71f 7439 gimple_call_combined_fn (stmt),
68e57f04
RS
7440 arg0,
7441 arg1,
7442 strict_overflow_p, depth);
7443}
7444
4534c203
RB
7445/* Return true if return value of call STMT is known to be non-negative.
7446 If the return value is based on the assumption that signed overflow is
7447 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7448 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7449
7450static bool
7451gimple_phi_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7452 int depth)
7453{
7454 for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
7455 {
7456 tree arg = gimple_phi_arg_def (stmt, i);
7457 if (!tree_single_nonnegative_warnv_p (arg, strict_overflow_p, depth + 1))
7458 return false;
7459 }
7460 return true;
7461}
7462
68e57f04
RS
7463/* Return true if STMT is known to compute a non-negative value.
7464 If the return value is based on the assumption that signed overflow is
7465 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
7466 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
7467
7468bool
7469gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p,
7470 int depth)
7471{
7472 switch (gimple_code (stmt))
7473 {
7474 case GIMPLE_ASSIGN:
7475 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p,
7476 depth);
7477 case GIMPLE_CALL:
7478 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p,
7479 depth);
4534c203
RB
7480 case GIMPLE_PHI:
7481 return gimple_phi_nonnegative_warnv_p (stmt, strict_overflow_p,
7482 depth);
68e57f04
RS
7483 default:
7484 return false;
7485 }
7486}
67dbe582
RS
7487
7488/* Return true if the floating-point value computed by assignment STMT
7489 is known to have an integer value. We also allow +Inf, -Inf and NaN
5a00b0aa 7490 to be considered integer values. Return false for signaling NaN.
67dbe582
RS
7491
7492 DEPTH is the current nesting depth of the query. */
7493
7494static bool
7495gimple_assign_integer_valued_real_p (gimple *stmt, int depth)
7496{
7497 enum tree_code code = gimple_assign_rhs_code (stmt);
7498 switch (get_gimple_rhs_class (code))
7499 {
7500 case GIMPLE_UNARY_RHS:
7501 return integer_valued_real_unary_p (gimple_assign_rhs_code (stmt),
7502 gimple_assign_rhs1 (stmt), depth);
7503 case GIMPLE_BINARY_RHS:
7504 return integer_valued_real_binary_p (gimple_assign_rhs_code (stmt),
7505 gimple_assign_rhs1 (stmt),
7506 gimple_assign_rhs2 (stmt), depth);
7507 case GIMPLE_TERNARY_RHS:
7508 return false;
7509 case GIMPLE_SINGLE_RHS:
7510 return integer_valued_real_single_p (gimple_assign_rhs1 (stmt), depth);
7511 case GIMPLE_INVALID_RHS:
7512 break;
7513 }
7514 gcc_unreachable ();
7515}
7516
7517/* Return true if the floating-point value computed by call STMT is known
7518 to have an integer value. We also allow +Inf, -Inf and NaN to be
5a00b0aa 7519 considered integer values. Return false for signaling NaN.
67dbe582
RS
7520
7521 DEPTH is the current nesting depth of the query. */
7522
7523static bool
7524gimple_call_integer_valued_real_p (gimple *stmt, int depth)
7525{
7526 tree arg0 = (gimple_call_num_args (stmt) > 0
7527 ? gimple_call_arg (stmt, 0)
7528 : NULL_TREE);
7529 tree arg1 = (gimple_call_num_args (stmt) > 1
7530 ? gimple_call_arg (stmt, 1)
7531 : NULL_TREE);
1d9da71f 7532 return integer_valued_real_call_p (gimple_call_combined_fn (stmt),
67dbe582
RS
7533 arg0, arg1, depth);
7534}
7535
7536/* Return true if the floating-point result of phi STMT is known to have
7537 an integer value. We also allow +Inf, -Inf and NaN to be considered
5a00b0aa 7538 integer values. Return false for signaling NaN.
67dbe582
RS
7539
7540 DEPTH is the current nesting depth of the query. */
7541
7542static bool
7543gimple_phi_integer_valued_real_p (gimple *stmt, int depth)
7544{
7545 for (unsigned i = 0; i < gimple_phi_num_args (stmt); ++i)
7546 {
7547 tree arg = gimple_phi_arg_def (stmt, i);
7548 if (!integer_valued_real_single_p (arg, depth + 1))
7549 return false;
7550 }
7551 return true;
7552}
7553
7554/* Return true if the floating-point value computed by STMT is known
7555 to have an integer value. We also allow +Inf, -Inf and NaN to be
5a00b0aa 7556 considered integer values. Return false for signaling NaN.
67dbe582
RS
7557
7558 DEPTH is the current nesting depth of the query. */
7559
7560bool
7561gimple_stmt_integer_valued_real_p (gimple *stmt, int depth)
7562{
7563 switch (gimple_code (stmt))
7564 {
7565 case GIMPLE_ASSIGN:
7566 return gimple_assign_integer_valued_real_p (stmt, depth);
7567 case GIMPLE_CALL:
7568 return gimple_call_integer_valued_real_p (stmt, depth);
7569 case GIMPLE_PHI:
7570 return gimple_phi_integer_valued_real_p (stmt, depth);
7571 default:
7572 return false;
7573 }
7574}