GCC与-fno-builtin似乎不起作用 [英] GCC with -fno-builtin does not seem to work

查看:949
本文介绍了GCC与-fno-builtin似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较GCC内建函数 memcpy 与libc中的一个函数。但是, -fno-builtin -fno-builtin-memcpy 的所有迭代似乎都被忽略。

  // g ++ -O3 foo.cpp -S或
// g ++ -O3 -fno-builtin foo.cpp -S
#include< string.h>
int main(){
volatile int n = 1000;
// int n = 1000;
浮动* x =新浮动[1000];
float * y =新浮点数[1000];
memcpy(y,x,sizeof(float)* n);
// __ builtin_memcpy(y,x,sizeof(float)* n);
}

我发现如果 n <上面的源代码中的code>不是易变的,那么它内联代码。然而,当 n 被设置为volatile时,它会调用 __ memcpy_chk ://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/libc---memcpy-chk-1.htmlrel =nofollow>带缓冲区溢出检查的memcpy 。如果 n 是易失性的,而我调用 __ builtin_memcpy ,那么它会调用 memcpy n ,内置代码才会生成。时间和 -fno-builtin 是无用的。我正在使用GCC 4.8.2。



-fno-builtin 过时了吗?即使在编译时知道 n ,是否有办法从C库中调用GCC调用 memcpy


解决方案

-fno-builtin -fno- builtin-memcpy 都会影响你用gcc 4.9.1所期望的效果。这可能只是gcc 4.8.2中的一个bug;这种选择的特殊组合并未被广泛使用。 -freestanding 是一个相关的开关,它可能在4.8.2中有效。

注意,编译器是在它的权利,以优化您的程序下降到

  int main(){return 0; } 

被调用时没有 -fno-builtin(-memcpy) n volatile 时,即使因为它可以(原则上)证明程序作为一个整体要么没有可观察到的副作用,要么其行为是未定义的。 (当 n not volatile 时,不能有UB;如果<读取时,code> n 超出 [0,1000] 范围, volatile 告诉编译器它不能假定 n 具有程序写入的值。)


I would like to compare the GCC builtin function memcpy versus the one one from libc. However, all iterations of -fno-builtin or -fno-builtin-memcpy seem to be ignored.

//g++ -O3 foo.cpp -S or
//g++ -O3 -fno-builtin foo.cpp -S
#include <string.h>
int main() {
    volatile int n = 1000;
    //int n = 1000;
    float *x = new float[1000];
    float *y = new float[1000];
    memcpy(y,x,sizeof(float)*n);
    //__builtin_memcpy(y,x,sizeof(float)*n);    
}

What I have found is that if n in the source code above is not volatile then it inlines built-in code. However, when n is made volatile then it calls the function __memcpy_chk which is a version of memcpy with buffer overflow checking. If n is volatile and I instead call __builtin_memcpy then it calls memcpy.

So my conclusion so far is that the builtin code is only generated if n is known at compile time and that -fno-builtin is useless. I'm using GCC 4.8.2.

Is -fno-builtin obsolete? Is there a way to make GCC call memcpy from the C library even when n is known at compile time?

解决方案

-fno-builtin and -fno-builtin-memcpy both have the effect you expected with gcc 4.9.1. This is probably just a bug in gcc 4.8.2; this particular combination of options is not widely used. -ffreestanding is a related switch that may have the effect you want with 4.8.2.

Note that the compiler is within its rights to optimize your program down to

int main() { return 0; }

when invoked without -fno-builtin(-memcpy) or -ffreestanding, even when n is volatile, as it can (in principle) prove that the program as a whole either has no observable side effects, or its behavior is undefined. (When n is not volatile, there cannot be UB; the UB happens if n is outside the range [0, 1000] when read, and volatile tells the compiler it can't assume n has the value written to it by the program.)

这篇关于GCC与-fno-builtin似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