# HG changeset patch # User Antoine Pitrou # Date 1229909251 -3600 diff -r 56747b9abe2d -r 537340dbaffa Include/opcode.h --- a/Include/opcode.h +++ b/Include/opcode.h @@ -96,9 +96,11 @@ extern "C" { #define IMPORT_FROM 109 /* Index in name list */ #define JUMP_FORWARD 110 /* Number of bytes to skip */ -#define JUMP_IF_FALSE 111 /* "" */ -#define JUMP_IF_TRUE 112 /* "" */ +#define POP_OR_JUMP 111 /* "" */ +#define JUMP_OR_POP 112 /* "" */ #define JUMP_ABSOLUTE 113 /* Target byte offset from beginning of code */ +#define POP_JUMP_IF_FALSE 114 /* Number of bytes to skip */ +#define POP_JUMP_IF_TRUE 115 /* "" */ #define LOAD_GLOBAL 116 /* Index in name list */ diff -r 56747b9abe2d -r 537340dbaffa Lib/opcode.py --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -131,9 +131,11 @@ name_op('IMPORT_NAME', 108) # Index name_op('IMPORT_FROM', 109) # Index in name list jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip -jrel_op('JUMP_IF_FALSE', 111) # "" -jrel_op('JUMP_IF_TRUE', 112) # "" -jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code +jabs_op('POP_OR_JUMP', 111) # Target byte offset from beginning of code +jabs_op('JUMP_OR_POP', 112) # "" +jabs_op('JUMP_ABSOLUTE', 113) # "" +jabs_op('POP_JUMP_IF_FALSE', 114) # "" +jabs_op('POP_JUMP_IF_TRUE', 115) # "" name_op('LOAD_GLOBAL', 116) # Index in name list diff -r 56747b9abe2d -r 537340dbaffa Python/ceval.c --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1965,8 +1965,54 @@ PyEval_EvalFrameEx(PyFrameObject *f, int Py_DECREF(w); SET_TOP(x); if (x == NULL) break; - PREDICT(JUMP_IF_FALSE); - PREDICT(JUMP_IF_TRUE); + PREDICT(POP_JUMP_IF_FALSE); + PREDICT(POP_JUMP_IF_TRUE); + continue; + + PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE); + case POP_JUMP_IF_FALSE: + w = POP(); + if (w == Py_True) { + Py_DECREF(w); + goto fast_next_opcode; + } + if (w == Py_False) { + Py_DECREF(w); + JUMPTO(oparg); + goto fast_next_opcode; + } + err = PyObject_IsTrue(w); + Py_DECREF(w); + if (err > 0) + err = 0; + else if (err == 0) + JUMPTO(oparg); + else + break; + continue; + + PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE); + case POP_JUMP_IF_TRUE: + w = POP(); + if (w == Py_False) { + Py_DECREF(w); + goto fast_next_opcode; + } + if (w == Py_True) { + Py_DECREF(w); + JUMPTO(oparg); + goto fast_next_opcode; + } + err = PyObject_IsTrue(w); + Py_DECREF(w); + if (err > 0) { + err = 0; + JUMPTO(oparg); + } + else if (err == 0) + ; + else + break; continue; case IMPORT_NAME: @@ -2043,44 +2089,49 @@ PyEval_EvalFrameEx(PyFrameObject *f, int JUMPBY(oparg); goto fast_next_opcode; - PREDICTED_WITH_ARG(JUMP_IF_FALSE); - case JUMP_IF_FALSE: + case POP_OR_JUMP: w = TOP(); if (w == Py_True) { - PREDICT(POP_TOP); + STACKADJ(-1); + Py_DECREF(w); goto fast_next_opcode; } if (w == Py_False) { - JUMPBY(oparg); + JUMPTO(oparg); goto fast_next_opcode; } err = PyObject_IsTrue(w); - if (err > 0) - err = 0; + if (err > 0) { + STACKADJ(-1); + Py_DECREF(w); + err = 0; + } else if (err == 0) - JUMPBY(oparg); - else - break; - continue; - - PREDICTED_WITH_ARG(JUMP_IF_TRUE); - case JUMP_IF_TRUE: + JUMPTO(oparg); + else + break; + continue; + + case JUMP_OR_POP: w = TOP(); if (w == Py_False) { - PREDICT(POP_TOP); + STACKADJ(-1); + Py_DECREF(w); goto fast_next_opcode; } if (w == Py_True) { - JUMPBY(oparg); + JUMPTO(oparg); goto fast_next_opcode; } err = PyObject_IsTrue(w); if (err > 0) { err = 0; - JUMPBY(oparg); - } - else if (err == 0) - ; + JUMPTO(oparg); + } + else if (err == 0) { + STACKADJ(-1); + Py_DECREF(w); + } else break; continue; diff -r 56747b9abe2d -r 537340dbaffa Python/compile.c --- a/Python/compile.c +++ b/Python/compile.c @@ -818,11 +818,15 @@ opcode_stack_effect(int opcode, int opar return 1; case JUMP_FORWARD: - case JUMP_IF_FALSE: - case JUMP_IF_TRUE: + case JUMP_OR_POP: /* -1 if jump not taken */ + case POP_OR_JUMP: /* "" */ case JUMP_ABSOLUTE: return 0; + case POP_JUMP_IF_FALSE: + case POP_JUMP_IF_TRUE: + return -1; + case LOAD_GLOBAL: return 1; @@ -1664,12 +1668,10 @@ compiler_ifexp(struct compiler *c, expr_ if (next == NULL) return 0; VISIT(c, expr, e->v.IfExp.test); - ADDOP_JREL(c, JUMP_IF_FALSE, next); - ADDOP(c, POP_TOP); + ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); VISIT(c, expr, e->v.IfExp.body); ADDOP_JREL(c, JUMP_FORWARD, end); compiler_use_next_block(c, next); - ADDOP(c, POP_TOP); VISIT(c, expr, e->v.IfExp.orelse); compiler_use_next_block(c, end); return 1; @@ -1727,9 +1729,6 @@ compiler_if(struct compiler *c, stmt_ty end = compiler_new_block(c); if (end == NULL) return 0; - next = compiler_new_block(c); - if (next == NULL) - return 0; constant = expr_constant(s->v.If.test); /* constant = 0: "if 0" @@ -1741,15 +1740,21 @@ compiler_if(struct compiler *c, stmt_ty } else if (constant == 1) { VISIT_SEQ(c, stmt, s->v.If.body); } else { + if (s->v.If.orelse) { + next = compiler_new_block(c); + if (next == NULL) + return 0; + } + else + next = end; VISIT(c, expr, s->v.If.test); - ADDOP_JREL(c, JUMP_IF_FALSE, next); - ADDOP(c, POP_TOP); + ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); VISIT_SEQ(c, stmt, s->v.If.body); ADDOP_JREL(c, JUMP_FORWARD, end); - compiler_use_next_block(c, next); - ADDOP(c, POP_TOP); - if (s->v.If.orelse) + if (s->v.If.orelse) { + compiler_use_next_block(c, next); VISIT_SEQ(c, stmt, s->v.If.orelse); + } } compiler_use_next_block(c, end); return 1; @@ -1823,8 +1828,7 @@ compiler_while(struct compiler *c, stmt_ so we need to set an extra line number. */ c->u->u_lineno_set = 0; VISIT(c, expr, s->v.While.test); - ADDOP_JREL(c, JUMP_IF_FALSE, anchor); - ADDOP(c, POP_TOP); + ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor); } VISIT_SEQ(c, stmt, s->v.While.body); ADDOP_JABS(c, JUMP_ABSOLUTE, loop); @@ -1835,7 +1839,6 @@ compiler_while(struct compiler *c, stmt_ if (constant == -1) { compiler_use_next_block(c, anchor); - ADDOP(c, POP_TOP); ADDOP(c, POP_BLOCK); } compiler_pop_fblock(c, LOOP, loop); @@ -2011,8 +2014,7 @@ compiler_try_except(struct compiler *c, ADDOP(c, DUP_TOP); VISIT(c, expr, handler->v.ExceptHandler.type); ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); - ADDOP_JREL(c, JUMP_IF_FALSE, except); - ADDOP(c, POP_TOP); + ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); } ADDOP(c, POP_TOP); if (handler->v.ExceptHandler.name) { @@ -2083,8 +2085,6 @@ compiler_try_except(struct compiler *c, } ADDOP_JREL(c, JUMP_FORWARD, end); compiler_use_next_block(c, except); - if (handler->v.ExceptHandler.type) - ADDOP(c, POP_TOP); } ADDOP(c, END_FINALLY); compiler_use_next_block(c, orelse); @@ -2263,8 +2263,7 @@ compiler_assert(struct compiler *c, stmt end = compiler_new_block(c); if (end == NULL) return 0; - ADDOP_JREL(c, JUMP_IF_TRUE, end); - ADDOP(c, POP_TOP); + ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); if (s->v.Assert.msg) { VISIT(c, expr, s->v.Assert.msg); @@ -2272,7 +2271,6 @@ compiler_assert(struct compiler *c, stmt } ADDOP_I(c, RAISE_VARARGS, 1); compiler_use_next_block(c, end); - ADDOP(c, POP_TOP); return 1; } @@ -2624,9 +2622,9 @@ compiler_boolop(struct compiler *c, expr assert(e->kind == BoolOp_kind); if (e->v.BoolOp.op == And) - jumpi = JUMP_IF_FALSE; - else - jumpi = JUMP_IF_TRUE; + jumpi = POP_OR_JUMP; + else + jumpi = JUMP_OR_POP; end = compiler_new_block(c); if (end == NULL) return 0; @@ -2635,8 +2633,7 @@ compiler_boolop(struct compiler *c, expr assert(n >= 0); for (i = 0; i < n; ++i) { VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); - ADDOP_JREL(c, jumpi, end); - ADDOP(c, POP_TOP) + ADDOP_JABS(c, jumpi, end); } VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); compiler_use_next_block(c, end); @@ -2732,9 +2729,8 @@ compiler_compare(struct compiler *c, exp ADDOP_I(c, COMPARE_OP, cmpop((cmpop_ty)(asdl_seq_GET( e->v.Compare.ops, i - 1)))); - ADDOP_JREL(c, JUMP_IF_FALSE, cleanup); + ADDOP_JABS(c, POP_OR_JUMP, cleanup); NEXT_BLOCK(c); - ADDOP(c, POP_TOP); if (i < (n - 1)) VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); @@ -2867,10 +2863,9 @@ compiler_comprehension_generator(struct for (i = 0; i < n; i++) { expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); VISIT(c, expr, e); - ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup); + ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); NEXT_BLOCK(c); - ADDOP(c, POP_TOP); - } + } if (++gen_index < asdl_seq_LEN(generators)) if (!compiler_comprehension_generator(c, @@ -2908,13 +2903,7 @@ compiler_comprehension_generator(struct compiler_use_next_block(c, skip); } - for (i = 0; i < n; i++) { - ADDOP_I(c, JUMP_FORWARD, 1); - if (i == 0) - compiler_use_next_block(c, if_cleanup); - - ADDOP(c, POP_TOP); - } + compiler_use_next_block(c, if_cleanup); ADDOP_JABS(c, JUMP_ABSOLUTE, start); compiler_use_next_block(c, anchor); diff -r 56747b9abe2d -r 537340dbaffa Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -89,8 +89,10 @@ extern time_t PyOS_GetLastModificationTi Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT) Python 3.1a0: 3140 (optimize list, set and dict comprehensions: change LIST_APPEND and SET_ADD, add MAP_ADD) + Python 3.1a0: 3152 (optimize conditional branches: + introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE) */ -#define MAGIC (3140 | ((long)'\r'<<16) | ((long)'\n'<<24)) +#define MAGIC (3152 | ((long)'\r'<<16) | ((long)'\n'<<24)) /* Magic word as global; note that _PyImport_Init() can change the value of this global to accommodate for alterations of how the diff -r 56747b9abe2d -r 537340dbaffa Python/peephole.c --- a/Python/peephole.c +++ b/Python/peephole.c @@ -13,7 +13,12 @@ #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) -#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) +#define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ + || op==POP_OR_JUMP || op==JUMP_OR_POP) +#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \ + || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ + || op==POP_OR_JUMP || op==JUMP_OR_POP) +#define JUMP_SIGN(op) (op==POP_JUMP_IF_TRUE || op==JUMP_OR_POP) #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) @@ -237,8 +242,10 @@ markblocks(unsigned char *code, Py_ssize switch (opcode) { case FOR_ITER: case JUMP_FORWARD: - case JUMP_IF_FALSE: - case JUMP_IF_TRUE: + case POP_OR_JUMP: + case JUMP_OR_POP: + case POP_JUMP_IF_FALSE: + case POP_JUMP_IF_TRUE: case JUMP_ABSOLUTE: case CONTINUE_LOOP: case SETUP_LOOP: @@ -363,29 +370,24 @@ PyCode_Optimize(PyObject *code, PyObject assert(PyList_Check(consts)); for (i=0 ; i a is not b not a in b --> a not in b @@ -417,29 +419,18 @@ PyCode_Optimize(PyObject *code, PyObject break; /* Skip over LOAD_CONST trueconst - JUMP_IF_FALSE xx POP_TOP */ + POP_JUMP_IF_FALSE xx */ case LOAD_CONST: cumlc = lastlc + 1; j = GETARG(codestr, i); - if (codestr[i+3] != JUMP_IF_FALSE || - codestr[i+6] != POP_TOP || + if (codestr[i+3] != POP_JUMP_IF_FALSE || !ISBASICBLOCK(blocks,i,7) || !PyObject_IsTrue(PyList_GET_ITEM(consts, j))) continue; - memset(codestr+i, NOP, 7); + memset(codestr+i, NOP, 6); cumlc = 0; break; - /* Replace POP_TOP JUMP_FORWARD 1 POP_TOP - with NOP NOP NOP NOP POP_TOP. */ - case POP_TOP: - if (UNCONDITIONAL_JUMP(codestr[i+1]) && - GETJUMPTGT(codestr, i+1) == i+5 && - codestr[i+4] == POP_TOP && - ISBASICBLOCK(blocks,i,4)) - memset(codestr+i, NOP, 4); - break; - /* Try to fold tuples of constants (includes a case for lists which are only used for "in" and "not in" tests). Skip over BUILD_SEQN 1 UNPACK_SEQN 1. @@ -524,27 +515,38 @@ PyCode_Optimize(PyObject *code, PyObject "if a or b:" "a and b or c" "(a and b) and c" - x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z - x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3 + x:POP_OR_JUMP y y:POP_OR_JUMP z --> x:POP_OR_JUMP z + x:POP_OR_JUMP y y:JUMP_OR_POP z --> x:POP_JUMP_IF_FALSE y+3 where y+3 is the instruction following the second test. */ - case JUMP_IF_FALSE: - case JUMP_IF_TRUE: + case POP_OR_JUMP: + case JUMP_OR_POP: tgt = GETJUMPTGT(codestr, i); j = codestr[tgt]; - if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) { - if (j == opcode) { - tgttgt = GETJUMPTGT(codestr, tgt) - i - 3; + if (CONDITIONAL_JUMP(j)) { + /* NOTE: all possible jumps here are + absolute! */ + if (JUMP_SIGN(j) == JUMP_SIGN(opcode)) { + tgttgt = GETJUMPTGT(codestr, tgt); + /* The current opcode inherits + its target's stack behaviour */ + codestr[i] = j; SETARG(codestr, i, tgttgt); + goto _optimize; } else { - tgt -= i; - SETARG(codestr, i, tgt); + if (JUMP_SIGN(opcode)) + codestr[i] = POP_JUMP_IF_TRUE; + else + codestr[i] = POP_JUMP_IF_FALSE; + SETARG(codestr, i, (tgt + 3)); + goto _optimize; } - break; } - /* Intentional fallthrough */ + /* Intentional fallthrough */ /* Replace jumps to unconditional jumps */ + case POP_JUMP_IF_FALSE: + case POP_JUMP_IF_TRUE: case FOR_ITER: case JUMP_FORWARD: case JUMP_ABSOLUTE: @@ -621,14 +623,16 @@ PyCode_Optimize(PyObject *code, PyObject case JUMP_ABSOLUTE: case CONTINUE_LOOP: + case POP_JUMP_IF_FALSE: + case POP_JUMP_IF_TRUE: + case POP_OR_JUMP: + case JUMP_OR_POP: j = addrmap[GETARG(codestr, i)]; SETARG(codestr, i, j); break; case FOR_ITER: case JUMP_FORWARD: - case JUMP_IF_FALSE: - case JUMP_IF_TRUE: case SETUP_LOOP: case SETUP_EXCEPT: case SETUP_FINALLY: