1 /*
2  * Copyright 2011      Leiden University. All rights reserved.
3  * Copyright 2012-2015 Ecole Normale Superieure. All rights reserved.
4  * Copyright 2015-2017 Sven Verdoolaege. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *    1. Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *    2. Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and documentation
31  * are those of the authors and should not be interpreted as
32  * representing official policies, either expressed or implied, of
33  * Leiden University.
34  */
35 
36 #include "config.h"
37 
38 #include <string.h>
39 #include <set>
40 #include <map>
41 #include <iostream>
42 #include <sstream>
43 #include <llvm/Support/raw_ostream.h>
44 #include <clang/AST/ASTContext.h>
45 #include <clang/AST/ASTDiagnostic.h>
46 #include <clang/AST/Attr.h>
47 #include <clang/AST/Expr.h>
48 #include <clang/AST/RecursiveASTVisitor.h>
49 
50 #include <isl/id.h>
51 #include <isl/space.h>
52 #include <isl/aff.h>
53 #include <isl/set.h>
54 #include <isl/union_set.h>
55 
56 #include "aff.h"
57 #include "array.h"
58 #include "clang_compatibility.h"
59 #include "clang.h"
60 #include "context.h"
61 #include "expr.h"
62 #include "expr_plus.h"
63 #include "id.h"
64 #include "inliner.h"
65 #include "inlined_calls.h"
66 #include "killed_locals.h"
67 #include "nest.h"
68 #include "options.h"
69 #include "scan.h"
70 #include "scop.h"
71 #include "scop_plus.h"
72 #include "substituter.h"
73 #include "tree.h"
74 #include "tree2scop.h"
75 
76 using namespace std;
77 using namespace clang;
78 
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)79 static enum pet_op_type UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind)
80 {
81 	switch (kind) {
82 	case UO_Minus:
83 		return pet_op_minus;
84 	case UO_Not:
85 		return pet_op_not;
86 	case UO_LNot:
87 		return pet_op_lnot;
88 	case UO_PostInc:
89 		return pet_op_post_inc;
90 	case UO_PostDec:
91 		return pet_op_post_dec;
92 	case UO_PreInc:
93 		return pet_op_pre_inc;
94 	case UO_PreDec:
95 		return pet_op_pre_dec;
96 	default:
97 		return pet_op_last;
98 	}
99 }
100 
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)101 static enum pet_op_type BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind)
102 {
103 	switch (kind) {
104 	case BO_AddAssign:
105 		return pet_op_add_assign;
106 	case BO_SubAssign:
107 		return pet_op_sub_assign;
108 	case BO_MulAssign:
109 		return pet_op_mul_assign;
110 	case BO_DivAssign:
111 		return pet_op_div_assign;
112 	case BO_AndAssign:
113 		return pet_op_and_assign;
114 	case BO_XorAssign:
115 		return pet_op_xor_assign;
116 	case BO_OrAssign:
117 		return pet_op_or_assign;
118 	case BO_Assign:
119 		return pet_op_assign;
120 	case BO_Add:
121 		return pet_op_add;
122 	case BO_Sub:
123 		return pet_op_sub;
124 	case BO_Mul:
125 		return pet_op_mul;
126 	case BO_Div:
127 		return pet_op_div;
128 	case BO_Rem:
129 		return pet_op_mod;
130 	case BO_Shl:
131 		return pet_op_shl;
132 	case BO_Shr:
133 		return pet_op_shr;
134 	case BO_EQ:
135 		return pet_op_eq;
136 	case BO_NE:
137 		return pet_op_ne;
138 	case BO_LE:
139 		return pet_op_le;
140 	case BO_GE:
141 		return pet_op_ge;
142 	case BO_LT:
143 		return pet_op_lt;
144 	case BO_GT:
145 		return pet_op_gt;
146 	case BO_And:
147 		return pet_op_and;
148 	case BO_Xor:
149 		return pet_op_xor;
150 	case BO_Or:
151 		return pet_op_or;
152 	case BO_LAnd:
153 		return pet_op_land;
154 	case BO_LOr:
155 		return pet_op_lor;
156 	default:
157 		return pet_op_last;
158 	}
159 }
160 
161 #ifdef GETTYPEINFORETURNSTYPEINFO
162 
size_in_bytes(ASTContext & context,QualType type)163 static int size_in_bytes(ASTContext &context, QualType type)
164 {
165 	return context.getTypeInfo(type).Width / 8;
166 }
167 
168 #else
169 
size_in_bytes(ASTContext & context,QualType type)170 static int size_in_bytes(ASTContext &context, QualType type)
171 {
172 	return context.getTypeInfo(type).first / 8;
173 }
174 
175 #endif
176 
177 /* Check if the element type corresponding to the given array type
178  * has a const qualifier.
179  */
const_base(QualType qt)180 static bool const_base(QualType qt)
181 {
182 	const Type *type = qt.getTypePtr();
183 
184 	if (type->isPointerType())
185 		return const_base(type->getPointeeType());
186 	if (type->isArrayType()) {
187 		const ArrayType *atype;
188 		type = type->getCanonicalTypeInternal().getTypePtr();
189 		atype = cast<ArrayType>(type);
190 		return const_base(atype->getElementType());
191 	}
192 
193 	return qt.isConstQualified();
194 }
195 
~PetScan()196 PetScan::~PetScan()
197 {
198 	std::map<const Type *, pet_expr *>::iterator it;
199 	std::map<FunctionDecl *, pet_function_summary *>::iterator it_s;
200 
201 	for (it = type_size.begin(); it != type_size.end(); ++it)
202 		pet_expr_free(it->second);
203 	for (it_s = summary_cache.begin(); it_s != summary_cache.end(); ++it_s)
204 		pet_function_summary_free(it_s->second);
205 
206 	isl_id_to_pet_expr_free(id_size);
207 	isl_union_map_free(value_bounds);
208 }
209 
210 /* Report a diagnostic on the range "range", unless autodetect is set.
211  */
report(SourceRange range,unsigned id)212 void PetScan::report(SourceRange range, unsigned id)
213 {
214 	if (options->autodetect)
215 		return;
216 
217 	SourceLocation loc = range.getBegin();
218 	DiagnosticsEngine &diag = PP.getDiagnostics();
219 	DiagnosticBuilder B = diag.Report(loc, id) << range;
220 }
221 
222 /* Report a diagnostic on "stmt", unless autodetect is set.
223  */
report(Stmt * stmt,unsigned id)224 void PetScan::report(Stmt *stmt, unsigned id)
225 {
226 	report(stmt->getSourceRange(), id);
227 }
228 
229 /* Report a diagnostic on "decl", unless autodetect is set.
230  */
report(Decl * decl,unsigned id)231 void PetScan::report(Decl *decl, unsigned id)
232 {
233 	report(decl->getSourceRange(), id);
234 }
235 
236 /* Called if we found something we (currently) cannot handle.
237  * We'll provide more informative warnings later.
238  *
239  * We only actually complain if autodetect is false.
240  */
unsupported(Stmt * stmt)241 void PetScan::unsupported(Stmt *stmt)
242 {
243 	DiagnosticsEngine &diag = PP.getDiagnostics();
244 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
245 					   "unsupported");
246 	report(stmt, id);
247 }
248 
249 /* Report an unsupported unary operator, unless autodetect is set.
250  */
report_unsupported_unary_operator(Stmt * stmt)251 void PetScan::report_unsupported_unary_operator(Stmt *stmt)
252 {
253 	DiagnosticsEngine &diag = PP.getDiagnostics();
254 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
255 			       "this type of unary operator is not supported");
256 	report(stmt, id);
257 }
258 
259 /* Report an unsupported binary operator, unless autodetect is set.
260  */
report_unsupported_binary_operator(Stmt * stmt)261 void PetScan::report_unsupported_binary_operator(Stmt *stmt)
262 {
263 	DiagnosticsEngine &diag = PP.getDiagnostics();
264 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
265 			       "this type of binary operator is not supported");
266 	report(stmt, id);
267 }
268 
269 /* Report an unsupported statement type, unless autodetect is set.
270  */
report_unsupported_statement_type(Stmt * stmt)271 void PetScan::report_unsupported_statement_type(Stmt *stmt)
272 {
273 	DiagnosticsEngine &diag = PP.getDiagnostics();
274 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
275 				   "this type of statement is not supported");
276 	report(stmt, id);
277 }
278 
279 /* Report a missing prototype, unless autodetect is set.
280  */
report_prototype_required(Stmt * stmt)281 void PetScan::report_prototype_required(Stmt *stmt)
282 {
283 	DiagnosticsEngine &diag = PP.getDiagnostics();
284 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
285 					   "prototype required");
286 	report(stmt, id);
287 }
288 
289 /* Report a missing increment, unless autodetect is set.
290  */
report_missing_increment(Stmt * stmt)291 void PetScan::report_missing_increment(Stmt *stmt)
292 {
293 	DiagnosticsEngine &diag = PP.getDiagnostics();
294 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
295 					   "missing increment");
296 	report(stmt, id);
297 }
298 
299 /* Report a missing summary function, unless autodetect is set.
300  */
report_missing_summary_function(Stmt * stmt)301 void PetScan::report_missing_summary_function(Stmt *stmt)
302 {
303 	DiagnosticsEngine &diag = PP.getDiagnostics();
304 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
305 					   "missing summary function");
306 	report(stmt, id);
307 }
308 
309 /* Report a missing summary function body, unless autodetect is set.
310  */
report_missing_summary_function_body(Stmt * stmt)311 void PetScan::report_missing_summary_function_body(Stmt *stmt)
312 {
313 	DiagnosticsEngine &diag = PP.getDiagnostics();
314 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
315 					   "missing summary function body");
316 	report(stmt, id);
317 }
318 
319 /* Report an unsupported argument in a call to an inlined function,
320  * unless autodetect is set.
321  */
report_unsupported_inline_function_argument(Stmt * stmt)322 void PetScan::report_unsupported_inline_function_argument(Stmt *stmt)
323 {
324 	DiagnosticsEngine &diag = PP.getDiagnostics();
325 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
326 				   "unsupported inline function call argument");
327 	report(stmt, id);
328 }
329 
330 /* Report an unsupported type of declaration, unless autodetect is set.
331  */
report_unsupported_declaration(Decl * decl)332 void PetScan::report_unsupported_declaration(Decl *decl)
333 {
334 	DiagnosticsEngine &diag = PP.getDiagnostics();
335 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
336 				   "unsupported declaration");
337 	report(decl, id);
338 }
339 
340 /* Report an unbalanced pair of scop/endscop pragmas, unless autodetect is set.
341  */
report_unbalanced_pragmas(SourceLocation scop,SourceLocation endscop)342 void PetScan::report_unbalanced_pragmas(SourceLocation scop,
343 	SourceLocation endscop)
344 {
345 	if (options->autodetect)
346 		return;
347 
348 	DiagnosticsEngine &diag = PP.getDiagnostics();
349 	{
350 		unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
351 					   "unbalanced endscop pragma");
352 		DiagnosticBuilder B2 = diag.Report(endscop, id);
353 	}
354 	{
355 		unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Note,
356 					   "corresponding scop pragma");
357 		DiagnosticBuilder B = diag.Report(scop, id);
358 	}
359 }
360 
361 /* Report a return statement in an unsupported context,
362  * unless autodetect is set.
363  */
report_unsupported_return(Stmt * stmt)364 void PetScan::report_unsupported_return(Stmt *stmt)
365 {
366 	DiagnosticsEngine &diag = PP.getDiagnostics();
367 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
368 			   "return statements not supported in this context");
369 	report(stmt, id);
370 }
371 
372 /* Report a return statement that does not appear at the end of a function,
373  * unless autodetect is set.
374  */
report_return_not_at_end_of_function(Stmt * stmt)375 void PetScan::report_return_not_at_end_of_function(Stmt *stmt)
376 {
377 	DiagnosticsEngine &diag = PP.getDiagnostics();
378 	unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
379 		       "return statement must be final statement in function");
380 	report(stmt, id);
381 }
382 
383 /* Extract an integer from "val", which is assumed to be non-negative.
384  */
extract_unsigned(isl_ctx * ctx,const llvm::APInt & val)385 static __isl_give isl_val *extract_unsigned(isl_ctx *ctx,
386 	const llvm::APInt &val)
387 {
388 	unsigned n;
389 	const uint64_t *data;
390 
391 	data = val.getRawData();
392 	n = val.getNumWords();
393 	return isl_val_int_from_chunks(ctx, n, sizeof(uint64_t), data);
394 }
395 
396 /* Extract an integer from "val".  If "is_signed" is set, then "val"
397  * is signed.  Otherwise it it unsigned.
398  */
extract_int(isl_ctx * ctx,bool is_signed,llvm::APInt val)399 static __isl_give isl_val *extract_int(isl_ctx *ctx, bool is_signed,
400 	llvm::APInt val)
401 {
402 	int is_negative = is_signed && val.isNegative();
403 	isl_val *v;
404 
405 	if (is_negative)
406 		val = -val;
407 
408 	v = extract_unsigned(ctx, val);
409 
410 	if (is_negative)
411 		v = isl_val_neg(v);
412 	return v;
413 }
414 
415 /* Extract an integer from "expr".
416  */
extract_int(isl_ctx * ctx,IntegerLiteral * expr)417 __isl_give isl_val *PetScan::extract_int(isl_ctx *ctx, IntegerLiteral *expr)
418 {
419 	const Type *type = expr->getType().getTypePtr();
420 	bool is_signed = type->hasSignedIntegerRepresentation();
421 
422 	return ::extract_int(ctx, is_signed, expr->getValue());
423 }
424 
425 /* Extract an integer from "expr".
426  * Return NULL if "expr" does not (obviously) represent an integer.
427  */
extract_int(clang::ParenExpr * expr)428 __isl_give isl_val *PetScan::extract_int(clang::ParenExpr *expr)
429 {
430 	return extract_int(expr->getSubExpr());
431 }
432 
433 /* Extract an integer from "expr".
434  * Return NULL if "expr" does not (obviously) represent an integer.
435  */
extract_int(clang::Expr * expr)436 __isl_give isl_val *PetScan::extract_int(clang::Expr *expr)
437 {
438 	if (expr->getStmtClass() == Stmt::IntegerLiteralClass)
439 		return extract_int(ctx, cast<IntegerLiteral>(expr));
440 	if (expr->getStmtClass() == Stmt::ParenExprClass)
441 		return extract_int(cast<ParenExpr>(expr));
442 
443 	unsupported(expr);
444 	return NULL;
445 }
446 
447 /* Extract a pet_expr from the APInt "val", which is assumed
448  * to be non-negative.
449  */
extract_expr(const llvm::APInt & val)450 __isl_give pet_expr *PetScan::extract_expr(const llvm::APInt &val)
451 {
452 	return pet_expr_new_int(extract_unsigned(ctx, val));
453 }
454 
455 /* Return the number of bits needed to represent the type of "decl",
456  * if it is an integer type.  Otherwise return 0.
457  * If qt is signed then return the opposite of the number of bits.
458  */
get_type_size(ValueDecl * decl)459 static int get_type_size(ValueDecl *decl)
460 {
461 	return pet_clang_get_type_size(decl->getType(), decl->getASTContext());
462 }
463 
464 /* Bound parameter "pos" of "set" to the possible values of "decl".
465  */
set_parameter_bounds(__isl_take isl_set * set,unsigned pos,ValueDecl * decl)466 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
467 	unsigned pos, ValueDecl *decl)
468 {
469 	int type_size;
470 	isl_ctx *ctx;
471 	isl_val *bound;
472 
473 	ctx = isl_set_get_ctx(set);
474 	type_size = get_type_size(decl);
475 	if (type_size == 0)
476 		isl_die(ctx, isl_error_invalid, "not an integer type",
477 			return isl_set_free(set));
478 	if (type_size > 0) {
479 		set = isl_set_lower_bound_si(set, isl_dim_param, pos, 0);
480 		bound = isl_val_int_from_ui(ctx, type_size);
481 		bound = isl_val_2exp(bound);
482 		bound = isl_val_sub_ui(bound, 1);
483 		set = isl_set_upper_bound_val(set, isl_dim_param, pos, bound);
484 	} else {
485 		bound = isl_val_int_from_ui(ctx, -type_size - 1);
486 		bound = isl_val_2exp(bound);
487 		bound = isl_val_sub_ui(bound, 1);
488 		set = isl_set_upper_bound_val(set, isl_dim_param, pos,
489 						isl_val_copy(bound));
490 		bound = isl_val_neg(bound);
491 		bound = isl_val_sub_ui(bound, 1);
492 		set = isl_set_lower_bound_val(set, isl_dim_param, pos, bound);
493 	}
494 
495 	return set;
496 }
497 
extract_index_expr(ImplicitCastExpr * expr)498 __isl_give pet_expr *PetScan::extract_index_expr(ImplicitCastExpr *expr)
499 {
500 	return extract_index_expr(expr->getSubExpr());
501 }
502 
503 /* Construct a pet_expr representing an index expression for an access
504  * to the variable referenced by "expr".
505  *
506  * If "expr" references an enum constant, then return an integer expression
507  * instead, representing the value of the enum constant.
508  */
extract_index_expr(DeclRefExpr * expr)509 __isl_give pet_expr *PetScan::extract_index_expr(DeclRefExpr *expr)
510 {
511 	return extract_index_expr(expr->getDecl());
512 }
513 
514 /* Construct a pet_expr representing an index expression for an access
515  * to the variable "decl".
516  *
517  * If "decl" is an enum constant, then we return an integer expression
518  * instead, representing the value of the enum constant.
519  */
extract_index_expr(ValueDecl * decl)520 __isl_give pet_expr *PetScan::extract_index_expr(ValueDecl *decl)
521 {
522 	isl_id *id;
523 
524 	if (isa<EnumConstantDecl>(decl))
525 		return extract_expr(cast<EnumConstantDecl>(decl));
526 
527 	id = pet_id_from_decl(ctx, decl);
528 	return pet_id_create_index_expr(id);
529 }
530 
531 /* Construct a pet_expr representing the index expression "expr"
532  * Return NULL on error.
533  *
534  * If "expr" is a reference to an enum constant, then return
535  * an integer expression instead, representing the value of the enum constant.
536  */
extract_index_expr(Expr * expr)537 __isl_give pet_expr *PetScan::extract_index_expr(Expr *expr)
538 {
539 	switch (expr->getStmtClass()) {
540 	case Stmt::ImplicitCastExprClass:
541 		return extract_index_expr(cast<ImplicitCastExpr>(expr));
542 	case Stmt::DeclRefExprClass:
543 		return extract_index_expr(cast<DeclRefExpr>(expr));
544 	case Stmt::ArraySubscriptExprClass:
545 		return extract_index_expr(cast<ArraySubscriptExpr>(expr));
546 	case Stmt::IntegerLiteralClass:
547 		return extract_expr(cast<IntegerLiteral>(expr));
548 	case Stmt::MemberExprClass:
549 		return extract_index_expr(cast<MemberExpr>(expr));
550 	default:
551 		unsupported(expr);
552 	}
553 	return NULL;
554 }
555 
556 /* Extract an index expression from the given array subscript expression.
557  *
558  * We first extract an index expression from the base.
559  * This will result in an index expression with a range that corresponds
560  * to the earlier indices.
561  * We then extract the current index and let
562  * pet_expr_access_subscript combine the two.
563  */
extract_index_expr(ArraySubscriptExpr * expr)564 __isl_give pet_expr *PetScan::extract_index_expr(ArraySubscriptExpr *expr)
565 {
566 	Expr *base = expr->getBase();
567 	Expr *idx = expr->getIdx();
568 	pet_expr *index;
569 	pet_expr *base_expr;
570 
571 	base_expr = extract_index_expr(base);
572 	index = extract_expr(idx);
573 
574 	base_expr = pet_expr_access_subscript(base_expr, index);
575 
576 	return base_expr;
577 }
578 
579 /* Extract an index expression from a member expression.
580  *
581  * If the base access (to the structure containing the member)
582  * is of the form
583  *
584  *	A[..]
585  *
586  * and the member is called "f", then the member access is of
587  * the form
588  *
589  *	A_f[A[..] -> f[]]
590  *
591  * If the member access is to an anonymous struct, then simply return
592  *
593  *	A[..]
594  *
595  * If the member access in the source code is of the form
596  *
597  *	A->f
598  *
599  * then it is treated as
600  *
601  *	A[0].f
602  */
extract_index_expr(MemberExpr * expr)603 __isl_give pet_expr *PetScan::extract_index_expr(MemberExpr *expr)
604 {
605 	Expr *base = expr->getBase();
606 	FieldDecl *field = cast<FieldDecl>(expr->getMemberDecl());
607 	pet_expr *base_index;
608 	isl_id *id;
609 
610 	base_index = extract_index_expr(base);
611 
612 	if (expr->isArrow()) {
613 		pet_expr *index = pet_expr_new_int(isl_val_zero(ctx));
614 		base_index = pet_expr_access_subscript(base_index, index);
615 	}
616 
617 	if (field->isAnonymousStructOrUnion())
618 		return base_index;
619 
620 	id = pet_id_from_decl(ctx, field);
621 
622 	return pet_expr_access_member(base_index, id);
623 }
624 
625 /* Mark the given access pet_expr as a write.
626  */
mark_write(__isl_take pet_expr * access)627 static __isl_give pet_expr *mark_write(__isl_take pet_expr *access)
628 {
629 	access = pet_expr_access_set_write(access, 1);
630 	access = pet_expr_access_set_read(access, 0);
631 
632 	return access;
633 }
634 
635 /* Mark the given (read) access pet_expr as also possibly being written.
636  * That is, initialize the may write access relation from the may read relation
637  * and initialize the must write access relation to the empty relation.
638  */
mark_may_write(__isl_take pet_expr * expr)639 static __isl_give pet_expr *mark_may_write(__isl_take pet_expr *expr)
640 {
641 	isl_union_map *access;
642 	isl_union_map *empty;
643 
644 	access = pet_expr_access_get_dependent_access(expr,
645 						pet_expr_access_may_read);
646 	empty = isl_union_map_empty(isl_union_map_get_space(access));
647 	expr = pet_expr_access_set_access(expr, pet_expr_access_may_write,
648 					    access);
649 	expr = pet_expr_access_set_access(expr, pet_expr_access_must_write,
650 					    empty);
651 
652 	return expr;
653 }
654 
655 /* Construct a pet_expr representing a unary operator expression.
656  */
extract_expr(UnaryOperator * expr)657 __isl_give pet_expr *PetScan::extract_expr(UnaryOperator *expr)
658 {
659 	int type_size;
660 	pet_expr *arg;
661 	enum pet_op_type op;
662 
663 	op = UnaryOperatorKind2pet_op_type(expr->getOpcode());
664 	if (op == pet_op_last) {
665 		report_unsupported_unary_operator(expr);
666 		return NULL;
667 	}
668 
669 	arg = extract_expr(expr->getSubExpr());
670 
671 	if (expr->isIncrementDecrementOp() &&
672 	    pet_expr_get_type(arg) == pet_expr_access) {
673 		arg = mark_write(arg);
674 		arg = pet_expr_access_set_read(arg, 1);
675 	}
676 
677 	type_size = pet_clang_get_type_size(expr->getType(), ast_context);
678 	return pet_expr_new_unary(type_size, op, arg);
679 }
680 
681 /* Construct a pet_expr representing a binary operator expression.
682  *
683  * If the top level operator is an assignment and the LHS is an access,
684  * then we mark that access as a write.  If the operator is a compound
685  * assignment, the access is marked as both a read and a write.
686  */
extract_expr(BinaryOperator * expr)687 __isl_give pet_expr *PetScan::extract_expr(BinaryOperator *expr)
688 {
689 	int type_size;
690 	pet_expr *lhs, *rhs;
691 	enum pet_op_type op;
692 
693 	op = BinaryOperatorKind2pet_op_type(expr->getOpcode());
694 	if (op == pet_op_last) {
695 		report_unsupported_binary_operator(expr);
696 		return NULL;
697 	}
698 
699 	lhs = extract_expr(expr->getLHS());
700 	rhs = extract_expr(expr->getRHS());
701 
702 	if (expr->isAssignmentOp() &&
703 	    pet_expr_get_type(lhs) == pet_expr_access) {
704 		lhs = mark_write(lhs);
705 		if (expr->isCompoundAssignmentOp())
706 			lhs = pet_expr_access_set_read(lhs, 1);
707 	}
708 
709 	type_size = pet_clang_get_type_size(expr->getType(), ast_context);
710 	return pet_expr_new_binary(type_size, op, lhs, rhs);
711 }
712 
713 /* Construct a pet_tree for a variable declaration and
714  * add the declaration to the list of declarations
715  * inside the current compound statement.
716  */
extract(Decl * decl)717 __isl_give pet_tree *PetScan::extract(Decl *decl)
718 {
719 	VarDecl *vd;
720 	pet_expr *lhs, *rhs;
721 	pet_tree *tree;
722 
723 	if (!isa<VarDecl>(decl)) {
724 		report_unsupported_declaration(decl);
725 		return NULL;
726 	}
727 
728 	vd = cast<VarDecl>(decl);
729 	declarations.push_back(vd);
730 
731 	lhs = extract_access_expr(vd);
732 	lhs = mark_write(lhs);
733 	if (!vd->getInit())
734 		tree = pet_tree_new_decl(lhs);
735 	else {
736 		rhs = extract_expr(vd->getInit());
737 		tree = pet_tree_new_decl_init(lhs, rhs);
738 	}
739 
740 	return tree;
741 }
742 
743 /* Construct a pet_tree for a variable declaration statement.
744  * If the declaration statement declares multiple variables,
745  * then return a group of pet_trees, one for each declared variable.
746  */
extract(DeclStmt * stmt)747 __isl_give pet_tree *PetScan::extract(DeclStmt *stmt)
748 {
749 	pet_tree *tree;
750 	unsigned n;
751 
752 	if (!stmt->isSingleDecl()) {
753 		const DeclGroup &group = stmt->getDeclGroup().getDeclGroup();
754 		n = group.size();
755 		tree = pet_tree_new_block(ctx, 0, n);
756 
757 		for (unsigned i = 0; i < n; ++i) {
758 			pet_tree *tree_i;
759 			pet_loc *loc;
760 
761 			tree_i = extract(group[i]);
762 			loc = construct_pet_loc(group[i]->getSourceRange(),
763 						false);
764 			tree_i = pet_tree_set_loc(tree_i, loc);
765 			tree = pet_tree_block_add_child(tree, tree_i);
766 		}
767 
768 		return tree;
769 	}
770 
771 	return extract(stmt->getSingleDecl());
772 }
773 
774 /* Construct a pet_expr representing a conditional operation.
775  */
extract_expr(ConditionalOperator * expr)776 __isl_give pet_expr *PetScan::extract_expr(ConditionalOperator *expr)
777 {
778 	pet_expr *cond, *lhs, *rhs;
779 
780 	cond = extract_expr(expr->getCond());
781 	lhs = extract_expr(expr->getTrueExpr());
782 	rhs = extract_expr(expr->getFalseExpr());
783 
784 	return pet_expr_new_ternary(cond, lhs, rhs);
785 }
786 
extract_expr(ImplicitCastExpr * expr)787 __isl_give pet_expr *PetScan::extract_expr(ImplicitCastExpr *expr)
788 {
789 	return extract_expr(expr->getSubExpr());
790 }
791 
792 /* Construct a pet_expr representing a floating point value.
793  *
794  * If the floating point literal does not appear in a macro,
795  * then we use the original representation in the source code
796  * as the string representation.  Otherwise, we use the pretty
797  * printer to produce a string representation.
798  */
extract_expr(FloatingLiteral * expr)799 __isl_give pet_expr *PetScan::extract_expr(FloatingLiteral *expr)
800 {
801 	double d;
802 	string s;
803 	const LangOptions &LO = PP.getLangOpts();
804 	SourceLocation loc = expr->getLocation();
805 
806 	if (!loc.isMacroID()) {
807 		SourceManager &SM = PP.getSourceManager();
808 		unsigned len = Lexer::MeasureTokenLength(loc, SM, LO);
809 		s = string(SM.getCharacterData(loc), len);
810 	} else {
811 		llvm::raw_string_ostream S(s);
812 		expr->printPretty(S, 0, PrintingPolicy(LO));
813 		S.str();
814 	}
815 	d = expr->getValueAsApproximateDouble();
816 	return pet_expr_new_double(ctx, d, s.c_str());
817 }
818 
819 /* Extract an index expression from "expr" and then convert it into
820  * an access pet_expr.
821  *
822  * If "expr" is a reference to an enum constant, then return
823  * an integer expression instead, representing the value of the enum constant.
824  */
extract_access_expr(Expr * expr)825 __isl_give pet_expr *PetScan::extract_access_expr(Expr *expr)
826 {
827 	pet_expr *index;
828 
829 	index = extract_index_expr(expr);
830 
831 	if (pet_expr_get_type(index) == pet_expr_int)
832 		return index;
833 
834 	return pet_expr_access_from_index(expr->getType(), index, ast_context);
835 }
836 
837 /* Extract an index expression from "decl" and then convert it into
838  * an access pet_expr.
839  */
extract_access_expr(ValueDecl * decl)840 __isl_give pet_expr *PetScan::extract_access_expr(ValueDecl *decl)
841 {
842 	return pet_expr_access_from_index(decl->getType(),
843 					extract_index_expr(decl), ast_context);
844 }
845 
extract_expr(ParenExpr * expr)846 __isl_give pet_expr *PetScan::extract_expr(ParenExpr *expr)
847 {
848 	return extract_expr(expr->getSubExpr());
849 }
850 
851 /* Extract an assume statement from the argument "expr"
852  * of a __builtin_assume or __pencil_assume statement.
853  */
extract_assume(Expr * expr)854 __isl_give pet_expr *PetScan::extract_assume(Expr *expr)
855 {
856 	return pet_expr_new_unary(0, pet_op_assume, extract_expr(expr));
857 }
858 
859 /* If "expr" is an address-of operator, then return its argument.
860  * Otherwise, return NULL.
861  */
extract_addr_of_arg(Expr * expr)862 static Expr *extract_addr_of_arg(Expr *expr)
863 {
864 	UnaryOperator *op;
865 
866 	if (expr->getStmtClass() != Stmt::UnaryOperatorClass)
867 		return NULL;
868 	op = cast<UnaryOperator>(expr);
869 	if (op->getOpcode() != UO_AddrOf)
870 		return NULL;
871 	return op->getSubExpr();
872 }
873 
874 /* Construct a pet_expr corresponding to the function call argument "expr".
875  * The argument appears in position "pos" of a call to function "fd".
876  *
877  * If we are passing along a pointer to an array element
878  * or an entire row or even higher dimensional slice of an array,
879  * then the function being called may write into the array.
880  *
881  * We assume here that if the function is declared to take a pointer
882  * to a const type, then the function may only perform a read
883  * and that otherwise, it may either perform a read or a write (or both).
884  * We only perform this check if "detect_writes" is set.
885  */
extract_argument(FunctionDecl * fd,int pos,Expr * expr,bool detect_writes)886 __isl_give pet_expr *PetScan::extract_argument(FunctionDecl *fd, int pos,
887 	Expr *expr, bool detect_writes)
888 {
889 	Expr *arg;
890 	pet_expr *res;
891 	int is_addr = 0, is_partial = 0;
892 
893 	expr = pet_clang_strip_casts(expr);
894 	arg = extract_addr_of_arg(expr);
895 	if (arg) {
896 		is_addr = 1;
897 		expr = arg;
898 	}
899 	res = extract_expr(expr);
900 	if (!res)
901 		return NULL;
902 	if (pet_clang_array_depth(expr->getType()) > 0)
903 		is_partial = 1;
904 	if (detect_writes && (is_addr || is_partial) &&
905 	    pet_expr_get_type(res) == pet_expr_access) {
906 		ParmVarDecl *parm;
907 		if (!fd->hasPrototype()) {
908 			report_prototype_required(expr);
909 			return pet_expr_free(res);
910 		}
911 		parm = fd->getParamDecl(pos);
912 		if (!const_base(parm->getType()))
913 			res = mark_may_write(res);
914 	}
915 
916 	if (is_addr)
917 		res = pet_expr_new_unary(0, pet_op_address_of, res);
918 	return res;
919 }
920 
921 /* Find the first FunctionDecl with the given name.
922  * "call" is the corresponding call expression and is only used
923  * for reporting errors.
924  *
925  * Return NULL on error.
926  */
find_decl_from_name(CallExpr * call,string name)927 FunctionDecl *PetScan::find_decl_from_name(CallExpr *call, string name)
928 {
929 	TranslationUnitDecl *tu = ast_context.getTranslationUnitDecl();
930 	DeclContext::decl_iterator begin = tu->decls_begin();
931 	DeclContext::decl_iterator end = tu->decls_end();
932 	for (DeclContext::decl_iterator i = begin; i != end; ++i) {
933 		FunctionDecl *fd = dyn_cast<FunctionDecl>(*i);
934 		if (!fd)
935 			continue;
936 		if (fd->getName().str().compare(name) != 0)
937 			continue;
938 		if (fd->hasBody())
939 			return fd;
940 		report_missing_summary_function_body(call);
941 		return NULL;
942 	}
943 	report_missing_summary_function(call);
944 	return NULL;
945 }
946 
947 /* Return the FunctionDecl for the summary function associated to the
948  * function called by "call".
949  *
950  * In particular, if the pencil option is set, then
951  * search for an annotate attribute formatted as
952  * "pencil_access(name)", where "name" is the name of the summary function.
953  *
954  * If no summary function was specified, then return the FunctionDecl
955  * that is actually being called.
956  *
957  * Return NULL on error.
958  */
get_summary_function(CallExpr * call)959 FunctionDecl *PetScan::get_summary_function(CallExpr *call)
960 {
961 	FunctionDecl *decl = call->getDirectCallee();
962 	if (!decl)
963 		return NULL;
964 
965 	if (!options->pencil)
966 		return decl;
967 
968 	specific_attr_iterator<AnnotateAttr> begin, end, i;
969 	begin = decl->specific_attr_begin<AnnotateAttr>();
970 	end = decl->specific_attr_end<AnnotateAttr>();
971 	for (i = begin; i != end; ++i) {
972 		string attr = (*i)->getAnnotation().str();
973 
974 		const char prefix[] = "pencil_access(";
975 		size_t start = attr.find(prefix);
976 		if (start == string::npos)
977 			continue;
978 		start += strlen(prefix);
979 		string name = attr.substr(start, attr.find(')') - start);
980 
981 		return find_decl_from_name(call, name);
982 	}
983 
984 	return decl;
985 }
986 
987 /* Is "name" the name of an assume statement?
988  * "pencil" indicates whether pencil builtins and pragmas should be supported.
989  * "__builtin_assume" is always accepted.
990  * If "pencil" is set, then "__pencil_assume" is also accepted.
991  */
is_assume(int pencil,const string & name)992 static bool is_assume(int pencil, const string &name)
993 {
994 	if (name == "__builtin_assume")
995 		return true;
996 	return pencil && name == "__pencil_assume";
997 }
998 
999 /* Construct a pet_expr representing a function call.
1000  *
1001  * If this->call2id is not NULL and it contains a mapping for this call,
1002  * then this means that the corresponding function has been inlined.
1003  * Return a pet_expr that reads from the variable that
1004  * stores the return value of the inlined call.
1005  *
1006  * In the special case of a "call" to __builtin_assume or __pencil_assume,
1007  * construct an assume expression instead.
1008  *
1009  * In the case of a "call" to __pencil_kill, the arguments
1010  * are neither read nor written (only killed), so there
1011  * is no need to check for writes to these arguments.
1012  *
1013  * __pencil_assume and __pencil_kill are only recognized
1014  * when the pencil option is set.
1015  */
extract_expr(CallExpr * expr)1016 __isl_give pet_expr *PetScan::extract_expr(CallExpr *expr)
1017 {
1018 	pet_expr *res = NULL;
1019 	FunctionDecl *fd;
1020 	string name;
1021 	unsigned n_arg;
1022 	bool is_kill;
1023 
1024 	if (call2id && call2id->find(expr) != call2id->end())
1025 		return pet_expr_access_from_id(isl_id_copy(call2id[0][expr]),
1026 						ast_context);
1027 
1028 	fd = expr->getDirectCallee();
1029 	if (!fd) {
1030 		unsupported(expr);
1031 		return NULL;
1032 	}
1033 
1034 	name = fd->getDeclName().getAsString();
1035 	n_arg = expr->getNumArgs();
1036 
1037 	if (n_arg == 1 && is_assume(options->pencil, name))
1038 		return extract_assume(expr->getArg(0));
1039 	is_kill = options->pencil && name == "__pencil_kill";
1040 
1041 	res = pet_expr_new_call(ctx, name.c_str(), n_arg);
1042 	if (!res)
1043 		return NULL;
1044 
1045 	for (unsigned i = 0; i < n_arg; ++i) {
1046 		Expr *arg = expr->getArg(i);
1047 		res = pet_expr_set_arg(res, i,
1048 			    PetScan::extract_argument(fd, i, arg, !is_kill));
1049 	}
1050 
1051 	fd = get_summary_function(expr);
1052 	if (!fd)
1053 		return pet_expr_free(res);
1054 
1055 	res = set_summary(res, fd);
1056 
1057 	return res;
1058 }
1059 
1060 /* Construct a pet_expr representing a (C style) cast.
1061  */
extract_expr(CStyleCastExpr * expr)1062 __isl_give pet_expr *PetScan::extract_expr(CStyleCastExpr *expr)
1063 {
1064 	pet_expr *arg;
1065 	QualType type;
1066 
1067 	arg = extract_expr(expr->getSubExpr());
1068 	if (!arg)
1069 		return NULL;
1070 
1071 	type = expr->getTypeAsWritten();
1072 	return pet_expr_new_cast(type.getAsString().c_str(), arg);
1073 }
1074 
1075 /* Construct a pet_expr representing an integer.
1076  */
extract_expr(IntegerLiteral * expr)1077 __isl_give pet_expr *PetScan::extract_expr(IntegerLiteral *expr)
1078 {
1079 	return pet_expr_new_int(extract_int(expr));
1080 }
1081 
1082 /* Construct a pet_expr representing the integer enum constant "ecd".
1083  */
extract_expr(EnumConstantDecl * ecd)1084 __isl_give pet_expr *PetScan::extract_expr(EnumConstantDecl *ecd)
1085 {
1086 	isl_val *v;
1087 	const llvm::APSInt &init = ecd->getInitVal();
1088 	v = ::extract_int(ctx, init.isSigned(), init);
1089 	return pet_expr_new_int(v);
1090 }
1091 
1092 /* Try and construct a pet_expr representing "expr".
1093  */
extract_expr(Expr * expr)1094 __isl_give pet_expr *PetScan::extract_expr(Expr *expr)
1095 {
1096 	switch (expr->getStmtClass()) {
1097 	case Stmt::UnaryOperatorClass:
1098 		return extract_expr(cast<UnaryOperator>(expr));
1099 	case Stmt::CompoundAssignOperatorClass:
1100 	case Stmt::BinaryOperatorClass:
1101 		return extract_expr(cast<BinaryOperator>(expr));
1102 	case Stmt::ImplicitCastExprClass:
1103 		return extract_expr(cast<ImplicitCastExpr>(expr));
1104 	case Stmt::ArraySubscriptExprClass:
1105 	case Stmt::DeclRefExprClass:
1106 	case Stmt::MemberExprClass:
1107 		return extract_access_expr(expr);
1108 	case Stmt::IntegerLiteralClass:
1109 		return extract_expr(cast<IntegerLiteral>(expr));
1110 	case Stmt::FloatingLiteralClass:
1111 		return extract_expr(cast<FloatingLiteral>(expr));
1112 	case Stmt::ParenExprClass:
1113 		return extract_expr(cast<ParenExpr>(expr));
1114 	case Stmt::ConditionalOperatorClass:
1115 		return extract_expr(cast<ConditionalOperator>(expr));
1116 	case Stmt::CallExprClass:
1117 		return extract_expr(cast<CallExpr>(expr));
1118 	case Stmt::CStyleCastExprClass:
1119 		return extract_expr(cast<CStyleCastExpr>(expr));
1120 	default:
1121 		unsupported(expr);
1122 	}
1123 	return NULL;
1124 }
1125 
1126 /* Check if the given initialization statement is an assignment.
1127  * If so, return that assignment.  Otherwise return NULL.
1128  */
initialization_assignment(Stmt * init)1129 BinaryOperator *PetScan::initialization_assignment(Stmt *init)
1130 {
1131 	BinaryOperator *ass;
1132 
1133 	if (init->getStmtClass() != Stmt::BinaryOperatorClass)
1134 		return NULL;
1135 
1136 	ass = cast<BinaryOperator>(init);
1137 	if (ass->getOpcode() != BO_Assign)
1138 		return NULL;
1139 
1140 	return ass;
1141 }
1142 
1143 /* Check if the given initialization statement is a declaration
1144  * of a single variable.
1145  * If so, return that declaration.  Otherwise return NULL.
1146  */
initialization_declaration(Stmt * init)1147 Decl *PetScan::initialization_declaration(Stmt *init)
1148 {
1149 	DeclStmt *decl;
1150 
1151 	if (init->getStmtClass() != Stmt::DeclStmtClass)
1152 		return NULL;
1153 
1154 	decl = cast<DeclStmt>(init);
1155 
1156 	if (!decl->isSingleDecl())
1157 		return NULL;
1158 
1159 	return decl->getSingleDecl();
1160 }
1161 
1162 /* Given the assignment operator in the initialization of a for loop,
1163  * extract the induction variable, i.e., the (integer)variable being
1164  * assigned.
1165  */
extract_induction_variable(BinaryOperator * init)1166 ValueDecl *PetScan::extract_induction_variable(BinaryOperator *init)
1167 {
1168 	Expr *lhs;
1169 	DeclRefExpr *ref;
1170 	ValueDecl *decl;
1171 	const Type *type;
1172 
1173 	lhs = init->getLHS();
1174 	if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1175 		unsupported(init);
1176 		return NULL;
1177 	}
1178 
1179 	ref = cast<DeclRefExpr>(lhs);
1180 	decl = ref->getDecl();
1181 	type = decl->getType().getTypePtr();
1182 
1183 	if (!type->isIntegerType()) {
1184 		unsupported(lhs);
1185 		return NULL;
1186 	}
1187 
1188 	return decl;
1189 }
1190 
1191 /* Given the initialization statement of a for loop and the single
1192  * declaration in this initialization statement,
1193  * extract the induction variable, i.e., the (integer) variable being
1194  * declared.
1195  */
extract_induction_variable(Stmt * init,Decl * decl)1196 VarDecl *PetScan::extract_induction_variable(Stmt *init, Decl *decl)
1197 {
1198 	VarDecl *vd;
1199 
1200 	vd = cast<VarDecl>(decl);
1201 
1202 	const QualType type = vd->getType();
1203 	if (!type->isIntegerType()) {
1204 		unsupported(init);
1205 		return NULL;
1206 	}
1207 
1208 	if (!vd->getInit()) {
1209 		unsupported(init);
1210 		return NULL;
1211 	}
1212 
1213 	return vd;
1214 }
1215 
1216 /* Check that op is of the form iv++ or iv--.
1217  * Return a pet_expr representing "1" or "-1" accordingly.
1218  */
extract_unary_increment(clang::UnaryOperator * op,clang::ValueDecl * iv)1219 __isl_give pet_expr *PetScan::extract_unary_increment(
1220 	clang::UnaryOperator *op, clang::ValueDecl *iv)
1221 {
1222 	Expr *sub;
1223 	DeclRefExpr *ref;
1224 	isl_val *v;
1225 
1226 	if (!op->isIncrementDecrementOp()) {
1227 		unsupported(op);
1228 		return NULL;
1229 	}
1230 
1231 	sub = op->getSubExpr();
1232 	if (sub->getStmtClass() != Stmt::DeclRefExprClass) {
1233 		unsupported(op);
1234 		return NULL;
1235 	}
1236 
1237 	ref = cast<DeclRefExpr>(sub);
1238 	if (ref->getDecl() != iv) {
1239 		unsupported(op);
1240 		return NULL;
1241 	}
1242 
1243 	if (op->isIncrementOp())
1244 		v = isl_val_one(ctx);
1245 	else
1246 		v = isl_val_negone(ctx);
1247 
1248 	return pet_expr_new_int(v);
1249 }
1250 
1251 /* Check if op is of the form
1252  *
1253  *	iv = expr
1254  *
1255  * and return the increment "expr - iv" as a pet_expr.
1256  */
extract_binary_increment(BinaryOperator * op,clang::ValueDecl * iv)1257 __isl_give pet_expr *PetScan::extract_binary_increment(BinaryOperator *op,
1258 	clang::ValueDecl *iv)
1259 {
1260 	int type_size;
1261 	Expr *lhs;
1262 	DeclRefExpr *ref;
1263 	pet_expr *expr, *expr_iv;
1264 
1265 	if (op->getOpcode() != BO_Assign) {
1266 		unsupported(op);
1267 		return NULL;
1268 	}
1269 
1270 	lhs = op->getLHS();
1271 	if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1272 		unsupported(op);
1273 		return NULL;
1274 	}
1275 
1276 	ref = cast<DeclRefExpr>(lhs);
1277 	if (ref->getDecl() != iv) {
1278 		unsupported(op);
1279 		return NULL;
1280 	}
1281 
1282 	expr = extract_expr(op->getRHS());
1283 	expr_iv = extract_expr(lhs);
1284 
1285 	type_size = pet_clang_get_type_size(iv->getType(), ast_context);
1286 	return pet_expr_new_binary(type_size, pet_op_sub, expr, expr_iv);
1287 }
1288 
1289 /* Check that op is of the form iv += cst or iv -= cst
1290  * and return a pet_expr corresponding to cst or -cst accordingly.
1291  */
extract_compound_increment(CompoundAssignOperator * op,clang::ValueDecl * iv)1292 __isl_give pet_expr *PetScan::extract_compound_increment(
1293 	CompoundAssignOperator *op, clang::ValueDecl *iv)
1294 {
1295 	Expr *lhs;
1296 	DeclRefExpr *ref;
1297 	bool neg = false;
1298 	pet_expr *expr;
1299 	BinaryOperatorKind opcode;
1300 
1301 	opcode = op->getOpcode();
1302 	if (opcode != BO_AddAssign && opcode != BO_SubAssign) {
1303 		unsupported(op);
1304 		return NULL;
1305 	}
1306 	if (opcode == BO_SubAssign)
1307 		neg = true;
1308 
1309 	lhs = op->getLHS();
1310 	if (lhs->getStmtClass() != Stmt::DeclRefExprClass) {
1311 		unsupported(op);
1312 		return NULL;
1313 	}
1314 
1315 	ref = cast<DeclRefExpr>(lhs);
1316 	if (ref->getDecl() != iv) {
1317 		unsupported(op);
1318 		return NULL;
1319 	}
1320 
1321 	expr = extract_expr(op->getRHS());
1322 	if (neg) {
1323 		int type_size;
1324 		type_size = pet_clang_get_type_size(op->getType(), ast_context);
1325 		expr = pet_expr_new_unary(type_size, pet_op_minus, expr);
1326 	}
1327 
1328 	return expr;
1329 }
1330 
1331 /* Check that the increment of the given for loop increments
1332  * (or decrements) the induction variable "iv" and return
1333  * the increment as a pet_expr if successful.
1334  */
extract_increment(clang::ForStmt * stmt,ValueDecl * iv)1335 __isl_give pet_expr *PetScan::extract_increment(clang::ForStmt *stmt,
1336 	ValueDecl *iv)
1337 {
1338 	Stmt *inc = stmt->getInc();
1339 
1340 	if (!inc) {
1341 		report_missing_increment(stmt);
1342 		return NULL;
1343 	}
1344 
1345 	if (inc->getStmtClass() == Stmt::UnaryOperatorClass)
1346 		return extract_unary_increment(cast<UnaryOperator>(inc), iv);
1347 	if (inc->getStmtClass() == Stmt::CompoundAssignOperatorClass)
1348 		return extract_compound_increment(
1349 				cast<CompoundAssignOperator>(inc), iv);
1350 	if (inc->getStmtClass() == Stmt::BinaryOperatorClass)
1351 		return extract_binary_increment(cast<BinaryOperator>(inc), iv);
1352 
1353 	unsupported(inc);
1354 	return NULL;
1355 }
1356 
1357 /* Construct a pet_tree for a while loop.
1358  *
1359  * If we were only able to extract part of the body, then simply
1360  * return that part.
1361  */
extract(WhileStmt * stmt)1362 __isl_give pet_tree *PetScan::extract(WhileStmt *stmt)
1363 {
1364 	pet_expr *pe_cond;
1365 	pet_tree *tree;
1366 
1367 	tree = extract(stmt->getBody());
1368 	if (partial)
1369 		return tree;
1370 	pe_cond = extract_expr(stmt->getCond());
1371 	tree = pet_tree_new_while(pe_cond, tree);
1372 
1373 	return tree;
1374 }
1375 
1376 /* Construct a pet_tree for a for statement.
1377  * The for loop is required to be of one of the following forms
1378  *
1379  *	for (i = init; condition; ++i)
1380  *	for (i = init; condition; --i)
1381  *	for (i = init; condition; i += constant)
1382  *	for (i = init; condition; i -= constant)
1383  *
1384  * We extract a pet_tree for the body and then include it in a pet_tree
1385  * of type pet_tree_for.
1386  *
1387  * As a special case, we also allow a for loop of the form
1388  *
1389  *	for (;;)
1390  *
1391  * in which case we return a pet_tree of type pet_tree_infinite_loop.
1392  *
1393  * If we were only able to extract part of the body, then simply
1394  * return that part.
1395  */
extract_for(ForStmt * stmt)1396 __isl_give pet_tree *PetScan::extract_for(ForStmt *stmt)
1397 {
1398 	BinaryOperator *ass;
1399 	Decl *decl;
1400 	Stmt *init;
1401 	Expr *rhs;
1402 	ValueDecl *iv;
1403 	pet_tree *tree;
1404 	int independent;
1405 	int declared;
1406 	pet_expr *pe_init, *pe_inc, *pe_iv, *pe_cond;
1407 
1408 	independent = is_current_stmt_marked_independent();
1409 
1410 	if (!stmt->getInit() && !stmt->getCond() && !stmt->getInc()) {
1411 		tree = extract(stmt->getBody());
1412 		if (partial)
1413 			return tree;
1414 		tree = pet_tree_new_infinite_loop(tree);
1415 		return tree;
1416 	}
1417 
1418 	init = stmt->getInit();
1419 	if (!init) {
1420 		unsupported(stmt);
1421 		return NULL;
1422 	}
1423 	if ((ass = initialization_assignment(init)) != NULL) {
1424 		iv = extract_induction_variable(ass);
1425 		if (!iv)
1426 			return NULL;
1427 		rhs = ass->getRHS();
1428 	} else if ((decl = initialization_declaration(init)) != NULL) {
1429 		VarDecl *var = extract_induction_variable(init, decl);
1430 		if (!var)
1431 			return NULL;
1432 		iv = var;
1433 		rhs = var->getInit();
1434 	} else {
1435 		unsupported(stmt->getInit());
1436 		return NULL;
1437 	}
1438 
1439 	declared = !initialization_assignment(stmt->getInit());
1440 	tree = extract(stmt->getBody());
1441 	if (partial)
1442 		return tree;
1443 	pe_iv = extract_access_expr(iv);
1444 	pe_iv = mark_write(pe_iv);
1445 	pe_init = extract_expr(rhs);
1446 	if (!stmt->getCond())
1447 		pe_cond = pet_expr_new_int(isl_val_one(ctx));
1448 	else
1449 		pe_cond = extract_expr(stmt->getCond());
1450 	pe_inc = extract_increment(stmt, iv);
1451 	tree = pet_tree_new_for(independent, declared, pe_iv, pe_init, pe_cond,
1452 				pe_inc, tree);
1453 	return tree;
1454 }
1455 
1456 /* Store the names of the variables declared in decl_context
1457  * in the set declared_names.  Make sure to only do this once by
1458  * setting declared_names_collected.
1459  */
collect_declared_names()1460 void PetScan::collect_declared_names()
1461 {
1462 	DeclContext *DC = decl_context;
1463 	DeclContext::decl_iterator it;
1464 
1465 	if (declared_names_collected)
1466 		return;
1467 
1468 	for (it = DC->decls_begin(); it != DC->decls_end(); ++it) {
1469 		Decl *D = *it;
1470 		NamedDecl *named;
1471 
1472 		if (!isa<NamedDecl>(D))
1473 			continue;
1474 		named = cast<NamedDecl>(D);
1475 		declared_names.insert(named->getName().str());
1476 	}
1477 
1478 	declared_names_collected = true;
1479 }
1480 
1481 /* Add the names in "names" that are not also in this->declared_names
1482  * to this->used_names.
1483  * It is up to the caller to make sure that declared_names has been
1484  * populated, if needed.
1485  */
add_new_used_names(const std::set<std::string> & names)1486 void PetScan::add_new_used_names(const std::set<std::string> &names)
1487 {
1488 	std::set<std::string>::const_iterator it;
1489 
1490 	for (it = names.begin(); it != names.end(); ++it) {
1491 		if (declared_names.find(*it) != declared_names.end())
1492 			continue;
1493 		used_names.insert(*it);
1494 	}
1495 }
1496 
1497 /* Is the name "name" used in any declaration other than "decl"?
1498  *
1499  * If the name was found to be in use before, the consider it to be in use.
1500  * Otherwise, check the DeclContext of the function containing the scop
1501  * as well as all ancestors of this DeclContext for declarations
1502  * other than "decl" that declare something called "name".
1503  */
name_in_use(const string & name,Decl * decl)1504 bool PetScan::name_in_use(const string &name, Decl *decl)
1505 {
1506 	DeclContext *DC;
1507 	DeclContext::decl_iterator it;
1508 
1509 	if (used_names.find(name) != used_names.end())
1510 		return true;
1511 
1512 	for (DC = decl_context; DC; DC = DC->getParent()) {
1513 		for (it = DC->decls_begin(); it != DC->decls_end(); ++it) {
1514 			Decl *D = *it;
1515 			NamedDecl *named;
1516 
1517 			if (D == decl)
1518 				continue;
1519 			if (!isa<NamedDecl>(D))
1520 				continue;
1521 			named = cast<NamedDecl>(D);
1522 			if (named->getName().str() == name)
1523 				return true;
1524 		}
1525 	}
1526 
1527 	return false;
1528 }
1529 
1530 /* Generate a new name based on "name" that is not in use.
1531  * Do so by adding a suffix _i, with i an integer.
1532  */
generate_new_name(const string & name)1533 string PetScan::generate_new_name(const string &name)
1534 {
1535 	string new_name;
1536 
1537 	do {
1538 		std::ostringstream oss;
1539 		oss << name << "_" << n_rename++;
1540 		new_name = oss.str();
1541 	} while (name_in_use(new_name, NULL));
1542 
1543 	return new_name;
1544 }
1545 
1546 /* Try and construct a pet_tree corresponding to a compound statement.
1547  *
1548  * "skip_declarations" is set if we should skip initial declarations
1549  * in the children of the compound statements.
1550  *
1551  * Collect a new set of declarations for the current compound statement.
1552  * If any of the names in these declarations is also used by another
1553  * declaration reachable from the current function, then rename it
1554  * to a name that is not already in use.
1555  * In particular, keep track of the old and new names in a pet_substituter
1556  * and apply the substitutions to the pet_tree corresponding to the
1557  * compound statement.
1558  */
extract(CompoundStmt * stmt,bool skip_declarations)1559 __isl_give pet_tree *PetScan::extract(CompoundStmt *stmt,
1560 	bool skip_declarations)
1561 {
1562 	pet_tree *tree;
1563 	std::vector<VarDecl *> saved_declarations;
1564 	std::vector<VarDecl *>::iterator it;
1565 	pet_substituter substituter;
1566 
1567 	saved_declarations = declarations;
1568 	declarations.clear();
1569 	tree = extract(stmt->children(), true, skip_declarations, stmt);
1570 	for (it = declarations.begin(); it != declarations.end(); ++it) {
1571 		isl_id *id;
1572 		pet_expr *expr;
1573 		VarDecl *decl = *it;
1574 		string name = decl->getName().str();
1575 		bool in_use = name_in_use(name, decl);
1576 
1577 		used_names.insert(name);
1578 		if (!in_use)
1579 			continue;
1580 
1581 		name = generate_new_name(name);
1582 		id = pet_id_from_name_and_decl(ctx, name.c_str(), decl);
1583 		expr = pet_expr_access_from_id(id, ast_context);
1584 		id = pet_id_from_decl(ctx, decl);
1585 		substituter.add_sub(id, expr);
1586 		used_names.insert(name);
1587 	}
1588 	tree = substituter.substitute(tree);
1589 	declarations = saved_declarations;
1590 
1591 	return tree;
1592 }
1593 
1594 /* Return the file offset of the expansion location of "Loc".
1595  */
getExpansionOffset(SourceManager & SM,SourceLocation Loc)1596 static unsigned getExpansionOffset(SourceManager &SM, SourceLocation Loc)
1597 {
1598 	return SM.getFileOffset(SM.getExpansionLoc(Loc));
1599 }
1600 
1601 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1602 
1603 /* Return a SourceLocation for the location after the first semicolon
1604  * after "loc".  If Lexer::findLocationAfterToken is available, we simply
1605  * call it and also skip trailing spaces and newline.
1606  */
location_after_semi(SourceLocation loc,SourceManager & SM,const LangOptions & LO)1607 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
1608 	const LangOptions &LO)
1609 {
1610 	return Lexer::findLocationAfterToken(loc, tok::semi, SM, LO, true);
1611 }
1612 
1613 #else
1614 
1615 /* Return a SourceLocation for the location after the first semicolon
1616  * after "loc".  If Lexer::findLocationAfterToken is not available,
1617  * we look in the underlying character data for the first semicolon.
1618  */
location_after_semi(SourceLocation loc,SourceManager & SM,const LangOptions & LO)1619 static SourceLocation location_after_semi(SourceLocation loc, SourceManager &SM,
1620 	const LangOptions &LO)
1621 {
1622 	const char *semi;
1623 	const char *s = SM.getCharacterData(loc);
1624 
1625 	semi = strchr(s, ';');
1626 	if (!semi)
1627 		return SourceLocation();
1628 	return loc.getFileLocWithOffset(semi + 1 - s);
1629 }
1630 
1631 #endif
1632 
1633 /* If the token at "loc" is the first token on the line, then return
1634  * a location referring to the start of the line and set *indent
1635  * to the indentation of "loc"
1636  * Otherwise, return "loc" and set *indent to "".
1637  *
1638  * This function is used to extend a scop to the start of the line
1639  * if the first token of the scop is also the first token on the line.
1640  *
1641  * We look for the first token on the line.  If its location is equal to "loc",
1642  * then the latter is the location of the first token on the line.
1643  */
move_to_start_of_line_if_first_token(SourceLocation loc,SourceManager & SM,const LangOptions & LO,char ** indent)1644 static SourceLocation move_to_start_of_line_if_first_token(SourceLocation loc,
1645 	SourceManager &SM, const LangOptions &LO, char **indent)
1646 {
1647 	std::pair<FileID, unsigned> file_offset_pair;
1648 	llvm::StringRef file;
1649 	const char *pos;
1650 	Token tok;
1651 	SourceLocation token_loc, line_loc;
1652 	int col;
1653 	const char *s;
1654 
1655 	loc = SM.getExpansionLoc(loc);
1656 	col = SM.getExpansionColumnNumber(loc);
1657 	line_loc = loc.getLocWithOffset(1 - col);
1658 	file_offset_pair = SM.getDecomposedLoc(line_loc);
1659 	file = SM.getBufferData(file_offset_pair.first, NULL);
1660 	pos = file.data() + file_offset_pair.second;
1661 
1662 	Lexer lexer(SM.getLocForStartOfFile(file_offset_pair.first), LO,
1663 					file.begin(), pos, file.end());
1664 	lexer.LexFromRawLexer(tok);
1665 	token_loc = tok.getLocation();
1666 
1667 	s = SM.getCharacterData(line_loc);
1668 	*indent = strndup(s, token_loc == loc ? col - 1 : 0);
1669 
1670 	if (token_loc == loc)
1671 		return line_loc;
1672 	else
1673 		return loc;
1674 }
1675 
1676 /* Construct a pet_loc corresponding to the region covered by "range".
1677  * If "skip_semi" is set, then we assume "range" is followed by
1678  * a semicolon and also include this semicolon.
1679  */
construct_pet_loc(SourceRange range,bool skip_semi)1680 __isl_give pet_loc *PetScan::construct_pet_loc(SourceRange range,
1681 	bool skip_semi)
1682 {
1683 	SourceLocation loc = range.getBegin();
1684 	SourceManager &SM = PP.getSourceManager();
1685 	const LangOptions &LO = PP.getLangOpts();
1686 	int line = PP.getSourceManager().getExpansionLineNumber(loc);
1687 	unsigned start, end;
1688 	char *indent;
1689 
1690 	loc = move_to_start_of_line_if_first_token(loc, SM, LO, &indent);
1691 	start = getExpansionOffset(SM, loc);
1692 	loc = range.getEnd();
1693 	if (skip_semi)
1694 		loc = location_after_semi(loc, SM, LO);
1695 	else
1696 		loc = PP.getLocForEndOfToken(loc);
1697 	end = getExpansionOffset(SM, loc);
1698 
1699 	return pet_loc_alloc(ctx, start, end, line, indent);
1700 }
1701 
1702 /* Convert a top-level pet_expr to an expression pet_tree.
1703  */
extract(__isl_take pet_expr * expr,SourceRange range,bool skip_semi)1704 __isl_give pet_tree *PetScan::extract(__isl_take pet_expr *expr,
1705 	SourceRange range, bool skip_semi)
1706 {
1707 	pet_loc *loc;
1708 	pet_tree *tree;
1709 
1710 	tree = pet_tree_new_expr(expr);
1711 	loc = construct_pet_loc(range, skip_semi);
1712 	tree = pet_tree_set_loc(tree, loc);
1713 
1714 	return tree;
1715 }
1716 
1717 /* Construct a pet_tree for an if statement.
1718  */
extract(IfStmt * stmt)1719 __isl_give pet_tree *PetScan::extract(IfStmt *stmt)
1720 {
1721 	pet_expr *pe_cond;
1722 	pet_tree *tree, *tree_else;
1723 
1724 	pe_cond = extract_expr(stmt->getCond());
1725 	tree = extract(stmt->getThen());
1726 	if (stmt->getElse()) {
1727 		tree_else = extract(stmt->getElse());
1728 		if (options->autodetect) {
1729 			if (tree && !tree_else) {
1730 				partial = true;
1731 				pet_expr_free(pe_cond);
1732 				return tree;
1733 			}
1734 			if (!tree && tree_else) {
1735 				partial = true;
1736 				pet_expr_free(pe_cond);
1737 				return tree_else;
1738 			}
1739 		}
1740 		tree = pet_tree_new_if_else(pe_cond, tree, tree_else);
1741 	} else
1742 		tree = pet_tree_new_if(pe_cond, tree);
1743 	return tree;
1744 }
1745 
1746 /* Is "parent" a compound statement that has "stmt" as its final child?
1747  */
final_in_compound(ReturnStmt * stmt,Stmt * parent)1748 static bool final_in_compound(ReturnStmt *stmt, Stmt *parent)
1749 {
1750 	CompoundStmt *c;
1751 
1752 	c = dyn_cast<CompoundStmt>(parent);
1753 	if (c) {
1754 		StmtIterator i;
1755 		Stmt *last;
1756 		StmtRange range = c->children();
1757 
1758 		for (i = range.first; i != range.second; ++i)
1759 			last = *i;
1760 		return last == stmt;
1761 	}
1762 	return false;
1763 }
1764 
1765 /* Try and construct a pet_tree for a return statement "stmt".
1766  *
1767  * Return statements are only allowed in a context where
1768  * this->return_root has been set.
1769  * Furthermore, "stmt" should appear as the last child
1770  * in the compound statement this->return_root.
1771  */
extract(ReturnStmt * stmt)1772 __isl_give pet_tree *PetScan::extract(ReturnStmt *stmt)
1773 {
1774 	pet_expr *val;
1775 
1776 	if (!return_root) {
1777 		report_unsupported_return(stmt);
1778 		return NULL;
1779 	}
1780 	if (!final_in_compound(stmt, return_root)) {
1781 		report_return_not_at_end_of_function(stmt);
1782 		return NULL;
1783 	}
1784 
1785 	val = extract_expr(stmt->getRetValue());
1786 	return pet_tree_new_return(val);
1787 }
1788 
1789 /* Try and construct a pet_tree for a label statement.
1790  */
extract(LabelStmt * stmt)1791 __isl_give pet_tree *PetScan::extract(LabelStmt *stmt)
1792 {
1793 	isl_id *label;
1794 	pet_tree *tree;
1795 
1796 	label = isl_id_alloc(ctx, stmt->getName(), NULL);
1797 
1798 	tree = extract(stmt->getSubStmt());
1799 	tree = pet_tree_set_label(tree, label);
1800 	return tree;
1801 }
1802 
1803 /* Update the location of "tree" to include the source range of "stmt".
1804  *
1805  * Actually, we create a new location based on the source range of "stmt" and
1806  * then extend this new location to include the region of the original location.
1807  * This ensures that the line number of the final location refers to "stmt".
1808  */
update_loc(__isl_take pet_tree * tree,Stmt * stmt)1809 __isl_give pet_tree *PetScan::update_loc(__isl_take pet_tree *tree, Stmt *stmt)
1810 {
1811 	pet_loc *loc, *tree_loc;
1812 
1813 	tree_loc = pet_tree_get_loc(tree);
1814 	loc = construct_pet_loc(stmt->getSourceRange(), false);
1815 	loc = pet_loc_update_start_end_from_loc(loc, tree_loc);
1816 	pet_loc_free(tree_loc);
1817 
1818 	tree = pet_tree_set_loc(tree, loc);
1819 	return tree;
1820 }
1821 
1822 /* Is "expr" of a type that can be converted to an access expression?
1823  */
is_access_expr_type(Expr * expr)1824 static bool is_access_expr_type(Expr *expr)
1825 {
1826 	switch (expr->getStmtClass()) {
1827 	case Stmt::ArraySubscriptExprClass:
1828 	case Stmt::DeclRefExprClass:
1829 	case Stmt::MemberExprClass:
1830 		return true;
1831 	default:
1832 		return false;
1833 	}
1834 }
1835 
1836 /* Tell the pet_inliner "inliner" about the formal arguments
1837  * in "fd" and the corresponding actual arguments in "call".
1838  * Return 0 if this was successful and -1 otherwise.
1839  *
1840  * Any pointer argument is treated as an array.
1841  * The other arguments are treated as scalars.
1842  *
1843  * In case of scalars, there is no restriction on the actual argument.
1844  * This actual argument is assigned to a variable with a name
1845  * that is derived from the name of the corresponding formal argument,
1846  * but made not to conflict with any variable names that are
1847  * already in use.
1848  *
1849  * In case of arrays, the actual argument needs to be an expression
1850  * of a type that can be converted to an access expression or the address
1851  * of such an expression, ignoring implicit and redundant casts.
1852  */
set_inliner_arguments(pet_inliner & inliner,CallExpr * call,FunctionDecl * fd)1853 int PetScan::set_inliner_arguments(pet_inliner &inliner, CallExpr *call,
1854 	FunctionDecl *fd)
1855 {
1856 	unsigned n;
1857 
1858 	n = fd->getNumParams();
1859 	for (unsigned i = 0; i < n; ++i) {
1860 		ParmVarDecl *parm = fd->getParamDecl(i);
1861 		QualType type = parm->getType();
1862 		Expr *arg, *sub;
1863 		pet_expr *expr;
1864 		int is_addr = 0;
1865 
1866 		arg = call->getArg(i);
1867 		if (pet_clang_array_depth(type) == 0) {
1868 			string name = parm->getName().str();
1869 			if (name_in_use(name, NULL))
1870 				name = generate_new_name(name);
1871 			used_names.insert(name);
1872 			inliner.add_scalar_arg(parm, name, extract_expr(arg));
1873 			continue;
1874 		}
1875 		arg = pet_clang_strip_casts(arg);
1876 		sub = extract_addr_of_arg(arg);
1877 		if (sub) {
1878 			is_addr = 1;
1879 			arg = pet_clang_strip_casts(sub);
1880 		}
1881 		if (!is_access_expr_type(arg)) {
1882 			report_unsupported_inline_function_argument(arg);
1883 			return -1;
1884 		}
1885 		expr = extract_access_expr(arg);
1886 		if (!expr)
1887 			return -1;
1888 		inliner.add_array_arg(parm, expr, is_addr);
1889 	}
1890 
1891 	return 0;
1892 }
1893 
1894 /* Internal data structure for PetScan::substitute_array_sizes.
1895  * ps is the PetScan on which the method was called.
1896  * substituter is the substituter that is used to substitute variables
1897  * in the size expressions.
1898  */
1899 struct pet_substitute_array_sizes_data {
1900 	PetScan *ps;
1901 	pet_substituter *substituter;
1902 };
1903 
1904 extern "C" {
1905 	static int substitute_array_size(__isl_keep pet_tree *tree, void *user);
1906 }
1907 
1908 /* If "tree" is a declaration, then perform the substitutions
1909  * in data->substituter on its size expression and store the result
1910  * in the size expression cache of data->ps such that the modified expression
1911  * will be used in subsequent calls to get_array_size.
1912  */
substitute_array_size(__isl_keep pet_tree * tree,void * user)1913 static int substitute_array_size(__isl_keep pet_tree *tree, void *user)
1914 {
1915 	struct pet_substitute_array_sizes_data *data;
1916 	isl_id *id;
1917 	pet_expr *var, *size;
1918 
1919 	if (!pet_tree_is_decl(tree))
1920 		return 0;
1921 
1922 	data = (struct pet_substitute_array_sizes_data *) user;
1923 	var = pet_tree_decl_get_var(tree);
1924 	id = pet_expr_access_get_id(var);
1925 	pet_expr_free(var);
1926 
1927 	size = data->ps->get_array_size(id);
1928 	size = data->substituter->substitute(size);
1929 	data->ps->set_array_size(id, size);
1930 
1931 	return 0;
1932 }
1933 
1934 /* Perform the substitutions in "substituter" on all the arrays declared
1935  * inside "tree" and store the results in the size expression cache
1936  * such that the modified expressions will be used in subsequent calls
1937  * to get_array_size.
1938  */
substitute_array_sizes(__isl_keep pet_tree * tree,pet_substituter * substituter)1939 int PetScan::substitute_array_sizes(__isl_keep pet_tree *tree,
1940 	pet_substituter *substituter)
1941 {
1942 	struct pet_substitute_array_sizes_data data = { this, substituter };
1943 
1944 	return pet_tree_foreach_sub_tree(tree, &substitute_array_size, &data);
1945 }
1946 
1947 /* Try and construct a pet_tree from the body of "fd" using the actual
1948  * arguments in "call" in place of the formal arguments.
1949  * "fd" is assumed to point to the declaration with a function body.
1950  * In particular, construct a block that consists of assignments
1951  * of (parts of) the actual arguments to temporary variables
1952  * followed by the inlined function body with the formal arguments
1953  * replaced by (expressions containing) these temporary variables.
1954  * If "return_id" is set, then it is used to store the return value
1955  * of the inlined function.
1956  *
1957  * The actual inlining is taken care of by the pet_inliner object.
1958  * This function merely calls set_inliner_arguments to tell
1959  * the pet_inliner about the actual arguments, extracts a pet_tree
1960  * from the body of the called function and then passes this pet_tree
1961  * to the pet_inliner.
1962  * The body of the called function is allowed to have a return statement
1963  * at the end.
1964  * The substitutions performed by the inliner are also applied
1965  * to the size expressions of the arrays declared in the inlined
1966  * function.  These size expressions are not stored in the tree
1967  * itself, but rather in the size expression cache.
1968  *
1969  * During the extraction of the function body, all variables names
1970  * that are declared in the calling function as well all variable
1971  * names that are known to be in use are considered to be in use
1972  * in the called function to ensure that there is no naming conflict.
1973  * Similarly, the additional names that are in use in the called function
1974  * are considered to be in use in the calling function as well.
1975  *
1976  * The location of the pet_tree is reset to the call site to ensure
1977  * that the extent of the scop does not include the body of the called
1978  * function.
1979  */
extract_inlined_call(CallExpr * call,FunctionDecl * fd,__isl_keep isl_id * return_id)1980 __isl_give pet_tree *PetScan::extract_inlined_call(CallExpr *call,
1981 	FunctionDecl *fd, __isl_keep isl_id *return_id)
1982 {
1983 	int save_autodetect;
1984 	pet_tree *tree;
1985 	pet_loc *tree_loc;
1986 	pet_inliner inliner(ctx, n_arg, ast_context);
1987 
1988 	if (set_inliner_arguments(inliner, call, fd) < 0)
1989 		return NULL;
1990 
1991 	save_autodetect = options->autodetect;
1992 	options->autodetect = 0;
1993 	PetScan body_scan(PP, ast_context, fd, loc, options,
1994 				isl_union_map_copy(value_bounds), independent);
1995 	collect_declared_names();
1996 	body_scan.add_new_used_names(declared_names);
1997 	body_scan.add_new_used_names(used_names);
1998 	body_scan.return_root = fd->getBody();
1999 	tree = body_scan.extract(fd->getBody(), false);
2000 	add_new_used_names(body_scan.used_names);
2001 	options->autodetect = save_autodetect;
2002 
2003 	tree_loc = construct_pet_loc(call->getSourceRange(), true);
2004 	tree = pet_tree_set_loc(tree, tree_loc);
2005 
2006 	substitute_array_sizes(tree, &inliner);
2007 
2008 	return inliner.inline_tree(tree, return_id);
2009 }
2010 
2011 /* Try and construct a pet_tree corresponding
2012  * to the expression statement "stmt".
2013  *
2014  * First look for function calls that have corresponding bodies
2015  * marked "inline".  Extract the inlined functions in a pet_inlined_calls
2016  * object.  Then extract the statement itself, replacing calls
2017  * to inlined function by accesses to the corresponding return variables, and
2018  * return the combined result.
2019  * If the outer expression is itself a call to an inlined function,
2020  * then it already appears as one of the inlined functions and
2021  * no separate pet_tree needs to be extracted for "stmt" itself.
2022  */
extract_expr_stmt(Stmt * stmt)2023 __isl_give pet_tree *PetScan::extract_expr_stmt(Stmt *stmt)
2024 {
2025 	pet_expr *expr;
2026 	pet_tree *tree;
2027 	pet_inlined_calls ic(this);
2028 
2029 	ic.collect(stmt);
2030 	if (ic.calls.size() >= 1 && ic.calls[0] == stmt) {
2031 		tree = pet_tree_new_block(ctx, 0, 0);
2032 	} else {
2033 		call2id = &ic.call2id;
2034 		expr = extract_expr(cast<Expr>(stmt));
2035 		tree = extract(expr, stmt->getSourceRange(), true);
2036 		call2id = NULL;
2037 	}
2038 	tree = ic.add_inlined(tree);
2039 	return tree;
2040 }
2041 
2042 /* Try and construct a pet_tree corresponding to "stmt".
2043  *
2044  * If "stmt" is a compound statement, then "skip_declarations"
2045  * indicates whether we should skip initial declarations in the
2046  * compound statement.
2047  *
2048  * If the constructed pet_tree is not a (possibly) partial representation
2049  * of "stmt", we update start and end of the pet_scop to those of "stmt".
2050  * In particular, if skip_declarations is set, then we may have skipped
2051  * declarations inside "stmt" and so the pet_scop may not represent
2052  * the entire "stmt".
2053  * Note that this function may be called with "stmt" referring to the entire
2054  * body of the function, including the outer braces.  In such cases,
2055  * skip_declarations will be set and the braces will not be taken into
2056  * account in tree->loc.
2057  */
extract(Stmt * stmt,bool skip_declarations)2058 __isl_give pet_tree *PetScan::extract(Stmt *stmt, bool skip_declarations)
2059 {
2060 	pet_tree *tree;
2061 
2062 	set_current_stmt(stmt);
2063 
2064 	if (isa<Expr>(stmt))
2065 		return extract_expr_stmt(cast<Expr>(stmt));
2066 
2067 	switch (stmt->getStmtClass()) {
2068 	case Stmt::WhileStmtClass:
2069 		tree = extract(cast<WhileStmt>(stmt));
2070 		break;
2071 	case Stmt::ForStmtClass:
2072 		tree = extract_for(cast<ForStmt>(stmt));
2073 		break;
2074 	case Stmt::IfStmtClass:
2075 		tree = extract(cast<IfStmt>(stmt));
2076 		break;
2077 	case Stmt::CompoundStmtClass:
2078 		tree = extract(cast<CompoundStmt>(stmt), skip_declarations);
2079 		break;
2080 	case Stmt::LabelStmtClass:
2081 		tree = extract(cast<LabelStmt>(stmt));
2082 		break;
2083 	case Stmt::ContinueStmtClass:
2084 		tree = pet_tree_new_continue(ctx);
2085 		break;
2086 	case Stmt::BreakStmtClass:
2087 		tree = pet_tree_new_break(ctx);
2088 		break;
2089 	case Stmt::DeclStmtClass:
2090 		tree = extract(cast<DeclStmt>(stmt));
2091 		break;
2092 	case Stmt::NullStmtClass:
2093 		tree = pet_tree_new_block(ctx, 0, 0);
2094 		break;
2095 	case Stmt::ReturnStmtClass:
2096 		tree = extract(cast<ReturnStmt>(stmt));
2097 		break;
2098 	default:
2099 		report_unsupported_statement_type(stmt);
2100 		return NULL;
2101 	}
2102 
2103 	if (partial || skip_declarations)
2104 		return tree;
2105 
2106 	return update_loc(tree, stmt);
2107 }
2108 
2109 /* Given a sequence of statements "stmt_range" of which the first "n_decl"
2110  * are declarations and of which the remaining statements are represented
2111  * by "tree", try and extend "tree" to include the last sequence of
2112  * the initial declarations that can be completely extracted.
2113  *
2114  * We start collecting the initial declarations and start over
2115  * whenever we come across a declaration that we cannot extract.
2116  * If we have been able to extract any declarations, then we
2117  * copy over the contents of "tree" at the end of the declarations.
2118  * Otherwise, we simply return the original "tree".
2119  */
insert_initial_declarations(__isl_take pet_tree * tree,int n_decl,StmtRange stmt_range)2120 __isl_give pet_tree *PetScan::insert_initial_declarations(
2121 	__isl_take pet_tree *tree, int n_decl, StmtRange stmt_range)
2122 {
2123 	StmtIterator i;
2124 	pet_tree *res;
2125 	int n_stmt;
2126 	int is_block;
2127 	int j;
2128 
2129 	n_stmt = pet_tree_block_n_child(tree);
2130 	is_block = pet_tree_block_get_block(tree);
2131 	res = pet_tree_new_block(ctx, is_block, n_decl + n_stmt);
2132 
2133 	for (i = stmt_range.first; n_decl; ++i, --n_decl) {
2134 		Stmt *child = *i;
2135 		pet_tree *tree_i;
2136 
2137 		tree_i = extract(child);
2138 		if (tree_i && !partial) {
2139 			res = pet_tree_block_add_child(res, tree_i);
2140 			continue;
2141 		}
2142 		pet_tree_free(tree_i);
2143 		partial = false;
2144 		if (pet_tree_block_n_child(res) == 0)
2145 			continue;
2146 		pet_tree_free(res);
2147 		res = pet_tree_new_block(ctx, is_block, n_decl + n_stmt);
2148 	}
2149 
2150 	if (pet_tree_block_n_child(res) == 0) {
2151 		pet_tree_free(res);
2152 		return tree;
2153 	}
2154 
2155 	for (j = 0; j < n_stmt; ++j) {
2156 		pet_tree *tree_i;
2157 
2158 		tree_i = pet_tree_block_get_child(tree, j);
2159 		res = pet_tree_block_add_child(res, tree_i);
2160 	}
2161 	pet_tree_free(tree);
2162 
2163 	return res;
2164 }
2165 
2166 /* Try and construct a pet_tree corresponding to (part of)
2167  * a sequence of statements.
2168  *
2169  * "block" is set if the sequence represents the children of
2170  * a compound statement.
2171  * "skip_declarations" is set if we should skip initial declarations
2172  * in the sequence of statements.
2173  * "parent" is the statement that has stmt_range as (some of) its children.
2174  *
2175  * If autodetect is set, then we allow the extraction of only a subrange
2176  * of the sequence of statements.  However, if there is at least one
2177  * kill and there is some subsequent statement for which we could not
2178  * construct a tree, then turn off the "block" property of the tree
2179  * such that no extra kill will be introduced at the end of the (partial)
2180  * block.  If, on the other hand, the final range contains
2181  * no statements, then we discard the entire range.
2182  * If only a subrange of the sequence was extracted, but each statement
2183  * in the sequence was extracted completely, and if there are some
2184  * variable declarations in the sequence before or inside
2185  * the extracted subrange, then check if any of these variables are
2186  * not used after the extracted subrange.  If so, add kills to these
2187  * variables.
2188  *
2189  * If the entire range was extracted, apart from some initial declarations,
2190  * then we try and extend the range with the latest of those initial
2191  * declarations.
2192  */
extract(StmtRange stmt_range,bool block,bool skip_declarations,Stmt * parent)2193 __isl_give pet_tree *PetScan::extract(StmtRange stmt_range, bool block,
2194 	bool skip_declarations, Stmt *parent)
2195 {
2196 	StmtIterator i;
2197 	int j, skip;
2198 	bool has_kills = false;
2199 	bool partial_range = false;
2200 	bool outer_partial = false;
2201 	pet_tree *tree;
2202 	SourceManager &SM = PP.getSourceManager();
2203 	pet_killed_locals kl(SM);
2204 	unsigned range_start, range_end;
2205 
2206 	for (i = stmt_range.first, j = 0; i != stmt_range.second; ++i, ++j)
2207 		;
2208 
2209 	tree = pet_tree_new_block(ctx, block, j);
2210 
2211 	skip = 0;
2212 	i = stmt_range.first;
2213 	if (skip_declarations)
2214 		for (; i != stmt_range.second; ++i) {
2215 			if ((*i)->getStmtClass() != Stmt::DeclStmtClass)
2216 				break;
2217 			if (options->autodetect)
2218 				kl.add_locals(cast<DeclStmt>(*i));
2219 			++skip;
2220 		}
2221 
2222 	for (; i != stmt_range.second; ++i) {
2223 		Stmt *child = *i;
2224 		pet_tree *tree_i;
2225 
2226 		tree_i = extract(child);
2227 		if (pet_tree_block_n_child(tree) != 0 && partial) {
2228 			pet_tree_free(tree_i);
2229 			break;
2230 		}
2231 		if (child->getStmtClass() == Stmt::DeclStmtClass) {
2232 			if (options->autodetect)
2233 				kl.add_locals(cast<DeclStmt>(child));
2234 			if (tree_i && block)
2235 				has_kills = true;
2236 		}
2237 		if (options->autodetect) {
2238 			if (tree_i) {
2239 				range_end = getExpansionOffset(SM,
2240 							end_loc(child));
2241 				if (pet_tree_block_n_child(tree) == 0)
2242 					range_start = getExpansionOffset(SM,
2243 							begin_loc(child));
2244 				tree = pet_tree_block_add_child(tree, tree_i);
2245 			} else {
2246 				partial_range = true;
2247 			}
2248 			if (pet_tree_block_n_child(tree) != 0 && !tree_i)
2249 				outer_partial = partial = true;
2250 		} else {
2251 			tree = pet_tree_block_add_child(tree, tree_i);
2252 		}
2253 
2254 		if (partial || !tree)
2255 			break;
2256 	}
2257 
2258 	if (!tree)
2259 		return NULL;
2260 
2261 	if (partial) {
2262 		if (has_kills)
2263 			tree = pet_tree_block_set_block(tree, 0);
2264 		if (outer_partial) {
2265 			kl.remove_accessed_after(parent,
2266 						 range_start, range_end);
2267 			tree = add_kills(tree, kl.locals);
2268 		}
2269 	} else if (partial_range) {
2270 		if (pet_tree_block_n_child(tree) == 0) {
2271 			pet_tree_free(tree);
2272 			return NULL;
2273 		}
2274 		partial = true;
2275 	} else if (skip > 0)
2276 		tree = insert_initial_declarations(tree, skip, stmt_range);
2277 
2278 	return tree;
2279 }
2280 
2281 extern "C" {
2282 	static __isl_give pet_expr *get_array_size(__isl_keep pet_expr *access,
2283 		void *user);
2284 	static struct pet_array *extract_array(__isl_keep pet_expr *access,
2285 		__isl_keep pet_context *pc, void *user);
2286 }
2287 
2288 /* Construct a pet_expr that holds the sizes of the array accessed
2289  * by "access".
2290  * This function is used as a callback to pet_context_add_parameters,
2291  * which is also passed a pointer to the PetScan object.
2292  */
get_array_size(__isl_keep pet_expr * access,void * user)2293 static __isl_give pet_expr *get_array_size(__isl_keep pet_expr *access,
2294 	void *user)
2295 {
2296 	PetScan *ps = (PetScan *) user;
2297 	isl_id *id;
2298 	pet_expr *size;
2299 
2300 	id = pet_expr_access_get_id(access);
2301 	size = ps->get_array_size(id);
2302 	isl_id_free(id);
2303 
2304 	return size;
2305 }
2306 
2307 /* Construct and return a pet_array corresponding to the variable
2308  * accessed by "access".
2309  * This function is used as a callback to pet_scop_from_pet_tree,
2310  * which is also passed a pointer to the PetScan object.
2311  */
extract_array(__isl_keep pet_expr * access,__isl_keep pet_context * pc,void * user)2312 static struct pet_array *extract_array(__isl_keep pet_expr *access,
2313 	__isl_keep pet_context *pc, void *user)
2314 {
2315 	PetScan *ps = (PetScan *) user;
2316 	isl_id *id;
2317 	pet_array *array;
2318 
2319 	id = pet_expr_access_get_id(access);
2320 	array = ps->extract_array(id, NULL, pc);
2321 	isl_id_free(id);
2322 
2323 	return array;
2324 }
2325 
2326 /* Extract a function summary from the body of "fd".
2327  *
2328  * We extract a scop from the function body in a context with as
2329  * parameters the integer arguments of the function.
2330  * We turn off autodetection (in case it was set) to ensure that
2331  * the entire function body is considered.
2332  * We then collect the accessed array elements and attach them
2333  * to the corresponding array arguments, taking into account
2334  * that the function body may access members of array elements.
2335  * The function body is allowed to have a return statement at the end.
2336  *
2337  * The reason for representing the integer arguments as parameters in
2338  * the context is that if we were to instead start with a context
2339  * with the function arguments as initial dimensions, then we would not
2340  * be able to refer to them from the array extents, without turning
2341  * array extents into maps.
2342  *
2343  * The result is stored in the summary_cache cache so that we can reuse
2344  * it if this method gets called on the same function again later on.
2345  */
get_summary(FunctionDecl * fd)2346 __isl_give pet_function_summary *PetScan::get_summary(FunctionDecl *fd)
2347 {
2348 	isl_space *space;
2349 	isl_set *domain;
2350 	pet_context *pc;
2351 	pet_tree *tree;
2352 	pet_function_summary *summary;
2353 	unsigned n;
2354 	ScopLoc loc;
2355 	int save_autodetect;
2356 	struct pet_scop *scop;
2357 	int int_size;
2358 	isl_union_set *may_read, *may_write, *must_write;
2359 	isl_union_map *to_inner;
2360 
2361 	if (summary_cache.find(fd) != summary_cache.end())
2362 		return pet_function_summary_copy(summary_cache[fd]);
2363 
2364 	space = isl_space_set_alloc(ctx, 0, 0);
2365 
2366 	n = fd->getNumParams();
2367 	summary = pet_function_summary_alloc(ctx, n);
2368 	for (unsigned i = 0; i < n; ++i) {
2369 		ParmVarDecl *parm = fd->getParamDecl(i);
2370 		QualType type = parm->getType();
2371 		isl_id *id;
2372 
2373 		if (!type->isIntegerType())
2374 			continue;
2375 		id = pet_id_from_decl(ctx, parm);
2376 		space = isl_space_insert_dims(space, isl_dim_param, 0, 1);
2377 		space = isl_space_set_dim_id(space, isl_dim_param, 0,
2378 						isl_id_copy(id));
2379 		summary = pet_function_summary_set_int(summary, i, id);
2380 	}
2381 
2382 	save_autodetect = options->autodetect;
2383 	options->autodetect = 0;
2384 	PetScan body_scan(PP, ast_context, fd, loc, options,
2385 				isl_union_map_copy(value_bounds), independent);
2386 
2387 	body_scan.return_root = fd->getBody();
2388 	tree = body_scan.extract(fd->getBody(), false);
2389 
2390 	domain = isl_set_universe(space);
2391 	pc = pet_context_alloc(domain);
2392 	pc = pet_context_add_parameters(pc, tree,
2393 						&::get_array_size, &body_scan);
2394 	int_size = size_in_bytes(ast_context, ast_context.IntTy);
2395 	scop = pet_scop_from_pet_tree(tree, int_size,
2396 					&::extract_array, &body_scan, pc);
2397 	scop = scan_arrays(scop, pc);
2398 	may_read = isl_union_map_range(pet_scop_get_may_reads(scop));
2399 	may_write = isl_union_map_range(pet_scop_get_may_writes(scop));
2400 	must_write = isl_union_map_range(pet_scop_get_must_writes(scop));
2401 	to_inner = pet_scop_compute_outer_to_inner(scop);
2402 	pet_scop_free(scop);
2403 
2404 	for (unsigned i = 0; i < n; ++i) {
2405 		ParmVarDecl *parm = fd->getParamDecl(i);
2406 		QualType type = parm->getType();
2407 		struct pet_array *array;
2408 		isl_space *space;
2409 		isl_union_set *data_set;
2410 		isl_union_set *may_read_i, *may_write_i, *must_write_i;
2411 
2412 		if (pet_clang_array_depth(type) == 0)
2413 			continue;
2414 
2415 		array = body_scan.extract_array(parm, NULL, pc);
2416 		space = array ? isl_set_get_space(array->extent) : NULL;
2417 		pet_array_free(array);
2418 		data_set = isl_union_set_from_set(isl_set_universe(space));
2419 		data_set = isl_union_set_apply(data_set,
2420 					isl_union_map_copy(to_inner));
2421 		may_read_i = isl_union_set_intersect(
2422 				isl_union_set_copy(may_read),
2423 				isl_union_set_copy(data_set));
2424 		may_write_i = isl_union_set_intersect(
2425 				isl_union_set_copy(may_write),
2426 				isl_union_set_copy(data_set));
2427 		must_write_i = isl_union_set_intersect(
2428 				isl_union_set_copy(must_write), data_set);
2429 		summary = pet_function_summary_set_array(summary, i,
2430 				may_read_i, may_write_i, must_write_i);
2431 	}
2432 
2433 	isl_union_set_free(may_read);
2434 	isl_union_set_free(may_write);
2435 	isl_union_set_free(must_write);
2436 	isl_union_map_free(to_inner);
2437 
2438 	options->autodetect = save_autodetect;
2439 	pet_context_free(pc);
2440 
2441 	summary_cache[fd] = pet_function_summary_copy(summary);
2442 
2443 	return summary;
2444 }
2445 
2446 /* If "fd" has a function body, then extract a function summary from
2447  * this body and attach it to the call expression "expr".
2448  *
2449  * Even if a function body is available, "fd" itself may point
2450  * to a declaration without function body.  We therefore first
2451  * replace it by the declaration that comes with a body (if any).
2452  */
set_summary(__isl_take pet_expr * expr,FunctionDecl * fd)2453 __isl_give pet_expr *PetScan::set_summary(__isl_take pet_expr *expr,
2454 	FunctionDecl *fd)
2455 {
2456 	pet_function_summary *summary;
2457 
2458 	if (!expr)
2459 		return NULL;
2460 	fd = pet_clang_find_function_decl_with_body(fd);
2461 	if (!fd)
2462 		return expr;
2463 
2464 	summary = get_summary(fd);
2465 
2466 	expr = pet_expr_call_set_summary(expr, summary);
2467 
2468 	return expr;
2469 }
2470 
2471 /* Extract a pet_scop from "tree".
2472  *
2473  * We simply call pet_scop_from_pet_tree with the appropriate arguments and
2474  * then add pet_arrays for all accessed arrays.
2475  * We populate the pet_context with assignments for all parameters used
2476  * inside "tree" or any of the size expressions for the arrays accessed
2477  * by "tree" so that they can be used in affine expressions.
2478  */
extract_scop(__isl_take pet_tree * tree)2479 struct pet_scop *PetScan::extract_scop(__isl_take pet_tree *tree)
2480 {
2481 	int int_size;
2482 	isl_set *domain;
2483 	pet_context *pc;
2484 	pet_scop *scop;
2485 
2486 	int_size = size_in_bytes(ast_context, ast_context.IntTy);
2487 
2488 	domain = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
2489 	pc = pet_context_alloc(domain);
2490 	pc = pet_context_add_parameters(pc, tree, &::get_array_size, this);
2491 	scop = pet_scop_from_pet_tree(tree, int_size,
2492 					&::extract_array, this, pc);
2493 	scop = scan_arrays(scop, pc);
2494 	pet_context_free(pc);
2495 
2496 	return scop;
2497 }
2498 
2499 /* Add a call to __pencil_kill to the end of "tree" that kills
2500  * all the variables in "locals" and return the result.
2501  *
2502  * No location is added to the kill because the most natural
2503  * location would lie outside the scop.  Attaching such a location
2504  * to this tree would extend the scope of the final result
2505  * to include the location.
2506  */
add_kills(__isl_take pet_tree * tree,set<ValueDecl * > locals)2507 __isl_give pet_tree *PetScan::add_kills(__isl_take pet_tree *tree,
2508 	set<ValueDecl *> locals)
2509 {
2510 	int i;
2511 	pet_expr *expr;
2512 	pet_tree *kill, *block;
2513 	set<ValueDecl *>::iterator it;
2514 
2515 	if (locals.size() == 0)
2516 		return tree;
2517 	expr = pet_expr_new_call(ctx, "__pencil_kill", locals.size());
2518 	i = 0;
2519 	for (it = locals.begin(); it != locals.end(); ++it) {
2520 		pet_expr *arg;
2521 		arg = extract_access_expr(*it);
2522 		expr = pet_expr_set_arg(expr, i++, arg);
2523 	}
2524 	kill = pet_tree_new_expr(expr);
2525 	block = pet_tree_new_block(ctx, 0, 2);
2526 	block = pet_tree_block_add_child(block, tree);
2527 	block = pet_tree_block_add_child(block, kill);
2528 
2529 	return block;
2530 }
2531 
2532 /* Check if the scop marked by the user is exactly this Stmt
2533  * or part of this Stmt.
2534  * If so, return a pet_scop corresponding to the marked region.
2535  * Otherwise, return NULL.
2536  *
2537  * If the scop is not further nested inside a child of "stmt",
2538  * then check if there are any variable declarations before the scop
2539  * inside "stmt".  If so, and if these variables are not used
2540  * after the scop, then add kills to the variables.
2541  *
2542  * If the scop starts in the middle of one of the children, without
2543  * also ending in that child, then report an error.
2544  */
scan(Stmt * stmt)2545 struct pet_scop *PetScan::scan(Stmt *stmt)
2546 {
2547 	SourceManager &SM = PP.getSourceManager();
2548 	unsigned start_off, end_off;
2549 	pet_tree *tree;
2550 
2551 	start_off = getExpansionOffset(SM, begin_loc(stmt));
2552 	end_off = getExpansionOffset(SM, end_loc(stmt));
2553 
2554 	if (start_off > loc.end)
2555 		return NULL;
2556 	if (end_off < loc.start)
2557 		return NULL;
2558 
2559 	if (start_off >= loc.start && end_off <= loc.end)
2560 		return extract_scop(extract(stmt));
2561 
2562 	pet_killed_locals kl(SM);
2563 	StmtIterator start;
2564 	for (start = stmt->child_begin(); start != stmt->child_end(); ++start) {
2565 		Stmt *child = *start;
2566 		if (!child)
2567 			continue;
2568 		start_off = getExpansionOffset(SM, begin_loc(child));
2569 		end_off = getExpansionOffset(SM, end_loc(child));
2570 		if (start_off < loc.start && end_off >= loc.end)
2571 			return scan(child);
2572 		if (start_off >= loc.start)
2573 			break;
2574 		if (loc.start < end_off) {
2575 			report_unbalanced_pragmas(loc.scop, loc.endscop);
2576 			return NULL;
2577 		}
2578 		if (isa<DeclStmt>(child))
2579 			kl.add_locals(cast<DeclStmt>(child));
2580 	}
2581 
2582 	StmtIterator end;
2583 	for (end = start; end != stmt->child_end(); ++end) {
2584 		Stmt *child = *end;
2585 		start_off = SM.getFileOffset(begin_loc(child));
2586 		if (start_off >= loc.end)
2587 			break;
2588 	}
2589 
2590 	kl.remove_accessed_after(stmt, loc.start, loc.end);
2591 
2592 	tree = extract(StmtRange(start, end), false, false, stmt);
2593 	tree = add_kills(tree, kl.locals);
2594 	return extract_scop(tree);
2595 }
2596 
2597 /* Set the size of index "pos" of "array" to "size".
2598  * In particular, add a constraint of the form
2599  *
2600  *	i_pos < size
2601  *
2602  * to array->extent and a constraint of the form
2603  *
2604  *	size >= 0
2605  *
2606  * to array->context.
2607  *
2608  * The domain of "size" is assumed to be zero-dimensional.
2609  */
update_size(struct pet_array * array,int pos,__isl_take isl_pw_aff * size)2610 static struct pet_array *update_size(struct pet_array *array, int pos,
2611 	__isl_take isl_pw_aff *size)
2612 {
2613 	isl_set *valid;
2614 	isl_set *univ;
2615 	isl_set *bound;
2616 	isl_space *dim;
2617 	isl_aff *aff;
2618 	isl_pw_aff *index;
2619 	isl_id *id;
2620 
2621 	if (!array)
2622 		goto error;
2623 
2624 	valid = isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size)));
2625 	array->context = isl_set_intersect(array->context, valid);
2626 
2627 	dim = isl_set_get_space(array->extent);
2628 	aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2629 	aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, 1);
2630 	univ = isl_set_universe(isl_aff_get_domain_space(aff));
2631 	index = isl_pw_aff_alloc(univ, aff);
2632 
2633 	size = isl_pw_aff_add_dims(size, isl_dim_in,
2634 				isl_set_dim(array->extent, isl_dim_set));
2635 	id = isl_set_get_tuple_id(array->extent);
2636 	size = isl_pw_aff_set_tuple_id(size, isl_dim_in, id);
2637 	bound = isl_pw_aff_lt_set(index, size);
2638 
2639 	array->extent = isl_set_intersect(array->extent, bound);
2640 
2641 	if (!array->context || !array->extent)
2642 		return pet_array_free(array);
2643 
2644 	return array;
2645 error:
2646 	isl_pw_aff_free(size);
2647 	return NULL;
2648 }
2649 
2650 #ifdef HAVE_DECAYEDTYPE
2651 
2652 /* If "qt" is a decayed type, then set *decayed to true and
2653  * return the original type.
2654  */
undecay(QualType qt,bool * decayed)2655 static QualType undecay(QualType qt, bool *decayed)
2656 {
2657 	const Type *type = qt.getTypePtr();
2658 
2659 	*decayed = isa<DecayedType>(type);
2660 	if (*decayed)
2661 		qt = cast<DecayedType>(type)->getOriginalType();
2662 	return qt;
2663 }
2664 
2665 #else
2666 
2667 /* If "qt" is a decayed type, then set *decayed to true and
2668  * return the original type.
2669  * Since this version of clang does not define a DecayedType,
2670  * we cannot obtain the original type even if it had been decayed and
2671  * we set *decayed to false.
2672  */
undecay(QualType qt,bool * decayed)2673 static QualType undecay(QualType qt, bool *decayed)
2674 {
2675 	*decayed = false;
2676 	return qt;
2677 }
2678 
2679 #endif
2680 
2681 /* Figure out the size of the array at position "pos" and all
2682  * subsequent positions from "qt" and update the corresponding
2683  * argument of "expr" accordingly.
2684  *
2685  * The initial type (when pos is zero) may be a pointer type decayed
2686  * from an array type, if this initial type is the type of a function
2687  * argument.  This only happens if the original array type has
2688  * a constant size in the outer dimension as otherwise we get
2689  * a VariableArrayType.  Try and obtain this original type (if available) and
2690  * take the outer array size into account if it was marked static.
2691  */
set_upper_bounds(__isl_take pet_expr * expr,QualType qt,int pos)2692 __isl_give pet_expr *PetScan::set_upper_bounds(__isl_take pet_expr *expr,
2693 	QualType qt, int pos)
2694 {
2695 	const ArrayType *atype;
2696 	pet_expr *size;
2697 	bool decayed = false;
2698 
2699 	if (!expr)
2700 		return NULL;
2701 
2702 	if (pos == 0)
2703 		qt = undecay(qt, &decayed);
2704 
2705 	if (qt->isPointerType()) {
2706 		qt = qt->getPointeeType();
2707 		return set_upper_bounds(expr, qt, pos + 1);
2708 	}
2709 	if (!qt->isArrayType())
2710 		return expr;
2711 
2712 	qt = qt->getCanonicalTypeInternal();
2713 	atype = cast<ArrayType>(qt.getTypePtr());
2714 
2715 	if (decayed && atype->getSizeModifier() != ArrayType::Static) {
2716 		qt = atype->getElementType();
2717 		return set_upper_bounds(expr, qt, pos + 1);
2718 	}
2719 
2720 	if (qt->isConstantArrayType()) {
2721 		const ConstantArrayType *ca = cast<ConstantArrayType>(atype);
2722 		size = extract_expr(ca->getSize());
2723 		expr = pet_expr_set_arg(expr, pos, size);
2724 	} else if (qt->isVariableArrayType()) {
2725 		const VariableArrayType *vla = cast<VariableArrayType>(atype);
2726 		size = extract_expr(vla->getSizeExpr());
2727 		expr = pet_expr_set_arg(expr, pos, size);
2728 	}
2729 
2730 	qt = atype->getElementType();
2731 
2732 	return set_upper_bounds(expr, qt, pos + 1);
2733 }
2734 
2735 /* Construct a pet_expr that holds the sizes of the array represented by "id".
2736  * The returned expression is a call expression with as arguments
2737  * the sizes in each dimension.  If we are unable to derive the size
2738  * in a given dimension, then the corresponding argument is set to infinity.
2739  * In fact, we initialize all arguments to infinity and then update
2740  * them if we are able to figure out the size.
2741  *
2742  * The result is stored in the id_size cache so that it can be reused
2743  * if this method is called on the same array identifier later.
2744  * The result is also stored in the type_size cache in case
2745  * it gets called on a different array identifier with the same type.
2746  */
get_array_size(__isl_keep isl_id * id)2747 __isl_give pet_expr *PetScan::get_array_size(__isl_keep isl_id *id)
2748 {
2749 	QualType qt = pet_id_get_array_type(id);
2750 	int depth;
2751 	pet_expr *expr, *inf;
2752 	const Type *type = qt.getTypePtr();
2753 	isl_maybe_pet_expr m;
2754 
2755 	m = isl_id_to_pet_expr_try_get(id_size, id);
2756 	if (m.valid < 0 || m.valid)
2757 		return m.value;
2758 	if (type_size.find(type) != type_size.end())
2759 		return pet_expr_copy(type_size[type]);
2760 
2761 	depth = pet_clang_array_depth(qt);
2762 	inf = pet_expr_new_int(isl_val_infty(ctx));
2763 	expr = pet_expr_new_call(ctx, "bounds", depth);
2764 	for (int i = 0; i < depth; ++i)
2765 		expr = pet_expr_set_arg(expr, i, pet_expr_copy(inf));
2766 	pet_expr_free(inf);
2767 
2768 	expr = set_upper_bounds(expr, qt, 0);
2769 	type_size[type] = pet_expr_copy(expr);
2770 	id_size = isl_id_to_pet_expr_set(id_size, isl_id_copy(id),
2771 					pet_expr_copy(expr));
2772 
2773 	return expr;
2774 }
2775 
2776 /* Set the array size of the array identified by "id" to "size",
2777  * replacing any previously stored value.
2778  */
set_array_size(__isl_take isl_id * id,__isl_take pet_expr * size)2779 void PetScan::set_array_size(__isl_take isl_id *id, __isl_take pet_expr *size)
2780 {
2781 	id_size = isl_id_to_pet_expr_set(id_size, id, size);
2782 }
2783 
2784 /* Does "expr" represent the "integer" infinity?
2785  */
is_infty(__isl_keep pet_expr * expr)2786 static int is_infty(__isl_keep pet_expr *expr)
2787 {
2788 	isl_val *v;
2789 	int res;
2790 
2791 	if (pet_expr_get_type(expr) != pet_expr_int)
2792 		return 0;
2793 	v = pet_expr_int_get_val(expr);
2794 	res = isl_val_is_infty(v);
2795 	isl_val_free(v);
2796 
2797 	return res;
2798 }
2799 
2800 /* Figure out the dimensions of an array "array" and
2801  * update "array" accordingly.
2802  *
2803  * We first construct a pet_expr that holds the sizes of the array
2804  * in each dimension.  The resulting expression may containing
2805  * infinity values for dimension where we are unable to derive
2806  * a size expression.
2807  *
2808  * The arguments of the size expression that have a value different from
2809  * infinity are then converted to an affine expression
2810  * within the context "pc" and incorporated into the size of "array".
2811  * If we are unable to convert a size expression to an affine expression or
2812  * if the size is not a (symbolic) constant,
2813  * then we leave the corresponding size of "array" untouched.
2814  */
set_upper_bounds(struct pet_array * array,__isl_keep pet_context * pc)2815 struct pet_array *PetScan::set_upper_bounds(struct pet_array *array,
2816 	__isl_keep pet_context *pc)
2817 {
2818 	int n;
2819 	isl_id *id;
2820 	pet_expr *expr;
2821 
2822 	if (!array)
2823 		return NULL;
2824 
2825 	id = isl_set_get_tuple_id(array->extent);
2826 	if (!id)
2827 		return pet_array_free(array);
2828 	expr = get_array_size(id);
2829 	isl_id_free(id);
2830 
2831 	n = pet_expr_get_n_arg(expr);
2832 	for (int i = 0; i < n; ++i) {
2833 		pet_expr *arg;
2834 		isl_pw_aff *size;
2835 
2836 		arg = pet_expr_get_arg(expr, i);
2837 		if (!is_infty(arg)) {
2838 			int dim;
2839 
2840 			size = pet_expr_extract_affine(arg, pc);
2841 			dim = isl_pw_aff_dim(size, isl_dim_in);
2842 			if (!size)
2843 				array = pet_array_free(array);
2844 			else if (isl_pw_aff_involves_nan(size) ||
2845 			    isl_pw_aff_involves_dims(size, isl_dim_in, 0, dim))
2846 				isl_pw_aff_free(size);
2847 			else {
2848 				size = isl_pw_aff_drop_dims(size,
2849 							    isl_dim_in, 0, dim);
2850 				array = update_size(array, i, size);
2851 			}
2852 		}
2853 		pet_expr_free(arg);
2854 	}
2855 	pet_expr_free(expr);
2856 
2857 	return array;
2858 }
2859 
2860 /* Does "decl" have a definition that we can keep track of in a pet_type?
2861  */
has_printable_definition(RecordDecl * decl)2862 static bool has_printable_definition(RecordDecl *decl)
2863 {
2864 	if (!decl->getDeclName())
2865 		return false;
2866 	return decl->getLexicalDeclContext() == decl->getDeclContext();
2867 }
2868 
2869 /* Add all TypedefType objects that appear when dereferencing "type"
2870  * to "types".
2871  */
insert_intermediate_typedefs(PetTypes * types,QualType type)2872 static void insert_intermediate_typedefs(PetTypes *types, QualType type)
2873 {
2874 	type = pet_clang_base_or_typedef_type(type);
2875 	while (isa<TypedefType>(type)) {
2876 		const TypedefType *tt;
2877 
2878 		tt = cast<TypedefType>(type);
2879 		types->insert(tt->getDecl());
2880 		type = tt->desugar();
2881 		type = pet_clang_base_or_typedef_type(type);
2882 	}
2883 }
2884 
2885 /* Construct and return a pet_array corresponding to the variable
2886  * represented by "id".
2887  * In particular, initialize array->extent to
2888  *
2889  *	{ name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2890  *
2891  * and then call set_upper_bounds to set the upper bounds on the indices
2892  * based on the type of the variable.  The upper bounds are converted
2893  * to affine expressions within the context "pc".
2894  *
2895  * If the base type is that of a record with a top-level definition or
2896  * of a typedef and if "types" is not null, then the RecordDecl or
2897  * TypedefType corresponding to the type, as well as any intermediate
2898  * TypedefType, is added to "types".
2899  *
2900  * If the base type is that of a record with no top-level definition,
2901  * then we replace it by "<subfield>".
2902  *
2903  * If the variable is a scalar, i.e., a zero-dimensional array,
2904  * then the "const" qualifier, if any, is removed from the base type.
2905  * This makes it easier for users of pet to turn initializations
2906  * into assignments.
2907  */
extract_array(__isl_keep isl_id * id,PetTypes * types,__isl_keep pet_context * pc)2908 struct pet_array *PetScan::extract_array(__isl_keep isl_id *id,
2909 	PetTypes *types, __isl_keep pet_context *pc)
2910 {
2911 	struct pet_array *array;
2912 	QualType qt = pet_id_get_array_type(id);
2913 	int depth = pet_clang_array_depth(qt);
2914 	QualType base = pet_clang_base_type(qt);
2915 	string name;
2916 	isl_space *space;
2917 
2918 	array = isl_calloc_type(ctx, struct pet_array);
2919 	if (!array)
2920 		return NULL;
2921 
2922 	space = isl_space_set_alloc(ctx, 0, depth);
2923 	space = isl_space_set_tuple_id(space, isl_dim_set, isl_id_copy(id));
2924 
2925 	array->extent = isl_set_nat_universe(space);
2926 
2927 	space = isl_space_params_alloc(ctx, 0);
2928 	array->context = isl_set_universe(space);
2929 
2930 	array = set_upper_bounds(array, pc);
2931 	if (!array)
2932 		return NULL;
2933 
2934 	if (depth == 0)
2935 		base.removeLocalConst();
2936 	name = base.getAsString();
2937 
2938 	if (types) {
2939 		insert_intermediate_typedefs(types, qt);
2940 		if (isa<TypedefType>(base)) {
2941 			types->insert(cast<TypedefType>(base)->getDecl());
2942 		} else if (base->isRecordType()) {
2943 			RecordDecl *decl = pet_clang_record_decl(base);
2944 			TypedefNameDecl *typedecl;
2945 			typedecl = decl->getTypedefNameForAnonDecl();
2946 			if (typedecl)
2947 				types->insert(typedecl);
2948 			else if (has_printable_definition(decl))
2949 				types->insert(decl);
2950 			else
2951 				name = "<subfield>";
2952 		}
2953 	}
2954 
2955 	array->element_type = strdup(name.c_str());
2956 	array->element_is_record = base->isRecordType();
2957 	array->element_size = size_in_bytes(ast_context, base);
2958 
2959 	return array;
2960 }
2961 
2962 /* Construct and return a pet_array corresponding to the variable "decl".
2963  */
extract_array(ValueDecl * decl,PetTypes * types,__isl_keep pet_context * pc)2964 struct pet_array *PetScan::extract_array(ValueDecl *decl,
2965 	PetTypes *types, __isl_keep pet_context *pc)
2966 {
2967 	isl_id *id;
2968 	pet_array *array;
2969 
2970 	id = pet_id_from_decl(ctx, decl);
2971 	array = extract_array(id, types, pc);
2972 	isl_id_free(id);
2973 
2974 	return array;
2975 }
2976 
2977 /* Construct and return a pet_array corresponding to the sequence
2978  * of declarations represented by "decls".
2979  * The upper bounds of the array are converted to affine expressions
2980  * within the context "pc".
2981  * If the sequence contains a single declaration, then it corresponds
2982  * to a simple array access.  Otherwise, it corresponds to a member access,
2983  * with the declaration for the substructure following that of the containing
2984  * structure in the sequence of declarations.
2985  * We start with the outermost substructure and then combine it with
2986  * information from the inner structures.
2987  *
2988  * Additionally, keep track of all required types in "types".
2989  */
extract_array(__isl_keep isl_id_list * decls,PetTypes * types,__isl_keep pet_context * pc)2990 struct pet_array *PetScan::extract_array(__isl_keep isl_id_list *decls,
2991 	PetTypes *types, __isl_keep pet_context *pc)
2992 {
2993 	int i, n;
2994 	isl_id *id;
2995 	struct pet_array *array;
2996 
2997 	id = isl_id_list_get_id(decls, 0);
2998 	array = extract_array(id, types, pc);
2999 	isl_id_free(id);
3000 
3001 	n = isl_id_list_n_id(decls);
3002 	for (i = 1; i < n; ++i) {
3003 		struct pet_array *parent;
3004 		const char *base_name, *field_name;
3005 		char *product_name;
3006 
3007 		parent = array;
3008 		id = isl_id_list_get_id(decls, i);
3009 		array = extract_array(id, types, pc);
3010 		isl_id_free(id);
3011 		if (!array)
3012 			return pet_array_free(parent);
3013 
3014 		base_name = isl_set_get_tuple_name(parent->extent);
3015 		field_name = isl_set_get_tuple_name(array->extent);
3016 		product_name = pet_array_member_access_name(ctx,
3017 							base_name, field_name);
3018 
3019 		array->extent = isl_set_product(isl_set_copy(parent->extent),
3020 						array->extent);
3021 		if (product_name)
3022 			array->extent = isl_set_set_tuple_name(array->extent,
3023 								product_name);
3024 		array->context = isl_set_intersect(array->context,
3025 						isl_set_copy(parent->context));
3026 
3027 		pet_array_free(parent);
3028 		free(product_name);
3029 
3030 		if (!array->extent || !array->context || !product_name)
3031 			return pet_array_free(array);
3032 	}
3033 
3034 	return array;
3035 }
3036 
3037 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3038 	RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3039 	std::set<TypeDecl *> &types_done);
3040 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3041 	TypedefNameDecl *decl, Preprocessor &PP, PetTypes &types,
3042 	std::set<TypeDecl *> &types_done);
3043 
3044 /* For each of the fields of "decl" that is itself a record type
3045  * or a typedef, or an array of such type, add a corresponding pet_type
3046  * to "scop".
3047  */
add_field_types(isl_ctx * ctx,struct pet_scop * scop,RecordDecl * decl,Preprocessor & PP,PetTypes & types,std::set<TypeDecl * > & types_done)3048 static struct pet_scop *add_field_types(isl_ctx *ctx, struct pet_scop *scop,
3049 	RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3050 	std::set<TypeDecl *> &types_done)
3051 {
3052 	RecordDecl::field_iterator it;
3053 
3054 	for (it = decl->field_begin(); it != decl->field_end(); ++it) {
3055 		QualType type = it->getType();
3056 
3057 		type = pet_clang_base_or_typedef_type(type);
3058 		if (isa<TypedefType>(type)) {
3059 			TypedefNameDecl *typedefdecl;
3060 
3061 			typedefdecl = cast<TypedefType>(type)->getDecl();
3062 			scop = add_type(ctx, scop, typedefdecl,
3063 				PP, types, types_done);
3064 		} else if (type->isRecordType()) {
3065 			RecordDecl *record;
3066 
3067 			record = pet_clang_record_decl(type);
3068 			scop = add_type(ctx, scop, record,
3069 				PP, types, types_done);
3070 		}
3071 	}
3072 
3073 	return scop;
3074 }
3075 
3076 /* Add a pet_type corresponding to "decl" to "scop", provided
3077  * it is a member of types.records and it has not been added before
3078  * (i.e., it is not a member of "types_done").
3079  *
3080  * Since we want the user to be able to print the types
3081  * in the order in which they appear in the scop, we need to
3082  * make sure that types of fields in a structure appear before
3083  * that structure.  We therefore call ourselves recursively
3084  * through add_field_types on the types of all record subfields.
3085  */
add_type(isl_ctx * ctx,struct pet_scop * scop,RecordDecl * decl,Preprocessor & PP,PetTypes & types,std::set<TypeDecl * > & types_done)3086 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3087 	RecordDecl *decl, Preprocessor &PP, PetTypes &types,
3088 	std::set<TypeDecl *> &types_done)
3089 {
3090 	string s;
3091 	llvm::raw_string_ostream S(s);
3092 
3093 	if (types.records.find(decl) == types.records.end())
3094 		return scop;
3095 	if (types_done.find(decl) != types_done.end())
3096 		return scop;
3097 
3098 	add_field_types(ctx, scop, decl, PP, types, types_done);
3099 
3100 	if (strlen(decl->getName().str().c_str()) == 0)
3101 		return scop;
3102 
3103 	decl->print(S, PrintingPolicy(PP.getLangOpts()));
3104 	S.str();
3105 
3106 	scop->types[scop->n_type] = pet_type_alloc(ctx,
3107 				    decl->getName().str().c_str(), s.c_str());
3108 	if (!scop->types[scop->n_type])
3109 		return pet_scop_free(scop);
3110 
3111 	types_done.insert(decl);
3112 
3113 	scop->n_type++;
3114 
3115 	return scop;
3116 }
3117 
3118 /* Add a pet_type corresponding to "decl" to "scop", provided
3119  * it is a member of types.typedefs and it has not been added before
3120  * (i.e., it is not a member of "types_done").
3121  *
3122  * If the underlying type is a structure, then we print the typedef
3123  * ourselves since clang does not print the definition of the structure
3124  * in the typedef.  We also make sure in this case that the types of
3125  * the fields in the structure are added first.
3126  * Since the definition of the structure also gets printed this way,
3127  * add it to types_done such that it will not be printed again,
3128  * not even without the typedef.
3129  */
add_type(isl_ctx * ctx,struct pet_scop * scop,TypedefNameDecl * decl,Preprocessor & PP,PetTypes & types,std::set<TypeDecl * > & types_done)3130 static struct pet_scop *add_type(isl_ctx *ctx, struct pet_scop *scop,
3131 	TypedefNameDecl *decl, Preprocessor &PP, PetTypes &types,
3132 	std::set<TypeDecl *> &types_done)
3133 {
3134 	string s;
3135 	llvm::raw_string_ostream S(s);
3136 	QualType qt = decl->getUnderlyingType();
3137 
3138 	if (types.typedefs.find(decl) == types.typedefs.end())
3139 		return scop;
3140 	if (types_done.find(decl) != types_done.end())
3141 		return scop;
3142 
3143 	if (qt->isRecordType()) {
3144 		RecordDecl *rec = pet_clang_record_decl(qt);
3145 
3146 		add_field_types(ctx, scop, rec, PP, types, types_done);
3147 		S << "typedef ";
3148 		rec->print(S, PrintingPolicy(PP.getLangOpts()));
3149 		S << " ";
3150 		S << decl->getName();
3151 		types_done.insert(rec);
3152 	} else {
3153 		decl->print(S, PrintingPolicy(PP.getLangOpts()));
3154 	}
3155 	S.str();
3156 
3157 	scop->types[scop->n_type] = pet_type_alloc(ctx,
3158 				    decl->getName().str().c_str(), s.c_str());
3159 	if (!scop->types[scop->n_type])
3160 		return pet_scop_free(scop);
3161 
3162 	types_done.insert(decl);
3163 
3164 	scop->n_type++;
3165 
3166 	return scop;
3167 }
3168 
3169 /* Construct a list of pet_arrays, one for each array (or scalar)
3170  * accessed inside "scop", add this list to "scop" and return the result.
3171  * The upper bounds of the arrays are converted to affine expressions
3172  * within the context "pc".
3173  *
3174  * The context of "scop" is updated with the intersection of
3175  * the contexts of all arrays, i.e., constraints on the parameters
3176  * that ensure that the arrays have a valid (non-negative) size.
3177  *
3178  * If any of the extracted arrays refers to a member access or
3179  * has a typedef'd type as base type,
3180  * then also add the required types to "scop".
3181  * The typedef types are printed first because their definitions
3182  * may include the definition of a struct and these struct definitions
3183  * should not be printed separately.  While the typedef definition
3184  * is being printed, the struct is marked as having been printed as well,
3185  * such that the later printing of the struct by itself can be prevented.
3186  *
3187  * If the sequence of nested array declarations from which the pet_array
3188  * is extracted appears as the prefix of some other sequence,
3189  * then the pet_array is marked as "outer".
3190  * The arrays that already appear in scop->arrays at the start of
3191  * this function are assumed to be simple arrays, so they are not marked
3192  * as outer.
3193  */
scan_arrays(struct pet_scop * scop,__isl_keep pet_context * pc)3194 struct pet_scop *PetScan::scan_arrays(struct pet_scop *scop,
3195 	__isl_keep pet_context *pc)
3196 {
3197 	int i, n;
3198 	array_desc_set arrays, has_sub;
3199 	array_desc_set::iterator it;
3200 	PetTypes types;
3201 	std::set<TypeDecl *> types_done;
3202 	std::set<clang::RecordDecl *, less_name>::iterator records_it;
3203 	std::set<clang::TypedefNameDecl *, less_name>::iterator typedefs_it;
3204 	int n_array;
3205 	struct pet_array **scop_arrays;
3206 
3207 	if (!scop)
3208 		return NULL;
3209 
3210 	pet_scop_collect_arrays(scop, arrays);
3211 	if (arrays.size() == 0)
3212 		return scop;
3213 
3214 	n_array = scop->n_array;
3215 
3216 	scop_arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3217 					n_array + arrays.size());
3218 	if (!scop_arrays)
3219 		goto error;
3220 	scop->arrays = scop_arrays;
3221 
3222 	for (it = arrays.begin(); it != arrays.end(); ++it) {
3223 		isl_id_list *list = isl_id_list_copy(*it);
3224 		int n = isl_id_list_n_id(list);
3225 		list = isl_id_list_drop(list, n - 1, 1);
3226 		has_sub.insert(list);
3227 	}
3228 
3229 	for (it = arrays.begin(), i = 0; it != arrays.end(); ++it, ++i) {
3230 		struct pet_array *array;
3231 		array = extract_array(*it, &types, pc);
3232 		scop->arrays[n_array + i] = array;
3233 		if (!scop->arrays[n_array + i])
3234 			goto error;
3235 		if (has_sub.find(*it) != has_sub.end())
3236 			array->outer = 1;
3237 		scop->n_array++;
3238 		scop->context = isl_set_intersect(scop->context,
3239 						isl_set_copy(array->context));
3240 		if (!scop->context)
3241 			goto error;
3242 	}
3243 
3244 	n = types.records.size() + types.typedefs.size();
3245 	if (n == 0)
3246 		return scop;
3247 
3248 	scop->types = isl_alloc_array(ctx, struct pet_type *, n);
3249 	if (!scop->types)
3250 		goto error;
3251 
3252 	for (typedefs_it = types.typedefs.begin();
3253 	     typedefs_it != types.typedefs.end(); ++typedefs_it)
3254 		scop = add_type(ctx, scop, *typedefs_it, PP, types, types_done);
3255 
3256 	for (records_it = types.records.begin();
3257 	     records_it != types.records.end(); ++records_it)
3258 		scop = add_type(ctx, scop, *records_it, PP, types, types_done);
3259 
3260 	return scop;
3261 error:
3262 	pet_scop_free(scop);
3263 	return NULL;
3264 }
3265 
3266 /* Bound all parameters in scop->context to the possible values
3267  * of the corresponding C variable.
3268  */
add_parameter_bounds(struct pet_scop * scop)3269 static struct pet_scop *add_parameter_bounds(struct pet_scop *scop)
3270 {
3271 	int n;
3272 
3273 	if (!scop)
3274 		return NULL;
3275 
3276 	n = isl_set_dim(scop->context, isl_dim_param);
3277 	for (int i = 0; i < n; ++i) {
3278 		isl_id *id;
3279 		ValueDecl *decl;
3280 
3281 		id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
3282 		if (pet_nested_in_id(id)) {
3283 			isl_id_free(id);
3284 			isl_die(isl_set_get_ctx(scop->context),
3285 				isl_error_internal,
3286 				"unresolved nested parameter", goto error);
3287 		}
3288 		decl = pet_id_get_decl(id);
3289 		isl_id_free(id);
3290 
3291 		scop->context = set_parameter_bounds(scop->context, i, decl);
3292 
3293 		if (!scop->context)
3294 			goto error;
3295 	}
3296 
3297 	return scop;
3298 error:
3299 	pet_scop_free(scop);
3300 	return NULL;
3301 }
3302 
3303 /* Construct a pet_scop from the given function.
3304  *
3305  * If the scop was delimited by scop and endscop pragmas, then we override
3306  * the file offsets by those derived from the pragmas.
3307  */
scan(FunctionDecl * fd)3308 struct pet_scop *PetScan::scan(FunctionDecl *fd)
3309 {
3310 	pet_scop *scop;
3311 	Stmt *stmt;
3312 
3313 	stmt = fd->getBody();
3314 
3315 	if (options->autodetect) {
3316 		set_current_stmt(stmt);
3317 		scop = extract_scop(extract(stmt, true));
3318 	} else {
3319 		current_line = loc.start_line;
3320 		scop = scan(stmt);
3321 		scop = pet_scop_update_start_end(scop, loc.start, loc.end);
3322 	}
3323 	scop = add_parameter_bounds(scop);
3324 	scop = pet_scop_gist(scop, value_bounds);
3325 
3326 	return scop;
3327 }
3328 
3329 /* Update this->last_line and this->current_line based on the fact
3330  * that we are about to consider "stmt".
3331  */
set_current_stmt(Stmt * stmt)3332 void PetScan::set_current_stmt(Stmt *stmt)
3333 {
3334 	SourceLocation loc = begin_loc(stmt);
3335 	SourceManager &SM = PP.getSourceManager();
3336 
3337 	last_line = current_line;
3338 	current_line = SM.getExpansionLineNumber(loc);
3339 }
3340 
3341 /* Is the current statement marked by an independent pragma?
3342  * That is, is there an independent pragma on a line between
3343  * the line of the current statement and the line of the previous statement.
3344  * The search is not implemented very efficiently.  We currently
3345  * assume that there are only a few independent pragmas, if any.
3346  */
is_current_stmt_marked_independent()3347 bool PetScan::is_current_stmt_marked_independent()
3348 {
3349 	for (unsigned i = 0; i < independent.size(); ++i) {
3350 		unsigned line = independent[i].line;
3351 
3352 		if (last_line < line && line < current_line)
3353 			return true;
3354 	}
3355 
3356 	return false;
3357 }
3358