我使用以下级别0和3优化编译了以下函数g++
版本4.7.2 20120921
:
double function1(double a, double b)
{
return (a+b)*(a+b);
}
拆卸0级优化版本可提供:
0000000000000000 <_Z9function1dd>:
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
4: f2 0f 11 45 f8 movsd QWORD PTR [rbp-0x8],xmm0
9: f2 0f 11 4d f0 movsd QWORD PTR [rbp-0x10],xmm1
e: f2 0f 10 45 f8 movsd xmm0,QWORD PTR [rbp-0x8]
13: 66 0f 28 c8 movapd xmm1,xmm0
17: f2 0f 58 4d f0 addsd xmm1,QWORD PTR [rbp-0x10]
1c: f2 0f 10 45 f8 movsd xmm0,QWORD PTR [rbp-0x8]
21: f2 0f 58 45 f0 addsd xmm0,QWORD PTR [rbp-0x10]
26: f2 0f 59 c1 mulsd xmm0,xmm1
2a: f2 0f 11 45 e8 movsd QWORD PTR [rbp-0x18],xmm0
2f: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
33: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
37: f2 0f 10 45 e8 movsd xmm0,QWORD PTR [rbp-0x18]
3c: 5d pop rbp
3d: c3 ret
3级优化提供:
0000000000000000 <_Z9function1dd>:
0: f2 0f 58 c1 addsd xmm0,xmm1
4: f2 0f 59 c0 mulsd xmm0,xmm0
8: c3 ret
在未优化的版本中,为什么代码需要做很多额外的工作?具体来说,是什么导致
mulsd
之后出现4条指令?他们所做的只是将xmm0
移至内存,从内存移至rax
,然后移至内存,再移至xmm0
。 最佳答案
这是编译器认为正在执行的操作的一种可能 View 。
非优化的编译器会从局部角度审视事物。它没有展望下一步将要做什么。它可能使用了非常有限的一组操作。例如,似乎将其某些工作传输限制在堆栈和xmm0之间,或堆栈和rax之间。
千篇一律的方法的另一个方面是,如果某些情况下需要做某事,那么它往往会无时无刻不在完成。特别是,某些功能需要足够的寄存器来要求将参数和中间结果保存到堆栈中。优化编译器仅在必要时这样做。非优化编译器无条件地执行此操作。
0000000000000000 <_Z9function1dd>:
// Push the stack
0: 55 push rbp
1: 48 89 e5 mov rbp,rsp
// Save the parameters to stack temporaries
4: f2 0f 11 45 f8 movsd QWORD PTR [rbp-0x8],xmm0
9: f2 0f 11 4d f0 movsd QWORD PTR [rbp-0x10],xmm1
// Load the temporary representing a into register xmm1, via xmm0
e: f2 0f 10 45 f8 movsd xmm0,QWORD PTR [rbp-0x8]
13: 66 0f 28 c8 movapd xmm1,xmm0
// Add the temporary representing b leaving (a+b) in xmm1
17: f2 0f 58 4d f0 addsd xmm1,QWORD PTR [rbp-0x10]
// Load the temporary representing a into xmm0
1c: f2 0f 10 45 f8 movsd xmm0,QWORD PTR [rbp-0x8]
// Add the temporary representing b, leaving (a+b) in xmm0
21: f2 0f 58 45 f0 addsd xmm0,QWORD PTR [rbp-0x10]
// Multiply (a+b)*(a+b)
26: f2 0f 59 c1 mulsd xmm0,xmm1
// Store the multiply result in a stack temporary
2a: f2 0f 11 45 e8 movsd QWORD PTR [rbp-0x18],xmm0
// Load the return value into rax
2f: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
// Move the return value to xmm0 via a stack temporary
33: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
37: f2 0f 10 45 e8 movsd xmm0,QWORD PTR [rbp-0x18]
// and return
3c: 5d pop rbp
3d: c3 ret