diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2013-12-31 19:30:47 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2013-12-31 19:30:47 +0000 |
commit | 2d4ba2ebbafe401fa406e80b09546655fb7eab3e (patch) | |
tree | f7fd361060b891271e22a72849a09b83c80477ce /llvm | |
parent | 0518453e2e35a7687da07b7ebb4aa5a402183811 (diff) | |
download | bcm5719-llvm-2d4ba2ebbafe401fa406e80b09546655fb7eab3e.tar.gz bcm5719-llvm-2d4ba2ebbafe401fa406e80b09546655fb7eab3e.zip |
Fold vector selects with undef elements in the condition. Fixes PR18319.
Patch by Ilia Filippov!
llvm-svn: 198267
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 21 | ||||
-rw-r--r-- | llvm/test/Assembler/ConstantExprFoldSelect.ll | 8 | ||||
-rw-r--r-- | llvm/test/Bitcode/select.ll | 2 |
3 files changed, 24 insertions, 7 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index f5e225cffc1..e3f8954ad89 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -705,12 +705,21 @@ Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, SmallVector<Constant*, 16> Result; Type *Ty = IntegerType::get(CondV->getContext(), 32); for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){ - ConstantInt *Cond = dyn_cast<ConstantInt>(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<Constant>(CondV->getOperand(i)); + if (V1Element == V2Element) { + V = V1Element; + } else if (isa<UndefValue>(Cond)) { + V = isa<UndefValue>(V1Element) ? V1Element : V2Element; + } else { + if (!isa<ConstantInt>(Cond)) break; + V = Cond->isNullValue() ? V2Element : V1Element; + } + Result.push_back(V); } // If we were able to build the vector, return it. diff --git a/llvm/test/Assembler/ConstantExprFoldSelect.ll b/llvm/test/Assembler/ConstantExprFoldSelect.ll new file mode 100644 index 00000000000..b000e02653c --- /dev/null +++ b/llvm/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> <i1 undef, i1 undef, i1 false, i1 true>, <4 x i16> <i16 undef, i16 2, i16 3, i16 4>, <4 x i16> <i16 -1, i16 -2, i16 -3, i16 -4>) to <4 x i8> +; CHECK: <i16 undef, i16 -2, i16 -3, i16 4> + ret void +} diff --git a/llvm/test/Bitcode/select.ll b/llvm/test/Bitcode/select.ll index 71e669a90cd..08a3061394d 100644 --- a/llvm/test/Bitcode/select.ll +++ b/llvm/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> <i1 false, i1 undef>, <2 x i32> zeroinitializer, <2 x i32> <i32 0, i32 undef>) +; CHECK: ret <2 x i32> <i32 0, i32 undef> ; CHECK: } |