logoalt Hacker News

ngruhntoday at 7:22 AM2 repliesview on HN

It can be used in static analysis or type checking. E.g.

    if (x >= 0) {
      x += 10
      if (x =< 9) {
        // unreachable 
      }
    }
By maintaining an interval of possible values of x, you can detect the unreachable branch, because the interval becomes empty:

    initial: [-oo, oo]
    x >= 0 : [0, oo]
    x += 10: [10, oo]
    x =< 9 : [10, 9] (empty)

Replies

Oberdiahtoday at 9:39 AM

I’m working on a static analyser at the moment that does this, and the inferences that can be made just from the information of intervals is quite impressive. One thing you run into pretty quickly though in a lot of languages is integer overflow ruining your day - in your example above the commented section is reachable for signed ints that support overflow and that adds a whole other layer of complexity to things.

thekomatoday at 9:18 AM

We recently implemented this idea in an LLVM optimisation pass based on value-range information from sensor datasheets [1].

[1]: https://dl.acm.org/doi/pdf/10.1145/3640537.3641576