循环不变代码外提

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

示例

假设有如下代码:

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
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.