@@ -188,6 +188,15 @@ static void optimizeBlock(Block* curr, Module* module) {
188
188
}
189
189
if (!child) continue ;
190
190
if (child->name .is ()) continue ; // named blocks can have breaks to them (and certainly do, if we ran RemoveUnusedNames and RemoveUnusedBrs)
191
+ if (child->type == unreachable) {
192
+ // an unreachable block can have a concrete final element (which is never reached)
193
+ if (!child->list .empty ()) {
194
+ if (isConcreteWasmType (child->list .back ()->type )) {
195
+ // just remove it
196
+ child->list .pop_back ();
197
+ }
198
+ }
199
+ }
191
200
ExpressionList merged (module->allocator );
192
201
for (size_t j = 0 ; j < i; j++) {
193
202
merged.push_back (curr->list [j]);
@@ -279,11 +288,14 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
279
288
}
280
289
281
290
void visitSelect (Select* curr) {
291
+ // TODO: for now, just stop when we see any side effect. instead, we could
292
+ // check effects carefully for reordering
282
293
Block* outer = nullptr ;
283
- outer = optimize (curr, curr->ifTrue , outer);
284
294
if (EffectAnalyzer (getPassOptions (), curr->ifTrue ).hasSideEffects ()) return ;
285
- outer = optimize (curr, curr->ifFalse , outer);
295
+ outer = optimize (curr, curr->ifTrue , outer);
286
296
if (EffectAnalyzer (getPassOptions (), curr->ifFalse ).hasSideEffects ()) return ;
297
+ outer = optimize (curr, curr->ifFalse , outer);
298
+ if (EffectAnalyzer (getPassOptions (), curr->condition ).hasSideEffects ()) return ;
287
299
optimize (curr, curr->condition , outer);
288
300
}
289
301
@@ -299,11 +311,13 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
299
311
}
300
312
301
313
template <typename T>
302
- void handleCall (T* curr, Block* outer = nullptr ) {
314
+ void handleCall (T* curr) {
315
+ Block* outer = nullptr ;
303
316
for (Index i = 0 ; i < curr->operands .size (); i++) {
304
- outer = optimize (curr, curr->operands [i], outer);
305
317
if (EffectAnalyzer (getPassOptions (), curr->operands [i]).hasSideEffects ()) return ;
318
+ outer = optimize (curr, curr->operands [i], outer);
306
319
}
320
+ return ;
307
321
}
308
322
309
323
void visitCall (Call* curr) {
@@ -315,9 +329,13 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
315
329
}
316
330
317
331
void visitCallIndirect (CallIndirect* curr) {
318
- auto * outer = optimize (curr, curr->target );
332
+ Block* outer = nullptr ;
333
+ for (Index i = 0 ; i < curr->operands .size (); i++) {
334
+ if (EffectAnalyzer (getPassOptions (), curr->operands [i]).hasSideEffects ()) return ;
335
+ outer = optimize (curr, curr->operands [i], outer);
336
+ }
319
337
if (EffectAnalyzer (getPassOptions (), curr->target ).hasSideEffects ()) return ;
320
- handleCall (curr, outer);
338
+ optimize (curr, curr-> target , outer);
321
339
}
322
340
};
323
341
0 commit comments