update isl for change in isl_set_read_from_str
[pet.git] / scan.cc
blobb613949e243fe5e0cf1a396f8294e6ad78d8589f
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Leiden University.
32 */
34 #include <set>
35 #include <map>
36 #include <iostream>
37 #include <clang/AST/ASTDiagnostic.h>
38 #include <clang/AST/Expr.h>
39 #include <clang/AST/RecursiveASTVisitor.h>
41 #include <isl/id.h>
42 #include <isl/space.h>
43 #include <isl/aff.h>
44 #include <isl/set.h>
46 #include "scan.h"
47 #include "scop.h"
48 #include "scop_plus.h"
50 #include "config.h"
52 using namespace std;
53 using namespace clang;
55 /* Look for any assignments to scalar variables in part of the parse
56 * tree and set assigned_value to NULL for each of them.
57 * Also reset assigned_value if the address of a scalar variable
58 * is being taken.
60 * This ensures that we won't use any previously stored value
61 * in the current subtree and its parents.
63 struct clear_assignments : RecursiveASTVisitor<clear_assignments> {
64 map<ValueDecl *, Expr *> &assigned_value;
66 clear_assignments(map<ValueDecl *, Expr *> &assigned_value) :
67 assigned_value(assigned_value) {}
69 bool VisitUnaryOperator(UnaryOperator *expr) {
70 Expr *arg;
71 DeclRefExpr *ref;
72 ValueDecl *decl;
74 if (expr->getOpcode() != UO_AddrOf)
75 return true;
77 arg = expr->getSubExpr();
78 if (arg->getStmtClass() != Stmt::DeclRefExprClass)
79 return true;
80 ref = cast<DeclRefExpr>(arg);
81 decl = ref->getDecl();
82 assigned_value[decl] = NULL;
83 return true;
86 bool VisitBinaryOperator(BinaryOperator *expr) {
87 Expr *lhs;
88 DeclRefExpr *ref;
89 ValueDecl *decl;
91 if (!expr->isAssignmentOp())
92 return true;
93 lhs = expr->getLHS();
94 if (lhs->getStmtClass() != Stmt::DeclRefExprClass)
95 return true;
96 ref = cast<DeclRefExpr>(lhs);
97 decl = ref->getDecl();
98 assigned_value[decl] = NULL;
99 return true;
103 /* Keep a copy of the currently assigned values.
105 * Any variable that is assigned a value inside the current scope
106 * is removed again when we leave the scope (either because it wasn't
107 * stored in the cache or because it has a different value in the cache).
109 struct assigned_value_cache {
110 map<ValueDecl *, Expr *> &assigned_value;
111 map<ValueDecl *, Expr *> cache;
113 assigned_value_cache(map<ValueDecl *, Expr *> &assigned_value) :
114 assigned_value(assigned_value), cache(assigned_value) {}
115 ~assigned_value_cache() {
116 map<ValueDecl *, Expr *>::iterator it = cache.begin();
117 for (it = assigned_value.begin(); it != assigned_value.end();
118 ++it) {
119 if (!it->second ||
120 (cache.find(it->first) != cache.end() &&
121 cache[it->first] != it->second))
122 cache[it->first] = NULL;
124 assigned_value = cache;
128 /* Called if we found something we (currently) cannot handle.
129 * We'll provide more informative warnings later.
131 * We only actually complain if autodetect is false.
133 void PetScan::unsupported(Stmt *stmt)
135 if (autodetect)
136 return;
138 SourceLocation loc = stmt->getLocStart();
139 Diagnostic &diag = PP.getDiagnostics();
140 unsigned id = diag.getCustomDiagID(Diagnostic::Warning, "unsupported");
141 DiagnosticBuilder B = diag.Report(loc, id) << stmt->getSourceRange();
144 /* Extract an integer from "expr" and store it in "v".
146 int PetScan::extract_int(IntegerLiteral *expr, isl_int *v)
148 const Type *type = expr->getType().getTypePtr();
149 int is_signed = type->hasSignedIntegerRepresentation();
151 if (is_signed) {
152 int64_t i = expr->getValue().getSExtValue();
153 isl_int_set_si(*v, i);
154 } else {
155 uint64_t i = expr->getValue().getZExtValue();
156 isl_int_set_ui(*v, i);
159 return 0;
162 /* Extract an affine expression from the IntegerLiteral "expr".
164 __isl_give isl_pw_aff *PetScan::extract_affine(IntegerLiteral *expr)
166 isl_space *dim = isl_space_set_alloc(ctx, 0, 0);
167 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
168 isl_aff *aff = isl_aff_zero_on_domain(ls);
169 isl_set *dom = isl_set_universe(dim);
170 isl_int v;
172 isl_int_init(v);
173 extract_int(expr, &v);
174 aff = isl_aff_add_constant(aff, v);
175 isl_int_clear(v);
177 return isl_pw_aff_alloc(dom, aff);
180 /* Extract an affine expression from the APInt "val".
182 __isl_give isl_pw_aff *PetScan::extract_affine(const llvm::APInt &val)
184 isl_space *dim = isl_space_set_alloc(ctx, 0, 0);
185 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
186 isl_aff *aff = isl_aff_zero_on_domain(ls);
187 isl_set *dom = isl_set_universe(dim);
188 isl_int v;
190 isl_int_init(v);
191 isl_int_set_ui(v, val.getZExtValue());
192 aff = isl_aff_add_constant(aff, v);
193 isl_int_clear(v);
195 return isl_pw_aff_alloc(dom, aff);
198 __isl_give isl_pw_aff *PetScan::extract_affine(ImplicitCastExpr *expr)
200 return extract_affine(expr->getSubExpr());
203 /* Extract an affine expression from the DeclRefExpr "expr".
205 * If we have recorded an expression that was assigned to the variable
206 * before, then we convert this expressoin to an isl_pw_aff if it is
207 * affine and to an extra parameter otherwise (provided nesting_enabled is set).
209 * Otherwise, we simply return an expression that is equal
210 * to a parameter corresponding to the referenced variable.
212 __isl_give isl_pw_aff *PetScan::extract_affine(DeclRefExpr *expr)
214 ValueDecl *decl = expr->getDecl();
215 const Type *type = decl->getType().getTypePtr();
216 isl_id *id;
217 isl_space *dim;
218 isl_aff *aff;
219 isl_set *dom;
221 if (!type->isIntegerType()) {
222 unsupported(expr);
223 return NULL;
226 if (assigned_value.find(decl) != assigned_value.end() &&
227 assigned_value[decl] != NULL) {
228 if (is_affine(assigned_value[decl]))
229 return extract_affine(assigned_value[decl]);
230 else
231 return non_affine(expr);
234 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
235 dim = isl_space_set_alloc(ctx, 1, 0);
237 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
239 dom = isl_set_universe(isl_space_copy(dim));
240 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
241 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
243 return isl_pw_aff_alloc(dom, aff);
246 /* Extract an affine expression from an integer division operation.
247 * In particular, if "expr" is lhs/rhs, then return
249 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
251 * The second argument (rhs) is required to be a (positive) integer constant.
253 __isl_give isl_pw_aff *PetScan::extract_affine_div(BinaryOperator *expr)
255 Expr *rhs_expr;
256 isl_pw_aff *lhs, *lhs_f, *lhs_c;
257 isl_pw_aff *res;
258 isl_int v;
259 isl_set *cond;
261 rhs_expr = expr->getRHS();
262 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
263 unsupported(expr);
264 return NULL;
267 lhs = extract_affine(expr->getLHS());
268 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
270 isl_int_init(v);
271 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
272 lhs = isl_pw_aff_scale_down(lhs, v);
273 isl_int_clear(v);
275 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(lhs));
276 lhs_c = isl_pw_aff_ceil(lhs);
277 res = isl_pw_aff_cond(cond, lhs_f, lhs_c);
279 return res;
282 /* Extract an affine expression from a modulo operation.
283 * In particular, if "expr" is lhs/rhs, then return
285 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
287 * The second argument (rhs) is required to be a (positive) integer constant.
289 __isl_give isl_pw_aff *PetScan::extract_affine_mod(BinaryOperator *expr)
291 Expr *rhs_expr;
292 isl_pw_aff *lhs, *lhs_f, *lhs_c;
293 isl_pw_aff *res;
294 isl_int v;
295 isl_set *cond;
297 rhs_expr = expr->getRHS();
298 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
299 unsupported(expr);
300 return NULL;
303 lhs = extract_affine(expr->getLHS());
304 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
306 isl_int_init(v);
307 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
308 res = isl_pw_aff_scale_down(isl_pw_aff_copy(lhs), v);
310 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(res));
311 lhs_c = isl_pw_aff_ceil(res);
312 res = isl_pw_aff_cond(cond, lhs_f, lhs_c);
314 res = isl_pw_aff_scale(res, v);
315 isl_int_clear(v);
317 res = isl_pw_aff_sub(lhs, res);
319 return res;
322 /* Extract an affine expression from a multiplication operation.
323 * This is only allowed if at least one of the two arguments
324 * is a (piecewise) constant.
326 __isl_give isl_pw_aff *PetScan::extract_affine_mul(BinaryOperator *expr)
328 isl_pw_aff *lhs;
329 isl_pw_aff *rhs;
331 lhs = extract_affine(expr->getLHS());
332 rhs = extract_affine(expr->getRHS());
334 if (!isl_pw_aff_is_cst(lhs) && !isl_pw_aff_is_cst(rhs)) {
335 isl_pw_aff_free(lhs);
336 isl_pw_aff_free(rhs);
337 unsupported(expr);
338 return NULL;
341 return isl_pw_aff_mul(lhs, rhs);
344 /* Extract an affine expression from an addition or subtraction operation.
346 __isl_give isl_pw_aff *PetScan::extract_affine_add(BinaryOperator *expr)
348 isl_pw_aff *lhs;
349 isl_pw_aff *rhs;
351 lhs = extract_affine(expr->getLHS());
352 rhs = extract_affine(expr->getRHS());
354 switch (expr->getOpcode()) {
355 case BO_Add:
356 return isl_pw_aff_add(lhs, rhs);
357 case BO_Sub:
358 return isl_pw_aff_sub(lhs, rhs);
359 default:
360 isl_pw_aff_free(lhs);
361 isl_pw_aff_free(rhs);
362 return NULL;
367 /* Compute
369 * pwaff mod 2^width
371 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff,
372 unsigned width)
374 isl_int mod;
376 isl_int_init(mod);
377 isl_int_set_si(mod, 1);
378 isl_int_mul_2exp(mod, mod, width);
380 pwaff = isl_pw_aff_mod(pwaff, mod);
382 isl_int_clear(mod);
384 return pwaff;
387 /* Extract an affine expression from some binary operations.
388 * If the result of the expression is unsigned, then we wrap it
389 * based on the size of the type.
391 __isl_give isl_pw_aff *PetScan::extract_affine(BinaryOperator *expr)
393 isl_pw_aff *res;
395 switch (expr->getOpcode()) {
396 case BO_Add:
397 case BO_Sub:
398 res = extract_affine_add(expr);
399 break;
400 case BO_Div:
401 res = extract_affine_div(expr);
402 break;
403 case BO_Rem:
404 res = extract_affine_mod(expr);
405 break;
406 case BO_Mul:
407 res = extract_affine_mul(expr);
408 break;
409 default:
410 unsupported(expr);
411 return NULL;
414 if (expr->getType()->isUnsignedIntegerType())
415 res = wrap(res, ast_context.getIntWidth(expr->getType()));
417 return res;
420 /* Extract an affine expression from a negation operation.
422 __isl_give isl_pw_aff *PetScan::extract_affine(UnaryOperator *expr)
424 if (expr->getOpcode() == UO_Minus)
425 return isl_pw_aff_neg(extract_affine(expr->getSubExpr()));
427 unsupported(expr);
428 return NULL;
431 __isl_give isl_pw_aff *PetScan::extract_affine(ParenExpr *expr)
433 return extract_affine(expr->getSubExpr());
436 /* Extract an affine expression from some special function calls.
437 * In particular, we handle "min", "max", "ceild" and "floord".
438 * In case of the latter two, the second argument needs to be
439 * a (positive) integer constant.
441 __isl_give isl_pw_aff *PetScan::extract_affine(CallExpr *expr)
443 FunctionDecl *fd;
444 string name;
445 isl_pw_aff *aff1, *aff2;
447 fd = expr->getDirectCallee();
448 if (!fd) {
449 unsupported(expr);
450 return NULL;
453 name = fd->getDeclName().getAsString();
454 if (!(expr->getNumArgs() == 2 && name == "min") &&
455 !(expr->getNumArgs() == 2 && name == "max") &&
456 !(expr->getNumArgs() == 2 && name == "floord") &&
457 !(expr->getNumArgs() == 2 && name == "ceild")) {
458 unsupported(expr);
459 return NULL;
462 if (name == "min" || name == "max") {
463 aff1 = extract_affine(expr->getArg(0));
464 aff2 = extract_affine(expr->getArg(1));
466 if (name == "min")
467 aff1 = isl_pw_aff_min(aff1, aff2);
468 else
469 aff1 = isl_pw_aff_max(aff1, aff2);
470 } else if (name == "floord" || name == "ceild") {
471 isl_int v;
472 Expr *arg2 = expr->getArg(1);
474 if (arg2->getStmtClass() != Stmt::IntegerLiteralClass) {
475 unsupported(expr);
476 return NULL;
478 aff1 = extract_affine(expr->getArg(0));
479 isl_int_init(v);
480 extract_int(cast<IntegerLiteral>(arg2), &v);
481 aff1 = isl_pw_aff_scale_down(aff1, v);
482 isl_int_clear(v);
483 if (name == "floord")
484 aff1 = isl_pw_aff_floor(aff1);
485 else
486 aff1 = isl_pw_aff_ceil(aff1);
487 } else {
488 unsupported(expr);
489 return NULL;
492 return aff1;
496 /* This method is called when we come across a non-affine expression.
497 * If nesting is allowed, we return a new parameter that corresponds
498 * to the non-affine expression. Otherwise, we simply complain.
500 * The new parameter is resolved in resolve_nested.
502 isl_pw_aff *PetScan::non_affine(Expr *expr)
504 isl_id *id;
505 isl_space *dim;
506 isl_aff *aff;
507 isl_set *dom;
509 if (!nesting_enabled) {
510 unsupported(expr);
511 return NULL;
514 id = isl_id_alloc(ctx, NULL, expr);
515 dim = isl_space_set_alloc(ctx, 1, 0);
517 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
519 dom = isl_set_universe(isl_space_copy(dim));
520 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
521 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
523 return isl_pw_aff_alloc(dom, aff);
526 /* Affine expressions are not supposed to contain array accesses,
527 * but if nesting is allowed, we return a parameter corresponding
528 * to the array access.
530 __isl_give isl_pw_aff *PetScan::extract_affine(ArraySubscriptExpr *expr)
532 return non_affine(expr);
535 /* Extract an affine expression from a conditional operation.
537 __isl_give isl_pw_aff *PetScan::extract_affine(ConditionalOperator *expr)
539 isl_set *cond;
540 isl_pw_aff *lhs, *rhs;
542 cond = extract_condition(expr->getCond());
543 lhs = extract_affine(expr->getTrueExpr());
544 rhs = extract_affine(expr->getFalseExpr());
546 return isl_pw_aff_cond(cond, lhs, rhs);
549 /* Extract an affine expression, if possible, from "expr".
550 * Otherwise return NULL.
552 __isl_give isl_pw_aff *PetScan::extract_affine(Expr *expr)
554 switch (expr->getStmtClass()) {
555 case Stmt::ImplicitCastExprClass:
556 return extract_affine(cast<ImplicitCastExpr>(expr));
557 case Stmt::IntegerLiteralClass:
558 return extract_affine(cast<IntegerLiteral>(expr));
559 case Stmt::DeclRefExprClass:
560 return extract_affine(cast<DeclRefExpr>(expr));
561 case Stmt::BinaryOperatorClass:
562 return extract_affine(cast<BinaryOperator>(expr));
563 case Stmt::UnaryOperatorClass:
564 return extract_affine(cast<UnaryOperator>(expr));
565 case Stmt::ParenExprClass:
566 return extract_affine(cast<ParenExpr>(expr));
567 case Stmt::CallExprClass:
568 return extract_affine(cast<CallExpr>(expr));
569 case Stmt::ArraySubscriptExprClass:
570 return extract_affine(cast<ArraySubscriptExpr>(expr));
571 case Stmt::ConditionalOperatorClass:
572 return extract_affine(cast<ConditionalOperator>(expr));
573 default:
574 unsupported(expr);
576 return NULL;
579 __isl_give isl_map *PetScan::extract_access(ImplicitCastExpr *expr)
581 return extract_access(expr->getSubExpr());
584 /* Return the depth of an array of the given type.
586 static int array_depth(const Type *type)
588 if (type->isPointerType())
589 return 1 + array_depth(type->getPointeeType().getTypePtr());
590 if (type->isArrayType()) {
591 const ArrayType *atype;
592 type = type->getCanonicalTypeInternal().getTypePtr();
593 atype = cast<ArrayType>(type);
594 return 1 + array_depth(atype->getElementType().getTypePtr());
596 return 0;
599 /* Return the element type of the given array type.
601 static QualType base_type(QualType qt)
603 const Type *type = qt.getTypePtr();
605 if (type->isPointerType())
606 return base_type(type->getPointeeType());
607 if (type->isArrayType()) {
608 const ArrayType *atype;
609 type = type->getCanonicalTypeInternal().getTypePtr();
610 atype = cast<ArrayType>(type);
611 return base_type(atype->getElementType());
613 return qt;
616 /* Check if the element type corresponding to the given array type
617 * has a const qualifier.
619 static bool const_base(QualType qt)
621 const Type *type = qt.getTypePtr();
623 if (type->isPointerType())
624 return const_base(type->getPointeeType());
625 if (type->isArrayType()) {
626 const ArrayType *atype;
627 type = type->getCanonicalTypeInternal().getTypePtr();
628 atype = cast<ArrayType>(type);
629 return const_base(atype->getElementType());
632 return qt.isConstQualified();
635 /* Extract an access relation from a reference to a variable.
636 * If the variable has name "A" and its type corresponds to an
637 * array of depth d, then the returned access relation is of the
638 * form
640 * { [] -> A[i_1,...,i_d] }
642 __isl_give isl_map *PetScan::extract_access(DeclRefExpr *expr)
644 ValueDecl *decl = expr->getDecl();
645 int depth = array_depth(decl->getType().getTypePtr());
646 isl_id *id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
647 isl_space *dim = isl_space_alloc(ctx, 0, 0, depth);
648 isl_map *access_rel;
650 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
652 access_rel = isl_map_universe(dim);
654 return access_rel;
657 /* Extract an access relation from an integer contant.
658 * If the value of the constant is "v", then the returned access relation
659 * is
661 * { [] -> [v] }
663 __isl_give isl_map *PetScan::extract_access(IntegerLiteral *expr)
665 return isl_map_from_pw_aff(extract_affine(expr));
668 /* Try and extract an access relation from the given Expr.
669 * Return NULL if it doesn't work out.
671 __isl_give isl_map *PetScan::extract_access(Expr *expr)
673 switch (expr->getStmtClass()) {
674 case Stmt::ImplicitCastExprClass:
675 return extract_access(cast<ImplicitCastExpr>(expr));
676 case Stmt::DeclRefExprClass:
677 return extract_access(cast<DeclRefExpr>(expr));
678 case Stmt::ArraySubscriptExprClass:
679 return extract_access(cast<ArraySubscriptExpr>(expr));
680 default:
681 unsupported(expr);
683 return NULL;
686 /* Assign the affine expression "index" to the output dimension "pos" of "map"
687 * and return the result.
689 __isl_give isl_map *set_index(__isl_take isl_map *map, int pos,
690 __isl_take isl_pw_aff *index)
692 isl_map *index_map;
693 int len = isl_map_dim(map, isl_dim_out);
694 isl_id *id;
696 index_map = isl_map_from_pw_aff(index);
697 index_map = isl_map_insert_dims(index_map, isl_dim_out, 0, pos);
698 index_map = isl_map_add_dims(index_map, isl_dim_out, len - pos - 1);
699 id = isl_map_get_tuple_id(map, isl_dim_out);
700 index_map = isl_map_set_tuple_id(index_map, isl_dim_out, id);
702 map = isl_map_intersect(map, index_map);
704 return map;
707 /* Extract an access relation from the given array subscript expression.
708 * If nesting is allowed in general, then we turn it on while
709 * examining the index expression.
711 * We first extract an access relation from the base.
712 * This will result in an access relation with a range that corresponds
713 * to the array being accessed and with earlier indices filled in already.
714 * We then extract the current index and fill that in as well.
715 * The position of the current index is based on the type of base.
716 * If base is the actual array variable, then the depth of this type
717 * will be the same as the depth of the array and we will fill in
718 * the first array index.
719 * Otherwise, the depth of the base type will be smaller and we will fill
720 * in a later index.
722 __isl_give isl_map *PetScan::extract_access(ArraySubscriptExpr *expr)
724 Expr *base = expr->getBase();
725 Expr *idx = expr->getIdx();
726 isl_pw_aff *index;
727 isl_map *base_access;
728 isl_map *access;
729 int depth = array_depth(base->getType().getTypePtr());
730 int pos;
731 bool save_nesting = nesting_enabled;
733 nesting_enabled = allow_nested;
735 base_access = extract_access(base);
736 index = extract_affine(idx);
738 nesting_enabled = save_nesting;
740 pos = isl_map_dim(base_access, isl_dim_out) - depth;
741 access = set_index(base_access, pos, index);
743 return access;
746 /* Check if "expr" calls function "minmax" with two arguments and if so
747 * make lhs and rhs refer to these two arguments.
749 static bool is_minmax(Expr *expr, const char *minmax, Expr *&lhs, Expr *&rhs)
751 CallExpr *call;
752 FunctionDecl *fd;
753 string name;
755 if (expr->getStmtClass() != Stmt::CallExprClass)
756 return false;
758 call = cast<CallExpr>(expr);
759 fd = call->getDirectCallee();
760 if (!fd)
761 return false;
763 if (call->getNumArgs() != 2)
764 return false;
766 name = fd->getDeclName().getAsString();
767 if (name != minmax)
768 return false;
770 lhs = call->getArg(0);
771 rhs = call->getArg(1);
773 return true;
776 /* Check if "expr" is of the form min(lhs, rhs) and if so make
777 * lhs and rhs refer to the two arguments.
779 static bool is_min(Expr *expr, Expr *&lhs, Expr *&rhs)
781 return is_minmax(expr, "min", lhs, rhs);
784 /* Check if "expr" is of the form max(lhs, rhs) and if so make
785 * lhs and rhs refer to the two arguments.
787 static bool is_max(Expr *expr, Expr *&lhs, Expr *&rhs)
789 return is_minmax(expr, "max", lhs, rhs);
792 /* Extract a set of values satisfying the comparison "LHS op RHS"
793 * "comp" is the original statement that "LHS op RHS" is derived from
794 * and is used for diagnostics.
796 * If the comparison is of the form
798 * a <= min(b,c)
800 * then the set is constructed as the intersection of the set corresponding
801 * to the comparisons
803 * a <= b and a <= c
805 * A similar optimization is performed for max(a,b) <= c.
806 * We do this because that will lead to simpler representations of the set.
807 * If isl is ever enhanced to explicitly deal with min and max expressions,
808 * this optimization can be removed.
810 __isl_give isl_set *PetScan::extract_comparison(BinaryOperatorKind op,
811 Expr *LHS, Expr *RHS, Stmt *comp)
813 isl_pw_aff *lhs;
814 isl_pw_aff *rhs;
815 isl_set *cond;
817 if (op == BO_GT)
818 return extract_comparison(BO_LT, RHS, LHS, comp);
819 if (op == BO_GE)
820 return extract_comparison(BO_LE, RHS, LHS, comp);
822 if (op == BO_LT || op == BO_LE) {
823 Expr *expr1, *expr2;
824 isl_set *set1, *set2;
825 if (is_min(RHS, expr1, expr2)) {
826 set1 = extract_comparison(op, LHS, expr1, comp);
827 set2 = extract_comparison(op, LHS, expr2, comp);
828 return isl_set_intersect(set1, set2);
830 if (is_max(LHS, expr1, expr2)) {
831 set1 = extract_comparison(op, expr1, RHS, comp);
832 set2 = extract_comparison(op, expr2, RHS, comp);
833 return isl_set_intersect(set1, set2);
837 lhs = extract_affine(LHS);
838 rhs = extract_affine(RHS);
840 switch (op) {
841 case BO_LT:
842 cond = isl_pw_aff_lt_set(lhs, rhs);
843 break;
844 case BO_LE:
845 cond = isl_pw_aff_le_set(lhs, rhs);
846 break;
847 case BO_EQ:
848 cond = isl_pw_aff_eq_set(lhs, rhs);
849 break;
850 case BO_NE:
851 cond = isl_pw_aff_ne_set(lhs, rhs);
852 break;
853 default:
854 isl_pw_aff_free(lhs);
855 isl_pw_aff_free(rhs);
856 unsupported(comp);
857 return NULL;
860 cond = isl_set_coalesce(cond);
862 return cond;
865 __isl_give isl_set *PetScan::extract_comparison(BinaryOperator *comp)
867 return extract_comparison(comp->getOpcode(), comp->getLHS(),
868 comp->getRHS(), comp);
871 /* Extract a set of values satisfying the negation (logical not)
872 * of a subexpression.
874 __isl_give isl_set *PetScan::extract_boolean(UnaryOperator *op)
876 isl_set *cond;
878 cond = extract_condition(op->getSubExpr());
880 return isl_set_complement(cond);
883 /* Extract a set of values satisfying the union (logical or)
884 * or intersection (logical and) of two subexpressions.
886 __isl_give isl_set *PetScan::extract_boolean(BinaryOperator *comp)
888 isl_set *lhs;
889 isl_set *rhs;
890 isl_set *cond;
892 lhs = extract_condition(comp->getLHS());
893 rhs = extract_condition(comp->getRHS());
895 switch (comp->getOpcode()) {
896 case BO_LAnd:
897 cond = isl_set_intersect(lhs, rhs);
898 break;
899 case BO_LOr:
900 cond = isl_set_union(lhs, rhs);
901 break;
902 default:
903 isl_set_free(lhs);
904 isl_set_free(rhs);
905 unsupported(comp);
906 return NULL;
909 return cond;
912 __isl_give isl_set *PetScan::extract_condition(UnaryOperator *expr)
914 switch (expr->getOpcode()) {
915 case UO_LNot:
916 return extract_boolean(expr);
917 default:
918 unsupported(expr);
919 return NULL;
923 /* Extract a set of values satisfying the condition "expr != 0".
925 __isl_give isl_set *PetScan::extract_implicit_condition(Expr *expr)
927 return isl_pw_aff_non_zero_set(extract_affine(expr));
930 /* Extract a set of values satisfying the condition expressed by "expr".
932 * If the expression doesn't look like a condition, we assume it
933 * is an affine expression and return the condition "expr != 0".
935 __isl_give isl_set *PetScan::extract_condition(Expr *expr)
937 BinaryOperator *comp;
939 if (!expr)
940 return isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
942 if (expr->getStmtClass() == Stmt::ParenExprClass)
943 return extract_condition(cast<ParenExpr>(expr)->getSubExpr());
945 if (expr->getStmtClass() == Stmt::UnaryOperatorClass)
946 return extract_condition(cast<UnaryOperator>(expr));
948 if (expr->getStmtClass() != Stmt::BinaryOperatorClass)
949 return extract_implicit_condition(expr);
951 comp = cast<BinaryOperator>(expr);
952 switch (comp->getOpcode()) {
953 case BO_LT:
954 case BO_LE:
955 case BO_GT:
956 case BO_GE:
957 case BO_EQ:
958 case BO_NE:
959 return extract_comparison(comp);
960 case BO_LAnd:
961 case BO_LOr:
962 return extract_boolean(comp);
963 default:
964 return extract_implicit_condition(expr);
968 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
970 switch (kind) {
971 case UO_Minus:
972 return pet_op_minus;
973 default:
974 return pet_op_last;
978 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
980 switch (kind) {
981 case BO_AddAssign:
982 return pet_op_add_assign;
983 case BO_SubAssign:
984 return pet_op_sub_assign;
985 case BO_MulAssign:
986 return pet_op_mul_assign;
987 case BO_DivAssign:
988 return pet_op_div_assign;
989 case BO_Assign:
990 return pet_op_assign;
991 case BO_Add:
992 return pet_op_add;
993 case BO_Sub:
994 return pet_op_sub;
995 case BO_Mul:
996 return pet_op_mul;
997 case BO_Div:
998 return pet_op_div;
999 case BO_EQ:
1000 return pet_op_eq;
1001 case BO_LE:
1002 return pet_op_le;
1003 case BO_LT:
1004 return pet_op_lt;
1005 case BO_GT:
1006 return pet_op_gt;
1007 default:
1008 return pet_op_last;
1012 /* Construct a pet_expr representing a unary operator expression.
1014 struct pet_expr *PetScan::extract_expr(UnaryOperator *expr)
1016 struct pet_expr *arg;
1017 enum pet_op_type op;
1019 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
1020 if (op == pet_op_last) {
1021 unsupported(expr);
1022 return NULL;
1025 arg = extract_expr(expr->getSubExpr());
1027 return pet_expr_new_unary(ctx, op, arg);
1030 /* Mark the given access pet_expr as a write.
1031 * If a scalar is being accessed, then mark its value
1032 * as unknown in assigned_value.
1034 void PetScan::mark_write(struct pet_expr *access)
1036 isl_id *id;
1037 ValueDecl *decl;
1039 access->acc.write = 1;
1040 access->acc.read = 0;
1042 if (isl_map_dim(access->acc.access, isl_dim_out) != 0)
1043 return;
1045 id = isl_map_get_tuple_id(access->acc.access, isl_dim_out);
1046 decl = (ValueDecl *) isl_id_get_user(id);
1047 assigned_value[decl] = NULL;
1048 isl_id_free(id);
1051 /* Construct a pet_expr representing a binary operator expression.
1053 * If the top level operator is an assignment and the LHS is an access,
1054 * then we mark that access as a write. If the operator is a compound
1055 * assignment, the access is marked as both a read and a write.
1057 * If "expr" assigns something to a scalar variable, then we keep track
1058 * of the assigned expression in assigned_value so that we can plug
1059 * it in when we later come across the same variable.
1061 struct pet_expr *PetScan::extract_expr(BinaryOperator *expr)
1063 struct pet_expr *lhs, *rhs;
1064 enum pet_op_type op;
1066 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
1067 if (op == pet_op_last) {
1068 unsupported(expr);
1069 return NULL;
1072 lhs = extract_expr(expr->getLHS());
1073 rhs = extract_expr(expr->getRHS());
1075 if (expr->isAssignmentOp() && lhs && lhs->type == pet_expr_access) {
1076 mark_write(lhs);
1077 if (expr->isCompoundAssignmentOp())
1078 lhs->acc.read = 1;
1081 if (expr->getOpcode() == BO_Assign &&
1082 lhs && lhs->type == pet_expr_access &&
1083 isl_map_dim(lhs->acc.access, isl_dim_out) == 0) {
1084 isl_id *id = isl_map_get_tuple_id(lhs->acc.access, isl_dim_out);
1085 ValueDecl *decl = (ValueDecl *) isl_id_get_user(id);
1086 assigned_value[decl] = expr->getRHS();
1087 isl_id_free(id);
1090 return pet_expr_new_binary(ctx, op, lhs, rhs);
1093 /* Construct a pet_expr representing a conditional operation.
1095 struct pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
1097 struct pet_expr *cond, *lhs, *rhs;
1099 cond = extract_expr(expr->getCond());
1100 lhs = extract_expr(expr->getTrueExpr());
1101 rhs = extract_expr(expr->getFalseExpr());
1103 return pet_expr_new_ternary(ctx, cond, lhs, rhs);
1106 struct pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
1108 return extract_expr(expr->getSubExpr());
1111 /* Construct a pet_expr representing a floating point value.
1113 struct pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
1115 return pet_expr_new_double(ctx, expr->getValueAsApproximateDouble());
1118 /* Extract an access relation from "expr" and then convert it into
1119 * a pet_expr.
1121 struct pet_expr *PetScan::extract_access_expr(Expr *expr)
1123 isl_map *access;
1124 struct pet_expr *pe;
1126 switch (expr->getStmtClass()) {
1127 case Stmt::ArraySubscriptExprClass:
1128 access = extract_access(cast<ArraySubscriptExpr>(expr));
1129 break;
1130 case Stmt::DeclRefExprClass:
1131 access = extract_access(cast<DeclRefExpr>(expr));
1132 break;
1133 case Stmt::IntegerLiteralClass:
1134 access = extract_access(cast<IntegerLiteral>(expr));
1135 break;
1136 default:
1137 unsupported(expr);
1138 return NULL;
1141 pe = pet_expr_from_access(access);
1143 return pe;
1146 struct pet_expr *PetScan::extract_expr(ParenExpr *expr)
1148 return extract_expr(expr->getSubExpr());
1151 /* Construct a pet_expr representing a function call.
1153 * If we are passing along a pointer to an array element
1154 * or an entire row or even higher dimensional slice of an array,
1155 * then the function being called may write into the array.
1157 * We assume here that if the function is declared to take a pointer
1158 * to a const type, then the function will perform a read
1159 * and that otherwise, it will perform a write.
1161 struct pet_expr *PetScan::extract_expr(CallExpr *expr)
1163 struct pet_expr *res = NULL;
1164 FunctionDecl *fd;
1165 string name;
1167 fd = expr->getDirectCallee();
1168 if (!fd) {
1169 unsupported(expr);
1170 return NULL;
1173 name = fd->getDeclName().getAsString();
1174 res = pet_expr_new_call(ctx, name.c_str(), expr->getNumArgs());
1175 if (!res)
1176 return NULL;
1178 for (int i = 0; i < expr->getNumArgs(); ++i) {
1179 Expr *arg = expr->getArg(i);
1180 int is_addr = 0;
1182 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
1183 ImplicitCastExpr *ice = cast<ImplicitCastExpr>(arg);
1184 arg = ice->getSubExpr();
1186 if (arg->getStmtClass() == Stmt::UnaryOperatorClass) {
1187 UnaryOperator *op = cast<UnaryOperator>(arg);
1188 if (op->getOpcode() == UO_AddrOf) {
1189 is_addr = 1;
1190 arg = op->getSubExpr();
1193 res->args[i] = PetScan::extract_expr(arg);
1194 if (!res->args[i])
1195 goto error;
1196 if (arg->getStmtClass() == Stmt::ArraySubscriptExprClass &&
1197 array_depth(arg->getType().getTypePtr()) > 0)
1198 is_addr = 1;
1199 if (is_addr && res->args[i]->type == pet_expr_access) {
1200 ParmVarDecl *parm = fd->getParamDecl(i);
1201 if (!const_base(parm->getType()))
1202 mark_write(res->args[i]);
1206 return res;
1207 error:
1208 pet_expr_free(res);
1209 return NULL;
1212 /* Try and onstruct a pet_expr representing "expr".
1214 struct pet_expr *PetScan::extract_expr(Expr *expr)
1216 switch (expr->getStmtClass()) {
1217 case Stmt::UnaryOperatorClass:
1218 return extract_expr(cast<UnaryOperator>(expr));
1219 case Stmt::CompoundAssignOperatorClass:
1220 case Stmt::BinaryOperatorClass:
1221 return extract_expr(cast<BinaryOperator>(expr));
1222 case Stmt::ImplicitCastExprClass:
1223 return extract_expr(cast<ImplicitCastExpr>(expr));
1224 case Stmt::ArraySubscriptExprClass:
1225 case Stmt::DeclRefExprClass:
1226 case Stmt::IntegerLiteralClass:
1227 return extract_access_expr(expr);
1228 case Stmt::FloatingLiteralClass:
1229 return extract_expr(cast<FloatingLiteral>(expr));
1230 case Stmt::ParenExprClass:
1231 return extract_expr(cast<ParenExpr>(expr));
1232 case Stmt::ConditionalOperatorClass:
1233 return extract_expr(cast<ConditionalOperator>(expr));
1234 case Stmt::CallExprClass:
1235 return extract_expr(cast<CallExpr>(expr));
1236 default:
1237 unsupported(expr);
1239 return NULL;
1242 /* Check if the given initialization statement is an assignment.
1243 * If so, return that assignment. Otherwise return NULL.
1245 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1247 BinaryOperator *ass;
1249 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1250 return NULL;
1252 ass = cast<BinaryOperator>(init);
1253 if (ass->getOpcode() != BO_Assign)
1254 return NULL;
1256 return ass;
1259 /* Check if the given initialization statement is a declaration
1260 * of a single variable.
1261 * If so, return that declaration. Otherwise return NULL.
1263 Decl *PetScan::initialization_declaration(Stmt *init)
1265 DeclStmt *decl;
1267 if (init->getStmtClass() != Stmt::DeclStmtClass)
1268 return NULL;
1270 decl = cast<DeclStmt>(init);
1272 if (!decl->isSingleDecl())
1273 return NULL;
1275 return decl->getSingleDecl();
1278 /* Given the assignment operator in the initialization of a for loop,
1279 * extract the induction variable, i.e., the (integer)variable being
1280 * assigned.
1282 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1284 Expr *lhs;
1285 DeclRefExpr *ref;
1286 ValueDecl *decl;
1287 const Type *type;
1289 lhs = init->getLHS();
1290 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1291 unsupported(init);
1292 return NULL;
1295 ref = cast<DeclRefExpr>(lhs);
1296 decl = ref->getDecl();
1297 type = decl->getType().getTypePtr();
1299 if (!type->isIntegerType()) {
1300 unsupported(lhs);
1301 return NULL;
1304 return decl;
1307 /* Given the initialization statement of a for loop and the single
1308 * declaration in this initialization statement,
1309 * extract the induction variable, i.e., the (integer) variable being
1310 * declared.
1312 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1314 VarDecl *vd;
1316 vd = cast<VarDecl>(decl);
1318 const QualType type = vd->getType();
1319 if (!type->isIntegerType()) {
1320 unsupported(init);
1321 return NULL;
1324 if (!vd->getInit()) {
1325 unsupported(init);
1326 return NULL;
1329 return vd;
1332 /* Check that op is of the form iv++ or iv--.
1333 * "inc" is accordingly set to 1 or -1.
1335 bool PetScan::check_unary_increment(UnaryOperator *op, clang::ValueDecl *iv,
1336 isl_int &inc)
1338 Expr *sub;
1339 DeclRefExpr *ref;
1341 if (!op->isIncrementDecrementOp()) {
1342 unsupported(op);
1343 return false;
1346 if (op->isIncrementOp())
1347 isl_int_set_si(inc, 1);
1348 else
1349 isl_int_set_si(inc, -1);
1351 sub = op->getSubExpr();
1352 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1353 unsupported(op);
1354 return false;
1357 ref = cast<DeclRefExpr>(sub);
1358 if (ref->getDecl() != iv) {
1359 unsupported(op);
1360 return false;
1363 return true;
1366 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1367 * has a single constant expression on a universe domain, then
1368 * put this constant in *user.
1370 static int extract_cst(__isl_take isl_set *set, __isl_take isl_aff *aff,
1371 void *user)
1373 isl_int *inc = (isl_int *)user;
1374 int res = 0;
1376 if (!isl_set_plain_is_universe(set) || !isl_aff_is_cst(aff))
1377 res = -1;
1378 else
1379 isl_aff_get_constant(aff, inc);
1381 isl_set_free(set);
1382 isl_aff_free(aff);
1384 return res;
1387 /* Check if op is of the form
1389 * iv = iv + inc
1391 * with inc a constant and set "inc" accordingly.
1393 * We extract an affine expression from the RHS and the subtract iv.
1394 * The result should be a constant.
1396 bool PetScan::check_binary_increment(BinaryOperator *op, clang::ValueDecl *iv,
1397 isl_int &inc)
1399 Expr *lhs;
1400 DeclRefExpr *ref;
1401 isl_id *id;
1402 isl_space *dim;
1403 isl_aff *aff;
1404 isl_pw_aff *val;
1406 if (op->getOpcode() != BO_Assign) {
1407 unsupported(op);
1408 return false;
1411 lhs = op->getLHS();
1412 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1413 unsupported(op);
1414 return false;
1417 ref = cast<DeclRefExpr>(lhs);
1418 if (ref->getDecl() != iv) {
1419 unsupported(op);
1420 return false;
1423 val = extract_affine(op->getRHS());
1425 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1427 dim = isl_space_set_alloc(ctx, 1, 0);
1428 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1429 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1430 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1432 val = isl_pw_aff_sub(val, isl_pw_aff_from_aff(aff));
1434 if (isl_pw_aff_foreach_piece(val, &extract_cst, &inc) < 0) {
1435 isl_pw_aff_free(val);
1436 unsupported(op);
1437 return false;
1440 isl_pw_aff_free(val);
1442 return true;
1445 /* Check that op is of the form iv += cst or iv -= cst.
1446 * "inc" is set to cst or -cst accordingly.
1448 bool PetScan::check_compound_increment(CompoundAssignOperator *op,
1449 clang::ValueDecl *iv, isl_int &inc)
1451 Expr *lhs, *rhs;
1452 DeclRefExpr *ref;
1453 bool neg = false;
1455 BinaryOperatorKind opcode;
1457 opcode = op->getOpcode();
1458 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1459 unsupported(op);
1460 return false;
1462 if (opcode == BO_SubAssign)
1463 neg = true;
1465 lhs = op->getLHS();
1466 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1467 unsupported(op);
1468 return false;
1471 ref = cast<DeclRefExpr>(lhs);
1472 if (ref->getDecl() != iv) {
1473 unsupported(op);
1474 return false;
1477 rhs = op->getRHS();
1479 if (rhs->getStmtClass() == Stmt::UnaryOperatorClass) {
1480 UnaryOperator *op = cast<UnaryOperator>(rhs);
1481 if (op->getOpcode() != UO_Minus) {
1482 unsupported(op);
1483 return false;
1486 neg = !neg;
1488 rhs = op->getSubExpr();
1491 if (rhs->getStmtClass() != Stmt::IntegerLiteralClass) {
1492 unsupported(op);
1493 return false;
1496 extract_int(cast<IntegerLiteral>(rhs), &inc);
1497 if (neg)
1498 isl_int_neg(inc, inc);
1500 return true;
1503 /* Check that the increment of the given for loop increments
1504 * (or decrements) the induction variable "iv".
1505 * "up" is set to true if the induction variable is incremented.
1507 bool PetScan::check_increment(ForStmt *stmt, ValueDecl *iv, isl_int &v)
1509 Stmt *inc = stmt->getInc();
1511 if (!inc) {
1512 unsupported(stmt);
1513 return false;
1516 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1517 return check_unary_increment(cast<UnaryOperator>(inc), iv, v);
1518 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1519 return check_compound_increment(
1520 cast<CompoundAssignOperator>(inc), iv, v);
1521 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1522 return check_binary_increment(cast<BinaryOperator>(inc), iv, v);
1524 unsupported(inc);
1525 return false;
1528 /* Embed the given iteration domain in an extra outer loop
1529 * with induction variable "var".
1530 * If this variable appeared as a parameter in the constraints,
1531 * it is replaced by the new outermost dimension.
1533 static __isl_give isl_set *embed(__isl_take isl_set *set,
1534 __isl_take isl_id *var)
1536 int pos;
1538 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
1539 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
1540 if (pos >= 0) {
1541 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
1542 set = isl_set_project_out(set, isl_dim_param, pos, 1);
1545 isl_id_free(var);
1546 return set;
1549 /* Construct a pet_scop for an infinite loop around the given body.
1551 * We extract a pet_scop for the body and then embed it in a loop with
1552 * iteration domain
1554 * { [t] : t >= 0 }
1556 * and schedule
1558 * { [t] -> [t] }
1560 struct pet_scop *PetScan::extract_infinite_loop(Stmt *body)
1562 isl_id *id;
1563 isl_space *dim;
1564 isl_set *domain;
1565 isl_map *sched;
1566 struct pet_scop *scop;
1568 scop = extract(body);
1569 if (!scop)
1570 return NULL;
1572 id = isl_id_alloc(ctx, "t", NULL);
1573 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
1574 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1575 dim = isl_space_from_domain(isl_set_get_space(domain));
1576 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1577 sched = isl_map_universe(dim);
1578 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1579 scop = pet_scop_embed(scop, domain, sched, id);
1581 return scop;
1584 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1586 * for (;;)
1587 * body
1590 struct pet_scop *PetScan::extract_infinite_for(ForStmt *stmt)
1592 return extract_infinite_loop(stmt->getBody());
1595 /* Check if the while loop is of the form
1597 * while (1)
1598 * body
1600 * If so, construct a scop for an infinite loop around body.
1601 * Otherwise, fail.
1603 struct pet_scop *PetScan::extract(WhileStmt *stmt)
1605 Expr *cond;
1606 isl_set *set;
1607 int is_universe;
1609 cond = stmt->getCond();
1610 if (!cond) {
1611 unsupported(stmt);
1612 return NULL;
1615 set = extract_condition(cond);
1616 is_universe = isl_set_plain_is_universe(set);
1617 isl_set_free(set);
1619 if (!is_universe) {
1620 unsupported(stmt);
1621 return NULL;
1624 return extract_infinite_loop(stmt->getBody());
1627 /* Check whether "cond" expresses a simple loop bound
1628 * on the only set dimension.
1629 * In particular, if "up" is set then "cond" should contain only
1630 * upper bounds on the set dimension.
1631 * Otherwise, it should contain only lower bounds.
1633 static bool is_simple_bound(__isl_keep isl_set *cond, isl_int inc)
1635 if (isl_int_is_pos(inc))
1636 return !isl_set_dim_has_lower_bound(cond, isl_dim_set, 0);
1637 else
1638 return !isl_set_dim_has_upper_bound(cond, isl_dim_set, 0);
1641 /* Extend a condition on a given iteration of a loop to one that
1642 * imposes the same condition on all previous iterations.
1643 * "domain" expresses the lower [upper] bound on the iterations
1644 * when up is set [not set].
1646 * In particular, we construct the condition (when up is set)
1648 * forall i' : (domain(i') and i' <= i) => cond(i')
1650 * which is equivalent to
1652 * not exists i' : domain(i') and i' <= i and not cond(i')
1654 * We construct this set by negating cond, applying a map
1656 * { [i'] -> [i] : domain(i') and i' <= i }
1658 * and then negating the result again.
1660 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1661 __isl_take isl_set *domain, isl_int inc)
1663 isl_map *previous_to_this;
1665 if (isl_int_is_pos(inc))
1666 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
1667 else
1668 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
1670 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
1672 cond = isl_set_complement(cond);
1673 cond = isl_set_apply(cond, previous_to_this);
1674 cond = isl_set_complement(cond);
1676 return cond;
1679 /* Construct a domain of the form
1681 * [id] -> { [] : exists a: id = init + a * inc and a >= 0 }
1683 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
1684 __isl_take isl_pw_aff *init, isl_int inc)
1686 isl_aff *aff;
1687 isl_space *dim;
1688 isl_set *set;
1690 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
1691 dim = isl_pw_aff_get_domain_space(init);
1692 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1693 aff = isl_aff_add_coefficient(aff, isl_dim_in, 0, inc);
1694 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1696 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
1697 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1698 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1699 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1701 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1703 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
1705 return isl_set_project_out(set, isl_dim_set, 0, 1);
1708 static unsigned get_type_size(ValueDecl *decl)
1710 return decl->getASTContext().getIntWidth(decl->getType());
1713 /* Assuming "cond" represents a simple bound on a loop where the loop
1714 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1715 * is possible.
1717 * Under the given assumptions, wrapping is only possible if "cond" allows
1718 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1719 * increasing iterator and 0 in case of a decreasing iterator.
1721 static bool can_wrap(__isl_keep isl_set *cond, ValueDecl *iv, isl_int inc)
1723 bool cw;
1724 isl_int limit;
1725 isl_set *test;
1727 test = isl_set_copy(cond);
1729 isl_int_init(limit);
1730 if (isl_int_is_neg(inc))
1731 isl_int_set_si(limit, 0);
1732 else {
1733 isl_int_set_si(limit, 1);
1734 isl_int_mul_2exp(limit, limit, get_type_size(iv));
1735 isl_int_sub_ui(limit, limit, 1);
1738 test = isl_set_fix(cond, isl_dim_set, 0, limit);
1739 cw = !isl_set_is_empty(test);
1740 isl_set_free(test);
1742 isl_int_clear(limit);
1744 return cw;
1747 /* Given a one-dimensional space, construct the following mapping on this
1748 * space
1750 * { [v] -> [v mod 2^width] }
1752 * where width is the number of bits used to represent the values
1753 * of the unsigned variable "iv".
1755 static __isl_give isl_map *compute_wrapping(__isl_take isl_space *dim,
1756 ValueDecl *iv)
1758 isl_int mod;
1759 isl_aff *aff;
1760 isl_map *map;
1762 isl_int_init(mod);
1763 isl_int_set_si(mod, 1);
1764 isl_int_mul_2exp(mod, mod, get_type_size(iv));
1766 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1767 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
1768 aff = isl_aff_mod(aff, mod);
1770 isl_int_clear(mod);
1772 return isl_map_from_basic_map(isl_basic_map_from_aff(aff));
1773 map = isl_map_reverse(map);
1776 /* Construct a pet_scop for a for statement.
1777 * The for loop is required to be of the form
1779 * for (i = init; condition; ++i)
1781 * or
1783 * for (i = init; condition; --i)
1785 * The initialization of the for loop should either be an assignment
1786 * to an integer variable, or a declaration of such a variable with
1787 * initialization.
1789 * We extract a pet_scop for the body and then embed it in a loop with
1790 * iteration domain and schedule
1792 * { [i] : i >= init and condition' }
1793 * { [i] -> [i] }
1795 * or
1797 * { [i] : i <= init and condition' }
1798 * { [i] -> [-i] }
1800 * Where condition' is equal to condition if the latter is
1801 * a simple upper [lower] bound and a condition that is extended
1802 * to apply to all previous iterations otherwise.
1804 * If the stride of the loop is not 1, then "i >= init" is replaced by
1806 * (exists a: i = init + stride * a and a >= 0)
1808 * If the loop iterator i is unsigned, then wrapping may occur.
1809 * During the computation, we work with a virtual iterator that
1810 * does not wrap. However, the condition in the code applies
1811 * to the wrapped value, so we need to change condition(i)
1812 * into condition([i % 2^width]).
1813 * After computing the virtual domain and schedule, we apply
1814 * the function { [v] -> [v % 2^width] } to the domain and the domain
1815 * of the schedule. In order not to lose any information, we also
1816 * need to intersect the domain of the schedule with the virtual domain
1817 * first, since some iterations in the wrapped domain may be scheduled
1818 * several times, typically an infinite number of times.
1819 * Note that there is no need to perform this final wrapping
1820 * if the loop condition (after wrapping) is simple.
1822 * Wrapping on unsigned iterators can be avoided entirely if
1823 * loop condition is simple, the loop iterator is incremented
1824 * [decremented] by one and the last value before wrapping cannot
1825 * possibly satisfy the loop condition.
1827 * Before extracting a pet_scop from the body we remove all
1828 * assignments in assigned_value to variables that are assigned
1829 * somewhere in the body of the loop.
1831 struct pet_scop *PetScan::extract_for(ForStmt *stmt)
1833 BinaryOperator *ass;
1834 Decl *decl;
1835 Stmt *init;
1836 Expr *lhs, *rhs;
1837 ValueDecl *iv;
1838 isl_space *dim;
1839 isl_set *domain;
1840 isl_map *sched;
1841 isl_set *cond;
1842 isl_id *id;
1843 struct pet_scop *scop;
1844 assigned_value_cache cache(assigned_value);
1845 isl_int inc;
1846 bool is_one;
1847 bool is_unsigned;
1848 bool is_simple;
1849 isl_map *wrap = NULL;
1851 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc())
1852 return extract_infinite_for(stmt);
1854 init = stmt->getInit();
1855 if (!init) {
1856 unsupported(stmt);
1857 return NULL;
1859 if ((ass = initialization_assignment(init)) != NULL) {
1860 iv = extract_induction_variable(ass);
1861 if (!iv)
1862 return NULL;
1863 lhs = ass->getLHS();
1864 rhs = ass->getRHS();
1865 } else if ((decl = initialization_declaration(init)) != NULL) {
1866 VarDecl *var = extract_induction_variable(init, decl);
1867 if (!var)
1868 return NULL;
1869 iv = var;
1870 rhs = var->getInit();
1871 lhs = DeclRefExpr::Create(iv->getASTContext(),
1872 var->getQualifierLoc(), iv, var->getInnerLocStart(),
1873 var->getType(), VK_LValue);
1874 } else {
1875 unsupported(stmt->getInit());
1876 return NULL;
1879 isl_int_init(inc);
1880 if (!check_increment(stmt, iv, inc)) {
1881 isl_int_clear(inc);
1882 return NULL;
1885 is_unsigned = iv->getType()->isUnsignedIntegerType();
1887 assigned_value[iv] = NULL;
1888 clear_assignments clear(assigned_value);
1889 clear.TraverseStmt(stmt->getBody());
1891 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1893 is_one = isl_int_is_one(inc) || isl_int_is_negone(inc);
1894 if (is_one)
1895 domain = extract_comparison(isl_int_is_pos(inc) ? BO_GE : BO_LE,
1896 lhs, rhs, init);
1897 else {
1898 isl_pw_aff *lb = extract_affine(rhs);
1899 domain = strided_domain(isl_id_copy(id), lb, inc);
1902 cond = extract_condition(stmt->getCond());
1903 cond = embed(cond, isl_id_copy(id));
1904 domain = embed(domain, isl_id_copy(id));
1905 is_simple = is_simple_bound(cond, inc);
1906 if (is_unsigned &&
1907 (!is_simple || !is_one || can_wrap(cond, iv, inc))) {
1908 wrap = compute_wrapping(isl_set_get_space(cond), iv);
1909 cond = isl_set_apply(cond, isl_map_reverse(isl_map_copy(wrap)));
1910 is_simple = is_simple && is_simple_bound(cond, inc);
1912 if (!is_simple)
1913 cond = valid_for_each_iteration(cond,
1914 isl_set_copy(domain), inc);
1915 domain = isl_set_intersect(domain, cond);
1916 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1917 dim = isl_space_from_domain(isl_set_get_space(domain));
1918 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1919 sched = isl_map_universe(dim);
1920 if (isl_int_is_pos(inc))
1921 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1922 else
1923 sched = isl_map_oppose(sched, isl_dim_in, 0, isl_dim_out, 0);
1925 if (is_unsigned && !is_simple) {
1926 wrap = isl_map_set_dim_id(wrap,
1927 isl_dim_out, 0, isl_id_copy(id));
1928 sched = isl_map_intersect_domain(sched, isl_set_copy(domain));
1929 domain = isl_set_apply(domain, isl_map_copy(wrap));
1930 sched = isl_map_apply_domain(sched, wrap);
1931 } else
1932 isl_map_free(wrap);
1934 scop = extract(stmt->getBody());
1935 scop = pet_scop_embed(scop, domain, sched, id);
1937 isl_int_clear(inc);
1938 return scop;
1941 struct pet_scop *PetScan::extract(CompoundStmt *stmt)
1943 return extract(stmt->children());
1946 /* Look for parameters in any access relation in "expr" that
1947 * refer to non-affine constructs. In particular, these are
1948 * parameters with no name.
1950 * If there are any such parameters, then the domain of the access
1951 * relation, which is still [] at this point, is replaced by
1952 * [[] -> [t_1,...,t_n]], with n the number of these parameters
1953 * (after identifying identical non-affine constructs).
1954 * The parameters are then equated to the corresponding t dimensions
1955 * and subsequently projected out.
1956 * param2pos maps the position of the parameter to the position
1957 * of the corresponding t dimension.
1959 struct pet_expr *PetScan::resolve_nested(struct pet_expr *expr)
1961 int n;
1962 int nparam;
1963 int n_in;
1964 isl_space *dim;
1965 isl_map *map;
1966 std::map<int,int> param2pos;
1968 if (!expr)
1969 return expr;
1971 for (int i = 0; i < expr->n_arg; ++i) {
1972 expr->args[i] = resolve_nested(expr->args[i]);
1973 if (!expr->args[i]) {
1974 pet_expr_free(expr);
1975 return NULL;
1979 if (expr->type != pet_expr_access)
1980 return expr;
1982 nparam = isl_map_dim(expr->acc.access, isl_dim_param);
1983 n = 0;
1984 for (int i = 0; i < nparam; ++i) {
1985 isl_id *id = isl_map_get_dim_id(expr->acc.access,
1986 isl_dim_param, i);
1987 if (id && isl_id_get_user(id) && !isl_id_get_name(id))
1988 n++;
1989 isl_id_free(id);
1992 if (n == 0)
1993 return expr;
1995 expr->n_arg = n;
1996 expr->args = isl_calloc_array(ctx, struct pet_expr *, n);
1997 if (!expr->args)
1998 goto error;
2000 n_in = isl_map_dim(expr->acc.access, isl_dim_in);
2001 for (int i = 0, pos = 0; i < nparam; ++i) {
2002 int j;
2003 isl_id *id = isl_map_get_dim_id(expr->acc.access,
2004 isl_dim_param, i);
2005 Expr *nested;
2007 if (!(id && isl_id_get_user(id) && !isl_id_get_name(id))) {
2008 isl_id_free(id);
2009 continue;
2012 nested = (Expr *) isl_id_get_user(id);
2013 expr->args[pos] = extract_expr(nested);
2015 for (j = 0; j < pos; ++j)
2016 if (pet_expr_is_equal(expr->args[j], expr->args[pos]))
2017 break;
2019 if (j < pos) {
2020 pet_expr_free(expr->args[pos]);
2021 param2pos[i] = n_in + j;
2022 n--;
2023 } else
2024 param2pos[i] = n_in + pos++;
2026 isl_id_free(id);
2028 expr->n_arg = n;
2030 dim = isl_map_get_space(expr->acc.access);
2031 dim = isl_space_domain(dim);
2032 dim = isl_space_from_domain(dim);
2033 dim = isl_space_add_dims(dim, isl_dim_out, n);
2034 map = isl_map_universe(dim);
2035 map = isl_map_domain_map(map);
2036 map = isl_map_reverse(map);
2037 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
2039 for (int i = nparam - 1; i >= 0; --i) {
2040 isl_id *id = isl_map_get_dim_id(expr->acc.access,
2041 isl_dim_param, i);
2042 if (!(id && isl_id_get_user(id) && !isl_id_get_name(id))) {
2043 isl_id_free(id);
2044 continue;
2047 expr->acc.access = isl_map_equate(expr->acc.access,
2048 isl_dim_param, i, isl_dim_in,
2049 param2pos[i]);
2050 expr->acc.access = isl_map_project_out(expr->acc.access,
2051 isl_dim_param, i, 1);
2053 isl_id_free(id);
2056 return expr;
2057 error:
2058 pet_expr_free(expr);
2059 return NULL;
2062 /* Convert a top-level pet_expr to a pet_scop with one statement.
2063 * This mainly involves resolving nested expression parameters
2064 * and setting the name of the iteration space.
2066 struct pet_scop *PetScan::extract(Stmt *stmt, struct pet_expr *expr)
2068 struct pet_stmt *ps;
2069 SourceLocation loc = stmt->getLocStart();
2070 int line = PP.getSourceManager().getExpansionLineNumber(loc);
2072 expr = resolve_nested(expr);
2073 ps = pet_stmt_from_pet_expr(ctx, line, n_stmt++, expr);
2074 return pet_scop_from_pet_stmt(ctx, ps);
2077 /* Check whether "expr" is an affine expression.
2078 * We turn on autodetection so that we won't generate any warnings
2079 * and turn off nesting, so that we won't accept any non-affine constructs.
2081 bool PetScan::is_affine(Expr *expr)
2083 isl_pw_aff *pwaff;
2084 int save_autodetect = autodetect;
2085 bool save_nesting = nesting_enabled;
2087 autodetect = 1;
2088 nesting_enabled = false;
2090 pwaff = extract_affine(expr);
2091 isl_pw_aff_free(pwaff);
2093 autodetect = save_autodetect;
2094 nesting_enabled = save_nesting;
2096 return pwaff != NULL;
2099 /* Check whether "expr" is an affine constraint.
2100 * We turn on autodetection so that we won't generate any warnings
2101 * and turn off nesting, so that we won't accept any non-affine constructs.
2103 bool PetScan::is_affine_condition(Expr *expr)
2105 isl_set *set;
2106 int save_autodetect = autodetect;
2107 bool save_nesting = nesting_enabled;
2109 autodetect = 1;
2110 nesting_enabled = false;
2112 set = extract_condition(expr);
2113 isl_set_free(set);
2115 autodetect = save_autodetect;
2116 nesting_enabled = save_nesting;
2118 return set != NULL;
2121 /* If the top-level expression of "stmt" is an assignment, then
2122 * return that assignment as a BinaryOperator.
2123 * Otherwise return NULL.
2125 static BinaryOperator *top_assignment_or_null(Stmt *stmt)
2127 BinaryOperator *ass;
2129 if (!stmt)
2130 return NULL;
2131 if (stmt->getStmtClass() != Stmt::BinaryOperatorClass)
2132 return NULL;
2134 ass = cast<BinaryOperator>(stmt);
2135 if(ass->getOpcode() != BO_Assign)
2136 return NULL;
2138 return ass;
2141 /* Check if the given if statement is a conditional assignement
2142 * with a non-affine condition. If so, construct a pet_scop
2143 * corresponding to this conditional assignment. Otherwise return NULL.
2145 * In particular we check if "stmt" is of the form
2147 * if (condition)
2148 * a = f(...);
2149 * else
2150 * a = g(...);
2152 * where a is some array or scalar access.
2153 * The constructed pet_scop then corresponds to the expression
2155 * a = condition ? f(...) : g(...)
2157 * All access relations in f(...) are intersected with condition
2158 * while all access relation in g(...) are intersected with the complement.
2160 struct pet_scop *PetScan::extract_conditional_assignment(IfStmt *stmt)
2162 BinaryOperator *ass_then, *ass_else;
2163 isl_map *write_then, *write_else;
2164 isl_set *cond, *comp;
2165 isl_map *map, *map_true, *map_false;
2166 int equal;
2167 struct pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
2168 bool save_nesting = nesting_enabled;
2170 ass_then = top_assignment_or_null(stmt->getThen());
2171 ass_else = top_assignment_or_null(stmt->getElse());
2173 if (!ass_then || !ass_else)
2174 return NULL;
2176 if (is_affine_condition(stmt->getCond()))
2177 return NULL;
2179 write_then = extract_access(ass_then->getLHS());
2180 write_else = extract_access(ass_else->getLHS());
2182 equal = isl_map_is_equal(write_then, write_else);
2183 isl_map_free(write_else);
2184 if (equal < 0 || !equal) {
2185 isl_map_free(write_then);
2186 return NULL;
2189 nesting_enabled = allow_nested;
2190 cond = extract_condition(stmt->getCond());
2191 nesting_enabled = save_nesting;
2192 comp = isl_set_complement(isl_set_copy(cond));
2193 map_true = isl_map_from_domain(isl_set_copy(cond));
2194 map_true = isl_map_add_dims(map_true, isl_dim_out, 1);
2195 map_true = isl_map_fix_si(map_true, isl_dim_out, 0, 1);
2196 map_false = isl_map_from_domain(isl_set_copy(comp));
2197 map_false = isl_map_add_dims(map_false, isl_dim_out, 1);
2198 map_false = isl_map_fix_si(map_false, isl_dim_out, 0, 0);
2199 map = isl_map_union_disjoint(map_true, map_false);
2201 pe_cond = pet_expr_from_access(map);
2203 pe_then = extract_expr(ass_then->getRHS());
2204 pe_then = pet_expr_restrict(pe_then, cond);
2205 pe_else = extract_expr(ass_else->getRHS());
2206 pe_else = pet_expr_restrict(pe_else, comp);
2208 pe = pet_expr_new_ternary(ctx, pe_cond, pe_then, pe_else);
2209 pe_write = pet_expr_from_access(write_then);
2210 if (pe_write) {
2211 pe_write->acc.write = 1;
2212 pe_write->acc.read = 0;
2214 pe = pet_expr_new_binary(ctx, pet_op_assign, pe_write, pe);
2215 return extract(stmt, pe);
2218 /* Construct a pet_scop for an if statement.
2220 struct pet_scop *PetScan::extract(IfStmt *stmt)
2222 isl_set *cond;
2223 struct pet_scop *scop_then, *scop_else, *scop;
2224 assigned_value_cache cache(assigned_value);
2226 scop = extract_conditional_assignment(stmt);
2227 if (scop)
2228 return scop;
2230 scop_then = extract(stmt->getThen());
2232 if (stmt->getElse()) {
2233 scop_else = extract(stmt->getElse());
2234 if (autodetect) {
2235 if (scop_then && !scop_else) {
2236 partial = true;
2237 return scop_then;
2239 if (!scop_then && scop_else) {
2240 partial = true;
2241 return scop_else;
2246 cond = extract_condition(stmt->getCond());
2247 scop = pet_scop_restrict(scop_then, isl_set_copy(cond));
2249 if (stmt->getElse()) {
2250 cond = isl_set_complement(cond);
2251 scop_else = pet_scop_restrict(scop_else, cond);
2252 scop = pet_scop_add(ctx, scop, scop_else);
2253 } else
2254 isl_set_free(cond);
2256 return scop;
2259 /* Try and construct a pet_scop corresponding to "stmt".
2261 struct pet_scop *PetScan::extract(Stmt *stmt)
2263 if (isa<Expr>(stmt))
2264 return extract(stmt, extract_expr(cast<Expr>(stmt)));
2266 switch (stmt->getStmtClass()) {
2267 case Stmt::WhileStmtClass:
2268 return extract(cast<WhileStmt>(stmt));
2269 case Stmt::ForStmtClass:
2270 return extract_for(cast<ForStmt>(stmt));
2271 case Stmt::IfStmtClass:
2272 return extract(cast<IfStmt>(stmt));
2273 case Stmt::CompoundStmtClass:
2274 return extract(cast<CompoundStmt>(stmt));
2275 default:
2276 unsupported(stmt);
2279 return NULL;
2282 /* Try and construct a pet_scop corresponding to (part of)
2283 * a sequence of statements.
2285 struct pet_scop *PetScan::extract(StmtRange stmt_range)
2287 pet_scop *scop;
2288 StmtIterator i;
2289 int j;
2290 bool partial_range = false;
2292 scop = pet_scop_empty(ctx);
2293 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j) {
2294 Stmt *child = *i;
2295 struct pet_scop *scop_i;
2296 scop_i = extract(child);
2297 if (scop && partial) {
2298 pet_scop_free(scop_i);
2299 break;
2301 scop_i = pet_scop_prefix(scop_i, j);
2302 if (autodetect) {
2303 if (scop_i)
2304 scop = pet_scop_add(ctx, scop, scop_i);
2305 else
2306 partial_range = true;
2307 if (scop->n_stmt != 0 && !scop_i)
2308 partial = true;
2309 } else {
2310 scop = pet_scop_add(ctx, scop, scop_i);
2312 if (partial)
2313 break;
2316 if (scop && partial_range)
2317 partial = true;
2319 return scop;
2322 /* Check if the scop marked by the user is exactly this Stmt
2323 * or part of this Stmt.
2324 * If so, return a pet_scop corresponding to the marked region.
2325 * Otherwise, return NULL.
2327 struct pet_scop *PetScan::scan(Stmt *stmt)
2329 SourceManager &SM = PP.getSourceManager();
2330 unsigned start_off, end_off;
2332 start_off = SM.getFileOffset(stmt->getLocStart());
2333 end_off = SM.getFileOffset(stmt->getLocEnd());
2335 if (start_off > loc.end)
2336 return NULL;
2337 if (end_off < loc.start)
2338 return NULL;
2339 if (start_off >= loc.start && end_off <= loc.end) {
2340 return extract(stmt);
2343 StmtIterator start;
2344 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
2345 Stmt *child = *start;
2346 start_off = SM.getFileOffset(child->getLocStart());
2347 end_off = SM.getFileOffset(child->getLocEnd());
2348 if (start_off < loc.start && end_off > loc.end)
2349 return scan(child);
2350 if (start_off >= loc.start)
2351 break;
2354 StmtIterator end;
2355 for (end = start; end != stmt->child_end(); ++end) {
2356 Stmt *child = *end;
2357 start_off = SM.getFileOffset(child->getLocStart());
2358 if (start_off >= loc.end)
2359 break;
2362 return extract(StmtRange(start, end));
2365 /* Set the size of index "pos" of "array" to "size".
2366 * In particular, add a constraint of the form
2368 * i_pos < size
2370 * to array->extent and a constraint of the form
2372 * size >= 0
2374 * to array->context.
2376 static struct pet_array *update_size(struct pet_array *array, int pos,
2377 __isl_take isl_pw_aff *size)
2379 isl_set *valid;
2380 isl_set *univ;
2381 isl_set *bound;
2382 isl_space *dim;
2383 isl_aff *aff;
2384 isl_pw_aff *index;
2385 isl_id *id;
2387 valid = isl_pw_aff_nonneg_set(isl_pw_aff_copy(size));
2388 array->context = isl_set_intersect(array->context, valid);
2390 dim = isl_set_get_space(array->extent);
2391 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2392 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
2393 univ = isl_set_universe(isl_aff_get_domain_space(aff));
2394 index = isl_pw_aff_alloc(univ, aff);
2396 size = isl_pw_aff_add_dims(size, isl_dim_in,
2397 isl_set_dim(array->extent, isl_dim_set));
2398 id = isl_set_get_tuple_id(array->extent);
2399 size = isl_pw_aff_set_tuple_id(size, id);
2400 bound = isl_pw_aff_lt_set(index, size);
2402 array->extent = isl_set_intersect(array->extent, bound);
2404 if (!array->context || !array->extent)
2405 goto error;
2407 return array;
2408 error:
2409 pet_array_free(array);
2410 return NULL;
2413 /* Figure out the size of the array at position "pos" and all
2414 * subsequent positions from "type" and update "array" accordingly.
2416 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
2417 const Type *type, int pos)
2419 const ArrayType *atype;
2420 isl_pw_aff *size;
2422 if (!array)
2423 return NULL;
2425 if (type->isPointerType()) {
2426 type = type->getPointeeType().getTypePtr();
2427 return set_upper_bounds(array, type, pos + 1);
2429 if (!type->isArrayType())
2430 return array;
2432 type = type->getCanonicalTypeInternal().getTypePtr();
2433 atype = cast<ArrayType>(type);
2435 if (type->isConstantArrayType()) {
2436 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
2437 size = extract_affine(ca->getSize());
2438 array = update_size(array, pos, size);
2439 } else if (type->isVariableArrayType()) {
2440 const VariableArrayType *vla = cast<VariableArrayType>(atype);
2441 size = extract_affine(vla->getSizeExpr());
2442 array = update_size(array, pos, size);
2445 type = atype->getElementType().getTypePtr();
2447 return set_upper_bounds(array, type, pos + 1);
2450 /* Construct and return a pet_array corresponding to the variable "decl".
2451 * In particular, initialize array->extent to
2453 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2455 * and then call set_upper_bounds to set the upper bounds on the indices
2456 * based on the type of the variable.
2458 struct pet_array *PetScan::extract_array(isl_ctx *ctx, ValueDecl *decl)
2460 struct pet_array *array;
2461 QualType qt = decl->getType();
2462 const Type *type = qt.getTypePtr();
2463 int depth = array_depth(type);
2464 QualType base = base_type(qt);
2465 string name;
2466 isl_id *id;
2467 isl_space *dim;
2469 array = isl_calloc_type(ctx, struct pet_array);
2470 if (!array)
2471 return NULL;
2473 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
2474 dim = isl_space_set_alloc(ctx, 0, depth);
2475 dim = isl_space_set_tuple_id(dim, isl_dim_set, id);
2477 array->extent = isl_set_nat_universe(dim);
2479 dim = isl_space_params_alloc(ctx, 0);
2480 array->context = isl_set_universe(dim);
2482 array = set_upper_bounds(array, type, 0);
2483 if (!array)
2484 return NULL;
2486 name = base.getAsString();
2487 array->element_type = strdup(name.c_str());
2489 return array;
2492 /* Construct a list of pet_arrays, one for each array (or scalar)
2493 * accessed inside "scop" add this list to "scop" and return the result.
2495 * The context of "scop" is updated with the intesection of
2496 * the contexts of all arrays, i.e., constraints on the parameters
2497 * that ensure that the arrays have a valid (non-negative) size.
2499 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop)
2501 int i;
2502 set<ValueDecl *> arrays;
2503 set<ValueDecl *>::iterator it;
2505 if (!scop)
2506 return NULL;
2508 pet_scop_collect_arrays(scop, arrays);
2510 scop->n_array = arrays.size();
2511 if (scop->n_array == 0)
2512 return scop;
2514 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
2515 if (!scop->arrays)
2516 goto error;
2518 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
2519 struct pet_array *array;
2520 scop->arrays[i] = array = extract_array(ctx, *it);
2521 if (!scop->arrays[i])
2522 goto error;
2523 scop->context = isl_set_intersect(scop->context,
2524 isl_set_copy(array->context));
2525 if (!scop->context)
2526 goto error;
2529 return scop;
2530 error:
2531 pet_scop_free(scop);
2532 return NULL;
2535 /* Construct a pet_scop from the given function.
2537 struct pet_scop *PetScan::scan(FunctionDecl *fd)
2539 pet_scop *scop;
2540 Stmt *stmt;
2542 stmt = fd->getBody();
2544 if (autodetect)
2545 scop = extract(stmt);
2546 else
2547 scop = scan(stmt);
2548 scop = pet_scop_detect_parameter_accesses(scop);
2549 scop = scan_arrays(scop);
2551 return scop;