帮助GHC在一个常量上unbox一个Int

jbe*_*man 7 optimization unboxing haskell ghc

这是一个测试程序:

main = do
    n <- fmap read $ getLine :: IO Int
    if (999999 == n) then putStrLn "equal" else return ()
Run Code Online (Sandbox Code Playgroud)

以下是编译时的相关核心位ghc --make -O2 -ddump-to-file -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques -ddump-core-stats -ddump-inlinings:

case readEither6 @ Int (run @ Int main3 ipv1) of _ [Occ=Dead] {
  [] -> case error @ Int readEither4 of wild1 { };
  : x ds1 ->
    case ds1 of _ [Occ=Dead] {
      [] ->
        case x of _ [Occ=Dead] { I# ipv2 ->
        case ipv2 of _ [Occ=Dead] {
          __DEFAULT -> (# ipv, () #);
          999999 -> hPutStr2 stdout main2 True ipv
        }
        };
      : ipv2 ipv3 -> case error @ Int readEither2 of wild2 { }
    }
Run Code Online (Sandbox Code Playgroud)

我想知道文字999999上的案例匹配是否真的是最有效的事情,如果不是,我怎么能鼓励GHC把这变成一个叫==#什么或什么?

(我的实际应用更多参与)

jam*_*idh 8

在我的Ubuntu框中,相关的核心代码编译到此

406168: 48 81 7b 07 3f 42 0f    cmpq   $0xf423f,0x7(%rbx)
40616f: 00 
406170: 75 28                   jne    40619a <c4fz_info+0x32>
406172: bf 52 e3 6d 00          mov    $0x6de352,%edi
406177: be 90 65 6d 00          mov    $0x6d6590,%esi
40617c: 41 be 18 6a 6d 00       mov    $0x6d6a18,%r14d
406182: 48 83 c5 08             add    $0x8,%rbp
406186: e9 65 3c 00 00          jmpq   409df0 <base_GHCziIOziHandleziText_hPutStr2_info>
Run Code Online (Sandbox Code Playgroud)

第一行到第三行基本上等同于c代码

if (variable == 999999) { ....
Run Code Online (Sandbox Code Playgroud)

这是最佳的,你可以获得.