compiler/ic/dce

  Source   Edit

Dead code elimination (=DCE) for IC.

For the lifetime-tracking hooks there exists a phase-ordering problem: we only know the set of used hooks after the MIR transformations are applied, but those are part of the code generation process and DCE is required to have happened prior.

The problem is currently solved by conservatively marking the types of all alive location definitions (locals, globals, constants, etc.) plus the parameter and return types of alive routines as alive (and with them, their attached operators). This tries to catch all types of which injectdestructors could inject hooks.

Types

AliveContext = object
  stack: seq[(int, TOptions, NodePos)] ## A stack for marking symbols as alive.
  decoder: PackedDecoder     ## We need a PackedDecoder for module ID address translations.
  thisModule: int            ## The module we're currently analysing for DCE.
  alive: AliveSyms           ## The final result of our computation.
  options: TOptions
  compilerProcs: Table[string, (int, int32)]
  graph: ModuleGraph         ## only used for lookup of type-bound operators
  
Purpose is to fill the 'alive' field.   Source   Edit
AliveSyms = seq[IntSet]
  Source   Edit

Procs

proc computeAliveSyms(g: PackedModuleGraph; graph: ModuleGraph; conf: ConfigRef): AliveSyms {.
    ...raises: [KeyError, Exception], tags: [ReadDirEffect, RootEffect].}
Entry point for our DCE algorithm.   Source   Edit
proc isAlive(a: AliveSyms; module: int; item: int32): bool {....raises: [],
    tags: [].}
Backends use this to query if a symbol is alive which means we need to produce (C/JS/etc) code for it.   Source   Edit