Skip to content

Commit b490b84

Browse files
committed
remove Expr .typ field
1 parent bd1423d commit b490b84

28 files changed

+202
-248
lines changed

base/boot.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
#mutable struct Expr
8383
# head::Symbol
8484
# args::Array{Any,1}
85-
# typ::Any
8685
#end
8786

8887
#struct LineNumberNode

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function precise_container_type(@nospecialize(arg), @nospecialize(typ), vtypes::
341341
if isa(arg, Expr) && arg.head === :call && (abstract_evals_to_constant(arg.args[1], svec, vtypes, sv) ||
342342
abstract_evals_to_constant(arg.args[1], tuple, vtypes, sv))
343343
aa = arg.args
344-
result = Any[ (isa(aa[j],Expr) ? aa[j].typ : abstract_eval(aa[j],vtypes,sv)) for j=2:length(aa) ]
344+
result = Any[ abstract_eval(aa[j],vtypes,sv) for j=2:length(aa) ]
345345
if _any(isvarargtype, result)
346346
return Any[Vararg{Any}]
347347
end
@@ -898,7 +898,6 @@ function abstract_eval(@nospecialize(e), vtypes::VarTable, sv::InferenceState)
898898
# replace singleton types with their equivalent Const object
899899
t = Const(t.instance)
900900
end
901-
e.typ = t
902901
return t
903902
end
904903

base/compiler/optimize.jl

Lines changed: 32 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ function optimize(me::InferenceState)
190190
# only care about certain errors (e.g. method errors and type errors).
191191
if length(me.src.code) < 10
192192
proven_pure = true
193-
for stmt in me.src.code
194-
if !statement_effect_free(stmt, me)
193+
for i in 1:length(me.src.code)
194+
if !statement_effect_free(me.src.code[i], me, me.src.ssavaluetypes[i])
195195
proven_pure = false
196196
break
197197
end
@@ -359,7 +359,6 @@ end
359359
function annotate_slot_load!(e::Expr, vtypes::VarTable, sv::InferenceState, undefs::Array{Bool,1})
360360
head = e.head
361361
i0 = 1
362-
e.typ = maybe_widen_conditional(e.typ)
363362
if is_meta_expr_head(head) || head === :const
364363
return
365364
end
@@ -406,13 +405,7 @@ function record_slot_assign!(sv::InferenceState)
406405
lhs = expr.args[1]
407406
rhs = expr.args[2]
408407
if isa(lhs, Slot)
409-
if isa(rhs, Slot)
410-
# exprtype isn't yet computed for slots
411-
vt = st_i[slot_id(rhs)].typ
412-
else
413-
vt = exprtype(rhs, sv)
414-
end
415-
vt = widenconst(vt)
408+
vt = widenconst(sv.src.ssavaluetypes[i])
416409
if vt !== Bottom
417410
id = slot_id(lhs)
418411
otherTy = slottypes[id]
@@ -517,7 +510,6 @@ end
517510

518511
# widen all Const elements in type annotations
519512
function _widen_all_consts!(e::Expr, untypedload::Vector{Bool}, slottypes::Vector{Any})
520-
e.typ = widenconst(e.typ)
521513
for i = 1:length(e.args)
522514
x = e.args[i]
523515
if isa(x, Expr)
@@ -704,29 +696,29 @@ function is_pure_builtin(@nospecialize(f))
704696
end
705697
end
706698

707-
function statement_effect_free(@nospecialize(e), me::InferenceState)
699+
function statement_effect_free(@nospecialize(e), me::InferenceState, @nospecialize(etype))
708700
if isa(e, Expr)
709701
if e.head === :(=)
710-
return !isa(e.args[1], GlobalRef) && effect_free(e.args[2], me, false)
702+
return !isa(e.args[1], GlobalRef) && effect_free(e.args[2], me, false, etype)
711703
elseif e.head === :gotoifnot
712-
return effect_free(e.args[1], me, false)
704+
return effect_free(e.args[1], me, false, etype)
713705
end
714706
elseif isa(e, GotoNode)
715707
return true
716708
end
717-
return effect_free(e, me, false)
709+
return effect_free(e, me, false, etype)
718710
end
719711

720-
effect_free(@nospecialize(e), s::InferenceState, allow_volatile::Bool) =
721-
effect_free(e, s.src, s.sp, allow_volatile)
712+
effect_free(@nospecialize(e), s::InferenceState, allow_volatile::Bool, @nospecialize(etype)) =
713+
effect_free(e, s.src, s.sp, allow_volatile, etype)
722714

723-
effect_free(@nospecialize(e), s::OptimizationState, allow_volatile::Bool) =
724-
effect_free(e, s.src, s.sp, allow_volatile)
715+
effect_free(@nospecialize(e), s::OptimizationState, allow_volatile::Bool, @nospecialize(etype)) =
716+
effect_free(e, s.src, s.sp, allow_volatile, etype)
725717

726718
# detect some important side-effect-free calls (allow_volatile=true)
727719
# and some affect-free calls (allow_volatile=false) -- affect_free means the call
728720
# cannot be affected by previous calls, except assignment nodes
729-
function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile::Bool)
721+
function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile::Bool, @nospecialize(etype))
730722
if isa(e, GlobalRef)
731723
return (isdefined(e.mod, e.name) && (allow_volatile || isconst(e.mod, e.name)))
732724
elseif isa(e, Slot)
@@ -739,9 +731,9 @@ function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile
739731
end
740732
if head === :static_parameter
741733
# if we aren't certain enough about the type, it might be an UndefVarError at runtime
742-
return isa(e.typ, Const) || issingletontype(widenconst(e.typ))
734+
return isa(etype, Const) || issingletontype(widenconst(etype))
743735
end
744-
if e.typ === Bottom
736+
if etype === Bottom
745737
return false
746738
end
747739
ea = e.args
@@ -753,12 +745,11 @@ function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile
753745
elseif is_known_call(e, getfield, src, spvals)
754746
nargs = length(ea)
755747
(3 <= nargs <= 4) || return false
756-
et = exprtype(e, src, spvals)
757748
# TODO: check ninitialized
758-
if !isa(et, Const) && !isconstType(et)
749+
if !isa(etype, Const) && !isconstType(etype)
759750
# first argument must be immutable to ensure e is affect_free
760751
a = ea[2]
761-
typ = unwrap_unionall(widenconst(exprtype(a, src, spvals)))
752+
typ = unwrap_unionall(widenconst(argextype(a, src, spvals)))
762753
if isType(typ)
763754
# all fields of subtypes of Type are effect-free
764755
# (including the non-inferrable uid field)
@@ -770,7 +761,7 @@ function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile
770761
end
771762
# fall-through
772763
elseif is_known_call(e, _apply, src, spvals) && length(ea) > 1
773-
ft = exprtype(ea[2], src, spvals)
764+
ft = argextype(ea[2], src, spvals)
774765
if !isa(ft, Const) || (!contains_is(_PURE_BUILTINS, ft.val) &&
775766
ft.val !== Core.sizeof)
776767
return false
@@ -781,7 +772,7 @@ function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile
781772
end
782773
elseif head === :new
783774
a = ea[1]
784-
typ = exprtype(a, src, spvals)
775+
typ = argextype(a, src, spvals)
785776
# `Expr(:new)` of unknown type could raise arbitrary TypeError.
786777
typ, isexact = instanceof_tfunc(typ)
787778
isexact || return false
@@ -792,7 +783,7 @@ function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile
792783
end
793784
fieldcount(typ) >= length(ea) - 1 || return false
794785
for fld_idx in 1:(length(ea) - 1)
795-
eT = exprtype(ea[fld_idx + 1], src, spvals)
786+
eT = argextype(ea[fld_idx + 1], src, spvals)
796787
fT = fieldtype(typ, fld_idx)
797788
eT fT || return false
798789
end
@@ -809,7 +800,7 @@ function effect_free(@nospecialize(e), src, spvals::SimpleVector, allow_volatile
809800
return false
810801
end
811802
for a in ea
812-
if !effect_free(a, src, spvals, allow_volatile)
803+
if !effect_free(a, src, spvals, allow_volatile, argextype(a, src, spvals))
813804
return false
814805
end
815806
end
@@ -845,19 +836,20 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, spvals::SimpleVector
845836
argcost = 0
846837
for a in ex.args
847838
if a isa Expr
848-
argcost = plus_saturate(argcost, statement_cost(a, line, src, spvals, params))
839+
argcost = plus_saturate(argcost, statement_cost(a, -1, src, spvals, params))
849840
end
850841
end
851842
if head == :return || head == :(=)
852843
return argcost
853844
end
845+
extyp = line == -1 ? Any : src.ssavaluetypes[line]
854846
if head == :call
855-
extyp = exprtype(ex.args[1], src, spvals)
856-
if isa(extyp, Type)
847+
ftyp = argextype(ex.args[1], src, spvals)
848+
if isa(ftyp, Type)
857849
return argcost
858850
end
859-
if isa(extyp, Const)
860-
f = (extyp::Const).val
851+
if isa(ftyp, Const)
852+
f = (ftyp::Const).val
861853
if isa(f, IntrinsicFunction)
862854
iidx = Int(reinterpret(Int32, f::IntrinsicFunction)) + 1
863855
if !isassigned(T_IFUNC_COST, iidx)
@@ -869,15 +861,15 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, spvals::SimpleVector
869861
if isa(f, Builtin)
870862
# The efficiency of operations like a[i] and s.b
871863
# depend strongly on whether the result can be
872-
# inferred, so check ex.typ
864+
# inferred, so check the type of ex
873865
if f == Main.Core.getfield || f == Main.Core.tuple
874866
# we might like to penalize non-inferrability, but
875867
# tuple iteration/destructuring makes that
876868
# impossible
877-
# return plus_saturate(argcost, isknowntype(ex.typ) ? 1 : params.inline_nonleaf_penalty)
869+
# return plus_saturate(argcost, isknowntype(extyp) ? 1 : params.inline_nonleaf_penalty)
878870
return argcost
879871
elseif f == Main.Core.arrayref
880-
return plus_saturate(argcost, isknowntype(ex.typ) ? 4 : params.inline_nonleaf_penalty)
872+
return plus_saturate(argcost, isknowntype(extyp) ? 4 : params.inline_nonleaf_penalty)
881873
end
882874
fidx = findfirst(x->x===f, T_FFUNC_KEY)
883875
if fidx === nothing
@@ -895,7 +887,7 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, spvals::SimpleVector
895887
# run-time of the function, we omit them from
896888
# consideration. This way, non-inlined error branches do not
897889
# prevent inlining.
898-
return ex.typ == Union{} ? 0 : plus_saturate(20, argcost)
890+
return extyp == Union{} ? 0 : plus_saturate(20, argcost)
899891
elseif head == :llvmcall
900892
return plus_saturate(10, argcost) # a wild guess at typical cost
901893
elseif head == :enter
@@ -935,38 +927,19 @@ function inline_worthy(body::Array{Any,1}, src::CodeInfo, spvals::SimpleVector,
935927
return bodycost <= cost_threshold
936928
end
937929

938-
function inline_worthy(body::Expr, src::CodeInfo, spvals::SimpleVector, params::Params,
939-
cost_threshold::Integer=params.inline_cost_threshold)
940-
bodycost = statement_cost(body, typemax(Int), src, spvals, params)
941-
return bodycost <= cost_threshold
942-
end
943-
944-
function inline_worthy(@nospecialize(body), src::CodeInfo, spvals::SimpleVector, params::Params,
945-
cost_threshold::Integer=params.inline_cost_threshold)
946-
newbody = exprtype(body, src, spvals)
947-
!isa(newbody, Expr) && return true
948-
return inline_worthy(newbody, src, spvals, params, cost_threshold)
949-
end
950-
951-
function mk_tuplecall(args, sv::OptimizationState)
952-
e = Expr(:call, TOP_TUPLE, args...)
953-
e.typ = tuple_tfunc(Tuple{Any[widenconst(exprtype(x, sv)) for x in args]...})
954-
return e
955-
end
956-
957930
function is_known_call(e::Expr, @nospecialize(func), src, spvals)
958931
if e.head !== :call
959932
return false
960933
end
961-
f = exprtype(e.args[1], src, spvals)
934+
f = argextype(e.args[1], src, spvals)
962935
return isa(f, Const) && f.val === func
963936
end
964937

965938
function is_known_call_p(e::Expr, @nospecialize(pred), src, spvals)
966939
if e.head !== :call
967940
return false
968941
end
969-
f = exprtype(e.args[1], src, spvals)
942+
f = argextype(e.args[1], src, spvals)
970943
return (isa(f, Const) && pred(f.val)) || (isType(f) && pred(f.parameters[1]))
971944
end
972945

base/compiler/ssair/driver.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ function just_construct_ssa(ci::CodeInfo, code::Vector{Any}, nargs::Int, spvals:
5757
oldidx = 1
5858
changemap = fill(0, length(code))
5959
while idx <= length(code)
60-
stmt = code[idx]
61-
if isexpr(stmt, :(=))
62-
stmt = stmt.args[2]
63-
end
64-
if isa(stmt, Expr) && stmt.typ === Union{}
60+
if code[idx] isa Expr && ci.ssavaluetypes[idx] === Union{}
6561
if !(idx < length(code) && isexpr(code[idx+1], :unreachable))
6662
insert!(code, idx + 1, ReturnNode())
6763
insert!(ci.codelocs, idx + 1, ci.codelocs[idx])
@@ -115,7 +111,7 @@ function just_construct_ssa(ci::CodeInfo, code::Vector{Any}, nargs::Int, spvals:
115111
argtypes = ci.slottypes[1:(nargs+1)]
116112
IRCode(code, Any[], ci.codelocs, flags, cfg, collect(LineInfoNode, ci.linetable), argtypes, meta, spvals)
117113
end
118-
@timeit "construct_ssa" ir = construct_ssa!(ci, code, ir, domtree, defuse_insts, nargs)
114+
@timeit "construct_ssa" ir = construct_ssa!(ci, code, ir, domtree, defuse_insts, nargs, spvals)
119115
return ir
120116
end
121117

0 commit comments

Comments
 (0)