aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/SparcV9/ModuloScheduling/DependenceAnalyzer.cpp
blob: d0b5db304a1db262d7c8e639d8fc41f08b2f1d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//===-- DependenceAnalyzer.cpp - DependenceAnalyzer  ------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//
//
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "ModuloSched"

#include "DependenceAnalyzer.h"
#include "llvm/Type.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Constants.h"

using namespace llvm;

namespace llvm {

  /// Create ModuloSchedulingPass
  FunctionPass *createDependenceAnalyzer() {
    return new DependenceAnalyzer(); 
  }
}

Statistic<> NoDeps("depanalyzer-nodeps", "Number of dependences eliminated");
Statistic<> NumDeps("depanalyzer-deps", 
		    "Number of dependences could not eliminate");
Statistic<> AdvDeps("depanalyzer-advdeps", 
		    "Number of dependences using advanced techniques");

bool DependenceAnalyzer::runOnFunction(Function &F) {
  AA = &getAnalysis<AliasAnalysis>();
  TD = &getAnalysis<TargetData>();
  SE = &getAnalysis<ScalarEvolution>();

  return  false;
}

static RegisterAnalysis<DependenceAnalyzer>X("depanalyzer", 
					     "Dependence Analyzer");
 
//  - Get inter and intra dependences between loads and stores
//
// Overview of Method: 
// Step 1: Use alias analysis to determine dependencies if values are loop 
//       invariant 
// Step 2: If pointers are not GEP, then there is a dependence.  
// Step 3: Compare GEP base pointers with AA. If no alias, no dependence. 
//         If may alias, then add a dependence. If must alias, then analyze 
//         further (Step 4) 
// Step 4: do advanced analysis
void DependenceAnalyzer::AnalyzeDeps(Value *val, Value *val2, bool valLoad, 
				     bool val2Load, 
				     std::vector<Dependence> &deps, 
				     BasicBlock *BB, 
				     bool srcBeforeDest) {
    
  bool loopInvariant = true;

  //Check if both are instructions and prove not loop invariant if possible
  if(Instruction *valInst = dyn_cast<Instruction>(val))
    if(valInst->getParent() == BB)
      loopInvariant = false;
  if(Instruction *val2Inst = dyn_cast<Instruction>(val2))
    if(val2Inst->getParent() == BB)
      loopInvariant = false;
   
    
  //If Loop invariant, let AA decide
  if(loopInvariant) {
    if(AA->alias(val, (unsigned)TD->getTypeSize(val->getType()),
		 val2,(unsigned)TD->getTypeSize(val2->getType()))
       != AliasAnalysis::NoAlias) {
      createDep(deps, valLoad, val2Load, srcBeforeDest);
    }
    else
      ++NoDeps;
    return;
  }
    
  //Otherwise, continue with step 2

  GetElementPtrInst *GP = dyn_cast<GetElementPtrInst>(val);
  GetElementPtrInst *GP2 = dyn_cast<GetElementPtrInst>(val2);

  //If both are not GP instructions, we can not do further analysis
  if(!GP || !GP2) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }


  //Otherwise, compare GEP bases (op #0) with Alias Analysis

  Value *GPop = GP->getOperand(0);
  Value *GP2op = GP2->getOperand(0);
  int alias = AA->alias(GPop, (unsigned)TD->getTypeSize(GPop->getType()),
			GP2op,(unsigned)TD->getTypeSize(GP2op->getType()));


  if(alias == AliasAnalysis::MustAlias) {
    //Further dep analysis to do
    advancedDepAnalysis(GP, GP2, valLoad, val2Load, deps, srcBeforeDest);
    ++AdvDeps;
  }
  else if(alias == AliasAnalysis::MayAlias) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
  }
  //Otherwise no dependence since there is no alias
  else
    ++NoDeps;
}


