Skip to content

Commit b4eb90c

Browse files
authored
wasm2js: precompute bitwise operations (#2101)
This happens on e.g. an i32 load of a constant offset, then we have constant >> 2.
1 parent 67019f9 commit b4eb90c

16 files changed

Lines changed: 213 additions & 183 deletions

src/tools/wasm2js.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ static void traversePost(Ref node, std::function<void(Ref)> visit) {
128128
traversePrePost(node, [](Ref node) {}, visit);
129129
}
130130

131+
#if 0
132+
static void replaceInPlace(Ref target, Ref value) {
133+
assert(target->isArray() && value->isArray());
134+
target->resize(value->size());
135+
for (size_t i = 0; i < value->size(); i++) {
136+
target[i] = value[i];
137+
}
138+
}
139+
#endif
140+
131141
static void optimizeJS(Ref ast) {
132142
// Helpers
133143

@@ -277,6 +287,26 @@ static void optimizeJS(Ref ast) {
277287
}
278288
}
279289
}
290+
// Pre-compute constant [op] constant, which the lowering can generate
291+
// in loads etc.
292+
if (node[2]->isNumber() && node[3]->isNumber()) {
293+
int32_t left = node[2]->getNumber();
294+
int32_t right = node[3]->getNumber();
295+
if (node[1] == OR) {
296+
node->setNumber(left | right);
297+
} else if (node[1] == AND) {
298+
node->setNumber(left & right);
299+
} else if (node[1] == XOR) {
300+
node->setNumber(left ^ right);
301+
} else if (node[1] == LSHIFT) {
302+
node->setNumber(left << (right & 31));
303+
} else if (node[1] == RSHIFT) {
304+
node->setNumber(int32_t(left) >> int32_t(right & 31));
305+
} else if (node[1] == TRSHIFT) {
306+
node->setNumber(uint32_t(left) >> uint32_t(right & 31));
307+
}
308+
return;
309+
}
280310
}
281311
// +(+x) => +x
282312
else if (isPlus(node)) {

test/wasm2js/br_table_temp.2asm.js.opt

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ function asmFunc(global, env, buffer) {
2525
}
2626

2727
function $5() {
28-
return 1 | 0;
28+
return 1;
2929
}
3030

3131
function $6() {
32-
return 2 | 0;
32+
return 2;
3333
}
3434

3535
function $7() {
@@ -42,20 +42,20 @@ function asmFunc(global, env, buffer) {
4242

4343
function $9($0) {
4444
$0 = $0 | 0;
45-
return 22 | 0;
45+
return 22;
4646
}
4747

4848
function $10($0) {
4949
$0 = $0 | 0;
50-
return 33 | 0;
50+
return 33;
5151
}
5252

5353
function $11($0) {
5454
$0 = $0 | 0;
5555
if ($0) {
56-
return 20 | 0
56+
return 20
5757
}
58-
return 22 | 0;
58+
return 22;
5959
}
6060

6161
function $12($0) {
@@ -88,15 +88,15 @@ function asmFunc(global, env, buffer) {
8888
break block;
8989
};
9090
}
91-
return 100 | 0;
91+
return 100;
9292
}
93-
return 101 | 0;
93+
return 101;
9494
}
95-
return 102 | 0;
95+
return 102;
9696
}
97-
return 103 | 0;
97+
return 103;
9898
}
99-
return 104 | 0;
99+
return 104;
100100
}
101101

102102
function $14($0) {
@@ -119,15 +119,15 @@ function asmFunc(global, env, buffer) {
119119
break block;
120120
};
121121
}
122-
return 210 | 0;
122+
return 210;
123123
}
124-
return 211 | 0;
124+
return 211;
125125
}
126-
return 212 | 0;
126+
return 212;
127127
}
128-
return 213 | 0;
128+
return 213;
129129
}
130-
return 214 | 0;
130+
return 214;
131131
}
132132

133133
function $15($0) {
@@ -12448,41 +12448,41 @@ function asmFunc(global, env, buffer) {
1244812448
break block;
1244912449
};
1245012450
}
12451-
return 0 | 0;
12451+
return 0;
1245212452
}
12453-
return 1 | 0;
12453+
return 1;
1245412454
}
1245512455

1245612456
function $20() {
12457-
return 3 | 0;
12457+
return 3;
1245812458
}
1245912459

1246012460
function $21() {
12461-
return 4 | 0;
12461+
return 4;
1246212462
}
1246312463

1246412464
function $22() {
12465-
return 5 | 0;
12465+
return 5;
1246612466
}
1246712467

1246812468
function $23() {
12469-
return 9 | 0;
12469+
return 9;
1247012470
}
1247112471

1247212472
function $25() {
12473-
return 8 | 0;
12473+
return 8;
1247412474
}
1247512475

1247612476
function $28() {
12477-
return 10 | 0;
12477+
return 10;
1247812478
}
1247912479

1248012480
function $29() {
12481-
return 11 | 0;
12481+
return 11;
1248212482
}
1248312483

1248412484
function $30() {
12485-
return 7 | 0;
12485+
return 7;
1248612486
}
1248712487

