View Issue Details

IDProjectCategoryView StatusLast Update
0037399FPCCompilerpublic2020-09-13 21:35
ReporterJ. Gareth Moreton Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityN/A
Status resolvedResolutionfixed 
PlatformCross-platformOSMicrosoft Windows 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0037399: [Patch / Refector] Peephole instruction removal function
DescriptionThis patch aims to reduce maintenance costs and the risk of forgetting to remove an instruction from the linked list prior to freeing it (leaving a dangling pointer). Most references to "asml.remove(hp1); hp1.free;" and similar pairs of statements have been replaced with a new "RemoveInstruction" routine that calls these two methods for the given tai object.

Also, where the current instruction (p) is freed nearby, RemoveCurrentP is called where possible.
Steps To ReproduceApply patch and confirm correct compilation on all relevant platforms.
Additional InformationShould an additional step be required to remove instructions in future, the RemoveInstruction routine can be updated to safely include this functionality.

Regression tests have been run successfully for i386-win32 and x86_64-win64 and "make fullcycle" cross compilation from these platforms.
TagsNo tags attached.
Fixed in Revision46862
FPCOldBugId
FPCTarget-
Attached Files

Activities

J. Gareth Moreton

2020-07-26 20:56

developer   ~0124347

Updated patch to work with latest trunk.
RemoveRefactor.patch (104,390 bytes)   
Index: compiler/aarch64/aoptcpu.pas
===================================================================
--- compiler/aarch64/aoptcpu.pas	(revision 45864)
+++ compiler/aarch64/aoptcpu.pas	(working copy)
@@ -189,8 +189,7 @@
             p.oper[1]^.ref^.offset:=taicpu(hp1).oper[2]^.val
           else
             p.oper[1]^.ref^.offset:=-taicpu(hp1).oper[2]^.val;
-          asml.Remove(hp1);
-          hp1.Free;
+          RemoveInstruction(hp1);
           Result:=true;
         end;
     end;
@@ -234,8 +233,7 @@
               alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.previous));
               if assigned(alloc) then
                 begin
-                  asml.Remove(alloc);
-                  alloc.free;
+                  RemoveInstruction(alloc);
                   dealloc.free;
                 end
               else
@@ -265,8 +263,7 @@
 
               { finally get rid of the mov }
               taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
-              asml.remove(movp);
-              movp.free;
+              RemoveInstruction(movp);
             end;
         end;
     end;
@@ -362,12 +359,8 @@
 
                 taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
                 asml.insertbefore(hp2, hp1);
-                GetNextInstruction(p, hp2);
-                asml.remove(p);
-                asml.remove(hp1);
-                p.free;
-                hp1.free;
-                p:=hp2;
+                RemoveInstruction(hp1);
+                RemoveCurrentP(p);
                 DebugMsg('Peephole FoldShiftProcess done', p);
                 Result:=true;
                 break;
@@ -444,16 +437,11 @@
         MatchInstruction(hp4, A_RET, [C_None], [PF_None]) and
         (taicpu(hp4).ops = 0) then
         begin
-          asml.Remove(p);
-          asml.Remove(hp1);
-          asml.Remove(hp3);
-          asml.Remove(hp4);
           taicpu(hp2).opcode:=A_B;
-          p.free;
-          hp1.free;
-          hp3.free;
-          hp4.free;
-          p:=hp2;
+          RemoveInstruction(hp1);
+          RemoveInstruction(hp3);
+          RemoveInstruction(hp4);
+          RemoveCurrentP(p, hp2);
           DebugMsg('Peephole Bl2B done', p);
           Result:=true;
         end;
@@ -512,11 +500,8 @@
           taicpu(hp2).fileinfo:=taicpu(hp1).fileinfo;
           asml.insertbefore(hp2, hp1);
 
-          asml.remove(p);
-          asml.remove(hp1);
-          p.free;
-          hp1.free;
-          p:=hp2;
+          RemoveInstruction(hp1);
+          RemoveCurrentP(p, hp2);
           DebugMsg('Peephole CMPB.E/NE2CBNZ/CBZ done', p);
           Result:=true;
         end;
Index: compiler/aopt.pas
===================================================================
--- compiler/aopt.pas	(revision 45864)
+++ compiler/aopt.pas	(working copy)
@@ -173,8 +173,7 @@
 {$ifdef DEBUG_OPTALLOC}
                             AsmL.InsertAfter(tai_comment.Create(strpnew('Removed allocation of '+std_regname(tai_regalloc(p).Reg))),p);
 {$endif DEBUG_OPTALLOC}
-                            AsmL.remove(p);
-                            p.free;
+                            RemoveInstruction(p);
                             p := hp1;
                             { not sure if this is useful, it even skips previous deallocs of the register (FK)
                             hp1 := p;
@@ -229,8 +228,7 @@
 {$ifdef DEBUG_OPTALLOC}
                             AsmL.InsertAfter(tai_comment.Create(strpnew('Removed deallocation of '+std_regname(tai_regalloc(p).Reg))),p);
 {$endif DEBUG_OPTALLOC}
-                            AsmL.remove(p);
-                            p.free;
+                            RemoveInstruction(p);
                             p := hp1;
                           end
                         else
Index: compiler/aoptobj.pas
===================================================================
--- compiler/aoptobj.pas	(revision 45864)
+++ compiler/aoptobj.pas	(working copy)
@@ -338,6 +338,9 @@
           reloaded with a new value or it is deallocated afterwards }
         function RegEndOfLife(reg: TRegister;p: taicpu): boolean;
 
+        { removes p from asml, then frees it }
+        procedure RemoveInstruction(const p : tai); inline;
+
         { removes p from asml, updates registers and replaces it by a valid value, if this is the case true is returned }
         function RemoveCurrentP(var p : tai): boolean;
 
@@ -1440,8 +1443,7 @@
                         end;
                       lastRemovedWasDealloc := (tai_regalloc(p1).ratype=ra_dealloc);
                       hp := tai(p1.Next);
-                      asml.Remove(p1);
-                      p1.free;
+                      RemoveInstruction(p1);
                       p1 := hp;
                     end
                   else
@@ -1487,6 +1489,13 @@
       end;
 
 
+    procedure TAOptObj.RemoveInstruction(const p : tai); inline;
+      begin
+        AsmL.Remove(p);
+        p.Free;
+      end;
+
+
     function TAOptObj.RemoveCurrentP(var p : tai) : boolean;
       var
         hp1 : tai;
@@ -1495,8 +1504,7 @@
         { p will be removed, update used register as we continue
           with the next instruction after p }
         UpdateUsedRegs(tai(p.Next));
-        AsmL.Remove(p);
-        p.Free;
+        RemoveInstruction(p);
         p:=hp1;
       end;
 
@@ -1504,8 +1512,7 @@
     procedure TAOptObj.RemoveCurrentP(var p: tai; const hp1: tai); inline;
       begin
         UpdateUsedRegs(tai(p.Next));
-        AsmL.Remove(p);
-        p.Free;
+        RemoveInstruction(p);
         p := hp1;
       end;
 
@@ -1594,8 +1601,7 @@
         if assigned(hp2) and (hp2.typ=ait_instruction) and
           (taicpu(hp2).opcode=A_NOP) then
           begin
-            asml.remove(hp2);
-            hp2.free;
+            RemoveInstruction(hp2);
           end;
         { Anything except A_NOP must be left in place: these instructions
           execute before branch, so code stays correct if branch is removed. }
@@ -1701,8 +1707,7 @@
                       { Check again in case there's more than one adjacent alignment entry
                         (a frequent construct under x86, for example). [Kit] }
                     end;
-                  asml.remove(hp1);
-                  hp1.free;
+                  RemoveInstruction(hp1);
                 end
               else
                 p:=hp1;
@@ -1747,8 +1752,7 @@
                   { Set tmp to the next valid entry }
                   tmp := tai(hp1.Next);
                   { Remove label }
-                  AsmL.Remove(hp1);
-                  hp1.Free;
+                  RemoveInstruction(hp1);
 
                   hp1 := tmp;
 
@@ -1786,8 +1790,7 @@
                                 end;
 
                               tmpNext := tai(tmp.Next);
-                              AsmL.Remove(tmp);
-                              tmp.Free;
+                              RemoveInstruction(tmp);
                               Result := True;
                               tmp := tmpNext;
                             end
@@ -1818,8 +1821,7 @@
 
                             { Remove label }
                             tmpNext := tai(tmp.Next);
-                            AsmL.Remove(tmp);
-                            tmp.Free;
+                            RemoveInstruction(tmp);
                             Result := True;
                             tmp := tmpNext;
 
@@ -1860,8 +1862,7 @@
 
             tmp := tai(CurrentAlign.next);
 
-            AsmL.Remove(CurrentAlign);
-            CurrentAlign.Free;
+            RemoveInstruction(CurrentAlign);
 
             CurrentAlign := tmp;
           end;
@@ -1880,8 +1881,7 @@
             ait_align:
               begin
                 tmp := tai(hp.Next);
-                asml.Remove(hp);
-                hp.Free;
+                RemoveInstruction(hp);
                 hp := tmp;
                 { Control flow will now return to 'repeat' }
               end;
@@ -1893,8 +1893,7 @@
                 if tai_label(hp).labsym.getrefs <> 0 then
                   InternalError(2019110802);
 {$endif EXTDEBUG}
-                asml.Remove(hp);
-                hp.Free;
+                RemoveInstruction(hp);
                 Exit;
               end;
             else
@@ -1993,8 +1992,7 @@
                         RemoveDelaySlot(p);
 {$endif cpudelayslot}
                         UpdateUsedRegs(tai(p.Next));
-                        AsmL.Remove(p);
-                        p.Free;
+                        RemoveInstruction(p);
                         p := hp1;
 
                         Result := True;
@@ -2033,8 +2031,7 @@
 {$ifdef cpudelayslot}
                             RemoveDelaySlot(hp1);
 {$endif cpudelayslot}
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
 
                             stoploop := False;
 
@@ -2084,8 +2081,7 @@
                         NCJLabel.decrefs;
                         GetNextInstruction(hp1, hp2);
 
-                        AsmL.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
 
                         hp1 := hp2;
 
@@ -2116,8 +2112,7 @@
                             RemoveDelaySlot(p);
 {$endif cpudelayslot}
                             UpdateUsedRegs(tai(p.Next));
-                            AsmL.Remove(p);
-                            p.Free;
+                            RemoveInstruction(p);
                             p := hp1;
 
                             Result := True;
@@ -2180,8 +2175,7 @@
 {$ifdef cpudelayslot}
             RemoveDelaySlot(p);
 {$endif cpudelayslot}
-            asml.remove(p);
-            p.free;
+            RemoveInstruction(p);
 
             StripDeadLabels(tmp, p);
 
Index: compiler/arm/aoptcpu.pas
===================================================================
--- compiler/arm/aoptcpu.pas	(revision 45864)
+++ compiler/arm/aoptcpu.pas	(working copy)
@@ -118,8 +118,8 @@
          (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
       begin
         asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
-        asml.remove(movp);
-        movp.free;
+        asml.Remove(cmpp);
+        cmpp.free;
         Result:=true;
       end;
     end;
@@ -364,8 +364,7 @@
               alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.previous));
               if assigned(alloc) then
                 begin
-                  asml.Remove(alloc);
-                  alloc.free;
+                  RemoveInstruction(alloc);
                   dealloc.free;
                 end
               else
@@ -395,8 +394,7 @@
 
               { finally get rid of the mov }
               taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
-              asml.remove(movp);
-              movp.free;
+              RemoveInstruction(movp);
             end;
         end;
     end;
@@ -515,8 +513,7 @@
               else
                 p.oper[1]^.ref^.signindex:=-1;
             end;
-          asml.Remove(hp1);
-          hp1.Free;
+          RemoveInstruction(hp1);
           Result:=true;
         end;
     end;
@@ -586,8 +583,7 @@
                    asml.insertbefore(hp2, p);
                  end;
 
-               asml.remove(hp1);
-               hp1.free;
+               RemoveInstruction(hp1);
                Result:=true;
              end
            else
@@ -616,8 +612,7 @@
                         if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
                           begin
                             DebugMsg('Peephole StrLdr2StrMov 1 done', hp1);
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
                           end
                         else
                           begin
@@ -656,8 +651,7 @@
                         taicpu(p).loadref(2,taicpu(p).oper[1]^.ref^);
                         taicpu(p).loadreg(1, taicpu(hp1).oper[0]^.reg);
                         taicpu(p).ops:=3;
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         result:=true;
                       end;
                     Result:=LookForPostindexedPattern(taicpu(p)) or Result;
@@ -689,8 +683,7 @@
                             if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
                               begin
                                 DebugMsg('Peephole LdrLdr2Ldr done', hp1);
-                                asml.remove(hp1);
-                                hp1.free;
+                                RemoveInstruction(hp1);
                               end
                             else
                               begin
@@ -725,8 +718,7 @@
                             taicpu(p).loadreg(1, taicpu(hp1).oper[0]^.reg);
                             taicpu(p).ops:=3;
                             taicpu(p).oppostfix:=PF_D;
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
                             result:=true;
                           end;
                       end;
@@ -753,8 +745,7 @@
                        begin
                          DebugMsg('Peephole LdrbAnd2Ldrb done', p);
                          taicpu(p).oper[0]^.reg := taicpu(hp1).oper[0]^.reg;
-                         asml.remove(hp1);
-                         hp1.free;
+                         RemoveInstruction(hp1);
                          result:=true;
                        end;
                     Result:=LookForPostindexedPattern(taicpu(p)) or Result;
