PetScan::extract_for: explicitly keep track of when iterator is virtual
[pet.git] / scan.cc
blob0f2e53e490bffeebdd3bf4f8c9d7c4f40a980034
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <set>
36 #include <map>
37 #include <iostream>
38 #include <clang/AST/ASTDiagnostic.h>
39 #include <clang/AST/Expr.h>
40 #include <clang/AST/RecursiveASTVisitor.h>
42 #include <isl/id.h>
43 #include <isl/space.h>
44 #include <isl/aff.h>
45 #include <isl/set.h>
47 #include "scan.h"
48 #include "scop.h"
49 #include "scop_plus.h"
51 #include "config.h"
53 using namespace std;
54 using namespace clang;
56 #ifdef DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION
57 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
59 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
60 SourceLocation(), var, var->getInnerLocStart(), var->getType(),
61 VK_LValue);
63 #else
64 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
66 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
67 var, var->getInnerLocStart(), var->getType(), VK_LValue);
69 #endif
71 /* Check if the element type corresponding to the given array type
72 * has a const qualifier.
74 static bool const_base(QualType qt)
76 const Type *type = qt.getTypePtr();
78 if (type->isPointerType())
79 return const_base(type->getPointeeType());
80 if (type->isArrayType()) {
81 const ArrayType *atype;
82 type = type->getCanonicalTypeInternal().getTypePtr();
83 atype = cast<ArrayType>(type);
84 return const_base(atype->getElementType());
87 return qt.isConstQualified();
90 /* Mark "decl" as having an unknown value in "assigned_value".
92 * If no (known or unknown) value was assigned to "decl" before,
93 * then it may have been treated as a parameter before and may
94 * therefore appear in a value assigned to another variable.
95 * If so, this assignment needs to be turned into an unknown value too.
97 static void clear_assignment(map<ValueDecl *, isl_pw_aff *> &assigned_value,
98 ValueDecl *decl)
100 map<ValueDecl *, isl_pw_aff *>::iterator it;
102 it = assigned_value.find(decl);
104 assigned_value[decl] = NULL;
106 if (it == assigned_value.end())
107 return;
109 for (it = assigned_value.begin(); it != assigned_value.end(); ++it) {
110 isl_pw_aff *pa = it->second;
111 int nparam = isl_pw_aff_dim(pa, isl_dim_param);
113 for (int i = 0; i < nparam; ++i) {
114 isl_id *id;
116 if (!isl_pw_aff_has_dim_id(pa, isl_dim_param, i))
117 continue;
118 id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
119 if (isl_id_get_user(id) == decl)
120 it->second = NULL;
121 isl_id_free(id);
126 /* Look for any assignments to scalar variables in part of the parse
127 * tree and set assigned_value to NULL for each of them.
128 * Also reset assigned_value if the address of a scalar variable
129 * is being taken. As an exception, if the address is passed to a function
130 * that is declared to receive a const pointer, then assigned_value is
131 * not reset.
133 * This ensures that we won't use any previously stored value
134 * in the current subtree and its parents.
136 struct clear_assignments : RecursiveASTVisitor<clear_assignments> {
137 map<ValueDecl *, isl_pw_aff *> &assigned_value;
138 set<UnaryOperator *> skip;
140 clear_assignments(map<ValueDecl *, isl_pw_aff *> &assigned_value) :
141 assigned_value(assigned_value) {}
143 /* Check for "address of" operators whose value is passed
144 * to a const pointer argument and add them to "skip", so that
145 * we can skip them in VisitUnaryOperator.
147 bool VisitCallExpr(CallExpr *expr) {
148 FunctionDecl *fd;
149 fd = expr->getDirectCallee();
150 if (!fd)
151 return true;
152 for (int i = 0; i < expr->getNumArgs(); ++i) {
153 Expr *arg = expr->getArg(i);
154 UnaryOperator *op;
155 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
156 ImplicitCastExpr *ice;
157 ice = cast<ImplicitCastExpr>(arg);
158 arg = ice->getSubExpr();
160 if (arg->getStmtClass() != Stmt::UnaryOperatorClass)
161 continue;
162 op = cast<UnaryOperator>(arg);
163 if (op->getOpcode() != UO_AddrOf)
164 continue;
165 if (const_base(fd->getParamDecl(i)->getType()))
166 skip.insert(op);
168 return true;
171 bool VisitUnaryOperator(UnaryOperator *expr) {
172 Expr *arg;
173 DeclRefExpr *ref;
174 ValueDecl *decl;
176 if (expr->getOpcode() != UO_AddrOf)
177 return true;
178 if (skip.find(expr) != skip.end())
179 return true;
181 arg = expr->getSubExpr();
182 if (arg->getStmtClass() != Stmt::DeclRefExprClass)
183 return true;
184 ref = cast<DeclRefExpr>(arg);
185 decl = ref->getDecl();
186 clear_assignment(assigned_value, decl);
187 return true;
190 bool VisitBinaryOperator(BinaryOperator *expr) {
191 Expr *lhs;
192 DeclRefExpr *ref;
193 ValueDecl *decl;
195 if (!expr->isAssignmentOp())
196 return true;
197 lhs = expr->getLHS();
198 if (lhs->getStmtClass() != Stmt::DeclRefExprClass)
199 return true;
200 ref = cast<DeclRefExpr>(lhs);
201 decl = ref->getDecl();
202 clear_assignment(assigned_value, decl);
203 return true;
207 /* Keep a copy of the currently assigned values.
209 * Any variable that is assigned a value inside the current scope
210 * is removed again when we leave the scope (either because it wasn't
211 * stored in the cache or because it has a different value in the cache).
213 struct assigned_value_cache {
214 map<ValueDecl *, isl_pw_aff *> &assigned_value;
215 map<ValueDecl *, isl_pw_aff *> cache;
217 assigned_value_cache(map<ValueDecl *, isl_pw_aff *> &assigned_value) :
218 assigned_value(assigned_value), cache(assigned_value) {}
219 ~assigned_value_cache() {
220 map<ValueDecl *, isl_pw_aff *>::iterator it = cache.begin();
221 for (it = assigned_value.begin(); it != assigned_value.end();
222 ++it) {
223 if (!it->second ||
224 (cache.find(it->first) != cache.end() &&
225 cache[it->first] != it->second))
226 cache[it->first] = NULL;
228 assigned_value = cache;
232 /* Insert an expression into the collection of expressions,
233 * provided it is not already in there.
234 * The isl_pw_affs are freed in the destructor.
236 void PetScan::insert_expression(__isl_take isl_pw_aff *expr)
238 std::set<isl_pw_aff *>::iterator it;
240 if (expressions.find(expr) == expressions.end())
241 expressions.insert(expr);
242 else
243 isl_pw_aff_free(expr);
246 PetScan::~PetScan()
248 std::set<isl_pw_aff *>::iterator it;
250 for (it = expressions.begin(); it != expressions.end(); ++it)
251 isl_pw_aff_free(*it);
253 isl_union_map_free(value_bounds);
256 /* Called if we found something we (currently) cannot handle.
257 * We'll provide more informative warnings later.
259 * We only actually complain if autodetect is false.
261 void PetScan::unsupported(Stmt *stmt, const char *msg)
263 if (autodetect)
264 return;
266 SourceLocation loc = stmt->getLocStart();
267 DiagnosticsEngine &diag = PP.getDiagnostics();
268 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
269 msg ? msg : "unsupported");
270 DiagnosticBuilder B = diag.Report(loc, id) << stmt->getSourceRange();
273 /* Extract an integer from "expr" and store it in "v".
275 int PetScan::extract_int(IntegerLiteral *expr, isl_int *v)
277 const Type *type = expr->getType().getTypePtr();
278 int is_signed = type->hasSignedIntegerRepresentation();
280 if (is_signed) {
281 int64_t i = expr->getValue().getSExtValue();
282 isl_int_set_si(*v, i);
283 } else {
284 uint64_t i = expr->getValue().getZExtValue();
285 isl_int_set_ui(*v, i);
288 return 0;
291 /* Extract an integer from "expr" and store it in "v".
292 * Return -1 if "expr" does not (obviously) represent an integer.
294 int PetScan::extract_int(clang::ParenExpr *expr, isl_int *v)
296 return extract_int(expr->getSubExpr(), v);
299 /* Extract an integer from "expr" and store it in "v".
300 * Return -1 if "expr" does not (obviously) represent an integer.
302 int PetScan::extract_int(clang::Expr *expr, isl_int *v)
304 if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
305 return extract_int(cast<IntegerLiteral>(expr), v);
306 if (expr->getStmtClass() == Stmt::ParenExprClass)
307 return extract_int(cast<ParenExpr>(expr), v);
309 unsupported(expr);
310 return -1;
313 /* Extract an affine expression from the IntegerLiteral "expr".
315 __isl_give isl_pw_aff *PetScan::extract_affine(IntegerLiteral *expr)
317 isl_space *dim = isl_space_params_alloc(ctx, 0);
318 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
319 isl_aff *aff = isl_aff_zero_on_domain(ls);
320 isl_set *dom = isl_set_universe(dim);
321 isl_int v;
323 isl_int_init(v);
324 extract_int(expr, &v);
325 aff = isl_aff_add_constant(aff, v);
326 isl_int_clear(v);
328 return isl_pw_aff_alloc(dom, aff);
331 /* Extract an affine expression from the APInt "val".
333 __isl_give isl_pw_aff *PetScan::extract_affine(const llvm::APInt &val)
335 isl_space *dim = isl_space_params_alloc(ctx, 0);
336 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
337 isl_aff *aff = isl_aff_zero_on_domain(ls);
338 isl_set *dom = isl_set_universe(dim);
339 isl_int v;
341 isl_int_init(v);
342 isl_int_set_ui(v, val.getZExtValue());
343 aff = isl_aff_add_constant(aff, v);
344 isl_int_clear(v);
346 return isl_pw_aff_alloc(dom, aff);
349 __isl_give isl_pw_aff *PetScan::extract_affine(ImplicitCastExpr *expr)
351 return extract_affine(expr->getSubExpr());
354 static unsigned get_type_size(ValueDecl *decl)
356 return decl->getASTContext().getIntWidth(decl->getType());
359 /* Bound parameter "pos" of "set" to the possible values of "decl".
361 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
362 unsigned pos, ValueDecl *decl)
364 unsigned width;
365 isl_int v;
367 isl_int_init(v);
369 width = get_type_size(decl);
370 if (decl->getType()->isUnsignedIntegerType()) {
371 set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
372 isl_int_set_si(v, 1);
373 isl_int_mul_2exp(v, v, width);
374 isl_int_sub_ui(v, v, 1);
375 set = isl_set_upper_bound(set, isl_dim_param, pos, v);
376 } else {
377 isl_int_set_si(v, 1);
378 isl_int_mul_2exp(v, v, width - 1);
379 isl_int_sub_ui(v, v, 1);
380 set = isl_set_upper_bound(set, isl_dim_param, pos, v);
381 isl_int_neg(v, v);
382 isl_int_sub_ui(v, v, 1);
383 set = isl_set_lower_bound(set, isl_dim_param, pos, v);
386 isl_int_clear(v);
388 return set;
391 /* Extract an affine expression from the DeclRefExpr "expr".
393 * If the variable has been assigned a value, then we check whether
394 * we know what (affine) value was assigned.
395 * If so, we return this value. Otherwise we convert "expr"
396 * to an extra parameter (provided nesting_enabled is set).
398 * Otherwise, we simply return an expression that is equal
399 * to a parameter corresponding to the referenced variable.
401 __isl_give isl_pw_aff *PetScan::extract_affine(DeclRefExpr *expr)
403 ValueDecl *decl = expr->getDecl();
404 const Type *type = decl->getType().getTypePtr();
405 isl_id *id;
406 isl_space *dim;
407 isl_aff *aff;
408 isl_set *dom;
410 if (!type->isIntegerType()) {
411 unsupported(expr);
412 return NULL;
415 if (assigned_value.find(decl) != assigned_value.end()) {
416 if (assigned_value[decl])
417 return isl_pw_aff_copy(assigned_value[decl]);
418 else
419 return nested_access(expr);
422 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
423 dim = isl_space_params_alloc(ctx, 1);
425 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
427 dom = isl_set_universe(isl_space_copy(dim));
428 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
429 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
431 return isl_pw_aff_alloc(dom, aff);
434 /* Extract an affine expression from an integer division operation.
435 * In particular, if "expr" is lhs/rhs, then return
437 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
439 * The second argument (rhs) is required to be a (positive) integer constant.
441 __isl_give isl_pw_aff *PetScan::extract_affine_div(BinaryOperator *expr)
443 Expr *rhs_expr;
444 isl_pw_aff *lhs, *lhs_f, *lhs_c;
445 isl_pw_aff *res;
446 isl_int v;
447 isl_set *cond;
449 rhs_expr = expr->getRHS();
450 isl_int_init(v);
451 if (extract_int(rhs_expr, &v) < 0) {
452 isl_int_clear(v);
453 return NULL;
456 lhs = extract_affine(expr->getLHS());
457 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
459 lhs = isl_pw_aff_scale_down(lhs, v);
460 isl_int_clear(v);
462 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(lhs));
463 lhs_c = isl_pw_aff_ceil(lhs);
464 res = isl_pw_aff_cond(isl_set_indicator_function(cond), lhs_f, lhs_c);
466 return res;
469 /* Extract an affine expression from a modulo operation.
470 * In particular, if "expr" is lhs/rhs, then return
472 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
474 * The second argument (rhs) is required to be a (positive) integer constant.
476 __isl_give isl_pw_aff *PetScan::extract_affine_mod(BinaryOperator *expr)
478 Expr *rhs_expr;
479 isl_pw_aff *lhs, *lhs_f, *lhs_c;
480 isl_pw_aff *res;
481 isl_int v;
482 isl_set *cond;
484 rhs_expr = expr->getRHS();
485 if (rhs_expr->getStmtClass() != Stmt::IntegerLiteralClass) {
486 unsupported(expr);
487 return NULL;
490 lhs = extract_affine(expr->getLHS());
491 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs));
493 isl_int_init(v);
494 extract_int(cast<IntegerLiteral>(rhs_expr), &v);
495 res = isl_pw_aff_scale_down(isl_pw_aff_copy(lhs), v);
497 lhs_f = isl_pw_aff_floor(isl_pw_aff_copy(res));
498 lhs_c = isl_pw_aff_ceil(res);
499 res = isl_pw_aff_cond(isl_set_indicator_function(cond), lhs_f, lhs_c);
501 res = isl_pw_aff_scale(res, v);
502 isl_int_clear(v);
504 res = isl_pw_aff_sub(lhs, res);
506 return res;
509 /* Extract an affine expression from a multiplication operation.
510 * This is only allowed if at least one of the two arguments
511 * is a (piecewise) constant.
513 __isl_give isl_pw_aff *PetScan::extract_affine_mul(BinaryOperator *expr)
515 isl_pw_aff *lhs;
516 isl_pw_aff *rhs;
518 lhs = extract_affine(expr->getLHS());
519 rhs = extract_affine(expr->getRHS());
521 if (!isl_pw_aff_is_cst(lhs) && !isl_pw_aff_is_cst(rhs)) {
522 isl_pw_aff_free(lhs);
523 isl_pw_aff_free(rhs);
524 unsupported(expr);
525 return NULL;
528 return isl_pw_aff_mul(lhs, rhs);
531 /* Extract an affine expression from an addition or subtraction operation.
533 __isl_give isl_pw_aff *PetScan::extract_affine_add(BinaryOperator *expr)
535 isl_pw_aff *lhs;
536 isl_pw_aff *rhs;
538 lhs = extract_affine(expr->getLHS());
539 rhs = extract_affine(expr->getRHS());
541 switch (expr->getOpcode()) {
542 case BO_Add:
543 return isl_pw_aff_add(lhs, rhs);
544 case BO_Sub:
545 return isl_pw_aff_sub(lhs, rhs);
546 default:
547 isl_pw_aff_free(lhs);
548 isl_pw_aff_free(rhs);
549 return NULL;
554 /* Compute
556 * pwaff mod 2^width
558 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff,
559 unsigned width)
561 isl_int mod;
563 isl_int_init(mod);
564 isl_int_set_si(mod, 1);
565 isl_int_mul_2exp(mod, mod, width);
567 pwaff = isl_pw_aff_mod(pwaff, mod);
569 isl_int_clear(mod);
571 return pwaff;
574 /* Extract an affine expression from a boolean expression.
575 * In particular, return the expression "expr ? 1 : 0".
577 __isl_give isl_pw_aff *PetScan::extract_implicit_affine(Expr *expr)
579 isl_set *cond = extract_condition(expr);
580 return isl_set_indicator_function(cond);
583 /* Extract an affine expression from some binary operations.
584 * If the result of the expression is unsigned, then we wrap it
585 * based on the size of the type.
587 __isl_give isl_pw_aff *PetScan::extract_affine(BinaryOperator *expr)
589 isl_pw_aff *res;
591 switch (expr->getOpcode()) {
592 case BO_Add:
593 case BO_Sub:
594 res = extract_affine_add(expr);
595 break;
596 case BO_Div:
597 res = extract_affine_div(expr);
598 break;
599 case BO_Rem:
600 res = extract_affine_mod(expr);
601 break;
602 case BO_Mul:
603 res = extract_affine_mul(expr);
604 break;
605 case BO_LT:
606 case BO_LE:
607 case BO_GT:
608 case BO_GE:
609 case BO_EQ:
610 case BO_NE:
611 case BO_LAnd:
612 case BO_LOr:
613 res = extract_implicit_affine(expr);
614 break;
615 default:
616 unsupported(expr);
617 return NULL;
620 if (expr->getType()->isUnsignedIntegerType())
621 res = wrap(res, ast_context.getIntWidth(expr->getType()));
623 return res;
626 /* Extract an affine expression from a negation operation.
628 __isl_give isl_pw_aff *PetScan::extract_affine(UnaryOperator *expr)
630 if (expr->getOpcode() == UO_Minus)
631 return isl_pw_aff_neg(extract_affine(expr->getSubExpr()));
632 if (expr->getOpcode() == UO_LNot)
633 return extract_implicit_affine(expr);
635 unsupported(expr);
636 return NULL;
639 __isl_give isl_pw_aff *PetScan::extract_affine(ParenExpr *expr)
641 return extract_affine(expr->getSubExpr());
644 /* Extract an affine expression from some special function calls.
645 * In particular, we handle "min", "max", "ceild" and "floord".
646 * In case of the latter two, the second argument needs to be
647 * a (positive) integer constant.
649 __isl_give isl_pw_aff *PetScan::extract_affine(CallExpr *expr)
651 FunctionDecl *fd;
652 string name;
653 isl_pw_aff *aff1, *aff2;
655 fd = expr->getDirectCallee();
656 if (!fd) {
657 unsupported(expr);
658 return NULL;
661 name = fd->getDeclName().getAsString();
662 if (!(expr->getNumArgs() == 2 && name == "min") &&
663 !(expr->getNumArgs() == 2 && name == "max") &&
664 !(expr->getNumArgs() == 2 && name == "floord") &&
665 !(expr->getNumArgs() == 2 && name == "ceild")) {
666 unsupported(expr);
667 return NULL;
670 if (name == "min" || name == "max") {
671 aff1 = extract_affine(expr->getArg(0));
672 aff2 = extract_affine(expr->getArg(1));
674 if (name == "min")
675 aff1 = isl_pw_aff_min(aff1, aff2);
676 else
677 aff1 = isl_pw_aff_max(aff1, aff2);
678 } else if (name == "floord" || name == "ceild") {
679 isl_int v;
680 Expr *arg2 = expr->getArg(1);
682 if (arg2->getStmtClass() != Stmt::IntegerLiteralClass) {
683 unsupported(expr);
684 return NULL;
686 aff1 = extract_affine(expr->getArg(0));
687 isl_int_init(v);
688 extract_int(cast<IntegerLiteral>(arg2), &v);
689 aff1 = isl_pw_aff_scale_down(aff1, v);
690 isl_int_clear(v);
691 if (name == "floord")
692 aff1 = isl_pw_aff_floor(aff1);
693 else
694 aff1 = isl_pw_aff_ceil(aff1);
695 } else {
696 unsupported(expr);
697 return NULL;
700 return aff1;
704 /* This method is called when we come across an access that is
705 * nested in what is supposed to be an affine expression.
706 * If nesting is allowed, we return a new parameter that corresponds
707 * to this nested access. Otherwise, we simply complain.
709 * The new parameter is resolved in resolve_nested.
711 isl_pw_aff *PetScan::nested_access(Expr *expr)
713 isl_id *id;
714 isl_space *dim;
715 isl_aff *aff;
716 isl_set *dom;
718 if (!nesting_enabled) {
719 unsupported(expr);
720 return NULL;
723 id = isl_id_alloc(ctx, NULL, expr);
724 dim = isl_space_params_alloc(ctx, 1);
726 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
728 dom = isl_set_universe(isl_space_copy(dim));
729 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
730 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
732 return isl_pw_aff_alloc(dom, aff);
735 /* Affine expressions are not supposed to contain array accesses,
736 * but if nesting is allowed, we return a parameter corresponding
737 * to the array access.
739 __isl_give isl_pw_aff *PetScan::extract_affine(ArraySubscriptExpr *expr)
741 return nested_access(expr);
744 /* Extract an affine expression from a conditional operation.
746 __isl_give isl_pw_aff *PetScan::extract_affine(ConditionalOperator *expr)
748 isl_set *cond;
749 isl_pw_aff *lhs, *rhs;
751 cond = extract_condition(expr->getCond());
752 lhs = extract_affine(expr->getTrueExpr());
753 rhs = extract_affine(expr->getFalseExpr());
755 return isl_pw_aff_cond(isl_set_indicator_function(cond), lhs, rhs);
758 /* Extract an affine expression, if possible, from "expr".
759 * Otherwise return NULL.
761 __isl_give isl_pw_aff *PetScan::extract_affine(Expr *expr)
763 switch (expr->getStmtClass()) {
764 case Stmt::ImplicitCastExprClass:
765 return extract_affine(cast<ImplicitCastExpr>(expr));
766 case Stmt::IntegerLiteralClass:
767 return extract_affine(cast<IntegerLiteral>(expr));
768 case Stmt::DeclRefExprClass:
769 return extract_affine(cast<DeclRefExpr>(expr));
770 case Stmt::BinaryOperatorClass:
771 return extract_affine(cast<BinaryOperator>(expr));
772 case Stmt::UnaryOperatorClass:
773 return extract_affine(cast<UnaryOperator>(expr));
774 case Stmt::ParenExprClass:
775 return extract_affine(cast<ParenExpr>(expr));
776 case Stmt::CallExprClass:
777 return extract_affine(cast<CallExpr>(expr));
778 case Stmt::ArraySubscriptExprClass:
779 return extract_affine(cast<ArraySubscriptExpr>(expr));
780 case Stmt::ConditionalOperatorClass:
781 return extract_affine(cast<ConditionalOperator>(expr));
782 default:
783 unsupported(expr);
785 return NULL;
788 __isl_give isl_map *PetScan::extract_access(ImplicitCastExpr *expr)
790 return extract_access(expr->getSubExpr());
793 /* Return the depth of an array of the given type.
795 static int array_depth(const Type *type)
797 if (type->isPointerType())
798 return 1 + array_depth(type->getPointeeType().getTypePtr());
799 if (type->isArrayType()) {
800 const ArrayType *atype;
801 type = type->getCanonicalTypeInternal().getTypePtr();
802 atype = cast<ArrayType>(type);
803 return 1 + array_depth(atype->getElementType().getTypePtr());
805 return 0;
808 /* Return the element type of the given array type.
810 static QualType base_type(QualType qt)
812 const Type *type = qt.getTypePtr();
814 if (type->isPointerType())
815 return base_type(type->getPointeeType());
816 if (type->isArrayType()) {
817 const ArrayType *atype;
818 type = type->getCanonicalTypeInternal().getTypePtr();
819 atype = cast<ArrayType>(type);
820 return base_type(atype->getElementType());
822 return qt;
825 /* Extract an access relation from a reference to a variable.
826 * If the variable has name "A" and its type corresponds to an
827 * array of depth d, then the returned access relation is of the
828 * form
830 * { [] -> A[i_1,...,i_d] }
832 __isl_give isl_map *PetScan::extract_access(DeclRefExpr *expr)
834 ValueDecl *decl = expr->getDecl();
835 int depth = array_depth(decl->getType().getTypePtr());
836 isl_id *id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
837 isl_space *dim = isl_space_alloc(ctx, 0, 0, depth);
838 isl_map *access_rel;
840 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
842 access_rel = isl_map_universe(dim);
844 return access_rel;
847 /* Extract an access relation from an integer contant.
848 * If the value of the constant is "v", then the returned access relation
849 * is
851 * { [] -> [v] }
853 __isl_give isl_map *PetScan::extract_access(IntegerLiteral *expr)
855 return isl_map_from_range(isl_set_from_pw_aff(extract_affine(expr)));
858 /* Try and extract an access relation from the given Expr.
859 * Return NULL if it doesn't work out.
861 __isl_give isl_map *PetScan::extract_access(Expr *expr)
863 switch (expr->getStmtClass()) {
864 case Stmt::ImplicitCastExprClass:
865 return extract_access(cast<ImplicitCastExpr>(expr));
866 case Stmt::DeclRefExprClass:
867 return extract_access(cast<DeclRefExpr>(expr));
868 case Stmt::ArraySubscriptExprClass:
869 return extract_access(cast<ArraySubscriptExpr>(expr));
870 default:
871 unsupported(expr);
873 return NULL;
876 /* Assign the affine expression "index" to the output dimension "pos" of "map"
877 * and return the result.
879 __isl_give isl_map *set_index(__isl_take isl_map *map, int pos,
880 __isl_take isl_pw_aff *index)
882 isl_map *index_map;
883 int len = isl_map_dim(map, isl_dim_out);
884 isl_id *id;
886 index_map = isl_map_from_range(isl_set_from_pw_aff(index));
887 index_map = isl_map_insert_dims(index_map, isl_dim_out, 0, pos);
888 index_map = isl_map_add_dims(index_map, isl_dim_out, len - pos - 1);
889 id = isl_map_get_tuple_id(map, isl_dim_out);
890 index_map = isl_map_set_tuple_id(index_map, isl_dim_out, id);
892 map = isl_map_intersect(map, index_map);
894 return map;
897 /* Extract an access relation from the given array subscript expression.
898 * If nesting is allowed in general, then we turn it on while
899 * examining the index expression.
901 * We first extract an access relation from the base.
902 * This will result in an access relation with a range that corresponds
903 * to the array being accessed and with earlier indices filled in already.
904 * We then extract the current index and fill that in as well.
905 * The position of the current index is based on the type of base.
906 * If base is the actual array variable, then the depth of this type
907 * will be the same as the depth of the array and we will fill in
908 * the first array index.
909 * Otherwise, the depth of the base type will be smaller and we will fill
910 * in a later index.
912 __isl_give isl_map *PetScan::extract_access(ArraySubscriptExpr *expr)
914 Expr *base = expr->getBase();
915 Expr *idx = expr->getIdx();
916 isl_pw_aff *index;
917 isl_map *base_access;
918 isl_map *access;
919 int depth = array_depth(base->getType().getTypePtr());
920 int pos;
921 bool save_nesting = nesting_enabled;
923 nesting_enabled = allow_nested;
925 base_access = extract_access(base);
926 index = extract_affine(idx);
928 nesting_enabled = save_nesting;
930 pos = isl_map_dim(base_access, isl_dim_out) - depth;
931 access = set_index(base_access, pos, index);
933 return access;
936 /* Check if "expr" calls function "minmax" with two arguments and if so
937 * make lhs and rhs refer to these two arguments.
939 static bool is_minmax(Expr *expr, const char *minmax, Expr *&lhs, Expr *&rhs)
941 CallExpr *call;
942 FunctionDecl *fd;
943 string name;
945 if (expr->getStmtClass() != Stmt::CallExprClass)
946 return false;
948 call = cast<CallExpr>(expr);
949 fd = call->getDirectCallee();
950 if (!fd)
951 return false;
953 if (call->getNumArgs() != 2)
954 return false;
956 name = fd->getDeclName().getAsString();
957 if (name != minmax)
958 return false;
960 lhs = call->getArg(0);
961 rhs = call->getArg(1);
963 return true;
966 /* Check if "expr" is of the form min(lhs, rhs) and if so make
967 * lhs and rhs refer to the two arguments.
969 static bool is_min(Expr *expr, Expr *&lhs, Expr *&rhs)
971 return is_minmax(expr, "min", lhs, rhs);
974 /* Check if "expr" is of the form max(lhs, rhs) and if so make
975 * lhs and rhs refer to the two arguments.
977 static bool is_max(Expr *expr, Expr *&lhs, Expr *&rhs)
979 return is_minmax(expr, "max", lhs, rhs);
982 /* Extract a set of values satisfying the comparison "LHS op RHS"
983 * "comp" is the original statement that "LHS op RHS" is derived from
984 * and is used for diagnostics.
986 * If the comparison is of the form
988 * a <= min(b,c)
990 * then the set is constructed as the intersection of the set corresponding
991 * to the comparisons
993 * a <= b and a <= c
995 * A similar optimization is performed for max(a,b) <= c.
996 * We do this because that will lead to simpler representations of the set.
997 * If isl is ever enhanced to explicitly deal with min and max expressions,
998 * this optimization can be removed.
1000 __isl_give isl_set *PetScan::extract_comparison(BinaryOperatorKind op,
1001 Expr *LHS, Expr *RHS, Stmt *comp)
1003 isl_pw_aff *lhs;
1004 isl_pw_aff *rhs;
1005 isl_set *cond;
1007 if (op == BO_GT)
1008 return extract_comparison(BO_LT, RHS, LHS, comp);
1009 if (op == BO_GE)
1010 return extract_comparison(BO_LE, RHS, LHS, comp);
1012 if (op == BO_LT || op == BO_LE) {
1013 Expr *expr1, *expr2;
1014 isl_set *set1, *set2;
1015 if (is_min(RHS, expr1, expr2)) {
1016 set1 = extract_comparison(op, LHS, expr1, comp);
1017 set2 = extract_comparison(op, LHS, expr2, comp);
1018 return isl_set_intersect(set1, set2);
1020 if (is_max(LHS, expr1, expr2)) {
1021 set1 = extract_comparison(op, expr1, RHS, comp);
1022 set2 = extract_comparison(op, expr2, RHS, comp);
1023 return isl_set_intersect(set1, set2);
1027 lhs = extract_affine(LHS);
1028 rhs = extract_affine(RHS);
1030 switch (op) {
1031 case BO_LT:
1032 cond = isl_pw_aff_lt_set(lhs, rhs);
1033 break;
1034 case BO_LE:
1035 cond = isl_pw_aff_le_set(lhs, rhs);
1036 break;
1037 case BO_EQ:
1038 cond = isl_pw_aff_eq_set(lhs, rhs);
1039 break;
1040 case BO_NE:
1041 cond = isl_pw_aff_ne_set(lhs, rhs);
1042 break;
1043 default:
1044 isl_pw_aff_free(lhs);
1045 isl_pw_aff_free(rhs);
1046 unsupported(comp);
1047 return NULL;
1050 cond = isl_set_coalesce(cond);
1052 return cond;
1055 __isl_give isl_set *PetScan::extract_comparison(BinaryOperator *comp)
1057 return extract_comparison(comp->getOpcode(), comp->getLHS(),
1058 comp->getRHS(), comp);
1061 /* Extract a set of values satisfying the negation (logical not)
1062 * of a subexpression.
1064 __isl_give isl_set *PetScan::extract_boolean(UnaryOperator *op)
1066 isl_set *cond;
1068 cond = extract_condition(op->getSubExpr());
1070 return isl_set_complement(cond);
1073 /* Extract a set of values satisfying the union (logical or)
1074 * or intersection (logical and) of two subexpressions.
1076 __isl_give isl_set *PetScan::extract_boolean(BinaryOperator *comp)
1078 isl_set *lhs;
1079 isl_set *rhs;
1080 isl_set *cond;
1082 lhs = extract_condition(comp->getLHS());
1083 rhs = extract_condition(comp->getRHS());
1085 switch (comp->getOpcode()) {
1086 case BO_LAnd:
1087 cond = isl_set_intersect(lhs, rhs);
1088 break;
1089 case BO_LOr:
1090 cond = isl_set_union(lhs, rhs);
1091 break;
1092 default:
1093 isl_set_free(lhs);
1094 isl_set_free(rhs);
1095 unsupported(comp);
1096 return NULL;
1099 return cond;
1102 __isl_give isl_set *PetScan::extract_condition(UnaryOperator *expr)
1104 switch (expr->getOpcode()) {
1105 case UO_LNot:
1106 return extract_boolean(expr);
1107 default:
1108 unsupported(expr);
1109 return NULL;
1113 /* Extract a set of values satisfying the condition "expr != 0".
1115 __isl_give isl_set *PetScan::extract_implicit_condition(Expr *expr)
1117 return isl_pw_aff_non_zero_set(extract_affine(expr));
1120 /* Extract a set of values satisfying the condition expressed by "expr".
1122 * If the expression doesn't look like a condition, we assume it
1123 * is an affine expression and return the condition "expr != 0".
1125 __isl_give isl_set *PetScan::extract_condition(Expr *expr)
1127 BinaryOperator *comp;
1129 if (!expr)
1130 return isl_set_universe(isl_space_params_alloc(ctx, 0));
1132 if (expr->getStmtClass() == Stmt::ParenExprClass)
1133 return extract_condition(cast<ParenExpr>(expr)->getSubExpr());
1135 if (expr->getStmtClass() == Stmt::UnaryOperatorClass)
1136 return extract_condition(cast<UnaryOperator>(expr));
1138 if (expr->getStmtClass() != Stmt::BinaryOperatorClass)
1139 return extract_implicit_condition(expr);
1141 comp = cast<BinaryOperator>(expr);
1142 switch (comp->getOpcode()) {
1143 case BO_LT:
1144 case BO_LE:
1145 case BO_GT:
1146 case BO_GE:
1147 case BO_EQ:
1148 case BO_NE:
1149 return extract_comparison(comp);
1150 case BO_LAnd:
1151 case BO_LOr:
1152 return extract_boolean(comp);
1153 default:
1154 return extract_implicit_condition(expr);
1158 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
1160 switch (kind) {
1161 case UO_Minus:
1162 return pet_op_minus;
1163 default:
1164 return pet_op_last;
1168 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
1170 switch (kind) {
1171 case BO_AddAssign:
1172 return pet_op_add_assign;
1173 case BO_SubAssign:
1174 return pet_op_sub_assign;
1175 case BO_MulAssign:
1176 return pet_op_mul_assign;
1177 case BO_DivAssign:
1178 return pet_op_div_assign;
1179 case BO_Assign:
1180 return pet_op_assign;
1181 case BO_Add:
1182 return pet_op_add;
1183 case BO_Sub:
1184 return pet_op_sub;
1185 case BO_Mul:
1186 return pet_op_mul;
1187 case BO_Div:
1188 return pet_op_div;
1189 case BO_EQ:
1190 return pet_op_eq;
1191 case BO_LE:
1192 return pet_op_le;
1193 case BO_LT:
1194 return pet_op_lt;
1195 case BO_GT:
1196 return pet_op_gt;
1197 default:
1198 return pet_op_last;
1202 /* Construct a pet_expr representing a unary operator expression.
1204 struct pet_expr *PetScan::extract_expr(UnaryOperator *expr)
1206 struct pet_expr *arg;
1207 enum pet_op_type op;
1209 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
1210 if (op == pet_op_last) {
1211 unsupported(expr);
1212 return NULL;
1215 arg = extract_expr(expr->getSubExpr());
1217 return pet_expr_new_unary(ctx, op, arg);
1220 /* Mark the given access pet_expr as a write.
1221 * If a scalar is being accessed, then mark its value
1222 * as unknown in assigned_value.
1224 void PetScan::mark_write(struct pet_expr *access)
1226 isl_id *id;
1227 ValueDecl *decl;
1229 access->acc.write = 1;
1230 access->acc.read = 0;
1232 if (isl_map_dim(access->acc.access, isl_dim_out) != 0)
1233 return;
1235 id = isl_map_get_tuple_id(access->acc.access, isl_dim_out);
1236 decl = (ValueDecl *) isl_id_get_user(id);
1237 clear_assignment(assigned_value, decl);
1238 isl_id_free(id);
1241 /* Construct a pet_expr representing a binary operator expression.
1243 * If the top level operator is an assignment and the LHS is an access,
1244 * then we mark that access as a write. If the operator is a compound
1245 * assignment, the access is marked as both a read and a write.
1247 * If "expr" assigns something to a scalar variable, then we mark
1248 * the variable as having been assigned. If, furthermore, the expression
1249 * is affine, then keep track of this value in assigned_value
1250 * so that we can plug it in when we later come across the same variable.
1252 struct pet_expr *PetScan::extract_expr(BinaryOperator *expr)
1254 struct pet_expr *lhs, *rhs;
1255 enum pet_op_type op;
1257 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
1258 if (op == pet_op_last) {
1259 unsupported(expr);
1260 return NULL;
1263 lhs = extract_expr(expr->getLHS());
1264 rhs = extract_expr(expr->getRHS());
1266 if (expr->isAssignmentOp() && lhs && lhs->type == pet_expr_access) {
1267 mark_write(lhs);
1268 if (expr->isCompoundAssignmentOp())
1269 lhs->acc.read = 1;
1272 if (expr->getOpcode() == BO_Assign &&
1273 lhs && lhs->type == pet_expr_access &&
1274 isl_map_dim(lhs->acc.access, isl_dim_out) == 0) {
1275 isl_id *id = isl_map_get_tuple_id(lhs->acc.access, isl_dim_out);
1276 ValueDecl *decl = (ValueDecl *) isl_id_get_user(id);
1277 Expr *rhs = expr->getRHS();
1278 isl_pw_aff *pa = try_extract_affine(rhs);
1279 clear_assignment(assigned_value, decl);
1280 if (pa) {
1281 assigned_value[decl] = pa;
1282 insert_expression(pa);
1284 isl_id_free(id);
1287 return pet_expr_new_binary(ctx, op, lhs, rhs);
1290 /* Construct a pet_expr representing a conditional operation.
1292 struct pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
1294 struct pet_expr *cond, *lhs, *rhs;
1296 cond = extract_expr(expr->getCond());
1297 lhs = extract_expr(expr->getTrueExpr());
1298 rhs = extract_expr(expr->getFalseExpr());
1300 return pet_expr_new_ternary(ctx, cond, lhs, rhs);
1303 struct pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
1305 return extract_expr(expr->getSubExpr());
1308 /* Construct a pet_expr representing a floating point value.
1310 struct pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
1312 return pet_expr_new_double(ctx, expr->getValueAsApproximateDouble());
1315 /* Extract an access relation from "expr" and then convert it into
1316 * a pet_expr.
1318 struct pet_expr *PetScan::extract_access_expr(Expr *expr)
1320 isl_map *access;
1321 struct pet_expr *pe;
1323 switch (expr->getStmtClass()) {
1324 case Stmt::ArraySubscriptExprClass:
1325 access = extract_access(cast<ArraySubscriptExpr>(expr));
1326 break;
1327 case Stmt::DeclRefExprClass:
1328 access = extract_access(cast<DeclRefExpr>(expr));
1329 break;
1330 case Stmt::IntegerLiteralClass:
1331 access = extract_access(cast<IntegerLiteral>(expr));
1332 break;
1333 default:
1334 unsupported(expr);
1335 return NULL;
1338 pe = pet_expr_from_access(access);
1340 return pe;
1343 struct pet_expr *PetScan::extract_expr(ParenExpr *expr)
1345 return extract_expr(expr->getSubExpr());
1348 /* Construct a pet_expr representing a function call.
1350 * If we are passing along a pointer to an array element
1351 * or an entire row or even higher dimensional slice of an array,
1352 * then the function being called may write into the array.
1354 * We assume here that if the function is declared to take a pointer
1355 * to a const type, then the function will perform a read
1356 * and that otherwise, it will perform a write.
1358 struct pet_expr *PetScan::extract_expr(CallExpr *expr)
1360 struct pet_expr *res = NULL;
1361 FunctionDecl *fd;
1362 string name;
1364 fd = expr->getDirectCallee();
1365 if (!fd) {
1366 unsupported(expr);
1367 return NULL;
1370 name = fd->getDeclName().getAsString();
1371 res = pet_expr_new_call(ctx, name.c_str(), expr->getNumArgs());
1372 if (!res)
1373 return NULL;
1375 for (int i = 0; i < expr->getNumArgs(); ++i) {
1376 Expr *arg = expr->getArg(i);
1377 int is_addr = 0;
1378 pet_expr *main_arg;
1380 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
1381 ImplicitCastExpr *ice = cast<ImplicitCastExpr>(arg);
1382 arg = ice->getSubExpr();
1384 if (arg->getStmtClass() == Stmt::UnaryOperatorClass) {
1385 UnaryOperator *op = cast<UnaryOperator>(arg);
1386 if (op->getOpcode() == UO_AddrOf) {
1387 is_addr = 1;
1388 arg = op->getSubExpr();
1391 res->args[i] = PetScan::extract_expr(arg);
1392 main_arg = res->args[i];
1393 if (is_addr)
1394 res->args[i] = pet_expr_new_unary(ctx,
1395 pet_op_address_of, res->args[i]);
1396 if (!res->args[i])
1397 goto error;
1398 if (arg->getStmtClass() == Stmt::ArraySubscriptExprClass &&
1399 array_depth(arg->getType().getTypePtr()) > 0)
1400 is_addr = 1;
1401 if (is_addr && main_arg->type == pet_expr_access) {
1402 ParmVarDecl *parm;
1403 if (!fd->hasPrototype()) {
1404 unsupported(expr, "prototype required");
1405 goto error;
1407 parm = fd->getParamDecl(i);
1408 if (!const_base(parm->getType()))
1409 mark_write(main_arg);
1413 return res;
1414 error:
1415 pet_expr_free(res);
1416 return NULL;
1419 /* Try and onstruct a pet_expr representing "expr".
1421 struct pet_expr *PetScan::extract_expr(Expr *expr)
1423 switch (expr->getStmtClass()) {
1424 case Stmt::UnaryOperatorClass:
1425 return extract_expr(cast<UnaryOperator>(expr));
1426 case Stmt::CompoundAssignOperatorClass:
1427 case Stmt::BinaryOperatorClass:
1428 return extract_expr(cast<BinaryOperator>(expr));
1429 case Stmt::ImplicitCastExprClass:
1430 return extract_expr(cast<ImplicitCastExpr>(expr));
1431 case Stmt::ArraySubscriptExprClass:
1432 case Stmt::DeclRefExprClass:
1433 case Stmt::IntegerLiteralClass:
1434 return extract_access_expr(expr);
1435 case Stmt::FloatingLiteralClass:
1436 return extract_expr(cast<FloatingLiteral>(expr));
1437 case Stmt::ParenExprClass:
1438 return extract_expr(cast<ParenExpr>(expr));
1439 case Stmt::ConditionalOperatorClass:
1440 return extract_expr(cast<ConditionalOperator>(expr));
1441 case Stmt::CallExprClass:
1442 return extract_expr(cast<CallExpr>(expr));
1443 default:
1444 unsupported(expr);
1446 return NULL;
1449 /* Check if the given initialization statement is an assignment.
1450 * If so, return that assignment. Otherwise return NULL.
1452 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1454 BinaryOperator *ass;
1456 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1457 return NULL;
1459 ass = cast<BinaryOperator>(init);
1460 if (ass->getOpcode() != BO_Assign)
1461 return NULL;
1463 return ass;
1466 /* Check if the given initialization statement is a declaration
1467 * of a single variable.
1468 * If so, return that declaration. Otherwise return NULL.
1470 Decl *PetScan::initialization_declaration(Stmt *init)
1472 DeclStmt *decl;
1474 if (init->getStmtClass() != Stmt::DeclStmtClass)
1475 return NULL;
1477 decl = cast<DeclStmt>(init);
1479 if (!decl->isSingleDecl())
1480 return NULL;
1482 return decl->getSingleDecl();
1485 /* Given the assignment operator in the initialization of a for loop,
1486 * extract the induction variable, i.e., the (integer)variable being
1487 * assigned.
1489 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1491 Expr *lhs;
1492 DeclRefExpr *ref;
1493 ValueDecl *decl;
1494 const Type *type;
1496 lhs = init->getLHS();
1497 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1498 unsupported(init);
1499 return NULL;
1502 ref = cast<DeclRefExpr>(lhs);
1503 decl = ref->getDecl();
1504 type = decl->getType().getTypePtr();
1506 if (!type->isIntegerType()) {
1507 unsupported(lhs);
1508 return NULL;
1511 return decl;
1514 /* Given the initialization statement of a for loop and the single
1515 * declaration in this initialization statement,
1516 * extract the induction variable, i.e., the (integer) variable being
1517 * declared.
1519 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1521 VarDecl *vd;
1523 vd = cast<VarDecl>(decl);
1525 const QualType type = vd->getType();
1526 if (!type->isIntegerType()) {
1527 unsupported(init);
1528 return NULL;
1531 if (!vd->getInit()) {
1532 unsupported(init);
1533 return NULL;
1536 return vd;
1539 /* Check that op is of the form iv++ or iv--.
1540 * "inc" is accordingly set to 1 or -1.
1542 bool PetScan::check_unary_increment(UnaryOperator *op, clang::ValueDecl *iv,
1543 isl_int &inc)
1545 Expr *sub;
1546 DeclRefExpr *ref;
1548 if (!op->isIncrementDecrementOp()) {
1549 unsupported(op);
1550 return false;
1553 if (op->isIncrementOp())
1554 isl_int_set_si(inc, 1);
1555 else
1556 isl_int_set_si(inc, -1);
1558 sub = op->getSubExpr();
1559 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1560 unsupported(op);
1561 return false;
1564 ref = cast<DeclRefExpr>(sub);
1565 if (ref->getDecl() != iv) {
1566 unsupported(op);
1567 return false;
1570 return true;
1573 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1574 * has a single constant expression on a universe domain, then
1575 * put this constant in *user.
1577 static int extract_cst(__isl_take isl_set *set, __isl_take isl_aff *aff,
1578 void *user)
1580 isl_int *inc = (isl_int *)user;
1581 int res = 0;
1583 if (!isl_set_plain_is_universe(set) || !isl_aff_is_cst(aff))
1584 res = -1;
1585 else
1586 isl_aff_get_constant(aff, inc);
1588 isl_set_free(set);
1589 isl_aff_free(aff);
1591 return res;
1594 /* Check if op is of the form
1596 * iv = iv + inc
1598 * with inc a constant and set "inc" accordingly.
1600 * We extract an affine expression from the RHS and the subtract iv.
1601 * The result should be a constant.
1603 bool PetScan::check_binary_increment(BinaryOperator *op, clang::ValueDecl *iv,
1604 isl_int &inc)
1606 Expr *lhs;
1607 DeclRefExpr *ref;
1608 isl_id *id;
1609 isl_space *dim;
1610 isl_aff *aff;
1611 isl_pw_aff *val;
1613 if (op->getOpcode() != BO_Assign) {
1614 unsupported(op);
1615 return false;
1618 lhs = op->getLHS();
1619 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1620 unsupported(op);
1621 return false;
1624 ref = cast<DeclRefExpr>(lhs);
1625 if (ref->getDecl() != iv) {
1626 unsupported(op);
1627 return false;
1630 val = extract_affine(op->getRHS());
1632 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1634 dim = isl_space_params_alloc(ctx, 1);
1635 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1636 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1637 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1639 val = isl_pw_aff_sub(val, isl_pw_aff_from_aff(aff));
1641 if (isl_pw_aff_foreach_piece(val, &extract_cst, &inc) < 0) {
1642 isl_pw_aff_free(val);
1643 unsupported(op);
1644 return false;
1647 isl_pw_aff_free(val);
1649 return true;
1652 /* Check that op is of the form iv += cst or iv -= cst.
1653 * "inc" is set to cst or -cst accordingly.
1655 bool PetScan::check_compound_increment(CompoundAssignOperator *op,
1656 clang::ValueDecl *iv, isl_int &inc)
1658 Expr *lhs, *rhs;
1659 DeclRefExpr *ref;
1660 bool neg = false;
1662 BinaryOperatorKind opcode;
1664 opcode = op->getOpcode();
1665 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1666 unsupported(op);
1667 return false;
1669 if (opcode == BO_SubAssign)
1670 neg = true;
1672 lhs = op->getLHS();
1673 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1674 unsupported(op);
1675 return false;
1678 ref = cast<DeclRefExpr>(lhs);
1679 if (ref->getDecl() != iv) {
1680 unsupported(op);
1681 return false;
1684 rhs = op->getRHS();
1686 if (rhs->getStmtClass() == Stmt::UnaryOperatorClass) {
1687 UnaryOperator *op = cast<UnaryOperator>(rhs);
1688 if (op->getOpcode() != UO_Minus) {
1689 unsupported(op);
1690 return false;
1693 neg = !neg;
1695 rhs = op->getSubExpr();
1698 if (rhs->getStmtClass() != Stmt::IntegerLiteralClass) {
1699 unsupported(op);
1700 return false;
1703 extract_int(cast<IntegerLiteral>(rhs), &inc);
1704 if (neg)
1705 isl_int_neg(inc, inc);
1707 return true;
1710 /* Check that the increment of the given for loop increments
1711 * (or decrements) the induction variable "iv".
1712 * "up" is set to true if the induction variable is incremented.
1714 bool PetScan::check_increment(ForStmt *stmt, ValueDecl *iv, isl_int &v)
1716 Stmt *inc = stmt->getInc();
1718 if (!inc) {
1719 unsupported(stmt);
1720 return false;
1723 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1724 return check_unary_increment(cast<UnaryOperator>(inc), iv, v);
1725 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1726 return check_compound_increment(
1727 cast<CompoundAssignOperator>(inc), iv, v);
1728 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1729 return check_binary_increment(cast<BinaryOperator>(inc), iv, v);
1731 unsupported(inc);
1732 return false;
1735 /* Embed the given iteration domain in an extra outer loop
1736 * with induction variable "var".
1737 * If this variable appeared as a parameter in the constraints,
1738 * it is replaced by the new outermost dimension.
1740 static __isl_give isl_set *embed(__isl_take isl_set *set,
1741 __isl_take isl_id *var)
1743 int pos;
1745 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
1746 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
1747 if (pos >= 0) {
1748 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
1749 set = isl_set_project_out(set, isl_dim_param, pos, 1);
1752 isl_id_free(var);
1753 return set;
1756 /* Construct a pet_scop for an infinite loop around the given body.
1758 * We extract a pet_scop for the body and then embed it in a loop with
1759 * iteration domain
1761 * { [t] : t >= 0 }
1763 * and schedule
1765 * { [t] -> [t] }
1767 struct pet_scop *PetScan::extract_infinite_loop(Stmt *body)
1769 isl_id *id;
1770 isl_space *dim;
1771 isl_set *domain;
1772 isl_map *sched;
1773 struct pet_scop *scop;
1775 scop = extract(body);
1776 if (!scop)
1777 return NULL;
1779 id = isl_id_alloc(ctx, "t", NULL);
1780 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
1781 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
1782 dim = isl_space_from_domain(isl_set_get_space(domain));
1783 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1784 sched = isl_map_universe(dim);
1785 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
1786 scop = pet_scop_embed(scop, domain, sched, id);
1788 return scop;
1791 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1793 * for (;;)
1794 * body
1797 struct pet_scop *PetScan::extract_infinite_for(ForStmt *stmt)
1799 return extract_infinite_loop(stmt->getBody());
1802 /* Check if the while loop is of the form
1804 * while (1)
1805 * body
1807 * If so, construct a scop for an infinite loop around body.
1808 * Otherwise, fail.
1810 struct pet_scop *PetScan::extract(WhileStmt *stmt)
1812 Expr *cond;
1813 isl_set *set;
1814 int is_universe;
1816 cond = stmt->getCond();
1817 if (!cond) {
1818 unsupported(stmt);
1819 return NULL;
1822 set = extract_condition(cond);
1823 is_universe = isl_set_plain_is_universe(set);
1824 isl_set_free(set);
1826 if (!is_universe) {
1827 unsupported(stmt);
1828 return NULL;
1831 return extract_infinite_loop(stmt->getBody());
1834 /* Check whether "cond" expresses a simple loop bound
1835 * on the only set dimension.
1836 * In particular, if "up" is set then "cond" should contain only
1837 * upper bounds on the set dimension.
1838 * Otherwise, it should contain only lower bounds.
1840 static bool is_simple_bound(__isl_keep isl_set *cond, isl_int inc)
1842 if (isl_int_is_pos(inc))
1843 return !isl_set_dim_has_lower_bound(cond, isl_dim_set, 0);
1844 else
1845 return !isl_set_dim_has_upper_bound(cond, isl_dim_set, 0);
1848 /* Extend a condition on a given iteration of a loop to one that
1849 * imposes the same condition on all previous iterations.
1850 * "domain" expresses the lower [upper] bound on the iterations
1851 * when inc is positive [negative].
1853 * In particular, we construct the condition (when inc is positive)
1855 * forall i' : (domain(i') and i' <= i) => cond(i')
1857 * which is equivalent to
1859 * not exists i' : domain(i') and i' <= i and not cond(i')
1861 * We construct this set by negating cond, applying a map
1863 * { [i'] -> [i] : domain(i') and i' <= i }
1865 * and then negating the result again.
1867 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
1868 __isl_take isl_set *domain, isl_int inc)
1870 isl_map *previous_to_this;
1872 if (isl_int_is_pos(inc))
1873 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
1874 else
1875 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
1877 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
1879 cond = isl_set_complement(cond);
1880 cond = isl_set_apply(cond, previous_to_this);
1881 cond = isl_set_complement(cond);
1883 return cond;
1886 /* Construct a domain of the form
1888 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
1890 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
1891 __isl_take isl_pw_aff *init, isl_int inc)
1893 isl_aff *aff;
1894 isl_space *dim;
1895 isl_set *set;
1897 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
1898 dim = isl_pw_aff_get_domain_space(init);
1899 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1900 aff = isl_aff_add_coefficient(aff, isl_dim_in, 0, inc);
1901 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
1903 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
1904 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1905 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1906 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1908 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
1910 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
1912 return isl_set_params(set);
1915 /* Assuming "cond" represents a simple bound on a loop where the loop
1916 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1917 * is possible.
1919 * Under the given assumptions, wrapping is only possible if "cond" allows
1920 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1921 * increasing iterator and 0 in case of a decreasing iterator.
1923 static bool can_wrap(__isl_keep isl_set *cond, ValueDecl *iv, isl_int inc)
1925 bool cw;
1926 isl_int limit;
1927 isl_set *test;
1929 test = isl_set_copy(cond);
1931 isl_int_init(limit);
1932 if (isl_int_is_neg(inc))
1933 isl_int_set_si(limit, 0);
1934 else {
1935 isl_int_set_si(limit, 1);
1936 isl_int_mul_2exp(limit, limit, get_type_size(iv));
1937 isl_int_sub_ui(limit, limit, 1);
1940 test = isl_set_fix(cond, isl_dim_set, 0, limit);
1941 cw = !isl_set_is_empty(test);
1942 isl_set_free(test);
1944 isl_int_clear(limit);
1946 return cw;
1949 /* Given a one-dimensional space, construct the following mapping on this
1950 * space
1952 * { [v] -> [v mod 2^width] }
1954 * where width is the number of bits used to represent the values
1955 * of the unsigned variable "iv".
1957 static __isl_give isl_map *compute_wrapping(__isl_take isl_space *dim,
1958 ValueDecl *iv)
1960 isl_int mod;
1961 isl_aff *aff;
1962 isl_map *map;
1964 isl_int_init(mod);
1965 isl_int_set_si(mod, 1);
1966 isl_int_mul_2exp(mod, mod, get_type_size(iv));
1968 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1969 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
1970 aff = isl_aff_mod(aff, mod);
1972 isl_int_clear(mod);
1974 return isl_map_from_basic_map(isl_basic_map_from_aff(aff));
1975 map = isl_map_reverse(map);
1978 /* Construct a pet_scop for a for statement.
1979 * The for loop is required to be of the form
1981 * for (i = init; condition; ++i)
1983 * or
1985 * for (i = init; condition; --i)
1987 * The initialization of the for loop should either be an assignment
1988 * to an integer variable, or a declaration of such a variable with
1989 * initialization.
1991 * The condition is allowed to contain nested accesses, provided
1992 * they are not being written to inside the body of the loop.
1994 * We extract a pet_scop for the body and then embed it in a loop with
1995 * iteration domain and schedule
1997 * { [i] : i >= init and condition' }
1998 * { [i] -> [i] }
2000 * or
2002 * { [i] : i <= init and condition' }
2003 * { [i] -> [-i] }
2005 * Where condition' is equal to condition if the latter is
2006 * a simple upper [lower] bound and a condition that is extended
2007 * to apply to all previous iterations otherwise.
2009 * If the stride of the loop is not 1, then "i >= init" is replaced by
2011 * (exists a: i = init + stride * a and a >= 0)
2013 * If the loop iterator i is unsigned, then wrapping may occur.
2014 * During the computation, we work with a virtual iterator that
2015 * does not wrap. However, the condition in the code applies
2016 * to the wrapped value, so we need to change condition(i)
2017 * into condition([i % 2^width]).
2018 * After computing the virtual domain and schedule, we apply
2019 * the function { [v] -> [v % 2^width] } to the domain and the domain
2020 * of the schedule. In order not to lose any information, we also
2021 * need to intersect the domain of the schedule with the virtual domain
2022 * first, since some iterations in the wrapped domain may be scheduled
2023 * several times, typically an infinite number of times.
2024 * Note that there is no need to perform this final wrapping
2025 * if the loop condition (after wrapping) is simple.
2027 * Wrapping on unsigned iterators can be avoided entirely if
2028 * loop condition is simple, the loop iterator is incremented
2029 * [decremented] by one and the last value before wrapping cannot
2030 * possibly satisfy the loop condition.
2032 * Before extracting a pet_scop from the body we remove all
2033 * assignments in assigned_value to variables that are assigned
2034 * somewhere in the body of the loop.
2036 struct pet_scop *PetScan::extract_for(ForStmt *stmt)
2038 BinaryOperator *ass;
2039 Decl *decl;
2040 Stmt *init;
2041 Expr *lhs, *rhs;
2042 ValueDecl *iv;
2043 isl_space *dim;
2044 isl_set *domain;
2045 isl_map *sched;
2046 isl_set *cond = NULL;
2047 isl_id *id;
2048 struct pet_scop *scop;
2049 assigned_value_cache cache(assigned_value);
2050 isl_int inc;
2051 bool is_one;
2052 bool is_unsigned;
2053 bool is_simple;
2054 bool is_virtual;
2055 isl_map *wrap = NULL;
2057 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc())
2058 return extract_infinite_for(stmt);
2060 init = stmt->getInit();
2061 if (!init) {
2062 unsupported(stmt);
2063 return NULL;
2065 if ((ass = initialization_assignment(init)) != NULL) {
2066 iv = extract_induction_variable(ass);
2067 if (!iv)
2068 return NULL;
2069 lhs = ass->getLHS();
2070 rhs = ass->getRHS();
2071 } else if ((decl = initialization_declaration(init)) != NULL) {
2072 VarDecl *var = extract_induction_variable(init, decl);
2073 if (!var)
2074 return NULL;
2075 iv = var;
2076 rhs = var->getInit();
2077 lhs = create_DeclRefExpr(var);
2078 } else {
2079 unsupported(stmt->getInit());
2080 return NULL;
2083 isl_int_init(inc);
2084 if (!check_increment(stmt, iv, inc)) {
2085 isl_int_clear(inc);
2086 return NULL;
2089 is_unsigned = iv->getType()->isUnsignedIntegerType();
2091 assigned_value.erase(iv);
2092 clear_assignments clear(assigned_value);
2093 clear.TraverseStmt(stmt->getBody());
2095 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
2097 is_one = isl_int_is_one(inc) || isl_int_is_negone(inc);
2098 if (is_one)
2099 domain = extract_comparison(isl_int_is_pos(inc) ? BO_GE : BO_LE,
2100 lhs, rhs, init);
2101 else {
2102 isl_pw_aff *lb = extract_affine(rhs);
2103 domain = strided_domain(isl_id_copy(id), lb, inc);
2106 scop = extract(stmt->getBody());
2108 cond = try_extract_nested_condition(stmt->getCond());
2109 if (cond && !is_nested_allowed(cond, scop)) {
2110 isl_set_free(cond);
2111 cond = NULL;
2114 if (!cond)
2115 cond = extract_condition(stmt->getCond());
2116 cond = embed(cond, isl_id_copy(id));
2117 domain = embed(domain, isl_id_copy(id));
2118 is_simple = is_simple_bound(cond, inc);
2119 is_virtual = is_unsigned &&
2120 (!is_simple || !is_one || can_wrap(cond, iv, inc));
2121 if (is_virtual) {
2122 wrap = compute_wrapping(isl_set_get_space(cond), iv);
2123 cond = isl_set_apply(cond, isl_map_reverse(isl_map_copy(wrap)));
2124 is_simple = is_simple && is_simple_bound(cond, inc);
2126 if (!is_simple)
2127 cond = valid_for_each_iteration(cond,
2128 isl_set_copy(domain), inc);
2129 domain = isl_set_intersect(domain, cond);
2130 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
2131 dim = isl_space_from_domain(isl_set_get_space(domain));
2132 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2133 sched = isl_map_universe(dim);
2134 if (isl_int_is_pos(inc))
2135 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
2136 else
2137 sched = isl_map_oppose(sched, isl_dim_in, 0, isl_dim_out, 0);
2139 if (is_virtual && !is_simple) {
2140 wrap = isl_map_set_dim_id(wrap,
2141 isl_dim_out, 0, isl_id_copy(id));
2142 sched = isl_map_intersect_domain(sched, isl_set_copy(domain));
2143 domain = isl_set_apply(domain, isl_map_copy(wrap));
2144 sched = isl_map_apply_domain(sched, wrap);
2145 } else
2146 isl_map_free(wrap);
2148 scop = pet_scop_embed(scop, domain, sched, id);
2149 scop = resolve_nested(scop);
2150 clear_assignment(assigned_value, iv);
2152 isl_int_clear(inc);
2153 return scop;
2156 struct pet_scop *PetScan::extract(CompoundStmt *stmt)
2158 return extract(stmt->children());
2161 /* Does "id" refer to a nested access?
2163 static bool is_nested_parameter(__isl_keep isl_id *id)
2165 return id && isl_id_get_user(id) && !isl_id_get_name(id);
2168 /* Does parameter "pos" of "space" refer to a nested access?
2170 static bool is_nested_parameter(__isl_keep isl_space *space, int pos)
2172 bool nested;
2173 isl_id *id;
2175 id = isl_space_get_dim_id(space, isl_dim_param, pos);
2176 nested = is_nested_parameter(id);
2177 isl_id_free(id);
2179 return nested;
2182 /* Does parameter "pos" of "map" refer to a nested access?
2184 static bool is_nested_parameter(__isl_keep isl_map *map, int pos)
2186 bool nested;
2187 isl_id *id;
2189 id = isl_map_get_dim_id(map, isl_dim_param, pos);
2190 nested = is_nested_parameter(id);
2191 isl_id_free(id);
2193 return nested;
2196 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
2198 static int n_nested_parameter(__isl_keep isl_space *space)
2200 int n = 0;
2201 int nparam;
2203 nparam = isl_space_dim(space, isl_dim_param);
2204 for (int i = 0; i < nparam; ++i)
2205 if (is_nested_parameter(space, i))
2206 ++n;
2208 return n;
2211 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
2213 static int n_nested_parameter(__isl_keep isl_map *map)
2215 isl_space *space;
2216 int n;
2218 space = isl_map_get_space(map);
2219 n = n_nested_parameter(space);
2220 isl_space_free(space);
2222 return n;
2225 /* For each nested access parameter in "space",
2226 * construct a corresponding pet_expr, place it in args and
2227 * record its position in "param2pos".
2228 * "n_arg" is the number of elements that are already in args.
2229 * The position recorded in "param2pos" takes this number into account.
2230 * If the pet_expr corresponding to a parameter is identical to
2231 * the pet_expr corresponding to an earlier parameter, then these two
2232 * parameters are made to refer to the same element in args.
2234 * Return the final number of elements in args or -1 if an error has occurred.
2236 int PetScan::extract_nested(__isl_keep isl_space *space,
2237 int n_arg, struct pet_expr **args, std::map<int,int> &param2pos)
2239 int nparam;
2241 nparam = isl_space_dim(space, isl_dim_param);
2242 for (int i = 0; i < nparam; ++i) {
2243 int j;
2244 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
2245 Expr *nested;
2247 if (!is_nested_parameter(id)) {
2248 isl_id_free(id);
2249 continue;
2252 nested = (Expr *) isl_id_get_user(id);
2253 args[n_arg] = extract_expr(nested);
2254 if (!args[n_arg])
2255 return -1;
2257 for (j = 0; j < n_arg; ++j)
2258 if (pet_expr_is_equal(args[j], args[n_arg]))
2259 break;
2261 if (j < n_arg) {
2262 pet_expr_free(args[n_arg]);
2263 args[n_arg] = NULL;
2264 param2pos[i] = j;
2265 } else
2266 param2pos[i] = n_arg++;
2268 isl_id_free(id);
2271 return n_arg;
2274 /* For each nested access parameter in the access relations in "expr",
2275 * construct a corresponding pet_expr, place it in expr->args and
2276 * record its position in "param2pos".
2277 * n is the number of nested access parameters.
2279 struct pet_expr *PetScan::extract_nested(struct pet_expr *expr, int n,
2280 std::map<int,int> &param2pos)
2282 isl_space *space;
2284 expr->args = isl_calloc_array(ctx, struct pet_expr *, n);
2285 expr->n_arg = n;
2286 if (!expr->args)
2287 goto error;
2289 space = isl_map_get_space(expr->acc.access);
2290 n = extract_nested(space, 0, expr->args, param2pos);
2291 isl_space_free(space);
2293 if (n < 0)
2294 goto error;
2296 expr->n_arg = n;
2297 return expr;
2298 error:
2299 pet_expr_free(expr);
2300 return NULL;
2303 /* Look for parameters in any access relation in "expr" that
2304 * refer to nested accesses. In particular, these are
2305 * parameters with no name.
2307 * If there are any such parameters, then the domain of the access
2308 * relation, which is still [] at this point, is replaced by
2309 * [[] -> [t_1,...,t_n]], with n the number of these parameters
2310 * (after identifying identical nested accesses).
2311 * The parameters are then equated to the corresponding t dimensions
2312 * and subsequently projected out.
2313 * param2pos maps the position of the parameter to the position
2314 * of the corresponding t dimension.
2316 struct pet_expr *PetScan::resolve_nested(struct pet_expr *expr)
2318 int n;
2319 int nparam;
2320 int n_in;
2321 isl_space *dim;
2322 isl_map *map;
2323 std::map<int,int> param2pos;
2325 if (!expr)
2326 return expr;
2328 for (int i = 0; i < expr->n_arg; ++i) {
2329 expr->args[i] = resolve_nested(expr->args[i]);
2330 if (!expr->args[i]) {
2331 pet_expr_free(expr);
2332 return NULL;
2336 if (expr->type != pet_expr_access)
2337 return expr;
2339 n = n_nested_parameter(expr->acc.access);
2340 if (n == 0)
2341 return expr;
2343 expr = extract_nested(expr, n, param2pos);
2344 if (!expr)
2345 return NULL;
2347 n = expr->n_arg;
2348 nparam = isl_map_dim(expr->acc.access, isl_dim_param);
2349 n_in = isl_map_dim(expr->acc.access, isl_dim_in);
2350 dim = isl_map_get_space(expr->acc.access);
2351 dim = isl_space_domain(dim);
2352 dim = isl_space_from_domain(dim);
2353 dim = isl_space_add_dims(dim, isl_dim_out, n);
2354 map = isl_map_universe(dim);
2355 map = isl_map_domain_map(map);
2356 map = isl_map_reverse(map);
2357 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
2359 for (int i = nparam - 1; i >= 0; --i) {
2360 isl_id *id = isl_map_get_dim_id(expr->acc.access,
2361 isl_dim_param, i);
2362 if (!is_nested_parameter(id)) {
2363 isl_id_free(id);
2364 continue;
2367 expr->acc.access = isl_map_equate(expr->acc.access,
2368 isl_dim_param, i, isl_dim_in,
2369 n_in + param2pos[i]);
2370 expr->acc.access = isl_map_project_out(expr->acc.access,
2371 isl_dim_param, i, 1);
2373 isl_id_free(id);
2376 return expr;
2377 error:
2378 pet_expr_free(expr);
2379 return NULL;
2382 /* Convert a top-level pet_expr to a pet_scop with one statement.
2383 * This mainly involves resolving nested expression parameters
2384 * and setting the name of the iteration space.
2385 * The name is given by "label" if it is non-NULL. Otherwise,
2386 * it is of the form S_<n_stmt>.
2388 struct pet_scop *PetScan::extract(Stmt *stmt, struct pet_expr *expr,
2389 __isl_take isl_id *label)
2391 struct pet_stmt *ps;
2392 SourceLocation loc = stmt->getLocStart();
2393 int line = PP.getSourceManager().getExpansionLineNumber(loc);
2395 expr = resolve_nested(expr);
2396 ps = pet_stmt_from_pet_expr(ctx, line, label, n_stmt++, expr);
2397 return pet_scop_from_pet_stmt(ctx, ps);
2400 /* Check if we can extract an affine expression from "expr".
2401 * Return the expressions as an isl_pw_aff if we can and NULL otherwise.
2402 * We turn on autodetection so that we won't generate any warnings
2403 * and turn off nesting, so that we won't accept any non-affine constructs.
2405 __isl_give isl_pw_aff *PetScan::try_extract_affine(Expr *expr)
2407 isl_pw_aff *pwaff;
2408 int save_autodetect = autodetect;
2409 bool save_nesting = nesting_enabled;
2411 autodetect = 1;
2412 nesting_enabled = false;
2414 pwaff = extract_affine(expr);
2416 autodetect = save_autodetect;
2417 nesting_enabled = save_nesting;
2419 return pwaff;
2422 /* Check whether "expr" is an affine expression.
2424 bool PetScan::is_affine(Expr *expr)
2426 isl_pw_aff *pwaff;
2428 pwaff = try_extract_affine(expr);
2429 isl_pw_aff_free(pwaff);
2431 return pwaff != NULL;
2434 /* Check whether "expr" is an affine constraint.
2435 * We turn on autodetection so that we won't generate any warnings
2436 * and turn off nesting, so that we won't accept any non-affine constructs.
2438 bool PetScan::is_affine_condition(Expr *expr)
2440 isl_set *set;
2441 int save_autodetect = autodetect;
2442 bool save_nesting = nesting_enabled;
2444 autodetect = 1;
2445 nesting_enabled = false;
2447 set = extract_condition(expr);
2448 isl_set_free(set);
2450 autodetect = save_autodetect;
2451 nesting_enabled = save_nesting;
2453 return set != NULL;
2456 /* Check if we can extract a condition from "expr".
2457 * Return the condition as an isl_set if we can and NULL otherwise.
2458 * If allow_nested is set, then the condition may involve parameters
2459 * corresponding to nested accesses.
2460 * We turn on autodetection so that we won't generate any warnings.
2462 __isl_give isl_set *PetScan::try_extract_nested_condition(Expr *expr)
2464 isl_set *set;
2465 int save_autodetect = autodetect;
2466 bool save_nesting = nesting_enabled;
2468 autodetect = 1;
2469 nesting_enabled = allow_nested;
2470 set = extract_condition(expr);
2472 autodetect = save_autodetect;
2473 nesting_enabled = save_nesting;
2475 return set;
2478 /* If the top-level expression of "stmt" is an assignment, then
2479 * return that assignment as a BinaryOperator.
2480 * Otherwise return NULL.
2482 static BinaryOperator *top_assignment_or_null(Stmt *stmt)
2484 BinaryOperator *ass;
2486 if (!stmt)
2487 return NULL;
2488 if (stmt->getStmtClass() != Stmt::BinaryOperatorClass)
2489 return NULL;
2491 ass = cast<BinaryOperator>(stmt);
2492 if(ass->getOpcode() != BO_Assign)
2493 return NULL;
2495 return ass;
2498 /* Check if the given if statement is a conditional assignement
2499 * with a non-affine condition. If so, construct a pet_scop
2500 * corresponding to this conditional assignment. Otherwise return NULL.
2502 * In particular we check if "stmt" is of the form
2504 * if (condition)
2505 * a = f(...);
2506 * else
2507 * a = g(...);
2509 * where a is some array or scalar access.
2510 * The constructed pet_scop then corresponds to the expression
2512 * a = condition ? f(...) : g(...)
2514 * All access relations in f(...) are intersected with condition
2515 * while all access relation in g(...) are intersected with the complement.
2517 struct pet_scop *PetScan::extract_conditional_assignment(IfStmt *stmt)
2519 BinaryOperator *ass_then, *ass_else;
2520 isl_map *write_then, *write_else;
2521 isl_set *cond, *comp;
2522 isl_map *map, *map_true, *map_false;
2523 int equal;
2524 struct pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
2525 bool save_nesting = nesting_enabled;
2527 ass_then = top_assignment_or_null(stmt->getThen());
2528 ass_else = top_assignment_or_null(stmt->getElse());
2530 if (!ass_then || !ass_else)
2531 return NULL;
2533 if (is_affine_condition(stmt->getCond()))
2534 return NULL;
2536 write_then = extract_access(ass_then->getLHS());
2537 write_else = extract_access(ass_else->getLHS());
2539 equal = isl_map_is_equal(write_then, write_else);
2540 isl_map_free(write_else);
2541 if (equal < 0 || !equal) {
2542 isl_map_free(write_then);
2543 return NULL;
2546 nesting_enabled = allow_nested;
2547 cond = extract_condition(stmt->getCond());
2548 nesting_enabled = save_nesting;
2549 comp = isl_set_complement(isl_set_copy(cond));
2550 map_true = isl_map_from_domain(isl_set_from_params(isl_set_copy(cond)));
2551 map_true = isl_map_add_dims(map_true, isl_dim_out, 1);
2552 map_true = isl_map_fix_si(map_true, isl_dim_out, 0, 1);
2553 map_false = isl_map_from_domain(isl_set_from_params(isl_set_copy(comp)));
2554 map_false = isl_map_add_dims(map_false, isl_dim_out, 1);
2555 map_false = isl_map_fix_si(map_false, isl_dim_out, 0, 0);
2556 map = isl_map_union_disjoint(map_true, map_false);
2558 pe_cond = pet_expr_from_access(map);
2560 pe_then = extract_expr(ass_then->getRHS());
2561 pe_then = pet_expr_restrict(pe_then, cond);
2562 pe_else = extract_expr(ass_else->getRHS());
2563 pe_else = pet_expr_restrict(pe_else, comp);
2565 pe = pet_expr_new_ternary(ctx, pe_cond, pe_then, pe_else);
2566 pe_write = pet_expr_from_access(write_then);
2567 if (pe_write) {
2568 pe_write->acc.write = 1;
2569 pe_write->acc.read = 0;
2571 pe = pet_expr_new_binary(ctx, pet_op_assign, pe_write, pe);
2572 return extract(stmt, pe);
2575 /* Create an access to a virtual array representing the result
2576 * of a condition.
2577 * Unlike other accessed data, the id of the array is NULL as
2578 * there is no ValueDecl in the program corresponding to the virtual
2579 * array.
2580 * The array starts out as a scalar, but grows along with the
2581 * statement writing to the array in pet_scop_embed.
2583 static __isl_give isl_map *create_test_access(isl_ctx *ctx, int test_nr)
2585 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
2586 isl_id *id;
2587 char name[50];
2589 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
2590 id = isl_id_alloc(ctx, name, NULL);
2591 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
2592 return isl_map_universe(dim);
2595 /* Create a pet_scop with a single statement evaluating "cond"
2596 * and writing the result to a virtual scalar, as expressed by
2597 * "access".
2599 struct pet_scop *PetScan::extract_non_affine_condition(Expr *cond,
2600 __isl_take isl_map *access)
2602 struct pet_expr *expr, *write;
2603 struct pet_stmt *ps;
2604 SourceLocation loc = cond->getLocStart();
2605 int line = PP.getSourceManager().getExpansionLineNumber(loc);
2607 write = pet_expr_from_access(access);
2608 if (write) {
2609 write->acc.write = 1;
2610 write->acc.read = 0;
2612 expr = extract_expr(cond);
2613 expr = pet_expr_new_binary(ctx, pet_op_assign, write, expr);
2614 ps = pet_stmt_from_pet_expr(ctx, line, NULL, n_stmt++, expr);
2615 return pet_scop_from_pet_stmt(ctx, ps);
2618 /* Add an array with the given extent ("access") to the list
2619 * of arrays in "scop" and return the extended pet_scop.
2620 * The array is marked as attaining values 0 and 1 only.
2622 static struct pet_scop *scop_add_array(struct pet_scop *scop,
2623 __isl_keep isl_map *access, clang::ASTContext &ast_ctx)
2625 isl_ctx *ctx = isl_map_get_ctx(access);
2626 isl_space *dim;
2627 struct pet_array **arrays;
2628 struct pet_array *array;
2630 if (!scop)
2631 return NULL;
2632 if (!ctx)
2633 goto error;
2635 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
2636 scop->n_array + 1);
2637 if (!arrays)
2638 goto error;
2639 scop->arrays = arrays;
2641 array = isl_calloc_type(ctx, struct pet_array);
2642 if (!array)
2643 goto error;
2645 array->extent = isl_map_range(isl_map_copy(access));
2646 dim = isl_space_params_alloc(ctx, 0);
2647 array->context = isl_set_universe(dim);
2648 dim = isl_space_set_alloc(ctx, 0, 1);
2649 array->value_bounds = isl_set_universe(dim);
2650 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
2651 isl_dim_set, 0, 0);
2652 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
2653 isl_dim_set, 0, 1);
2654 array->element_type = strdup("int");
2655 array->element_size = ast_ctx.getTypeInfo(ast_ctx.IntTy).first / 8;
2657 scop->arrays[scop->n_array] = array;
2658 scop->n_array++;
2660 if (!array->extent || !array->context)
2661 goto error;
2663 return scop;
2664 error:
2665 pet_scop_free(scop);
2666 return NULL;
2669 extern "C" {
2670 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
2671 void *user);
2674 /* Apply the map pointed to by "user" to the domain of the access
2675 * relation, thereby embedding it in the range of the map.
2676 * The domain of both relations is the zero-dimensional domain.
2678 static __isl_give isl_map *embed_access(__isl_take isl_map *access, void *user)
2680 isl_map *map = (isl_map *) user;
2682 return isl_map_apply_domain(access, isl_map_copy(map));
2685 /* Apply "map" to all access relations in "expr".
2687 static struct pet_expr *embed(struct pet_expr *expr, __isl_keep isl_map *map)
2689 return pet_expr_foreach_access(expr, &embed_access, map);
2692 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
2694 static int n_nested_parameter(__isl_keep isl_set *set)
2696 isl_space *space;
2697 int n;
2699 space = isl_set_get_space(set);
2700 n = n_nested_parameter(space);
2701 isl_space_free(space);
2703 return n;
2706 /* Remove all parameters from "map" that refer to nested accesses.
2708 static __isl_give isl_map *remove_nested_parameters(__isl_take isl_map *map)
2710 int nparam;
2711 isl_space *space;
2713 space = isl_map_get_space(map);
2714 nparam = isl_space_dim(space, isl_dim_param);
2715 for (int i = nparam - 1; i >= 0; --i)
2716 if (is_nested_parameter(space, i))
2717 map = isl_map_project_out(map, isl_dim_param, i, 1);
2718 isl_space_free(space);
2720 return map;
2723 extern "C" {
2724 static __isl_give isl_map *access_remove_nested_parameters(
2725 __isl_take isl_map *access, void *user);
2728 static __isl_give isl_map *access_remove_nested_parameters(
2729 __isl_take isl_map *access, void *user)
2731 return remove_nested_parameters(access);
2734 /* Remove all nested access parameters from the schedule and all
2735 * accesses of "stmt".
2736 * There is no need to remove them from the domain as these parameters
2737 * have already been removed from the domain when this function is called.
2739 static struct pet_stmt *remove_nested_parameters(struct pet_stmt *stmt)
2741 if (!stmt)
2742 return NULL;
2743 stmt->schedule = remove_nested_parameters(stmt->schedule);
2744 stmt->body = pet_expr_foreach_access(stmt->body,
2745 &access_remove_nested_parameters, NULL);
2746 if (!stmt->schedule || !stmt->body)
2747 goto error;
2748 for (int i = 0; i < stmt->n_arg; ++i) {
2749 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2750 &access_remove_nested_parameters, NULL);
2751 if (!stmt->args[i])
2752 goto error;
2755 return stmt;
2756 error:
2757 pet_stmt_free(stmt);
2758 return NULL;
2761 /* For each nested access parameter in the domain of "stmt",
2762 * construct a corresponding pet_expr, place it in stmt->args and
2763 * record its position in "param2pos".
2764 * n is the number of nested access parameters.
2766 struct pet_stmt *PetScan::extract_nested(struct pet_stmt *stmt, int n,
2767 std::map<int,int> &param2pos)
2769 isl_space *space;
2770 unsigned n_arg;
2771 struct pet_expr **args;
2773 n_arg = stmt->n_arg;
2774 args = isl_realloc_array(ctx, stmt->args, struct pet_expr *, n_arg + n);
2775 if (!args)
2776 goto error;
2777 stmt->args = args;
2778 stmt->n_arg += n;
2780 space = isl_set_get_space(stmt->domain);
2781 n = extract_nested(space, n_arg, stmt->args, param2pos);
2782 isl_space_free(space);
2784 if (n < 0)
2785 goto error;
2787 stmt->n_arg = n;
2788 return stmt;
2789 error:
2790 pet_stmt_free(stmt);
2791 return NULL;
2794 /* Look for parameters in the iteration domain of "stmt" that
2795 * refer to nested accesses. In particular, these are
2796 * parameters with no name.
2798 * If there are any such parameters, then as many extra variables
2799 * (after identifying identical nested accesses) are added to the
2800 * range of the map wrapped inside the domain.
2801 * If the original domain is not a wrapped map, then a new wrapped
2802 * map is created with zero output dimensions.
2803 * The parameters are then equated to the corresponding output dimensions
2804 * and subsequently projected out, from the iteration domain,
2805 * the schedule and the access relations.
2806 * For each of the output dimensions, a corresponding argument
2807 * expression is added. Initially they are created with
2808 * a zero-dimensional domain, so they have to be embedded
2809 * in the current iteration domain.
2810 * param2pos maps the position of the parameter to the position
2811 * of the corresponding output dimension in the wrapped map.
2813 struct pet_stmt *PetScan::resolve_nested(struct pet_stmt *stmt)
2815 int n;
2816 int nparam;
2817 unsigned n_arg;
2818 isl_map *map;
2819 std::map<int,int> param2pos;
2821 if (!stmt)
2822 return NULL;
2824 n = n_nested_parameter(stmt->domain);
2825 if (n == 0)
2826 return stmt;
2828 n_arg = stmt->n_arg;
2829 stmt = extract_nested(stmt, n, param2pos);
2830 if (!stmt)
2831 return NULL;
2833 n = stmt->n_arg - n_arg;
2834 nparam = isl_set_dim(stmt->domain, isl_dim_param);
2835 if (isl_set_is_wrapping(stmt->domain))
2836 map = isl_set_unwrap(stmt->domain);
2837 else
2838 map = isl_map_from_domain(stmt->domain);
2839 map = isl_map_add_dims(map, isl_dim_out, n);
2841 for (int i = nparam - 1; i >= 0; --i) {
2842 isl_id *id;
2844 if (!is_nested_parameter(map, i))
2845 continue;
2847 id = isl_map_get_tuple_id(stmt->args[param2pos[i]]->acc.access,
2848 isl_dim_out);
2849 map = isl_map_set_dim_id(map, isl_dim_out, param2pos[i], id);
2850 map = isl_map_equate(map, isl_dim_param, i, isl_dim_out,
2851 param2pos[i]);
2852 map = isl_map_project_out(map, isl_dim_param, i, 1);
2855 stmt->domain = isl_map_wrap(map);
2857 map = isl_set_unwrap(isl_set_copy(stmt->domain));
2858 map = isl_map_from_range(isl_map_domain(map));
2859 for (int pos = n_arg; pos < stmt->n_arg; ++pos)
2860 stmt->args[pos] = embed(stmt->args[pos], map);
2861 isl_map_free(map);
2863 stmt = remove_nested_parameters(stmt);
2865 return stmt;
2866 error:
2867 pet_stmt_free(stmt);
2868 return NULL;
2871 /* For each statement in "scop", move the parameters that correspond
2872 * to nested access into the ranges of the domains and create
2873 * corresponding argument expressions.
2875 struct pet_scop *PetScan::resolve_nested(struct pet_scop *scop)
2877 if (!scop)
2878 return NULL;
2880 for (int i = 0; i < scop->n_stmt; ++i) {
2881 scop->stmts[i] = resolve_nested(scop->stmts[i]);
2882 if (!scop->stmts[i])
2883 goto error;
2886 return scop;
2887 error:
2888 pet_scop_free(scop);
2889 return NULL;
2892 /* Does "space" involve any parameters that refer to nested
2893 * accesses, i.e., parameters with no name?
2895 static bool has_nested(__isl_keep isl_space *space)
2897 int nparam;
2899 nparam = isl_space_dim(space, isl_dim_param);
2900 for (int i = 0; i < nparam; ++i)
2901 if (is_nested_parameter(space, i))
2902 return true;
2904 return false;
2907 /* Does "set" involve any parameters that refer to nested
2908 * accesses, i.e., parameters with no name?
2910 static bool has_nested(__isl_keep isl_set *set)
2912 isl_space *space;
2913 bool nested;
2915 space = isl_set_get_space(set);
2916 nested = has_nested(space);
2917 isl_space_free(space);
2919 return nested;
2922 /* Given an access expression "expr", is the variable accessed by
2923 * "expr" assigned anywhere inside "scop"?
2925 static bool is_assigned(pet_expr *expr, pet_scop *scop)
2927 bool assigned = false;
2928 isl_id *id;
2930 id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
2931 assigned = pet_scop_writes(scop, id);
2932 isl_id_free(id);
2934 return assigned;
2937 /* Are all nested access parameters in "set" allowed given "scop".
2938 * In particular, is none of them written by anywhere inside "scop".
2940 bool PetScan::is_nested_allowed(__isl_keep isl_set *set, pet_scop *scop)
2942 int nparam;
2944 nparam = isl_set_dim(set, isl_dim_param);
2945 for (int i = 0; i < nparam; ++i) {
2946 Expr *nested;
2947 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
2948 pet_expr *expr;
2949 bool allowed;
2951 if (!is_nested_parameter(id)) {
2952 isl_id_free(id);
2953 continue;
2956 nested = (Expr *) isl_id_get_user(id);
2957 expr = extract_expr(nested);
2958 allowed = expr && expr->type == pet_expr_access &&
2959 !is_assigned(expr, scop);
2961 pet_expr_free(expr);
2962 isl_id_free(id);
2964 if (!allowed)
2965 return false;
2968 return true;
2971 /* Construct a pet_scop for an if statement.
2973 * If the condition fits the pattern of a conditional assignment,
2974 * then it is handled by extract_conditional_assignment.
2975 * Otherwise, we do the following.
2977 * If the condition is affine, then the condition is added
2978 * to the iteration domains of the then branch, while the
2979 * opposite of the condition in added to the iteration domains
2980 * of the else branch, if any.
2981 * We allow the condition to be dynamic, i.e., to refer to
2982 * scalars or array elements that may be written to outside
2983 * of the given if statement. These nested accesses are then represented
2984 * as output dimensions in the wrapping iteration domain.
2985 * If it also written _inside_ the then or else branch, then
2986 * we treat the condition as non-affine.
2987 * As explained below, this will introduce an extra statement.
2988 * For aesthetic reasons, we want this statement to have a statement
2989 * number that is lower than those of the then and else branches.
2990 * In order to evaluate if will need such a statement, however, we
2991 * first construct scops for the then and else branches.
2992 * We therefore reserve a statement number if we might have to
2993 * introduce such an extra statement.
2995 * If the condition is not affine, then we create a separate
2996 * statement that writes the result of the condition to a virtual scalar.
2997 * A constraint requiring the value of this virtual scalar to be one
2998 * is added to the iteration domains of the then branch.
2999 * Similarly, a constraint requiring the value of this virtual scalar
3000 * to be zero is added to the iteration domains of the else branch, if any.
3001 * We adjust the schedules to ensure that the virtual scalar is written
3002 * before it is read.
3004 struct pet_scop *PetScan::extract(IfStmt *stmt)
3006 struct pet_scop *scop_then, *scop_else, *scop;
3007 assigned_value_cache cache(assigned_value);
3008 isl_map *test_access = NULL;
3009 isl_set *cond;
3010 int stmt_id;
3012 scop = extract_conditional_assignment(stmt);
3013 if (scop)
3014 return scop;
3016 cond = try_extract_nested_condition(stmt->getCond());
3017 if (allow_nested && (!cond || has_nested(cond)))
3018 stmt_id = n_stmt++;
3020 scop_then = extract(stmt->getThen());
3022 if (stmt->getElse()) {
3023 scop_else = extract(stmt->getElse());
3024 if (autodetect) {
3025 if (scop_then && !scop_else) {
3026 partial = true;
3027 isl_set_free(cond);
3028 return scop_then;
3030 if (!scop_then && scop_else) {
3031 partial = true;
3032 isl_set_free(cond);
3033 return scop_else;
3038 if (cond &&
3039 (!is_nested_allowed(cond, scop_then) ||
3040 (stmt->getElse() && !is_nested_allowed(cond, scop_else)))) {
3041 isl_set_free(cond);
3042 cond = NULL;
3044 if (allow_nested && !cond) {
3045 int save_n_stmt = n_stmt;
3046 test_access = create_test_access(ctx, n_test++);
3047 n_stmt = stmt_id;
3048 scop = extract_non_affine_condition(stmt->getCond(),
3049 isl_map_copy(test_access));
3050 n_stmt = save_n_stmt;
3051 scop = scop_add_array(scop, test_access, ast_context);
3052 if (!scop) {
3053 pet_scop_free(scop_then);
3054 pet_scop_free(scop_else);
3055 isl_map_free(test_access);
3056 return NULL;
3060 if (!scop) {
3061 if (!cond)
3062 cond = extract_condition(stmt->getCond());
3063 scop = pet_scop_restrict(scop_then, isl_set_copy(cond));
3065 if (stmt->getElse()) {
3066 cond = isl_set_complement(cond);
3067 scop_else = pet_scop_restrict(scop_else, cond);
3068 scop = pet_scop_add(ctx, scop, scop_else);
3069 } else
3070 isl_set_free(cond);
3071 scop = resolve_nested(scop);
3072 } else {
3073 scop = pet_scop_prefix(scop, 0);
3074 scop_then = pet_scop_prefix(scop_then, 1);
3075 scop_then = pet_scop_filter(scop_then,
3076 isl_map_copy(test_access), 1);
3077 scop = pet_scop_add(ctx, scop, scop_then);
3078 if (stmt->getElse()) {
3079 scop_else = pet_scop_prefix(scop_else, 1);
3080 scop_else = pet_scop_filter(scop_else, test_access, 0);
3081 scop = pet_scop_add(ctx, scop, scop_else);
3082 } else
3083 isl_map_free(test_access);
3086 return scop;
3089 /* Try and construct a pet_scop for a label statement.
3090 * We currently only allow labels on expression statements.
3092 struct pet_scop *PetScan::extract(LabelStmt *stmt)
3094 isl_id *label;
3095 Stmt *sub;
3097 sub = stmt->getSubStmt();
3098 if (!isa<Expr>(sub)) {
3099 unsupported(stmt);
3100 return NULL;
3103 label = isl_id_alloc(ctx, stmt->getName(), NULL);
3105 return extract(sub, extract_expr(cast<Expr>(sub)), label);
3108 /* Try and construct a pet_scop corresponding to "stmt".
3110 struct pet_scop *PetScan::extract(Stmt *stmt)
3112 if (isa<Expr>(stmt))
3113 return extract(stmt, extract_expr(cast<Expr>(stmt)));
3115 switch (stmt->getStmtClass()) {
3116 case Stmt::WhileStmtClass:
3117 return extract(cast<WhileStmt>(stmt));
3118 case Stmt::ForStmtClass:
3119 return extract_for(cast<ForStmt>(stmt));
3120 case Stmt::IfStmtClass:
3121 return extract(cast<IfStmt>(stmt));
3122 case Stmt::CompoundStmtClass:
3123 return extract(cast<CompoundStmt>(stmt));
3124 case Stmt::LabelStmtClass:
3125 return extract(cast<LabelStmt>(stmt));
3126 default:
3127 unsupported(stmt);
3130 return NULL;
3133 /* Try and construct a pet_scop corresponding to (part of)
3134 * a sequence of statements.
3136 struct pet_scop *PetScan::extract(StmtRange stmt_range)
3138 pet_scop *scop;
3139 StmtIterator i;
3140 int j;
3141 bool partial_range = false;
3143 scop = pet_scop_empty(ctx);
3144 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j) {
3145 Stmt *child = *i;
3146 struct pet_scop *scop_i;
3147 scop_i = extract(child);
3148 if (scop && partial) {
3149 pet_scop_free(scop_i);
3150 break;
3152 scop_i = pet_scop_prefix(scop_i, j);
3153 if (autodetect) {
3154 if (scop_i)
3155 scop = pet_scop_add(ctx, scop, scop_i);
3156 else
3157 partial_range = true;
3158 if (scop->n_stmt != 0 && !scop_i)
3159 partial = true;
3160 } else {
3161 scop = pet_scop_add(ctx, scop, scop_i);
3163 if (partial)
3164 break;
3167 if (scop && partial_range)
3168 partial = true;
3170 return scop;
3173 /* Check if the scop marked by the user is exactly this Stmt
3174 * or part of this Stmt.
3175 * If so, return a pet_scop corresponding to the marked region.
3176 * Otherwise, return NULL.
3178 struct pet_scop *PetScan::scan(Stmt *stmt)
3180 SourceManager &SM = PP.getSourceManager();
3181 unsigned start_off, end_off;
3183 start_off = SM.getFileOffset(stmt->getLocStart());
3184 end_off = SM.getFileOffset(stmt->getLocEnd());
3186 if (start_off > loc.end)
3187 return NULL;
3188 if (end_off < loc.start)
3189 return NULL;
3190 if (start_off >= loc.start && end_off <= loc.end) {
3191 return extract(stmt);
3194 StmtIterator start;
3195 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
3196 Stmt *child = *start;
3197 if (!child)
3198 continue;
3199 start_off = SM.getFileOffset(child->getLocStart());
3200 end_off = SM.getFileOffset(child->getLocEnd());
3201 if (start_off < loc.start && end_off > loc.end)
3202 return scan(child);
3203 if (start_off >= loc.start)
3204 break;
3207 StmtIterator end;
3208 for (end = start; end != stmt->child_end(); ++end) {
3209 Stmt *child = *end;
3210 start_off = SM.getFileOffset(child->getLocStart());
3211 if (start_off >= loc.end)
3212 break;
3215 return extract(StmtRange(start, end));
3218 /* Set the size of index "pos" of "array" to "size".
3219 * In particular, add a constraint of the form
3221 * i_pos < size
3223 * to array->extent and a constraint of the form
3225 * size >= 0
3227 * to array->context.
3229 static struct pet_array *update_size(struct pet_array *array, int pos,
3230 __isl_take isl_pw_aff *size)
3232 isl_set *valid;
3233 isl_set *univ;
3234 isl_set *bound;
3235 isl_space *dim;
3236 isl_aff *aff;
3237 isl_pw_aff *index;
3238 isl_id *id;
3240 valid = isl_pw_aff_nonneg_set(isl_pw_aff_copy(size));
3241 array->context = isl_set_intersect(array->context, valid);
3243 dim = isl_set_get_space(array->extent);
3244 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
3245 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
3246 univ = isl_set_universe(isl_aff_get_domain_space(aff));
3247 index = isl_pw_aff_alloc(univ, aff);
3249 size = isl_pw_aff_add_dims(size, isl_dim_in,
3250 isl_set_dim(array->extent, isl_dim_set));
3251 id = isl_set_get_tuple_id(array->extent);
3252 size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
3253 bound = isl_pw_aff_lt_set(index, size);
3255 array->extent = isl_set_intersect(array->extent, bound);
3257 if (!array->context || !array->extent)
3258 goto error;
3260 return array;
3261 error:
3262 pet_array_free(array);
3263 return NULL;
3266 /* Figure out the size of the array at position "pos" and all
3267 * subsequent positions from "type" and update "array" accordingly.
3269 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
3270 const Type *type, int pos)
3272 const ArrayType *atype;
3273 isl_pw_aff *size;
3275 if (!array)
3276 return NULL;
3278 if (type->isPointerType()) {
3279 type = type->getPointeeType().getTypePtr();
3280 return set_upper_bounds(array, type, pos + 1);
3282 if (!type->isArrayType())
3283 return array;
3285 type = type->getCanonicalTypeInternal().getTypePtr();
3286 atype = cast<ArrayType>(type);
3288 if (type->isConstantArrayType()) {
3289 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
3290 size = extract_affine(ca->getSize());
3291 array = update_size(array, pos, size);
3292 } else if (type->isVariableArrayType()) {
3293 const VariableArrayType *vla = cast<VariableArrayType>(atype);
3294 size = extract_affine(vla->getSizeExpr());
3295 array = update_size(array, pos, size);
3298 type = atype->getElementType().getTypePtr();
3300 return set_upper_bounds(array, type, pos + 1);
3303 /* Construct and return a pet_array corresponding to the variable "decl".
3304 * In particular, initialize array->extent to
3306 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
3308 * and then call set_upper_bounds to set the upper bounds on the indices
3309 * based on the type of the variable.
3311 struct pet_array *PetScan::extract_array(isl_ctx *ctx, ValueDecl *decl)
3313 struct pet_array *array;
3314 QualType qt = decl->getType();
3315 const Type *type = qt.getTypePtr();
3316 int depth = array_depth(type);
3317 QualType base = base_type(qt);
3318 string name;
3319 isl_id *id;
3320 isl_space *dim;
3322 array = isl_calloc_type(ctx, struct pet_array);
3323 if (!array)
3324 return NULL;
3326 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
3327 dim = isl_space_set_alloc(ctx, 0, depth);
3328 dim = isl_space_set_tuple_id(dim, isl_dim_set, id);
3330 array->extent = isl_set_nat_universe(dim);
3332 dim = isl_space_params_alloc(ctx, 0);
3333 array->context = isl_set_universe(dim);
3335 array = set_upper_bounds(array, type, 0);
3336 if (!array)
3337 return NULL;
3339 name = base.getAsString();
3340 array->element_type = strdup(name.c_str());
3341 array->element_size = decl->getASTContext().getTypeInfo(base).first / 8;
3343 return array;
3346 /* Construct a list of pet_arrays, one for each array (or scalar)
3347 * accessed inside "scop" add this list to "scop" and return the result.
3349 * The context of "scop" is updated with the intesection of
3350 * the contexts of all arrays, i.e., constraints on the parameters
3351 * that ensure that the arrays have a valid (non-negative) size.
3353 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop)
3355 int i;
3356 set<ValueDecl *> arrays;
3357 set<ValueDecl *>::iterator it;
3358 int n_array;
3359 struct pet_array **scop_arrays;
3361 if (!scop)
3362 return NULL;
3364 pet_scop_collect_arrays(scop, arrays);
3365 if (arrays.size() == 0)
3366 return scop;
3368 n_array = scop->n_array;
3370 scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3371 n_array + arrays.size());
3372 if (!scop_arrays)
3373 goto error;
3374 scop->arrays = scop_arrays;
3376 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
3377 struct pet_array *array;
3378 scop->arrays[n_array + i] = array = extract_array(ctx, *it);
3379 if (!scop->arrays[n_array + i])
3380 goto error;
3381 scop->n_array++;
3382 scop->context = isl_set_intersect(scop->context,
3383 isl_set_copy(array->context));
3384 if (!scop->context)
3385 goto error;
3388 return scop;
3389 error:
3390 pet_scop_free(scop);
3391 return NULL;
3394 /* Bound all parameters in scop->context to the possible values
3395 * of the corresponding C variable.
3397 static struct pet_scop *add_parameter_bounds(struct pet_scop *scop)
3399 int n;
3401 if (!scop)
3402 return NULL;
3404 n = isl_set_dim(scop->context, isl_dim_param);
3405 for (int i = 0; i < n; ++i) {
3406 isl_id *id;
3407 ValueDecl *decl;
3409 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
3410 decl = (ValueDecl *) isl_id_get_user(id);
3411 isl_id_free(id);
3413 scop->context = set_parameter_bounds(scop->context, i, decl);
3415 if (!scop->context)
3416 goto error;
3419 return scop;
3420 error:
3421 pet_scop_free(scop);
3422 return NULL;
3425 /* Construct a pet_scop from the given function.
3427 struct pet_scop *PetScan::scan(FunctionDecl *fd)
3429 pet_scop *scop;
3430 Stmt *stmt;
3432 stmt = fd->getBody();
3434 if (autodetect)
3435 scop = extract(stmt);
3436 else
3437 scop = scan(stmt);
3438 scop = pet_scop_detect_parameter_accesses(scop);
3439 scop = scan_arrays(scop);
3440 scop = add_parameter_bounds(scop);
3441 scop = pet_scop_gist(scop, value_bounds);
3443 return scop;