blob: fd951af4f2216872fb699423532a34d2217ca3d4 [file] [log] [blame]
Tobias Grosser120db6b2011-11-07 12:58:54 +00001
2#include "polly/Support/SCEVValidator.h"
3
4#include "llvm/Analysis/ScalarEvolution.h"
5#include "llvm/Analysis/ScalarEvolutionExpressions.h"
6#include "llvm/Analysis/RegionInfo.h"
7
Tobias Grosser60b54f12011-11-08 15:41:28 +00008#include <vector>
9
Tobias Grosser120db6b2011-11-07 12:58:54 +000010using namespace llvm;
11
12namespace SCEVType {
13 enum TYPE {INT, PARAM, IV, INVALID};
14}
15
16struct ValidatorResult {
17 SCEVType::TYPE type;
Tobias Grosser60b54f12011-11-08 15:41:28 +000018 std::vector<const SCEV*> Parameters;
Tobias Grosser120db6b2011-11-07 12:58:54 +000019
20 ValidatorResult() : type(SCEVType::INVALID) {};
21
22 ValidatorResult(const ValidatorResult &vres) {
23 type = vres.type;
Tobias Grosser60b54f12011-11-08 15:41:28 +000024 Parameters = vres.Parameters;
Tobias Grosser120db6b2011-11-07 12:58:54 +000025 };
26
27 ValidatorResult(SCEVType::TYPE type) : type(type) {};
Tobias Grosser7ffe4e82011-11-17 12:56:10 +000028 ValidatorResult(SCEVType::TYPE type, const SCEV *Expr) : type(type) {
Tobias Grosser60b54f12011-11-08 15:41:28 +000029 Parameters.push_back(Expr);
30 };
Tobias Grosser120db6b2011-11-07 12:58:54 +000031
32 bool isConstant() {
33 return type == SCEVType::INT || type == SCEVType::PARAM;
34 }
35
36 bool isValid() {
37 return type != SCEVType::INVALID;
38 }
39
40 bool isIV() {
41 return type == SCEVType::IV;
42 }
43
44 bool isINT() {
45 return type == SCEVType::INT;
46 }
Tobias Grosser60b54f12011-11-08 15:41:28 +000047
48 void addParamsFrom(struct ValidatorResult &Source) {
49 Parameters.insert(Parameters.end(), Source.Parameters.begin(),
50 Source.Parameters.end());
51 }
Tobias Grosser120db6b2011-11-07 12:58:54 +000052};
53
54/// Check if a SCEV is valid in a SCoP.
55struct SCEVValidator
56 : public SCEVVisitor<SCEVValidator, struct ValidatorResult> {
57private:
58 const Region *R;
59 ScalarEvolution &SE;
Tobias Grossere5e171e2011-11-10 12:45:03 +000060 const Value *BaseAddress;
Tobias Grosser120db6b2011-11-07 12:58:54 +000061
62public:
63 SCEVValidator(const Region *R, ScalarEvolution &SE,
Tobias Grossere5e171e2011-11-10 12:45:03 +000064 const Value *BaseAddress) : R(R), SE(SE),
Tobias Grosser120db6b2011-11-07 12:58:54 +000065 BaseAddress(BaseAddress) {};
66
67 struct ValidatorResult visitConstant(const SCEVConstant *Constant) {
68 return ValidatorResult(SCEVType::INT);
69 }
70
Tobias Grosser7ffe4e82011-11-17 12:56:10 +000071 struct ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +000072 ValidatorResult Op = visit(Expr->getOperand());
73
74 // We currently do not represent a truncate expression as an affine
75 // expression. If it is constant during Scop execution, we treat it as a
76 // parameter, otherwise we bail out.
77 if (Op.isConstant())
Tobias Grosser60b54f12011-11-08 15:41:28 +000078 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +000079
80 return ValidatorResult (SCEVType::INVALID);
81 }
82
Tobias Grosser7ffe4e82011-11-17 12:56:10 +000083 struct ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +000084 ValidatorResult Op = visit(Expr->getOperand());
85
86 // We currently do not represent a zero extend expression as an affine
87 // expression. If it is constant during Scop execution, we treat it as a
88 // parameter, otherwise we bail out.
89 if (Op.isConstant())
Tobias Grosser60b54f12011-11-08 15:41:28 +000090 return ValidatorResult (SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +000091
92 return ValidatorResult(SCEVType::INVALID);
93 }
94
Tobias Grosser7ffe4e82011-11-17 12:56:10 +000095 struct ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +000096 // We currently allow only signed SCEV expressions. In the case of a
97 // signed value, a sign extend is a noop.
98 //
99 // TODO: Reconsider this when we add support for unsigned values.
100 return visit(Expr->getOperand());
101 }
102
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000103 struct ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000104 ValidatorResult Return(SCEVType::INT);
105
106 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
107 ValidatorResult Op = visit(Expr->getOperand(i));
108
109 if (!Op.isValid())
110 return ValidatorResult(SCEVType::INVALID);
111
112 Return.type = std::max(Return.type, Op.type);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000113 Return.addParamsFrom(Op);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000114 }
115
116 // TODO: Check for NSW and NUW.
117 return Return;
118 }
119
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000120 struct ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000121 ValidatorResult Return(SCEVType::INT);
122
123 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
124 ValidatorResult Op = visit(Expr->getOperand(i));
125
126 if (Op.type == SCEVType::INT)
127 continue;
128
129 if (Op.type == SCEVType::INVALID || Return.type != SCEVType::INT)
130 return ValidatorResult(SCEVType::INVALID);
131
132 Return.type = Op.type;
Tobias Grosser60b54f12011-11-08 15:41:28 +0000133 Return.addParamsFrom(Op);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000134 }
135
136 // TODO: Check for NSW and NUW.
137 return Return;
138 }
139
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000140 struct ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000141 ValidatorResult LHS = visit(Expr->getLHS());
142 ValidatorResult RHS = visit(Expr->getRHS());
143
Tobias Grosser2a7cd942011-11-17 12:56:12 +0000144 // We currently do not represent an unsigned devision as an affine
Tobias Grosser120db6b2011-11-07 12:58:54 +0000145 // expression. If the division is constant during Scop execution we treat it
146 // as a parameter, otherwise we bail out.
147 if (LHS.isConstant() && RHS.isConstant())
Tobias Grosser60b54f12011-11-08 15:41:28 +0000148 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000149
150 return ValidatorResult(SCEVType::INVALID);
151 }
152
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000153 struct ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000154 if (!Expr->isAffine())
155 return ValidatorResult(SCEVType::INVALID);
156
157 ValidatorResult Start = visit(Expr->getStart());
158 ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE));
159
160 if (!Start.isValid() || !Recurrence.isValid() || Recurrence.isIV())
161 return ValidatorResult(SCEVType::INVALID);
162
163
164 if (!R->contains(Expr->getLoop())) {
165 if (Start.isIV())
166 return ValidatorResult(SCEVType::INVALID);
167 else
Tobias Grosser60b54f12011-11-08 15:41:28 +0000168 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000169 }
170
171 if (!Recurrence.isINT())
172 return ValidatorResult(SCEVType::INVALID);
173
Tobias Grosser60b54f12011-11-08 15:41:28 +0000174 ValidatorResult Result(SCEVType::IV);
175 Result.addParamsFrom(Start);
176 return Result;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000177 }
178
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000179 struct ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000180 ValidatorResult Return(SCEVType::INT);
181
182 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
183 ValidatorResult Op = visit(Expr->getOperand(i));
184
185 if (!Op.isValid())
186 return ValidatorResult(SCEVType::INVALID);
187
188 Return.type = std::max(Return.type, Op.type);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000189 Return.addParamsFrom(Op);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000190 }
191
192 return Return;
193 }
194
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000195 struct ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) {
Tobias Grosser60b54f12011-11-08 15:41:28 +0000196 ValidatorResult Return(SCEVType::PARAM);
197
Tobias Grosser120db6b2011-11-07 12:58:54 +0000198 // We do not support unsigned operations. If 'Expr' is constant during Scop
199 // execution we treat this as a parameter, otherwise we bail out.
200 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
201 ValidatorResult Op = visit(Expr->getOperand(i));
202
203 if (!Op.isConstant())
204 return ValidatorResult(SCEVType::INVALID);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000205
206 Return.addParamsFrom(Op);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000207 }
208
Tobias Grosser60b54f12011-11-08 15:41:28 +0000209 return Return;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000210 }
211
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000212 ValidatorResult visitUnknown(const SCEVUnknown *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000213 Value *V = Expr->getValue();
214
215 if (isa<UndefValue>(V))
216 return ValidatorResult(SCEVType::INVALID);
217
Tobias Grosser120db6b2011-11-07 12:58:54 +0000218 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue()))
219 if (R->contains(I))
220 return ValidatorResult(SCEVType::INVALID);
221
Tobias Grossere5e171e2011-11-10 12:45:03 +0000222 if (BaseAddress == V)
223 return ValidatorResult(SCEVType::INVALID);
224
225 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000226 }
227};
228
229namespace polly {
230 bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE,
Tobias Grossere5e171e2011-11-10 12:45:03 +0000231 const Value *BaseAddress) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000232 if (isa<SCEVCouldNotCompute>(Expr))
233 return false;
234
Tobias Grosser120db6b2011-11-07 12:58:54 +0000235 SCEVValidator Validator(R, SE, BaseAddress);
236 ValidatorResult Result = Validator.visit(Expr);
237
238 return Result.isValid();
239 }
Tobias Grosser60b54f12011-11-08 15:41:28 +0000240
241 std::vector<const SCEV*> getParamsInAffineExpr(const Region *R,
242 const SCEV *Expr,
243 ScalarEvolution &SE,
Tobias Grossere5e171e2011-11-10 12:45:03 +0000244 const Value *BaseAddress) {
Tobias Grosser60b54f12011-11-08 15:41:28 +0000245 if (isa<SCEVCouldNotCompute>(Expr))
246 return std::vector<const SCEV*>();
247
Tobias Grosser60b54f12011-11-08 15:41:28 +0000248 SCEVValidator Validator(R, SE, BaseAddress);
249 ValidatorResult Result = Validator.visit(Expr);
250
251 return Result.Parameters;
252 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000253}
254
255