[erlang-bugs] R14B04 and R15A: Possible bug when compiling code using binary expressions with hipe

Björn Gustavsson <>
Sun Dec 11 10:42:58 CET 2011

On Sat, Dec 10, 2011 at 10:14 AM, Kostis Sagonas <> wrote:
> However, the native compiler crash should be fixed.  It really boils down to
> the following code:
>  -module(cowboy).
>  -export([f/1]).
>  f(<<42, _/bits>> = B) -> B.
> for which the BEAM compiler produces:
>  label 1:
>    {func_info,{atom,cowboy},{atom,f},1}
>  label 2:
>    {test,bs_start_match2,{f,1},[{x,0},1,0,{x,1}]}
>    {test,bs_match_string,{f,1},[{x,1},8,<<"*">>]}
>    return
> This is obviously correct BEAM code, but I think it's suboptimal. I would
> have expected some lighter-weight instruction instead of a bs_match_string
> one, which presumably involves some loop in its implementation.  Bjorn what
> say you?

It is not obvious to me without measuring.

If you don't use bs_match_string, you will need one more

 label 1:
 label 2:

That is, one instruction to retrieve 8 bits from the binary and one
instruction to compare that it is equal to 42.

Provided that the binary is aligned on a byte boundary, bs_match_string
will use memcmp(). I think that GCC will inline calls to memcmp(), but
I have no idea how efficient it will be for a size value of one.


Björn Gustavsson, Erlang/OTP, Ericsson AB

More information about the erlang-bugs mailing list