OSDN Git Service

* ipa-pure-const.c (propagate): Fix type in handling functions
[pf3gnuchains/gcc-fork.git] / gcc / ipa-pure-const.c
1 /* Callgraph based analysis of static variables.
2    Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This file marks functions as being either const (TREE_READONLY) or
23    pure (DECL_PURE_P).  It can also set a variant of these that
24    are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
25
26    This must be run after inlining decisions have been made since
27    otherwise, the local sets will not contain information that is
28    consistent with post inlined state.  The global sets are not prone
29    to this problem since they are by definition transitive.  */
30
31 /* The code in this module is called by the ipa pass manager. It
32    should be one of the later passes since it's information is used by
33    the rest of the compilation. */
34
35 #include "config.h"
36 #include "system.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "tree.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "langhooks.h"
44 #include "pointer-set.h"
45 #include "ggc.h"
46 #include "ipa-utils.h"
47 #include "gimple.h"
48 #include "cgraph.h"
49 #include "output.h"
50 #include "flags.h"
51 #include "timevar.h"
52 #include "toplev.h"
53 #include "diagnostic.h"
54 #include "gimple-pretty-print.h"
55 #include "langhooks.h"
56 #include "target.h"
57 #include "lto-streamer.h"
58 #include "cfgloop.h"
59 #include "tree-scalar-evolution.h"
60 #include "intl.h"
61 #include "opts.h"
62
63 static struct pointer_set_t *visited_nodes;
64
65 /* Lattice values for const and pure functions.  Everything starts out
66    being const, then may drop to pure and then neither depending on
67    what is found.  */
68 enum pure_const_state_e
69 {
70   IPA_CONST,
71   IPA_PURE,
72   IPA_NEITHER
73 };
74
75 const char *pure_const_names[3] = {"const", "pure", "neither"};
76
77 /* Holder for the const_state.  There is one of these per function
78    decl.  */
79 struct funct_state_d
80 {
81   /* See above.  */
82   enum pure_const_state_e pure_const_state;
83   /* What user set here; we can be always sure about this.  */
84   enum pure_const_state_e state_previously_known;
85   bool looping_previously_known;
86
87   /* True if the function could possibly infinite loop.  There are a
88      lot of ways that this could be determined.  We are pretty
89      conservative here.  While it is possible to cse pure and const
90      calls, it is not legal to have dce get rid of the call if there
91      is a possibility that the call could infinite loop since this is
92      a behavioral change.  */
93   bool looping;
94
95   bool can_throw;
96 };
97
98 typedef struct funct_state_d * funct_state;
99
100 /* The storage of the funct_state is abstracted because there is the
101    possibility that it may be desirable to move this to the cgraph
102    local info.  */
103
104 /* Array, indexed by cgraph node uid, of function states.  */
105
106 DEF_VEC_P (funct_state);
107 DEF_VEC_ALLOC_P (funct_state, heap);
108 static VEC (funct_state, heap) *funct_state_vec;
109
110 /* Holders of ipa cgraph hooks: */
111 static struct cgraph_node_hook_list *function_insertion_hook_holder;
112 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
113 static struct cgraph_node_hook_list *node_removal_hook_holder;
114
115 /* Try to guess if function body will always be visible to compiler
116    when compiling the call and whether compiler will be able
117    to propagate the information by itself.  */
118
119 static bool
120 function_always_visible_to_compiler_p (tree decl)
121 {
122   return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl));
123 }
124
125 /* Emit suggestion about attribute ATTRIB_NAME for DECL.  KNOWN_FINITE
126    is true if the function is known to be finite.  The diagnostic is
127    controlled by OPTION.  WARNED_ABOUT is a pointer_set unique for
128    OPTION, this function may initialize it and it is always returned
129    by the function.  */
130
131 static struct pointer_set_t *
132 suggest_attribute (int option, tree decl, bool known_finite,
133                    struct pointer_set_t *warned_about,
134                    const char * attrib_name)
135 {
136   if (!option_enabled (option))
137     return warned_about;
138   if (TREE_THIS_VOLATILE (decl)
139       || (known_finite && function_always_visible_to_compiler_p (decl)))
140     return warned_about;
141
142   if (!warned_about)
143     warned_about = pointer_set_create (); 
144   if (pointer_set_contains (warned_about, decl))
145     return warned_about;
146   pointer_set_insert (warned_about, decl);
147   warning_at (DECL_SOURCE_LOCATION (decl),
148               option,
149               known_finite
150               ? _("function might be candidate for attribute %<%s%>")
151               : _("function might be candidate for attribute %<%s%>"
152                   " if it is known to return normally"), attrib_name);
153   return warned_about;
154 }
155
156 /* Emit suggestion about __attribute_((pure)) for DECL.  KNOWN_FINITE
157    is true if the function is known to be finite.  */
158
159 static void
160 warn_function_pure (tree decl, bool known_finite)
161 {
162   static struct pointer_set_t *warned_about;
163
164   warned_about 
165     = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
166                          known_finite, warned_about, "pure");
167 }
168
169 /* Emit suggestion about __attribute_((const)) for DECL.  KNOWN_FINITE
170    is true if the function is known to be finite.  */
171
172 static void
173 warn_function_const (tree decl, bool known_finite)
174 {
175   static struct pointer_set_t *warned_about;
176   warned_about 
177     = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
178                          known_finite, warned_about, "const");
179 }
180 /* Init the function state.  */
181
182 static void
183 finish_state (void)
184 {
185   free (funct_state_vec);
186 }
187
188
189 /* Return true if we have a function state for NODE.  */
190
191 static inline bool
192 has_function_state (struct cgraph_node *node)
193 {
194   if (!funct_state_vec
195       || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
196     return false;
197   return VEC_index (funct_state, funct_state_vec, node->uid) != NULL;
198 }
199
200 /* Return the function state from NODE.  */
201
202 static inline funct_state
203 get_function_state (struct cgraph_node *node)
204 {
205   static struct funct_state_d varying
206     = { IPA_NEITHER, IPA_NEITHER, true, true, true };
207   if (!funct_state_vec
208       || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
209     /* We might want to put correct previously_known state into varying.  */
210     return &varying;
211   return VEC_index (funct_state, funct_state_vec, node->uid);
212 }
213
214 /* Set the function state S for NODE.  */
215
216 static inline void
217 set_function_state (struct cgraph_node *node, funct_state s)
218 {
219   if (!funct_state_vec
220       || VEC_length (funct_state, funct_state_vec) <= (unsigned int)node->uid)
221      VEC_safe_grow_cleared (funct_state, heap, funct_state_vec, node->uid + 1);
222   VEC_replace (funct_state, funct_state_vec, node->uid, s);
223 }
224
225 /* Check to see if the use (or definition when CHECKING_WRITE is true)
226    variable T is legal in a function that is either pure or const.  */
227
228 static inline void
229 check_decl (funct_state local,
230             tree t, bool checking_write)
231 {
232   /* Do not want to do anything with volatile except mark any
233      function that uses one to be not const or pure.  */
234   if (TREE_THIS_VOLATILE (t))
235     {
236       local->pure_const_state = IPA_NEITHER;
237       if (dump_file)
238         fprintf (dump_file, "    Volatile operand is not const/pure");
239       return;
240     }
241
242   /* Do not care about a local automatic that is not static.  */
243   if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
244     return;
245
246   /* If the variable has the "used" attribute, treat it as if it had a
247      been touched by the devil.  */
248   if (DECL_PRESERVE_P (t))
249     {
250       local->pure_const_state = IPA_NEITHER;
251       if (dump_file)
252         fprintf (dump_file, "    Used static/global variable is not const/pure\n");
253       return;
254     }
255
256   /* Since we have dealt with the locals and params cases above, if we
257      are CHECKING_WRITE, this cannot be a pure or constant
258      function.  */
259   if (checking_write)
260     {
261       local->pure_const_state = IPA_NEITHER;
262       if (dump_file)
263         fprintf (dump_file, "    static/global memory write is not const/pure\n");
264       return;
265     }
266
267   if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
268     {
269       /* Readonly reads are safe.  */
270       if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t)))
271         return; /* Read of a constant, do not change the function state.  */
272       else
273         {
274           if (dump_file)
275             fprintf (dump_file, "    global memory read is not const\n");
276           /* Just a regular read.  */
277           if (local->pure_const_state == IPA_CONST)
278             local->pure_const_state = IPA_PURE;
279         }
280     }
281   else
282     {
283       /* Compilation level statics can be read if they are readonly
284          variables.  */
285       if (TREE_READONLY (t))
286         return;
287
288       if (dump_file)
289         fprintf (dump_file, "    static memory read is not const\n");
290       /* Just a regular read.  */
291       if (local->pure_const_state == IPA_CONST)
292         local->pure_const_state = IPA_PURE;
293     }
294 }
295
296
297 /* Check to see if the use (or definition when CHECKING_WRITE is true)
298    variable T is legal in a function that is either pure or const.  */
299
300 static inline void
301 check_op (funct_state local, tree t, bool checking_write)
302 {
303   t = get_base_address (t);
304   if (t && TREE_THIS_VOLATILE (t))
305     {
306       local->pure_const_state = IPA_NEITHER;
307       if (dump_file)
308         fprintf (dump_file, "    Volatile indirect ref is not const/pure\n");
309       return;
310     }
311   else if (t
312            && INDIRECT_REF_P (t)
313            && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
314            && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0)))
315     {
316       if (dump_file)
317         fprintf (dump_file, "    Indirect ref to local memory is OK\n");
318       return;
319     }
320   else if (checking_write)
321     {
322       local->pure_const_state = IPA_NEITHER;
323       if (dump_file)
324         fprintf (dump_file, "    Indirect ref write is not const/pure\n");
325       return;
326     }
327   else
328     {
329       if (dump_file)
330         fprintf (dump_file, "    Indirect ref read is not const\n");
331       if (local->pure_const_state == IPA_CONST)
332         local->pure_const_state = IPA_PURE;
333     }
334 }
335
336 /* Check the parameters of a function call to CALL_EXPR to see if
337    there are any references in the parameters that are not allowed for
338    pure or const functions.  Also check to see if this is either an
339    indirect call, a call outside the compilation unit, or has special
340    attributes that may also effect the purity.  The CALL_EXPR node for
341    the entire call expression.  */
342
343 static void
344 check_call (funct_state local, gimple call, bool ipa)
345 {
346   int flags = gimple_call_flags (call);
347   tree callee_t = gimple_call_fndecl (call);
348   bool possibly_throws = stmt_could_throw_p (call);
349   bool possibly_throws_externally = (possibly_throws
350                                      && stmt_can_throw_external (call));
351
352   if (possibly_throws)
353     {
354       unsigned int i;
355       for (i = 0; i < gimple_num_ops (call); i++)
356         if (gimple_op (call, i)
357             && tree_could_throw_p (gimple_op (call, i)))
358           {
359             if (possibly_throws && cfun->can_throw_non_call_exceptions)
360               {
361                 if (dump_file)
362                   fprintf (dump_file, "    operand can throw; looping\n");
363                 local->looping = true;
364               }
365             if (possibly_throws_externally)
366               {
367                 if (dump_file)
368                   fprintf (dump_file, "    operand can throw externally\n");
369                 local->can_throw = true;
370               }
371           }
372     }
373
374   /* The const and pure flags are set by a variety of places in the
375      compiler (including here).  If someone has already set the flags
376      for the callee, (such as for some of the builtins) we will use
377      them, otherwise we will compute our own information.
378
379      Const and pure functions have less clobber effects than other
380      functions so we process these first.  Otherwise if it is a call
381      outside the compilation unit or an indirect call we punt.  This
382      leaves local calls which will be processed by following the call
383      graph.  */
384   if (callee_t)
385     {
386       /* built_in_return is really just an return statemnt.  */
387       if (gimple_call_builtin_p (call, BUILT_IN_RETURN))
388         return;
389       /* When bad things happen to bad functions, they cannot be const
390          or pure.  */
391       if (setjmp_call_p (callee_t))
392         {
393           if (dump_file)
394             fprintf (dump_file, "    setjmp is not const/pure\n");
395           local->looping = true;
396           local->pure_const_state = IPA_NEITHER;
397         }
398
399       if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
400         switch (DECL_FUNCTION_CODE (callee_t))
401           {
402           case BUILT_IN_LONGJMP:
403           case BUILT_IN_NONLOCAL_GOTO:
404             if (dump_file)
405               fprintf (dump_file, "    longjmp and nonlocal goto is not const/pure\n");
406             local->pure_const_state = IPA_NEITHER;
407             local->looping = true;
408             break;
409           default:
410             break;
411           }
412     }
413
414   /* When not in IPA mode, we can still handle self recursion.  */
415   if (!ipa && callee_t == current_function_decl)
416     {
417       if (dump_file)
418         fprintf (dump_file, "    Recursive call can loop.\n");
419       local->looping = true;
420     }
421   /* Either calle is unknown or we are doing local analysis.
422      Look to see if there are any bits available for the callee (such as by
423      declaration or because it is builtin) and process solely on the basis of
424      those bits. */
425   else if (!ipa || !callee_t)
426     {
427       if (possibly_throws && cfun->can_throw_non_call_exceptions)
428         {
429           if (dump_file)
430             fprintf (dump_file, "    can throw; looping\n");
431           local->looping = true;
432         }
433       if (possibly_throws_externally)
434         {
435           if (dump_file)
436             {
437               fprintf (dump_file, "    can throw externally to lp %i\n",
438                        lookup_stmt_eh_lp (call));
439               if (callee_t)
440                 fprintf (dump_file, "     callee:%s\n",
441                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
442             }
443           local->can_throw = true;
444         }
445       if (flags & ECF_CONST)
446         {
447           if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
448             {
449               if (dump_file)
450                 fprintf (dump_file, "    calls looping pure.\n");
451               local->looping = true;
452             }
453          }
454       else if (flags & ECF_PURE)
455         {
456           if (callee_t && DECL_LOOPING_CONST_OR_PURE_P (callee_t))
457             {
458               if (dump_file)
459                 fprintf (dump_file, "    calls looping const.\n");
460               local->looping = true;
461             }
462           if (dump_file)
463             fprintf (dump_file, "    pure function call in not const\n");
464           if (local->pure_const_state == IPA_CONST)
465             local->pure_const_state = IPA_PURE;
466         }
467       else if ((flags & (ECF_NORETURN | ECF_NOTHROW))
468                == (ECF_NORETURN | ECF_NOTHROW)
469                || (!flag_exceptions && (flags & ECF_NORETURN)))
470         {
471           if (dump_file)
472             fprintf (dump_file, "    noreturn nothrow call is looping pure\n");
473           if (local->pure_const_state == IPA_CONST)
474             local->pure_const_state = IPA_PURE;
475           local->looping = true;
476         }
477       else
478         {
479           if (dump_file)
480             fprintf (dump_file, "    uknown function call is not const/pure\n");
481           local->pure_const_state = IPA_NEITHER;
482           local->looping = true;
483         }
484     }
485   /* Direct functions calls are handled by IPA propagation.  */
486 }
487
488 /* Wrapper around check_decl for loads.  */
489
490 static bool
491 check_load (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
492 {
493   if (DECL_P (op))
494     check_decl ((funct_state)data, op, false);
495   else
496     check_op ((funct_state)data, op, false);
497   return false;
498 }
499
500 /* Wrapper around check_decl for stores.  */
501
502 static bool
503 check_store (gimple stmt ATTRIBUTE_UNUSED, tree op, void *data)
504 {
505   if (DECL_P (op))
506     check_decl ((funct_state)data, op, true);
507   else
508     check_op ((funct_state)data, op, true);
509   return false;
510 }
511
512 /* Look into pointer pointed to by GSIP and figure out what interesting side
513    effects it has.  */
514 static void
515 check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
516 {
517   gimple stmt = gsi_stmt (*gsip);
518   unsigned int i = 0;
519
520   if (is_gimple_debug (stmt))
521     return;
522
523   if (dump_file)
524     {
525       fprintf (dump_file, "  scanning: ");
526       print_gimple_stmt (dump_file, stmt, 0, 0);
527     }
528
529   /* Look for loads and stores.  */
530   walk_stmt_load_store_ops (stmt, local, check_load, check_store);
531
532   if (gimple_code (stmt) != GIMPLE_CALL
533       && stmt_could_throw_p (stmt))
534     {
535       if (cfun->can_throw_non_call_exceptions)
536         {
537           if (dump_file)
538             fprintf (dump_file, "    can throw; looping");
539           local->looping = true;
540         }
541       if (stmt_can_throw_external (stmt))
542         {
543           if (dump_file)
544             fprintf (dump_file, "    can throw externally");
545           local->can_throw = true;
546         }
547     }
548   switch (gimple_code (stmt))
549     {
550     case GIMPLE_CALL:
551       check_call (local, stmt, ipa);
552       break;
553     case GIMPLE_LABEL:
554       if (DECL_NONLOCAL (gimple_label_label (stmt)))
555         /* Target of long jump. */
556         {
557           if (dump_file)
558             fprintf (dump_file, "    nonlocal label is not const/pure");
559           local->pure_const_state = IPA_NEITHER;
560         }
561       break;
562     case GIMPLE_ASM:
563       for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
564         {
565           tree op = gimple_asm_clobber_op (stmt, i);
566           if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
567             {
568               if (dump_file)
569                 fprintf (dump_file, "    memory asm clobber is not const/pure");
570               /* Abandon all hope, ye who enter here. */
571               local->pure_const_state = IPA_NEITHER;
572             }
573         }
574       if (gimple_asm_volatile_p (stmt))
575         {
576           if (dump_file)
577             fprintf (dump_file, "    volatile is not const/pure");
578           /* Abandon all hope, ye who enter here. */
579           local->pure_const_state = IPA_NEITHER;
580           local->looping = true;
581         }
582       return;
583     default:
584       break;
585     }
586 }
587
588
589 /* This is the main routine for finding the reference patterns for
590    global variables within a function FN.  */
591
592 static funct_state
593 analyze_function (struct cgraph_node *fn, bool ipa)
594 {
595   tree decl = fn->decl;
596   tree old_decl = current_function_decl;
597   funct_state l;
598   basic_block this_block;
599
600   l = XCNEW (struct funct_state_d);
601   l->pure_const_state = IPA_CONST;
602   l->state_previously_known = IPA_NEITHER;
603   l->looping_previously_known = true;
604   l->looping = false;
605   l->can_throw = false;
606
607   if (dump_file)
608     {
609       fprintf (dump_file, "\n\n local analysis of %s\n ",
610                cgraph_node_name (fn));
611     }
612
613   push_cfun (DECL_STRUCT_FUNCTION (decl));
614   current_function_decl = decl;
615
616   FOR_EACH_BB (this_block)
617     {
618       gimple_stmt_iterator gsi;
619       struct walk_stmt_info wi;
620
621       memset (&wi, 0, sizeof(wi));
622       for (gsi = gsi_start_bb (this_block);
623            !gsi_end_p (gsi);
624            gsi_next (&gsi))
625         {
626           check_stmt (&gsi, l, ipa);
627           if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
628             goto end;
629         }
630     }
631
632 end:
633   if (l->pure_const_state != IPA_NEITHER)
634     {
635       /* Const functions cannot have back edges (an
636          indication of possible infinite loop side
637          effect.  */
638       if (mark_dfs_back_edges ())
639         {
640           /* Preheaders are needed for SCEV to work.
641              Simple lateches and recorded exits improve chances that loop will
642              proved to be finite in testcases such as in loop-15.c and loop-24.c  */
643           loop_optimizer_init (LOOPS_NORMAL
644                                | LOOPS_HAVE_RECORDED_EXITS);
645           if (dump_file && (dump_flags & TDF_DETAILS))
646             flow_loops_dump (dump_file, NULL, 0);
647           if (mark_irreducible_loops ())
648             {
649               if (dump_file)
650                 fprintf (dump_file, "    has irreducible loops\n");
651               l->looping = true;
652             }
653           else
654             {
655               loop_iterator li;
656               struct loop *loop;
657               scev_initialize ();
658               FOR_EACH_LOOP (li, loop, 0)
659                 if (!finite_loop_p (loop))
660                   {
661                     if (dump_file)
662                       fprintf (dump_file, "    can not prove finiteness of loop %i\n", loop->num);
663                     l->looping =true;
664                     break;
665                   }
666               scev_finalize ();
667             }
668           loop_optimizer_finalize ();
669         }
670     }
671
672   if (TREE_READONLY (decl))
673     {
674       l->pure_const_state = IPA_CONST;
675       l->state_previously_known = IPA_CONST;
676       if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
677         l->looping = false, l->looping_previously_known = false;
678     }
679   if (DECL_PURE_P (decl))
680     {
681       if (l->pure_const_state != IPA_CONST)
682         l->pure_const_state = IPA_PURE;
683       l->state_previously_known = IPA_PURE;
684       if (!DECL_LOOPING_CONST_OR_PURE_P (decl))
685         l->looping = false, l->looping_previously_known = false;
686     }
687   if (TREE_NOTHROW (decl))
688     l->can_throw = false;
689
690   pop_cfun ();
691   current_function_decl = old_decl;
692   if (dump_file)
693     {
694       if (l->looping)
695         fprintf (dump_file, "Function is locally looping.\n");
696       if (l->can_throw)
697         fprintf (dump_file, "Function is locally throwing.\n");
698       if (l->pure_const_state == IPA_CONST)
699         fprintf (dump_file, "Function is locally const.\n");
700       if (l->pure_const_state == IPA_PURE)
701         fprintf (dump_file, "Function is locally pure.\n");
702     }
703   return l;
704 }
705
706 /* Called when new function is inserted to callgraph late.  */
707 static void
708 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
709 {
710  if (cgraph_function_body_availability (node) < AVAIL_OVERWRITABLE)
711    return;
712   /* There are some shared nodes, in particular the initializers on
713      static declarations.  We do not need to scan them more than once
714      since all we would be interested in are the addressof
715      operations.  */
716   visited_nodes = pointer_set_create ();
717   if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
718     set_function_state (node, analyze_function (node, true));
719   pointer_set_destroy (visited_nodes);
720   visited_nodes = NULL;
721 }
722
723 /* Called when new clone is inserted to callgraph late.  */
724
725 static void
726 duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
727                      void *data ATTRIBUTE_UNUSED)
728 {
729   if (has_function_state (src))
730     {
731       funct_state l = XNEW (struct funct_state_d);
732       gcc_assert (!has_function_state (dst));
733       memcpy (l, get_function_state (src), sizeof (*l));
734       set_function_state (dst, l);
735     }
736 }
737
738 /* Called when new clone is inserted to callgraph late.  */
739
740 static void
741 remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
742 {
743   if (has_function_state (node))
744     {
745       free (get_function_state (node));
746       set_function_state (node, NULL);
747     }
748 }
749
750 \f
751 static void
752 register_hooks (void)
753 {
754   static bool init_p = false;
755
756   if (init_p)
757     return;
758
759   init_p = true;
760
761   node_removal_hook_holder =
762       cgraph_add_node_removal_hook (&remove_node_data, NULL);
763   node_duplication_hook_holder =
764       cgraph_add_node_duplication_hook (&duplicate_node_data, NULL);
765   function_insertion_hook_holder =
766       cgraph_add_function_insertion_hook (&add_new_function, NULL);
767 }
768
769
770 /* Analyze each function in the cgraph to see if it is locally PURE or
771    CONST.  */
772
773 static void
774 generate_summary (void)
775 {
776   struct cgraph_node *node;
777
778   register_hooks ();
779
780   /* There are some shared nodes, in particular the initializers on
781      static declarations.  We do not need to scan them more than once
782      since all we would be interested in are the addressof
783      operations.  */
784   visited_nodes = pointer_set_create ();
785
786   /* Process all of the functions.
787
788      We process AVAIL_OVERWRITABLE functions.  We can not use the results
789      by default, but the info can be used at LTO with -fwhole-program or
790      when function got clonned and the clone is AVAILABLE.  */
791
792   for (node = cgraph_nodes; node; node = node->next)
793     if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
794       set_function_state (node, analyze_function (node, true));
795
796   pointer_set_destroy (visited_nodes);
797   visited_nodes = NULL;
798 }
799
800
801 /* Serialize the ipa info for lto.  */
802
803 static void
804 pure_const_write_summary (cgraph_node_set set,
805                           varpool_node_set vset ATTRIBUTE_UNUSED)
806 {
807   struct cgraph_node *node;
808   struct lto_simple_output_block *ob
809     = lto_create_simple_output_block (LTO_section_ipa_pure_const);
810   unsigned int count = 0;
811   cgraph_node_set_iterator csi;
812
813   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
814     {
815       node = csi_node (csi);
816       if (node->analyzed && has_function_state (node))
817         count++;
818     }
819
820   lto_output_uleb128_stream (ob->main_stream, count);
821
822   /* Process all of the functions.  */
823   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
824     {
825       node = csi_node (csi);
826       if (node->analyzed && has_function_state (node))
827         {
828           struct bitpack_d *bp;
829           funct_state fs;
830           int node_ref;
831           lto_cgraph_encoder_t encoder;
832
833           fs = get_function_state (node);
834
835           encoder = ob->decl_state->cgraph_node_encoder;
836           node_ref = lto_cgraph_encoder_encode (encoder, node);
837           lto_output_uleb128_stream (ob->main_stream, node_ref);
838
839           /* Note that flags will need to be read in the opposite
840              order as we are pushing the bitflags into FLAGS.  */
841           bp = bitpack_create ();
842           bp_pack_value (bp, fs->pure_const_state, 2);
843           bp_pack_value (bp, fs->state_previously_known, 2);
844           bp_pack_value (bp, fs->looping_previously_known, 1);
845           bp_pack_value (bp, fs->looping, 1);
846           bp_pack_value (bp, fs->can_throw, 1);
847           lto_output_bitpack (ob->main_stream, bp);
848           bitpack_delete (bp);
849         }
850     }
851
852   lto_destroy_simple_output_block (ob);
853 }
854
855
856 /* Deserialize the ipa info for lto.  */
857
858 static void
859 pure_const_read_summary (void)
860 {
861   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
862   struct lto_file_decl_data *file_data;
863   unsigned int j = 0;
864
865   register_hooks ();
866   while ((file_data = file_data_vec[j++]))
867     {
868       const char *data;
869       size_t len;
870       struct lto_input_block *ib
871         = lto_create_simple_input_block (file_data,
872                                          LTO_section_ipa_pure_const,
873                                          &data, &len);
874       if (ib)
875         {
876           unsigned int i;
877           unsigned int count = lto_input_uleb128 (ib);
878
879           for (i = 0; i < count; i++)
880             {
881               unsigned int index;
882               struct cgraph_node *node;
883               struct bitpack_d *bp;
884               funct_state fs;
885               lto_cgraph_encoder_t encoder;
886
887               fs = XCNEW (struct funct_state_d);
888               index = lto_input_uleb128 (ib);
889               encoder = file_data->cgraph_node_encoder;
890               node = lto_cgraph_encoder_deref (encoder, index);
891               set_function_state (node, fs);
892
893               /* Note that the flags must be read in the opposite
894                  order in which they were written (the bitflags were
895                  pushed into FLAGS).  */
896               bp = lto_input_bitpack (ib);
897               fs->pure_const_state
898                         = (enum pure_const_state_e) bp_unpack_value (bp, 2);
899               fs->state_previously_known
900                         = (enum pure_const_state_e) bp_unpack_value (bp, 2);
901               fs->looping_previously_known = bp_unpack_value (bp, 1);
902               fs->looping = bp_unpack_value (bp, 1);
903               fs->can_throw = bp_unpack_value (bp, 1);
904               if (dump_file)
905                 {
906                   int flags = flags_from_decl_or_type (node->decl);
907                   fprintf (dump_file, "Read info for %s/%i ",
908                            cgraph_node_name (node),
909                            node->uid);
910                   if (flags & ECF_CONST)
911                     fprintf (dump_file, " const");
912                   if (flags & ECF_PURE)
913                     fprintf (dump_file, " pure");
914                   if (flags & ECF_NOTHROW)
915                     fprintf (dump_file, " nothrow");
916                   fprintf (dump_file, "\n  pure const state: %s\n",
917                            pure_const_names[fs->pure_const_state]);
918                   fprintf (dump_file, "  previously known state: %s\n",
919                            pure_const_names[fs->looping_previously_known]);
920                   if (fs->looping)
921                     fprintf (dump_file,"  function is locally looping\n");
922                   if (fs->looping_previously_known)
923                     fprintf (dump_file,"  function is previously known looping\n");
924                   if (fs->can_throw)
925                     fprintf (dump_file,"  function is locally throwing\n");
926                 }
927               bitpack_delete (bp);
928             }
929
930           lto_destroy_simple_input_block (file_data,
931                                           LTO_section_ipa_pure_const,
932                                           ib, data, len);
933         }
934     }
935 }
936
937
938 static bool
939 ignore_edge (struct cgraph_edge *e)
940 {
941   return (!e->can_throw_external);
942 }
943
944 /* Return true if NODE is self recursive function.  */
945
946 static bool
947 self_recursive_p (struct cgraph_node *node)
948 {
949   struct cgraph_edge *e;
950   for (e = node->callees; e; e = e->next_callee)
951     if (e->callee == node)
952       return true;
953   return false;
954 }
955
956 /* Produce the global information by preforming a transitive closure
957    on the local information that was produced by generate_summary.
958    Note that there is no function_transform pass since this only
959    updates the function_decl.  */
960
961 static unsigned int
962 propagate (void)
963 {
964   struct cgraph_node *node;
965   struct cgraph_node *w;
966   struct cgraph_node **order =
967     XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
968   int order_pos;
969   int i;
970   struct ipa_dfs_info * w_info;
971
972   cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
973   cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
974   cgraph_remove_node_removal_hook (node_removal_hook_holder);
975   order_pos = ipa_utils_reduced_inorder (order, true, false, NULL);
976   if (dump_file)
977     {
978       dump_cgraph (dump_file);
979       ipa_utils_print_order(dump_file, "reduced", order, order_pos);
980     }
981
982   /* Propagate the local information thru the call graph to produce
983      the global information.  All the nodes within a cycle will have
984      the same info so we collapse cycles first.  Then we can do the
985      propagation in one pass from the leaves to the roots.  */
986   for (i = 0; i < order_pos; i++ )
987     {
988       enum pure_const_state_e pure_const_state = IPA_CONST;
989       bool looping = false;
990       int count = 0;
991       node = order[i];
992
993       if (dump_file && (dump_flags & TDF_DETAILS))
994         fprintf (dump_file, "Starting cycle\n");
995
996       /* Find the worst state for any node in the cycle.  */
997       w = node;
998       while (w)
999         {
1000           struct cgraph_edge *e;
1001           funct_state w_l = get_function_state (w);
1002           if (dump_file && (dump_flags & TDF_DETAILS))
1003             fprintf (dump_file, "  Visiting %s/%i state:%s looping %i\n",
1004                      cgraph_node_name (w),
1005                      w->uid,
1006                      pure_const_names[w_l->pure_const_state],
1007                      w_l->looping);
1008           if (pure_const_state < w_l->pure_const_state)
1009             pure_const_state = w_l->pure_const_state;
1010
1011           if (w_l->looping)
1012             looping = true;
1013           if (cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1014             {
1015               looping |= w_l->looping_previously_known;
1016               if (pure_const_state < w_l->state_previously_known)
1017                 pure_const_state = w_l->state_previously_known;
1018               if (dump_file && (dump_flags & TDF_DETAILS))
1019                 {
1020                   fprintf (dump_file,
1021                            "    Overwritable. state %s looping %i\n",
1022                            pure_const_names[w_l->state_previously_known],
1023                            w_l->looping_previously_known);
1024                 }
1025             }
1026
1027           if (pure_const_state == IPA_NEITHER)
1028             break;
1029
1030           count++;
1031
1032           if (count > 1)
1033             looping = true;
1034
1035           for (e = w->callees; e; e = e->next_callee)
1036             {
1037               struct cgraph_node *y = e->callee;
1038               enum pure_const_state_e edge_state = IPA_CONST;
1039               bool edge_looping = false;
1040
1041               if (dump_file && (dump_flags & TDF_DETAILS))
1042                 {
1043                   fprintf (dump_file,
1044                            "    Call to %s/%i",
1045                            cgraph_node_name (e->callee),
1046                            e->callee->uid);
1047                 }
1048               if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
1049                 {
1050                   funct_state y_l = get_function_state (y);
1051                   if (dump_file && (dump_flags & TDF_DETAILS))
1052                     {
1053                       fprintf (dump_file,
1054                                " state:%s looping:%i\n",
1055                                pure_const_names[y_l->pure_const_state],
1056                                y_l->looping);
1057                     }
1058                   if (y_l->pure_const_state > IPA_PURE
1059                       && cgraph_edge_cannot_lead_to_return (e))
1060                     {
1061                       if (dump_file && (dump_flags & TDF_DETAILS))
1062                         {       
1063                           fprintf (dump_file,
1064                                    "        Ignoring side effects -> pure, looping\n");
1065                         }
1066                       edge_state = IPA_PURE;
1067                       edge_looping = true;
1068                     }
1069                   else
1070                     {
1071                       edge_state = y_l->pure_const_state;
1072                       edge_looping = y_l->looping;
1073                     }
1074                 }
1075               else
1076                 {
1077                   int flags = flags_from_decl_or_type (y->decl);
1078
1079                   if (flags & ECF_LOOPING_CONST_OR_PURE)
1080                     {
1081                       edge_looping = true;
1082                       if (dump_file && (dump_flags & TDF_DETAILS))
1083                         fprintf (dump_file, " unavailable looping");
1084                     }
1085                   if (flags & ECF_CONST)
1086                     {
1087                       if (dump_file && (dump_flags & TDF_DETAILS))
1088                         fprintf (dump_file, " const\n");
1089                     }
1090                   else if (flags & ECF_PURE)
1091                     {
1092                       edge_state = IPA_PURE;
1093                       if (dump_file && (dump_flags & TDF_DETAILS))
1094                         fprintf (dump_file, " pure\n");
1095                     }
1096                   else if (cgraph_edge_cannot_lead_to_return (e))
1097                     {
1098                       edge_state = IPA_PURE;
1099                       edge_looping = true;
1100                       if (dump_file && (dump_flags & TDF_DETAILS))
1101                         fprintf (dump_file, " ignoring side effects->pure looping\n");
1102                     }
1103                   else
1104                     {
1105                       if (dump_file && (dump_flags & TDF_DETAILS))
1106                         fprintf (dump_file, " neihter\n");
1107                       edge_state = IPA_NEITHER;
1108                       edge_looping = true;
1109                     }
1110                 }
1111
1112               /* Merge the results with what we already know.
1113                  When we found function to be NEITHER, but we know
1114                  it is looping pure const, be sure to set the looping flag. */
1115               pure_const_state = MAX (pure_const_state, MIN (edge_state,
1116                                       w_l->state_previously_known));
1117               if (edge_state > w_l->state_previously_known)
1118                 looping = MAX (looping, w_l->looping_previously_known);
1119               else
1120                 looping = MAX (looping, MIN (edge_looping,
1121                                              w_l->looping_previously_known));
1122               if (pure_const_state == IPA_NEITHER)
1123                 break;
1124             }
1125           w_info = (struct ipa_dfs_info *) w->aux;
1126           w = w_info->next_cycle;
1127         }
1128       if (dump_file && (dump_flags & TDF_DETAILS))
1129         fprintf (dump_file, "Result %s looping %i\n",
1130                  pure_const_names [pure_const_state],
1131                  looping);
1132
1133       /* Copy back the region's pure_const_state which is shared by
1134          all nodes in the region.  */
1135       w = node;
1136       while (w)
1137         {
1138           funct_state w_l = get_function_state (w);
1139           enum pure_const_state_e this_state = pure_const_state;
1140           bool this_looping = looping;
1141
1142           if (w_l->state_previously_known != IPA_NEITHER
1143               && this_state > w_l->state_previously_known)
1144             {
1145               this_state = w_l->state_previously_known;
1146               this_looping |= w_l->looping_previously_known;
1147             }
1148           if (!this_looping && self_recursive_p (w))
1149             this_looping = true;
1150           if (!w_l->looping_previously_known)
1151             this_looping = false;
1152
1153           /* All nodes within a cycle share the same info.  */
1154           w_l->pure_const_state = this_state;
1155           w_l->looping = this_looping;
1156
1157           switch (this_state)
1158             {
1159             case IPA_CONST:
1160               if (!TREE_READONLY (w->decl))
1161                 {
1162                   warn_function_const (w->decl, !this_looping);
1163                   if (dump_file)
1164                     fprintf (dump_file, "Function found to be %sconst: %s\n",
1165                              this_looping ? "looping " : "",
1166                              cgraph_node_name (w));
1167                 }
1168               cgraph_set_readonly_flag (w, true);
1169               cgraph_set_looping_const_or_pure_flag (w, this_looping);
1170               break;
1171
1172             case IPA_PURE:
1173               if (!DECL_PURE_P (w->decl))
1174                 {
1175                   warn_function_pure (w->decl, !this_looping);
1176                   if (dump_file)
1177                     fprintf (dump_file, "Function found to be %spure: %s\n",
1178                              this_looping ? "looping " : "",
1179                              cgraph_node_name (w));
1180                 }
1181               cgraph_set_pure_flag (w, true);
1182               cgraph_set_looping_const_or_pure_flag (w, this_looping);
1183               break;
1184
1185             default:
1186               break;
1187             }
1188           w_info = (struct ipa_dfs_info *) w->aux;
1189           w = w_info->next_cycle;
1190         }
1191     }
1192
1193   /* Cleanup. */
1194   for (node = cgraph_nodes; node; node = node->next)
1195     {
1196       /* Get rid of the aux information.  */
1197       if (node->aux)
1198         {
1199           w_info = (struct ipa_dfs_info *) node->aux;
1200           free (node->aux);
1201           node->aux = NULL;
1202         }
1203     }
1204   order_pos = ipa_utils_reduced_inorder (order, true, false, ignore_edge);
1205   if (dump_file)
1206     {
1207       dump_cgraph (dump_file);
1208       ipa_utils_print_order(dump_file, "reduced for nothrow", order, order_pos);
1209     }
1210   /* Propagate the local information thru the call graph to produce
1211      the global information.  All the nodes within a cycle will have
1212      the same info so we collapse cycles first.  Then we can do the
1213      propagation in one pass from the leaves to the roots.  */
1214   for (i = 0; i < order_pos; i++ )
1215     {
1216       bool can_throw = false;
1217       node = order[i];
1218
1219       /* Find the worst state for any node in the cycle.  */
1220       w = node;
1221       while (w)
1222         {
1223           struct cgraph_edge *e;
1224           funct_state w_l = get_function_state (w);
1225
1226           if (w_l->can_throw
1227               || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1228             can_throw = true;
1229
1230           if (can_throw)
1231             break;
1232
1233           for (e = w->callees; e; e = e->next_callee)
1234             {
1235               struct cgraph_node *y = e->callee;
1236
1237               if (cgraph_function_body_availability (y) > AVAIL_OVERWRITABLE)
1238                 {
1239                   funct_state y_l = get_function_state (y);
1240
1241                   if (can_throw)
1242                     break;
1243                   if (y_l->can_throw && !TREE_NOTHROW (w->decl)
1244                       && e->can_throw_external)
1245                     can_throw = true;
1246                 }
1247               else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
1248                 can_throw = true;
1249             }
1250           w_info = (struct ipa_dfs_info *) w->aux;
1251           w = w_info->next_cycle;
1252         }
1253
1254       /* Copy back the region's pure_const_state which is shared by
1255          all nodes in the region.  */
1256       w = node;
1257       while (w)
1258         {
1259           funct_state w_l = get_function_state (w);
1260           if (!can_throw && !TREE_NOTHROW (w->decl))
1261             {
1262               struct cgraph_edge *e;
1263               cgraph_set_nothrow_flag (w, true);
1264               for (e = w->callers; e; e = e->next_caller)
1265                 e->can_throw_external = false;
1266               if (dump_file)
1267                 fprintf (dump_file, "Function found to be nothrow: %s\n",
1268                          cgraph_node_name (w));
1269             }
1270           else if (can_throw && !TREE_NOTHROW (w->decl))
1271             w_l->can_throw = true;
1272           w_info = (struct ipa_dfs_info *) w->aux;
1273           w = w_info->next_cycle;
1274         }
1275     }
1276
1277   /* Cleanup. */
1278   for (node = cgraph_nodes; node; node = node->next)
1279     {
1280       /* Get rid of the aux information.  */
1281       if (node->aux)
1282         {
1283           w_info = (struct ipa_dfs_info *) node->aux;
1284           free (node->aux);
1285           node->aux = NULL;
1286         }
1287       if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE
1288           && has_function_state (node))
1289         free (get_function_state (node));
1290     }
1291
1292   free (order);
1293   VEC_free (funct_state, heap, funct_state_vec);
1294   finish_state ();
1295   return 0;
1296 }
1297
1298 static bool
1299 gate_pure_const (void)
1300 {
1301   return (flag_ipa_pure_const
1302           /* Don't bother doing anything if the program has errors.  */
1303           && !seen_error ());
1304 }
1305
1306 struct ipa_opt_pass_d pass_ipa_pure_const =
1307 {
1308  {
1309   IPA_PASS,
1310   "pure-const",                         /* name */
1311   gate_pure_const,                      /* gate */
1312   propagate,                            /* execute */
1313   NULL,                                 /* sub */
1314   NULL,                                 /* next */
1315   0,                                    /* static_pass_number */
1316   TV_IPA_PURE_CONST,                    /* tv_id */
1317   0,                                    /* properties_required */
1318   0,                                    /* properties_provided */
1319   0,                                    /* properties_destroyed */
1320   0,                                    /* todo_flags_start */
1321   0                                     /* todo_flags_finish */
1322  },
1323  generate_summary,                      /* generate_summary */
1324  pure_const_write_summary,              /* write_summary */
1325  pure_const_read_summary,               /* read_summary */
1326  NULL,                                  /* write_optimization_summary */
1327  NULL,                                  /* read_optimization_summary */
1328  NULL,                                  /* stmt_fixup */
1329  0,                                     /* TODOs */
1330  NULL,                                  /* function_transform */
1331  NULL                                   /* variable_transform */
1332 };
1333
1334 /* Return true if function should be skipped for local pure const analysis.  */
1335
1336 static bool
1337 skip_function_for_local_pure_const (struct cgraph_node *node)
1338 {
1339   /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations
1340      we must not promote functions that are called by already processed functions.  */
1341
1342   if (function_called_by_processed_nodes_p ())
1343     {
1344       if (dump_file)
1345         fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
1346       return true;
1347     }
1348   if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
1349     {
1350       if (dump_file)
1351         fprintf (dump_file, "Function is not available or overwrittable; not analyzing.\n");
1352       return true;
1353     }
1354   return false;
1355 }
1356
1357 /* Simple local pass for pure const discovery reusing the analysis from
1358    ipa_pure_const.   This pass is effective when executed together with
1359    other optimization passes in early optimization pass queue.  */
1360
1361 static unsigned int
1362 local_pure_const (void)
1363 {
1364   bool changed = false;
1365   funct_state l;
1366   bool skip;
1367   struct cgraph_node *node;
1368
1369   node = cgraph_node (current_function_decl);
1370   skip = skip_function_for_local_pure_const (node);
1371   if (!warn_suggest_attribute_const
1372       && !warn_suggest_attribute_pure
1373       && skip)
1374     return 0;
1375
1376   /* First do NORETURN discovery.  */
1377   if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
1378       && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0)
1379     {
1380       if (warn_missing_noreturn
1381           && !lang_hooks.missing_noreturn_ok_p (cfun->decl))
1382         warning_at (DECL_SOURCE_LOCATION (cfun->decl), OPT_Wmissing_noreturn,
1383                     "function might be possible candidate "
1384                     "for attribute %<noreturn%>");
1385       if (dump_file)
1386         fprintf (dump_file, "Function found to be noreturn: %s\n",
1387                  lang_hooks.decl_printable_name (current_function_decl, 2));
1388
1389       /* Update declaration and reduce profile to executed once.  */
1390       TREE_THIS_VOLATILE (current_function_decl) = 1;
1391       if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
1392         node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1393
1394       changed = true;
1395     }
1396   l = analyze_function (node, false);
1397
1398   switch (l->pure_const_state)
1399     {
1400     case IPA_CONST:
1401       if (!TREE_READONLY (current_function_decl))
1402         {
1403           warn_function_const (current_function_decl, !l->looping);
1404           if (!skip)
1405             {
1406               cgraph_set_readonly_flag (node, true);
1407               cgraph_set_looping_const_or_pure_flag (node, l->looping);
1408               changed = true;
1409             }
1410           if (dump_file)
1411             fprintf (dump_file, "Function found to be %sconst: %s\n",
1412                      l->looping ? "looping " : "",
1413                      lang_hooks.decl_printable_name (current_function_decl,
1414                                                      2));
1415         }
1416       else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1417                && !l->looping)
1418         {
1419           if (!skip)
1420             {
1421               cgraph_set_looping_const_or_pure_flag (node, false);
1422               changed = true;
1423             }
1424           if (dump_file)
1425             fprintf (dump_file, "Function found to be non-looping: %s\n",
1426                      lang_hooks.decl_printable_name (current_function_decl,
1427                                                      2));
1428         }
1429       break;
1430
1431     case IPA_PURE:
1432       if (!DECL_PURE_P (current_function_decl))
1433         {
1434           if (!skip)
1435             {
1436               cgraph_set_pure_flag (node, true);
1437               cgraph_set_looping_const_or_pure_flag (node, l->looping);
1438               changed = true;
1439             }
1440           warn_function_pure (current_function_decl, !l->looping);
1441           if (dump_file)
1442             fprintf (dump_file, "Function found to be %spure: %s\n",
1443                      l->looping ? "looping " : "",
1444                      lang_hooks.decl_printable_name (current_function_decl,
1445                                                      2));
1446         }
1447       else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1448                && !l->looping)
1449         {
1450           if (!skip)
1451             {
1452               cgraph_set_looping_const_or_pure_flag (node, false);
1453               changed = true;
1454             }
1455           if (dump_file)
1456             fprintf (dump_file, "Function found to be non-looping: %s\n",
1457                      lang_hooks.decl_printable_name (current_function_decl,
1458                                                      2));
1459         }
1460       break;
1461
1462     default:
1463       break;
1464     }
1465   if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1466     {
1467       struct cgraph_edge *e;
1468
1469       cgraph_set_nothrow_flag (node, true);
1470       for (e = node->callers; e; e = e->next_caller)
1471         e->can_throw_external = false;
1472       changed = true;
1473       if (dump_file)
1474         fprintf (dump_file, "Function found to be nothrow: %s\n",
1475                  lang_hooks.decl_printable_name (current_function_decl,
1476                                                  2));
1477     }
1478   if (l)
1479     free (l);
1480   if (changed)
1481     return execute_fixup_cfg ();
1482   else
1483     return 0;
1484 }
1485
1486 struct gimple_opt_pass pass_local_pure_const =
1487 {
1488  {
1489   GIMPLE_PASS,
1490   "local-pure-const",                   /* name */
1491   gate_pure_const,                      /* gate */
1492   local_pure_const,                     /* execute */
1493   NULL,                                 /* sub */
1494   NULL,                                 /* next */
1495   0,                                    /* static_pass_number */
1496   TV_IPA_PURE_CONST,                    /* tv_id */
1497   0,                                    /* properties_required */
1498   0,                                    /* properties_provided */
1499   0,                                    /* properties_destroyed */
1500   0,                                    /* todo_flags_start */
1501   0                                     /* todo_flags_finish */
1502  }
1503 };