简体   繁体   English

为什么RV32I包含ADDI和XORI等指令而不包括BLTI?

[英]Why does RV32I include instructions like ADDI and XORI but not BLTI?

I'm not experienced in ISA design. 我没有ISA设计经验。 I've been reading https://riscv.org/specifications/ chapter 2, page 21. 我一直在阅读https://riscv.org/specifications/第2章,第21页。

Could someone explain why RISC-V has arithmetic and logical instructions which use immediates, such as ADDI and XORI, but not similar conditional branch instructions, such as BLTI, BEQI, etc. 有人可以解释为什么RISC-V具有使用immediates的算术和逻辑指令,例如ADDI和XORI,但不是类似的条件分支指令,例如BLTI,BEQI等。

(Where B ranch L ess T han I mmediate would compare a register to a constant and branch if it were less.) (其中B ranch L ess T han I mmediate会将寄存器与常量进行比较,如果它更少,则将分支进行比较。)

My uninformed opinion is that BLTI would be frequently used for fixed-length loops in C, such as: 我不知情的意见是BLTI经常用于C中的固定长度循环,例如:

for (int i = 0; i < 16; i++) {
    ...
}

Why are arithmetic and logic instructions more deserving of immediate variants than branch instructions? 为什么算术和逻辑指令比分支指令更值得立即变体?

Branches already need to encode the branch target offset as an immediate, fitting two immediate operands in there would be harder. 分支已经需要将分支目标偏移编码为立即,在那里拟合两个立即操作数将更难。 Making them both much smaller would enable them to fit, but would also reduce the usefulness of the instruction. 使它们都小得多可以使它们适合,但也会降低指令的实用性。

Such a branch would be useful occasionally, but in my opinion you are overestimating its usefulness: in typical loops there is no need to directly compare the loop counter against its boundary value, indeed most loop variables do not even make it into the compiled code. 这样的分支偶尔会有用,但在我看来,你高估了它的用处:在典型的循环中,没有必要直接将循环计数器与其边界值进行比较,实际上大多数循环变量甚至不能将它变成编译代码。

As a small example (using a higher count to avoid a full unroll of the loop), 作为一个小例子(使用更高的计数来避免完全展开循环),

int test(int *data) {
    int sum = 0;
    for (int i = 0; i < 255; i++)
        sum += data[i];
    return sum;
}

Is compiled by Clang into: 由Clang编译成:

test(int*):                              # @test(int*)
    addi    a2, zero, 1020
    mv      a3, zero
    mv      a1, zero
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
    add     a4, a0, a3
    lw      a4, 0(a4)
    add     a1, a4, a1
    addi    a3, a3, 4
    bne     a3, a2, .LBB0_1
    mv      a0, a1
    ret

What Clang has done here is computing the final address and then looping until that address is reached, removing the loop counter from existence. Clang在这里做的是计算最终地址 ,然后循环直到达到该地址,从而消除循环计数器。

This is a somewhat special case, but there are other tricks too. 这是一个有点特殊的情况,但也有其他一些技巧。 For example, in many cases the loop exit test can be transformed to a loop that exits when a register is decremented to zero, which is easy to test since RISCV has bnez . 例如,在许多情况下,循环退出测试可以转换为循环,当寄存器递减到零时退出,这很容易测试,因为RISCV具有bnez The original loop counter can co-exist with that if necessary (without participating in the loop exit test), or it can again disappear if possible. 如果需要,原始循环计数器可以与之共存(不参与循环退出测试),或者如果可能的话,它可以再次消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RiscV 汇编程序 - 试验 RV32I 的“slli”命令 - RiscV assembler - experimenting with the 'slli' command for RV32I 使用 RISC-V (RV32I) 编译器无需递归计算第 n 个斐波那契数 - Calculate nth Fibonacci number using RISC-V (RV32I) compiler without recursion MIPS I-Type指令的周期数(addi) - MIPS Amount of Cycles for I-Type instructions (addi) 分支预测器的预测中是否还包含I / O指令? - Does the branch predictor also include I/O instructions in its prediction? NASM是否在32位x86中生成的机器指令中包含段寄存器? - Does NASM include the segment registers in the generated machine instructions in 32-bit x86? 为什么编译的 rust 代码的程序集 output 不包含任何 asm 指令? - Why the assembly output of a compiled rust code does not include any asm instructions? 64 位 RISC-V RV64 是否允许访问寄存器的低 32 位? - Does 64-bit RISC-V RV64 allow access to the low 32 bits of registers? 如何正确使用ADDIS和ADDI将32位常数直接与寄存器相加? - How do I correctly use ADDIS and ADDI to sum a 32 bit constant directly to a register? “ addi a0,zero,2”在伪代码中是什么意思? - What does `addi a0, zero, 2` mean in pseudocode? RISC-V 使用 LUI 和 ADDI 构建 32 位常量 - RISC-V build 32-bit constants with LUI and ADDI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM