@asmlift/core 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/README.md +25 -24
  2. package/package.json +1 -1
  3. package/src/aggregate.ts +149 -0
  4. package/src/backend/c.ts +1 -0
  5. package/src/backend/cfamily.ts +165 -50
  6. package/src/backend/cpp.ts +2 -1
  7. package/src/backend/pascal.ts +31 -12
  8. package/src/contracts.ts +173 -16
  9. package/src/declare.ts +69 -20
  10. package/src/frontend/asmdata.ts +7 -1
  11. package/src/frontend/call-plan.ts +964 -60
  12. package/src/frontend/dead-reads.ts +628 -0
  13. package/src/frontend/device-pins.ts +23 -4
  14. package/src/frontend/emit.ts +4 -3
  15. package/src/frontend/fpu.ts +224 -17
  16. package/src/frontend/frame-objects.ts +1232 -192
  17. package/src/frontend/frontend.ts +64 -3
  18. package/src/frontend/high-half.ts +53 -4
  19. package/src/frontend/jump-table.ts +530 -0
  20. package/src/frontend/local-object.ts +158 -32
  21. package/src/frontend/mips.ts +2076 -215
  22. package/src/frontend/own-struct-return.ts +500 -0
  23. package/src/frontend/ppc.ts +2711 -232
  24. package/src/frontend/register-struct-call.ts +484 -0
  25. package/src/frontend/reloc-symbol.ts +22 -7
  26. package/src/frontend/ssa.ts +1460 -44
  27. package/src/frontend/stackargs.ts +106 -57
  28. package/src/frontend/thumb.ts +2044 -318
  29. package/src/ir/alias.ts +42 -0
  30. package/src/ir/bits.ts +681 -1
  31. package/src/ir/core.ts +100 -5
  32. package/src/ir/discipline.ts +86 -21
  33. package/src/ir/entry.ts +159 -0
  34. package/src/{structure → ir}/loops.ts +6 -2
  35. package/src/ir/opcodes.ts +109 -9
  36. package/src/ir/types.ts +86 -2
  37. package/src/ir/verify.ts +80 -11
  38. package/src/l3/ast.ts +290 -18
  39. package/src/l3/basecse.ts +33 -5
  40. package/src/l3/constinit.ts +382 -0
  41. package/src/l3/dce.ts +2 -2
  42. package/src/l3/initfirst.ts +42 -11
  43. package/src/l3/intcell.ts +2 -1
  44. package/src/l3/narrowdecl.ts +77 -36
  45. package/src/l3/pollguard.ts +8 -2
  46. package/src/l3/ptrcell.ts +31 -34
  47. package/src/l3/ptrfield.ts +123 -8
  48. package/src/l3/regspell.ts +9 -4
  49. package/src/l3/reindex.ts +283 -96
  50. package/src/l3/storage.ts +98 -1
  51. package/src/l3/symbol-refs.ts +84 -6
  52. package/src/l3/tailmerge.ts +4 -2
  53. package/src/l3/typing.ts +108 -15
  54. package/src/l3/unreduce.ts +6 -5
  55. package/src/l3/volatileptr.ts +17 -54
  56. package/src/mangle.ts +30 -0
  57. package/src/pattern/engine.ts +361 -73
  58. package/src/pipeline.ts +541 -24
  59. package/src/proto-context.ts +905 -45
  60. package/src/proto.ts +555 -40
  61. package/src/raise/aggmembers.ts +290 -0
  62. package/src/raise/arrays.ts +5 -1
  63. package/src/raise/declared-volatile.ts +207 -31
  64. package/src/raise/floathelpers.ts +254 -33
  65. package/src/raise/gvn.ts +3 -3
  66. package/src/raise/latch.ts +162 -6
  67. package/src/raise/memberarrays.ts +10 -4
  68. package/src/raise/offsetnames.ts +10 -26
  69. package/src/raise/pairparams.ts +33 -0
  70. package/src/raise/paramwidth.ts +196 -6
  71. package/src/raise/pre-recovery.ts +102 -19
  72. package/src/raise/pre-structure.ts +54 -0
  73. package/src/raise/recover.ts +12 -5
  74. package/src/raise/reroll.ts +694 -0
  75. package/src/raise/shortcircuit.ts +26 -12
  76. package/src/raise/softdiv.ts +1 -1
  77. package/src/raise/storesign.ts +153 -0
  78. package/src/raise/struct-arrays.ts +30 -11
  79. package/src/raise/structs.ts +396 -33
  80. package/src/raise/truncload.ts +4 -3
  81. package/src/raise/unthread.ts +400 -0
  82. package/src/rank-declare.ts +118 -12
  83. package/src/rank-variations.ts +89 -31
  84. package/src/rank.ts +307 -54
  85. package/src/runtime-helpers.ts +140 -22
  86. package/src/structure/analysis.ts +165 -2
  87. package/src/structure/arith-ops.ts +23 -0
  88. package/src/structure/globalaccess.ts +111 -9
  89. package/src/structure/hazards.ts +16 -64
  90. package/src/structure/local-statics.ts +145 -9
  91. package/src/structure/namecoalesce.ts +1 -1
  92. package/src/structure/pointer-spelling.ts +1106 -0
  93. package/src/structure/redundant-test.ts +1 -1
  94. package/src/structure/structure.ts +2180 -1054
  95. package/src/structure/switch-recover.ts +244 -100
  96. package/src/symbols.ts +6 -0
  97. package/src/target.ts +765 -52
  98. package/src/trace.ts +26 -8
  99. package/src/variation-definitions.ts +126 -14
  100. package/src/variation-gates.ts +2 -0
  101. package/src/variation-tokens.ts +5 -1
@@ -100,6 +100,7 @@
100
100
  // the map spells two ways at either end, a width the map does not state, a width that disagrees
101
101
  // with the access, a store onto a `const` name — refuse NOTHING here and are pinned by
102
102
  // `offset-names.test.ts` alone.
103
+ import { constDisplacementOf } from '../ir/alias';
103
104
  import { type Fn, type Op, type Value, defOpMap } from '../ir/core';
104
105
  import { verify } from '../ir/verify';
105
106
  import { type Gate, firstRejection } from '../l3/gates';
@@ -317,33 +318,16 @@ function addressesByName(symbols: SymbolMap): Map<string, { addr: number; info:
317
318
  }
318
319
 
319
320
  /** The named base an address value stands on, following `add`/`sub` chains through constants —
320
- * the `adds r1,#12` walk is a chain of them, and each link is its own candidate. Stops at
321
- * anything else, block parameters included (they have no def, so a loop-carried pointer
322
- * induction never resolves). */
321
+ * the `adds r1,#12` walk is a chain of them, and each link is its own candidate. The chain is
322
+ * ir/alias.ts `constDisplacementOf`, the reading raise/structs.ts shares; it stops at anything
323
+ * else, block parameters included (they have no def, so a loop-carried pointer induction never
324
+ * resolves). */
323
325
  function namedBase(v: Value, defOf: Map<Value, Op>): { sym: string; code: boolean; offset: number } | null {
324
- const d = defOf.get(v);
325
- if (d === undefined) {
326
- return null;
327
- }
328
- if (d.opcode === 'gaddr') {
329
- return { sym: d.attrs.sym as string, code: d.attrs.code === true, offset: 0 };
330
- }
331
- if (d.opcode !== 'add' && d.opcode !== 'sub') {
332
- return null;
333
- }
334
- const [a, b] = d.operands;
335
- const ka = defOf.get(a)?.opcode === 'const' ? (defOf.get(a)!.attrs.value as number) : null;
336
- const kb = defOf.get(b)?.opcode === 'const' ? (defOf.get(b)!.attrs.value as number) : null;
337
- // `sub` is not commutative: only `base - K` is an offset off the base.
338
- if (kb !== null) {
339
- const base = namedBase(a, defOf);
340
- return base && { ...base, offset: base.offset + (d.opcode === 'sub' ? -kb : kb) };
341
- }
342
- if (ka !== null && d.opcode === 'add') {
343
- const base = namedBase(b, defOf);
344
- return base && { ...base, offset: base.offset + ka };
345
- }
346
- return null;
326
+ const disp = constDisplacementOf(defOf, v);
327
+ const d = defOf.get(disp?.root ?? v);
328
+ return d?.opcode === 'gaddr'
329
+ ? { sym: d.attrs.sym as string, code: d.attrs.code === true, offset: disp?.k ?? 0 }
330
+ : null;
347
331
  }
348
332
 
349
333
  /** Every `add`/`sub` in `fn` that resolves to a named base plus a non-zero constant, with the
@@ -14,6 +14,9 @@
14
14
  // 64-bit value; it is a word this function uses as a word, which is what a 64-bit argument passed
15
15
  // to a helper and separately tested would look like. Fusing it would delete a use.
16
16
  import { Fn, Op, Value } from '../ir/core';
17
+ import { intWidth } from '../ir/types';
18
+ import { type FnProto, declaredWidth } from '../proto';
19
+ import { RaiseUnsupportedError } from './errors';
17
20
 
18
21
  /** Count every operand and edge-argument occurrence of each value. Occurrences, not sites: a value
19
22
  * passed to `f(x, x)` is used twice, and a rule that said "used once" about it would be wrong. */
@@ -79,3 +82,33 @@ export function fuseParamPairs(fn: Fn): boolean {
79
82
  }
80
83
  return changed;
81
84
  }
85
+
86
+ /** A FUNCTION DECLARED WITH AN INTEGER PARAMETER WIDER THAN A REGISTER, whose lift reads that
87
+ * parameter's words apart, refuses — on every frontend, since this reads only the entry block
88
+ * `fuseParamPairs` leaves. The return's twin is pipeline.ts `refuseNarrowReturnsOfWideDeclaration`.
89
+ *
90
+ * The pair is fused only where the lift built its `concat` (a helper call, a carry chain); a body
91
+ * that tests or combines the two words itself (`return a != 0;` is `or` of the halves on every
92
+ * compiler) leaves two word parameters, and the lift prints `s32 f(s32 a0, s32 a1)` against the
93
+ * caller's own `int f(long long a)` — a candidate the compiler rejects as a redeclaration. It is
94
+ * correct C under the ABI, and the wrong function for the declaration in hand. Counting is
95
+ * enough: every fused parameter is wider than a register and only `fuseParamPairs` makes one, so
96
+ * fewer of them than the declaration names means a declared one was left in halves. A
97
+ * declaration the caller does not state (a context's own entry is left out,
98
+ * proto-context.ts `withContextPrototypes`) asks nothing here. */
99
+ export function refuseSplitWideParams(fn: Fn, self: FnProto | undefined): void {
100
+ const declared = Array.isArray(self?.params) ? self.params : [];
101
+ const wide = declared.filter((t) => (declaredWidth(t) ?? 0) > 32);
102
+ if (wide.length === 0) {
103
+ return;
104
+ }
105
+ const fused = fn.blocks[0].params.filter((p) => (intWidth(p.type) ?? 0) > 32).length;
106
+ if (fused < wide.length) {
107
+ const at = declared.findIndex((t) => (declaredWidth(t) ?? 0) > 32);
108
+ throw new RaiseUnsupportedError(
109
+ `cannot lift '${fn.name}': its declaration takes parameter ${at + 1} as '${declared[at]}', and this lift ` +
110
+ 'reads the two halves of a 64-bit value as two word parameters (a 64-bit parameter is one only where the ' +
111
+ 'lift pairs its halves)',
112
+ );
113
+ }
114
+ }
@@ -122,11 +122,46 @@
122
122
  // pool load in the benchmark's agbcc references (extscale.ts's header has the census). Only the
