Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/coreclr/jit/loopcloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2067,8 +2067,19 @@ void Compiler::optCloneLoop(FlowGraphNaturalLoop* loop, LoopCloneContext* contex
// bottomRedirBlk [BBJ_ALWAYS --> bottomNext]
// ... slow cloned loop (not yet inserted)
// bottomNext
BasicBlock* bottom = loop->GetLexicallyBottomMostBlock();
BasicBlock* newPred = bottom;
BasicBlock* bottom = loop->GetLexicallyBottomMostBlock();
BasicBlock* beforeSlowPreheader = bottom;

// Ensure the slow loop preheader ends up in the same EH region
// as the preheader.
//
bool inTry = false;
unsigned const enclosingRegion = ehGetMostNestedRegionIndex(preheader, &inTry);
if (!BasicBlock::sameEHRegion(beforeSlowPreheader, preheader))
{
beforeSlowPreheader = fgFindInsertPoint(enclosingRegion, inTry, bottom, /* endBlk */ nullptr,
/* nearBlk */ bottom, /* jumpBlk */ nullptr, /* runRarely */ false);
}

// Create a new preheader for the slow loop immediately before the slow
// loop itself. All failed conditions will branch to the slow preheader.
Expand All @@ -2078,22 +2089,20 @@ void Compiler::optCloneLoop(FlowGraphNaturalLoop* loop, LoopCloneContext* contex
// The slow preheader needs to go in the same EH region as the preheader.
//
JITDUMP("Create unique preheader for slow path loop\n");
const bool extendRegion = BasicBlock::sameEHRegion(bottom, preheader);
BasicBlock* slowPreheader = fgNewBBafter(BBJ_ALWAYS, newPred, extendRegion);
JITDUMP("Adding " FMT_BB " after " FMT_BB "\n", slowPreheader->bbNum, newPred->bbNum);
slowPreheader->bbWeight = newPred->isRunRarely() ? BB_ZERO_WEIGHT : ambientWeight;
slowPreheader->CopyFlags(newPred, (BBF_PROF_WEIGHT | BBF_RUN_RARELY));
const bool extendRegion = BasicBlock::sameEHRegion(beforeSlowPreheader, preheader);
BasicBlock* slowPreheader = fgNewBBafter(BBJ_ALWAYS, beforeSlowPreheader, extendRegion);
JITDUMP("Adding " FMT_BB " after " FMT_BB "\n", slowPreheader->bbNum, beforeSlowPreheader->bbNum);
slowPreheader->bbWeight = preheader->isRunRarely() ? BB_ZERO_WEIGHT : ambientWeight;
slowPreheader->CopyFlags(preheader, (BBF_PROF_WEIGHT | BBF_RUN_RARELY));
slowPreheader->scaleBBWeight(LoopCloneContext::slowPathWeightScaleFactor);

// If we didn't extend the region above (because the last loop
// If we didn't extend the region above (because the beforeSlowPreheader
// block was in some enclosed EH region), put the slow preheader
// into the appropriate region, and make appropriate extent updates.
//
if (!extendRegion)
{
slowPreheader->copyEHRegion(preheader);
bool isTry = false;
unsigned enclosingRegion = ehGetMostNestedRegionIndex(slowPreheader, &isTry);

if (enclosingRegion != 0)
{
Expand All @@ -2111,11 +2120,11 @@ void Compiler::optCloneLoop(FlowGraphNaturalLoop* loop, LoopCloneContext* contex
}
}
}
newPred = slowPreheader;

// Now we'll clone the blocks of the loop body. These cloned blocks will be the slow path.

//
BlockToBlockMap* blockMap = new (getAllocator(CMK_LoopClone)) BlockToBlockMap(getAllocator(CMK_LoopClone));
BasicBlock* newPred = slowPreheader;

if (cloneLoopsWithEH)
{
Expand Down
Loading