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