summaryrefslogtreecommitdiff
path: root/compiler/jvm/aoptcpu.pas
blob: 877f2b196c877bc3e805104904a86301160cfe4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
    Copyright (c) 2015 by Jonas Maebe, member of the Free Pascal
    Development Team

    This unit implements the JVM optimizer object

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************
}


Unit aoptcpu;

{$i fpcdefs.inc}

Interface

uses cpubase, aasmtai, aopt, aoptcpub;

Type
  TCpuAsmOptimizer = class(TAsmOptimizer)
   protected
    function RemoveDoubleSwap(var p: tai): boolean;
    function RemoveCommutativeSwap(var p: tai): boolean;
    function RemoveLoadLoadSwap(var p: tai): boolean;
   public
    { uses the same constructor as TAopObj }
    function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
    procedure PeepHoleOptPass2;override;
    function PostPeepHoleOptsCpu(var p: tai): boolean; override;
  End;

Implementation

  uses
    aasmbase,aasmcpu,cgbase;


  function TCpuAsmOptimizer.RemoveDoubleSwap(var p: tai): boolean;
    var
      next, next2: tai;
    begin
      result:=false;
      { remove two successive "swap" instructions }
      if (taicpu(p).opcode=a_swap) and
         GetNextInstruction(p,next) and
         (next.typ=ait_instruction) and
         (taicpu(next).opcode=a_swap) then
        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;
          result:=true;
        end;
    end;


  { returns whether p is an instruction that does not consume any stack slots,
    and adds a new item on the stack that is one stack slot wide }
  function OpCreatesSingleStackSlot(p: tai): boolean;
    begin
      result:=
        (p.typ=ait_instruction) and
        (taicpu(p).opcode in
          [a_aload, a_aload_0, a_aload_1, a_aload_2, a_aload_3,
           a_bipush,
           a_fconst_0, a_fconst_1, a_fconst_2,
           a_fload, a_fload_0, a_fload_1, a_fload_2, a_fload_3,
           a_getstatic,
           a_iconst_m1, a_iconst_0, a_iconst_1, a_iconst_2, a_iconst_3,
           a_iconst_4, a_iconst_5,
           a_iload, a_iload_0, a_iload_1, a_iload_2, a_iload_3,
           a_new,
           a_sipush]);
    end;


  function OpIsCommutativeSingleSlots(p: tai): boolean;
    begin
      result:=
        (p.typ=ait_instruction) and
        (taicpu(p).opcode in
          [a_fadd, a_fmul,
           a_iadd, a_iand, a_imul, a_ior, a_ixor,
           a_pop2])
    end;


  function TCpuAsmOptimizer.RemoveCommutativeSwap(var p: tai): boolean;
    var
      next: tai;
    begin
      result:=false;
      if (taicpu(p).opcode<>a_swap) then
        exit;
      { if the next opcode is commutative operation, we can remove the swap }
      if GetNextInstruction(p,next) and
         OpIsCommutativeSingleSlots(next) then
        begin
          asml.remove(p);
          p.free;
          p:=next;
          result:=true;
          exit;
        end;
    end;


  function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
    var
      next, next2: tai;
    begin
      result:=false;
      case p.typ of
        ait_instruction:
          begin
            if RemoveDoubleSwap(p) or
               RemoveCommutativeSwap(p) then
              exit(true)
          end;
        else
          ;
      end;
    end;


  procedure TCpuAsmOptimizer.PeepHoleOptPass2;
    begin
    end;


  function TCpuAsmOptimizer.RemoveLoadLoadSwap(var p: tai): boolean;
    var
      next, prev1, prev2: tai;
    begin
      result:=false;
      if (taicpu(p).opcode<>a_swap) then
        exit;
      { if we can swap the previous two instructions that put the items on the
        stack, we can remove the swap -- only do this in PostPeepholeOpts,
        because this may make the temp alloc information invalid. Ideally, we
        should move the tempallocs around too }
      if GetLastInstruction(p,prev1) and
         OpCreatesSingleStackSlot(prev1) and
         GetLastInstruction(prev1,prev2) and
         OpCreatesSingleStackSlot(prev2) then
        begin
          next:=tai(p.next);
          asml.remove(prev2);
          asml.InsertAfter(prev2,prev1);
          asml.remove(p);
          p.free;
          p:=next;
          result:=true;
        end;
    end;


  function TCpuAsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
    begin
      result:=
        (p.typ=ait_instruction) and
        RemoveLoadLoadSwap(p);
    end;

begin
  casmoptimizer:=TCpuAsmOptimizer;
End.