blob: a739f3b4c9021c9f5d2f7a7b3f4e0604cd002c74 [file] [log] [blame]
Tobias Grosser120db6b2011-11-07 12:58:54 +00001
2#include "polly/Support/SCEVValidator.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +00003#include "polly/ScopInfo.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +00004
Tobias Grossereeb776a2012-09-08 14:00:37 +00005#define DEBUG_TYPE "polly-scev-validator"
6#include "llvm/Support/Debug.h"
Tobias Grosser120db6b2011-11-07 12:58:54 +00007#include "llvm/Analysis/ScalarEvolution.h"
8#include "llvm/Analysis/ScalarEvolutionExpressions.h"
9#include "llvm/Analysis/RegionInfo.h"
10
Tobias Grosser60b54f12011-11-08 15:41:28 +000011#include <vector>
12
Tobias Grosser120db6b2011-11-07 12:58:54 +000013using namespace llvm;
14
15namespace SCEVType {
Tobias Grosserde49b8f2013-02-22 08:21:52 +000016/// @brief The type of a SCEV
17///
18/// To check for the validity of a SCEV we assign to each SCEV a type. The
19/// possible types are INT, PARAM, IV and INVALID. The order of the types is
20/// important. The subexpressions of SCEV with a type X can only have a type
21/// that is smaller or equal than X.
22enum TYPE {
23 // An integer value.
24 INT,
Tobias Grosserb1f07c52011-11-17 12:56:17 +000025
Tobias Grosserde49b8f2013-02-22 08:21:52 +000026 // An expression that is constant during the execution of the Scop,
27 // but that may depend on parameters unknown at compile time.
28 PARAM,
Tobias Grosserb1f07c52011-11-17 12:56:17 +000029
Tobias Grosserde49b8f2013-02-22 08:21:52 +000030 // An expression that may change during the execution of the SCoP.
31 IV,
Tobias Grosserb1f07c52011-11-17 12:56:17 +000032
Tobias Grosserde49b8f2013-02-22 08:21:52 +000033 // An invalid expression.
34 INVALID
35};
Tobias Grosser120db6b2011-11-07 12:58:54 +000036}
37
Tobias Grosserb1f07c52011-11-17 12:56:17 +000038/// @brief The result the validator returns for a SCEV expression.
Tobias Grosserfa043f02011-11-17 12:56:19 +000039class ValidatorResult {
Tobias Grosserb1f07c52011-11-17 12:56:17 +000040 /// @brief The type of the expression
Tobias Grosseredb8a282011-11-17 12:56:21 +000041 SCEVType::TYPE Type;
Tobias Grosserb1f07c52011-11-17 12:56:17 +000042
43 /// @brief The set of Parameters in the expression.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000044 std::vector<const SCEV *> Parameters;
Tobias Grosser120db6b2011-11-07 12:58:54 +000045
Tobias Grosserfa043f02011-11-17 12:56:19 +000046public:
Tobias Grosserb1f07c52011-11-17 12:56:17 +000047 /// @brief The copy constructor
Tobias Grosseredb8a282011-11-17 12:56:21 +000048 ValidatorResult(const ValidatorResult &Source) {
49 Type = Source.Type;
50 Parameters = Source.Parameters;
Tobias Grosserde49b8f2013-02-22 08:21:52 +000051 }
Tobias Grosser120db6b2011-11-07 12:58:54 +000052
Tobias Grosserb1f07c52011-11-17 12:56:17 +000053 /// @brief Construct a result with a certain type and no parameters.
Tobias Grosser371bada2012-03-16 10:16:28 +000054 ValidatorResult(SCEVType::TYPE Type) : Type(Type) {
55 assert(Type != SCEVType::PARAM && "Did you forget to pass the parameter");
Tobias Grosserde49b8f2013-02-22 08:21:52 +000056 }
Tobias Grosserb1f07c52011-11-17 12:56:17 +000057
58 /// @brief Construct a result with a certain type and a single parameter.
Tobias Grosseredb8a282011-11-17 12:56:21 +000059 ValidatorResult(SCEVType::TYPE Type, const SCEV *Expr) : Type(Type) {
Tobias Grosser60b54f12011-11-08 15:41:28 +000060 Parameters.push_back(Expr);
Tobias Grosserde49b8f2013-02-22 08:21:52 +000061 }
Tobias Grosser120db6b2011-11-07 12:58:54 +000062
Tobias Grossereeb776a2012-09-08 14:00:37 +000063 /// @brief Get the type of the ValidatorResult.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000064 SCEVType::TYPE getType() { return Type; }
Tobias Grossereeb776a2012-09-08 14:00:37 +000065
Tobias Grosserb1f07c52011-11-17 12:56:17 +000066 /// @brief Is the analyzed SCEV constant during the execution of the SCoP.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000067 bool isConstant() { return Type == SCEVType::INT || Type == SCEVType::PARAM; }
Tobias Grosser120db6b2011-11-07 12:58:54 +000068
Tobias Grosserb1f07c52011-11-17 12:56:17 +000069 /// @brief Is the analyzed SCEV valid.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000070 bool isValid() { return Type != SCEVType::INVALID; }
Tobias Grosser120db6b2011-11-07 12:58:54 +000071
Tobias Grosseredb8a282011-11-17 12:56:21 +000072 /// @brief Is the analyzed SCEV of Type IV.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000073 bool isIV() { return Type == SCEVType::IV; }
Tobias Grosser120db6b2011-11-07 12:58:54 +000074
Tobias Grosseredb8a282011-11-17 12:56:21 +000075 /// @brief Is the analyzed SCEV of Type INT.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000076 bool isINT() { return Type == SCEVType::INT; }
Tobias Grosser60b54f12011-11-08 15:41:28 +000077
Tobias Grossereeb776a2012-09-08 14:00:37 +000078 /// @brief Is the analyzed SCEV of Type PARAM.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000079 bool isPARAM() { return Type == SCEVType::PARAM; }
Tobias Grossereeb776a2012-09-08 14:00:37 +000080
Tobias Grosserfa043f02011-11-17 12:56:19 +000081 /// @brief Get the parameters of this validator result.
Tobias Grosserde49b8f2013-02-22 08:21:52 +000082 std::vector<const SCEV *> getParameters() { return Parameters; }
Tobias Grosserfa043f02011-11-17 12:56:19 +000083
Tobias Grosserb1f07c52011-11-17 12:56:17 +000084 /// @brief Add the parameters of Source to this result.
Tobias Grosserfa043f02011-11-17 12:56:19 +000085 void addParamsFrom(class ValidatorResult &Source) {
Tobias Grosserde49b8f2013-02-22 08:21:52 +000086 Parameters.insert(Parameters.end(), Source.Parameters.begin(),
Tobias Grosser60b54f12011-11-08 15:41:28 +000087 Source.Parameters.end());
88 }
Tobias Grosserfa043f02011-11-17 12:56:19 +000089
90 /// @brief Merge a result.
91 ///
Tobias Grosseredb8a282011-11-17 12:56:21 +000092 /// This means to merge the parameters and to set the Type to the most
93 /// specific Type that matches both.
Tobias Grosserfa043f02011-11-17 12:56:19 +000094 void merge(class ValidatorResult &ToMerge) {
Tobias Grosseredb8a282011-11-17 12:56:21 +000095 Type = std::max(Type, ToMerge.Type);
Tobias Grosserfa043f02011-11-17 12:56:19 +000096 addParamsFrom(ToMerge);
97 }
Tobias Grosser540757b2012-03-16 10:12:37 +000098
99 void print(raw_ostream &OS) {
100 switch (Type) {
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000101 case SCEVType::INT:
102 OS << "SCEVType::INT";
Tobias Grosser540757b2012-03-16 10:12:37 +0000103 break;
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000104 case SCEVType::PARAM:
105 OS << "SCEVType::PARAM";
Tobias Grosser540757b2012-03-16 10:12:37 +0000106 break;
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000107 case SCEVType::IV:
108 OS << "SCEVType::IV";
Tobias Grosser540757b2012-03-16 10:12:37 +0000109 break;
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000110 case SCEVType::INVALID:
111 OS << "SCEVType::INVALID";
Tobias Grosser540757b2012-03-16 10:12:37 +0000112 break;
113 }
114 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000115};
116
Tobias Grosser540757b2012-03-16 10:12:37 +0000117raw_ostream &operator<<(raw_ostream &OS, class ValidatorResult &VR) {
118 VR.print(OS);
119 return OS;
120}
121
Tobias Grosser120db6b2011-11-07 12:58:54 +0000122/// Check if a SCEV is valid in a SCoP.
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000123struct SCEVValidator :
124 public SCEVVisitor<SCEVValidator, class ValidatorResult> {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000125private:
126 const Region *R;
127 ScalarEvolution &SE;
Tobias Grossere5e171e2011-11-10 12:45:03 +0000128 const Value *BaseAddress;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000129
130public:
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000131 SCEVValidator(const Region *R, ScalarEvolution &SE, const Value *BaseAddress)
132 : R(R), SE(SE), BaseAddress(BaseAddress) {}
Tobias Grosser120db6b2011-11-07 12:58:54 +0000133
Tobias Grosserfa043f02011-11-17 12:56:19 +0000134 class ValidatorResult visitConstant(const SCEVConstant *Constant) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000135 return ValidatorResult(SCEVType::INT);
136 }
137
Tobias Grosserfa043f02011-11-17 12:56:19 +0000138 class ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000139 ValidatorResult Op = visit(Expr->getOperand());
140
Tobias Grossereeb776a2012-09-08 14:00:37 +0000141 switch (Op.getType()) {
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000142 case SCEVType::INT:
143 case SCEVType::PARAM:
144 // We currently do not represent a truncate expression as an affine
145 // expression. If it is constant during Scop execution, we treat it as a
146 // parameter.
147 return ValidatorResult(SCEVType::PARAM, Expr);
148 case SCEVType::IV:
149 DEBUG(dbgs() << "INVALID: Truncation of SCEVType::IV expression");
150 return ValidatorResult(SCEVType::INVALID);
151 case SCEVType::INVALID:
152 return Op;
Tobias Grossereeb776a2012-09-08 14:00:37 +0000153 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000154
Tobias Grossereeb776a2012-09-08 14:00:37 +0000155 llvm_unreachable("Unknown SCEVType");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000156 }
157
Tobias Grosserfa043f02011-11-17 12:56:19 +0000158 class ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000159 ValidatorResult Op = visit(Expr->getOperand());
160
Tobias Grossereeb776a2012-09-08 14:00:37 +0000161 switch (Op.getType()) {
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000162 case SCEVType::INT:
163 case SCEVType::PARAM:
164 // We currently do not represent a truncate expression as an affine
165 // expression. If it is constant during Scop execution, we treat it as a
166 // parameter.
167 return ValidatorResult(SCEVType::PARAM, Expr);
168 case SCEVType::IV:
169 DEBUG(dbgs() << "INVALID: ZeroExtend of SCEVType::IV expression");
170 return ValidatorResult(SCEVType::INVALID);
171 case SCEVType::INVALID:
172 return Op;
Tobias Grossereeb776a2012-09-08 14:00:37 +0000173 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000174
Tobias Grossereeb776a2012-09-08 14:00:37 +0000175 llvm_unreachable("Unknown SCEVType");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000176 }
177
Tobias Grosserfa043f02011-11-17 12:56:19 +0000178 class ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000179 // We currently allow only signed SCEV expressions. In the case of a
180 // signed value, a sign extend is a noop.
181 //
182 // TODO: Reconsider this when we add support for unsigned values.
183 return visit(Expr->getOperand());
184 }
185
Tobias Grosserfa043f02011-11-17 12:56:19 +0000186 class ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000187 ValidatorResult Return(SCEVType::INT);
188
189 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
190 ValidatorResult Op = visit(Expr->getOperand(i));
Tobias Grosserfa043f02011-11-17 12:56:19 +0000191 Return.merge(Op);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000192
193 // Early exit.
194 if (!Return.isValid())
195 break;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000196 }
197
198 // TODO: Check for NSW and NUW.
199 return Return;
200 }
201
Tobias Grosserfa043f02011-11-17 12:56:19 +0000202 class ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000203 ValidatorResult Return(SCEVType::INT);
204
205 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
206 ValidatorResult Op = visit(Expr->getOperand(i));
207
Tobias Grosserfa043f02011-11-17 12:56:19 +0000208 if (Op.isINT())
Tobias Grosser120db6b2011-11-07 12:58:54 +0000209 continue;
210
Tobias Grosserecb50922013-04-10 07:42:28 +0000211 if (Op.isPARAM() && Return.isPARAM()) {
212 Return.merge(Op);
213 continue;
214 }
215
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000216 if ((Op.isIV() || Op.isPARAM()) && !Return.isINT()) {
Tobias Grossereeb776a2012-09-08 14:00:37 +0000217 DEBUG(dbgs() << "INVALID: More than one non-int operand in MulExpr\n"
218 << "\tExpr: " << *Expr << "\n"
219 << "\tPrevious expression type: " << Return << "\n"
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000220 << "\tNext operand (" << Op
221 << "): " << *Expr->getOperand(i) << "\n");
Tobias Grossereeb776a2012-09-08 14:00:37 +0000222
Tobias Grosser120db6b2011-11-07 12:58:54 +0000223 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000224 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000225
Tobias Grosserfa043f02011-11-17 12:56:19 +0000226 Return.merge(Op);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000227 }
228
229 // TODO: Check for NSW and NUW.
230 return Return;
231 }
232
Tobias Grosserfa043f02011-11-17 12:56:19 +0000233 class ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000234 ValidatorResult LHS = visit(Expr->getLHS());
235 ValidatorResult RHS = visit(Expr->getRHS());
236
Tobias Grosserf9fbbdf2012-04-09 19:46:05 +0000237 // We currently do not represent an unsigned division as an affine
Tobias Grosser120db6b2011-11-07 12:58:54 +0000238 // expression. If the division is constant during Scop execution we treat it
239 // as a parameter, otherwise we bail out.
240 if (LHS.isConstant() && RHS.isConstant())
Tobias Grosser60b54f12011-11-08 15:41:28 +0000241 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000242
Tobias Grossereeb776a2012-09-08 14:00:37 +0000243 DEBUG(dbgs() << "INVALID: unsigned division of non-constant expressions");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000244 return ValidatorResult(SCEVType::INVALID);
245 }
246
Tobias Grosserfa043f02011-11-17 12:56:19 +0000247 class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) {
Tobias Grossereeb776a2012-09-08 14:00:37 +0000248 if (!Expr->isAffine()) {
249 DEBUG(dbgs() << "INVALID: AddRec is not affine");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000250 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000251 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000252
253 ValidatorResult Start = visit(Expr->getStart());
254 ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE));
255
Tobias Grossereeb776a2012-09-08 14:00:37 +0000256 if (!Start.isValid())
257 return Start;
258
259 if (!Recurrence.isValid())
260 return Recurrence;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000261
Tobias Grosserbcc0a0d2011-11-17 12:56:14 +0000262 if (R->contains(Expr->getLoop())) {
263 if (Recurrence.isINT()) {
264 ValidatorResult Result(SCEVType::IV);
265 Result.addParamsFrom(Start);
266 return Result;
267 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000268
Tobias Grossereeb776a2012-09-08 14:00:37 +0000269 DEBUG(dbgs() << "INVALID: AddRec within scop has non-int"
270 "recurrence part");
Tobias Grosserbcc0a0d2011-11-17 12:56:14 +0000271 return ValidatorResult(SCEVType::INVALID);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000272 }
273
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000274 assert(Start.isConstant() && Recurrence.isConstant() &&
275 "Expected 'Start' and 'Recurrence' to be constant");
Tobias Grossereeb776a2012-09-08 14:00:37 +0000276 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000277 }
278
Tobias Grosserfa043f02011-11-17 12:56:19 +0000279 class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) {
Tobias Grosser371bada2012-03-16 10:16:28 +0000280 ValidatorResult Return(SCEVType::INT, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000281
282 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
283 ValidatorResult Op = visit(Expr->getOperand(i));
284
285 if (!Op.isValid())
Tobias Grossereeb776a2012-09-08 14:00:37 +0000286 return Op;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000287
Tobias Grosserfa043f02011-11-17 12:56:19 +0000288 Return.merge(Op);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000289 }
290
291 return Return;
292 }
293
Tobias Grosserfa043f02011-11-17 12:56:19 +0000294 class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000295 // We do not support unsigned operations. If 'Expr' is constant during Scop
296 // execution we treat this as a parameter, otherwise we bail out.
297 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
298 ValidatorResult Op = visit(Expr->getOperand(i));
299
Tobias Grossereeb776a2012-09-08 14:00:37 +0000300 if (!Op.isConstant()) {
301 DEBUG(dbgs() << "INVALID: UMaxExpr has a non-constant operand");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000302 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000303 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000304 }
305
Tobias Grosser371bada2012-03-16 10:16:28 +0000306 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000307 }
308
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000309 ValidatorResult visitUnknown(const SCEVUnknown *Expr) {
Tobias Grosser120db6b2011-11-07 12:58:54 +0000310 Value *V = Expr->getValue();
311
Tobias Grosser3ec2abc2012-03-16 16:36:47 +0000312 // We currently only support integer types. It may be useful to support
313 // pointer types, e.g. to support code like:
314 //
315 // if (A)
316 // A[i] = 1;
317 //
318 // See test/CodeGen/20120316-InvalidCast.ll
Tobias Grossereeb776a2012-09-08 14:00:37 +0000319 if (!Expr->getType()->isIntegerTy()) {
320 DEBUG(dbgs() << "INVALID: UnknownExpr is not an integer type");
Tobias Grosser3ec2abc2012-03-16 16:36:47 +0000321 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000322 }
Tobias Grosser3ec2abc2012-03-16 16:36:47 +0000323
Tobias Grossereeb776a2012-09-08 14:00:37 +0000324 if (isa<UndefValue>(V)) {
325 DEBUG(dbgs() << "INVALID: UnknownExpr references an undef value");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000326 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000327 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000328
Tobias Grosser120db6b2011-11-07 12:58:54 +0000329 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue()))
Tobias Grossereeb776a2012-09-08 14:00:37 +0000330 if (R->contains(I)) {
331 DEBUG(dbgs() << "INVALID: UnknownExpr references an instruction "
332 "within the region\n");
Tobias Grosser120db6b2011-11-07 12:58:54 +0000333 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000334 }
Tobias Grosser120db6b2011-11-07 12:58:54 +0000335
Tobias Grossereeb776a2012-09-08 14:00:37 +0000336 if (BaseAddress == V) {
337 DEBUG(dbgs() << "INVALID: UnknownExpr references BaseAddress\n");
Tobias Grossere5e171e2011-11-10 12:45:03 +0000338 return ValidatorResult(SCEVType::INVALID);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000339 }
Tobias Grossere5e171e2011-11-10 12:45:03 +0000340
341 return ValidatorResult(SCEVType::PARAM, Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000342 }
343};
344
Sebastian Pop97cb8132013-03-18 20:21:13 +0000345/// @brief Check whether a SCEV refers to an SSA name defined inside a region.
346///
347struct SCEVInRegionDependences :
348 public SCEVVisitor<SCEVInRegionDependences, bool> {
349public:
350
351 /// Returns true when the SCEV has SSA names defined in region R.
352 static bool hasDependences(const SCEV *S, const Region *R) {
353 SCEVInRegionDependences Ignore(R);
354 return Ignore.visit(S);
355 }
356
357 SCEVInRegionDependences(const Region *R) : R(R) {}
358
359 bool visit(const SCEV *Expr) {
360 return SCEVVisitor<SCEVInRegionDependences, bool>::visit(Expr);
361 }
362
363 bool visitConstant(const SCEVConstant *Constant) { return false; }
364
365 bool visitTruncateExpr(const SCEVTruncateExpr *Expr) {
366 return visit(Expr->getOperand());
367 }
368
369 bool visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
370 return visit(Expr->getOperand());
371 }
372
373 bool visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
374 return visit(Expr->getOperand());
375 }
376
377 bool visitAddExpr(const SCEVAddExpr *Expr) {
378 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
379 if (visit(Expr->getOperand(i)))
380 return true;
381
382 return false;
383 }
384
385 bool visitMulExpr(const SCEVMulExpr *Expr) {
386 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
387 if (visit(Expr->getOperand(i)))
388 return true;
389
390 return false;
391 }
392
393 bool visitUDivExpr(const SCEVUDivExpr *Expr) {
394 if (visit(Expr->getLHS()))
395 return true;
396
397 if (visit(Expr->getRHS()))
398 return true;
399
400 return false;
401 }
402
403 bool visitAddRecExpr(const SCEVAddRecExpr *Expr) {
404 if (visit(Expr->getStart()))
405 return true;
406
407 for (size_t i = 0; i < Expr->getNumOperands(); ++i)
408 if (visit(Expr->getOperand(i)))
409 return true;
410
411 return false;
412 }
413
414 bool visitSMaxExpr(const SCEVSMaxExpr *Expr) {
415 for (size_t i = 0; i < Expr->getNumOperands(); ++i)
416 if (visit(Expr->getOperand(i)))
417 return true;
418
419 return false;
420 }
421
422 bool visitUMaxExpr(const SCEVUMaxExpr *Expr) {
423 for (size_t i = 0; i < Expr->getNumOperands(); ++i)
424 if (visit(Expr->getOperand(i)))
425 return true;
426
427 return false;
428 }
429
430 bool visitUnknown(const SCEVUnknown *Expr) {
431 Instruction *Inst = dyn_cast<Instruction>(Expr->getValue());
432
433 // Return true when Inst is defined inside the region R.
434 if (Inst && R->contains(Inst))
435 return true;
436
437 return false;
438 }
439
440private:
441 const Region *R;
442};
443
Tobias Grosser120db6b2011-11-07 12:58:54 +0000444namespace polly {
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000445bool hasScalarDepsInsideRegion(const SCEV *Expr, const Region *R) {
446 return SCEVInRegionDependences::hasDependences(Expr, R);
447}
Sebastian Pop97cb8132013-03-18 20:21:13 +0000448
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000449bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE,
450 const Value *BaseAddress) {
451 if (isa<SCEVCouldNotCompute>(Expr))
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000452 return false;
Tobias Grosser120db6b2011-11-07 12:58:54 +0000453
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000454 SCEVValidator Validator(R, SE, BaseAddress);
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000455 DEBUG(dbgs() << "\n"; dbgs() << "Expr: " << *Expr << "\n";
456 dbgs() << "Region: " << R->getNameStr() << "\n"; dbgs() << " -> ");
Tobias Grossereeb776a2012-09-08 14:00:37 +0000457
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000458 ValidatorResult Result = Validator.visit(Expr);
Tobias Grosser120db6b2011-11-07 12:58:54 +0000459
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000460 DEBUG(if (Result.isValid()) dbgs() << "VALID\n"; dbgs() << "\n";);
Tobias Grossereeb776a2012-09-08 14:00:37 +0000461
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000462 return Result.isValid();
463}
Tobias Grosser60b54f12011-11-08 15:41:28 +0000464
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000465std::vector<const SCEV *>
466getParamsInAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE,
467 const Value *BaseAddress) {
468 if (isa<SCEVCouldNotCompute>(Expr))
469 return std::vector<const SCEV *>();
Tobias Grosser60b54f12011-11-08 15:41:28 +0000470
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000471 SCEVValidator Validator(R, SE, BaseAddress);
472 ValidatorResult Result = Validator.visit(Expr);
Tobias Grosser60b54f12011-11-08 15:41:28 +0000473
Tobias Grosserde49b8f2013-02-22 08:21:52 +0000474 return Result.getParameters();
475}
Tobias Grosser120db6b2011-11-07 12:58:54 +0000476}