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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
{
Copyright (c) 1998-2004 by Jonas Maebe, member of the Free Pascal
Development Team
This unit contains the interface routines between the code generator
and the optimizer.
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 aopt;
{$i fpcdefs.inc}
{ $define DEBUG_OPTALLOC}
{ $define DEBUG_INSTRUCTIONREGISTERDEPENDENCIES}
Interface
Uses
aasmbase,aasmtai,aasmdata,aasmcpu,
aoptobj;
Type
TAsmOptimizer = class(TAoptObj)
{ _AsmL is the PAasmOutpout list that has to be optimized }
Constructor create(_AsmL: TAsmList); virtual; reintroduce;
{ call the necessary optimizer procedures }
Procedure Optimize;virtual;
Destructor destroy;override;
private
procedure FindLoHiLabels;
{ Builds a table with the locations of the labels in the TAsmList.
Also fixes some RegDeallocs like "# %eax released; push (%eax)" }
Procedure BuildLabelTableAndFixRegAlloc;
procedure clear;
protected
procedure pass_1;
End;
TAsmOptimizerClass = class of TAsmOptimizer;
TAsmScheduler = class(TAoptObj)
{ _AsmL is the PAasmOutpout list that has to be re-scheduled }
Constructor Create(_AsmL: TAsmList); virtual; reintroduce;
Procedure Optimize;
function SchedulerPass1Cpu(var p: tai): boolean; virtual; abstract;
procedure SchedulerPass1;
end;
TAsmSchedulerClass = class of TAsmScheduler;
var
casmoptimizer : TAsmOptimizerClass;
cpreregallocscheduler : TAsmSchedulerClass;
procedure Optimize(AsmL:TAsmList);
procedure PreRegallocSchedule(AsmL:TAsmList);
Implementation
uses
cutils,
globtype, globals,
verbose,
cpubase,
cgbase,
aoptcpu;
Constructor TAsmOptimizer.create(_AsmL: TAsmList);
Begin
inherited create(_asml,nil,nil,nil);
{ setup labeltable, always necessary }
New(LabelInfo);
End;
procedure TAsmOptimizer.FindLoHiLabels;
{ Walks through the paasmlist to find the lowest and highest label number. }
{ Returns the last Pai object of the current block }
Var LabelFound: Boolean;
p: tai;
Begin
LabelInfo^.LowLabel := High(longint);
LabelInfo^.HighLabel := 0;
LabelInfo^.LabelDif := 0;
LabelInfo^.LabelTable:=nil;
LabelFound := False;
P := BlockStart;
With LabelInfo^ Do
Begin
While Assigned(P) And
((P.typ <> Ait_Marker) Or
(tai_Marker(P).Kind <> mark_AsmBlockStart)) Do
Begin
If (p.typ = ait_label) and
(tai_Label(p).labsym.labeltype=alt_jump) and
(tai_Label(p).labsym.is_used) Then
Begin
LabelFound := True;
If (tai_Label(p).labsym.labelnr < LowLabel) Then
LowLabel := tai_Label(p).labsym.labelnr;
If (tai_Label(p).labsym.labelnr > HighLabel) Then
HighLabel := tai_Label(p).labsym.labelnr
End;
GetNextInstruction(p, p)
End;
blockend:=p;
If LabelFound
Then LabelDif := HighLabel-LowLabel+1
Else LabelDif := 0
End
End;
Procedure TAsmOptimizer.BuildLabelTableAndFixRegAlloc;
Var p,hp1, hp2: tai;
Regs: TAllUsedRegs;
LabelIdx : longint;
Begin
CreateUsedRegs(Regs);
With LabelInfo^ Do
begin
If (LabelDif <> 0) Then
Begin
GetMem(LabelTable, LabelDif*SizeOf(TLabelTableItem));
FillChar(LabelTable^, LabelDif*SizeOf(TLabelTableItem), 0);
end;
p := BlockStart;
While (P <> BlockEnd) Do
Begin
Case p.typ Of
ait_Label:
begin
If tai_label(p).labsym.is_used and
(tai_Label(p).labsym.labeltype=alt_jump) then
begin
LabelIdx:=tai_label(p).labsym.labelnr-LowLabel;
if LabelIdx>int64(LabelDif) then
internalerror(200604202);
LabelTable^[LabelIdx].PaiObj := p;
end;
end;
ait_regAlloc:
begin
if tai_regalloc(p).ratype=ra_alloc then
Begin
If Not(RegInUsedRegs(tai_regalloc(p).Reg,Regs)) Then
IncludeRegInUsedRegs(tai_regalloc(p).Reg,Regs)
Else
Begin
hp1 := tai(p.previous);
{$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;
p := hp1;
{ not sure if this is useful, it even skips previous deallocs of the register (FK)
hp1 := p;
hp2 := nil;
While GetLastInstruction(hp1, hp1) And
Not(RegInInstruction(tai_regalloc(p).Reg, hp1)) Do
hp2:=hp1;
If hp2<>nil Then
Begin
hp1:=tai_regalloc.DeAlloc(tai_regalloc(p).Reg,hp2);
InsertLLItem(tai(hp2.previous), hp2, hp1);
End;
}
End;
End
else if tai_regalloc(p).ratype=ra_dealloc then
Begin
ExcludeRegFromUsedRegs(tai_regalloc(p).Reg,Regs);
hp1 := p;
hp2 := nil;
While Not(assigned(FindRegAlloc(tai_regalloc(p).Reg, tai(hp1.Next)))) And
GetNextInstruction(hp1, hp1) And
InstructionLoadsFromReg(tai_regalloc(p).Reg, hp1) Do
hp2 := hp1;
{ move deallocations }
If hp2 <> nil Then
Begin
hp1 := tai(p.previous);
{$ifdef DEBUG_OPTALLOC}
AsmL.InsertAfter(tai_comment.Create(strpnew('Moved deallocation of '+std_regname(tai_regalloc(p).Reg))),p);
{$endif DEBUG_OPTALLOC}
AsmL.Remove(p);
InsertLLItem(hp2, tai(hp2.Next), p);
{ don't remove this deallocation later on when merging dealloc/alloc pairs because
it marks indenpendent use of a register
This could be also achieved by a separate passes for merging first and then later
moving but I did not choose this solution because it takes more time and code (FK) }
tai_regalloc(p).keep:=true;
{$ifdef DEBUG_OPTALLOC}
AsmL.InsertAfter(tai_comment.Create(strpnew('Moved deallocation of '+std_regname(tai_regalloc(p).Reg)+' here')),hp2);
{$endif DEBUG_OPTALLOC}
p := hp1;
End
{ merge allocations/deallocations }
else if assigned(findregalloc(tai_regalloc(p).reg, tai(p.next)))
and getnextinstruction(p,hp1) and
{ don't merge deallocations/allocation which mark a new use of register, this
enables more possibilities for the peephole optimizer }
not(tai_regalloc(p).keep) then
begin
hp1 := tai(p.previous);
{$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;
p := hp1;
end;
End
End
End;
P := tai(p.Next);
While Assigned(p) and
(p <> blockend) and
(p.typ in (SkipInstr - [ait_regalloc])) Do
P := tai(P.Next)
End;
end;
ReleaseUsedRegs(Regs);
End;
procedure tasmoptimizer.clear;
begin
if assigned(LabelInfo^.labeltable) then
begin
freemem(LabelInfo^.labeltable);
LabelInfo^.labeltable := nil;
end;
LabelInfo^.labeldif:=0;
LabelInfo^.lowlabel:=high(longint);
LabelInfo^.highlabel:=0;
end;
procedure tasmoptimizer.pass_1;
begin
findlohilabels;
BuildLabelTableAndFixRegAlloc;
end;
Procedure TAsmOptimizer.Optimize;
Var
HP: tai;
pass: longint;
Begin
pass:=0;
BlockStart := tai(AsmL.First);
pass_1;
While Assigned(BlockStart) Do
Begin
if (cs_opt_peephole in current_settings.optimizerswitches) then
begin
if pass = 0 then
PrePeepHoleOpts;
{ Peephole optimizations }
PeepHoleOptPass1;
{ Only perform them twice in the first pass }
if pass = 0 then
PeepHoleOptPass1;
end;
{ more peephole optimizations }
if (cs_opt_peephole in current_settings.optimizerswitches) then
begin
PeepHoleOptPass2;
{ if pass = last_pass then }
PostPeepHoleOpts;
end;
{ free memory }
clear;
{ continue where we left off, BlockEnd is either the start of an }
{ assembler block or nil}
BlockStart := BlockEnd;
While Assigned(BlockStart) And
(BlockStart.typ = ait_Marker) And
(tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
Begin
{ we stopped at an assembler block, so skip it }
While GetNextInstruction(BlockStart, BlockStart) And
((BlockStart.Typ <> Ait_Marker) Or
(tai_Marker(Blockstart).Kind <> mark_AsmBlockEnd)) Do;
{ blockstart now contains a tai_marker(mark_AsmBlockEnd) }
If GetNextInstruction(BlockStart, HP) And
((HP.typ <> ait_Marker) Or
(Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
{ There is no assembler block anymore after the current one, so }
{ optimize the next block of "normal" instructions }
pass_1
{ Otherwise, skip the next assembler block }
else
blockStart := hp;
End
End;
End;
Destructor TAsmOptimizer.Destroy;
Begin
if assigned(LabelInfo^.LabelTable) then
Freemem(LabelInfo^.LabelTable);
Dispose(LabelInfo);
inherited Destroy;
End;
constructor TAsmScheduler.Create(_AsmL: TAsmList);
begin
inherited create(_asml,nil,nil,nil);
end;
procedure TAsmScheduler.SchedulerPass1;
var
p : tai;
begin
p:=BlockStart;
while p<>BlockEnd Do
begin
if SchedulerPass1Cpu(p) then
continue;
p:=tai(p.next);
end;
end;
procedure TAsmScheduler.Optimize;
Var
HP: tai;
Begin
BlockStart := tai(AsmL.First);
While Assigned(BlockStart) Do
Begin
{ Peephole optimizations }
SchedulerPass1;
{ continue where we left off, BlockEnd is either the start of an }
{ assembler block or nil}
BlockStart:=BlockEnd;
While Assigned(BlockStart) And
(BlockStart.typ = ait_Marker) And
(tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
Begin
{ we stopped at an assembler block, so skip it }
While GetNextInstruction(BlockStart, BlockStart) And
((BlockStart.Typ <> Ait_Marker) Or
(tai_Marker(Blockstart).Kind <> mark_AsmBlockEnd)) Do;
{ blockstart now contains a tai_marker(mark_AsmBlockEnd) }
If not(GetNextInstruction(BlockStart, HP) And
((HP.typ <> ait_Marker) Or
(Tai_Marker(HP).Kind <> mark_AsmBlockStart))) Then
{ skip the next assembler block }
blockStart := hp;
End
End;
End;
procedure Optimize(AsmL:TAsmList);
var
p : TAsmOptimizer;
begin
p:=casmoptimizer.Create(AsmL);
p.Optimize;
{$ifdef DEBUG_INSTRUCTIONREGISTERDEPENDENCIES}
p.Debug_InsertInstrRegisterDependencyInfo;
{$endif DEBUG_INSTRUCTIONREGISTERDEPENDENCIES}
p.free
end;
procedure PreRegallocSchedule(AsmL:TAsmList);
var
p : TAsmScheduler;
begin
p:=cpreregallocscheduler.Create(AsmL);
p.Optimize;
p.free
end;
end.
|