Fix statement insertion point in GT_STORE_LCL_VAR decomposition
authorMike Danes <onemihaid@hotmail.com>
Wed, 22 Jun 2016 16:44:34 +0000 (19:44 +0300)
committerMike Danes <onemihaid@hotmail.com>
Wed, 22 Jun 2016 16:44:34 +0000 (19:44 +0300)
In some cases a newly created statement is inserted between the current statement and its embedded statements instead of being inserted after the current statement and its embedded statements.

Commit migrated from https://github.com/dotnet/coreclr/commit/b193390eca99368683835efdecb1f382b1443af3

src/coreclr/src/jit/lower.cpp

index 060a00e..9bc90b9 100755 (executable)
@@ -318,30 +318,49 @@ void Lowering::DecomposeNode(GenTreePtr* pTree, Compiler::fgWalkData* data)
                 nextTree->gtPrev = hiStore;
             }
             nextTree = hiRhs;
-            GenTreeStmt* stmt;
             GenTreeStmt* currStmt = comp->compCurStmt->AsStmt();
             bool isEmbeddedStmt = !currStmt->gtStmtIsTopLevel();
             if (!isEmbeddedStmt)
             {
                 tree->gtNext = nullptr;
                 hiRhs->gtPrev = nullptr;
-                stmt = comp->fgNewStmtFromTree(hiStore);
-                comp->fgInsertStmtAfter(comp->compCurBB, comp->compCurStmt, stmt);
             }
-            else
+
+            // TODO-Cleanup: DecomposeStoreInd contains identical code, 
+            // this should be moved to a common function
+            if (isEmbeddedStmt)
             {
+                // Find a parent statment containing storeIndHigh.
                 GenTree* parentStmt = currStmt;
                 while ((parentStmt != nullptr) && (!parentStmt->AsStmt()->gtStmtIsTopLevel()))
                 {
                     parentStmt = parentStmt->gtPrev;
                 }
                 assert(parentStmt);
-                stmt = comp->fgMakeEmbeddedStmt(comp->compCurBB, hiStore, parentStmt);
+
+                GenTreeStmt* stmt = comp->fgMakeEmbeddedStmt(comp->compCurBB, hiStore, parentStmt);
+                stmt->gtStmtILoffsx = comp->compCurStmt->gtStmt.gtStmtILoffsx;
+#ifdef DEBUG
+                stmt->gtStmtLastILoffs = comp->compCurStmt->gtStmt.gtStmtLastILoffs;
+#endif // DEBUG
             }
-            stmt->gtStmtILoffsx = comp->compCurStmt->gtStmt.gtStmtILoffsx;
+            else
+            {
+                GenTreeStmt* stmt = comp->fgNewStmtFromTree(hiStore);
+                stmt->gtStmtILoffsx = comp->compCurStmt->gtStmt.gtStmtILoffsx;
 #ifdef DEBUG
-            stmt->gtStmtLastILoffs = comp->compCurStmt->gtStmt.gtStmtLastILoffs;
+                stmt->gtStmtLastILoffs = comp->compCurStmt->gtStmt.gtStmtLastILoffs;
 #endif // DEBUG
+
+                // Find an insert point. Skip all embedded statements.
+                GenTree* insertPt = currStmt;
+                while ((insertPt->gtNext != nullptr) && (!insertPt->gtNext->AsStmt()->gtStmtIsTopLevel()))
+                {
+                    insertPt = insertPt->gtNext;
+                }
+
+                comp->fgInsertStmtAfter(comp->compCurBB, insertPt, stmt);
+            }
         }
         break;
     case GT_CAST: