Order the compiler use CPU Registers for some counters of loops or temp variables?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Order the compiler use CPU Registers for some counters of loops or temp variables?

Jump to solution
330 Views
AndyMax
Contributor III

How to order the compiler use CPU Registers for some counters of loops or vars without using RAM/Stack?

0 Kudos
Reply
1 Solution
311 Views
petervlna
NXP TechSupport
NXP TechSupport

Hello,

Modern compilers (including those used for MPC57xx, like GCC or Green Hills) automatically optimize register usage. You cannot force a variable into a register using standard C/C++ syntax. Even the register keyword is only a hint, and compilers may ignore it if register pressure is high or if the variable doesn't benefit from being in a register.

Use inline assembly:

This gives you direct control over register usage, but sacrifices portability and maintainability.

asm volatile (
    "li r3, 0\n\t"  // Load immediate into register r3
    "loop:\n\t"
    "addi r3, r3, 1\n\t"
    "cmpwi r3, 1000\n\t"
    "blt loop\n\t"
 
or sure register keyword. This suggests to the compiler that i should be stored in a register.
for (register int i = 0; i < N; i++) {
// loop logic
}

You can also check your compiler optimizations and settings, perform aggressive register allocation and loop unrolling.

In past it was common to simply use assembly code. It was optimized to max and did exactly what was told.

Best regards,

Peter

View solution in original post

1 Reply
312 Views
petervlna
NXP TechSupport
NXP TechSupport

Hello,

Modern compilers (including those used for MPC57xx, like GCC or Green Hills) automatically optimize register usage. You cannot force a variable into a register using standard C/C++ syntax. Even the register keyword is only a hint, and compilers may ignore it if register pressure is high or if the variable doesn't benefit from being in a register.

Use inline assembly:

This gives you direct control over register usage, but sacrifices portability and maintainability.

asm volatile (
    "li r3, 0\n\t"  // Load immediate into register r3
    "loop:\n\t"
    "addi r3, r3, 1\n\t"
    "cmpwi r3, 1000\n\t"
    "blt loop\n\t"
 
or sure register keyword. This suggests to the compiler that i should be stored in a register.
for (register int i = 0; i < N; i++) {
// loop logic
}

You can also check your compiler optimizations and settings, perform aggressive register allocation and loop unrolling.

In past it was common to simply use assembly code. It was optimized to max and did exactly what was told.

Best regards,

Peter