@@ -818,11 +809,8 @@
                               begin
                                 DebugMsg('Peephole optimizer removed superfluous 16 Bit zero extension', hp1);
                                 taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
-                                asml.remove(p);
-                                asml.remove(hp1);
-                                p.free;
-                                hp1.free;
-                                p:=hp2;
+                                RemoveInstruction(hp1);
+                                RemoveCurrentP(p, hp2);
                                 Result:=true;
                               end;
                           end
@@ -856,8 +844,7 @@
                                   internalerror(2008072803);
                               end;
                             DebugMsg('Peephole ShiftShift2Shift 1 done', p);
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
                             result := true;
                           end
                         { fold
@@ -895,10 +882,8 @@
                                         DebugMsg('Peephole ShiftShiftShift2ShiftShift 1a done', p);
                                         inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm-taicpu(hp1).oper[2]^.shifterop^.shiftimm);
                                         taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
-                                        asml.remove(hp1);
-                                        asml.remove(hp2);
-                                        hp1.free;
-                                        hp2.free;
+                                        RemoveInstruction(hp1);
+                                        RemoveInstruction(hp2);
 
                                         if taicpu(p).oper[2]^.shifterop^.shiftimm>=32 then
                                           begin
@@ -915,8 +900,7 @@
 
                                     dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
                                     taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
-                                    asml.remove(hp2);
-                                    hp2.free;
+                                    RemoveInstruction(hp2);
                                     result := true;
                                   end;
                               end
@@ -937,15 +921,11 @@
                                 dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
                                 taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
                                 DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
-                                asml.remove(p);
-                                p.free;
-                                p:=hp2;
+                                RemoveCurrentP(p, hp2);
                                 if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
                                   begin
                                     taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
-                                    asml.remove(hp1);
-                                    hp1.free;
-                                    p:=hp2;
+                                    RemoveInstruction(hp1);
                                   end;
                                 result := true;
                               end;
@@ -982,8 +962,7 @@
                            begin
                              DebugMsg('Peephole LsrAnd2Lsr done', hp1);
                              taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
-                             asml.remove(hp1);
-                             hp1.free;
+                             RemoveInstruction(hp1);
                              result:=true;
                            end
                          else if MatchInstruction(hp1, A_BIC, [taicpu(p).condition], [taicpu(p).oppostfix]) and
@@ -996,8 +975,7 @@
                            begin
                              DebugMsg('Peephole LsrBic2Lsr done', hp1);
                              taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
-                             asml.remove(hp1);
-                             hp1.free;
+                             RemoveInstruction(hp1);
                              result:=true;
                            end;
                        end;
@@ -1028,13 +1006,8 @@
                              taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
                              taicpu(hp1).ops := 3;
 
-                             GetNextInstruction(p,hp1);
+                             RemoveCurrentP(p);
 
-                             asml.Remove(p);
-                             p.Free;
-
-                             p:=hp1;
-
                              result:=true;
                              exit;
                            end
@@ -1048,13 +1021,8 @@
                              taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
                              taicpu(hp1).ops := 3;
 
-                             GetNextInstruction(p,hp1);
+                             RemoveCurrentP(p);
 
-                             asml.Remove(p);
-                             p.Free;
-
-                             p:=hp1;
-
                              result:=true;
                              exit;
                            end
@@ -1071,13 +1039,8 @@
                              taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
                              taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
 
-                             GetNextInstruction(p,hp1);
+                             RemoveCurrentP(p);
 
-                             asml.Remove(p);
-                             p.Free;
-
-                             p:=hp1;
-
                              result:=true;
                              exit;
                            end;
@@ -1155,8 +1118,7 @@
                             begin
                               DebugMsg('Peephole MovStrMov done', hp2);
                               GetNextInstruction(hp2,hp1);
-                              asml.remove(hp2);
-                              hp2.free;
+                              RemoveInstruction(hp2);
                               result:=true;
                               if not assigned(hp1) then break;
                             end
@@ -1173,9 +1135,7 @@
                                 not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
                             begin
                               DebugMsg('Peephole MovMov done', p);
-                              asml.remove(p);
-                              p.free;
-                              p:=hp1;
+                              RemoveCurrentP(p, hp1);
                               GetNextInstruction(hp1,hp1);
                               result:=true;
                               if not assigned(hp1) then
@@ -1232,10 +1192,8 @@
                             asml.InsertAfter(dealloc,hp1);
                           end;
 
-                        GetNextInstruction(p, hp1);
-                        asml.remove(p);
-                        p.free;
-                        p:=hp1;
+                        RemoveCurrentP(p);
+
                         result:=true;
                       end;
 
@@ -1335,12 +1293,8 @@
                                 AllocRegBetween(taicpu(p).oper[2]^.shifterop^.rs,p,hp1,UsedRegs);
                               AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp1,UsedRegs);
                               asml.insertbefore(hp2, hp1);
-                              GetNextInstruction(p, hp2);
-                              asml.remove(p);
-                              asml.remove(hp1);
-                              p.free;
-                              hp1.free;
-                              p:=hp2;
+                              RemoveInstruction(hp1);
+                              RemoveCurrentP(p);
                               DebugMsg('Peephole FoldShiftProcess done', p);
                               Result:=true;
                               break;
@@ -1417,10 +1371,7 @@
                          taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
                          taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
                          DebugMsg('Peephole FoldShiftLdrStr done', hp1);
-                         GetNextInstruction(p, hp1);
-                         asml.remove(p);
-                         p.free;
-                         p:=hp1;
+                         RemoveCurrentP(p);
                          Result:=true;
                        end;
                     {
@@ -1509,10 +1460,7 @@
                                     if hp1=hp2 then
                                       break;
                                   end;
-                                GetNextInstruction(p,hp1);
-                                asml.remove(p);
-                                p.free;
-                                p:=hp1;
+                                RemoveCurrentP(p);
                                 result:=true;
                                 break;
                               end;
@@ -1533,11 +1481,8 @@
                     if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
                       LookForPreindexedPattern(taicpu(p)) then
                       begin
-                        GetNextInstruction(p,hp1);
                         DebugMsg('Peephole Add/Sub to Preindexed done', p);
-                        asml.remove(p);
-                        p.free;
-                        p:=hp1;
+                        RemoveCurrentP(p);
                         Result:=true;
                       end;
                     {
@@ -1601,9 +1546,7 @@
 
                             taicpu(hp1).ops:=4;
 
-                            asml.remove(p);
-                            p.free;
-                            p:=hp1;
+                            RemoveCurrentP(p, hp1);
                           end
                         else
                           begin
@@ -1668,10 +1611,7 @@
                           end
                         else
                           taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
-                        GetNextInstruction(p, hp1);
-                        asml.remove(p);
-                        p.free;
-                        p:=hp1;
+                        RemoveCurrentP(p);
                       end;
                   end;
 {$endif dummy}
@@ -1753,16 +1693,11 @@
                       (taicpu(hp4).oper[1]^.typ = top_regset) and
                       (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
                       begin
-                        asml.Remove(p);
-                        asml.Remove(hp1);
-                        asml.Remove(hp3);
-                        asml.Remove(hp4);
                         taicpu(hp2).opcode:=A_B;
-                        p.free;
-                        hp1.free;
-                        hp3.free;
-                        hp4.free;
-                        p:=hp2;
+                        RemoveInstruction(hp1);
+                        RemoveInstruction(hp3);
+                        RemoveInstruction(hp4);
+                        RemoveCurrentP(p, hp2);
                         DebugMsg('Peephole Bl2B done', p);
                       end;
                   end;
@@ -1785,8 +1720,7 @@
                       MatchOperand(taicpu(p).oper[1]^, taicpu(hp1).oper[0]^) and
                       MatchOperand(taicpu(p).oper[2]^, taicpu(hp1).oper[1]^) then
                       begin
-                        asml.Remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         DebugMsg('Peephole VMovVMov2VMov done', p);
                       end;
                   end;
@@ -1903,8 +1837,7 @@
                                         ignore the label if it was the only usage in the
                                         jump moved away }
                                       tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
-                                      asml.remove(hp2);
-                                      hp2.free;
+                                      RemoveInstruction(hp2);
                                       continue;
                                     end;
                                 end
@@ -1990,12 +1923,10 @@
                                             DebugMsg('Peephole BccB2Cond done',hp3);
                                             { remove Bcc }
                                             tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
-                                            asml.remove(hp3);
-                                            hp3.free;
+                                            RemoveInstruction(hp3);
                                             { remove B }
                                             tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
-                                            asml.remove(hp2);
-                                            hp2.free;
+                                            RemoveInstruction(hp2);
                                             continue;
                                          end;
                                      end;
@@ -2346,8 +2277,7 @@
           DebugMsg('Peephole Stm2Push done', p);
           hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
           AsmL.InsertAfter(hp, p);
-          asml.Remove(p);
-          p:=hp;
+          RemoveCurrentP(p, hp);
           result:=true;
         end
       {else if (p.typ=ait_instruction) and
@@ -2360,9 +2290,7 @@
           DebugMsg('Peephole Str2Push done', p);
           hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
           asml.InsertAfter(hp, p);
-          asml.Remove(p);
-          p.Free;
-          p:=hp;
+          RemoveCurrentP(p, hp);
           result:=true;
         end}
       else if (p.typ=ait_instruction) and
@@ -2374,6 +2302,8 @@
           DebugMsg('Peephole Ldm2Pop done', p);
           hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
           asml.InsertBefore(hp, p);
+          { Best not to use RemoveCurrentP here because hp is before p, and we don't want to change
+            the register state to something unpredictable as a result. [Kit] }
           asml.Remove(p);
           p.Free;
           p:=hp;
@@ -2445,13 +2375,9 @@
 
           asml.InsertAfter(hp2, hp1);
 
-          asml.Remove(hp1);
-          hp1.Free;
-          asml.Remove(p);
-          p.Free;
+          RemoveInstruction(hp1);
+          RemoveCurrentP(p, hp2);
 
-          p := hp2;
-
           result := true;
         end}
     end;
@@ -2538,8 +2464,7 @@
                                       end;
 
                                       tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
-                                      asml.remove(hp2);
-                                      hp2.free;
+                                      RemoveInstruction(hp2);
                                       continue;
                                     end;
                                 end;
Index: compiler/avr/aoptcpu.pas
===================================================================
--- compiler/avr/aoptcpu.pas	(revision 45864)
+++ compiler/avr/aoptcpu.pas	(working copy)
@@ -286,8 +286,7 @@
 
           TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
 
-          asml.remove(hp1);
-          hp1.free;
+          RemoveInstruction(hp1);
         end;
     end;
 
@@ -367,8 +366,7 @@
 
                 DebugMsg('Peephole OpCp2Op performed', p);
 
-                asml.remove(hp1);
-                hp1.free;
+                RemoveInstruction(hp1);
                 Result:=true;
               end
             else
@@ -415,10 +413,8 @@
 
                             if assigned(alloc) and assigned(dealloc) then
                               begin
-                                asml.Remove(alloc);
-                                alloc.Free;
-                                asml.Remove(dealloc);
-                                dealloc.Free;
+                                RemoveInstruction(alloc);
+                                RemoveInstruction(dealloc);
                               end;
 
                             DebugMsg('Peephole LdiOp2Opi performed', p);
@@ -494,10 +490,8 @@
                             taicpu(p).opcode:=A_SBI;
                             taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
                             taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val));
