blob: d603b7b21e310f525de37fbec65d83d7355691c1 [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/IR/Constants.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000018#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/Passes.h"
20#include "llvm/Analysis/ScalarEvolution.h"
21#include "llvm/Analysis/ScalarEvolutionExpressions.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/CommandLine.h"
30#include "llvm/Support/Debug.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000031#include "llvm/Support/raw_ostream.h"
32
33using namespace llvm;
34
Chandler Carruthf1221bd2014-04-22 02:48:03 +000035#define DL_NAME "delinearize"
36#define DEBUG_TYPE DL_NAME
37
Benjamin Kramer9e501ec2013-11-13 15:35:17 +000038namespace {
39
Sebastian Popc62c6792013-11-12 22:47:20 +000040class Delinearization : public FunctionPass {
41 Delinearization(const Delinearization &); // do not implement
42protected:
43 Function *F;
44 LoopInfo *LI;
45 ScalarEvolution *SE;
46
47public:
48 static char ID; // Pass identification, replacement for typeid
49
50 Delinearization() : FunctionPass(ID) {
51 initializeDelinearizationPass(*PassRegistry::getPassRegistry());
52 }
Craig Toppere9ba7592014-03-05 07:30:04 +000053 bool runOnFunction(Function &F) override;
54 void getAnalysisUsage(AnalysisUsage &AU) const override;
Craig Topper9f008862014-04-15 04:59:12 +000055 void print(raw_ostream &O, const Module *M = nullptr) const override;
Sebastian Popc62c6792013-11-12 22:47:20 +000056};
57
Benjamin Kramer9e501ec2013-11-13 15:35:17 +000058} // end anonymous namespace
59
Sebastian Popc62c6792013-11-12 22:47:20 +000060void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000062 AU.addRequired<LoopInfoWrapperPass>();
Sebastian Popc62c6792013-11-12 22:47:20 +000063 AU.addRequired<ScalarEvolution>();
64}
65
66bool Delinearization::runOnFunction(Function &F) {
67 this->F = &F;
68 SE = &getAnalysis<ScalarEvolution>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000069 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Sebastian Popc62c6792013-11-12 22:47:20 +000070 return false;
71}
72
73static Value *getPointerOperand(Instruction &Inst) {
74 if (LoadInst *Load = dyn_cast<LoadInst>(&Inst))
75 return Load->getPointerOperand();
76 else if (StoreInst *Store = dyn_cast<StoreInst>(&Inst))
77 return Store->getPointerOperand();
78 else if (GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(&Inst))
79 return Gep->getPointerOperand();
Craig Topper9f008862014-04-15 04:59:12 +000080 return nullptr;
Sebastian Popc62c6792013-11-12 22:47:20 +000081}
82
83void Delinearization::print(raw_ostream &O, const Module *) const {
84 O << "Delinearization on function " << F->getName() << ":\n";
85 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
86 Instruction *Inst = &(*I);
Sebastian Pop7ee14722013-11-13 22:37:58 +000087
88 // Only analyze loads and stores.
Sebastian Popc62c6792013-11-12 22:47:20 +000089 if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
90 !isa<GetElementPtrInst>(Inst))
91 continue;
92
93 const BasicBlock *BB = Inst->getParent();
94 // Delinearize the memory access as analyzed in all the surrounding loops.
95 // Do not analyze memory accesses outside loops.
Craig Topper9f008862014-04-15 04:59:12 +000096 for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
Sebastian Popc62c6792013-11-12 22:47:20 +000097 const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
Sebastian Pop28e6b972014-05-27 22:41:51 +000098
99 const SCEVUnknown *BasePointer =
100 dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn));
101 // Do not delinearize if we cannot find the base pointer.
102 if (!BasePointer)
103 break;
104 AccessFn = SE->getMinusSCEV(AccessFn, BasePointer);
Sebastian Popc62c6792013-11-12 22:47:20 +0000105 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(AccessFn);
Sebastian Pop7ee14722013-11-13 22:37:58 +0000106
107 // Do not try to delinearize memory accesses that are not AddRecs.
Sebastian Popc62c6792013-11-12 22:47:20 +0000108 if (!AR)
109 break;
110
Sebastian Pop28e6b972014-05-27 22:41:51 +0000111
Tobias Grosserc3d9db22014-04-09 07:53:49 +0000112 O << "\n";
113 O << "Inst:" << *Inst << "\n";
114 O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
Sebastian Popc62c6792013-11-12 22:47:20 +0000115 O << "AddRec: " << *AR << "\n";
116
117 SmallVector<const SCEV *, 3> Subscripts, Sizes;
Sebastian Pop28e6b972014-05-27 22:41:51 +0000118 AR->delinearize(*SE, Subscripts, Sizes, SE->getElementSize(Inst));
Sebastian Popa6e58602014-05-27 22:41:45 +0000119 if (Subscripts.size() == 0 || Sizes.size() == 0 ||
Sebastian Pop448712b2014-05-07 18:01:20 +0000120 Subscripts.size() != Sizes.size()) {
Sebastian Popc62c6792013-11-12 22:47:20 +0000121 O << "failed to delinearize\n";
122 continue;
123 }
Sebastian Pop28e6b972014-05-27 22:41:51 +0000124
125 O << "Base offset: " << *BasePointer << "\n";
Sebastian Popc62c6792013-11-12 22:47:20 +0000126 O << "ArrayDecl[UnknownSize]";
Sebastian Pop448712b2014-05-07 18:01:20 +0000127 int Size = Subscripts.size();
Sebastian Popc62c6792013-11-12 22:47:20 +0000128 for (int i = 0; i < Size - 1; i++)
129 O << "[" << *Sizes[i] << "]";
130 O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
131
132 O << "ArrayRef";
133 for (int i = 0; i < Size; i++)
134 O << "[" << *Subscripts[i] << "]";
135 O << "\n";
136 }
137 }
138}
139
140char Delinearization::ID = 0;
141static const char delinearization_name[] = "Delinearization";
142INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
143 true)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000144INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Sebastian Popc62c6792013-11-12 22:47:20 +0000145INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
146
147FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }