update isl for rename of isl_map_insert
[pet.git] / scan.cc
blobdd97edbb68c9ad38072a32d92c9663551d394ee1
1 #include <set>
2 #include <map>
3 #include <iostream>
4 #include <clang/AST/ASTDiagnostic.h>
5 #include <clang/AST/Expr.h>
6 #include <clang/AST/RecursiveASTVisitor.h>
8 #include <isl/id.h>
9 #include <isl/dim.h>
10 #include <isl/aff.h>
11 #include <isl/set.h>
13 #include "scan.h"
14 #include "scop.h"
15 #include "scop_plus.h"
17 #include "config.h"
19 using namespace std;
20 using namespace clang;
22 /* Look for any assignments to variables in part of the parse
23 * tree and set assigned_value to NULL for each of them.
25 * This ensures that we won't use any previously stored value
26 * in the current subtree and its parents.
28 struct clear_assignments : RecursiveASTVisitor<clear_assignments> {
29 map<ValueDecl *, Expr *> &assigned_value;
31 clear_assignments(map<ValueDecl *, Expr *> &assigned_value) :
32 assigned_value(assigned_value) {}
34 bool VisitBinaryOperator(BinaryOperator *expr) {
35 Expr *lhs;
36 DeclRefExpr *ref;
37 ValueDecl *decl;
39 if (!expr->isAssignmentOp())
40 return true;
41 lhs = expr->getLHS();
42 if (lhs->getStmtClass() != Stmt::DeclRefExprClass)
43 return true;
44 ref = cast<DeclRefExpr>(lhs);
45 decl = ref->getDecl();
46 assigned_value[decl] = NULL;
47 return true;
51 /* Keep a copy of the currently assigned values.
53 * Any variable that is assigned a value inside the current scope
54 * is removed again when we leave the scope (either because it wasn't
55 * stored in the cache or because it has a different value in the cache).
57 struct assigned_value_cache {
58 map<ValueDecl *, Expr *> &assigned_value;
59 map<ValueDecl *, Expr *> cache;
61 assigned_value_cache(map<ValueDecl *, Expr *> &assigned_value) :
62 assigned_value(assigned_value), cache(assigned_value) {}
63 ~assigned_value_cache() {
64 map<ValueDecl *, Expr *>::iterator it = cache.begin();
65 for (it = assigned_value.begin(); it != assigned_value.end();
66 ++it) {
67 if (!it->second ||
68 (cache.find(it->first) != cache.end() &&
69 cache[it->first] != it->second))
70 cache[it->first] = NULL;
72 assigned_value = cache;
76 /* Called if we found something we (currently) cannot handle.
77 * We'll provide more informative warnings later.
79 * We only actually complain if autodetect is false.
81 void PetScan::unsupported(Stmt *stmt)
83 if (autodetect)
84 return;
86 SourceLocation loc = stmt->getLocStart();
87 Diagnostic &diag = PP.getDiagnostics();
88 unsigned id = diag.getCustomDiagID(Diagnostic::Warning, "unsupported");
89 DiagnosticBuilder B = diag.Report(loc, id) << stmt->getSourceRange();
92 /* Extract an integer from "expr" and store it in "v".
94 int PetScan::extract_int(IntegerLiteral *expr, isl_int *v)
96 const Type *type = expr->getType().getTypePtr();
97 int is_signed = type->hasSignedIntegerRepresentation();
99 if (is_signed) {
100 int64_t i = expr->getValue().getSExtValue();
101 isl_int_set_si(*v, i);
102 } else {
103 uint64_t i = expr->getValue().getZExtValue();
104 isl_int_set_ui(*v, i);
107 return 0;
110 /* Extract an affine expression from the IntegerLiteral "expr".
112 __isl_give isl_pw_aff *PetScan::extract_affine(IntegerLiteral *expr)
114 isl_dim *dim = isl_dim_set_alloc(ctx, 0, 0);
115 isl_local_space *ls = isl_local_space_from_dim(isl_dim_copy(dim));
116 isl_aff *aff = isl_aff_zero(ls);
117 isl_set *dom = isl_set_universe(dim);
118 isl_int v;
120 isl_int_init(v);
121 extract_int(expr, &v);
122 aff = isl_aff_add_constant(aff, v);
123 isl_int_clear(v);
125 return isl_pw_aff_alloc(dom, aff);
128 /* Extract an affine expression from the APInt "val".
130 __isl_give isl_pw_aff *PetScan::extract_affine(const llvm::APInt &val)
132 isl_dim *dim = isl_dim_set_alloc(ctx, 0, 0);
133 isl_local_space *ls = isl_local_space_from_dim(isl_dim_copy(dim));
134 isl_aff *aff = isl_aff_zero(ls);
135 isl_set *dom = isl_set_universe(dim);
136 isl_int v;
138 isl_int_init(v);
139 isl_int_set_ui(v, val.getZExtValue());
140 aff = isl_aff_add_constant(aff, v);
141 isl_int_clear(v);
143 return isl_pw_aff_alloc(dom, aff);
146 __isl_give isl_pw_aff *PetScan::extract_affine(ImplicitCastExpr *expr)
148 return extract_affine(expr->getSubExpr());
151 /* Extract an affine expression from the DeclRefExpr "expr".
153 * If we have recorded an expression that was assigned to the variable
154 * before, then we convert this expressoin to an isl_pw_aff if it is
155 * affine and to an extra parameter otherwise (provided nesting_enabled is set).
157 * Otherwise, we simply return an expression that is equal
158 * to a parameter corresponding to the referenced variable.
160 __isl_give isl_pw_aff *PetScan::extract_affine(DeclRefExpr *expr)
162 ValueDecl *decl = expr->getDecl();
163 const Type *type = decl->getType().getTypePtr();
164 isl_id *id;
165 isl_dim *dim;
166 isl_aff *aff;
167 isl_set *dom;
169 if (!type->isIntegerType()) {
170 unsupported(expr);
171 return NULL;
174 if (assigned_value.find(decl) != assigned_value.end() &&
175 assigned_value[decl] != NULL) {
176 if (is_affine(assigned_value[decl]))
177 return extract_affine(assigned_value[decl]);
178 else
179 return non_affine(expr);
182 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
183 dim = isl_dim_set_alloc(ctx, 1, 0);
185 dim = isl_dim_set_dim_id(dim, isl_dim_param, 0, id);
187 dom = isl_set_universe(isl_dim_copy(dim));
188 aff = isl_aff_zero(isl_local_space_from_dim(dim));
189 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
191 return isl_pw_aff_alloc(dom, aff);
194 /* Extract an affine expression from an integer division operation.
195 * In particular, if "expr" is lhs/rhs, then return
197 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
199 * The second argument (rhs) is required to be a (positive) integer constant.
201 __isl_give isl_pw_aff *PetScan::extract_affine_div(BinaryOperator *expr)
203 Expr *rhs_expr;
204 isl_pw_aff *lhs, *lhs_f, *lhs_c;
205 isl_pw_aff *res;
206 isl_int v;
207 isl_set *cond;
209 rhs_expr = expr->getRHS();
210 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
211 unsupported(expr);
212 return NULL;
215 lhs = extract_affine(expr->getLHS());
216 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
218 isl_int_init(v);
219 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
220 lhs = isl_pw_aff_scale_down(lhs, v);
221 isl_int_clear(v);
223 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(lhs));
224 lhs_c = isl_pw_aff_ceil(lhs);
225 res = isl_pw_aff_cond(cond, lhs_f, lhs_c);
227 return res;
230 /* Extract an affine expression from a modulo operation.
231 * In particular, if "expr" is lhs/rhs, then return
233 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
235 * The second argument (rhs) is required to be a (positive) integer constant.
237 __isl_give isl_pw_aff *PetScan::extract_affine_mod(BinaryOperator *expr)
239 Expr *rhs_expr;
240 isl_pw_aff *lhs, *lhs_f, *lhs_c;
241 isl_pw_aff *res;
242 isl_int v;
243 isl_set *cond;
245 rhs_expr = expr->getRHS();
246 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
247 unsupported(expr);
248 return NULL;
251 lhs = extract_affine(expr->getLHS());
252 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
254 isl_int_init(v);
255 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
256 res = isl_pw_aff_scale_down(isl_pw_aff_copy(lhs), v);
258 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(res));
259 lhs_c = isl_pw_aff_ceil(res);
260 res = isl_pw_aff_cond(cond, lhs_f, lhs_c);
262 res = isl_pw_aff_scale(res, v);
263 isl_int_clear(v);
265 res = isl_pw_aff_sub(lhs, res);
267 return res;
270 /* Extract an affine expression from a multiplication operation.
271 * This is only allowed if at least one of the two arguments
272 * is a (piecewise) constant.
274 __isl_give isl_pw_aff *PetScan::extract_affine_mul(BinaryOperator *expr)
276 isl_pw_aff *lhs;
277 isl_pw_aff *rhs;
279 lhs = extract_affine(expr->getLHS());
280 rhs = extract_affine(expr->getRHS());
282 if (!isl_pw_aff_is_cst(lhs) && !isl_pw_aff_is_cst(rhs)) {
283 isl_pw_aff_free(lhs);
284 isl_pw_aff_free(rhs);
285 unsupported(expr);
286 return NULL;
289 return isl_pw_aff_mul(lhs, rhs);
292 /* Extract an affine expression from an addition or subtraction operation.
294 __isl_give isl_pw_aff *PetScan::extract_affine_add(BinaryOperator *expr)
296 isl_pw_aff *lhs;
297 isl_pw_aff *rhs;
299 lhs = extract_affine(expr->getLHS());
300 rhs = extract_affine(expr->getRHS());
302 switch (expr->getOpcode()) {
303 case BO_Add:
304 return isl_pw_aff_add(lhs, rhs);
305 case BO_Sub:
306 return isl_pw_aff_sub(lhs, rhs);
307 default:
308 isl_pw_aff_free(lhs);
309 isl_pw_aff_free(rhs);
310 return NULL;
315 /* Compute
317 * pwaff mod 2^width
319 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff,
320 unsigned width)
322 isl_int mod;
324 isl_int_init(mod);
325 isl_int_set_si(mod, 1);
326 isl_int_mul_2exp(mod, mod, width);
328 pwaff = isl_pw_aff_mod(pwaff, mod);
330 isl_int_clear(mod);
332 return pwaff;
335 /* Extract an affine expression from some binary operations.
336 * If the result of the expression is unsigned, then we wrap it
337 * based on the size of the type.
339 __isl_give isl_pw_aff *PetScan::extract_affine(BinaryOperator *expr)
341 isl_pw_aff *res;
343 switch (expr->getOpcode()) {
344 case BO_Add:
345 case BO_Sub:
346 res = extract_affine_add(expr);
347 break;
348 case BO_Div:
349 res = extract_affine_div(expr);
350 break;
351 case BO_Rem:
352 res = extract_affine_mod(expr);
353 break;
354 case BO_Mul:
355 res = extract_affine_mul(expr);
356 break;
357 default:
358 unsupported(expr);
359 return NULL;
362 if (expr->getType()->isUnsignedIntegerType())
363 res = wrap(res, ast_context.getIntWidth(expr->getType()));
365 return res;
368 /* Extract an affine expression from a negation operation.
370 __isl_give isl_pw_aff *PetScan::extract_affine(UnaryOperator *expr)
372 if (expr->getOpcode() == UO_Minus)
373 return isl_pw_aff_neg(extract_affine(expr->getSubExpr()));
375 unsupported(expr);
376 return NULL;
379 __isl_give isl_pw_aff *PetScan::extract_affine(ParenExpr *expr)
381 return extract_affine(expr->getSubExpr());
384 /* Extract an affine expression from some special function calls.
385 * In particular, we handle "min", "max", "ceild" and "floord".
386 * In case of the latter two, the second argument needs to be
387 * a (positive) integer constant.
389 __isl_give isl_pw_aff *PetScan::extract_affine(CallExpr *expr)
391 FunctionDecl *fd;
392 string name;
393 isl_pw_aff *aff1, *aff2;
395 fd = expr->getDirectCallee();
396 if (!fd) {
397 unsupported(expr);
398 return NULL;
401 name = fd->getDeclName().getAsString();
402 if (!(expr->getNumArgs() == 2 && name == "min") &&
403 !(expr->getNumArgs() == 2 && name == "max") &&
404 !(expr->getNumArgs() == 2 && name == "floord") &&
405 !(expr->getNumArgs() == 2 && name == "ceild")) {
406 unsupported(expr);
407 return NULL;
410 if (name == "min" || name == "max") {
411 aff1 = extract_affine(expr->getArg(0));
412 aff2 = extract_affine(expr->getArg(1));
414 if (name == "min")
415 aff1 = isl_pw_aff_min(aff1, aff2);
416 else
417 aff1 = isl_pw_aff_max(aff1, aff2);
418 } else if (name == "floord" || name == "ceild") {
419 isl_int v;
420 Expr *arg2 = expr->getArg(1);
422 if (arg2->getStmtClass() != Stmt::IntegerLiteralClass) {
423 unsupported(expr);
424 return NULL;
426 aff1 = extract_affine(expr->getArg(0));
427 isl_int_init(v);
428 extract_int(cast<IntegerLiteral>(arg2), &v);
429 aff1 = isl_pw_aff_scale_down(aff1, v);
430 isl_int_clear(v);
431 if (name == "floord")
432 aff1 = isl_pw_aff_floor(aff1);
433 else
434 aff1 = isl_pw_aff_ceil(aff1);
435 } else {
436 unsupported(expr);
437 return NULL;
440 return aff1;
444 /* This method is called when we come across a non-affine expression.
445 * If nesting is allowed, we return a new parameter that corresponds
446 * to the non-affine expression. Otherwise, we simply complain.
448 * The new parameter is resolved in resolve_nested.
450 isl_pw_aff *PetScan::non_affine(Expr *expr)
452 isl_id *id;
453 isl_dim *dim;
454 isl_aff *aff;
455 isl_set *dom;
457 if (!nesting_enabled) {
458 unsupported(expr);
459 return NULL;
462 id = isl_id_alloc(ctx, NULL, expr);
463 dim = isl_dim_set_alloc(ctx, 1, 0);
465 dim = isl_dim_set_dim_id(dim, isl_dim_param, 0, id);
467 dom = isl_set_universe(isl_dim_copy(dim));
468 aff = isl_aff_zero(isl_local_space_from_dim(dim));
469 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
471 return isl_pw_aff_alloc(dom, aff);
474 /* Affine expressions are not supposed to contain array accesses,
475 * but if nesting is allowed, we return a parameter corresponding
476 * to the array access.
478 __isl_give isl_pw_aff *PetScan::extract_affine(ArraySubscriptExpr *expr)
480 return non_affine(expr);
483 /* Extract an affine expression from a conditional operation.
485 __isl_give isl_pw_aff *PetScan::extract_affine(ConditionalOperator *expr)
487 isl_set *cond;
488 isl_pw_aff *lhs, *rhs;
490 cond = extract_condition(expr->getCond());
491 lhs = extract_affine(expr->getTrueExpr());
492 rhs = extract_affine(expr->getFalseExpr());
494 return isl_pw_aff_cond(cond, lhs, rhs);
497 /* Extract an affine expression, if possible, from "expr".
498 * Otherwise return NULL.
500 __isl_give isl_pw_aff *PetScan::extract_affine(Expr *expr)
502 switch (expr->getStmtClass()) {
503 case Stmt::ImplicitCastExprClass:
504 return extract_affine(cast<ImplicitCastExpr>(expr));
505 case Stmt::IntegerLiteralClass:
506 return extract_affine(cast<IntegerLiteral>(expr));
507 case Stmt::DeclRefExprClass:
508 return extract_affine(cast<DeclRefExpr>(expr));
509 case Stmt::BinaryOperatorClass:
510 return extract_affine(cast<BinaryOperator>(expr));
511 case Stmt::UnaryOperatorClass:
512 return extract_affine(cast<UnaryOperator>(expr));
513 case Stmt::ParenExprClass:
514 return extract_affine(cast<ParenExpr>(expr));
515 case Stmt::CallExprClass:
516 return extract_affine(cast<CallExpr>(expr));
517 case Stmt::ArraySubscriptExprClass:
518 return extract_affine(cast<ArraySubscriptExpr>(expr));
519 case Stmt::ConditionalOperatorClass:
520 return extract_affine(cast<ConditionalOperator>(expr));
521 default:
522 unsupported(expr);
524 return NULL;
527 __isl_give isl_map *PetScan::extract_access(ImplicitCastExpr *expr)
529 return extract_access(expr->getSubExpr());
532 /* Return the depth of an array of the given type.
534 static int array_depth(const Type *type)
536 if (type->isPointerType())
537 return 1 + array_depth(type->getPointeeType().getTypePtr());
538 if (type->isArrayType()) {
539 const ArrayType *atype;
540 type = type->getCanonicalTypeInternal().getTypePtr();
541 atype = cast<ArrayType>(type);
542 return 1 + array_depth(atype->getElementType().getTypePtr());
544 return 0;
547 /* Return the element type of the given array type.
549 static QualType base_type(QualType qt)
551 const Type *type = qt.getTypePtr();
553 if (type->isPointerType())
554 return base_type(type->getPointeeType());
555 if (type->isArrayType()) {
556 const ArrayType *atype;
557 type = type->getCanonicalTypeInternal().getTypePtr();
558 atype = cast<ArrayType>(type);
559 return base_type(atype->getElementType());
561 return qt;
564 /* Check if the element type corresponding to the given array type
565 * has a const qualifier.
567 static bool const_base(QualType qt)
569 const Type *type = qt.getTypePtr();
571 if (type->isPointerType())
572 return const_base(type->getPointeeType());
573 if (type->isArrayType()) {
574 const ArrayType *atype;
575 type = type->getCanonicalTypeInternal().getTypePtr();
576 atype = cast<ArrayType>(type);
577 return const_base(atype->getElementType());
580 return qt.isConstQualified();
583 /* Extract an access relation from a reference to a variable.
584 * If the variable has name "A" and its type corresponds to an
585 * array of depth d, then the returned access relation is of the
586 * form
588 * { [] -> A[i_1,...,i_d] }
590 __isl_give isl_map *PetScan::extract_access(DeclRefExpr *expr)
592 ValueDecl *decl = expr->getDecl();
593 int depth = array_depth(decl->getType().getTypePtr());
594 isl_id *id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
595 isl_dim *dim = isl_dim_alloc(ctx, 0, 0, depth);
596 isl_map *access_rel;
598 dim = isl_dim_set_tuple_id(dim, isl_dim_out, id);
600 access_rel = isl_map_universe(dim);
602 return access_rel;
605 /* Extract an access relation from an integer contant.
606 * If the value of the constant is "v", then the returned access relation
607 * is
609 * { [] -> [v] }
611 __isl_give isl_map *PetScan::extract_access(IntegerLiteral *expr)
613 return isl_map_from_pw_aff(extract_affine(expr));
616 /* Try and extract an access relation from the given Expr.
617 * Return NULL if it doesn't work out.
619 __isl_give isl_map *PetScan::extract_access(Expr *expr)
621 switch (expr->getStmtClass()) {
622 case Stmt::ImplicitCastExprClass:
623 return extract_access(cast<ImplicitCastExpr>(expr));
624 case Stmt::DeclRefExprClass:
625 return extract_access(cast<DeclRefExpr>(expr));
626 case Stmt::ArraySubscriptExprClass:
627 return extract_access(cast<ArraySubscriptExpr>(expr));
628 default:
629 unsupported(expr);
631 return NULL;
634 /* Assign the affine expression "index" to the output dimension "pos" of "map"
635 * and return the result.
637 __isl_give isl_map *set_index(__isl_take isl_map *map, int pos,
638 __isl_take isl_pw_aff *index)
640 isl_map *index_map;
641 int len = isl_map_dim(map, isl_dim_out);
642 isl_id *id;
644 index_map = isl_map_from_pw_aff(index);
645 index_map = isl_map_insert_dims(index_map, isl_dim_out, 0, pos);
646 index_map = isl_map_add_dims(index_map, isl_dim_out, len - pos - 1);
647 id = isl_map_get_tuple_id(map, isl_dim_out);
648 index_map = isl_map_set_tuple_id(index_map, isl_dim_out, id);
650 map = isl_map_intersect(map, index_map);
652 return map;
655 /* Extract an access relation from the given array subscript expression.
656 * If nesting is allowed in general, then we turn it on while
657 * examining the index expression.
659 * We first extract an access relation from the base.
660 * This will result in an access relation with a range that corresponds
661 * to the array being accessed and with earlier indices filled in already.
662 * We then extract the current index and fill that in as well.
663 * The position of the current index is based on the type of base.
664 * If base is the actual array variable, then the depth of this type
665 * will be the same as the depth of the array and we will fill in
666 * the first array index.
667 * Otherwise, the depth of the base type will be smaller and we will fill
668 * in a later index.
670 __isl_give isl_map *PetScan::extract_access(ArraySubscriptExpr *expr)
672 Expr *base = expr->getBase();
673 Expr *idx = expr->getIdx();
674 isl_pw_aff *index;
675 isl_map *base_access;
676 isl_map *access;
677 int depth = array_depth(base->getType().getTypePtr());
678 int pos;
679 bool save_nesting = nesting_enabled;
681 nesting_enabled = allow_nested;
683 base_access = extract_access(base);
684 index = extract_affine(idx);
686 nesting_enabled = save_nesting;
688 pos = isl_map_dim(base_access, isl_dim_out) - depth;
689 access = set_index(base_access, pos, index);
691 return access;
694 /* Check if "expr" calls function "minmax" with two arguments and if so
695 * make lhs and rhs refer to these two arguments.
697 static bool is_minmax(Expr *expr, const char *minmax, Expr *&lhs, Expr *&rhs)
699 CallExpr *call;
700 FunctionDecl *fd;
701 string name;
703 if (expr->getStmtClass() != Stmt::CallExprClass)
704 return false;
706 call = cast<CallExpr>(expr);
707 fd = call->getDirectCallee();
708 if (!fd)
709 return false;
711 if (call->getNumArgs() != 2)
712 return false;
714 name = fd->getDeclName().getAsString();
715 if (name != minmax)
716 return false;
718 lhs = call->getArg(0);
719 rhs = call->getArg(1);
721 return true;
724 /* Check if "expr" is of the form min(lhs, rhs) and if so make
725 * lhs and rhs refer to the two arguments.
727 static bool is_min(Expr *expr, Expr *&lhs, Expr *&rhs)
729 return is_minmax(expr, "min", lhs, rhs);
732 /* Check if "expr" is of the form max(lhs, rhs) and if so make
733 * lhs and rhs refer to the two arguments.
735 static bool is_max(Expr *expr, Expr *&lhs, Expr *&rhs)
737 return is_minmax(expr, "max", lhs, rhs);
740 /* Extract a set of values satisfying the comparison "LHS op RHS"
741 * "comp" is the original expression that "LHS op RHS" is derived from
742 * and is used for diagnostics.
744 * If the comparison is of the form
746 * a <= min(b,c)
748 * then the set is constructed as the intersection of the set corresponding
749 * to the comparisons
751 * a <= b and a <= c
753 * A similar optimization is performed for max(a,b) <= c.
754 * We do this because that will lead to simpler representations of the set.
755 * If isl is ever enhanced to explicitly deal with min and max expressions,
756 * this optimization can be removed.
758 __isl_give isl_set *PetScan::extract_comparison(BinaryOperatorKind op,
759 Expr *LHS, Expr *RHS, Expr *comp)
761 isl_pw_aff *lhs;
762 isl_pw_aff *rhs;
763 isl_set *cond;
765 if (op == BO_GT)
766 return extract_comparison(BO_LT, RHS, LHS, comp);
767 if (op == BO_GE)
768 return extract_comparison(BO_LE, RHS, LHS, comp);
770 if (op == BO_LT || op == BO_LE) {
771 Expr *expr1, *expr2;
772 isl_set *set1, *set2;
773 if (is_min(RHS, expr1, expr2)) {
774 set1 = extract_comparison(op, LHS, expr1, comp);
775 set2 = extract_comparison(op, LHS, expr2, comp);
776 return isl_set_intersect(set1, set2);
778 if (is_max(LHS, expr1, expr2)) {
779 set1 = extract_comparison(op, expr1, RHS, comp);
780 set2 = extract_comparison(op, expr2, RHS, comp);
781 return isl_set_intersect(set1, set2);
785 lhs = extract_affine(LHS);
786 rhs = extract_affine(RHS);
788 switch (op) {
789 case BO_LT:
790 cond = isl_pw_aff_lt_set(lhs, rhs);
791 break;
792 case BO_LE:
793 cond = isl_pw_aff_le_set(lhs, rhs);
794 break;
795 case BO_EQ:
796 cond = isl_pw_aff_eq_set(lhs, rhs);
797 break;
798 case BO_NE:
799 cond = isl_pw_aff_ne_set(lhs, rhs);
800 break;
801 default:
802 isl_pw_aff_free(lhs);
803 isl_pw_aff_free(rhs);
804 unsupported(comp);
805 return NULL;
808 cond = isl_set_coalesce(cond);
810 return cond;
813 __isl_give isl_set *PetScan::extract_comparison(BinaryOperator *comp)
815 return extract_comparison(comp->getOpcode(), comp->getLHS(),
816 comp->getRHS(), comp);
819 /* Extract a set of values satisfying the negation (logical not)
820 * of a subexpression.
822 __isl_give isl_set *PetScan::extract_boolean(UnaryOperator *op)
824 isl_set *cond;
826 cond = extract_condition(op->getSubExpr());
828 return isl_set_complement(cond);
831 /* Extract a set of values satisfying the union (logical or)
832 * or intersection (logical and) of two subexpressions.
834 __isl_give isl_set *PetScan::extract_boolean(BinaryOperator *comp)
836 isl_set *lhs;
837 isl_set *rhs;
838 isl_set *cond;
840 lhs = extract_condition(comp->getLHS());
841 rhs = extract_condition(comp->getRHS());
843 switch (comp->getOpcode()) {
844 case BO_LAnd:
845 cond = isl_set_intersect(lhs, rhs);
846 break;
847 case BO_LOr:
848 cond = isl_set_union(lhs, rhs);
849 break;
850 default:
851 isl_set_free(lhs);
852 isl_set_free(rhs);
853 unsupported(comp);
854 return NULL;
857 return cond;
860 __isl_give isl_set *PetScan::extract_condition(UnaryOperator *expr)
862 switch (expr->getOpcode()) {
863 case UO_LNot:
864 return extract_boolean(expr);
865 default:
866 unsupported(expr);
867 return NULL;
871 /* Extract a set of values satisfying the condition "expr != 0".
873 __isl_give isl_set *PetScan::extract_implicit_condition(Expr *expr)
875 return isl_pw_aff_non_zero_set(extract_affine(expr));
878 /* Extract a set of values satisfying the condition expressed by "expr".
880 * If the expression doesn't look like a condition, we assume it
881 * is an affine expression and return the condition "expr != 0".
883 __isl_give isl_set *PetScan::extract_condition(Expr *expr)
885 BinaryOperator *comp;
887 if (!expr)
888 return isl_set_universe(isl_dim_set_alloc(ctx, 0, 0));
890 if (expr->getStmtClass() == Stmt::ParenExprClass)
891 return extract_condition(cast<ParenExpr>(expr)->getSubExpr());
893 if (expr->getStmtClass() == Stmt::UnaryOperatorClass)
894 return extract_condition(cast<UnaryOperator>(expr));
896 if (expr->getStmtClass() != Stmt::BinaryOperatorClass)
897 return extract_implicit_condition(expr);
899 comp = cast<BinaryOperator>(expr);
900 switch (comp->getOpcode()) {
901 case BO_LT:
902 case BO_LE:
903 case BO_GT:
904 case BO_GE:
905 case BO_EQ:
906 case BO_NE:
907 return extract_comparison(comp);
908 case BO_LAnd:
909 case BO_LOr:
910 return extract_boolean(comp);
911 default:
912 return extract_implicit_condition(expr);
916 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
918 switch (kind) {
919 case UO_Minus:
920 return pet_op_minus;
921 default:
922 return pet_op_last;
926 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
928 switch (kind) {
929 case BO_AddAssign:
930 return pet_op_add_assign;
931 case BO_SubAssign:
932 return pet_op_sub_assign;
933 case BO_MulAssign:
934 return pet_op_mul_assign;
935 case BO_DivAssign:
936 return pet_op_div_assign;
937 case BO_Assign:
938 return pet_op_assign;
939 case BO_Add:
940 return pet_op_add;
941 case BO_Sub:
942 return pet_op_sub;
943 case BO_Mul:
944 return pet_op_mul;
945 case BO_Div:
946 return pet_op_div;
947 case BO_LE:
948 return pet_op_le;
949 case BO_LT:
950 return pet_op_lt;
951 case BO_GT:
952 return pet_op_gt;
953 default:
954 return pet_op_last;
958 /* Construct a pet_expr representing a unary operator expression.
960 struct pet_expr *PetScan::extract_expr(UnaryOperator *expr)
962 struct pet_expr *arg;
963 enum pet_op_type op;
965 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
966 if (op == pet_op_last) {
967 unsupported(expr);
968 return NULL;
971 arg = extract_expr(expr->getSubExpr());
973 return pet_expr_new_unary(ctx, op, arg);
976 /* Construct a pet_expr representing a binary operator expression.
978 * If the top level operator is an assignment and the LHS is an access,
979 * then we mark that access as a write. If the operator is a compound
980 * assignment, the access is marked as both a read and a write.
982 * If "expr" assigns something to a scalar variable, then we keep track
983 * of the assigned expression in assigned_value so that we can plug
984 * it in when we later come across the same variable.
986 struct pet_expr *PetScan::extract_expr(BinaryOperator *expr)
988 struct pet_expr *lhs, *rhs;
989 enum pet_op_type op;
991 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
992 if (op == pet_op_last) {
993 unsupported(expr);
994 return NULL;
997 lhs = extract_expr(expr->getLHS());
998 rhs = extract_expr(expr->getRHS());
1000 if (expr->isAssignmentOp() && lhs && lhs->type == pet_expr_access) {
1001 lhs->acc.write = 1;
1002 if (!expr->isCompoundAssignmentOp())
1003 lhs->acc.read = 0;
1006 if (expr->getOpcode() == BO_Assign &&
1007 lhs && lhs->type == pet_expr_access &&
1008 isl_map_dim(lhs->acc.access, isl_dim_out) == 0) {
1009 isl_id *id = isl_map_get_tuple_id(lhs->acc.access, isl_dim_out);
1010 ValueDecl *decl = (ValueDecl *) isl_id_get_user(id);
1011 assigned_value[decl] = expr->getRHS();
1012 isl_id_free(id);
1015 return pet_expr_new_binary(ctx, op, lhs, rhs);
1018 /* Construct a pet_expr representing a conditional operation.
1020 struct pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
1022 struct pet_expr *cond, *lhs, *rhs;
1024 cond = extract_expr(expr->getCond());
1025 lhs = extract_expr(expr->getTrueExpr());
1026 rhs = extract_expr(expr->getFalseExpr());
1028 return pet_expr_new_ternary(ctx, cond, lhs, rhs);
1031 struct pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
1033 return extract_expr(expr->getSubExpr());
1036 /* Construct a pet_expr representing a floating point value.
1038 struct pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
1040 return pet_expr_new_double(ctx, expr->getValueAsApproximateDouble());
1043 /* Extract an access relation from "expr" and then convert it into
1044 * a pet_expr.
1046 struct pet_expr *PetScan::extract_access_expr(Expr *expr)
1048 isl_map *access;
1049 struct pet_expr *pe;
1051 switch (expr->getStmtClass()) {
1052 case Stmt::ArraySubscriptExprClass:
1053 access = extract_access(cast<ArraySubscriptExpr>(expr));
1054 break;
1055 case Stmt::DeclRefExprClass:
1056 access = extract_access(cast<DeclRefExpr>(expr));
1057 break;
1058 case Stmt::IntegerLiteralClass:
1059 access = extract_access(cast<IntegerLiteral>(expr));
1060 break;
1061 default:
1062 unsupported(expr);
1063 return NULL;
1066 pe = pet_expr_from_access(access);
1068 return pe;
1071 struct pet_expr *PetScan::extract_expr(ParenExpr *expr)
1073 return extract_expr(expr->getSubExpr());
1076 /* Construct a pet_expr representing a function call.
1078 * If we are passing along a pointer to an array element
1079 * or an entire row or even higher dimensional slice of an array,
1080 * then the function being called may write into the array.
1082 * We assume here that if the function is declared to take a pointer
1083 * to a const type, then the function will perform a read
1084 * and that otherwise, it will perform a write.
1086 struct pet_expr *PetScan::extract_expr(CallExpr *expr)
1088 struct pet_expr *res = NULL;
1089 FunctionDecl *fd;
1090 string name;
1092 fd = expr->getDirectCallee();
1093 if (!fd) {
1094 unsupported(expr);
1095 return NULL;
1098 name = fd->getDeclName().getAsString();
1099 res = pet_expr_new_call(ctx, name.c_str(), expr->getNumArgs());
1100 if (!res)
1101 return NULL;
1103 for (int i = 0; i < expr->getNumArgs(); ++i) {
1104 Expr *arg = expr->getArg(i);
1105 int is_addr = 0;
1107 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
1108 ImplicitCastExpr *ice = cast<ImplicitCastExpr>(arg);
1109 arg = ice->getSubExpr();
1111 if (arg->getStmtClass() == Stmt::UnaryOperatorClass) {
1112 UnaryOperator *op = cast<UnaryOperator>(arg);
1113 if (op->getOpcode() == UO_AddrOf) {
1114 is_addr = 1;
1115 arg = op->getSubExpr();
1118 res->args[i] = PetScan::extract_expr(arg);
1119 if (!res->args[i])
1120 goto error;
1121 if (arg->getStmtClass() == Stmt::ArraySubscriptExprClass &&
1122 array_depth(arg->getType().getTypePtr()) > 0)
1123 is_addr = 1;
1124 if (is_addr && res->args[i]->type == pet_expr_access) {
1125 ParmVarDecl *parm = fd->getParamDecl(i);
1126 if (!const_base(parm->getType())) {
1127 res->args[i]->acc.write = 1;
1128 res->args[i]->acc.read = 0;
1133 return res;
1134 error:
1135 pet_expr_free(res);
1136 return NULL;
1139 /* Try and onstruct a pet_expr representing "expr".
1141 struct pet_expr *PetScan::extract_expr(Expr *expr)
1143 switch (expr->getStmtClass()) {
1144 case Stmt::UnaryOperatorClass:
1145 return extract_expr(cast<UnaryOperator>(expr));
1146 case Stmt::CompoundAssignOperatorClass:
1147 case Stmt::BinaryOperatorClass:
1148 return extract_expr(cast<BinaryOperator>(expr));
1149 case Stmt::ImplicitCastExprClass:
1150 return extract_expr(cast<ImplicitCastExpr>(expr));
1151 case Stmt::ArraySubscriptExprClass:
1152 case Stmt::DeclRefExprClass:
1153 case Stmt::IntegerLiteralClass:
1154 return extract_access_expr(expr);
1155 case Stmt::FloatingLiteralClass:
1156 return extract_expr(cast<FloatingLiteral>(expr));
1157 case Stmt::ParenExprClass:
1158 return extract_expr(cast<ParenExpr>(expr));
1159 case Stmt::ConditionalOperatorClass:
1160 return extract_expr(cast<ConditionalOperator>(expr));
1161 case Stmt::CallExprClass:
1162 return extract_expr(cast<CallExpr>(expr));
1163 default:
1164 unsupported(expr);
1166 return NULL;
1169 /* Extract the initialization part of a for loop.
1170 * The initialization is required to be an assignment.
1171 * Return this assignment operator.
1173 BinaryOperator *PetScan::extract_initialization(ForStmt *stmt)
1175 Stmt *init = stmt->getInit();
1176 BinaryOperator *ass;
1178 if (!init) {
1179 unsupported(stmt);
1180 return NULL;
1183 if (init->getStmtClass() != Stmt::BinaryOperatorClass) {
1184 unsupported(init);
1185 return NULL;
1188 ass = cast<BinaryOperator>(init);
1189 if (ass->getOpcode() != BO_Assign) {
1190 unsupported(init);
1191 return NULL;
1194 return ass;
1197 /* Given the assignment operator in the initialization of a for loop,
1198 * extract the induction variable, i.e., the (integer)variable being
1199 * assigned.
1201 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1203 Expr *lhs;
1204 DeclRefExpr *ref;
1205 ValueDecl *decl;
1206 const Type *type;
1208 lhs = init->getLHS();
1209 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1210 unsupported(init);
1211 return NULL;
1214 ref = cast<DeclRefExpr>(lhs);
1215 decl = ref->getDecl();
1216 type = decl->getType().getTypePtr();
1218 if (!type->isIntegerType()) {
1219 unsupported(lhs);
1220 return NULL;
1223 return decl;
1226 /* Check that op is of the form iv++ or iv--.
1227 * "inc" is accordingly set to 1 or -1.
1229 bool PetScan::check_unary_increment(UnaryOperator *op, clang::ValueDecl *iv,
1230 isl_int &inc)
1232 Expr *sub;
1233 DeclRefExpr *ref;
1235 if (!op->isIncrementDecrementOp()) {
1236 unsupported(op);
1237 return false;
1240 if (op->isIncrementOp())
1241 isl_int_set_si(inc, 1);
1242 else
1243 isl_int_set_si(inc, -1);
1245 sub = op->getSubExpr();
1246 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1247 unsupported(op);
1248 return false;
1251 ref = cast<DeclRefExpr>(sub);
1252 if (ref->getDecl() != iv) {
1253 unsupported(op);
1254 return false;
1257 return true;
1260 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1261 * has a single constant expression on a universe domain, then
1262 * put this constant in *user.
1264 static int extract_cst(__isl_take isl_set *set, __isl_take isl_aff *aff,
1265 void *user)
1267 isl_int *inc = (isl_int *)user;
1268 int res = 0;
1270 if (!isl_set_plain_is_universe(set) || !isl_aff_is_cst(aff))
1271 res = -1;
1272 else
1273 isl_aff_get_constant(aff, inc);
1275 isl_set_free(set);
1276 isl_aff_free(aff);
1278 return res;
1281 /* Check if op is of the form
1283 * iv = iv + inc
1285 * with inc a constant and set "inc" accordingly.
1287 * We extract an affine expression from the RHS and the subtract iv.
1288 * The result should be a constant.
1290 bool PetScan::check_binary_increment(BinaryOperator *op, clang::ValueDecl *iv,
1291 isl_int &inc)
1293 Expr *lhs;
1294 DeclRefExpr *ref;
1295 isl_id *id;
1296 isl_dim *dim;
1297 isl_aff *aff;
1298 isl_pw_aff *val;
1300 if (op->getOpcode() != BO_Assign) {
1301 unsupported(op);
1302 return false;
1305 lhs = op->getLHS();
1306 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1307 unsupported(op);
1308 return false;
1311 ref = cast<DeclRefExpr>(lhs);
1312 if (ref->getDecl() != iv) {
1313 unsupported(op);
1314 return false;
1317 val = extract_affine(op->getRHS());
1319 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1321 dim = isl_dim_set_alloc(ctx, 1, 0);
1322 dim = isl_dim_set_dim_id(dim, isl_dim_param, 0, id);
1323 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1324 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1326 val = isl_pw_aff_sub(val, isl_pw_aff_from_aff(aff));
1328 if (isl_pw_aff_foreach_piece(val, &extract_cst, &inc) < 0) {
1329 isl_pw_aff_free(val);
1330 unsupported(op);
1331 return false;
1334 isl_pw_aff_free(val);
1336 return true;
1339 /* Check that op is of the form iv += cst or iv -= cst.
1340 * "inc" is set to cst or -cst accordingly.
1342 bool PetScan::check_compound_increment(CompoundAssignOperator *op,
1343 clang::ValueDecl *iv, isl_int &inc)
1345 Expr *lhs, *rhs;
1346 DeclRefExpr *ref;
1347 bool neg = false;
1349 BinaryOperatorKind opcode;
1351 opcode = op->getOpcode();
1352 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1353 unsupported(op);
1354 return false;
1356 if (opcode == BO_SubAssign)
1357 neg = true;
1359 lhs = op->getLHS();
1360 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1361 unsupported(op);
1362 return false;
1365 ref = cast<DeclRefExpr>(lhs);
1366 if (ref->getDecl() != iv) {
1367 unsupported(op);
1368 return false;
1371 rhs = op->getRHS();
1373 if (rhs->getStmtClass() == Stmt::UnaryOperatorClass) {
1374 UnaryOperator *op = cast<UnaryOperator>(rhs);
1375 if (op->getOpcode() != UO_Minus) {
1376 unsupported(op);
1377 return false;
1380 neg = !neg;
1382 rhs = op->getSubExpr();
1385 if (rhs->getStmtClass() != Stmt::IntegerLiteralClass) {
1386 unsupported(op);
1387 return false;
1390 extract_int(cast<IntegerLiteral>(rhs), &inc);
1391 if (neg)
1392 isl_int_neg(inc, inc);
1394 return true;
1397 /* Check that the increment of the given for loop increments
1398 * (or decrements) the induction variable "iv".
1399 * "up" is set to true if the induction variable is incremented.
1401 bool PetScan::check_increment(ForStmt *stmt, ValueDecl *iv, isl_int &v)
1403 Stmt *inc = stmt->getInc();
1405 if (!inc) {
1406 unsupported(stmt);
1407 return false;
1410 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1411 return check_unary_increment(cast<UnaryOperator>(inc), iv, v);
1412 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1413 return check_compound_increment(
1414 cast<CompoundAssignOperator>(inc), iv, v);
1415 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1416 return check_binary_increment(cast<BinaryOperator>(inc), iv, v);
1418 unsupported(inc);
1419 return false;
1422 /* Embed the given iteration domain in an extra outer loop
1423 * with induction variable "var".
1424 * If this variable appeared as a parameter in the constraints,
1425 * it is replaced by the new outermost dimension.
1427 static __isl_give isl_set *embed(__isl_take isl_set *set,
1428 __isl_take isl_id *var)
1430 int pos;
1432 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
1433 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
1434 if (pos >= 0) {
1435 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
1436 set = isl_set_project_out(set, isl_dim_param, pos, 1);
1439 isl_id_free(var);
1440 return set;
1443 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1445 * for (;;)
1446 * body
1448 * We extract a pet_scop for the body and then embed it in a loop with
1449 * iteration domain
1451 * { [t] : t >= 0 }
1453 * and schedule
1455 * { [t] -> [t] }
1457 struct pet_scop *PetScan::extract_infinite_for(ForStmt *stmt)
1459 isl_id *id;
1460 isl_dim *dim;
1461 isl_set *domain;
1462 isl_map *sched;
1463 struct pet_scop *scop;
1465 scop = extract(stmt->getBody());
1466 if (!scop)
1467 return NULL;
1469 id = isl_id_alloc(ctx, "t", NULL);
1470 domain = isl_set_nat_universe(isl_dim_set_alloc(ctx, 0, 1));
1471 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1472 dim = isl_dim_from_domain(isl_set_get_dim(domain));
1473 dim = isl_dim_add(dim, isl_dim_out, 1);
1474 sched = isl_map_universe(dim);
1475 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1476 scop = pet_scop_embed(scop, domain, sched, id);
1478 return scop;
1481 /* Check whether "cond" expresses a simple loop bound
1482 * on the only set dimension.
1483 * In particular, if "up" is set then "cond" should contain only
1484 * upper bounds on the set dimension.
1485 * Otherwise, it should contain only lower bounds.
1487 static bool is_simple_bound(__isl_keep isl_set *cond, isl_int inc)
1489 if (isl_int_is_pos(inc))
1490 return !isl_set_dim_has_lower_bound(cond, isl_dim_set, 0);
1491 else
1492 return !isl_set_dim_has_upper_bound(cond, isl_dim_set, 0);
1495 /* Extend a condition on a given iteration of a loop to one that
1496 * imposes the same condition on all previous iterations.
1497 * "domain" expresses the lower [upper] bound on the iterations
1498 * when up is set [not set].
1500 * In particular, we construct the condition (when up is set)
1502 * forall i' : (domain(i') and i' <= i) => cond(i')
1504 * which is equivalent to
1506 * not exists i' : domain(i') and i' <= i and not cond(i')
1508 * We construct this set by negating cond, applying a map
1510 * { [i'] -> [i] : domain(i') and i' <= i }
1512 * and then negating the result again.
1514 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1515 __isl_take isl_set *domain, isl_int inc)
1517 isl_map *previous_to_this;
1519 if (isl_int_is_pos(inc))
1520 previous_to_this = isl_map_lex_le(isl_set_get_dim(domain));
1521 else
1522 previous_to_this = isl_map_lex_ge(isl_set_get_dim(domain));
1524 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
1526 cond = isl_set_complement(cond);
1527 cond = isl_set_apply(cond, previous_to_this);
1528 cond = isl_set_complement(cond);
1530 return cond;
1533 /* Construct a domain of the form
1535 * [id] -> { [] : exists a: id = init + a * inc and a >= 0 }
1537 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
1538 __isl_take isl_pw_aff *init, isl_int inc)
1540 isl_aff *aff;
1541 isl_dim *dim;
1542 isl_set *set;
1544 init = isl_pw_aff_insert_dims(init, isl_dim_set, 0, 1);
1545 aff = isl_aff_zero(isl_local_space_from_dim(isl_pw_aff_get_dim(init)));
1546 aff = isl_aff_add_coefficient(aff, isl_dim_set, 0, inc);
1547 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1549 dim = isl_dim_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
1550 dim = isl_dim_set_dim_id(dim, isl_dim_param, 0, id);
1551 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1552 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1554 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1556 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
1558 return isl_set_project_out(set, isl_dim_set, 0, 1);
1561 static unsigned get_type_size(ValueDecl *decl)
1563 return decl->getASTContext().getIntWidth(decl->getType());
1566 /* Assuming "cond" represents a simple bound on a loop where the loop
1567 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1568 * is possible.
1570 * Under the given assumptions, wrapping is only possible if "cond" allows
1571 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1572 * increasing iterator and 0 in case of a decreasing iterator.
1574 static bool can_wrap(__isl_keep isl_set *cond, ValueDecl *iv, isl_int inc)
1576 bool cw;
1577 isl_int limit;
1578 isl_set *test;
1580 test = isl_set_copy(cond);
1582 isl_int_init(limit);
1583 if (isl_int_is_neg(inc))
1584 isl_int_set_si(limit, 0);
1585 else {
1586 isl_int_set_si(limit, 1);
1587 isl_int_mul_2exp(limit, limit, get_type_size(iv));
1588 isl_int_sub_ui(limit, limit, 1);
1591 test = isl_set_fix(cond, isl_dim_set, 0, limit);
1592 cw = !isl_set_is_empty(test);
1593 isl_set_free(test);
1595 isl_int_clear(limit);
1597 return cw;
1600 /* Given a one-dimensional space, construct the following mapping on this
1601 * space
1603 * { [v] -> [v mod 2^width] }
1605 * where width is the number of bits used to represent the values
1606 * of the unsigned variable "iv".
1608 static __isl_give isl_map *compute_wrapping(__isl_take isl_dim *dim,
1609 ValueDecl *iv)
1611 isl_int mod;
1612 isl_aff *aff;
1613 isl_map *map;
1615 isl_int_init(mod);
1616 isl_int_set_si(mod, 1);
1617 isl_int_mul_2exp(mod, mod, get_type_size(iv));
1619 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1620 aff = isl_aff_add_coefficient_si(aff, isl_dim_set, 0, 1);
1621 aff = isl_aff_mod(aff, mod);
1623 isl_int_clear(mod);
1625 return isl_map_from_basic_map(isl_basic_map_from_aff(aff));
1626 map = isl_map_reverse(map);
1629 /* Construct a pet_scop for a for statement.
1630 * The for loop is required to be of the form
1632 * for (i = init; condition; ++i)
1634 * or
1636 * for (i = init; condition; --i)
1638 * We extract a pet_scop for the body and then embed it in a loop with
1639 * iteration domain and schedule
1641 * { [i] : i >= init and condition' }
1642 * { [i] -> [i] }
1644 * or
1646 * { [i] : i <= init and condition' }
1647 * { [i] -> [-i] }
1649 * Where condition' is equal to condition if the latter is
1650 * a simple upper [lower] bound and a condition that is extended
1651 * to apply to all previous iterations otherwise.
1653 * If the stride of the loop is not 1, then "i >= init" is replaced by
1655 * (exists a: i = init + stride * a and a >= 0)
1657 * If the loop iterator i is unsigned, then wrapping may occur.
1658 * During the computation, we work with a virtual iterator that
1659 * does not wrap. However, the condition in the code applies
1660 * to the wrapped value, so we need to change condition(i)
1661 * into condition([i % 2^width]).
1662 * After computing the virtual domain and schedule, we apply
1663 * the function { [v] -> [v % 2^width] } to the domain and the domain
1664 * of the schedule. In order not to lose any information, we also
1665 * need to intersect the domain of the schedule with the virtual domain
1666 * first, since some iterations in the wrapped domain may be scheduled
1667 * several times, typically an infinite number of times.
1668 * Note that there is no need to perform this final wrapping
1669 * if the loop condition (after wrapping) is simple.
1671 * Wrapping on unsigned iterators can be avoided entirely if
1672 * loop condition is simple, the loop iterator is incremented
1673 * [decremented] by one and the last value before wrapping cannot
1674 * possibly satisfy the loop condition.
1676 * Before extracting a pet_scop from the body we remove all
1677 * assignments in assigned_value to variables that are assigned
1678 * somewhere in the body of the loop.
1680 struct pet_scop *PetScan::extract_for(ForStmt *stmt)
1682 BinaryOperator *init;
1683 ValueDecl *iv;
1684 isl_dim *dim;
1685 isl_set *domain;
1686 isl_map *sched;
1687 isl_set *cond;
1688 isl_id *id;
1689 struct pet_scop *scop;
1690 assigned_value_cache cache(assigned_value);
1691 isl_int inc;
1692 bool is_one;
1693 bool is_unsigned;
1694 bool is_simple;
1695 isl_map *wrap = NULL;
1697 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc())
1698 return extract_infinite_for(stmt);
1700 init = PetScan::extract_initialization(stmt);
1701 if (!init)
1702 return NULL;
1703 iv = extract_induction_variable(init);
1704 if (!iv)
1705 return NULL;
1707 isl_int_init(inc);
1708 if (!check_increment(stmt, iv, inc)) {
1709 isl_int_clear(inc);
1710 return NULL;
1713 is_unsigned = iv->getType()->isUnsignedIntegerType();
1715 assigned_value[iv] = NULL;
1716 clear_assignments clear(assigned_value);
1717 clear.TraverseStmt(stmt->getBody());
1719 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1721 is_one = isl_int_is_one(inc) || isl_int_is_negone(inc);
1722 if (is_one)
1723 domain = extract_comparison(isl_int_is_pos(inc) ? BO_GE : BO_LE,
1724 init->getLHS(), init->getRHS(), init);
1725 else {
1726 isl_pw_aff *lb = extract_affine(init->getRHS());
1727 domain = strided_domain(isl_id_copy(id), lb, inc);
1730 cond = extract_condition(stmt->getCond());
1731 cond = embed(cond, isl_id_copy(id));
1732 domain = embed(domain, isl_id_copy(id));
1733 is_simple = is_simple_bound(cond, inc);
1734 if (is_unsigned &&
1735 (!is_simple || !is_one || can_wrap(cond, iv, inc))) {
1736 wrap = compute_wrapping(isl_set_get_dim(cond), iv);
1737 cond = isl_set_apply(cond, isl_map_reverse(isl_map_copy(wrap)));
1738 is_simple = is_simple && is_simple_bound(cond, inc);
1740 if (!is_simple)
1741 cond = valid_for_each_iteration(cond,
1742 isl_set_copy(domain), inc);
1743 domain = isl_set_intersect(domain, cond);
1744 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1745 dim = isl_dim_from_domain(isl_set_get_dim(domain));
1746 dim = isl_dim_add(dim, isl_dim_out, 1);
1747 sched = isl_map_universe(dim);
1748 if (isl_int_is_pos(inc))
1749 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1750 else
1751 sched = isl_map_oppose(sched, isl_dim_in, 0, isl_dim_out, 0);
1753 if (is_unsigned && !is_simple) {
1754 wrap = isl_map_set_dim_id(wrap,
1755 isl_dim_out, 0, isl_id_copy(id));
1756 sched = isl_map_intersect_domain(sched, isl_set_copy(domain));
1757 domain = isl_set_apply(domain, isl_map_copy(wrap));
1758 sched = isl_map_apply_domain(sched, wrap);
1759 } else
1760 isl_map_free(wrap);
1762 scop = extract(stmt->getBody());
1763 scop = pet_scop_embed(scop, domain, sched, id);
1765 isl_int_clear(inc);
1766 return scop;
1769 struct pet_scop *PetScan::extract(CompoundStmt *stmt)
1771 return extract(stmt->children());
1774 /* Look for parameters in any access relation in "expr" that
1775 * refer to non-affine constructs. In particular, these are
1776 * parameters with no name.
1778 * If there are any such parameters, then the domain of the access
1779 * relation, which is still [] at this point, is replaced by
1780 * [[] -> [t_1,...,t_n]], with n the number of these parameters
1781 * (after identifying identical non-affine constructs).
1782 * The parameters are then equated to the corresponding t dimensions
1783 * and subsequently projected out.
1784 * param2pos maps the position of the parameter to the position
1785 * of the corresponding t dimension.
1787 struct pet_expr *PetScan::resolve_nested(struct pet_expr *expr)
1789 int n;
1790 int nparam;
1791 int n_in;
1792 isl_dim *dim;
1793 isl_map *map;
1794 std::map<int,int> param2pos;
1796 if (!expr)
1797 return expr;
1799 for (int i = 0; i < expr->n_arg; ++i) {
1800 expr->args[i] = resolve_nested(expr->args[i]);
1801 if (!expr->args[i]) {
1802 pet_expr_free(expr);
1803 return NULL;
1807 if (expr->type != pet_expr_access)
1808 return expr;
1810 nparam = isl_map_dim(expr->acc.access, isl_dim_param);
1811 n = 0;
1812 for (int i = 0; i < nparam; ++i) {
1813 isl_id *id = isl_map_get_dim_id(expr->acc.access,
1814 isl_dim_param, i);
1815 if (id && isl_id_get_user(id) && !isl_id_get_name(id))
1816 n++;
1817 isl_id_free(id);
1820 if (n == 0)
1821 return expr;
1823 expr->n_arg = n;
1824 expr->args = isl_calloc_array(ctx, struct pet_expr *, n);
1825 if (!expr->args)
1826 goto error;
1828 n_in = isl_map_dim(expr->acc.access, isl_dim_in);
1829 for (int i = 0, pos = 0; i < nparam; ++i) {
1830 int j;
1831 isl_id *id = isl_map_get_dim_id(expr->acc.access,
1832 isl_dim_param, i);
1833 Expr *nested;
1835 if (!(id && isl_id_get_user(id) && !isl_id_get_name(id))) {
1836 isl_id_free(id);
1837 continue;
1840 nested = (Expr *) isl_id_get_user(id);
1841 expr->args[pos] = extract_expr(nested);
1843 for (j = 0; j < pos; ++j)
1844 if (pet_expr_is_equal(expr->args[j], expr->args[pos]))
1845 break;
1847 if (j < pos) {
1848 pet_expr_free(expr->args[pos]);
1849 param2pos[i] = n_in + j;
1850 n--;
1851 } else
1852 param2pos[i] = n_in + pos++;
1854 isl_id_free(id);
1856 expr->n_arg = n;
1858 dim = isl_map_get_dim(expr->acc.access);
1859 dim = isl_dim_domain(dim);
1860 dim = isl_dim_from_domain(dim);
1861 dim = isl_dim_add(dim, isl_dim_out, n);
1862 map = isl_map_universe(dim);
1863 map = isl_map_domain_map(map);
1864 map = isl_map_reverse(map);
1865 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
1867 for (int i = nparam - 1; i >= 0; --i) {
1868 isl_id *id = isl_map_get_dim_id(expr->acc.access,
1869 isl_dim_param, i);
1870 if (!(id && isl_id_get_user(id) && !isl_id_get_name(id))) {
1871 isl_id_free(id);
1872 continue;
1875 expr->acc.access = isl_map_equate(expr->acc.access,
1876 isl_dim_param, i, isl_dim_in,
1877 param2pos[i]);
1878 expr->acc.access = isl_map_project_out(expr->acc.access,
1879 isl_dim_param, i, 1);
1881 isl_id_free(id);
1884 return expr;
1885 error:
1886 pet_expr_free(expr);
1887 return NULL;
1890 /* Convert a top-level pet_expr to a pet_scop with one statement.
1891 * This mainly involves resolving nested expression parameters
1892 * and setting the name of the iteration space.
1894 struct pet_scop *PetScan::extract(Stmt *stmt, struct pet_expr *expr)
1896 struct pet_stmt *ps;
1897 SourceLocation loc = stmt->getLocStart();
1898 int line = PP.getSourceManager().getExpansionLineNumber(loc);
1900 expr = resolve_nested(expr);
1901 ps = pet_stmt_from_pet_expr(ctx, line, n_stmt++, expr);
1902 return pet_scop_from_pet_stmt(ctx, ps);
1905 /* Check whether "expr" is an affine expression.
1906 * We turn on autodetection so that we won't generate any warnings
1907 * and turn off nesting, so that we won't accept any non-affine constructs.
1909 bool PetScan::is_affine(Expr *expr)
1911 isl_pw_aff *pwaff;
1912 int save_autodetect = autodetect;
1913 bool save_nesting = nesting_enabled;
1915 autodetect = 1;
1916 nesting_enabled = false;
1918 pwaff = extract_affine(expr);
1919 isl_pw_aff_free(pwaff);
1921 autodetect = save_autodetect;
1922 nesting_enabled = save_nesting;
1924 return pwaff != NULL;
1927 /* Check whether "expr" is an affine constraint.
1928 * We turn on autodetection so that we won't generate any warnings
1929 * and turn off nesting, so that we won't accept any non-affine constructs.
1931 bool PetScan::is_affine_condition(Expr *expr)
1933 isl_set *set;
1934 int save_autodetect = autodetect;
1935 bool save_nesting = nesting_enabled;
1937 autodetect = 1;
1938 nesting_enabled = false;
1940 set = extract_condition(expr);
1941 isl_set_free(set);
1943 autodetect = save_autodetect;
1944 nesting_enabled = save_nesting;
1946 return set != NULL;
1949 /* If the top-level expression of "stmt" is an assignment, then
1950 * return that assignment as a BinaryOperator.
1951 * Otherwise return NULL.
1953 static BinaryOperator *top_assignment_or_null(Stmt *stmt)
1955 BinaryOperator *ass;
1957 if (!stmt)
1958 return NULL;
1959 if (stmt->getStmtClass() != Stmt::BinaryOperatorClass)
1960 return NULL;
1962 ass = cast<BinaryOperator>(stmt);
1963 if(ass->getOpcode() != BO_Assign)
1964 return NULL;
1966 return ass;
1969 /* Check if the given if statement is a conditional assignement
1970 * with a non-affine condition. If so, construct a pet_scop
1971 * corresponding to this conditional assignment. Otherwise return NULL.
1973 * In particular we check if "stmt" is of the form
1975 * if (condition)
1976 * a = f(...);
1977 * else
1978 * a = g(...);
1980 * where a is some array or scalar access.
1981 * The constructed pet_scop then corresponds to the expression
1983 * a = condition ? f(...) : g(...)
1985 * All access relations in f(...) are intersected with condition
1986 * while all access relation in g(...) are intersected with the complement.
1988 struct pet_scop *PetScan::extract_conditional_assignment(IfStmt *stmt)
1990 BinaryOperator *ass_then, *ass_else;
1991 isl_map *write_then, *write_else;
1992 isl_set *cond, *comp;
1993 isl_map *map, *map_true, *map_false;
1994 int equal;
1995 struct pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
1996 bool save_nesting = nesting_enabled;
1998 ass_then = top_assignment_or_null(stmt->getThen());
1999 ass_else = top_assignment_or_null(stmt->getElse());
2001 if (!ass_then || !ass_else)
2002 return NULL;
2004 if (is_affine_condition(stmt->getCond()))
2005 return NULL;
2007 write_then = extract_access(ass_then->getLHS());
2008 write_else = extract_access(ass_else->getLHS());
2010 equal = isl_map_is_equal(write_then, write_else);
2011 isl_map_free(write_else);
2012 if (equal < 0 || !equal) {
2013 isl_map_free(write_then);
2014 return NULL;
2017 nesting_enabled = allow_nested;
2018 cond = extract_condition(stmt->getCond());
2019 nesting_enabled = save_nesting;
2020 comp = isl_set_complement(isl_set_copy(cond));
2021 map_true = isl_map_from_domain(isl_set_copy(cond));
2022 map_true = isl_map_add_dims(map_true, isl_dim_out, 1);
2023 map_true = isl_map_fix_si(map_true, isl_dim_out, 0, 1);
2024 map_false = isl_map_from_domain(isl_set_copy(comp));
2025 map_false = isl_map_add_dims(map_false, isl_dim_out, 1);
2026 map_false = isl_map_fix_si(map_false, isl_dim_out, 0, 0);
2027 map = isl_map_union_disjoint(map_true, map_false);
2029 pe_cond = pet_expr_from_access(map);
2031 pe_then = extract_expr(ass_then->getRHS());
2032 pe_then = pet_expr_restrict(pe_then, cond);
2033 pe_else = extract_expr(ass_else->getRHS());
2034 pe_else = pet_expr_restrict(pe_else, comp);
2036 pe = pet_expr_new_ternary(ctx, pe_cond, pe_then, pe_else);
2037 pe_write = pet_expr_from_access(write_then);
2038 if (pe_write) {
2039 pe_write->acc.write = 1;
2040 pe_write->acc.read = 0;
2042 pe = pet_expr_new_binary(ctx, pet_op_assign, pe_write, pe);
2043 return extract(stmt, pe);
2046 /* Construct a pet_scop for an if statement.
2048 struct pet_scop *PetScan::extract(IfStmt *stmt)
2050 isl_set *cond;
2051 struct pet_scop *scop_then, *scop_else, *scop;
2052 assigned_value_cache cache(assigned_value);
2054 scop = extract_conditional_assignment(stmt);
2055 if (scop)
2056 return scop;
2058 scop_then = extract(stmt->getThen());
2060 if (stmt->getElse()) {
2061 scop_else = extract(stmt->getElse());
2062 if (autodetect) {
2063 if (scop_then && !scop_else) {
2064 partial = true;
2065 return scop_then;
2067 if (!scop_then && scop_else) {
2068 partial = true;
2069 return scop_else;
2074 cond = extract_condition(stmt->getCond());
2075 scop = pet_scop_restrict(scop_then, isl_set_copy(cond));
2077 if (stmt->getElse()) {
2078 cond = isl_set_complement(cond);
2079 scop_else = pet_scop_restrict(scop_else, cond);
2080 scop = pet_scop_add(ctx, scop, scop_else);
2081 } else
2082 isl_set_free(cond);
2084 return scop;
2087 /* Try and construct a pet_scop corresponding to "stmt".
2089 struct pet_scop *PetScan::extract(Stmt *stmt)
2091 if (isa<Expr>(stmt))
2092 return extract(stmt, extract_expr(cast<Expr>(stmt)));
2094 switch (stmt->getStmtClass()) {
2095 case Stmt::ForStmtClass:
2096 return extract_for(cast<ForStmt>(stmt));
2097 case Stmt::IfStmtClass:
2098 return extract(cast<IfStmt>(stmt));
2099 case Stmt::CompoundStmtClass:
2100 return extract(cast<CompoundStmt>(stmt));
2101 default:
2102 unsupported(stmt);
2105 return NULL;
2108 /* Try and construct a pet_scop corresponding to (part of)
2109 * a sequence of statements.
2111 struct pet_scop *PetScan::extract(StmtRange stmt_range)
2113 pet_scop *scop;
2114 StmtIterator i;
2115 int j;
2116 bool partial_range = false;
2118 if (autodetect)
2119 scop = NULL;
2120 else
2121 scop = pet_scop_empty(ctx);
2122 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j) {
2123 Stmt *child = *i;
2124 struct pet_scop *scop_i;
2125 scop_i = extract(child);
2126 if (scop && partial) {
2127 pet_scop_free(scop_i);
2128 break;
2130 scop_i = pet_scop_prefix(scop_i, j);
2131 if (autodetect) {
2132 if (!scop) {
2133 if (j != 0)
2134 partial_range = true;
2135 scop = scop_i;
2136 } else if (scop_i)
2137 scop = pet_scop_add(ctx, scop, scop_i);
2138 else
2139 partial = true;
2140 } else {
2141 scop = pet_scop_add(ctx, scop, scop_i);
2143 if (partial)
2144 break;
2147 if (scop && partial_range)
2148 partial = true;
2150 return scop;
2153 /* Check if the scop marked by the user is exactly this Stmt
2154 * or part of this Stmt.
2155 * If so, return a pet_scop corresponding to the marked region.
2156 * Otherwise, return NULL.
2158 struct pet_scop *PetScan::scan(Stmt *stmt)
2160 SourceManager &SM = PP.getSourceManager();
2161 unsigned start_off, end_off;
2163 start_off = SM.getFileOffset(stmt->getLocStart());
2164 end_off = SM.getFileOffset(stmt->getLocEnd());
2166 if (start_off > loc.end)
2167 return NULL;
2168 if (end_off < loc.start)
2169 return NULL;
2170 if (start_off >= loc.start && end_off <= loc.end) {
2171 return extract(stmt);
2174 StmtIterator start;
2175 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
2176 Stmt *child = *start;
2177 start_off = SM.getFileOffset(child->getLocStart());
2178 end_off = SM.getFileOffset(child->getLocEnd());
2179 if (start_off < loc.start && end_off > loc.end)
2180 return scan(child);
2181 if (start_off >= loc.start)
2182 break;
2185 StmtIterator end;
2186 for (end = start; end != stmt->child_end(); ++end) {
2187 Stmt *child = *end;
2188 start_off = SM.getFileOffset(child->getLocStart());
2189 if (start_off >= loc.end)
2190 break;
2193 return extract(StmtRange(start, end));
2196 /* Set the size of index "pos" of "array" to "size".
2197 * In particular, add a constraint of the form
2199 * i_pos < size
2201 * to array->extent and a constraint of the form
2203 * size >= 0
2205 * to array->context.
2207 static struct pet_array *update_size(struct pet_array *array, int pos,
2208 __isl_take isl_pw_aff *size)
2210 isl_set *valid;
2211 isl_set *univ;
2212 isl_set *bound;
2213 isl_dim *dim;
2214 isl_aff *aff;
2215 isl_pw_aff *index;
2216 isl_id *id;
2218 valid = isl_pw_aff_nonneg_set(isl_pw_aff_copy(size));
2219 array->context = isl_set_intersect(array->context, valid);
2221 dim = isl_set_get_dim(array->extent);
2222 aff = isl_aff_zero(isl_local_space_from_dim(dim));
2223 aff = isl_aff_add_coefficient_si(aff, isl_dim_set, pos, 1);
2224 univ = isl_set_universe(isl_aff_get_dim(aff));
2225 index = isl_pw_aff_alloc(univ, aff);
2227 size = isl_pw_aff_add_dims(size, isl_dim_set,
2228 isl_set_dim(array->extent, isl_dim_set));
2229 id = isl_set_get_tuple_id(array->extent);
2230 size = isl_pw_aff_set_tuple_id(size, id);
2231 bound = isl_pw_aff_lt_set(index, size);
2233 array->extent = isl_set_intersect(array->extent, bound);
2235 if (!array->context || !array->extent)
2236 goto error;
2238 return array;
2239 error:
2240 pet_array_free(array);
2241 return NULL;
2244 /* Figure out the size of the array at position "pos" and all
2245 * subsequent positions from "type" and update "array" accordingly.
2247 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
2248 const Type *type, int pos)
2250 const ArrayType *atype;
2251 isl_pw_aff *size;
2253 if (type->isPointerType()) {
2254 type = type->getPointeeType().getTypePtr();
2255 return set_upper_bounds(array, type, pos + 1);
2257 if (!type->isArrayType())
2258 return array;
2260 type = type->getCanonicalTypeInternal().getTypePtr();
2261 atype = cast<ArrayType>(type);
2263 if (type->isConstantArrayType()) {
2264 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
2265 size = extract_affine(ca->getSize());
2266 array = update_size(array, pos, size);
2267 } else if (type->isVariableArrayType()) {
2268 const VariableArrayType *vla = cast<VariableArrayType>(atype);
2269 size = extract_affine(vla->getSizeExpr());
2270 array = update_size(array, pos, size);
2273 type = atype->getElementType().getTypePtr();
2275 return set_upper_bounds(array, type, pos + 1);
2278 /* Construct and return a pet_array corresponding to the variable "decl".
2279 * In particular, initialize array->extent to
2281 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2283 * and then call set_upper_bounds to set the upper bounds on the indices
2284 * based on the type of the variable.
2286 struct pet_array *PetScan::extract_array(isl_ctx *ctx, ValueDecl *decl)
2288 struct pet_array *array;
2289 QualType qt = decl->getType();
2290 const Type *type = qt.getTypePtr();
2291 int depth = array_depth(type);
2292 QualType base = base_type(qt);
2293 string name;
2294 isl_id *id;
2295 isl_dim *dim;
2297 array = isl_calloc_type(ctx, struct pet_array);
2298 if (!array)
2299 return NULL;
2301 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
2302 dim = isl_dim_set_alloc(ctx, 0, depth);
2303 dim = isl_dim_set_tuple_id(dim, isl_dim_set, id);
2305 array->extent = isl_set_nat_universe(dim);
2307 dim = isl_dim_set_alloc(ctx, 0, 0);
2308 array->context = isl_set_universe(dim);
2310 array = set_upper_bounds(array, type, 0);
2311 if (!array)
2312 return NULL;
2314 name = base.getAsString();
2315 array->element_type = strdup(name.c_str());
2317 return array;
2320 /* Construct a list of pet_arrays, one for each array (or scalar)
2321 * accessed inside "scop" add this list to "scop" and return the result.
2323 * The context of "scop" is updated with the intesection of
2324 * the contexts of all arrays, i.e., constraints on the parameters
2325 * that ensure that the arrays have a valid (non-negative) size.
2327 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop)
2329 int i;
2330 set<ValueDecl *> arrays;
2331 set<ValueDecl *>::iterator it;
2333 if (!scop)
2334 return NULL;
2336 pet_scop_collect_arrays(scop, arrays);
2338 scop->n_array = arrays.size();
2339 if (scop->n_array == 0)
2340 return scop;
2342 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
2343 if (!scop->arrays)
2344 goto error;
2346 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
2347 struct pet_array *array;
2348 scop->arrays[i] = array = extract_array(ctx, *it);
2349 if (!scop->arrays[i])
2350 goto error;
2351 scop->context = isl_set_intersect(scop->context,
2352 isl_set_copy(array->context));
2353 if (!scop->context)
2354 goto error;
2357 return scop;
2358 error:
2359 pet_scop_free(scop);
2360 return NULL;
2363 /* Construct a pet_scop from the given function.
2365 struct pet_scop *PetScan::scan(FunctionDecl *fd)
2367 pet_scop *scop;
2368 Stmt *stmt;
2370 stmt = fd->getBody();
2372 if (autodetect)
2373 scop = extract(stmt);
2374 else
2375 scop = scan(stmt);
2376 scop = pet_scop_detect_parameter_accesses(scop);
2377 scop = scan_arrays(scop);
2379 return scop;