用什么命令可以防止一段代码被优化掉?

JiuJiuMeir 2012-11-08 03:50:13
例如:

INT32 *restrict z;
INT32 *restrict c;
...
startIdx = 20208;
z = (INT32*)&DMRS_LUT[startIdx];

startIdx = 4800;
c = (INT32*)&cycShiftLUT[startIdx];
...

对于倒数四行,GCC 优化设置为O3时,前两行总是被优化掉;如果将四行代码的前两行和后两行互换位置,还是前两行被优化掉
...全文
5087 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
t_bug1225 2012-11-14
  • 打赏
  • 举报
回复
引用《程序员自我修养》中的一句话: volatile能够阻止编译器调整顺序,但是也无法阻止CPU动态调度换序。
JiuJiuMeir 2012-11-09
  • 打赏
  • 举报
回复
引用 8 楼 turingo 的回复:
用volatile修饰是可以防止gcc O3优化的,下面给一段测试代码: C/C++ code 123456789101112131415 #include <stdio.h> int main(int argc, char* argv[]) { volatile int a; /* int a; */ a = 80; a……
如果a是传入的参数地址,这个volatile该加在哪里?
g313105 2012-11-09
  • 打赏
  • 举报
回复
我是小菜,不理解啊
JiuJiuMeir 2012-11-09
  • 打赏
  • 举报
回复
引用 20 楼 mujiok2003 的回复:
In the C programming language, as of the C99 standard, restrict is a keyword that can be used in pointer declarations. The restrict keyword is a declaration of intent given by the programmer to the co……
这个有用,谢谢
jiandingzhe 2012-11-09
  • 打赏
  • 举报
回复
引用 16 楼 luciferisnotsatan 的回复:
引用 15 楼 JiuJiuMeir 的回复:引用 14 楼 luciferisnotsatan 的回复:restrcict能达到类似下面的效果。这个本来就是用于优化的关键字,不要优化,就不要用这个。 int *restrict p = (int *)malloc(sizeof(int)); .... *p += 10; .... // 不管中间有什么……
有用。非常简单的例子:

void vec4_add(float* a, float* b) {
    a[0] += b[0];
    a[1] += b[1];
    a[2] += b[2];
    a[3] += b[3];
}
如果不带restrict,编译器不能确保a和b指向的空间是不是有重叠,就会编译成4次加载内容、执行浮点加法、传回内容,不做任何优化。而如果a和b都restrict,编译器会优化为两次SIMD读取、一次SIMD加、一次SIMD传回内存,快4倍。
gaochizhen33 2012-11-09
  • 打赏
  • 举报
回复
volatile 肯定OK啊。 去掉 restrict 关键字。
mujiok2003 2012-11-08
  • 打赏
  • 举报
回复
In the C programming language, as of the C99 standard, restrict is a keyword that can be used in pointer declarations. The restrict keyword is a declaration of intent given by the programmer to the compiler. It says that for the lifetime of the pointer, only it or a value directly derived from it (such as ​pointer + 1​) will be used to access the object to which it points. This limits the effects of pointer aliasing, aiding caching optimizations. If the declaration of intent is not followed and the object is accessed by an independent pointer, this will result in undefined behavior.
mujiok2003 2012-11-08
  • 打赏
  • 举报
回复
引用 18 楼 JiuJiuMeir 的回复:
引用 16 楼 luciferisnotsatan 的回复:引用 15 楼 JiuJiuMeir 的回复: 引用 14 楼 luciferisnotsatan 的回复:restrcict能达到类似下面的效果。这个本来就是用于优化的关键字,不要优化,就不要用这个。 int *restrict p = (int *)malloc(sizeof(int)); ..……
维基百科
JiuJiuMeir 2012-11-08
  • 打赏
  • 举报
回复
引用 16 楼 luciferisnotsatan 的回复:
引用 15 楼 JiuJiuMeir 的回复: 引用 14 楼 luciferisnotsatan 的回复:restrcict能达到类似下面的效果。这个本来就是用于优化的关键字,不要优化,就不要用这个。 int *restrict p = (int *)malloc(sizeof(int)); .... *p += 10; .... // 不管中间有什么代码 *p += 5……
不知道哪里有深入讲讲restrict的文章,希望能再了解了解
mujiok2003 2012-11-08
  • 打赏
  • 举报
