2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
38 #include <llvm/Support/raw_ostream.h>
39 #include <clang/AST/ASTContext.h>
40 #include <clang/AST/ASTDiagnostic.h>
41 #include <clang/AST/Expr.h>
42 #include <clang/AST/RecursiveASTVisitor.h>
45 #include <isl/space.h>
52 #include "scop_plus.h"
57 using namespace clang
;
59 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
60 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
62 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
63 SourceLocation(), var
, false, var
->getInnerLocStart(),
64 var
->getType(), VK_LValue
);
66 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
67 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
69 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
70 SourceLocation(), var
, var
->getInnerLocStart(), var
->getType(),
74 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
76 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
77 var
, var
->getInnerLocStart(), var
->getType(), VK_LValue
);
81 /* Check if the element type corresponding to the given array type
82 * has a const qualifier.
84 static bool const_base(QualType qt
)
86 const Type
*type
= qt
.getTypePtr();
88 if (type
->isPointerType())
89 return const_base(type
->getPointeeType());
90 if (type
->isArrayType()) {
91 const ArrayType
*atype
;
92 type
= type
->getCanonicalTypeInternal().getTypePtr();
93 atype
= cast
<ArrayType
>(type
);
94 return const_base(atype
->getElementType());
97 return qt
.isConstQualified();
100 /* Mark "decl" as having an unknown value in "assigned_value".
102 * If no (known or unknown) value was assigned to "decl" before,
103 * then it may have been treated as a parameter before and may
104 * therefore appear in a value assigned to another variable.
105 * If so, this assignment needs to be turned into an unknown value too.
107 static void clear_assignment(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
,
110 map
<ValueDecl
*, isl_pw_aff
*>::iterator it
;
112 it
= assigned_value
.find(decl
);
114 assigned_value
[decl
] = NULL
;
116 if (it
== assigned_value
.end())
119 for (it
= assigned_value
.begin(); it
!= assigned_value
.end(); ++it
) {
120 isl_pw_aff
*pa
= it
->second
;
121 int nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
123 for (int i
= 0; i
< nparam
; ++i
) {
126 if (!isl_pw_aff_has_dim_id(pa
, isl_dim_param
, i
))
128 id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
129 if (isl_id_get_user(id
) == decl
)
136 /* Look for any assignments to scalar variables in part of the parse
137 * tree and set assigned_value to NULL for each of them.
138 * Also reset assigned_value if the address of a scalar variable
139 * is being taken. As an exception, if the address is passed to a function
140 * that is declared to receive a const pointer, then assigned_value is
143 * This ensures that we won't use any previously stored value
144 * in the current subtree and its parents.
146 struct clear_assignments
: RecursiveASTVisitor
<clear_assignments
> {
147 map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
;
148 set
<UnaryOperator
*> skip
;
150 clear_assignments(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
) :
151 assigned_value(assigned_value
) {}
153 /* Check for "address of" operators whose value is passed
154 * to a const pointer argument and add them to "skip", so that
155 * we can skip them in VisitUnaryOperator.
157 bool VisitCallExpr(CallExpr
*expr
) {
159 fd
= expr
->getDirectCallee();
162 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
163 Expr
*arg
= expr
->getArg(i
);
165 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
166 ImplicitCastExpr
*ice
;
167 ice
= cast
<ImplicitCastExpr
>(arg
);
168 arg
= ice
->getSubExpr();
170 if (arg
->getStmtClass() != Stmt::UnaryOperatorClass
)
172 op
= cast
<UnaryOperator
>(arg
);
173 if (op
->getOpcode() != UO_AddrOf
)
175 if (const_base(fd
->getParamDecl(i
)->getType()))
181 bool VisitUnaryOperator(UnaryOperator
*expr
) {
186 switch (expr
->getOpcode()) {
196 if (skip
.find(expr
) != skip
.end())
199 arg
= expr
->getSubExpr();
200 if (arg
->getStmtClass() != Stmt::DeclRefExprClass
)
202 ref
= cast
<DeclRefExpr
>(arg
);
203 decl
= ref
->getDecl();
204 clear_assignment(assigned_value
, decl
);
208 bool VisitBinaryOperator(BinaryOperator
*expr
) {
213 if (!expr
->isAssignmentOp())
215 lhs
= expr
->getLHS();
216 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
)
218 ref
= cast
<DeclRefExpr
>(lhs
);
219 decl
= ref
->getDecl();
220 clear_assignment(assigned_value
, decl
);
225 /* Keep a copy of the currently assigned values.
227 * Any variable that is assigned a value inside the current scope
228 * is removed again when we leave the scope (either because it wasn't
229 * stored in the cache or because it has a different value in the cache).
231 struct assigned_value_cache
{
232 map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
;
233 map
<ValueDecl
*, isl_pw_aff
*> cache
;
235 assigned_value_cache(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
) :
236 assigned_value(assigned_value
), cache(assigned_value
) {}
237 ~assigned_value_cache() {
238 map
<ValueDecl
*, isl_pw_aff
*>::iterator it
= cache
.begin();
239 for (it
= assigned_value
.begin(); it
!= assigned_value
.end();
242 (cache
.find(it
->first
) != cache
.end() &&
243 cache
[it
->first
] != it
->second
))
244 cache
[it
->first
] = NULL
;
246 assigned_value
= cache
;
250 /* Insert an expression into the collection of expressions,
251 * provided it is not already in there.
252 * The isl_pw_affs are freed in the destructor.
254 void PetScan::insert_expression(__isl_take isl_pw_aff
*expr
)
256 std::set
<isl_pw_aff
*>::iterator it
;
258 if (expressions
.find(expr
) == expressions
.end())
259 expressions
.insert(expr
);
261 isl_pw_aff_free(expr
);
266 std::set
<isl_pw_aff
*>::iterator it
;
268 for (it
= expressions
.begin(); it
!= expressions
.end(); ++it
)
269 isl_pw_aff_free(*it
);
271 isl_union_map_free(value_bounds
);
274 /* Called if we found something we (currently) cannot handle.
275 * We'll provide more informative warnings later.
277 * We only actually complain if autodetect is false.
279 void PetScan::unsupported(Stmt
*stmt
, const char *msg
)
281 if (options
->autodetect
)
284 SourceLocation loc
= stmt
->getLocStart();
285 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
286 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
287 msg
? msg
: "unsupported");
288 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
291 /* Extract an integer from "expr" and store it in "v".
293 int PetScan::extract_int(IntegerLiteral
*expr
, isl_int
*v
)
295 const Type
*type
= expr
->getType().getTypePtr();
296 int is_signed
= type
->hasSignedIntegerRepresentation();
299 int64_t i
= expr
->getValue().getSExtValue();
300 isl_int_set_si(*v
, i
);
302 uint64_t i
= expr
->getValue().getZExtValue();
303 isl_int_set_ui(*v
, i
);
309 /* Extract an integer from "expr" and store it in "v".
310 * Return -1 if "expr" does not (obviously) represent an integer.
312 int PetScan::extract_int(clang::ParenExpr
*expr
, isl_int
*v
)
314 return extract_int(expr
->getSubExpr(), v
);
317 /* Extract an integer from "expr" and store it in "v".
318 * Return -1 if "expr" does not (obviously) represent an integer.
320 int PetScan::extract_int(clang::Expr
*expr
, isl_int
*v
)
322 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
323 return extract_int(cast
<IntegerLiteral
>(expr
), v
);
324 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
325 return extract_int(cast
<ParenExpr
>(expr
), v
);
331 /* Extract an affine expression from the IntegerLiteral "expr".
333 __isl_give isl_pw_aff
*PetScan::extract_affine(IntegerLiteral
*expr
)
335 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
336 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
337 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
338 isl_set
*dom
= isl_set_universe(dim
);
342 extract_int(expr
, &v
);
343 aff
= isl_aff_add_constant(aff
, v
);
346 return isl_pw_aff_alloc(dom
, aff
);
349 /* Extract an affine expression from the APInt "val".
351 __isl_give isl_pw_aff
*PetScan::extract_affine(const llvm::APInt
&val
)
353 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
354 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
355 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
356 isl_set
*dom
= isl_set_universe(dim
);
360 isl_int_set_ui(v
, val
.getZExtValue());
361 aff
= isl_aff_add_constant(aff
, v
);
364 return isl_pw_aff_alloc(dom
, aff
);
367 __isl_give isl_pw_aff
*PetScan::extract_affine(ImplicitCastExpr
*expr
)
369 return extract_affine(expr
->getSubExpr());
372 static unsigned get_type_size(ValueDecl
*decl
)
374 return decl
->getASTContext().getIntWidth(decl
->getType());
377 /* Bound parameter "pos" of "set" to the possible values of "decl".
379 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
380 unsigned pos
, ValueDecl
*decl
)
387 width
= get_type_size(decl
);
388 if (decl
->getType()->isUnsignedIntegerType()) {
389 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
390 isl_int_set_si(v
, 1);
391 isl_int_mul_2exp(v
, v
, width
);
392 isl_int_sub_ui(v
, v
, 1);
393 set
= isl_set_upper_bound(set
, isl_dim_param
, pos
, v
);
395 isl_int_set_si(v
, 1);
396 isl_int_mul_2exp(v
, v
, width
- 1);
397 isl_int_sub_ui(v
, v
, 1);
398 set
= isl_set_upper_bound(set
, isl_dim_param
, pos
, v
);
400 isl_int_sub_ui(v
, v
, 1);
401 set
= isl_set_lower_bound(set
, isl_dim_param
, pos
, v
);
409 /* Extract an affine expression from the DeclRefExpr "expr".
411 * If the variable has been assigned a value, then we check whether
412 * we know what (affine) value was assigned.
413 * If so, we return this value. Otherwise we convert "expr"
414 * to an extra parameter (provided nesting_enabled is set).
416 * Otherwise, we simply return an expression that is equal
417 * to a parameter corresponding to the referenced variable.
419 __isl_give isl_pw_aff
*PetScan::extract_affine(DeclRefExpr
*expr
)
421 ValueDecl
*decl
= expr
->getDecl();
422 const Type
*type
= decl
->getType().getTypePtr();
428 if (!type
->isIntegerType()) {
433 if (assigned_value
.find(decl
) != assigned_value
.end()) {
434 if (assigned_value
[decl
])
435 return isl_pw_aff_copy(assigned_value
[decl
]);
437 return nested_access(expr
);
440 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
441 dim
= isl_space_params_alloc(ctx
, 1);
443 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
445 dom
= isl_set_universe(isl_space_copy(dim
));
446 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
447 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
449 return isl_pw_aff_alloc(dom
, aff
);
452 /* Extract an affine expression from an integer division operation.
453 * In particular, if "expr" is lhs/rhs, then return
455 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
457 * The second argument (rhs) is required to be a (positive) integer constant.
459 __isl_give isl_pw_aff
*PetScan::extract_affine_div(BinaryOperator
*expr
)
462 isl_pw_aff
*rhs
, *lhs
;
464 rhs
= extract_affine(expr
->getRHS());
465 is_cst
= isl_pw_aff_is_cst(rhs
);
466 if (is_cst
< 0 || !is_cst
) {
467 isl_pw_aff_free(rhs
);
473 lhs
= extract_affine(expr
->getLHS());
475 return isl_pw_aff_tdiv_q(lhs
, rhs
);
478 /* Extract an affine expression from a modulo operation.
479 * In particular, if "expr" is lhs/rhs, then return
481 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
483 * The second argument (rhs) is required to be a (positive) integer constant.
485 __isl_give isl_pw_aff
*PetScan::extract_affine_mod(BinaryOperator
*expr
)
488 isl_pw_aff
*rhs
, *lhs
;
490 rhs
= extract_affine(expr
->getRHS());
491 is_cst
= isl_pw_aff_is_cst(rhs
);
492 if (is_cst
< 0 || !is_cst
) {
493 isl_pw_aff_free(rhs
);
499 lhs
= extract_affine(expr
->getLHS());
501 return isl_pw_aff_tdiv_r(lhs
, rhs
);
504 /* Extract an affine expression from a multiplication operation.
505 * This is only allowed if at least one of the two arguments
506 * is a (piecewise) constant.
508 __isl_give isl_pw_aff
*PetScan::extract_affine_mul(BinaryOperator
*expr
)
513 lhs
= extract_affine(expr
->getLHS());
514 rhs
= extract_affine(expr
->getRHS());
516 if (!isl_pw_aff_is_cst(lhs
) && !isl_pw_aff_is_cst(rhs
)) {
517 isl_pw_aff_free(lhs
);
518 isl_pw_aff_free(rhs
);
523 return isl_pw_aff_mul(lhs
, rhs
);
526 /* Extract an affine expression from an addition or subtraction operation.
528 __isl_give isl_pw_aff
*PetScan::extract_affine_add(BinaryOperator
*expr
)
533 lhs
= extract_affine(expr
->getLHS());
534 rhs
= extract_affine(expr
->getRHS());
536 switch (expr
->getOpcode()) {
538 return isl_pw_aff_add(lhs
, rhs
);
540 return isl_pw_aff_sub(lhs
, rhs
);
542 isl_pw_aff_free(lhs
);
543 isl_pw_aff_free(rhs
);
553 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
,
559 isl_int_set_si(mod
, 1);
560 isl_int_mul_2exp(mod
, mod
, width
);
562 pwaff
= isl_pw_aff_mod(pwaff
, mod
);
569 /* Limit the domain of "pwaff" to those elements where the function
572 * 2^{width-1} <= pwaff < 2^{width-1}
574 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
578 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
579 isl_local_space
*ls
= isl_local_space_from_space(space
);
585 isl_int_set_si(v
, 1);
586 isl_int_mul_2exp(v
, v
, width
- 1);
588 bound
= isl_aff_zero_on_domain(ls
);
589 bound
= isl_aff_add_constant(bound
, v
);
590 b
= isl_pw_aff_from_aff(bound
);
592 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
593 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
595 b
= isl_pw_aff_neg(b
);
596 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
597 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
604 /* Handle potential overflows on signed computations.
606 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
607 * the we adjust the domain of "pa" to avoid overflows.
609 __isl_give isl_pw_aff
*PetScan::signed_overflow(__isl_take isl_pw_aff
*pa
,
612 if (options
->signed_overflow
== PET_OVERFLOW_AVOID
)
613 pa
= avoid_overflow(pa
, width
);
618 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
620 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
621 __isl_take isl_set
*dom
)
624 pa
= isl_set_indicator_function(set
);
625 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
629 /* Extract an affine expression from some binary operations.
630 * If the result of the expression is unsigned, then we wrap it
631 * based on the size of the type. Otherwise, we ensure that
632 * no overflow occurs.
634 __isl_give isl_pw_aff
*PetScan::extract_affine(BinaryOperator
*expr
)
639 switch (expr
->getOpcode()) {
642 res
= extract_affine_add(expr
);
645 res
= extract_affine_div(expr
);
648 res
= extract_affine_mod(expr
);
651 res
= extract_affine_mul(expr
);
661 return extract_condition(expr
);
667 width
= ast_context
.getIntWidth(expr
->getType());
668 if (expr
->getType()->isUnsignedIntegerType())
669 res
= wrap(res
, width
);
671 res
= signed_overflow(res
, width
);
676 /* Extract an affine expression from a negation operation.
678 __isl_give isl_pw_aff
*PetScan::extract_affine(UnaryOperator
*expr
)
680 if (expr
->getOpcode() == UO_Minus
)
681 return isl_pw_aff_neg(extract_affine(expr
->getSubExpr()));
682 if (expr
->getOpcode() == UO_LNot
)
683 return extract_condition(expr
);
689 __isl_give isl_pw_aff
*PetScan::extract_affine(ParenExpr
*expr
)
691 return extract_affine(expr
->getSubExpr());
694 /* Extract an affine expression from some special function calls.
695 * In particular, we handle "min", "max", "ceild" and "floord".
696 * In case of the latter two, the second argument needs to be
697 * a (positive) integer constant.
699 __isl_give isl_pw_aff
*PetScan::extract_affine(CallExpr
*expr
)
703 isl_pw_aff
*aff1
, *aff2
;
705 fd
= expr
->getDirectCallee();
711 name
= fd
->getDeclName().getAsString();
712 if (!(expr
->getNumArgs() == 2 && name
== "min") &&
713 !(expr
->getNumArgs() == 2 && name
== "max") &&
714 !(expr
->getNumArgs() == 2 && name
== "floord") &&
715 !(expr
->getNumArgs() == 2 && name
== "ceild")) {
720 if (name
== "min" || name
== "max") {
721 aff1
= extract_affine(expr
->getArg(0));
722 aff2
= extract_affine(expr
->getArg(1));
725 aff1
= isl_pw_aff_min(aff1
, aff2
);
727 aff1
= isl_pw_aff_max(aff1
, aff2
);
728 } else if (name
== "floord" || name
== "ceild") {
730 Expr
*arg2
= expr
->getArg(1);
732 if (arg2
->getStmtClass() != Stmt::IntegerLiteralClass
) {
736 aff1
= extract_affine(expr
->getArg(0));
738 extract_int(cast
<IntegerLiteral
>(arg2
), &v
);
739 aff1
= isl_pw_aff_scale_down(aff1
, v
);
741 if (name
== "floord")
742 aff1
= isl_pw_aff_floor(aff1
);
744 aff1
= isl_pw_aff_ceil(aff1
);
753 /* This method is called when we come across an access that is
754 * nested in what is supposed to be an affine expression.
755 * If nesting is allowed, we return a new parameter that corresponds
756 * to this nested access. Otherwise, we simply complain.
758 * Note that we currently don't allow nested accesses themselves
759 * to contain any nested accesses, so we check if we can extract
760 * the access without any nesting and complain if we can't.
762 * The new parameter is resolved in resolve_nested.
764 isl_pw_aff
*PetScan::nested_access(Expr
*expr
)
772 if (!nesting_enabled
) {
777 allow_nested
= false;
778 access
= extract_access(expr
);
784 isl_map_free(access
);
786 id
= isl_id_alloc(ctx
, NULL
, expr
);
787 dim
= isl_space_params_alloc(ctx
, 1);
789 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
791 dom
= isl_set_universe(isl_space_copy(dim
));
792 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
793 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
795 return isl_pw_aff_alloc(dom
, aff
);
798 /* Affine expressions are not supposed to contain array accesses,
799 * but if nesting is allowed, we return a parameter corresponding
800 * to the array access.
802 __isl_give isl_pw_aff
*PetScan::extract_affine(ArraySubscriptExpr
*expr
)
804 return nested_access(expr
);
807 /* Extract an affine expression from a conditional operation.
809 __isl_give isl_pw_aff
*PetScan::extract_affine(ConditionalOperator
*expr
)
811 isl_pw_aff
*cond
, *lhs
, *rhs
, *res
;
813 cond
= extract_condition(expr
->getCond());
814 lhs
= extract_affine(expr
->getTrueExpr());
815 rhs
= extract_affine(expr
->getFalseExpr());
817 return isl_pw_aff_cond(cond
, lhs
, rhs
);
820 /* Extract an affine expression, if possible, from "expr".
821 * Otherwise return NULL.
823 __isl_give isl_pw_aff
*PetScan::extract_affine(Expr
*expr
)
825 switch (expr
->getStmtClass()) {
826 case Stmt::ImplicitCastExprClass
:
827 return extract_affine(cast
<ImplicitCastExpr
>(expr
));
828 case Stmt::IntegerLiteralClass
:
829 return extract_affine(cast
<IntegerLiteral
>(expr
));
830 case Stmt::DeclRefExprClass
:
831 return extract_affine(cast
<DeclRefExpr
>(expr
));
832 case Stmt::BinaryOperatorClass
:
833 return extract_affine(cast
<BinaryOperator
>(expr
));
834 case Stmt::UnaryOperatorClass
:
835 return extract_affine(cast
<UnaryOperator
>(expr
));
836 case Stmt::ParenExprClass
:
837 return extract_affine(cast
<ParenExpr
>(expr
));
838 case Stmt::CallExprClass
:
839 return extract_affine(cast
<CallExpr
>(expr
));
840 case Stmt::ArraySubscriptExprClass
:
841 return extract_affine(cast
<ArraySubscriptExpr
>(expr
));
842 case Stmt::ConditionalOperatorClass
:
843 return extract_affine(cast
<ConditionalOperator
>(expr
));
850 __isl_give isl_map
*PetScan::extract_access(ImplicitCastExpr
*expr
)
852 return extract_access(expr
->getSubExpr());
855 /* Return the depth of an array of the given type.
857 static int array_depth(const Type
*type
)
859 if (type
->isPointerType())
860 return 1 + array_depth(type
->getPointeeType().getTypePtr());
861 if (type
->isArrayType()) {
862 const ArrayType
*atype
;
863 type
= type
->getCanonicalTypeInternal().getTypePtr();
864 atype
= cast
<ArrayType
>(type
);
865 return 1 + array_depth(atype
->getElementType().getTypePtr());
870 /* Return the element type of the given array type.
872 static QualType
base_type(QualType qt
)
874 const Type
*type
= qt
.getTypePtr();
876 if (type
->isPointerType())
877 return base_type(type
->getPointeeType());
878 if (type
->isArrayType()) {
879 const ArrayType
*atype
;
880 type
= type
->getCanonicalTypeInternal().getTypePtr();
881 atype
= cast
<ArrayType
>(type
);
882 return base_type(atype
->getElementType());
887 /* Extract an access relation from a reference to a variable.
888 * If the variable has name "A" and its type corresponds to an
889 * array of depth d, then the returned access relation is of the
892 * { [] -> A[i_1,...,i_d] }
894 __isl_give isl_map
*PetScan::extract_access(DeclRefExpr
*expr
)
896 return extract_access(expr
->getDecl());
899 /* Extract an access relation from a variable.
900 * If the variable has name "A" and its type corresponds to an
901 * array of depth d, then the returned access relation is of the
904 * { [] -> A[i_1,...,i_d] }
906 __isl_give isl_map
*PetScan::extract_access(ValueDecl
*decl
)
908 int depth
= array_depth(decl
->getType().getTypePtr());
909 isl_id
*id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
910 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, depth
);
913 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
915 access_rel
= isl_map_universe(dim
);
920 /* Extract an access relation from an integer contant.
921 * If the value of the constant is "v", then the returned access relation
926 __isl_give isl_map
*PetScan::extract_access(IntegerLiteral
*expr
)
928 return isl_map_from_range(isl_set_from_pw_aff(extract_affine(expr
)));
931 /* Try and extract an access relation from the given Expr.
932 * Return NULL if it doesn't work out.
934 __isl_give isl_map
*PetScan::extract_access(Expr
*expr
)
936 switch (expr
->getStmtClass()) {
937 case Stmt::ImplicitCastExprClass
:
938 return extract_access(cast
<ImplicitCastExpr
>(expr
));
939 case Stmt::DeclRefExprClass
:
940 return extract_access(cast
<DeclRefExpr
>(expr
));
941 case Stmt::ArraySubscriptExprClass
:
942 return extract_access(cast
<ArraySubscriptExpr
>(expr
));
943 case Stmt::IntegerLiteralClass
:
944 return extract_access(cast
<IntegerLiteral
>(expr
));
951 /* Assign the affine expression "index" to the output dimension "pos" of "map",
952 * restrict the domain to those values that result in a non-negative index
953 * and return the result.
955 __isl_give isl_map
*set_index(__isl_take isl_map
*map
, int pos
,
956 __isl_take isl_pw_aff
*index
)
959 int len
= isl_map_dim(map
, isl_dim_out
);
963 domain
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(index
));
964 index
= isl_pw_aff_intersect_domain(index
, domain
);
965 index_map
= isl_map_from_range(isl_set_from_pw_aff(index
));
966 index_map
= isl_map_insert_dims(index_map
, isl_dim_out
, 0, pos
);
967 index_map
= isl_map_add_dims(index_map
, isl_dim_out
, len
- pos
- 1);
968 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
969 index_map
= isl_map_set_tuple_id(index_map
, isl_dim_out
, id
);
971 map
= isl_map_intersect(map
, index_map
);
976 /* Extract an access relation from the given array subscript expression.
977 * If nesting is allowed in general, then we turn it on while
978 * examining the index expression.
980 * We first extract an access relation from the base.
981 * This will result in an access relation with a range that corresponds
982 * to the array being accessed and with earlier indices filled in already.
983 * We then extract the current index and fill that in as well.
984 * The position of the current index is based on the type of base.
985 * If base is the actual array variable, then the depth of this type
986 * will be the same as the depth of the array and we will fill in
987 * the first array index.
988 * Otherwise, the depth of the base type will be smaller and we will fill
991 __isl_give isl_map
*PetScan::extract_access(ArraySubscriptExpr
*expr
)
993 Expr
*base
= expr
->getBase();
994 Expr
*idx
= expr
->getIdx();
996 isl_map
*base_access
;
998 int depth
= array_depth(base
->getType().getTypePtr());
1000 bool save_nesting
= nesting_enabled
;
1002 nesting_enabled
= allow_nested
;
1004 base_access
= extract_access(base
);
1005 index
= extract_affine(idx
);
1007 nesting_enabled
= save_nesting
;
1009 pos
= isl_map_dim(base_access
, isl_dim_out
) - depth
;
1010 access
= set_index(base_access
, pos
, index
);
1015 /* Check if "expr" calls function "minmax" with two arguments and if so
1016 * make lhs and rhs refer to these two arguments.
1018 static bool is_minmax(Expr
*expr
, const char *minmax
, Expr
*&lhs
, Expr
*&rhs
)
1024 if (expr
->getStmtClass() != Stmt::CallExprClass
)
1027 call
= cast
<CallExpr
>(expr
);
1028 fd
= call
->getDirectCallee();
1032 if (call
->getNumArgs() != 2)
1035 name
= fd
->getDeclName().getAsString();
1039 lhs
= call
->getArg(0);
1040 rhs
= call
->getArg(1);
1045 /* Check if "expr" is of the form min(lhs, rhs) and if so make
1046 * lhs and rhs refer to the two arguments.
1048 static bool is_min(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
1050 return is_minmax(expr
, "min", lhs
, rhs
);
1053 /* Check if "expr" is of the form max(lhs, rhs) and if so make
1054 * lhs and rhs refer to the two arguments.
1056 static bool is_max(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
1058 return is_minmax(expr
, "max", lhs
, rhs
);
1061 /* Return "lhs && rhs", defined on the shared definition domain.
1063 static __isl_give isl_pw_aff
*pw_aff_and(__isl_take isl_pw_aff
*lhs
,
1064 __isl_take isl_pw_aff
*rhs
)
1069 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
1070 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1071 cond
= isl_set_intersect(isl_pw_aff_non_zero_set(lhs
),
1072 isl_pw_aff_non_zero_set(rhs
));
1073 return indicator_function(cond
, dom
);
1076 /* Return "lhs && rhs", with shortcut semantics.
1077 * That is, if lhs is false, then the result is defined even if rhs is not.
1078 * In practice, we compute lhs ? rhs : lhs.
1080 static __isl_give isl_pw_aff
*pw_aff_and_then(__isl_take isl_pw_aff
*lhs
,
1081 __isl_take isl_pw_aff
*rhs
)
1083 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), rhs
, lhs
);
1086 /* Return "lhs || rhs", with shortcut semantics.
1087 * That is, if lhs is true, then the result is defined even if rhs is not.
1088 * In practice, we compute lhs ? lhs : rhs.
1090 static __isl_give isl_pw_aff
*pw_aff_or_else(__isl_take isl_pw_aff
*lhs
,
1091 __isl_take isl_pw_aff
*rhs
)
1093 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), lhs
, rhs
);
1096 /* Extract an affine expressions representing the comparison "LHS op RHS"
1097 * "comp" is the original statement that "LHS op RHS" is derived from
1098 * and is used for diagnostics.
1100 * If the comparison is of the form
1104 * then the expression is constructed as the conjunction of
1109 * A similar optimization is performed for max(a,b) <= c.
1110 * We do this because that will lead to simpler representations
1111 * of the expression.
1112 * If isl is ever enhanced to explicitly deal with min and max expressions,
1113 * this optimization can be removed.
1115 __isl_give isl_pw_aff
*PetScan::extract_comparison(BinaryOperatorKind op
,
1116 Expr
*LHS
, Expr
*RHS
, Stmt
*comp
)
1125 return extract_comparison(BO_LT
, RHS
, LHS
, comp
);
1127 return extract_comparison(BO_LE
, RHS
, LHS
, comp
);
1129 if (op
== BO_LT
|| op
== BO_LE
) {
1130 Expr
*expr1
, *expr2
;
1131 if (is_min(RHS
, expr1
, expr2
)) {
1132 lhs
= extract_comparison(op
, LHS
, expr1
, comp
);
1133 rhs
= extract_comparison(op
, LHS
, expr2
, comp
);
1134 return pw_aff_and(lhs
, rhs
);
1136 if (is_max(LHS
, expr1
, expr2
)) {
1137 lhs
= extract_comparison(op
, expr1
, RHS
, comp
);
1138 rhs
= extract_comparison(op
, expr2
, RHS
, comp
);
1139 return pw_aff_and(lhs
, rhs
);
1143 lhs
= extract_affine(LHS
);
1144 rhs
= extract_affine(RHS
);
1146 dom
= isl_pw_aff_domain(isl_pw_aff_copy(lhs
));
1147 dom
= isl_set_intersect(dom
, isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1151 cond
= isl_pw_aff_lt_set(lhs
, rhs
);
1154 cond
= isl_pw_aff_le_set(lhs
, rhs
);
1157 cond
= isl_pw_aff_eq_set(lhs
, rhs
);
1160 cond
= isl_pw_aff_ne_set(lhs
, rhs
);
1163 isl_pw_aff_free(lhs
);
1164 isl_pw_aff_free(rhs
);
1170 cond
= isl_set_coalesce(cond
);
1171 res
= indicator_function(cond
, dom
);
1176 __isl_give isl_pw_aff
*PetScan::extract_comparison(BinaryOperator
*comp
)
1178 return extract_comparison(comp
->getOpcode(), comp
->getLHS(),
1179 comp
->getRHS(), comp
);
1182 /* Extract an affine expression representing the negation (logical not)
1183 * of a subexpression.
1185 __isl_give isl_pw_aff
*PetScan::extract_boolean(UnaryOperator
*op
)
1187 isl_set
*set_cond
, *dom
;
1188 isl_pw_aff
*cond
, *res
;
1190 cond
= extract_condition(op
->getSubExpr());
1192 dom
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1194 set_cond
= isl_pw_aff_zero_set(cond
);
1196 res
= indicator_function(set_cond
, dom
);
1201 /* Extract an affine expression representing the disjunction (logical or)
1202 * or conjunction (logical and) of two subexpressions.
1204 __isl_give isl_pw_aff
*PetScan::extract_boolean(BinaryOperator
*comp
)
1206 isl_pw_aff
*lhs
, *rhs
;
1208 lhs
= extract_condition(comp
->getLHS());
1209 rhs
= extract_condition(comp
->getRHS());
1211 switch (comp
->getOpcode()) {
1213 return pw_aff_and_then(lhs
, rhs
);
1215 return pw_aff_or_else(lhs
, rhs
);
1217 isl_pw_aff_free(lhs
);
1218 isl_pw_aff_free(rhs
);
1225 __isl_give isl_pw_aff
*PetScan::extract_condition(UnaryOperator
*expr
)
1227 switch (expr
->getOpcode()) {
1229 return extract_boolean(expr
);
1236 /* Extract the affine expression "expr != 0 ? 1 : 0".
1238 __isl_give isl_pw_aff
*PetScan::extract_implicit_condition(Expr
*expr
)
1243 res
= extract_affine(expr
);
1245 dom
= isl_pw_aff_domain(isl_pw_aff_copy(res
));
1246 set
= isl_pw_aff_non_zero_set(res
);
1248 res
= indicator_function(set
, dom
);
1253 /* Extract an affine expression from a boolean expression.
1254 * In particular, return the expression "expr ? 1 : 0".
1256 * If the expression doesn't look like a condition, we assume it
1257 * is an affine expression and return the condition "expr != 0 ? 1 : 0".
1259 __isl_give isl_pw_aff
*PetScan::extract_condition(Expr
*expr
)
1261 BinaryOperator
*comp
;
1264 isl_set
*u
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
1265 return indicator_function(u
, isl_set_copy(u
));
1268 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
1269 return extract_condition(cast
<ParenExpr
>(expr
)->getSubExpr());
1271 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
)
1272 return extract_condition(cast
<UnaryOperator
>(expr
));
1274 if (expr
->getStmtClass() != Stmt::BinaryOperatorClass
)
1275 return extract_implicit_condition(expr
);
1277 comp
= cast
<BinaryOperator
>(expr
);
1278 switch (comp
->getOpcode()) {
1285 return extract_comparison(comp
);
1288 return extract_boolean(comp
);
1290 return extract_implicit_condition(expr
);
1294 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
1298 return pet_op_minus
;
1300 return pet_op_post_inc
;
1302 return pet_op_post_dec
;
1304 return pet_op_pre_inc
;
1306 return pet_op_pre_dec
;
1312 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
1316 return pet_op_add_assign
;
1318 return pet_op_sub_assign
;
1320 return pet_op_mul_assign
;
1322 return pet_op_div_assign
;
1324 return pet_op_assign
;
1348 /* Construct a pet_expr representing a unary operator expression.
1350 struct pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
1352 struct pet_expr
*arg
;
1353 enum pet_op_type op
;
1355 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
1356 if (op
== pet_op_last
) {
1361 arg
= extract_expr(expr
->getSubExpr());
1363 if (expr
->isIncrementDecrementOp() &&
1364 arg
&& arg
->type
== pet_expr_access
) {
1369 return pet_expr_new_unary(ctx
, op
, arg
);
1372 /* Mark the given access pet_expr as a write.
1373 * If a scalar is being accessed, then mark its value
1374 * as unknown in assigned_value.
1376 void PetScan::mark_write(struct pet_expr
*access
)
1384 access
->acc
.write
= 1;
1385 access
->acc
.read
= 0;
1387 if (isl_map_dim(access
->acc
.access
, isl_dim_out
) != 0)
1390 id
= isl_map_get_tuple_id(access
->acc
.access
, isl_dim_out
);
1391 decl
= (ValueDecl
*) isl_id_get_user(id
);
1392 clear_assignment(assigned_value
, decl
);
1396 /* Assign "rhs" to "lhs".
1398 * In particular, if "lhs" is a scalar variable, then mark
1399 * the variable as having been assigned. If, furthermore, "rhs"
1400 * is an affine expression, then keep track of this value in assigned_value
1401 * so that we can plug it in when we later come across the same variable.
1403 void PetScan::assign(struct pet_expr
*lhs
, Expr
*rhs
)
1411 if (lhs
->type
!= pet_expr_access
)
1413 if (isl_map_dim(lhs
->acc
.access
, isl_dim_out
) != 0)
1416 id
= isl_map_get_tuple_id(lhs
->acc
.access
, isl_dim_out
);
1417 decl
= (ValueDecl
*) isl_id_get_user(id
);
1420 pa
= try_extract_affine(rhs
);
1421 clear_assignment(assigned_value
, decl
);
1424 assigned_value
[decl
] = pa
;
1425 insert_expression(pa
);
1428 /* Construct a pet_expr representing a binary operator expression.
1430 * If the top level operator is an assignment and the LHS is an access,
1431 * then we mark that access as a write. If the operator is a compound
1432 * assignment, the access is marked as both a read and a write.
1434 * If "expr" assigns something to a scalar variable, then we mark
1435 * the variable as having been assigned. If, furthermore, the expression
1436 * is affine, then keep track of this value in assigned_value
1437 * so that we can plug it in when we later come across the same variable.
1439 struct pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
1441 struct pet_expr
*lhs
, *rhs
;
1442 enum pet_op_type op
;
1444 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
1445 if (op
== pet_op_last
) {
1450 lhs
= extract_expr(expr
->getLHS());
1451 rhs
= extract_expr(expr
->getRHS());
1453 if (expr
->isAssignmentOp() && lhs
&& lhs
->type
== pet_expr_access
) {
1455 if (expr
->isCompoundAssignmentOp())
1459 if (expr
->getOpcode() == BO_Assign
)
1460 assign(lhs
, expr
->getRHS());
1462 return pet_expr_new_binary(ctx
, op
, lhs
, rhs
);
1465 /* Construct a pet_scop with a single statement killing the entire
1468 struct pet_scop
*PetScan::kill(Stmt
*stmt
, struct pet_array
*array
)
1471 struct pet_expr
*expr
;
1475 access
= isl_map_from_range(isl_set_copy(array
->extent
));
1476 expr
= pet_expr_kill_from_access(access
);
1477 return extract(stmt
, expr
);
1480 /* Construct a pet_scop for a (single) variable declaration.
1482 * The scop contains the variable being declared (as an array)
1483 * and a statement killing the array.
1485 * If the variable is initialized in the AST, then the scop
1486 * also contains an assignment to the variable.
1488 struct pet_scop
*PetScan::extract(DeclStmt
*stmt
)
1492 struct pet_expr
*lhs
, *rhs
, *pe
;
1493 struct pet_scop
*scop_decl
, *scop
;
1494 struct pet_array
*array
;
1496 if (!stmt
->isSingleDecl()) {
1501 decl
= stmt
->getSingleDecl();
1502 vd
= cast
<VarDecl
>(decl
);
1504 array
= extract_array(ctx
, vd
);
1506 array
->declared
= 1;
1507 scop_decl
= kill(stmt
, array
);
1508 scop_decl
= pet_scop_add_array(scop_decl
, array
);
1513 lhs
= pet_expr_from_access(extract_access(vd
));
1514 rhs
= extract_expr(vd
->getInit());
1517 assign(lhs
, vd
->getInit());
1519 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, lhs
, rhs
);
1520 scop
= extract(stmt
, pe
);
1522 scop_decl
= pet_scop_prefix(scop_decl
, 0);
1523 scop
= pet_scop_prefix(scop
, 1);
1525 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
1530 /* Construct a pet_expr representing a conditional operation.
1532 * We first try to extract the condition as an affine expression.
1533 * If that fails, we construct a pet_expr tree representing the condition.
1535 struct pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
1537 struct pet_expr
*cond
, *lhs
, *rhs
;
1540 pa
= try_extract_affine(expr
->getCond());
1542 isl_set
*test
= isl_set_from_pw_aff(pa
);
1543 cond
= pet_expr_from_access(isl_map_from_range(test
));
1545 cond
= extract_expr(expr
->getCond());
1546 lhs
= extract_expr(expr
->getTrueExpr());
1547 rhs
= extract_expr(expr
->getFalseExpr());
1549 return pet_expr_new_ternary(ctx
, cond
, lhs
, rhs
);
1552 struct pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
1554 return extract_expr(expr
->getSubExpr());
1557 /* Construct a pet_expr representing a floating point value.
1559 * If the floating point literal does not appear in a macro,
1560 * then we use the original representation in the source code
1561 * as the string representation. Otherwise, we use the pretty
1562 * printer to produce a string representation.
1564 struct pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
1568 const LangOptions
&LO
= PP
.getLangOpts();
1569 SourceLocation loc
= expr
->getLocation();
1571 if (!loc
.isMacroID()) {
1572 SourceManager
&SM
= PP
.getSourceManager();
1573 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
1574 s
= string(SM
.getCharacterData(loc
), len
);
1576 llvm::raw_string_ostream
S(s
);
1577 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
1580 d
= expr
->getValueAsApproximateDouble();
1581 return pet_expr_new_double(ctx
, d
, s
.c_str());
1584 /* Extract an access relation from "expr" and then convert it into
1587 struct pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
1590 struct pet_expr
*pe
;
1592 access
= extract_access(expr
);
1594 pe
= pet_expr_from_access(access
);
1599 struct pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
1601 return extract_expr(expr
->getSubExpr());
1604 /* Construct a pet_expr representing a function call.
1606 * If we are passing along a pointer to an array element
1607 * or an entire row or even higher dimensional slice of an array,
1608 * then the function being called may write into the array.
1610 * We assume here that if the function is declared to take a pointer
1611 * to a const type, then the function will perform a read
1612 * and that otherwise, it will perform a write.
1614 struct pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1616 struct pet_expr
*res
= NULL
;
1620 fd
= expr
->getDirectCallee();
1626 name
= fd
->getDeclName().getAsString();
1627 res
= pet_expr_new_call(ctx
, name
.c_str(), expr
->getNumArgs());
1631 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
1632 Expr
*arg
= expr
->getArg(i
);
1636 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
1637 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(arg
);
1638 arg
= ice
->getSubExpr();
1640 if (arg
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1641 UnaryOperator
*op
= cast
<UnaryOperator
>(arg
);
1642 if (op
->getOpcode() == UO_AddrOf
) {
1644 arg
= op
->getSubExpr();
1647 res
->args
[i
] = PetScan::extract_expr(arg
);
1648 main_arg
= res
->args
[i
];
1650 res
->args
[i
] = pet_expr_new_unary(ctx
,
1651 pet_op_address_of
, res
->args
[i
]);
1654 if (arg
->getStmtClass() == Stmt::ArraySubscriptExprClass
&&
1655 array_depth(arg
->getType().getTypePtr()) > 0)
1657 if (is_addr
&& main_arg
->type
== pet_expr_access
) {
1659 if (!fd
->hasPrototype()) {
1660 unsupported(expr
, "prototype required");
1663 parm
= fd
->getParamDecl(i
);
1664 if (!const_base(parm
->getType()))
1665 mark_write(main_arg
);
1675 /* Construct a pet_expr representing a (C style) cast.
1677 struct pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
1679 struct pet_expr
*arg
;
1682 arg
= extract_expr(expr
->getSubExpr());
1686 type
= expr
->getTypeAsWritten();
1687 return pet_expr_new_cast(ctx
, type
.getAsString().c_str(), arg
);
1690 /* Try and onstruct a pet_expr representing "expr".
1692 struct pet_expr
*PetScan::extract_expr(Expr
*expr
)
1694 switch (expr
->getStmtClass()) {
1695 case Stmt::UnaryOperatorClass
:
1696 return extract_expr(cast
<UnaryOperator
>(expr
));
1697 case Stmt::CompoundAssignOperatorClass
:
1698 case Stmt::BinaryOperatorClass
:
1699 return extract_expr(cast
<BinaryOperator
>(expr
));
1700 case Stmt::ImplicitCastExprClass
:
1701 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1702 case Stmt::ArraySubscriptExprClass
:
1703 case Stmt::DeclRefExprClass
:
1704 case Stmt::IntegerLiteralClass
:
1705 return extract_access_expr(expr
);
1706 case Stmt::FloatingLiteralClass
:
1707 return extract_expr(cast
<FloatingLiteral
>(expr
));
1708 case Stmt::ParenExprClass
:
1709 return extract_expr(cast
<ParenExpr
>(expr
));
1710 case Stmt::ConditionalOperatorClass
:
1711 return extract_expr(cast
<ConditionalOperator
>(expr
));
1712 case Stmt::CallExprClass
:
1713 return extract_expr(cast
<CallExpr
>(expr
));
1714 case Stmt::CStyleCastExprClass
:
1715 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1722 /* Check if the given initialization statement is an assignment.
1723 * If so, return that assignment. Otherwise return NULL.
1725 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1727 BinaryOperator
*ass
;
1729 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1732 ass
= cast
<BinaryOperator
>(init
);
1733 if (ass
->getOpcode() != BO_Assign
)
1739 /* Check if the given initialization statement is a declaration
1740 * of a single variable.
1741 * If so, return that declaration. Otherwise return NULL.
1743 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1747 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1750 decl
= cast
<DeclStmt
>(init
);
1752 if (!decl
->isSingleDecl())
1755 return decl
->getSingleDecl();
1758 /* Given the assignment operator in the initialization of a for loop,
1759 * extract the induction variable, i.e., the (integer)variable being
1762 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1769 lhs
= init
->getLHS();
1770 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1775 ref
= cast
<DeclRefExpr
>(lhs
);
1776 decl
= ref
->getDecl();
1777 type
= decl
->getType().getTypePtr();
1779 if (!type
->isIntegerType()) {
1787 /* Given the initialization statement of a for loop and the single
1788 * declaration in this initialization statement,
1789 * extract the induction variable, i.e., the (integer) variable being
1792 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1796 vd
= cast
<VarDecl
>(decl
);
1798 const QualType type
= vd
->getType();
1799 if (!type
->isIntegerType()) {
1804 if (!vd
->getInit()) {
1812 /* Check that op is of the form iv++ or iv--.
1813 * Return an affine expression "1" or "-1" accordingly.
1815 __isl_give isl_pw_aff
*PetScan::extract_unary_increment(
1816 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1823 if (!op
->isIncrementDecrementOp()) {
1828 sub
= op
->getSubExpr();
1829 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1834 ref
= cast
<DeclRefExpr
>(sub
);
1835 if (ref
->getDecl() != iv
) {
1840 space
= isl_space_params_alloc(ctx
, 0);
1841 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1843 if (op
->isIncrementOp())
1844 aff
= isl_aff_add_constant_si(aff
, 1);
1846 aff
= isl_aff_add_constant_si(aff
, -1);
1848 return isl_pw_aff_from_aff(aff
);
1851 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1852 * has a single constant expression, then put this constant in *user.
1853 * The caller is assumed to have checked that this function will
1854 * be called exactly once.
1856 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
1859 isl_int
*inc
= (isl_int
*)user
;
1862 if (isl_aff_is_cst(aff
))
1863 isl_aff_get_constant(aff
, inc
);
1873 /* Check if op is of the form
1877 * and return inc as an affine expression.
1879 * We extract an affine expression from the RHS, subtract iv and return
1882 __isl_give isl_pw_aff
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1883 clang::ValueDecl
*iv
)
1892 if (op
->getOpcode() != BO_Assign
) {
1898 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1903 ref
= cast
<DeclRefExpr
>(lhs
);
1904 if (ref
->getDecl() != iv
) {
1909 val
= extract_affine(op
->getRHS());
1911 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1913 dim
= isl_space_params_alloc(ctx
, 1);
1914 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1915 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1916 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1918 val
= isl_pw_aff_sub(val
, isl_pw_aff_from_aff(aff
));
1923 /* Check that op is of the form iv += cst or iv -= cst
1924 * and return an affine expression corresponding oto cst or -cst accordingly.
1926 __isl_give isl_pw_aff
*PetScan::extract_compound_increment(
1927 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1933 BinaryOperatorKind opcode
;
1935 opcode
= op
->getOpcode();
1936 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1940 if (opcode
== BO_SubAssign
)
1944 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1949 ref
= cast
<DeclRefExpr
>(lhs
);
1950 if (ref
->getDecl() != iv
) {
1955 val
= extract_affine(op
->getRHS());
1957 val
= isl_pw_aff_neg(val
);
1962 /* Check that the increment of the given for loop increments
1963 * (or decrements) the induction variable "iv" and return
1964 * the increment as an affine expression if successful.
1966 __isl_give isl_pw_aff
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1969 Stmt
*inc
= stmt
->getInc();
1976 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1977 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1978 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1979 return extract_compound_increment(
1980 cast
<CompoundAssignOperator
>(inc
), iv
);
1981 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1982 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1988 /* Embed the given iteration domain in an extra outer loop
1989 * with induction variable "var".
1990 * If this variable appeared as a parameter in the constraints,
1991 * it is replaced by the new outermost dimension.
1993 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
1994 __isl_take isl_id
*var
)
1998 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
1999 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
2001 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
2002 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2009 /* Return those elements in the space of "cond" that come after
2010 * (based on "sign") an element in "cond".
2012 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
2014 isl_map
*previous_to_this
;
2017 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
2019 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
2021 cond
= isl_set_apply(cond
, previous_to_this
);
2026 /* Create the infinite iteration domain
2028 * { [id] : id >= 0 }
2030 * If "scop" has an affine skip of type pet_skip_later,
2031 * then remove those iterations i that have an earlier iteration
2032 * where the skip condition is satisfied, meaning that iteration i
2034 * Since we are dealing with a loop without loop iterator,
2035 * the skip condition cannot refer to the current loop iterator and
2036 * so effectively, the returned set is of the form
2038 * { [0]; [id] : id >= 1 and not skip }
2040 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
,
2041 struct pet_scop
*scop
)
2043 isl_ctx
*ctx
= isl_id_get_ctx(id
);
2047 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
2048 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
2050 if (!pet_scop_has_affine_skip(scop
, pet_skip_later
))
2053 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
2054 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 1);
2055 skip
= isl_set_params(skip
);
2056 skip
= embed(skip
, isl_id_copy(id
));
2057 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2058 domain
= isl_set_subtract(domain
, after(skip
, 1));
2063 /* Create an identity mapping on the space containing "domain".
2065 static __isl_give isl_map
*identity_map(__isl_keep isl_set
*domain
)
2070 space
= isl_space_map_from_set(isl_set_get_space(domain
));
2071 id
= isl_map_identity(space
);
2076 /* Add a filter to "scop" that imposes that it is only executed
2077 * when "break_access" has a zero value for all previous iterations
2080 * The input "break_access" has a zero-dimensional domain and range.
2082 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
2083 __isl_take isl_map
*break_access
, __isl_take isl_set
*domain
, int sign
)
2085 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2089 id_test
= isl_map_get_tuple_id(break_access
, isl_dim_out
);
2090 break_access
= isl_map_add_dims(break_access
, isl_dim_in
, 1);
2091 break_access
= isl_map_add_dims(break_access
, isl_dim_out
, 1);
2092 break_access
= isl_map_intersect_range(break_access
, domain
);
2093 break_access
= isl_map_set_tuple_id(break_access
, isl_dim_out
, id_test
);
2095 prev
= isl_map_lex_gt_first(isl_map_get_space(break_access
), 1);
2097 prev
= isl_map_lex_lt_first(isl_map_get_space(break_access
), 1);
2098 break_access
= isl_map_intersect(break_access
, prev
);
2099 scop
= pet_scop_filter(scop
, break_access
, 0);
2100 scop
= pet_scop_merge_filters(scop
);
2105 /* Construct a pet_scop for an infinite loop around the given body.
2107 * We extract a pet_scop for the body and then embed it in a loop with
2116 * If the body contains any break, then it is taken into
2117 * account in infinite_domain (if the skip condition is affine)
2118 * or in scop_add_break (if the skip condition is not affine).
2120 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
)
2126 struct pet_scop
*scop
;
2129 scop
= extract(body
);
2133 id
= isl_id_alloc(ctx
, "t", NULL
);
2134 domain
= infinite_domain(isl_id_copy(id
), scop
);
2135 ident
= identity_map(domain
);
2137 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
2139 access
= pet_scop_get_skip_map(scop
, pet_skip_later
);
2141 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
2142 isl_map_copy(ident
), ident
, id
);
2144 scop
= scop_add_break(scop
, access
, domain
, 1);
2146 isl_set_free(domain
);
2151 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
2157 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
)
2159 return extract_infinite_loop(stmt
->getBody());
2162 /* Create an access to a virtual array representing the result
2164 * Unlike other accessed data, the id of the array is NULL as
2165 * there is no ValueDecl in the program corresponding to the virtual
2167 * The array starts out as a scalar, but grows along with the
2168 * statement writing to the array in pet_scop_embed.
2170 static __isl_give isl_map
*create_test_access(isl_ctx
*ctx
, int test_nr
)
2172 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, 0);
2176 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
2177 id
= isl_id_alloc(ctx
, name
, NULL
);
2178 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
2179 return isl_map_universe(dim
);
2182 /* Add an array with the given extent ("access") to the list
2183 * of arrays in "scop" and return the extended pet_scop.
2184 * The array is marked as attaining values 0 and 1 only and
2185 * as each element being assigned at most once.
2187 static struct pet_scop
*scop_add_array(struct pet_scop
*scop
,
2188 __isl_keep isl_map
*access
, clang::ASTContext
&ast_ctx
)
2190 isl_ctx
*ctx
= isl_map_get_ctx(access
);
2192 struct pet_array
*array
;
2199 array
= isl_calloc_type(ctx
, struct pet_array
);
2203 array
->extent
= isl_map_range(isl_map_copy(access
));
2204 dim
= isl_space_params_alloc(ctx
, 0);
2205 array
->context
= isl_set_universe(dim
);
2206 dim
= isl_space_set_alloc(ctx
, 0, 1);
2207 array
->value_bounds
= isl_set_universe(dim
);
2208 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
2210 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
2212 array
->element_type
= strdup("int");
2213 array
->element_size
= ast_ctx
.getTypeInfo(ast_ctx
.IntTy
).first
/ 8;
2214 array
->uniquely_defined
= 1;
2216 if (!array
->extent
|| !array
->context
)
2217 array
= pet_array_free(array
);
2219 scop
= pet_scop_add_array(scop
, array
);
2223 pet_scop_free(scop
);
2227 /* Construct a pet_scop for a while loop of the form
2232 * In particular, construct a scop for an infinite loop around body and
2233 * intersect the domain with the affine expression.
2234 * Note that this intersection may result in an empty loop.
2236 struct pet_scop
*PetScan::extract_affine_while(__isl_take isl_pw_aff
*pa
,
2239 struct pet_scop
*scop
;
2243 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2244 dom
= isl_pw_aff_non_zero_set(pa
);
2245 scop
= extract_infinite_loop(body
);
2246 scop
= pet_scop_restrict(scop
, dom
);
2247 scop
= pet_scop_restrict_context(scop
, valid
);
2252 /* Construct a scop for a while, given the scops for the condition
2253 * and the body, the filter access and the iteration domain of
2256 * In particular, the scop for the condition is filtered to depend
2257 * on "test_access" evaluating to true for all previous iterations
2258 * of the loop, while the scop for the body is filtered to depend
2259 * on "test_access" evaluating to true for all iterations up to the
2260 * current iteration.
2262 * These filtered scops are then combined into a single scop.
2264 * "sign" is positive if the iterator increases and negative
2267 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
2268 struct pet_scop
*scop_body
, __isl_take isl_map
*test_access
,
2269 __isl_take isl_set
*domain
, int sign
)
2271 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2275 id_test
= isl_map_get_tuple_id(test_access
, isl_dim_out
);
2276 test_access
= isl_map_add_dims(test_access
, isl_dim_in
, 1);
2277 test_access
= isl_map_add_dims(test_access
, isl_dim_out
, 1);
2278 test_access
= isl_map_intersect_range(test_access
, domain
);
2279 test_access
= isl_map_set_tuple_id(test_access
, isl_dim_out
, id_test
);
2281 prev
= isl_map_lex_ge_first(isl_map_get_space(test_access
), 1);
2283 prev
= isl_map_lex_le_first(isl_map_get_space(test_access
), 1);
2284 test_access
= isl_map_intersect(test_access
, prev
);
2285 scop_body
= pet_scop_filter(scop_body
, isl_map_copy(test_access
), 1);
2287 prev
= isl_map_lex_gt_first(isl_map_get_space(test_access
), 1);
2289 prev
= isl_map_lex_lt_first(isl_map_get_space(test_access
), 1);
2290 test_access
= isl_map_intersect(test_access
, prev
);
2291 scop_cond
= pet_scop_filter(scop_cond
, test_access
, 1);
2293 return pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
2296 /* Check if the while loop is of the form
2298 * while (affine expression)
2301 * If so, call extract_affine_while to construct a scop.
2303 * Otherwise, construct a generic while scop, with iteration domain
2304 * { [t] : t >= 0 }. The scop consists of two parts, one for
2305 * evaluating the condition and one for the body.
2306 * The schedule is adjusted to reflect that the condition is evaluated
2307 * before the body is executed and the body is filtered to depend
2308 * on the result of the condition evaluating to true on all iterations
2309 * up to the current iteration, while the evaluation the condition itself
2310 * is filtered to depend on the result of the condition evaluating to true
2311 * on all previous iterations.
2312 * The context of the scop representing the body is dropped
2313 * because we don't know how many times the body will be executed,
2316 * If the body contains any break, then it is taken into
2317 * account in infinite_domain (if the skip condition is affine)
2318 * or in scop_add_break (if the skip condition is not affine).
2320 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
)
2324 isl_map
*test_access
;
2328 struct pet_scop
*scop
, *scop_body
;
2330 isl_map
*break_access
;
2332 cond
= stmt
->getCond();
2338 clear_assignments
clear(assigned_value
);
2339 clear
.TraverseStmt(stmt
->getBody());
2341 pa
= try_extract_affine_condition(cond
);
2343 return extract_affine_while(pa
, stmt
->getBody());
2345 if (!allow_nested
) {
2350 test_access
= create_test_access(ctx
, n_test
++);
2351 scop
= extract_non_affine_condition(cond
, isl_map_copy(test_access
));
2352 scop
= scop_add_array(scop
, test_access
, ast_context
);
2353 scop_body
= extract(stmt
->getBody());
2355 id
= isl_id_alloc(ctx
, "t", NULL
);
2356 domain
= infinite_domain(isl_id_copy(id
), scop_body
);
2357 ident
= identity_map(domain
);
2359 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
2361 break_access
= pet_scop_get_skip_map(scop_body
, pet_skip_later
);
2363 scop
= pet_scop_prefix(scop
, 0);
2364 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_map_copy(ident
),
2365 isl_map_copy(ident
), isl_id_copy(id
));
2366 scop_body
= pet_scop_reset_context(scop_body
);
2367 scop_body
= pet_scop_prefix(scop_body
, 1);
2368 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
2369 isl_map_copy(ident
), ident
, id
);
2371 if (has_var_break
) {
2372 scop
= scop_add_break(scop
, isl_map_copy(break_access
),
2373 isl_set_copy(domain
), 1);
2374 scop_body
= scop_add_break(scop_body
, break_access
,
2375 isl_set_copy(domain
), 1);
2377 scop
= scop_add_while(scop
, scop_body
, test_access
, domain
, 1);
2382 /* Check whether "cond" expresses a simple loop bound
2383 * on the only set dimension.
2384 * In particular, if "up" is set then "cond" should contain only
2385 * upper bounds on the set dimension.
2386 * Otherwise, it should contain only lower bounds.
2388 static bool is_simple_bound(__isl_keep isl_set
*cond
, isl_int inc
)
2390 if (isl_int_is_pos(inc
))
2391 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
2393 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
2396 /* Extend a condition on a given iteration of a loop to one that
2397 * imposes the same condition on all previous iterations.
2398 * "domain" expresses the lower [upper] bound on the iterations
2399 * when inc is positive [negative].
2401 * In particular, we construct the condition (when inc is positive)
2403 * forall i' : (domain(i') and i' <= i) => cond(i')
2405 * which is equivalent to
2407 * not exists i' : domain(i') and i' <= i and not cond(i')
2409 * We construct this set by negating cond, applying a map
2411 * { [i'] -> [i] : domain(i') and i' <= i }
2413 * and then negating the result again.
2415 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
2416 __isl_take isl_set
*domain
, isl_int inc
)
2418 isl_map
*previous_to_this
;
2420 if (isl_int_is_pos(inc
))
2421 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
2423 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
2425 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
2427 cond
= isl_set_complement(cond
);
2428 cond
= isl_set_apply(cond
, previous_to_this
);
2429 cond
= isl_set_complement(cond
);
2434 /* Construct a domain of the form
2436 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
2438 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
2439 __isl_take isl_pw_aff
*init
, isl_int inc
)
2445 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
2446 dim
= isl_pw_aff_get_domain_space(init
);
2447 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2448 aff
= isl_aff_add_coefficient(aff
, isl_dim_in
, 0, inc
);
2449 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
2451 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
2452 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
2453 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2454 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
2456 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
2458 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
2460 return isl_set_params(set
);
2463 /* Assuming "cond" represents a bound on a loop where the loop
2464 * iterator "iv" is incremented (or decremented) by one, check if wrapping
2467 * Under the given assumptions, wrapping is only possible if "cond" allows
2468 * for the last value before wrapping, i.e., 2^width - 1 in case of an
2469 * increasing iterator and 0 in case of a decreasing iterator.
2471 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
, isl_int inc
)
2477 test
= isl_set_copy(cond
);
2479 isl_int_init(limit
);
2480 if (isl_int_is_neg(inc
))
2481 isl_int_set_si(limit
, 0);
2483 isl_int_set_si(limit
, 1);
2484 isl_int_mul_2exp(limit
, limit
, get_type_size(iv
));
2485 isl_int_sub_ui(limit
, limit
, 1);
2488 test
= isl_set_fix(cond
, isl_dim_set
, 0, limit
);
2489 cw
= !isl_set_is_empty(test
);
2492 isl_int_clear(limit
);
2497 /* Given a one-dimensional space, construct the following mapping on this
2500 * { [v] -> [v mod 2^width] }
2502 * where width is the number of bits used to represent the values
2503 * of the unsigned variable "iv".
2505 static __isl_give isl_map
*compute_wrapping(__isl_take isl_space
*dim
,
2513 isl_int_set_si(mod
, 1);
2514 isl_int_mul_2exp(mod
, mod
, get_type_size(iv
));
2516 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2517 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2518 aff
= isl_aff_mod(aff
, mod
);
2522 return isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2523 map
= isl_map_reverse(map
);
2526 /* Project out the parameter "id" from "set".
2528 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
2529 __isl_keep isl_id
*id
)
2533 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
2535 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2540 /* Compute the set of parameters for which "set1" is a subset of "set2".
2542 * set1 is a subset of set2 if
2544 * forall i in set1 : i in set2
2548 * not exists i in set1 and i not in set2
2552 * not exists i in set1 \ set2
2554 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
2555 __isl_take isl_set
*set2
)
2557 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
2560 /* Compute the set of parameter values for which "cond" holds
2561 * on the next iteration for each element of "dom".
2563 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2564 * and then compute the set of parameters for which the result is a subset
2567 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
2568 __isl_take isl_set
*dom
, isl_int inc
)
2574 space
= isl_set_get_space(dom
);
2575 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2576 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2577 aff
= isl_aff_add_constant(aff
, inc
);
2578 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2580 dom
= isl_set_apply(dom
, next
);
2582 return enforce_subset(dom
, cond
);
2585 /* Does "id" refer to a nested access?
2587 static bool is_nested_parameter(__isl_keep isl_id
*id
)
2589 return id
&& isl_id_get_user(id
) && !isl_id_get_name(id
);
2592 /* Does parameter "pos" of "space" refer to a nested access?
2594 static bool is_nested_parameter(__isl_keep isl_space
*space
, int pos
)
2599 id
= isl_space_get_dim_id(space
, isl_dim_param
, pos
);
2600 nested
= is_nested_parameter(id
);
2606 /* Does "space" involve any parameters that refer to nested
2607 * accesses, i.e., parameters with no name?
2609 static bool has_nested(__isl_keep isl_space
*space
)
2613 nparam
= isl_space_dim(space
, isl_dim_param
);
2614 for (int i
= 0; i
< nparam
; ++i
)
2615 if (is_nested_parameter(space
, i
))
2621 /* Does "pa" involve any parameters that refer to nested
2622 * accesses, i.e., parameters with no name?
2624 static bool has_nested(__isl_keep isl_pw_aff
*pa
)
2629 space
= isl_pw_aff_get_space(pa
);
2630 nested
= has_nested(space
);
2631 isl_space_free(space
);
2636 /* Construct a pet_scop for a for statement.
2637 * The for loop is required to be of the form
2639 * for (i = init; condition; ++i)
2643 * for (i = init; condition; --i)
2645 * The initialization of the for loop should either be an assignment
2646 * to an integer variable, or a declaration of such a variable with
2649 * The condition is allowed to contain nested accesses, provided
2650 * they are not being written to inside the body of the loop.
2651 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2652 * essentially treated as a while loop, with iteration domain
2653 * { [i] : i >= init }.
2655 * We extract a pet_scop for the body and then embed it in a loop with
2656 * iteration domain and schedule
2658 * { [i] : i >= init and condition' }
2663 * { [i] : i <= init and condition' }
2666 * Where condition' is equal to condition if the latter is
2667 * a simple upper [lower] bound and a condition that is extended
2668 * to apply to all previous iterations otherwise.
2670 * If the condition is non-affine, then we drop the condition from the
2671 * iteration domain and instead create a separate statement
2672 * for evaluating the condition. The body is then filtered to depend
2673 * on the result of the condition evaluating to true on all iterations
2674 * up to the current iteration, while the evaluation the condition itself
2675 * is filtered to depend on the result of the condition evaluating to true
2676 * on all previous iterations.
2677 * The context of the scop representing the body is dropped
2678 * because we don't know how many times the body will be executed,
2681 * If the stride of the loop is not 1, then "i >= init" is replaced by
2683 * (exists a: i = init + stride * a and a >= 0)
2685 * If the loop iterator i is unsigned, then wrapping may occur.
2686 * During the computation, we work with a virtual iterator that
2687 * does not wrap. However, the condition in the code applies
2688 * to the wrapped value, so we need to change condition(i)
2689 * into condition([i % 2^width]).
2690 * After computing the virtual domain and schedule, we apply
2691 * the function { [v] -> [v % 2^width] } to the domain and the domain
2692 * of the schedule. In order not to lose any information, we also
2693 * need to intersect the domain of the schedule with the virtual domain
2694 * first, since some iterations in the wrapped domain may be scheduled
2695 * several times, typically an infinite number of times.
2696 * Note that there may be no need to perform this final wrapping
2697 * if the loop condition (after wrapping) satisfies certain conditions.
2698 * However, the is_simple_bound condition is not enough since it doesn't
2699 * check if there even is an upper bound.
2701 * If the loop condition is non-affine, then we keep the virtual
2702 * iterator in the iteration domain and instead replace all accesses
2703 * to the original iterator by the wrapping of the virtual iterator.
2705 * Wrapping on unsigned iterators can be avoided entirely if
2706 * loop condition is simple, the loop iterator is incremented
2707 * [decremented] by one and the last value before wrapping cannot
2708 * possibly satisfy the loop condition.
2710 * Before extracting a pet_scop from the body we remove all
2711 * assignments in assigned_value to variables that are assigned
2712 * somewhere in the body of the loop.
2714 * Valid parameters for a for loop are those for which the initial
2715 * value itself, the increment on each domain iteration and
2716 * the condition on both the initial value and
2717 * the result of incrementing the iterator for each iteration of the domain
2719 * If the loop condition is non-affine, then we only consider validity
2720 * of the initial value.
2722 * If the body contains any break, then we keep track of it in "skip"
2723 * (if the skip condition is affine) or it is handled in scop_add_break
2724 * (if the skip condition is not affine).
2725 * Note that the affine break condition needs to be considered with
2726 * respect to previous iterations in the virtual domain (if any)
2727 * and that the domain needs to be kept virtual if there is a non-affine
2730 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
)
2732 BinaryOperator
*ass
;
2740 isl_set
*cond
= NULL
;
2741 isl_set
*skip
= NULL
;
2743 struct pet_scop
*scop
, *scop_cond
= NULL
;
2744 assigned_value_cache
cache(assigned_value
);
2750 bool keep_virtual
= false;
2751 bool has_affine_break
;
2753 isl_map
*wrap
= NULL
;
2754 isl_pw_aff
*pa
, *pa_inc
, *init_val
;
2755 isl_set
*valid_init
;
2756 isl_set
*valid_cond
;
2757 isl_set
*valid_cond_init
;
2758 isl_set
*valid_cond_next
;
2760 isl_map
*test_access
= NULL
, *break_access
= NULL
;
2763 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
2764 return extract_infinite_for(stmt
);
2766 init
= stmt
->getInit();
2771 if ((ass
= initialization_assignment(init
)) != NULL
) {
2772 iv
= extract_induction_variable(ass
);
2775 lhs
= ass
->getLHS();
2776 rhs
= ass
->getRHS();
2777 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
2778 VarDecl
*var
= extract_induction_variable(init
, decl
);
2782 rhs
= var
->getInit();
2783 lhs
= create_DeclRefExpr(var
);
2785 unsupported(stmt
->getInit());
2789 pa_inc
= extract_increment(stmt
, iv
);
2794 if (isl_pw_aff_n_piece(pa_inc
) != 1 ||
2795 isl_pw_aff_foreach_piece(pa_inc
, &extract_cst
, &inc
) < 0) {
2796 isl_pw_aff_free(pa_inc
);
2797 unsupported(stmt
->getInc());
2801 valid_inc
= isl_pw_aff_domain(pa_inc
);
2803 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
2805 assigned_value
.erase(iv
);
2806 clear_assignments
clear(assigned_value
);
2807 clear
.TraverseStmt(stmt
->getBody());
2809 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
2811 pa
= try_extract_nested_condition(stmt
->getCond());
2812 if (allow_nested
&& (!pa
|| has_nested(pa
)))
2815 scop
= extract(stmt
->getBody());
2817 has_affine_break
= scop
&&
2818 pet_scop_has_affine_skip(scop
, pet_skip_later
);
2819 if (has_affine_break
) {
2820 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
2821 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 1);
2822 skip
= isl_set_params(skip
);
2824 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
2825 if (has_var_break
) {
2826 break_access
= pet_scop_get_skip_map(scop
, pet_skip_later
);
2827 keep_virtual
= true;
2830 if (pa
&& !is_nested_allowed(pa
, scop
)) {
2831 isl_pw_aff_free(pa
);
2835 if (!allow_nested
&& !pa
)
2836 pa
= try_extract_affine_condition(stmt
->getCond());
2837 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2838 cond
= isl_pw_aff_non_zero_set(pa
);
2839 if (allow_nested
&& !cond
) {
2840 int save_n_stmt
= n_stmt
;
2841 test_access
= create_test_access(ctx
, n_test
++);
2843 scop_cond
= extract_non_affine_condition(stmt
->getCond(),
2844 isl_map_copy(test_access
));
2845 n_stmt
= save_n_stmt
;
2846 scop_cond
= scop_add_array(scop_cond
, test_access
, ast_context
);
2847 scop_cond
= pet_scop_prefix(scop_cond
, 0);
2848 scop
= pet_scop_reset_context(scop
);
2849 scop
= pet_scop_prefix(scop
, 1);
2850 keep_virtual
= true;
2851 cond
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2854 cond
= embed(cond
, isl_id_copy(id
));
2855 skip
= embed(skip
, isl_id_copy(id
));
2856 valid_cond
= isl_set_coalesce(valid_cond
);
2857 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
2858 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
2859 is_one
= isl_int_is_one(inc
) || isl_int_is_negone(inc
);
2860 is_virtual
= is_unsigned
&& (!is_one
|| can_wrap(cond
, iv
, inc
));
2862 init_val
= extract_affine(rhs
);
2863 valid_cond_init
= enforce_subset(
2864 isl_set_from_pw_aff(isl_pw_aff_copy(init_val
)),
2865 isl_set_copy(valid_cond
));
2866 if (is_one
&& !is_virtual
) {
2867 isl_pw_aff_free(init_val
);
2868 pa
= extract_comparison(isl_int_is_pos(inc
) ? BO_GE
: BO_LE
,
2870 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2871 valid_init
= set_project_out_by_id(valid_init
, id
);
2872 domain
= isl_pw_aff_non_zero_set(pa
);
2874 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
2875 domain
= strided_domain(isl_id_copy(id
), init_val
, inc
);
2878 domain
= embed(domain
, isl_id_copy(id
));
2881 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
2882 rev_wrap
= isl_map_reverse(isl_map_copy(wrap
));
2883 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
2884 skip
= isl_set_apply(skip
, isl_map_copy(rev_wrap
));
2885 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
2886 valid_inc
= isl_set_apply(valid_inc
, rev_wrap
);
2888 is_simple
= is_simple_bound(cond
, inc
);
2890 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
2891 is_simple
= is_simple_bound(cond
, inc
);
2894 cond
= valid_for_each_iteration(cond
,
2895 isl_set_copy(domain
), inc
);
2896 domain
= isl_set_intersect(domain
, cond
);
2897 if (has_affine_break
) {
2898 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2899 skip
= after(skip
, isl_int_sgn(inc
));
2900 domain
= isl_set_subtract(domain
, skip
);
2902 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
2903 space
= isl_space_from_domain(isl_set_get_space(domain
));
2904 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2905 sched
= isl_map_universe(space
);
2906 if (isl_int_is_pos(inc
))
2907 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
2909 sched
= isl_map_oppose(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
2911 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
), inc
);
2912 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
2914 if (is_virtual
&& !keep_virtual
) {
2915 wrap
= isl_map_set_dim_id(wrap
,
2916 isl_dim_out
, 0, isl_id_copy(id
));
2917 sched
= isl_map_intersect_domain(sched
, isl_set_copy(domain
));
2918 domain
= isl_set_apply(domain
, isl_map_copy(wrap
));
2919 sched
= isl_map_apply_domain(sched
, wrap
);
2921 if (!(is_virtual
&& keep_virtual
)) {
2922 space
= isl_set_get_space(domain
);
2923 wrap
= isl_map_identity(isl_space_map_from_set(space
));
2926 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
2927 isl_map_copy(sched
), isl_map_copy(wrap
), isl_id_copy(id
));
2928 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
2929 scop
= resolve_nested(scop
);
2931 scop
= scop_add_break(scop
, break_access
, isl_set_copy(domain
),
2934 scop
= scop_add_while(scop_cond
, scop
, test_access
, domain
,
2936 isl_set_free(valid_inc
);
2938 scop
= pet_scop_restrict_context(scop
, valid_inc
);
2939 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
2940 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
2941 isl_set_free(domain
);
2943 clear_assignment(assigned_value
, iv
);
2947 scop
= pet_scop_restrict_context(scop
, valid_init
);
2952 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
, bool skip_declarations
)
2954 return extract(stmt
->children(), true, skip_declarations
);
2957 /* Does parameter "pos" of "map" refer to a nested access?
2959 static bool is_nested_parameter(__isl_keep isl_map
*map
, int pos
)
2964 id
= isl_map_get_dim_id(map
, isl_dim_param
, pos
);
2965 nested
= is_nested_parameter(id
);
2971 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
2973 static int n_nested_parameter(__isl_keep isl_space
*space
)
2978 nparam
= isl_space_dim(space
, isl_dim_param
);
2979 for (int i
= 0; i
< nparam
; ++i
)
2980 if (is_nested_parameter(space
, i
))
2986 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
2988 static int n_nested_parameter(__isl_keep isl_map
*map
)
2993 space
= isl_map_get_space(map
);
2994 n
= n_nested_parameter(space
);
2995 isl_space_free(space
);
3000 /* For each nested access parameter in "space",
3001 * construct a corresponding pet_expr, place it in args and
3002 * record its position in "param2pos".
3003 * "n_arg" is the number of elements that are already in args.
3004 * The position recorded in "param2pos" takes this number into account.
3005 * If the pet_expr corresponding to a parameter is identical to
3006 * the pet_expr corresponding to an earlier parameter, then these two
3007 * parameters are made to refer to the same element in args.
3009 * Return the final number of elements in args or -1 if an error has occurred.
3011 int PetScan::extract_nested(__isl_keep isl_space
*space
,
3012 int n_arg
, struct pet_expr
**args
, std::map
<int,int> ¶m2pos
)
3016 nparam
= isl_space_dim(space
, isl_dim_param
);
3017 for (int i
= 0; i
< nparam
; ++i
) {
3019 isl_id
*id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
3022 if (!is_nested_parameter(id
)) {
3027 nested
= (Expr
*) isl_id_get_user(id
);
3028 args
[n_arg
] = extract_expr(nested
);
3032 for (j
= 0; j
< n_arg
; ++j
)
3033 if (pet_expr_is_equal(args
[j
], args
[n_arg
]))
3037 pet_expr_free(args
[n_arg
]);
3041 param2pos
[i
] = n_arg
++;
3049 /* For each nested access parameter in the access relations in "expr",
3050 * construct a corresponding pet_expr, place it in expr->args and
3051 * record its position in "param2pos".
3052 * n is the number of nested access parameters.
3054 struct pet_expr
*PetScan::extract_nested(struct pet_expr
*expr
, int n
,
3055 std::map
<int,int> ¶m2pos
)
3059 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n
);
3064 space
= isl_map_get_space(expr
->acc
.access
);
3065 n
= extract_nested(space
, 0, expr
->args
, param2pos
);
3066 isl_space_free(space
);
3074 pet_expr_free(expr
);
3078 /* Look for parameters in any access relation in "expr" that
3079 * refer to nested accesses. In particular, these are
3080 * parameters with no name.
3082 * If there are any such parameters, then the domain of the access
3083 * relation, which is still [] at this point, is replaced by
3084 * [[] -> [t_1,...,t_n]], with n the number of these parameters
3085 * (after identifying identical nested accesses).
3086 * The parameters are then equated to the corresponding t dimensions
3087 * and subsequently projected out.
3088 * param2pos maps the position of the parameter to the position
3089 * of the corresponding t dimension.
3091 struct pet_expr
*PetScan::resolve_nested(struct pet_expr
*expr
)
3098 std::map
<int,int> param2pos
;
3103 for (int i
= 0; i
< expr
->n_arg
; ++i
) {
3104 expr
->args
[i
] = resolve_nested(expr
->args
[i
]);
3105 if (!expr
->args
[i
]) {
3106 pet_expr_free(expr
);
3111 if (expr
->type
!= pet_expr_access
)
3114 n
= n_nested_parameter(expr
->acc
.access
);
3118 expr
= extract_nested(expr
, n
, param2pos
);
3123 nparam
= isl_map_dim(expr
->acc
.access
, isl_dim_param
);
3124 n_in
= isl_map_dim(expr
->acc
.access
, isl_dim_in
);
3125 dim
= isl_map_get_space(expr
->acc
.access
);
3126 dim
= isl_space_domain(dim
);
3127 dim
= isl_space_from_domain(dim
);
3128 dim
= isl_space_add_dims(dim
, isl_dim_out
, n
);
3129 map
= isl_map_universe(dim
);
3130 map
= isl_map_domain_map(map
);
3131 map
= isl_map_reverse(map
);
3132 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
3134 for (int i
= nparam
- 1; i
>= 0; --i
) {
3135 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
3137 if (!is_nested_parameter(id
)) {
3142 expr
->acc
.access
= isl_map_equate(expr
->acc
.access
,
3143 isl_dim_param
, i
, isl_dim_in
,
3144 n_in
+ param2pos
[i
]);
3145 expr
->acc
.access
= isl_map_project_out(expr
->acc
.access
,
3146 isl_dim_param
, i
, 1);
3153 pet_expr_free(expr
);
3157 /* Convert a top-level pet_expr to a pet_scop with one statement.
3158 * This mainly involves resolving nested expression parameters
3159 * and setting the name of the iteration space.
3160 * The name is given by "label" if it is non-NULL. Otherwise,
3161 * it is of the form S_<n_stmt>.
3163 struct pet_scop
*PetScan::extract(Stmt
*stmt
, struct pet_expr
*expr
,
3164 __isl_take isl_id
*label
)
3166 struct pet_stmt
*ps
;
3167 SourceLocation loc
= stmt
->getLocStart();
3168 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3170 expr
= resolve_nested(expr
);
3171 ps
= pet_stmt_from_pet_expr(ctx
, line
, label
, n_stmt
++, expr
);
3172 return pet_scop_from_pet_stmt(ctx
, ps
);
3175 /* Check if we can extract an affine expression from "expr".
3176 * Return the expressions as an isl_pw_aff if we can and NULL otherwise.
3177 * We turn on autodetection so that we won't generate any warnings
3178 * and turn off nesting, so that we won't accept any non-affine constructs.
3180 __isl_give isl_pw_aff
*PetScan::try_extract_affine(Expr
*expr
)
3183 int save_autodetect
= options
->autodetect
;
3184 bool save_nesting
= nesting_enabled
;
3186 options
->autodetect
= 1;
3187 nesting_enabled
= false;
3189 pwaff
= extract_affine(expr
);
3191 options
->autodetect
= save_autodetect
;
3192 nesting_enabled
= save_nesting
;
3197 /* Check whether "expr" is an affine expression.
3199 bool PetScan::is_affine(Expr
*expr
)
3203 pwaff
= try_extract_affine(expr
);
3204 isl_pw_aff_free(pwaff
);
3206 return pwaff
!= NULL
;
3209 /* Check if we can extract an affine constraint from "expr".
3210 * Return the constraint as an isl_set if we can and NULL otherwise.
3211 * We turn on autodetection so that we won't generate any warnings
3212 * and turn off nesting, so that we won't accept any non-affine constructs.
3214 __isl_give isl_pw_aff
*PetScan::try_extract_affine_condition(Expr
*expr
)
3217 int save_autodetect
= options
->autodetect
;
3218 bool save_nesting
= nesting_enabled
;
3220 options
->autodetect
= 1;
3221 nesting_enabled
= false;
3223 cond
= extract_condition(expr
);
3225 options
->autodetect
= save_autodetect
;
3226 nesting_enabled
= save_nesting
;
3231 /* Check whether "expr" is an affine constraint.
3233 bool PetScan::is_affine_condition(Expr
*expr
)
3237 cond
= try_extract_affine_condition(expr
);
3238 isl_pw_aff_free(cond
);
3240 return cond
!= NULL
;
3243 /* Check if we can extract a condition from "expr".
3244 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
3245 * If allow_nested is set, then the condition may involve parameters
3246 * corresponding to nested accesses.
3247 * We turn on autodetection so that we won't generate any warnings.
3249 __isl_give isl_pw_aff
*PetScan::try_extract_nested_condition(Expr
*expr
)
3252 int save_autodetect
= options
->autodetect
;
3253 bool save_nesting
= nesting_enabled
;
3255 options
->autodetect
= 1;
3256 nesting_enabled
= allow_nested
;
3257 cond
= extract_condition(expr
);
3259 options
->autodetect
= save_autodetect
;
3260 nesting_enabled
= save_nesting
;
3265 /* If the top-level expression of "stmt" is an assignment, then
3266 * return that assignment as a BinaryOperator.
3267 * Otherwise return NULL.
3269 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
3271 BinaryOperator
*ass
;
3275 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
3278 ass
= cast
<BinaryOperator
>(stmt
);
3279 if(ass
->getOpcode() != BO_Assign
)
3285 /* Check if the given if statement is a conditional assignement
3286 * with a non-affine condition. If so, construct a pet_scop
3287 * corresponding to this conditional assignment. Otherwise return NULL.
3289 * In particular we check if "stmt" is of the form
3296 * where a is some array or scalar access.
3297 * The constructed pet_scop then corresponds to the expression
3299 * a = condition ? f(...) : g(...)
3301 * All access relations in f(...) are intersected with condition
3302 * while all access relation in g(...) are intersected with the complement.
3304 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
)
3306 BinaryOperator
*ass_then
, *ass_else
;
3307 isl_map
*write_then
, *write_else
;
3308 isl_set
*cond
, *comp
;
3312 struct pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
3313 bool save_nesting
= nesting_enabled
;
3315 if (!options
->detect_conditional_assignment
)
3318 ass_then
= top_assignment_or_null(stmt
->getThen());
3319 ass_else
= top_assignment_or_null(stmt
->getElse());
3321 if (!ass_then
|| !ass_else
)
3324 if (is_affine_condition(stmt
->getCond()))
3327 write_then
= extract_access(ass_then
->getLHS());
3328 write_else
= extract_access(ass_else
->getLHS());
3330 equal
= isl_map_is_equal(write_then
, write_else
);
3331 isl_map_free(write_else
);
3332 if (equal
< 0 || !equal
) {
3333 isl_map_free(write_then
);
3337 nesting_enabled
= allow_nested
;
3338 pa
= extract_condition(stmt
->getCond());
3339 nesting_enabled
= save_nesting
;
3340 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
3341 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
3342 map
= isl_map_from_range(isl_set_from_pw_aff(pa
));
3344 pe_cond
= pet_expr_from_access(map
);
3346 pe_then
= extract_expr(ass_then
->getRHS());
3347 pe_then
= pet_expr_restrict(pe_then
, cond
);
3348 pe_else
= extract_expr(ass_else
->getRHS());
3349 pe_else
= pet_expr_restrict(pe_else
, comp
);
3351 pe
= pet_expr_new_ternary(ctx
, pe_cond
, pe_then
, pe_else
);
3352 pe_write
= pet_expr_from_access(write_then
);
3354 pe_write
->acc
.write
= 1;
3355 pe_write
->acc
.read
= 0;
3357 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, pe_write
, pe
);
3358 return extract(stmt
, pe
);
3361 /* Create a pet_scop with a single statement evaluating "cond"
3362 * and writing the result to a virtual scalar, as expressed by
3365 struct pet_scop
*PetScan::extract_non_affine_condition(Expr
*cond
,
3366 __isl_take isl_map
*access
)
3368 struct pet_expr
*expr
, *write
;
3369 struct pet_stmt
*ps
;
3370 struct pet_scop
*scop
;
3371 SourceLocation loc
= cond
->getLocStart();
3372 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3374 write
= pet_expr_from_access(access
);
3376 write
->acc
.write
= 1;
3377 write
->acc
.read
= 0;
3379 expr
= extract_expr(cond
);
3380 expr
= resolve_nested(expr
);
3381 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, write
, expr
);
3382 ps
= pet_stmt_from_pet_expr(ctx
, line
, NULL
, n_stmt
++, expr
);
3383 scop
= pet_scop_from_pet_stmt(ctx
, ps
);
3384 scop
= resolve_nested(scop
);
3390 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
3394 /* Apply the map pointed to by "user" to the domain of the access
3395 * relation, thereby embedding it in the range of the map.
3396 * The domain of both relations is the zero-dimensional domain.
3398 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
, void *user
)
3400 isl_map
*map
= (isl_map
*) user
;
3402 return isl_map_apply_domain(access
, isl_map_copy(map
));
3405 /* Apply "map" to all access relations in "expr".
3407 static struct pet_expr
*embed(struct pet_expr
*expr
, __isl_keep isl_map
*map
)
3409 return pet_expr_foreach_access(expr
, &embed_access
, map
);
3412 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
3414 static int n_nested_parameter(__isl_keep isl_set
*set
)
3419 space
= isl_set_get_space(set
);
3420 n
= n_nested_parameter(space
);
3421 isl_space_free(space
);
3426 /* Remove all parameters from "map" that refer to nested accesses.
3428 static __isl_give isl_map
*remove_nested_parameters(__isl_take isl_map
*map
)
3433 space
= isl_map_get_space(map
);
3434 nparam
= isl_space_dim(space
, isl_dim_param
);
3435 for (int i
= nparam
- 1; i
>= 0; --i
)
3436 if (is_nested_parameter(space
, i
))
3437 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3438 isl_space_free(space
);
3444 static __isl_give isl_map
*access_remove_nested_parameters(
3445 __isl_take isl_map
*access
, void *user
);
3448 static __isl_give isl_map
*access_remove_nested_parameters(
3449 __isl_take isl_map
*access
, void *user
)
3451 return remove_nested_parameters(access
);
3454 /* Remove all nested access parameters from the schedule and all
3455 * accesses of "stmt".
3456 * There is no need to remove them from the domain as these parameters
3457 * have already been removed from the domain when this function is called.
3459 static struct pet_stmt
*remove_nested_parameters(struct pet_stmt
*stmt
)
3463 stmt
->schedule
= remove_nested_parameters(stmt
->schedule
);
3464 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
3465 &access_remove_nested_parameters
, NULL
);
3466 if (!stmt
->schedule
|| !stmt
->body
)
3468 for (int i
= 0; i
< stmt
->n_arg
; ++i
) {
3469 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
3470 &access_remove_nested_parameters
, NULL
);
3477 pet_stmt_free(stmt
);
3481 /* For each nested access parameter in the domain of "stmt",
3482 * construct a corresponding pet_expr, place it before the original
3483 * elements in stmt->args and record its position in "param2pos".
3484 * n is the number of nested access parameters.
3486 struct pet_stmt
*PetScan::extract_nested(struct pet_stmt
*stmt
, int n
,
3487 std::map
<int,int> ¶m2pos
)
3492 struct pet_expr
**args
;
3494 n_arg
= stmt
->n_arg
;
3495 args
= isl_calloc_array(ctx
, struct pet_expr
*, n
+ n_arg
);
3499 space
= isl_set_get_space(stmt
->domain
);
3500 n_arg
= extract_nested(space
, 0, args
, param2pos
);
3501 isl_space_free(space
);
3506 for (i
= 0; i
< stmt
->n_arg
; ++i
)
3507 args
[n_arg
+ i
] = stmt
->args
[i
];
3510 stmt
->n_arg
+= n_arg
;
3515 for (i
= 0; i
< n
; ++i
)
3516 pet_expr_free(args
[i
]);
3519 pet_stmt_free(stmt
);
3523 /* Check whether any of the arguments i of "stmt" starting at position "n"
3524 * is equal to one of the first "n" arguments j.
3525 * If so, combine the constraints on arguments i and j and remove
3528 static struct pet_stmt
*remove_duplicate_arguments(struct pet_stmt
*stmt
, int n
)
3537 if (n
== stmt
->n_arg
)
3540 map
= isl_set_unwrap(stmt
->domain
);
3542 for (i
= stmt
->n_arg
- 1; i
>= n
; --i
) {
3543 for (j
= 0; j
< n
; ++j
)
3544 if (pet_expr_is_equal(stmt
->args
[i
], stmt
->args
[j
]))
3549 map
= isl_map_equate(map
, isl_dim_out
, i
, isl_dim_out
, j
);
3550 map
= isl_map_project_out(map
, isl_dim_out
, i
, 1);
3552 pet_expr_free(stmt
->args
[i
]);
3553 for (j
= i
; j
+ 1 < stmt
->n_arg
; ++j
)
3554 stmt
->args
[j
] = stmt
->args
[j
+ 1];
3558 stmt
->domain
= isl_map_wrap(map
);
3563 pet_stmt_free(stmt
);
3567 /* Look for parameters in the iteration domain of "stmt" that
3568 * refer to nested accesses. In particular, these are
3569 * parameters with no name.
3571 * If there are any such parameters, then as many extra variables
3572 * (after identifying identical nested accesses) are inserted in the
3573 * range of the map wrapped inside the domain, before the original variables.
3574 * If the original domain is not a wrapped map, then a new wrapped
3575 * map is created with zero output dimensions.
3576 * The parameters are then equated to the corresponding output dimensions
3577 * and subsequently projected out, from the iteration domain,
3578 * the schedule and the access relations.
3579 * For each of the output dimensions, a corresponding argument
3580 * expression is inserted. Initially they are created with
3581 * a zero-dimensional domain, so they have to be embedded
3582 * in the current iteration domain.
3583 * param2pos maps the position of the parameter to the position
3584 * of the corresponding output dimension in the wrapped map.
3586 struct pet_stmt
*PetScan::resolve_nested(struct pet_stmt
*stmt
)
3592 std::map
<int,int> param2pos
;
3597 n
= n_nested_parameter(stmt
->domain
);
3601 n_arg
= stmt
->n_arg
;
3602 stmt
= extract_nested(stmt
, n
, param2pos
);
3606 n
= stmt
->n_arg
- n_arg
;
3607 nparam
= isl_set_dim(stmt
->domain
, isl_dim_param
);
3608 if (isl_set_is_wrapping(stmt
->domain
))
3609 map
= isl_set_unwrap(stmt
->domain
);
3611 map
= isl_map_from_domain(stmt
->domain
);
3612 map
= isl_map_insert_dims(map
, isl_dim_out
, 0, n
);
3614 for (int i
= nparam
- 1; i
>= 0; --i
) {
3617 if (!is_nested_parameter(map
, i
))
3620 id
= isl_map_get_tuple_id(stmt
->args
[param2pos
[i
]]->acc
.access
,
3622 map
= isl_map_set_dim_id(map
, isl_dim_out
, param2pos
[i
], id
);
3623 map
= isl_map_equate(map
, isl_dim_param
, i
, isl_dim_out
,
3625 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3628 stmt
->domain
= isl_map_wrap(map
);
3630 map
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
3631 map
= isl_map_from_range(isl_map_domain(map
));
3632 for (int pos
= 0; pos
< n
; ++pos
)
3633 stmt
->args
[pos
] = embed(stmt
->args
[pos
], map
);
3636 stmt
= remove_nested_parameters(stmt
);
3637 stmt
= remove_duplicate_arguments(stmt
, n
);
3641 pet_stmt_free(stmt
);
3645 /* For each statement in "scop", move the parameters that correspond
3646 * to nested access into the ranges of the domains and create
3647 * corresponding argument expressions.
3649 struct pet_scop
*PetScan::resolve_nested(struct pet_scop
*scop
)
3654 for (int i
= 0; i
< scop
->n_stmt
; ++i
) {
3655 scop
->stmts
[i
] = resolve_nested(scop
->stmts
[i
]);
3656 if (!scop
->stmts
[i
])
3662 pet_scop_free(scop
);
3666 /* Given an access expression "expr", is the variable accessed by
3667 * "expr" assigned anywhere inside "scop"?
3669 static bool is_assigned(pet_expr
*expr
, pet_scop
*scop
)
3671 bool assigned
= false;
3674 id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
3675 assigned
= pet_scop_writes(scop
, id
);
3681 /* Are all nested access parameters in "pa" allowed given "scop".
3682 * In particular, is none of them written by anywhere inside "scop".
3684 * If "scop" has any skip conditions, then no nested access parameters
3685 * are allowed. In particular, if there is any nested access in a guard
3686 * for a piece of code containing a "continue", then we want to introduce
3687 * a separate statement for evaluating this guard so that we can express
3688 * that the result is false for all previous iterations.
3690 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff
*pa
, pet_scop
*scop
)
3697 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
3698 for (int i
= 0; i
< nparam
; ++i
) {
3700 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
3704 if (!is_nested_parameter(id
)) {
3709 if (pet_scop_has_skip(scop
, pet_skip_now
)) {
3714 nested
= (Expr
*) isl_id_get_user(id
);
3715 expr
= extract_expr(nested
);
3716 allowed
= expr
&& expr
->type
== pet_expr_access
&&
3717 !is_assigned(expr
, scop
);
3719 pet_expr_free(expr
);
3729 /* Do we need to construct a skip condition of the given type
3730 * on an if statement, given that the if condition is non-affine?
3732 * pet_scop_filter_skip can only handle the case where the if condition
3733 * holds (the then branch) and the skip condition is universal.
3734 * In any other case, we need to construct a new skip condition.
3736 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
3737 bool have_else
, enum pet_skip type
)
3739 if (have_else
&& scop_else
&& pet_scop_has_skip(scop_else
, type
))
3741 if (scop_then
&& pet_scop_has_skip(scop_then
, type
) &&
3742 !pet_scop_has_universal_skip(scop_then
, type
))
3747 /* Do we need to construct a skip condition of the given type
3748 * on an if statement, given that the if condition is affine?
3750 * There is no need to construct a new skip condition if all
3751 * the skip conditions are affine.
3753 static bool need_skip_aff(struct pet_scop
*scop_then
,
3754 struct pet_scop
*scop_else
, bool have_else
, enum pet_skip type
)
3756 if (scop_then
&& pet_scop_has_var_skip(scop_then
, type
))
3758 if (have_else
&& scop_else
&& pet_scop_has_var_skip(scop_else
, type
))
3763 /* Do we need to construct a skip condition of the given type
3764 * on an if statement?
3766 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
3767 bool have_else
, enum pet_skip type
, bool affine
)
3770 return need_skip_aff(scop_then
, scop_else
, have_else
, type
);
3772 return need_skip(scop_then
, scop_else
, have_else
, type
);
3775 /* Construct an affine expression pet_expr that is evaluates
3776 * to the constant "val".
3778 static struct pet_expr
*universally(isl_ctx
*ctx
, int val
)
3783 space
= isl_space_alloc(ctx
, 0, 0, 1);
3784 map
= isl_map_universe(space
);
3785 map
= isl_map_fix_si(map
, isl_dim_out
, 0, val
);
3787 return pet_expr_from_access(map
);
3790 /* Construct an affine expression pet_expr that is evaluates
3791 * to the constant 1.
3793 static struct pet_expr
*universally_true(isl_ctx
*ctx
)
3795 return universally(ctx
, 1);
3798 /* Construct an affine expression pet_expr that is evaluates
3799 * to the constant 0.
3801 static struct pet_expr
*universally_false(isl_ctx
*ctx
)
3803 return universally(ctx
, 0);
3806 /* Given an access relation "test_access" for the if condition,
3807 * an access relation "skip_access" for the skip condition and
3808 * scops for the then and else branches, construct a scop for
3809 * computing "skip_access".
3811 * The computed scop contains a single statement that essentially does
3813 * skip_cond = test_cond ? skip_cond_then : skip_cond_else
3815 * If the skip conditions of the then and/or else branch are not affine,
3816 * then they need to be filtered by test_access.
3817 * If they are missing, then this means the skip condition is false.
3819 * Since we are constructing a skip condition for the if statement,
3820 * the skip conditions on the then and else branches are removed.
3822 static struct pet_scop
*extract_skip(PetScan
*scan
,
3823 __isl_take isl_map
*test_access
, __isl_take isl_map
*skip_access
,
3824 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
, bool have_else
,
3827 struct pet_expr
*expr_then
, *expr_else
, *expr
, *expr_skip
;
3828 struct pet_stmt
*stmt
;
3829 struct pet_scop
*scop
;
3830 isl_ctx
*ctx
= scan
->ctx
;
3834 if (have_else
&& !scop_else
)
3837 if (pet_scop_has_skip(scop_then
, type
)) {
3838 expr_then
= pet_scop_get_skip_expr(scop_then
, type
);
3839 pet_scop_reset_skip(scop_then
, type
);
3840 if (!pet_expr_is_affine(expr_then
))
3841 expr_then
= pet_expr_filter(expr_then
,
3842 isl_map_copy(test_access
), 1);
3844 expr_then
= universally_false(ctx
);
3846 if (have_else
&& pet_scop_has_skip(scop_else
, type
)) {
3847 expr_else
= pet_scop_get_skip_expr(scop_else
, type
);
3848 pet_scop_reset_skip(scop_else
, type
);
3849 if (!pet_expr_is_affine(expr_else
))
3850 expr_else
= pet_expr_filter(expr_else
,
3851 isl_map_copy(test_access
), 0);
3853 expr_else
= universally_false(ctx
);
3855 expr
= pet_expr_from_access(test_access
);
3856 expr
= pet_expr_new_ternary(ctx
, expr
, expr_then
, expr_else
);
3857 expr_skip
= pet_expr_from_access(isl_map_copy(skip_access
));
3859 expr_skip
->acc
.write
= 1;
3860 expr_skip
->acc
.read
= 0;
3862 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
3863 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, scan
->n_stmt
++, expr
);
3865 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
3866 scop
= scop_add_array(scop
, skip_access
, scan
->ast_context
);
3867 isl_map_free(skip_access
);
3871 isl_map_free(test_access
);
3872 isl_map_free(skip_access
);
3876 /* Is scop's skip_now condition equal to its skip_later condition?
3877 * In particular, this means that it either has no skip_now condition
3878 * or both a skip_now and a skip_later condition (that are equal to each other).
3880 static bool skip_equals_skip_later(struct pet_scop
*scop
)
3882 int has_skip_now
, has_skip_later
;
3884 isl_set
*skip_now
, *skip_later
;
3888 has_skip_now
= pet_scop_has_skip(scop
, pet_skip_now
);
3889 has_skip_later
= pet_scop_has_skip(scop
, pet_skip_later
);
3890 if (has_skip_now
!= has_skip_later
)
3895 skip_now
= pet_scop_get_skip(scop
, pet_skip_now
);
3896 skip_later
= pet_scop_get_skip(scop
, pet_skip_later
);
3897 equal
= isl_set_is_equal(skip_now
, skip_later
);
3898 isl_set_free(skip_now
);
3899 isl_set_free(skip_later
);
3904 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
3906 static void drop_skip_later(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
3908 pet_scop_reset_skip(scop1
, pet_skip_later
);
3909 pet_scop_reset_skip(scop2
, pet_skip_later
);
3912 /* Structure that handles the construction of skip conditions.
3914 * scop_then and scop_else represent the then and else branches
3915 * of the if statement
3917 * skip[type] is true if we need to construct a skip condition of that type
3918 * equal is set if the skip conditions of types pet_skip_now and pet_skip_later
3919 * are equal to each other
3920 * access[type] is the virtual array representing the skip condition
3921 * scop[type] is a scop for computing the skip condition
3923 struct pet_skip_info
{
3929 struct pet_scop
*scop
[2];
3931 pet_skip_info(isl_ctx
*ctx
) : ctx(ctx
) {}
3933 operator bool() { return skip
[pet_skip_now
] || skip
[pet_skip_later
]; }
3936 /* Structure that handles the construction of skip conditions on if statements.
3938 * scop_then and scop_else represent the then and else branches
3939 * of the if statement
3941 struct pet_skip_info_if
: public pet_skip_info
{
3942 struct pet_scop
*scop_then
, *scop_else
;
3945 pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
3946 struct pet_scop
*scop_else
, bool have_else
, bool affine
);
3947 void extract(PetScan
*scan
, __isl_keep isl_map
*access
,
3948 enum pet_skip type
);
3949 void extract(PetScan
*scan
, __isl_keep isl_map
*access
);
3950 void extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
);
3951 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
3953 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
3956 /* Initialize a pet_skip_info_if structure based on the then and else branches
3957 * and based on whether the if condition is affine or not.
3959 pet_skip_info_if::pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
3960 struct pet_scop
*scop_else
, bool have_else
, bool affine
) :
3961 pet_skip_info(ctx
), scop_then(scop_then
), scop_else(scop_else
),
3962 have_else(have_else
)
3964 skip
[pet_skip_now
] =
3965 need_skip(scop_then
, scop_else
, have_else
, pet_skip_now
, affine
);
3966 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop_then
) &&
3967 (!have_else
|| skip_equals_skip_later(scop_else
));
3968 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
3969 need_skip(scop_then
, scop_else
, have_else
, pet_skip_later
, affine
);
3972 /* If we need to construct a skip condition of the given type,
3975 * "map" represents the if condition.
3977 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_map
*map
,
3983 access
[type
] = create_test_access(isl_map_get_ctx(map
), scan
->n_test
++);
3984 scop
[type
] = extract_skip(scan
, isl_map_copy(map
),
3985 isl_map_copy(access
[type
]),
3986 scop_then
, scop_else
, have_else
, type
);
3989 /* Construct the required skip conditions, given the if condition "map".
3991 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_map
*map
)
3993 extract(scan
, map
, pet_skip_now
);
3994 extract(scan
, map
, pet_skip_later
);
3996 drop_skip_later(scop_then
, scop_else
);
3999 /* Construct the required skip conditions, given the if condition "cond".
4001 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
)
4006 if (!skip
[pet_skip_now
] && !skip
[pet_skip_later
])
4009 test_set
= isl_set_from_pw_aff(isl_pw_aff_copy(cond
));
4010 test
= isl_map_from_range(test_set
);
4011 extract(scan
, test
);
4015 /* Add the computed skip condition of the give type to "main" and
4016 * add the scop for computing the condition at the given offset.
4018 * If equal is set, then we only computed a skip condition for pet_skip_now,
4019 * but we also need to set it as main's pet_skip_later.
4021 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*main
,
4022 enum pet_skip type
, int offset
)
4029 skip_set
= isl_map_range(access
[type
]);
4030 access
[type
] = NULL
;
4031 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4032 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4036 main
= pet_scop_set_skip(main
, pet_skip_later
,
4037 isl_set_copy(skip_set
));
4039 main
= pet_scop_set_skip(main
, type
, skip_set
);
4044 /* Add the computed skip conditions to "main" and
4045 * add the scops for computing the conditions at the given offset.
4047 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*scop
, int offset
)
4049 scop
= add(scop
, pet_skip_now
, offset
);
4050 scop
= add(scop
, pet_skip_later
, offset
);
4055 /* Construct a pet_scop for a non-affine if statement.
4057 * We create a separate statement that writes the result
4058 * of the non-affine condition to a virtual scalar.
4059 * A constraint requiring the value of this virtual scalar to be one
4060 * is added to the iteration domains of the then branch.
4061 * Similarly, a constraint requiring the value of this virtual scalar
4062 * to be zero is added to the iteration domains of the else branch, if any.
4063 * We adjust the schedules to ensure that the virtual scalar is written
4064 * before it is read.
4066 * If there are any breaks or continues in the then and/or else
4067 * branches, then we may have to compute a new skip condition.
4068 * This is handled using a pet_skip_info_if object.
4069 * On initialization, the object checks if skip conditions need
4070 * to be computed. If so, it does so in "extract" and adds them in "add".
4072 struct pet_scop
*PetScan::extract_non_affine_if(Expr
*cond
,
4073 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
4074 bool have_else
, int stmt_id
)
4076 struct pet_scop
*scop
;
4077 isl_map
*test_access
;
4078 int save_n_stmt
= n_stmt
;
4080 test_access
= create_test_access(ctx
, n_test
++);
4082 scop
= extract_non_affine_condition(cond
, isl_map_copy(test_access
));
4083 n_stmt
= save_n_stmt
;
4084 scop
= scop_add_array(scop
, test_access
, ast_context
);
4086 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, have_else
, false);
4087 skip
.extract(this, test_access
);
4089 scop
= pet_scop_prefix(scop
, 0);
4090 scop_then
= pet_scop_prefix(scop_then
, 1);
4091 scop_then
= pet_scop_filter(scop_then
, isl_map_copy(test_access
), 1);
4093 scop_else
= pet_scop_prefix(scop_else
, 1);
4094 scop_else
= pet_scop_filter(scop_else
, test_access
, 0);
4095 scop_then
= pet_scop_add_par(ctx
, scop_then
, scop_else
);
4097 isl_map_free(test_access
);
4099 scop
= pet_scop_add_seq(ctx
, scop
, scop_then
);
4101 scop
= skip
.add(scop
, 2);
4106 /* Construct a pet_scop for an if statement.
4108 * If the condition fits the pattern of a conditional assignment,
4109 * then it is handled by extract_conditional_assignment.
4110 * Otherwise, we do the following.
4112 * If the condition is affine, then the condition is added
4113 * to the iteration domains of the then branch, while the
4114 * opposite of the condition in added to the iteration domains
4115 * of the else branch, if any.
4116 * We allow the condition to be dynamic, i.e., to refer to
4117 * scalars or array elements that may be written to outside
4118 * of the given if statement. These nested accesses are then represented
4119 * as output dimensions in the wrapping iteration domain.
4120 * If it also written _inside_ the then or else branch, then
4121 * we treat the condition as non-affine.
4122 * As explained in extract_non_affine_if, this will introduce
4123 * an extra statement.
4124 * For aesthetic reasons, we want this statement to have a statement
4125 * number that is lower than those of the then and else branches.
4126 * In order to evaluate if will need such a statement, however, we
4127 * first construct scops for the then and else branches.
4128 * We therefore reserve a statement number if we might have to
4129 * introduce such an extra statement.
4131 * If the condition is not affine, then the scop is created in
4132 * extract_non_affine_if.
4134 * If there are any breaks or continues in the then and/or else
4135 * branches, then we may have to compute a new skip condition.
4136 * This is handled using a pet_skip_info_if object.
4137 * On initialization, the object checks if skip conditions need
4138 * to be computed. If so, it does so in "extract" and adds them in "add".
4140 struct pet_scop
*PetScan::extract(IfStmt
*stmt
)
4142 struct pet_scop
*scop_then
, *scop_else
= NULL
, *scop
;
4148 scop
= extract_conditional_assignment(stmt
);
4152 cond
= try_extract_nested_condition(stmt
->getCond());
4153 if (allow_nested
&& (!cond
|| has_nested(cond
)))
4157 assigned_value_cache
cache(assigned_value
);
4158 scop_then
= extract(stmt
->getThen());
4161 if (stmt
->getElse()) {
4162 assigned_value_cache
cache(assigned_value
);
4163 scop_else
= extract(stmt
->getElse());
4164 if (options
->autodetect
) {
4165 if (scop_then
&& !scop_else
) {
4167 isl_pw_aff_free(cond
);
4170 if (!scop_then
&& scop_else
) {
4172 isl_pw_aff_free(cond
);
4179 (!is_nested_allowed(cond
, scop_then
) ||
4180 (stmt
->getElse() && !is_nested_allowed(cond
, scop_else
)))) {
4181 isl_pw_aff_free(cond
);
4184 if (allow_nested
&& !cond
)
4185 return extract_non_affine_if(stmt
->getCond(), scop_then
,
4186 scop_else
, stmt
->getElse(), stmt_id
);
4189 cond
= extract_condition(stmt
->getCond());
4191 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, stmt
->getElse(), true);
4192 skip
.extract(this, cond
);
4194 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
4195 set
= isl_pw_aff_non_zero_set(cond
);
4196 scop
= pet_scop_restrict(scop_then
, isl_set_copy(set
));
4198 if (stmt
->getElse()) {
4199 set
= isl_set_subtract(isl_set_copy(valid
), set
);
4200 scop_else
= pet_scop_restrict(scop_else
, set
);
4201 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
4204 scop
= resolve_nested(scop
);
4205 scop
= pet_scop_restrict_context(scop
, valid
);
4208 scop
= pet_scop_prefix(scop
, 0);
4209 scop
= skip
.add(scop
, 1);
4214 /* Try and construct a pet_scop for a label statement.
4215 * We currently only allow labels on expression statements.
4217 struct pet_scop
*PetScan::extract(LabelStmt
*stmt
)
4222 sub
= stmt
->getSubStmt();
4223 if (!isa
<Expr
>(sub
)) {
4228 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
4230 return extract(sub
, extract_expr(cast
<Expr
>(sub
)), label
);
4233 /* Construct a pet_scop for a continue statement.
4235 * We simply create an empty scop with a universal pet_skip_now
4236 * skip condition. This skip condition will then be taken into
4237 * account by the enclosing loop construct, possibly after
4238 * being incorporated into outer skip conditions.
4240 struct pet_scop
*PetScan::extract(ContinueStmt
*stmt
)
4246 scop
= pet_scop_empty(ctx
);
4250 space
= isl_space_set_alloc(ctx
, 0, 1);
4251 set
= isl_set_universe(space
);
4252 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
4253 scop
= pet_scop_set_skip(scop
, pet_skip_now
, set
);
4258 /* Construct a pet_scop for a break statement.
4260 * We simply create an empty scop with both a universal pet_skip_now
4261 * skip condition and a universal pet_skip_later skip condition.
4262 * These skip conditions will then be taken into
4263 * account by the enclosing loop construct, possibly after
4264 * being incorporated into outer skip conditions.
4266 struct pet_scop
*PetScan::extract(BreakStmt
*stmt
)
4272 scop
= pet_scop_empty(ctx
);
4276 space
= isl_space_set_alloc(ctx
, 0, 1);
4277 set
= isl_set_universe(space
);
4278 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
4279 scop
= pet_scop_set_skip(scop
, pet_skip_now
, isl_set_copy(set
));
4280 scop
= pet_scop_set_skip(scop
, pet_skip_later
, set
);
4285 /* Try and construct a pet_scop corresponding to "stmt".
4287 * If "stmt" is a compound statement, then "skip_declarations"
4288 * indicates whether we should skip initial declarations in the
4289 * compound statement.
4291 struct pet_scop
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
4293 if (isa
<Expr
>(stmt
))
4294 return extract(stmt
, extract_expr(cast
<Expr
>(stmt
)));
4296 switch (stmt
->getStmtClass()) {
4297 case Stmt::WhileStmtClass
:
4298 return extract(cast
<WhileStmt
>(stmt
));
4299 case Stmt::ForStmtClass
:
4300 return extract_for(cast
<ForStmt
>(stmt
));
4301 case Stmt::IfStmtClass
:
4302 return extract(cast
<IfStmt
>(stmt
));
4303 case Stmt::CompoundStmtClass
:
4304 return extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
4305 case Stmt::LabelStmtClass
:
4306 return extract(cast
<LabelStmt
>(stmt
));
4307 case Stmt::ContinueStmtClass
:
4308 return extract(cast
<ContinueStmt
>(stmt
));
4309 case Stmt::BreakStmtClass
:
4310 return extract(cast
<BreakStmt
>(stmt
));
4311 case Stmt::DeclStmtClass
:
4312 return extract(cast
<DeclStmt
>(stmt
));
4320 /* Do we need to construct a skip condition of the given type
4321 * on a sequence of statements?
4323 * There is no need to construct a new skip condition if only
4324 * only of the two statements has a skip condition or if both
4325 * of their skip conditions are affine.
4327 * In principle we also don't need a new continuation variable if
4328 * the continuation of scop2 is affine, but then we would need
4329 * to allow more complicated forms of continuations.
4331 static bool need_skip_seq(struct pet_scop
*scop1
, struct pet_scop
*scop2
,
4334 if (!scop1
|| !pet_scop_has_skip(scop1
, type
))
4336 if (!scop2
|| !pet_scop_has_skip(scop2
, type
))
4338 if (pet_scop_has_affine_skip(scop1
, type
) &&
4339 pet_scop_has_affine_skip(scop2
, type
))
4344 /* Construct a scop for computing the skip condition of the given type and
4345 * with access relation "skip_access" for a sequence of two scops "scop1"
4348 * The computed scop contains a single statement that essentially does
4350 * skip_cond = skip_cond_1 ? 1 : skip_cond_2
4352 * or, in other words, skip_cond1 || skip_cond2.
4353 * In this expression, skip_cond_2 is filtered to reflect that it is
4354 * only evaluated when skip_cond_1 is false.
4356 * The skip condition on scop1 is not removed because it still needs
4357 * to be applied to scop2 when these two scops are combined.
4359 static struct pet_scop
*extract_skip_seq(PetScan
*ps
,
4360 __isl_take isl_map
*skip_access
,
4361 struct pet_scop
*scop1
, struct pet_scop
*scop2
, enum pet_skip type
)
4364 struct pet_expr
*expr1
, *expr2
, *expr
, *expr_skip
;
4365 struct pet_stmt
*stmt
;
4366 struct pet_scop
*scop
;
4367 isl_ctx
*ctx
= ps
->ctx
;
4369 if (!scop1
|| !scop2
)
4372 expr1
= pet_scop_get_skip_expr(scop1
, type
);
4373 expr2
= pet_scop_get_skip_expr(scop2
, type
);
4374 pet_scop_reset_skip(scop2
, type
);
4376 expr2
= pet_expr_filter(expr2
, isl_map_copy(expr1
->acc
.access
), 0);
4378 expr
= universally_true(ctx
);
4379 expr
= pet_expr_new_ternary(ctx
, expr1
, expr
, expr2
);
4380 expr_skip
= pet_expr_from_access(isl_map_copy(skip_access
));
4382 expr_skip
->acc
.write
= 1;
4383 expr_skip
->acc
.read
= 0;
4385 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
4386 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, ps
->n_stmt
++, expr
);
4388 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
4389 scop
= scop_add_array(scop
, skip_access
, ps
->ast_context
);
4390 isl_map_free(skip_access
);
4394 isl_map_free(skip_access
);
4398 /* Structure that handles the construction of skip conditions
4399 * on sequences of statements.
4401 * scop1 and scop2 represent the two statements that are combined
4403 struct pet_skip_info_seq
: public pet_skip_info
{
4404 struct pet_scop
*scop1
, *scop2
;
4406 pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4407 struct pet_scop
*scop2
);
4408 void extract(PetScan
*scan
, enum pet_skip type
);
4409 void extract(PetScan
*scan
);
4410 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
4412 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
4415 /* Initialize a pet_skip_info_seq structure based on
4416 * on the two statements that are going to be combined.
4418 pet_skip_info_seq::pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4419 struct pet_scop
*scop2
) : pet_skip_info(ctx
), scop1(scop1
), scop2(scop2
)
4421 skip
[pet_skip_now
] = need_skip_seq(scop1
, scop2
, pet_skip_now
);
4422 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop1
) &&
4423 skip_equals_skip_later(scop2
);
4424 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
4425 need_skip_seq(scop1
, scop2
, pet_skip_later
);
4428 /* If we need to construct a skip condition of the given type,
4431 void pet_skip_info_seq::extract(PetScan
*scan
, enum pet_skip type
)
4436 access
[type
] = create_test_access(ctx
, scan
->n_test
++);
4437 scop
[type
] = extract_skip_seq(scan
, isl_map_copy(access
[type
]),
4438 scop1
, scop2
, type
);
4441 /* Construct the required skip conditions.
4443 void pet_skip_info_seq::extract(PetScan
*scan
)
4445 extract(scan
, pet_skip_now
);
4446 extract(scan
, pet_skip_later
);
4448 drop_skip_later(scop1
, scop2
);
4451 /* Add the computed skip condition of the give type to "main" and
4452 * add the scop for computing the condition at the given offset (the statement
4453 * number). Within this offset, the condition is computed at position 1
4454 * to ensure that it is computed after the corresponding statement.
4456 * If equal is set, then we only computed a skip condition for pet_skip_now,
4457 * but we also need to set it as main's pet_skip_later.
4459 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*main
,
4460 enum pet_skip type
, int offset
)
4467 skip_set
= isl_map_range(access
[type
]);
4468 access
[type
] = NULL
;
4469 scop
[type
] = pet_scop_prefix(scop
[type
], 1);
4470 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4471 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4475 main
= pet_scop_set_skip(main
, pet_skip_later
,
4476 isl_set_copy(skip_set
));
4478 main
= pet_scop_set_skip(main
, type
, skip_set
);
4483 /* Add the computed skip conditions to "main" and
4484 * add the scops for computing the conditions at the given offset.
4486 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*scop
, int offset
)
4488 scop
= add(scop
, pet_skip_now
, offset
);
4489 scop
= add(scop
, pet_skip_later
, offset
);
4494 /* Extract a clone of the kill statement in "scop".
4495 * "scop" is expected to have been created from a DeclStmt
4496 * and should have the kill as its first statement.
4498 struct pet_stmt
*PetScan::extract_kill(struct pet_scop
*scop
)
4500 struct pet_expr
*kill
;
4501 struct pet_stmt
*stmt
;
4506 if (scop
->n_stmt
< 1)
4507 isl_die(ctx
, isl_error_internal
,
4508 "expecting at least one statement", return NULL
);
4509 stmt
= scop
->stmts
[0];
4510 if (stmt
->body
->type
!= pet_expr_unary
||
4511 stmt
->body
->op
!= pet_op_kill
)
4512 isl_die(ctx
, isl_error_internal
,
4513 "expecting kill statement", return NULL
);
4515 access
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
4516 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
4517 kill
= pet_expr_kill_from_access(access
);
4518 return pet_stmt_from_pet_expr(ctx
, stmt
->line
, NULL
, n_stmt
++, kill
);
4521 /* Mark all arrays in "scop" as being exposed.
4523 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
4527 for (int i
= 0; i
< scop
->n_array
; ++i
)
4528 scop
->arrays
[i
]->exposed
= 1;
4532 /* Try and construct a pet_scop corresponding to (part of)
4533 * a sequence of statements.
4535 * "block" is set if the sequence respresents the children of
4536 * a compound statement.
4537 * "skip_declarations" is set if we should skip initial declarations
4538 * in the sequence of statements.
4540 * If there are any breaks or continues in the individual statements,
4541 * then we may have to compute a new skip condition.
4542 * This is handled using a pet_skip_info_seq object.
4543 * On initialization, the object checks if skip conditions need
4544 * to be computed. If so, it does so in "extract" and adds them in "add".
4546 * If "block" is set, then we need to insert kill statements at
4547 * the end of the block for any array that has been declared by
4548 * one of the statements in the sequence. Each of these declarations
4549 * results in the construction of a kill statement at the place
4550 * of the declaration, so we simply collect duplicates of
4551 * those kill statements and append these duplicates to the constructed scop.
4553 * If "block" is not set, then any array declared by one of the statements
4554 * in the sequence is marked as being exposed.
4556 struct pet_scop
*PetScan::extract(StmtRange stmt_range
, bool block
,
4557 bool skip_declarations
)
4562 bool partial_range
= false;
4563 set
<struct pet_stmt
*> kills
;
4564 set
<struct pet_stmt
*>::iterator it
;
4566 scop
= pet_scop_empty(ctx
);
4567 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
4569 struct pet_scop
*scop_i
;
4571 if (skip_declarations
&&
4572 child
->getStmtClass() == Stmt::DeclStmtClass
)
4575 scop_i
= extract(child
);
4576 if (scop
&& partial
) {
4577 pet_scop_free(scop_i
);
4580 pet_skip_info_seq
skip(ctx
, scop
, scop_i
);
4583 scop_i
= pet_scop_prefix(scop_i
, 0);
4584 if (scop_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
) {
4586 kills
.insert(extract_kill(scop_i
));
4588 scop_i
= mark_exposed(scop_i
);
4590 scop_i
= pet_scop_prefix(scop_i
, j
);
4591 if (options
->autodetect
) {
4593 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
4595 partial_range
= true;
4596 if (scop
->n_stmt
!= 0 && !scop_i
)
4599 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
4602 scop
= skip
.add(scop
, j
);
4608 for (it
= kills
.begin(); it
!= kills
.end(); ++it
) {
4610 scop_j
= pet_scop_from_pet_stmt(ctx
, *it
);
4611 scop_j
= pet_scop_prefix(scop_j
, j
);
4612 scop
= pet_scop_add_seq(ctx
, scop
, scop_j
);
4615 if (scop
&& partial_range
)
4621 /* Return the file offset of the expansion location of "Loc".
4623 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
4625 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
4628 /* Check if the scop marked by the user is exactly this Stmt
4629 * or part of this Stmt.
4630 * If so, return a pet_scop corresponding to the marked region.
4631 * Otherwise, return NULL.
4633 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
4635 SourceManager
&SM
= PP
.getSourceManager();
4636 unsigned start_off
, end_off
;
4638 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
4639 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
4641 if (start_off
> loc
.end
)
4643 if (end_off
< loc
.start
)
4645 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
4646 return extract(stmt
);
4650 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
4651 Stmt
*child
= *start
;
4654 start_off
= getExpansionOffset(SM
, child
->getLocStart());
4655 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
4656 if (start_off
< loc
.start
&& end_off
> loc
.end
)
4658 if (start_off
>= loc
.start
)
4663 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
4665 start_off
= SM
.getFileOffset(child
->getLocStart());
4666 if (start_off
>= loc
.end
)
4670 return extract(StmtRange(start
, end
), false, false);
4673 /* Set the size of index "pos" of "array" to "size".
4674 * In particular, add a constraint of the form
4678 * to array->extent and a constraint of the form
4682 * to array->context.
4684 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
4685 __isl_take isl_pw_aff
*size
)
4695 valid
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
));
4696 array
->context
= isl_set_intersect(array
->context
, valid
);
4698 dim
= isl_set_get_space(array
->extent
);
4699 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
4700 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
4701 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
4702 index
= isl_pw_aff_alloc(univ
, aff
);
4704 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
4705 isl_set_dim(array
->extent
, isl_dim_set
));
4706 id
= isl_set_get_tuple_id(array
->extent
);
4707 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
4708 bound
= isl_pw_aff_lt_set(index
, size
);
4710 array
->extent
= isl_set_intersect(array
->extent
, bound
);
4712 if (!array
->context
|| !array
->extent
)
4717 pet_array_free(array
);
4721 /* Figure out the size of the array at position "pos" and all
4722 * subsequent positions from "type" and update "array" accordingly.
4724 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
4725 const Type
*type
, int pos
)
4727 const ArrayType
*atype
;
4733 if (type
->isPointerType()) {
4734 type
= type
->getPointeeType().getTypePtr();
4735 return set_upper_bounds(array
, type
, pos
+ 1);
4737 if (!type
->isArrayType())
4740 type
= type
->getCanonicalTypeInternal().getTypePtr();
4741 atype
= cast
<ArrayType
>(type
);
4743 if (type
->isConstantArrayType()) {
4744 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
4745 size
= extract_affine(ca
->getSize());
4746 array
= update_size(array
, pos
, size
);
4747 } else if (type
->isVariableArrayType()) {
4748 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
4749 size
= extract_affine(vla
->getSizeExpr());
4750 array
= update_size(array
, pos
, size
);
4753 type
= atype
->getElementType().getTypePtr();
4755 return set_upper_bounds(array
, type
, pos
+ 1);
4758 /* Is "T" the type of a variable length array with static size?
4760 static bool is_vla_with_static_size(QualType T
)
4762 const VariableArrayType
*vlatype
;
4764 if (!T
->isVariableArrayType())
4766 vlatype
= cast
<VariableArrayType
>(T
);
4767 return vlatype
->getSizeModifier() == VariableArrayType::Static
;
4770 /* Return the type of "decl" as an array.
4772 * In particular, if "decl" is a parameter declaration that
4773 * is a variable length array with a static size, then
4774 * return the original type (i.e., the variable length array).
4775 * Otherwise, return the type of decl.
4777 static QualType
get_array_type(ValueDecl
*decl
)
4782 parm
= dyn_cast
<ParmVarDecl
>(decl
);
4784 return decl
->getType();
4786 T
= parm
->getOriginalType();
4787 if (!is_vla_with_static_size(T
))
4788 return decl
->getType();
4792 /* Construct and return a pet_array corresponding to the variable "decl".
4793 * In particular, initialize array->extent to
4795 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
4797 * and then call set_upper_bounds to set the upper bounds on the indices
4798 * based on the type of the variable.
4800 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
)
4802 struct pet_array
*array
;
4803 QualType qt
= get_array_type(decl
);
4804 const Type
*type
= qt
.getTypePtr();
4805 int depth
= array_depth(type
);
4806 QualType base
= base_type(qt
);
4811 array
= isl_calloc_type(ctx
, struct pet_array
);
4815 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
4816 dim
= isl_space_set_alloc(ctx
, 0, depth
);
4817 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
4819 array
->extent
= isl_set_nat_universe(dim
);
4821 dim
= isl_space_params_alloc(ctx
, 0);
4822 array
->context
= isl_set_universe(dim
);
4824 array
= set_upper_bounds(array
, type
, 0);
4828 name
= base
.getAsString();
4829 array
->element_type
= strdup(name
.c_str());
4830 array
->element_size
= decl
->getASTContext().getTypeInfo(base
).first
/ 8;
4835 /* Construct a list of pet_arrays, one for each array (or scalar)
4836 * accessed inside "scop", add this list to "scop" and return the result.
4838 * The context of "scop" is updated with the intersection of
4839 * the contexts of all arrays, i.e., constraints on the parameters
4840 * that ensure that the arrays have a valid (non-negative) size.
4842 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
)
4845 set
<ValueDecl
*> arrays
;
4846 set
<ValueDecl
*>::iterator it
;
4848 struct pet_array
**scop_arrays
;
4853 pet_scop_collect_arrays(scop
, arrays
);
4854 if (arrays
.size() == 0)
4857 n_array
= scop
->n_array
;
4859 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
4860 n_array
+ arrays
.size());
4863 scop
->arrays
= scop_arrays
;
4865 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
4866 struct pet_array
*array
;
4867 scop
->arrays
[n_array
+ i
] = array
= extract_array(ctx
, *it
);
4868 if (!scop
->arrays
[n_array
+ i
])
4871 scop
->context
= isl_set_intersect(scop
->context
,
4872 isl_set_copy(array
->context
));
4879 pet_scop_free(scop
);
4883 /* Bound all parameters in scop->context to the possible values
4884 * of the corresponding C variable.
4886 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
4893 n
= isl_set_dim(scop
->context
, isl_dim_param
);
4894 for (int i
= 0; i
< n
; ++i
) {
4898 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
4899 if (is_nested_parameter(id
)) {
4901 isl_die(isl_set_get_ctx(scop
->context
),
4903 "unresolved nested parameter", goto error
);
4905 decl
= (ValueDecl
*) isl_id_get_user(id
);
4908 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
4916 pet_scop_free(scop
);
4920 /* Construct a pet_scop from the given function.
4922 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
4927 stmt
= fd
->getBody();
4929 if (options
->autodetect
)
4930 scop
= extract(stmt
, true);
4933 scop
= pet_scop_detect_parameter_accesses(scop
);
4934 scop
= scan_arrays(scop
);
4935 scop
= add_parameter_bounds(scop
);
4936 scop
= pet_scop_gist(scop
, value_bounds
);