[llvm] cf5e888 - [instsimplify] When compare allocas, consider their minimal size

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 17 09:53:33 PST 2022


Author: Philip Reames
Date: 2022-02-17T09:53:24-08:00
New Revision: cf5e88864b286e5b3433cd2d7995fe9465d57804

URL: https://github.com/llvm/llvm-project/commit/cf5e88864b286e5b3433cd2d7995fe9465d57804
DIFF: https://github.com/llvm/llvm-project/commit/cf5e88864b286e5b3433cd2d7995fe9465d57804.diff

LOG: [instsimplify] When compare allocas, consider their minimal size

The code was using exact sizing only, but since what we really need is just to make sure the offsets are in bounds, a minimum bound on the object size is sufficient.

To demonstrate the difference, support computing minimum sizes from obects of scalable vector type.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/lib/Analysis/MemoryBuiltins.cpp
    llvm/test/Transforms/InstSimplify/compare.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 7d5b62d9a804..35e93143f96a 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2631,6 +2631,7 @@ computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
         (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
       uint64_t LHSSize, RHSSize;
       ObjectSizeOpts Opts;
+      Opts.EvalMode = ObjectSizeOpts::Mode::Min;
       Opts.NullIsUnknownSize =
           NullPointerIsDefined(cast<AllocaInst>(LHS)->getFunction());
       if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&

diff  --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index 5dc56654512a..a52b1a8b19cc 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -659,10 +659,10 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
   if (!I.getAllocatedType()->isSized())
     return unknown();
 
-  if (isa<ScalableVectorType>(I.getAllocatedType()))
+  TypeSize ElemSize = DL.getTypeAllocSize(I.getAllocatedType());
+  if (ElemSize.isScalable() && Options.EvalMode != ObjectSizeOpts::Mode::Min)
     return unknown();
-
-  APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
+  APInt Size(IntTyBits, ElemSize.getKnownMinSize());
   if (!I.isArrayAllocation())
     return std::make_pair(align(Size, I.getAlign()), Zero);
 

diff  --git a/llvm/test/Transforms/InstSimplify/compare.ll b/llvm/test/Transforms/InstSimplify/compare.ll
index 7daee2a8a8da..25a6c19d2291 100644
--- a/llvm/test/Transforms/InstSimplify/compare.ll
+++ b/llvm/test/Transforms/InstSimplify/compare.ll
@@ -2727,5 +2727,15 @@ define i1 @zero_sized_alloca2() {
   ret i1 %res
 }
 
+define i1 @scalar_vectors_are_non_empty() {
+; CHECK-LABEL: @scalar_vectors_are_non_empty(
+; CHECK-NEXT:    ret i1 true
+;
+  %a = alloca <vscale x 2 x i32>
+  %b = alloca <vscale x 2 x i32>
+  %res = icmp ne <vscale x 2 x i32>* %a, %b
+  ret i1 %res
+}
+
 
 attributes #0 = { null_pointer_is_valid }


        


More information about the llvm-commits mailing list