From 5771073e0bc0c092d1cc5d462216adcbc118b5ae Mon Sep 17 00:00:00 2001 From: yury Date: Wed, 21 Oct 2015 15:35:54 +0000 Subject: [PATCH] * Fixed general peephole optimization of conditional jumps after r30446. It has been broken, since r30446 had added some IsJumpToLabel() checks, which tests for unconditional jump, but the optimization code expects also conditional jumps. - Renamed IsJumpToLabel() to IsJumpToLabelUncond() to avoid confusions. - Added IsJumpToLabel() to check for any jump to a label. - Added comments. git-svn-id: trunk@32114 - --- compiler/aoptobj.pas | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/compiler/aoptobj.pas b/compiler/aoptobj.pas index 393a14f1ac..c22f849f83 100644 --- a/compiler/aoptobj.pas +++ b/compiler/aoptobj.pas @@ -1174,7 +1174,9 @@ Unit AoptObj; end; {$pop} - function IsJumpToLabel(hp: taicpu): boolean; + + { Returns True if hp is an unconditional jump to a label } + function IsJumpToLabelUncond(hp: taicpu): boolean; begin {$if defined(avr)} result:=(hp.opcode in aopt_uncondjmp) and @@ -1190,6 +1192,16 @@ Unit AoptObj; end; + { Returns True if hp is any jump to a label } + function IsJumpToLabel(hp: taicpu): boolean; + begin + result:=hp.is_jmp and + (hp.ops>0) and + (JumpTargetOp(hp)^.typ = top_ref) and + (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel); + end; + + procedure TAOptObj.RemoveDelaySlot(hp1:tai); var hp2: tai; @@ -1239,7 +1251,7 @@ Unit AoptObj; (taicpu(p1).is_jmp) then if { the next instruction after the label where the jump hp arrives} { is unconditional or of the same type as hp, so continue } - IsJumpToLabel(taicpu(p1)) + IsJumpToLabelUncond(taicpu(p1)) {$if not defined(MIPS) and not defined(JVM)} { for MIPS, it isn't enough to check the condition; first operands must be same, too. } or @@ -1254,7 +1266,7 @@ Unit AoptObj; SkipLabels(p1,p2) and (p2.typ = ait_instruction) and (taicpu(p2).is_jmp) and - (IsJumpToLabel(taicpu(p2)) or + (IsJumpToLabelUncond(taicpu(p2)) or (conditions_equal(taicpu(p2).condition,hp.condition))) and SkipLabels(p1,p1)) {$endif not MIPS and not JVM} @@ -1354,7 +1366,7 @@ Unit AoptObj; { the following if-block removes all code between a jmp and the next label, because it can never be executed } - if IsJumpToLabel(taicpu(p)) then + if IsJumpToLabelUncond(taicpu(p)) then begin hp2:=p; while GetNextInstruction(hp2, hp1) and @@ -1387,11 +1399,11 @@ Unit AoptObj; end else break; end; - { remove jumps to a label coming right after them } if GetNextInstruction(p, hp1) then begin SkipEntryExitMarker(hp1,hp1); - if IsJumpToLabel(taicpu(p)) and + { remove unconditional jumps to a label coming right after them } + if IsJumpToLabelUncond(taicpu(p)) and FindLabel(tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol), hp1) and { TODO: FIXME removing the first instruction fails} (p<>blockstart) then @@ -1409,10 +1421,17 @@ Unit AoptObj; end else if assigned(hp1) then begin + { change the following jumps: + jmp lab_1 jmp lab_2 + jmp lab_2 >>> + lab_1: lab_2: + + lab_2: + } if hp1.typ = ait_label then SkipLabels(hp1,hp1); if (tai(hp1).typ=ait_instruction) and - IsJumpToLabel(taicpu(hp1)) and + IsJumpToLabelUncond(taicpu(hp1)) and GetNextInstruction(hp1, hp2) and IsJumpToLabel(taicpu(p)) and FindLabel(tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol), hp2) then