Let’s say I start with the following:
julia> function f(x, y)
if y > 10
x + 1
else
x * 2
end
end
f (generic function with 1 method)
julia> @code_warntype f(2, 20)
Body::Int64
1 ─ %1 = (Base.slt_int)(10, y)::Bool
└── goto #3 if not %1
2 ─ %3 = (Base.add_int)(x, 1)::Int64
└── return %3
3 ─ %5 = (Base.mul_int)(x, 2)::Int64
└── return %5
Then, I realize that the compiler can optimize away a branch if it knows that it always choose the other branch:
julia> g() = f(2,3)
g (generic function with 2 methods)
julia> @code_warntype g()
Body::Int64
1 ─ return 4
That’s great, but if I define a global constant then why wouldn’t it optimize in this case?
julia> const cy = 3
3
julia> g(y) = f(2, y)
g (generic function with 2 methods)
julia> @code_warntype g(cy)
Body::Int64
1 ─ %1 = (Base.slt_int)(10, y)::Bool
└── goto #3 if not %1
2 ─ goto #4
3 ─ goto #4
4 ┄ %5 = φ (#2 => 3, #3 => 4)::Int64
└── return %5