From aa8be5aeead7ad894270aa025e7165169c1a54d2 Mon Sep 17 00:00:00 2001 From: Bjorn Pettersson Date: Mon, 14 Sep 2020 22:53:54 +0200 Subject: [PATCH] [Scalarizer] Avoid changing name of non-instructions The "takeName" logic in ScalarizerVisitor::gather did not consider that the value vector could refer to non-instructions, such as global variables. This patch make sure that we avoid changing the name of a value if it isn't an instruction. Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D87685 --- llvm/lib/Transforms/Scalar/Scalarizer.cpp | 3 ++- llvm/test/Transforms/Scalarizer/global-bug-2.ll | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Transforms/Scalarizer/global-bug-2.ll diff --git a/llvm/lib/Transforms/Scalar/Scalarizer.cpp b/llvm/lib/Transforms/Scalar/Scalarizer.cpp index 3bc0cbd..c7fe21f 100644 --- a/llvm/lib/Transforms/Scalar/Scalarizer.cpp +++ b/llvm/lib/Transforms/Scalar/Scalarizer.cpp @@ -398,7 +398,8 @@ void ScalarizerVisitor::gather(Instruction *Op, const ValueVector &CV) { continue; Instruction *Old = cast(V); - CV[I]->takeName(Old); + if (isa(CV[I])) + CV[I]->takeName(Old); Old->replaceAllUsesWith(CV[I]); PotentiallyDeadInstrs.emplace_back(Old); } diff --git a/llvm/test/Transforms/Scalarizer/global-bug-2.ll b/llvm/test/Transforms/Scalarizer/global-bug-2.ll new file mode 100644 index 0000000..60f61ab --- /dev/null +++ b/llvm/test/Transforms/Scalarizer/global-bug-2.ll @@ -0,0 +1,20 @@ +; RUN: opt < %s -scalarizer -S -o - | FileCheck %s +; RUN: opt < %s -passes='function(scalarizer)' -S | FileCheck %s + +; The scalarizer used to change the name of the global variable +; Check that the we don't do that any longer. +; +; CHECK: @c.a = global i16 0, align 1 + +@c.a = global i16 0, align 1 + +define void @c() { +entry: + br label %for.cond1 + +for.cond1: ; preds = %for.cond1, %entry + %d.sroa.0.0 = phi <4 x i16*> [ , %entry ], [ %d.sroa.0.1.vec.insert, %for.cond1 ] + %d.sroa.0.0.vec.extract = extractelement <4 x i16*> %d.sroa.0.0, i32 0 + %d.sroa.0.1.vec.insert = shufflevector <4 x i16*> , <4 x i16*> %d.sroa.0.0, <4 x i32> + br label %for.cond1 +} -- 2.7.4