Visual Studio 2022 generates bad code (UPDATE: not any more)

I recently came across the issue of some games not working in Supermodel when using a Visual Studio 2022 release build. After hours of searching for a root cause, I discovered that it's due to the optimizing compiler generating bad code.
Here is the code that is incorrectly compiled, when ppc603_exception(EXCEPTION_IRQ) is called:
If the MSR_IP flag is set, then we should be passing 0xFFF00500 to the ppc_change_pc() function, otherwise we should be passing 0x00000500. But the VS2022 compiler generates code that always passes 0xFFF00500, regardless of whether MSP_IP is set or not.
Here are some relevant excerpts from the generated assembly file:
ebx contains the value of MSR and is tested for the MSR_IP flag (0x40); if it is set, eax is set to 0xFFF00500, otherwise it is set to 0x00000500. So far, so good.
The intention is to set ecx (0xFFF00500) to the value of edx (0x00000500) if the MSR_FLAG is not set. However, the compiler has messed up and is using the wrong compare instruction; instead of checking the carry flag (which is the one that stores the result of a bit test), it is checking the zero flag which, while correct for a regular test instruction, is undefined after a bit test. In this case, ecx is not being set to 0x00000500 and the ppc_change_pc() function is passed the wrong value (it should be noted that ppc.npc is always set to the correct value). Eventually, at some point the PowerPC interpreter fetches the wrong opcode and chaos ensues.
I'm going to switch back to Visual Studio 2019 for the time being since it compiles this code correctly. We shouldn't have to try and work around a bug with the VS2022 compiler.
Here is the code that is incorrectly compiled, when ppc603_exception(EXCEPTION_IRQ) is called:
- Code: Select all
if( msr & MSR_IP )
ppc.npc = 0xfff00000 | 0x0500;
else
ppc.npc = 0x00000000 | 0x0500;
ppc_change_pc(ppc.npc);
If the MSR_IP flag is set, then we should be passing 0xFFF00500 to the ppc_change_pc() function, otherwise we should be passing 0x00000500. But the VS2022 compiler generates code that always passes 0xFFF00500, regardless of whether MSP_IP is set or not.
Here are some relevant excerpts from the generated assembly file:
- Code: Select all
; 51 : if (msr & MSR_IP)
mov edx, 1280 ; 00000500H
bt ebx, 6
mov eax, edx
mov ecx, -1047296 ; fff00500H
cmovb eax, ecx
; 235 : {
; 236 : ppc603_exception(EXCEPTION_IRQ);
; 237 : }
jmp $LN199@ppc603_che
ebx contains the value of MSR and is tested for the MSR_IP flag (0x40); if it is set, eax is set to 0xFFF00500, otherwise it is set to 0x00000500. So far, so good.
- Code: Select all
test ebx, 64 ; 00000040H
$LN199@ppc603_che:
cmove ecx, edx
mov DWORD PTR ?ppc@@3UPPC_REGS@@A+136, eax
add rsp, 32 ; 00000020H
pop rbx
jmp ?ppc_change_pc@@YAXI@Z ; ppc_change_pc
The intention is to set ecx (0xFFF00500) to the value of edx (0x00000500) if the MSR_FLAG is not set. However, the compiler has messed up and is using the wrong compare instruction; instead of checking the carry flag (which is the one that stores the result of a bit test), it is checking the zero flag which, while correct for a regular test instruction, is undefined after a bit test. In this case, ecx is not being set to 0x00000500 and the ppc_change_pc() function is passed the wrong value (it should be noted that ppc.npc is always set to the correct value). Eventually, at some point the PowerPC interpreter fetches the wrong opcode and chaos ensues.
I'm going to switch back to Visual Studio 2019 for the time being since it compiles this code correctly. We shouldn't have to try and work around a bug with the VS2022 compiler.