我想检查boost::variant在我的代码中应用的程序集输出,以便查看哪些中间调用被优化掉了.
当我编译以下示例(使用GCC 5.3 g++ -O3 -std=c++14 -S)时,似乎编译器优化了所有内容并直接返回100:
(...)
main:
.LFB9320:
.cfi_startproc
movl $100, %eax
ret
.cfi_endproc
(...)
Run Code Online (Sandbox Code Playgroud)
#include <boost/variant.hpp>
struct Foo
{
int get() { return 100; }
};
struct Bar
{
int get() { return 999; }
};
using Variant = boost::variant<Foo, Bar>;
int run(Variant v)
{
return boost::apply_visitor([](auto& x){return x.get();}, v);
}
int main()
{
Foo f;
return run(f);
}
Run Code Online (Sandbox Code Playgroud)
但是,完整的程序集输出包含的内容远远超过上面的摘录,对我而言,它看起来永远不会被调用.有没有办法告诉GCC/clang删除所有"噪音"并输出程序运行时实际调用的内容?
完整装配输出:
.file "main1.cpp"
.section .rodata.str1.8,"aMS",@progbits,1
.align 8
.LC0:
.string "/opt/boost/include/boost/variant/detail/forced_return.hpp"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1: …Run Code Online (Sandbox Code Playgroud) (这个问题最初是关于CVTSI2SD指令和我认为它在Pentium M CPU上不起作用的事实,但实际上是因为我使用的是自定义操作系统,我需要手动启用SSE.)
我有一个Pentium M CPU和一个自定义操作系统,到目前为止没有使用SSE指令,但我现在需要使用它们.
尝试执行任何SSE指令会导致中断6,非法操作码(在Linux中会导致a SIGILL,但这不是Linux),也在英特尔架构软件开发人员手册中提到(我现在将其称为IASDM) as #UD - 无效操作码(UnDefined Opcode).
编辑:彼得·科德斯实际上确定了正确的原因,并指出了解决方案,我在下面继续:
如果您运行的是不支持在上下文切换时保存XMM regs的古老操作系统,则不会设置其中一个机器控制寄存器中的SSE使能位.
实际上,IASDM提到了这一点:
如果操作系统没有为SSE提供足够的系统级支持,则执行SSE或SSE2指令也可以生成#UD.
Peter Cordes向我指出了SSE OSDev wiki,它描述了如何通过写入CR0和CR4控制寄存器来启用SSE :
clear the CR0.EM bit (bit 2) [ CR0 &= ~(1 << 2) ]
set the CR0.MP bit (bit 1) [ CR0 |= (1 << 1) ]
set the CR4.OSFXSR bit (bit 9) [ CR4 |= (1 << 9) ]
set the CR4.OSXMMEXCPT bit (bit …Run Code Online (Sandbox Code Playgroud)