From 1edc51b56a0c27db5561d7bb117918f88c28780a Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Mon, 24 Oct 2022 11:35:24 -0700 Subject: [PATCH] [InstCombine] Explicitly check for scalable TypeSize. Instead of assuming it is a fixed size. Reviewed By: peterwaller-arm Differential Revision: https://reviews.llvm.org/D136517 --- .../InstCombine/InstCombineLoadStoreAlloca.cpp | 7 +++++-- .../gep-object-size-less-than-or-equal-typesize.ll | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/InstCombine/gep-object-size-less-than-or-equal-typesize.ll diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index d145832..a1f29cb 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -769,10 +769,13 @@ static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, if (!CS) return false; - uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType()); + TypeSize TS = DL.getTypeAllocSize(AI->getAllocatedType()); + if (TS.isScalable()) + return false; // Make sure that, even if the multiplication below would wrap as an // uint64_t, we still do the right thing. - if ((CS->getValue().zext(128) * APInt(128, TypeSize)).ugt(MaxSize)) + if ((CS->getValue().zext(128) * APInt(128, TS.getFixedSize())) + .ugt(MaxSize)) return false; continue; } diff --git a/llvm/test/Transforms/InstCombine/gep-object-size-less-than-or-equal-typesize.ll b/llvm/test/Transforms/InstCombine/gep-object-size-less-than-or-equal-typesize.ll new file mode 100644 index 0000000..2bcf8f0 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/gep-object-size-less-than-or-equal-typesize.ll @@ -0,0 +1,17 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -instcombine -S %s | FileCheck %s + +define i8 @foo( %x, i64 %idx) { +; CHECK-LABEL: @foo( +; CHECK-NEXT: [[A:%.*]] = alloca , align 1 +; CHECK-NEXT: store [[X:%.*]], ptr [[A]], align 1 +; CHECK-NEXT: [[B:%.*]] = getelementptr i8, ptr [[A]], i64 [[IDX:%.*]] +; CHECK-NEXT: [[C:%.*]] = load i8, ptr [[B]], align 1 +; CHECK-NEXT: ret i8 [[C]] +; + %a = alloca + store %x, ptr %a + %b = getelementptr i8, ptr %a, i64 %idx + %c = load i8, ptr %b + ret i8 %c +} -- 2.7.4