diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index 29a277a9a2e34916fef1a18361b677b0baf5cea4..6649038c40ad386b9019ae06edabbb159b58a7b6 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -11033,7 +11033,7 @@ 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->AreBlocksInSameLoops(NewFTNum, NewNFTNum));
+	bool LoopExitCase = this->IsBlockInAnyLoop(LeftBlockNum) && this->IsBlockInAnyLoop(RightBlockNum) && (!(this->AreBlocksInSameLoops(NewFTNum, NewNFTNum)));
 	STARSCFGBlock *LeftCFGBlock = this->ShadowCFGBlocks[LeftBlockNum];
 	STARSCFGBlock *RightCFGBlock = this->ShadowCFGBlocks[RightBlockNum];