Mica 7.7.8 makes optimized code shorter on both processors and brings it closer again to the code GCC -O2 writes. A branch that only chooses which value a variable takes becomes a conditional move, a loop counted down to zero branches on its decrement’s flags, a routine that calls nothing keeps no frame, and parameters, narrow values and constants reach their readers without copies of their own. No routine grows for a choice it converts.

The address of a field or an element behind a nil pointer stops the program with nil_dereference where the address is taken, where it used to be handed on and written through. And the export lists every instruction at its program point. From the export, the Mica extension for VS Code draws a routine’s live ranges, one value followed through the control flow, and the heap’s ownership map.

A choice without a jump

{
    Choices - two functions whose branches only choose which value a variable takes.

    'Larger' keeps the larger of its two parameters, and 'Sign' answers one for a negative value and zero otherwise. Neither
    arm calculates, calls or writes memory, so at O1 and O2 each branch becomes a conditional move and the jump goes.
}
library Choices;

const
    Negative = 1;
    NotNegative = 0;

{ The larger of two values, kept in a local variable. }
exp function Larger(a : int64, b : int64) : int64;
var
    held : int64;
begin
    held := b;

    if a >= b then
        held := a;

    Larger := held;
end;

{ One for a negative value and zero for any other. }
exp function Sign(value : int64) : int64;
begin
    if value < 0 then
        Sign := Negative
    else
        Sign := NotNegative;
end;

begin
end.
mica --compile --optimize O2 --platform linux,arm64,utf-8 --source Choices.mica --build build

On AArch64, build/Choices.s holds Larger and Sign as three instructions each:

  cmp         x0, x1
  csel        x0, x0, x1, ge
  ret
  cmp         x0, #0
  cset        x0, lt
  ret

The same build for linux,amd64,utf-8 writes them for x86-64:

  mov         rax, rsi
  cmp         rdi, rax
  cmovge      rax, rdi
  ret
  test        rdi, rdi
  mov         rax, 1
  mov         r11, 0
  cmovge      rax, r11
  ret

