memory-management - 为什么 clang 的 `-O3` alloca 比 g++ 快 2 倍

标签 memory-management compiler-construction clang micro-optimization

On the use and abuse of alloca

在上一个问题的底部获得了一些基准。 clang 显然在 -O3 中有更好的实现优化器配置文件。是什么赋予了?叮当有没有偷工减料?另外,由于 clang 是现代编译器,它的 alloca 实现中是否有任何安全性或其他有趣的属性?

最佳答案

delnan 的猜测是真的。但是他没有考虑到测试很糟糕,clang可以优化出实际alloca操作来自 alloca_test .
alloca_test只有 llvm ir 操作 alloca,但没有 alloca()函数调用:

%11 = call i32 @_Z18random_string_sizev()
%12 = alloca i8, i32 %11

比较 malloc_test :
%11 = call i32 @_Z18random_string_sizev()
%12 = call i8* @malloc(i32 %11)

即使与 -O1 alloca_test 中不再有 alloca :
define void @_Z11alloca_testv() nounwind {
; <label>:0
  %1 = tail call i32 @_Z18random_vector_sizev()
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph, %0
  %i.01 = phi i32 [ %4, %.lr.ph ], [ 0, %0 ]
  %3 = tail call i32 @_Z18random_string_sizev()
  %4 = add nsw i32 %i.01, 1
  %exitcond = icmp eq i32 %4, %1
  br i1 %exitcond, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  ret void
}

而对于 malloc_test , malloc 调用还在:
%6 = tail call i32 @_Z18random_string_sizev()
%7 = tail call i8* @malloc(i32 %6)

我也应该说g++ -O3 (在 4.1 和 4.5.2 中测试)不会优化堆栈大小的变化(alloca 主要效果)。

关于memory-management - 为什么 clang 的 `-O3` alloca 比 g++ 快 2 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5820483/

相关文章:

c++ - 是否可以在 C++ 中声明 operator= private 并同时由编译器合成

delphi - Delphi 中的全局变量和基于堆的变量总是不稳定的吗?

c++ - 错误: "va_start" used in Win64 ABI funtion getting thi seero in clang help me to solve this?

c++ - 我必须删除 C++ 中的静态数组吗?

linux - 来自内核模块的用户进程的已用堆大小

memory-management - Linux 内存过量使用详细信息

c++ - Tilera 交叉编译 - 链接错误

C++:关于重载 operator new 的一般想法

llvm - 如何用clang替换llvm-ld?

c++ - '-print_statistics' 链接器选项在 Xcode 中产生错误