1248812488
function $32($0, $1) {
@@ -12506,85 +12506,85 @@ function asmFunc(global, env, buffer) {
1250612506
function $34($0, $1) {
1250712507
$0 = $0 | 0;
1250812508
$1 = $1 | 0;
12509-
return 5 | 0;
12509+
return 5;
1251012510
}
1251112511

1251212512
function $35($0, $1) {
1251312513
$0 = $0 | 0;
1251412514
$1 = $1 | 0;
12515-
return 6 | 0;
12515+
return 6;
1251612516
}
1251712517

1251812518
function $38() {
12519-
return 12 | 0;
12519+
return 12;
1252012520
}
1252112521

1252212522
function $39() {
12523-
return 13 | 0;
12523+
return 13;
1252412524
}
1252512525

1252612526
function $40() {
12527-
return 14 | 0;
12527+
return 14;
1252812528
}
1252912529

1253012530
function $41() {
12531-
return 20 | 0;
12531+
return 20;
1253212532
}
1253312533

1253412534
function $42() {
12535-
return 21 | 0;
12535+
return 21;
1253612536
}
1253712537

1253812538
function $43() {
12539-
return 22 | 0;
12539+
return 22;
1254012540
}
1254112541

1254212542
function $44() {
12543-
return 23 | 0;
12543+
return 23;
1254412544
}
1254512545

1254612546
function $45() {
12547-
return 17 | 0;
12547+
return 17;
1254812548
}
1254912549

1255012550
function $47() {
12551-
return 30 | 0;
12551+
return 30;
1255212552
}
1255312553

1255412554
function $49() {
12555-
return 31 | 0;
12555+
return 31;
1255612556
}
1255712557

1255812558
function $50() {
12559-
return 32 | 0;
12559+
return 32;
1256012560
}
1256112561

1256212562
function $51() {
12563-
return 33 | 0;
12563+
return 33;
1256412564
}
1256512565

1256612566
function $54() {
12567-
return 45 | 0;
12567+
return 45;
1256812568
}
1256912569

1257012570
function $55() {
12571-
return 44 | 0;
12571+
return 44;
1257212572
}
1257312573

1257412574
function $56() {
12575-
return 43 | 0;
12575+
return 43;
1257612576
}
1257712577

1257812578
function $57() {
12579-
return 42 | 0;
12579+
return 42;
1258012580
}
1258112581

1258212582
function $58() {
12583-
return 41 | 0;
12583+
return 41;
1258412584
}
1258512585

1258612586
function $59() {
12587-
return 40 | 0;
12587+
return 40;
1258812588
}
1258912589

1259012590
function $60($0) {

test/wasm2js/conversions-modified.2asm.js.opt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ function asmFunc(global, env, buffer) {
172172
}
173173

174174
function legalstub$22($0, $1) {
175-
wasm2js_scratch_store_i32(0 | 0, $0 | 0);
176-
wasm2js_scratch_store_i32(1 | 0, $1 | 0);
175+
wasm2js_scratch_store_i32(0, $0 | 0);
176+
wasm2js_scratch_store_i32(1, $1 | 0);
177177
return +wasm2js_scratch_load_f64();
178178
}
179179

180180
function legalstub$24($0) {
181181
var $1 = 0, $2 = 0;
182182
wasm2js_scratch_store_f64(+$0);
183-
$1 = wasm2js_scratch_load_i32(1 | 0) | 0;
184-
$2 = wasm2js_scratch_load_i32(0 | 0) | 0;
183+
$1 = wasm2js_scratch_load_i32(1) | 0;
184+
$2 = wasm2js_scratch_load_i32(0) | 0;
185185
i64toi32_i32$HIGH_BITS = $1;
186186
$1 = $2;
187187
setTempRet0(i64toi32_i32$HIGH_BITS | 0);

test/wasm2js/emscripten.2asm.js.opt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ function asmFunc(global, env, buffer) {
2626
var syscall$54 = env.__syscall54;
2727
// EMSCRIPTEN_START_FUNCS;
2828
function main() {
29-
syscall$6(1 | 0, 2 | 0) | 0;
30-
syscall$54(3 | 0, 4 | 0) | 0;
31-
FUNCTION_TABLE[HEAP32[1030 >> 2]]();
29+
syscall$6(1, 2) | 0;
30+
syscall$54(3, 4) | 0;
31+
FUNCTION_TABLE[HEAP32[257]]();
3232
}
3333

3434
function other() {
@@ -40,11 +40,11 @@ function asmFunc(global, env, buffer) {
4040
}
4141

4242
function bar() {
43-
HEAP32[16 >> 2] = 3;
44-
HEAPF32[16 >> 2] = 7;
45-
HEAPF64[16 >> 3] = 11;
46-
HEAP8[16 | 0] = 15;
47-
HEAP16[16 >> 1] = 19;
43+
HEAP32[4] = 3;
44+
HEAPF32[4] = 7;
45+
HEAPF64[2] = 11;
46+
HEAP8[16] = 15;
47+
HEAP16[8] = 19;
4848
}
4949

5050
function __growWasmMemory($0) {
@@ -63,17 +63,17 @@ function asmFunc(global, env, buffer) {
6363

6464
function select($0) {
6565
$0 = $0 | 0;
66-
return ($0 ? $0 : HEAP32[16 >> 2]) | 0;
66+
return ($0 ? $0 : HEAP32[4]) | 0;
6767
}
6868

6969
function bools($0) {
7070
$0 = $0 | 0;
71-
bools(HEAP8[0 | 0] & 1);
72-
bools(HEAP8[0 | 0] & 1);
73-
bools(HEAP16[0 >> 1] & 1);
74-
bools(HEAP16[0 >> 1] & 1);
75-
bools(HEAP32[0 >> 2] & 1);
76-
bools(HEAPU8[0 | 0] & 2);
71+
bools(HEAP8[0] & 1);
72+
bools(HEAP8[0] & 1);
73+
bools(HEAP16[0] & 1);
74+
bools(HEAP16[0] & 1);
75+
bools(HEAP32[0] & 1);
76+
bools(HEAPU8[0] & 2);
7777
bools($0 ^ 1);
7878
if (!$0) {
7979
bools(2)

0 commit comments

Comments
 (0)