blob: dd5af9d43ef8a0ad0ba0789c4876a48e00d657a2 [file] [log] [blame]
Sebastian Popc62c6792013-11-12 22:47:20 +00001//===---- 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 Popc62c6792013-11-12 22:47:20 +000017#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/Analysis/Passes.h"
19#include "llvm/Analysis/ScalarEvolution.h"
20#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000021#include "llvm/IR/Constants.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000024#include "llvm/IR/InstIterator.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/IR/Instructions.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Type.h"
28#include "llvm/Pass.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000029#include "llvm/Support/Debug.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000030#include "llvm/Support/raw_ostream.h"
31
32using namespace llvm;
33
Chandler Carruthf1221bd2014-04-22 02:48:03 +000034#define DL_NAME "delinearize"
35#define DEBUG_TYPE DL_NAME
36
Benjamin Kramer9e501ec2013-11-13 15:35:17 +000037namespace {
38
Sebastian Popc62c6792013-11-12 22:47:20 +000039class Delinearization : public FunctionPass {
40 Delinearization(const Delinearization &); // do not implement
41protected:
42 Function *F;
43 LoopInfo *LI;
44 ScalarEvolution *SE;
45
46public:
47 static char ID; // Pass identification, replacement for typeid
48
49 Delinearization() : FunctionPass(ID) {
50 initializeDelinearizationPass(*PassRegistry::getPassRegistry());
51 }
Craig Toppere9ba7592014-03-05 07:30:04 +000052 bool runOnFunction(Function &F) override;
53 void getAnalysisUsage(AnalysisUsage &AU) const override;
Craig Topper9f008862014-04-15 04:59:12 +000054 void print(raw_ostream &O, const Module *M = nullptr) const override;
Sebastian Popc62c6792013-11-12 22:47:20 +000055};
56
Benjamin Kramer9e501ec2013-11-13 15:35:17 +000057} // end anonymous namespace
58
Sebastian Popc62c6792013-11-12 22:47:20 +000059void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000061 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +000062 AU.addRequired<ScalarEvolutionWrapperPass>();
Sebastian Popc62c6792013-11-12 22:47:20 +000063}
64
65bool Delinearization::runOnFunction(Function &F) {
66 this->F = &F;
Chandler Carruth2f1fd162015-08-17 02:08:17 +000067 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000068 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Sebastian Popc62c6792013-11-12 22:47:20 +000069 return false;
70}
71
72static Value *getPointerOperand(Instruction &Inst) {
73 if (LoadInst *Load = dyn_cast<LoadInst>(&Inst))
74 return Load->getPointerOperand();
75 else if (StoreInst *Store = dyn_cast<StoreInst>(&Inst))
76 return Store->getPointerOperand();
77 else if (GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(&Inst))
78 return Gep->getPointerOperand();
Craig Topper9f008862014-04-15 04:59:12 +000079 return nullptr;
Sebastian Popc62c6792013-11-12 22:47:20 +000080}
81
82void Delinearization::print(raw_ostream &O, const Module *) const {
83 O << "Delinearization on function " << F->getName() << ":\n";
84 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
85 Instruction *Inst = &(*I);
Sebastian Pop7ee14722013-11-13 22:37:58 +000086
87 // Only analyze loads and stores.
Sebastian Popc62c6792013-11-12 22:47:20 +000088 if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
89 !isa<GetElementPtrInst>(Inst))
90 continue;
91
92 const BasicBlock *BB = Inst->getParent();
93 // Delinearize the memory access as analyzed in all the surrounding loops.
94 // Do not analyze memory accesses outside loops.
Craig Topper9f008862014-04-15 04:59:12 +000095 for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
Sebastian Popc62c6792013-11-12 22:47:20 +000096 const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
Sebastian Pop28e6b972014-05-27 22:41:51 +000097
98 const SCEVUnknown *BasePointer =
99 dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn));
100 // Do not delinearize if we cannot find the base pointer.
101 if (!BasePointer)
102 break;
103 AccessFn = SE->getMinusSCEV(AccessFn, BasePointer);
Sebastian Pop28e6b972014-05-27 22:41:51 +0000104
Tobias Grosserc3d9db22014-04-09 07:53:49 +0000105 O << "\n";
106 O << "Inst:" << *Inst << "\n";
107 O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
Tobias Grosser374bce02015-10-12 08:02:00 +0000108 O << "AccessFunction: " << *AccessFn << "\n";
Sebastian Popc62c6792013-11-12 22:47:20 +0000109
110 SmallVector<const SCEV *, 3> Subscripts, Sizes;
Tobias Grosser374bce02015-10-12 08:02:00 +0000111 SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst));
Sebastian Popa6e58602014-05-27 22:41:45 +0000112 if (Subscripts.size() == 0 || Sizes.size() == 0 ||
Sebastian Pop448712b2014-05-07 18:01:20 +0000113 Subscripts.size() != Sizes.size()) {
Sebastian Popc62c6792013-11-12 22:47:20 +0000114 O << "failed to delinearize\n";
115 continue;
116 }
Sebastian Pop28e6b972014-05-27 22:41:51 +0000117
118 O << "Base offset: " << *BasePointer << "\n";
Sebastian Popc62c6792013-11-12 22:47:20 +0000119 O << "ArrayDecl[UnknownSize]";
Sebastian Pop448712b2014-05-07 18:01:20 +0000120 int Size = Subscripts.size();
Sebastian Popc62c6792013-11-12 22:47:20 +0000121 for (int i = 0; i < Size - 1; i++)
122 O << "[" << *Sizes[i] << "]";
123 O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
124
125 O << "ArrayRef";
126 for (int i = 0; i < Size; i++)
127 O << "[" << *Subscripts[i] << "]";
128 O << "\n";
129 }
130 }
131}
132
133char Delinearization::ID = 0;
134static const char delinearization_name[] = "Delinearization";
135INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
136 true)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000137INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Sebastian Popc62c6792013-11-12 22:47:20 +0000138INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
139
140FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }