diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 6649038c40ad386b9019ae06edabbb159b58a7b6..1c5aeb1e992cd227a1da4ce4116c3d4026cea26d 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -11033,7 +11033,11 @@ void SMPFunction::BuildShadowCFG(void) {
 void SMPFunction::CoalesceShadowBlocks(int LeftBlockNum, int RightBlockNum, bool NegateLeft, SMPoperator TopOper, int NewFTNum, int NewNFTNum) {
 	assert((0 <= LeftBlockNum) && (LeftBlockNum < (int)this->ShadowCFGBlocks.size()));
 	assert((0 <= RightBlockNum) && (RightBlockNum < (int)this->ShadowCFGBlocks.size()));
-	bool LoopExitCase = this->IsBlockInAnyLoop(LeftBlockNum) && this->IsBlockInAnyLoop(RightBlockNum) && (!(this->AreBlocksInSameLoops(NewFTNum, NewNFTNum)));
+	bool InsideLoop = this->IsBlockInAnyLoop(LeftBlockNum);
+	int InnerMostLoopNum = InsideLoop ? this->GetInnermostLoopNum(LeftBlockNum) : -1;
+	// Are both successor blocks in the same innermost loop? If not, this is a loop exit,
+	//  Note that we could enter into a still more innermost loop as long as we don't exit the current one.
+	bool LoopExitCase =  InsideLoop && (!(this->IsBlockInLoop(NewFTNum, (size_t) InnerMostLoopNum) && this->IsBlockInLoop(NewNFTNum, (size_t) InnerMostLoopNum)));
 	STARSCFGBlock *LeftCFGBlock = this->ShadowCFGBlocks[LeftBlockNum];
 	STARSCFGBlock *RightCFGBlock = this->ShadowCFGBlocks[RightBlockNum];