[llvm] [InstCombine] Ignore frees when inferring inbounds (PR #202304)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 8 02:57:21 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Nikita Popov (nikic)
<details>
<summary>Changes</summary>
>From LangRef:
> Note that the object does not have to be live anymore; being
> in-bounds of a deallocated object is sufficient.
---
Full diff: https://github.com/llvm/llvm-project/pull/202304.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+3-1)
- (modified) llvm/test/Transforms/InstCombine/inbounds-gep.ll (+1)
``````````diff
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)
``````````
</details>
https://github.com/llvm/llvm-project/pull/202304
More information about the llvm-commits
mailing list