1 //===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/IR/Constants.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/ScalarEvolution.h"
21 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/InstIterator.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
35 #define DL_NAME "delinearize"
36 #define DEBUG_TYPE DL_NAME
40 class Delinearization : public FunctionPass {
41 Delinearization(const Delinearization &); // do not implement
48 static char ID; // Pass identification, replacement for typeid
50 Delinearization() : FunctionPass(ID) {
51 initializeDelinearizationPass(*PassRegistry::getPassRegistry());
53 bool runOnFunction(Function &F) override;
54 void getAnalysisUsage(AnalysisUsage &AU) const override;
55 void print(raw_ostream &O, const Module *M = nullptr) const override;
58 } // end anonymous namespace
60 void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.addRequired<LoopInfo>();
63 AU.addRequired<ScalarEvolution>();
66 bool Delinearization::runOnFunction(Function &F) {
68 SE = &getAnalysis<ScalarEvolution>();
69 LI = &getAnalysis<LoopInfo>();
73 static 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();
83 void 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);
88 // Only analyze loads and stores.
89 if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
90 !isa<GetElementPtrInst>(Inst))
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.
96 for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
97 const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
98 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(AccessFn);
100 // Do not try to delinearize memory accesses that are not AddRecs.
105 O << "Inst:" << *Inst << "\n";
106 O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
108 O << "AddRec: " << *AR << "\n";
110 SmallVector<const SCEV *, 3> Subscripts, Sizes;
111 const SCEV *Res = AR->delinearize(*SE, Subscripts, Sizes);
112 int Size = Subscripts.size();
113 if (Res == AR || Size == 0) {
114 O << "failed to delinearize\n";
117 O << "Base offset: " << *Res << "\n";
118 O << "ArrayDecl[UnknownSize]";
119 for (int i = 0; i < Size - 1; i++)
120 O << "[" << *Sizes[i] << "]";
121 O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
124 for (int i = 0; i < Size; i++)
125 O << "[" << *Subscripts[i] << "]";
131 char Delinearization::ID = 0;
132 static const char delinearization_name[] = "Delinearization";
133 INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
135 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
136 INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
138 FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }