las*_*owh 8 c compiler-construction llvm clang
如果"alloc.c"中包含以下代码:
typedef __typeof__(sizeof(int)) size_t;
extern void *calloc (size_t __nmemb, size_t __size)
__attribute__ ((__nothrow__ )) __attribute__ ((__malloc__)) ;
extern void free (void *__ptr) __attribute__ ((__nothrow__ ));
struct _astruct {
int l;
};
int main() {
struct _astruct *f = (struct _astruct*)calloc(1, sizeof(struct _astruct));
f->l = 99;
int x = f->l;
free(f);
return x;
}
Run Code Online (Sandbox Code Playgroud)
(我知道这不是声明calloc/free的首选方法,但它是为了简化下面的输出.)
然后clang -O3 -S -emit-llvm alloc.c用Clang/LLVM 3.3 运行" ",你得到:
; ModuleID = 'alloc.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: nounwind readnone uwtable
define i32 @main() #0 {
entry:
ret i32 99
}
attributes #0 = { nounwind readnone uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
Run Code Online (Sandbox Code Playgroud)
你可以看到它完全取消了对calloc的调用.真棒.但是,一般情况下,编译器无法消除对其没有源的函数的调用(可能存在副作用).所以Clang似乎有calloc/free的特殊案例代码.这是因为如果你将所有出现的代码中的"calloc"调用为"_calloc",那么"分配"就会返回.
所以我的问题是,有没有办法告诉Clang/LLVM特定功能是"仅分配"功能?也许我可以对我自己的一个函数的定义进行一些注释,这将允许LLVM优化远离分配,就像它可以用于calloc/malloc一样.