[llvm] 714c4a5 - [InstCombine] Ignore frees when inferring inbounds (#202304)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 05:36:11 PDT 2026
Author: Nikita Popov
Date: 2026-06-08T14:36:06+02:00
New Revision: 714c4a5f8007e5ee52a6338f325680fb7ab89a7a
URL: https://github.com/llvm/llvm-project/commit/714c4a5f8007e5ee52a6338f325680fb7ab89a7a
DIFF: https://github.com/llvm/llvm-project/commit/714c4a5f8007e5ee52a6338f325680fb7ab89a7a.diff
LOG: [InstCombine] Ignore frees when inferring inbounds (#202304)
>From LangRef:
> Note that the object does not have to be live anymore; being
> in-bounds of a deallocated object is sufficient.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/inbounds-gep.ll
Removed:
################################################################################
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