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 <clang/AST/ASTContext.h>
39 #include <clang/AST/ASTDiagnostic.h>
40 #include <clang/AST/Expr.h>
41 #include <clang/AST/RecursiveASTVisitor.h>
44 #include <isl/space.h>
51 #include "scop_plus.h"
56 using namespace clang
;
58 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
59 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
61 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
62 SourceLocation(), var
, false, var
->getInnerLocStart(),
63 var
->getType(), VK_LValue
);
65 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
66 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
68 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
69 SourceLocation(), var
, var
->getInnerLocStart(), var
->getType(),
73 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
75 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
76 var
, var
->getInnerLocStart(), var
->getType(), VK_LValue
);
80 /* Check if the element type corresponding to the given array type
81 * has a const qualifier.
83 static bool const_base(QualType qt
)
85 const Type
*type
= qt
.getTypePtr();
87 if (type
->isPointerType())
88 return const_base(type
->getPointeeType());
89 if (type
->isArrayType()) {
90 const ArrayType
*atype
;
91 type
= type
->getCanonicalTypeInternal().getTypePtr();
92 atype
= cast
<ArrayType
>(type
);
93 return const_base(atype
->getElementType());
96 return qt
.isConstQualified();
99 /* Mark "decl" as having an unknown value in "assigned_value".
101 * If no (known or unknown) value was assigned to "decl" before,
102 * then it may have been treated as a parameter before and may
103 * therefore appear in a value assigned to another variable.
104 * If so, this assignment needs to be turned into an unknown value too.
106 static void clear_assignment(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
,
109 map
<ValueDecl
*, isl_pw_aff
*>::iterator it
;
111 it
= assigned_value
.find(decl
);
113 assigned_value
[decl
] = NULL
;
115 if (it
== assigned_value
.end())
118 for (it
= assigned_value
.begin(); it
!= assigned_value
.end(); ++it
) {
119 isl_pw_aff
*pa
= it
->second
;
120 int nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
122 for (int i
= 0; i
< nparam
; ++i
) {
125 if (!isl_pw_aff_has_dim_id(pa
, isl_dim_param
, i
))
127 id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
128 if (isl_id_get_user(id
) == decl
)
135 /* Look for any assignments to scalar variables in part of the parse
136 * tree and set assigned_value to NULL for each of them.
137 * Also reset assigned_value if the address of a scalar variable
138 * is being taken. As an exception, if the address is passed to a function
139 * that is declared to receive a const pointer, then assigned_value is
142 * This ensures that we won't use any previously stored value
143 * in the current subtree and its parents.
145 struct clear_assignments
: RecursiveASTVisitor
<clear_assignments
> {
146 map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
;
147 set
<UnaryOperator
*> skip
;
149 clear_assignments(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
) :
150 assigned_value(assigned_value
) {}
152 /* Check for "address of" operators whose value is passed
153 * to a const pointer argument and add them to "skip", so that
154 * we can skip them in VisitUnaryOperator.
156 bool VisitCallExpr(CallExpr
*expr
) {
158 fd
= expr
->getDirectCallee();
161 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
162 Expr
*arg
= expr
->getArg(i
);
164 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
165 ImplicitCastExpr
*ice
;
166 ice
= cast
<ImplicitCastExpr
>(arg
);
167 arg
= ice
->getSubExpr();
169 if (arg
->getStmtClass() != Stmt::UnaryOperatorClass
)
171 op
= cast
<UnaryOperator
>(arg
);
172 if (op
->getOpcode() != UO_AddrOf
)
174 if (const_base(fd
->getParamDecl(i
)->getType()))
180 bool VisitUnaryOperator(UnaryOperator
*expr
) {
185 switch (expr
->getOpcode()) {
195 if (skip
.find(expr
) != skip
.end())
198 arg
= expr
->getSubExpr();
199 if (arg
->getStmtClass() != Stmt::DeclRefExprClass
)
201 ref
= cast
<DeclRefExpr
>(arg
);
202 decl
= ref
->getDecl();
203 clear_assignment(assigned_value
, decl
);
207 bool VisitBinaryOperator(BinaryOperator
*expr
) {
212 if (!expr
->isAssignmentOp())
214 lhs
= expr
->getLHS();
215 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
)
217 ref
= cast
<DeclRefExpr
>(lhs
);
218 decl
= ref
->getDecl();
219 clear_assignment(assigned_value
, decl
);
224 /* Keep a copy of the currently assigned values.
226 * Any variable that is assigned a value inside the current scope
227 * is removed again when we leave the scope (either because it wasn't
228 * stored in the cache or because it has a different value in the cache).
230 struct assigned_value_cache
{
231 map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
;
232 map
<ValueDecl
*, isl_pw_aff
*> cache
;
234 assigned_value_cache(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
) :
235 assigned_value(assigned_value
), cache(assigned_value
) {}
236 ~assigned_value_cache() {
237 map
<ValueDecl
*, isl_pw_aff
*>::iterator it
= cache
.begin();
238 for (it
= assigned_value
.begin(); it
!= assigned_value
.end();
241 (cache
.find(it
->first
) != cache
.end() &&
242 cache
[it
->first
] != it
->second
))
243 cache
[it
->first
] = NULL
;
245 assigned_value
= cache
;
249 /* Insert an expression into the collection of expressions,
250 * provided it is not already in there.
251 * The isl_pw_affs are freed in the destructor.
253 void PetScan::insert_expression(__isl_take isl_pw_aff
*expr
)
255 std::set
<isl_pw_aff
*>::iterator it
;
257 if (expressions
.find(expr
) == expressions
.end())
258 expressions
.insert(expr
);
260 isl_pw_aff_free(expr
);
265 std::set
<isl_pw_aff
*>::iterator it
;
267 for (it
= expressions
.begin(); it
!= expressions
.end(); ++it
)
268 isl_pw_aff_free(*it
);
270 isl_union_map_free(value_bounds
);
273 /* Called if we found something we (currently) cannot handle.
274 * We'll provide more informative warnings later.
276 * We only actually complain if autodetect is false.
278 void PetScan::unsupported(Stmt
*stmt
, const char *msg
)
280 if (options
->autodetect
)
283 SourceLocation loc
= stmt
->getLocStart();
284 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
285 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
286 msg
? msg
: "unsupported");
287 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
290 /* Extract an integer from "expr" and store it in "v".
292 int PetScan::extract_int(IntegerLiteral
*expr
, isl_int
*v
)
294 const Type
*type
= expr
->getType().getTypePtr();
295 int is_signed
= type
->hasSignedIntegerRepresentation();
298 int64_t i
= expr
->getValue().getSExtValue();
299 isl_int_set_si(*v
, i
);
301 uint64_t i
= expr
->getValue().getZExtValue();
302 isl_int_set_ui(*v
, i
);
308 /* Extract an integer from "expr" and store it in "v".
309 * Return -1 if "expr" does not (obviously) represent an integer.
311 int PetScan::extract_int(clang::ParenExpr
*expr
, isl_int
*v
)
313 return extract_int(expr
->getSubExpr(), v
);
316 /* Extract an integer from "expr" and store it in "v".
317 * Return -1 if "expr" does not (obviously) represent an integer.
319 int PetScan::extract_int(clang::Expr
*expr
, isl_int
*v
)
321 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
322 return extract_int(cast
<IntegerLiteral
>(expr
), v
);
323 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
324 return extract_int(cast
<ParenExpr
>(expr
), v
);
330 /* Extract an affine expression from the IntegerLiteral "expr".
332 __isl_give isl_pw_aff
*PetScan::extract_affine(IntegerLiteral
*expr
)
334 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
335 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
336 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
337 isl_set
*dom
= isl_set_universe(dim
);
341 extract_int(expr
, &v
);
342 aff
= isl_aff_add_constant(aff
, v
);
345 return isl_pw_aff_alloc(dom
, aff
);
348 /* Extract an affine expression from the APInt "val".
350 __isl_give isl_pw_aff
*PetScan::extract_affine(const llvm::APInt
&val
)
352 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
353 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
354 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
355 isl_set
*dom
= isl_set_universe(dim
);
359 isl_int_set_ui(v
, val
.getZExtValue());
360 aff
= isl_aff_add_constant(aff
, v
);
363 return isl_pw_aff_alloc(dom
, aff
);
366 __isl_give isl_pw_aff
*PetScan::extract_affine(ImplicitCastExpr
*expr
)
368 return extract_affine(expr
->getSubExpr());
371 static unsigned get_type_size(ValueDecl
*decl
)
373 return decl
->getASTContext().getIntWidth(decl
->getType());
376 /* Bound parameter "pos" of "set" to the possible values of "decl".
378 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
379 unsigned pos
, ValueDecl
*decl
)
386 width
= get_type_size(decl
);
387 if (decl
->getType()->isUnsignedIntegerType()) {
388 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
389 isl_int_set_si(v
, 1);
390 isl_int_mul_2exp(v
, v
, width
);
391 isl_int_sub_ui(v
, v
, 1);
392 set
= isl_set_upper_bound(set
, isl_dim_param
, pos
, v
);
394 isl_int_set_si(v
, 1);
395 isl_int_mul_2exp(v
, v
, width
- 1);
396 isl_int_sub_ui(v
, v
, 1);
397 set
= isl_set_upper_bound(set
, isl_dim_param
, pos
, v
);
399 isl_int_sub_ui(v
, v
, 1);
400 set
= isl_set_lower_bound(set
, isl_dim_param
, pos
, v
);
408 /* Extract an affine expression from the DeclRefExpr "expr".
410 * If the variable has been assigned a value, then we check whether
411 * we know what (affine) value was assigned.
412 * If so, we return this value. Otherwise we convert "expr"
413 * to an extra parameter (provided nesting_enabled is set).
415 * Otherwise, we simply return an expression that is equal
416 * to a parameter corresponding to the referenced variable.
418 __isl_give isl_pw_aff
*PetScan::extract_affine(DeclRefExpr
*expr
)
420 ValueDecl
*decl
= expr
->getDecl();
421 const Type
*type
= decl
->getType().getTypePtr();
427 if (!type
->isIntegerType()) {
432 if (assigned_value
.find(decl
) != assigned_value
.end()) {
433 if (assigned_value
[decl
])
434 return isl_pw_aff_copy(assigned_value
[decl
]);
436 return nested_access(expr
);
439 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
440 dim
= isl_space_params_alloc(ctx
, 1);
442 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
444 dom
= isl_set_universe(isl_space_copy(dim
));
445 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
446 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
448 return isl_pw_aff_alloc(dom
, aff
);
451 /* Extract an affine expression from an integer division operation.
452 * In particular, if "expr" is lhs/rhs, then return
454 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
456 * The second argument (rhs) is required to be a (positive) integer constant.
458 __isl_give isl_pw_aff
*PetScan::extract_affine_div(BinaryOperator
*expr
)
461 isl_pw_aff
*rhs
, *lhs
;
463 rhs
= extract_affine(expr
->getRHS());
464 is_cst
= isl_pw_aff_is_cst(rhs
);
465 if (is_cst
< 0 || !is_cst
) {
466 isl_pw_aff_free(rhs
);
472 lhs
= extract_affine(expr
->getLHS());
474 return isl_pw_aff_tdiv_q(lhs
, rhs
);
477 /* Extract an affine expression from a modulo operation.
478 * In particular, if "expr" is lhs/rhs, then return
480 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
482 * The second argument (rhs) is required to be a (positive) integer constant.
484 __isl_give isl_pw_aff
*PetScan::extract_affine_mod(BinaryOperator
*expr
)
487 isl_pw_aff
*rhs
, *lhs
;
489 rhs
= extract_affine(expr
->getRHS());
490 is_cst
= isl_pw_aff_is_cst(rhs
);
491 if (is_cst
< 0 || !is_cst
) {
492 isl_pw_aff_free(rhs
);
498 lhs
= extract_affine(expr
->getLHS());
500 return isl_pw_aff_tdiv_r(lhs
, rhs
);
503 /* Extract an affine expression from a multiplication operation.
504 * This is only allowed if at least one of the two arguments
505 * is a (piecewise) constant.
507 __isl_give isl_pw_aff
*PetScan::extract_affine_mul(BinaryOperator
*expr
)
512 lhs
= extract_affine(expr
->getLHS());
513 rhs
= extract_affine(expr
->getRHS());
515 if (!isl_pw_aff_is_cst(lhs
) && !isl_pw_aff_is_cst(rhs
)) {
516 isl_pw_aff_free(lhs
);
517 isl_pw_aff_free(rhs
);
522 return isl_pw_aff_mul(lhs
, rhs
);
525 /* Extract an affine expression from an addition or subtraction operation.
527 __isl_give isl_pw_aff
*PetScan::extract_affine_add(BinaryOperator
*expr
)
532 lhs
= extract_affine(expr
->getLHS());
533 rhs
= extract_affine(expr
->getRHS());
535 switch (expr
->getOpcode()) {
537 return isl_pw_aff_add(lhs
, rhs
);
539 return isl_pw_aff_sub(lhs
, rhs
);
541 isl_pw_aff_free(lhs
);
542 isl_pw_aff_free(rhs
);
552 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
,
558 isl_int_set_si(mod
, 1);
559 isl_int_mul_2exp(mod
, mod
, width
);
561 pwaff
= isl_pw_aff_mod(pwaff
, mod
);
568 /* Limit the domain of "pwaff" to those elements where the function
571 * 2^{width-1} <= pwaff < 2^{width-1}
573 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
577 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
578 isl_local_space
*ls
= isl_local_space_from_space(space
);
584 isl_int_set_si(v
, 1);
585 isl_int_mul_2exp(v
, v
, width
- 1);
587 bound
= isl_aff_zero_on_domain(ls
);
588 bound
= isl_aff_add_constant(bound
, v
);
589 b
= isl_pw_aff_from_aff(bound
);
591 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
592 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
594 b
= isl_pw_aff_neg(b
);
595 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
596 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
603 /* Handle potential overflows on signed computations.
605 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
606 * the we adjust the domain of "pa" to avoid overflows.
608 __isl_give isl_pw_aff
*PetScan::signed_overflow(__isl_take isl_pw_aff
*pa
,
611 if (options
->signed_overflow
== PET_OVERFLOW_AVOID
)
612 pa
= avoid_overflow(pa
, width
);
617 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
619 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
620 __isl_take isl_set
*dom
)
623 pa
= isl_set_indicator_function(set
);
624 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
628 /* Extract an affine expression from some binary operations.
629 * If the result of the expression is unsigned, then we wrap it
630 * based on the size of the type. Otherwise, we ensure that
631 * no overflow occurs.
633 __isl_give isl_pw_aff
*PetScan::extract_affine(BinaryOperator
*expr
)
638 switch (expr
->getOpcode()) {
641 res
= extract_affine_add(expr
);
644 res
= extract_affine_div(expr
);
647 res
= extract_affine_mod(expr
);
650 res
= extract_affine_mul(expr
);
660 return extract_condition(expr
);
666 width
= ast_context
.getIntWidth(expr
->getType());
667 if (expr
->getType()->isUnsignedIntegerType())
668 res
= wrap(res
, width
);
670 res
= signed_overflow(res
, width
);
675 /* Extract an affine expression from a negation operation.
677 __isl_give isl_pw_aff
*PetScan::extract_affine(UnaryOperator
*expr
)
679 if (expr
->getOpcode() == UO_Minus
)
680 return isl_pw_aff_neg(extract_affine(expr
->getSubExpr()));
681 if (expr
->getOpcode() == UO_LNot
)
682 return extract_condition(expr
);
688 __isl_give isl_pw_aff
*PetScan::extract_affine(ParenExpr
*expr
)
690 return extract_affine(expr
->getSubExpr());
693 /* Extract an affine expression from some special function calls.
694 * In particular, we handle "min", "max", "ceild" and "floord".
695 * In case of the latter two, the second argument needs to be
696 * a (positive) integer constant.
698 __isl_give isl_pw_aff
*PetScan::extract_affine(CallExpr
*expr
)
702 isl_pw_aff
*aff1
, *aff2
;
704 fd
= expr
->getDirectCallee();
710 name
= fd
->getDeclName().getAsString();
711 if (!(expr
->getNumArgs() == 2 && name
== "min") &&
712 !(expr
->getNumArgs() == 2 && name
== "max") &&
713 !(expr
->getNumArgs() == 2 && name
== "floord") &&
714 !(expr
->getNumArgs() == 2 && name
== "ceild")) {
719 if (name
== "min" || name
== "max") {
720 aff1
= extract_affine(expr
->getArg(0));
721 aff2
= extract_affine(expr
->getArg(1));
724 aff1
= isl_pw_aff_min(aff1
, aff2
);
726 aff1
= isl_pw_aff_max(aff1
, aff2
);
727 } else if (name
== "floord" || name
== "ceild") {
729 Expr
*arg2
= expr
->getArg(1);
731 if (arg2
->getStmtClass() != Stmt::IntegerLiteralClass
) {
735 aff1
= extract_affine(expr
->getArg(0));
737 extract_int(cast
<IntegerLiteral
>(arg2
), &v
);
738 aff1
= isl_pw_aff_scale_down(aff1
, v
);
740 if (name
== "floord")
741 aff1
= isl_pw_aff_floor(aff1
);
743 aff1
= isl_pw_aff_ceil(aff1
);
752 /* This method is called when we come across an access that is
753 * nested in what is supposed to be an affine expression.
754 * If nesting is allowed, we return a new parameter that corresponds
755 * to this nested access. Otherwise, we simply complain.
757 * Note that we currently don't allow nested accesses themselves
758 * to contain any nested accesses, so we check if we can extract
759 * the access without any nesting and complain if we can't.
761 * The new parameter is resolved in resolve_nested.
763 isl_pw_aff
*PetScan::nested_access(Expr
*expr
)
771 if (!nesting_enabled
) {
776 allow_nested
= false;
777 access
= extract_access(expr
);
783 isl_map_free(access
);
785 id
= isl_id_alloc(ctx
, NULL
, expr
);
786 dim
= isl_space_params_alloc(ctx
, 1);
788 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
790 dom
= isl_set_universe(isl_space_copy(dim
));
791 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
792 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
794 return isl_pw_aff_alloc(dom
, aff
);
797 /* Affine expressions are not supposed to contain array accesses,
798 * but if nesting is allowed, we return a parameter corresponding
799 * to the array access.
801 __isl_give isl_pw_aff
*PetScan::extract_affine(ArraySubscriptExpr
*expr
)
803 return nested_access(expr
);
806 /* Extract an affine expression from a conditional operation.
808 __isl_give isl_pw_aff
*PetScan::extract_affine(ConditionalOperator
*expr
)
810 isl_pw_aff
*cond
, *lhs
, *rhs
, *res
;
812 cond
= extract_condition(expr
->getCond());
813 lhs
= extract_affine(expr
->getTrueExpr());
814 rhs
= extract_affine(expr
->getFalseExpr());
816 return isl_pw_aff_cond(cond
, lhs
, rhs
);
819 /* Extract an affine expression, if possible, from "expr".
820 * Otherwise return NULL.
822 __isl_give isl_pw_aff
*PetScan::extract_affine(Expr
*expr
)
824 switch (expr
->getStmtClass()) {
825 case Stmt::ImplicitCastExprClass
:
826 return extract_affine(cast
<ImplicitCastExpr
>(expr
));
827 case Stmt::IntegerLiteralClass
:
828 return extract_affine(cast
<IntegerLiteral
>(expr
));
829 case Stmt::DeclRefExprClass
:
830 return extract_affine(cast
<DeclRefExpr
>(expr
));
831 case Stmt::BinaryOperatorClass
:
832 return extract_affine(cast
<BinaryOperator
>(expr
));
833 case Stmt::UnaryOperatorClass
:
834 return extract_affine(cast
<UnaryOperator
>(expr
));
835 case Stmt::ParenExprClass
:
836 return extract_affine(cast
<ParenExpr
>(expr
));
837 case Stmt::CallExprClass
:
838 return extract_affine(cast
<CallExpr
>(expr
));
839 case Stmt::ArraySubscriptExprClass
:
840 return extract_affine(cast
<ArraySubscriptExpr
>(expr
));
841 case Stmt::ConditionalOperatorClass
:
842 return extract_affine(cast
<ConditionalOperator
>(expr
));
849 __isl_give isl_map
*PetScan::extract_access(ImplicitCastExpr
*expr
)
851 return extract_access(expr
->getSubExpr());
854 /* Return the depth of an array of the given type.
856 static int array_depth(const Type
*type
)
858 if (type
->isPointerType())
859 return 1 + array_depth(type
->getPointeeType().getTypePtr());
860 if (type
->isArrayType()) {
861 const ArrayType
*atype
;
862 type
= type
->getCanonicalTypeInternal().getTypePtr();
863 atype
= cast
<ArrayType
>(type
);
864 return 1 + array_depth(atype
->getElementType().getTypePtr());
869 /* Return the element type of the given array type.
871 static QualType
base_type(QualType qt
)
873 const Type
*type
= qt
.getTypePtr();
875 if (type
->isPointerType())
876 return base_type(type
->getPointeeType());
877 if (type
->isArrayType()) {
878 const ArrayType
*atype
;
879 type
= type
->getCanonicalTypeInternal().getTypePtr();
880 atype
= cast
<ArrayType
>(type
);
881 return base_type(atype
->getElementType());
886 /* Extract an access relation from a reference to a variable.
887 * If the variable has name "A" and its type corresponds to an
888 * array of depth d, then the returned access relation is of the
891 * { [] -> A[i_1,...,i_d] }
893 __isl_give isl_map
*PetScan::extract_access(DeclRefExpr
*expr
)
895 return extract_access(expr
->getDecl());
898 /* Extract an access relation from a variable.
899 * If the variable has name "A" and its type corresponds to an
900 * array of depth d, then the returned access relation is of the
903 * { [] -> A[i_1,...,i_d] }
905 __isl_give isl_map
*PetScan::extract_access(ValueDecl
*decl
)
907 int depth
= array_depth(decl
->getType().getTypePtr());
908 isl_id
*id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
909 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, depth
);
912 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
914 access_rel
= isl_map_universe(dim
);
919 /* Extract an access relation from an integer contant.
920 * If the value of the constant is "v", then the returned access relation
925 __isl_give isl_map
*PetScan::extract_access(IntegerLiteral
*expr
)
927 return isl_map_from_range(isl_set_from_pw_aff(extract_affine(expr
)));
930 /* Try and extract an access relation from the given Expr.
931 * Return NULL if it doesn't work out.
933 __isl_give isl_map
*PetScan::extract_access(Expr
*expr
)
935 switch (expr
->getStmtClass()) {
936 case Stmt::ImplicitCastExprClass
:
937 return extract_access(cast
<ImplicitCastExpr
>(expr
));
938 case Stmt::DeclRefExprClass
:
939 return extract_access(cast
<DeclRefExpr
>(expr
));
940 case Stmt::ArraySubscriptExprClass
:
941 return extract_access(cast
<ArraySubscriptExpr
>(expr
));
942 case Stmt::IntegerLiteralClass
:
943 return extract_access(cast
<IntegerLiteral
>(expr
));
950 /* Assign the affine expression "index" to the output dimension "pos" of "map",
951 * restrict the domain to those values that result in a non-negative index
952 * and return the result.
954 __isl_give isl_map
*set_index(__isl_take isl_map
*map
, int pos
,
955 __isl_take isl_pw_aff
*index
)
958 int len
= isl_map_dim(map
, isl_dim_out
);
962 domain
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(index
));
963 index
= isl_pw_aff_intersect_domain(index
, domain
);
964 index_map
= isl_map_from_range(isl_set_from_pw_aff(index
));
965 index_map
= isl_map_insert_dims(index_map
, isl_dim_out
, 0, pos
);
966 index_map
= isl_map_add_dims(index_map
, isl_dim_out
, len
- pos
- 1);
967 id
= isl_map_get_tuple_id(map
, isl_dim_out
);
968 index_map
= isl_map_set_tuple_id(index_map
, isl_dim_out
, id
);
970 map
= isl_map_intersect(map
, index_map
);
975 /* Extract an access relation from the given array subscript expression.
976 * If nesting is allowed in general, then we turn it on while
977 * examining the index expression.
979 * We first extract an access relation from the base.
980 * This will result in an access relation with a range that corresponds
981 * to the array being accessed and with earlier indices filled in already.
982 * We then extract the current index and fill that in as well.
983 * The position of the current index is based on the type of base.
984 * If base is the actual array variable, then the depth of this type
985 * will be the same as the depth of the array and we will fill in
986 * the first array index.
987 * Otherwise, the depth of the base type will be smaller and we will fill
990 __isl_give isl_map
*PetScan::extract_access(ArraySubscriptExpr
*expr
)
992 Expr
*base
= expr
->getBase();
993 Expr
*idx
= expr
->getIdx();
995 isl_map
*base_access
;
997 int depth
= array_depth(base
->getType().getTypePtr());
999 bool save_nesting
= nesting_enabled
;
1001 nesting_enabled
= allow_nested
;
1003 base_access
= extract_access(base
);
1004 index
= extract_affine(idx
);
1006 nesting_enabled
= save_nesting
;
1008 pos
= isl_map_dim(base_access
, isl_dim_out
) - depth
;
1009 access
= set_index(base_access
, pos
, index
);
1014 /* Check if "expr" calls function "minmax" with two arguments and if so
1015 * make lhs and rhs refer to these two arguments.
1017 static bool is_minmax(Expr
*expr
, const char *minmax
, Expr
*&lhs
, Expr
*&rhs
)
1023 if (expr
->getStmtClass() != Stmt::CallExprClass
)
1026 call
= cast
<CallExpr
>(expr
);
1027 fd
= call
->getDirectCallee();
1031 if (call
->getNumArgs() != 2)
1034 name
= fd
->getDeclName().getAsString();
1038 lhs
= call
->getArg(0);
1039 rhs
= call
->getArg(1);
1044 /* Check if "expr" is of the form min(lhs, rhs) and if so make
1045 * lhs and rhs refer to the two arguments.
1047 static bool is_min(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
1049 return is_minmax(expr
, "min", lhs
, rhs
);
1052 /* Check if "expr" is of the form max(lhs, rhs) and if so make
1053 * lhs and rhs refer to the two arguments.
1055 static bool is_max(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
1057 return is_minmax(expr
, "max", lhs
, rhs
);
1060 /* Return "lhs && rhs", defined on the shared definition domain.
1062 static __isl_give isl_pw_aff
*pw_aff_and(__isl_take isl_pw_aff
*lhs
,
1063 __isl_take isl_pw_aff
*rhs
)
1068 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
1069 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1070 cond
= isl_set_intersect(isl_pw_aff_non_zero_set(lhs
),
1071 isl_pw_aff_non_zero_set(rhs
));
1072 return indicator_function(cond
, dom
);
1075 /* Return "lhs && rhs", with shortcut semantics.
1076 * That is, if lhs is false, then the result is defined even if rhs is not.
1077 * In practice, we compute lhs ? rhs : lhs.
1079 static __isl_give isl_pw_aff
*pw_aff_and_then(__isl_take isl_pw_aff
*lhs
,
1080 __isl_take isl_pw_aff
*rhs
)
1082 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), rhs
, lhs
);
1085 /* Return "lhs || rhs", with shortcut semantics.
1086 * That is, if lhs is true, then the result is defined even if rhs is not.
1087 * In practice, we compute lhs ? lhs : rhs.
1089 static __isl_give isl_pw_aff
*pw_aff_or_else(__isl_take isl_pw_aff
*lhs
,
1090 __isl_take isl_pw_aff
*rhs
)
1092 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), lhs
, rhs
);
1095 /* Extract an affine expressions representing the comparison "LHS op RHS"
1096 * "comp" is the original statement that "LHS op RHS" is derived from
1097 * and is used for diagnostics.
1099 * If the comparison is of the form
1103 * then the expression is constructed as the conjunction of
1108 * A similar optimization is performed for max(a,b) <= c.
1109 * We do this because that will lead to simpler representations
1110 * of the expression.
1111 * If isl is ever enhanced to explicitly deal with min and max expressions,
1112 * this optimization can be removed.
1114 __isl_give isl_pw_aff
*PetScan::extract_comparison(BinaryOperatorKind op
,
1115 Expr
*LHS
, Expr
*RHS
, Stmt
*comp
)
1124 return extract_comparison(BO_LT
, RHS
, LHS
, comp
);
1126 return extract_comparison(BO_LE
, RHS
, LHS
, comp
);
1128 if (op
== BO_LT
|| op
== BO_LE
) {
1129 Expr
*expr1
, *expr2
;
1130 if (is_min(RHS
, expr1
, expr2
)) {
1131 lhs
= extract_comparison(op
, LHS
, expr1
, comp
);
1132 rhs
= extract_comparison(op
, LHS
, expr2
, comp
);
1133 return pw_aff_and(lhs
, rhs
);
1135 if (is_max(LHS
, expr1
, expr2
)) {
1136 lhs
= extract_comparison(op
, expr1
, RHS
, comp
);
1137 rhs
= extract_comparison(op
, expr2
, RHS
, comp
);
1138 return pw_aff_and(lhs
, rhs
);
1142 lhs
= extract_affine(LHS
);
1143 rhs
= extract_affine(RHS
);
1145 dom
= isl_pw_aff_domain(isl_pw_aff_copy(lhs
));
1146 dom
= isl_set_intersect(dom
, isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1150 cond
= isl_pw_aff_lt_set(lhs
, rhs
);
1153 cond
= isl_pw_aff_le_set(lhs
, rhs
);
1156 cond
= isl_pw_aff_eq_set(lhs
, rhs
);
1159 cond
= isl_pw_aff_ne_set(lhs
, rhs
);
1162 isl_pw_aff_free(lhs
);
1163 isl_pw_aff_free(rhs
);
1169 cond
= isl_set_coalesce(cond
);
1170 res
= indicator_function(cond
, dom
);
1175 __isl_give isl_pw_aff
*PetScan::extract_comparison(BinaryOperator
*comp
)
1177 return extract_comparison(comp
->getOpcode(), comp
->getLHS(),
1178 comp
->getRHS(), comp
);
1181 /* Extract an affine expression representing the negation (logical not)
1182 * of a subexpression.
1184 __isl_give isl_pw_aff
*PetScan::extract_boolean(UnaryOperator
*op
)
1186 isl_set
*set_cond
, *dom
;
1187 isl_pw_aff
*cond
, *res
;
1189 cond
= extract_condition(op
->getSubExpr());
1191 dom
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1193 set_cond
= isl_pw_aff_zero_set(cond
);
1195 res
= indicator_function(set_cond
, dom
);
1200 /* Extract an affine expression representing the disjunction (logical or)
1201 * or conjunction (logical and) of two subexpressions.
1203 __isl_give isl_pw_aff
*PetScan::extract_boolean(BinaryOperator
*comp
)
1205 isl_pw_aff
*lhs
, *rhs
;
1207 lhs
= extract_condition(comp
->getLHS());
1208 rhs
= extract_condition(comp
->getRHS());
1210 switch (comp
->getOpcode()) {
1212 return pw_aff_and_then(lhs
, rhs
);
1214 return pw_aff_or_else(lhs
, rhs
);
1216 isl_pw_aff_free(lhs
);
1217 isl_pw_aff_free(rhs
);
1224 __isl_give isl_pw_aff
*PetScan::extract_condition(UnaryOperator
*expr
)
1226 switch (expr
->getOpcode()) {
1228 return extract_boolean(expr
);
1235 /* Extract the affine expression "expr != 0 ? 1 : 0".
1237 __isl_give isl_pw_aff
*PetScan::extract_implicit_condition(Expr
*expr
)
1242 res
= extract_affine(expr
);
1244 dom
= isl_pw_aff_domain(isl_pw_aff_copy(res
));
1245 set
= isl_pw_aff_non_zero_set(res
);
1247 res
= indicator_function(set
, dom
);
1252 /* Extract an affine expression from a boolean expression.
1253 * In particular, return the expression "expr ? 1 : 0".
1255 * If the expression doesn't look like a condition, we assume it
1256 * is an affine expression and return the condition "expr != 0 ? 1 : 0".
1258 __isl_give isl_pw_aff
*PetScan::extract_condition(Expr
*expr
)
1260 BinaryOperator
*comp
;
1263 isl_set
*u
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
1264 return indicator_function(u
, isl_set_copy(u
));
1267 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
1268 return extract_condition(cast
<ParenExpr
>(expr
)->getSubExpr());
1270 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
)
1271 return extract_condition(cast
<UnaryOperator
>(expr
));
1273 if (expr
->getStmtClass() != Stmt::BinaryOperatorClass
)
1274 return extract_implicit_condition(expr
);
1276 comp
= cast
<BinaryOperator
>(expr
);
1277 switch (comp
->getOpcode()) {
1284 return extract_comparison(comp
);
1287 return extract_boolean(comp
);
1289 return extract_implicit_condition(expr
);
1293 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
1297 return pet_op_minus
;
1299 return pet_op_post_inc
;
1301 return pet_op_post_dec
;
1303 return pet_op_pre_inc
;
1305 return pet_op_pre_dec
;
1311 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
1315 return pet_op_add_assign
;
1317 return pet_op_sub_assign
;
1319 return pet_op_mul_assign
;
1321 return pet_op_div_assign
;
1323 return pet_op_assign
;
1347 /* Construct a pet_expr representing a unary operator expression.
1349 struct pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
1351 struct pet_expr
*arg
;
1352 enum pet_op_type op
;
1354 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
1355 if (op
== pet_op_last
) {
1360 arg
= extract_expr(expr
->getSubExpr());
1362 if (expr
->isIncrementDecrementOp() &&
1363 arg
&& arg
->type
== pet_expr_access
) {
1368 return pet_expr_new_unary(ctx
, op
, arg
);
1371 /* Mark the given access pet_expr as a write.
1372 * If a scalar is being accessed, then mark its value
1373 * as unknown in assigned_value.
1375 void PetScan::mark_write(struct pet_expr
*access
)
1383 access
->acc
.write
= 1;
1384 access
->acc
.read
= 0;
1386 if (isl_map_dim(access
->acc
.access
, isl_dim_out
) != 0)
1389 id
= isl_map_get_tuple_id(access
->acc
.access
, isl_dim_out
);
1390 decl
= (ValueDecl
*) isl_id_get_user(id
);
1391 clear_assignment(assigned_value
, decl
);
1395 /* Assign "rhs" to "lhs".
1397 * In particular, if "lhs" is a scalar variable, then mark
1398 * the variable as having been assigned. If, furthermore, "rhs"
1399 * is an affine expression, then keep track of this value in assigned_value
1400 * so that we can plug it in when we later come across the same variable.
1402 void PetScan::assign(struct pet_expr
*lhs
, Expr
*rhs
)
1410 if (lhs
->type
!= pet_expr_access
)
1412 if (isl_map_dim(lhs
->acc
.access
, isl_dim_out
) != 0)
1415 id
= isl_map_get_tuple_id(lhs
->acc
.access
, isl_dim_out
);
1416 decl
= (ValueDecl
*) isl_id_get_user(id
);
1419 pa
= try_extract_affine(rhs
);
1420 clear_assignment(assigned_value
, decl
);
1423 assigned_value
[decl
] = pa
;
1424 insert_expression(pa
);
1427 /* Construct a pet_expr representing a binary operator expression.
1429 * If the top level operator is an assignment and the LHS is an access,
1430 * then we mark that access as a write. If the operator is a compound
1431 * assignment, the access is marked as both a read and a write.
1433 * If "expr" assigns something to a scalar variable, then we mark
1434 * the variable as having been assigned. If, furthermore, the expression
1435 * is affine, then keep track of this value in assigned_value
1436 * so that we can plug it in when we later come across the same variable.
1438 struct pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
1440 struct pet_expr
*lhs
, *rhs
;
1441 enum pet_op_type op
;
1443 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
1444 if (op
== pet_op_last
) {
1449 lhs
= extract_expr(expr
->getLHS());
1450 rhs
= extract_expr(expr
->getRHS());
1452 if (expr
->isAssignmentOp() && lhs
&& lhs
->type
== pet_expr_access
) {
1454 if (expr
->isCompoundAssignmentOp())
1458 if (expr
->getOpcode() == BO_Assign
)
1459 assign(lhs
, expr
->getRHS());
1461 return pet_expr_new_binary(ctx
, op
, lhs
, rhs
);
1464 /* Construct a pet_scop with a single statement killing the entire
1467 struct pet_scop
*PetScan::kill(Stmt
*stmt
, struct pet_array
*array
)
1470 struct pet_expr
*expr
;
1474 access
= isl_map_from_range(isl_set_copy(array
->extent
));
1475 expr
= pet_expr_kill_from_access(access
);
1476 return extract(stmt
, expr
);
1479 /* Construct a pet_scop for a (single) variable declaration.
1481 * The scop contains the variable being declared (as an array)
1482 * and a statement killing the array.
1484 * If the variable is initialized in the AST, then the scop
1485 * also contains an assignment to the variable.
1487 struct pet_scop
*PetScan::extract(DeclStmt
*stmt
)
1491 struct pet_expr
*lhs
, *rhs
, *pe
;
1492 struct pet_scop
*scop_decl
, *scop
;
1493 struct pet_array
*array
;
1495 if (!stmt
->isSingleDecl()) {
1500 decl
= stmt
->getSingleDecl();
1501 vd
= cast
<VarDecl
>(decl
);
1503 array
= extract_array(ctx
, vd
);
1505 array
->declared
= 1;
1506 scop_decl
= kill(stmt
, array
);
1507 scop_decl
= pet_scop_add_array(scop_decl
, array
);
1512 lhs
= pet_expr_from_access(extract_access(vd
));
1513 rhs
= extract_expr(vd
->getInit());
1516 assign(lhs
, vd
->getInit());
1518 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, lhs
, rhs
);
1519 scop
= extract(stmt
, pe
);
1521 scop_decl
= pet_scop_prefix(scop_decl
, 0);
1522 scop
= pet_scop_prefix(scop
, 1);
1524 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
1529 /* Construct a pet_expr representing a conditional operation.
1531 * We first try to extract the condition as an affine expression.
1532 * If that fails, we construct a pet_expr tree representing the condition.
1534 struct pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
1536 struct pet_expr
*cond
, *lhs
, *rhs
;
1539 pa
= try_extract_affine(expr
->getCond());
1541 isl_set
*test
= isl_set_from_pw_aff(pa
);
1542 cond
= pet_expr_from_access(isl_map_from_range(test
));
1544 cond
= extract_expr(expr
->getCond());
1545 lhs
= extract_expr(expr
->getTrueExpr());
1546 rhs
= extract_expr(expr
->getFalseExpr());
1548 return pet_expr_new_ternary(ctx
, cond
, lhs
, rhs
);
1551 struct pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
1553 return extract_expr(expr
->getSubExpr());
1556 /* Construct a pet_expr representing a floating point value.
1558 struct pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
1560 return pet_expr_new_double(ctx
, expr
->getValueAsApproximateDouble());
1563 /* Extract an access relation from "expr" and then convert it into
1566 struct pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
1569 struct pet_expr
*pe
;
1571 access
= extract_access(expr
);
1573 pe
= pet_expr_from_access(access
);
1578 struct pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
1580 return extract_expr(expr
->getSubExpr());
1583 /* Construct a pet_expr representing a function call.
1585 * If we are passing along a pointer to an array element
1586 * or an entire row or even higher dimensional slice of an array,
1587 * then the function being called may write into the array.
1589 * We assume here that if the function is declared to take a pointer
1590 * to a const type, then the function will perform a read
1591 * and that otherwise, it will perform a write.
1593 struct pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1595 struct pet_expr
*res
= NULL
;
1599 fd
= expr
->getDirectCallee();
1605 name
= fd
->getDeclName().getAsString();
1606 res
= pet_expr_new_call(ctx
, name
.c_str(), expr
->getNumArgs());
1610 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
1611 Expr
*arg
= expr
->getArg(i
);
1615 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
1616 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(arg
);
1617 arg
= ice
->getSubExpr();
1619 if (arg
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1620 UnaryOperator
*op
= cast
<UnaryOperator
>(arg
);
1621 if (op
->getOpcode() == UO_AddrOf
) {
1623 arg
= op
->getSubExpr();
1626 res
->args
[i
] = PetScan::extract_expr(arg
);
1627 main_arg
= res
->args
[i
];
1629 res
->args
[i
] = pet_expr_new_unary(ctx
,
1630 pet_op_address_of
, res
->args
[i
]);
1633 if (arg
->getStmtClass() == Stmt::ArraySubscriptExprClass
&&
1634 array_depth(arg
->getType().getTypePtr()) > 0)
1636 if (is_addr
&& main_arg
->type
== pet_expr_access
) {
1638 if (!fd
->hasPrototype()) {
1639 unsupported(expr
, "prototype required");
1642 parm
= fd
->getParamDecl(i
);
1643 if (!const_base(parm
->getType()))
1644 mark_write(main_arg
);
1654 /* Try and onstruct a pet_expr representing "expr".
1656 struct pet_expr
*PetScan::extract_expr(Expr
*expr
)
1658 switch (expr
->getStmtClass()) {
1659 case Stmt::UnaryOperatorClass
:
1660 return extract_expr(cast
<UnaryOperator
>(expr
));
1661 case Stmt::CompoundAssignOperatorClass
:
1662 case Stmt::BinaryOperatorClass
:
1663 return extract_expr(cast
<BinaryOperator
>(expr
));
1664 case Stmt::ImplicitCastExprClass
:
1665 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1666 case Stmt::ArraySubscriptExprClass
:
1667 case Stmt::DeclRefExprClass
:
1668 case Stmt::IntegerLiteralClass
:
1669 return extract_access_expr(expr
);
1670 case Stmt::FloatingLiteralClass
:
1671 return extract_expr(cast
<FloatingLiteral
>(expr
));
1672 case Stmt::ParenExprClass
:
1673 return extract_expr(cast
<ParenExpr
>(expr
));
1674 case Stmt::ConditionalOperatorClass
:
1675 return extract_expr(cast
<ConditionalOperator
>(expr
));
1676 case Stmt::CallExprClass
:
1677 return extract_expr(cast
<CallExpr
>(expr
));
1684 /* Check if the given initialization statement is an assignment.
1685 * If so, return that assignment. Otherwise return NULL.
1687 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1689 BinaryOperator
*ass
;
1691 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1694 ass
= cast
<BinaryOperator
>(init
);
1695 if (ass
->getOpcode() != BO_Assign
)
1701 /* Check if the given initialization statement is a declaration
1702 * of a single variable.
1703 * If so, return that declaration. Otherwise return NULL.
1705 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1709 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1712 decl
= cast
<DeclStmt
>(init
);
1714 if (!decl
->isSingleDecl())
1717 return decl
->getSingleDecl();
1720 /* Given the assignment operator in the initialization of a for loop,
1721 * extract the induction variable, i.e., the (integer)variable being
1724 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1731 lhs
= init
->getLHS();
1732 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1737 ref
= cast
<DeclRefExpr
>(lhs
);
1738 decl
= ref
->getDecl();
1739 type
= decl
->getType().getTypePtr();
1741 if (!type
->isIntegerType()) {
1749 /* Given the initialization statement of a for loop and the single
1750 * declaration in this initialization statement,
1751 * extract the induction variable, i.e., the (integer) variable being
1754 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1758 vd
= cast
<VarDecl
>(decl
);
1760 const QualType type
= vd
->getType();
1761 if (!type
->isIntegerType()) {
1766 if (!vd
->getInit()) {
1774 /* Check that op is of the form iv++ or iv--.
1775 * Return an affine expression "1" or "-1" accordingly.
1777 __isl_give isl_pw_aff
*PetScan::extract_unary_increment(
1778 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1785 if (!op
->isIncrementDecrementOp()) {
1790 sub
= op
->getSubExpr();
1791 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1796 ref
= cast
<DeclRefExpr
>(sub
);
1797 if (ref
->getDecl() != iv
) {
1802 space
= isl_space_params_alloc(ctx
, 0);
1803 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1805 if (op
->isIncrementOp())
1806 aff
= isl_aff_add_constant_si(aff
, 1);
1808 aff
= isl_aff_add_constant_si(aff
, -1);
1810 return isl_pw_aff_from_aff(aff
);
1813 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
1814 * has a single constant expression, then put this constant in *user.
1815 * The caller is assumed to have checked that this function will
1816 * be called exactly once.
1818 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
1821 isl_int
*inc
= (isl_int
*)user
;
1824 if (isl_aff_is_cst(aff
))
1825 isl_aff_get_constant(aff
, inc
);
1835 /* Check if op is of the form
1839 * and return inc as an affine expression.
1841 * We extract an affine expression from the RHS, subtract iv and return
1844 __isl_give isl_pw_aff
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1845 clang::ValueDecl
*iv
)
1854 if (op
->getOpcode() != BO_Assign
) {
1860 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1865 ref
= cast
<DeclRefExpr
>(lhs
);
1866 if (ref
->getDecl() != iv
) {
1871 val
= extract_affine(op
->getRHS());
1873 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
1875 dim
= isl_space_params_alloc(ctx
, 1);
1876 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1877 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1878 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1880 val
= isl_pw_aff_sub(val
, isl_pw_aff_from_aff(aff
));
1885 /* Check that op is of the form iv += cst or iv -= cst
1886 * and return an affine expression corresponding oto cst or -cst accordingly.
1888 __isl_give isl_pw_aff
*PetScan::extract_compound_increment(
1889 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1895 BinaryOperatorKind opcode
;
1897 opcode
= op
->getOpcode();
1898 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1902 if (opcode
== BO_SubAssign
)
1906 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1911 ref
= cast
<DeclRefExpr
>(lhs
);
1912 if (ref
->getDecl() != iv
) {
1917 val
= extract_affine(op
->getRHS());
1919 val
= isl_pw_aff_neg(val
);
1924 /* Check that the increment of the given for loop increments
1925 * (or decrements) the induction variable "iv" and return
1926 * the increment as an affine expression if successful.
1928 __isl_give isl_pw_aff
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1931 Stmt
*inc
= stmt
->getInc();
1938 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1939 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1940 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1941 return extract_compound_increment(
1942 cast
<CompoundAssignOperator
>(inc
), iv
);
1943 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1944 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1950 /* Embed the given iteration domain in an extra outer loop
1951 * with induction variable "var".
1952 * If this variable appeared as a parameter in the constraints,
1953 * it is replaced by the new outermost dimension.
1955 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
1956 __isl_take isl_id
*var
)
1960 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
1961 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
1963 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
1964 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
1971 /* Return those elements in the space of "cond" that come after
1972 * (based on "sign") an element in "cond".
1974 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
1976 isl_map
*previous_to_this
;
1979 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
1981 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
1983 cond
= isl_set_apply(cond
, previous_to_this
);
1988 /* Create the infinite iteration domain
1990 * { [id] : id >= 0 }
1992 * If "scop" has an affine skip of type pet_skip_later,
1993 * then remove those iterations i that have an earlier iteration
1994 * where the skip condition is satisfied, meaning that iteration i
1996 * Since we are dealing with a loop without loop iterator,
1997 * the skip condition cannot refer to the current loop iterator and
1998 * so effectively, the returned set is of the form
2000 * { [0]; [id] : id >= 1 and not skip }
2002 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
,
2003 struct pet_scop
*scop
)
2005 isl_ctx
*ctx
= isl_id_get_ctx(id
);
2009 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
2010 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
2012 if (!pet_scop_has_affine_skip(scop
, pet_skip_later
))
2015 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
2016 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 1);
2017 skip
= isl_set_params(skip
);
2018 skip
= embed(skip
, isl_id_copy(id
));
2019 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2020 domain
= isl_set_subtract(domain
, after(skip
, 1));
2025 /* Create an identity mapping on the space containing "domain".
2027 static __isl_give isl_map
*identity_map(__isl_keep isl_set
*domain
)
2032 space
= isl_space_map_from_set(isl_set_get_space(domain
));
2033 id
= isl_map_identity(space
);
2038 /* Add a filter to "scop" that imposes that it is only executed
2039 * when "break_access" has a zero value for all previous iterations
2042 * The input "break_access" has a zero-dimensional domain and range.
2044 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
2045 __isl_take isl_map
*break_access
, __isl_take isl_set
*domain
, int sign
)
2047 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2051 id_test
= isl_map_get_tuple_id(break_access
, isl_dim_out
);
2052 break_access
= isl_map_add_dims(break_access
, isl_dim_in
, 1);
2053 break_access
= isl_map_add_dims(break_access
, isl_dim_out
, 1);
2054 break_access
= isl_map_intersect_range(break_access
, domain
);
2055 break_access
= isl_map_set_tuple_id(break_access
, isl_dim_out
, id_test
);
2057 prev
= isl_map_lex_gt_first(isl_map_get_space(break_access
), 1);
2059 prev
= isl_map_lex_lt_first(isl_map_get_space(break_access
), 1);
2060 break_access
= isl_map_intersect(break_access
, prev
);
2061 scop
= pet_scop_filter(scop
, break_access
, 0);
2062 scop
= pet_scop_merge_filters(scop
);
2067 /* Construct a pet_scop for an infinite loop around the given body.
2069 * We extract a pet_scop for the body and then embed it in a loop with
2078 * If the body contains any break, then it is taken into
2079 * account in infinite_domain (if the skip condition is affine)
2080 * or in scop_add_break (if the skip condition is not affine).
2082 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
)
2088 struct pet_scop
*scop
;
2091 scop
= extract(body
);
2095 id
= isl_id_alloc(ctx
, "t", NULL
);
2096 domain
= infinite_domain(isl_id_copy(id
), scop
);
2097 ident
= identity_map(domain
);
2099 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
2101 access
= pet_scop_get_skip_map(scop
, pet_skip_later
);
2103 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
2104 isl_map_copy(ident
), ident
, id
);
2106 scop
= scop_add_break(scop
, access
, domain
, 1);
2108 isl_set_free(domain
);
2113 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
2119 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
)
2121 return extract_infinite_loop(stmt
->getBody());
2124 /* Create an access to a virtual array representing the result
2126 * Unlike other accessed data, the id of the array is NULL as
2127 * there is no ValueDecl in the program corresponding to the virtual
2129 * The array starts out as a scalar, but grows along with the
2130 * statement writing to the array in pet_scop_embed.
2132 static __isl_give isl_map
*create_test_access(isl_ctx
*ctx
, int test_nr
)
2134 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, 0);
2138 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
2139 id
= isl_id_alloc(ctx
, name
, NULL
);
2140 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
2141 return isl_map_universe(dim
);
2144 /* Add an array with the given extent ("access") to the list
2145 * of arrays in "scop" and return the extended pet_scop.
2146 * The array is marked as attaining values 0 and 1 only and
2147 * as each element being assigned at most once.
2149 static struct pet_scop
*scop_add_array(struct pet_scop
*scop
,
2150 __isl_keep isl_map
*access
, clang::ASTContext
&ast_ctx
)
2152 isl_ctx
*ctx
= isl_map_get_ctx(access
);
2154 struct pet_array
*array
;
2161 array
= isl_calloc_type(ctx
, struct pet_array
);
2165 array
->extent
= isl_map_range(isl_map_copy(access
));
2166 dim
= isl_space_params_alloc(ctx
, 0);
2167 array
->context
= isl_set_universe(dim
);
2168 dim
= isl_space_set_alloc(ctx
, 0, 1);
2169 array
->value_bounds
= isl_set_universe(dim
);
2170 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
2172 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
2174 array
->element_type
= strdup("int");
2175 array
->element_size
= ast_ctx
.getTypeInfo(ast_ctx
.IntTy
).first
/ 8;
2176 array
->uniquely_defined
= 1;
2178 if (!array
->extent
|| !array
->context
)
2179 array
= pet_array_free(array
);
2181 scop
= pet_scop_add_array(scop
, array
);
2185 pet_scop_free(scop
);
2189 /* Construct a pet_scop for a while loop of the form
2194 * In particular, construct a scop for an infinite loop around body and
2195 * intersect the domain with the affine expression.
2196 * Note that this intersection may result in an empty loop.
2198 struct pet_scop
*PetScan::extract_affine_while(__isl_take isl_pw_aff
*pa
,
2201 struct pet_scop
*scop
;
2205 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2206 dom
= isl_pw_aff_non_zero_set(pa
);
2207 scop
= extract_infinite_loop(body
);
2208 scop
= pet_scop_restrict(scop
, dom
);
2209 scop
= pet_scop_restrict_context(scop
, valid
);
2214 /* Construct a scop for a while, given the scops for the condition
2215 * and the body, the filter access and the iteration domain of
2218 * In particular, the scop for the condition is filtered to depend
2219 * on "test_access" evaluating to true for all previous iterations
2220 * of the loop, while the scop for the body is filtered to depend
2221 * on "test_access" evaluating to true for all iterations up to the
2222 * current iteration.
2224 * These filtered scops are then combined into a single scop.
2226 * "sign" is positive if the iterator increases and negative
2229 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
2230 struct pet_scop
*scop_body
, __isl_take isl_map
*test_access
,
2231 __isl_take isl_set
*domain
, int sign
)
2233 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2237 id_test
= isl_map_get_tuple_id(test_access
, isl_dim_out
);
2238 test_access
= isl_map_add_dims(test_access
, isl_dim_in
, 1);
2239 test_access
= isl_map_add_dims(test_access
, isl_dim_out
, 1);
2240 test_access
= isl_map_intersect_range(test_access
, domain
);
2241 test_access
= isl_map_set_tuple_id(test_access
, isl_dim_out
, id_test
);
2243 prev
= isl_map_lex_ge_first(isl_map_get_space(test_access
), 1);
2245 prev
= isl_map_lex_le_first(isl_map_get_space(test_access
), 1);
2246 test_access
= isl_map_intersect(test_access
, prev
);
2247 scop_body
= pet_scop_filter(scop_body
, isl_map_copy(test_access
), 1);
2249 prev
= isl_map_lex_gt_first(isl_map_get_space(test_access
), 1);
2251 prev
= isl_map_lex_lt_first(isl_map_get_space(test_access
), 1);
2252 test_access
= isl_map_intersect(test_access
, prev
);
2253 scop_cond
= pet_scop_filter(scop_cond
, test_access
, 1);
2255 return pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
2258 /* Check if the while loop is of the form
2260 * while (affine expression)
2263 * If so, call extract_affine_while to construct a scop.
2265 * Otherwise, construct a generic while scop, with iteration domain
2266 * { [t] : t >= 0 }. The scop consists of two parts, one for
2267 * evaluating the condition and one for the body.
2268 * The schedule is adjusted to reflect that the condition is evaluated
2269 * before the body is executed and the body is filtered to depend
2270 * on the result of the condition evaluating to true on all iterations
2271 * up to the current iteration, while the evaluation the condition itself
2272 * is filtered to depend on the result of the condition evaluating to true
2273 * on all previous iterations.
2274 * The context of the scop representing the body is dropped
2275 * because we don't know how many times the body will be executed,
2278 * If the body contains any break, then it is taken into
2279 * account in infinite_domain (if the skip condition is affine)
2280 * or in scop_add_break (if the skip condition is not affine).
2282 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
)
2286 isl_map
*test_access
;
2290 struct pet_scop
*scop
, *scop_body
;
2292 isl_map
*break_access
;
2294 cond
= stmt
->getCond();
2300 clear_assignments
clear(assigned_value
);
2301 clear
.TraverseStmt(stmt
->getBody());
2303 pa
= try_extract_affine_condition(cond
);
2305 return extract_affine_while(pa
, stmt
->getBody());
2307 if (!allow_nested
) {
2312 test_access
= create_test_access(ctx
, n_test
++);
2313 scop
= extract_non_affine_condition(cond
, isl_map_copy(test_access
));
2314 scop
= scop_add_array(scop
, test_access
, ast_context
);
2315 scop_body
= extract(stmt
->getBody());
2317 id
= isl_id_alloc(ctx
, "t", NULL
);
2318 domain
= infinite_domain(isl_id_copy(id
), scop_body
);
2319 ident
= identity_map(domain
);
2321 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
2323 break_access
= pet_scop_get_skip_map(scop_body
, pet_skip_later
);
2325 scop
= pet_scop_prefix(scop
, 0);
2326 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_map_copy(ident
),
2327 isl_map_copy(ident
), isl_id_copy(id
));
2328 scop_body
= pet_scop_reset_context(scop_body
);
2329 scop_body
= pet_scop_prefix(scop_body
, 1);
2330 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
2331 isl_map_copy(ident
), ident
, id
);
2333 if (has_var_break
) {
2334 scop
= scop_add_break(scop
, isl_map_copy(break_access
),
2335 isl_set_copy(domain
), 1);
2336 scop_body
= scop_add_break(scop_body
, break_access
,
2337 isl_set_copy(domain
), 1);
2339 scop
= scop_add_while(scop
, scop_body
, test_access
, domain
, 1);
2344 /* Check whether "cond" expresses a simple loop bound
2345 * on the only set dimension.
2346 * In particular, if "up" is set then "cond" should contain only
2347 * upper bounds on the set dimension.
2348 * Otherwise, it should contain only lower bounds.
2350 static bool is_simple_bound(__isl_keep isl_set
*cond
, isl_int inc
)
2352 if (isl_int_is_pos(inc
))
2353 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
2355 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
2358 /* Extend a condition on a given iteration of a loop to one that
2359 * imposes the same condition on all previous iterations.
2360 * "domain" expresses the lower [upper] bound on the iterations
2361 * when inc is positive [negative].
2363 * In particular, we construct the condition (when inc is positive)
2365 * forall i' : (domain(i') and i' <= i) => cond(i')
2367 * which is equivalent to
2369 * not exists i' : domain(i') and i' <= i and not cond(i')
2371 * We construct this set by negating cond, applying a map
2373 * { [i'] -> [i] : domain(i') and i' <= i }
2375 * and then negating the result again.
2377 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
2378 __isl_take isl_set
*domain
, isl_int inc
)
2380 isl_map
*previous_to_this
;
2382 if (isl_int_is_pos(inc
))
2383 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
2385 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
2387 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
2389 cond
= isl_set_complement(cond
);
2390 cond
= isl_set_apply(cond
, previous_to_this
);
2391 cond
= isl_set_complement(cond
);
2396 /* Construct a domain of the form
2398 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
2400 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
2401 __isl_take isl_pw_aff
*init
, isl_int inc
)
2407 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
2408 dim
= isl_pw_aff_get_domain_space(init
);
2409 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2410 aff
= isl_aff_add_coefficient(aff
, isl_dim_in
, 0, inc
);
2411 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
2413 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
2414 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
2415 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2416 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
2418 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
2420 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
2422 return isl_set_params(set
);
2425 /* Assuming "cond" represents a bound on a loop where the loop
2426 * iterator "iv" is incremented (or decremented) by one, check if wrapping
2429 * Under the given assumptions, wrapping is only possible if "cond" allows
2430 * for the last value before wrapping, i.e., 2^width - 1 in case of an
2431 * increasing iterator and 0 in case of a decreasing iterator.
2433 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
, isl_int inc
)
2439 test
= isl_set_copy(cond
);
2441 isl_int_init(limit
);
2442 if (isl_int_is_neg(inc
))
2443 isl_int_set_si(limit
, 0);
2445 isl_int_set_si(limit
, 1);
2446 isl_int_mul_2exp(limit
, limit
, get_type_size(iv
));
2447 isl_int_sub_ui(limit
, limit
, 1);
2450 test
= isl_set_fix(cond
, isl_dim_set
, 0, limit
);
2451 cw
= !isl_set_is_empty(test
);
2454 isl_int_clear(limit
);
2459 /* Given a one-dimensional space, construct the following mapping on this
2462 * { [v] -> [v mod 2^width] }
2464 * where width is the number of bits used to represent the values
2465 * of the unsigned variable "iv".
2467 static __isl_give isl_map
*compute_wrapping(__isl_take isl_space
*dim
,
2475 isl_int_set_si(mod
, 1);
2476 isl_int_mul_2exp(mod
, mod
, get_type_size(iv
));
2478 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2479 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2480 aff
= isl_aff_mod(aff
, mod
);
2484 return isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2485 map
= isl_map_reverse(map
);
2488 /* Project out the parameter "id" from "set".
2490 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
2491 __isl_keep isl_id
*id
)
2495 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
2497 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2502 /* Compute the set of parameters for which "set1" is a subset of "set2".
2504 * set1 is a subset of set2 if
2506 * forall i in set1 : i in set2
2510 * not exists i in set1 and i not in set2
2514 * not exists i in set1 \ set2
2516 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
2517 __isl_take isl_set
*set2
)
2519 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
2522 /* Compute the set of parameter values for which "cond" holds
2523 * on the next iteration for each element of "dom".
2525 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2526 * and then compute the set of parameters for which the result is a subset
2529 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
2530 __isl_take isl_set
*dom
, isl_int inc
)
2536 space
= isl_set_get_space(dom
);
2537 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2538 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2539 aff
= isl_aff_add_constant(aff
, inc
);
2540 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2542 dom
= isl_set_apply(dom
, next
);
2544 return enforce_subset(dom
, cond
);
2547 /* Does "id" refer to a nested access?
2549 static bool is_nested_parameter(__isl_keep isl_id
*id
)
2551 return id
&& isl_id_get_user(id
) && !isl_id_get_name(id
);
2554 /* Does parameter "pos" of "space" refer to a nested access?
2556 static bool is_nested_parameter(__isl_keep isl_space
*space
, int pos
)
2561 id
= isl_space_get_dim_id(space
, isl_dim_param
, pos
);
2562 nested
= is_nested_parameter(id
);
2568 /* Does "space" involve any parameters that refer to nested
2569 * accesses, i.e., parameters with no name?
2571 static bool has_nested(__isl_keep isl_space
*space
)
2575 nparam
= isl_space_dim(space
, isl_dim_param
);
2576 for (int i
= 0; i
< nparam
; ++i
)
2577 if (is_nested_parameter(space
, i
))
2583 /* Does "pa" involve any parameters that refer to nested
2584 * accesses, i.e., parameters with no name?
2586 static bool has_nested(__isl_keep isl_pw_aff
*pa
)
2591 space
= isl_pw_aff_get_space(pa
);
2592 nested
= has_nested(space
);
2593 isl_space_free(space
);
2598 /* Construct a pet_scop for a for statement.
2599 * The for loop is required to be of the form
2601 * for (i = init; condition; ++i)
2605 * for (i = init; condition; --i)
2607 * The initialization of the for loop should either be an assignment
2608 * to an integer variable, or a declaration of such a variable with
2611 * The condition is allowed to contain nested accesses, provided
2612 * they are not being written to inside the body of the loop.
2613 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2614 * essentially treated as a while loop, with iteration domain
2615 * { [i] : i >= init }.
2617 * We extract a pet_scop for the body and then embed it in a loop with
2618 * iteration domain and schedule
2620 * { [i] : i >= init and condition' }
2625 * { [i] : i <= init and condition' }
2628 * Where condition' is equal to condition if the latter is
2629 * a simple upper [lower] bound and a condition that is extended
2630 * to apply to all previous iterations otherwise.
2632 * If the condition is non-affine, then we drop the condition from the
2633 * iteration domain and instead create a separate statement
2634 * for evaluating the condition. The body is then filtered to depend
2635 * on the result of the condition evaluating to true on all iterations
2636 * up to the current iteration, while the evaluation the condition itself
2637 * is filtered to depend on the result of the condition evaluating to true
2638 * on all previous iterations.
2639 * The context of the scop representing the body is dropped
2640 * because we don't know how many times the body will be executed,
2643 * If the stride of the loop is not 1, then "i >= init" is replaced by
2645 * (exists a: i = init + stride * a and a >= 0)
2647 * If the loop iterator i is unsigned, then wrapping may occur.
2648 * During the computation, we work with a virtual iterator that
2649 * does not wrap. However, the condition in the code applies
2650 * to the wrapped value, so we need to change condition(i)
2651 * into condition([i % 2^width]).
2652 * After computing the virtual domain and schedule, we apply
2653 * the function { [v] -> [v % 2^width] } to the domain and the domain
2654 * of the schedule. In order not to lose any information, we also
2655 * need to intersect the domain of the schedule with the virtual domain
2656 * first, since some iterations in the wrapped domain may be scheduled
2657 * several times, typically an infinite number of times.
2658 * Note that there may be no need to perform this final wrapping
2659 * if the loop condition (after wrapping) satisfies certain conditions.
2660 * However, the is_simple_bound condition is not enough since it doesn't
2661 * check if there even is an upper bound.
2663 * If the loop condition is non-affine, then we keep the virtual
2664 * iterator in the iteration domain and instead replace all accesses
2665 * to the original iterator by the wrapping of the virtual iterator.
2667 * Wrapping on unsigned iterators can be avoided entirely if
2668 * loop condition is simple, the loop iterator is incremented
2669 * [decremented] by one and the last value before wrapping cannot
2670 * possibly satisfy the loop condition.
2672 * Before extracting a pet_scop from the body we remove all
2673 * assignments in assigned_value to variables that are assigned
2674 * somewhere in the body of the loop.
2676 * Valid parameters for a for loop are those for which the initial
2677 * value itself, the increment on each domain iteration and
2678 * the condition on both the initial value and
2679 * the result of incrementing the iterator for each iteration of the domain
2681 * If the loop condition is non-affine, then we only consider validity
2682 * of the initial value.
2684 * If the body contains any break, then we keep track of it in "skip"
2685 * (if the skip condition is affine) or it is handled in scop_add_break
2686 * (if the skip condition is not affine).
2687 * Note that the affine break condition needs to be considered with
2688 * respect to previous iterations in the virtual domain (if any)
2689 * and that the domain needs to be kept virtual if there is a non-affine
2692 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
)
2694 BinaryOperator
*ass
;
2702 isl_set
*cond
= NULL
;
2703 isl_set
*skip
= NULL
;
2705 struct pet_scop
*scop
, *scop_cond
= NULL
;
2706 assigned_value_cache
cache(assigned_value
);
2712 bool keep_virtual
= false;
2713 bool has_affine_break
;
2715 isl_map
*wrap
= NULL
;
2716 isl_pw_aff
*pa
, *pa_inc
, *init_val
;
2717 isl_set
*valid_init
;
2718 isl_set
*valid_cond
;
2719 isl_set
*valid_cond_init
;
2720 isl_set
*valid_cond_next
;
2722 isl_map
*test_access
= NULL
, *break_access
= NULL
;
2725 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
2726 return extract_infinite_for(stmt
);
2728 init
= stmt
->getInit();
2733 if ((ass
= initialization_assignment(init
)) != NULL
) {
2734 iv
= extract_induction_variable(ass
);
2737 lhs
= ass
->getLHS();
2738 rhs
= ass
->getRHS();
2739 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
2740 VarDecl
*var
= extract_induction_variable(init
, decl
);
2744 rhs
= var
->getInit();
2745 lhs
= create_DeclRefExpr(var
);
2747 unsupported(stmt
->getInit());
2751 pa_inc
= extract_increment(stmt
, iv
);
2756 if (isl_pw_aff_n_piece(pa_inc
) != 1 ||
2757 isl_pw_aff_foreach_piece(pa_inc
, &extract_cst
, &inc
) < 0) {
2758 isl_pw_aff_free(pa_inc
);
2759 unsupported(stmt
->getInc());
2763 valid_inc
= isl_pw_aff_domain(pa_inc
);
2765 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
2767 assigned_value
.erase(iv
);
2768 clear_assignments
clear(assigned_value
);
2769 clear
.TraverseStmt(stmt
->getBody());
2771 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
2773 pa
= try_extract_nested_condition(stmt
->getCond());
2774 if (allow_nested
&& (!pa
|| has_nested(pa
)))
2777 scop
= extract(stmt
->getBody());
2779 has_affine_break
= scop
&&
2780 pet_scop_has_affine_skip(scop
, pet_skip_later
);
2781 if (has_affine_break
) {
2782 skip
= pet_scop_get_skip(scop
, pet_skip_later
);
2783 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 1);
2784 skip
= isl_set_params(skip
);
2786 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
2787 if (has_var_break
) {
2788 break_access
= pet_scop_get_skip_map(scop
, pet_skip_later
);
2789 keep_virtual
= true;
2792 if (pa
&& !is_nested_allowed(pa
, scop
)) {
2793 isl_pw_aff_free(pa
);
2797 if (!allow_nested
&& !pa
)
2798 pa
= try_extract_affine_condition(stmt
->getCond());
2799 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2800 cond
= isl_pw_aff_non_zero_set(pa
);
2801 if (allow_nested
&& !cond
) {
2802 int save_n_stmt
= n_stmt
;
2803 test_access
= create_test_access(ctx
, n_test
++);
2805 scop_cond
= extract_non_affine_condition(stmt
->getCond(),
2806 isl_map_copy(test_access
));
2807 n_stmt
= save_n_stmt
;
2808 scop_cond
= scop_add_array(scop_cond
, test_access
, ast_context
);
2809 scop_cond
= pet_scop_prefix(scop_cond
, 0);
2810 scop
= pet_scop_reset_context(scop
);
2811 scop
= pet_scop_prefix(scop
, 1);
2812 keep_virtual
= true;
2813 cond
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2816 cond
= embed(cond
, isl_id_copy(id
));
2817 skip
= embed(skip
, isl_id_copy(id
));
2818 valid_cond
= isl_set_coalesce(valid_cond
);
2819 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
2820 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
2821 is_one
= isl_int_is_one(inc
) || isl_int_is_negone(inc
);
2822 is_virtual
= is_unsigned
&& (!is_one
|| can_wrap(cond
, iv
, inc
));
2824 init_val
= extract_affine(rhs
);
2825 valid_cond_init
= enforce_subset(
2826 isl_set_from_pw_aff(isl_pw_aff_copy(init_val
)),
2827 isl_set_copy(valid_cond
));
2828 if (is_one
&& !is_virtual
) {
2829 isl_pw_aff_free(init_val
);
2830 pa
= extract_comparison(isl_int_is_pos(inc
) ? BO_GE
: BO_LE
,
2832 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2833 valid_init
= set_project_out_by_id(valid_init
, id
);
2834 domain
= isl_pw_aff_non_zero_set(pa
);
2836 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
2837 domain
= strided_domain(isl_id_copy(id
), init_val
, inc
);
2840 domain
= embed(domain
, isl_id_copy(id
));
2843 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
2844 rev_wrap
= isl_map_reverse(isl_map_copy(wrap
));
2845 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
2846 skip
= isl_set_apply(skip
, isl_map_copy(rev_wrap
));
2847 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
2848 valid_inc
= isl_set_apply(valid_inc
, rev_wrap
);
2850 is_simple
= is_simple_bound(cond
, inc
);
2852 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
2853 is_simple
= is_simple_bound(cond
, inc
);
2856 cond
= valid_for_each_iteration(cond
,
2857 isl_set_copy(domain
), inc
);
2858 domain
= isl_set_intersect(domain
, cond
);
2859 if (has_affine_break
) {
2860 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2861 skip
= after(skip
, isl_int_sgn(inc
));
2862 domain
= isl_set_subtract(domain
, skip
);
2864 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
2865 space
= isl_space_from_domain(isl_set_get_space(domain
));
2866 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2867 sched
= isl_map_universe(space
);
2868 if (isl_int_is_pos(inc
))
2869 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
2871 sched
= isl_map_oppose(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
2873 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
), inc
);
2874 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
2876 if (is_virtual
&& !keep_virtual
) {
2877 wrap
= isl_map_set_dim_id(wrap
,
2878 isl_dim_out
, 0, isl_id_copy(id
));
2879 sched
= isl_map_intersect_domain(sched
, isl_set_copy(domain
));
2880 domain
= isl_set_apply(domain
, isl_map_copy(wrap
));
2881 sched
= isl_map_apply_domain(sched
, wrap
);
2883 if (!(is_virtual
&& keep_virtual
)) {
2884 space
= isl_set_get_space(domain
);
2885 wrap
= isl_map_identity(isl_space_map_from_set(space
));
2888 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
2889 isl_map_copy(sched
), isl_map_copy(wrap
), isl_id_copy(id
));
2890 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
2891 scop
= resolve_nested(scop
);
2893 scop
= scop_add_break(scop
, break_access
, isl_set_copy(domain
),
2896 scop
= scop_add_while(scop_cond
, scop
, test_access
, domain
,
2898 isl_set_free(valid_inc
);
2900 scop
= pet_scop_restrict_context(scop
, valid_inc
);
2901 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
2902 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
2903 isl_set_free(domain
);
2905 clear_assignment(assigned_value
, iv
);
2909 scop
= pet_scop_restrict_context(scop
, valid_init
);
2914 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
, bool skip_declarations
)
2916 return extract(stmt
->children(), true, skip_declarations
);
2919 /* Does parameter "pos" of "map" refer to a nested access?
2921 static bool is_nested_parameter(__isl_keep isl_map
*map
, int pos
)
2926 id
= isl_map_get_dim_id(map
, isl_dim_param
, pos
);
2927 nested
= is_nested_parameter(id
);
2933 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
2935 static int n_nested_parameter(__isl_keep isl_space
*space
)
2940 nparam
= isl_space_dim(space
, isl_dim_param
);
2941 for (int i
= 0; i
< nparam
; ++i
)
2942 if (is_nested_parameter(space
, i
))
2948 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
2950 static int n_nested_parameter(__isl_keep isl_map
*map
)
2955 space
= isl_map_get_space(map
);
2956 n
= n_nested_parameter(space
);
2957 isl_space_free(space
);
2962 /* For each nested access parameter in "space",
2963 * construct a corresponding pet_expr, place it in args and
2964 * record its position in "param2pos".
2965 * "n_arg" is the number of elements that are already in args.
2966 * The position recorded in "param2pos" takes this number into account.
2967 * If the pet_expr corresponding to a parameter is identical to
2968 * the pet_expr corresponding to an earlier parameter, then these two
2969 * parameters are made to refer to the same element in args.
2971 * Return the final number of elements in args or -1 if an error has occurred.
2973 int PetScan::extract_nested(__isl_keep isl_space
*space
,
2974 int n_arg
, struct pet_expr
**args
, std::map
<int,int> ¶m2pos
)
2978 nparam
= isl_space_dim(space
, isl_dim_param
);
2979 for (int i
= 0; i
< nparam
; ++i
) {
2981 isl_id
*id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
2984 if (!is_nested_parameter(id
)) {
2989 nested
= (Expr
*) isl_id_get_user(id
);
2990 args
[n_arg
] = extract_expr(nested
);
2994 for (j
= 0; j
< n_arg
; ++j
)
2995 if (pet_expr_is_equal(args
[j
], args
[n_arg
]))
2999 pet_expr_free(args
[n_arg
]);
3003 param2pos
[i
] = n_arg
++;
3011 /* For each nested access parameter in the access relations in "expr",
3012 * construct a corresponding pet_expr, place it in expr->args and
3013 * record its position in "param2pos".
3014 * n is the number of nested access parameters.
3016 struct pet_expr
*PetScan::extract_nested(struct pet_expr
*expr
, int n
,
3017 std::map
<int,int> ¶m2pos
)
3021 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n
);
3026 space
= isl_map_get_space(expr
->acc
.access
);
3027 n
= extract_nested(space
, 0, expr
->args
, param2pos
);
3028 isl_space_free(space
);
3036 pet_expr_free(expr
);
3040 /* Look for parameters in any access relation in "expr" that
3041 * refer to nested accesses. In particular, these are
3042 * parameters with no name.
3044 * If there are any such parameters, then the domain of the access
3045 * relation, which is still [] at this point, is replaced by
3046 * [[] -> [t_1,...,t_n]], with n the number of these parameters
3047 * (after identifying identical nested accesses).
3048 * The parameters are then equated to the corresponding t dimensions
3049 * and subsequently projected out.
3050 * param2pos maps the position of the parameter to the position
3051 * of the corresponding t dimension.
3053 struct pet_expr
*PetScan::resolve_nested(struct pet_expr
*expr
)
3060 std::map
<int,int> param2pos
;
3065 for (int i
= 0; i
< expr
->n_arg
; ++i
) {
3066 expr
->args
[i
] = resolve_nested(expr
->args
[i
]);
3067 if (!expr
->args
[i
]) {
3068 pet_expr_free(expr
);
3073 if (expr
->type
!= pet_expr_access
)
3076 n
= n_nested_parameter(expr
->acc
.access
);
3080 expr
= extract_nested(expr
, n
, param2pos
);
3085 nparam
= isl_map_dim(expr
->acc
.access
, isl_dim_param
);
3086 n_in
= isl_map_dim(expr
->acc
.access
, isl_dim_in
);
3087 dim
= isl_map_get_space(expr
->acc
.access
);
3088 dim
= isl_space_domain(dim
);
3089 dim
= isl_space_from_domain(dim
);
3090 dim
= isl_space_add_dims(dim
, isl_dim_out
, n
);
3091 map
= isl_map_universe(dim
);
3092 map
= isl_map_domain_map(map
);
3093 map
= isl_map_reverse(map
);
3094 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
3096 for (int i
= nparam
- 1; i
>= 0; --i
) {
3097 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
3099 if (!is_nested_parameter(id
)) {
3104 expr
->acc
.access
= isl_map_equate(expr
->acc
.access
,
3105 isl_dim_param
, i
, isl_dim_in
,
3106 n_in
+ param2pos
[i
]);
3107 expr
->acc
.access
= isl_map_project_out(expr
->acc
.access
,
3108 isl_dim_param
, i
, 1);
3115 pet_expr_free(expr
);
3119 /* Convert a top-level pet_expr to a pet_scop with one statement.
3120 * This mainly involves resolving nested expression parameters
3121 * and setting the name of the iteration space.
3122 * The name is given by "label" if it is non-NULL. Otherwise,
3123 * it is of the form S_<n_stmt>.
3125 struct pet_scop
*PetScan::extract(Stmt
*stmt
, struct pet_expr
*expr
,
3126 __isl_take isl_id
*label
)
3128 struct pet_stmt
*ps
;
3129 SourceLocation loc
= stmt
->getLocStart();
3130 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3132 expr
= resolve_nested(expr
);
3133 ps
= pet_stmt_from_pet_expr(ctx
, line
, label
, n_stmt
++, expr
);
3134 return pet_scop_from_pet_stmt(ctx
, ps
);
3137 /* Check if we can extract an affine expression from "expr".
3138 * Return the expressions as an isl_pw_aff if we can and NULL otherwise.
3139 * We turn on autodetection so that we won't generate any warnings
3140 * and turn off nesting, so that we won't accept any non-affine constructs.
3142 __isl_give isl_pw_aff
*PetScan::try_extract_affine(Expr
*expr
)
3145 int save_autodetect
= options
->autodetect
;
3146 bool save_nesting
= nesting_enabled
;
3148 options
->autodetect
= 1;
3149 nesting_enabled
= false;
3151 pwaff
= extract_affine(expr
);
3153 options
->autodetect
= save_autodetect
;
3154 nesting_enabled
= save_nesting
;
3159 /* Check whether "expr" is an affine expression.
3161 bool PetScan::is_affine(Expr
*expr
)
3165 pwaff
= try_extract_affine(expr
);
3166 isl_pw_aff_free(pwaff
);
3168 return pwaff
!= NULL
;
3171 /* Check if we can extract an affine constraint from "expr".
3172 * Return the constraint as an isl_set if we can and NULL otherwise.
3173 * We turn on autodetection so that we won't generate any warnings
3174 * and turn off nesting, so that we won't accept any non-affine constructs.
3176 __isl_give isl_pw_aff
*PetScan::try_extract_affine_condition(Expr
*expr
)
3179 int save_autodetect
= options
->autodetect
;
3180 bool save_nesting
= nesting_enabled
;
3182 options
->autodetect
= 1;
3183 nesting_enabled
= false;
3185 cond
= extract_condition(expr
);
3187 options
->autodetect
= save_autodetect
;
3188 nesting_enabled
= save_nesting
;
3193 /* Check whether "expr" is an affine constraint.
3195 bool PetScan::is_affine_condition(Expr
*expr
)
3199 cond
= try_extract_affine_condition(expr
);
3200 isl_pw_aff_free(cond
);
3202 return cond
!= NULL
;
3205 /* Check if we can extract a condition from "expr".
3206 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
3207 * If allow_nested is set, then the condition may involve parameters
3208 * corresponding to nested accesses.
3209 * We turn on autodetection so that we won't generate any warnings.
3211 __isl_give isl_pw_aff
*PetScan::try_extract_nested_condition(Expr
*expr
)
3214 int save_autodetect
= options
->autodetect
;
3215 bool save_nesting
= nesting_enabled
;
3217 options
->autodetect
= 1;
3218 nesting_enabled
= allow_nested
;
3219 cond
= extract_condition(expr
);
3221 options
->autodetect
= save_autodetect
;
3222 nesting_enabled
= save_nesting
;
3227 /* If the top-level expression of "stmt" is an assignment, then
3228 * return that assignment as a BinaryOperator.
3229 * Otherwise return NULL.
3231 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
3233 BinaryOperator
*ass
;
3237 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
3240 ass
= cast
<BinaryOperator
>(stmt
);
3241 if(ass
->getOpcode() != BO_Assign
)
3247 /* Check if the given if statement is a conditional assignement
3248 * with a non-affine condition. If so, construct a pet_scop
3249 * corresponding to this conditional assignment. Otherwise return NULL.
3251 * In particular we check if "stmt" is of the form
3258 * where a is some array or scalar access.
3259 * The constructed pet_scop then corresponds to the expression
3261 * a = condition ? f(...) : g(...)
3263 * All access relations in f(...) are intersected with condition
3264 * while all access relation in g(...) are intersected with the complement.
3266 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
)
3268 BinaryOperator
*ass_then
, *ass_else
;
3269 isl_map
*write_then
, *write_else
;
3270 isl_set
*cond
, *comp
;
3274 struct pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
3275 bool save_nesting
= nesting_enabled
;
3277 if (!options
->detect_conditional_assignment
)
3280 ass_then
= top_assignment_or_null(stmt
->getThen());
3281 ass_else
= top_assignment_or_null(stmt
->getElse());
3283 if (!ass_then
|| !ass_else
)
3286 if (is_affine_condition(stmt
->getCond()))
3289 write_then
= extract_access(ass_then
->getLHS());
3290 write_else
= extract_access(ass_else
->getLHS());
3292 equal
= isl_map_is_equal(write_then
, write_else
);
3293 isl_map_free(write_else
);
3294 if (equal
< 0 || !equal
) {
3295 isl_map_free(write_then
);
3299 nesting_enabled
= allow_nested
;
3300 pa
= extract_condition(stmt
->getCond());
3301 nesting_enabled
= save_nesting
;
3302 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
3303 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
3304 map
= isl_map_from_range(isl_set_from_pw_aff(pa
));
3306 pe_cond
= pet_expr_from_access(map
);
3308 pe_then
= extract_expr(ass_then
->getRHS());
3309 pe_then
= pet_expr_restrict(pe_then
, cond
);
3310 pe_else
= extract_expr(ass_else
->getRHS());
3311 pe_else
= pet_expr_restrict(pe_else
, comp
);
3313 pe
= pet_expr_new_ternary(ctx
, pe_cond
, pe_then
, pe_else
);
3314 pe_write
= pet_expr_from_access(write_then
);
3316 pe_write
->acc
.write
= 1;
3317 pe_write
->acc
.read
= 0;
3319 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, pe_write
, pe
);
3320 return extract(stmt
, pe
);
3323 /* Create a pet_scop with a single statement evaluating "cond"
3324 * and writing the result to a virtual scalar, as expressed by
3327 struct pet_scop
*PetScan::extract_non_affine_condition(Expr
*cond
,
3328 __isl_take isl_map
*access
)
3330 struct pet_expr
*expr
, *write
;
3331 struct pet_stmt
*ps
;
3332 struct pet_scop
*scop
;
3333 SourceLocation loc
= cond
->getLocStart();
3334 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3336 write
= pet_expr_from_access(access
);
3338 write
->acc
.write
= 1;
3339 write
->acc
.read
= 0;
3341 expr
= extract_expr(cond
);
3342 expr
= resolve_nested(expr
);
3343 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, write
, expr
);
3344 ps
= pet_stmt_from_pet_expr(ctx
, line
, NULL
, n_stmt
++, expr
);
3345 scop
= pet_scop_from_pet_stmt(ctx
, ps
);
3346 scop
= resolve_nested(scop
);
3352 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
3356 /* Apply the map pointed to by "user" to the domain of the access
3357 * relation, thereby embedding it in the range of the map.
3358 * The domain of both relations is the zero-dimensional domain.
3360 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
, void *user
)
3362 isl_map
*map
= (isl_map
*) user
;
3364 return isl_map_apply_domain(access
, isl_map_copy(map
));
3367 /* Apply "map" to all access relations in "expr".
3369 static struct pet_expr
*embed(struct pet_expr
*expr
, __isl_keep isl_map
*map
)
3371 return pet_expr_foreach_access(expr
, &embed_access
, map
);
3374 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
3376 static int n_nested_parameter(__isl_keep isl_set
*set
)
3381 space
= isl_set_get_space(set
);
3382 n
= n_nested_parameter(space
);
3383 isl_space_free(space
);
3388 /* Remove all parameters from "map" that refer to nested accesses.
3390 static __isl_give isl_map
*remove_nested_parameters(__isl_take isl_map
*map
)
3395 space
= isl_map_get_space(map
);
3396 nparam
= isl_space_dim(space
, isl_dim_param
);
3397 for (int i
= nparam
- 1; i
>= 0; --i
)
3398 if (is_nested_parameter(space
, i
))
3399 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3400 isl_space_free(space
);
3406 static __isl_give isl_map
*access_remove_nested_parameters(
3407 __isl_take isl_map
*access
, void *user
);
3410 static __isl_give isl_map
*access_remove_nested_parameters(
3411 __isl_take isl_map
*access
, void *user
)
3413 return remove_nested_parameters(access
);
3416 /* Remove all nested access parameters from the schedule and all
3417 * accesses of "stmt".
3418 * There is no need to remove them from the domain as these parameters
3419 * have already been removed from the domain when this function is called.
3421 static struct pet_stmt
*remove_nested_parameters(struct pet_stmt
*stmt
)
3425 stmt
->schedule
= remove_nested_parameters(stmt
->schedule
);
3426 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
3427 &access_remove_nested_parameters
, NULL
);
3428 if (!stmt
->schedule
|| !stmt
->body
)
3430 for (int i
= 0; i
< stmt
->n_arg
; ++i
) {
3431 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
3432 &access_remove_nested_parameters
, NULL
);
3439 pet_stmt_free(stmt
);
3443 /* For each nested access parameter in the domain of "stmt",
3444 * construct a corresponding pet_expr, place it before the original
3445 * elements in stmt->args and record its position in "param2pos".
3446 * n is the number of nested access parameters.
3448 struct pet_stmt
*PetScan::extract_nested(struct pet_stmt
*stmt
, int n
,
3449 std::map
<int,int> ¶m2pos
)
3454 struct pet_expr
**args
;
3456 n_arg
= stmt
->n_arg
;
3457 args
= isl_calloc_array(ctx
, struct pet_expr
*, n
+ n_arg
);
3461 space
= isl_set_get_space(stmt
->domain
);
3462 n_arg
= extract_nested(space
, 0, args
, param2pos
);
3463 isl_space_free(space
);
3468 for (i
= 0; i
< stmt
->n_arg
; ++i
)
3469 args
[n_arg
+ i
] = stmt
->args
[i
];
3472 stmt
->n_arg
+= n_arg
;
3477 for (i
= 0; i
< n
; ++i
)
3478 pet_expr_free(args
[i
]);
3481 pet_stmt_free(stmt
);
3485 /* Check whether any of the arguments i of "stmt" starting at position "n"
3486 * is equal to one of the first "n" arguments j.
3487 * If so, combine the constraints on arguments i and j and remove
3490 static struct pet_stmt
*remove_duplicate_arguments(struct pet_stmt
*stmt
, int n
)
3499 if (n
== stmt
->n_arg
)
3502 map
= isl_set_unwrap(stmt
->domain
);
3504 for (i
= stmt
->n_arg
- 1; i
>= n
; --i
) {
3505 for (j
= 0; j
< n
; ++j
)
3506 if (pet_expr_is_equal(stmt
->args
[i
], stmt
->args
[j
]))
3511 map
= isl_map_equate(map
, isl_dim_out
, i
, isl_dim_out
, j
);
3512 map
= isl_map_project_out(map
, isl_dim_out
, i
, 1);
3514 pet_expr_free(stmt
->args
[i
]);
3515 for (j
= i
; j
+ 1 < stmt
->n_arg
; ++j
)
3516 stmt
->args
[j
] = stmt
->args
[j
+ 1];
3520 stmt
->domain
= isl_map_wrap(map
);
3525 pet_stmt_free(stmt
);
3529 /* Look for parameters in the iteration domain of "stmt" that
3530 * refer to nested accesses. In particular, these are
3531 * parameters with no name.
3533 * If there are any such parameters, then as many extra variables
3534 * (after identifying identical nested accesses) are inserted in the
3535 * range of the map wrapped inside the domain, before the original variables.
3536 * If the original domain is not a wrapped map, then a new wrapped
3537 * map is created with zero output dimensions.
3538 * The parameters are then equated to the corresponding output dimensions
3539 * and subsequently projected out, from the iteration domain,
3540 * the schedule and the access relations.
3541 * For each of the output dimensions, a corresponding argument
3542 * expression is inserted. Initially they are created with
3543 * a zero-dimensional domain, so they have to be embedded
3544 * in the current iteration domain.
3545 * param2pos maps the position of the parameter to the position
3546 * of the corresponding output dimension in the wrapped map.
3548 struct pet_stmt
*PetScan::resolve_nested(struct pet_stmt
*stmt
)
3554 std::map
<int,int> param2pos
;
3559 n
= n_nested_parameter(stmt
->domain
);
3563 n_arg
= stmt
->n_arg
;
3564 stmt
= extract_nested(stmt
, n
, param2pos
);
3568 n
= stmt
->n_arg
- n_arg
;
3569 nparam
= isl_set_dim(stmt
->domain
, isl_dim_param
);
3570 if (isl_set_is_wrapping(stmt
->domain
))
3571 map
= isl_set_unwrap(stmt
->domain
);
3573 map
= isl_map_from_domain(stmt
->domain
);
3574 map
= isl_map_insert_dims(map
, isl_dim_out
, 0, n
);
3576 for (int i
= nparam
- 1; i
>= 0; --i
) {
3579 if (!is_nested_parameter(map
, i
))
3582 id
= isl_map_get_tuple_id(stmt
->args
[param2pos
[i
]]->acc
.access
,
3584 map
= isl_map_set_dim_id(map
, isl_dim_out
, param2pos
[i
], id
);
3585 map
= isl_map_equate(map
, isl_dim_param
, i
, isl_dim_out
,
3587 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3590 stmt
->domain
= isl_map_wrap(map
);
3592 map
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
3593 map
= isl_map_from_range(isl_map_domain(map
));
3594 for (int pos
= 0; pos
< n
; ++pos
)
3595 stmt
->args
[pos
] = embed(stmt
->args
[pos
], map
);
3598 stmt
= remove_nested_parameters(stmt
);
3599 stmt
= remove_duplicate_arguments(stmt
, n
);
3603 pet_stmt_free(stmt
);
3607 /* For each statement in "scop", move the parameters that correspond
3608 * to nested access into the ranges of the domains and create
3609 * corresponding argument expressions.
3611 struct pet_scop
*PetScan::resolve_nested(struct pet_scop
*scop
)
3616 for (int i
= 0; i
< scop
->n_stmt
; ++i
) {
3617 scop
->stmts
[i
] = resolve_nested(scop
->stmts
[i
]);
3618 if (!scop
->stmts
[i
])
3624 pet_scop_free(scop
);
3628 /* Given an access expression "expr", is the variable accessed by
3629 * "expr" assigned anywhere inside "scop"?
3631 static bool is_assigned(pet_expr
*expr
, pet_scop
*scop
)
3633 bool assigned
= false;
3636 id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
3637 assigned
= pet_scop_writes(scop
, id
);
3643 /* Are all nested access parameters in "pa" allowed given "scop".
3644 * In particular, is none of them written by anywhere inside "scop".
3646 * If "scop" has any skip conditions, then no nested access parameters
3647 * are allowed. In particular, if there is any nested access in a guard
3648 * for a piece of code containing a "continue", then we want to introduce
3649 * a separate statement for evaluating this guard so that we can express
3650 * that the result is false for all previous iterations.
3652 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff
*pa
, pet_scop
*scop
)
3659 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
3660 for (int i
= 0; i
< nparam
; ++i
) {
3662 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
3666 if (!is_nested_parameter(id
)) {
3671 if (pet_scop_has_skip(scop
, pet_skip_now
)) {
3676 nested
= (Expr
*) isl_id_get_user(id
);
3677 expr
= extract_expr(nested
);
3678 allowed
= expr
&& expr
->type
== pet_expr_access
&&
3679 !is_assigned(expr
, scop
);
3681 pet_expr_free(expr
);
3691 /* Do we need to construct a skip condition of the given type
3692 * on an if statement, given that the if condition is non-affine?
3694 * pet_scop_filter_skip can only handle the case where the if condition
3695 * holds (the then branch) and the skip condition is universal.
3696 * In any other case, we need to construct a new skip condition.
3698 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
3699 bool have_else
, enum pet_skip type
)
3701 if (have_else
&& scop_else
&& pet_scop_has_skip(scop_else
, type
))
3703 if (scop_then
&& pet_scop_has_skip(scop_then
, type
) &&
3704 !pet_scop_has_universal_skip(scop_then
, type
))
3709 /* Do we need to construct a skip condition of the given type
3710 * on an if statement, given that the if condition is affine?
3712 * There is no need to construct a new skip condition if all
3713 * the skip conditions are affine.
3715 static bool need_skip_aff(struct pet_scop
*scop_then
,
3716 struct pet_scop
*scop_else
, bool have_else
, enum pet_skip type
)
3718 if (scop_then
&& pet_scop_has_var_skip(scop_then
, type
))
3720 if (have_else
&& scop_else
&& pet_scop_has_var_skip(scop_else
, type
))
3725 /* Do we need to construct a skip condition of the given type
3726 * on an if statement?
3728 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
3729 bool have_else
, enum pet_skip type
, bool affine
)
3732 return need_skip_aff(scop_then
, scop_else
, have_else
, type
);
3734 return need_skip(scop_then
, scop_else
, have_else
, type
);
3737 /* Construct an affine expression pet_expr that is evaluates
3738 * to the constant "val".
3740 static struct pet_expr
*universally(isl_ctx
*ctx
, int val
)
3745 space
= isl_space_alloc(ctx
, 0, 0, 1);
3746 map
= isl_map_universe(space
);
3747 map
= isl_map_fix_si(map
, isl_dim_out
, 0, val
);
3749 return pet_expr_from_access(map
);
3752 /* Construct an affine expression pet_expr that is evaluates
3753 * to the constant 1.
3755 static struct pet_expr
*universally_true(isl_ctx
*ctx
)
3757 return universally(ctx
, 1);
3760 /* Construct an affine expression pet_expr that is evaluates
3761 * to the constant 0.
3763 static struct pet_expr
*universally_false(isl_ctx
*ctx
)
3765 return universally(ctx
, 0);
3768 /* Given an access relation "test_access" for the if condition,
3769 * an access relation "skip_access" for the skip condition and
3770 * scops for the then and else branches, construct a scop for
3771 * computing "skip_access".
3773 * The computed scop contains a single statement that essentially does
3775 * skip_cond = test_cond ? skip_cond_then : skip_cond_else
3777 * If the skip conditions of the then and/or else branch are not affine,
3778 * then they need to be filtered by test_access.
3779 * If they are missing, then this means the skip condition is false.
3781 * Since we are constructing a skip condition for the if statement,
3782 * the skip conditions on the then and else branches are removed.
3784 static struct pet_scop
*extract_skip(PetScan
*scan
,
3785 __isl_take isl_map
*test_access
, __isl_take isl_map
*skip_access
,
3786 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
, bool have_else
,
3789 struct pet_expr
*expr_then
, *expr_else
, *expr
, *expr_skip
;
3790 struct pet_stmt
*stmt
;
3791 struct pet_scop
*scop
;
3792 isl_ctx
*ctx
= scan
->ctx
;
3796 if (have_else
&& !scop_else
)
3799 if (pet_scop_has_skip(scop_then
, type
)) {
3800 expr_then
= pet_scop_get_skip_expr(scop_then
, type
);
3801 pet_scop_reset_skip(scop_then
, type
);
3802 if (!pet_expr_is_affine(expr_then
))
3803 expr_then
= pet_expr_filter(expr_then
,
3804 isl_map_copy(test_access
), 1);
3806 expr_then
= universally_false(ctx
);
3808 if (have_else
&& pet_scop_has_skip(scop_else
, type
)) {
3809 expr_else
= pet_scop_get_skip_expr(scop_else
, type
);
3810 pet_scop_reset_skip(scop_else
, type
);
3811 if (!pet_expr_is_affine(expr_else
))
3812 expr_else
= pet_expr_filter(expr_else
,
3813 isl_map_copy(test_access
), 0);
3815 expr_else
= universally_false(ctx
);
3817 expr
= pet_expr_from_access(test_access
);
3818 expr
= pet_expr_new_ternary(ctx
, expr
, expr_then
, expr_else
);
3819 expr_skip
= pet_expr_from_access(isl_map_copy(skip_access
));
3821 expr_skip
->acc
.write
= 1;
3822 expr_skip
->acc
.read
= 0;
3824 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
3825 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, scan
->n_stmt
++, expr
);
3827 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
3828 scop
= scop_add_array(scop
, skip_access
, scan
->ast_context
);
3829 isl_map_free(skip_access
);
3833 isl_map_free(test_access
);
3834 isl_map_free(skip_access
);
3838 /* Is scop's skip_now condition equal to its skip_later condition?
3839 * In particular, this means that it either has no skip_now condition
3840 * or both a skip_now and a skip_later condition (that are equal to each other).
3842 static bool skip_equals_skip_later(struct pet_scop
*scop
)
3844 int has_skip_now
, has_skip_later
;
3846 isl_set
*skip_now
, *skip_later
;
3850 has_skip_now
= pet_scop_has_skip(scop
, pet_skip_now
);
3851 has_skip_later
= pet_scop_has_skip(scop
, pet_skip_later
);
3852 if (has_skip_now
!= has_skip_later
)
3857 skip_now
= pet_scop_get_skip(scop
, pet_skip_now
);
3858 skip_later
= pet_scop_get_skip(scop
, pet_skip_later
);
3859 equal
= isl_set_is_equal(skip_now
, skip_later
);
3860 isl_set_free(skip_now
);
3861 isl_set_free(skip_later
);
3866 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
3868 static void drop_skip_later(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
3870 pet_scop_reset_skip(scop1
, pet_skip_later
);
3871 pet_scop_reset_skip(scop2
, pet_skip_later
);
3874 /* Structure that handles the construction of skip conditions.
3876 * scop_then and scop_else represent the then and else branches
3877 * of the if statement
3879 * skip[type] is true if we need to construct a skip condition of that type
3880 * equal is set if the skip conditions of types pet_skip_now and pet_skip_later
3881 * are equal to each other
3882 * access[type] is the virtual array representing the skip condition
3883 * scop[type] is a scop for computing the skip condition
3885 struct pet_skip_info
{
3891 struct pet_scop
*scop
[2];
3893 pet_skip_info(isl_ctx
*ctx
) : ctx(ctx
) {}
3895 operator bool() { return skip
[pet_skip_now
] || skip
[pet_skip_later
]; }
3898 /* Structure that handles the construction of skip conditions on if statements.
3900 * scop_then and scop_else represent the then and else branches
3901 * of the if statement
3903 struct pet_skip_info_if
: public pet_skip_info
{
3904 struct pet_scop
*scop_then
, *scop_else
;
3907 pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
3908 struct pet_scop
*scop_else
, bool have_else
, bool affine
);
3909 void extract(PetScan
*scan
, __isl_keep isl_map
*access
,
3910 enum pet_skip type
);
3911 void extract(PetScan
*scan
, __isl_keep isl_map
*access
);
3912 void extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
);
3913 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
3915 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
3918 /* Initialize a pet_skip_info_if structure based on the then and else branches
3919 * and based on whether the if condition is affine or not.
3921 pet_skip_info_if::pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
3922 struct pet_scop
*scop_else
, bool have_else
, bool affine
) :
3923 pet_skip_info(ctx
), scop_then(scop_then
), scop_else(scop_else
),
3924 have_else(have_else
)
3926 skip
[pet_skip_now
] =
3927 need_skip(scop_then
, scop_else
, have_else
, pet_skip_now
, affine
);
3928 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop_then
) &&
3929 (!have_else
|| skip_equals_skip_later(scop_else
));
3930 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
3931 need_skip(scop_then
, scop_else
, have_else
, pet_skip_later
, affine
);
3934 /* If we need to construct a skip condition of the given type,
3937 * "map" represents the if condition.
3939 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_map
*map
,
3945 access
[type
] = create_test_access(isl_map_get_ctx(map
), scan
->n_test
++);
3946 scop
[type
] = extract_skip(scan
, isl_map_copy(map
),
3947 isl_map_copy(access
[type
]),
3948 scop_then
, scop_else
, have_else
, type
);
3951 /* Construct the required skip conditions, given the if condition "map".
3953 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_map
*map
)
3955 extract(scan
, map
, pet_skip_now
);
3956 extract(scan
, map
, pet_skip_later
);
3958 drop_skip_later(scop_then
, scop_else
);
3961 /* Construct the required skip conditions, given the if condition "cond".
3963 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
)
3968 if (!skip
[pet_skip_now
] && !skip
[pet_skip_later
])
3971 test_set
= isl_set_from_pw_aff(isl_pw_aff_copy(cond
));
3972 test
= isl_map_from_range(test_set
);
3973 extract(scan
, test
);
3977 /* Add the computed skip condition of the give type to "main" and
3978 * add the scop for computing the condition at the given offset.
3980 * If equal is set, then we only computed a skip condition for pet_skip_now,
3981 * but we also need to set it as main's pet_skip_later.
3983 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*main
,
3984 enum pet_skip type
, int offset
)
3991 skip_set
= isl_map_range(access
[type
]);
3992 access
[type
] = NULL
;
3993 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
3994 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
3998 main
= pet_scop_set_skip(main
, pet_skip_later
,
3999 isl_set_copy(skip_set
));
4001 main
= pet_scop_set_skip(main
, type
, skip_set
);
4006 /* Add the computed skip conditions to "main" and
4007 * add the scops for computing the conditions at the given offset.
4009 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*scop
, int offset
)
4011 scop
= add(scop
, pet_skip_now
, offset
);
4012 scop
= add(scop
, pet_skip_later
, offset
);
4017 /* Construct a pet_scop for a non-affine if statement.
4019 * We create a separate statement that writes the result
4020 * of the non-affine condition to a virtual scalar.
4021 * A constraint requiring the value of this virtual scalar to be one
4022 * is added to the iteration domains of the then branch.
4023 * Similarly, a constraint requiring the value of this virtual scalar
4024 * to be zero is added to the iteration domains of the else branch, if any.
4025 * We adjust the schedules to ensure that the virtual scalar is written
4026 * before it is read.
4028 * If there are any breaks or continues in the then and/or else
4029 * branches, then we may have to compute a new skip condition.
4030 * This is handled using a pet_skip_info_if object.
4031 * On initialization, the object checks if skip conditions need
4032 * to be computed. If so, it does so in "extract" and adds them in "add".
4034 struct pet_scop
*PetScan::extract_non_affine_if(Expr
*cond
,
4035 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
4036 bool have_else
, int stmt_id
)
4038 struct pet_scop
*scop
;
4039 isl_map
*test_access
;
4040 int save_n_stmt
= n_stmt
;
4042 test_access
= create_test_access(ctx
, n_test
++);
4044 scop
= extract_non_affine_condition(cond
, isl_map_copy(test_access
));
4045 n_stmt
= save_n_stmt
;
4046 scop
= scop_add_array(scop
, test_access
, ast_context
);
4048 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, have_else
, false);
4049 skip
.extract(this, test_access
);
4051 scop
= pet_scop_prefix(scop
, 0);
4052 scop_then
= pet_scop_prefix(scop_then
, 1);
4053 scop_then
= pet_scop_filter(scop_then
, isl_map_copy(test_access
), 1);
4055 scop_else
= pet_scop_prefix(scop_else
, 1);
4056 scop_else
= pet_scop_filter(scop_else
, test_access
, 0);
4057 scop_then
= pet_scop_add_par(ctx
, scop_then
, scop_else
);
4059 isl_map_free(test_access
);
4061 scop
= pet_scop_add_seq(ctx
, scop
, scop_then
);
4063 scop
= skip
.add(scop
, 2);
4068 /* Construct a pet_scop for an if statement.
4070 * If the condition fits the pattern of a conditional assignment,
4071 * then it is handled by extract_conditional_assignment.
4072 * Otherwise, we do the following.
4074 * If the condition is affine, then the condition is added
4075 * to the iteration domains of the then branch, while the
4076 * opposite of the condition in added to the iteration domains
4077 * of the else branch, if any.
4078 * We allow the condition to be dynamic, i.e., to refer to
4079 * scalars or array elements that may be written to outside
4080 * of the given if statement. These nested accesses are then represented
4081 * as output dimensions in the wrapping iteration domain.
4082 * If it also written _inside_ the then or else branch, then
4083 * we treat the condition as non-affine.
4084 * As explained in extract_non_affine_if, this will introduce
4085 * an extra statement.
4086 * For aesthetic reasons, we want this statement to have a statement
4087 * number that is lower than those of the then and else branches.
4088 * In order to evaluate if will need such a statement, however, we
4089 * first construct scops for the then and else branches.
4090 * We therefore reserve a statement number if we might have to
4091 * introduce such an extra statement.
4093 * If the condition is not affine, then the scop is created in
4094 * extract_non_affine_if.
4096 * If there are any breaks or continues in the then and/or else
4097 * branches, then we may have to compute a new skip condition.
4098 * This is handled using a pet_skip_info_if object.
4099 * On initialization, the object checks if skip conditions need
4100 * to be computed. If so, it does so in "extract" and adds them in "add".
4102 struct pet_scop
*PetScan::extract(IfStmt
*stmt
)
4104 struct pet_scop
*scop_then
, *scop_else
= NULL
, *scop
;
4110 scop
= extract_conditional_assignment(stmt
);
4114 cond
= try_extract_nested_condition(stmt
->getCond());
4115 if (allow_nested
&& (!cond
|| has_nested(cond
)))
4119 assigned_value_cache
cache(assigned_value
);
4120 scop_then
= extract(stmt
->getThen());
4123 if (stmt
->getElse()) {
4124 assigned_value_cache
cache(assigned_value
);
4125 scop_else
= extract(stmt
->getElse());
4126 if (options
->autodetect
) {
4127 if (scop_then
&& !scop_else
) {
4129 isl_pw_aff_free(cond
);
4132 if (!scop_then
&& scop_else
) {
4134 isl_pw_aff_free(cond
);
4141 (!is_nested_allowed(cond
, scop_then
) ||
4142 (stmt
->getElse() && !is_nested_allowed(cond
, scop_else
)))) {
4143 isl_pw_aff_free(cond
);
4146 if (allow_nested
&& !cond
)
4147 return extract_non_affine_if(stmt
->getCond(), scop_then
,
4148 scop_else
, stmt
->getElse(), stmt_id
);
4151 cond
= extract_condition(stmt
->getCond());
4153 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, stmt
->getElse(), true);
4154 skip
.extract(this, cond
);
4156 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
4157 set
= isl_pw_aff_non_zero_set(cond
);
4158 scop
= pet_scop_restrict(scop_then
, isl_set_copy(set
));
4160 if (stmt
->getElse()) {
4161 set
= isl_set_subtract(isl_set_copy(valid
), set
);
4162 scop_else
= pet_scop_restrict(scop_else
, set
);
4163 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
4166 scop
= resolve_nested(scop
);
4167 scop
= pet_scop_restrict_context(scop
, valid
);
4170 scop
= pet_scop_prefix(scop
, 0);
4171 scop
= skip
.add(scop
, 1);
4176 /* Try and construct a pet_scop for a label statement.
4177 * We currently only allow labels on expression statements.
4179 struct pet_scop
*PetScan::extract(LabelStmt
*stmt
)
4184 sub
= stmt
->getSubStmt();
4185 if (!isa
<Expr
>(sub
)) {
4190 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
4192 return extract(sub
, extract_expr(cast
<Expr
>(sub
)), label
);
4195 /* Construct a pet_scop for a continue statement.
4197 * We simply create an empty scop with a universal pet_skip_now
4198 * skip condition. This skip condition will then be taken into
4199 * account by the enclosing loop construct, possibly after
4200 * being incorporated into outer skip conditions.
4202 struct pet_scop
*PetScan::extract(ContinueStmt
*stmt
)
4208 scop
= pet_scop_empty(ctx
);
4212 space
= isl_space_set_alloc(ctx
, 0, 1);
4213 set
= isl_set_universe(space
);
4214 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
4215 scop
= pet_scop_set_skip(scop
, pet_skip_now
, set
);
4220 /* Construct a pet_scop for a break statement.
4222 * We simply create an empty scop with both a universal pet_skip_now
4223 * skip condition and a universal pet_skip_later skip condition.
4224 * These skip conditions will then be taken into
4225 * account by the enclosing loop construct, possibly after
4226 * being incorporated into outer skip conditions.
4228 struct pet_scop
*PetScan::extract(BreakStmt
*stmt
)
4234 scop
= pet_scop_empty(ctx
);
4238 space
= isl_space_set_alloc(ctx
, 0, 1);
4239 set
= isl_set_universe(space
);
4240 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
4241 scop
= pet_scop_set_skip(scop
, pet_skip_now
, isl_set_copy(set
));
4242 scop
= pet_scop_set_skip(scop
, pet_skip_later
, set
);
4247 /* Try and construct a pet_scop corresponding to "stmt".
4249 * If "stmt" is a compound statement, then "skip_declarations"
4250 * indicates whether we should skip initial declarations in the
4251 * compound statement.
4253 struct pet_scop
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
4255 if (isa
<Expr
>(stmt
))
4256 return extract(stmt
, extract_expr(cast
<Expr
>(stmt
)));
4258 switch (stmt
->getStmtClass()) {
4259 case Stmt::WhileStmtClass
:
4260 return extract(cast
<WhileStmt
>(stmt
));
4261 case Stmt::ForStmtClass
:
4262 return extract_for(cast
<ForStmt
>(stmt
));
4263 case Stmt::IfStmtClass
:
4264 return extract(cast
<IfStmt
>(stmt
));
4265 case Stmt::CompoundStmtClass
:
4266 return extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
4267 case Stmt::LabelStmtClass
:
4268 return extract(cast
<LabelStmt
>(stmt
));
4269 case Stmt::ContinueStmtClass
:
4270 return extract(cast
<ContinueStmt
>(stmt
));
4271 case Stmt::BreakStmtClass
:
4272 return extract(cast
<BreakStmt
>(stmt
));
4273 case Stmt::DeclStmtClass
:
4274 return extract(cast
<DeclStmt
>(stmt
));
4282 /* Do we need to construct a skip condition of the given type
4283 * on a sequence of statements?
4285 * There is no need to construct a new skip condition if only
4286 * only of the two statements has a skip condition or if both
4287 * of their skip conditions are affine.
4289 * In principle we also don't need a new continuation variable if
4290 * the continuation of scop2 is affine, but then we would need
4291 * to allow more complicated forms of continuations.
4293 static bool need_skip_seq(struct pet_scop
*scop1
, struct pet_scop
*scop2
,
4296 if (!scop1
|| !pet_scop_has_skip(scop1
, type
))
4298 if (!scop2
|| !pet_scop_has_skip(scop2
, type
))
4300 if (pet_scop_has_affine_skip(scop1
, type
) &&
4301 pet_scop_has_affine_skip(scop2
, type
))
4306 /* Construct a scop for computing the skip condition of the given type and
4307 * with access relation "skip_access" for a sequence of two scops "scop1"
4310 * The computed scop contains a single statement that essentially does
4312 * skip_cond = skip_cond_1 ? 1 : skip_cond_2
4314 * or, in other words, skip_cond1 || skip_cond2.
4315 * In this expression, skip_cond_2 is filtered to reflect that it is
4316 * only evaluated when skip_cond_1 is false.
4318 * The skip condition on scop1 is not removed because it still needs
4319 * to be applied to scop2 when these two scops are combined.
4321 static struct pet_scop
*extract_skip_seq(PetScan
*ps
,
4322 __isl_take isl_map
*skip_access
,
4323 struct pet_scop
*scop1
, struct pet_scop
*scop2
, enum pet_skip type
)
4326 struct pet_expr
*expr1
, *expr2
, *expr
, *expr_skip
;
4327 struct pet_stmt
*stmt
;
4328 struct pet_scop
*scop
;
4329 isl_ctx
*ctx
= ps
->ctx
;
4331 if (!scop1
|| !scop2
)
4334 expr1
= pet_scop_get_skip_expr(scop1
, type
);
4335 expr2
= pet_scop_get_skip_expr(scop2
, type
);
4336 pet_scop_reset_skip(scop2
, type
);
4338 expr2
= pet_expr_filter(expr2
, isl_map_copy(expr1
->acc
.access
), 0);
4340 expr
= universally_true(ctx
);
4341 expr
= pet_expr_new_ternary(ctx
, expr1
, expr
, expr2
);
4342 expr_skip
= pet_expr_from_access(isl_map_copy(skip_access
));
4344 expr_skip
->acc
.write
= 1;
4345 expr_skip
->acc
.read
= 0;
4347 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
4348 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, ps
->n_stmt
++, expr
);
4350 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
4351 scop
= scop_add_array(scop
, skip_access
, ps
->ast_context
);
4352 isl_map_free(skip_access
);
4356 isl_map_free(skip_access
);
4360 /* Structure that handles the construction of skip conditions
4361 * on sequences of statements.
4363 * scop1 and scop2 represent the two statements that are combined
4365 struct pet_skip_info_seq
: public pet_skip_info
{
4366 struct pet_scop
*scop1
, *scop2
;
4368 pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4369 struct pet_scop
*scop2
);
4370 void extract(PetScan
*scan
, enum pet_skip type
);
4371 void extract(PetScan
*scan
);
4372 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
4374 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
4377 /* Initialize a pet_skip_info_seq structure based on
4378 * on the two statements that are going to be combined.
4380 pet_skip_info_seq::pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4381 struct pet_scop
*scop2
) : pet_skip_info(ctx
), scop1(scop1
), scop2(scop2
)
4383 skip
[pet_skip_now
] = need_skip_seq(scop1
, scop2
, pet_skip_now
);
4384 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop1
) &&
4385 skip_equals_skip_later(scop2
);
4386 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
4387 need_skip_seq(scop1
, scop2
, pet_skip_later
);
4390 /* If we need to construct a skip condition of the given type,
4393 void pet_skip_info_seq::extract(PetScan
*scan
, enum pet_skip type
)
4398 access
[type
] = create_test_access(ctx
, scan
->n_test
++);
4399 scop
[type
] = extract_skip_seq(scan
, isl_map_copy(access
[type
]),
4400 scop1
, scop2
, type
);
4403 /* Construct the required skip conditions.
4405 void pet_skip_info_seq::extract(PetScan
*scan
)
4407 extract(scan
, pet_skip_now
);
4408 extract(scan
, pet_skip_later
);
4410 drop_skip_later(scop1
, scop2
);
4413 /* Add the computed skip condition of the give type to "main" and
4414 * add the scop for computing the condition at the given offset (the statement
4415 * number). Within this offset, the condition is computed at position 1
4416 * to ensure that it is computed after the corresponding statement.
4418 * If equal is set, then we only computed a skip condition for pet_skip_now,
4419 * but we also need to set it as main's pet_skip_later.
4421 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*main
,
4422 enum pet_skip type
, int offset
)
4429 skip_set
= isl_map_range(access
[type
]);
4430 access
[type
] = NULL
;
4431 scop
[type
] = pet_scop_prefix(scop
[type
], 1);
4432 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4433 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4437 main
= pet_scop_set_skip(main
, pet_skip_later
,
4438 isl_set_copy(skip_set
));
4440 main
= pet_scop_set_skip(main
, type
, skip_set
);
4445 /* Add the computed skip conditions to "main" and
4446 * add the scops for computing the conditions at the given offset.
4448 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*scop
, int offset
)
4450 scop
= add(scop
, pet_skip_now
, offset
);
4451 scop
= add(scop
, pet_skip_later
, offset
);
4456 /* Extract a clone of the kill statement in "scop".
4457 * "scop" is expected to have been created from a DeclStmt
4458 * and should have the kill as its first statement.
4460 struct pet_stmt
*PetScan::extract_kill(struct pet_scop
*scop
)
4462 struct pet_expr
*kill
;
4463 struct pet_stmt
*stmt
;
4468 if (scop
->n_stmt
< 1)
4469 isl_die(ctx
, isl_error_internal
,
4470 "expecting at least one statement", return NULL
);
4471 stmt
= scop
->stmts
[0];
4472 if (stmt
->body
->type
!= pet_expr_unary
||
4473 stmt
->body
->op
!= pet_op_kill
)
4474 isl_die(ctx
, isl_error_internal
,
4475 "expecting kill statement", return NULL
);
4477 access
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
4478 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
4479 kill
= pet_expr_kill_from_access(access
);
4480 return pet_stmt_from_pet_expr(ctx
, stmt
->line
, NULL
, n_stmt
++, kill
);
4483 /* Mark all arrays in "scop" as being exposed.
4485 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
4489 for (int i
= 0; i
< scop
->n_array
; ++i
)
4490 scop
->arrays
[i
]->exposed
= 1;
4494 /* Try and construct a pet_scop corresponding to (part of)
4495 * a sequence of statements.
4497 * "block" is set if the sequence respresents the children of
4498 * a compound statement.
4499 * "skip_declarations" is set if we should skip initial declarations
4500 * in the sequence of statements.
4502 * If there are any breaks or continues in the individual statements,
4503 * then we may have to compute a new skip condition.
4504 * This is handled using a pet_skip_info_seq object.
4505 * On initialization, the object checks if skip conditions need
4506 * to be computed. If so, it does so in "extract" and adds them in "add".
4508 * If "block" is set, then we need to insert kill statements at
4509 * the end of the block for any array that has been declared by
4510 * one of the statements in the sequence. Each of these declarations
4511 * results in the construction of a kill statement at the place
4512 * of the declaration, so we simply collect duplicates of
4513 * those kill statements and append these duplicates to the constructed scop.
4515 * If "block" is not set, then any array declared by one of the statements
4516 * in the sequence is marked as being exposed.
4518 struct pet_scop
*PetScan::extract(StmtRange stmt_range
, bool block
,
4519 bool skip_declarations
)
4524 bool partial_range
= false;
4525 set
<struct pet_stmt
*> kills
;
4526 set
<struct pet_stmt
*>::iterator it
;
4528 scop
= pet_scop_empty(ctx
);
4529 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
4531 struct pet_scop
*scop_i
;
4533 if (skip_declarations
&&
4534 child
->getStmtClass() == Stmt::DeclStmtClass
)
4537 scop_i
= extract(child
);
4538 if (scop
&& partial
) {
4539 pet_scop_free(scop_i
);
4542 pet_skip_info_seq
skip(ctx
, scop
, scop_i
);
4545 scop_i
= pet_scop_prefix(scop_i
, 0);
4546 if (scop_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
) {
4548 kills
.insert(extract_kill(scop_i
));
4550 scop_i
= mark_exposed(scop_i
);
4552 scop_i
= pet_scop_prefix(scop_i
, j
);
4553 if (options
->autodetect
) {
4555 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
4557 partial_range
= true;
4558 if (scop
->n_stmt
!= 0 && !scop_i
)
4561 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
4564 scop
= skip
.add(scop
, j
);
4570 for (it
= kills
.begin(); it
!= kills
.end(); ++it
) {
4572 scop_j
= pet_scop_from_pet_stmt(ctx
, *it
);
4573 scop_j
= pet_scop_prefix(scop_j
, j
);
4574 scop
= pet_scop_add_seq(ctx
, scop
, scop_j
);
4577 if (scop
&& partial_range
)
4583 /* Return the file offset of the expansion location of "Loc".
4585 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
4587 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
4590 /* Check if the scop marked by the user is exactly this Stmt
4591 * or part of this Stmt.
4592 * If so, return a pet_scop corresponding to the marked region.
4593 * Otherwise, return NULL.
4595 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
4597 SourceManager
&SM
= PP
.getSourceManager();
4598 unsigned start_off
, end_off
;
4600 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
4601 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
4603 if (start_off
> loc
.end
)
4605 if (end_off
< loc
.start
)
4607 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
4608 return extract(stmt
);
4612 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
4613 Stmt
*child
= *start
;
4616 start_off
= getExpansionOffset(SM
, child
->getLocStart());
4617 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
4618 if (start_off
< loc
.start
&& end_off
> loc
.end
)
4620 if (start_off
>= loc
.start
)
4625 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
4627 start_off
= SM
.getFileOffset(child
->getLocStart());
4628 if (start_off
>= loc
.end
)
4632 return extract(StmtRange(start
, end
), false, false);
4635 /* Set the size of index "pos" of "array" to "size".
4636 * In particular, add a constraint of the form
4640 * to array->extent and a constraint of the form
4644 * to array->context.
4646 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
4647 __isl_take isl_pw_aff
*size
)
4657 valid
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
));
4658 array
->context
= isl_set_intersect(array
->context
, valid
);
4660 dim
= isl_set_get_space(array
->extent
);
4661 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
4662 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
4663 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
4664 index
= isl_pw_aff_alloc(univ
, aff
);
4666 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
4667 isl_set_dim(array
->extent
, isl_dim_set
));
4668 id
= isl_set_get_tuple_id(array
->extent
);
4669 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
4670 bound
= isl_pw_aff_lt_set(index
, size
);
4672 array
->extent
= isl_set_intersect(array
->extent
, bound
);
4674 if (!array
->context
|| !array
->extent
)
4679 pet_array_free(array
);
4683 /* Figure out the size of the array at position "pos" and all
4684 * subsequent positions from "type" and update "array" accordingly.
4686 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
4687 const Type
*type
, int pos
)
4689 const ArrayType
*atype
;
4695 if (type
->isPointerType()) {
4696 type
= type
->getPointeeType().getTypePtr();
4697 return set_upper_bounds(array
, type
, pos
+ 1);
4699 if (!type
->isArrayType())
4702 type
= type
->getCanonicalTypeInternal().getTypePtr();
4703 atype
= cast
<ArrayType
>(type
);
4705 if (type
->isConstantArrayType()) {
4706 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
4707 size
= extract_affine(ca
->getSize());
4708 array
= update_size(array
, pos
, size
);
4709 } else if (type
->isVariableArrayType()) {
4710 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
4711 size
= extract_affine(vla
->getSizeExpr());
4712 array
= update_size(array
, pos
, size
);
4715 type
= atype
->getElementType().getTypePtr();
4717 return set_upper_bounds(array
, type
, pos
+ 1);
4720 /* Is "T" the type of a variable length array with static size?
4722 static bool is_vla_with_static_size(QualType T
)
4724 const VariableArrayType
*vlatype
;
4726 if (!T
->isVariableArrayType())
4728 vlatype
= cast
<VariableArrayType
>(T
);
4729 return vlatype
->getSizeModifier() == VariableArrayType::Static
;
4732 /* Return the type of "decl" as an array.
4734 * In particular, if "decl" is a parameter declaration that
4735 * is a variable length array with a static size, then
4736 * return the original type (i.e., the variable length array).
4737 * Otherwise, return the type of decl.
4739 static QualType
get_array_type(ValueDecl
*decl
)
4744 parm
= dyn_cast
<ParmVarDecl
>(decl
);
4746 return decl
->getType();
4748 T
= parm
->getOriginalType();
4749 if (!is_vla_with_static_size(T
))
4750 return decl
->getType();
4754 /* Construct and return a pet_array corresponding to the variable "decl".
4755 * In particular, initialize array->extent to
4757 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
4759 * and then call set_upper_bounds to set the upper bounds on the indices
4760 * based on the type of the variable.
4762 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
)
4764 struct pet_array
*array
;
4765 QualType qt
= get_array_type(decl
);
4766 const Type
*type
= qt
.getTypePtr();
4767 int depth
= array_depth(type
);
4768 QualType base
= base_type(qt
);
4773 array
= isl_calloc_type(ctx
, struct pet_array
);
4777 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
4778 dim
= isl_space_set_alloc(ctx
, 0, depth
);
4779 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
4781 array
->extent
= isl_set_nat_universe(dim
);
4783 dim
= isl_space_params_alloc(ctx
, 0);
4784 array
->context
= isl_set_universe(dim
);
4786 array
= set_upper_bounds(array
, type
, 0);
4790 name
= base
.getAsString();
4791 array
->element_type
= strdup(name
.c_str());
4792 array
->element_size
= decl
->getASTContext().getTypeInfo(base
).first
/ 8;
4797 /* Construct a list of pet_arrays, one for each array (or scalar)
4798 * accessed inside "scop", add this list to "scop" and return the result.
4800 * The context of "scop" is updated with the intersection of
4801 * the contexts of all arrays, i.e., constraints on the parameters
4802 * that ensure that the arrays have a valid (non-negative) size.
4804 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
)
4807 set
<ValueDecl
*> arrays
;
4808 set
<ValueDecl
*>::iterator it
;
4810 struct pet_array
**scop_arrays
;
4815 pet_scop_collect_arrays(scop
, arrays
);
4816 if (arrays
.size() == 0)
4819 n_array
= scop
->n_array
;
4821 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
4822 n_array
+ arrays
.size());
4825 scop
->arrays
= scop_arrays
;
4827 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
4828 struct pet_array
*array
;
4829 scop
->arrays
[n_array
+ i
] = array
= extract_array(ctx
, *it
);
4830 if (!scop
->arrays
[n_array
+ i
])
4833 scop
->context
= isl_set_intersect(scop
->context
,
4834 isl_set_copy(array
->context
));
4841 pet_scop_free(scop
);
4845 /* Bound all parameters in scop->context to the possible values
4846 * of the corresponding C variable.
4848 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
4855 n
= isl_set_dim(scop
->context
, isl_dim_param
);
4856 for (int i
= 0; i
< n
; ++i
) {
4860 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
4861 if (is_nested_parameter(id
)) {
4863 isl_die(isl_set_get_ctx(scop
->context
),
4865 "unresolved nested parameter", goto error
);
4867 decl
= (ValueDecl
*) isl_id_get_user(id
);
4870 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
4878 pet_scop_free(scop
);
4882 /* Construct a pet_scop from the given function.
4884 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
4889 stmt
= fd
->getBody();
4891 if (options
->autodetect
)
4892 scop
= extract(stmt
, true);
4895 scop
= pet_scop_detect_parameter_accesses(scop
);
4896 scop
= scan_arrays(scop
);
4897 scop
= add_parameter_bounds(scop
);
4898 scop
= pet_scop_gist(scop
, value_bounds
);