2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2013 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
39 #include <llvm/Support/raw_ostream.h>
40 #include <clang/AST/ASTContext.h>
41 #include <clang/AST/ASTDiagnostic.h>
42 #include <clang/AST/Expr.h>
43 #include <clang/AST/RecursiveASTVisitor.h>
46 #include <isl/space.h>
54 #include "scop_plus.h"
59 using namespace clang
;
61 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
62 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
64 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
65 SourceLocation(), var
, false, var
->getInnerLocStart(),
66 var
->getType(), VK_LValue
);
68 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
69 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
71 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
72 SourceLocation(), var
, var
->getInnerLocStart(), var
->getType(),
76 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
78 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
79 var
, var
->getInnerLocStart(), var
->getType(), VK_LValue
);
83 /* Check if the element type corresponding to the given array type
84 * has a const qualifier.
86 static bool const_base(QualType qt
)
88 const Type
*type
= qt
.getTypePtr();
90 if (type
->isPointerType())
91 return const_base(type
->getPointeeType());
92 if (type
->isArrayType()) {
93 const ArrayType
*atype
;
94 type
= type
->getCanonicalTypeInternal().getTypePtr();
95 atype
= cast
<ArrayType
>(type
);
96 return const_base(atype
->getElementType());
99 return qt
.isConstQualified();
102 /* Mark "decl" as having an unknown value in "assigned_value".
104 * If no (known or unknown) value was assigned to "decl" before,
105 * then it may have been treated as a parameter before and may
106 * therefore appear in a value assigned to another variable.
107 * If so, this assignment needs to be turned into an unknown value too.
109 static void clear_assignment(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
,
112 map
<ValueDecl
*, isl_pw_aff
*>::iterator it
;
114 it
= assigned_value
.find(decl
);
116 assigned_value
[decl
] = NULL
;
118 if (it
== assigned_value
.end())
121 for (it
= assigned_value
.begin(); it
!= assigned_value
.end(); ++it
) {
122 isl_pw_aff
*pa
= it
->second
;
123 int nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
125 for (int i
= 0; i
< nparam
; ++i
) {
128 if (!isl_pw_aff_has_dim_id(pa
, isl_dim_param
, i
))
130 id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
131 if (isl_id_get_user(id
) == decl
)
138 /* Look for any assignments to scalar variables in part of the parse
139 * tree and set assigned_value to NULL for each of them.
140 * Also reset assigned_value if the address of a scalar variable
141 * is being taken. As an exception, if the address is passed to a function
142 * that is declared to receive a const pointer, then assigned_value is
145 * This ensures that we won't use any previously stored value
146 * in the current subtree and its parents.
148 struct clear_assignments
: RecursiveASTVisitor
<clear_assignments
> {
149 map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
;
150 set
<UnaryOperator
*> skip
;
152 clear_assignments(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
) :
153 assigned_value(assigned_value
) {}
155 /* Check for "address of" operators whose value is passed
156 * to a const pointer argument and add them to "skip", so that
157 * we can skip them in VisitUnaryOperator.
159 bool VisitCallExpr(CallExpr
*expr
) {
161 fd
= expr
->getDirectCallee();
164 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
165 Expr
*arg
= expr
->getArg(i
);
167 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
168 ImplicitCastExpr
*ice
;
169 ice
= cast
<ImplicitCastExpr
>(arg
);
170 arg
= ice
->getSubExpr();
172 if (arg
->getStmtClass() != Stmt::UnaryOperatorClass
)
174 op
= cast
<UnaryOperator
>(arg
);
175 if (op
->getOpcode() != UO_AddrOf
)
177 if (const_base(fd
->getParamDecl(i
)->getType()))
183 bool VisitUnaryOperator(UnaryOperator
*expr
) {
188 switch (expr
->getOpcode()) {
198 if (skip
.find(expr
) != skip
.end())
201 arg
= expr
->getSubExpr();
202 if (arg
->getStmtClass() != Stmt::DeclRefExprClass
)
204 ref
= cast
<DeclRefExpr
>(arg
);
205 decl
= ref
->getDecl();
206 clear_assignment(assigned_value
, decl
);
210 bool VisitBinaryOperator(BinaryOperator
*expr
) {
215 if (!expr
->isAssignmentOp())
217 lhs
= expr
->getLHS();
218 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
)
220 ref
= cast
<DeclRefExpr
>(lhs
);
221 decl
= ref
->getDecl();
222 clear_assignment(assigned_value
, decl
);
227 /* Keep a copy of the currently assigned values.
229 * Any variable that is assigned a value inside the current scope
230 * is removed again when we leave the scope (either because it wasn't
231 * stored in the cache or because it has a different value in the cache).
233 struct assigned_value_cache
{
234 map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
;
235 map
<ValueDecl
*, isl_pw_aff
*> cache
;
237 assigned_value_cache(map
<ValueDecl
*, isl_pw_aff
*> &assigned_value
) :
238 assigned_value(assigned_value
), cache(assigned_value
) {}
239 ~assigned_value_cache() {
240 map
<ValueDecl
*, isl_pw_aff
*>::iterator it
= cache
.begin();
241 for (it
= assigned_value
.begin(); it
!= assigned_value
.end();
244 (cache
.find(it
->first
) != cache
.end() &&
245 cache
[it
->first
] != it
->second
))
246 cache
[it
->first
] = NULL
;
248 assigned_value
= cache
;
252 /* Insert an expression into the collection of expressions,
253 * provided it is not already in there.
254 * The isl_pw_affs are freed in the destructor.
256 void PetScan::insert_expression(__isl_take isl_pw_aff
*expr
)
258 std::set
<isl_pw_aff
*>::iterator it
;
260 if (expressions
.find(expr
) == expressions
.end())
261 expressions
.insert(expr
);
263 isl_pw_aff_free(expr
);
268 std::set
<isl_pw_aff
*>::iterator it
;
270 for (it
= expressions
.begin(); it
!= expressions
.end(); ++it
)
271 isl_pw_aff_free(*it
);
273 isl_union_map_free(value_bounds
);
276 /* Called if we found something we (currently) cannot handle.
277 * We'll provide more informative warnings later.
279 * We only actually complain if autodetect is false.
281 void PetScan::unsupported(Stmt
*stmt
, const char *msg
)
283 if (options
->autodetect
)
286 SourceLocation loc
= stmt
->getLocStart();
287 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
288 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
289 msg
? msg
: "unsupported");
290 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
293 /* Extract an integer from "expr".
295 __isl_give isl_val
*PetScan::extract_int(isl_ctx
*ctx
, IntegerLiteral
*expr
)
297 const Type
*type
= expr
->getType().getTypePtr();
298 int is_signed
= type
->hasSignedIntegerRepresentation();
299 llvm::APInt val
= expr
->getValue();
300 int is_negative
= is_signed
&& val
.isNegative();
306 v
= extract_unsigned(ctx
, val
);
313 /* Extract an integer from "val", which assumed to be non-negative.
315 __isl_give isl_val
*PetScan::extract_unsigned(isl_ctx
*ctx
,
316 const llvm::APInt
&val
)
319 const uint64_t *data
;
321 data
= val
.getRawData();
322 n
= val
.getNumWords();
323 return isl_val_int_from_chunks(ctx
, n
, sizeof(uint64_t), data
);
326 /* Extract an integer from "expr".
327 * Return NULL if "expr" does not (obviously) represent an integer.
329 __isl_give isl_val
*PetScan::extract_int(clang::ParenExpr
*expr
)
331 return extract_int(expr
->getSubExpr());
334 /* Extract an integer from "expr".
335 * Return NULL if "expr" does not (obviously) represent an integer.
337 __isl_give isl_val
*PetScan::extract_int(clang::Expr
*expr
)
339 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
340 return extract_int(ctx
, cast
<IntegerLiteral
>(expr
));
341 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
342 return extract_int(cast
<ParenExpr
>(expr
));
348 /* Extract an affine expression from the IntegerLiteral "expr".
350 __isl_give isl_pw_aff
*PetScan::extract_affine(IntegerLiteral
*expr
)
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
);
358 v
= extract_int(expr
);
359 aff
= isl_aff_add_constant_val(aff
, v
);
361 return isl_pw_aff_alloc(dom
, aff
);
364 /* Extract an affine expression from the APInt "val", which is assumed
365 * to be non-negative.
367 __isl_give isl_pw_aff
*PetScan::extract_affine(const llvm::APInt
&val
)
369 isl_space
*dim
= isl_space_params_alloc(ctx
, 0);
370 isl_local_space
*ls
= isl_local_space_from_space(isl_space_copy(dim
));
371 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
372 isl_set
*dom
= isl_set_universe(dim
);
375 v
= extract_unsigned(ctx
, val
);
376 aff
= isl_aff_add_constant_val(aff
, v
);
378 return isl_pw_aff_alloc(dom
, aff
);
381 __isl_give isl_pw_aff
*PetScan::extract_affine(ImplicitCastExpr
*expr
)
383 return extract_affine(expr
->getSubExpr());
386 static unsigned get_type_size(ValueDecl
*decl
)
388 return decl
->getASTContext().getIntWidth(decl
->getType());
391 /* Bound parameter "pos" of "set" to the possible values of "decl".
393 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
394 unsigned pos
, ValueDecl
*decl
)
400 ctx
= isl_set_get_ctx(set
);
401 width
= get_type_size(decl
);
402 if (decl
->getType()->isUnsignedIntegerType()) {
403 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
404 bound
= isl_val_int_from_ui(ctx
, width
);
405 bound
= isl_val_2exp(bound
);
406 bound
= isl_val_sub_ui(bound
, 1);
407 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
, bound
);
409 bound
= isl_val_int_from_ui(ctx
, width
- 1);
410 bound
= isl_val_2exp(bound
);
411 bound
= isl_val_sub_ui(bound
, 1);
412 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
,
413 isl_val_copy(bound
));
414 bound
= isl_val_neg(bound
);
415 bound
= isl_val_sub_ui(bound
, 1);
416 set
= isl_set_lower_bound_val(set
, isl_dim_param
, pos
, bound
);
422 /* Extract an affine expression from the DeclRefExpr "expr".
424 * If the variable has been assigned a value, then we check whether
425 * we know what (affine) value was assigned.
426 * If so, we return this value. Otherwise we convert "expr"
427 * to an extra parameter (provided nesting_enabled is set).
429 * Otherwise, we simply return an expression that is equal
430 * to a parameter corresponding to the referenced variable.
432 __isl_give isl_pw_aff
*PetScan::extract_affine(DeclRefExpr
*expr
)
434 ValueDecl
*decl
= expr
->getDecl();
435 const Type
*type
= decl
->getType().getTypePtr();
441 if (!type
->isIntegerType()) {
446 if (assigned_value
.find(decl
) != assigned_value
.end()) {
447 if (assigned_value
[decl
])
448 return isl_pw_aff_copy(assigned_value
[decl
]);
450 return nested_access(expr
);
453 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
454 dim
= isl_space_params_alloc(ctx
, 1);
456 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
458 dom
= isl_set_universe(isl_space_copy(dim
));
459 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
460 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
462 return isl_pw_aff_alloc(dom
, aff
);
465 /* Extract an affine expression from an integer division operation.
466 * In particular, if "expr" is lhs/rhs, then return
468 * lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs)
470 * The second argument (rhs) is required to be a (positive) integer constant.
472 __isl_give isl_pw_aff
*PetScan::extract_affine_div(BinaryOperator
*expr
)
475 isl_pw_aff
*rhs
, *lhs
;
477 rhs
= extract_affine(expr
->getRHS());
478 is_cst
= isl_pw_aff_is_cst(rhs
);
479 if (is_cst
< 0 || !is_cst
) {
480 isl_pw_aff_free(rhs
);
486 lhs
= extract_affine(expr
->getLHS());
488 return isl_pw_aff_tdiv_q(lhs
, rhs
);
491 /* Extract an affine expression from a modulo operation.
492 * In particular, if "expr" is lhs/rhs, then return
494 * lhs - rhs * (lhs >= 0 ? floor(lhs/rhs) : ceil(lhs/rhs))
496 * The second argument (rhs) is required to be a (positive) integer constant.
498 __isl_give isl_pw_aff
*PetScan::extract_affine_mod(BinaryOperator
*expr
)
501 isl_pw_aff
*rhs
, *lhs
;
503 rhs
= extract_affine(expr
->getRHS());
504 is_cst
= isl_pw_aff_is_cst(rhs
);
505 if (is_cst
< 0 || !is_cst
) {
506 isl_pw_aff_free(rhs
);
512 lhs
= extract_affine(expr
->getLHS());
514 return isl_pw_aff_tdiv_r(lhs
, rhs
);
517 /* Extract an affine expression from a multiplication operation.
518 * This is only allowed if at least one of the two arguments
519 * is a (piecewise) constant.
521 __isl_give isl_pw_aff
*PetScan::extract_affine_mul(BinaryOperator
*expr
)
526 lhs
= extract_affine(expr
->getLHS());
527 rhs
= extract_affine(expr
->getRHS());
529 if (!isl_pw_aff_is_cst(lhs
) && !isl_pw_aff_is_cst(rhs
)) {
530 isl_pw_aff_free(lhs
);
531 isl_pw_aff_free(rhs
);
536 return isl_pw_aff_mul(lhs
, rhs
);
539 /* Extract an affine expression from an addition or subtraction operation.
541 __isl_give isl_pw_aff
*PetScan::extract_affine_add(BinaryOperator
*expr
)
546 lhs
= extract_affine(expr
->getLHS());
547 rhs
= extract_affine(expr
->getRHS());
549 switch (expr
->getOpcode()) {
551 return isl_pw_aff_add(lhs
, rhs
);
553 return isl_pw_aff_sub(lhs
, rhs
);
555 isl_pw_aff_free(lhs
);
556 isl_pw_aff_free(rhs
);
566 static __isl_give isl_pw_aff
*wrap(__isl_take isl_pw_aff
*pwaff
,
572 ctx
= isl_pw_aff_get_ctx(pwaff
);
573 mod
= isl_val_int_from_ui(ctx
, width
);
574 mod
= isl_val_2exp(mod
);
576 pwaff
= isl_pw_aff_mod_val(pwaff
, mod
);
581 /* Limit the domain of "pwaff" to those elements where the function
584 * 2^{width-1} <= pwaff < 2^{width-1}
586 static __isl_give isl_pw_aff
*avoid_overflow(__isl_take isl_pw_aff
*pwaff
,
591 isl_space
*space
= isl_pw_aff_get_domain_space(pwaff
);
592 isl_local_space
*ls
= isl_local_space_from_space(space
);
597 ctx
= isl_pw_aff_get_ctx(pwaff
);
598 v
= isl_val_int_from_ui(ctx
, width
- 1);
601 bound
= isl_aff_zero_on_domain(ls
);
602 bound
= isl_aff_add_constant_val(bound
, v
);
603 b
= isl_pw_aff_from_aff(bound
);
605 dom
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff
), isl_pw_aff_copy(b
));
606 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
608 b
= isl_pw_aff_neg(b
);
609 dom
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff
), b
);
610 pwaff
= isl_pw_aff_intersect_domain(pwaff
, dom
);
615 /* Handle potential overflows on signed computations.
617 * If options->signed_overflow is set to PET_OVERFLOW_AVOID,
618 * the we adjust the domain of "pa" to avoid overflows.
620 __isl_give isl_pw_aff
*PetScan::signed_overflow(__isl_take isl_pw_aff
*pa
,
623 if (options
->signed_overflow
== PET_OVERFLOW_AVOID
)
624 pa
= avoid_overflow(pa
, width
);
629 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
631 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
632 __isl_take isl_set
*dom
)
635 pa
= isl_set_indicator_function(set
);
636 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
640 /* Extract an affine expression from some binary operations.
641 * If the result of the expression is unsigned, then we wrap it
642 * based on the size of the type. Otherwise, we ensure that
643 * no overflow occurs.
645 __isl_give isl_pw_aff
*PetScan::extract_affine(BinaryOperator
*expr
)
650 switch (expr
->getOpcode()) {
653 res
= extract_affine_add(expr
);
656 res
= extract_affine_div(expr
);
659 res
= extract_affine_mod(expr
);
662 res
= extract_affine_mul(expr
);
672 return extract_condition(expr
);
678 width
= ast_context
.getIntWidth(expr
->getType());
679 if (expr
->getType()->isUnsignedIntegerType())
680 res
= wrap(res
, width
);
682 res
= signed_overflow(res
, width
);
687 /* Extract an affine expression from a negation operation.
689 __isl_give isl_pw_aff
*PetScan::extract_affine(UnaryOperator
*expr
)
691 if (expr
->getOpcode() == UO_Minus
)
692 return isl_pw_aff_neg(extract_affine(expr
->getSubExpr()));
693 if (expr
->getOpcode() == UO_LNot
)
694 return extract_condition(expr
);
700 __isl_give isl_pw_aff
*PetScan::extract_affine(ParenExpr
*expr
)
702 return extract_affine(expr
->getSubExpr());
705 /* Extract an affine expression from some special function calls.
706 * In particular, we handle "min", "max", "ceild" and "floord".
707 * In case of the latter two, the second argument needs to be
708 * a (positive) integer constant.
710 __isl_give isl_pw_aff
*PetScan::extract_affine(CallExpr
*expr
)
714 isl_pw_aff
*aff1
, *aff2
;
716 fd
= expr
->getDirectCallee();
722 name
= fd
->getDeclName().getAsString();
723 if (!(expr
->getNumArgs() == 2 && name
== "min") &&
724 !(expr
->getNumArgs() == 2 && name
== "max") &&
725 !(expr
->getNumArgs() == 2 && name
== "floord") &&
726 !(expr
->getNumArgs() == 2 && name
== "ceild")) {
731 if (name
== "min" || name
== "max") {
732 aff1
= extract_affine(expr
->getArg(0));
733 aff2
= extract_affine(expr
->getArg(1));
736 aff1
= isl_pw_aff_min(aff1
, aff2
);
738 aff1
= isl_pw_aff_max(aff1
, aff2
);
739 } else if (name
== "floord" || name
== "ceild") {
741 Expr
*arg2
= expr
->getArg(1);
743 if (arg2
->getStmtClass() != Stmt::IntegerLiteralClass
) {
747 aff1
= extract_affine(expr
->getArg(0));
748 v
= extract_int(cast
<IntegerLiteral
>(arg2
));
749 aff1
= isl_pw_aff_scale_down_val(aff1
, v
);
750 if (name
== "floord")
751 aff1
= isl_pw_aff_floor(aff1
);
753 aff1
= isl_pw_aff_ceil(aff1
);
762 /* This method is called when we come across an access that is
763 * nested in what is supposed to be an affine expression.
764 * If nesting is allowed, we return a new parameter that corresponds
765 * to this nested access. Otherwise, we simply complain.
767 * Note that we currently don't allow nested accesses themselves
768 * to contain any nested accesses, so we check if we can extract
769 * the access without any nesting and complain if we can't.
771 * The new parameter is resolved in resolve_nested.
773 isl_pw_aff
*PetScan::nested_access(Expr
*expr
)
779 isl_multi_pw_aff
*index
;
781 if (!nesting_enabled
) {
786 allow_nested
= false;
787 index
= extract_index(expr
);
793 isl_multi_pw_aff_free(index
);
795 id
= isl_id_alloc(ctx
, NULL
, expr
);
796 dim
= isl_space_params_alloc(ctx
, 1);
798 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
800 dom
= isl_set_universe(isl_space_copy(dim
));
801 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
802 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
804 return isl_pw_aff_alloc(dom
, aff
);
807 /* Affine expressions are not supposed to contain array accesses,
808 * but if nesting is allowed, we return a parameter corresponding
809 * to the array access.
811 __isl_give isl_pw_aff
*PetScan::extract_affine(ArraySubscriptExpr
*expr
)
813 return nested_access(expr
);
816 /* Affine expressions are not supposed to contain member accesses,
817 * but if nesting is allowed, we return a parameter corresponding
818 * to the member access.
820 __isl_give isl_pw_aff
*PetScan::extract_affine(MemberExpr
*expr
)
822 return nested_access(expr
);
825 /* Extract an affine expression from a conditional operation.
827 __isl_give isl_pw_aff
*PetScan::extract_affine(ConditionalOperator
*expr
)
829 isl_pw_aff
*cond
, *lhs
, *rhs
;
831 cond
= extract_condition(expr
->getCond());
832 lhs
= extract_affine(expr
->getTrueExpr());
833 rhs
= extract_affine(expr
->getFalseExpr());
835 return isl_pw_aff_cond(cond
, lhs
, rhs
);
838 /* Extract an affine expression, if possible, from "expr".
839 * Otherwise return NULL.
841 __isl_give isl_pw_aff
*PetScan::extract_affine(Expr
*expr
)
843 switch (expr
->getStmtClass()) {
844 case Stmt::ImplicitCastExprClass
:
845 return extract_affine(cast
<ImplicitCastExpr
>(expr
));
846 case Stmt::IntegerLiteralClass
:
847 return extract_affine(cast
<IntegerLiteral
>(expr
));
848 case Stmt::DeclRefExprClass
:
849 return extract_affine(cast
<DeclRefExpr
>(expr
));
850 case Stmt::BinaryOperatorClass
:
851 return extract_affine(cast
<BinaryOperator
>(expr
));
852 case Stmt::UnaryOperatorClass
:
853 return extract_affine(cast
<UnaryOperator
>(expr
));
854 case Stmt::ParenExprClass
:
855 return extract_affine(cast
<ParenExpr
>(expr
));
856 case Stmt::CallExprClass
:
857 return extract_affine(cast
<CallExpr
>(expr
));
858 case Stmt::ArraySubscriptExprClass
:
859 return extract_affine(cast
<ArraySubscriptExpr
>(expr
));
860 case Stmt::MemberExprClass
:
861 return extract_affine(cast
<MemberExpr
>(expr
));
862 case Stmt::ConditionalOperatorClass
:
863 return extract_affine(cast
<ConditionalOperator
>(expr
));
870 __isl_give isl_multi_pw_aff
*PetScan::extract_index(ImplicitCastExpr
*expr
)
872 return extract_index(expr
->getSubExpr());
875 /* Return the depth of an array of the given type.
877 static int array_depth(const Type
*type
)
879 if (type
->isPointerType())
880 return 1 + array_depth(type
->getPointeeType().getTypePtr());
881 if (type
->isArrayType()) {
882 const ArrayType
*atype
;
883 type
= type
->getCanonicalTypeInternal().getTypePtr();
884 atype
= cast
<ArrayType
>(type
);
885 return 1 + array_depth(atype
->getElementType().getTypePtr());
890 /* Return the depth of the array accessed by the index expression "index".
891 * If "index" is an affine expression, i.e., if it does not access
892 * any array, then return 1.
893 * If "index" represent a member access, i.e., if its range is a wrapped
894 * relation, then return the sum of the depth of the array of structures
895 * and that of the member inside the structure.
897 static int extract_depth(__isl_keep isl_multi_pw_aff
*index
)
905 if (isl_multi_pw_aff_range_is_wrapping(index
)) {
906 int domain_depth
, range_depth
;
907 isl_multi_pw_aff
*domain
, *range
;
909 domain
= isl_multi_pw_aff_copy(index
);
910 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
911 domain_depth
= extract_depth(domain
);
912 isl_multi_pw_aff_free(domain
);
913 range
= isl_multi_pw_aff_copy(index
);
914 range
= isl_multi_pw_aff_range_factor_range(range
);
915 range_depth
= extract_depth(range
);
916 isl_multi_pw_aff_free(range
);
918 return domain_depth
+ range_depth
;
921 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
924 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
927 decl
= (ValueDecl
*) isl_id_get_user(id
);
930 return array_depth(decl
->getType().getTypePtr());
933 /* Extract an index expression from a reference to a variable.
934 * If the variable has name "A", then the returned index expression
939 __isl_give isl_multi_pw_aff
*PetScan::extract_index(DeclRefExpr
*expr
)
941 return extract_index(expr
->getDecl());
944 /* Extract an index expression from a variable.
945 * If the variable has name "A", then the returned index expression
950 __isl_give isl_multi_pw_aff
*PetScan::extract_index(ValueDecl
*decl
)
952 isl_id
*id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
953 isl_space
*space
= isl_space_alloc(ctx
, 0, 0, 0);
955 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
957 return isl_multi_pw_aff_zero(space
);
960 /* Extract an index expression from an integer contant.
961 * If the value of the constant is "v", then the returned access relation
966 __isl_give isl_multi_pw_aff
*PetScan::extract_index(IntegerLiteral
*expr
)
968 isl_multi_pw_aff
*mpa
;
970 mpa
= isl_multi_pw_aff_from_pw_aff(extract_affine(expr
));
971 mpa
= isl_multi_pw_aff_from_range(mpa
);
975 /* Try and extract an index expression from the given Expr.
976 * Return NULL if it doesn't work out.
978 __isl_give isl_multi_pw_aff
*PetScan::extract_index(Expr
*expr
)
980 switch (expr
->getStmtClass()) {
981 case Stmt::ImplicitCastExprClass
:
982 return extract_index(cast
<ImplicitCastExpr
>(expr
));
983 case Stmt::DeclRefExprClass
:
984 return extract_index(cast
<DeclRefExpr
>(expr
));
985 case Stmt::ArraySubscriptExprClass
:
986 return extract_index(cast
<ArraySubscriptExpr
>(expr
));
987 case Stmt::IntegerLiteralClass
:
988 return extract_index(cast
<IntegerLiteral
>(expr
));
989 case Stmt::MemberExprClass
:
990 return extract_index(cast
<MemberExpr
>(expr
));
997 /* Given a partial index expression "base" and an extra index "index",
998 * append the extra index to "base" and return the result.
999 * Additionally, add the constraints that the extra index is non-negative.
1000 * If "index" represent a member access, i.e., if its range is a wrapped
1001 * relation, then we recursively extend the range of this nested relation.
1003 static __isl_give isl_multi_pw_aff
*subscript(__isl_take isl_multi_pw_aff
*base
,
1004 __isl_take isl_pw_aff
*index
)
1008 isl_multi_pw_aff
*access
;
1011 member_access
= isl_multi_pw_aff_range_is_wrapping(base
);
1012 if (member_access
< 0)
1014 if (member_access
) {
1015 isl_multi_pw_aff
*domain
, *range
;
1018 id
= isl_multi_pw_aff_get_tuple_id(base
, isl_dim_out
);
1019 domain
= isl_multi_pw_aff_copy(base
);
1020 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
1021 range
= isl_multi_pw_aff_range_factor_range(base
);
1022 range
= subscript(range
, index
);
1023 access
= isl_multi_pw_aff_range_product(domain
, range
);
1024 access
= isl_multi_pw_aff_set_tuple_id(access
, isl_dim_out
, id
);
1028 id
= isl_multi_pw_aff_get_tuple_id(base
, isl_dim_set
);
1029 index
= isl_pw_aff_from_range(index
);
1030 domain
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(index
));
1031 index
= isl_pw_aff_intersect_domain(index
, domain
);
1032 access
= isl_multi_pw_aff_from_pw_aff(index
);
1033 access
= isl_multi_pw_aff_flat_range_product(base
, access
);
1034 access
= isl_multi_pw_aff_set_tuple_id(access
, isl_dim_set
, id
);
1038 isl_multi_pw_aff_free(base
);
1039 isl_pw_aff_free(index
);
1043 /* Extract an index expression from the given array subscript expression.
1044 * If nesting is allowed in general, then we turn it on while
1045 * examining the index expression.
1047 * We first extract an index expression from the base.
1048 * This will result in an index expression with a range that corresponds
1049 * to the earlier indices.
1050 * We then extract the current index, restrict its domain
1051 * to those values that result in a non-negative index and
1052 * append the index to the base index expression.
1054 __isl_give isl_multi_pw_aff
*PetScan::extract_index(ArraySubscriptExpr
*expr
)
1056 Expr
*base
= expr
->getBase();
1057 Expr
*idx
= expr
->getIdx();
1059 isl_multi_pw_aff
*base_access
;
1060 isl_multi_pw_aff
*access
;
1061 bool save_nesting
= nesting_enabled
;
1063 nesting_enabled
= allow_nested
;
1065 base_access
= extract_index(base
);
1066 index
= extract_affine(idx
);
1068 nesting_enabled
= save_nesting
;
1070 access
= subscript(base_access
, index
);
1075 /* Construct a name for a member access by concatenating the name
1076 * of the array of structures and the member, separated by an underscore.
1078 * The caller is responsible for freeing the result.
1080 static char *member_access_name(isl_ctx
*ctx
, const char *base
,
1086 len
= strlen(base
) + 1 + strlen(field
);
1087 name
= isl_alloc_array(ctx
, char, len
+ 1);
1090 snprintf(name
, len
+ 1, "%s_%s", base
, field
);
1095 /* Given an index expression "base" for an element of an array of structures
1096 * and an expression "field" for the field member being accessed, construct
1097 * an index expression for an access to that member of the given structure.
1098 * In particular, take the range product of "base" and "field" and
1099 * attach a name to the result.
1101 static __isl_give isl_multi_pw_aff
*member(__isl_take isl_multi_pw_aff
*base
,
1102 __isl_take isl_multi_pw_aff
*field
)
1105 isl_multi_pw_aff
*access
;
1106 const char *base_name
, *field_name
;
1109 ctx
= isl_multi_pw_aff_get_ctx(base
);
1111 base_name
= isl_multi_pw_aff_get_tuple_name(base
, isl_dim_out
);
1112 field_name
= isl_multi_pw_aff_get_tuple_name(field
, isl_dim_out
);
1113 name
= member_access_name(ctx
, base_name
, field_name
);
1115 access
= isl_multi_pw_aff_range_product(base
, field
);
1117 access
= isl_multi_pw_aff_set_tuple_name(access
, isl_dim_out
, name
);
1123 /* Extract an index expression from a member expression.
1125 * If the base access (to the structure containing the member)
1130 * and the member is called "f", then the member access is of
1133 * [] -> A_f[A[..] -> f[]]
1135 * If the member access is to an anonymous struct, then simply return
1139 * If the member access in the source code is of the form
1143 * then it is treated as
1147 __isl_give isl_multi_pw_aff
*PetScan::extract_index(MemberExpr
*expr
)
1149 Expr
*base
= expr
->getBase();
1150 FieldDecl
*field
= cast
<FieldDecl
>(expr
->getMemberDecl());
1151 isl_multi_pw_aff
*base_access
, *field_access
;
1155 base_access
= extract_index(base
);
1157 if (expr
->isArrow()) {
1158 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
1159 isl_local_space
*ls
= isl_local_space_from_space(space
);
1160 isl_aff
*aff
= isl_aff_zero_on_domain(ls
);
1161 isl_pw_aff
*index
= isl_pw_aff_from_aff(aff
);
1162 base_access
= subscript(base_access
, index
);
1165 if (field
->isAnonymousStructOrUnion())
1168 id
= isl_id_alloc(ctx
, field
->getName().str().c_str(), field
);
1169 space
= isl_multi_pw_aff_get_domain_space(base_access
);
1170 space
= isl_space_from_domain(space
);
1171 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
1172 field_access
= isl_multi_pw_aff_zero(space
);
1174 return member(base_access
, field_access
);
1177 /* Check if "expr" calls function "minmax" with two arguments and if so
1178 * make lhs and rhs refer to these two arguments.
1180 static bool is_minmax(Expr
*expr
, const char *minmax
, Expr
*&lhs
, Expr
*&rhs
)
1186 if (expr
->getStmtClass() != Stmt::CallExprClass
)
1189 call
= cast
<CallExpr
>(expr
);
1190 fd
= call
->getDirectCallee();
1194 if (call
->getNumArgs() != 2)
1197 name
= fd
->getDeclName().getAsString();
1201 lhs
= call
->getArg(0);
1202 rhs
= call
->getArg(1);
1207 /* Check if "expr" is of the form min(lhs, rhs) and if so make
1208 * lhs and rhs refer to the two arguments.
1210 static bool is_min(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
1212 return is_minmax(expr
, "min", lhs
, rhs
);
1215 /* Check if "expr" is of the form max(lhs, rhs) and if so make
1216 * lhs and rhs refer to the two arguments.
1218 static bool is_max(Expr
*expr
, Expr
*&lhs
, Expr
*&rhs
)
1220 return is_minmax(expr
, "max", lhs
, rhs
);
1223 /* Return "lhs && rhs", defined on the shared definition domain.
1225 static __isl_give isl_pw_aff
*pw_aff_and(__isl_take isl_pw_aff
*lhs
,
1226 __isl_take isl_pw_aff
*rhs
)
1231 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
1232 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1233 cond
= isl_set_intersect(isl_pw_aff_non_zero_set(lhs
),
1234 isl_pw_aff_non_zero_set(rhs
));
1235 return indicator_function(cond
, dom
);
1238 /* Return "lhs && rhs", with shortcut semantics.
1239 * That is, if lhs is false, then the result is defined even if rhs is not.
1240 * In practice, we compute lhs ? rhs : lhs.
1242 static __isl_give isl_pw_aff
*pw_aff_and_then(__isl_take isl_pw_aff
*lhs
,
1243 __isl_take isl_pw_aff
*rhs
)
1245 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), rhs
, lhs
);
1248 /* Return "lhs || rhs", with shortcut semantics.
1249 * That is, if lhs is true, then the result is defined even if rhs is not.
1250 * In practice, we compute lhs ? lhs : rhs.
1252 static __isl_give isl_pw_aff
*pw_aff_or_else(__isl_take isl_pw_aff
*lhs
,
1253 __isl_take isl_pw_aff
*rhs
)
1255 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), lhs
, rhs
);
1258 /* Extract an affine expressions representing the comparison "LHS op RHS"
1259 * "comp" is the original statement that "LHS op RHS" is derived from
1260 * and is used for diagnostics.
1262 * If the comparison is of the form
1266 * then the expression is constructed as the conjunction of
1271 * A similar optimization is performed for max(a,b) <= c.
1272 * We do this because that will lead to simpler representations
1273 * of the expression.
1274 * If isl is ever enhanced to explicitly deal with min and max expressions,
1275 * this optimization can be removed.
1277 __isl_give isl_pw_aff
*PetScan::extract_comparison(BinaryOperatorKind op
,
1278 Expr
*LHS
, Expr
*RHS
, Stmt
*comp
)
1287 return extract_comparison(BO_LT
, RHS
, LHS
, comp
);
1289 return extract_comparison(BO_LE
, RHS
, LHS
, comp
);
1291 if (op
== BO_LT
|| op
== BO_LE
) {
1292 Expr
*expr1
, *expr2
;
1293 if (is_min(RHS
, expr1
, expr2
)) {
1294 lhs
= extract_comparison(op
, LHS
, expr1
, comp
);
1295 rhs
= extract_comparison(op
, LHS
, expr2
, comp
);
1296 return pw_aff_and(lhs
, rhs
);
1298 if (is_max(LHS
, expr1
, expr2
)) {
1299 lhs
= extract_comparison(op
, expr1
, RHS
, comp
);
1300 rhs
= extract_comparison(op
, expr2
, RHS
, comp
);
1301 return pw_aff_and(lhs
, rhs
);
1305 lhs
= extract_affine(LHS
);
1306 rhs
= extract_affine(RHS
);
1308 dom
= isl_pw_aff_domain(isl_pw_aff_copy(lhs
));
1309 dom
= isl_set_intersect(dom
, isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
1313 cond
= isl_pw_aff_lt_set(lhs
, rhs
);
1316 cond
= isl_pw_aff_le_set(lhs
, rhs
);
1319 cond
= isl_pw_aff_eq_set(lhs
, rhs
);
1322 cond
= isl_pw_aff_ne_set(lhs
, rhs
);
1325 isl_pw_aff_free(lhs
);
1326 isl_pw_aff_free(rhs
);
1332 cond
= isl_set_coalesce(cond
);
1333 res
= indicator_function(cond
, dom
);
1338 __isl_give isl_pw_aff
*PetScan::extract_comparison(BinaryOperator
*comp
)
1340 return extract_comparison(comp
->getOpcode(), comp
->getLHS(),
1341 comp
->getRHS(), comp
);
1344 /* Extract an affine expression representing the negation (logical not)
1345 * of a subexpression.
1347 __isl_give isl_pw_aff
*PetScan::extract_boolean(UnaryOperator
*op
)
1349 isl_set
*set_cond
, *dom
;
1350 isl_pw_aff
*cond
, *res
;
1352 cond
= extract_condition(op
->getSubExpr());
1354 dom
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
1356 set_cond
= isl_pw_aff_zero_set(cond
);
1358 res
= indicator_function(set_cond
, dom
);
1363 /* Extract an affine expression representing the disjunction (logical or)
1364 * or conjunction (logical and) of two subexpressions.
1366 __isl_give isl_pw_aff
*PetScan::extract_boolean(BinaryOperator
*comp
)
1368 isl_pw_aff
*lhs
, *rhs
;
1370 lhs
= extract_condition(comp
->getLHS());
1371 rhs
= extract_condition(comp
->getRHS());
1373 switch (comp
->getOpcode()) {
1375 return pw_aff_and_then(lhs
, rhs
);
1377 return pw_aff_or_else(lhs
, rhs
);
1379 isl_pw_aff_free(lhs
);
1380 isl_pw_aff_free(rhs
);
1387 __isl_give isl_pw_aff
*PetScan::extract_condition(UnaryOperator
*expr
)
1389 switch (expr
->getOpcode()) {
1391 return extract_boolean(expr
);
1398 /* Extract the affine expression "expr != 0 ? 1 : 0".
1400 __isl_give isl_pw_aff
*PetScan::extract_implicit_condition(Expr
*expr
)
1405 res
= extract_affine(expr
);
1407 dom
= isl_pw_aff_domain(isl_pw_aff_copy(res
));
1408 set
= isl_pw_aff_non_zero_set(res
);
1410 res
= indicator_function(set
, dom
);
1415 /* Extract an affine expression from a boolean expression.
1416 * In particular, return the expression "expr ? 1 : 0".
1418 * If the expression doesn't look like a condition, we assume it
1419 * is an affine expression and return the condition "expr != 0 ? 1 : 0".
1421 __isl_give isl_pw_aff
*PetScan::extract_condition(Expr
*expr
)
1423 BinaryOperator
*comp
;
1426 isl_set
*u
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
1427 return indicator_function(u
, isl_set_copy(u
));
1430 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
1431 return extract_condition(cast
<ParenExpr
>(expr
)->getSubExpr());
1433 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
)
1434 return extract_condition(cast
<UnaryOperator
>(expr
));
1436 if (expr
->getStmtClass() != Stmt::BinaryOperatorClass
)
1437 return extract_implicit_condition(expr
);
1439 comp
= cast
<BinaryOperator
>(expr
);
1440 switch (comp
->getOpcode()) {
1447 return extract_comparison(comp
);
1450 return extract_boolean(comp
);
1452 return extract_implicit_condition(expr
);
1456 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
1460 return pet_op_minus
;
1462 return pet_op_post_inc
;
1464 return pet_op_post_dec
;
1466 return pet_op_pre_inc
;
1468 return pet_op_pre_dec
;
1474 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
1478 return pet_op_add_assign
;
1480 return pet_op_sub_assign
;
1482 return pet_op_mul_assign
;
1484 return pet_op_div_assign
;
1486 return pet_op_assign
;
1510 /* Construct a pet_expr representing a unary operator expression.
1512 struct pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
1514 struct pet_expr
*arg
;
1515 enum pet_op_type op
;
1517 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
1518 if (op
== pet_op_last
) {
1523 arg
= extract_expr(expr
->getSubExpr());
1525 if (expr
->isIncrementDecrementOp() &&
1526 arg
&& arg
->type
== pet_expr_access
) {
1531 return pet_expr_new_unary(ctx
, op
, arg
);
1534 /* Mark the given access pet_expr as a write.
1535 * If a scalar is being accessed, then mark its value
1536 * as unknown in assigned_value.
1538 void PetScan::mark_write(struct pet_expr
*access
)
1546 access
->acc
.write
= 1;
1547 access
->acc
.read
= 0;
1549 if (!pet_expr_is_scalar_access(access
))
1552 id
= pet_expr_access_get_id(access
);
1553 decl
= (ValueDecl
*) isl_id_get_user(id
);
1554 clear_assignment(assigned_value
, decl
);
1558 /* Assign "rhs" to "lhs".
1560 * In particular, if "lhs" is a scalar variable, then mark
1561 * the variable as having been assigned. If, furthermore, "rhs"
1562 * is an affine expression, then keep track of this value in assigned_value
1563 * so that we can plug it in when we later come across the same variable.
1565 void PetScan::assign(struct pet_expr
*lhs
, Expr
*rhs
)
1573 if (!pet_expr_is_scalar_access(lhs
))
1576 id
= pet_expr_access_get_id(lhs
);
1577 decl
= (ValueDecl
*) isl_id_get_user(id
);
1580 pa
= try_extract_affine(rhs
);
1581 clear_assignment(assigned_value
, decl
);
1584 assigned_value
[decl
] = pa
;
1585 insert_expression(pa
);
1588 /* Construct a pet_expr representing a binary operator expression.
1590 * If the top level operator is an assignment and the LHS is an access,
1591 * then we mark that access as a write. If the operator is a compound
1592 * assignment, the access is marked as both a read and a write.
1594 * If "expr" assigns something to a scalar variable, then we mark
1595 * the variable as having been assigned. If, furthermore, the expression
1596 * is affine, then keep track of this value in assigned_value
1597 * so that we can plug it in when we later come across the same variable.
1599 struct pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
1601 struct pet_expr
*lhs
, *rhs
;
1602 enum pet_op_type op
;
1604 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
1605 if (op
== pet_op_last
) {
1610 lhs
= extract_expr(expr
->getLHS());
1611 rhs
= extract_expr(expr
->getRHS());
1613 if (expr
->isAssignmentOp() && lhs
&& lhs
->type
== pet_expr_access
) {
1615 if (expr
->isCompoundAssignmentOp())
1619 if (expr
->getOpcode() == BO_Assign
)
1620 assign(lhs
, expr
->getRHS());
1622 return pet_expr_new_binary(ctx
, op
, lhs
, rhs
);
1625 /* Construct a pet_scop with a single statement killing the entire
1628 struct pet_scop
*PetScan::kill(Stmt
*stmt
, struct pet_array
*array
)
1632 isl_multi_pw_aff
*index
;
1634 struct pet_expr
*expr
;
1638 access
= isl_map_from_range(isl_set_copy(array
->extent
));
1639 id
= isl_set_get_tuple_id(array
->extent
);
1640 space
= isl_space_alloc(ctx
, 0, 0, 0);
1641 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
1642 index
= isl_multi_pw_aff_zero(space
);
1643 expr
= pet_expr_kill_from_access_and_index(access
, index
);
1644 return extract(stmt
, expr
);
1647 /* Construct a pet_scop for a (single) variable declaration.
1649 * The scop contains the variable being declared (as an array)
1650 * and a statement killing the array.
1652 * If the variable is initialized in the AST, then the scop
1653 * also contains an assignment to the variable.
1655 struct pet_scop
*PetScan::extract(DeclStmt
*stmt
)
1659 struct pet_expr
*lhs
, *rhs
, *pe
;
1660 struct pet_scop
*scop_decl
, *scop
;
1661 struct pet_array
*array
;
1663 if (!stmt
->isSingleDecl()) {
1668 decl
= stmt
->getSingleDecl();
1669 vd
= cast
<VarDecl
>(decl
);
1671 array
= extract_array(ctx
, vd
, NULL
);
1673 array
->declared
= 1;
1674 scop_decl
= kill(stmt
, array
);
1675 scop_decl
= pet_scop_add_array(scop_decl
, array
);
1680 lhs
= extract_access_expr(vd
);
1681 rhs
= extract_expr(vd
->getInit());
1684 assign(lhs
, vd
->getInit());
1686 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, lhs
, rhs
);
1687 scop
= extract(stmt
, pe
);
1689 scop_decl
= pet_scop_prefix(scop_decl
, 0);
1690 scop
= pet_scop_prefix(scop
, 1);
1692 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
1697 /* Construct a pet_expr representing a conditional operation.
1699 * We first try to extract the condition as an affine expression.
1700 * If that fails, we construct a pet_expr tree representing the condition.
1702 struct pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
1704 struct pet_expr
*cond
, *lhs
, *rhs
;
1707 pa
= try_extract_affine(expr
->getCond());
1709 isl_multi_pw_aff
*test
= isl_multi_pw_aff_from_pw_aff(pa
);
1710 test
= isl_multi_pw_aff_from_range(test
);
1711 cond
= pet_expr_from_index(test
);
1713 cond
= extract_expr(expr
->getCond());
1714 lhs
= extract_expr(expr
->getTrueExpr());
1715 rhs
= extract_expr(expr
->getFalseExpr());
1717 return pet_expr_new_ternary(ctx
, cond
, lhs
, rhs
);
1720 struct pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
1722 return extract_expr(expr
->getSubExpr());
1725 /* Construct a pet_expr representing a floating point value.
1727 * If the floating point literal does not appear in a macro,
1728 * then we use the original representation in the source code
1729 * as the string representation. Otherwise, we use the pretty
1730 * printer to produce a string representation.
1732 struct pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
1736 const LangOptions
&LO
= PP
.getLangOpts();
1737 SourceLocation loc
= expr
->getLocation();
1739 if (!loc
.isMacroID()) {
1740 SourceManager
&SM
= PP
.getSourceManager();
1741 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
1742 s
= string(SM
.getCharacterData(loc
), len
);
1744 llvm::raw_string_ostream
S(s
);
1745 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
1748 d
= expr
->getValueAsApproximateDouble();
1749 return pet_expr_new_double(ctx
, d
, s
.c_str());
1752 /* Extract an index expression from "expr" and then convert it into
1753 * an access pet_expr.
1755 struct pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
1757 isl_multi_pw_aff
*index
;
1758 struct pet_expr
*pe
;
1761 index
= extract_index(expr
);
1762 depth
= extract_depth(index
);
1764 pe
= pet_expr_from_index_and_depth(index
, depth
);
1769 /* Extract an index expression from "decl" and then convert it into
1770 * an access pet_expr.
1772 struct pet_expr
*PetScan::extract_access_expr(ValueDecl
*decl
)
1774 isl_multi_pw_aff
*index
;
1775 struct pet_expr
*pe
;
1778 index
= extract_index(decl
);
1779 depth
= extract_depth(index
);
1781 pe
= pet_expr_from_index_and_depth(index
, depth
);
1786 struct pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
1788 return extract_expr(expr
->getSubExpr());
1791 /* Construct a pet_expr representing a function call.
1793 * If we are passing along a pointer to an array element
1794 * or an entire row or even higher dimensional slice of an array,
1795 * then the function being called may write into the array.
1797 * We assume here that if the function is declared to take a pointer
1798 * to a const type, then the function will perform a read
1799 * and that otherwise, it will perform a write.
1801 struct pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1803 struct pet_expr
*res
= NULL
;
1807 fd
= expr
->getDirectCallee();
1813 name
= fd
->getDeclName().getAsString();
1814 res
= pet_expr_new_call(ctx
, name
.c_str(), expr
->getNumArgs());
1818 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
1819 Expr
*arg
= expr
->getArg(i
);
1824 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
1825 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(arg
);
1826 arg
= ice
->getSubExpr();
1828 if (arg
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1829 UnaryOperator
*op
= cast
<UnaryOperator
>(arg
);
1830 if (op
->getOpcode() == UO_AddrOf
) {
1832 arg
= op
->getSubExpr();
1835 res
->args
[i
] = PetScan::extract_expr(arg
);
1836 main_arg
= res
->args
[i
];
1838 res
->args
[i
] = pet_expr_new_unary(ctx
,
1839 pet_op_address_of
, res
->args
[i
]);
1842 sc
= arg
->getStmtClass();
1843 if ((sc
== Stmt::ArraySubscriptExprClass
||
1844 sc
== Stmt::MemberExprClass
) &&
1845 array_depth(arg
->getType().getTypePtr()) > 0)
1847 if (is_addr
&& main_arg
->type
== pet_expr_access
) {
1849 if (!fd
->hasPrototype()) {
1850 unsupported(expr
, "prototype required");
1853 parm
= fd
->getParamDecl(i
);
1854 if (!const_base(parm
->getType()))
1855 mark_write(main_arg
);
1865 /* Construct a pet_expr representing a (C style) cast.
1867 struct pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
1869 struct pet_expr
*arg
;
1872 arg
= extract_expr(expr
->getSubExpr());
1876 type
= expr
->getTypeAsWritten();
1877 return pet_expr_new_cast(ctx
, type
.getAsString().c_str(), arg
);
1880 /* Try and onstruct a pet_expr representing "expr".
1882 struct pet_expr
*PetScan::extract_expr(Expr
*expr
)
1884 switch (expr
->getStmtClass()) {
1885 case Stmt::UnaryOperatorClass
:
1886 return extract_expr(cast
<UnaryOperator
>(expr
));
1887 case Stmt::CompoundAssignOperatorClass
:
1888 case Stmt::BinaryOperatorClass
:
1889 return extract_expr(cast
<BinaryOperator
>(expr
));
1890 case Stmt::ImplicitCastExprClass
:
1891 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1892 case Stmt::ArraySubscriptExprClass
:
1893 case Stmt::DeclRefExprClass
:
1894 case Stmt::IntegerLiteralClass
:
1895 case Stmt::MemberExprClass
:
1896 return extract_access_expr(expr
);
1897 case Stmt::FloatingLiteralClass
:
1898 return extract_expr(cast
<FloatingLiteral
>(expr
));
1899 case Stmt::ParenExprClass
:
1900 return extract_expr(cast
<ParenExpr
>(expr
));
1901 case Stmt::ConditionalOperatorClass
:
1902 return extract_expr(cast
<ConditionalOperator
>(expr
));
1903 case Stmt::CallExprClass
:
1904 return extract_expr(cast
<CallExpr
>(expr
));
1905 case Stmt::CStyleCastExprClass
:
1906 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1913 /* Check if the given initialization statement is an assignment.
1914 * If so, return that assignment. Otherwise return NULL.
1916 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1918 BinaryOperator
*ass
;
1920 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1923 ass
= cast
<BinaryOperator
>(init
);
1924 if (ass
->getOpcode() != BO_Assign
)
1930 /* Check if the given initialization statement is a declaration
1931 * of a single variable.
1932 * If so, return that declaration. Otherwise return NULL.
1934 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1938 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1941 decl
= cast
<DeclStmt
>(init
);
1943 if (!decl
->isSingleDecl())
1946 return decl
->getSingleDecl();
1949 /* Given the assignment operator in the initialization of a for loop,
1950 * extract the induction variable, i.e., the (integer)variable being
1953 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1960 lhs
= init
->getLHS();
1961 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1966 ref
= cast
<DeclRefExpr
>(lhs
);
1967 decl
= ref
->getDecl();
1968 type
= decl
->getType().getTypePtr();
1970 if (!type
->isIntegerType()) {
1978 /* Given the initialization statement of a for loop and the single
1979 * declaration in this initialization statement,
1980 * extract the induction variable, i.e., the (integer) variable being
1983 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1987 vd
= cast
<VarDecl
>(decl
);
1989 const QualType type
= vd
->getType();
1990 if (!type
->isIntegerType()) {
1995 if (!vd
->getInit()) {
2003 /* Check that op is of the form iv++ or iv--.
2004 * Return an affine expression "1" or "-1" accordingly.
2006 __isl_give isl_pw_aff
*PetScan::extract_unary_increment(
2007 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
2014 if (!op
->isIncrementDecrementOp()) {
2019 sub
= op
->getSubExpr();
2020 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
2025 ref
= cast
<DeclRefExpr
>(sub
);
2026 if (ref
->getDecl() != iv
) {
2031 space
= isl_space_params_alloc(ctx
, 0);
2032 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2034 if (op
->isIncrementOp())
2035 aff
= isl_aff_add_constant_si(aff
, 1);
2037 aff
= isl_aff_add_constant_si(aff
, -1);
2039 return isl_pw_aff_from_aff(aff
);
2042 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
2043 * has a single constant expression, then put this constant in *user.
2044 * The caller is assumed to have checked that this function will
2045 * be called exactly once.
2047 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
2050 isl_val
**inc
= (isl_val
**)user
;
2053 if (isl_aff_is_cst(aff
))
2054 *inc
= isl_aff_get_constant_val(aff
);
2064 /* Check if op is of the form
2068 * and return inc as an affine expression.
2070 * We extract an affine expression from the RHS, subtract iv and return
2073 __isl_give isl_pw_aff
*PetScan::extract_binary_increment(BinaryOperator
*op
,
2074 clang::ValueDecl
*iv
)
2083 if (op
->getOpcode() != BO_Assign
) {
2089 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
2094 ref
= cast
<DeclRefExpr
>(lhs
);
2095 if (ref
->getDecl() != iv
) {
2100 val
= extract_affine(op
->getRHS());
2102 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
2104 dim
= isl_space_params_alloc(ctx
, 1);
2105 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
2106 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2107 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
2109 val
= isl_pw_aff_sub(val
, isl_pw_aff_from_aff(aff
));
2114 /* Check that op is of the form iv += cst or iv -= cst
2115 * and return an affine expression corresponding oto cst or -cst accordingly.
2117 __isl_give isl_pw_aff
*PetScan::extract_compound_increment(
2118 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
2124 BinaryOperatorKind opcode
;
2126 opcode
= op
->getOpcode();
2127 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
2131 if (opcode
== BO_SubAssign
)
2135 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
2140 ref
= cast
<DeclRefExpr
>(lhs
);
2141 if (ref
->getDecl() != iv
) {
2146 val
= extract_affine(op
->getRHS());
2148 val
= isl_pw_aff_neg(val
);
2153 /* Check that the increment of the given for loop increments
2154 * (or decrements) the induction variable "iv" and return
2155 * the increment as an affine expression if successful.
2157 __isl_give isl_pw_aff
*PetScan::extract_increment(clang::ForStmt
*stmt
,
2160 Stmt
*inc
= stmt
->getInc();
2167 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
2168 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
2169 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
2170 return extract_compound_increment(
2171 cast
<CompoundAssignOperator
>(inc
), iv
);
2172 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
2173 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
2179 /* Embed the given iteration domain in an extra outer loop
2180 * with induction variable "var".
2181 * If this variable appeared as a parameter in the constraints,
2182 * it is replaced by the new outermost dimension.
2184 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
2185 __isl_take isl_id
*var
)
2189 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
2190 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
2192 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
2193 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2200 /* Return those elements in the space of "cond" that come after
2201 * (based on "sign") an element in "cond".
2203 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
2205 isl_map
*previous_to_this
;
2208 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
2210 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
2212 cond
= isl_set_apply(cond
, previous_to_this
);
2217 /* Create the infinite iteration domain
2219 * { [id] : id >= 0 }
2221 * If "scop" has an affine skip of type pet_skip_later,
2222 * then remove those iterations i that have an earlier iteration
2223 * where the skip condition is satisfied, meaning that iteration i
2225 * Since we are dealing with a loop without loop iterator,
2226 * the skip condition cannot refer to the current loop iterator and
2227 * so effectively, the returned set is of the form
2229 * { [0]; [id] : id >= 1 and not skip }
2231 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
,
2232 struct pet_scop
*scop
)
2234 isl_ctx
*ctx
= isl_id_get_ctx(id
);
2238 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
2239 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
2241 if (!pet_scop_has_affine_skip(scop
, pet_skip_later
))
2244 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
2245 skip
= embed(skip
, isl_id_copy(id
));
2246 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2247 domain
= isl_set_subtract(domain
, after(skip
, 1));
2252 /* Create an identity affine expression on the space containing "domain",
2253 * which is assumed to be one-dimensional.
2255 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
2257 isl_local_space
*ls
;
2259 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
2260 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
2263 /* Create an affine expression that maps elements
2264 * of a single-dimensional array "id_test" to the previous element
2265 * (according to "inc"), provided this element belongs to "domain".
2266 * That is, create the affine expression
2268 * { id[x] -> id[x - inc] : x - inc in domain }
2270 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
2271 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
2274 isl_local_space
*ls
;
2276 isl_multi_pw_aff
*prev
;
2278 space
= isl_set_get_space(domain
);
2279 ls
= isl_local_space_from_space(space
);
2280 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
2281 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
2282 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
2283 domain
= isl_set_preimage_multi_pw_aff(domain
,
2284 isl_multi_pw_aff_copy(prev
));
2285 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
2286 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
2291 /* Add an implication to "scop" expressing that if an element of
2292 * virtual array "id_test" has value "satisfied" then all previous elements
2293 * of this array also have that value. The set of previous elements
2294 * is bounded by "domain". If "sign" is negative then iterator
2295 * is decreasing and we express that all subsequent array elements
2296 * (but still defined previously) have the same value.
2298 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
2299 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
2305 domain
= isl_set_set_tuple_id(domain
, id_test
);
2306 space
= isl_set_get_space(domain
);
2308 map
= isl_map_lex_ge(space
);
2310 map
= isl_map_lex_le(space
);
2311 map
= isl_map_intersect_range(map
, domain
);
2312 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
2317 /* Add a filter to "scop" that imposes that it is only executed
2318 * when the variable identified by "id_test" has a zero value
2319 * for all previous iterations of "domain".
2321 * In particular, add a filter that imposes that the array
2322 * has a zero value at the previous iteration of domain and
2323 * add an implication that implies that it then has that
2324 * value for all previous iterations.
2326 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
2327 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
2328 __isl_take isl_val
*inc
)
2330 isl_multi_pw_aff
*prev
;
2331 int sign
= isl_val_sgn(inc
);
2333 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
2334 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
2335 scop
= pet_scop_filter(scop
, prev
, 0);
2340 /* Construct a pet_scop for an infinite loop around the given body.
2342 * We extract a pet_scop for the body and then embed it in a loop with
2351 * If the body contains any break, then it is taken into
2352 * account in infinite_domain (if the skip condition is affine)
2353 * or in scop_add_break (if the skip condition is not affine).
2355 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
)
2357 isl_id
*id
, *id_test
;
2360 struct pet_scop
*scop
;
2363 scop
= extract(body
);
2367 id
= isl_id_alloc(ctx
, "t", NULL
);
2368 domain
= infinite_domain(isl_id_copy(id
), scop
);
2369 ident
= identity_aff(domain
);
2371 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
2373 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
2375 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
2376 isl_map_from_aff(isl_aff_copy(ident
)), ident
, id
);
2378 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
2380 isl_set_free(domain
);
2385 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
2391 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
)
2393 return extract_infinite_loop(stmt
->getBody());
2396 /* Create an index expression for an access to a virtual array
2397 * representing the result of a condition.
2398 * Unlike other accessed data, the id of the array is NULL as
2399 * there is no ValueDecl in the program corresponding to the virtual
2401 * The array starts out as a scalar, but grows along with the
2402 * statement writing to the array in pet_scop_embed.
2404 static __isl_give isl_multi_pw_aff
*create_test_index(isl_ctx
*ctx
, int test_nr
)
2406 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, 0);
2410 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
2411 id
= isl_id_alloc(ctx
, name
, NULL
);
2412 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
2413 return isl_multi_pw_aff_zero(dim
);
2416 /* Add an array with the given extent (range of "index") to the list
2417 * of arrays in "scop" and return the extended pet_scop.
2418 * The array is marked as attaining values 0 and 1 only and
2419 * as each element being assigned at most once.
2421 static struct pet_scop
*scop_add_array(struct pet_scop
*scop
,
2422 __isl_keep isl_multi_pw_aff
*index
, clang::ASTContext
&ast_ctx
)
2424 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(index
);
2426 struct pet_array
*array
;
2434 array
= isl_calloc_type(ctx
, struct pet_array
);
2438 access
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index
));
2439 array
->extent
= isl_map_range(access
);
2440 dim
= isl_space_params_alloc(ctx
, 0);
2441 array
->context
= isl_set_universe(dim
);
2442 dim
= isl_space_set_alloc(ctx
, 0, 1);
2443 array
->value_bounds
= isl_set_universe(dim
);
2444 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
2446 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
2448 array
->element_type
= strdup("int");
2449 array
->element_size
= ast_ctx
.getTypeInfo(ast_ctx
.IntTy
).first
/ 8;
2450 array
->uniquely_defined
= 1;
2452 if (!array
->extent
|| !array
->context
)
2453 array
= pet_array_free(array
);
2455 scop
= pet_scop_add_array(scop
, array
);
2459 pet_scop_free(scop
);
2463 /* Construct a pet_scop for a while loop of the form
2468 * In particular, construct a scop for an infinite loop around body and
2469 * intersect the domain with the affine expression.
2470 * Note that this intersection may result in an empty loop.
2472 struct pet_scop
*PetScan::extract_affine_while(__isl_take isl_pw_aff
*pa
,
2475 struct pet_scop
*scop
;
2479 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2480 dom
= isl_pw_aff_non_zero_set(pa
);
2481 scop
= extract_infinite_loop(body
);
2482 scop
= pet_scop_restrict(scop
, dom
);
2483 scop
= pet_scop_restrict_context(scop
, valid
);
2488 /* Construct a scop for a while, given the scops for the condition
2489 * and the body, the filter identifier and the iteration domain of
2492 * In particular, the scop for the condition is filtered to depend
2493 * on "id_test" evaluating to true for all previous iterations
2494 * of the loop, while the scop for the body is filtered to depend
2495 * on "id_test" evaluating to true for all iterations up to the
2496 * current iteration.
2497 * The actual filter only imposes that this virtual array has
2498 * value one on the previous or the current iteration.
2499 * The fact that this condition also applies to the previous
2500 * iterations is enforced by an implication.
2502 * These filtered scops are then combined into a single scop.
2504 * "sign" is positive if the iterator increases and negative
2507 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
2508 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
2509 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
2511 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2513 isl_multi_pw_aff
*test_index
;
2514 isl_multi_pw_aff
*prev
;
2515 int sign
= isl_val_sgn(inc
);
2516 struct pet_scop
*scop
;
2518 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
2519 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
2521 space
= isl_space_map_from_set(isl_set_get_space(domain
));
2522 test_index
= isl_multi_pw_aff_identity(space
);
2523 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
2524 isl_id_copy(id_test
));
2525 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
2527 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
2528 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
2533 /* Check if the while loop is of the form
2535 * while (affine expression)
2538 * If so, call extract_affine_while to construct a scop.
2540 * Otherwise, construct a generic while scop, with iteration domain
2541 * { [t] : t >= 0 }. The scop consists of two parts, one for
2542 * evaluating the condition and one for the body.
2543 * The schedule is adjusted to reflect that the condition is evaluated
2544 * before the body is executed and the body is filtered to depend
2545 * on the result of the condition evaluating to true on all iterations
2546 * up to the current iteration, while the evaluation the condition itself
2547 * is filtered to depend on the result of the condition evaluating to true
2548 * on all previous iterations.
2549 * The context of the scop representing the body is dropped
2550 * because we don't know how many times the body will be executed,
2553 * If the body contains any break, then it is taken into
2554 * account in infinite_domain (if the skip condition is affine)
2555 * or in scop_add_break (if the skip condition is not affine).
2557 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
)
2560 isl_id
*id
, *id_test
, *id_break_test
;
2561 isl_multi_pw_aff
*test_index
;
2565 struct pet_scop
*scop
, *scop_body
;
2568 cond
= stmt
->getCond();
2574 clear_assignments
clear(assigned_value
);
2575 clear
.TraverseStmt(stmt
->getBody());
2577 pa
= try_extract_affine_condition(cond
);
2579 return extract_affine_while(pa
, stmt
->getBody());
2581 if (!allow_nested
) {
2586 test_index
= create_test_index(ctx
, n_test
++);
2587 scop
= extract_non_affine_condition(cond
,
2588 isl_multi_pw_aff_copy(test_index
));
2589 scop
= scop_add_array(scop
, test_index
, ast_context
);
2590 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
2591 isl_multi_pw_aff_free(test_index
);
2592 scop_body
= extract(stmt
->getBody());
2594 id
= isl_id_alloc(ctx
, "t", NULL
);
2595 domain
= infinite_domain(isl_id_copy(id
), scop_body
);
2596 ident
= identity_aff(domain
);
2598 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
2600 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
2602 scop
= pet_scop_prefix(scop
, 0);
2603 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
2604 isl_map_from_aff(isl_aff_copy(ident
)),
2605 isl_aff_copy(ident
), isl_id_copy(id
));
2606 scop_body
= pet_scop_reset_context(scop_body
);
2607 scop_body
= pet_scop_prefix(scop_body
, 1);
2608 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
2609 isl_map_from_aff(isl_aff_copy(ident
)), ident
, id
);
2611 if (has_var_break
) {
2612 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
2613 isl_set_copy(domain
), isl_val_one(ctx
));
2614 scop_body
= scop_add_break(scop_body
, id_break_test
,
2615 isl_set_copy(domain
), isl_val_one(ctx
));
2617 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
2623 /* Check whether "cond" expresses a simple loop bound
2624 * on the only set dimension.
2625 * In particular, if "up" is set then "cond" should contain only
2626 * upper bounds on the set dimension.
2627 * Otherwise, it should contain only lower bounds.
2629 static bool is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
2631 if (isl_val_is_pos(inc
))
2632 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
2634 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
2637 /* Extend a condition on a given iteration of a loop to one that
2638 * imposes the same condition on all previous iterations.
2639 * "domain" expresses the lower [upper] bound on the iterations
2640 * when inc is positive [negative].
2642 * In particular, we construct the condition (when inc is positive)
2644 * forall i' : (domain(i') and i' <= i) => cond(i')
2646 * which is equivalent to
2648 * not exists i' : domain(i') and i' <= i and not cond(i')
2650 * We construct this set by negating cond, applying a map
2652 * { [i'] -> [i] : domain(i') and i' <= i }
2654 * and then negating the result again.
2656 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
2657 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
2659 isl_map
*previous_to_this
;
2661 if (isl_val_is_pos(inc
))
2662 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
2664 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
2666 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
2668 cond
= isl_set_complement(cond
);
2669 cond
= isl_set_apply(cond
, previous_to_this
);
2670 cond
= isl_set_complement(cond
);
2677 /* Construct a domain of the form
2679 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
2681 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
2682 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
2688 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
2689 dim
= isl_pw_aff_get_domain_space(init
);
2690 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2691 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
2692 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
2694 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
2695 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
2696 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2697 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
2699 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
2701 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
2703 return isl_set_params(set
);
2706 /* Assuming "cond" represents a bound on a loop where the loop
2707 * iterator "iv" is incremented (or decremented) by one, check if wrapping
2710 * Under the given assumptions, wrapping is only possible if "cond" allows
2711 * for the last value before wrapping, i.e., 2^width - 1 in case of an
2712 * increasing iterator and 0 in case of a decreasing iterator.
2714 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
,
2715 __isl_keep isl_val
*inc
)
2722 test
= isl_set_copy(cond
);
2724 ctx
= isl_set_get_ctx(test
);
2725 if (isl_val_is_neg(inc
))
2726 limit
= isl_val_zero(ctx
);
2728 limit
= isl_val_int_from_ui(ctx
, get_type_size(iv
));
2729 limit
= isl_val_2exp(limit
);
2730 limit
= isl_val_sub_ui(limit
, 1);
2733 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
2734 cw
= !isl_set_is_empty(test
);
2740 /* Given a one-dimensional space, construct the following affine expression
2743 * { [v] -> [v mod 2^width] }
2745 * where width is the number of bits used to represent the values
2746 * of the unsigned variable "iv".
2748 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
2755 ctx
= isl_space_get_ctx(dim
);
2756 mod
= isl_val_int_from_ui(ctx
, get_type_size(iv
));
2757 mod
= isl_val_2exp(mod
);
2759 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2760 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2761 aff
= isl_aff_mod_val(aff
, mod
);
2766 /* Project out the parameter "id" from "set".
2768 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
2769 __isl_keep isl_id
*id
)
2773 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
2775 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2780 /* Compute the set of parameters for which "set1" is a subset of "set2".
2782 * set1 is a subset of set2 if
2784 * forall i in set1 : i in set2
2788 * not exists i in set1 and i not in set2
2792 * not exists i in set1 \ set2
2794 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
2795 __isl_take isl_set
*set2
)
2797 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
2800 /* Compute the set of parameter values for which "cond" holds
2801 * on the next iteration for each element of "dom".
2803 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2804 * and then compute the set of parameters for which the result is a subset
2807 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
2808 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
2814 space
= isl_set_get_space(dom
);
2815 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2816 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2817 aff
= isl_aff_add_constant_val(aff
, inc
);
2818 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2820 dom
= isl_set_apply(dom
, next
);
2822 return enforce_subset(dom
, cond
);
2825 /* Does "id" refer to a nested access?
2827 static bool is_nested_parameter(__isl_keep isl_id
*id
)
2829 return id
&& isl_id_get_user(id
) && !isl_id_get_name(id
);
2832 /* Does parameter "pos" of "space" refer to a nested access?
2834 static bool is_nested_parameter(__isl_keep isl_space
*space
, int pos
)
2839 id
= isl_space_get_dim_id(space
, isl_dim_param
, pos
);
2840 nested
= is_nested_parameter(id
);
2846 /* Does "space" involve any parameters that refer to nested
2847 * accesses, i.e., parameters with no name?
2849 static bool has_nested(__isl_keep isl_space
*space
)
2853 nparam
= isl_space_dim(space
, isl_dim_param
);
2854 for (int i
= 0; i
< nparam
; ++i
)
2855 if (is_nested_parameter(space
, i
))
2861 /* Does "pa" involve any parameters that refer to nested
2862 * accesses, i.e., parameters with no name?
2864 static bool has_nested(__isl_keep isl_pw_aff
*pa
)
2869 space
= isl_pw_aff_get_space(pa
);
2870 nested
= has_nested(space
);
2871 isl_space_free(space
);
2876 /* Construct a pet_scop for a for statement.
2877 * The for loop is required to be of the form
2879 * for (i = init; condition; ++i)
2883 * for (i = init; condition; --i)
2885 * The initialization of the for loop should either be an assignment
2886 * to an integer variable, or a declaration of such a variable with
2889 * The condition is allowed to contain nested accesses, provided
2890 * they are not being written to inside the body of the loop.
2891 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2892 * essentially treated as a while loop, with iteration domain
2893 * { [i] : i >= init }.
2895 * We extract a pet_scop for the body and then embed it in a loop with
2896 * iteration domain and schedule
2898 * { [i] : i >= init and condition' }
2903 * { [i] : i <= init and condition' }
2906 * Where condition' is equal to condition if the latter is
2907 * a simple upper [lower] bound and a condition that is extended
2908 * to apply to all previous iterations otherwise.
2910 * If the condition is non-affine, then we drop the condition from the
2911 * iteration domain and instead create a separate statement
2912 * for evaluating the condition. The body is then filtered to depend
2913 * on the result of the condition evaluating to true on all iterations
2914 * up to the current iteration, while the evaluation the condition itself
2915 * is filtered to depend on the result of the condition evaluating to true
2916 * on all previous iterations.
2917 * The context of the scop representing the body is dropped
2918 * because we don't know how many times the body will be executed,
2921 * If the stride of the loop is not 1, then "i >= init" is replaced by
2923 * (exists a: i = init + stride * a and a >= 0)
2925 * If the loop iterator i is unsigned, then wrapping may occur.
2926 * During the computation, we work with a virtual iterator that
2927 * does not wrap. However, the condition in the code applies
2928 * to the wrapped value, so we need to change condition(i)
2929 * into condition([i % 2^width]).
2930 * After computing the virtual domain and schedule, we apply
2931 * the function { [v] -> [v % 2^width] } to the domain and the domain
2932 * of the schedule. In order not to lose any information, we also
2933 * need to intersect the domain of the schedule with the virtual domain
2934 * first, since some iterations in the wrapped domain may be scheduled
2935 * several times, typically an infinite number of times.
2936 * Note that there may be no need to perform this final wrapping
2937 * if the loop condition (after wrapping) satisfies certain conditions.
2938 * However, the is_simple_bound condition is not enough since it doesn't
2939 * check if there even is an upper bound.
2941 * If the loop condition is non-affine, then we keep the virtual
2942 * iterator in the iteration domain and instead replace all accesses
2943 * to the original iterator by the wrapping of the virtual iterator.
2945 * Wrapping on unsigned iterators can be avoided entirely if
2946 * loop condition is simple, the loop iterator is incremented
2947 * [decremented] by one and the last value before wrapping cannot
2948 * possibly satisfy the loop condition.
2950 * Before extracting a pet_scop from the body we remove all
2951 * assignments in assigned_value to variables that are assigned
2952 * somewhere in the body of the loop.
2954 * Valid parameters for a for loop are those for which the initial
2955 * value itself, the increment on each domain iteration and
2956 * the condition on both the initial value and
2957 * the result of incrementing the iterator for each iteration of the domain
2959 * If the loop condition is non-affine, then we only consider validity
2960 * of the initial value.
2962 * If the body contains any break, then we keep track of it in "skip"
2963 * (if the skip condition is affine) or it is handled in scop_add_break
2964 * (if the skip condition is not affine).
2965 * Note that the affine break condition needs to be considered with
2966 * respect to previous iterations in the virtual domain (if any)
2967 * and that the domain needs to be kept virtual if there is a non-affine
2970 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
)
2972 BinaryOperator
*ass
;
2980 isl_set
*cond
= NULL
;
2981 isl_set
*skip
= NULL
;
2982 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
2983 struct pet_scop
*scop
, *scop_cond
= NULL
;
2984 assigned_value_cache
cache(assigned_value
);
2990 bool keep_virtual
= false;
2991 bool has_affine_break
;
2993 isl_aff
*wrap
= NULL
;
2994 isl_pw_aff
*pa
, *pa_inc
, *init_val
;
2995 isl_set
*valid_init
;
2996 isl_set
*valid_cond
;
2997 isl_set
*valid_cond_init
;
2998 isl_set
*valid_cond_next
;
3002 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
3003 return extract_infinite_for(stmt
);
3005 init
= stmt
->getInit();
3010 if ((ass
= initialization_assignment(init
)) != NULL
) {
3011 iv
= extract_induction_variable(ass
);
3014 lhs
= ass
->getLHS();
3015 rhs
= ass
->getRHS();
3016 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
3017 VarDecl
*var
= extract_induction_variable(init
, decl
);
3021 rhs
= var
->getInit();
3022 lhs
= create_DeclRefExpr(var
);
3024 unsupported(stmt
->getInit());
3028 pa_inc
= extract_increment(stmt
, iv
);
3033 if (isl_pw_aff_n_piece(pa_inc
) != 1 ||
3034 isl_pw_aff_foreach_piece(pa_inc
, &extract_cst
, &inc
) < 0) {
3035 isl_pw_aff_free(pa_inc
);
3036 unsupported(stmt
->getInc());
3040 valid_inc
= isl_pw_aff_domain(pa_inc
);
3042 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
3044 assigned_value
.erase(iv
);
3045 clear_assignments
clear(assigned_value
);
3046 clear
.TraverseStmt(stmt
->getBody());
3048 id
= isl_id_alloc(ctx
, iv
->getName().str().c_str(), iv
);
3050 pa
= try_extract_nested_condition(stmt
->getCond());
3051 if (allow_nested
&& (!pa
|| has_nested(pa
)))
3054 scop
= extract(stmt
->getBody());
3056 has_affine_break
= scop
&&
3057 pet_scop_has_affine_skip(scop
, pet_skip_later
);
3058 if (has_affine_break
)
3059 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
3060 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
3061 if (has_var_break
) {
3062 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
3063 keep_virtual
= true;
3066 if (pa
&& !is_nested_allowed(pa
, scop
)) {
3067 isl_pw_aff_free(pa
);
3071 if (!allow_nested
&& !pa
)
3072 pa
= try_extract_affine_condition(stmt
->getCond());
3073 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
3074 cond
= isl_pw_aff_non_zero_set(pa
);
3075 if (allow_nested
&& !cond
) {
3076 isl_multi_pw_aff
*test_index
;
3077 int save_n_stmt
= n_stmt
;
3078 test_index
= create_test_index(ctx
, n_test
++);
3080 scop_cond
= extract_non_affine_condition(stmt
->getCond(),
3081 isl_multi_pw_aff_copy(test_index
));
3082 n_stmt
= save_n_stmt
;
3083 scop_cond
= scop_add_array(scop_cond
, test_index
, ast_context
);
3084 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
3086 isl_multi_pw_aff_free(test_index
);
3087 scop_cond
= pet_scop_prefix(scop_cond
, 0);
3088 scop
= pet_scop_reset_context(scop
);
3089 scop
= pet_scop_prefix(scop
, 1);
3090 keep_virtual
= true;
3091 cond
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
3094 cond
= embed(cond
, isl_id_copy(id
));
3095 skip
= embed(skip
, isl_id_copy(id
));
3096 valid_cond
= isl_set_coalesce(valid_cond
);
3097 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
3098 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
3099 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
3100 is_virtual
= is_unsigned
&& (!is_one
|| can_wrap(cond
, iv
, inc
));
3102 init_val
= extract_affine(rhs
);
3103 valid_cond_init
= enforce_subset(
3104 isl_set_from_pw_aff(isl_pw_aff_copy(init_val
)),
3105 isl_set_copy(valid_cond
));
3106 if (is_one
&& !is_virtual
) {
3107 isl_pw_aff_free(init_val
);
3108 pa
= extract_comparison(isl_val_is_pos(inc
) ? BO_GE
: BO_LE
,
3110 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
3111 valid_init
= set_project_out_by_id(valid_init
, id
);
3112 domain
= isl_pw_aff_non_zero_set(pa
);
3114 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
3115 domain
= strided_domain(isl_id_copy(id
), init_val
,
3119 domain
= embed(domain
, isl_id_copy(id
));
3122 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
3123 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
3124 rev_wrap
= isl_map_reverse(rev_wrap
);
3125 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
3126 skip
= isl_set_apply(skip
, isl_map_copy(rev_wrap
));
3127 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
3128 valid_inc
= isl_set_apply(valid_inc
, rev_wrap
);
3130 is_simple
= is_simple_bound(cond
, inc
);
3132 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
3133 is_simple
= is_simple_bound(cond
, inc
);
3136 cond
= valid_for_each_iteration(cond
,
3137 isl_set_copy(domain
), isl_val_copy(inc
));
3138 domain
= isl_set_intersect(domain
, cond
);
3139 if (has_affine_break
) {
3140 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
3141 skip
= after(skip
, isl_val_sgn(inc
));
3142 domain
= isl_set_subtract(domain
, skip
);
3144 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
3145 space
= isl_space_from_domain(isl_set_get_space(domain
));
3146 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3147 sched
= isl_map_universe(space
);
3148 if (isl_val_is_pos(inc
))
3149 sched
= isl_map_equate(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
3151 sched
= isl_map_oppose(sched
, isl_dim_in
, 0, isl_dim_out
, 0);
3153 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
3155 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
3157 if (is_virtual
&& !keep_virtual
) {
3158 isl_map
*wrap_map
= isl_map_from_aff(wrap
);
3159 wrap_map
= isl_map_set_dim_id(wrap_map
,
3160 isl_dim_out
, 0, isl_id_copy(id
));
3161 sched
= isl_map_intersect_domain(sched
, isl_set_copy(domain
));
3162 domain
= isl_set_apply(domain
, isl_map_copy(wrap_map
));
3163 sched
= isl_map_apply_domain(sched
, wrap_map
);
3165 if (!(is_virtual
&& keep_virtual
))
3166 wrap
= identity_aff(domain
);
3168 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
3169 isl_map_copy(sched
), isl_aff_copy(wrap
), isl_id_copy(id
));
3170 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
3171 scop
= resolve_nested(scop
);
3173 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
3176 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
3178 isl_set_free(valid_inc
);
3180 scop
= pet_scop_restrict_context(scop
, valid_inc
);
3181 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
3182 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
3183 isl_set_free(domain
);
3185 clear_assignment(assigned_value
, iv
);
3189 scop
= pet_scop_restrict_context(scop
, valid_init
);
3194 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
, bool skip_declarations
)
3196 return extract(stmt
->children(), true, skip_declarations
);
3199 /* Does parameter "pos" of "map" refer to a nested access?
3201 static bool is_nested_parameter(__isl_keep isl_map
*map
, int pos
)
3206 id
= isl_map_get_dim_id(map
, isl_dim_param
, pos
);
3207 nested
= is_nested_parameter(id
);
3213 /* How many parameters of "space" refer to nested accesses, i.e., have no name?
3215 static int n_nested_parameter(__isl_keep isl_space
*space
)
3220 nparam
= isl_space_dim(space
, isl_dim_param
);
3221 for (int i
= 0; i
< nparam
; ++i
)
3222 if (is_nested_parameter(space
, i
))
3228 /* How many parameters of "map" refer to nested accesses, i.e., have no name?
3230 static int n_nested_parameter(__isl_keep isl_map
*map
)
3235 space
= isl_map_get_space(map
);
3236 n
= n_nested_parameter(space
);
3237 isl_space_free(space
);
3242 /* For each nested access parameter in "space",
3243 * construct a corresponding pet_expr, place it in args and
3244 * record its position in "param2pos".
3245 * "n_arg" is the number of elements that are already in args.
3246 * The position recorded in "param2pos" takes this number into account.
3247 * If the pet_expr corresponding to a parameter is identical to
3248 * the pet_expr corresponding to an earlier parameter, then these two
3249 * parameters are made to refer to the same element in args.
3251 * Return the final number of elements in args or -1 if an error has occurred.
3253 int PetScan::extract_nested(__isl_keep isl_space
*space
,
3254 int n_arg
, struct pet_expr
**args
, std::map
<int,int> ¶m2pos
)
3258 nparam
= isl_space_dim(space
, isl_dim_param
);
3259 for (int i
= 0; i
< nparam
; ++i
) {
3261 isl_id
*id
= isl_space_get_dim_id(space
, isl_dim_param
, i
);
3264 if (!is_nested_parameter(id
)) {
3269 nested
= (Expr
*) isl_id_get_user(id
);
3270 args
[n_arg
] = extract_expr(nested
);
3274 for (j
= 0; j
< n_arg
; ++j
)
3275 if (pet_expr_is_equal(args
[j
], args
[n_arg
]))
3279 pet_expr_free(args
[n_arg
]);
3283 param2pos
[i
] = n_arg
++;
3291 /* For each nested access parameter in the access relations in "expr",
3292 * construct a corresponding pet_expr, place it in expr->args and
3293 * record its position in "param2pos".
3294 * n is the number of nested access parameters.
3296 struct pet_expr
*PetScan::extract_nested(struct pet_expr
*expr
, int n
,
3297 std::map
<int,int> ¶m2pos
)
3301 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n
);
3306 space
= isl_map_get_space(expr
->acc
.access
);
3307 n
= extract_nested(space
, 0, expr
->args
, param2pos
);
3308 isl_space_free(space
);
3316 pet_expr_free(expr
);
3320 /* Look for parameters in any access relation in "expr" that
3321 * refer to nested accesses. In particular, these are
3322 * parameters with no name.
3324 * If there are any such parameters, then the domain of the index
3325 * expression and the access relation, which is still [] at this point,
3326 * is replaced by [[] -> [t_1,...,t_n]], with n the number of these parameters
3327 * (after identifying identical nested accesses).
3329 * This transformation is performed in several steps.
3330 * We first extract the arguments in extract_nested.
3331 * param2pos maps the original parameter position to the position
3333 * Then we move these parameters to input dimension.
3334 * t2pos maps the positions of these temporary input dimensions
3335 * to the positions of the corresponding arguments.
3336 * Finally, we express there temporary dimensions in term of the domain
3337 * [[] -> [t_1,...,t_n]] and precompose index expression and access
3338 * relations with this function.
3340 struct pet_expr
*PetScan::resolve_nested(struct pet_expr
*expr
)
3345 isl_local_space
*ls
;
3348 std::map
<int,int> param2pos
;
3349 std::map
<int,int> t2pos
;
3354 for (int i
= 0; i
< expr
->n_arg
; ++i
) {
3355 expr
->args
[i
] = resolve_nested(expr
->args
[i
]);
3356 if (!expr
->args
[i
]) {
3357 pet_expr_free(expr
);
3362 if (expr
->type
!= pet_expr_access
)
3365 n
= n_nested_parameter(expr
->acc
.access
);
3369 expr
= extract_nested(expr
, n
, param2pos
);
3373 expr
= pet_expr_access_align_params(expr
);
3376 nparam
= isl_map_dim(expr
->acc
.access
, isl_dim_param
);
3379 for (int i
= nparam
- 1; i
>= 0; --i
) {
3380 isl_id
*id
= isl_map_get_dim_id(expr
->acc
.access
,
3382 if (!is_nested_parameter(id
)) {
3387 expr
->acc
.access
= isl_map_move_dims(expr
->acc
.access
,
3388 isl_dim_in
, n
, isl_dim_param
, i
, 1);
3389 expr
->acc
.index
= isl_multi_pw_aff_move_dims(expr
->acc
.index
,
3390 isl_dim_in
, n
, isl_dim_param
, i
, 1);
3391 t2pos
[n
] = param2pos
[i
];
3397 space
= isl_multi_pw_aff_get_space(expr
->acc
.index
);
3398 space
= isl_space_set_from_params(isl_space_params(space
));
3399 space
= isl_space_add_dims(space
, isl_dim_set
, expr
->n_arg
);
3400 space
= isl_space_wrap(isl_space_from_range(space
));
3401 ls
= isl_local_space_from_space(isl_space_copy(space
));
3402 space
= isl_space_from_domain(space
);
3403 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
3404 ma
= isl_multi_aff_zero(space
);
3406 for (int i
= 0; i
< n
; ++i
) {
3407 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3408 isl_dim_set
, t2pos
[i
]);
3409 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3411 isl_local_space_free(ls
);
3413 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(expr
->acc
.access
,
3414 isl_multi_aff_copy(ma
));
3415 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
3421 /* Return the file offset of the expansion location of "Loc".
3423 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
3425 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
3428 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
3430 /* Return a SourceLocation for the location after the first semicolon
3431 * after "loc". If Lexer::findLocationAfterToken is available, we simply
3432 * call it and also skip trailing spaces and newline.
3434 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
3435 const LangOptions
&LO
)
3437 return Lexer::findLocationAfterToken(loc
, tok::semi
, SM
, LO
, true);
3442 /* Return a SourceLocation for the location after the first semicolon
3443 * after "loc". If Lexer::findLocationAfterToken is not available,
3444 * we look in the underlying character data for the first semicolon.
3446 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
3447 const LangOptions
&LO
)
3450 const char *s
= SM
.getCharacterData(loc
);
3452 semi
= strchr(s
, ';');
3454 return SourceLocation();
3455 return loc
.getFileLocWithOffset(semi
+ 1 - s
);
3460 /* If the token at "loc" is the first token on the line, then return
3461 * a location referring to the start of the line.
3462 * Otherwise, return "loc".
3464 * This function is used to extend a scop to the start of the line
3465 * if the first token of the scop is also the first token on the line.
3467 * We look for the first token on the line. If its location is equal to "loc",
3468 * then the latter is the location of the first token on the line.
3470 static SourceLocation
move_to_start_of_line_if_first_token(SourceLocation loc
,
3471 SourceManager
&SM
, const LangOptions
&LO
)
3473 std::pair
<FileID
, unsigned> file_offset_pair
;
3474 llvm::StringRef file
;
3477 SourceLocation token_loc
, line_loc
;
3480 loc
= SM
.getExpansionLoc(loc
);
3481 col
= SM
.getExpansionColumnNumber(loc
);
3482 line_loc
= loc
.getLocWithOffset(1 - col
);
3483 file_offset_pair
= SM
.getDecomposedLoc(line_loc
);
3484 file
= SM
.getBufferData(file_offset_pair
.first
, NULL
);
3485 pos
= file
.data() + file_offset_pair
.second
;
3487 Lexer
lexer(SM
.getLocForStartOfFile(file_offset_pair
.first
), LO
,
3488 file
.begin(), pos
, file
.end());
3489 lexer
.LexFromRawLexer(tok
);
3490 token_loc
= tok
.getLocation();
3492 if (token_loc
== loc
)
3498 /* Convert a top-level pet_expr to a pet_scop with one statement.
3499 * This mainly involves resolving nested expression parameters
3500 * and setting the name of the iteration space.
3501 * The name is given by "label" if it is non-NULL. Otherwise,
3502 * it is of the form S_<n_stmt>.
3503 * start and end of the pet_scop are derived from those of "stmt".
3505 struct pet_scop
*PetScan::extract(Stmt
*stmt
, struct pet_expr
*expr
,
3506 __isl_take isl_id
*label
)
3508 struct pet_stmt
*ps
;
3509 struct pet_scop
*scop
;
3510 SourceLocation loc
= stmt
->getLocStart();
3511 SourceManager
&SM
= PP
.getSourceManager();
3512 const LangOptions
&LO
= PP
.getLangOpts();
3513 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3514 unsigned start
, end
;
3516 expr
= resolve_nested(expr
);
3517 ps
= pet_stmt_from_pet_expr(ctx
, line
, label
, n_stmt
++, expr
);
3518 scop
= pet_scop_from_pet_stmt(ctx
, ps
);
3520 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
);
3521 start
= getExpansionOffset(SM
, loc
);
3522 loc
= stmt
->getLocEnd();
3523 loc
= location_after_semi(loc
, SM
, LO
);
3524 end
= getExpansionOffset(SM
, loc
);
3526 scop
= pet_scop_update_start_end(scop
, start
, end
);
3530 /* Check if we can extract an affine expression from "expr".
3531 * Return the expressions as an isl_pw_aff if we can and NULL otherwise.
3532 * We turn on autodetection so that we won't generate any warnings
3533 * and turn off nesting, so that we won't accept any non-affine constructs.
3535 __isl_give isl_pw_aff
*PetScan::try_extract_affine(Expr
*expr
)
3538 int save_autodetect
= options
->autodetect
;
3539 bool save_nesting
= nesting_enabled
;
3541 options
->autodetect
= 1;
3542 nesting_enabled
= false;
3544 pwaff
= extract_affine(expr
);
3546 options
->autodetect
= save_autodetect
;
3547 nesting_enabled
= save_nesting
;
3552 /* Check whether "expr" is an affine expression.
3554 bool PetScan::is_affine(Expr
*expr
)
3558 pwaff
= try_extract_affine(expr
);
3559 isl_pw_aff_free(pwaff
);
3561 return pwaff
!= NULL
;
3564 /* Check if we can extract an affine constraint from "expr".
3565 * Return the constraint as an isl_set if we can and NULL otherwise.
3566 * We turn on autodetection so that we won't generate any warnings
3567 * and turn off nesting, so that we won't accept any non-affine constructs.
3569 __isl_give isl_pw_aff
*PetScan::try_extract_affine_condition(Expr
*expr
)
3572 int save_autodetect
= options
->autodetect
;
3573 bool save_nesting
= nesting_enabled
;
3575 options
->autodetect
= 1;
3576 nesting_enabled
= false;
3578 cond
= extract_condition(expr
);
3580 options
->autodetect
= save_autodetect
;
3581 nesting_enabled
= save_nesting
;
3586 /* Check whether "expr" is an affine constraint.
3588 bool PetScan::is_affine_condition(Expr
*expr
)
3592 cond
= try_extract_affine_condition(expr
);
3593 isl_pw_aff_free(cond
);
3595 return cond
!= NULL
;
3598 /* Check if we can extract a condition from "expr".
3599 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
3600 * If allow_nested is set, then the condition may involve parameters
3601 * corresponding to nested accesses.
3602 * We turn on autodetection so that we won't generate any warnings.
3604 __isl_give isl_pw_aff
*PetScan::try_extract_nested_condition(Expr
*expr
)
3607 int save_autodetect
= options
->autodetect
;
3608 bool save_nesting
= nesting_enabled
;
3610 options
->autodetect
= 1;
3611 nesting_enabled
= allow_nested
;
3612 cond
= extract_condition(expr
);
3614 options
->autodetect
= save_autodetect
;
3615 nesting_enabled
= save_nesting
;
3620 /* If the top-level expression of "stmt" is an assignment, then
3621 * return that assignment as a BinaryOperator.
3622 * Otherwise return NULL.
3624 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
3626 BinaryOperator
*ass
;
3630 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
3633 ass
= cast
<BinaryOperator
>(stmt
);
3634 if(ass
->getOpcode() != BO_Assign
)
3640 /* Check if the given if statement is a conditional assignement
3641 * with a non-affine condition. If so, construct a pet_scop
3642 * corresponding to this conditional assignment. Otherwise return NULL.
3644 * In particular we check if "stmt" is of the form
3651 * where a is some array or scalar access.
3652 * The constructed pet_scop then corresponds to the expression
3654 * a = condition ? f(...) : g(...)
3656 * All access relations in f(...) are intersected with condition
3657 * while all access relation in g(...) are intersected with the complement.
3659 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
)
3661 BinaryOperator
*ass_then
, *ass_else
;
3662 isl_multi_pw_aff
*write_then
, *write_else
;
3663 isl_set
*cond
, *comp
;
3664 isl_multi_pw_aff
*index
;
3667 struct pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
, *pe_write
;
3668 bool save_nesting
= nesting_enabled
;
3670 if (!options
->detect_conditional_assignment
)
3673 ass_then
= top_assignment_or_null(stmt
->getThen());
3674 ass_else
= top_assignment_or_null(stmt
->getElse());
3676 if (!ass_then
|| !ass_else
)
3679 if (is_affine_condition(stmt
->getCond()))
3682 write_then
= extract_index(ass_then
->getLHS());
3683 write_else
= extract_index(ass_else
->getLHS());
3685 equal
= isl_multi_pw_aff_plain_is_equal(write_then
, write_else
);
3686 isl_multi_pw_aff_free(write_else
);
3687 if (equal
< 0 || !equal
) {
3688 isl_multi_pw_aff_free(write_then
);
3692 nesting_enabled
= allow_nested
;
3693 pa
= extract_condition(stmt
->getCond());
3694 nesting_enabled
= save_nesting
;
3695 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
3696 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
3697 index
= isl_multi_pw_aff_from_range(isl_multi_pw_aff_from_pw_aff(pa
));
3699 pe_cond
= pet_expr_from_index(index
);
3701 pe_then
= extract_expr(ass_then
->getRHS());
3702 pe_then
= pet_expr_restrict(pe_then
, cond
);
3703 pe_else
= extract_expr(ass_else
->getRHS());
3704 pe_else
= pet_expr_restrict(pe_else
, comp
);
3706 pe
= pet_expr_new_ternary(ctx
, pe_cond
, pe_then
, pe_else
);
3707 pe_write
= pet_expr_from_index_and_depth(write_then
,
3708 extract_depth(write_then
));
3710 pe_write
->acc
.write
= 1;
3711 pe_write
->acc
.read
= 0;
3713 pe
= pet_expr_new_binary(ctx
, pet_op_assign
, pe_write
, pe
);
3714 return extract(stmt
, pe
);
3717 /* Create a pet_scop with a single statement evaluating "cond"
3718 * and writing the result to a virtual scalar, as expressed by
3721 struct pet_scop
*PetScan::extract_non_affine_condition(Expr
*cond
,
3722 __isl_take isl_multi_pw_aff
*index
)
3724 struct pet_expr
*expr
, *write
;
3725 struct pet_stmt
*ps
;
3726 struct pet_scop
*scop
;
3727 SourceLocation loc
= cond
->getLocStart();
3728 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
3730 write
= pet_expr_from_index(index
);
3732 write
->acc
.write
= 1;
3733 write
->acc
.read
= 0;
3735 expr
= extract_expr(cond
);
3736 expr
= resolve_nested(expr
);
3737 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, write
, expr
);
3738 ps
= pet_stmt_from_pet_expr(ctx
, line
, NULL
, n_stmt
++, expr
);
3739 scop
= pet_scop_from_pet_stmt(ctx
, ps
);
3740 scop
= resolve_nested(scop
);
3746 static struct pet_expr
*embed_access(struct pet_expr
*expr
, void *user
);
3749 /* Precompose the access relation and the index expression associated
3750 * to "expr" with the function pointed to by "user",
3751 * thereby embedding the access relation in the domain of this function.
3752 * The initial domain of the access relation and the index expression
3753 * is the zero-dimensional domain.
3755 static struct pet_expr
*embed_access(struct pet_expr
*expr
, void *user
)
3757 isl_multi_aff
*ma
= (isl_multi_aff
*) user
;
3759 expr
->acc
.access
= isl_map_preimage_domain_multi_aff(expr
->acc
.access
,
3760 isl_multi_aff_copy(ma
));
3761 expr
->acc
.index
= isl_multi_pw_aff_pullback_multi_aff(expr
->acc
.index
,
3762 isl_multi_aff_copy(ma
));
3763 if (!expr
->acc
.access
|| !expr
->acc
.index
)
3768 pet_expr_free(expr
);
3772 /* Precompose all access relations in "expr" with "ma", thereby
3773 * embedding them in the domain of "ma".
3775 static struct pet_expr
*embed(struct pet_expr
*expr
,
3776 __isl_keep isl_multi_aff
*ma
)
3778 return pet_expr_map_access(expr
, &embed_access
, ma
);
3781 /* How many parameters of "set" refer to nested accesses, i.e., have no name?
3783 static int n_nested_parameter(__isl_keep isl_set
*set
)
3788 space
= isl_set_get_space(set
);
3789 n
= n_nested_parameter(space
);
3790 isl_space_free(space
);
3795 /* Remove all parameters from "map" that refer to nested accesses.
3797 static __isl_give isl_map
*remove_nested_parameters(__isl_take isl_map
*map
)
3802 space
= isl_map_get_space(map
);
3803 nparam
= isl_space_dim(space
, isl_dim_param
);
3804 for (int i
= nparam
- 1; i
>= 0; --i
)
3805 if (is_nested_parameter(space
, i
))
3806 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
3807 isl_space_free(space
);
3812 /* Remove all parameters from "mpa" that refer to nested accesses.
3814 static __isl_give isl_multi_pw_aff
*remove_nested_parameters(
3815 __isl_take isl_multi_pw_aff
*mpa
)
3820 space
= isl_multi_pw_aff_get_space(mpa
);
3821 nparam
= isl_space_dim(space
, isl_dim_param
);
3822 for (int i
= nparam
- 1; i
>= 0; --i
) {
3823 if (!is_nested_parameter(space
, i
))
3825 mpa
= isl_multi_pw_aff_drop_dims(mpa
, isl_dim_param
, i
, 1);
3827 isl_space_free(space
);
3832 /* Remove all parameters from the index expression and access relation of "expr"
3833 * that refer to nested accesses.
3835 static struct pet_expr
*remove_nested_parameters(struct pet_expr
*expr
)
3837 expr
->acc
.access
= remove_nested_parameters(expr
->acc
.access
);
3838 expr
->acc
.index
= remove_nested_parameters(expr
->acc
.index
);
3839 if (!expr
->acc
.access
|| !expr
->acc
.index
)
3844 pet_expr_free(expr
);
3849 static struct pet_expr
*expr_remove_nested_parameters(
3850 struct pet_expr
*expr
, void *user
);
3853 static struct pet_expr
*expr_remove_nested_parameters(
3854 struct pet_expr
*expr
, void *user
)
3856 return remove_nested_parameters(expr
);
3859 /* Remove all nested access parameters from the schedule and all
3860 * accesses of "stmt".
3861 * There is no need to remove them from the domain as these parameters
3862 * have already been removed from the domain when this function is called.
3864 static struct pet_stmt
*remove_nested_parameters(struct pet_stmt
*stmt
)
3868 stmt
->schedule
= remove_nested_parameters(stmt
->schedule
);
3869 stmt
->body
= pet_expr_map_access(stmt
->body
,
3870 &expr_remove_nested_parameters
, NULL
);
3871 if (!stmt
->schedule
|| !stmt
->body
)
3873 for (int i
= 0; i
< stmt
->n_arg
; ++i
) {
3874 stmt
->args
[i
] = pet_expr_map_access(stmt
->args
[i
],
3875 &expr_remove_nested_parameters
, NULL
);
3882 pet_stmt_free(stmt
);
3886 /* For each nested access parameter in the domain of "stmt",
3887 * construct a corresponding pet_expr, place it before the original
3888 * elements in stmt->args and record its position in "param2pos".
3889 * n is the number of nested access parameters.
3891 struct pet_stmt
*PetScan::extract_nested(struct pet_stmt
*stmt
, int n
,
3892 std::map
<int,int> ¶m2pos
)
3897 struct pet_expr
**args
;
3899 n_arg
= stmt
->n_arg
;
3900 args
= isl_calloc_array(ctx
, struct pet_expr
*, n
+ n_arg
);
3904 space
= isl_set_get_space(stmt
->domain
);
3905 n_arg
= extract_nested(space
, 0, args
, param2pos
);
3906 isl_space_free(space
);
3911 for (i
= 0; i
< stmt
->n_arg
; ++i
)
3912 args
[n_arg
+ i
] = stmt
->args
[i
];
3915 stmt
->n_arg
+= n_arg
;
3920 for (i
= 0; i
< n
; ++i
)
3921 pet_expr_free(args
[i
]);
3924 pet_stmt_free(stmt
);
3928 /* Check whether any of the arguments i of "stmt" starting at position "n"
3929 * is equal to one of the first "n" arguments j.
3930 * If so, combine the constraints on arguments i and j and remove
3933 static struct pet_stmt
*remove_duplicate_arguments(struct pet_stmt
*stmt
, int n
)
3942 if (n
== stmt
->n_arg
)
3945 map
= isl_set_unwrap(stmt
->domain
);
3947 for (i
= stmt
->n_arg
- 1; i
>= n
; --i
) {
3948 for (j
= 0; j
< n
; ++j
)
3949 if (pet_expr_is_equal(stmt
->args
[i
], stmt
->args
[j
]))
3954 map
= isl_map_equate(map
, isl_dim_out
, i
, isl_dim_out
, j
);
3955 map
= isl_map_project_out(map
, isl_dim_out
, i
, 1);
3957 pet_expr_free(stmt
->args
[i
]);
3958 for (j
= i
; j
+ 1 < stmt
->n_arg
; ++j
)
3959 stmt
->args
[j
] = stmt
->args
[j
+ 1];
3963 stmt
->domain
= isl_map_wrap(map
);
3968 pet_stmt_free(stmt
);
3972 /* Look for parameters in the iteration domain of "stmt" that
3973 * refer to nested accesses. In particular, these are
3974 * parameters with no name.
3976 * If there are any such parameters, then as many extra variables
3977 * (after identifying identical nested accesses) are inserted in the
3978 * range of the map wrapped inside the domain, before the original variables.
3979 * If the original domain is not a wrapped map, then a new wrapped
3980 * map is created with zero output dimensions.
3981 * The parameters are then equated to the corresponding output dimensions
3982 * and subsequently projected out, from the iteration domain,
3983 * the schedule and the access relations.
3984 * For each of the output dimensions, a corresponding argument
3985 * expression is inserted. Initially they are created with
3986 * a zero-dimensional domain, so they have to be embedded
3987 * in the current iteration domain.
3988 * param2pos maps the position of the parameter to the position
3989 * of the corresponding output dimension in the wrapped map.
3991 struct pet_stmt
*PetScan::resolve_nested(struct pet_stmt
*stmt
)
3999 std::map
<int,int> param2pos
;
4004 n
= n_nested_parameter(stmt
->domain
);
4008 n_arg
= stmt
->n_arg
;
4009 stmt
= extract_nested(stmt
, n
, param2pos
);
4013 n
= stmt
->n_arg
- n_arg
;
4014 nparam
= isl_set_dim(stmt
->domain
, isl_dim_param
);
4015 if (isl_set_is_wrapping(stmt
->domain
))
4016 map
= isl_set_unwrap(stmt
->domain
);
4018 map
= isl_map_from_domain(stmt
->domain
);
4019 map
= isl_map_insert_dims(map
, isl_dim_out
, 0, n
);
4021 for (int i
= nparam
- 1; i
>= 0; --i
) {
4024 if (!is_nested_parameter(map
, i
))
4027 id
= pet_expr_access_get_id(stmt
->args
[param2pos
[i
]]);
4028 map
= isl_map_set_dim_id(map
, isl_dim_out
, param2pos
[i
], id
);
4029 map
= isl_map_equate(map
, isl_dim_param
, i
, isl_dim_out
,
4031 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
4034 stmt
->domain
= isl_map_wrap(map
);
4036 space
= isl_space_unwrap(isl_set_get_space(stmt
->domain
));
4037 space
= isl_space_from_domain(isl_space_domain(space
));
4038 ma
= isl_multi_aff_zero(space
);
4039 for (int pos
= 0; pos
< n
; ++pos
)
4040 stmt
->args
[pos
] = embed(stmt
->args
[pos
], ma
);
4041 isl_multi_aff_free(ma
);
4043 stmt
= remove_nested_parameters(stmt
);
4044 stmt
= remove_duplicate_arguments(stmt
, n
);
4049 /* For each statement in "scop", move the parameters that correspond
4050 * to nested access into the ranges of the domains and create
4051 * corresponding argument expressions.
4053 struct pet_scop
*PetScan::resolve_nested(struct pet_scop
*scop
)
4058 for (int i
= 0; i
< scop
->n_stmt
; ++i
) {
4059 scop
->stmts
[i
] = resolve_nested(scop
->stmts
[i
]);
4060 if (!scop
->stmts
[i
])
4066 pet_scop_free(scop
);
4070 /* Given an access expression "expr", is the variable accessed by
4071 * "expr" assigned anywhere inside "scop"?
4073 static bool is_assigned(pet_expr
*expr
, pet_scop
*scop
)
4075 bool assigned
= false;
4078 id
= pet_expr_access_get_id(expr
);
4079 assigned
= pet_scop_writes(scop
, id
);
4085 /* Are all nested access parameters in "pa" allowed given "scop".
4086 * In particular, is none of them written by anywhere inside "scop".
4088 * If "scop" has any skip conditions, then no nested access parameters
4089 * are allowed. In particular, if there is any nested access in a guard
4090 * for a piece of code containing a "continue", then we want to introduce
4091 * a separate statement for evaluating this guard so that we can express
4092 * that the result is false for all previous iterations.
4094 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff
*pa
, pet_scop
*scop
)
4101 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
4102 for (int i
= 0; i
< nparam
; ++i
) {
4104 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
4108 if (!is_nested_parameter(id
)) {
4113 if (pet_scop_has_skip(scop
, pet_skip_now
)) {
4118 nested
= (Expr
*) isl_id_get_user(id
);
4119 expr
= extract_expr(nested
);
4120 allowed
= expr
&& expr
->type
== pet_expr_access
&&
4121 !is_assigned(expr
, scop
);
4123 pet_expr_free(expr
);
4133 /* Do we need to construct a skip condition of the given type
4134 * on an if statement, given that the if condition is non-affine?
4136 * pet_scop_filter_skip can only handle the case where the if condition
4137 * holds (the then branch) and the skip condition is universal.
4138 * In any other case, we need to construct a new skip condition.
4140 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
4141 bool have_else
, enum pet_skip type
)
4143 if (have_else
&& scop_else
&& pet_scop_has_skip(scop_else
, type
))
4145 if (scop_then
&& pet_scop_has_skip(scop_then
, type
) &&
4146 !pet_scop_has_universal_skip(scop_then
, type
))
4151 /* Do we need to construct a skip condition of the given type
4152 * on an if statement, given that the if condition is affine?
4154 * There is no need to construct a new skip condition if all
4155 * the skip conditions are affine.
4157 static bool need_skip_aff(struct pet_scop
*scop_then
,
4158 struct pet_scop
*scop_else
, bool have_else
, enum pet_skip type
)
4160 if (scop_then
&& pet_scop_has_var_skip(scop_then
, type
))
4162 if (have_else
&& scop_else
&& pet_scop_has_var_skip(scop_else
, type
))
4167 /* Do we need to construct a skip condition of the given type
4168 * on an if statement?
4170 static bool need_skip(struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
4171 bool have_else
, enum pet_skip type
, bool affine
)
4174 return need_skip_aff(scop_then
, scop_else
, have_else
, type
);
4176 return need_skip(scop_then
, scop_else
, have_else
, type
);
4179 /* Construct an affine expression pet_expr that evaluates
4180 * to the constant "val".
4182 static struct pet_expr
*universally(isl_ctx
*ctx
, int val
)
4184 isl_local_space
*ls
;
4186 isl_multi_pw_aff
*mpa
;
4188 ls
= isl_local_space_from_space(isl_space_set_alloc(ctx
, 0, 0));
4189 aff
= isl_aff_val_on_domain(ls
, isl_val_int_from_si(ctx
, val
));
4190 mpa
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
4192 return pet_expr_from_index(mpa
);
4195 /* Construct an affine expression pet_expr that evaluates
4196 * to the constant 1.
4198 static struct pet_expr
*universally_true(isl_ctx
*ctx
)
4200 return universally(ctx
, 1);
4203 /* Construct an affine expression pet_expr that evaluates
4204 * to the constant 0.
4206 static struct pet_expr
*universally_false(isl_ctx
*ctx
)
4208 return universally(ctx
, 0);
4211 /* Given an index expression "test_index" for the if condition,
4212 * an index expression "skip_index" for the skip condition and
4213 * scops for the then and else branches, construct a scop for
4214 * computing "skip_index".
4216 * The computed scop contains a single statement that essentially does
4218 * skip_index = test_cond ? skip_cond_then : skip_cond_else
4220 * If the skip conditions of the then and/or else branch are not affine,
4221 * then they need to be filtered by test_index.
4222 * If they are missing, then this means the skip condition is false.
4224 * Since we are constructing a skip condition for the if statement,
4225 * the skip conditions on the then and else branches are removed.
4227 static struct pet_scop
*extract_skip(PetScan
*scan
,
4228 __isl_take isl_multi_pw_aff
*test_index
,
4229 __isl_take isl_multi_pw_aff
*skip_index
,
4230 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
, bool have_else
,
4233 struct pet_expr
*expr_then
, *expr_else
, *expr
, *expr_skip
;
4234 struct pet_stmt
*stmt
;
4235 struct pet_scop
*scop
;
4236 isl_ctx
*ctx
= scan
->ctx
;
4240 if (have_else
&& !scop_else
)
4243 if (pet_scop_has_skip(scop_then
, type
)) {
4244 expr_then
= pet_scop_get_skip_expr(scop_then
, type
);
4245 pet_scop_reset_skip(scop_then
, type
);
4246 if (!pet_expr_is_affine(expr_then
))
4247 expr_then
= pet_expr_filter(expr_then
,
4248 isl_multi_pw_aff_copy(test_index
), 1);
4250 expr_then
= universally_false(ctx
);
4252 if (have_else
&& pet_scop_has_skip(scop_else
, type
)) {
4253 expr_else
= pet_scop_get_skip_expr(scop_else
, type
);
4254 pet_scop_reset_skip(scop_else
, type
);
4255 if (!pet_expr_is_affine(expr_else
))
4256 expr_else
= pet_expr_filter(expr_else
,
4257 isl_multi_pw_aff_copy(test_index
), 0);
4259 expr_else
= universally_false(ctx
);
4261 expr
= pet_expr_from_index(test_index
);
4262 expr
= pet_expr_new_ternary(ctx
, expr
, expr_then
, expr_else
);
4263 expr_skip
= pet_expr_from_index(isl_multi_pw_aff_copy(skip_index
));
4265 expr_skip
->acc
.write
= 1;
4266 expr_skip
->acc
.read
= 0;
4268 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
4269 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, scan
->n_stmt
++, expr
);
4271 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
4272 scop
= scop_add_array(scop
, skip_index
, scan
->ast_context
);
4273 isl_multi_pw_aff_free(skip_index
);
4277 isl_multi_pw_aff_free(test_index
);
4278 isl_multi_pw_aff_free(skip_index
);
4282 /* Is scop's skip_now condition equal to its skip_later condition?
4283 * In particular, this means that it either has no skip_now condition
4284 * or both a skip_now and a skip_later condition (that are equal to each other).
4286 static bool skip_equals_skip_later(struct pet_scop
*scop
)
4288 int has_skip_now
, has_skip_later
;
4290 isl_multi_pw_aff
*skip_now
, *skip_later
;
4294 has_skip_now
= pet_scop_has_skip(scop
, pet_skip_now
);
4295 has_skip_later
= pet_scop_has_skip(scop
, pet_skip_later
);
4296 if (has_skip_now
!= has_skip_later
)
4301 skip_now
= pet_scop_get_skip(scop
, pet_skip_now
);
4302 skip_later
= pet_scop_get_skip(scop
, pet_skip_later
);
4303 equal
= isl_multi_pw_aff_is_equal(skip_now
, skip_later
);
4304 isl_multi_pw_aff_free(skip_now
);
4305 isl_multi_pw_aff_free(skip_later
);
4310 /* Drop the skip conditions of type pet_skip_later from scop1 and scop2.
4312 static void drop_skip_later(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
4314 pet_scop_reset_skip(scop1
, pet_skip_later
);
4315 pet_scop_reset_skip(scop2
, pet_skip_later
);
4318 /* Structure that handles the construction of skip conditions.
4320 * scop_then and scop_else represent the then and else branches
4321 * of the if statement
4323 * skip[type] is true if we need to construct a skip condition of that type
4324 * equal is set if the skip conditions of types pet_skip_now and pet_skip_later
4325 * are equal to each other
4326 * index[type] is an index expression from a zero-dimension domain
4327 * to the virtual array representing the skip condition
4328 * scop[type] is a scop for computing the skip condition
4330 struct pet_skip_info
{
4335 isl_multi_pw_aff
*index
[2];
4336 struct pet_scop
*scop
[2];
4338 pet_skip_info(isl_ctx
*ctx
) : ctx(ctx
) {}
4340 operator bool() { return skip
[pet_skip_now
] || skip
[pet_skip_later
]; }
4343 /* Structure that handles the construction of skip conditions on if statements.
4345 * scop_then and scop_else represent the then and else branches
4346 * of the if statement
4348 struct pet_skip_info_if
: public pet_skip_info
{
4349 struct pet_scop
*scop_then
, *scop_else
;
4352 pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
4353 struct pet_scop
*scop_else
, bool have_else
, bool affine
);
4354 void extract(PetScan
*scan
, __isl_keep isl_multi_pw_aff
*index
,
4355 enum pet_skip type
);
4356 void extract(PetScan
*scan
, __isl_keep isl_multi_pw_aff
*index
);
4357 void extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
);
4358 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
4360 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
4363 /* Initialize a pet_skip_info_if structure based on the then and else branches
4364 * and based on whether the if condition is affine or not.
4366 pet_skip_info_if::pet_skip_info_if(isl_ctx
*ctx
, struct pet_scop
*scop_then
,
4367 struct pet_scop
*scop_else
, bool have_else
, bool affine
) :
4368 pet_skip_info(ctx
), scop_then(scop_then
), scop_else(scop_else
),
4369 have_else(have_else
)
4371 skip
[pet_skip_now
] =
4372 need_skip(scop_then
, scop_else
, have_else
, pet_skip_now
, affine
);
4373 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop_then
) &&
4374 (!have_else
|| skip_equals_skip_later(scop_else
));
4375 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
4376 need_skip(scop_then
, scop_else
, have_else
, pet_skip_later
, affine
);
4379 /* If we need to construct a skip condition of the given type,
4382 * "mpa" represents the if condition.
4384 void pet_skip_info_if::extract(PetScan
*scan
,
4385 __isl_keep isl_multi_pw_aff
*mpa
, enum pet_skip type
)
4392 ctx
= isl_multi_pw_aff_get_ctx(mpa
);
4393 index
[type
] = create_test_index(ctx
, scan
->n_test
++);
4394 scop
[type
] = extract_skip(scan
, isl_multi_pw_aff_copy(mpa
),
4395 isl_multi_pw_aff_copy(index
[type
]),
4396 scop_then
, scop_else
, have_else
, type
);
4399 /* Construct the required skip conditions, given the if condition "index".
4401 void pet_skip_info_if::extract(PetScan
*scan
,
4402 __isl_keep isl_multi_pw_aff
*index
)
4404 extract(scan
, index
, pet_skip_now
);
4405 extract(scan
, index
, pet_skip_later
);
4407 drop_skip_later(scop_then
, scop_else
);
4410 /* Construct the required skip conditions, given the if condition "cond".
4412 void pet_skip_info_if::extract(PetScan
*scan
, __isl_keep isl_pw_aff
*cond
)
4414 isl_multi_pw_aff
*test
;
4416 if (!skip
[pet_skip_now
] && !skip
[pet_skip_later
])
4419 test
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_copy(cond
));
4420 test
= isl_multi_pw_aff_from_range(test
);
4421 extract(scan
, test
);
4422 isl_multi_pw_aff_free(test
);
4425 /* Add the computed skip condition of the give type to "main" and
4426 * add the scop for computing the condition at the given offset.
4428 * If equal is set, then we only computed a skip condition for pet_skip_now,
4429 * but we also need to set it as main's pet_skip_later.
4431 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*main
,
4432 enum pet_skip type
, int offset
)
4437 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4438 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4442 main
= pet_scop_set_skip(main
, pet_skip_later
,
4443 isl_multi_pw_aff_copy(index
[type
]));
4445 main
= pet_scop_set_skip(main
, type
, index
[type
]);
4451 /* Add the computed skip conditions to "main" and
4452 * add the scops for computing the conditions at the given offset.
4454 struct pet_scop
*pet_skip_info_if::add(struct pet_scop
*scop
, int offset
)
4456 scop
= add(scop
, pet_skip_now
, offset
);
4457 scop
= add(scop
, pet_skip_later
, offset
);
4462 /* Construct a pet_scop for a non-affine if statement.
4464 * We create a separate statement that writes the result
4465 * of the non-affine condition to a virtual scalar.
4466 * A constraint requiring the value of this virtual scalar to be one
4467 * is added to the iteration domains of the then branch.
4468 * Similarly, a constraint requiring the value of this virtual scalar
4469 * to be zero is added to the iteration domains of the else branch, if any.
4470 * We adjust the schedules to ensure that the virtual scalar is written
4471 * before it is read.
4473 * If there are any breaks or continues in the then and/or else
4474 * branches, then we may have to compute a new skip condition.
4475 * This is handled using a pet_skip_info_if object.
4476 * On initialization, the object checks if skip conditions need
4477 * to be computed. If so, it does so in "extract" and adds them in "add".
4479 struct pet_scop
*PetScan::extract_non_affine_if(Expr
*cond
,
4480 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
4481 bool have_else
, int stmt_id
)
4483 struct pet_scop
*scop
;
4484 isl_multi_pw_aff
*test_index
;
4485 int save_n_stmt
= n_stmt
;
4487 test_index
= create_test_index(ctx
, n_test
++);
4489 scop
= extract_non_affine_condition(cond
,
4490 isl_multi_pw_aff_copy(test_index
));
4491 n_stmt
= save_n_stmt
;
4492 scop
= scop_add_array(scop
, test_index
, ast_context
);
4494 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, have_else
, false);
4495 skip
.extract(this, test_index
);
4497 scop
= pet_scop_prefix(scop
, 0);
4498 scop_then
= pet_scop_prefix(scop_then
, 1);
4499 scop_then
= pet_scop_filter(scop_then
,
4500 isl_multi_pw_aff_copy(test_index
), 1);
4502 scop_else
= pet_scop_prefix(scop_else
, 1);
4503 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
4504 scop_then
= pet_scop_add_par(ctx
, scop_then
, scop_else
);
4506 isl_multi_pw_aff_free(test_index
);
4508 scop
= pet_scop_add_seq(ctx
, scop
, scop_then
);
4510 scop
= skip
.add(scop
, 2);
4515 /* Construct a pet_scop for an if statement.
4517 * If the condition fits the pattern of a conditional assignment,
4518 * then it is handled by extract_conditional_assignment.
4519 * Otherwise, we do the following.
4521 * If the condition is affine, then the condition is added
4522 * to the iteration domains of the then branch, while the
4523 * opposite of the condition in added to the iteration domains
4524 * of the else branch, if any.
4525 * We allow the condition to be dynamic, i.e., to refer to
4526 * scalars or array elements that may be written to outside
4527 * of the given if statement. These nested accesses are then represented
4528 * as output dimensions in the wrapping iteration domain.
4529 * If it also written _inside_ the then or else branch, then
4530 * we treat the condition as non-affine.
4531 * As explained in extract_non_affine_if, this will introduce
4532 * an extra statement.
4533 * For aesthetic reasons, we want this statement to have a statement
4534 * number that is lower than those of the then and else branches.
4535 * In order to evaluate if will need such a statement, however, we
4536 * first construct scops for the then and else branches.
4537 * We therefore reserve a statement number if we might have to
4538 * introduce such an extra statement.
4540 * If the condition is not affine, then the scop is created in
4541 * extract_non_affine_if.
4543 * If there are any breaks or continues in the then and/or else
4544 * branches, then we may have to compute a new skip condition.
4545 * This is handled using a pet_skip_info_if object.
4546 * On initialization, the object checks if skip conditions need
4547 * to be computed. If so, it does so in "extract" and adds them in "add".
4549 struct pet_scop
*PetScan::extract(IfStmt
*stmt
)
4551 struct pet_scop
*scop_then
, *scop_else
= NULL
, *scop
;
4557 scop
= extract_conditional_assignment(stmt
);
4561 cond
= try_extract_nested_condition(stmt
->getCond());
4562 if (allow_nested
&& (!cond
|| has_nested(cond
)))
4566 assigned_value_cache
cache(assigned_value
);
4567 scop_then
= extract(stmt
->getThen());
4570 if (stmt
->getElse()) {
4571 assigned_value_cache
cache(assigned_value
);
4572 scop_else
= extract(stmt
->getElse());
4573 if (options
->autodetect
) {
4574 if (scop_then
&& !scop_else
) {
4576 isl_pw_aff_free(cond
);
4579 if (!scop_then
&& scop_else
) {
4581 isl_pw_aff_free(cond
);
4588 (!is_nested_allowed(cond
, scop_then
) ||
4589 (stmt
->getElse() && !is_nested_allowed(cond
, scop_else
)))) {
4590 isl_pw_aff_free(cond
);
4593 if (allow_nested
&& !cond
)
4594 return extract_non_affine_if(stmt
->getCond(), scop_then
,
4595 scop_else
, stmt
->getElse(), stmt_id
);
4598 cond
= extract_condition(stmt
->getCond());
4600 pet_skip_info_if
skip(ctx
, scop_then
, scop_else
, stmt
->getElse(), true);
4601 skip
.extract(this, cond
);
4603 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
4604 set
= isl_pw_aff_non_zero_set(cond
);
4605 scop
= pet_scop_restrict(scop_then
, isl_set_copy(set
));
4607 if (stmt
->getElse()) {
4608 set
= isl_set_subtract(isl_set_copy(valid
), set
);
4609 scop_else
= pet_scop_restrict(scop_else
, set
);
4610 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
4613 scop
= resolve_nested(scop
);
4614 scop
= pet_scop_restrict_context(scop
, valid
);
4617 scop
= pet_scop_prefix(scop
, 0);
4618 scop
= skip
.add(scop
, 1);
4623 /* Try and construct a pet_scop for a label statement.
4624 * We currently only allow labels on expression statements.
4626 struct pet_scop
*PetScan::extract(LabelStmt
*stmt
)
4631 sub
= stmt
->getSubStmt();
4632 if (!isa
<Expr
>(sub
)) {
4637 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
4639 return extract(sub
, extract_expr(cast
<Expr
>(sub
)), label
);
4642 /* Return a one-dimensional multi piecewise affine expression that is equal
4643 * to the constant 1 and is defined over a zero-dimensional domain.
4645 static __isl_give isl_multi_pw_aff
*one_mpa(isl_ctx
*ctx
)
4648 isl_local_space
*ls
;
4651 space
= isl_space_set_alloc(ctx
, 0, 0);
4652 ls
= isl_local_space_from_space(space
);
4653 aff
= isl_aff_zero_on_domain(ls
);
4654 aff
= isl_aff_set_constant_si(aff
, 1);
4656 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
4659 /* Construct a pet_scop for a continue statement.
4661 * We simply create an empty scop with a universal pet_skip_now
4662 * skip condition. This skip condition will then be taken into
4663 * account by the enclosing loop construct, possibly after
4664 * being incorporated into outer skip conditions.
4666 struct pet_scop
*PetScan::extract(ContinueStmt
*stmt
)
4670 scop
= pet_scop_empty(ctx
);
4674 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(ctx
));
4679 /* Construct a pet_scop for a break statement.
4681 * We simply create an empty scop with both a universal pet_skip_now
4682 * skip condition and a universal pet_skip_later skip condition.
4683 * These skip conditions will then be taken into
4684 * account by the enclosing loop construct, possibly after
4685 * being incorporated into outer skip conditions.
4687 struct pet_scop
*PetScan::extract(BreakStmt
*stmt
)
4690 isl_multi_pw_aff
*skip
;
4692 scop
= pet_scop_empty(ctx
);
4696 skip
= one_mpa(ctx
);
4697 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
4698 isl_multi_pw_aff_copy(skip
));
4699 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
4704 /* Try and construct a pet_scop corresponding to "stmt".
4706 * If "stmt" is a compound statement, then "skip_declarations"
4707 * indicates whether we should skip initial declarations in the
4708 * compound statement.
4710 * If the constructed pet_scop is not a (possibly) partial representation
4711 * of "stmt", we update start and end of the pet_scop to those of "stmt".
4712 * In particular, if skip_declarations, then we may have skipped declarations
4713 * inside "stmt" and so the pet_scop may not represent the entire "stmt".
4714 * Note that this function may be called with "stmt" referring to the entire
4715 * body of the function, including the outer braces. In such cases,
4716 * skip_declarations will be set and the braces will not be taken into
4717 * account in scop->start and scop->end.
4719 struct pet_scop
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
4721 struct pet_scop
*scop
;
4722 unsigned start
, end
;
4724 SourceManager
&SM
= PP
.getSourceManager();
4725 const LangOptions
&LO
= PP
.getLangOpts();
4727 if (isa
<Expr
>(stmt
))
4728 return extract(stmt
, extract_expr(cast
<Expr
>(stmt
)));
4730 switch (stmt
->getStmtClass()) {
4731 case Stmt::WhileStmtClass
:
4732 scop
= extract(cast
<WhileStmt
>(stmt
));
4734 case Stmt::ForStmtClass
:
4735 scop
= extract_for(cast
<ForStmt
>(stmt
));
4737 case Stmt::IfStmtClass
:
4738 scop
= extract(cast
<IfStmt
>(stmt
));
4740 case Stmt::CompoundStmtClass
:
4741 scop
= extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
4743 case Stmt::LabelStmtClass
:
4744 scop
= extract(cast
<LabelStmt
>(stmt
));
4746 case Stmt::ContinueStmtClass
:
4747 scop
= extract(cast
<ContinueStmt
>(stmt
));
4749 case Stmt::BreakStmtClass
:
4750 scop
= extract(cast
<BreakStmt
>(stmt
));
4752 case Stmt::DeclStmtClass
:
4753 scop
= extract(cast
<DeclStmt
>(stmt
));
4760 if (partial
|| skip_declarations
)
4763 loc
= stmt
->getLocStart();
4764 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
);
4765 start
= getExpansionOffset(SM
, loc
);
4766 loc
= PP
.getLocForEndOfToken(stmt
->getLocEnd());
4767 end
= getExpansionOffset(SM
, loc
);
4768 scop
= pet_scop_update_start_end(scop
, start
, end
);
4773 /* Do we need to construct a skip condition of the given type
4774 * on a sequence of statements?
4776 * There is no need to construct a new skip condition if only
4777 * only of the two statements has a skip condition or if both
4778 * of their skip conditions are affine.
4780 * In principle we also don't need a new continuation variable if
4781 * the continuation of scop2 is affine, but then we would need
4782 * to allow more complicated forms of continuations.
4784 static bool need_skip_seq(struct pet_scop
*scop1
, struct pet_scop
*scop2
,
4787 if (!scop1
|| !pet_scop_has_skip(scop1
, type
))
4789 if (!scop2
|| !pet_scop_has_skip(scop2
, type
))
4791 if (pet_scop_has_affine_skip(scop1
, type
) &&
4792 pet_scop_has_affine_skip(scop2
, type
))
4797 /* Construct a scop for computing the skip condition of the given type and
4798 * with index expression "skip_index" for a sequence of two scops "scop1"
4801 * The computed scop contains a single statement that essentially does
4803 * skip_index = skip_cond_1 ? 1 : skip_cond_2
4805 * or, in other words, skip_cond1 || skip_cond2.
4806 * In this expression, skip_cond_2 is filtered to reflect that it is
4807 * only evaluated when skip_cond_1 is false.
4809 * The skip condition on scop1 is not removed because it still needs
4810 * to be applied to scop2 when these two scops are combined.
4812 static struct pet_scop
*extract_skip_seq(PetScan
*ps
,
4813 __isl_take isl_multi_pw_aff
*skip_index
,
4814 struct pet_scop
*scop1
, struct pet_scop
*scop2
, enum pet_skip type
)
4816 struct pet_expr
*expr1
, *expr2
, *expr
, *expr_skip
;
4817 struct pet_stmt
*stmt
;
4818 struct pet_scop
*scop
;
4819 isl_ctx
*ctx
= ps
->ctx
;
4821 if (!scop1
|| !scop2
)
4824 expr1
= pet_scop_get_skip_expr(scop1
, type
);
4825 expr2
= pet_scop_get_skip_expr(scop2
, type
);
4826 pet_scop_reset_skip(scop2
, type
);
4828 expr2
= pet_expr_filter(expr2
,
4829 isl_multi_pw_aff_copy(expr1
->acc
.index
), 0);
4831 expr
= universally_true(ctx
);
4832 expr
= pet_expr_new_ternary(ctx
, expr1
, expr
, expr2
);
4833 expr_skip
= pet_expr_from_index(isl_multi_pw_aff_copy(skip_index
));
4835 expr_skip
->acc
.write
= 1;
4836 expr_skip
->acc
.read
= 0;
4838 expr
= pet_expr_new_binary(ctx
, pet_op_assign
, expr_skip
, expr
);
4839 stmt
= pet_stmt_from_pet_expr(ctx
, -1, NULL
, ps
->n_stmt
++, expr
);
4841 scop
= pet_scop_from_pet_stmt(ctx
, stmt
);
4842 scop
= scop_add_array(scop
, skip_index
, ps
->ast_context
);
4843 isl_multi_pw_aff_free(skip_index
);
4847 isl_multi_pw_aff_free(skip_index
);
4851 /* Structure that handles the construction of skip conditions
4852 * on sequences of statements.
4854 * scop1 and scop2 represent the two statements that are combined
4856 struct pet_skip_info_seq
: public pet_skip_info
{
4857 struct pet_scop
*scop1
, *scop2
;
4859 pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4860 struct pet_scop
*scop2
);
4861 void extract(PetScan
*scan
, enum pet_skip type
);
4862 void extract(PetScan
*scan
);
4863 struct pet_scop
*add(struct pet_scop
*scop
, enum pet_skip type
,
4865 struct pet_scop
*add(struct pet_scop
*scop
, int offset
);
4868 /* Initialize a pet_skip_info_seq structure based on
4869 * on the two statements that are going to be combined.
4871 pet_skip_info_seq::pet_skip_info_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
4872 struct pet_scop
*scop2
) : pet_skip_info(ctx
), scop1(scop1
), scop2(scop2
)
4874 skip
[pet_skip_now
] = need_skip_seq(scop1
, scop2
, pet_skip_now
);
4875 equal
= skip
[pet_skip_now
] && skip_equals_skip_later(scop1
) &&
4876 skip_equals_skip_later(scop2
);
4877 skip
[pet_skip_later
] = skip
[pet_skip_now
] && !equal
&&
4878 need_skip_seq(scop1
, scop2
, pet_skip_later
);
4881 /* If we need to construct a skip condition of the given type,
4884 void pet_skip_info_seq::extract(PetScan
*scan
, enum pet_skip type
)
4889 index
[type
] = create_test_index(ctx
, scan
->n_test
++);
4890 scop
[type
] = extract_skip_seq(scan
, isl_multi_pw_aff_copy(index
[type
]),
4891 scop1
, scop2
, type
);
4894 /* Construct the required skip conditions.
4896 void pet_skip_info_seq::extract(PetScan
*scan
)
4898 extract(scan
, pet_skip_now
);
4899 extract(scan
, pet_skip_later
);
4901 drop_skip_later(scop1
, scop2
);
4904 /* Add the computed skip condition of the given type to "main" and
4905 * add the scop for computing the condition at the given offset (the statement
4906 * number). Within this offset, the condition is computed at position 1
4907 * to ensure that it is computed after the corresponding statement.
4909 * If equal is set, then we only computed a skip condition for pet_skip_now,
4910 * but we also need to set it as main's pet_skip_later.
4912 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*main
,
4913 enum pet_skip type
, int offset
)
4918 scop
[type
] = pet_scop_prefix(scop
[type
], 1);
4919 scop
[type
] = pet_scop_prefix(scop
[type
], offset
);
4920 main
= pet_scop_add_par(ctx
, main
, scop
[type
]);
4924 main
= pet_scop_set_skip(main
, pet_skip_later
,
4925 isl_multi_pw_aff_copy(index
[type
]));
4927 main
= pet_scop_set_skip(main
, type
, index
[type
]);
4933 /* Add the computed skip conditions to "main" and
4934 * add the scops for computing the conditions at the given offset.
4936 struct pet_scop
*pet_skip_info_seq::add(struct pet_scop
*scop
, int offset
)
4938 scop
= add(scop
, pet_skip_now
, offset
);
4939 scop
= add(scop
, pet_skip_later
, offset
);
4944 /* Extract a clone of the kill statement in "scop".
4945 * "scop" is expected to have been created from a DeclStmt
4946 * and should have the kill as its first statement.
4948 struct pet_stmt
*PetScan::extract_kill(struct pet_scop
*scop
)
4950 struct pet_expr
*kill
;
4951 struct pet_stmt
*stmt
;
4952 isl_multi_pw_aff
*index
;
4957 if (scop
->n_stmt
< 1)
4958 isl_die(ctx
, isl_error_internal
,
4959 "expecting at least one statement", return NULL
);
4960 stmt
= scop
->stmts
[0];
4961 if (stmt
->body
->type
!= pet_expr_unary
||
4962 stmt
->body
->op
!= pet_op_kill
)
4963 isl_die(ctx
, isl_error_internal
,
4964 "expecting kill statement", return NULL
);
4966 index
= isl_multi_pw_aff_copy(stmt
->body
->args
[0]->acc
.index
);
4967 access
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
4968 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
4969 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
4970 kill
= pet_expr_kill_from_access_and_index(access
, index
);
4971 return pet_stmt_from_pet_expr(ctx
, stmt
->line
, NULL
, n_stmt
++, kill
);
4974 /* Mark all arrays in "scop" as being exposed.
4976 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
4980 for (int i
= 0; i
< scop
->n_array
; ++i
)
4981 scop
->arrays
[i
]->exposed
= 1;
4985 /* Try and construct a pet_scop corresponding to (part of)
4986 * a sequence of statements.
4988 * "block" is set if the sequence respresents the children of
4989 * a compound statement.
4990 * "skip_declarations" is set if we should skip initial declarations
4991 * in the sequence of statements.
4993 * If there are any breaks or continues in the individual statements,
4994 * then we may have to compute a new skip condition.
4995 * This is handled using a pet_skip_info_seq object.
4996 * On initialization, the object checks if skip conditions need
4997 * to be computed. If so, it does so in "extract" and adds them in "add".
4999 * If "block" is set, then we need to insert kill statements at
5000 * the end of the block for any array that has been declared by
5001 * one of the statements in the sequence. Each of these declarations
5002 * results in the construction of a kill statement at the place
5003 * of the declaration, so we simply collect duplicates of
5004 * those kill statements and append these duplicates to the constructed scop.
5006 * If "block" is not set, then any array declared by one of the statements
5007 * in the sequence is marked as being exposed.
5009 * If autodetect is set, then we allow the extraction of only a subrange
5010 * of the sequence of statements. However, if there is at least one statement
5011 * for which we could not construct a scop and the final range contains
5012 * either no statements or at least one kill, then we discard the entire
5015 struct pet_scop
*PetScan::extract(StmtRange stmt_range
, bool block
,
5016 bool skip_declarations
)
5021 bool partial_range
= false;
5022 set
<struct pet_stmt
*> kills
;
5023 set
<struct pet_stmt
*>::iterator it
;
5025 scop
= pet_scop_empty(ctx
);
5026 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
5028 struct pet_scop
*scop_i
;
5030 if (skip_declarations
&&
5031 child
->getStmtClass() == Stmt::DeclStmtClass
)
5034 scop_i
= extract(child
);
5035 if (scop
->n_stmt
!= 0 && partial
) {
5036 pet_scop_free(scop_i
);
5039 pet_skip_info_seq
skip(ctx
, scop
, scop_i
);
5042 scop_i
= pet_scop_prefix(scop_i
, 0);
5043 if (scop_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
) {
5045 kills
.insert(extract_kill(scop_i
));
5047 scop_i
= mark_exposed(scop_i
);
5049 scop_i
= pet_scop_prefix(scop_i
, j
);
5050 if (options
->autodetect
) {
5052 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
5054 partial_range
= true;
5055 if (scop
->n_stmt
!= 0 && !scop_i
)
5058 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
5061 scop
= skip
.add(scop
, j
);
5063 if (partial
|| !scop
)
5067 for (it
= kills
.begin(); it
!= kills
.end(); ++it
) {
5069 scop_j
= pet_scop_from_pet_stmt(ctx
, *it
);
5070 scop_j
= pet_scop_prefix(scop_j
, j
);
5071 scop
= pet_scop_add_seq(ctx
, scop
, scop_j
);
5074 if (scop
&& partial_range
) {
5075 if (scop
->n_stmt
== 0 || kills
.size() != 0) {
5076 pet_scop_free(scop
);
5085 /* Check if the scop marked by the user is exactly this Stmt
5086 * or part of this Stmt.
5087 * If so, return a pet_scop corresponding to the marked region.
5088 * Otherwise, return NULL.
5090 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
5092 SourceManager
&SM
= PP
.getSourceManager();
5093 unsigned start_off
, end_off
;
5095 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
5096 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
5098 if (start_off
> loc
.end
)
5100 if (end_off
< loc
.start
)
5102 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
5103 return extract(stmt
);
5107 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
5108 Stmt
*child
= *start
;
5111 start_off
= getExpansionOffset(SM
, child
->getLocStart());
5112 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
5113 if (start_off
< loc
.start
&& end_off
>= loc
.end
)
5115 if (start_off
>= loc
.start
)
5120 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
5122 start_off
= SM
.getFileOffset(child
->getLocStart());
5123 if (start_off
>= loc
.end
)
5127 return extract(StmtRange(start
, end
), false, false);
5130 /* Set the size of index "pos" of "array" to "size".
5131 * In particular, add a constraint of the form
5135 * to array->extent and a constraint of the form
5139 * to array->context.
5141 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
5142 __isl_take isl_pw_aff
*size
)
5152 valid
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
));
5153 array
->context
= isl_set_intersect(array
->context
, valid
);
5155 dim
= isl_set_get_space(array
->extent
);
5156 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
5157 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
5158 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
5159 index
= isl_pw_aff_alloc(univ
, aff
);
5161 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
5162 isl_set_dim(array
->extent
, isl_dim_set
));
5163 id
= isl_set_get_tuple_id(array
->extent
);
5164 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
5165 bound
= isl_pw_aff_lt_set(index
, size
);
5167 array
->extent
= isl_set_intersect(array
->extent
, bound
);
5169 if (!array
->context
|| !array
->extent
)
5174 pet_array_free(array
);
5178 /* Figure out the size of the array at position "pos" and all
5179 * subsequent positions from "type" and update "array" accordingly.
5181 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
5182 const Type
*type
, int pos
)
5184 const ArrayType
*atype
;
5190 if (type
->isPointerType()) {
5191 type
= type
->getPointeeType().getTypePtr();
5192 return set_upper_bounds(array
, type
, pos
+ 1);
5194 if (!type
->isArrayType())
5197 type
= type
->getCanonicalTypeInternal().getTypePtr();
5198 atype
= cast
<ArrayType
>(type
);
5200 if (type
->isConstantArrayType()) {
5201 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
5202 size
= extract_affine(ca
->getSize());
5203 array
= update_size(array
, pos
, size
);
5204 } else if (type
->isVariableArrayType()) {
5205 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
5206 size
= extract_affine(vla
->getSizeExpr());
5207 array
= update_size(array
, pos
, size
);
5210 type
= atype
->getElementType().getTypePtr();
5212 return set_upper_bounds(array
, type
, pos
+ 1);
5215 /* Is "T" the type of a variable length array with static size?
5217 static bool is_vla_with_static_size(QualType T
)
5219 const VariableArrayType
*vlatype
;
5221 if (!T
->isVariableArrayType())
5223 vlatype
= cast
<VariableArrayType
>(T
);
5224 return vlatype
->getSizeModifier() == VariableArrayType::Static
;
5227 /* Return the type of "decl" as an array.
5229 * In particular, if "decl" is a parameter declaration that
5230 * is a variable length array with a static size, then
5231 * return the original type (i.e., the variable length array).
5232 * Otherwise, return the type of decl.
5234 static QualType
get_array_type(ValueDecl
*decl
)
5239 parm
= dyn_cast
<ParmVarDecl
>(decl
);
5241 return decl
->getType();
5243 T
= parm
->getOriginalType();
5244 if (!is_vla_with_static_size(T
))
5245 return decl
->getType();
5249 /* Does "decl" have definition that we can keep track of in a pet_type?
5251 static bool has_printable_definition(RecordDecl
*decl
)
5253 if (!decl
->getDeclName())
5255 return decl
->getLexicalDeclContext() == decl
->getDeclContext();
5258 /* Construct and return a pet_array corresponding to the variable "decl".
5259 * In particular, initialize array->extent to
5261 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
5263 * and then call set_upper_bounds to set the upper bounds on the indices
5264 * based on the type of the variable.
5266 * If the base type is that of a record with a top-level definition and
5267 * if "types" is not null, then the RecordDecl corresponding to the type
5268 * is added to "types".
5270 * If the base type is that of a record with no top-level definition,
5271 * then we replace it by "<subfield>".
5273 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
,
5274 lex_recorddecl_set
*types
)
5276 struct pet_array
*array
;
5277 QualType qt
= get_array_type(decl
);
5278 const Type
*type
= qt
.getTypePtr();
5279 int depth
= array_depth(type
);
5280 QualType base
= pet_clang_base_type(qt
);
5285 array
= isl_calloc_type(ctx
, struct pet_array
);
5289 id
= isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
5290 dim
= isl_space_set_alloc(ctx
, 0, depth
);
5291 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
5293 array
->extent
= isl_set_nat_universe(dim
);
5295 dim
= isl_space_params_alloc(ctx
, 0);
5296 array
->context
= isl_set_universe(dim
);
5298 array
= set_upper_bounds(array
, type
, 0);
5302 name
= base
.getAsString();
5304 if (types
&& base
->isRecordType()) {
5305 RecordDecl
*decl
= pet_clang_record_decl(base
);
5306 if (has_printable_definition(decl
))
5307 types
->insert(decl
);
5309 name
= "<subfield>";
5312 array
->element_type
= strdup(name
.c_str());
5313 array
->element_is_record
= base
->isRecordType();
5314 array
->element_size
= decl
->getASTContext().getTypeInfo(base
).first
/ 8;
5319 /* Construct and return a pet_array corresponding to the sequence
5320 * of declarations "decls".
5321 * If the sequence contains a single declaration, then it corresponds
5322 * to a simple array access. Otherwise, it corresponds to a member access,
5323 * with the declaration for the substructure following that of the containing
5324 * structure in the sequence of declarations.
5325 * We start with the outermost substructure and then combine it with
5326 * information from the inner structures.
5328 * Additionally, keep track of all required types in "types".
5330 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
,
5331 vector
<ValueDecl
*> decls
, lex_recorddecl_set
*types
)
5333 struct pet_array
*array
;
5334 vector
<ValueDecl
*>::iterator it
;
5338 array
= extract_array(ctx
, *it
, types
);
5340 for (++it
; it
!= decls
.end(); ++it
) {
5341 struct pet_array
*parent
;
5342 const char *base_name
, *field_name
;
5346 array
= extract_array(ctx
, *it
, types
);
5348 return pet_array_free(parent
);
5350 base_name
= isl_set_get_tuple_name(parent
->extent
);
5351 field_name
= isl_set_get_tuple_name(array
->extent
);
5352 product_name
= member_access_name(ctx
, base_name
, field_name
);
5354 array
->extent
= isl_set_product(isl_set_copy(parent
->extent
),
5357 array
->extent
= isl_set_set_tuple_name(array
->extent
,
5359 array
->context
= isl_set_intersect(array
->context
,
5360 isl_set_copy(parent
->context
));
5362 pet_array_free(parent
);
5365 if (!array
->extent
|| !array
->context
|| !product_name
)
5366 return pet_array_free(array
);
5372 /* Add a pet_type corresponding to "decl" to "scop, provided
5373 * it is a member of "types" and it has not been added before
5374 * (i.e., it is not a member of "types_done".
5376 * Since we want the user to be able to print the types
5377 * in the order in which they appear in the scop, we need to
5378 * make sure that types of fields in a structure appear before
5379 * that structure. We therefore call ourselves recursively
5380 * on the types of all record subfields.
5382 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
5383 RecordDecl
*decl
, Preprocessor
&PP
, lex_recorddecl_set
&types
,
5384 lex_recorddecl_set
&types_done
)
5387 llvm::raw_string_ostream
S(s
);
5388 RecordDecl::field_iterator it
;
5390 if (types
.find(decl
) == types
.end())
5392 if (types_done
.find(decl
) != types_done
.end())
5395 for (it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
5397 QualType type
= it
->getType();
5399 if (!type
->isRecordType())
5401 record
= pet_clang_record_decl(type
);
5402 scop
= add_type(ctx
, scop
, record
, PP
, types
, types_done
);
5405 if (strlen(decl
->getName().str().c_str()) == 0)
5408 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
5411 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
5412 decl
->getName().str().c_str(), s
.c_str());
5413 if (!scop
->types
[scop
->n_type
])
5414 return pet_scop_free(scop
);
5416 types_done
.insert(decl
);
5423 /* Construct a list of pet_arrays, one for each array (or scalar)
5424 * accessed inside "scop", add this list to "scop" and return the result.
5426 * The context of "scop" is updated with the intersection of
5427 * the contexts of all arrays, i.e., constraints on the parameters
5428 * that ensure that the arrays have a valid (non-negative) size.
5430 * If the any of the extracted arrays refers to a member access,
5431 * then also add the required types to "scop".
5433 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
)
5436 set
<vector
<ValueDecl
*> > arrays
;
5437 set
<vector
<ValueDecl
*> >::iterator it
;
5438 lex_recorddecl_set types
;
5439 lex_recorddecl_set types_done
;
5440 lex_recorddecl_set::iterator types_it
;
5442 struct pet_array
**scop_arrays
;
5447 pet_scop_collect_arrays(scop
, arrays
);
5448 if (arrays
.size() == 0)
5451 n_array
= scop
->n_array
;
5453 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
5454 n_array
+ arrays
.size());
5457 scop
->arrays
= scop_arrays
;
5459 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
5460 struct pet_array
*array
;
5461 array
= extract_array(ctx
, *it
, &types
);
5462 scop
->arrays
[n_array
+ i
] = array
;
5463 if (!scop
->arrays
[n_array
+ i
])
5466 scop
->context
= isl_set_intersect(scop
->context
,
5467 isl_set_copy(array
->context
));
5472 if (types
.size() == 0)
5475 scop
->types
= isl_alloc_array(ctx
, struct pet_type
*, types
.size());
5479 for (types_it
= types
.begin(); types_it
!= types
.end(); ++types_it
)
5480 scop
= add_type(ctx
, scop
, *types_it
, PP
, types
, types_done
);
5484 pet_scop_free(scop
);
5488 /* Bound all parameters in scop->context to the possible values
5489 * of the corresponding C variable.
5491 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
5498 n
= isl_set_dim(scop
->context
, isl_dim_param
);
5499 for (int i
= 0; i
< n
; ++i
) {
5503 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
5504 if (is_nested_parameter(id
)) {
5506 isl_die(isl_set_get_ctx(scop
->context
),
5508 "unresolved nested parameter", goto error
);
5510 decl
= (ValueDecl
*) isl_id_get_user(id
);
5513 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
5521 pet_scop_free(scop
);
5525 /* Construct a pet_scop from the given function.
5527 * If the scop was delimited by scop and endscop pragmas, then we override
5528 * the file offsets by those derived from the pragmas.
5530 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
5535 stmt
= fd
->getBody();
5537 if (options
->autodetect
)
5538 scop
= extract(stmt
, true);
5541 scop
= pet_scop_update_start_end(scop
, loc
.start
, loc
.end
);
5543 scop
= pet_scop_detect_parameter_accesses(scop
);
5544 scop
= scan_arrays(scop
);
5545 scop
= add_parameter_bounds(scop
);
5546 scop
= pet_scop_gist(scop
, value_bounds
);