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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

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

跳至解决方案
295 次查看
AndyMax
Contributor III

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

0 项奖励
回复
1 解答
276 次查看
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

在原帖中查看解决方案

1 回复
277 次查看
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