pet_array_free: return NULL pointer of type struct pet_array *
[pet.git] / scan.cc
blob32160b5e45091cfac8eec4db99277ed896354bc7
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/ASTContext.h>
39 #include <clang/AST/ASTDiagnostic.h>
40 #include <clang/AST/Expr.h>
41 #include <clang/AST/RecursiveASTVisitor.h>
43 #include <isl/id.h>
44 #include <isl/space.h>
45 #include <isl/aff.h>
46 #include <isl/set.h>
48 #include "options.h"
49 #include "scan.h"
50 #include "scop.h"
51 #include "scop_plus.h"
53 #include "config.h"
55 using namespace std;
56 using namespace clang;
58 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
59 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
61 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
62 SourceLocation(), var, false, var->getInnerLocStart(),
63 var->getType(), VK_LValue);
65 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
66 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
68 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
69 SourceLocation(), var, var->getInnerLocStart(), var->getType(),
70 VK_LValue);
72 #else
73 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
75 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
76 var, var->getInnerLocStart(), var->getType(), VK_LValue);
78 #endif
80 /* Check if the element type corresponding to the given array type
81 * has a const qualifier.
83 static bool const_base(QualType qt)
85 const Type *type = qt.getTypePtr();
87 if (type->isPointerType())
88 return const_base(type->getPointeeType());
89 if (type->isArrayType()) {
90 const ArrayType *atype;
91 type = type->getCanonicalTypeInternal().getTypePtr();
92 atype = cast<ArrayType>(type);
93 return const_base(atype->getElementType());
96 return qt.isConstQualified();
99 /* Mark "decl" as having an unknown value in "assigned_value".
101 * If no (known or unknown) value was assigned to "decl" before,
102 * then it may have been treated as a parameter before and may
103 * therefore appear in a value assigned to another variable.
104 * If so, this assignment needs to be turned into an unknown value too.
106 static void clear_assignment(map<ValueDecl *, isl_pw_aff *> &assigned_value,
107 ValueDecl *decl)
109 map<ValueDecl *, isl_pw_aff *>::iterator it;
111 it = assigned_value.find(decl);
113 assigned_value[decl] = NULL;
115 if (it == assigned_value.end())
116 return;
118 for (it = assigned_value.begin(); it != assigned_value.end(); ++it) {
119 isl_pw_aff *pa = it->second;
120 int nparam = isl_pw_aff_dim(pa, isl_dim_param);
122 for (int i = 0; i < nparam; ++i) {
123 isl_id *id;
125 if (!isl_pw_aff_has_dim_id(pa, isl_dim_param, i))
126 continue;
127 id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
128 if (isl_id_get_user(id) == decl)
129 it->second = NULL;
130 isl_id_free(id);
135 /* Look for any assignments to scalar variables in part of the parse
136 * tree and set assigned_value to NULL for each of them.
137 * Also reset assigned_value if the address of a scalar variable
138 * is being taken. As an exception, if the address is passed to a function
139 * that is declared to receive a const pointer, then assigned_value is
140 * not reset.
142 * This ensures that we won't use any previously stored value
143 * in the current subtree and its parents.
145 struct clear_assignments : RecursiveASTVisitor<clear_assignments> {
146 map<ValueDecl *, isl_pw_aff *> &assigned_value;
147 set<UnaryOperator *> skip;
149 clear_assignments(map<ValueDecl *, isl_pw_aff *> &assigned_value) :
150 assigned_value(assigned_value) {}
152 /* Check for "address of" operators whose value is passed
153 * to a const pointer argument and add them to "skip", so that
154 * we can skip them in VisitUnaryOperator.
156 bool VisitCallExpr(CallExpr *expr) {
157 FunctionDecl *fd;
158 fd = expr->getDirectCallee();
159 if (!fd)
160 return true;
161 for (int i = 0; i < expr->getNumArgs(); ++i) {
162 Expr *arg = expr->getArg(i);
163 UnaryOperator *op;
164 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
165 ImplicitCastExpr *ice;
166 ice = cast<ImplicitCastExpr>(arg);
167 arg = ice->getSubExpr();
169 if (arg->getStmtClass() != Stmt::UnaryOperatorClass)
170 continue;
171 op = cast<UnaryOperator>(arg);
172 if (op->getOpcode() != UO_AddrOf)
173 continue;
174 if (const_base(fd->getParamDecl(i)->getType()))
175 skip.insert(op);
177 return true;
180 bool VisitUnaryOperator(UnaryOperator *expr) {
181 Expr *arg;
182 DeclRefExpr *ref;
183 ValueDecl *decl;
185 switch (expr->getOpcode()) {
186 case UO_AddrOf:
187 case UO_PostInc:
188 case UO_PostDec:
189 case UO_PreInc:
190 case UO_PreDec:
191 break;
192 default:
193 return true;
195 if (skip.find(expr) != skip.end())
196 return true;
198 arg = expr->getSubExpr();
199 if (arg->getStmtClass() != Stmt::DeclRefExprClass)
200 return true;
201 ref = cast<DeclRefExpr>(arg);
202 decl = ref->getDecl();
203 clear_assignment(assigned_value, decl);
204 return true;
207 bool VisitBinaryOperator(BinaryOperator *expr) {
208 Expr *lhs;
209 DeclRefExpr *ref;
210 ValueDecl *decl;
212 if (!expr->isAssignmentOp())
213 return true;
214 lhs = expr->getLHS();
215 if (lhs->getStmtClass() != Stmt::DeclRefExprClass)
216 return true;
217 ref = cast<DeclRefExpr>(lhs);
218 decl = ref->getDecl();
219 clear_assignment(assigned_value, decl);
220 return true;
224 /* Keep a copy of the currently assigned values.
226 * Any variable that is assigned a value inside the current scope
227 * is removed again when we leave the scope (either because it wasn't
228 * stored in the cache or because it has a different value in the cache).
230 struct assigned_value_cache {
231 map<ValueDecl *, isl_pw_aff *> &assigned_value;
232 map<ValueDecl *, isl_pw_aff *> cache;
234 assigned_value_cache(map<ValueDecl *, isl_pw_aff *> &assigned_value) :
235 assigned_value(assigned_value), cache(assigned_value) {}
236 ~assigned_value_cache() {
237 map<ValueDecl *, isl_pw_aff *>::iterator it = cache.begin();
238 for (it = assigned_value.begin(); it != assigned_value.end();
239 ++it) {
240 if (!it->second ||
241 (cache.find(it->first) != cache.end() &&
242 cache[it->first] != it->second))
243 cache[it->first] = NULL;
245 assigned_value = cache;
249 /* Insert an expression into the collection of expressions,
250 * provided it is not already in there.
251 * The isl_pw_affs are freed in the destructor.
253 void PetScan::insert_expression(__isl_take isl_pw_aff *expr)
255 std::set<isl_pw_aff *>::iterator it;
257 if (expressions.find(expr) == expressions.end())
258 expressions.insert(expr);
259 else
260 isl_pw_aff_free(expr);
263 PetScan::~PetScan()
265 std::set<isl_pw_aff *>::iterator it;
267 for (it = expressions.begin(); it != expressions.end(); ++it)
268 isl_pw_aff_free(*it);
270 isl_union_map_free(value_bounds);
273 /* Called if we found something we (currently) cannot handle.
274 * We'll provide more informative warnings later.
276 * We only actually complain if autodetect is false.
278 void PetScan::unsupported(Stmt *stmt, const char *msg)
280 if (options->autodetect)
281 return;
283 SourceLocation loc = stmt->getLocStart();
284 DiagnosticsEngine &diag = PP.getDiagnostics();
285 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
286 msg ? msg : "unsupported");
287 DiagnosticBuilder B = diag.Report(loc, id) << stmt->getSourceRange();
290 /* Extract an integer from "expr" and store it in "v".
292 int PetScan::extract_int(IntegerLiteral *expr, isl_int *v)
294 const Type *type = expr->getType().getTypePtr();
295 int is_signed = type->hasSignedIntegerRepresentation();
297 if (is_signed) {
298 int64_t i = expr->getValue().getSExtValue();
299 isl_int_set_si(*v, i);
300 } else {
301 uint64_t i = expr->getValue().getZExtValue();
302 isl_int_set_ui(*v, i);
305 return 0;
308 /* Extract an integer from "expr" and store it in "v".
309 * Return -1 if "expr" does not (obviously) represent an integer.
311 int PetScan::extract_int(clang::ParenExpr *expr, isl_int *v)
313 return extract_int(expr->getSubExpr(), v);
316 /* Extract an integer from "expr" and store it in "v".
317 * Return -1 if "expr" does not (obviously) represent an integer.
319 int PetScan::extract_int(clang::Expr *expr, isl_int *v)
321 if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
322 return extract_int(cast<IntegerLiteral>(expr), v);
323 if (expr->getStmtClass() == Stmt::ParenExprClass)
324 return extract_int(cast<ParenExpr>(expr), v);
326 unsupported(expr);
327 return -1;
330 /* Extract an affine expression from the IntegerLiteral "expr".
332 __isl_give isl_pw_aff *PetScan::extract_affine(IntegerLiteral *expr)
334 isl_space *dim = isl_space_params_alloc(ctx, 0);
335 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
336 isl_aff *aff = isl_aff_zero_on_domain(ls);
337 isl_set *dom = isl_set_universe(dim);
338 isl_int v;
340 isl_int_init(v);
341 extract_int(expr, &v);
342 aff = isl_aff_add_constant(aff, v);
343 isl_int_clear(v);
345 return isl_pw_aff_alloc(dom, aff);
348 /* Extract an affine expression from the APInt "val".
350 __isl_give isl_pw_aff *PetScan::extract_affine(const llvm::APInt &val)
352 isl_space *dim = isl_space_params_alloc(ctx, 0);
353 isl_local_space *ls = isl_local_space_from_space(isl_space_copy(dim));
354 isl_aff *aff = isl_aff_zero_on_domain(ls);
355 isl_set *dom = isl_set_universe(dim);
356 isl_int v;
358 isl_int_init(v);
359 isl_int_set_ui(v, val.getZExtValue());
360 aff = isl_aff_add_constant(aff, v);
361 isl_int_clear(v);
363 return isl_pw_aff_alloc(dom, aff);
366 __isl_give isl_pw_aff *PetScan::extract_affine(ImplicitCastExpr *expr)
368 return extract_affine(expr->getSubExpr());
371 static unsigned get_type_size(ValueDecl *decl)
373 return decl->getASTContext().getIntWidth(decl->getType());
376 /* Bound parameter "pos" of "set" to the possible values of "decl".
378 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
379 unsigned pos, ValueDecl *decl)
381 unsigned width;
382 isl_int v;
384 isl_int_init(v);
386 width = get_type_size(decl);
387 if (decl->getType()->isUnsignedIntegerType()) {
388 set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
389 isl_int_set_si(v, 1);
390 isl_int_mul_2exp(v, v, width);
391 isl_int_sub_ui(v, v, 1);
392 set = isl_set_upper_bound(set, isl_dim_param, pos, v);
393 } else {
394 isl_int_set_si(v, 1);
395 isl_int_mul_2exp(v, v, width - 1);
396 isl_int_sub_ui(v, v, 1);
397 set = isl_set_upper_bound(set, isl_dim_param, pos, v);
398 isl_int_neg(v, v);
399 isl_int_sub_ui(v, v, 1);
400 set = isl_set_lower_bound(set, isl_dim_param, pos, v);
403 isl_int_clear(v);
405 return set;
408 /* Extract an affine expression from the DeclRefExpr "expr".
410 * If the variable has been assigned a value, then we check whether
411 * we know what (affine) value was assigned.
412 * If so, we return this value. Otherwise we convert "expr"
413 * to an extra parameter (provided nesting_enabled is set).
415 * Otherwise, we simply return an expression that is equal
416 * to a parameter corresponding to the referenced variable.
418 __isl_give isl_pw_aff *PetScan::extract_affine(DeclRefExpr *expr)
420 ValueDecl *decl = expr->getDecl();
421 const Type *type = decl->getType().getTypePtr();
422 isl_id *id;
423 isl_space *dim;
424 isl_aff *aff;
425 isl_set *dom;
427 if (!type->isIntegerType()) {
428 unsupported(expr);
429 return NULL;
432 if (assigned_value.find(decl) != assigned_value.end()) {
433 if (assigned_value[decl])
434 return isl_pw_aff_copy(assigned_value[decl]);
435 else
436 return nested_access(expr);
439 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
440 dim = isl_space_params_alloc(ctx, 1);
442 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
444 dom = isl_set_universe(isl_space_copy(dim));
445 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
446 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
448 return isl_pw_aff_alloc(dom, aff);
451 /* Extract an affine expression from an integer division operation.
452 * In particular, if "expr" is lhs/rhs, then return
454 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
456 * The second argument (rhs) is required to be a (positive) integer constant.
458 __isl_give isl_pw_aff *PetScan::extract_affine_div(BinaryOperator *expr)
460 int is_cst;
461 isl_pw_aff *rhs, *lhs;
463 rhs = extract_affine(expr->getRHS());
464 is_cst = isl_pw_aff_is_cst(rhs);
465 if (is_cst < 0 || !is_cst) {
466 isl_pw_aff_free(rhs);
467 if (!is_cst)
468 unsupported(expr);
469 return NULL;
472 lhs = extract_affine(expr->getLHS());
474 return isl_pw_aff_tdiv_q(lhs, rhs);
477 /* Extract an affine expression from a modulo operation.
478 * In particular, if "expr" is lhs/rhs, then return
480 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
482 * The second argument (rhs) is required to be a (positive) integer constant.
484 __isl_give isl_pw_aff *PetScan::extract_affine_mod(BinaryOperator *expr)
486 int is_cst;
487 isl_pw_aff *rhs, *lhs;
489 rhs = extract_affine(expr->getRHS());
490 is_cst = isl_pw_aff_is_cst(rhs);
491 if (is_cst < 0 || !is_cst) {
492 isl_pw_aff_free(rhs);
493 if (!is_cst)
494 unsupported(expr);
495 return NULL;
498 lhs = extract_affine(expr->getLHS());
500 return isl_pw_aff_tdiv_r(lhs, rhs);
503 /* Extract an affine expression from a multiplication operation.
504 * This is only allowed if at least one of the two arguments
505 * is a (piecewise) constant.
507 __isl_give isl_pw_aff *PetScan::extract_affine_mul(BinaryOperator *expr)
509 isl_pw_aff *lhs;
510 isl_pw_aff *rhs;
512 lhs = extract_affine(expr->getLHS());
513 rhs = extract_affine(expr->getRHS());
515 if (!isl_pw_aff_is_cst(lhs) && !isl_pw_aff_is_cst(rhs)) {
516 isl_pw_aff_free(lhs);
517 isl_pw_aff_free(rhs);
518 unsupported(expr);
519 return NULL;
522 return isl_pw_aff_mul(lhs, rhs);
525 /* Extract an affine expression from an addition or subtraction operation.
527 __isl_give isl_pw_aff *PetScan::extract_affine_add(BinaryOperator *expr)
529 isl_pw_aff *lhs;
530 isl_pw_aff *rhs;
532 lhs = extract_affine(expr->getLHS());
533 rhs = extract_affine(expr->getRHS());
535 switch (expr->getOpcode()) {
536 case BO_Add:
537 return isl_pw_aff_add(lhs, rhs);
538 case BO_Sub:
539 return isl_pw_aff_sub(lhs, rhs);
540 default:
541 isl_pw_aff_free(lhs);
542 isl_pw_aff_free(rhs);
543 return NULL;
548 /* Compute
550 * pwaff mod 2^width
552 static __isl_give isl_pw_aff *wrap(__isl_take isl_pw_aff *pwaff,
553 unsigned width)
555 isl_int mod;
557 isl_int_init(mod);
558 isl_int_set_si(mod, 1);
559 isl_int_mul_2exp(mod, mod, width);
561 pwaff = isl_pw_aff_mod(pwaff, mod);
563 isl_int_clear(mod);
565 return pwaff;
568 /* Limit the domain of "pwaff" to those elements where the function
569 * value satisfies
571 * 2^{width-1} <= pwaff < 2^{width-1}
573 static __isl_give isl_pw_aff *avoid_overflow(__isl_take isl_pw_aff *pwaff,
574 unsigned width)
576 isl_int v;
577 isl_space *space = isl_pw_aff_get_domain_space(pwaff);
578 isl_local_space *ls = isl_local_space_from_space(space);
579 isl_aff *bound;
580 isl_set *dom;
581 isl_pw_aff *b;
583 isl_int_init(v);
584 isl_int_set_si(v, 1);
585 isl_int_mul_2exp(v, v, width - 1);
587 bound = isl_aff_zero_on_domain(ls);
588 bound = isl_aff_add_constant(bound, v);
589 b = isl_pw_aff_from_aff(bound);
591 dom = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff), isl_pw_aff_copy(b));
592 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
594 b = isl_pw_aff_neg(b);
595 dom = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff), b);
596 pwaff = isl_pw_aff_intersect_domain(pwaff, dom);
598 isl_int_clear(v);
600 return pwaff;
603 /* Handle potential overflows on signed computations.
605 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
606 * the we adjust the domain of "pa" to avoid overflows.
608 __isl_give isl_pw_aff *PetScan::signed_overflow(__isl_take isl_pw_aff *pa,
609 unsigned width)
611 if (options->signed_overflow == PET_OVERFLOW_AVOID)
612 pa = avoid_overflow(pa, width);
614 return pa;
617 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
619 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
620 __isl_take isl_set *dom)
622 isl_pw_aff *pa;
623 pa = isl_set_indicator_function(set);
624 pa = isl_pw_aff_intersect_domain(pa, dom);
625 return pa;
628 /* Extract an affine expression from some binary operations.
629 * If the result of the expression is unsigned, then we wrap it
630 * based on the size of the type. Otherwise, we ensure that
631 * no overflow occurs.
633 __isl_give isl_pw_aff *PetScan::extract_affine(BinaryOperator *expr)
635 isl_pw_aff *res;
636 unsigned width;
638 switch (expr->getOpcode()) {
639 case BO_Add:
640 case BO_Sub:
641 res = extract_affine_add(expr);
642 break;
643 case BO_Div:
644 res = extract_affine_div(expr);
645 break;
646 case BO_Rem:
647 res = extract_affine_mod(expr);
648 break;
649 case BO_Mul:
650 res = extract_affine_mul(expr);
651 break;
652 case BO_LT:
653 case BO_LE:
654 case BO_GT:
655 case BO_GE:
656 case BO_EQ:
657 case BO_NE:
658 case BO_LAnd:
659 case BO_LOr:
660 return extract_condition(expr);
661 default:
662 unsupported(expr);
663 return NULL;
666 width = ast_context.getIntWidth(expr->getType());
667 if (expr->getType()->isUnsignedIntegerType())
668 res = wrap(res, width);
669 else
670 res = signed_overflow(res, width);
672 return res;
675 /* Extract an affine expression from a negation operation.
677 __isl_give isl_pw_aff *PetScan::extract_affine(UnaryOperator *expr)
679 if (expr->getOpcode() == UO_Minus)
680 return isl_pw_aff_neg(extract_affine(expr->getSubExpr()));
681 if (expr->getOpcode() == UO_LNot)
682 return extract_condition(expr);
684 unsupported(expr);
685 return NULL;
688 __isl_give isl_pw_aff *PetScan::extract_affine(ParenExpr *expr)
690 return extract_affine(expr->getSubExpr());
693 /* Extract an affine expression from some special function calls.
694 * In particular, we handle "min", "max", "ceild" and "floord".
695 * In case of the latter two, the second argument needs to be
696 * a (positive) integer constant.
698 __isl_give isl_pw_aff *PetScan::extract_affine(CallExpr *expr)
700 FunctionDecl *fd;
701 string name;
702 isl_pw_aff *aff1, *aff2;
704 fd = expr->getDirectCallee();
705 if (!fd) {
706 unsupported(expr);
707 return NULL;
710 name = fd->getDeclName().getAsString();
711 if (!(expr->getNumArgs() == 2 && name == "min") &&
712 !(expr->getNumArgs() == 2 && name == "max") &&
713 !(expr->getNumArgs() == 2 && name == "floord") &&
714 !(expr->getNumArgs() == 2 && name == "ceild")) {
715 unsupported(expr);
716 return NULL;
719 if (name == "min" || name == "max") {
720 aff1 = extract_affine(expr->getArg(0));
721 aff2 = extract_affine(expr->getArg(1));
723 if (name == "min")
724 aff1 = isl_pw_aff_min(aff1, aff2);
725 else
726 aff1 = isl_pw_aff_max(aff1, aff2);
727 } else if (name == "floord" || name == "ceild") {
728 isl_int v;
729 Expr *arg2 = expr->getArg(1);
731 if (arg2->getStmtClass() != Stmt::IntegerLiteralClass) {
732 unsupported(expr);
733 return NULL;
735 aff1 = extract_affine(expr->getArg(0));
736 isl_int_init(v);
737 extract_int(cast<IntegerLiteral>(arg2), &v);
738 aff1 = isl_pw_aff_scale_down(aff1, v);
739 isl_int_clear(v);
740 if (name == "floord")
741 aff1 = isl_pw_aff_floor(aff1);
742 else
743 aff1 = isl_pw_aff_ceil(aff1);
744 } else {
745 unsupported(expr);
746 return NULL;
749 return aff1;
752 /* This method is called when we come across an access that is
753 * nested in what is supposed to be an affine expression.
754 * If nesting is allowed, we return a new parameter that corresponds
755 * to this nested access. Otherwise, we simply complain.
757 * Note that we currently don't allow nested accesses themselves
758 * to contain any nested accesses, so we check if we can extract
759 * the access without any nesting and complain if we can't.
761 * The new parameter is resolved in resolve_nested.
763 isl_pw_aff *PetScan::nested_access(Expr *expr)
765 isl_id *id;
766 isl_space *dim;
767 isl_aff *aff;
768 isl_set *dom;
769 isl_map *access;
771 if (!nesting_enabled) {
772 unsupported(expr);
773 return NULL;
776 allow_nested = false;
777 access = extract_access(expr);
778 allow_nested = true;
779 if (!access) {
780 unsupported(expr);
781 return NULL;
783 isl_map_free(access);
785 id = isl_id_alloc(ctx, NULL, expr);
786 dim = isl_space_params_alloc(ctx, 1);
788 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
790 dom = isl_set_universe(isl_space_copy(dim));
791 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
792 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
794 return isl_pw_aff_alloc(dom, aff);
797 /* Affine expressions are not supposed to contain array accesses,
798 * but if nesting is allowed, we return a parameter corresponding
799 * to the array access.
801 __isl_give isl_pw_aff *PetScan::extract_affine(ArraySubscriptExpr *expr)
803 return nested_access(expr);
806 /* Extract an affine expression from a conditional operation.
808 __isl_give isl_pw_aff *PetScan::extract_affine(ConditionalOperator *expr)
810 isl_pw_aff *cond, *lhs, *rhs, *res;
812 cond = extract_condition(expr->getCond());
813 lhs = extract_affine(expr->getTrueExpr());
814 rhs = extract_affine(expr->getFalseExpr());
816 return isl_pw_aff_cond(cond, lhs, rhs);
819 /* Extract an affine expression, if possible, from "expr".
820 * Otherwise return NULL.
822 __isl_give isl_pw_aff *PetScan::extract_affine(Expr *expr)
824 switch (expr->getStmtClass()) {
825 case Stmt::ImplicitCastExprClass:
826 return extract_affine(cast<ImplicitCastExpr>(expr));
827 case Stmt::IntegerLiteralClass:
828 return extract_affine(cast<IntegerLiteral>(expr));
829 case Stmt::DeclRefExprClass:
830 return extract_affine(cast<DeclRefExpr>(expr));
831 case Stmt::BinaryOperatorClass:
832 return extract_affine(cast<BinaryOperator>(expr));
833 case Stmt::UnaryOperatorClass:
834 return extract_affine(cast<UnaryOperator>(expr));
835 case Stmt::ParenExprClass:
836 return extract_affine(cast<ParenExpr>(expr));
837 case Stmt::CallExprClass:
838 return extract_affine(cast<CallExpr>(expr));
839 case Stmt::ArraySubscriptExprClass:
840 return extract_affine(cast<ArraySubscriptExpr>(expr));
841 case Stmt::ConditionalOperatorClass:
842 return extract_affine(cast<ConditionalOperator>(expr));
843 default:
844 unsupported(expr);
846 return NULL;
849 __isl_give isl_map *PetScan::extract_access(ImplicitCastExpr *expr)
851 return extract_access(expr->getSubExpr());
854 /* Return the depth of an array of the given type.
856 static int array_depth(const Type *type)
858 if (type->isPointerType())
859 return 1 + array_depth(type->getPointeeType().getTypePtr());
860 if (type->isArrayType()) {
861 const ArrayType *atype;
862 type = type->getCanonicalTypeInternal().getTypePtr();
863 atype = cast<ArrayType>(type);
864 return 1 + array_depth(atype->getElementType().getTypePtr());
866 return 0;
869 /* Return the element type of the given array type.
871 static QualType base_type(QualType qt)
873 const Type *type = qt.getTypePtr();
875 if (type->isPointerType())
876 return base_type(type->getPointeeType());
877 if (type->isArrayType()) {
878 const ArrayType *atype;
879 type = type->getCanonicalTypeInternal().getTypePtr();
880 atype = cast<ArrayType>(type);
881 return base_type(atype->getElementType());
883 return qt;
886 /* Extract an access relation from a reference to a variable.
887 * If the variable has name "A" and its type corresponds to an
888 * array of depth d, then the returned access relation is of the
889 * form
891 * { [] -> A[i_1,...,i_d] }
893 __isl_give isl_map *PetScan::extract_access(DeclRefExpr *expr)
895 ValueDecl *decl = expr->getDecl();
896 int depth = array_depth(decl->getType().getTypePtr());
897 isl_id *id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
898 isl_space *dim = isl_space_alloc(ctx, 0, 0, depth);
899 isl_map *access_rel;
901 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
903 access_rel = isl_map_universe(dim);
905 return access_rel;
908 /* Extract an access relation from an integer contant.
909 * If the value of the constant is "v", then the returned access relation
910 * is
912 * { [] -> [v] }
914 __isl_give isl_map *PetScan::extract_access(IntegerLiteral *expr)
916 return isl_map_from_range(isl_set_from_pw_aff(extract_affine(expr)));
919 /* Try and extract an access relation from the given Expr.
920 * Return NULL if it doesn't work out.
922 __isl_give isl_map *PetScan::extract_access(Expr *expr)
924 switch (expr->getStmtClass()) {
925 case Stmt::ImplicitCastExprClass:
926 return extract_access(cast<ImplicitCastExpr>(expr));
927 case Stmt::DeclRefExprClass:
928 return extract_access(cast<DeclRefExpr>(expr));
929 case Stmt::ArraySubscriptExprClass:
930 return extract_access(cast<ArraySubscriptExpr>(expr));
931 case Stmt::IntegerLiteralClass:
932 return extract_access(cast<IntegerLiteral>(expr));
933 default:
934 unsupported(expr);
936 return NULL;
939 /* Assign the affine expression "index" to the output dimension "pos" of "map",
940 * restrict the domain to those values that result in a non-negative index
941 * and return the result.
943 __isl_give isl_map *set_index(__isl_take isl_map *map, int pos,
944 __isl_take isl_pw_aff *index)
946 isl_map *index_map;
947 int len = isl_map_dim(map, isl_dim_out);
948 isl_id *id;
949 isl_set *domain;
951 domain = isl_pw_aff_nonneg_set(isl_pw_aff_copy(index));
952 index = isl_pw_aff_intersect_domain(index, domain);
953 index_map = isl_map_from_range(isl_set_from_pw_aff(index));
954 index_map = isl_map_insert_dims(index_map, isl_dim_out, 0, pos);
955 index_map = isl_map_add_dims(index_map, isl_dim_out, len - pos - 1);
956 id = isl_map_get_tuple_id(map, isl_dim_out);
957 index_map = isl_map_set_tuple_id(index_map, isl_dim_out, id);
959 map = isl_map_intersect(map, index_map);
961 return map;
964 /* Extract an access relation from the given array subscript expression.
965 * If nesting is allowed in general, then we turn it on while
966 * examining the index expression.
968 * We first extract an access relation from the base.
969 * This will result in an access relation with a range that corresponds
970 * to the array being accessed and with earlier indices filled in already.
971 * We then extract the current index and fill that in as well.
972 * The position of the current index is based on the type of base.
973 * If base is the actual array variable, then the depth of this type
974 * will be the same as the depth of the array and we will fill in
975 * the first array index.
976 * Otherwise, the depth of the base type will be smaller and we will fill
977 * in a later index.
979 __isl_give isl_map *PetScan::extract_access(ArraySubscriptExpr *expr)
981 Expr *base = expr->getBase();
982 Expr *idx = expr->getIdx();
983 isl_pw_aff *index;
984 isl_map *base_access;
985 isl_map *access;
986 int depth = array_depth(base->getType().getTypePtr());
987 int pos;
988 bool save_nesting = nesting_enabled;
990 nesting_enabled = allow_nested;
992 base_access = extract_access(base);
993 index = extract_affine(idx);
995 nesting_enabled = save_nesting;
997 pos = isl_map_dim(base_access, isl_dim_out) - depth;
998 access = set_index(base_access, pos, index);
1000 return access;
1003 /* Check if "expr" calls function "minmax" with two arguments and if so
1004 * make lhs and rhs refer to these two arguments.
1006 static bool is_minmax(Expr *expr, const char *minmax, Expr *&lhs, Expr *&rhs)
1008 CallExpr *call;
1009 FunctionDecl *fd;
1010 string name;
1012 if (expr->getStmtClass() != Stmt::CallExprClass)
1013 return false;
1015 call = cast<CallExpr>(expr);
1016 fd = call->getDirectCallee();
1017 if (!fd)
1018 return false;
1020 if (call->getNumArgs() != 2)
1021 return false;
1023 name = fd->getDeclName().getAsString();
1024 if (name != minmax)
1025 return false;
1027 lhs = call->getArg(0);
1028 rhs = call->getArg(1);
1030 return true;
1033 /* Check if "expr" is of the form min(lhs, rhs) and if so make
1034 * lhs and rhs refer to the two arguments.
1036 static bool is_min(Expr *expr, Expr *&lhs, Expr *&rhs)
1038 return is_minmax(expr, "min", lhs, rhs);
1041 /* Check if "expr" is of the form max(lhs, rhs) and if so make
1042 * lhs and rhs refer to the two arguments.
1044 static bool is_max(Expr *expr, Expr *&lhs, Expr *&rhs)
1046 return is_minmax(expr, "max", lhs, rhs);
1049 /* Return "lhs && rhs", defined on the shared definition domain.
1051 static __isl_give isl_pw_aff *pw_aff_and(__isl_take isl_pw_aff *lhs,
1052 __isl_take isl_pw_aff *rhs)
1054 isl_set *cond;
1055 isl_set *dom;
1057 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
1058 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1059 cond = isl_set_intersect(isl_pw_aff_non_zero_set(lhs),
1060 isl_pw_aff_non_zero_set(rhs));
1061 return indicator_function(cond, dom);
1064 /* Return "lhs && rhs", with shortcut semantics.
1065 * That is, if lhs is false, then the result is defined even if rhs is not.
1066 * In practice, we compute lhs ? rhs : lhs.
1068 static __isl_give isl_pw_aff *pw_aff_and_then(__isl_take isl_pw_aff *lhs,
1069 __isl_take isl_pw_aff *rhs)
1071 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), rhs, lhs);
1074 /* Return "lhs || rhs", with shortcut semantics.
1075 * That is, if lhs is true, then the result is defined even if rhs is not.
1076 * In practice, we compute lhs ? lhs : rhs.
1078 static __isl_give isl_pw_aff *pw_aff_or_else(__isl_take isl_pw_aff *lhs,
1079 __isl_take isl_pw_aff *rhs)
1081 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), lhs, rhs);
1084 /* Extract an affine expressions representing the comparison "LHS op RHS"
1085 * "comp" is the original statement that "LHS op RHS" is derived from
1086 * and is used for diagnostics.
1088 * If the comparison is of the form
1090 * a <= min(b,c)
1092 * then the expression is constructed as the conjunction of
1093 * the comparisons
1095 * a <= b and a <= c
1097 * A similar optimization is performed for max(a,b) <= c.
1098 * We do this because that will lead to simpler representations
1099 * of the expression.
1100 * If isl is ever enhanced to explicitly deal with min and max expressions,
1101 * this optimization can be removed.
1103 __isl_give isl_pw_aff *PetScan::extract_comparison(BinaryOperatorKind op,
1104 Expr *LHS, Expr *RHS, Stmt *comp)
1106 isl_pw_aff *lhs;
1107 isl_pw_aff *rhs;
1108 isl_pw_aff *res;
1109 isl_set *cond;
1110 isl_set *dom;
1112 if (op == BO_GT)
1113 return extract_comparison(BO_LT, RHS, LHS, comp);
1114 if (op == BO_GE)
1115 return extract_comparison(BO_LE, RHS, LHS, comp);
1117 if (op == BO_LT || op == BO_LE) {
1118 Expr *expr1, *expr2;
1119 if (is_min(RHS, expr1, expr2)) {
1120 lhs = extract_comparison(op, LHS, expr1, comp);
1121 rhs = extract_comparison(op, LHS, expr2, comp);
1122 return pw_aff_and(lhs, rhs);
1124 if (is_max(LHS, expr1, expr2)) {
1125 lhs = extract_comparison(op, expr1, RHS, comp);
1126 rhs = extract_comparison(op, expr2, RHS, comp);
1127 return pw_aff_and(lhs, rhs);
1131 lhs = extract_affine(LHS);
1132 rhs = extract_affine(RHS);
1134 dom = isl_pw_aff_domain(isl_pw_aff_copy(lhs));
1135 dom = isl_set_intersect(dom, isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
1137 switch (op) {
1138 case BO_LT:
1139 cond = isl_pw_aff_lt_set(lhs, rhs);
1140 break;
1141 case BO_LE:
1142 cond = isl_pw_aff_le_set(lhs, rhs);
1143 break;
1144 case BO_EQ:
1145 cond = isl_pw_aff_eq_set(lhs, rhs);
1146 break;
1147 case BO_NE:
1148 cond = isl_pw_aff_ne_set(lhs, rhs);
1149 break;
1150 default:
1151 isl_pw_aff_free(lhs);
1152 isl_pw_aff_free(rhs);
1153 isl_set_free(dom);
1154 unsupported(comp);
1155 return NULL;
1158 cond = isl_set_coalesce(cond);
1159 res = indicator_function(cond, dom);
1161 return res;
1164 __isl_give isl_pw_aff *PetScan::extract_comparison(BinaryOperator *comp)
1166 return extract_comparison(comp->getOpcode(), comp->getLHS(),
1167 comp->getRHS(), comp);
1170 /* Extract an affine expression representing the negation (logical not)
1171 * of a subexpression.
1173 __isl_give isl_pw_aff *PetScan::extract_boolean(UnaryOperator *op)
1175 isl_set *set_cond, *dom;
1176 isl_pw_aff *cond, *res;
1178 cond = extract_condition(op->getSubExpr());
1180 dom = isl_pw_aff_domain(isl_pw_aff_copy(cond));
1182 set_cond = isl_pw_aff_zero_set(cond);
1184 res = indicator_function(set_cond, dom);
1186 return res;
1189 /* Extract an affine expression representing the disjunction (logical or)
1190 * or conjunction (logical and) of two subexpressions.
1192 __isl_give isl_pw_aff *PetScan::extract_boolean(BinaryOperator *comp)
1194 isl_pw_aff *lhs, *rhs;
1196 lhs = extract_condition(comp->getLHS());
1197 rhs = extract_condition(comp->getRHS());
1199 switch (comp->getOpcode()) {
1200 case BO_LAnd:
1201 return pw_aff_and_then(lhs, rhs);
1202 case BO_LOr:
1203 return pw_aff_or_else(lhs, rhs);
1204 default:
1205 isl_pw_aff_free(lhs);
1206 isl_pw_aff_free(rhs);
1209 unsupported(comp);
1210 return NULL;
1213 __isl_give isl_pw_aff *PetScan::extract_condition(UnaryOperator *expr)
1215 switch (expr->getOpcode()) {
1216 case UO_LNot:
1217 return extract_boolean(expr);
1218 default:
1219 unsupported(expr);
1220 return NULL;
1224 /* Extract the affine expression "expr != 0 ? 1 : 0".
1226 __isl_give isl_pw_aff *PetScan::extract_implicit_condition(Expr *expr)
1228 isl_pw_aff *res;
1229 isl_set *set, *dom;
1231 res = extract_affine(expr);
1233 dom = isl_pw_aff_domain(isl_pw_aff_copy(res));
1234 set = isl_pw_aff_non_zero_set(res);
1236 res = indicator_function(set, dom);
1238 return res;
1241 /* Extract an affine expression from a boolean expression.
1242 * In particular, return the expression "expr ? 1 : 0".
1244 * If the expression doesn't look like a condition, we assume it
1245 * is an affine expression and return the condition "expr != 0 ? 1 : 0".
1247 __isl_give isl_pw_aff *PetScan::extract_condition(Expr *expr)
1249 BinaryOperator *comp;
1251 if (!expr) {
1252 isl_set *u = isl_set_universe(isl_space_params_alloc(ctx, 0));
1253 return indicator_function(u, isl_set_copy(u));
1256 if (expr->getStmtClass() == Stmt::ParenExprClass)
1257 return extract_condition(cast<ParenExpr>(expr)->getSubExpr());
1259 if (expr->getStmtClass() == Stmt::UnaryOperatorClass)
1260 return extract_condition(cast<UnaryOperator>(expr));
1262 if (expr->getStmtClass() != Stmt::BinaryOperatorClass)
1263 return extract_implicit_condition(expr);
1265 comp = cast<BinaryOperator>(expr);
1266 switch (comp->getOpcode()) {
1267 case BO_LT:
1268 case BO_LE:
1269 case BO_GT:
1270 case BO_GE:
1271 case BO_EQ:
1272 case BO_NE:
1273 return extract_comparison(comp);
1274 case BO_LAnd:
1275 case BO_LOr:
1276 return extract_boolean(comp);
1277 default:
1278 return extract_implicit_condition(expr);
1282 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
1284 switch (kind) {
1285 case UO_Minus:
1286 return pet_op_minus;
1287 case UO_PostInc:
1288 return pet_op_post_inc;
1289 case UO_PostDec:
1290 return pet_op_post_dec;
1291 case UO_PreInc:
1292 return pet_op_pre_inc;
1293 case UO_PreDec:
1294 return pet_op_pre_dec;
1295 default:
1296 return pet_op_last;
1300 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
1302 switch (kind) {
1303 case BO_AddAssign:
1304 return pet_op_add_assign;
1305 case BO_SubAssign:
1306 return pet_op_sub_assign;
1307 case BO_MulAssign:
1308 return pet_op_mul_assign;
1309 case BO_DivAssign:
1310 return pet_op_div_assign;
1311 case BO_Assign:
1312 return pet_op_assign;
1313 case BO_Add:
1314 return pet_op_add;
1315 case BO_Sub:
1316 return pet_op_sub;
1317 case BO_Mul:
1318 return pet_op_mul;
1319 case BO_Div:
1320 return pet_op_div;
1321 case BO_Rem:
1322 return pet_op_mod;
1323 case BO_EQ:
1324 return pet_op_eq;
1325 case BO_LE:
1326 return pet_op_le;
1327 case BO_LT:
1328 return pet_op_lt;
1329 case BO_GT:
1330 return pet_op_gt;
1331 default:
1332 return pet_op_last;
1336 /* Construct a pet_expr representing a unary operator expression.
1338 struct pet_expr *PetScan::extract_expr(UnaryOperator *expr)
1340 struct pet_expr *arg;
1341 enum pet_op_type op;
1343 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
1344 if (op == pet_op_last) {
1345 unsupported(expr);
1346 return NULL;
1349 arg = extract_expr(expr->getSubExpr());
1351 if (expr->isIncrementDecrementOp() &&
1352 arg && arg->type == pet_expr_access) {
1353 mark_write(arg);
1354 arg->acc.read = 1;
1357 return pet_expr_new_unary(ctx, op, arg);
1360 /* Mark the given access pet_expr as a write.
1361 * If a scalar is being accessed, then mark its value
1362 * as unknown in assigned_value.
1364 void PetScan::mark_write(struct pet_expr *access)
1366 isl_id *id;
1367 ValueDecl *decl;
1369 access->acc.write = 1;
1370 access->acc.read = 0;
1372 if (isl_map_dim(access->acc.access, isl_dim_out) != 0)
1373 return;
1375 id = isl_map_get_tuple_id(access->acc.access, isl_dim_out);
1376 decl = (ValueDecl *) isl_id_get_user(id);
1377 clear_assignment(assigned_value, decl);
1378 isl_id_free(id);
1381 /* Construct a pet_expr representing a binary operator expression.
1383 * If the top level operator is an assignment and the LHS is an access,
1384 * then we mark that access as a write. If the operator is a compound
1385 * assignment, the access is marked as both a read and a write.
1387 * If "expr" assigns something to a scalar variable, then we mark
1388 * the variable as having been assigned. If, furthermore, the expression
1389 * is affine, then keep track of this value in assigned_value
1390 * so that we can plug it in when we later come across the same variable.
1392 struct pet_expr *PetScan::extract_expr(BinaryOperator *expr)
1394 struct pet_expr *lhs, *rhs;
1395 enum pet_op_type op;
1397 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
1398 if (op == pet_op_last) {
1399 unsupported(expr);
1400 return NULL;
1403 lhs = extract_expr(expr->getLHS());
1404 rhs = extract_expr(expr->getRHS());
1406 if (expr->isAssignmentOp() && lhs && lhs->type == pet_expr_access) {
1407 mark_write(lhs);
1408 if (expr->isCompoundAssignmentOp())
1409 lhs->acc.read = 1;
1412 if (expr->getOpcode() == BO_Assign &&
1413 lhs && lhs->type == pet_expr_access &&
1414 isl_map_dim(lhs->acc.access, isl_dim_out) == 0) {
1415 isl_id *id = isl_map_get_tuple_id(lhs->acc.access, isl_dim_out);
1416 ValueDecl *decl = (ValueDecl *) isl_id_get_user(id);
1417 Expr *rhs = expr->getRHS();
1418 isl_pw_aff *pa = try_extract_affine(rhs);
1419 clear_assignment(assigned_value, decl);
1420 if (pa) {
1421 assigned_value[decl] = pa;
1422 insert_expression(pa);
1424 isl_id_free(id);
1427 return pet_expr_new_binary(ctx, op, lhs, rhs);
1430 /* Construct a pet_expr representing a conditional operation.
1432 * We first try to extract the condition as an affine expression.
1433 * If that fails, we construct a pet_expr tree representing the condition.
1435 struct pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
1437 struct pet_expr *cond, *lhs, *rhs;
1438 isl_pw_aff *pa;
1440 pa = try_extract_affine(expr->getCond());
1441 if (pa) {
1442 isl_set *test = isl_set_from_pw_aff(pa);
1443 cond = pet_expr_from_access(isl_map_from_range(test));
1444 } else
1445 cond = extract_expr(expr->getCond());
1446 lhs = extract_expr(expr->getTrueExpr());
1447 rhs = extract_expr(expr->getFalseExpr());
1449 return pet_expr_new_ternary(ctx, cond, lhs, rhs);
1452 struct pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
1454 return extract_expr(expr->getSubExpr());
1457 /* Construct a pet_expr representing a floating point value.
1459 struct pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
1461 return pet_expr_new_double(ctx, expr->getValueAsApproximateDouble());
1464 /* Extract an access relation from "expr" and then convert it into
1465 * a pet_expr.
1467 struct pet_expr *PetScan::extract_access_expr(Expr *expr)
1469 isl_map *access;
1470 struct pet_expr *pe;
1472 access = extract_access(expr);
1474 pe = pet_expr_from_access(access);
1476 return pe;
1479 struct pet_expr *PetScan::extract_expr(ParenExpr *expr)
1481 return extract_expr(expr->getSubExpr());
1484 /* Construct a pet_expr representing a function call.
1486 * If we are passing along a pointer to an array element
1487 * or an entire row or even higher dimensional slice of an array,
1488 * then the function being called may write into the array.
1490 * We assume here that if the function is declared to take a pointer
1491 * to a const type, then the function will perform a read
1492 * and that otherwise, it will perform a write.
1494 struct pet_expr *PetScan::extract_expr(CallExpr *expr)
1496 struct pet_expr *res = NULL;
1497 FunctionDecl *fd;
1498 string name;
1500 fd = expr->getDirectCallee();
1501 if (!fd) {
1502 unsupported(expr);
1503 return NULL;
1506 name = fd->getDeclName().getAsString();
1507 res = pet_expr_new_call(ctx, name.c_str(), expr->getNumArgs());
1508 if (!res)
1509 return NULL;
1511 for (int i = 0; i < expr->getNumArgs(); ++i) {
1512 Expr *arg = expr->getArg(i);
1513 int is_addr = 0;
1514 pet_expr *main_arg;
1516 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
1517 ImplicitCastExpr *ice = cast<ImplicitCastExpr>(arg);
1518 arg = ice->getSubExpr();
1520 if (arg->getStmtClass() == Stmt::UnaryOperatorClass) {
1521 UnaryOperator *op = cast<UnaryOperator>(arg);
1522 if (op->getOpcode() == UO_AddrOf) {
1523 is_addr = 1;
1524 arg = op->getSubExpr();
1527 res->args[i] = PetScan::extract_expr(arg);
1528 main_arg = res->args[i];
1529 if (is_addr)
1530 res->args[i] = pet_expr_new_unary(ctx,
1531 pet_op_address_of, res->args[i]);
1532 if (!res->args[i])
1533 goto error;
1534 if (arg->getStmtClass() == Stmt::ArraySubscriptExprClass &&
1535 array_depth(arg->getType().getTypePtr()) > 0)
1536 is_addr = 1;
1537 if (is_addr && main_arg->type == pet_expr_access) {
1538 ParmVarDecl *parm;
1539 if (!fd->hasPrototype()) {
1540 unsupported(expr, "prototype required");
1541 goto error;
1543 parm = fd->getParamDecl(i);
1544 if (!const_base(parm->getType()))
1545 mark_write(main_arg);
1549 return res;
1550 error:
1551 pet_expr_free(res);
1552 return NULL;
1555 /* Try and onstruct a pet_expr representing "expr".
1557 struct pet_expr *PetScan::extract_expr(Expr *expr)
1559 switch (expr->getStmtClass()) {
1560 case Stmt::UnaryOperatorClass:
1561 return extract_expr(cast<UnaryOperator>(expr));
1562 case Stmt::CompoundAssignOperatorClass:
1563 case Stmt::BinaryOperatorClass:
1564 return extract_expr(cast<BinaryOperator>(expr));
1565 case Stmt::ImplicitCastExprClass:
1566 return extract_expr(cast<ImplicitCastExpr>(expr));
1567 case Stmt::ArraySubscriptExprClass:
1568 case Stmt::DeclRefExprClass:
1569 case Stmt::IntegerLiteralClass:
1570 return extract_access_expr(expr);
1571 case Stmt::FloatingLiteralClass:
1572 return extract_expr(cast<FloatingLiteral>(expr));
1573 case Stmt::ParenExprClass:
1574 return extract_expr(cast<ParenExpr>(expr));
1575 case Stmt::ConditionalOperatorClass:
1576 return extract_expr(cast<ConditionalOperator>(expr));
1577 case Stmt::CallExprClass:
1578 return extract_expr(cast<CallExpr>(expr));
1579 default:
1580 unsupported(expr);
1582 return NULL;
1585 /* Check if the given initialization statement is an assignment.
1586 * If so, return that assignment. Otherwise return NULL.
1588 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1590 BinaryOperator *ass;
1592 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1593 return NULL;
1595 ass = cast<BinaryOperator>(init);
1596 if (ass->getOpcode() != BO_Assign)
1597 return NULL;
1599 return ass;
1602 /* Check if the given initialization statement is a declaration
1603 * of a single variable.
1604 * If so, return that declaration. Otherwise return NULL.
1606 Decl *PetScan::initialization_declaration(Stmt *init)
1608 DeclStmt *decl;
1610 if (init->getStmtClass() != Stmt::DeclStmtClass)
1611 return NULL;
1613 decl = cast<DeclStmt>(init);
1615 if (!decl->isSingleDecl())
1616 return NULL;
1618 return decl->getSingleDecl();
1621 /* Given the assignment operator in the initialization of a for loop,
1622 * extract the induction variable, i.e., the (integer)variable being
1623 * assigned.
1625 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1627 Expr *lhs;
1628 DeclRefExpr *ref;
1629 ValueDecl *decl;
1630 const Type *type;
1632 lhs = init->getLHS();
1633 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1634 unsupported(init);
1635 return NULL;
1638 ref = cast<DeclRefExpr>(lhs);
1639 decl = ref->getDecl();
1640 type = decl->getType().getTypePtr();
1642 if (!type->isIntegerType()) {
1643 unsupported(lhs);
1644 return NULL;
1647 return decl;
1650 /* Given the initialization statement of a for loop and the single
1651 * declaration in this initialization statement,
1652 * extract the induction variable, i.e., the (integer) variable being
1653 * declared.
1655 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1657 VarDecl *vd;
1659 vd = cast<VarDecl>(decl);
1661 const QualType type = vd->getType();
1662 if (!type->isIntegerType()) {
1663 unsupported(init);
1664 return NULL;
1667 if (!vd->getInit()) {
1668 unsupported(init);
1669 return NULL;
1672 return vd;
1675 /* Check that op is of the form iv++ or iv--.
1676 * Return an affine expression "1" or "-1" accordingly.
1678 __isl_give isl_pw_aff *PetScan::extract_unary_increment(
1679 clang::UnaryOperator *op, clang::ValueDecl *iv)
1681 Expr *sub;
1682 DeclRefExpr *ref;
1683 isl_space *space;
1684 isl_aff *aff;
1686 if (!op->isIncrementDecrementOp()) {
1687 unsupported(op);
1688 return NULL;
1691 sub = op->getSubExpr();
1692 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1693 unsupported(op);
1694 return NULL;
1697 ref = cast<DeclRefExpr>(sub);
1698 if (ref->getDecl() != iv) {
1699 unsupported(op);
1700 return NULL;
1703 space = isl_space_params_alloc(ctx, 0);
1704 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
1706 if (op->isIncrementOp())
1707 aff = isl_aff_add_constant_si(aff, 1);
1708 else
1709 aff = isl_aff_add_constant_si(aff, -1);
1711 return isl_pw_aff_from_aff(aff);
1714 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1715 * has a single constant expression, then put this constant in *user.
1716 * The caller is assumed to have checked that this function will
1717 * be called exactly once.
1719 static int extract_cst(__isl_take isl_set *set, __isl_take isl_aff *aff,
1720 void *user)
1722 isl_int *inc = (isl_int *)user;
1723 int res = 0;
1725 if (isl_aff_is_cst(aff))
1726 isl_aff_get_constant(aff, inc);
1727 else
1728 res = -1;
1730 isl_set_free(set);
1731 isl_aff_free(aff);
1733 return res;
1736 /* Check if op is of the form
1738 * iv = iv + inc
1740 * and return inc as an affine expression.
1742 * We extract an affine expression from the RHS, subtract iv and return
1743 * the result.
1745 __isl_give isl_pw_aff *PetScan::extract_binary_increment(BinaryOperator *op,
1746 clang::ValueDecl *iv)
1748 Expr *lhs;
1749 DeclRefExpr *ref;
1750 isl_id *id;
1751 isl_space *dim;
1752 isl_aff *aff;
1753 isl_pw_aff *val;
1755 if (op->getOpcode() != BO_Assign) {
1756 unsupported(op);
1757 return NULL;
1760 lhs = op->getLHS();
1761 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1762 unsupported(op);
1763 return NULL;
1766 ref = cast<DeclRefExpr>(lhs);
1767 if (ref->getDecl() != iv) {
1768 unsupported(op);
1769 return NULL;
1772 val = extract_affine(op->getRHS());
1774 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
1776 dim = isl_space_params_alloc(ctx, 1);
1777 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
1778 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1779 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
1781 val = isl_pw_aff_sub(val, isl_pw_aff_from_aff(aff));
1783 return val;
1786 /* Check that op is of the form iv += cst or iv -= cst
1787 * and return an affine expression corresponding oto cst or -cst accordingly.
1789 __isl_give isl_pw_aff *PetScan::extract_compound_increment(
1790 CompoundAssignOperator *op, clang::ValueDecl *iv)
1792 Expr *lhs;
1793 DeclRefExpr *ref;
1794 bool neg = false;
1795 isl_pw_aff *val;
1796 BinaryOperatorKind opcode;
1798 opcode = op->getOpcode();
1799 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1800 unsupported(op);
1801 return NULL;
1803 if (opcode == BO_SubAssign)
1804 neg = true;
1806 lhs = op->getLHS();
1807 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1808 unsupported(op);
1809 return NULL;
1812 ref = cast<DeclRefExpr>(lhs);
1813 if (ref->getDecl() != iv) {
1814 unsupported(op);
1815 return NULL;
1818 val = extract_affine(op->getRHS());
1819 if (neg)
1820 val = isl_pw_aff_neg(val);
1822 return val;
1825 /* Check that the increment of the given for loop increments
1826 * (or decrements) the induction variable "iv" and return
1827 * the increment as an affine expression if successful.
1829 __isl_give isl_pw_aff *PetScan::extract_increment(clang::ForStmt *stmt,
1830 ValueDecl *iv)
1832 Stmt *inc = stmt->getInc();
1834 if (!inc) {
1835 unsupported(stmt);
1836 return NULL;
1839 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1840 return extract_unary_increment(cast<UnaryOperator>(inc), iv);
1841 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1842 return extract_compound_increment(
1843 cast<CompoundAssignOperator>(inc), iv);
1844 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1845 return extract_binary_increment(cast<BinaryOperator>(inc), iv);
1847 unsupported(inc);
1848 return NULL;
1851 /* Embed the given iteration domain in an extra outer loop
1852 * with induction variable "var".
1853 * If this variable appeared as a parameter in the constraints,
1854 * it is replaced by the new outermost dimension.
1856 static __isl_give isl_set *embed(__isl_take isl_set *set,
1857 __isl_take isl_id *var)
1859 int pos;
1861 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
1862 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
1863 if (pos >= 0) {
1864 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
1865 set = isl_set_project_out(set, isl_dim_param, pos, 1);
1868 isl_id_free(var);
1869 return set;
1872 /* Return those elements in the space of "cond" that come after
1873 * (based on "sign") an element in "cond".
1875 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
1877 isl_map *previous_to_this;
1879 if (sign > 0)
1880 previous_to_this = isl_map_lex_lt(isl_set_get_space(cond));
1881 else
1882 previous_to_this = isl_map_lex_gt(isl_set_get_space(cond));
1884 cond = isl_set_apply(cond, previous_to_this);
1886 return cond;
1889 /* Create the infinite iteration domain
1891 * { [id] : id >= 0 }
1893 * If "scop" has an affine skip of type pet_skip_later,
1894 * then remove those iterations i that have an earlier iteration
1895 * where the skip condition is satisfied, meaning that iteration i
1896 * is not executed.
1897 * Since we are dealing with a loop without loop iterator,
1898 * the skip condition cannot refer to the current loop iterator and
1899 * so effectively, the returned set is of the form
1901 * { [0]; [id] : id >= 1 and not skip }
1903 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id,
1904 struct pet_scop *scop)
1906 isl_ctx *ctx = isl_id_get_ctx(id);
1907 isl_set *domain;
1908 isl_set *skip;
1910 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
1911 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
1913 if (!pet_scop_has_affine_skip(scop, pet_skip_later))
1914 return domain;
1916 skip = pet_scop_get_skip(scop, pet_skip_later);
1917 skip = isl_set_fix_si(skip, isl_dim_set, 0, 1);
1918 skip = isl_set_params(skip);
1919 skip = embed(skip, isl_id_copy(id));
1920 skip = isl_set_intersect(skip , isl_set_copy(domain));
1921 domain = isl_set_subtract(domain, after(skip, 1));
1923 return domain;
1926 /* Create an identity mapping on the space containing "domain".
1928 static __isl_give isl_map *identity_map(__isl_keep isl_set *domain)
1930 isl_space *space;
1931 isl_map *id;
1933 space = isl_space_map_from_set(isl_set_get_space(domain));
1934 id = isl_map_identity(space);
1936 return id;
1939 /* Add a filter to "scop" that imposes that it is only executed
1940 * when "break_access" has a zero value for all previous iterations
1941 * of "domain".
1943 * The input "break_access" has a zero-dimensional domain and range.
1945 static struct pet_scop *scop_add_break(struct pet_scop *scop,
1946 __isl_take isl_map *break_access, __isl_take isl_set *domain, int sign)
1948 isl_ctx *ctx = isl_set_get_ctx(domain);
1949 isl_id *id_test;
1950 isl_map *prev;
1952 id_test = isl_map_get_tuple_id(break_access, isl_dim_out);
1953 break_access = isl_map_add_dims(break_access, isl_dim_in, 1);
1954 break_access = isl_map_add_dims(break_access, isl_dim_out, 1);
1955 break_access = isl_map_intersect_range(break_access, domain);
1956 break_access = isl_map_set_tuple_id(break_access, isl_dim_out, id_test);
1957 if (sign > 0)
1958 prev = isl_map_lex_gt_first(isl_map_get_space(break_access), 1);
1959 else
1960 prev = isl_map_lex_lt_first(isl_map_get_space(break_access), 1);
1961 break_access = isl_map_intersect(break_access, prev);
1962 scop = pet_scop_filter(scop, break_access, 0);
1963 scop = pet_scop_merge_filters(scop);
1965 return scop;
1968 /* Construct a pet_scop for an infinite loop around the given body.
1970 * We extract a pet_scop for the body and then embed it in a loop with
1971 * iteration domain
1973 * { [t] : t >= 0 }
1975 * and schedule
1977 * { [t] -> [t] }
1979 * If the body contains any break, then it is taken into
1980 * account in infinite_domain (if the skip condition is affine)
1981 * or in scop_add_break (if the skip condition is not affine).
1983 struct pet_scop *PetScan::extract_infinite_loop(Stmt *body)
1985 isl_id *id;
1986 isl_set *domain;
1987 isl_map *ident;
1988 isl_map *access;
1989 struct pet_scop *scop;
1990 bool has_var_break;
1992 scop = extract(body);
1993 if (!scop)
1994 return NULL;
1996 id = isl_id_alloc(ctx, "t", NULL);
1997 domain = infinite_domain(isl_id_copy(id), scop);
1998 ident = identity_map(domain);
2000 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
2001 if (has_var_break)
2002 access = pet_scop_get_skip_map(scop, pet_skip_later);
2004 scop = pet_scop_embed(scop, isl_set_copy(domain),
2005 isl_map_copy(ident), ident, id);
2006 if (has_var_break)
2007 scop = scop_add_break(scop, access, domain, 1);
2008 else
2009 isl_set_free(domain);
2011 return scop;
2014 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
2016 * for (;;)
2017 * body
2020 struct pet_scop *PetScan::extract_infinite_for(ForStmt *stmt)
2022 return extract_infinite_loop(stmt->getBody());
2025 /* Create an access to a virtual array representing the result
2026 * of a condition.
2027 * Unlike other accessed data, the id of the array is NULL as
2028 * there is no ValueDecl in the program corresponding to the virtual
2029 * array.
2030 * The array starts out as a scalar, but grows along with the
2031 * statement writing to the array in pet_scop_embed.
2033 static __isl_give isl_map *create_test_access(isl_ctx *ctx, int test_nr)
2035 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
2036 isl_id *id;
2037 char name[50];
2039 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
2040 id = isl_id_alloc(ctx, name, NULL);
2041 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
2042 return isl_map_universe(dim);
2045 /* Add an array with the given extent ("access") to the list
2046 * of arrays in "scop" and return the extended pet_scop.
2047 * The array is marked as attaining values 0 and 1 only and
2048 * as each element being assigned at most once.
2050 static struct pet_scop *scop_add_array(struct pet_scop *scop,
2051 __isl_keep isl_map *access, clang::ASTContext &ast_ctx)
2053 isl_ctx *ctx = isl_map_get_ctx(access);
2054 isl_space *dim;
2055 struct pet_array **arrays;
2056 struct pet_array *array;
2058 if (!scop)
2059 return NULL;
2060 if (!ctx)
2061 goto error;
2063 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
2064 scop->n_array + 1);
2065 if (!arrays)
2066 goto error;
2067 scop->arrays = arrays;
2069 array = isl_calloc_type(ctx, struct pet_array);
2070 if (!array)
2071 goto error;
2073 array->extent = isl_map_range(isl_map_copy(access));
2074 dim = isl_space_params_alloc(ctx, 0);
2075 array->context = isl_set_universe(dim);
2076 dim = isl_space_set_alloc(ctx, 0, 1);
2077 array->value_bounds = isl_set_universe(dim);
2078 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
2079 isl_dim_set, 0, 0);
2080 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
2081 isl_dim_set, 0, 1);
2082 array->element_type = strdup("int");
2083 array->element_size = ast_ctx.getTypeInfo(ast_ctx.IntTy).first / 8;
2084 array->uniquely_defined = 1;
2086 scop->arrays[scop->n_array] = array;
2087 scop->n_array++;
2089 if (!array->extent || !array->context)
2090 goto error;
2092 return scop;
2093 error:
2094 pet_scop_free(scop);
2095 return NULL;
2098 /* Construct a pet_scop for a while loop of the form
2100 * while (pa)
2101 * body
2103 * In particular, construct a scop for an infinite loop around body and
2104 * intersect the domain with the affine expression.
2105 * Note that this intersection may result in an empty loop.
2107 struct pet_scop *PetScan::extract_affine_while(__isl_take isl_pw_aff *pa,
2108 Stmt *body)
2110 struct pet_scop *scop;
2111 isl_set *dom;
2112 isl_set *valid;
2114 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
2115 dom = isl_pw_aff_non_zero_set(pa);
2116 scop = extract_infinite_loop(body);
2117 scop = pet_scop_restrict(scop, dom);
2118 scop = pet_scop_restrict_context(scop, valid);
2120 return scop;
2123 /* Construct a scop for a while, given the scops for the condition
2124 * and the body, the filter access and the iteration domain of
2125 * the while loop.
2127 * In particular, the scop for the condition is filtered to depend
2128 * on "test_access" evaluating to true for all previous iterations
2129 * of the loop, while the scop for the body is filtered to depend
2130 * on "test_access" evaluating to true for all iterations up to the
2131 * current iteration.
2133 * These filtered scops are then combined into a single scop.
2135 * "sign" is positive if the iterator increases and negative
2136 * if it decreases.
2138 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
2139 struct pet_scop *scop_body, __isl_take isl_map *test_access,
2140 __isl_take isl_set *domain, int sign)
2142 isl_ctx *ctx = isl_set_get_ctx(domain);
2143 isl_id *id_test;
2144 isl_map *prev;
2146 id_test = isl_map_get_tuple_id(test_access, isl_dim_out);
2147 test_access = isl_map_add_dims(test_access, isl_dim_in, 1);
2148 test_access = isl_map_add_dims(test_access, isl_dim_out, 1);
2149 test_access = isl_map_intersect_range(test_access, domain);
2150 test_access = isl_map_set_tuple_id(test_access, isl_dim_out, id_test);
2151 if (sign > 0)
2152 prev = isl_map_lex_ge_first(isl_map_get_space(test_access), 1);
2153 else
2154 prev = isl_map_lex_le_first(isl_map_get_space(test_access), 1);
2155 test_access = isl_map_intersect(test_access, prev);
2156 scop_body = pet_scop_filter(scop_body, isl_map_copy(test_access), 1);
2157 if (sign > 0)
2158 prev = isl_map_lex_gt_first(isl_map_get_space(test_access), 1);
2159 else
2160 prev = isl_map_lex_lt_first(isl_map_get_space(test_access), 1);
2161 test_access = isl_map_intersect(test_access, prev);
2162 scop_cond = pet_scop_filter(scop_cond, test_access, 1);
2164 return pet_scop_add_seq(ctx, scop_cond, scop_body);
2167 /* Check if the while loop is of the form
2169 * while (affine expression)
2170 * body
2172 * If so, call extract_affine_while to construct a scop.
2174 * Otherwise, construct a generic while scop, with iteration domain
2175 * { [t] : t >= 0 }. The scop consists of two parts, one for
2176 * evaluating the condition and one for the body.
2177 * The schedule is adjusted to reflect that the condition is evaluated
2178 * before the body is executed and the body is filtered to depend
2179 * on the result of the condition evaluating to true on all iterations
2180 * up to the current iteration, while the evaluation the condition itself
2181 * is filtered to depend on the result of the condition evaluating to true
2182 * on all previous iterations.
2183 * The context of the scop representing the body is dropped
2184 * because we don't know how many times the body will be executed,
2185 * if at all.
2187 * If the body contains any break, then it is taken into
2188 * account in infinite_domain (if the skip condition is affine)
2189 * or in scop_add_break (if the skip condition is not affine).
2191 struct pet_scop *PetScan::extract(WhileStmt *stmt)
2193 Expr *cond;
2194 isl_id *id;
2195 isl_map *test_access;
2196 isl_set *domain;
2197 isl_map *ident;
2198 isl_pw_aff *pa;
2199 struct pet_scop *scop, *scop_body;
2200 bool has_var_break;
2201 isl_map *break_access;
2203 cond = stmt->getCond();
2204 if (!cond) {
2205 unsupported(stmt);
2206 return NULL;
2209 clear_assignments clear(assigned_value);
2210 clear.TraverseStmt(stmt->getBody());
2212 pa = try_extract_affine_condition(cond);
2213 if (pa)
2214 return extract_affine_while(pa, stmt->getBody());
2216 if (!allow_nested) {
2217 unsupported(stmt);
2218 return NULL;
2221 test_access = create_test_access(ctx, n_test++);
2222 scop = extract_non_affine_condition(cond, isl_map_copy(test_access));
2223 scop = scop_add_array(scop, test_access, ast_context);
2224 scop_body = extract(stmt->getBody());
2226 id = isl_id_alloc(ctx, "t", NULL);
2227 domain = infinite_domain(isl_id_copy(id), scop_body);
2228 ident = identity_map(domain);
2230 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
2231 if (has_var_break)
2232 break_access = pet_scop_get_skip_map(scop_body, pet_skip_later);
2234 scop = pet_scop_prefix(scop, 0);
2235 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_map_copy(ident),
2236 isl_map_copy(ident), isl_id_copy(id));
2237 scop_body = pet_scop_reset_context(scop_body);
2238 scop_body = pet_scop_prefix(scop_body, 1);
2239 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
2240 isl_map_copy(ident), ident, id);
2242 if (has_var_break) {
2243 scop = scop_add_break(scop, isl_map_copy(break_access),
2244 isl_set_copy(domain), 1);
2245 scop_body = scop_add_break(scop_body, break_access,
2246 isl_set_copy(domain), 1);
2248 scop = scop_add_while(scop, scop_body, test_access, domain, 1);
2250 return scop;
2253 /* Check whether "cond" expresses a simple loop bound
2254 * on the only set dimension.
2255 * In particular, if "up" is set then "cond" should contain only
2256 * upper bounds on the set dimension.
2257 * Otherwise, it should contain only lower bounds.
2259 static bool is_simple_bound(__isl_keep isl_set *cond, isl_int inc)
2261 if (isl_int_is_pos(inc))
2262 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
2263 else
2264 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
2267 /* Extend a condition on a given iteration of a loop to one that
2268 * imposes the same condition on all previous iterations.
2269 * "domain" expresses the lower [upper] bound on the iterations
2270 * when inc is positive [negative].
2272 * In particular, we construct the condition (when inc is positive)
2274 * forall i' : (domain(i') and i' <= i) => cond(i')
2276 * which is equivalent to
2278 * not exists i' : domain(i') and i' <= i and not cond(i')
2280 * We construct this set by negating cond, applying a map
2282 * { [i'] -> [i] : domain(i') and i' <= i }
2284 * and then negating the result again.
2286 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
2287 __isl_take isl_set *domain, isl_int inc)
2289 isl_map *previous_to_this;
2291 if (isl_int_is_pos(inc))
2292 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
2293 else
2294 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
2296 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
2298 cond = isl_set_complement(cond);
2299 cond = isl_set_apply(cond, previous_to_this);
2300 cond = isl_set_complement(cond);
2302 return cond;
2305 /* Construct a domain of the form
2307 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
2309 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
2310 __isl_take isl_pw_aff *init, isl_int inc)
2312 isl_aff *aff;
2313 isl_space *dim;
2314 isl_set *set;
2316 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
2317 dim = isl_pw_aff_get_domain_space(init);
2318 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2319 aff = isl_aff_add_coefficient(aff, isl_dim_in, 0, inc);
2320 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
2322 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
2323 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
2324 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2325 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
2327 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
2329 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
2331 return isl_set_params(set);
2334 /* Assuming "cond" represents a bound on a loop where the loop
2335 * iterator "iv" is incremented (or decremented) by one, check if wrapping
2336 * is possible.
2338 * Under the given assumptions, wrapping is only possible if "cond" allows
2339 * for the last value before wrapping, i.e., 2^width - 1 in case of an
2340 * increasing iterator and 0 in case of a decreasing iterator.
2342 static bool can_wrap(__isl_keep isl_set *cond, ValueDecl *iv, isl_int inc)
2344 bool cw;
2345 isl_int limit;
2346 isl_set *test;
2348 test = isl_set_copy(cond);
2350 isl_int_init(limit);
2351 if (isl_int_is_neg(inc))
2352 isl_int_set_si(limit, 0);
2353 else {
2354 isl_int_set_si(limit, 1);
2355 isl_int_mul_2exp(limit, limit, get_type_size(iv));
2356 isl_int_sub_ui(limit, limit, 1);
2359 test = isl_set_fix(cond, isl_dim_set, 0, limit);
2360 cw = !isl_set_is_empty(test);
2361 isl_set_free(test);
2363 isl_int_clear(limit);
2365 return cw;
2368 /* Given a one-dimensional space, construct the following mapping on this
2369 * space
2371 * { [v] -> [v mod 2^width] }
2373 * where width is the number of bits used to represent the values
2374 * of the unsigned variable "iv".
2376 static __isl_give isl_map *compute_wrapping(__isl_take isl_space *dim,
2377 ValueDecl *iv)
2379 isl_int mod;
2380 isl_aff *aff;
2381 isl_map *map;
2383 isl_int_init(mod);
2384 isl_int_set_si(mod, 1);
2385 isl_int_mul_2exp(mod, mod, get_type_size(iv));
2387 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2388 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2389 aff = isl_aff_mod(aff, mod);
2391 isl_int_clear(mod);
2393 return isl_map_from_basic_map(isl_basic_map_from_aff(aff));
2394 map = isl_map_reverse(map);
2397 /* Project out the parameter "id" from "set".
2399 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
2400 __isl_keep isl_id *id)
2402 int pos;
2404 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
2405 if (pos >= 0)
2406 set = isl_set_project_out(set, isl_dim_param, pos, 1);
2408 return set;
2411 /* Compute the set of parameters for which "set1" is a subset of "set2".
2413 * set1 is a subset of set2 if
2415 * forall i in set1 : i in set2
2417 * or
2419 * not exists i in set1 and i not in set2
2421 * i.e.,
2423 * not exists i in set1 \ set2
2425 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
2426 __isl_take isl_set *set2)
2428 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
2431 /* Compute the set of parameter values for which "cond" holds
2432 * on the next iteration for each element of "dom".
2434 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2435 * and then compute the set of parameters for which the result is a subset
2436 * of "cond".
2438 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
2439 __isl_take isl_set *dom, isl_int inc)
2441 isl_space *space;
2442 isl_aff *aff;
2443 isl_map *next;
2445 space = isl_set_get_space(dom);
2446 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2447 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2448 aff = isl_aff_add_constant(aff, inc);
2449 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
2451 dom = isl_set_apply(dom, next);
2453 return enforce_subset(dom, cond);
2456 /* Does "id" refer to a nested access?
2458 static bool is_nested_parameter(__isl_keep isl_id *id)
2460 return id && isl_id_get_user(id) && !isl_id_get_name(id);
2463 /* Does parameter "pos" of "space" refer to a nested access?
2465 static bool is_nested_parameter(__isl_keep isl_space *space, int pos)
2467 bool nested;
2468 isl_id *id;
2470 id = isl_space_get_dim_id(space, isl_dim_param, pos);
2471 nested = is_nested_parameter(id);
2472 isl_id_free(id);
2474 return nested;
2477 /* Does "space" involve any parameters that refer to nested
2478 * accesses, i.e., parameters with no name?
2480 static bool has_nested(__isl_keep isl_space *space)
2482 int nparam;
2484 nparam = isl_space_dim(space, isl_dim_param);
2485 for (int i = 0; i < nparam; ++i)
2486 if (is_nested_parameter(space, i))
2487 return true;
2489 return false;
2492 /* Does "pa" involve any parameters that refer to nested
2493 * accesses, i.e., parameters with no name?
2495 static bool has_nested(__isl_keep isl_pw_aff *pa)
2497 isl_space *space;
2498 bool nested;
2500 space = isl_pw_aff_get_space(pa);
2501 nested = has_nested(space);
2502 isl_space_free(space);
2504 return nested;
2507 /* Construct a pet_scop for a for statement.
2508 * The for loop is required to be of the form
2510 * for (i = init; condition; ++i)
2512 * or
2514 * for (i = init; condition; --i)
2516 * The initialization of the for loop should either be an assignment
2517 * to an integer variable, or a declaration of such a variable with
2518 * initialization.
2520 * The condition is allowed to contain nested accesses, provided
2521 * they are not being written to inside the body of the loop.
2522 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2523 * essentially treated as a while loop, with iteration domain
2524 * { [i] : i >= init }.
2526 * We extract a pet_scop for the body and then embed it in a loop with
2527 * iteration domain and schedule
2529 * { [i] : i >= init and condition' }
2530 * { [i] -> [i] }
2532 * or
2534 * { [i] : i <= init and condition' }
2535 * { [i] -> [-i] }
2537 * Where condition' is equal to condition if the latter is
2538 * a simple upper [lower] bound and a condition that is extended
2539 * to apply to all previous iterations otherwise.
2541 * If the condition is non-affine, then we drop the condition from the
2542 * iteration domain and instead create a separate statement
2543 * for evaluating the condition. The body is then filtered to depend
2544 * on the result of the condition evaluating to true on all iterations
2545 * up to the current iteration, while the evaluation the condition itself
2546 * is filtered to depend on the result of the condition evaluating to true
2547 * on all previous iterations.
2548 * The context of the scop representing the body is dropped
2549 * because we don't know how many times the body will be executed,
2550 * if at all.
2552 * If the stride of the loop is not 1, then "i >= init" is replaced by
2554 * (exists a: i = init + stride * a and a >= 0)
2556 * If the loop iterator i is unsigned, then wrapping may occur.
2557 * During the computation, we work with a virtual iterator that
2558 * does not wrap. However, the condition in the code applies
2559 * to the wrapped value, so we need to change condition(i)
2560 * into condition([i % 2^width]).
2561 * After computing the virtual domain and schedule, we apply
2562 * the function { [v] -> [v % 2^width] } to the domain and the domain
2563 * of the schedule. In order not to lose any information, we also
2564 * need to intersect the domain of the schedule with the virtual domain
2565 * first, since some iterations in the wrapped domain may be scheduled
2566 * several times, typically an infinite number of times.
2567 * Note that there may be no need to perform this final wrapping
2568 * if the loop condition (after wrapping) satisfies certain conditions.
2569 * However, the is_simple_bound condition is not enough since it doesn't
2570 * check if there even is an upper bound.
2572 * If the loop condition is non-affine, then we keep the virtual
2573 * iterator in the iteration domain and instead replace all accesses
2574 * to the original iterator by the wrapping of the virtual iterator.
2576 * Wrapping on unsigned iterators can be avoided entirely if
2577 * loop condition is simple, the loop iterator is incremented
2578 * [decremented] by one and the last value before wrapping cannot
2579 * possibly satisfy the loop condition.
2581 * Before extracting a pet_scop from the body we remove all
2582 * assignments in assigned_value to variables that are assigned
2583 * somewhere in the body of the loop.
2585 * Valid parameters for a for loop are those for which the initial
2586 * value itself, the increment on each domain iteration and
2587 * the condition on both the initial value and
2588 * the result of incrementing the iterator for each iteration of the domain
2589 * can be evaluated.
2590 * If the loop condition is non-affine, then we only consider validity
2591 * of the initial value.
2593 * If the body contains any break, then we keep track of it in "skip"
2594 * (if the skip condition is affine) or it is handled in scop_add_break
2595 * (if the skip condition is not affine).
2596 * Note that the affine break condition needs to be considered with
2597 * respect to previous iterations in the virtual domain (if any)
2598 * and that the domain needs to be kept virtual if there is a non-affine
2599 * break condition.
2601 struct pet_scop *PetScan::extract_for(ForStmt *stmt)
2603 BinaryOperator *ass;
2604 Decl *decl;
2605 Stmt *init;
2606 Expr *lhs, *rhs;
2607 ValueDecl *iv;
2608 isl_space *space;
2609 isl_set *domain;
2610 isl_map *sched;
2611 isl_set *cond = NULL;
2612 isl_set *skip = NULL;
2613 isl_id *id;
2614 struct pet_scop *scop, *scop_cond = NULL;
2615 assigned_value_cache cache(assigned_value);
2616 isl_int inc;
2617 bool is_one;
2618 bool is_unsigned;
2619 bool is_simple;
2620 bool is_virtual;
2621 bool keep_virtual = false;
2622 bool has_affine_break;
2623 bool has_var_break;
2624 isl_map *wrap = NULL;
2625 isl_pw_aff *pa, *pa_inc, *init_val;
2626 isl_set *valid_init;
2627 isl_set *valid_cond;
2628 isl_set *valid_cond_init;
2629 isl_set *valid_cond_next;
2630 isl_set *valid_inc;
2631 isl_map *test_access = NULL, *break_access = NULL;
2632 int stmt_id;
2634 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc())
2635 return extract_infinite_for(stmt);
2637 init = stmt->getInit();
2638 if (!init) {
2639 unsupported(stmt);
2640 return NULL;
2642 if ((ass = initialization_assignment(init)) != NULL) {
2643 iv = extract_induction_variable(ass);
2644 if (!iv)
2645 return NULL;
2646 lhs = ass->getLHS();
2647 rhs = ass->getRHS();
2648 } else if ((decl = initialization_declaration(init)) != NULL) {
2649 VarDecl *var = extract_induction_variable(init, decl);
2650 if (!var)
2651 return NULL;
2652 iv = var;
2653 rhs = var->getInit();
2654 lhs = create_DeclRefExpr(var);
2655 } else {
2656 unsupported(stmt->getInit());
2657 return NULL;
2660 pa_inc = extract_increment(stmt, iv);
2661 if (!pa_inc)
2662 return NULL;
2664 isl_int_init(inc);
2665 if (isl_pw_aff_n_piece(pa_inc) != 1 ||
2666 isl_pw_aff_foreach_piece(pa_inc, &extract_cst, &inc) < 0) {
2667 isl_pw_aff_free(pa_inc);
2668 unsupported(stmt->getInc());
2669 isl_int_clear(inc);
2670 return NULL;
2672 valid_inc = isl_pw_aff_domain(pa_inc);
2674 is_unsigned = iv->getType()->isUnsignedIntegerType();
2676 assigned_value.erase(iv);
2677 clear_assignments clear(assigned_value);
2678 clear.TraverseStmt(stmt->getBody());
2680 id = isl_id_alloc(ctx, iv->getName().str().c_str(), iv);
2682 pa = try_extract_nested_condition(stmt->getCond());
2683 if (allow_nested && (!pa || has_nested(pa)))
2684 stmt_id = n_stmt++;
2686 scop = extract(stmt->getBody());
2688 has_affine_break = scop &&
2689 pet_scop_has_affine_skip(scop, pet_skip_later);
2690 if (has_affine_break) {
2691 skip = pet_scop_get_skip(scop, pet_skip_later);
2692 skip = isl_set_fix_si(skip, isl_dim_set, 0, 1);
2693 skip = isl_set_params(skip);
2695 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
2696 if (has_var_break) {
2697 break_access = pet_scop_get_skip_map(scop, pet_skip_later);
2698 keep_virtual = true;
2701 if (pa && !is_nested_allowed(pa, scop)) {
2702 isl_pw_aff_free(pa);
2703 pa = NULL;
2706 if (!allow_nested && !pa)
2707 pa = try_extract_affine_condition(stmt->getCond());
2708 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
2709 cond = isl_pw_aff_non_zero_set(pa);
2710 if (allow_nested && !cond) {
2711 int save_n_stmt = n_stmt;
2712 test_access = create_test_access(ctx, n_test++);
2713 n_stmt = stmt_id;
2714 scop_cond = extract_non_affine_condition(stmt->getCond(),
2715 isl_map_copy(test_access));
2716 n_stmt = save_n_stmt;
2717 scop_cond = scop_add_array(scop_cond, test_access, ast_context);
2718 scop_cond = pet_scop_prefix(scop_cond, 0);
2719 scop = pet_scop_reset_context(scop);
2720 scop = pet_scop_prefix(scop, 1);
2721 keep_virtual = true;
2722 cond = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
2725 cond = embed(cond, isl_id_copy(id));
2726 skip = embed(skip, isl_id_copy(id));
2727 valid_cond = isl_set_coalesce(valid_cond);
2728 valid_cond = embed(valid_cond, isl_id_copy(id));
2729 valid_inc = embed(valid_inc, isl_id_copy(id));
2730 is_one = isl_int_is_one(inc) || isl_int_is_negone(inc);
2731 is_virtual = is_unsigned && (!is_one || can_wrap(cond, iv, inc));
2733 init_val = extract_affine(rhs);
2734 valid_cond_init = enforce_subset(
2735 isl_set_from_pw_aff(isl_pw_aff_copy(init_val)),
2736 isl_set_copy(valid_cond));
2737 if (is_one && !is_virtual) {
2738 isl_pw_aff_free(init_val);
2739 pa = extract_comparison(isl_int_is_pos(inc) ? BO_GE : BO_LE,
2740 lhs, rhs, init);
2741 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
2742 valid_init = set_project_out_by_id(valid_init, id);
2743 domain = isl_pw_aff_non_zero_set(pa);
2744 } else {
2745 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
2746 domain = strided_domain(isl_id_copy(id), init_val, inc);
2749 domain = embed(domain, isl_id_copy(id));
2750 if (is_virtual) {
2751 isl_map *rev_wrap;
2752 wrap = compute_wrapping(isl_set_get_space(cond), iv);
2753 rev_wrap = isl_map_reverse(isl_map_copy(wrap));
2754 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
2755 skip = isl_set_apply(skip, isl_map_copy(rev_wrap));
2756 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
2757 valid_inc = isl_set_apply(valid_inc, rev_wrap);
2759 is_simple = is_simple_bound(cond, inc);
2760 if (!is_simple) {
2761 cond = isl_set_gist(cond, isl_set_copy(domain));
2762 is_simple = is_simple_bound(cond, inc);
2764 if (!is_simple)
2765 cond = valid_for_each_iteration(cond,
2766 isl_set_copy(domain), inc);
2767 domain = isl_set_intersect(domain, cond);
2768 if (has_affine_break) {
2769 skip = isl_set_intersect(skip , isl_set_copy(domain));
2770 skip = after(skip, isl_int_sgn(inc));
2771 domain = isl_set_subtract(domain, skip);
2773 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
2774 space = isl_space_from_domain(isl_set_get_space(domain));
2775 space = isl_space_add_dims(space, isl_dim_out, 1);
2776 sched = isl_map_universe(space);
2777 if (isl_int_is_pos(inc))
2778 sched = isl_map_equate(sched, isl_dim_in, 0, isl_dim_out, 0);
2779 else
2780 sched = isl_map_oppose(sched, isl_dim_in, 0, isl_dim_out, 0);
2782 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain), inc);
2783 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
2785 if (is_virtual && !keep_virtual) {
2786 wrap = isl_map_set_dim_id(wrap,
2787 isl_dim_out, 0, isl_id_copy(id));
2788 sched = isl_map_intersect_domain(sched, isl_set_copy(domain));
2789 domain = isl_set_apply(domain, isl_map_copy(wrap));
2790 sched = isl_map_apply_domain(sched, wrap);
2792 if (!(is_virtual && keep_virtual)) {
2793 space = isl_set_get_space(domain);
2794 wrap = isl_map_identity(isl_space_map_from_set(space));
2797 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
2798 isl_map_copy(sched), isl_map_copy(wrap), isl_id_copy(id));
2799 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
2800 scop = resolve_nested(scop);
2801 if (has_var_break)
2802 scop = scop_add_break(scop, break_access, isl_set_copy(domain),
2803 isl_int_sgn(inc));
2804 if (test_access) {
2805 scop = scop_add_while(scop_cond, scop, test_access, domain,
2806 isl_int_sgn(inc));
2807 isl_set_free(valid_inc);
2808 } else {
2809 scop = pet_scop_restrict_context(scop, valid_inc);
2810 scop = pet_scop_restrict_context(scop, valid_cond_next);
2811 scop = pet_scop_restrict_context(scop, valid_cond_init);
2812 isl_set_free(domain);
2814 clear_assignment(assigned_value, iv);
2816 isl_int_clear(inc);
2818 scop = pet_scop_restrict_context(scop, valid_init);
2820 return scop;
2823 struct pet_scop *PetScan::extract(CompoundStmt *stmt)
2825 return extract(stmt->children());
2828 /* Does parameter "pos" of "map" refer to a nested access?
2830 static bool is_nested_parameter(__isl_keep isl_map *map, int pos)
2832 bool nested;
2833 isl_id *id;
2835 id = isl_map_get_dim_id(map, isl_dim_param, pos);
2836 nested = is_nested_parameter(id);
2837 isl_id_free(id);
2839 return nested;
2842 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
2844 static int n_nested_parameter(__isl_keep isl_space *space)
2846 int n = 0;
2847 int nparam;
2849 nparam = isl_space_dim(space, isl_dim_param);
2850 for (int i = 0; i < nparam; ++i)
2851 if (is_nested_parameter(space, i))
2852 ++n;
2854 return n;
2857 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
2859 static int n_nested_parameter(__isl_keep isl_map *map)
2861 isl_space *space;
2862 int n;
2864 space = isl_map_get_space(map);
2865 n = n_nested_parameter(space);
2866 isl_space_free(space);
2868 return n;
2871 /* For each nested access parameter in "space",
2872 * construct a corresponding pet_expr, place it in args and
2873 * record its position in "param2pos".
2874 * "n_arg" is the number of elements that are already in args.
2875 * The position recorded in "param2pos" takes this number into account.
2876 * If the pet_expr corresponding to a parameter is identical to
2877 * the pet_expr corresponding to an earlier parameter, then these two
2878 * parameters are made to refer to the same element in args.
2880 * Return the final number of elements in args or -1 if an error has occurred.
2882 int PetScan::extract_nested(__isl_keep isl_space *space,
2883 int n_arg, struct pet_expr **args, std::map<int,int> &param2pos)
2885 int nparam;
2887 nparam = isl_space_dim(space, isl_dim_param);
2888 for (int i = 0; i < nparam; ++i) {
2889 int j;
2890 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
2891 Expr *nested;
2893 if (!is_nested_parameter(id)) {
2894 isl_id_free(id);
2895 continue;
2898 nested = (Expr *) isl_id_get_user(id);
2899 args[n_arg] = extract_expr(nested);
2900 if (!args[n_arg])
2901 return -1;
2903 for (j = 0; j < n_arg; ++j)
2904 if (pet_expr_is_equal(args[j], args[n_arg]))
2905 break;
2907 if (j < n_arg) {
2908 pet_expr_free(args[n_arg]);
2909 args[n_arg] = NULL;
2910 param2pos[i] = j;
2911 } else
2912 param2pos[i] = n_arg++;
2914 isl_id_free(id);
2917 return n_arg;
2920 /* For each nested access parameter in the access relations in "expr",
2921 * construct a corresponding pet_expr, place it in expr->args and
2922 * record its position in "param2pos".
2923 * n is the number of nested access parameters.
2925 struct pet_expr *PetScan::extract_nested(struct pet_expr *expr, int n,
2926 std::map<int,int> &param2pos)
2928 isl_space *space;
2930 expr->args = isl_calloc_array(ctx, struct pet_expr *, n);
2931 expr->n_arg = n;
2932 if (!expr->args)
2933 goto error;
2935 space = isl_map_get_space(expr->acc.access);
2936 n = extract_nested(space, 0, expr->args, param2pos);
2937 isl_space_free(space);
2939 if (n < 0)
2940 goto error;
2942 expr->n_arg = n;
2943 return expr;
2944 error:
2945 pet_expr_free(expr);
2946 return NULL;
2949 /* Look for parameters in any access relation in "expr" that
2950 * refer to nested accesses. In particular, these are
2951 * parameters with no name.
2953 * If there are any such parameters, then the domain of the access
2954 * relation, which is still [] at this point, is replaced by
2955 * [[] -> [t_1,...,t_n]], with n the number of these parameters
2956 * (after identifying identical nested accesses).
2957 * The parameters are then equated to the corresponding t dimensions
2958 * and subsequently projected out.
2959 * param2pos maps the position of the parameter to the position
2960 * of the corresponding t dimension.
2962 struct pet_expr *PetScan::resolve_nested(struct pet_expr *expr)
2964 int n;
2965 int nparam;
2966 int n_in;
2967 isl_space *dim;
2968 isl_map *map;
2969 std::map<int,int> param2pos;
2971 if (!expr)
2972 return expr;
2974 for (int i = 0; i < expr->n_arg; ++i) {
2975 expr->args[i] = resolve_nested(expr->args[i]);
2976 if (!expr->args[i]) {
2977 pet_expr_free(expr);
2978 return NULL;
2982 if (expr->type != pet_expr_access)
2983 return expr;
2985 n = n_nested_parameter(expr->acc.access);
2986 if (n == 0)
2987 return expr;
2989 expr = extract_nested(expr, n, param2pos);
2990 if (!expr)
2991 return NULL;
2993 n = expr->n_arg;
2994 nparam = isl_map_dim(expr->acc.access, isl_dim_param);
2995 n_in = isl_map_dim(expr->acc.access, isl_dim_in);
2996 dim = isl_map_get_space(expr->acc.access);
2997 dim = isl_space_domain(dim);
2998 dim = isl_space_from_domain(dim);
2999 dim = isl_space_add_dims(dim, isl_dim_out, n);
3000 map = isl_map_universe(dim);
3001 map = isl_map_domain_map(map);
3002 map = isl_map_reverse(map);
3003 expr->acc.access = isl_map_apply_domain(expr->acc.access, map);
3005 for (int i = nparam - 1; i >= 0; --i) {
3006 isl_id *id = isl_map_get_dim_id(expr->acc.access,
3007 isl_dim_param, i);
3008 if (!is_nested_parameter(id)) {
3009 isl_id_free(id);
3010 continue;
3013 expr->acc.access = isl_map_equate(expr->acc.access,
3014 isl_dim_param, i, isl_dim_in,
3015 n_in + param2pos[i]);
3016 expr->acc.access = isl_map_project_out(expr->acc.access,
3017 isl_dim_param, i, 1);
3019 isl_id_free(id);
3022 return expr;
3023 error:
3024 pet_expr_free(expr);
3025 return NULL;
3028 /* Convert a top-level pet_expr to a pet_scop with one statement.
3029 * This mainly involves resolving nested expression parameters
3030 * and setting the name of the iteration space.
3031 * The name is given by "label" if it is non-NULL. Otherwise,
3032 * it is of the form S_<n_stmt>.
3034 struct pet_scop *PetScan::extract(Stmt *stmt, struct pet_expr *expr,
3035 __isl_take isl_id *label)
3037 struct pet_stmt *ps;
3038 SourceLocation loc = stmt->getLocStart();
3039 int line = PP.getSourceManager().getExpansionLineNumber(loc);
3041 expr = resolve_nested(expr);
3042 ps = pet_stmt_from_pet_expr(ctx, line, label, n_stmt++, expr);
3043 return pet_scop_from_pet_stmt(ctx, ps);
3046 /* Check if we can extract an affine expression from "expr".
3047 * Return the expressions as an isl_pw_aff if we can and NULL otherwise.
3048 * We turn on autodetection so that we won't generate any warnings
3049 * and turn off nesting, so that we won't accept any non-affine constructs.
3051 __isl_give isl_pw_aff *PetScan::try_extract_affine(Expr *expr)
3053 isl_pw_aff *pwaff;
3054 int save_autodetect = options->autodetect;
3055 bool save_nesting = nesting_enabled;
3057 options->autodetect = 1;
3058 nesting_enabled = false;
3060 pwaff = extract_affine(expr);
3062 options->autodetect = save_autodetect;
3063 nesting_enabled = save_nesting;
3065 return pwaff;
3068 /* Check whether "expr" is an affine expression.
3070 bool PetScan::is_affine(Expr *expr)
3072 isl_pw_aff *pwaff;
3074 pwaff = try_extract_affine(expr);
3075 isl_pw_aff_free(pwaff);
3077 return pwaff != NULL;
3080 /* Check if we can extract an affine constraint from "expr".
3081 * Return the constraint as an isl_set if we can and NULL otherwise.
3082 * We turn on autodetection so that we won't generate any warnings
3083 * and turn off nesting, so that we won't accept any non-affine constructs.
3085 __isl_give isl_pw_aff *PetScan::try_extract_affine_condition(Expr *expr)
3087 isl_pw_aff *cond;
3088 int save_autodetect = options->autodetect;
3089 bool save_nesting = nesting_enabled;
3091 options->autodetect = 1;
3092 nesting_enabled = false;
3094 cond = extract_condition(expr);
3096 options->autodetect = save_autodetect;
3097 nesting_enabled = save_nesting;
3099 return cond;
3102 /* Check whether "expr" is an affine constraint.
3104 bool PetScan::is_affine_condition(Expr *expr)
3106 isl_pw_aff *cond;
3108 cond = try_extract_affine_condition(expr);
3109 isl_pw_aff_free(cond);
3111 return cond != NULL;
3114 /* Check if we can extract a condition from "expr".
3115 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
3116 * If allow_nested is set, then the condition may involve parameters
3117 * corresponding to nested accesses.
3118 * We turn on autodetection so that we won't generate any warnings.
3120 __isl_give isl_pw_aff *PetScan::try_extract_nested_condition(Expr *expr)
3122 isl_pw_aff *cond;
3123 int save_autodetect = options->autodetect;
3124 bool save_nesting = nesting_enabled;
3126 options->autodetect = 1;
3127 nesting_enabled = allow_nested;
3128 cond = extract_condition(expr);
3130 options->autodetect = save_autodetect;
3131 nesting_enabled = save_nesting;
3133 return cond;
3136 /* If the top-level expression of "stmt" is an assignment, then
3137 * return that assignment as a BinaryOperator.
3138 * Otherwise return NULL.
3140 static BinaryOperator *top_assignment_or_null(Stmt *stmt)
3142 BinaryOperator *ass;
3144 if (!stmt)
3145 return NULL;
3146 if (stmt->getStmtClass() != Stmt::BinaryOperatorClass)
3147 return NULL;
3149 ass = cast<BinaryOperator>(stmt);
3150 if(ass->getOpcode() != BO_Assign)
3151 return NULL;
3153 return ass;
3156 /* Check if the given if statement is a conditional assignement
3157 * with a non-affine condition. If so, construct a pet_scop
3158 * corresponding to this conditional assignment. Otherwise return NULL.
3160 * In particular we check if "stmt" is of the form
3162 * if (condition)
3163 * a = f(...);
3164 * else
3165 * a = g(...);
3167 * where a is some array or scalar access.
3168 * The constructed pet_scop then corresponds to the expression
3170 * a = condition ? f(...) : g(...)
3172 * All access relations in f(...) are intersected with condition
3173 * while all access relation in g(...) are intersected with the complement.
3175 struct pet_scop *PetScan::extract_conditional_assignment(IfStmt *stmt)
3177 BinaryOperator *ass_then, *ass_else;
3178 isl_map *write_then, *write_else;
3179 isl_set *cond, *comp;
3180 isl_map *map;
3181 isl_pw_aff *pa;
3182 int equal;
3183 struct pet_expr *pe_cond, *pe_then, *pe_else, *pe, *pe_write;
3184 bool save_nesting = nesting_enabled;
3186 if (!options->detect_conditional_assignment)
3187 return NULL;
3189 ass_then = top_assignment_or_null(stmt->getThen());
3190 ass_else = top_assignment_or_null(stmt->getElse());
3192 if (!ass_then || !ass_else)
3193 return NULL;
3195 if (is_affine_condition(stmt->getCond()))
3196 return NULL;
3198 write_then = extract_access(ass_then->getLHS());
3199 write_else = extract_access(ass_else->getLHS());
3201 equal = isl_map_is_equal(write_then, write_else);
3202 isl_map_free(write_else);
3203 if (equal < 0 || !equal) {
3204 isl_map_free(write_then);
3205 return NULL;
3208 nesting_enabled = allow_nested;
3209 pa = extract_condition(stmt->getCond());
3210 nesting_enabled = save_nesting;
3211 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
3212 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
3213 map = isl_map_from_range(isl_set_from_pw_aff(pa));
3215 pe_cond = pet_expr_from_access(map);
3217 pe_then = extract_expr(ass_then->getRHS());
3218 pe_then = pet_expr_restrict(pe_then, cond);
3219 pe_else = extract_expr(ass_else->getRHS());
3220 pe_else = pet_expr_restrict(pe_else, comp);
3222 pe = pet_expr_new_ternary(ctx, pe_cond, pe_then, pe_else);
3223 pe_write = pet_expr_from_access(write_then);
3224 if (pe_write) {
3225 pe_write->acc.write = 1;
3226 pe_write->acc.read = 0;
3228 pe = pet_expr_new_binary(ctx, pet_op_assign, pe_write, pe);
3229 return extract(stmt, pe);
3232 /* Create a pet_scop with a single statement evaluating "cond"
3233 * and writing the result to a virtual scalar, as expressed by
3234 * "access".
3236 struct pet_scop *PetScan::extract_non_affine_condition(Expr *cond,
3237 __isl_take isl_map *access)
3239 struct pet_expr *expr, *write;
3240 struct pet_stmt *ps;
3241 struct pet_scop *scop;
3242 SourceLocation loc = cond->getLocStart();
3243 int line = PP.getSourceManager().getExpansionLineNumber(loc);
3245 write = pet_expr_from_access(access);
3246 if (write) {
3247 write->acc.write = 1;
3248 write->acc.read = 0;
3250 expr = extract_expr(cond);
3251 expr = resolve_nested(expr);
3252 expr = pet_expr_new_binary(ctx, pet_op_assign, write, expr);
3253 ps = pet_stmt_from_pet_expr(ctx, line, NULL, n_stmt++, expr);
3254 scop = pet_scop_from_pet_stmt(ctx, ps);
3255 scop = resolve_nested(scop);
3257 return scop;
3260 extern "C" {
3261 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
3262 void *user);
3265 /* Apply the map pointed to by "user" to the domain of the access
3266 * relation, thereby embedding it in the range of the map.
3267 * The domain of both relations is the zero-dimensional domain.
3269 static __isl_give isl_map *embed_access(__isl_take isl_map *access, void *user)
3271 isl_map *map = (isl_map *) user;
3273 return isl_map_apply_domain(access, isl_map_copy(map));
3276 /* Apply "map" to all access relations in "expr".
3278 static struct pet_expr *embed(struct pet_expr *expr, __isl_keep isl_map *map)
3280 return pet_expr_foreach_access(expr, &embed_access, map);
3283 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
3285 static int n_nested_parameter(__isl_keep isl_set *set)
3287 isl_space *space;
3288 int n;
3290 space = isl_set_get_space(set);
3291 n = n_nested_parameter(space);
3292 isl_space_free(space);
3294 return n;
3297 /* Remove all parameters from "map" that refer to nested accesses.
3299 static __isl_give isl_map *remove_nested_parameters(__isl_take isl_map *map)
3301 int nparam;
3302 isl_space *space;
3304 space = isl_map_get_space(map);
3305 nparam = isl_space_dim(space, isl_dim_param);
3306 for (int i = nparam - 1; i >= 0; --i)
3307 if (is_nested_parameter(space, i))
3308 map = isl_map_project_out(map, isl_dim_param, i, 1);
3309 isl_space_free(space);
3311 return map;
3314 extern "C" {
3315 static __isl_give isl_map *access_remove_nested_parameters(
3316 __isl_take isl_map *access, void *user);
3319 static __isl_give isl_map *access_remove_nested_parameters(
3320 __isl_take isl_map *access, void *user)
3322 return remove_nested_parameters(access);
3325 /* Remove all nested access parameters from the schedule and all
3326 * accesses of "stmt".
3327 * There is no need to remove them from the domain as these parameters
3328 * have already been removed from the domain when this function is called.
3330 static struct pet_stmt *remove_nested_parameters(struct pet_stmt *stmt)
3332 if (!stmt)
3333 return NULL;
3334 stmt->schedule = remove_nested_parameters(stmt->schedule);
3335 stmt->body = pet_expr_foreach_access(stmt->body,
3336 &access_remove_nested_parameters, NULL);
3337 if (!stmt->schedule || !stmt->body)
3338 goto error;
3339 for (int i = 0; i < stmt->n_arg; ++i) {
3340 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
3341 &access_remove_nested_parameters, NULL);
3342 if (!stmt->args[i])
3343 goto error;
3346 return stmt;
3347 error:
3348 pet_stmt_free(stmt);
3349 return NULL;
3352 /* For each nested access parameter in the domain of "stmt",
3353 * construct a corresponding pet_expr, place it before the original
3354 * elements in stmt->args and record its position in "param2pos".
3355 * n is the number of nested access parameters.
3357 struct pet_stmt *PetScan::extract_nested(struct pet_stmt *stmt, int n,
3358 std::map<int,int> &param2pos)
3360 int i;
3361 isl_space *space;
3362 int n_arg;
3363 struct pet_expr **args;
3365 n_arg = stmt->n_arg;
3366 args = isl_calloc_array(ctx, struct pet_expr *, n + n_arg);
3367 if (!args)
3368 goto error;
3370 space = isl_set_get_space(stmt->domain);
3371 n_arg = extract_nested(space, 0, args, param2pos);
3372 isl_space_free(space);
3374 if (n_arg < 0)
3375 goto error;
3377 for (i = 0; i < stmt->n_arg; ++i)
3378 args[n_arg + i] = stmt->args[i];
3379 free(stmt->args);
3380 stmt->args = args;
3381 stmt->n_arg += n_arg;
3383 return stmt;
3384 error:
3385 if (args) {
3386 for (i = 0; i < n; ++i)
3387 pet_expr_free(args[i]);
3388 free(args);
3390 pet_stmt_free(stmt);
3391 return NULL;
3394 /* Check whether any of the arguments i of "stmt" starting at position "n"
3395 * is equal to one of the first "n" arguments j.
3396 * If so, combine the constraints on arguments i and j and remove
3397 * argument i.
3399 static struct pet_stmt *remove_duplicate_arguments(struct pet_stmt *stmt, int n)
3401 int i, j;
3402 isl_map *map;
3404 if (!stmt)
3405 return NULL;
3406 if (n == 0)
3407 return stmt;
3408 if (n == stmt->n_arg)
3409 return stmt;
3411 map = isl_set_unwrap(stmt->domain);
3413 for (i = stmt->n_arg - 1; i >= n; --i) {
3414 for (j = 0; j < n; ++j)
3415 if (pet_expr_is_equal(stmt->args[i], stmt->args[j]))
3416 break;
3417 if (j >= n)
3418 continue;
3420 map = isl_map_equate(map, isl_dim_out, i, isl_dim_out, j);
3421 map = isl_map_project_out(map, isl_dim_out, i, 1);
3423 pet_expr_free(stmt->args[i]);
3424 for (j = i; j + 1 < stmt->n_arg; ++j)
3425 stmt->args[j] = stmt->args[j + 1];
3426 stmt->n_arg--;
3429 stmt->domain = isl_map_wrap(map);
3430 if (!stmt->domain)
3431 goto error;
3432 return stmt;
3433 error:
3434 pet_stmt_free(stmt);
3435 return NULL;
3438 /* Look for parameters in the iteration domain of "stmt" that
3439 * refer to nested accesses. In particular, these are
3440 * parameters with no name.
3442 * If there are any such parameters, then as many extra variables
3443 * (after identifying identical nested accesses) are inserted in the
3444 * range of the map wrapped inside the domain, before the original variables.
3445 * If the original domain is not a wrapped map, then a new wrapped
3446 * map is created with zero output dimensions.
3447 * The parameters are then equated to the corresponding output dimensions
3448 * and subsequently projected out, from the iteration domain,
3449 * the schedule and the access relations.
3450 * For each of the output dimensions, a corresponding argument
3451 * expression is inserted. Initially they are created with
3452 * a zero-dimensional domain, so they have to be embedded
3453 * in the current iteration domain.
3454 * param2pos maps the position of the parameter to the position
3455 * of the corresponding output dimension in the wrapped map.
3457 struct pet_stmt *PetScan::resolve_nested(struct pet_stmt *stmt)
3459 int n;
3460 int nparam;
3461 unsigned n_arg;
3462 isl_map *map;
3463 std::map<int,int> param2pos;
3465 if (!stmt)
3466 return NULL;
3468 n = n_nested_parameter(stmt->domain);
3469 if (n == 0)
3470 return stmt;
3472 n_arg = stmt->n_arg;
3473 stmt = extract_nested(stmt, n, param2pos);
3474 if (!stmt)
3475 return NULL;
3477 n = stmt->n_arg - n_arg;
3478 nparam = isl_set_dim(stmt->domain, isl_dim_param);
3479 if (isl_set_is_wrapping(stmt->domain))
3480 map = isl_set_unwrap(stmt->domain);
3481 else
3482 map = isl_map_from_domain(stmt->domain);
3483 map = isl_map_insert_dims(map, isl_dim_out, 0, n);
3485 for (int i = nparam - 1; i >= 0; --i) {
3486 isl_id *id;
3488 if (!is_nested_parameter(map, i))
3489 continue;
3491 id = isl_map_get_tuple_id(stmt->args[param2pos[i]]->acc.access,
3492 isl_dim_out);
3493 map = isl_map_set_dim_id(map, isl_dim_out, param2pos[i], id);
3494 map = isl_map_equate(map, isl_dim_param, i, isl_dim_out,
3495 param2pos[i]);
3496 map = isl_map_project_out(map, isl_dim_param, i, 1);
3499 stmt->domain = isl_map_wrap(map);
3501 map = isl_set_unwrap(isl_set_copy(stmt->domain));
3502 map = isl_map_from_range(isl_map_domain(map));
3503 for (int pos = 0; pos < n; ++pos)
3504 stmt->args[pos] = embed(stmt->args[pos], map);
3505 isl_map_free(map);
3507 stmt = remove_nested_parameters(stmt);
3508 stmt = remove_duplicate_arguments(stmt, n);
3510 return stmt;
3511 error:
3512 pet_stmt_free(stmt);
3513 return NULL;
3516 /* For each statement in "scop", move the parameters that correspond
3517 * to nested access into the ranges of the domains and create
3518 * corresponding argument expressions.
3520 struct pet_scop *PetScan::resolve_nested(struct pet_scop *scop)
3522 if (!scop)
3523 return NULL;
3525 for (int i = 0; i < scop->n_stmt; ++i) {
3526 scop->stmts[i] = resolve_nested(scop->stmts[i]);
3527 if (!scop->stmts[i])
3528 goto error;
3531 return scop;
3532 error:
3533 pet_scop_free(scop);
3534 return NULL;
3537 /* Given an access expression "expr", is the variable accessed by
3538 * "expr" assigned anywhere inside "scop"?
3540 static bool is_assigned(pet_expr *expr, pet_scop *scop)
3542 bool assigned = false;
3543 isl_id *id;
3545 id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
3546 assigned = pet_scop_writes(scop, id);
3547 isl_id_free(id);
3549 return assigned;
3552 /* Are all nested access parameters in "pa" allowed given "scop".
3553 * In particular, is none of them written by anywhere inside "scop".
3555 * If "scop" has any skip conditions, then no nested access parameters
3556 * are allowed. In particular, if there is any nested access in a guard
3557 * for a piece of code containing a "continue", then we want to introduce
3558 * a separate statement for evaluating this guard so that we can express
3559 * that the result is false for all previous iterations.
3561 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff *pa, pet_scop *scop)
3563 int nparam;
3565 if (!scop)
3566 return true;
3568 nparam = isl_pw_aff_dim(pa, isl_dim_param);
3569 for (int i = 0; i < nparam; ++i) {
3570 Expr *nested;
3571 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
3572 pet_expr *expr;
3573 bool allowed;
3575 if (!is_nested_parameter(id)) {
3576 isl_id_free(id);
3577 continue;
3580 if (pet_scop_has_skip(scop, pet_skip_now)) {
3581 isl_id_free(id);
3582 return false;
3585 nested = (Expr *) isl_id_get_user(id);
3586 expr = extract_expr(nested);
3587 allowed = expr && expr->type == pet_expr_access &&
3588 !is_assigned(expr, scop);
3590 pet_expr_free(expr);
3591 isl_id_free(id);
3593 if (!allowed)
3594 return false;
3597 return true;
3600 /* Do we need to construct a skip condition of the given type
3601 * on an if statement, given that the if condition is non-affine?
3603 * pet_scop_filter_skip can only handle the case where the if condition
3604 * holds (the then branch) and the skip condition is universal.
3605 * In any other case, we need to construct a new skip condition.
3607 static bool need_skip(struct pet_scop *scop_then, struct pet_scop *scop_else,
3608 bool have_else, enum pet_skip type)
3610 if (have_else && scop_else && pet_scop_has_skip(scop_else, type))
3611 return true;
3612 if (scop_then && pet_scop_has_skip(scop_then, type) &&
3613 !pet_scop_has_universal_skip(scop_then, type))
3614 return true;
3615 return false;
3618 /* Do we need to construct a skip condition of the given type
3619 * on an if statement, given that the if condition is affine?
3621 * There is no need to construct a new skip condition if all
3622 * the skip conditions are affine.
3624 static bool need_skip_aff(struct pet_scop *scop_then,
3625 struct pet_scop *scop_else, bool have_else, enum pet_skip type)
3627 if (scop_then && pet_scop_has_var_skip(scop_then, type))
3628 return true;
3629 if (have_else && scop_else && pet_scop_has_var_skip(scop_else, type))
3630 return true;
3631 return false;
3634 /* Do we need to construct a skip condition of the given type
3635 * on an if statement?
3637 static bool need_skip(struct pet_scop *scop_then, struct pet_scop *scop_else,
3638 bool have_else, enum pet_skip type, bool affine)
3640 if (affine)
3641 return need_skip_aff(scop_then, scop_else, have_else, type);
3642 else
3643 return need_skip(scop_then, scop_else, have_else, type);
3646 /* Construct an affine expression pet_expr that is evaluates
3647 * to the constant "val".
3649 static struct pet_expr *universally(isl_ctx *ctx, int val)
3651 isl_space *space;
3652 isl_map *map;
3654 space = isl_space_alloc(ctx, 0, 0, 1);
3655 map = isl_map_universe(space);
3656 map = isl_map_fix_si(map, isl_dim_out, 0, val);
3658 return pet_expr_from_access(map);
3661 /* Construct an affine expression pet_expr that is evaluates
3662 * to the constant 1.
3664 static struct pet_expr *universally_true(isl_ctx *ctx)
3666 return universally(ctx, 1);
3669 /* Construct an affine expression pet_expr that is evaluates
3670 * to the constant 0.
3672 static struct pet_expr *universally_false(isl_ctx *ctx)
3674 return universally(ctx, 0);
3677 /* Given an access relation "test_access" for the if condition,
3678 * an access relation "skip_access" for the skip condition and
3679 * scops for the then and else branches, construct a scop for
3680 * computing "skip_access".
3682 * The computed scop contains a single statement that essentially does
3684 * skip_cond = test_cond ? skip_cond_then : skip_cond_else
3686 * If the skip conditions of the then and/or else branch are not affine,
3687 * then they need to be filtered by test_access.
3688 * If they are missing, then this means the skip condition is false.
3690 * Since we are constructing a skip condition for the if statement,
3691 * the skip conditions on the then and else branches are removed.
3693 static struct pet_scop *extract_skip(PetScan *scan,
3694 __isl_take isl_map *test_access, __isl_take isl_map *skip_access,
3695 struct pet_scop *scop_then, struct pet_scop *scop_else, bool have_else,
3696 enum pet_skip type)
3698 struct pet_expr *expr_then, *expr_else, *expr, *expr_skip;
3699 struct pet_stmt *stmt;
3700 struct pet_scop *scop;
3701 isl_ctx *ctx = scan->ctx;
3703 if (!scop_then)
3704 goto error;
3705 if (have_else && !scop_else)
3706 goto error;
3708 if (pet_scop_has_skip(scop_then, type)) {
3709 expr_then = pet_scop_get_skip_expr(scop_then, type);
3710 pet_scop_reset_skip(scop_then, type);
3711 if (!pet_expr_is_affine(expr_then))
3712 expr_then = pet_expr_filter(expr_then,
3713 isl_map_copy(test_access), 1);
3714 } else
3715 expr_then = universally_false(ctx);
3717 if (have_else && pet_scop_has_skip(scop_else, type)) {
3718 expr_else = pet_scop_get_skip_expr(scop_else, type);
3719 pet_scop_reset_skip(scop_else, type);
3720 if (!pet_expr_is_affine(expr_else))
3721 expr_else = pet_expr_filter(expr_else,
3722 isl_map_copy(test_access), 0);
3723 } else
3724 expr_else = universally_false(ctx);
3726 expr = pet_expr_from_access(test_access);
3727 expr = pet_expr_new_ternary(ctx, expr, expr_then, expr_else);
3728 expr_skip = pet_expr_from_access(isl_map_copy(skip_access));
3729 if (expr_skip) {
3730 expr_skip->acc.write = 1;
3731 expr_skip->acc.read = 0;
3733 expr = pet_expr_new_binary(ctx, pet_op_assign, expr_skip, expr);
3734 stmt = pet_stmt_from_pet_expr(ctx, -1, NULL, scan->n_stmt++, expr);
3736 scop = pet_scop_from_pet_stmt(ctx, stmt);
3737 scop = scop_add_array(scop, skip_access, scan->ast_context);
3738 isl_map_free(skip_access);
3740 return scop;
3741 error:
3742 isl_map_free(test_access);
3743 isl_map_free(skip_access);
3744 return NULL;
3747 /* Is scop's skip_now condition equal to its skip_later condition?
3748 * In particular, this means that it either has no skip_now condition
3749 * or both a skip_now and a skip_later condition (that are equal to each other).
3751 static bool skip_equals_skip_later(struct pet_scop *scop)
3753 int has_skip_now, has_skip_later;
3754 int equal;
3755 isl_set *skip_now, *skip_later;
3757 if (!scop)
3758 return false;
3759 has_skip_now = pet_scop_has_skip(scop, pet_skip_now);
3760 has_skip_later = pet_scop_has_skip(scop, pet_skip_later);
3761 if (has_skip_now != has_skip_later)
3762 return false;
3763 if (!has_skip_now)
3764 return true;
3766 skip_now = pet_scop_get_skip(scop, pet_skip_now);
3767 skip_later = pet_scop_get_skip(scop, pet_skip_later);
3768 equal = isl_set_is_equal(skip_now, skip_later);
3769 isl_set_free(skip_now);
3770 isl_set_free(skip_later);
3772 return equal;
3775 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
3777 static void drop_skip_later(struct pet_scop *scop1, struct pet_scop *scop2)
3779 pet_scop_reset_skip(scop1, pet_skip_later);
3780 pet_scop_reset_skip(scop2, pet_skip_later);
3783 /* Structure that handles the construction of skip conditions.
3785 * scop_then and scop_else represent the then and else branches
3786 * of the if statement
3788 * skip[type] is true if we need to construct a skip condition of that type
3789 * equal is set if the skip conditions of types pet_skip_now and pet_skip_later
3790 * are equal to each other
3791 * access[type] is the virtual array representing the skip condition
3792 * scop[type] is a scop for computing the skip condition
3794 struct pet_skip_info {
3795 isl_ctx *ctx;
3797 bool skip[2];
3798 bool equal;
3799 isl_map *access[2];
3800 struct pet_scop *scop[2];
3802 pet_skip_info(isl_ctx *ctx) : ctx(ctx) {}
3804 operator bool() { return skip[pet_skip_now] || skip[pet_skip_later]; }
3807 /* Structure that handles the construction of skip conditions on if statements.
3809 * scop_then and scop_else represent the then and else branches
3810 * of the if statement
3812 struct pet_skip_info_if : public pet_skip_info {
3813 struct pet_scop *scop_then, *scop_else;
3814 bool have_else;
3816 pet_skip_info_if(isl_ctx *ctx, struct pet_scop *scop_then,
3817 struct pet_scop *scop_else, bool have_else, bool affine);
3818 void extract(PetScan *scan, __isl_keep isl_map *access,
3819 enum pet_skip type);
3820 void extract(PetScan *scan, __isl_keep isl_map *access);
3821 void extract(PetScan *scan, __isl_keep isl_pw_aff *cond);
3822 struct pet_scop *add(struct pet_scop *scop, enum pet_skip type,
3823 int offset);
3824 struct pet_scop *add(struct pet_scop *scop, int offset);
3827 /* Initialize a pet_skip_info_if structure based on the then and else branches
3828 * and based on whether the if condition is affine or not.
3830 pet_skip_info_if::pet_skip_info_if(isl_ctx *ctx, struct pet_scop *scop_then,
3831 struct pet_scop *scop_else, bool have_else, bool affine) :
3832 pet_skip_info(ctx), scop_then(scop_then), scop_else(scop_else),
3833 have_else(have_else)
3835 skip[pet_skip_now] =
3836 need_skip(scop_then, scop_else, have_else, pet_skip_now, affine);
3837 equal = skip[pet_skip_now] && skip_equals_skip_later(scop_then) &&
3838 (!have_else || skip_equals_skip_later(scop_else));
3839 skip[pet_skip_later] = skip[pet_skip_now] && !equal &&
3840 need_skip(scop_then, scop_else, have_else, pet_skip_later, affine);
3843 /* If we need to construct a skip condition of the given type,
3844 * then do so now.
3846 * "map" represents the if condition.
3848 void pet_skip_info_if::extract(PetScan *scan, __isl_keep isl_map *map,
3849 enum pet_skip type)
3851 if (!skip[type])
3852 return;
3854 access[type] = create_test_access(isl_map_get_ctx(map), scan->n_test++);
3855 scop[type] = extract_skip(scan, isl_map_copy(map),
3856 isl_map_copy(access[type]),
3857 scop_then, scop_else, have_else, type);
3860 /* Construct the required skip conditions, given the if condition "map".
3862 void pet_skip_info_if::extract(PetScan *scan, __isl_keep isl_map *map)
3864 extract(scan, map, pet_skip_now);
3865 extract(scan, map, pet_skip_later);
3866 if (equal)
3867 drop_skip_later(scop_then, scop_else);
3870 /* Construct the required skip conditions, given the if condition "cond".
3872 void pet_skip_info_if::extract(PetScan *scan, __isl_keep isl_pw_aff *cond)
3874 isl_set *test_set;
3875 isl_map *test;
3877 if (!skip[pet_skip_now] && !skip[pet_skip_later])
3878 return;
3880 test_set = isl_set_from_pw_aff(isl_pw_aff_copy(cond));
3881 test = isl_map_from_range(test_set);
3882 extract(scan, test);
3883 isl_map_free(test);
3886 /* Add the computed skip condition of the give type to "main" and
3887 * add the scop for computing the condition at the given offset.
3889 * If equal is set, then we only computed a skip condition for pet_skip_now,
3890 * but we also need to set it as main's pet_skip_later.
3892 struct pet_scop *pet_skip_info_if::add(struct pet_scop *main,
3893 enum pet_skip type, int offset)
3895 isl_set *skip_set;
3897 if (!skip[type])
3898 return main;
3900 skip_set = isl_map_range(access[type]);
3901 access[type] = NULL;
3902 scop[type] = pet_scop_prefix(scop[type], offset);
3903 main = pet_scop_add_par(ctx, main, scop[type]);
3904 scop[type] = NULL;
3906 if (equal)
3907 main = pet_scop_set_skip(main, pet_skip_later,
3908 isl_set_copy(skip_set));
3910 main = pet_scop_set_skip(main, type, skip_set);
3912 return main;
3915 /* Add the computed skip conditions to "main" and
3916 * add the scops for computing the conditions at the given offset.
3918 struct pet_scop *pet_skip_info_if::add(struct pet_scop *scop, int offset)
3920 scop = add(scop, pet_skip_now, offset);
3921 scop = add(scop, pet_skip_later, offset);
3923 return scop;
3926 /* Construct a pet_scop for a non-affine if statement.
3928 * We create a separate statement that writes the result
3929 * of the non-affine condition to a virtual scalar.
3930 * A constraint requiring the value of this virtual scalar to be one
3931 * is added to the iteration domains of the then branch.
3932 * Similarly, a constraint requiring the value of this virtual scalar
3933 * to be zero is added to the iteration domains of the else branch, if any.
3934 * We adjust the schedules to ensure that the virtual scalar is written
3935 * before it is read.
3937 * If there are any breaks or continues in the then and/or else
3938 * branches, then we may have to compute a new skip condition.
3939 * This is handled using a pet_skip_info_if object.
3940 * On initialization, the object checks if skip conditions need
3941 * to be computed. If so, it does so in "extract" and adds them in "add".
3943 struct pet_scop *PetScan::extract_non_affine_if(Expr *cond,
3944 struct pet_scop *scop_then, struct pet_scop *scop_else,
3945 bool have_else, int stmt_id)
3947 struct pet_scop *scop;
3948 isl_map *test_access;
3949 int save_n_stmt = n_stmt;
3951 test_access = create_test_access(ctx, n_test++);
3952 n_stmt = stmt_id;
3953 scop = extract_non_affine_condition(cond, isl_map_copy(test_access));
3954 n_stmt = save_n_stmt;
3955 scop = scop_add_array(scop, test_access, ast_context);
3957 pet_skip_info_if skip(ctx, scop_then, scop_else, have_else, false);
3958 skip.extract(this, test_access);
3960 scop = pet_scop_prefix(scop, 0);
3961 scop_then = pet_scop_prefix(scop_then, 1);
3962 scop_then = pet_scop_filter(scop_then, isl_map_copy(test_access), 1);
3963 if (have_else) {
3964 scop_else = pet_scop_prefix(scop_else, 1);
3965 scop_else = pet_scop_filter(scop_else, test_access, 0);
3966 scop_then = pet_scop_add_par(ctx, scop_then, scop_else);
3967 } else
3968 isl_map_free(test_access);
3970 scop = pet_scop_add_seq(ctx, scop, scop_then);
3972 scop = skip.add(scop, 2);
3974 return scop;
3977 /* Construct a pet_scop for an if statement.
3979 * If the condition fits the pattern of a conditional assignment,
3980 * then it is handled by extract_conditional_assignment.
3981 * Otherwise, we do the following.
3983 * If the condition is affine, then the condition is added
3984 * to the iteration domains of the then branch, while the
3985 * opposite of the condition in added to the iteration domains
3986 * of the else branch, if any.
3987 * We allow the condition to be dynamic, i.e., to refer to
3988 * scalars or array elements that may be written to outside
3989 * of the given if statement. These nested accesses are then represented
3990 * as output dimensions in the wrapping iteration domain.
3991 * If it also written _inside_ the then or else branch, then
3992 * we treat the condition as non-affine.
3993 * As explained in extract_non_affine_if, this will introduce
3994 * an extra statement.
3995 * For aesthetic reasons, we want this statement to have a statement
3996 * number that is lower than those of the then and else branches.
3997 * In order to evaluate if will need such a statement, however, we
3998 * first construct scops for the then and else branches.
3999 * We therefore reserve a statement number if we might have to
4000 * introduce such an extra statement.
4002 * If the condition is not affine, then the scop is created in
4003 * extract_non_affine_if.
4005 * If there are any breaks or continues in the then and/or else
4006 * branches, then we may have to compute a new skip condition.
4007 * This is handled using a pet_skip_info_if object.
4008 * On initialization, the object checks if skip conditions need
4009 * to be computed. If so, it does so in "extract" and adds them in "add".
4011 struct pet_scop *PetScan::extract(IfStmt *stmt)
4013 struct pet_scop *scop_then, *scop_else = NULL, *scop;
4014 isl_pw_aff *cond;
4015 int stmt_id;
4016 isl_set *set;
4017 isl_set *valid;
4019 scop = extract_conditional_assignment(stmt);
4020 if (scop)
4021 return scop;
4023 cond = try_extract_nested_condition(stmt->getCond());
4024 if (allow_nested && (!cond || has_nested(cond)))
4025 stmt_id = n_stmt++;
4028 assigned_value_cache cache(assigned_value);
4029 scop_then = extract(stmt->getThen());
4032 if (stmt->getElse()) {
4033 assigned_value_cache cache(assigned_value);
4034 scop_else = extract(stmt->getElse());
4035 if (options->autodetect) {
4036 if (scop_then && !scop_else) {
4037 partial = true;
4038 isl_pw_aff_free(cond);
4039 return scop_then;
4041 if (!scop_then && scop_else) {
4042 partial = true;
4043 isl_pw_aff_free(cond);
4044 return scop_else;
4049 if (cond &&
4050 (!is_nested_allowed(cond, scop_then) ||
4051 (stmt->getElse() && !is_nested_allowed(cond, scop_else)))) {
4052 isl_pw_aff_free(cond);
4053 cond = NULL;
4055 if (allow_nested && !cond)
4056 return extract_non_affine_if(stmt->getCond(), scop_then,
4057 scop_else, stmt->getElse(), stmt_id);
4059 if (!cond)
4060 cond = extract_condition(stmt->getCond());
4062 pet_skip_info_if skip(ctx, scop_then, scop_else, stmt->getElse(), true);
4063 skip.extract(this, cond);
4065 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
4066 set = isl_pw_aff_non_zero_set(cond);
4067 scop = pet_scop_restrict(scop_then, isl_set_copy(set));
4069 if (stmt->getElse()) {
4070 set = isl_set_subtract(isl_set_copy(valid), set);
4071 scop_else = pet_scop_restrict(scop_else, set);
4072 scop = pet_scop_add_par(ctx, scop, scop_else);
4073 } else
4074 isl_set_free(set);
4075 scop = resolve_nested(scop);
4076 scop = pet_scop_restrict_context(scop, valid);
4078 if (skip)
4079 scop = pet_scop_prefix(scop, 0);
4080 scop = skip.add(scop, 1);
4082 return scop;
4085 /* Try and construct a pet_scop for a label statement.
4086 * We currently only allow labels on expression statements.
4088 struct pet_scop *PetScan::extract(LabelStmt *stmt)
4090 isl_id *label;
4091 Stmt *sub;
4093 sub = stmt->getSubStmt();
4094 if (!isa<Expr>(sub)) {
4095 unsupported(stmt);
4096 return NULL;
4099 label = isl_id_alloc(ctx, stmt->getName(), NULL);
4101 return extract(sub, extract_expr(cast<Expr>(sub)), label);
4104 /* Construct a pet_scop for a continue statement.
4106 * We simply create an empty scop with a universal pet_skip_now
4107 * skip condition. This skip condition will then be taken into
4108 * account by the enclosing loop construct, possibly after
4109 * being incorporated into outer skip conditions.
4111 struct pet_scop *PetScan::extract(ContinueStmt *stmt)
4113 pet_scop *scop;
4114 isl_space *space;
4115 isl_set *set;
4117 scop = pet_scop_empty(ctx);
4118 if (!scop)
4119 return NULL;
4121 space = isl_space_set_alloc(ctx, 0, 1);
4122 set = isl_set_universe(space);
4123 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
4124 scop = pet_scop_set_skip(scop, pet_skip_now, set);
4126 return scop;
4129 /* Construct a pet_scop for a break statement.
4131 * We simply create an empty scop with both a universal pet_skip_now
4132 * skip condition and a universal pet_skip_later skip condition.
4133 * These skip conditions will then be taken into
4134 * account by the enclosing loop construct, possibly after
4135 * being incorporated into outer skip conditions.
4137 struct pet_scop *PetScan::extract(BreakStmt *stmt)
4139 pet_scop *scop;
4140 isl_space *space;
4141 isl_set *set;
4143 scop = pet_scop_empty(ctx);
4144 if (!scop)
4145 return NULL;
4147 space = isl_space_set_alloc(ctx, 0, 1);
4148 set = isl_set_universe(space);
4149 set = isl_set_fix_si(set, isl_dim_set, 0, 1);
4150 scop = pet_scop_set_skip(scop, pet_skip_now, isl_set_copy(set));
4151 scop = pet_scop_set_skip(scop, pet_skip_later, set);
4153 return scop;
4156 /* Try and construct a pet_scop corresponding to "stmt".
4158 struct pet_scop *PetScan::extract(Stmt *stmt)
4160 if (isa<Expr>(stmt))
4161 return extract(stmt, extract_expr(cast<Expr>(stmt)));
4163 switch (stmt->getStmtClass()) {
4164 case Stmt::WhileStmtClass:
4165 return extract(cast<WhileStmt>(stmt));
4166 case Stmt::ForStmtClass:
4167 return extract_for(cast<ForStmt>(stmt));
4168 case Stmt::IfStmtClass:
4169 return extract(cast<IfStmt>(stmt));
4170 case Stmt::CompoundStmtClass:
4171 return extract(cast<CompoundStmt>(stmt));
4172 case Stmt::LabelStmtClass:
4173 return extract(cast<LabelStmt>(stmt));
4174 case Stmt::ContinueStmtClass:
4175 return extract(cast<ContinueStmt>(stmt));
4176 case Stmt::BreakStmtClass:
4177 return extract(cast<BreakStmt>(stmt));
4178 default:
4179 unsupported(stmt);
4182 return NULL;
4185 /* Do we need to construct a skip condition of the given type
4186 * on a sequence of statements?
4188 * There is no need to construct a new skip condition if only
4189 * only of the two statements has a skip condition or if both
4190 * of their skip conditions are affine.
4192 * In principle we also don't need a new continuation variable if
4193 * the continuation of scop2 is affine, but then we would need
4194 * to allow more complicated forms of continuations.
4196 static bool need_skip_seq(struct pet_scop *scop1, struct pet_scop *scop2,
4197 enum pet_skip type)
4199 if (!scop1 || !pet_scop_has_skip(scop1, type))
4200 return false;
4201 if (!scop2 || !pet_scop_has_skip(scop2, type))
4202 return false;
4203 if (pet_scop_has_affine_skip(scop1, type) &&
4204 pet_scop_has_affine_skip(scop2, type))
4205 return false;
4206 return true;
4209 /* Construct a scop for computing the skip condition of the given type and
4210 * with access relation "skip_access" for a sequence of two scops "scop1"
4211 * and "scop2".
4213 * The computed scop contains a single statement that essentially does
4215 * skip_cond = skip_cond_1 ? 1 : skip_cond_2
4217 * or, in other words, skip_cond1 || skip_cond2.
4218 * In this expression, skip_cond_2 is filtered to reflect that it is
4219 * only evaluated when skip_cond_1 is false.
4221 * The skip condition on scop1 is not removed because it still needs
4222 * to be applied to scop2 when these two scops are combined.
4224 static struct pet_scop *extract_skip_seq(PetScan *ps,
4225 __isl_take isl_map *skip_access,
4226 struct pet_scop *scop1, struct pet_scop *scop2, enum pet_skip type)
4228 isl_map *access;
4229 struct pet_expr *expr1, *expr2, *expr, *expr_skip;
4230 struct pet_stmt *stmt;
4231 struct pet_scop *scop;
4232 isl_ctx *ctx = ps->ctx;
4234 if (!scop1 || !scop2)
4235 goto error;
4237 expr1 = pet_scop_get_skip_expr(scop1, type);
4238 expr2 = pet_scop_get_skip_expr(scop2, type);
4239 pet_scop_reset_skip(scop2, type);
4241 expr2 = pet_expr_filter(expr2, isl_map_copy(expr1->acc.access), 0);
4243 expr = universally_true(ctx);
4244 expr = pet_expr_new_ternary(ctx, expr1, expr, expr2);
4245 expr_skip = pet_expr_from_access(isl_map_copy(skip_access));
4246 if (expr_skip) {
4247 expr_skip->acc.write = 1;
4248 expr_skip->acc.read = 0;
4250 expr = pet_expr_new_binary(ctx, pet_op_assign, expr_skip, expr);
4251 stmt = pet_stmt_from_pet_expr(ctx, -1, NULL, ps->n_stmt++, expr);
4253 scop = pet_scop_from_pet_stmt(ctx, stmt);
4254 scop = scop_add_array(scop, skip_access, ps->ast_context);
4255 isl_map_free(skip_access);
4257 return scop;
4258 error:
4259 isl_map_free(skip_access);
4260 return NULL;
4263 /* Structure that handles the construction of skip conditions
4264 * on sequences of statements.
4266 * scop1 and scop2 represent the two statements that are combined
4268 struct pet_skip_info_seq : public pet_skip_info {
4269 struct pet_scop *scop1, *scop2;
4271 pet_skip_info_seq(isl_ctx *ctx, struct pet_scop *scop1,
4272 struct pet_scop *scop2);
4273 void extract(PetScan *scan, enum pet_skip type);
4274 void extract(PetScan *scan);
4275 struct pet_scop *add(struct pet_scop *scop, enum pet_skip type,
4276 int offset);
4277 struct pet_scop *add(struct pet_scop *scop, int offset);
4280 /* Initialize a pet_skip_info_seq structure based on
4281 * on the two statements that are going to be combined.
4283 pet_skip_info_seq::pet_skip_info_seq(isl_ctx *ctx, struct pet_scop *scop1,
4284 struct pet_scop *scop2) : pet_skip_info(ctx), scop1(scop1), scop2(scop2)
4286 skip[pet_skip_now] = need_skip_seq(scop1, scop2, pet_skip_now);
4287 equal = skip[pet_skip_now] && skip_equals_skip_later(scop1) &&
4288 skip_equals_skip_later(scop2);
4289 skip[pet_skip_later] = skip[pet_skip_now] && !equal &&
4290 need_skip_seq(scop1, scop2, pet_skip_later);
4293 /* If we need to construct a skip condition of the given type,
4294 * then do so now.
4296 void pet_skip_info_seq::extract(PetScan *scan, enum pet_skip type)
4298 if (!skip[type])
4299 return;
4301 access[type] = create_test_access(ctx, scan->n_test++);
4302 scop[type] = extract_skip_seq(scan, isl_map_copy(access[type]),
4303 scop1, scop2, type);
4306 /* Construct the required skip conditions.
4308 void pet_skip_info_seq::extract(PetScan *scan)
4310 extract(scan, pet_skip_now);
4311 extract(scan, pet_skip_later);
4312 if (equal)
4313 drop_skip_later(scop1, scop2);
4316 /* Add the computed skip condition of the give type to "main" and
4317 * add the scop for computing the condition at the given offset (the statement
4318 * number). Within this offset, the condition is computed at position 1
4319 * to ensure that it is computed after the corresponding statement.
4321 * If equal is set, then we only computed a skip condition for pet_skip_now,
4322 * but we also need to set it as main's pet_skip_later.
4324 struct pet_scop *pet_skip_info_seq::add(struct pet_scop *main,
4325 enum pet_skip type, int offset)
4327 isl_set *skip_set;
4329 if (!skip[type])
4330 return main;
4332 skip_set = isl_map_range(access[type]);
4333 access[type] = NULL;
4334 scop[type] = pet_scop_prefix(scop[type], 1);
4335 scop[type] = pet_scop_prefix(scop[type], offset);
4336 main = pet_scop_add_par(ctx, main, scop[type]);
4337 scop[type] = NULL;
4339 if (equal)
4340 main = pet_scop_set_skip(main, pet_skip_later,
4341 isl_set_copy(skip_set));
4343 main = pet_scop_set_skip(main, type, skip_set);
4345 return main;
4348 /* Add the computed skip conditions to "main" and
4349 * add the scops for computing the conditions at the given offset.
4351 struct pet_scop *pet_skip_info_seq::add(struct pet_scop *scop, int offset)
4353 scop = add(scop, pet_skip_now, offset);
4354 scop = add(scop, pet_skip_later, offset);
4356 return scop;
4359 /* Try and construct a pet_scop corresponding to (part of)
4360 * a sequence of statements.
4362 * If there are any breaks or continues in the individual statements,
4363 * then we may have to compute a new skip condition.
4364 * This is handled using a pet_skip_info_seq object.
4365 * On initialization, the object checks if skip conditions need
4366 * to be computed. If so, it does so in "extract" and adds them in "add".
4368 struct pet_scop *PetScan::extract(StmtRange stmt_range)
4370 pet_scop *scop;
4371 StmtIterator i;
4372 int j;
4373 bool partial_range = false;
4375 scop = pet_scop_empty(ctx);
4376 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j) {
4377 Stmt *child = *i;
4378 struct pet_scop *scop_i;
4380 scop_i = extract(child);
4381 if (scop && partial) {
4382 pet_scop_free(scop_i);
4383 break;
4385 pet_skip_info_seq skip(ctx, scop, scop_i);
4386 skip.extract(this);
4387 if (skip)
4388 scop_i = pet_scop_prefix(scop_i, 0);
4389 scop_i = pet_scop_prefix(scop_i, j);
4390 if (options->autodetect) {
4391 if (scop_i)
4392 scop = pet_scop_add_seq(ctx, scop, scop_i);
4393 else
4394 partial_range = true;
4395 if (scop->n_stmt != 0 && !scop_i)
4396 partial = true;
4397 } else {
4398 scop = pet_scop_add_seq(ctx, scop, scop_i);
4401 scop = skip.add(scop, j);
4403 if (partial)
4404 break;
4407 if (scop && partial_range)
4408 partial = true;
4410 return scop;
4413 /* Return the file offset of the expansion location of "Loc".
4415 static unsigned getExpansionOffset(SourceManager &SM, SourceLocation Loc)
4417 return SM.getFileOffset(SM.getExpansionLoc(Loc));
4420 /* Check if the scop marked by the user is exactly this Stmt
4421 * or part of this Stmt.
4422 * If so, return a pet_scop corresponding to the marked region.
4423 * Otherwise, return NULL.
4425 struct pet_scop *PetScan::scan(Stmt *stmt)
4427 SourceManager &SM = PP.getSourceManager();
4428 unsigned start_off, end_off;
4430 start_off = getExpansionOffset(SM, stmt->getLocStart());
4431 end_off = getExpansionOffset(SM, stmt->getLocEnd());
4433 if (start_off > loc.end)
4434 return NULL;
4435 if (end_off < loc.start)
4436 return NULL;
4437 if (start_off >= loc.start && end_off <= loc.end) {
4438 return extract(stmt);
4441 StmtIterator start;
4442 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
4443 Stmt *child = *start;
4444 if (!child)
4445 continue;
4446 start_off = getExpansionOffset(SM, child->getLocStart());
4447 end_off = getExpansionOffset(SM, child->getLocEnd());
4448 if (start_off < loc.start && end_off > loc.end)
4449 return scan(child);
4450 if (start_off >= loc.start)
4451 break;
4454 StmtIterator end;
4455 for (end = start; end != stmt->child_end(); ++end) {
4456 Stmt *child = *end;
4457 start_off = SM.getFileOffset(child->getLocStart());
4458 if (start_off >= loc.end)
4459 break;
4462 return extract(StmtRange(start, end));
4465 /* Set the size of index "pos" of "array" to "size".
4466 * In particular, add a constraint of the form
4468 * i_pos < size
4470 * to array->extent and a constraint of the form
4472 * size >= 0
4474 * to array->context.
4476 static struct pet_array *update_size(struct pet_array *array, int pos,
4477 __isl_take isl_pw_aff *size)
4479 isl_set *valid;
4480 isl_set *univ;
4481 isl_set *bound;
4482 isl_space *dim;
4483 isl_aff *aff;
4484 isl_pw_aff *index;
4485 isl_id *id;
4487 valid = isl_pw_aff_nonneg_set(isl_pw_aff_copy(size));
4488 array->context = isl_set_intersect(array->context, valid);
4490 dim = isl_set_get_space(array->extent);
4491 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
4492 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
4493 univ = isl_set_universe(isl_aff_get_domain_space(aff));
4494 index = isl_pw_aff_alloc(univ, aff);
4496 size = isl_pw_aff_add_dims(size, isl_dim_in,
4497 isl_set_dim(array->extent, isl_dim_set));
4498 id = isl_set_get_tuple_id(array->extent);
4499 size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
4500 bound = isl_pw_aff_lt_set(index, size);
4502 array->extent = isl_set_intersect(array->extent, bound);
4504 if (!array->context || !array->extent)
4505 goto error;
4507 return array;
4508 error:
4509 pet_array_free(array);
4510 return NULL;
4513 /* Figure out the size of the array at position "pos" and all
4514 * subsequent positions from "type" and update "array" accordingly.
4516 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
4517 const Type *type, int pos)
4519 const ArrayType *atype;
4520 isl_pw_aff *size;
4522 if (!array)
4523 return NULL;
4525 if (type->isPointerType()) {
4526 type = type->getPointeeType().getTypePtr();
4527 return set_upper_bounds(array, type, pos + 1);
4529 if (!type->isArrayType())
4530 return array;
4532 type = type->getCanonicalTypeInternal().getTypePtr();
4533 atype = cast<ArrayType>(type);
4535 if (type->isConstantArrayType()) {
4536 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
4537 size = extract_affine(ca->getSize());
4538 array = update_size(array, pos, size);
4539 } else if (type->isVariableArrayType()) {
4540 const VariableArrayType *vla = cast<VariableArrayType>(atype);
4541 size = extract_affine(vla->getSizeExpr());
4542 array = update_size(array, pos, size);
4545 type = atype->getElementType().getTypePtr();
4547 return set_upper_bounds(array, type, pos + 1);
4550 /* Is "T" the type of a variable length array with static size?
4552 static bool is_vla_with_static_size(QualType T)
4554 const VariableArrayType *vlatype;
4556 if (!T->isVariableArrayType())
4557 return false;
4558 vlatype = cast<VariableArrayType>(T);
4559 return vlatype->getSizeModifier() == VariableArrayType::Static;
4562 /* Return the type of "decl" as an array.
4564 * In particular, if "decl" is a parameter declaration that
4565 * is a variable length array with a static size, then
4566 * return the original type (i.e., the variable length array).
4567 * Otherwise, return the type of decl.
4569 static QualType get_array_type(ValueDecl *decl)
4571 ParmVarDecl *parm;
4572 QualType T;
4574 parm = dyn_cast<ParmVarDecl>(decl);
4575 if (!parm)
4576 return decl->getType();
4578 T = parm->getOriginalType();
4579 if (!is_vla_with_static_size(T))
4580 return decl->getType();
4581 return T;
4584 /* Construct and return a pet_array corresponding to the variable "decl".
4585 * In particular, initialize array->extent to
4587 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
4589 * and then call set_upper_bounds to set the upper bounds on the indices
4590 * based on the type of the variable.
4592 struct pet_array *PetScan::extract_array(isl_ctx *ctx, ValueDecl *decl)
4594 struct pet_array *array;
4595 QualType qt = get_array_type(decl);
4596 const Type *type = qt.getTypePtr();
4597 int depth = array_depth(type);
4598 QualType base = base_type(qt);
4599 string name;
4600 isl_id *id;
4601 isl_space *dim;
4603 array = isl_calloc_type(ctx, struct pet_array);
4604 if (!array)
4605 return NULL;
4607 id = isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
4608 dim = isl_space_set_alloc(ctx, 0, depth);
4609 dim = isl_space_set_tuple_id(dim, isl_dim_set, id);
4611 array->extent = isl_set_nat_universe(dim);
4613 dim = isl_space_params_alloc(ctx, 0);
4614 array->context = isl_set_universe(dim);
4616 array = set_upper_bounds(array, type, 0);
4617 if (!array)
4618 return NULL;
4620 name = base.getAsString();
4621 array->element_type = strdup(name.c_str());
4622 array->element_size = decl->getASTContext().getTypeInfo(base).first / 8;
4624 return array;
4627 /* Construct a list of pet_arrays, one for each array (or scalar)
4628 * accessed inside "scop", add this list to "scop" and return the result.
4630 * The context of "scop" is updated with the intersection of
4631 * the contexts of all arrays, i.e., constraints on the parameters
4632 * that ensure that the arrays have a valid (non-negative) size.
4634 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop)
4636 int i;
4637 set<ValueDecl *> arrays;
4638 set<ValueDecl *>::iterator it;
4639 int n_array;
4640 struct pet_array **scop_arrays;
4642 if (!scop)
4643 return NULL;
4645 pet_scop_collect_arrays(scop, arrays);
4646 if (arrays.size() == 0)
4647 return scop;
4649 n_array = scop->n_array;
4651 scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
4652 n_array + arrays.size());
4653 if (!scop_arrays)
4654 goto error;
4655 scop->arrays = scop_arrays;
4657 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
4658 struct pet_array *array;
4659 scop->arrays[n_array + i] = array = extract_array(ctx, *it);
4660 if (!scop->arrays[n_array + i])
4661 goto error;
4662 scop->n_array++;
4663 scop->context = isl_set_intersect(scop->context,
4664 isl_set_copy(array->context));
4665 if (!scop->context)
4666 goto error;
4669 return scop;
4670 error:
4671 pet_scop_free(scop);
4672 return NULL;
4675 /* Bound all parameters in scop->context to the possible values
4676 * of the corresponding C variable.
4678 static struct pet_scop *add_parameter_bounds(struct pet_scop *scop)
4680 int n;
4682 if (!scop)
4683 return NULL;
4685 n = isl_set_dim(scop->context, isl_dim_param);
4686 for (int i = 0; i < n; ++i) {
4687 isl_id *id;
4688 ValueDecl *decl;
4690 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
4691 if (is_nested_parameter(id)) {
4692 isl_id_free(id);
4693 isl_die(isl_set_get_ctx(scop->context),
4694 isl_error_internal,
4695 "unresolved nested parameter", goto error);
4697 decl = (ValueDecl *) isl_id_get_user(id);
4698 isl_id_free(id);
4700 scop->context = set_parameter_bounds(scop->context, i, decl);
4702 if (!scop->context)
4703 goto error;
4706 return scop;
4707 error:
4708 pet_scop_free(scop);
4709 return NULL;
4712 /* Construct a pet_scop from the given function.
4714 struct pet_scop *PetScan::scan(FunctionDecl *fd)
4716 pet_scop *scop;
4717 Stmt *stmt;
4719 stmt = fd->getBody();
4721 if (options->autodetect)
4722 scop = extract(stmt);
4723 else
4724 scop = scan(stmt);
4725 scop = pet_scop_detect_parameter_accesses(scop);
4726 scop = scan_arrays(scop);
4727 scop = add_parameter_bounds(scop);
4728 scop = pet_scop_gist(scop, value_bounds);
4730 return scop;