From cf5e88864b286e5b3433cd2d7995fe9465d57804 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Thu, 17 Feb 2022 09:50:32 -0800 Subject: [PATCH] [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. --- llvm/lib/Analysis/InstructionSimplify.cpp | 1 + llvm/lib/Analysis/MemoryBuiltins.cpp | 6 +++--- llvm/test/Transforms/InstSimplify/compare.ll | 10 ++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 7d5b62d..35e9314 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(RHS) || isa(RHS))) { uint64_t LHSSize, RHSSize; ObjectSizeOpts Opts; + Opts.EvalMode = ObjectSizeOpts::Mode::Min; Opts.NullIsUnknownSize = NullPointerIsDefined(cast(LHS)->getFunction()); if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp index 5dc5665..a52b1a8 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(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 7daee2a..25a6c19 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 + %b = alloca + %res = icmp ne * %a, %b + ret i1 %res +} + attributes #0 = { null_pointer_is_valid } -- 2.7.4