@@ -232,11 +232,39 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
232232 // this is already an if-else. if one side is a dead end, we can append to the other, if
233233 // there is no returned value to concern us
234234 assert (!isConcreteWasmType (iff->type )); // can't be, since in the middle of a block
235+
236+ // ensures the first node is a block, if it isn't already, and merges in the second,
237+ // either as a single element or, if a block, by appending to the first block. this
238+ // keeps the order of operations in place, that is, the appended element will be
239+ // executed after the first node's elements
240+ auto blockifyMerge = [&](Expression* any, Expression* append) -> Block* {
241+ Block* block = nullptr ;
242+ if (any) block = any->dynCast <Block>();
243+ // if the first isn't a block, or it's a block with a name (so we might
244+ // branch to the end, and so can't append to it, we might skip that code!)
245+ // then make a new block
246+ if (!block || block->name .is ()) {
247+ block = builder.makeBlock (any);
248+ } else {
249+ assert (!isConcreteWasmType (block->type ));
250+ }
251+ auto * other = append->dynCast <Block>();
252+ if (!other) {
253+ block->list .push_back (append);
254+ } else {
255+ for (auto * item : other->list ) {
256+ block->list .push_back (item);
257+ }
258+ }
259+ block->finalize ();
260+ return block;
261+ };
262+
235263 if (ExpressionAnalyzer::obviouslyDoesNotFlowOut (iff->ifTrue )) {
236- iff->ifFalse = builder. blockifyMerge (iff->ifFalse , builder.stealSlice (block, i + 1 , list.size ()));
264+ iff->ifFalse = blockifyMerge (iff->ifFalse , builder.stealSlice (block, i + 1 , list.size ()));
237265 return true ;
238266 } else if (ExpressionAnalyzer::obviouslyDoesNotFlowOut (iff->ifFalse )) {
239- iff->ifTrue = builder. blockifyMerge (iff->ifTrue , builder.stealSlice (block, i + 1 , list.size ()));
267+ iff->ifTrue = blockifyMerge (iff->ifTrue , builder.stealSlice (block, i + 1 , list.size ()));
240268 return true ;
241269 }
242270 }
0 commit comments