[llvm] [InstCombine] Sink instructions across assumes (PR #205314)

Nikolas Klauser via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 23 03:37:59 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/205314

This causes InstCombine to not reach a fixpoint when assumes can be dropped due to the load. Sinking the instructions causes the assume to not be dropped anymore, but I don't think that's a big problem.


>From 8455deddcf5295b63ce7fb3f4a4ec5987f9b21c6 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 23 Jun 2026 12:36:45 +0200
Subject: [PATCH] [InstCombine] Sink instructions across assumes

---
 .../InstCombine/InstructionCombining.cpp       |  2 +-
 llvm/test/Transforms/InstCombine/assume.ll     | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 1e24ff8d51057..749d76ca29fbb 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -5629,7 +5629,7 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
     for (BasicBlock::iterator Scan = std::next(I->getIterator()),
                               E = I->getParent()->end();
          Scan != E; ++Scan)
-      if (Scan->mayWriteToMemory())
+      if (Scan->mayWriteToMemory() && !isa<AssumeInst>(Scan))
         return false;
   }
 
diff --git a/llvm/test/Transforms/InstCombine/assume.ll b/llvm/test/Transforms/InstCombine/assume.ll
index 1902fcbb95afd..df46e048634f3 100644
--- a/llvm/test/Transforms/InstCombine/assume.ll
+++ b/llvm/test/Transforms/InstCombine/assume.ll
@@ -851,6 +851,24 @@ define void @nonnull_gep_not_inbounds(ptr %p, i64 %i) {
   ret void
 }
 
+define void @nonnull_move_fixpoint(ptr %ptr) {
+; CHECK-LABEL: @nonnull_move_fixpoint(
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "nonnull"(ptr [[PTR:%.*]]) ]
+; CHECK-NEXT:    br label [[BB:%.*]]
+; CHECK:       bb:
+; CHECK-NEXT:    [[PTR2:%.*]] = load ptr, ptr [[PTR]], align 8
+; CHECK-NEXT:    store i32 0, ptr [[PTR2]], align 4
+; CHECK-NEXT:    ret void
+;
+  %ptr2 = load ptr, ptr %ptr, align 8
+  call void @llvm.assume(i1 true) [ "nonnull"(ptr %ptr) ]
+  br label %bb
+
+bb:
+  store i32 0, ptr %ptr2, align 4
+  ret void
+}
+
 define void @always_true_assumption() {
 ; CHECK-LABEL: @always_true_assumption(
 ; CHECK-NEXT:    ret void



More information about the llvm-commits mailing list