-                            asml.Remove(hp1);
-                            hp1.Free;
-                            asml.Remove(hp2);
-                            hp2.Free;
+                            RemoveInstruction(hp1);
+                            RemoveInstruction(hp2);
                             result:=true;
                           end
                          {
@@ -522,10 +516,8 @@
                             taicpu(p).opcode:=A_CBI;
                             taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
                             taicpu(p).loadconst(1,BsrByte(not(taicpu(hp1).oper[1]^.val)));
-                            asml.Remove(hp1);
-                            hp1.Free;
-                            asml.Remove(hp2);
-                            hp2.Free;
+                            RemoveInstruction(hp1);
+                            RemoveInstruction(hp2);
                             result:=true;
                           end
                          {
@@ -555,8 +547,7 @@
 
                             taicpu(p).loadconst(0,taicpu(p).oper[1]^.val);
                             taicpu(p).loadconst(1,BsrByte(taicpu(hp1).oper[1]^.val));
-                            asml.Remove(hp1);
-                            hp1.Free;
+                            RemoveInstruction(hp1);
 
                             taicpu(hp2).condition:=C_None;
                             if CPUAVR_HAS_JMP_CALL in cpu_capabilities[current_settings.cputype] then
@@ -602,8 +593,7 @@
 
                         DebugMsg('Peephole InSbrx2Sbix performed', p);
 
-                        asml.Remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
 
                         result:=true;
                       end;
@@ -650,8 +640,7 @@
                         else
                           taicpu(p).opcode:=A_SBRC;
 
-                        asml.Remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
 
                         result:=true;
                       end
@@ -748,10 +737,8 @@
 
                         if assigned(alloc) and assigned(dealloc) then
                           begin
-                            asml.Remove(alloc);
-                            alloc.Free;
-                            asml.Remove(dealloc);
-                            dealloc.Free;
+                            RemoveInstruction(alloc);
+                            RemoveInstruction(dealloc);
                           end;
 
                         result:=RemoveCurrentP(p);
@@ -797,15 +784,9 @@
 
                            taicpu(hp3).loadreg(1, taicpu(p).oper[0]^.reg);
 
-                           { We're removing 3 concurrent instructions.  Remove hp1
-                             and hp2 manually instead of calling RemoveCurrentP
-                             as this means we won't be calling UpdateUsedRegs 3 times }
-                           asml.Remove(hp1);
-                           hp1.Free;
+                           RemoveInstruction(hp1);
+                           RemoveInstruction(hp2);
 
-                           asml.Remove(hp2);
-                           hp2.Free;
-
                            { By removing p last, we've guaranteed that p.Next is
                              valid (storing it prior to removing the instructions
                              may result in a dangling pointer if hp1 immediately
@@ -842,10 +823,8 @@
                            IncludeRegInUsedRegs(taicpu(hp3).oper[0]^.reg,UsedRegs);
                            UpdateUsedRegs(tai(p.Next));
 
-                           asml.Remove(hp2);
-                           hp2.Free;
-                           asml.Remove(hp3);
-                           hp3.Free;
+                           RemoveInstruction(hp2);
+                           RemoveInstruction(hp3);
 
                            result:=true;
                          end
@@ -861,8 +840,7 @@
 
                        taicpu(p).opcode:=A_JMP;
 
-                       asml.Remove(hp1);
-                       hp1.Free;
+                       RemoveInstruction(hp1);
 
                        result:=true;
                     end;
@@ -875,8 +853,7 @@
 
                        taicpu(p).opcode:=A_RJMP;
 
-                       asml.Remove(hp1);
-                       hp1.Free;
+                       RemoveInstruction(hp1);
 
                        result:=true;
                     end;
@@ -937,10 +914,8 @@
 
                         if assigned(alloc) and assigned(dealloc) then
                           begin
-                            asml.Remove(alloc);
-                            alloc.Free;
-                            asml.Remove(dealloc);
-                            dealloc.Free;
+                            RemoveInstruction(alloc);
+                            RemoveInstruction(dealloc);
                           end;
 
                         { life range of reg1 is increased }
@@ -1002,14 +977,11 @@
 
                         if assigned(alloc) and assigned(dealloc) then
                           begin
-                            asml.Remove(alloc);
-                            alloc.Free;
-                            asml.Remove(dealloc);
-                            dealloc.Free;
+                            RemoveInstruction(alloc);
+                            RemoveInstruction(dealloc);
                           end;
 
-                        asml.remove(hp2);
-                        hp2.free;
+                        RemoveInstruction(hp2);
 
                         result:=RemoveCurrentP(p);
                       end
@@ -1048,16 +1020,13 @@
 
                         if assigned(alloc) and assigned(dealloc) then
                           begin
-                            asml.Remove(alloc);
-                            alloc.Free;
-                            asml.Remove(dealloc);
-                            dealloc.Free;
+                            RemoveInstruction(alloc);
+                            RemoveInstruction(dealloc);
                           end;
 
                         result:=RemoveCurrentP(p);
 
-                        asml.remove(hp2);
-                        hp2.free;
+                        RemoveInstruction(hp2);
                       end
                     { fold
                       mov reg2,reg0
@@ -1092,8 +1061,7 @@
                           end;
 
                         taicpu(p).opcode:=A_MOVW;
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         result:=true;
                       end
                     {
@@ -1179,12 +1147,9 @@
                         tai_label(hp3).labsym.decrefs;
                         tai_label(hp5).labsym.decrefs;
 
-                        AsmL.remove(hp1);
-                        taicpu(hp1).Free;
+                        RemoveInstruction(hp1);
+                        RemoveInstruction(hp2);
 
-                        AsmL.remove(hp2);
-                        taicpu(hp2).Free;
-
                         result:=true;
                       end;
                   end;
Index: compiler/i386/aoptcpu.pas
===================================================================
--- compiler/i386/aoptcpu.pas	(revision 45864)
+++ compiler/i386/aoptcpu.pas	(working copy)
@@ -168,8 +168,7 @@
                       begin
                         taicpu(p).changeopsize(S_L);
                         taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                       end;
                   end;
Index: compiler/i8086/aoptcpu.pas
===================================================================
--- compiler/i8086/aoptcpu.pas	(revision 45864)
+++ compiler/i8086/aoptcpu.pas	(working copy)
@@ -104,8 +104,7 @@
                         end;
                         if assigned(FindRegDealloc(taicpu(hp2).oper[0]^.reg,tai(hp2.Next))) then
                           begin
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
                             DebugMsg('Peephole optimizer MovMovMov2LXX',p);
                           end
                         else
@@ -114,8 +113,7 @@
                             DebugMsg('Peephole optimizer MovMovMov2LXXMov',p);
                           end;
 
-                        asml.remove(hp2);
-                        hp2.free;
+                        RemoveInstruction(hp2);
                         result:=true;
                       end
                     else if MatchInstruction(p,A_MOV,[S_W]) and
@@ -129,9 +127,7 @@
                         taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
                         { take care of the register (de)allocs following p }
                         UpdateUsedRegs(tai(p.next));
-                        asml.remove(p);
-                        p.free;
-                        p:=hp1;
+                        RemoveCurrentP(p, hp1);
                         result:=true;
                       end;
                   end;
Index: compiler/jvm/aoptcpu.pas
===================================================================
--- compiler/jvm/aoptcpu.pas	(revision 45864)
+++ compiler/jvm/aoptcpu.pas	(working copy)
@@ -62,11 +62,8 @@
         begin
           { can't be the instruction, must end in a return or so }
           next2:=tai(next.next);
-          asml.remove(p);
-          asml.remove(next);
-          p.free;
-          next.free;
-          p:=next2;
+          RemoveInstruction(next);
+          RemoveCurrentP(p, next2);
           result:=true;
         end;
     end;
@@ -114,9 +111,7 @@
       if GetNextInstruction(p,next) and
          OpIsCommutativeSingleSlots(next) then
         begin
-          asml.remove(p);
-          p.free;
-          p:=next;
+          RemoveCurrentP(p, next);
           result:=true;
           exit;
         end;
@@ -165,9 +160,7 @@
           next:=tai(p.next);
           asml.remove(prev2);
           asml.InsertAfter(prev2,prev1);
-          asml.remove(p);
-          p.free;
-          p:=next;
+          RemoveCurrentP(p, next);
           result:=true;
         end;
     end;
Index: compiler/m68k/aoptcpu.pas
===================================================================
--- compiler/m68k/aoptcpu.pas	(revision 45864)
+++ compiler/m68k/aoptcpu.pas	(working copy)
@@ -211,8 +211,7 @@
                   begin
                     {  move %reg0, %tmpreg; move %tmpreg, <ea> -> move %reg0, <ea> }
                     taicpu(p).loadOper(1,taicpu(next).oper[1]^);
-                    asml.remove(next);
-                    next.free;
+                    RemoveInstruction(next);
                     result:=true;
                     { also remove leftover move %reg0, %reg0, which can occur as the result
                       of the previous optimization, if %reg0 and %tmpreg was different types
@@ -220,10 +219,7 @@
                     if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
                       begin
                         DebugMsg('Optimizer: '+opstr+' + '+opstr+' removed',p);
-                        GetNextInstruction(p,next);
-                        asml.remove(p);
-                        p.free;
-                        p:=next;
+                        RemoveCurrentP(p);
                       end
                     else
                       DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #1',p)
@@ -247,8 +243,7 @@
                         begin
                           DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #3',p);
                           taicpu(p).loadOper(1,taicpu(next).oper[1]^);
-                          asml.remove(next);
-                          next.free;
+                          RemoveInstruction(next);
                           result:=true;
                         end;
                   end;
@@ -291,10 +286,8 @@
                   Disabled, because it breaks some tests... :( (KB)
                   DebugMsg('Optimizer: '+opstr+' + OP + '+opstr+' to OP #1',next);
                   taicpu(next).loadOper(taicpu(next).ops-1,taicpu(p).oper[0]^);
-                  asml.remove(p);
-                  asml.remove(next2);
-                  p.free;
-                  next2.free;
+                  RemoveInstruction(next2);
+                  RemoveCurrentP(p, next);
                   result:=true;
                   }
                 end;
@@ -326,10 +319,7 @@
                    (taicpu(p).oper[0]^.ref^.offset = 0) then
                   begin
                     DebugMsg('Optimizer: LEA 0(Ax),Ax removed',p);
-                    GetNextInstruction(p,next);
-                    asml.remove(p);
-                    p.free;
-                    p:=next;
+                    RemoveCurrentP(p);
                     result:=true;
                   end;
               { Address register sub/add can be replaced with ADDQ/SUBQ or LEA if the value is in the
Index: compiler/mips/aoptcpu.pas
===================================================================
--- compiler/mips/aoptcpu.pas	(revision 45864)
+++ compiler/mips/aoptcpu.pas	(working copy)
@@ -285,8 +285,7 @@
               alloc:=FindRegAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
               if assigned(alloc) then
                 begin
-                  asml.Remove(alloc);
-                  alloc.free;
+                  RemoveInstruction(alloc);
                   dealloc.free;
                 end
               else
@@ -306,8 +305,7 @@
               { finally get rid of the mov }
               taicpu(p).loadreg(0,taicpu(next).oper[0]^.reg);
               DebugMsg('Peephole: Move removed 1',next);
-              asml.remove(next);
-              next.free;
+              RemoveInstruction(next);
               result:=true;
             end
           else       // no dealloc found
@@ -329,8 +327,7 @@
                 begin
                   taicpu(p).loadreg(0,taicpu(next).oper[0]^.reg);
                   DebugMsg('Peephole: Move removed 2',next);
-                  asml.remove(next);
-                  next.free;
+                  RemoveInstruction(next);
                   result:=true;
                 end;
             end;
@@ -349,9 +346,7 @@
         begin
           next.loadreg(0,taicpu(p).oper[1]^.reg);
           DebugMsg('Peephole: Move removed 3',p);
-          asml.remove(p);
-          p.free;
-          p:=next;
+          RemoveCurrentP(p, next);
         end;
     end;
 
@@ -369,9 +364,7 @@
           AllocRegBetween(taicpu(p).oper[1]^.reg,p,next,UsedRegs);
           next.oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
           DebugMsg('Peephole: Move removed 4',p);
-          asml.remove(p);
-          p.free;
-          p:=next;
+          RemoveCurrentP(p, next);
         end;
     end;
 
@@ -407,9 +400,7 @@
                           taicpu(next).fileinfo:=taicpu(p).fileinfo;
                           asml.insertbefore(next,p);
                         end;
-                      asml.remove(p);
-                      p.Free;
-                      p:=next;
+                      RemoveCurrentP(p, next);
                       result:=true;
                     end;
                 end;
@@ -423,9 +414,7 @@
                     Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) then
                     begin
                       taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
-                      asml.remove(p);
-                      p.free;
-                      p:=next;
+                      RemoveCurrentP(p, next);
                       result:=true;
                     end
                   else
@@ -456,11 +445,8 @@
                       if Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next2.next))) then
                         begin
                           taicpu(next2).loadreg(0,taicpu(p).oper[1]^.reg);
-                          asml.remove(p);
-                          asml.remove(next);
-                          p.free;
-                          next.free;
-                          p:=next2;
+                          RemoveInstruction(next);
+                          RemoveCurrentP(p, next2);
                           result:=true;
                         end;
                     end
@@ -491,8 +477,7 @@
                       (taicpu(next).oper[2]^.val=255)
                     )) then
                     begin
-                      asml.remove(next);
-                      next.free;
+                      RemoveInstruction(next);
                       result:=true;
                     end
                   else
@@ -520,10 +505,8 @@
                       (taicpu(next).oper[2]^.val=24)
                     )) then
                     begin
-                      asml.remove(next);
-                      asml.remove(next2);
-                      next.free;
-                      next2.free;
+                      RemoveInstruction(next);
+                      RemoveInstruction(next2);
                       result:=true;
                     end
                   { Remove zero extension if register is used only for byte/word memory store }
@@ -536,9 +519,7 @@
                     assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) then
                     begin
                       taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
-                      asml.remove(p);
-                      p.free;
-                      p:=next;
+                      RemoveCurrentP(p, next);
                       result:=true;
                     end
                   else
@@ -584,9 +565,7 @@
                           if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
                             begin
                               taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p, next);
                               result:=true;
                             end
                           { TODO: if Ry=NR_R0, this effectively changes instruction into MOVE,
@@ -594,9 +573,7 @@
                           else if MatchOperand(taicpu(next).oper[2]^,taicpu(p).oper[0]^.reg) then
                             begin
                               taicpu(next).loadreg(2,taicpu(p).oper[1]^.reg);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p, next);
                               result:=true;
                             end;
                         end
@@ -608,17 +585,13 @@
                           if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
                             begin
                               taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p, next);
                               result:=true;
                             end
                           else if MatchOperand(taicpu(next).oper[2]^,taicpu(p).oper[0]^.reg) then
                             begin
                               taicpu(next).loadreg(2,taicpu(p).oper[1]^.reg);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p, next);
                               result:=true;
                             end;
                         end
@@ -631,9 +604,7 @@
                               taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
                               if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
                                 taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p, next);
                               result:=true;
                             end
                           else if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
@@ -641,9 +612,7 @@
                               taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
                               if MatchOperand(taicpu(next).oper[0]^,taicpu(p).oper[0]^.reg) then
                                 taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p, next);
                               result:=true;
                             end;
                         end
@@ -652,10 +621,7 @@
                           { Ended up with move between same register? Suicide then. }
                           if (taicpu(p).oper[0]^.reg=taicpu(p).oper[1]^.reg) then
                             begin
-                              GetNextInstruction(p,next);
-                              asml.remove(p);
-                              p.free;
-                              p:=next;
+                              RemoveCurrentP(p);
                               result:=true;
                             end;
                         end;
