Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 1 | //===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This implements an analysis pass that tries to delinearize all GEP |
| 10 | // instructions in all loops using the SCEV analysis functionality. This pass is |
| 11 | // only used for testing purposes: if your pass needs delinearization, please |
| 12 | // use the on-demand SCEVAddRecExpr::delinearize() function. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
| 17 | #include "llvm/Analysis/Passes.h" |
| 18 | #include "llvm/Analysis/ScalarEvolution.h" |
| 19 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DerivedTypes.h" |
| 22 | #include "llvm/IR/Function.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
| 26 | #include "llvm/IR/Type.h" |
| 27 | #include "llvm/Pass.h" |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 33 | #define DL_NAME "delinearize" |
| 34 | #define DEBUG_TYPE DL_NAME |
| 35 | |
Benjamin Kramer | 9e501ec | 2013-11-13 15:35:17 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 38 | class Delinearization : public FunctionPass { |
| 39 | Delinearization(const Delinearization &); // do not implement |
| 40 | protected: |
| 41 | Function *F; |
| 42 | LoopInfo *LI; |
| 43 | ScalarEvolution *SE; |
| 44 | |
| 45 | public: |
| 46 | static char ID; // Pass identification, replacement for typeid |
| 47 | |
| 48 | Delinearization() : FunctionPass(ID) { |
| 49 | initializeDelinearizationPass(*PassRegistry::getPassRegistry()); |
| 50 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 51 | bool runOnFunction(Function &F) override; |
| 52 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 53 | void print(raw_ostream &O, const Module *M = nullptr) const override; |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Benjamin Kramer | 9e501ec | 2013-11-13 15:35:17 +0000 | [diff] [blame] | 56 | } // end anonymous namespace |
| 57 | |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 58 | void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const { |
| 59 | AU.setPreservesAll(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 60 | AU.addRequired<LoopInfoWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 61 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool Delinearization::runOnFunction(Function &F) { |
| 65 | this->F = &F; |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 66 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 67 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 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); |
Sebastian Pop | 7ee1472 | 2013-11-13 22:37:58 +0000 | [diff] [blame] | 75 | |
| 76 | // Only analyze loads and stores. |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 77 | if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) && |
| 78 | !isa<GetElementPtrInst>(Inst)) |
| 79 | continue; |
| 80 | |
| 81 | const BasicBlock *BB = Inst->getParent(); |
| 82 | // Delinearize the memory access as analyzed in all the surrounding loops. |
| 83 | // Do not analyze memory accesses outside loops. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 84 | for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) { |
Renato Golin | 038ede2 | 2018-03-09 21:05:58 +0000 | [diff] [blame] | 85 | const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(Inst), L); |
Sebastian Pop | 28e6b97 | 2014-05-27 22:41:51 +0000 | [diff] [blame] | 86 | |
| 87 | const SCEVUnknown *BasePointer = |
| 88 | dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn)); |
| 89 | // Do not delinearize if we cannot find the base pointer. |
| 90 | if (!BasePointer) |
| 91 | break; |
| 92 | AccessFn = SE->getMinusSCEV(AccessFn, BasePointer); |
Sebastian Pop | 28e6b97 | 2014-05-27 22:41:51 +0000 | [diff] [blame] | 93 | |
Tobias Grosser | c3d9db2 | 2014-04-09 07:53:49 +0000 | [diff] [blame] | 94 | O << "\n"; |
| 95 | O << "Inst:" << *Inst << "\n"; |
| 96 | O << "In Loop with Header: " << L->getHeader()->getName() << "\n"; |
Tobias Grosser | 374bce0 | 2015-10-12 08:02:00 +0000 | [diff] [blame] | 97 | O << "AccessFunction: " << *AccessFn << "\n"; |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 98 | |
| 99 | SmallVector<const SCEV *, 3> Subscripts, Sizes; |
Tobias Grosser | 374bce0 | 2015-10-12 08:02:00 +0000 | [diff] [blame] | 100 | SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst)); |
Sebastian Pop | a6e5860 | 2014-05-27 22:41:45 +0000 | [diff] [blame] | 101 | if (Subscripts.size() == 0 || Sizes.size() == 0 || |
Sebastian Pop | 448712b | 2014-05-07 18:01:20 +0000 | [diff] [blame] | 102 | Subscripts.size() != Sizes.size()) { |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 103 | O << "failed to delinearize\n"; |
| 104 | continue; |
| 105 | } |
Sebastian Pop | 28e6b97 | 2014-05-27 22:41:51 +0000 | [diff] [blame] | 106 | |
| 107 | O << "Base offset: " << *BasePointer << "\n"; |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 108 | O << "ArrayDecl[UnknownSize]"; |
Sebastian Pop | 448712b | 2014-05-07 18:01:20 +0000 | [diff] [blame] | 109 | int Size = Subscripts.size(); |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 110 | for (int i = 0; i < Size - 1; i++) |
| 111 | O << "[" << *Sizes[i] << "]"; |
| 112 | O << " with elements of " << *Sizes[Size - 1] << " bytes.\n"; |
| 113 | |
| 114 | O << "ArrayRef"; |
| 115 | for (int i = 0; i < Size; i++) |
| 116 | O << "[" << *Subscripts[i] << "]"; |
| 117 | O << "\n"; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | char Delinearization::ID = 0; |
| 123 | static const char delinearization_name[] = "Delinearization"; |
| 124 | INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true, |
| 125 | true) |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 126 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
Sebastian Pop | c62c679 | 2013-11-12 22:47:20 +0000 | [diff] [blame] | 127 | INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true) |
| 128 | |
| 129 | FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; } |