I played around with second derivatives and ForwardDiff.jl and found something unexpected, so I would like to ask about it. Basically, the generated LLVM representation is multiplying a whole code branch by zero before returning. Here’s a simple example:

```
julia> using ForwardDiff: derivative
julia> f(x) = x^3
f (generic function with 1 method)
julia> ∂f(x) = derivative(f, x)
∂f (generic function with 1 method)
julia> ∂²f(x) = derivative(∂f, x)
∂²f (generic function with 1 method)
julia> f(1.0), ∂f(1.0), ∂²f(1.0) # everything OK so far
(1.0, 3.0, 6.0)
```

And here are the LLVM representations for both the first- and second-order derivatives (with some annotations made by me - please tell me if I’m reading correctly):

```
julia> @code_llvm debuginfo=:none ∂f(1.0)
define double @"julia_\E2\88\82f_17305"(double) {
top:
%1 = fmul double %0, %0 # %1 = x * x
%2 = fmul double %1, 3.000000e+00 # %2 = 3 * x^2
ret double %2 # very nice, great success
}
julia> @code_llvm debuginfo=:none ∂²f(1.0)
define double @"julia_\E2\88\82\C2\B2f_17314"(double) {
top:
%1 = fmul double %0, %0 # %1 = x * x
%2 = fmul double %0, 2.000000e+00 # %2 = 2 * x
%3 = fmul double %1, 3.000000e+00 # %3 = 3 * x^2
%4 = fmul double %2, 3.000000e+00 # %4 = 3 * 2 * x
%5 = fmul double %3, 0.000000e+00 # %5 = 0 * 3 * x^2 # <= 🤔
%6 = fadd double %4, %5 # %6 = 6 * x
ret double %6 # %1, %3 and %5 are not required!
}
```

Is this expected? Can this be optimized? I think so since the above does not happen if `x`

is an `Int64`

:

```
julia> @code_llvm debuginfo=:none ∂²f(1)
define i64 @"julia_\E2\88\82\C2\B2f_17357"(i64) {
top:
%1 = mul i64 %0, 6
ret i64 %1
}
```