22static cl::opt<unsigned>
24 cl::desc(
"Override the vector register size in bits, "
25 "which is otherwise found by querying TTI."));
28 cl::desc(
"Allow non-power-of-2 vectorization."));
39 for (
Value *BndlV : Bndl) {
40 auto *BndlI = cast<Instruction>(BndlV);
41 Operands.push_back(BndlI->getOperand(OpIdx));
49 auto *BotI = cast<Instruction>(
50 *std::max_element(Instrs.
begin(), Instrs.
end(), [](
auto *V1,
auto *V2) {
51 return cast<Instruction>(V1)->comesBefore(cast<Instruction>(V2));
54 return std::next(BotI->getIterator());
60 assert(
all_of(Bndl, [](
auto *V) {
return isa<Instruction>(V); }) &&
61 "Expect Instructions!");
62 auto &Ctx = Bndl[0]->getContext();
69 auto Opcode = cast<Instruction>(Bndl[0])->getOpcode();
71 case Instruction::Opcode::ZExt:
72 case Instruction::Opcode::SExt:
73 case Instruction::Opcode::FPToUI:
74 case Instruction::Opcode::FPToSI:
75 case Instruction::Opcode::FPExt:
76 case Instruction::Opcode::PtrToInt:
77 case Instruction::Opcode::IntToPtr:
78 case Instruction::Opcode::SIToFP:
79 case Instruction::Opcode::UIToFP:
80 case Instruction::Opcode::Trunc:
81 case Instruction::Opcode::FPTrunc:
82 case Instruction::Opcode::BitCast: {
86 case Instruction::Opcode::FCmp:
87 case Instruction::Opcode::ICmp: {
88 auto Pred = cast<CmpInst>(Bndl[0])->getPredicate();
91 return cast<CmpInst>(SBV)->getPredicate() == Pred;
93 "Expected same predicate across bundle.");
97 case Instruction::Opcode::Select: {
101 case Instruction::Opcode::FNeg: {
102 auto *UOp0 = cast<UnaryOperator>(Bndl[0]);
103 auto OpC = UOp0->getOpcode();
107 case Instruction::Opcode::Add:
108 case Instruction::Opcode::FAdd:
109 case Instruction::Opcode::Sub:
110 case Instruction::Opcode::FSub:
111 case Instruction::Opcode::Mul:
112 case Instruction::Opcode::FMul:
113 case Instruction::Opcode::UDiv:
114 case Instruction::Opcode::SDiv:
115 case Instruction::Opcode::FDiv:
116 case Instruction::Opcode::URem:
117 case Instruction::Opcode::SRem:
118 case Instruction::Opcode::FRem:
119 case Instruction::Opcode::Shl:
120 case Instruction::Opcode::LShr:
121 case Instruction::Opcode::AShr:
122 case Instruction::Opcode::And:
123 case Instruction::Opcode::Or:
124 case Instruction::Opcode::Xor: {
125 auto *BinOp0 = cast<BinaryOperator>(Bndl[0]);
129 BinOp0, WhereIt, Ctx,
"Vec");
131 case Instruction::Opcode::Load: {
132 auto *Ld0 = cast<LoadInst>(Bndl[0]);
133 Value *
Ptr = Ld0->getPointerOperand();
136 case Instruction::Opcode::Store: {
137 auto Align = cast<StoreInst>(Bndl[0])->getAlign();
142 case Instruction::Opcode::Br:
143 case Instruction::Opcode::Ret:
144 case Instruction::Opcode::PHI:
145 case Instruction::Opcode::AddrSpaceCast:
146 case Instruction::Opcode::Call:
147 case Instruction::Opcode::GetElementPtr:
158void BottomUpVec::tryEraseDeadInstrs() {
160 SmallVector<Instruction *> SortedDeadInstrCandidates(
161 DeadInstrCandidates.begin(), DeadInstrCandidates.end());
162 sort(SortedDeadInstrCandidates,
163 [](Instruction *I1, Instruction *I2) {
return I1->comesBefore(I2); });
164 for (Instruction *
I :
reverse(SortedDeadInstrCandidates)) {
166 I->eraseFromParent();
168 DeadInstrCandidates.clear();
171Value *BottomUpVec::createPack(ArrayRef<Value *> ToPack) {
181 Context &Ctx = ToPack[0]->getContext();
183 unsigned InsertIdx = 0;
184 for (Value *Elm : ToPack) {
187 if (Elm->getType()->isVectorTy()) {
189 cast<FixedVectorType>(Elm->getType())->getNumElements();
190 for (
auto ExtrLane : seq<int>(0, NumElms)) {
197 if (!isa<Constant>(ExtrI))
198 WhereIt = std::next(cast<Instruction>(ExtrI)->getIterator());
203 LastInsert, ExtrI, InsertLaneC, WhereIt, Ctx,
"VPack");
204 if (!isa<Constant>(InsertI)) {
205 LastInsert = InsertI;
206 WhereIt = std::next(cast<Instruction>(LastInsert)->getIterator());
215 WhereIt, Ctx,
"Pack");
216 if (
auto *NewI = dyn_cast<Instruction>(LastInsert))
217 WhereIt = std::next(NewI->getIterator());
223void BottomUpVec::collectPotentiallyDeadInstrs(ArrayRef<Value *> Bndl) {
224 for (Value *V : Bndl)
225 DeadInstrCandidates.insert(cast<Instruction>(V));
227 auto Opcode = cast<Instruction>(Bndl[0])->getOpcode();
229 case Instruction::Opcode::Load: {
233 DeadInstrCandidates.insert(
Ptr);
236 case Instruction::Opcode::Store: {
240 DeadInstrCandidates.insert(
Ptr);
248Value *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
unsigned Depth) {
249 Value *NewVec =
nullptr;
250 const auto &LegalityRes = Legality->canVectorize(Bndl);
251 switch (LegalityRes.getSubclassID()) {
253 auto *
I = cast<Instruction>(Bndl[0]);
254 SmallVector<Value *, 2> VecOperands;
255 switch (
I->getOpcode()) {
256 case Instruction::Opcode::Load:
260 case Instruction::Opcode::Store: {
263 VecOperands.push_back(VecOp);
269 for (
auto OpIdx : seq<unsigned>(
I->getNumOperands())) {
271 VecOperands.push_back(VecOp);
275 NewVec = createVectorInstr(Bndl, VecOperands);
279 if (NewVec !=
nullptr)
280 collectPotentiallyDeadInstrs(Bndl);
287 NewVec = createPack(Bndl);
294bool BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) {
295 DeadInstrCandidates.clear();
297 vectorizeRec(Bndl, 0);
298 tryEraseDeadInstrs();
303 Legality = std::make_unique<LegalityAnalysis>(
304 A.getAA(),
A.getScalarEvolution(),
F.getParent()->getDataLayout(),
307 const auto &
DL =
F.getParent()->getDataLayout();
308 unsigned VecRegBits =
318 for (
SeedBundle &Seeds : SC.getStoreSeeds()) {
321 Seeds[Seeds.getFirstUnusedElementIdx()])),
324 auto DivideBy2 = [](
unsigned Num) {
332 for (
unsigned SliceElms = std::min(VecRegBits / ElmBits,
333 Seeds.getNumUnusedBits() / ElmBits);
334 SliceElms >= 2u; SliceElms = DivideBy2(SliceElms)) {
339 for (
unsigned Offset = Seeds.getFirstUnusedElementIdx(),
350 if (SeedSlice.empty())
353 assert(SeedSlice.size() >= 2 &&
"Should have been rejected!");
361 Change |= tryVectorize(SeedSliceVals);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
mir Rename Register Operands
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
InstListType::iterator iterator
Instruction iterators...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
LLVM Value Representation.
static Value * createWithCopiedFlags(Instruction::Opcode Op, Value *LHS, Value *RHS, Value *CopyFrom, InsertPosition Pos, Context &Ctx, const Twine &Name="")
BottomUpVec(StringRef Pipeline)
bool runOnFunction(Function &F, const Analyses &A) final
\Returns true if it modifies F.
static Value * create(Type *DestTy, Opcode Op, Value *Operand, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static CmpInst * create(Predicate Pred, Value *S1, Value *S2, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
static Value * create(Value *Vec, Value *Idx, InsertPosition Pos, Context &Ctx, const Twine &Name="")
A pass that runs on a sandbox::Function.
static Value * create(Value *Vec, Value *NewElt, Value *Idx, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx, const Twine &Name="")
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of candidate Instructions for vectorizing together.
static Value * create(Value *Cond, Value *True, Value *False, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx)
static Type * getInt32Ty(Context &Ctx)
static Value * createWithCopiedFlags(Instruction::Opcode Op, Value *OpV, Value *CopyFrom, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static unsigned getNumBits(Type *Ty, const DataLayout &DL)
\Returns the number of bits of Ty.
static Type * getExpectedType(const Value *V)
\Returns the expected type of Value V.
A SandboxIR Value has users. This is the base class.
static Type * getCommonScalarType(ArrayRef< Value * > Bndl)
Similar to tryGetCommonScalarType() but will assert that there is a common type.
static unsigned getNumLanes(Type *Ty)
\Returns the number of vector lanes of Ty or 1 if not a vector.
static Type * getWideType(Type *ElemTy, unsigned NumElts)
\Returns <NumElts x ElemTy>.
static Type * getElementType(Type *Ty)
Returns Ty if scalar or its element type if vector.
static unsigned getFloorPowerOf2(unsigned Num)
\Returns the first integer power of 2 that is <= Num.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
initializer< Ty > init(const Ty &Val)
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
@ Widen
ā€¨Collect scalar values.
static BasicBlock::iterator getInsertPointAfterInstrs(ArrayRef< Value * > Instrs)
static SmallVector< Value *, 4 > getOperand(ArrayRef< Value * > Bndl, unsigned OpIdx)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
static cl::opt< unsigned > OverrideVecRegBits("sbvec-vec-reg-bits", cl::init(0), cl::Hidden, cl::desc("Override the vector register size in bits, " "which is otherwise found by querying TTI."))
static cl::opt< bool > AllowNonPow2("sbvec-allow-non-pow2", cl::init(false), cl::Hidden, cl::desc("Allow non-power-of-2 vectorization."))