1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use crate::base::pass_registry;
use strum_macros::EnumIter;

/// A Binaryen optimization pass.
///
/// These have the same names as given on the command line to
/// `wasm-opt`, but with Rust capitalization conventions.
// Keep these in the same order as PassRegistry::registerPasses
#[non_exhaustive]
#[derive(Clone, Debug, EnumIter)]
pub enum Pass {
    /// Lower unaligned loads and stores to smaller aligned ones.
    AlignmentLowering,
    /// Async/await style transform, allowing pausing and resuming.
    Asyncify,
    /// Tries to avoid reinterpret operations via more loads.
    AvoidReinterprets,
    /// Removes arguments to calls in an lto-like manner.
    Dae,
    /// Removes arguments to calls in an lto-like manner, and optimizes where removed.
    DaeOptimizing,
    /// Refine and merge abstract (never-created) types.
    AbstractTypeRefining,
    /// Reduce # of locals by coalescing.
    CoalesceLocals,
    /// Reduce # of locals by coalescing and learning.
    CoalesceLocalsLearning,
    /// Push code forward, potentially making it not always execute.
    CodePushing,
    /// Fold code, merging duplicates.
    CodeFolding,
    /// Hoist repeated constants to a local.
    ConstHoisting,
    /// Propagate constant struct field values.
    Cfp,
    /// Removes unreachable code.
    Dce,
    /// Forces all loads and stores to have alignment 1.
    Dealign,
    /// Instrument the wasm to convert NaNs into 0 at runtime.
    DeNan,
    /// Turns indirect calls into direct ones.
    Directize,
    /// Discards global effect info.
    DiscardGlobalEffects,
    /// Optimizes using the DataFlow SSA IR.
    Dfo,
    /// Dump DWARF debug info sections from the read binary.
    DwarfDump,
    /// Removes duplicate imports.
    DuplicateImportElimination,
    /// Removes duplicate functions.
    DuplicateFunctionElimination,
    /// Emit the target features section in the output.
    EmitTargetFeatures,
    /// Leaves just one function (useful for debugging).
    ExtractFunction,
    /// Leaves just one function selected by index.
    ExtractFunctionIndex,
    /// Flattens out code, removing nesting.
    Flatten,
    /// Emulates function pointer casts, allowing incorrect indirect calls to (sometimes) work.
    FpCastEmu,
    /// Reports function metrics.
    FuncMetrics,
    /// Generate dynCall fuctions used by emscripten ABI.
    GenerateDyncalls,
    /// Generate dynCall functions used by emscripten ABI, but only for functions with i64 in their signature (which cannot be invoked via the wasm table without JavaScript BigInt support).
    GenerateI64Dyncalls,
    /// Generate global effect info (helps later passes).
    GenerateGlobalEffects,
    /// Generate Stack IR.
    GenerateStackIr,
    /// Refine the types of globals.
    GlobalRefining,
    /// Globally optimize GC types.
    Gto,
    /// Globally optimize struct values.
    Gsi,
    /// Grand unified flow analyses.
    ///
    /// Optimize the entire program using information about what content can actually appear in each location.
    Gufa,
    /// GUFA plus add casts for all inferences.
    GufaCastAll,
    /// Gufa plus local optimizations in functions we modified.
    GufaOptimizing,
    /// Apply more specific subtypes to type fields where possible.
    TypeRefining,
    /// Replace GC allocations with locals.
    Heap2Local,
    /// Inline __original_main into main.
    InlineMain,
    /// Inline functions (you probably want inlining-optimizing).
    Inlining,
    /// Inline functions and optimizes where we inlined.
    InliningOptimizing,
    /// Lower away binaryen intrinsics.
    IntrinsicLowering,
    /// Wrap imports and exports for JavaScript promise integration.
    Jspi,
    /// Legalizes i64 types on the import/export boundary.
    LegalizeJsInterface,
    /// Legalizes i64 types on the import/export boundary in a minimal manner, only on things only JS will call.
    LegalizeJsInterfaceMinimally,
    /// Common subexpression elimination inside basic blocks.
    LocalCse,
    /// Apply more specific subtypes to locals where possible.
    LocalSubtyping,
    /// Instrument the build with logging of where execution goes.
    LogExecution,
    /// Lower all uses of i64s to use i32s instead.
    I64ToI32Lowering,
    /// Instrument the build with code to intercept all loads and stores.
    InstrumentLocals,
    /// Instrument the build with code to intercept all loads and stores.
    InstrumentMemory,
    /// Loop invariant code motion.
    Licm,
    /// Attempt to merge segments to fit within web limits.
    LimitSegments,
    /// Lower loads and stores to a 64-bit memory to instead use a 32-bit one.
    Memory64Lowering,
    /// Packs memory into separate segments, skipping zeros.
    MemoryPacking,
    /// Merges blocks to their parents.
    MergeBlocks,
    /// Merges similar functions when benefical.
    MergeSimilarFunctions,
    /// Merges locals when beneficial.
    MergeLocals,
    /// Reports metrics.
    Metrics,
    /// Minifies import names (only those, and not export names), and emits a mapping to the minified ones.
    MinifyImports,
    /// Minifies both import and export names, and emits a mapping to the minified ones.
    MinifyImportsAndExports,
    /// Minifies both import and export names, and emits a mapping to the minified ones, and minifies the modules as well.
    MinifyImportsAndExportsAndModules,
    /// Apply the assumption that asyncify imports always unwind, and we never rewind.
    ModAsyncifyAlwaysAndOnlyUnwind,
    /// Apply the assumption that asyncify never unwinds.
    ModAsyncifyNeverUnwind,
    /// Creates specialized versions of functions.
    Monomorphize,
    /// Creates specialized versions of functions (even if unhelpful).
    MonomorphizeAlways,
    /// Combines multiple memories into a single memory.
    MultiMemoryLowering,
    /// Combines multiple memories into a single memory, trapping if the read or write is larger than the length of the memory's data.
    MultiMemoryLoweringWithBoundsChecks,
    /// Name list.
    Nm,
    /// (Re)name all heap types.
    NameTypes,
    /// Reduces calls to code that only runs once.
    OnceReduction,
    /// Optimizes added constants into load/store offsets.
    OptimizeAddedConstants,
    /// Optimizes added constants into load/store offsets, propagating them across locals too.
    OptimizeAddedConstantsPropagate,
    /// Eliminate and reuse casts.
    OptimizeCasts,
    /// Optimizes instruction combinations.
    OptimizeInstructions,
    /// Optimize Stack IR.
    OptimizeStackIr,
    /// Pick load signs based on their uses.
    PickLoadSigns,
    /// Tranform Binaryen IR into Poppy IR.
    Poppify,
    /// Miscellaneous optimizations for Emscripten-generated code.
    PostEmscripten,
    /// Early optimize of the instruction combinations for js.
    OptimizeForJs,
    /// Computes compile-time evaluatable expressions.
    Precompute,
    /// Computes compile-time evaluatable expressions and propagates.
    PrecomputePropagate,
    /// Print in s-expression format.
    Print,
    /// Print in minified s-expression format.
    PrintMinified,
    /// Print options for enabled features.
    PrintFeatures,
    /// Print in full s-expression format.
    PrintFull,
    /// Print call graph.
    PrintCallGraph,
    /// Print a map of function indexes to names.
    PrintFunctionMap,
    /// (Alias for print-function-map).
    Symbolmap,
    /// Print out Stack IR (useful for internal debugging).
    PrintStackIr,
    /// Removes operations incompatible with js.
    RemoveNonJsOps,
    /// Removes imports and replaces them with nops.
    RemoveImports,
    /// Removes memory segments.
    RemoveMemory,
    /// Removes breaks from locations that are not needed.
    RemoveUnusedBrs,
    /// Removes unused module elements.
    RemoveUnusedModuleElements,
    /// Removes unused module elements that are not functions.
    RemoveUnusedNonfunctionModuleElements,
    /// Removes names from locations that are never branched to.
    RemoveUnusedNames,
    /// Remove unused private GC types.
    RemoveUnusedTypes,
    /// Sorts functions by name (useful for debugging).
    ReorderFunctionsByName,
    /// Sorts functions by access frequency.
    ReorderFunctions,
    /// Sorts globals by access frequency.
    ReorderGlobals,
    /// Sorts locals by access frequency.
    RecorderLocals,
    /// Re-optimize control flow using the relooper algorithm.
    Rereloop,
    /// Remove redundant local.sets.
    Rse,
    /// Write the module to binary, then read it.
    Roundtrip,
    /// Instrument loads and stores to check for invalid behavior.
    SafeHeap,
    /// Sets specified globals to specified values.
    SetGlobals,
    /// Remove params from function signature types where possible.
    SignaturePruning,
    /// Apply more specific subtypes to signature types where possible.
    SignatureRefining,
    /// Lower sign-ext operations to wasm mvp.
    SignextLowering,
    /// Miscellaneous globals-related optimizations.
    SimplifyGlobals,
    /// Miscellaneous globals-related optimizations, and optimizes where we replaced global.gets with constants.
    SimplifyGlobalsOptimizing,
    /// Miscellaneous locals-related optimizations.
    SimplifyLocals,
    /// Miscellaneous locals-related optimizations (no nesting at all; preserves flatness).
    SimplifyLocalsNonesting,
    /// Miscellaneous locals-related optimizations (no tees).
    SimplifyLocalsNotee,
    /// Miscellaneous locals-related optimizations (no structure).
    SimplifyLocalsNostructure,
    /// Miscellaneous locals-related optimizations (no tees or structure).
    SimplifyLocalsNoteeNostructure,
    /// Emit Souper IR in text form.
    Souperify,
    /// Emit Souper IR in text form (single-use nodes only).
    SouperifySingleUse,
    /// Spill pointers to the C stack (useful for Boehm-style GC).
    SpillPointers,
    /// Stub out unsupported JS operations.
    StubUnsupportedJs,
    /// Ssa-ify variables so that they have a single assignment.
    Ssa,
    /// Ssa-ify variables so that they have a single assignment, ignoring merges.
    SsaNomerge,
    /// Deprecated; same as strip-debug.
    Strip,
    /// Enforce limits on llvm's __stack_pointer global.
    StackCheck,
    /// Strip debug info (including the names section).
    StripDebug,
    /// Strip dwarf debug info.
    StripDwarf,
    /// Strip the wasm producers section.
    StripProducers,
    /// Strip EH instructions.
    StripEh,
    /// Strip the wasm target features section.
    StripTargetFeatuers,
    /// Replace trapping operations with clamping semantics.
    TrapModeClamp,
    /// Replace trapping operations with js semantics.
    TrapModeJs,
    /// Merge types to their supertypes where possible.
    TypeMerging,
    /// Create new nominal types to help other optimizations.
    TypeSsa,
    /// Removes local.tees, replacing them with sets and gets.
    Untee,
    /// Removes obviously unneeded code.
    Vacuum,
}

impl Pass {
    /// Returns the name of the pass.
    ///
    /// This is the same name used by Binaryen to identify the pass on the command line.
    pub fn name(&self) -> &'static str {
        use Pass::*;
        match self {
            AlignmentLowering => "alignment-lowering",
            Asyncify => "asyncify",
            AvoidReinterprets => "avoid-reinterprets",
            Dae => "dae",
            DaeOptimizing => "dae-optimizing",
            AbstractTypeRefining => "abstract-type-refining",
            CoalesceLocals => "coalesce-locals",
            CoalesceLocalsLearning => "coalesce-locals-learning",
            CodePushing => "code-pushing",
            CodeFolding => "code-folding",
            ConstHoisting => "const-hoisting",
            Cfp => "cfp",
            Dce => "dce",
            Dealign => "dealign",
            DeNan => "denan",
            DiscardGlobalEffects => "discard-global-effects",
            Directize => "directize",
            Dfo => "dfo",
            DwarfDump => "dwarfdump",
            DuplicateImportElimination => "duplicate-import-elimination",
            DuplicateFunctionElimination => "duplicate-function-elimination",
            EmitTargetFeatures => "emit-target-features",
            ExtractFunction => "extract-function",
            ExtractFunctionIndex => "extract-function-index",
            Flatten => "flatten",
            FpCastEmu => "fpcast-emu",
            FuncMetrics => "func-metrics",
            GenerateDyncalls => "generate-dyncalls",
            GenerateI64Dyncalls => "generate-i64-dyncalls",
            GenerateGlobalEffects => "generate-global-effects",
            GenerateStackIr => "generate-stack-ir",
            GlobalRefining => "global-refining",
            Gto => "gto",
            Gsi => "gsi",
            Gufa => "gufa",
            GufaCastAll => "gufa-cast-all",
            GufaOptimizing => "gufa-optimizing",
            TypeRefining => "type-refining",
            Heap2Local => "heap2local",
            InlineMain => "inline-main",
            Inlining => "inlining",
            InliningOptimizing => "inlining-optimizing",
            IntrinsicLowering => "intrinsic-lowering",
            Jspi => "jspi",
            LegalizeJsInterface => "legalize-js-interface",
            LegalizeJsInterfaceMinimally => "legalize-js-interface-minimally",
            LocalCse => "local-cse",
            LocalSubtyping => "local-subtyping",
            LogExecution => "log-execution",
            I64ToI32Lowering => "i64-to-i32-lowering",
            InstrumentLocals => "instrument-locals",
            InstrumentMemory => "instrument-memory",
            Licm => "licm",
            LimitSegments => "limit-segments",
            Memory64Lowering => "memory64-lowering",
            MemoryPacking => "memory-packing",
            MergeBlocks => "merge-blocks",
            MergeSimilarFunctions => "merge-similar-functions",
            MergeLocals => "merge-locals",
            Metrics => "metrics",
            MinifyImports => "minify-imports",
            MinifyImportsAndExports => "minify-imports-and-exports",
            MinifyImportsAndExportsAndModules => "minify-imports-and-exports-and-modules",
            ModAsyncifyAlwaysAndOnlyUnwind => "mod-asyncify-always-and-only-unwind",
            ModAsyncifyNeverUnwind => "mod-asyncify-never-unwind",
            Monomorphize => "monomorphize",
            MonomorphizeAlways => "monomorphize-always",
            MultiMemoryLowering => "multi-memory-lowering",
            MultiMemoryLoweringWithBoundsChecks => "multi-memory-lowering-with-bounds-checks",
            Nm => "nm",
            NameTypes => "name-types",
            OnceReduction => "once-reduction",
            OptimizeAddedConstants => "optimize-added-constants",
            OptimizeAddedConstantsPropagate => "optimize-added-constants-propagate",
            OptimizeCasts => "optimize-casts",
            OptimizeInstructions => "optimize-instructions",
            OptimizeStackIr => "optimize-stack-ir",
            PickLoadSigns => "pick-load-signs",
            Poppify => "poppify",
            PostEmscripten => "post-emscripten",
            OptimizeForJs => "optimize-for-js",
            Precompute => "precompute",
            PrecomputePropagate => "precompute-propagate",
            Print => "print",
            PrintMinified => "print-minified",
            PrintFeatures => "print-features",
            PrintFull => "print-full",
            PrintCallGraph => "print-call-graph",
            PrintFunctionMap => "print-function-map",
            Symbolmap => "symbolmap",
            PrintStackIr => "print-stack-ir",
            RemoveNonJsOps => "remove-non-js-ops",
            RemoveImports => "remove-imports",
            RemoveMemory => "remove-memory",
            RemoveUnusedBrs => "remove-unused-brs",
            RemoveUnusedModuleElements => "remove-unused-module-elements",
            RemoveUnusedNonfunctionModuleElements => "remove-unused-nonfunction-module-elements",
            RemoveUnusedNames => "remove-unused-names",
            RemoveUnusedTypes => "remove-unused-types",
            ReorderFunctionsByName => "reorder-functions-by-name",
            ReorderFunctions => "reorder-functions",
            ReorderGlobals => "reorder-globals",
            RecorderLocals => "reorder-locals",
            Rereloop => "rereloop",
            Rse => "rse",
            Roundtrip => "roundtrip",
            SafeHeap => "safe-heap",
            SetGlobals => "set-globals",
            SignaturePruning => "signature-pruning",
            SignatureRefining => "signature-refining",
            SignextLowering => "signext-lowering",
            SimplifyGlobals => "simplify-globals",
            SimplifyGlobalsOptimizing => "simplify-globals-optimizing",
            SimplifyLocals => "simplify-locals",
            SimplifyLocalsNonesting => "simplify-locals-nonesting",
            SimplifyLocalsNotee => "simplify-locals-notee",
            SimplifyLocalsNostructure => "simplify-locals-nostructure",
            SimplifyLocalsNoteeNostructure => "simplify-locals-notee-nostructure",
            Souperify => "souperify",
            SouperifySingleUse => "souperify-single-use",
            SpillPointers => "spill-pointers",
            StubUnsupportedJs => "stub-unsupported-js",
            Ssa => "ssa",
            SsaNomerge => "ssa-nomerge",
            Strip => "strip",
            StackCheck => "stack-check",
            StripDebug => "strip-debug",
            StripDwarf => "strip-dwarf",
            StripProducers => "strip-producers",
            StripEh => "strip-eh",
            StripTargetFeatuers => "strip-target-features",
            TrapModeClamp => "trap-mode-clamp",
            TrapModeJs => "trap-mode-js",
            TypeMerging => "type-merging",
            TypeSsa => "type-ssa",
            Untee => "untee",
            Vacuum => "vacuum",
        }
    }

    /// Get Binaryen's description of the pass.
    pub fn description(&self) -> String {
        // NB: This will abort if the name is invalid
        pass_registry::get_pass_description(self.name())
    }
}