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