Skip to content

Branchless ==# is compiled to branchy code

This started as a comment on #6135 (closed), but Jan Stolarek requested that I file a separate report. The branchless tests <#, >#, <=#, and >=#, and their wordy cousins, seem to work properly, but ==# and eqWord# don't. If I write

foo x = tagToEnum# ((x <# 3#) `orI#` (x ># 100#) `orI#`
                    (x ==# 12#) `orI#` (x ==# 15#))

I get (in 7.8.3 and in 7.9)

IsDigit.foo :: GHC.Prim.Int# -> GHC.Types.Bool
[GblId,
 Arity=1,
 Caf=NoCafRefs,
 Str=DmdType <S,1*U>,
 Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
         WorkFree=True, Expandable=True,
         Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=False)
         Tmpl= \ (x3_a2if [Occ=Once!] :: GHC.Prim.Int#) ->
                 case x3_a2if of wild_Xe {
                   __DEFAULT ->
                     GHC.Prim.tagToEnum#
                       @ GHC.Types.Bool
                       (GHC.Prim.orI# (GHC.Prim.<# wild_Xe 3) (GHC.Prim.># wild_Xe 100));
                   12 -> GHC.Types.True;
                   15 -> GHC.Types.True
                 }}]
IsDigit.foo =
  \ (x3_a2if :: GHC.Prim.Int#) ->
    case x3_a2if of wild_Xe {
      __DEFAULT ->
        GHC.Prim.tagToEnum#
          @ GHC.Types.Bool
          (GHC.Prim.orI# (GHC.Prim.<# wild_Xe 3) (GHC.Prim.># wild_Xe 100));
      12 -> GHC.Types.True;
      15 -> GHC.Types.True

and branching assembly to match. Jan Stolarek indicates this is probably a problem with constant folding.

Trac metadata
Trac field Value
Version 7.9
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC jstolarek
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information