movsx/movzx optimization can damage output code
Original Reporter info from Mantis: Sergei Baranov
-
Reporter name:
Original Reporter info from Mantis: Sergei Baranov
- Reporter name:
Description:
Attached sample work differently when compiled with or without -O2 option.
Steps to reproduce:
Compile with -MDelphi with and without -O2 option (-OoREGVAR / -OonoREGVAR). Under -O2 incorrect code generate for parameter dwCreationDisposition from source create [ord ((mode and 3) = 0) + ord ((mode and $30) <> 0) shl 1]. In assembler source (-alnrt) [21] no value put into register edx before usage.
Additional information:
In compiler\i386\popt386.pas near 1289 line under A_MOVSX,A_MOVZX labels has no check, that reg2 from comment description in lines 1 and 3 of sample code the same reg2, line with getsupreg..in incorrect for shl opcode, and has no check RegUsedAfterInstruction for reg2 like in other branches. My local fix look like:
A_MOVSX,
A_MOVZX :
begin
TmpUsedRegs := UsedRegs;
if (taicpu(p).oper[1]^.typ = top_reg) and
GetNextInstruction(p,hp1) and
(hp1.typ = ait_instruction) and
IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
GetNextInstruction(hp1,hp2) and
(hp2.typ = ait_instruction) and
(taicpu(hp2).opcode = A_MOV) and
(taicpu(hp2).oper[0]^.typ = top_reg) and
(taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
OpsEqual(taicpu(hp2).oper[1]^,taicpu(p).oper[0]^) and
not RegUsedAfterInstruction (taicpu (hp2).oper [0]^.reg, hp2, TmpUsedRegs) then
Mantis conversion info:
- Mantis ID: 23431
- Platform: i386
- Version: 2.6.1
- Fixed in version: 3.0.0
- Fixed in revision: 23128 (#2ea69792)
- Monitored by: » Vincent (Vincent Snijders)