Delinearization.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===//
  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 implements an analysis pass that tries to delinearize all GEP
  11. // instructions in all loops using the SCEV analysis functionality. This pass is
  12. // only used for testing purposes: if your pass needs delinearization, please
  13. // use the on-demand SCEVAddRecExpr::delinearize() function.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/Analysis/LoopInfo.h"
  18. #include "llvm/Analysis/Passes.h"
  19. #include "llvm/Analysis/ScalarEvolution.h"
  20. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  21. #include "llvm/IR/DerivedTypes.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/InstIterator.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/LLVMContext.h"
  26. #include "llvm/IR/Type.h"
  27. #include "llvm/Pass.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. using namespace llvm;
  32. #define DL_NAME "delinearize"
  33. #define DEBUG_TYPE DL_NAME
  34. namespace {
  35. class Delinearization : public FunctionPass {
  36. Delinearization(const Delinearization &); // do not implement
  37. protected:
  38. Function *F;
  39. LoopInfo *LI;
  40. ScalarEvolution *SE;
  41. public:
  42. static char ID; // Pass identification, replacement for typeid
  43. Delinearization() : FunctionPass(ID) {
  44. initializeDelinearizationPass(*PassRegistry::getPassRegistry());
  45. }
  46. bool runOnFunction(Function &F) override;
  47. void getAnalysisUsage(AnalysisUsage &AU) const override;
  48. void print(raw_ostream &O, const Module *M = nullptr) const override;
  49. };
  50. } // end anonymous namespace
  51. void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
  52. AU.setPreservesAll();
  53. AU.addRequired<LoopInfoWrapperPass>();
  54. AU.addRequired<ScalarEvolution>();
  55. }
  56. bool Delinearization::runOnFunction(Function &F) {
  57. this->F = &F;
  58. SE = &getAnalysis<ScalarEvolution>();
  59. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  60. return false;
  61. }
  62. static Value *getPointerOperand(Instruction &Inst) {
  63. if (LoadInst *Load = dyn_cast<LoadInst>(&Inst))
  64. return Load->getPointerOperand();
  65. else if (StoreInst *Store = dyn_cast<StoreInst>(&Inst))
  66. return Store->getPointerOperand();
  67. else if (GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(&Inst))
  68. return Gep->getPointerOperand();
  69. return nullptr;
  70. }
  71. void Delinearization::print(raw_ostream &O, const Module *) const {
  72. O << "Delinearization on function " << F->getName() << ":\n";
  73. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
  74. Instruction *Inst = &(*I);
  75. // Only analyze loads and stores.
  76. if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
  77. !isa<GetElementPtrInst>(Inst))
  78. continue;
  79. const BasicBlock *BB = Inst->getParent();
  80. // Delinearize the memory access as analyzed in all the surrounding loops.
  81. // Do not analyze memory accesses outside loops.
  82. for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
  83. const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
  84. const SCEVUnknown *BasePointer =
  85. dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn));
  86. // Do not delinearize if we cannot find the base pointer.
  87. if (!BasePointer)
  88. break;
  89. AccessFn = SE->getMinusSCEV(AccessFn, BasePointer);
  90. const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(AccessFn);
  91. // Do not try to delinearize memory accesses that are not AddRecs.
  92. if (!AR)
  93. break;
  94. O << "\n";
  95. O << "Inst:" << *Inst << "\n";
  96. O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
  97. O << "AddRec: " << *AR << "\n";
  98. SmallVector<const SCEV *, 3> Subscripts, Sizes;
  99. SE->delinearize(AR, Subscripts, Sizes, SE->getElementSize(Inst));
  100. if (Subscripts.size() == 0 || Sizes.size() == 0 ||
  101. Subscripts.size() != Sizes.size()) {
  102. O << "failed to delinearize\n";
  103. continue;
  104. }
  105. O << "Base offset: " << *BasePointer << "\n";
  106. O << "ArrayDecl[UnknownSize]";
  107. int Size = Subscripts.size();
  108. for (int i = 0; i < Size - 1; i++)
  109. O << "[" << *Sizes[i] << "]";
  110. O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
  111. O << "ArrayRef";
  112. for (int i = 0; i < Size; i++)
  113. O << "[" << *Subscripts[i] << "]";
  114. O << "\n";
  115. }
  116. }
  117. }
  118. char Delinearization::ID = 0;
  119. static const char delinearization_name[] = "Delinearization";
  120. INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
  121. true)
  122. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  123. INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
  124. FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }