[Patches] [ python-Patches-1346238 ] A constant folding optimization pass for the AST

SourceForge.net noreply at sourceforge.net
Wed Nov 2 19:49:16 CET 2005


Patches item #1346238, was opened at 2005-11-02 19:49
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1346238&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Rune Holm (titanstar)
Assigned to: Nobody/Anonymous (nobody)
Summary: A constant folding optimization pass for the AST

Initial Comment:
This patch adds the following: A visitor interface
generalized from the existing ast pass code in order to
make it easy to write ast passes that only care about
specific node types. A constant folding pass that looks
for operations involving number or string literals, and
calculates these at compile time. Example code snippets
that this pass will optimize:

3 + 4 + x => 7 + x

2 ** 2 ** 2 => 16

4 and 5 and x and 6 => x and 6

4 or 5 or x => 4

4 and 5 and ~6 => -7


When combined with patch 1346214, the compiler will
also optimize statements like

if 2**2**2 - 16: expensive_computation() => nothing

The patch adds two new files: Include/optimize.h and
Python.optimize.c. This was done because I anticipate
adding more AST optimizations later using the same
visitor interface, and Python/compile.c is already very
crowded with byte code generation and bytecode
optimization. If new files aren't desired, I could
easily change the pass to add the extra code to compile.c

This patch combined with patch 1346214 passes the unit
tests on all the platforms I've tested it on, namely:
macos 10.3/ppc
linux/x86
linux/amd64
linux/ppc
linux/ia64

valgrind on linux/x86 does not reveal any additional
leaks or uninitialized accesses that aren't already in
the svn head.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1346238&group_id=5470


More information about the Patches mailing list