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. Say I have a very simple program in Godbolt (here):
int square(int num) {
return num * num;
}
int main(int argc, const char* argv[]){
return square(argc);
}
The MIPS instruction output is:
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? 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. So how is this working if it's returning before doing the multiply?