# Why doesn't \`x \< zero(x)\` for an unsigned integer lead to dead-code elimination?

**URL:** <https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752>\
**Category:** General Usage\
**Tags:** integer\
**Created:** [September 11, 2023, 6:24pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752 "2023-09-11T18:24:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 11, 2023, 6:24pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/1 "2023-09-11T18:24:20Z")

</div>

```julia
julia> isnegative(x) = x < zero(x)
isnegative (generic function with 1 method)

julia> @code_typed isnegative(UInt(2))
CodeInfo(
1 ─ %1 = Base.ult_int(x, 0x0000000000000000)::Bool
└── return %1
) => Bool

```

I would have thought that this would be evaluated to `false` for an unsigned integer, although it’s a bit of a special case

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 11, 2023, 6:25pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/2 "2023-09-11T18:25:57Z")

</div>

LLVM is definitely smart enough to figure this one out but inference isn’t yet.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 12, 2023, 1:10pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/3 "2023-09-12T13:10:00Z")

</div>

If I understand your comment correctly,

```julia
julia> @code_llvm isnegative(UInt(1))
; @ REPL[1]:1 within `isnegative`
define i8 @julia_isnegative_267(i64 zeroext %0) #0 {
top:
  ret i8 0
}

```

shows that the function returns `0`, so this does evaluate to `false` in real code? The issue is with `@code_typed`, then?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 12, 2023, 1:34pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/4 "2023-09-12T13:34:28Z")

</div>

roughly yes. To be specific, it’s not an issue with `@code_typed`, but with the typed code. Julia goes through several stages of optimization, and `@code_typed` is correctly showing that this optimization is not being done by julia (but it is getting done by LLVM). It would be better if Julia was smart enough to prove this because deleting code early in the compilation pipeline tends to speed up compilation (because the rest of compilation then has less code to work with).

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [September 12, 2023, 2:58pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/5 "2023-09-12T14:58:37Z")

</div>

> [@Oscar\_Smith](#):
>
> It would be better if Julia was smart enough to prove this because deleting code early in the compilation pipeline tends to speed up compilation (because the rest of compilation then has less code to work with).

Also might allow type inference to use it, if you have a `if isnegative(x)` branch where it returns different types on both sides of the branch.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 13, 2023, 2:40pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/6 "2023-09-13T14:40:34Z")

</div>

It does constant-fold(?). So change/clarify the title?

If I understand @Oscar_Smith correctly, that happens, just late in the compilation pipeline. I.e. we can rely on it, and that it would most likely inline, even without:

```julia
julia> @inline isnegative(x) = x < zero(x)

```

What you mentioned would only speed up compilation, not runtime use of code after that.

The `ret i8 0` that @code\_llvm doesn’t seem bad enough, but @code\_native shows more verbose with e.g. unneeded `movq	%rsp, %rbp`. You would get that in your code if NOT inlined, but otherwise not.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [September 13, 2023, 2:51pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/7 "2023-09-13T14:51:00Z")

</div>

My intended use case was the one suggested above, where `isnegative` is used in an `if-else` with different types returned in each branch. I was hoping that this may be evaluated in a type-stable manner. What wording would you recommend for the title?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 13, 2023, 2:53pm UTC](https://discourse.julialang.org/t/why-doesnt-x-zero-x-for-an-unsigned-integer-lead-to-dead-code-elimination/103752/8 "2023-09-13T14:53:44Z")

</div>

I believe you’re looking for dead code elimination. Constant folding isn’t valid here because `x` isn’t known.