@@ -693,9 +659,7 @@
                         InternalError(2014100401);
                       end;
                       taicpu(next).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
-                      asml.remove(p);
-                      p.free;
-                      p:=next;
+                      RemoveCurrentP(p, next);
                       result:=true;
                     end
                   else
@@ -824,8 +788,7 @@
                                         jump moved away }
                                       tasmlabel(taicpu(hp2).oper[taicpu(hp2).ops-1]^.ref^.symbol).decrefs;
                                       RemoveDelaySlot(hp2);
-                                      asml.remove(hp2);
-                                      hp2.free;
+                                      RemoveInstruction(hp2);
                                       continue;
                                     end;
                                 end
@@ -890,15 +853,13 @@
                                           { remove bCC }
                                           tasmlabel(taicpu(hp3).oper[taicpu(hp3).ops-1]^.ref^.symbol).decrefs;
                                           RemoveDelaySlot(hp3);
-                                          asml.remove(hp3);
-                                          hp3.free;
+                                          RemoveInstruction(hp3);
                                           { remove jmp }
                                           if (p=hp2) then
                                             GetNextInstruction(hp2,p);
                                           tasmlabel(taicpu(hp2).oper[taicpu(hp2).ops-1]^.ref^.symbol).decrefs;
                                           RemoveDelaySlot(hp2);
-                                          asml.remove(hp2);
-                                          hp2.free;
+                                          RemoveInstruction(hp2);
                                           continue;
                                         end;
                                     end;
Index: compiler/powerpc/aoptcpu.pas
===================================================================
--- compiler/powerpc/aoptcpu.pas	(revision 45864)
+++ compiler/powerpc/aoptcpu.pas	(working copy)
@@ -71,10 +71,8 @@
                p.loadreg(1,p.oper[0]^.reg);
                p.loadreg(0,next1.oper[0]^.reg);
                p.loadconst(2,31);
-               asml.remove(next1);
-               next1.free;
-               asml.remove(next2);
-               next2.free;
+               RemoveInstruction(next1);
+               RemoveInstruction(next2);
              end
            else
              result := false;
@@ -124,13 +122,10 @@
                  end
                else
                  begin
-                   asml.remove(next1);
-                   next1.free;
-                   asml.remove(next3);
-                   next3.free;
+                   RemoveInstruction(next1);
+                   RemoveInstruction(next3);
                  end;
-               asml.remove(next2);
-               next2.free;
+               RemoveInstruction(next2);
              end
            else
              begin
@@ -144,8 +139,7 @@
                 next1.opcode := A_SRWI;
                 next1.loadreg(1,next1.oper[0]^.reg);
                 next1.loadconst(2,5);
-                asml.remove(next2);
-                next2.free;
+                RemoveInstruction(next2);
               end;
          end;
         else
@@ -221,9 +215,7 @@
                           else
                             ;
                         end;
-                      asml.remove(p);
-                      p.free;
-                      p := next1;
+                      RemoveCurrentP(p, next1);
                       result := true;
                     end;
                 end;
@@ -273,8 +265,7 @@
                               taicpu(p).loadconst(3,0);
                               taicpu(p).loadconst(4,31-taicpu(next1).oper[2]^.val);
                             end;
-                          asml.remove(next1);
-                          next1.free;
+                          RemoveInstruction(next1);
                           result := true;
                         end;
                       A_RLWINM:
@@ -333,9 +324,7 @@
                             if (taicpu(p).oper[2]^.val = 0) then
                               begin
                                 next1 := tai(p.next);
-                                asml.remove(p);
-                                p.free;
-                                p := next1;
+                                RemoveCurrentP(p, next1);
                                 result := true;
                               end;
                           end;
@@ -349,8 +338,7 @@
                             taicpu(p).freeop(4);
                             taicpu(p).ops := 2;
                             taicpu(p).opercnt := 2;
-                            asml.remove(next1);
-                            next1.free;
+                            RemoveInstruction(next1);
                             result := true;
                           end
                         else if tcgppc(cg).get_rlwi_const(l1,l1,l2) then
@@ -358,8 +346,7 @@
                             taicpu(p).oper[2]^.val := (taicpu(p).oper[2]^.val + shlcount) and 31;
                             taicpu(p).oper[3]^.val := l1;
                             taicpu(p).oper[4]^.val := l2;
-                            asml.remove(next1);
-                            next1.free;
+                            RemoveInstruction(next1);
                             result := true;
                           end;
                       end;
@@ -497,8 +484,7 @@
                  (taicpu(next1).oper[1]^.reg = taicpu(p).oper[0]^.reg))) and
                changetomodifyflags(taicpu(p)) then
               begin
-                asml.remove(next1);
-                next1.free;
+                RemoveInstruction(next1);
                 result := true;
               end;
           end;
Index: compiler/riscv/aoptcpurv.pas
===================================================================
--- compiler/riscv/aoptcpurv.pas	(revision 45864)
+++ compiler/riscv/aoptcpurv.pas	(working copy)
@@ -184,8 +184,7 @@
         if moveback and (not GetLastInstruction(orig,n)) then
           GetNextInstruction(orig,n);
 
-        AsmL.Remove(orig);
-        orig.Free;
+        RemoveInstruction(orig);
 
         orig:=n;
       end;
Index: compiler/sparcgen/aoptcpu.pas
===================================================================
--- compiler/sparcgen/aoptcpu.pas	(revision 45864)
+++ compiler/sparcgen/aoptcpu.pas	(working copy)
@@ -210,8 +210,7 @@
               alloc:=FindRegAlloc(taicpu(p).oper[2]^.reg,tai(hp1.Next));
               if assigned(alloc) then
                 begin
-                  asml.Remove(alloc);
-                  alloc.free;
+                  RemoveInstruction(alloc);
                   dealloc.free;
                 end
               else
@@ -231,8 +230,7 @@
               { finally get rid of the mov }
               taicpu(p).loadreg(2,taicpu(next).oper[1]^.reg);
               DebugMsg('Peephole OpMov2Op done',p);
-              asml.remove(next);
-              next.free;
+              RemoveInstruction(next);
             end;
         end;
     end;
@@ -272,11 +270,8 @@
                         begin
                           taicpu(next2).loadreg(0,taicpu(p).oper[0]^.reg);
                           DebugMsg('Peephole SLLSRxSTH2STH done',next2);
-                          asml.remove(p);
-                          asml.remove(next);
-                          p.free;
-                          next.free;
-                          p:=next2;
+                          RemoveInstruction(next);
+                          RemoveCurrentP(p, next2);
                         end;
                     end
                   else
@@ -309,11 +304,8 @@
                         begin
                           taicpu(next2).loadreg(0,taicpu(p).oper[0]^.reg);
                           DebugMsg('Peephole SLLXSRxXST2ST done',next2);
-                          asml.remove(p);
-                          asml.remove(next);
-                          p.free;
-                          next.free;
-                          p:=next2;
+                          RemoveInstruction(next);
+                          RemoveCurrentP(p,next2);
                         end;
                     end
                   else
@@ -329,9 +321,7 @@
                     (taicpu(p).oper[2]^.reg=NR_G0) then
                     begin
                       next:=tai(p.next);
-                      asml.remove(p);
-                      p.free;
-                      p:=next;
+                      RemoveCurrentP(p, next);
                     end
                   { kill zero extension after right shift (e.g. happens with "high(dword)")}
                   else if (taicpu(p).oper[1]^.typ=top_const) and
@@ -347,10 +337,8 @@
                     (taicpu(next).oper[1]^.val=taicpu(next2).oper[1]^.val) and
                     (taicpu(next).oper[1]^.val=16) then
                     begin
-                      asml.remove(next);
-                      asml.remove(next2);
-                      next.free;
-                      next2.free;
+                      RemoveInstruction(next);
+                      RemoveInstruction(next2);
                     end
                   else
                     TryRemoveMov(p,A_MOV);
@@ -377,10 +365,8 @@
                       (taicpu(next).oper[1]^.val=24)
                     )) then
                     begin
-                      asml.remove(next);
-                      asml.remove(next2);
-                      next.free;
-                      next2.free;
+                      RemoveInstruction(next);
+                      RemoveInstruction(next2);
                     end
                   else if (taicpu(p).oper[1]^.typ=top_const) and
                     (taicpu(p).oper[1]^.val=255) and
@@ -394,9 +380,7 @@
                       if not RegUsedAfterInstruction(taicpu(p).oper[2]^.reg,next,TmpUsedRegs) then
                         begin
                           taicpu(next).loadreg(0,taicpu(p).oper[0]^.reg);
-                          asml.remove(p);
-                          p.free;
-                          p:=next;
+                          RemoveCurrentP(p, next);
                         end;
                     end
                   else
Index: compiler/symdef.pas
===================================================================
--- compiler/symdef.pas	(revision 45864)
+++ compiler/symdef.pas	(working copy)
@@ -2352,9 +2352,10 @@
                  not trecorddef(self).contains_cross_aword_field and
                  { records cannot go into registers on 16 bit targets for now }
                  (sizeof(aint)>2) and
-                 not trecorddef(self).contains_float_field
-                ) and
-                not needs_inittable;
+                 (not trecorddef(self).contains_float_field) or
+                  (recsize <= sizeof(aint))
+                 ) and
+                 not needs_inittable;
 {$endif cpuhighleveltarget}
             end;
           else
Index: compiler/symsym.pas
===================================================================
--- compiler/symsym.pas	(revision 45864)
+++ compiler/symsym.pas	(working copy)
@@ -1728,8 +1728,6 @@
 
 
     function tabstractvarsym.is_regvar(refpara: boolean):boolean;
-      var
-        tempdef : tdef;
       begin
         { Register variables are not allowed in the following cases:
            - regvars are disabled
@@ -1748,7 +1746,6 @@
 {$if not defined(powerpc) and not defined(powerpc64)}
                 and ((vardef.typ <> recorddef) or
                      (varregable = vr_addr) or
-                     tabstractrecordsymtable(tabstractrecorddef(vardef).symtable).has_single_field(tempdef) or
                      not(varstate in [vs_written,vs_readwritten]));
 {$endif}
       end;
Index: compiler/x86/aoptx86.pas
===================================================================
--- compiler/x86/aoptx86.pas	(revision 45864)
+++ compiler/x86/aoptx86.pas	(working copy)
@@ -1086,8 +1086,7 @@
                   else
                     Internalerror(2017050701)
                 end;
-                asml.remove(hp1);
-                hp1.free;
+                RemoveInstruction(hp1);
               end;
           end;
       end;
@@ -1379,8 +1378,7 @@
                (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
                (getsupreg(tai_regalloc(hp2).reg) = supreg) then
               begin
-                asml.remove(hp2);
-                hp2.free;
+                RemoveInstruction(hp2);
                 break;
               end;
           until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
@@ -1440,8 +1438,7 @@
                       begin
                         DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 1',p);
                         taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         result:=true;
                         exit;
                       end
@@ -1453,8 +1450,7 @@
                     else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
                       begin
                         DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 2',p);
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         result:=true;
                         exit;
                       end
@@ -1478,8 +1474,7 @@
                       DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVS*2(V)MOVS* 1',p);
                       taicpu(p).opcode:=taicpu(hp1).opcode;
                       taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
-                      asml.Remove(hp1);
-                      hp1.Free;
+                      RemoveInstruction(hp1);
                       result:=true;
                       exit;
                     end
@@ -1562,8 +1557,7 @@
                       begin
                         taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
                         RemoveCurrentP(p, hp1); // <-- Is this actually safe? hp1 is not necessarily the next instruction. [Kit]
-                        asml.Remove(hp2);
-                        hp2.Free;
+                        RemoveInstruction(hp2);
                       end;
                   end
                 else if (hp1.typ = ait_instruction) and
@@ -1602,8 +1596,7 @@
                           RemoveCurrentP(p, nil);
                         p:=hp1;
                         taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
-                        asml.remove(hp2);
-                        hp2.Free;
+                        RemoveInstruction(hp2);
                         result:=true;
                       end;
                   end;
@@ -1639,8 +1632,7 @@
               begin
                 taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
                 DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
-                asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -1910,9 +1902,7 @@
                   begin
                     { We can remove the original MOV }
                     DebugMsg(SPeepholeOptimization + 'Mov2Nop 3 done',p);
-                    Asml.Remove(p);
-                    p.Free;
-                    p := hp1;
+                    RemoveCurrentP(p, hp1);
 
                     { TmpUsedRegs contains the results of "UpdateUsedRegs(tai(p.Next))" already,
                       so just restore it to UsedRegs instead of calculating it again }
@@ -1980,8 +1970,7 @@
                       begin
                         GetNextInstruction_p := GetNextInstruction(hp1, hp2);
                         DebugMsg(SPeepholeOptimization + 'Mov2Nop 4 done',hp1);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
 
                         { The instruction after what was hp1 is now the immediate next instruction,
                           so we can continue to make optimisations if it's present }
@@ -2068,8 +2057,7 @@
                               InternalError(2020021001);
                           end;
                           DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 2 done',p);
-                          asml.Remove(hp1);
-                          hp1.Free;
+                          RemoveInstruction(hp1);
                           Result := True;
                           Exit;
                         end;
@@ -2124,8 +2112,7 @@
                             and ffffffffh, %reg
                         }
                         DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end;
@@ -2137,8 +2124,7 @@
                             and ffffffffffffffffh, %reg
                         }
                         DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end;
@@ -2163,8 +2149,7 @@
                         DebugMsg(SPeepholeOptimization + 'MovAndTest2Test done',p);
                         taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
                         taicpu(hp1).opcode:=A_TEST;
-                        asml.Remove(hp2);
-                        hp2.free;
+                        RemoveInstruction(hp2);
                         RemoveCurrentP(p, hp1);
                         Result:=true;
                         exit;
@@ -2263,8 +2248,7 @@
                         DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
                           ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
 
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                       end;
 
                     Result := True;
@@ -2313,8 +2297,7 @@
                                 AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
                               taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
                               DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
-                              asml.remove(hp1);
-                              hp1.free;
+                              RemoveInstruction(hp1);
                               Result:=true;
                               Exit;
                             end;
@@ -2332,8 +2315,7 @@
                             }
                             taicpu(p).loadreg(1, taicpu(hp1).oper[1]^.reg);
                             DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
                             Result:=true;
                             Exit;
                           end;
