forked from davidmalcolm/gcc-python-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-python-pass.c
715 lines (609 loc) · 19.2 KB
/
gcc-python-pass.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
Copyright 2011-2013, 2015 David Malcolm <[email protected]>
Copyright 2011-2013, 2015 Red Hat, Inc.
This 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 of the License, or
(at your option) any later version.
This program 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 this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include "gcc-python.h"
#include "gcc-python-wrappers.h"
#include "diagnostic.h"
#include "gcc-c-api/gcc-function.h"
#include "gcc-c-api/gcc-location.h"
#if (GCC_VERSION >= 4009)
#include "context.h"
#include "pass_manager.h"
#endif
/*
Wrapper for GCC's (opt_pass *)
*/
/*
Ensure we have a unique PyGccPass per pass address (by maintaining a dict)
For passes defined in Python, this dictionary maps from
long (struct opt_pass *) to the gcc.Pass wrapper object for that pass
The references on the right-hand-side keep these wrappers alive
*/
static PyObject *pass_wrapper_cache = NULL;
static bool impl_gate(function *fun)
{
PyObject *pass_obj;
PyObject *cfun_obj = NULL;
PyObject* result_obj;
int result;
gcc_location saved_loc = gcc_get_input_location();
/*
It appears that current_pass is not set by when gcc (4.7 at least) when
it invokes gate for an IPA_PASS within execute_ipa_summary_passes
(in gcc/passes.c), so we don't have a way of figuring out which pass
we were called on.
Reported as http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54959
*/
if (NULL == current_pass) {
return true;
}
assert(current_pass);
pass_obj = PyGccPass_New(current_pass);
assert(pass_obj); /* we own a ref at this point */
if (!PyObject_HasAttrString(pass_obj, "gate")) {
/* No "gate" method? Always execute this pass: */
Py_DECREF(pass_obj);
return true;
}
/* Supply the function, if any */
if (fun) {
assert (fun == cfun);
gcc_function cf = gcc_get_current_function();
/* Temporarily override input_location to the top of the function: */
gcc_set_input_location(gcc_function_get_start(cf));
cfun_obj = PyGccFunction_New(cf);
if (!cfun_obj) {
PyGcc_PrintException("Unhandled Python exception raised calling 'gate' method");
Py_DECREF(pass_obj);
gcc_set_input_location(saved_loc);
return false;
}
result_obj = PyObject_CallMethod(pass_obj, (char*)"gate", (char*)"O",
cfun_obj, NULL);
} else {
result_obj = PyObject_CallMethod(pass_obj, (char*)"gate", NULL);
}
Py_XDECREF(cfun_obj);
Py_DECREF(pass_obj);
if (!result_obj) {
PyGcc_PrintException("Unhandled Python exception raised calling 'gate' method");
gcc_set_input_location(saved_loc);
return false;
}
result = PyObject_IsTrue(result_obj);
Py_DECREF(result_obj);
gcc_set_input_location(saved_loc);
return result;
}
static unsigned int impl_execute(function *fun)
{
PyObject *pass_obj;
PyObject *cfun_obj = NULL;
PyObject* result_obj;
gcc_location saved_loc = gcc_get_input_location();
assert(current_pass);
pass_obj = PyGccPass_New(current_pass);
assert(pass_obj); /* we own a ref at this point */
if (fun) {
assert (fun == cfun);
gcc_function cf = gcc_get_current_function();
/* Temporarily override input_location to the top of the function: */
gcc_set_input_location(gcc_function_get_start(cf));
cfun_obj = PyGccFunction_New(cf);
if (!cfun_obj) {
PyGcc_PrintException("Unhandled Python exception raised calling 'execute' method");
Py_DECREF(pass_obj);
gcc_set_input_location(saved_loc);
return false;
}
result_obj = PyObject_CallMethod(pass_obj, (char*)"execute",
(char*)"O", cfun_obj, NULL);
} else {
result_obj = PyObject_CallMethod(pass_obj, (char*)"execute", NULL);
}
Py_XDECREF(cfun_obj);
Py_DECREF(pass_obj);
if (!result_obj) {
PyGcc_PrintException("Unhandled Python exception raised calling 'execute' method");
gcc_set_input_location(saved_loc);
return 0;
}
if (result_obj == Py_None) {
Py_DECREF(result_obj);
gcc_set_input_location(saved_loc);
return 0;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(result_obj)) {
long result = PyInt_AS_LONG(result_obj);
Py_DECREF(result_obj);
gcc_set_input_location(saved_loc);
return result;
}
#endif
if (PyLong_Check(result_obj)) {
long result = PyLong_AsLong(result_obj);
Py_DECREF(result_obj);
gcc_set_input_location(saved_loc);
return result;
}
PyErr_Format(PyExc_TypeError,
"execute returned a non-integer" \
"(type %.200s)",
Py_TYPE(result_obj)->tp_name);
Py_DECREF(result_obj);
PyGcc_PrintException("Unhandled Python exception raised calling 'execute' method");
gcc_set_input_location(saved_loc);
return 0;
}
#if (GCC_VERSION >= 4009)
/*
GCC 4.9 converted passes to a C++ class hierarchy, with methods for gate
and execute.
*/
#if (GCC_VERSION >= 5000)
/* GCC 5 added a "fun" param to the "gate" and "execute" vfuncs of
pass opt_pass. */
# define PASS_DECLARE_GATE_AND_EXECUTE \
bool gate (function *fun) { return impl_gate(fun); } \
unsigned int execute (function *fun) { return impl_execute(fun); }
#else
/* ...whereas in GCC 4.9 they took no params, with cfun being implied. */
# define PASS_DECLARE_GATE_AND_EXECUTE \
bool gate () { return impl_gate(cfun); } \
unsigned int execute () { return impl_execute(cfun); }
#endif /* #if (GCC_VERSION >= 5000) */
class PyGccGimplePass : public gimple_opt_pass
{
public:
PyGccGimplePass(const pass_data& data, gcc::context *ctxt) :
gimple_opt_pass(data, ctxt)
{
}
PASS_DECLARE_GATE_AND_EXECUTE
opt_pass *clone() {return this; }
};
class PyGccRtlPass : public rtl_opt_pass
{
public:
PyGccRtlPass (const pass_data& data, gcc::context *ctxt)
: rtl_opt_pass (data, ctxt)
{
}
PASS_DECLARE_GATE_AND_EXECUTE
opt_pass *clone() {return this; }
};
class PyGccIpaPass : public ipa_opt_pass_d
{
public:
PyGccIpaPass(const pass_data& data, gcc::context *ctxt)
: ipa_opt_pass_d (data, ctxt,
NULL, /* void (*generate_summary) (void), */
NULL, /* void (*write_summary) (void), */
NULL, /* void (*read_summary) (void), */
NULL, /* void (*write_optimization_summary) (void), */
NULL, /* void (*read_optimization_summary) (void), */
NULL, /* void (*stmt_fixup) (struct cgraph_node *, gimple *), */
0, /* unsigned int function_transform_todo_flags_start, */
NULL, /* unsigned int (*function_transform) (struct cgraph_node *), */
NULL) /* void (*variable_transform) (struct varpool_node *)) */
{
}
PASS_DECLARE_GATE_AND_EXECUTE
opt_pass *clone() {return this; }
};
class PyGccSimpleIpaPass : public simple_ipa_opt_pass
{
public:
PyGccSimpleIpaPass(const pass_data& data, gcc::context *ctxt)
: simple_ipa_opt_pass(data, ctxt)
{
}
PASS_DECLARE_GATE_AND_EXECUTE
opt_pass *clone() {return this; }
};
#else /* #if (GCC_VERSION >= 4009) */
/*
Before GCC 4.9, passes were implemented using callback functions.
*/
static bool gate_cb(void)
{
return impl_gate(cfun);
}
static unsigned int execute_cb(void)
{
return impl_execute(cfun);
}
#endif /* #else clause of if (GCC_VERSION >= 4009) */
static int
do_pass_init(PyObject *s, PyObject *args, PyObject *kwargs,
enum opt_pass_type pass_type,
size_t sizeof_pass)
{
struct PyGccPass *self = (struct PyGccPass *)s;
const char *name;
const char *keywords[] = {"name",
NULL};
struct opt_pass *pass;
/*
We need to call _track manually as we're not using PyGccWrapper_New():
*/
PyGccWrapper_Track(&self->head);
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"s:gcc.Pass.__init__", (char**)keywords,
&name)) {
return -1;
}
#if (GCC_VERSION >= 4009)
pass_data pass_data;
memset(&pass_data, 0, sizeof(pass_data));
pass_data.type = pass_type;
pass_data.name = PyGcc_strdup(name);
#if (GCC_VERSION < 5000)
pass_data.has_gate = true;
pass_data.has_execute = true;
#endif
switch (pass_type) {
case GIMPLE_PASS:
pass = new PyGccGimplePass (pass_data, g);
break;
case RTL_PASS:
pass = new PyGccRtlPass (pass_data, g);
break;
case SIMPLE_IPA_PASS:
pass = new PyGccSimpleIpaPass (pass_data, g);
break;
case IPA_PASS:
pass = new PyGccIpaPass (pass_data, g);
break;
default:
gcc_unreachable();
}
#else /* #if (GCC_VERSION >= 4009) */
pass = (struct opt_pass*)PyMem_Malloc(sizeof_pass);
if (!pass) {
return -1;
}
memset(pass, 0, sizeof_pass);
pass->type = pass_type;
pass->name = PyGcc_strdup(name);
/* does the name need to be unique?
mapping from opt_pass ptr to callable?
(the ID (as a long) is unique)
*/
if (!pass->name) {
PyMem_Free(pass);
return -1;
}
pass->gate = gate_cb;
pass->execute = execute_cb;
#endif /* ending the #else clause of #if (GCC_VERSION >= 4009) */
if (PyGcc_insert_new_wrapper_into_cache(&pass_wrapper_cache,
pass,
s)) {
return -1;
}
self->pass = pass;
return 0; // FIXME
}
int
PyGccGimplePass_init(PyObject *self, PyObject *args, PyObject *kwds)
{
return do_pass_init(self, args, kwds,
GIMPLE_PASS,
sizeof(struct gimple_opt_pass));
}
int
PyGccRtlPass_init(PyObject *self, PyObject *args, PyObject *kwds)
{
return do_pass_init(self, args, kwds,
RTL_PASS,
sizeof(struct rtl_opt_pass));
}
int
PyGccSimpleIpaPass_init(PyObject *self, PyObject *args, PyObject *kwds)
{
return do_pass_init(self, args, kwds,
SIMPLE_IPA_PASS,
sizeof(struct simple_ipa_opt_pass));
}
int
PyGccIpaPass_init(PyObject *self, PyObject *args, PyObject *kwds)
{
return do_pass_init(self, args, kwds,
IPA_PASS,
sizeof(struct ipa_opt_pass_d));
}
PyObject *
PyGccPass_repr(struct PyGccPass *self)
{
return PyGccString_FromFormat("%s(name='%s')",
Py_TYPE(self)->tp_name,
self->pass->name);
}
#if (GCC_VERSION >= 4009)
static struct dump_file_info *
get_dump_file_info(int phase)
{
gcc::dump_manager *dumps = g->get_dumps ();
return dumps->get_dump_file_info (phase);
}
#endif
/* In GCC 4.8, dump_enabled_p changed from acting on one pass to the
current phase (as of r192773), and dump_phase_enabled_p() was added (as
of r192692).
Unfortunately, dump_phase_enabled_p() is static within gcc/dumpfile.c,
so we use a copy of the 2 lines of relevant code:
*/
static bool
is_dump_enabled(struct opt_pass *pass)
{
#if (GCC_VERSION >= 4008)
struct dump_file_info *dfi = get_dump_file_info(pass->static_pass_number);
return dfi->pstate || dfi->alt_state;
#else
return dump_enabled_p(pass->static_pass_number);
#endif
}
PyObject *
PyGccPass_get_dump_enabled(struct PyGccPass *self, void *closure)
{
return PyBool_FromLong(is_dump_enabled(self->pass));
}
/* In GCC 4.8, this field became "pstate" */
#if (GCC_VERSION >= 4008)
#define DFI_STATE(dfi) (dfi)->pstate
#else
#define DFI_STATE(dfi) (dfi)->state
#endif
int
PyGccPass_set_dump_enabled(struct PyGccPass *self, PyObject *value, void *closure)
{
struct dump_file_info *dfi = get_dump_file_info (self->pass->static_pass_number);
assert(dfi);
int newbool = PyObject_IsTrue(value);
if (newbool == -1) {
return -1;
}
if (DFI_STATE (dfi) == 0) {
/* Dumping was disabled: */
if (newbool) {
/* Enabling: */
DFI_STATE (dfi) = -1;
return 0;
} else {
/* No change: */
return 0;
}
} else {
if (DFI_STATE (dfi) < 0) {
/* Dumping was enabled but has not yet started */
if (newbool) {
/* No change: */
return 0;
} else {
/* Disabling: */
DFI_STATE (dfi) = 0;
return 0;
}
} else {
assert(DFI_STATE (dfi) > 0);
/* Dumping was enabled and has already started */
if (newbool) {
/* No change: */
return 0;
} else {
/* Can't disable after it's started: */
PyErr_SetString(PyExc_RuntimeError,
"Can't disable dumping: already started");
return -1;
}
}
}
}
/* In GCC 4.9, passes moved from being globals to fields of the
pass_manager. */
#if (GCC_VERSION >= 4009)
#define GET_PASS_LIST(PASS_NAME) (g->get_passes()->PASS_NAME)
#else
#define GET_PASS_LIST(PASS_NAME) (PASS_NAME)
#endif
PyObject *
PyGccPass_get_roots(PyObject *cls, PyObject *noargs)
{
/*
There are 5 "roots" for the pass tree; see gcc/passes.c
*/
PyObject *result;
PyObject *passobj;
result = PyTuple_New(5);
if (!result) {
goto error;
}
#define SET_PASS(IDX, PASS_NAME) \
passobj = PyGccPass_New(GET_PASS_LIST(PASS_NAME)); \
if (!passobj) goto error; \
PyTuple_SET_ITEM(result, IDX, passobj); \
(void)0;
SET_PASS(0, all_lowering_passes);
SET_PASS(1, all_small_ipa_passes);
SET_PASS(2, all_regular_ipa_passes);
/* all_late_ipa_passes appeared in r175336 */
/* r204984 eliminated all_lto_gen_passes */
#if (GCC_VERSION >= 4009)
SET_PASS(3, all_late_ipa_passes);
#else
SET_PASS(3, all_lto_gen_passes);
#endif
SET_PASS(4, all_passes);
return result;
error:
Py_XDECREF(result);
return NULL;
}
static struct opt_pass *
find_pass_by_name(const char *name, struct opt_pass *pass_list)
{
struct opt_pass *pass;
for (pass = pass_list; pass; pass = pass->next)
{
if (pass->name && !strcmp (name, pass->name)) {
/* Found: */
return pass;
}
if (pass->sub) {
/* Recurse: */
struct opt_pass *result = find_pass_by_name(name, pass->sub);
if (result) {
return result;
}
}
}
/* Not found: */
return NULL;
}
PyObject *
PyGccPass_get_by_name(PyObject *cls, PyObject *args, PyObject *kwargs)
{
const char *name;
const char *keywords[] = {"name",
NULL};
struct opt_pass *result;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"s:get_by_name", (char**)keywords,
&name)) {
return NULL;
}
#define SEARCH_WITHIN_LIST(PASS_LIST) \
result = find_pass_by_name(name, (GET_PASS_LIST(PASS_LIST))); \
if (result) { \
return PyGccPass_New(result); \
}
SEARCH_WITHIN_LIST(all_lowering_passes);
SEARCH_WITHIN_LIST(all_small_ipa_passes);
SEARCH_WITHIN_LIST(all_regular_ipa_passes);
/* all_late_ipa_passes appeared in r175336 */
/* r204984 eliminated all_lto_gen_passes */
#if (GCC_VERSION >= 4009)
SEARCH_WITHIN_LIST(all_late_ipa_passes);
#else
SEARCH_WITHIN_LIST(all_lto_gen_passes);
#endif
SEARCH_WITHIN_LIST(all_passes);
/* Not found: */
PyErr_Format(PyExc_ValueError, "pass named '%s' not found", name);
return NULL;
}
static PyObject *
impl_register(struct PyGccPass *self, PyObject *args, PyObject *kwargs,
enum pass_positioning_ops pos_op, const char *arg_format)
{
struct register_pass_info rpi;
const char *keywords[] = {"name",
"instance_number",
NULL};
rpi.pass = self->pass;
rpi.pos_op = pos_op;
rpi.ref_pass_instance_number = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
arg_format, (char**)keywords,
&rpi.reference_pass_name,
&rpi.ref_pass_instance_number)) {
return NULL;
}
/* (Failures lead to a fatal error) */
register_pass (&rpi);
Py_RETURN_NONE;
}
PyObject *
PyGccPass_register_before(struct PyGccPass *self, PyObject *args, PyObject *kwargs)
{
return impl_register(self, args, kwargs,
PASS_POS_INSERT_BEFORE,
"s|i:register_before");
}
PyObject *
PyGccPass_register_after(struct PyGccPass *self, PyObject *args, PyObject *kwargs)
{
return impl_register(self, args, kwargs,
PASS_POS_INSERT_AFTER,
"s|i:register_after");
}
PyObject *
PyGccPass_replace(struct PyGccPass *self, PyObject *args, PyObject *kwargs)
{
return impl_register(self, args, kwargs,
PASS_POS_REPLACE,
"s|i:replace");
}
static PyGccWrapperTypeObject *
get_type_for_pass_type(enum opt_pass_type pt)
{
switch (pt) {
default: assert(0);
case GIMPLE_PASS:
return &PyGccGimplePass_TypeObj;
case RTL_PASS:
return &PyGccRtlPass_TypeObj;
case SIMPLE_IPA_PASS:
return &PyGccSimpleIpaPass_TypeObj;
case IPA_PASS:
return &PyGccIpaPass_TypeObj;
}
};
static PyObject *
real_make_pass_wrapper(void *p)
{
struct opt_pass *pass = (struct opt_pass *)p;
PyGccWrapperTypeObject *type_obj;
struct PyGccPass *pass_obj = NULL;
if (NULL == pass) {
Py_RETURN_NONE;
}
type_obj = get_type_for_pass_type(pass->type);
pass_obj = PyGccWrapper_New(struct PyGccPass, type_obj);
if (!pass_obj) {
goto error;
}
pass_obj->pass = pass;
/* FIXME: do we need to do something for the GCC GC? */
return (PyObject*)pass_obj;
error:
return NULL;
}
void
PyGcc_WrtpMarkForPyGccPass(PyGccPass *wrapper)
{
/*
This function is empty: struct opt_pass does not have a GTY()
and any (struct opt_pass*) is either statically-allocated, or
allocated by us within do_pass_init using PyMem_Malloc
*/
}
PyObject *
PyGccPass_New(struct opt_pass *pass)
{
return PyGcc_LazilyCreateWrapper(&pass_wrapper_cache,
pass,
real_make_pass_wrapper);
}
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/