Two Optimization Blockers Limiting the Compiler to Generate Optimized Code

Modern compilers employ sophisticated algorithms to determine what values are computed in a program and how they are used[1]. However, compilers apply only safe optimizations to a program, and constraining the compiler to perform only safe optimizations eliminates possible sources of undesired run-time behavior.  But it also means that the programmer must make more of an effort to write programs in a way that the compiler can then transform into efficient machine-level code[1].

      A major optimization blocker is due to memory aliasing. Consider the following two procedures:

void zyb1(int *xp, int *yp)
{
    *xp += *yp;
    *xp += *yp;
}

void zyb2(int *xp, int *yp)
{
    *xp += 2* *yp;
}
      At first glance, both procedures seem to have identical behavior. And function zyb2 is more efficient. It requires only three memory references(read *xp, read *yp, write *xp), whereas zyb1 requires six. Hence, if a compiler is given procedure zyb1 to compile, one might think it could generate more efficient code based on the computations performed by zyb2.

      However, consider the case in which xp and yp are equal. The result of zyb1 will be that the value at xp will be increased by a factor of 4. On the other hand, the result of zyb2 will be that the value at xp will be increased by a factor of 3. The compiler therefore cannot generate code in the style of zyb2 as an optimized version of zyb1.

      A second optimization blocker is on account of function calls. As an example, consider the following two procedures:

int f();

int func1() {
    return f() + f() + f() + f();
}

int func2() {
    return 4*f();
}
It might seem at first that both compute the same result, but with func2 calling f only once, whereas func1 calls it four times. It is tempting to generate code in the style of func2 when given func1 as the source.

      But consider the following code for f:

int counter = 0;

int f() {    
    return counter++;
}

This function has a side effect—it modifies some part of the global program state[1]. In particular, a call to func1 would return 0+1+2+3=6, whereas a call to func2 yields 4·0=0.

      Most compilers do not try to determine whether a function is free of side effects. Instead, the compiler assumes the worst case and leaves function calls intact[1].

References

[1] Randal E. Bryant, David R. O'Hallaron(2011). COMPUTER SYSTEMS A Programmer's Perspective (Second Edition).Beijing: China Machine Press.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值