mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
gcc-callbacks.cpp
Go to the documentation of this file.
1 /* MAGEEC GCC Plugin Callbacks
2  Copyright (C) 2013, 2014 Embecosm Limited and University of Bristol
3 
4  This file is part of MAGEEC.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 
20 /* We need to undefine these as gcc-plugin.h redefines them */
21 #undef PACKAGE_BUGREPORT
22 #undef PACKAGE_NAME
23 #undef PACKAGE_STRING
24 #undef PACKAGE_TARNAME
25 #undef PACKAGE_VERSION
26 
27 #include "gcc-plugin.h"
28 #include "tree-pass.h"
29 #include "function.h"
30 #include "mageec-plugin.h"
31 #include "mageec/mageec.h"
32 #include <string>
33 #include <stdio.h>
34 
35 void mageec_gcc_finish (void *gcc_data __attribute__((unused)),
36  void *user_data __attribute__((unused)))
37 {
38  fprintf (stderr, "GCC: Finish\n");
39  mageec_inst.finish();
40 }
41 
42 void dummy_callback (void *gcc_data, void *user_data)
43 {
44  printf ("Dummy Called\n");
45  printf (" gcc_data: %p\n", gcc_data);
46  printf (" user_data: %p\n", user_data);
47 }
48 
49 void mageec_gcc_start_file (void *gcc_data, void *user_data)
50 {
51  mageec_inst.new_file ("example.c");
52  fprintf (stderr, "GCC: Start File\n");
53  fprintf (stderr, " gcc_data: %p\n", gcc_data);
54  fprintf (stderr, " user_data: %p\n", user_data);
55 }
56 
57 void mageec_gcc_finish_file (void *gcc_data, void *user_data)
58 {
59  mageec_inst.end_file ();
60  fprintf (stderr, "GCC: End File\n");
61  fprintf (stderr, " gcc_data: %p\n", gcc_data);
62  fprintf (stderr, " user_data: %p\n", user_data);
63 }
64 
70 static std::string pass_type_str(opt_pass* pass)
71 {
72  if (pass == NULL)
73  return "*NULL*";
74  switch (pass->type)
75  {
76  case GIMPLE_PASS:
77  return "GIMPLE";
78  break;
79  case RTL_PASS:
80  return "RTL";
81  break;
82  case SIMPLE_IPA_PASS:
83  return "SIMPLE_IPA";
84  break;
85  case IPA_PASS:
86  return "IPA";
87  break;
88  default:
89  return "*UNKNOWN*";
90  break;
91  }
92  return "*UNKNOWN*";
93 }
94 
98 void mageec_pass_gate (void *gcc_data,
99  void *user_data __attribute__((unused)))
100 {
101  short *result = (short *)gcc_data;
102  if (mageec_config["print_pass_info"])
103  fprintf (stderr, "Pass: '%s', Type: %s, Function: '%s', Gate: %i\n",
104  current_pass->name, pass_type_str(current_pass).c_str(),
105  current_function_name(), *result);
106 
107  if (!mageec_config["no_decision"])
108  {
109  mageec::decision d = mageec_inst.make_decision(current_pass->name,
110  current_function_name());
111  bool mageec_changed = false;
112  switch (d) {
114  return;
116  if (*result == 0)
117  mageec_changed = true;
118  *result = (short)1;
119  break;
121  if (*result == 1)
122  mageec_changed = true;
123  *result = (short)0;
124  }
125  if (mageec_config["print_pass_info"] && mageec_changed)
126  fprintf (stderr, " New gate: %i\n", *result);
127  }
128 }