clang优化器的一个问题
📜 历史文件已不具备现实意义
今天的一个偶然的发现。FreeBSD clang version 3.6.1 (tags/RELEASE_361/final 237755) 20150525。clang 3.8 2015/07/20 的版本同样有此问题。
之前, FreeBSD 上 strndup(3) 的实现是这样的:
| |
而 OpenBSD 上的实现,则是这样的:
| |
共 9 篇文章。
📜 历史文件已不具备现实意义
今天的一个偶然的发现。FreeBSD clang version 3.6.1 (tags/RELEASE_361/final 237755) 20150525。clang 3.8 2015/07/20 的版本同样有此问题。
之前, FreeBSD 上 strndup(3) 的实现是这样的:
| |
而 OpenBSD 上的实现,则是这样的:
| |
今天 强迫症 朱小瘦同学提到一个非常有意思的问题,一个32bit的无符号整数算术右移32个bit应该得多少?
我们知道算术右移一个bit相当于除2,所以一个32bit无符号整数除以 ,理论上,应该得0。
然而事实不是这样。测试显示在 x86 系统上,一个32bit无符号整数算术右移32个bit之后得到的是原数。例如下面这个测试程序:
| |
不启用任何优化的话,编译出来的程序得到的结果是:
5a5a5a5a更进一步,我们将上面的测试改写为:
| |
以前一直没注意过这个问题,今天在邮件列表看到 Matthew Flemming 发的邮件才知道实际上 C 标准并不保证 sizeof(void *) == sizeof(int(*)(void))。不过,几乎所有的现代系统上这个等式都是成立的。
LLVM 是 Illinois 大学发起的一个开源项目,它到底是什么呢?从字面上看,它是一个虚机系统,然而这又和之前为大家所熟知的 JVM 以及 .net Runtime 这样的虚机不同,它提供了一套中立的中间代码和编译基础设施,并围绕这些设施提供了一套全新的编译策略(使得优化能够在编译、连接、运行环境执行过程中,以及安装之后以有效的方式进行)和其他一些非常有意思的功能。
为什么这个项目很重要呢?对于普通的开发人员来说,LLVM计划提供了越来越多的可以使用、编译器以外的其他工具。例如代码静态检查工具 LLVM/Clang Static Analyzer,是一个 Clang 的子项目,能够使用同样的 Makefile 生成 HTML 格式的分析报告;而对关注编译技术的开发人员来说,LLVM提供了很多优点:
What is strict aliasing? To make the long thing short, it means that the compiler apply strictest aliasing rules applicable to the language and having these assumptions the optimizer can do further optimizations.
To quote gcc manual:
Finally the long-awaited gcc 3.4.0 RELEASE has been released. When will it be ported to FreeBSD’s main tree?
It’s our pleasure to see GCC 3.4 import to be listed in FreeBSD 5.3-R todo list. Unfortunatelly, gmake is now a required prerequisite when building GCC 3.4, this may slow down the import progress.
Today Tom Rhodes (trhodes@) has committed a massive patch against FreeBSD-HEAD, allowing Intel C/C++ Compiler to build kernel. This change should not change the current gcc based build infrasture.
junsu has pointed out that gcc has the ablity to do profile, record the data and optimize the generated code according to the data. The nearest compiler guru I can visit is Prof. Husheng LIAO (sorry, the link here is not available to the Internet at the time writing, but this is hopefully to be changed soon) who recently concentrate his research in optimization for the Java Language Compiling process.
Further information will be available here soon.