From 93849fdc0d40ea8566f0c847069c11993bcd687e Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Tue, 30 Apr 2019 23:09:26 +0000 Subject: [PATCH] [InstCombine] Limit a vector demanded elts rule which was producing invalid IR. The demanded elts rules introduced for GEPs in https://reviews.llvm.org/rL356293 replaced vector constants with undefs (by design). It turns out that the LangRef disallows such cases when indexing structs. The right fix is probably to relax the langref requirement, and update other passes to expect the result, but for the moment, limit the transform to avoid compiler crashes. This should fix https://bugs.llvm.org/show_bug.cgi?id=41624. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359633 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineSimplifyDemanded.cpp | 12 ++++++++++++ test/Transforms/InstCombine/vec_demanded_elts.ll | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index 1d52938d1f8..42406e27e35 100644 --- a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -1169,6 +1169,18 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, default: break; case Instruction::GetElementPtr: { + // The LangRef requires that struct geps have all constant indices. As + // such, we can't convert any operand to partial undef. + auto mayIndexStructType = [](GetElementPtrInst &GEP) { + for (auto I = gep_type_begin(GEP), E = gep_type_end(GEP); + I != E; I++) + if (I.isStruct()) + return true;; + return false; + }; + if (mayIndexStructType(cast(*I))) + break; + // Conservatively track the demanded elements back through any vector // operands we may have. We know there must be at least one, or we // wouldn't have a vector result to get here. Note that we intentionally diff --git a/test/Transforms/InstCombine/vec_demanded_elts.ll b/test/Transforms/InstCombine/vec_demanded_elts.ll index a56152cca59..3f172be3d83 100644 --- a/test/Transforms/InstCombine/vec_demanded_elts.ll +++ b/test/Transforms/InstCombine/vec_demanded_elts.ll @@ -638,3 +638,16 @@ define i32* @gep_demanded_lane_undef(i32* %base, i64 %idx) { %ee = extractelement <2 x i32*> %gep, i32 1 ret i32* %ee } + + +;; LangRef has an odd quirk around FCAs which make it illegal to use undef +;; indices. +define i32* @PR41624(<2 x { i32, i32 }*> %a) { +; CHECK-LABEL: @PR41624( +; CHECK-NEXT: %w = getelementptr { i32, i32 }, <2 x { i32, i32 }*> %a, <2 x i64> , <2 x i32> zeroinitializer +; CHECK-NEXT: %r = extractelement <2 x i32*> %w, i32 0 +; CHECK-NEXT: ret i32* %r + %w = getelementptr { i32, i32 }, <2 x { i32, i32 }*> %a, <2 x i64> , <2 x i32> zeroinitializer + %r = extractelement <2 x i32*> %w, i32 0 + ret i32* %r +} -- 2.50.1