123
123
  // fold's own extensions are judged this way; a plain cast's extension sits at its right shift,
124
124
  // where its position says nothing about its `lsl`'s.
125
- import { type Fn, type Op, type Value, replaceAllUsesWith, successorsOf } from '../ir/core';
125
+ //
126
+ // EVERY READER AN EXTENSION, the `/narrow-param` lift variation (`narrowExtendedParams`). agbcc does
127
+ // not always widen a narrow declared parameter in the prologue: where every use of it is itself an
128
+ // extension it widens at each use instead, and then the object carries no declaration to read.
129
+ // `pokeemerald:LoadMonInfo` declares `s16 partyId` and reads it as `lsl #16` behind a pool load, an
130
+ // `asr #14` for the scaled index and an `lsr #16` for a `u16` argument, so `raw-reader` refuses it
131
+ // (two readers), and with that ablated `not-prologue` (behind the pool load) and `fused-behind-pool`.
132
+ // Declared `s16` and read `a << 2` and `(u16)a`, or declared `s32` and read `(s16)a << 2` and
133
+ // `(u16)a`, the two are one object in isolation and differ only through the register allocation
134
+ // around them (a sibling `u8` parameter's prologue copy), so nothing in the asm decides it and the
135
+ // differ referees. Declaring the parameter at width w changes no reader's value when every reader is
136
+ // an extension to w' ≤ w: ext_w'(trunc_w x) = ext_w'(x). The width is the widest reader's, its
137
+ // signedness the caller's declaration where that spells one and the first reader of that width's
138
+ // otherwise; each reader of exactly that type becomes the parameter itself. Where no prototype
139
+ // declares the width, the candidate loses every score tie (rank.ts `guessesParamWidth`): without
140
+ // a sibling to separate them the two declarations are often one object, and the narrow one costs
141
+ // each prototyped caller the truncation `proto-width` is about.
142
+ //
143
+ // A LOW MASK IS AN EXTENSION READER. mwcc reads a `u16` parameter as `clrlwi rD,rS,16`, which the
144
+ // PPC frontend lifts as `and rS, 0xffff` — as it must, since the same instruction is every other
145
+ // mask too — so a parameter mwcc reads only through it reaches this pass with no `zext` reader at
146
+ // all. `and v, 0xffff` is `zext v {width=16}` value for value, so it counts as one (`extensionOf`),
147
+ // unsigned, and becomes the parameter itself when the parameter is declared `u16`. The two
148
+ // declarations are not one object there: over a `long *a`, mwcc_242_81 compiles
149
+ // `*a = *a & ~b; *a = *a | c;` to `not; and` when `b` and `c` are `unsigned short`, and to `andc` when
150
+ // they are `unsigned long` and the body masks them, at `-O4,p` and at Mario Party 4's unit flags.
151
+ //
152
+ // The rule is about the IR, not the ISA, so it reaches every frontend that lifts a mask as `and`.
153
+ // IDO reads a declared `u16`/`u8` parameter as `sw a0,0(sp); andi a0,a0,0xffff`, and frontend/mips.ts
154
+ // lifts the `andi` as that mask too: `s32 f(u16 a, s32 b){ return a + b; }` scores 3 wide and 0
155
+ // narrowed. The witness gates still decide whether IDO may narrow: a mask applied in place with no
156
+ // home store (`a = a & 0xffff`) is refused by `unhomed-param`. agbcc spells a mask as `and` with a
157
+ // pooled `0xffff` or a `mov #0xff`, so the variation is offered there too, and loses, because agbcc
158
+ // extends a declared narrow parameter with a shift pair: `s32 f(u32 a, s32 b){ return (a & 0xffff)
159
+ // + b; }` scores 0 wide and 6 narrowed.
160
+ import { type Fn, type Op, type Value, defOpMap, replaceAllUsesWith, successorsOf } from '../ir/core';
126
161
  import { CAST_WIDTHS, MATERIALIZING_OPS } from '../ir/opcodes';
127
162
  import { T } from '../ir/types';
128
163
  import { type Gate, firstRejection } from '../l3/gates';
129
- import { type FnProto, declaredWidth } from '../proto';
164
+ import { type FnProto, declaredSigned, declaredWidth } from '../proto';
130
165
  import type { NarrowParamWitness } from '../target';
131
166
 
132
167
  /** What the gates below judge: one entry parameter and the extension that reads it. */
