summaryrefslogtreecommitdiff
path: root/gcc/gimple-escaper.c
blob: a3cd880c2818d92cbbc9c1ffd1101b135af7be61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple-expr.h"
#include "predict.h"
#include "alloc-pool.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "fold-const.h"
#include "gimple-fold.h"
#include "symbol-summary.h"
#include "tree-vrp.h"
#include "ipa-prop.h"
#include "tree-pretty-print.h"
#include "tree-inline.h"
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "tree-ssa-ccp.h"
#include "stringpool.h"
#include "attribs.h"
#include "tree-ssa-alias.h"
#include "tree-ssanames.h"
#include "gimple.h"
#include "cfg.h" // needed for gimple-iterator.h
#include "gimple-iterator.h"
#include "gimple-ssa.h"
#include <stdbool.h>

#include "gimple-escaper.hpp"

void
GimpleEscaper::_init()
{
  cgraph_node *cnode = NULL;
  FOR_EACH_FUNCTION(cnode)
  {
    gcc_assert(cnode);
    const bool filter = GimpleEscaper::filter_known_function(cnode);
    if (filter) continue;

    const_tree decl = cnode->decl;
    gcc_assert(decl);
    undefined.insert(decl);
  }

  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY(cnode)
  {
    gcc_assert(cnode);
    cnode->get_untransformed_body();
    const_tree decl = cnode->decl;
    gcc_assert(decl);
    undefined.erase(decl);
  }
}

bool
GimpleEscaper::is_function_escaping(cgraph_node *cnode)
{
  const bool filter = GimpleEscaper::filter_known_function(cnode);
  if (filter) return false;

  gcc_assert(cnode);
  return cnode->externally_visible;
}

bool 
GimpleEscaper::filter_known_function(cgraph_node *node)
{
  gcc_assert(node);
  bool filter = false;
  const char *_free = "free";
  const char *_malloc = "malloc";
  const char *_realloc = "realloc";
  const char *_calloc = "calloc";
  const char *_memset = "memset";
  const char *name = node->name();
  gcc_assert(name);
  filter |= strcmp(_free, name) == 0;
  filter |= strcmp(_malloc, name) == 0;
  filter |= strcmp(_realloc, name) == 0;
  filter |= strcmp(_memset, name) == 0;
  filter |= strcmp(_calloc, name) == 0;
  return filter;
}

void
GimpleEscaper::_walk_pre(__attribute__((unused)) const_tree t)
{
  // Is any global variable escaping?
  Reason reason;
  //exprEscaper.update(expr, reason);
}

void
GimpleEscaper::_walk_pre(__attribute__((unused)) gassign *s)
{
  Reason reason;
  // We really should also walk over the different assigns...

// Is there a type cast?
//exprEscaper.update(expr, reason);
}

void
GimpleEscaper::_walk_pre(__attribute__((unused)) greturn *s)
{
  Reason reason;
  //exprEscaper.update(expr, reason);
}

void
GimpleEscaper::_walk_pre(__attribute__((unused)) gcond *s)
{
  Reason reason;
  //exprEscaper.update(expr, reason);
}

void
GimpleEscaper::_walk_pre(gcall *s)
{
  tree fn = gimple_call_fndecl(s);
  const bool is_undefined = undefined.find(fn) != undefined.end();

  Reason arg_reason;
  arg_reason.is_escaping = is_undefined;
  arg_reason.parameter_is_visible = is_undefined;
  unsigned n = gimple_call_num_args(s);
  for (unsigned i = 0; i < n; i++)
  {
    const_tree a = gimple_call_arg(s, i);
    gcc_assert(a);
    exprEscaper.update(a, arg_reason);
  }

  const_tree lhs = gimple_call_lhs(s);
  if (!lhs) return;

  Reason return_reason;
  return_reason.is_escaping = is_undefined;
  return_reason.return_is_visible = is_undefined;
  exprEscaper.update(lhs, return_reason);
}