diff --git a/src/qml/compiler/qv4ssa.cpp b/src/qml/compiler/qv4ssa.cpp
index 31e3ed867e3f4004a6494504ae1d37240831c8e2..d2222a04585315af5076a07e9f8651128b8de554 100644
--- a/src/qml/compiler/qv4ssa.cpp
+++ b/src/qml/compiler/qv4ssa.cpp
@@ -2991,7 +2991,19 @@ void splitCriticalEdges(IR::Function *f, DominatorTree &df, StatementWorklist &w
             toBB->in[inIdx] = newBB;
             newBB->out.append(toBB);
 
-            newBB->setContainingGroup(toBB->containingGroup());
+            // add newBB to the correct loop group
+            if (toBB->isGroupStart()) {
+                BasicBlock *container;
+                for (container = fromBB->containingGroup(); container; container = container->containingGroup())
+                     if (container == toBB)
+                         break;
+                if (container == toBB) // if we were already inside the toBB loop
+                    newBB->setContainingGroup(toBB);
+                else
+                    newBB->setContainingGroup(toBB->containingGroup());
+            } else {
+                newBB->setContainingGroup(toBB->containingGroup());
+            }
 
             // patch the terminator
             Stmt *terminator = fromBB->terminator();