# Optimization¶

The IR-code generated by the front-end can be optimized in many ways. The compiler does not have the best way to optimize code, but instead has a bag of tricks it can use.

## Abstract base classes¶

The optimization passes all subclass one of the following base classes.

class ppci.opt.transform.ModulePass

Base class of all optimizing passes.

Subclass this class to implement your own optimization pass.

run(ir_module)

Run this pass over a module

class ppci.opt.transform.FunctionPass

Base pass that loops over all functions in a module

on_function(function: ppci.ir.SubRoutine)

Override this virtual method

run(ir_module: ppci.ir.Module)

Main entry point for the pass

class ppci.opt.transform.BlockPass

Base pass that loops over all blocks

on_block(block: ppci.ir.Block)

Override this virtual method

on_function(function)

Loops over each block in the function

class ppci.opt.transform.InstructionPass

Base pass that loops over all instructions

on_block(block)

Loops over each instruction in the block

on_instruction(instruction)

Override this virtual method

## Optimization passes¶

class ppci.opt.Mem2RegPromotor

Tries to find alloc instructions only used by load and store instructions and replace them with values and phi nodes

class ppci.opt.LoadAfterStorePass

Remove load after store to the same location.

[x] = a
b = [x]
c = b + 2


transforms into:

[x] = a
c = a + 2

class ppci.opt.DeleteUnusedInstructionsPass

Remove unused variables from a block

class ppci.opt.RemoveAddZeroPass

Replace additions with zero with the value itself. Replace multiplication by 1 with value itself.

class ppci.opt.CommonSubexpressionEliminationPass

Replace common sub expressions (cse) with the previously defined one.

class ppci.opt.cjmp.CJumpPass