From 7d56483207b8102855bd0c3cd8f6e5412165736c Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 9 Sep 2021 21:55:28 +0200 Subject: [PATCH] [IR] Handle constant expressions in containsUndefinedElement() If the constant is a constant expression, then getAggregateElement() will return null. Guard against this before calling HasFn(). (cherry picked from commit af382b93831ae6a58bce8bc075458cfd056e3976) --- lib/IR/Constants.cpp | 8 +++++--- test/Transforms/InstSimplify/ConstProp/vecreduce.ll | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index 6c75085a667..1e72cb4d3a6 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -315,9 +315,11 @@ containsUndefinedElement(const Constant *C, return false; for (unsigned i = 0, e = cast(VTy)->getNumElements(); - i != e; ++i) - if (HasFn(C->getAggregateElement(i))) - return true; + i != e; ++i) { + if (Constant *Elem = C->getAggregateElement(i)) + if (HasFn(Elem)) + return true; + } } return false; diff --git a/test/Transforms/InstSimplify/ConstProp/vecreduce.ll b/test/Transforms/InstSimplify/ConstProp/vecreduce.ll index e27180b1a89..b407c908cf2 100644 --- a/test/Transforms/InstSimplify/ConstProp/vecreduce.ll +++ b/test/Transforms/InstSimplify/ConstProp/vecreduce.ll @@ -87,6 +87,15 @@ define i32 @add_poison1() { ret i32 %x } +define i32 @add_constexpr() { +; CHECK-LABEL: @add_constexpr( +; CHECK-NEXT: [[X:%.*]] = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> bitcast (<4 x i64> to <8 x i32>)) +; CHECK-NEXT: ret i32 [[X]] +; + %x = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> bitcast (<4 x i64> to <8 x i32>)) + ret i32 %x +} + define i32 @mul_0() { ; CHECK-LABEL: @mul_0( ; CHECK-NEXT: ret i32 0