Skip to content

Commit

Permalink
inlining: optimize and_int(x, true) and or_int(x, false)
Browse files Browse the repository at this point in the history
We need an additional inlining pass to optimize cases like
`and_int(x, true)` and `or_int(x, false)` to `x`.
  • Loading branch information
aviatesk committed Jun 7, 2024
1 parent 9477472 commit 6372248
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,24 @@ function early_inline_special_case(ir::IRCode, stmt::Expr, flag::UInt32,
elseif (optimizer_lattice(state.interp), cond, Bool) && stmt.args[3] === stmt.args[4]
return SomeCase(stmt.args[3])
end
elseif f === and_int && length(argtypes) == 3
argtype2 = argtypes[2]
if argtype2 isa Const && argtype2.val === true
return SomeCase(stmt.args[3])
end
argtype3 = argtypes[3]
if argtype3 isa Const && argtype3.val === true
return SomeCase(stmt.args[2])
end
elseif f === or_int && length(argtypes) == 3
argtype2 = argtypes[2]
if argtype2 isa Const && argtype2.val === false
return SomeCase(stmt.args[3])
end
argtype3 = argtypes[3]
if argtype3 isa Const && argtype3.val === false
return SomeCase(stmt.args[2])
end
end
return nothing
end
Expand Down
6 changes: 6 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2214,3 +2214,9 @@ let ir = Base.code_ircode((Issue52644,); optimize_until="Inlining") do t
@test irfunc(Issue52644(Tuple{})) === :DataType
@test_throws MethodError irfunc(Issue52644(Tuple{<:Integer}))
end

# inlining optimization for `and_int` and `or_int`
@test fully_eliminated(x->x&true, (Bool,); retval=Argument(2))
@test fully_eliminated(x->true&x, (Bool,); retval=Argument(2))
@test fully_eliminated(x->x|false, (Bool,); retval=Argument(2))
@test fully_eliminated(x->false|x, (Bool,); retval=Argument(2))

0 comments on commit 6372248

Please sign in to comment.