LCOV - code coverage report
Current view: top level - gcc/c-family - c-gimplify.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 90 107 84.1 %
Date: 2020-03-28 11:57:23 Functions: 4 5 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Tree lowering pass.  This pass gimplifies the tree representation built
       2                 :            :    by the C-based front ends.  The structure of gimplified, or
       3                 :            :    language-independent, trees is dictated by the grammar described in this
       4                 :            :    file.
       5                 :            :    Copyright (C) 2002-2020 Free Software Foundation, Inc.
       6                 :            :    Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net>
       7                 :            :    Re-written to support lowering of whole function trees, documentation
       8                 :            :    and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com>
       9                 :            : 
      10                 :            : This file is part of GCC.
      11                 :            : 
      12                 :            : GCC is free software; you can redistribute it and/or modify it under
      13                 :            : the terms of the GNU General Public License as published by the Free
      14                 :            : Software Foundation; either version 3, or (at your option) any later
      15                 :            : version.
      16                 :            : 
      17                 :            : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      18                 :            : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      19                 :            : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      20                 :            : for more details.
      21                 :            : 
      22                 :            : You should have received a copy of the GNU General Public License
      23                 :            : along with GCC; see the file COPYING3.  If not see
      24                 :            : <http://www.gnu.org/licenses/>.  */
      25                 :            : 
      26                 :            : #include "config.h"
      27                 :            : #include "system.h"
      28                 :            : #include "coretypes.h"
      29                 :            : #include "tm.h"
      30                 :            : #include "function.h"
      31                 :            : #include "basic-block.h"
      32                 :            : #include "tree.h"
      33                 :            : #include "gimple.h"
      34                 :            : #include "cgraph.h"
      35                 :            : #include "c-pretty-print.h"
      36                 :            : #include "gimplify.h"
      37                 :            : #include "langhooks.h"
      38                 :            : #include "dumpfile.h"
      39                 :            : #include "c-ubsan.h"
      40                 :            : 
      41                 :            : /*  The gimplification pass converts the language-dependent trees
      42                 :            :     (ld-trees) emitted by the parser into language-independent trees
      43                 :            :     (li-trees) that are the target of SSA analysis and transformations.
      44                 :            : 
      45                 :            :     Language-independent trees are based on the SIMPLE intermediate
      46                 :            :     representation used in the McCAT compiler framework:
      47                 :            : 
      48                 :            :     "Designing the McCAT Compiler Based on a Family of Structured
      49                 :            :     Intermediate Representations,"
      50                 :            :     L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
      51                 :            :     Proceedings of the 5th International Workshop on Languages and
      52                 :            :     Compilers for Parallel Computing, no. 757 in Lecture Notes in
      53                 :            :     Computer Science, New Haven, Connecticut, pp. 406-420,
      54                 :            :     Springer-Verlag, August 3-5, 1992.
      55                 :            : 
      56                 :            :     http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
      57                 :            : 
      58                 :            :     Basically, we walk down gimplifying the nodes that we encounter.  As we
      59                 :            :     walk back up, we check that they fit our constraints, and copy them
      60                 :            :     into temporaries if not.  */
      61                 :            : 
      62                 :            : /* Callback for c_genericize.  */
      63                 :            : 
      64                 :            : static tree
      65                 :     831786 : ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees, void *data)
      66                 :            : {
      67                 :     831786 :   hash_set<tree> *pset = (hash_set<tree> *) data;
      68                 :            : 
      69                 :     831786 :   if (TREE_CODE (*tp) == BIND_EXPR)
      70                 :            :     {
      71                 :            :       /* Since walk_tree doesn't call the callback function on the decls
      72                 :            :          in BIND_EXPR_VARS, we have to walk them manually, so we can avoid
      73                 :            :          instrumenting DECL_INITIAL of TREE_STATIC vars.  */
      74                 :      41436 :       *walk_subtrees = 0;
      75                 :      49546 :       for (tree decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
      76                 :            :         {
      77                 :       8110 :           if (TREE_STATIC (decl))
      78                 :         97 :             continue;
      79                 :       8013 :           walk_tree (&DECL_INITIAL (decl), ubsan_walk_array_refs_r, pset,
      80                 :            :                      pset);
      81                 :       8013 :           walk_tree (&DECL_SIZE (decl), ubsan_walk_array_refs_r, pset, pset);
      82                 :       8013 :           walk_tree (&DECL_SIZE_UNIT (decl), ubsan_walk_array_refs_r, pset,
      83                 :            :                      pset);
      84                 :            :         }
      85                 :      41436 :       walk_tree (&BIND_EXPR_BODY (*tp), ubsan_walk_array_refs_r, pset, pset);
      86                 :            :     }
      87                 :     790350 :   else if (TREE_CODE (*tp) == ADDR_EXPR
      88                 :     790350 :            && TREE_CODE (TREE_OPERAND (*tp, 0)) == ARRAY_REF)
      89                 :            :     {
      90                 :        334 :       ubsan_maybe_instrument_array_ref (&TREE_OPERAND (*tp, 0), true);
      91                 :            :       /* Make sure ubsan_maybe_instrument_array_ref is not called again
      92                 :            :          on the ARRAY_REF, the above call might not instrument anything
      93                 :            :          as the index might be constant or masked, so ensure it is not
      94                 :            :          walked again and walk its subtrees manually.  */
      95                 :        334 :       tree aref = TREE_OPERAND (*tp, 0);
      96                 :        334 :       pset->add (aref);
      97                 :        334 :       *walk_subtrees = 0;
      98                 :        334 :       walk_tree (&TREE_OPERAND (aref, 0), ubsan_walk_array_refs_r, pset, pset);
      99                 :        334 :       walk_tree (&TREE_OPERAND (aref, 1), ubsan_walk_array_refs_r, pset, pset);
     100                 :        334 :       walk_tree (&TREE_OPERAND (aref, 2), ubsan_walk_array_refs_r, pset, pset);
     101                 :        334 :       walk_tree (&TREE_OPERAND (aref, 3), ubsan_walk_array_refs_r, pset, pset);
     102                 :            :     }
     103                 :     790016 :   else if (TREE_CODE (*tp) == ARRAY_REF)
     104                 :       2794 :     ubsan_maybe_instrument_array_ref (tp, false);
     105                 :     831786 :   return NULL_TREE;
     106                 :            : }
     107                 :            : 
     108                 :            : /* Gimplification of statement trees.  */
     109                 :            : 
     110                 :            : /* Convert the tree representation of FNDECL from C frontend trees to
     111                 :            :    GENERIC.  */
     112                 :            : 
     113                 :            : void
     114                 :   30037400 : c_genericize (tree fndecl)
     115                 :            : {
     116                 :   30037400 :   FILE *dump_orig;
     117                 :   30037400 :   dump_flags_t local_dump_flags;
     118                 :   30037400 :   struct cgraph_node *cgn;
     119                 :            : 
     120                 :   30037400 :   if (flag_sanitize & SANITIZE_BOUNDS)
     121                 :            :     {
     122                 :      89214 :       hash_set<tree> pset;
     123                 :      44607 :       walk_tree (&DECL_SAVED_TREE (fndecl), ubsan_walk_array_refs_r, &pset,
     124                 :            :                  &pset);
     125                 :            :     }
     126                 :            : 
     127                 :   30037400 :   if (warn_duplicated_branches)
     128                 :        136 :     walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
     129                 :            :                                   do_warn_duplicated_branches_r, NULL);
     130                 :            : 
     131                 :            :   /* Dump the C-specific tree IR.  */
     132                 :   30037400 :   dump_orig = get_dump_info (TDI_original, &local_dump_flags);
     133                 :   30037400 :   if (dump_orig)
     134                 :            :     {
     135                 :        871 :       fprintf (dump_orig, "\n;; Function %s",
     136                 :        871 :                lang_hooks.decl_printable_name (fndecl, 2));
     137                 :       1742 :       fprintf (dump_orig, " (%s)\n",
     138                 :        871 :                (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
     139                 :          0 :                 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
     140                 :        871 :       fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
     141                 :        871 :       fprintf (dump_orig, "\n");
     142                 :            : 
     143                 :        871 :       if (local_dump_flags & TDF_RAW)
     144                 :          1 :         dump_node (DECL_SAVED_TREE (fndecl),
     145                 :            :                    TDF_SLIM | local_dump_flags, dump_orig);
     146                 :            :       else
     147                 :        870 :         print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
     148                 :        871 :       fprintf (dump_orig, "\n");
     149                 :            :     }
     150                 :            : 
     151                 :            :   /* Dump all nested functions now.  */
     152                 :   30037400 :   cgn = cgraph_node::get_create (fndecl);
     153                 :   30038800 :   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
     154                 :       1400 :     c_genericize (cgn->decl);
     155                 :   30037400 : }
     156                 :            : 
     157                 :            : static void
     158                 :          0 : add_block_to_enclosing (tree block)
     159                 :            : {
     160                 :          0 :   unsigned i;
     161                 :          0 :   tree enclosing;
     162                 :          0 :   gbind *bind;
     163                 :          0 :   vec<gbind *> stack = gimple_bind_expr_stack ();
     164                 :            : 
     165                 :          0 :   FOR_EACH_VEC_ELT (stack, i, bind)
     166                 :          0 :     if (gimple_bind_block (bind))
     167                 :            :       break;
     168                 :            : 
     169                 :          0 :   enclosing = gimple_bind_block (bind);
     170                 :          0 :   BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
     171                 :          0 : }
     172                 :            : 
     173                 :            : /* Genericize a scope by creating a new BIND_EXPR.
     174                 :            :    BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
     175                 :            :      In the latter case, we need to create a new BLOCK and add it to the
     176                 :            :      BLOCK_SUBBLOCKS of the enclosing block.
     177                 :            :    BODY is a chain of C _STMT nodes for the contents of the scope, to be
     178                 :            :      genericized.  */
     179                 :            : 
     180                 :            : tree
     181                 :   41157400 : c_build_bind_expr (location_t loc, tree block, tree body)
     182                 :            : {
     183                 :   41157400 :   tree decls, bind;
     184                 :            : 
     185                 :   41157400 :   if (block == NULL_TREE)
     186                 :            :     decls = NULL_TREE;
     187                 :   24579000 :   else if (TREE_CODE (block) == BLOCK)
     188                 :   24579000 :     decls = BLOCK_VARS (block);
     189                 :            :   else
     190                 :            :     {
     191                 :          0 :       decls = block;
     192                 :          0 :       if (DECL_ARTIFICIAL (decls))
     193                 :            :         block = NULL_TREE;
     194                 :            :       else
     195                 :            :         {
     196                 :          0 :           block = make_node (BLOCK);
     197                 :          0 :           BLOCK_VARS (block) = decls;
     198                 :          0 :           add_block_to_enclosing (block);
     199                 :            :         }
     200                 :            :     }
     201                 :            : 
     202                 :   41157400 :   if (!body)
     203                 :          0 :     body = build_empty_stmt (loc);
     204                 :   41157400 :   if (decls || block)
     205                 :            :     {
     206                 :   24579000 :       bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
     207                 :   24579000 :       TREE_SIDE_EFFECTS (bind) = 1;
     208                 :   24579000 :       SET_EXPR_LOCATION (bind, loc);
     209                 :            :     }
     210                 :            :   else
     211                 :            :     bind = body;
     212                 :            : 
     213                 :   41157400 :   return bind;
     214                 :            : }
     215                 :            : 
     216                 :            : /* Gimplification of expression trees.  */
     217                 :            : 
     218                 :            : /* Do C-specific gimplification on *EXPR_P.  PRE_P and POST_P are as in
     219                 :            :    gimplify_expr.  */
     220                 :            : 
     221                 :            : int
     222                 :  168035000 : c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
     223                 :            :                  gimple_seq *post_p ATTRIBUTE_UNUSED)
     224                 :            : {
     225                 :  168035000 :   enum tree_code code = TREE_CODE (*expr_p);
     226                 :            : 
     227                 :  168035000 :   switch (code)
     228                 :            :     {
     229                 :     267693 :     case LSHIFT_EXPR:
     230                 :     267693 :     case RSHIFT_EXPR:
     231                 :     267693 :     case LROTATE_EXPR:
     232                 :     267693 :     case RROTATE_EXPR:
     233                 :     267693 :       {
     234                 :            :         /* We used to convert the right operand of a shift-expression
     235                 :            :            to an integer_type_node in the FEs.  But it is unnecessary
     236                 :            :            and not desirable for diagnostics and sanitizers.  We keep
     237                 :            :            this here to not pessimize the code, but we convert to an
     238                 :            :            unsigned type, because negative shift counts are undefined
     239                 :            :            anyway.
     240                 :            :            We should get rid of this conversion when we have a proper
     241                 :            :            type demotion/promotion pass.  */
     242                 :     267693 :         tree *op1_p = &TREE_OPERAND (*expr_p, 1);
     243                 :     267693 :         if (!VECTOR_TYPE_P (TREE_TYPE (*op1_p))
     244                 :     266073 :             && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
     245                 :            :                                     unsigned_type_node)
     246                 :     489563 :             && !types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (*op1_p)),
     247                 :            :                                     integer_type_node))
     248                 :            :           /* Make sure to unshare the result, tree sharing is invalid
     249                 :            :              during gimplification.  */
     250                 :      17272 :           *op1_p = unshare_expr (convert (unsigned_type_node, *op1_p));
     251                 :            :         break;
     252                 :            :       }
     253                 :            : 
     254                 :    2098320 :     case DECL_EXPR:
     255                 :            :       /* This is handled mostly by gimplify.c, but we have to deal with
     256                 :            :          not warning about int x = x; as it is a GCC extension to turn off
     257                 :            :          this warning but only if warn_init_self is zero.  */
     258                 :    2098320 :       if (VAR_P (DECL_EXPR_DECL (*expr_p))
     259                 :    2084080 :           && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
     260                 :    2084080 :           && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
     261                 :    1991840 :           && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
     262                 :    2099080 :           && !warn_init_self)
     263                 :        736 :         TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
     264                 :            :       break;
     265                 :            : 
     266                 :     664508 :     case PREINCREMENT_EXPR:
     267                 :     664508 :     case PREDECREMENT_EXPR:
     268                 :     664508 :     case POSTINCREMENT_EXPR:
     269                 :     664508 :     case POSTDECREMENT_EXPR:
     270                 :     664508 :       {
     271                 :     664508 :         tree type = TREE_TYPE (TREE_OPERAND (*expr_p, 0));
     272                 :     664508 :         if (INTEGRAL_TYPE_P (type) && c_promoting_integer_type_p (type))
     273                 :            :           {
     274                 :       5855 :             if (!TYPE_OVERFLOW_WRAPS (type))
     275                 :       2178 :               type = unsigned_type_for (type);
     276                 :       3677 :             return gimplify_self_mod_expr (expr_p, pre_p, post_p, 1, type);
     277                 :            :           }
     278                 :            :         break;
     279                 :            :       }
     280                 :            : 
     281                 :            :     default:;
     282                 :            :     }
     283                 :            : 
     284                 :            :   return GS_UNHANDLED;
     285                 :            : }

Generated by: LCOV version 1.0

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 --enable-host-shared. GCC test suite is run with the built compiler.