@@ -141,6 +176,8 @@ export interface NarrowParamCandidate {
141
176
  inPrologue: boolean;
142
177
  /** reads of the RAW parameter anywhere in the function */
143
178
  uses: number;
179
+ /** reads of it that are not an extension to `width` or narrower */
180
+ wideReaders: number;
144
181
  /** the width the caller's own prototype declares for this parameter, if it declares one */
145
182
  declared: number | undefined;
146
183
  /** the extension is one raise/extscale.ts re-split from a fused pair whose `shl` the machine ran
@@ -192,14 +229,14 @@ export const PARAM_WIDTH_GATES: readonly Gate<NarrowParamCandidate>[] = [
192
229
  },
193
230
  {
194
231
  id: 'not-prologue',
195
- why: 'an extension behind body code is where the SOURCE wrote the cast — and on a schedule that interleaves them, the refusing answer',
232
+ why: 'an extension behind body code may be the source writing the cast, and nothing in the fan referees a narrow signature against the wide one',
196
233
  sound: true,
197
234
  guardedBy: 'param-width.test.ts: an extension behind a nullary call is body code',
198
235
  rejects: (c) => !c.inPrologue,
199
236
  },
200
237
  {
201
238
  id: 'no-declaration-witness',
202
- why: 'this compiler spells a narrow declaration and a body cast the same way, so the asm decides nothing',
239
+ why: 'the compiler spells a narrow declaration and a body cast the same way, so the assembly decides nothing',
203
240
  sound: true,
204
241
  guardedBy:
205
242
  'param-width.test.ts: a compiler whose two spellings are one object refuses the narrowing, measured or not',
@@ -207,14 +244,14 @@ export const PARAM_WIDTH_GATES: readonly Gate<NarrowParamCandidate>[] = [
207
244
  },
208
245
  {
209
246
  id: 'unhomed-param',
210
- why: 'this compiler homes a narrow DECLARED parameter, so the absent home store proves the declaration was wide',
247
+ why: 'the compiler stores a narrow declared parameter to the stack on entry, so one it did not store was declared wide',
211
248
  sound: true,
212
249
  guardedBy: 'param-width.test.ts: a homing compiler refuses the parameter it did not home',
213
250
  rejects: (c) => c.witness === 'home-store-and-in-place' && !c.homed,
214
251
  },
215
252
  {
216
253
  id: 'widened-elsewhere',
217
- why: 'this compiler widens a narrow DECLARED parameter in the argument register itself, so a widening that lands in a scratch register is not a declaration',
254
+ why: 'the compiler widens a narrow declared parameter in its own argument register, so a widening into another register is no declaration',
218
255
  sound: true,
219
256
  guardedBy:
220
257
  'param-width.test.ts: \u2026and refuses one it homed but widened SOMEWHERE ELSE \u2014 that is a 64-bit half',
@@ -229,6 +266,96 @@ export const PARAM_WIDTH_GATES: readonly Gate<NarrowParamCandidate>[] = [
229
266
  },
230
267
  ];
231
268
 
269
+ const byId = (id: string): Gate<NarrowParamCandidate> => PARAM_WIDTH_GATES.find((g) => g.id === id)!;
270
+
271
+ /** `/narrow-param`'s table (`narrowExtendedParams`): a second CONSUMER of the default's rules, not an
272
+ * ablation of its table, so it is built by id rather than through `ablateHeuristic`. It leaves out
273
+ * the three rules that read the prologue's evidence and adds the one that reads the readers.
274
+ * `raw-reader` gives way to `wide-reader`, which is sound for the same reason it is: every reader
275
+ * is an extension the pass rewrites. `not-prologue` and `fused-behind-pool` read fields this pass
276
+ * does not observe (it fills `inPrologue` and `fusedBehindPool` false). Their `sound` is a claim
277
+ * about the DEFAULT, whose narrowing reaches every candidate with no wide sibling, so a wrong width
278
+ * there is a signature nothing referees and every prototyped caller pays for. A /narrow-param
279
+ * candidate keeps the wide default beside it and loses every tie unless a prototype declares the
280
+ * width (rank.ts `guessesParamWidth`), so for it the same question is refereed: a rule sound for a
281
+ * declaration is a heuristic for a generated candidate (l3/gates.ts, `ORDER_SHAPE_GATES`).
282
+ * param-width.test.ts holds the set left out to exactly these three, so a sound rule added to the
283
+ * default is either added here or argued out by name. */
284
+ export const PARAM_READER_WIDTH_GATES: readonly Gate<NarrowParamCandidate>[] = [
285
+ byId('entry-is-join'),
286
+ byId('param-typed'),
287
+ byId('cast-width'),
288
+ {
289
+ id: 'wide-reader',
290
+ why: 'a reader that is not an extension to this width or narrower reads bits a narrow declaration drops',
291
+ sound: true,
292
+ guardedBy: 'param-width.test.ts: a reader that is no narrow extension keeps the parameter wide',
293
+ rejects: (c) => c.wideReaders !== 0,
294
+ },
295
+ byId('proto-width'),
296
+ byId('no-declaration-witness'),
297
+ byId('unhomed-param'),
298
+ byId('widened-elsewhere'),
299
+ ];
300
+
301
+ const isExtension = (op: Op): boolean => op.opcode === 'sext' || op.opcode === 'zext';
302
+
303
+ /** The unsigned extension a MASK spells: `and v, 0xff` is `zext v {width=8}` and `and v, 0xffff` is
304
+ * `zext v {width=16}`, value for value. */
305
+ const MASK_WIDTHS: ReadonlyMap<number, number> = new Map([
306
+ [0xff, 8],
307
+ [0xffff, 16],
308
+ ]);
309
+
310
+ /** What `op` reads `v` as, when it reads it ONLY through an extension: its width and signedness.
311
+ * An `and` with a low mask counts, because that is the extension's spelling where the machine
312
+ * has no extension of its own to lift: PPC's `clrlwi rD,rS,16` and MIPS's `andi rD,rS,0xffff`
313
+ * (each frontend lifts it as the mask, and must, because every other mask is the same instruction).
314
+ * Null for any other reader, including an extension or a mask of something besides `v` alone. */
315
+ function extensionOf(op: Op, v: Value, defs: ReadonlyMap<Value, Op>): { width: number; signed: boolean } | null {
316
+ if (isExtension(op)) {
317
+ return op.operands.length === 1 && op.operands[0] === v
318
+ ? { width: op.attrs.width as number, signed: op.opcode === 'sext' }
319
+ : null;
320
+ }
321
+ if (op.opcode !== 'and' || op.operands.length !== 2 || !op.operands.includes(v)) {
322
+ return null;
323
+ }
324
+ const mask = defs.get(op.operands[op.operands[0] === v ? 1 : 0]);
325
+ const width = mask?.opcode === 'const' ? MASK_WIDTHS.get(mask.attrs.value as number) : undefined;
326
+ return width === undefined ? null : { width, signed: false };
327
+ }
328
+
329
+ /** The ops that read `v` and the number of branch arguments that pass it, anywhere in `fn`. */
330
+ function readersOf(fn: Fn, v: Value): { ops: Op[]; edges: number } {
331
+ const ops: Op[] = [];
332
+ let edges = 0;
333
+ for (const b of fn.blocks) {
334
+ for (const op of b.ops) {
335
+ if (op.operands.includes(v)) {
336
+ ops.push(op);
337
+ }
338
+ for (const s of op.successors) {
339
+ edges += s.args.filter((a) => a === v).length;
340
+ }
341
+ }
342
+ }
343
+ return { ops, edges };
344
+ }
345
+
346
+ /** How many reads of `v` are not an extension of `v` alone (`extensionOf`) to `width` or narrower. */
347
+ function wideReaderCount(fn: Fn, v: Value, width: number, defs: ReadonlyMap<Value, Op>): number {
348
+ const { ops, edges } = readersOf(fn, v);
349
+ return (
350
+ edges +
351
+ ops.reduce(
352
+ (n, op) =>
353
+ n + ((extensionOf(op, v, defs)?.width ?? Infinity) <= width ? 0 : op.operands.filter((o) => o === v).length),
354
+ 0,
355
+ )
356
+ );
357
+ }
358
+
232
359
  /** How many times `v` is read anywhere in `fn` — op operands and branch arguments alike. */
233
360
  function useCount(fn: Fn, v: Value): number {
234
361
  let n = 0;
@@ -282,6 +409,7 @@ export function narrowEntryParams(
282
409
  }
283
410
  prologue.add(op);
284
411
  }
412
+ const defs = defOpMap(fn);
285
413
  let narrowed = 0;
286
414
  for (const op of [...entry.ops]) {
287
415
  if (op.opcode !== 'sext' && op.opcode !== 'zext') {
@@ -298,6 +426,7 @@ export function narrowEntryParams(
298
426
  entryIsJoin,
299
427
  inPrologue: prologue.has(op),
300
428
  uses: useCount(fn, p),
429
+ wideReaders: wideReaderCount(fn, p, width, defs),
301
430
  declared: declared[entry.params.indexOf(p)],
302
431
  fusedBehindPool: fusedBehindPool.has(op),
303
432
  witness,
@@ -314,3 +443,64 @@ export function narrowEntryParams(
314
443
  }
315
444
  return narrowed;
316
445
  }
446
+
447
+ /** `/narrow-param`: type each entry parameter every reader of which is an extension of it at the
448
+ * widest reader's width, and drop the readers of exactly that type (EVERY READER AN EXTENSION).
449
+ * `apply: false` changes nothing and reports what it would narrow. Returns each parameter it
450
+ * narrowed. */
451
+ export function narrowExtendedParams(
452
+ fn: Fn,
453
+ witness: NarrowParamWitness,
454
+ self?: FnProto,
455
+ gates: readonly Gate<NarrowParamCandidate>[] = PARAM_READER_WIDTH_GATES,
456
+ apply = true,
457
+ ): NarrowParamCandidate[] {
458
+ const evidence = fn.paramEvidence;
459
+ const entry = fn.blocks[0];
460
+ const declaredTypes = Array.isArray(self?.params) ? self.params : [];
461
+ const entryIsJoin = fn.blocks.some((b) => successorsOf(b).includes(entry));
462
+ const narrowed: NarrowParamCandidate[] = [];
463
+ const defs = defOpMap(fn);
464
+ for (const [i, p] of entry.params.entries()) {
465
+ const extensions = readersOf(fn, p).ops.flatMap((op) => {
466
+ const ext = extensionOf(op, p, defs);
467
+ return ext === null ? [] : [{ op, ...ext }];
468
+ });
469
+ if (extensions.length === 0) {
470
+ continue;
471
+ }
472
+ const width = Math.max(...extensions.map((e) => e.width));
473
+ const first = extensions.find((e) => e.width === width)!;
474
+ const declared = declaredTypes[i];
475
+ const c: NarrowParamCandidate = {
476
+ param: p,
477
+ width,
478
+ entryIsJoin,
479
+ inPrologue: false,
480
+ uses: useCount(fn, p),
481
+ wideReaders: wideReaderCount(fn, p, width, defs),
482
+ declared: declared === undefined ? undefined : declaredWidth(declared),
483
+ fusedBehindPool: false,
484
+ witness,
485
+ homed: evidence?.get(p)?.deadHome ?? false,
486
+ selfRedefined: evidence?.get(p)?.selfRedefined ?? false,
487
+ };
488
+ if (firstRejection(gates, c) !== null) {
489
+ continue;
490
+ }
491
+ narrowed.push(c);
492
+ if (!apply) {
493
+ continue;
494
+ }
495
+ const signed = (declared === undefined ? undefined : declaredSigned(declared)) ?? first.signed;
496
+ p.type = T.int(width, signed);
497
+ for (const { op, ...ext } of extensions) {
498
+ if (ext.width === width && ext.signed === signed) {
499
+ replaceAllUsesWith(fn, op.results[0], p);
500
+ const home = fn.blocks.find((b) => b.ops.includes(op))!;
501
+ home.ops.splice(home.ops.indexOf(op), 1);
502
+ }
503
+ }
504
+ }
505
+ return narrowed;
506
+ }
@@ -12,9 +12,10 @@
12
12
  // in the driver.
13
13
  import { Block, Fn } from '../ir/core';
14
14
  import { simplifyTrivialPhis } from '../ir/simplify';
15
- import { dce } from '../pattern/engine';
16
- import type { FnProto } from '../proto';
15
+ import { NOT_FCMP_PATTERNS, applyPattern, dce } from '../pattern/engine';
16
+ import { type FnProto, declaresVoidReturn } from '../proto';
17
17
  import type { TargetDescription } from '../target';
18
+ import { readDeclaredMembers } from './aggmembers';
18
19
  import { recognizeArrays } from './arrays';
19
20
  import { recognizeConsts } from './const';
20
21
  import { recognizeDivPow2 } from './divpow2';
@@ -33,12 +34,13 @@ import { recognizeMagicDivision } from './magicdiv';
33
34
  import { recognizeMemberArrays } from './memberarrays';
34
35
  import { rerootNarrowReads } from './narrow';
35
36
  import { type MergeShape, mergeShapes, narrowBlockLocals } from './narrowlocal';
36
- import { fuseParamPairs } from './pairparams';
37
- import { PARAM_WIDTH_GATES, narrowEntryParams } from './paramwidth';
37
+ import { fuseParamPairs, refuseSplitWideParams } from './pairparams';
38
+ import { PARAM_READER_WIDTH_GATES, PARAM_WIDTH_GATES, narrowEntryParams, narrowExtendedParams } from './paramwidth';
38
39
  import { type BranchShortCircuitOptions, recognizeBranchShortCircuit, recognizeShortCircuit } from './shortcircuit';
39
40
  import { recognizeSoftDiv } from './softdiv';
41
+ import { seedNarrowStoreSigns } from './storesign';
40
42
  import { recognizeStructArrays } from './struct-arrays';
41
- import { recognizeStructs } from './structs';
43
+ import { type DisplacedOptions, foldDisplacedAccesses, recognizeStructs } from './structs';
42
44
  import { TRUNC_LOAD_GATES, foldTruncatedLoads } from './truncload';
43
45
  import { recognizeWideHelpers, refuseUnmodelledHelpers } from './widehelpers';
44
46
 
@@ -49,6 +51,19 @@ import { recognizeWideHelpers, refuseUnmodelledHelpers } from './widehelpers';
49
51
  export interface PreRecoveryOptions {
50
52
  /** raise/shortcircuit.ts `recognizeBranchShortCircuit` — the connective-vs-comparison-tree question. */
51
53
  shortCircuit?: BranchShortCircuitOptions;
54
+ /** raise/paramwidth.ts `narrowExtendedParams` — the `/narrow-param` lift. */
55
+ paramWidth?: ParamWidthOptions;
56
+ /** raise/structs.ts `foldDisplacedAccesses` — the `/far-cast` lift. */
57
+ displaced?: DisplacedOptions;
58
+ }
59
+
60
+ export interface ParamWidthOptions {
61
+ /** also narrow a parameter every reader of which is an extension (`/narrow-param`) */
62
+ extendedReaders?: boolean;
63
+ /** called when a lift without `extendedReaders` has a parameter that would narrow with it */
64
+ onExtendedReaders?: () => void;
65
+ /** called when the `extendedReaders` lift narrows a parameter whose width no prototype declares */
66
+ onUndeclaredWidth?: () => void;
52
67
  }
53
68
 
54
69
  /** CFG facts read off the function ONCE, before the first pass below rewrites it.
@@ -95,9 +110,9 @@ export interface PreRecoveryPass {
95
110
 
96
111
  /** THE ordered pre-recovery pass list — the single source of truth shared by pipeline / rank / report.
97
112
  * address-numbering → const-materialize → magic-division → pow2-division → soft-division →
98
- * scaled-extension → array-legalize → truncated-load → struct-array → member-array → struct-pointer →
99
- * short-circuit → branch-short-circuit → narrow-reads → narrow-local → parameter-width →
100
- * scaled-extension-restore.
113
+ * scaled-extension → store-sign → array-legalize → truncated-load → struct-array → member-array →
114
+ * struct-pointer → short-circuit → branch-short-circuit → narrow-reads → narrow-local →
115
+ * parameter-width → scaled-extension-restore.
101
116
  * See each recognizer's file for the rationale. */
102
117
  export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
103
118
  // FIRST: collapsing duplicate address definitions removes block params every later recognizer
@@ -137,9 +152,19 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
137
152
  // from `widehelpers` because it re-types words as floats — a helper's, a parameter a callee
138
153
  // declares `double`, a literal — with its own closed-set refusal, and a single-precision helper
139
154
  // is a float helper that is not wide.
155
+ // A fold re-runs the float compares' negation idioms (pattern/engine.ts `NOT_FCMP_PATTERNS`),
156
+ // which ran before there was a float compare to negate.
140
157
  {
141
158
  id: 'floathelpers',
142
- run: (fn, _self, _opts, target) => recognizeFloatHelpers(fn, target),
159
+ run: (fn, _self, _opts, target) => {
160
+ if (!recognizeFloatHelpers(fn, target)) {
161
+ return false;
162
+ }
163
+ if (NOT_FCMP_PATTERNS.reduce((n, p) => n + applyPattern(fn, p, target), 0) > 0) {
164
+ dce(fn);
165
+ }
166
+ return true;
167
+ },
143
168
  dce: false,
144
169
  },
145
170
  // NOT gated, where `softdiv` is, and the asymmetry is the argument: a soft DIVISION is
@@ -166,7 +191,17 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
166
191
  // AFTER it, because the `concat`s it reads are the ones the frontend built at the call the pass
167
192
  // above has just rewritten — and because a pair the rewrite refused is a pair this must not fuse
168
193
  // into a signature either.
169
- { id: 'pairparams', run: (fn) => fuseParamPairs(fn), dce: false },
194
+ // It also refuses a declared 64-bit parameter the lift left in halves, which is the one place the
195
+ // fusion's answer is final (`refuseSplitWideParams`).
196
+ {
197
+ id: 'pairparams',
198
+ run: (fn, self) => {
199
+ const fused = fuseParamPairs(fn);
200
+ refuseSplitWideParams(fn, self);
201
+ return fused;
202
+ },
203
+ dce: false,
204
+ },
170
205
  // AFTER `const`, which folds a shift pair over a constant to the constant it computes, and BEFORE
171
206
  // the three array recognizers, whose input this pass produces: `shl(ext(x), k)` is an element
172
207
  // scale they legalize and the fused pair is not. `dce: true` — the `shl` a fold leaves readerless.
@@ -178,6 +213,11 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
178
213
  dce: true,
179
214
  gate: foldsShiftPairCasts,
180
215
  },
216
+ // BEFORE `arrays` and `struct-arrays`, which carry the stamp into the `astore` they make (and
217
+ // struct-arrays types a field only ever stored from it), and before `structs`, which does the
218
+ // same for a constant-offset field. `member-arrays` overwrites an astore's `signed` with its
219
+ // member's (raise/memberarrays.ts says why). `dce: false` — it changes no op.
220
+ { id: 'storesign', run: (fn, _self, _opts, target) => seedNarrowStoreSigns(fn, target), dce: false },
181
221
  { id: 'arrays', run: recognizeArrays, dce: true },
182
222
  // BEFORE the three struct synthesizers, and that is what the pass is FOR: `raise/structs.ts` is
183
223
  // the pass that refuses one offset read at two widths, and it can only see one width per offset
@@ -207,7 +247,11 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
207
247
  // struct-arrays AFTER arrays (scalar stride==width shapes are claimed first — see the
208
248
  // discriminator note in raise/struct-arrays.ts) and BEFORE structs (an element's field
209
249
  // accesses must not be re-derived as constant-offset struct-pointer accesses).
210
- { id: 'struct-arrays', run: recognizeStructArrays, dce: true },
250
+ {
251
+ id: 'struct-arrays',
252
+ run: (fn, _self, _opts, target) => recognizeStructArrays(fn, target.compilerBehaviors.aggregateBoundary),
253
+ dce: true,
254
+ },
211
255
  // member-arrays AFTER struct-arrays, and that order IS load-bearing — but the order alone does
212
256
  // not deconflict them, because the two passes claim different VALUES: struct-arrays types the
213
257
  // materialized `add(P, K)` and member-arrays groups on `P`, so `P->tbl[i].f` reaches both. What
@@ -217,11 +261,28 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
217
261
  // sits there so the three struct synthesizers read in the order of their evidence. `narrowlocal`
218
262
  // reads the carrier's extension chain rather than any address, so nothing here orders against it.
219
263
  { id: 'member-arrays', run: (fn) => recognizeMemberArrays(fn), dce: true },
264
+ // RIGHT BEFORE `structs`, whose input it widens: an access through a materialised `add(p, K)`
265
+ // joins `p`'s access set at `off + K`. After `truncload`, `struct-arrays` and `member-arrays`, so
266
+ // none of them sees a folded access, and a displacement either of the last two typed is refused.
267
+ // `dce: true` — the `add` chain a fold leaves readerless. `opts.displaced` is `/far-cast`.
268
+ {
269
+ id: 'displaced',
270
+ run: (fn, self, opts, target) =>
271
+ foldDisplacedAccesses(fn, opts.displaced, target.compilerBehaviors, declaresVoidReturn(self)),
272
+ dce: true,
273
+ },
220
274
  {
221
275
  id: 'structs',
222
276
  run: (fn, _self, _opts, target) => recognizeStructs(fn, target.compilerBehaviors.aggregateBoundary),
223
277
  dce: false,
224
278
  },
279
+ // AFTER `structs`, which synthesizes layouts for bases it can type and never for a frame object
280
+ // the audit already typed as a declared struct — the only base this reads.
281
+ {
282
+ id: 'aggmembers',
283
+ run: (fn, _self, _opts, target) => readDeclaredMembers(fn, target.capabilities.endianness === 'little'),
284
+ dce: true,
285
+ },
225
286
  { id: 'shortcircuit', run: recognizeShortCircuit, dce: true },
226
287
  // The control-flow sibling, value form FIRST.
227
288
  //
@@ -278,16 +339,13 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
278
339
  // The `target` argument is read by TWO of this pass's gates, and unlike `narrowlocal`'s single
279
340
  // conjunct it is not a tuning knob: `narrowParamWitness` says WHICH fact in the object settles a
280
341
  // parameter's declared width on this compiler, and a target that names none refuses outright.
342
+ //
343
+ // `opts.paramWidth` adds the `/narrow-param` lift after it, over the parameters it left wide
344
+ // (raise/paramwidth.ts, EVERY READER AN EXTENSION), or asks whether that lift would narrow any.
281
345
  {
282
346
  id: 'paramwidth',
283
- run: (fn, self, _opts, target, lifted) =>
284
- narrowEntryParams(
285
- fn,
286
- target.compilerBehaviors.narrowParamWitness ?? 'none',
287
- self,
288
- PARAM_WIDTH_GATES,
289
- lifted.scales.behindPool,
290
- ),
347
+ run: (fn, self, opts, target, lifted) =>
348
+ narrowParamWidths(fn, self, opts.paramWidth, target, lifted, PARAM_WIDTH_GATES, PARAM_READER_WIDTH_GATES),
291
349
  dce: false,
292
350
  },
293
351
  // LAST, after every pass that can CLAIM what `extscale` exposed — the two width passes above take
@@ -302,6 +360,31 @@ export const PRE_RECOVERY_PASSES: PreRecoveryPass[] = [
302
360
  },
303
361
  ];
304
362
 
363
+ /** The `paramwidth` entry's body, taking both of its tables, so a census can pass wrapped ones. */
364
+ export function narrowParamWidths(
365
+ fn: Fn,
366
+ self: FnProto | undefined,
367
+ opts: ParamWidthOptions | undefined,
368
+ target: TargetDescription,
369
+ lifted: PreRecoveryFacts,
370
+ prologueGates: typeof PARAM_WIDTH_GATES,
371
+ readerGates: typeof PARAM_READER_WIDTH_GATES,
372
+ ): number {
373
+ const witness = target.compilerBehaviors.narrowParamWitness ?? 'none';
374
+ const n = narrowEntryParams(fn, witness, self, prologueGates, lifted.scales.behindPool);
375
+ if (opts?.extendedReaders) {
376
+ const lifted = narrowExtendedParams(fn, witness, self, readerGates);
377
+ if (lifted.some((c) => c.declared === undefined)) {
378
+ opts.onUndeclaredWidth?.();
379
+ }
380
+ return n + lifted.length;
381
+ }
382
+ if (opts?.onExtendedReaders && narrowExtendedParams(fn, witness, self, readerGates, false).length > 0) {
383
+ opts.onExtendedReaders();
384
+ }
385
+ return n;
386
+ }
387
+
305
388
  /** Run the pre-recovery passes in order. For each pass whose gate passes and that CHANGES the IR, run
306
389
  * `dce` when the pass declares it, then invoke `afterPass(pass, result)` (the caller's verify/trace
307
390
  * hook), if given.
@@ -0,0 +1,54 @@
1
+ // asmlift — the CFG folds that run last before structuring, after return-sinking: each rewrites a
2
+ // loop's edges into a shape a loop spelling owns, and each is its own stage in the trace, so a
3
+ // re-spelled function names the fold that moved its CFG.
4
+ //
5
+ // A MUTABLE RECORD, not a sum of four calls, for the reason `PRE_RECOVERY_PASSES` is one: it is the
6
+ // caller-side seam a process outside core swaps to census a fold's gate table (`pnpm bench gates`,
7
+ // apps/benchmark/src/run/gate-census.ts). A static import binding cannot be swapped.
8
+ import { Fn } from '../ir/core';
9
+ import { foldEmptyLatches, foldExitTrampolines } from './latch';
10
+ import { rerollPeeledTests } from './reroll';
11
+ import { unthreadLoopExits } from './unthread';
12
+
13
+ export interface PreStructureFold {
14
+ /** stable; the trace stage is `stage:<id>` */
15
+ readonly id: string;
16
+ /** the trace stage's title */
17
+ readonly title: string;
18
+ /** how many rewrites the fold made; 0 leaves the function as it was */
19
+ run: (fn: Fn) => number;
20
+ }
21
+
22
+ /** THE ORDER CARRIES LOAD, and each fold runs ONCE: there is no fixpoint across folds, so a fold
23
+ * that exposes another's shape must run before it. The re-roll needs the latch's way out and the
24
+ * entry's to reach ONE block, and on `synthetic:break_value:agbcc` they reach it only after the
25
+ * trampoline fold has spliced out the empty block the break leaves through; ahead of it, the re-roll
26
+ * declines and the loop with it ('unrecovered back-edge'). `reroll.test.ts` pins it by lifting that
27
+ * row's pre-fold IR through this record. The latch fold comes first for the same reason in kind:
28
+ * the folds after it read the back edge it leaves. The un-threading reads loop exits, which the
29
+ * trampoline fold reshapes, and sits ahead of the re-roll so a loop it gives one way out is one
30
+ * the re-roll may then read. That place is a precaution: `synthetic:thrbreak:agbcc` and the
31
+ * pointer walk unthread.ts names lift the same C wherever the fold sits, so `unthread.test.ts`
32
+ * pins its position, not a lift that needs it. */
33
+ export const PRE_STRUCTURE_FOLDS: PreStructureFold[] = [
34
+ {
35
+ id: 'latchfold',
36
+ title: 'Empty-latch folding (a latch that only jumps back becomes its back edge)',
37
+ run: (fn) => foldEmptyLatches(fn),
38
+ },
39
+ {
40
+ id: 'trampoline',
41
+ title: 'Exit-trampoline folding (an empty block on a loop exit becomes that edge)',
42
+ run: (fn) => foldExitTrampolines(fn),
43
+ },
44
+ {
45
+ id: 'unthread',
46
+ title: 'Loop-exit un-threading (an exit jump-threaded past the re-test after the loop goes back to it)',
47
+ run: (fn) => unthreadLoopExits(fn),
48
+ },
49
+ {
50
+ id: 'reroll',
51
+ title: 'Peeled-test re-rolling (a loop test peeled into the entry becomes the first iteration)',
52
+ run: (fn) => rerollPeeledTests(fn),
53
+ },
54
+ ];