相关疑难解决方法(0)

未使用的功能会被优化吗?

一个相当简单的问题......这些天的编译器倾向于进行大量的优化.它们是否也从最终输出中删除未使用的函数?

c c++ compiler-construction

49
推荐指数
5
解决办法
3万
查看次数

全局const优化和符号插入

我正在尝试使用gcc和clang来查看它们是否可以优化

#define SCOPE static
SCOPE const struct wrap_ { const int x; } ptr = { 42 /*==0x2a*/ };
SCOPE struct wrap { const struct wrap_ *ptr; } const w = { &ptr };
int ret_global(void) { return w.ptr->x; }
Run Code Online (Sandbox Code Playgroud)

返回一个中间常量.

事实证明他们可以:

0000000000000010 <ret_global>:
   10:  b8 2a 00 00 00          mov    $0x2a,%eax
   15:  c3                      retq   
Run Code Online (Sandbox Code Playgroud)

但令人惊讶的是,移除静态会产生相同的装配输出.这让我很好奇,因为如果全局不是static它应该是可插入的并且用中间体替换引用应该防止全局变量上的位置.

确实如此:

#!/bin/sh -eu
: ${CC:=gcc}
cat > lib.c <<EOF
int ret_42(void) { return 42; }

#define SCOPE 
SCOPE const struct wrap_ { …
Run Code Online (Sandbox Code Playgroud)

c compiler-construction optimization linker elf

6
推荐指数
1
解决办法
126
查看次数

标签 统计

c ×2

compiler-construction ×2

c++ ×1

elf ×1

linker ×1

optimization ×1