@@ -2376,8 +2358,7 @@
                         if taicpu(p).oper[0]^.typ=top_reg then
                           AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
                         DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end
@@ -2400,8 +2381,7 @@
                              cmp mem1, reg1
                            }
                           begin
-                            asml.remove(hp2);
-                            hp2.free;
+                            RemoveInstruction(hp2);
                             taicpu(hp1).opcode := A_CMP;
                             taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
                             taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
@@ -2440,8 +2420,7 @@
                           DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
                           taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
                           taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
-                          asml.remove(hp2);
-                          hp2.free;
+                          RemoveInstruction(hp2);
                         end
 {$ifdef i386}
                       { this is enabled for i386 only, as the rules to create the reg sets below
@@ -2485,10 +2464,7 @@
                           AllocRegBetween(taicpu(p).oper[1]^.reg,p,hp2,usedregs);
                         end
                       else
-                        begin
-                          asml.remove(hp2);
-                          hp2.free;
-                        end
+                        RemoveInstruction(hp2)
 {$endif i386}
                         ;
                   end;
@@ -2598,13 +2574,11 @@
                         begin
                           DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + RegName1 + '; removed unnecessary instruction (MovMov2MovNop 6b}',hp2);
                           AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
-                          asml.remove(hp2);
-                          hp2.Free;
+                          RemoveInstruction(hp2);
                         end
                       else
                         begin
-                          asml.remove(hp2);
-                          hp2.Free;
+                          RemoveInstruction(hp2);
 
                           { We can remove the original MOV too }
                           DebugMsg(SPeepholeOptimization + 'MovMov2NopNop 6b done',p);
@@ -2695,8 +2669,7 @@
                         and ffffffffh, %reg
                     }
                     DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 3 done',p);
-                    asml.remove(hp2);
-                    hp2.free;
+                    RemoveInstruction(hp2);
                     Result:=true;
                     exit;
                   end;
@@ -2728,9 +2701,7 @@
             )
           ) then
           begin
-            asml.remove(p);
-            p.free;
-            p:=hp1;
+            RemoveCurrentP(p, hp1);
             DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
             RemoveLastDeallocForFuncRes(p);
             Result:=true;
@@ -2822,8 +2793,7 @@
                       ->
                         decw    %si             addw    %dx,%si       p
                     }
-                    asml.remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
                     RemoveCurrentP(p, hp1);
                     Result:=True;
                     Exit;
@@ -2907,8 +2877,7 @@
                       ->
                         decw    %si             addw    %dx,%si       p
                     }
-                    asml.remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
                   end;
               end;
           end;
@@ -2926,9 +2895,7 @@
             Taicpu(hp2).opcode:=A_MOV;
             asml.remove(hp1);
             insertllitem(hp2,hp2.next,hp1);
-            asml.remove(p);
-            p.free;
-            p:=hp1;
+            RemoveCurrentP(p, hp1);
             Result:=true;
             exit;
           end;
@@ -2956,9 +2923,7 @@
                 Taicpu(hp1).opcode:=A_ADD;
                 Taicpu(hp1).oper[0]^.ref^:=Taicpu(p).oper[0]^.ref^;
                 DebugMsg(SPeepholeOptimization + 'MovLea2Add done',hp1);
-                asml.remove(p);
-                p.free;
-                p:=hp1;
+                RemoveCurrentP(p, hp1);
                 result:=true;
                 exit;
               end;
@@ -2968,7 +2933,7 @@
 
    function TX86AsmOptimizer.OptPass1MOVXX(var p : tai) : boolean;
       var
-        hp1 : tai;
+        hp1,hp2 : tai;
       begin
         Result:=false;
         if taicpu(p).ops <> 2 then
@@ -3001,15 +2966,13 @@
                         if (taicpu(p).oper[1]^.typ=top_reg) and
                           not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
                           begin
-                            asml.remove(p);
-                            p.free;
-                            GetNextInstruction(hp1,p);
+                            GetNextInstruction(hp1,hp2);
+                            RemoveCurrentP(p, hp2);
                             DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
                           end
                         else
                           DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end
@@ -3048,8 +3011,7 @@
                 taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
                 taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
                 DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
-                asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -3146,8 +3108,7 @@
               begin
                 taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
                 DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
-                asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -3306,8 +3267,7 @@
                 MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
                 begin
                   taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
-                  asml.remove(hp1);
-                  hp1.free;
+                  RemoveInstruction(hp1);
                 end;
              A_SUB:
                if MatchOpType(taicpu(hp1),top_const,top_reg) and
@@ -3314,8 +3274,7 @@
                  MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
                  begin
                    taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
-                   asml.remove(hp1);
-                   hp1.free;
+                   RemoveInstruction(hp1);
                  end;
              A_ADD:
                begin
@@ -3323,13 +3282,11 @@
                    MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
                    begin
                      taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
-                     asml.remove(hp1);
-                     hp1.free;
+                     RemoveInstruction(hp1);
                      if (taicpu(p).oper[0]^.val = 0) then
                        begin
                          hp1 := tai(p.next);
-                         asml.remove(p);
-                         p.free;
+                         RemoveCurrentP(p, hp1);
                          if not GetLastInstruction(hp1, p) then
                            p := hp1;
                          DoSubAddOpt := True;
@@ -3377,9 +3334,7 @@
                     if taicpu(hp1).oper[0]^.typ=top_reg then
                       setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
                     hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := hp1;
+                    RemoveCurrentP(p, hp1);
                     Result:=true;
                     exit;
                   end;
@@ -3449,8 +3404,7 @@
                         if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
                           tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
                         TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                       end
                   end
                 else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
@@ -3465,8 +3419,7 @@
                       else
                         internalerror(2019050536);
                     end;
-                    asml.remove(hp1);
-                    hp1.free;
+                    RemoveInstruction(hp1);
                   end
                 else
                   if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
@@ -3487,8 +3440,7 @@
                         else
                           internalerror(2019050535);
                       end;
-                      asml.remove(hp1);
-                      hp1.free;
+                      RemoveInstruction(hp1);
                     end;
               end;
             if TmpBool2
@@ -3544,136 +3496,12 @@
              end;
           end
 {$endif x86_64}
-        else if
-          GetNextInstruction(p, hp1) and (hp1.typ = ait_instruction) and MatchOpType(taicpu(hp1), top_const, top_reg) and
-          (
-            (
-              MatchInstruction(hp1, A_AND, [taicpu(p).opsize]) and
-              SetAndTest(hp1, hp2)
-{$ifdef x86_64}
-            ) or
-            (
-              MatchInstruction(hp1, A_MOV, [taicpu(p).opsize]) and
-              GetNextInstruction(hp1, hp2) and
-              MatchInstruction(hp2, A_AND, [taicpu(p).opsize]) and
-              MatchOpType(taicpu(hp2), top_reg, top_reg) and
-              (taicpu(hp1).oper[1]^.reg = taicpu(hp2).oper[0]^.reg)
-{$endif x86_64}
-            )
-          ) and
-          (taicpu(p).oper[1]^.reg = taicpu(hp2).oper[1]^.reg) then
-          begin
-            { Change:
-                shl x, %reg1
-                mov -(1<<x), %reg2
-                and %reg2, %reg1
-
-              Or:
-                shl x, %reg1
-                and -(1<<x), %reg1
-
-              To just:
-                shl x, %reg1
-
-              Since the and operation only zeroes bits that are already zero from the shl operation
-            }
-            case taicpu(p).oper[0]^.val of
-               8:
-                 mask:=$FFFFFFFFFFFFFF00;
-               16:
-                 mask:=$FFFFFFFFFFFF0000;
-               32:
-                 mask:=$FFFFFFFF00000000;
-               63:
-                 { Constant pre-calculated to prevent overflow errors with Int64 }
-                 mask:=$8000000000000000;
-               else
-                 begin
-                   if taicpu(p).oper[0]^.val >= 64 then
-                     { Shouldn't happen realistically, since the register
-                       is guaranteed to be set to zero at this point }
-                     mask := 0
-                   else
-                     mask := -(Int64(1 shl taicpu(p).oper[0]^.val));
-                 end;
-            end;
-
-            if taicpu(hp1).oper[0]^.val = mask then
-              begin
-                { Everything checks out, perform the optimisation, as long as
-                  the FLAGS register isn't being used}
-                TransferUsedRegs(TmpUsedRegs);
-                UpdateUsedRegs(TmpUsedRegs, tai(p.next));
-
-{$ifdef x86_64}
-                if (hp1 <> hp2) then
-                  begin
-                    { "shl/mov/and" version }
-                    UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
-
-                    { Don't do the optimisation if the FLAGS register is in use }
-                    if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp2, TmpUsedRegs)) then
-                      begin
-                        DebugMsg(SPeepholeOptimization + 'ShlMovAnd2Shl', p);
-                        { Don't remove the 'mov' instruction if its register is used elsewhere }
-                        if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
-                          begin
-                            asml.Remove(hp1);
-                            hp1.Free;
-                            Result := True;
-                          end;
-
-                        { Only set Result to True if the 'mov' instruction was removed }
-                        asml.Remove(hp2);
-                        hp2.Free;
-                      end;
-                  end
-                else
-{$endif x86_64}
-                  begin
-                    { "shl/and" version }
-
-                    { Don't do the optimisation if the FLAGS register is in use }
-                    if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
-                      begin
-                        DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
-                        asml.Remove(hp1);
-                        hp1.Free;
-                        Result := True;
-                      end;
-                  end;
-
-                Exit;
-              end
-            else {$ifdef x86_64}if (hp1 = hp2) then{$endif x86_64}
-              begin
-                { Even if the mask doesn't allow for its removal, we might be
-                  able to optimise the mask for the "shl/and" version, which
-                  may permit other peephole optimisations }
-{$ifdef DEBUG_AOPTCPU}
-                mask := taicpu(hp1).oper[0]^.val and mask;
-                if taicpu(hp1).oper[0]^.val <> mask then
-                  begin
-                    DebugMsg(
-                      SPeepholeOptimization +
-                      'Changed mask from $' + debug_tostr(taicpu(hp1).oper[0]^.val) +
-                      ' to $' + debug_tostr(mask) +
-                      'based on previous instruction (ShlAnd2ShlAnd)', hp1);
-                    taicpu(hp1).oper[0]^.val := mask;
-                  end;
-{$else DEBUG_AOPTCPU}
-                { If debugging is off, just set the operand even if it's the same }
-                taicpu(hp1).oper[0]^.val := taicpu(hp1).oper[0]^.val and mask;
-{$endif DEBUG_AOPTCPU}
-              end;
-
-          end;
       end;
 
 
     function TX86AsmOptimizer.OptPass1SETcc(var p: tai): boolean;
       var
-        hp1,hp2,next: tai; SetC, JumpC: TAsmCond; Unconditional: Boolean;
+        hp1,hp2: tai; SetC, JumpC: TAsmCond; Unconditional: Boolean;
       begin
         Result:=false;
 
@@ -3701,10 +3529,8 @@
             jne    label
           }
           begin
-            next := tai(p.Next);
-
             TransferUsedRegs(TmpUsedRegs);
-            UpdateUsedRegs(TmpUsedRegs, next);
+            UpdateUsedRegs(TmpUsedRegs, tai(p.next));
             UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
 
             JumpC := taicpu(hp2).condition;
@@ -3728,8 +3554,7 @@
                   Exit;
               end;
 
-            asml.Remove(hp1);
-            hp1.Free;
+            RemoveInstruction(hp1);
 
             if Unconditional then
               MakeUnconditional(taicpu(hp2))
@@ -3743,11 +3568,8 @@
 
             if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
               begin
-                asml.Remove(p);
-                UpdateUsedRegs(next);
-                p.Free;
+                RemoveCurrentP(p, hp2);
                 Result := True;
-                p := hp2;
               end;
 
             DebugMsg(SPeepholeOptimization + 'SETcc/TESTCmp/Jcc -> Jcc',p);
@@ -3782,11 +3604,8 @@
                    (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
                (taicpu(p).oper[0]^.ref^.index = NR_NO) then
               begin
-                asml.remove(p);
-                asml.remove(hp1);
-                p.free;
-                hp1.free;
-                p := hp2;
+                RemoveInstruction(hp1);
+                RemoveCurrentP(p, hp2);
                 RemoveLastDeallocForFuncRes(p);
                 Result := true;
               end
@@ -3799,8 +3618,7 @@
                   if (taicpu(p).opcode = A_FSTP) then
                     taicpu(p).opcode := A_FST
                   else taicpu(p).opcode := A_FIST;
-                  asml.remove(hp1);
-                  hp1.free;
+                  RemoveInstruction(hp1);
                 end
             *)
           end;
@@ -3840,9 +3658,7 @@
                     end;
                     taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
                     taicpu(hp1).oper[1]^.reg := NR_ST;
-                    asml.remove(p);
-                    p.free;
-                    p := hp1;
+                    RemoveCurrentP(p, hp1);
                     Result:=true;
                     exit;
                   end;
@@ -3870,9 +3686,7 @@
                   faddp/                       fmul     st, st
                   fmulp  st, st1 (hp2) }
                 begin
-                  asml.remove(p);
-                  p.free;
-                  p := hp1;
+                  RemoveCurrentP(p, hp1);
                   if (taicpu(hp2).opcode = A_FADDP) then
                     taicpu(hp2).opcode := A_FADD
                   else
@@ -3907,8 +3721,7 @@
                         else
                           internalerror(2019050533);
                       end;
-                      asml.remove(hp2);
-                      hp2.free;
+                      RemoveInstruction(hp2);
                     end
                   else
                     ;
@@ -3958,8 +3771,7 @@
                              begin
                                DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
                                TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
-                               AsmL.Remove(hp1);
-                               hp1.Free;
+                               RemoveInstruction(hp1);
                                { Since hp1 was deleted, hp2 must not be updated }
                                Continue;
                              end
@@ -4124,8 +3936,7 @@
          MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
          begin
            DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
-           asml.Remove(hp1);
-           hp1.Free;
+           RemoveInstruction(hp1);
            Result:=true;
            Exit;
          end;
@@ -4152,8 +3963,7 @@
          MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
          begin
            DebugMsg(SPeepholeOptimization + 'VPXorVPXor2PXor done',hp1);
-           asml.Remove(hp1);
-           hp1.Free;
+           RemoveInstruction(hp1);
            Result:=true;
            Exit;
          end;
@@ -4251,8 +4061,7 @@
                 taicpu(p).opcode := A_LEA;
                 taicpu(p).loadref(0, NewRef);
 
-                Asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
 
                 Result := True;
                 Exit;
@@ -4282,9 +4091,7 @@
               not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
             then
               begin
-                asml.remove(p);
-                p.free;
-                p := hp1;
+                RemoveCurrentP(p, hp1);
                 Result:=true;
               end;
 
@@ -4323,12 +4130,9 @@
                 AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
                 taicpu(hp1).opcode := A_XCHG;
 
-                asml.Remove(p);
-                asml.Remove(hp2);
-                p.Free;
-                hp2.Free;
+                RemoveInstruction(hp2);
+                RemoveCurrentP(p, hp1);
 
-                p := hp1;
                 Result := True;
                 Exit;
               end;
@@ -4352,8 +4156,7 @@
                             cltd
                         }
                         DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
-                        Asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         taicpu(p).opcode := A_CDQ;
                         taicpu(p).opsize := S_NO;
                         taicpu(p).clearop(1);
@@ -4434,8 +4237,7 @@
                             taicpu(p).clearop(0);
                             taicpu(p).ops:=0;
 
-                            AsmL.Remove(hp1);
-                            hp1.Free;
+                            RemoveInstruction(hp1);
 
                             taicpu(hp2).loadreg(0, NR_EDX);
                             taicpu(hp2).loadreg(1, NR_EAX);
@@ -4481,8 +4283,7 @@
                         cqto
                     }
                     DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
-                    Asml.Remove(hp1);
-                    hp1.Free;
+                    RemoveInstruction(hp1);
                     taicpu(p).opcode := A_CQO;
                     taicpu(p).opsize := S_NO;
                     taicpu(p).clearop(1);
@@ -4563,8 +4364,7 @@
                     taicpu(hp1).clearop(0);
                     taicpu(hp1).ops:=0;
 
-                    AsmL.Remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
 (*
 {$ifdef x86_64}
                   end
@@ -4611,8 +4411,7 @@
                     taicpu(hp1).clearop(0);
                     taicpu(hp1).ops:=0;
 
-                    AsmL.Remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
 {$endif x86_64}
 *)
                   end;
@@ -4674,11 +4473,8 @@
                   else
                     taicpu(hp1).loadRef(1,taicpu(p).oper[0]^.ref^);
                 end;
-                asml.remove(p);
-                asml.remove(hp2);
-                p.free;
-                hp2.free;
-                p := hp1
+                RemoveInstruction(hp2);
+                RemoveCurrentP(p, hp1);
               end;
             Exit;
 {$ifdef x86_64}
@@ -4766,8 +4562,7 @@
             taicpu(hp1).opcode := A_ADD;
 
             { Delete old ADD/LEA instruction }
-            asml.remove(hp2);
-            hp2.free;
+            RemoveInstruction(hp2);
 
             { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
             taicpu(hp3).opcode := A_RCR;
@@ -4806,8 +4601,7 @@
                 taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
                 taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
                 DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
-                asml.remove(hp1);
-                hp1.free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -5237,8 +5031,7 @@
                           DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
 
                           { Remove the original jump }
-                          asml.Remove(p);
-                          p.Free;
+                          RemoveInstruction(p);
 
                           GetNextInstruction(hp2, p); { Instruction after the label }
 
@@ -5336,8 +5129,7 @@
                                 DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
 
                                 { remove jCC }
-                                asml.remove(hp1);
-                                hp1.free;
+                                RemoveInstruction(hp1);
 
                                 { Now we can safely decrement it }
                                 tasmlabel(symbol).decrefs;
@@ -5348,8 +5140,7 @@
                                 { remove jmp }
                                 symbol := taicpu(hp2).oper[0]^.ref^.symbol;
 
-                                asml.remove(hp2);
-                                hp2.free;
+                                RemoveInstruction(hp2);
 
                                 { As before, now we can safely decrement it }
                                 tasmlabel(symbol).decrefs;
@@ -5438,11 +5229,8 @@
                 decw    %si             addw    %dx,%si       p
             }
             DebugMsg(SPeepholeOptimization + 'var3',p);
-            asml.remove(p);
-            asml.remove(hp2);
-            p.free;
-            hp2.free;
-            p:=hp1;
+            RemoveInstruction(hp2);
+            RemoveCurrentP(p, hp1);
           end
         else if reg_and_hp1_is_instr and
           (taicpu(hp1).opcode = A_MOV) and
@@ -5480,8 +5268,7 @@
                 else
 {$endif x86_64}
                   taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
-                asml.remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
               end;
           end
         else if reg_and_hp1_is_instr and
@@ -5526,15 +5313,13 @@
                     if (taicpu(hp1).oper[0]^.val = $ff) then
                       begin
                         DebugMsg(SPeepholeOptimization + 'var4',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                       end;
                     S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
                       if (taicpu(hp1).oper[0]^.val = $ffff) then
                         begin
                           DebugMsg(SPeepholeOptimization + 'var5',p);
-                          asml.remove(hp1);
-                          hp1.free;
+                          RemoveInstruction(hp1);
                         end;
 {$ifdef x86_64}
                     S_LQ:
@@ -5542,8 +5327,7 @@
                         begin
                           if (cs_asm_source in current_settings.globalswitches) then
                             asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
-                          asml.remove(hp1);
-                          hp1.Free;
+                          RemoveInstruction(hp1);
                         end;
 {$endif x86_64}
                   else
@@ -5736,9 +5520,7 @@
               begin
                 taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
                 DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
-                asml.remove(p);
-                p.Free;
-                p:=hp1;
+                RemoveCurrentP(p, hp1);
                 Result:=true;
                 exit;
               end
@@ -5774,8 +5556,7 @@
                         }
                         DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
 
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Exit;
                       end;
                   end
@@ -5835,8 +5616,7 @@
                        then
                        begin
                          DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
-                         asml.remove(hp1);
-                         hp1.free;
+                         RemoveInstruction(hp1);
                          Exit;
                        end;
                   end
@@ -6026,10 +5806,8 @@
             taicpu(hp1).is_jmp := true;
             DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
             RemoveCurrentP(p, hp4);
-            AsmL.Remove(hp2);
-            hp2.free;
-            AsmL.Remove(hp3);
-            hp3.free;
+            RemoveInstruction(hp2);
+            RemoveInstruction(hp3);
             Result:=true;
           end;
       end;
@@ -6079,10 +5857,8 @@
             taicpu(hp1).is_jmp := true;
             DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
             RemoveCurrentP(p, hp4);
-            AsmL.Remove(hp2);
-            hp2.free;
-            AsmL.Remove(hp3);
-            hp3.free;
+            RemoveInstruction(hp2);
+            RemoveInstruction(hp3);
             Result:=true;
           end;
 {$endif x86_64}
@@ -6236,9 +6012,7 @@
                      (taicpu(hp1).opcode <> A_SUB))) then
                   begin
                     hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := tai(hp1);
+                    RemoveCurrentP(p, hp1);
                     Result:=true;
                   end;
               end;
