Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - Why did the compiler put an instruction after the MIPS "j" instruction that returns from a function?

I'm looking at some compiler output for a MIPS platform and struggling to understand how a function returns and what is allowable.

Here's a simple example:

int two_x_squared(int x)
{
    return 2*x*x;
}

If I compile it with Compiler Explorer I see

two_x_squared(int):
        sll     $2,$4,1
        mult    $2,$4
        mflo    $2
        j       $31
        nop

OK, no big deal here, I'm guessing j $31 jumps to the return address, and the nop might be something required to protect against speculative execution in the pipeline.

But then I compile with XC32 under -O2 and I get

two_x_squared:
    mul $4,$4,$4
    j   $31
    sll $2,$4,1

So... the line after the j $31 gets executed after the jump?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is called the branch delay slot.  Yes, the branch actually executes one instruction later than you would expect, and the compiler is supposed to fill the delay slot with something useful — by moving something done logically before the branch into that slot, or by moving something that would happen after the branch into that slot.

This was introduced into the original MIPS architecture (as well as HP PA RISC, others) to help with pipelined processors, as they have to drain and refill the pipeline on taken branches, which wastes instruction cycles.

The feature has been removed in later MIPS processors as well as the follow on open-source RISC V hardware.  More modern hardware uses other approaches to mitigate the wasted cycles associated pipeline refill, including branch prediction, some out of order execution, speculation, executing branches earlier in the pipeline.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...