2 * Copyright 2011 Leiden University. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
37 #include <clang/AST/ASTDiagnostic.h>
38 #include <clang/AST/Expr.h>
39 #include <clang/AST/RecursiveASTVisitor.h>
42 #include <isl/space.h>
48 #include "scop_plus.h"
53 using namespace clang
;
55 /* Look for any assignments to scalar variables in part of the parse
56 * tree and set assigned_value to NULL for each of them.
57 * Also reset assigned_value if the address of a scalar variable
60 * This ensures that we won't use any previously stored value
61 * in the current subtree and its parents.
63 struct clear_assignments
: RecursiveASTVisitor
<clear_assignments
> {
64 map
<ValueDecl
*, Expr
*> &assigned_value
;
66 clear_assignments(map
<ValueDecl
*, Expr
*> &assigned_value
) :
67 assigned_value(assigned_value
) {}
69 bool VisitUnaryOperator(UnaryOperator
*expr
) {
74 if (expr
->getOpcode() != UO_AddrOf
)
77 arg
= expr
->getSubExpr();
78 if (arg
->getStmtClass() != Stmt::DeclRefExprClass
)
80 ref
= cast
<DeclRefExpr
>(arg
);
81 decl
= ref
->getDecl();
82 assigned_value
[decl
] = NULL
;
86 bool VisitBinaryOperator(BinaryOperator
*expr
) {
91 if (!expr
->isAssignmentOp())
94 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
)
96 ref
= cast
<DeclRefExpr
>(lhs
);
97 decl
= ref
->getDecl();
98 assigned_value
[decl
] = NULL
;
103 /* Keep a copy of the currently assigned values.
105 * Any variable that is assigned a value inside the current scope
106 * is removed again when we leave the scope (either because it wasn't
107 * stored in the cache or because it has a different value in the cache).
109 struct assigned_value_cache
{
110 map
<ValueDecl
*, Expr
*> &assigned_value
;
111 map
<ValueDecl
*, Expr
*> cache
;
113 assigned_value_cache(map
<ValueDecl
*, Expr
*> &assigned_value
) :
114 assigned_value(assigned_value
), cache(assigned_value
) {}
115 ~assigned_value_cache() {
116 map
<ValueDecl
*, Expr
*>::iterator it
= cache
.begin();
117 for (it
= assigned_value
.begin(); it
!= assigned_value
.end();
120 (cache
.find(it
->first
) != cache
.end() &&
121 cache
[it
->first
] != it
->second
))
122 cache
[it
->first
] = NULL
;
124 assigned_value
= cache
;
128 /* Called if we found something we (currently) cannot handle.
129 * We'll provide more informative warnings later.
131 * We only actually complain if autodetect is false.
133 void PetScan::unsupported(Stmt
*stmt
)
138 SourceLocation loc
= stmt
->getLocStart();
139 Diagnostic
&diag
= PP
.getDiagnostics();
140 unsigned id
= diag
.getCustomDiagID(Diagnostic::Warning
, "unsupported");
141 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
144 /* Extract an integer from "expr" and store it in "v".
146 int PetScan::extract_int(IntegerLiteral
*expr
, isl_int
*v
)
148 const Type
*type
= expr
->getType().getTypePtr();
149 int is_signed
= type
->hasSignedIntegerRepresentation();
152 int64_t i
= expr
->getValue().getSExtValue();
153 isl_int_set_si(*v
, i
);
155 uint64_t i
= expr
->getValue().getZExtValue();
156 isl_int_set_ui(*v
, i
);
162 /* Extract an affine expression from the IntegerLiteral "expr".
164 __isl_give isl_pw_aff
*PetScan::extract_affine(IntegerLiteral
*expr
)
166 isl_space
*dim
= isl_space_set_alloc(ctx
, 0, 0);
167 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
168 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
169 isl_set
*dom
= isl_set_universe(dim
);
173 extract_int(expr
, &v
);
174 aff
= isl_aff_add_constant(aff
, v
);
177 return isl_pw_aff_alloc(dom
, aff
);
180 /* Extract an affine expression from the APInt "val".
182 __isl_give isl_pw_aff
*PetScan::extract_affine(const llvm::APInt
&val
)
184 isl_space
*dim
= isl_space_set_alloc(ctx
, 0, 0);
185 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
186 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
187 isl_set
*dom
= isl_set_universe(dim
);
191 isl_int_set_ui(v
, val
.getZExtValue());
192 aff
= isl_aff_add_constant(aff
, v
);
195 return isl_pw_aff_alloc(dom
, aff
);
198 __isl_give isl_pw_aff
*PetScan::extract_affine(ImplicitCastExpr
*expr
)
200 return extract_affine(expr
->getSubExpr());
203 /* Extract an affine expression from the DeclRefExpr "expr".
205 * If we have recorded an expression that was assigned to the variable
206 * before, then we convert this expressoin to an isl_pw_aff if it is
207 * affine and to an extra parameter otherwise (provided nesting_enabled is set).
209 * Otherwise, we simply return an expression that is equal
210 * to a parameter corresponding to the referenced variable.
212 __isl_give isl_pw_aff
*PetScan::extract_affine(DeclRefExpr
*expr
)
214 ValueDecl
*decl
= expr
->getDecl();
215 const Type
*type
= decl
->getType().getTypePtr();
221 if (!type
->isIntegerType()) {
226 if (assigned_value
.find(decl
) != assigned_value
.end() &&
227 assigned_value
[decl
] != NULL
) {
228 if (is_affine(assigned_value
[decl
]))
229 return extract_affine(assigned_value
[decl
]);
231 return non_affine(expr
);
234 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
235 dim
= isl_space_set_alloc(ctx
, 1, 0);
237 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
239 dom
= isl_set_universe(isl_space_copy(dim
));
240 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
241 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
243 return isl_pw_aff_alloc(dom
, aff
);
246 /* Extract an affine expression from an integer division operation.
247 * In particular, if "expr" is lhs/rhs, then return
249 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
251 * The second argument (rhs) is required to be a (positive) integer constant.
253 __isl_give isl_pw_aff
*PetScan::extract_affine_div(BinaryOperator
*expr
)
256 isl_pw_aff
*lhs
, *lhs_f
, *lhs_c
;
261 rhs_expr
= expr
->getRHS();
262 if (rhs_expr
->getStmtClass() != Stmt::IntegerLiteralClass
) {
267 lhs
= extract_affine(expr
->getLHS());
268 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs
));
271 extract_int(cast
<IntegerLiteral
>(rhs_expr
), &v
);
272 lhs
= isl_pw_aff_scale_down(lhs
, v
);
275 lhs_f
= isl_pw_aff_floor(isl_pw_aff_copy(lhs
));
276 lhs_c
= isl_pw_aff_ceil(lhs
);
277 res
= isl_pw_aff_cond(cond
, lhs_f
, lhs_c
);
282 /* Extract an affine expression from a modulo operation.
283 * In particular, if "expr" is lhs/rhs, then return
285 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
287 * The second argument (rhs) is required to be a (positive) integer constant.
289 __isl_give isl_pw_aff
*PetScan::extract_affine_mod(BinaryOperator
*expr
)
292 isl_pw_aff
*lhs
, *lhs_f
, *lhs_c
;
297 rhs_expr
= expr
->getRHS();
298 if (rhs_expr
->getStmtClass() != Stmt::IntegerLiteralClass
) {
303 lhs
= extract_affine(expr
->getLHS());
304 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs
));
307 extract_int(cast
<IntegerLiteral
>(rhs_expr
), &v
);
308 res
= isl_pw_aff_scale_down(isl_pw_aff_copy(lhs
), v
);
310 lhs_f
= isl_pw_aff_floor(isl_pw_aff_copy(res
));
311 lhs_c
= isl_pw_aff_ceil(res
);
312 res
= isl_pw_aff_cond(cond
, lhs_f
, lhs_c
);
314 res
= isl_pw_aff_scale(res
, v
);
317 res
= isl_pw_aff_sub(lhs
, res
);
322 /* Extract an affine expression from a multiplication operation.
323 * This is only allowed if at least one of the two arguments
324 * is a (piecewise) constant.
326 __isl_give isl_pw_aff
*PetScan::extract_affine_mul(BinaryOperator
*expr
)
331 lhs
= extract_affine(expr
->getLHS());
332 rhs
= extract_affine(expr
->getRHS());
334 if (!isl_pw_aff_is_cst(lhs
) && !isl_pw_aff_is_cst(rhs
)) {
335 isl_pw_aff_free(lhs
);
336 isl_pw_aff_free(rhs
);
341 return isl_pw_aff_mul(lhs
, rhs
);
344 /* Extract an affine expression from an addition or subtraction operation.
346 __isl_give isl_pw_aff
*PetScan::extract_affine_add(BinaryOperator
*expr
)
351 lhs
= extract_affine(expr
->getLHS());
352 rhs
= extract_affine(expr
->getRHS());
354 switch (expr
->getOpcode()) {
356 return isl_pw_aff_add(lhs
, rhs
);
358 return isl_pw_aff_sub(lhs
, rhs
);
360 isl_pw_aff_free(lhs
);
361 isl_pw_aff_free(rhs
);
371 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
,
377 isl_int_set_si(mod
, 1);
378 isl_int_mul_2exp(mod
, mod
, width
);
380 pwaff
= isl_pw_aff_mod(pwaff
, mod
);
387 /* Extract an affine expression from some binary operations.
388 * If the result of the expression is unsigned, then we wrap it
389 * based on the size of the type.
391 __isl_give isl_pw_aff
*PetScan::extract_affine(BinaryOperator
*expr
)
395 switch (expr
->getOpcode()) {
398 res
= extract_affine_add(expr
);
401 res
= extract_affine_div(expr
);
404 res
= extract_affine_mod(expr
);
407 res
= extract_affine_mul(expr
);
414 if (expr
->getType()->isUnsignedIntegerType())
415 res
= wrap(res
, ast_context
.getIntWidth(expr
->getType()));
420 /* Extract an affine expression from a negation operation.
422 __isl_give isl_pw_aff
*PetScan::extract_affine(UnaryOperator
*expr
)
424 if (expr
->getOpcode() == UO_Minus
)
425 return isl_pw_aff_neg(extract_affine(expr
->getSubExpr()));
431 __isl_give isl_pw_aff
*PetScan::extract_affine(ParenExpr
*expr
)
433 return extract_affine(expr
->getSubExpr());
436 /* Extract an affine expression from some special function calls.
437 * In particular, we handle "min", "max", "ceild" and "floord".
438 * In case of the latter two, the second argument needs to be
439 * a (positive) integer constant.
441 __isl_give isl_pw_aff
*PetScan::extract_affine(CallExpr
*expr
)
445 isl_pw_aff
*aff1
, *aff2
;
447 fd
= expr
->getDirectCallee();
453 name
= fd
->getDeclName().getAsString();
454 if (!(expr
->getNumArgs() == 2 && name
== "min") &&
455 !(expr
->getNumArgs() == 2 && name
== "max") &&
456 !(expr
->getNumArgs() == 2 && name
== "floord") &&
457 !(expr
->getNumArgs() == 2 && name
== "ceild")) {
462 if (name
== "min" || name
== "max") {
463 aff1
= extract_affine(expr
->getArg(0));
464 aff2
= extract_affine(expr
->getArg(1));
467 aff1
= isl_pw_aff_min(aff1
, aff2
);
469 aff1
= isl_pw_aff_max(aff1
, aff2
);
470 } else if (name
== "floord" || name
== "ceild") {
472 Expr
*arg2
= expr
->getArg(1);
474 if (arg2
->getStmtClass() != Stmt::IntegerLiteralClass
) {
478 aff1
= extract_affine(expr
->getArg(0));
480 extract_int(cast
<IntegerLiteral
>(arg2
), &v
);
481 aff1
= isl_pw_aff_scale_down(aff1
, v
);
483 if (name
== "floord")
484 aff1
= isl_pw_aff_floor(aff1
);
486 aff1
= isl_pw_aff_ceil(aff1
);
496 /* This method is called when we come across a non-affine expression.
497 * If nesting is allowed, we return a new parameter that corresponds
498 * to the non-affine expression. Otherwise, we simply complain.
500 * The new parameter is resolved in resolve_nested.
502 isl_pw_aff
*PetScan::non_affine(Expr
*expr
)
509 if (!nesting_enabled
) {
514 id
= isl_id_alloc(ctx
, NULL
, expr
);
515 dim
= isl_space_set_alloc(ctx
, 1, 0);
517 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
519 dom
= isl_set_universe(isl_space_copy(dim
));
520 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
521 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
523 return isl_pw_aff_alloc(dom
, aff
);
526 /* Affine expressions are not supposed to contain array accesses,
527 * but if nesting is allowed, we return a parameter corresponding
528 * to the array access.
530 __isl_give isl_pw_aff
*PetScan::extract_affine(ArraySubscriptExpr
*expr
)
532 return non_affine(expr
);
535 /* Extract an affine expression from a conditional operation.
537 __isl_give isl_pw_aff
*PetScan::extract_affine(ConditionalOperator
*expr
)
540 isl_pw_aff
*lhs
, *rhs
;
542 cond
= extract_condition(expr
->getCond());
543 lhs
= extract_affine(expr
->getTrueExpr());
544 rhs
= extract_affine(expr
->getFalseExpr());
546 return isl_pw_aff_cond(cond
, lhs
, rhs
);
549 /* Extract an affine expression, if possible, from "expr".
550 * Otherwise return NULL.
552 __isl_give isl_pw_aff
*PetScan::extract_affine(Expr
*expr
)
554 switch (expr
->getStmtClass()) {
555 case Stmt::ImplicitCastExprClass
:
556 return extract_affine(cast
<ImplicitCastExpr
>(expr
));
557 case Stmt::IntegerLiteralClass
:
558 return extract_affine(cast
<IntegerLiteral
>(expr
));
559 case Stmt::DeclRefExprClass
:
560 return extract_affine(cast
<DeclRefExpr
>(expr
));
561 case Stmt::BinaryOperatorClass
:
562 return extract_affine(cast
<BinaryOperator
>(expr
));
563 case Stmt::UnaryOperatorClass
:
564 return extract_affine(cast
<UnaryOperator
>(expr
));
565 case Stmt::ParenExprClass
:
566 return extract_affine(cast
<ParenExpr
>(expr
));
567 case Stmt::CallExprClass
:
568 return extract_affine(cast
<CallExpr
>(expr
));
569 case Stmt::ArraySubscriptExprClass
:
570 return extract_affine(cast
<ArraySubscriptExpr
>(expr
));
571 case Stmt::ConditionalOperatorClass
:
572 return extract_affine(cast
<ConditionalOperator
>(expr
));
579 __isl_give isl_map
*PetScan::extract_access(ImplicitCastExpr
*expr
)
581 return extract_access(expr
->getSubExpr());
584 /* Return the depth of an array of the given type.
586 static int array_depth(const Type
*type
)
588 if (type
->isPointerType())
589 return 1 + array_depth(type
->getPointeeType().getTypePtr());
590 if (type
->isArrayType()) {
591 const ArrayType
*atype
;
592 type
= type
->getCanonicalTypeInternal().getTypePtr();
593 atype
= cast
<ArrayType
>(type
);
594 return 1 + array_depth(atype
->getElementType().getTypePtr());
599 /* Return the element type of the given array type.
601 static QualType
base_type(QualType qt
)
603 const Type
*type
= qt
.getTypePtr();
605 if (type
->isPointerType())
606 return base_type(type
->getPointeeType());
607 if (type
->isArrayType()) {
608 const ArrayType
*atype
;
609 type
= type
->getCanonicalTypeInternal().getTypePtr();
610 atype
= cast
<ArrayType
>(type
);
611 return base_type(atype
->getElementType());
616 /* Check if the element type corresponding to the given array type
617 * has a const qualifier.
619 static bool const_base(QualType qt
)
621 const Type
*type
= qt
.getTypePtr();
623 if (type
->isPointerType())
624 return const_base(type
->getPointeeType());
625 if (type
->isArrayType()) {
626 const ArrayType
*atype
;
627 type
= type
->getCanonicalTypeInternal().getTypePtr();
628 atype
= cast
<ArrayType
>(type
);
629 return const_base(atype
->getElementType());
632 return qt
.isConstQualified();
635 /* Extract an access relation from a reference to a variable.
636 * If the variable has name "A" and its type corresponds to an
637 * array of depth d, then the returned access relation is of the
640 * { [] -> A[i_1,...,i_d] }
642 __isl_give isl_map
*PetScan::extract_access(DeclRefExpr
*expr
)
644 ValueDecl
*decl
= expr
->getDecl();
645 int depth
= array_depth(decl
->getType().getTypePtr());
646 isl_id
*id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
647 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, depth
);
650 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
652 access_rel
= isl_map_universe(dim
);
657 /* Extract an access relation from an integer contant.
658 * If the value of the constant is "v", then the returned access relation
663 __isl_give isl_map
*PetScan::extract_access(IntegerLiteral
*expr
)
665 return isl_map_from_pw_aff(extract_affine(expr
));
668 /* Try and extract an access relation from the given Expr.
669 * Return NULL if it doesn't work out.
671 __isl_give isl_map
*PetScan::extract_access(Expr
*expr
)
673 switch (expr
->getStmtClass()) {
674 case Stmt::ImplicitCastExprClass
:
675 return extract_access(cast
<ImplicitCastExpr
>(expr
));
676 case Stmt::DeclRefExprClass
:
677 return extract_access(cast
<DeclRefExpr
>(expr
));
678 case Stmt::ArraySubscriptExprClass
:
679 return extract_access(cast
<ArraySubscriptExpr
>(expr
));
686 /* Assign the affine expression "index" to the output dimension "pos" of "map"
687 * and return the result.
689 __isl_give isl_map
*set_index(__isl_take isl_map
*map
, int pos
,
690 __isl_take isl_pw_aff
*index
)
693 int len
= isl_map_dim(map
, isl_dim_out
);
696 index_map
= isl_map_from_pw_aff(index
);
697 index_map
= isl_map_insert_dims(index_map
, isl_dim_out
, 0, pos
);
698 index_map
= isl_map_add_dims(index_map
, isl_dim_out
, len
- pos
- 1);
699 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
700 index_map
= isl_map_set_tuple_id(index_map
, isl_dim_out
, id
);
702 map
= isl_map_intersect(map
, index_map
);
707 /* Extract an access relation from the given array subscript expression.
708 * If nesting is allowed in general, then we turn it on while
709 * examining the index expression.
711 * We first extract an access relation from the base.
712 * This will result in an access relation with a range that corresponds
713 * to the array being accessed and with earlier indices filled in already.
714 * We then extract the current index and fill that in as well.
715 * The position of the current index is based on the type of base.
716 * If base is the actual array variable, then the depth of this type
717 * will be the same as the depth of the array and we will fill in
718 * the first array index.
719 * Otherwise, the depth of the base type will be smaller and we will fill
722 __isl_give isl_map
*PetScan::extract_access(ArraySubscriptExpr
*expr
)
724 Expr
*base
= expr
->getBase();
725 Expr
*idx
= expr
->getIdx();
727 isl_map
*base_access
;
729 int depth
= array_depth(base
->getType().getTypePtr());
731 bool save_nesting
= nesting_enabled
;
733 nesting_enabled
= allow_nested
;
735 base_access
= extract_access(base
);
736 index
= extract_affine(idx
);
738 nesting_enabled
= save_nesting
;
740 pos
= isl_map_dim(base_access
, isl_dim_out
) - depth
;
741 access
= set_index(base_access
, pos
, index
);
746 /* Check if "expr" calls function "minmax" with two arguments and if so
747 * make lhs and rhs refer to these two arguments.
749 static bool is_minmax(Expr
*expr
, const char *minmax
, Expr
*&lhs
, Expr
*&rhs
)
755 if (expr
->getStmtClass() != Stmt::CallExprClass
)
758 call
= cast
<CallExpr
>(expr
);
759 fd
= call
->getDirectCallee();
763 if (call
->getNumArgs() != 2)
766 name
= fd
->getDeclName().getAsString();
770 lhs
= call
->getArg(0);
771 rhs
= call
->getArg(1);
776 /* Check if "expr" is of the form min(lhs, rhs) and if so make
777 * lhs and rhs refer to the two arguments.
779 static bool is_min(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
781 return is_minmax(expr
, "min", lhs
, rhs
);
784 /* Check if "expr" is of the form max(lhs, rhs) and if so make
785 * lhs and rhs refer to the two arguments.
787 static bool is_max(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
789 return is_minmax(expr
, "max", lhs
, rhs
);
792 /* Extract a set of values satisfying the comparison "LHS op RHS"
793 * "comp" is the original statement that "LHS op RHS" is derived from
794 * and is used for diagnostics.
796 * If the comparison is of the form
800 * then the set is constructed as the intersection of the set corresponding
805 * A similar optimization is performed for max(a,b) <= c.
806 * We do this because that will lead to simpler representations of the set.
807 * If isl is ever enhanced to explicitly deal with min and max expressions,
808 * this optimization can be removed.
810 __isl_give isl_set
*PetScan::extract_comparison(BinaryOperatorKind op
,
811 Expr
*LHS
, Expr
*RHS
, Stmt
*comp
)
818 return extract_comparison(BO_LT
, RHS
, LHS
, comp
);
820 return extract_comparison(BO_LE
, RHS
, LHS
, comp
);
822 if (op
== BO_LT
|| op
== BO_LE
) {
824 isl_set
*set1
, *set2
;
825 if (is_min(RHS
, expr1
, expr2
)) {
826 set1
= extract_comparison(op
, LHS
, expr1
, comp
);
827 set2
= extract_comparison(op
, LHS
, expr2
, comp
);
828 return isl_set_intersect(set1
, set2
);
830 if (is_max(LHS
, expr1
, expr2
)) {
831 set1
= extract_comparison(op
, expr1
, RHS
, comp
);
832 set2
= extract_comparison(op
, expr2
, RHS
, comp
);
833 return isl_set_intersect(set1
, set2
);
837 lhs
= extract_affine(LHS
);
838 rhs
= extract_affine(RHS
);
842 cond
= isl_pw_aff_lt_set(lhs
, rhs
);
845 cond
= isl_pw_aff_le_set(lhs
, rhs
);
848 cond
= isl_pw_aff_eq_set(lhs
, rhs
);
851 cond
= isl_pw_aff_ne_set(lhs
, rhs
);
854 isl_pw_aff_free(lhs
);
855 isl_pw_aff_free(rhs
);
860 cond
= isl_set_coalesce(cond
);
865 __isl_give isl_set
*PetScan::extract_comparison(BinaryOperator
*comp
)
867 return extract_comparison(comp
->getOpcode(), comp
->getLHS(),
868 comp
->getRHS(), comp
);
871 /* Extract a set of values satisfying the negation (logical not)
872 * of a subexpression.
874 __isl_give isl_set
*PetScan::extract_boolean(UnaryOperator
*op
)
878 cond
= extract_condition(op
->getSubExpr());
880 return isl_set_complement(cond
);
883 /* Extract a set of values satisfying the union (logical or)
884 * or intersection (logical and) of two subexpressions.
886 __isl_give isl_set
*PetScan::extract_boolean(BinaryOperator
*comp
)
892 lhs
= extract_condition(comp
->getLHS());
893 rhs
= extract_condition(comp
->getRHS());
895 switch (comp
->getOpcode()) {
897 cond
= isl_set_intersect(lhs
, rhs
);
900 cond
= isl_set_union(lhs
, rhs
);
912 __isl_give isl_set
*PetScan::extract_condition(UnaryOperator
*expr
)
914 switch (expr
->getOpcode()) {
916 return extract_boolean(expr
);
923 /* Extract a set of values satisfying the condition "expr != 0".
925 __isl_give isl_set
*PetScan::extract_implicit_condition(Expr
*expr
)
927 return isl_pw_aff_non_zero_set(extract_affine(expr
));
930 /* Extract a set of values satisfying the condition expressed by "expr".
932 * If the expression doesn't look like a condition, we assume it
933 * is an affine expression and return the condition "expr != 0".
935 __isl_give isl_set
*PetScan::extract_condition(Expr
*expr
)
937 BinaryOperator
*comp
;
940 return isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
942 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
943 return extract_condition(cast
<ParenExpr
>(expr
)->getSubExpr());
945 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
)
946 return extract_condition(cast
<UnaryOperator
>(expr
));
948 if (expr
->getStmtClass() != Stmt::BinaryOperatorClass
)
949 return extract_implicit_condition(expr
);
951 comp
= cast
<BinaryOperator
>(expr
);
952 switch (comp
->getOpcode()) {
959 return extract_comparison(comp
);
962 return extract_boolean(comp
);
964 return extract_implicit_condition(expr
);
968 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
978 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
982 return pet_op_add_assign
;
984 return pet_op_sub_assign
;
986 return pet_op_mul_assign
;
988 return pet_op_div_assign
;
990 return pet_op_assign
;
1012 /* Construct a pet_expr representing a unary operator expression.
1014 struct pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
1016 struct pet_expr
*arg
;
1017 enum pet_op_type op
;
1019 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
1020 if (op
== pet_op_last
) {
1025 arg
= extract_expr(expr
->getSubExpr());
1027 return pet_expr_new_unary(ctx
, op
, arg
);
1030 /* Mark the given access pet_expr as a write.
1031 * If a scalar is being accessed, then mark its value
1032 * as unknown in assigned_value.
1034 void PetScan::mark_write(struct pet_expr
*access
)
1039 access
->acc
.write
= 1;
1040 access
->acc
.read
= 0;
1042 if (isl_map_dim(access
->acc
.access
, isl_dim_out
) != 0)
1045 id
= isl_map_get_tuple_id(access
->acc
.access
, isl_dim_out
);
1046 decl
= (ValueDecl
*) isl_id_get_user(id
);
1047 assigned_value
[decl
] = NULL
;
1051 /* Construct a pet_expr representing a binary operator expression.
1053 * If the top level operator is an assignment and the LHS is an access,
1054 * then we mark that access as a write. If the operator is a compound
1055 * assignment, the access is marked as both a read and a write.
1057 * If "expr" assigns something to a scalar variable, then we keep track
1058 * of the assigned expression in assigned_value so that we can plug
1059 * it in when we later come across the same variable.
1061 struct pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
1063 struct pet_expr
*lhs
, *rhs
;
1064 enum pet_op_type op
;
1066 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
1067 if (op
== pet_op_last
) {
1072 lhs
= extract_expr(expr
->getLHS());
1073 rhs
= extract_expr(expr
->getRHS());
1075 if (expr
->isAssignmentOp() && lhs
&& lhs
->type
== pet_expr_access
) {
1077 if (expr
->isCompoundAssignmentOp())
1081 if (expr
->getOpcode() == BO_Assign
&&
1082 lhs
&& lhs
->type
== pet_expr_access
&&
1083 isl_map_dim(lhs
->acc
.access
, isl_dim_out
) == 0) {
1084 isl_id
*id
= isl_map_get_tuple_id(lhs
->acc
.access
, isl_dim_out
);
1085 ValueDecl
*decl
= (ValueDecl
*) isl_id_get_user(id
);
1086 assigned_value
[decl
] = expr
->getRHS();
1090 return pet_expr_new_binary(ctx
, op
, lhs
, rhs
);
1093 /* Construct a pet_expr representing a conditional operation.
1095 struct pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
1097 struct pet_expr
*cond
, *lhs
, *rhs
;
1099 cond
= extract_expr(expr
->getCond());
1100 lhs
= extract_expr(expr
->getTrueExpr());
1101 rhs
= extract_expr(expr
->getFalseExpr());
1103 return pet_expr_new_ternary(ctx
, cond
, lhs
, rhs
);
1106 struct pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
1108 return extract_expr(expr
->getSubExpr());
1111 /* Construct a pet_expr representing a floating point value.
1113 struct pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
1115 return pet_expr_new_double(ctx
, expr
->getValueAsApproximateDouble());
1118 /* Extract an access relation from "expr" and then convert it into
1121 struct pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
1124 struct pet_expr
*pe
;
1126 switch (expr
->getStmtClass()) {
1127 case Stmt::ArraySubscriptExprClass
:
1128 access
= extract_access(cast
<ArraySubscriptExpr
>(expr
));
1130 case Stmt::DeclRefExprClass
:
1131 access
= extract_access(cast
<DeclRefExpr
>(expr
));
1133 case Stmt::IntegerLiteralClass
:
1134 access
= extract_access(cast
<IntegerLiteral
>(expr
));
1141 pe
= pet_expr_from_access(access
);
1146 struct pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
1148 return extract_expr(expr
->getSubExpr());
1151 /* Construct a pet_expr representing a function call.
1153 * If we are passing along a pointer to an array element
1154 * or an entire row or even higher dimensional slice of an array,
1155 * then the function being called may write into the array.
1157 * We assume here that if the function is declared to take a pointer
1158 * to a const type, then the function will perform a read
1159 * and that otherwise, it will perform a write.
1161 struct pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1163 struct pet_expr
*res
= NULL
;
1167 fd
= expr
->getDirectCallee();
1173 name
= fd
->getDeclName().getAsString();
1174 res
= pet_expr_new_call(ctx
, name
.c_str(), expr
->getNumArgs());
1178 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
1179 Expr
*arg
= expr
->getArg(i
);
1182 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
1183 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(arg
);
1184 arg
= ice
->getSubExpr();
1186 if (arg
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1187 UnaryOperator
*op
= cast
<UnaryOperator
>(arg
);
1188 if (op
->getOpcode() == UO_AddrOf
) {
1190 arg
= op
->getSubExpr();
1193 res
->args
[i
] = PetScan::extract_expr(arg
);
1196 if (arg
->getStmtClass() == Stmt::ArraySubscriptExprClass
&&
1197 array_depth(arg
->getType().getTypePtr()) > 0)
1199 if (is_addr
&& res
->args
[i
]->type
== pet_expr_access
) {
1200 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1201 if (!const_base(parm
->getType()))
1202 mark_write(res
->args
[i
]);
1212 /* Try and onstruct a pet_expr representing "expr".
1214 struct pet_expr
*PetScan::extract_expr(Expr
*expr
)
1216 switch (expr
->getStmtClass()) {
1217 case Stmt::UnaryOperatorClass
:
1218 return extract_expr(cast
<UnaryOperator
>(expr
));
1219 case Stmt::CompoundAssignOperatorClass
:
1220 case Stmt::BinaryOperatorClass
:
1221 return extract_expr(cast
<BinaryOperator
>(expr
));
1222 case Stmt::ImplicitCastExprClass
:
1223 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1224 case Stmt::ArraySubscriptExprClass
:
1225 case Stmt::DeclRefExprClass
:
1226 case Stmt::IntegerLiteralClass
:
1227 return extract_access_expr(expr
);
1228 case Stmt::FloatingLiteralClass
:
1229 return extract_expr(cast
<FloatingLiteral
>(expr
));
1230 case Stmt::ParenExprClass
:
1231 return extract_expr(cast
<ParenExpr
>(expr
));
1232 case Stmt::ConditionalOperatorClass
:
1233 return extract_expr(cast
<ConditionalOperator
>(expr
));
1234 case Stmt::CallExprClass
:
1235 return extract_expr(cast
<CallExpr
>(expr
));
1242 /* Check if the given initialization statement is an assignment.
1243 * If so, return that assignment. Otherwise return NULL.
1245 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1247 BinaryOperator
*ass
;
1249 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1252 ass
= cast
<BinaryOperator
>(init
);
1253 if (ass
->getOpcode() != BO_Assign
)
1259 /* Check if the given initialization statement is a declaration
1260 * of a single variable.
1261 * If so, return that declaration. Otherwise return NULL.
1263 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1267 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1270 decl
= cast
<DeclStmt
>(init
);
1272 if (!decl
->isSingleDecl())
1275 return decl
->getSingleDecl();
1278 /* Given the assignment operator in the initialization of a for loop,
1279 * extract the induction variable, i.e., the (integer)variable being
1282 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1289 lhs
= init
->getLHS();
1290 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1295 ref
= cast
<DeclRefExpr
>(lhs
);
1296 decl
= ref
->getDecl();
1297 type
= decl
->getType().getTypePtr();
1299 if (!type
->isIntegerType()) {
1307 /* Given the initialization statement of a for loop and the single
1308 * declaration in this initialization statement,
1309 * extract the induction variable, i.e., the (integer) variable being
1312 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1316 vd
= cast
<VarDecl
>(decl
);
1318 const QualType type
= vd
->getType();
1319 if (!type
->isIntegerType()) {
1324 if (!vd
->getInit()) {
1332 /* Check that op is of the form iv++ or iv--.
1333 * "inc" is accordingly set to 1 or -1.
1335 bool PetScan::check_unary_increment(UnaryOperator
*op
, clang::ValueDecl
*iv
,
1341 if (!op
->isIncrementDecrementOp()) {
1346 if (op
->isIncrementOp())
1347 isl_int_set_si(inc
, 1);
1349 isl_int_set_si(inc
, -1);
1351 sub
= op
->getSubExpr();
1352 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1357 ref
= cast
<DeclRefExpr
>(sub
);
1358 if (ref
->getDecl() != iv
) {
1366 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1367 * has a single constant expression on a universe domain, then
1368 * put this constant in *user.
1370 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
1373 isl_int
*inc
= (isl_int
*)user
;
1376 if (!isl_set_plain_is_universe(set
) || !isl_aff_is_cst(aff
))
1379 isl_aff_get_constant(aff
, inc
);
1387 /* Check if op is of the form
1391 * with inc a constant and set "inc" accordingly.
1393 * We extract an affine expression from the RHS and the subtract iv.
1394 * The result should be a constant.
1396 bool PetScan::check_binary_increment(BinaryOperator
*op
, clang::ValueDecl
*iv
,
1406 if (op
->getOpcode() != BO_Assign
) {
1412 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1417 ref
= cast
<DeclRefExpr
>(lhs
);
1418 if (ref
->getDecl() != iv
) {
1423 val
= extract_affine(op
->getRHS());
1425 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1427 dim
= isl_space_set_alloc(ctx
, 1, 0);
1428 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1429 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1430 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1432 val
= isl_pw_aff_sub(val
, isl_pw_aff_from_aff(aff
));
1434 if (isl_pw_aff_foreach_piece(val
, &extract_cst
, &inc
) < 0) {
1435 isl_pw_aff_free(val
);
1440 isl_pw_aff_free(val
);
1445 /* Check that op is of the form iv += cst or iv -= cst.
1446 * "inc" is set to cst or -cst accordingly.
1448 bool PetScan::check_compound_increment(CompoundAssignOperator
*op
,
1449 clang::ValueDecl
*iv
, isl_int
&inc
)
1455 BinaryOperatorKind opcode
;
1457 opcode
= op
->getOpcode();
1458 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1462 if (opcode
== BO_SubAssign
)
1466 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1471 ref
= cast
<DeclRefExpr
>(lhs
);
1472 if (ref
->getDecl() != iv
) {
1479 if (rhs
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1480 UnaryOperator
*op
= cast
<UnaryOperator
>(rhs
);
1481 if (op
->getOpcode() != UO_Minus
) {
1488 rhs
= op
->getSubExpr();
1491 if (rhs
->getStmtClass() != Stmt::IntegerLiteralClass
) {
1496 extract_int(cast
<IntegerLiteral
>(rhs
), &inc
);
1498 isl_int_neg(inc
, inc
);
1503 /* Check that the increment of the given for loop increments
1504 * (or decrements) the induction variable "iv".
1505 * "up" is set to true if the induction variable is incremented.
1507 bool PetScan::check_increment(ForStmt
*stmt
, ValueDecl
*iv
, isl_int
&v
)
1509 Stmt
*inc
= stmt
->getInc();
1516 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1517 return check_unary_increment(cast
<UnaryOperator
>(inc
), iv
, v
);
1518 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1519 return check_compound_increment(
1520 cast
<CompoundAssignOperator
>(inc
), iv
, v
);
1521 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1522 return check_binary_increment(cast
<BinaryOperator
>(inc
), iv
, v
);
1528 /* Embed the given iteration domain in an extra outer loop
1529 * with induction variable "var".
1530 * If this variable appeared as a parameter in the constraints,
1531 * it is replaced by the new outermost dimension.
1533 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
1534 __isl_take isl_id
*var
)
1538 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
1539 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
1541 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
1542 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
1549 /* Construct a pet_scop for an infinite loop around the given body.
1551 * We extract a pet_scop for the body and then embed it in a loop with
1560 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
)
1566 struct pet_scop
*scop
;
1568 scop
= extract(body
);
1572 id
= isl_id_alloc(ctx
, "t", NULL
);
1573 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
1574 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1575 dim
= isl_space_from_domain(isl_set_get_space(domain
));
1576 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
1577 sched
= isl_map_universe(dim
);
1578 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
1579 scop
= pet_scop_embed(scop
, domain
, sched
, id
);
1584 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1590 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
)
1592 return extract_infinite_loop(stmt
->getBody());
1595 /* Check if the while loop is of the form
1600 * If so, construct a scop for an infinite loop around body.
1603 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
)
1609 cond
= stmt
->getCond();
1615 set
= extract_condition(cond
);
1616 is_universe
= isl_set_plain_is_universe(set
);
1624 return extract_infinite_loop(stmt
->getBody());
1627 /* Check whether "cond" expresses a simple loop bound
1628 * on the only set dimension.
1629 * In particular, if "up" is set then "cond" should contain only
1630 * upper bounds on the set dimension.
1631 * Otherwise, it should contain only lower bounds.
1633 static bool is_simple_bound(__isl_keep isl_set
*cond
, isl_int inc
)
1635 if (isl_int_is_pos(inc
))
1636 return !isl_set_dim_has_lower_bound(cond
, isl_dim_set
, 0);
1638 return !isl_set_dim_has_upper_bound(cond
, isl_dim_set
, 0);
1641 /* Extend a condition on a given iteration of a loop to one that
1642 * imposes the same condition on all previous iterations.
1643 * "domain" expresses the lower [upper] bound on the iterations
1644 * when up is set [not set].
1646 * In particular, we construct the condition (when up is set)
1648 * forall i' : (domain(i') and i' <= i) => cond(i')
1650 * which is equivalent to
1652 * not exists i' : domain(i') and i' <= i and not cond(i')
1654 * We construct this set by negating cond, applying a map
1656 * { [i'] -> [i] : domain(i') and i' <= i }
1658 * and then negating the result again.
1660 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
1661 __isl_take isl_set
*domain
, isl_int inc
)
1663 isl_map
*previous_to_this
;
1665 if (isl_int_is_pos(inc
))
1666 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
1668 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
1670 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
1672 cond
= isl_set_complement(cond
);
1673 cond
= isl_set_apply(cond
, previous_to_this
);
1674 cond
= isl_set_complement(cond
);
1679 /* Construct a domain of the form
1681 * [id] -> { [] : exists a: id = init + a * inc and a >= 0 }
1683 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
1684 __isl_take isl_pw_aff
*init
, isl_int inc
)
1690 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
1691 dim
= isl_pw_aff_get_domain_space(init
);
1692 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1693 aff
= isl_aff_add_coefficient(aff
, isl_dim_in
, 0, inc
);
1694 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1696 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
1697 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1698 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1699 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1701 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1703 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
1705 return isl_set_project_out(set
, isl_dim_set
, 0, 1);
1708 static unsigned get_type_size(ValueDecl
*decl
)
1710 return decl
->getASTContext().getIntWidth(decl
->getType());
1713 /* Assuming "cond" represents a simple bound on a loop where the loop
1714 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1717 * Under the given assumptions, wrapping is only possible if "cond" allows
1718 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1719 * increasing iterator and 0 in case of a decreasing iterator.
1721 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
, isl_int inc
)
1727 test
= isl_set_copy(cond
);
1729 isl_int_init(limit
);
1730 if (isl_int_is_neg(inc
))
1731 isl_int_set_si(limit
, 0);
1733 isl_int_set_si(limit
, 1);
1734 isl_int_mul_2exp(limit
, limit
, get_type_size(iv
));
1735 isl_int_sub_ui(limit
, limit
, 1);
1738 test
= isl_set_fix(cond
, isl_dim_set
, 0, limit
);
1739 cw
= !isl_set_is_empty(test
);
1742 isl_int_clear(limit
);
1747 /* Given a one-dimensional space, construct the following mapping on this
1750 * { [v] -> [v mod 2^width] }
1752 * where width is the number of bits used to represent the values
1753 * of the unsigned variable "iv".
1755 static __isl_give isl_map
*compute_wrapping(__isl_take isl_space
*dim
,
1763 isl_int_set_si(mod
, 1);
1764 isl_int_mul_2exp(mod
, mod
, get_type_size(iv
));
1766 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1767 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
1768 aff
= isl_aff_mod(aff
, mod
);
1772 return isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
1773 map
= isl_map_reverse(map
);
1776 /* Construct a pet_scop for a for statement.
1777 * The for loop is required to be of the form
1779 * for (i = init; condition; ++i)
1783 * for (i = init; condition; --i)
1785 * The initialization of the for loop should either be an assignment
1786 * to an integer variable, or a declaration of such a variable with
1789 * We extract a pet_scop for the body and then embed it in a loop with
1790 * iteration domain and schedule
1792 * { [i] : i >= init and condition' }
1797 * { [i] : i <= init and condition' }
1800 * Where condition' is equal to condition if the latter is
1801 * a simple upper [lower] bound and a condition that is extended
1802 * to apply to all previous iterations otherwise.
1804 * If the stride of the loop is not 1, then "i >= init" is replaced by
1806 * (exists a: i = init + stride * a and a >= 0)
1808 * If the loop iterator i is unsigned, then wrapping may occur.
1809 * During the computation, we work with a virtual iterator that
1810 * does not wrap. However, the condition in the code applies
1811 * to the wrapped value, so we need to change condition(i)
1812 * into condition([i % 2^width]).
1813 * After computing the virtual domain and schedule, we apply
1814 * the function { [v] -> [v % 2^width] } to the domain and the domain
1815 * of the schedule. In order not to lose any information, we also
1816 * need to intersect the domain of the schedule with the virtual domain
1817 * first, since some iterations in the wrapped domain may be scheduled
1818 * several times, typically an infinite number of times.
1819 * Note that there is no need to perform this final wrapping
1820 * if the loop condition (after wrapping) is simple.
1822 * Wrapping on unsigned iterators can be avoided entirely if
1823 * loop condition is simple, the loop iterator is incremented
1824 * [decremented] by one and the last value before wrapping cannot
1825 * possibly satisfy the loop condition.
1827 * Before extracting a pet_scop from the body we remove all
1828 * assignments in assigned_value to variables that are assigned
1829 * somewhere in the body of the loop.
1831 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
)
1833 BinaryOperator
*ass
;
1843 struct pet_scop
*scop
;
1844 assigned_value_cache
cache(assigned_value
);
1849 isl_map
*wrap
= NULL
;
1851 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
1852 return extract_infinite_for(stmt
);
1854 init
= stmt
->getInit();
1859 if ((ass
= initialization_assignment(init
)) != NULL
) {
1860 iv
= extract_induction_variable(ass
);
1863 lhs
= ass
->getLHS();
1864 rhs
= ass
->getRHS();
1865 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
1866 VarDecl
*var
= extract_induction_variable(init
, decl
);
1870 rhs
= var
->getInit();
1871 lhs
= DeclRefExpr::Create(iv
->getASTContext(),
1872 var
->getQualifierLoc(), iv
, var
->getInnerLocStart(),
1873 var
->getType(), VK_LValue
);
1875 unsupported(stmt
->getInit());
1880 if (!check_increment(stmt
, iv
, inc
)) {
1885 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
1887 assigned_value
[iv
] = NULL
;
1888 clear_assignments
clear(assigned_value
);
1889 clear
.TraverseStmt(stmt
->getBody());
1891 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1893 is_one
= isl_int_is_one(inc
) || isl_int_is_negone(inc
);
1895 domain
= extract_comparison(isl_int_is_pos(inc
) ? BO_GE
: BO_LE
,
1898 isl_pw_aff
*lb
= extract_affine(rhs
);
1899 domain
= strided_domain(isl_id_copy(id
), lb
, inc
);
1902 cond
= extract_condition(stmt
->getCond());
1903 cond
= embed(cond
, isl_id_copy(id
));
1904 domain
= embed(domain
, isl_id_copy(id
));
1905 is_simple
= is_simple_bound(cond
, inc
);
1907 (!is_simple
|| !is_one
|| can_wrap(cond
, iv
, inc
))) {
1908 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
1909 cond
= isl_set_apply(cond
, isl_map_reverse(isl_map_copy(wrap
)));
1910 is_simple
= is_simple
&& is_simple_bound(cond
, inc
);
1913 cond
= valid_for_each_iteration(cond
,
1914 isl_set_copy(domain
), inc
);
1915 domain
= isl_set_intersect(domain
, cond
);
1916 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1917 dim
= isl_space_from_domain(isl_set_get_space(domain
));
1918 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
1919 sched
= isl_map_universe(dim
);
1920 if (isl_int_is_pos(inc
))
1921 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
1923 sched
= isl_map_oppose(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
1925 if (is_unsigned
&& !is_simple
) {
1926 wrap
= isl_map_set_dim_id(wrap
,
1927 isl_dim_out
, 0, isl_id_copy(id
));
1928 sched
= isl_map_intersect_domain(sched
, isl_set_copy(domain
));
1929 domain
= isl_set_apply(domain
, isl_map_copy(wrap
));
1930 sched
= isl_map_apply_domain(sched
, wrap
);
1934 scop
= extract(stmt
->getBody());
1935 scop
= pet_scop_embed(scop
, domain
, sched
, id
);
1941 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
)
1943 return extract(stmt
->children());
1946 /* Look for parameters in any access relation in "expr" that
1947 * refer to non-affine constructs. In particular, these are
1948 * parameters with no name.
1950 * If there are any such parameters, then the domain of the access
1951 * relation, which is still [] at this point, is replaced by
1952 * [[] -> [t_1,...,t_n]], with n the number of these parameters
1953 * (after identifying identical non-affine constructs).
1954 * The parameters are then equated to the corresponding t dimensions
1955 * and subsequently projected out.
1956 * param2pos maps the position of the parameter to the position
1957 * of the corresponding t dimension.
1959 struct pet_expr
*PetScan::resolve_nested(struct pet_expr
*expr
)
1966 std::map
<int,int> param2pos
;
1971 for (int i
= 0; i
< expr
->n_arg
; ++i
) {
1972 expr
->args
[i
] = resolve_nested(expr
->args
[i
]);
1973 if (!expr
->args
[i
]) {
1974 pet_expr_free(expr
);
1979 if (expr
->type
!= pet_expr_access
)
1982 nparam
= isl_map_dim(expr
->acc
.access
, isl_dim_param
);
1984 for (int i
= 0; i
< nparam
; ++i
) {
1985 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
1987 if (id
&& isl_id_get_user(id
) && !isl_id_get_name(id
))
1996 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n
);
2000 n_in
= isl_map_dim(expr
->acc
.access
, isl_dim_in
);
2001 for (int i
= 0, pos
= 0; i
< nparam
; ++i
) {
2003 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
2007 if (!(id
&& isl_id_get_user(id
) && !isl_id_get_name(id
))) {
2012 nested
= (Expr
*) isl_id_get_user(id
);
2013 expr
->args
[pos
] = extract_expr(nested
);
2015 for (j
= 0; j
< pos
; ++j
)
2016 if (pet_expr_is_equal(expr
->args
[j
], expr
->args
[pos
]))
2020 pet_expr_free(expr
->args
[pos
]);
2021 param2pos
[i
] = n_in
+ j
;
2024 param2pos
[i
] = n_in
+ pos
++;
2030 dim
= isl_map_get_space(expr
->acc
.access
);
2031 dim
= isl_space_domain(dim
);
2032 dim
= isl_space_from_domain(dim
);
2033 dim
= isl_space_add_dims(dim
, isl_dim_out
, n
);
2034 map
= isl_map_universe(dim
);
2035 map
= isl_map_domain_map(map
);
2036 map
= isl_map_reverse(map
);
2037 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
2039 for (int i
= nparam
- 1; i
>= 0; --i
) {
2040 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
2042 if (!(id
&& isl_id_get_user(id
) && !isl_id_get_name(id
))) {
2047 expr
->acc
.access
= isl_map_equate(expr
->acc
.access
,
2048 isl_dim_param
, i
, isl_dim_in
,
2050 expr
->acc
.access
= isl_map_project_out(expr
->acc
.access
,
2051 isl_dim_param
, i
, 1);
2058 pet_expr_free(expr
);
2062 /* Convert a top-level pet_expr to a pet_scop with one statement.
2063 * This mainly involves resolving nested expression parameters
2064 * and setting the name of the iteration space.
2066 struct pet_scop
*PetScan::extract(Stmt
*stmt
, struct pet_expr
*expr
)
2068 struct pet_stmt
*ps
;
2069 SourceLocation loc
= stmt
->getLocStart();
2070 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
2072 expr
= resolve_nested(expr
);
2073 ps
= pet_stmt_from_pet_expr(ctx
, line
, n_stmt
++, expr
);
2074 return pet_scop_from_pet_stmt(ctx
, ps
);
2077 /* Check whether "expr" is an affine expression.
2078 * We turn on autodetection so that we won't generate any warnings
2079 * and turn off nesting, so that we won't accept any non-affine constructs.
2081 bool PetScan::is_affine(Expr
*expr
)
2084 int save_autodetect
= autodetect
;
2085 bool save_nesting
= nesting_enabled
;
2088 nesting_enabled
= false;
2090 pwaff
= extract_affine(expr
);
2091 isl_pw_aff_free(pwaff
);
2093 autodetect
= save_autodetect
;
2094 nesting_enabled
= save_nesting
;
2096 return pwaff
!= NULL
;
2099 /* Check whether "expr" is an affine constraint.
2100 * We turn on autodetection so that we won't generate any warnings
2101 * and turn off nesting, so that we won't accept any non-affine constructs.
2103 bool PetScan::is_affine_condition(Expr
*expr
)
2106 int save_autodetect
= autodetect
;
2107 bool save_nesting
= nesting_enabled
;
2110 nesting_enabled
= false;
2112 set
= extract_condition(expr
);
2115 autodetect
= save_autodetect
;
2116 nesting_enabled
= save_nesting
;
2121 /* If the top-level expression of "stmt" is an assignment, then
2122 * return that assignment as a BinaryOperator.
2123 * Otherwise return NULL.
2125 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
2127 BinaryOperator
*ass
;
2131 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
2134 ass
= cast
<BinaryOperator
>(stmt
);
2135 if(ass
->getOpcode() != BO_Assign
)
2141 /* Check if the given if statement is a conditional assignement
2142 * with a non-affine condition. If so, construct a pet_scop
2143 * corresponding to this conditional assignment. Otherwise return NULL.
2145 * In particular we check if "stmt" is of the form
2152 * where a is some array or scalar access.
2153 * The constructed pet_scop then corresponds to the expression
2155 * a = condition ? f(...) : g(...)
2157 * All access relations in f(...) are intersected with condition
2158 * while all access relation in g(...) are intersected with the complement.
2160 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
)
2162 BinaryOperator
*ass_then
, *ass_else
;
2163 isl_map
*write_then
, *write_else
;
2164 isl_set
*cond
, *comp
;
2165 isl_map
*map
, *map_true
, *map_false
;
2167 struct pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
2168 bool save_nesting
= nesting_enabled
;
2170 ass_then
= top_assignment_or_null(stmt
->getThen());
2171 ass_else
= top_assignment_or_null(stmt
->getElse());
2173 if (!ass_then
|| !ass_else
)
2176 if (is_affine_condition(stmt
->getCond()))
2179 write_then
= extract_access(ass_then
->getLHS());
2180 write_else
= extract_access(ass_else
->getLHS());
2182 equal
= isl_map_is_equal(write_then
, write_else
);
2183 isl_map_free(write_else
);
2184 if (equal
< 0 || !equal
) {
2185 isl_map_free(write_then
);
2189 nesting_enabled
= allow_nested
;
2190 cond
= extract_condition(stmt
->getCond());
2191 nesting_enabled
= save_nesting
;
2192 comp
= isl_set_complement(isl_set_copy(cond
));
2193 map_true
= isl_map_from_domain(isl_set_copy(cond
));
2194 map_true
= isl_map_add_dims(map_true
, isl_dim_out
, 1);
2195 map_true
= isl_map_fix_si(map_true
, isl_dim_out
, 0, 1);
2196 map_false
= isl_map_from_domain(isl_set_copy(comp
));
2197 map_false
= isl_map_add_dims(map_false
, isl_dim_out
, 1);
2198 map_false
= isl_map_fix_si(map_false
, isl_dim_out
, 0, 0);
2199 map
= isl_map_union_disjoint(map_true
, map_false
);
2201 pe_cond
= pet_expr_from_access(map
);
2203 pe_then
= extract_expr(ass_then
->getRHS());
2204 pe_then
= pet_expr_restrict(pe_then
, cond
);
2205 pe_else
= extract_expr(ass_else
->getRHS());
2206 pe_else
= pet_expr_restrict(pe_else
, comp
);
2208 pe
= pet_expr_new_ternary(ctx
, pe_cond
, pe_then
, pe_else
);
2209 pe_write
= pet_expr_from_access(write_then
);
2211 pe_write
->acc
.write
= 1;
2212 pe_write
->acc
.read
= 0;
2214 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, pe_write
, pe
);
2215 return extract(stmt
, pe
);
2218 /* Construct a pet_scop for an if statement.
2220 struct pet_scop
*PetScan::extract(IfStmt
*stmt
)
2223 struct pet_scop
*scop_then
, *scop_else
, *scop
;
2224 assigned_value_cache
cache(assigned_value
);
2226 scop
= extract_conditional_assignment(stmt
);
2230 scop_then
= extract(stmt
->getThen());
2232 if (stmt
->getElse()) {
2233 scop_else
= extract(stmt
->getElse());
2235 if (scop_then
&& !scop_else
) {
2239 if (!scop_then
&& scop_else
) {
2246 cond
= extract_condition(stmt
->getCond());
2247 scop
= pet_scop_restrict(scop_then
, isl_set_copy(cond
));
2249 if (stmt
->getElse()) {
2250 cond
= isl_set_complement(cond
);
2251 scop_else
= pet_scop_restrict(scop_else
, cond
);
2252 scop
= pet_scop_add(ctx
, scop
, scop_else
);
2259 /* Try and construct a pet_scop corresponding to "stmt".
2261 struct pet_scop
*PetScan::extract(Stmt
*stmt
)
2263 if (isa
<Expr
>(stmt
))
2264 return extract(stmt
, extract_expr(cast
<Expr
>(stmt
)));
2266 switch (stmt
->getStmtClass()) {
2267 case Stmt::WhileStmtClass
:
2268 return extract(cast
<WhileStmt
>(stmt
));
2269 case Stmt::ForStmtClass
:
2270 return extract_for(cast
<ForStmt
>(stmt
));
2271 case Stmt::IfStmtClass
:
2272 return extract(cast
<IfStmt
>(stmt
));
2273 case Stmt::CompoundStmtClass
:
2274 return extract(cast
<CompoundStmt
>(stmt
));
2282 /* Try and construct a pet_scop corresponding to (part of)
2283 * a sequence of statements.
2285 struct pet_scop
*PetScan::extract(StmtRange stmt_range
)
2290 bool partial_range
= false;
2292 scop
= pet_scop_empty(ctx
);
2293 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
2295 struct pet_scop
*scop_i
;
2296 scop_i
= extract(child
);
2297 if (scop
&& partial
) {
2298 pet_scop_free(scop_i
);
2301 scop_i
= pet_scop_prefix(scop_i
, j
);
2304 scop
= pet_scop_add(ctx
, scop
, scop_i
);
2306 partial_range
= true;
2307 if (scop
->n_stmt
!= 0 && !scop_i
)
2310 scop
= pet_scop_add(ctx
, scop
, scop_i
);
2316 if (scop
&& partial_range
)
2322 /* Check if the scop marked by the user is exactly this Stmt
2323 * or part of this Stmt.
2324 * If so, return a pet_scop corresponding to the marked region.
2325 * Otherwise, return NULL.
2327 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
2329 SourceManager
&SM
= PP
.getSourceManager();
2330 unsigned start_off
, end_off
;
2332 start_off
= SM
.getFileOffset(stmt
->getLocStart());
2333 end_off
= SM
.getFileOffset(stmt
->getLocEnd());
2335 if (start_off
> loc
.end
)
2337 if (end_off
< loc
.start
)
2339 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
2340 return extract(stmt
);
2344 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
2345 Stmt
*child
= *start
;
2348 start_off
= SM
.getFileOffset(child
->getLocStart());
2349 end_off
= SM
.getFileOffset(child
->getLocEnd());
2350 if (start_off
< loc
.start
&& end_off
> loc
.end
)
2352 if (start_off
>= loc
.start
)
2357 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
2359 start_off
= SM
.getFileOffset(child
->getLocStart());
2360 if (start_off
>= loc
.end
)
2364 return extract(StmtRange(start
, end
));
2367 /* Set the size of index "pos" of "array" to "size".
2368 * In particular, add a constraint of the form
2372 * to array->extent and a constraint of the form
2376 * to array->context.
2378 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
2379 __isl_take isl_pw_aff
*size
)
2389 valid
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
));
2390 array
->context
= isl_set_intersect(array
->context
, valid
);
2392 dim
= isl_set_get_space(array
->extent
);
2393 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2394 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
2395 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
2396 index
= isl_pw_aff_alloc(univ
, aff
);
2398 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
2399 isl_set_dim(array
->extent
, isl_dim_set
));
2400 id
= isl_set_get_tuple_id(array
->extent
);
2401 size
= isl_pw_aff_set_tuple_id(size
, id
);
2402 bound
= isl_pw_aff_lt_set(index
, size
);
2404 array
->extent
= isl_set_intersect(array
->extent
, bound
);
2406 if (!array
->context
|| !array
->extent
)
2411 pet_array_free(array
);
2415 /* Figure out the size of the array at position "pos" and all
2416 * subsequent positions from "type" and update "array" accordingly.
2418 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
2419 const Type
*type
, int pos
)
2421 const ArrayType
*atype
;
2427 if (type
->isPointerType()) {
2428 type
= type
->getPointeeType().getTypePtr();
2429 return set_upper_bounds(array
, type
, pos
+ 1);
2431 if (!type
->isArrayType())
2434 type
= type
->getCanonicalTypeInternal().getTypePtr();
2435 atype
= cast
<ArrayType
>(type
);
2437 if (type
->isConstantArrayType()) {
2438 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
2439 size
= extract_affine(ca
->getSize());
2440 array
= update_size(array
, pos
, size
);
2441 } else if (type
->isVariableArrayType()) {
2442 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
2443 size
= extract_affine(vla
->getSizeExpr());
2444 array
= update_size(array
, pos
, size
);
2447 type
= atype
->getElementType().getTypePtr();
2449 return set_upper_bounds(array
, type
, pos
+ 1);
2452 /* Construct and return a pet_array corresponding to the variable "decl".
2453 * In particular, initialize array->extent to
2455 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2457 * and then call set_upper_bounds to set the upper bounds on the indices
2458 * based on the type of the variable.
2460 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
)
2462 struct pet_array
*array
;
2463 QualType qt
= decl
->getType();
2464 const Type
*type
= qt
.getTypePtr();
2465 int depth
= array_depth(type
);
2466 QualType base
= base_type(qt
);
2471 array
= isl_calloc_type(ctx
, struct pet_array
);
2475 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
2476 dim
= isl_space_set_alloc(ctx
, 0, depth
);
2477 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
2479 array
->extent
= isl_set_nat_universe(dim
);
2481 dim
= isl_space_params_alloc(ctx
, 0);
2482 array
->context
= isl_set_universe(dim
);
2484 array
= set_upper_bounds(array
, type
, 0);
2488 name
= base
.getAsString();
2489 array
->element_type
= strdup(name
.c_str());
2494 /* Construct a list of pet_arrays, one for each array (or scalar)
2495 * accessed inside "scop" add this list to "scop" and return the result.
2497 * The context of "scop" is updated with the intesection of
2498 * the contexts of all arrays, i.e., constraints on the parameters
2499 * that ensure that the arrays have a valid (non-negative) size.
2501 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
)
2504 set
<ValueDecl
*> arrays
;
2505 set
<ValueDecl
*>::iterator it
;
2510 pet_scop_collect_arrays(scop
, arrays
);
2512 scop
->n_array
= arrays
.size();
2513 if (scop
->n_array
== 0)
2516 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*, scop
->n_array
);
2520 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
2521 struct pet_array
*array
;
2522 scop
->arrays
[i
] = array
= extract_array(ctx
, *it
);
2523 if (!scop
->arrays
[i
])
2525 scop
->context
= isl_set_intersect(scop
->context
,
2526 isl_set_copy(array
->context
));
2533 pet_scop_free(scop
);
2537 /* Construct a pet_scop from the given function.
2539 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
2544 stmt
= fd
->getBody();
2547 scop
= extract(stmt
);
2550 scop
= pet_scop_detect_parameter_accesses(scop
);
2551 scop
= scan_arrays(scop
);