Mercurial > lcfOS
view python/transform.py @ 175:a51b3c956386
Added function call in expressions
author | Windel Bouwman |
---|---|
date | Fri, 19 Apr 2013 22:15:54 +0200 |
parents | 3eb06f5fb987 |
children | 5fd02aa38b42 |
line wrap: on
line source
from ir import * # Standard passes: class FunctionPass: def run(self, ir): """ Main entry point for the pass """ for f in ir.Functions: self.onFunction(f) def onFunction(self, f): """ Override this virtual method """ raise NotImplementedError() class BasicBlockPass(FunctionPass): def onFunction(self, f): for bb in f.BasicBlocks: self.onBasicBlock(bb) def onBasicBlock(self, bb): """ Override this virtual method """ raise NotImplementedError() class InstructionPass(BasicBlockPass): def onBasicBlock(self, bb): for ins in iter(bb.Instructions): self.onInstruction(ins) def onInstruction(self, ins): """ Override this virtual method """ raise NotImplementedError() # Usefull transforms: class ConstantFolder(InstructionPass): def onInstruction(self, i): if type(i) is ImmLoad: i.target.constval = i.value elif type(i) is BinaryOperator: a = i.value1 b = i.value2 if hasattr(a, 'constval') and hasattr(b,'constval'): op = i.operation if op == '+': i2 = ImmLoad(i.result, a.constval + b.constval) i.Parent.replaceInstruction(i, i2) elif op == '*': i2 = ImmLoad(i.result, a.constval * b.constval) i.Parent.replaceInstruction(i, i2) elif op == '-': i2 = ImmLoad(i.result, a.constval - b.constval) i.Parent.replaceInstruction(i, i2) class DeadCodeDeleter(BasicBlockPass): def onBasicBlock(self, bb): def instructionUsed(ins): if len(ins.defs) == 0: # In case this instruction does not define any variables, assume it is usefull. return True for d in ins.defs: if d.IsUsed: return True return False bb.Instructions = list(filter(instructionUsed, bb.Instructions)) def isAllocPromotable(allocinst): # Check if alloc value is only used by load and store operations. assert type(allocinst) is Alloc for use in ai.value.used_by: print(use.user, use) if not type(use.user) in [Load, Store]: # TODO: check volatile return False otherUse = True return True class Mem2RegPromotor(FunctionPass): def onFunction(self, f): print(f)