当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - IR LLVM form vs assembly

问题描述:

https://idea.popcount.org/2013-07-24-ir-is-better-than-assembly/

Hi,

I grasp an idea of IR but one issue is not-clear.

Why is the IR better than an assembly? I've read what author said about it and I see that generally it is higher in the sense of abstraction. It allows a type-control. But, I would like ask about something else:

Especially, LLVM IR is a kind of intermediate form. But, why the optimization is conducted on that form? Is it impossible to optimize assembly output? Indeed, it is possible? So, why the LLVM IR is so great to use it in that context? I suppose that analysis of the IR form is just simpler and "more possible".

网友答案:

This is how I read your question: Why compilers don't do a quick translation to a non-optimized assembly and then optimize that.

Optimizing assembly imposes some serious restrictions. As Chris Stathis mentioned, an assembly optimizer will be limited to one platform. The IR optimizer is independent of the platform.

Also there are too many hardware specific details encoded in assembly. Compiler optimizations are already difficult, so there is no reason to make that problem more complicated by adding irrelevant hardware details to them. For example in assembly code, you have a limited number of registers of specific size. In IR you can use as many registers as you want. There is no reason to make your optimization algorithm more complicated by forcing it to keep track of hardware registers.

LLVM IR is has some advantages to other compiler IRs that I know about. One important advantage is that LLVM IR is in SSA form. That means a variable can be defined only once and its value can never change. This simplifies compiler optimizations to a very significant degree.

By looking at LLVM IR manual it may not be evident that it is in SSA form. But once you look at the data structures used to represent the IR in the LLVM library, you will see that you cannot write non-SSA code in LLVM IR.

分享给朋友:
您可能感兴趣的文章:
随机阅读: