Skip to content

Commit 7608fa8

Browse files
authored
gh-106149: move _PyCfg_BasicblockLastInstr and make it local to flowgraph.c (#107180)
1 parent ac2d85a commit 7608fa8

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

Diff for: Include/internal/pycore_flowgraph.h

-13
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,12 @@ int _PyCfgBuilder_Addop(_PyCfgBuilder *g, int opcode, int oparg, _PyCompilerSrcL
8888
int _PyCfgBuilder_Init(_PyCfgBuilder *g);
8989
void _PyCfgBuilder_Fini(_PyCfgBuilder *g);
9090

91-
_PyCfgInstruction* _PyCfg_BasicblockLastInstr(const _PyCfgBasicblock *b);
9291
int _PyCfg_OptimizeCodeUnit(_PyCfgBuilder *g, PyObject *consts, PyObject *const_cache,
9392
int code_flags, int nlocals, int nparams, int firstlineno);
9493
int _PyCfg_Stackdepth(_PyCfgBasicblock *entryblock, int code_flags);
9594
void _PyCfg_ConvertPseudoOps(_PyCfgBasicblock *entryblock);
9695
int _PyCfg_ResolveJumps(_PyCfgBuilder *g);
9796

98-
99-
static inline int
100-
basicblock_nofallthrough(const _PyCfgBasicblock *b) {
101-
_PyCfgInstruction *last = _PyCfg_BasicblockLastInstr(b);
102-
return (last &&
103-
(IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
104-
IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
105-
}
106-
107-
#define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
108-
#define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
109-
11097
PyCodeObject *
11198
_PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *u, PyObject *const_cache,
11299
PyObject *consts, int maxdepth, _PyCompile_InstructionSequence *instrs,

Diff for: Python/flowgraph.c

+34-23
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ basicblock_append_instructions(basicblock *target, basicblock *source)
137137
return SUCCESS;
138138
}
139139

140+
static cfg_instr *
141+
basicblock_last_instr(const basicblock *b) {
142+
assert(b->b_iused >= 0);
143+
if (b->b_iused > 0) {
144+
assert(b->b_instr != NULL);
145+
return &b->b_instr[b->b_iused - 1];
146+
}
147+
return NULL;
148+
}
149+
150+
static inline int
151+
basicblock_nofallthrough(const _PyCfgBasicblock *b) {
152+
_PyCfgInstruction *last = basicblock_last_instr(b);
153+
return (last &&
154+
(IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
155+
IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
156+
}
157+
158+
#define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
159+
#define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
160+
140161
static basicblock *
141162
copy_basicblock(cfg_builder *g, basicblock *block)
142163
{
@@ -186,7 +207,7 @@ dump_instr(cfg_instr *i)
186207

187208
static inline int
188209
basicblock_returns(const basicblock *b) {
189-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
210+
cfg_instr *last = basicblock_last_instr(b);
190211
return last && (last->i_opcode == RETURN_VALUE || last->i_opcode == RETURN_CONST);
191212
}
192213

@@ -228,26 +249,16 @@ cfg_builder_use_next_block(cfg_builder *g, basicblock *block)
228249
return block;
229250
}
230251

231-
cfg_instr *
232-
_PyCfg_BasicblockLastInstr(const basicblock *b) {
233-
assert(b->b_iused >= 0);
234-
if (b->b_iused > 0) {
235-
assert(b->b_instr != NULL);
236-
return &b->b_instr[b->b_iused - 1];
237-
}
238-
return NULL;
239-
}
240-
241252
static inline int
242253
basicblock_exits_scope(const basicblock *b) {
243-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
254+
cfg_instr *last = basicblock_last_instr(b);
244255
return last && IS_SCOPE_EXIT_OPCODE(last->i_opcode);
245256
}
246257

247258
static bool
248259
cfg_builder_current_block_is_terminated(cfg_builder *g)
249260
{
250-
cfg_instr *last = _PyCfg_BasicblockLastInstr(g->g_curblock);
261+
cfg_instr *last = basicblock_last_instr(g->g_curblock);
251262
if (last && IS_TERMINATOR_OPCODE(last->i_opcode)) {
252263
return true;
253264
}
@@ -371,7 +382,7 @@ no_empty_basic_blocks(cfg_builder *g) {
371382
static bool
372383
no_redundant_jumps(cfg_builder *g) {
373384
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
374-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
385+
cfg_instr *last = basicblock_last_instr(b);
375386
if (last != NULL) {
376387
if (IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
377388
assert(last->i_target != b->b_next);
@@ -390,7 +401,7 @@ no_redundant_jumps(cfg_builder *g) {
390401

391402
static int
392403
normalize_jumps_in_block(cfg_builder *g, basicblock *b) {
393-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
404+
cfg_instr *last = basicblock_last_instr(b);
394405
if (last == NULL || !is_jump(last) ||
395406
IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
396407
return SUCCESS;
@@ -953,7 +964,7 @@ remove_redundant_jumps(cfg_builder *g) {
953964
*/
954965
assert(no_empty_basic_blocks(g));
955966
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
956-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
967+
cfg_instr *last = basicblock_last_instr(b);
957968
assert(last != NULL);
958969
assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
959970
if (IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) {
@@ -979,7 +990,7 @@ remove_redundant_jumps(cfg_builder *g) {
979990
*/
980991
static int
981992
inline_small_exit_blocks(basicblock *bb) {
982-
cfg_instr *last = _PyCfg_BasicblockLastInstr(bb);
993+
cfg_instr *last = basicblock_last_instr(bb);
983994
if (last == NULL) {
984995
return 0;
985996
}
@@ -1715,7 +1726,7 @@ scan_block_for_locals(basicblock *b, basicblock ***sp)
17151726
if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
17161727
maybe_push(b->b_next, unsafe_mask, sp);
17171728
}
1718-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
1729+
cfg_instr *last = basicblock_last_instr(b);
17191730
if (last && is_jump(last)) {
17201731
assert(last->i_target != NULL);
17211732
maybe_push(last->i_target, unsafe_mask, sp);
@@ -2020,7 +2031,7 @@ push_cold_blocks_to_end(cfg_builder *g, int code_flags) {
20202031
b->b_next = explicit_jump;
20212032

20222033
/* set target */
2023-
cfg_instr *last = _PyCfg_BasicblockLastInstr(explicit_jump);
2034+
cfg_instr *last = basicblock_last_instr(explicit_jump);
20242035
last->i_target = explicit_jump->b_next;
20252036
}
20262037
}
@@ -2126,7 +2137,7 @@ duplicate_exits_without_lineno(cfg_builder *g)
21262137
*/
21272138
basicblock *entryblock = g->g_entryblock;
21282139
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
2129-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
2140+
cfg_instr *last = basicblock_last_instr(b);
21302141
assert(last != NULL);
21312142
if (is_jump(last)) {
21322143
basicblock *target = last->i_target;
@@ -2150,7 +2161,7 @@ duplicate_exits_without_lineno(cfg_builder *g)
21502161
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
21512162
if (BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_iused > 0) {
21522163
if (is_exit_without_lineno(b->b_next)) {
2153-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
2164+
cfg_instr *last = basicblock_last_instr(b);
21542165
assert(last != NULL);
21552166
b->b_next->b_instr[0].i_loc = last->i_loc;
21562167
}
@@ -2170,7 +2181,7 @@ duplicate_exits_without_lineno(cfg_builder *g)
21702181
static void
21712182
propagate_line_numbers(basicblock *entryblock) {
21722183
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
2173-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
2184+
cfg_instr *last = basicblock_last_instr(b);
21742185
if (last == NULL) {
21752186
continue;
21762187
}
@@ -2210,7 +2221,7 @@ guarantee_lineno_for_exits(basicblock *entryblock, int firstlineno) {
22102221
int lineno = firstlineno;
22112222
assert(lineno > 0);
22122223
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
2213-
cfg_instr *last = _PyCfg_BasicblockLastInstr(b);
2224+
cfg_instr *last = basicblock_last_instr(b);
22142225
if (last == NULL) {
22152226
continue;
22162227
}

0 commit comments

Comments
 (0)