-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
intrinsics.cpp
1716 lines (1597 loc) · 71.8 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 <array>
#include <bitset>
#include <string>
#include "ccall.cpp"
//Mark our stats as being from intrinsics irgen
#undef DEBUG_TYPE
#define DEBUG_TYPE "julia_irgen_intrinsics"
STATISTIC(EmittedConstants, "Number of constants emitted");
STATISTIC(EmittedCoercedUnboxes, "Number of unbox coercions emitted");
STATISTIC(EmittedUnboxes, "Number of unboxes emitted");
STATISTIC(EmittedRuntimeCalls, "Number of runtime intrinsic calls emitted");
STATISTIC(EmittedIntrinsics, "Number of intrinsic calls emitted");
STATISTIC(Emitted_pointerref, "Number of pointerref calls emitted");
STATISTIC(Emitted_pointerset, "Number of pointerset calls emitted");
STATISTIC(Emitted_pointerarith, "Number of pointer arithmetic calls emitted");
STATISTIC(Emitted_atomic_fence, "Number of atomic_fence calls emitted");
STATISTIC(Emitted_atomic_pointerref, "Number of atomic_pointerref calls emitted");
STATISTIC(Emitted_atomic_pointerop, "Number of atomic_pointerop calls emitted");
STATISTIC(Emitted_bitcast, "Number of bitcast calls emitted");
STATISTIC(Emitted_trunc_int, "Number of trunc_int calls emitted");
STATISTIC(Emitted_sext_int, "Number of sext_int calls emitted");
STATISTIC(Emitted_zext_int, "Number of zext_int calls emitted");
STATISTIC(Emitted_uitofp, "Number of uitofp calls emitted");
STATISTIC(Emitted_sitofp, "Number of sitofp calls emitted");
STATISTIC(Emitted_fptoui, "Number of fptoui calls emitted");
STATISTIC(Emitted_fptosi, "Number of fptosi calls emitted");
STATISTIC(Emitted_fptrunc, "Number of fptrunc calls emitted");
STATISTIC(Emitted_fpext, "Number of fpext calls emitted");
STATISTIC(Emitted_not_int, "Number of not_int calls emitted");
STATISTIC(Emitted_have_fma, "Number of have_fma calls emitted");
STATISTIC(EmittedUntypedIntrinsics, "Number of untyped intrinsics emitted");
using namespace JL_I;
FunctionType *get_intr_args1(LLVMContext &C) { return FunctionType::get(JuliaType::get_prjlvalue_ty(C), {JuliaType::get_prjlvalue_ty(C)}, false); }
FunctionType *get_intr_args2(LLVMContext &C) { return FunctionType::get(JuliaType::get_prjlvalue_ty(C), {JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C)}, false); }
FunctionType *get_intr_args3(LLVMContext &C) { return FunctionType::get(JuliaType::get_prjlvalue_ty(C), {JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C)}, false); }
FunctionType *get_intr_args4(LLVMContext &C) { return FunctionType::get(JuliaType::get_prjlvalue_ty(C), {JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C)}, false); }
FunctionType *get_intr_args5(LLVMContext &C) { return FunctionType::get(JuliaType::get_prjlvalue_ty(C), {JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C), JuliaType::get_prjlvalue_ty(C)}, false); }
const auto &runtime_func() {
static struct runtime_funcs_t {
std::array<JuliaFunction<> *, num_intrinsics> runtime_func;
runtime_funcs_t() :
runtime_func{
#define ADD_I(name, nargs) new JuliaFunction<>{XSTR(jl_##name), get_intr_args##nargs, nullptr},
#define ADD_HIDDEN ADD_I
#define ALIAS(alias, base) nullptr,
INTRINSICS
#undef ADD_I
#undef ADD_HIDDEN
#undef ALIAS
}
{
#define ADD_I(name, nargs)
#define ADD_HIDDEN(name, nargs)
#define ALIAS(alias, base) runtime_func[alias] = runtime_func[base];
INTRINSICS
#undef ADD_I
#undef ADD_HIDDEN
#undef ALIAS
}
} runtime_funcs;
return runtime_funcs.runtime_func;
}
const auto &float_func() {
static struct float_funcs_t {
std::bitset<num_intrinsics> float_func;
float_funcs_t() {
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[add_float_fast] = true;
float_func[sub_float_fast] = true;
float_func[mul_float_fast] = true;
float_func[div_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[abs_float] = true;
float_func[copysign_float] = true;
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;
}
} float_funcs;
return float_funcs.float_func;
}
extern "C" JL_DLLEXPORT_CODEGEN
uint32_t jl_get_LLVM_VERSION_impl(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());
auto &ctxt = t->getContext();
if (nb == 64)
return getDoubleTy(ctxt);
if (nb == 32)
return getFloatTy(ctxt);
if (nb == 16)
return getHalfTy(ctxt);
if (nb == 128)
return getFP128Ty(ctxt);
return NULL;
}
// convert an llvm type to same-size int type
static Type *INTT(Type *t, const DataLayout &DL)
{
auto &ctxt = t->getContext();
if (t->isIntegerTy())
return t;
if (t->isPointerTy())
return DL.getIntPtrType(t);
if (t == getDoubleTy(ctxt))
return getInt64Ty(ctxt);
if (t == getFloatTy(ctxt))
return getInt32Ty(ctxt);
if (t == getHalfTy(ctxt) || t == getBFloatTy(ctxt))
return getInt16Ty(ctxt);
unsigned nb = t->getPrimitiveSizeInBits();
assert(t != getVoidTy(ctxt) && nb > 0);
return IntegerType::get(ctxt, nb);
}
static Value *uint_cnvt(jl_codectx_t &ctx, Type *to, Value *x)
{
return ctx.builder.CreateZExtOrTrunc(x, to);
}
static Constant *julia_const_to_llvm(jl_codectx_t &ctx, const void *ptr, jl_datatype_t *bt)
{
// assumes `jl_is_pointerfree(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_bool_type)
return ConstantInt::get(getInt8Ty(ctx.builder.getContext()), (*(const uint8_t*)ptr) ? 1 : 0);
Type *lt = julia_struct_to_llvm(ctx, (jl_value_t*)bt, NULL);
if (jl_is_vecelement_type((jl_value_t*)bt) && !jl_is_uniontype(jl_tparam0(bt)))
bt = (jl_datatype_t*)jl_tparam0(bt);
if (type_is_ghost(lt))
return UndefValue::get(lt);
if (lt->isFloatTy()) {
uint32_t data32 = *(const uint32_t*)ptr;
return ConstantFP::get(ctx.builder.getContext(),
APFloat(lt->getFltSemantics(), APInt(32, data32)));
}
if (lt->isDoubleTy()) {
uint64_t data64 = *(const uint64_t*)ptr;
return ConstantFP::get(ctx.builder.getContext(),
APFloat(lt->getFltSemantics(), APInt(64, data64)));
}
if (lt->isFloatingPointTy() || lt->isIntegerTy() || lt->isPointerTy()) {
int nb = jl_datatype_size(bt);
APInt val(8 * nb, 0);
void *bits = const_cast<uint64_t*>(val.getRawData());
assert(sys::IsLittleEndianHost);
memcpy(bits, ptr, nb);
if (lt->isFloatingPointTy()) {
return ConstantFP::get(ctx.builder.getContext(),
APFloat(lt->getFltSemantics(), val));
}
if (lt->isPointerTy()) {
Type *Ty = IntegerType::get(ctx.builder.getContext(), 8 * nb);
Constant *addr = ConstantInt::get(Ty, val);
return ConstantExpr::getIntToPtr(addr, lt);
}
assert(cast<IntegerType>(lt)->getBitWidth() == 8u * nb);
return ConstantInt::get(lt, val);
}
size_t nf = jl_datatype_nfields(bt);
SmallVector<Constant*, 0> fields(0);
for (size_t i = 0; i < nf; i++) {
size_t offs = jl_field_offset(bt, i);
jl_value_t *ft = jl_field_type(bt, i);
Type *lft = julia_type_to_llvm(ctx, ft);
if (type_is_ghost(lft))
continue;
assert(!jl_field_isptr(bt, i));
unsigned llvm_idx = isa<StructType>(lt) ? convert_struct_offset(jl_Module->getDataLayout(), lt, offs) : i;
while (fields.size() < llvm_idx)
fields.push_back(
UndefValue::get(GetElementPtrInst::getTypeAtIndex(lt, fields.size())));
const uint8_t *ov = (const uint8_t*)ptr + offs;
if (jl_is_uniontype(ft)) {
// compute the same type layout as julia_struct_to_llvm
size_t fsz = 0, al = 0;
(void)jl_islayout_inline(ft, &fsz, &al); // compute al
fsz = jl_field_size(bt, i); // get LLT_ALIGN(fsz+1,al)
uint8_t sel = ((const uint8_t*)ptr)[offs + fsz - 1];
jl_value_t *active_ty = jl_nth_union_component(ft, sel);
size_t active_sz = jl_datatype_size(active_ty);
Type *AlignmentType = IntegerType::get(ctx.builder.getContext(), 8 * al);
unsigned NumATy = (fsz - 1) / al;
unsigned remainder = (fsz - 1) % al;
while (NumATy--) {
Constant *fld;
if (active_sz > 0) {
APInt Elem(8 * al, 0);
void *bits = const_cast<uint64_t*>(Elem.getRawData());
if (active_sz > al) {
memcpy(bits, ov, al);
active_sz -= al;
}
else {
memcpy(bits, ov, active_sz);
active_sz = 0;
}
fld = ConstantInt::get(AlignmentType, Elem);
}
else {
fld = UndefValue::get(AlignmentType);
}
ov += al;
fields.push_back(fld);
}
while (remainder--) {
Constant *fld;
if (active_sz > 0) {
uint8_t byte = *ov;
APInt Elem(8, byte);
active_sz -= 1;
fld = ConstantInt::get(getInt8Ty(ctx.builder.getContext()), Elem);
}
else {
fld = UndefValue::get(getInt8Ty(ctx.builder.getContext()));
}
ov += 1;
fields.push_back(fld);
}
fields.push_back(ConstantInt::get(getInt8Ty(ctx.builder.getContext()), sel));
}
else {
Constant *val = julia_const_to_llvm(ctx, ov, (jl_datatype_t*)ft);
fields.push_back(val);
}
}
if (lt->isVectorTy())
return ConstantVector::get(fields);
if (StructType *st = dyn_cast<StructType>(lt))
return ConstantStruct::get(st, fields);
if (ArrayType *at = dyn_cast<ArrayType>(lt))
return ConstantArray::get(at, fields);
assert(false && "Unknown LLVM type");
jl_unreachable();
}
static Constant *julia_const_to_llvm(jl_codectx_t &ctx, jl_value_t *e)
{
if (e == jl_true)
return ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 1);
if (e == jl_false)
return ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 0);
jl_value_t *bt = jl_typeof(e);
if (!jl_is_pointerfree(bt))
return NULL;
return julia_const_to_llvm(ctx, e, (jl_datatype_t*)bt);
}
static Constant *undef_value_for_type(Type *T) {
auto tracked = CountTrackedPointers(T);
Constant *undef;
if (tracked.count)
// make sure gc pointers (including ptr_phi of union-split) are initialized to NULL
undef = Constant::getNullValue(T);
else
undef = UndefValue::get(T);
return undef;
}
// rebuild a struct type with any i1 Bool (e.g. the llvmcall type) widened to i8 (the native size for memcpy)
static Type *zext_struct_type(Type *T)
{
if (auto *AT = dyn_cast<ArrayType>(T)) {
return ArrayType::get(AT->getElementType(), AT->getNumElements());
}
else if (auto *ST = dyn_cast<StructType>(T)) {
SmallVector<Type*> Elements(ST->element_begin(), ST->element_end());
for (size_t i = 0; i < Elements.size(); i++) {
Elements[i] = zext_struct_type(Elements[i]);
}
return StructType::get(ST->getContext(), Elements, ST->isPacked());
}
else if (auto *VT = dyn_cast<VectorType>(T)) {
return VectorType::get(zext_struct_type(VT->getElementType()), VT);
}
else if (auto *IT = dyn_cast<IntegerType>(T)) {
unsigned BitWidth = IT->getBitWidth();
if (alignTo(BitWidth, 8) != BitWidth)
return IntegerType::get(IT->getContext(), alignTo(BitWidth, 8));
}
return T;
}
// rebuild a struct with any i1 Bool (e.g. the llvmcall type) widened to i8 (the native size for memcpy)
static Value *zext_struct_helper(jl_codectx_t &ctx, Value *V, Type *T2)
{
Type *T = V->getType();
if (T == T2)
return V;
if (auto *AT = dyn_cast<ArrayType>(T2)) {
Value *V2 = undef_value_for_type(AT);
for (size_t i = 0; i < AT->getNumElements(); i++) {
Value *E = zext_struct_helper(ctx, ctx.builder.CreateExtractValue(V, i), AT->getElementType());
V2 = ctx.builder.CreateInsertValue(V2, E, i);
}
return V2;
}
else if (auto *ST = dyn_cast<StructType>(T2)) {
Value *V2 = undef_value_for_type(ST);
for (size_t i = 0; i < ST->getNumElements(); i++) {
Value *E = zext_struct_helper(ctx, ctx.builder.CreateExtractValue(V, i), ST->getElementType(i));
V2 = ctx.builder.CreateInsertValue(V2, E, i);
}
return V2;
}
else if (T2->isIntegerTy() || T2->isVectorTy()) {
return ctx.builder.CreateZExt(V, T2);
}
return V;
}
static Value *zext_struct(jl_codectx_t &ctx, Value *V)
{
return zext_struct_helper(ctx, V, zext_struct_type(V->getType()));
}
static Value *emit_unboxed_coercion(jl_codectx_t &ctx, Type *to, Value *unboxed)
{
if (unboxed->getType() == to)
return unboxed;
if (CastInst::castIsValid(Instruction::Trunc, unboxed, to))
return ctx.builder.CreateTrunc(unboxed, to);
unboxed = zext_struct(ctx, unboxed);
Type *ty = unboxed->getType();
if (ty == to)
return unboxed;
bool frompointer = ty->isPointerTy();
bool topointer = to->isPointerTy();
const DataLayout &DL = jl_Module->getDataLayout();
if (ty->isVoidTy() || DL.getTypeSizeInBits(ty) != DL.getTypeSizeInBits(to)) {
// this can happen in dead code
CreateTrap(ctx.builder);
return UndefValue::get(to);
}
else if (!ty->isIntOrPtrTy() && !ty->isFloatingPointTy()) {
assert(DL.getTypeSizeInBits(ty) == DL.getTypeSizeInBits(to));
Align align = std::max(DL.getPrefTypeAlign(ty), DL.getPrefTypeAlign(to));
AllocaInst *cast = emit_static_alloca(ctx, ty, align);
setName(ctx.emission_context, cast, "coercion");
ctx.builder.CreateAlignedStore(unboxed, cast, align);
unboxed = ctx.builder.CreateAlignedLoad(to, cast, align);
}
else if (frompointer) {
Type *INTT_to = INTT(to, DL);
unboxed = ctx.builder.CreatePtrToInt(unboxed, INTT_to);
setName(ctx.emission_context, unboxed, "coercion");
if (INTT_to != to) //TODO when is this true?
unboxed = ctx.builder.CreateBitCast(unboxed, to);
}
else if (topointer) {
Type *INTT_to = INTT(to, DL);
if (to != INTT_to) //TODO when is this true?
unboxed = ctx.builder.CreateBitCast(unboxed, INTT_to);
unboxed = emit_inttoptr(ctx, unboxed, to);
setName(ctx.emission_context, unboxed, "coercion");
}
else {
unboxed = ctx.builder.CreateBitCast(unboxed, to);
}
return unboxed;
}
// emit code to unpack a raw value from a box into registers
static Value *emit_unbox(jl_codectx_t &ctx, Type *to, const jl_cgval_t &x, jl_value_t *jt)
{
assert(to != getVoidTy(ctx.builder.getContext()));
// 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 nullptr;
}
CreateTrap(ctx.builder);
return UndefValue::get(to); // type mismatch error
}
Constant *c = x.constant ? julia_const_to_llvm(ctx, x.constant) : nullptr;
if ((x.inline_roots.empty() && !x.ispointer()) || c != nullptr) { // already unboxed, but sometimes need conversion
Value *unboxed = c ? c : x.V;
return emit_unboxed_coercion(ctx, to, unboxed);
}
// bools stored as int8, so an extra Trunc is needed to get an int1
Value *p = x.constant ? literal_pointer_val(ctx, x.constant) : x.V;
if (jt == (jl_value_t*)jl_bool_type || to->isIntegerTy(1)) {
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, x.tbaa);
Instruction *unbox_load = ai.decorateInst(ctx.builder.CreateLoad(getInt8Ty(ctx.builder.getContext()), p));
setName(ctx.emission_context, unbox_load, p->getName() + ".unbox");
if (jt == (jl_value_t*)jl_bool_type)
unbox_load->setMetadata(LLVMContext::MD_range, MDNode::get(ctx.builder.getContext(), {
ConstantAsMetadata::get(ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 0)),
ConstantAsMetadata::get(ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 2)) }));
Value *unboxed;
if (to->isIntegerTy(1))
unboxed = ctx.builder.CreateTrunc(unbox_load, to);
else
unboxed = unbox_load; // `to` must be Int8Ty
return unboxed;
}
unsigned alignment = julia_alignment(jt);
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, x.tbaa);
if (!x.inline_roots.empty()) {
assert(x.typ == jt);
AllocaInst *combined = emit_static_alloca(ctx, to, Align(alignment));
auto combined_ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack);
recombine_value(ctx, x, combined, combined_ai, Align(alignment), false);
p = combined;
ai = combined_ai;
}
Instruction *load = ctx.builder.CreateAlignedLoad(to, p, Align(alignment));
setName(ctx.emission_context, load, p->getName() + ".unbox");
return ai.decorateInst(load);
}
// emit code to store a raw value into a destination
static void emit_unbox_store(jl_codectx_t &ctx, const jl_cgval_t &x, Value *dest, MDNode *tbaa_dest, Align alignment, bool isVolatile)
{
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.
return;
}
auto dest_ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa_dest);
if (!x.inline_roots.empty()) {
recombine_value(ctx, x, dest, dest_ai, alignment, isVolatile);
return;
}
if (!x.ispointer()) { // already unboxed, but sometimes need conversion (e.g. f32 -> i32)
assert(x.V);
Value *unboxed = zext_struct(ctx, x.V);
StoreInst *store = ctx.builder.CreateAlignedStore(unboxed, dest, alignment);
store->setVolatile(isVolatile);
dest_ai.decorateInst(store);
return;
}
Value *src = data_pointer(ctx, x);
auto src_ai = jl_aliasinfo_t::fromTBAA(ctx, x.tbaa);
emit_memcpy(ctx, dest, dest_ai, src, src_ai, jl_datatype_size(x.typ), Align(alignment), Align(julia_alignment(x.typ)), isVolatile);
}
static jl_datatype_t *staticeval_bitstype(const jl_cgval_t &targ)
{
// evaluate an argument at compile time to determine what type it is
jl_value_t *unw = jl_unwrap_unionall(targ.typ);
if (jl_is_type_type(unw)) {
jl_value_t *bt = jl_tparam0(unw);
if (jl_is_primitivetype(bt))
return (jl_datatype_t*)bt;
}
return NULL;
}
static jl_cgval_t emit_runtime_call(jl_codectx_t &ctx, JL_I::intrinsic f, ArrayRef<jl_cgval_t> argv, size_t nargs)
{
Function *func = prepare_call(runtime_func()[f]);
SmallVector<Value *, 0> argvalues(nargs);
for (size_t i = 0; i < nargs; ++i) {
argvalues[i] = boxed(ctx, argv[i]);
}
Value *r = ctx.builder.CreateCall(func, argvalues);
return mark_julia_type(ctx, r, true, (jl_value_t*)jl_any_type);
}
// 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(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
// Give the arguments names //
const jl_cgval_t &bt_value = argv[0];
const jl_cgval_t &v = argv[1];
jl_datatype_t *bt = staticeval_bitstype(bt_value);
// it's easier to throw a good error from C than llvm
if (!bt)
return emit_runtime_call(ctx, bitcast, argv, 2);
Type *llvmt = bitstype_to_llvm((jl_value_t*)bt, ctx.builder.getContext(), true);
uint32_t nb = jl_datatype_size(bt);
Value *bt_value_rt = NULL;
if (!jl_is_concrete_type((jl_value_t*)bt)) {
bt_value_rt = boxed(ctx, bt_value);
emit_concretecheck(ctx, bt_value_rt, "bitcast: target type not a leaf primitive type");
}
// Examine the second argument //
bool isboxed;
Type *vxt = julia_type_to_llvm(ctx, v.typ, &isboxed);
if (!jl_is_primitivetype(v.typ) || jl_datatype_size(v.typ) != nb) {
Value *typ = emit_typeof(ctx, v, false, false);
if (!jl_is_primitivetype(v.typ)) {
if (jl_is_datatype(v.typ) && !jl_is_abstracttype(v.typ)) {
emit_error(ctx, "bitcast: value not a primitive type");
return jl_cgval_t();
}
else {
Value *isprimitive = emit_datatype_isprimitivetype(ctx, typ);
error_unless(ctx, isprimitive, "bitcast: value not a primitive type");
}
}
if (jl_is_datatype(v.typ) && !jl_is_abstracttype(v.typ)) {
emit_error(ctx, "bitcast: argument size does not match size of target type");
return jl_cgval_t();
}
else {
Value *size = emit_datatype_size(ctx, typ);
auto sizecheck = ctx.builder.CreateICmpEQ(size, ConstantInt::get(size->getType(), nb));
setName(ctx.emission_context, sizecheck, "sizecheck");
error_unless(ctx,
sizecheck,
"bitcast: argument size does not match size of target type");
}
}
assert(!v.isghost);
Value *vx = NULL;
if (!v.ispointer())
vx = v.V;
else if (v.constant)
vx = julia_const_to_llvm(ctx, 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;
auto storage_type = vxt->isIntegerTy(1) ? getInt8Ty(ctx.builder.getContext()) : vxt;
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, v.tbaa);
vx = ai.decorateInst(ctx.builder.CreateLoad(
storage_type,
data_pointer(ctx, v)));
setName(ctx.emission_context, vx, "bitcast");
}
vxt = vx->getType();
if (vxt != llvmt) {
if (llvmt->isIntegerTy(1)) {
vx = ctx.builder.CreateTrunc(vx, llvmt);
} else if (vxt->isIntegerTy(1) && llvmt->isIntegerTy(8)) {
vx = ctx.builder.CreateZExt(vx, llvmt);
} else if (vxt->isPointerTy() && !llvmt->isPointerTy()) {
vx = ctx.builder.CreatePtrToInt(vx, llvmt);
if (isa<Instruction>(vx) && !vx->hasName())
// CreatePtrToInt may undo an IntToPtr
setName(ctx.emission_context, vx, "bitcast_coercion");
} else if (!vxt->isPointerTy() && llvmt->isPointerTy()) {
vx = emit_inttoptr(ctx, vx, llvmt);
if (isa<Instruction>(vx) && !vx->hasName())
// emit_inttoptr may undo an PtrToInt
setName(ctx.emission_context, vx, "bitcast_coercion");
} else if (vxt->isPointerTy() && llvmt->isPointerTy()) {
// emit_bitcast preserves the origin address space, which we can't have here
vx = ctx.builder.CreateAddrSpaceCast(vx, llvmt);
if (isa<Instruction>(vx) && !vx->hasName())
// cast may have been folded
setName(ctx.emission_context, vx, "bitcast_coercion");
} else {
vx = emit_bitcast(ctx, vx, llvmt);
if (isa<Instruction>(vx) && !vx->hasName())
// emit_bitcast may undo another bitcast
setName(ctx.emission_context, vx, "bitcast_coercion");
}
}
if (jl_is_concrete_type((jl_value_t*)bt)) {
return mark_julia_type(ctx, vx, false, bt);
}
else {
unsigned align = sizeof(void*); // Allocations are at least pointer aligned
Value *box = emit_allocobj(ctx, nb, bt_value_rt, true, align);
setName(ctx.emission_context, box, "bitcast_box");
init_bits_value(ctx, box, vx, ctx.tbaa().tbaa_immut);
return mark_julia_type(ctx, box, true, bt->name->wrapper);
}
}
static jl_cgval_t generic_cast(
jl_codectx_t &ctx,
intrinsic f, Instruction::CastOps Op,
ArrayRef<jl_cgval_t> argv, bool toint, bool fromint)
{
auto &TT = ctx.emission_context.TargetTriple;
auto &DL = ctx.emission_context.DL;
const jl_cgval_t &targ = argv[0];
const jl_cgval_t &v = argv[1];
jl_datatype_t *jlto = staticeval_bitstype(targ);
if (!jlto || !jl_is_primitivetype(v.typ))
return emit_runtime_call(ctx, f, argv, 2);
uint32_t nb = jl_datatype_size(jlto);
Type *to = bitstype_to_llvm((jl_value_t*)jlto, ctx.builder.getContext(), true);
Type *vt = bitstype_to_llvm(v.typ, ctx.builder.getContext(), true);
if (toint)
to = INTT(to, DL);
else
to = FLOATT(to);
if (fromint)
vt = INTT(vt, DL);
else
vt = FLOATT(vt);
if (!to || !vt)
return emit_runtime_call(ctx, f, argv, 2);
Value *from = emit_unbox(ctx, vt, v, v.typ);
if (!CastInst::castIsValid(Op, from, to))
return emit_runtime_call(ctx, f, argv, 2);
if (Op == Instruction::FPExt) {
if (jl_floattemp_var_needed(TT)) {
// 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.
Align align(julia_alignment((jl_value_t*)jlto));
Value *jlfloattemp_var = emit_static_alloca(ctx, from->getType(), align);
setName(ctx.emission_context, jlfloattemp_var, "rounding_slot");
ctx.builder.CreateAlignedStore(from, jlfloattemp_var, align);
from = ctx.builder.CreateAlignedLoad(from->getType(), jlfloattemp_var, align, /*force this to load from the stack*/true);
setName(ctx.emission_context, from, "rounded");
}
}
Value *ans = ctx.builder.CreateCast(Op, from, to);
if (f == fptosi || f == fptoui)
ans = ctx.builder.CreateFreeze(ans);
if (jl_is_concrete_type((jl_value_t*)jlto)) {
return mark_julia_type(ctx, ans, false, jlto);
}
else {
Value *targ_rt = boxed(ctx, targ);
emit_concretecheck(ctx, targ_rt, std::string(jl_intrinsic_name(f)) + ": target type not a leaf primitive type");
unsigned align = sizeof(void*); // Allocations are at least pointer aligned
Value *box = emit_allocobj(ctx, nb, targ_rt, true, align);
setName(ctx.emission_context, box, "cast_box");
init_bits_value(ctx, box, ans, ctx.tbaa().tbaa_immut);
return mark_julia_type(ctx, box, true, jlto->name->wrapper);
}
}
static jl_cgval_t emit_runtime_pointerref(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
return emit_runtime_call(ctx, pointerref, argv, 3);
}
static jl_cgval_t emit_pointerref(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
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(ctx, argv);
unsigned align_nb = jl_unbox_long(align.constant);
if (i.typ != (jl_value_t*)jl_long_type)
return emit_runtime_pointerref(ctx, argv);
jl_value_t *aty = e.typ;
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerref(ctx, argv);
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerref(ctx, argv);
if (!is_valid_intrinsic_elptr(ety)) {
emit_error(ctx, "pointerref: invalid pointer type");
return jl_cgval_t();
}
Value *idx = emit_unbox(ctx, ctx.types().T_size, i, (jl_value_t*)jl_long_type);
Value *im1 = ctx.builder.CreateSub(idx, ConstantInt::get(ctx.types().T_size, 1));
setName(ctx.emission_context, im1, "pointerref_idx");
if (ety == (jl_value_t*)jl_any_type) {
Value *thePtr = emit_unbox(ctx, ctx.types().T_pprjlvalue, e, e.typ);
if (isa<Instruction>(thePtr) && !thePtr->hasName())
setName(ctx.emission_context, thePtr, "unbox_any_ptr");
LoadInst *load = ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, ctx.builder.CreateInBoundsGEP(ctx.types().T_prjlvalue, thePtr, im1), Align(align_nb));
setName(ctx.emission_context, load, "any_unbox");
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_data);
ai.decorateInst(load);
return mark_julia_type(ctx, load, true, ety);
}
else if (!deserves_stack(ety)) {
assert(jl_is_datatype(ety));
uint64_t size = jl_datatype_size(ety);
Value *strct = emit_allocobj(ctx, (jl_datatype_t*)ety, true);
setName(ctx.emission_context, strct, "pointerref_box");
im1 = ctx.builder.CreateMul(im1, ConstantInt::get(ctx.types().T_size,
LLT_ALIGN(size, jl_datatype_align(ety))));
setName(ctx.emission_context, im1, "pointerref_offset");
Value *thePtr = emit_unbox(ctx, getPointerTy(ctx.builder.getContext()), e, e.typ);
thePtr = emit_ptrgep(ctx, thePtr, im1);
setName(ctx.emission_context, thePtr, "pointerref_src");
MDNode *tbaa = best_tbaa(ctx.tbaa(), ety);
emit_memcpy(ctx, strct, jl_aliasinfo_t::fromTBAA(ctx, tbaa), thePtr, jl_aliasinfo_t::fromTBAA(ctx, nullptr), size, Align(sizeof(jl_value_t*)), Align(align_nb));
return mark_julia_type(ctx, strct, true, ety);
}
else {
bool isboxed;
Type *ptrty = julia_type_to_llvm(ctx, ety, &isboxed);
assert(!isboxed);
if (!type_is_ghost(ptrty)) {
Value *thePtr = emit_unbox(ctx, ptrty->getPointerTo(), e, e.typ);
thePtr = ctx.builder.CreateInBoundsGEP(ptrty, thePtr, im1);
auto load = typed_load(ctx, thePtr, nullptr, ety, ctx.tbaa().tbaa_data, nullptr, isboxed, AtomicOrdering::NotAtomic, false, align_nb);
setName(ctx.emission_context, load.V, "pointerref");
return load;
}
else {
return ghostValue(ctx, ety);
}
}
}
static jl_cgval_t emit_runtime_pointerset(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
return emit_runtime_call(ctx, pointerset, argv, 4);
}
// e[i] = x
static jl_cgval_t emit_pointerset(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
const jl_cgval_t &e = argv[0];
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(ctx, argv);
unsigned align_nb = jl_unbox_long(align.constant);
if (i.typ != (jl_value_t*)jl_long_type)
return emit_runtime_pointerset(ctx, argv);
jl_value_t *aty = e.typ;
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerset(ctx, argv);
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerset(ctx, argv);
if (align.constant == NULL || !jl_is_long(align.constant))
return emit_runtime_pointerset(ctx, argv);
if (!is_valid_intrinsic_elptr(ety)) {
emit_error(ctx, "pointerset: invalid pointer type");
return jl_cgval_t();
}
emit_typecheck(ctx, x, ety, "pointerset");
x = update_julia_type(ctx, x, ety);
if (x.typ == jl_bottom_type)
return jl_cgval_t();
Value *idx = emit_unbox(ctx, ctx.types().T_size, i, (jl_value_t*)jl_long_type);
Value *im1 = ctx.builder.CreateSub(idx, ConstantInt::get(ctx.types().T_size, 1));
setName(ctx.emission_context, im1, "pointerset_idx");
Value *thePtr = emit_unbox(ctx, getPointerTy(ctx.builder.getContext()), e, e.typ);
if (ety == (jl_value_t*)jl_any_type) {
// unsafe_store to Ptr{Any} is allowed to implicitly drop GC roots.
auto gep = ctx.builder.CreateInBoundsGEP(ctx.types().T_size, thePtr, im1);
setName(ctx.emission_context, gep, "pointerset_ptr");
auto val = ctx.builder.CreatePtrToInt(emit_pointer_from_objref(ctx, boxed(ctx, x)), ctx.types().T_size);
setName(ctx.emission_context, val, "pointerset_val");
Instruction *store = ctx.builder.CreateAlignedStore(val, gep, Align(align_nb));
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_data);
ai.decorateInst(store);
}
else if (!x.inline_roots.empty()) {
recombine_value(ctx, e, thePtr, jl_aliasinfo_t(), Align(align_nb), false);
}
else if (x.ispointer()) {
uint64_t size = jl_datatype_size(ety);
im1 = ctx.builder.CreateMul(im1, ConstantInt::get(ctx.types().T_size,
LLT_ALIGN(size, jl_datatype_align(ety))));
setName(ctx.emission_context, im1, "pointerset_offset");
auto gep = emit_ptrgep(ctx, thePtr, im1);
setName(ctx.emission_context, gep, "pointerset_ptr");
emit_memcpy(ctx, gep, jl_aliasinfo_t::fromTBAA(ctx, nullptr), x, size, Align(align_nb), Align(julia_alignment(ety)));
}
else {
bool isboxed;
Type *ptrty = julia_type_to_llvm(ctx, ety, &isboxed);
assert(!isboxed);
if (!type_is_ghost(ptrty)) {
thePtr = ctx.builder.CreateInBoundsGEP(ptrty, thePtr, im1);
typed_store(ctx, thePtr, x, jl_cgval_t(), ety, ctx.tbaa().tbaa_data, nullptr, nullptr, isboxed,
AtomicOrdering::NotAtomic, AtomicOrdering::NotAtomic, align_nb, nullptr, true, false, false, false, false, false, nullptr, "atomic_pointerset", nullptr, nullptr);
}
}
return e;
}
// ptr + offset
// ptr - offset
static jl_cgval_t emit_pointerarith(jl_codectx_t &ctx, intrinsic f,
ArrayRef<jl_cgval_t> argv)
{
jl_value_t *ptrtyp = argv[0].typ;
jl_value_t *offtyp = argv[1].typ;
if (!jl_is_cpointer_type(ptrtyp) || offtyp != (jl_value_t *)jl_ulong_type)
return emit_runtime_call(ctx, f, argv, argv.size());
assert(f == add_ptr || f == sub_ptr);
Value *ptr = emit_unbox(ctx, ctx.types().T_ptr, argv[0], ptrtyp);
Value *off = emit_unbox(ctx, ctx.types().T_size, argv[1], offtyp);
if (f == sub_ptr)
off = ctx.builder.CreateNeg(off);
Value *ans = ctx.builder.CreateGEP(getInt8Ty(ctx.builder.getContext()), ptr, off);
if (jl_is_concrete_type(ptrtyp)) {
return mark_julia_type(ctx, ans, false, ptrtyp);
}
else {
Value *box = emit_allocobj(ctx, (jl_datatype_t *)ptrtyp, true);
setName(ctx.emission_context, box, "ptr_box");
init_bits_value(ctx, box, ans, ctx.tbaa().tbaa_immut);
return mark_julia_type(ctx, box, true, (jl_datatype_t *)ptrtyp);
}
}
static jl_cgval_t emit_atomicfence(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
const jl_cgval_t &ord = argv[0];
if (ord.constant && jl_is_symbol(ord.constant)) {
enum jl_memory_order order = jl_get_atomic_order((jl_sym_t*)ord.constant, true, true);
if (order == jl_memory_order_invalid) {
emit_atomic_error(ctx, "invalid atomic ordering");
return jl_cgval_t(); // unreachable
}
if (order > jl_memory_order_monotonic)
ctx.builder.CreateFence(get_llvm_atomic_order(order));
return ghostValue(ctx, jl_nothing_type);
}
return emit_runtime_call(ctx, atomic_fence, argv, 1);
}
static jl_cgval_t emit_atomic_pointerref(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
{
const jl_cgval_t &e = argv[0];
const jl_cgval_t &ord = argv[1];
jl_value_t *aty = e.typ;
if (!jl_is_cpointer_type(aty) || !ord.constant || !jl_is_symbol(ord.constant))
return emit_runtime_call(ctx, atomic_pointerref, argv, 2);
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_call(ctx, atomic_pointerref, argv, 2);
enum jl_memory_order order = jl_get_atomic_order((jl_sym_t*)ord.constant, true, false);
if (order == jl_memory_order_invalid) {
emit_atomic_error(ctx, "invalid atomic ordering");
return jl_cgval_t(); // unreachable
}
AtomicOrdering llvm_order = get_llvm_atomic_order(order);
if (ety == (jl_value_t*)jl_any_type) {
Value *thePtr = emit_unbox(ctx, ctx.types().T_pprjlvalue, e, e.typ);
LoadInst *load = ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, thePtr, Align(sizeof(jl_value_t*)));
setName(ctx.emission_context, load, "atomic_pointerref");
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_data);
ai.decorateInst(load);
load->setOrdering(llvm_order);
return mark_julia_type(ctx, load, true, ety);
}
if (!is_valid_intrinsic_elptr(ety)) {
emit_error(ctx, "atomic_pointerref: invalid pointer type");
return jl_cgval_t();
}
size_t nb = jl_datatype_size(ety);
if ((nb & (nb - 1)) != 0 || nb > MAX_POINTERATOMIC_SIZE) {
emit_error(ctx, "atomic_pointerref: invalid pointer for atomic operation");
return jl_cgval_t();
}
if (!deserves_stack(ety)) {
assert(jl_is_datatype(ety));
Value *strct = emit_allocobj(ctx, (jl_datatype_t*)ety, true);
setName(ctx.emission_context, strct, "atomic_pointerref_box");
Value *thePtr = emit_unbox(ctx, getPointerTy(ctx.builder.getContext()), e, e.typ);
Type *loadT = Type::getIntNTy(ctx.builder.getContext(), nb * 8);
MDNode *tbaa = best_tbaa(ctx.tbaa(), ety);
LoadInst *load = ctx.builder.CreateAlignedLoad(loadT, thePtr, Align(nb));
setName(ctx.emission_context, load, "atomic_pointerref");
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa);
ai.decorateInst(load);
load->setOrdering(llvm_order);
thePtr = strct;
StoreInst *store = ctx.builder.CreateAlignedStore(load, thePtr, Align(julia_alignment(ety)));
ai.decorateInst(store);
return mark_julia_type(ctx, strct, true, ety);
}
else {
bool isboxed;
Type *ptrty = julia_type_to_llvm(ctx, ety, &isboxed);
assert(!isboxed);
if (!type_is_ghost(ptrty)) {
Value *thePtr = emit_unbox(ctx, ptrty->getPointerTo(), e, e.typ);
auto load = typed_load(ctx, thePtr, nullptr, ety, ctx.tbaa().tbaa_data, nullptr, isboxed, llvm_order, false, nb);
setName(ctx.emission_context, load.V, "atomic_pointerref");
return load;
}
else {
if (order > jl_memory_order_monotonic)
ctx.builder.CreateFence(llvm_order);
return ghostValue(ctx, ety);
}
}
}
// e[i] = x (set)
// e[i] <= x (swap)
// e[i] y => x (replace)
// x(e[i], y) (modify)
static jl_cgval_t emit_atomic_pointerop(jl_codectx_t &ctx, intrinsic f, ArrayRef<jl_cgval_t> argv, int nargs, const jl_cgval_t *modifyop)
{
bool issetfield = f == atomic_pointerset;
bool isreplacefield = f == atomic_pointerreplace;
bool isswapfield = f == atomic_pointerswap;
bool ismodifyfield = f == atomic_pointermodify;
const jl_cgval_t undefval;
const jl_cgval_t &e = argv[0];
jl_cgval_t x = isreplacefield || ismodifyfield ? argv[2] : argv[1];
const jl_cgval_t &y = isreplacefield || ismodifyfield ? argv[1] : undefval;
const jl_cgval_t &ord = isreplacefield || ismodifyfield ? argv[3] : argv[2];
const jl_cgval_t &failord = isreplacefield ? argv[4] : undefval;