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
;
56 /* Check if the element type corresponding to the given array type
57 * has a const qualifier.
59 static bool const_base(QualType qt
)
61 const Type
*type
= qt
.getTypePtr();
63 if (type
->isPointerType())
64 return const_base(type
->getPointeeType());
65 if (type
->isArrayType()) {
66 const ArrayType
*atype
;
67 type
= type
->getCanonicalTypeInternal().getTypePtr();
68 atype
= cast
<ArrayType
>(type
);
69 return const_base(atype
->getElementType());
72 return qt
.isConstQualified();
75 /* Look for any assignments to scalar variables in part of the parse
76 * tree and set assigned_value to NULL for each of them.
77 * Also reset assigned_value if the address of a scalar variable
78 * is being taken. As an exception, if the address is passed to a function
79 * that is declared to receive a const pointer, then assigned_value is
82 * This ensures that we won't use any previously stored value
83 * in the current subtree and its parents.
85 struct clear_assignments
: RecursiveASTVisitor
<clear_assignments
> {
86 map
<ValueDecl
*, Expr
*> &assigned_value
;
87 set
<UnaryOperator
*> skip
;
89 clear_assignments(map
<ValueDecl
*, Expr
*> &assigned_value
) :
90 assigned_value(assigned_value
) {}
92 /* Check for "address of" operators whose value is passed
93 * to a const pointer argument and add them to "skip", so that
94 * we can skip them in VisitUnaryOperator.
96 bool VisitCallExpr(CallExpr
*expr
) {
98 fd
= expr
->getDirectCallee();
101 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
102 Expr
*arg
= expr
->getArg(i
);
104 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
105 ImplicitCastExpr
*ice
;
106 ice
= cast
<ImplicitCastExpr
>(arg
);
107 arg
= ice
->getSubExpr();
109 if (arg
->getStmtClass() != Stmt::UnaryOperatorClass
)
111 op
= cast
<UnaryOperator
>(arg
);
112 if (op
->getOpcode() != UO_AddrOf
)
114 if (const_base(fd
->getParamDecl(i
)->getType()))
120 bool VisitUnaryOperator(UnaryOperator
*expr
) {
125 if (expr
->getOpcode() != UO_AddrOf
)
127 if (skip
.find(expr
) != skip
.end())
130 arg
= expr
->getSubExpr();
131 if (arg
->getStmtClass() != Stmt::DeclRefExprClass
)
133 ref
= cast
<DeclRefExpr
>(arg
);
134 decl
= ref
->getDecl();
135 assigned_value
[decl
] = NULL
;
139 bool VisitBinaryOperator(BinaryOperator
*expr
) {
144 if (!expr
->isAssignmentOp())
146 lhs
= expr
->getLHS();
147 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
)
149 ref
= cast
<DeclRefExpr
>(lhs
);
150 decl
= ref
->getDecl();
151 assigned_value
[decl
] = NULL
;
156 /* Keep a copy of the currently assigned values.
158 * Any variable that is assigned a value inside the current scope
159 * is removed again when we leave the scope (either because it wasn't
160 * stored in the cache or because it has a different value in the cache).
162 struct assigned_value_cache
{
163 map
<ValueDecl
*, Expr
*> &assigned_value
;
164 map
<ValueDecl
*, Expr
*> cache
;
166 assigned_value_cache(map
<ValueDecl
*, Expr
*> &assigned_value
) :
167 assigned_value(assigned_value
), cache(assigned_value
) {}
168 ~assigned_value_cache() {
169 map
<ValueDecl
*, Expr
*>::iterator it
= cache
.begin();
170 for (it
= assigned_value
.begin(); it
!= assigned_value
.end();
173 (cache
.find(it
->first
) != cache
.end() &&
174 cache
[it
->first
] != it
->second
))
175 cache
[it
->first
] = NULL
;
177 assigned_value
= cache
;
181 /* Called if we found something we (currently) cannot handle.
182 * We'll provide more informative warnings later.
184 * We only actually complain if autodetect is false.
186 void PetScan::unsupported(Stmt
*stmt
)
191 SourceLocation loc
= stmt
->getLocStart();
192 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
193 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
195 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
198 /* Extract an integer from "expr" and store it in "v".
200 int PetScan::extract_int(IntegerLiteral
*expr
, isl_int
*v
)
202 const Type
*type
= expr
->getType().getTypePtr();
203 int is_signed
= type
->hasSignedIntegerRepresentation();
206 int64_t i
= expr
->getValue().getSExtValue();
207 isl_int_set_si(*v
, i
);
209 uint64_t i
= expr
->getValue().getZExtValue();
210 isl_int_set_ui(*v
, i
);
216 /* Extract an affine expression from the IntegerLiteral "expr".
218 __isl_give isl_pw_aff
*PetScan::extract_affine(IntegerLiteral
*expr
)
220 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
221 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
222 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
223 isl_set
*dom
= isl_set_universe(dim
);
227 extract_int(expr
, &v
);
228 aff
= isl_aff_add_constant(aff
, v
);
231 return isl_pw_aff_alloc(dom
, aff
);
234 /* Extract an affine expression from the APInt "val".
236 __isl_give isl_pw_aff
*PetScan::extract_affine(const llvm::APInt
&val
)
238 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
239 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
240 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
241 isl_set
*dom
= isl_set_universe(dim
);
245 isl_int_set_ui(v
, val
.getZExtValue());
246 aff
= isl_aff_add_constant(aff
, v
);
249 return isl_pw_aff_alloc(dom
, aff
);
252 __isl_give isl_pw_aff
*PetScan::extract_affine(ImplicitCastExpr
*expr
)
254 return extract_affine(expr
->getSubExpr());
257 /* Extract an affine expression from the DeclRefExpr "expr".
259 * If the variable has been assigned a value, then we check whether
260 * we know what expression was assigned and whether this expression
261 * is affine. If so, we convert the expression to an isl_pw_aff
262 * and to an extra parameter otherwise (provided nesting_enabled is set).
264 * Otherwise, we simply return an expression that is equal
265 * to a parameter corresponding to the referenced variable.
267 __isl_give isl_pw_aff
*PetScan::extract_affine(DeclRefExpr
*expr
)
269 ValueDecl
*decl
= expr
->getDecl();
270 const Type
*type
= decl
->getType().getTypePtr();
276 if (!type
->isIntegerType()) {
281 if (assigned_value
.find(decl
) != assigned_value
.end()) {
282 if (assigned_value
[decl
] && is_affine(assigned_value
[decl
]))
283 return extract_affine(assigned_value
[decl
]);
285 return non_affine(expr
);
288 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
289 dim
= isl_space_params_alloc(ctx
, 1);
291 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
293 dom
= isl_set_universe(isl_space_copy(dim
));
294 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
295 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
297 return isl_pw_aff_alloc(dom
, aff
);
300 /* Extract an affine expression from an integer division operation.
301 * In particular, if "expr" is lhs/rhs, then return
303 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
305 * The second argument (rhs) is required to be a (positive) integer constant.
307 __isl_give isl_pw_aff
*PetScan::extract_affine_div(BinaryOperator
*expr
)
310 isl_pw_aff
*lhs
, *lhs_f
, *lhs_c
;
315 rhs_expr
= expr
->getRHS();
316 if (rhs_expr
->getStmtClass() != Stmt::IntegerLiteralClass
) {
321 lhs
= extract_affine(expr
->getLHS());
322 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs
));
325 extract_int(cast
<IntegerLiteral
>(rhs_expr
), &v
);
326 lhs
= isl_pw_aff_scale_down(lhs
, v
);
329 lhs_f
= isl_pw_aff_floor(isl_pw_aff_copy(lhs
));
330 lhs_c
= isl_pw_aff_ceil(lhs
);
331 res
= isl_pw_aff_cond(cond
, lhs_f
, lhs_c
);
336 /* Extract an affine expression from a modulo operation.
337 * In particular, if "expr" is lhs/rhs, then return
339 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
341 * The second argument (rhs) is required to be a (positive) integer constant.
343 __isl_give isl_pw_aff
*PetScan::extract_affine_mod(BinaryOperator
*expr
)
346 isl_pw_aff
*lhs
, *lhs_f
, *lhs_c
;
351 rhs_expr
= expr
->getRHS();
352 if (rhs_expr
->getStmtClass() != Stmt::IntegerLiteralClass
) {
357 lhs
= extract_affine(expr
->getLHS());
358 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(lhs
));
361 extract_int(cast
<IntegerLiteral
>(rhs_expr
), &v
);
362 res
= isl_pw_aff_scale_down(isl_pw_aff_copy(lhs
), v
);
364 lhs_f
= isl_pw_aff_floor(isl_pw_aff_copy(res
));
365 lhs_c
= isl_pw_aff_ceil(res
);
366 res
= isl_pw_aff_cond(cond
, lhs_f
, lhs_c
);
368 res
= isl_pw_aff_scale(res
, v
);
371 res
= isl_pw_aff_sub(lhs
, res
);
376 /* Extract an affine expression from a multiplication operation.
377 * This is only allowed if at least one of the two arguments
378 * is a (piecewise) constant.
380 __isl_give isl_pw_aff
*PetScan::extract_affine_mul(BinaryOperator
*expr
)
385 lhs
= extract_affine(expr
->getLHS());
386 rhs
= extract_affine(expr
->getRHS());
388 if (!isl_pw_aff_is_cst(lhs
) && !isl_pw_aff_is_cst(rhs
)) {
389 isl_pw_aff_free(lhs
);
390 isl_pw_aff_free(rhs
);
395 return isl_pw_aff_mul(lhs
, rhs
);
398 /* Extract an affine expression from an addition or subtraction operation.
400 __isl_give isl_pw_aff
*PetScan::extract_affine_add(BinaryOperator
*expr
)
405 lhs
= extract_affine(expr
->getLHS());
406 rhs
= extract_affine(expr
->getRHS());
408 switch (expr
->getOpcode()) {
410 return isl_pw_aff_add(lhs
, rhs
);
412 return isl_pw_aff_sub(lhs
, rhs
);
414 isl_pw_aff_free(lhs
);
415 isl_pw_aff_free(rhs
);
425 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
,
431 isl_int_set_si(mod
, 1);
432 isl_int_mul_2exp(mod
, mod
, width
);
434 pwaff
= isl_pw_aff_mod(pwaff
, mod
);
441 /* Extract an affine expression from some binary operations.
442 * If the result of the expression is unsigned, then we wrap it
443 * based on the size of the type.
445 __isl_give isl_pw_aff
*PetScan::extract_affine(BinaryOperator
*expr
)
449 switch (expr
->getOpcode()) {
452 res
= extract_affine_add(expr
);
455 res
= extract_affine_div(expr
);
458 res
= extract_affine_mod(expr
);
461 res
= extract_affine_mul(expr
);
468 if (expr
->getType()->isUnsignedIntegerType())
469 res
= wrap(res
, ast_context
.getIntWidth(expr
->getType()));
474 /* Extract an affine expression from a negation operation.
476 __isl_give isl_pw_aff
*PetScan::extract_affine(UnaryOperator
*expr
)
478 if (expr
->getOpcode() == UO_Minus
)
479 return isl_pw_aff_neg(extract_affine(expr
->getSubExpr()));
485 __isl_give isl_pw_aff
*PetScan::extract_affine(ParenExpr
*expr
)
487 return extract_affine(expr
->getSubExpr());
490 /* Extract an affine expression from some special function calls.
491 * In particular, we handle "min", "max", "ceild" and "floord".
492 * In case of the latter two, the second argument needs to be
493 * a (positive) integer constant.
495 __isl_give isl_pw_aff
*PetScan::extract_affine(CallExpr
*expr
)
499 isl_pw_aff
*aff1
, *aff2
;
501 fd
= expr
->getDirectCallee();
507 name
= fd
->getDeclName().getAsString();
508 if (!(expr
->getNumArgs() == 2 && name
== "min") &&
509 !(expr
->getNumArgs() == 2 && name
== "max") &&
510 !(expr
->getNumArgs() == 2 && name
== "floord") &&
511 !(expr
->getNumArgs() == 2 && name
== "ceild")) {
516 if (name
== "min" || name
== "max") {
517 aff1
= extract_affine(expr
->getArg(0));
518 aff2
= extract_affine(expr
->getArg(1));
521 aff1
= isl_pw_aff_min(aff1
, aff2
);
523 aff1
= isl_pw_aff_max(aff1
, aff2
);
524 } else if (name
== "floord" || name
== "ceild") {
526 Expr
*arg2
= expr
->getArg(1);
528 if (arg2
->getStmtClass() != Stmt::IntegerLiteralClass
) {
532 aff1
= extract_affine(expr
->getArg(0));
534 extract_int(cast
<IntegerLiteral
>(arg2
), &v
);
535 aff1
= isl_pw_aff_scale_down(aff1
, v
);
537 if (name
== "floord")
538 aff1
= isl_pw_aff_floor(aff1
);
540 aff1
= isl_pw_aff_ceil(aff1
);
550 /* This method is called when we come across a non-affine expression.
551 * If nesting is allowed, we return a new parameter that corresponds
552 * to the non-affine expression. Otherwise, we simply complain.
554 * The new parameter is resolved in resolve_nested.
556 isl_pw_aff
*PetScan::non_affine(Expr
*expr
)
563 if (!nesting_enabled
) {
568 id
= isl_id_alloc(ctx
, NULL
, expr
);
569 dim
= isl_space_params_alloc(ctx
, 1);
571 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
573 dom
= isl_set_universe(isl_space_copy(dim
));
574 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
575 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
577 return isl_pw_aff_alloc(dom
, aff
);
580 /* Affine expressions are not supposed to contain array accesses,
581 * but if nesting is allowed, we return a parameter corresponding
582 * to the array access.
584 __isl_give isl_pw_aff
*PetScan::extract_affine(ArraySubscriptExpr
*expr
)
586 return non_affine(expr
);
589 /* Extract an affine expression from a conditional operation.
591 __isl_give isl_pw_aff
*PetScan::extract_affine(ConditionalOperator
*expr
)
594 isl_pw_aff
*lhs
, *rhs
;
596 cond
= extract_condition(expr
->getCond());
597 lhs
= extract_affine(expr
->getTrueExpr());
598 rhs
= extract_affine(expr
->getFalseExpr());
600 return isl_pw_aff_cond(cond
, lhs
, rhs
);
603 /* Extract an affine expression, if possible, from "expr".
604 * Otherwise return NULL.
606 __isl_give isl_pw_aff
*PetScan::extract_affine(Expr
*expr
)
608 switch (expr
->getStmtClass()) {
609 case Stmt::ImplicitCastExprClass
:
610 return extract_affine(cast
<ImplicitCastExpr
>(expr
));
611 case Stmt::IntegerLiteralClass
:
612 return extract_affine(cast
<IntegerLiteral
>(expr
));
613 case Stmt::DeclRefExprClass
:
614 return extract_affine(cast
<DeclRefExpr
>(expr
));
615 case Stmt::BinaryOperatorClass
:
616 return extract_affine(cast
<BinaryOperator
>(expr
));
617 case Stmt::UnaryOperatorClass
:
618 return extract_affine(cast
<UnaryOperator
>(expr
));
619 case Stmt::ParenExprClass
:
620 return extract_affine(cast
<ParenExpr
>(expr
));
621 case Stmt::CallExprClass
:
622 return extract_affine(cast
<CallExpr
>(expr
));
623 case Stmt::ArraySubscriptExprClass
:
624 return extract_affine(cast
<ArraySubscriptExpr
>(expr
));
625 case Stmt::ConditionalOperatorClass
:
626 return extract_affine(cast
<ConditionalOperator
>(expr
));
633 __isl_give isl_map
*PetScan::extract_access(ImplicitCastExpr
*expr
)
635 return extract_access(expr
->getSubExpr());
638 /* Return the depth of an array of the given type.
640 static int array_depth(const Type
*type
)
642 if (type
->isPointerType())
643 return 1 + array_depth(type
->getPointeeType().getTypePtr());
644 if (type
->isArrayType()) {
645 const ArrayType
*atype
;
646 type
= type
->getCanonicalTypeInternal().getTypePtr();
647 atype
= cast
<ArrayType
>(type
);
648 return 1 + array_depth(atype
->getElementType().getTypePtr());
653 /* Return the element type of the given array type.
655 static QualType
base_type(QualType qt
)
657 const Type
*type
= qt
.getTypePtr();
659 if (type
->isPointerType())
660 return base_type(type
->getPointeeType());
661 if (type
->isArrayType()) {
662 const ArrayType
*atype
;
663 type
= type
->getCanonicalTypeInternal().getTypePtr();
664 atype
= cast
<ArrayType
>(type
);
665 return base_type(atype
->getElementType());
670 /* Extract an access relation from a reference to a variable.
671 * If the variable has name "A" and its type corresponds to an
672 * array of depth d, then the returned access relation is of the
675 * { [] -> A[i_1,...,i_d] }
677 __isl_give isl_map
*PetScan::extract_access(DeclRefExpr
*expr
)
679 ValueDecl
*decl
= expr
->getDecl();
680 int depth
= array_depth(decl
->getType().getTypePtr());
681 isl_id
*id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
682 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, depth
);
685 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
687 access_rel
= isl_map_universe(dim
);
692 /* Extract an access relation from an integer contant.
693 * If the value of the constant is "v", then the returned access relation
698 __isl_give isl_map
*PetScan::extract_access(IntegerLiteral
*expr
)
700 return isl_map_from_range(isl_set_from_pw_aff(extract_affine(expr
)));
703 /* Try and extract an access relation from the given Expr.
704 * Return NULL if it doesn't work out.
706 __isl_give isl_map
*PetScan::extract_access(Expr
*expr
)
708 switch (expr
->getStmtClass()) {
709 case Stmt::ImplicitCastExprClass
:
710 return extract_access(cast
<ImplicitCastExpr
>(expr
));
711 case Stmt::DeclRefExprClass
:
712 return extract_access(cast
<DeclRefExpr
>(expr
));
713 case Stmt::ArraySubscriptExprClass
:
714 return extract_access(cast
<ArraySubscriptExpr
>(expr
));
721 /* Assign the affine expression "index" to the output dimension "pos" of "map"
722 * and return the result.
724 __isl_give isl_map
*set_index(__isl_take isl_map
*map
, int pos
,
725 __isl_take isl_pw_aff
*index
)
728 int len
= isl_map_dim(map
, isl_dim_out
);
731 index_map
= isl_map_from_range(isl_set_from_pw_aff(index
));
732 index_map
= isl_map_insert_dims(index_map
, isl_dim_out
, 0, pos
);
733 index_map
= isl_map_add_dims(index_map
, isl_dim_out
, len
- pos
- 1);
734 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
735 index_map
= isl_map_set_tuple_id(index_map
, isl_dim_out
, id
);
737 map
= isl_map_intersect(map
, index_map
);
742 /* Extract an access relation from the given array subscript expression.
743 * If nesting is allowed in general, then we turn it on while
744 * examining the index expression.
746 * We first extract an access relation from the base.
747 * This will result in an access relation with a range that corresponds
748 * to the array being accessed and with earlier indices filled in already.
749 * We then extract the current index and fill that in as well.
750 * The position of the current index is based on the type of base.
751 * If base is the actual array variable, then the depth of this type
752 * will be the same as the depth of the array and we will fill in
753 * the first array index.
754 * Otherwise, the depth of the base type will be smaller and we will fill
757 __isl_give isl_map
*PetScan::extract_access(ArraySubscriptExpr
*expr
)
759 Expr
*base
= expr
->getBase();
760 Expr
*idx
= expr
->getIdx();
762 isl_map
*base_access
;
764 int depth
= array_depth(base
->getType().getTypePtr());
766 bool save_nesting
= nesting_enabled
;
768 nesting_enabled
= allow_nested
;
770 base_access
= extract_access(base
);
771 index
= extract_affine(idx
);
773 nesting_enabled
= save_nesting
;
775 pos
= isl_map_dim(base_access
, isl_dim_out
) - depth
;
776 access
= set_index(base_access
, pos
, index
);
781 /* Check if "expr" calls function "minmax" with two arguments and if so
782 * make lhs and rhs refer to these two arguments.
784 static bool is_minmax(Expr
*expr
, const char *minmax
, Expr
*&lhs
, Expr
*&rhs
)
790 if (expr
->getStmtClass() != Stmt::CallExprClass
)
793 call
= cast
<CallExpr
>(expr
);
794 fd
= call
->getDirectCallee();
798 if (call
->getNumArgs() != 2)
801 name
= fd
->getDeclName().getAsString();
805 lhs
= call
->getArg(0);
806 rhs
= call
->getArg(1);
811 /* Check if "expr" is of the form min(lhs, rhs) and if so make
812 * lhs and rhs refer to the two arguments.
814 static bool is_min(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
816 return is_minmax(expr
, "min", lhs
, rhs
);
819 /* Check if "expr" is of the form max(lhs, rhs) and if so make
820 * lhs and rhs refer to the two arguments.
822 static bool is_max(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
824 return is_minmax(expr
, "max", lhs
, rhs
);
827 /* Extract a set of values satisfying the comparison "LHS op RHS"
828 * "comp" is the original statement that "LHS op RHS" is derived from
829 * and is used for diagnostics.
831 * If the comparison is of the form
835 * then the set is constructed as the intersection of the set corresponding
840 * A similar optimization is performed for max(a,b) <= c.
841 * We do this because that will lead to simpler representations of the set.
842 * If isl is ever enhanced to explicitly deal with min and max expressions,
843 * this optimization can be removed.
845 __isl_give isl_set
*PetScan::extract_comparison(BinaryOperatorKind op
,
846 Expr
*LHS
, Expr
*RHS
, Stmt
*comp
)
853 return extract_comparison(BO_LT
, RHS
, LHS
, comp
);
855 return extract_comparison(BO_LE
, RHS
, LHS
, comp
);
857 if (op
== BO_LT
|| op
== BO_LE
) {
859 isl_set
*set1
, *set2
;
860 if (is_min(RHS
, expr1
, expr2
)) {
861 set1
= extract_comparison(op
, LHS
, expr1
, comp
);
862 set2
= extract_comparison(op
, LHS
, expr2
, comp
);
863 return isl_set_intersect(set1
, set2
);
865 if (is_max(LHS
, expr1
, expr2
)) {
866 set1
= extract_comparison(op
, expr1
, RHS
, comp
);
867 set2
= extract_comparison(op
, expr2
, RHS
, comp
);
868 return isl_set_intersect(set1
, set2
);
872 lhs
= extract_affine(LHS
);
873 rhs
= extract_affine(RHS
);
877 cond
= isl_pw_aff_lt_set(lhs
, rhs
);
880 cond
= isl_pw_aff_le_set(lhs
, rhs
);
883 cond
= isl_pw_aff_eq_set(lhs
, rhs
);
886 cond
= isl_pw_aff_ne_set(lhs
, rhs
);
889 isl_pw_aff_free(lhs
);
890 isl_pw_aff_free(rhs
);
895 cond
= isl_set_coalesce(cond
);
900 __isl_give isl_set
*PetScan::extract_comparison(BinaryOperator
*comp
)
902 return extract_comparison(comp
->getOpcode(), comp
->getLHS(),
903 comp
->getRHS(), comp
);
906 /* Extract a set of values satisfying the negation (logical not)
907 * of a subexpression.
909 __isl_give isl_set
*PetScan::extract_boolean(UnaryOperator
*op
)
913 cond
= extract_condition(op
->getSubExpr());
915 return isl_set_complement(cond
);
918 /* Extract a set of values satisfying the union (logical or)
919 * or intersection (logical and) of two subexpressions.
921 __isl_give isl_set
*PetScan::extract_boolean(BinaryOperator
*comp
)
927 lhs
= extract_condition(comp
->getLHS());
928 rhs
= extract_condition(comp
->getRHS());
930 switch (comp
->getOpcode()) {
932 cond
= isl_set_intersect(lhs
, rhs
);
935 cond
= isl_set_union(lhs
, rhs
);
947 __isl_give isl_set
*PetScan::extract_condition(UnaryOperator
*expr
)
949 switch (expr
->getOpcode()) {
951 return extract_boolean(expr
);
958 /* Extract a set of values satisfying the condition "expr != 0".
960 __isl_give isl_set
*PetScan::extract_implicit_condition(Expr
*expr
)
962 return isl_pw_aff_non_zero_set(extract_affine(expr
));
965 /* Extract a set of values satisfying the condition expressed by "expr".
967 * If the expression doesn't look like a condition, we assume it
968 * is an affine expression and return the condition "expr != 0".
970 __isl_give isl_set
*PetScan::extract_condition(Expr
*expr
)
972 BinaryOperator
*comp
;
975 return isl_set_universe(isl_space_params_alloc(ctx
, 0));
977 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
978 return extract_condition(cast
<ParenExpr
>(expr
)->getSubExpr());
980 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
)
981 return extract_condition(cast
<UnaryOperator
>(expr
));
983 if (expr
->getStmtClass() != Stmt::BinaryOperatorClass
)
984 return extract_implicit_condition(expr
);
986 comp
= cast
<BinaryOperator
>(expr
);
987 switch (comp
->getOpcode()) {
994 return extract_comparison(comp
);
997 return extract_boolean(comp
);
999 return extract_implicit_condition(expr
);
1003 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
1007 return pet_op_minus
;
1013 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
1017 return pet_op_add_assign
;
1019 return pet_op_sub_assign
;
1021 return pet_op_mul_assign
;
1023 return pet_op_div_assign
;
1025 return pet_op_assign
;
1047 /* Construct a pet_expr representing a unary operator expression.
1049 struct pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
1051 struct pet_expr
*arg
;
1052 enum pet_op_type op
;
1054 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
1055 if (op
== pet_op_last
) {
1060 arg
= extract_expr(expr
->getSubExpr());
1062 return pet_expr_new_unary(ctx
, op
, arg
);
1065 /* Mark the given access pet_expr as a write.
1066 * If a scalar is being accessed, then mark its value
1067 * as unknown in assigned_value.
1069 void PetScan::mark_write(struct pet_expr
*access
)
1074 access
->acc
.write
= 1;
1075 access
->acc
.read
= 0;
1077 if (isl_map_dim(access
->acc
.access
, isl_dim_out
) != 0)
1080 id
= isl_map_get_tuple_id(access
->acc
.access
, isl_dim_out
);
1081 decl
= (ValueDecl
*) isl_id_get_user(id
);
1082 assigned_value
[decl
] = NULL
;
1086 /* Construct a pet_expr representing a binary operator expression.
1088 * If the top level operator is an assignment and the LHS is an access,
1089 * then we mark that access as a write. If the operator is a compound
1090 * assignment, the access is marked as both a read and a write.
1092 * If "expr" assigns something to a scalar variable, then we keep track
1093 * of the assigned expression in assigned_value so that we can plug
1094 * it in when we later come across the same variable.
1096 struct pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
1098 struct pet_expr
*lhs
, *rhs
;
1099 enum pet_op_type op
;
1101 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
1102 if (op
== pet_op_last
) {
1107 lhs
= extract_expr(expr
->getLHS());
1108 rhs
= extract_expr(expr
->getRHS());
1110 if (expr
->isAssignmentOp() && lhs
&& lhs
->type
== pet_expr_access
) {
1112 if (expr
->isCompoundAssignmentOp())
1116 if (expr
->getOpcode() == BO_Assign
&&
1117 lhs
&& lhs
->type
== pet_expr_access
&&
1118 isl_map_dim(lhs
->acc
.access
, isl_dim_out
) == 0) {
1119 isl_id
*id
= isl_map_get_tuple_id(lhs
->acc
.access
, isl_dim_out
);
1120 ValueDecl
*decl
= (ValueDecl
*) isl_id_get_user(id
);
1121 assigned_value
[decl
] = expr
->getRHS();
1125 return pet_expr_new_binary(ctx
, op
, lhs
, rhs
);
1128 /* Construct a pet_expr representing a conditional operation.
1130 struct pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
1132 struct pet_expr
*cond
, *lhs
, *rhs
;
1134 cond
= extract_expr(expr
->getCond());
1135 lhs
= extract_expr(expr
->getTrueExpr());
1136 rhs
= extract_expr(expr
->getFalseExpr());
1138 return pet_expr_new_ternary(ctx
, cond
, lhs
, rhs
);
1141 struct pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
1143 return extract_expr(expr
->getSubExpr());
1146 /* Construct a pet_expr representing a floating point value.
1148 struct pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
1150 return pet_expr_new_double(ctx
, expr
->getValueAsApproximateDouble());
1153 /* Extract an access relation from "expr" and then convert it into
1156 struct pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
1159 struct pet_expr
*pe
;
1161 switch (expr
->getStmtClass()) {
1162 case Stmt::ArraySubscriptExprClass
:
1163 access
= extract_access(cast
<ArraySubscriptExpr
>(expr
));
1165 case Stmt::DeclRefExprClass
:
1166 access
= extract_access(cast
<DeclRefExpr
>(expr
));
1168 case Stmt::IntegerLiteralClass
:
1169 access
= extract_access(cast
<IntegerLiteral
>(expr
));
1176 pe
= pet_expr_from_access(access
);
1181 struct pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
1183 return extract_expr(expr
->getSubExpr());
1186 /* Construct a pet_expr representing a function call.
1188 * If we are passing along a pointer to an array element
1189 * or an entire row or even higher dimensional slice of an array,
1190 * then the function being called may write into the array.
1192 * We assume here that if the function is declared to take a pointer
1193 * to a const type, then the function will perform a read
1194 * and that otherwise, it will perform a write.
1196 struct pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1198 struct pet_expr
*res
= NULL
;
1202 fd
= expr
->getDirectCallee();
1208 name
= fd
->getDeclName().getAsString();
1209 res
= pet_expr_new_call(ctx
, name
.c_str(), expr
->getNumArgs());
1213 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
1214 Expr
*arg
= expr
->getArg(i
);
1217 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
1218 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(arg
);
1219 arg
= ice
->getSubExpr();
1221 if (arg
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1222 UnaryOperator
*op
= cast
<UnaryOperator
>(arg
);
1223 if (op
->getOpcode() == UO_AddrOf
) {
1225 arg
= op
->getSubExpr();
1228 res
->args
[i
] = PetScan::extract_expr(arg
);
1231 if (arg
->getStmtClass() == Stmt::ArraySubscriptExprClass
&&
1232 array_depth(arg
->getType().getTypePtr()) > 0)
1234 if (is_addr
&& res
->args
[i
]->type
== pet_expr_access
) {
1235 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1236 if (!const_base(parm
->getType()))
1237 mark_write(res
->args
[i
]);
1247 /* Try and onstruct a pet_expr representing "expr".
1249 struct pet_expr
*PetScan::extract_expr(Expr
*expr
)
1251 switch (expr
->getStmtClass()) {
1252 case Stmt::UnaryOperatorClass
:
1253 return extract_expr(cast
<UnaryOperator
>(expr
));
1254 case Stmt::CompoundAssignOperatorClass
:
1255 case Stmt::BinaryOperatorClass
:
1256 return extract_expr(cast
<BinaryOperator
>(expr
));
1257 case Stmt::ImplicitCastExprClass
:
1258 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1259 case Stmt::ArraySubscriptExprClass
:
1260 case Stmt::DeclRefExprClass
:
1261 case Stmt::IntegerLiteralClass
:
1262 return extract_access_expr(expr
);
1263 case Stmt::FloatingLiteralClass
:
1264 return extract_expr(cast
<FloatingLiteral
>(expr
));
1265 case Stmt::ParenExprClass
:
1266 return extract_expr(cast
<ParenExpr
>(expr
));
1267 case Stmt::ConditionalOperatorClass
:
1268 return extract_expr(cast
<ConditionalOperator
>(expr
));
1269 case Stmt::CallExprClass
:
1270 return extract_expr(cast
<CallExpr
>(expr
));
1277 /* Check if the given initialization statement is an assignment.
1278 * If so, return that assignment. Otherwise return NULL.
1280 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1282 BinaryOperator
*ass
;
1284 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1287 ass
= cast
<BinaryOperator
>(init
);
1288 if (ass
->getOpcode() != BO_Assign
)
1294 /* Check if the given initialization statement is a declaration
1295 * of a single variable.
1296 * If so, return that declaration. Otherwise return NULL.
1298 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1302 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1305 decl
= cast
<DeclStmt
>(init
);
1307 if (!decl
->isSingleDecl())
1310 return decl
->getSingleDecl();
1313 /* Given the assignment operator in the initialization of a for loop,
1314 * extract the induction variable, i.e., the (integer)variable being
1317 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1324 lhs
= init
->getLHS();
1325 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1330 ref
= cast
<DeclRefExpr
>(lhs
);
1331 decl
= ref
->getDecl();
1332 type
= decl
->getType().getTypePtr();
1334 if (!type
->isIntegerType()) {
1342 /* Given the initialization statement of a for loop and the single
1343 * declaration in this initialization statement,
1344 * extract the induction variable, i.e., the (integer) variable being
1347 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1351 vd
= cast
<VarDecl
>(decl
);
1353 const QualType type
= vd
->getType();
1354 if (!type
->isIntegerType()) {
1359 if (!vd
->getInit()) {
1367 /* Check that op is of the form iv++ or iv--.
1368 * "inc" is accordingly set to 1 or -1.
1370 bool PetScan::check_unary_increment(UnaryOperator
*op
, clang::ValueDecl
*iv
,
1376 if (!op
->isIncrementDecrementOp()) {
1381 if (op
->isIncrementOp())
1382 isl_int_set_si(inc
, 1);
1384 isl_int_set_si(inc
, -1);
1386 sub
= op
->getSubExpr();
1387 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1392 ref
= cast
<DeclRefExpr
>(sub
);
1393 if (ref
->getDecl() != iv
) {
1401 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1402 * has a single constant expression on a universe domain, then
1403 * put this constant in *user.
1405 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
1408 isl_int
*inc
= (isl_int
*)user
;
1411 if (!isl_set_plain_is_universe(set
) || !isl_aff_is_cst(aff
))
1414 isl_aff_get_constant(aff
, inc
);
1422 /* Check if op is of the form
1426 * with inc a constant and set "inc" accordingly.
1428 * We extract an affine expression from the RHS and the subtract iv.
1429 * The result should be a constant.
1431 bool PetScan::check_binary_increment(BinaryOperator
*op
, clang::ValueDecl
*iv
,
1441 if (op
->getOpcode() != BO_Assign
) {
1447 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1452 ref
= cast
<DeclRefExpr
>(lhs
);
1453 if (ref
->getDecl() != iv
) {
1458 val
= extract_affine(op
->getRHS());
1460 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1462 dim
= isl_space_params_alloc(ctx
, 1);
1463 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1464 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1465 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1467 val
= isl_pw_aff_sub(val
, isl_pw_aff_from_aff(aff
));
1469 if (isl_pw_aff_foreach_piece(val
, &extract_cst
, &inc
) < 0) {
1470 isl_pw_aff_free(val
);
1475 isl_pw_aff_free(val
);
1480 /* Check that op is of the form iv += cst or iv -= cst.
1481 * "inc" is set to cst or -cst accordingly.
1483 bool PetScan::check_compound_increment(CompoundAssignOperator
*op
,
1484 clang::ValueDecl
*iv
, isl_int
&inc
)
1490 BinaryOperatorKind opcode
;
1492 opcode
= op
->getOpcode();
1493 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1497 if (opcode
== BO_SubAssign
)
1501 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1506 ref
= cast
<DeclRefExpr
>(lhs
);
1507 if (ref
->getDecl() != iv
) {
1514 if (rhs
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1515 UnaryOperator
*op
= cast
<UnaryOperator
>(rhs
);
1516 if (op
->getOpcode() != UO_Minus
) {
1523 rhs
= op
->getSubExpr();
1526 if (rhs
->getStmtClass() != Stmt::IntegerLiteralClass
) {
1531 extract_int(cast
<IntegerLiteral
>(rhs
), &inc
);
1533 isl_int_neg(inc
, inc
);
1538 /* Check that the increment of the given for loop increments
1539 * (or decrements) the induction variable "iv".
1540 * "up" is set to true if the induction variable is incremented.
1542 bool PetScan::check_increment(ForStmt
*stmt
, ValueDecl
*iv
, isl_int
&v
)
1544 Stmt
*inc
= stmt
->getInc();
1551 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1552 return check_unary_increment(cast
<UnaryOperator
>(inc
), iv
, v
);
1553 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1554 return check_compound_increment(
1555 cast
<CompoundAssignOperator
>(inc
), iv
, v
);
1556 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1557 return check_binary_increment(cast
<BinaryOperator
>(inc
), iv
, v
);
1563 /* Embed the given iteration domain in an extra outer loop
1564 * with induction variable "var".
1565 * If this variable appeared as a parameter in the constraints,
1566 * it is replaced by the new outermost dimension.
1568 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
1569 __isl_take isl_id
*var
)
1573 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
1574 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
1576 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
1577 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
1584 /* Construct a pet_scop for an infinite loop around the given body.
1586 * We extract a pet_scop for the body and then embed it in a loop with
1595 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
)
1601 struct pet_scop
*scop
;
1603 scop
= extract(body
);
1607 id
= isl_id_alloc(ctx
, "t", NULL
);
1608 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
1609 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1610 dim
= isl_space_from_domain(isl_set_get_space(domain
));
1611 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
1612 sched
= isl_map_universe(dim
);
1613 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
1614 scop
= pet_scop_embed(scop
, domain
, sched
, id
);
1619 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1625 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
)
1627 return extract_infinite_loop(stmt
->getBody());
1630 /* Check if the while loop is of the form
1635 * If so, construct a scop for an infinite loop around body.
1638 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
)
1644 cond
= stmt
->getCond();
1650 set
= extract_condition(cond
);
1651 is_universe
= isl_set_plain_is_universe(set
);
1659 return extract_infinite_loop(stmt
->getBody());
1662 /* Check whether "cond" expresses a simple loop bound
1663 * on the only set dimension.
1664 * In particular, if "up" is set then "cond" should contain only
1665 * upper bounds on the set dimension.
1666 * Otherwise, it should contain only lower bounds.
1668 static bool is_simple_bound(__isl_keep isl_set
*cond
, isl_int inc
)
1670 if (isl_int_is_pos(inc
))
1671 return !isl_set_dim_has_lower_bound(cond
, isl_dim_set
, 0);
1673 return !isl_set_dim_has_upper_bound(cond
, isl_dim_set
, 0);
1676 /* Extend a condition on a given iteration of a loop to one that
1677 * imposes the same condition on all previous iterations.
1678 * "domain" expresses the lower [upper] bound on the iterations
1679 * when up is set [not set].
1681 * In particular, we construct the condition (when up is set)
1683 * forall i' : (domain(i') and i' <= i) => cond(i')
1685 * which is equivalent to
1687 * not exists i' : domain(i') and i' <= i and not cond(i')
1689 * We construct this set by negating cond, applying a map
1691 * { [i'] -> [i] : domain(i') and i' <= i }
1693 * and then negating the result again.
1695 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
1696 __isl_take isl_set
*domain
, isl_int inc
)
1698 isl_map
*previous_to_this
;
1700 if (isl_int_is_pos(inc
))
1701 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
1703 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
1705 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
1707 cond
= isl_set_complement(cond
);
1708 cond
= isl_set_apply(cond
, previous_to_this
);
1709 cond
= isl_set_complement(cond
);
1714 /* Construct a domain of the form
1716 * [id] -> { [] : exists a: id = init + a * inc and a >= 0 }
1718 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
1719 __isl_take isl_pw_aff
*init
, isl_int inc
)
1725 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
1726 dim
= isl_pw_aff_get_domain_space(init
);
1727 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1728 aff
= isl_aff_add_coefficient(aff
, isl_dim_in
, 0, inc
);
1729 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1731 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
1732 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1733 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1734 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1736 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1738 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
1740 return isl_set_project_out(set
, isl_dim_set
, 0, 1);
1743 static unsigned get_type_size(ValueDecl
*decl
)
1745 return decl
->getASTContext().getIntWidth(decl
->getType());
1748 /* Assuming "cond" represents a simple bound on a loop where the loop
1749 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1752 * Under the given assumptions, wrapping is only possible if "cond" allows
1753 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1754 * increasing iterator and 0 in case of a decreasing iterator.
1756 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
, isl_int inc
)
1762 test
= isl_set_copy(cond
);
1764 isl_int_init(limit
);
1765 if (isl_int_is_neg(inc
))
1766 isl_int_set_si(limit
, 0);
1768 isl_int_set_si(limit
, 1);
1769 isl_int_mul_2exp(limit
, limit
, get_type_size(iv
));
1770 isl_int_sub_ui(limit
, limit
, 1);
1773 test
= isl_set_fix(cond
, isl_dim_set
, 0, limit
);
1774 cw
= !isl_set_is_empty(test
);
1777 isl_int_clear(limit
);
1782 /* Given a one-dimensional space, construct the following mapping on this
1785 * { [v] -> [v mod 2^width] }
1787 * where width is the number of bits used to represent the values
1788 * of the unsigned variable "iv".
1790 static __isl_give isl_map
*compute_wrapping(__isl_take isl_space
*dim
,
1798 isl_int_set_si(mod
, 1);
1799 isl_int_mul_2exp(mod
, mod
, get_type_size(iv
));
1801 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1802 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
1803 aff
= isl_aff_mod(aff
, mod
);
1807 return isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
1808 map
= isl_map_reverse(map
);
1811 /* Construct a pet_scop for a for statement.
1812 * The for loop is required to be of the form
1814 * for (i = init; condition; ++i)
1818 * for (i = init; condition; --i)
1820 * The initialization of the for loop should either be an assignment
1821 * to an integer variable, or a declaration of such a variable with
1824 * We extract a pet_scop for the body and then embed it in a loop with
1825 * iteration domain and schedule
1827 * { [i] : i >= init and condition' }
1832 * { [i] : i <= init and condition' }
1835 * Where condition' is equal to condition if the latter is
1836 * a simple upper [lower] bound and a condition that is extended
1837 * to apply to all previous iterations otherwise.
1839 * If the stride of the loop is not 1, then "i >= init" is replaced by
1841 * (exists a: i = init + stride * a and a >= 0)
1843 * If the loop iterator i is unsigned, then wrapping may occur.
1844 * During the computation, we work with a virtual iterator that
1845 * does not wrap. However, the condition in the code applies
1846 * to the wrapped value, so we need to change condition(i)
1847 * into condition([i % 2^width]).
1848 * After computing the virtual domain and schedule, we apply
1849 * the function { [v] -> [v % 2^width] } to the domain and the domain
1850 * of the schedule. In order not to lose any information, we also
1851 * need to intersect the domain of the schedule with the virtual domain
1852 * first, since some iterations in the wrapped domain may be scheduled
1853 * several times, typically an infinite number of times.
1854 * Note that there is no need to perform this final wrapping
1855 * if the loop condition (after wrapping) is simple.
1857 * Wrapping on unsigned iterators can be avoided entirely if
1858 * loop condition is simple, the loop iterator is incremented
1859 * [decremented] by one and the last value before wrapping cannot
1860 * possibly satisfy the loop condition.
1862 * Before extracting a pet_scop from the body we remove all
1863 * assignments in assigned_value to variables that are assigned
1864 * somewhere in the body of the loop.
1866 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
)
1868 BinaryOperator
*ass
;
1878 struct pet_scop
*scop
;
1879 assigned_value_cache
cache(assigned_value
);
1884 isl_map
*wrap
= NULL
;
1886 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
1887 return extract_infinite_for(stmt
);
1889 init
= stmt
->getInit();
1894 if ((ass
= initialization_assignment(init
)) != NULL
) {
1895 iv
= extract_induction_variable(ass
);
1898 lhs
= ass
->getLHS();
1899 rhs
= ass
->getRHS();
1900 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
1901 VarDecl
*var
= extract_induction_variable(init
, decl
);
1905 rhs
= var
->getInit();
1906 lhs
= DeclRefExpr::Create(iv
->getASTContext(),
1907 var
->getQualifierLoc(), iv
, var
->getInnerLocStart(),
1908 var
->getType(), VK_LValue
);
1910 unsupported(stmt
->getInit());
1915 if (!check_increment(stmt
, iv
, inc
)) {
1920 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
1922 assigned_value
.erase(iv
);
1923 clear_assignments
clear(assigned_value
);
1924 clear
.TraverseStmt(stmt
->getBody());
1926 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1928 is_one
= isl_int_is_one(inc
) || isl_int_is_negone(inc
);
1930 domain
= extract_comparison(isl_int_is_pos(inc
) ? BO_GE
: BO_LE
,
1933 isl_pw_aff
*lb
= extract_affine(rhs
);
1934 domain
= strided_domain(isl_id_copy(id
), lb
, inc
);
1937 cond
= extract_condition(stmt
->getCond());
1938 cond
= embed(cond
, isl_id_copy(id
));
1939 domain
= embed(domain
, isl_id_copy(id
));
1940 is_simple
= is_simple_bound(cond
, inc
);
1942 (!is_simple
|| !is_one
|| can_wrap(cond
, iv
, inc
))) {
1943 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
1944 cond
= isl_set_apply(cond
, isl_map_reverse(isl_map_copy(wrap
)));
1945 is_simple
= is_simple
&& is_simple_bound(cond
, inc
);
1948 cond
= valid_for_each_iteration(cond
,
1949 isl_set_copy(domain
), inc
);
1950 domain
= isl_set_intersect(domain
, cond
);
1951 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
1952 dim
= isl_space_from_domain(isl_set_get_space(domain
));
1953 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
1954 sched
= isl_map_universe(dim
);
1955 if (isl_int_is_pos(inc
))
1956 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
1958 sched
= isl_map_oppose(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
1960 if (is_unsigned
&& !is_simple
) {
1961 wrap
= isl_map_set_dim_id(wrap
,
1962 isl_dim_out
, 0, isl_id_copy(id
));
1963 sched
= isl_map_intersect_domain(sched
, isl_set_copy(domain
));
1964 domain
= isl_set_apply(domain
, isl_map_copy(wrap
));
1965 sched
= isl_map_apply_domain(sched
, wrap
);
1969 scop
= extract(stmt
->getBody());
1970 scop
= pet_scop_embed(scop
, domain
, sched
, id
);
1971 assigned_value
[iv
] = NULL
;
1977 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
)
1979 return extract(stmt
->children());
1982 /* Look for parameters in any access relation in "expr" that
1983 * refer to non-affine constructs. In particular, these are
1984 * parameters with no name.
1986 * If there are any such parameters, then the domain of the access
1987 * relation, which is still [] at this point, is replaced by
1988 * [[] -> [t_1,...,t_n]], with n the number of these parameters
1989 * (after identifying identical non-affine constructs).
1990 * The parameters are then equated to the corresponding t dimensions
1991 * and subsequently projected out.
1992 * param2pos maps the position of the parameter to the position
1993 * of the corresponding t dimension.
1995 struct pet_expr
*PetScan::resolve_nested(struct pet_expr
*expr
)
2002 std::map
<int,int> param2pos
;
2007 for (int i
= 0; i
< expr
->n_arg
; ++i
) {
2008 expr
->args
[i
] = resolve_nested(expr
->args
[i
]);
2009 if (!expr
->args
[i
]) {
2010 pet_expr_free(expr
);
2015 if (expr
->type
!= pet_expr_access
)
2018 nparam
= isl_map_dim(expr
->acc
.access
, isl_dim_param
);
2020 for (int i
= 0; i
< nparam
; ++i
) {
2021 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
2023 if (id
&& isl_id_get_user(id
) && !isl_id_get_name(id
))
2032 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n
);
2036 n_in
= isl_map_dim(expr
->acc
.access
, isl_dim_in
);
2037 for (int i
= 0, pos
= 0; i
< nparam
; ++i
) {
2039 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
2043 if (!(id
&& isl_id_get_user(id
) && !isl_id_get_name(id
))) {
2048 nested
= (Expr
*) isl_id_get_user(id
);
2049 expr
->args
[pos
] = extract_expr(nested
);
2051 for (j
= 0; j
< pos
; ++j
)
2052 if (pet_expr_is_equal(expr
->args
[j
], expr
->args
[pos
]))
2056 pet_expr_free(expr
->args
[pos
]);
2057 param2pos
[i
] = n_in
+ j
;
2060 param2pos
[i
] = n_in
+ pos
++;
2066 dim
= isl_map_get_space(expr
->acc
.access
);
2067 dim
= isl_space_domain(dim
);
2068 dim
= isl_space_from_domain(dim
);
2069 dim
= isl_space_add_dims(dim
, isl_dim_out
, n
);
2070 map
= isl_map_universe(dim
);
2071 map
= isl_map_domain_map(map
);
2072 map
= isl_map_reverse(map
);
2073 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
2075 for (int i
= nparam
- 1; i
>= 0; --i
) {
2076 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
2078 if (!(id
&& isl_id_get_user(id
) && !isl_id_get_name(id
))) {
2083 expr
->acc
.access
= isl_map_equate(expr
->acc
.access
,
2084 isl_dim_param
, i
, isl_dim_in
,
2086 expr
->acc
.access
= isl_map_project_out(expr
->acc
.access
,
2087 isl_dim_param
, i
, 1);
2094 pet_expr_free(expr
);
2098 /* Convert a top-level pet_expr to a pet_scop with one statement.
2099 * This mainly involves resolving nested expression parameters
2100 * and setting the name of the iteration space.
2101 * The name is given by "label" if it is non-NULL. Otherwise,
2102 * it is of the form S_<n_stmt>.
2104 struct pet_scop
*PetScan::extract(Stmt
*stmt
, struct pet_expr
*expr
,
2105 __isl_take isl_id
*label
)
2107 struct pet_stmt
*ps
;
2108 SourceLocation loc
= stmt
->getLocStart();
2109 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
2111 expr
= resolve_nested(expr
);
2112 ps
= pet_stmt_from_pet_expr(ctx
, line
, label
, n_stmt
++, expr
);
2113 return pet_scop_from_pet_stmt(ctx
, ps
);
2116 /* Check whether "expr" is an affine expression.
2117 * We turn on autodetection so that we won't generate any warnings
2118 * and turn off nesting, so that we won't accept any non-affine constructs.
2120 bool PetScan::is_affine(Expr
*expr
)
2123 int save_autodetect
= autodetect
;
2124 bool save_nesting
= nesting_enabled
;
2127 nesting_enabled
= false;
2129 pwaff
= extract_affine(expr
);
2130 isl_pw_aff_free(pwaff
);
2132 autodetect
= save_autodetect
;
2133 nesting_enabled
= save_nesting
;
2135 return pwaff
!= NULL
;
2138 /* Check whether "expr" is an affine constraint.
2139 * We turn on autodetection so that we won't generate any warnings
2140 * and turn off nesting, so that we won't accept any non-affine constructs.
2142 bool PetScan::is_affine_condition(Expr
*expr
)
2145 int save_autodetect
= autodetect
;
2146 bool save_nesting
= nesting_enabled
;
2149 nesting_enabled
= false;
2151 set
= extract_condition(expr
);
2154 autodetect
= save_autodetect
;
2155 nesting_enabled
= save_nesting
;
2160 /* If the top-level expression of "stmt" is an assignment, then
2161 * return that assignment as a BinaryOperator.
2162 * Otherwise return NULL.
2164 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
2166 BinaryOperator
*ass
;
2170 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
2173 ass
= cast
<BinaryOperator
>(stmt
);
2174 if(ass
->getOpcode() != BO_Assign
)
2180 /* Check if the given if statement is a conditional assignement
2181 * with a non-affine condition. If so, construct a pet_scop
2182 * corresponding to this conditional assignment. Otherwise return NULL.
2184 * In particular we check if "stmt" is of the form
2191 * where a is some array or scalar access.
2192 * The constructed pet_scop then corresponds to the expression
2194 * a = condition ? f(...) : g(...)
2196 * All access relations in f(...) are intersected with condition
2197 * while all access relation in g(...) are intersected with the complement.
2199 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
)
2201 BinaryOperator
*ass_then
, *ass_else
;
2202 isl_map
*write_then
, *write_else
;
2203 isl_set
*cond
, *comp
;
2204 isl_map
*map
, *map_true
, *map_false
;
2206 struct pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
2207 bool save_nesting
= nesting_enabled
;
2209 ass_then
= top_assignment_or_null(stmt
->getThen());
2210 ass_else
= top_assignment_or_null(stmt
->getElse());
2212 if (!ass_then
|| !ass_else
)
2215 if (is_affine_condition(stmt
->getCond()))
2218 write_then
= extract_access(ass_then
->getLHS());
2219 write_else
= extract_access(ass_else
->getLHS());
2221 equal
= isl_map_is_equal(write_then
, write_else
);
2222 isl_map_free(write_else
);
2223 if (equal
< 0 || !equal
) {
2224 isl_map_free(write_then
);
2228 nesting_enabled
= allow_nested
;
2229 cond
= extract_condition(stmt
->getCond());
2230 nesting_enabled
= save_nesting
;
2231 comp
= isl_set_complement(isl_set_copy(cond
));
2232 map_true
= isl_map_from_domain(isl_set_from_params(isl_set_copy(cond
)));
2233 map_true
= isl_map_add_dims(map_true
, isl_dim_out
, 1);
2234 map_true
= isl_map_fix_si(map_true
, isl_dim_out
, 0, 1);
2235 map_false
= isl_map_from_domain(isl_set_from_params(isl_set_copy(comp
)));
2236 map_false
= isl_map_add_dims(map_false
, isl_dim_out
, 1);
2237 map_false
= isl_map_fix_si(map_false
, isl_dim_out
, 0, 0);
2238 map
= isl_map_union_disjoint(map_true
, map_false
);
2240 pe_cond
= pet_expr_from_access(map
);
2242 pe_then
= extract_expr(ass_then
->getRHS());
2243 pe_then
= pet_expr_restrict(pe_then
, cond
);
2244 pe_else
= extract_expr(ass_else
->getRHS());
2245 pe_else
= pet_expr_restrict(pe_else
, comp
);
2247 pe
= pet_expr_new_ternary(ctx
, pe_cond
, pe_then
, pe_else
);
2248 pe_write
= pet_expr_from_access(write_then
);
2250 pe_write
->acc
.write
= 1;
2251 pe_write
->acc
.read
= 0;
2253 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, pe_write
, pe
);
2254 return extract(stmt
, pe
);
2257 /* Create an access to a virtual array representing the result
2259 * Unlike other accessed data, the id of the array is NULL as
2260 * there is no ValueDecl in the program corresponding to the virtual
2262 * The array starts out as a scalar, but grows along with the
2263 * statement writing to the array in pet_scop_embed.
2265 static __isl_give isl_map
*create_test_access(isl_ctx
*ctx
, int test_nr
)
2267 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, 0);
2271 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
2272 id
= isl_id_alloc(ctx
, name
, NULL
);
2273 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
2274 return isl_map_universe(dim
);
2277 /* Create a pet_scop with a single statement evaluating "cond"
2278 * and writing the result to a virtual scalar, as expressed by
2281 struct pet_scop
*PetScan::extract_non_affine_condition(Expr
*cond
,
2282 __isl_take isl_map
*access
)
2284 struct pet_expr
*expr
, *write
;
2285 struct pet_stmt
*ps
;
2286 SourceLocation loc
= cond
->getLocStart();
2287 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
2289 write
= pet_expr_from_access(access
);
2291 write
->acc
.write
= 1;
2292 write
->acc
.read
= 0;
2294 expr
= extract_expr(cond
);
2295 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, write
, expr
);
2296 ps
= pet_stmt_from_pet_expr(ctx
, line
, NULL
, n_stmt
++, expr
);
2297 return pet_scop_from_pet_stmt(ctx
, ps
);
2300 /* Add an array with the given extend ("access") to the list
2301 * of arrays in "scop" and return the extended pet_scop.
2302 * The array is marked as attaining values 0 and 1 only.
2304 static struct pet_scop
*scop_add_array(struct pet_scop
*scop
,
2305 __isl_keep isl_map
*access
)
2307 isl_ctx
*ctx
= isl_map_get_ctx(access
);
2309 struct pet_array
**arrays
;
2310 struct pet_array
*array
;
2317 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
2321 scop
->arrays
= arrays
;
2323 array
= isl_calloc_type(ctx
, struct pet_array
);
2327 array
->extent
= isl_map_range(isl_map_copy(access
));
2328 dim
= isl_space_params_alloc(ctx
, 0);
2329 array
->context
= isl_set_universe(dim
);
2330 dim
= isl_space_set_alloc(ctx
, 0, 1);
2331 array
->value_bounds
= isl_set_universe(dim
);
2332 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
2334 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
2336 array
->element_type
= strdup("int");
2338 scop
->arrays
[scop
->n_array
] = array
;
2341 if (!array
->extent
|| !array
->context
)
2346 pet_scop_free(scop
);
2350 /* Construct a pet_scop for an if statement.
2352 * If the condition fits the pattern of a conditional assignment,
2353 * then it is handled by extract_conditional_assignment.
2354 * Otherwise, we do the following.
2356 * If the condition is affine, then the condition is added
2357 * to the iteration domains of the then branch, while the
2358 * opposite of the condition in added to the iteration domains
2359 * of the else branch, if any.
2361 * If the condition is not-affine, then we create a separate
2362 * statement that write the result of the condition to a virtual scalar.
2363 * A constraint requiring the value of this virtual scalar to be one
2364 * is added to the iteration domains of the then branch.
2365 * Similarly, a constraint requiring the value of this virtual scalar
2366 * to be zero is added to the iteration domains of the else branch, if any.
2367 * We adjust the schedules to ensure that the virtual scalar is written
2368 * before it is read.
2370 struct pet_scop
*PetScan::extract(IfStmt
*stmt
)
2372 struct pet_scop
*scop_then
, *scop_else
, *scop
;
2373 assigned_value_cache
cache(assigned_value
);
2374 isl_map
*test_access
= NULL
;
2376 scop
= extract_conditional_assignment(stmt
);
2380 if (allow_nested
&& !is_affine_condition(stmt
->getCond())) {
2381 test_access
= create_test_access(ctx
, n_test
++);
2382 scop
= extract_non_affine_condition(stmt
->getCond(),
2383 isl_map_copy(test_access
));
2384 scop
= scop_add_array(scop
, test_access
);
2386 isl_map_free(test_access
);
2391 scop_then
= extract(stmt
->getThen());
2393 if (stmt
->getElse()) {
2394 scop_else
= extract(stmt
->getElse());
2396 if (scop_then
&& !scop_else
) {
2398 pet_scop_free(scop
);
2399 isl_map_free(test_access
);
2402 if (!scop_then
&& scop_else
) {
2404 pet_scop_free(scop
);
2405 isl_map_free(test_access
);
2413 cond
= extract_condition(stmt
->getCond());
2414 scop
= pet_scop_restrict(scop_then
, isl_set_copy(cond
));
2416 if (stmt
->getElse()) {
2417 cond
= isl_set_complement(cond
);
2418 scop_else
= pet_scop_restrict(scop_else
, cond
);
2419 scop
= pet_scop_add(ctx
, scop
, scop_else
);
2423 scop
= pet_scop_prefix(scop
, 0);
2424 scop_then
= pet_scop_prefix(scop_then
, 1);
2425 scop_then
= pet_scop_filter(scop_then
,
2426 isl_map_copy(test_access
), 1);
2427 scop
= pet_scop_add(ctx
, scop
, scop_then
);
2428 if (stmt
->getElse()) {
2429 scop_else
= pet_scop_prefix(scop_else
, 1);
2430 scop_else
= pet_scop_filter(scop_else
, test_access
, 0);
2431 scop
= pet_scop_add(ctx
, scop
, scop_else
);
2433 isl_map_free(test_access
);
2439 /* Try and construct a pet_scop for a label statement.
2440 * We currently only allow labels on expression statements.
2442 struct pet_scop
*PetScan::extract(LabelStmt
*stmt
)
2447 sub
= stmt
->getSubStmt();
2448 if (!isa
<Expr
>(sub
)) {
2453 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
2455 return extract(sub
, extract_expr(cast
<Expr
>(sub
)), label
);
2458 /* Try and construct a pet_scop corresponding to "stmt".
2460 struct pet_scop
*PetScan::extract(Stmt
*stmt
)
2462 if (isa
<Expr
>(stmt
))
2463 return extract(stmt
, extract_expr(cast
<Expr
>(stmt
)));
2465 switch (stmt
->getStmtClass()) {
2466 case Stmt::WhileStmtClass
:
2467 return extract(cast
<WhileStmt
>(stmt
));
2468 case Stmt::ForStmtClass
:
2469 return extract_for(cast
<ForStmt
>(stmt
));
2470 case Stmt::IfStmtClass
:
2471 return extract(cast
<IfStmt
>(stmt
));
2472 case Stmt::CompoundStmtClass
:
2473 return extract(cast
<CompoundStmt
>(stmt
));
2474 case Stmt::LabelStmtClass
:
2475 return extract(cast
<LabelStmt
>(stmt
));
2483 /* Try and construct a pet_scop corresponding to (part of)
2484 * a sequence of statements.
2486 struct pet_scop
*PetScan::extract(StmtRange stmt_range
)
2491 bool partial_range
= false;
2493 scop
= pet_scop_empty(ctx
);
2494 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
2496 struct pet_scop
*scop_i
;
2497 scop_i
= extract(child
);
2498 if (scop
&& partial
) {
2499 pet_scop_free(scop_i
);
2502 scop_i
= pet_scop_prefix(scop_i
, j
);
2505 scop
= pet_scop_add(ctx
, scop
, scop_i
);
2507 partial_range
= true;
2508 if (scop
->n_stmt
!= 0 && !scop_i
)
2511 scop
= pet_scop_add(ctx
, scop
, scop_i
);
2517 if (scop
&& partial_range
)
2523 /* Check if the scop marked by the user is exactly this Stmt
2524 * or part of this Stmt.
2525 * If so, return a pet_scop corresponding to the marked region.
2526 * Otherwise, return NULL.
2528 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
2530 SourceManager
&SM
= PP
.getSourceManager();
2531 unsigned start_off
, end_off
;
2533 start_off
= SM
.getFileOffset(stmt
->getLocStart());
2534 end_off
= SM
.getFileOffset(stmt
->getLocEnd());
2536 if (start_off
> loc
.end
)
2538 if (end_off
< loc
.start
)
2540 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
2541 return extract(stmt
);
2545 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
2546 Stmt
*child
= *start
;
2549 start_off
= SM
.getFileOffset(child
->getLocStart());
2550 end_off
= SM
.getFileOffset(child
->getLocEnd());
2551 if (start_off
< loc
.start
&& end_off
> loc
.end
)
2553 if (start_off
>= loc
.start
)
2558 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
2560 start_off
= SM
.getFileOffset(child
->getLocStart());
2561 if (start_off
>= loc
.end
)
2565 return extract(StmtRange(start
, end
));
2568 /* Set the size of index "pos" of "array" to "size".
2569 * In particular, add a constraint of the form
2573 * to array->extent and a constraint of the form
2577 * to array->context.
2579 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
2580 __isl_take isl_pw_aff
*size
)
2590 valid
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
));
2591 array
->context
= isl_set_intersect(array
->context
, valid
);
2593 dim
= isl_set_get_space(array
->extent
);
2594 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2595 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
2596 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
2597 index
= isl_pw_aff_alloc(univ
, aff
);
2599 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
2600 isl_set_dim(array
->extent
, isl_dim_set
));
2601 id
= isl_set_get_tuple_id(array
->extent
);
2602 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
2603 bound
= isl_pw_aff_lt_set(index
, size
);
2605 array
->extent
= isl_set_intersect(array
->extent
, bound
);
2607 if (!array
->context
|| !array
->extent
)
2612 pet_array_free(array
);
2616 /* Figure out the size of the array at position "pos" and all
2617 * subsequent positions from "type" and update "array" accordingly.
2619 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
2620 const Type
*type
, int pos
)
2622 const ArrayType
*atype
;
2628 if (type
->isPointerType()) {
2629 type
= type
->getPointeeType().getTypePtr();
2630 return set_upper_bounds(array
, type
, pos
+ 1);
2632 if (!type
->isArrayType())
2635 type
= type
->getCanonicalTypeInternal().getTypePtr();
2636 atype
= cast
<ArrayType
>(type
);
2638 if (type
->isConstantArrayType()) {
2639 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
2640 size
= extract_affine(ca
->getSize());
2641 array
= update_size(array
, pos
, size
);
2642 } else if (type
->isVariableArrayType()) {
2643 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
2644 size
= extract_affine(vla
->getSizeExpr());
2645 array
= update_size(array
, pos
, size
);
2648 type
= atype
->getElementType().getTypePtr();
2650 return set_upper_bounds(array
, type
, pos
+ 1);
2653 /* Construct and return a pet_array corresponding to the variable "decl".
2654 * In particular, initialize array->extent to
2656 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2658 * and then call set_upper_bounds to set the upper bounds on the indices
2659 * based on the type of the variable.
2661 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
)
2663 struct pet_array
*array
;
2664 QualType qt
= decl
->getType();
2665 const Type
*type
= qt
.getTypePtr();
2666 int depth
= array_depth(type
);
2667 QualType base
= base_type(qt
);
2672 array
= isl_calloc_type(ctx
, struct pet_array
);
2676 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
2677 dim
= isl_space_set_alloc(ctx
, 0, depth
);
2678 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
2680 array
->extent
= isl_set_nat_universe(dim
);
2682 dim
= isl_space_params_alloc(ctx
, 0);
2683 array
->context
= isl_set_universe(dim
);
2685 array
= set_upper_bounds(array
, type
, 0);
2689 name
= base
.getAsString();
2690 array
->element_type
= strdup(name
.c_str());
2695 /* Construct a list of pet_arrays, one for each array (or scalar)
2696 * accessed inside "scop" add this list to "scop" and return the result.
2698 * The context of "scop" is updated with the intesection of
2699 * the contexts of all arrays, i.e., constraints on the parameters
2700 * that ensure that the arrays have a valid (non-negative) size.
2702 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
)
2705 set
<ValueDecl
*> arrays
;
2706 set
<ValueDecl
*>::iterator it
;
2708 struct pet_array
**scop_arrays
;
2713 pet_scop_collect_arrays(scop
, arrays
);
2714 if (arrays
.size() == 0)
2717 n_array
= scop
->n_array
;
2719 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
2720 n_array
+ arrays
.size());
2723 scop
->arrays
= scop_arrays
;
2725 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
2726 struct pet_array
*array
;
2727 scop
->arrays
[n_array
+ i
] = array
= extract_array(ctx
, *it
);
2728 if (!scop
->arrays
[n_array
+ i
])
2731 scop
->context
= isl_set_intersect(scop
->context
,
2732 isl_set_copy(array
->context
));
2739 pet_scop_free(scop
);
2743 /* Construct a pet_scop from the given function.
2745 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
2750 stmt
= fd
->getBody();
2753 scop
= extract(stmt
);
2756 scop
= pet_scop_detect_parameter_accesses(scop
);
2757 scop
= scan_arrays(scop
);