@@ -370,8 +370,6 @@ public static PreMainPostResult insertPrePostLoops(Loop loop) {
370
370
AbstractMergeNode postMergeNode = postEndNode .merge ();
371
371
graph .getDebug ().dump (DebugContext .VERY_DETAILED_LEVEL , graph , "After post loop duplication" );
372
372
373
- preLoopBegin .incrementSplits ();
374
- preLoopBegin .incrementSplits ();
375
373
preLoopBegin .setPreLoop ();
376
374
mainLoopBegin .setMainLoop ();
377
375
postLoopBegin .setPostLoop ();
@@ -454,8 +452,18 @@ public static PreMainPostResult insertPrePostLoops(Loop loop) {
454
452
assert mainLoopExitNode .predecessor () instanceof IfNode : Assertions .errorMessage (mainLoopExitNode );
455
453
assert postLoopExitNode .predecessor () instanceof IfNode : Assertions .errorMessage (postLoopExitNode );
456
454
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 );
459
467
460
468
if (graph .isAfterStage (StageFlag .VALUE_PROXY_REMOVAL )) {
461
469
// The pre and post loops don't require safepoints at all
@@ -473,7 +481,8 @@ public static PreMainPostResult insertPrePostLoops(Loop loop) {
473
481
474
482
/**
475
483
* 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.
477
486
*/
478
487
private static void setSingleVisitedLoopFrequencySplitProbability (AbstractBeginNode lex ) {
479
488
IfNode ifNode = ((IfNode ) lex .predecessor ());
@@ -482,11 +491,12 @@ private static void setSingleVisitedLoopFrequencySplitProbability(AbstractBeginN
482
491
}
483
492
484
493
/**
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.
487
497
*/
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 ;
490
500
if (probability <= 0D ) {
491
501
setSingleVisitedLoopFrequencySplitProbability (lex );
492
502
return ;
0 commit comments