PPCLoopDataPrefetch.cpp 7.9 KB

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