update isl for isl_pw_aff_has_dim_id
[pet.git] / scan.cc
blob8ea2579c99156ba7aebae9d5b51e7cf90bad3094
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;
56 /* Check if the element type corresponding to the given array type
57 * has a const qualifier.
59 static bool const_base(QualType qt)
61 const Type *type = qt.getTypePtr();
63 if (type->isPointerType())
64 return const_base(type->getPointeeType());
65 if (type->isArrayType()) {
66 const ArrayType *atype;
67 type = type->getCanonicalTypeInternal().getTypePtr();
68 atype = cast<ArrayType>(type);
69 return const_base(atype->getElementType());
72 return qt.isConstQualified();
75 /* Look for any assignments to scalar variables in part of the parse
76 * tree and set assigned_value to NULL for each of them.
77 * Also reset assigned_value if the address of a scalar variable
78 * is being taken. As an exception, if the address is passed to a function
79 * that is declared to receive a const pointer, then assigned_value is
80 * not reset.
82 * This ensures that we won't use any previously stored value
83 * in the current subtree and its parents.
85 struct clear_assignments : RecursiveASTVisitor<clear_assignments> {
86 map<ValueDecl *, Expr *> &assigned_value;
87 set<UnaryOperator *> skip;
89 clear_assignments(map<ValueDecl *, Expr *> &assigned_value) :
90 assigned_value(assigned_value) {}
92 /* Check for "address of" operators whose value is passed
93 * to a const pointer argument and add them to "skip", so that
94 * we can skip them in VisitUnaryOperator.
96 bool VisitCallExpr(CallExpr *expr) {
97 FunctionDecl *fd;
98 fd = expr->getDirectCallee();
99 if (!fd)
100 return true;
101 for (int i = 0; i < expr->getNumArgs(); ++i) {
102 Expr *arg = expr->getArg(i);
103 UnaryOperator *op;
104 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
105 ImplicitCastExpr *ice;
106 ice = cast<ImplicitCastExpr>(arg);
107 arg = ice->getSubExpr();
109 if (arg->getStmtClass() != Stmt::UnaryOperatorClass)
110 continue;
111 op = cast<UnaryOperator>(arg);
112 if (op->getOpcode() != UO_AddrOf)
113 continue;
114 if (const_base(fd->getParamDecl(i)->getType()))
115 skip.insert(op);
117 return true;
120 bool VisitUnaryOperator(UnaryOperator *expr) {
121 Expr *arg;
122 DeclRefExpr *ref;
123 ValueDecl *decl;
125 if (expr->getOpcode() != UO_AddrOf)
126 return true;
127 if (skip.find(expr) != skip.end())
128 return true;
130 arg = expr->getSubExpr();
131 if (arg->getStmtClass() != Stmt::DeclRefExprClass)
132 return true;
133 ref = cast<DeclRefExpr>(arg);
134 decl = ref->getDecl();
135 assigned_value[decl] = NULL;
136 return true;
139 bool VisitBinaryOperator(BinaryOperator *expr) {
140 Expr *lhs;
141 DeclRefExpr *ref;
142 ValueDecl *decl;
144 if (!expr->isAssignmentOp())
145 return true;
146 lhs = expr->getLHS();
147 if (lhs->getStmtClass() != Stmt::DeclRefExprClass)
148 return true;
149 ref = cast<DeclRefExpr>(lhs);
150 decl = ref->getDecl();
151 assigned_value[decl] = NULL;
152 return true;
156 /* Keep a copy of the currently assigned values.
158 * Any variable that is assigned a value inside the current scope
159 * is removed again when we leave the scope (either because it wasn't
160 * stored in the cache or because it has a different value in the cache).
162 struct assigned_value_cache {
163 map<ValueDecl *, Expr *> &assigned_value;
164 map<ValueDecl *, Expr *> cache;
166 assigned_value_cache(map<ValueDecl *, Expr *> &assigned_value) :
167 assigned_value(assigned_value), cache(assigned_value) {}
168 ~assigned_value_cache() {
169 map<ValueDecl *, Expr *>::iterator it = cache.begin();
170 for (it = assigned_value.begin(); it != assigned_value.end();
171 ++it) {
172 if (!it->second ||
173 (cache.find(it->first) != cache.end() &&
174 cache[it->first] != it->second))
175 cache[it->first] = NULL;
177 assigned_value = cache;
181 /* Called if we found something we (currently) cannot handle.
182 * We'll provide more informative warnings later.
184 * We only actually complain if autodetect is false.
186 void PetScan::unsupported(Stmt *stmt)
188 if (autodetect)
189 return;
191 SourceLocation loc = stmt->getLocStart();
192 DiagnosticsEngine &diag = PP.getDiagnostics();
193 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
194 "unsupported");
195 DiagnosticBuilder B = diag.Report(loc, id) << stmt->getSourceRange();
198 /* Extract an integer from "expr" and store it in "v".
200 int PetScan::extract_int(IntegerLiteral *expr, isl_int *v)
202 const Type *type = expr->getType().getTypePtr();
203 int is_signed = type->hasSignedIntegerRepresentation();
205 if (is_signed) {
206 int64_t i = expr->getValue().getSExtValue();
207 isl_int_set_si(*v, i);
208 } else {
209 uint64_t i = expr->getValue().getZExtValue();
210 isl_int_set_ui(*v, i);
213 return 0;
216 /* Extract an affine expression from the IntegerLiteral "expr".
218 __isl_give isl_pw_aff *PetScan::extract_affine(IntegerLiteral *expr)
220 isl_space *dim = isl_space_params_alloc(ctx, 0);
221 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
222 isl_aff *aff = isl_aff_zero_on_domain(ls);
223 isl_set *dom = isl_set_universe(dim);
224 isl_int v;
226 isl_int_init(v);
227 extract_int(expr, &v);
228 aff = isl_aff_add_constant(aff, v);
229 isl_int_clear(v);
231 return isl_pw_aff_alloc(dom, aff);
234 /* Extract an affine expression from the APInt "val".
236 __isl_give isl_pw_aff *PetScan::extract_affine(const llvm::APInt &val)
238 isl_space *dim = isl_space_params_alloc(ctx, 0);
239 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
240 isl_aff *aff = isl_aff_zero_on_domain(ls);
241 isl_set *dom = isl_set_universe(dim);
242 isl_int v;
244 isl_int_init(v);
245 isl_int_set_ui(v, val.getZExtValue());
246 aff = isl_aff_add_constant(aff, v);
247 isl_int_clear(v);
249 return isl_pw_aff_alloc(dom, aff);
252 __isl_give isl_pw_aff *PetScan::extract_affine(ImplicitCastExpr *expr)
254 return extract_affine(expr->getSubExpr());
257 /* Extract an affine expression from the DeclRefExpr "expr".
259 * If the variable has been assigned a value, then we check whether
260 * we know what expression was assigned and whether this expression
261 * is affine. If so, we convert the expression to an isl_pw_aff
262 * and to an extra parameter otherwise (provided nesting_enabled is set).
264 * Otherwise, we simply return an expression that is equal
265 * to a parameter corresponding to the referenced variable.
267 __isl_give isl_pw_aff *PetScan::extract_affine(DeclRefExpr *expr)
269 ValueDecl *decl = expr->getDecl();
270 const Type *type = decl->getType().getTypePtr();
271 isl_id *id;
272 isl_space *dim;
273 isl_aff *aff;
274 isl_set *dom;
276 if (!type->isIntegerType()) {
277 unsupported(expr);
278 return NULL;
281 if (assigned_value.find(decl) != assigned_value.end()) {
282 if (assigned_value[decl] && is_affine(assigned_value[decl]))
283 return extract_affine(assigned_value[decl]);
284 else
285 return nested_access(expr);
288 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
289 dim = isl_space_params_alloc(ctx, 1);
291 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
293 dom = isl_set_universe(isl_space_copy(dim));
294 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
295 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
297 return isl_pw_aff_alloc(dom, aff);
300 /* Extract an affine expression from an integer division operation.
301 * In particular, if "expr" is lhs/rhs, then return
303 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
305 * The second argument (rhs) is required to be a (positive) integer constant.
307 __isl_give isl_pw_aff *PetScan::extract_affine_div(BinaryOperator *expr)
309 Expr *rhs_expr;
310 isl_pw_aff *lhs, *lhs_f, *lhs_c;
311 isl_pw_aff *res;
312 isl_int v;
313 isl_set *cond;
315 rhs_expr = expr->getRHS();
316 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
317 unsupported(expr);
318 return NULL;
321 lhs = extract_affine(expr->getLHS());
322 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
324 isl_int_init(v);
325 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
326 lhs = isl_pw_aff_scale_down(lhs, v);
327 isl_int_clear(v);
329 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(lhs));
330 lhs_c = isl_pw_aff_ceil(lhs);
331 res = isl_pw_aff_cond(cond, lhs_f, lhs_c);
333 return res;
336 /* Extract an affine expression from a modulo operation.
337 * In particular, if "expr" is lhs/rhs, then return
339 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
341 * The second argument (rhs) is required to be a (positive) integer constant.
343 __isl_give isl_pw_aff *PetScan::extract_affine_mod(BinaryOperator *expr)
345 Expr *rhs_expr;
346 isl_pw_aff *lhs, *lhs_f, *lhs_c;
347 isl_pw_aff *res;
348 isl_int v;
349 isl_set *cond;
351 rhs_expr = expr->getRHS();
352 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
353 unsupported(expr);
354 return NULL;
357 lhs = extract_affine(expr->getLHS());
358 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
360 isl_int_init(v);
361 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
362 res = isl_pw_aff_scale_down(isl_pw_aff_copy(lhs), v);
364 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(res));
365 lhs_c = isl_pw_aff_ceil(res);
366 res = isl_pw_aff_cond(cond, lhs_f, lhs_c);
368 res = isl_pw_aff_scale(res, v);
369 isl_int_clear(v);
371 res = isl_pw_aff_sub(lhs, res);
373 return res;
376 /* Extract an affine expression from a multiplication operation.
377 * This is only allowed if at least one of the two arguments
378 * is a (piecewise) constant.
380 __isl_give isl_pw_aff *PetScan::extract_affine_mul(BinaryOperator *expr)
382 isl_pw_aff *lhs;
383 isl_pw_aff *rhs;
385 lhs = extract_affine(expr->getLHS());
386 rhs = extract_affine(expr->getRHS());
388 if (!isl_pw_aff_is_cst(lhs) && !isl_pw_aff_is_cst(rhs)) {
389 isl_pw_aff_free(lhs);
390 isl_pw_aff_free(rhs);
391 unsupported(expr);
392 return NULL;
395 return isl_pw_aff_mul(lhs, rhs);
398 /* Extract an affine expression from an addition or subtraction operation.
400 __isl_give isl_pw_aff *PetScan::extract_affine_add(BinaryOperator *expr)
402 isl_pw_aff *lhs;
403 isl_pw_aff *rhs;
405 lhs = extract_affine(expr->getLHS());
406 rhs = extract_affine(expr->getRHS());
408 switch (expr->getOpcode()) {
409 case BO_Add:
410 return isl_pw_aff_add(lhs, rhs);
411 case BO_Sub:
412 return isl_pw_aff_sub(lhs, rhs);
413 default:
414 isl_pw_aff_free(lhs);
415 isl_pw_aff_free(rhs);
416 return NULL;
421 /* Compute
423 * pwaff mod 2^width
425 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff,
426 unsigned width)
428 isl_int mod;
430 isl_int_init(mod);
431 isl_int_set_si(mod, 1);
432 isl_int_mul_2exp(mod, mod, width);
434 pwaff = isl_pw_aff_mod(pwaff, mod);
436 isl_int_clear(mod);
438 return pwaff;
441 /* Extract an affine expression from some binary operations.
442 * If the result of the expression is unsigned, then we wrap it
443 * based on the size of the type.
445 __isl_give isl_pw_aff *PetScan::extract_affine(BinaryOperator *expr)
447 isl_pw_aff *res;
449 switch (expr->getOpcode()) {
450 case BO_Add:
451 case BO_Sub:
452 res = extract_affine_add(expr);
453 break;
454 case BO_Div:
455 res = extract_affine_div(expr);
456 break;
457 case BO_Rem:
458 res = extract_affine_mod(expr);
459 break;
460 case BO_Mul:
461 res = extract_affine_mul(expr);
462 break;
463 default:
464 unsupported(expr);
465 return NULL;
468 if (expr->getType()->isUnsignedIntegerType())
469 res = wrap(res, ast_context.getIntWidth(expr->getType()));
471 return res;
474 /* Extract an affine expression from a negation operation.
476 __isl_give isl_pw_aff *PetScan::extract_affine(UnaryOperator *expr)
478 if (expr->getOpcode() == UO_Minus)
479 return isl_pw_aff_neg(extract_affine(expr->getSubExpr()));
481 unsupported(expr);
482 return NULL;
485 __isl_give isl_pw_aff *PetScan::extract_affine(ParenExpr *expr)
487 return extract_affine(expr->getSubExpr());
490 /* Extract an affine expression from some special function calls.
491 * In particular, we handle "min", "max", "ceild" and "floord".
492 * In case of the latter two, the second argument needs to be
493 * a (positive) integer constant.
495 __isl_give isl_pw_aff *PetScan::extract_affine(CallExpr *expr)
497 FunctionDecl *fd;
498 string name;
499 isl_pw_aff *aff1, *aff2;
501 fd = expr->getDirectCallee();
502 if (!fd) {
503 unsupported(expr);
504 return NULL;
507 name = fd->getDeclName().getAsString();
508 if (!(expr->getNumArgs() == 2 && name == "min") &&
509 !(expr->getNumArgs() == 2 && name == "max") &&
510 !(expr->getNumArgs() == 2 && name == "floord") &&
511 !(expr->getNumArgs() == 2 && name == "ceild")) {
512 unsupported(expr);
513 return NULL;
516 if (name == "min" || name == "max") {
517 aff1 = extract_affine(expr->getArg(0));
518 aff2 = extract_affine(expr->getArg(1));
520 if (name == "min")
521 aff1 = isl_pw_aff_min(aff1, aff2);
522 else
523 aff1 = isl_pw_aff_max(aff1, aff2);
524 } else if (name == "floord" || name == "ceild") {
525 isl_int v;
526 Expr *arg2 = expr->getArg(1);
528 if (arg2->getStmtClass() != Stmt::IntegerLiteralClass) {
529 unsupported(expr);
530 return NULL;
532 aff1 = extract_affine(expr->getArg(0));
533 isl_int_init(v);
534 extract_int(cast<IntegerLiteral>(arg2), &v);
535 aff1 = isl_pw_aff_scale_down(aff1, v);
536 isl_int_clear(v);
537 if (name == "floord")
538 aff1 = isl_pw_aff_floor(aff1);
539 else
540 aff1 = isl_pw_aff_ceil(aff1);
541 } else {
542 unsupported(expr);
543 return NULL;
546 return aff1;
550 /* This method is called when we come across an access that is
551 * nested in what is supposed to be an affine expression.
552 * If nesting is allowed, we return a new parameter that corresponds
553 * to this nested access. Otherwise, we simply complain.
555 * The new parameter is resolved in resolve_nested.
557 isl_pw_aff *PetScan::nested_access(Expr *expr)
559 isl_id *id;
560 isl_space *dim;
561 isl_aff *aff;
562 isl_set *dom;
564 if (!nesting_enabled) {
565 unsupported(expr);
566 return NULL;
569 id = isl_id_alloc(ctx, NULL, expr);
570 dim = isl_space_params_alloc(ctx, 1);
572 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
574 dom = isl_set_universe(isl_space_copy(dim));
575 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
576 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
578 return isl_pw_aff_alloc(dom, aff);
581 /* Affine expressions are not supposed to contain array accesses,
582 * but if nesting is allowed, we return a parameter corresponding
583 * to the array access.
585 __isl_give isl_pw_aff *PetScan::extract_affine(ArraySubscriptExpr *expr)
587 return nested_access(expr);
590 /* Extract an affine expression from a conditional operation.
592 __isl_give isl_pw_aff *PetScan::extract_affine(ConditionalOperator *expr)
594 isl_set *cond;
595 isl_pw_aff *lhs, *rhs;
597 cond = extract_condition(expr->getCond());
598 lhs = extract_affine(expr->getTrueExpr());
599 rhs = extract_affine(expr->getFalseExpr());
601 return isl_pw_aff_cond(cond, lhs, rhs);
604 /* Extract an affine expression, if possible, from "expr".
605 * Otherwise return NULL.
607 __isl_give isl_pw_aff *PetScan::extract_affine(Expr *expr)
609 switch (expr->getStmtClass()) {
610 case Stmt::ImplicitCastExprClass:
611 return extract_affine(cast<ImplicitCastExpr>(expr));
612 case Stmt::IntegerLiteralClass:
613 return extract_affine(cast<IntegerLiteral>(expr));
614 case Stmt::DeclRefExprClass:
615 return extract_affine(cast<DeclRefExpr>(expr));
616 case Stmt::BinaryOperatorClass:
617 return extract_affine(cast<BinaryOperator>(expr));
618 case Stmt::UnaryOperatorClass:
619 return extract_affine(cast<UnaryOperator>(expr));
620 case Stmt::ParenExprClass:
621 return extract_affine(cast<ParenExpr>(expr));
622 case Stmt::CallExprClass:
623 return extract_affine(cast<CallExpr>(expr));
624 case Stmt::ArraySubscriptExprClass:
625 return extract_affine(cast<ArraySubscriptExpr>(expr));
626 case Stmt::ConditionalOperatorClass:
627 return extract_affine(cast<ConditionalOperator>(expr));
628 default:
629 unsupported(expr);
631 return NULL;
634 __isl_give isl_map *PetScan::extract_access(ImplicitCastExpr *expr)
636 return extract_access(expr->getSubExpr());
639 /* Return the depth of an array of the given type.
641 static int array_depth(const Type *type)
643 if (type->isPointerType())
644 return 1 + array_depth(type->getPointeeType().getTypePtr());
645 if (type->isArrayType()) {
646 const ArrayType *atype;
647 type = type->getCanonicalTypeInternal().getTypePtr();
648 atype = cast<ArrayType>(type);
649 return 1 + array_depth(atype->getElementType().getTypePtr());
651 return 0;
654 /* Return the element type of the given array type.
656 static QualType base_type(QualType qt)
658 const Type *type = qt.getTypePtr();
660 if (type->isPointerType())
661 return base_type(type->getPointeeType());
662 if (type->isArrayType()) {
663 const ArrayType *atype;
664 type = type->getCanonicalTypeInternal().getTypePtr();
665 atype = cast<ArrayType>(type);
666 return base_type(atype->getElementType());
668 return qt;
671 /* Extract an access relation from a reference to a variable.
672 * If the variable has name "A" and its type corresponds to an
673 * array of depth d, then the returned access relation is of the
674 * form
676 * { [] -> A[i_1,...,i_d] }
678 __isl_give isl_map *PetScan::extract_access(DeclRefExpr *expr)
680 ValueDecl *decl = expr->getDecl();
681 int depth = array_depth(decl->getType().getTypePtr());
682 isl_id *id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
683 isl_space *dim = isl_space_alloc(ctx, 0, 0, depth);
684 isl_map *access_rel;
686 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
688 access_rel = isl_map_universe(dim);
690 return access_rel;
693 /* Extract an access relation from an integer contant.
694 * If the value of the constant is "v", then the returned access relation
695 * is
697 * { [] -> [v] }
699 __isl_give isl_map *PetScan::extract_access(IntegerLiteral *expr)
701 return isl_map_from_range(isl_set_from_pw_aff(extract_affine(expr)));
704 /* Try and extract an access relation from the given Expr.
705 * Return NULL if it doesn't work out.
707 __isl_give isl_map *PetScan::extract_access(Expr *expr)
709 switch (expr->getStmtClass()) {
710 case Stmt::ImplicitCastExprClass:
711 return extract_access(cast<ImplicitCastExpr>(expr));
712 case Stmt::DeclRefExprClass:
713 return extract_access(cast<DeclRefExpr>(expr));
714 case Stmt::ArraySubscriptExprClass:
715 return extract_access(cast<ArraySubscriptExpr>(expr));
716 default:
717 unsupported(expr);
719 return NULL;
722 /* Assign the affine expression "index" to the output dimension "pos" of "map"
723 * and return the result.
725 __isl_give isl_map *set_index(__isl_take isl_map *map, int pos,
726 __isl_take isl_pw_aff *index)
728 isl_map *index_map;
729 int len = isl_map_dim(map, isl_dim_out);
730 isl_id *id;
732 index_map = isl_map_from_range(isl_set_from_pw_aff(index));
733 index_map = isl_map_insert_dims(index_map, isl_dim_out, 0, pos);
734 index_map = isl_map_add_dims(index_map, isl_dim_out, len - pos - 1);
735 id = isl_map_get_tuple_id(map, isl_dim_out);
736 index_map = isl_map_set_tuple_id(index_map, isl_dim_out, id);
738 map = isl_map_intersect(map, index_map);
740 return map;
743 /* Extract an access relation from the given array subscript expression.
744 * If nesting is allowed in general, then we turn it on while
745 * examining the index expression.
747 * We first extract an access relation from the base.
748 * This will result in an access relation with a range that corresponds
749 * to the array being accessed and with earlier indices filled in already.
750 * We then extract the current index and fill that in as well.
751 * The position of the current index is based on the type of base.
752 * If base is the actual array variable, then the depth of this type
753 * will be the same as the depth of the array and we will fill in
754 * the first array index.
755 * Otherwise, the depth of the base type will be smaller and we will fill
756 * in a later index.
758 __isl_give isl_map *PetScan::extract_access(ArraySubscriptExpr *expr)
760 Expr *base = expr->getBase();
761 Expr *idx = expr->getIdx();
762 isl_pw_aff *index;
763 isl_map *base_access;
764 isl_map *access;
765 int depth = array_depth(base->getType().getTypePtr());
766 int pos;
767 bool save_nesting = nesting_enabled;
769 nesting_enabled = allow_nested;
771 base_access = extract_access(base);
772 index = extract_affine(idx);
774 nesting_enabled = save_nesting;
776 pos = isl_map_dim(base_access, isl_dim_out) - depth;
777 access = set_index(base_access, pos, index);
779 return access;
782 /* Check if "expr" calls function "minmax" with two arguments and if so
783 * make lhs and rhs refer to these two arguments.
785 static bool is_minmax(Expr *expr, const char *minmax, Expr *&lhs, Expr *&rhs)
787 CallExpr *call;
788 FunctionDecl *fd;
789 string name;
791 if (expr->getStmtClass() != Stmt::CallExprClass)
792 return false;
794 call = cast<CallExpr>(expr);
795 fd = call->getDirectCallee();
796 if (!fd)
797 return false;
799 if (call->getNumArgs() != 2)
800 return false;
802 name = fd->getDeclName().getAsString();
803 if (name != minmax)
804 return false;
806 lhs = call->getArg(0);
807 rhs = call->getArg(1);
809 return true;
812 /* Check if "expr" is of the form min(lhs, rhs) and if so make
813 * lhs and rhs refer to the two arguments.
815 static bool is_min(Expr *expr, Expr *&lhs, Expr *&rhs)
817 return is_minmax(expr, "min", lhs, rhs);
820 /* Check if "expr" is of the form max(lhs, rhs) and if so make
821 * lhs and rhs refer to the two arguments.
823 static bool is_max(Expr *expr, Expr *&lhs, Expr *&rhs)
825 return is_minmax(expr, "max", lhs, rhs);
828 /* Extract a set of values satisfying the comparison "LHS op RHS"
829 * "comp" is the original statement that "LHS op RHS" is derived from
830 * and is used for diagnostics.
832 * If the comparison is of the form
834 * a <= min(b,c)
836 * then the set is constructed as the intersection of the set corresponding
837 * to the comparisons
839 * a <= b and a <= c
841 * A similar optimization is performed for max(a,b) <= c.
842 * We do this because that will lead to simpler representations of the set.
843 * If isl is ever enhanced to explicitly deal with min and max expressions,
844 * this optimization can be removed.
846 __isl_give isl_set *PetScan::extract_comparison(BinaryOperatorKind op,
847 Expr *LHS, Expr *RHS, Stmt *comp)
849 isl_pw_aff *lhs;
850 isl_pw_aff *rhs;
851 isl_set *cond;
853 if (op == BO_GT)
854 return extract_comparison(BO_LT, RHS, LHS, comp);
855 if (op == BO_GE)
856 return extract_comparison(BO_LE, RHS, LHS, comp);
858 if (op == BO_LT || op == BO_LE) {
859 Expr *expr1, *expr2;
860 isl_set *set1, *set2;
861 if (is_min(RHS, expr1, expr2)) {
862 set1 = extract_comparison(op, LHS, expr1, comp);
863 set2 = extract_comparison(op, LHS, expr2, comp);
864 return isl_set_intersect(set1, set2);
866 if (is_max(LHS, expr1, expr2)) {
867 set1 = extract_comparison(op, expr1, RHS, comp);
868 set2 = extract_comparison(op, expr2, RHS, comp);
869 return isl_set_intersect(set1, set2);
873 lhs = extract_affine(LHS);
874 rhs = extract_affine(RHS);
876 switch (op) {
877 case BO_LT:
878 cond = isl_pw_aff_lt_set(lhs, rhs);
879 break;
880 case BO_LE:
881 cond = isl_pw_aff_le_set(lhs, rhs);
882 break;
883 case BO_EQ:
884 cond = isl_pw_aff_eq_set(lhs, rhs);
885 break;
886 case BO_NE:
887 cond = isl_pw_aff_ne_set(lhs, rhs);
888 break;
889 default:
890 isl_pw_aff_free(lhs);
891 isl_pw_aff_free(rhs);
892 unsupported(comp);
893 return NULL;
896 cond = isl_set_coalesce(cond);
898 return cond;
901 __isl_give isl_set *PetScan::extract_comparison(BinaryOperator *comp)
903 return extract_comparison(comp->getOpcode(), comp->getLHS(),
904 comp->getRHS(), comp);
907 /* Extract a set of values satisfying the negation (logical not)
908 * of a subexpression.
910 __isl_give isl_set *PetScan::extract_boolean(UnaryOperator *op)
912 isl_set *cond;
914 cond = extract_condition(op->getSubExpr());
916 return isl_set_complement(cond);
919 /* Extract a set of values satisfying the union (logical or)
920 * or intersection (logical and) of two subexpressions.
922 __isl_give isl_set *PetScan::extract_boolean(BinaryOperator *comp)
924 isl_set *lhs;
925 isl_set *rhs;
926 isl_set *cond;
928 lhs = extract_condition(comp->getLHS());
929 rhs = extract_condition(comp->getRHS());
931 switch (comp->getOpcode()) {
932 case BO_LAnd:
933 cond = isl_set_intersect(lhs, rhs);
934 break;
935 case BO_LOr:
936 cond = isl_set_union(lhs, rhs);
937 break;
938 default:
939 isl_set_free(lhs);
940 isl_set_free(rhs);
941 unsupported(comp);
942 return NULL;
945 return cond;
948 __isl_give isl_set *PetScan::extract_condition(UnaryOperator *expr)
950 switch (expr->getOpcode()) {
951 case UO_LNot:
952 return extract_boolean(expr);
953 default:
954 unsupported(expr);
955 return NULL;
959 /* Extract a set of values satisfying the condition "expr != 0".
961 __isl_give isl_set *PetScan::extract_implicit_condition(Expr *expr)
963 return isl_pw_aff_non_zero_set(extract_affine(expr));
966 /* Extract a set of values satisfying the condition expressed by "expr".
968 * If the expression doesn't look like a condition, we assume it
969 * is an affine expression and return the condition "expr != 0".
971 __isl_give isl_set *PetScan::extract_condition(Expr *expr)
973 BinaryOperator *comp;
975 if (!expr)
976 return isl_set_universe(isl_space_params_alloc(ctx, 0));
978 if (expr->getStmtClass() == Stmt::ParenExprClass)
979 return extract_condition(cast<ParenExpr>(expr)->getSubExpr());
981 if (expr->getStmtClass() == Stmt::UnaryOperatorClass)
982 return extract_condition(cast<UnaryOperator>(expr));
984 if (expr->getStmtClass() != Stmt::BinaryOperatorClass)
985 return extract_implicit_condition(expr);
987 comp = cast<BinaryOperator>(expr);
988 switch (comp->getOpcode()) {
989 case BO_LT:
990 case BO_LE:
991 case BO_GT:
992 case BO_GE:
993 case BO_EQ:
994 case BO_NE:
995 return extract_comparison(comp);
996 case BO_LAnd:
997 case BO_LOr:
998 return extract_boolean(comp);
999 default:
1000 return extract_implicit_condition(expr);
1004 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
1006 switch (kind) {
1007 case UO_Minus:
1008 return pet_op_minus;
1009 default:
1010 return pet_op_last;
1014 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
1016 switch (kind) {
1017 case BO_AddAssign:
1018 return pet_op_add_assign;
1019 case BO_SubAssign:
1020 return pet_op_sub_assign;
1021 case BO_MulAssign:
1022 return pet_op_mul_assign;
1023 case BO_DivAssign:
1024 return pet_op_div_assign;
1025 case BO_Assign:
1026 return pet_op_assign;
1027 case BO_Add:
1028 return pet_op_add;
1029 case BO_Sub:
1030 return pet_op_sub;
1031 case BO_Mul:
1032 return pet_op_mul;
1033 case BO_Div:
1034 return pet_op_div;
1035 case BO_EQ:
1036 return pet_op_eq;
1037 case BO_LE:
1038 return pet_op_le;
1039 case BO_LT:
1040 return pet_op_lt;
1041 case BO_GT:
1042 return pet_op_gt;
1043 default:
1044 return pet_op_last;
1048 /* Construct a pet_expr representing a unary operator expression.
1050 struct pet_expr *PetScan::extract_expr(UnaryOperator *expr)
1052 struct pet_expr *arg;
1053 enum pet_op_type op;
1055 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
1056 if (op == pet_op_last) {
1057 unsupported(expr);
1058 return NULL;
1061 arg = extract_expr(expr->getSubExpr());
1063 return pet_expr_new_unary(ctx, op, arg);
1066 /* Mark the given access pet_expr as a write.
1067 * If a scalar is being accessed, then mark its value
1068 * as unknown in assigned_value.
1070 void PetScan::mark_write(struct pet_expr *access)
1072 isl_id *id;
1073 ValueDecl *decl;
1075 access->acc.write = 1;
1076 access->acc.read = 0;
1078 if (isl_map_dim(access->acc.access, isl_dim_out) != 0)
1079 return;
1081 id = isl_map_get_tuple_id(access->acc.access, isl_dim_out);
1082 decl = (ValueDecl *) isl_id_get_user(id);
1083 assigned_value[decl] = NULL;
1084 isl_id_free(id);
1087 /* Construct a pet_expr representing a binary operator expression.
1089 * If the top level operator is an assignment and the LHS is an access,
1090 * then we mark that access as a write. If the operator is a compound
1091 * assignment, the access is marked as both a read and a write.
1093 * If "expr" assigns something to a scalar variable, then we keep track
1094 * of the assigned expression in assigned_value so that we can plug
1095 * it in when we later come across the same variable.
1097 struct pet_expr *PetScan::extract_expr(BinaryOperator *expr)
1099 struct pet_expr *lhs, *rhs;
1100 enum pet_op_type op;
1102 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
1103 if (op == pet_op_last) {
1104 unsupported(expr);
1105 return NULL;
1108 lhs = extract_expr(expr->getLHS());
1109 rhs = extract_expr(expr->getRHS());
1111 if (expr->isAssignmentOp() && lhs && lhs->type == pet_expr_access) {
1112 mark_write(lhs);
1113 if (expr->isCompoundAssignmentOp())
1114 lhs->acc.read = 1;
1117 if (expr->getOpcode() == BO_Assign &&
1118 lhs && lhs->type == pet_expr_access &&
1119 isl_map_dim(lhs->acc.access, isl_dim_out) == 0) {
1120 isl_id *id = isl_map_get_tuple_id(lhs->acc.access, isl_dim_out);
1121 ValueDecl *decl = (ValueDecl *) isl_id_get_user(id);
1122 assigned_value[decl] = expr->getRHS();
1123 isl_id_free(id);
1126 return pet_expr_new_binary(ctx, op, lhs, rhs);
1129 /* Construct a pet_expr representing a conditional operation.
1131 struct pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
1133 struct pet_expr *cond, *lhs, *rhs;
1135 cond = extract_expr(expr->getCond());
1136 lhs = extract_expr(expr->getTrueExpr());
1137 rhs = extract_expr(expr->getFalseExpr());
1139 return pet_expr_new_ternary(ctx, cond, lhs, rhs);
1142 struct pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
1144 return extract_expr(expr->getSubExpr());
1147 /* Construct a pet_expr representing a floating point value.
1149 struct pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
1151 return pet_expr_new_double(ctx, expr->getValueAsApproximateDouble());
1154 /* Extract an access relation from "expr" and then convert it into
1155 * a pet_expr.
1157 struct pet_expr *PetScan::extract_access_expr(Expr *expr)
1159 isl_map *access;
1160 struct pet_expr *pe;
1162 switch (expr->getStmtClass()) {
1163 case Stmt::ArraySubscriptExprClass:
1164 access = extract_access(cast<ArraySubscriptExpr>(expr));
1165 break;
1166 case Stmt::DeclRefExprClass:
1167 access = extract_access(cast<DeclRefExpr>(expr));
1168 break;
1169 case Stmt::IntegerLiteralClass:
1170 access = extract_access(cast<IntegerLiteral>(expr));
1171 break;
1172 default:
1173 unsupported(expr);
1174 return NULL;
1177 pe = pet_expr_from_access(access);
1179 return pe;
1182 struct pet_expr *PetScan::extract_expr(ParenExpr *expr)
1184 return extract_expr(expr->getSubExpr());
1187 /* Construct a pet_expr representing a function call.
1189 * If we are passing along a pointer to an array element
1190 * or an entire row or even higher dimensional slice of an array,
1191 * then the function being called may write into the array.
1193 * We assume here that if the function is declared to take a pointer
1194 * to a const type, then the function will perform a read
1195 * and that otherwise, it will perform a write.
1197 struct pet_expr *PetScan::extract_expr(CallExpr *expr)
1199 struct pet_expr *res = NULL;
1200 FunctionDecl *fd;
1201 string name;
1203 fd = expr->getDirectCallee();
1204 if (!fd) {
1205 unsupported(expr);
1206 return NULL;
1209 name = fd->getDeclName().getAsString();
1210 res = pet_expr_new_call(ctx, name.c_str(), expr->getNumArgs());
1211 if (!res)
1212 return NULL;
1214 for (int i = 0; i < expr->getNumArgs(); ++i) {
1215 Expr *arg = expr->getArg(i);
1216 int is_addr = 0;
1218 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
1219 ImplicitCastExpr *ice = cast<ImplicitCastExpr>(arg);
1220 arg = ice->getSubExpr();
1222 if (arg->getStmtClass() == Stmt::UnaryOperatorClass) {
1223 UnaryOperator *op = cast<UnaryOperator>(arg);
1224 if (op->getOpcode() == UO_AddrOf) {
1225 is_addr = 1;
1226 arg = op->getSubExpr();
1229 res->args[i] = PetScan::extract_expr(arg);
1230 if (!res->args[i])
1231 goto error;
1232 if (arg->getStmtClass() == Stmt::ArraySubscriptExprClass &&
1233 array_depth(arg->getType().getTypePtr()) > 0)
1234 is_addr = 1;
1235 if (is_addr && res->args[i]->type == pet_expr_access) {
1236 ParmVarDecl *parm = fd->getParamDecl(i);
1237 if (!const_base(parm->getType()))
1238 mark_write(res->args[i]);
1242 return res;
1243 error:
1244 pet_expr_free(res);
1245 return NULL;
1248 /* Try and onstruct a pet_expr representing "expr".
1250 struct pet_expr *PetScan::extract_expr(Expr *expr)
1252 switch (expr->getStmtClass()) {
1253 case Stmt::UnaryOperatorClass:
1254 return extract_expr(cast<UnaryOperator>(expr));
1255 case Stmt::CompoundAssignOperatorClass:
1256 case Stmt::BinaryOperatorClass:
1257 return extract_expr(cast<BinaryOperator>(expr));
1258 case Stmt::ImplicitCastExprClass:
1259 return extract_expr(cast<ImplicitCastExpr>(expr));
1260 case Stmt::ArraySubscriptExprClass:
1261 case Stmt::DeclRefExprClass:
1262 case Stmt::IntegerLiteralClass:
1263 return extract_access_expr(expr);
1264 case Stmt::FloatingLiteralClass:
1265 return extract_expr(cast<FloatingLiteral>(expr));
1266 case Stmt::ParenExprClass:
1267 return extract_expr(cast<ParenExpr>(expr));
1268 case Stmt::ConditionalOperatorClass:
1269 return extract_expr(cast<ConditionalOperator>(expr));
1270 case Stmt::CallExprClass:
1271 return extract_expr(cast<CallExpr>(expr));
1272 default:
1273 unsupported(expr);
1275 return NULL;
1278 /* Check if the given initialization statement is an assignment.
1279 * If so, return that assignment. Otherwise return NULL.
1281 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1283 BinaryOperator *ass;
1285 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1286 return NULL;
1288 ass = cast<BinaryOperator>(init);
1289 if (ass->getOpcode() != BO_Assign)
1290 return NULL;
1292 return ass;
1295 /* Check if the given initialization statement is a declaration
1296 * of a single variable.
1297 * If so, return that declaration. Otherwise return NULL.
1299 Decl *PetScan::initialization_declaration(Stmt *init)
1301 DeclStmt *decl;
1303 if (init->getStmtClass() != Stmt::DeclStmtClass)
1304 return NULL;
1306 decl = cast<DeclStmt>(init);
1308 if (!decl->isSingleDecl())
1309 return NULL;
1311 return decl->getSingleDecl();
1314 /* Given the assignment operator in the initialization of a for loop,
1315 * extract the induction variable, i.e., the (integer)variable being
1316 * assigned.
1318 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1320 Expr *lhs;
1321 DeclRefExpr *ref;
1322 ValueDecl *decl;
1323 const Type *type;
1325 lhs = init->getLHS();
1326 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1327 unsupported(init);
1328 return NULL;
1331 ref = cast<DeclRefExpr>(lhs);
1332 decl = ref->getDecl();
1333 type = decl->getType().getTypePtr();
1335 if (!type->isIntegerType()) {
1336 unsupported(lhs);
1337 return NULL;
1340 return decl;
1343 /* Given the initialization statement of a for loop and the single
1344 * declaration in this initialization statement,
1345 * extract the induction variable, i.e., the (integer) variable being
1346 * declared.
1348 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1350 VarDecl *vd;
1352 vd = cast<VarDecl>(decl);
1354 const QualType type = vd->getType();
1355 if (!type->isIntegerType()) {
1356 unsupported(init);
1357 return NULL;
1360 if (!vd->getInit()) {
1361 unsupported(init);
1362 return NULL;
1365 return vd;
1368 /* Check that op is of the form iv++ or iv--.
1369 * "inc" is accordingly set to 1 or -1.
1371 bool PetScan::check_unary_increment(UnaryOperator *op, clang::ValueDecl *iv,
1372 isl_int &inc)
1374 Expr *sub;
1375 DeclRefExpr *ref;
1377 if (!op->isIncrementDecrementOp()) {
1378 unsupported(op);
1379 return false;
1382 if (op->isIncrementOp())
1383 isl_int_set_si(inc, 1);
1384 else
1385 isl_int_set_si(inc, -1);
1387 sub = op->getSubExpr();
1388 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1389 unsupported(op);
1390 return false;
1393 ref = cast<DeclRefExpr>(sub);
1394 if (ref->getDecl() != iv) {
1395 unsupported(op);
1396 return false;
1399 return true;
1402 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1403 * has a single constant expression on a universe domain, then
1404 * put this constant in *user.
1406 static int extract_cst(__isl_take isl_set *set, __isl_take isl_aff *aff,
1407 void *user)
1409 isl_int *inc = (isl_int *)user;
1410 int res = 0;
1412 if (!isl_set_plain_is_universe(set) || !isl_aff_is_cst(aff))
1413 res = -1;
1414 else
1415 isl_aff_get_constant(aff, inc);
1417 isl_set_free(set);
1418 isl_aff_free(aff);
1420 return res;
1423 /* Check if op is of the form
1425 * iv = iv + inc
1427 * with inc a constant and set "inc" accordingly.
1429 * We extract an affine expression from the RHS and the subtract iv.
1430 * The result should be a constant.
1432 bool PetScan::check_binary_increment(BinaryOperator *op, clang::ValueDecl *iv,
1433 isl_int &inc)
1435 Expr *lhs;
1436 DeclRefExpr *ref;
1437 isl_id *id;
1438 isl_space *dim;
1439 isl_aff *aff;
1440 isl_pw_aff *val;
1442 if (op->getOpcode() != BO_Assign) {
1443 unsupported(op);
1444 return false;
1447 lhs = op->getLHS();
1448 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1449 unsupported(op);
1450 return false;
1453 ref = cast<DeclRefExpr>(lhs);
1454 if (ref->getDecl() != iv) {
1455 unsupported(op);
1456 return false;
1459 val = extract_affine(op->getRHS());
1461 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1463 dim = isl_space_params_alloc(ctx, 1);
1464 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1465 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1466 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1468 val = isl_pw_aff_sub(val, isl_pw_aff_from_aff(aff));
1470 if (isl_pw_aff_foreach_piece(val, &extract_cst, &inc) < 0) {
1471 isl_pw_aff_free(val);
1472 unsupported(op);
1473 return false;
1476 isl_pw_aff_free(val);
1478 return true;
1481 /* Check that op is of the form iv += cst or iv -= cst.
1482 * "inc" is set to cst or -cst accordingly.
1484 bool PetScan::check_compound_increment(CompoundAssignOperator *op,
1485 clang::ValueDecl *iv, isl_int &inc)
1487 Expr *lhs, *rhs;
1488 DeclRefExpr *ref;
1489 bool neg = false;
1491 BinaryOperatorKind opcode;
1493 opcode = op->getOpcode();
1494 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1495 unsupported(op);
1496 return false;
1498 if (opcode == BO_SubAssign)
1499 neg = true;
1501 lhs = op->getLHS();
1502 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1503 unsupported(op);
1504 return false;
1507 ref = cast<DeclRefExpr>(lhs);
1508 if (ref->getDecl() != iv) {
1509 unsupported(op);
1510 return false;
1513 rhs = op->getRHS();
1515 if (rhs->getStmtClass() == Stmt::UnaryOperatorClass) {
1516 UnaryOperator *op = cast<UnaryOperator>(rhs);
1517 if (op->getOpcode() != UO_Minus) {
1518 unsupported(op);
1519 return false;
1522 neg = !neg;
1524 rhs = op->getSubExpr();
1527 if (rhs->getStmtClass() != Stmt::IntegerLiteralClass) {
1528 unsupported(op);
1529 return false;
1532 extract_int(cast<IntegerLiteral>(rhs), &inc);
1533 if (neg)
1534 isl_int_neg(inc, inc);
1536 return true;
1539 /* Check that the increment of the given for loop increments
1540 * (or decrements) the induction variable "iv".
1541 * "up" is set to true if the induction variable is incremented.
1543 bool PetScan::check_increment(ForStmt *stmt, ValueDecl *iv, isl_int &v)
1545 Stmt *inc = stmt->getInc();
1547 if (!inc) {
1548 unsupported(stmt);
1549 return false;
1552 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1553 return check_unary_increment(cast<UnaryOperator>(inc), iv, v);
1554 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1555 return check_compound_increment(
1556 cast<CompoundAssignOperator>(inc), iv, v);
1557 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1558 return check_binary_increment(cast<BinaryOperator>(inc), iv, v);
1560 unsupported(inc);
1561 return false;
1564 /* Embed the given iteration domain in an extra outer loop
1565 * with induction variable "var".
1566 * If this variable appeared as a parameter in the constraints,
1567 * it is replaced by the new outermost dimension.
1569 static __isl_give isl_set *embed(__isl_take isl_set *set,
1570 __isl_take isl_id *var)
1572 int pos;
1574 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
1575 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
1576 if (pos >= 0) {
1577 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
1578 set = isl_set_project_out(set, isl_dim_param, pos, 1);
1581 isl_id_free(var);
1582 return set;
1585 /* Construct a pet_scop for an infinite loop around the given body.
1587 * We extract a pet_scop for the body and then embed it in a loop with
1588 * iteration domain
1590 * { [t] : t >= 0 }
1592 * and schedule
1594 * { [t] -> [t] }
1596 struct pet_scop *PetScan::extract_infinite_loop(Stmt *body)
1598 isl_id *id;
1599 isl_space *dim;
1600 isl_set *domain;
1601 isl_map *sched;
1602 struct pet_scop *scop;
1604 scop = extract(body);
1605 if (!scop)
1606 return NULL;
1608 id = isl_id_alloc(ctx, "t", NULL);
1609 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
1610 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1611 dim = isl_space_from_domain(isl_set_get_space(domain));
1612 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1613 sched = isl_map_universe(dim);
1614 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1615 scop = pet_scop_embed(scop, domain, sched, id);
1617 return scop;
1620 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1622 * for (;;)
1623 * body
1626 struct pet_scop *PetScan::extract_infinite_for(ForStmt *stmt)
1628 return extract_infinite_loop(stmt->getBody());
1631 /* Check if the while loop is of the form
1633 * while (1)
1634 * body
1636 * If so, construct a scop for an infinite loop around body.
1637 * Otherwise, fail.
1639 struct pet_scop *PetScan::extract(WhileStmt *stmt)
1641 Expr *cond;
1642 isl_set *set;
1643 int is_universe;
1645 cond = stmt->getCond();
1646 if (!cond) {
1647 unsupported(stmt);
1648 return NULL;
1651 set = extract_condition(cond);
1652 is_universe = isl_set_plain_is_universe(set);
1653 isl_set_free(set);
1655 if (!is_universe) {
1656 unsupported(stmt);
1657 return NULL;
1660 return extract_infinite_loop(stmt->getBody());
1663 /* Check whether "cond" expresses a simple loop bound
1664 * on the only set dimension.
1665 * In particular, if "up" is set then "cond" should contain only
1666 * upper bounds on the set dimension.
1667 * Otherwise, it should contain only lower bounds.
1669 static bool is_simple_bound(__isl_keep isl_set *cond, isl_int inc)
1671 if (isl_int_is_pos(inc))
1672 return !isl_set_dim_has_lower_bound(cond, isl_dim_set, 0);
1673 else
1674 return !isl_set_dim_has_upper_bound(cond, isl_dim_set, 0);
1677 /* Extend a condition on a given iteration of a loop to one that
1678 * imposes the same condition on all previous iterations.
1679 * "domain" expresses the lower [upper] bound on the iterations
1680 * when inc is positive [negative].
1682 * In particular, we construct the condition (when inc is positive)
1684 * forall i' : (domain(i') and i' <= i) => cond(i')
1686 * which is equivalent to
1688 * not exists i' : domain(i') and i' <= i and not cond(i')
1690 * We construct this set by negating cond, applying a map
1692 * { [i'] -> [i] : domain(i') and i' <= i }
1694 * and then negating the result again.
1696 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1697 __isl_take isl_set *domain, isl_int inc)
1699 isl_map *previous_to_this;
1701 if (isl_int_is_pos(inc))
1702 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
1703 else
1704 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
1706 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
1708 cond = isl_set_complement(cond);
1709 cond = isl_set_apply(cond, previous_to_this);
1710 cond = isl_set_complement(cond);
1712 return cond;
1715 /* Construct a domain of the form
1717 * [id] -> { [] : exists a: id = init + a * inc and a >= 0 }
1719 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
1720 __isl_take isl_pw_aff *init, isl_int inc)
1722 isl_aff *aff;
1723 isl_space *dim;
1724 isl_set *set;
1726 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
1727 dim = isl_pw_aff_get_domain_space(init);
1728 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1729 aff = isl_aff_add_coefficient(aff, isl_dim_in, 0, inc);
1730 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1732 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
1733 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1734 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1735 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1737 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1739 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
1741 return isl_set_project_out(set, isl_dim_set, 0, 1);
1744 static unsigned get_type_size(ValueDecl *decl)
1746 return decl->getASTContext().getIntWidth(decl->getType());
1749 /* Assuming "cond" represents a simple bound on a loop where the loop
1750 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1751 * is possible.
1753 * Under the given assumptions, wrapping is only possible if "cond" allows
1754 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1755 * increasing iterator and 0 in case of a decreasing iterator.
1757 static bool can_wrap(__isl_keep isl_set *cond, ValueDecl *iv, isl_int inc)
1759 bool cw;
1760 isl_int limit;
1761 isl_set *test;
1763 test = isl_set_copy(cond);
1765 isl_int_init(limit);
1766 if (isl_int_is_neg(inc))
1767 isl_int_set_si(limit, 0);
1768 else {
1769 isl_int_set_si(limit, 1);
1770 isl_int_mul_2exp(limit, limit, get_type_size(iv));
1771 isl_int_sub_ui(limit, limit, 1);
1774 test = isl_set_fix(cond, isl_dim_set, 0, limit);
1775 cw = !isl_set_is_empty(test);
1776 isl_set_free(test);
1778 isl_int_clear(limit);
1780 return cw;
1783 /* Given a one-dimensional space, construct the following mapping on this
1784 * space
1786 * { [v] -> [v mod 2^width] }
1788 * where width is the number of bits used to represent the values
1789 * of the unsigned variable "iv".
1791 static __isl_give isl_map *compute_wrapping(__isl_take isl_space *dim,
1792 ValueDecl *iv)
1794 isl_int mod;
1795 isl_aff *aff;
1796 isl_map *map;
1798 isl_int_init(mod);
1799 isl_int_set_si(mod, 1);
1800 isl_int_mul_2exp(mod, mod, get_type_size(iv));
1802 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1803 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
1804 aff = isl_aff_mod(aff, mod);
1806 isl_int_clear(mod);
1808 return isl_map_from_basic_map(isl_basic_map_from_aff(aff));
1809 map = isl_map_reverse(map);
1812 /* Construct a pet_scop for a for statement.
1813 * The for loop is required to be of the form
1815 * for (i = init; condition; ++i)
1817 * or
1819 * for (i = init; condition; --i)
1821 * The initialization of the for loop should either be an assignment
1822 * to an integer variable, or a declaration of such a variable with
1823 * initialization.
1825 * The condition is allowed to contain nested accesses, provided
1826 * they are not being written to inside the body of the loop.
1828 * We extract a pet_scop for the body and then embed it in a loop with
1829 * iteration domain and schedule
1831 * { [i] : i >= init and condition' }
1832 * { [i] -> [i] }
1834 * or
1836 * { [i] : i <= init and condition' }
1837 * { [i] -> [-i] }
1839 * Where condition' is equal to condition if the latter is
1840 * a simple upper [lower] bound and a condition that is extended
1841 * to apply to all previous iterations otherwise.
1843 * If the stride of the loop is not 1, then "i >= init" is replaced by
1845 * (exists a: i = init + stride * a and a >= 0)
1847 * If the loop iterator i is unsigned, then wrapping may occur.
1848 * During the computation, we work with a virtual iterator that
1849 * does not wrap. However, the condition in the code applies
1850 * to the wrapped value, so we need to change condition(i)
1851 * into condition([i % 2^width]).
1852 * After computing the virtual domain and schedule, we apply
1853 * the function { [v] -> [v % 2^width] } to the domain and the domain
1854 * of the schedule. In order not to lose any information, we also
1855 * need to intersect the domain of the schedule with the virtual domain
1856 * first, since some iterations in the wrapped domain may be scheduled
1857 * several times, typically an infinite number of times.
1858 * Note that there is no need to perform this final wrapping
1859 * if the loop condition (after wrapping) is simple.
1861 * Wrapping on unsigned iterators can be avoided entirely if
1862 * loop condition is simple, the loop iterator is incremented
1863 * [decremented] by one and the last value before wrapping cannot
1864 * possibly satisfy the loop condition.
1866 * Before extracting a pet_scop from the body we remove all
1867 * assignments in assigned_value to variables that are assigned
1868 * somewhere in the body of the loop.
1870 struct pet_scop *PetScan::extract_for(ForStmt *stmt)
1872 BinaryOperator *ass;
1873 Decl *decl;
1874 Stmt *init;
1875 Expr *lhs, *rhs;
1876 ValueDecl *iv;
1877 isl_space *dim;
1878 isl_set *domain;
1879 isl_map *sched;
1880 isl_set *cond = NULL;
1881 isl_id *id;
1882 struct pet_scop *scop;
1883 assigned_value_cache cache(assigned_value);
1884 isl_int inc;
1885 bool is_one;
1886 bool is_unsigned;
1887 bool is_simple;
1888 isl_map *wrap = NULL;
1890 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc())
1891 return extract_infinite_for(stmt);
1893 init = stmt->getInit();
1894 if (!init) {
1895 unsupported(stmt);
1896 return NULL;
1898 if ((ass = initialization_assignment(init)) != NULL) {
1899 iv = extract_induction_variable(ass);
1900 if (!iv)
1901 return NULL;
1902 lhs = ass->getLHS();
1903 rhs = ass->getRHS();
1904 } else if ((decl = initialization_declaration(init)) != NULL) {
1905 VarDecl *var = extract_induction_variable(init, decl);
1906 if (!var)
1907 return NULL;
1908 iv = var;
1909 rhs = var->getInit();
1910 lhs = DeclRefExpr::Create(iv->getASTContext(),
1911 var->getQualifierLoc(), iv, var->getInnerLocStart(),
1912 var->getType(), VK_LValue);
1913 } else {
1914 unsupported(stmt->getInit());
1915 return NULL;
1918 isl_int_init(inc);
1919 if (!check_increment(stmt, iv, inc)) {
1920 isl_int_clear(inc);
1921 return NULL;
1924 is_unsigned = iv->getType()->isUnsignedIntegerType();
1926 assigned_value.erase(iv);
1927 clear_assignments clear(assigned_value);
1928 clear.TraverseStmt(stmt->getBody());
1930 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1932 is_one = isl_int_is_one(inc) || isl_int_is_negone(inc);
1933 if (is_one)
1934 domain = extract_comparison(isl_int_is_pos(inc) ? BO_GE : BO_LE,
1935 lhs, rhs, init);
1936 else {
1937 isl_pw_aff *lb = extract_affine(rhs);
1938 domain = strided_domain(isl_id_copy(id), lb, inc);
1941 scop = extract(stmt->getBody());
1943 cond = try_extract_nested_condition(stmt->getCond());
1944 if (cond && !is_nested_allowed(cond, scop)) {
1945 isl_set_free(cond);
1946 cond = NULL;
1949 if (!cond)
1950 cond = extract_condition(stmt->getCond());
1951 cond = embed(cond, isl_id_copy(id));
1952 domain = embed(domain, isl_id_copy(id));
1953 is_simple = is_simple_bound(cond, inc);
1954 if (is_unsigned &&
1955 (!is_simple || !is_one || can_wrap(cond, iv, inc))) {
1956 wrap = compute_wrapping(isl_set_get_space(cond), iv);
1957 cond = isl_set_apply(cond, isl_map_reverse(isl_map_copy(wrap)));
1958 is_simple = is_simple && is_simple_bound(cond, inc);
1960 if (!is_simple)
1961 cond = valid_for_each_iteration(cond,
1962 isl_set_copy(domain), inc);
1963 domain = isl_set_intersect(domain, cond);
1964 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1965 dim = isl_space_from_domain(isl_set_get_space(domain));
1966 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1967 sched = isl_map_universe(dim);
1968 if (isl_int_is_pos(inc))
1969 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1970 else
1971 sched = isl_map_oppose(sched, isl_dim_in, 0, isl_dim_out, 0);
1973 if (is_unsigned && !is_simple) {
1974 wrap = isl_map_set_dim_id(wrap,
1975 isl_dim_out, 0, isl_id_copy(id));
1976 sched = isl_map_intersect_domain(sched, isl_set_copy(domain));
1977 domain = isl_set_apply(domain, isl_map_copy(wrap));
1978 sched = isl_map_apply_domain(sched, wrap);
1979 } else
1980 isl_map_free(wrap);
1982 scop = pet_scop_embed(scop, domain, sched, id);
1983 scop = resolve_nested(scop);
1984 assigned_value[iv] = NULL;
1986 isl_int_clear(inc);
1987 return scop;
1990 struct pet_scop *PetScan::extract(CompoundStmt *stmt)
1992 return extract(stmt->children());
1995 /* Does "id" refer to a nested access?
1997 static bool is_nested_parameter(__isl_keep isl_id *id)
1999 return id && isl_id_get_user(id) && !isl_id_get_name(id);
2002 /* Does parameter "pos" of "space" refer to a nested access?
2004 static bool is_nested_parameter(__isl_keep isl_space *space, int pos)
2006 bool nested;
2007 isl_id *id;
2009 id = isl_space_get_dim_id(space, isl_dim_param, pos);
2010 nested = is_nested_parameter(id);
2011 isl_id_free(id);
2013 return nested;
2016 /* Does parameter "pos" of "map" refer to a nested access?
2018 static bool is_nested_parameter(__isl_keep isl_map *map, int pos)
2020 bool nested;
2021 isl_id *id;
2023 id = isl_map_get_dim_id(map, isl_dim_param, pos);
2024 nested = is_nested_parameter(id);
2025 isl_id_free(id);
2027 return nested;
2030 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
2032 static int n_nested_parameter(__isl_keep isl_space *space)
2034 int n = 0;
2035 int nparam;
2037 nparam = isl_space_dim(space, isl_dim_param);
2038 for (int i = 0; i < nparam; ++i)
2039 if (is_nested_parameter(space, i))
2040 ++n;
2042 return n;
2045 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
2047 static int n_nested_parameter(__isl_keep isl_map *map)
2049 isl_space *space;
2050 int n;
2052 space = isl_map_get_space(map);
2053 n = n_nested_parameter(space);
2054 isl_space_free(space);
2056 return n;
2059 /* For each nested access parameter in "space",
2060 * construct a corresponding pet_expr, place it in args and
2061 * record its position in "param2pos".
2062 * "n_arg" is the number of elements that are already in args.
2063 * The position recorded in "param2pos" takes this number into account.
2064 * If the pet_expr corresponding to a parameter is identical to
2065 * the pet_expr corresponding to an earlier parameter, then these two
2066 * parameters are made to refer to the same element in args.
2068 * Return the final number of elements in args or -1 if an error has occurred.
2070 int PetScan::extract_nested(__isl_keep isl_space *space,
2071 int n_arg, struct pet_expr **args, std::map<int,int> &param2pos)
2073 int nparam;
2075 nparam = isl_space_dim(space, isl_dim_param);
2076 for (int i = 0; i < nparam; ++i) {
2077 int j;
2078 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
2079 Expr *nested;
2081 if (!is_nested_parameter(id)) {
2082 isl_id_free(id);
2083 continue;
2086 nested = (Expr *) isl_id_get_user(id);
2087 args[n_arg] = extract_expr(nested);
2088 if (!args[n_arg])
2089 return -1;
2091 for (j = 0; j < n_arg; ++j)
2092 if (pet_expr_is_equal(args[j], args[n_arg]))
2093 break;
2095 if (j < n_arg) {
2096 pet_expr_free(args[n_arg]);
2097 args[n_arg] = NULL;
2098 param2pos[i] = j;
2099 } else
2100 param2pos[i] = n_arg++;
2102 isl_id_free(id);
2105 return n_arg;
2108 /* For each nested access parameter in the access relations in "expr",
2109 * construct a corresponding pet_expr, place it in expr->args and
2110 * record its position in "param2pos".
2111 * n is the number of nested access parameters.
2113 struct pet_expr *PetScan::extract_nested(struct pet_expr *expr, int n,
2114 std::map<int,int> &param2pos)
2116 isl_space *space;
2118 expr->args = isl_calloc_array(ctx, struct pet_expr *, n);
2119 expr->n_arg = n;
2120 if (!expr->args)
2121 goto error;
2123 space = isl_map_get_space(expr->acc.access);
2124 n = extract_nested(space, 0, expr->args, param2pos);
2125 isl_space_free(space);
2127 if (n < 0)
2128 goto error;
2130 expr->n_arg = n;
2131 return expr;
2132 error:
2133 pet_expr_free(expr);
2134 return NULL;
2137 /* Look for parameters in any access relation in "expr" that
2138 * refer to nested accesses. In particular, these are
2139 * parameters with no name.
2141 * If there are any such parameters, then the domain of the access
2142 * relation, which is still [] at this point, is replaced by
2143 * [[] -> [t_1,...,t_n]], with n the number of these parameters
2144 * (after identifying identical nested accesses).
2145 * The parameters are then equated to the corresponding t dimensions
2146 * and subsequently projected out.
2147 * param2pos maps the position of the parameter to the position
2148 * of the corresponding t dimension.
2150 struct pet_expr *PetScan::resolve_nested(struct pet_expr *expr)
2152 int n;
2153 int nparam;
2154 int n_in;
2155 isl_space *dim;
2156 isl_map *map;
2157 std::map<int,int> param2pos;
2159 if (!expr)
2160 return expr;
2162 for (int i = 0; i < expr->n_arg; ++i) {
2163 expr->args[i] = resolve_nested(expr->args[i]);
2164 if (!expr->args[i]) {
2165 pet_expr_free(expr);
2166 return NULL;
2170 if (expr->type != pet_expr_access)
2171 return expr;
2173 n = n_nested_parameter(expr->acc.access);
2174 if (n == 0)
2175 return expr;
2177 expr = extract_nested(expr, n, param2pos);
2178 if (!expr)
2179 return NULL;
2181 n = expr->n_arg;
2182 nparam = isl_map_dim(expr->acc.access, isl_dim_param);
2183 n_in = isl_map_dim(expr->acc.access, isl_dim_in);
2184 dim = isl_map_get_space(expr->acc.access);
2185 dim = isl_space_domain(dim);
2186 dim = isl_space_from_domain(dim);
2187 dim = isl_space_add_dims(dim, isl_dim_out, n);
2188 map = isl_map_universe(dim);
2189 map = isl_map_domain_map(map);
2190 map = isl_map_reverse(map);
2191 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
2193 for (int i = nparam - 1; i >= 0; --i) {
2194 isl_id *id = isl_map_get_dim_id(expr->acc.access,
2195 isl_dim_param, i);
2196 if (!is_nested_parameter(id)) {
2197 isl_id_free(id);
2198 continue;
2201 expr->acc.access = isl_map_equate(expr->acc.access,
2202 isl_dim_param, i, isl_dim_in,
2203 n_in + param2pos[i]);
2204 expr->acc.access = isl_map_project_out(expr->acc.access,
2205 isl_dim_param, i, 1);
2207 isl_id_free(id);
2210 return expr;
2211 error:
2212 pet_expr_free(expr);
2213 return NULL;
2216 /* Convert a top-level pet_expr to a pet_scop with one statement.
2217 * This mainly involves resolving nested expression parameters
2218 * and setting the name of the iteration space.
2219 * The name is given by "label" if it is non-NULL. Otherwise,
2220 * it is of the form S_<n_stmt>.
2222 struct pet_scop *PetScan::extract(Stmt *stmt, struct pet_expr *expr,
2223 __isl_take isl_id *label)
2225 struct pet_stmt *ps;
2226 SourceLocation loc = stmt->getLocStart();
2227 int line = PP.getSourceManager().getExpansionLineNumber(loc);
2229 expr = resolve_nested(expr);
2230 ps = pet_stmt_from_pet_expr(ctx, line, label, n_stmt++, expr);
2231 return pet_scop_from_pet_stmt(ctx, ps);
2234 /* Check whether "expr" is an affine expression.
2235 * We turn on autodetection so that we won't generate any warnings
2236 * and turn off nesting, so that we won't accept any non-affine constructs.
2238 bool PetScan::is_affine(Expr *expr)
2240 isl_pw_aff *pwaff;
2241 int save_autodetect = autodetect;
2242 bool save_nesting = nesting_enabled;
2244 autodetect = 1;
2245 nesting_enabled = false;
2247 pwaff = extract_affine(expr);
2248 isl_pw_aff_free(pwaff);
2250 autodetect = save_autodetect;
2251 nesting_enabled = save_nesting;
2253 return pwaff != NULL;
2256 /* Check whether "expr" is an affine constraint.
2257 * We turn on autodetection so that we won't generate any warnings
2258 * and turn off nesting, so that we won't accept any non-affine constructs.
2260 bool PetScan::is_affine_condition(Expr *expr)
2262 isl_set *set;
2263 int save_autodetect = autodetect;
2264 bool save_nesting = nesting_enabled;
2266 autodetect = 1;
2267 nesting_enabled = false;
2269 set = extract_condition(expr);
2270 isl_set_free(set);
2272 autodetect = save_autodetect;
2273 nesting_enabled = save_nesting;
2275 return set != NULL;
2278 /* Check if we can extract a condition from "expr".
2279 * Return the condition as an isl_set if we can and NULL otherwise.
2280 * If allow_nested is set, then the condition may involve parameters
2281 * corresponding to nested accesses.
2282 * We turn on autodetection so that we won't generate any warnings.
2284 __isl_give isl_set *PetScan::try_extract_nested_condition(Expr *expr)
2286 isl_set *set;
2287 int save_autodetect = autodetect;
2288 bool save_nesting = nesting_enabled;
2290 autodetect = 1;
2291 nesting_enabled = allow_nested;
2292 set = extract_condition(expr);
2294 autodetect = save_autodetect;
2295 nesting_enabled = save_nesting;
2297 return set;
2300 /* If the top-level expression of "stmt" is an assignment, then
2301 * return that assignment as a BinaryOperator.
2302 * Otherwise return NULL.
2304 static BinaryOperator *top_assignment_or_null(Stmt *stmt)
2306 BinaryOperator *ass;
2308 if (!stmt)
2309 return NULL;
2310 if (stmt->getStmtClass() != Stmt::BinaryOperatorClass)
2311 return NULL;
2313 ass = cast<BinaryOperator>(stmt);
2314 if(ass->getOpcode() != BO_Assign)
2315 return NULL;
2317 return ass;
2320 /* Check if the given if statement is a conditional assignement
2321 * with a non-affine condition. If so, construct a pet_scop
2322 * corresponding to this conditional assignment. Otherwise return NULL.
2324 * In particular we check if "stmt" is of the form
2326 * if (condition)
2327 * a = f(...);
2328 * else
2329 * a = g(...);
2331 * where a is some array or scalar access.
2332 * The constructed pet_scop then corresponds to the expression
2334 * a = condition ? f(...) : g(...)
2336 * All access relations in f(...) are intersected with condition
2337 * while all access relation in g(...) are intersected with the complement.
2339 struct pet_scop *PetScan::extract_conditional_assignment(IfStmt *stmt)
2341 BinaryOperator *ass_then, *ass_else;
2342 isl_map *write_then, *write_else;
2343 isl_set *cond, *comp;
2344 isl_map *map, *map_true, *map_false;
2345 int equal;
2346 struct pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
2347 bool save_nesting = nesting_enabled;
2349 ass_then = top_assignment_or_null(stmt->getThen());
2350 ass_else = top_assignment_or_null(stmt->getElse());
2352 if (!ass_then || !ass_else)
2353 return NULL;
2355 if (is_affine_condition(stmt->getCond()))
2356 return NULL;
2358 write_then = extract_access(ass_then->getLHS());
2359 write_else = extract_access(ass_else->getLHS());
2361 equal = isl_map_is_equal(write_then, write_else);
2362 isl_map_free(write_else);
2363 if (equal < 0 || !equal) {
2364 isl_map_free(write_then);
2365 return NULL;
2368 nesting_enabled = allow_nested;
2369 cond = extract_condition(stmt->getCond());
2370 nesting_enabled = save_nesting;
2371 comp = isl_set_complement(isl_set_copy(cond));
2372 map_true = isl_map_from_domain(isl_set_from_params(isl_set_copy(cond)));
2373 map_true = isl_map_add_dims(map_true, isl_dim_out, 1);
2374 map_true = isl_map_fix_si(map_true, isl_dim_out, 0, 1);
2375 map_false = isl_map_from_domain(isl_set_from_params(isl_set_copy(comp)));
2376 map_false = isl_map_add_dims(map_false, isl_dim_out, 1);
2377 map_false = isl_map_fix_si(map_false, isl_dim_out, 0, 0);
2378 map = isl_map_union_disjoint(map_true, map_false);
2380 pe_cond = pet_expr_from_access(map);
2382 pe_then = extract_expr(ass_then->getRHS());
2383 pe_then = pet_expr_restrict(pe_then, cond);
2384 pe_else = extract_expr(ass_else->getRHS());
2385 pe_else = pet_expr_restrict(pe_else, comp);
2387 pe = pet_expr_new_ternary(ctx, pe_cond, pe_then, pe_else);
2388 pe_write = pet_expr_from_access(write_then);
2389 if (pe_write) {
2390 pe_write->acc.write = 1;
2391 pe_write->acc.read = 0;
2393 pe = pet_expr_new_binary(ctx, pet_op_assign, pe_write, pe);
2394 return extract(stmt, pe);
2397 /* Create an access to a virtual array representing the result
2398 * of a condition.
2399 * Unlike other accessed data, the id of the array is NULL as
2400 * there is no ValueDecl in the program corresponding to the virtual
2401 * array.
2402 * The array starts out as a scalar, but grows along with the
2403 * statement writing to the array in pet_scop_embed.
2405 static __isl_give isl_map *create_test_access(isl_ctx *ctx, int test_nr)
2407 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
2408 isl_id *id;
2409 char name[50];
2411 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
2412 id = isl_id_alloc(ctx, name, NULL);
2413 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
2414 return isl_map_universe(dim);
2417 /* Create a pet_scop with a single statement evaluating "cond"
2418 * and writing the result to a virtual scalar, as expressed by
2419 * "access".
2421 struct pet_scop *PetScan::extract_non_affine_condition(Expr *cond,
2422 __isl_take isl_map *access)
2424 struct pet_expr *expr, *write;
2425 struct pet_stmt *ps;
2426 SourceLocation loc = cond->getLocStart();
2427 int line = PP.getSourceManager().getExpansionLineNumber(loc);
2429 write = pet_expr_from_access(access);
2430 if (write) {
2431 write->acc.write = 1;
2432 write->acc.read = 0;
2434 expr = extract_expr(cond);
2435 expr = pet_expr_new_binary(ctx, pet_op_assign, write, expr);
2436 ps = pet_stmt_from_pet_expr(ctx, line, NULL, n_stmt++, expr);
2437 return pet_scop_from_pet_stmt(ctx, ps);
2440 /* Add an array with the given extend ("access") to the list
2441 * of arrays in "scop" and return the extended pet_scop.
2442 * The array is marked as attaining values 0 and 1 only.
2444 static struct pet_scop *scop_add_array(struct pet_scop *scop,
2445 __isl_keep isl_map *access)
2447 isl_ctx *ctx = isl_map_get_ctx(access);
2448 isl_space *dim;
2449 struct pet_array **arrays;
2450 struct pet_array *array;
2452 if (!scop)
2453 return NULL;
2454 if (!ctx)
2455 goto error;
2457 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
2458 scop->n_array + 1);
2459 if (!arrays)
2460 goto error;
2461 scop->arrays = arrays;
2463 array = isl_calloc_type(ctx, struct pet_array);
2464 if (!array)
2465 goto error;
2467 array->extent = isl_map_range(isl_map_copy(access));
2468 dim = isl_space_params_alloc(ctx, 0);
2469 array->context = isl_set_universe(dim);
2470 dim = isl_space_set_alloc(ctx, 0, 1);
2471 array->value_bounds = isl_set_universe(dim);
2472 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
2473 isl_dim_set, 0, 0);
2474 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
2475 isl_dim_set, 0, 1);
2476 array->element_type = strdup("int");
2478 scop->arrays[scop->n_array] = array;
2479 scop->n_array++;
2481 if (!array->extent || !array->context)
2482 goto error;
2484 return scop;
2485 error:
2486 pet_scop_free(scop);
2487 return NULL;
2490 extern "C" {
2491 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
2492 void *user);
2495 /* Apply the map pointed to by "user" to the domain of the access
2496 * relation, thereby embedding it in the range of the map.
2497 * The domain of both relations is the zero-dimensional domain.
2499 static __isl_give isl_map *embed_access(__isl_take isl_map *access, void *user)
2501 isl_map *map = (isl_map *) user;
2503 return isl_map_apply_domain(access, isl_map_copy(map));
2506 /* Apply "map" to all access relations in "expr".
2508 static struct pet_expr *embed(struct pet_expr *expr, __isl_keep isl_map *map)
2510 return pet_expr_foreach_access(expr, &embed_access, map);
2513 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
2515 static int n_nested_parameter(__isl_keep isl_set *set)
2517 isl_space *space;
2518 int n;
2520 space = isl_set_get_space(set);
2521 n = n_nested_parameter(space);
2522 isl_space_free(space);
2524 return n;
2527 /* Remove all parameters from "map" that refer to nested accesses.
2529 static __isl_give isl_map *remove_nested_parameters(__isl_take isl_map *map)
2531 int nparam;
2532 isl_space *space;
2534 space = isl_map_get_space(map);
2535 nparam = isl_space_dim(space, isl_dim_param);
2536 for (int i = nparam - 1; i >= 0; --i)
2537 if (is_nested_parameter(space, i))
2538 map = isl_map_project_out(map, isl_dim_param, i, 1);
2539 isl_space_free(space);
2541 return map;
2544 extern "C" {
2545 static __isl_give isl_map *access_remove_nested_parameters(
2546 __isl_take isl_map *access, void *user);
2549 static __isl_give isl_map *access_remove_nested_parameters(
2550 __isl_take isl_map *access, void *user)
2552 return remove_nested_parameters(access);
2555 /* Remove all nested access parameters from the schedule and all
2556 * accesses of "stmt".
2557 * There is no need to remove them from the domain as these parameters
2558 * have already been removed from the domain when this function is called.
2560 static struct pet_stmt *remove_nested_parameters(struct pet_stmt *stmt)
2562 if (!stmt)
2563 return NULL;
2564 stmt->schedule = remove_nested_parameters(stmt->schedule);
2565 stmt->body = pet_expr_foreach_access(stmt->body,
2566 &access_remove_nested_parameters, NULL);
2567 if (!stmt->schedule || !stmt->body)
2568 goto error;
2569 for (int i = 0; i < stmt->n_arg; ++i) {
2570 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2571 &access_remove_nested_parameters, NULL);
2572 if (!stmt->args[i])
2573 goto error;
2576 return stmt;
2577 error:
2578 pet_stmt_free(stmt);
2579 return NULL;
2582 /* For each nested access parameter in the domain of "stmt",
2583 * construct a corresponding pet_expr, place it in stmt->args and
2584 * record its position in "param2pos".
2585 * n is the number of nested access parameters.
2587 struct pet_stmt *PetScan::extract_nested(struct pet_stmt *stmt, int n,
2588 std::map<int,int> &param2pos)
2590 isl_space *space;
2591 unsigned n_arg;
2592 struct pet_expr **args;
2594 n_arg = stmt->n_arg;
2595 args = isl_realloc_array(ctx, stmt->args, struct pet_expr *, n_arg + n);
2596 if (!args)
2597 goto error;
2598 stmt->args = args;
2599 stmt->n_arg += n;
2601 space = isl_set_get_space(stmt->domain);
2602 n = extract_nested(space, n_arg, stmt->args, param2pos);
2603 isl_space_free(space);
2605 if (n < 0)
2606 goto error;
2608 stmt->n_arg = n;
2609 return stmt;
2610 error:
2611 pet_stmt_free(stmt);
2612 return NULL;
2615 /* Look for parameters in the iteration domain of "stmt" taht
2616 * refer to nested accesses. In particular, these are
2617 * parameters with no name.
2619 * If there are any such parameters, then as many extra variables
2620 * (after identifying identical nested accesses) are added to the
2621 * range of the map wrapped inside the domain.
2622 * If the original domain is not a wrapped map, then a new wrapped
2623 * map is created with zero output dimensions.
2624 * The parameters are then equated to the corresponding output dimensions
2625 * and subsequently projected out, from the iteration domain,
2626 * the schedule and the access relations.
2627 * For each of the output dimensions, a corresponding argument
2628 * expression is added. Initially they are created with
2629 * a zero-dimensional domain, so they have to be embedded
2630 * in the current iteration domain.
2631 * param2pos maps the position of the parameter to the position
2632 * of the corresponding output dimension in the wrapped map.
2634 struct pet_stmt *PetScan::resolve_nested(struct pet_stmt *stmt)
2636 int n;
2637 int nparam;
2638 unsigned n_arg;
2639 isl_map *map;
2640 std::map<int,int> param2pos;
2642 if (!stmt)
2643 return NULL;
2645 n = n_nested_parameter(stmt->domain);
2646 if (n == 0)
2647 return stmt;
2649 n_arg = stmt->n_arg;
2650 stmt = extract_nested(stmt, n, param2pos);
2651 if (!stmt)
2652 return NULL;
2654 n = stmt->n_arg - n_arg;
2655 nparam = isl_set_dim(stmt->domain, isl_dim_param);
2656 if (isl_set_is_wrapping(stmt->domain))
2657 map = isl_set_unwrap(stmt->domain);
2658 else
2659 map = isl_map_from_domain(stmt->domain);
2660 map = isl_map_add_dims(map, isl_dim_out, n);
2662 for (int i = nparam - 1; i >= 0; --i) {
2663 isl_id *id;
2665 if (!is_nested_parameter(map, i))
2666 continue;
2668 id = isl_map_get_tuple_id(stmt->args[param2pos[i]]->acc.access,
2669 isl_dim_out);
2670 map = isl_map_set_dim_id(map, isl_dim_out, param2pos[i], id);
2671 map = isl_map_equate(map, isl_dim_param, i, isl_dim_out,
2672 param2pos[i]);
2673 map = isl_map_project_out(map, isl_dim_param, i, 1);
2676 stmt->domain = isl_map_wrap(map);
2678 map = isl_set_unwrap(isl_set_copy(stmt->domain));
2679 map = isl_map_from_range(isl_map_domain(map));
2680 for (int pos = n_arg; pos < stmt->n_arg; ++pos)
2681 stmt->args[pos] = embed(stmt->args[pos], map);
2682 isl_map_free(map);
2684 stmt = remove_nested_parameters(stmt);
2686 return stmt;
2687 error:
2688 pet_stmt_free(stmt);
2689 return NULL;
2692 /* For each statement in "scop", move the parameters that correspond
2693 * to nested access into the ranges of the domains and create
2694 * corresponding argument expressions.
2696 struct pet_scop *PetScan::resolve_nested(struct pet_scop *scop)
2698 if (!scop)
2699 return NULL;
2701 for (int i = 0; i < scop->n_stmt; ++i) {
2702 scop->stmts[i] = resolve_nested(scop->stmts[i]);
2703 if (!scop->stmts[i])
2704 goto error;
2707 return scop;
2708 error:
2709 pet_scop_free(scop);
2710 return NULL;
2713 /* Does "space" involve any parameters that refer to nested
2714 * accesses, i.e., parameters with no name?
2716 static bool has_nested(__isl_keep isl_space *space)
2718 int nparam;
2720 nparam = isl_space_dim(space, isl_dim_param);
2721 for (int i = 0; i < nparam; ++i)
2722 if (is_nested_parameter(space, i))
2723 return true;
2725 return false;
2728 /* Does "set" involve any parameters that refer to nested
2729 * accesses, i.e., parameters with no name?
2731 static bool has_nested(__isl_keep isl_set *set)
2733 isl_space *space;
2734 bool nested;
2736 space = isl_set_get_space(set);
2737 nested = has_nested(space);
2738 isl_space_free(space);
2740 return nested;
2743 /* Given an access expression "expr", is the variable accessed by
2744 * "expr" assigned anywhere inside "scop"?
2746 static bool is_assigned(pet_expr *expr, pet_scop *scop)
2748 bool assigned = false;
2749 isl_id *id;
2751 id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2752 assigned = pet_scop_writes(scop, id);
2753 isl_id_free(id);
2755 return assigned;
2758 /* Are all nested access parameters in "set" allowed given "scop".
2759 * In particular, is none of them written by anywhere inside "scop".
2761 bool PetScan::is_nested_allowed(__isl_keep isl_set *set, pet_scop *scop)
2763 int nparam;
2765 nparam = isl_set_dim(set, isl_dim_param);
2766 for (int i = 0; i < nparam; ++i) {
2767 Expr *nested;
2768 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2769 pet_expr *expr;
2770 bool allowed;
2772 if (!is_nested_parameter(id)) {
2773 isl_id_free(id);
2774 continue;
2777 nested = (Expr *) isl_id_get_user(id);
2778 expr = extract_expr(nested);
2779 allowed = expr && expr->type == pet_expr_access &&
2780 !is_assigned(expr, scop);
2782 pet_expr_free(expr);
2783 isl_id_free(id);
2785 if (!allowed)
2786 return false;
2789 return true;
2792 /* Construct a pet_scop for an if statement.
2794 * If the condition fits the pattern of a conditional assignment,
2795 * then it is handled by extract_conditional_assignment.
2796 * Otherwise, we do the following.
2798 * If the condition is affine, then the condition is added
2799 * to the iteration domains of the then branch, while the
2800 * opposite of the condition in added to the iteration domains
2801 * of the else branch, if any.
2802 * We allow the condition to be dynamic, i.e., to refer to
2803 * scalars or array elements that may be written to outside
2804 * of the given if statement. These nested accesses are then represented
2805 * as output dimensions in the wrapping iteration domain.
2806 * If it also written _inside_ the then or else branch, then
2807 * we treat the condition as non-affine.
2808 * As explained below, this will introduce an extra statement.
2809 * For aesthetic reasons, we want this statement to have a statement
2810 * number that is lower than those of the then and else branches.
2811 * In order to evaluate if will need such a statement, however, we
2812 * first construct scops for the then and else branches.
2813 * We therefore reserve a statement number if we might have to
2814 * introduce such an extra statement.
2816 * If the condition is not affine, then we create a separate
2817 * statement that write the result of the condition to a virtual scalar.
2818 * A constraint requiring the value of this virtual scalar to be one
2819 * is added to the iteration domains of the then branch.
2820 * Similarly, a constraint requiring the value of this virtual scalar
2821 * to be zero is added to the iteration domains of the else branch, if any.
2822 * We adjust the schedules to ensure that the virtual scalar is written
2823 * before it is read.
2825 struct pet_scop *PetScan::extract(IfStmt *stmt)
2827 struct pet_scop *scop_then, *scop_else, *scop;
2828 assigned_value_cache cache(assigned_value);
2829 isl_map *test_access = NULL;
2830 isl_set *cond;
2831 int stmt_id;
2833 scop = extract_conditional_assignment(stmt);
2834 if (scop)
2835 return scop;
2837 cond = try_extract_nested_condition(stmt->getCond());
2838 if (allow_nested && (!cond || has_nested(cond)))
2839 stmt_id = n_stmt++;
2841 scop_then = extract(stmt->getThen());
2843 if (stmt->getElse()) {
2844 scop_else = extract(stmt->getElse());
2845 if (autodetect) {
2846 if (scop_then && !scop_else) {
2847 partial = true;
2848 isl_set_free(cond);
2849 return scop_then;
2851 if (!scop_then && scop_else) {
2852 partial = true;
2853 isl_set_free(cond);
2854 return scop_else;
2859 if (cond &&
2860 (!is_nested_allowed(cond, scop_then) ||
2861 (stmt->getElse() && !is_nested_allowed(cond, scop_else)))) {
2862 isl_set_free(cond);
2863 cond = NULL;
2865 if (allow_nested && !cond) {
2866 int save_n_stmt = n_stmt;
2867 test_access = create_test_access(ctx, n_test++);
2868 n_stmt = stmt_id;
2869 scop = extract_non_affine_condition(stmt->getCond(),
2870 isl_map_copy(test_access));
2871 n_stmt = save_n_stmt;
2872 scop = scop_add_array(scop, test_access);
2873 if (!scop) {
2874 pet_scop_free(scop_then);
2875 pet_scop_free(scop_else);
2876 isl_map_free(test_access);
2877 return NULL;
2881 if (!scop) {
2882 if (!cond)
2883 cond = extract_condition(stmt->getCond());
2884 scop = pet_scop_restrict(scop_then, isl_set_copy(cond));
2886 if (stmt->getElse()) {
2887 cond = isl_set_complement(cond);
2888 scop_else = pet_scop_restrict(scop_else, cond);
2889 scop = pet_scop_add(ctx, scop, scop_else);
2890 } else
2891 isl_set_free(cond);
2892 scop = resolve_nested(scop);
2893 } else {
2894 scop = pet_scop_prefix(scop, 0);
2895 scop_then = pet_scop_prefix(scop_then, 1);
2896 scop_then = pet_scop_filter(scop_then,
2897 isl_map_copy(test_access), 1);
2898 scop = pet_scop_add(ctx, scop, scop_then);
2899 if (stmt->getElse()) {
2900 scop_else = pet_scop_prefix(scop_else, 1);
2901 scop_else = pet_scop_filter(scop_else, test_access, 0);
2902 scop = pet_scop_add(ctx, scop, scop_else);
2903 } else
2904 isl_map_free(test_access);
2907 return scop;
2910 /* Try and construct a pet_scop for a label statement.
2911 * We currently only allow labels on expression statements.
2913 struct pet_scop *PetScan::extract(LabelStmt *stmt)
2915 isl_id *label;
2916 Stmt *sub;
2918 sub = stmt->getSubStmt();
2919 if (!isa<Expr>(sub)) {
2920 unsupported(stmt);
2921 return NULL;
2924 label = isl_id_alloc(ctx, stmt->getName(), NULL);
2926 return extract(sub, extract_expr(cast<Expr>(sub)), label);
2929 /* Try and construct a pet_scop corresponding to "stmt".
2931 struct pet_scop *PetScan::extract(Stmt *stmt)
2933 if (isa<Expr>(stmt))
2934 return extract(stmt, extract_expr(cast<Expr>(stmt)));
2936 switch (stmt->getStmtClass()) {
2937 case Stmt::WhileStmtClass:
2938 return extract(cast<WhileStmt>(stmt));
2939 case Stmt::ForStmtClass:
2940 return extract_for(cast<ForStmt>(stmt));
2941 case Stmt::IfStmtClass:
2942 return extract(cast<IfStmt>(stmt));
2943 case Stmt::CompoundStmtClass:
2944 return extract(cast<CompoundStmt>(stmt));
2945 case Stmt::LabelStmtClass:
2946 return extract(cast<LabelStmt>(stmt));
2947 default:
2948 unsupported(stmt);
2951 return NULL;
2954 /* Try and construct a pet_scop corresponding to (part of)
2955 * a sequence of statements.
2957 struct pet_scop *PetScan::extract(StmtRange stmt_range)
2959 pet_scop *scop;
2960 StmtIterator i;
2961 int j;
2962 bool partial_range = false;
2964 scop = pet_scop_empty(ctx);
2965 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j) {
2966 Stmt *child = *i;
2967 struct pet_scop *scop_i;
2968 scop_i = extract(child);
2969 if (scop && partial) {
2970 pet_scop_free(scop_i);
2971 break;
2973 scop_i = pet_scop_prefix(scop_i, j);
2974 if (autodetect) {
2975 if (scop_i)
2976 scop = pet_scop_add(ctx, scop, scop_i);
2977 else
2978 partial_range = true;
2979 if (scop->n_stmt != 0 && !scop_i)
2980 partial = true;
2981 } else {
2982 scop = pet_scop_add(ctx, scop, scop_i);
2984 if (partial)
2985 break;
2988 if (scop && partial_range)
2989 partial = true;
2991 return scop;
2994 /* Check if the scop marked by the user is exactly this Stmt
2995 * or part of this Stmt.
2996 * If so, return a pet_scop corresponding to the marked region.
2997 * Otherwise, return NULL.
2999 struct pet_scop *PetScan::scan(Stmt *stmt)
3001 SourceManager &SM = PP.getSourceManager();
3002 unsigned start_off, end_off;
3004 start_off = SM.getFileOffset(stmt->getLocStart());
3005 end_off = SM.getFileOffset(stmt->getLocEnd());
3007 if (start_off > loc.end)
3008 return NULL;
3009 if (end_off < loc.start)
3010 return NULL;
3011 if (start_off >= loc.start && end_off <= loc.end) {
3012 return extract(stmt);
3015 StmtIterator start;
3016 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
3017 Stmt *child = *start;
3018 if (!child)
3019 continue;
3020 start_off = SM.getFileOffset(child->getLocStart());
3021 end_off = SM.getFileOffset(child->getLocEnd());
3022 if (start_off < loc.start && end_off > loc.end)
3023 return scan(child);
3024 if (start_off >= loc.start)
3025 break;
3028 StmtIterator end;
3029 for (end = start; end != stmt->child_end(); ++end) {
3030 Stmt *child = *end;
3031 start_off = SM.getFileOffset(child->getLocStart());
3032 if (start_off >= loc.end)
3033 break;
3036 return extract(StmtRange(start, end));
3039 /* Set the size of index "pos" of "array" to "size".
3040 * In particular, add a constraint of the form
3042 * i_pos < size
3044 * to array->extent and a constraint of the form
3046 * size >= 0
3048 * to array->context.
3050 static struct pet_array *update_size(struct pet_array *array, int pos,
3051 __isl_take isl_pw_aff *size)
3053 isl_set *valid;
3054 isl_set *univ;
3055 isl_set *bound;
3056 isl_space *dim;
3057 isl_aff *aff;
3058 isl_pw_aff *index;
3059 isl_id *id;
3061 valid = isl_pw_aff_nonneg_set(isl_pw_aff_copy(size));
3062 array->context = isl_set_intersect(array->context, valid);
3064 dim = isl_set_get_space(array->extent);
3065 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
3066 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
3067 univ = isl_set_universe(isl_aff_get_domain_space(aff));
3068 index = isl_pw_aff_alloc(univ, aff);
3070 size = isl_pw_aff_add_dims(size, isl_dim_in,
3071 isl_set_dim(array->extent, isl_dim_set));
3072 id = isl_set_get_tuple_id(array->extent);
3073 size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
3074 bound = isl_pw_aff_lt_set(index, size);
3076 array->extent = isl_set_intersect(array->extent, bound);
3078 if (!array->context || !array->extent)
3079 goto error;
3081 return array;
3082 error:
3083 pet_array_free(array);
3084 return NULL;
3087 /* Figure out the size of the array at position "pos" and all
3088 * subsequent positions from "type" and update "array" accordingly.
3090 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
3091 const Type *type, int pos)
3093 const ArrayType *atype;
3094 isl_pw_aff *size;
3096 if (!array)
3097 return NULL;
3099 if (type->isPointerType()) {
3100 type = type->getPointeeType().getTypePtr();
3101 return set_upper_bounds(array, type, pos + 1);
3103 if (!type->isArrayType())
3104 return array;
3106 type = type->getCanonicalTypeInternal().getTypePtr();
3107 atype = cast<ArrayType>(type);
3109 if (type->isConstantArrayType()) {
3110 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
3111 size = extract_affine(ca->getSize());
3112 array = update_size(array, pos, size);
3113 } else if (type->isVariableArrayType()) {
3114 const VariableArrayType *vla = cast<VariableArrayType>(atype);
3115 size = extract_affine(vla->getSizeExpr());
3116 array = update_size(array, pos, size);
3119 type = atype->getElementType().getTypePtr();
3121 return set_upper_bounds(array, type, pos + 1);
3124 /* Construct and return a pet_array corresponding to the variable "decl".
3125 * In particular, initialize array->extent to
3127 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
3129 * and then call set_upper_bounds to set the upper bounds on the indices
3130 * based on the type of the variable.
3132 struct pet_array *PetScan::extract_array(isl_ctx *ctx, ValueDecl *decl)
3134 struct pet_array *array;
3135 QualType qt = decl->getType();
3136 const Type *type = qt.getTypePtr();
3137 int depth = array_depth(type);
3138 QualType base = base_type(qt);
3139 string name;
3140 isl_id *id;
3141 isl_space *dim;
3143 array = isl_calloc_type(ctx, struct pet_array);
3144 if (!array)
3145 return NULL;
3147 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
3148 dim = isl_space_set_alloc(ctx, 0, depth);
3149 dim = isl_space_set_tuple_id(dim, isl_dim_set, id);
3151 array->extent = isl_set_nat_universe(dim);
3153 dim = isl_space_params_alloc(ctx, 0);
3154 array->context = isl_set_universe(dim);
3156 array = set_upper_bounds(array, type, 0);
3157 if (!array)
3158 return NULL;
3160 name = base.getAsString();
3161 array->element_type = strdup(name.c_str());
3163 return array;
3166 /* Construct a list of pet_arrays, one for each array (or scalar)
3167 * accessed inside "scop" add this list to "scop" and return the result.
3169 * The context of "scop" is updated with the intesection of
3170 * the contexts of all arrays, i.e., constraints on the parameters
3171 * that ensure that the arrays have a valid (non-negative) size.
3173 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop)
3175 int i;
3176 set<ValueDecl *> arrays;
3177 set<ValueDecl *>::iterator it;
3178 int n_array;
3179 struct pet_array **scop_arrays;
3181 if (!scop)
3182 return NULL;
3184 pet_scop_collect_arrays(scop, arrays);
3185 if (arrays.size() == 0)
3186 return scop;
3188 n_array = scop->n_array;
3190 scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3191 n_array + arrays.size());
3192 if (!scop_arrays)
3193 goto error;
3194 scop->arrays = scop_arrays;
3196 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
3197 struct pet_array *array;
3198 scop->arrays[n_array + i] = array = extract_array(ctx, *it);
3199 if (!scop->arrays[n_array + i])
3200 goto error;
3201 scop->n_array++;
3202 scop->context = isl_set_intersect(scop->context,
3203 isl_set_copy(array->context));
3204 if (!scop->context)
3205 goto error;
3208 return scop;
3209 error:
3210 pet_scop_free(scop);
3211 return NULL;
3214 /* Construct a pet_scop from the given function.
3216 struct pet_scop *PetScan::scan(FunctionDecl *fd)
3218 pet_scop *scop;
3219 Stmt *stmt;
3221 stmt = fd->getBody();
3223 if (autodetect)
3224 scop = extract(stmt);
3225 else
3226 scop = scan(stmt);
3227 scop = pet_scop_detect_parameter_accesses(scop);
3228 scop = scan_arrays(scop);
3230 return scop;