PageRenderTime 73ms CodeModel.GetById 14ms app.highlight 53ms RepoModel.GetById 2ms app.codeStats 0ms

/Python/peephole.c

http://unladen-swallow.googlecode.com/
C | 680 lines | 498 code | 50 blank | 132 comment | 131 complexity | 1f61433d50c0be2cd8c591c203abf907 MD5 | raw file
  1/* Peephole optimizations for bytecode compiler. */
  2
  3#include "Python.h"
  4
  5#include "Python-ast.h"
  6#include "node.h"
  7#include "pyarena.h"
  8#include "ast.h"
  9#include "code.h"
 10#include "compile.h"
 11#include "symtable.h"
 12#include "opcode.h"
 13
 14#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
 15#define UNCONDITIONAL_JUMP(op)	(op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
 16#define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
 17	|| op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
 18#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \
 19	|| op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
 20	|| op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
 21#define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP)
 22#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
 23#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
 24#define CODESIZE(op)  (HAS_ARG(op) ? 3 : 1)
 25#define ISBASICBLOCK(blocks, start, bytes) \
 26	(blocks[start]==blocks[start+bytes-1])
 27
 28/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
 29   with	   LOAD_CONST (c1, c2, ... cn).
 30   The consts table must still be in list form so that the
 31   new constant (c1, c2, ... cn) can be appended.
 32   Called with codestr pointing to the first LOAD_CONST.
 33   Bails out with no change if one or more of the LOAD_CONSTs is missing. 
 34   Also works for BUILD_LIST when followed by an "in" or "not in" test.
 35*/
 36static int
 37tuple_of_constants(unsigned char *codestr, Py_ssize_t n, PyObject *consts)
 38{
 39	PyObject *newconst, *constant;
 40	Py_ssize_t i, arg, len_consts;
 41
 42	/* Pre-conditions */
 43	assert(PyList_CheckExact(consts));
 44	assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
 45	assert(GETARG(codestr, (n*3)) == n);
 46	for (i=0 ; i<n ; i++)
 47		assert(codestr[i*3] == LOAD_CONST);
 48
 49	/* Buildup new tuple of constants */
 50	newconst = PyTuple_New(n);
 51	if (newconst == NULL)
 52		return 0;
 53	len_consts = PyList_GET_SIZE(consts);
 54	for (i=0 ; i<n ; i++) {
 55		arg = GETARG(codestr, (i*3));
 56		assert(arg < len_consts);
 57		constant = PyList_GET_ITEM(consts, arg);
 58		Py_INCREF(constant);
 59		PyTuple_SET_ITEM(newconst, i, constant);
 60	}
 61
 62	/* Append folded constant onto consts */
 63	if (PyList_Append(consts, newconst)) {
 64		Py_DECREF(newconst);
 65		return 0;
 66	}
 67	Py_DECREF(newconst);
 68
 69	/* Write NOPs over old LOAD_CONSTS and
 70	   add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
 71	memset(codestr, NOP, n*3);
 72	codestr[n*3] = LOAD_CONST;
 73	SETARG(codestr, (n*3), len_consts);
 74	return 1;
 75}
 76
 77/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
 78   with	   LOAD_CONST binop(c1,c2)
 79   The consts table must still be in list form so that the
 80   new constant can be appended.
 81   Called with codestr pointing to the first LOAD_CONST. 
 82   Abandons the transformation if the folding fails (i.e.  1+'a').  
 83   If the new constant is a sequence, only folds when the size
 84   is below a threshold value.	That keeps pyc files from
 85   becoming large in the presence of code like:	 (None,)*1000.
 86*/
 87static int
 88fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
 89{
 90	PyObject *newconst, *v, *w;
 91	Py_ssize_t len_consts, size;
 92	int opcode;
 93
 94	/* Pre-conditions */
 95	assert(PyList_CheckExact(consts));
 96	assert(codestr[0] == LOAD_CONST);
 97	assert(codestr[3] == LOAD_CONST);
 98
 99	/* Create new constant */
100	v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
101	w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
102	opcode = codestr[6];
103	switch (opcode) {
104		case BINARY_POWER:
105			newconst = PyNumber_Power(v, w, Py_None);
106			break;
107		case BINARY_MULTIPLY:
108			newconst = PyNumber_Multiply(v, w);
109			break;
110		case BINARY_DIVIDE:
111			/* Cannot fold this operation statically since
112                           the result can depend on the run-time presence
113                           of the -Qnew flag */
114			return 0;
115		case BINARY_TRUE_DIVIDE:
116			newconst = PyNumber_TrueDivide(v, w);
117			break;
118		case BINARY_FLOOR_DIVIDE:
119			newconst = PyNumber_FloorDivide(v, w);
120			break;
121		case BINARY_MODULO:
122			newconst = PyNumber_Remainder(v, w);
123			break;
124		case BINARY_ADD:
125			newconst = PyNumber_Add(v, w);
126			break;
127		case BINARY_SUBTRACT:
128			newconst = PyNumber_Subtract(v, w);
129			break;
130		case BINARY_SUBSCR:
131			newconst = PyObject_GetItem(v, w);
132			break;
133		case BINARY_LSHIFT:
134			newconst = PyNumber_Lshift(v, w);
135			break;
136		case BINARY_RSHIFT:
137			newconst = PyNumber_Rshift(v, w);
138			break;
139		case BINARY_AND:
140			newconst = PyNumber_And(v, w);
141			break;
142		case BINARY_XOR:
143			newconst = PyNumber_Xor(v, w);
144			break;
145		case BINARY_OR:
146			newconst = PyNumber_Or(v, w);
147			break;
148		default:
149			/* Called with an unknown opcode */
150			PyErr_Format(PyExc_SystemError,
151			     "unexpected binary operation %d on a constant",
152				     opcode);
153			return 0;
154	}
155	if (newconst == NULL) {
156		PyErr_Clear();
157		return 0;
158	}
159	size = PyObject_Size(newconst);
160	if (size == -1)
161		PyErr_Clear();
162	else if (size > 20) {
163		Py_DECREF(newconst);
164		return 0;
165	}
166
167	/* Append folded constant into consts table */
168	len_consts = PyList_GET_SIZE(consts);
169	if (PyList_Append(consts, newconst)) {
170		Py_DECREF(newconst);
171		return 0;
172	}
173	Py_DECREF(newconst);
174
175	/* Write NOP NOP NOP NOP LOAD_CONST newconst */
176	memset(codestr, NOP, 4);
177	codestr[4] = LOAD_CONST;
178	SETARG(codestr, 4, len_consts);
179	return 1;
180}
181
182static int
183fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
184{
185	PyObject *newconst=NULL, *v;
186	Py_ssize_t len_consts;
187	int opcode;
188
189	/* Pre-conditions */
190	assert(PyList_CheckExact(consts));
191	assert(codestr[0] == LOAD_CONST);
192
193	/* Create new constant */
194	v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
195	opcode = codestr[3];
196	switch (opcode) {
197		case UNARY_NEGATIVE:
198			/* Preserve the sign of -0.0 */
199			if (PyObject_IsTrue(v) == 1)
200				newconst = PyNumber_Negative(v);
201			break;
202		case UNARY_CONVERT:
203			newconst = PyObject_Repr(v);
204			break;
205		case UNARY_INVERT:
206			newconst = PyNumber_Invert(v);
207			break;
208		default:
209			/* Called with an unknown opcode */
210			PyErr_Format(PyExc_SystemError,
211			     "unexpected unary operation %d on a constant",
212				     opcode);
213			return 0;
214	}
215	if (newconst == NULL) {
216		PyErr_Clear();
217		return 0;
218	}
219
220	/* Append folded constant into consts table */
221	len_consts = PyList_GET_SIZE(consts);
222	if (PyList_Append(consts, newconst)) {
223		Py_DECREF(newconst);
224		return 0;
225	}
226	Py_DECREF(newconst);
227
228	/* Write NOP LOAD_CONST newconst */
229	codestr[0] = NOP;
230	codestr[1] = LOAD_CONST;
231	SETARG(codestr, 1, len_consts);
232	return 1;
233}
234
235/* Marks instructions that can be the targets of jumps so we keep them	
236   distinct through the peephole optimizer. */
237static unsigned int *
238markblocks(unsigned char *code, Py_ssize_t len, PyObject *lineno_obj)
239{
240	unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
241        unsigned char *lineno;
242        Py_ssize_t tabsiz;
243        Py_ssize_t code_index = 0;
244        Py_ssize_t k;
245	int i,j, opcode, blockcnt = 0;
246
247	if (blocks == NULL) {
248		PyErr_NoMemory();
249		return NULL;
250	}
251	memset(blocks, 0, len*sizeof(int));
252
253	/* Mark labels in the first pass */
254	for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
255		opcode = code[i];
256		switch (opcode) {
257			case FOR_ITER:
258			case JUMP_FORWARD:
259			case JUMP_IF_FALSE_OR_POP:
260			case JUMP_IF_TRUE_OR_POP:
261			case POP_JUMP_IF_FALSE:
262			case POP_JUMP_IF_TRUE:
263			case JUMP_ABSOLUTE:
264			case CONTINUE_LOOP:
265			case SETUP_LOOP:
266			case SETUP_EXCEPT:
267			case SETUP_FINALLY:
268				j = GETJUMPTGT(code, i);
269				blocks[j] = 1;
270				break;
271		}
272	}
273
274        /* Make sure none of the optimizations occur across line number
275           boundaries, by making each logical line seem like its own block. 
276           This both makes code easier to debug and allows users to sensibly
277           set the line number. */
278        lineno = (unsigned char *)PyString_AS_STRING(lineno_obj);
279        tabsiz = PyString_GET_SIZE(lineno_obj);
280        for (k=0; k < tabsiz; k += 2) {
281                code_index += lineno[k];
282                /* This actually makes sense: a trace function
283                   can jump to the start of any line, so each
284                   line is a basic block. */
285                blocks[code_index] = 1;
286        }
287	/* Build block numbers in the second pass */
288	for (i=0 ; i<len ; i++) {
289		blockcnt += blocks[i];	/* increment blockcnt over labels */
290		blocks[i] = blockcnt;
291	}
292	return blocks;
293}
294
295/* Perform basic peephole optimizations to components of a code object.
296   The consts object should still be in list form to allow new constants 
297   to be appended.
298
299   To keep the optimizer simple, it bails out (does nothing) for code
300   containing extended arguments or that has a length over 32,700.  That 
301   allows us to avoid overflow and sign issues.	 Likewise, it bails when
302   the lineno table has complex encoding for gaps >= 255.
303
304   Optimizations are restricted to simple transformations occuring within a
305   single basic block.	All transformations keep the code size the same or 
306   smaller.  For those that reduce size, the gaps are initially filled with 
307   NOPs.  Later those NOPs are removed and the jump addresses retargeted in 
308   a single pass.  Line numbering is adjusted accordingly. */
309
310PyObject *
311PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
312                PyObject *lineno_obj)
313{
314	Py_ssize_t i, j, codelen;
315	int nops, h, adj;
316	int tgt, tgttgt, opcode;
317	unsigned char *codestr = NULL;
318	unsigned char *lineno;
319	int *addrmap = NULL;
320	int new_line, cum_orig_line, last_line, tabsiz;
321	int cumlc=0, lastlc=0;	/* Count runs of consecutive LOAD_CONSTs */
322	unsigned int *blocks = NULL;
323	char *name;
324
325	/* Bail out if an exception is set */
326	if (PyErr_Occurred())
327		goto exitUnchanged;
328
329	/* Bypass optimization when the lineno table is too complex */
330	assert(PyString_Check(lineno_obj));
331	lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
332	tabsiz = PyString_GET_SIZE(lineno_obj);
333	if (memchr(lineno, 255, tabsiz) != NULL)
334		goto exitUnchanged;
335
336	/* Avoid situations where jump retargeting could overflow */
337	assert(PyString_Check(code));
338	codelen = PyString_GET_SIZE(code);
339	if (codelen > 32700)
340		goto exitUnchanged;
341
342	/* Make a modifiable copy of the code string */
343	codestr = (unsigned char *)PyMem_Malloc(codelen);
344	if (codestr == NULL)
345		goto exitUnchanged;
346	codestr = (unsigned char *)memcpy(codestr, 
347					  PyString_AS_STRING(code), codelen);
348
349	/* Verify that RETURN_VALUE terminates the codestring.	This allows
350	   the various transformation patterns to look ahead several
351	   instructions without additional checks to make sure they are not
352	   looking beyond the end of the code string.
353	*/
354	if (codestr[codelen-1] != RETURN_VALUE)
355		goto exitUnchanged;
356
357	/* Mapping to new jump targets after NOPs are removed */
358	addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
359	if (addrmap == NULL)
360		goto exitUnchanged;
361
362	blocks = markblocks(codestr, codelen, lineno_obj);
363	if (blocks == NULL)
364		goto exitUnchanged;
365	assert(PyList_Check(consts));
366
367	for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
368	  reoptimize_current:
369		opcode = codestr[i];
370
371		lastlc = cumlc;
372		cumlc = 0;
373
374		switch (opcode) {
375			/* Replace UNARY_NOT POP_JUMP_IF_FALSE 
376			   with    POP_JUMP_IF_TRUE */
377			case UNARY_NOT:
378				if (codestr[i+1] != POP_JUMP_IF_FALSE
379				    || !ISBASICBLOCK(blocks,i,4))
380					continue;
381				j = GETARG(codestr, i+1);
382				codestr[i] = POP_JUMP_IF_TRUE;
383				SETARG(codestr, i, j);
384				codestr[i+3] = NOP;
385				goto reoptimize_current;
386
387				/* not a is b -->  a is not b
388				   not a in b -->  a not in b
389				   not a is not b -->  a is b
390				   not a not in b -->  a in b
391				*/
392			case COMPARE_OP:
393				j = GETARG(codestr, i);
394				if (j < 6  ||  j > 9  ||
395				    codestr[i+3] != UNARY_NOT  || 
396				    !ISBASICBLOCK(blocks,i,4))
397					continue;
398				SETARG(codestr, i, (j^1));
399				codestr[i+3] = NOP;
400				break;
401
402				/* Replace LOAD_GLOBAL/LOAD_NAME None
403                                   with LOAD_CONST None */
404			case LOAD_NAME:
405			case LOAD_GLOBAL:
406				j = GETARG(codestr, i);
407				name = PyString_AsString(PyTuple_GET_ITEM(names, j));
408				if (name == NULL  ||  strcmp(name, "None") != 0)
409					continue;
410				for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
411					if (PyList_GET_ITEM(consts, j) == Py_None)
412						break;
413				}
414				if (j == PyList_GET_SIZE(consts)) {
415					if (PyList_Append(consts, Py_None) == -1)
416					        goto exitUnchanged;                                        
417				}
418				assert(PyList_GET_ITEM(consts, j) == Py_None);
419				codestr[i] = LOAD_CONST;
420				SETARG(codestr, i, j);
421				cumlc = lastlc + 1;
422				break;
423
424				/* Skip over LOAD_CONST trueconst
425				   POP_JUMP_IF_FALSE xx. This improves
426				   "while 1" performance. */
427			case LOAD_CONST:
428				cumlc = lastlc + 1;
429				j = GETARG(codestr, i);
430				if (codestr[i+3] != POP_JUMP_IF_FALSE  ||
431				    !ISBASICBLOCK(blocks,i,6)  ||
432				    !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
433					continue;
434				memset(codestr+i, NOP, 6);
435				cumlc = 0;
436				break;
437
438				/* Try to fold tuples of constants (includes a case for lists
439				   which are only used for "in" and "not in" tests).
440				   Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
441				   Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
442				   Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
443			case BUILD_TUPLE:
444			case BUILD_LIST:
445				j = GETARG(codestr, i);
446				h = i - 3 * j;
447				if (h >= 0  &&
448				    j <= lastlc	 &&
449				    ((opcode == BUILD_TUPLE && 
450				      ISBASICBLOCK(blocks, h, 3*(j+1))) ||
451				     (opcode == BUILD_LIST && 
452				      codestr[i+3]==COMPARE_OP && 
453				      ISBASICBLOCK(blocks, h, 3*(j+2)) &&
454				      (GETARG(codestr,i+3)==6 ||
455				       GETARG(codestr,i+3)==7))) &&
456				    tuple_of_constants(&codestr[h], j, consts)) {
457					assert(codestr[i] == LOAD_CONST);
458					cumlc = 1;
459					break;
460				}
461				if (codestr[i+3] != UNPACK_SEQUENCE  ||
462				    !ISBASICBLOCK(blocks,i,6) ||
463				    j != GETARG(codestr, i+3))
464					continue;
465				if (j == 1) {
466					memset(codestr+i, NOP, 6);
467				} else if (j == 2) {
468					codestr[i] = ROT_TWO;
469					memset(codestr+i+1, NOP, 5);
470				} else if (j == 3) {
471					codestr[i] = ROT_THREE;
472					codestr[i+1] = ROT_TWO;
473					memset(codestr+i+2, NOP, 4);
474				}
475				break;
476
477				/* Fold binary ops on constants.
478				   LOAD_CONST c1 LOAD_CONST c2 BINOP -->  LOAD_CONST binop(c1,c2) */
479			case BINARY_POWER:
480			case BINARY_MULTIPLY:
481			case BINARY_TRUE_DIVIDE:
482			case BINARY_FLOOR_DIVIDE:
483			case BINARY_MODULO:
484			case BINARY_ADD:
485			case BINARY_SUBTRACT:
486			case BINARY_SUBSCR:
487			case BINARY_LSHIFT:
488			case BINARY_RSHIFT:
489			case BINARY_AND:
490			case BINARY_XOR:
491			case BINARY_OR:
492				if (lastlc >= 2	 &&
493				    ISBASICBLOCK(blocks, i-6, 7)  &&
494				    fold_binops_on_constants(&codestr[i-6], consts)) {
495					i -= 2;
496					assert(codestr[i] == LOAD_CONST);
497					cumlc = 1;
498				}
499				break;
500
501				/* Fold unary ops on constants.
502				   LOAD_CONST c1  UNARY_OP -->	LOAD_CONST unary_op(c) */
503			case UNARY_NEGATIVE:
504			case UNARY_CONVERT:
505			case UNARY_INVERT:
506				if (lastlc >= 1	 &&
507				    ISBASICBLOCK(blocks, i-3, 4)  &&
508				    fold_unaryops_on_constants(&codestr[i-3], consts))	{
509					i -= 2;
510					assert(codestr[i] == LOAD_CONST);
511					cumlc = 1;
512				}
513				break;
514
515				/* Simplify conditional jump to conditional jump where the
516				   result of the first test implies the success of a similar
517				   test or the failure of the opposite test.
518				   Arises in code like:
519				   "if a and b:"
520				   "if a or b:"
521				   "a and b or c"
522				   "(a and b) and c"
523				   x:JUMP_IF_FALSE_OR_POP y   y:JUMP_IF_FALSE_OR_POP z
524				      -->  x:JUMP_IF_FALSE_OR_POP z
525				   x:JUMP_IF_FALSE_OR_POP y   y:JUMP_IF_TRUE_OR_POP z
526				      -->  x:POP_JUMP_IF_FALSE y+3
527				   where y+3 is the instruction following the second test.
528				*/
529			case JUMP_IF_FALSE_OR_POP:
530			case JUMP_IF_TRUE_OR_POP:
531				tgt = GETJUMPTGT(codestr, i);
532				j = codestr[tgt];
533				if (CONDITIONAL_JUMP(j)) {
534					/* NOTE: all possible jumps here are
535					   absolute! */
536					if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) {
537						/* The second jump will be
538						   taken iff the first is. */
539						tgttgt = GETJUMPTGT(codestr, tgt);
540						/* The current opcode inherits
541						   its target's stack behaviour */
542						codestr[i] = j;
543						SETARG(codestr, i, tgttgt);
544						goto reoptimize_current;
545					} else {
546						/* The second jump is not taken
547						   if the first is (so jump past
548						   it), and all conditional
549						   jumps pop their argument when
550						   they're not taken (so change
551						   the first jump to pop its
552						   argument when it's taken). */
553						if (JUMPS_ON_TRUE(opcode))
554							codestr[i] = POP_JUMP_IF_TRUE;
555						else
556							codestr[i] = POP_JUMP_IF_FALSE;
557						SETARG(codestr, i, (tgt + 3));
558						goto reoptimize_current;
559					}
560				}
561				/* Intentional fallthrough */
562
563				/* Replace jumps to unconditional jumps */
564			case POP_JUMP_IF_FALSE:
565			case POP_JUMP_IF_TRUE:
566			case FOR_ITER:
567			case JUMP_FORWARD:
568			case JUMP_ABSOLUTE:
569			case CONTINUE_LOOP:
570			case SETUP_LOOP:
571			case SETUP_EXCEPT:
572			case SETUP_FINALLY:
573				tgt = GETJUMPTGT(codestr, i);
574				/* Replace JUMP_* to a RETURN into just a RETURN */
575				if (UNCONDITIONAL_JUMP(opcode) &&
576				    codestr[tgt] == RETURN_VALUE) {
577					codestr[i] = RETURN_VALUE;
578					memset(codestr+i+1, NOP, 2);
579					continue;
580				}
581				if (!UNCONDITIONAL_JUMP(codestr[tgt]))
582					continue;
583				tgttgt = GETJUMPTGT(codestr, tgt);
584				if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
585					opcode = JUMP_ABSOLUTE;
586				if (!ABSOLUTE_JUMP(opcode))
587					tgttgt -= i + 3;     /* Calc relative jump addr */
588				if (tgttgt < 0)		  /* No backward relative jumps */
589					continue;
590				codestr[i] = opcode;
591				SETARG(codestr, i, tgttgt);
592				break;
593
594			case EXTENDED_ARG:
595				goto exitUnchanged;
596
597				/* Replace RETURN LOAD_CONST None RETURN with just RETURN */
598				/* Remove unreachable JUMPs after RETURN */
599			case RETURN_VALUE:
600				if (i+4 >= codelen)
601					continue;
602				if (codestr[i+4] == RETURN_VALUE &&
603				    ISBASICBLOCK(blocks,i,5))
604					memset(codestr+i+1, NOP, 4);
605				else if (UNCONDITIONAL_JUMP(codestr[i+1]) &&
606				         ISBASICBLOCK(blocks,i,4))
607					memset(codestr+i+1, NOP, 3);
608				break;
609		}
610	}
611
612	/* Fixup linenotab */
613	for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
614		addrmap[i] = i - nops;
615		if (codestr[i] == NOP)
616			nops++;
617	}
618	cum_orig_line = 0;
619	last_line = 0;
620	for (i=0 ; i < tabsiz ; i+=2) {
621		cum_orig_line += lineno[i];
622		new_line = addrmap[cum_orig_line];
623		/* We checked above that no two lines are more than 255
624		   code elements apart.  Since the peephole optimizer
625		   only reduces the size of the bytecode, we know that
626		   they're still less than 255 apart.  */
627		assert (new_line - last_line < 255);
628		lineno[i] =((unsigned char)(new_line - last_line));
629		last_line = new_line;
630	}
631
632	/* Remove NOPs and fixup jump targets */
633	for (i=0, h=0 ; i<codelen ; ) {
634		opcode = codestr[i];
635		switch (opcode) {
636			case NOP:
637				i++;
638				continue;
639
640			case JUMP_ABSOLUTE:
641			case CONTINUE_LOOP:
642			case POP_JUMP_IF_FALSE:
643			case POP_JUMP_IF_TRUE:
644			case JUMP_IF_FALSE_OR_POP:
645			case JUMP_IF_TRUE_OR_POP:
646				j = addrmap[GETARG(codestr, i)];
647				SETARG(codestr, i, j);
648				break;
649
650			case FOR_ITER:
651			case JUMP_FORWARD:
652			case SETUP_LOOP:
653			case SETUP_EXCEPT:
654			case SETUP_FINALLY:
655				j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
656				SETARG(codestr, i, j);
657				break;
658		}
659		adj = CODESIZE(opcode);
660		while (adj--)
661			codestr[h++] = codestr[i++];
662	}
663	assert(h + nops == codelen);
664
665	code = PyString_FromStringAndSize((char *)codestr, h);
666	PyMem_Free(addrmap);
667	PyMem_Free(codestr);
668	PyMem_Free(blocks);
669	return code;
670
671 exitUnchanged:
672	if (blocks != NULL)
673		PyMem_Free(blocks);
674	if (addrmap != NULL)
675		PyMem_Free(addrmap);
676	if (codestr != NULL)
677		PyMem_Free(codestr);
678	Py_INCREF(code);
679	return code;
680}