59 #define DEBUG_TYPE "interleaved-access"
62 "lower-interleaved-accesses",
63 cl::desc(
"Enable lowering interleaved accesses to intrinsics"),
72 InterleavedAccess() :
FunctionPass(
ID), DT(nullptr), TLI(nullptr) {
76 StringRef getPassName()
const override {
return "Interleaved Access Pass"; }
93 bool lowerInterleavedLoad(
LoadInst *LI,
111 "Lower interleaved memory accesses to target specific intrinsics",
false,
115 "Lower interleaved memory accesses to target specific
intrinsics",
false,
119 return new InterleavedAccess();
128 for (Index = 0; Index < Factor; Index++) {
133 for (; i < Mask.
size(); i++)
134 if (Mask[i] >= 0 && static_cast<unsigned>(Mask[i]) != Index + i * Factor)
137 if (i == Mask.
size())
150 unsigned &Index,
unsigned MaxFactor) {
155 for (Factor = 2; Factor <= MaxFactor; Factor++)
174 unsigned MaxFactor,
unsigned OpNumElts) {
175 unsigned NumElts = Mask.
size();
180 for (Factor = 2; Factor <= MaxFactor; Factor++) {
181 if (NumElts % Factor)
184 unsigned LaneLen = NumElts / Factor;
192 for (; I < Factor; I++) {
193 unsigned SavedLaneValue;
194 unsigned SavedNoUndefs = 0;
197 for (J = 0; J < LaneLen - 1; J++) {
199 unsigned Lane = J * Factor +
I;
200 unsigned NextLane = Lane + Factor;
201 int LaneValue = Mask[Lane];
202 int NextLaneValue = Mask[NextLane];
205 if (LaneValue >= 0 && NextLaneValue >= 0 &&
206 LaneValue + 1 != NextLaneValue)
210 if (LaneValue >= 0 && NextLaneValue < 0) {
211 SavedLaneValue = LaneValue;
220 if (SavedNoUndefs > 0 && LaneValue < 0) {
222 if (NextLaneValue >= 0 &&
223 SavedLaneValue + SavedNoUndefs != (
unsigned)NextLaneValue)
235 }
else if (Mask[(LaneLen - 1) * Factor + I] >= 0) {
237 StartMask = Mask[(LaneLen - 1) * Factor + I] - J;
238 }
else if (SavedNoUndefs > 0) {
240 StartMask = SavedLaneValue - (LaneLen - 1 - SavedNoUndefs);
247 if (StartMask + LaneLen > OpNumElts*2)
259 bool InterleavedAccess::lowerInterleavedLoad(
273 if (Extract && isa<ConstantInt>(Extract->getIndexOperand())) {
278 if (!SVI || !isa<UndefValue>(SVI->
getOperand(1)))
284 if (Shuffles.
empty())
287 unsigned Factor, Index;
298 Type *VecTy = Shuffles[0]->getType();
302 for (
unsigned i = 1; i < Shuffles.
size(); i++) {
303 if (Shuffles[i]->
getType() != VecTy)
315 if (!tryReplaceExtracts(Extracts, Shuffles))
318 DEBUG(
dbgs() <<
"IA: Found an interleaved load: " << *LI <<
"\n");
321 if (!TLI->lowerInterleavedLoad(LI, Shuffles, Indices, Factor))
324 for (
auto SVI : Shuffles)
331 bool InterleavedAccess::tryReplaceExtracts(
337 if (Extracts.
empty())
344 for (
auto *Extract : Extracts) {
347 auto *IndexOperand = cast<ConstantInt>(Extract->getIndexOperand());
348 auto Index = IndexOperand->getSExtValue();
353 for (
auto *Shuffle : Shuffles) {
357 if (!DT->dominates(Shuffle, Extract))
364 Shuffle->getShuffleMask(Indices);
365 for (
unsigned I = 0;
I < Indices.
size(); ++
I)
366 if (Indices[
I] == Index) {
367 assert(Extract->getOperand(0) == Shuffle->getOperand(0) &&
368 "Vector operations do not match");
369 ReplacementMap[Extract] = std::make_pair(Shuffle,
I);
374 if (ReplacementMap.
count(Extract))
380 if (!ReplacementMap.
count(Extract))
386 for (
auto &Replacement : ReplacementMap) {
387 auto *Extract = Replacement.first;
388 auto *Vector = Replacement.second.first;
389 auto Index = Replacement.second.second;
390 Builder.SetInsertPoint(Extract);
391 Extract->replaceAllUsesWith(Builder.CreateExtractElement(Vector, Index));
392 Extract->eraseFromParent();
398 bool InterleavedAccess::lowerInterleavedStore(
413 DEBUG(
dbgs() <<
"IA: Found an interleaved store: " << *SI <<
"\n");
416 if (!TLI->lowerInterleavedStore(SI, SVI, Factor))
425 bool InterleavedAccess::runOnFunction(
Function &
F) {
426 auto *
TPC = getAnalysisIfAvailable<TargetPassConfig>();
432 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
434 TLI =
TM.getSubtargetImpl(F)->getTargetLowering();
435 MaxFactor = TLI->getMaxSupportedInterleaveFactor();
439 bool Changed =
false;
442 if (
LoadInst *LI = dyn_cast<LoadInst>(&
I))
443 Changed |= lowerInterleavedLoad(LI, DeadInsts);
446 Changed |= lowerInterleavedStore(SI, DeadInsts);
449 for (
auto I : DeadInsts)
450 I->eraseFromParent();
Value * getValueOperand()
void push_back(const T &Elt)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Lower interleaved memory accesses to target specific false
static cl::opt< bool > LowerInterleavedAccesses("lower-interleaved-accesses", cl::desc("Enable lowering interleaved accesses to intrinsics"), cl::init(true), cl::Hidden)
This instruction constructs a fixed permutation of two input vectors.
An instruction for reading from memory.
StringRef getName() const
Return a constant reference to the value's name.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
LLVM_NODISCARD bool empty() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
An instruction for storing to memory.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
size_t size() const
size - Get the array size.
static bool isReInterleaveMask(ArrayRef< int > Mask, unsigned &Factor, unsigned MaxFactor, unsigned OpNumElts)
Check if the mask can be used in an interleaved store.
initializer< Ty > init(const Ty &Val)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
The instances of the Type class are immutable: once they are created, they are never changed...
FunctionPass * createInterleavedAccessPass()
InterleavedAccess Pass - This pass identifies and matches interleaved memory accesses to target speci...
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
bool empty() const
empty - Check if the array is empty.
static wasm::ValType getType(const TargetRegisterClass *RC)
void initializeInterleavedAccessPass(PassRegistry &)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned getVectorNumElements() const
Lower interleaved memory accesses to target specific intrinsics
static bool isDeInterleaveMask(ArrayRef< int > Mask, unsigned &Factor, unsigned &Index, unsigned MaxFactor)
Check if the mask is a DE-interleave mask for an interleaved load.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
INITIALIZE_PASS_BEGIN(InterleavedAccess, DEBUG_TYPE,"Lower interleaved memory accesses to target specific intrinsics", false, false) INITIALIZE_PASS_END(InterleavedAccess
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
Legacy analysis pass which computes a DominatorTree.
static bool isDeInterleaveMaskOfFactor(ArrayRef< int > Mask, unsigned Factor, unsigned &Index)
Check if the mask is a DE-interleave mask of the given factor Factor like: <Index, Index+Factor, ..., Index+(NumElts-1)*Factor>
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
This file describes how to lower LLVM code to machine code.