All passes added in llvm.passes. More tests. Some bug fixes.
git-svn-id: http://llvm-py.googlecode.com/svn/trunk@33 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
parent
4ee007190c
commit
18177ba0fa
3 changed files with 172 additions and 31 deletions
|
|
@ -110,10 +110,10 @@ class ExecutionEngine(object):
|
||||||
core.check_is_function(fn)
|
core.check_is_function(fn)
|
||||||
_core.LLVMFreeMachineCodeForFunction(self.ptr, fn.ptr)
|
_core.LLVMFreeMachineCodeForFunction(self.ptr, fn.ptr)
|
||||||
|
|
||||||
# CRASH! BOOM!! BANG!!! TODO FIXME
|
|
||||||
def add_module_provider(self, mp):
|
def add_module_provider(self, mp):
|
||||||
core.check_is_module_provider(mp)
|
core.check_is_module_provider(mp)
|
||||||
_core.LLVMAddModuleProvider(self.ptr, mp.ptr)
|
_core.LLVMAddModuleProvider(self.ptr, mp.ptr)
|
||||||
|
mp._own(self)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_data(self):
|
def target_data(self):
|
||||||
|
|
|
||||||
159
llvm/passes.py
159
llvm/passes.py
|
|
@ -1,33 +1,82 @@
|
||||||
"""Pass managers and passes.
|
"""Pass managers and passes.
|
||||||
|
|
||||||
This module provides the LLVM pass managers and the passes themselves.
|
This module provides the LLVM pass managers and the passes themselves.
|
||||||
Passes that are currently available are:
|
All transformation passes listed at http://www.llvm.org/docs/Passes.html
|
||||||
|
are available.
|
||||||
Simple Constant Propagation
|
|
||||||
Combine Redundant Instructions
|
|
||||||
Promote Memory to Register
|
|
||||||
Demote all values to stack slots
|
|
||||||
Reassociate expressions
|
|
||||||
Global Value Numbering
|
|
||||||
Simplify the CFG
|
|
||||||
|
|
||||||
See http://www.llvm.org/docs/Passes.html for the full list of passes
|
|
||||||
available in LLVM.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import llvm # top-level, for common stuff
|
||||||
import llvm.ee as ee # target data
|
import llvm.ee as ee # target data
|
||||||
|
import llvm.core as core # module provider, function etc.
|
||||||
import llvm._core as _core # C wrappers
|
import llvm._core as _core # C wrappers
|
||||||
from llvm._util import * # utility functions
|
from llvm._util import * # utility functions
|
||||||
|
|
||||||
|
|
||||||
# passes
|
# passes
|
||||||
PASS_CONSTANT_PROPAGATION = 1
|
PASS_AGGRESSIVE_DCE = 1
|
||||||
PASS_INSTRUCTION_COMBINING = 2
|
PASS_ARGUMENT_PROMOTION = 2
|
||||||
PASS_PROMOTE_MEMORY_TO_REGISTER = 3
|
PASS_BLOCK_PLACEMENT = 3
|
||||||
PASS_DEMOTE_MEMORY_TO_REGISTER = 4
|
PASS_BREAK_CRITICAL_EDGES = 4
|
||||||
PASS_REASSOCIATE = 5
|
PASS_CODE_GEN_PREPARE = 5
|
||||||
PASS_GVN = 6
|
PASS_COND_PROPAGATION = 6
|
||||||
PASS_CFG_SIMPLIFICATION = 7
|
PASS_CONSTANT_MERGE = 7
|
||||||
|
PASS_CONSTANT_PROPAGATION = 8
|
||||||
|
PASS_DEAD_CODE_ELIMINATION = 9
|
||||||
|
PASS_DEAD_ARG_ELIMINATION = 10
|
||||||
|
PASS_DEAD_TYPE_ELIMINATION = 11
|
||||||
|
PASS_DEAD_INST_ELIMINATION = 12
|
||||||
|
PASS_DEAD_STORE_ELIMINATION = 13
|
||||||
|
PASS_GCSE = 14
|
||||||
|
PASS_GLOBAL_DCE = 15
|
||||||
|
PASS_GLOBAL_OPTIMIZER = 16
|
||||||
|
PASS_GVN = 17
|
||||||
|
PASS_GVNPRE = 18
|
||||||
|
PASS_IND_MEM_REM = 19
|
||||||
|
PASS_IND_VAR_SIMPLIFY = 20
|
||||||
|
PASS_FUNCTION_INLINING = 21
|
||||||
|
PASS_BLOCK_PROFILER = 22
|
||||||
|
PASS_EDGE_PROFILER = 23
|
||||||
|
PASS_FUNCTION_PROFILER = 24
|
||||||
|
PASS_NULL_PROFILER_RS = 25
|
||||||
|
PASS_RS_PROFILING = 26
|
||||||
|
PASS_INSTRUCTION_COMBINING = 27
|
||||||
|
PASS_INTERNALIZE = 28
|
||||||
|
PASS_IP_CONSTANT_PROPAGATION = 29
|
||||||
|
PASS_IPSCCP = 30
|
||||||
|
PASS_JUMP_THREADING = 31
|
||||||
|
PASS_LCSSA = 32
|
||||||
|
PASS_LICM = 33
|
||||||
|
PASS_LOOP_DELETION = 34
|
||||||
|
PASS_LOOP_EXTRACTOR = 35
|
||||||
|
PASS_SINGLE_LOOP_EXTRACTOR = 36
|
||||||
|
PASS_LOOP_INDEX_SPLIT = 37
|
||||||
|
PASS_LOOP_STRENGTH_REDUCE = 38
|
||||||
|
PASS_LOOP_ROTATE = 39
|
||||||
|
PASS_LOOP_UNROLL = 40
|
||||||
|
PASS_LOOP_UNSWITCH = 41
|
||||||
|
PASS_LOOP_SIMPLIFY = 42
|
||||||
|
PASS_LOWER_ALLOCATIONS = 43
|
||||||
|
PASS_LOWER_INVOKE = 44
|
||||||
|
PASS_LOWER_SET_JMP = 45
|
||||||
|
PASS_LOWER_SWITCH = 46
|
||||||
|
PASS_PROMOTE_MEMORY_TO_REGISTER = 47
|
||||||
|
PASS_MEM_CPY_OPT = 48
|
||||||
|
PASS_UNIFY_FUNCTION_EXIT_NODES = 49
|
||||||
|
PASS_PREDICATE_SIMPLIFIER = 50
|
||||||
|
PASS_PRUNE_EH = 51
|
||||||
|
PASS_RAISE_ALLOCATIONS = 52
|
||||||
|
PASS_REASSOCIATE = 53
|
||||||
|
PASS_DEMOTE_REGISTER_TO_MEMORY = 54
|
||||||
|
PASS_SCALAR_REPL_AGGREGATES = 55
|
||||||
|
PASS_SCCP = 56
|
||||||
|
PASS_SIMPLIFY_LIB_CALLS = 57
|
||||||
|
PASS_CFG_SIMPLIFICATION = 58
|
||||||
|
PASS_STRIP_SYMBOLS = 59
|
||||||
|
PASS_STRIP_DEAD_PROTOTYPES = 60
|
||||||
|
PASS_STRUCT_RET_PROMOTION = 61
|
||||||
|
PASS_TAIL_CALL_ELIMINATION = 62
|
||||||
|
PASS_TAIL_DUPLICATION = 63
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
|
|
@ -35,13 +84,69 @@ PASS_CFG_SIMPLIFICATION = 7
|
||||||
#===----------------------------------------------------------------------===
|
#===----------------------------------------------------------------------===
|
||||||
|
|
||||||
_pass_creator = {
|
_pass_creator = {
|
||||||
|
PASS_AGGRESSIVE_DCE : _core.LLVMAddAggressiveDCEPass,
|
||||||
|
PASS_ARGUMENT_PROMOTION : _core.LLVMAddArgumentPromotionPass,
|
||||||
|
PASS_BLOCK_PLACEMENT : _core.LLVMAddBlockPlacementPass,
|
||||||
|
PASS_BREAK_CRITICAL_EDGES : _core.LLVMAddBreakCriticalEdgesPass,
|
||||||
|
PASS_CODE_GEN_PREPARE : _core.LLVMAddCodeGenPreparePass,
|
||||||
|
PASS_COND_PROPAGATION : _core.LLVMAddCondPropagationPass,
|
||||||
|
PASS_CONSTANT_MERGE : _core.LLVMAddConstantMergePass,
|
||||||
PASS_CONSTANT_PROPAGATION : _core.LLVMAddConstantPropagationPass,
|
PASS_CONSTANT_PROPAGATION : _core.LLVMAddConstantPropagationPass,
|
||||||
PASS_INSTRUCTION_COMBINING : _core.LLVMAddInstructionCombiningPass,
|
PASS_DEAD_CODE_ELIMINATION : _core.LLVMAddDeadCodeEliminationPass,
|
||||||
PASS_PROMOTE_MEMORY_TO_REGISTER : _core.LLVMAddPromoteMemoryToRegisterPass,
|
PASS_DEAD_ARG_ELIMINATION : _core.LLVMAddDeadArgEliminationPass,
|
||||||
PASS_DEMOTE_MEMORY_TO_REGISTER : _core.LLVMAddDemoteMemoryToRegisterPass,
|
PASS_DEAD_TYPE_ELIMINATION : _core.LLVMAddDeadTypeEliminationPass,
|
||||||
PASS_REASSOCIATE : _core.LLVMAddReassociatePass,
|
PASS_DEAD_INST_ELIMINATION : _core.LLVMAddDeadInstEliminationPass,
|
||||||
|
PASS_DEAD_STORE_ELIMINATION : _core.LLVMAddDeadStoreEliminationPass,
|
||||||
|
PASS_GCSE : _core.LLVMAddGCSEPass,
|
||||||
|
PASS_GLOBAL_DCE : _core.LLVMAddGlobalDCEPass,
|
||||||
|
PASS_GLOBAL_OPTIMIZER : _core.LLVMAddGlobalOptimizerPass,
|
||||||
PASS_GVN : _core.LLVMAddGVNPass,
|
PASS_GVN : _core.LLVMAddGVNPass,
|
||||||
|
PASS_GVNPRE : _core.LLVMAddGVNPREPass,
|
||||||
|
PASS_IND_MEM_REM : _core.LLVMAddIndMemRemPass,
|
||||||
|
PASS_IND_VAR_SIMPLIFY : _core.LLVMAddIndVarSimplifyPass,
|
||||||
|
PASS_FUNCTION_INLINING : _core.LLVMAddFunctionInliningPass,
|
||||||
|
PASS_BLOCK_PROFILER : _core.LLVMAddBlockProfilerPass,
|
||||||
|
PASS_EDGE_PROFILER : _core.LLVMAddEdgeProfilerPass,
|
||||||
|
PASS_FUNCTION_PROFILER : _core.LLVMAddFunctionProfilerPass,
|
||||||
|
PASS_NULL_PROFILER_RS : _core.LLVMAddNullProfilerRSPass,
|
||||||
|
PASS_RS_PROFILING : _core.LLVMAddRSProfilingPass,
|
||||||
|
PASS_INSTRUCTION_COMBINING : _core.LLVMAddInstructionCombiningPass,
|
||||||
|
PASS_INTERNALIZE : _core.LLVMAddInternalizePass,
|
||||||
|
PASS_IP_CONSTANT_PROPAGATION : _core.LLVMAddIPConstantPropagationPass,
|
||||||
|
PASS_IPSCCP : _core.LLVMAddIPSCCPPass,
|
||||||
|
PASS_JUMP_THREADING : _core.LLVMAddJumpThreadingPass,
|
||||||
|
PASS_LCSSA : _core.LLVMAddLCSSAPass,
|
||||||
|
PASS_LICM : _core.LLVMAddLICMPass,
|
||||||
|
PASS_LOOP_DELETION : _core.LLVMAddLoopDeletionPass,
|
||||||
|
PASS_LOOP_EXTRACTOR : _core.LLVMAddLoopExtractorPass,
|
||||||
|
PASS_SINGLE_LOOP_EXTRACTOR : _core.LLVMAddSingleLoopExtractorPass,
|
||||||
|
PASS_LOOP_INDEX_SPLIT : _core.LLVMAddLoopIndexSplitPass,
|
||||||
|
PASS_LOOP_STRENGTH_REDUCE : _core.LLVMAddLoopStrengthReducePass,
|
||||||
|
PASS_LOOP_ROTATE : _core.LLVMAddLoopRotatePass,
|
||||||
|
PASS_LOOP_UNROLL : _core.LLVMAddLoopUnrollPass,
|
||||||
|
PASS_LOOP_UNSWITCH : _core.LLVMAddLoopUnswitchPass,
|
||||||
|
PASS_LOOP_SIMPLIFY : _core.LLVMAddLoopSimplifyPass,
|
||||||
|
PASS_LOWER_ALLOCATIONS : _core.LLVMAddLowerAllocationsPass,
|
||||||
|
PASS_LOWER_INVOKE : _core.LLVMAddLowerInvokePass,
|
||||||
|
PASS_LOWER_SET_JMP : _core.LLVMAddLowerSetJmpPass,
|
||||||
|
PASS_LOWER_SWITCH : _core.LLVMAddLowerSwitchPass,
|
||||||
|
PASS_PROMOTE_MEMORY_TO_REGISTER : _core.LLVMAddPromoteMemoryToRegisterPass,
|
||||||
|
PASS_MEM_CPY_OPT : _core.LLVMAddMemCpyOptPass,
|
||||||
|
PASS_UNIFY_FUNCTION_EXIT_NODES : _core.LLVMAddUnifyFunctionExitNodesPass,
|
||||||
|
PASS_PREDICATE_SIMPLIFIER : _core.LLVMAddPredicateSimplifierPass,
|
||||||
|
PASS_PRUNE_EH : _core.LLVMAddPruneEHPass,
|
||||||
|
PASS_RAISE_ALLOCATIONS : _core.LLVMAddRaiseAllocationsPass,
|
||||||
|
PASS_REASSOCIATE : _core.LLVMAddReassociatePass,
|
||||||
|
PASS_DEMOTE_REGISTER_TO_MEMORY : _core.LLVMAddDemoteRegisterToMemoryPass,
|
||||||
|
PASS_SCALAR_REPL_AGGREGATES : _core.LLVMAddScalarReplAggregatesPass,
|
||||||
|
PASS_SCCP : _core.LLVMAddSCCPPass,
|
||||||
|
PASS_SIMPLIFY_LIB_CALLS : _core.LLVMAddSimplifyLibCallsPass,
|
||||||
PASS_CFG_SIMPLIFICATION : _core.LLVMAddCFGSimplificationPass,
|
PASS_CFG_SIMPLIFICATION : _core.LLVMAddCFGSimplificationPass,
|
||||||
|
PASS_STRIP_SYMBOLS : _core.LLVMAddStripSymbolsPass,
|
||||||
|
PASS_STRIP_DEAD_PROTOTYPES : _core.LLVMAddStripDeadPrototypesPass,
|
||||||
|
PASS_STRUCT_RET_PROMOTION : _core.LLVMAddStructRetPromotionPass,
|
||||||
|
PASS_TAIL_CALL_ELIMINATION : _core.LLVMAddTailCallEliminationPass,
|
||||||
|
PASS_TAIL_DUPLICATION : _core.LLVMAddTailDuplicationPass,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -67,7 +172,7 @@ class PassManager(object):
|
||||||
elif tgt_data_or_pass_id in _pass_creator:
|
elif tgt_data_or_pass_id in _pass_creator:
|
||||||
self._add_pass(tgt_data_or_pass_id)
|
self._add_pass(tgt_data_or_pass_id)
|
||||||
else:
|
else:
|
||||||
raise LLVMException, "invalid pass_id"
|
raise llvm.LLVMException, ("invalid pass_id (%s)" % str(tgt_data_or_pass_id))
|
||||||
|
|
||||||
def _add_target_data(self, tgt):
|
def _add_target_data(self, tgt):
|
||||||
_core.LLVMAddTargetData(tgt.ptr, self.ptr)
|
_core.LLVMAddTargetData(tgt.ptr, self.ptr)
|
||||||
|
|
@ -77,7 +182,7 @@ class PassManager(object):
|
||||||
cfn(self.ptr)
|
cfn(self.ptr)
|
||||||
|
|
||||||
def run(self, module):
|
def run(self, module):
|
||||||
check_is_module(module)
|
core.check_is_module(module)
|
||||||
return _core.LLVMRunPassManager(self.ptr, module.ptr)
|
return _core.LLVMRunPassManager(self.ptr, module.ptr)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -85,7 +190,7 @@ class FunctionPassManager(PassManager):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def new(mp):
|
def new(mp):
|
||||||
check_is_module_provider(mp)
|
core.check_is_module_provider(mp)
|
||||||
return FunctionPassManager(_core.LLVMCreateFunctionPassManager(mp.ptr))
|
return FunctionPassManager(_core.LLVMCreateFunctionPassManager(mp.ptr))
|
||||||
|
|
||||||
def __init__(self, ptr):
|
def __init__(self, ptr):
|
||||||
|
|
@ -95,7 +200,7 @@ class FunctionPassManager(PassManager):
|
||||||
_core.LLVMInitializeFunctionPassManager(self.ptr)
|
_core.LLVMInitializeFunctionPassManager(self.ptr)
|
||||||
|
|
||||||
def run(self, fn):
|
def run(self, fn):
|
||||||
check_is_function(fn)
|
core.check_is_function(fn)
|
||||||
return _core.LLVMRunFunctionPassManager(self.ptr, fn.ptr)
|
return _core.LLVMRunFunctionPassManager(self.ptr, fn.ptr)
|
||||||
|
|
||||||
def finalize(self):
|
def finalize(self):
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,16 @@
|
||||||
from llvm import *
|
from llvm import *
|
||||||
from llvm.core import *
|
from llvm.core import *
|
||||||
from llvm.ee import *
|
from llvm.ee import *
|
||||||
|
from llvm.passes import *
|
||||||
|
|
||||||
ti = Type.int()
|
ti = Type.int()
|
||||||
|
|
||||||
|
|
||||||
def do_llvmexception():
|
def do_llvmexception():
|
||||||
print " Testing class LLVMException"
|
print " Testing class LLVMException"
|
||||||
e = LLVMException()
|
e = LLVMException()
|
||||||
|
|
||||||
|
|
||||||
def do_ownable():
|
def do_ownable():
|
||||||
print " Testing class Ownable"
|
print " Testing class Ownable"
|
||||||
o = Ownable(None, lambda x: None)
|
o = Ownable(None, lambda x: None)
|
||||||
|
|
@ -455,16 +458,15 @@ def do_executionengine():
|
||||||
bb = f.append_basic_block('entry')
|
bb = f.append_basic_block('entry')
|
||||||
b = Builder.new(bb)
|
b = Builder.new(bb)
|
||||||
b.ret(Constant.int(ti, 42))
|
b.ret(Constant.int(ti, 42))
|
||||||
|
ee.run_static_ctors()
|
||||||
gv = ee.run_function(f, [])
|
gv = ee.run_function(f, [])
|
||||||
is42 = gv.as_int() == 42
|
is42 = gv.as_int() == 42
|
||||||
ee.run_static_ctors()
|
|
||||||
ee.run_static_dtors()
|
ee.run_static_dtors()
|
||||||
ee.free_machine_code_for(f)
|
ee.free_machine_code_for(f)
|
||||||
t = ee.target_data
|
t = ee.target_data
|
||||||
m2 = Module.new('b')
|
m2 = Module.new('b')
|
||||||
mp2 = ModuleProvider.new(m2)
|
mp2 = ModuleProvider.new(m2)
|
||||||
# CRASHES!! TODO FIXME
|
ee.add_module_provider(mp2)
|
||||||
# ee.add_module_provider(mp2)
|
|
||||||
m3 = Module.new('c')
|
m3 = Module.new('c')
|
||||||
mp3 = ModuleProvider.new(m3)
|
mp3 = ModuleProvider.new(m3)
|
||||||
ee2 = ExecutionEngine.new(mp3, False)
|
ee2 = ExecutionEngine.new(mp3, False)
|
||||||
|
|
@ -477,11 +479,45 @@ def do_llvm_ee():
|
||||||
do_executionengine()
|
do_executionengine()
|
||||||
|
|
||||||
|
|
||||||
|
def do_passmanager():
|
||||||
|
print " Testing class PassManager"
|
||||||
|
pm = PassManager.new()
|
||||||
|
pm.add(TargetData.new(''))
|
||||||
|
for i in range(1, 10):
|
||||||
|
pm.add(i)
|
||||||
|
pm.run(Module.new('a'))
|
||||||
|
|
||||||
|
|
||||||
|
def do_functionpassmanager():
|
||||||
|
print " Testing class FunctionPassManager"
|
||||||
|
m = Module.new('a')
|
||||||
|
mp = ModuleProvider.new(m)
|
||||||
|
ft = Type.function(ti, [])
|
||||||
|
f = m.add_function(ft, 'func')
|
||||||
|
bb = f.append_basic_block('entry')
|
||||||
|
b = Builder.new(bb)
|
||||||
|
b.ret(Constant.int(ti, 42))
|
||||||
|
fpm = FunctionPassManager.new(mp)
|
||||||
|
fpm.add(TargetData.new(''))
|
||||||
|
# to be fixed.
|
||||||
|
# fpm.add(PASS_FUNCTION_INLINING)
|
||||||
|
fpm.initialize()
|
||||||
|
fpm.run(f)
|
||||||
|
fpm.finalize()
|
||||||
|
|
||||||
|
|
||||||
|
def do_llvm_passes():
|
||||||
|
print " Testing module llvm.passes"
|
||||||
|
do_passmanager()
|
||||||
|
do_functionpassmanager()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print "Testing package llvm"
|
print "Testing package llvm"
|
||||||
do_llvm()
|
do_llvm()
|
||||||
do_llvm_core()
|
do_llvm_core()
|
||||||
do_llvm_ee()
|
do_llvm_ee()
|
||||||
|
do_llvm_passes()
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue