[llvm] [InstCombine] Replace getAllocatedType() with getAllocationSize() (PR #177435)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 11:01:04 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Jameson Nash (vtjnash)

<details>
<summary>Changes</summary>

Replace uses of getAllocatedType() with the more semantic
getAllocationSize() method in the alloca dereferenceability check
and zero-size alloca merging logic.

This simplifies the code by:
- Eliminating manual isArrayAllocation() checks (handled by getAllocationSize)
- Eliminating superfluous isSized() checks (the verifier rejects them already)
- Using TypeSize::isScalable() for scalable vector handling (before casting to uint64_t)
- Using TypeSize::isZero() for zero-size checks

---
Full diff: https://github.com/llvm/llvm-project/pull/177435.diff


2 Files Affected:

- (modified) llvm/lib/IR/Instructions.cpp (+3) 
- (modified) llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (+35-38) 


``````````diff
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index be307cf08ccc5..52b6c35e18d95 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -64,6 +64,9 @@ static cl::opt<bool> DisableI2pP2iOpt(
 std::optional<TypeSize>
 AllocaInst::getAllocationSize(const DataLayout &DL) const {
   TypeSize Size = DL.getTypeAllocSize(getAllocatedType());
+  // Zero-sized types can return early since 0 * N = 0 for any array size N.
+  if (Size.isZero())
+    return Size;
   if (isArrayAllocation()) {
     auto *C = dyn_cast<ConstantInt>(getArraySize());
     if (!C)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index eff8e6f8f5739..73bd84723b8ab 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -176,13 +176,11 @@ isOnlyCopiedFromConstantMemory(AAResults *AA,
 /// Returns true if V is dereferenceable for size of alloca.
 static bool isDereferenceableForAllocaSize(const Value *V, const AllocaInst *AI,
                                            const DataLayout &DL) {
-  if (AI->isArrayAllocation())
-    return false;
-  uint64_t AllocaSize = DL.getTypeStoreSize(AI->getAllocatedType());
-  if (!AllocaSize)
+  std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
+  if (!AllocaSize || AllocaSize->isScalable())
     return false;
   return isDereferenceableAndAlignedPointer(V, AI->getAlign(),
-                                            APInt(64, AllocaSize), DL);
+                                            APInt(64, *AllocaSize), DL);
 }
 
 static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC,
@@ -503,40 +501,39 @@ Instruction *InstCombinerImpl::visitAllocaInst(AllocaInst &AI) {
   if (auto *I = simplifyAllocaArraySize(*this, AI, DT))
     return I;
 
-  if (AI.getAllocatedType()->isSized()) {
-    // Move all alloca's of zero byte objects to the entry block and merge them
-    // together.  Note that we only do this for alloca's, because malloc should
-    // allocate and return a unique pointer, even for a zero byte allocation.
-    if (DL.getTypeAllocSize(AI.getAllocatedType()).getKnownMinValue() == 0) {
-      // For a zero sized alloca there is no point in doing an array allocation.
-      // This is helpful if the array size is a complicated expression not used
-      // elsewhere.
-      if (AI.isArrayAllocation())
-        return replaceOperand(AI, 0,
-            ConstantInt::get(AI.getArraySize()->getType(), 1));
-
-      // Get the first instruction in the entry block.
-      BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
-      BasicBlock::iterator FirstInst = EntryBlock.getFirstNonPHIOrDbg();
-      if (&*FirstInst != &AI) {
-        // If the entry block doesn't start with a zero-size alloca then move
-        // this one to the start of the entry block.  There is no problem with
-        // dominance as the array size was forced to a constant earlier already.
-        AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
-        if (!EntryAI || !EntryAI->getAllocatedType()->isSized() ||
-            DL.getTypeAllocSize(EntryAI->getAllocatedType())
-                    .getKnownMinValue() != 0) {
-          AI.moveBefore(FirstInst);
-          return &AI;
-        }
-
-        // Replace this zero-sized alloca with the one at the start of the entry
-        // block after ensuring that the address will be aligned enough for both
-        // types.
-        const Align MaxAlign = std::max(EntryAI->getAlign(), AI.getAlign());
-        EntryAI->setAlignment(MaxAlign);
-        return replaceInstUsesWith(AI, EntryAI);
+  // Move all alloca's of zero byte objects to the entry block and merge them
+  // together.  Note that we only do this for alloca's, because malloc should
+  // allocate and return a unique pointer, even for a zero byte allocation.
+  std::optional<TypeSize> Size = AI.getAllocationSize(DL);
+  if (Size && Size->isZero()) {
+    // For a zero sized alloca there is no point in doing an array allocation.
+    // This is helpful if the array size is a complicated expression not used
+    // elsewhere.
+    if (AI.isArrayAllocation())
+      return replaceOperand(AI, 0,
+          ConstantInt::get(AI.getArraySize()->getType(), 1));
+
+    // Get the first instruction in the entry block.
+    BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
+    BasicBlock::iterator FirstInst = EntryBlock.getFirstNonPHIOrDbg();
+    if (&*FirstInst != &AI) {
+      // If the entry block doesn't start with a zero-size alloca then move
+      // this one to the start of the entry block.  There is no problem with
+      // dominance as the array size was forced to a constant earlier already.
+      AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
+      std::optional<TypeSize> EntryAISize =
+          EntryAI ? EntryAI->getAllocationSize(DL) : std::nullopt;
+      if (!EntryAISize || !EntryAISize->isZero()) {
+        AI.moveBefore(FirstInst);
+        return &AI;
       }
+
+      // Replace this zero-sized alloca with the one at the start of the entry
+      // block after ensuring that the address will be aligned enough for both
+      // types.
+      const Align MaxAlign = std::max(EntryAI->getAlign(), AI.getAlign());
+      EntryAI->setAlignment(MaxAlign);
+      return replaceInstUsesWith(AI, EntryAI);
     }
   }
 

``````````

</details>


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


More information about the llvm-commits mailing list