summaryrefslogtreecommitdiff
path: root/gcc/ipa-hello-world.c
blob: 8aa305f8812b76d97790bf771411b5137271f3af (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#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"
#include "gimple-iterator.h"
#include "gimple-ssa.h"

#include "compare-types.h"
#include "types-inlines.h"
#include <set>
#include <string>

#include "collect-types.h"
#include "name-types.h"

namespace type_playground {
enum type_comparison_func_enum {
  EQ_POINTER = 0,
  EQ_IDENTIFIER,
  EQ_MAIN_VARIANT,
  EQ_CANONICAL,
  EQ_STRUCTURAL,
  EQ_CANONICAL_STRICT,
  EQ_COMPARE,
  EQ_END
};
static unsigned const type_comparisons = EQ_END;
static const char* names[type_comparisons] = {
	"EQ_POINTER",
	"EQ_IDENTIFIER",
	"EQ_MAIN_VARIANT",
	"EQ_CANONICAL",
	"EQ_CANONICAL_STRICT",
	"EQ_STRUCTURAL",
	"EQ_COMPARE"
};
typedef bool (*type_comparison_func_t)(const_tree, const_tree);
static type_comparison_func_t comparisons[type_comparisons] = { 
	eq_pointer,
	eq_identifier,
	eq_main_variant,
	eq_canonical,
	eq_canonical_internal,
	eq_type_structural,
	eq_type_compare
};
}


static void
collect_types_from_cnode_decl(cgraph_node *cnode, typeset &types)
{
  gcc_assert(cnode);
  const_tree decl = cnode->decl;
  gcc_assert(decl);
  const_tree decl_type = TREE_TYPE(decl);
  gcc_assert(decl_type);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert(func);
  push_cfun(func);
  // This will collect return, arguments and decl_type itself
  collect_types(decl_type, types);
  pop_cfun();
}

static void
collect_types_from_cnode_locals(cgraph_node *cnode, typeset &types)
{
  gcc_assert(cnode);
  const_tree decl = cnode->decl;
  gcc_assert(decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert(func);
  int i = 0;
  tree var_decl = NULL;
  FOR_EACH_LOCAL_DECL(func, i, var_decl)
  {
    gcc_assert(var_decl);
    const_tree var_decl_type = TREE_TYPE(var_decl);
    collect_types(var_decl_type, types);
  }
}

static void
collect_types_from_cnode_ssa_names(cgraph_node *cnode, typeset &types)
{
  gcc_assert(cnode);
  const_tree decl = cnode->decl;
  gcc_assert(decl);
  function *func = DECL_STRUCT_FUNCTION (decl);
  gcc_assert(func);
  size_t i = 0;
  tree ssa_name = NULL;
  push_cfun(func);
  FOR_EACH_SSA_NAME(i, ssa_name, cfun)
  {
     gcc_assert(ssa_name);
     const_tree ssa_name_type = TREE_TYPE(ssa_name);
     collect_types(ssa_name_type, types);
  }
  pop_cfun();
}

static void
collect_types_from_functions_with_gimple_body(cgraph_node *cnode, typeset &types)
{
  gcc_assert(cnode);
  collect_types_from_cnode_decl(cnode, types);
  collect_types_from_cnode_locals(cnode, types);
  //collect_types_from_cnode_ssa_names(cnode, types);
}

static void
print_types_in_set(typeset &types)
{
  for (auto it = types.cbegin(); it != types.cend(); ++it)
  {
    const_tree type = *it;
    std::string name = type_to_string(type);
    log("name: %s\n", name.c_str());
  }
}

static const bool
filter_comparisons_functions_char(const char* function_name)
{
  // Everything should run
  if (!flag_tp_comparison_functions) return true;

  const size_t buffer_size = 1024;
  const size_t cli_length = strlen(flag_tp_comparison_functions);
  gcc_assert(buffer_size > cli_length);
  char whitelist[buffer_size];
  strcpy(whitelist, flag_tp_comparison_functions);
  

  char* saveptr = whitelist;
  char* token = NULL;
  while (token = strtok_r(saveptr, ",", &saveptr))
  {
    const bool name_allowed = strcmp(token, function_name) == 0;
    if (name_allowed) return true;
  }

  return false;
}

static const bool
filter_comparisons_functions_int(unsigned function_id)
{
  if (!flag_tp_comparison_functions) return true;

  const char* function_name = type_playground::names[function_id];
  return filter_comparisons_functions_char(function_name);
}

static const bool
filter_comparisons_functions(type_playground::type_comparison_func_t func)
{
  if (!flag_tp_comparison_functions) return true;

  for (unsigned i = 0; i < type_playground::type_comparisons; i++)
  {
    if (func != type_playground::comparisons[i]) continue;
    
    return filter_comparisons_functions_int(i);
  }

  return false;
}


static void
compare_types_in_set(typeset &types)
{
  for (auto i = types.cbegin(); i != types.cend(); ++i)
  {
    for (auto j = types.cbegin(); j != types.cend(); ++j)
    {
    const_tree a = *i;
    const_tree b = *j;
    log("%s x %s = ", type_to_string(a).c_str(), type_to_string(b).c_str());
      for (unsigned k = 0; k < type_playground::type_comparisons; k++)
      {
	type_playground::type_comparison_func_t comparison = type_playground::comparisons[k];
	const bool allowed_to_run = filter_comparisons_functions(comparison);
	if (!allowed_to_run) continue;

	const bool result = comparison(a, b);
	log("%s, ", result ? "t" : "f");
      }
    log("\n");
    }
  }
}

static void
filter_out_types_in_set(typeset &types)
{
  // compare everything
  if (!flag_tp_types_compared) return;

  const size_t buffer_size = 1024;
  const size_t cli_length = strlen(flag_tp_comparison_functions);
  gcc_assert(buffer_size > cli_length);
  char whitelist[buffer_size];
  strcpy(whitelist, flag_tp_types_compared);
  
  bool name_allowed = false;
  for (auto it = types.cbegin(); it != types.cend(); name_allowed ? ++it : it = types.erase(it))
  {
    const_tree type = *it;
    std::string observed_name = get_type_identifier(type);
    char* saveptr = whitelist;
    char* expected_name = NULL;
    name_allowed = false;

    while (expected_name = strtok_r(saveptr, ",", &saveptr))
    {
      name_allowed |= strcmp(expected_name, observed_name.c_str()) == 0;
    }
  }

}

static unsigned int
iphw_execute()
{
  //test_type_equality::run_tests();
  //test_naming_types::run_tests();
  cgraph_node *node = NULL;
  typeset types;
  FOR_EACH_FUNCTION_WITH_GIMPLE_BODY(node)
  {
    node->get_untransformed_body();
    collect_types_from_functions_with_gimple_body(node, types);
  }

  filter_out_types_in_set(types);
  compare_types_in_set(types);
  return 0;
}

namespace {
const pass_data pass_data_ipa_hello_world =
{
  SIMPLE_IPA_PASS,
  "hello-world",
  OPTGROUP_NONE,
  TV_NONE,
  (PROP_cfg | PROP_ssa),
  0,
  0,
  TODO_rebuild_alias,
  0,
};

class pass_ipa_hello_world : public simple_ipa_opt_pass
{
public:
  pass_ipa_hello_world (gcc::context *ctx)
    : simple_ipa_opt_pass(pass_data_ipa_hello_world, ctx)
  {}

  virtual bool gate(function*) { return flag_ipa_hello_world; }
  virtual unsigned execute (function*) { return iphw_execute(); }
};
} // anon namespace

simple_ipa_opt_pass*
make_pass_ipa_hello_world (gcc::context *ctx)
{
  return new pass_ipa_hello_world (ctx);
}