回复
试试用函数指针:
INT32* at(INT32* array, int index)
{
  return array + index;
}
//startIdx = 20208;
//z = (INT32*)&DMRS_LUT[startIdx];
INT32* (*pf)(INT32*, int) = &a;
z = (*pf)(DMRS_LUT, 20208);
luciferisnotsatan 2012-11-08
  • 打赏
  • 举报
回复
引用 15 楼 JiuJiuMeir 的回复:
引用 14 楼 luciferisnotsatan 的回复:restrcict能达到类似下面的效果。这个本来就是用于优化的关键字,不要优化,就不要用这个。 int *restrict p = (int *)malloc(sizeof(int)); .... *p += 10; .... // 不管中间有什么代码 *p += 5; // 编译器会放心大……
其实感觉C99加这个关键字没用。如果使用restrcit,而中途其他指针改变了该处内存的值。编译器没检测出来,代码不就出问题了(检测工作交给程序员来做,这不科学)。反之,如果编译器有能力检测出没其他指针改变该处内存,那么还要restrict干嘛呢,默认都当有restrict,没变就优化,变了不优化?
JiuJiuMeir 2012-11-08
  • 打赏
  • 举报
回复
引用 14 楼 luciferisnotsatan 的回复:
restrcict能达到类似下面的效果。这个本来就是用于优化的关键字,不要优化,就不要用这个。 int *restrict p = (int *)malloc(sizeof(int)); .... *p += 10; .... // 不管中间有什么代码 *p += 5; // 编译器会放心大胆地把这两行加优化成 *p += 15这么一句。
原来是这样,编译器胆子还真不小,谢谢指点
luciferisnotsatan 2012-11-08
  • 打赏
  • 举报
回复
restrcict能达到类似下面的效果。这个本来就是用于优化的关键字,不要优化,就不要用这个。 int *restrict p = (int *)malloc(sizeof(int)); .... *p += 10; .... // 不管中间有什么代码 *p += 5; // 编译器会放心大胆地把这两行加优化成 *p += 15这么一句。
JiuJiuMeir 2012-11-08
  • 打赏
  • 举报
回复
引用 10 楼 luciferisnotsatan 的回复:
之前没注意上面的restrict,用restrict修饰,然后再改变指针指向,这个关键字是这么用的?
restrict我了解不深入,但是看到过这么用的
luciferisnotsatan 2012-11-08
  • 打赏
  • 举报
回复
restrict本来就是做优化的,说明restrict修饰的指针是存取对象的唯一方法。代码其他处不会有别的指针对那对象作修改
JiuJiuMeir 2012-11-08
  • 打赏
  • 举报
回复
引用 8 楼 turingo 的回复:
用volatile修饰是可以防止gcc O3优化的,下面给一段测试代码: C/C++ code 123456789101112131415 #include <stdio.h> int main(int argc, char* argv[]) { volatile int a; /* int a; */ a = 80; a……
嗯,是呀,我再试试
luciferisnotsatan 2012-11-08
  • 打赏
  • 举报
回复
之前没注意上面的restrict,用restrict修饰,然后再改变指针指向,这个关键字是这么用的?
ysj5970689 2012-11-08
  • 打赏
  • 举报
回复
奥 这怎么能积分
图灵狗 2012-11-08
  • 打赏
  • 举报
回复
用volatile修饰是可以防止gcc O3优化的,下面给一段测试代码:

#include <stdio.h>

int main(int argc, char* argv[])
{
	volatile int a;
	/* int a; */
	
	a = 80;
	a = 51;

	printf("%d\n", a);

	return 0;
}
当用int a时:

gcc 4.c -S
	movl	$80, 28(%esp)
	movl	$51, 28(%esp)
gcc 4.c -O3 -S
	movl	$51, 28(%esp)
当用volatile int a时:

gcc 4.c -O3 -S
	movl	$80, 28(%esp)
	movl	$51, 28(%esp)
JiuJiuMeir 2012-11-08
  • 打赏
  • 举报
回复
引用 6 楼 luciferisnotsatan 的回复:
z后面没用过?不然优化掉后,z是什么值? c相同
z后面要用,z和c都是用于计算下一个变量的。z就是没有被赋值,地址还是0x00000000
加载更多回复(6)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