// advancedDepAnalysis - Do advanced data dependence tests
void DependenceAnalyzer::advancedDepAnalysis(GetElementPtrInst *gp1, 
					     GetElementPtrInst *gp2,
					     bool valLoad,
					     bool val2Load,
					     std::vector<Dependence> &deps,
					     bool srcBeforeDest) {

  //Check if both GEPs are in a simple form: 3 ops, constant 0 as second arg
  if(gp1->getNumOperands() != 3 || gp2->getNumOperands() != 3) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }

  //Check second arg is constant 0
  bool GPok = false;
  if(Constant *c1 = dyn_cast<Constant>(gp1->getOperand(1)))
    if(Constant *c2 = dyn_cast<Constant>(gp2->getOperand(1)))
      if(c1->isNullValue() && c2->isNullValue())
	GPok = true;
  
  if(!GPok) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;

  }

  Value *Gep1Idx = gp1->getOperand(2);
  Value *Gep2Idx = gp2->getOperand(2);

  if(CastInst *c1 = dyn_cast<CastInst>(Gep1Idx))
    Gep1Idx = c1->getOperand(0);
  if(CastInst *c2 = dyn_cast<CastInst>(Gep2Idx))
    Gep2Idx = c2->getOperand(0);
  
  //Get SCEV for each index into the area
  SCEVHandle SV1 = SE->getSCEV(Gep1Idx);
  SCEVHandle SV2 = SE->getSCEV(Gep2Idx);

  //Now handle special cases of dependence analysis
  //SV1->print(std::cerr);
  //std::cerr << "\n";
  //SV2->print(std::cerr);
  //std::cerr << "\n";

  //Check if we have an SCEVAddExpr, cause we can only handle those
  SCEVAddRecExpr *SVAdd1 = dyn_cast<SCEVAddRecExpr>(SV1);
  SCEVAddRecExpr *SVAdd2 = dyn_cast<SCEVAddRecExpr>(SV2);

  //Default to having a dependence since we can't analyze further
  if(!SVAdd1 || !SVAdd2) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }

  //Check if not Affine, we can't handle those
  if(!SVAdd1->isAffine( ) || !SVAdd2->isAffine()) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }

  //We know the SCEV is in the form A + B*x, check that B is the same for both
  SCEVConstant *B1 = dyn_cast<SCEVConstant>(SVAdd1->getOperand(1));
  SCEVConstant *B2 = dyn_cast<SCEVConstant>(SVAdd2->getOperand(1));

  if(B1->getValue() != B2->getValue()) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }
  
  if(B1->getValue()->getRawValue() != 1 || B2->getValue()->getRawValue() != 1) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }


  SCEVConstant *A1 = dyn_cast<SCEVConstant>(SVAdd1->getOperand(0));
  SCEVConstant *A2 = dyn_cast<SCEVConstant>(SVAdd2->getOperand(0));

  //Come back and deal with nested SCEV!
  if(!A1 || !A2) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }

  //If equal, create dep as normal
  if(A1->getValue() == A2->getValue()) {
    createDep(deps, valLoad, val2Load, srcBeforeDest);
    return;
  }
  //Eliminate a dep if this is a intra dep
  else if(srcBeforeDest) {
    ++NoDeps;
    return;
  }
  
  //Find constant index difference
  int diff = A1->getValue()->getRawValue() - A2->getValue()->getRawValue();
  //std::cerr << diff << "\n";
  if(diff > 5)
    diff = 2;

  if(diff > 0)
    createDep(deps, valLoad, val2Load, srcBeforeDest, diff);
  
  //assert(diff > 0 && "Expected diff to be greater then 0");
}

// Create dependences once its determined these two instructions
// references the same memory
void DependenceAnalyzer::createDep(std::vector<Dependence> &deps, 
				   bool valLoad, bool val2Load, 
				   bool srcBeforeDest, int diff) {

  //If the source instruction occurs after the destination instruction
  //(execution order), then this dependence is across iterations
  if(!srcBeforeDest && (diff==0))
    diff = 1;

  //If load/store pair
  if(valLoad && !val2Load) {
    if(srcBeforeDest) 
      //Anti Dep
      deps.push_back(Dependence(diff, Dependence::AntiDep));
    else
      deps.push_back(Dependence(diff, Dependence::TrueDep));

    ++NumDeps;
  }
  //If store/load pair
  else if(!valLoad && val2Load) {
    if(srcBeforeDest) 
      //True Dep
      deps.push_back(Dependence(diff, Dependence::TrueDep));
    else
      deps.push_back(Dependence(diff, Dependence::AntiDep));
    ++NumDeps;
  }
  //If store/store pair
  else if(!valLoad && !val2Load) {
    //True Dep
    deps.push_back(Dependence(diff, Dependence::OutputDep));
    ++NumDeps;
  }
}