From 4648f967bdb769a954cdce0cf995a45d9aafdc55 Mon Sep 17 00:00:00 2001 From: "J. Gareth \"Curious Kit\" Moreton" Date: Mon, 24 Feb 2025 05:04:15 +0000 Subject: [PATCH] * x86: Fixed bug in "MovMovMov2MovMovMov" optimisation where a register wasn't properly tracked --- compiler/x86/aoptx86.pas | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/x86/aoptx86.pas b/compiler/x86/aoptx86.pas index b4fe4fef7e..375a60eafe 100644 --- a/compiler/x86/aoptx86.pas +++ b/compiler/x86/aoptx86.pas @@ -4369,6 +4369,13 @@ unit aoptx86; begin taicpu(hp2).loadreg(0, p_SourceReg); + TransferUsedRegs(TmpUsedRegs); + UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1); + { Make sure the register is allocated between these instructions + even though it doesn't change value, since it may cause + optimisations on a later pass to behave incorrectly. (Fixes #41155) } + AllocRegBetween(p_SourceReg, hp1, hp2, TmpUsedRegs); + DebugMsg(SPeepholeOptimization + 'Optimised register duplication and memory read (MovMovMov2MovMovMov)', p); Result := True; if taicpu(hp2).oper[1]^.reg = p_TargetReg then @@ -4379,11 +4386,6 @@ unit aoptx86; end else begin - { Check to see if %reg2 is no longer in use } - TransferUsedRegs(TmpUsedRegs); - UpdateUsedRegsBetween(TmpUsedRegs, tai(p.Next), hp1); - UpdateUsedRegsBetween(TmpUsedRegs, tai(hp1.Next), hp2); - if not RegUsedAfterInstruction(p_TargetReg, hp2, TmpUsedRegs) then begin DebugMsg(SPeepholeOptimization + 'Mov2Nop 5b done', p);