optimization

package
v2.0.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LivenessAnalysisPassName = "liveness-analysis"
	LivenessAnalysis         = "liveness"
)
View Source
const ConstantPropagationPassName = "constant-propagation"
View Source
const PeepholePassName = "peephole"
View Source
const RegisterCoalescingPassName = "register-coalescing"

Variables

View Source
var (
	ErrCFGBuildFailed            = errors.New("failed to build control flow graph")
	ErrPassFailed                = errors.New("optimization pass failed")
	ErrMissingDependency         = errors.New("missing optimization pass dependency")
	ErrDependencyRefreshModified = errors.New("refreshed optimization dependency modified program")
)

Functions

func Run

func Run(program *bytecode.Program, level Level) error

Types

type Analyzer

type Analyzer struct {
	// contains filtered or unexported fields
}

Analyzer provides analysis capabilities for control flow graphs

func NewAnalyzer

func NewAnalyzer(cfg *ControlFlowGraph) *Analyzer

NewAnalyzer creates a new CFG analyzer

func (*Analyzer) CalculateDominators

func (a *Analyzer) CalculateDominators() map[int]*BasicBlock

CalculateDominators computes the dominator tree for the CFG A block A dominates block B if all paths from entry to B go through A

func (*Analyzer) FindBackEdges

func (a *Analyzer) FindBackEdges() [][2]*BasicBlock

FindBackEdges identifies back edges in the CFG (edges that create loops) A back edge is an edge from a node to one of its ancestors in a DFS traversal

func (*Analyzer) FindReachableBlocks

func (a *Analyzer) FindReachableBlocks() []*BasicBlock

FindReachableBlocks returns all blocks reachable from the entry block

func (*Analyzer) FindUnreachableBlocks

func (a *Analyzer) FindUnreachableBlocks() []*BasicBlock

FindUnreachableBlocks returns all blocks not reachable from the entry block

type BasicBlock

type BasicBlock struct {
	Instructions []bytecode.Instruction
	Successors   []*BasicBlock
	Predecessors []*BasicBlock
	ID           int
	Start        int
	End          int
}

BasicBlock represents a sequence of instructions with a single entry and exit point

func NewBasicBlock

func NewBasicBlock(id, start int) *BasicBlock

NewBasicBlock creates a new basic block with the given ID and start position

func (*BasicBlock) AddInstruction

func (bb *BasicBlock) AddInstruction(inst bytecode.Instruction)

AddInstruction adds an instruction to the basic block

func (*BasicBlock) AddSuccessor

func (bb *BasicBlock) AddSuccessor(succ *BasicBlock)

AddSuccessor adds a successor block to this block

func (*BasicBlock) IsTerminator

func (bb *BasicBlock) IsTerminator() bool

IsTerminator returns true if the block ends with a terminator instruction

func (*BasicBlock) String

func (bb *BasicBlock) String() string

String returns a string representation of the basic block

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder constructs a control flow graph from bytecode

func NewBuilder

func NewBuilder(program *bytecode.Program) *Builder

NewBuilder creates a new CFG builder for the given program

func (*Builder) Build

func (b *Builder) Build() (*ControlFlowGraph, error)

Build constructs the control flow graph for the program

type ConstantPropagationPass

type ConstantPropagationPass struct{}

ConstantPropagationPass performs a simple constant propagation and folding. It is conservative across control-flow merges: a register is considered constant only if all predecessors agree on the same constant value.

func (*ConstantPropagationPass) Name

func (p *ConstantPropagationPass) Name() string

func (*ConstantPropagationPass) Requires

func (p *ConstantPropagationPass) Requires() []string

func (*ConstantPropagationPass) Run

type ControlFlowGraph

type ControlFlowGraph struct {
	Entry  *BasicBlock   // Entry block (first instruction)
	Exit   *BasicBlock   // Exit block (virtual block representing program exit)
	Blocks []*BasicBlock // All basic blocks in the program
}

ControlFlowGraph represents the control flow structure of a bytecode program

func (*ControlFlowGraph) String

func (cfg *ControlFlowGraph) String() string

String returns a human-readable representation of the CFG

func (*ControlFlowGraph) ToDOT

func (cfg *ControlFlowGraph) ToDOT() string

ToDOT converts the CFG to Graphviz DOT format for visualization

type Level

type Level int
const (
	LevelNone Level = iota
	LevelBasic
	LevelFull
	LevelAggressive
)

type LivenessAnalysisPass

type LivenessAnalysisPass struct{}

LivenessAnalysisPass performs liveness analysis to determine which registers are live (potentially used) at each program point

func (*LivenessAnalysisPass) Name

func (p *LivenessAnalysisPass) Name() string

Name returns the pass name

func (*LivenessAnalysisPass) Requires

func (p *LivenessAnalysisPass) Requires() []string

func (*LivenessAnalysisPass) Run

Run executes liveness analysis on the program

type LivenessInfo

type LivenessInfo struct {
	LiveIn  map[int]bool // Registers live at block entry
	LiveOut map[int]bool // Registers live at block exit
	Use     map[int]bool // Registers used in block before definition
	Def     map[int]bool // Registers defined in block
}

LivenessInfo contains liveness information for a basic block

type Pass

type Pass interface {
	Name() string
	Requires() []string
	Run(ctx *PassContext) (*PassResult, error)
}

func NewConstantPropagationPass

func NewConstantPropagationPass() Pass

NewConstantPropagationPass creates a new constant propagation pass.

func NewLivenessAnalysisPass

func NewLivenessAnalysisPass() Pass

NewLivenessAnalysisPass creates a new liveness analysis pass

func NewPeepholePass

func NewPeepholePass() Pass

NewPeepholePass creates a new peephole pass.

func NewRegisterCoalescingPass

func NewRegisterCoalescingPass() Pass

NewRegisterCoalescingPass creates a new register coalescing pass

type PassContext

type PassContext struct {
	Program  *bytecode.Program
	CFG      *ControlFlowGraph
	Metadata map[string]any // Shared metadata between passes
}

type PassResult

type PassResult struct {
	Metadata map[string]any
	Modified bool
}

PassResult contains the result of running a pass

type PeepholePass

type PeepholePass struct{}

PeepholePass performs simple local rewrites to remove redundant instructions.

func (*PeepholePass) Name

func (p *PeepholePass) Name() string

Name returns the pass name.

func (*PeepholePass) Requires

func (p *PeepholePass) Requires() []string

Requires returns dependencies for the pass.

func (*PeepholePass) Run

func (p *PeepholePass) Run(ctx *PassContext) (*PassResult, error)

Run executes the peephole optimizations on the program.

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

Pipeline manages a sequence of passes to be executed on a program

func NewPipeline

func NewPipeline() *Pipeline

NewPipeline creates a new pass pipeline

func (*Pipeline) Add

func (p *Pipeline) Add(pass Pass)

Add adds a pass to the pipeline

func (*Pipeline) Run

func (p *Pipeline) Run(program *bytecode.Program) (*PipelineResult, error)

Run executes all passes in the pipeline

type PipelineResult

type PipelineResult struct {
	Modified bool
}

PipelineResult contains the results of running a pipeline

type RegisterCoalescingPass

type RegisterCoalescingPass struct{}

RegisterCoalescingPass performs register coalescing to reduce register usage by renumbering registers based on liveness intervals

func (*RegisterCoalescingPass) Name

func (p *RegisterCoalescingPass) Name() string

Name returns the pass name

func (*RegisterCoalescingPass) Requires

func (p *RegisterCoalescingPass) Requires() []string

func (*RegisterCoalescingPass) Run

Run executes register coalescing on the program

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL