← Back to Pull requests

[GVN] Allow loop-load PRE when the blocker is in an inner loop #227983

Open
anupkum-sgs opened this pull request with 513 lines diff across 6 files
View on GitHub
anupkum-sgs anupkum-sgs commented on Oct 01, 2026 07:20 UTC
Author

performLoopLoadPRE does not perform PRE when the blocker is inside an
inner loop. This patch allows the inner-loop blocker, and the outermost
exit block of the inner loop is selected for reload insertion, so that
the reload runs once each time the inner loop is left.

canBeFreed used by GVN is very conservative. Even if there is no free
between LoadPtr's load and the point where the reload would be inserted
(inside the outermost exit block), it can still return true. If the
address can be freed, the reload is still legal when willNotFreeBetween
finds no deallocation between the header load and that reload.

The non-linear walk in willNotFreeBetween landed in #223580. This patch
uses it along with canBeFreed and raises MaxInstrsToCheckForFree from
32 to 200, so that nested and larger loops can be covered and more
opportunities are found.

RFC: https://discourse.llvm.org/t/rfc-extending-gvns-loop-load-pre/91740

Diff: 513 lines (+418 / -95) across 6 files Reviewer: nikic Review duration: 87201m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.55) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.55 · 513 lines diff across 6 files
0.55
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.93 · 4 substantive comment(s)
0.93
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
88%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.373
Real PR Conversation Timeline
6 items
github-actions[bot] github-actions[bot] commented on Oct 01, 2026 07:21 UTC
Bot

<!--LLVM NEW CONTRIBUTOR COMMENT-->

Hello @anupkum-sgs :wave:

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
The InstCombine Contributor Guide lays out a series of rules that contributions to InstCombine (and other middle-end components) should follow.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.

---

Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.

---

If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 01, 2026 07:22 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: anupkum-sgs

<details>
<summary>Changes</summary>

performLoopLoadPRE does not perform PRE when the blocker is inside an
inner loop. This patch allows the inner-loop blocker, and the outermost
exit block of the inner loop is selected for reload insertion, so that
the reload runs once each time the inner loop is left.

canBeFreed used by GVN is very conservative. Even if there is no free
between LoadPtr's load and the point where the reload would be inserted
(inside the outermost exit block), it can still return true. If the
address can be freed, the reload is still legal when willNotFreeBetween
finds no deallocation between the header load and that reload.

The non-linear walk in willNotFreeBetween landed in #<!-- -->223580. This patch
uses it along with canBeFreed and raises MaxInstrsToCheckForFree from
32 to 200, so that nested and larger loops can be covered and more
opportunities are found.

RFC: https://discourse.llvm.org/t/rfc-extending-gvns-loop-load-pre/91740

---

Patch is 37.64 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/227983.diff

