Skip to content

Commit 8de4345

Browse files
committed
Fix adapting of loop exit probabilities.
1 parent f734b47 commit 8de4345

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/loop/phases/LoopTransformations.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,6 @@ public static PreMainPostResult insertPrePostLoops(Loop loop) {
370370
AbstractMergeNode postMergeNode = postEndNode.merge();
371371
graph.getDebug().dump(DebugContext.VERY_DETAILED_LEVEL, graph, "After post loop duplication");
372372

373-
preLoopBegin.incrementSplits();
374-
preLoopBegin.incrementSplits();
375373
preLoopBegin.setPreLoop();
376374
mainLoopBegin.setMainLoop();
377375
postLoopBegin.setPostLoop();
@@ -454,8 +452,18 @@ public static PreMainPostResult insertPrePostLoops(Loop loop) {
454452
assert mainLoopExitNode.predecessor() instanceof IfNode : Assertions.errorMessage(mainLoopExitNode);
455453
assert postLoopExitNode.predecessor() instanceof IfNode : Assertions.errorMessage(postLoopExitNode);
456454

457-
setSingleVisitedLoopFrequencySplitProbability(preLoopExitNode);
458-
setSingleVisitedLoopFrequencySplitProbability(postLoopExitNode);
455+
/*
456+
* The bodies of pre and post loops are assumed to be executed just once. As the local loop
457+
* frequency is calculated from the loop exit probabilities, it has to be taken into account
458+
* how often the exit check is performed. If the loop is inverted, i.e., tail-counted, the
459+
* exit will be taken at the end of the first body execution. Thus, the frequency of the
460+
* exit check is 1. If the loop is head-counted, the exit check will be performed twice,
461+
* which is reflected by a frequency of 2. This results in the correct relative frequency
462+
* being propagated into the loop body.
463+
*/
464+
final int prePostFrequency = loop.counted().isInverted() ? 1 : 2;
465+
adaptCountedLoopExitProbability(preLoopExitNode, prePostFrequency);
466+
adaptCountedLoopExitProbability(postLoopExitNode, prePostFrequency);
459467

460468
if (graph.isAfterStage(StageFlag.VALUE_PROXY_REMOVAL)) {
461469
// The pre and post loops don't require safepoints at all
@@ -473,7 +481,8 @@ public static PreMainPostResult insertPrePostLoops(Loop loop) {
473481

474482
/**
475483
* Inject a split probability for the (counted) loop check that will result in a loop frequency
476-
* of 1 (in case this is the only loop exit).
484+
* of 1 (in case this is the only loop exit). This implies that the loop body is expected to be
485+
* never entered.
477486
*/
478487
private static void setSingleVisitedLoopFrequencySplitProbability(AbstractBeginNode lex) {
479488
IfNode ifNode = ((IfNode) lex.predecessor());
@@ -482,11 +491,12 @@ private static void setSingleVisitedLoopFrequencySplitProbability(AbstractBeginN
482491
}
483492

484493
/**
485-
* Inject a new frequency for the condition dominating the given loop exit path. This
486-
* calculation will act as if the given loop exit is the only exit of the loop.
494+
* Inject a new branch probability for the condition dominating the given loop exit path. This
495+
* probability is based on the local frequency of the exit check. This calculation will act as
496+
* if the given loop exit is the only exit of the loop.
487497
*/
488-
public static void adaptCountedLoopExitProbability(AbstractBeginNode lex, double newFrequency) {
489-
double probability = 1.0D - 1.0D / newFrequency;
498+
public static void adaptCountedLoopExitProbability(AbstractBeginNode lex, double newExitCheckFrequency) {
499+
double probability = 1.0D - 1.0D / newExitCheckFrequency;
490500
if (probability <= 0D) {
491501
setSingleVisitedLoopFrequencySplitProbability(lex);
492502
return;

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/LoopBeginNode.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ public LoopBeginNode() {
210210
super(TYPE);
211211
loopOrigFrequency = 1;
212212
unswitches = 0;
213-
splits = 0;
214213
loopEndsSafepointState = SafepointState.ENABLED;
215214
loopExitsSafepointState = SafepointState.ENABLED;
216215
guestLoopEndsSafepointState = SafepointState.ENABLED;
@@ -483,10 +482,6 @@ public EndNode forwardEnd() {
483482
return forwardEndAt(0);
484483
}
485484

486-
public void incrementSplits() {
487-
splits++;
488-
}
489-
490485
public int peelings() {
491486
return peelings;
492487
}

0 commit comments

Comments
 (0)