forked from davidmalcolm/gcc-python-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcc-cfg.c
344 lines (287 loc) · 7.64 KB
/
gcc-cfg.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
/*
Copyright 2012, 2013, 2015 David Malcolm <dmalcolm@redhat.com>
Copyright 2012, 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 "gcc-cfg.h"
#include "tree.h"
#include "function.h"
#include "basic-block.h"
#if (GCC_VERSION >= 4009)
#include "tree-ssa-alias.h" /* needed by gimple.h in 4.9 */
#include "internal-fn.h" /* needed by gimple.h in 4.9 */
#include "is-a.h" /* needed by gimple.h in 4.9 */
#include "predict.h" /* needed by gimple.h in 4.9 */
#include "gimple-expr.h" /* needed by gimple.h in 4.9 */
#endif
#include "gimple.h"
/* gcc 4.9 moved gimple_stmt_iterator into this header */
#if (GCC_VERSION >= 4009)
#include "gimple-iterator.h"
#endif
/* gcc 10 removed this header */
#if (GCC_VERSION < 10000)
#include "params.h"
#endif
#include "tree.h"
#include "diagnostic.h"
#include "cgraph.h"
#include "opts.h"
#include "rtl.h"
#include "gcc-private-compat.h"
/***********************************************************
gcc_cfg
************************************************************/
GCC_IMPLEMENT_PRIVATE_API (struct gcc_cfg)
gcc_private_make_cfg (struct control_flow_graph *inner)
{
struct gcc_cfg result;
result.inner = inner;
return result;
}
GCC_IMPLEMENT_PUBLIC_API (void) gcc_cfg_mark_in_use (gcc_cfg cfg)
{
gt_ggc_mx_control_flow_graph (cfg.inner);
}
GCC_IMPLEMENT_PUBLIC_API (gcc_cfg_block) gcc_cfg_get_entry (gcc_cfg cfg)
{
return gcc_private_make_cfg_block (cfg.inner->x_entry_block_ptr);
}
GCC_IMPLEMENT_PUBLIC_API (gcc_cfg_block) gcc_cfg_get_exit (gcc_cfg cfg)
{
return gcc_private_make_cfg_block (cfg.inner->x_exit_block_ptr);
}
GCC_IMPLEMENT_PUBLIC_API (bool)
gcc_cfg_for_each_block (gcc_cfg cfg,
bool (*cb) (gcc_cfg_block block, void *user_data),
void *user_data)
{
int i;
for (i = 0; i < cfg.inner->x_n_basic_blocks; i++)
{
basic_block bb = GCC_COMPAT_VEC_INDEX (basic_block,
cfg.inner->x_basic_block_info,
i);
if (cb (gcc_private_make_cfg_block (bb), user_data))
{
return true;
}
}
return false;
}
/***********************************************************
gcc_cfg_block
************************************************************/
GCC_IMPLEMENT_PRIVATE_API (struct gcc_cfg_block)
gcc_private_make_cfg_block (basic_block inner)
{
struct gcc_cfg_block result;
result.inner = inner;
return result;
}
GCC_IMPLEMENT_PUBLIC_API (void)
gcc_cfg_block_mark_in_use (gcc_cfg_block block)
{
gt_ggc_mx_basic_block_def (block.inner);
}
GCC_IMPLEMENT_PUBLIC_API (int)
gcc_cfg_block_get_index (gcc_cfg_block block)
{
return block.inner->index;
}
static bool
for_each_edge (
#if (GCC_VERSION >= 4008)
vec<edge, va_gc> *vec_edges,
#else
VEC (edge, gc) * vec_edges,
#endif
bool (*cb) (gcc_cfg_edge edge, void *user_data),
void *user_data)
{
int i;
edge e;
GCC_COMPAT_FOR_EACH_VEC_ELT (edge, vec_edges, i, e)
{
if (cb (gcc_private_make_cfg_edge (e), user_data))
{
return true;
}
}
return false;
}
GCC_IMPLEMENT_PUBLIC_API (bool)
gcc_cfg_block_for_each_pred_edge (gcc_cfg_block block,
bool (*cb) (gcc_cfg_edge edge,
void *user_data),
void *user_data)
{
return for_each_edge (block.inner->preds, cb, user_data);
}
GCC_IMPLEMENT_PUBLIC_API (bool)
gcc_cfg_block_for_each_succ_edge (gcc_cfg_block block,
bool (*cb) (gcc_cfg_edge edge,
void *user_data),
void *user_data)
{
return for_each_edge (block.inner->succs, cb, user_data);
}
/* In GCC 4.7, struct basic_block_def had a
struct gimple_bb_info * gimple;
within its il union.
In GCC 4.8, this became:
struct gimple_bb_info gimple
i.e. it is no longer dereferenced
*/
static struct gimple_bb_info *
checked_get_gimple_info(gcc_cfg_block block)
{
if (block.inner->flags & BB_RTL)
{
return NULL;
}
#if (GCC_VERSION >= 4008)
return &block.inner->il.gimple;
#else
return block.inner->il.gimple;
#endif
}
GCC_IMPLEMENT_PUBLIC_API (bool)
gcc_cfg_block_for_each_gimple_phi (gcc_cfg_block block,
bool (*cb) (gcc_gimple_phi phi,
void *user_data),
void *user_data)
{
gimple_stmt_iterator gsi;
struct gimple_bb_info *info;
info = checked_get_gimple_info(block);
if (NULL == info)
{
return false;
}
for (gsi = gsi_start (info->phi_nodes);
!gsi_end_p (gsi); gsi_next (&gsi))
{
gimple_stmt_ptr stmt = gsi_stmt (gsi);
if (cb (gcc_private_make_gimple_phi (stmt), user_data))
{
return true;
}
}
return false;
}
GCC_IMPLEMENT_PUBLIC_API (bool)
gcc_cfg_block_for_each_gimple (gcc_cfg_block block,
bool (*cb) (gcc_gimple stmt, void *user_data),
void *user_data)
{
gimple_stmt_iterator gsi;
struct gimple_bb_info *info;
info = checked_get_gimple_info(block);
if (NULL == info)
{
return false;
}
for (gsi = gsi_start (info->seq);
!gsi_end_p (gsi); gsi_next (&gsi))
{
gimple_stmt_ptr stmt = gsi_stmt (gsi);
if (cb (gcc_private_make_gimple (stmt), user_data))
{
return true;
}
}
return false;
}
GCC_IMPLEMENT_PUBLIC_API (bool)
gcc_cfg_block_for_each_rtl_insn (gcc_cfg_block block,
bool (*cb) (gcc_rtl_insn insn,
void *user_data),
void *user_data)
{
#if (GCC_VERSION >= 5000)
rtx_insn *insn;
#else
rtx insn;
#endif
if (!(block.inner->flags & BB_RTL))
{
return false;
}
FOR_BB_INSNS (block.inner, insn)
{
if (cb (gcc_private_make_rtl_insn (insn), user_data))
{
return true;
}
}
return false;
}
/***********************************************************
gcc_cfg_edge
************************************************************/
GCC_IMPLEMENT_PRIVATE_API (struct gcc_cfg_edge)
gcc_private_make_cfg_edge (edge inner)
{
struct gcc_cfg_edge result;
result.inner = inner;
return result;
}
GCC_IMPLEMENT_PUBLIC_API (void) gcc_cfg_edge_mark_in_use (gcc_cfg_edge edge)
{
gt_ggc_mx_edge_def (edge.inner);
}
GCC_IMPLEMENT_PUBLIC_API (gcc_cfg_block)
gcc_cfg_edge_get_src (gcc_cfg_edge edge)
{
return gcc_private_make_cfg_block (edge.inner->src);
}
GCC_IMPLEMENT_PUBLIC_API (gcc_cfg_block)
gcc_cfg_edge_get_dest (gcc_cfg_edge edge)
{
return gcc_private_make_cfg_block (edge.inner->dest);
}
GCC_PUBLIC_API (bool) gcc_cfg_edge_is_true_value (gcc_cfg_edge edge)
{
return (edge.inner->flags & EDGE_TRUE_VALUE) == EDGE_TRUE_VALUE;
}
GCC_PUBLIC_API (bool) gcc_cfg_edge_is_false_value (gcc_cfg_edge edge)
{
return (edge.inner->flags & EDGE_FALSE_VALUE) == EDGE_FALSE_VALUE;
}
GCC_PUBLIC_API(bool)
gcc_cfg_edge_is_loop_exit(gcc_cfg_edge edge)
{
return (edge.inner->flags & EDGE_LOOP_EXIT) == EDGE_LOOP_EXIT;
}
GCC_PUBLIC_API(bool)
gcc_cfg_edge_get_can_fallthru(gcc_cfg_edge edge)
{
return (edge.inner->flags & EDGE_CAN_FALLTHRU) == EDGE_CAN_FALLTHRU;
}
GCC_PUBLIC_API(bool)
gcc_cfg_edge_is_complex(gcc_cfg_edge edge)
{
return (edge.inner->flags & EDGE_COMPLEX);
}
GCC_PUBLIC_API(bool)
gcc_cfg_edge_is_eh(gcc_cfg_edge edge)
{
return (edge.inner->flags & EDGE_EH) == EDGE_EH;
}
/*
Local variables:
c-basic-offset: 2
indent-tabs-mode: nil
End:
*/