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