6 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+1-1)

  • (modified) llvm/lib/Transforms/Scalar/GVN.cpp (+30-4)

  • (modified) llvm/test/Transforms/GVN/PRE/pre-aliasning-path.ll (+14-6)

  • (modified) llvm/test/Transforms/GVN/PRE/pre-loop-load.ll (+343-13)

  • (modified) llvm/test/Transforms/LoopUnroll/unroll-cleanup.ll (+15-10)

  • (modified) llvm/test/Transforms/PhaseOrdering/X86/ptrtoaddr-ptrtoint.ll (+15-61)
  • ``````

    diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
    index f56375e4dbe73bf..71e283c086246f8 100644
    --- a/llvm/lib/Analysis/ValueTracking.cpp
    +++ b/llvm/lib/Analysis/ValueTracking.cpp
    @@ -93,7 +93,7 @@ static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
     
     /// Maximum number of instructions to check between assume and context
     /// instruction.
    -static constexpr unsigned MaxInstrsToCheckForFree = 32;
    +static constexpr unsigned MaxInstrsToCheckForFree = 200;
     
     template <typename InstTy>
     static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
    diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
    index b8a44882af86712..18ac54555f24e74 100644
    --- a/llvm/lib/Transforms/Scalar/GVN.cpp
    +++ b/llvm/lib/Transforms/Scalar/GVN.cpp
    @@ -2008,9 +2008,29 @@ bool GVNPass::performLoopLoadPRE(LoadInst *Load,
         if (LoopBlock)
           return false;
     
    -    // Do not sink into inner loops. This may be non-profitable.
    -    if (L != LI->getLoopFor(Blocker))
    -      return false;
    +    // A reload at the end of a nested blocker runs once per inner iteration,
    +    // which is more often than the header. Put it on the exit of the inner
    +    // loop that is directly inside L instead. That block is on every path
    +    // from the blocker back to L's header, and it runs once each time that inner
    +    // loop is left.
    +    if (const Loop *Inner = LI->getLoopFor(Blocker); Inner != L) {
    +      // The blocker is inside Inner, which may itself be nested further down.
    +      // Walk parents until Inner is the loop directly inside L that contains
    +      // it.
    +      while (Inner->getParentLoop() != L)
    +        Inner = Inner->getParentLoop();
    +
    +      BasicBlock *Exit = Inner->getExitBlock();
    +
    +      // The exit has to be in L. If it lands in another loop, such as a sibling
    +      // of Inner, the reload runs once per iteration of that loop, which can
    +      // be more often than L's header. getExitBlock() is null when Inner has
    +      // more than one exit, and PRE is aborted in that case.
    +      if (!Exit || LI->getLoopFor(Exit) != L)
    +        return false;
    +
    +      Blocker = Exit;
    +    }
     
         // Blocks that dominate the latch execute on every single iteration, maybe
         // except the last one. So PREing into these blocks doesn't make much sense
    @@ -2032,7 +2052,13 @@ bool GVNPass::performLoopLoadPRE(LoadInst *Load,
     
       // Make sure the memory at this pointer cannot be freed, therefore we can
       // safely reload from it after clobber.
    -  if (LoadPtr->canBeFreed())
    +  //
    +  // The header load has already dereferenced LoadPtr on this iteration, so
    +  // only a deallocation between that load and the reload in LoopBlock can make
    +  // the same address unsafe to read again. Check every path between these two
    +  // points for an instruction that may deallocate the memory.
    +  if (LoadPtr->canBeFreed() &&
    +      !willNotFreeBetween(Load, LoopBlock->getTerminator(), DT))
         return false;
     
       // TODO: Support critical edge splitting if blocker has more than 1 successor.
    diff --git a/llvm/test/Transforms/GVN/PRE/pre-aliasning-path.ll b/llvm/test/Transforms/GVN/PRE/pre-aliasning-path.ll
    index 7dc884f87e3605a..3d4feb76aaa44a6 100644
    --- a/llvm/test/Transforms/GVN/PRE/pre-aliasning-path.ll
    +++ b/llvm/test/Transforms/GVN/PRE/pre-aliasning-path.ll
    @@ -8,22 +8,26 @@ declare void @side_effect_1(i32 %x) nofree
     
     declare void @no_side_effect() readonly
     
    -; TODO: We can PRE the load into the cold path, removing it from the hot path.
    +; We can PRE the load into the cold path, removing it from the hot path: the
    +; callee is nofree, so %p cannot be deallocated between the loads.
     define i32 @test_01(ptr %p) {
     ; CHECK-LABEL: @test_01(
     ; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE1:%.*]] = load i32, ptr [[P:%.*]], align 4
     ; CHECK-NEXT:    br label [[LOOP:%.*]]
     ; CHECK:       loop:
    -; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    -; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE1]], [[ENTRY:%.*]] ], [ [[X2:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
     ; CHECK-NEXT:    [[COND:%.*]] = icmp ult i32 [[X]], 100
     ; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
     ; CHECK:       hot_path:
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       cold_path:
     ; CHECK-NEXT:    call void @side_effect_0() #[[ATTR0:[0-9]+]]
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       backedge:
    +; CHECK-NEXT:    [[X2]] = phi i32 [ [[X_PRE]], [[COLD_PATH]] ], [ [[X]], [[HOT_PATH]] ]
     ; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
     ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
     ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    @@ -55,22 +59,26 @@ exit:
       ret i32 %x
     }
     
    -; TODO: We can PRE the load into the cold path, removing it from the hot path.
    +; We can PRE the load into the cold path, removing it from the hot path: the
    +; callee is nofree, so %p cannot be deallocated between the loads.
     define i32 @test_02(ptr %p) {
     ; CHECK-LABEL: @test_02(
     ; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE1:%.*]] = load i32, ptr [[P:%.*]], align 4
     ; CHECK-NEXT:    br label [[LOOP:%.*]]
     ; CHECK:       loop:
    -; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    -; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE1]], [[ENTRY:%.*]] ], [ [[X2:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
     ; CHECK-NEXT:    [[COND:%.*]] = icmp ult i32 [[X]], 100
     ; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
     ; CHECK:       hot_path:
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       cold_path:
     ; CHECK-NEXT:    call void @side_effect_1(i32 [[X]]) #[[ATTR0]]
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       backedge:
    +; CHECK-NEXT:    [[X2]] = phi i32 [ [[X_PRE]], [[COLD_PATH]] ], [ [[X]], [[HOT_PATH]] ]
     ; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
     ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
     ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    diff --git a/llvm/test/Transforms/GVN/PRE/pre-loop-load.ll b/llvm/test/Transforms/GVN/PRE/pre-loop-load.ll
    index 8f1b73ad8383d92..ce22a95bbfc20c9 100644
    --- a/llvm/test/Transforms/GVN/PRE/pre-loop-load.ll
    +++ b/llvm/test/Transforms/GVN/PRE/pre-loop-load.ll
    @@ -107,22 +107,26 @@ exit:
     }
     
     
    -; TODO: We can PRE the load away from the hot path.
    +; We can PRE the load away from the hot path: @side_effect is nofree, so the
    +; object under %p cannot be deallocated between the loads.
     define i32 @test_load_on_cold_path(ptr %p) {
     ; CHECK-LABEL: @test_load_on_cold_path(
     ; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE1:%.*]] = load i32, ptr [[P:%.*]], align 4
     ; CHECK-NEXT:    br label [[LOOP:%.*]]
     ; CHECK:       loop:
    -; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    -; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE1]], [[ENTRY:%.*]] ], [ [[X2:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
     ; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
     ; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
     ; CHECK:       hot_path:
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       cold_path:
     ; CHECK-NEXT:    call void @side_effect() #[[ATTR0:[0-9]+]]
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       backedge:
    +; CHECK-NEXT:    [[X2]] = phi i32 [ [[X_PRE]], [[COLD_PATH]] ], [ [[X]], [[HOT_PATH]] ]
     ; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
     ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
     ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    @@ -355,22 +359,26 @@ exit:
       ret i32 %x
     }
     
    -; TODO: We can PRE via splitting of the critical edge in the cold path.
    +; We PRE into the cold path. TODO: split the critical edge so that the reload
    +; does not also run on the path that leaves the loop.
     define i32 @test_load_on_exiting_cold_path_01(ptr %p) {
     ; CHECK-LABEL: @test_load_on_exiting_cold_path_01(
     ; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE2:%.*]] = load i32, ptr [[P:%.*]], align 4
     ; CHECK-NEXT:    br label [[LOOP:%.*]]
     ; CHECK:       loop:
    -; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    -; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE2]], [[ENTRY:%.*]] ], [ [[X3:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
     ; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
     ; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
     ; CHECK:       hot_path:
     ; CHECK-NEXT:    br label [[BACKEDGE]]
     ; CHECK:       cold_path:
     ; CHECK-NEXT:    [[SIDE_COND:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
     ; CHECK-NEXT:    br i1 [[SIDE_COND]], label [[BACKEDGE]], label [[COLD_EXIT:%.*]]
     ; CHECK:       backedge:
    +; CHECK-NEXT:    [[X3]] = phi i32 [ [[X_PRE]], [[COLD_PATH]] ], [ [[X]], [[HOT_PATH]] ]
     ; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
     ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
     ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    @@ -570,14 +578,17 @@ exit:
       ret i32 %x
     }
     
    -; TODO: We can PRE via splitting of the critical edge in the cold path. Make sure we only insert 1 load.
    +; We PRE into the last cold block, inserting exactly 1 load. TODO: split the
    +; critical edge so that the reload does not also run on the path that leaves the
    +; loop.
     define i32 @test_load_on_multi_exiting_cold_path(ptr %p) {
     ; CHECK-LABEL: @test_load_on_multi_exiting_cold_path(
     ; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE2:%.*]] = load i32, ptr [[P:%.*]], align 4
     ; CHECK-NEXT:    br label [[LOOP:%.*]]
     ; CHECK:       loop:
    -; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    -; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE2]], [[ENTRY:%.*]] ], [ [[X3:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
     ; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
     ; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH_1:%.*]]
     ; CHECK:       hot_path:
    @@ -590,8 +601,10 @@ define i32 @test_load_on_multi_exiting_cold_path(ptr %p) {
     ; CHECK-NEXT:    br i1 [[SIDE_COND_2]], label [[COLD_PATH_3:%.*]], label [[COLD_EXIT]]
     ; CHECK:       cold_path.3:
     ; CHECK-NEXT:    [[SIDE_COND_3:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
     ; CHECK-NEXT:    br i1 [[SIDE_COND_3]], label [[BACKEDGE]], label [[COLD_EXIT]]
     ; CHECK:       backedge:
    +; CHECK-NEXT:    [[X3]] = phi i32 [ [[X_PRE]], [[COLD_PATH_3]] ], [ [[X]], [[HOT_PATH]] ]
     ; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
     ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
     ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    @@ -636,7 +649,8 @@ cold_exit:
       ret i32 -1
     }
     
    -; TODO: PRE via splittinga backedge in the cold loop. Make sure we don't insert a load into an inner loop.
    +; Do not PRE. The inner loop's unique exit is the outer latch %backedge, so the
    +; remapped reload site fails the latch-dominance profitability check.
     define i32 @test_inner_loop(ptr %p, i1 %arg) {
     ; CHECK-LABEL: @test_inner_loop(
     ; CHECK-NEXT:  entry:
    @@ -773,14 +787,16 @@ exit:
       ret i32 %x
     }
     
    -; TODO: We can PRE via split of critical edge.
    +; We PRE into the call block. TODO: split the critical edge so that the reload
    +; does not also run on the path that leaves the loop.
     define i32 @test_side_exit_after_merge(ptr %p) {
     ; CHECK-LABEL: @test_side_exit_after_merge(
     ; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE2:%.*]] = load i32, ptr [[P:%.*]], align 4
     ; CHECK-NEXT:    br label [[LOOP:%.*]]
     ; CHECK:       loop:
    -; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    -; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE2]], [[ENTRY:%.*]] ], [ [[X3:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
     ; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
     ; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
     ; CHECK:       hot_path:
    @@ -790,11 +806,14 @@ define i32 @test_side_exit_after_merge(ptr %p) {
     ; CHECK-NEXT:    br i1 [[COND_1]], label [[DO_CALL:%.*]], label [[SIDE_EXITING:%.*]]
     ; CHECK:       do_call:
     ; CHECK-NEXT:    [[SIDE_COND:%.*]] = call i1 @side_effect_cond() #[[ATTR0]]
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
     ; CHECK-NEXT:    br label [[SIDE_EXITING]]
     ; CHECK:       side_exiting:
    +; CHECK-NEXT:    [[X4:%.*]] = phi i32 [ [[X_PRE]], [[DO_CALL]] ], [ 0, [[COLD_PATH]] ]
     ; CHECK-NEXT:    [[SIDE_COND_PHI:%.*]] = phi i1 [ [[SIDE_COND]], [[DO_CALL]] ], [ true, [[COLD_PATH]] ]
     ; CHECK-NEXT:    br i1 [[SIDE_COND_PHI]], label [[BACKEDGE]], label [[COLD_EXIT:%.*]]
     ; CHECK:       backedge:
    +; CHECK-NEXT:    [[X3]] = phi i32 [ [[X4]], [[SIDE_EXITING]] ], [ [[X]], [[HOT_PATH]] ]
     ; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
     ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
     ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    @@ -940,3 +959,314 @@ backedge:
     exit:
       ret i32 %x
     }
    +
    +; The blocker sits inside an inner loop. Reloading there would run once per
    +; inner iteration, which is more often than the header we are relieving, so the
    +; reload goes to the inner loop's exit block instead.
    +define i32 @test_blocker_in_inner_loop(ptr %p, i1 %arg) {
    +; CHECK-LABEL: @test_blocker_in_inner_loop(
    +; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE1:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    br label [[LOOP:%.*]]
    +; CHECK:       loop:
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE1]], [[ENTRY:%.*]] ], [ [[X2:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
    +; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
    +; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
    +; CHECK:       hot_path:
    +; CHECK-NEXT:    br label [[BACKEDGE]]
    +; CHECK:       cold_path:
    +; CHECK-NEXT:    br label [[INNER_LOOP:%.*]]
    +; CHECK:       inner_loop:
    +; CHECK-NEXT:    call void @side_effect() #[[ATTR0]]
    +; CHECK-NEXT:    br i1 [[ARG:%.*]], label [[INNER_LOOP]], label [[INNER_EXIT:%.*]]
    +; CHECK:       inner_exit:
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
    +; CHECK-NEXT:    br label [[BACKEDGE]]
    +; CHECK:       backedge:
    +; CHECK-NEXT:    [[X2]] = phi i32 [ [[X_PRE]], [[INNER_EXIT]] ], [ [[X]], [[HOT_PATH]] ]
    +; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
    +; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
    +; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    +; CHECK:       exit:
    +; CHECK-NEXT:    ret i32 [[X]]
    +;
    +entry:
    +  br label %loop
    +
    +loop:
    +  %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
    +  %x = load i32, ptr %p
    +  %cond = icmp ne i32 %x, 0
    +  br i1 %cond, label %hot_path, label %cold_path
    +
    +hot_path:
    +  br label %backedge
    +
    +cold_path:
    +  br label %inner_loop
    +
    +inner_loop:
    +  call void @side_effect() nofree
    +  br i1 %arg, label %inner_loop, label %inner_exit
    +
    +inner_exit:
    +  br label %backedge
    +
    +backedge:
    +  %iv.next = add i32 %iv, %x
    +  %loop.cond = icmp ult i32 %iv.next, 1000
    +  br i1 %loop.cond, label %loop, label %exit
    +
    +exit:
    +  ret i32 %x
    +}
    +
    +; Same, with the blocker two levels deep. The reload goes to the exit block of
    +; the loop directly nested in the loop being optimized, not to the exit of the
    +; loop immediately containing the blocker.
    +define i32 @test_blocker_in_inner_loop_nested(ptr %p, i1 %inner.back, i1 %mid.back) {
    +; CHECK-LABEL: @test_blocker_in_inner_loop_nested(
    +; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    [[X_PRE1:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    br label [[LOOP:%.*]]
    +; CHECK:       loop:
    +; CHECK-NEXT:    [[X:%.*]] = phi i32 [ [[X_PRE1]], [[ENTRY:%.*]] ], [ [[X2:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE]] ]
    +; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
    +; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[COLD_PATH:%.*]]
    +; CHECK:       hot_path:
    +; CHECK-NEXT:    br label [[BACKEDGE]]
    +; CHECK:       cold_path:
    +; CHECK-NEXT:    br label [[MID_LOOP:%.*]]
    +; CHECK:       mid_loop:
    +; CHECK-NEXT:    br label [[INNER_LOOP:%.*]]
    +; CHECK:       inner_loop:
    +; CHECK-NEXT:    call void @side_effect() #[[ATTR0]]
    +; CHECK-NEXT:    br i1 [[ARG:%.*]], label [[INNER_LOOP]], label [[INNER_EXIT:%.*]]
    +; CHECK:       inner_exit:
    +; CHECK-NEXT:    br i1 [[MID_BACK:%.*]], label [[MID_LOOP]], label [[MID_EXIT:%.*]]
    +; CHECK:       mid_exit:
    +; CHECK-NEXT:    [[X_PRE:%.*]] = load i32, ptr [[P]], align 4
    +; CHECK-NEXT:    br label [[BACKEDGE]]
    +; CHECK:       backedge:
    +; CHECK-NEXT:    [[X2]] = phi i32 [ [[X_PRE]], [[MID_EXIT]] ], [ [[X]], [[HOT_PATH]] ]
    +; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
    +; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
    +; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    +; CHECK:       exit:
    +; CHECK-NEXT:    ret i32 [[X]]
    +;
    +entry:
    +  br label %loop
    +
    +loop:
    +  %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
    +  %x = load i32, ptr %p
    +  %cond = icmp ne i32 %x, 0
    +  br i1 %cond, label %hot_path, label %cold_path
    +
    +hot_path:
    +  br label %backedge
    +
    +cold_path:
    +  br label %mid_loop
    +
    +mid_loop:
    +  br label %inner_loop
    +
    +inner_loop:
    +  call void @side_effect() nofree
    +  br i1 %inner.back, label %inner_loop, label %inner_exit
    +
    +inner_exit:
    +  br i1 %mid.back, label %mid_loop, label %mid_exit
    +
    +mid_exit:
    +  br label %backedge
    +
    +backedge:
    +  %iv.next = add i32 %iv, %x
    +  %loop.cond = icmp ult i32 %iv.next, 1000
    +  br i1 %loop.cond, label %loop, label %exit
    +
    +exit:
    +  ret i32 %x
    +}
    +
    +; Do not PRE when the inner loop exits into a sibling loop: placing the reload
    +; in the sibling header could execute it more often than the outer header.
    +define i32 @test_blocker_exits_to_sibling_loop_neg(ptr %p, i1 %inner.back, i1 %sibling.back) {
    +; CHECK-LABEL: @test_blocker_exits_to_sibling_loop_neg(
    +; CHECK-NEXT:  entry:
    +; CHECK-NEXT:    br label [[LOOP:%.*]]
    +; CHECK:       loop:
    +; CHECK-NEXT:    [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[BACKEDGE:%.*]] ]
    +; CHECK-NEXT:    [[X:%.*]] = load i32, ptr [[P:%.*]], align 4
    +; CHECK-NEXT:    [[COND:%.*]] = icmp ne i32 [[X]], 0
    +; CHECK-NEXT:    br i1 [[COND]], label [[HOT_PATH:%.*]], label [[INNER_LOOP:%.*]]
    +; CHECK:       hot.path:
    +; CHECK-NEXT:    br label [[BACKEDGE]]
    +; CHECK:       inner.loop:
    +; CHECK-NEXT:    call void @side_effect() #[[ATTR0]]
    +; CHECK-NEXT:    br i1 [[INNER_BACK:%.*]], label [[INNER_LOOP]], label [[SIBLING_LOOP:%.*]]
    +; CHECK:       sibling.loop:
    +; CHECK-NEXT:    br i1 [[SIBLING_BACK:%.*]], label [[SIBLING_LOOP]], label [[BACKEDGE]]
    +; CHECK:       backedge:
    +; CHECK-NEXT:    [[IV_NEXT]] = add i32 [[IV]], [[X]]
    +; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp ult i32 [[IV_NEXT]], 1000
    +; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
    +; CHECK:       exit:
    +; CHECK-NEXT:    ret i32 [[X]]
    +;
    +entry:
    +  br label %loop
    +
    +loop:
    +  %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
    +  %x = load i32, ptr %p
    +  %cond = icmp ne i32 %x, 0
    +  br i1 %cond, label %hot.path, label %inner.loop
    +
    +hot.path:
    +  br label %backedge
    +
    +inner.loop:
    +  call...
    [truncated]
    
    
    ``````

    </details>

    nikic nikic requested changes on Oct 01, 2026 07:30 UTC
    Changes Requested Reviewer

    This is mixing multiple changes. The use of willNotFreeBetween() can be landed separately, and raising the limit on the walk needs to be evaluated independently -- this looks like an inappropriately high limit for an ad-hoc query.

    anupkum-sgs anupkum-sgs commented on Oct 01, 2026 07:53 UTC
    Author

    This is mixing multiple changes. The use of willNotFreeBetween() can be landed separately, and raising the limit on the walk needs to be evaluated independently -- this looks like an inappropriately high limit for an ad-hoc query.

    In most of the cases I found that the inner-loop blocker is due to an
    aliasing issue, and in such an aliasing scenario canBeFreed returns true
    (which I mentioned in my RFC), so even if we allow the inner-loop blocker
    without willNotFreeBetween it would be meaningless.

    So I think we can do the following:
    1) first PR for the use of willNotFreeBetween
    2) then to allow the inner-loop blocker

    Shall I proceed with this?

    nikic nikic commented on Oct 01, 2026 08:08 UTC
    Contributor

    Yes, doing the willNotFreeBetween() change first sounds reasonable.

    Reviewers
    Assignees
    anupkum-sgs anupkum-sgs
    Labels
    risk: low confidence: 88.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.7% (MEDIUM) · Higher = larger blast radius
    54.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.5% · Lower = rushed or shallow review
    88.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.3% · Safe to merge
    6.3%