Ensure scratch BB in tail recursion -> loop xform
authorJoseph Tremoulet <jotrem@microsoft.com>
Wed, 9 Aug 2017 02:11:23 +0000 (22:11 -0400)
committerJoseph Tremoulet <jotrem@microsoft.com>
Wed, 9 Aug 2017 02:11:23 +0000 (22:11 -0400)
Our loop detection requires a predecessor outside the loop, so without
this the loops often don't get optimized as such.

src/jit/morph.cpp

index c1312aa..3972f56 100644 (file)
@@ -7701,9 +7701,14 @@ void Compiler::fgMorphRecursiveFastTailCallIntoLoop(BasicBlock* block, GenTreeCa
     // Remove the call
     fgRemoveStmt(block, last);
 
-    // Set the loop edge.
+    // Set the loop edge.  Ensure we have a scratch block and then target the
+    // next block.  Loop detection needs to see a pred out of the loop, so
+    // mark the scratch block BBF_DONT_REMOVE to prevent empty block removal
+    // on it.
+    fgEnsureFirstBBisScratch();
+    fgFirstBB->bbFlags |= BBF_DONT_REMOVE;
     block->bbJumpKind = BBJ_ALWAYS;
-    block->bbJumpDest = fgFirstBBisScratch() ? fgFirstBB->bbNext : fgFirstBB;
+    block->bbJumpDest = fgFirstBB->bbNext;
     fgAddRefPred(block->bbJumpDest, block);
     block->bbFlags &= ~BBF_HAS_JMP;
 }