diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -840,8 +840,13 @@ return false; SmallVector Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx); - Type *AllocTy = - GetElementPtrInst::getIndexedType(GEPI->getSourceElementType(), Ops); + Type *SourceElementType = GEPI->getSourceElementType(); + // Size information about scalable vectors is not available, so we cannot + // deduce whether indexing at n is undefined behaviour or not. Bail out. + if (isa(SourceElementType)) + return false; + + Type *AllocTy = GetElementPtrInst::getIndexedType(SourceElementType, Ops); if (!AllocTy || !AllocTy->isSized()) return false; const DataLayout &DL = IC.getDataLayout(); diff --git a/llvm/test/Transforms/InstCombine/gep-can-replace-gep-idx-with-zero-typesize.ll b/llvm/test/Transforms/InstCombine/gep-can-replace-gep-idx-with-zero-typesize.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/gep-can-replace-gep-idx-with-zero-typesize.ll @@ -0,0 +1,25 @@ +; RUN: opt -S -O2 -mtriple=aarch64-linux-gnu -mattr=+sve < %s 2>%t +; RUN: FileCheck --check-prefix=WARN --allow-empty %s <%t + +; This regression test is verifying that the optimization defined by +; canReplaceGEPIdxWithZero, which replaces a GEP index with zero iff we can show +; a value other than zero would cause undefined behaviour, does not throw a +; 'assumption that TypeSize is not scalable' warning when the source element type +; is a scalable vector. + +; If the source element is a scalable vector type, then we cannot deduce whether +; or not indexing at a given index is undefined behaviour, because the size of +; the vector is not known. + +; If this check fails please read test/CodeGen/AArch64/README for instructions +; on how to resolve it. +; WARN-NOT: warning: {{.*}}TypeSize is not scalable + +declare void @do_something( %x) + +define void @can_replace_gep_idx_with_zero_typesize(i64 %n, * %a, i64 %b) { + %idx = getelementptr , * %a, i64 %b + %tmp = load , * %idx + call void @do_something( %tmp) + ret void +}