[llvm] r364441 - [InstCombine] simplify code for inserts -> splat; NFC
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 26 08:52:59 PDT 2019
Author: spatel
Date: Wed Jun 26 08:52:59 2019
New Revision: 364441
URL: http://llvm.org/viewvc/llvm-project?rev=364441&view=rev
Log:
[InstCombine] simplify code for inserts -> splat; NFC
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp?rev=364441&r1=364440&r2=364441&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineVectorOps.cpp Wed Jun 26 08:52:59 2019
@@ -662,18 +662,17 @@ static bool isShuffleEquivalentToSelect(
return true;
}
-// Turn a chain of inserts that splats a value into a canonical insert + shuffle
-// splat. That is:
-// insertelt(insertelt(insertelt(insertelt X, %k, 0), %k, 1), %k, 2) ... ->
-// shufflevector(insertelt(X, %k, 0), undef, zero)
-static Instruction *foldInsSequenceIntoBroadcast(InsertElementInst &InsElt) {
- // We are interested in the last insert in a chain. So, if this insert
- // has a single user, and that user is an insert, bail.
+/// Turn a chain of inserts that splats a value into an insert + shuffle:
+/// insertelt(insertelt(insertelt(insertelt X, %k, 0), %k, 1), %k, 2) ... ->
+/// shufflevector(insertelt(X, %k, 0), undef, zero)
+static Instruction *foldInsSequenceIntoSplat(InsertElementInst &InsElt) {
+ // We are interested in the last insert in a chain. So if this insert has a
+ // single user and that user is an insert, bail.
if (InsElt.hasOneUse() && isa<InsertElementInst>(InsElt.user_back()))
return nullptr;
- VectorType *VT = cast<VectorType>(InsElt.getType());
- int NumElements = VT->getNumElements();
+ auto *VecTy = cast<VectorType>(InsElt.getType());
+ unsigned NumElements = VecTy->getNumElements();
// Do not try to do this for a one-element vector, since that's a nop,
// and will cause an inf-loop.
@@ -709,20 +708,15 @@ static Instruction *foldInsSequenceIntoB
if (llvm::any_of(ElementPresent, [](bool Present) { return !Present; }))
return nullptr;
- // All right, create the insert + shuffle.
- Instruction *InsertFirst;
- if (cast<ConstantInt>(FirstIE->getOperand(2))->isZero())
- InsertFirst = FirstIE;
- else
- InsertFirst = InsertElementInst::Create(
- UndefValue::get(VT), SplatVal,
- ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), 0),
- "", &InsElt);
+ // Create the insert + shuffle.
+ Type *Int32Ty = Type::getInt32Ty(InsElt.getContext());
+ UndefValue *UndefVec = UndefValue::get(VecTy);
+ Constant *Zero = ConstantInt::get(Int32Ty, 0);
+ if (!cast<ConstantInt>(FirstIE->getOperand(2))->isZero())
+ FirstIE = InsertElementInst::Create(UndefVec, SplatVal, Zero, "", &InsElt);
- Constant *ZeroMask = ConstantAggregateZero::get(
- VectorType::get(Type::getInt32Ty(InsElt.getContext()), NumElements));
-
- return new ShuffleVectorInst(InsertFirst, UndefValue::get(VT), ZeroMask);
+ Constant *ZeroMask = ConstantVector::getSplat(NumElements, Zero);
+ return new ShuffleVectorInst(FirstIE, UndefVec, ZeroMask);
}
/// If we have an insertelement instruction feeding into another insertelement
@@ -940,9 +934,7 @@ Instruction *InstCombiner::visitInsertEl
if (Instruction *NewInsElt = hoistInsEltConst(IE, Builder))
return NewInsElt;
- // Turn a sequence of inserts that broadcasts a scalar into a single
- // insert + shufflevector.
- if (Instruction *Broadcast = foldInsSequenceIntoBroadcast(IE))
+ if (Instruction *Broadcast = foldInsSequenceIntoSplat(IE))
return Broadcast;
return nullptr;
More information about the llvm-commits
mailing list