From 99cebadb5c971b5946a727dfb09ba43b6d7fe542 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Tue, 31 Dec 2013 19:30:47 +0000 Subject: [PATCH] Fold vector selects with undef elements in the condition. Fixes PR18319. Patch by Ilia Filippov! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198267 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/ConstantFold.cpp | 21 +++++++++++++++------ test/Assembler/ConstantExprFoldSelect.ll | 8 ++++++++ test/Bitcode/select.ll | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 test/Assembler/ConstantExprFoldSelect.ll diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index f5e225cffc1..e3f8954ad89 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -705,12 +705,21 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, SmallVector Result; Type *Ty = IntegerType::get(CondV->getContext(), 32); for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){ - ConstantInt *Cond = dyn_cast(CondV->getOperand(i)); - if (Cond == 0) break; - - Constant *V = Cond->isNullValue() ? V2 : V1; - Constant *Res = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); - Result.push_back(Res); + Constant *V; + Constant *V1Element = ConstantExpr::getExtractElement(V1, + ConstantInt::get(Ty, i)); + Constant *V2Element = ConstantExpr::getExtractElement(V2, + ConstantInt::get(Ty, i)); + Constant *Cond = dyn_cast(CondV->getOperand(i)); + if (V1Element == V2Element) { + V = V1Element; + } else if (isa(Cond)) { + V = isa(V1Element) ? V1Element : V2Element; + } else { + if (!isa(Cond)) break; + V = Cond->isNullValue() ? V2Element : V1Element; + } + Result.push_back(V); } // If we were able to build the vector, return it. diff --git a/test/Assembler/ConstantExprFoldSelect.ll b/test/Assembler/ConstantExprFoldSelect.ll new file mode 100644 index 00000000000..b000e02653c --- /dev/null +++ b/test/Assembler/ConstantExprFoldSelect.ll @@ -0,0 +1,8 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s +; PR18319 + +define void @function() { + %c = trunc <4 x i16> select (<4 x i1> , <4 x i16> , <4 x i16> ) to <4 x i8> +; CHECK: + ret void +} diff --git a/test/Bitcode/select.ll b/test/Bitcode/select.ll index 71e669a90cd..08a3061394d 100644 --- a/test/Bitcode/select.ll +++ b/test/Bitcode/select.ll @@ -5,5 +5,5 @@ define <2 x i32> @main() { } ; CHECK: define <2 x i32> @main() { -; CHECK: ret <2 x i32> select (<2 x i1> , <2 x i32> zeroinitializer, <2 x i32> ) +; CHECK: ret <2 x i32> ; CHECK: }