CGRAOmp  0.1
LoopDependencyAnalysis.hpp
Go to the documentation of this file.
1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022 Amano laboratory, Keio University & Processor Research Team, RIKEN Center for Computational Science
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * File: /include/LoopDependencyAnalysis.hpp
25 * Project: CGRAOmp
26 * Author: Takuya Kojima in The University of Tokyo (tkojima@hal.ipc.i.u-tokyo.ac.jp)
27 * Created Date: 18-02-2022 18:10:42
28 * Last Modified: 20-02-2022 22:42:54
29 */
30 #ifndef LoopDependencyAnalysis_H
31 #define LoopDependencyAnalysis_H
32 
33 #include "llvm/IR/PassManager.h"
34 #include "llvm/Analysis/LoopInfo.h"
35 #include "llvm/Analysis/LoopAnalysisManager.h"
36 #include "llvm/ADT/iterator_range.h"
37 #include "llvm/ADT/SmallVector.h"
38 
39 #include "llvm/Analysis/ScalarEvolution.h"
40 #include "llvm/ADT/Optional.h"
41 
42 using namespace llvm;
43 
44 
45 namespace CGRAOmp {
46 
52  public:
53  enum class DepKind {
54  Simple,
55  Memory,
56  InductionVar,
57  };
58 
66  LoopDependency(DepKind kind, Value* def, Value *init,
67  PHINode *phi, int distance) :
68  def(def), distance(distance), kind(kind), phi(phi),
69  init(init) {};
70 
71  DepKind getKind() const {
72  return kind;
73  }
74 
75  bool correspond(PHINode *P) {
76  return P == phi;
77  }
78 
79  // bool correspond(Instruction *U) {
80  // return U == use;
81  // }
82 
83  Value* getDef() {
84  return def;
85  }
86 
87  Instruction* getDefInst() {
88  return dyn_cast<Instruction>(def);
89  }
90 
91  Value* getInit() {
92  return init;
93  }
94 
95  PHINode* getPhi() {
96  return phi;
97  }
98 
99  int getDistance() {
100  return distance;
101  }
102 
103  protected:
104  PHINode* phi;
106  Value *def;
107  Value *init;
108 
109  int distance;
110 
111  };
112 
118  public:
119  SimpleLoopDependency(Instruction *def, Value* init, PHINode* phi) :
120  LoopDependency(DepKind::Simple, def, init, phi, 1) {};
121 
122  static bool classof(const LoopDependency* LD) {
123  return LD->getKind() == LoopDependency::DepKind::Simple;
124  }
125  private:
126 
127  };
128 
134  public:
135  MemoryLoopDependency(StoreInst *store, LoadInst *load, int distance) :
136  LoopDependency(DepKind::Memory, store->getOperand(0),
137  nullptr, nullptr, distance),
138  store(store), load(load) {};
139 
140 
141  LoadInst *getLoad() {
142  return load;
143  }
144 
145  StoreInst *getStore() {
146  return store;
147  }
148 
149  static bool classof(const LoopDependency* LD) {
150  return LD->getKind() == LoopDependency::DepKind::Memory;
151  }
152  private:
153  LoadInst *load;
154  StoreInst *store;
155 
156  };
157 
163  public:
164  InductionVariableDependency(PHINode *indvar, Instruction *bin_op,
165  Value* start, Value *step) :
166  LoopDependency(DepKind::InductionVar, bin_op, start, indvar, 1),
167  step(step) {};
168 
169 
170  static bool classof(const LoopDependency* LD) {
171  return LD->getKind() == LoopDependency::DepKind::InductionVar;
172  }
173  private:
174  Value *step, *start;
175  Instruction* bin_op;
176  };
177 
183  public:
184  using DepList = SmallVector<LoopDependency*>;
185 
186  using dep_iterator = DepList::iterator;
187 
188  // induction variable dependency
190  return indvar_dep_list.begin();
191  }
193  return indvar_dep_list.end();
194  }
195  inline iterator_range<dep_iterator> idv_deps() {
196  return make_range(idv_dep_begin(), idv_dep_end());
197  }
198 
200  indvar_dep_list.emplace_back(D);
201  };
202 
203  int getNumIdvDep() {
204  return indvar_dep_list.size();
205  }
206 
207  // memory dependency
209  return mem_dep_list.begin();
210  }
212  return mem_dep_list.end();
213  }
214  inline iterator_range<dep_iterator> mem_deps() {
215  return make_range(mem_dep_begin(), mem_dep_end());
216  }
217 
219  mem_dep_list.emplace_back(D);
220  }
221 
222  int getNumMemDep() {
223  return mem_dep_list.size();
224  }
225 
226  // loop-carried dependency
228  return lc_dep_list.begin();
229  }
231  return lc_dep_list.end();
232  }
233  inline iterator_range<dep_iterator> lc_deps() {
234  return make_range(lc_dep_begin(), lc_dep_end());
235  }
236 
238  lc_dep_list.emplace_back(D);
239  }
240 
241  int getNumDep() {
242  return lc_dep_list.size();
243  }
244 
245  private:
249  };
250 
251 
257  public AnalysisInfoMixin<LoopDependencyAnalysisPass> {
258  public:
260  Result run(Loop &L, LoopAnalysisManager &AM,
261  LoopStandardAnalysisResults &AR);
262  private:
263  friend AnalysisInfoMixin<LoopDependencyAnalysisPass>;
264  static AnalysisKey Key;
265 
266 
267  Optional<int> getDistance(Instruction* A, Instruction *B, ScalarEvolution &SE);
268 
269  };
270 }
271 
272 
273 #endif //LoopDependencyAnalysis_H
CGRAOmp::LoopDependency::getInit
Value * getInit()
Definition: LoopDependencyAnalysis.hpp:91
CGRAOmp::LoopDependencyAnalysisPass::Key
static AnalysisKey Key
Definition: LoopDependencyAnalysis.hpp:264
llvm
Definition: OptionPlugin.cpp:128
CGRAOmp::LoopDependencyInfo::idv_deps
iterator_range< dep_iterator > idv_deps()
Definition: LoopDependencyAnalysis.hpp:195
CGRAOmp::LoopDependency::def
Value * def
Definition: LoopDependencyAnalysis.hpp:106
CGRAOmp::InductionVariableDependency::InductionVariableDependency
InductionVariableDependency(PHINode *indvar, Instruction *bin_op, Value *start, Value *step)
Definition: LoopDependencyAnalysis.hpp:164
CGRAOmp::InductionVariableDependency::bin_op
Instruction * bin_op
Definition: LoopDependencyAnalysis.hpp:175
CGRAOmp::SimpleLoopDependency::classof
static bool classof(const LoopDependency *LD)
Definition: LoopDependencyAnalysis.hpp:122
CGRAOmp::LoopDependencyInfo::mem_dep_list
DepList mem_dep_list
Definition: LoopDependencyAnalysis.hpp:247
CGRAOmp::LoopDependencyInfo::idv_dep_end
dep_iterator idv_dep_end()
Definition: LoopDependencyAnalysis.hpp:192
CGRAOmp::LoopDependency::getDistance
int getDistance()
Definition: LoopDependencyAnalysis.hpp:99
CGRAOmp::LoopDependency::init
Value * init
Definition: LoopDependencyAnalysis.hpp:107
CGRAOmp::LoopDependency
A base class for loop dependency relationship.
Definition: LoopDependencyAnalysis.hpp:51
CGRAOmp::LoopDependencyInfo::mem_dep_begin
dep_iterator mem_dep_begin()
Definition: LoopDependencyAnalysis.hpp:208
CGRAOmp::LoopDependencyInfo::DepList
SmallVector< LoopDependency * > DepList
Definition: LoopDependencyAnalysis.hpp:184
CGRAOmp::LoopDependency::phi
PHINode * phi
Definition: LoopDependencyAnalysis.hpp:104
CGRAOmp::InductionVariableDependency::classof
static bool classof(const LoopDependency *LD)
Definition: LoopDependencyAnalysis.hpp:170
CGRAOmp::LoopDependency::kind
DepKind kind
Definition: LoopDependencyAnalysis.hpp:105
CGRAOmp::LoopDependencyInfo::lc_dep_begin
dep_iterator lc_dep_begin()
Definition: LoopDependencyAnalysis.hpp:227
CGRAOmp::LoopDependencyInfo::dep_iterator
DepList::iterator dep_iterator
Definition: LoopDependencyAnalysis.hpp:186
CGRAOmp::MemoryLoopDependency::getLoad
LoadInst * getLoad()
Definition: LoopDependencyAnalysis.hpp:141
CGRAOmp::LoopDependencyInfo::getNumIdvDep
int getNumIdvDep()
Definition: LoopDependencyAnalysis.hpp:203
CGRAOmp::LoopDependency::DepKind
DepKind
Definition: LoopDependencyAnalysis.hpp:53
CGRAOmp::LoopDependencyInfo::lc_deps
iterator_range< dep_iterator > lc_deps()
Definition: LoopDependencyAnalysis.hpp:233
CGRAOmp
Definition: AGVerifyPass.hpp:50
CGRAOmp::LoopDependencyInfo::indvar_dep_list
DepList indvar_dep_list
Definition: LoopDependencyAnalysis.hpp:246
CGRAOmp::SimpleLoopDependency
A derived class from LoopDependency for loop carried dependency via register.
Definition: LoopDependencyAnalysis.hpp:117
CGRAOmp::LoopDependencyInfo::idv_dep_begin
dep_iterator idv_dep_begin()
Definition: LoopDependencyAnalysis.hpp:189
CGRAOmp::MemoryLoopDependency::getStore
StoreInst * getStore()
Definition: LoopDependencyAnalysis.hpp:145
CGRAOmp::LoopDependencyInfo::add_mem_dep
void add_mem_dep(MemoryLoopDependency *D)
Definition: LoopDependencyAnalysis.hpp:218
CGRAOmp::LoopDependencyInfo::mem_dep_end
dep_iterator mem_dep_end()
Definition: LoopDependencyAnalysis.hpp:211
CGRAOmp::MemoryLoopDependency::load
LoadInst * load
Definition: LoopDependencyAnalysis.hpp:153
CGRAOmp::LoopDependencyInfo
LoopDependency analysis result.
Definition: LoopDependencyAnalysis.hpp:182
CGRAOmp::LoopDependencyInfo::add_dep
void add_dep(SimpleLoopDependency *D)
Definition: LoopDependencyAnalysis.hpp:237
CGRAOmp::LoopDependencyInfo::lc_dep_end
dep_iterator lc_dep_end()
Definition: LoopDependencyAnalysis.hpp:230
CGRAOmp::MemoryLoopDependency::store
StoreInst * store
Definition: LoopDependencyAnalysis.hpp:154
CGRAOmp::MemoryLoopDependency::MemoryLoopDependency
MemoryLoopDependency(StoreInst *store, LoadInst *load, int distance)
Definition: LoopDependencyAnalysis.hpp:135
CGRAOmp::LoopDependencyInfo::add_idv_dep
void add_idv_dep(InductionVariableDependency *D)
Definition: LoopDependencyAnalysis.hpp:199
CGRAOmp::LoopDependency::correspond
bool correspond(PHINode *P)
Definition: LoopDependencyAnalysis.hpp:75
CGRAOmp::LoopDependency::getPhi
PHINode * getPhi()
Definition: LoopDependencyAnalysis.hpp:95
CGRAOmp::LoopDependencyInfo::getNumDep
int getNumDep()
Definition: LoopDependencyAnalysis.hpp:241
CGRAOmp::MemoryLoopDependency
A derived class from LoopDependency for loop carried dependency via memory access.
Definition: LoopDependencyAnalysis.hpp:133
CGRAOmp::InductionVariableDependency
A derived class from LoopDependency for loop carried dependency associated with loop induction variab...
Definition: LoopDependencyAnalysis.hpp:162
CGRAOmp::LoopDependencyInfo::mem_deps
iterator_range< dep_iterator > mem_deps()
Definition: LoopDependencyAnalysis.hpp:214
CGRAOmp::LoopDependency::getDef
Value * getDef()
Definition: LoopDependencyAnalysis.hpp:83
CGRAOmp::LoopDependency::getDefInst
Instruction * getDefInst()
Definition: LoopDependencyAnalysis.hpp:87
CGRAOmp::InductionVariableDependency::step
Value * step
Definition: LoopDependencyAnalysis.hpp:174
CGRAOmp::MemoryLoopDependency::classof
static bool classof(const LoopDependency *LD)
Definition: LoopDependencyAnalysis.hpp:149
CGRAOmp::LoopDependency::distance
int distance
Definition: LoopDependencyAnalysis.hpp:109
CGRAOmp::LoopDependency::LoopDependency
LoopDependency(DepKind kind, Value *def, Value *init, PHINode *phi, int distance)
Construct a new Loop Dependency object for normal loop carried dependency.
Definition: LoopDependencyAnalysis.hpp:66
CGRAOmp::LoopDependency::getKind
DepKind getKind() const
Definition: LoopDependencyAnalysis.hpp:71
CGRAOmp::LoopDependencyInfo::lc_dep_list
DepList lc_dep_list
Definition: LoopDependencyAnalysis.hpp:248
CGRAOmp::LoopDependencyInfo::getNumMemDep
int getNumMemDep()
Definition: LoopDependencyAnalysis.hpp:222
CGRAOmp::SimpleLoopDependency::SimpleLoopDependency
SimpleLoopDependency(Instruction *def, Value *init, PHINode *phi)
Definition: LoopDependencyAnalysis.hpp:119
CGRAOmp::LoopDependencyAnalysisPass
A loop pass to analyze memory access for decoupling.
Definition: LoopDependencyAnalysis.hpp:256