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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
{
Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
Development Team
This unit contains the base of all optimizer related objects
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 aoptbase;
{$i fpcdefs.inc}
interface
uses
aasmbase,aasmcpu,aasmtai,aasmdata,
cpubase,
cgbase,
cgutils;
Type
{ the number of tai objects processed by an optimizer object since the last
time a register was modified }
{ size at each dimension depends on the registers of this type }
TInstrSinceLastMod = Array[tregistertype] of pbyte;
{ the TAopBase object implements the basic methods that most other }
{ assembler optimizer objects require }
Type
TAoptBase = class
{ processor independent methods }
constructor create; virtual;
destructor destroy;override;
{ returns true if register Reg is used by instruction p1 }
Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;virtual;
{ returns true if register Reg occurs in operand op }
Function RegInOp(Reg: TRegister; const op: toper): Boolean;
{ returns true if register Reg is used in the reference Ref }
Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;virtual;
{ returns true if the references are completely equal }
{Function RefsEqual(Const R1, R2: TReference): Boolean;}
{ gets the next tai object after current that contains info relevant }
{ to the optimizer in p1. If there is none, it returns false and }
{ sets p1 to nil }
Function GetNextInstruction(Current: tai; Var Next: tai): Boolean;
{ gets the previous tai object after current that contains info }
{ relevant to the optimizer in last. If there is none, it retuns }
{ false and sets last to nil }
Function GetLastInstruction(Current: tai; Var Last: tai): Boolean;
function SkipEntryExitMarker(current: tai; var next: tai): boolean;
{ processor dependent methods }
{ returns the maximum width component of Reg. Only has to be }
{ overridden for the 80x86 (afaik) }
Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
{ returns true if Reg1 and Reg2 are of the samae width. Only has to }
{ overridden for the 80x86 (afaik) }
Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
{ returns whether P is a load instruction (load contents from a }
{ memory location or (register) variable into a register) }
Function IsLoadMemReg(p: tai): Boolean; Virtual; Abstract;
{ returns whether P is a load constant instruction (load a constant }
{ into a register) }
Function IsLoadConstReg(p: tai): Boolean; Virtual; Abstract;
{ returns whether P is a store instruction (store contents from a
register to a memory location or to a (register) variable) }
Function IsStoreRegMem(p: tai): Boolean; Virtual; Abstract;
{ create a paicpu Object that loads the contents of reg1 into reg2 }
Function a_load_reg_reg(reg1, reg2: TRegister): taicpu; Virtual; Abstract;
{ returns true if reg is used by any instruction between p1 and p2 }
Function RegUsedBetween(reg: TRegister; p1, p2: tai): Boolean;
{ returns true if reg is modified by any instruction between p1 and p2 }
function RegModifiedBetween(reg: TRegister; p1, p2: tai): Boolean;
end;
function labelCanBeSkipped(p: tai_label): boolean;
implementation
uses
globtype,globals,aoptcpub;
constructor taoptbase.create;
begin
inherited create;
end;
destructor taoptbase.destroy;
begin
inherited destroy;
end;
Function TAOptBase.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
Var
Count: longint;
Begin
result:=false;
if p1.typ<>ait_instruction then
exit;
for Count:=0 to TInstr(p1).ops-1 do
if RegInOp(Reg, TInstr(p1).oper[Count]^) then
exit(true);
result:=false;
End;
Function TAOptBase.RegInOp(Reg: TRegister; const op: toper): Boolean;
Begin
Case op.typ Of
Top_Reg: RegInOp := Reg = op.reg;
Top_Ref: RegInOp := RegInRef(Reg, op.ref^);
{$ifdef arm}
Top_Shifterop: RegInOp := op.shifterop^.rs = Reg;
Top_RegSet: RegInOp := getsupreg(Reg) in op.regset^;
{$endif arm}
Else RegInOp := False
End
End;
Function TAOptBase.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
Begin
Reg := RegMaxSize(Reg);
RegInRef := (Ref.Base = Reg)
{$ifdef cpurefshaveindexreg}
Or (Ref.Index = Reg)
{$endif cpurefshaveindexreg}
End;
Function TAOptBase.RegModifiedByInstruction(Reg: TRegister; p1: tai): Boolean;
Begin
Result:=true;
End;
function labelCanBeSkipped(p: tai_label): boolean;
begin
labelCanBeSkipped := not(p.labsym.is_used) or (p.labsym.labeltype<>alt_jump);
end;
Function TAOptBase.GetNextInstruction(Current: tai; Var Next: tai): Boolean;
Begin
Repeat
Current := tai(Current.Next);
While Assigned(Current) And
((Current.typ In SkipInstr) or
{$if defined(SPARC) or defined(MIPS)}
((Current.typ=ait_instruction) and
(taicpu(Current).opcode=A_NOP)
) or
{$endif SPARC or MIPS}
((Current.typ = ait_label) And
labelCanBeSkipped(Tai_Label(Current)))) Do
Current := tai(Current.Next);
If Assigned(Current) And
(Current.typ = ait_Marker) And
(Tai_Marker(Current).Kind = mark_NoPropInfoStart) Then
Begin
While Assigned(Current) And
((Current.typ <> ait_Marker) Or
(Tai_Marker(Current).Kind <> mark_NoPropInfoEnd)) Do
Current := Tai(Current.Next);
End;
Until Not(Assigned(Current)) Or
(Current.typ <> ait_Marker) Or
(Tai_Marker(Current).Kind <> mark_NoPropInfoEnd);
Next := Current;
If Assigned(Current) And
Not((Current.typ In SkipInstr) or
((Current.typ = ait_label) And
labelCanBeSkipped(Tai_Label(Current))))
Then GetNextInstruction := True
Else
Begin
Next := Nil;
GetNextInstruction := False;
End;
End;
Function TAOptBase.GetLastInstruction(Current: tai; Var Last: tai): Boolean;
Begin
Repeat
Current := Tai(Current.previous);
While Assigned(Current) And
(((Current.typ = ait_Marker) And
Not(Tai_Marker(Current).Kind in [mark_AsmBlockEnd{,mark_NoPropInfoEnd}])) or
(Current.typ In SkipInstr) or
((Current.typ = ait_label) And
labelCanBeSkipped(Tai_Label(Current)))) Do
Current := Tai(Current.previous);
{ If Assigned(Current) And
(Current.typ = ait_Marker) And
(Tai_Marker(Current).Kind = mark_NoPropInfoEnd) Then
Begin
While Assigned(Current) And
((Current.typ <> ait_Marker) Or
(Tai_Marker(Current).Kind <> mark_NoPropInfoStart)) Do
Current := Tai(Current.previous);
End; }
Until Not(Assigned(Current)) Or
(Current.typ <> ait_Marker) Or
not(tai_Marker(current).Kind in [mark_NoPropInfoStart,mark_NoPropInfoEnd]);
If Not(Assigned(Current)) or
(Current.typ In SkipInstr) or
((Current.typ = ait_label) And
labelCanBeSkipped(Tai_Label(Current))) or
((Current.typ = ait_Marker) And
(Tai_Marker(Current).Kind = mark_AsmBlockEnd))
Then
Begin
Last := Nil;
GetLastInstruction := False
End
Else
Begin
Last := Current;
GetLastInstruction := True;
End;
End;
function TAOptBase.SkipEntryExitMarker(current: tai; var next: tai): boolean;
begin
result:=true;
if current.typ<>ait_marker then
exit;
next:=current;
while GetNextInstruction(next,next) do
begin
if (next.typ<>ait_marker) or not(tai_marker(next).Kind in [mark_Position,mark_BlockStart]) then
exit;
end;
result:=false;
end;
Function TAOptBase.RegUsedBetween(reg : TRegister;p1,p2 : tai) : Boolean;
Begin
Result:=false;
while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
if RegInInstruction(reg,p1) then
begin
Result:=true;
exit;
end;
end;
Function TAOptBase.RegModifiedBetween(reg : TRegister;p1,p2 : tai) : Boolean;
Begin
Result:=false;
while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
if RegModifiedByInstruction(reg,p1) then
begin
Result:=true;
exit;
end;
end;
{ ******************* Processor dependent stuff *************************** }
Function TAOptBase.RegMaxSize(Reg: TRegister): TRegister;
Begin
RegMaxSize := Reg
End;
Function TAOptBase.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
Begin
RegsSameSize := True
End;
end.
|