Skip to content

Commit 5d82d80

Browse files
authored
add tfuncs for [and|or]_int intrinsics (#51266)
So that they can be constant folded when either of argument is known to be `Core([false|true])`. It may help inference accuracy.
1 parent ad27e67 commit 5d82d80

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

base/compiler/tfuncs.jl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,33 @@ add_tfunc(div_float_fast, 2, 2, math_tfunc, 2)
198198
# bitwise operators
199199
# -----------------
200200

201+
@nospecs and_int_tfunc(𝕃::AbstractLattice, x, y) = and_int_tfunc(widenlattice(𝕃), x, y)
202+
@nospecs function and_int_tfunc(𝕃::ConstsLattice, x, y)
203+
if isa(x, Const) && x.val === false && widenconst(y) === Bool
204+
return Const(false)
205+
elseif isa(y, Const) && y.val === false && widenconst(x) === Bool
206+
return Const(false)
207+
end
208+
return and_int_tfunc(widenlattice(𝕃), x, y)
209+
end
210+
@nospecs and_int_tfunc(::JLTypeLattice, x, y) = widenconst(x)
211+
212+
@nospecs or_int_tfunc(𝕃::AbstractLattice, x, y) = or_int_tfunc(widenlattice(𝕃), x, y)
213+
@nospecs function or_int_tfunc(𝕃::ConstsLattice, x, y)
214+
if isa(x, Const) && x.val === true && widenconst(y) === Bool
215+
return Const(true)
216+
elseif isa(y, Const) && y.val === true && widenconst(x) === Bool
217+
return Const(true)
218+
end
219+
return or_int_tfunc(widenlattice(𝕃), x, y)
220+
end
221+
@nospecs or_int_tfunc(::JLTypeLattice, x, y) = widenconst(x)
222+
201223
@nospecs shift_tfunc(𝕃::AbstractLattice, x, y) = shift_tfunc(widenlattice(𝕃), x, y)
202224
@nospecs shift_tfunc(::JLTypeLattice, x, y) = widenconst(x)
203225

204-
add_tfunc(and_int, 2, 2, math_tfunc, 1)
205-
add_tfunc(or_int, 2, 2, math_tfunc, 1)
226+
add_tfunc(and_int, 2, 2, and_int_tfunc, 1)
227+
add_tfunc(or_int, 2, 2, or_int_tfunc, 1)
206228
add_tfunc(xor_int, 2, 2, math_tfunc, 1)
207229
add_tfunc(not_int, 1, 1, math_tfunc, 0) # usually used as not_int(::Bool) to negate a condition
208230
add_tfunc(shl_int, 2, 2, shift_tfunc, 1)

test/compiler/inference.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5190,3 +5190,11 @@ foo51090(b) = return bar51090(b)
51905190
@test Base.return_types() do
51915191
Base.or_int(true, 1)
51925192
end |> only === Union{}
5193+
5194+
# [add|or]_int tfuncs
5195+
@test Base.return_types((Bool,)) do b
5196+
Val(Core.Intrinsics.and_int(b, false))
5197+
end |> only == Val{false}
5198+
@test Base.return_types((Bool,)) do b
5199+
Val(Core.Intrinsics.or_int(true, b))
5200+
end |> only == Val{true}

0 commit comments

Comments
 (0)