mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-18 11:24:01 +00:00
LoopVectorizer: Add support for loop-unrolling during vectorization for increasing the ILP. At the moment this feature is disabled by default and this commit should not cause any functional changes.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171436 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -42,6 +42,11 @@ static cl::opt<unsigned>
|
|||||||
VectorizationFactor("force-vector-width", cl::init(0), cl::Hidden,
|
VectorizationFactor("force-vector-width", cl::init(0), cl::Hidden,
|
||||||
cl::desc("Sets the SIMD width. Zero is autoselect."));
|
cl::desc("Sets the SIMD width. Zero is autoselect."));
|
||||||
|
|
||||||
|
static cl::opt<unsigned>
|
||||||
|
VectorizationUnroll("force-vector-unroll", cl::init(1), cl::Hidden,
|
||||||
|
cl::desc("Sets the vectorization unroll count. "
|
||||||
|
"Zero is autoselect."));
|
||||||
|
|
||||||
static cl::opt<bool>
|
static cl::opt<bool>
|
||||||
EnableIfConversion("enable-if-conversion", cl::init(true), cl::Hidden,
|
EnableIfConversion("enable-if-conversion", cl::init(true), cl::Hidden,
|
||||||
cl::desc("Enable if-conversion during vectorization."));
|
cl::desc("Enable if-conversion during vectorization."));
|
||||||
@ -117,7 +122,7 @@ struct LoopVectorize : public LoopPass {
|
|||||||
F->getParent()->getModuleIdentifier()<<"\n");
|
F->getParent()->getModuleIdentifier()<<"\n");
|
||||||
|
|
||||||
// If we decided that it is *legal* to vectorizer the loop then do it.
|
// If we decided that it is *legal* to vectorizer the loop then do it.
|
||||||
InnerLoopVectorizer LB(L, SE, LI, DT, DL, VF);
|
InnerLoopVectorizer LB(L, SE, LI, DT, DL, VF, VectorizationUnroll);
|
||||||
LB.vectorize(&LVL);
|
LB.vectorize(&LVL);
|
||||||
|
|
||||||
DEBUG(verifyFunction(*L->getHeader()->getParent()));
|
DEBUG(verifyFunction(*L->getHeader()->getParent()));
|
||||||
@ -180,7 +185,8 @@ Value *InnerLoopVectorizer::getBroadcastInstrs(Value *V) {
|
|||||||
return Shuf;
|
return Shuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value *InnerLoopVectorizer::getConsecutiveVector(Value* Val, bool Negate) {
|
Value *InnerLoopVectorizer::getConsecutiveVector(Value* Val, unsigned StartIdx,
|
||||||
|
bool Negate) {
|
||||||
assert(Val->getType()->isVectorTy() && "Must be a vector");
|
assert(Val->getType()->isVectorTy() && "Must be a vector");
|
||||||
assert(Val->getType()->getScalarType()->isIntegerTy() &&
|
assert(Val->getType()->getScalarType()->isIntegerTy() &&
|
||||||
"Elem must be an integer");
|
"Elem must be an integer");
|
||||||
@ -191,8 +197,10 @@ Value *InnerLoopVectorizer::getConsecutiveVector(Value* Val, bool Negate) {
|
|||||||
SmallVector<Constant*, 8> Indices;
|
SmallVector<Constant*, 8> Indices;
|
||||||
|
|
||||||
// Create a vector of consecutive numbers from zero to VF.
|
// Create a vector of consecutive numbers from zero to VF.
|
||||||
for (int i = 0; i < VLen; ++i)
|
for (int i = 0; i < VLen; ++i) {
|
||||||
Indices.push_back(ConstantInt::get(ITy, Negate ? (-i): i ));
|
int Idx = Negate ? (-i): i;
|
||||||
|
Indices.push_back(ConstantInt::get(ITy, StartIdx + Idx));
|
||||||
|
}
|
||||||
|
|
||||||
// Add the consecutive indices to the vector value.
|
// Add the consecutive indices to the vector value.
|
||||||
Constant *Cv = ConstantVector::get(Indices);
|
Constant *Cv = ConstantVector::get(Indices);
|
||||||
@ -244,18 +252,20 @@ bool LoopVectorizationLegality::isUniform(Value *V) {
|
|||||||
return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
|
return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
|
||||||
}
|
}
|
||||||
|
|
||||||
Value *InnerLoopVectorizer::getVectorValue(Value *V) {
|
InnerLoopVectorizer::VectorParts&
|
||||||
|
InnerLoopVectorizer::getVectorValue(Value *V) {
|
||||||
assert(V != Induction && "The new induction variable should not be used.");
|
assert(V != Induction && "The new induction variable should not be used.");
|
||||||
assert(!V->getType()->isVectorTy() && "Can't widen a vector");
|
assert(!V->getType()->isVectorTy() && "Can't widen a vector");
|
||||||
// If we saved a vectorized copy of V, use it.
|
|
||||||
Value *&MapEntry = WidenMap[V];
|
|
||||||
if (MapEntry)
|
|
||||||
return MapEntry;
|
|
||||||
|
|
||||||
// Broadcast V and save the value for future uses.
|
// If we have this scalar in the map, return it.
|
||||||
|
if (WidenMap.has(V))
|
||||||
|
return WidenMap.get(V);
|
||||||
|
|
||||||
|
// If this scalar is unknown, assume that it is a constant or that it is
|
||||||
|
// loop invariant. Broadcast V and save the value for future uses.
|
||||||
Value *B = getBroadcastInstrs(V);
|
Value *B = getBroadcastInstrs(V);
|
||||||
MapEntry = B;
|
WidenMap.splat(V, B);
|
||||||
return B;
|
return WidenMap.get(V);
|
||||||
}
|
}
|
||||||
|
|
||||||
Constant*
|
Constant*
|
||||||
@ -277,7 +287,7 @@ Value *InnerLoopVectorizer::reverseVector(Value *Vec) {
|
|||||||
void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr) {
|
void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr) {
|
||||||
assert(!Instr->getType()->isAggregateType() && "Can't handle vectors");
|
assert(!Instr->getType()->isAggregateType() && "Can't handle vectors");
|
||||||
// Holds vector parameters or scalars, in case of uniform vals.
|
// Holds vector parameters or scalars, in case of uniform vals.
|
||||||
SmallVector<Value*, 8> Params;
|
SmallVector<VectorParts, 4> Params;
|
||||||
|
|
||||||
// Find all of the vectorized parameters.
|
// Find all of the vectorized parameters.
|
||||||
for (unsigned op = 0, e = Instr->getNumOperands(); op != e; ++op) {
|
for (unsigned op = 0, e = Instr->getNumOperands(); op != e; ++op) {
|
||||||
@ -295,12 +305,14 @@ void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr) {
|
|||||||
// If the src is an instruction that appeared earlier in the basic block
|
// If the src is an instruction that appeared earlier in the basic block
|
||||||
// then it should already be vectorized.
|
// then it should already be vectorized.
|
||||||
if (SrcInst && OrigLoop->contains(SrcInst)) {
|
if (SrcInst && OrigLoop->contains(SrcInst)) {
|
||||||
assert(WidenMap.count(SrcInst) && "Source operand is unavailable");
|
assert(WidenMap.has(SrcInst) && "Source operand is unavailable");
|
||||||
// The parameter is a vector value from earlier.
|
// The parameter is a vector value from earlier.
|
||||||
Params.push_back(WidenMap[SrcInst]);
|
Params.push_back(WidenMap.get(SrcInst));
|
||||||
} else {
|
} else {
|
||||||
// The parameter is a scalar from outside the loop. Maybe even a constant.
|
// The parameter is a scalar from outside the loop. Maybe even a constant.
|
||||||
Params.push_back(SrcOp);
|
VectorParts Scalars;
|
||||||
|
Scalars.append(UF, SrcOp);
|
||||||
|
Params.push_back(Scalars);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,39 +321,38 @@ void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr) {
|
|||||||
|
|
||||||
// Does this instruction return a value ?
|
// Does this instruction return a value ?
|
||||||
bool IsVoidRetTy = Instr->getType()->isVoidTy();
|
bool IsVoidRetTy = Instr->getType()->isVoidTy();
|
||||||
Value *VecResults = 0;
|
|
||||||
|
|
||||||
// If we have a return value, create an empty vector. We place the scalarized
|
Value *UndefVec = IsVoidRetTy ? 0 :
|
||||||
// instructions in this vector.
|
UndefValue::get(VectorType::get(Instr->getType(), VF));
|
||||||
if (!IsVoidRetTy)
|
// Create a new entry in the WidenMap and initialize it to Undef or Null.
|
||||||
VecResults = UndefValue::get(VectorType::get(Instr->getType(), VF));
|
VectorParts &VecResults = WidenMap.splat(Instr, UndefVec);
|
||||||
|
|
||||||
// For each scalar that we create:
|
// For each scalar that we create:
|
||||||
for (unsigned i = 0; i < VF; ++i) {
|
for (unsigned Width = 0; Width < VF; ++Width) {
|
||||||
Instruction *Cloned = Instr->clone();
|
// For each vector unroll 'part':
|
||||||
if (!IsVoidRetTy)
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
Cloned->setName(Instr->getName() + ".cloned");
|
Instruction *Cloned = Instr->clone();
|
||||||
// Replace the operands of the cloned instrucions with extracted scalars.
|
if (!IsVoidRetTy)
|
||||||
for (unsigned op = 0, e = Instr->getNumOperands(); op != e; ++op) {
|
Cloned->setName(Instr->getName() + ".cloned");
|
||||||
Value *Op = Params[op];
|
// Replace the operands of the cloned instrucions with extracted scalars.
|
||||||
// Param is a vector. Need to extract the right lane.
|
for (unsigned op = 0, e = Instr->getNumOperands(); op != e; ++op) {
|
||||||
if (Op->getType()->isVectorTy())
|
Value *Op = Params[op][Part];
|
||||||
Op = Builder.CreateExtractElement(Op, Builder.getInt32(i));
|
// Param is a vector. Need to extract the right lane.
|
||||||
Cloned->setOperand(op, Op);
|
if (Op->getType()->isVectorTy())
|
||||||
|
Op = Builder.CreateExtractElement(Op, Builder.getInt32(Width));
|
||||||
|
Cloned->setOperand(op, Op);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Place the cloned scalar in the new loop.
|
||||||
|
Builder.Insert(Cloned);
|
||||||
|
|
||||||
|
// If the original scalar returns a value we need to place it in a vector
|
||||||
|
// so that future users will be able to use it.
|
||||||
|
if (!IsVoidRetTy)
|
||||||
|
VecResults[Part] = Builder.CreateInsertElement(VecResults[Part], Cloned,
|
||||||
|
Builder.getInt32(Width));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place the cloned scalar in the new loop.
|
|
||||||
Builder.Insert(Cloned);
|
|
||||||
|
|
||||||
// If the original scalar returns a value we need to place it in a vector
|
|
||||||
// so that future users will be able to use it.
|
|
||||||
if (!IsVoidRetTy)
|
|
||||||
VecResults = Builder.CreateInsertElement(VecResults, Cloned,
|
|
||||||
Builder.getInt32(i));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsVoidRetTy)
|
|
||||||
WidenMap[Instr] = VecResults;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Value*
|
Value*
|
||||||
@ -503,7 +514,9 @@ InnerLoopVectorizer::createEmptyLoop(LoopVectorizationLegality *Legal) {
|
|||||||
|
|
||||||
// Generate the induction variable.
|
// Generate the induction variable.
|
||||||
Induction = Builder.CreatePHI(IdxTy, 2, "index");
|
Induction = Builder.CreatePHI(IdxTy, 2, "index");
|
||||||
Constant *Step = ConstantInt::get(IdxTy, VF);
|
// The loop step is equal to the vectorization factor (num of SIMD elements)
|
||||||
|
// times the unroll factor (num of SIMD instructions).
|
||||||
|
Constant *Step = ConstantInt::get(IdxTy, VF * UF);
|
||||||
|
|
||||||
// We may need to extend the index in case there is a type mismatch.
|
// We may need to extend the index in case there is a type mismatch.
|
||||||
// We know that the count starts at zero and does not overflow.
|
// We know that the count starts at zero and does not overflow.
|
||||||
@ -521,8 +534,7 @@ InnerLoopVectorizer::createEmptyLoop(LoopVectorizationLegality *Legal) {
|
|||||||
|
|
||||||
// Now we need to generate the expression for N - (N % VF), which is
|
// Now we need to generate the expression for N - (N % VF), which is
|
||||||
// the part that the vectorized body will execute.
|
// the part that the vectorized body will execute.
|
||||||
Constant *CIVF = ConstantInt::get(IdxTy, VF);
|
Value *R = BinaryOperator::CreateURem(Count, Step, "n.mod.vf", Loc);
|
||||||
Value *R = BinaryOperator::CreateURem(Count, CIVF, "n.mod.vf", Loc);
|
|
||||||
Value *CountRoundDown = BinaryOperator::CreateSub(Count, R, "n.vec", Loc);
|
Value *CountRoundDown = BinaryOperator::CreateSub(Count, R, "n.vec", Loc);
|
||||||
Value *IdxEndRoundDown = BinaryOperator::CreateAdd(CountRoundDown, StartIdx,
|
Value *IdxEndRoundDown = BinaryOperator::CreateAdd(CountRoundDown, StartIdx,
|
||||||
"end.idx.rnd.down", Loc);
|
"end.idx.rnd.down", Loc);
|
||||||
@ -775,7 +787,6 @@ InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) {
|
|||||||
for (PhiVector::iterator it = RdxPHIsToFix.begin(), e = RdxPHIsToFix.end();
|
for (PhiVector::iterator it = RdxPHIsToFix.begin(), e = RdxPHIsToFix.end();
|
||||||
it != e; ++it) {
|
it != e; ++it) {
|
||||||
PHINode *RdxPhi = *it;
|
PHINode *RdxPhi = *it;
|
||||||
PHINode *VecRdxPhi = dyn_cast<PHINode>(WidenMap[RdxPhi]);
|
|
||||||
assert(RdxPhi && "Unable to recover vectorized PHI");
|
assert(RdxPhi && "Unable to recover vectorized PHI");
|
||||||
|
|
||||||
// Find the reduction variable descriptor.
|
// Find the reduction variable descriptor.
|
||||||
@ -791,8 +802,8 @@ InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) {
|
|||||||
Builder.SetInsertPoint(LoopBypassBlock->getTerminator());
|
Builder.SetInsertPoint(LoopBypassBlock->getTerminator());
|
||||||
|
|
||||||
// This is the vector-clone of the value that leaves the loop.
|
// This is the vector-clone of the value that leaves the loop.
|
||||||
Value *VectorExit = getVectorValue(RdxDesc.LoopExitInstr);
|
VectorParts &VectorExit = getVectorValue(RdxDesc.LoopExitInstr);
|
||||||
Type *VecTy = VectorExit->getType();
|
Type *VecTy = VectorExit[0]->getType();
|
||||||
|
|
||||||
// Find the reduction identity variable. Zero for addition, or, xor,
|
// Find the reduction identity variable. Zero for addition, or, xor,
|
||||||
// one for multiplication, -1 for And.
|
// one for multiplication, -1 for And.
|
||||||
@ -811,10 +822,17 @@ InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) {
|
|||||||
|
|
||||||
// Reductions do not have to start at zero. They can start with
|
// Reductions do not have to start at zero. They can start with
|
||||||
// any loop invariant values.
|
// any loop invariant values.
|
||||||
VecRdxPhi->addIncoming(VectorStart, VecPreheader);
|
VectorParts &VecRdxPhi = WidenMap.get(RdxPhi);
|
||||||
Value *Val =
|
BasicBlock *Latch = OrigLoop->getLoopLatch();
|
||||||
getVectorValue(RdxPhi->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
|
Value *LoopVal = RdxPhi->getIncomingValueForBlock(Latch);
|
||||||
VecRdxPhi->addIncoming(Val, LoopVectorBody);
|
VectorParts &Val = getVectorValue(LoopVal);
|
||||||
|
for (unsigned part = 0; part < UF; ++part) {
|
||||||
|
// Make sure to add the reduction stat value only to the
|
||||||
|
// first unroll part.
|
||||||
|
Value *StartVal = (part == 0) ? VectorStart : Identity;
|
||||||
|
cast<PHINode>(VecRdxPhi[part])->addIncoming(StartVal, VecPreheader);
|
||||||
|
cast<PHINode>(VecRdxPhi[part])->addIncoming(Val[part], LoopVectorBody);
|
||||||
|
}
|
||||||
|
|
||||||
// Before each round, move the insertion point right between
|
// Before each round, move the insertion point right between
|
||||||
// the PHIs and the values we are going to write.
|
// the PHIs and the values we are going to write.
|
||||||
@ -822,18 +840,54 @@ InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) {
|
|||||||
// instructions.
|
// instructions.
|
||||||
Builder.SetInsertPoint(LoopMiddleBlock->getFirstInsertionPt());
|
Builder.SetInsertPoint(LoopMiddleBlock->getFirstInsertionPt());
|
||||||
|
|
||||||
// This PHINode contains the vectorized reduction variable, or
|
VectorParts RdxParts;
|
||||||
// the initial value vector, if we bypass the vector loop.
|
for (unsigned part = 0; part < UF; ++part) {
|
||||||
PHINode *NewPhi = Builder.CreatePHI(VecTy, 2, "rdx.vec.exit.phi");
|
// This PHINode contains the vectorized reduction variable, or
|
||||||
NewPhi->addIncoming(VectorStart, LoopBypassBlock);
|
// the initial value vector, if we bypass the vector loop.
|
||||||
NewPhi->addIncoming(getVectorValue(RdxDesc.LoopExitInstr), LoopVectorBody);
|
VectorParts &RdxExitVal = getVectorValue(RdxDesc.LoopExitInstr);
|
||||||
|
PHINode *NewPhi = Builder.CreatePHI(VecTy, 2, "rdx.vec.exit.phi");
|
||||||
|
Value *StartVal = (part == 0) ? VectorStart : Identity;
|
||||||
|
NewPhi->addIncoming(StartVal, LoopBypassBlock);
|
||||||
|
NewPhi->addIncoming(RdxExitVal[part], LoopVectorBody);
|
||||||
|
RdxParts.push_back(NewPhi);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reduce all of the unrolled parts into a single vector.
|
||||||
|
Value *ReducedPartRdx = RdxParts[0];
|
||||||
|
for (unsigned part = 1; part < UF; ++part) {
|
||||||
|
switch (RdxDesc.Kind) {
|
||||||
|
case LoopVectorizationLegality::IntegerAdd:
|
||||||
|
ReducedPartRdx =
|
||||||
|
Builder.CreateAdd(RdxParts[part], ReducedPartRdx, "add.rdx");
|
||||||
|
break;
|
||||||
|
case LoopVectorizationLegality::IntegerMult:
|
||||||
|
ReducedPartRdx =
|
||||||
|
Builder.CreateMul(RdxParts[part], ReducedPartRdx, "mul.rdx");
|
||||||
|
break;
|
||||||
|
case LoopVectorizationLegality::IntegerOr:
|
||||||
|
ReducedPartRdx =
|
||||||
|
Builder.CreateOr(RdxParts[part], ReducedPartRdx, "or.rdx");
|
||||||
|
break;
|
||||||
|
case LoopVectorizationLegality::IntegerAnd:
|
||||||
|
ReducedPartRdx =
|
||||||
|
Builder.CreateAnd(RdxParts[part], ReducedPartRdx, "and.rdx");
|
||||||
|
break;
|
||||||
|
case LoopVectorizationLegality::IntegerXor:
|
||||||
|
ReducedPartRdx =
|
||||||
|
Builder.CreateXor(RdxParts[part], ReducedPartRdx, "xor.rdx");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
llvm_unreachable("Unknown reduction operation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
|
// VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
|
||||||
// and vector ops, reducing the set of values being computed by half each
|
// and vector ops, reducing the set of values being computed by half each
|
||||||
// round.
|
// round.
|
||||||
assert(isPowerOf2_32(VF) &&
|
assert(isPowerOf2_32(VF) &&
|
||||||
"Reduction emission only supported for pow2 vectors!");
|
"Reduction emission only supported for pow2 vectors!");
|
||||||
Value *TmpVec = NewPhi;
|
Value *TmpVec = ReducedPartRdx;
|
||||||
SmallVector<Constant*, 32> ShuffleMask(VF, 0);
|
SmallVector<Constant*, 32> ShuffleMask(VF, 0);
|
||||||
for (unsigned i = VF; i != 1; i >>= 1) {
|
for (unsigned i = VF; i != 1; i >>= 1) {
|
||||||
// Move the upper half of the vector to the lower half.
|
// Move the upper half of the vector to the lower half.
|
||||||
@ -922,27 +976,34 @@ InnerLoopVectorizer::vectorizeLoop(LoopVectorizationLegality *Legal) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value *InnerLoopVectorizer::createEdgeMask(BasicBlock *Src, BasicBlock *Dst) {
|
InnerLoopVectorizer::VectorParts
|
||||||
|
InnerLoopVectorizer::createEdgeMask(BasicBlock *Src, BasicBlock *Dst) {
|
||||||
assert(std::find(pred_begin(Dst), pred_end(Dst), Src) != pred_end(Dst) &&
|
assert(std::find(pred_begin(Dst), pred_end(Dst), Src) != pred_end(Dst) &&
|
||||||
"Invalid edge");
|
"Invalid edge");
|
||||||
|
|
||||||
Value *SrcMask = createBlockInMask(Src);
|
VectorParts SrcMask = createBlockInMask(Src);
|
||||||
|
|
||||||
// The terminator has to be a branch inst!
|
// The terminator has to be a branch inst!
|
||||||
BranchInst *BI = dyn_cast<BranchInst>(Src->getTerminator());
|
BranchInst *BI = dyn_cast<BranchInst>(Src->getTerminator());
|
||||||
assert(BI && "Unexpected terminator found");
|
assert(BI && "Unexpected terminator found");
|
||||||
|
|
||||||
Value *EdgeMask = SrcMask;
|
|
||||||
if (BI->isConditional()) {
|
if (BI->isConditional()) {
|
||||||
EdgeMask = getVectorValue(BI->getCondition());
|
VectorParts EdgeMask = getVectorValue(BI->getCondition());
|
||||||
|
|
||||||
if (BI->getSuccessor(0) != Dst)
|
if (BI->getSuccessor(0) != Dst)
|
||||||
EdgeMask = Builder.CreateNot(EdgeMask);
|
for (unsigned part = 0; part < UF; ++part)
|
||||||
|
EdgeMask[part] = Builder.CreateNot(EdgeMask[part]);
|
||||||
|
|
||||||
|
for (unsigned part = 0; part < UF; ++part)
|
||||||
|
EdgeMask[part] = Builder.CreateAnd(EdgeMask[part], SrcMask[part]);
|
||||||
|
return EdgeMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Builder.CreateAnd(EdgeMask, SrcMask);
|
return SrcMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value *InnerLoopVectorizer::createBlockInMask(BasicBlock *BB) {
|
InnerLoopVectorizer::VectorParts
|
||||||
|
InnerLoopVectorizer::createBlockInMask(BasicBlock *BB) {
|
||||||
assert(OrigLoop->contains(BB) && "Block is not a part of a loop");
|
assert(OrigLoop->contains(BB) && "Block is not a part of a loop");
|
||||||
|
|
||||||
// Loop incoming mask is all-one.
|
// Loop incoming mask is all-one.
|
||||||
@ -953,11 +1014,14 @@ Value *InnerLoopVectorizer::createBlockInMask(BasicBlock *BB) {
|
|||||||
|
|
||||||
// This is the block mask. We OR all incoming edges, and with zero.
|
// This is the block mask. We OR all incoming edges, and with zero.
|
||||||
Value *Zero = ConstantInt::get(IntegerType::getInt1Ty(BB->getContext()), 0);
|
Value *Zero = ConstantInt::get(IntegerType::getInt1Ty(BB->getContext()), 0);
|
||||||
Value *BlockMask = getVectorValue(Zero);
|
VectorParts BlockMask = getVectorValue(Zero);
|
||||||
|
|
||||||
// For each pred:
|
// For each pred:
|
||||||
for (pred_iterator it = pred_begin(BB), e = pred_end(BB); it != e; ++it)
|
for (pred_iterator it = pred_begin(BB), e = pred_end(BB); it != e; ++it) {
|
||||||
BlockMask = Builder.CreateOr(BlockMask, createEdgeMask(*it, BB));
|
VectorParts EM = createEdgeMask(*it, BB);
|
||||||
|
for (unsigned part = 0; part < UF; ++part)
|
||||||
|
BlockMask[part] = Builder.CreateOr(BlockMask[part], EM[part]);
|
||||||
|
}
|
||||||
|
|
||||||
return BlockMask;
|
return BlockMask;
|
||||||
}
|
}
|
||||||
@ -969,6 +1033,7 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
|
|
||||||
// For each instruction in the old loop.
|
// For each instruction in the old loop.
|
||||||
for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
|
for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
|
||||||
|
VectorParts &Entry = WidenMap.get(it);
|
||||||
switch (it->getOpcode()) {
|
switch (it->getOpcode()) {
|
||||||
case Instruction::Br:
|
case Instruction::Br:
|
||||||
// Nothing to do for PHIs and BR, since we already took care of the
|
// Nothing to do for PHIs and BR, since we already took care of the
|
||||||
@ -978,11 +1043,12 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
PHINode* P = cast<PHINode>(it);
|
PHINode* P = cast<PHINode>(it);
|
||||||
// Handle reduction variables:
|
// Handle reduction variables:
|
||||||
if (Legal->getReductionVars()->count(P)) {
|
if (Legal->getReductionVars()->count(P)) {
|
||||||
// This is phase one of vectorizing PHIs.
|
for (unsigned part = 0; part < UF; ++part) {
|
||||||
Type *VecTy = VectorType::get(it->getType(), VF);
|
// This is phase one of vectorizing PHIs.
|
||||||
WidenMap[it] =
|
Type *VecTy = VectorType::get(it->getType(), VF);
|
||||||
PHINode::Create(VecTy, 2, "vec.phi",
|
Entry[part] = PHINode::Create(VecTy, 2, "vec.phi",
|
||||||
LoopVectorBody->getFirstInsertionPt());
|
LoopVectorBody-> getFirstInsertionPt());
|
||||||
|
}
|
||||||
PV->push_back(P);
|
PV->push_back(P);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -996,12 +1062,15 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
// At this point we generate the predication tree. There may be
|
// At this point we generate the predication tree. There may be
|
||||||
// duplications since this is a simple recursive scan, but future
|
// duplications since this is a simple recursive scan, but future
|
||||||
// optimizations will clean it up.
|
// optimizations will clean it up.
|
||||||
Value *Cond = createEdgeMask(P->getIncomingBlock(0), P->getParent());
|
VectorParts Cond = createEdgeMask(P->getIncomingBlock(0),
|
||||||
WidenMap[P] =
|
P->getParent());
|
||||||
Builder.CreateSelect(Cond,
|
|
||||||
getVectorValue(P->getIncomingValue(0)),
|
for (unsigned part = 0; part < UF; ++part) {
|
||||||
getVectorValue(P->getIncomingValue(1)),
|
VectorParts &In0 = getVectorValue(P->getIncomingValue(0));
|
||||||
"predphi");
|
VectorParts &In1 = getVectorValue(P->getIncomingValue(1));
|
||||||
|
Entry[part] = Builder.CreateSelect(Cond[part], In0[part], In1[part],
|
||||||
|
"predphi");
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1021,8 +1090,8 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
Value *Broadcasted = getBroadcastInstrs(Induction);
|
Value *Broadcasted = getBroadcastInstrs(Induction);
|
||||||
// After broadcasting the induction variable we need to make the
|
// After broadcasting the induction variable we need to make the
|
||||||
// vector consecutive by adding 0, 1, 2 ...
|
// vector consecutive by adding 0, 1, 2 ...
|
||||||
Value *ConsecutiveInduction = getConsecutiveVector(Broadcasted);
|
for (unsigned part = 0; part < UF; ++part)
|
||||||
WidenMap[OldInduction] = ConsecutiveInduction;
|
Entry[part] = getConsecutiveVector(Broadcasted, VF * part, false);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case LoopVectorizationLegality::ReverseIntInduction:
|
case LoopVectorizationLegality::ReverseIntInduction:
|
||||||
@ -1054,9 +1123,8 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
Value *Broadcasted = getBroadcastInstrs(ReverseInd);
|
Value *Broadcasted = getBroadcastInstrs(ReverseInd);
|
||||||
// After broadcasting the induction variable we need to make the
|
// After broadcasting the induction variable we need to make the
|
||||||
// vector consecutive by adding ... -3, -2, -1, 0.
|
// vector consecutive by adding ... -3, -2, -1, 0.
|
||||||
Value *ConsecutiveInduction = getConsecutiveVector(Broadcasted,
|
for (unsigned part = 0; part < UF; ++part)
|
||||||
true);
|
Entry[part] = getConsecutiveVector(Broadcasted, -VF * part, true);
|
||||||
WidenMap[it] = ConsecutiveInduction;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1065,19 +1133,21 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
|
|
||||||
// This is the vector of results. Notice that we don't generate
|
// This is the vector of results. Notice that we don't generate
|
||||||
// vector geps because scalar geps result in better code.
|
// vector geps because scalar geps result in better code.
|
||||||
Value *VecVal = UndefValue::get(VectorType::get(P->getType(), VF));
|
for (unsigned part = 0; part < UF; ++part) {
|
||||||
for (unsigned int i = 0; i < VF; ++i) {
|
Value *VecVal = UndefValue::get(VectorType::get(P->getType(), VF));
|
||||||
Constant *Idx = ConstantInt::get(Induction->getType(), i);
|
for (unsigned int i = 0; i < VF; ++i) {
|
||||||
Value *GlobalIdx = Builder.CreateAdd(NormalizedIdx, Idx,
|
Constant *Idx = ConstantInt::get(Induction->getType(),
|
||||||
"gep.idx");
|
i + part * VF);
|
||||||
Value *SclrGep = Builder.CreateGEP(II.StartValue, GlobalIdx,
|
Value *GlobalIdx = Builder.CreateAdd(NormalizedIdx, Idx,
|
||||||
"next.gep");
|
"gep.idx");
|
||||||
VecVal = Builder.CreateInsertElement(VecVal, SclrGep,
|
Value *SclrGep = Builder.CreateGEP(II.StartValue, GlobalIdx,
|
||||||
Builder.getInt32(i),
|
"next.gep");
|
||||||
"insert.gep");
|
VecVal = Builder.CreateInsertElement(VecVal, SclrGep,
|
||||||
|
Builder.getInt32(i),
|
||||||
|
"insert.gep");
|
||||||
|
}
|
||||||
|
Entry[part] = VecVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
WidenMap[it] = VecVal;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1103,41 +1173,48 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
case Instruction::Xor: {
|
case Instruction::Xor: {
|
||||||
// Just widen binops.
|
// Just widen binops.
|
||||||
BinaryOperator *BinOp = dyn_cast<BinaryOperator>(it);
|
BinaryOperator *BinOp = dyn_cast<BinaryOperator>(it);
|
||||||
Value *A = getVectorValue(it->getOperand(0));
|
VectorParts &A = getVectorValue(it->getOperand(0));
|
||||||
Value *B = getVectorValue(it->getOperand(1));
|
VectorParts &B = getVectorValue(it->getOperand(1));
|
||||||
|
|
||||||
// Use this vector value for all users of the original instruction.
|
// Use this vector value for all users of the original instruction.
|
||||||
Value *V = Builder.CreateBinOp(BinOp->getOpcode(), A, B);
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
WidenMap[it] = V;
|
Value *V = Builder.CreateBinOp(BinOp->getOpcode(), A[Part], B[Part]);
|
||||||
|
|
||||||
// Update the NSW, NUW and Exact flags.
|
// Update the NSW, NUW and Exact flags.
|
||||||
BinaryOperator *VecOp = cast<BinaryOperator>(V);
|
BinaryOperator *VecOp = cast<BinaryOperator>(V);
|
||||||
if (isa<OverflowingBinaryOperator>(BinOp)) {
|
if (isa<OverflowingBinaryOperator>(BinOp)) {
|
||||||
VecOp->setHasNoSignedWrap(BinOp->hasNoSignedWrap());
|
VecOp->setHasNoSignedWrap(BinOp->hasNoSignedWrap());
|
||||||
VecOp->setHasNoUnsignedWrap(BinOp->hasNoUnsignedWrap());
|
VecOp->setHasNoUnsignedWrap(BinOp->hasNoUnsignedWrap());
|
||||||
|
}
|
||||||
|
if (isa<PossiblyExactOperator>(VecOp))
|
||||||
|
VecOp->setIsExact(BinOp->isExact());
|
||||||
|
|
||||||
|
Entry[Part] = V;
|
||||||
}
|
}
|
||||||
if (isa<PossiblyExactOperator>(VecOp))
|
|
||||||
VecOp->setIsExact(BinOp->isExact());
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Instruction::Select: {
|
case Instruction::Select: {
|
||||||
// Widen selects.
|
// Widen selects.
|
||||||
// If the selector is loop invariant we can create a select
|
// If the selector is loop invariant we can create a select
|
||||||
// instruction with a scalar condition. Otherwise, use vector-select.
|
// instruction with a scalar condition. Otherwise, use vector-select.
|
||||||
Value *Cond = it->getOperand(0);
|
bool InvariantCond = SE->isLoopInvariant(SE->getSCEV(it->getOperand(0)),
|
||||||
bool InvariantCond = SE->isLoopInvariant(SE->getSCEV(Cond), OrigLoop);
|
OrigLoop);
|
||||||
|
|
||||||
// The condition can be loop invariant but still defined inside the
|
// The condition can be loop invariant but still defined inside the
|
||||||
// loop. This means that we can't just use the original 'cond' value.
|
// loop. This means that we can't just use the original 'cond' value.
|
||||||
// We have to take the 'vectorized' value and pick the first lane.
|
// We have to take the 'vectorized' value and pick the first lane.
|
||||||
// Instcombine will make this a no-op.
|
// Instcombine will make this a no-op.
|
||||||
Cond = getVectorValue(Cond);
|
VectorParts &Cond = getVectorValue(it->getOperand(0));
|
||||||
if (InvariantCond)
|
VectorParts &Op0 = getVectorValue(it->getOperand(1));
|
||||||
Cond = Builder.CreateExtractElement(Cond, Builder.getInt32(0));
|
VectorParts &Op1 = getVectorValue(it->getOperand(2));
|
||||||
|
Value *ScalarCond = Builder.CreateExtractElement(Cond[0],
|
||||||
Value *Op0 = getVectorValue(it->getOperand(1));
|
Builder.getInt32(0));
|
||||||
Value *Op1 = getVectorValue(it->getOperand(2));
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
WidenMap[it] = Builder.CreateSelect(Cond, Op0, Op1);
|
Entry[Part] = Builder.CreateSelect(
|
||||||
|
InvariantCond ? ScalarCond : Cond[Part],
|
||||||
|
Op0[Part],
|
||||||
|
Op1[Part]);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1146,12 +1223,16 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
// Widen compares. Generate vector compares.
|
// Widen compares. Generate vector compares.
|
||||||
bool FCmp = (it->getOpcode() == Instruction::FCmp);
|
bool FCmp = (it->getOpcode() == Instruction::FCmp);
|
||||||
CmpInst *Cmp = dyn_cast<CmpInst>(it);
|
CmpInst *Cmp = dyn_cast<CmpInst>(it);
|
||||||
Value *A = getVectorValue(it->getOperand(0));
|
VectorParts &A = getVectorValue(it->getOperand(0));
|
||||||
Value *B = getVectorValue(it->getOperand(1));
|
VectorParts &B = getVectorValue(it->getOperand(1));
|
||||||
if (FCmp)
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
WidenMap[it] = Builder.CreateFCmp(Cmp->getPredicate(), A, B);
|
Value *C = 0;
|
||||||
else
|
if (FCmp)
|
||||||
WidenMap[it] = Builder.CreateICmp(Cmp->getPredicate(), A, B);
|
C = Builder.CreateFCmp(Cmp->getPredicate(), A[Part], B[Part]);
|
||||||
|
else
|
||||||
|
C = Builder.CreateICmp(Cmp->getPredicate(), A[Part], B[Part]);
|
||||||
|
Entry[Part] = C;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1173,12 +1254,17 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle consecutive stores.
|
||||||
|
|
||||||
GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(Ptr);
|
GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(Ptr);
|
||||||
if (Gep) {
|
if (Gep) {
|
||||||
// The last index does not have to be the induction. It can be
|
// The last index does not have to be the induction. It can be
|
||||||
// consecutive and be a function of the index. For example A[I+1];
|
// consecutive and be a function of the index. For example A[I+1];
|
||||||
unsigned NumOperands = Gep->getNumOperands();
|
unsigned NumOperands = Gep->getNumOperands();
|
||||||
Value *LastIndex = getVectorValue(Gep->getOperand(NumOperands - 1));
|
|
||||||
|
Value *LastGepOperand = Gep->getOperand(NumOperands - 1);
|
||||||
|
VectorParts &GEPParts = getVectorValue(LastGepOperand);
|
||||||
|
Value *LastIndex = GEPParts[0];
|
||||||
LastIndex = Builder.CreateExtractElement(LastIndex, Zero);
|
LastIndex = Builder.CreateExtractElement(LastIndex, Zero);
|
||||||
|
|
||||||
// Create the new GEP with the new induction variable.
|
// Create the new GEP with the new induction variable.
|
||||||
@ -1188,19 +1274,28 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
} else {
|
} else {
|
||||||
// Use the induction element ptr.
|
// Use the induction element ptr.
|
||||||
assert(isa<PHINode>(Ptr) && "Invalid induction ptr");
|
assert(isa<PHINode>(Ptr) && "Invalid induction ptr");
|
||||||
Ptr = Builder.CreateExtractElement(getVectorValue(Ptr), Zero);
|
VectorParts &PtrVal = getVectorValue(Ptr);
|
||||||
|
Ptr = Builder.CreateExtractElement(PtrVal[0], Zero);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the address is consecutive but reversed, then the
|
VectorParts &StoredVal = getVectorValue(SI->getValueOperand());
|
||||||
// wide load needs to start at the last vector element.
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
if (Reverse)
|
// Calculate the pointer for the specific unroll-part.
|
||||||
Ptr = Builder.CreateGEP(Ptr, Builder.getInt32(1 - VF));
|
Value *PartPtr = Builder.CreateGEP(Ptr, Builder.getInt32(Part * VF));
|
||||||
|
|
||||||
Ptr = Builder.CreateBitCast(Ptr, StTy->getPointerTo());
|
if (Reverse) {
|
||||||
Value *Val = getVectorValue(SI->getValueOperand());
|
// If we store to reverse consecutive memory locations then we need
|
||||||
if (Reverse)
|
// to reverse the order of elements in the stored value.
|
||||||
Val = reverseVector(Val);
|
StoredVal[Part] = reverseVector(StoredVal[Part]);
|
||||||
Builder.CreateStore(Val, Ptr)->setAlignment(Alignment);
|
// If the address is consecutive but reversed, then the
|
||||||
|
// wide store needs to start at the last vector element.
|
||||||
|
PartPtr = Builder.CreateGEP(Ptr, Builder.getInt32(-Part * VF));
|
||||||
|
PartPtr = Builder.CreateGEP(PartPtr, Builder.getInt32(1 - VF));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value *VecPtr = Builder.CreateBitCast(PartPtr, StTy->getPointerTo());
|
||||||
|
Builder.CreateStore(StoredVal[Part], VecPtr)->setAlignment(Alignment);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Instruction::Load: {
|
case Instruction::Load: {
|
||||||
@ -1224,7 +1319,10 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
// The last index does not have to be the induction. It can be
|
// The last index does not have to be the induction. It can be
|
||||||
// consecutive and be a function of the index. For example A[I+1];
|
// consecutive and be a function of the index. For example A[I+1];
|
||||||
unsigned NumOperands = Gep->getNumOperands();
|
unsigned NumOperands = Gep->getNumOperands();
|
||||||
Value *LastIndex = getVectorValue(Gep->getOperand(NumOperands -1));
|
|
||||||
|
Value *LastGepOperand = Gep->getOperand(NumOperands - 1);
|
||||||
|
VectorParts &GEPParts = getVectorValue(LastGepOperand);
|
||||||
|
Value *LastIndex = GEPParts[0];
|
||||||
LastIndex = Builder.CreateExtractElement(LastIndex, Zero);
|
LastIndex = Builder.CreateExtractElement(LastIndex, Zero);
|
||||||
|
|
||||||
// Create the new GEP with the new induction variable.
|
// Create the new GEP with the new induction variable.
|
||||||
@ -1234,19 +1332,26 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
} else {
|
} else {
|
||||||
// Use the induction element ptr.
|
// Use the induction element ptr.
|
||||||
assert(isa<PHINode>(Ptr) && "Invalid induction ptr");
|
assert(isa<PHINode>(Ptr) && "Invalid induction ptr");
|
||||||
Ptr = Builder.CreateExtractElement(getVectorValue(Ptr), Zero);
|
VectorParts &PtrVal = getVectorValue(Ptr);
|
||||||
|
Ptr = Builder.CreateExtractElement(PtrVal[0], Zero);
|
||||||
}
|
}
|
||||||
// If the address is consecutive but reversed, then the
|
|
||||||
// wide load needs to start at the last vector element.
|
|
||||||
if (Reverse)
|
|
||||||
Ptr = Builder.CreateGEP(Ptr, Builder.getInt32(1 - VF));
|
|
||||||
|
|
||||||
Ptr = Builder.CreateBitCast(Ptr, RetTy->getPointerTo());
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
LI = Builder.CreateLoad(Ptr);
|
// Calculate the pointer for the specific unroll-part.
|
||||||
LI->setAlignment(Alignment);
|
Value *PartPtr = Builder.CreateGEP(Ptr, Builder.getInt32(Part * VF));
|
||||||
|
|
||||||
// Use this vector value for all users of the load.
|
if (Reverse) {
|
||||||
WidenMap[it] = Reverse ? reverseVector(LI) : LI;
|
// If the address is consecutive but reversed, then the
|
||||||
|
// wide store needs to start at the last vector element.
|
||||||
|
PartPtr = Builder.CreateGEP(Ptr, Builder.getInt32(-Part * VF));
|
||||||
|
PartPtr = Builder.CreateGEP(PartPtr, Builder.getInt32(1 - VF));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value *VecPtr = Builder.CreateBitCast(PartPtr, RetTy->getPointerTo());
|
||||||
|
Value *LI = Builder.CreateLoad(VecPtr, "wide.load");
|
||||||
|
cast<LoadInst>(LI)->setAlignment(Alignment);
|
||||||
|
Entry[Part] = Reverse ? reverseVector(LI) : LI;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Instruction::ZExt:
|
case Instruction::ZExt:
|
||||||
@ -1271,13 +1376,16 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
Value *ScalarCast = Builder.CreateCast(CI->getOpcode(), Induction,
|
Value *ScalarCast = Builder.CreateCast(CI->getOpcode(), Induction,
|
||||||
CI->getType());
|
CI->getType());
|
||||||
Value *Broadcasted = getBroadcastInstrs(ScalarCast);
|
Value *Broadcasted = getBroadcastInstrs(ScalarCast);
|
||||||
WidenMap[it] = getConsecutiveVector(Broadcasted);
|
for (unsigned Part = 0; Part < UF; ++Part)
|
||||||
|
Entry[Part] = getConsecutiveVector(Broadcasted, VF * Part, false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/// Vectorize casts.
|
/// Vectorize casts.
|
||||||
Value *A = getVectorValue(it->getOperand(0));
|
|
||||||
Type *DestTy = VectorType::get(CI->getType()->getScalarType(), VF);
|
Type *DestTy = VectorType::get(CI->getType()->getScalarType(), VF);
|
||||||
WidenMap[it] = Builder.CreateCast(CI->getOpcode(), A, DestTy);
|
|
||||||
|
VectorParts &A = getVectorValue(it->getOperand(0));
|
||||||
|
for (unsigned Part = 0; Part < UF; ++Part)
|
||||||
|
Entry[Part] = Builder.CreateCast(CI->getOpcode(), A[Part], DestTy);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1286,12 +1394,16 @@ InnerLoopVectorizer::vectorizeBlockInLoop(LoopVectorizationLegality *Legal,
|
|||||||
Module *M = BB->getParent()->getParent();
|
Module *M = BB->getParent()->getParent();
|
||||||
IntrinsicInst *II = cast<IntrinsicInst>(it);
|
IntrinsicInst *II = cast<IntrinsicInst>(it);
|
||||||
Intrinsic::ID ID = II->getIntrinsicID();
|
Intrinsic::ID ID = II->getIntrinsicID();
|
||||||
SmallVector<Value*, 4> Args;
|
for (unsigned Part = 0; Part < UF; ++Part) {
|
||||||
for (unsigned i = 0, ie = II->getNumArgOperands(); i != ie; ++i)
|
SmallVector<Value*, 4> Args;
|
||||||
Args.push_back(getVectorValue(II->getArgOperand(i)));
|
for (unsigned i = 0, ie = II->getNumArgOperands(); i != ie; ++i) {
|
||||||
Type *Tys[] = { VectorType::get(II->getType()->getScalarType(), VF) };
|
VectorParts &Arg = getVectorValue(II->getArgOperand(i));
|
||||||
Function *F = Intrinsic::getDeclaration(M, ID, Tys);
|
Args.push_back(Arg[Part]);
|
||||||
WidenMap[it] = Builder.CreateCall(F, Args);
|
}
|
||||||
|
Type *Tys[] = { VectorType::get(II->getType()->getScalarType(), VF) };
|
||||||
|
Function *F = Intrinsic::getDeclaration(M, ID, Tys);
|
||||||
|
Entry[Part] = Builder.CreateCall(F, Args);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,6 +54,8 @@
|
|||||||
#include "llvm/Analysis/ScalarEvolution.h"
|
#include "llvm/Analysis/ScalarEvolution.h"
|
||||||
#include "llvm/IR/IRBuilder.h"
|
#include "llvm/IR/IRBuilder.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
/// We don't vectorize loops with a known constant trip count below this number.
|
/// We don't vectorize loops with a known constant trip count below this number.
|
||||||
@ -91,9 +93,11 @@ class InnerLoopVectorizer {
|
|||||||
public:
|
public:
|
||||||
/// Ctor.
|
/// Ctor.
|
||||||
InnerLoopVectorizer(Loop *Orig, ScalarEvolution *Se, LoopInfo *Li,
|
InnerLoopVectorizer(Loop *Orig, ScalarEvolution *Se, LoopInfo *Li,
|
||||||
DominatorTree *Dt, DataLayout *Dl, unsigned VecWidth):
|
DominatorTree *Dt, DataLayout *Dl,
|
||||||
|
unsigned VecWidth, unsigned UnrollFactor):
|
||||||
OrigLoop(Orig), SE(Se), LI(Li), DT(Dt), DL(Dl), VF(VecWidth),
|
OrigLoop(Orig), SE(Se), LI(Li), DT(Dt), DL(Dl), VF(VecWidth),
|
||||||
Builder(Se->getContext()), Induction(0), OldInduction(0) { }
|
UF(UnrollFactor), Builder(Se->getContext()), Induction(0), OldInduction(0),
|
||||||
|
WidenMap(UnrollFactor) { }
|
||||||
|
|
||||||
// Perform the actual loop widening (vectorization).
|
// Perform the actual loop widening (vectorization).
|
||||||
void vectorize(LoopVectorizationLegality *Legal) {
|
void vectorize(LoopVectorizationLegality *Legal) {
|
||||||
@ -109,6 +113,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
/// A small list of PHINodes.
|
/// A small list of PHINodes.
|
||||||
typedef SmallVector<PHINode*, 4> PhiVector;
|
typedef SmallVector<PHINode*, 4> PhiVector;
|
||||||
|
/// When we unroll loops we have multiple vector values for each scalar.
|
||||||
|
/// This data structure holds the unrolled and vectorized values that
|
||||||
|
/// originated from one scalar instruction.
|
||||||
|
typedef SmallVector<Value*, 2> VectorParts;
|
||||||
|
|
||||||
/// Add code that checks at runtime if the accessed arrays overlap.
|
/// Add code that checks at runtime if the accessed arrays overlap.
|
||||||
/// Returns the comparator value or NULL if no check is needed.
|
/// Returns the comparator value or NULL if no check is needed.
|
||||||
@ -122,10 +130,10 @@ private:
|
|||||||
/// A helper function that computes the predicate of the block BB, assuming
|
/// A helper function that computes the predicate of the block BB, assuming
|
||||||
/// that the header block of the loop is set to True. It returns the *entry*
|
/// that the header block of the loop is set to True. It returns the *entry*
|
||||||
/// mask for the block BB.
|
/// mask for the block BB.
|
||||||
Value *createBlockInMask(BasicBlock *BB);
|
VectorParts createBlockInMask(BasicBlock *BB);
|
||||||
/// A helper function that computes the predicate of the edge between SRC
|
/// A helper function that computes the predicate of the edge between SRC
|
||||||
/// and DST.
|
/// and DST.
|
||||||
Value *createEdgeMask(BasicBlock *Src, BasicBlock *Dst);
|
VectorParts createEdgeMask(BasicBlock *Src, BasicBlock *Dst);
|
||||||
|
|
||||||
/// A helper function to vectorize a single BB within the innermost loop.
|
/// A helper function to vectorize a single BB within the innermost loop.
|
||||||
void vectorizeBlockInLoop(LoopVectorizationLegality *Legal, BasicBlock *BB,
|
void vectorizeBlockInLoop(LoopVectorizationLegality *Legal, BasicBlock *BB,
|
||||||
@ -148,14 +156,15 @@ private:
|
|||||||
|
|
||||||
/// This function adds 0, 1, 2 ... to each vector element, starting at zero.
|
/// This function adds 0, 1, 2 ... to each vector element, starting at zero.
|
||||||
/// If Negate is set then negative numbers are added e.g. (0, -1, -2, ...).
|
/// If Negate is set then negative numbers are added e.g. (0, -1, -2, ...).
|
||||||
Value *getConsecutiveVector(Value* Val, bool Negate = false);
|
/// The sequence starts at StartIndex.
|
||||||
|
Value *getConsecutiveVector(Value* Val, unsigned StartIdx, bool Negate);
|
||||||
|
|
||||||
/// When we go over instructions in the basic block we rely on previous
|
/// When we go over instructions in the basic block we rely on previous
|
||||||
/// values within the current basic block or on loop invariant values.
|
/// values within the current basic block or on loop invariant values.
|
||||||
/// When we widen (vectorize) values we place them in the map. If the values
|
/// When we widen (vectorize) values we place them in the map. If the values
|
||||||
/// are not within the map, they have to be loop invariant, so we simply
|
/// are not within the map, they have to be loop invariant, so we simply
|
||||||
/// broadcast them into a vector.
|
/// broadcast them into a vector.
|
||||||
Value *getVectorValue(Value *V);
|
VectorParts &getVectorValue(Value *V);
|
||||||
|
|
||||||
/// Get a uniform vector of constant integers. We use this to get
|
/// Get a uniform vector of constant integers. We use this to get
|
||||||
/// vectors of ones and zeros for the reduction code.
|
/// vectors of ones and zeros for the reduction code.
|
||||||
@ -164,22 +173,61 @@ private:
|
|||||||
/// Generate a shuffle sequence that will reverse the vector Vec.
|
/// Generate a shuffle sequence that will reverse the vector Vec.
|
||||||
Value *reverseVector(Value *Vec);
|
Value *reverseVector(Value *Vec);
|
||||||
|
|
||||||
typedef DenseMap<Value*, Value*> ValueMap;
|
/// This is a helper class that holds the vectorizer state. It maps scalar
|
||||||
|
/// instructions to vector instructions. When the code is 'unrolled' then
|
||||||
|
/// then a single scalar value is mapped to multiple vector parts. The parts
|
||||||
|
/// are stored in the VectorPart type.
|
||||||
|
struct ValueMap {
|
||||||
|
/// C'tor. UnrollFactor controls the number of vectors ('parts') that
|
||||||
|
/// are mapped.
|
||||||
|
ValueMap(unsigned UnrollFactor) : UF(UnrollFactor) {}
|
||||||
|
|
||||||
|
/// \return True if 'Key' is saved in the Value Map.
|
||||||
|
bool has(Value *Key) { return MapStoreage.count(Key); }
|
||||||
|
|
||||||
|
/// Initializes a new entry in the map. Sets all of the vector parts to the
|
||||||
|
/// save value in 'Val'.
|
||||||
|
/// \return A reference to a vector with splat values.
|
||||||
|
VectorParts &splat(Value *Key, Value *Val) {
|
||||||
|
MapStoreage[Key].clear();
|
||||||
|
MapStoreage[Key].append(UF, Val);
|
||||||
|
return MapStoreage[Key];
|
||||||
|
}
|
||||||
|
|
||||||
|
///\return A reference to the value that is stored at 'Key'.
|
||||||
|
VectorParts &get(Value *Key) {
|
||||||
|
if (!has(Key))
|
||||||
|
MapStoreage[Key].resize(UF);
|
||||||
|
return MapStoreage[Key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The unroll factor. Each entry in the map stores this number of vector
|
||||||
|
/// elements.
|
||||||
|
unsigned UF;
|
||||||
|
|
||||||
|
/// Map storage. We use std::map and not DenseMap because insertions to a
|
||||||
|
/// dense map invalidates its iterators.
|
||||||
|
std::map<Value*, VectorParts> MapStoreage;
|
||||||
|
};
|
||||||
|
|
||||||
/// The original loop.
|
/// The original loop.
|
||||||
Loop *OrigLoop;
|
Loop *OrigLoop;
|
||||||
// Scev analysis to use.
|
/// Scev analysis to use.
|
||||||
ScalarEvolution *SE;
|
ScalarEvolution *SE;
|
||||||
// Loop Info.
|
/// Loop Info.
|
||||||
LoopInfo *LI;
|
LoopInfo *LI;
|
||||||
// Dominator Tree.
|
/// Dominator Tree.
|
||||||
DominatorTree *DT;
|
DominatorTree *DT;
|
||||||
// Data Layout.
|
/// Data Layout.
|
||||||
DataLayout *DL;
|
DataLayout *DL;
|
||||||
// The vectorization factor to use.
|
/// The vectorization SIMD factor to use. Each vector will have this many
|
||||||
|
/// vector elements.
|
||||||
unsigned VF;
|
unsigned VF;
|
||||||
|
/// The vectorization unroll factor to use. Each scalar is vectorized to this
|
||||||
|
/// many different vector instructions.
|
||||||
|
unsigned UF;
|
||||||
|
|
||||||
// The builder that we use
|
/// The builder that we use
|
||||||
IRBuilder<> Builder;
|
IRBuilder<> Builder;
|
||||||
|
|
||||||
// --- Vectorization state ---
|
// --- Vectorization state ---
|
||||||
@ -203,7 +251,7 @@ private:
|
|||||||
PHINode *Induction;
|
PHINode *Induction;
|
||||||
/// The induction variable of the old basic block.
|
/// The induction variable of the old basic block.
|
||||||
PHINode *OldInduction;
|
PHINode *OldInduction;
|
||||||
// Maps scalars to widened vectors.
|
/// Maps scalars to widened vectors.
|
||||||
ValueMap WidenMap;
|
ValueMap WidenMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user