InterleavedAccessPass.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/Support/raw_ostream.h"
  44. #include "llvm/Target/TargetLowering.h"
  45. #include "llvm/Target/TargetSubtargetInfo.h"
  46. using namespace llvm;
  47. #define DEBUG_TYPE "interleaved-access"
  48. static cl::opt<bool> LowerInterleavedAccesses(
  49. "lower-interleaved-accesses",
  50. cl::desc("Enable lowering interleaved accesses to intrinsics"),
  51. cl::init(false), cl::Hidden);
  52. static unsigned MaxFactor; // The maximum supported interleave factor.
  53. namespace llvm {
  54. static void initializeInterleavedAccessPass(PassRegistry &);
  55. }
  56. namespace {
  57. class InterleavedAccess : public FunctionPass {
  58. public:
  59. static char ID;
  60. InterleavedAccess(const TargetMachine *TM = nullptr)
  61. : FunctionPass(ID), TM(TM), TLI(nullptr) {
  62. initializeInterleavedAccessPass(*PassRegistry::getPassRegistry());
  63. }
  64. const char *getPassName() const override { return "Interleaved Access Pass"; }
  65. bool runOnFunction(Function &F) override;
  66. private:
  67. const TargetMachine *TM;
  68. const TargetLowering *TLI;
  69. /// \brief Transform an interleaved load into target specific intrinsics.
  70. bool lowerInterleavedLoad(LoadInst *LI,
  71. SmallVector<Instruction *, 32> &DeadInsts);
  72. /// \brief Transform an interleaved store into target specific intrinsics.
  73. bool lowerInterleavedStore(StoreInst *SI,
  74. SmallVector<Instruction *, 32> &DeadInsts);
  75. };
  76. } // end anonymous namespace.
  77. char InterleavedAccess::ID = 0;
  78. INITIALIZE_TM_PASS(InterleavedAccess, "interleaved-access",
  79. "Lower interleaved memory accesses to target specific intrinsics",
  80. false, false)
  81. FunctionPass *llvm::createInterleavedAccessPass(const TargetMachine *TM) {
  82. return new InterleavedAccess(TM);
  83. }
  84. /// \brief Check if the mask is a DE-interleave mask of the given factor
  85. /// \p Factor like:
  86. /// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor>
  87. static bool isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, unsigned Factor,
  88. unsigned &Index) {
  89. // Check all potential start indices from 0 to (Factor - 1).
  90. for (Index = 0; Index < Factor; Index++) {
  91. unsigned i = 0;
  92. // Check that elements are in ascending order by Factor. Ignore undef
  93. // elements.
  94. for (; i < Mask.size(); i++)
  95. if (Mask[i] >= 0 && static_cast<unsigned>(Mask[i]) != Index + i * Factor)
  96. break;
  97. if (i == Mask.size())
  98. return true;
  99. }
  100. return false;
  101. }
  102. /// \brief Check if the mask is a DE-interleave mask for an interleaved load.
  103. ///
  104. /// E.g. DE-interleave masks (Factor = 2) could be:
  105. /// <0, 2, 4, 6> (mask of index 0 to extract even elements)
  106. /// <1, 3, 5, 7> (mask of index 1 to extract odd elements)
  107. static bool isDeInterleaveMask(ArrayRef<int> Mask, unsigned &Factor,
  108. unsigned &Index) {
  109. if (Mask.size() < 2)
  110. return false;
  111. // Check potential Factors.
  112. for (Factor = 2; Factor <= MaxFactor; Factor++)
  113. if (isDeInterleaveMaskOfFactor(Mask, Factor, Index))
  114. return true;
  115. return false;
  116. }
  117. /// \brief Check if the mask is RE-interleave mask for an interleaved store.
  118. ///
  119. /// I.e. <0, NumSubElts, ... , NumSubElts*(Factor - 1), 1, NumSubElts + 1, ...>
  120. ///
  121. /// E.g. The RE-interleave mask (Factor = 2) could be:
  122. /// <0, 4, 1, 5, 2, 6, 3, 7>
  123. static bool isReInterleaveMask(ArrayRef<int> Mask, unsigned &Factor) {
  124. unsigned NumElts = Mask.size();
  125. if (NumElts < 4)
  126. return false;
  127. // Check potential Factors.
  128. for (Factor = 2; Factor <= MaxFactor; Factor++) {
  129. if (NumElts % Factor)
  130. continue;
  131. unsigned NumSubElts = NumElts / Factor;
  132. if (!isPowerOf2_32(NumSubElts))
  133. continue;
  134. // Check whether each element matchs the RE-interleaved rule. Ignore undef
  135. // elements.
  136. unsigned i = 0;
  137. for (; i < NumElts; i++)
  138. if (Mask[i] >= 0 &&
  139. static_cast<unsigned>(Mask[i]) !=
  140. (i % Factor) * NumSubElts + i / Factor)
  141. break;
  142. // Find a RE-interleaved mask of current factor.
  143. if (i == NumElts)
  144. return true;
  145. }
  146. return false;
  147. }
  148. bool InterleavedAccess::lowerInterleavedLoad(
  149. LoadInst *LI, SmallVector<Instruction *, 32> &DeadInsts) {
  150. if (!LI->isSimple())
  151. return false;
  152. SmallVector<ShuffleVectorInst *, 4> Shuffles;
  153. // Check if all users of this load are shufflevectors.
  154. for (auto UI = LI->user_begin(), E = LI->user_end(); UI != E; UI++) {
  155. ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(*UI);
  156. if (!SVI || !isa<UndefValue>(SVI->getOperand(1)))
  157. return false;
  158. Shuffles.push_back(SVI);
  159. }
  160. if (Shuffles.empty())
  161. return false;
  162. unsigned Factor, Index;
  163. // Check if the first shufflevector is DE-interleave shuffle.
  164. if (!isDeInterleaveMask(Shuffles[0]->getShuffleMask(), Factor, Index))
  165. return false;
  166. // Holds the corresponding index for each DE-interleave shuffle.
  167. SmallVector<unsigned, 4> Indices;
  168. Indices.push_back(Index);
  169. Type *VecTy = Shuffles[0]->getType();
  170. // Check if other shufflevectors are also DE-interleaved of the same type
  171. // and factor as the first shufflevector.
  172. for (unsigned i = 1; i < Shuffles.size(); i++) {
  173. if (Shuffles[i]->getType() != VecTy)
  174. return false;
  175. if (!isDeInterleaveMaskOfFactor(Shuffles[i]->getShuffleMask(), Factor,
  176. Index))
  177. return false;
  178. Indices.push_back(Index);
  179. }
  180. DEBUG(dbgs() << "IA: Found an interleaved load: " << *LI << "\n");
  181. // Try to create target specific intrinsics to replace the load and shuffles.
  182. if (!TLI->lowerInterleavedLoad(LI, Shuffles, Indices, Factor))
  183. return false;
  184. for (auto SVI : Shuffles)
  185. DeadInsts.push_back(SVI);
  186. DeadInsts.push_back(LI);
  187. return true;
  188. }
  189. bool InterleavedAccess::lowerInterleavedStore(
  190. StoreInst *SI, SmallVector<Instruction *, 32> &DeadInsts) {
  191. if (!SI->isSimple())
  192. return false;
  193. ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(SI->getValueOperand());
  194. if (!SVI || !SVI->hasOneUse())
  195. return false;
  196. // Check if the shufflevector is RE-interleave shuffle.
  197. unsigned Factor;
  198. if (!isReInterleaveMask(SVI->getShuffleMask(), Factor))
  199. return false;
  200. DEBUG(dbgs() << "IA: Found an interleaved store: " << *SI << "\n");
  201. // Try to create target specific intrinsics to replace the store and shuffle.
  202. if (!TLI->lowerInterleavedStore(SI, SVI, Factor))
  203. return false;
  204. // Already have a new target specific interleaved store. Erase the old store.
  205. DeadInsts.push_back(SI);
  206. DeadInsts.push_back(SVI);
  207. return true;
  208. }
  209. bool InterleavedAccess::runOnFunction(Function &F) {
  210. if (!TM || !LowerInterleavedAccesses)
  211. return false;
  212. DEBUG(dbgs() << "*** " << getPassName() << ": " << F.getName() << "\n");
  213. TLI = TM->getSubtargetImpl(F)->getTargetLowering();
  214. MaxFactor = TLI->getMaxSupportedInterleaveFactor();
  215. // Holds dead instructions that will be erased later.
  216. SmallVector<Instruction *, 32> DeadInsts;
  217. bool Changed = false;
  218. for (auto &I : inst_range(F)) {
  219. if (LoadInst *LI = dyn_cast<LoadInst>(&I))
  220. Changed |= lowerInterleavedLoad(LI, DeadInsts);
  221. if (StoreInst *SI = dyn_cast<StoreInst>(&I))
  222. Changed |= lowerInterleavedStore(SI, DeadInsts);
  223. }
  224. for (auto I : DeadInsts)
  225. I->eraseFromParent();
  226. return Changed;
  227. }