[llvm] [InstCombine] Ignore frees when inferring inbounds (PR #202304)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 02:56:42 PDT 2026
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/202304
>From LangRef:
> Note that the object does not have to be live anymore; being
> in-bounds of a deallocated object is sufficient.
>From 10e428527808ea3fb2c4348d9a0b6000256529f4 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 8 Jun 2026 11:54:40 +0200
Subject: [PATCH] [InstCombine] Ignore frees when inferring inbounds
>From LangRef:
> Note that the object does not have to be live anymore; being
> in-bounds of a deallocated object is sufficient.
---
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 4 +++-
llvm/test/Transforms/InstCombine/inbounds-gep.ll | 1 +
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 89bf4bf713421..608966a5e9289 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3615,7 +3615,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
bool CanBeNull, CanBeFreed;
uint64_t DerefBytes = UnderlyingPtrOp->getPointerDereferenceableBytes(
DL, CanBeNull, CanBeFreed);
- if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
+ // We can ignore CanBeFreed here, because inbounds is explicitly allowed to
+ // refer to a deallocated object.
+ if (!CanBeNull && DerefBytes != 0) {
if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
BasePtrOffset.isNonNegative()) {
APInt AllocSize(IdxWidth, DerefBytes);
diff --git a/llvm/test/Transforms/InstCombine/inbounds-gep.ll b/llvm/test/Transforms/InstCombine/inbounds-gep.ll
index 467e5a82f89cc..0ea71da485766 100644
--- a/llvm/test/Transforms/InstCombine/inbounds-gep.ll
+++ b/llvm/test/Transforms/InstCombine/inbounds-gep.ll
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+; RUN: opt -passes=instcombine -use-dereferenceable-at-point-semantics -S < %s | FileCheck %s
declare ptr @g()
declare ptr @use(ptr)
More information about the llvm-commits
mailing list