diff options
author | Simon Pilgrim <llvm-dev@redking.me.uk> | 2020-10-13 14:35:02 +0100 |
---|---|---|
committer | Simon Pilgrim <llvm-dev@redking.me.uk> | 2020-10-13 14:35:18 +0100 |
commit | 9c3138bd6d8b3e303f0f711753506b330ffa8df0 (patch) | |
tree | 6a5076189712eb2beb51cd794b0178dfc6d3bb74 /llvm/lib/IR/Constants.cpp | |
parent | 2e604d23b42e2b59b8884c7b4c2f27b62cba5fe3 (diff) | |
download | llvm-9c3138bd6d8b3e303f0f711753506b330ffa8df0.zip llvm-9c3138bd6d8b3e303f0f711753506b330ffa8df0.tar.gz llvm-9c3138bd6d8b3e303f0f711753506b330ffa8df0.tar.bz2 |
[InstCombine] visitTrunc - pass through undefs for trunc(shift(trunc/ext(x),c)) patterns
Based on the recent patches D88475 and D88429 where we are losing undef values due to extension/comparisons.
I've added a Constant::mergeUndefsWith method that merges the undef scalar/elements from another Constant into a specific Constant.
Differential Revision: https://reviews.llvm.org/D88687
Diffstat (limited to 'llvm/lib/IR/Constants.cpp')
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 9f83861..7eca7dd 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -737,6 +737,40 @@ Constant *Constant::replaceUndefsWith(Constant *C, Constant *Replacement) { return ConstantVector::get(NewC); } +Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) { + assert(C && Other && "Expected non-nullptr constant arguments"); + if (match(C, m_Undef())) + return C; + + Type *Ty = C->getType(); + if (match(Other, m_Undef())) + return UndefValue::get(Ty); + + auto *VTy = dyn_cast<FixedVectorType>(Ty); + if (!VTy) + return C; + + Type *EltTy = VTy->getElementType(); + unsigned NumElts = VTy->getNumElements(); + assert(isa<FixedVectorType>(Other->getType()) && + cast<FixedVectorType>(Other->getType())->getNumElements() == NumElts && + "Type mismatch"); + + bool FoundExtraUndef = false; + SmallVector<Constant *, 32> NewC(NumElts); + for (unsigned I = 0; I != NumElts; ++I) { + NewC[I] = C->getAggregateElement(I); + Constant *OtherEltC = Other->getAggregateElement(I); + assert(NewC[I] && OtherEltC && "Unknown vector element"); + if (!match(NewC[I], m_Undef()) && match(OtherEltC, m_Undef())) { + NewC[I] = UndefValue::get(EltTy); + FoundExtraUndef = true; + } + } + if (FoundExtraUndef) + return ConstantVector::get(NewC); + return C; +} //===----------------------------------------------------------------------===// // ConstantInt |