简体   繁体   English

C的优化MIPS指令立即返回?

[英]Optimized MIPS instructions from C returning immediately?

I've been playing around with Godbolt a bit to see how the compiler optimizes instructions, and something I've noticed is how the optimization of simple C programs seems to return immediately without actually doing the computation. 我一直在玩Godbolt,看看编译器如何优化指令,我注意到的是简单C程序的优化如何在没有实际计算的情况下立即返回。 Say I have a very simple program in Godbolt ( here ): 说我在Godbolt有一个非常简单的程序( 这里 ):

int square(int num) {
    return num * num;
}

int main(int argc, const char* argv[]){
    return square(argc);
}

The MIPS instruction output is: MIPS指令输出是:

square:
        j       $31
        mul     $2,$4,$4

main:
        j       $31
        mul     $2,$4,$4

From my recollection of MIPS, isn't this just jumping to the $ra register immediately, effectively not doing anything at all? 从我对MIPS的回忆中,这不是立即跳到$ra寄存器,实际上根本没有做任何事情吗? I thought that once we j $31 or jump to the return address (and not doing a jal ), we're effectively returning from the function at that point. 我认为,一旦我们j $31或跳转到返回地址(而不是jal ),我们就会有效地从该函数返回。 So how is this working if it's returning before doing the multiply? 那么如果在进行乘法运算之前它是如何工作的呢?

I'm totally not an assembler guy, but I did read about this a little while ago. 我完全不是一个装配工,但我刚才读过这篇文章。 The answer: branch delay slots . 答案: 分支延迟槽 Read more here: https://devblogs.microsoft.com/oldnewthing/20180411-00/?p=98485 在这里阅读更多内容: https//devblogs.microsoft.com/oldnewthing/20180411-00/?p = 98485

When you perform a branch instruction, the instruction after the branch instruction is executed, even if the branch is taken. 执行分支指令时,即使执行了分支,也会执行分支指令后的指令。 The branch itself is delayed by one instruction. 分支本身被一条指令延迟。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM