从Haskell代码生成LLVM IR

Ada*_*dam 7 haskell llvm ghc llvm-ir

我的目标是使用不同语言(主要是C,C++,Obj-C和Haskell)的源代码,并告诉他们各种统计数据.(例如,变量,函数,内存分配,复杂性等)

LLVM似乎是一个完美的工具,因为我可以为这些语言生成bitcode,并且使用LLVM的可定制传递,我几乎可以做任何事情.对于C系列它工作正常,以C程序(test.c)为例:

#include <stdio.h>
int main( )
{
    int num1, num2, sum;
    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2); 
    sum = num1 + num2;
    printf("Sum: %d",sum);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后我跑:

clang -emit-llvm test.c -c -o test.bc
opt -load [MY AWESOME PASS] [ARGS]
Run Code Online (Sandbox Code Playgroud)

瞧,我几乎有我需要的一切:

1 instcount - Number of Add insts
 4 instcount - Number of Alloca insts
 3 instcount - Number of Call insts
 3 instcount - Number of Load insts
 1 instcount - Number of Ret insts
 2 instcount - Number of Store insts
 1 instcount - Number of basic blocks
14 instcount - Number of instructions (of all types)
12 instcount - Number of memory instructions
 1 instcount - Number of non-external functions
Run Code Online (Sandbox Code Playgroud)

我想用Haskell程序实现同样的目标.拿test.hs:

module Test where

quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
    where
        lesser = filter (< p) xs
        greater = filter (>= p) xs
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做

ghc -fllvm -keep-llvm-files -fforce-recomp test.hs
opt -load [MY AWESOME PASS] [ARGS]
Run Code Online (Sandbox Code Playgroud)

我得到了以下结果,这对我的目的来说似乎完全无用(在本文开头提到),因为对于这几行代码来说,它们显然不正确.我想这与GHC有关,因为新创建的.ll文件本身是52Kb,而.llC程序的文件只有2Kb.

31 instcount - Number of Add insts
  92 instcount - Number of Alloca insts
   2 instcount - Number of And insts
  30 instcount - Number of BitCast insts
  24 instcount - Number of Br insts
  22 instcount - Number of Call insts
 109 instcount - Number of GetElementPtr insts
  17 instcount - Number of ICmp insts
  54 instcount - Number of IntToPtr insts
 326 instcount - Number of Load insts
  65 instcount - Number of PtrToInt insts
  22 instcount - Number of Ret insts
 206 instcount - Number of Store insts
   8 instcount - Number of Sub insts
  46 instcount - Number of basic blocks
1008 instcount - Number of instructions (of all types)
 755 instcount - Number of memory instructions
  10 instcount - Number of non-external functions
Run Code Online (Sandbox Code Playgroud)

我的问题是我应该如何能够将Haskell代码与其他代码进行比较而不需要这么大的数字?它甚至可能吗?我应该继续使用GHC生成LLVM IR吗?我应该使用哪些其他工具?