@@ -188,6 +188,15 @@ static void optimizeBlock(Block* curr, Module* module) {
188188 }
189189 if (!child) continue ;
190190 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+ }
191200 ExpressionList merged (module ->allocator );
192201 for (size_t j = 0 ; j < i; j++) {
193202 merged.push_back (curr->list [j]);
@@ -279,11 +288,14 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
279288 }
280289
281290 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
282293 Block* outer = nullptr ;
283- outer = optimize (curr, curr->ifTrue , outer);
284294 if (EffectAnalyzer (getPassOptions (), curr->ifTrue ).hasSideEffects ()) return ;
285- outer = optimize (curr, curr->ifFalse , outer);
295+ outer = optimize (curr, curr->ifTrue , outer);
286296 if (EffectAnalyzer (getPassOptions (), curr->ifFalse ).hasSideEffects ()) return ;
297+ outer = optimize (curr, curr->ifFalse , outer);
298+ if (EffectAnalyzer (getPassOptions (), curr->condition ).hasSideEffects ()) return ;
287299 optimize (curr, curr->condition , outer);
288300 }
289301
@@ -299,11 +311,13 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
299311 }
300312
301313 template <typename T>
302- void handleCall (T* curr, Block* outer = nullptr ) {
314+ void handleCall (T* curr) {
315+ Block* outer = nullptr ;
303316 for (Index i = 0 ; i < curr->operands .size (); i++) {
304- outer = optimize (curr, curr->operands [i], outer);
305317 if (EffectAnalyzer (getPassOptions (), curr->operands [i]).hasSideEffects ()) return ;
318+ outer = optimize (curr, curr->operands [i], outer);
306319 }
320+ return ;
307321 }
308322
309323 void visitCall (Call* curr) {
@@ -315,9 +329,13 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
315329 }
316330
317331 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+ }
319337 if (EffectAnalyzer (getPassOptions (), curr->target ).hasSideEffects ()) return ;
320- handleCall (curr, outer);
338+ optimize (curr, curr-> target , outer);
321339 }
322340};
323341
0 commit comments