Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 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 | |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
| 18 | #include "llvm/Analysis/Passes.h" |
| 19 | #include "llvm/Analysis/ScalarEvolution.h" |
| 20 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Mehdi Amini | f6071e1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 974a445 | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DerivedTypes.h" |
| 23 | #include "llvm/IR/Function.h" |
Chandler Carruth | 876ac60 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 24 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 974a445 | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/LLVMContext.h" |
| 27 | #include "llvm/IR/Type.h" |
| 28 | #include "llvm/Pass.h" |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
Chandler Carruth | 4da2537 | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 34 | #define DL_NAME "delinearize" |
| 35 | #define DEBUG_TYPE DL_NAME |
| 36 | |
Benjamin Kramer | 1a36261 | 2013-11-13 15:35:17 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 39 | class Delinearization : public FunctionPass { |
| 40 | Delinearization(const Delinearization &); // do not implement |
| 41 | protected: |
| 42 | Function *F; |
| 43 | LoopInfo *LI; |
| 44 | ScalarEvolution *SE; |
| 45 | |
| 46 | public: |
| 47 | static char ID; // Pass identification, replacement for typeid |
| 48 | |
| 49 | Delinearization() : FunctionPass(ID) { |
| 50 | initializeDelinearizationPass(*PassRegistry::getPassRegistry()); |
| 51 | } |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 52 | bool runOnFunction(Function &F) override; |
| 53 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 54 | void print(raw_ostream &O, const Module *M = nullptr) const override; |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Benjamin Kramer | 1a36261 | 2013-11-13 15:35:17 +0000 | [diff] [blame] | 57 | } // end anonymous namespace |
| 58 | |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 59 | void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const { |
| 60 | AU.setPreservesAll(); |
Chandler Carruth | de5df29 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 61 | AU.addRequired<LoopInfoWrapperPass>(); |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 62 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool Delinearization::runOnFunction(Function &F) { |
| 66 | this->F = &F; |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 67 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Chandler Carruth | de5df29 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 68 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 72 | void Delinearization::print(raw_ostream &O, const Module *) const { |
| 73 | O << "Delinearization on function " << F->getName() << ":\n"; |
| 74 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 75 | Instruction *Inst = &(*I); |
Sebastian Pop | f44941d | 2013-11-13 22:37:58 +0000 | [diff] [blame] | 76 | |
| 77 | // Only analyze loads and stores. |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 78 | if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) && |
| 79 | !isa<GetElementPtrInst>(Inst)) |
| 80 | continue; |
| 81 | |
| 82 | const BasicBlock *BB = Inst->getParent(); |
| 83 | // Delinearize the memory access as analyzed in all the surrounding loops. |
| 84 | // Do not analyze memory accesses outside loops. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 85 | for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) { |
Renato Golin | 6587343 | 2018-03-09 21:05:58 +0000 | [diff] [blame] | 86 | const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(Inst), L); |
Sebastian Pop | 79facc9 | 2014-05-27 22:41:51 +0000 | [diff] [blame] | 87 | |
| 88 | const SCEVUnknown *BasePointer = |
| 89 | dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn)); |
| 90 | // Do not delinearize if we cannot find the base pointer. |
| 91 | if (!BasePointer) |
| 92 | break; |
| 93 | AccessFn = SE->getMinusSCEV(AccessFn, BasePointer); |
Sebastian Pop | 79facc9 | 2014-05-27 22:41:51 +0000 | [diff] [blame] | 94 | |
Tobias Grosser | 144ffd6 | 2014-04-09 07:53:49 +0000 | [diff] [blame] | 95 | O << "\n"; |
| 96 | O << "Inst:" << *Inst << "\n"; |
| 97 | O << "In Loop with Header: " << L->getHeader()->getName() << "\n"; |
Tobias Grosser | 87cb71d | 2015-10-12 08:02:00 +0000 | [diff] [blame] | 98 | O << "AccessFunction: " << *AccessFn << "\n"; |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 99 | |
| 100 | SmallVector<const SCEV *, 3> Subscripts, Sizes; |
Tobias Grosser | 87cb71d | 2015-10-12 08:02:00 +0000 | [diff] [blame] | 101 | SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst)); |
Sebastian Pop | 421b2c5 | 2014-05-27 22:41:45 +0000 | [diff] [blame] | 102 | if (Subscripts.size() == 0 || Sizes.size() == 0 || |
Sebastian Pop | 5026b2c | 2014-05-07 18:01:20 +0000 | [diff] [blame] | 103 | Subscripts.size() != Sizes.size()) { |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 104 | O << "failed to delinearize\n"; |
| 105 | continue; |
| 106 | } |
Sebastian Pop | 79facc9 | 2014-05-27 22:41:51 +0000 | [diff] [blame] | 107 | |
| 108 | O << "Base offset: " << *BasePointer << "\n"; |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 109 | O << "ArrayDecl[UnknownSize]"; |
Sebastian Pop | 5026b2c | 2014-05-07 18:01:20 +0000 | [diff] [blame] | 110 | int Size = Subscripts.size(); |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 111 | for (int i = 0; i < Size - 1; i++) |
| 112 | O << "[" << *Sizes[i] << "]"; |
| 113 | O << " with elements of " << *Sizes[Size - 1] << " bytes.\n"; |
| 114 | |
| 115 | O << "ArrayRef"; |
| 116 | for (int i = 0; i < Size; i++) |
| 117 | O << "[" << *Subscripts[i] << "]"; |
| 118 | O << "\n"; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | char Delinearization::ID = 0; |
| 124 | static const char delinearization_name[] = "Delinearization"; |
| 125 | INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true, |
| 126 | true) |
Chandler Carruth | de5df29 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 127 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Sebastian Pop | 5230ad6 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 128 | INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true) |
| 129 | |
| 130 | FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; } |