@@ -6255,9 +6029,7 @@
                    (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
                   begin
                     hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := tai(hp1);
+                    RemoveCurrentP(p, hp1);
                     Result:=true;
                   end;
               end;
@@ -6286,9 +6058,7 @@
                         ;
                     end;
                     hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := tai(hp1);
+                    RemoveCurrentP(p, hp1);
                     Result:=true;
                   end;
               end
@@ -6325,8 +6095,7 @@
             InsertLLItem(p.previous, p, hp2);
             taicpu(p).opcode := A_JMP;
             taicpu(p).is_jmp := true;
-            asml.remove(hp1);
-            hp1.free;
+            RemoveInstruction(hp1);
             Result:=true;
           end
         else
@@ -6357,8 +6126,7 @@
               end
             else
               DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
-            asml.remove(hp1);
-            hp1.free;
+            RemoveInstruction(hp1);
             Result:=true;
           end;
       end;
Index: compiler/xtensa/aoptcpu.pas
===================================================================
--- compiler/xtensa/aoptcpu.pas	(revision 45864)
+++ compiler/xtensa/aoptcpu.pas	(working copy)
@@ -219,8 +219,7 @@
               alloc:=FindRegAllocBackward(taicpu(p).oper[0]^.reg,tai(p.previous));
               if assigned(alloc) then
                 begin
-                  asml.Remove(alloc);
-                  alloc.free;
+                  RemoveInstruction(alloc);
                   dealloc.free;
                 end
               else
@@ -252,8 +251,7 @@
               //  and (taicpu(movp).oper[0]^.reg = taicpu(p).oper[1]^.ref^.base)
               //then
               //  taicpu(p).oper[1]^.ref^.addressmode:=AM_OFFSET;
-              asml.remove(movp);
-              movp.free;
+              RemoveInstruction(movp);
             end;
         end;
     end;
RemoveRefactor.patch (104,390 bytes)   

Florian

2020-08-10 15:25

administrator   ~0124728

Not sure why but after applying the patch, the results object files are longer.

J. Gareth Moreton

2020-08-10 21:35

developer   ~0124744

That's a little bit odd. There should be no difference. I'll regenerate the patch and do a bit of investigating, since the files growing implies a bug.

J. Gareth Moreton

2020-08-28 17:04

developer   ~0125173

Okay, this patch introduces the "RemoveInstruction" method and implements it on i386 and x86_64. My testing has been a little limited (only on Windows platforms under -O2), but a side-by-side comparison of the RTL's .o files yields no differences.

(Some situations where "asml.remove(p); p.free; p := hp1;" were also changed to "RemoveCurrentP(p, hp1);" etc)

I figure doing it platform by platform is the safest bet. Once this patch gets your seal of approval, I'll do the same for i8086-msdos, arm-linux and aarch64-linux.
removecurrentp-x86.patch (34,520 bytes)   
Index: compiler/aoptobj.pas
===================================================================
--- compiler/aoptobj.pas	(revision 46717)
+++ compiler/aoptobj.pas	(working copy)
@@ -344,6 +344,9 @@
         { removes p from asml, updates registers and replaces p with hp1 (if the next instruction was known beforehand) }
         procedure RemoveCurrentP(var p: tai; const hp1: tai); inline;
 
+        { removes hp from asml then frees it }
+        procedure RemoveInstruction(const hp: tai); inline;
+
        { traces sucessive jumps to their final destination and sets it, e.g.
          je l1                je l3
          <code>               <code>
@@ -1510,6 +1513,13 @@
       end;
 
 
+    procedure TAOptObj.RemoveInstruction(const hp: tai); inline;
+      begin
+        AsmL.Remove(hp);
+        hp.Free;
+      end;
+
+
     function FindLiveLabel(hp: tai; var l: tasmlabel): Boolean;
       var
         next: tai;
Index: compiler/x86/aoptx86.pas
===================================================================
--- compiler/x86/aoptx86.pas	(revision 46717)
+++ compiler/x86/aoptx86.pas	(working copy)
@@ -1090,8 +1090,7 @@
                   else
                     Internalerror(2017050701)
                 end;
-                asml.remove(hp1);
-                hp1.free;
+                RemoveInstruction(hp1);
               end;
           end;
       end;
@@ -1383,8 +1382,7 @@
                (getregtype(tai_regalloc(hp2).reg) = R_INTREGISTER) and
                (getsupreg(tai_regalloc(hp2).reg) = supreg) then
               begin
-                asml.remove(hp2);
-                hp2.free;
+                RemoveInstruction(hp2);
                 break;
               end;
           until not(assigned(hp2)) or regInInstruction(newreg(R_INTREGISTER,supreg,R_SUBWHOLE),hp2);
@@ -1444,8 +1442,7 @@
                       begin
                         DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 1',p);
                         taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         result:=true;
                         exit;
                       end
@@ -1457,8 +1454,7 @@
                     else if MatchOperand(taicpu(p).oper[0]^,taicpu(hp1).oper[1]^) then
                       begin
                         DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVA*2(V)MOVA* 2',p);
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         result:=true;
                         exit;
                       end
@@ -1482,8 +1478,7 @@
                       DebugMsg(SPeepholeOptimization + '(V)MOVA*(V)MOVS*2(V)MOVS* 1',p);
                       taicpu(p).opcode:=taicpu(hp1).opcode;
                       taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
-                      asml.Remove(hp1);
-                      hp1.Free;
+                      RemoveInstruction(hp1);
                       result:=true;
                       exit;
                     end
@@ -1566,8 +1561,7 @@
                       begin
                         taicpu(hp1).loadoper(2,taicpu(p).oper[0]^);
                         RemoveCurrentP(p, hp1); // <-- Is this actually safe? hp1 is not necessarily the next instruction. [Kit]
-                        asml.Remove(hp2);
-                        hp2.Free;
+                        RemoveInstruction(hp2);
                       end;
                   end
                 else if (hp1.typ = ait_instruction) and
@@ -1606,8 +1600,7 @@
                           RemoveCurrentP(p, nil);
                         p:=hp1;
                         taicpu(hp1).loadoper(1, taicpu(hp2).oper[1]^);
-                        asml.remove(hp2);
-                        hp2.Free;
+                        RemoveInstruction(hp2);
                         result:=true;
                       end;
                   end;
@@ -1643,8 +1636,7 @@
               begin
                 taicpu(p).loadoper(2,taicpu(hp1).oper[1]^);
                 DebugMsg(SPeepholeOptimization + 'VOpVmov2VOp done',p);
-                asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -1972,9 +1964,7 @@
                   begin
                     { We can remove the original MOV }
                     DebugMsg(SPeepholeOptimization + 'Mov2Nop 3 done',p);
-                    Asml.Remove(p);
-                    p.Free;
-                    p := hp1;
+                    RemoveCurrentp(p, hp1);
 
                     { TmpUsedRegs contains the results of "UpdateUsedRegs(tai(p.Next))" already,
                       so just restore it to UsedRegs instead of calculating it again }
@@ -2042,8 +2032,7 @@
                       begin
                         GetNextInstruction_p := GetNextInstruction(hp1, hp2);
                         DebugMsg(SPeepholeOptimization + 'Mov2Nop 4 done',hp1);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
 
                         { The instruction after what was hp1 is now the immediate next instruction,
                           so we can continue to make optimisations if it's present }
@@ -2130,8 +2119,7 @@
                               InternalError(2020021001);
                           end;
                           DebugMsg(SPeepholeOptimization + 'MovMovXX2MovXX 2 done',p);
-                          asml.Remove(hp1);
-                          hp1.Free;
+                          RemoveInstruction(hp1);
                           Result := True;
                           Exit;
                         end;
@@ -2186,8 +2174,7 @@
                             and ffffffffh, %reg
                         }
                         DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 1 done',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end;
@@ -2199,8 +2186,7 @@
                             and ffffffffffffffffh, %reg
                         }
                         DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 2 done',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end;
@@ -2225,8 +2211,7 @@
                         DebugMsg(SPeepholeOptimization + 'MovAndTest2Test done',p);
                         taicpu(hp1).loadoper(1,taicpu(p).oper[0]^);
                         taicpu(hp1).opcode:=A_TEST;
-                        asml.Remove(hp2);
-                        hp2.free;
+                        RemoveInstruction(hp2);
                         RemoveCurrentP(p, hp1);
                         Result:=true;
                         exit;
@@ -2325,8 +2310,7 @@
                         DebugMsg(SPeepholeOptimization + PreMessage + '; and' + debug_opsize2str(taicpu(hp1).opsize) + ' $' + MaskNum + ',' + RegName2 +
                           ' -> movz' + debug_opsize2str(NewSize) + ' ' + InputVal + ',' + RegName2, p);
 
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                       end;
 
                     Result := True;
@@ -2375,8 +2359,7 @@
                                 AllocRegBetween(taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
                               taicpu(p).loadOper(1,taicpu(hp1).oper[1]^);
                               DebugMsg(SPeepholeOptimization + 'MovMov2Mov 5 done',p);
-                              asml.remove(hp1);
-                              hp1.free;
+                              RemoveInstruction(hp1);
                               Result:=true;
                               Exit;
                             end;
@@ -2394,8 +2377,7 @@
                             }
                             taicpu(p).loadreg(1, taicpu(hp1).oper[1]^.reg);
                             DebugMsg(SPeepholeOptimization + 'MovMov2Mov 3 done',p);
-                            asml.remove(hp1);
-                            hp1.free;
+                            RemoveInstruction(hp1);
                             Result:=true;
                             Exit;
                           end;
@@ -2438,8 +2420,7 @@
                         if taicpu(p).oper[0]^.typ=top_reg then
                           AllocRegBetween(taicpu(p).oper[0]^.reg,p,hp1,usedregs);
                         DebugMsg(SPeepholeOptimization + 'MovMov2Mov 1',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Result:=true;
                         exit;
                       end
@@ -2462,8 +2443,7 @@
                              cmp mem1, reg1
                            }
                           begin
-                            asml.remove(hp2);
-                            hp2.free;
+                            RemoveInstruction(hp2);
                             taicpu(hp1).opcode := A_CMP;
                             taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
                             taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
@@ -2502,8 +2482,7 @@
                           DebugMsg(SPeepholeOptimization + 'MovMovMov2MovMov 1 done',p);
                           taicpu(p).loadoper(1,taicpu(hp2).oper[1]^);
                           taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
-                          asml.remove(hp2);
-                          hp2.free;
+                          RemoveInstruction(hp2);
                         end
 {$ifdef i386}
                       { this is enabled for i386 only, as the rules to create the reg sets below
@@ -2548,8 +2527,7 @@
                         end
                       else
                         begin
-                          asml.remove(hp2);
-                          hp2.free;
+                          RemoveInstruction(hp2);
                         end
 {$endif i386}
                         ;
@@ -2663,13 +2641,11 @@
                                 begin
                                   DebugMsg(SPeepholeOptimization + debug_regname(CurrentReg) + ' = ' + RegName1 + '; removed unnecessary instruction (MovMov2MovNop 6b}',hp2);
                                   AllocRegBetween(CurrentReg, p, hp2, UsedRegs);
-                                  asml.remove(hp2);
-                                  hp2.Free;
+                                  RemoveInstruction(hp2);
                                 end
                               else
                                 begin
-                                  asml.remove(hp2);
-                                  hp2.Free;
+                                  RemoveInstruction(hp2);
 
                                   { We can remove the original MOV too }
                                   DebugMsg(SPeepholeOptimization + 'MovMov2NopNop 6b done',p);
@@ -2769,8 +2745,7 @@
 
                     DebugMsg(SPeepholeOptimization + 'Removed movs/z instruction and extended earlier write (MovMovs/z2Mov/s/z)', hp2);
                     AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp2, UsedRegs);
-                    AsmL.Remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
 
                     Result := True;
                     Exit;
@@ -2800,8 +2775,7 @@
                         and ffffffffh, %reg
                     }
                     DebugMsg(SPeepholeOptimization + 'MovAnd2Mov 3 done',p);
-                    asml.remove(hp2);
-                    hp2.free;
+                    RemoveInstruction(hp2);
                     Result:=true;
                     exit;
                   end;
@@ -2833,9 +2807,7 @@
             )
           ) then
           begin
-            asml.remove(p);
-            p.free;
-            p:=hp1;
+            RemoveCurrentp(p, hp1);
             DebugMsg(SPeepholeOptimization + 'removed deadstore before leave/ret',p);
             RemoveLastDeallocForFuncRes(p);
             Result:=true;
@@ -2975,8 +2947,7 @@
                       ->
                         decw    %si             addw    %dx,%si       p
                     }
-                    asml.remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
                     RemoveCurrentP(p, hp1);
                     Result:=True;
                     Exit;
@@ -3060,8 +3031,7 @@
                       ->
                         decw    %si             addw    %dx,%si       p
                     }
-                    asml.remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
                   end;
               end;
 
@@ -3080,9 +3050,7 @@
             Taicpu(hp2).opcode:=A_MOV;
             asml.remove(hp1);
             insertllitem(hp2,hp2.next,hp1);
-            asml.remove(p);
-            p.free;
-            p:=hp1;
+            RemoveCurrentp(p, hp1);
             Result:=true;
             exit;
           end;
@@ -3124,15 +3092,15 @@
                         if (taicpu(p).oper[1]^.typ=top_reg) and
                           not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,UsedRegs)) then
                           begin
-                            asml.remove(p);
-                            p.free;
-                            GetNextInstruction(hp1,p);
                             DebugMsg(SPeepholeOptimization + 'MovXXMovXX2Nop 1 done',p);
+                            RemoveInstruction(hp1);
+                            RemoveCurrentp(p); { p will now be equal to the instruction that follows what was hp1 }
                           end
                         else
-                          DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                          begin
+                            DebugMsg(SPeepholeOptimization + 'MovXXMovXX2MoVXX 1 done',p);
+                            RemoveInstruction(hp1);
+                          end;
                         Result:=true;
                         exit;
                       end
@@ -3171,8 +3139,7 @@
                 taicpu(p).loadoper(0,taicpu(hp1).oper[0]^);
                 taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
                 DebugMsg(SPeepholeOptimization + 'OpMov2Op done',p);
-                asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -3243,8 +3210,7 @@
               begin
                 taicpu(p).loadoper(1,taicpu(hp1).oper[1]^);
                 DebugMsg(SPeepholeOptimization + 'LeaMov2Lea done',p);
-                asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -3403,8 +3369,7 @@
                 MatchOperand(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
                 begin
                   taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+1);
-                  asml.remove(hp1);
-                  hp1.free;
+                  RemoveInstruction(hp1);
                 end;
              A_SUB:
                if MatchOpType(taicpu(hp1),top_const,top_reg) and
@@ -3411,8 +3376,7 @@
                  MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
                  begin
                    taicpu(p).loadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
-                   asml.remove(hp1);
-                   hp1.free;
+                   RemoveInstruction(hp1);
                  end;
              A_ADD:
                begin
@@ -3420,13 +3384,11 @@
                    MatchOperand(taicpu(hp1).oper[1]^,taicpu(p).oper[1]^) then
                    begin
                      taicpu(p).loadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
-                     asml.remove(hp1);
-                     hp1.free;
+                     RemoveInstruction(hp1);
                      if (taicpu(p).oper[0]^.val = 0) then
                        begin
                          hp1 := tai(p.next);
-                         asml.remove(p);
-                         p.free;
+                         RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
                          if not GetLastInstruction(hp1, p) then
                            p := hp1;
                          DoSubAddOpt := True;
@@ -3474,9 +3436,7 @@
                     if taicpu(hp1).oper[0]^.typ=top_reg then
                       setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
                     hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := hp1;
+                    RemoveCurrentp(p, hp1);
                     Result:=true;
                     exit;
                   end;
@@ -3546,8 +3506,7 @@
                         if taicpu(hp1).oper[0]^.ref^.scalefactor<>0 then
                           tmpref.scalefactor:=tmpref.scalefactor*taicpu(hp1).oper[0]^.ref^.scalefactor;
                         TmpRef.base := taicpu(hp1).oper[0]^.ref^.base;
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                       end
                   end
                 else if (taicpu(hp1).oper[0]^.typ = Top_Const) then
@@ -3562,8 +3521,7 @@
                       else
                         internalerror(2019050536);
                     end;
-                    asml.remove(hp1);
-                    hp1.free;
+                    RemoveInstruction(hp1);
                   end
                 else
                   if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
@@ -3584,8 +3542,7 @@
                         else
                           internalerror(2019050535);
                       end;
-                      asml.remove(hp1);
-                      hp1.free;
+                      RemoveInstruction(hp1);
                     end;
               end;
             if TmpBool2
@@ -3715,14 +3672,12 @@
                         { Don't remove the 'mov' instruction if its register is used elsewhere }
                         if not(RegUsedAfterInstruction(taicpu(hp1).oper[1]^.reg, hp2, TmpUsedRegs)) then
                           begin
-                            asml.Remove(hp1);
-                            hp1.Free;
+                            RemoveInstruction(hp1);
                             Result := True;
                           end;
 
                         { Only set Result to True if the 'mov' instruction was removed }
-                        asml.Remove(hp2);
-                        hp2.Free;
+                        RemoveInstruction(hp2);
                       end;
                   end
                 else
@@ -3734,8 +3689,7 @@
                     if not(RegUsedAfterInstruction(NR_DEFAULTFLAGS, hp1, TmpUsedRegs)) then
                       begin
                         DebugMsg(SPeepholeOptimization + 'ShlAnd2Shl', p);
-                        asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         Result := True;
                       end;
                   end;
@@ -3825,8 +3779,7 @@
                   Exit;
               end;
 
-            asml.Remove(hp1);
-            hp1.Free;
+            RemoveInstruction(hp1);
 
             if Unconditional then
               MakeUnconditional(taicpu(hp2))
@@ -3840,11 +3793,8 @@
 
             if not RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs) then
               begin
-                asml.Remove(p);
-                UpdateUsedRegs(next);
-                p.Free;
+                RemoveCurrentp(p, hp2);
                 Result := True;
-                p := hp2;
               end;
 
             DebugMsg(SPeepholeOptimization + 'SETcc/TESTCmp/Jcc -> Jcc',p);
@@ -3879,11 +3829,8 @@
                    (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
                (taicpu(p).oper[0]^.ref^.index = NR_NO) then
               begin
-                asml.remove(p);
-                asml.remove(hp1);
-                p.free;
-                hp1.free;
-                p := hp2;
+                RemoveInstruction(hp1);
+                RemoveCurrentP(p, hp2);
                 RemoveLastDeallocForFuncRes(p);
                 Result := true;
               end
@@ -3896,8 +3843,7 @@
                   if (taicpu(p).opcode = A_FSTP) then
                     taicpu(p).opcode := A_FST
                   else taicpu(p).opcode := A_FIST;
-                  asml.remove(hp1);
-                  hp1.free;
+                  RemoveInstruction(hp1);
                 end
             *)
           end;
