Tuesday, February 10, 2015

How to decide function call arguments passing for performance improvement

I would be considering ARM v7 architecture to explain this topic.

ARM has R0 to R15 registers.
R0 to R3 will be used for passing function arguments.
R0 and R1 basically used for return values.

So ideally till 4 words , function can pass arguments through registers. If it is more , it will push it to stack.

Func( int , double , int )  =>  if we call function like this

R0 -> int
R1 -> unused ( due to alignment needed , R1 will not be used for Double)
R2, R3 -> combinely holds Double value
stack -> final int will be placed in stack


Modify the function like below: 
Func( int ,  int , double )  =>  if we call function like this

R0 -> int

R1, R2 -> combinely holds Double value
R3 -> int

We avoided using stack by arranging arguments in proper order.

This will avoid usage of stack and will improve performance.

2 comments: