-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
intrinsics.cpp
1245 lines (1143 loc) · 45.6 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
// This file is a part of Julia. License is MIT: https://julialang.org/license
namespace JL_I {
#include "intrinsics.h"
}
#include "ccall.cpp"
using namespace JL_I;
static Function *runtime_func[num_intrinsics];
static bool float_func[num_intrinsics];
static void jl_init_intrinsic_functions_codegen(Module *m)
{
std::vector<Type *> args1(0); \
args1.push_back(T_pjlvalue); \
std::vector<Type *> args2(0); \
args2.push_back(T_pjlvalue); \
args2.push_back(T_pjlvalue); \
std::vector<Type *> args3(0); \
args3.push_back(T_pjlvalue); \
args3.push_back(T_pjlvalue); \
args3.push_back(T_pjlvalue); \
std::vector<Type *> args4(0); \
args4.push_back(T_pjlvalue); \
args4.push_back(T_pjlvalue); \
args4.push_back(T_pjlvalue); \
args4.push_back(T_pjlvalue);
#define ADD_I(name, nargs) do { \
Function *func = Function::Create(FunctionType::get(T_pjlvalue, args##nargs, false), \
Function::ExternalLinkage, "jl_"#name, m); \
runtime_func[name] = func; \
add_named_global(func, &jl_##name); \
} while (0);
#define ADD_HIDDEN ADD_I
#define ALIAS(alias, base) runtime_func[alias] = runtime_func[base];
INTRINSICS
#undef ADD_I
#undef ADD_HIDDEN
#undef ALIAS
float_func[neg_float] = true;
float_func[neg_float_fast] = true;
float_func[add_float] = true;
float_func[sub_float] = true;
float_func[mul_float] = true;
float_func[div_float] = true;
float_func[rem_float] = true;
float_func[add_float_fast] = true;
float_func[sub_float_fast] = true;
float_func[mul_float_fast] = true;
float_func[div_float_fast] = true;
float_func[rem_float_fast] = true;
float_func[fma_float] = true;
float_func[muladd_float] = true;
float_func[eq_float] = true;
float_func[ne_float] = true;
float_func[lt_float] = true;
float_func[le_float] = true;
float_func[eq_float_fast] = true;
float_func[ne_float_fast] = true;
float_func[lt_float_fast] = true;
float_func[le_float_fast] = true;
float_func[fpiseq] = true;
float_func[fpislt] = true;
float_func[abs_float] = true;
//float_func[copysign_float] = false; // this is actually an integer operation
float_func[ceil_llvm] = true;
float_func[floor_llvm] = true;
float_func[trunc_llvm] = true;
float_func[rint_llvm] = true;
float_func[sqrt_llvm] = true;
float_func[sqrt_llvm_fast] = true;
}
extern "C"
JL_DLLEXPORT uint32_t jl_get_LLVM_VERSION(void)
{
return 10000 * LLVM_VERSION_MAJOR + 100 * LLVM_VERSION_MINOR
#ifdef LLVM_VERSION_PATCH
+ LLVM_VERSION_PATCH
#endif
;
}
/*
low-level intrinsics design:
intrinsics only operate on bitstype values
any composite type is expected to be handled via its constructor,
so it is not permitted here
functions like add_int expect unboxed values of matching types
every operation that can return an unboxed value does so.
this maximizes opportunities for composing functions without
unnecessary boxing.
the bitcast function does nothing except change the type tag
of a value. At the user-level, it is perhaps better known as reinterpret.
boxing is delayed until absolutely necessary, and handled at the point
where the box is needed.
all intrinsics have a non-compiled implementation, this file contains
the optimizations for handling them unboxed
*/
// convert an llvm type to same-size float type
static Type *FLOATT(Type *t)
{
if (t->isFloatingPointTy())
return t;
unsigned nb = (t->isPointerTy() ? sizeof(void*) * 8 : t->getPrimitiveSizeInBits());
if (nb == 64)
return T_float64;
if (nb == 32)
return T_float32;
#ifndef DISABLE_FLOAT16
if (nb == 16)
return T_float16;
#endif
if (nb == 128)
return T_float128;
return NULL;
}
// convert an llvm type to same-size int type
static Type *INTT(Type *t)
{
if (t->isIntegerTy())
return t;
if (t->isPointerTy())
return T_size;
if (t == T_float64)
return T_int64;
if (t == T_float32)
return T_int32;
if (t == T_float16)
return T_int16;
unsigned nb = t->getPrimitiveSizeInBits();
assert(t != T_void && nb > 0);
return IntegerType::get(jl_LLVMContext, nb);
}
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);
}
#if JL_LLVM_VERSION >= 40000
#define LLVM_FP(a,b) APFloat(a(),b)
#else
#define LLVM_FP(a,b) APFloat(a,b)
#endif
static Constant *julia_const_to_llvm(void *ptr, jl_value_t *bt)
{
// assumes `jl_isbits(bt)`.
// `ptr` can point to a inline field, do not read the tag from it.
// make sure to return exactly the type specified by
// julia_type_to_llvm as this will be assumed by the callee.
if (bt == (jl_value_t*)jl_bool_type)
return ConstantInt::get(T_int8, (*(uint8_t*)ptr) ? 1 : 0);
if (bt == (jl_value_t*)jl_ssavalue_type)
return NULL;
if (jl_is_vecelement_type(bt))
bt = jl_tparam0(bt);
if (jl_is_cpointer_type(bt))
return ConstantExpr::getIntToPtr(ConstantInt::get(T_size, *(uintptr_t*)ptr), julia_type_to_llvm(bt));
if (jl_is_primitivetype(bt)) {
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*)ptr;
return ConstantInt::get(T_int8, data8);
}
case 2: {
uint16_t data16 = *(uint16_t*)ptr;
return ConstantInt::get(T_int16, data16);
}
case 4: {
uint32_t data32 = *(uint32_t*)ptr;
if (bt == (jl_value_t*)jl_float32_type)
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*)ptr;
if (bt == (jl_value_t*)jl_float64_type)
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*)ptr;
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));
return ConstantInt::get(IntegerType::get(jl_LLVMContext,8*nb),val);
}
}
size_t nf = jl_datatype_nfields(bt);
Constant **fields = (Constant**)alloca(nf * sizeof(Constant*));
for (size_t i = 0; i < nf; i++) {
size_t offs = jl_field_offset((jl_datatype_t*)bt, i);
jl_value_t *ft = jl_field_type(bt, i);
Constant *val = julia_const_to_llvm((char*)ptr + offs, ft);
if (val == NULL)
return NULL;
fields[i] = val;
}
Type *t = julia_struct_to_llvm(bt, NULL, NULL);
if (type_is_ghost(t))
return UndefValue::get(NoopType);
if (t->isVectorTy())
return ConstantVector::get(ArrayRef<Constant*>(fields, nf));
if (StructType *st = dyn_cast<StructType>(t)) {
return ConstantStruct::get(st, ArrayRef<Constant*>(fields, nf));
}
else {
ArrayType *at = cast<ArrayType>(t);
return ConstantArray::get(at, ArrayRef<Constant*>(fields, nf));
}
}
static Constant *julia_const_to_llvm(jl_value_t *e)
{
if (e == jl_true)
return ConstantInt::get(T_int8, 1);
if (e == jl_false)
return ConstantInt::get(T_int8, 0);
jl_value_t *bt = jl_typeof(e);
if (!jl_isbits(bt))
return NULL;
return julia_const_to_llvm(e, bt);
}
static jl_cgval_t ghostValue(jl_value_t *ty);
// emit code to unpack a raw value from a box into registers or a stack slot
static Value *emit_unbox(Type *to, const jl_cgval_t &x, jl_value_t *jt, Value *dest, bool volatile_store)
{
assert(to != T_void);
// TODO: fully validate that x.typ == jt?
if (x.isghost) {
// 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.
if (type_is_ghost(to)) {
return NULL;
}
//emit_error("emit_unbox: a type mismatch error in occurred during codegen", ctx);
return UndefValue::get(to); // type mismatch error
}
Constant *c = x.constant ? julia_const_to_llvm(x.constant) : NULL;
if (!x.ispointer() || c) { // already unboxed, but sometimes need conversion
Value *unboxed = c ? c : x.V;
Type *ty = unboxed->getType();
assert(ty != T_void);
bool frompointer = ty->isPointerTy();
bool topointer = to->isPointerTy();
if (frompointer && topointer) {
unboxed = emit_bitcast(unboxed, to);
}
else if (frompointer) {
Type *INTT_to = INTT(to);
unboxed = builder.CreatePtrToInt(unboxed, INTT_to);
if (INTT_to != to)
unboxed = builder.CreateBitCast(unboxed, to);
}
else if (topointer) {
Type *INTT_to = INTT(to);
if (to != INTT_to)
unboxed = builder.CreateBitCast(unboxed, INTT_to);
unboxed = builder.CreateIntToPtr(unboxed, to);
}
else if (ty == T_int1 && to == T_int8) {
// bools may be stored internally as int8
unboxed = builder.CreateZExt(unboxed, T_int8);
}
else if (ty != to) {
unboxed = builder.CreateBitCast(unboxed, to);
}
if (!dest)
return unboxed;
builder.CreateStore(unboxed, dest, volatile_store);
return NULL;
}
// if this is a derived pointer, make sure the root usage itself is also visible to the delete-root pass
if (x.gcroot && x.V != x.gcroot)
mark_gc_use(x);
// bools stored as int8, so an extra Trunc is needed to get an int1
Value *p = x.constant ? literal_pointer_val(x.constant) : x.V;
Type *ptype = (to == T_int1 ? T_pint8 : to->getPointerTo());
if (p->getType() != ptype)
p = emit_bitcast(p, ptype);
Value *unboxed = NULL;
if (to == T_int1)
unboxed = builder.CreateTrunc(tbaa_decorate(x.tbaa, builder.CreateLoad(p)), T_int1);
else if (jt == (jl_value_t*)jl_bool_type)
unboxed = builder.CreateZExt(builder.CreateTrunc(tbaa_decorate(x.tbaa, builder.CreateLoad(p)), T_int1), to);
if (unboxed) {
if (!dest)
return unboxed;
builder.CreateStore(unboxed, dest);
return NULL;
}
int alignment;
if (x.isboxed) {
// julia's gc gives 16-byte aligned addresses
alignment = 16;
}
else if (jt) {
alignment = julia_alignment(p, jt, 0);
}
else {
// stack has default alignment
alignment = 0;
}
if (dest) {
// callers using the dest argument only use it for a stack slot for now
alignment = 0;
MDNode *tbaa = x.tbaa;
// the memcpy intrinsic does not allow to specify different alias tags
// for the load part (x.tbaa) and the store part (tbaa_stack).
// since the tbaa lattice has to be a tree we have unfortunately
// x.tbaa ∪ tbaa_stack = tbaa_root if x.tbaa != tbaa_stack
if (tbaa != tbaa_stack)
tbaa = NULL;
builder.CreateMemCpy(dest, p, jl_datatype_size(jt), alignment, volatile_store, tbaa);
return NULL;
}
else {
Instruction *load;
if (alignment)
load = builder.CreateAlignedLoad(p, alignment);
else
load = builder.CreateLoad(p);
return tbaa_decorate(x.tbaa, load);
}
}
static jl_value_t *staticeval_bitstype(const jl_cgval_t &targ)
{
// evaluate an argument at compile time to determine what type it is
if (jl_is_type_type(targ.typ)) {
jl_value_t *bt = jl_tparam0(targ.typ);
if (jl_is_primitivetype(bt))
return bt;
}
return NULL;
}
static jl_cgval_t emit_runtime_call(JL_I::intrinsic f, const jl_cgval_t *argv, size_t nargs, jl_codectx_t *ctx)
{
Value *func = prepare_call(runtime_func[f]);
Value **argvalues = (Value**)alloca(sizeof(Value*) * nargs);
for (size_t i = 0; i < nargs; ++i) {
argvalues[i] = boxed(argv[i], ctx);
}
Value *r = builder.CreateCall(func, makeArrayRef(argvalues, nargs));
return mark_julia_type(r, true, (jl_value_t*)jl_any_type, ctx);
}
// put a bits type tag on some value (despite the name, this doesn't necessarily actually change anything about the value however)
static jl_cgval_t generic_bitcast(const jl_cgval_t *argv, jl_codectx_t *ctx)
{
// Give the arguments names //
const jl_cgval_t &bt_value = argv[0];
const jl_cgval_t &v = argv[1];
jl_value_t *bt = staticeval_bitstype(bt_value);
// it's easier to throw a good error from C than llvm
if (!bt)
return emit_runtime_call(bitcast, argv, 2, ctx);
Type *llvmt = bitstype_to_llvm(bt);
int nb = jl_datatype_size(bt);
// Examine the second argument //
bool isboxed;
Type *vxt = julia_type_to_llvm(v.typ, &isboxed);
if (!jl_is_primitivetype(v.typ) || jl_datatype_size(v.typ) != nb) {
Value *typ = emit_typeof_boxed(v, ctx);
if (!jl_is_primitivetype(v.typ)) {
if (isboxed) {
Value *isbits = emit_datatype_isbitstype(typ);
error_unless(isbits, "bitcast: expected primitive type value for second argument", ctx);
}
else {
emit_error("bitcast: expected primitive type value for second argument", ctx);
return jl_cgval_t();
}
}
if (jl_datatype_size(v.typ) != nb) {
if (isboxed) {
Value *size = emit_datatype_size(typ);
error_unless(builder.CreateICmpEQ(size, ConstantInt::get(T_int32, nb)),
"bitcast: argument size does not match size of target type", ctx);
}
else {
emit_error("bitcast: argument size does not match size of target type", ctx);
return jl_cgval_t();
}
}
}
assert(!v.isghost);
Value *vx = NULL;
if (!v.ispointer())
vx = v.V;
else if (v.constant)
vx = julia_const_to_llvm(v.constant);
if (v.ispointer() && vx == NULL) {
// try to load as original Type, to preserve llvm optimizations
// but if the v.typ is not well known, use llvmt
if (isboxed)
vxt = llvmt;
vx = tbaa_decorate(v.tbaa, builder.CreateLoad(data_pointer(v, ctx,
vxt == T_int1 ? T_pint8 : vxt->getPointerTo())));
}
vxt = vx->getType();
if (vxt != llvmt) {
if (llvmt == T_int1)
vx = builder.CreateTrunc(vx, llvmt);
else if (vxt == T_int1 && llvmt == T_int8)
vx = builder.CreateZExt(vx, llvmt);
else if (vxt->isPointerTy() && !llvmt->isPointerTy())
vx = builder.CreatePtrToInt(vx, llvmt);
else if (!vxt->isPointerTy() && llvmt->isPointerTy())
vx = builder.CreateIntToPtr(vx, llvmt);
else
vx = emit_bitcast(vx, llvmt);
}
if (jl_is_leaf_type(bt)) {
return mark_julia_type(vx, false, bt, ctx);
}
else {
Value *box = emit_allocobj(ctx, nb, boxed(bt_value, ctx));
init_bits_value(box, vx, tbaa_immut);
return mark_julia_type(box, true, bt, ctx);
}
}
static jl_cgval_t generic_cast(
intrinsic f, Value *(*generic)(Type*, Value*, jl_codectx_t*),
const jl_cgval_t *argv, jl_codectx_t *ctx, bool toint, bool fromint)
{
const jl_cgval_t &targ = argv[0];
const jl_cgval_t &v = argv[1];
jl_value_t *jlto = staticeval_bitstype(targ);
if (!jlto || !jl_is_primitivetype(v.typ))
return emit_runtime_call(f, argv, 2, ctx);
Type *to = bitstype_to_llvm(jlto);
Type *vt = bitstype_to_llvm(v.typ);
if (toint)
to = INTT(to);
else
to = FLOATT(to);
if (fromint)
vt = INTT(vt);
else
vt = FLOATT(vt);
if (!to || !vt)
return emit_runtime_call(f, argv, 2, ctx);
Value *from = emit_unbox(vt, v, v.typ);
Value *ans = generic(to, from, ctx);
return mark_julia_type(ans, false, jlto, ctx);
}
static Value *generic_trunc(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateTrunc(x, to);
}
static Value *generic_trunc_uchecked(Type *to, Value *x, jl_codectx_t *ctx)
{
Value *ans = builder.CreateTrunc(x, to);
Value *back = builder.CreateZExt(ans, x->getType());
raise_exception_unless(builder.CreateICmpEQ(back, x),
literal_pointer_val(jl_inexact_exception), ctx);
return ans;
}
static Value *generic_trunc_schecked(Type *to, Value *x, jl_codectx_t *ctx)
{
Value *ans = builder.CreateTrunc(x, to);
Value *back = builder.CreateSExt(ans, x->getType());
raise_exception_unless(builder.CreateICmpEQ(back, x),
literal_pointer_val(jl_inexact_exception), ctx);
return ans;
}
static Value *generic_sext(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateSExt(x, to);
}
static Value *generic_zext(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateZExt(x, to);
}
static Value *generic_uitofp(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateUIToFP(x, to);
}
static Value *generic_sitofp(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateSIToFP(x, to);
}
static Value *generic_fptoui(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateFPToUI(x, to);
}
static Value *generic_fptosi(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateFPToSI(x, to);
}
static Value *generic_fptrunc(Type *to, Value *x, jl_codectx_t *ctx)
{
return builder.CreateFPTrunc(x, to);
}
static Value *generic_fpext(Type *to, Value *x, jl_codectx_t *ctx)
{
#ifdef 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.
Value *jlfloattemp_var = emit_static_alloca(x->getType());
builder.CreateStore(x, jlfloattemp_var);
x = builder.CreateLoad(jlfloattemp_var, true);
#endif
return builder.CreateFPExt(x, to);
}
static jl_cgval_t emit_runtime_pointerref(jl_cgval_t *argv, jl_codectx_t *ctx)
{
return emit_runtime_call(pointerref, argv, 3, ctx);
}
static jl_cgval_t emit_pointerref(jl_cgval_t *argv, jl_codectx_t *ctx)
{
const jl_cgval_t &e = argv[0];
const jl_cgval_t &i = argv[1];
const jl_cgval_t &align = argv[2];
if (align.constant == NULL || !jl_is_long(align.constant))
return emit_runtime_pointerref(argv, ctx);
unsigned align_nb = jl_unbox_long(align.constant);
if (i.typ != (jl_value_t*)jl_long_type)
return emit_runtime_pointerref(argv, ctx);
jl_value_t *aty = e.typ;
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerref(argv, ctx);
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerref(argv, ctx);
if (!jl_is_datatype(ety))
ety = (jl_value_t*)jl_any_type;
Value *idx = emit_unbox(T_size, i, (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) {
Value *thePtr = emit_unbox(T_ppjlvalue, e, e.typ);
return mark_julia_type(
builder.CreateAlignedLoad(builder.CreateGEP(thePtr, im1), align_nb),
true,
ety, ctx);
}
if (!jl_is_structtype(ety) || jl_is_array_type(ety) || !jl_is_leaf_type(ety)) {
emit_error("pointerref: invalid pointer type", ctx);
return jl_cgval_t();
}
assert(jl_is_datatype(ety));
uint64_t size = jl_datatype_size(ety);
Value *strct = emit_allocobj(ctx, size,
literal_pointer_val((jl_value_t*)ety));
im1 = builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, jl_datatype_align(ety))));
Value *thePtr = emit_unbox(T_pint8, e, e.typ);
thePtr = builder.CreateGEP(emit_bitcast(thePtr, T_pint8), im1);
builder.CreateMemCpy(emit_bitcast(strct, T_pint8), thePtr, size, 1);
return mark_julia_type(strct, true, ety, ctx);
}
bool isboxed;
Type *ptrty = julia_type_to_llvm(e.typ, &isboxed);
assert(!isboxed);
Value *thePtr = emit_unbox(ptrty, e, e.typ);
return typed_load(thePtr, im1, ety, ctx, tbaa_data, true, align_nb);
}
static jl_cgval_t emit_runtime_pointerset(jl_cgval_t *argv, jl_codectx_t *ctx)
{
return emit_runtime_call(pointerset, argv, 4, ctx);
}
// e[i] = x
static jl_cgval_t emit_pointerset(jl_cgval_t *argv, jl_codectx_t *ctx)
{
const jl_cgval_t &e = argv[0];
const jl_cgval_t &x = argv[1];
const jl_cgval_t &i = argv[2];
const jl_cgval_t &align = argv[3];
if (align.constant == NULL || !jl_is_long(align.constant))
return emit_runtime_pointerset(argv, ctx);
unsigned align_nb = jl_unbox_long(align.constant);
if (i.typ != (jl_value_t*)jl_long_type)
return emit_runtime_pointerset(argv, ctx);
jl_value_t *aty = e.typ;
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerset(argv, ctx);
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerset(argv, ctx);
if (align.constant == NULL || !jl_is_long(align.constant))
return emit_runtime_pointerset(argv, ctx);
if (!jl_is_datatype(ety))
ety = (jl_value_t*)jl_any_type;
emit_typecheck(x, ety, "pointerset: type mismatch in assign", ctx);
Value *idx = emit_unbox(T_size, i, (jl_value_t*)jl_long_type);
Value *im1 = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
Value *thePtr;
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 jl_cgval_t();
}
thePtr = emit_unbox(T_pint8, e, e.typ);
uint64_t size = jl_datatype_size(ety);
im1 = builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, jl_datatype_align(ety))));
builder.CreateMemCpy(builder.CreateGEP(thePtr, im1),
data_pointer(x, ctx, T_pint8), size, align_nb);
}
else {
bool isboxed;
Type *ptrty = julia_type_to_llvm(e.typ, &isboxed);
assert(!isboxed);
thePtr = emit_unbox(ptrty, e, e.typ);
typed_store(thePtr, im1, x, ety, ctx, tbaa_data, NULL, align_nb);
}
return mark_julia_type(thePtr, false, aty, ctx);
}
static Value *emit_checked_srem_int(Value *x, Value *den, jl_codectx_t *ctx)
{
Type *t = den->getType();
raise_exception_unless(builder.CreateICmpNE(den, ConstantInt::get(t,0)),
literal_pointer_val(jl_diverror_exception), ctx);
BasicBlock *m1BB = BasicBlock::Create(jl_LLVMContext,"minus1",ctx->f);
BasicBlock *okBB = BasicBlock::Create(jl_LLVMContext,"oksrem",ctx->f);
BasicBlock *cont = BasicBlock::Create(jl_LLVMContext,"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();
#if JL_LLVM_VERSION >= 30800
builder.setFastMathFlags(fmf);
#else
builder.SetFastMathFlags(fmf);
#endif
}
}
IRBuilder<>& operator()() const { return builder; }
~math_builder() {
#if JL_LLVM_VERSION >= 30800
builder.setFastMathFlags(old_fmf);
#else
builder.SetFastMathFlags(old_fmf);
#endif
}
};
static Value *emit_untyped_intrinsic(intrinsic f, Value **argvalues, size_t nargs,
jl_codectx_t *ctx, jl_datatype_t **newtyp, jl_value_t *xtyp);
static jl_cgval_t emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
jl_codectx_t *ctx)
{
assert(f < num_intrinsics);
if (f == cglobal && nargs == 1)
f = cglobal_auto;
unsigned expected_nargs = jl_intrinsic_nargs((int)f);
if (expected_nargs && expected_nargs != nargs) {
jl_errorf("intrinsic #%d %s: wrong number of arguments", f, jl_intrinsic_name((int)f));
}
if (f == llvmcall)
return emit_llvmcall(args, nargs, ctx);
if (f == cglobal_auto || f == cglobal)
return emit_cglobal(args, nargs, ctx);
jl_cgval_t *argv = (jl_cgval_t*)alloca(sizeof(jl_cgval_t) * nargs);
for (size_t i = 0; i < nargs; ++i) {
argv[i] = emit_expr(args[i + 1], ctx);
}
// this forces everything to use runtime-intrinsics (e.g. for testing)
// return emit_runtime_call(f, argv, nargs, ctx);
switch (f) {
case arraylen:
return mark_julia_type(emit_arraylen(argv[0], args[1], ctx), false, jl_long_type, ctx);
case pointerref:
return emit_pointerref(argv, ctx);
case pointerset:
return emit_pointerset(argv, ctx);
case bitcast:
return generic_bitcast(argv, ctx);
case trunc_int:
return generic_cast(f, generic_trunc, argv, ctx, true, true);
case checked_trunc_uint:
return generic_cast(f, generic_trunc_uchecked, argv, ctx, true, true);
case checked_trunc_sint:
return generic_cast(f, generic_trunc_schecked, argv, ctx, true, true);
case sext_int:
return generic_cast(f, generic_sext, argv, ctx, true, true);
case zext_int:
return generic_cast(f, generic_zext, argv, ctx, true, true);
case uitofp:
return generic_cast(f, generic_uitofp, argv, ctx, false, true);
case sitofp:
return generic_cast(f, generic_sitofp, argv, ctx, false, true);
case fptoui:
return generic_cast(f, generic_fptoui, argv, ctx, true, false);
case fptosi:
return generic_cast(f, generic_fptosi, argv, ctx, true, false);
case fptrunc:
return generic_cast(f, generic_fptrunc, argv, ctx, false, false);
case fpext:
return generic_cast(f, generic_fpext, argv, ctx, false, false);
case select_value: {
Value *isfalse = emit_condition(argv[0], "select_value", ctx); // emit the first argument
// emit X and Y arguments
const jl_cgval_t &x = argv[1];
const jl_cgval_t &y = argv[2];
jl_value_t *t1 = x.typ;
jl_value_t *t2 = y.typ;
// check the return value was valid
if (t1 == jl_bottom_type && t2 == jl_bottom_type)
return jl_cgval_t(); // undefined
if (t1 == jl_bottom_type)
return y;
if (t2 == jl_bottom_type)
return x;
Value *ifelse_result;
bool isboxed;
Type *llt1 = julia_type_to_llvm(t1, &isboxed);
if (t1 != t2)
isboxed = true;
if (!isboxed) {
if (type_is_ghost(llt1))
return x;
ifelse_result = builder.CreateSelect(isfalse,
emit_unbox(llt1, y, t1),
emit_unbox(llt1, x, t1));
}
else {
ifelse_result = builder.CreateSelect(isfalse,
boxed(y, ctx),
boxed(x, ctx));
}
jl_value_t *jt = (t1 == t2 ? t1 : (jl_value_t*)jl_any_type);
mark_gc_use(x);
mark_gc_use(y);
return mark_julia_type(ifelse_result, isboxed, jt, ctx);
}
case not_int: {
const jl_cgval_t &x = argv[0];
if (!jl_is_primitivetype(x.typ))
return emit_runtime_call(f, argv, nargs, ctx);
Type *xt = INTT(bitstype_to_llvm(x.typ));
Value *from = emit_unbox(xt, x, x.typ);
Value *ans;
if (x.typ == (jl_value_t*)jl_bool_type)
ans = builder.CreateXor(from, ConstantInt::get(T_int8, 1, true));
else
ans = builder.CreateXor(from, ConstantInt::get(xt, -1, true));
return mark_julia_type(ans, false, x.typ, ctx);
}
default: {
assert(nargs >= 1 && "invalid nargs for intrinsic call");
const jl_cgval_t &xinfo = argv[0];
// verify argument types
if (!jl_is_primitivetype(xinfo.typ))
return emit_runtime_call(f, argv, nargs, ctx);
Type *xtyp = bitstype_to_llvm(xinfo.typ);
if (float_func[f])
xtyp = FLOATT(xtyp);
else
xtyp = INTT(xtyp);
if (!xtyp)
return emit_runtime_call(f, argv, nargs, ctx);
Type **argt = (Type**)alloca(sizeof(Type*) * nargs);
argt[0] = xtyp;
if (f == shl_int || f == lshr_int || f == ashr_int) {
if (!jl_is_primitivetype(argv[1].typ))
return emit_runtime_call(f, argv, nargs, ctx);
argt[1] = INTT(bitstype_to_llvm(argv[1].typ));
}
else {
for (size_t i = 1; i < nargs; ++i) {
if (xinfo.typ != argv[i].typ)
return emit_runtime_call(f, argv, nargs, ctx);
argt[i] = xtyp;
}
}
// unbox the arguments
Value **argvalues = (Value**)alloca(sizeof(Value*) * nargs);
for (size_t i = 0; i < nargs; ++i) {
argvalues[i] = emit_unbox(argt[i], argv[i], argv[i].typ);
}
// call the intrinsic
jl_value_t *newtyp = NULL;
Value *r = emit_untyped_intrinsic(f, argvalues, nargs, ctx, (jl_datatype_t**)&newtyp, xinfo.typ);
if (r->getType() == T_int1)
r = builder.CreateZExt(r, T_int8);
return mark_julia_type(r, false, newtyp ? newtyp : xinfo.typ, ctx);
}
}
assert(0 && "unreachable");
}
static Value *emit_untyped_intrinsic(intrinsic f, Value **argvalues, size_t nargs,
jl_codectx_t *ctx, jl_datatype_t **newtyp, jl_value_t *xtyp)
{
Value *x = nargs > 0 ? argvalues[0] : NULL;
Value *y = nargs > 1 ? argvalues[1] : NULL;
Value *z = nargs > 2 ? argvalues[2] : NULL;
Type *t = x->getType();
switch (f) {
case neg_int:
#if JL_LLVM_VERSION >= 30700
return builder.CreateNeg(x);
#else
return builder.CreateSub(ConstantInt::get(t, 0), x);
#endif
case add_int: return builder.CreateAdd(x, y);
case sub_int: return builder.CreateSub(x, y);
case mul_int: return builder.CreateMul(x, y);
case sdiv_int: return builder.CreateSDiv(x, y);
case udiv_int: return builder.CreateUDiv(x, y);
case srem_int: return builder.CreateSRem(x, y);
case urem_int: return builder.CreateURem(x, y);
// 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
#if JL_LLVM_VERSION >= 30500
case neg_float: return math_builder(ctx)().CreateFSub(ConstantFP::get(t, -0.0), x);
case neg_float_fast: return math_builder(ctx, true)().CreateFNeg(x);
#else
case neg_float:
return math_builder(ctx)().CreateFMul(ConstantFP::get(t, -1.0), x);
case neg_float_fast:
return math_builder(ctx, true)().CreateFMul(ConstantFP::get(t, -1.0), x);
#endif
case add_float: return math_builder(ctx)().CreateFAdd(x, y);
case sub_float: return math_builder(ctx)().CreateFSub(x, y);
case mul_float: return math_builder(ctx)().CreateFMul(x, y);
case div_float: return math_builder(ctx)().CreateFDiv(x, y);
case rem_float: return math_builder(ctx)().CreateFRem(x, y);
case add_float_fast: return math_builder(ctx, true)().CreateFAdd(x, y);
case sub_float_fast: return math_builder(ctx, true)().CreateFSub(x, y);
case mul_float_fast: return math_builder(ctx, true)().CreateFMul(x, y);
case div_float_fast: return math_builder(ctx, true)().CreateFDiv(x, y);
case rem_float_fast: return math_builder(ctx, true)().CreateFRem(x, y);
case fma_float: {
assert(y->getType() == x->getType());
assert(z->getType() == y->getType());
Value *fmaintr = Intrinsic::getDeclaration(jl_Module, Intrinsic::fma, makeArrayRef(t));
#if JL_LLVM_VERSION >= 30700
return builder.CreateCall(fmaintr, {x, y, z});
#else
return builder.CreateCall3(fmaintr, x, y, z);
#endif
}
case muladd_float: {
#if JL_LLVM_VERSION >= 30400
assert(y->getType() == x->getType());
assert(z->getType() == y->getType());
Value *muladdintr = Intrinsic::getDeclaration(jl_Module, Intrinsic::fmuladd, makeArrayRef(t));
#if JL_LLVM_VERSION >= 30700
return builder.CreateCall(muladdintr, {x, y, z});
#else
return builder.CreateCall3(muladdintr, x, y, z);
#endif
#else
return math_builder(ctx, true)().CreateFAdd(builder.CreateFMul(x, y), z);
#endif
}
case checked_sadd_int:
case checked_uadd_int:
case checked_ssub_int:
case checked_usub_int:
case checked_smul_int:
case checked_umul_int: {
assert(x->getType() == y->getType());
Intrinsic::ID intr_id =
(f == checked_sadd_int ?
Intrinsic::sadd_with_overflow :
(f == checked_uadd_int ?
Intrinsic::uadd_with_overflow :
(f == checked_ssub_int ?
Intrinsic::ssub_with_overflow :
(f == checked_usub_int ?
Intrinsic::usub_with_overflow :
(f == checked_smul_int ?
Intrinsic::smul_with_overflow :
Intrinsic::umul_with_overflow)))));
Value *intr = Intrinsic::getDeclaration(jl_Module, intr_id, makeArrayRef(t));
#if JL_LLVM_VERSION >= 30700
Value *res = builder.CreateCall(intr, {x, y});
#else
Value *res = builder.CreateCall2(intr, x, y);
#endif
Value *val = builder.CreateExtractValue(res, ArrayRef<unsigned>(0));
Value *obit = builder.CreateExtractValue(res, ArrayRef<unsigned>(1));
Value *obyte = builder.CreateZExt(obit, T_int8);