[llvm] [InstCombine] Fix InstCombine pass to sink llvm.assume calls (PR #229658)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 03:49:00 PDT 2026


================
@@ -5586,21 +5586,37 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
         return false;
   }
 
+  // Preserve llvm.assume users in the source block when sinking their operand.
+  // The other operands dominate SrcBlock and therefore also DestBlock.
+  SmallPtrSet<AssumeInst *, 2> AssumesToSink;
+  for (User *U : I->users()) {
+    auto *Assume = dyn_cast<AssumeInst>(U);
+    if (!Assume || Assume->getParent() != SrcBlock)
+      continue;
+    AssumesToSink.insert(Assume);
+  }
+
   I->dropDroppableUses([&](const Use *U) {
-    auto *I = dyn_cast<Instruction>(U->getUser());
-    if (I && I->getParent() != DestBlock) {
-      Worklist.add(I);
+    auto *User = dyn_cast<Instruction>(U->getUser());
+    if (User && User->getParent() != DestBlock) {
+      // Don't drop assumes that we are going to sink to DestBlock.
+      if (auto *A = dyn_cast<AssumeInst>(User); A && AssumesToSink.count(A))
+        return false;
+      Worklist.add(User);
       return true;
     }
     return false;
   });
-  /// FIXME: We could remove droppable uses that are not dominated by
-  /// the new position.
 
   BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt();
   I->moveBefore(*DestBlock, InsertPos);
   ++NumSunkInst;
 
+  // Sink the collected assumes right after I in DestBlock so that the
+  // alignment/attribute guarantees they encode remain visible to later passes.
+  for (AssumeInst *Assume : AssumesToSink)
----------------
nikic wrote:

This will introduce non-determinism, you can't iterate over SmallPtrSet. You need SmallSetVector for that.

https://github.com/llvm/llvm-project/pull/229658


More information about the llvm-commits mailing list