Safety checks on TEST removals and better FLAG tracking

This commit is contained in:
J. Gareth "Curious Kit" Moreton 2021-12-13 11:06:05 +00:00 committed by J. Gareth "Kit" Moreton
parent 06da0b3db6
commit 7a15312b54

View File

@ -4320,6 +4320,8 @@ unit aoptx86;
begin begin
DebugMsg(SPeepholeOptimization + 'TEST/Jcc/TEST; removed superfluous TEST', p_dist); DebugMsg(SPeepholeOptimization + 'TEST/Jcc/TEST; removed superfluous TEST', p_dist);
RemoveInstruction(p_dist); RemoveInstruction(p_dist);
{ Don't let the flags register become deallocated and reallocated between the jumps }
AllocRegBetween(NR_DEFAULTFLAGS, hp1, hp1_dist, UsedRegs);
Result := True; Result := True;
if condition_in(taicpu(hp1_dist).condition, taicpu(hp1).condition) then if condition_in(taicpu(hp1_dist).condition, taicpu(hp1).condition) then
begin begin
@ -4365,7 +4367,6 @@ unit aoptx86;
} }
if ((FirstValue or SecondValue) = FirstValue) then if ((FirstValue or SecondValue) = FirstValue) then
begin begin
RemoveInstruction(p_dist);
if (taicpu(hp1_dist).condition in [C_E, C_Z]) then if (taicpu(hp1_dist).condition in [C_E, C_Z]) then
begin begin
if IsJumpToLabel(taicpu(hp1_dist)) then if IsJumpToLabel(taicpu(hp1_dist)) then
@ -4373,12 +4374,19 @@ unit aoptx86;
DebugMsg(SPeepholeOptimization + 'TEST/JE/TEST/JE merged', p); DebugMsg(SPeepholeOptimization + 'TEST/JE/TEST/JE merged', p);
RemoveInstruction(hp1_dist); RemoveInstruction(hp1_dist);
{ Only remove the second test if no jumps or other conditional instructions follow }
TransferUsedRegs(TmpUsedRegs);
if not RegUsedAfterInstruction(NR_DEFAULTFLAGS, p_dist, TmpUsedRegs) then
RemoveInstruction(p_dist);
Result := True; Result := True;
Exit; Exit;
end end
else if (taicpu(hp1_dist).condition in [C_NE, C_NZ]) then else if (taicpu(hp1_dist).condition in [C_NE, C_NZ]) then
begin begin
DebugMsg(SPeepholeOptimization + 'TEST/JE/TEST/JNE merged', p); DebugMsg(SPeepholeOptimization + 'TEST/JE/TEST/JNE merged', p);
RemoveInstruction(p_dist);
MakeUnconditional(taicpu(hp1_dist)); MakeUnconditional(taicpu(hp1_dist));
RemoveDeadCodeAfterJump(hp1_dist); RemoveDeadCodeAfterJump(hp1_dist);
Result := True; Result := True;
@ -4409,7 +4417,11 @@ unit aoptx86;
TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol).DecRefs; TAsmLabel(taicpu(hp1_dist).oper[0]^.ref^.symbol).DecRefs;
DebugMsg(SPeepholeOptimization + 'TEST/JNE/TEST/JNE merged', p); DebugMsg(SPeepholeOptimization + 'TEST/JNE/TEST/JNE merged', p);
RemoveInstruction(p_dist); { Only remove the second test if no jumps or other conditional instructions follow }
TransferUsedRegs(TmpUsedRegs);
if not RegUsedAfterInstruction(NR_DEFAULTFLAGS, p_dist, TmpUsedRegs) then
RemoveInstruction(p_dist);
RemoveInstruction(hp1_dist); RemoveInstruction(hp1_dist);
Result := True; Result := True;
Exit; Exit;