Skip to content

Commit 9ff6911

Browse files
committed
STORE_FAST_R instead of more STORE_FASTs. Fixed peepholer to not optimize away copies into locals (only tmps)
1 parent d5830af commit 9ff6911

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

Python/compile.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,13 @@ compiler_addop_o(struct compiler *c, location loc,
17331733
if (arg < 0) {
17341734
return ERROR;
17351735
}
1736-
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc);
1736+
if (c->c_regcode && opcode == STORE_FAST) {
1737+
return cfg_builder_addop(CFG_BUILDER(c), STORE_FAST_R, arg, loc,
1738+
NAME_OPARG(arg), UNUSED_OPARG, UNUSED_OPARG);
1739+
}
1740+
else {
1741+
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc);
1742+
}
17371743
}
17381744

17391745
static int
@@ -8683,9 +8689,10 @@ makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
86838689
return co;
86848690
}
86858691

8692+
#define VV 0
86868693

86878694
/* For debugging purposes only */
8688-
#if 0
8695+
#if VV
86898696
static void
86908697
dump_instr(struct instr *i)
86918698
{
@@ -10054,6 +10061,7 @@ translate_jump_labels_to_targets(basicblock *entryblock)
1005410061
}
1005510062

1005610063

10064+
1005710065
static int
1005810066
reduce_traffic_between_registers_and_stack(basicblock *b)
1005910067
{
@@ -10065,8 +10073,10 @@ reduce_traffic_between_registers_and_stack(basicblock *b)
1006510073
}
1006610074
}
1006710075
bool changed = true;
10076+
if (VV) fprintf(stderr, "-----------------------------\n");
1006810077
while (changed) {
1006910078
changed = false;
10079+
if (VV) dump_basicblock(b);
1007010080
for (int i = 0; i < b->b_iused - 1; i++) {
1007110081
struct instr *instr = &b->b_instr[i];
1007210082
if (instr->i_opcode == LOAD_CONST_R || instr->i_opcode == LOAD_FAST_R) {
@@ -10079,7 +10089,7 @@ reduce_traffic_between_registers_and_stack(basicblock *b)
1007910089
if (next_i < b->b_iused && b->b_instr[next_i].i_opcode == STORE_FAST_R) {
1008010090
struct instr *next = &b->b_instr[next_i];
1008110091
INSTR_SET_OP2(next, COPY_R, 0, instr->i_oparg1, next->i_oparg1);
10082-
if (instr->i_opcode == LOAD_CONST_R) {
10092+
if (instr->i_opcode == LOAD_CONST_R || instr->i_oparg1.type == TMP_REG) {
1008310093
INSTR_SET_OP0(instr, NOP);
1008410094
}
1008510095
else {
@@ -10089,8 +10099,22 @@ reduce_traffic_between_registers_and_stack(basicblock *b)
1008910099
changed = true;
1009010100
}
1009110101
}
10102+
else if (instr->i_opcode == STORE_FAST_R && instr->i_oparg1.type == TMP_REG) {
10103+
int next_i = i + 1;
10104+
while (next_i < b->b_iused && b->b_instr[next_i].i_opcode == NOP) {
10105+
next_i++;
10106+
}
10107+
if (next_i < b->b_iused && b->b_instr[next_i].i_opcode == LOAD_FAST_R) {
10108+
struct instr *next = &b->b_instr[next_i];
10109+
if (SAME_REGISTER(instr->i_oparg1, next->i_oparg1)) {
10110+
INSTR_SET_OP0(instr, NOP);
10111+
INSTR_SET_OP0(next, NOP);
10112+
}
10113+
}
10114+
}
1009210115
}
1009310116
}
10117+
if (VV) dump_basicblock(b);
1009410118
return 0;
1009510119
}
1009610120

@@ -10106,7 +10130,13 @@ propagate_register_copies(basicblock *b)
1010610130
struct instr *next = &b->b_instr[j];
1010710131
/* These should all be reads, because SSA */
1010810132
if (SAME_REGISTER(next->i_oparg1, dst)) {
10109-
next->i_oparg1 = src;
10133+
if (next->i_opcode == CHECK_FAST_R && src.type == TMP_REG) {
10134+
INSTR_SET_OP0(next, NOP); /* no need to check tmps */
10135+
}
10136+
else if (next->i_opcode != LOAD_FAST_R &&
10137+
next->i_opcode != LOAD_CONST_R) {
10138+
next->i_oparg1 = src;
10139+
}
1011010140
}
1011110141
if (SAME_REGISTER(next->i_oparg2, dst)) {
1011210142
next->i_oparg2 = src;
@@ -10115,9 +10145,16 @@ propagate_register_copies(basicblock *b)
1011510145
next->i_oparg3 = src;
1011610146
}
1011710147
}
10118-
INSTR_SET_OP0(instr, NOP);
10148+
if (dst.type == TMP_REG) {
10149+
INSTR_SET_OP0(instr, NOP);
10150+
}
1011910151
}
1012010152
}
10153+
10154+
if (VV) {
10155+
fprintf(stderr, "after propagate_register_copies:\n");
10156+
dump_basicblock(b);
10157+
}
1012110158
return 0;
1012210159
}
1012310160

0 commit comments

Comments
 (0)