我一直在看llvm-gcc最近生成的一些LLVM程序集,我注意到一个反复出现的声明,我不确定它的用途.
例如,以下C程序:
int main(void)
{
void (*f)(void) = (0x21332);
f();
}
Run Code Online (Sandbox Code Playgroud)
使用"llvm-gcc -emit-llvm -S"编译时将生成以下代码(删除不相关的部分):
define i32 @main() nounwind {
entry:
%retval = alloca i32 ; <i32*> [#uses=1]
%f = alloca void ()* ; <void ()**> [#uses=2]
%"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
store void ()* inttoptr (i64 135986 to void ()*), void ()** %f, align 4
%0 = load void ()** %f, align 4 ; <void ()*> [#uses=1]
call void %0() nounwind
br label %return
Run Code Online (Sandbox Code Playgroud)
我对这条线的目的感兴趣:
%"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
Run Code Online (Sandbox Code Playgroud)
似乎没有做任何事情因为它所分配的变量从未再次使用过,而bitcast本身也毫无意义.我能想到的是,它实际上是作为一个nop插入的,用于以后的代码生成/分析目的,表明代码的有趣部分.
从llvm-gcc源代码:gcc/llvm-convert.cpp,它只是用作帮助程序Value*,它将被死指令消除传递删除.
Run Code Online (Sandbox Code Playgroud)// Create a dummy instruction in the entry block as a marker to insert new // alloc instructions before. It doesn't matter what this instruction is, // it is dead. This allows us to insert allocas in order without having to // scan for an insertion point. Use BitCast for int -> int