26#define DEBUG_PREFIX_LOCAL DEBUG_PREFIX "LoadStoreVec: "
47void LoadStoreVec::saveIR(Region &R) {
50 CostBefore = SB.getAfterCost() - SB.getBeforeCost();
54bool LoadStoreVec::acceptOrRevert() {
59 <<
" (After=" << CostAfter <<
" Before=" << CostBefore
62 if (CostGain > Opts.sbvec_cost_threshold) {
74 assert(!
ElemTy->isVectorTy() &&
"Expected a scalar element type!");
78template <
typename TypeRangeT>
79bool LoadStoreVec::canUnpack(
FixedVectorType *FromTy, TypeRangeT ToTypes) {
81 return all_of(ToTypes, [
this, VecElemTy](
Type *ToTy) {
89void LoadStoreVec::unpackIfNeeded(
LoadInst *VecLoad,
99 if (!Orig->hasNUses(0)) {
102 Unpacked, Orig->getType(), WhereIt, *Ctx, *DL);
103 Orig->replaceAllUsesWith(Unpacked);
126 Context &Ctx = Ty->getContext();
136SameSizeReinterpret::getCastChain(Type *SrcTy, Type *DestTy,
137 const DataLayout &
DL) {
139 "Cannot reinterpret a non-integral pointer!");
141 "Expected types of the same size!");
146 if (Ty->getScalarType()->isPointerTy()) {
147 Ty = getIntTypeFor(Ty,
DL);
148 Chain.emplace_back(Instruction::Opcode::PtrToInt, Ty);
150 bool DestIsPtr = DestTy->getScalarType()->isPointerTy();
151 Type *BitCastTy = DestIsPtr ? getIntTypeFor(DestTy,
DL) : DestTy;
153 Chain.emplace_back(Instruction::Opcode::BitCast, BitCastTy);
155 Chain.emplace_back(Instruction::Opcode::IntToPtr, DestTy);
158 assert(Chain.size() <= 3 &&
"Unexpected reinterpret cast chain!");
169 for (
auto [
Opc, Ty] : getCastChain(
V->getType(), DestTy,
DL))
179 for (
auto [
Opc, Ty] : getCastChain(
C->getType(), DestTy,
DL)) {
181 case Instruction::Opcode::PtrToInt:
184 case Instruction::Opcode::BitCast:
187 case Instruction::Opcode::IntToPtr:
200 auto *LaneTy = VecTy->getScalarType();
206 "Chain contains a constant which cannot be converted or split");
216 for (
Value *Elm : AggrCOp->operands())
220 ConstantElements.push_back(SeqCOp->getElementAsConstant(ElmIdx));
222 auto *ZeroElm =
Zero->getSequentialElement();
223 for ([[maybe_unused]]
auto Cnt :
225 ConstantElements.push_back(ZeroElm);
228 for ([[maybe_unused]]
auto Cnt :
232 ConstantElements.push_back(Elm);
235 for ([[maybe_unused]]
auto Cnt :
239 ConstantElements.push_back(Elm);
243 for ([[maybe_unused]]
auto Cnt :
247 ConstantElements.push_back(Elm);
252 ConstantElements.push_back(COp);
263 LaneTyConstants.
reserve(ConstantElements.size());
264 for (Constant *
C : ConstantElements) {
266 if (Bits == LaneBits) {
270 LaneTyConstants.push_back(Lane);
271 }
else if (Bits > LaneBits) {
280 Val = CI->getValue();
282 Val = CFP->getValue().bitcastToAPInt();
285 unsigned NumSlices =
Bits / LaneBits;
287 unsigned EndianAwareSlice =
288 DL->isLittleEndian() ? SliceIdx : NumSlices - 1 - SliceIdx;
292 *Ctx, Val.extractBits(LaneBits, EndianAwareSlice * LaneBits));
295 if (SliceC ==
nullptr)
297 LaneTyConstants.push_back(SliceC);
307 if (!canVectorize(Stores))
311 for (
auto *
I : Stores) {
324 if (LI->getParent() != BB)
326 if (LI->hasNUsesOrMore(2))
332 if (!AllLoads && !AllConstants)
338 Value *VecOp =
nullptr;
339 SmallVector<Instruction *, 8> Loads;
344 if (!canVectorize(Loads)) {
348 VecOp = createVectorLoad(Loads);
349 assert(VecOp !=
nullptr &&
"Expected vector value!");
350 }
else if (AllConstants) {
355 VecOp = createConstantVector(Constants);
356 if (VecOp ==
nullptr) {
369 DeadInstrMorgue.collectPotentiallyDeadInstrs(Stores);
371 DeadInstrMorgue.collectPotentiallyDeadInstrs<
Instruction>(Loads);
372 DeadInstrMorgue.tryEraseDeadInstrs();
374 return acceptOrRevert();
379 auto VecTy = canVectorize(Loads);
391 auto *
VecLoad = createVectorLoad(Loads);
392 assert(VecLoad !=
nullptr &&
"Expected vector load");
393 unpackIfNeeded(VecLoad, Loads);
395 DeadInstrMorgue.collectPotentiallyDeadInstrs(Loads);
396 DeadInstrMorgue.tryEraseDeadInstrs();
398 if (!acceptOrRevert())
407 Function &
F = *Bndl[0]->getParent()->getParent();
408 DL = &
F.getParent()->getDataLayout();
409 Ctx = &
F.getContext();
412 auto Opc = Bndl[0]->getOpcode();
415 "Expected a homogeneous seed slice!");
419 case Instruction::Opcode::Load:
422 case Instruction::Opcode::Store:
430 F.getParent()->getDataLayout(), A->getTTI(),
431 F.getContext(), IMaps, Dir);
439 case Instruction::Opcode::Load:
440 Changed = vectorizeLoads(Bndl, Rgn) !=
nullptr;
442 case Instruction::Opcode::Store:
443 Changed = vectorizeStores(Bndl, Rgn);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
static constexpr Value * getValue(Ty &ValueOrUse)
#define DEBUG_PREFIX_LOCAL
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
InstListType::iterator iterator
Instruction iterators...
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
Class to represent fixed width SIMD vectors.
void reserve(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
Type * getElementType() const
An ArrayRef of Values or Instructions that we can print/dump for debugging.
static LLVM_ABI Value * create(Type *DestTy, Opcode Op, Value *Operand, InsertPosition Pos, Context &Ctx, const Twine &Name="")
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty)
static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty)
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty)
static LLVM_ABI Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
static LLVM_ABI Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
void save()
Convenience function for getTracker().save()
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Maps the original instructions to the vectorized instrs and the reverse.
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
static LLVM_ABI IntegerType * get(Context &C, unsigned NumBits)
Performs the legality analysis and returns a LegalityResult object.
LLVM_ABI const LegalityResult & canVectorizeLSV(BndlRef< Instruction * > Bndl)
Similar to canVectorize() but specific to LoadStoreVec.
static LLVM_ABI LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx, const Twine &Name="")
bool runOnRegion(Region &Rgn, const Analyses &A) final
\Returns true if it modifies R.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Context & getContext() const
static LLVM_ABI Constant * reinterpretConstant(Constant *C, Type *DestTy, const DataLayout &DL)
Reinterprets constant C as DestTy, e.g., float 1.0 to i32 0x3f800000, or a null pointer to i64 0.
static LLVM_ABI Value * reinterpretValue(Value *V, Type *DestTy, BasicBlock::iterator WhereIt, Context &Ctx, const DataLayout &DL)
Reinterprets V as DestTy, which must have the same size in DL, inserting casts before WhereIt.
static LLVM_ABI bool canReinterpret(Type *SrcTy, Type *DestTy, const DataLayout &DL)
static LLVM_ABI StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx)
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
bool isPointerTy() const
True if this is an instance of PointerType.
static void propagateMetadata(Instruction *I, const ValueRangeT &VL)
static unsigned getNumBits(Type *Ty, const DataLayout &DL)
\Returns the number of bits of Ty.
A SandboxIR Value has users. This is the base class.
static Instruction * getLowest(ArrayRef< Instruction * > Instrs)
\Returns the instruction in Instrs that is lowest in the BB.
static unsigned getNumLanes(Type *Ty)
\Returns the number of vector lanes of Ty or 1 if not a vector.
static Value * unpack(Value *FromVec, Type *ExtrTy, unsigned Lane, BasicBlock::iterator WhereIt)
Emits the necessary instruction sequence to extract element of type ExtrTy at Lane from FromVec.
static LLVM_DUMP_METHOD void dump(ArrayRef< Value * > Bndl)
Helper dump function for debugging.
static Align getVecAlignment(AlignRangeT AlignRange, TypeRangeT TypeRange)
Given the alignments of the instructions before vectorization, this returns the alignment of the vect...
static Type * getCombinedVectorTypeFor(BndlRef< T * > Bndl, const DataLayout &DL)
\Returns the combined vector type for Bndl, even when the element types differ.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
BndlRef(const T &OneElt) -> BndlRef< T >
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
static bool isNonIntegralPtr(Type *Ty, const DataLayout &DL)
static Type * getWidenedType(Type *ElemTy, unsigned N)
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto map_range(ContainerTy &&C, FuncTy F)
Return a range that applies F to the elements of C.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
const VectorizeOptions & getVectorizeOptions(const LLVMContext &C)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.