Skip to content

Commit 764d876

Browse files
authored
compiler: minor refactors (#43105)
- factor the `typeintersect(A, B) === Bottom` pattern into `hasintersect` - remove `uniontypes` allocation within `isdefined_tfunc` - don't use `==` for comparison to `Bottom`
1 parent 89c137c commit 764d876

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ function abstract_iteration(interp::AbstractInterpreter, @nospecialize(itft), @n
970970
end
971971
if nounion === Union{} || (nounion.parameters[1] <: valtype && nounion.parameters[2] <: statetype)
972972
# reached a fixpoint or iterator failed/gave invalid answer
973-
if typeintersect(stateordonet_widened, Nothing) === Union{}
973+
if !hasintersect(stateordonet_widened, Nothing)
974974
# ... but cannot terminate
975975
if !may_have_terminated
976976
# ... and cannot have terminated prior to this loop
@@ -1497,7 +1497,7 @@ function abstract_call(interp::AbstractInterpreter, arginfo::ArgInfo,
14971497
elseif f === nothing
14981498
# non-constant function, but the number of arguments is known
14991499
# and the ft is not a Builtin or IntrinsicFunction
1500-
if typeintersect(widenconst(ft), Union{Builtin, Core.OpaqueClosure}) != Union{}
1500+
if hasintersect(widenconst(ft), Union{Builtin, Core.OpaqueClosure})
15011501
add_remark!(interp, sv, "Could not identify method table for call")
15021502
return CallMeta(Any, false)
15031503
end

base/compiler/ssair/passes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function walk_to_defs(compact::IncrementalCompact, @nospecialize(defssa), @nospe
195195
val = OldSSAValue(val.id)
196196
end
197197
edge_typ = widenconst(compact_exprtype(compact, val))
198-
typeintersect(edge_typ, typeconstraint) === Union{} && continue
198+
hasintersect(edge_typ, typeconstraint) || continue
199199
push!(possible_predecessors, n)
200200
end
201201
for n in possible_predecessors

base/compiler/tfuncs.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function instanceof_tfunc(@nospecialize(t))
7575
t = widenconst(t)
7676
if t === Bottom
7777
return Bottom, true, true, false # runtime unreachable
78-
elseif t === typeof(Bottom) || typeintersect(t, Type) === Bottom
78+
elseif t === typeof(Bottom) || !hasintersect(t, Type)
7979
return Bottom, true, false, false # literal Bottom or non-Type
8080
elseif isType(t)
8181
tp = t.parameters[1]
@@ -246,7 +246,7 @@ function egal_tfunc(@nospecialize(x), @nospecialize(y))
246246
return Const(false)
247247
elseif isa(xx, Const) && isa(yy, Const)
248248
return Const(xx.val === yy.val)
249-
elseif typeintersect(widenconst(xx), widenconst(yy)) === Bottom
249+
elseif !hasintersect(widenconst(xx), widenconst(yy))
250250
return Const(false)
251251
elseif (isa(xx, Const) && y === typeof(xx.val) && isdefined(y, :instance)) ||
252252
(isa(yy, Const) && x === typeof(yy.val) && isdefined(x, :instance))
@@ -258,9 +258,9 @@ add_tfunc(===, 2, 2, egal_tfunc, 1)
258258

259259
function isdefined_nothrow(argtypes::Array{Any, 1})
260260
length(argtypes) == 2 || return false
261-
return typeintersect(widenconst(argtypes[1]), Module) === Union{} ?
262-
(argtypes[2] Symbol || argtypes[2] Int) :
263-
argtypes[2] Symbol
261+
return hasintersect(widenconst(argtypes[1]), Module) ?
262+
argtypes[2] Symbol :
263+
(argtypes[2] Symbol || argtypes[2] Int)
264264
end
265265
isdefined_tfunc(arg1, sym, order) = (@nospecialize; isdefined_tfunc(arg1, sym))
266266
function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
@@ -275,8 +275,9 @@ function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
275275
a1 = unwrap_unionall(a1)
276276
if isa(a1, DataType) && !isabstracttype(a1)
277277
if a1 === Module
278-
Symbol <: widenconst(sym) || return Bottom
279-
if isa(sym, Const) && isa(sym.val, Symbol) && isa(arg1, Const) && isdefined(arg1.val, sym.val)
278+
hasintersect(widenconst(sym), Symbol) || return Bottom
279+
if isa(sym, Const) && isa(sym.val, Symbol) && isa(arg1, Const) &&
280+
isdefined(arg1.val::Module, sym.val::Symbol)
280281
return Const(true)
281282
end
282283
elseif isa(sym, Const)
@@ -314,11 +315,8 @@ function isdefined_tfunc(@nospecialize(arg1), @nospecialize(sym))
314315
end
315316
end
316317
elseif isa(a1, Union)
317-
t = Bottom
318-
for u in uniontypes(a1)
319-
t = tmerge(t, isdefined_tfunc(u, sym))
320-
end
321-
return t
318+
return tmerge(isdefined_tfunc(a1.a, sym),
319+
isdefined_tfunc(a1.b, sym))
322320
end
323321
return Bool
324322
end
@@ -341,7 +339,7 @@ function sizeof_nothrow(@nospecialize(x))
341339
if t === Bottom
342340
# x must be an instance (not a Type) or is the Bottom type object
343341
x = widenconst(x)
344-
return typeintersect(x, Type) === Union{}
342+
return !hasintersect(x, Type)
345343
end
346344
x = unwrap_unionall(t)
347345
if isconcrete
@@ -599,9 +597,7 @@ function isa_tfunc(@nospecialize(v), @nospecialize(tt))
599597
if t === Bottom
600598
# check if t could be equivalent to typeof(Bottom), since that's valid in `isa`, but the set of `v` is empty
601599
# if `t` cannot have instances, it's also invalid on the RHS of isa
602-
if typeintersect(widenconst(tt), Type) === Union{}
603-
return Union{}
604-
end
600+
hasintersect(widenconst(tt), Type) || return Union{}
605601
return Const(false)
606602
end
607603
if !has_free_typevars(t)
@@ -617,7 +613,7 @@ function isa_tfunc(@nospecialize(v), @nospecialize(tt))
617613
end
618614
v = widenconst(v)
619615
isdispatchelem(v) && return Const(false)
620-
if typeintersect(v, t) === Bottom
616+
if !hasintersect(v, t)
621617
# similar to `isnotbrokensubtype` check above, `typeintersect(v, t)`
622618
# can't be trusted for kind types so we do an extra check here
623619
if !iskindtype(v)
@@ -640,7 +636,7 @@ function subtype_tfunc(@nospecialize(a), @nospecialize(b))
640636
return Const(true)
641637
end
642638
else
643-
if isexact_a || (b !== Bottom && typeintersect(a, b) === Union{})
639+
if isexact_a || (b !== Bottom && !hasintersect(a, b))
644640
return Const(false)
645641
end
646642
end
@@ -673,7 +669,7 @@ function fieldcount_noerror(@nospecialize t)
673669
return nothing
674670
end
675671
t = t::DataType
676-
elseif t == Union{}
672+
elseif t === Union{}
677673
return 0
678674
end
679675
if !(t isa DataType)
@@ -1297,7 +1293,7 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
12971293
end
12981294
else
12991295
if !isType(ai)
1300-
if !isa(ai, Type) || typeintersect(ai, Type) !== Bottom || typeintersect(ai, TypeVar) !== Bottom
1296+
if !isa(ai, Type) || hasintersect(ai, Type) || hasintersect(ai, TypeVar)
13011297
hasnonType = true
13021298
else
13031299
return Bottom

base/compiler/typeutils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ function typesubtract(@nospecialize(a), @nospecialize(b), MAX_UNION_SPLITTING::I
175175
return a # TODO: improve this bound?
176176
end
177177

178+
hasintersect(@nospecialize(a), @nospecialize(b)) = typeintersect(a, b) !== Bottom
179+
178180
function tvar_extent(@nospecialize t)
179181
while t isa TypeVar
180182
t = t.ub

0 commit comments

Comments
 (0)