921d6ce06e91bcd6ffab3de76cecb9d3ff0db97c
[oota-llvm.git] / lib / Analysis / LoopVR.cpp
1 //===- LoopVR.cpp - Value Range analysis driven by loop information -------===//
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 // FIXME: What does this do?
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "loopvr"
15 #include "llvm/Analysis/LoopVR.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26
27 char LoopVR::ID = 0;
28 static RegisterPass<LoopVR> X("loopvr", "Loop Value Ranges", false, true);
29
30 /// getRange - determine the range for a particular SCEV within a given Loop
31 ConstantRange LoopVR::getRange(const SCEV *S, Loop *L, ScalarEvolution &SE) {
32   const SCEV *T = SE.getBackedgeTakenCount(L);
33   if (isa<SCEVCouldNotCompute>(T))
34     return ConstantRange(cast<IntegerType>(S->getType())->getBitWidth(), true);
35
36   T = SE.getTruncateOrZeroExtend(T, S->getType());
37   return getRange(S, T, SE);
38 }
39
40 /// getRange - determine the range for a particular SCEV with a given trip count
41 ConstantRange LoopVR::getRange(const SCEV *S, const SCEV *T, ScalarEvolution &SE){
42
43   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
44     return ConstantRange(C->getValue()->getValue());
45     
46   ConstantRange FullSet(cast<IntegerType>(S->getType())->getBitWidth(), true);
47
48   // {x,+,y,+,...z}. We detect overflow by checking the size of the set after
49   // summing the upper and lower.
50   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
51     ConstantRange X = getRange(Add->getOperand(0), T, SE);
52     if (X.isFullSet()) return FullSet;
53     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) {
54       ConstantRange Y = getRange(Add->getOperand(i), T, SE);
55       if (Y.isFullSet()) return FullSet;
56
57       APInt Spread_X = X.getSetSize(), Spread_Y = Y.getSetSize();
58       APInt NewLower = X.getLower() + Y.getLower();
59       APInt NewUpper = X.getUpper() + Y.getUpper() - 1;
60       if (NewLower == NewUpper)
61         return FullSet;
62
63       X = ConstantRange(NewLower, NewUpper);
64       if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
65         return FullSet; // we've wrapped, therefore, full set.
66     }
67     return X;
68   }
69
70   // {x,*,y,*,...,z}. In order to detect overflow, we use k*bitwidth where
71   // k is the number of terms being multiplied.
72   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
73     ConstantRange X = getRange(Mul->getOperand(0), T, SE);
74     if (X.isFullSet()) return FullSet;
75
76     const IntegerType *Ty = IntegerType::get(X.getBitWidth());
77     const IntegerType *ExTy = IntegerType::get(X.getBitWidth() *
78                                                Mul->getNumOperands());
79     ConstantRange XExt = X.zeroExtend(ExTy->getBitWidth());
80
81     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) {
82       ConstantRange Y = getRange(Mul->getOperand(i), T, SE);
83       if (Y.isFullSet()) return FullSet;
84
85       ConstantRange YExt = Y.zeroExtend(ExTy->getBitWidth());
86       XExt = ConstantRange(XExt.getLower() * YExt.getLower(),
87                            ((XExt.getUpper()-1) * (YExt.getUpper()-1)) + 1);
88     }
89     return XExt.truncate(Ty->getBitWidth());
90   }
91
92   // X smax Y smax ... Z is: range(smax(X_smin, Y_smin, ..., Z_smin),
93   //                               smax(X_smax, Y_smax, ..., Z_smax))
94   // It doesn't matter if one of the SCEVs has FullSet because we're taking
95   // a maximum of the minimums across all of them.
96   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
97     ConstantRange X = getRange(SMax->getOperand(0), T, SE);
98     if (X.isFullSet()) return FullSet;
99
100     APInt smin = X.getSignedMin(), smax = X.getSignedMax();
101     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) {
102       ConstantRange Y = getRange(SMax->getOperand(i), T, SE);
103       smin = APIntOps::smax(smin, Y.getSignedMin());
104       smax = APIntOps::smax(smax, Y.getSignedMax());
105     }
106     if (smax + 1 == smin) return FullSet;
107     return ConstantRange(smin, smax + 1);
108   }
109
110   // X umax Y umax ... Z is: range(umax(X_umin, Y_umin, ..., Z_umin),
111   //                               umax(X_umax, Y_umax, ..., Z_umax))
112   // It doesn't matter if one of the SCEVs has FullSet because we're taking
113   // a maximum of the minimums across all of them.
114   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
115     ConstantRange X = getRange(UMax->getOperand(0), T, SE);
116     if (X.isFullSet()) return FullSet;
117
118     APInt umin = X.getUnsignedMin(), umax = X.getUnsignedMax();
119     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) {
120       ConstantRange Y = getRange(UMax->getOperand(i), T, SE);
121       umin = APIntOps::umax(umin, Y.getUnsignedMin());
122       umax = APIntOps::umax(umax, Y.getUnsignedMax());
123     }
124     if (umax + 1 == umin) return FullSet;
125     return ConstantRange(umin, umax + 1);
126   }
127
128   // L udiv R. Luckily, there's only ever 2 sides to a udiv.
129   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
130     ConstantRange L = getRange(UDiv->getLHS(), T, SE);
131     ConstantRange R = getRange(UDiv->getRHS(), T, SE);
132     if (L.isFullSet() && R.isFullSet()) return FullSet;
133
134     if (R.getUnsignedMax() == 0) {
135       // RHS must be single-element zero. Return an empty set.
136       return ConstantRange(R.getBitWidth(), false);
137     }
138
139     APInt Lower = L.getUnsignedMin().udiv(R.getUnsignedMax());
140
141     APInt Upper;
142
143     if (R.getUnsignedMin() == 0) {
144       // Just because it contains zero, doesn't mean it will also contain one.
145       ConstantRange NotZero(APInt(L.getBitWidth(), 1),
146                             APInt::getNullValue(L.getBitWidth()));
147       R = R.intersectWith(NotZero);
148     }
149  
150     // But, the intersection might still include zero. If it does, then we know
151     // it also included one.
152     if (R.contains(APInt::getNullValue(L.getBitWidth())))
153       Upper = L.getUnsignedMax();
154     else
155       Upper = L.getUnsignedMax().udiv(R.getUnsignedMin());
156
157     return ConstantRange(Lower, Upper);
158   }
159
160   // ConstantRange already implements the cast operators.
161
162   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
163     T = SE.getTruncateOrZeroExtend(T, ZExt->getOperand()->getType());
164     ConstantRange X = getRange(ZExt->getOperand(), T, SE);
165     return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
166   }
167
168   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
169     T = SE.getTruncateOrZeroExtend(T, SExt->getOperand()->getType());
170     ConstantRange X = getRange(SExt->getOperand(), T, SE);
171     return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
172   }
173
174   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
175     T = SE.getTruncateOrZeroExtend(T, Trunc->getOperand()->getType());
176     ConstantRange X = getRange(Trunc->getOperand(), T, SE);
177     if (X.isFullSet()) return FullSet;
178     return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
179   }
180
181   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
182     const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
183     if (!Trip) return FullSet;
184
185     if (AddRec->isAffine()) {
186       const SCEV *StartHandle = AddRec->getStart();
187       const SCEV *StepHandle = AddRec->getOperand(1);
188
189       const SCEVConstant *Step = dyn_cast<SCEVConstant>(StepHandle);
190       if (!Step) return FullSet;
191
192       uint32_t ExWidth = 2 * Trip->getValue()->getBitWidth();
193       APInt TripExt = Trip->getValue()->getValue(); TripExt.zext(ExWidth);
194       APInt StepExt = Step->getValue()->getValue(); StepExt.zext(ExWidth);
195       if ((TripExt * StepExt).ugt(APInt::getLowBitsSet(ExWidth, ExWidth >> 1)))
196         return FullSet;
197
198       const SCEV *EndHandle = SE.getAddExpr(StartHandle,
199                                            SE.getMulExpr(T, StepHandle));
200       const SCEVConstant *Start = dyn_cast<SCEVConstant>(StartHandle);
201       const SCEVConstant *End = dyn_cast<SCEVConstant>(EndHandle);
202       if (!Start || !End) return FullSet;
203
204       const APInt &StartInt = Start->getValue()->getValue();
205       const APInt &EndInt = End->getValue()->getValue();
206       const APInt &StepInt = Step->getValue()->getValue();
207
208       if (StepInt.isNegative()) {
209         if (EndInt == StartInt + 1) return FullSet;
210         return ConstantRange(EndInt, StartInt + 1);
211       } else {
212         if (StartInt == EndInt + 1) return FullSet;
213         return ConstantRange(StartInt, EndInt + 1);
214       }
215     }
216   }
217
218   // TODO: non-affine addrec, udiv, SCEVUnknown (narrowed from elsewhere)?
219
220   return FullSet;
221 }
222
223 void LoopVR::getAnalysisUsage(AnalysisUsage &AU) const {
224   AU.addRequiredTransitive<LoopInfo>();
225   AU.addRequiredTransitive<ScalarEvolution>();
226   AU.setPreservesAll();
227 }
228
229 bool LoopVR::runOnFunction(Function &F) { Map.clear(); return false; }
230
231 void LoopVR::print(std::ostream &os, const Module *) const {
232   raw_os_ostream OS(os);
233   for (std::map<Value *, ConstantRange *>::const_iterator I = Map.begin(),
234        E = Map.end(); I != E; ++I) {
235     OS << *I->first << ": " << *I->second << '\n';
236   }
237 }
238
239 void LoopVR::releaseMemory() {
240   for (std::map<Value *, ConstantRange *>::iterator I = Map.begin(),
241        E = Map.end(); I != E; ++I) {
242     delete I->second;
243   }
244
245   Map.clear();  
246 }
247
248 ConstantRange LoopVR::compute(Value *V) {
249   if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
250     return ConstantRange(CI->getValue());
251
252   Instruction *I = dyn_cast<Instruction>(V);
253   if (!I)
254     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
255
256   LoopInfo &LI = getAnalysis<LoopInfo>();
257
258   Loop *L = LI.getLoopFor(I->getParent());
259   if (!L || L->isLoopInvariant(I))
260     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
261
262   ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
263
264   const SCEV *S = SE.getSCEV(I);
265   if (isa<SCEVUnknown>(S) || isa<SCEVCouldNotCompute>(S))
266     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
267
268   return ConstantRange(getRange(S, L, SE));
269 }
270
271 ConstantRange LoopVR::get(Value *V) {
272   std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
273   if (I == Map.end()) {
274     ConstantRange *CR = new ConstantRange(compute(V));
275     Map[V] = CR;
276     return *CR;
277   }
278
279   return *I->second;
280 }
281
282 void LoopVR::remove(Value *V) {
283   std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
284   if (I != Map.end()) {
285     delete I->second;
286     Map.erase(I);
287   }
288 }
289
290 void LoopVR::narrow(Value *V, const ConstantRange &CR) {
291   if (CR.isFullSet()) return;
292
293   std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
294   if (I == Map.end())
295     Map[V] = new ConstantRange(CR);
296   else
297     Map[V] = new ConstantRange(Map[V]->intersectWith(CR));
298 }