%“分配点”的目的是什么? llvm 代码中出现的行?

发布于 2024-08-02 16:28:13 字数 875 浏览 11 评论 0原文

我最近一直在查看 llvm-gcc 生成的一些 LLVM 程序集,并且注意到一个反复出现的语句,我不确定其目的。

例如,以下 C 程序:

int main(void)
{
   void (*f)(void) = (0x21332);
   f();
}

当使用“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

我对该行的目的感兴趣:

%"alloca point" = bitcast i32 0 to i32      ; <i32> [#uses=0]

似乎没有任何东西作为它分配给的变量都不会再被使用,并且位广播本身是毫无意义的。我能想到的是,它实际上是作为 nop 插入的,用于以后的代码生成/分析目的,指示代码中有趣的部分。

I've been looking at some LLVM assembly produced by llvm-gcc lately and I've noticed a recurring statement of which I'm not sure its purpose.

For example, the following C program:

int main(void)
{
   void (*f)(void) = (0x21332);
   f();
}

When compiled with "llvm-gcc -emit-llvm -S" will produce the following code (irrelevant parts removed):

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

I'm interested in the purpose of the line:

%"alloca point" = bitcast i32 0 to i32      ; <i32> [#uses=0]

Doesn't seem to do anything as the variable it assigns to is never used again and the bitcast itself is pointless. All I can think of is that its inserted really as a nop for later code generation / analysis purposes, indicating interesting parts of the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩耳倾听 2024-08-09 16:28:14

来自 llvm-gcc 源: gcc/ llvm-convert.cpp,它只是用作辅助值*,它将被死指令消除过程删除。

// 在入口块中创建一条虚拟指令作为插入新指令的标记
// 之前分配指令。这个指令是什么并不重要,
// 它死了。这允许我们按顺序插入分配器,而不必
// 扫描插入点。对 int 使用 BitCast ->整数

From the llvm-gcc source: gcc/llvm-convert.cpp, it's just used as a helper Value* and it will be removed by a dead instruction elimination pass.

// 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
古镇旧梦 2024-08-09 16:28:14

在互联网上找到了这个:
其大小可以在编译时确定的分配器将在计算堆栈帧大小时在堆栈上分配空间。对于可变大小的分配,目标特定代码必须调整堆栈大小,根据需要调整帧指针和堆栈指针,并将传出参数的位置调整到堆栈顶部。

听起来它的存在是为了使一些堆栈空间正确工作。

Found this on the internets:
Allocas whose size can be determined at compile time will be allocated space on the stack when the stack frame size is calculated. For variable sized allocas the target specific code will have to resize the stack, adjusting the frame pointer and stack pointer as needed, and adjust the locations for the outgoing parameters to the top of the stack.

sounds like its there to make some stack space work out correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文