BoUpSLP::BoUpSLP(BasicBlock *Bb, ScalarEvolution *S, DataLayout *Dl,
TargetTransformInfo *Tti, AliasAnalysis *Aa, Loop *Lp) :
- BB(Bb), SE(S), DL(Dl), TTI(Tti), AA(Aa), L(Lp) {
+ Builder(S->getContext()), BB(Bb), SE(S), DL(Dl), TTI(Tti), AA(Aa), L(Lp) {
numberInstructions();
}
DEBUG(dbgs() << "SLP: Found cost=" << Cost << " for VF=" << VF << "\n");
if (Cost < CostThreshold) {
DEBUG(dbgs() << "SLP: Decided to vectorize cost=" << Cost << "\n");
+ Builder.SetInsertPoint(getInsertionPoint(getLastIndex(Operands,VF)));
vectorizeTree(Operands, VF);
i += VF - 1;
Changed = true;
}
bool BoUpSLP::vectorizeStores(ArrayRef<StoreInst *> Stores, int costThreshold) {
- ValueSet Heads, Tails;
+ SetVector<Value*> Heads, Tails;
SmallDenseMap<Value*, Value*> ConsecutiveChain;
// We may run into multiple chains that merge into a single chain. We mark the
}
// For stores that start but don't end a link in the chain:
- for (ValueSet::iterator it = Heads.begin(), e = Heads.end();it != e; ++it) {
+ for (SetVector<Value*>::iterator it = Heads.begin(), e = Heads.end();
+ it != e; ++it) {
if (Tails.count(*it)) continue;
// We found a store instr that starts a chain. Now follow the chain and try
}
void BoUpSLP::vectorizeArith(ArrayRef<Value *> Operands) {
+ int LastIdx = getLastIndex(Operands, Operands.size());
+ Instruction *Loc = getInsertionPoint(LastIdx);
+ Builder.SetInsertPoint(Loc);
+
+ assert(getFirstUserIndex(Operands, Operands.size()) > LastIdx &&
+ "Vectorizing with in-tree users");
+
Value *Vec = vectorizeTree(Operands, Operands.size());
- BasicBlock::iterator Loc = cast<Instruction>(Vec);
- IRBuilder<> Builder(++Loc);
// After vectorizing the operands we need to generate extractelement
// instructions and replace all of the uses of the scalar values with
// the values that we extracted from the vectorized tree.
MustExtract.clear();
// Find the location of the last root.
- unsigned LastRootIndex = InstrIdx[GetLastInstr(VL, VL.size())];
+ int LastRootIndex = getLastIndex(VL, VL.size());
+ int FirstUserIndex = getFirstUserIndex(VL, VL.size());
+
+ // Don't vectorize if there are users of the tree roots inside the tree
+ // itself.
+ if (LastRootIndex > FirstUserIndex)
+ return max_cost;
// Scan the tree and find which value is used by which lane, and which values
// must be scalarized.
// Check that instructions with multiple users can be vectorized. Mark unsafe
// instructions.
- for (ValueSet::iterator it = MultiUserVals.begin(),
+ for (SetVector<Value*>::iterator it = MultiUserVals.begin(),
e = MultiUserVals.end(); it != e; ++it) {
// Check that all of the users of this instr are within the tree
// and that they are all from the same lane.
// We don't have an ordering problem if the user is not in this basic
// block.
Instruction *Inst = cast<Instruction>(*I);
- if (Inst->getParent() == BB) {
- // We don't have an ordering problem if the user is after the last
- // root.
- unsigned Idx = InstrIdx[Inst];
- if (Idx < LastRootIndex) {
- MustScalarize.insert(*it);
- DEBUG(dbgs()<<"SLP: Adding to MustScalarize "
- "because of an unsafe out of tree usage.\n");
- break;
- }
+ if (Inst->getParent() != BB) {
+ MustExtract.insert(*it);
+ continue;
+ }
+
+ // We don't have an ordering problem if the user is after the last root.
+ int Idx = InstrIdx[Inst];
+ if (Idx < LastRootIndex) {
+ MustScalarize.insert(*it);
+ DEBUG(dbgs()<<"SLP: Adding to MustScalarize "
+ "because of an unsafe out of tree usage.\n");
+ break;
}
+
DEBUG(dbgs()<<"SLP: Adding to MustExtract "
"because of a safe out of tree usage.\n");
MustExtract.insert(*it);
if (!I || Opcode != I->getOpcode()) return;
}
- // Mark instructions with multiple users.
- for (unsigned i = 0, e = VL.size(); i < e; ++i) {
- Instruction *I = dyn_cast<Instruction>(VL[i]);
- // Remember to check if all of the users of this instr are vectorized
- // within our tree.
- if (I && I->getNumUses() > 1) MultiUserVals.insert(I);
- }
-
for (int i = 0, e = VL.size(); i < e; ++i) {
// Check that the instruction is only used within
// one lane.
LaneMap[VL[i]] = i;
}
+ // Mark instructions with multiple users.
+ for (unsigned i = 0, e = VL.size(); i < e; ++i) {
+ Instruction *I = dyn_cast<Instruction>(VL[i]);
+ // Remember to check if all of the users of this instr are vectorized
+ // within our tree. At depth zero we have no local users, only external
+ // users that we don't care about.
+ if (Depth && I && I->getNumUses() > 1) {
+ DEBUG(dbgs()<<"SLP: Adding to MultiUserVals "
+ "because it has multiple users:" << *I << " \n");
+ MultiUserVals.insert(I);
+ }
+ }
+
switch (Opcode) {
case Instruction::ZExt:
case Instruction::SExt:
// Check if it is safe to sink the loads or the stores.
if (Opcode == Instruction::Load || Opcode == Instruction::Store) {
- int MaxIdx = InstrIdx[VL0];
- for (unsigned i = 1, e = VL.size(); i < e; ++i )
- MaxIdx = std::max(MaxIdx, InstrIdx[VL[i]]);
-
+ int MaxIdx = getLastIndex(VL, VL.size());
Instruction *Last = InstrVec[MaxIdx];
+
for (unsigned i = 0, e = VL.size(); i < e; ++i ) {
if (VL[i] == Last) continue;
Value *Barrier = isUnsafeToSink(cast<Instruction>(VL[i]), Last);
}
}
-Instruction *BoUpSLP::GetLastInstr(ArrayRef<Value *> VL, unsigned VF) {
+int BoUpSLP::getLastIndex(ArrayRef<Value *> VL, unsigned VF) {
int MaxIdx = InstrIdx[BB->getFirstNonPHI()];
for (unsigned i = 0; i < VF; ++i )
MaxIdx = std::max(MaxIdx, InstrIdx[VL[i]]);
- return InstrVec[MaxIdx + 1];
+ return MaxIdx;
+}
+
+int BoUpSLP::getFirstUserIndex(ArrayRef<Value *> VL, unsigned VF) {
+ // Find the first user of the values.
+ int FirstUser = InstrVec.size();
+ for (unsigned i = 0; i < VF; ++i) {
+ for (Value::use_iterator U = VL[i]->use_begin(), UE = VL[i]->use_end();
+ U != UE; ++U) {
+ Instruction *Instr = dyn_cast<Instruction>(*U);
+ if (!Instr || Instr->getParent() != BB)
+ continue;
+
+ FirstUser = std::min(FirstUser, InstrIdx[Instr]);
+ }
+ }
+ return FirstUser;
+}
+
+int BoUpSLP::getLastIndex(Instruction *I, Instruction *J) {
+ assert(I->getParent() == BB && "Invalid parent for instruction I");
+ assert(J->getParent() == BB && "Invalid parent for instruction J");
+ return std::max(InstrIdx[I],InstrIdx[J]);
+}
+
+Instruction *BoUpSLP::getInsertionPoint(unsigned Index) {
+ return InstrVec[Index + 1];
}
Value *BoUpSLP::Scalarize(ArrayRef<Value *> VL, VectorType *Ty) {
- IRBuilder<> Builder(GetLastInstr(VL, Ty->getNumElements()));
Value *Vec = UndefValue::get(Ty);
for (unsigned i=0; i < Ty->getNumElements(); ++i) {
// Generate the 'InsertElement' instruction.
Value *BoUpSLP::vectorizeTree(ArrayRef<Value *> VL, int VF) {
Value *V = vectorizeTree_rec(VL, VF);
- Instruction *LastInstr = GetLastInstr(VL, VL.size());
- int LastInstrIdx = InstrIdx[LastInstr];
- IRBuilder<> Builder(LastInstr);
- for (ValueSet::iterator it = MustExtract.begin(), e = MustExtract.end();
- it != e; ++it) {
+ int LastInstrIdx = getLastIndex(VL, VL.size());
+ for (SetVector<Value*>::iterator it = MustExtract.begin(),
+ e = MustExtract.end(); it != e; ++it) {
Instruction *I = cast<Instruction>(*it);
+
+ // This is a scalarized value, so we can use the original value.
+ // No need to extract from the vector.
+ if (!LaneMap.count(I))
+ continue;
+
Value *Vec = VectorizedValues[I];
- assert(LaneMap.count(I) && "Unable to find the lane for the external use");
+ // We decided not to vectorize I because one of its users was not
+ // vectorizerd. This is okay.
+ if (!Vec)
+ continue;
+
Value *Idx = Builder.getInt32(LaneMap[I]);
Value *Extract = Builder.CreateExtractElement(Vec, Idx);
bool Replaced = false;
- for (Value::use_iterator U = I->use_begin(), UE = U->use_end(); U != UE;
+ for (Value::use_iterator U = I->use_begin(), UE = I->use_end(); U != UE;
++U) {
Instruction *UI = cast<Instruction>(*U);
if (UI->getParent() != I->getParent() || InstrIdx[UI] > LastInstrIdx)
}
// Check that this is a simple vector constant.
- if (AllConst || AllSameScalar) return Scalarize(VL, VecTy);
+ if (AllConst || AllSameScalar)
+ return Scalarize(VL, VecTy);
// Scalarize unknown structures.
Instruction *VL0 = dyn_cast<Instruction>(VL[0]);
- if (!VL0) return Scalarize(VL, VecTy);
+ if (!VL0)
+ return Scalarize(VL, VecTy);
- if (VectorizedValues.count(VL0)) return VectorizedValues[VL0];
+ if (VectorizedValues.count(VL0)) {
+ Value * Vec = VectorizedValues[VL0];
+ for (int i = 0; i < VF; ++i)
+ VectorizedValues[VL[i]] = Vec;
+ return Vec;
+ }
unsigned Opcode = VL0->getOpcode();
for (unsigned i = 0, e = VF; i < e; ++i) {
Instruction *I = dyn_cast<Instruction>(VL[i]);
// If not all of the instructions are identical then we have to scalarize.
- if (!I || Opcode != I->getOpcode()) return Scalarize(VL, VecTy);
+ if (!I || Opcode != I->getOpcode())
+ return Scalarize(VL, VecTy);
}
switch (Opcode) {
for (int i = 0; i < VF; ++i)
INVL.push_back(cast<Instruction>(VL[i])->getOperand(0));
Value *InVec = vectorizeTree_rec(INVL, VF);
- IRBuilder<> Builder(GetLastInstr(VL, VF));
CastInst *CI = dyn_cast<CastInst>(VL0);
Value *V = Builder.CreateCast(CI->getOpcode(), InVec, VecTy);
Value *LHS = vectorizeTree_rec(LHSVL, VF);
Value *RHS = vectorizeTree_rec(RHSVL, VF);
- IRBuilder<> Builder(GetLastInstr(VL, VF));
BinaryOperator *BinOp = cast<BinaryOperator>(VL0);
Value *V = Builder.CreateBinOp(BinOp->getOpcode(), LHS,RHS);
if (!isConsecutiveAccess(VL[i-1], VL[i]))
return Scalarize(VL, VecTy);
- IRBuilder<> Builder(GetLastInstr(VL, VF));
- Value *VecPtr = Builder.CreateBitCast(LI->getPointerOperand(),
- VecTy->getPointerTo());
- LI = Builder.CreateLoad(VecPtr);
+ // Loads are inserted at the head of the tree because we don't want to sink
+ // them all the way down past store instructions.
+ Instruction *Loc = getInsertionPoint(getLastIndex(VL, VL.size()));
+ IRBuilder<> LoadBuilder(Loc);
+ Value *VecPtr = LoadBuilder.CreateBitCast(LI->getPointerOperand(),
+ VecTy->getPointerTo());
+ LI = LoadBuilder.CreateLoad(VecPtr);
LI->setAlignment(Alignment);
for (int i = 0; i < VF; ++i)
ValueOp.push_back(cast<StoreInst>(VL[i])->getValueOperand());
Value *VecValue = vectorizeTree_rec(ValueOp, VF);
-
- IRBuilder<> Builder(GetLastInstr(VL, VF));
Value *VecPtr = Builder.CreateBitCast(SI->getPointerOperand(),
VecTy->getPointerTo());
Builder.CreateStore(VecValue, VecPtr)->setAlignment(Alignment);
#define LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/IR/IRBuilder.h"
#include <vector>
namespace llvm {
/// \returns the pointer to the barrier instruction if we can't sink.
Value *isUnsafeToSink(Instruction *Src, Instruction *Dst);
- /// \returns the instruction that appears last in the BB from \p VL.
+ /// \returns the index of the last instrucion in the BB from \p VL.
/// Only consider the first \p VF elements.
- Instruction *GetLastInstr(ArrayRef<Value *> VL, unsigned VF);
+ int getLastIndex(ArrayRef<Value *> VL, unsigned VF);
+
+ /// \returns the index of the first User of \p VL.
+ /// Only consider the first \p VF elements.
+ int getFirstUserIndex(ArrayRef<Value *> VL, unsigned VF);
+
+ /// \returns the instruction \p I or \p Jt hat appears last in the BB .
+ int getLastIndex(Instruction *I, Instruction *J);
+
+ /// \returns the insertion point for \p Index.
+ Instruction *getInsertionPoint(unsigned Index);
/// \returns a vector from a collection of scalars in \p VL.
Value *Scalarize(ArrayRef<Value *> VL, VectorType *Ty);
/// Contains values that have users outside of the vectorized graph.
/// We need to generate extract instructions for these values.
/// NOTICE: The vectorization methods also use this set.
- ValueSet MustExtract;
+ SetVector<Value*> MustExtract;
/// Contains a list of values that are used outside the current tree. This
/// set must be reset between runs.
- ValueSet MultiUserVals;
+ SetVector<Value*> MultiUserVals;
/// Maps values in the tree to the vector lanes that uses them. This map must
/// be reset between runs of getCost.
std::map<Value*, int> LaneMap;
/// A list of instructions to ignore while sinking
/// memory instructions. This map must be reset between runs of getCost.
- SmallPtrSet<Value *, 8> MemBarrierIgnoreList;
+ ValueSet MemBarrierIgnoreList;
// -- Containers that are used during vectorizeTree -- //
/// Iterating over this list is faster than calling LICM.
ValueList GatherInstructions;
+ /// Instruction builder to construct the vectorized tree.
+ IRBuilder<> Builder;
+
// Analysis and block reference.
BasicBlock *BB;
ScalarEvolution *SE;
--- /dev/null
+; RUN: opt < %s -basicaa -slp-vectorizer -dce -S -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.7.0"
+
+@.str = private unnamed_addr constant [6 x i8] c"bingo\00", align 1
+
+; We can't vectorize when the roots are used inside the tree.
+;CHECK: @in_tree_user
+;CHECK-NOT: load <2 x double>
+;CHECK: ret
+define void @in_tree_user(double* nocapture %A, i32 %n) {
+entry:
+ %conv = sitofp i32 %n to double
+ br label %for.body
+
+for.body: ; preds = %for.inc, %entry
+ %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.inc ]
+ %0 = shl nsw i64 %indvars.iv, 1
+ %arrayidx = getelementptr inbounds double* %A, i64 %0
+ %1 = load double* %arrayidx, align 8
+ %mul1 = fmul double %conv, %1
+ %mul2 = fmul double %mul1, 7.000000e+00
+ %add = fadd double %mul2, 5.000000e+00
+ %BadValue = fadd double %add, %add ; <------------------ In tree user.
+ %2 = or i64 %0, 1
+ %arrayidx6 = getelementptr inbounds double* %A, i64 %2
+ %3 = load double* %arrayidx6, align 8
+ %mul8 = fmul double %conv, %3
+ %mul9 = fmul double %mul8, 4.000000e+00
+ %add10 = fadd double %mul9, 9.000000e+00
+ %cmp11 = fcmp ogt double %add, %add10
+ br i1 %cmp11, label %if.then, label %for.inc
+
+if.then: ; preds = %for.body
+ %call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str, i64 0, i64 0))
+ br label %for.inc
+
+for.inc: ; preds = %for.body, %if.then
+ %indvars.iv.next = add i64 %indvars.iv, 1
+ %lftr.wideiv = trunc i64 %indvars.iv.next to i32
+ %exitcond = icmp eq i32 %lftr.wideiv, 100
+ br i1 %exitcond, label %for.end, label %for.body
+
+for.end: ; preds = %for.inc
+ ret void
+}
+
+declare i32 @printf(i8* nocapture, ...)
+