LLVM学习——IR指令

LLVM官方文档手册:LLVM Language Reference Manual — LLVM 16.0.0git documentation

1. alloca

该指令用于分配内存堆栈给当前执行的函数,当这个函数返回它的caller时会被自动释放。

e.g. %a = alloca i32, align4

解释:

  • i32表示32位,4字节。LLVM中将整数类型定义为i N,其中N是整数所占用的位数,这里N取32
  • align 4表示向4“对齐”:即便数据没有占用4个字节,也要为其分配4字节。比如你需要使用64进制数字,那就只要i48, align 6即可

2.load/store

load是读出内容,store是写入内容

%0 = load i32* %a, align 4  表示读出%a中的值

store i32 1, i32* %a, align 4 表示将1写入%a中

3.判断及跳转icmp/br

icmp可以根据两个整数值的比较(op1,op2)返回一个布尔类型的值或者布尔矢量

比较规则由参数cond确定,具体比较规则如下:

  • eq: yields true if the operands are equal, false otherwise. 不区分有符号和无符号
  • ne: yields true if the operands are unequal, false otherwise. 不区分有符号和无符号
  • ugt: interprets the operands as unsigned values and yields true if op1 is greater than op2.
  • uge: interprets the operands as unsigned values and yields true if op1 is greater than or equal to op2.
  • ult: interprets the operands as unsigned values and yields true if op1 is less than op2.
  • ule: interprets the operands as unsigned values and yields true if op1 is less than or equal to op2.
  • sgt: interprets the operands as signed values and yields true if op1 is greater than op2.
  • sge: interprets the operands as signed values and yields true if op1 is greater than or equal to op2.
  • slt: interprets the operands as signed values and yields true if op1 is less than op2.
  • sle: interprets the operands as signed values and yields true if op1 is less than or equal to op2.
  • sgt: interprets the operands as signed values and yields true if op1 is greater than op2.

无疑,icmp是用于判断的指令,但是仅仅判断出结果来还不够,仍需要根据判断结果进行相应的选择性操作,if语句才完整

br提供一个选择分支结构,可根据cond的情况使程序转向label <iftrue>或label <iffalse>,标准格式为:

br i1 <cond>, label <iftrue>, label <iffalse>

另外br也有一种特殊形式:无条件分支(Unconditional branch)。当在某种情况时,不必进行条件判断而直接跳转至某一个特定的程序入口标签(label)处(感觉类似于一个“goto”),标准格式为:

  br label <dest> 

如: if.then: ; preds = %entry store i32 1, i32* %c, align 4 br label %if.end If then完事后,直接跳转到if.end

4.二元运算符加(add)减(sub)乘(mul)除(div)求余(rem)

前面加f的是浮点运算,加u的是返回无符号整型值(unsigned integer),加s返回的是有符号的;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值