OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [testsuite/] [gcc.dg/] [plugin/] [selfassign.c] - Blame information for rev 307

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 298 jeremybenn
/* This plugin contains an analysis pass that detects and warns about
2
   self-assignment statements.  */
3
/* { dg-options "-O" } */
4
 
5
#include "gcc-plugin.h"
6
#include "config.h"
7
#include "system.h"
8
#include "coretypes.h"
9
#include "tm.h"
10
#include "toplev.h"
11
#include "basic-block.h"
12
#include "gimple.h"
13
#include "tree.h"
14
#include "tree-pass.h"
15
#include "intl.h"
16
#include "plugin-version.h"
17
 
18
int plugin_is_GPL_compatible;
19
 
20
/* Indicate whether to check overloaded operator '=', which is performed by
21
   default. To disable it, use -fplugin-arg-NAME-no-check-operator-eq.  */
22
bool check_operator_eq = true;
23
 
24
/* Given a rhs EXPR of a gimple assign statement, if it is
25
   - SSA_NAME : returns its var decl, or, if it is a temp variable,
26
                returns the rhs of its SSA def statement.
27
   - VAR_DECL, PARM_DECL, FIELD_DECL, or a reference expression :
28
                returns EXPR itself.
29
   - any other expression : returns NULL_TREE.  */
30
 
31
static tree
32
get_real_ref_rhs (tree expr)
33
{
34
  switch (TREE_CODE (expr))
35
    {
36
      case SSA_NAME:
37
        {
38
          /* Given a self-assign statement, say foo.x = foo.x,
39
             the IR (after SSA) looks like:
40
 
41
             D.1797_14 = foo.x;
42
             foo.x ={v} D.1797_14;
43
 
44
             So if the rhs EXPR is an SSA_NAME of a temp variable,
45
             e.g. D.1797_14, we need to grab the rhs of its SSA def
46
             statement (i.e. foo.x).  */
47
          tree vdecl = SSA_NAME_VAR (expr);
48
          if (DECL_ARTIFICIAL (vdecl)
49
              && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
50
            {
51
              gimple def_stmt = SSA_NAME_DEF_STMT (expr);
52
              /* We are only interested in an assignment with a single
53
                 rhs operand because if it is not, the original assignment
54
                 will not possibly be a self-assignment.  */
55
              if (is_gimple_assign (def_stmt)
56
                  && (get_gimple_rhs_class (gimple_assign_rhs_code (def_stmt))
57
                      == GIMPLE_SINGLE_RHS))
58
                return get_real_ref_rhs (gimple_assign_rhs1 (def_stmt));
59
              else
60
                return NULL_TREE;
61
            }
62
          else
63
            return vdecl;
64
        }
65
      case VAR_DECL:
66
      case PARM_DECL:
67
      case FIELD_DECL:
68
      case COMPONENT_REF:
69
      case INDIRECT_REF:
70
      case ARRAY_REF:
71
        return expr;
72
      default:
73
        return NULL_TREE;
74
    }
75
}
76
 
77
/* Given an expression tree, EXPR, that may contains SSA names, returns an
78
   equivalent tree with the SSA names converted to var/parm/field decls
79
   so that it can be used with '%E' format modifier when emitting warning
80
   messages.
81
 
82
   This function currently only supports VAR/PARM/FIELD_DECL, reference
83
   expressions (COMPONENT_REF, INDIRECT_REF, ARRAY_REF), integer constant,
84
   and SSA_NAME. If EXPR contains any other tree nodes (e.g. an arithmetic
85
   expression appears in array index), NULL_TREE is returned.  */
86
 
87
static tree
88
get_non_ssa_expr (tree expr)
89
{
90
  switch (TREE_CODE (expr))
91
    {
92
      case VAR_DECL:
93
      case PARM_DECL:
94
      case FIELD_DECL:
95
        {
96
          if (DECL_NAME (expr))
97
            return expr;
98
          else
99
            return NULL_TREE;
100
        }
101
      case COMPONENT_REF:
102
        {
103
          tree base, orig_base = TREE_OPERAND (expr, 0);
104
          tree component, orig_component = TREE_OPERAND (expr, 1);
105
          base = get_non_ssa_expr (orig_base);
106
          if (!base)
107
            return NULL_TREE;
108
          component = get_non_ssa_expr (orig_component);
109
          if (!component)
110
            return NULL_TREE;
111
          /* If either BASE or COMPONENT is converted, build a new
112
             component reference tree.  */
113
          if (base != orig_base || component != orig_component)
114
            return build3 (COMPONENT_REF, TREE_TYPE (component),
115
                           base, component, NULL_TREE);
116
          else
117
            return expr;
118
        }
119
      case INDIRECT_REF:
120
        {
121
          tree orig_base = TREE_OPERAND (expr, 0);
122
          tree base = get_non_ssa_expr (orig_base);
123
          if (!base)
124
            return NULL_TREE;
125
          /* If BASE is converted, build a new indirect reference tree.  */
126
          if (base != orig_base)
127
            return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (base)), base);
128
          else
129
            return expr;
130
        }
131
      case ARRAY_REF:
132
        {
133
          tree array, orig_array = TREE_OPERAND (expr, 0);
134
          tree index, orig_index = TREE_OPERAND (expr, 1);
135
          array = get_non_ssa_expr (orig_array);
136
          if (!array)
137
            return NULL_TREE;
138
          index = get_non_ssa_expr (orig_index);
139
          if (!index)
140
            return NULL_TREE;
141
          /* If either ARRAY or INDEX is converted, build a new array
142
             reference tree.  */
143
          if (array != orig_array || index != orig_index)
144
            return build4 (ARRAY_REF, TREE_TYPE (expr), array, index,
145
                           TREE_OPERAND (expr, 2), TREE_OPERAND (expr, 3));
146
          else
147
            return expr;
148
        }
149
      case SSA_NAME:
150
        {
151
          tree vdecl = SSA_NAME_VAR (expr);
152
          if (DECL_ARTIFICIAL (vdecl)
153
              && !gimple_nop_p (SSA_NAME_DEF_STMT (expr)))
154
            {
155
              gimple def_stmt = SSA_NAME_DEF_STMT (expr);
156
              if (is_gimple_assign (def_stmt)
157
                  && (get_gimple_rhs_class (gimple_assign_rhs_code (def_stmt))
158
                      == GIMPLE_SINGLE_RHS))
159
                vdecl = gimple_assign_rhs1 (def_stmt);
160
            }
161
          return get_non_ssa_expr (vdecl);
162
        }
163
      case INTEGER_CST:
164
        return expr;
165
      default:
166
        /* Return NULL_TREE for any other kind of tree nodes.  */
167
        return NULL_TREE;
168
    }
169
}
170
 
171
/* Given the LHS and (real) RHS of a gimple assign statement, STMT, check if
172
   they are the same. If so, print a warning message about self-assignment.  */
173
 
174
static void
175
compare_and_warn (gimple stmt, tree lhs, tree rhs)
176
{
177
  if (operand_equal_p (lhs, rhs, OEP_PURE_SAME))
178
    {
179
      location_t location;
180
      location = (gimple_has_location (stmt)
181
                  ? gimple_location (stmt)
182
                  : (DECL_P (lhs)
183
                     ? DECL_SOURCE_LOCATION (lhs)
184
                     : input_location));
185
      /* If LHS contains any tree node not currently supported by
186
         get_non_ssa_expr, simply emit a generic warning without
187
         specifying LHS in the message.  */
188
      lhs = get_non_ssa_expr (lhs);
189
      if (lhs)
190
        warning_at (location, 0, G_("%qE is assigned to itself"), lhs);
191
      else
192
        warning_at (location, 0, G_("self-assignment detected"));
193
    }
194
}
195
 
196
/* Check and warn if STMT is a self-assign statement.  */
197
 
198
static void
199
warn_self_assign (gimple stmt)
200
{
201
  tree rhs, lhs;
202
 
203
  /* Check assigment statement.  */
204
  if (is_gimple_assign (stmt)
205
      && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
206
          == GIMPLE_SINGLE_RHS))
207
    {
208
      rhs = get_real_ref_rhs (gimple_assign_rhs1 (stmt));
209
      if (!rhs)
210
        return;
211
 
212
      lhs = gimple_assign_lhs (stmt);
213
      if (TREE_CODE (lhs) == SSA_NAME)
214
        {
215
          lhs = SSA_NAME_VAR (lhs);
216
          if (DECL_ARTIFICIAL (lhs))
217
            return;
218
        }
219
 
220
      compare_and_warn (stmt, lhs, rhs);
221
    }
222
  /* Check overloaded operator '=' (if enabled).  */
223
  else if (check_operator_eq && is_gimple_call (stmt))
224
    {
225
      tree fdecl = gimple_call_fndecl (stmt);
226
      if (fdecl && (DECL_NAME (fdecl) == maybe_get_identifier ("operator=")))
227
        {
228
          /* If 'operator=' takes reference operands, the arguments will be
229
             ADDR_EXPR trees. In this case, just remove the address-taken
230
             operator before we compare the lhs and rhs.  */
231
          lhs = gimple_call_arg (stmt, 0);
232
          if (TREE_CODE (lhs) == ADDR_EXPR)
233
            lhs = TREE_OPERAND (lhs, 0);
234
          rhs = gimple_call_arg (stmt, 1);
235
          if (TREE_CODE (rhs) == ADDR_EXPR)
236
            rhs = TREE_OPERAND (rhs, 0);
237
 
238
          compare_and_warn (stmt, lhs, rhs);
239
        }
240
    }
241
}
242
 
243
/* Entry point for the self-assignment detection pass.  */
244
 
245
static unsigned int
246
execute_warn_self_assign (void)
247
{
248
  gimple_stmt_iterator gsi;
249
  basic_block bb;
250
 
251
  FOR_EACH_BB (bb)
252
    {
253
      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
254
        warn_self_assign (gsi_stmt (gsi));
255
    }
256
 
257
  return 0;
258
}
259
 
260
/* Pass gate function. Currently always returns true.  */
261
 
262
static bool
263
gate_warn_self_assign (void)
264
{
265
  return true;
266
}
267
 
268
static struct gimple_opt_pass pass_warn_self_assign =
269
{
270
  {
271
    GIMPLE_PASS,
272
    "warn_self_assign",                   /* name */
273
    gate_warn_self_assign,                /* gate */
274
    execute_warn_self_assign,             /* execute */
275
    NULL,                                 /* sub */
276
    NULL,                                 /* next */
277
    0,                                    /* static_pass_number */
278
    0,                                    /* tv_id */
279
    PROP_ssa,                             /* properties_required */
280
    0,                                    /* properties_provided */
281
    0,                                    /* properties_destroyed */
282
    0,                                    /* todo_flags_start */
283
    TODO_dump_func                        /* todo_flags_finish */
284
  }
285
};
286
 
287
/* The initialization routine exposed to and called by GCC. The spec of this
288
   function is defined in gcc/gcc-plugin.h.
289
 
290
   PLUGIN_NAME - name of the plugin (useful for error reporting)
291
   ARGC        - the size of the ARGV array
292
   ARGV        - an array of key-value argument pair
293
 
294
   Returns 0 if initialization finishes successfully.
295
 
296
   Note that this function needs to be named exactly "plugin_init".  */
297
 
298
int
299
plugin_init (struct plugin_name_args *plugin_info,
300
             struct plugin_gcc_version *version)
301
{
302
  struct register_pass_info pass_info;
303
  const char *plugin_name = plugin_info->base_name;
304
  int argc = plugin_info->argc;
305
  struct plugin_argument *argv = plugin_info->argv;
306
  bool enabled = true;
307
  int i;
308
 
309
  if (!plugin_default_version_check (version, &gcc_version))
310
    return 1;
311
 
312
  /* Self-assign detection should happen after SSA is constructed.  */
313
  pass_info.pass = &pass_warn_self_assign.pass;
314
  pass_info.reference_pass_name = "ssa";
315
  pass_info.ref_pass_instance_number = 1;
316
  pass_info.pos_op = PASS_POS_INSERT_AFTER;
317
 
318
  /* Process the plugin arguments. This plugin takes the following arguments:
319
     check-operator-eq, no-check-operator-eq, enable, and disable.
320
     By default, the analysis is enabled with 'operator=' checked.  */
321
  for (i = 0; i < argc; ++i)
322
    {
323
      if (!strcmp (argv[i].key, "check-operator-eq"))
324
        {
325
          if (argv[i].value)
326
            warning (0, G_("option '-fplugin-arg-%s-check-operator-eq=%s'"
327
                           " ignored (superfluous '=%s')"),
328
                     plugin_name, argv[i].value, argv[i].value);
329
          else
330
            check_operator_eq = true;
331
        }
332
      else if (!strcmp (argv[i].key, "no-check-operator-eq"))
333
        {
334
          if (argv[i].value)
335
            warning (0, G_("option '-fplugin-arg-%s-no-check-operator-eq=%s'"
336
                           " ignored (superfluous '=%s')"),
337
                     plugin_name, argv[i].value, argv[i].value);
338
          else
339
            check_operator_eq = false;
340
        }
341
      else if (!strcmp (argv[i].key, "enable"))
342
        {
343
          if (argv[i].value)
344
            warning (0, G_("option '-fplugin-arg-%s-enable=%s' ignored"
345
                           " (superfluous '=%s')"),
346
                     plugin_name, argv[i].value, argv[i].value);
347
          else
348
            enabled = true;
349
        }
350
      else if (!strcmp (argv[i].key, "disable"))
351
        {
352
          if (argv[i].value)
353
            warning (0, G_("option '-fplugin-arg-%s-disable=%s' ignored"
354
                           " (superfluous '=%s')"),
355
                     plugin_name, argv[i].value, argv[i].value);
356
          else
357
            enabled = false;
358
        }
359
      else
360
        warning (0, G_("plugin %qs: unrecognized argument %qs ignored"),
361
                 plugin_name, argv[i].key);
362
    }
363
 
364
  /* Register this new pass with GCC if the analysis is enabled.  */
365
  if (enabled)
366
    register_callback (plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
367
                       &pass_info);
368
 
369
  return 0;
370
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.