LCOV - code coverage report
Current view: top level - gcc - ipa-pure-const.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.6 % 1080 1022
Test Date: 2024-04-20 14:03:02 Functions: 96.6 % 58 56
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Callgraph based analysis of static variables.
       2                 :             :    Copyright (C) 2004-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify it under
       8                 :             : the terms of the GNU General Public License as published by the Free
       9                 :             : Software Foundation; either version 3, or (at your option) any later
      10                 :             : version.
      11                 :             : 
      12                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13                 :             : 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                 :             : /* This file marks functions as being either const (TREE_READONLY) or
      22                 :             :    pure (DECL_PURE_P).  It can also set a variant of these that
      23                 :             :    are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P).
      24                 :             : 
      25                 :             :    This must be run after inlining decisions have been made since
      26                 :             :    otherwise, the local sets will not contain information that is
      27                 :             :    consistent with post inlined state.  The global sets are not prone
      28                 :             :    to this problem since they are by definition transitive.  */
      29                 :             : 
      30                 :             : /* The code in this module is called by the ipa pass manager. It
      31                 :             :    should be one of the later passes since it's information is used by
      32                 :             :    the rest of the compilation. */
      33                 :             : 
      34                 :             : #include "config.h"
      35                 :             : #include "system.h"
      36                 :             : #include "coretypes.h"
      37                 :             : #include "backend.h"
      38                 :             : #include "target.h"
      39                 :             : #include "tree.h"
      40                 :             : #include "gimple.h"
      41                 :             : #include "tree-pass.h"
      42                 :             : #include "tree-streamer.h"
      43                 :             : #include "cgraph.h"
      44                 :             : #include "diagnostic.h"
      45                 :             : #include "calls.h"
      46                 :             : #include "cfganal.h"
      47                 :             : #include "tree-eh.h"
      48                 :             : #include "gimple-iterator.h"
      49                 :             : #include "gimple-walk.h"
      50                 :             : #include "tree-cfg.h"
      51                 :             : #include "tree-ssa-loop-niter.h"
      52                 :             : #include "langhooks.h"
      53                 :             : #include "ipa-utils.h"
      54                 :             : #include "gimple-pretty-print.h"
      55                 :             : #include "cfgloop.h"
      56                 :             : #include "tree-scalar-evolution.h"
      57                 :             : #include "intl.h"
      58                 :             : #include "opts.h"
      59                 :             : #include "ssa.h"
      60                 :             : #include "alloc-pool.h"
      61                 :             : #include "symbol-summary.h"
      62                 :             : #include "sreal.h"
      63                 :             : #include "ipa-cp.h"
      64                 :             : #include "ipa-prop.h"
      65                 :             : #include "ipa-fnsummary.h"
      66                 :             : #include "symtab-thunks.h"
      67                 :             : #include "dbgcnt.h"
      68                 :             : 
      69                 :             : /* Lattice values for const and pure functions.  Everything starts out
      70                 :             :    being const, then may drop to pure and then neither depending on
      71                 :             :    what is found.  */
      72                 :             : enum pure_const_state_e
      73                 :             : {
      74                 :             :   IPA_CONST,
      75                 :             :   IPA_PURE,
      76                 :             :   IPA_NEITHER
      77                 :             : };
      78                 :             : 
      79                 :             : static const char *pure_const_names[3] = {"const", "pure", "neither"};
      80                 :             : 
      81                 :             : enum malloc_state_e
      82                 :             : {
      83                 :             :   STATE_MALLOC_TOP,
      84                 :             :   STATE_MALLOC,
      85                 :             :   STATE_MALLOC_BOTTOM
      86                 :             : };
      87                 :             : 
      88                 :             : static const char *malloc_state_names[] = {"malloc_top", "malloc", "malloc_bottom"};
      89                 :             : 
      90                 :             : /* Holder for the const_state.  There is one of these per function
      91                 :             :    decl.  */
      92                 :             : class funct_state_d
      93                 :             : {
      94                 :             : public:
      95                 :     2429445 :   funct_state_d (): pure_const_state (IPA_NEITHER),
      96                 :     2429445 :     state_previously_known (IPA_NEITHER), looping_previously_known (true),
      97                 :     2429445 :     looping (true), can_throw (true), can_free (true),
      98                 :           0 :     malloc_state (STATE_MALLOC_BOTTOM) {}
      99                 :             : 
     100                 :     2323559 :   funct_state_d (const funct_state_d &s): pure_const_state (s.pure_const_state),
     101                 :     2323559 :     state_previously_known (s.state_previously_known),
     102                 :     2323559 :     looping_previously_known (s.looping_previously_known),
     103                 :     2323559 :     looping (s.looping), can_throw (s.can_throw), can_free (s.can_free),
     104                 :     2323559 :     malloc_state (s.malloc_state) {}
     105                 :             : 
     106                 :             :   /* See above.  */
     107                 :             :   enum pure_const_state_e pure_const_state;
     108                 :             :   /* What user set here; we can be always sure about this.  */
     109                 :             :   enum pure_const_state_e state_previously_known;
     110                 :             :   bool looping_previously_known;
     111                 :             : 
     112                 :             :   /* True if the function could possibly infinite loop.  There are a
     113                 :             :      lot of ways that this could be determined.  We are pretty
     114                 :             :      conservative here.  While it is possible to cse pure and const
     115                 :             :      calls, it is not legal to have dce get rid of the call if there
     116                 :             :      is a possibility that the call could infinite loop since this is
     117                 :             :      a behavioral change.  */
     118                 :             :   bool looping;
     119                 :             : 
     120                 :             :   bool can_throw;
     121                 :             : 
     122                 :             :   /* If function can call free, munmap or otherwise make previously
     123                 :             :      non-trapping memory accesses trapping.  */
     124                 :             :   bool can_free;
     125                 :             : 
     126                 :             :   enum malloc_state_e malloc_state;
     127                 :             : };
     128                 :             : 
     129                 :             : typedef class funct_state_d * funct_state;
     130                 :             : 
     131                 :             : /* The storage of the funct_state is abstracted because there is the
     132                 :             :    possibility that it may be desirable to move this to the cgraph
     133                 :             :    local info.  */
     134                 :             : 
     135                 :             : class funct_state_summary_t:
     136                 :             :   public fast_function_summary <funct_state_d *, va_heap>
     137                 :             : {
     138                 :             : public:
     139                 :      154054 :   funct_state_summary_t (symbol_table *symtab):
     140                 :      308108 :     fast_function_summary <funct_state_d *, va_heap> (symtab) {}
     141                 :             : 
     142                 :             :   void insert (cgraph_node *, funct_state_d *state) final override;
     143                 :             :   void duplicate (cgraph_node *src_node, cgraph_node *dst_node,
     144                 :             :                   funct_state_d *src_data,
     145                 :             :                   funct_state_d *dst_data) final override;
     146                 :             : };
     147                 :             : 
     148                 :             : static funct_state_summary_t *funct_state_summaries = NULL;
     149                 :             : 
     150                 :             : static bool gate_pure_const (void);
     151                 :             : 
     152                 :             : namespace {
     153                 :             : 
     154                 :             : const pass_data pass_data_ipa_pure_const =
     155                 :             : {
     156                 :             :   IPA_PASS, /* type */
     157                 :             :   "pure-const", /* name */
     158                 :             :   OPTGROUP_NONE, /* optinfo_flags */
     159                 :             :   TV_IPA_PURE_CONST, /* tv_id */
     160                 :             :   0, /* properties_required */
     161                 :             :   0, /* properties_provided */
     162                 :             :   0, /* properties_destroyed */
     163                 :             :   0, /* todo_flags_start */
     164                 :             :   0, /* todo_flags_finish */
     165                 :             : };
     166                 :             : 
     167                 :             : class pass_ipa_pure_const : public ipa_opt_pass_d
     168                 :             : {
     169                 :             : public:
     170                 :             :   pass_ipa_pure_const(gcc::context *ctxt);
     171                 :             : 
     172                 :             :   /* opt_pass methods: */
     173                 :     1158926 :   bool gate (function *) final override { return gate_pure_const (); }
     174                 :             :   unsigned int execute (function *fun) final override;
     175                 :             : 
     176                 :             :   void register_hooks (void);
     177                 :             : 
     178                 :             : private:
     179                 :             :   bool init_p;
     180                 :             : }; // class pass_ipa_pure_const
     181                 :             : 
     182                 :             : } // anon namespace
     183                 :             : 
     184                 :             : /* Try to guess if function body will always be visible to compiler
     185                 :             :    when compiling the call and whether compiler will be able
     186                 :             :    to propagate the information by itself.  */
     187                 :             : 
     188                 :             : static bool
     189                 :          27 : function_always_visible_to_compiler_p (tree decl)
     190                 :             : {
     191                 :          23 :   return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl)
     192                 :          50 :           || DECL_COMDAT (decl));
     193                 :             : }
     194                 :             : 
     195                 :             : /* Emit suggestion about attribute ATTRIB_NAME for DECL.  KNOWN_FINITE
     196                 :             :    is true if the function is known to be finite.  The diagnostic is
     197                 :             :    controlled by OPTION.  WARNED_ABOUT is a hash_set<tree> unique for
     198                 :             :    OPTION, this function may initialize it and it is always returned
     199                 :             :    by the function.  */
     200                 :             : 
     201                 :             : static hash_set<tree> *
     202                 :     1121401 : suggest_attribute (int option, tree decl, bool known_finite,
     203                 :             :                    hash_set<tree> *warned_about,
     204                 :             :                    const char * attrib_name)
     205                 :             : {
     206                 :     1121401 :   if (!option_enabled (option, lang_hooks.option_lang_mask (), &global_options))
     207                 :             :     return warned_about;
     208                 :          31 :   if (TREE_THIS_VOLATILE (decl)
     209                 :          31 :       || (known_finite && function_always_visible_to_compiler_p (decl)))
     210                 :             :     return warned_about;
     211                 :             : 
     212                 :          27 :   if (!warned_about)
     213                 :          15 :     warned_about = new hash_set<tree>;
     214                 :          27 :   if (warned_about->contains (decl))
     215                 :             :     return warned_about;
     216                 :          27 :   warned_about->add (decl);
     217                 :          31 :   warning_at (DECL_SOURCE_LOCATION (decl),
     218                 :             :               option,
     219                 :             :               known_finite
     220                 :             :               ? G_("function might be candidate for attribute %qs")
     221                 :             :               : G_("function might be candidate for attribute %qs"
     222                 :             :                    " if it is known to return normally"), attrib_name);
     223                 :          27 :   return warned_about;
     224                 :             : }
     225                 :             : 
     226                 :             : /* Emit suggestion about __attribute_((pure)) for DECL.  KNOWN_FINITE
     227                 :             :    is true if the function is known to be finite.  */
     228                 :             : 
     229                 :             : static void
     230                 :      318320 : warn_function_pure (tree decl, bool known_finite)
     231                 :             : {
     232                 :             :   /* Declaring a void function pure makes no sense and is diagnosed
     233                 :             :      by -Wattributes because calling it would have no effect.  */
     234                 :      318320 :   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
     235                 :             :     return;
     236                 :             : 
     237                 :      291921 :   static hash_set<tree> *warned_about;
     238                 :      291921 :   warned_about
     239                 :      291921 :     = suggest_attribute (OPT_Wsuggest_attribute_pure, decl,
     240                 :             :                          known_finite, warned_about, "pure");
     241                 :             : }
     242                 :             : 
     243                 :             : /* Emit suggestion about __attribute_((const)) for DECL.  KNOWN_FINITE
     244                 :             :    is true if the function is known to be finite.  */
     245                 :             : 
     246                 :             : static void
     247                 :      876341 : warn_function_const (tree decl, bool known_finite)
     248                 :             : {
     249                 :             :   /* Declaring a void function const makes no sense is diagnosed
     250                 :             :      by -Wattributes because calling it would have no effect.  */
     251                 :      876341 :   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
     252                 :             :     return;
     253                 :             : 
     254                 :      614877 :   static hash_set<tree> *warned_about;
     255                 :      614877 :   warned_about
     256                 :      614877 :     = suggest_attribute (OPT_Wsuggest_attribute_const, decl,
     257                 :             :                          known_finite, warned_about, "const");
     258                 :             : }
     259                 :             : 
     260                 :             : /* Emit suggestion about __attribute__((malloc)) for DECL.  */
     261                 :             : 
     262                 :             : static void
     263                 :           3 : warn_function_malloc (tree decl)
     264                 :             : {
     265                 :           3 :   static hash_set<tree> *warned_about;
     266                 :           3 :   warned_about
     267                 :           3 :     = suggest_attribute (OPT_Wsuggest_attribute_malloc, decl,
     268                 :             :                          true, warned_about, "malloc");
     269                 :           3 : }
     270                 :             : 
     271                 :             : /* Emit suggestion about __attribute__((noreturn)) for DECL.  */
     272                 :             : 
     273                 :             : static void
     274                 :       29082 : warn_function_noreturn (tree decl)
     275                 :             : {
     276                 :       29082 :   tree original_decl = decl;
     277                 :             : 
     278                 :       29082 :   static hash_set<tree> *warned_about;
     279                 :       29082 :   if (!lang_hooks.missing_noreturn_ok_p (decl)
     280                 :       29082 :       && targetm.warn_func_return (decl))
     281                 :       17522 :     warned_about
     282                 :       17522 :       = suggest_attribute (OPT_Wsuggest_attribute_noreturn, original_decl,
     283                 :             :                            true, warned_about, "noreturn");
     284                 :       29082 : }
     285                 :             : 
     286                 :             : void
     287                 :       12167 : warn_function_cold (tree decl)
     288                 :             : {
     289                 :       12167 :   tree original_decl = decl;
     290                 :             : 
     291                 :       12167 :   static hash_set<tree> *warned_about;
     292                 :       12167 :   warned_about
     293                 :       12167 :     = suggest_attribute (OPT_Wsuggest_attribute_cold, original_decl,
     294                 :             :                          true, warned_about, "cold");
     295                 :       12167 : }
     296                 :             : 
     297                 :             : void
     298                 :      184911 : warn_function_returns_nonnull (tree decl)
     299                 :             : {
     300                 :      184911 :   static hash_set<tree> *warned_about;
     301                 :      184911 :   warned_about
     302                 :      184911 :     = suggest_attribute (OPT_Wsuggest_attribute_returns_nonnull, decl,
     303                 :             :                          true, warned_about, "returns_nonnull");
     304                 :      184911 : }
     305                 :             : 
     306                 :             : /* Check to see if the use (or definition when CHECKING_WRITE is true)
     307                 :             :    variable T is legal in a function that is either pure or const.  */
     308                 :             : 
     309                 :             : static inline void
     310                 :    20033213 : check_decl (funct_state local,
     311                 :             :             tree t, bool checking_write, bool ipa)
     312                 :             : {
     313                 :             :   /* Do not want to do anything with volatile except mark any
     314                 :             :      function that uses one to be not const or pure.  */
     315                 :    20033213 :   if (TREE_THIS_VOLATILE (t))
     316                 :             :     {
     317                 :     1431469 :       local->pure_const_state = IPA_NEITHER;
     318                 :     1431469 :       if (dump_file)
     319                 :          40 :         fprintf (dump_file, "    Volatile operand is not const/pure\n");
     320                 :     1431469 :       return;
     321                 :             :     }
     322                 :             : 
     323                 :             :   /* Do not care about a local automatic that is not static.  */
     324                 :    18601744 :   if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
     325                 :             :     return;
     326                 :             : 
     327                 :             :   /* If the variable has the "used" attribute, treat it as if it had a
     328                 :             :      been touched by the devil.  */
     329                 :     3326713 :   if (DECL_PRESERVE_P (t))
     330                 :             :     {
     331                 :        1354 :       local->pure_const_state = IPA_NEITHER;
     332                 :        1354 :       if (dump_file)
     333                 :           0 :         fprintf (dump_file, "    Used static/global variable is not const/pure\n");
     334                 :        1354 :       return;
     335                 :             :     }
     336                 :             : 
     337                 :             :   /* In IPA mode we are not interested in checking actual loads and stores;
     338                 :             :      they will be processed at propagation time using ipa_ref.  */
     339                 :     3325359 :   if (ipa)
     340                 :             :     return;
     341                 :             : 
     342                 :             :   /* Since we have dealt with the locals and params cases above, if we
     343                 :             :      are CHECKING_WRITE, this cannot be a pure or constant
     344                 :             :      function.  */
     345                 :     2139827 :   if (checking_write)
     346                 :             :     {
     347                 :      774524 :       local->pure_const_state = IPA_NEITHER;
     348                 :      774524 :       if (dump_file)
     349                 :           1 :         fprintf (dump_file, "    static/global memory write is not const/pure\n");
     350                 :      774524 :       return;
     351                 :             :     }
     352                 :             : 
     353                 :     1365303 :   if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
     354                 :             :     {
     355                 :             :       /* Readonly reads are safe.  */
     356                 :      786638 :       if (TREE_READONLY (t))
     357                 :             :         return; /* Read of a constant, do not change the function state.  */
     358                 :             :       else
     359                 :             :         {
     360                 :      783253 :           if (dump_file)
     361                 :           0 :             fprintf (dump_file, "    global memory read is not const\n");
     362                 :             :           /* Just a regular read.  */
     363                 :      783253 :           if (local->pure_const_state == IPA_CONST)
     364                 :      159393 :             local->pure_const_state = IPA_PURE;
     365                 :             :         }
     366                 :             :     }
     367                 :             :   else
     368                 :             :     {
     369                 :             :       /* Compilation level statics can be read if they are readonly
     370                 :             :          variables.  */
     371                 :      578665 :       if (TREE_READONLY (t))
     372                 :             :         return;
     373                 :             : 
     374                 :      554788 :       if (dump_file)
     375                 :           1 :         fprintf (dump_file, "    static memory read is not const\n");
     376                 :             :       /* Just a regular read.  */
     377                 :      554788 :       if (local->pure_const_state == IPA_CONST)
     378                 :       31394 :         local->pure_const_state = IPA_PURE;
     379                 :             :     }
     380                 :             : }
     381                 :             : 
     382                 :             : 
     383                 :             : /* Check to see if the use (or definition when CHECKING_WRITE is true)
     384                 :             :    variable T is legal in a function that is either pure or const.  */
     385                 :             : 
     386                 :             : static inline void
     387                 :    16201813 : check_op (funct_state local, tree t, bool checking_write)
     388                 :             : {
     389                 :    16201813 :   t = get_base_address (t);
     390                 :    16201813 :   if (t && TREE_THIS_VOLATILE (t))
     391                 :             :     {
     392                 :       32470 :       local->pure_const_state = IPA_NEITHER;
     393                 :       32470 :       if (dump_file)
     394                 :           2 :         fprintf (dump_file, "    Volatile indirect ref is not const/pure\n");
     395                 :       32470 :       return;
     396                 :             :     }
     397                 :    16169343 :   else if (refs_local_or_readonly_memory_p (t))
     398                 :             :     {
     399                 :     3502699 :       if (dump_file)
     400                 :          11 :         fprintf (dump_file, "    Indirect ref to local or readonly "
     401                 :             :                  "memory is OK\n");
     402                 :     3502699 :       return;
     403                 :             :     }
     404                 :    12666644 :   else if (checking_write)
     405                 :             :     {
     406                 :     4435332 :       local->pure_const_state = IPA_NEITHER;
     407                 :     4435332 :       if (dump_file)
     408                 :          66 :         fprintf (dump_file, "    Indirect ref write is not const/pure\n");
     409                 :     4435332 :       return;
     410                 :             :     }
     411                 :             :   else
     412                 :             :     {
     413                 :     8231312 :       if (dump_file)
     414                 :         183 :         fprintf (dump_file, "    Indirect ref read is not const\n");
     415                 :     8231312 :       if (local->pure_const_state == IPA_CONST)
     416                 :     1166230 :         local->pure_const_state = IPA_PURE;
     417                 :             :     }
     418                 :             : }
     419                 :             : 
     420                 :             : /* compute state based on ECF FLAGS and store to STATE and LOOPING.  */
     421                 :             : 
     422                 :             : static void
     423                 :    14634065 : state_from_flags (enum pure_const_state_e *state, bool *looping,
     424                 :             :                   int flags, bool cannot_lead_to_return)
     425                 :             : {
     426                 :    14634065 :   *looping = false;
     427                 :    14634065 :   if (flags & ECF_LOOPING_CONST_OR_PURE)
     428                 :             :     {
     429                 :      188946 :       *looping = true;
     430                 :      188946 :       if (dump_file && (dump_flags & TDF_DETAILS))
     431                 :           0 :         fprintf (dump_file, " looping\n");
     432                 :             :     }
     433                 :    14634065 :   if (flags & ECF_CONST)
     434                 :             :     {
     435                 :     1001900 :       *state = IPA_CONST;
     436                 :     1001900 :       if (dump_file && (dump_flags & TDF_DETAILS))
     437                 :           3 :         fprintf (dump_file, " const\n");
     438                 :             :     }
     439                 :    13632165 :   else if (flags & ECF_PURE)
     440                 :             :     {
     441                 :     1160335 :       *state = IPA_PURE;
     442                 :     1160335 :       if (dump_file && (dump_flags & TDF_DETAILS))
     443                 :           4 :         fprintf (dump_file, " pure\n");
     444                 :             :     }
     445                 :    12471830 :   else if (cannot_lead_to_return)
     446                 :             :     {
     447                 :      875024 :       *state = IPA_PURE;
     448                 :      875024 :       *looping = true;
     449                 :      875024 :       if (dump_file && (dump_flags & TDF_DETAILS))
     450                 :           1 :         fprintf (dump_file, " ignoring side effects->pure looping\n");
     451                 :             :     }
     452                 :             :   else
     453                 :             :     {
     454                 :    11596806 :       if (dump_file && (dump_flags & TDF_DETAILS))
     455                 :          42 :         fprintf (dump_file, " neither\n");
     456                 :    11596806 :       *state = IPA_NEITHER;
     457                 :    11596806 :       *looping = true;
     458                 :             :     }
     459                 :    14634065 : }
     460                 :             : 
     461                 :             : /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
     462                 :             :    into STATE and LOOPING better of the two variants.
     463                 :             :    Be sure to merge looping correctly.  IPA_NEITHER functions
     464                 :             :    have looping 0 even if they don't have to return.  */
     465                 :             : 
     466                 :             : static inline void
     467                 :     6353213 : better_state (enum pure_const_state_e *state, bool *looping,
     468                 :             :               enum pure_const_state_e state2, bool looping2)
     469                 :             : {
     470                 :     6353213 :   if (state2 < *state)
     471                 :             :     {
     472                 :       30228 :       if (*state == IPA_NEITHER)
     473                 :       29512 :         *looping = looping2;
     474                 :             :       else
     475                 :        1432 :         *looping = MIN (*looping, looping2);
     476                 :       30228 :       *state = state2;
     477                 :             :     }
     478                 :     6322985 :   else if (state2 != IPA_NEITHER)
     479                 :     1833759 :     *looping = MIN (*looping, looping2);
     480                 :     6353213 : }
     481                 :             : 
     482                 :             : /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store
     483                 :             :    into STATE and LOOPING worse of the two variants.
     484                 :             :    N is the actual node called.  */
     485                 :             : 
     486                 :             : static inline void
     487                 :    13545918 : worse_state (enum pure_const_state_e *state, bool *looping,
     488                 :             :              enum pure_const_state_e state2, bool looping2,
     489                 :             :              struct symtab_node *from,
     490                 :             :              struct symtab_node *to)
     491                 :             : {
     492                 :             :   /* Consider function:
     493                 :             : 
     494                 :             :      bool a(int *p)
     495                 :             :      {
     496                 :             :        return *p==*p;
     497                 :             :      }
     498                 :             : 
     499                 :             :      During early optimization we will turn this into:
     500                 :             : 
     501                 :             :      bool a(int *p)
     502                 :             :      {
     503                 :             :        return true;
     504                 :             :      }
     505                 :             : 
     506                 :             :      Now if this function will be detected as CONST however when interposed it
     507                 :             :      may end up being just pure.  We always must assume the worst scenario here.
     508                 :             :    */
     509                 :    13545918 :   if (*state == IPA_CONST && state2 == IPA_CONST
     510                 :    13545918 :       && to && !TREE_READONLY (to->decl) && !to->binds_to_current_def_p (from))
     511                 :             :     {
     512                 :        2618 :       if (dump_file && (dump_flags & TDF_DETAILS))
     513                 :           0 :         fprintf (dump_file, "Dropping state to PURE because call to %s may not "
     514                 :             :                  "bind to current def.\n", to->dump_name ());
     515                 :             :       state2 = IPA_PURE;
     516                 :             :     }
     517                 :    13545918 :   *state = MAX (*state, state2);
     518                 :    13545918 :   *looping = MAX (*looping, looping2);
     519                 :    13545918 : }
     520                 :             : 
     521                 :             : /* Recognize special cases of builtins that are by themselves not const
     522                 :             :    but function using them is.  */
     523                 :             : bool
     524                 :    22125472 : builtin_safe_for_const_function_p (bool *looping, tree callee)
     525                 :             : {
     526                 :    22125472 :   if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
     527                 :     6006473 :     switch (DECL_FUNCTION_CODE (callee))
     528                 :             :       {
     529                 :      534474 :       case BUILT_IN_RETURN:
     530                 :      534474 :       case BUILT_IN_UNREACHABLE:
     531                 :      534474 :       CASE_BUILT_IN_ALLOCA:
     532                 :      534474 :       case BUILT_IN_STACK_SAVE:
     533                 :      534474 :       case BUILT_IN_STACK_RESTORE:
     534                 :      534474 :       case BUILT_IN_EH_POINTER:
     535                 :      534474 :       case BUILT_IN_EH_FILTER:
     536                 :      534474 :       case BUILT_IN_UNWIND_RESUME:
     537                 :      534474 :       case BUILT_IN_CXA_END_CLEANUP:
     538                 :      534474 :       case BUILT_IN_EH_COPY_VALUES:
     539                 :      534474 :       case BUILT_IN_FRAME_ADDRESS:
     540                 :      534474 :       case BUILT_IN_APPLY_ARGS:
     541                 :      534474 :       case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
     542                 :      534474 :       case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
     543                 :      534474 :       case BUILT_IN_DWARF_CFA:
     544                 :      534474 :       case BUILT_IN_RETURN_ADDRESS:
     545                 :      534474 :         *looping = false;
     546                 :      534474 :         return true;
     547                 :        5386 :       case BUILT_IN_PREFETCH:
     548                 :        5386 :         *looping = true;
     549                 :        5386 :         return true;
     550                 :             :       default:
     551                 :             :         break;
     552                 :             :       }
     553                 :             :   return false;
     554                 :             : }
     555                 :             : 
     556                 :             : /* Check the parameters of a function call to CALL_EXPR to see if
     557                 :             :    there are any references in the parameters that are not allowed for
     558                 :             :    pure or const functions.  Also check to see if this is either an
     559                 :             :    indirect call, a call outside the compilation unit, or has special
     560                 :             :    attributes that may also effect the purity.  The CALL_EXPR node for
     561                 :             :    the entire call expression.  */
     562                 :             : 
     563                 :             : static void
     564                 :    14322869 : check_call (funct_state local, gcall *call, bool ipa)
     565                 :             : {
     566                 :    14322869 :   int flags = gimple_call_flags (call);
     567                 :    14322869 :   tree callee_t = gimple_call_fndecl (call);
     568                 :    14322869 :   bool possibly_throws = stmt_could_throw_p (cfun, call);
     569                 :    14322869 :   bool possibly_throws_externally = (possibly_throws
     570                 :    14322869 :                                      && stmt_can_throw_external (cfun, call));
     571                 :             : 
     572                 :     5392522 :   if (possibly_throws)
     573                 :             :     {
     574                 :             :       unsigned int i;
     575                 :    32299916 :       for (i = 0; i < gimple_num_ops (call); i++)
     576                 :    26907394 :         if (gimple_op (call, i)
     577                 :    26907394 :             && tree_could_throw_p (gimple_op (call, i)))
     578                 :             :           {
     579                 :       80167 :             if (possibly_throws && cfun->can_throw_non_call_exceptions)
     580                 :             :               {
     581                 :       80167 :                 if (dump_file)
     582                 :           0 :                   fprintf (dump_file, "    operand can throw; looping\n");
     583                 :       80167 :                 local->looping = true;
     584                 :             :               }
     585                 :       80167 :             if (possibly_throws_externally)
     586                 :             :               {
     587                 :       66810 :                 if (dump_file)
     588                 :           0 :                   fprintf (dump_file, "    operand can throw externally\n");
     589                 :       66810 :                 local->can_throw = true;
     590                 :             :               }
     591                 :             :           }
     592                 :             :     }
     593                 :             : 
     594                 :             :   /* The const and pure flags are set by a variety of places in the
     595                 :             :      compiler (including here).  If someone has already set the flags
     596                 :             :      for the callee, (such as for some of the builtins) we will use
     597                 :             :      them, otherwise we will compute our own information.
     598                 :             : 
     599                 :             :      Const and pure functions have less clobber effects than other
     600                 :             :      functions so we process these first.  Otherwise if it is a call
     601                 :             :      outside the compilation unit or an indirect call we punt.  This
     602                 :             :      leaves local calls which will be processed by following the call
     603                 :             :      graph.  */
     604                 :    14322869 :   if (callee_t)
     605                 :             :     {
     606                 :    13426302 :       bool call_looping;
     607                 :             : 
     608                 :    13426302 :       if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
     609                 :    13426302 :           && !nonfreeing_call_p (call))
     610                 :      631293 :         local->can_free = true;
     611                 :             : 
     612                 :    13426302 :       if (builtin_safe_for_const_function_p (&call_looping, callee_t))
     613                 :             :         {
     614                 :      180186 :           worse_state (&local->pure_const_state, &local->looping,
     615                 :             :                        IPA_CONST, call_looping,
     616                 :             :                        NULL, NULL);
     617                 :      180186 :           return;
     618                 :             :         }
     619                 :             :       /* When bad things happen to bad functions, they cannot be const
     620                 :             :          or pure.  */
     621                 :    13246116 :       if (setjmp_call_p (callee_t))
     622                 :             :         {
     623                 :        2368 :           if (dump_file)
     624                 :           0 :             fprintf (dump_file, "    setjmp is not const/pure\n");
     625                 :        2368 :           local->looping = true;
     626                 :        2368 :           local->pure_const_state = IPA_NEITHER;
     627                 :             :         }
     628                 :             : 
     629                 :    13246116 :       if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
     630                 :     3082969 :         switch (DECL_FUNCTION_CODE (callee_t))
     631                 :             :           {
     632                 :        1856 :           case BUILT_IN_LONGJMP:
     633                 :        1856 :           case BUILT_IN_NONLOCAL_GOTO:
     634                 :        1856 :             if (dump_file)
     635                 :           0 :               fprintf (dump_file,
     636                 :             :                        "    longjmp and nonlocal goto is not const/pure\n");
     637                 :        1856 :             local->pure_const_state = IPA_NEITHER;
     638                 :        1856 :             local->looping = true;
     639                 :        1856 :             break;
     640                 :             :           default:
     641                 :             :             break;
     642                 :             :           }
     643                 :             :     }
     644                 :      896567 :   else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call))
     645                 :      139762 :     local->can_free = true;
     646                 :             : 
     647                 :             :   /* When not in IPA mode, we can still handle self recursion.  */
     648                 :    14142683 :   if (!ipa && callee_t
     649                 :    14142683 :       && recursive_call_p (current_function_decl, callee_t))
     650                 :             :     {
     651                 :       19180 :       if (dump_file)
     652                 :           0 :         fprintf (dump_file, "    Recursive call can loop.\n");
     653                 :       19180 :       local->looping = true;
     654                 :             :     }
     655                 :             :   /* Either callee is unknown or we are doing local analysis.
     656                 :             :      Look to see if there are any bits available for the callee (such as by
     657                 :             :      declaration or because it is builtin) and process solely on the basis of
     658                 :             :      those bits.  Handle internal calls always, those calls don't have
     659                 :             :      corresponding cgraph edges and thus aren't processed during
     660                 :             :      the propagation.  */
     661                 :    14123503 :   else if (!ipa || gimple_call_internal_p (call))
     662                 :             :     {
     663                 :     9232552 :       enum pure_const_state_e call_state;
     664                 :     9232552 :       bool call_looping;
     665                 :     9232552 :       if (possibly_throws && cfun->can_throw_non_call_exceptions)
     666                 :             :         {
     667                 :     2147640 :           if (dump_file)
     668                 :           0 :             fprintf (dump_file, "    can throw; looping\n");
     669                 :     2147640 :           local->looping = true;
     670                 :             :         }
     671                 :     9232552 :       if (possibly_throws_externally)
     672                 :             :         {
     673                 :     2524661 :           if (dump_file)
     674                 :             :             {
     675                 :           0 :               fprintf (dump_file, "    can throw externally to lp %i\n",
     676                 :             :                        lookup_stmt_eh_lp (call));
     677                 :           0 :               if (callee_t)
     678                 :           0 :                 fprintf (dump_file, "     callee:%s\n",
     679                 :           0 :                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t)));
     680                 :             :             }
     681                 :     2524661 :           local->can_throw = true;
     682                 :             :         }
     683                 :     9232552 :       if (dump_file && (dump_flags & TDF_DETAILS))
     684                 :          22 :         fprintf (dump_file, "    checking flags for call:");
     685                 :     9232552 :       state_from_flags (&call_state, &call_looping, flags,
     686                 :     9232552 :                         ((flags & (ECF_NORETURN | ECF_NOTHROW))
     687                 :             :                          == (ECF_NORETURN | ECF_NOTHROW))
     688                 :     9232552 :                         || (!flag_exceptions && (flags & ECF_NORETURN)));
     689                 :     9232552 :       worse_state (&local->pure_const_state, &local->looping,
     690                 :             :                    call_state, call_looping, NULL, NULL);
     691                 :             :     }
     692                 :             :   /* Direct functions calls are handled by IPA propagation.  */
     693                 :             : }
     694                 :             : 
     695                 :             : /* Wrapper around check_decl for loads in local more.  */
     696                 :             : 
     697                 :             : static bool
     698                 :    11632266 : check_load (gimple *, tree op, tree, void *data)
     699                 :             : {
     700                 :    11632266 :   if (DECL_P (op))
     701                 :     5179686 :     check_decl ((funct_state)data, op, false, false);
     702                 :             :   else
     703                 :     6452580 :     check_op ((funct_state)data, op, false);
     704                 :    11632266 :   return false;
     705                 :             : }
     706                 :             : 
     707                 :             : /* Wrapper around check_decl for stores in local more.  */
     708                 :             : 
     709                 :             : static bool
     710                 :    12710780 : check_store (gimple *, tree op, tree, void *data)
     711                 :             : {
     712                 :    12710780 :   if (DECL_P (op))
     713                 :     7724843 :     check_decl ((funct_state)data, op, true, false);
     714                 :             :   else
     715                 :     4985937 :     check_op ((funct_state)data, op, true);
     716                 :    12710780 :   return false;
     717                 :             : }
     718                 :             : 
     719                 :             : /* Wrapper around check_decl for loads in ipa mode.  */
     720                 :             : 
     721                 :             : static bool
     722                 :     5845601 : check_ipa_load (gimple *, tree op, tree, void *data)
     723                 :             : {
     724                 :     5845601 :   if (DECL_P (op))
     725                 :     2960501 :     check_decl ((funct_state)data, op, false, true);
     726                 :             :   else
     727                 :     2885100 :     check_op ((funct_state)data, op, false);
     728                 :     5845601 :   return false;
     729                 :             : }
     730                 :             : 
     731                 :             : /* Wrapper around check_decl for stores in ipa mode.  */
     732                 :             : 
     733                 :             : static bool
     734                 :     6046379 : check_ipa_store (gimple *, tree op, tree, void *data)
     735                 :             : {
     736                 :     6046379 :   if (DECL_P (op))
     737                 :     4168183 :     check_decl ((funct_state)data, op, true, true);
     738                 :             :   else
     739                 :     1878196 :     check_op ((funct_state)data, op, true);
     740                 :     6046379 :   return false;
     741                 :             : }
     742                 :             : 
     743                 :             : /* Look into pointer pointed to by GSIP and figure out what interesting side
     744                 :             :    effects it has.  */
     745                 :             : static void
     746                 :   160424914 : check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
     747                 :             : {
     748                 :   160424914 :   gimple *stmt = gsi_stmt (*gsip);
     749                 :             : 
     750                 :   160424914 :   if (is_gimple_debug (stmt))
     751                 :             :     return;
     752                 :             : 
     753                 :             :   /* Do consider clobber as side effects before IPA, so we rather inline
     754                 :             :      C++ destructors and keep clobber semantics than eliminate them.
     755                 :             : 
     756                 :             :      Similar logic is in ipa-modref.
     757                 :             : 
     758                 :             :      TODO: We may get smarter during early optimizations on these and let
     759                 :             :      functions containing only clobbers to be optimized more.  This is a common
     760                 :             :      case of C++ destructors.  */
     761                 :             : 
     762                 :    85494159 :   if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt))
     763                 :             :     return;
     764                 :             : 
     765                 :    83266534 :   if (dump_file)
     766                 :             :     {
     767                 :        1783 :       fprintf (dump_file, "  scanning: ");
     768                 :        1783 :       print_gimple_stmt (dump_file, stmt, 0);
     769                 :             :     }
     770                 :             : 
     771                 :    83266534 :   if (gimple_has_volatile_ops (stmt)
     772                 :    71143876 :       && !gimple_clobber_p (stmt))
     773                 :             :     {
     774                 :     1474230 :       local->pure_const_state = IPA_NEITHER;
     775                 :     1474230 :       if (dump_file)
     776                 :          43 :         fprintf (dump_file, "    Volatile stmt is not const/pure\n");
     777                 :             :     }
     778                 :             : 
     779                 :             :   /* Look for loads and stores.  */
     780                 :   139452919 :   walk_stmt_load_store_ops (stmt, local,
     781                 :             :                             ipa ? check_ipa_load : check_load,
     782                 :             :                             ipa ? check_ipa_store :  check_store);
     783                 :             : 
     784                 :    83266534 :   if (gimple_code (stmt) != GIMPLE_CALL
     785                 :    83266534 :       && stmt_could_throw_p (cfun, stmt))
     786                 :             :     {
     787                 :     2653128 :       if (cfun->can_throw_non_call_exceptions)
     788                 :             :         {
     789                 :     2354154 :           if (dump_file)
     790                 :           0 :             fprintf (dump_file, "    can throw; looping\n");
     791                 :     2354154 :           local->looping = true;
     792                 :             :         }
     793                 :     2653128 :       if (stmt_can_throw_external (cfun, stmt))
     794                 :             :         {
     795                 :     2244145 :           if (dump_file)
     796                 :           8 :             fprintf (dump_file, "    can throw externally\n");
     797                 :     2244145 :           local->can_throw = true;
     798                 :             :         }
     799                 :             :       else
     800                 :      408983 :         if (dump_file)
     801                 :           0 :           fprintf (dump_file, "    can throw\n");
     802                 :             :     }
     803                 :    83266534 :   switch (gimple_code (stmt))
     804                 :             :     {
     805                 :    14322869 :     case GIMPLE_CALL:
     806                 :    14322869 :       check_call (local, as_a <gcall *> (stmt), ipa);
     807                 :    14322869 :       break;
     808                 :     1603615 :     case GIMPLE_LABEL:
     809                 :     1603615 :       if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
     810                 :             :         /* Target of long jump. */
     811                 :             :         {
     812                 :         812 :           if (dump_file)
     813                 :           0 :             fprintf (dump_file, "    nonlocal label is not const/pure\n");
     814                 :         812 :           local->pure_const_state = IPA_NEITHER;
     815                 :             :         }
     816                 :             :       break;
     817                 :      282508 :     case GIMPLE_ASM:
     818                 :      282508 :       if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt)))
     819                 :             :         {
     820                 :      118075 :           if (dump_file)
     821                 :           4 :             fprintf (dump_file, "    memory asm clobber is not const/pure\n");
     822                 :             :           /* Abandon all hope, ye who enter here. */
     823                 :      118075 :           local->pure_const_state = IPA_NEITHER;
     824                 :      118075 :           local->can_free = true;
     825                 :             :         }
     826                 :      282508 :       if (gimple_asm_volatile_p (as_a <gasm *> (stmt)))
     827                 :             :         {
     828                 :      254524 :           if (dump_file)
     829                 :           4 :             fprintf (dump_file, "    volatile is not const/pure\n");
     830                 :             :           /* Abandon all hope, ye who enter here. */
     831                 :      254524 :           local->pure_const_state = IPA_NEITHER;
     832                 :      254524 :           local->looping = true;
     833                 :      254524 :           local->can_free = true;
     834                 :             :         }
     835                 :             :       return;
     836                 :             :     default:
     837                 :             :       break;
     838                 :             :     }
     839                 :             : }
     840                 :             : 
     841                 :             : /* Check that RETVAL is used only in STMT and in comparisons against 0.
     842                 :             :    RETVAL is return value of the function and STMT is return stmt.  */
     843                 :             : 
     844                 :             : static bool
     845                 :      414086 : check_retval_uses (tree retval, gimple *stmt)
     846                 :             : {
     847                 :      414086 :   imm_use_iterator use_iter;
     848                 :      414086 :   gimple *use_stmt;
     849                 :             : 
     850                 :      892258 :   FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, retval)
     851                 :      598449 :     if (gcond *cond = dyn_cast<gcond *> (use_stmt))
     852                 :             :       {
     853                 :       14097 :         tree op2 = gimple_cond_rhs (cond);
     854                 :       14097 :         if (!integer_zerop (op2))
     855                 :             :           return false;
     856                 :             :       }
     857                 :      584352 :     else if (gassign *ga = dyn_cast<gassign *> (use_stmt))
     858                 :             :       {
     859                 :       78767 :         enum tree_code code = gimple_assign_rhs_code (ga);
     860                 :       78767 :         if (TREE_CODE_CLASS (code) != tcc_comparison)
     861                 :             :           return false;
     862                 :        2670 :         if (!integer_zerop (gimple_assign_rhs2 (ga)))
     863                 :             :           return false;
     864                 :             :       }
     865                 :      505585 :     else if (is_gimple_debug (use_stmt))
     866                 :             :       ;
     867                 :      406391 :     else if (use_stmt != stmt)
     868                 :      414086 :       return false;
     869                 :             : 
     870                 :      293809 :   return true;
     871                 :             : }
     872                 :             : 
     873                 :             : /* malloc_candidate_p() checks if FUN can possibly be annotated with malloc
     874                 :             :    attribute. Currently this function does a very conservative analysis.
     875                 :             :    FUN is considered to be a candidate if
     876                 :             :    1) It returns a value of pointer type.
     877                 :             :    2) SSA_NAME_DEF_STMT (return_value) is either a function call or
     878                 :             :       a phi, and element of phi is either NULL or
     879                 :             :       SSA_NAME_DEF_STMT(element) is function call.
     880                 :             :    3) The return-value has immediate uses only within comparisons (gcond or gassign)
     881                 :             :       and return_stmt (and likewise a phi arg has immediate use only within comparison
     882                 :             :       or the phi stmt).  */
     883                 :             : 
     884                 :             : #define DUMP_AND_RETURN(reason)  \
     885                 :             : {  \
     886                 :             :   if (dump_file && (dump_flags & TDF_DETAILS))  \
     887                 :             :     fprintf (dump_file, "\n%s is not a malloc candidate, reason: %s\n", \
     888                 :             :              (node->dump_name ()), (reason));  \
     889                 :             :   return false;  \
     890                 :             : }
     891                 :             : 
     892                 :             : static bool
     893                 :      354820 : malloc_candidate_p_1 (function *fun, tree retval, gimple *ret_stmt, bool ipa,
     894                 :             :                       bitmap visited)
     895                 :             : {
     896                 :      354820 :   cgraph_node *node = cgraph_node::get_create (fun->decl);
     897                 :      354820 :   if (!bitmap_set_bit (visited, SSA_NAME_VERSION (retval)))
     898                 :             :     return true;
     899                 :             : 
     900                 :      354820 :   if (!check_retval_uses (retval, ret_stmt))
     901                 :       94887 :     DUMP_AND_RETURN("Return value has uses outside return stmt"
     902                 :             :                     " and comparisons against 0.")
     903                 :             : 
     904                 :      259933 :   gimple *def = SSA_NAME_DEF_STMT (retval);
     905                 :             : 
     906                 :      259933 :   if (gcall *call_stmt = dyn_cast<gcall *> (def))
     907                 :             :     {
     908                 :       62196 :       tree callee_decl = gimple_call_fndecl (call_stmt);
     909                 :       62196 :       if (!callee_decl)
     910                 :             :         return false;
     911                 :             : 
     912                 :      112421 :       if (!ipa && !DECL_IS_MALLOC (callee_decl))
     913                 :       33650 :         DUMP_AND_RETURN("callee_decl does not have malloc attribute for"
     914                 :             :                         " non-ipa mode.")
     915                 :             : 
     916                 :       27159 :       cgraph_edge *cs = node->get_edge (call_stmt);
     917                 :       27159 :       if (cs)
     918                 :             :         {
     919                 :        9197 :           ipa_call_summary *es = ipa_call_summaries->get_create (cs);
     920                 :        9197 :           es->is_return_callee_uncaptured = true;
     921                 :             :         }
     922                 :             :     }
     923                 :             : 
     924                 :      197737 :     else if (gphi *phi = dyn_cast<gphi *> (def))
     925                 :             :       {
     926                 :             :         bool all_args_zero = true;
     927                 :       82099 :         for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
     928                 :             :           {
     929                 :       77769 :             tree arg = gimple_phi_arg_def (phi, i);
     930                 :       77769 :             if (integer_zerop (arg))
     931                 :       15954 :               continue;
     932                 :             : 
     933                 :       61815 :             all_args_zero = false;
     934                 :       61815 :             if (TREE_CODE (arg) != SSA_NAME)
     935                 :        2549 :               DUMP_AND_RETURN ("phi arg is not SSA_NAME.");
     936                 :       59266 :             if (!check_retval_uses (arg, phi))
     937                 :       25390 :               DUMP_AND_RETURN ("phi arg has uses outside phi"
     938                 :             :                                  " and comparisons against 0.")
     939                 :             : 
     940                 :       33876 :             gimple *arg_def = SSA_NAME_DEF_STMT (arg);
     941                 :       33876 :             if (is_a<gphi *> (arg_def))
     942                 :             :               {
     943                 :        3455 :                 if (!malloc_candidate_p_1 (fun, arg, phi, ipa, visited))
     944                 :        3371 :                     DUMP_AND_RETURN ("nested phi fail")
     945                 :          84 :                 continue;
     946                 :             :               }
     947                 :             : 
     948                 :       30421 :             gcall *call_stmt = dyn_cast<gcall *> (arg_def);
     949                 :       30421 :             if (!call_stmt)
     950                 :       15550 :               DUMP_AND_RETURN ("phi arg is a not a call_stmt.")
     951                 :             : 
     952                 :       14871 :             tree callee_decl = gimple_call_fndecl (call_stmt);
     953                 :       14871 :             if (!callee_decl)
     954                 :             :               return false;
     955                 :       19061 :             if (!ipa && !DECL_IS_MALLOC (callee_decl))
     956                 :        4159 :               DUMP_AND_RETURN("callee_decl does not have malloc attribute"
     957                 :             :                               " for non-ipa mode.")
     958                 :             : 
     959                 :        7828 :             cgraph_edge *cs = node->get_edge (call_stmt);
     960                 :        7828 :             if (cs)
     961                 :             :               {
     962                 :        5071 :                 ipa_call_summary *es = ipa_call_summaries->get_create (cs);
     963                 :        5071 :                 es->is_return_callee_uncaptured = true;
     964                 :             :               }
     965                 :             :           }
     966                 :             : 
     967                 :        4330 :         if (all_args_zero)
     968                 :           4 :           DUMP_AND_RETURN ("Return value is a phi with all args equal to 0.")
     969                 :             :       }
     970                 :             : 
     971                 :             :     else
     972                 :      139504 :       DUMP_AND_RETURN("def_stmt of return value is not a call or phi-stmt.")
     973                 :             : 
     974                 :             :   return true;
     975                 :             : }
     976                 :             : 
     977                 :             : static bool
     978                 :     5561715 : malloc_candidate_p (function *fun, bool ipa)
     979                 :             : {
     980                 :     5561715 :   basic_block exit_block = EXIT_BLOCK_PTR_FOR_FN (fun);
     981                 :     5561715 :   edge e;
     982                 :     5561715 :   edge_iterator ei;
     983                 :     5561715 :   cgraph_node *node = cgraph_node::get_create (fun->decl);
     984                 :             : 
     985                 :     5561715 :   if (EDGE_COUNT (exit_block->preds) == 0
     986                 :     5559371 :       || !flag_delete_null_pointer_checks)
     987                 :             :     return false;
     988                 :             : 
     989                 :     5433039 :   auto_bitmap visited;
     990                 :     5464440 :   FOR_EACH_EDGE (e, ei, exit_block->preds)
     991                 :             :     {
     992                 :     5433039 :       gimple_stmt_iterator gsi = gsi_last_bb (e->src);
     993                 :    10833426 :       greturn *ret_stmt = dyn_cast<greturn *> (gsi_stmt (gsi));
     994                 :             : 
     995                 :     5431788 :       if (!ret_stmt)
     996                 :     5433039 :         return false;
     997                 :             : 
     998                 :     5431788 :       tree retval = gimple_return_retval (ret_stmt);
     999                 :     5431788 :       if (!retval)
    1000                 :     2465946 :         DUMP_AND_RETURN("No return value.")
    1001                 :             : 
    1002                 :     2965842 :       if (TREE_CODE (retval) != SSA_NAME
    1003                 :     2965842 :           || TREE_CODE (TREE_TYPE (retval)) != POINTER_TYPE)
    1004                 :     2614477 :         DUMP_AND_RETURN("Return value is not SSA_NAME or not a pointer type.")
    1005                 :             : 
    1006                 :      351365 :       if (!malloc_candidate_p_1 (fun, retval, ret_stmt, ipa, visited))
    1007                 :             :         return false;
    1008                 :             :     }
    1009                 :             : 
    1010                 :       31401 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1011                 :           8 :     fprintf (dump_file, "\nFound %s to be candidate for malloc attribute\n",
    1012                 :           4 :              IDENTIFIER_POINTER (DECL_NAME (fun->decl)));
    1013                 :             :   return true;
    1014                 :     5433039 : }
    1015                 :             : 
    1016                 :             : #undef DUMP_AND_RETURN
    1017                 :             : 
    1018                 :             : /* Return true if function is known to be finite.  */
    1019                 :             : 
    1020                 :             : bool
    1021                 :     3921027 : finite_function_p ()
    1022                 :             : {
    1023                 :             :   /* Const functions cannot have back edges (an
    1024                 :             :      indication of possible infinite loop side
    1025                 :             :      effect.  */
    1026                 :     3921027 :   bool finite = true;
    1027                 :     3921027 :   if (mark_dfs_back_edges ())
    1028                 :             :     {
    1029                 :             :       /* Preheaders are needed for SCEV to work.
    1030                 :             :          Simple latches and recorded exits improve chances that loop will
    1031                 :             :          proved to be finite in testcases such as in loop-15.c
    1032                 :             :          and loop-24.c  */
    1033                 :      377454 :       loop_optimizer_init (LOOPS_HAVE_PREHEADERS
    1034                 :             :                            | LOOPS_HAVE_SIMPLE_LATCHES
    1035                 :             :                            | LOOPS_HAVE_RECORDED_EXITS);
    1036                 :      377454 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1037                 :           0 :         flow_loops_dump (dump_file, NULL, 0);
    1038                 :      377454 :       if (mark_irreducible_loops ())
    1039                 :             :         {
    1040                 :        1808 :           if (dump_file)
    1041                 :           0 :             fprintf (dump_file, "    has irreducible loops\n");
    1042                 :             :           finite = false;
    1043                 :             :         }
    1044                 :             :       else
    1045                 :             :         {
    1046                 :      375646 :           scev_initialize ();
    1047                 :     1860990 :           for (auto loop : loops_list (cfun, 0))
    1048                 :      807373 :             if (!finite_loop_p (loop))
    1049                 :             :               {
    1050                 :       73321 :                 if (dump_file)
    1051                 :           1 :                   fprintf (dump_file, "    cannot prove finiteness of "
    1052                 :             :                            "loop %i\n", loop->num);
    1053                 :             :                 finite =false;
    1054                 :             :                 break;
    1055                 :      375646 :               }
    1056                 :      375646 :           scev_finalize ();
    1057                 :             :         }
    1058                 :      377454 :       loop_optimizer_finalize ();
    1059                 :             :     }
    1060                 :     3921027 :   return finite;
    1061                 :             : }
    1062                 :             : 
    1063                 :             : /* This is the main routine for finding the reference patterns for
    1064                 :             :    global variables within a function FN.  */
    1065                 :             : 
    1066                 :             : static funct_state
    1067                 :     4380027 : analyze_function (struct cgraph_node *fn, bool ipa)
    1068                 :             : {
    1069                 :     4380027 :   tree decl = fn->decl;
    1070                 :     4380027 :   funct_state l;
    1071                 :     4380027 :   basic_block this_block;
    1072                 :             : 
    1073                 :     4380027 :   l = XCNEW (class funct_state_d);
    1074                 :     4380027 :   l->pure_const_state = IPA_CONST;
    1075                 :     4380027 :   l->state_previously_known = IPA_NEITHER;
    1076                 :     4380027 :   l->looping_previously_known = true;
    1077                 :     4380027 :   l->looping = false;
    1078                 :     4380027 :   l->can_throw = false;
    1079                 :     4380027 :   l->can_free = false;
    1080                 :     4380027 :   state_from_flags (&l->state_previously_known, &l->looping_previously_known,
    1081                 :     4380027 :                     flags_from_decl_or_type (fn->decl),
    1082                 :     4380027 :                     fn->cannot_return_p ());
    1083                 :             : 
    1084                 :     4380027 :   if (fn->thunk || fn->alias)
    1085                 :             :     {
    1086                 :             :       /* Thunk gets propagated through, so nothing interesting happens.  */
    1087                 :       61831 :       gcc_assert (ipa);
    1088                 :       61831 :       if (fn->thunk && thunk_info::get (fn)->virtual_offset_p)
    1089                 :         810 :         l->pure_const_state = IPA_NEITHER;
    1090                 :       61831 :       return l;
    1091                 :             :     }
    1092                 :             : 
    1093                 :     4318196 :   if (dump_file)
    1094                 :             :     {
    1095                 :         201 :       fprintf (dump_file, "\n\n local analysis of %s\n ",
    1096                 :             :                fn->dump_name ());
    1097                 :             :     }
    1098                 :             : 
    1099                 :     4318196 :   push_cfun (DECL_STRUCT_FUNCTION (decl));
    1100                 :             : 
    1101                 :    32984030 :   FOR_EACH_BB_FN (this_block, cfun)
    1102                 :             :     {
    1103                 :    28730291 :       gimple_stmt_iterator gsi;
    1104                 :    28730291 :       struct walk_stmt_info wi;
    1105                 :             : 
    1106                 :    28730291 :       memset (&wi, 0, sizeof (wi));
    1107                 :    57460582 :       for (gsi = gsi_start_bb (this_block);
    1108                 :   189090748 :            !gsi_end_p (gsi);
    1109                 :   160360457 :            gsi_next (&gsi))
    1110                 :             :         {
    1111                 :             :           /* NULL memory accesses terminates BB.  These accesses are known
    1112                 :             :              to trip undefined behaviour.  gimple-ssa-isolate-paths turns them
    1113                 :             :              to volatile accesses and adds builtin_trap call which would
    1114                 :             :              confuse us otherwise.  */
    1115                 :   160427087 :           if (infer_nonnull_range_by_dereference (gsi_stmt (gsi),
    1116                 :             :                                                   null_pointer_node))
    1117                 :             :             {
    1118                 :        2173 :               if (dump_file)
    1119                 :           0 :                 fprintf (dump_file, "  NULL memory access; terminating BB%s\n",
    1120                 :           0 :                          flag_non_call_exceptions ? "; looping" : "");
    1121                 :        2173 :               if (flag_non_call_exceptions)
    1122                 :             :                 {
    1123                 :         402 :                   l->looping = true;
    1124                 :         402 :                   if (stmt_can_throw_external (cfun, gsi_stmt (gsi)))
    1125                 :             :                     {
    1126                 :         308 :                       if (dump_file)
    1127                 :           0 :                         fprintf (dump_file, "    can throw externally\n");
    1128                 :         308 :                       l->can_throw = true;
    1129                 :             :                     }
    1130                 :             :                 }
    1131                 :             :               break;
    1132                 :             :             }
    1133                 :   160424914 :           check_stmt (&gsi, l, ipa);
    1134                 :   160424914 :           if (l->pure_const_state == IPA_NEITHER
    1135                 :   104083032 :               && l->looping
    1136                 :    84370595 :               && l->can_throw
    1137                 :    42970354 :               && l->can_free)
    1138                 :       64457 :             goto end;
    1139                 :             :         }
    1140                 :             :     }
    1141                 :             : 
    1142                 :     4253739 : end:
    1143                 :     4318196 :   if (l->pure_const_state != IPA_NEITHER
    1144                 :     1884840 :       && !l->looping
    1145                 :     6024343 :       && !finite_function_p ())
    1146                 :       23229 :     l->looping = true;
    1147                 :             : 
    1148                 :     4318196 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1149                 :          23 :     fprintf (dump_file, "    checking previously known:");
    1150                 :             : 
    1151                 :     4318196 :   better_state (&l->pure_const_state, &l->looping,
    1152                 :             :                 l->state_previously_known,
    1153                 :     4318196 :                 l->looping_previously_known);
    1154                 :     4318196 :   if (TREE_NOTHROW (decl))
    1155                 :     2926592 :     l->can_throw = false;
    1156                 :             : 
    1157                 :     4318196 :   l->malloc_state = STATE_MALLOC_BOTTOM;
    1158                 :     4318196 :   if (DECL_IS_MALLOC (decl))
    1159                 :        6122 :     l->malloc_state = STATE_MALLOC;
    1160                 :     4312074 :   else if (ipa && malloc_candidate_p (DECL_STRUCT_FUNCTION (decl), true))
    1161                 :       11050 :     l->malloc_state = STATE_MALLOC_TOP;
    1162                 :     4301024 :   else if (malloc_candidate_p (DECL_STRUCT_FUNCTION (decl), false))
    1163                 :       20351 :     l->malloc_state = STATE_MALLOC;
    1164                 :             : 
    1165                 :     4318196 :   pop_cfun ();
    1166                 :     4318196 :   if (dump_file)
    1167                 :             :     {
    1168                 :         201 :       if (l->looping)
    1169                 :          51 :         fprintf (dump_file, "Function is locally looping.\n");
    1170                 :         201 :       if (l->can_throw)
    1171                 :           8 :         fprintf (dump_file, "Function is locally throwing.\n");
    1172                 :         201 :       if (l->pure_const_state == IPA_CONST)
    1173                 :         108 :         fprintf (dump_file, "Function is locally const.\n");
    1174                 :         201 :       if (l->pure_const_state == IPA_PURE)
    1175                 :           8 :         fprintf (dump_file, "Function is locally pure.\n");
    1176                 :         201 :       if (l->can_free)
    1177                 :           4 :         fprintf (dump_file, "Function can locally free.\n");
    1178                 :         201 :       if (l->malloc_state == STATE_MALLOC)
    1179                 :           8 :         fprintf (dump_file, "Function is locally malloc.\n");
    1180                 :             :     }
    1181                 :             :   return l;
    1182                 :             : }
    1183                 :             : 
    1184                 :             : void
    1185                 :       18365 : funct_state_summary_t::insert (cgraph_node *node, funct_state_d *state)
    1186                 :             : {
    1187                 :             :   /* There are some shared nodes, in particular the initializers on
    1188                 :             :      static declarations.  We do not need to scan them more than once
    1189                 :             :      since all we would be interested in are the addressof
    1190                 :             :      operations.  */
    1191                 :       18365 :   if (opt_for_fn (node->decl, flag_ipa_pure_const))
    1192                 :             :     {
    1193                 :       18364 :       funct_state_d *a = analyze_function (node, true);
    1194                 :       18364 :       new (state) funct_state_d (*a);
    1195                 :       18364 :       free (a);
    1196                 :             :     }
    1197                 :             :   else
    1198                 :             :     /* Do not keep stale summaries.  */
    1199                 :           1 :     funct_state_summaries->remove (node);
    1200                 :       18365 : }
    1201                 :             : 
    1202                 :             : /* Called when new clone is inserted to callgraph late.  */
    1203                 :             : 
    1204                 :             : void
    1205                 :      995577 : funct_state_summary_t::duplicate (cgraph_node *, cgraph_node *dst,
    1206                 :             :                                   funct_state_d *src_data,
    1207                 :             :                                   funct_state_d *dst_data)
    1208                 :             : {
    1209                 :      995577 :   new (dst_data) funct_state_d (*src_data);
    1210                 :      995577 :   if (dst_data->malloc_state == STATE_MALLOC
    1211                 :      995577 :       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (dst->decl))))
    1212                 :          13 :     dst_data->malloc_state = STATE_MALLOC_BOTTOM;
    1213                 :      995577 : }
    1214                 :             : 
    1215                 :             : 
    1216                 :             : void
    1217                 :      154054 : pass_ipa_pure_const::
    1218                 :             : register_hooks (void)
    1219                 :             : {
    1220                 :      154054 :   if (init_p)
    1221                 :             :     return;
    1222                 :             : 
    1223                 :      154054 :   init_p = true;
    1224                 :             : 
    1225                 :      154054 :   funct_state_summaries = new funct_state_summary_t (symtab);
    1226                 :             : }
    1227                 :             : 
    1228                 :             : 
    1229                 :             : /* Analyze each function in the cgraph to see if it is locally PURE or
    1230                 :             :    CONST.  */
    1231                 :             : 
    1232                 :             : static void
    1233                 :      142004 : pure_const_generate_summary (void)
    1234                 :             : {
    1235                 :      142004 :   struct cgraph_node *node;
    1236                 :             : 
    1237                 :      142004 :   pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
    1238                 :      142004 :   pass->register_hooks ();
    1239                 :             : 
    1240                 :             :   /* Process all of the functions.
    1241                 :             : 
    1242                 :             :      We process AVAIL_INTERPOSABLE functions.  We cannot use the results
    1243                 :             :      by default, but the info can be used at LTO with -fwhole-program or
    1244                 :             :      when function got cloned and the clone is AVAILABLE.  */
    1245                 :             : 
    1246                 :     1451764 :   FOR_EACH_DEFINED_FUNCTION (node)
    1247                 :     1309760 :     if (opt_for_fn (node->decl, flag_ipa_pure_const))
    1248                 :             :       {
    1249                 :     1309618 :         funct_state_d *a = analyze_function (node, true);
    1250                 :     1309618 :         new (funct_state_summaries->get_create (node)) funct_state_d (*a);
    1251                 :     1309618 :         free (a);
    1252                 :             :       }
    1253                 :      142004 : }
    1254                 :             : 
    1255                 :             : 
    1256                 :             : /* Serialize the ipa info for lto.  */
    1257                 :             : 
    1258                 :             : static void
    1259                 :       18531 : pure_const_write_summary (void)
    1260                 :             : {
    1261                 :       18531 :   struct cgraph_node *node;
    1262                 :       18531 :   struct lto_simple_output_block *ob
    1263                 :       18531 :     = lto_create_simple_output_block (LTO_section_ipa_pure_const);
    1264                 :       18531 :   unsigned int count = 0;
    1265                 :       18531 :   lto_symtab_encoder_iterator lsei;
    1266                 :       18531 :   lto_symtab_encoder_t encoder;
    1267                 :             : 
    1268                 :       18531 :   encoder = lto_get_out_decl_state ()->symtab_node_encoder;
    1269                 :             : 
    1270                 :      215168 :   for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
    1271                 :       89150 :        lsei_next_function_in_partition (&lsei))
    1272                 :             :     {
    1273                 :       89150 :       node = lsei_cgraph_node (lsei);
    1274                 :       89150 :       if (node->definition && funct_state_summaries->exists (node))
    1275                 :       89006 :         count++;
    1276                 :             :     }
    1277                 :             : 
    1278                 :       18531 :   streamer_write_uhwi_stream (ob->main_stream, count);
    1279                 :             : 
    1280                 :             :   /* Process all of the functions.  */
    1281                 :      215168 :   for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
    1282                 :       89150 :        lsei_next_function_in_partition (&lsei))
    1283                 :             :     {
    1284                 :       89150 :       node = lsei_cgraph_node (lsei);
    1285                 :       89150 :       funct_state_d *fs = funct_state_summaries->get (node);
    1286                 :       89150 :       if (node->definition && fs != NULL)
    1287                 :             :         {
    1288                 :       89006 :           struct bitpack_d bp;
    1289                 :       89006 :           int node_ref;
    1290                 :       89006 :           lto_symtab_encoder_t encoder;
    1291                 :             : 
    1292                 :       89006 :           encoder = ob->decl_state->symtab_node_encoder;
    1293                 :       89006 :           node_ref = lto_symtab_encoder_encode (encoder, node);
    1294                 :       89006 :           streamer_write_uhwi_stream (ob->main_stream, node_ref);
    1295                 :             : 
    1296                 :             :           /* Note that flags will need to be read in the opposite
    1297                 :             :              order as we are pushing the bitflags into FLAGS.  */
    1298                 :       89006 :           bp = bitpack_create (ob->main_stream);
    1299                 :       89006 :           bp_pack_value (&bp, fs->pure_const_state, 2);
    1300                 :       89006 :           bp_pack_value (&bp, fs->state_previously_known, 2);
    1301                 :       89006 :           bp_pack_value (&bp, fs->looping_previously_known, 1);
    1302                 :       89006 :           bp_pack_value (&bp, fs->looping, 1);
    1303                 :       89006 :           bp_pack_value (&bp, fs->can_throw, 1);
    1304                 :       89006 :           bp_pack_value (&bp, fs->can_free, 1);
    1305                 :       89006 :           bp_pack_value (&bp, fs->malloc_state, 2);
    1306                 :       89006 :           streamer_write_bitpack (&bp);
    1307                 :             :         }
    1308                 :             :     }
    1309                 :             : 
    1310                 :       18531 :   lto_destroy_simple_output_block (ob);
    1311                 :       18531 : }
    1312                 :             : 
    1313                 :             : 
    1314                 :             : /* Deserialize the ipa info for lto.  */
    1315                 :             : 
    1316                 :             : static void
    1317                 :       12050 : pure_const_read_summary (void)
    1318                 :             : {
    1319                 :       12050 :   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
    1320                 :       12050 :   struct lto_file_decl_data *file_data;
    1321                 :       12050 :   unsigned int j = 0;
    1322                 :             : 
    1323                 :       12050 :   pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass);
    1324                 :       12050 :   pass->register_hooks ();
    1325                 :             : 
    1326                 :       37160 :   while ((file_data = file_data_vec[j++]))
    1327                 :             :     {
    1328                 :       13060 :       const char *data;
    1329                 :       13060 :       size_t len;
    1330                 :       13060 :       class lto_input_block *ib
    1331                 :       13060 :         = lto_create_simple_input_block (file_data,
    1332                 :             :                                          LTO_section_ipa_pure_const,
    1333                 :             :                                          &data, &len);
    1334                 :       13060 :       if (ib)
    1335                 :             :         {
    1336                 :       10041 :           unsigned int i;
    1337                 :       10041 :           unsigned int count = streamer_read_uhwi (ib);
    1338                 :             : 
    1339                 :       84267 :           for (i = 0; i < count; i++)
    1340                 :             :             {
    1341                 :       74226 :               unsigned int index;
    1342                 :       74226 :               struct cgraph_node *node;
    1343                 :       74226 :               struct bitpack_d bp;
    1344                 :       74226 :               funct_state fs;
    1345                 :       74226 :               lto_symtab_encoder_t encoder;
    1346                 :             : 
    1347                 :       74226 :               index = streamer_read_uhwi (ib);
    1348                 :       74226 :               encoder = file_data->symtab_node_encoder;
    1349                 :       74226 :               node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
    1350                 :             :                                                                         index));
    1351                 :             : 
    1352                 :       74226 :               fs = funct_state_summaries->get_create (node);
    1353                 :             :               /* Note that the flags must be read in the opposite
    1354                 :             :                  order in which they were written (the bitflags were
    1355                 :             :                  pushed into FLAGS).  */
    1356                 :       74226 :               bp = streamer_read_bitpack (ib);
    1357                 :       74226 :               fs->pure_const_state
    1358                 :       74226 :                         = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
    1359                 :       74226 :               fs->state_previously_known
    1360                 :       74226 :                         = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
    1361                 :       74226 :               fs->looping_previously_known = bp_unpack_value (&bp, 1);
    1362                 :       74226 :               fs->looping = bp_unpack_value (&bp, 1);
    1363                 :       74226 :               fs->can_throw = bp_unpack_value (&bp, 1);
    1364                 :       74226 :               fs->can_free = bp_unpack_value (&bp, 1);
    1365                 :       74226 :               fs->malloc_state
    1366                 :       74226 :                         = (enum malloc_state_e) bp_unpack_value (&bp, 2);
    1367                 :             : 
    1368                 :       74226 :               if (dump_file)
    1369                 :             :                 {
    1370                 :           0 :                   int flags = flags_from_decl_or_type (node->decl);
    1371                 :           0 :                   fprintf (dump_file, "Read info for %s ", node->dump_name ());
    1372                 :           0 :                   if (flags & ECF_CONST)
    1373                 :           0 :                     fprintf (dump_file, " const");
    1374                 :           0 :                   if (flags & ECF_PURE)
    1375                 :           0 :                     fprintf (dump_file, " pure");
    1376                 :           0 :                   if (flags & ECF_NOTHROW)
    1377                 :           0 :                     fprintf (dump_file, " nothrow");
    1378                 :           0 :                   fprintf (dump_file, "\n  pure const state: %s\n",
    1379                 :           0 :                            pure_const_names[fs->pure_const_state]);
    1380                 :           0 :                   fprintf (dump_file, "  previously known state: %s\n",
    1381                 :           0 :                            pure_const_names[fs->state_previously_known]);
    1382                 :           0 :                   if (fs->looping)
    1383                 :           0 :                     fprintf (dump_file,"  function is locally looping\n");
    1384                 :           0 :                   if (fs->looping_previously_known)
    1385                 :           0 :                     fprintf (dump_file,"  function is previously known looping\n");
    1386                 :           0 :                   if (fs->can_throw)
    1387                 :           0 :                     fprintf (dump_file,"  function is locally throwing\n");
    1388                 :           0 :                   if (fs->can_free)
    1389                 :           0 :                     fprintf (dump_file,"  function can locally free\n");
    1390                 :           0 :                   fprintf (dump_file, "\n malloc state: %s\n",
    1391                 :           0 :                            malloc_state_names[fs->malloc_state]);
    1392                 :             :                 }
    1393                 :             :             }
    1394                 :             : 
    1395                 :       10041 :           lto_destroy_simple_input_block (file_data,
    1396                 :             :                                           LTO_section_ipa_pure_const,
    1397                 :             :                                           ib, data, len);
    1398                 :             :         }
    1399                 :             :     }
    1400                 :       12050 : }
    1401                 :             : 
    1402                 :             : /* We only propagate across edges that can throw externally and their callee
    1403                 :             :    is not interposable.  */
    1404                 :             : 
    1405                 :             : static bool
    1406                 :     6472973 : ignore_edge_for_nothrow (struct cgraph_edge *e)
    1407                 :             : {
    1408                 :     6472973 :   if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
    1409                 :             :     return true;
    1410                 :             : 
    1411                 :     1936872 :   enum availability avail;
    1412                 :     1936872 :   cgraph_node *ultimate_target
    1413                 :     1936872 :     = e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
    1414                 :     1936872 :   if (avail <= AVAIL_INTERPOSABLE || TREE_NOTHROW (ultimate_target->decl))
    1415                 :             :     return true;
    1416                 :      637502 :   return ((opt_for_fn (e->callee->decl, flag_non_call_exceptions)
    1417                 :      201773 :            && !e->callee->binds_to_current_def_p (e->caller))
    1418                 :      637453 :           || !opt_for_fn (e->caller->decl, flag_ipa_pure_const)
    1419                 :     1273931 :           || !opt_for_fn (ultimate_target->decl, flag_ipa_pure_const));
    1420                 :             : }
    1421                 :             : 
    1422                 :             : /* Return true if NODE is self recursive function.
    1423                 :             :    Indirectly recursive functions appears as non-trivial strongly
    1424                 :             :    connected components, so we need to care about self recursion
    1425                 :             :    only.  */
    1426                 :             : 
    1427                 :             : static bool
    1428                 :     1832814 : self_recursive_p (struct cgraph_node *node)
    1429                 :             : {
    1430                 :     1832814 :   struct cgraph_edge *e;
    1431                 :     7062203 :   for (e = node->callees; e; e = e->next_callee)
    1432                 :     5232780 :     if (e->callee->function_symbol () == node)
    1433                 :             :       return true;
    1434                 :             :   return false;
    1435                 :             : }
    1436                 :             : 
    1437                 :             : /* Return true if N is cdtor that is not const or pure.  In this case we may
    1438                 :             :    need to remove unreachable function if it is marked const/pure.  */
    1439                 :             : 
    1440                 :             : static bool
    1441                 :       49074 : cdtor_p (cgraph_node *n, void *)
    1442                 :             : {
    1443                 :       49074 :   if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl))
    1444                 :           4 :     return ((!TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl))
    1445                 :           4 :             || DECL_LOOPING_CONST_OR_PURE_P (n->decl));
    1446                 :             :   return false;
    1447                 :             : }
    1448                 :             : 
    1449                 :             : /* Skip edges from and to nodes without ipa_pure_const enabled.
    1450                 :             :    Ignore not available symbols.  */
    1451                 :             : 
    1452                 :             : static bool
    1453                 :     6472973 : ignore_edge_for_pure_const (struct cgraph_edge *e)
    1454                 :             : {
    1455                 :     6472973 :   enum availability avail;
    1456                 :     6472973 :   cgraph_node *ultimate_target
    1457                 :     6472973 :     = e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
    1458                 :             : 
    1459                 :     6472973 :   return (avail <= AVAIL_INTERPOSABLE
    1460                 :     2238351 :           || !opt_for_fn (e->caller->decl, flag_ipa_pure_const)
    1461                 :     8702578 :           || !opt_for_fn (ultimate_target->decl,
    1462                 :     6472973 :                           flag_ipa_pure_const));
    1463                 :             : }
    1464                 :             : 
    1465                 :             : /* Return true if function should be skipped for local pure const analysis.  */
    1466                 :             : 
    1467                 :             : static bool
    1468                 :     4344399 : skip_function_for_local_pure_const (struct cgraph_node *node)
    1469                 :             : {
    1470                 :             :   /* Because we do not schedule pass_fixup_cfg over whole program after early
    1471                 :             :      optimizations we must not promote functions that are called by already
    1472                 :             :      processed functions.  */
    1473                 :             : 
    1474                 :     4344399 :   if (function_called_by_processed_nodes_p ())
    1475                 :             :     {
    1476                 :        4747 :       if (dump_file)
    1477                 :           1 :         fprintf (dump_file, "Function called in recursive cycle; ignoring\n");
    1478                 :        4747 :       return true;
    1479                 :             :     }
    1480                 :             :   /* Save some work and do not analyze functions which are interposable and
    1481                 :             :      do not have any non-interposable aliases.  */
    1482                 :     4339652 :   if (node->get_availability () <= AVAIL_INTERPOSABLE
    1483                 :      197001 :       && !flag_lto
    1484                 :     4530435 :       && !node->has_aliases_p ())
    1485                 :             :     {
    1486                 :      181095 :       if (dump_file)
    1487                 :           0 :         fprintf (dump_file,
    1488                 :             :                  "Function is interposable; not analyzing.\n");
    1489                 :      181095 :       return true;
    1490                 :             :     }
    1491                 :             :   return false;
    1492                 :             : }
    1493                 :             : 
    1494                 :             : /* Make function const and output warning.  If LOCAL is true,
    1495                 :             :    return true if anything changed.  Otherwise return true if
    1496                 :             :    we may have introduced removale ctors.  */
    1497                 :             : 
    1498                 :             : bool
    1499                 :     1458345 : ipa_make_function_const (struct cgraph_node *node, bool looping, bool local)
    1500                 :             : {
    1501                 :     1458345 :   bool cdtor = false;
    1502                 :             : 
    1503                 :     1458345 :   if (TREE_READONLY (node->decl)
    1504                 :     1458345 :       && (looping || !DECL_LOOPING_CONST_OR_PURE_P (node->decl)))
    1505                 :             :     return false;
    1506                 :      876341 :   warn_function_const (node->decl, !looping);
    1507                 :      876341 :   if (local && skip_function_for_local_pure_const (node))
    1508                 :             :     return false;
    1509                 :      857687 :   if (dump_file)
    1510                 :          58 :     fprintf (dump_file, "Function found to be %sconst: %s\n",
    1511                 :             :              looping ? "looping " : "",
    1512                 :             :              node->dump_name ());
    1513                 :      857687 :   if (!local && !looping)
    1514                 :       42786 :     cdtor = node->call_for_symbol_and_aliases (cdtor_p, NULL, true);
    1515                 :      857687 :   if (!dbg_cnt (ipa_attr))
    1516                 :             :     return false;
    1517                 :      857687 :   if (node->set_const_flag (true, looping))
    1518                 :             :     {
    1519                 :      480178 :       if (dump_file)
    1520                 :          58 :         fprintf (dump_file,
    1521                 :             :                  "Declaration updated to be %sconst: %s\n",
    1522                 :             :                  looping ? "looping " : "",
    1523                 :             :                  node->dump_name ());
    1524                 :      480178 :       if (local)
    1525                 :             :         return true;
    1526                 :        2591 :       return cdtor;
    1527                 :             :     }
    1528                 :             :   return false;
    1529                 :             : }
    1530                 :             : 
    1531                 :             : /* Make function const and output warning.  If LOCAL is true,
    1532                 :             :    return true if anything changed.  Otherwise return true if
    1533                 :             :    we may have introduced removale ctors.  */
    1534                 :             : 
    1535                 :             : bool
    1536                 :      937157 : ipa_make_function_pure (struct cgraph_node *node, bool looping, bool local)
    1537                 :             : {
    1538                 :      937157 :   bool cdtor = false;
    1539                 :             : 
    1540                 :      937157 :   if (TREE_READONLY (node->decl)
    1541                 :      937157 :       || (DECL_PURE_P (node->decl)
    1542                 :      618505 :           && (looping || !DECL_LOOPING_CONST_OR_PURE_P (node->decl))))
    1543                 :             :     return false;
    1544                 :      318320 :   warn_function_pure (node->decl, !looping);
    1545                 :      318320 :   if (local && skip_function_for_local_pure_const (node))
    1546                 :             :     return false;
    1547                 :      309618 :   if (dump_file)
    1548                 :           8 :     fprintf (dump_file, "Function found to be %spure: %s\n",
    1549                 :             :              looping ? "looping " : "",
    1550                 :             :              node->dump_name ());
    1551                 :      309618 :   if (!local && !looping)
    1552                 :        3150 :     cdtor = node->call_for_symbol_and_aliases (cdtor_p, NULL, true);
    1553                 :      309618 :   if (!dbg_cnt (ipa_attr))
    1554                 :             :     return false;
    1555                 :      309618 :   if (node->set_pure_flag (true, looping))
    1556                 :             :     {
    1557                 :      298579 :       if (dump_file)
    1558                 :           8 :         fprintf (dump_file,
    1559                 :             :                  "Declaration updated to be %spure: %s\n",
    1560                 :             :                  looping ? "looping " : "",
    1561                 :             :                  node->dump_name ());
    1562                 :      298579 :       if (local)
    1563                 :             :         return true;
    1564                 :        7336 :       return cdtor;
    1565                 :             :     }
    1566                 :             :   return false;
    1567                 :             : }
    1568                 :             : 
    1569                 :             : /* Produce transitive closure over the callgraph and compute pure/const
    1570                 :             :    attributes.  */
    1571                 :             : 
    1572                 :             : static bool
    1573                 :      145420 : propagate_pure_const (void)
    1574                 :             : {
    1575                 :      145420 :   struct cgraph_node *node;
    1576                 :      145420 :   struct cgraph_node *w;
    1577                 :      145420 :   struct cgraph_node **order =
    1578                 :      145420 :     XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
    1579                 :      145420 :   int order_pos;
    1580                 :      145420 :   int i;
    1581                 :      145420 :   struct ipa_dfs_info * w_info;
    1582                 :      145420 :   bool remove_p = false;
    1583                 :             : 
    1584                 :      145420 :   order_pos = ipa_reduced_postorder (order, true,
    1585                 :             :                                      ignore_edge_for_pure_const);
    1586                 :      145420 :   if (dump_file)
    1587                 :             :     {
    1588                 :          31 :       cgraph_node::dump_cgraph (dump_file);
    1589                 :          31 :       ipa_print_order (dump_file, "reduced", order, order_pos);
    1590                 :             :     }
    1591                 :             : 
    1592                 :             :   /* Propagate the local information through the call graph to produce
    1593                 :             :      the global information.  All the nodes within a cycle will have
    1594                 :             :      the same info so we collapse cycles first.  Then we can do the
    1595                 :             :      propagation in one pass from the leaves to the roots.  */
    1596                 :     2278070 :   for (i = 0; i < order_pos; i++ )
    1597                 :             :     {
    1598                 :     2132650 :       enum pure_const_state_e pure_const_state = IPA_CONST;
    1599                 :     2132650 :       bool looping = false;
    1600                 :     2132650 :       int count = 0;
    1601                 :     2132650 :       node = order[i];
    1602                 :             : 
    1603                 :     2132650 :       if (node->alias)
    1604                 :       37360 :         continue;
    1605                 :             : 
    1606                 :     2095290 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1607                 :           5 :         fprintf (dump_file, "Starting cycle\n");
    1608                 :             : 
    1609                 :             :       /* Find the worst state for any node in the cycle.  */
    1610                 :             :       w = node;
    1611                 :     3535869 :       while (w && pure_const_state != IPA_NEITHER)
    1612                 :             :         {
    1613                 :     2098163 :           struct cgraph_edge *e;
    1614                 :     2098163 :           struct cgraph_edge *ie;
    1615                 :     2098163 :           int i;
    1616                 :     2098163 :           struct ipa_ref *ref = NULL;
    1617                 :             : 
    1618                 :     2098163 :           funct_state w_l = funct_state_summaries->get_create (w);
    1619                 :     2098163 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1620                 :           6 :             fprintf (dump_file, "  Visiting %s state:%s looping %i\n",
    1621                 :             :                      w->dump_name (),
    1622                 :           6 :                      pure_const_names[w_l->pure_const_state],
    1623                 :           6 :                      w_l->looping);
    1624                 :             : 
    1625                 :             :           /* First merge in function body properties.
    1626                 :             :              We are safe to pass NULL as FROM and TO because we will take care
    1627                 :             :              of possible interposition when walking callees.  */
    1628                 :     2098163 :           worse_state (&pure_const_state, &looping,
    1629                 :     2098163 :                        w_l->pure_const_state, w_l->looping,
    1630                 :             :                        NULL, NULL);
    1631                 :     2098163 :           if (pure_const_state == IPA_NEITHER)
    1632                 :             :             break;
    1633                 :             : 
    1634                 :     1440579 :           count++;
    1635                 :             : 
    1636                 :             :           /* We consider recursive cycles as possibly infinite.
    1637                 :             :              This might be relaxed since infinite recursion leads to stack
    1638                 :             :              overflow.  */
    1639                 :     1440579 :           if (count > 1)
    1640                 :        2873 :             looping = true;
    1641                 :             : 
    1642                 :             :           /* Now walk the edges and merge in callee properties.  */
    1643                 :     2155707 :           for (e = w->callees; e && pure_const_state != IPA_NEITHER;
    1644                 :      715128 :                e = e->next_callee)
    1645                 :             :             {
    1646                 :     1675840 :               enum availability avail;
    1647                 :     1675840 :               struct cgraph_node *y = e->callee->
    1648                 :     3351680 :                                 function_or_virtual_thunk_symbol (&avail,
    1649                 :     1675840 :                                                                   e->caller);
    1650                 :     1675840 :               enum pure_const_state_e edge_state = IPA_CONST;
    1651                 :     1675840 :               bool edge_looping = false;
    1652                 :             : 
    1653                 :     1675840 :               if (e->recursive_p ())
    1654                 :        5914 :                 looping = true;
    1655                 :             : 
    1656                 :     1675840 :               if (dump_file && (dump_flags & TDF_DETAILS))
    1657                 :             :                 {
    1658                 :           7 :                   fprintf (dump_file, "    Call to %s",
    1659                 :           7 :                            e->callee->dump_name ());
    1660                 :             :                 }
    1661                 :     1675840 :               if (avail > AVAIL_INTERPOSABLE)
    1662                 :             :                 {
    1663                 :      571570 :                   funct_state y_l = funct_state_summaries->get_create (y);
    1664                 :             : 
    1665                 :      571570 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1666                 :             :                     {
    1667                 :           2 :                       fprintf (dump_file,
    1668                 :             :                                " state:%s looping:%i\n",
    1669                 :           2 :                                pure_const_names[y_l->pure_const_state],
    1670                 :           2 :                                y_l->looping);
    1671                 :             :                     }
    1672                 :      571570 :                   if (y_l->pure_const_state > IPA_PURE
    1673                 :      571570 :                       && e->cannot_lead_to_return_p ())
    1674                 :             :                     {
    1675                 :        8093 :                       if (dump_file && (dump_flags & TDF_DETAILS))
    1676                 :           0 :                         fprintf (dump_file,
    1677                 :             :                                  "        Ignoring side effects"
    1678                 :             :                                  " -> pure, looping\n");
    1679                 :        8093 :                       edge_state = IPA_PURE;
    1680                 :        8093 :                       edge_looping = true;
    1681                 :             :                     }
    1682                 :             :                   else
    1683                 :             :                     {
    1684                 :      563477 :                       edge_state = y_l->pure_const_state;
    1685                 :      563477 :                       edge_looping = y_l->looping;
    1686                 :             :                     }
    1687                 :             :                 }
    1688                 :     1104270 :               else if (builtin_safe_for_const_function_p (&edge_looping,
    1689                 :             :                                                            y->decl))
    1690                 :             :                 edge_state = IPA_CONST;
    1691                 :             :               else
    1692                 :      996120 :                 state_from_flags (&edge_state, &edge_looping,
    1693                 :      996120 :                                   flags_from_decl_or_type (y->decl),
    1694                 :      996120 :                                   e->cannot_lead_to_return_p ());
    1695                 :             : 
    1696                 :             :               /* Merge the results with what we already know.  */
    1697                 :     1675840 :               better_state (&edge_state, &edge_looping,
    1698                 :             :                             w_l->state_previously_known,
    1699                 :     1675840 :                             w_l->looping_previously_known);
    1700                 :     1675840 :               worse_state (&pure_const_state, &looping,
    1701                 :     1675840 :                            edge_state, edge_looping, e->caller, e->callee);
    1702                 :     1675840 :               if (pure_const_state == IPA_NEITHER)
    1703                 :             :                 break;
    1704                 :             :             }
    1705                 :             : 
    1706                 :             :           /* Now process the indirect call.  */
    1707                 :     1440579 :           for (ie = w->indirect_calls;
    1708                 :     1441226 :                ie && pure_const_state != IPA_NEITHER; ie = ie->next_callee)
    1709                 :             :             {
    1710                 :       25366 :               enum pure_const_state_e edge_state = IPA_CONST;
    1711                 :       25366 :               bool edge_looping = false;
    1712                 :             : 
    1713                 :       25366 :               if (dump_file && (dump_flags & TDF_DETAILS))
    1714                 :           0 :                 fprintf (dump_file, "    Indirect call");
    1715                 :       50732 :               state_from_flags (&edge_state, &edge_looping,
    1716                 :       25366 :                                 ie->indirect_info->ecf_flags,
    1717                 :       25366 :                                 ie->cannot_lead_to_return_p ());
    1718                 :             :               /* Merge the results with what we already know.  */
    1719                 :       25366 :               better_state (&edge_state, &edge_looping,
    1720                 :             :                             w_l->state_previously_known,
    1721                 :       25366 :                             w_l->looping_previously_known);
    1722                 :       25366 :               worse_state (&pure_const_state, &looping,
    1723                 :             :                            edge_state, edge_looping, NULL, NULL);
    1724                 :       25366 :               if (pure_const_state == IPA_NEITHER)
    1725                 :             :                 break;
    1726                 :             :             }
    1727                 :             : 
    1728                 :             :           /* And finally all loads and stores.  */
    1729                 :      307915 :           for (i = 0; w->iterate_reference (i, ref)
    1730                 :     2363064 :                && pure_const_state != IPA_NEITHER; i++)
    1731                 :             :             {
    1732                 :      333811 :               enum pure_const_state_e ref_state = IPA_CONST;
    1733                 :      333811 :               bool ref_looping = false;
    1734                 :      333811 :               switch (ref->use)
    1735                 :             :                 {
    1736                 :      211376 :                 case IPA_REF_LOAD:
    1737                 :             :                   /* readonly reads are safe.  */
    1738                 :      211376 :                   if (TREE_READONLY (ref->referred->decl))
    1739                 :             :                     break;
    1740                 :      198662 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1741                 :           0 :                     fprintf (dump_file, "    nonreadonly global var read\n");
    1742                 :      198662 :                   ref_state = IPA_PURE;
    1743                 :      198662 :                   break;
    1744                 :       87582 :                 case IPA_REF_STORE:
    1745                 :       87582 :                   if (ref->cannot_lead_to_return ())
    1746                 :             :                     break;
    1747                 :       25955 :                   ref_state = IPA_NEITHER;
    1748                 :       25955 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1749                 :           0 :                     fprintf (dump_file, "    global var write\n");
    1750                 :             :                   break;
    1751                 :             :                 case IPA_REF_ADDR:
    1752                 :             :                   break;
    1753                 :           0 :                 default:
    1754                 :           0 :                   gcc_unreachable ();
    1755                 :             :                 }
    1756                 :      333811 :               better_state (&ref_state, &ref_looping,
    1757                 :             :                             w_l->state_previously_known,
    1758                 :      333811 :                             w_l->looping_previously_known);
    1759                 :      333811 :               worse_state (&pure_const_state, &looping,
    1760                 :             :                            ref_state, ref_looping, NULL, NULL);
    1761                 :      333811 :               if (pure_const_state == IPA_NEITHER)
    1762                 :             :                 break;
    1763                 :             :             }
    1764                 :     1440579 :           w_info = (struct ipa_dfs_info *) w->aux;
    1765                 :     1440579 :           w = w_info->next_cycle;
    1766                 :             :         }
    1767                 :     2095290 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1768                 :           5 :         fprintf (dump_file, "Result %s looping %i\n",
    1769                 :           5 :                  pure_const_names [pure_const_state],
    1770                 :             :                  looping);
    1771                 :             : 
    1772                 :             :       /* Find the worst state of can_free for any node in the cycle.  */
    1773                 :             :       bool can_free = false;
    1774                 :             :       w = node;
    1775                 :     4193958 :       while (w && !can_free)
    1776                 :             :         {
    1777                 :     2098668 :           struct cgraph_edge *e;
    1778                 :     2098668 :           funct_state w_l = funct_state_summaries->get (w);
    1779                 :             : 
    1780                 :     2098668 :           if (w_l->can_free
    1781                 :     1923962 :               || w->get_availability () == AVAIL_INTERPOSABLE
    1782                 :     3948273 :               || w->indirect_calls)
    1783                 :             :             can_free = true;
    1784                 :             : 
    1785                 :     3618490 :           for (e = w->callees; e && !can_free; e = e->next_callee)
    1786                 :             :             {
    1787                 :     1519822 :               enum availability avail;
    1788                 :     1519822 :               struct cgraph_node *y = e->callee->
    1789                 :     3039644 :                                 function_or_virtual_thunk_symbol (&avail,
    1790                 :     1519822 :                                                                   e->caller);
    1791                 :             : 
    1792                 :     1519822 :               if (avail > AVAIL_INTERPOSABLE)
    1793                 :      703675 :                 can_free = funct_state_summaries->get (y)->can_free;
    1794                 :             :               else
    1795                 :             :                 can_free = true;
    1796                 :             :             }
    1797                 :     2098668 :           w_info = (struct ipa_dfs_info *) w->aux;
    1798                 :     2098668 :           w = w_info->next_cycle;
    1799                 :             :         }
    1800                 :             : 
    1801                 :             :       /* Copy back the region's pure_const_state which is shared by
    1802                 :             :          all nodes in the region.  */
    1803                 :             :       w = node;
    1804                 :     4211511 :       while (w)
    1805                 :             :         {
    1806                 :     2116221 :           funct_state w_l = funct_state_summaries->get (w);
    1807                 :     2116221 :           enum pure_const_state_e this_state = pure_const_state;
    1808                 :     2116221 :           bool this_looping = looping;
    1809                 :             : 
    1810                 :     2116221 :           w_l->can_free = can_free;
    1811                 :     2116221 :           w->nonfreeing_fn = !can_free;
    1812                 :     2116221 :           if (!can_free && dump_file)
    1813                 :          28 :             fprintf (dump_file, "Function found not to call free: %s\n",
    1814                 :             :                      w->dump_name ());
    1815                 :             : 
    1816                 :     2116221 :           if (w_l->state_previously_known != IPA_NEITHER
    1817                 :      343454 :               && this_state > w_l->state_previously_known)
    1818                 :             :             {
    1819                 :         920 :               if (this_state == IPA_NEITHER)
    1820                 :          50 :                 this_looping = w_l->looping_previously_known;
    1821                 :             :               this_state = w_l->state_previously_known;
    1822                 :             :             }
    1823                 :     2116221 :           if (!this_looping && self_recursive_p (w))
    1824                 :             :             this_looping = true;
    1825                 :     2116221 :           if (!w_l->looping_previously_known)
    1826                 :      261265 :             this_looping = false;
    1827                 :             : 
    1828                 :             :           /* All nodes within a cycle share the same info.  */
    1829                 :     2116221 :           w_l->pure_const_state = this_state;
    1830                 :     2116221 :           w_l->looping = this_looping;
    1831                 :             : 
    1832                 :             :           /* Inline clones share declaration with their offline copies;
    1833                 :             :              do not modify their declarations since the offline copy may
    1834                 :             :              be different.  */
    1835                 :     2116221 :           if (!w->inlined_to)
    1836                 :      991405 :             switch (this_state)
    1837                 :             :               {
    1838                 :      149103 :               case IPA_CONST:
    1839                 :      149103 :                 remove_p |= ipa_make_function_const (w, this_looping, false);
    1840                 :      149103 :                 break;
    1841                 :             : 
    1842                 :       94657 :               case IPA_PURE:
    1843                 :       94657 :                 remove_p |= ipa_make_function_pure (w, this_looping, false);
    1844                 :       94657 :                 break;
    1845                 :             : 
    1846                 :             :               default:
    1847                 :             :                 break;
    1848                 :             :               }
    1849                 :     2116221 :           w_info = (struct ipa_dfs_info *) w->aux;
    1850                 :     2116221 :           w = w_info->next_cycle;
    1851                 :             :         }
    1852                 :             :     }
    1853                 :             : 
    1854                 :      145420 :   ipa_free_postorder_info ();
    1855                 :      145420 :   free (order);
    1856                 :      145420 :   return remove_p;
    1857                 :             : }
    1858                 :             : 
    1859                 :             : /* Produce transitive closure over the callgraph and compute nothrow
    1860                 :             :    attributes.  */
    1861                 :             : 
    1862                 :             : static void
    1863                 :      145420 : propagate_nothrow (void)
    1864                 :             : {
    1865                 :      145420 :   struct cgraph_node *node;
    1866                 :      145420 :   struct cgraph_node *w;
    1867                 :      145420 :   struct cgraph_node **order =
    1868                 :      145420 :     XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
    1869                 :      145420 :   int order_pos;
    1870                 :      145420 :   int i;
    1871                 :      145420 :   struct ipa_dfs_info * w_info;
    1872                 :             : 
    1873                 :      145420 :   order_pos = ipa_reduced_postorder (order, true,
    1874                 :             :                                      ignore_edge_for_nothrow);
    1875                 :      145420 :   if (dump_file)
    1876                 :             :     {
    1877                 :          31 :       cgraph_node::dump_cgraph (dump_file);
    1878                 :          31 :       ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
    1879                 :             :     }
    1880                 :             : 
    1881                 :             :   /* Propagate the local information through the call graph to produce
    1882                 :             :      the global information.  All the nodes within a cycle will have
    1883                 :             :      the same info so we collapse cycles first.  Then we can do the
    1884                 :             :      propagation in one pass from the leaves to the roots.  */
    1885                 :     2294025 :   for (i = 0; i < order_pos; i++ )
    1886                 :             :     {
    1887                 :     2148605 :       bool can_throw = false;
    1888                 :     2148605 :       node = order[i];
    1889                 :             : 
    1890                 :     2148605 :       if (node->alias)
    1891                 :       37360 :         continue;
    1892                 :             : 
    1893                 :             :       /* Find the worst state for any node in the cycle.  */
    1894                 :             :       w = node;
    1895                 :     4222646 :       while (w && !can_throw)
    1896                 :             :         {
    1897                 :     2111401 :           struct cgraph_edge *e, *ie;
    1898                 :             : 
    1899                 :     2111401 :           if (!TREE_NOTHROW (w->decl))
    1900                 :             :             {
    1901                 :      848047 :               funct_state w_l = funct_state_summaries->get_create (w);
    1902                 :             : 
    1903                 :      848047 :               if (w_l->can_throw
    1904                 :      848047 :                   || w->get_availability () == AVAIL_INTERPOSABLE)
    1905                 :             :                 can_throw = true;
    1906                 :             : 
    1907                 :     1539926 :               for (e = w->callees; e && !can_throw; e = e->next_callee)
    1908                 :             :                 {
    1909                 :      691879 :                   enum availability avail;
    1910                 :             : 
    1911                 :      691879 :                   if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl))
    1912                 :      161734 :                     continue;
    1913                 :             : 
    1914                 :      530145 :                   struct cgraph_node *y = e->callee->
    1915                 :     1060290 :                                    function_or_virtual_thunk_symbol (&avail,
    1916                 :      530145 :                                                                      e->caller);
    1917                 :             : 
    1918                 :             :                   /* We can use info about the callee only if we know it
    1919                 :             :                      cannot be interposed.
    1920                 :             :                      When callee is compiled with non-call exceptions we also
    1921                 :             :                      must check that the declaration is bound to current
    1922                 :             :                      body as other semantically equivalent body may still
    1923                 :             :                      throw.  */
    1924                 :      530145 :                   if (avail <= AVAIL_INTERPOSABLE
    1925                 :      530145 :                       || (!TREE_NOTHROW (y->decl)
    1926                 :      253464 :                           && (funct_state_summaries->get_create (y)->can_throw
    1927                 :        2109 :                               || (opt_for_fn (y->decl, flag_non_call_exceptions)
    1928                 :         486 :                                   && !e->callee->binds_to_current_def_p (w)))))
    1929                 :             :                     can_throw = true;
    1930                 :             :                 }
    1931                 :      861361 :               for (ie = w->indirect_calls; ie && !can_throw;
    1932                 :       13314 :                    ie = ie->next_callee)
    1933                 :       13314 :                 if (ie->can_throw_external
    1934                 :       11888 :                     && !(ie->indirect_info->ecf_flags & ECF_NOTHROW))
    1935                 :       13314 :                   can_throw = true;
    1936                 :             :             }
    1937                 :     2111401 :           w_info = (struct ipa_dfs_info *) w->aux;
    1938                 :     2111401 :           w = w_info->next_cycle;
    1939                 :             :         }
    1940                 :             : 
    1941                 :             :       /* Copy back the region's pure_const_state which is shared by
    1942                 :             :          all nodes in the region.  */
    1943                 :             :       w = node;
    1944                 :     4227466 :       while (w)
    1945                 :             :         {
    1946                 :     2116221 :           funct_state w_l = funct_state_summaries->get_create (w);
    1947                 :     2116221 :           if (!can_throw && !TREE_NOTHROW (w->decl))
    1948                 :             :             {
    1949                 :             :               /* Inline clones share declaration with their offline copies;
    1950                 :             :                  do not modify their declarations since the offline copy may
    1951                 :             :                  be different.  */
    1952                 :       12504 :               if (!w->inlined_to)
    1953                 :             :                 {
    1954                 :        2987 :                   w->set_nothrow_flag (true);
    1955                 :        2987 :                   if (dump_file)
    1956                 :           0 :                     fprintf (dump_file, "Function found to be nothrow: %s\n",
    1957                 :             :                              w->dump_name ());
    1958                 :             :                 }
    1959                 :             :             }
    1960                 :      840327 :           else if (can_throw && !TREE_NOTHROW (w->decl))
    1961                 :      840327 :             w_l->can_throw = true;
    1962                 :     2116221 :           w_info = (struct ipa_dfs_info *) w->aux;
    1963                 :     2116221 :           w = w_info->next_cycle;
    1964                 :             :         }
    1965                 :             :     }
    1966                 :             : 
    1967                 :      145420 :   ipa_free_postorder_info ();
    1968                 :      145420 :   free (order);
    1969                 :      145420 : }
    1970                 :             : 
    1971                 :             : /* Debugging function to dump state of malloc lattice.  */
    1972                 :             : 
    1973                 :             : DEBUG_FUNCTION
    1974                 :             : static void
    1975                 :      290840 : dump_malloc_lattice (FILE *dump_file, const char *s)
    1976                 :             : {
    1977                 :      290840 :   if (!dump_file)
    1978                 :             :     return;
    1979                 :             : 
    1980                 :          62 :   fprintf (dump_file, "\n\nMALLOC LATTICE %s:\n", s);
    1981                 :          62 :   cgraph_node *node;
    1982                 :         644 :   FOR_EACH_FUNCTION (node)
    1983                 :             :     {
    1984                 :         260 :       funct_state fs = funct_state_summaries->get (node);
    1985                 :         260 :       if (fs)
    1986                 :         170 :         fprintf (dump_file, "%s: %s\n", node->dump_name (),
    1987                 :         170 :                  malloc_state_names[fs->malloc_state]);
    1988                 :             :     }
    1989                 :             : }
    1990                 :             : 
    1991                 :             : /* Propagate malloc attribute across the callgraph.  */
    1992                 :             : 
    1993                 :             : static void
    1994                 :      145420 : propagate_malloc (void)
    1995                 :             : {
    1996                 :      145420 :   cgraph_node *node;
    1997                 :     7340812 :   FOR_EACH_FUNCTION (node)
    1998                 :             :     {
    1999                 :     3524986 :       if (DECL_IS_MALLOC (node->decl))
    2000                 :       41492 :         if (!funct_state_summaries->exists (node))
    2001                 :             :           {
    2002                 :       20651 :             funct_state fs = funct_state_summaries->get_create (node);
    2003                 :       20651 :             fs->malloc_state = STATE_MALLOC;
    2004                 :             :           }
    2005                 :             :     }
    2006                 :             : 
    2007                 :      145420 :   dump_malloc_lattice (dump_file, "Initial");
    2008                 :      145420 :   struct cgraph_node **order
    2009                 :      145420 :     = XNEWVEC (struct cgraph_node *, symtab->cgraph_count);
    2010                 :      145420 :   int order_pos = ipa_reverse_postorder (order);
    2011                 :      145420 :   bool changed = true;
    2012                 :             : 
    2013                 :      438190 :   while (changed)
    2014                 :             :     {
    2015                 :      147350 :       changed = false;
    2016                 :             :       /* Walk in postorder.  */
    2017                 :     4529170 :       for (int i = order_pos - 1; i >= 0; --i)
    2018                 :             :         {
    2019                 :     4381820 :           cgraph_node *node = order[i];
    2020                 :     5963203 :           if (node->alias
    2021                 :     4381820 :               || !node->definition
    2022                 :     4381820 :               || !funct_state_summaries->exists (node))
    2023                 :     4323372 :             continue;
    2024                 :             : 
    2025                 :     2800437 :           funct_state l = funct_state_summaries->get (node);
    2026                 :             : 
    2027                 :             :           /* FIXME: add support for indirect-calls.  */
    2028                 :     2800437 :           if (node->indirect_calls)
    2029                 :             :             {
    2030                 :      151367 :               l->malloc_state = STATE_MALLOC_BOTTOM;
    2031                 :      151367 :               continue;
    2032                 :             :             }
    2033                 :             : 
    2034                 :     2649070 :           if (node->get_availability () <= AVAIL_INTERPOSABLE)
    2035                 :             :             {
    2036                 :       88692 :               l->malloc_state = STATE_MALLOC_BOTTOM;
    2037                 :       88692 :               continue;
    2038                 :             :             }
    2039                 :             : 
    2040                 :     2560378 :           if (l->malloc_state == STATE_MALLOC_BOTTOM)
    2041                 :     2501930 :             continue;
    2042                 :             : 
    2043                 :       58448 :           auto_vec<cgraph_node *, 16> callees;
    2044                 :      221134 :           for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
    2045                 :             :             {
    2046                 :      162686 :               ipa_call_summary *es = ipa_call_summaries->get_create (cs);
    2047                 :      162686 :               if (es && es->is_return_callee_uncaptured)
    2048                 :       12216 :                 callees.safe_push (cs->callee);
    2049                 :             :             }
    2050                 :             : 
    2051                 :       58448 :           malloc_state_e new_state = l->malloc_state;
    2052                 :      141328 :           for (unsigned j = 0; j < callees.length (); j++)
    2053                 :             :             {
    2054                 :       12216 :               cgraph_node *callee = callees[j];
    2055                 :       12216 :               if (!funct_state_summaries->exists (node))
    2056                 :             :                 {
    2057                 :             :                   new_state = STATE_MALLOC_BOTTOM;
    2058                 :             :                   break;
    2059                 :             :                 }
    2060                 :       12216 :               malloc_state_e callee_state
    2061                 :       12216 :                 = funct_state_summaries->get_create (callee)->malloc_state;
    2062                 :       12216 :               if (new_state < callee_state)
    2063                 :       10351 :                 new_state = callee_state;
    2064                 :             :             }
    2065                 :       58448 :           if (new_state != l->malloc_state)
    2066                 :             :             {
    2067                 :       10333 :               changed = true;
    2068                 :       10333 :               l->malloc_state = new_state;
    2069                 :             :             }
    2070                 :       58448 :         }
    2071                 :             :     }
    2072                 :             : 
    2073                 :     2299005 :   FOR_EACH_DEFINED_FUNCTION (node)
    2074                 :     2153585 :     if (funct_state_summaries->exists (node))
    2075                 :             :       {
    2076                 :     2141231 :         funct_state l = funct_state_summaries->get (node);
    2077                 :     2141231 :         if (!node->alias
    2078                 :     2116221 :             && l->malloc_state == STATE_MALLOC
    2079                 :       15846 :             && !node->inlined_to
    2080                 :     2141623 :             && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (node->decl))))
    2081                 :             :           {
    2082                 :         392 :             if (dump_file && (dump_flags & TDF_DETAILS))
    2083                 :           6 :               fprintf (dump_file, "Function %s found to be malloc\n",
    2084                 :             :                        node->dump_name ());
    2085                 :             : 
    2086                 :         392 :             bool malloc_decl_p = DECL_IS_MALLOC (node->decl);
    2087                 :         392 :             node->set_malloc_flag (true);
    2088                 :         392 :             if (!malloc_decl_p && warn_suggest_attribute_malloc)
    2089                 :           0 :                 warn_function_malloc (node->decl);
    2090                 :             :           }
    2091                 :             :       }
    2092                 :             : 
    2093                 :      145420 :   dump_malloc_lattice (dump_file, "after propagation");
    2094                 :      145420 :   ipa_free_postorder_info ();
    2095                 :      145420 :   free (order);
    2096                 :      145420 : }
    2097                 :             : 
    2098                 :             : /* Produce the global information by preforming a transitive closure
    2099                 :             :    on the local information that was produced by generate_summary.  */
    2100                 :             : 
    2101                 :             : unsigned int
    2102                 :      145420 : pass_ipa_pure_const::
    2103                 :             : execute (function *)
    2104                 :             : {
    2105                 :      145420 :   bool remove_p;
    2106                 :             : 
    2107                 :             :   /* Nothrow makes more function to not lead to return and improve
    2108                 :             :      later analysis.  */
    2109                 :      145420 :   propagate_nothrow ();
    2110                 :      145420 :   propagate_malloc ();
    2111                 :      145420 :   remove_p = propagate_pure_const ();
    2112                 :             : 
    2113                 :      145420 :   delete funct_state_summaries;
    2114                 :      145420 :   return remove_p ? TODO_remove_functions : 0;
    2115                 :             : }
    2116                 :             : 
    2117                 :             : static bool
    2118                 :     3790299 : gate_pure_const (void)
    2119                 :             : {
    2120                 :      579718 :   return flag_ipa_pure_const || in_lto_p;
    2121                 :             : }
    2122                 :             : 
    2123                 :      281914 : pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt)
    2124                 :             :     : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt,
    2125                 :             :                      pure_const_generate_summary, /* generate_summary */
    2126                 :             :                      pure_const_write_summary, /* write_summary */
    2127                 :             :                      pure_const_read_summary, /* read_summary */
    2128                 :             :                      NULL, /* write_optimization_summary */
    2129                 :             :                      NULL, /* read_optimization_summary */
    2130                 :             :                      NULL, /* stmt_fixup */
    2131                 :             :                      0, /* function_transform_todo_flags_start */
    2132                 :             :                      NULL, /* function_transform */
    2133                 :             :                      NULL), /* variable_transform */
    2134                 :      281914 :   init_p (false) {}
    2135                 :             : 
    2136                 :             : ipa_opt_pass_d *
    2137                 :      281914 : make_pass_ipa_pure_const (gcc::context *ctxt)
    2138                 :             : {
    2139                 :      281914 :   return new pass_ipa_pure_const (ctxt);
    2140                 :             : }
    2141                 :             : 
    2142                 :             : /* Simple local pass for pure const discovery reusing the analysis from
    2143                 :             :    ipa_pure_const.   This pass is effective when executed together with
    2144                 :             :    other optimization passes in early optimization pass queue.  */
    2145                 :             : 
    2146                 :             : namespace {
    2147                 :             : 
    2148                 :             : const pass_data pass_data_local_pure_const =
    2149                 :             : {
    2150                 :             :   GIMPLE_PASS, /* type */
    2151                 :             :   "local-pure-const", /* name */
    2152                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    2153                 :             :   TV_IPA_PURE_CONST, /* tv_id */
    2154                 :             :   0, /* properties_required */
    2155                 :             :   0, /* properties_provided */
    2156                 :             :   0, /* properties_destroyed */
    2157                 :             :   0, /* todo_flags_start */
    2158                 :             :   0, /* todo_flags_finish */
    2159                 :             : };
    2160                 :             : 
    2161                 :             : class pass_local_pure_const : public gimple_opt_pass
    2162                 :             : {
    2163                 :             : public:
    2164                 :      563828 :   pass_local_pure_const (gcc::context *ctxt)
    2165                 :     1127656 :     : gimple_opt_pass (pass_data_local_pure_const, ctxt)
    2166                 :             :   {}
    2167                 :             : 
    2168                 :             :   /* opt_pass methods: */
    2169                 :      281914 :   opt_pass * clone () final override
    2170                 :             :   {
    2171                 :      281914 :     return new pass_local_pure_const (m_ctxt);
    2172                 :             :   }
    2173                 :     3210836 :   bool gate (function *) final override { return gate_pure_const (); }
    2174                 :             :   unsigned int execute (function *) final override;
    2175                 :             : 
    2176                 :             : }; // class pass_local_pure_const
    2177                 :             : 
    2178                 :             : unsigned int
    2179                 :     3210531 : pass_local_pure_const::execute (function *fun)
    2180                 :             : {
    2181                 :     3210531 :   bool changed = false;
    2182                 :     3210531 :   funct_state l;
    2183                 :     3210531 :   bool skip;
    2184                 :     3210531 :   struct cgraph_node *node;
    2185                 :             : 
    2186                 :     3210531 :   node = cgraph_node::get (current_function_decl);
    2187                 :     3210531 :   skip = skip_function_for_local_pure_const (node);
    2188                 :             : 
    2189                 :     3210531 :   if (!warn_suggest_attribute_const
    2190                 :     3210508 :       && !warn_suggest_attribute_pure
    2191                 :     3210487 :       && skip)
    2192                 :             :     return 0;
    2193                 :             : 
    2194                 :     3052045 :   l = analyze_function (node, false);
    2195                 :             : 
    2196                 :             :   /* Do NORETURN discovery.  */
    2197                 :     3052045 :   if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
    2198                 :     6078630 :       && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
    2199                 :             :     {
    2200                 :       29077 :       warn_function_noreturn (fun->decl);
    2201                 :       29077 :       if (dump_file)
    2202                 :           1 :         fprintf (dump_file, "Function found to be noreturn: %s\n",
    2203                 :             :                  current_function_name ());
    2204                 :             : 
    2205                 :             :       /* Update declaration and reduce profile to executed once.  */
    2206                 :       29077 :       if (cgraph_node::get (current_function_decl)->set_noreturn_flag (true))
    2207                 :             :         changed = true;
    2208                 :       29077 :       if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE)
    2209                 :       11310 :         node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
    2210                 :             :     }
    2211                 :             : 
    2212                 :     3052045 :   switch (l->pure_const_state)
    2213                 :             :     {
    2214                 :      613833 :     case IPA_CONST:
    2215                 :     1227666 :       changed |= ipa_make_function_const
    2216                 :      613833 :                    (cgraph_node::get (current_function_decl), l->looping, true);
    2217                 :      613833 :       break;
    2218                 :             : 
    2219                 :      372199 :     case IPA_PURE:
    2220                 :      744398 :       changed |= ipa_make_function_pure
    2221                 :      372199 :                    (cgraph_node::get (current_function_decl), l->looping, true);
    2222                 :      372199 :       break;
    2223                 :             : 
    2224                 :             :     default:
    2225                 :             :       break;
    2226                 :             :     }
    2227                 :     3052045 :   if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
    2228                 :             :     {
    2229                 :       21158 :       node->set_nothrow_flag (true);
    2230                 :       21158 :       changed = true;
    2231                 :       21158 :       if (dump_file)
    2232                 :           2 :         fprintf (dump_file, "Function found to be nothrow: %s\n",
    2233                 :             :                  current_function_name ());
    2234                 :             :     }
    2235                 :             : 
    2236                 :     3052045 :   if (l->malloc_state == STATE_MALLOC
    2237                 :     3052045 :       && !DECL_IS_MALLOC (current_function_decl))
    2238                 :             :     {
    2239                 :       20351 :       node->set_malloc_flag (true);
    2240                 :       20351 :       if (warn_suggest_attribute_malloc)
    2241                 :           3 :         warn_function_malloc (node->decl);
    2242                 :       20351 :       changed = true;
    2243                 :       20351 :       if (dump_file)
    2244                 :           2 :         fprintf (dump_file, "Function found to be malloc: %s\n",
    2245                 :             :                  node->dump_name ());
    2246                 :             :     }
    2247                 :             : 
    2248                 :     3052045 :   free (l);
    2249                 :     3052045 :   if (changed)
    2250                 :      815578 :     return execute_fixup_cfg ();
    2251                 :             :   else
    2252                 :             :     return 0;
    2253                 :             : }
    2254                 :             : 
    2255                 :             : } // anon namespace
    2256                 :             : 
    2257                 :             : gimple_opt_pass *
    2258                 :      281914 : make_pass_local_pure_const (gcc::context *ctxt)
    2259                 :             : {
    2260                 :      281914 :   return new pass_local_pure_const (ctxt);
    2261                 :             : }
    2262                 :             : 
    2263                 :             : /* Emit noreturn warnings.  */
    2264                 :             : 
    2265                 :             : namespace {
    2266                 :             : 
    2267                 :             : const pass_data pass_data_warn_function_noreturn =
    2268                 :             : {
    2269                 :             :   GIMPLE_PASS, /* type */
    2270                 :             :   "*warn_function_noreturn", /* name */
    2271                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    2272                 :             :   TV_NONE, /* tv_id */
    2273                 :             :   PROP_cfg, /* properties_required */
    2274                 :             :   0, /* properties_provided */
    2275                 :             :   0, /* properties_destroyed */
    2276                 :             :   0, /* todo_flags_start */
    2277                 :             :   0, /* todo_flags_finish */
    2278                 :             : };
    2279                 :             : 
    2280                 :             : class pass_warn_function_noreturn : public gimple_opt_pass
    2281                 :             : {
    2282                 :             : public:
    2283                 :      281914 :   pass_warn_function_noreturn (gcc::context *ctxt)
    2284                 :      563828 :     : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
    2285                 :             :   {}
    2286                 :             : 
    2287                 :             :   /* opt_pass methods: */
    2288                 :     1427379 :   bool gate (function *) final override
    2289                 :             :   {
    2290                 :     1427379 :     return warn_suggest_attribute_noreturn;
    2291                 :             :   }
    2292                 :          29 :   unsigned int execute (function *fun) final override
    2293                 :             :     {
    2294                 :          29 :       if (!TREE_THIS_VOLATILE (current_function_decl)
    2295                 :          29 :           && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
    2296                 :           5 :         warn_function_noreturn (current_function_decl);
    2297                 :          29 :       return 0;
    2298                 :             :     }
    2299                 :             : 
    2300                 :             : }; // class pass_warn_function_noreturn
    2301                 :             : 
    2302                 :             : } // anon namespace
    2303                 :             : 
    2304                 :             : gimple_opt_pass *
    2305                 :      281914 : make_pass_warn_function_noreturn (gcc::context *ctxt)
    2306                 :             : {
    2307                 :      281914 :   return new pass_warn_function_noreturn (ctxt);
    2308                 :             : }
    2309                 :             : 
    2310                 :             : /* Simple local pass for pure const discovery reusing the analysis from
    2311                 :             :    ipa_pure_const.   This pass is effective when executed together with
    2312                 :             :    other optimization passes in early optimization pass queue.  */
    2313                 :             : 
    2314                 :             : namespace {
    2315                 :             : 
    2316                 :             : const pass_data pass_data_nothrow =
    2317                 :             : {
    2318                 :             :   GIMPLE_PASS, /* type */
    2319                 :             :   "nothrow", /* name */
    2320                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    2321                 :             :   TV_IPA_PURE_CONST, /* tv_id */
    2322                 :             :   0, /* properties_required */
    2323                 :             :   0, /* properties_provided */
    2324                 :             :   0, /* properties_destroyed */
    2325                 :             :   0, /* todo_flags_start */
    2326                 :             :   0, /* todo_flags_finish */
    2327                 :             : };
    2328                 :             : 
    2329                 :             : class pass_nothrow : public gimple_opt_pass
    2330                 :             : {
    2331                 :             : public:
    2332                 :      281914 :   pass_nothrow (gcc::context *ctxt)
    2333                 :      563828 :     : gimple_opt_pass (pass_data_nothrow, ctxt)
    2334                 :             :   {}
    2335                 :             : 
    2336                 :             :   /* opt_pass methods: */
    2337                 :           0 :   opt_pass * clone () final override { return new pass_nothrow (m_ctxt); }
    2338                 :     2670043 :   bool gate (function *) final override { return optimize; }
    2339                 :             :   unsigned int execute (function *) final override;
    2340                 :             : 
    2341                 :             : }; // class pass_nothrow
    2342                 :             : 
    2343                 :             : unsigned int
    2344                 :     2225522 : pass_nothrow::execute (function *)
    2345                 :             : {
    2346                 :     2225522 :   struct cgraph_node *node;
    2347                 :     2225522 :   basic_block this_block;
    2348                 :             : 
    2349                 :     2225522 :   if (TREE_NOTHROW (current_function_decl))
    2350                 :             :     return 0;
    2351                 :             : 
    2352                 :     1382263 :   node = cgraph_node::get (current_function_decl);
    2353                 :             : 
    2354                 :             :   /* We run during lowering, we cannot really use availability yet.  */
    2355                 :     1382263 :   if (cgraph_node::get (current_function_decl)->get_availability ()
    2356                 :             :       <= AVAIL_INTERPOSABLE)
    2357                 :             :     {
    2358                 :       77001 :       if (dump_file)
    2359                 :           0 :         fprintf (dump_file, "Function is interposable;"
    2360                 :             :                  " not analyzing.\n");
    2361                 :       77001 :       return true;
    2362                 :             :     }
    2363                 :             : 
    2364                 :    10034916 :   FOR_EACH_BB_FN (this_block, cfun)
    2365                 :             :     {
    2366                 :    18545026 :       for (gimple_stmt_iterator gsi = gsi_start_bb (this_block);
    2367                 :    45215762 :            !gsi_end_p (gsi);
    2368                 :    35943249 :            gsi_next (&gsi))
    2369                 :    36486108 :         if (stmt_can_throw_external (cfun, gsi_stmt (gsi)))
    2370                 :             :           {
    2371                 :      544196 :             if (is_gimple_call (gsi_stmt (gsi)))
    2372                 :             :               {
    2373                 :      342508 :                 tree callee_t = gimple_call_fndecl (gsi_stmt (gsi));
    2374                 :      342508 :                 if (callee_t && recursive_call_p (current_function_decl,
    2375                 :             :                                                   callee_t))
    2376                 :        1337 :                   continue;
    2377                 :             :               }
    2378                 :             :         
    2379                 :      542859 :             if (dump_file)
    2380                 :             :               {
    2381                 :           0 :                 fprintf (dump_file, "Statement can throw: ");
    2382                 :           0 :                 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0);
    2383                 :             :               }
    2384                 :      542859 :             return 0;
    2385                 :             :           }
    2386                 :             :     }
    2387                 :             : 
    2388                 :      762403 :   node->set_nothrow_flag (true);
    2389                 :             : 
    2390                 :      762403 :   bool cfg_changed = false;
    2391                 :      762403 :   if (self_recursive_p (node))
    2392                 :       29442 :     FOR_EACH_BB_FN (this_block, cfun)
    2393                 :       79605 :       if (gcall *g = safe_dyn_cast <gcall *> (*gsi_last_bb (this_block)))
    2394                 :             :         {
    2395                 :        2006 :           tree callee_t = gimple_call_fndecl (g);
    2396                 :        2006 :           if (callee_t
    2397                 :        1906 :               && recursive_call_p (current_function_decl, callee_t)
    2398                 :         559 :               && maybe_clean_eh_stmt (g)
    2399                 :        2010 :               && gimple_purge_dead_eh_edges (this_block))
    2400                 :             :             cfg_changed = true;
    2401                 :             :         }
    2402                 :             : 
    2403                 :      762403 :   if (dump_file)
    2404                 :          33 :     fprintf (dump_file, "Function found to be nothrow: %s\n",
    2405                 :             :              current_function_name ());
    2406                 :      762403 :   return cfg_changed ? TODO_cleanup_cfg : 0;
    2407                 :             : }
    2408                 :             : 
    2409                 :             : } // anon namespace
    2410                 :             : 
    2411                 :             : gimple_opt_pass *
    2412                 :      281914 : make_pass_nothrow (gcc::context *ctxt)
    2413                 :             : {
    2414                 :      281914 :   return new pass_nothrow (ctxt);
    2415                 :             : }
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.