简体   繁体   English

寄存器分配后的常量传播

[英]constant propagation after register allocation

I am wondering why it is not advisable to do constant propagation after register allocation (RA) as well. 我想知道为什么在寄存器分配(RA)之后进行常量传播也是不可取的。 After several optimization passes (post RA) there is scope for peephole optimizations like constant propagation/dead-code elimination etc. I can think of only two reasons, 经过几次优化过程(后RA),有窥孔优化的余地,如恒定传播/死码消除等。我只能想到两个原因,

  1. that these optimizations are easy to do on SSA form. 这些优化很容易在SSA表单上进行。
  2. peephole opt. 窥视孔选择。 post RA will result in increased compilation time. RA后会导致编译时间增加。

Are there any other reasons? 还有其他原因吗?

If it is okay to perform peephole opt. 如果可以执行窥视孔选择。 post RA then what should be the data structures/algorithms (any paper, reference etc. would be helpful). 然后发布RA应该是什么数据结构/算法(任何论文,参考等都会有所帮助)。

EDIT: in response to 500 - Internal Server Error's comment. 编辑:响应500 - 内部服务器错误的评论。 After optimization passes like phi-elimination (which is, eg, in llvm-clang, merged with register allocation), global scheduling like: pulling up instructions to parent basic blocks etc. 在优化传递之后,如phi-elimination(例如,在llvm-clang中,与寄存器分配合并),全局调度如:将指令拉到父基本块等。

EDIT2: EDIT2:

通行证

In the example shown in figure: The register allocator figures out that v1 and v2 has the same value and hence, assigns same register (r1) to them. 在图中所示的示例中:寄存器分配器指出v1和v2具有相同的值,因此为它们分配相同的寄存器(r1)。 After register allocation a common sub-expression elimination pass can eliminate r2 = r1 from basic block #4. 在寄存器分配之后,公共子表达式消除通道可以从基本块#4中消除r2 = r1

See: Constant folding 请参阅: 恒定折叠

The example given, 给出的例子,

 int x = 14;
 int y = 7 - x / 2;
 return y * (28 / x + 2);

The value x is completely unused after the constant folding. 在常量折叠之后,值x完全未使用。 If RA was used first, it would create registers for x . 如果首先使用RA ,它将为x创建寄存器。 So there is a chance for some pruning before running the RA phase, even if the results are the same. 因此,即使结果相同,也有可能在运行RA阶段之前进行一些修剪。 If there are even more variables, spills could be avoided. 如果有更多变量,可以避免溢出。 These would be difficult to undo after the registers are allocated. 分配寄存器后,这些很难撤消。

I think that instead of constant propagation you are thinking of strength reduction ? 我认为你不是在不断传播,而是在考虑减少力量 This is more in the spirit of peephole optimizations ; 这更符合窥视孔优化的精神; or I don't understand what you mean by constant propagation during the peephole phase, which is usually a back-end portion. 或者我不明白你在窥视孔期间不断传播的意思,这通常是一个后端部分。

Any Constant folding that was applied before register allocation should be identical, unless variables have been made constant or code was found dead; 寄存器分配之前应用的任何常量折叠应该是相同的,除非变量已经变为常量或者代码被发现死亡; Ie the CFG has changed. CFG已经改变。 per Mystical 每个神秘的

SSA Elimination after Register Allocation describes the LLVM structure. 寄存器分配后的SSA消除描述了LLVM结构。 I believe that the SSA could be annotated with constant values so that on Phi elimination unneeded moves can be avoided. 我相信SSA可以用常数值注释,这样就可以避免Phi消除不需要的动作。 This is probably an artifact of the SSA elimination after RA and other compilers won't be experiencing this issue. 在RA和其他编译器不会遇到此问题之后 ,这可能是SSA消除的工件。 A separate pass will slow compilation, so addressing the issue in existing passes would be better. 单独的传递将减慢编译速度,因此解决现有传递中的问题会更好。 I think the following code illustrates the issue, 我认为以下代码说明了这个问题,

 int foo(int a, int b)
 {
    int c;
    if(a > 0)
        c = 7;
    else
        c = a * b + 10;
    return a + c;
 }

Upon phi elimination the code looks like, 消除phi代码看起来像,

 int foo(int a, int b)
 {
    int c;
    if(a > 0) {
        c = 7;
        return a + c;  /* Should reduce to "a+7" */
    } else {
        c = a * b + 10;
        return a + c;
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM