@@ -933,6 +933,7 @@ Loop::InsertLandingPad(FlowGraph *fg)
933933
934934 landingPadLabel->SetBasicBlock (landingPad);
935935 landingPadLabel->SetRegion (headBlock->GetFirstInstr ()->AsLabelInstr ()->GetRegion ());
936+ landingPadLabel->m_hasNonBranchRef = headBlock->GetFirstInstr ()->AsLabelInstr ()->m_hasNonBranchRef ;
936937 landingPad->SetBlockNum (fg->blockCount ++);
937938 landingPad->SetFirstInstr (landingPadLabel);
938939 landingPad->SetLastInstr (landingPadLabel);
@@ -1768,6 +1769,10 @@ FlowGraph::Destroy(void)
17681769 break ;
17691770 case Js::OpCode::BrOnNoException:
17701771 Assert (region->GetType () == RegionTypeTry || region->GetType () == RegionTypeCatch || region->GetType () == RegionTypeFinally ||
1772+ // A BrOnException from finally to early exit can be converted to BrOnNoException and Br
1773+ // The Br block maybe a common successor block for early exit along with the BrOnNoException block
1774+ // Region from Br block will be picked up from a predecessor which is not BrOnNoException due to early exit
1775+ // See test0() in test/EH/tryfinallytests.js
17711776 (predRegion->GetType () == RegionTypeFinally && predBlock->GetLastInstr ()->AsBranchInstr ()->m_brFinallyToEarlyExit ));
17721777 break ;
17731778 case Js::OpCode::Br:
@@ -1782,11 +1787,13 @@ FlowGraph::Destroy(void)
17821787 }
17831788 else if (region->GetType () == RegionTypeFinally && region != predRegion)
17841789 {
1785- AssertMsg (predRegion->GetType () == RegionTypeTry, " Bad region type for the try" );
1790+ // We may be left with edges from finally region to early exit
1791+ AssertMsg (predRegion->IsNonExceptingFinally () || predRegion->GetType () == RegionTypeTry, " Bad region type for the try" );
17861792 }
17871793 else
17881794 {
1789- AssertMsg (region == predRegion, " Bad region propagation through interior block" );
1795+ // We may be left with edges from finally region to early exit
1796+ AssertMsg (predRegion->IsNonExceptingFinally () || region == predRegion, " Bad region propagation through interior block" );
17901797 }
17911798 break ;
17921799 default :
@@ -1895,7 +1902,6 @@ FlowGraph::UpdateRegionForBlock(BasicBlock * block)
18951902 // If a LeaveNull block had only BrOnException edges from predecessor
18961903 // We will end up in inaccurate region propagation if we propagate from the predecessor edges
18971904 // So pick up the finally region from the map
1898- Assert (this ->leaveNullLabelToFinallyLabelMap ->ContainsKey (block->GetFirstInstr ()->AsLabelInstr ()));
18991905 IR::LabelInstr * finallyLabel = this ->leaveNullLabelToFinallyLabelMap ->Item (block->GetFirstInstr ()->AsLabelInstr ());
19001906 Assert (finallyLabel);
19011907 region = finallyLabel->GetRegion ();
@@ -1939,7 +1945,11 @@ FlowGraph::UpdateRegionForBlock(BasicBlock * block)
19391945 labelInstr->m_hasNonBranchRef = true ;
19401946 }
19411947
1942- if (region && this ->func ->HasFinally () && region->GetType () == RegionTypeRoot && !labelInstr->m_hasNonBranchRef )
1948+ // One of the pred blocks maybe an eh region, in that case it is important to mark this label's m_hasNonBranchRef
1949+ // If not later in codegen, this label can get deleted. And during SccLiveness, region is propagated to newly created labels in lowerer from the previous label's region
1950+ // We can end up assigning an eh region to a label in a non eh region. And if there is a bailout in such a region, bad things will happen in the interpreter :)
1951+ // See test2() in tryfinallytests.js
1952+ if (region && region->GetType () == RegionTypeRoot && !labelInstr->m_hasNonBranchRef )
19431953 {
19441954 FOREACH_PREDECESSOR_BLOCK (predBlock, block)
19451955 {
@@ -2440,7 +2450,7 @@ FlowGraph::InsertCompensationCodeForBlockMove(FlowEdge * edge, bool insertToLoo
24402450
24412451 bool assignRegionsBeforeGlobopt = this ->func ->HasTry () && (this ->func ->DoOptimizeTry () ||
24422452 (this ->func ->IsSimpleJit () && this ->func ->hasBailout ));
2443- // MGTODO : maybe we can just set the pred region instead of calling Propagate function ?
2453+
24442454 if (assignRegionsBeforeGlobopt)
24452455 {
24462456 UpdateRegionForBlockFromEHPred (compBlock);
0 commit comments