forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intrinsics.cpp
1370 lines (1288 loc) · 53.2 KB
/
intrinsics.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
namespace JL_I {
enum intrinsic {
// wrap and unwrap
box=0, unbox,
// arithmetic
neg_int, add_int, sub_int, mul_int,
sdiv_int, udiv_int, srem_int, urem_int, smod_int,
neg_float, add_float, sub_float, mul_float, div_float, rem_float,
fma_float, muladd_float,
// fast arithmetic
neg_float_fast, add_float_fast, sub_float_fast,
mul_float_fast, div_float_fast, rem_float_fast,
// same-type comparisons
eq_int, ne_int,
slt_int, ult_int,
sle_int, ule_int,
eq_float, ne_float,
lt_float, le_float,
eq_float_fast, ne_float_fast,
lt_float_fast, le_float_fast,
fpiseq, fpislt,
// bitwise operators
and_int, or_int, xor_int, not_int, shl_int, lshr_int, ashr_int,
bswap_int, ctpop_int, ctlz_int, cttz_int,
// conversion
sext_int, zext_int, trunc_int,
fptoui, fptosi, uitofp, sitofp,
fptrunc, fpext,
// checked conversion
checked_fptosi, checked_fptoui,
checked_trunc_sint, checked_trunc_uint, check_top_bit,
// checked arithmetic
checked_sadd, checked_uadd, checked_ssub, checked_usub,
checked_smul, checked_umul,
nan_dom_err,
// functions
abs_float, copysign_float, flipsign_int, select_value,
ceil_llvm, floor_llvm, trunc_llvm, rint_llvm,
sqrt_llvm, powi_llvm,
sqrt_llvm_fast,
// pointer access
pointerref, pointerset,
// c interface
ccall, cglobal, jl_alloca, llvmcall
};
};
using namespace JL_I;
#include "ccall.cpp"
/*
low-level intrinsics design:
functions like add_int expect unboxed values of matching bit-length.
every operation that can return an unboxed value does so.
this maximizes opportunities for composing functions without
unnecessary boxing.
this means that box and unbox functions might do nothing except change
the type tag of a value.
boxing is delayed until absolutely necessary, and handled at the point
where the box is needed.
*/
static Type *FTnbits(size_t nb)
{
#ifndef DISABLE_FLOAT16
if (nb == 16)
return Type::getHalfTy(jl_LLVMContext);
else
#endif
if (nb == 32)
return Type::getFloatTy(jl_LLVMContext);
else if (nb == 64)
return Type::getDoubleTy(jl_LLVMContext);
else if (nb == 128)
return Type::getFP128Ty(jl_LLVMContext);
else
jl_error("Unsupported Float Size");
}
// convert int type to same-size float type
static Type *FT(Type *t)
{
if (t->isFloatingPointTy())
return t;
return FTnbits(t->getPrimitiveSizeInBits());
}
// reinterpret-cast to float
static Value *FP(Value *v)
{
if (v->getType()->isFloatingPointTy())
return v;
return builder.CreateBitCast(v, FT(v->getType()));
}
// convert float type to same-size int type
static Type *JL_INTT(Type *t)
{
if (t->isIntegerTy())
return t;
if (t->isPointerTy())
return T_size;
if (t == T_float32) return T_int32;
assert(t == T_float64);
return T_int64;
}
// reinterpret-cast to int
static Value *JL_INT(Value *v)
{
Type *t = v->getType();
if (t->isIntegerTy())
return v;
if (t->isPointerTy())
return builder.CreatePtrToInt(v, JL_INTT(t));
return builder.CreateBitCast(v, JL_INTT(t));
}
static Value *uint_cnvt(Type *to, Value *x)
{
Type *t = x->getType();
if (t == to) return x;
if (to->getPrimitiveSizeInBits() < x->getType()->getPrimitiveSizeInBits())
return builder.CreateTrunc(x, to);
return builder.CreateZExt(x, to);
}
#define LLVM_FP(a,b) APFloat(a,b)
static Constant *julia_const_to_llvm(jl_value_t *e)
{
jl_value_t *jt = jl_typeof(e);
jl_datatype_t *bt = (jl_datatype_t*)jt;
if (!jl_is_datatype(bt) || bt == jl_gensym_type)
return NULL;
if (e == jl_true)
return ConstantInt::get(T_int1, 1);
if (e == jl_false)
return ConstantInt::get(T_int1, 0);
if (jl_is_cpointer_type(jt))
return ConstantExpr::getIntToPtr(ConstantInt::get(T_size, jl_unbox_long(e)), julia_type_to_llvm((jl_value_t*)bt));
if (jl_is_bitstype(jt)) {
int nb = jl_datatype_size(bt);
//TODO: non-power-of-2 size datatypes may not be interpreted correctly on big-endian systems
switch (nb) {
case 1: {
uint8_t data8 = *(uint8_t*)jl_data_ptr(e);
return ConstantInt::get(T_int8, data8);
}
case 2: {
uint16_t data16 = *(uint16_t*)jl_data_ptr(e);
#ifndef DISABLE_FLOAT16
if (jl_is_float(e)) {
return ConstantFP::get(jl_LLVMContext,LLVM_FP(APFloat::IEEEhalf,APInt(16,data16)));
}
#endif
return ConstantInt::get(T_int16, data16);
}
case 4: {
uint32_t data32 = *(uint32_t*)jl_data_ptr(e);
if (jl_is_float(e)) {
return ConstantFP::get(jl_LLVMContext,LLVM_FP(APFloat::IEEEsingle,APInt(32,data32)));
}
return ConstantInt::get(T_int32, data32);
}
case 8: {
uint64_t data64 = *(uint64_t*)jl_data_ptr(e);
if (jl_is_float(e)) {
return ConstantFP::get(jl_LLVMContext,LLVM_FP(APFloat::IEEEdouble,APInt(64,data64)));
}
return ConstantInt::get(T_int64, data64);
}
default:
size_t nw = (nb+sizeof(uint64_t)-1)/sizeof(uint64_t);
uint64_t *data = (uint64_t*)jl_data_ptr(e);
APInt val;
#if !defined(_P64)
// malloc may not be 16-byte aligned on P32,
// but we must ensure that llvm's uint64_t reads don't fall
// off the end of a page
// where 16-byte alignment requirement == (8-byte typetag) % (uint64_t ArrayRef access)
if (nb % 16 != 0) {
uint64_t *data_a64 = (uint64_t*)alloca(sizeof(uint64_t)*nw);
memcpy(data_a64, data, nb);
val = APInt(8*nb, ArrayRef<uint64_t>(data_a64, nw));
}
else
#endif
val = APInt(8*nb, ArrayRef<uint64_t>(data, nw));
if (nb == 16 && jl_is_float(e)) {
return ConstantFP::get(jl_LLVMContext,LLVM_FP(APFloat::IEEEquad,val));
// If we have a floating point type that's not hardware supported, just treat it like an integer for LLVM purposes
}
return ConstantInt::get(IntegerType::get(jl_LLVMContext,8*nb),val);
}
}
if (jl_isbits(jt)) {
size_t nf = jl_datatype_nfields(bt), i;
size_t llvm_nf = 0;
Constant **fields = (Constant**)alloca(nf * sizeof(Constant*));
jl_value_t *f=NULL;
JL_GC_PUSH1(&f);
for(i=0; i < nf; i++) {
f = jl_get_nth_field(e, i);
Constant *val;
if (f == jl_true)
val = ConstantInt::get(T_int8,1);
else if (f == jl_false)
val = ConstantInt::get(T_int8,0);
else
val = julia_const_to_llvm(f);
if (val == NULL) {
JL_GC_POP();
return NULL;
}
if (val->getType() != NoopType)
fields[llvm_nf++] = val;
}
JL_GC_POP();
Type *t = julia_struct_to_llvm(jt);
if (t == T_void || t->isEmptyTy())
return UndefValue::get(NoopType);
if (t->isStructTy()) {
StructType *st = dyn_cast<StructType>(t);
assert(st);
return ConstantStruct::get(st, ArrayRef<Constant*>(fields,llvm_nf));
}
else if (t->isVectorTy()) {
return ConstantVector::get(ArrayRef<Constant*>(fields,llvm_nf));
}
else {
assert(t->isArrayTy());
ArrayType *at = dyn_cast<ArrayType>(t);
assert(at);
return ConstantArray::get(at, ArrayRef<Constant*>(fields,llvm_nf));
}
}
return NULL;
}
static Value *emit_unboxed(jl_value_t *e, jl_codectx_t *ctx)
{
Constant *c = julia_const_to_llvm(e);
if (c) return mark_julia_type(c, jl_typeof(e));
return emit_expr(e, ctx, false);
}
static Value *ghostValue(jl_value_t *ty);
// emit code to unpack a raw value from a box
static Value *emit_unbox(Type *to, Value *x, jl_value_t *jt)
{
Type *ty = (x == NULL) ? NULL : x->getType();
if (x == NULL || ty == NoopType) {
if (to == T_void) {
if (jt != NULL)
return (ty == NoopType && julia_type_of(x) == jt) ? x : ghostValue(jt);
return NULL;
}
return UndefValue::get(to);
}
if (ty != jl_pvalue_llvmt) {
// bools are stored internally as int8 (for now)
if (ty == T_int1 && to == T_int8)
return builder.CreateZExt(x, T_int8);
if (ty->isPointerTy() && !to->isPointerTy())
return builder.CreatePtrToInt(x, to);
if (!ty->isPointerTy() && to->isPointerTy())
return builder.CreateIntToPtr(x, to);
if (ty != to) {
// this can happen when a branch yielding a different type ends
// up being dead code, and type inference knows that the other
// branch's type is the only one that matters.
// assert(ty == T_void);
return UndefValue::get(to);
}
return x;
}
Value *p = data_pointer(x);
if (to == T_int1) {
// bools stored as int8, so an extra Trunc is needed to get an int1
return builder.CreateTrunc(builder.
CreateLoad(builder.
CreateBitCast(p, T_pint8), false),
T_int1);
}
if (to->isStructTy() && !to->isSized()) {
// empty struct - TODO - is this a good way to represent it?
assert(to != T_void);
return UndefValue::get(to);
}
// TODO: stricter alignment if possible
return builder.CreateAlignedLoad(builder.CreateBitCast(p, to->getPointerTo()), sizeof(void*), false);
}
// unbox trying to determine type automatically
static Value *auto_unbox(jl_value_t *x, jl_codectx_t *ctx)
{
Value *v = emit_unboxed(x, ctx);
if (v->getType() != jl_pvalue_llvmt) {
return v;
}
jl_value_t *bt = expr_type(x, ctx);
if (!jl_is_bitstype(bt)) {
if (jl_is_symbol(x)) {
std::map<jl_sym_t*,jl_varinfo_t>::iterator it = ctx->vars.find((jl_sym_t*)x);
if (it != ctx->vars.end())
bt = (*it).second.declType;
}
if (bt == NULL || !jl_is_bitstype(bt)) {
// TODO: make sure this code is valid; hopefully it is
// unreachable but it should still be well-formed.
emit_error("auto_unbox: unable to determine argument type", ctx);
// This isn't correct but probably most likely to cause
// the least amount of trouble
return UndefValue::get(T_int64);
}
}
Type *to = julia_type_to_llvm(bt);
if (to == NULL || to == jl_pvalue_llvmt) {
unsigned int nb = jl_datatype_size(bt)*8;
to = IntegerType::get(jl_LLVMContext, nb);
}
if (to == T_void) {
return NULL;
}
return emit_unbox(to, v, bt);
}
// figure out how many bits a bitstype has at compile time, or -1
int try_to_determine_bitstype_nbits(jl_value_t *targ, jl_codectx_t *ctx)
{
jl_value_t *et = expr_type(targ, ctx);
if (jl_is_type_type(et)) {
jl_value_t *p = jl_tparam0(et);
if (p == (jl_value_t*)jl_bool_type)
return 1;
if (jl_is_bitstype(p))
return jl_datatype_size(p)*8;
if (jl_is_typevar(p)) {
jl_value_t *ub = ((jl_tvar_t*)p)->ub;
if (jl_is_bitstype(ub))
return jl_datatype_size(ub)*8;
}
}
return -1;
}
// unbox using user-specified type
static Value *generic_unbox(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
jl_value_t *et = expr_type(targ, ctx);
if (jl_is_type_type(et)) {
jl_value_t *p = jl_tparam0(et);
if (jl_is_leaf_type(p)) {
Type *to = julia_type_to_llvm(p);
return emit_unbox(to, emit_unboxed(x,ctx), p);
}
}
int nb = try_to_determine_bitstype_nbits(targ, ctx);
if (nb == -1) {
jl_value_t *bt=NULL;
JL_TRY {
bt = jl_interpret_toplevel_expr_in(ctx->module, targ,
jl_svec_data(ctx->sp),
jl_svec_len(ctx->sp)/2);
}
JL_CATCH {
}
if (bt == NULL || !jl_is_bitstype(bt)) {
//jl_error("unbox: could not determine argument size");
emit_error("unbox: could not determine argument size", ctx);
return UndefValue::get(T_void);
}
nb = (bt==(jl_value_t*)jl_bool_type) ? 1 : jl_datatype_size(bt)*8;
}
Type *to = IntegerType::get(jl_LLVMContext, nb);
return emit_unbox(to, emit_unboxed(x, ctx), et);
}
static Value *generic_box(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
int nb = try_to_determine_bitstype_nbits(targ, ctx);
Type *llvmt = NULL;
jl_value_t *bt = NULL;
jl_value_t *et = expr_type(targ, ctx);
if (jl_is_type_type(et) && jl_is_leaf_type(jl_tparam0(et)) &&
jl_is_bitstype(jl_tparam0(et))) {
bt = jl_tparam0(et);
}
else {
JL_TRY {
bt = jl_interpret_toplevel_expr_in(ctx->module, targ,
jl_svec_data(ctx->sp),
jl_svec_len(ctx->sp)/2);
}
JL_CATCH {
}
}
if (bt == NULL) {
}
else if (!jl_is_bitstype(bt)) {
emit_error("reinterpret: expected bits type as first argument", ctx);
return UndefValue::get(jl_pvalue_llvmt);
}
else {
llvmt = julia_type_to_llvm(bt);
if (llvmt == jl_pvalue_llvmt) {
// this happens if !jl_is_leaf_type(bt)
llvmt = NULL;
bt = NULL;
}
if (nb == -1)
nb = (bt==(jl_value_t*)jl_bool_type) ? 1 : jl_datatype_size(bt)*8;
}
if (nb == -1) {
emit_error("box: could not determine argument size", ctx);
return UndefValue::get(jl_pvalue_llvmt);
}
if (llvmt == NULL)
llvmt = IntegerType::get(jl_LLVMContext, nb);
Value *vx = auto_unbox(x, ctx);
Type *vxt = vx->getType();
//if (vx->getType()->getPrimitiveSizeInBits() != (unsigned)nb)
// jl_errorf("box: expected argument with %d bits, got %d", nb,
// vx->getType()->getPrimitiveSizeInBits());
if (vxt != llvmt) {
if (vxt == T_void)
return vx;
if (!vxt->isSingleValueType()) {
jl_error("box: argument not of a primitive type");
}
if (llvmt == T_int1) {
vx = builder.CreateTrunc(vx, llvmt);
}
else if (vxt == T_int1 && llvmt == T_int8) {
vx = builder.CreateZExt(vx, llvmt);
}
else {
// getPrimitiveSizeInBits() == 0 for pointers
if (vxt->getPrimitiveSizeInBits() != llvmt->getPrimitiveSizeInBits() &&
!(vxt->isPointerTy() && llvmt->getPrimitiveSizeInBits() == sizeof(void*)*8) &&
!(llvmt->isPointerTy() && vxt->getPrimitiveSizeInBits() == sizeof(void*)*8)) {
emit_error("box: argument is of incorrect size", ctx);
return UndefValue::get(llvmt);
}
// PtrToInt and IntToPtr ignore size differences
if (vxt->isPointerTy() && !llvmt->isPointerTy()) {
vx = builder.CreatePtrToInt(vx, llvmt);
}
else if (!vxt->isPointerTy() && llvmt->isPointerTy()) {
vx = builder.CreateIntToPtr(vx, llvmt);
}
else {
vx = builder.CreateBitCast(vx, llvmt);
}
}
}
if (bt != NULL) {
return mark_julia_type(vx, bt);
}
// dynamically-determined type; evaluate.
return allocate_box_dynamic(emit_expr(targ, ctx), ConstantInt::get(T_size,(nb+7)/8), vx);
}
static Type *staticeval_bitstype(jl_value_t *targ, const char *fname, jl_codectx_t *ctx)
{
jl_value_t *bt =
jl_interpret_toplevel_expr_in(ctx->module, targ,
jl_svec_data(ctx->sp),
jl_svec_len(ctx->sp)/2);
if (!jl_is_bitstype(bt))
jl_errorf("%s: expected bits type as first argument", fname);
Type *to = julia_type_to_llvm(bt);
if (to == NULL) {
unsigned int nb = jl_datatype_size(bt)*8;
to = IntegerType::get(jl_LLVMContext, nb);
}
return to;
}
// NOTE: signd (signed) only relevant if check == true
static Value *generic_trunc(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx, bool check, bool signd)
{
Type *to = staticeval_bitstype(targ, "trunc_int", ctx);
Value *ix = JL_INT(auto_unbox(x,ctx));
Value *ans = builder.CreateTrunc(ix, to);
if (check) {
Value *back = signd ? builder.CreateSExt(ans, ix->getType()) :
builder.CreateZExt(ans, ix->getType());
raise_exception_unless(builder.CreateICmpEQ(back, ix),
prepare_global(jlinexacterr_var), ctx);
}
return ans;
}
static Value *generic_sext(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
Type *to = staticeval_bitstype(targ, "sext_int", ctx);
return builder.CreateSExt(JL_INT(auto_unbox(x,ctx)), to);
}
static Value *generic_zext(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
Type *to = staticeval_bitstype(targ, "zext_int", ctx);
return builder.CreateZExt(JL_INT(auto_unbox(x,ctx)), to);
}
static Value *emit_eqfsi(Value *x, Value *y)
{
x = FP(x);
Value *fy = JL_INT(y);
// using all 64-bit is slightly faster than using mixed sizes
Value *xx = x, *vv = fy;
if (x->getType() == T_float32)
xx = builder.CreateFPExt(xx, T_float64);
if (vv->getType()->getPrimitiveSizeInBits() < 64)
vv = builder.CreateSExt(vv, T_int64);
Value *back = builder.CreateSIToFP(vv, xx->getType());
return builder.CreateAnd
(builder.CreateFCmpOEQ(xx, back),
builder.CreateICmpEQ(vv, builder.CreateFPToSI(back, vv->getType())));
}
static Value *emit_eqfui(Value *x, Value *y)
{
x = FP(x);
Value *fy = JL_INT(y);
// using all 64-bit is slightly faster than using mixed sizes
Value *xx = x, *vv = fy;
if (x->getType() == T_float32)
xx = builder.CreateFPExt(xx, T_float64);
if (vv->getType()->getPrimitiveSizeInBits() < 64)
vv = builder.CreateZExt(vv, T_int64);
Value *back = builder.CreateUIToFP(vv, xx->getType());
return builder.CreateAnd
(builder.CreateFCmpOEQ(xx, back),
builder.CreateICmpEQ(vv, builder.CreateFPToUI(back, vv->getType())));
}
static Value *emit_checked_fptosi(Type *to, Value *x, jl_codectx_t *ctx)
{
x = FP(x);
Value *v = builder.CreateFPToSI(x, to);
if (x->getType() == T_float32 && to == T_int32) {
raise_exception_unless
(builder.CreateFCmpOEQ(builder.CreateFPExt(x, T_float64),
builder.CreateSIToFP(v, T_float64)),
prepare_global(jlinexacterr_var), ctx);
}
else {
raise_exception_unless(emit_eqfsi(x, v), prepare_global(jlinexacterr_var), ctx);
}
return v;
}
static Value *emit_checked_fptosi(jl_value_t *targ, Value *x, jl_codectx_t *ctx)
{
return emit_checked_fptosi(staticeval_bitstype(targ, "checked_fptosi", ctx), x, ctx);
}
static Value *emit_checked_fptoui(Type *to, Value *x, jl_codectx_t *ctx)
{
x = FP(x);
Value *v = builder.CreateFPToUI(x, to);
if (x->getType() == T_float32 && to == T_int32) {
raise_exception_unless
(builder.CreateFCmpOEQ(builder.CreateFPExt(x, T_float64),
builder.CreateUIToFP(v, T_float64)),
prepare_global(jlinexacterr_var), ctx);
}
else {
raise_exception_unless(emit_eqfui(x, v), prepare_global(jlinexacterr_var), ctx);
}
return v;
}
static Value *emit_checked_fptoui(jl_value_t *targ, Value *x, jl_codectx_t *ctx)
{
return emit_checked_fptoui(staticeval_bitstype(targ, "checked_fptoui", ctx), x, ctx);
}
static Value *emit_runtime_pointerref(jl_value_t *e, jl_value_t *i, jl_codectx_t *ctx)
{
Value *preffunc =
jl_Module->getOrInsertFunction("jl_pointerref",
FunctionType::get(jl_pvalue_llvmt, two_pvalue_llvmt, false));
int ldepth = ctx->argDepth;
Value *parg = emit_boxed_rooted(e, ctx);
Value *iarg = boxed(emit_expr(i, ctx), ctx);
Value *ret = builder.CreateCall2(prepare_call(preffunc), parg, iarg);
ctx->argDepth = ldepth;
return ret;
}
static Value *emit_pointerref(jl_value_t *e, jl_value_t *i, jl_codectx_t *ctx)
{
jl_value_t *aty = expr_type(e, ctx);
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerref(e, i, ctx);
//jl_error("pointerref: expected pointer type as first argument");
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerref(e, i, ctx);
//jl_error("pointerref: invalid pointer");
if (expr_type(i, ctx) != (jl_value_t*)jl_long_type)
return emit_runtime_pointerref(e, i, ctx);
//jl_error("pointerref: invalid index type");
Value *thePtr = auto_unbox(e,ctx);
Value *idx = emit_unbox(T_size, emit_unboxed(i, ctx), (jl_value_t*)jl_long_type);
Value *im1 = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
if (!jl_isbits(ety)) {
if (ety == (jl_value_t*)jl_any_type)
return builder.CreateLoad(builder.CreateGEP(
builder.CreateBitCast(thePtr, jl_ppvalue_llvmt),
im1));
if (!jl_is_structtype(ety) || jl_is_array_type(ety) || !jl_is_leaf_type(ety)) {
emit_error("pointerref: invalid pointer type", ctx);
return NULL;
}
assert(jl_is_datatype(ety));
uint64_t size = jl_datatype_size(ety);
Value *strct =
builder.CreateCall(prepare_call(jlallocobj_func),
ConstantInt::get(T_size,
sizeof(void*)+size));
builder.CreateStore(literal_pointer_val((jl_value_t*)ety),
emit_typeptr_addr(strct));
im1 = builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, ((jl_datatype_t*)ety)->alignment)));
thePtr = builder.CreateGEP(builder.CreateBitCast(thePtr, T_pint8), im1);
builder.CreateMemCpy(builder.CreateBitCast(strct, T_pint8),
thePtr, size, 1);
return mark_julia_type(strct, ety);
}
// TODO: alignment?
return typed_load(thePtr, im1, ety, ctx, tbaa_user, 1);
}
static Value *emit_runtime_pointerset(jl_value_t *e, jl_value_t *x, jl_value_t *i, jl_codectx_t *ctx)
{
Value *psetfunc =
jl_Module->getOrInsertFunction("jl_pointerset",
FunctionType::get(T_void, three_pvalue_llvmt, false));
int ldepth = ctx->argDepth;
Value *parg = emit_boxed_rooted(e, ctx);
Value *iarg = emit_boxed_rooted(i, ctx);
Value *xarg = boxed(emit_expr(x, ctx), ctx);
builder.CreateCall3(prepare_call(psetfunc), parg, xarg, iarg);
ctx->argDepth = ldepth;
return parg;
}
// e[i] = x
static Value *emit_pointerset(jl_value_t *e, jl_value_t *x, jl_value_t *i, jl_codectx_t *ctx)
{
jl_value_t *aty = expr_type(e, ctx);
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerset(e, x, i, ctx);
//jl_error("pointerset: expected pointer type as first argument");
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerset(e, x, i, ctx);
//jl_error("pointerset: invalid pointer");
jl_value_t *xty = expr_type(x, ctx);
Value *val=NULL;
if (!jl_subtype(xty, ety, 0)) {
val = emit_expr(x,ctx);
emit_typecheck(val, ety, "pointerset: type mismatch in assign", ctx);
}
if (expr_type(i, ctx) != (jl_value_t*)jl_long_type)
return emit_runtime_pointerset(e, x, i, ctx);
//jl_error("pointerset: invalid index type");
Value *idx = emit_unbox(T_size, emit_unboxed(i, ctx),(jl_value_t*)jl_long_type);
Value *im1 = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
Value *thePtr = auto_unbox(e,ctx);
if (!jl_isbits(ety) && ety != (jl_value_t*)jl_any_type) {
if (!jl_is_structtype(ety) || jl_is_array_type(ety) || !jl_is_leaf_type(ety)) {
emit_error("pointerset: invalid pointer type", ctx);
return NULL;
}
if (val==NULL) val = emit_expr(x,ctx,true,true);
assert(val->getType() == jl_pvalue_llvmt); //Boxed
assert(jl_is_datatype(ety));
uint64_t size = ((jl_datatype_t*)ety)->size;
im1 = builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, ((jl_datatype_t*)ety)->alignment)));
builder.CreateMemCpy(builder.CreateGEP(builder.CreateBitCast(thePtr, T_pint8), im1),
builder.CreateBitCast(val, T_pint8), size, 1);
}
else {
if (val == NULL) {
if (ety == (jl_value_t*)jl_any_type)
val = emit_expr(x,ctx);
else
val = emit_unboxed(x,ctx);
}
// TODO: alignment?
typed_store(thePtr, im1, val, ety, ctx, tbaa_user, NULL, 1);
}
return mark_julia_type(thePtr, aty);
}
static Value *emit_srem(Value *x, Value *den, jl_codectx_t *ctx)
{
Type *t = den->getType();
raise_exception_unless(builder.CreateICmpNE(den, ConstantInt::get(t,0)),
prepare_global(jldiverr_var), ctx);
BasicBlock *m1BB = BasicBlock::Create(getGlobalContext(),"minus1",ctx->f);
BasicBlock *okBB = BasicBlock::Create(getGlobalContext(),"oksrem",ctx->f);
BasicBlock *cont = BasicBlock::Create(getGlobalContext(),"after_srem",ctx->f);
PHINode *ret = PHINode::Create(t, 2);
builder.CreateCondBr(builder.CreateICmpEQ(den,ConstantInt::get(t,-1,true)),
m1BB, okBB);
builder.SetInsertPoint(m1BB);
builder.CreateBr(cont);
builder.SetInsertPoint(okBB);
Value *sremval = builder.CreateSRem(x, den);
builder.CreateBr(cont);
builder.SetInsertPoint(cont);
ret->addIncoming(// rem(typemin, -1) is undefined
ConstantInt::get(t,0), m1BB);
ret->addIncoming(sremval, okBB);
builder.Insert(ret);
return ret;
}
// Temporarily switch the builder to fast-math mode if requested
struct math_builder {
FastMathFlags old_fmf;
math_builder(jl_codectx_t *ctx, bool always_fast = false):
old_fmf(builder.getFastMathFlags())
{
if (jl_options.fast_math != JL_OPTIONS_FAST_MATH_OFF &&
(always_fast ||
jl_options.fast_math == JL_OPTIONS_FAST_MATH_ON)) {
FastMathFlags fmf;
fmf.setUnsafeAlgebra();
builder.SetFastMathFlags(fmf);
}
}
IRBuilder<>& operator()() const { return builder; }
~math_builder() {
builder.SetFastMathFlags(old_fmf);
}
};
static Value *emit_smod(Value *x, Value *den, jl_codectx_t *ctx)
{
Type *t = den->getType();
raise_exception_unless(builder.CreateICmpNE(den, ConstantInt::get(t,0)),
prepare_global(jldiverr_var), ctx);
BasicBlock *m1BB = BasicBlock::Create(getGlobalContext(),"minus1",ctx->f);
BasicBlock *okBB = BasicBlock::Create(getGlobalContext(),"oksmod",ctx->f);
BasicBlock *cont = BasicBlock::Create(getGlobalContext(),"after_smod",ctx->f);
PHINode *ret = PHINode::Create(t, 2);
builder.CreateCondBr(builder.CreateICmpEQ(den,ConstantInt::get(t,-1,true)),
m1BB, okBB);
builder.SetInsertPoint(m1BB);
builder.CreateBr(cont);
builder.SetInsertPoint(okBB);
Value *rem = builder.CreateSRem(x,den);
Value *smodval =
builder.
CreateSelect(builder.CreateICmpEQ(builder.CreateICmpSLT(x,ConstantInt::get(t,0)),
builder.CreateICmpSLT(den,ConstantInt::get(t,0))),
// mod == rem for arguments with same sign
rem,
builder.CreateSRem(builder.CreateAdd(den,rem),den));
builder.CreateBr(cont);
builder.SetInsertPoint(cont);
ret->addIncoming(// rem(typemin, -1) is undefined
ConstantInt::get(t,0), m1BB);
ret->addIncoming(smodval, okBB);
builder.Insert(ret);
return ret;
}
#define HANDLE(intr,n) \
case intr: if (nargs!=n) jl_error(#intr": wrong number of arguments");
static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
jl_codectx_t *ctx)
{
switch (f) {
case ccall: return emit_ccall(args, nargs, ctx);
case cglobal: return emit_cglobal(args, nargs, ctx);
case llvmcall: return emit_llvmcall(args, nargs, ctx);
HANDLE(box,2) return generic_box(args[1], args[2], ctx);
HANDLE(unbox,2) return generic_unbox(args[1], args[2], ctx);
HANDLE(trunc_int,2) return generic_trunc(args[1], args[2], ctx, false, false);
HANDLE(checked_trunc_sint,2)
return generic_trunc(args[1], args[2], ctx, true, true);
HANDLE(checked_trunc_uint,2)
return generic_trunc(args[1], args[2], ctx, true, false);
HANDLE(sext_int,2) return generic_sext(args[1], args[2], ctx);
HANDLE(zext_int,2) return generic_zext(args[1], args[2], ctx);
HANDLE(pointerref,2) return emit_pointerref(args[1], args[2], ctx);
HANDLE(pointerset,3) return emit_pointerset(args[1], args[2], args[3], ctx);
HANDLE(checked_fptosi,2) {
Value *x = FP(auto_unbox(args[2], ctx));
return emit_checked_fptosi(args[1], x, ctx);
}
HANDLE(checked_fptoui,2) {
Value *x = FP(auto_unbox(args[2], ctx));
return emit_checked_fptoui(args[1], x, ctx);
}
HANDLE(uitofp,2) return builder.CreateUIToFP(JL_INT(auto_unbox(args[2],ctx)), FTnbits(try_to_determine_bitstype_nbits(args[1],ctx)));
HANDLE(sitofp,2) return builder.CreateSIToFP(JL_INT(auto_unbox(args[2],ctx)), FTnbits(try_to_determine_bitstype_nbits(args[1],ctx)));
case fptoui:
if (nargs == 1) {
Value *x = FP(auto_unbox(args[1], ctx));
return builder.CreateFPToUI(FP(x), JL_INTT(x->getType()));
}
else if (nargs == 2) {
return builder.CreateFPToUI(FP(auto_unbox(args[2],ctx)),
Type::getIntNTy(jl_LLVMContext, try_to_determine_bitstype_nbits(args[1],ctx)));
}
else {
jl_error("fptoui: wrong number of arguments");
}
case fptosi:
if (nargs == 1) {
Value *x = FP(auto_unbox(args[1], ctx));
return builder.CreateFPToSI(FP(x), JL_INTT(x->getType()));
}
else if (nargs == 2) {
return builder.CreateFPToSI(FP(auto_unbox(args[2],ctx)),
Type::getIntNTy(jl_LLVMContext, try_to_determine_bitstype_nbits(args[1],ctx)));
}
else {
jl_error("fptosi: wrong number of arguments");
}
HANDLE(fptrunc,2) return builder.CreateFPTrunc(FP(auto_unbox(args[2],ctx)), FTnbits(try_to_determine_bitstype_nbits(args[1],ctx)));
HANDLE(fpext,2) {
Value *x = auto_unbox(args[2],ctx);
#if JL_NEED_FLOATTEMP_VAR
// Target platform might carry extra precision.
// Force rounding to single precision first. The reason is that it's
// fine to keep working in extended precision as long as it's
// understood that everything is implicitly rounded to 23 bits,
// but if we start looking at more bits we need to actually do the
// rounding first instead of carrying around incorrect low bits.
builder.CreateStore(FP(x), builder.CreateBitCast(prepare_global(jlfloattemp_var),FT(x->getType())->getPointerTo()), true);
x = builder.CreateLoad(builder.CreateBitCast(prepare_global(jlfloattemp_var),FT(x->getType())->getPointerTo()), true);
#endif
return builder.CreateFPExt(x, FTnbits(try_to_determine_bitstype_nbits(args[1],ctx)));
}
HANDLE(select_value,3) {
Value *isfalse = emit_condition(args[1], "select_value", ctx);
jl_value_t *t1 = expr_type(args[2], ctx);
Type *llt1 = julia_type_to_llvm(t1);
jl_value_t *t2 = expr_type(args[3], ctx);
Type *llt2 = julia_type_to_llvm(t2);
int argStart = ctx->argDepth;
Value *ifelse_result;
if (llt1 == jl_pvalue_llvmt && llt2 == jl_pvalue_llvmt) {
Value *arg1 = emit_expr(args[3], ctx, false);
if (arg1->getType() == jl_pvalue_llvmt)
make_gcroot(arg1, ctx);
ifelse_result = builder.CreateSelect(isfalse,
arg1,
emit_expr(args[2], ctx, false));
}
else if (t1 == t2 && llt1 == llt2 && llt1 != jl_pvalue_llvmt) {
Value *x = auto_unbox(args[3], ctx);
ifelse_result = tpropagate(x, builder.CreateSelect(isfalse,
x,
auto_unbox(args[2], ctx)));
}
else {
Value *arg1 = boxed(emit_expr(args[3],ctx,false), ctx, expr_type(args[3],ctx));
make_gcroot(arg1, ctx);
ifelse_result = builder.CreateSelect(isfalse,
arg1,
boxed(emit_expr(args[2],ctx,false), ctx, expr_type(args[2],ctx)));
}
ctx->argDepth = argStart;
return ifelse_result;
}
default: ;
}
if (nargs < 1) jl_error("invalid intrinsic call");
Value *x = auto_unbox(args[1], ctx);
Value *y = NULL;
if (nargs>1) {
y = auto_unbox(args[2], ctx);
}
Value *z = NULL;
if (nargs>2) {
z = auto_unbox(args[3], ctx);
}
Type *t = x->getType();
if (t == T_void || (y && y->getType() == T_void) || (z && z->getType() == T_void))
return t == T_void ? x : y->getType() == T_void ? y : z;
Value *fy;
Value *den;
Value *typemin;
switch (f) {
HANDLE(neg_int,1) return builder.CreateSub(ConstantInt::get(t, 0), JL_INT(x));
HANDLE(add_int,2) return builder.CreateAdd(JL_INT(x), JL_INT(y));
HANDLE(sub_int,2) return builder.CreateSub(JL_INT(x), JL_INT(y));
HANDLE(mul_int,2) return builder.CreateMul(JL_INT(x), JL_INT(y));
HANDLE(sdiv_int,2)
den = JL_INT(y);
t = den->getType();
x = JL_INT(x);
typemin = builder.CreateShl(ConstantInt::get(t,1),
x->getType()->getPrimitiveSizeInBits()-1);
raise_exception_unless(builder.
CreateAnd(builder.
CreateICmpNE(den, ConstantInt::get(t,0)),
builder.
CreateOr(builder.
CreateICmpNE(den,
ConstantInt::get(t,-1,true)),
builder.CreateICmpNE(x, typemin))),
prepare_global(jldiverr_var), ctx);
return builder.CreateSDiv(x, den);
HANDLE(udiv_int,2)
den = JL_INT(y);
t = den->getType();
raise_exception_unless(builder.CreateICmpNE(den, ConstantInt::get(t,0)),
prepare_global(jldiverr_var), ctx);
return builder.CreateUDiv(JL_INT(x), den);
HANDLE(srem_int,2)
return emit_srem(JL_INT(x), JL_INT(y), ctx);
HANDLE(urem_int,2)
den = JL_INT(y);
t = den->getType();
raise_exception_unless(builder.CreateICmpNE(den, ConstantInt::get(t,0)),
prepare_global(jldiverr_var), ctx);
return builder.CreateURem(JL_INT(x), den);
HANDLE(smod_int,2)
return emit_smod(JL_INT(x), JL_INT(y), ctx);
// Implements IEEE negate. Unfortunately there is no compliant way
// to implement this in LLVM 3.4, though there are two different idioms
// that do the correct thing on LLVM <= 3.3 and >= 3.5 respectively.
// See issue #7868
#ifdef LLVM35
HANDLE(neg_float,1) return math_builder(ctx)().CreateFSub(ConstantFP::get(FT(t), -0.0), FP(x));
HANDLE(neg_float_fast,1) return math_builder(ctx, true)().CreateFNeg(FP(x));
#else
HANDLE(neg_float,1)
return math_builder(ctx)().CreateFMul(ConstantFP::get(FT(t), -1.0), FP(x));
HANDLE(neg_float_fast,1)
return math_builder(ctx, true)().CreateFMul(ConstantFP::get(FT(t), -1.0), FP(x));
#endif
HANDLE(add_float,2) return math_builder(ctx)().CreateFAdd(FP(x), FP(y));
HANDLE(sub_float,2) return math_builder(ctx)().CreateFSub(FP(x), FP(y));
HANDLE(mul_float,2) return math_builder(ctx)().CreateFMul(FP(x), FP(y));
HANDLE(div_float,2) return math_builder(ctx)().CreateFDiv(FP(x), FP(y));
HANDLE(rem_float,2) return math_builder(ctx)().CreateFRem(FP(x), FP(y));
HANDLE(add_float_fast,2) return math_builder(ctx, true)().CreateFAdd(FP(x), FP(y));
HANDLE(sub_float_fast,2) return math_builder(ctx, true)().CreateFSub(FP(x), FP(y));
HANDLE(mul_float_fast,2) return math_builder(ctx, true)().CreateFMul(FP(x), FP(y));
HANDLE(div_float_fast,2) return math_builder(ctx, true)().CreateFDiv(FP(x), FP(y));
HANDLE(rem_float_fast,2) return math_builder(ctx, true)().CreateFRem(FP(x), FP(y));
HANDLE(fma_float,3) {
assert(y->getType() == x->getType());
assert(z->getType() == y->getType());
return builder.CreateCall3
(Intrinsic::getDeclaration(jl_Module, Intrinsic::fma,
ArrayRef<Type*>(x->getType())),
FP(x), FP(y), FP(z));
}
HANDLE(muladd_float,3)
#ifdef LLVM34
{
assert(y->getType() == x->getType());
assert(z->getType() == y->getType());
return builder.CreateCall3
(Intrinsic::getDeclaration(jl_Module, Intrinsic::fmuladd,