Package org.ek9lang.compiler.phase9


package org.ek9lang.compiler.phase9
M - Intermediate Representation Analysis and Optimization.

This phase is designed to analyse and optimize the Intermediate Representation of the developed code.

While this could take quite a bit of effort, it's probably worth doing the basics and then consider what the final code generation layer will be. This could be Java byte code or LVM code.

Both of the above potential output forms have very good optimizers built in, so it might be worth depending on those initially.

IRAnalysis and IROptimisation are the main entry points for this phase.

  • Class
    Description
    THE record of which scopes are entered in which ACTIVATION during an IRStructureWalk: the operation body is one activation, and every per-iteration list (CompositeInstr.Activation.PER_ITERATION) starts a new one for as long as it is walked.
    THE static acyclicity of a generated aggregate for the cycle collector (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §4.2, §12.2 items 4-5, §14.2 S3).
    The SAFE_POINT after_threads of one instruction list (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, T10): after the exit of the innermost scope in this list that holds a call which starts runtime threads and finishes only once they have all stopped (StartsAndStopsRuntimeThreads) - an async pipeline's statement scope, the scope of a server's accept or serve.
    THE value each list of a chain hands to the chain's result: an arm's body result, the default's result - keyed by the list's identity (two equal lists are two places).
    THE back-edge placement of IR_OPTIMISATION: a point instruction at the end of every loop body of an operation - the back edge, reached once per iteration after the body's own scope has dropped its values - unless one of the same opcode is already last.
    The SAFE_POINT before_threads of one instruction list (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, S0b): ahead of each call that starts runtime threads (StartsRuntimeThreads).
    THE callback-reachable operations of a program: those the runtime may call - directly, or through other operations - while one of its C frames is live below them (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §14.3).
    THE symbol an IR call runs, read back from what the call names: its target type (a user construct of the program's IR first, else a type or function the program resolves by fully qualified name - a core type, a parameterisation), the method by its IR name (OperatorMap.resolvedMethodName(MethodSymbol), so _addAss finds +=) and the fully qualified types of its parameters; a function's _call is the function itself, and so is a function's <init> (a dynamic function's capture constructor is not a member symbol: the function stands for it, and it keeps every capture); an aggregate's <init> is the constructor taking exactly those parameter types.
    THE placement of CANCEL_POINT (EK9_BACKEND_IR_CONTRACTS.md Contract 24): an IR_OPTIMISATION pass over the ownership form, run after SafePointPlacement, which states which operations are callback-reachable.
    V-C (EK9_BACKEND_IR_CONTRACTS.md Contract 24), over the placed IR beside V-S: a CANCEL_POINT only in an operation CancelPointPlacement.isPlacedIn(OperationInstr) accepts - never in code no async task can run, nor in a class initialiser - and in every such operation at the end of every loop body (BackEdgePoints.isLoopBody(CompositeInstr.NestedList)), so no loop a task runs can keep it from stopping.
    THE reviewed clean runtime routines - a new row is a C review first.
    One reviewed route: the callee type, the method, its parameter types, and which argument (zero based, as CallInstr.getArguments()) carries the function value the routine calls back.
    THE census of one program's CONFINE placement (N-V V1; master plan row 5, M2): how many sites ConfinePlacement stamped, by the allocated type's fully qualified name, and whether the program's fuse (DerivationContradictions) tripped - the numbers the -v line and -Dek9.confine.print quote, kept on the program (CompilableProgram.getConfineCensus()) so a harness that compiled a program in-process can ask it, never a stderr parser.
    THE rule of which allocation sites a CONFINE follows (N-V V1 and V3; NV_PER_SITE_CONFINEMENT.md §2, the plan's §2.2 with the review's changes and the V3 holder rule re-argued in `nv_v23/DESIGN.md`; EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11 N-V) - what ConfinePlacement places and ConfineVerifier (V-N) demands exactly, one function over FACTS the IR already carries (PublishedValues' standing under D1-a: IR_OPTIMISATION computes no fact here).
    THE placement of CONFINE (N-V V1 and V3; NV_PER_SITE_CONFINEMENT.md §3.3): an IR_OPTIMISATION pass after SafePointPlacement and CancelPointPlacement and BEFORE PublishPlacement, over the ownership form, before the lowering.
    V-N (N-V V1 and V3; NV_PER_SITE_CONFINEMENT.md §3.3, the V-P discipline), two checks over every operation: exact: each instruction is immediately followed by exactly the CONFINEs ConfinedSites.decide(IRConstruct, OperationInstr) says, in order - reason and holder included - and every CONFINE is one of those.
    THE per-program FUSE of the CONFINE stamp (N-V V1, review 2026-11-13 F4 (a); option A, acceptance-neutral): the two cross-checks of phase 5's derived RETAINS against the per-value escape record, computed in IR_ANALYSIS's single-threaded tail over the records EscapeFacts has just stamped and kept on the program (CompilableProgram.getDerivationContradictions()) for IR_OPTIMISATION to read - a program with ANY finding gets no CONFINE anywhere (ConfinedSites).
    THE stamping of the per-value escape record (E1.1, 2026-11-12; EK9_IR_ESCAPE_LIFETIME_AND_SHARING_FACTS.md §2, the escape half): OperationEscapes over every operation of every construct, in module order, each value's EscapeMetaDataDetails set on the instruction that defines it.
    THE calls that hand values to runtime threads for the life of a server or of the program (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, S2): one handler serves every connection, the ready handler runs on its own thread while the caller serves, and the signal dispatcher keeps its handler until the program ends.
    MULTI THREADED over sources for the verifiers, then a SINGLE THREADED tail that stamps the whole-program facts.
    SINGLE THREADED.
    THE "this instruction makes a new, child-bearing object" test of the IR (NV_PER_SITE_CONFINEMENT.md §2.1; N-V V1): a constructor CALL with a result whose receiver is neither this nor super (a delegation makes nothing), a LIST_LITERAL or a DICT_LITERAL.
    THE "nothing can be kept into a value of this type" test by type NAME (N-V review 2026-11-13, F1): a value of a childless core type (AcyclicTypes.CHILDLESS_CORE_TYPES - String, Integer, Date, ...) holds no reference, so a view of a holder that turns out to be such a content leads nowhere, and the holder need not inherit what the content reaches (phase9/OperationEscapes' reverse propagation skips it; the census expects no more of it).
    THE "this instruction declares a parameter" test over an operation's IR: a CALLER-owned SLOT_DECLARE that is not the operation's returning variable (IsReturningSlot).
    THE "is this slot the operation's returning variable" test over an operation's IR: the name the operation's symbol gives its returning symbol (<- rtn as T?, or as the author named it), else rtn or _rtn (a generated operator's, where a capture is named rtn - D37) when the CALLER owns that slot in the body - a local the author happened to name rtn is none of these.
    The two ownership rules that need to know which instruction lists run once per loop ITERATION (an activation), not just which scope ids exist - EK9_IR_OWNERSHIP_MODEL.md §12, stage 0, stated in today's opcodes: A value is owned by the activation that makes it (model V-F, the A11 rule).
    THE variables an operation body's ownership form moves out of (the value of a consuming SLOT_ASSIGN).
    THE escape walk of one operation (E1.1, 2026-11-12; EK9_IR_ESCAPE_LIFETIME_AND_SHARING_FACTS.md §3): one pass over IRStructureWalk classifying every instruction by OPCODE in a switch with no default - an opcode this walk does not classify does not compile until someone says what it does with a reference - then a monotone fixpoint over the holders.
    THE findings of a per-body ownership check over every operation of a construct, each as "operation: problem", in operation order; an operation with no body has none.
    THE borrow rule of the ownership form, V-I (EK9_IR_OWNERSHIP_MODEL.md §8, EK9_IR_OWNERSHIP_STAGE4_DESIGN.md §6): no borrowed value is used after its source may have let it go.
    THE ownership lowering (EK9_IR_OWNERSHIP_MODEL.md §11.1): rewrites every ownership-form instruction into today's opcodes, so both back ends keep consuming exactly what they consume now.
    THE per-path rules of the ownership form (EK9_IR_OWNERSHIP_STAGE4_DESIGN.md): V-A - every OWNED value is consumed exactly once on every path to a normal exit and to every exceptional exit - and V-J - a path that ends in a Panic owes nothing.
    THE local rules of the ownership form (EK9_IR_OWNERSHIP_MODEL.md §11.2; stage-1 design §7) - the ones that need only the structured lists, not a per-path analysis: V-C only an OWNED value is consumed, not twice in one straight list, and a value MOVED on a path (by DROP or a [consume] assignment) is not used after it on that path - its count now belongs to where it went, which may release it (Swift's use-after-move rule); V-D a CAST forwards its operand's kind, and consumes an OWNED operand; V-E the arms of a chain hand back values of one kind; V-F DROP_AT_EXIT names a scope open in the current activation (the A11 rule); V-G a slot's owner is open (or the CALLER or OBJECT) where the slot is first filled - its declaration, or its first [init] assignment when it is declared before its owner is entered (Contract 3: a try's resources and its region's first failure) - a slot is declared once per activation, and a slot is never itself dropped; V-H a consumed argument is consumed by its call; an unmanaged result carries no count; V-M the IR is in the ownership form ONLY: RETAIN, RELEASE, RELEASE_FIELD and SCOPE_REGISTER are OwnershipLowering's output, never phase 7's, and REFERENCE / TYPED_REFERENCE declares only a compiler temporary that holds no count of its own - every named variable is a SLOT_DECLARE with its owner (stage 3).
    THE switches of IR_OPTIMISATION's passes (D3', Steve 2026-11-12; EK9_IR_OPTIMISATION_RAILS_AND_REWRITES.md A.1, item 3 - one system property, not the pass manager): -D"ek9.opt.disable"=<pass>[,<pass>] names the placement or rewrite passes IR_OPTIMISATION leaves out, so a differential leg can run a program with and without one pass and a native defect can be attributed to it.
    THE CalleeOfCall of a whole program: over every aggregate and function the program knows by fully qualified name - core ones included - and every construct in its IR (a parameterisation such as _List_<digest> is found there, not by a search for its name), then a fully qualified search.
    THE ConfinedSites of a whole program (N-V V1; mirror of ProgramPublishedValues): the emission filter's type facts by fully qualified name and the program's fuse.
    THE PublishedValues of a whole program (S2b): the registered-type fact by name - every aggregate the program knows, core ones included (a List of String a registered component holds is registered; one nothing published reaches is not), and every construct in its IR, read through IsRegisteredType - failing closed for a name it does not know; and THE program's callee resolution (ProgramCalleeOfCall).
    THE rule of which values an instruction publishes (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, S2 and S2b) - what PublishPlacement places and PublishVerifier (V-P) demands exactly: a STATIC_FIELD_ASSIGN into _Application - an application's _prepare storing a registration runtime threads can reach, a [registered] type (REGISTRY; S2c - one only the program's own thread uses stays mortal); any other static store - an enumeration's values, a cycle-guard owner, all in a _c_init (CONSTANT); a call that hands values to runtime threads (HandsValuesToRuntimeThreads, HANDLER); the store barrier (STORE, naming the holder the value goes into): a FIELD_ASSIGN into a field declared by a type a published value may reach, or a SLOT_ASSIGN this.<property> in an operation of a [registered] construct - except into this while it is being constructed (a constructor, _i_init, _c_init: E08250 keeps this from escaping, so nothing published holds it yet); and an argument a call may KEEP in its receiver or in another argument (KeptArgumentHolders, from the callee's RETAINS) when that holder's type is one a published value may reach - items += line on a registered component's List (review 2026-11-02, M2) - as well as the operand of a :=:, :~: or :^: into such a receiver, which shares the operand's contents. "A type a published value may reach" is the [registered] fact by name, failing closed: a type the program does not know to be unreached is reached.
    Where an instruction is: in an operation of a registered construct or not, and while its object is still being constructed or not (a constructor, _i_init, _c_init).
    THE placement of PUBLISH (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, S2): an IR_OPTIMISATION pass after SafePointPlacement, over the ownership form, before the lowering.
    V-P (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, S2 and S2b), two checks over every operation: exact: each instruction is immediately preceded by exactly the PUBLISHes PublishedValues says, in order, holders included; and every PUBLISH is one of those.
    Every reference an instruction stores INTO an object - the edges a published object could gain - classified by OPCODE, for V-P's audit (PublishVerifier, review 2026-11-02 M4).
    A reference to value stored into holder: a variable, IRConstants.THIS, or null for a static.
    THE backend-independent verifier of the ARC RETAIN/RELEASE/SCOPE balance invariant for the +1 ownership-transfer convention, over the LOWERED form - model rule V-L, run by OwnershipLowering on every construct it lowers (EK9_IR_OWNERSHIP_MODEL.md §11.2, stage 3).
    A single balance finding for one construct/operation.
    THE placement of SAFE_POINT (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §14.4, S2): the first IR_OPTIMISATION pass, over the ownership form, before the lowering.
    V-S (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §14.5): a SAFE_POINT only in an operation that is not callback-reachable - a collection under a runtime frame could free what that frame holds uncounted.
    THE short text of an instruction named in an ownership finding: a composite by its opcode, anything else by its IR text without the debug comment and the purity annotation, cut at 110 characters - so a finding shows the instruction's shape without the operation's IR.
    THE calls that start runtime threads and finish only once every one of them has stopped (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, T10): a server's accept, acceptUntilComplete, serve and serveUntilComplete return after shutting down the scheduler they started, and an async pipeline's Ek9Async.create() begins the statement whose close() drains its workers and does the same.
    THE calls that start runtime threads (EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §13.11, S0b): every call StartsAndStopsRuntimeThreads names - an async pipeline's Ek9Async.create(), a server's accept and serve, each of which starts the carrier scheduler - and Signals.register, which starts the signal dispatcher that never stops.
    THE stamping of the whole-program FACTS the IR states (D1-a, Steve 2026-11-12; EK9_ESCAPE_MUTATION_ANALYSIS_POSITION.md §3, EK9_NATIVE_CYCLE_COLLECTOR_DESIGN.md §14.2): on every construct [acyclic] (AcyclicTypes), [confined] (IsConfinedType, decided in PRE_IR_CHECKS) and [registered] (IsRegisteredType, likewise), and on every operation [callback] (CallbackReachableOperations).