New interface function is added to VectorUtils

Value *getSplatValue(Value *Val);

It complements the CreateVectorSplat(), which creates 2 instructions - insertelement and shuffle with all-zero mask.

The new function recognizes the pattern - insertelement+shuffle and returns the splat value (or nullptr).
It also returns a splat value form ConstantDataVector, for completeness.

Differential Revision:	http://reviews.llvm.org/D11124

llvm-svn: 246371
This commit is contained in:
Elena Demikhovsky
2015-08-30 07:28:18 +00:00
parent 799e880e95
commit a59fcfa56b
3 changed files with 44 additions and 17 deletions

View File

@@ -18,6 +18,8 @@
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Constants.h"
using namespace llvm;
using namespace llvm::PatternMatch;
@@ -406,3 +408,27 @@ Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
// Otherwise, we don't know.
return nullptr;
}
/// \brief Get splat value if the input is a splat vector or return nullptr.
/// The value may be extracted from a splat constants vector or from
/// a sequence of instructions that broadcast a single value into a vector.
llvm::Value *llvm::getSplatValue(Value *V) {
llvm::ConstantDataVector *CV = dyn_cast<llvm::ConstantDataVector>(V);
if (CV)
return CV->getSplatValue();
llvm::ShuffleVectorInst *ShuffleInst = dyn_cast<llvm::ShuffleVectorInst>(V);
if (!ShuffleInst)
return nullptr;
// All-zero (our undef) shuffle mask elements.
for (int i : ShuffleInst->getShuffleMask())
if (i != 0 && i != -1)
return nullptr;
// The first shuffle source is 'insertelement' with index 0.
llvm::InsertElementInst *InsertEltInst =
dyn_cast<llvm::InsertElementInst>(ShuffleInst->getOperand(0));
if (!InsertEltInst || !isa<ConstantInt>(InsertEltInst->getOperand(2)) ||
!cast<ConstantInt>(InsertEltInst->getOperand(2))->isNullValue())
return nullptr;
return InsertEltInst->getOperand(1);
}