PPCLoopDataPrefetch.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //===-------- PPCLoopDataPrefetch.cpp - Loop Data Prefetching Pass --------===//
  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 a Loop Data Prefetching Pass.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "ppc-loop-data-prefetch"
  14. #include "PPC.h"
  15. #include "llvm/Transforms/Scalar.h"
  16. #include "llvm/ADT/DepthFirstIterator.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/Analysis/AssumptionCache.h"
  19. #include "llvm/Analysis/CodeMetrics.h"
  20. #include "llvm/Analysis/InstructionSimplify.h"
  21. #include "llvm/Analysis/LoopInfo.h"
  22. #include "llvm/Analysis/ScalarEvolution.h"
  23. #include "llvm/Analysis/ScalarEvolutionExpander.h"
  24. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  25. #include "llvm/Analysis/TargetTransformInfo.h"
  26. #include "llvm/Analysis/ValueTracking.h"
  27. #include "llvm/IR/CFG.h"
  28. #include "llvm/IR/Dominators.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/Module.h"
  32. #include "llvm/Support/CommandLine.h"
  33. #include "llvm/Support/Debug.h"
  34. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  35. #include "llvm/Transforms/Utils/Local.h"
  36. #include "llvm/Transforms/Utils/ValueMapper.h"
  37. using namespace llvm;
  38. // By default, we limit this to creating 16 PHIs (which is a little over half
  39. // of the allocatable register set).
  40. static cl::opt<bool>
  41. PrefetchWrites("ppc-loop-prefetch-writes", cl::Hidden, cl::init(false),
  42. cl::desc("Prefetch write addresses"));
  43. // This seems like a reasonable default for the BG/Q (this pass is enabled, by
  44. // default, only on the BG/Q).
  45. static cl::opt<unsigned>
  46. PrefDist("ppc-loop-prefetch-distance", cl::Hidden, cl::init(300),
  47. cl::desc("The loop prefetch distance"));
  48. static cl::opt<unsigned>
  49. CacheLineSize("ppc-loop-prefetch-cache-line", cl::Hidden, cl::init(64),
  50. cl::desc("The loop prefetch cache line size"));
  51. namespace llvm {
  52. void initializePPCLoopDataPrefetchPass(PassRegistry&);
  53. }
  54. namespace {
  55. class PPCLoopDataPrefetch : public FunctionPass {
  56. public:
  57. static char ID; // Pass ID, replacement for typeid
  58. PPCLoopDataPrefetch() : FunctionPass(ID) {
  59. initializePPCLoopDataPrefetchPass(*PassRegistry::getPassRegistry());
  60. }
  61. void getAnalysisUsage(AnalysisUsage &AU) const override {
  62. AU.addRequired<AssumptionCacheTracker>();
  63. AU.addPreserved<DominatorTreeWrapperPass>();
  64. AU.addRequired<LoopInfoWrapperPass>();
  65. AU.addPreserved<LoopInfoWrapperPass>();
  66. AU.addRequired<ScalarEvolution>();
  67. // FIXME: For some reason, preserving SE here breaks LSR (even if
  68. // this pass changes nothing).
  69. // AU.addPreserved<ScalarEvolution>();
  70. AU.addRequired<TargetTransformInfoWrapperPass>();
  71. }
  72. bool runOnFunction(Function &F) override;
  73. bool runOnLoop(Loop *L);
  74. private:
  75. AssumptionCache *AC;
  76. LoopInfo *LI;
  77. ScalarEvolution *SE;
  78. const TargetTransformInfo *TTI;
  79. const DataLayout *DL;
  80. };
  81. }
  82. char PPCLoopDataPrefetch::ID = 0;
  83. INITIALIZE_PASS_BEGIN(PPCLoopDataPrefetch, "ppc-loop-data-prefetch",
  84. "PPC Loop Data Prefetch", false, false)
  85. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  86. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  87. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  88. INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
  89. INITIALIZE_PASS_END(PPCLoopDataPrefetch, "ppc-loop-data-prefetch",
  90. "PPC Loop Data Prefetch", false, false)
  91. FunctionPass *llvm::createPPCLoopDataPrefetchPass() { return new PPCLoopDataPrefetch(); }
  92. bool PPCLoopDataPrefetch::runOnFunction(Function &F) {
  93. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  94. SE = &getAnalysis<ScalarEvolution>();
  95. DL = &F.getParent()->getDataLayout();
  96. AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  97. TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  98. bool MadeChange = false;
  99. for (auto I = LI->begin(), IE = LI->end(); I != IE; ++I)
  100. for (auto L = df_begin(*I), LE = df_end(*I); L != LE; ++L)
  101. MadeChange |= runOnLoop(*L);
  102. return MadeChange;
  103. }
  104. bool PPCLoopDataPrefetch::runOnLoop(Loop *L) {
  105. bool MadeChange = false;
  106. // Only prefetch in the inner-most loop
  107. if (!L->empty())
  108. return MadeChange;
  109. SmallPtrSet<const Value *, 32> EphValues;
  110. CodeMetrics::collectEphemeralValues(L, AC, EphValues);
  111. // Calculate the number of iterations ahead to prefetch
  112. CodeMetrics Metrics;
  113. for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
  114. I != IE; ++I) {
  115. // If the loop already has prefetches, then assume that the user knows
  116. // what he or she is doing and don't add any more.
  117. for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
  118. J != JE; ++J)
  119. if (CallInst *CI = dyn_cast<CallInst>(J))
  120. if (Function *F = CI->getCalledFunction())
  121. if (F->getIntrinsicID() == Intrinsic::prefetch)
  122. return MadeChange;
  123. Metrics.analyzeBasicBlock(*I, *TTI, EphValues);
  124. }
  125. unsigned LoopSize = Metrics.NumInsts;
  126. if (!LoopSize)
  127. LoopSize = 1;
  128. unsigned ItersAhead = PrefDist/LoopSize;
  129. if (!ItersAhead)
  130. ItersAhead = 1;
  131. SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>, 16> PrefLoads;
  132. for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
  133. I != IE; ++I) {
  134. for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
  135. J != JE; ++J) {
  136. Value *PtrValue;
  137. Instruction *MemI;
  138. if (LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
  139. MemI = LMemI;
  140. PtrValue = LMemI->getPointerOperand();
  141. } else if (StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
  142. if (!PrefetchWrites) continue;
  143. MemI = SMemI;
  144. PtrValue = SMemI->getPointerOperand();
  145. } else continue;
  146. unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace();
  147. if (PtrAddrSpace)
  148. continue;
  149. if (L->isLoopInvariant(PtrValue))
  150. continue;
  151. const SCEV *LSCEV = SE->getSCEV(PtrValue);
  152. const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV);
  153. if (!LSCEVAddRec)
  154. continue;
  155. // We don't want to double prefetch individual cache lines. If this load
  156. // is known to be within one cache line of some other load that has
  157. // already been prefetched, then don't prefetch this one as well.
  158. bool DupPref = false;
  159. for (SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>,
  160. 16>::iterator K = PrefLoads.begin(), KE = PrefLoads.end();
  161. K != KE; ++K) {
  162. const SCEV *PtrDiff = SE->getMinusSCEV(LSCEVAddRec, K->second);
  163. if (const SCEVConstant *ConstPtrDiff =
  164. dyn_cast<SCEVConstant>(PtrDiff)) {
  165. int64_t PD = std::abs(ConstPtrDiff->getValue()->getSExtValue());
  166. if (PD < (int64_t) CacheLineSize) {
  167. DupPref = true;
  168. break;
  169. }
  170. }
  171. }
  172. if (DupPref)
  173. continue;
  174. const SCEV *NextLSCEV = SE->getAddExpr(LSCEVAddRec, SE->getMulExpr(
  175. SE->getConstant(LSCEVAddRec->getType(), ItersAhead),
  176. LSCEVAddRec->getStepRecurrence(*SE)));
  177. if (!isSafeToExpand(NextLSCEV, *SE))
  178. continue;
  179. PrefLoads.push_back(std::make_pair(MemI, LSCEVAddRec));
  180. Type *I8Ptr = Type::getInt8PtrTy((*I)->getContext(), PtrAddrSpace);
  181. SCEVExpander SCEVE(*SE, J->getModule()->getDataLayout(), "prefaddr");
  182. Value *PrefPtrValue = SCEVE.expandCodeFor(NextLSCEV, I8Ptr, MemI);
  183. IRBuilder<> Builder(MemI);
  184. Module *M = (*I)->getParent()->getParent();
  185. Type *I32 = Type::getInt32Ty((*I)->getContext());
  186. Value *PrefetchFunc = Intrinsic::getDeclaration(M, Intrinsic::prefetch);
  187. Builder.CreateCall(
  188. PrefetchFunc,
  189. {PrefPtrValue,
  190. ConstantInt::get(I32, MemI->mayReadFromMemory() ? 0 : 1),
  191. ConstantInt::get(I32, 3), ConstantInt::get(I32, 1)});
  192. MadeChange = true;
  193. }
  194. }
  195. return MadeChange;
  196. }