[llvm] 6c27fc9 - [InstCombine] Fix InstCombine pass to sink llvm.assume calls (#229658)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 08:15:56 PDT 2026
Author: anilkund
Date: 2026-10-11T15:15:50Z
New Revision: 6c27fc921b90808f0c9180209e44b7ba7a8cd4b1
URL: https://github.com/llvm/llvm-project/commit/6c27fc921b90808f0c9180209e44b7ba7a8cd4b1
DIFF: https://github.com/llvm/llvm-project/commit/6c27fc921b90808f0c9180209e44b7ba7a8cd4b1.diff
LOG: [InstCombine] Fix InstCombine pass to sink llvm.assume calls (#229658)
When trying to sink a GEP instruction into the loop preheader the
InstCombine pass ignores and deletes an llvm.assume call that sets the
alignment for the pointer of the GEP instruction which later passes
depend on. This patch sinks such llvm.assume calls while sinking a
dependent instruction.
Fixes https://github.com/llvm/llvm-project/issues/226698.
Added:
llvm/test/Transforms/InstCombine/sink-align-assume.ll
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 8d299bcbc4369..ea5c3357cf95f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -5583,21 +5583,30 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
return false;
}
+ SmallVector<AssumeInst *, 2> AssumesToSink;
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) {
+ if (auto *Assume = dyn_cast<AssumeInst>(User);
+ Assume && Assume->getParent() == SrcBlock) {
+ AssumesToSink.push_back(Assume);
+ 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)
+ Assume->moveAfter(I);
+
// Also sink all related debug uses from the source basic block. Otherwise we
// get debug use before the def. Attempt to salvage debug uses first, to
// maximise the range variables have location for. If we cannot salvage, then
diff --git a/llvm/test/Transforms/InstCombine/sink-align-assume.ll b/llvm/test/Transforms/InstCombine/sink-align-assume.ll
new file mode 100644
index 0000000000000..5622e62522ee9
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/sink-align-assume.ll
@@ -0,0 +1,110 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -p instcombine -S %s | FileCheck %s
+;
+; Verify that an "align" assume on a value is sunk to the destination block
+; together with the instruction it references, rather than being dropped.
+
+; Simple case: GEP + align assume sunk from entry to a unique-successor block.
+define void @test_sink_align_assume_simple(ptr %p, i32 %offset, i1 %cond) {
+; CHECK-LABEL: define void @test_sink_align_assume_simple(
+; CHECK-SAME: ptr [[P:%.*]], i32 [[OFFSET:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br i1 [[COND]], label %[[THEN:.*]], label %[[EXIT:.*]]
+; CHECK: [[THEN]]:
+; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[OFFSET]] to i64
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i8], ptr [[P]], i64 [[TMP0]]
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i32 8) ]
+; CHECK-NEXT: store float 1.000000e+00, ptr [[GEP]], align 8
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ %gep = getelementptr inbounds [4 x i8], ptr %p, i32 %offset
+ call void @llvm.assume(i1 true) [ "align"(ptr %gep, i32 8) ]
+ br i1 %cond, label %then, label %exit
+
+then:
+ store float 1.0, ptr %gep, align 8
+ br label %exit
+
+exit:
+ ret void
+}
+
+; Assumes in the source block sink with the GEP, but an assume in a sibling
+; block must be dropped because the sunk GEP no longer dominates that block.
+define void @test_sink_some_align_assumes(ptr %p, i32 %offset, i1 %cond) {
+; CHECK-LABEL: define void @test_sink_some_align_assumes(
+; CHECK-SAME: ptr [[P:%.*]], i32 [[OFFSET:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br i1 [[COND]], label %[[THEN:.*]], label %[[ELSE:.*]]
+; CHECK: [[THEN]]:
+; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[OFFSET]] to i64
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i8], ptr [[P]], i64 [[TMP0]]
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i32 8) ]
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i32 16) ]
+; CHECK-NEXT: store i8 0, ptr [[GEP]], align 8
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[ELSE]]:
+; CHECK-NEXT: br label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ %gep = getelementptr inbounds [4 x i8], ptr %p, i32 %offset
+ call void @llvm.assume(i1 true) [ "align"(ptr %gep, i32 8) ]
+ call void @llvm.assume(i1 true) [ "align"(ptr %gep, i32 16) ]
+ br i1 %cond, label %then, label %else
+
+then:
+ store i8 0, ptr %gep, align 8
+ br label %exit
+
+else:
+ call void @llvm.assume(i1 true) [ "align"(ptr %gep, i32 32) ]
+ br label %exit
+
+exit:
+ ret void
+}
+
+; The GEP is used only as the loop PHI's incoming value, so it and its align
+; assume must sink to the loop preheader.
+define void @test_sink_align_assume_loop(ptr %p, i64 %offset, i1 %cond) {
+; CHECK-LABEL: define void @test_sink_align_assume_loop(
+; CHECK-SAME: ptr [[P:%.*]], i64 [[OFFSET:%.*]], i1 [[COND:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[LOOP_PREHEADER:.*]]
+; CHECK: [[LOOP_PREHEADER]]:
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i8], ptr [[P]], i64 [[OFFSET]]
+; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i32 8) ]
+; CHECK-NEXT: br label %[[LOOP:.*]]
+; CHECK: [[LOOP]]:
+; CHECK-NEXT: [[PTR:%.*]] = phi ptr [ [[GEP]], %[[LOOP_PREHEADER]] ], [ [[NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT: store i8 0, ptr [[PTR]], align 8
+; CHECK-NEXT: [[NEXT]] = getelementptr i8, ptr [[PTR]], i64 1
+; CHECK-NEXT: br i1 [[COND]], label %[[LOOP]], label %[[EXIT]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ %gep = getelementptr inbounds [4 x i8], ptr %p, i64 %offset
+ call void @llvm.assume(i1 true) [ "align"(ptr %gep, i32 8) ]
+ br label %loop.preheader
+
+loop.preheader:
+ br label %loop
+
+loop:
+ %ptr = phi ptr [ %gep, %loop.preheader ], [ %next, %loop ]
+ store i8 0, ptr %ptr, align 8
+ %next = getelementptr i8, ptr %ptr, i64 1
+ br i1 %cond, label %loop, label %exit
+
+exit:
+ ret void
+}
+
+
+declare void @llvm.assume(i1 noundef)
More information about the llvm-commits
mailing list