-
Notifications
You must be signed in to change notification settings - Fork 3
/
Transform.cpp
2705 lines (2573 loc) · 120 KB
/
Transform.cpp
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
#include <clang/Tooling/Tooling.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/Sema/Scope.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/AST/Stmt.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/Decl.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/Arg.h>
#include <clang/Driver/Options.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/FileSystem.h>
#include <string>
#include <cctype>
#include <memory>
#include "../../lib/Sema/TreeTransform.h"
#include <clang/AST/PrettyPrinter.h>
using namespace clang;
using namespace clang::tooling;
using llvm::APInt;
namespace {
struct is_ident_char {
typedef bool result_type;
typedef char argument_type;
bool operator()(char arg) const {
return std::isalnum(arg) || arg == '_';
}
};
std::string get_file_id(const std::string& filename) {
uint32_t seed = 0;
for(std::string::const_iterator iter = filename.begin(), end = filename.end(); iter != end; ++iter) {
seed ^= uint32_t(*iter) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
std::string as_identifier(llvm::sys::path::stem(filename));
std::replace_if(as_identifier.begin(), as_identifier.end(), std::not1(is_ident_char()), '_');
return (as_identifier + "_" + llvm::Twine(seed)).str();
}
/* Copied from DeclPrinter.cpp */
static QualType GetBaseType(QualType T) {
// FIXME: This should be on the Type class!
QualType BaseType = T;
while (!BaseType->isSpecifierType()) {
if (isa<TypedefType>(BaseType))
break;
else if (const PointerType* PTy = BaseType->getAs<PointerType>())
BaseType = PTy->getPointeeType();
else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
BaseType = BPy->getPointeeType();
else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
BaseType = ATy->getElementType();
else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
BaseType = FTy->getReturnType();
else if (const VectorType *VTy = BaseType->getAs<VectorType>())
BaseType = VTy->getElementType();
else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
BaseType = RTy->getPointeeType();
else
llvm_unreachable("Unknown declarator!");
}
return BaseType;
}
static QualType getDeclType(Decl* D) {
if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
return TDD->getUnderlyingType();
if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
return VD->getType();
return QualType();
}
typedef enum {
CFNK_PSHARED = 0,
CFNK_PSHARED_STRICT,
CFNK_SHARED,
CFNK_SHARED_STRICT
} UPCRCommGroupKind;
struct UPCRCommFn {
private:
FunctionDecl* Decls[4];
public:
UPCRCommFn() : Decls() {}
FunctionDecl*& operator[](int idx) {
return Decls[idx];
}
FunctionDecl*& operator()(bool Phaseless, bool Strict = false) {
return Decls[Phaseless? (Strict? CFNK_PSHARED_STRICT : CFNK_PSHARED)
: (Strict? CFNK_SHARED_STRICT : CFNK_SHARED)];
}
};
struct UPCRDecls {
FunctionDecl * upcr_notify;
FunctionDecl * upcr_wait;
FunctionDecl * upcr_barrier;
FunctionDecl * upcr_poll;
FunctionDecl * upcr_mythread;
FunctionDecl * upcr_threads;
FunctionDecl * upcr_hasMyAffinity_pshared;
FunctionDecl * upcr_hasMyAffinity_shared;
FunctionDecl * UPCR_BEGIN_FUNCTION;
FunctionDecl * UPCR_EXIT_FUNCTION;
FunctionDecl * UPCRT_STARTUP_PSHALLOC;
FunctionDecl * UPCRT_STARTUP_SHALLOC;
FunctionDecl * upcr_startup_pshalloc;
FunctionDecl * upcr_startup_shalloc;
FunctionDecl * UPCR_TLD_ADDR;
FunctionDecl * UPCR_ADD_SHARED;
FunctionDecl * UPCR_ADD_PSHAREDI;
FunctionDecl * UPCR_ADD_PSHARED1;
FunctionDecl * UPCR_INC_SHARED;
FunctionDecl * UPCR_INC_PSHAREDI;
FunctionDecl * UPCR_INC_PSHARED1;
FunctionDecl * UPCR_SUB_SHARED;
FunctionDecl * UPCR_SUB_PSHAREDI;
FunctionDecl * UPCR_SUB_PSHARED1;
FunctionDecl * UPCR_ISEQUAL_SHARED_SHARED;
FunctionDecl * UPCR_ISEQUAL_SHARED_PSHARED;
FunctionDecl * UPCR_ISEQUAL_PSHARED_SHARED;
FunctionDecl * UPCR_ISEQUAL_PSHARED_PSHARED;
FunctionDecl * UPCR_PSHARED_TO_LOCAL;
FunctionDecl * UPCR_SHARED_TO_LOCAL;
FunctionDecl * UPCR_ISNULL_PSHARED;
FunctionDecl * UPCR_ISNULL_SHARED;
FunctionDecl * UPCR_SHARED_TO_PSHARED;
FunctionDecl * UPCR_PSHARED_TO_SHARED;
FunctionDecl * UPCR_SHARED_RESETPHASE;
FunctionDecl * UPCR_ADDRFIELD_SHARED;
FunctionDecl * UPCR_ADDRFIELD_PSHARED;
UPCRCommFn UPCR_GET;
UPCRCommFn UPCR_GET_IVAL;
UPCRCommFn UPCR_GET_FVAL;
UPCRCommFn UPCR_GET_DVAL;
UPCRCommFn UPCR_PUT;
UPCRCommFn UPCR_PUT_IVAL;
UPCRCommFn UPCR_PUT_FVAL;
UPCRCommFn UPCR_PUT_DVAL;
VarDecl * upcrt_forall_control;
VarDecl * upcr_null_shared;
VarDecl * upcr_null_pshared;
QualType upcr_shared_ptr_t;
QualType upcr_pshared_ptr_t;
QualType upcr_startup_shalloc_t;
QualType upcr_startup_pshalloc_t;
QualType upcr_register_value_t;
SourceLocation FakeLocation;
explicit UPCRDecls(ASTContext& Context) {
SourceManager& SourceMgr = Context.getSourceManager();
FakeLocation = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
// types
// Make sure that the size and alignment are correct.
QualType SharedPtrTy = Context.getPointerType(Context.getSharedType(Context.VoidTy));
upcr_shared_ptr_t = CreateTypedefType(Context, "upcr_shared_ptr_t", SharedPtrTy);
upcr_pshared_ptr_t = CreateTypedefType(Context, "upcr_pshared_ptr_t", SharedPtrTy);
upcr_startup_shalloc_t = CreateTypedefType(Context, "upcr_startup_shalloc_t");
upcr_startup_pshalloc_t = CreateTypedefType(Context, "upcr_startup_pshalloc_t");
// FIXME: This is a fair assumption, but should really get true type
upcr_register_value_t = CreateTypedefType(Context, "upcr_register_value_t", Context.getUIntPtrType());
// upcr_notify
{
QualType argTypes[] = { Context.IntTy, Context.IntTy };
upcr_notify = CreateFunction(Context, "upcr_notify", Context.VoidTy, argTypes, 2);
}
// upcr_wait
{
QualType argTypes[] = { Context.IntTy, Context.IntTy };
upcr_wait = CreateFunction(Context, "upcr_wait", Context.VoidTy, argTypes, 2);
}
// upcr_barrier
{
QualType argTypes[] = { Context.IntTy, Context.IntTy };
upcr_barrier = CreateFunction(Context, "upcr_barrier", Context.VoidTy, argTypes, 2);
}
// upcr_poll
{
upcr_poll = CreateFunction(Context, "upcr_poll", Context.VoidTy, 0, 0);
}
// upcr_mythread
{
upcr_mythread = CreateFunction(Context, "upcr_mythread", Context.IntTy, 0, 0);
}
// upcr_threads
{
upcr_threads = CreateFunction(Context, "upcr_threads", Context.IntTy, 0, 0);
}
// upcr_hasMyAffinity_pshared
{
QualType argTypes[] = { upcr_pshared_ptr_t };
upcr_hasMyAffinity_pshared = CreateFunction(Context, "upcr_hasMyAffinity_pshared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_hasMyAffinity_shared
{
QualType argTypes[] = { upcr_shared_ptr_t };
upcr_hasMyAffinity_shared = CreateFunction(Context, "upcr_hasMyAffinity_shared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADD_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.IntTy, Context.IntTy };
UPCR_ADD_SHARED = CreateFunction(Context, "upcr_add_shared", upcr_shared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADD_PSHAREDI
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_ADD_PSHAREDI = CreateFunction(Context, "upcr_add_psharedI", upcr_pshared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADD_PSHARED1
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_ADD_PSHARED1 = CreateFunction(Context, "upcr_add_pshared1", upcr_pshared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_INC_SHARED
{
QualType argTypes[] = { Context.getPointerType(upcr_shared_ptr_t), Context.IntTy, Context.IntTy, Context.IntTy };
UPCR_INC_SHARED = CreateFunction(Context, "upcr_inc_shared", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_INC_PSHAREDI
{
QualType argTypes[] = { Context.getPointerType(upcr_pshared_ptr_t), Context.IntTy, Context.IntTy };
UPCR_INC_PSHAREDI = CreateFunction(Context, "upcr_inc_psharedI", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_INC_PSHARED1
{
QualType argTypes[] = { Context.getPointerType(upcr_pshared_ptr_t), Context.IntTy, Context.IntTy };
UPCR_INC_PSHARED1 = CreateFunction(Context, "upcr_inc_pshared1", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SUB_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, upcr_shared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_SUB_SHARED = CreateFunction(Context, "upcr_sub_shared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SUB_PSHAREDI
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_pshared_ptr_t, Context.IntTy };
UPCR_SUB_PSHAREDI = CreateFunction(Context, "upcr_sub_psharedI", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SUB_PSHARED1
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_pshared_ptr_t, Context.IntTy };
UPCR_SUB_PSHARED1 = CreateFunction(Context, "upcr_sub_pshared1", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_SHARED_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, upcr_shared_ptr_t };
UPCR_ISEQUAL_SHARED_SHARED = CreateFunction(Context, "upcr_isequal_shared_shared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_SHARED_PSHARED
{
QualType argTypes[] = { upcr_shared_ptr_t, upcr_pshared_ptr_t };
UPCR_ISEQUAL_SHARED_PSHARED = CreateFunction(Context, "upcr_isequal_shared_pshared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_PSHARED_SHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_shared_ptr_t };
UPCR_ISEQUAL_PSHARED_SHARED = CreateFunction(Context, "upcr_isequal_pshared_shared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISEQUAL_PSHARED_PSHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t, upcr_pshared_ptr_t };
UPCR_ISEQUAL_PSHARED_PSHARED = CreateFunction(Context, "upcr_isequal_pshared_pshared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SHARED_TO_LOCAL
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_SHARED_TO_LOCAL = CreateFunction(Context, "upcr_shared_to_local", Context.VoidPtrTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PSHARED_TO_LOCAL
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_PSHARED_TO_LOCAL = CreateFunction(Context, "upcr_pshared_to_local", Context.VoidPtrTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISNULL_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_ISNULL_SHARED = CreateFunction(Context, "upcr_isnull_shared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ISNULL_PSHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_ISNULL_PSHARED = CreateFunction(Context, "upcr_isnull_pshared", Context.IntTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SHARED_TO_PSHARED
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_SHARED_TO_PSHARED = CreateFunction(Context, "upcr_shared_to_pshared", upcr_pshared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_PSHARED_TO_SHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_PSHARED_TO_SHARED = CreateFunction(Context, "upcr_pshared_to_shared", upcr_shared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_SHARED_RESETPHASE
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_SHARED_RESETPHASE = CreateFunction(Context, "upcr_shared_resetphase", upcr_shared_ptr_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_ADDRFIELD_SHARED
{
QualType argTypes[] = { upcr_shared_ptr_t };
UPCR_ADDRFIELD_SHARED = CreateFunction(Context, "upcr_addrfield_shared", Context.getUIntPtrType(), argTypes, 1);
}
// UPCR_ADDRFIELD_PSHARED
{
QualType argTypes[] = { upcr_pshared_ptr_t };
UPCR_ADDRFIELD_PSHARED = CreateFunction(Context, "upcr_addrfield_pshared", Context.getUIntPtrType(), argTypes, 1);
}
// UPCR_BEGIN_FUNCTION
{
UPCR_BEGIN_FUNCTION = CreateFunction(Context, "UPCR_BEGIN_FUNCTION", Context.VoidTy, NULL, 0);
}
// UPCR_EXIT_FUNCTION
{
UPCR_EXIT_FUNCTION = CreateFunction(Context, "UPCR_EXIT_FUNCTION", Context.VoidTy, NULL, 0);
}
// UPCRT_STARTUP_PSHALLOC
{
QualType argTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy, Context.IntTy, Context.IntTy, Context. getPointerType(Context.getConstType(Context.CharTy)) };
UPCRT_STARTUP_PSHALLOC = CreateFunction(Context, "UPCRT_STARTUP_PSHALLOC", upcr_startup_pshalloc_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCRT_STARTUP_SHALLOC
{
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.IntTy, Context.IntTy, Context.IntTy, Context. getPointerType(Context.getConstType(Context.CharTy)) };
UPCRT_STARTUP_SHALLOC = CreateFunction(Context, "UPCRT_STARTUP_SHALLOC", upcr_startup_shalloc_t, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_startup_pshalloc
{
QualType argTypes[] = { Context.getPointerType(upcr_startup_pshalloc_t), Context.IntTy };
upcr_startup_pshalloc = CreateFunction(Context, "upcr_startup_pshalloc", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// upcr_startup_shalloc
{
QualType argTypes[] = { Context.getPointerType(upcr_startup_shalloc_t), Context.IntTy };
upcr_startup_shalloc = CreateFunction(Context, "upcr_startup_shalloc", Context.VoidTy, argTypes, sizeof(argTypes)/sizeof(argTypes[0]));
}
// UPCR_TLD_ADDR
{
UPCR_TLD_ADDR = CreateFunction(Context, "UPCR_TLD_ADDR", Context.VoidPtrTy, NULL, 0, true/*variadic*/);
}
// UPCR_GET_{,P}SHARED{,_STRICT}
{
QualType pargTypes[] = { Context.VoidPtrTy, upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET[CFNK_PSHARED] = CreateFunction(Context, "upcr_get_pshared", Context.VoidTy, pargTypes, 4);
UPCR_GET[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_get_pshared_strict", Context.VoidTy, pargTypes, 4);
QualType argTypes[] = { Context.VoidPtrTy, upcr_shared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET[CFNK_SHARED] = CreateFunction(Context, "upcr_get_shared", Context.VoidTy, argTypes, 4);
UPCR_GET[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_get_shared_strict", Context.VoidTy, argTypes, 4);
}
// UPCR_GET_{,P}SHARED_IVAL{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET_IVAL[CFNK_PSHARED] = CreateFunction(Context, "upcr_get_pshared_val", upcr_register_value_t, pargTypes, 3);
UPCR_GET_IVAL[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_get_pshared_val_strict", upcr_register_value_t, pargTypes, 3);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.IntTy };
UPCR_GET_IVAL[CFNK_SHARED] = CreateFunction(Context, "upcr_get_shared_val", upcr_register_value_t, argTypes, 3);
UPCR_GET_IVAL[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_get_shared_val_strict", upcr_register_value_t, argTypes, 3);
}
// UPCR_GET_{,P}SHARED_FVAL{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy };
UPCR_GET_FVAL[CFNK_PSHARED] = CreateFunction(Context, "upcr_get_pshared_floatval", Context.FloatTy, pargTypes, 2);
UPCR_GET_FVAL[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_get_pshared_floatval_strict", Context.FloatTy, pargTypes, 2);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy };
UPCR_GET_FVAL[CFNK_SHARED] = CreateFunction(Context, "upcr_get_shared_floatval", Context.FloatTy, argTypes, 2);
UPCR_GET_FVAL[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_get_shared_floatval_strict", Context.FloatTy, argTypes, 2);
}
// UPCR_GET_{,P}SHARED_DVAL{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy };
UPCR_GET_DVAL[CFNK_PSHARED] = CreateFunction(Context, "upcr_get_pshared_doubleval", Context.DoubleTy, pargTypes, 2);
UPCR_GET_DVAL[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_get_pshared_doubleval_strict", Context.DoubleTy, pargTypes, 2);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy };
UPCR_GET_DVAL[CFNK_SHARED] = CreateFunction(Context, "upcr_get_shared_doubleval", Context.DoubleTy, argTypes, 2);
UPCR_GET_DVAL[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_get_shared_doubleval_strict", Context.DoubleTy, argTypes, 2);
}
// UPCR_PUT_{,P}SHARED{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
UPCR_PUT[CFNK_PSHARED] = CreateFunction(Context, "upcr_put_pshared", Context.VoidTy, pargTypes, 4);
UPCR_PUT[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_put_pshared_strict", Context.VoidTy, pargTypes, 4);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.VoidPtrTy, Context.IntTy };
UPCR_PUT[CFNK_SHARED] = CreateFunction(Context, "upcr_put_shared", Context.VoidTy, argTypes, 4);
UPCR_PUT[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_put_shared_strict", Context.VoidTy, argTypes, 4);
}
// UPCR_PUT_{,P}SHARED_IVAL{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy, upcr_register_value_t, Context.IntTy };
UPCR_PUT_IVAL[CFNK_PSHARED] = CreateFunction(Context, "upcr_put_pshared_val", Context.VoidTy, pargTypes, 4);
UPCR_PUT_IVAL[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_put_pshared_val_strict", Context.VoidTy, pargTypes, 4);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, upcr_register_value_t, Context.IntTy };
UPCR_PUT_IVAL[CFNK_SHARED] = CreateFunction(Context, "upcr_put_shared_val", Context.VoidTy, argTypes, 4);
UPCR_PUT_IVAL[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_put_shared_val_strict", Context.VoidTy, argTypes, 4);
}
// UPCR_PUT_{,P}SHARED_FVAL{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.FloatTy };
UPCR_PUT_FVAL[CFNK_PSHARED] = CreateFunction(Context, "upcr_put_pshared_floatval", Context.VoidTy, pargTypes, 3);
UPCR_PUT_FVAL[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_put_pshared_floatval_strict", Context.VoidTy, pargTypes, 3);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.FloatTy };
UPCR_PUT_FVAL[CFNK_SHARED] = CreateFunction(Context, "upcr_put_shared_floatval", Context.VoidTy, argTypes, 3);
UPCR_PUT_FVAL[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_put_shared_floatval_strict", Context.VoidTy, argTypes, 3);
}
// UPCR_PUT_{,P}SHARED_DVAL{,_STRICT}
{
QualType pargTypes[] = { upcr_pshared_ptr_t, Context.IntTy, Context.DoubleTy };
UPCR_PUT_DVAL[CFNK_PSHARED] = CreateFunction(Context, "upcr_put_pshared_doubleval", Context.VoidTy, pargTypes, 3);
UPCR_PUT_DVAL[CFNK_PSHARED_STRICT] = CreateFunction(Context, "upcr_put_pshared_doubleval_strict", Context.VoidTy, pargTypes, 3);
QualType argTypes[] = { upcr_shared_ptr_t, Context.IntTy, Context.DoubleTy };
UPCR_PUT_DVAL[CFNK_SHARED] = CreateFunction(Context, "upcr_put_shared_doubleval", Context.VoidTy, argTypes, 3);
UPCR_PUT_DVAL[CFNK_SHARED_STRICT] = CreateFunction(Context, "upcr_put_shared_doubleval_strict", Context.VoidTy, argTypes, 3);
}
// upcrt_forall_control
{
DeclContext *DC = Context.getTranslationUnitDecl();
upcrt_forall_control = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get("upcrt_forall_control"), Context.IntTy, Context.getTrivialTypeSourceInfo(Context.IntTy), SC_Extern);
}
// upcr_null_shared
{
DeclContext *DC = Context.getTranslationUnitDecl();
upcr_null_shared = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get("upcr_null_shared"), upcr_shared_ptr_t, Context.getTrivialTypeSourceInfo(upcr_shared_ptr_t), SC_Extern);
}
// upcr_null_pshared
{
DeclContext *DC = Context.getTranslationUnitDecl();
upcr_null_pshared = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get("upcr_null_pshared"), upcr_pshared_ptr_t, Context.getTrivialTypeSourceInfo(upcr_pshared_ptr_t), SC_Extern);
}
}
FunctionDecl *CreateFunction(ASTContext& Context, StringRef name, QualType RetType, QualType * argTypes, int numArgs, bool Variadic = false) {
DeclContext *DC = Context.getTranslationUnitDecl();
FunctionProtoType::ExtProtoInfo Info;
if(Variadic) {
Info.Variadic = true;
}
QualType Ty = Context.getFunctionType(RetType, llvm::makeArrayRef(argTypes, numArgs), Info);
FunctionDecl *Result = FunctionDecl::Create(Context, DC, FakeLocation, FakeLocation, DeclarationName(&Context.Idents.get(name)), Ty, Context.getTrivialTypeSourceInfo(Ty), SC_Extern);
llvm::SmallVector<ParmVarDecl *, 4> Params;
for(int i = 0; i < numArgs; ++i) {
Params.push_back(ParmVarDecl::Create(Context, Result, SourceLocation(), SourceLocation(), 0, argTypes[i], 0, SC_None, 0));
Params[i]->setScopeInfo(0, i);
}
Result->setParams(Params);
return Result;
}
QualType CreateTypedefType(ASTContext& Context, StringRef name) {
return CreateTypedefType(Context, name, Context.IntTy);
}
QualType CreateTypedefType(ASTContext& Context, StringRef name, QualType BaseTy) {
DeclContext *DC = Context.getTranslationUnitDecl();
TypedefDecl *Typedef = TypedefDecl::Create(Context, DC, SourceLocation(), SourceLocation(), &Context.Idents.get(name), Context.getTrivialTypeSourceInfo(BaseTy));
return Context.getTypedefType(Typedef);
}
};
class SubstituteType : public clang::TreeTransform<SubstituteType> {
typedef TreeTransform<SubstituteType> TreeTransformS;
public:
SubstituteType(Sema &S, QualType F, QualType T) : TreeTransformS(S), From(F), To(T) {}
QualType TransformType(TypeLocBuilder& TLB, TypeLoc Ty) {
if(SemaRef.Context.hasSameType(Ty.getType(), From)) {
Ty = SemaRef.Context.getTrivialTypeSourceInfo(To)->getTypeLoc();
}
return TreeTransformS::TransformType(TLB, Ty);
}
using TreeTransformS::TransformType;
private:
QualType From;
QualType To;
};
// This class checks for arrays and other constructs that require a typedef
// when used with UPCR_TLD_DEFINE.
class CheckForArray : public clang::RecursiveASTVisitor<CheckForArray> {
public:
CheckForArray() : Found(false) {}
bool VisitArrayType(ArrayType *) {
Found = true;
return false;
}
bool VisitFunctionType(FunctionType *) {
Found = true;
return false;
}
bool Found;
};
// The following expressions require dynamic initialization:
// &global (If global is shared, then the address is set
// dynamically, if it's local, then the address is thread
// specific.
class CheckForDynamicInitializer : public clang::RecursiveASTVisitor<CheckForDynamicInitializer> {
public:
CheckForDynamicInitializer() : Found(false) {}
bool VisitDeclRefExpr(DeclRefExpr *E) {
if(isa<VarDecl>(E->getDecl())) {
Found = true;
return false;
}
return true;
}
bool VisitExpr(Expr *E) {
if(E->getType()->hasPointerToSharedRepresentation()) {
Found = true;
return false;
}
return true;
}
bool Found;
};
// Determines whether a typedef can safely be moved to
// the global scope. Variable arrays cannot be, since
// they depend on local variables.
class CheckForLocalType : public clang::RecursiveASTVisitor<CheckForLocalType> {
public:
CheckForLocalType() : Found(false) {}
bool VisitVariableArrayType(VariableArrayType *) {
Found = true;
return false;
}
bool VisitUPCThreadArrayType(UPCThreadArrayType *) {
Found = true;
return false;
}
bool Found;
};
class RemoveUPCTransform : public clang::TreeTransform<RemoveUPCTransform> {
typedef TreeTransform<RemoveUPCTransform> TreeTransformUPC;
private:
bool haveOffsetOf;
bool haveVAArg;
public:
RemoveUPCTransform(Sema& S, UPCRDecls* D, const std::string& fileid)
: TreeTransformUPC(S), AnonRecordID(0), StaticLocalVarID(0),
Decls(D), FileString(fileid) {
haveOffsetOf = haveVAArg = false;
}
bool HaveOffsetOf() { return haveOffsetOf; }
ExprResult TransformOffsetOfExpr(OffsetOfExpr *E) {
haveOffsetOf = true;
return TreeTransformUPC::TransformOffsetOfExpr(E);
}
bool HaveVAArg() { return haveVAArg; }
ExprResult TransformVAArgExpr(VAArgExpr *E) {
haveVAArg = true;
return TreeTransformUPC::TransformVAArgExpr(E);
}
bool AlwaysRebuild() { return true; }
ExprResult BuildParens(Expr * E) {
return SemaRef.ActOnParenExpr(SourceLocation(), SourceLocation(), E);
}
ExprResult BuildComma(Expr * LHS, Expr * RHS) {
return SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Comma, LHS, RHS);
}
// TreeTransform ignores AlwayRebuild for literals
ExprResult TransformIntegerLiteral(IntegerLiteral *E) {
return IntegerLiteral::Create(SemaRef.Context, E->getValue(), E->getType(), E->getLocation());
}
ExprResult BuildUPCRCall(FunctionDecl *FD, std::vector<Expr*>& args, SourceLocation Loc) {
ExprResult Fn = SemaRef.BuildDeclRefExpr(FD, FD->getType(), VK_LValue, Loc);
return SemaRef.BuildResolvedCallExpr(Fn.get(), FD, Loc, args, Loc);
}
ExprResult BuildUPCRCall(FunctionDecl *FD, std::vector<Expr*>& args) {
return BuildUPCRCall(FD, args, SourceLocation());
}
ExprResult BuildUPCRDeclRef(VarDecl *VD) {
return SemaRef.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, SourceLocation());
}
Expr * CreateSimpleDeclRef(VarDecl *VD) {
//return SemaRef.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, SourceLocation()).get();
//return dynamic_cast<Expr *>(SemaRef.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, SourceLocation()));
return dyn_cast<Expr>(SemaRef.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, SourceLocation()));
}
int AnonRecordID;
int StaticLocalVarID;
IdentifierInfo *getRecordDeclName(IdentifierInfo * OrigName) {
return OrigName;
}
struct ArrayDimensionT {
ArrayDimensionT(ASTContext& Context) :
ArrayDimension(Context.getTypeSize(Context.getSizeType()), 1),
HasThread(false),
ElementSize(0),
E(NULL)
{}
llvm::APInt ArrayDimension;
bool HasThread;
int64_t ElementSize; // Matches representation of CharUnits
Expr *E;
};
ArrayDimensionT GetArrayDimension(QualType Ty) {
ArrayDimensionT Result(SemaRef.Context);
QualType ElemTy = Ty.getCanonicalType();
while(const ArrayType *AT = dyn_cast<ArrayType>(ElemTy.getTypePtr())) {
if(const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Result.ArrayDimension *= CAT->getSize();
} else if(const UPCThreadArrayType *TAT = dyn_cast<UPCThreadArrayType>(AT)) {
if(TAT->getThread()) {
Result.HasThread = true;
}
Result.ArrayDimension *= TAT->getSize();
} else if(const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
Expr *Val = BuildParens(TransformExpr(VAT->getSizeExpr()).get()).get();
if(Result.E) {
Result.E = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Result.E, Val).get();
} else {
Result.E = Val;
}
} else if(isa<IncompleteArrayType>(AT)) {
Result.ArrayDimension = 0;
} else {
assert(!"Other array types should not syntax check");
}
ElemTy = AT->getElementType();
}
Result.ElementSize = SemaRef.Context.getTypeSizeInChars(ElemTy).getQuantity();
return Result;
}
Expr * MaybeAddParensForMultiply(Expr * E) {
if(isa<ParenExpr>(E) || isa<IntegerLiteral>(E) ||
isa<CallExpr>(E))
return E;
else
return BuildParens(E).get();
}
ExprResult MaybeAdjustForArray(const ArrayDimensionT & Dims, Expr * E, BinaryOperatorKind Op) {
if(Dims.ArrayDimension == 1 && !Dims.E && !Dims.HasThread) {
return E;
} else {
Expr *Dimension = IntegerLiteral::Create(SemaRef.Context, Dims.ArrayDimension, SemaRef.Context.getSizeType(), SourceLocation());
if(Dims.HasThread) {
std::vector<Expr*> args;
Expr *Threads = BuildUPCRCall(Decls->upcr_threads, args).get();
Dimension = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Dimension, Threads).get();
}
if(Dims.E) {
Dimension = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Dimension, Dims.E).get();
}
if(Dims.HasThread || Dims.E) {
Dimension = BuildParens(Dimension).get();
}
return BuildParens(SemaRef.CreateBuiltinBinOp(SourceLocation(), Op, MaybeAddParensForMultiply(E), Dimension).get());
}
}
std::vector<Expr*> BuildUPCBarrierArgs(Expr *ID) {
ASTContext& Context = SemaRef.Context;
bool isAnon = !ID;
if(isAnon) {
ID = IntegerLiteral::Create(Context, APInt(32, 0), Context.IntTy, SourceLocation());
} else if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ID)) {
QualType Ty = ICE->getSubExpr()->getType();
if(isPointerToShared(Ty) || Ty.getQualifiers().hasShared()) {
ID = TransformExpr(ID).get();
} else {
ID = TransformExpr(ICE->getSubExpr()).get();
TypeSourceInfo *Type;
if(ICE->getCastKind() == CK_PointerToIntegral) {
// Pointer types get 2 casts; this one silences "narrowing" warnings
Type = Context.getTrivialTypeSourceInfo(Context.getIntPtrType());
ID = SemaRef.BuildCStyleCastExpr(SourceLocation(), Type, SourceLocation(), ID).get();
}
Type = Context.getTrivialTypeSourceInfo(Context.IntTy);
ID = SemaRef.BuildCStyleCastExpr(SourceLocation(), Type, SourceLocation(), ID).get();
}
} else {
ID = TransformExpr(ID).get();
}
std::vector<Expr*> args;
args.push_back(ID);
args.push_back(IntegerLiteral::Create(Context, APInt(32, isAnon), Context.IntTy, SourceLocation()));
return args;
}
StmtResult TransformUPCNotifyStmt(UPCNotifyStmt *S) {
std::vector<Expr*> args = BuildUPCBarrierArgs(S->getIdValue());
Stmt *result = BuildUPCRCall(Decls->upcr_notify, args, S->getBeginLoc()).get();
return result;
}
StmtResult TransformUPCWaitStmt(UPCWaitStmt *S) {
std::vector<Expr*> args = BuildUPCBarrierArgs(S->getIdValue());
Stmt *result = BuildUPCRCall(Decls->upcr_wait, args, S->getBeginLoc()).get();
return result;
}
StmtResult TransformUPCBarrierStmt(UPCBarrierStmt *S) {
std::vector<Expr*> args = BuildUPCBarrierArgs(S->getIdValue());
Stmt *result = BuildUPCRCall(Decls->upcr_barrier, args, S->getBeginLoc()).get();
return result;
}
StmtResult TransformUPCFenceStmt(UPCFenceStmt *S) {
std::vector<Expr*> args;
Stmt *result = BuildUPCRCall(Decls->upcr_poll, args, S->getBeginLoc()).get();
return result;
}
ExprResult TransformUPCThreadExpr(UPCThreadExpr *E) {
std::vector<Expr*> args;
Expr *Call = BuildUPCRCall(Decls->upcr_threads, args).get();
return SemaRef.BuildCStyleCastExpr(SourceLocation(), SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy), SourceLocation(), Call);
}
ExprResult TransformUPCMyThreadExpr(UPCMyThreadExpr *E) {
std::vector<Expr*> args;
Expr *Call = BuildUPCRCall(Decls->upcr_mythread, args).get();
return SemaRef.BuildCStyleCastExpr(SourceLocation(), SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy), SourceLocation(), Call);
}
ExprResult TransformInitializer(Expr *Init, bool CXXDirectInit) {
if(!Init)
return Init;
// Have to handle this separately, as TreeTransform
// strips off ImplicitCastExprs in TransformInitializer.
if(ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init)) {
if((ICE->getCastKind() == CK_LValueToRValue && ICE->getSubExpr()->getType().getQualifiers().hasShared()) ||
isPointerToShared(ICE->getSubExpr()->getType())) {
return TransformExpr(ICE);
} else {
ExprResult UPCCast = MaybeTransformUPCRCast(ICE);
if(!UPCCast.isInvalid())
return UPCCast;
return TransformInitializer(ICE->getSubExpr(), CXXDirectInit);
}
}
return TreeTransformUPC::TransformInitializer(Init, CXXDirectInit);
}
ExprResult TransformCStyleCastExpr(CStyleCastExpr *E) {
ExprResult UPCCast = MaybeTransformUPCRCast(E);
if(!UPCCast.isInvalid()) {
return UPCCast;
} else {
// The default transform strips off implicit casts
TypeSourceInfo *Type = TransformType(E->getTypeInfoAsWritten());
if (!Type)
return ExprError();
ExprResult SubExpr = TransformExpr(E->getSubExpr());
if (SubExpr.isInvalid())
return ExprError();
return RebuildCStyleCastExpr(E->getLParenLoc(),
Type,
E->getRParenLoc(),
SubExpr.get());
}
}
ExprResult TransformImplicitCastExpr(ImplicitCastExpr *E) {
if(E->getCastKind() == CK_LValueToRValue && E->getSubExpr()->getType().getQualifiers().hasShared()) {
return BuildUPCRLoad(TransformExpr(E->getSubExpr()).get(), E->getSubExpr()->getType());
} else {
ExprResult UPCCast = MaybeTransformUPCRCast(E);
if(!UPCCast.isInvalid()) {
return UPCCast;
}
// We can't use the default transform, because it
// strips off all implicit casts. We may need to
// process the subexpression
return TransformExpr(E->getSubExpr());
}
}
bool isPointerToShared(QualType Ty) {
if(const PointerType * PTy = Ty->getAs<PointerType>()) {
return PTy->getPointeeType().getQualifiers().hasShared();
} else {
return false;
}
}
IntegerLiteral *CreateInteger(QualType Ty, int Value) {
return IntegerLiteral::Create(SemaRef.Context, APInt(SemaRef.Context.getTypeSize(Ty), Value), Ty, SourceLocation());
}
bool isLiteralInt(const Expr *E, uint64_t value) {
const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(E->IgnoreParenCasts());
return (Lit && Lit->getValue() == value);
}
bool typeFitsUPCRValuePutGet(QualType Ty) {
// FIXME: pointers-to-shared may fit, but since we cannot cast freely between
// them and upcr_register_value_t, we also can't xfer them by value.
if(isPointerToShared(Ty)) return false;
return Ty->isSpecificBuiltinType(BuiltinType::Float) ||
Ty->isSpecificBuiltinType(BuiltinType::Double) ||
((Ty->isIntegralOrEnumerationType() || Ty->isPointerType()) &&
(SemaRef.Context.getTypeSize(Ty) <= SemaRef.Context.getTypeSize(Decls->upcr_register_value_t)));
}
Expr *FoldUPCRLoadStore(Expr* &E, bool &Phaseless) {
Expr *Offset = NULL;
while (CallExpr *CE = dyn_cast<CallExpr>(E->IgnoreParens())) {
FunctionDecl *FD = CE->getDirectCallee();
if (FD == Decls->UPCR_SHARED_TO_PSHARED || FD == Decls->UPCR_PSHARED_TO_SHARED) {
// Fold phased/phaseless conversion into choice of Accessor
// NOTE: Dropping a shared_to_pshared() in this manner is NOT a correct
// transform if any definite pointer-to-shared arithmetic were then applied.
// It is acceptible here ONLY because Put and Get don't use the phase.
E = CE->getArg(0);
Phaseless = !Phaseless;
} else if (FD == Decls->UPCR_ADD_PSHAREDI) {
// Fold (non-zero) indefinite address arithmetic into the Offset
E = CE->getArg(0);
Expr *Elemsz = CE->getArg(1);
Expr *Inc = CE->getArg(2);
Expr *NewOffset;
if (isLiteralInt(Elemsz,1)) {
NewOffset = Inc;
} else {
Elemsz = MaybeAddParensForMultiply(Elemsz);
Inc = MaybeAddParensForMultiply(Inc);
NewOffset = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Mul, Elemsz, Inc).get();
}
if (Offset) {
Offset = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Add, Offset, NewOffset).get();
} else {
Offset = NewOffset;
}
} else {
break;
}
}
if (Offset) return Offset;
return CreateInteger(SemaRef.Context.getSizeType(), 0);
}
// If LoadVar is passed, then the result will contain an assignment to it.
// Otherwise the result will use a temporary only if necessary.
// Regardless, the value of the expression will be the result of the Load.
Expr *BuildUPCRLoad(Expr * Ptr, QualType Ty, Expr * LoadVar = NULL) {
Qualifiers Quals = Ty.getQualifiers();
bool Phaseless = isPhaseless(Ty);
bool Strict = Quals.hasStrict();
// Try to fold offset and phased/phaseless conversions:
Expr *Offset = FoldUPCRLoadStore(Ptr, Phaseless);
std::vector<Expr*> args;
Expr *Result;
QualType ResultType = TransformType(Ty).getUnqualifiedType();
if(typeFitsUPCRValuePutGet(ResultType)) {
// Case 1. Get by value, with type cast if necesssary
args.push_back(Ptr);
args.push_back(Offset);
UPCRCommFn *Accessor;
if(ResultType->isSpecificBuiltinType(BuiltinType::Float)) {
Accessor = &Decls->UPCR_GET_FVAL;
} else if(ResultType->isSpecificBuiltinType(BuiltinType::Double)) {
Accessor = &Decls->UPCR_GET_DVAL;
} else {
Accessor = &Decls->UPCR_GET_IVAL;
args.push_back(CreateInteger(SemaRef.Context.getSizeType(),SemaRef.Context.getTypeSizeInChars(Ty).getQuantity()));
}
Result = BuildUPCRCall((*Accessor)(Phaseless,Strict), args).get();
// NOTE: Without a cast the float and double cases yield an assertion failure!?
TypeSourceInfo *CastTo = SemaRef.Context.getTrivialTypeSourceInfo(ResultType);
Result = SemaRef.BuildCStyleCastExpr(SourceLocation(), CastTo, SourceLocation(), Result).get();
if(LoadVar) {
Result = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, LoadVar, Result).get();
}
Result = BuildParens(Result).get();
} else {
// Case 2. Get by reference, to callers LoadVar if passed
VarDecl *TmpVar = NULL;
if (!LoadVar) { // Create a LoadVar if the caller doesn't provide one
TmpVar = CreateTmpVar(ResultType);
LoadVar = CreateSimpleDeclRef(TmpVar);
}
args.push_back(SemaRef.CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, LoadVar).get());
args.push_back(Ptr);
args.push_back(Offset);
args.push_back(CreateInteger(SemaRef.Context.getSizeType(),SemaRef.Context.getTypeSizeInChars(Ty).getQuantity()));
Result = BuildUPCRCall(Decls->UPCR_GET(Phaseless,Strict), args).get();
if(TmpVar) Result = BuildParens(BuildComma(Result, CreateSimpleDeclRef(TmpVar)).get()).get();
}
return Result;
}
ExprResult BuildUPCRSharedToPshared(Expr *Ptr) {
CallExpr *CE = dyn_cast<CallExpr>(Ptr->IgnoreParens());
FunctionDecl *Child = CE? CE->getDirectCallee() : 0;
if(Child == Decls->UPCR_PSHARED_TO_SHARED) {
// upcr_shared_to_pshared(pshared_to_shared(p)) -> p
return ExprResult(CE->getArg(0));
} else if(Child == Decls->UPCR_SHARED_RESETPHASE) {
// shared_to_pshared(resetphase(p)) -> shared_to_pshared(p)
Ptr = CE->getArg(0);
}
std::vector<Expr*> args;
args.push_back(Ptr);
return BuildUPCRCall(Decls->UPCR_SHARED_TO_PSHARED, args);
}
ExprResult BuildUPCRPsharedToShared(Expr *Ptr) {
CallExpr *CE = dyn_cast<CallExpr>(Ptr->IgnoreParens());
FunctionDecl *Child = CE? CE->getDirectCallee() : 0;
if(Child == Decls->UPCR_SHARED_TO_PSHARED) {
// pshared_to_shared(shared_to_pshared(p)) -> resetphase(p)
return BuildUPCRSharedResetPhase(CE->getArg(0));
}
std::vector<Expr*> args;
args.push_back(Ptr);
return BuildUPCRCall(Decls->UPCR_PSHARED_TO_SHARED, args);
}
ExprResult BuildUPCRSharedResetPhase(Expr *Ptr) {
CallExpr *CE = dyn_cast<CallExpr>(Ptr->IgnoreParens());
FunctionDecl *Child = CE? CE->getDirectCallee() : 0;
if(Child == Decls->UPCR_SHARED_RESETPHASE) {
// resetphase(resetphase(p)) -> resetphase(p)
return ExprResult(Ptr);
}
std::vector<Expr*> args;
args.push_back(Ptr);
return BuildUPCRCall(Decls->UPCR_SHARED_RESETPHASE, args);
}
ExprResult MaybeTransformUPCRCast(CastExpr *E) {
if(E->getCastKind() == CK_UPCSharedToLocal) {
bool Phaseless = isPhaseless(E->getSubExpr()->getType()->getAs<PointerType>()->getPointeeType());
FunctionDecl *Accessor = Phaseless? Decls->UPCR_PSHARED_TO_LOCAL : Decls->UPCR_SHARED_TO_LOCAL;
std::vector<Expr*> args;
args.push_back(TransformExpr(E->getSubExpr()).get());
ExprResult Result = BuildUPCRCall(Accessor, args);
TypeSourceInfo *Ty = SemaRef.Context.getTrivialTypeSourceInfo(TransformType(E->getType()));
return SemaRef.BuildCStyleCastExpr(SourceLocation(), Ty, SourceLocation(), Result.get());
} else if(E->getCastKind() == CK_NullToPointer && isPointerToShared(E->getType())) {
bool Phaseless = isPhaseless(E->getType()->getAs<PointerType>()->getPointeeType());
return BuildUPCRDeclRef(Phaseless? Decls->upcr_null_pshared : Decls->upcr_null_shared);
} else if((E->getCastKind() == CK_BitCast ||
E->getCastKind() == CK_UPCBitCastZeroPhase) &&
isPointerToShared(E->getType())) {
QualType DstPointee = E->getType()->getAs<PointerType>()->getPointeeType();
QualType SrcPointee = E->getSubExpr()->getType()->getAs<PointerType>()->getPointeeType();
ExprResult Result = TransformExpr(E->getSubExpr());
if(isPhaseless(DstPointee) && !isPhaseless(SrcPointee)) {
return BuildUPCRSharedToPshared(Result.get());
} else if(!isPhaseless(DstPointee) && isPhaseless(SrcPointee)) {
return BuildUPCRPsharedToShared(Result.get());
} else if(!isPhaseless(DstPointee) && !isPhaseless(SrcPointee) &&
E->getCastKind() == CK_UPCBitCastZeroPhase) {
return BuildUPCRSharedResetPhase(Result.get());
} else {
return Result;
}
} else if(E->getCastKind() == CK_PointerToIntegral &&
isPointerToShared(E->getSubExpr()->getType())) {
bool Phaseless = isPhaseless(E->getSubExpr()->getType()->getAs<PointerType>()->getPointeeType());
FunctionDecl *Accessor = Phaseless? Decls->UPCR_ADDRFIELD_PSHARED : Decls->UPCR_ADDRFIELD_SHARED;
std::vector<Expr*> args;
args.push_back(TransformExpr(E->getSubExpr()).get());
Expr *Result = BuildUPCRCall(Accessor, args).get();
TypeSourceInfo *Type = SemaRef.Context.getTrivialTypeSourceInfo(TransformType(E->getType()));
return SemaRef.BuildCStyleCastExpr(SourceLocation(), Type, SourceLocation(), Result);
}
return ExprError();
}
ExprResult BuildUPCRStore(Expr * LHS, Expr * RHS, QualType Ty, bool ReturnValue = true) {
Qualifiers Quals = Ty.getQualifiers();
bool Phaseless = isPhaseless(Ty);
bool Strict = Quals.hasStrict();
// Try to fold offset and phased/phaseless conversions:
Expr *Offset = FoldUPCRLoadStore(LHS, Phaseless);
// Select the default function to call
UPCRCommFn *Accessor = &Decls->UPCR_PUT;
Expr *SetTmp = NULL;
Expr *SrcArg = NULL;
Expr *RetVal = NULL;
bool NeedSize = true;
QualType ResultType = TransformType(Ty).getUnqualifiedType();
QualType RHSType = RHS->getType().getUnqualifiedType();
if(typeFitsUPCRValuePutGet(ResultType)) {
if (RHS->isLValue() && !ReturnValue) {
// Case 1. Put RHS by value, with type cast if necessary
SrcArg = RHS;
if(!SemaRef.Context.typesAreCompatible(ResultType, RHSType)) {
TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(ResultType);
SrcArg = SemaRef.BuildCStyleCastExpr(SourceLocation(), TSI, SourceLocation(), SrcArg).get();
}
} else {
// Case 2. Store value of RHS in a temporary. which is Put by value
// If(ReturnValue) then the temporary's value is returned
VarDecl *TmpVar = CreateTmpVar(ResultType);
SetTmp = SemaRef.CreateBuiltinBinOp(SourceLocation(), BO_Assign, CreateSimpleDeclRef(TmpVar), RHS).get();
SrcArg = CreateSimpleDeclRef(TmpVar);
if(ReturnValue) RetVal = CreateSimpleDeclRef(TmpVar);
}
if(ResultType->isSpecificBuiltinType(BuiltinType::Float)) {
Accessor = &Decls->UPCR_PUT_FVAL;
NeedSize = false;
} else if(ResultType->isSpecificBuiltinType(BuiltinType::Double)) {
Accessor = &Decls->UPCR_PUT_DVAL;
NeedSize = false;
} else {
TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Decls->upcr_register_value_t);
SrcArg = SemaRef.BuildCStyleCastExpr(SourceLocation(), TSI, SourceLocation(), SrcArg).get();
Accessor = &Decls->UPCR_PUT_IVAL;
}
} else if (RHS->isLValue() && !ReturnValue &&
SemaRef.Context.typesAreCompatible(ResultType, RHSType)) {