@@ -3937,9 +3883,7 @@
                     end;
                     taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
                     taicpu(hp1).oper[1]^.reg := NR_ST;
-                    asml.remove(p);
-                    p.free;
-                    p := hp1;
+                    RemoveCurrentP(p, hp1);
                     Result:=true;
                     exit;
                   end;
@@ -3967,9 +3911,7 @@
                   faddp/                       fmul     st, st
                   fmulp  st, st1 (hp2) }
                 begin
-                  asml.remove(p);
-                  p.free;
-                  p := hp1;
+                  RemoveCurrentP(p, hp1);
                   if (taicpu(hp2).opcode = A_FADDP) then
                     taicpu(hp2).opcode := A_FADD
                   else
@@ -4004,8 +3946,7 @@
                         else
                           internalerror(2019050533);
                       end;
-                      asml.remove(hp2);
-                      hp2.free;
+                      RemoveInstruction(hp2);
                     end
                   else
                     ;
@@ -4055,8 +3996,7 @@
                              begin
                                DebugMsg(SPeepholeOptimization + 'Cmpcc2Testcc - condition B/C/NAE/O --> Never (jump removed)', hp1);
                                TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
-                               AsmL.Remove(hp1);
-                               hp1.Free;
+                               RemoveInstruction(hp1);
                                { Since hp1 was deleted, hp2 must not be updated }
                                Continue;
                              end
@@ -4221,8 +4161,7 @@
          MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) then
          begin
            DebugMsg(SPeepholeOptimization + 'PXorPXor2PXor done',hp1);
-           asml.Remove(hp1);
-           hp1.Free;
+           RemoveInstruction(hp1);
            Result:=true;
            Exit;
          end;
@@ -4249,8 +4188,7 @@
          MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^,taicpu(hp1).oper[2]^) then
          begin
            DebugMsg(SPeepholeOptimization + 'VPXorVPXor2PXor done',hp1);
-           asml.Remove(hp1);
-           hp1.Free;
+           RemoveInstruction(hp1);
            Result:=true;
            Exit;
          end;
@@ -4348,8 +4286,7 @@
                 taicpu(p).opcode := A_LEA;
                 taicpu(p).loadref(0, NewRef);
 
-                Asml.Remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
 
                 Result := True;
                 Exit;
@@ -4379,9 +4316,7 @@
               not RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)
             then
               begin
-                asml.remove(p);
-                p.free;
-                p := hp1;
+                RemoveCurrentP(p, hp1);
                 Result:=true;
               end;
 
@@ -4420,12 +4355,9 @@
                 AllocRegBetween(taicpu(hp2).oper[1]^.reg, p, hp1, UsedRegs);
                 taicpu(hp1).opcode := A_XCHG;
 
-                asml.Remove(p);
-                asml.Remove(hp2);
-                p.Free;
-                hp2.Free;
+                RemoveCurrentP(p, hp1);
+                RemoveInstruction(hp2);
 
-                p := hp1;
                 Result := True;
                 Exit;
               end;
@@ -4449,8 +4381,7 @@
                             cltd
                         }
                         DebugMsg(SPeepholeOptimization + 'MovSar2Cltd', p);
-                        Asml.Remove(hp1);
-                        hp1.Free;
+                        RemoveInstruction(hp1);
                         taicpu(p).opcode := A_CDQ;
                         taicpu(p).opsize := S_NO;
                         taicpu(p).clearop(1);
@@ -4531,8 +4462,7 @@
                             taicpu(p).clearop(0);
                             taicpu(p).ops:=0;
 
-                            AsmL.Remove(hp1);
-                            hp1.Free;
+                            RemoveInstruction(hp1);
 
                             taicpu(hp2).loadreg(0, NR_EDX);
                             taicpu(hp2).loadreg(1, NR_EAX);
@@ -4578,8 +4508,7 @@
                         cqto
                     }
                     DebugMsg(SPeepholeOptimization + 'MovSar2Cqto', p);
-                    Asml.Remove(hp1);
-                    hp1.Free;
+                    RemoveInstruction(hp1);
                     taicpu(p).opcode := A_CQO;
                     taicpu(p).opsize := S_NO;
                     taicpu(p).clearop(1);
@@ -4660,8 +4589,7 @@
                     taicpu(hp1).clearop(0);
                     taicpu(hp1).ops:=0;
 
-                    AsmL.Remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
 (*
 {$ifdef x86_64}
                   end
@@ -4708,8 +4636,7 @@
                     taicpu(hp1).clearop(0);
                     taicpu(hp1).ops:=0;
 
-                    AsmL.Remove(hp2);
-                    hp2.Free;
+                    RemoveInstruction(hp2);
 {$endif x86_64}
 *)
                   end;
@@ -4799,8 +4726,7 @@
             taicpu(hp1).opcode := A_ADD;
 
             { Delete old ADD/LEA instruction }
-            asml.remove(hp2);
-            hp2.free;
+            RemoveInstruction(hp2);
 
             { Convert "shrq $1, reg1q" to "rcr $1, reg1d" }
             taicpu(hp3).opcode := A_RCR;
@@ -4839,8 +4765,7 @@
                 taicpu(p).loadreg(2,taicpu(p).oper[1]^.reg);
                 taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
                 DebugMsg(SPeepholeOptimization + 'MovImul2Imul done',p);
-                asml.remove(hp1);
-                hp1.free;
+                RemoveInstruction(hp1);
                 result:=true;
               end;
           end;
@@ -5270,8 +5195,7 @@
                           DebugMsg(SPeepholeOptimization+'JccMov2CMov',p);
 
                           { Remove the original jump }
-                          asml.Remove(p);
-                          p.Free;
+                          RemoveInstruction(p); { Note, the choice to not use RemoveCurrentp is deliberate }
 
                           GetNextInstruction(hp2, p); { Instruction after the label }
 
@@ -5369,8 +5293,7 @@
                                 DebugMsg(SPeepholeOptimization+'JccMovJmpMov2CMovCMov',hp1);
 
                                 { remove jCC }
-                                asml.remove(hp1);
-                                hp1.free;
+                                RemoveInstruction(hp1);
 
                                 { Now we can safely decrement it }
                                 tasmlabel(symbol).decrefs;
@@ -5381,8 +5304,7 @@
                                 { remove jmp }
                                 symbol := taicpu(hp2).oper[0]^.ref^.symbol;
 
-                                asml.remove(hp2);
-                                hp2.free;
+                                RemoveInstruction(hp2);
 
                                 { As before, now we can safely decrement it }
                                 tasmlabel(symbol).decrefs;
@@ -5483,11 +5405,8 @@
                 decw    %si             addw    %dx,%si       p
             }
             DebugMsg(SPeepholeOptimization + 'var3',p);
-            asml.remove(p);
-            asml.remove(hp2);
-            p.free;
-            hp2.free;
-            p:=hp1;
+            RemoveCurrentP(p, hp1);
+            RemoveInstruction(hp2);
           end
         else if reg_and_hp1_is_instr and
           (taicpu(hp1).opcode = A_MOV) and
@@ -5525,8 +5444,7 @@
                 else
 {$endif x86_64}
                   taicpu(p).loadreg(1,taicpu(hp1).oper[1]^.reg);
-                asml.remove(hp1);
-                hp1.Free;
+                RemoveInstruction(hp1);
               end;
           end
         else if reg_and_hp1_is_instr and
@@ -5571,15 +5489,13 @@
                     if (taicpu(hp1).oper[0]^.val = $ff) then
                       begin
                         DebugMsg(SPeepholeOptimization + 'var4',p);
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                       end;
                     S_WL{$ifdef x86_64}, S_WQ{$endif x86_64}:
                       if (taicpu(hp1).oper[0]^.val = $ffff) then
                         begin
                           DebugMsg(SPeepholeOptimization + 'var5',p);
-                          asml.remove(hp1);
-                          hp1.free;
+                          RemoveInstruction(hp1);
                         end;
 {$ifdef x86_64}
                     S_LQ:
@@ -5587,8 +5503,7 @@
                         begin
                           if (cs_asm_source in current_settings.globalswitches) then
                             asml.insertbefore(tai_comment.create(strpnew(SPeepholeOptimization + 'var6')),p);
-                          asml.remove(hp1);
-                          hp1.Free;
+                          RemoveInstruction(hp1);
                         end;
 {$endif x86_64}
                   else
@@ -5781,9 +5696,7 @@
               begin
                 taicpu(hp1).loadConst(0, taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
                 DebugMsg(SPeepholeOptimization + 'AndAnd2And done',hp1);
-                asml.remove(p);
-                p.Free;
-                p:=hp1;
+                RemoveCurrentP(p, hp1);
                 Result:=true;
                 exit;
               end
@@ -5819,8 +5732,7 @@
                         }
                         DebugMsg(SPeepholeOptimization + 'AndMovzToAnd done',p);
 
-                        asml.remove(hp1);
-                        hp1.free;
+                        RemoveInstruction(hp1);
                         Exit;
                       end;
                   end
@@ -5880,8 +5792,7 @@
                        then
                        begin
                          DebugMsg(SPeepholeOptimization + 'AndMovsxToAnd',p);
-                         asml.remove(hp1);
-                         hp1.free;
+                         RemoveInstruction(hp1);
                          Exit;
                        end;
                   end
@@ -6074,10 +5985,8 @@
             taicpu(hp1).is_jmp := true;
             DebugMsg(SPeepholeOptimization + 'LeaCallLeaRet2Jmp done',p);
             RemoveCurrentP(p, hp4);
-            AsmL.Remove(hp2);
-            hp2.free;
-            AsmL.Remove(hp3);
-            hp3.free;
+            RemoveInstruction(hp2);
+            RemoveInstruction(hp3);
             Result:=true;
           end;
       end;
@@ -6127,10 +6036,8 @@
             taicpu(hp1).is_jmp := true;
             DebugMsg(SPeepholeOptimization + 'PushCallPushRet2Jmp done',p);
             RemoveCurrentP(p, hp4);
-            AsmL.Remove(hp2);
-            hp2.free;
-            AsmL.Remove(hp3);
-            hp3.free;
+            RemoveInstruction(hp2);
+            RemoveInstruction(hp3);
             Result:=true;
           end;
 {$endif x86_64}
@@ -6283,10 +6190,7 @@
                     ((taicpu(hp1).opcode <> A_ADD) and
                      (taicpu(hp1).opcode <> A_SUB))) then
                   begin
-                    hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := tai(hp1);
+                    RemoveCurrentP(p, hp2);
                     Result:=true;
                   end;
               end;
@@ -6302,10 +6206,7 @@
                   { and in case of carry for A(E)/B(E)/C/NC                  }
                    (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
                   begin
-                    hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := tai(hp1);
+                    RemoveCurrentP(p, hp2);
                     Result:=true;
                   end;
               end;
@@ -6333,10 +6234,7 @@
                       else
                         ;
                     end;
-                    hp1 := tai(p.next);
-                    asml.remove(p);
-                    p.free;
-                    p := tai(hp1);
+                    RemoveCurrentP(p, hp2);
                     Result:=true;
                   end;
               end
@@ -6373,8 +6271,7 @@
             InsertLLItem(p.previous, p, hp2);
             taicpu(p).opcode := A_JMP;
             taicpu(p).is_jmp := true;
-            asml.remove(hp1);
-            hp1.free;
+            RemoveInstruction(hp1);
             Result:=true;
           end
         else
@@ -6405,8 +6302,7 @@
               end
             else
               DebugMsg(SPeepholeOptimization + 'CallRet2Call done',p);
-            asml.remove(hp1);
-            hp1.free;
+            RemoveInstruction(hp1);
             Result:=true;
           end;
       end;
removecurrentp-x86.patch (34,520 bytes)   

J. Gareth Moreton

2020-08-28 17:05

developer   ~0125174

(I also just realised I gave the patch an incorrect name... it should be called removeinstruction-x86, not removecurrentp-x86)

Florian

2020-09-13 21:35

administrator   ~0125529

Thanks, applied.

Issue History

Date Modified Username Field Change
2020-07-21 20:35 J. Gareth Moreton New Issue
2020-07-21 20:35 J. Gareth Moreton File Added: RemoveRefactor.patch
2020-07-26 20:55 J. Gareth Moreton File Deleted: RemoveRefactor.patch
2020-07-26 20:56 J. Gareth Moreton Note Added: 0124347
2020-07-26 20:56 J. Gareth Moreton File Added: RemoveRefactor.patch
2020-08-10 15:25 Florian Note Added: 0124728
2020-08-10 21:35 J. Gareth Moreton Note Added: 0124744
2020-08-28 17:04 J. Gareth Moreton Note Added: 0125173
2020-08-28 17:04 J. Gareth Moreton File Added: removecurrentp-x86.patch
2020-08-28 17:05 J. Gareth Moreton Note Added: 0125174
2020-09-13 21:35 Florian Assigned To => Florian
2020-09-13 21:35 Florian Status new => resolved
2020-09-13 21:35 Florian Resolution open => fixed
2020-09-13 21:35 Florian Fixed in Version => 3.3.1
2020-09-13 21:35 Florian Fixed in Revision => 46862
2020-09-13 21:35 Florian FPCTarget => -
2020-09-13 21:35 Florian Note Added: 0125529