How to stop the msvc optimizer from flushing registers?



  • I am trying to optimize the inner loop of an interpreter, and looking at the generated machine code I notice that the compiler always writes a local variable back to the stack frame after it has been modified, even though the variable is held in the same register throughout the entire loop & its lifetime. Sometimes it randomly loads it from the stack as well, even though it was already in the same register just before, and no control flow is present in between.

    The variable is not static/volatile, is not aliased (its address is never taken), and its register is never used for other purposes in the generated code. I have all optimisations on max and even have /Oa on. Using vs.net.

    How do I stop the superfluous flushing/loading from happening?



  • Use the register keyword:

    register int i;
    for(i = 0; i < 100; ++i)
    {
    // ...
    }
    

    http://msdn.microsoft.com/library/en-us/vclang/html/_pluslang_the_register_keyword.asp



  • Wenn du so tief in der optimierung steckst, und sie zwingend notwendig ist würde ich an dieser Stelle auf die Assemblerprogrammierung wechseln.

    Das Register Keyword ist ein Wunsch an den Compiler diese Variable in Registern zu halten. Der Compiler kann entscheiden es zu tun muß es aber nicht.
    :p

    🙂



  • danke fuer die antworten. Aber das register keyword wird von msvc 100%-ig ignoriert. Die interpreter soll halt portabel sein, ich will es deshalb in C++ schreiben das auf wichtiche platformen (wie x86) gut optimiert werden kann. Das is durchaus einfach, nuer in diesen fall nicht. Assembler schreiben das schneller ist als was compiler generieren ist auch nicht mal leicht.



  • Dann nimm den Compiler von Intel, der Optimiert was das Zeug hält!



  • Ehm, inline asm ist die einzige Möglichkeit. register ist schon lange keine Empfehlung mehr wie bspw. inline. Der Compiler weiß in der Regel besser was wann in welches Register gehört.


Anmelden zum Antworten