loops.js (1954B)
1 // Move a nested if out of line. 2 let module = new WebAssembly.Module(wasmTextToBinary(` 3 (module 4 (type (;0;) (func)) 5 (type (;1;) (func (param i32) (result i32))) 6 (type (;2;) (func (result i32))) 7 (func $__wasm_nullptr (type 0) 8 unreachable) 9 (func $main (type 2) (result i32) 10 (local i32 i32 i32 i32) 11 i32.const 0 12 local.tee 2 13 local.set 3 14 loop 15 local.get 2 16 i32.const 50000 17 i32.eq 18 (@metadata.code.branch_hint "\\00") if 19 i32.const 1 20 local.set 3 21 end 22 local.get 2 23 i32.const 1 24 i32.add 25 local.tee 2 26 i32.const 100000 27 i32.ne 28 (@metadata.code.branch_hint "\\01") br_if 0 (;@1;) 29 end 30 local.get 3) 31 (table (;0;) 1 1 funcref) 32 (memory (;0;) 17 128) 33 (global (;0;) (mut i32) (i32.const 42)) 34 (export "memory" (memory 0)) 35 (export "_main" (func $main)) 36 (elem (;0;) (i32.const 0) func $__wasm_nullptr) 37 (type (;0;) (func (param i32))) 38 )`)); 39 40 assertEq(wasmParsedBranchHints(module), true); 41 let instance = new WebAssembly.Instance(module); 42 instance.exports._main(10); 43 44 45 // A loop with unlikely blocks in the middle. 46 let m = new WebAssembly.Module(wasmTextToBinary(` 47 (module 48 (type (;0;) (func (param i32))) 49 (memory (;0;) 1 1) 50 (func $test (param i32) 51 (local i32) 52 loop 53 local.get 1 54 local.get 0 55 i32.const 1 56 i32.sub 57 i32.eq 58 (@metadata.code.branch_hint "\\00") 59 if 60 local.get 1 61 call $test 62 end 63 local.get 1 64 local.get 0 65 i32.const 2 66 i32.sub 67 i32.eq 68 (@metadata.code.branch_hint "\\00") 69 if 70 local.get 1 71 call $test 72 end 73 local.get 1 74 i32.const 1 75 i32.add 76 local.tee 1 77 local.get 0 78 i32.lt_u 79 br_if 0 80 end 81 return 82 ) 83 (export "test" (func $test)) 84 )`)); 85 86 assertEq(wasmParsedBranchHints(m), true); 87 88 instance = new WebAssembly.Instance(m); 89 instance.exports.test(10);