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!");
95 Context &Ctx = Ty->getContext();
105SameSizeReinterpret::getCastChain(Type *SrcTy, Type *DestTy,
106 const DataLayout &
DL) {
108 "Cannot reinterpret a non-integral pointer!");
110 "Expected types of the same size!");
115 if (Ty->getScalarType()->isPointerTy()) {
116 Ty = getIntTypeFor(Ty,
DL);
117 Chain.emplace_back(Instruction::Opcode::PtrToInt, Ty);
119 bool DestIsPtr = DestTy->getScalarType()->isPointerTy();
120 Type *BitCastTy = DestIsPtr ? getIntTypeFor(DestTy,
DL) : DestTy;
122 Chain.emplace_back(Instruction::Opcode::BitCast, BitCastTy);
124 Chain.emplace_back(Instruction::Opcode::IntToPtr, DestTy);
127 assert(Chain.size() <= 3 &&
"Unexpected reinterpret cast chain!");
138 for (
auto [
Opc, Ty] : getCastChain(
V->getType(), DestTy,
DL))
148 for (
auto [
Opc, Ty] : getCastChain(
C->getType(), DestTy,
DL)) {
150 case Instruction::Opcode::PtrToInt:
153 case Instruction::Opcode::BitCast:
156 case Instruction::Opcode::IntToPtr:
169 auto *LaneTy = VecTy->getScalarType();
175 "Chain contains a constant which cannot be converted or split");
185 for (
Value *Elm : AggrCOp->operands())
189 ConstantElements.push_back(SeqCOp->getElementAsConstant(ElmIdx));
191 auto *ZeroElm =
Zero->getSequentialElement();
192 for ([[maybe_unused]]
auto Cnt :
194 ConstantElements.push_back(ZeroElm);
197 for ([[maybe_unused]]
auto Cnt :
201 ConstantElements.push_back(Elm);
204 for ([[maybe_unused]]
auto Cnt :
208 ConstantElements.push_back(Elm);
212 for ([[maybe_unused]]
auto Cnt :
216 ConstantElements.push_back(Elm);
221 ConstantElements.push_back(COp);
232 LaneTyConstants.
reserve(ConstantElements.size());
233 for (Constant *
C : ConstantElements) {
235 if (Bits == LaneBits) {
239 LaneTyConstants.push_back(Lane);
240 }
else if (Bits > LaneBits) {
249 Val = CI->getValue();
251 Val = CFP->getValue().bitcastToAPInt();
254 unsigned NumSlices =
Bits / LaneBits;
256 unsigned EndianAwareSlice =
257 DL->isLittleEndian() ? SliceIdx : NumSlices - 1 - SliceIdx;
261 *Ctx, Val.extractBits(LaneBits, EndianAwareSlice * LaneBits));
264 if (SliceC ==
nullptr)
266 LaneTyConstants.push_back(SliceC);
276 if (!canVectorize(Stores))
280 for (
auto *
I : Stores) {
293 if (LI->getParent() != BB)
295 if (LI->hasNUsesOrMore(2))
301 if (!AllLoads && !AllConstants)
307 Value *VecOp =
nullptr;
308 SmallVector<Instruction *, 8> Loads;
313 if (!canVectorize(Loads)) {
317 VecOp = createVectorLoad(Loads);
318 assert(VecOp !=
nullptr &&
"Expected vector value!");
319 }
else if (AllConstants) {
324 VecOp = createConstantVector(Constants);
325 if (VecOp ==
nullptr) {
338 DeadInstrMorgue.collectPotentiallyDeadInstrs(Stores);
340 DeadInstrMorgue.collectPotentiallyDeadInstrs<
Instruction>(Loads);
341 DeadInstrMorgue.tryEraseDeadInstrs();
343 return acceptOrRevert();
348 auto VecTy = canVectorize(Loads);
365 auto *
VecLoad = createVectorLoad(Loads);
366 assert(VecLoad !=
nullptr &&
"Expected vector load");
377 if (!
I->hasNUses(0)) {
381 I->replaceAllUsesWith(Unpacked);
386 DeadInstrMorgue.collectPotentiallyDeadInstrs(Loads);
387 DeadInstrMorgue.tryEraseDeadInstrs();
389 if (!acceptOrRevert())
398 Function &
F = *Bndl[0]->getParent()->getParent();
399 DL = &
F.getParent()->getDataLayout();
400 Ctx = &
F.getContext();
403 auto Opc = Bndl[0]->getOpcode();
406 "Expected a homogeneous seed slice!");
410 case Instruction::Opcode::Load:
413 case Instruction::Opcode::Store:
421 F.getParent()->getDataLayout(), A->getTTI(),
422 F.getContext(), IMaps, Dir);
430 case Instruction::Opcode::Load:
431 Changed = vectorizeLoads(Bndl, Rgn) !=
nullptr;
433 case Instruction::Opcode::Store:
434 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.
An instruction for reading from memory.
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.
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.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
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.
This struct is a compact representation of a valid (non-zero power of two) alignment.