aboutsummaryrefslogtreecommitdiff
path: root/gcc/ici/ici.c
blob: 6ea7264157781ad11847912c5a2f5f569958b68a (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Callgraph based interprocedural optimizations.
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
   Free Software Foundation, Inc.
   Contributed by Jan Hubicka

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "cgraph.h"
#include "function.h"
#include "tree-pass.h"
#include "opts.h"
#include "plugin.h" /* Need internal version for invoke_plugin_callbacks.  */
#include "highlev-plugin-internal.h"

void plugin_is_GPL_compatible (void);
static void ici_load_function_specific_optimizations (void);

void
plugin_is_GPL_compatible (void)
{
}

static int ici_all_ipa_passes = 1;
static int ici_bypass_gimple_in_ipa = 0;

/* Perform ICI internal tasks at the start of IPA passes.
   This must run before any ICI user registered callbacks, which is why
   register_plugin_event must unregister / re-register this callback when
   a ICI user callback for PLUGIN_ALL_IPA_PASSES_START is added.  */
static void
ici_all_ipa_passes_start (void *gcc_data ATTRIBUTE_UNUSED,
			  void *user_data ATTRIBUTE_UNUSED)
{
  ici_load_function_specific_optimizations ();
  register_event_parameter ("all_ipa_passes", &ici_all_ipa_passes, EP_SILENT);
  register_event_parameter ("bypass_gimple_in_ipa", &ici_bypass_gimple_in_ipa, 
			    EP_SILENT);
}

/* Execute ICI internal tasks at the end of IPA passes.
   This must run after all ICI user registered PLUGIN_ALL_IPA_PASSES_END
   callbacks, which we get automatically be registering this callback
   first.  */
static void
ici_all_ipa_passes_end (void *gcc_data ATTRIBUTE_UNUSED,
			void *user_data ATTRIBUTE_UNUSED)
{
  unregister_event_parameter ("bypass_gimple_in_ipa");
  unregister_event_parameter ("all_ipa_passes");
}

/* This one again should run before ICI user callbacks.  */
static void
ici_early_gimple_passes_start (void *gcc_data ATTRIBUTE_UNUSED,
			       void *user_data ATTRIBUTE_UNUSED)
{
  /* Switch to GIMPLE passes.  */
  ici_all_ipa_passes = 2;

  /* GIMPLE passes are recorded only once.  ici_bypass_gimple_in_ipa
     will be set again in ici_set_bypass_gimple_in_ipa.  */
  ici_bypass_gimple_in_ipa = 0;
}

/* Set bypass_gimple_in_ipa back to 1 to stop recording GIMPLE passes.  */
static void
ici_set_bypass_gimple_in_ipa (void *gcc_data ATTRIBUTE_UNUSED,
			      void *user_data ATTRIBUTE_UNUSED)
{
  ici_bypass_gimple_in_ipa = 1;
}

/* The low-level callback implementation calls the last registered
   callback first.  Make sure that, if appropriate, the ICI internal
   callback for EVENT is registered last, i.e. called first.
   If EVENT is -1, we are called from the ICI plugin_init, and are to
   initialize all permanent ICI internall callbacks.  */
void
ici_refresh_internal_callbacks (int event)
{
  if (event == PLUGIN_ALL_IPA_PASSES_START)
    unregister_callback ("ICI_INTERNAL", PLUGIN_ALL_IPA_PASSES_START);
  if (event == PLUGIN_ALL_IPA_PASSES_START || event < 0)
    register_callback ("ICI_INTERNAL", PLUGIN_ALL_IPA_PASSES_START,
		       ici_all_ipa_passes_start, NULL);
  if (event == PLUGIN_EARLY_GIMPLE_PASSES_START)
    unregister_callback ("ICI_INTERNAL", PLUGIN_EARLY_GIMPLE_PASSES_START);
  if (event == PLUGIN_EARLY_GIMPLE_PASSES_START || event < 0)
    register_callback ("ICI_INTERNAL", PLUGIN_EARLY_GIMPLE_PASSES_START,
		       ici_early_gimple_passes_start, NULL);
  if (event < 0)
    {
      register_callback ("ICI_INTERNAL", PLUGIN_ALL_IPA_PASSES_END,
			 ici_all_ipa_passes_end, NULL);
      register_callback ("ICI_INTERNAL", PLUGIN_PASS_EXECUTION,
			 ici_set_bypass_gimple_in_ipa, NULL);
    }
}

int
plugin_init (struct plugin_name_args *plugin_info ATTRIBUTE_UNUSED,
	     struct plugin_gcc_version *version ATTRIBUTE_UNUSED)
{
  static struct register_pass_info instrument_functions_info
    = { &pass_instrument_functions.pass, "early_local_cleanups",
	1, PASS_POS_INSERT_BEFORE
      };

  ici_refresh_internal_callbacks (-1);
  register_callback ("ICI_INTERNAL", PLUGIN_PASS_MANAGER_SETUP,
		     NULL, &instrument_functions_info);
  load_ici_plugin ();
  return 0; /* Success.  */
}

/* Load function specific optimizations with parameters.  */

/* separate string into arguments.  */
static void
ici_separate_arguments (const char *string, unsigned int *argc, char ***argv)
{
  const char *p;
  char c;
  char **args;
  unsigned int len; 
  int i;

  /* Count number of args  */
  p = string;
  c = *p;
  *argc = 1; 
  while (c)
    {    
      if ((c == ' ' || c == '\t') && len) 
        {    
          len = 0; 
          ++*argc;
        }    
      else 
        len = 1; 
      c = *++p;
    }    
  if (len)
    ++*argc;

  args = (char **) xmalloc (sizeof(char *) * (*argc));
  *argv = args;
  args[0] = (char*)xmalloc(sizeof(char)); /* argv[0] unavailable  */
  args[0][0]='\0';
  i = 1; 
  p = string;
  c = *p;
  len = 0; 
  while (c)
    {
      if (c == ' ' || c == '\t')
        {
        if (len)
          {           
           *(args + i) = (char *) xmalloc (sizeof(char) * (len + 1));
            strncpy (*(args + i), (p - len), len);
	     args[i][len] = '\0';
            ++i;
            len = 0;
          }
        }
      else
        ++len;
      c = *++p;
    }
  if (len)
     {
      *(args + i) = (char *) xmalloc (sizeof(char) * (len + 1));
      strncpy (*(args + i), (p - len), len);
	args[i][len] = '\0';
     }
}

/* free arguments string.  */
static void 
ici_free_arguments (unsigned int argc, char **argv)
{
  unsigned int i;
  for (i = 0; i < argc; ++i) 
    {    
      free (argv[i]); 
    }    
  free (argv);
 
}


/* load function specific option strings and save to function structures.  */
static void 
ici_load_function_specific_optimizations (void)
{
  static char *ici_function_spec_string;
  struct cgraph_node *node;
  struct function *old_cfun = cfun; /* Backup cfun.  */
  for (node = cgraph_nodes; node; node = node->next)
    {
      struct function *fun;
      tree fun_decl;

      fun = DECL_STRUCT_FUNCTION (node->decl);
      if (!fun)
        continue;
      set_cfun (fun);

      ici_function_spec_string = NULL;
      invoke_named_callbacks ("function_spec_loader", "function_spec_string",
			      EP_VOID, (void *) &ici_function_spec_string,
			      NULL);
      if (!ici_function_spec_string)
        continue;

      fun_decl = fun->decl;
      if (TREE_CODE (fun_decl) == FUNCTION_DECL)
        {
          unsigned int argc;
          char **argv;
          struct cl_optimization old_global_opts;
          tree old_function_opts;
	   int saved_flag_strict_aliasing;
	   int saved_flag_pcc_struct_return, 
	        saved_flag_omit_frame_pointer,
		 saved_flag_asynchronous_unwind_tables;

          /* Save old global and function-specific optimizations.  */
          cl_optimization_save (&old_global_opts);

          /* Store function-specific optimizations to global.  */
          old_function_opts
            =  DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fun_decl);
          if (old_function_opts)
            cl_optimization_restore (TREE_OPTIMIZATION (old_function_opts));

          /* Parse options string.  */
          ici_separate_arguments (ici_function_spec_string, &argc, &argv);

          /* Save flags which should not be changed.  */

          /* Change global optimizations with loaded 
             function specific string  */
           saved_flag_strict_aliasing = flag_strict_aliasing;
	    saved_flag_omit_frame_pointer = flag_omit_frame_pointer;
	    saved_flag_pcc_struct_return = flag_pcc_struct_return;
	    saved_flag_asynchronous_unwind_tables = flag_asynchronous_unwind_tables;
           decode_options (argc, (const char **) argv);
		   
	    flag_strict_aliasing = saved_flag_strict_aliasing;
           flag_omit_frame_pointer = saved_flag_omit_frame_pointer;
   	    flag_asynchronous_unwind_tables = saved_flag_asynchronous_unwind_tables;
     	    flag_pcc_struct_return = saved_flag_pcc_struct_return;
			
           ici_free_arguments (argc, argv);
	    argv = NULL;

          /* Restore saved flags.  */

          /* Store global optimizations to function.  */
          DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fun_decl)
            = build_optimization_node ();

          /* Restore old global optmizations.  */
          cl_optimization_restore (&old_global_opts);
        }

      free (ici_function_spec_string);
    }
  /* Restore cfun  */
  set_cfun (old_cfun);
}

/* ICI: Execute one IPA summary pass  */
void
execute_one_ipa_summary_pass (struct opt_pass *pass)
{
  gcc_assert (!current_function_decl);
  gcc_assert (!cfun);
  gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
  if (!quiet_flag && !cfun)
    fprintf (stderr, " <summary generate>");

  /* Generate summary as execute_ipa_summary_passes,
     but only for current pass.  */
  if (pass->type == IPA_PASS
      && (!pass->gate || pass->gate ()))
    {
      pass_init_dump_file (pass);
      ((struct ipa_opt_pass_d *)pass)->generate_summary ();
      pass_fini_dump_file (pass);
    }
}

int
invoke_named_callbacks (const char *name, ...)
{
  va_list va;
  int retval;
  int event = get_named_event_id (name, NO_INSERT);

  if (event < 0)
    return PLUGEVT_NO_SUCH_EVENT;
  va_start (va, name);
  retval = invoke_plugin_callbacks (event, &va);
  va_end (va);
  return retval;
}