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 /* Try and onstruct a pet_expr representing "expr".
1677 struct pet_expr
*PetScan::extract_expr(Expr
*expr
)
1679 switch (expr
->getStmtClass()) {
1680 case Stmt::UnaryOperatorClass
:
1681 return extract_expr(cast
<UnaryOperator
>(expr
));
1682 case Stmt::CompoundAssignOperatorClass
:
1683 case Stmt::BinaryOperatorClass
:
1684 return extract_expr(cast
<BinaryOperator
>(expr
));
1685 case Stmt::ImplicitCastExprClass
:
1686 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1687 case Stmt::ArraySubscriptExprClass
:
1688 case Stmt::DeclRefExprClass
:
1689 case Stmt::IntegerLiteralClass
:
1690 return extract_access_expr(expr
);
1691 case Stmt::FloatingLiteralClass
:
1692 return extract_expr(cast
<FloatingLiteral
>(expr
));
1693 case Stmt::ParenExprClass
:
1694 return extract_expr(cast
<ParenExpr
>(expr
));
1695 case Stmt::ConditionalOperatorClass
:
1696 return extract_expr(cast
<ConditionalOperator
>(expr
));
1697 case Stmt::CallExprClass
:
1698 return extract_expr(cast
<CallExpr
>(expr
));
1705 /* Check if the given initialization statement is an assignment.
1706 * If so, return that assignment. Otherwise return NULL.
1708 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1710 BinaryOperator
*ass
;
1712 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1715 ass
= cast
<BinaryOperator
>(init
);
1716 if (ass
->getOpcode() != BO_Assign
)
1722 /* Check if the given initialization statement is a declaration
1723 * of a single variable.
1724 * If so, return that declaration. Otherwise return NULL.
1726 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1730 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1733 decl
= cast
<DeclStmt
>(init
);
1735 if (!decl
->isSingleDecl())
1738 return decl
->getSingleDecl();
1741 /* Given the assignment operator in the initialization of a for loop,
1742 * extract the induction variable, i.e., the (integer)variable being
1745 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1752 lhs
= init
->getLHS();
1753 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1758 ref
= cast
<DeclRefExpr
>(lhs
);
1759 decl
= ref
->getDecl();
1760 type
= decl
->getType().getTypePtr();
1762 if (!type
->isIntegerType()) {
1770 /* Given the initialization statement of a for loop and the single
1771 * declaration in this initialization statement,
1772 * extract the induction variable, i.e., the (integer) variable being
1775 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1779 vd
= cast
<VarDecl
>(decl
);
1781 const QualType type
= vd
->getType();
1782 if (!type
->isIntegerType()) {
1787 if (!vd
->getInit()) {
1795 /* Check that op is of the form iv++ or iv--.
1796 * Return an affine expression "1" or "-1" accordingly.
1798 __isl_give isl_pw_aff
*PetScan::extract_unary_increment(
1799 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1806 if (!op
->isIncrementDecrementOp()) {
1811 sub
= op
->getSubExpr();
1812 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1817 ref
= cast
<DeclRefExpr
>(sub
);
1818 if (ref
->getDecl() != iv
) {
1823 space
= isl_space_params_alloc(ctx
, 0);
1824 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1826 if (op
->isIncrementOp())
1827 aff
= isl_aff_add_constant_si(aff
, 1);
1829 aff
= isl_aff_add_constant_si(aff
, -1);
1831 return isl_pw_aff_from_aff(aff
);
1834 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1835 * has a single constant expression, then put this constant in *user.
1836 * The caller is assumed to have checked that this function will
1837 * be called exactly once.
1839 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
1842 isl_int
*inc
= (isl_int
*)user
;
1845 if (isl_aff_is_cst(aff
))
1846 isl_aff_get_constant(aff
, inc
);
1856 /* Check if op is of the form
1860 * and return inc as an affine expression.
1862 * We extract an affine expression from the RHS, subtract iv and return
1865 __isl_give isl_pw_aff
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1866 clang::ValueDecl
*iv
)
1875 if (op
->getOpcode() != BO_Assign
) {
1881 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1886 ref
= cast
<DeclRefExpr
>(lhs
);
1887 if (ref
->getDecl() != iv
) {
1892 val
= extract_affine(op
->getRHS());
1894 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1896 dim
= isl_space_params_alloc(ctx
, 1);
1897 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1898 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1899 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1901 val
= isl_pw_aff_sub(val
, isl_pw_aff_from_aff(aff
));
1906 /* Check that op is of the form iv += cst or iv -= cst
1907 * and return an affine expression corresponding oto cst or -cst accordingly.
1909 __isl_give isl_pw_aff
*PetScan::extract_compound_increment(
1910 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1916 BinaryOperatorKind opcode
;
1918 opcode
= op
->getOpcode();
1919 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1923 if (opcode
== BO_SubAssign
)
1927 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1932 ref
= cast
<DeclRefExpr
>(lhs
);
1933 if (ref
->getDecl() != iv
) {
1938 val
= extract_affine(op
->getRHS());
1940 val
= isl_pw_aff_neg(val
);
1945 /* Check that the increment of the given for loop increments
1946 * (or decrements) the induction variable "iv" and return
1947 * the increment as an affine expression if successful.
1949 __isl_give isl_pw_aff
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1952 Stmt
*inc
= stmt
->getInc();
1959 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1960 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1961 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1962 return extract_compound_increment(
1963 cast
<CompoundAssignOperator
>(inc
), iv
);
1964 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1965 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1971 /* Embed the given iteration domain in an extra outer loop
1972 * with induction variable "var".
1973 * If this variable appeared as a parameter in the constraints,
1974 * it is replaced by the new outermost dimension.
1976 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
1977 __isl_take isl_id
*var
)
1981 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
1982 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
1984 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
1985 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
1992 /* Return those elements in the space of "cond" that come after
1993 * (based on "sign") an element in "cond".
1995 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
1997 isl_map
*previous_to_this
;
2000 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
2002 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
2004 cond
= isl_set_apply(cond
, previous_to_this
);
2009 /* Create the infinite iteration domain
2011 * { [id] : id >= 0 }
2013 * If "scop" has an affine skip of type pet_skip_later,
2014 * then remove those iterations i that have an earlier iteration
2015 * where the skip condition is satisfied, meaning that iteration i
2017 * Since we are dealing with a loop without loop iterator,
2018 * the skip condition cannot refer to the current loop iterator and
2019 * so effectively, the returned set is of the form
2021 * { [0]; [id] : id >= 1 and not skip }
2023 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
,
2024 struct pet_scop
*scop
)
2026 isl_ctx
*ctx
= isl_id_get_ctx(id
);
2030 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
2031 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
2033 if (!pet_scop_has_affine_skip(scop
, pet_skip_later
))
2036 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
2037 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 1);
2038 skip
= isl_set_params(skip
);
2039 skip
= embed(skip
, isl_id_copy(id
));
2040 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2041 domain
= isl_set_subtract(domain
, after(skip
, 1));
2046 /* Create an identity mapping on the space containing "domain".
2048 static __isl_give isl_map
*identity_map(__isl_keep isl_set
*domain
)
2053 space
= isl_space_map_from_set(isl_set_get_space(domain
));
2054 id
= isl_map_identity(space
);
2059 /* Add a filter to "scop" that imposes that it is only executed
2060 * when "break_access" has a zero value for all previous iterations
2063 * The input "break_access" has a zero-dimensional domain and range.
2065 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
2066 __isl_take isl_map
*break_access
, __isl_take isl_set
*domain
, int sign
)
2068 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2072 id_test
= isl_map_get_tuple_id(break_access
, isl_dim_out
);
2073 break_access
= isl_map_add_dims(break_access
, isl_dim_in
, 1);
2074 break_access
= isl_map_add_dims(break_access
, isl_dim_out
, 1);
2075 break_access
= isl_map_intersect_range(break_access
, domain
);
2076 break_access
= isl_map_set_tuple_id(break_access
, isl_dim_out
, id_test
);
2078 prev
= isl_map_lex_gt_first(isl_map_get_space(break_access
), 1);
2080 prev
= isl_map_lex_lt_first(isl_map_get_space(break_access
), 1);
2081 break_access
= isl_map_intersect(break_access
, prev
);
2082 scop
= pet_scop_filter(scop
, break_access
, 0);
2083 scop
= pet_scop_merge_filters(scop
);
2088 /* Construct a pet_scop for an infinite loop around the given body.
2090 * We extract a pet_scop for the body and then embed it in a loop with
2099 * If the body contains any break, then it is taken into
2100 * account in infinite_domain (if the skip condition is affine)
2101 * or in scop_add_break (if the skip condition is not affine).
2103 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
)
2109 struct pet_scop
*scop
;
2112 scop
= extract(body
);
2116 id
= isl_id_alloc(ctx
, "t", NULL
);
2117 domain
= infinite_domain(isl_id_copy(id
), scop
);
2118 ident
= identity_map(domain
);
2120 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
2122 access
= pet_scop_get_skip_map(scop
, pet_skip_later
);
2124 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
2125 isl_map_copy(ident
), ident
, id
);
2127 scop
= scop_add_break(scop
, access
, domain
, 1);
2129 isl_set_free(domain
);
2134 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
2140 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
)
2142 return extract_infinite_loop(stmt
->getBody());
2145 /* Create an access to a virtual array representing the result
2147 * Unlike other accessed data, the id of the array is NULL as
2148 * there is no ValueDecl in the program corresponding to the virtual
2150 * The array starts out as a scalar, but grows along with the
2151 * statement writing to the array in pet_scop_embed.
2153 static __isl_give isl_map
*create_test_access(isl_ctx
*ctx
, int test_nr
)
2155 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, 0);
2159 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
2160 id
= isl_id_alloc(ctx
, name
, NULL
);
2161 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
2162 return isl_map_universe(dim
);
2165 /* Add an array with the given extent ("access") to the list
2166 * of arrays in "scop" and return the extended pet_scop.
2167 * The array is marked as attaining values 0 and 1 only and
2168 * as each element being assigned at most once.
2170 static struct pet_scop
*scop_add_array(struct pet_scop
*scop
,
2171 __isl_keep isl_map
*access
, clang::ASTContext
&ast_ctx
)
2173 isl_ctx
*ctx
= isl_map_get_ctx(access
);
2175 struct pet_array
*array
;
2182 array
= isl_calloc_type(ctx
, struct pet_array
);
2186 array
->extent
= isl_map_range(isl_map_copy(access
));
2187 dim
= isl_space_params_alloc(ctx
, 0);
2188 array
->context
= isl_set_universe(dim
);
2189 dim
= isl_space_set_alloc(ctx
, 0, 1);
2190 array
->value_bounds
= isl_set_universe(dim
);
2191 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
2193 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
2195 array
->element_type
= strdup("int");
2196 array
->element_size
= ast_ctx
.getTypeInfo(ast_ctx
.IntTy
).first
/ 8;
2197 array
->uniquely_defined
= 1;
2199 if (!array
->extent
|| !array
->context
)
2200 array
= pet_array_free(array
);
2202 scop
= pet_scop_add_array(scop
, array
);
2206 pet_scop_free(scop
);
2210 /* Construct a pet_scop for a while loop of the form
2215 * In particular, construct a scop for an infinite loop around body and
2216 * intersect the domain with the affine expression.
2217 * Note that this intersection may result in an empty loop.
2219 struct pet_scop
*PetScan::extract_affine_while(__isl_take isl_pw_aff
*pa
,
2222 struct pet_scop
*scop
;
2226 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2227 dom
= isl_pw_aff_non_zero_set(pa
);
2228 scop
= extract_infinite_loop(body
);
2229 scop
= pet_scop_restrict(scop
, dom
);
2230 scop
= pet_scop_restrict_context(scop
, valid
);
2235 /* Construct a scop for a while, given the scops for the condition
2236 * and the body, the filter access and the iteration domain of
2239 * In particular, the scop for the condition is filtered to depend
2240 * on "test_access" evaluating to true for all previous iterations
2241 * of the loop, while the scop for the body is filtered to depend
2242 * on "test_access" evaluating to true for all iterations up to the
2243 * current iteration.
2245 * These filtered scops are then combined into a single scop.
2247 * "sign" is positive if the iterator increases and negative
2250 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
2251 struct pet_scop
*scop_body
, __isl_take isl_map
*test_access
,
2252 __isl_take isl_set
*domain
, int sign
)
2254 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2258 id_test
= isl_map_get_tuple_id(test_access
, isl_dim_out
);
2259 test_access
= isl_map_add_dims(test_access
, isl_dim_in
, 1);
2260 test_access
= isl_map_add_dims(test_access
, isl_dim_out
, 1);
2261 test_access
= isl_map_intersect_range(test_access
, domain
);
2262 test_access
= isl_map_set_tuple_id(test_access
, isl_dim_out
, id_test
);
2264 prev
= isl_map_lex_ge_first(isl_map_get_space(test_access
), 1);
2266 prev
= isl_map_lex_le_first(isl_map_get_space(test_access
), 1);
2267 test_access
= isl_map_intersect(test_access
, prev
);
2268 scop_body
= pet_scop_filter(scop_body
, isl_map_copy(test_access
), 1);
2270 prev
= isl_map_lex_gt_first(isl_map_get_space(test_access
), 1);
2272 prev
= isl_map_lex_lt_first(isl_map_get_space(test_access
), 1);
2273 test_access
= isl_map_intersect(test_access
, prev
);
2274 scop_cond
= pet_scop_filter(scop_cond
, test_access
, 1);
2276 return pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
2279 /* Check if the while loop is of the form
2281 * while (affine expression)
2284 * If so, call extract_affine_while to construct a scop.
2286 * Otherwise, construct a generic while scop, with iteration domain
2287 * { [t] : t >= 0 }. The scop consists of two parts, one for
2288 * evaluating the condition and one for the body.
2289 * The schedule is adjusted to reflect that the condition is evaluated
2290 * before the body is executed and the body is filtered to depend
2291 * on the result of the condition evaluating to true on all iterations
2292 * up to the current iteration, while the evaluation the condition itself
2293 * is filtered to depend on the result of the condition evaluating to true
2294 * on all previous iterations.
2295 * The context of the scop representing the body is dropped
2296 * because we don't know how many times the body will be executed,
2299 * If the body contains any break, then it is taken into
2300 * account in infinite_domain (if the skip condition is affine)
2301 * or in scop_add_break (if the skip condition is not affine).
2303 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
)
2307 isl_map
*test_access
;
2311 struct pet_scop
*scop
, *scop_body
;
2313 isl_map
*break_access
;
2315 cond
= stmt
->getCond();
2321 clear_assignments
clear(assigned_value
);
2322 clear
.TraverseStmt(stmt
->getBody());
2324 pa
= try_extract_affine_condition(cond
);
2326 return extract_affine_while(pa
, stmt
->getBody());
2328 if (!allow_nested
) {
2333 test_access
= create_test_access(ctx
, n_test
++);
2334 scop
= extract_non_affine_condition(cond
, isl_map_copy(test_access
));
2335 scop
= scop_add_array(scop
, test_access
, ast_context
);
2336 scop_body
= extract(stmt
->getBody());
2338 id
= isl_id_alloc(ctx
, "t", NULL
);
2339 domain
= infinite_domain(isl_id_copy(id
), scop_body
);
2340 ident
= identity_map(domain
);
2342 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
2344 break_access
= pet_scop_get_skip_map(scop_body
, pet_skip_later
);
2346 scop
= pet_scop_prefix(scop
, 0);
2347 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_map_copy(ident
),
2348 isl_map_copy(ident
), isl_id_copy(id
));
2349 scop_body
= pet_scop_reset_context(scop_body
);
2350 scop_body
= pet_scop_prefix(scop_body
, 1);
2351 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
2352 isl_map_copy(ident
), ident
, id
);
2354 if (has_var_break
) {
2355 scop
= scop_add_break(scop
, isl_map_copy(break_access
),
2356 isl_set_copy(domain
), 1);
2357 scop_body
= scop_add_break(scop_body
, break_access
,
2358 isl_set_copy(domain
), 1);
2360 scop
= scop_add_while(scop
, scop_body
, test_access
, domain
, 1);
2365 /* Check whether "cond" expresses a simple loop bound
2366 * on the only set dimension.
2367 * In particular, if "up" is set then "cond" should contain only
2368 * upper bounds on the set dimension.
2369 * Otherwise, it should contain only lower bounds.
2371 static bool is_simple_bound(__isl_keep isl_set
*cond
, isl_int inc
)
2373 if (isl_int_is_pos(inc
))
2374 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
2376 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
2379 /* Extend a condition on a given iteration of a loop to one that
2380 * imposes the same condition on all previous iterations.
2381 * "domain" expresses the lower [upper] bound on the iterations
2382 * when inc is positive [negative].
2384 * In particular, we construct the condition (when inc is positive)
2386 * forall i' : (domain(i') and i' <= i) => cond(i')
2388 * which is equivalent to
2390 * not exists i' : domain(i') and i' <= i and not cond(i')
2392 * We construct this set by negating cond, applying a map
2394 * { [i'] -> [i] : domain(i') and i' <= i }
2396 * and then negating the result again.
2398 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
2399 __isl_take isl_set
*domain
, isl_int inc
)
2401 isl_map
*previous_to_this
;
2403 if (isl_int_is_pos(inc
))
2404 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
2406 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
2408 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
2410 cond
= isl_set_complement(cond
);
2411 cond
= isl_set_apply(cond
, previous_to_this
);
2412 cond
= isl_set_complement(cond
);
2417 /* Construct a domain of the form
2419 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
2421 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
2422 __isl_take isl_pw_aff
*init
, isl_int inc
)
2428 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
2429 dim
= isl_pw_aff_get_domain_space(init
);
2430 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2431 aff
= isl_aff_add_coefficient(aff
, isl_dim_in
, 0, inc
);
2432 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
2434 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
2435 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
2436 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2437 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
2439 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
2441 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
2443 return isl_set_params(set
);
2446 /* Assuming "cond" represents a bound on a loop where the loop
2447 * iterator "iv" is incremented (or decremented) by one, check if wrapping
2450 * Under the given assumptions, wrapping is only possible if "cond" allows
2451 * for the last value before wrapping, i.e., 2^width - 1 in case of an
2452 * increasing iterator and 0 in case of a decreasing iterator.
2454 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
, isl_int inc
)
2460 test
= isl_set_copy(cond
);
2462 isl_int_init(limit
);
2463 if (isl_int_is_neg(inc
))
2464 isl_int_set_si(limit
, 0);
2466 isl_int_set_si(limit
, 1);
2467 isl_int_mul_2exp(limit
, limit
, get_type_size(iv
));
2468 isl_int_sub_ui(limit
, limit
, 1);
2471 test
= isl_set_fix(cond
, isl_dim_set
, 0, limit
);
2472 cw
= !isl_set_is_empty(test
);
2475 isl_int_clear(limit
);
2480 /* Given a one-dimensional space, construct the following mapping on this
2483 * { [v] -> [v mod 2^width] }
2485 * where width is the number of bits used to represent the values
2486 * of the unsigned variable "iv".
2488 static __isl_give isl_map
*compute_wrapping(__isl_take isl_space
*dim
,
2496 isl_int_set_si(mod
, 1);
2497 isl_int_mul_2exp(mod
, mod
, get_type_size(iv
));
2499 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2500 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2501 aff
= isl_aff_mod(aff
, mod
);
2505 return isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2506 map
= isl_map_reverse(map
);
2509 /* Project out the parameter "id" from "set".
2511 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
2512 __isl_keep isl_id
*id
)
2516 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
2518 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2523 /* Compute the set of parameters for which "set1" is a subset of "set2".
2525 * set1 is a subset of set2 if
2527 * forall i in set1 : i in set2
2531 * not exists i in set1 and i not in set2
2535 * not exists i in set1 \ set2
2537 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
2538 __isl_take isl_set
*set2
)
2540 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
2543 /* Compute the set of parameter values for which "cond" holds
2544 * on the next iteration for each element of "dom".
2546 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2547 * and then compute the set of parameters for which the result is a subset
2550 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
2551 __isl_take isl_set
*dom
, isl_int inc
)
2557 space
= isl_set_get_space(dom
);
2558 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2559 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2560 aff
= isl_aff_add_constant(aff
, inc
);
2561 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2563 dom
= isl_set_apply(dom
, next
);
2565 return enforce_subset(dom
, cond
);
2568 /* Does "id" refer to a nested access?
2570 static bool is_nested_parameter(__isl_keep isl_id
*id
)
2572 return id
&& isl_id_get_user(id
) && !isl_id_get_name(id
);
2575 /* Does parameter "pos" of "space" refer to a nested access?
2577 static bool is_nested_parameter(__isl_keep isl_space
*space
, int pos
)
2582 id
= isl_space_get_dim_id(space
, isl_dim_param
, pos
);
2583 nested
= is_nested_parameter(id
);
2589 /* Does "space" involve any parameters that refer to nested
2590 * accesses, i.e., parameters with no name?
2592 static bool has_nested(__isl_keep isl_space
*space
)
2596 nparam
= isl_space_dim(space
, isl_dim_param
);
2597 for (int i
= 0; i
< nparam
; ++i
)
2598 if (is_nested_parameter(space
, i
))
2604 /* Does "pa" involve any parameters that refer to nested
2605 * accesses, i.e., parameters with no name?
2607 static bool has_nested(__isl_keep isl_pw_aff
*pa
)
2612 space
= isl_pw_aff_get_space(pa
);
2613 nested
= has_nested(space
);
2614 isl_space_free(space
);
2619 /* Construct a pet_scop for a for statement.
2620 * The for loop is required to be of the form
2622 * for (i = init; condition; ++i)
2626 * for (i = init; condition; --i)
2628 * The initialization of the for loop should either be an assignment
2629 * to an integer variable, or a declaration of such a variable with
2632 * The condition is allowed to contain nested accesses, provided
2633 * they are not being written to inside the body of the loop.
2634 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2635 * essentially treated as a while loop, with iteration domain
2636 * { [i] : i >= init }.
2638 * We extract a pet_scop for the body and then embed it in a loop with
2639 * iteration domain and schedule
2641 * { [i] : i >= init and condition' }
2646 * { [i] : i <= init and condition' }
2649 * Where condition' is equal to condition if the latter is
2650 * a simple upper [lower] bound and a condition that is extended
2651 * to apply to all previous iterations otherwise.
2653 * If the condition is non-affine, then we drop the condition from the
2654 * iteration domain and instead create a separate statement
2655 * for evaluating the condition. The body is then filtered to depend
2656 * on the result of the condition evaluating to true on all iterations
2657 * up to the current iteration, while the evaluation the condition itself
2658 * is filtered to depend on the result of the condition evaluating to true
2659 * on all previous iterations.
2660 * The context of the scop representing the body is dropped
2661 * because we don't know how many times the body will be executed,
2664 * If the stride of the loop is not 1, then "i >= init" is replaced by
2666 * (exists a: i = init + stride * a and a >= 0)
2668 * If the loop iterator i is unsigned, then wrapping may occur.
2669 * During the computation, we work with a virtual iterator that
2670 * does not wrap. However, the condition in the code applies
2671 * to the wrapped value, so we need to change condition(i)
2672 * into condition([i % 2^width]).
2673 * After computing the virtual domain and schedule, we apply
2674 * the function { [v] -> [v % 2^width] } to the domain and the domain
2675 * of the schedule. In order not to lose any information, we also
2676 * need to intersect the domain of the schedule with the virtual domain
2677 * first, since some iterations in the wrapped domain may be scheduled
2678 * several times, typically an infinite number of times.
2679 * Note that there may be no need to perform this final wrapping
2680 * if the loop condition (after wrapping) satisfies certain conditions.
2681 * However, the is_simple_bound condition is not enough since it doesn't
2682 * check if there even is an upper bound.
2684 * If the loop condition is non-affine, then we keep the virtual
2685 * iterator in the iteration domain and instead replace all accesses
2686 * to the original iterator by the wrapping of the virtual iterator.
2688 * Wrapping on unsigned iterators can be avoided entirely if
2689 * loop condition is simple, the loop iterator is incremented
2690 * [decremented] by one and the last value before wrapping cannot
2691 * possibly satisfy the loop condition.
2693 * Before extracting a pet_scop from the body we remove all
2694 * assignments in assigned_value to variables that are assigned
2695 * somewhere in the body of the loop.
2697 * Valid parameters for a for loop are those for which the initial
2698 * value itself, the increment on each domain iteration and
2699 * the condition on both the initial value and
2700 * the result of incrementing the iterator for each iteration of the domain
2702 * If the loop condition is non-affine, then we only consider validity
2703 * of the initial value.
2705 * If the body contains any break, then we keep track of it in "skip"
2706 * (if the skip condition is affine) or it is handled in scop_add_break
2707 * (if the skip condition is not affine).
2708 * Note that the affine break condition needs to be considered with
2709 * respect to previous iterations in the virtual domain (if any)
2710 * and that the domain needs to be kept virtual if there is a non-affine
2713 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
)
2715 BinaryOperator
*ass
;
2723 isl_set
*cond
= NULL
;
2724 isl_set
*skip
= NULL
;
2726 struct pet_scop
*scop
, *scop_cond
= NULL
;
2727 assigned_value_cache
cache(assigned_value
);
2733 bool keep_virtual
= false;
2734 bool has_affine_break
;
2736 isl_map
*wrap
= NULL
;
2737 isl_pw_aff
*pa
, *pa_inc
, *init_val
;
2738 isl_set
*valid_init
;
2739 isl_set
*valid_cond
;
2740 isl_set
*valid_cond_init
;
2741 isl_set
*valid_cond_next
;
2743 isl_map
*test_access
= NULL
, *break_access
= NULL
;
2746 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
2747 return extract_infinite_for(stmt
);
2749 init
= stmt
->getInit();
2754 if ((ass
= initialization_assignment(init
)) != NULL
) {
2755 iv
= extract_induction_variable(ass
);
2758 lhs
= ass
->getLHS();
2759 rhs
= ass
->getRHS();
2760 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
2761 VarDecl
*var
= extract_induction_variable(init
, decl
);
2765 rhs
= var
->getInit();
2766 lhs
= create_DeclRefExpr(var
);
2768 unsupported(stmt
->getInit());
2772 pa_inc
= extract_increment(stmt
, iv
);
2777 if (isl_pw_aff_n_piece(pa_inc
) != 1 ||
2778 isl_pw_aff_foreach_piece(pa_inc
, &extract_cst
, &inc
) < 0) {
2779 isl_pw_aff_free(pa_inc
);
2780 unsupported(stmt
->getInc());
2784 valid_inc
= isl_pw_aff_domain(pa_inc
);
2786 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
2788 assigned_value
.erase(iv
);
2789 clear_assignments
clear(assigned_value
);
2790 clear
.TraverseStmt(stmt
->getBody());
2792 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
2794 pa
= try_extract_nested_condition(stmt
->getCond());
2795 if (allow_nested
&& (!pa
|| has_nested(pa
)))
2798 scop
= extract(stmt
->getBody());
2800 has_affine_break
= scop
&&
2801 pet_scop_has_affine_skip(scop
, pet_skip_later
);
2802 if (has_affine_break
) {
2803 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
2804 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 1);
2805 skip
= isl_set_params(skip
);
2807 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
2808 if (has_var_break
) {
2809 break_access
= pet_scop_get_skip_map(scop
, pet_skip_later
);
2810 keep_virtual
= true;
2813 if (pa
&& !is_nested_allowed(pa
, scop
)) {
2814 isl_pw_aff_free(pa
);
2818 if (!allow_nested
&& !pa
)
2819 pa
= try_extract_affine_condition(stmt
->getCond());
2820 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2821 cond
= isl_pw_aff_non_zero_set(pa
);
2822 if (allow_nested
&& !cond
) {
2823 int save_n_stmt
= n_stmt
;
2824 test_access
= create_test_access(ctx
, n_test
++);
2826 scop_cond
= extract_non_affine_condition(stmt
->getCond(),
2827 isl_map_copy(test_access
));
2828 n_stmt
= save_n_stmt
;
2829 scop_cond
= scop_add_array(scop_cond
, test_access
, ast_context
);
2830 scop_cond
= pet_scop_prefix(scop_cond
, 0);
2831 scop
= pet_scop_reset_context(scop
);
2832 scop
= pet_scop_prefix(scop
, 1);
2833 keep_virtual
= true;
2834 cond
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2837 cond
= embed(cond
, isl_id_copy(id
));
2838 skip
= embed(skip
, isl_id_copy(id
));
2839 valid_cond
= isl_set_coalesce(valid_cond
);
2840 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
2841 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
2842 is_one
= isl_int_is_one(inc
) || isl_int_is_negone(inc
);
2843 is_virtual
= is_unsigned
&& (!is_one
|| can_wrap(cond
, iv
, inc
));
2845 init_val
= extract_affine(rhs
);
2846 valid_cond_init
= enforce_subset(
2847 isl_set_from_pw_aff(isl_pw_aff_copy(init_val
)),
2848 isl_set_copy(valid_cond
));
2849 if (is_one
&& !is_virtual
) {
2850 isl_pw_aff_free(init_val
);
2851 pa
= extract_comparison(isl_int_is_pos(inc
) ? BO_GE
: BO_LE
,
2853 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2854 valid_init
= set_project_out_by_id(valid_init
, id
);
2855 domain
= isl_pw_aff_non_zero_set(pa
);
2857 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
2858 domain
= strided_domain(isl_id_copy(id
), init_val
, inc
);
2861 domain
= embed(domain
, isl_id_copy(id
));
2864 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
2865 rev_wrap
= isl_map_reverse(isl_map_copy(wrap
));
2866 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
2867 skip
= isl_set_apply(skip
, isl_map_copy(rev_wrap
));
2868 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
2869 valid_inc
= isl_set_apply(valid_inc
, rev_wrap
);
2871 is_simple
= is_simple_bound(cond
, inc
);
2873 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
2874 is_simple
= is_simple_bound(cond
, inc
);
2877 cond
= valid_for_each_iteration(cond
,
2878 isl_set_copy(domain
), inc
);
2879 domain
= isl_set_intersect(domain
, cond
);
2880 if (has_affine_break
) {
2881 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2882 skip
= after(skip
, isl_int_sgn(inc
));
2883 domain
= isl_set_subtract(domain
, skip
);
2885 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
2886 space
= isl_space_from_domain(isl_set_get_space(domain
));
2887 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2888 sched
= isl_map_universe(space
);
2889 if (isl_int_is_pos(inc
))
2890 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
2892 sched
= isl_map_oppose(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
2894 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
), inc
);
2895 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
2897 if (is_virtual
&& !keep_virtual
) {
2898 wrap
= isl_map_set_dim_id(wrap
,
2899 isl_dim_out
, 0, isl_id_copy(id
));
2900 sched
= isl_map_intersect_domain(sched
, isl_set_copy(domain
));
2901 domain
= isl_set_apply(domain
, isl_map_copy(wrap
));
2902 sched
= isl_map_apply_domain(sched
, wrap
);
2904 if (!(is_virtual
&& keep_virtual
)) {
2905 space
= isl_set_get_space(domain
);
2906 wrap
= isl_map_identity(isl_space_map_from_set(space
));
2909 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
2910 isl_map_copy(sched
), isl_map_copy(wrap
), isl_id_copy(id
));
2911 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
2912 scop
= resolve_nested(scop
);
2914 scop
= scop_add_break(scop
, break_access
, isl_set_copy(domain
),
2917 scop
= scop_add_while(scop_cond
, scop
, test_access
, domain
,
2919 isl_set_free(valid_inc
);
2921 scop
= pet_scop_restrict_context(scop
, valid_inc
);
2922 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
2923 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
2924 isl_set_free(domain
);
2926 clear_assignment(assigned_value
, iv
);
2930 scop
= pet_scop_restrict_context(scop
, valid_init
);
2935 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
, bool skip_declarations
)
2937 return extract(stmt
->children(), true, skip_declarations
);
2940 /* Does parameter "pos" of "map" refer to a nested access?
2942 static bool is_nested_parameter(__isl_keep isl_map
*map
, int pos
)
2947 id
= isl_map_get_dim_id(map
, isl_dim_param
, pos
);
2948 nested
= is_nested_parameter(id
);
2954 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
2956 static int n_nested_parameter(__isl_keep isl_space
*space
)
2961 nparam
= isl_space_dim(space
, isl_dim_param
);
2962 for (int i
= 0; i
< nparam
; ++i
)
2963 if (is_nested_parameter(space
, i
))
2969 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
2971 static int n_nested_parameter(__isl_keep isl_map
*map
)
2976 space
= isl_map_get_space(map
);
2977 n
= n_nested_parameter(space
);
2978 isl_space_free(space
);
2983 /* For each nested access parameter in "space",
2984 * construct a corresponding pet_expr, place it in args and
2985 * record its position in "param2pos".
2986 * "n_arg" is the number of elements that are already in args.
2987 * The position recorded in "param2pos" takes this number into account.
2988 * If the pet_expr corresponding to a parameter is identical to
2989 * the pet_expr corresponding to an earlier parameter, then these two
2990 * parameters are made to refer to the same element in args.
2992 * Return the final number of elements in args or -1 if an error has occurred.
2994 int PetScan::extract_nested(__isl_keep isl_space
*space
,
2995 int n_arg
, struct pet_expr
**args
, std::map
<int,int> ¶m2pos
)
2999 nparam
= isl_space_dim(space
, isl_dim_param
);
3000 for (int i
= 0; i
< nparam
; ++i
) {
3002 isl_id
*id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
3005 if (!is_nested_parameter(id
)) {
3010 nested
= (Expr
*) isl_id_get_user(id
);
3011 args
[n_arg
] = extract_expr(nested
);
3015 for (j
= 0; j
< n_arg
; ++j
)
3016 if (pet_expr_is_equal(args
[j
], args
[n_arg
]))
3020 pet_expr_free(args
[n_arg
]);
3024 param2pos
[i
] = n_arg
++;
3032 /* For each nested access parameter in the access relations in "expr",
3033 * construct a corresponding pet_expr, place it in expr->args and
3034 * record its position in "param2pos".
3035 * n is the number of nested access parameters.
3037 struct pet_expr
*PetScan::extract_nested(struct pet_expr
*expr
, int n
,
3038 std::map
<int,int> ¶m2pos
)
3042 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n
);
3047 space
= isl_map_get_space(expr
->acc
.access
);
3048 n
= extract_nested(space
, 0, expr
->args
, param2pos
);
3049 isl_space_free(space
);
3057 pet_expr_free(expr
);
3061 /* Look for parameters in any access relation in "expr" that
3062 * refer to nested accesses. In particular, these are
3063 * parameters with no name.
3065 * If there are any such parameters, then the domain of the access
3066 * relation, which is still [] at this point, is replaced by
3067 * [[] -> [t_1,...,t_n]], with n the number of these parameters
3068 * (after identifying identical nested accesses).
3069 * The parameters are then equated to the corresponding t dimensions
3070 * and subsequently projected out.
3071 * param2pos maps the position of the parameter to the position
3072 * of the corresponding t dimension.
3074 struct pet_expr
*PetScan::resolve_nested(struct pet_expr
*expr
)
3081 std::map
<int,int> param2pos
;
3086 for (int i
= 0; i
< expr
->n_arg
; ++i
) {
3087 expr
->args
[i
] = resolve_nested(expr
->args
[i
]);
3088 if (!expr
->args
[i
]) {
3089 pet_expr_free(expr
);
3094 if (expr
->type
!= pet_expr_access
)
3097 n
= n_nested_parameter(expr
->acc
.access
);
3101 expr
= extract_nested(expr
, n
, param2pos
);
3106 nparam
= isl_map_dim(expr
->acc
.access
, isl_dim_param
);
3107 n_in
= isl_map_dim(expr
->acc
.access
, isl_dim_in
);
3108 dim
= isl_map_get_space(expr
->acc
.access
);
3109 dim
= isl_space_domain(dim
);
3110 dim
= isl_space_from_domain(dim
);
3111 dim
= isl_space_add_dims(dim
, isl_dim_out
, n
);
3112 map
= isl_map_universe(dim
);
3113 map
= isl_map_domain_map(map
);
3114 map
= isl_map_reverse(map
);
3115 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
3117 for (int i
= nparam
- 1; i
>= 0; --i
) {
3118 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
3120 if (!is_nested_parameter(id
)) {
3125 expr
->acc
.access
= isl_map_equate(expr
->acc
.access
,
3126 isl_dim_param
, i
, isl_dim_in
,
3127 n_in
+ param2pos
[i
]);
3128 expr
->acc
.access
= isl_map_project_out(expr
->acc
.access
,
3129 isl_dim_param
, i
, 1);
3136 pet_expr_free(expr
);
3140 /* Convert a top-level pet_expr to a pet_scop with one statement.
3141 * This mainly involves resolving nested expression parameters
3142 * and setting the name of the iteration space.
3143 * The name is given by "label" if it is non-NULL. Otherwise,
3144 * it is of the form S_<n_stmt>.
3146 struct pet_scop
*PetScan::extract(Stmt
*stmt
, struct pet_expr
*expr
,
3147 __isl_take isl_id
*label
)
3149 struct pet_stmt
*ps
;
3150 SourceLocation loc
= stmt
->getLocStart();
3151 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3153 expr
= resolve_nested(expr
);
3154 ps
= pet_stmt_from_pet_expr(ctx
, line
, label
, n_stmt
++, expr
);
3155 return pet_scop_from_pet_stmt(ctx
, ps
);
3158 /* Check if we can extract an affine expression from "expr".
3159 * Return the expressions as an isl_pw_aff if we can and NULL otherwise.
3160 * We turn on autodetection so that we won't generate any warnings
3161 * and turn off nesting, so that we won't accept any non-affine constructs.
3163 __isl_give isl_pw_aff
*PetScan::try_extract_affine(Expr
*expr
)
3166 int save_autodetect
= options
->autodetect
;
3167 bool save_nesting
= nesting_enabled
;
3169 options
->autodetect
= 1;
3170 nesting_enabled
= false;
3172 pwaff
= extract_affine(expr
);
3174 options
->autodetect
= save_autodetect
;
3175 nesting_enabled
= save_nesting
;
3180 /* Check whether "expr" is an affine expression.
3182 bool PetScan::is_affine(Expr
*expr
)
3186 pwaff
= try_extract_affine(expr
);
3187 isl_pw_aff_free(pwaff
);
3189 return pwaff
!= NULL
;
3192 /* Check if we can extract an affine constraint from "expr".
3193 * Return the constraint as an isl_set if we can and NULL otherwise.
3194 * We turn on autodetection so that we won't generate any warnings
3195 * and turn off nesting, so that we won't accept any non-affine constructs.
3197 __isl_give isl_pw_aff
*PetScan::try_extract_affine_condition(Expr
*expr
)
3200 int save_autodetect
= options
->autodetect
;
3201 bool save_nesting
= nesting_enabled
;
3203 options
->autodetect
= 1;
3204 nesting_enabled
= false;
3206 cond
= extract_condition(expr
);
3208 options
->autodetect
= save_autodetect
;
3209 nesting_enabled
= save_nesting
;
3214 /* Check whether "expr" is an affine constraint.
3216 bool PetScan::is_affine_condition(Expr
*expr
)
3220 cond
= try_extract_affine_condition(expr
);
3221 isl_pw_aff_free(cond
);
3223 return cond
!= NULL
;
3226 /* Check if we can extract a condition from "expr".
3227 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
3228 * If allow_nested is set, then the condition may involve parameters
3229 * corresponding to nested accesses.
3230 * We turn on autodetection so that we won't generate any warnings.
3232 __isl_give isl_pw_aff
*PetScan::try_extract_nested_condition(Expr
*expr
)
3235 int save_autodetect
= options
->autodetect
;
3236 bool save_nesting
= nesting_enabled
;
3238 options
->autodetect
= 1;
3239 nesting_enabled
= allow_nested
;
3240 cond
= extract_condition(expr
);
3242 options
->autodetect
= save_autodetect
;
3243 nesting_enabled
= save_nesting
;
3248 /* If the top-level expression of "stmt" is an assignment, then
3249 * return that assignment as a BinaryOperator.
3250 * Otherwise return NULL.
3252 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
3254 BinaryOperator
*ass
;
3258 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
3261 ass
= cast
<BinaryOperator
>(stmt
);
3262 if(ass
->getOpcode() != BO_Assign
)
3268 /* Check if the given if statement is a conditional assignement
3269 * with a non-affine condition. If so, construct a pet_scop
3270 * corresponding to this conditional assignment. Otherwise return NULL.
3272 * In particular we check if "stmt" is of the form
3279 * where a is some array or scalar access.
3280 * The constructed pet_scop then corresponds to the expression
3282 * a = condition ? f(...) : g(...)
3284 * All access relations in f(...) are intersected with condition
3285 * while all access relation in g(...) are intersected with the complement.
3287 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
)
3289 BinaryOperator
*ass_then
, *ass_else
;
3290 isl_map
*write_then
, *write_else
;
3291 isl_set
*cond
, *comp
;
3295 struct pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
3296 bool save_nesting
= nesting_enabled
;
3298 if (!options
->detect_conditional_assignment
)
3301 ass_then
= top_assignment_or_null(stmt
->getThen());
3302 ass_else
= top_assignment_or_null(stmt
->getElse());
3304 if (!ass_then
|| !ass_else
)
3307 if (is_affine_condition(stmt
->getCond()))
3310 write_then
= extract_access(ass_then
->getLHS());
3311 write_else
= extract_access(ass_else
->getLHS());
3313 equal
= isl_map_is_equal(write_then
, write_else
);
3314 isl_map_free(write_else
);
3315 if (equal
< 0 || !equal
) {
3316 isl_map_free(write_then
);
3320 nesting_enabled
= allow_nested
;
3321 pa
= extract_condition(stmt
->getCond());
3322 nesting_enabled
= save_nesting
;
3323 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
3324 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
3325 map
= isl_map_from_range(isl_set_from_pw_aff(pa
));
3327 pe_cond
= pet_expr_from_access(map
);
3329 pe_then
= extract_expr(ass_then
->getRHS());
3330 pe_then
= pet_expr_restrict(pe_then
, cond
);
3331 pe_else
= extract_expr(ass_else
->getRHS());
3332 pe_else
= pet_expr_restrict(pe_else
, comp
);
3334 pe
= pet_expr_new_ternary(ctx
, pe_cond
, pe_then
, pe_else
);
3335 pe_write
= pet_expr_from_access(write_then
);
3337 pe_write
->acc
.write
= 1;
3338 pe_write
->acc
.read
= 0;
3340 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, pe_write
, pe
);
3341 return extract(stmt
, pe
);
3344 /* Create a pet_scop with a single statement evaluating "cond"
3345 * and writing the result to a virtual scalar, as expressed by
3348 struct pet_scop
*PetScan::extract_non_affine_condition(Expr
*cond
,
3349 __isl_take isl_map
*access
)
3351 struct pet_expr
*expr
, *write
;
3352 struct pet_stmt
*ps
;
3353 struct pet_scop
*scop
;
3354 SourceLocation loc
= cond
->getLocStart();
3355 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3357 write
= pet_expr_from_access(access
);
3359 write
->acc
.write
= 1;
3360 write
->acc
.read
= 0;
3362 expr
= extract_expr(cond
);
3363 expr
= resolve_nested(expr
);
3364 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, write
, expr
);
3365 ps
= pet_stmt_from_pet_expr(ctx
, line
, NULL
, n_stmt
++, expr
);
3366 scop
= pet_scop_from_pet_stmt(ctx
, ps
);
3367 scop
= resolve_nested(scop
);
3373 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
3377 /* Apply the map pointed to by "user" to the domain of the access
3378 * relation, thereby embedding it in the range of the map.
3379 * The domain of both relations is the zero-dimensional domain.
3381 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
, void *user
)
3383 isl_map
*map
= (isl_map
*) user
;
3385 return isl_map_apply_domain(access
, isl_map_copy(map
));
3388 /* Apply "map" to all access relations in "expr".
3390 static struct pet_expr
*embed(struct pet_expr
*expr
, __isl_keep isl_map
*map
)
3392 return pet_expr_foreach_access(expr
, &embed_access
, map
);
3395 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
3397 static int n_nested_parameter(__isl_keep isl_set
*set
)
3402 space
= isl_set_get_space(set
);
3403 n
= n_nested_parameter(space
);
3404 isl_space_free(space
);
3409 /* Remove all parameters from "map" that refer to nested accesses.
3411 static __isl_give isl_map
*remove_nested_parameters(__isl_take isl_map
*map
)
3416 space
= isl_map_get_space(map
);
3417 nparam
= isl_space_dim(space
, isl_dim_param
);
3418 for (int i
= nparam
- 1; i
>= 0; --i
)
3419 if (is_nested_parameter(space
, i
))
3420 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3421 isl_space_free(space
);
3427 static __isl_give isl_map
*access_remove_nested_parameters(
3428 __isl_take isl_map
*access
, void *user
);
3431 static __isl_give isl_map
*access_remove_nested_parameters(
3432 __isl_take isl_map
*access
, void *user
)
3434 return remove_nested_parameters(access
);
3437 /* Remove all nested access parameters from the schedule and all
3438 * accesses of "stmt".
3439 * There is no need to remove them from the domain as these parameters
3440 * have already been removed from the domain when this function is called.
3442 static struct pet_stmt
*remove_nested_parameters(struct pet_stmt
*stmt
)
3446 stmt
->schedule
= remove_nested_parameters(stmt
->schedule
);
3447 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
3448 &access_remove_nested_parameters
, NULL
);
3449 if (!stmt
->schedule
|| !stmt
->body
)
3451 for (int i
= 0; i
< stmt
->n_arg
; ++i
) {
3452 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
3453 &access_remove_nested_parameters
, NULL
);
3460 pet_stmt_free(stmt
);
3464 /* For each nested access parameter in the domain of "stmt",
3465 * construct a corresponding pet_expr, place it before the original
3466 * elements in stmt->args and record its position in "param2pos".
3467 * n is the number of nested access parameters.
3469 struct pet_stmt
*PetScan::extract_nested(struct pet_stmt
*stmt
, int n
,
3470 std::map
<int,int> ¶m2pos
)
3475 struct pet_expr
**args
;
3477 n_arg
= stmt
->n_arg
;
3478 args
= isl_calloc_array(ctx
, struct pet_expr
*, n
+ n_arg
);
3482 space
= isl_set_get_space(stmt
->domain
);
3483 n_arg
= extract_nested(space
, 0, args
, param2pos
);
3484 isl_space_free(space
);
3489 for (i
= 0; i
< stmt
->n_arg
; ++i
)
3490 args
[n_arg
+ i
] = stmt
->args
[i
];
3493 stmt
->n_arg
+= n_arg
;
3498 for (i
= 0; i
< n
; ++i
)
3499 pet_expr_free(args
[i
]);
3502 pet_stmt_free(stmt
);
3506 /* Check whether any of the arguments i of "stmt" starting at position "n"
3507 * is equal to one of the first "n" arguments j.
3508 * If so, combine the constraints on arguments i and j and remove
3511 static struct pet_stmt
*remove_duplicate_arguments(struct pet_stmt
*stmt
, int n
)
3520 if (n
== stmt
->n_arg
)
3523 map
= isl_set_unwrap(stmt
->domain
);
3525 for (i
= stmt
->n_arg
- 1; i
>= n
; --i
) {
3526 for (j
= 0; j
< n
; ++j
)
3527 if (pet_expr_is_equal(stmt
->args
[i
], stmt
->args
[j
]))
3532 map
= isl_map_equate(map
, isl_dim_out
, i
, isl_dim_out
, j
);
3533 map
= isl_map_project_out(map
, isl_dim_out
, i
, 1);
3535 pet_expr_free(stmt
->args
[i
]);
3536 for (j
= i
; j
+ 1 < stmt
->n_arg
; ++j
)
3537 stmt
->args
[j
] = stmt
->args
[j
+ 1];
3541 stmt
->domain
= isl_map_wrap(map
);
3546 pet_stmt_free(stmt
);
3550 /* Look for parameters in the iteration domain of "stmt" that
3551 * refer to nested accesses. In particular, these are
3552 * parameters with no name.
3554 * If there are any such parameters, then as many extra variables
3555 * (after identifying identical nested accesses) are inserted in the
3556 * range of the map wrapped inside the domain, before the original variables.
3557 * If the original domain is not a wrapped map, then a new wrapped
3558 * map is created with zero output dimensions.
3559 * The parameters are then equated to the corresponding output dimensions
3560 * and subsequently projected out, from the iteration domain,
3561 * the schedule and the access relations.
3562 * For each of the output dimensions, a corresponding argument
3563 * expression is inserted. Initially they are created with
3564 * a zero-dimensional domain, so they have to be embedded
3565 * in the current iteration domain.
3566 * param2pos maps the position of the parameter to the position
3567 * of the corresponding output dimension in the wrapped map.
3569 struct pet_stmt
*PetScan::resolve_nested(struct pet_stmt
*stmt
)
3575 std::map
<int,int> param2pos
;
3580 n
= n_nested_parameter(stmt
->domain
);
3584 n_arg
= stmt
->n_arg
;
3585 stmt
= extract_nested(stmt
, n
, param2pos
);
3589 n
= stmt
->n_arg
- n_arg
;
3590 nparam
= isl_set_dim(stmt
->domain
, isl_dim_param
);
3591 if (isl_set_is_wrapping(stmt
->domain
))
3592 map
= isl_set_unwrap(stmt
->domain
);
3594 map
= isl_map_from_domain(stmt
->domain
);
3595 map
= isl_map_insert_dims(map
, isl_dim_out
, 0, n
);
3597 for (int i
= nparam
- 1; i
>= 0; --i
) {
3600 if (!is_nested_parameter(map
, i
))
3603 id
= isl_map_get_tuple_id(stmt
->args
[param2pos
[i
]]->acc
.access
,
3605 map
= isl_map_set_dim_id(map
, isl_dim_out
, param2pos
[i
], id
);
3606 map
= isl_map_equate(map
, isl_dim_param
, i
, isl_dim_out
,
3608 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3611 stmt
->domain
= isl_map_wrap(map
);
3613 map
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
3614 map
= isl_map_from_range(isl_map_domain(map
));
3615 for (int pos
= 0; pos
< n
; ++pos
)
3616 stmt
->args
[pos
] = embed(stmt
->args
[pos
], map
);
3619 stmt
= remove_nested_parameters(stmt
);
3620 stmt
= remove_duplicate_arguments(stmt
, n
);
3624 pet_stmt_free(stmt
);
3628 /* For each statement in "scop", move the parameters that correspond
3629 * to nested access into the ranges of the domains and create
3630 * corresponding argument expressions.
3632 struct pet_scop
*PetScan::resolve_nested(struct pet_scop
*scop
)
3637 for (int i
= 0; i
< scop
->n_stmt
; ++i
) {
3638 scop
->stmts
[i
] = resolve_nested(scop
->stmts
[i
]);
3639 if (!scop
->stmts
[i
])
3645 pet_scop_free(scop
);
3649 /* Given an access expression "expr", is the variable accessed by
3650 * "expr" assigned anywhere inside "scop"?
3652 static bool is_assigned(pet_expr
*expr
, pet_scop
*scop
)
3654 bool assigned
= false;
3657 id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
3658 assigned
= pet_scop_writes(scop
, id
);
3664 /* Are all nested access parameters in "pa" allowed given "scop".
3665 * In particular, is none of them written by anywhere inside "scop".
3667 * If "scop" has any skip conditions, then no nested access parameters
3668 * are allowed. In particular, if there is any nested access in a guard
3669 * for a piece of code containing a "continue", then we want to introduce
3670 * a separate statement for evaluating this guard so that we can express
3671 * that the result is false for all previous iterations.
3673 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff
*pa
, pet_scop
*scop
)
3680 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
3681 for (int i
= 0; i
< nparam
; ++i
) {
3683 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
3687 if (!is_nested_parameter(id
)) {
3692 if (pet_scop_has_skip(scop
, pet_skip_now
)) {
3697 nested
= (Expr
*) isl_id_get_user(id
);
3698 expr
= extract_expr(nested
);
3699 allowed
= expr
&& expr
->type
== pet_expr_access
&&
3700 !is_assigned(expr
, scop
);
3702 pet_expr_free(expr
);
3712 /* Do we need to construct a skip condition of the given type
3713 * on an if statement, given that the if condition is non-affine?
3715 * pet_scop_filter_skip can only handle the case where the if condition
3716 * holds (the then branch) and the skip condition is universal.
3717 * In any other case, we need to construct a new skip condition.
3719 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
3720 bool have_else
, enum pet_skip type
)
3722 if (have_else
&& scop_else
&& pet_scop_has_skip(scop_else
, type
))
3724 if (scop_then
&& pet_scop_has_skip(scop_then
, type
) &&
3725 !pet_scop_has_universal_skip(scop_then
, type
))
3730 /* Do we need to construct a skip condition of the given type
3731 * on an if statement, given that the if condition is affine?
3733 * There is no need to construct a new skip condition if all
3734 * the skip conditions are affine.
3736 static bool need_skip_aff(struct pet_scop
*scop_then
,
3737 struct pet_scop
*scop_else
, bool have_else
, enum pet_skip type
)
3739 if (scop_then
&& pet_scop_has_var_skip(scop_then
, type
))
3741 if (have_else
&& scop_else
&& pet_scop_has_var_skip(scop_else
, type
))
3746 /* Do we need to construct a skip condition of the given type
3747 * on an if statement?
3749 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
3750 bool have_else
, enum pet_skip type
, bool affine
)
3753 return need_skip_aff(scop_then
, scop_else
, have_else
, type
);
3755 return need_skip(scop_then
, scop_else
, have_else
, type
);
3758 /* Construct an affine expression pet_expr that is evaluates
3759 * to the constant "val".
3761 static struct pet_expr
*universally(isl_ctx
*ctx
, int val
)
3766 space
= isl_space_alloc(ctx
, 0, 0, 1);
3767 map
= isl_map_universe(space
);
3768 map
= isl_map_fix_si(map
, isl_dim_out
, 0, val
);
3770 return pet_expr_from_access(map
);
3773 /* Construct an affine expression pet_expr that is evaluates
3774 * to the constant 1.
3776 static struct pet_expr
*universally_true(isl_ctx
*ctx
)
3778 return universally(ctx
, 1);
3781 /* Construct an affine expression pet_expr that is evaluates
3782 * to the constant 0.
3784 static struct pet_expr
*universally_false(isl_ctx
*ctx
)
3786 return universally(ctx
, 0);
3789 /* Given an access relation "test_access" for the if condition,
3790 * an access relation "skip_access" for the skip condition and
3791 * scops for the then and else branches, construct a scop for
3792 * computing "skip_access".
3794 * The computed scop contains a single statement that essentially does
3796 * skip_cond = test_cond ? skip_cond_then : skip_cond_else
3798 * If the skip conditions of the then and/or else branch are not affine,
3799 * then they need to be filtered by test_access.
3800 * If they are missing, then this means the skip condition is false.
3802 * Since we are constructing a skip condition for the if statement,
3803 * the skip conditions on the then and else branches are removed.
3805 static struct pet_scop
*extract_skip(PetScan
*scan
,
3806 __isl_take isl_map
*test_access
, __isl_take isl_map
*skip_access
,
3807 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
, bool have_else
,
3810 struct pet_expr
*expr_then
, *expr_else
, *expr
, *expr_skip
;
3811 struct pet_stmt
*stmt
;
3812 struct pet_scop
*scop
;
3813 isl_ctx
*ctx
= scan
->ctx
;
3817 if (have_else
&& !scop_else
)
3820 if (pet_scop_has_skip(scop_then
, type
)) {
3821 expr_then
= pet_scop_get_skip_expr(scop_then
, type
);
3822 pet_scop_reset_skip(scop_then
, type
);
3823 if (!pet_expr_is_affine(expr_then
))
3824 expr_then
= pet_expr_filter(expr_then
,
3825 isl_map_copy(test_access
), 1);
3827 expr_then
= universally_false(ctx
);
3829 if (have_else
&& pet_scop_has_skip(scop_else
, type
)) {
3830 expr_else
= pet_scop_get_skip_expr(scop_else
, type
);
3831 pet_scop_reset_skip(scop_else
, type
);
3832 if (!pet_expr_is_affine(expr_else
))
3833 expr_else
= pet_expr_filter(expr_else
,
3834 isl_map_copy(test_access
), 0);
3836 expr_else
= universally_false(ctx
);
3838 expr
= pet_expr_from_access(test_access
);
3839 expr
= pet_expr_new_ternary(ctx
, expr
, expr_then
, expr_else
);
3840 expr_skip
= pet_expr_from_access(isl_map_copy(skip_access
));
3842 expr_skip
->acc
.write
= 1;
3843 expr_skip
->acc
.read
= 0;
3845 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
3846 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, scan
->n_stmt
++, expr
);
3848 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
3849 scop
= scop_add_array(scop
, skip_access
, scan
->ast_context
);
3850 isl_map_free(skip_access
);
3854 isl_map_free(test_access
);
3855 isl_map_free(skip_access
);
3859 /* Is scop's skip_now condition equal to its skip_later condition?
3860 * In particular, this means that it either has no skip_now condition
3861 * or both a skip_now and a skip_later condition (that are equal to each other).
3863 static bool skip_equals_skip_later(struct pet_scop
*scop
)
3865 int has_skip_now
, has_skip_later
;
3867 isl_set
*skip_now
, *skip_later
;
3871 has_skip_now
= pet_scop_has_skip(scop
, pet_skip_now
);
3872 has_skip_later
= pet_scop_has_skip(scop
, pet_skip_later
);
3873 if (has_skip_now
!= has_skip_later
)
3878 skip_now
= pet_scop_get_skip(scop
, pet_skip_now
);
3879 skip_later
= pet_scop_get_skip(scop
, pet_skip_later
);
3880 equal
= isl_set_is_equal(skip_now
, skip_later
);
3881 isl_set_free(skip_now
);
3882 isl_set_free(skip_later
);
3887 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
3889 static void drop_skip_later(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
3891 pet_scop_reset_skip(scop1
, pet_skip_later
);
3892 pet_scop_reset_skip(scop2
, pet_skip_later
);
3895 /* Structure that handles the construction of skip conditions.
3897 * scop_then and scop_else represent the then and else branches
3898 * of the if statement
3900 * skip[type] is true if we need to construct a skip condition of that type
3901 * equal is set if the skip conditions of types pet_skip_now and pet_skip_later
3902 * are equal to each other
3903 * access[type] is the virtual array representing the skip condition
3904 * scop[type] is a scop for computing the skip condition
3906 struct pet_skip_info
{
3912 struct pet_scop
*scop
[2];
3914 pet_skip_info(isl_ctx
*ctx
) : ctx(ctx
) {}
3916 operator bool() { return skip
[pet_skip_now
] || skip
[pet_skip_later
]; }
3919 /* Structure that handles the construction of skip conditions on if statements.
3921 * scop_then and scop_else represent the then and else branches
3922 * of the if statement
3924 struct pet_skip_info_if
: public pet_skip_info
{
3925 struct pet_scop
*scop_then
, *scop_else
;
3928 pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
3929 struct pet_scop
*scop_else
, bool have_else
, bool affine
);
3930 void extract(PetScan
*scan
, __isl_keep isl_map
*access
,
3931 enum pet_skip type
);
3932 void extract(PetScan
*scan
, __isl_keep isl_map
*access
);
3933 void extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
);
3934 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
3936 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
3939 /* Initialize a pet_skip_info_if structure based on the then and else branches
3940 * and based on whether the if condition is affine or not.
3942 pet_skip_info_if::pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
3943 struct pet_scop
*scop_else
, bool have_else
, bool affine
) :
3944 pet_skip_info(ctx
), scop_then(scop_then
), scop_else(scop_else
),
3945 have_else(have_else
)
3947 skip
[pet_skip_now
] =
3948 need_skip(scop_then
, scop_else
, have_else
, pet_skip_now
, affine
);
3949 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop_then
) &&
3950 (!have_else
|| skip_equals_skip_later(scop_else
));
3951 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
3952 need_skip(scop_then
, scop_else
, have_else
, pet_skip_later
, affine
);
3955 /* If we need to construct a skip condition of the given type,
3958 * "map" represents the if condition.
3960 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_map
*map
,
3966 access
[type
] = create_test_access(isl_map_get_ctx(map
), scan
->n_test
++);
3967 scop
[type
] = extract_skip(scan
, isl_map_copy(map
),
3968 isl_map_copy(access
[type
]),
3969 scop_then
, scop_else
, have_else
, type
);
3972 /* Construct the required skip conditions, given the if condition "map".
3974 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_map
*map
)
3976 extract(scan
, map
, pet_skip_now
);
3977 extract(scan
, map
, pet_skip_later
);
3979 drop_skip_later(scop_then
, scop_else
);
3982 /* Construct the required skip conditions, given the if condition "cond".
3984 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
)
3989 if (!skip
[pet_skip_now
] && !skip
[pet_skip_later
])
3992 test_set
= isl_set_from_pw_aff(isl_pw_aff_copy(cond
));
3993 test
= isl_map_from_range(test_set
);
3994 extract(scan
, test
);
3998 /* Add the computed skip condition of the give type to "main" and
3999 * add the scop for computing the condition at the given offset.
4001 * If equal is set, then we only computed a skip condition for pet_skip_now,
4002 * but we also need to set it as main's pet_skip_later.
4004 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*main
,
4005 enum pet_skip type
, int offset
)
4012 skip_set
= isl_map_range(access
[type
]);
4013 access
[type
] = NULL
;
4014 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4015 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4019 main
= pet_scop_set_skip(main
, pet_skip_later
,
4020 isl_set_copy(skip_set
));
4022 main
= pet_scop_set_skip(main
, type
, skip_set
);
4027 /* Add the computed skip conditions to "main" and
4028 * add the scops for computing the conditions at the given offset.
4030 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*scop
, int offset
)
4032 scop
= add(scop
, pet_skip_now
, offset
);
4033 scop
= add(scop
, pet_skip_later
, offset
);
4038 /* Construct a pet_scop for a non-affine if statement.
4040 * We create a separate statement that writes the result
4041 * of the non-affine condition to a virtual scalar.
4042 * A constraint requiring the value of this virtual scalar to be one
4043 * is added to the iteration domains of the then branch.
4044 * Similarly, a constraint requiring the value of this virtual scalar
4045 * to be zero is added to the iteration domains of the else branch, if any.
4046 * We adjust the schedules to ensure that the virtual scalar is written
4047 * before it is read.
4049 * If there are any breaks or continues in the then and/or else
4050 * branches, then we may have to compute a new skip condition.
4051 * This is handled using a pet_skip_info_if object.
4052 * On initialization, the object checks if skip conditions need
4053 * to be computed. If so, it does so in "extract" and adds them in "add".
4055 struct pet_scop
*PetScan::extract_non_affine_if(Expr
*cond
,
4056 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
4057 bool have_else
, int stmt_id
)
4059 struct pet_scop
*scop
;
4060 isl_map
*test_access
;
4061 int save_n_stmt
= n_stmt
;
4063 test_access
= create_test_access(ctx
, n_test
++);
4065 scop
= extract_non_affine_condition(cond
, isl_map_copy(test_access
));
4066 n_stmt
= save_n_stmt
;
4067 scop
= scop_add_array(scop
, test_access
, ast_context
);
4069 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, have_else
, false);
4070 skip
.extract(this, test_access
);
4072 scop
= pet_scop_prefix(scop
, 0);
4073 scop_then
= pet_scop_prefix(scop_then
, 1);
4074 scop_then
= pet_scop_filter(scop_then
, isl_map_copy(test_access
), 1);
4076 scop_else
= pet_scop_prefix(scop_else
, 1);
4077 scop_else
= pet_scop_filter(scop_else
, test_access
, 0);
4078 scop_then
= pet_scop_add_par(ctx
, scop_then
, scop_else
);
4080 isl_map_free(test_access
);
4082 scop
= pet_scop_add_seq(ctx
, scop
, scop_then
);
4084 scop
= skip
.add(scop
, 2);
4089 /* Construct a pet_scop for an if statement.
4091 * If the condition fits the pattern of a conditional assignment,
4092 * then it is handled by extract_conditional_assignment.
4093 * Otherwise, we do the following.
4095 * If the condition is affine, then the condition is added
4096 * to the iteration domains of the then branch, while the
4097 * opposite of the condition in added to the iteration domains
4098 * of the else branch, if any.
4099 * We allow the condition to be dynamic, i.e., to refer to
4100 * scalars or array elements that may be written to outside
4101 * of the given if statement. These nested accesses are then represented
4102 * as output dimensions in the wrapping iteration domain.
4103 * If it also written _inside_ the then or else branch, then
4104 * we treat the condition as non-affine.
4105 * As explained in extract_non_affine_if, this will introduce
4106 * an extra statement.
4107 * For aesthetic reasons, we want this statement to have a statement
4108 * number that is lower than those of the then and else branches.
4109 * In order to evaluate if will need such a statement, however, we
4110 * first construct scops for the then and else branches.
4111 * We therefore reserve a statement number if we might have to
4112 * introduce such an extra statement.
4114 * If the condition is not affine, then the scop is created in
4115 * extract_non_affine_if.
4117 * If there are any breaks or continues in the then and/or else
4118 * branches, then we may have to compute a new skip condition.
4119 * This is handled using a pet_skip_info_if object.
4120 * On initialization, the object checks if skip conditions need
4121 * to be computed. If so, it does so in "extract" and adds them in "add".
4123 struct pet_scop
*PetScan::extract(IfStmt
*stmt
)
4125 struct pet_scop
*scop_then
, *scop_else
= NULL
, *scop
;
4131 scop
= extract_conditional_assignment(stmt
);
4135 cond
= try_extract_nested_condition(stmt
->getCond());
4136 if (allow_nested
&& (!cond
|| has_nested(cond
)))
4140 assigned_value_cache
cache(assigned_value
);
4141 scop_then
= extract(stmt
->getThen());
4144 if (stmt
->getElse()) {
4145 assigned_value_cache
cache(assigned_value
);
4146 scop_else
= extract(stmt
->getElse());
4147 if (options
->autodetect
) {
4148 if (scop_then
&& !scop_else
) {
4150 isl_pw_aff_free(cond
);
4153 if (!scop_then
&& scop_else
) {
4155 isl_pw_aff_free(cond
);
4162 (!is_nested_allowed(cond
, scop_then
) ||
4163 (stmt
->getElse() && !is_nested_allowed(cond
, scop_else
)))) {
4164 isl_pw_aff_free(cond
);
4167 if (allow_nested
&& !cond
)
4168 return extract_non_affine_if(stmt
->getCond(), scop_then
,
4169 scop_else
, stmt
->getElse(), stmt_id
);
4172 cond
= extract_condition(stmt
->getCond());
4174 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, stmt
->getElse(), true);
4175 skip
.extract(this, cond
);
4177 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
4178 set
= isl_pw_aff_non_zero_set(cond
);
4179 scop
= pet_scop_restrict(scop_then
, isl_set_copy(set
));
4181 if (stmt
->getElse()) {
4182 set
= isl_set_subtract(isl_set_copy(valid
), set
);
4183 scop_else
= pet_scop_restrict(scop_else
, set
);
4184 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
4187 scop
= resolve_nested(scop
);
4188 scop
= pet_scop_restrict_context(scop
, valid
);
4191 scop
= pet_scop_prefix(scop
, 0);
4192 scop
= skip
.add(scop
, 1);
4197 /* Try and construct a pet_scop for a label statement.
4198 * We currently only allow labels on expression statements.
4200 struct pet_scop
*PetScan::extract(LabelStmt
*stmt
)
4205 sub
= stmt
->getSubStmt();
4206 if (!isa
<Expr
>(sub
)) {
4211 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
4213 return extract(sub
, extract_expr(cast
<Expr
>(sub
)), label
);
4216 /* Construct a pet_scop for a continue statement.
4218 * We simply create an empty scop with a universal pet_skip_now
4219 * skip condition. This skip condition will then be taken into
4220 * account by the enclosing loop construct, possibly after
4221 * being incorporated into outer skip conditions.
4223 struct pet_scop
*PetScan::extract(ContinueStmt
*stmt
)
4229 scop
= pet_scop_empty(ctx
);
4233 space
= isl_space_set_alloc(ctx
, 0, 1);
4234 set
= isl_set_universe(space
);
4235 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
4236 scop
= pet_scop_set_skip(scop
, pet_skip_now
, set
);
4241 /* Construct a pet_scop for a break statement.
4243 * We simply create an empty scop with both a universal pet_skip_now
4244 * skip condition and a universal pet_skip_later skip condition.
4245 * These skip conditions will then be taken into
4246 * account by the enclosing loop construct, possibly after
4247 * being incorporated into outer skip conditions.
4249 struct pet_scop
*PetScan::extract(BreakStmt
*stmt
)
4255 scop
= pet_scop_empty(ctx
);
4259 space
= isl_space_set_alloc(ctx
, 0, 1);
4260 set
= isl_set_universe(space
);
4261 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
4262 scop
= pet_scop_set_skip(scop
, pet_skip_now
, isl_set_copy(set
));
4263 scop
= pet_scop_set_skip(scop
, pet_skip_later
, set
);
4268 /* Try and construct a pet_scop corresponding to "stmt".
4270 * If "stmt" is a compound statement, then "skip_declarations"
4271 * indicates whether we should skip initial declarations in the
4272 * compound statement.
4274 struct pet_scop
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
4276 if (isa
<Expr
>(stmt
))
4277 return extract(stmt
, extract_expr(cast
<Expr
>(stmt
)));
4279 switch (stmt
->getStmtClass()) {
4280 case Stmt::WhileStmtClass
:
4281 return extract(cast
<WhileStmt
>(stmt
));
4282 case Stmt::ForStmtClass
:
4283 return extract_for(cast
<ForStmt
>(stmt
));
4284 case Stmt::IfStmtClass
:
4285 return extract(cast
<IfStmt
>(stmt
));
4286 case Stmt::CompoundStmtClass
:
4287 return extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
4288 case Stmt::LabelStmtClass
:
4289 return extract(cast
<LabelStmt
>(stmt
));
4290 case Stmt::ContinueStmtClass
:
4291 return extract(cast
<ContinueStmt
>(stmt
));
4292 case Stmt::BreakStmtClass
:
4293 return extract(cast
<BreakStmt
>(stmt
));
4294 case Stmt::DeclStmtClass
:
4295 return extract(cast
<DeclStmt
>(stmt
));
4303 /* Do we need to construct a skip condition of the given type
4304 * on a sequence of statements?
4306 * There is no need to construct a new skip condition if only
4307 * only of the two statements has a skip condition or if both
4308 * of their skip conditions are affine.
4310 * In principle we also don't need a new continuation variable if
4311 * the continuation of scop2 is affine, but then we would need
4312 * to allow more complicated forms of continuations.
4314 static bool need_skip_seq(struct pet_scop
*scop1
, struct pet_scop
*scop2
,
4317 if (!scop1
|| !pet_scop_has_skip(scop1
, type
))
4319 if (!scop2
|| !pet_scop_has_skip(scop2
, type
))
4321 if (pet_scop_has_affine_skip(scop1
, type
) &&
4322 pet_scop_has_affine_skip(scop2
, type
))
4327 /* Construct a scop for computing the skip condition of the given type and
4328 * with access relation "skip_access" for a sequence of two scops "scop1"
4331 * The computed scop contains a single statement that essentially does
4333 * skip_cond = skip_cond_1 ? 1 : skip_cond_2
4335 * or, in other words, skip_cond1 || skip_cond2.
4336 * In this expression, skip_cond_2 is filtered to reflect that it is
4337 * only evaluated when skip_cond_1 is false.
4339 * The skip condition on scop1 is not removed because it still needs
4340 * to be applied to scop2 when these two scops are combined.
4342 static struct pet_scop
*extract_skip_seq(PetScan
*ps
,
4343 __isl_take isl_map
*skip_access
,
4344 struct pet_scop
*scop1
, struct pet_scop
*scop2
, enum pet_skip type
)
4347 struct pet_expr
*expr1
, *expr2
, *expr
, *expr_skip
;
4348 struct pet_stmt
*stmt
;
4349 struct pet_scop
*scop
;
4350 isl_ctx
*ctx
= ps
->ctx
;
4352 if (!scop1
|| !scop2
)
4355 expr1
= pet_scop_get_skip_expr(scop1
, type
);
4356 expr2
= pet_scop_get_skip_expr(scop2
, type
);
4357 pet_scop_reset_skip(scop2
, type
);
4359 expr2
= pet_expr_filter(expr2
, isl_map_copy(expr1
->acc
.access
), 0);
4361 expr
= universally_true(ctx
);
4362 expr
= pet_expr_new_ternary(ctx
, expr1
, expr
, expr2
);
4363 expr_skip
= pet_expr_from_access(isl_map_copy(skip_access
));
4365 expr_skip
->acc
.write
= 1;
4366 expr_skip
->acc
.read
= 0;
4368 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
4369 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, ps
->n_stmt
++, expr
);
4371 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
4372 scop
= scop_add_array(scop
, skip_access
, ps
->ast_context
);
4373 isl_map_free(skip_access
);
4377 isl_map_free(skip_access
);
4381 /* Structure that handles the construction of skip conditions
4382 * on sequences of statements.
4384 * scop1 and scop2 represent the two statements that are combined
4386 struct pet_skip_info_seq
: public pet_skip_info
{
4387 struct pet_scop
*scop1
, *scop2
;
4389 pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4390 struct pet_scop
*scop2
);
4391 void extract(PetScan
*scan
, enum pet_skip type
);
4392 void extract(PetScan
*scan
);
4393 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
4395 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
4398 /* Initialize a pet_skip_info_seq structure based on
4399 * on the two statements that are going to be combined.
4401 pet_skip_info_seq::pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4402 struct pet_scop
*scop2
) : pet_skip_info(ctx
), scop1(scop1
), scop2(scop2
)
4404 skip
[pet_skip_now
] = need_skip_seq(scop1
, scop2
, pet_skip_now
);
4405 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop1
) &&
4406 skip_equals_skip_later(scop2
);
4407 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
4408 need_skip_seq(scop1
, scop2
, pet_skip_later
);
4411 /* If we need to construct a skip condition of the given type,
4414 void pet_skip_info_seq::extract(PetScan
*scan
, enum pet_skip type
)
4419 access
[type
] = create_test_access(ctx
, scan
->n_test
++);
4420 scop
[type
] = extract_skip_seq(scan
, isl_map_copy(access
[type
]),
4421 scop1
, scop2
, type
);
4424 /* Construct the required skip conditions.
4426 void pet_skip_info_seq::extract(PetScan
*scan
)
4428 extract(scan
, pet_skip_now
);
4429 extract(scan
, pet_skip_later
);
4431 drop_skip_later(scop1
, scop2
);
4434 /* Add the computed skip condition of the give type to "main" and
4435 * add the scop for computing the condition at the given offset (the statement
4436 * number). Within this offset, the condition is computed at position 1
4437 * to ensure that it is computed after the corresponding statement.
4439 * If equal is set, then we only computed a skip condition for pet_skip_now,
4440 * but we also need to set it as main's pet_skip_later.
4442 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*main
,
4443 enum pet_skip type
, int offset
)
4450 skip_set
= isl_map_range(access
[type
]);
4451 access
[type
] = NULL
;
4452 scop
[type
] = pet_scop_prefix(scop
[type
], 1);
4453 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4454 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4458 main
= pet_scop_set_skip(main
, pet_skip_later
,
4459 isl_set_copy(skip_set
));
4461 main
= pet_scop_set_skip(main
, type
, skip_set
);
4466 /* Add the computed skip conditions to "main" and
4467 * add the scops for computing the conditions at the given offset.
4469 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*scop
, int offset
)
4471 scop
= add(scop
, pet_skip_now
, offset
);
4472 scop
= add(scop
, pet_skip_later
, offset
);
4477 /* Extract a clone of the kill statement in "scop".
4478 * "scop" is expected to have been created from a DeclStmt
4479 * and should have the kill as its first statement.
4481 struct pet_stmt
*PetScan::extract_kill(struct pet_scop
*scop
)
4483 struct pet_expr
*kill
;
4484 struct pet_stmt
*stmt
;
4489 if (scop
->n_stmt
< 1)
4490 isl_die(ctx
, isl_error_internal
,
4491 "expecting at least one statement", return NULL
);
4492 stmt
= scop
->stmts
[0];
4493 if (stmt
->body
->type
!= pet_expr_unary
||
4494 stmt
->body
->op
!= pet_op_kill
)
4495 isl_die(ctx
, isl_error_internal
,
4496 "expecting kill statement", return NULL
);
4498 access
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
4499 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
4500 kill
= pet_expr_kill_from_access(access
);
4501 return pet_stmt_from_pet_expr(ctx
, stmt
->line
, NULL
, n_stmt
++, kill
);
4504 /* Mark all arrays in "scop" as being exposed.
4506 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
4510 for (int i
= 0; i
< scop
->n_array
; ++i
)
4511 scop
->arrays
[i
]->exposed
= 1;
4515 /* Try and construct a pet_scop corresponding to (part of)
4516 * a sequence of statements.
4518 * "block" is set if the sequence respresents the children of
4519 * a compound statement.
4520 * "skip_declarations" is set if we should skip initial declarations
4521 * in the sequence of statements.
4523 * If there are any breaks or continues in the individual statements,
4524 * then we may have to compute a new skip condition.
4525 * This is handled using a pet_skip_info_seq object.
4526 * On initialization, the object checks if skip conditions need
4527 * to be computed. If so, it does so in "extract" and adds them in "add".
4529 * If "block" is set, then we need to insert kill statements at
4530 * the end of the block for any array that has been declared by
4531 * one of the statements in the sequence. Each of these declarations
4532 * results in the construction of a kill statement at the place
4533 * of the declaration, so we simply collect duplicates of
4534 * those kill statements and append these duplicates to the constructed scop.
4536 * If "block" is not set, then any array declared by one of the statements
4537 * in the sequence is marked as being exposed.
4539 struct pet_scop
*PetScan::extract(StmtRange stmt_range
, bool block
,
4540 bool skip_declarations
)
4545 bool partial_range
= false;
4546 set
<struct pet_stmt
*> kills
;
4547 set
<struct pet_stmt
*>::iterator it
;
4549 scop
= pet_scop_empty(ctx
);
4550 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
4552 struct pet_scop
*scop_i
;
4554 if (skip_declarations
&&
4555 child
->getStmtClass() == Stmt::DeclStmtClass
)
4558 scop_i
= extract(child
);
4559 if (scop
&& partial
) {
4560 pet_scop_free(scop_i
);
4563 pet_skip_info_seq
skip(ctx
, scop
, scop_i
);
4566 scop_i
= pet_scop_prefix(scop_i
, 0);
4567 if (scop_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
) {
4569 kills
.insert(extract_kill(scop_i
));
4571 scop_i
= mark_exposed(scop_i
);
4573 scop_i
= pet_scop_prefix(scop_i
, j
);
4574 if (options
->autodetect
) {
4576 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
4578 partial_range
= true;
4579 if (scop
->n_stmt
!= 0 && !scop_i
)
4582 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
4585 scop
= skip
.add(scop
, j
);
4591 for (it
= kills
.begin(); it
!= kills
.end(); ++it
) {
4593 scop_j
= pet_scop_from_pet_stmt(ctx
, *it
);
4594 scop_j
= pet_scop_prefix(scop_j
, j
);
4595 scop
= pet_scop_add_seq(ctx
, scop
, scop_j
);
4598 if (scop
&& partial_range
)
4604 /* Return the file offset of the expansion location of "Loc".
4606 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
4608 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
4611 /* Check if the scop marked by the user is exactly this Stmt
4612 * or part of this Stmt.
4613 * If so, return a pet_scop corresponding to the marked region.
4614 * Otherwise, return NULL.
4616 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
4618 SourceManager
&SM
= PP
.getSourceManager();
4619 unsigned start_off
, end_off
;
4621 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
4622 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
4624 if (start_off
> loc
.end
)
4626 if (end_off
< loc
.start
)
4628 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
4629 return extract(stmt
);
4633 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
4634 Stmt
*child
= *start
;
4637 start_off
= getExpansionOffset(SM
, child
->getLocStart());
4638 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
4639 if (start_off
< loc
.start
&& end_off
> loc
.end
)
4641 if (start_off
>= loc
.start
)
4646 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
4648 start_off
= SM
.getFileOffset(child
->getLocStart());
4649 if (start_off
>= loc
.end
)
4653 return extract(StmtRange(start
, end
), false, false);
4656 /* Set the size of index "pos" of "array" to "size".
4657 * In particular, add a constraint of the form
4661 * to array->extent and a constraint of the form
4665 * to array->context.
4667 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
4668 __isl_take isl_pw_aff
*size
)
4678 valid
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
));
4679 array
->context
= isl_set_intersect(array
->context
, valid
);
4681 dim
= isl_set_get_space(array
->extent
);
4682 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
4683 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
4684 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
4685 index
= isl_pw_aff_alloc(univ
, aff
);
4687 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
4688 isl_set_dim(array
->extent
, isl_dim_set
));
4689 id
= isl_set_get_tuple_id(array
->extent
);
4690 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
4691 bound
= isl_pw_aff_lt_set(index
, size
);
4693 array
->extent
= isl_set_intersect(array
->extent
, bound
);
4695 if (!array
->context
|| !array
->extent
)
4700 pet_array_free(array
);
4704 /* Figure out the size of the array at position "pos" and all
4705 * subsequent positions from "type" and update "array" accordingly.
4707 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
4708 const Type
*type
, int pos
)
4710 const ArrayType
*atype
;
4716 if (type
->isPointerType()) {
4717 type
= type
->getPointeeType().getTypePtr();
4718 return set_upper_bounds(array
, type
, pos
+ 1);
4720 if (!type
->isArrayType())
4723 type
= type
->getCanonicalTypeInternal().getTypePtr();
4724 atype
= cast
<ArrayType
>(type
);
4726 if (type
->isConstantArrayType()) {
4727 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
4728 size
= extract_affine(ca
->getSize());
4729 array
= update_size(array
, pos
, size
);
4730 } else if (type
->isVariableArrayType()) {
4731 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
4732 size
= extract_affine(vla
->getSizeExpr());
4733 array
= update_size(array
, pos
, size
);
4736 type
= atype
->getElementType().getTypePtr();
4738 return set_upper_bounds(array
, type
, pos
+ 1);
4741 /* Is "T" the type of a variable length array with static size?
4743 static bool is_vla_with_static_size(QualType T
)
4745 const VariableArrayType
*vlatype
;
4747 if (!T
->isVariableArrayType())
4749 vlatype
= cast
<VariableArrayType
>(T
);
4750 return vlatype
->getSizeModifier() == VariableArrayType::Static
;
4753 /* Return the type of "decl" as an array.
4755 * In particular, if "decl" is a parameter declaration that
4756 * is a variable length array with a static size, then
4757 * return the original type (i.e., the variable length array).
4758 * Otherwise, return the type of decl.
4760 static QualType
get_array_type(ValueDecl
*decl
)
4765 parm
= dyn_cast
<ParmVarDecl
>(decl
);
4767 return decl
->getType();
4769 T
= parm
->getOriginalType();
4770 if (!is_vla_with_static_size(T
))
4771 return decl
->getType();
4775 /* Construct and return a pet_array corresponding to the variable "decl".
4776 * In particular, initialize array->extent to
4778 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
4780 * and then call set_upper_bounds to set the upper bounds on the indices
4781 * based on the type of the variable.
4783 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
)
4785 struct pet_array
*array
;
4786 QualType qt
= get_array_type(decl
);
4787 const Type
*type
= qt
.getTypePtr();
4788 int depth
= array_depth(type
);
4789 QualType base
= base_type(qt
);
4794 array
= isl_calloc_type(ctx
, struct pet_array
);
4798 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
4799 dim
= isl_space_set_alloc(ctx
, 0, depth
);
4800 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
4802 array
->extent
= isl_set_nat_universe(dim
);
4804 dim
= isl_space_params_alloc(ctx
, 0);
4805 array
->context
= isl_set_universe(dim
);
4807 array
= set_upper_bounds(array
, type
, 0);
4811 name
= base
.getAsString();
4812 array
->element_type
= strdup(name
.c_str());
4813 array
->element_size
= decl
->getASTContext().getTypeInfo(base
).first
/ 8;
4818 /* Construct a list of pet_arrays, one for each array (or scalar)
4819 * accessed inside "scop", add this list to "scop" and return the result.
4821 * The context of "scop" is updated with the intersection of
4822 * the contexts of all arrays, i.e., constraints on the parameters
4823 * that ensure that the arrays have a valid (non-negative) size.
4825 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
)
4828 set
<ValueDecl
*> arrays
;
4829 set
<ValueDecl
*>::iterator it
;
4831 struct pet_array
**scop_arrays
;
4836 pet_scop_collect_arrays(scop
, arrays
);
4837 if (arrays
.size() == 0)
4840 n_array
= scop
->n_array
;
4842 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
4843 n_array
+ arrays
.size());
4846 scop
->arrays
= scop_arrays
;
4848 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
4849 struct pet_array
*array
;
4850 scop
->arrays
[n_array
+ i
] = array
= extract_array(ctx
, *it
);
4851 if (!scop
->arrays
[n_array
+ i
])
4854 scop
->context
= isl_set_intersect(scop
->context
,
4855 isl_set_copy(array
->context
));
4862 pet_scop_free(scop
);
4866 /* Bound all parameters in scop->context to the possible values
4867 * of the corresponding C variable.
4869 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
4876 n
= isl_set_dim(scop
->context
, isl_dim_param
);
4877 for (int i
= 0; i
< n
; ++i
) {
4881 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
4882 if (is_nested_parameter(id
)) {
4884 isl_die(isl_set_get_ctx(scop
->context
),
4886 "unresolved nested parameter", goto error
);
4888 decl
= (ValueDecl
*) isl_id_get_user(id
);
4891 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
4899 pet_scop_free(scop
);
4903 /* Construct a pet_scop from the given function.
4905 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
4910 stmt
= fd
->getBody();
4912 if (options
->autodetect
)
4913 scop
= extract(stmt
, true);
4916 scop
= pet_scop_detect_parameter_accesses(scop
);
4917 scop
= scan_arrays(scop
);
4918 scop
= add_parameter_bounds(scop
);
4919 scop
= pet_scop_gist(scop
, value_bounds
);