From 122b0640fc97202bacb630744dfc6da58f11af42 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Thu, 9 Jul 2020 11:01:11 -0700 Subject: [PATCH] [InstSimplify] Don't fold vectors of partial undef in SimplifySelectInst if the non-undef element value might produce poison We can't fold to the non-undef value unless we know it isn't poison. So check each element with isGuaranteedNotToBeUndefOrPoison. This currently rules out all constant expressions. Differential Revision: https://reviews.llvm.org/D83442 --- llvm/lib/Analysis/InstructionSimplify.cpp | 6 ++++-- llvm/test/Transforms/InstSimplify/select.ll | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 8cd5d20..277e290 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4135,9 +4135,11 @@ static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, // one element is undef, choose the defined element as the safe result. if (TEltC == FEltC) NewC.push_back(TEltC); - else if (isa(TEltC)) + else if (isa(TEltC) && + isGuaranteedNotToBeUndefOrPoison(FEltC)) NewC.push_back(FEltC); - else if (isa(FEltC)) + else if (isa(FEltC) && + isGuaranteedNotToBeUndefOrPoison(TEltC)) NewC.push_back(TEltC); else break; diff --git a/llvm/test/Transforms/InstSimplify/select.ll b/llvm/test/Transforms/InstSimplify/select.ll index 0f43c8f6..8b69bad 100644 --- a/llvm/test/Transforms/InstSimplify/select.ll +++ b/llvm/test/Transforms/InstSimplify/select.ll @@ -848,3 +848,17 @@ define i32 @false_undef_false_freeze(i1 %cond, i32 %x) { %s = select i1 %cond, i32 undef, i32 %xf ret i32 %s } + +@g = external global i32, align 1 + +; Make sure we don't fold partial undef vectors when constexprs are involved. +; We would need to prove the constexpr doesn't result in poison which we aren't +; equiped to do yet. +define <2 x i32> @false_undef_true_constextpr_vec(i1 %cond) { +; CHECK-LABEL: @false_undef_true_constextpr_vec( +; CHECK-NEXT: [[S:%.*]] = select i1 [[COND:%.*]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[S]] +; + %s = select i1 %cond, <2 x i32> , <2 x i32> + ret <2 x i32> %s +} -- 2.7.4