5412ba62
by Mikolaj Konarski at 2026-11-20T00:02:39+01:00
Fix stale and false comments in the NCG
Of what anyone working with the NCG needs, this commit serves "Comments
and Notes that are true of the code they describe."
Comments and documentation only: the generated code is the same. Each
item names a comment, says what it said, and says what holds instead.
- The comment above genMachCode in GHC.CmmToAsm named stix code
and the UniqSM monad. The code is Cmm, and genMachCode switches
from the UniqDSM monad to NatM.
- NcgImpl's extractUnwindPoints, in GHC.CmmToAsm.Monad, placed
Note [Unwinding information in the NCG] in that module. The Note
is in GHC.CmmToAsm.
- The header of GHC.CmmToAsm.PIC named the modules CLabel, Cmm,
AsmCodeGen and MachCodeGen. It now names the modules and functions
that do what it lists. Two comments further down named MachCodeGen:
as calling cmmMakeDynamicReference for static data, which
the instruction selectors do, and as calling the initialization
of the PIC base register for every procedure that accessed it, which
cmmTopCodeGen in the x86 and PowerPC instruction selectors does,
but not on AIX. They now say so.
- The header of GHC.CmmToAsm.Reg.Target said that the module keeps
the NCG from emitting code for an architecture other than the host's.
Each of its functions dispatches on the target platform's
architecture, and the sentence is deleted.
- Note [CFG based code layout] in GHC.CmmToAsm.BlockLayout ran
optimizeCFG right after instruction selection. cmmNativeGen runs
it after register allocation and shortcutting. The Note presented
the next two steps, loopInfo's loop analysis and mkGlobalWeights'
refinement of the edge weights, as unconditional; optimizeCFG takes
them only under -fcmm-static-pred, which -O implies, and the Note now
says so. It said that the refined weights are normalized relative
to the most often visited node; they are relative to the entry node,
to which calcFreqs gives frequency 1.
- Note [CFG Edge Weights] in GHC.CmmToAsm.CFG gave the default weight
as 20k; defaultWeights has 1000. It said that likely branches
are taken more than 80% of the time and unlikely ones less than 20%;
the default weights give 75% and 25%. It said that the false side
of a regular branch is preferred; getCfg gives both sides the same
weight, the x86-64 instruction selector adds 3 to the false side
of a floating-point comparison, and optHsPatterns' favourFewerPreds
may then prefer one side by its predecessors. It said that calls
are ignored completely; the layout leaves a call's edge out while
it builds chains and while it combines neighbouring chains,
and divides its weight by 64 when it merges the chains.
- Note [Layout relevant edge weights] in GHC.CmmToAsm.BlockLayout also
said that edges constituted by a call are ignored. The layout ignores
them while it builds the chains and while it combines neighbouring
chains, and counts a 64th of their weight when it merges the chains.
- Note [Static Branch Prediction] in GHC.CmmToAsm.CFG named two
heuristics where staticBranchPrediction applies three, ohPredicts
being the third.
- In staticBranchPrediction, in GHC.CmmToAsm.CFG, a comment said that s1
is the branch in the true case; s1 is the successor with the smaller
label. The guard commented "Only backedges" is also reached by a back
edge beside a heap or stack check's edge, and its comment now says so.
- The comment above findBackEdges, in GHC.CmmToAsm.CFG, said
that it finds back edges by BFS. It finds them by a depth-first search
(classifyEdges in GHC.Data.Graph.Directed).
- The Instruction class asked regUsageOfInstr to state precisely
the registers an instruction reads and writes. x86's states them
as the register allocators are to see them, and the comment now says
so, with examples of where that departs from what the hardware does.
- The comment at the last arm of regUsageOfInstr's MOV case,
in GHC.CmmToAsm.X86.Instr, said that the MOV instructions reaching
it zero any remaining upper part of the destination. Some do, such
as a 32-bit MOV into a general register and a load at FF32 or FF64;
others keep it, such as a MOV between registers at FF32 or FF64,
printed movss or movsd, and an 8-bit or 16-bit MOV into a general
register. The comment now also says that the usage of the other MOV
instructions has a register destination written, not read.
- Comments in the liveness pass, in both register allocators and
at the Ord instances of CmmTickScope, LocalReg and VirtualReg said
that deterministic code generation is not supported, which
-fobject-determinism provides. In the liveness pass
and the allocators, each now says whether the order at hand matters
and, where it does, what it decides; at the Ord instances,
that the order compares uniques and so is deterministic only
if the uniques are.
- In GHC.CmmToAsm.Reg.Graph.SpillCost, a comment said that there
are no loops in the code, so that the block frequencies can be 1.
Given a CFG, slurpSpillCostInfo scales the uses and definitions
it counts by the CFG's block frequencies; the comment is deleted.
- linearRegAlloc's haddock and the comment over process said
that a block of an SCC is allocated before a jump into it only
if it is the procedure's first block; any of its entry blocks is.
The comment over process also said that process loops where a block's
assignment never gets set. It stops after a round that allocates
no block, and so drops such a block.
- findPrefRealReg's comment said that the preferred register
is the first one the variable was assigned to. It is the variable's
register in the first entry assignment, in allocation order, that maps
it to a register; a block's entry assignment is recorded at the first
jump into it.
- Two comments in GHC.CmmToAsm.Reg.Graph named the flag -fasm-lint,
which is -dasm-lint.
- Two comments in GHC.CmmToAsm.Reg.Linear.Base named the stats flag
-ddrop-asm-stats, which is -ddump-asm-stats.
- The executable specification of FreeRegs,
in GHC.CmmToAsm.Reg.Linear.FreeRegs, handed out the most recently
released registers first. It now gives noFreeRegs as [] and has
getFreeRegs hand out the registers in each instance's fixed order.
- The header of GHC.CmmToAsm.Reg.Linear.StackMap said that a slot stays
associated with its register to the end of the procedure. A register
that outgrows its slot moves to new ones; what holds is that no other
register gets the old slot.
- The comment at handleComponent's case for a cycle of moves,
in GHC.CmmToAsm.Reg.Linear.JoinToTargets, said that it spills all
the cycle's source registers. It spills one.
- makeRegMovementGraph's comment said that cyclic components seem
to occur only very rarely. They are frequent in some code:
in the benchmark of orthotope, a Haskell array library on Hackage,
compiled by a GHC build of 2026-10 whose linear allocator
was master's, the fixup code breaks 39 register cycles through a stack
slot in $w$s$wequalT1 and 59 in $w$s$w$ccompare1. The two counts
were not taken again on master. The sentence is deleted.
- A comment at expandNode's case for a move from InBoth to InMem,
in GHC.CmmToAsm.Reg.Linear.JoinToTargets, said that the source's slot
is guaranteed to be the destination's. They can differ where
the virtual register has more than one slot, and the value
is then stored from its register; the comment now says that the guard
fails only there.
- GHC.CmmToAsm.X86.Ppr's pprNatCmmDecl cited Note [Internal proc labels]
in GHC.Cmm.Label, across two lines. The Note is in GHC.Cmm.CLabel,
and the citation is now on one line.
- A comment in GHC.CmmToAsm.Reg.Linear.AArch64 said that getFreeRegs
scans the integer registers from 28 down. It starts from x18,
and the comment now also says that it starts from v31 among the v
registers.
- The comment at classOfRealReg in GHC.CmmToAsm.X86.Regs said "On x86"
of what holds on i386 alone, and numbered the registers with 8-bit
versions 1-4, and the allocatable ones among them 1, 3 and 4. It now
says "On i386", and they are 0-3, and 0, 2 and 3.
- The haddock of RegArch's Separate, in GHC.Platform.Reg.Class, gave
RISC-V as an example. registerArch gives RISC-V NoVectors
and no architecture Separate.
- The stmtToInstrs of X86, AArch64, LA64 and RV64 took the condition
of a conditional branch to be likely false, GHC.Cmm.ContFlowOpt having
arranged for the likely side to fall through. ContFlowOpt also inverts
a condition whatever its likelihood where the condition can
be inverted, the true target has one predecessor and the false target
more than one, so the condition may be likely true; none of the four
genCondBranch functions reads the likelihood.
- mkCmmIfThenElse', in GHC.StgToCmm.Monad, said that the NCG does
not know about likely annotations. The PowerPC code generator has
printed them as branch hints since 2017, and since 2018 getCfg has
weighted the CFG's edges by them, which on x86-64 the block layout
and the order of some conditional branches' jumps (invertCondBranches)
follow. Its clause on a fallthrough is replaced by what the swap does:
where the test can be inverted it becomes a likely-true test
of the inverse, and GHC.Cmm.ContFlowOpt swaps the branches back where
the true target, after shortcutting, has one predecessor.
- The users' guide said that -fcmm-static-pred's prediction is used
by the register allocation and code layout passes. The prediction
is made only in the x86-64 native code generator, and is used, after
register allocation and without -finfo-table-map, for the layout
and for the order of some conditional branches' jumps
(invertCondBranches). The entry now also says that on x86-64 the graph
colouring allocators (-fregs-graph, -fregs-iterative) weight the uses
and definitions they count by the same heuristics whatever the flag
says, without -finfo-table-map (slurpSpillCostInfo, mkGlobalWeights),
but choose what to spill by live range length alone (chooseSpill,
spillCost_length), so that the flag does not change their allocation.
- The users' guide's entry of -fcmm-sink said that the sinking pass
attempts to find identical Cmm blocks and eliminate the duplicates.
Those are words of the -fcmm-elim-common-blocks entry, run
into this entry's sentence since the two entries were written;
the sentence now says only that the pass attempts to move variable
bindings closer to their usage sites.
Assisted-by: Claude Opus 5.5