LLVM and the future of optimization

后端 未结 7 1848
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 07:11

I realize that LLVM has a long way to go, but theoretically, can the optimizations that are in GCC/ICC/etc. for individual languages be applied to LLVM byte code? If so, does t

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-19 07:57

    In general, no.

    For example, in Haskell a common optimization is strictness analysis, which allows the compiler to determine which variables are always in head-normal form and therefore can be forced + inlined without changing program semantics. This is not possible with LLVM.

    Explanation: In Haskell, a function (Int, Int) -> Int is more or less equivalent to the type in C:

    typedef int (*returns_int)();
    struct pair { returns_int first, second};
    typedef struct pair *(*returns_pair)();
    
    int function(returns_pair arg);
    

    The compiler can analyze function and determine that it always evaluates its argument and always extracts the contents, transforming the function into this:

    int function(int x, int y); // note that it takes *two* arguments now
    

    This is well beyond the capability of LLVM. Maybe, in the future, with some really heavy interprocedural optimization... but realistically speaking, this will not happen in the foreseeable future.

    Example 2: There are Java VMs which can transform virtual function calls into direct function calls. However, this isn't something that LLVM can do — because this transformation must be undone dynamically if another class is loaded which implements the same interface.

    In general, when you compile a program to LLVM you lose much of the semantic information about the original program. LLVM bytecode is capable of representing any code, but its type system is fairly limited — and your choice of type system affects what optimizations you can do.

提交回复
热议问题