LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Delinearization.cpp
Go to the documentation of this file.
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 
17 #define DL_NAME "delinearize"
18 #define DEBUG_TYPE DL_NAME
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/Pass.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Analysis/LoopInfo.h"
27 #include "llvm/Analysis/Passes.h"
31 #include "llvm/Support/Debug.h"
34 
35 using namespace llvm;
36 
37 namespace {
38 
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) {
51  }
52  virtual bool runOnFunction(Function &F);
53  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
54  virtual void print(raw_ostream &O, const Module *M = 0) const;
55 };
56 
57 } // end anonymous namespace
58 
59 void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
60  AU.setPreservesAll();
61  AU.addRequired<LoopInfo>();
63 }
64 
65 bool Delinearization::runOnFunction(Function &F) {
66  this->F = &F;
67  SE = &getAnalysis<ScalarEvolution>();
68  LI = &getAnalysis<LoopInfo>();
69  return false;
70 }
71 
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();
79  return NULL;
80 }
81 
82 void 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);
86 
87  // Only analyze loads and stores.
88  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.
95  for (Loop *L = LI->getLoopFor(BB); L != NULL; L = L->getParentLoop()) {
96  const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
97  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(AccessFn);
98 
99  // Do not try to delinearize memory accesses that are not AddRecs.
100  if (!AR)
101  break;
102 
103  O << "AddRec: " << *AR << "\n";
104 
105  SmallVector<const SCEV *, 3> Subscripts, Sizes;
106  const SCEV *Res = AR->delinearize(*SE, Subscripts, Sizes);
107  int Size = Subscripts.size();
108  if (Res == AR || Size == 0) {
109  O << "failed to delinearize\n";
110  continue;
111  }
112  O << "Base offset: " << *Res << "\n";
113  O << "ArrayDecl[UnknownSize]";
114  for (int i = 0; i < Size - 1; i++)
115  O << "[" << *Sizes[i] << "]";
116  O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
117 
118  O << "ArrayRef";
119  for (int i = 0; i < Size; i++)
120  O << "[" << *Subscripts[i] << "]";
121  O << "\n";
122  }
123  }
124 }
125 
126 char Delinearization::ID = 0;
127 static const char delinearization_name[] = "Delinearization";
129  true)
131 INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
132 
133 FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
F(f)
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:411
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:167
static Value * getPointerOperand(Instruction &Inst)
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:128
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
void initializeDelinearizationPass(PassRegistry &)
#define true
Definition: ConvertUTF.c:65
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
FunctionPass * createDelinearizationPass()
static const char delinearization_name[]
INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true, true) FunctionPass *llvm
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:66
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:129
const BasicBlock * getParent() const
Definition: Instruction.h:52
#define DL_NAME