-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.cpp
4220 lines (3483 loc) · 120 KB
/
type.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
/*
Copyright (c) 2010-2015, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER
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.
*/
/** @file type.cpp
@brief Definitions for classes related to type representation
*/
#include "type.h"
#include "expr.h"
#include "sym.h"
#include "llvmutil.h"
#include "module.h"
#include <stdio.h>
#include <map>
#if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
#include <llvm/Value.h>
#include <llvm/Module.h>
#else
#include <llvm/IR/Value.h>
#include <llvm/IR/Module.h>
#endif
#if ISPC_LLVM_VERSION >= ISPC_LLVM_3_5 // LLVM 3.5+
#include <llvm/IR/DebugInfo.h>
#include <llvm/IR/DIBuilder.h>
#else
#include <llvm/DebugInfo.h>
#include <llvm/DIBuilder.h>
#endif
#include <llvm/Support/Dwarf.h>
/** Utility routine used in code that prints out declarations; returns true
if the given name should be printed, false otherwise. This allows us
to omit the names for various internal things (whose names start with
double underscores) and emit anonymous declarations for them instead.
*/
static bool
lShouldPrintName(const std::string &name) {
if (name.size() == 0)
return false;
else if (name[0] != '_' && name[0] != '$')
return true;
else
return (name.size() == 1) || (name[1] != '_');
}
/** Utility routine to create a llvm array type of the given number of
the given element type. */
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
static llvm::DIType lCreateDIArray(llvm::DIType eltType, int count) {
#else // LLVM 3.7++
static llvm::DIType *lCreateDIArray(llvm::DIType *eltType, int count) {
#endif
#if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
int lowerBound = 0, upperBound = count-1;
if (count == 0) {
// unsized array -> indicate with low > high
lowerBound = 1;
upperBound = 0;
}
llvm::Value *sub = m->diBuilder->getOrCreateSubrange(lowerBound, upperBound);
#elif ISPC_LLVM_VERSION <= ISPC_LLVM_3_5
llvm::Value *sub = m->diBuilder->getOrCreateSubrange(0, count);
#endif
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_5
std::vector<llvm::Value *> subs;
#else // LLVM 3.6++
llvm::Metadata *sub = m->diBuilder->getOrCreateSubrange(0, count);
std::vector<llvm::Metadata *> subs;
#endif
subs.push_back(sub);
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
llvm::DIArray subArray = m->diBuilder->getOrCreateArray(subs);
uint64_t size = eltType.getSizeInBits() * count;
uint64_t align = eltType.getAlignInBits();
#else // LLVM 3.7++
llvm::DINodeArray subArray = m->diBuilder->getOrCreateArray(subs);
uint64_t size = eltType->getSizeInBits() * count;
uint64_t align = eltType->getAlignInBits();
#endif
return m->diBuilder->createArrayType(size, align, eltType, subArray);
}
///////////////////////////////////////////////////////////////////////////
// Variability
std::string
Variability::GetString() const {
switch (type) {
case Uniform: return "uniform";
case Varying: return "varying";
case SOA: {
char buf[32];
sprintf(buf, "soa<%d>", soaWidth);
return buf;
}
case Unbound: return "/*unbound*/";
default:
FATAL("Unhandled variability");
return "";
}
}
std::string
Variability::MangleString() const {
switch (type) {
case Uniform:
return "un";
case Varying:
return "vy";
case SOA: {
char buf[32];
sprintf(buf, "soa<%d>", soaWidth);
return buf;
}
case Unbound:
FATAL("Unbound unexpected in Variability::MangleString()");
default:
FATAL("Unhandled variability");
return "";
}
}
///////////////////////////////////////////////////////////////////////////
// AtomicType
const AtomicType *AtomicType::UniformBool =
new AtomicType(AtomicType::TYPE_BOOL, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingBool =
new AtomicType(AtomicType::TYPE_BOOL, Variability::Varying, false);
const AtomicType *AtomicType::UniformInt8 =
new AtomicType(AtomicType::TYPE_INT8, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingInt8 =
new AtomicType(AtomicType::TYPE_INT8, Variability::Varying, false);
const AtomicType *AtomicType::UniformUInt8 =
new AtomicType(AtomicType::TYPE_UINT8, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingUInt8 =
new AtomicType(AtomicType::TYPE_UINT8, Variability::Varying, false);
const AtomicType *AtomicType::UniformInt16 =
new AtomicType(AtomicType::TYPE_INT16, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingInt16 =
new AtomicType(AtomicType::TYPE_INT16, Variability::Varying, false);
const AtomicType *AtomicType::UniformUInt16 =
new AtomicType(AtomicType::TYPE_UINT16, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingUInt16 =
new AtomicType(AtomicType::TYPE_UINT16, Variability::Varying, false);
const AtomicType *AtomicType::UniformInt32 =
new AtomicType(AtomicType::TYPE_INT32, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingInt32 =
new AtomicType(AtomicType::TYPE_INT32, Variability::Varying, false);
const AtomicType *AtomicType::UniformUInt32 =
new AtomicType(AtomicType::TYPE_UINT32, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingUInt32 =
new AtomicType(AtomicType::TYPE_UINT32, Variability::Varying, false);
const AtomicType *AtomicType::UniformFloat =
new AtomicType(AtomicType::TYPE_FLOAT, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingFloat =
new AtomicType(AtomicType::TYPE_FLOAT, Variability::Varying, false);
const AtomicType *AtomicType::UniformInt64 =
new AtomicType(AtomicType::TYPE_INT64, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingInt64 =
new AtomicType(AtomicType::TYPE_INT64, Variability::Varying, false);
const AtomicType *AtomicType::UniformUInt64 =
new AtomicType(AtomicType::TYPE_UINT64, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingUInt64 =
new AtomicType(AtomicType::TYPE_UINT64, Variability::Varying, false);
const AtomicType *AtomicType::UniformDouble =
new AtomicType(AtomicType::TYPE_DOUBLE, Variability::Uniform, false);
const AtomicType *AtomicType::VaryingDouble =
new AtomicType(AtomicType::TYPE_DOUBLE, Variability::Varying, false);
const AtomicType *AtomicType::Void =
new AtomicType(TYPE_VOID, Variability::Uniform, false);
AtomicType::AtomicType(BasicType bt, Variability v, bool ic)
: Type(ATOMIC_TYPE), basicType(bt), variability(v), isConst(ic) {
asOtherConstType = NULL;
asUniformType = asVaryingType = NULL;
}
Variability
AtomicType::GetVariability() const {
return variability;
}
bool
Type::IsPointerType() const {
return (CastType<PointerType>(this) != NULL);
}
bool
Type::IsArrayType() const {
return (CastType<ArrayType>(this) != NULL);
}
bool
Type::IsReferenceType() const {
return (CastType<ReferenceType>(this) != NULL);
}
bool
Type::IsVoidType() const {
return EqualIgnoringConst(this, AtomicType::Void);
}
bool
Type::IsPolymorphicType() const {
const FunctionType *ft = CastType<FunctionType>(this);
if (ft) {
for (int i=0; i<ft->GetNumParameters(); i++) {
if (ft->GetParameterType(i)->IsPolymorphicType())
return true;
}
return false;
}
return (CastType<PolyType>(GetBaseType()) != NULL);
}
bool
AtomicType::IsFloatType() const {
return (basicType == TYPE_FLOAT || basicType == TYPE_DOUBLE);
}
bool
AtomicType::IsIntType() const {
return (basicType == TYPE_INT8 || basicType == TYPE_UINT8 ||
basicType == TYPE_INT16 || basicType == TYPE_UINT16 ||
basicType == TYPE_INT32 || basicType == TYPE_UINT32 ||
basicType == TYPE_INT64 || basicType == TYPE_UINT64);
}
bool
AtomicType::IsUnsignedType() const {
return (basicType == TYPE_UINT8 || basicType == TYPE_UINT16 ||
basicType == TYPE_UINT32 || basicType == TYPE_UINT64);
}
bool
AtomicType::IsBoolType() const {
return basicType == TYPE_BOOL;
}
bool
AtomicType::IsConstType() const {
return isConst;
}
const AtomicType *
AtomicType::GetAsUnsignedType() const {
if (IsUnsignedType() == true)
return this;
if (IsIntType() == false)
return NULL;
switch (basicType) {
case TYPE_INT8:
return new AtomicType(TYPE_UINT8, variability, isConst);
case TYPE_INT16:
return new AtomicType(TYPE_UINT16, variability, isConst);
case TYPE_INT32:
return new AtomicType(TYPE_UINT32, variability, isConst);
case TYPE_INT64:
return new AtomicType(TYPE_UINT64, variability, isConst);
default:
FATAL("Unexpected basicType in GetAsUnsignedType()");
return NULL;
}
}
const AtomicType *
AtomicType::GetAsConstType() const {
if (isConst == true)
return this;
if (asOtherConstType == NULL) {
asOtherConstType = new AtomicType(basicType, variability, true);
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
}
const AtomicType *
AtomicType::GetAsNonConstType() const {
if (isConst == false)
return this;
if (asOtherConstType == NULL) {
asOtherConstType = new AtomicType(basicType, variability, false);
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
}
const AtomicType *
AtomicType::GetBaseType() const {
return this;
}
const AtomicType *
AtomicType::GetAsVaryingType() const {
Assert(basicType != TYPE_VOID);
if (variability == Variability::Varying)
return this;
if (asVaryingType == NULL) {
asVaryingType = new AtomicType(basicType, Variability::Varying, isConst);
if (variability == Variability::Uniform)
asVaryingType->asUniformType = this;
}
return asVaryingType;
}
const AtomicType *
AtomicType::GetAsUniformType() const {
Assert(basicType != TYPE_VOID);
if (variability == Variability::Uniform)
return this;
if (asUniformType == NULL) {
asUniformType = new AtomicType(basicType, Variability::Uniform, isConst);
if (variability == Variability::Varying)
asUniformType->asVaryingType = this;
}
return asUniformType;
}
const AtomicType *
AtomicType::GetAsUnboundVariabilityType() const {
Assert(basicType != TYPE_VOID);
if (variability == Variability::Unbound)
return this;
return new AtomicType(basicType, Variability::Unbound, isConst);
}
const AtomicType *
AtomicType::GetAsSOAType(int width) const {
Assert(basicType != TYPE_VOID);
if (variability == Variability(Variability::SOA, width))
return this;
return new AtomicType(basicType, Variability(Variability::SOA, width), isConst);
}
const AtomicType *
AtomicType::ResolveUnboundVariability(Variability v) const {
Assert(v != Variability::Unbound);
if (variability != Variability::Unbound)
return this;
return new AtomicType(basicType, v, isConst);
}
std::string
AtomicType::GetString() const {
std::string ret;
if (isConst) ret += "const ";
if (basicType != TYPE_VOID) {
ret += variability.GetString();
ret += " ";
}
switch (basicType) {
case TYPE_VOID: ret += "void"; break;
case TYPE_BOOL: ret += "bool"; break;
case TYPE_INT8: ret += "int8"; break;
case TYPE_UINT8: ret += "unsigned int8"; break;
case TYPE_INT16: ret += "int16"; break;
case TYPE_UINT16: ret += "unsigned int16"; break;
case TYPE_INT32: ret += "int32"; break;
case TYPE_UINT32: ret += "unsigned int32"; break;
case TYPE_FLOAT: ret += "float"; break;
case TYPE_INT64: ret += "int64"; break;
case TYPE_UINT64: ret += "unsigned int64"; break;
case TYPE_DOUBLE: ret += "double"; break;
default: FATAL("Logic error in AtomicType::GetString()");
}
return ret;
}
std::string
AtomicType::Mangle() const {
std::string ret;
if (isConst) ret += "C";
ret += variability.MangleString();
switch (basicType) {
case TYPE_VOID: ret += "v"; break;
case TYPE_BOOL: ret += "b"; break;
case TYPE_INT8: ret += "t"; break;
case TYPE_UINT8: ret += "T"; break;
case TYPE_INT16: ret += "s"; break;
case TYPE_UINT16: ret += "S"; break;
case TYPE_INT32: ret += "i"; break;
case TYPE_UINT32: ret += "u"; break;
case TYPE_FLOAT: ret += "f"; break;
case TYPE_INT64: ret += "I"; break;
case TYPE_UINT64: ret += "U"; break;
case TYPE_DOUBLE: ret += "d"; break;
default: FATAL("Logic error in AtomicType::Mangle()");
}
return ret;
}
std::string
AtomicType::GetCDeclaration(const std::string &name) const {
std::string ret;
if (variability == Variability::Unbound) {
Assert(m->errorCount > 0);
return ret;
}
if (isConst) ret += "const ";
switch (basicType) {
case TYPE_VOID: ret += "void"; break;
case TYPE_BOOL: ret += "bool"; break;
case TYPE_INT8: ret += "int8_t"; break;
case TYPE_UINT8: ret += "uint8_t"; break;
case TYPE_INT16: ret += "int16_t"; break;
case TYPE_UINT16: ret += "uint16_t"; break;
case TYPE_INT32: ret += "int32_t"; break;
case TYPE_UINT32: ret += "uint32_t"; break;
case TYPE_FLOAT: ret += "float"; break;
case TYPE_INT64: ret += "int64_t"; break;
case TYPE_UINT64: ret += "uint64_t"; break;
case TYPE_DOUBLE: ret += "double"; break;
default: FATAL("Logic error in AtomicType::GetCDeclaration()");
}
if (lShouldPrintName(name)) {
ret += " ";
ret += name;
}
if (variability == Variability::SOA) {
char buf[32];
sprintf(buf, "[%d]", variability.soaWidth);
ret += buf;
}
return ret;
}
llvm::Type *
AtomicType::LLVMType(llvm::LLVMContext *ctx) const {
Assert(variability.type != Variability::Unbound);
bool isUniform = (variability == Variability::Uniform);
bool isVarying = (variability == Variability::Varying);
if (isUniform || isVarying) {
switch (basicType) {
case TYPE_VOID:
return llvm::Type::getVoidTy(*ctx);
case TYPE_BOOL:
return isUniform ? LLVMTypes::BoolType : LLVMTypes::BoolVectorType;
case TYPE_INT8:
case TYPE_UINT8:
return isUniform ? LLVMTypes::Int8Type : LLVMTypes::Int8VectorType;
case TYPE_INT16:
case TYPE_UINT16:
return isUniform ? LLVMTypes::Int16Type : LLVMTypes::Int16VectorType;
case TYPE_INT32:
case TYPE_UINT32:
return isUniform ? LLVMTypes::Int32Type : LLVMTypes::Int32VectorType;
case TYPE_FLOAT:
return isUniform ? LLVMTypes::FloatType : LLVMTypes::FloatVectorType;
case TYPE_INT64:
case TYPE_UINT64:
return isUniform ? LLVMTypes::Int64Type : LLVMTypes::Int64VectorType;
case TYPE_DOUBLE:
return isUniform ? LLVMTypes::DoubleType : LLVMTypes::DoubleVectorType;
default:
FATAL("logic error in AtomicType::LLVMType");
return NULL;
}
}
else {
ArrayType at(GetAsUniformType(), variability.soaWidth);
return at.LLVMType(ctx);
}
}
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
llvm::DIType AtomicType::GetDIType(llvm::DIDescriptor scope) const {
#else //LLVM 3.7++
llvm::DIType *AtomicType::GetDIType(llvm::DIScope *scope) const {
#endif
Assert(variability.type != Variability::Unbound);
if (variability.type == Variability::Uniform) {
switch (basicType) {
case TYPE_VOID:
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
return llvm::DIType();
#else //LLVM 3.7++
return NULL;
#endif
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_9
case TYPE_BOOL:
return m->diBuilder->createBasicType("bool", 32 /* size */, 32 /* align */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_INT8:
return m->diBuilder->createBasicType("int8", 8 /* size */, 8 /* align */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT8:
return m->diBuilder->createBasicType("uint8", 8 /* size */, 8 /* align */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_INT16:
return m->diBuilder->createBasicType("int16", 16 /* size */, 16 /* align */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT16:
return m->diBuilder->createBasicType("uint16", 16 /* size */, 16 /* align */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_INT32:
return m->diBuilder->createBasicType("int32", 32 /* size */, 32 /* align */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT32:
return m->diBuilder->createBasicType("uint32", 32 /* size */, 32 /* align */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_FLOAT:
return m->diBuilder->createBasicType("float", 32 /* size */, 32 /* align */,
llvm::dwarf::DW_ATE_float);
break;
case TYPE_DOUBLE:
return m->diBuilder->createBasicType("double", 64 /* size */, 64 /* align */,
llvm::dwarf::DW_ATE_float);
break;
case TYPE_INT64:
return m->diBuilder->createBasicType("int64", 64 /* size */, 64 /* align */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT64:
return m->diBuilder->createBasicType("uint64", 64 /* size */, 64 /* align */,
llvm::dwarf::DW_ATE_unsigned);
break;
#else // LLVM 4.0+
case TYPE_BOOL:
return m->diBuilder->createBasicType("bool", 32 /* size */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_INT8:
return m->diBuilder->createBasicType("int8", 8 /* size */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT8:
return m->diBuilder->createBasicType("uint8", 8 /* size */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_INT16:
return m->diBuilder->createBasicType("int16", 16 /* size */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT16:
return m->diBuilder->createBasicType("uint16", 16 /* size */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_INT32:
return m->diBuilder->createBasicType("int32", 32 /* size */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT32:
return m->diBuilder->createBasicType("uint32", 32 /* size */,
llvm::dwarf::DW_ATE_unsigned);
break;
case TYPE_FLOAT:
return m->diBuilder->createBasicType("float", 32 /* size */,
llvm::dwarf::DW_ATE_float);
break;
case TYPE_DOUBLE:
return m->diBuilder->createBasicType("double", 64 /* size */,
llvm::dwarf::DW_ATE_float);
break;
case TYPE_INT64:
return m->diBuilder->createBasicType("int64", 64 /* size */,
llvm::dwarf::DW_ATE_signed);
break;
case TYPE_UINT64:
return m->diBuilder->createBasicType("uint64", 64 /* size */,
llvm::dwarf::DW_ATE_unsigned);
break;
#endif
default:
FATAL("unhandled basic type in AtomicType::GetDIType()");
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
return llvm::DIType();
#else //LLVM 3.7+
return NULL;
#endif
}
}
else if (variability == Variability::Varying) {
#if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
llvm::Value *sub = m->diBuilder->getOrCreateSubrange(0, g->target->getVectorWidth()-1);
#elif ISPC_LLVM_VERSION > ISPC_VERSION_3_2 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_5
llvm::Value *sub = m->diBuilder->getOrCreateSubrange(0, g->target->getVectorWidth());
#else // LLVM 3.6+
llvm::Metadata *sub = m->diBuilder->getOrCreateSubrange(0, g->target->getVectorWidth());
#endif
#if ISPC_LLVM_VERSION > ISPC_VERSION_3_2 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
llvm::DIArray subArray = m->diBuilder->getOrCreateArray(sub);
llvm::DIType unifType = GetAsUniformType()->GetDIType(scope);
uint64_t size = unifType.getSizeInBits() * g->target->getVectorWidth();
uint64_t align = unifType.getAlignInBits() * g->target->getVectorWidth();
#else // LLVM 3.7+
llvm::DINodeArray subArray = m->diBuilder->getOrCreateArray(sub);
llvm::DIType *unifType = GetAsUniformType()->GetDIType(scope);
//llvm::DebugNodeArray subArray = m->diBuilder->getOrCreateArray(sub);
//llvm::MDType *unifType = GetAsUniformType()->GetDIType(scope);
uint64_t size = unifType->getSizeInBits() * g->target->getVectorWidth();
uint64_t align = unifType->getAlignInBits()* g->target->getVectorWidth();
#endif
return m->diBuilder->createVectorType(size, align, unifType, subArray);
}
else {
Assert(variability == Variability::SOA);
ArrayType at(GetAsUniformType(), variability.soaWidth);
return at.GetDIType(scope);
}
}
///////////////////////////////////////////////////////////////////////////
// PolyType
const PolyType *PolyType::UniformInteger =
new PolyType(PolyType::TYPE_INTEGER, Variability::Uniform, false);
const PolyType *PolyType::VaryingInteger =
new PolyType(PolyType::TYPE_INTEGER, Variability::Varying, false);
const PolyType *PolyType::UniformFloating =
new PolyType(PolyType::TYPE_FLOATING, Variability::Uniform, false);
const PolyType *PolyType::VaryingFloating =
new PolyType(PolyType::TYPE_FLOATING, Variability::Varying, false);
const PolyType *PolyType::UniformNumber =
new PolyType(PolyType::TYPE_NUMBER, Variability::Uniform, false);
const PolyType *PolyType::VaryingNumber =
new PolyType(PolyType::TYPE_NUMBER, Variability::Varying, false);
const Type *
PolyType::ReplaceType(const Type *from, const Type *to) {
const Type *t = to;
if (from->IsPointerType()) {
t = new PointerType(to,
from->GetVariability(),
from->IsConstType());
} else if (from->IsArrayType()) {
t = new ArrayType(to,
CastType<ArrayType>(from)->GetElementCount());
} else if (from->IsReferenceType()) {
t = new ReferenceType(to);
}
if (from->IsVaryingType())
t = t->GetAsVaryingType();
if (g->debugPrint) {
fprintf(stderr, "Replacing type \"%s\" with \"%s\"\n",
from->GetString().c_str(),
t->GetString().c_str());
}
return t;
}
bool
PolyType::Less(const Type *a, const Type *b) {
const PolyType *pa = CastType<PolyType>(a->GetBaseType());
const PolyType *pb = CastType<PolyType>(b->GetBaseType());
if (!pa || !pb) {
char buf[1024];
snprintf(buf, 1024, "Calling lPolyTypeLess on non-polymorphic types"
"\"%s\" and \"%s\"\n",
a->GetString().c_str(), b->GetString().c_str());
FATAL(buf);
}
if (pa->restriction < pb->restriction)
return true;
if (pa->restriction > pb->restriction)
return false;
if (pa->GetQuant() < pb->GetQuant())
return true;
return false;
}
PolyType::PolyType(PolyRestriction r, Variability v, bool ic)
: Type(POLY_TYPE), restriction(r), variability(v), isConst(ic), quant(-1) {
asOtherConstType = NULL;
asUniformType = asVaryingType = NULL;
expandedTypes = NULL;
}
PolyType::PolyType(PolyRestriction r, Variability v, bool ic, int q)
: Type(POLY_TYPE), restriction(r), variability(v), isConst(ic), quant(q) {
asOtherConstType = NULL;
asUniformType = asVaryingType = NULL;
expandedTypes = NULL;
}
Variability
PolyType::GetVariability() const {
return variability;
}
int
PolyType::GetQuant() const {
return quant;
}
bool
PolyType::IsFloatType() const {
return (restriction == TYPE_FLOATING);
}
bool
PolyType::IsIntType() const {
return (restriction == TYPE_INTEGER);
}
bool
PolyType::IsUnsignedType() const {
return false;
}
bool
PolyType::IsBoolType() const {
return false;
}
bool
PolyType::IsConstType() const {
return isConst;
}
const PolyType *
PolyType::GetAsUnsignedType() const {
return NULL;
}
const PolyType *
PolyType::GetAsConstType() const {
if (isConst == true)
return this;
if (asOtherConstType == NULL) {
asOtherConstType = new PolyType(restriction, variability, true, quant);
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
}
const PolyType *
PolyType::GetAsNonConstType() const {
if (isConst == false)
return this;
if (asOtherConstType == NULL) {
asOtherConstType = new PolyType(restriction, variability, false, quant);
asOtherConstType->asOtherConstType = this;
}
return asOtherConstType;
}
const PolyType *
PolyType::GetBaseType() const {
return this;
}
const PolyType *
PolyType::GetAsVaryingType() const {
if (variability == Variability::Varying)
return this;
if (asVaryingType == NULL) {
asVaryingType = new PolyType(restriction, Variability::Varying,
isConst, quant);
if (variability == Variability::Uniform)
asVaryingType->asUniformType = this;
}
return asVaryingType;
}
const PolyType *
PolyType::GetAsUniformType() const {
if (variability == Variability::Uniform)
return this;
if (asUniformType == NULL) {
asUniformType = new PolyType(restriction, Variability::Uniform,
isConst, quant);
if (variability == Variability::Varying)
asUniformType->asVaryingType = this;
}
return asUniformType;
}
const std::vector<AtomicType *>::iterator
PolyType::ExpandBegin() const {
if (expandedTypes)
return expandedTypes->begin();
expandedTypes = new std::vector<AtomicType *>();
if (restriction == TYPE_INTEGER || restriction == TYPE_NUMBER) {
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_INT8, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_UINT8, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_INT16, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_UINT16, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_INT32, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_UINT32, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_INT64, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_UINT64, variability, isConst));
}
if (restriction == TYPE_FLOATING || restriction == TYPE_NUMBER) {
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_FLOAT, variability, isConst));
expandedTypes->push_back(new AtomicType(AtomicType::TYPE_DOUBLE, variability, isConst));
}
return expandedTypes->begin();
}
const std::vector<AtomicType *>::iterator
PolyType::ExpandEnd() const {
Assert(expandedTypes != NULL);
return expandedTypes->end();
}
const PolyType *
PolyType::GetAsUnboundVariabilityType() const {
if (variability == Variability::Unbound)
return this;
return new PolyType(restriction, Variability::Unbound, isConst, quant);
}
const PolyType *
PolyType::GetAsSOAType(int width) const {
if (variability == Variability(Variability::SOA, width))
return this;
return new PolyType(restriction, Variability(Variability::SOA, width),
isConst, quant);
}
const PolyType *
PolyType::ResolveUnboundVariability(Variability v) const {
Assert(v != Variability::Unbound);
if (variability != Variability::Unbound)
return this;
return new PolyType(restriction, v, isConst, quant);
}
const PolyType *
PolyType::Quantify(int q) const {
return new PolyType(restriction, variability, isConst, q);
}
bool
PolyType::CanBeType(const Type *t) const {
const PolyType *pt = CastType<PolyType>(t);
if (pt) {
return (restriction == pt->restriction ||
restriction == TYPE_NUMBER);
}
const AtomicType *at = CastType<AtomicType>(t);
if (at) {
switch (restriction) {
case TYPE_INTEGER:
return at->IsIntType();
case TYPE_FLOATING:
return at->IsFloatType();
case TYPE_NUMBER:
return at->IsIntType() || at->IsFloatType();
default:
FATAL("Unmatched case for polymorphic restriction");
}
}
// not an atomic type or polymorphic type
return false;
}
std::string
PolyType::GetString() const {
std::string ret;
if (isConst) ret += "const ";
ret += variability.GetString();
ret += " ";
switch (restriction) {
case TYPE_INTEGER: ret += "integer"; break;
case TYPE_FLOATING: ret += "floating"; break;
case TYPE_NUMBER: ret += "number"; break;
default: FATAL("Logic error in PolyType::GetString()");
}
if (quant >= 0) {
ret += "$";
ret += std::to_string(quant);
}
return ret;
}
std::string
PolyType::Mangle() const {
std::string ret;
if (isConst) ret += "C";
ret += variability.MangleString();