forked from Meinersbur/pet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pet.cc
1327 lines (1137 loc) · 35.7 KB
/
pet.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011 Leiden University. All rights reserved.
* Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Leiden University.
*/
#include "config.h"
#include <stdlib.h>
#include <map>
#include <vector>
#include <iostream>
#ifdef HAVE_ADT_OWNINGPTR_H
#include <llvm/ADT/OwningPtr.h>
#else
#include <memory>
#endif
#ifdef HAVE_LLVM_OPTION_ARG_H
#include <llvm/Option/Arg.h>
#endif
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/ManagedStatic.h>
#include <llvm/Support/Host.h>
#include <clang/Basic/Version.h>
#include <clang/Basic/FileSystemOptions.h>
#include <clang/Basic/FileManager.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Driver/Tool.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
#include <clang/Basic/DiagnosticOptions.h>
#else
#include <clang/Frontend/DiagnosticOptions.h>
#endif
#include <clang/Frontend/TextDiagnosticPrinter.h>
#ifdef HAVE_LEX_HEADERSEARCHOPTIONS_H
#include <clang/Lex/HeaderSearchOptions.h>
#else
#include <clang/Frontend/HeaderSearchOptions.h>
#endif
#ifdef HAVE_CLANG_BASIC_LANGSTANDARD_H
#include <clang/Basic/LangStandard.h>
#else
#include <clang/Frontend/LangStandard.h>
#endif
#ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
#include <clang/Lex/PreprocessorOptions.h>
#else
#include <clang/Frontend/PreprocessorOptions.h>
#endif
#include <clang/Frontend/FrontendOptions.h>
#include <clang/Frontend/Utils.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/Pragma.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/SemaDiagnostic.h>
#include <clang/Parse/Parser.h>
#include <clang/Parse/ParseAST.h>
#include <isl/ctx.h>
#include <isl/constraint.h>
#include <pet.h>
#include "clang_compatibility.h"
#include "id.h"
#include "options.h"
#include "scan.h"
#include "print.h"
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
using namespace std;
using namespace clang;
using namespace clang::driver;
#ifdef HAVE_LLVM_OPTION_ARG_H
using namespace llvm::opt;
#endif
#ifdef HAVE_ADT_OWNINGPTR_H
#define unique_ptr llvm::OwningPtr
#endif
/* Called if we found something we didn't expect in one of the pragmas.
* We'll provide more informative warnings later.
*/
static void unsupported(Preprocessor &PP, SourceLocation loc)
{
DiagnosticsEngine &diag = PP.getDiagnostics();
unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
"unsupported");
DiagnosticBuilder B = diag.Report(loc, id);
}
static int get_int(const char *s)
{
return s[0] == '"' ? atoi(s + 1) : atoi(s);
}
static ValueDecl *get_value_decl(Sema &sema, Token &token)
{
IdentifierInfo *name;
Decl *decl;
if (token.isNot(tok::identifier))
return NULL;
name = token.getIdentifierInfo();
decl = sema.LookupSingleName(sema.TUScope, name,
token.getLocation(), Sema::LookupOrdinaryName);
return decl ? cast_or_null<ValueDecl>(decl) : NULL;
}
/* Handle pragmas of the form
*
* #pragma value_bounds identifier lower_bound upper_bound
*
* For each such pragma, add a mapping
* { identifier[] -> [i] : lower_bound <= i <= upper_bound }
* to value_bounds.
*/
struct PragmaValueBoundsHandler : public PragmaHandler {
Sema &sema;
isl_ctx *ctx;
isl_union_map *value_bounds;
PragmaValueBoundsHandler(isl_ctx *ctx, Sema &sema) :
PragmaHandler("value_bounds"), sema(sema), ctx(ctx) {
isl_space *space = isl_space_params_alloc(ctx, 0);
value_bounds = isl_union_map_empty(space);
}
~PragmaValueBoundsHandler() {
isl_union_map_free(value_bounds);
}
virtual void HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &ScopTok) {
isl_id *id;
isl_space *dim;
isl_map *map;
ValueDecl *vd;
Token token;
int lb;
int ub;
PP.Lex(token);
vd = get_value_decl(sema, token);
if (!vd) {
unsupported(PP, token.getLocation());
return;
}
PP.Lex(token);
if (!token.isLiteral()) {
unsupported(PP, token.getLocation());
return;
}
lb = get_int(token.getLiteralData());
PP.Lex(token);
if (!token.isLiteral()) {
unsupported(PP, token.getLocation());
return;
}
ub = get_int(token.getLiteralData());
dim = isl_space_alloc(ctx, 0, 0, 1);
map = isl_map_universe(dim);
map = isl_map_lower_bound_si(map, isl_dim_out, 0, lb);
map = isl_map_upper_bound_si(map, isl_dim_out, 0, ub);
id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
map = isl_map_set_tuple_id(map, isl_dim_in, id);
value_bounds = isl_union_map_add_map(value_bounds, map);
}
};
/* Given a variable declaration, check if it has an integer initializer
* and if so, add a parameter corresponding to the variable to "value"
* with its value fixed to the integer initializer and return the result.
*/
static __isl_give isl_set *extract_initialization(__isl_take isl_set *value,
ValueDecl *decl)
{
VarDecl *vd;
Expr *expr;
IntegerLiteral *il;
isl_val *v;
isl_ctx *ctx;
isl_id *id;
isl_space *space;
isl_set *set;
vd = cast<VarDecl>(decl);
if (!vd)
return value;
if (!vd->getType()->isIntegerType())
return value;
expr = vd->getInit();
if (!expr)
return value;
il = cast<IntegerLiteral>(expr);
if (!il)
return value;
ctx = isl_set_get_ctx(value);
id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
space = isl_space_params_alloc(ctx, 1);
space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
set = isl_set_universe(space);
v = PetScan::extract_int(ctx, il);
set = isl_set_fix_val(set, isl_dim_param, 0, v);
return isl_set_intersect(value, set);
}
/* Handle pragmas of the form
*
* #pragma parameter identifier lower_bound
* and
* #pragma parameter identifier lower_bound upper_bound
*
* For each such pragma, intersect the context with the set
* [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
*/
struct PragmaParameterHandler : public PragmaHandler {
Sema &sema;
isl_set *&context;
isl_set *&context_value;
PragmaParameterHandler(Sema &sema, isl_set *&context,
isl_set *&context_value) :
PragmaHandler("parameter"), sema(sema), context(context),
context_value(context_value) {}
virtual void HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &ScopTok) {
isl_id *id;
isl_ctx *ctx = isl_set_get_ctx(context);
isl_space *dim;
isl_set *set;
ValueDecl *vd;
Token token;
int lb;
int ub;
bool has_ub = false;
PP.Lex(token);
vd = get_value_decl(sema, token);
if (!vd) {
unsupported(PP, token.getLocation());
return;
}
PP.Lex(token);
if (!token.isLiteral()) {
unsupported(PP, token.getLocation());
return;
}
lb = get_int(token.getLiteralData());
PP.Lex(token);
if (token.isLiteral()) {
has_ub = true;
ub = get_int(token.getLiteralData());
} else if (token.isNot(tok::eod)) {
unsupported(PP, token.getLocation());
return;
}
id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
dim = isl_space_params_alloc(ctx, 1);
dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
set = isl_set_universe(dim);
set = isl_set_lower_bound_si(set, isl_dim_param, 0, lb);
if (has_ub)
set = isl_set_upper_bound_si(set, isl_dim_param, 0, ub);
context = isl_set_intersect(context, set);
context_value = extract_initialization(context_value, vd);
}
};
/* Handle pragmas of the form
*
* #pragma pencil independent
*
* For each such pragma, add an entry to the "independent" vector.
*/
struct PragmaPencilHandler : public PragmaHandler {
std::vector<Independent> &independent;
PragmaPencilHandler(std::vector<Independent> &independent) :
PragmaHandler("pencil"), independent(independent) {}
virtual void HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &PencilTok) {
Token token;
IdentifierInfo *info;
PP.Lex(token);
if (token.isNot(tok::identifier))
return;
info = token.getIdentifierInfo();
if (!info->isStr("independent"))
return;
PP.Lex(token);
if (token.isNot(tok::eod))
return;
SourceManager &SM = PP.getSourceManager();
SourceLocation sloc = PencilTok.getLocation();
unsigned line = SM.getExpansionLineNumber(sloc);
independent.push_back(Independent(line));
}
};
#ifdef HAVE_TRANSLATELINECOL
/* Return a SourceLocation for line "line", column "col" of file "FID".
*/
SourceLocation translateLineCol(SourceManager &SM, FileID FID, unsigned line,
unsigned col)
{
return SM.translateLineCol(FID, line, col);
}
#else
/* Return a SourceLocation for line "line", column "col" of file "FID".
*/
SourceLocation translateLineCol(SourceManager &SM, FileID FID, unsigned line,
unsigned col)
{
return SM.getLocation(SM.getFileEntryForID(FID), line, col);
}
#endif
/* List of pairs of #pragma scop and #pragma endscop locations.
*/
struct ScopLocList {
std::vector<ScopLoc> list;
/* Add a new start (#pragma scop) location to the list.
* If the last #pragma scop did not have a matching
* #pragma endscop then overwrite it.
* "start" points to the location of the scop pragma.
*/
void add_start(SourceManager &SM, SourceLocation start) {
ScopLoc loc;
loc.scop = start;
int line = SM.getExpansionLineNumber(start);
start = translateLineCol(SM, SM.getFileID(start), line, 1);
loc.start_line = line;
loc.start = SM.getFileOffset(start);
if (list.size() == 0 || list[list.size() - 1].end != 0)
list.push_back(loc);
else
list[list.size() - 1] = loc;
}
/* Set the end location (#pragma endscop) of the last pair
* in the list.
* If there is no such pair of if the end of that pair
* is already set, then ignore the spurious #pragma endscop.
* "end" points to the location of the endscop pragma.
*/
void add_end(SourceManager &SM, SourceLocation end) {
if (list.size() == 0 || list[list.size() - 1].end != 0)
return;
list[list.size() - 1].endscop = end;
int line = SM.getExpansionLineNumber(end);
end = translateLineCol(SM, SM.getFileID(end), line + 1, 1);
list[list.size() - 1].end = SM.getFileOffset(end);
}
};
/* Handle pragmas of the form
*
* #pragma scop
*
* In particular, store the location of the line containing
* the pragma in the list "scops".
*/
struct PragmaScopHandler : public PragmaHandler {
ScopLocList &scops;
PragmaScopHandler(ScopLocList &scops) :
PragmaHandler("scop"), scops(scops) {}
virtual void HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &ScopTok) {
SourceManager &SM = PP.getSourceManager();
SourceLocation sloc = ScopTok.getLocation();
scops.add_start(SM, sloc);
}
};
/* Handle pragmas of the form
*
* #pragma endscop
*
* In particular, store the location of the line following the one containing
* the pragma in the list "scops".
*/
struct PragmaEndScopHandler : public PragmaHandler {
ScopLocList &scops;
PragmaEndScopHandler(ScopLocList &scops) :
PragmaHandler("endscop"), scops(scops) {}
virtual void HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &EndScopTok) {
SourceManager &SM = PP.getSourceManager();
SourceLocation sloc = EndScopTok.getLocation();
scops.add_end(SM, sloc);
}
};
/* Handle pragmas of the form
*
* #pragma live-out identifier, identifier, ...
*
* Each identifier on the line is stored in live_out.
*/
struct PragmaLiveOutHandler : public PragmaHandler {
Sema &sema;
set<ValueDecl *> &live_out;
PragmaLiveOutHandler(Sema &sema, set<ValueDecl *> &live_out) :
PragmaHandler("live"), sema(sema), live_out(live_out) {}
virtual void HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &ScopTok) {
Token token;
PP.Lex(token);
if (token.isNot(tok::minus))
return;
PP.Lex(token);
if (token.isNot(tok::identifier) ||
!token.getIdentifierInfo()->isStr("out"))
return;
PP.Lex(token);
while (token.isNot(tok::eod)) {
ValueDecl *vd;
vd = get_value_decl(sema, token);
if (!vd) {
unsupported(PP, token.getLocation());
return;
}
live_out.insert(vd);
PP.Lex(token);
if (token.is(tok::comma))
PP.Lex(token);
}
}
};
/* For each array in "scop", set its value_bounds property
* based on the information in "value_bounds" and
* mark it as live_out if it appears in "live_out".
*/
static void update_arrays(struct pet_scop *scop,
__isl_take isl_union_map *value_bounds, set<ValueDecl *> &live_out)
{
set<ValueDecl *>::iterator lo_it;
isl_ctx *ctx = isl_union_map_get_ctx(value_bounds);
if (!scop) {
isl_union_map_free(value_bounds);
return;
}
for (int i = 0; i < scop->n_array; ++i) {
isl_id *id;
isl_space *space;
isl_map *bounds;
ValueDecl *decl;
pet_array *array = scop->arrays[i];
id = isl_set_get_tuple_id(array->extent);
decl = pet_id_get_decl(id);
space = isl_space_alloc(ctx, 0, 0, 1);
space = isl_space_set_tuple_id(space, isl_dim_in, id);
bounds = isl_union_map_extract_map(value_bounds, space);
if (!isl_map_plain_is_empty(bounds))
array->value_bounds = isl_map_range(bounds);
else
isl_map_free(bounds);
lo_it = live_out.find(decl);
if (lo_it != live_out.end())
array->live_out = 1;
}
isl_union_map_free(value_bounds);
}
/* Extract a pet_scop (if any) from each appropriate function.
* Each detected scop is passed to "fn".
* When autodetecting, at most one scop is extracted from each function.
* If "function" is not NULL, then we only extract a pet_scop if the
* name of the function matches.
* If "autodetect" is false, then we only extract if we have seen
* scop and endscop pragmas and if these are situated inside the function
* body.
*/
struct PetASTConsumer : public ASTConsumer {
Preprocessor &PP;
ASTContext &ast_context;
DiagnosticsEngine &diags;
ScopLocList &scops;
std::vector<Independent> independent;
const char *function;
pet_options *options;
isl_ctx *ctx;
isl_set *context;
isl_set *context_value;
set<ValueDecl *> live_out;
PragmaValueBoundsHandler *vb_handler;
isl_stat (*fn)(struct pet_scop *scop, void *user);
void *user;
bool error;
PetASTConsumer(isl_ctx *ctx, Preprocessor &PP, ASTContext &ast_context,
DiagnosticsEngine &diags, ScopLocList &scops,
const char *function, pet_options *options,
isl_stat (*fn)(struct pet_scop *scop, void *user), void *user) :
PP(PP), ast_context(ast_context), diags(diags),
scops(scops), function(function), options(options),
ctx(ctx),
vb_handler(NULL), fn(fn), user(user), error(false)
{
isl_space *space;
space = isl_space_params_alloc(ctx, 0);
context = isl_set_universe(isl_space_copy(space));
context_value = isl_set_universe(space);
}
~PetASTConsumer() {
isl_set_free(context);
isl_set_free(context_value);
}
void handle_value_bounds(Sema *sema) {
vb_handler = new PragmaValueBoundsHandler(ctx, *sema);
PP.AddPragmaHandler(vb_handler);
}
/* Add all pragma handlers to this->PP.
* The pencil pragmas are only handled if the pencil option is set.
*/
void add_pragma_handlers(Sema *sema) {
PP.AddPragmaHandler(new PragmaParameterHandler(*sema, context,
context_value));
if (options->pencil) {
PragmaHandler *PH;
PH = new PragmaPencilHandler(independent);
PP.AddPragmaHandler(PH);
}
handle_value_bounds(sema);
}
__isl_give isl_union_map *get_value_bounds() {
return isl_union_map_copy(vb_handler->value_bounds);
}
/* Pass "scop" to "fn" after performing some postprocessing.
* In particular, add the context and value_bounds constraints
* speficied through pragmas, add reference identifiers and
* reset user pointers on parameters and tuple ids.
*
* If "scop" does not contain any statements and autodetect
* is turned on, then skip it.
*/
void call_fn(pet_scop *scop) {
if (!scop) {
error = true;
return;
}
if (diags.hasErrorOccurred()) {
error = true;
pet_scop_free(scop);
return;
}
if (options->autodetect && scop->n_stmt == 0) {
pet_scop_free(scop);
return;
}
scop->context = isl_set_intersect(scop->context,
isl_set_copy(context));
scop->context_value = isl_set_intersect(scop->context_value,
isl_set_copy(context_value));
update_arrays(scop, get_value_bounds(), live_out);
scop = pet_scop_add_ref_ids(scop);
scop = pet_scop_anonymize(scop);
if (fn(scop, user) < 0)
error = true;
}
/* For each explicitly marked scop (using pragmas),
* extract the scop and call "fn" on it if it is inside "fd".
*/
void scan_scops(FunctionDecl *fd) {
unsigned start, end;
vector<ScopLoc>::iterator it;
isl_union_map *vb = vb_handler->value_bounds;
SourceManager &SM = PP.getSourceManager();
pet_scop *scop;
if (scops.list.size() == 0)
return;
start = SM.getFileOffset(begin_loc(fd));
end = SM.getFileOffset(end_loc(fd));
for (it = scops.list.begin(); it != scops.list.end(); ++it) {
ScopLoc loc = *it;
if (!loc.end)
continue;
if (start > loc.end)
continue;
if (end < loc.start)
continue;
PetScan ps(PP, ast_context, fd, loc, options,
isl_union_map_copy(vb), independent);
scop = ps.scan(fd);
call_fn(scop);
}
}
virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef dg) {
DeclGroupRef::iterator it;
if (error)
return HandleTopLevelDeclContinue;
for (it = dg.begin(); it != dg.end(); ++it) {
isl_union_map *vb = vb_handler->value_bounds;
FunctionDecl *fd = dyn_cast<clang::FunctionDecl>(*it);
if (!fd)
continue;
if (!fd->hasBody())
continue;
if (function &&
fd->getNameInfo().getAsString() != function)
continue;
if (options->autodetect) {
ScopLoc loc;
pet_scop *scop;
PetScan ps(PP, ast_context, fd, loc, options,
isl_union_map_copy(vb),
independent);
scop = ps.scan(fd);
if (!scop)
continue;
call_fn(scop);
continue;
}
scan_scops(fd);
}
return HandleTopLevelDeclContinue;
}
};
static const char *ResourceDir =
CLANG_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
static const char *implicit_functions[] = {
"min", "max", "intMod", "intCeil", "intFloor", "ceild", "floord"
};
static const char *pencil_implicit_functions[] = {
"imin", "umin", "imax", "umax", "__pencil_kill"
};
/* Should "ident" be treated as an implicit function?
* If "pencil" is set, then also allow pencil specific builtins.
*/
static bool is_implicit(const IdentifierInfo *ident, int pencil)
{
const char *name = ident->getNameStart();
for (size_t i = 0; i < ARRAY_SIZE(implicit_functions); ++i)
if (!strcmp(name, implicit_functions[i]))
return true;
if (!pencil)
return false;
for (size_t i = 0; i < ARRAY_SIZE(pencil_implicit_functions); ++i)
if (!strcmp(name, pencil_implicit_functions[i]))
return true;
return false;
}
/* Ignore implicit function declaration warnings on
* "min", "max", "ceild" and "floord" as we detect and handle these
* in PetScan.
* If "pencil" is set, then also ignore them on pencil specific
* builtins.
*/
struct MyDiagnosticPrinter : public TextDiagnosticPrinter {
const DiagnosticOptions *DiagOpts;
int pencil;
#ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
MyDiagnosticPrinter(DiagnosticOptions *DO, int pencil) :
TextDiagnosticPrinter(llvm::errs(), DO), pencil(pencil) {}
virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
return new MyDiagnosticPrinter(&Diags.getDiagnosticOptions(),
pencil);
}
#else
MyDiagnosticPrinter(const DiagnosticOptions &DO, int pencil) :
DiagOpts(&DO), TextDiagnosticPrinter(llvm::errs(), DO),
pencil(pencil) {}
virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
return new MyDiagnosticPrinter(*DiagOpts, pencil);
}
#endif
virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
const DiagnosticInfo &info) {
if (info.getID() == diag::ext_implicit_function_decl &&
info.getNumArgs() >= 1 &&
info.getArgKind(0) == DiagnosticsEngine::ak_identifierinfo &&
is_implicit(info.getArgIdentifier(0), pencil))
/* ignore warning */;
else
TextDiagnosticPrinter::HandleDiagnostic(level, info);
}
};
#ifdef USE_ARRAYREF
#ifdef HAVE_CXXISPRODUCTION
static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
{
return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
"", false, false, Diags);
}
#elif defined(HAVE_ISPRODUCTION)
static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
{
return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
"", false, Diags);
}
#elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
{
return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
"", Diags);
}
#else
static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
{
return new Driver(binary, llvm::sys::getDefaultTargetTriple(), Diags);
}
#endif
namespace clang { namespace driver { class Job; } }
/* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
* We fix this with a simple overloaded function here.
*/
struct ClangAPI {
static Job *command(Job *J) { return J; }
static Job *command(Job &J) { return &J; }
static Command *command(Command &C) { return &C; }
};
/* Create a CompilerInvocation object that stores the command line
* arguments constructed by the driver.
* The arguments are mainly useful for setting up the system include
* paths on newer clangs and on some platforms.
*/
static CompilerInvocation *construct_invocation(const char *filename,
DiagnosticsEngine &Diags)
{
const char *binary = CLANG_PREFIX"/bin/clang";
const unique_ptr<Driver> driver(construct_driver(binary, Diags));
std::vector<const char *> Argv;
Argv.push_back(binary);
Argv.push_back(filename);
const unique_ptr<Compilation> compilation(
driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
JobList &Jobs = compilation->getJobs();
if (Jobs.size() < 1)
return NULL;
Command *cmd = cast<Command>(ClangAPI::command(*Jobs.begin()));
if (strcmp(cmd->getCreator().getName(), "clang"))
return NULL;
const ArgStringList *args = &cmd->getArguments();
CompilerInvocation *invocation = new CompilerInvocation;
CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
args->data() + args->size(),
Diags);
return invocation;
}
#else
static CompilerInvocation *construct_invocation(const char *filename,
DiagnosticsEngine &Diags)
{
return NULL;
}
#endif
#ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
static MyDiagnosticPrinter *construct_printer(CompilerInstance *Clang,
int pencil)
{
return new MyDiagnosticPrinter(new DiagnosticOptions(), pencil);
}
#else
static MyDiagnosticPrinter *construct_printer(CompilerInstance *Clang,
int pencil)
{
return new MyDiagnosticPrinter(Clang->getDiagnosticOpts(), pencil);
}
#endif
#ifdef CREATETARGETINFO_TAKES_SHARED_PTR
static TargetInfo *create_target_info(CompilerInstance *Clang,
DiagnosticsEngine &Diags)
{
shared_ptr<TargetOptions> TO = Clang->getInvocation().TargetOpts;
TO->Triple = llvm::sys::getDefaultTargetTriple();
return TargetInfo::CreateTargetInfo(Diags, TO);
}
#elif defined(CREATETARGETINFO_TAKES_POINTER)
static TargetInfo *create_target_info(CompilerInstance *Clang,
DiagnosticsEngine &Diags)
{
TargetOptions &TO = Clang->getTargetOpts();
TO.Triple = llvm::sys::getDefaultTargetTriple();
return TargetInfo::CreateTargetInfo(Diags, &TO);
}
#else
static TargetInfo *create_target_info(CompilerInstance *Clang,
DiagnosticsEngine &Diags)
{
TargetOptions &TO = Clang->getTargetOpts();
TO.Triple = llvm::sys::getDefaultTargetTriple();
return TargetInfo::CreateTargetInfo(Diags, TO);
}
#endif
#ifdef CREATEDIAGNOSTICS_TAKES_ARG
static void create_diagnostics(CompilerInstance *Clang)
{
Clang->createDiagnostics(0, NULL);
}
#else
static void create_diagnostics(CompilerInstance *Clang)
{
Clang->createDiagnostics();
}
#endif
#ifdef CREATEPREPROCESSOR_TAKES_TUKIND
static void create_preprocessor(CompilerInstance *Clang)
{
Clang->createPreprocessor(TU_Complete);
}
#else
static void create_preprocessor(CompilerInstance *Clang)
{
Clang->createPreprocessor();
}
#endif
#ifdef ADDPATH_TAKES_4_ARGUMENTS
void add_path(HeaderSearchOptions &HSO, string Path)
{
HSO.AddPath(Path, frontend::Angled, false, false);
}
#else
void add_path(HeaderSearchOptions &HSO, string Path)
{
HSO.AddPath(Path, frontend::Angled, true, false, false);
}
#endif
#ifdef HAVE_SETMAINFILEID
static void create_main_file_id(SourceManager &SM, const FileEntry *file)
{
SM.setMainFileID(SM.createFileID(file, SourceLocation(),
SrcMgr::C_User));
}
#else
static void create_main_file_id(SourceManager &SM, const FileEntry *file)
{
SM.createMainFileID(file);
}
#endif
#ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
static void set_lang_defaults(CompilerInstance *Clang)
{
PreprocessorOptions &PO = Clang->getPreprocessorOpts();
TargetOptions &TO = Clang->getTargetOpts();
llvm::Triple T(TO.Triple);
CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C, T, PO,
LangStandard::lang_unspecified);
}
#else
static void set_lang_defaults(CompilerInstance *Clang)
{