7.7.7 wrote Larger in twelve instructions on AArch64 and fourteen on x86-64, with a frame, a copy of each parameter and a jump around the store. Its AArch64 version:

  stp         x29, x30, [sp, #-16]!
  mov         x29, sp
  sub         sp, sp, #16
  mov         x17, x0
  mov         x10, x1
  mov         x0, x10
  cmp         x17, x10
  b.lt        .Ll2.1
  mov         x0, x17
.Ll2.1:
  mov         sp, x29
  ldp         x29, x30, [sp], #16
  ret

At O1 and O2 a branch that only chooses which value a local variable takes is one conditional move where the variable lives in a register, and a parameter compared and answered on one of two arms is compared in the register it arrived in, x0 and x1 on AArch64 and rdi on x86-64. A variable that lives in the frame takes a plain store behind one jump. A choice between two literal values, such as Sign’s two constants, converts where the if compares integers or pointers and the variable is a 64-bit integer: a cset on AArch64, where an arm that stores zero reads it from xzr.

A narrower variable, a condition read from a boolean variable or answered by a call, and an arm that calculates keep their jump, as does an arm that divides, converts, calls, reads through a pointer or writes memory another routine can see. So a checked build still traps only on the road that asked for it. A choice turned into a conditional move is never longer than its branch was. Leaving SSA shows the conditional store the compiler writes for such a branch before it becomes the move.

Loops counted down to zero

{
    Countdown - a loop that counts its rounds down to zero.

    'SquaresDown' adds the squares of the count, the count less one and so on down to one. Each round ends in the
    decrement of 'remaining', and at O2 the loop branches on that decrement's own flags.
}
library Countdown;

{ The sum of the squares from the count down to one. }
exp function SquaresDown(count : int64) : int64;
var
    remaining : int64;
    total : int64;
begin
    remaining := count;
    total := 0;

    while remaining > 0 do
    begin
        total := total + remaining * remaining;
        remaining := remaining - 1;
    end;

    SquaresDown := total;
end;

begin
end.

At O2 on AArch64 each round is a madd, the decrement as subs, and one b.ne on the decrement’s flags:

  mov         x14, x0
  movz        x17, #0
  cmp         x14, #0
  b.le        .Ll2.3
.Ll2.2.rotated:
  madd        x17, x14, x14, x17
  subs        x14, x14, #1
  b.ne        .Ll2.2.rotated
.Ll2.3:
  mov         x0, x17
  ret

7.7.7 ended the round in sub x14, x14, #1, cmp x14, #0 and b.gt, comparing the counter with zero again. On x86-64 the round now ends in sub r9, 1 and jne, where 7.7.7 wrote sub r9, 1, test r9, r9 and jg. A counter stepped up by one from a negative start does the same. At O1 and O2 a result tested for zero right behind the addition, subtraction or negation that computed it, and on x86-64 behind a logical operation, takes no compare of its own.

A routine that calls nothing keeps no frame

{
    Leaf - a function that calls nothing and needs no stack.

    'Same' answers the value it is handed. It calls no routine and keeps nothing in memory, so at O1 and O2 it builds no
    frame on either processor.
}
library Leaf;

{ The value handed in, answered as it came. }
exp function Same(value : int32) : int32;
begin
    Same := value;
end;

begin
end.

At O1 and O2 Same is two instructions, on AArch64:

  mov         w0, w0
  ret

and on x86-64:

  mov         eax, edi
  ret

7.7.7 wrote eight instructions on AArch64 and seven on x86-64, most of them building and taking down a frame:

  stp         x29, x30, [sp, #-16]!
  mov         x29, sp
  sub         sp, sp, #16
  mov         x13, x0
  mov         w0, w13
  mov         sp, x29
  ldp         x29, x30, [sp], #16
  ret

A routine that calls nothing and never needs its stack drops its frame on both processors, as Larger, Sign and SquaresDown above did. Its call-frame information goes with the frame, so gdb and perf still unwind from such a routine to its caller, and a breakpoint in a body inlined into it still stops in the inlined frame.

Code closer to GCC -O2

At O1 and O2 the two ends of a copy that hold one value share a register, so a loop updates its values in place and saves fewer registers. A merge behind a branch the compiler decided reads the one value left, where it loaded the variable again. A string, a sum or a record read through a pointer is addressed through the register the pointer is in, with no copy of the pointer first, on both processors.

On x86-64 a narrow value is copied and a narrow constant written through the 32-bit register, mov eax, edi where it wrote mov al, dil, and the count of vector registers a call to a C routine with a variable argument list receives is xor eax, eax, where it was mov al, 0. An addition whose sum is only copied on is one lea in more places. On AArch64 a call’s result handed on as the next call’s first argument is not copied back into x0, a value loaded ahead of a record copied by a short loop goes straight into the register that reads it, a 32-bit copy read by a 64-bit reader folds into the reader, and a branch to the very next instruction goes, as on x86-64.

A choice at a routine’s end writes the register the routine answers in. This function answers a or b by comparing two float64 parameters:

{
    Ends - a choice at a routine's end, between two integers by a comparison of two floating-point values.

    'Choose' answers 'a' where 'x' is greater than 'y' and 'b' otherwise. The choice writes the register the function
    answers in.
}
library Ends;

{ The first integer where the first float is the greater, the second otherwise. }
exp function Choose(a : int32, b : int32, x : float64, y : float64) : int32;
var
    held : int32;
begin
    held := b;

    if x > y then
        held := a;

    Choose := held;
end;

begin
end.

On x86-64 at O2 it is four instructions, where 7.7.7 wrote thirteen with a saved register:

  mov         eax, esi
  ucomisd     xmm0, xmm1
  cmova       eax, edi
  ret

Integer identities leave no instruction at O1 and O2 on both processors: x + 0 and 0 + x, x - 0, x * 1 and 1 * x, x or 0, x xor 0 and x and -1 with the literal on either side, and a shift by a count of zero. Bit words and shifts of known integers are computed by the compiler at the result type’s width; a shift count past the width folds only in a build that checks no arithmetic, and the checked tiers keep the shift and its bit_shift_out_of_range trap. Floating point keeps its operations, for signed zero and signalling not-a-numbers. A float literal the register allocation cannot keep across calls is built again beside each instruction that reads it, never kept in a frame slot, so a routine of many such literals has a smaller frame.

A nil pointer is caught where a place’s address is taken

{
    NilField - the address of a field behind a nil pointer, lent to a routine that writes through it.

    'keeper.target' is nil, so 'keeper.target.b' names no place. The program takes that place's address and hands it to
    'Bump', which writes through it; the run stops where the address is taken.
}
program NilField;

imp
    WriteLn : std;

const
    AfterLine = "the write went through";

type
    { Two counters side by side. }
    Pair = record
        a : int64;
        b : int64;
    end;

    { A record that reaches a pair through a pointer. }
    Holder = record
        target : pointer Pair;
    end;

var
    keeper : Holder;

{ The value a pointer names grows by one, written back through the pointer. }
procedure Bump(counter : pointer int64);
begin
    value counter := value counter + 1;
end;

begin
    keeper.target := nil;
    Bump(address keeper.target.b);
    WriteLn(AfterLine);
end.
mica --compile --link --optimize O2 --source NilField.mica --build build
./build/NilField
Mica runtime failure: reason=nil_dereference (9)
Mica runtime context: file=NilField.mica, line=38, column=31
Mica runtime source:     Bump(address keeper.target.b);

The run stops at the statement that names the place, with exit status 1, at every tier on both processors. 7.7.7 formed an address from the nil pointer, handed it to Bump, and the program ended by a segmentation fault once Bump wrote through it, with no message, at every tier.

A test on an address that cannot be nil goes: the address of a name’s own storage, or of a place behind a pointer already tested. So a loop over an array of records in a frame runs without a compare per access, and a list walk tests its cursor once a round. A routine that only lends a place behind its pointer parameter pays for the test: a compare, a branch to a cold stub and, in a routine that kept no frame, a frame.

Every instruction at its program point

--export writes a new stage, 105.program-points: the unit the code generator compiles, one instruction per line in the order the unit holds it, with its program point, the value it defines, its source line and its Spectra text. Each call’s arguments stand at points of their own ahead of it. Export Choices from above:

mica --compile --optimize O2 --platform linux,arm64,utf-8 --source Choices.mica --build build --export

build/export/Choices.105.program-points lists Larger at points 4 to 16, behind the unit’s initialization at points 0 to 3, with the conditional store of line 21 at point 10. Its header and Larger:

program points of the unit the out-of-ssa listing prints
 point   value    line  instruction

function Choices.Larger: points 4 to 16
     4       -      17  @Choices__Larger__utf8,Choices.Larger:
     5       -      17      prologue @Choices.Larger, 1
     6       1      18      t2.1:int64 = loadVariable p2.2:int64 #1
     7       2      18      storeVariable t2.1:int64, v2.1:int64 #2
     8       3      20      t2.2:int64 = loadVariable p2.1:int64 #3
     9       4      20      t2.4:bool = greaterEqual t2.2:int64, t2.1:int64 #4
    10       5      21      storeVariableWhen t2.2:int64, t2.4:bool, v2.1:int64 #5
    11       -      20  @l2.1:
    12       6      23      t2.6:int64 = loadVariable v2.1:int64 #6
    13       7      23      storeVariable t2.6:int64, v2.2:int64 #7
    14       -      24  @Choices.Larger_leave:
    15       8      24      loadReturnVariable @Choices.Larger, v2.2:int64 #8
    16       -      24      epilogue @Choices.Larger

The live ranges of 070.liveness and the reservations of 110.placement count in these points. held is v2.1, and the liveness writes v2.1: [7, 13): it is live from its first store at point 7 up to its last read at point 12. A range or a reservation is now found in what a reader sees. The export page walks every stage of an export and the program points among them.

Three of the export’s other stages say more. The placement states under each function whose routine it is and what became of its code, routine of the unit, code inlined for one inlined into every caller, and its JSON twin says the same as origin, code and source. The memory stage names a generic record’s release by the name its cells are made under, Node__int32 where it printed Node.int32. The optimization ledger counts the conditional stores a build formed, and the out-of-SSA listing and every later stage print them by name.

In the editor

The Mica extension for VS Code 5.9.0 draws three pictures from the export.

Show Live Ranges draws how long each value of a routine lives: a lane for every parameter, variable and temporary over the routine’s program points, with a bar wherever the value is live, grouped under the source line each value begins on. In the picture, the routine’s SSA listing stands on the left and its lanes on the right, the variables in green and the temporaries in amber. A dotted stretch in a lane is a hole: a variable such as previous is not live between its last read and the store that writes it again.

Show Live Ranges beside a routine’s SSA listing: one lane per value over the program points, grouped under the source line it begins on, variables in green and temporaries in amber

A bar’s hover names the value, its range and the instructions at its two ends; a click moves the editor to the source lines the value lives across, and a point of the axis opens its instruction in the program points, which open in a view of their own.

Follow Value in Control Flow marks one value in the routine’s control-flow drawing. The arrow at the end of a lane, or a value named on a line of any listing, chooses it. Here previous is followed: the block that defines it and the block that defines and uses it stand in heavy outlines, the block it is live through without touching it is dotted, and each edge it is live along is drawn heavier with the word live. The header names the definitions that reach each use.

Follow Value in Control Flow: the live ranges on the left, and on the right the routine’s blocks with the followed variable’s definitions in heavy outlines, a block it is live through dotted and the edges it is live along drawn heavier

Show Ownership Map draws the shape of a unit’s heap as the compiler proved it before the program runs: a box for each type whose cells live on the heap, the sites that make its cells above its name, each with its line, its routine and what becomes of the cell, and the sites that release them below. In the picture, Order is made at line 46 and handed over to a new owner, and the int64 made at line 57 is released on every path by the deferred release at line 58.

Show Ownership Map beside a program: a box for Order and one for int64, each with the line that makes its cells and what becomes of them above the type’s name and the line that releases them below

An arrow from one box to another is a slot through which a cell owns another, so a tree’s node carries an arrow back to itself for its children. Every box, site and arrow opens its line of the source, and the export overview’s memory stage links to the map. All three drawings need the extension 5.9.0.

Fixed

  • A narrow integer made from a 32-bit array element, T[i] as int16, links at O2 on AArch64, where the assembler refused the reload the optimizer wrote through the element’s address with “invalid shift amount”.
  • An unsigned narrowing whose value is spilled at O2 on AArch64 and read straight back keeps its zero extension, where the bits above the narrow width survived and a comparison read them.
  • An O2,profiling build’s code is the release build’s to the byte again.

Upgrading

Things a 7.7.7 build meets differently in 7.7.8:

  • A program that takes the address of a field or an element behind a nil pointer stops there with reason=nil_dereference (9) and the line and column of the statement, at every tier on both processors, where it ended by a segmentation fault once a routine wrote through the address.
  • A routine that only lends a place behind its pointer parameter pays for the nil test: a compare, a branch to a cold stub and, in a routine that kept no frame, a frame.
  • No compile-time diagnostic is new or speaks differently.
  • Dragon SDK: Spectra gains the operations storeVariableWhen and storeVariableUnless, which store a value into a variable where a boolean is set or clear and leave the variable as it is otherwise; capi/include/mica_dragon.h names them DRG_OP_STORE_VARIABLE_WHEN and DRG_OP_STORE_VARIABLE_UNLESS. They appear only in the unit leaving static single assignment at O1 and O2, Mica’s own generator writes neither, and the validator holds a frontend’s use of them to a temporary, a boolean and a whole variable or parameter of an integer, boolean or pointer type. The runtime floor contract stays at version 9.

The packages and the commands that install them are on the install page.