blob: ccd5400df8d00ff378475f04404cb8d4f7cf679d [file] [log] [blame]
Nick Lewyckya11e2eb2008-06-30 00:04:21 +00001//===- 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//===----------------------------------------------------------------------===//
Chris Lattner944fac72008-08-23 22:23:09 +00009//
10// FIXME: What does this do?
11//
12//===----------------------------------------------------------------------===//
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000013
14#define DEBUG_TYPE "loopvr"
15#include "llvm/Analysis/LoopVR.h"
16#include "llvm/Constants.h"
17#include "llvm/Instructions.h"
Owen Anderson76f600b2009-07-06 22:37:39 +000018#include "llvm/LLVMContext.h"
Dan Gohman03ee68a2009-07-13 22:19:41 +000019#include "llvm/Analysis/LoopInfo.h"
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000020#include "llvm/Analysis/ScalarEvolutionExpressions.h"
21#include "llvm/Assembly/Writer.h"
22#include "llvm/Support/CFG.h"
23#include "llvm/Support/Debug.h"
Chris Lattner944fac72008-08-23 22:23:09 +000024#include "llvm/Support/raw_ostream.h"
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000025using namespace llvm;
26
27char LoopVR::ID = 0;
Dan Gohman2ba682c2009-03-23 15:50:52 +000028static RegisterPass<LoopVR> X("loopvr", "Loop Value Ranges", false, true);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000029
30/// getRange - determine the range for a particular SCEV within a given Loop
Dan Gohman0bba49c2009-07-07 17:06:11 +000031ConstantRange LoopVR::getRange(const SCEV *S, Loop *L, ScalarEvolution &SE) {
32 const SCEV *T = SE.getBackedgeTakenCount(L);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000033 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
Dan Gohman0bba49c2009-07-07 17:06:11 +000041ConstantRange LoopVR::getRange(const SCEV *S, const SCEV *T, ScalarEvolution &SE){
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000042
Dan Gohmanb40c2362009-04-18 17:57:20 +000043 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000044 return ConstantRange(C->getValue()->getValue());
Owen Andersone922c022009-07-22 00:24:57 +000045
46 LLVMContext &Context = SE.getContext();
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000047
48 ConstantRange FullSet(cast<IntegerType>(S->getType())->getBitWidth(), true);
49
50 // {x,+,y,+,...z}. We detect overflow by checking the size of the set after
51 // summing the upper and lower.
Dan Gohmanb40c2362009-04-18 17:57:20 +000052 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000053 ConstantRange X = getRange(Add->getOperand(0), T, SE);
54 if (X.isFullSet()) return FullSet;
55 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) {
56 ConstantRange Y = getRange(Add->getOperand(i), T, SE);
57 if (Y.isFullSet()) return FullSet;
58
59 APInt Spread_X = X.getSetSize(), Spread_Y = Y.getSetSize();
60 APInt NewLower = X.getLower() + Y.getLower();
61 APInt NewUpper = X.getUpper() + Y.getUpper() - 1;
62 if (NewLower == NewUpper)
63 return FullSet;
64
65 X = ConstantRange(NewLower, NewUpper);
66 if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
67 return FullSet; // we've wrapped, therefore, full set.
68 }
69 return X;
70 }
71
72 // {x,*,y,*,...,z}. In order to detect overflow, we use k*bitwidth where
73 // k is the number of terms being multiplied.
Dan Gohmanb40c2362009-04-18 17:57:20 +000074 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000075 ConstantRange X = getRange(Mul->getOperand(0), T, SE);
76 if (X.isFullSet()) return FullSet;
77
Owen Andersone922c022009-07-22 00:24:57 +000078 const IntegerType *Ty = Context.getIntegerType(X.getBitWidth());
79 const IntegerType *ExTy = Context.getIntegerType(X.getBitWidth() *
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000080 Mul->getNumOperands());
81 ConstantRange XExt = X.zeroExtend(ExTy->getBitWidth());
82
83 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) {
84 ConstantRange Y = getRange(Mul->getOperand(i), T, SE);
85 if (Y.isFullSet()) return FullSet;
86
87 ConstantRange YExt = Y.zeroExtend(ExTy->getBitWidth());
88 XExt = ConstantRange(XExt.getLower() * YExt.getLower(),
89 ((XExt.getUpper()-1) * (YExt.getUpper()-1)) + 1);
90 }
91 return XExt.truncate(Ty->getBitWidth());
92 }
93
94 // X smax Y smax ... Z is: range(smax(X_smin, Y_smin, ..., Z_smin),
95 // smax(X_smax, Y_smax, ..., Z_smax))
96 // It doesn't matter if one of the SCEVs has FullSet because we're taking
97 // a maximum of the minimums across all of them.
Dan Gohmanb40c2362009-04-18 17:57:20 +000098 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +000099 ConstantRange X = getRange(SMax->getOperand(0), T, SE);
100 if (X.isFullSet()) return FullSet;
101
102 APInt smin = X.getSignedMin(), smax = X.getSignedMax();
103 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) {
104 ConstantRange Y = getRange(SMax->getOperand(i), T, SE);
105 smin = APIntOps::smax(smin, Y.getSignedMin());
106 smax = APIntOps::smax(smax, Y.getSignedMax());
107 }
108 if (smax + 1 == smin) return FullSet;
109 return ConstantRange(smin, smax + 1);
110 }
111
112 // X umax Y umax ... Z is: range(umax(X_umin, Y_umin, ..., Z_umin),
113 // umax(X_umax, Y_umax, ..., Z_umax))
114 // It doesn't matter if one of the SCEVs has FullSet because we're taking
115 // a maximum of the minimums across all of them.
Dan Gohmanb40c2362009-04-18 17:57:20 +0000116 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000117 ConstantRange X = getRange(UMax->getOperand(0), T, SE);
118 if (X.isFullSet()) return FullSet;
119
120 APInt umin = X.getUnsignedMin(), umax = X.getUnsignedMax();
121 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) {
122 ConstantRange Y = getRange(UMax->getOperand(i), T, SE);
123 umin = APIntOps::umax(umin, Y.getUnsignedMin());
124 umax = APIntOps::umax(umax, Y.getUnsignedMax());
125 }
126 if (umax + 1 == umin) return FullSet;
127 return ConstantRange(umin, umax + 1);
128 }
129
130 // L udiv R. Luckily, there's only ever 2 sides to a udiv.
Dan Gohmanb40c2362009-04-18 17:57:20 +0000131 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000132 ConstantRange L = getRange(UDiv->getLHS(), T, SE);
133 ConstantRange R = getRange(UDiv->getRHS(), T, SE);
134 if (L.isFullSet() && R.isFullSet()) return FullSet;
135
136 if (R.getUnsignedMax() == 0) {
137 // RHS must be single-element zero. Return an empty set.
138 return ConstantRange(R.getBitWidth(), false);
139 }
140
141 APInt Lower = L.getUnsignedMin().udiv(R.getUnsignedMax());
142
143 APInt Upper;
144
145 if (R.getUnsignedMin() == 0) {
146 // Just because it contains zero, doesn't mean it will also contain one.
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000147 ConstantRange NotZero(APInt(L.getBitWidth(), 1),
148 APInt::getNullValue(L.getBitWidth()));
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000149 R = R.intersectWith(NotZero);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000150 }
151
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000152 // But, the intersection might still include zero. If it does, then we know
153 // it also included one.
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000154 if (R.contains(APInt::getNullValue(L.getBitWidth())))
155 Upper = L.getUnsignedMax();
156 else
157 Upper = L.getUnsignedMax().udiv(R.getUnsignedMin());
158
159 return ConstantRange(Lower, Upper);
160 }
161
162 // ConstantRange already implements the cast operators.
163
Dan Gohmanb40c2362009-04-18 17:57:20 +0000164 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000165 T = SE.getTruncateOrZeroExtend(T, ZExt->getOperand()->getType());
166 ConstantRange X = getRange(ZExt->getOperand(), T, SE);
167 return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
168 }
169
Dan Gohmanb40c2362009-04-18 17:57:20 +0000170 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000171 T = SE.getTruncateOrZeroExtend(T, SExt->getOperand()->getType());
172 ConstantRange X = getRange(SExt->getOperand(), T, SE);
173 return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
174 }
175
Dan Gohmanb40c2362009-04-18 17:57:20 +0000176 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000177 T = SE.getTruncateOrZeroExtend(T, Trunc->getOperand()->getType());
178 ConstantRange X = getRange(Trunc->getOperand(), T, SE);
179 if (X.isFullSet()) return FullSet;
180 return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
181 }
182
Dan Gohmanb40c2362009-04-18 17:57:20 +0000183 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
184 const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000185 if (!Trip) return FullSet;
186
187 if (AddRec->isAffine()) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000188 const SCEV *StartHandle = AddRec->getStart();
189 const SCEV *StepHandle = AddRec->getOperand(1);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000190
Dan Gohmanb40c2362009-04-18 17:57:20 +0000191 const SCEVConstant *Step = dyn_cast<SCEVConstant>(StepHandle);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000192 if (!Step) return FullSet;
193
194 uint32_t ExWidth = 2 * Trip->getValue()->getBitWidth();
195 APInt TripExt = Trip->getValue()->getValue(); TripExt.zext(ExWidth);
196 APInt StepExt = Step->getValue()->getValue(); StepExt.zext(ExWidth);
197 if ((TripExt * StepExt).ugt(APInt::getLowBitsSet(ExWidth, ExWidth >> 1)))
198 return FullSet;
199
Dan Gohman0bba49c2009-07-07 17:06:11 +0000200 const SCEV *EndHandle = SE.getAddExpr(StartHandle,
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000201 SE.getMulExpr(T, StepHandle));
Dan Gohmanb40c2362009-04-18 17:57:20 +0000202 const SCEVConstant *Start = dyn_cast<SCEVConstant>(StartHandle);
203 const SCEVConstant *End = dyn_cast<SCEVConstant>(EndHandle);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000204 if (!Start || !End) return FullSet;
205
206 const APInt &StartInt = Start->getValue()->getValue();
207 const APInt &EndInt = End->getValue()->getValue();
208 const APInt &StepInt = Step->getValue()->getValue();
209
210 if (StepInt.isNegative()) {
211 if (EndInt == StartInt + 1) return FullSet;
212 return ConstantRange(EndInt, StartInt + 1);
213 } else {
214 if (StartInt == EndInt + 1) return FullSet;
215 return ConstantRange(StartInt, EndInt + 1);
216 }
217 }
218 }
219
220 // TODO: non-affine addrec, udiv, SCEVUnknown (narrowed from elsewhere)?
221
222 return FullSet;
223}
224
Dan Gohman03ee68a2009-07-13 22:19:41 +0000225void LoopVR::getAnalysisUsage(AnalysisUsage &AU) const {
226 AU.addRequiredTransitive<LoopInfo>();
227 AU.addRequiredTransitive<ScalarEvolution>();
228 AU.setPreservesAll();
229}
230
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000231bool LoopVR::runOnFunction(Function &F) { Map.clear(); return false; }
232
233void LoopVR::print(std::ostream &os, const Module *) const {
Chris Lattner944fac72008-08-23 22:23:09 +0000234 raw_os_ostream OS(os);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000235 for (std::map<Value *, ConstantRange *>::const_iterator I = Map.begin(),
236 E = Map.end(); I != E; ++I) {
Chris Lattner944fac72008-08-23 22:23:09 +0000237 OS << *I->first << ": " << *I->second << '\n';
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000238 }
239}
240
241void LoopVR::releaseMemory() {
242 for (std::map<Value *, ConstantRange *>::iterator I = Map.begin(),
243 E = Map.end(); I != E; ++I) {
244 delete I->second;
245 }
246
247 Map.clear();
248}
249
250ConstantRange LoopVR::compute(Value *V) {
251 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
252 return ConstantRange(CI->getValue());
253
254 Instruction *I = dyn_cast<Instruction>(V);
255 if (!I)
256 return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
257
258 LoopInfo &LI = getAnalysis<LoopInfo>();
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000259
260 Loop *L = LI.getLoopFor(I->getParent());
Torok Edwinc83889a2008-10-27 10:18:45 +0000261 if (!L || L->isLoopInvariant(I))
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000262 return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
263
Torok Edwinc83889a2008-10-27 10:18:45 +0000264 ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
265
Dan Gohman0bba49c2009-07-07 17:06:11 +0000266 const SCEV *S = SE.getSCEV(I);
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000267 if (isa<SCEVUnknown>(S) || isa<SCEVCouldNotCompute>(S))
268 return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
269
270 return ConstantRange(getRange(S, L, SE));
271}
272
273ConstantRange LoopVR::get(Value *V) {
274 std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
275 if (I == Map.end()) {
276 ConstantRange *CR = new ConstantRange(compute(V));
277 Map[V] = CR;
278 return *CR;
279 }
280
281 return *I->second;
282}
283
284void LoopVR::remove(Value *V) {
285 std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
286 if (I != Map.end()) {
287 delete I->second;
288 Map.erase(I);
289 }
290}
291
292void LoopVR::narrow(Value *V, const ConstantRange &CR) {
293 if (CR.isFullSet()) return;
294
295 std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
296 if (I == Map.end())
297 Map[V] = new ConstantRange(CR);
298 else
Nick Lewycky3a4a8842009-07-18 06:34:42 +0000299 Map[V] = new ConstantRange(Map[V]->intersectWith(CR));
Nick Lewyckya11e2eb2008-06-30 00:04:21 +0000300}