1 //===-- DependenceAnalyzer.cpp - DependenceAnalyzer ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "ModuloSched"
16 #include "DependenceAnalyzer.h"
17 #include "llvm/Type.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Constants.h"
26 /// Create ModuloSchedulingPass
27 FunctionPass *createDependenceAnalyzer() {
28 return new DependenceAnalyzer();
32 Statistic<> NoDeps("depanalyzer-nodeps", "Number of dependences eliminated");
33 Statistic<> NumDeps("depanalyzer-deps",
34 "Number of dependences could not eliminate");
35 Statistic<> AdvDeps("depanalyzer-advdeps",
36 "Number of dependences using advanced techniques");
38 bool DependenceAnalyzer::runOnFunction(Function &F) {
39 AA = &getAnalysis<AliasAnalysis>();
40 TD = &getAnalysis<TargetData>();
41 SE = &getAnalysis<ScalarEvolution>();
46 static RegisterAnalysis<DependenceAnalyzer>X("depanalyzer",
47 "Dependence Analyzer");
49 // - Get inter and intra dependences between loads and stores
51 // Overview of Method:
52 // Step 1: Use alias analysis to determine dependencies if values are loop
54 // Step 2: If pointers are not GEP, then there is a dependence.
55 // Step 3: Compare GEP base pointers with AA. If no alias, no dependence.
56 // If may alias, then add a dependence. If must alias, then analyze
58 // Step 4: do advanced analysis
59 void DependenceAnalyzer::AnalyzeDeps(Value *val, Value *val2, bool valLoad,
61 std::vector<Dependence> &deps,
65 bool loopInvariant = true;
67 //Check if both are instructions and prove not loop invariant if possible
68 if(Instruction *valInst = dyn_cast<Instruction>(val))
69 if(valInst->getParent() == BB)
70 loopInvariant = false;
71 if(Instruction *val2Inst = dyn_cast<Instruction>(val2))
72 if(val2Inst->getParent() == BB)
73 loopInvariant = false;
76 //If Loop invariant, let AA decide
78 if(AA->alias(val, (unsigned)TD->getTypeSize(val->getType()),
79 val2,(unsigned)TD->getTypeSize(val2->getType()))
80 != AliasAnalysis::NoAlias) {
81 createDep(deps, valLoad, val2Load, srcBeforeDest);
88 //Otherwise, continue with step 2
90 GetElementPtrInst *GP = dyn_cast<GetElementPtrInst>(val);
91 GetElementPtrInst *GP2 = dyn_cast<GetElementPtrInst>(val2);
93 //If both are not GP instructions, we can not do further analysis
95 createDep(deps, valLoad, val2Load, srcBeforeDest);
100 //Otherwise, compare GEP bases (op #0) with Alias Analysis
102 Value *GPop = GP->getOperand(0);
103 Value *GP2op = GP2->getOperand(0);
104 int alias = AA->alias(GPop, (unsigned)TD->getTypeSize(GPop->getType()),
105 GP2op,(unsigned)TD->getTypeSize(GP2op->getType()));
108 if(alias == AliasAnalysis::MustAlias) {
109 //Further dep analysis to do
110 advancedDepAnalysis(GP, GP2, valLoad, val2Load, deps, srcBeforeDest);
113 else if(alias == AliasAnalysis::MayAlias) {
114 createDep(deps, valLoad, val2Load, srcBeforeDest);
116 //Otherwise no dependence since there is no alias
122 // advancedDepAnalysis - Do advanced data dependence tests
123 void DependenceAnalyzer::advancedDepAnalysis(GetElementPtrInst *gp1,
124 GetElementPtrInst *gp2,
127 std::vector<Dependence> &deps,
128 bool srcBeforeDest) {
130 //Check if both GEPs are in a simple form: 3 ops, constant 0 as second arg
131 if(gp1->getNumOperands() != 3 || gp2->getNumOperands() != 3) {
132 createDep(deps, valLoad, val2Load, srcBeforeDest);
136 //Check second arg is constant 0
138 if(Constant *c1 = dyn_cast<Constant>(gp1->getOperand(1)))
139 if(Constant *c2 = dyn_cast<Constant>(gp2->getOperand(1)))
140 if(c1->isNullValue() && c2->isNullValue())
144 createDep(deps, valLoad, val2Load, srcBeforeDest);
149 Value *Gep1Idx = gp1->getOperand(2);
150 Value *Gep2Idx = gp2->getOperand(2);
152 if(CastInst *c1 = dyn_cast<CastInst>(Gep1Idx))
153 Gep1Idx = c1->getOperand(0);
154 if(CastInst *c2 = dyn_cast<CastInst>(Gep2Idx))
155 Gep2Idx = c2->getOperand(0);
157 //Get SCEV for each index into the area
158 SCEVHandle SV1 = SE->getSCEV(Gep1Idx);
159 SCEVHandle SV2 = SE->getSCEV(Gep2Idx);
161 //Now handle special cases of dependence analysis
162 //SV1->print(std::cerr);
164 //SV2->print(std::cerr);
167 //Check if we have an SCEVAddExpr, cause we can only handle those
168 SCEVAddRecExpr *SVAdd1 = dyn_cast<SCEVAddRecExpr>(SV1);
169 SCEVAddRecExpr *SVAdd2 = dyn_cast<SCEVAddRecExpr>(SV2);
171 //Default to having a dependence since we can't analyze further
172 if(!SVAdd1 || !SVAdd2) {
173 createDep(deps, valLoad, val2Load, srcBeforeDest);
177 //Check if not Affine, we can't handle those
178 if(!SVAdd1->isAffine( ) || !SVAdd2->isAffine()) {
179 createDep(deps, valLoad, val2Load, srcBeforeDest);
183 //We know the SCEV is in the form A + B*x, check that B is the same for both
184 SCEVConstant *B1 = dyn_cast<SCEVConstant>(SVAdd1->getOperand(1));
185 SCEVConstant *B2 = dyn_cast<SCEVConstant>(SVAdd2->getOperand(1));
187 if(B1->getValue() != B2->getValue()) {
188 createDep(deps, valLoad, val2Load, srcBeforeDest);
192 if(B1->getValue()->getRawValue() != 1 || B2->getValue()->getRawValue() != 1) {
193 createDep(deps, valLoad, val2Load, srcBeforeDest);
198 SCEVConstant *A1 = dyn_cast<SCEVConstant>(SVAdd1->getOperand(0));
199 SCEVConstant *A2 = dyn_cast<SCEVConstant>(SVAdd2->getOperand(0));
201 //Come back and deal with nested SCEV!
203 createDep(deps, valLoad, val2Load, srcBeforeDest);
207 //If equal, create dep as normal
208 if(A1->getValue() == A2->getValue()) {
209 createDep(deps, valLoad, val2Load, srcBeforeDest);
212 //Eliminate a dep if this is a intra dep
213 else if(srcBeforeDest) {
218 //Find constant index difference
219 int diff = A1->getValue()->getRawValue() - A2->getValue()->getRawValue();
220 //std::cerr << diff << "\n";
225 createDep(deps, valLoad, val2Load, srcBeforeDest, diff);
227 //assert(diff > 0 && "Expected diff to be greater then 0");
230 // Create dependences once its determined these two instructions
231 // references the same memory
232 void DependenceAnalyzer::createDep(std::vector<Dependence> &deps,
233 bool valLoad, bool val2Load,
234 bool srcBeforeDest, int diff) {
236 //If the source instruction occurs after the destination instruction
237 //(execution order), then this dependence is across iterations
238 if(!srcBeforeDest && (diff==0))
242 if(valLoad && !val2Load) {
245 deps.push_back(Dependence(diff, Dependence::AntiDep));
247 deps.push_back(Dependence(diff, Dependence::TrueDep));
252 else if(!valLoad && val2Load) {
255 deps.push_back(Dependence(diff, Dependence::TrueDep));
257 deps.push_back(Dependence(diff, Dependence::AntiDep));
260 //If store/store pair
261 else if(!valLoad && !val2Load) {
263 deps.push_back(Dependence(diff, Dependence::OutputDep));
270 //Get Dependence Info for a pair of Instructions
271 DependenceResult DependenceAnalyzer::getDependenceInfo(Instruction *inst1,
273 bool srcBeforeDest) {
274 std::vector<Dependence> deps;
276 DEBUG(std::cerr << "Inst1: " << *inst1 << "\n");
277 DEBUG(std::cerr << "Inst2: " << *inst2 << "\n");
281 return DependenceResult(deps);
283 if(LoadInst *ldInst = dyn_cast<LoadInst>(inst1)) {
285 if(StoreInst *stInst = dyn_cast<StoreInst>(inst2))
286 AnalyzeDeps(ldInst->getOperand(0), stInst->getOperand(1),
287 true, false, deps, ldInst->getParent(), srcBeforeDest);
289 else if(StoreInst *stInst = dyn_cast<StoreInst>(inst1)) {
291 if(LoadInst *ldInst = dyn_cast<LoadInst>(inst2))
292 AnalyzeDeps(stInst->getOperand(1), ldInst->getOperand(0), false, true,
293 deps, ldInst->getParent(), srcBeforeDest);
295 else if(StoreInst *stInst2 = dyn_cast<StoreInst>(inst2))
296 AnalyzeDeps(stInst->getOperand(1), stInst2->getOperand(1), false, false,
297 deps, stInst->getParent(), srcBeforeDest);
300 assert(0 && "Expected a load or a store\n");
302 DependenceResult dr = DependenceResult(deps);