InterleavedAccessPass.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //=----------------------- InterleavedAccessPass.cpp -----------------------==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Interleaved Access pass, which identifies
  11. // interleaved memory accesses and transforms into target specific intrinsics.
  12. //
  13. // An interleaved load reads data from memory into several vectors, with
  14. // DE-interleaving the data on a factor. An interleaved store writes several
  15. // vectors to memory with RE-interleaving the data on a factor.
  16. //
  17. // As interleaved accesses are hard to be identified in CodeGen (mainly because
  18. // the VECTOR_SHUFFLE DAG node is quite different from the shufflevector IR),
  19. // we identify and transform them to intrinsics in this pass. So the intrinsics
  20. // can be easily matched into target specific instructions later in CodeGen.
  21. //
  22. // E.g. An interleaved load (Factor = 2):
  23. // %wide.vec = load <8 x i32>, <8 x i32>* %ptr
  24. // %v0 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <0, 2, 4, 6>
  25. // %v1 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <1, 3, 5, 7>
  26. //
  27. // It could be transformed into a ld2 intrinsic in AArch64 backend or a vld2
  28. // intrinsic in ARM backend.
  29. //
  30. // E.g. An interleaved store (Factor = 3):
  31. // %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
  32. // <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
  33. // store <12 x i32> %i.vec, <12 x i32>* %ptr
  34. //
  35. // It could be transformed into a st3 intrinsic in AArch64 backend or a vst3
  36. // intrinsic in ARM backend.
  37. //
  38. //===----------------------------------------------------------------------===//
  39. #include "llvm/CodeGen/Passes.h"
  40. #include "llvm/IR/InstIterator.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/MathExtras.h"
  43. #include "llvm/Target/TargetLowering.h"
  44. #include "llvm/Target/TargetSubtargetInfo.h"
  45. using namespace llvm;
  46. #define DEBUG_TYPE "interleaved-access"
  47. static cl::opt<bool> LowerInterleavedAccesses(
  48. "lower-interleaved-accesses",
  49. cl::desc("Enable lowering interleaved accesses to intrinsics"),
  50. cl::init(false), cl::Hidden);
  51. static unsigned MaxFactor; // The maximum supported interleave factor.
  52. namespace llvm {
  53. static void initializeInterleavedAccessPass(PassRegistry &);
  54. }
  55. namespace {
  56. class InterleavedAccess : public FunctionPass {
  57. public:
  58. static char ID;
  59. InterleavedAccess(const TargetMachine *TM = nullptr)
  60. : FunctionPass(ID), TM(TM), TLI(nullptr) {
  61. initializeInterleavedAccessPass(*PassRegistry::getPassRegistry());
  62. }
  63. const char *getPassName() const override { return "Interleaved Access Pass"; }
  64. bool runOnFunction(Function &F) override;
  65. private:
  66. const TargetMachine *TM;
  67. const TargetLowering *TLI;
  68. /// \brief Transform an interleaved load into target specific intrinsics.
  69. bool lowerInterleavedLoad(LoadInst *LI,
  70. SmallVector<Instruction *, 32> &DeadInsts);
  71. /// \brief Transform an interleaved store into target specific intrinsics.
  72. bool lowerInterleavedStore(StoreInst *SI,
  73. SmallVector<Instruction *, 32> &DeadInsts);
  74. };
  75. } // end anonymous namespace.
  76. char InterleavedAccess::ID = 0;
  77. INITIALIZE_TM_PASS(InterleavedAccess, "interleaved-access",
  78. "Lower interleaved memory accesses to target specific intrinsics",
  79. false, false)
  80. FunctionPass *llvm::createInterleavedAccessPass(const TargetMachine *TM) {
  81. return new InterleavedAccess(TM);
  82. }
  83. /// \brief Check if the mask is a DE-interleave mask of the given factor
  84. /// \p Factor like:
  85. /// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor>
  86. static bool isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, unsigned Factor,
  87. unsigned &Index) {
  88. // Check all potential start indices from 0 to (Factor - 1).
  89. for (Index = 0; Index < Factor; Index++) {
  90. unsigned i = 0;
  91. // Check that elements are in ascending order by Factor. Ignore undef
  92. // elements.
  93. for (; i < Mask.size(); i++)
  94. if (Mask[i] >= 0 && static_cast<unsigned>(Mask[i]) != Index + i * Factor)
  95. break;
  96. if (i == Mask.size())
  97. return true;
  98. }
  99. return false;
  100. }
  101. /// \brief Check if the mask is a DE-interleave mask for an interleaved load.
  102. ///
  103. /// E.g. DE-interleave masks (Factor = 2) could be:
  104. /// <0, 2, 4, 6> (mask of index 0 to extract even elements)
  105. /// <1, 3, 5, 7> (mask of index 1 to extract odd elements)
  106. static bool isDeInterleaveMask(ArrayRef<int> Mask, unsigned &Factor,
  107. unsigned &Index) {
  108. if (Mask.size() < 2)
  109. return false;
  110. // Check potential Factors.
  111. for (Factor = 2; Factor <= MaxFactor; Factor++)
  112. if (isDeInterleaveMaskOfFactor(Mask, Factor, Index))
  113. return true;
  114. return false;
  115. }
  116. /// \brief Check if the mask is RE-interleave mask for an interleaved store.
  117. ///
  118. /// I.e. <0, NumSubElts, ... , NumSubElts*(Factor - 1), 1, NumSubElts + 1, ...>
  119. ///
  120. /// E.g. The RE-interleave mask (Factor = 2) could be:
  121. /// <0, 4, 1, 5, 2, 6, 3, 7>
  122. static bool isReInterleaveMask(ArrayRef<int> Mask, unsigned &Factor) {
  123. unsigned NumElts = Mask.size();
  124. if (NumElts < 4)
  125. return false;
  126. // Check potential Factors.
  127. for (Factor = 2; Factor <= MaxFactor; Factor++) {
  128. if (NumElts % Factor)
  129. continue;
  130. unsigned NumSubElts = NumElts / Factor;
  131. if (!isPowerOf2_32(NumSubElts))
  132. continue;
  133. // Check whether each element matchs the RE-interleaved rule. Ignore undef
  134. // elements.
  135. unsigned i = 0;
  136. for (; i < NumElts; i++)
  137. if (Mask[i] >= 0 &&
  138. static_cast<unsigned>(Mask[i]) !=
  139. (i % Factor) * NumSubElts + i / Factor)
  140. break;
  141. // Find a RE-interleaved mask of current factor.
  142. if (i == NumElts)
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool InterleavedAccess::lowerInterleavedLoad(
  148. LoadInst *LI, SmallVector<Instruction *, 32> &DeadInsts) {
  149. if (!LI->isSimple())
  150. return false;
  151. SmallVector<ShuffleVectorInst *, 4> Shuffles;
  152. // Check if all users of this load are shufflevectors.
  153. for (auto UI = LI->user_begin(), E = LI->user_end(); UI != E; UI++) {
  154. ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(*UI);
  155. if (!SVI || !isa<UndefValue>(SVI->getOperand(1)))
  156. return false;
  157. Shuffles.push_back(SVI);
  158. }
  159. if (Shuffles.empty())
  160. return false;
  161. unsigned Factor, Index;
  162. // Check if the first shufflevector is DE-interleave shuffle.
  163. if (!isDeInterleaveMask(Shuffles[0]->getShuffleMask(), Factor, Index))
  164. return false;
  165. // Holds the corresponding index for each DE-interleave shuffle.
  166. SmallVector<unsigned, 4> Indices;
  167. Indices.push_back(Index);
  168. Type *VecTy = Shuffles[0]->getType();
  169. // Check if other shufflevectors are also DE-interleaved of the same type
  170. // and factor as the first shufflevector.
  171. for (unsigned i = 1; i < Shuffles.size(); i++) {
  172. if (Shuffles[i]->getType() != VecTy)
  173. return false;
  174. if (!isDeInterleaveMaskOfFactor(Shuffles[i]->getShuffleMask(), Factor,
  175. Index))
  176. return false;
  177. Indices.push_back(Index);
  178. }
  179. DEBUG(dbgs() << "IA: Found an interleaved load: " << *LI << "\n");
  180. // Try to create target specific intrinsics to replace the load and shuffles.
  181. if (!TLI->lowerInterleavedLoad(LI, Shuffles, Indices, Factor))
  182. return false;
  183. for (auto SVI : Shuffles)
  184. DeadInsts.push_back(SVI);
  185. DeadInsts.push_back(LI);
  186. return true;
  187. }
  188. bool InterleavedAccess::lowerInterleavedStore(
  189. StoreInst *SI, SmallVector<Instruction *, 32> &DeadInsts) {
  190. if (!SI->isSimple())
  191. return false;
  192. ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(SI->getValueOperand());
  193. if (!SVI || !SVI->hasOneUse())
  194. return false;
  195. // Check if the shufflevector is RE-interleave shuffle.
  196. unsigned Factor;
  197. if (!isReInterleaveMask(SVI->getShuffleMask(), Factor))
  198. return false;
  199. DEBUG(dbgs() << "IA: Found an interleaved store: " << *SI << "\n");
  200. // Try to create target specific intrinsics to replace the store and shuffle.
  201. if (!TLI->lowerInterleavedStore(SI, SVI, Factor))
  202. return false;
  203. // Already have a new target specific interleaved store. Erase the old store.
  204. DeadInsts.push_back(SI);
  205. DeadInsts.push_back(SVI);
  206. return true;
  207. }
  208. bool InterleavedAccess::runOnFunction(Function &F) {
  209. if (!TM || !LowerInterleavedAccesses)
  210. return false;
  211. DEBUG(dbgs() << "*** " << getPassName() << ": " << F.getName() << "\n");
  212. TLI = TM->getSubtargetImpl(F)->getTargetLowering();
  213. MaxFactor = TLI->getMaxSupportedInterleaveFactor();
  214. // Holds dead instructions that will be erased later.
  215. SmallVector<Instruction *, 32> DeadInsts;
  216. bool Changed = false;
  217. for (auto &I : inst_range(F)) {
  218. if (LoadInst *LI = dyn_cast<LoadInst>(&I))
  219. Changed |= lowerInterleavedLoad(LI, DeadInsts);
  220. if (StoreInst *SI = dyn_cast<StoreInst>(&I))
  221. Changed |= lowerInterleavedStore(SI, DeadInsts);
  222. }
  223. for (auto I : DeadInsts)
  224. I->eraseFromParent();
  225. return Changed;
  226. }