PetScan::scan(FunctionDecl *): construct a pet_context up front
[pet.git] / scan.cc
blob4868d620b80e17bed73f1ea5f85270a86b195978
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 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 <string.h>
36 #include <set>
37 #include <map>
38 #include <iostream>
39 #include <llvm/Support/raw_ostream.h>
40 #include <clang/AST/ASTContext.h>
41 #include <clang/AST/ASTDiagnostic.h>
42 #include <clang/AST/Expr.h>
43 #include <clang/AST/RecursiveASTVisitor.h>
45 #include <isl/id.h>
46 #include <isl/space.h>
47 #include <isl/aff.h>
48 #include <isl/set.h>
50 #include "aff.h"
51 #include "array.h"
52 #include "clang.h"
53 #include "context.h"
54 #include "expr.h"
55 #include "expr_arg.h"
56 #include "nest.h"
57 #include "options.h"
58 #include "scan.h"
59 #include "scop.h"
60 #include "scop_plus.h"
61 #include "skip.h"
63 #include "config.h"
65 using namespace std;
66 using namespace clang;
68 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
70 switch (kind) {
71 case UO_Minus:
72 return pet_op_minus;
73 case UO_Not:
74 return pet_op_not;
75 case UO_LNot:
76 return pet_op_lnot;
77 case UO_PostInc:
78 return pet_op_post_inc;
79 case UO_PostDec:
80 return pet_op_post_dec;
81 case UO_PreInc:
82 return pet_op_pre_inc;
83 case UO_PreDec:
84 return pet_op_pre_dec;
85 default:
86 return pet_op_last;
90 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
92 switch (kind) {
93 case BO_AddAssign:
94 return pet_op_add_assign;
95 case BO_SubAssign:
96 return pet_op_sub_assign;
97 case BO_MulAssign:
98 return pet_op_mul_assign;
99 case BO_DivAssign:
100 return pet_op_div_assign;
101 case BO_Assign:
102 return pet_op_assign;
103 case BO_Add:
104 return pet_op_add;
105 case BO_Sub:
106 return pet_op_sub;
107 case BO_Mul:
108 return pet_op_mul;
109 case BO_Div:
110 return pet_op_div;
111 case BO_Rem:
112 return pet_op_mod;
113 case BO_Shl:
114 return pet_op_shl;
115 case BO_Shr:
116 return pet_op_shr;
117 case BO_EQ:
118 return pet_op_eq;
119 case BO_NE:
120 return pet_op_ne;
121 case BO_LE:
122 return pet_op_le;
123 case BO_GE:
124 return pet_op_ge;
125 case BO_LT:
126 return pet_op_lt;
127 case BO_GT:
128 return pet_op_gt;
129 case BO_And:
130 return pet_op_and;
131 case BO_Xor:
132 return pet_op_xor;
133 case BO_Or:
134 return pet_op_or;
135 case BO_LAnd:
136 return pet_op_land;
137 case BO_LOr:
138 return pet_op_lor;
139 default:
140 return pet_op_last;
144 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
145 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
147 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
148 SourceLocation(), var, false, var->getInnerLocStart(),
149 var->getType(), VK_LValue);
151 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
152 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
154 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
155 SourceLocation(), var, var->getInnerLocStart(), var->getType(),
156 VK_LValue);
158 #else
159 static DeclRefExpr *create_DeclRefExpr(VarDecl *var)
161 return DeclRefExpr::Create(var->getASTContext(), var->getQualifierLoc(),
162 var, var->getInnerLocStart(), var->getType(), VK_LValue);
164 #endif
166 /* Check if the element type corresponding to the given array type
167 * has a const qualifier.
169 static bool const_base(QualType qt)
171 const Type *type = qt.getTypePtr();
173 if (type->isPointerType())
174 return const_base(type->getPointeeType());
175 if (type->isArrayType()) {
176 const ArrayType *atype;
177 type = type->getCanonicalTypeInternal().getTypePtr();
178 atype = cast<ArrayType>(type);
179 return const_base(atype->getElementType());
182 return qt.isConstQualified();
185 /* Create an isl_id that refers to the named declarator "decl".
187 static __isl_give isl_id *create_decl_id(isl_ctx *ctx, NamedDecl *decl)
189 return isl_id_alloc(ctx, decl->getName().str().c_str(), decl);
192 /* Look for any assignments to scalar variables in part of the parse
193 * tree and mark them as having an unknown value in "pc".
194 * If the address of a scalar variable is being taken, then mark
195 * it as having an unknown value as well. As an exception,
196 * if the address is passed to a function
197 * that is declared to receive a const pointer, then "pc" is not updated.
199 * This ensures that we won't use any previously stored value
200 * in the current subtree and its parents.
202 struct clear_assignments : RecursiveASTVisitor<clear_assignments> {
203 isl_ctx *ctx;
204 pet_context *&pc;
205 set<UnaryOperator *> skip;
207 clear_assignments(pet_context *&pc) : pc(pc),
208 ctx(pet_context_get_ctx(pc)) {}
210 /* Check for "address of" operators whose value is passed
211 * to a const pointer argument and add them to "skip", so that
212 * we can skip them in VisitUnaryOperator.
214 bool VisitCallExpr(CallExpr *expr) {
215 FunctionDecl *fd;
216 fd = expr->getDirectCallee();
217 if (!fd)
218 return true;
219 for (int i = 0; i < expr->getNumArgs(); ++i) {
220 Expr *arg = expr->getArg(i);
221 UnaryOperator *op;
222 if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) {
223 ImplicitCastExpr *ice;
224 ice = cast<ImplicitCastExpr>(arg);
225 arg = ice->getSubExpr();
227 if (arg->getStmtClass() != Stmt::UnaryOperatorClass)
228 continue;
229 op = cast<UnaryOperator>(arg);
230 if (op->getOpcode() != UO_AddrOf)
231 continue;
232 if (const_base(fd->getParamDecl(i)->getType()))
233 skip.insert(op);
235 return true;
238 bool VisitUnaryOperator(UnaryOperator *expr) {
239 Expr *arg;
240 DeclRefExpr *ref;
241 ValueDecl *decl;
243 switch (expr->getOpcode()) {
244 case UO_AddrOf:
245 case UO_PostInc:
246 case UO_PostDec:
247 case UO_PreInc:
248 case UO_PreDec:
249 break;
250 default:
251 return true;
253 if (skip.find(expr) != skip.end())
254 return true;
256 arg = expr->getSubExpr();
257 if (arg->getStmtClass() != Stmt::DeclRefExprClass)
258 return true;
259 ref = cast<DeclRefExpr>(arg);
260 decl = ref->getDecl();
261 pc = pet_context_mark_assigned(pc, create_decl_id(ctx, decl));
262 return true;
265 bool VisitBinaryOperator(BinaryOperator *expr) {
266 Expr *lhs;
267 DeclRefExpr *ref;
268 ValueDecl *decl;
270 if (!expr->isAssignmentOp())
271 return true;
272 lhs = expr->getLHS();
273 if (lhs->getStmtClass() != Stmt::DeclRefExprClass)
274 return true;
275 ref = cast<DeclRefExpr>(lhs);
276 decl = ref->getDecl();
277 pc = pet_context_mark_assigned(pc, create_decl_id(ctx, decl));
278 return true;
282 PetScan::~PetScan()
284 isl_union_map_free(value_bounds);
287 /* Report a diagnostic, unless autodetect is set.
289 void PetScan::report(Stmt *stmt, unsigned id)
291 if (options->autodetect)
292 return;
294 SourceLocation loc = stmt->getLocStart();
295 DiagnosticsEngine &diag = PP.getDiagnostics();
296 DiagnosticBuilder B = diag.Report(loc, id) << stmt->getSourceRange();
299 /* Called if we found something we (currently) cannot handle.
300 * We'll provide more informative warnings later.
302 * We only actually complain if autodetect is false.
304 void PetScan::unsupported(Stmt *stmt)
306 DiagnosticsEngine &diag = PP.getDiagnostics();
307 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
308 "unsupported");
309 report(stmt, id);
312 /* Report a missing prototype, unless autodetect is set.
314 void PetScan::report_prototype_required(Stmt *stmt)
316 DiagnosticsEngine &diag = PP.getDiagnostics();
317 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
318 "prototype required");
319 report(stmt, id);
322 /* Report a missing increment, unless autodetect is set.
324 void PetScan::report_missing_increment(Stmt *stmt)
326 DiagnosticsEngine &diag = PP.getDiagnostics();
327 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
328 "missing increment");
329 report(stmt, id);
332 /* Extract an integer from "expr".
334 __isl_give isl_val *PetScan::extract_int(isl_ctx *ctx, IntegerLiteral *expr)
336 const Type *type = expr->getType().getTypePtr();
337 int is_signed = type->hasSignedIntegerRepresentation();
338 llvm::APInt val = expr->getValue();
339 int is_negative = is_signed && val.isNegative();
340 isl_val *v;
342 if (is_negative)
343 val = -val;
345 v = extract_unsigned(ctx, val);
347 if (is_negative)
348 v = isl_val_neg(v);
349 return v;
352 /* Extract an integer from "val", which is assumed to be non-negative.
354 __isl_give isl_val *PetScan::extract_unsigned(isl_ctx *ctx,
355 const llvm::APInt &val)
357 unsigned n;
358 const uint64_t *data;
360 data = val.getRawData();
361 n = val.getNumWords();
362 return isl_val_int_from_chunks(ctx, n, sizeof(uint64_t), data);
365 /* Extract an integer from "expr".
366 * Return NULL if "expr" does not (obviously) represent an integer.
368 __isl_give isl_val *PetScan::extract_int(clang::ParenExpr *expr)
370 return extract_int(expr->getSubExpr());
373 /* Extract an integer from "expr".
374 * Return NULL if "expr" does not (obviously) represent an integer.
376 __isl_give isl_val *PetScan::extract_int(clang::Expr *expr)
378 if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
379 return extract_int(ctx, cast<IntegerLiteral>(expr));
380 if (expr->getStmtClass() == Stmt::ParenExprClass)
381 return extract_int(cast<ParenExpr>(expr));
383 unsupported(expr);
384 return NULL;
387 /* Extract a pet_expr from the APInt "val", which is assumed
388 * to be non-negative.
390 __isl_give pet_expr *PetScan::extract_expr(const llvm::APInt &val)
392 return pet_expr_new_int(extract_unsigned(ctx, val));
395 /* Return the number of bits needed to represent the type "qt",
396 * if it is an integer type. Otherwise return 0.
397 * If qt is signed then return the opposite of the number of bits.
399 static int get_type_size(QualType qt, ASTContext &ast_context)
401 int size;
403 if (!qt->isIntegerType())
404 return 0;
406 size = ast_context.getIntWidth(qt);
407 if (!qt->isUnsignedIntegerType())
408 size = -size;
410 return size;
413 /* Return the number of bits needed to represent the type of "decl",
414 * if it is an integer type. Otherwise return 0.
415 * If qt is signed then return the opposite of the number of bits.
417 static int get_type_size(ValueDecl *decl)
419 return get_type_size(decl->getType(), decl->getASTContext());
422 /* Bound parameter "pos" of "set" to the possible values of "decl".
424 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
425 unsigned pos, ValueDecl *decl)
427 int type_size;
428 isl_ctx *ctx;
429 isl_val *bound;
431 ctx = isl_set_get_ctx(set);
432 type_size = get_type_size(decl);
433 if (type_size == 0)
434 isl_die(ctx, isl_error_invalid, "not an integer type",
435 return isl_set_free(set));
436 if (type_size > 0) {
437 set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
438 bound = isl_val_int_from_ui(ctx, type_size);
439 bound = isl_val_2exp(bound);
440 bound = isl_val_sub_ui(bound, 1);
441 set = isl_set_upper_bound_val(set, isl_dim_param, pos, bound);
442 } else {
443 bound = isl_val_int_from_ui(ctx, -type_size - 1);
444 bound = isl_val_2exp(bound);
445 bound = isl_val_sub_ui(bound, 1);
446 set = isl_set_upper_bound_val(set, isl_dim_param, pos,
447 isl_val_copy(bound));
448 bound = isl_val_neg(bound);
449 bound = isl_val_sub_ui(bound, 1);
450 set = isl_set_lower_bound_val(set, isl_dim_param, pos, bound);
453 return set;
456 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
458 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
459 __isl_take isl_set *dom)
461 isl_pw_aff *pa;
462 pa = isl_set_indicator_function(set);
463 pa = isl_pw_aff_intersect_domain(pa, isl_set_coalesce(dom));
464 return pa;
467 /* Extract an affine expression, if possible, from "expr"
468 * within the context "pc", except that nesting is enabled.
469 * Otherwise return NULL.
471 __isl_give isl_pw_aff *PetScan::extract_affine(Expr *expr,
472 __isl_keep pet_context *pc)
474 pet_expr *pe;
475 isl_pw_aff *pa;
477 pe = extract_expr(expr);
478 if (!pe)
479 return NULL;
480 pe = pet_expr_plug_in_args(pe, pc);
481 pa = pet_expr_extract_affine(pe, pc);
482 if (isl_pw_aff_involves_nan(pa)) {
483 unsupported(expr);
484 pa = isl_pw_aff_free(pa);
486 pet_expr_free(pe);
488 return pa;
491 __isl_give pet_expr *PetScan::extract_index_expr(ImplicitCastExpr *expr)
493 return extract_index_expr(expr->getSubExpr());
496 /* Return the depth of an array of the given type.
498 static int array_depth(const Type *type)
500 if (type->isPointerType())
501 return 1 + array_depth(type->getPointeeType().getTypePtr());
502 if (type->isArrayType()) {
503 const ArrayType *atype;
504 type = type->getCanonicalTypeInternal().getTypePtr();
505 atype = cast<ArrayType>(type);
506 return 1 + array_depth(atype->getElementType().getTypePtr());
508 return 0;
511 /* Return the depth of the array accessed by the index expression "index".
512 * If "index" is an affine expression, i.e., if it does not access
513 * any array, then return 1.
514 * If "index" represent a member access, i.e., if its range is a wrapped
515 * relation, then return the sum of the depth of the array of structures
516 * and that of the member inside the structure.
518 static int extract_depth(__isl_keep isl_multi_pw_aff *index)
520 isl_id *id;
521 ValueDecl *decl;
523 if (!index)
524 return -1;
526 if (isl_multi_pw_aff_range_is_wrapping(index)) {
527 int domain_depth, range_depth;
528 isl_multi_pw_aff *domain, *range;
530 domain = isl_multi_pw_aff_copy(index);
531 domain = isl_multi_pw_aff_range_factor_domain(domain);
532 domain_depth = extract_depth(domain);
533 isl_multi_pw_aff_free(domain);
534 range = isl_multi_pw_aff_copy(index);
535 range = isl_multi_pw_aff_range_factor_range(range);
536 range_depth = extract_depth(range);
537 isl_multi_pw_aff_free(range);
539 return domain_depth + range_depth;
542 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
543 return 1;
545 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
546 if (!id)
547 return -1;
548 decl = (ValueDecl *) isl_id_get_user(id);
549 isl_id_free(id);
551 return array_depth(decl->getType().getTypePtr());
554 /* Return the depth of the array accessed by the access expression "expr".
556 static int extract_depth(__isl_keep pet_expr *expr)
558 isl_multi_pw_aff *index;
559 int depth;
561 index = pet_expr_access_get_index(expr);
562 depth = extract_depth(index);
563 isl_multi_pw_aff_free(index);
565 return depth;
568 /* Construct a pet_expr representing an index expression for an access
569 * to the variable referenced by "expr".
571 __isl_give pet_expr *PetScan::extract_index_expr(DeclRefExpr *expr)
573 return extract_index_expr(expr->getDecl());
576 /* Construct a pet_expr representing an index expression for an access
577 * to the variable "decl".
579 __isl_give pet_expr *PetScan::extract_index_expr(ValueDecl *decl)
581 isl_id *id = create_decl_id(ctx, decl);
582 isl_space *space = isl_space_alloc(ctx, 0, 0, 0);
584 space = isl_space_set_tuple_id(space, isl_dim_out, id);
586 return pet_expr_from_index(isl_multi_pw_aff_zero(space));
589 /* Construct a pet_expr representing the index expression "expr"
590 * Return NULL on error.
592 __isl_give pet_expr *PetScan::extract_index_expr(Expr *expr)
594 switch (expr->getStmtClass()) {
595 case Stmt::ImplicitCastExprClass:
596 return extract_index_expr(cast<ImplicitCastExpr>(expr));
597 case Stmt::DeclRefExprClass:
598 return extract_index_expr(cast<DeclRefExpr>(expr));
599 case Stmt::ArraySubscriptExprClass:
600 return extract_index_expr(cast<ArraySubscriptExpr>(expr));
601 case Stmt::IntegerLiteralClass:
602 return extract_expr(cast<IntegerLiteral>(expr));
603 case Stmt::MemberExprClass:
604 return extract_index_expr(cast<MemberExpr>(expr));
605 default:
606 unsupported(expr);
608 return NULL;
611 /* Extract an index expression from the given array subscript expression.
613 * We first extract an index expression from the base.
614 * This will result in an index expression with a range that corresponds
615 * to the earlier indices.
616 * We then extract the current index and let
617 * pet_expr_access_subscript combine the two.
619 __isl_give pet_expr *PetScan::extract_index_expr(ArraySubscriptExpr *expr)
621 Expr *base = expr->getBase();
622 Expr *idx = expr->getIdx();
623 pet_expr *index;
624 pet_expr *base_expr;
626 base_expr = extract_index_expr(base);
627 index = extract_expr(idx);
629 base_expr = pet_expr_access_subscript(base_expr, index);
631 return base_expr;
634 /* Extract an index expression from a member expression.
636 * If the base access (to the structure containing the member)
637 * is of the form
639 * A[..]
641 * and the member is called "f", then the member access is of
642 * the form
644 * A_f[A[..] -> f[]]
646 * If the member access is to an anonymous struct, then simply return
648 * A[..]
650 * If the member access in the source code is of the form
652 * A->f
654 * then it is treated as
656 * A[0].f
658 __isl_give pet_expr *PetScan::extract_index_expr(MemberExpr *expr)
660 Expr *base = expr->getBase();
661 FieldDecl *field = cast<FieldDecl>(expr->getMemberDecl());
662 pet_expr *base_index;
663 isl_id *id;
665 base_index = extract_index_expr(base);
667 if (expr->isArrow()) {
668 pet_expr *index = pet_expr_new_int(isl_val_zero(ctx));
669 base_index = pet_expr_access_subscript(base_index, index);
672 if (field->isAnonymousStructOrUnion())
673 return base_index;
675 id = create_decl_id(ctx, field);
677 return pet_expr_access_member(base_index, id);
680 /* Check if "expr" calls function "minmax" with two arguments and if so
681 * make lhs and rhs refer to these two arguments.
683 static bool is_minmax(Expr *expr, const char *minmax, Expr *&lhs, Expr *&rhs)
685 CallExpr *call;
686 FunctionDecl *fd;
687 string name;
689 if (expr->getStmtClass() != Stmt::CallExprClass)
690 return false;
692 call = cast<CallExpr>(expr);
693 fd = call->getDirectCallee();
694 if (!fd)
695 return false;
697 if (call->getNumArgs() != 2)
698 return false;
700 name = fd->getDeclName().getAsString();
701 if (name != minmax)
702 return false;
704 lhs = call->getArg(0);
705 rhs = call->getArg(1);
707 return true;
710 /* Check if "expr" is of the form min(lhs, rhs) and if so make
711 * lhs and rhs refer to the two arguments.
713 static bool is_min(Expr *expr, Expr *&lhs, Expr *&rhs)
715 return is_minmax(expr, "min", lhs, rhs);
718 /* Check if "expr" is of the form max(lhs, rhs) and if so make
719 * lhs and rhs refer to the two arguments.
721 static bool is_max(Expr *expr, Expr *&lhs, Expr *&rhs)
723 return is_minmax(expr, "max", lhs, rhs);
726 /* Extract an affine expressions representing the comparison "LHS op RHS"
727 * within the context "pc".
728 * "comp" is the original statement that "LHS op RHS" is derived from
729 * and is used for diagnostics.
731 * If the comparison is of the form
733 * a <= min(b,c)
735 * then the expression is constructed as the conjunction of
736 * the comparisons
738 * a <= b and a <= c
740 * A similar optimization is performed for max(a,b) <= c.
741 * We do this because that will lead to simpler representations
742 * of the expression.
743 * If isl is ever enhanced to explicitly deal with min and max expressions,
744 * this optimization can be removed.
746 __isl_give isl_pw_aff *PetScan::extract_comparison(BinaryOperatorKind op,
747 Expr *LHS, Expr *RHS, Stmt *comp, __isl_keep pet_context *pc)
749 isl_pw_aff *lhs;
750 isl_pw_aff *rhs;
751 isl_pw_aff *res;
752 isl_set *cond;
753 isl_set *dom;
754 enum pet_op_type type;
756 if (op == BO_GT)
757 return extract_comparison(BO_LT, RHS, LHS, comp, pc);
758 if (op == BO_GE)
759 return extract_comparison(BO_LE, RHS, LHS, comp, pc);
761 if (op == BO_LT || op == BO_LE) {
762 Expr *expr1, *expr2;
763 if (is_min(RHS, expr1, expr2)) {
764 lhs = extract_comparison(op, LHS, expr1, comp, pc);
765 rhs = extract_comparison(op, LHS, expr2, comp, pc);
766 return pet_and(lhs, rhs);
768 if (is_max(LHS, expr1, expr2)) {
769 lhs = extract_comparison(op, expr1, RHS, comp, pc);
770 rhs = extract_comparison(op, expr2, RHS, comp, pc);
771 return pet_and(lhs, rhs);
775 lhs = extract_affine(LHS, pc);
776 rhs = extract_affine(RHS, pc);
778 type = BinaryOperatorKind2pet_op_type(op);
779 return pet_comparison(type, lhs, rhs);
782 __isl_give isl_pw_aff *PetScan::extract_comparison(BinaryOperator *comp,
783 __isl_keep pet_context *pc)
785 return extract_comparison(comp->getOpcode(), comp->getLHS(),
786 comp->getRHS(), comp, pc);
789 /* Extract an affine expression from a boolean expression
790 * within the context "pc".
791 * In particular, return the expression "expr ? 1 : 0".
792 * Return NULL if we are unable to extract an affine expression.
794 * We first convert the clang::Expr to a pet_expr and
795 * then extract an affine expression from that pet_expr.
797 __isl_give isl_pw_aff *PetScan::extract_condition(Expr *expr,
798 __isl_keep pet_context *pc)
800 isl_pw_aff *cond;
801 pet_expr *pe;
803 if (!expr) {
804 isl_set *u = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
805 return indicator_function(u, isl_set_copy(u));
808 pe = extract_expr(expr);
809 pe = pet_expr_plug_in_args(pe, pc);
810 pc = pet_context_copy(pc);
811 pc = pet_context_set_allow_nested(pc, nesting_enabled);
812 cond = pet_expr_extract_affine_condition(pe, pc);
813 if (isl_pw_aff_involves_nan(cond))
814 cond = isl_pw_aff_free(cond);
815 pet_context_free(pc);
816 pet_expr_free(pe);
817 return cond;
820 /* Mark the given access pet_expr as a write.
822 static __isl_give pet_expr *mark_write(__isl_take pet_expr *access)
824 access = pet_expr_access_set_write(access, 1);
825 access = pet_expr_access_set_read(access, 0);
827 return access;
830 /* Construct a pet_expr representing a unary operator expression.
832 __isl_give pet_expr *PetScan::extract_expr(UnaryOperator *expr)
834 pet_expr *arg;
835 enum pet_op_type op;
837 op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
838 if (op == pet_op_last) {
839 unsupported(expr);
840 return NULL;
843 arg = extract_expr(expr->getSubExpr());
845 if (expr->isIncrementDecrementOp() &&
846 pet_expr_get_type(arg) == pet_expr_access) {
847 arg = mark_write(arg);
848 arg = pet_expr_access_set_read(arg, 1);
851 return pet_expr_new_unary(op, arg);
854 /* If the access expression "expr" writes to a (non-virtual) scalar,
855 * then mark the scalar as having an unknown value in "pc".
857 static int clear_write(__isl_keep pet_expr *expr, void *user)
859 isl_id *id;
860 pet_context **pc = (pet_context **) user;
862 if (!pet_expr_access_is_write(expr))
863 return 0;
864 if (!pet_expr_is_scalar_access(expr))
865 return 0;
867 id = pet_expr_access_get_id(expr);
868 if (isl_id_get_user(id))
869 *pc = pet_context_mark_assigned(*pc, id);
870 else
871 isl_id_free(id);
873 return 0;
876 /* Update "pc" by taking into account the writes in "stmt".
877 * That is, first mark all scalar variables that are written by "stmt"
878 * as having an unknown value. Afterwards,
879 * if "stmt" is a top-level (i.e., unconditional) assignment
880 * to a scalar variable, then update "pc" accordingly.
882 * In particular, if the lhs of the assignment is a scalar variable, then mark
883 * the variable as having been assigned. If, furthermore, the rhs
884 * is an affine expression, then keep track of this value in "pc"
885 * so that we can plug it in when we later come across the same variable.
887 * We skip assignments to virtual arrays (those with NULL user pointer).
889 __isl_give pet_context *PetScan::handle_writes(struct pet_stmt *stmt,
890 __isl_take pet_context *pc)
892 pet_expr *body = stmt->body;
893 pet_expr *arg;
894 isl_id *id;
895 isl_pw_aff *pa;
897 if (pet_expr_foreach_access_expr(body, &clear_write, &pc) < 0)
898 return pet_context_free(pc);
900 if (!pet_stmt_is_assign(stmt))
901 return pc;
902 if (!isl_set_plain_is_universe(stmt->domain))
903 return pc;
904 arg = pet_expr_get_arg(body, 0);
905 if (!pet_expr_is_scalar_access(arg)) {
906 pet_expr_free(arg);
907 return pc;
910 id = pet_expr_access_get_id(arg);
911 pet_expr_free(arg);
913 if (!isl_id_get_user(id)) {
914 isl_id_free(id);
915 return pc;
918 arg = pet_expr_get_arg(body, 1);
919 pa = pet_expr_extract_affine(arg, pc);
920 pc = pet_context_mark_assigned(pc, isl_id_copy(id));
921 pet_expr_free(arg);
923 if (isl_pw_aff_involves_nan(pa))
924 pa = isl_pw_aff_free(pa);
925 if (!pa) {
926 isl_id_free(id);
927 return pc;
930 pc = pet_context_set_value(pc, id, pa);
932 return pc;
935 /* Update "pc" based on the write accesses (and, in particular,
936 * assignments) in "scop".
938 __isl_give pet_context *PetScan::handle_writes(struct pet_scop *scop,
939 __isl_take pet_context *pc)
941 if (!scop)
942 return pet_context_free(pc);
943 for (int i = 0; i < scop->n_stmt; ++i)
944 pc = handle_writes(scop->stmts[i], pc);
946 return pc;
949 /* Construct a pet_expr representing a binary operator expression.
951 * If the top level operator is an assignment and the LHS is an access,
952 * then we mark that access as a write. If the operator is a compound
953 * assignment, the access is marked as both a read and a write.
955 __isl_give pet_expr *PetScan::extract_expr(BinaryOperator *expr)
957 int type_size;
958 pet_expr *lhs, *rhs;
959 enum pet_op_type op;
961 op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
962 if (op == pet_op_last) {
963 unsupported(expr);
964 return NULL;
967 lhs = extract_expr(expr->getLHS());
968 rhs = extract_expr(expr->getRHS());
970 if (expr->isAssignmentOp() &&
971 pet_expr_get_type(lhs) == pet_expr_access) {
972 lhs = mark_write(lhs);
973 if (expr->isCompoundAssignmentOp())
974 lhs = pet_expr_access_set_read(lhs, 1);
977 type_size = get_type_size(expr->getType(), ast_context);
978 return pet_expr_new_binary(type_size, op, lhs, rhs);
981 /* Construct a pet_scop with a single statement killing the entire
982 * array "array" within the context "pc".
984 struct pet_scop *PetScan::kill(Stmt *stmt, struct pet_array *array,
985 __isl_keep pet_context *pc)
987 isl_id *id;
988 isl_space *space;
989 isl_multi_pw_aff *index;
990 isl_map *access;
991 pet_expr *expr;
993 if (!array)
994 return NULL;
995 access = isl_map_from_range(isl_set_copy(array->extent));
996 id = isl_set_get_tuple_id(array->extent);
997 space = isl_space_alloc(ctx, 0, 0, 0);
998 space = isl_space_set_tuple_id(space, isl_dim_out, id);
999 index = isl_multi_pw_aff_zero(space);
1000 expr = pet_expr_kill_from_access_and_index(access, index);
1001 return extract(expr, stmt->getSourceRange(), false, pc);
1004 /* Construct a pet_scop for a (single) variable declaration
1005 * within the context "pc".
1007 * The scop contains the variable being declared (as an array)
1008 * and a statement killing the array.
1010 * If the variable is initialized in the AST, then the scop
1011 * also contains an assignment to the variable.
1013 struct pet_scop *PetScan::extract(DeclStmt *stmt, __isl_keep pet_context *pc)
1015 int type_size;
1016 Decl *decl;
1017 VarDecl *vd;
1018 pet_expr *lhs, *rhs, *pe;
1019 struct pet_scop *scop_decl, *scop;
1020 struct pet_array *array;
1022 if (!stmt->isSingleDecl()) {
1023 unsupported(stmt);
1024 return NULL;
1027 decl = stmt->getSingleDecl();
1028 vd = cast<VarDecl>(decl);
1030 array = extract_array(ctx, vd, NULL, pc);
1031 if (array)
1032 array->declared = 1;
1033 scop_decl = kill(stmt, array, pc);
1034 scop_decl = pet_scop_add_array(scop_decl, array);
1036 if (!vd->getInit())
1037 return scop_decl;
1039 lhs = extract_access_expr(vd);
1040 rhs = extract_expr(vd->getInit());
1042 lhs = mark_write(lhs);
1044 type_size = get_type_size(vd->getType(), ast_context);
1045 pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
1046 scop = extract(pe, stmt->getSourceRange(), false, pc);
1048 scop_decl = pet_scop_prefix(scop_decl, 0);
1049 scop = pet_scop_prefix(scop, 1);
1051 scop = pet_scop_add_seq(ctx, scop_decl, scop);
1053 return scop;
1056 /* Construct a pet_expr representing a conditional operation.
1058 __isl_give pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
1060 pet_expr *cond, *lhs, *rhs;
1061 isl_pw_aff *pa;
1063 cond = extract_expr(expr->getCond());
1064 lhs = extract_expr(expr->getTrueExpr());
1065 rhs = extract_expr(expr->getFalseExpr());
1067 return pet_expr_new_ternary(cond, lhs, rhs);
1070 __isl_give pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
1072 return extract_expr(expr->getSubExpr());
1075 /* Construct a pet_expr representing a floating point value.
1077 * If the floating point literal does not appear in a macro,
1078 * then we use the original representation in the source code
1079 * as the string representation. Otherwise, we use the pretty
1080 * printer to produce a string representation.
1082 __isl_give pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
1084 double d;
1085 string s;
1086 const LangOptions &LO = PP.getLangOpts();
1087 SourceLocation loc = expr->getLocation();
1089 if (!loc.isMacroID()) {
1090 SourceManager &SM = PP.getSourceManager();
1091 unsigned len = Lexer::MeasureTokenLength(loc, SM, LO);
1092 s = string(SM.getCharacterData(loc), len);
1093 } else {
1094 llvm::raw_string_ostream S(s);
1095 expr->printPretty(S, 0, PrintingPolicy(LO));
1096 S.str();
1098 d = expr->getValueAsApproximateDouble();
1099 return pet_expr_new_double(ctx, d, s.c_str());
1102 /* Convert the index expression "index" into an access pet_expr of type "qt".
1104 __isl_give pet_expr *PetScan::extract_access_expr(QualType qt,
1105 __isl_take pet_expr *index)
1107 int depth;
1108 int type_size;
1110 depth = extract_depth(index);
1111 type_size = get_type_size(qt, ast_context);
1113 index = pet_expr_set_type_size(index, type_size);
1114 index = pet_expr_access_set_depth(index, depth);
1116 return index;
1119 /* Extract an index expression from "expr" and then convert it into
1120 * an access pet_expr.
1122 __isl_give pet_expr *PetScan::extract_access_expr(Expr *expr)
1124 return extract_access_expr(expr->getType(), extract_index_expr(expr));
1127 /* Extract an index expression from "decl" and then convert it into
1128 * an access pet_expr.
1130 __isl_give pet_expr *PetScan::extract_access_expr(ValueDecl *decl)
1132 return extract_access_expr(decl->getType(), extract_index_expr(decl));
1135 __isl_give pet_expr *PetScan::extract_expr(ParenExpr *expr)
1137 return extract_expr(expr->getSubExpr());
1140 /* Extract an assume statement from the argument "expr"
1141 * of a __pencil_assume statement.
1143 __isl_give pet_expr *PetScan::extract_assume(Expr *expr)
1145 return pet_expr_new_unary(pet_op_assume, extract_expr(expr));
1148 /* Construct a pet_expr corresponding to the function call argument "expr".
1149 * The argument appears in position "pos" of a call to function "fd".
1151 * If we are passing along a pointer to an array element
1152 * or an entire row or even higher dimensional slice of an array,
1153 * then the function being called may write into the array.
1155 * We assume here that if the function is declared to take a pointer
1156 * to a const type, then the function will perform a read
1157 * and that otherwise, it will perform a write.
1159 __isl_give pet_expr *PetScan::extract_argument(FunctionDecl *fd, int pos,
1160 Expr *expr)
1162 pet_expr *res;
1163 int is_addr = 0, is_partial = 0;
1164 Stmt::StmtClass sc;
1166 if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) {
1167 ImplicitCastExpr *ice = cast<ImplicitCastExpr>(expr);
1168 expr = ice->getSubExpr();
1170 if (expr->getStmtClass() == Stmt::UnaryOperatorClass) {
1171 UnaryOperator *op = cast<UnaryOperator>(expr);
1172 if (op->getOpcode() == UO_AddrOf) {
1173 is_addr = 1;
1174 expr = op->getSubExpr();
1177 res = extract_expr(expr);
1178 if (!res)
1179 return NULL;
1180 sc = expr->getStmtClass();
1181 if ((sc == Stmt::ArraySubscriptExprClass ||
1182 sc == Stmt::MemberExprClass) &&
1183 array_depth(expr->getType().getTypePtr()) > 0)
1184 is_partial = 1;
1185 if ((is_addr || is_partial) &&
1186 pet_expr_get_type(res) == pet_expr_access) {
1187 ParmVarDecl *parm;
1188 if (!fd->hasPrototype()) {
1189 report_prototype_required(expr);
1190 return pet_expr_free(res);
1192 parm = fd->getParamDecl(pos);
1193 if (!const_base(parm->getType()))
1194 res = mark_write(res);
1197 if (is_addr)
1198 res = pet_expr_new_unary(pet_op_address_of, res);
1199 return res;
1202 /* Construct a pet_expr representing a function call.
1204 * In the special case of a "call" to __pencil_assume,
1205 * construct an assume expression instead.
1207 __isl_give pet_expr *PetScan::extract_expr(CallExpr *expr)
1209 pet_expr *res = NULL;
1210 FunctionDecl *fd;
1211 string name;
1212 unsigned n_arg;
1214 fd = expr->getDirectCallee();
1215 if (!fd) {
1216 unsupported(expr);
1217 return NULL;
1220 name = fd->getDeclName().getAsString();
1221 n_arg = expr->getNumArgs();
1223 if (n_arg == 1 && name == "__pencil_assume")
1224 return extract_assume(expr->getArg(0));
1226 res = pet_expr_new_call(ctx, name.c_str(), n_arg);
1227 if (!res)
1228 return NULL;
1230 for (int i = 0; i < n_arg; ++i) {
1231 Expr *arg = expr->getArg(i);
1232 res = pet_expr_set_arg(res, i,
1233 PetScan::extract_argument(fd, i, arg));
1236 return res;
1239 /* Construct a pet_expr representing a (C style) cast.
1241 __isl_give pet_expr *PetScan::extract_expr(CStyleCastExpr *expr)
1243 pet_expr *arg;
1244 QualType type;
1246 arg = extract_expr(expr->getSubExpr());
1247 if (!arg)
1248 return NULL;
1250 type = expr->getTypeAsWritten();
1251 return pet_expr_new_cast(type.getAsString().c_str(), arg);
1254 /* Construct a pet_expr representing an integer.
1256 __isl_give pet_expr *PetScan::extract_expr(IntegerLiteral *expr)
1258 return pet_expr_new_int(extract_int(expr));
1261 /* Try and construct a pet_expr representing "expr".
1263 __isl_give pet_expr *PetScan::extract_expr(Expr *expr)
1265 switch (expr->getStmtClass()) {
1266 case Stmt::UnaryOperatorClass:
1267 return extract_expr(cast<UnaryOperator>(expr));
1268 case Stmt::CompoundAssignOperatorClass:
1269 case Stmt::BinaryOperatorClass:
1270 return extract_expr(cast<BinaryOperator>(expr));
1271 case Stmt::ImplicitCastExprClass:
1272 return extract_expr(cast<ImplicitCastExpr>(expr));
1273 case Stmt::ArraySubscriptExprClass:
1274 case Stmt::DeclRefExprClass:
1275 case Stmt::MemberExprClass:
1276 return extract_access_expr(expr);
1277 case Stmt::IntegerLiteralClass:
1278 return extract_expr(cast<IntegerLiteral>(expr));
1279 case Stmt::FloatingLiteralClass:
1280 return extract_expr(cast<FloatingLiteral>(expr));
1281 case Stmt::ParenExprClass:
1282 return extract_expr(cast<ParenExpr>(expr));
1283 case Stmt::ConditionalOperatorClass:
1284 return extract_expr(cast<ConditionalOperator>(expr));
1285 case Stmt::CallExprClass:
1286 return extract_expr(cast<CallExpr>(expr));
1287 case Stmt::CStyleCastExprClass:
1288 return extract_expr(cast<CStyleCastExpr>(expr));
1289 default:
1290 unsupported(expr);
1292 return NULL;
1295 /* Check if the given initialization statement is an assignment.
1296 * If so, return that assignment. Otherwise return NULL.
1298 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1300 BinaryOperator *ass;
1302 if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1303 return NULL;
1305 ass = cast<BinaryOperator>(init);
1306 if (ass->getOpcode() != BO_Assign)
1307 return NULL;
1309 return ass;
1312 /* Check if the given initialization statement is a declaration
1313 * of a single variable.
1314 * If so, return that declaration. Otherwise return NULL.
1316 Decl *PetScan::initialization_declaration(Stmt *init)
1318 DeclStmt *decl;
1320 if (init->getStmtClass() != Stmt::DeclStmtClass)
1321 return NULL;
1323 decl = cast<DeclStmt>(init);
1325 if (!decl->isSingleDecl())
1326 return NULL;
1328 return decl->getSingleDecl();
1331 /* Given the assignment operator in the initialization of a for loop,
1332 * extract the induction variable, i.e., the (integer)variable being
1333 * assigned.
1335 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1337 Expr *lhs;
1338 DeclRefExpr *ref;
1339 ValueDecl *decl;
1340 const Type *type;
1342 lhs = init->getLHS();
1343 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1344 unsupported(init);
1345 return NULL;
1348 ref = cast<DeclRefExpr>(lhs);
1349 decl = ref->getDecl();
1350 type = decl->getType().getTypePtr();
1352 if (!type->isIntegerType()) {
1353 unsupported(lhs);
1354 return NULL;
1357 return decl;
1360 /* Given the initialization statement of a for loop and the single
1361 * declaration in this initialization statement,
1362 * extract the induction variable, i.e., the (integer) variable being
1363 * declared.
1365 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1367 VarDecl *vd;
1369 vd = cast<VarDecl>(decl);
1371 const QualType type = vd->getType();
1372 if (!type->isIntegerType()) {
1373 unsupported(init);
1374 return NULL;
1377 if (!vd->getInit()) {
1378 unsupported(init);
1379 return NULL;
1382 return vd;
1385 /* Check that op is of the form iv++ or iv--.
1386 * Return a pet_expr representing "1" or "-1" accordingly.
1388 __isl_give pet_expr *PetScan::extract_unary_increment(
1389 clang::UnaryOperator *op, clang::ValueDecl *iv)
1391 Expr *sub;
1392 DeclRefExpr *ref;
1393 isl_val *v;
1395 if (!op->isIncrementDecrementOp()) {
1396 unsupported(op);
1397 return NULL;
1400 sub = op->getSubExpr();
1401 if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1402 unsupported(op);
1403 return NULL;
1406 ref = cast<DeclRefExpr>(sub);
1407 if (ref->getDecl() != iv) {
1408 unsupported(op);
1409 return NULL;
1412 if (op->isIncrementOp())
1413 v = isl_val_one(ctx);
1414 else
1415 v = isl_val_negone(ctx);
1417 return pet_expr_new_int(v);
1420 /* Check if op is of the form
1422 * iv = expr
1424 * and return the increment "expr - iv" as a pet_expr.
1426 __isl_give pet_expr *PetScan::extract_binary_increment(BinaryOperator *op,
1427 clang::ValueDecl *iv)
1429 int type_size;
1430 Expr *lhs;
1431 DeclRefExpr *ref;
1432 pet_expr *expr, *expr_iv;
1434 if (op->getOpcode() != BO_Assign) {
1435 unsupported(op);
1436 return NULL;
1439 lhs = op->getLHS();
1440 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1441 unsupported(op);
1442 return NULL;
1445 ref = cast<DeclRefExpr>(lhs);
1446 if (ref->getDecl() != iv) {
1447 unsupported(op);
1448 return NULL;
1451 expr = extract_expr(op->getRHS());
1452 expr_iv = extract_expr(lhs);
1454 type_size = get_type_size(iv->getType(), ast_context);
1455 return pet_expr_new_binary(type_size, pet_op_sub, expr, expr_iv);
1458 /* Check that op is of the form iv += cst or iv -= cst
1459 * and return a pet_expr corresponding to cst or -cst accordingly.
1461 __isl_give pet_expr *PetScan::extract_compound_increment(
1462 CompoundAssignOperator *op, clang::ValueDecl *iv)
1464 Expr *lhs;
1465 DeclRefExpr *ref;
1466 bool neg = false;
1467 pet_expr *expr;
1468 BinaryOperatorKind opcode;
1470 opcode = op->getOpcode();
1471 if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1472 unsupported(op);
1473 return NULL;
1475 if (opcode == BO_SubAssign)
1476 neg = true;
1478 lhs = op->getLHS();
1479 if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1480 unsupported(op);
1481 return NULL;
1484 ref = cast<DeclRefExpr>(lhs);
1485 if (ref->getDecl() != iv) {
1486 unsupported(op);
1487 return NULL;
1490 expr = extract_expr(op->getRHS());
1491 if (neg)
1492 expr = pet_expr_new_unary(pet_op_minus, expr);
1494 return expr;
1497 /* Check that the increment of the given for loop increments
1498 * (or decrements) the induction variable "iv" and return
1499 * the increment as a pet_expr if successful.
1501 __isl_give pet_expr *PetScan::extract_increment(clang::ForStmt *stmt,
1502 ValueDecl *iv)
1504 Stmt *inc = stmt->getInc();
1506 if (!inc) {
1507 report_missing_increment(stmt);
1508 return NULL;
1511 if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1512 return extract_unary_increment(cast<UnaryOperator>(inc), iv);
1513 if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1514 return extract_compound_increment(
1515 cast<CompoundAssignOperator>(inc), iv);
1516 if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1517 return extract_binary_increment(cast<BinaryOperator>(inc), iv);
1519 unsupported(inc);
1520 return NULL;
1523 /* Embed the given iteration domain in an extra outer loop
1524 * with induction variable "var".
1525 * If this variable appeared as a parameter in the constraints,
1526 * it is replaced by the new outermost dimension.
1528 static __isl_give isl_set *embed(__isl_take isl_set *set,
1529 __isl_take isl_id *var)
1531 int pos;
1533 set = isl_set_insert_dims(set, isl_dim_set, 0, 1);
1534 pos = isl_set_find_dim_by_id(set, isl_dim_param, var);
1535 if (pos >= 0) {
1536 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, 0);
1537 set = isl_set_project_out(set, isl_dim_param, pos, 1);
1540 isl_id_free(var);
1541 return set;
1544 /* Return those elements in the space of "cond" that come after
1545 * (based on "sign") an element in "cond".
1547 static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
1549 isl_map *previous_to_this;
1551 if (sign > 0)
1552 previous_to_this = isl_map_lex_lt(isl_set_get_space(cond));
1553 else
1554 previous_to_this = isl_map_lex_gt(isl_set_get_space(cond));
1556 cond = isl_set_apply(cond, previous_to_this);
1558 return cond;
1561 /* Create the infinite iteration domain
1563 * { [id] : id >= 0 }
1565 * If "scop" has an affine skip of type pet_skip_later,
1566 * then remove those iterations i that have an earlier iteration
1567 * where the skip condition is satisfied, meaning that iteration i
1568 * is not executed.
1569 * Since we are dealing with a loop without loop iterator,
1570 * the skip condition cannot refer to the current loop iterator and
1571 * so effectively, the returned set is of the form
1573 * { [0]; [id] : id >= 1 and not skip }
1575 static __isl_give isl_set *infinite_domain(__isl_take isl_id *id,
1576 struct pet_scop *scop)
1578 isl_ctx *ctx = isl_id_get_ctx(id);
1579 isl_set *domain;
1580 isl_set *skip;
1582 domain = isl_set_nat_universe(isl_space_set_alloc(ctx, 0, 1));
1583 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, id);
1585 if (!pet_scop_has_affine_skip(scop, pet_skip_later))
1586 return domain;
1588 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
1589 skip = embed(skip, isl_id_copy(id));
1590 skip = isl_set_intersect(skip , isl_set_copy(domain));
1591 domain = isl_set_subtract(domain, after(skip, 1));
1593 return domain;
1596 /* Create an identity affine expression on the space containing "domain",
1597 * which is assumed to be one-dimensional.
1599 static __isl_give isl_aff *identity_aff(__isl_keep isl_set *domain)
1601 isl_local_space *ls;
1603 ls = isl_local_space_from_space(isl_set_get_space(domain));
1604 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1607 /* Create an affine expression that maps elements
1608 * of a single-dimensional array "id_test" to the previous element
1609 * (according to "inc"), provided this element belongs to "domain".
1610 * That is, create the affine expression
1612 * { id[x] -> id[x - inc] : x - inc in domain }
1614 static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
1615 __isl_take isl_set *domain, __isl_take isl_val *inc)
1617 isl_space *space;
1618 isl_local_space *ls;
1619 isl_aff *aff;
1620 isl_multi_pw_aff *prev;
1622 space = isl_set_get_space(domain);
1623 ls = isl_local_space_from_space(space);
1624 aff = isl_aff_var_on_domain(ls, isl_dim_set, 0);
1625 aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
1626 prev = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1627 domain = isl_set_preimage_multi_pw_aff(domain,
1628 isl_multi_pw_aff_copy(prev));
1629 prev = isl_multi_pw_aff_intersect_domain(prev, domain);
1630 prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
1632 return prev;
1635 /* Add an implication to "scop" expressing that if an element of
1636 * virtual array "id_test" has value "satisfied" then all previous elements
1637 * of this array also have that value. The set of previous elements
1638 * is bounded by "domain". If "sign" is negative then the iterator
1639 * is decreasing and we express that all subsequent array elements
1640 * (but still defined previously) have the same value.
1642 static struct pet_scop *add_implication(struct pet_scop *scop,
1643 __isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
1644 int satisfied)
1646 isl_space *space;
1647 isl_map *map;
1649 domain = isl_set_set_tuple_id(domain, id_test);
1650 space = isl_set_get_space(domain);
1651 if (sign > 0)
1652 map = isl_map_lex_ge(space);
1653 else
1654 map = isl_map_lex_le(space);
1655 map = isl_map_intersect_range(map, domain);
1656 scop = pet_scop_add_implication(scop, map, satisfied);
1658 return scop;
1661 /* Add a filter to "scop" that imposes that it is only executed
1662 * when the variable identified by "id_test" has a zero value
1663 * for all previous iterations of "domain".
1665 * In particular, add a filter that imposes that the array
1666 * has a zero value at the previous iteration of domain and
1667 * add an implication that implies that it then has that
1668 * value for all previous iterations.
1670 static struct pet_scop *scop_add_break(struct pet_scop *scop,
1671 __isl_take isl_id *id_test, __isl_take isl_set *domain,
1672 __isl_take isl_val *inc)
1674 isl_multi_pw_aff *prev;
1675 int sign = isl_val_sgn(inc);
1677 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
1678 scop = add_implication(scop, id_test, domain, sign, 0);
1679 scop = pet_scop_filter(scop, prev, 0);
1681 return scop;
1684 /* Construct a pet_scop for an infinite loop around the given body
1685 * within the context "pc".
1687 * We extract a pet_scop for the body and then embed it in a loop with
1688 * iteration domain
1690 * { [t] : t >= 0 }
1692 * and schedule
1694 * { [t] -> [t] }
1696 * If the body contains any break, then it is taken into
1697 * account in infinite_domain (if the skip condition is affine)
1698 * or in scop_add_break (if the skip condition is not affine).
1700 * If we were only able to extract part of the body, then simply
1701 * return that part.
1703 struct pet_scop *PetScan::extract_infinite_loop(Stmt *body,
1704 __isl_keep pet_context *pc)
1706 isl_id *id, *id_test;
1707 isl_set *domain;
1708 isl_aff *ident;
1709 struct pet_scop *scop;
1710 bool has_var_break;
1712 scop = extract(body, pc);
1713 if (!scop)
1714 return NULL;
1715 if (partial)
1716 return scop;
1718 id = isl_id_alloc(ctx, "t", NULL);
1719 domain = infinite_domain(isl_id_copy(id), scop);
1720 ident = identity_aff(domain);
1722 has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
1723 if (has_var_break)
1724 id_test = pet_scop_get_skip_id(scop, pet_skip_later);
1726 scop = pet_scop_embed(scop, isl_set_copy(domain),
1727 isl_aff_copy(ident), ident, id);
1728 if (has_var_break)
1729 scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
1730 else
1731 isl_set_free(domain);
1733 return scop;
1736 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1738 * for (;;)
1739 * body
1741 * within the context "pc".
1743 struct pet_scop *PetScan::extract_infinite_for(ForStmt *stmt,
1744 __isl_keep pet_context *pc)
1746 struct pet_scop *scop;
1748 pc = pet_context_copy(pc);
1749 clear_assignments clear(pc);
1750 clear.TraverseStmt(stmt->getBody());
1752 scop = extract_infinite_loop(stmt->getBody(), pc);
1753 pet_context_free(pc);
1754 return scop;
1757 /* Add an array with the given extent (range of "index") to the list
1758 * of arrays in "scop" and return the extended pet_scop.
1759 * The array is marked as attaining values 0 and 1 only and
1760 * as each element being assigned at most once.
1762 static struct pet_scop *scop_add_array(struct pet_scop *scop,
1763 __isl_keep isl_multi_pw_aff *index, clang::ASTContext &ast_ctx)
1765 int int_size = ast_ctx.getTypeInfo(ast_ctx.IntTy).first / 8;
1767 return pet_scop_add_boolean_array(scop, isl_multi_pw_aff_copy(index),
1768 int_size);
1771 /* Construct a pet_scop for a while loop of the form
1773 * while (pa)
1774 * body
1776 * within the context "pc".
1777 * In particular, construct a scop for an infinite loop around body and
1778 * intersect the domain with the affine expression.
1779 * Note that this intersection may result in an empty loop.
1781 struct pet_scop *PetScan::extract_affine_while(__isl_take isl_pw_aff *pa,
1782 Stmt *body, __isl_take pet_context *pc)
1784 struct pet_scop *scop;
1785 isl_set *dom;
1786 isl_set *valid;
1788 valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1789 dom = isl_pw_aff_non_zero_set(pa);
1790 scop = extract_infinite_loop(body, pc);
1791 scop = pet_scop_restrict(scop, isl_set_params(dom));
1792 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
1794 pet_context_free(pc);
1795 return scop;
1798 /* Construct a scop for a while, given the scops for the condition
1799 * and the body, the filter identifier and the iteration domain of
1800 * the while loop.
1802 * In particular, the scop for the condition is filtered to depend
1803 * on "id_test" evaluating to true for all previous iterations
1804 * of the loop, while the scop for the body is filtered to depend
1805 * on "id_test" evaluating to true for all iterations up to the
1806 * current iteration.
1807 * The actual filter only imposes that this virtual array has
1808 * value one on the previous or the current iteration.
1809 * The fact that this condition also applies to the previous
1810 * iterations is enforced by an implication.
1812 * These filtered scops are then combined into a single scop.
1814 * "sign" is positive if the iterator increases and negative
1815 * if it decreases.
1817 static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
1818 struct pet_scop *scop_body, __isl_take isl_id *id_test,
1819 __isl_take isl_set *domain, __isl_take isl_val *inc)
1821 isl_ctx *ctx = isl_set_get_ctx(domain);
1822 isl_space *space;
1823 isl_multi_pw_aff *test_index;
1824 isl_multi_pw_aff *prev;
1825 int sign = isl_val_sgn(inc);
1826 struct pet_scop *scop;
1828 prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
1829 scop_cond = pet_scop_filter(scop_cond, prev, 1);
1831 space = isl_space_map_from_set(isl_set_get_space(domain));
1832 test_index = isl_multi_pw_aff_identity(space);
1833 test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
1834 isl_id_copy(id_test));
1835 scop_body = pet_scop_filter(scop_body, test_index, 1);
1837 scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
1838 scop = add_implication(scop, id_test, domain, sign, 1);
1840 return scop;
1843 /* Check if the while loop is of the form
1845 * while (affine expression)
1846 * body
1848 * If so, call extract_affine_while to construct a scop.
1850 * Otherwise, extract the body and pass control to extract_while
1851 * to extend the iteration domain with an infinite loop.
1852 * If we were only able to extract part of the body, then simply
1853 * return that part.
1855 * "pc" is the context in which the affine expressions in the scop are created.
1857 struct pet_scop *PetScan::extract(WhileStmt *stmt, __isl_keep pet_context *pc)
1859 Expr *cond;
1860 int test_nr, stmt_nr;
1861 isl_pw_aff *pa;
1862 struct pet_scop *scop, *scop_body;
1864 cond = stmt->getCond();
1865 if (!cond) {
1866 unsupported(stmt);
1867 return NULL;
1870 pc = pet_context_copy(pc);
1871 clear_assignments clear(pc);
1872 clear.TraverseStmt(stmt->getBody());
1874 pa = extract_condition(cond, pc);
1875 if (pa)
1876 return extract_affine_while(pa, stmt->getBody(), pc);
1878 if (!allow_nested) {
1879 unsupported(stmt);
1880 pet_context_free(pc);
1881 return NULL;
1884 test_nr = n_test++;
1885 stmt_nr = n_stmt++;
1886 scop_body = extract(stmt->getBody(), pc);
1887 if (partial) {
1888 pet_context_free(pc);
1889 return scop_body;
1892 return extract_while(cond, test_nr, stmt_nr, scop_body, NULL, pc);
1895 /* Construct a generic while scop, with iteration domain
1896 * { [t] : t >= 0 } around "scop_body" within the context "pc".
1897 * The scop consists of two parts,
1898 * one for evaluating the condition "cond" and one for the body.
1899 * "test_nr" is the sequence number of the virtual test variable that contains
1900 * the result of the condition and "stmt_nr" is the sequence number
1901 * of the statement that evaluates the condition.
1902 * If "scop_inc" is not NULL, then it is added at the end of the body,
1903 * after replacing any skip conditions resulting from continue statements
1904 * by the skip conditions resulting from break statements (if any).
1906 * The schedule is adjusted to reflect that the condition is evaluated
1907 * before the body is executed and the body is filtered to depend
1908 * on the result of the condition evaluating to true on all iterations
1909 * up to the current iteration, while the evaluation of the condition itself
1910 * is filtered to depend on the result of the condition evaluating to true
1911 * on all previous iterations.
1912 * The context of the scop representing the body is dropped
1913 * because we don't know how many times the body will be executed,
1914 * if at all.
1916 * If the body contains any break, then it is taken into
1917 * account in infinite_domain (if the skip condition is affine)
1918 * or in scop_add_break (if the skip condition is not affine).
1920 struct pet_scop *PetScan::extract_while(Expr *cond, int test_nr, int stmt_nr,
1921 struct pet_scop *scop_body, struct pet_scop *scop_inc,
1922 __isl_take pet_context *pc)
1924 isl_id *id, *id_test, *id_break_test;
1925 isl_set *domain;
1926 isl_aff *ident;
1927 isl_multi_pw_aff *test_index;
1928 struct pet_scop *scop;
1929 bool has_var_break;
1931 test_index = pet_create_test_index(ctx, test_nr);
1932 scop = extract_non_affine_condition(cond, stmt_nr,
1933 isl_multi_pw_aff_copy(test_index), pc);
1934 scop = scop_add_array(scop, test_index, ast_context);
1935 id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
1936 isl_multi_pw_aff_free(test_index);
1938 id = isl_id_alloc(ctx, "t", NULL);
1939 domain = infinite_domain(isl_id_copy(id), scop_body);
1940 ident = identity_aff(domain);
1942 has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
1943 if (has_var_break)
1944 id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
1946 scop = pet_scop_prefix(scop, 0);
1947 scop = pet_scop_embed(scop, isl_set_copy(domain), isl_aff_copy(ident),
1948 isl_aff_copy(ident), isl_id_copy(id));
1949 scop_body = pet_scop_reset_context(scop_body);
1950 scop_body = pet_scop_prefix(scop_body, 1);
1951 if (scop_inc) {
1952 scop_inc = pet_scop_prefix(scop_inc, 2);
1953 if (pet_scop_has_skip(scop_body, pet_skip_later)) {
1954 isl_multi_pw_aff *skip;
1955 skip = pet_scop_get_skip(scop_body, pet_skip_later);
1956 scop_body = pet_scop_set_skip(scop_body,
1957 pet_skip_now, skip);
1958 } else
1959 pet_scop_reset_skip(scop_body, pet_skip_now);
1960 scop_body = pet_scop_add_seq(ctx, scop_body, scop_inc);
1962 scop_body = pet_scop_embed(scop_body, isl_set_copy(domain),
1963 isl_aff_copy(ident), ident, id);
1965 if (has_var_break) {
1966 scop = scop_add_break(scop, isl_id_copy(id_break_test),
1967 isl_set_copy(domain), isl_val_one(ctx));
1968 scop_body = scop_add_break(scop_body, id_break_test,
1969 isl_set_copy(domain), isl_val_one(ctx));
1971 scop = scop_add_while(scop, scop_body, id_test, domain,
1972 isl_val_one(ctx));
1974 pet_context_free(pc);
1975 return scop;
1978 /* Check whether "cond" expresses a simple loop bound
1979 * on the only set dimension.
1980 * In particular, if "up" is set then "cond" should contain only
1981 * upper bounds on the set dimension.
1982 * Otherwise, it should contain only lower bounds.
1984 static bool is_simple_bound(__isl_keep isl_set *cond, __isl_keep isl_val *inc)
1986 if (isl_val_is_pos(inc))
1987 return !isl_set_dim_has_any_lower_bound(cond, isl_dim_set, 0);
1988 else
1989 return !isl_set_dim_has_any_upper_bound(cond, isl_dim_set, 0);
1992 /* Extend a condition on a given iteration of a loop to one that
1993 * imposes the same condition on all previous iterations.
1994 * "domain" expresses the lower [upper] bound on the iterations
1995 * when inc is positive [negative].
1997 * In particular, we construct the condition (when inc is positive)
1999 * forall i' : (domain(i') and i' <= i) => cond(i')
2001 * which is equivalent to
2003 * not exists i' : domain(i') and i' <= i and not cond(i')
2005 * We construct this set by negating cond, applying a map
2007 * { [i'] -> [i] : domain(i') and i' <= i }
2009 * and then negating the result again.
2011 static __isl_give isl_set *valid_for_each_iteration(__isl_take isl_set *cond,
2012 __isl_take isl_set *domain, __isl_take isl_val *inc)
2014 isl_map *previous_to_this;
2016 if (isl_val_is_pos(inc))
2017 previous_to_this = isl_map_lex_le(isl_set_get_space(domain));
2018 else
2019 previous_to_this = isl_map_lex_ge(isl_set_get_space(domain));
2021 previous_to_this = isl_map_intersect_domain(previous_to_this, domain);
2023 cond = isl_set_complement(cond);
2024 cond = isl_set_apply(cond, previous_to_this);
2025 cond = isl_set_complement(cond);
2027 isl_val_free(inc);
2029 return cond;
2032 /* Construct a domain of the form
2034 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
2036 static __isl_give isl_set *strided_domain(__isl_take isl_id *id,
2037 __isl_take isl_pw_aff *init, __isl_take isl_val *inc)
2039 isl_aff *aff;
2040 isl_space *dim;
2041 isl_set *set;
2043 init = isl_pw_aff_insert_dims(init, isl_dim_in, 0, 1);
2044 dim = isl_pw_aff_get_domain_space(init);
2045 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2046 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, 0, inc);
2047 init = isl_pw_aff_add(init, isl_pw_aff_from_aff(aff));
2049 dim = isl_space_set_alloc(isl_pw_aff_get_ctx(init), 1, 1);
2050 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
2051 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2052 aff = isl_aff_add_coefficient_si(aff, isl_dim_param, 0, 1);
2054 set = isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff), init);
2056 set = isl_set_lower_bound_si(set, isl_dim_set, 0, 0);
2058 return isl_set_params(set);
2061 /* Assuming "cond" represents a bound on a loop where the loop
2062 * iterator "iv" is incremented (or decremented) by one, check if wrapping
2063 * is possible.
2065 * Under the given assumptions, wrapping is only possible if "cond" allows
2066 * for the last value before wrapping, i.e., 2^width - 1 in case of an
2067 * increasing iterator and 0 in case of a decreasing iterator.
2069 static bool can_wrap(__isl_keep isl_set *cond, ValueDecl *iv,
2070 __isl_keep isl_val *inc)
2072 bool cw;
2073 isl_ctx *ctx;
2074 isl_val *limit;
2075 isl_set *test;
2077 test = isl_set_copy(cond);
2079 ctx = isl_set_get_ctx(test);
2080 if (isl_val_is_neg(inc))
2081 limit = isl_val_zero(ctx);
2082 else {
2083 limit = isl_val_int_from_ui(ctx, get_type_size(iv));
2084 limit = isl_val_2exp(limit);
2085 limit = isl_val_sub_ui(limit, 1);
2088 test = isl_set_fix_val(cond, isl_dim_set, 0, limit);
2089 cw = !isl_set_is_empty(test);
2090 isl_set_free(test);
2092 return cw;
2095 /* Given a one-dimensional space, construct the following affine expression
2096 * on this space
2098 * { [v] -> [v mod 2^width] }
2100 * where width is the number of bits used to represent the values
2101 * of the unsigned variable "iv".
2103 static __isl_give isl_aff *compute_wrapping(__isl_take isl_space *dim,
2104 ValueDecl *iv)
2106 isl_ctx *ctx;
2107 isl_val *mod;
2108 isl_aff *aff;
2110 ctx = isl_space_get_ctx(dim);
2111 mod = isl_val_int_from_ui(ctx, get_type_size(iv));
2112 mod = isl_val_2exp(mod);
2114 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2115 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2116 aff = isl_aff_mod_val(aff, mod);
2118 return aff;
2121 /* Project out the parameter "id" from "set".
2123 static __isl_give isl_set *set_project_out_by_id(__isl_take isl_set *set,
2124 __isl_keep isl_id *id)
2126 int pos;
2128 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
2129 if (pos >= 0)
2130 set = isl_set_project_out(set, isl_dim_param, pos, 1);
2132 return set;
2135 /* Compute the set of parameters for which "set1" is a subset of "set2".
2137 * set1 is a subset of set2 if
2139 * forall i in set1 : i in set2
2141 * or
2143 * not exists i in set1 and i not in set2
2145 * i.e.,
2147 * not exists i in set1 \ set2
2149 static __isl_give isl_set *enforce_subset(__isl_take isl_set *set1,
2150 __isl_take isl_set *set2)
2152 return isl_set_complement(isl_set_params(isl_set_subtract(set1, set2)));
2155 /* Compute the set of parameter values for which "cond" holds
2156 * on the next iteration for each element of "dom".
2158 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2159 * and then compute the set of parameters for which the result is a subset
2160 * of "cond".
2162 static __isl_give isl_set *valid_on_next(__isl_take isl_set *cond,
2163 __isl_take isl_set *dom, __isl_take isl_val *inc)
2165 isl_space *space;
2166 isl_aff *aff;
2167 isl_map *next;
2169 space = isl_set_get_space(dom);
2170 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2171 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
2172 aff = isl_aff_add_constant_val(aff, inc);
2173 next = isl_map_from_basic_map(isl_basic_map_from_aff(aff));
2175 dom = isl_set_apply(dom, next);
2177 return enforce_subset(dom, cond);
2180 /* Extract the for loop "stmt" as a while loop within the context "pc".
2181 * "iv" is the loop iterator. "init" is the initialization.
2182 * "inc" is the increment.
2184 * That is, the for loop has the form
2186 * for (iv = init; cond; iv += inc)
2187 * body;
2189 * and is treated as
2191 * iv = init;
2192 * while (cond) {
2193 * body;
2194 * iv += inc;
2197 * except that the skips resulting from any continue statements
2198 * in body do not apply to the increment, but are replaced by the skips
2199 * resulting from break statements.
2201 * If "iv" is declared in the for loop, then it is killed before
2202 * and after the loop.
2204 struct pet_scop *PetScan::extract_non_affine_for(ForStmt *stmt, ValueDecl *iv,
2205 __isl_take pet_expr *init, __isl_take pet_expr *inc,
2206 __isl_take pet_context *pc)
2208 int declared;
2209 int test_nr, stmt_nr;
2210 pet_expr *expr_iv;
2211 struct pet_scop *scop_init, *scop_inc, *scop, *scop_body;
2212 int type_size;
2213 struct pet_array *array;
2214 struct pet_scop *scop_kill;
2216 if (!allow_nested) {
2217 unsupported(stmt);
2218 pet_context_free(pc);
2219 return NULL;
2222 pc = pet_context_mark_assigned(pc, create_decl_id(ctx, iv));
2224 declared = !initialization_assignment(stmt->getInit());
2226 expr_iv = extract_access_expr(iv);
2227 expr_iv = mark_write(expr_iv);
2228 type_size = pet_expr_get_type_size(expr_iv);
2229 init = pet_expr_new_binary(type_size, pet_op_assign, expr_iv, init);
2230 scop_init = extract(init, stmt->getInit()->getSourceRange(), false, pc);
2231 scop_init = pet_scop_prefix(scop_init, declared);
2233 test_nr = n_test++;
2234 stmt_nr = n_stmt++;
2235 scop_body = extract(stmt->getBody(), pc);
2236 if (partial) {
2237 pet_scop_free(scop_init);
2238 pet_context_free(pc);
2239 return scop_body;
2242 expr_iv = extract_access_expr(iv);
2243 expr_iv = mark_write(expr_iv);
2244 type_size = pet_expr_get_type_size(expr_iv);
2245 inc = pet_expr_new_binary(type_size, pet_op_add_assign, expr_iv, inc);
2246 scop_inc = extract(inc, stmt->getInc()->getSourceRange(), false, pc);
2247 if (!scop_inc) {
2248 pet_scop_free(scop_init);
2249 pet_scop_free(scop_body);
2250 pet_context_free(pc);
2251 return NULL;
2254 scop = extract_while(stmt->getCond(), test_nr, stmt_nr, scop_body,
2255 scop_inc, pet_context_copy(pc));
2257 scop = pet_scop_prefix(scop, declared + 1);
2258 scop = pet_scop_add_seq(ctx, scop_init, scop);
2260 if (!declared) {
2261 pet_context_free(pc);
2262 return scop;
2265 array = extract_array(ctx, iv, NULL, pc);
2266 if (array)
2267 array->declared = 1;
2268 scop_kill = kill(stmt, array, pc);
2269 scop_kill = pet_scop_prefix(scop_kill, 0);
2270 scop = pet_scop_add_seq(ctx, scop_kill, scop);
2271 scop_kill = kill(stmt, array, pc);
2272 scop_kill = pet_scop_add_array(scop_kill, array);
2273 scop_kill = pet_scop_prefix(scop_kill, 3);
2274 scop = pet_scop_add_seq(ctx, scop, scop_kill);
2276 pet_context_free(pc);
2277 return scop;
2280 /* Construct a pet_scop for a for statement within the context "pc".
2281 * The for loop is required to be of one of the following forms
2283 * for (i = init; condition; ++i)
2284 * for (i = init; condition; --i)
2285 * for (i = init; condition; i += constant)
2286 * for (i = init; condition; i -= constant)
2288 * The initialization of the for loop should either be an assignment
2289 * of a static affine value to an integer variable, or a declaration
2290 * of such a variable with initialization.
2292 * If the initialization or the increment do not satisfy the above
2293 * conditions, i.e., if the initialization is not static affine
2294 * or the increment is not constant, then the for loop is extracted
2295 * as a while loop instead.
2297 * The condition is allowed to contain nested accesses, provided
2298 * they are not being written to inside the body of the loop.
2299 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2300 * essentially treated as a while loop, with iteration domain
2301 * { [i] : i >= init }.
2303 * We extract a pet_scop for the body and then embed it in a loop with
2304 * iteration domain and schedule
2306 * { [i] : i >= init and condition' }
2307 * { [i] -> [i] }
2309 * or
2311 * { [i] : i <= init and condition' }
2312 * { [i] -> [-i] }
2314 * Where condition' is equal to condition if the latter is
2315 * a simple upper [lower] bound and a condition that is extended
2316 * to apply to all previous iterations otherwise.
2318 * If the condition is non-affine, then we drop the condition from the
2319 * iteration domain and instead create a separate statement
2320 * for evaluating the condition. The body is then filtered to depend
2321 * on the result of the condition evaluating to true on all iterations
2322 * up to the current iteration, while the evaluation the condition itself
2323 * is filtered to depend on the result of the condition evaluating to true
2324 * on all previous iterations.
2325 * The context of the scop representing the body is dropped
2326 * because we don't know how many times the body will be executed,
2327 * if at all.
2329 * If the stride of the loop is not 1, then "i >= init" is replaced by
2331 * (exists a: i = init + stride * a and a >= 0)
2333 * If the loop iterator i is unsigned, then wrapping may occur.
2334 * We therefore use a virtual iterator instead that does not wrap.
2335 * However, the condition in the code applies
2336 * to the wrapped value, so we need to change condition(i)
2337 * into condition([i % 2^width]). Similarly, we replace all accesses
2338 * to the original iterator by the wrapping of the virtual iterator.
2339 * Note that there may be no need to perform this final wrapping
2340 * if the loop condition (after wrapping) satisfies certain conditions.
2341 * However, the is_simple_bound condition is not enough since it doesn't
2342 * check if there even is an upper bound.
2344 * Wrapping on unsigned iterators can be avoided entirely if
2345 * loop condition is simple, the loop iterator is incremented
2346 * [decremented] by one and the last value before wrapping cannot
2347 * possibly satisfy the loop condition.
2349 * Before extracting a pet_scop from the body we remove all
2350 * assignments in "pc" to variables that are assigned
2351 * somewhere in the body of the loop.
2353 * Valid parameters for a for loop are those for which the initial
2354 * value itself, the increment on each domain iteration and
2355 * the condition on both the initial value and
2356 * the result of incrementing the iterator for each iteration of the domain
2357 * can be evaluated.
2358 * If the loop condition is non-affine, then we only consider validity
2359 * of the initial value.
2361 * If the body contains any break, then we keep track of it in "skip"
2362 * (if the skip condition is affine) or it is handled in scop_add_break
2363 * (if the skip condition is not affine).
2364 * Note that the affine break condition needs to be considered with
2365 * respect to previous iterations in the virtual domain (if any).
2367 * If we were only able to extract part of the body, then simply
2368 * return that part.
2370 struct pet_scop *PetScan::extract_for(ForStmt *stmt, __isl_keep pet_context *pc)
2372 BinaryOperator *ass;
2373 Decl *decl;
2374 Stmt *init;
2375 Expr *lhs, *rhs;
2376 ValueDecl *iv;
2377 isl_local_space *ls;
2378 isl_set *domain;
2379 isl_aff *sched;
2380 isl_set *cond = NULL;
2381 isl_set *skip = NULL;
2382 isl_id *id, *id_test = NULL, *id_break_test;
2383 struct pet_scop *scop, *scop_cond = NULL;
2384 isl_val *inc;
2385 bool is_one;
2386 bool is_unsigned;
2387 bool is_simple;
2388 bool is_virtual;
2389 bool has_affine_break;
2390 bool has_var_break;
2391 isl_aff *wrap = NULL;
2392 isl_pw_aff *pa, *pa_inc, *init_val;
2393 isl_set *valid_init;
2394 isl_set *valid_cond;
2395 isl_set *valid_cond_init;
2396 isl_set *valid_cond_next;
2397 isl_set *valid_inc;
2398 int stmt_id;
2399 pet_expr *pe_init, *pe_inc;
2400 pet_context *pc_init_val;
2402 if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc())
2403 return extract_infinite_for(stmt, pc);
2405 init = stmt->getInit();
2406 if (!init) {
2407 unsupported(stmt);
2408 return NULL;
2410 if ((ass = initialization_assignment(init)) != NULL) {
2411 iv = extract_induction_variable(ass);
2412 if (!iv)
2413 return NULL;
2414 lhs = ass->getLHS();
2415 rhs = ass->getRHS();
2416 } else if ((decl = initialization_declaration(init)) != NULL) {
2417 VarDecl *var = extract_induction_variable(init, decl);
2418 if (!var)
2419 return NULL;
2420 iv = var;
2421 rhs = var->getInit();
2422 lhs = create_DeclRefExpr(var);
2423 } else {
2424 unsupported(stmt->getInit());
2425 return NULL;
2428 id = create_decl_id(ctx, iv);
2430 pc = pet_context_copy(pc);
2431 pc = pet_context_clear_value(pc, isl_id_copy(id));
2432 clear_assignments clear(pc);
2433 clear.TraverseStmt(stmt->getBody());
2435 pe_init = extract_expr(rhs);
2436 pe_inc = extract_increment(stmt, iv);
2437 pc_init_val = pet_context_copy(pc);
2438 pc_init_val = pet_context_mark_unknown(pc_init_val, isl_id_copy(id));
2439 init_val = pet_expr_extract_affine(pe_init, pc_init_val);
2440 pet_context_free(pc_init_val);
2441 pa_inc = pet_expr_extract_affine(pe_inc, pc);
2442 inc = pet_extract_cst(pa_inc);
2443 if (!pe_init || !pe_inc || !inc || isl_val_is_nan(inc) ||
2444 isl_pw_aff_involves_nan(pa_inc) ||
2445 isl_pw_aff_involves_nan(init_val)) {
2446 isl_id_free(id);
2447 isl_val_free(inc);
2448 isl_pw_aff_free(pa_inc);
2449 isl_pw_aff_free(init_val);
2450 if (pe_init && pe_inc && !(pa_inc && !inc))
2451 return extract_non_affine_for(stmt, iv,
2452 pe_init, pe_inc, pc);
2453 pet_expr_free(pe_init);
2454 pet_expr_free(pe_inc);
2455 pet_context_free(pc);
2456 return NULL;
2458 pet_expr_free(pe_init);
2459 pet_expr_free(pe_inc);
2461 pa = try_extract_nested_condition(stmt->getCond(), pc);
2462 if (allow_nested && (!pa || pet_nested_any_in_pw_aff(pa)))
2463 stmt_id = n_stmt++;
2465 scop = extract(stmt->getBody(), pc);
2466 if (partial) {
2467 isl_id_free(id);
2468 isl_pw_aff_free(init_val);
2469 isl_pw_aff_free(pa_inc);
2470 isl_pw_aff_free(pa);
2471 isl_val_free(inc);
2472 pet_context_free(pc);
2473 return scop;
2476 valid_inc = isl_pw_aff_domain(pa_inc);
2478 is_unsigned = iv->getType()->isUnsignedIntegerType();
2480 has_affine_break = scop &&
2481 pet_scop_has_affine_skip(scop, pet_skip_later);
2482 if (has_affine_break)
2483 skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
2484 has_var_break = scop && pet_scop_has_var_skip(scop, pet_skip_later);
2485 if (has_var_break)
2486 id_break_test = pet_scop_get_skip_id(scop, pet_skip_later);
2488 if (pa && !is_nested_allowed(pa, scop)) {
2489 isl_pw_aff_free(pa);
2490 pa = NULL;
2493 if (!allow_nested && !pa)
2494 pa = extract_condition(stmt->getCond(), pc);
2495 valid_cond = isl_pw_aff_domain(isl_pw_aff_copy(pa));
2496 cond = isl_pw_aff_non_zero_set(pa);
2497 if (allow_nested && !cond) {
2498 isl_multi_pw_aff *test_index;
2499 int save_n_stmt = n_stmt;
2500 test_index = pet_create_test_index(ctx, n_test++);
2501 n_stmt = stmt_id;
2502 scop_cond = extract_non_affine_condition(stmt->getCond(),
2503 n_stmt++, isl_multi_pw_aff_copy(test_index), pc);
2504 n_stmt = save_n_stmt;
2505 scop_cond = scop_add_array(scop_cond, test_index, ast_context);
2506 id_test = isl_multi_pw_aff_get_tuple_id(test_index,
2507 isl_dim_out);
2508 isl_multi_pw_aff_free(test_index);
2509 scop_cond = pet_scop_prefix(scop_cond, 0);
2510 scop = pet_scop_reset_context(scop);
2511 scop = pet_scop_prefix(scop, 1);
2512 cond = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
2515 cond = embed(cond, isl_id_copy(id));
2516 skip = embed(skip, isl_id_copy(id));
2517 valid_cond = isl_set_coalesce(valid_cond);
2518 valid_cond = embed(valid_cond, isl_id_copy(id));
2519 valid_inc = embed(valid_inc, isl_id_copy(id));
2520 is_one = isl_val_is_one(inc) || isl_val_is_negone(inc);
2521 is_virtual = is_unsigned && (!is_one || can_wrap(cond, iv, inc));
2523 valid_cond_init = enforce_subset(
2524 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val))),
2525 isl_set_copy(valid_cond));
2526 if (is_one && !is_virtual) {
2527 isl_pw_aff_free(init_val);
2528 pa = extract_comparison(isl_val_is_pos(inc) ? BO_GE : BO_LE,
2529 lhs, rhs, init, pc);
2530 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(pa));
2531 valid_init = set_project_out_by_id(valid_init, id);
2532 domain = isl_pw_aff_non_zero_set(pa);
2533 } else {
2534 valid_init = isl_pw_aff_domain(isl_pw_aff_copy(init_val));
2535 domain = strided_domain(isl_id_copy(id), init_val,
2536 isl_val_copy(inc));
2539 domain = embed(domain, isl_id_copy(id));
2540 if (is_virtual) {
2541 isl_map *rev_wrap;
2542 wrap = compute_wrapping(isl_set_get_space(cond), iv);
2543 rev_wrap = isl_map_from_aff(isl_aff_copy(wrap));
2544 rev_wrap = isl_map_reverse(rev_wrap);
2545 cond = isl_set_apply(cond, isl_map_copy(rev_wrap));
2546 skip = isl_set_apply(skip, isl_map_copy(rev_wrap));
2547 valid_cond = isl_set_apply(valid_cond, isl_map_copy(rev_wrap));
2548 valid_inc = isl_set_apply(valid_inc, rev_wrap);
2550 is_simple = is_simple_bound(cond, inc);
2551 if (!is_simple) {
2552 cond = isl_set_gist(cond, isl_set_copy(domain));
2553 is_simple = is_simple_bound(cond, inc);
2555 if (!is_simple)
2556 cond = valid_for_each_iteration(cond,
2557 isl_set_copy(domain), isl_val_copy(inc));
2558 domain = isl_set_intersect(domain, cond);
2559 if (has_affine_break) {
2560 skip = isl_set_intersect(skip , isl_set_copy(domain));
2561 skip = after(skip, isl_val_sgn(inc));
2562 domain = isl_set_subtract(domain, skip);
2564 domain = isl_set_set_dim_id(domain, isl_dim_set, 0, isl_id_copy(id));
2565 ls = isl_local_space_from_space(isl_set_get_space(domain));
2566 sched = isl_aff_var_on_domain(ls, isl_dim_set, 0);
2567 if (isl_val_is_neg(inc))
2568 sched = isl_aff_neg(sched);
2570 valid_cond_next = valid_on_next(valid_cond, isl_set_copy(domain),
2571 isl_val_copy(inc));
2572 valid_inc = enforce_subset(isl_set_copy(domain), valid_inc);
2574 if (!is_virtual)
2575 wrap = identity_aff(domain);
2577 scop_cond = pet_scop_embed(scop_cond, isl_set_copy(domain),
2578 isl_aff_copy(sched), isl_aff_copy(wrap), isl_id_copy(id));
2579 scop = pet_scop_embed(scop, isl_set_copy(domain), sched, wrap, id);
2580 scop = resolve_nested(scop);
2581 if (has_var_break)
2582 scop = scop_add_break(scop, id_break_test, isl_set_copy(domain),
2583 isl_val_copy(inc));
2584 if (id_test) {
2585 scop = scop_add_while(scop_cond, scop, id_test, domain,
2586 isl_val_copy(inc));
2587 isl_set_free(valid_inc);
2588 } else {
2589 scop = pet_scop_restrict_context(scop, valid_inc);
2590 scop = pet_scop_restrict_context(scop, valid_cond_next);
2591 scop = pet_scop_restrict_context(scop, valid_cond_init);
2592 isl_set_free(domain);
2595 isl_val_free(inc);
2597 scop = pet_scop_restrict_context(scop, isl_set_params(valid_init));
2599 pet_context_free(pc);
2600 return scop;
2603 /* Try and construct a pet_scop corresponding to a compound statement
2604 * within the context "pc".
2606 * "skip_declarations" is set if we should skip initial declarations
2607 * in the children of the compound statements. This then implies
2608 * that this sequence of children should not be treated as a block
2609 * since the initial statements may be skipped.
2611 struct pet_scop *PetScan::extract(CompoundStmt *stmt,
2612 __isl_keep pet_context *pc, bool skip_declarations)
2614 return extract(stmt->children(),
2615 !skip_declarations, skip_declarations, pc);
2618 /* For each nested access parameter in "space",
2619 * construct a corresponding pet_expr, place it in args and
2620 * record its position in "param2pos".
2621 * "n_arg" is the number of elements that are already in args.
2622 * The position recorded in "param2pos" takes this number into account.
2623 * If the pet_expr corresponding to a parameter is identical to
2624 * the pet_expr corresponding to an earlier parameter, then these two
2625 * parameters are made to refer to the same element in args.
2627 * Return the final number of elements in args or -1 if an error has occurred.
2629 int PetScan::extract_nested(__isl_keep isl_space *space,
2630 int n_arg, pet_expr **args, std::map<int,int> &param2pos)
2632 int nparam;
2634 nparam = isl_space_dim(space, isl_dim_param);
2635 for (int i = 0; i < nparam; ++i) {
2636 int j;
2637 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
2639 if (!pet_nested_in_id(id)) {
2640 isl_id_free(id);
2641 continue;
2644 args[n_arg] = pet_nested_extract_expr(id);
2645 isl_id_free(id);
2646 if (!args[n_arg])
2647 return -1;
2649 for (j = 0; j < n_arg; ++j)
2650 if (pet_expr_is_equal(args[j], args[n_arg]))
2651 break;
2653 if (j < n_arg) {
2654 pet_expr_free(args[n_arg]);
2655 args[n_arg] = NULL;
2656 param2pos[i] = j;
2657 } else
2658 param2pos[i] = n_arg++;
2661 return n_arg;
2664 /* For each nested access parameter in the access relations in "expr",
2665 * construct a corresponding pet_expr, append it to the arguments of "expr"
2666 * and record its position in "param2pos" (relative to the initial
2667 * number of arguments).
2668 * n is the number of nested access parameters.
2670 __isl_give pet_expr *PetScan::extract_nested(__isl_take pet_expr *expr, int n,
2671 std::map<int,int> &param2pos)
2673 isl_space *space;
2674 int i, n_arg;
2675 pet_expr **args;
2677 args = isl_calloc_array(ctx, pet_expr *, n);
2678 if (!args)
2679 return pet_expr_free(expr);
2681 n_arg = pet_expr_get_n_arg(expr);
2682 space = pet_expr_access_get_parameter_space(expr);
2683 n = extract_nested(space, 0, args, param2pos);
2684 isl_space_free(space);
2686 if (n < 0)
2687 expr = pet_expr_free(expr);
2688 else
2689 expr = pet_expr_set_n_arg(expr, n_arg + n);
2691 for (i = 0; i < n; ++i)
2692 expr = pet_expr_set_arg(expr, n_arg + i, args[i]);
2693 free(args);
2695 return expr;
2698 /* Are "expr1" and "expr2" both array accesses such that
2699 * the access relation of "expr1" is a subset of that of "expr2"?
2700 * Only take into account the first "n_arg" arguments.
2702 static int is_sub_access(__isl_keep pet_expr *expr1, __isl_keep pet_expr *expr2,
2703 int n_arg)
2705 isl_id *id1, *id2;
2706 isl_map *access1, *access2;
2707 int is_subset;
2708 int i, n1, n2;
2710 if (!expr1 || !expr2)
2711 return 0;
2712 if (pet_expr_get_type(expr1) != pet_expr_access)
2713 return 0;
2714 if (pet_expr_get_type(expr2) != pet_expr_access)
2715 return 0;
2716 if (pet_expr_is_affine(expr1))
2717 return 0;
2718 if (pet_expr_is_affine(expr2))
2719 return 0;
2720 n1 = pet_expr_get_n_arg(expr1);
2721 if (n1 > n_arg)
2722 n1 = n_arg;
2723 n2 = pet_expr_get_n_arg(expr2);
2724 if (n2 > n_arg)
2725 n2 = n_arg;
2726 if (n1 != n2)
2727 return 0;
2728 for (i = 0; i < n1; ++i) {
2729 pet_expr *arg1, *arg2;
2730 int equal;
2731 arg1 = pet_expr_get_arg(expr1, i);
2732 arg2 = pet_expr_get_arg(expr2, i);
2733 equal = pet_expr_is_equal(arg1, arg2);
2734 pet_expr_free(arg1);
2735 pet_expr_free(arg2);
2736 if (equal < 0 || !equal)
2737 return equal;
2739 id1 = pet_expr_access_get_id(expr1);
2740 id2 = pet_expr_access_get_id(expr2);
2741 isl_id_free(id1);
2742 isl_id_free(id2);
2743 if (!id1 || !id2)
2744 return 0;
2745 if (id1 != id2)
2746 return 0;
2748 access1 = pet_expr_access_get_access(expr1);
2749 access2 = pet_expr_access_get_access(expr2);
2750 is_subset = isl_map_is_subset(access1, access2);
2751 isl_map_free(access1);
2752 isl_map_free(access2);
2754 return is_subset;
2757 /* Mark self dependences among the arguments of "expr" starting at "first".
2758 * These arguments have already been added to the list of arguments
2759 * but are not yet referenced directly from the index expression.
2760 * Instead, they are still referenced through parameters encoding
2761 * nested accesses.
2763 * In particular, if "expr" is a read access, then check the arguments
2764 * starting at "first" to see if "expr" accesses a subset of
2765 * the elements accessed by the argument, or under more restrictive conditions.
2766 * If so, then this nested access can be removed from the constraints
2767 * governing the outer access. There is no point in restricting
2768 * accesses to an array if in order to evaluate the restriction,
2769 * we have to access the same elements (or more).
2771 * Rather than removing the argument at this point (which would
2772 * complicate the resolution of the other nested accesses), we simply
2773 * mark it here by replacing it by a NaN pet_expr.
2774 * These NaNs are then later removed in remove_marked_self_dependences.
2776 static __isl_give pet_expr *mark_self_dependences(__isl_take pet_expr *expr,
2777 int first)
2779 int n;
2781 if (pet_expr_access_is_write(expr))
2782 return expr;
2784 n = pet_expr_get_n_arg(expr);
2785 for (int i = first; i < n; ++i) {
2786 int mark;
2787 pet_expr *arg;
2789 arg = pet_expr_get_arg(expr, i);
2790 mark = is_sub_access(expr, arg, first);
2791 pet_expr_free(arg);
2792 if (mark < 0)
2793 return pet_expr_free(expr);
2794 if (!mark)
2795 continue;
2797 arg = pet_expr_new_int(isl_val_nan(pet_expr_get_ctx(expr)));
2798 expr = pet_expr_set_arg(expr, i, arg);
2801 return expr;
2804 /* Is "expr" a NaN integer expression?
2806 static int expr_is_nan(__isl_keep pet_expr *expr)
2808 isl_val *v;
2809 int is_nan;
2811 if (pet_expr_get_type(expr) != pet_expr_int)
2812 return 0;
2814 v = pet_expr_int_get_val(expr);
2815 is_nan = isl_val_is_nan(v);
2816 isl_val_free(v);
2818 return is_nan;
2821 /* Check if we have marked any self dependences (as NaNs)
2822 * in mark_self_dependences and remove them here.
2823 * It is safe to project them out since these arguments
2824 * can at most be referenced from the condition of the access relation,
2825 * but do not appear in the index expression.
2826 * "dim" is the dimension of the iteration domain.
2828 static __isl_give pet_expr *remove_marked_self_dependences(
2829 __isl_take pet_expr *expr, int dim, int first)
2831 int n;
2833 n = pet_expr_get_n_arg(expr);
2834 for (int i = n - 1; i >= first; --i) {
2835 int is_nan;
2836 pet_expr *arg;
2838 arg = pet_expr_get_arg(expr, i);
2839 is_nan = expr_is_nan(arg);
2840 pet_expr_free(arg);
2841 if (!is_nan)
2842 continue;
2843 expr = pet_expr_access_project_out_arg(expr, dim, i);
2846 return expr;
2849 /* Look for parameters in any access relation in "expr" that
2850 * refer to nested accesses. In particular, these are
2851 * parameters with name "__pet_expr".
2853 * If there are any such parameters, then the domain of the index
2854 * expression and the access relation, which is either [] or
2855 * [[] -> [a_1,...,a_m]] at this point, is replaced by [[] -> [t_1,...,t_n]] or
2856 * [[] -> [a_1,...,a_m,t_1,...,t_n]], with m the original number of arguments
2857 * (n_arg) and n the number of these parameters
2858 * (after identifying identical nested accesses).
2860 * This transformation is performed in several steps.
2861 * We first extract the arguments in extract_nested.
2862 * param2pos maps the original parameter position to the position
2863 * of the argument beyond the initial (n_arg) number of arguments.
2864 * Then we move these parameters to input dimensions.
2865 * t2pos maps the positions of these temporary input dimensions
2866 * to the positions of the corresponding arguments.
2867 * Finally, we express these temporary dimensions in terms of the domain
2868 * [[] -> [a_1,...,a_m,t_1,...,t_n]] and precompose index expression and access
2869 * relations with this function.
2871 __isl_give pet_expr *PetScan::resolve_nested(__isl_take pet_expr *expr)
2873 int n, n_arg;
2874 int nparam;
2875 isl_space *space;
2876 isl_local_space *ls;
2877 isl_aff *aff;
2878 isl_multi_aff *ma;
2879 std::map<int,int> param2pos;
2880 std::map<int,int> t2pos;
2882 if (!expr)
2883 return expr;
2885 n_arg = pet_expr_get_n_arg(expr);
2886 for (int i = 0; i < n_arg; ++i) {
2887 pet_expr *arg;
2888 arg = pet_expr_get_arg(expr, i);
2889 arg = resolve_nested(arg);
2890 expr = pet_expr_set_arg(expr, i, arg);
2893 if (pet_expr_get_type(expr) != pet_expr_access)
2894 return expr;
2896 space = pet_expr_access_get_parameter_space(expr);
2897 n = pet_nested_n_in_space(space);
2898 isl_space_free(space);
2899 if (n == 0)
2900 return expr;
2902 expr = extract_nested(expr, n, param2pos);
2903 if (!expr)
2904 return NULL;
2906 expr = pet_expr_access_align_params(expr);
2907 expr = mark_self_dependences(expr, n_arg);
2908 if (!expr)
2909 return NULL;
2911 n = 0;
2912 space = pet_expr_access_get_parameter_space(expr);
2913 nparam = isl_space_dim(space, isl_dim_param);
2914 for (int i = nparam - 1; i >= 0; --i) {
2915 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
2916 if (!pet_nested_in_id(id)) {
2917 isl_id_free(id);
2918 continue;
2921 expr = pet_expr_access_move_dims(expr,
2922 isl_dim_in, n_arg + n, isl_dim_param, i, 1);
2923 t2pos[n] = n_arg + param2pos[i];
2924 n++;
2926 isl_id_free(id);
2928 isl_space_free(space);
2930 space = pet_expr_access_get_parameter_space(expr);
2931 space = isl_space_set_from_params(space);
2932 space = isl_space_add_dims(space, isl_dim_set,
2933 pet_expr_get_n_arg(expr));
2934 space = isl_space_wrap(isl_space_from_range(space));
2935 ls = isl_local_space_from_space(isl_space_copy(space));
2936 space = isl_space_from_domain(space);
2937 space = isl_space_add_dims(space, isl_dim_out, n_arg + n);
2938 ma = isl_multi_aff_zero(space);
2940 for (int i = 0; i < n_arg; ++i) {
2941 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2942 isl_dim_set, i);
2943 ma = isl_multi_aff_set_aff(ma, i, aff);
2945 for (int i = 0; i < n; ++i) {
2946 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2947 isl_dim_set, t2pos[i]);
2948 ma = isl_multi_aff_set_aff(ma, n_arg + i, aff);
2950 isl_local_space_free(ls);
2952 expr = pet_expr_access_pullback_multi_aff(expr, ma);
2954 expr = remove_marked_self_dependences(expr, 0, n_arg);
2956 return expr;
2959 /* Return the file offset of the expansion location of "Loc".
2961 static unsigned getExpansionOffset(SourceManager &SM, SourceLocation Loc)
2963 return SM.getFileOffset(SM.getExpansionLoc(Loc));
2966 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
2968 /* Return a SourceLocation for the location after the first semicolon
2969 * after "loc". If Lexer::findLocationAfterToken is available, we simply
2970 * call it and also skip trailing spaces and newline.
2972 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
2973 const LangOptions &LO)
2975 return Lexer::findLocationAfterToken(loc, tok::semi, SM, LO, true);
2978 #else
2980 /* Return a SourceLocation for the location after the first semicolon
2981 * after "loc". If Lexer::findLocationAfterToken is not available,
2982 * we look in the underlying character data for the first semicolon.
2984 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
2985 const LangOptions &LO)
2987 const char *semi;
2988 const char *s = SM.getCharacterData(loc);
2990 semi = strchr(s, ';');
2991 if (!semi)
2992 return SourceLocation();
2993 return loc.getFileLocWithOffset(semi + 1 - s);
2996 #endif
2998 /* If the token at "loc" is the first token on the line, then return
2999 * a location referring to the start of the line.
3000 * Otherwise, return "loc".
3002 * This function is used to extend a scop to the start of the line
3003 * if the first token of the scop is also the first token on the line.
3005 * We look for the first token on the line. If its location is equal to "loc",
3006 * then the latter is the location of the first token on the line.
3008 static SourceLocation move_to_start_of_line_if_first_token(SourceLocation loc,
3009 SourceManager &SM, const LangOptions &LO)
3011 std::pair<FileID, unsigned> file_offset_pair;
3012 llvm::StringRef file;
3013 const char *pos;
3014 Token tok;
3015 SourceLocation token_loc, line_loc;
3016 int col;
3018 loc = SM.getExpansionLoc(loc);
3019 col = SM.getExpansionColumnNumber(loc);
3020 line_loc = loc.getLocWithOffset(1 - col);
3021 file_offset_pair = SM.getDecomposedLoc(line_loc);
3022 file = SM.getBufferData(file_offset_pair.first, NULL);
3023 pos = file.data() + file_offset_pair.second;
3025 Lexer lexer(SM.getLocForStartOfFile(file_offset_pair.first), LO,
3026 file.begin(), pos, file.end());
3027 lexer.LexFromRawLexer(tok);
3028 token_loc = tok.getLocation();
3030 if (token_loc == loc)
3031 return line_loc;
3032 else
3033 return loc;
3036 /* Update start and end of "scop" to include the region covered by "range".
3037 * If "skip_semi" is set, then we assume "range" is followed by
3038 * a semicolon and also include this semicolon.
3040 struct pet_scop *PetScan::update_scop_start_end(struct pet_scop *scop,
3041 SourceRange range, bool skip_semi)
3043 SourceLocation loc = range.getBegin();
3044 SourceManager &SM = PP.getSourceManager();
3045 const LangOptions &LO = PP.getLangOpts();
3046 unsigned start, end;
3048 loc = move_to_start_of_line_if_first_token(loc, SM, LO);
3049 start = getExpansionOffset(SM, loc);
3050 loc = range.getEnd();
3051 if (skip_semi)
3052 loc = location_after_semi(loc, SM, LO);
3053 else
3054 loc = PP.getLocForEndOfToken(loc);
3055 end = getExpansionOffset(SM, loc);
3057 scop = pet_scop_update_start_end(scop, start, end);
3058 return scop;
3061 /* Convert a top-level pet_expr to a pet_scop with one statement
3062 * within the context "pc".
3063 * This mainly involves resolving nested expression parameters
3064 * and setting the name of the iteration space.
3065 * The name is given by "label" if it is non-NULL. Otherwise,
3066 * it is of the form S_<n_stmt>.
3067 * start and end of the pet_scop are derived from "range" and "skip_semi".
3068 * In particular, if "skip_semi" is set then the semicolon following "range"
3069 * is also included.
3071 struct pet_scop *PetScan::extract(__isl_take pet_expr *expr, SourceRange range,
3072 bool skip_semi, __isl_keep pet_context *pc, __isl_take isl_id *label)
3074 struct pet_stmt *ps;
3075 struct pet_scop *scop;
3076 SourceLocation loc = range.getBegin();
3077 int line = PP.getSourceManager().getExpansionLineNumber(loc);
3079 expr = pet_expr_plug_in_args(expr, pc);
3080 expr = resolve_nested(expr);
3081 expr = pet_expr_resolve_assume(expr, pc);
3082 ps = pet_stmt_from_pet_expr(line, label, n_stmt++, expr);
3083 scop = pet_scop_from_pet_stmt(ctx, ps);
3085 scop = update_scop_start_end(scop, range, skip_semi);
3086 return scop;
3089 /* Check whether "expr" is an affine constraint within the context "pc".
3091 bool PetScan::is_affine_condition(Expr *expr, __isl_keep pet_context *pc)
3093 isl_pw_aff *cond;
3095 cond = extract_condition(expr, pc);
3096 isl_pw_aff_free(cond);
3098 return cond != NULL;
3101 /* Check if we can extract a condition from "expr" within the context "pc".
3102 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
3103 * If allow_nested is set, then the condition may involve parameters
3104 * corresponding to nested accesses.
3105 * We turn on autodetection so that we won't generate any warnings.
3107 __isl_give isl_pw_aff *PetScan::try_extract_nested_condition(Expr *expr,
3108 __isl_keep pet_context *pc)
3110 isl_pw_aff *cond;
3111 int save_autodetect = options->autodetect;
3112 bool save_nesting = nesting_enabled;
3114 options->autodetect = 1;
3115 nesting_enabled = allow_nested;
3116 cond = extract_condition(expr, pc);
3118 options->autodetect = save_autodetect;
3119 nesting_enabled = save_nesting;
3121 return cond;
3124 /* If the top-level expression of "stmt" is an assignment, then
3125 * return that assignment as a BinaryOperator.
3126 * Otherwise return NULL.
3128 static BinaryOperator *top_assignment_or_null(Stmt *stmt)
3130 BinaryOperator *ass;
3132 if (!stmt)
3133 return NULL;
3134 if (stmt->getStmtClass() != Stmt::BinaryOperatorClass)
3135 return NULL;
3137 ass = cast<BinaryOperator>(stmt);
3138 if(ass->getOpcode() != BO_Assign)
3139 return NULL;
3141 return ass;
3144 /* Check if the given if statement is a conditional assignement
3145 * with a non-affine condition within the context "pc".
3146 * If so, construct a pet_scop corresponding to this conditional assignment.
3147 * Otherwise return NULL.
3149 * In particular we check if "stmt" is of the form
3151 * if (condition)
3152 * a = f(...);
3153 * else
3154 * a = g(...);
3156 * where a is some array or scalar access.
3157 * The constructed pet_scop then corresponds to the expression
3159 * a = condition ? f(...) : g(...)
3161 * All access relations in f(...) are intersected with condition
3162 * while all access relation in g(...) are intersected with the complement.
3164 struct pet_scop *PetScan::extract_conditional_assignment(IfStmt *stmt,
3165 __isl_keep pet_context *pc)
3167 BinaryOperator *ass_then, *ass_else;
3168 pet_expr *write_then, *write_else;
3169 isl_set *cond, *comp;
3170 isl_multi_pw_aff *index;
3171 isl_pw_aff *pa;
3172 int equal;
3173 int type_size;
3174 pet_expr *pe_cond, *pe_then, *pe_else, *pe;
3175 bool save_nesting = nesting_enabled;
3177 if (!options->detect_conditional_assignment)
3178 return NULL;
3180 ass_then = top_assignment_or_null(stmt->getThen());
3181 ass_else = top_assignment_or_null(stmt->getElse());
3183 if (!ass_then || !ass_else)
3184 return NULL;
3186 if (is_affine_condition(stmt->getCond(), pc))
3187 return NULL;
3189 write_then = extract_access_expr(ass_then->getLHS());
3190 write_else = extract_access_expr(ass_else->getLHS());
3192 equal = pet_expr_is_equal(write_then, write_else);
3193 pet_expr_free(write_else);
3194 if (equal < 0 || !equal) {
3195 pet_expr_free(write_then);
3196 return NULL;
3199 nesting_enabled = allow_nested;
3200 pa = extract_condition(stmt->getCond(), pc);
3201 nesting_enabled = save_nesting;
3202 cond = isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa));
3203 comp = isl_pw_aff_zero_set(isl_pw_aff_copy(pa));
3204 index = isl_multi_pw_aff_from_pw_aff(pa);
3206 pe_cond = pet_expr_from_index(index);
3208 pe_then = extract_expr(ass_then->getRHS());
3209 pe_then = pet_expr_restrict(pe_then, cond);
3210 pe_else = extract_expr(ass_else->getRHS());
3211 pe_else = pet_expr_restrict(pe_else, comp);
3213 pe = pet_expr_new_ternary(pe_cond, pe_then, pe_else);
3214 write_then = pet_expr_access_set_write(write_then, 1);
3215 write_then = pet_expr_access_set_read(write_then, 0);
3216 type_size = get_type_size(ass_then->getType(), ast_context);
3217 pe = pet_expr_new_binary(type_size, pet_op_assign, write_then, pe);
3218 return extract(pe, stmt->getSourceRange(), false, pc);
3221 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
3222 * evaluating "cond" and writing the result to a virtual scalar,
3223 * as expressed by "index".
3224 * Do so within the context "pc".
3226 struct pet_scop *PetScan::extract_non_affine_condition(Expr *cond, int stmt_nr,
3227 __isl_take isl_multi_pw_aff *index, __isl_keep pet_context *pc)
3229 pet_expr *expr, *write;
3230 struct pet_stmt *ps;
3231 SourceLocation loc = cond->getLocStart();
3232 int line = PP.getSourceManager().getExpansionLineNumber(loc);
3234 write = pet_expr_from_index(index);
3235 write = pet_expr_access_set_write(write, 1);
3236 write = pet_expr_access_set_read(write, 0);
3237 expr = extract_expr(cond);
3238 expr = pet_expr_plug_in_args(expr, pc);
3239 expr = resolve_nested(expr);
3240 expr = pet_expr_new_binary(1, pet_op_assign, write, expr);
3241 ps = pet_stmt_from_pet_expr(line, NULL, stmt_nr, expr);
3242 return pet_scop_from_pet_stmt(ctx, ps);
3245 extern "C" {
3246 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr,
3247 void *user);
3250 /* Precompose the access relation and the index expression associated
3251 * to "expr" with the function pointed to by "user",
3252 * thereby embedding the access relation in the domain of this function.
3253 * The initial domain of the access relation and the index expression
3254 * is the zero-dimensional domain.
3256 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr, void *user)
3258 isl_multi_aff *ma = (isl_multi_aff *) user;
3260 return pet_expr_access_pullback_multi_aff(expr, isl_multi_aff_copy(ma));
3263 /* Precompose all access relations in "expr" with "ma", thereby
3264 * embedding them in the domain of "ma".
3266 static __isl_give pet_expr *embed(__isl_take pet_expr *expr,
3267 __isl_keep isl_multi_aff *ma)
3269 return pet_expr_map_access(expr, &embed_access, ma);
3272 /* For each nested access parameter in the domain of "stmt",
3273 * construct a corresponding pet_expr, place it before the original
3274 * elements in stmt->args and record its position in "param2pos".
3275 * n is the number of nested access parameters.
3277 struct pet_stmt *PetScan::extract_nested(struct pet_stmt *stmt, int n,
3278 std::map<int,int> &param2pos)
3280 int i;
3281 isl_space *space;
3282 int n_arg;
3283 pet_expr **args;
3285 n_arg = stmt->n_arg;
3286 args = isl_calloc_array(ctx, pet_expr *, n + n_arg);
3287 if (!args)
3288 goto error;
3290 space = isl_set_get_space(stmt->domain);
3291 n_arg = extract_nested(space, 0, args, param2pos);
3292 isl_space_free(space);
3294 if (n_arg < 0)
3295 goto error;
3297 for (i = 0; i < stmt->n_arg; ++i)
3298 args[n_arg + i] = stmt->args[i];
3299 free(stmt->args);
3300 stmt->args = args;
3301 stmt->n_arg += n_arg;
3303 return stmt;
3304 error:
3305 if (args) {
3306 for (i = 0; i < n; ++i)
3307 pet_expr_free(args[i]);
3308 free(args);
3310 pet_stmt_free(stmt);
3311 return NULL;
3314 /* Check whether any of the arguments i of "stmt" starting at position "n"
3315 * is equal to one of the first "n" arguments j.
3316 * If so, combine the constraints on arguments i and j and remove
3317 * argument i.
3319 static struct pet_stmt *remove_duplicate_arguments(struct pet_stmt *stmt, int n)
3321 int i, j;
3322 isl_map *map;
3324 if (!stmt)
3325 return NULL;
3326 if (n == 0)
3327 return stmt;
3328 if (n == stmt->n_arg)
3329 return stmt;
3331 map = isl_set_unwrap(stmt->domain);
3333 for (i = stmt->n_arg - 1; i >= n; --i) {
3334 for (j = 0; j < n; ++j)
3335 if (pet_expr_is_equal(stmt->args[i], stmt->args[j]))
3336 break;
3337 if (j >= n)
3338 continue;
3340 map = isl_map_equate(map, isl_dim_out, i, isl_dim_out, j);
3341 map = isl_map_project_out(map, isl_dim_out, i, 1);
3343 pet_expr_free(stmt->args[i]);
3344 for (j = i; j + 1 < stmt->n_arg; ++j)
3345 stmt->args[j] = stmt->args[j + 1];
3346 stmt->n_arg--;
3349 stmt->domain = isl_map_wrap(map);
3350 if (!stmt->domain)
3351 goto error;
3352 return stmt;
3353 error:
3354 pet_stmt_free(stmt);
3355 return NULL;
3358 /* Look for parameters in the iteration domain of "stmt" that
3359 * refer to nested accesses. In particular, these are
3360 * parameters with name "__pet_expr".
3362 * If there are any such parameters, then as many extra variables
3363 * (after identifying identical nested accesses) are inserted in the
3364 * range of the map wrapped inside the domain, before the original variables.
3365 * If the original domain is not a wrapped map, then a new wrapped
3366 * map is created with zero output dimensions.
3367 * The parameters are then equated to the corresponding output dimensions
3368 * and subsequently projected out, from the iteration domain,
3369 * the schedule and the access relations.
3370 * For each of the output dimensions, a corresponding argument
3371 * expression is inserted. Initially they are created with
3372 * a zero-dimensional domain, so they have to be embedded
3373 * in the current iteration domain.
3374 * param2pos maps the position of the parameter to the position
3375 * of the corresponding output dimension in the wrapped map.
3377 struct pet_stmt *PetScan::resolve_nested(struct pet_stmt *stmt)
3379 int n;
3380 int nparam;
3381 unsigned n_arg;
3382 isl_map *map;
3383 isl_space *space;
3384 isl_multi_aff *ma;
3385 std::map<int,int> param2pos;
3387 if (!stmt)
3388 return NULL;
3390 n = pet_nested_n_in_set(stmt->domain);
3391 if (n == 0)
3392 return stmt;
3394 n_arg = stmt->n_arg;
3395 stmt = extract_nested(stmt, n, param2pos);
3396 if (!stmt)
3397 return NULL;
3399 n = stmt->n_arg - n_arg;
3400 nparam = isl_set_dim(stmt->domain, isl_dim_param);
3401 if (isl_set_is_wrapping(stmt->domain))
3402 map = isl_set_unwrap(stmt->domain);
3403 else
3404 map = isl_map_from_domain(stmt->domain);
3405 map = isl_map_insert_dims(map, isl_dim_out, 0, n);
3407 for (int i = nparam - 1; i >= 0; --i) {
3408 isl_id *id;
3410 if (!pet_nested_in_map(map, i))
3411 continue;
3413 id = pet_expr_access_get_id(stmt->args[param2pos[i]]);
3414 map = isl_map_set_dim_id(map, isl_dim_out, param2pos[i], id);
3415 map = isl_map_equate(map, isl_dim_param, i, isl_dim_out,
3416 param2pos[i]);
3417 map = isl_map_project_out(map, isl_dim_param, i, 1);
3420 stmt->domain = isl_map_wrap(map);
3422 space = isl_space_unwrap(isl_set_get_space(stmt->domain));
3423 space = isl_space_from_domain(isl_space_domain(space));
3424 ma = isl_multi_aff_zero(space);
3425 for (int pos = 0; pos < n; ++pos)
3426 stmt->args[pos] = embed(stmt->args[pos], ma);
3427 isl_multi_aff_free(ma);
3429 stmt = pet_stmt_remove_nested_parameters(stmt);
3430 stmt = remove_duplicate_arguments(stmt, n);
3432 return stmt;
3435 /* For each statement in "scop", move the parameters that correspond
3436 * to nested access into the ranges of the domains and create
3437 * corresponding argument expressions.
3439 struct pet_scop *PetScan::resolve_nested(struct pet_scop *scop)
3441 if (!scop)
3442 return NULL;
3444 for (int i = 0; i < scop->n_stmt; ++i) {
3445 scop->stmts[i] = resolve_nested(scop->stmts[i]);
3446 if (!scop->stmts[i])
3447 goto error;
3450 return scop;
3451 error:
3452 pet_scop_free(scop);
3453 return NULL;
3456 /* Given an access expression "expr", is the variable accessed by
3457 * "expr" assigned anywhere inside "scop"?
3459 static bool is_assigned(__isl_keep pet_expr *expr, pet_scop *scop)
3461 bool assigned = false;
3462 isl_id *id;
3464 id = pet_expr_access_get_id(expr);
3465 assigned = pet_scop_writes(scop, id);
3466 isl_id_free(id);
3468 return assigned;
3471 /* Are all nested access parameters in "pa" allowed given "scop".
3472 * In particular, is none of them written by anywhere inside "scop".
3474 * If "scop" has any skip conditions, then no nested access parameters
3475 * are allowed. In particular, if there is any nested access in a guard
3476 * for a piece of code containing a "continue", then we want to introduce
3477 * a separate statement for evaluating this guard so that we can express
3478 * that the result is false for all previous iterations.
3480 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff *pa, pet_scop *scop)
3482 int nparam;
3484 if (!scop)
3485 return true;
3487 if (!pet_nested_any_in_pw_aff(pa))
3488 return true;
3490 if (pet_scop_has_skip(scop, pet_skip_now))
3491 return false;
3493 nparam = isl_pw_aff_dim(pa, isl_dim_param);
3494 for (int i = 0; i < nparam; ++i) {
3495 isl_id *id = isl_pw_aff_get_dim_id(pa, isl_dim_param, i);
3496 pet_expr *expr;
3497 bool allowed;
3499 if (!pet_nested_in_id(id)) {
3500 isl_id_free(id);
3501 continue;
3504 expr = pet_nested_extract_expr(id);
3505 allowed = pet_expr_get_type(expr) == pet_expr_access &&
3506 !is_assigned(expr, scop);
3508 pet_expr_free(expr);
3509 isl_id_free(id);
3511 if (!allowed)
3512 return false;
3515 return true;
3518 /* Construct a pet_scop for a non-affine if statement within the context "pc".
3520 * We create a separate statement that writes the result
3521 * of the non-affine condition to a virtual scalar.
3522 * A constraint requiring the value of this virtual scalar to be one
3523 * is added to the iteration domains of the then branch.
3524 * Similarly, a constraint requiring the value of this virtual scalar
3525 * to be zero is added to the iteration domains of the else branch, if any.
3526 * We adjust the schedules to ensure that the virtual scalar is written
3527 * before it is read.
3529 * If there are any breaks or continues in the then and/or else
3530 * branches, then we may have to compute a new skip condition.
3531 * This is handled using a pet_skip_info object.
3532 * On initialization, the object checks if skip conditions need
3533 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
3534 * adds them in pet_skip_info_if_add.
3536 struct pet_scop *PetScan::extract_non_affine_if(Expr *cond,
3537 struct pet_scop *scop_then, struct pet_scop *scop_else,
3538 bool have_else, int stmt_id, __isl_take pet_context *pc)
3540 struct pet_scop *scop;
3541 isl_multi_pw_aff *test_index;
3542 int int_size;
3543 int save_n_stmt = n_stmt;
3545 test_index = pet_create_test_index(ctx, n_test++);
3546 n_stmt = stmt_id;
3547 scop = extract_non_affine_condition(cond, n_stmt++,
3548 isl_multi_pw_aff_copy(test_index), pc);
3549 n_stmt = save_n_stmt;
3550 scop = scop_add_array(scop, test_index, ast_context);
3552 pet_skip_info skip;
3553 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else, have_else, 0);
3554 int_size = ast_context.getTypeInfo(ast_context.IntTy).first / 8;
3555 pet_skip_info_if_extract_index(&skip, test_index, int_size,
3556 &n_stmt, &n_test);
3558 scop = pet_scop_prefix(scop, 0);
3559 scop_then = pet_scop_prefix(scop_then, 1);
3560 scop_then = pet_scop_filter(scop_then,
3561 isl_multi_pw_aff_copy(test_index), 1);
3562 if (have_else) {
3563 scop_else = pet_scop_prefix(scop_else, 1);
3564 scop_else = pet_scop_filter(scop_else, test_index, 0);
3565 scop_then = pet_scop_add_par(ctx, scop_then, scop_else);
3566 } else
3567 isl_multi_pw_aff_free(test_index);
3569 scop = pet_scop_add_seq(ctx, scop, scop_then);
3571 scop = pet_skip_info_if_add(&skip, scop, 2);
3573 pet_context_free(pc);
3574 return scop;
3577 /* Construct a pet_scop for an if statement within the context "pc".
3579 * If the condition fits the pattern of a conditional assignment,
3580 * then it is handled by extract_conditional_assignment.
3581 * Otherwise, we do the following.
3583 * If the condition is affine, then the condition is added
3584 * to the iteration domains of the then branch, while the
3585 * opposite of the condition in added to the iteration domains
3586 * of the else branch, if any.
3587 * We allow the condition to be dynamic, i.e., to refer to
3588 * scalars or array elements that may be written to outside
3589 * of the given if statement. These nested accesses are then represented
3590 * as output dimensions in the wrapping iteration domain.
3591 * If it is also written _inside_ the then or else branch, then
3592 * we treat the condition as non-affine.
3593 * As explained in extract_non_affine_if, this will introduce
3594 * an extra statement.
3595 * For aesthetic reasons, we want this statement to have a statement
3596 * number that is lower than those of the then and else branches.
3597 * In order to evaluate if we will need such a statement, however, we
3598 * first construct scops for the then and else branches.
3599 * We therefore reserve a statement number if we might have to
3600 * introduce such an extra statement.
3602 * If the condition is not affine, then the scop is created in
3603 * extract_non_affine_if.
3605 * If there are any breaks or continues in the then and/or else
3606 * branches, then we may have to compute a new skip condition.
3607 * This is handled using a pet_skip_info object.
3608 * On initialization, the object checks if skip conditions need
3609 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
3610 * adds them in pet_skip_info_if_add.
3612 struct pet_scop *PetScan::extract(IfStmt *stmt, __isl_keep pet_context *pc)
3614 struct pet_scop *scop_then, *scop_else = NULL, *scop;
3615 isl_pw_aff *cond;
3616 int stmt_id;
3617 int int_size;
3618 isl_set *set;
3619 isl_set *valid;
3621 pc = pet_context_copy(pc);
3622 clear_assignments clear(pc);
3623 clear.TraverseStmt(stmt->getThen());
3624 if (stmt->getElse())
3625 clear.TraverseStmt(stmt->getElse());
3627 scop = extract_conditional_assignment(stmt, pc);
3628 if (scop) {
3629 pet_context_free(pc);
3630 return scop;
3633 cond = try_extract_nested_condition(stmt->getCond(), pc);
3634 if (allow_nested && (!cond || pet_nested_any_in_pw_aff(cond)))
3635 stmt_id = n_stmt++;
3637 scop_then = extract(stmt->getThen(), pc);
3639 if (stmt->getElse()) {
3640 scop_else = extract(stmt->getElse(), pc);
3641 if (options->autodetect) {
3642 if (scop_then && !scop_else) {
3643 partial = true;
3644 isl_pw_aff_free(cond);
3645 pet_context_free(pc);
3646 return scop_then;
3648 if (!scop_then && scop_else) {
3649 partial = true;
3650 isl_pw_aff_free(cond);
3651 pet_context_free(pc);
3652 return scop_else;
3657 if (cond &&
3658 (!is_nested_allowed(cond, scop_then) ||
3659 (stmt->getElse() && !is_nested_allowed(cond, scop_else)))) {
3660 isl_pw_aff_free(cond);
3661 cond = NULL;
3663 if (allow_nested && !cond)
3664 return extract_non_affine_if(stmt->getCond(), scop_then,
3665 scop_else, stmt->getElse(), stmt_id, pc);
3667 if (!cond)
3668 cond = extract_condition(stmt->getCond(), pc);
3670 pet_skip_info skip;
3671 pet_skip_info_if_init(&skip, ctx, scop_then, scop_else,
3672 stmt->getElse() != NULL, 1);
3673 pet_skip_info_if_extract_cond(&skip, cond, int_size, &n_stmt, &n_test);
3675 valid = isl_pw_aff_domain(isl_pw_aff_copy(cond));
3676 set = isl_pw_aff_non_zero_set(cond);
3677 scop = pet_scop_restrict(scop_then, isl_set_params(isl_set_copy(set)));
3679 if (stmt->getElse()) {
3680 set = isl_set_subtract(isl_set_copy(valid), set);
3681 scop_else = pet_scop_restrict(scop_else, isl_set_params(set));
3682 scop = pet_scop_add_par(ctx, scop, scop_else);
3683 } else
3684 isl_set_free(set);
3685 scop = resolve_nested(scop);
3686 scop = pet_scop_restrict_context(scop, isl_set_params(valid));
3688 if (pet_skip_info_has_skip(&skip))
3689 scop = pet_scop_prefix(scop, 0);
3690 scop = pet_skip_info_if_add(&skip, scop, 1);
3692 pet_context_free(pc);
3693 return scop;
3696 /* Try and construct a pet_scop for a label statement within the context "pc".
3697 * We currently only allow labels on expression statements.
3699 struct pet_scop *PetScan::extract(LabelStmt *stmt, __isl_keep pet_context *pc)
3701 isl_id *label;
3702 Stmt *sub;
3704 sub = stmt->getSubStmt();
3705 if (!isa<Expr>(sub)) {
3706 unsupported(stmt);
3707 return NULL;
3710 label = isl_id_alloc(ctx, stmt->getName(), NULL);
3712 return extract(extract_expr(cast<Expr>(sub)), stmt->getSourceRange(),
3713 true, pc, label);
3716 /* Return a one-dimensional multi piecewise affine expression that is equal
3717 * to the constant 1 and is defined over a zero-dimensional domain.
3719 static __isl_give isl_multi_pw_aff *one_mpa(isl_ctx *ctx)
3721 isl_space *space;
3722 isl_local_space *ls;
3723 isl_aff *aff;
3725 space = isl_space_set_alloc(ctx, 0, 0);
3726 ls = isl_local_space_from_space(space);
3727 aff = isl_aff_zero_on_domain(ls);
3728 aff = isl_aff_set_constant_si(aff, 1);
3730 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
3733 /* Construct a pet_scop for a continue statement.
3735 * We simply create an empty scop with a universal pet_skip_now
3736 * skip condition. This skip condition will then be taken into
3737 * account by the enclosing loop construct, possibly after
3738 * being incorporated into outer skip conditions.
3740 struct pet_scop *PetScan::extract(ContinueStmt *stmt)
3742 pet_scop *scop;
3744 scop = pet_scop_empty(ctx);
3745 if (!scop)
3746 return NULL;
3748 scop = pet_scop_set_skip(scop, pet_skip_now, one_mpa(ctx));
3750 return scop;
3753 /* Construct a pet_scop for a break statement.
3755 * We simply create an empty scop with both a universal pet_skip_now
3756 * skip condition and a universal pet_skip_later skip condition.
3757 * These skip conditions will then be taken into
3758 * account by the enclosing loop construct, possibly after
3759 * being incorporated into outer skip conditions.
3761 struct pet_scop *PetScan::extract(BreakStmt *stmt)
3763 pet_scop *scop;
3764 isl_multi_pw_aff *skip;
3766 scop = pet_scop_empty(ctx);
3767 if (!scop)
3768 return NULL;
3770 skip = one_mpa(ctx);
3771 scop = pet_scop_set_skip(scop, pet_skip_now,
3772 isl_multi_pw_aff_copy(skip));
3773 scop = pet_scop_set_skip(scop, pet_skip_later, skip);
3775 return scop;
3778 /* Try and construct a pet_scop corresponding to "stmt"
3779 * within the context "pc".
3781 * If "stmt" is a compound statement, then "skip_declarations"
3782 * indicates whether we should skip initial declarations in the
3783 * compound statement.
3785 * If the constructed pet_scop is not a (possibly) partial representation
3786 * of "stmt", we update start and end of the pet_scop to those of "stmt".
3787 * In particular, if skip_declarations is set, then we may have skipped
3788 * declarations inside "stmt" and so the pet_scop may not represent
3789 * the entire "stmt".
3790 * Note that this function may be called with "stmt" referring to the entire
3791 * body of the function, including the outer braces. In such cases,
3792 * skip_declarations will be set and the braces will not be taken into
3793 * account in scop->start and scop->end.
3795 struct pet_scop *PetScan::extract(Stmt *stmt, __isl_keep pet_context *pc,
3796 bool skip_declarations)
3798 struct pet_scop *scop;
3800 if (isa<Expr>(stmt))
3801 return extract(extract_expr(cast<Expr>(stmt)),
3802 stmt->getSourceRange(), true, pc);
3804 switch (stmt->getStmtClass()) {
3805 case Stmt::WhileStmtClass:
3806 scop = extract(cast<WhileStmt>(stmt), pc);
3807 break;
3808 case Stmt::ForStmtClass:
3809 scop = extract_for(cast<ForStmt>(stmt), pc);
3810 break;
3811 case Stmt::IfStmtClass:
3812 scop = extract(cast<IfStmt>(stmt), pc);
3813 break;
3814 case Stmt::CompoundStmtClass:
3815 scop = extract(cast<CompoundStmt>(stmt), pc, skip_declarations);
3816 break;
3817 case Stmt::LabelStmtClass:
3818 scop = extract(cast<LabelStmt>(stmt), pc);
3819 break;
3820 case Stmt::ContinueStmtClass:
3821 scop = extract(cast<ContinueStmt>(stmt));
3822 break;
3823 case Stmt::BreakStmtClass:
3824 scop = extract(cast<BreakStmt>(stmt));
3825 break;
3826 case Stmt::DeclStmtClass:
3827 scop = extract(cast<DeclStmt>(stmt), pc);
3828 break;
3829 default:
3830 unsupported(stmt);
3831 return NULL;
3834 if (partial || skip_declarations)
3835 return scop;
3837 scop = update_scop_start_end(scop, stmt->getSourceRange(), false);
3839 return scop;
3842 /* Extract a clone of the kill statement in "scop".
3843 * "scop" is expected to have been created from a DeclStmt
3844 * and should have the kill as its first statement.
3846 struct pet_stmt *PetScan::extract_kill(struct pet_scop *scop)
3848 pet_expr *kill;
3849 struct pet_stmt *stmt;
3850 isl_multi_pw_aff *index;
3851 isl_map *access;
3852 pet_expr *arg;
3854 if (!scop)
3855 return NULL;
3856 if (scop->n_stmt < 1)
3857 isl_die(ctx, isl_error_internal,
3858 "expecting at least one statement", return NULL);
3859 stmt = scop->stmts[0];
3860 if (!pet_stmt_is_kill(stmt))
3861 isl_die(ctx, isl_error_internal,
3862 "expecting kill statement", return NULL);
3864 arg = pet_expr_get_arg(stmt->body, 0);
3865 index = pet_expr_access_get_index(arg);
3866 access = pet_expr_access_get_access(arg);
3867 pet_expr_free(arg);
3868 index = isl_multi_pw_aff_reset_tuple_id(index, isl_dim_in);
3869 access = isl_map_reset_tuple_id(access, isl_dim_in);
3870 kill = pet_expr_kill_from_access_and_index(access, index);
3871 return pet_stmt_from_pet_expr(stmt->line, NULL, n_stmt++, kill);
3874 /* Mark all arrays in "scop" as being exposed.
3876 static struct pet_scop *mark_exposed(struct pet_scop *scop)
3878 if (!scop)
3879 return NULL;
3880 for (int i = 0; i < scop->n_array; ++i)
3881 scop->arrays[i]->exposed = 1;
3882 return scop;
3885 /* Try and construct a pet_scop corresponding to (part of)
3886 * a sequence of statements within the context "pc".
3888 * "block" is set if the sequence respresents the children of
3889 * a compound statement.
3890 * "skip_declarations" is set if we should skip initial declarations
3891 * in the sequence of statements.
3893 * After extracting a statement, we update "pc"
3894 * based on the top-level assignments in the statement
3895 * so that we can exploit them in subsequent statements in the same block.
3897 * If there are any breaks or continues in the individual statements,
3898 * then we may have to compute a new skip condition.
3899 * This is handled using a pet_skip_info object.
3900 * On initialization, the object checks if skip conditions need
3901 * to be computed. If so, it does so in pet_skip_info_seq_extract and
3902 * adds them in pet_skip_info_seq_add.
3904 * If "block" is set, then we need to insert kill statements at
3905 * the end of the block for any array that has been declared by
3906 * one of the statements in the sequence. Each of these declarations
3907 * results in the construction of a kill statement at the place
3908 * of the declaration, so we simply collect duplicates of
3909 * those kill statements and append these duplicates to the constructed scop.
3911 * If "block" is not set, then any array declared by one of the statements
3912 * in the sequence is marked as being exposed.
3914 * If autodetect is set, then we allow the extraction of only a subrange
3915 * of the sequence of statements. However, if there is at least one statement
3916 * for which we could not construct a scop and the final range contains
3917 * either no statements or at least one kill, then we discard the entire
3918 * range.
3920 struct pet_scop *PetScan::extract(StmtRange stmt_range, bool block,
3921 bool skip_declarations, __isl_keep pet_context *pc)
3923 pet_scop *scop;
3924 StmtIterator i;
3925 int int_size;
3926 int j;
3927 bool partial_range = false;
3928 set<struct pet_stmt *> kills;
3929 set<struct pet_stmt *>::iterator it;
3931 int_size = ast_context.getTypeInfo(ast_context.IntTy).first / 8;
3933 pc = pet_context_copy(pc);
3934 scop = pet_scop_empty(ctx);
3935 for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j) {
3936 Stmt *child = *i;
3937 struct pet_scop *scop_i;
3939 if (scop->n_stmt == 0 && skip_declarations &&
3940 child->getStmtClass() == Stmt::DeclStmtClass)
3941 continue;
3943 scop_i = extract(child, pc);
3944 if (scop->n_stmt != 0 && partial) {
3945 pet_scop_free(scop_i);
3946 break;
3948 pc = handle_writes(scop_i, pc);
3949 pet_skip_info skip;
3950 pet_skip_info_seq_init(&skip, ctx, scop, scop_i);
3951 pet_skip_info_seq_extract(&skip, int_size, &n_stmt, &n_test);
3952 if (pet_skip_info_has_skip(&skip))
3953 scop_i = pet_scop_prefix(scop_i, 0);
3954 if (scop_i && child->getStmtClass() == Stmt::DeclStmtClass) {
3955 if (block)
3956 kills.insert(extract_kill(scop_i));
3957 else
3958 scop_i = mark_exposed(scop_i);
3960 scop_i = pet_scop_prefix(scop_i, j);
3961 if (options->autodetect) {
3962 if (scop_i)
3963 scop = pet_scop_add_seq(ctx, scop, scop_i);
3964 else
3965 partial_range = true;
3966 if (scop->n_stmt != 0 && !scop_i)
3967 partial = true;
3968 } else {
3969 scop = pet_scop_add_seq(ctx, scop, scop_i);
3972 scop = pet_skip_info_seq_add(&skip, scop, j);
3974 if (partial || !scop)
3975 break;
3978 for (it = kills.begin(); it != kills.end(); ++it) {
3979 pet_scop *scop_j;
3980 scop_j = pet_scop_from_pet_stmt(ctx, *it);
3981 scop_j = pet_scop_prefix(scop_j, j);
3982 scop = pet_scop_add_seq(ctx, scop, scop_j);
3985 pet_context_free(pc);
3987 if (scop && partial_range) {
3988 if (scop->n_stmt == 0 || kills.size() != 0) {
3989 pet_scop_free(scop);
3990 return NULL;
3992 partial = true;
3995 return scop;
3998 /* Check if the scop marked by the user is exactly this Stmt
3999 * or part of this Stmt.
4000 * If so, return a pet_scop corresponding to the marked region.
4001 * The pet_scop is created within the context "pc".
4002 * Otherwise, return NULL.
4004 struct pet_scop *PetScan::scan(Stmt *stmt, __isl_keep pet_context *pc)
4006 SourceManager &SM = PP.getSourceManager();
4007 unsigned start_off, end_off;
4009 start_off = getExpansionOffset(SM, stmt->getLocStart());
4010 end_off = getExpansionOffset(SM, stmt->getLocEnd());
4012 if (start_off > loc.end)
4013 return NULL;
4014 if (end_off < loc.start)
4015 return NULL;
4016 if (start_off >= loc.start && end_off <= loc.end) {
4017 return extract(stmt, pc);
4020 StmtIterator start;
4021 for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
4022 Stmt *child = *start;
4023 if (!child)
4024 continue;
4025 start_off = getExpansionOffset(SM, child->getLocStart());
4026 end_off = getExpansionOffset(SM, child->getLocEnd());
4027 if (start_off < loc.start && end_off >= loc.end)
4028 return scan(child, pc);
4029 if (start_off >= loc.start)
4030 break;
4033 StmtIterator end;
4034 for (end = start; end != stmt->child_end(); ++end) {
4035 Stmt *child = *end;
4036 start_off = SM.getFileOffset(child->getLocStart());
4037 if (start_off >= loc.end)
4038 break;
4041 return extract(StmtRange(start, end), false, false, pc);
4044 /* Set the size of index "pos" of "array" to "size".
4045 * In particular, add a constraint of the form
4047 * i_pos < size
4049 * to array->extent and a constraint of the form
4051 * size >= 0
4053 * to array->context.
4055 static struct pet_array *update_size(struct pet_array *array, int pos,
4056 __isl_take isl_pw_aff *size)
4058 isl_set *valid;
4059 isl_set *univ;
4060 isl_set *bound;
4061 isl_space *dim;
4062 isl_aff *aff;
4063 isl_pw_aff *index;
4064 isl_id *id;
4066 if (!array)
4067 goto error;
4069 valid = isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size)));
4070 array->context = isl_set_intersect(array->context, valid);
4072 dim = isl_set_get_space(array->extent);
4073 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
4074 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
4075 univ = isl_set_universe(isl_aff_get_domain_space(aff));
4076 index = isl_pw_aff_alloc(univ, aff);
4078 size = isl_pw_aff_add_dims(size, isl_dim_in,
4079 isl_set_dim(array->extent, isl_dim_set));
4080 id = isl_set_get_tuple_id(array->extent);
4081 size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
4082 bound = isl_pw_aff_lt_set(index, size);
4084 array->extent = isl_set_intersect(array->extent, bound);
4086 if (!array->context || !array->extent)
4087 return pet_array_free(array);
4089 return array;
4090 error:
4091 isl_pw_aff_free(size);
4092 return NULL;
4095 /* Figure out the size of the array at position "pos" and all
4096 * subsequent positions from "type" and update the corresponding
4097 * argument of "expr" accordingly.
4099 __isl_give pet_expr *PetScan::set_upper_bounds(__isl_take pet_expr *expr,
4100 const Type *type, int pos)
4102 const ArrayType *atype;
4103 pet_expr *size;
4105 if (!expr)
4106 return NULL;
4108 if (type->isPointerType()) {
4109 type = type->getPointeeType().getTypePtr();
4110 return set_upper_bounds(expr, type, pos + 1);
4112 if (!type->isArrayType())
4113 return expr;
4115 type = type->getCanonicalTypeInternal().getTypePtr();
4116 atype = cast<ArrayType>(type);
4118 if (type->isConstantArrayType()) {
4119 const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
4120 size = extract_expr(ca->getSize());
4121 expr = pet_expr_set_arg(expr, pos, size);
4122 } else if (type->isVariableArrayType()) {
4123 const VariableArrayType *vla = cast<VariableArrayType>(atype);
4124 size = extract_expr(vla->getSizeExpr());
4125 expr = pet_expr_set_arg(expr, pos, size);
4128 type = atype->getElementType().getTypePtr();
4130 return set_upper_bounds(expr, type, pos + 1);
4133 /* Does "expr" represent the "integer" infinity?
4135 static int is_infty(__isl_keep pet_expr *expr)
4137 isl_val *v;
4138 int res;
4140 if (pet_expr_get_type(expr) != pet_expr_int)
4141 return 0;
4142 v = pet_expr_int_get_val(expr);
4143 res = isl_val_is_infty(v);
4144 isl_val_free(v);
4146 return res;
4149 /* Figure out the dimensions of an array "array" based on its type
4150 * "type" and update "array" accordingly.
4152 * We first construct a pet_expr that holds the sizes of the array
4153 * in each dimension. The expression is initialized to infinity
4154 * and updated from the type.
4156 * The arguments of the size expression that have been updated
4157 * are then converted to an affine expression within the context "pc" and
4158 * incorporated into the size of "array". If we are unable to convert
4159 * a size expression to an affine expression, then we leave
4160 * the corresponding size of "array" untouched.
4162 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
4163 const Type *type, __isl_keep pet_context *pc)
4165 int depth = array_depth(type);
4166 pet_expr *expr, *inf;
4168 if (!array)
4169 return NULL;
4171 inf = pet_expr_new_int(isl_val_infty(ctx));
4172 expr = pet_expr_new_call(ctx, "bounds", depth);
4173 for (int i = 0; i < depth; ++i)
4174 expr = pet_expr_set_arg(expr, i, pet_expr_copy(inf));
4175 pet_expr_free(inf);
4177 expr = set_upper_bounds(expr, type, 0);
4179 for (int i = 0; i < depth; ++i) {
4180 pet_expr *arg;
4181 isl_pw_aff *size;
4183 arg = pet_expr_get_arg(expr, i);
4184 if (!is_infty(arg)) {
4185 size = pet_expr_extract_affine(arg, pc);
4186 if (!size)
4187 array = pet_array_free(array);
4188 else if (isl_pw_aff_involves_nan(size))
4189 isl_pw_aff_free(size);
4190 else
4191 array = update_size(array, i, size);
4193 pet_expr_free(arg);
4195 pet_expr_free(expr);
4197 return array;
4200 /* Is "T" the type of a variable length array with static size?
4202 static bool is_vla_with_static_size(QualType T)
4204 const VariableArrayType *vlatype;
4206 if (!T->isVariableArrayType())
4207 return false;
4208 vlatype = cast<VariableArrayType>(T);
4209 return vlatype->getSizeModifier() == VariableArrayType::Static;
4212 /* Return the type of "decl" as an array.
4214 * In particular, if "decl" is a parameter declaration that
4215 * is a variable length array with a static size, then
4216 * return the original type (i.e., the variable length array).
4217 * Otherwise, return the type of decl.
4219 static QualType get_array_type(ValueDecl *decl)
4221 ParmVarDecl *parm;
4222 QualType T;
4224 parm = dyn_cast<ParmVarDecl>(decl);
4225 if (!parm)
4226 return decl->getType();
4228 T = parm->getOriginalType();
4229 if (!is_vla_with_static_size(T))
4230 return decl->getType();
4231 return T;
4234 /* Does "decl" have definition that we can keep track of in a pet_type?
4236 static bool has_printable_definition(RecordDecl *decl)
4238 if (!decl->getDeclName())
4239 return false;
4240 return decl->getLexicalDeclContext() == decl->getDeclContext();
4243 /* Construct and return a pet_array corresponding to the variable "decl".
4244 * In particular, initialize array->extent to
4246 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
4248 * and then call set_upper_bounds to set the upper bounds on the indices
4249 * based on the type of the variable. The upper bounds are converted
4250 * to affine expressions within the context "pc".
4252 * If the base type is that of a record with a top-level definition and
4253 * if "types" is not null, then the RecordDecl corresponding to the type
4254 * is added to "types".
4256 * If the base type is that of a record with no top-level definition,
4257 * then we replace it by "<subfield>".
4259 struct pet_array *PetScan::extract_array(isl_ctx *ctx, ValueDecl *decl,
4260 lex_recorddecl_set *types, __isl_keep pet_context *pc)
4262 struct pet_array *array;
4263 QualType qt = get_array_type(decl);
4264 const Type *type = qt.getTypePtr();
4265 int depth = array_depth(type);
4266 QualType base = pet_clang_base_type(qt);
4267 string name;
4268 isl_id *id;
4269 isl_space *dim;
4271 array = isl_calloc_type(ctx, struct pet_array);
4272 if (!array)
4273 return NULL;
4275 id = create_decl_id(ctx, decl);
4276 dim = isl_space_set_alloc(ctx, 0, depth);
4277 dim = isl_space_set_tuple_id(dim, isl_dim_set, id);
4279 array->extent = isl_set_nat_universe(dim);
4281 dim = isl_space_params_alloc(ctx, 0);
4282 array->context = isl_set_universe(dim);
4284 array = set_upper_bounds(array, type, pc);
4285 if (!array)
4286 return NULL;
4288 name = base.getAsString();
4290 if (types && base->isRecordType()) {
4291 RecordDecl *decl = pet_clang_record_decl(base);
4292 if (has_printable_definition(decl))
4293 types->insert(decl);
4294 else
4295 name = "<subfield>";
4298 array->element_type = strdup(name.c_str());
4299 array->element_is_record = base->isRecordType();
4300 array->element_size = decl->getASTContext().getTypeInfo(base).first / 8;
4302 return array;
4305 /* Construct and return a pet_array corresponding to the sequence
4306 * of declarations "decls".
4307 * The upper bounds of the array are converted to affine expressions
4308 * within the context "pc".
4309 * If the sequence contains a single declaration, then it corresponds
4310 * to a simple array access. Otherwise, it corresponds to a member access,
4311 * with the declaration for the substructure following that of the containing
4312 * structure in the sequence of declarations.
4313 * We start with the outermost substructure and then combine it with
4314 * information from the inner structures.
4316 * Additionally, keep track of all required types in "types".
4318 struct pet_array *PetScan::extract_array(isl_ctx *ctx,
4319 vector<ValueDecl *> decls, lex_recorddecl_set *types,
4320 __isl_keep pet_context *pc)
4322 struct pet_array *array;
4323 vector<ValueDecl *>::iterator it;
4325 it = decls.begin();
4327 array = extract_array(ctx, *it, types, pc);
4329 for (++it; it != decls.end(); ++it) {
4330 struct pet_array *parent;
4331 const char *base_name, *field_name;
4332 char *product_name;
4334 parent = array;
4335 array = extract_array(ctx, *it, types, pc);
4336 if (!array)
4337 return pet_array_free(parent);
4339 base_name = isl_set_get_tuple_name(parent->extent);
4340 field_name = isl_set_get_tuple_name(array->extent);
4341 product_name = pet_array_member_access_name(ctx,
4342 base_name, field_name);
4344 array->extent = isl_set_product(isl_set_copy(parent->extent),
4345 array->extent);
4346 if (product_name)
4347 array->extent = isl_set_set_tuple_name(array->extent,
4348 product_name);
4349 array->context = isl_set_intersect(array->context,
4350 isl_set_copy(parent->context));
4352 pet_array_free(parent);
4353 free(product_name);
4355 if (!array->extent || !array->context || !product_name)
4356 return pet_array_free(array);
4359 return array;
4362 /* Add a pet_type corresponding to "decl" to "scop, provided
4363 * it is a member of "types" and it has not been added before
4364 * (i.e., it is not a member of "types_done".
4366 * Since we want the user to be able to print the types
4367 * in the order in which they appear in the scop, we need to
4368 * make sure that types of fields in a structure appear before
4369 * that structure. We therefore call ourselves recursively
4370 * on the types of all record subfields.
4372 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
4373 RecordDecl *decl, Preprocessor &PP, lex_recorddecl_set &types,
4374 lex_recorddecl_set &types_done)
4376 string s;
4377 llvm::raw_string_ostream S(s);
4378 RecordDecl::field_iterator it;
4380 if (types.find(decl) == types.end())
4381 return scop;
4382 if (types_done.find(decl) != types_done.end())
4383 return scop;
4385 for (it = decl->field_begin(); it != decl->field_end(); ++it) {
4386 RecordDecl *record;
4387 QualType type = it->getType();
4389 if (!type->isRecordType())
4390 continue;
4391 record = pet_clang_record_decl(type);
4392 scop = add_type(ctx, scop, record, PP, types, types_done);
4395 if (strlen(decl->getName().str().c_str()) == 0)
4396 return scop;
4398 decl->print(S, PrintingPolicy(PP.getLangOpts()));
4399 S.str();
4401 scop->types[scop->n_type] = pet_type_alloc(ctx,
4402 decl->getName().str().c_str(), s.c_str());
4403 if (!scop->types[scop->n_type])
4404 return pet_scop_free(scop);
4406 types_done.insert(decl);
4408 scop->n_type++;
4410 return scop;
4413 /* Construct a list of pet_arrays, one for each array (or scalar)
4414 * accessed inside "scop", add this list to "scop" and return the result.
4415 * The upper bounds of the arrays are converted to affine expressions
4416 * within the context "pc".
4418 * The context of "scop" is updated with the intersection of
4419 * the contexts of all arrays, i.e., constraints on the parameters
4420 * that ensure that the arrays have a valid (non-negative) size.
4422 * If the any of the extracted arrays refers to a member access,
4423 * then also add the required types to "scop".
4425 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop,
4426 __isl_keep pet_context *pc)
4428 int i;
4429 array_desc_set arrays;
4430 array_desc_set::iterator it;
4431 lex_recorddecl_set types;
4432 lex_recorddecl_set types_done;
4433 lex_recorddecl_set::iterator types_it;
4434 int n_array;
4435 struct pet_array **scop_arrays;
4437 if (!scop)
4438 return NULL;
4440 pet_scop_collect_arrays(scop, arrays);
4441 if (arrays.size() == 0)
4442 return scop;
4444 n_array = scop->n_array;
4446 scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
4447 n_array + arrays.size());
4448 if (!scop_arrays)
4449 goto error;
4450 scop->arrays = scop_arrays;
4452 for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
4453 struct pet_array *array;
4454 array = extract_array(ctx, *it, &types, pc);
4455 scop->arrays[n_array + i] = array;
4456 if (!scop->arrays[n_array + i])
4457 goto error;
4458 scop->n_array++;
4459 scop->context = isl_set_intersect(scop->context,
4460 isl_set_copy(array->context));
4461 if (!scop->context)
4462 goto error;
4465 if (types.size() == 0)
4466 return scop;
4468 scop->types = isl_alloc_array(ctx, struct pet_type *, types.size());
4469 if (!scop->types)
4470 goto error;
4472 for (types_it = types.begin(); types_it != types.end(); ++types_it)
4473 scop = add_type(ctx, scop, *types_it, PP, types, types_done);
4475 return scop;
4476 error:
4477 pet_scop_free(scop);
4478 return NULL;
4481 /* Bound all parameters in scop->context to the possible values
4482 * of the corresponding C variable.
4484 static struct pet_scop *add_parameter_bounds(struct pet_scop *scop)
4486 int n;
4488 if (!scop)
4489 return NULL;
4491 n = isl_set_dim(scop->context, isl_dim_param);
4492 for (int i = 0; i < n; ++i) {
4493 isl_id *id;
4494 ValueDecl *decl;
4496 id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
4497 if (pet_nested_in_id(id)) {
4498 isl_id_free(id);
4499 isl_die(isl_set_get_ctx(scop->context),
4500 isl_error_internal,
4501 "unresolved nested parameter", goto error);
4503 decl = (ValueDecl *) isl_id_get_user(id);
4504 isl_id_free(id);
4506 scop->context = set_parameter_bounds(scop->context, i, decl);
4508 if (!scop->context)
4509 goto error;
4512 return scop;
4513 error:
4514 pet_scop_free(scop);
4515 return NULL;
4518 /* Construct a pet_scop from the given function.
4520 * If the scop was delimited by scop and endscop pragmas, then we override
4521 * the file offsets by those derived from the pragmas.
4523 struct pet_scop *PetScan::scan(FunctionDecl *fd)
4525 pet_scop *scop;
4526 Stmt *stmt;
4527 pet_context *pc;
4529 stmt = fd->getBody();
4531 pc = pet_context_alloc(isl_space_set_alloc(ctx, 0, 0));
4532 if (options->autodetect) {
4533 scop = extract(stmt, pc, true);
4534 } else {
4535 scop = scan(stmt, pc);
4536 scop = pet_scop_update_start_end(scop, loc.start, loc.end);
4538 scop = pet_scop_detect_parameter_accesses(scop);
4539 scop = scan_arrays(scop, pc);
4540 pet_context_free(pc);
4541 scop = add_parameter_bounds(scop);
4542 scop = pet_scop_gist(scop, value_bounds);
4544 return scop;