@@ -204,8 +204,8 @@ bool Compiler::TypeInstantiationComplexityExceeds(CORINFO_CLASS_HANDLE handle, i
204204
205205class SubstitutePlaceholdersAndDevirtualizeWalker : public GenTreeVisitor <SubstitutePlaceholdersAndDevirtualizeWalker>
206206{
207- bool m_madeChanges = false ;
208- Statement* m_curStmt = nullptr ;
207+ bool m_madeChanges = false ;
208+ Statement* m_firstNewStmt = nullptr ;
209209
210210public:
211211 enum
@@ -226,20 +226,20 @@ class SubstitutePlaceholdersAndDevirtualizeWalker : public GenTreeVisitor<Substi
226226 }
227227
228228 // ------------------------------------------------------------------------
229- // WalkStatement: Walk the tree of a statement, and return the newly added
230- // statement if any, otherwise return the original statement.
229+ // WalkStatement: Walk the tree of a statement, and return the first newly
230+ // added statement if any, otherwise return the original statement.
231231 //
232232 // Arguments:
233233 // stmt - the statement to walk.
234234 //
235235 // Return Value:
236- // The newly added statement if any, or the original statement.
236+ // The first newly added statement if any, or the original statement.
237237 //
238238 Statement* WalkStatement (Statement* stmt)
239239 {
240- m_curStmt = stmt ;
241- WalkTree (m_curStmt ->GetRootNodePointer (), nullptr );
242- return m_curStmt ;
240+ m_firstNewStmt = nullptr ;
241+ WalkTree (stmt ->GetRootNodePointer (), nullptr );
242+ return m_firstNewStmt == nullptr ? stmt : m_firstNewStmt ;
243243 }
244244
245245 fgWalkResult PreOrderVisit (GenTree** use, GenTree* user)
@@ -616,23 +616,27 @@ class SubstitutePlaceholdersAndDevirtualizeWalker : public GenTreeVisitor<Substi
616616
617617 if (call->IsInlineCandidate ())
618618 {
619- CallArg* retBuffer = call->gtArgs .GetRetBufferArg ();
620-
621619 // If the call is the top-level expression in a statement, and it returns void,
622620 // there will be no use of its return value, and we can just inline it directly.
623621 // In this case we don't need to create a RET_EXPR node for it.
624- if (parent != nullptr || genActualType ( call->TypeGet ()) != TYP_VOID || retBuffer != nullptr )
622+ if (parent != nullptr || call->gtReturnType != TYP_VOID)
625623 {
626624 Statement* stmt = m_compiler->gtNewStmt (call);
627- m_compiler->fgInsertStmtBefore (m_compiler->compCurBB , m_curStmt, stmt);
625+ m_compiler->fgInsertStmtBefore (m_compiler->compCurBB , m_compiler->compCurStmt , stmt);
626+ if (m_firstNewStmt == nullptr )
627+ {
628+ m_firstNewStmt = stmt;
629+ }
630+
628631 GenTreeRetExpr* retExpr =
629632 m_compiler->gtNewInlineCandidateReturnExpr (call->AsCall (),
630633 genActualType (call->TypeGet ()));
631-
632634 call->GetSingleInlineCandidateInfo ()->retExpr = retExpr;
633635
634- m_curStmt = stmt;
635- *pTree = retExpr;
636+ JITDUMP (" Creating new RET_EXPR for [%06u]:\n " , call->gtTreeID );
637+ DISPTREE (retExpr);
638+
639+ *pTree = retExpr;
636640 }
637641
638642 call->GetSingleInlineCandidateInfo ()->exactContextHandle = context;
@@ -802,7 +806,8 @@ PhaseStatus Compiler::fgInline()
802806 // possible further optimization, as the (now complete) GT_RET_EXPR
803807 // replacement may have enabled optimizations by providing more
804808 // specific types for trees or variables.
805- stmt = walker.WalkStatement (stmt);
809+ compCurStmt = stmt;
810+ stmt = walker.WalkStatement (stmt);
806811
807812 GenTree* expr = stmt->GetRootNode ();
808813
0 commit comments