LLVM IR alloca指令 [英] LLVM IR alloca instruction

查看:136
本文介绍了LLVM IR alloca指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的玩具编译器设计一个IR(例如LLVM IR),但我不知道 alloca 指令在进一步分析中的目的是什么?哪些优化使用了 alloca 信息?

I want to design a IR (like LLVM IR) for my toy compiler and I don't know what is the purpose of the alloca instruction in further analysis. In which optimizations alloca informations are used?

推荐答案

"alloca"指令在当前正在执行的功能,当此功能被自动释放时函数返回到其调用方.该对象始终分配在数据布局中指示的alloca的地址空间.

The ‘alloca‘ instruction allocates memory on the stack frame of the currently executing function, to be automatically released when this function returns to its caller. The object is always allocated in the address space for allocas indicated in the datalayout.

"alloca"指令通常用于表示自动必须具有地址的变量.

The ‘alloca‘ instruction is commonly used to represent automatic variables that must have an address available.

更多信息,请点击此处

llvm IR书中的一些注释:

Some notes from llvm IR book:

据说整个LLVM文件的内容(程序集或位代码)都定义了LLVM模块.该模块是LLVM IR顶层数据结构.每个模块包含一系列功能,其中包含一系列基本模块,这些基本模块包含一系列指令.该模块还包含支持该模型的外围实体,例如全局变量,目标数据布局,外部函数原型以及数据结构声明.

所以alloca指令(在我的基础上)只是为了支持IR.

So alloca instruction (in my undersating) is just to support IR.

例如以下代码:

int sum(int a, int b) {
  return a+b;
}

IR中的外观如下:

; Function Attrs: noinline nounwind uwtable
define i32 @sum(int, int)(i32, i32) #0 !dbg !6 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, i32* %3, align 4
  call void @llvm.dbg.declare(metadata i32* %3, metadata !10, metadata !11), !dbg !12
  store i32 %1, i32* %4, align 4
  call void @llvm.dbg.declare(metadata i32* %4, metadata !13, metadata !11), !dbg !14
  %5 = load i32, i32* %3, align 4, !dbg !15
  %6 = load i32, i32* %4, align 4, !dbg !16
  %7 = add nsw i32 %5, %6, !dbg !17
  ret i32 %7, !dbg !18
}

alloca指令在以下位置的堆栈帧上保留空间:当前功能.空间量由元素类型决定大小,并且遵循指定的对齐方式.第一条指令,%a.addr = alloca i32,对齐4,分配一个4字节的堆栈元素,该元素遵守4字节对齐方式.存储指向堆栈元素的指针在本地标识符%a.addr中.alloca指令通常是用于表示本地(自动)变量.

The alloca instruction reserves space on the stack frame of the current function. The amount of space is determined by element type size, and it respects a specified alignment. The first instruction, %a.addr = alloca i32, align 4, allocates a 4-byte stack element, which respects a 4-byte alignment. A pointer to the stack element is stored in the local identifier, %a.addr. The alloca instruction is commonly used to represent local (automatic) variables.

这篇关于LLVM IR alloca指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