-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
tfuncs.jl
1893 lines (1812 loc) · 67.2 KB
/
tfuncs.jl
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
#############
# constants #
#############
@nospecialize
const _NAMEDTUPLE_NAME = NamedTuple.body.body.name
const INT_INF = typemax(Int) # integer infinity
const N_IFUNC = reinterpret(Int32, have_fma) + 1
const T_IFUNC = Vector{Tuple{Int, Int, Any}}(undef, N_IFUNC)
const T_IFUNC_COST = Vector{Int}(undef, N_IFUNC)
const T_FFUNC_KEY = Vector{Any}()
const T_FFUNC_VAL = Vector{Tuple{Int, Int, Any}}()
const T_FFUNC_COST = Vector{Int}()
function find_tfunc(@nospecialize f)
for i = 1:length(T_FFUNC_KEY)
if T_FFUNC_KEY[i] === f
return i
end
end
end
const DATATYPE_TYPES_FIELDINDEX = fieldindex(DataType, :types)
##########
# tfuncs #
##########
# Note that in most places in the compiler here, we'll assume that T=Type{S} is well-formed,
# and implies that `S <: Type`, not `1::Type{1}`, for example.
# This means that isType(T) implies we can call subtype on T.parameters[1], etc.
function add_tfunc(f::IntrinsicFunction, minarg::Int, maxarg::Int, @nospecialize(tfunc), cost::Int)
idx = reinterpret(Int32, f) + 1
T_IFUNC[idx] = (minarg, maxarg, tfunc)
T_IFUNC_COST[idx] = cost
end
# TODO: add @nospecialize on `f` and declare its type as `Builtin` when that's supported
function add_tfunc(f::Function, minarg::Int, maxarg::Int, @nospecialize(tfunc), cost::Int)
push!(T_FFUNC_KEY, f)
push!(T_FFUNC_VAL, (minarg, maxarg, tfunc))
push!(T_FFUNC_COST, cost)
end
add_tfunc(throw, 1, 1, (@nospecialize(x)) -> Bottom, 0)
# the inverse of typeof_tfunc
# returns (type, isexact, isconcrete, istype)
# if isexact is false, the actual runtime type may (will) be a subtype of t
# if isconcrete is true, the actual runtime type is definitely concrete (unreachable if not valid as a typeof)
# if istype is true, the actual runtime value will definitely be a type (e.g. this is false for Union{Type{Int}, Int})
function instanceof_tfunc(@nospecialize(t))
if isa(t, Const)
if isa(t.val, Type) && valid_as_lattice(t.val)
return t.val, true, isconcretetype(t.val), true
end
return Bottom, true, false, false # runtime throws on non-Type
end
t = widenconst(t)
if t === Bottom
return Bottom, true, true, false # runtime unreachable
elseif t === typeof(Bottom) || !hasintersect(t, Type)
return Bottom, true, false, false # literal Bottom or non-Type
elseif isType(t)
tp = t.parameters[1]
valid_as_lattice(tp) || return Bottom, true, false, false # runtime unreachable / throws on non-Type
return tp, !has_free_typevars(tp), isconcretetype(tp), true
elseif isa(t, UnionAll)
t′ = unwrap_unionall(t)
t′′, isexact, isconcrete, istype = instanceof_tfunc(t′)
tr = rewrap_unionall(t′′, t)
if t′′ isa DataType && t′′.name !== Tuple.name && !has_free_typevars(tr)
# a real instance must be within the declared bounds of the type,
# so we can intersect with the original wrapper.
tr = typeintersect(tr, t′′.name.wrapper)
isconcrete = !isabstracttype(t′′)
if tr === Union{}
# runtime unreachable (our inference Type{T} where S is
# uninhabited with any runtime T that exists)
isexact = true
end
end
return tr, isexact, isconcrete, istype
elseif isa(t, Union)
ta, isexact_a, isconcrete_a, istype_a = instanceof_tfunc(t.a)
tb, isexact_b, isconcrete_b, istype_b = instanceof_tfunc(t.b)
isconcrete = isconcrete_a && isconcrete_b
istype = istype_a && istype_b
# most users already handle the Union case, so here we assume that
# `isexact` only cares about the answers where there's actually a Type
# (and assuming other cases causing runtime errors)
ta === Union{} && return tb, isexact_b, isconcrete, istype
tb === Union{} && return ta, isexact_a, isconcrete, istype
return Union{ta, tb}, false, isconcrete, istype # at runtime, will be exactly one of these
end
return Any, false, false, false
end
bitcast_tfunc(@nospecialize(t), @nospecialize(x)) = instanceof_tfunc(t)[1]
math_tfunc(@nospecialize(x)) = widenconst(x)
math_tfunc(@nospecialize(x), @nospecialize(y)) = widenconst(x)
math_tfunc(@nospecialize(x), @nospecialize(y), @nospecialize(z)) = widenconst(x)
fptoui_tfunc(@nospecialize(t), @nospecialize(x)) = bitcast_tfunc(t, x)
fptosi_tfunc(@nospecialize(t), @nospecialize(x)) = bitcast_tfunc(t, x)
## conversion ##
add_tfunc(bitcast, 2, 2, bitcast_tfunc, 1)
add_tfunc(sext_int, 2, 2, bitcast_tfunc, 1)
add_tfunc(zext_int, 2, 2, bitcast_tfunc, 1)
add_tfunc(trunc_int, 2, 2, bitcast_tfunc, 1)
add_tfunc(fptoui, 2, 2, fptoui_tfunc, 1)
add_tfunc(fptosi, 2, 2, fptosi_tfunc, 1)
add_tfunc(uitofp, 2, 2, bitcast_tfunc, 1)
add_tfunc(sitofp, 2, 2, bitcast_tfunc, 1)
add_tfunc(fptrunc, 2, 2, bitcast_tfunc, 1)
add_tfunc(fpext, 2, 2, bitcast_tfunc, 1)
## arithmetic ##
add_tfunc(neg_int, 1, 1, math_tfunc, 1)
add_tfunc(add_int, 2, 2, math_tfunc, 1)
add_tfunc(sub_int, 2, 2, math_tfunc, 1)
add_tfunc(mul_int, 2, 2, math_tfunc, 4)
add_tfunc(sdiv_int, 2, 2, math_tfunc, 30)
add_tfunc(udiv_int, 2, 2, math_tfunc, 30)
add_tfunc(srem_int, 2, 2, math_tfunc, 30)
add_tfunc(urem_int, 2, 2, math_tfunc, 30)
add_tfunc(add_ptr, 2, 2, math_tfunc, 1)
add_tfunc(sub_ptr, 2, 2, math_tfunc, 1)
add_tfunc(neg_float, 1, 1, math_tfunc, 1)
add_tfunc(add_float, 2, 2, math_tfunc, 1)
add_tfunc(sub_float, 2, 2, math_tfunc, 1)
add_tfunc(mul_float, 2, 2, math_tfunc, 4)
add_tfunc(div_float, 2, 2, math_tfunc, 20)
add_tfunc(rem_float, 2, 2, math_tfunc, 20)
add_tfunc(fma_float, 3, 3, math_tfunc, 5)
add_tfunc(muladd_float, 3, 3, math_tfunc, 5)
## fast arithmetic ##
add_tfunc(neg_float_fast, 1, 1, math_tfunc, 1)
add_tfunc(add_float_fast, 2, 2, math_tfunc, 1)
add_tfunc(sub_float_fast, 2, 2, math_tfunc, 1)
add_tfunc(mul_float_fast, 2, 2, math_tfunc, 2)
add_tfunc(div_float_fast, 2, 2, math_tfunc, 10)
add_tfunc(rem_float_fast, 2, 2, math_tfunc, 10)
## bitwise operators ##
add_tfunc(and_int, 2, 2, math_tfunc, 1)
add_tfunc(or_int, 2, 2, math_tfunc, 1)
add_tfunc(xor_int, 2, 2, math_tfunc, 1)
add_tfunc(not_int, 1, 1, math_tfunc, 0) # usually used as not_int(::Bool) to negate a condition
add_tfunc(shl_int, 2, 2, math_tfunc, 1)
add_tfunc(lshr_int, 2, 2, math_tfunc, 1)
add_tfunc(ashr_int, 2, 2, math_tfunc, 1)
add_tfunc(bswap_int, 1, 1, math_tfunc, 1)
add_tfunc(ctpop_int, 1, 1, math_tfunc, 1)
add_tfunc(ctlz_int, 1, 1, math_tfunc, 1)
add_tfunc(cttz_int, 1, 1, math_tfunc, 1)
add_tfunc(checked_sdiv_int, 2, 2, math_tfunc, 40)
add_tfunc(checked_udiv_int, 2, 2, math_tfunc, 40)
add_tfunc(checked_srem_int, 2, 2, math_tfunc, 40)
add_tfunc(checked_urem_int, 2, 2, math_tfunc, 40)
## functions ##
add_tfunc(abs_float, 1, 1, math_tfunc, 2)
add_tfunc(copysign_float, 2, 2, math_tfunc, 2)
add_tfunc(flipsign_int, 2, 2, math_tfunc, 1)
add_tfunc(ceil_llvm, 1, 1, math_tfunc, 10)
add_tfunc(floor_llvm, 1, 1, math_tfunc, 10)
add_tfunc(trunc_llvm, 1, 1, math_tfunc, 10)
add_tfunc(rint_llvm, 1, 1, math_tfunc, 10)
add_tfunc(sqrt_llvm, 1, 1, math_tfunc, 20)
add_tfunc(sqrt_llvm_fast, 1, 1, math_tfunc, 20)
## same-type comparisons ##
cmp_tfunc(@nospecialize(x), @nospecialize(y)) = Bool
add_tfunc(eq_int, 2, 2, cmp_tfunc, 1)
add_tfunc(ne_int, 2, 2, cmp_tfunc, 1)
add_tfunc(slt_int, 2, 2, cmp_tfunc, 1)
add_tfunc(ult_int, 2, 2, cmp_tfunc, 1)
add_tfunc(sle_int, 2, 2, cmp_tfunc, 1)
add_tfunc(ule_int, 2, 2, cmp_tfunc, 1)
add_tfunc(eq_float, 2, 2, cmp_tfunc, 2)
add_tfunc(ne_float, 2, 2, cmp_tfunc, 2)
add_tfunc(lt_float, 2, 2, cmp_tfunc, 2)
add_tfunc(le_float, 2, 2, cmp_tfunc, 2)
add_tfunc(fpiseq, 2, 2, cmp_tfunc, 1)
add_tfunc(eq_float_fast, 2, 2, cmp_tfunc, 1)
add_tfunc(ne_float_fast, 2, 2, cmp_tfunc, 1)
add_tfunc(lt_float_fast, 2, 2, cmp_tfunc, 1)
add_tfunc(le_float_fast, 2, 2, cmp_tfunc, 1)
## checked arithmetic ##
chk_tfunc(@nospecialize(x), @nospecialize(y)) = Tuple{widenconst(x), Bool}
add_tfunc(checked_sadd_int, 2, 2, chk_tfunc, 10)
add_tfunc(checked_uadd_int, 2, 2, chk_tfunc, 10)
add_tfunc(checked_ssub_int, 2, 2, chk_tfunc, 10)
add_tfunc(checked_usub_int, 2, 2, chk_tfunc, 10)
add_tfunc(checked_smul_int, 2, 2, chk_tfunc, 10)
add_tfunc(checked_umul_int, 2, 2, chk_tfunc, 10)
## other, misc intrinsics ##
add_tfunc(Core.Intrinsics.llvmcall, 3, INT_INF,
(@nospecialize(fptr), @nospecialize(rt), @nospecialize(at), a...) -> instanceof_tfunc(rt)[1], 10)
cglobal_tfunc(@nospecialize(fptr)) = Ptr{Cvoid}
cglobal_tfunc(@nospecialize(fptr), @nospecialize(t)) = (isType(t) ? Ptr{t.parameters[1]} : Ptr)
cglobal_tfunc(@nospecialize(fptr), t::Const) = (isa(t.val, Type) ? Ptr{t.val} : Ptr)
add_tfunc(Core.Intrinsics.cglobal, 1, 2, cglobal_tfunc, 5)
add_tfunc(Core.Intrinsics.have_fma, 1, 1, @nospecialize(x)->Bool, 1)
function ifelse_tfunc(@nospecialize(cnd), @nospecialize(x), @nospecialize(y))
if isa(cnd, Const)
if cnd.val === true
return x
elseif cnd.val === false
return y
else
return Bottom
end
elseif isa(cnd, Conditional)
# optimized (if applicable) in abstract_call
elseif !(Bool ⊑ cnd)
return Bottom
end
return tmerge(x, y)
end
add_tfunc(Core.ifelse, 3, 3, ifelse_tfunc, 1)
function egal_tfunc(@nospecialize(x), @nospecialize(y))
xx = widenconditional(x)
yy = widenconditional(y)
if isa(x, Conditional) && isa(yy, Const)
yy.val === false && return Conditional(x.var, x.elsetype, x.vtype)
yy.val === true && return x
return Const(false)
elseif isa(y, Conditional) && isa(xx, Const)
xx.val === false && return Conditional(y.var, y.elsetype, y.vtype)
xx.val === true && return y
return Const(false)
elseif isa(xx, Const) && isa(yy, Const)
return Const(xx.val === yy.val)
elseif !hasintersect(widenconst(xx), widenconst(yy))
return Const(false)
elseif (isa(xx, Const) && y === typeof(xx.val) && isdefined(y, :instance)) ||
(isa(yy, Const) && x === typeof(yy.val) && isdefined(x, :instance))
return Const(true)
end
return Bool
end
add_tfunc(===, 2, 2, egal_tfunc, 1)
function isdefined_nothrow(argtypes::Array{Any, 1})
length(argtypes) == 2 || return false
return hasintersect(widenconst(argtypes[1]), Module) ?
argtypes[2] ⊑ Symbol :
(argtypes[2] ⊑ Symbol || argtypes[2] ⊑ Int)
end
isdefined_tfunc(arg1, sym, order) = (@nospecialize; isdefined_tfunc(arg1, sym))
function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
if isa(arg1, Const)
a1 = typeof(arg1.val)
else
a1 = widenconst(arg1)
end
if isType(a1)
return Bool
end
a1 = unwrap_unionall(a1)
if isa(a1, DataType) && !isabstracttype(a1)
if a1 === Module
hasintersect(widenconst(sym), Symbol) || return Bottom
if isa(sym, Const) && isa(sym.val, Symbol) && isa(arg1, Const) &&
isdefined(arg1.val::Module, sym.val::Symbol)
return Const(true)
end
elseif isa(sym, Const)
val = sym.val
if isa(val, Symbol)
idx = fieldindex(a1, val, false)::Int
elseif isa(val, Int)
idx = val
else
return Bottom
end
if 1 <= idx <= datatype_min_ninitialized(a1)
return Const(true)
elseif a1.name === _NAMEDTUPLE_NAME
if isconcretetype(a1)
return Const(false)
else
ns = a1.parameters[1]
if isa(ns, Tuple)
return Const(1 <= idx <= length(ns))
end
end
elseif idx <= 0 || (!isvatuple(a1) && idx > fieldcount(a1))
return Const(false)
elseif isa(arg1, Const)
arg1v = (arg1::Const).val
if !ismutable(arg1v) || isdefined(arg1v, idx) || isconst(typeof(arg1v), idx)
return Const(isdefined(arg1v, idx))
end
elseif !isvatuple(a1)
fieldT = fieldtype(a1, idx)
if isa(fieldT, DataType) && isbitstype(fieldT)
return Const(true)
end
end
end
elseif isa(a1, Union)
return tmerge(isdefined_tfunc(a1.a, sym),
isdefined_tfunc(a1.b, sym))
end
return Bool
end
add_tfunc(isdefined, 2, 3, isdefined_tfunc, 1)
function sizeof_nothrow(@nospecialize(x))
if isa(x, Const)
if !isa(x.val, Type) || x.val === DataType
return true
end
elseif isa(x, Conditional)
return true
end
xu = unwrap_unionall(x)
if isa(xu, Union)
return sizeof_nothrow(rewrap_unionall(xu.a, x)) &&
sizeof_nothrow(rewrap_unionall(xu.b, x))
end
t, exact, isconcrete = instanceof_tfunc(x)
if t === Bottom
# x must be an instance (not a Type) or is the Bottom type object
x = widenconst(x)
return !hasintersect(x, Type)
end
x = unwrap_unionall(t)
if isconcrete
if isa(x, DataType) && x.layout != C_NULL
# there's just a few concrete types with an opaque layout
(datatype_nfields(x) == 0 && !datatype_pointerfree(x)) && return false
end
return true # these must always have a size of these
end
exact || return false # Could always be the type Bottom at runtime, for example, which throws
t === DataType && return true # DataType itself has a size
if isa(x, Union)
isinline = uniontype_layout(x)[1]
return isinline # even any subset of this union would have a size
end
isa(x, DataType) || return false
x.layout == C_NULL && return false
(datatype_nfields(x) == 0 && !datatype_pointerfree(x)) && return false # is-layout-opaque
return true
end
function _const_sizeof(@nospecialize(x))
# Constant Vector does not have constant size
isa(x, Vector) && return Int
size = try
Core.sizeof(x)
catch ex
# Might return
# "argument is an abstract type; size is indeterminate" or
# "type does not have a fixed size"
isa(ex, ErrorException) || rethrow()
return Int
end
return Const(size)
end
function sizeof_tfunc(@nospecialize(x),)
isa(x, Const) && return _const_sizeof(x.val)
isa(x, Conditional) && return _const_sizeof(Bool)
isconstType(x) && return _const_sizeof(x.parameters[1])
xu = unwrap_unionall(x)
if isa(xu, Union)
return tmerge(sizeof_tfunc(rewrap_unionall(xu.a, x)),
sizeof_tfunc(rewrap_unionall(xu.b, x)))
end
# Core.sizeof operates on either a type or a value. First check which
# case we're in.
t, exact = instanceof_tfunc(x)
if t !== Bottom
# The value corresponding to `x` at runtime could be a type.
# Normalize the query to ask about that type.
x = unwrap_unionall(t)
if exact && isa(x, Union)
isinline = uniontype_layout(x)[1]
return isinline ? Const(Int(Core.sizeof(x))) : Bottom
end
isa(x, DataType) || return Int
(isconcretetype(x) || isprimitivetype(x)) && return _const_sizeof(x)
else
x = widenconst(x)
x !== DataType && isconcretetype(x) && return _const_sizeof(x)
isprimitivetype(x) && return _const_sizeof(x)
end
return Int
end
add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 1)
function nfields_tfunc(@nospecialize(x))
isa(x, Const) && return Const(nfields(x.val))
isa(x, Conditional) && return Const(0)
x = unwrap_unionall(widenconst(x))
isconstType(x) && return Const(nfields(x.parameters[1]))
if isa(x, DataType) && !isabstracttype(x)
if !(x.name === Tuple.name && isvatuple(x)) &&
!(x.name === _NAMEDTUPLE_NAME && !isconcretetype(x))
return Const(isdefined(x, :types) ? length(x.types) : length(x.name.names))
end
end
if isa(x, Union)
na = nfields_tfunc(x.a)
na === Int && return Int
return tmerge(na, nfields_tfunc(x.b))
end
return Int
end
add_tfunc(nfields, 1, 1, nfields_tfunc, 1)
add_tfunc(Core._expr, 1, INT_INF, (@nospecialize args...)->Expr, 100)
add_tfunc(svec, 0, INT_INF, (@nospecialize args...)->SimpleVector, 20)
function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub_arg))
lb = Union{}
ub = Any
ub_certain = lb_certain = true
if isa(n, Const)
nval = n.val
isa(nval, Symbol) || return Union{}
if isa(lb_arg, Const)
lb = lb_arg.val
elseif isType(lb_arg)
lb = lb_arg.parameters[1]
lb_certain = false
else
return TypeVar
end
if isa(ub_arg, Const)
ub = ub_arg.val
elseif isType(ub_arg)
ub = ub_arg.parameters[1]
ub_certain = false
else
return TypeVar
end
tv = TypeVar(nval, lb, ub)
return PartialTypeVar(tv, lb_certain, ub_certain)
end
return TypeVar
end
function typebound_nothrow(b)
b = widenconst(b)
(b ⊑ TypeVar) && return true
if isType(b)
return true
end
return false
end
function typevar_nothrow(n, lb, ub)
(n ⊑ Symbol) || return false
typebound_nothrow(lb) || return false
typebound_nothrow(ub) || return false
return true
end
add_tfunc(Core._typevar, 3, 3, typevar_tfunc, 100)
add_tfunc(applicable, 1, INT_INF, (@nospecialize(f), args...)->Bool, 100)
add_tfunc(Core.Intrinsics.arraylen, 1, 1, @nospecialize(x)->Int, 4)
function arraysize_tfunc(@nospecialize(ary), @nospecialize(dim))
hasintersect(widenconst(ary), Array) || return Bottom
hasintersect(widenconst(dim), Int) || return Bottom
return Int
end
add_tfunc(arraysize, 2, 2, arraysize_tfunc, 4)
function arraysize_nothrow(argtypes::Vector{Any})
length(argtypes) == 2 || return false
ary = argtypes[1]
dim = argtypes[2]
ary ⊑ Array || return false
if isa(dim, Const)
dimval = dim.val
return isa(dimval, Int) && dimval > 0
end
return false
end
function pointer_eltype(@nospecialize(ptr))
a = widenconst(ptr)
if !has_free_typevars(a)
unw = unwrap_unionall(a)
if isa(unw, DataType) && unw.name === Ptr.body.name
T = unw.parameters[1]
valid_as_lattice(T) || return Bottom
return rewrap_unionall(T, a)
end
end
return Any
end
function atomic_pointermodify_tfunc(ptr, op, v, order)
@nospecialize
a = widenconst(ptr)
if !has_free_typevars(a)
unw = unwrap_unionall(a)
if isa(unw, DataType) && unw.name === Ptr.body.name
T = unw.parameters[1]
# note: we could sometimes refine this to a PartialStruct if we analyzed `op(T, T)::T`
valid_as_lattice(T) || return Bottom
return rewrap_unionall(Pair{T, T}, a)
end
end
return Pair
end
function atomic_pointerreplace_tfunc(ptr, x, v, success_order, failure_order)
@nospecialize
a = widenconst(ptr)
if !has_free_typevars(a)
unw = unwrap_unionall(a)
if isa(unw, DataType) && unw.name === Ptr.body.name
T = unw.parameters[1]
valid_as_lattice(T) || return Bottom
return rewrap_unionall(ccall(:jl_apply_cmpswap_type, Any, (Any,), T), a)
end
end
return ccall(:jl_apply_cmpswap_type, Any, (Any,), T) where T
end
add_tfunc(pointerref, 3, 3, (a, i, align) -> (@nospecialize; pointer_eltype(a)), 4)
add_tfunc(pointerset, 4, 4, (a, v, i, align) -> (@nospecialize; a), 5)
add_tfunc(atomic_fence, 1, 1, (order) -> (@nospecialize; Nothing), 4)
add_tfunc(atomic_pointerref, 2, 2, (a, order) -> (@nospecialize; pointer_eltype(a)), 4)
add_tfunc(atomic_pointerset, 3, 3, (a, v, order) -> (@nospecialize; a), 5)
add_tfunc(atomic_pointerswap, 3, 3, (a, v, order) -> (@nospecialize; pointer_eltype(a)), 5)
add_tfunc(atomic_pointermodify, 4, 4, atomic_pointermodify_tfunc, 5)
add_tfunc(atomic_pointerreplace, 5, 5, atomic_pointerreplace_tfunc, 5)
# more accurate typeof_tfunc for vararg tuples abstract only in length
function typeof_concrete_vararg(t::DataType)
np = length(t.parameters)
for i = 1:np
p = t.parameters[i]
if i == np && isvarargtype(p)
if isdefined(p, :T) && !isdefined(p, :N) && isconcretetype(p.T)
return Type{Tuple{t.parameters[1:np-1]..., Vararg{p.T, N}}} where N
end
elseif !isconcretetype(p)
break
end
end
return nothing
end
function typeof_tfunc(@nospecialize(t))
isa(t, Const) && return Const(typeof(t.val))
t = widenconst(t)
if isType(t)
tp = t.parameters[1]
if hasuniquerep(tp)
return Const(typeof(tp))
end
elseif isa(t, DataType)
if isconcretetype(t)
return Const(t)
elseif t === Any
return DataType
else
if t.name === Tuple.name
tt = typeof_concrete_vararg(t)
tt === nothing || return tt
end
return Type{<:t}
end
elseif isa(t, Union)
a = widenconst(_typeof_tfunc(t.a))
b = widenconst(_typeof_tfunc(t.b))
return Union{a, b}
elseif isa(t, UnionAll)
u = unwrap_unionall(t)
if isa(u, DataType) && !isabstracttype(u)
if u.name === Tuple.name
uu = typeof_concrete_vararg(u)
if uu !== nothing
return rewrap_unionall(uu, t)
end
else
return rewrap_unionall(Type{u}, t)
end
end
return rewrap_unionall(widenconst(typeof_tfunc(u)), t)
end
return DataType # typeof(anything)::DataType
end
# helper function of `typeof_tfunc`, which accepts `TypeVar`
function _typeof_tfunc(@nospecialize(t))
if isa(t, TypeVar)
return t.ub !== Any ? _typeof_tfunc(t.ub) : DataType
end
return typeof_tfunc(t)
end
add_tfunc(typeof, 1, 1, typeof_tfunc, 1)
function typeassert_tfunc(@nospecialize(v), @nospecialize(t))
t = instanceof_tfunc(t)[1]
t === Any && return v
return tmeet(v, t)
end
add_tfunc(typeassert, 2, 2, typeassert_tfunc, 4)
function isa_tfunc(@nospecialize(v), @nospecialize(tt))
t, isexact = instanceof_tfunc(tt)
if t === Bottom
# check if t could be equivalent to typeof(Bottom), since that's valid in `isa`, but the set of `v` is empty
# if `t` cannot have instances, it's also invalid on the RHS of isa
hasintersect(widenconst(tt), Type) || return Union{}
return Const(false)
end
if !has_free_typevars(t)
if v ⊑ t
if isexact && isnotbrokensubtype(v, t)
return Const(true)
end
else
if isa(v, Const) || isa(v, Conditional)
# this and the `isdispatchelem` below test for knowledge of a
# leaftype appearing on the LHS (ensuring the isa is precise)
return Const(false)
end
v = widenconst(v)
isdispatchelem(v) && return Const(false)
if !hasintersect(v, t)
# similar to `isnotbrokensubtype` check above, `typeintersect(v, t)`
# can't be trusted for kind types so we do an extra check here
if !iskindtype(v)
return Const(false)
end
end
end
end
# TODO: handle non-leaftype(t) by testing against lower and upper bounds
return Bool
end
add_tfunc(isa, 2, 2, isa_tfunc, 1)
function subtype_tfunc(@nospecialize(a), @nospecialize(b))
a, isexact_a = instanceof_tfunc(a)
b, isexact_b = instanceof_tfunc(b)
if !has_free_typevars(a) && !has_free_typevars(b)
if a <: b
if isexact_b || a === Bottom
return Const(true)
end
else
if isexact_a || (b !== Bottom && !hasintersect(a, b))
return Const(false)
end
end
end
return Bool
end
add_tfunc(<:, 2, 2, subtype_tfunc, 10)
function fieldcount_noerror(@nospecialize t)
if t isa UnionAll || t isa Union
t = argument_datatype(t)
if t === nothing
return nothing
end
t = t::DataType
elseif t === Union{}
return 0
end
if !(t isa DataType)
return nothing
end
if t.name === _NAMEDTUPLE_NAME
names, types = t.parameters
if names isa Tuple
return length(names)
end
if types isa DataType && types <: Tuple
return fieldcount_noerror(types)
end
abstr = true
else
abstr = isabstracttype(t) || (t.name === Tuple.name && isvatuple(t))
end
if abstr
return nothing
end
return isdefined(t, :types) ? length(t.types) : length(t.name.names)
end
function try_compute_fieldidx(typ::DataType, @nospecialize(field))
if isa(field, Symbol)
field = fieldindex(typ, field, false)
field == 0 && return nothing
elseif isa(field, Int)
# Numerical field name can only be of type `Int`
max_fields = fieldcount_noerror(typ)
max_fields === nothing && return nothing
(1 <= field <= max_fields) || return nothing
else
return nothing
end
return field
end
function getfield_nothrow(argtypes::Vector{Any})
if length(argtypes) == 2
boundscheck = Bool
elseif length(argtypes) == 3
boundscheck = argtypes[3]
if boundscheck === Const(:not_atomic) # TODO: this is assuming not atomic
boundscheck = Bool
end
elseif length(argtypes) == 4
boundscheck = argtypes[4]
else
return false
end
widenconst(boundscheck) !== Bool && return false
bounds_check_disabled = isa(boundscheck, Const) && boundscheck.val === false
return getfield_nothrow(argtypes[1], argtypes[2], !bounds_check_disabled)
end
function getfield_nothrow(@nospecialize(s00), @nospecialize(name), boundscheck::Bool)
# If we don't have boundscheck and don't know the field, don't even bother
if boundscheck
isa(name, Const) || return false
end
# If we have s00 being a const, we can potentially refine our type-based analysis above
if isa(s00, Const) || isconstType(s00)
if !isa(s00, Const)
sv = s00.parameters[1]
else
sv = s00.val
end
if isa(name, Const)
nval = name.val
if !isa(nval, Symbol)
isa(sv, Module) && return false
isa(nval, Int) || return false
end
return isdefined(sv, nval)
end
if !boundscheck && !isa(sv, Module)
# If bounds checking is disabled and all fields are assigned,
# we may assume that we don't throw
for i = 1:fieldcount(typeof(sv))
isdefined(sv, i) || return false
end
return true
end
return false
end
s0 = widenconst(s00)
s = unwrap_unionall(s0)
if isa(s, Union)
return getfield_nothrow(rewrap_unionall(s.a, s00), name, boundscheck) &&
getfield_nothrow(rewrap_unionall(s.b, s00), name, boundscheck)
elseif isa(s, DataType)
# Can't say anything about abstract types
isabstracttype(s) && return false
s.name.atomicfields == C_NULL || return false # TODO: currently we're only testing for ordering == :not_atomic
# If all fields are always initialized, and bounds check is disabled, we can assume
# we don't throw
if !boundscheck && s.name.n_uninitialized == 0
return true
end
# Else we need to know what the field is
isa(name, Const) || return false
field = try_compute_fieldidx(s, name.val)
field === nothing && return false
field <= datatype_min_ninitialized(s) && return true
# `try_compute_fieldidx` already check for field index bound.
!isvatuple(s) && isbitstype(fieldtype(s0, field)) && return true
end
return false
end
function getfield_tfunc(s00, name, boundscheck_or_order)
@nospecialize
t = isvarargtype(boundscheck_or_order) ? unwrapva(boundscheck_or_order) :
widenconst(boundscheck_or_order)
hasintersect(t, Symbol) || hasintersect(t, Bool) || return Bottom
return getfield_tfunc(s00, name)
end
function getfield_tfunc(s00, name, order, boundscheck)
@nospecialize
hasintersect(widenconst(order), Symbol) || return Bottom
if isvarargtype(boundscheck)
t = unwrapva(boundscheck)
hasintersect(t, Symbol) || hasintersect(t, Bool) || return Bottom
else
hasintersect(widenconst(boundscheck), Bool) || return Bottom
end
return getfield_tfunc(s00, name)
end
getfield_tfunc(@nospecialize(s00), @nospecialize(name)) = _getfield_tfunc(s00, name, false)
function _getfield_tfunc(@nospecialize(s00), @nospecialize(name), setfield::Bool)
if isa(s00, Conditional)
return Bottom # Bool has no fields
elseif isa(s00, Const) || isconstType(s00)
if !isa(s00, Const)
sv = s00.parameters[1]
else
sv = s00.val
end
if isa(name, Const)
nv = name.val
if isa(sv, Module)
setfield && return Bottom
if isa(nv, Symbol)
return abstract_eval_global(sv, nv)
end
return Bottom
end
if isa(nv, Symbol)
nv = fieldindex(typeof(sv), nv, false)
end
if !isa(nv, Int)
return Bottom
end
if isa(sv, DataType) && nv == DATATYPE_TYPES_FIELDINDEX && isdefined(sv, nv)
return Const(getfield(sv, nv))
end
if isconst(typeof(sv), nv)
if isdefined(sv, nv)
return Const(getfield(sv, nv))
end
return Union{}
end
end
s = typeof(sv)
elseif isa(s00, PartialStruct)
s = widenconst(s00)
sty = unwrap_unionall(s)::DataType
if isa(name, Const)
nv = name.val
if isa(nv, Symbol)
nv = fieldindex(sty, nv, false)
end
if isa(nv, Int) && 1 <= nv <= length(s00.fields)
return unwrapva(s00.fields[nv])
end
end
else
s = unwrap_unionall(s00)
end
if isa(s, Union)
return tmerge(_getfield_tfunc(rewrap_unionall(s.a, s00), name, setfield),
_getfield_tfunc(rewrap_unionall(s.b, s00), name, setfield))
end
isa(s, DataType) || return Any
isabstracttype(s) && return Any
if s <: Tuple && !(Int <: widenconst(name))
return Bottom
end
if s <: Module
setfield && return Bottom
hasintersect(widenconst(name), Symbol) || return Bottom
return Any
end
if s.name === _NAMEDTUPLE_NAME && !isconcretetype(s)
if isa(name, Const) && isa(name.val, Symbol)
if isa(s.parameters[1], Tuple)
name = Const(Int(ccall(:jl_field_index, Cint, (Any, Any, Cint), s, name.val, false)+1))
else
name = Int
end
elseif Symbol ⊑ name
name = Int
end
_ts = unwraptv(s.parameters[2])
_ts = rewrap_unionall(_ts, s00)
if !(_ts <: Tuple)
return Any
end
return _getfield_tfunc(_ts, name, setfield)
end
ftypes = datatype_fieldtypes(s)
nf = length(ftypes)
# If no value has this type, then this statement should be unreachable.
# Bail quickly now.
if !has_concrete_subtype(s) || nf == 0
return Bottom
end
if isa(name, Conditional)
return Bottom # can't index fields with Bool
end
if !isa(name, Const)
name = widenconst(name)
if !(Int <: name || Symbol <: name)
return Bottom
end
if nf == 1
return rewrap_unionall(unwrapva(ftypes[1]), s00)
end
# union together types of all fields
t = Bottom
for i in 1:nf
_ft = ftypes[i]
setfield && isconst(s, i) && continue
t = tmerge(t, rewrap_unionall(unwrapva(_ft), s00))
t === Any && break
end
return t
end
fld = name.val
if isa(fld, Symbol)
fld = fieldindex(s, fld, false)
end
if !isa(fld, Int)
return Bottom
end
if s <: Tuple && fld >= nf && isvarargtype(ftypes[nf])
return rewrap_unionall(unwrapva(ftypes[nf]), s00)
end
if fld < 1 || fld > nf
return Bottom
elseif setfield && isconst(s, fld)
return Bottom
end
R = ftypes[fld]
if isempty(s.parameters)
return R
end
return rewrap_unionall(R, s00)
end
function setfield!_tfunc(o, f, v, order)
@nospecialize
if !isvarargtype(order)
hasintersect(widenconst(order), Symbol) || return Bottom
end
return setfield!_tfunc(o, f, v)
end
function setfield!_tfunc(o, f, v)
@nospecialize
mutability_errorcheck(o) || return Bottom
ft = _getfield_tfunc(o, f, true)
ft === Bottom && return Bottom
hasintersect(widenconst(v), widenconst(ft)) || return Bottom
return v
end
function mutability_errorcheck(@nospecialize obj)
objt0 = widenconst(obj)
objt = unwrap_unionall(objt0)
if isa(objt, Union)
return mutability_errorcheck(rewrap_unionall(objt.a, objt0)) ||
mutability_errorcheck(rewrap_unionall(objt.b, objt0))
elseif isa(objt, DataType)
# Can't say anything about abstract types
isabstracttype(objt) && return true
return ismutabletype(objt)
end
return true
end
function setfield!_nothrow(argtypes::Vector{Any})
if length(argtypes) == 4
order = argtypes[4]
order === Const(:not_atomic) || return false # currently setfield!_nothrow is assuming not atomic
else
length(argtypes) == 3 || return false
end
return setfield!_nothrow(argtypes[1], argtypes[2], argtypes[3])
end
function setfield!_nothrow(s00, name, v)
@nospecialize
s0 = widenconst(s00)
s = unwrap_unionall(s0)
if isa(s, Union)
return setfield!_nothrow(rewrap_unionall(s.a, s00), name, v) &&
setfield!_nothrow(rewrap_unionall(s.b, s00), name, v)
elseif isa(s, DataType)
# Can't say anything about abstract types
isabstracttype(s) && return false
ismutabletype(s) || return false
s.name.atomicfields == C_NULL || return false # TODO: currently we're only testing for ordering == :not_atomic
isa(name, Const) || return false
field = try_compute_fieldidx(s, name.val)
field === nothing && return false
# `try_compute_fieldidx` already check for field index bound.
isconst(s, field) && return false
v_expected = fieldtype(s0, field)
return v ⊑ v_expected
end
return false
end
swapfield!_tfunc(o, f, v, order) = (@nospecialize; getfield_tfunc(o, f))
swapfield!_tfunc(o, f, v) = (@nospecialize; getfield_tfunc(o, f))
modifyfield!_tfunc(o, f, op, v, order) = (@nospecialize; modifyfield!_tfunc(o, f, op, v))
function modifyfield!_tfunc(o, f, op, v)
@nospecialize
T = _fieldtype_tfunc(o, isconcretetype(o), f)
T === Bottom && return Bottom
PT = Const(Pair)
return instanceof_tfunc(apply_type_tfunc(PT, T, T))[1]
end
function abstract_modifyfield!(interp::AbstractInterpreter, argtypes::Vector{Any}, sv::InferenceState)
nargs = length(argtypes)