循环不变代码外提

维基百科,自由的百科全书

循环不变代码外提(英語:loop-invariant code motion,缩写LICM)在计算机编程中是指将循环不变的语句或表达式移到循环体之外,而不改变程序的语义。循环不变代码外提在英文中又被称为hoistingscalar promotion,是编译器中常见的优化方法。

示例[编辑]

假设有如下代码:

for (int i = 0; i < n; i++) {
    x = y + z;
    a[i] = 6 * i + x * x;
}

其中x = y + zx * x这两步运算是循环不变的,会在优化中被移出循环体之外。经LICM优化后的代码为:

x = y + z;
t1 = x * x;
for (int i = 0; i < n; i++) {
    a[i] = 6 * i + t1;
}

参见[编辑]

参考文献[编辑]

  • Aho, Alfred V.; Sethi, Ravi; & Ullman, Jeffrey D. (1986). Compilers: Principles, Techniques, and Tools. Addison Wesley. ISBN 0-201-10088-6.
  • Compiler Optimizations — Hoisting