aoptobj.pas 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit contains the processor independent assembler optimizer
  5. object, base for the dataflow analyzer, peepholeoptimizer and
  6. common subexpression elimination objects.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit AoptObj;
  21. {$i fpcdefs.inc}
  22. { general, processor independent objects for use by the assembler optimizer }
  23. Interface
  24. uses
  25. globtype,
  26. aasmbase,aasmcpu,aasmtai,aasmdata,
  27. cclasses,
  28. cgbase,cgutils,
  29. cpubase,
  30. aoptbase,aoptcpub,aoptda;
  31. { ************************************************************************* }
  32. { ********************************* Constants ***************************** }
  33. { ************************************************************************* }
  34. Const
  35. {Possible register content types}
  36. con_Unknown = 0;
  37. con_ref = 1;
  38. con_const = 2;
  39. {***************** Types ****************}
  40. Type
  41. { ************************************************************************* }
  42. { ************************* Some general type definitions ***************** }
  43. { ************************************************************************* }
  44. TRefCompare = Function(const r1, r2: TReference): Boolean;
  45. //!!! FIXME
  46. TRegArray = Array[byte] of tsuperregister;
  47. TRegSet = tcpuregisterset;
  48. { possible actions on an operand: read, write or modify (= read & write) }
  49. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  50. { ************************************************************************* }
  51. { * Object to hold information on which regiters are in use and which not * }
  52. { ************************************************************************* }
  53. { TUsedRegs }
  54. TUsedRegs = class
  55. Constructor create(aTyp : TRegisterType);
  56. Constructor create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  57. Destructor Destroy;override;
  58. Procedure Clear;
  59. { update the info with the pairegalloc objects coming after
  60. p }
  61. procedure Update(p: Tai; IgnoreNewAllocs: Boolean=false);
  62. { is Reg currently in use }
  63. Function IsUsed(Reg: TRegister): Boolean;
  64. { get all the currently used registers }
  65. Function GetUsedRegs: TRegSet;
  66. Private
  67. Typ : TRegisterType;
  68. UsedRegs: TRegSet;
  69. End;
  70. { ************************************************************************* }
  71. { ******************* Contents of the integer registers ******************* }
  72. { ************************************************************************* }
  73. { size of the integer that holds the state number of a register. Can be any }
  74. { integer type, so it can be changed to reduce the size of the TContent }
  75. { structure or to improve alignment }
  76. TStateInt = Byte;
  77. TContent = Record
  78. { start and end of block instructions that defines the }
  79. { content of this register. If Typ = con_const, then }
  80. { Longint(StartMod) = value of the constant) }
  81. StartMod: Tai;
  82. { starts at 0, gets increased everytime the register is }
  83. { written to }
  84. WState: TStateInt;
  85. { starts at 0, gets increased everytime the register is read }
  86. { from }
  87. RState: TStateInt;
  88. { how many instructions starting with StarMod does the block }
  89. { consist of }
  90. NrOfMods: Byte;
  91. { the type of the content of the register: unknown, memory }
  92. { (variable) or constant }
  93. Typ: Byte;
  94. End;
  95. //!!! FIXME
  96. TRegContent = Array[byte] Of TContent;
  97. { ************************************************************************** }
  98. { information object with the contents of every register. Every Tai object }
  99. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  100. { ************************************************************************** }
  101. { TPaiProp }
  102. TPaiProp = class(TAoptBaseCpu)
  103. Regs: TRegContent;
  104. { can this instruction be removed? }
  105. CanBeRemoved: Boolean;
  106. Constructor create; reintroduce;
  107. { checks the whole sequence of which (so regs[which].StartMod and and }
  108. { the next NrOfMods Tai objects) to see whether Reg is used somewhere, }
  109. { without it being loaded with something else first }
  110. Function RegInSequence(Reg, which: TRegister): Boolean;
  111. { destroy the contents of a register, as well as those whose contents }
  112. { are based on those of that register }
  113. Procedure DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  114. TInstrSinceLastMod);
  115. { if the contents of WhichReg (can be R_NO in case of a constant) are }
  116. { written to memory at the location Ref, the contents of the registers }
  117. { that depend on Ref have to be destroyed }
  118. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister; var
  119. InstrSinceLastMod: TInstrSinceLastMod);
  120. { an instruction reads from operand o }
  121. Procedure ReadOp(const o:toper);
  122. { an instruction reads from reference Ref }
  123. Procedure ReadRef(Ref: PReference);
  124. { an instruction reads from register Reg }
  125. Procedure ReadReg(Reg: TRegister);
  126. { an instruction writes/modifies operand o and this has special }
  127. { side-effects or modifies the contents in such a way that we can't }
  128. { simply add this instruction to the sequence of instructions that }
  129. { describe the contents of the operand, so destroy it }
  130. Procedure DestroyOp(const o:Toper; var InstrSinceLastMod:
  131. TInstrSinceLastMod);
  132. { destroy the contents of all registers }
  133. Procedure DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  134. { a register's contents are modified, but not destroyed (the new value
  135. depends on the old one) }
  136. Procedure ModifyReg(reg: TRegister; var InstrSinceLastMod:
  137. TInstrSinceLastMod);
  138. { an operand's contents are modified, but not destroyed (the new value
  139. depends on the old one) }
  140. Procedure ModifyOp(const oper: TOper; var InstrSinceLastMod:
  141. TInstrSinceLastMod);
  142. { increase the write state of a register (call every time a register is
  143. written to) }
  144. Procedure IncWState(Reg: TRegister);
  145. { increase the read state of a register (call every time a register is }
  146. { read from) }
  147. Procedure IncRState(Reg: TRegister);
  148. { get the write state of a register }
  149. Function GetWState(Reg: TRegister): TStateInt;
  150. { get the read state of a register }
  151. Function GetRState(Reg: TRegister): TStateInt;
  152. { get the type of contents of a register }
  153. Function GetRegContentType(Reg: TRegister): Byte;
  154. Destructor Done;
  155. Private
  156. Procedure IncState(var s: TStateInt);
  157. { returns whether the reference Ref is used somewhere in the loading }
  158. { sequence Content }
  159. Function RefInSequence(Const Ref: TReference; Content: TContent;
  160. RefsEq: TRefCompare): Boolean;
  161. { returns whether the instruction P reads from and/or writes }
  162. { to Reg }
  163. Function RefInInstruction(Const Ref: TReference; p: Tai;
  164. RefsEq: TRefCompare): Boolean;
  165. { returns whether two references with at least one pointing to an array }
  166. { may point to the same memory location }
  167. End;
  168. { ************************************************************************* }
  169. { ************************ Label information ****************************** }
  170. { ************************************************************************* }
  171. TLabelTableItem = Record
  172. PaiObj: Tai;
  173. End;
  174. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  175. PLabelTable = ^TLabelTable;
  176. PLabelInfo = ^TLabelInfo;
  177. TLabelInfo = Record
  178. { the highest and lowest label number occurring in the current code }
  179. { fragment }
  180. LowLabel, HighLabel: longint;
  181. LabelDif: cardinal;
  182. { table that contains the addresses of the Pai_Label objects associated
  183. with each label number }
  184. LabelTable: PLabelTable;
  185. End;
  186. { ************************************************************************* }
  187. { ********** General optimizer object, used to derive others from ********* }
  188. { ************************************************************************* }
  189. TAllUsedRegs = array[TRegisterType] of TUsedRegs;
  190. { TAOptObj }
  191. TAOptObj = class(TAoptBaseCpu)
  192. { the PAasmOutput list this optimizer instance works on }
  193. AsmL: TAsmList;
  194. { The labelinfo record contains the addresses of the Tai objects }
  195. { that are labels, how many labels there are and the min and max }
  196. { label numbers }
  197. LabelInfo: PLabelInfo;
  198. { Start and end of the block that is currently being optimized }
  199. BlockStart, BlockEnd: Tai;
  200. DFA: TAOptDFA;
  201. UsedRegs: TAllUsedRegs;
  202. { _AsmL is the PAasmOutpout list that has to be optimized, }
  203. { _BlockStart and _BlockEnd the start and the end of the block }
  204. { that has to be optimized and _LabelInfo a pointer to a }
  205. { TLabelInfo record }
  206. Constructor create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  207. _LabelInfo: PLabelInfo); virtual; reintroduce;
  208. Destructor Destroy;override;
  209. { processor independent methods }
  210. Procedure CreateUsedRegs(var regs: TAllUsedRegs);
  211. Procedure ClearUsedRegs;
  212. Procedure UpdateUsedRegs(p : Tai);
  213. procedure UpdateUsedRegs(var Regs: TAllUsedRegs; p: Tai);
  214. Function CopyUsedRegs(var dest : TAllUsedRegs) : boolean;
  215. Procedure ReleaseUsedRegs(const regs : TAllUsedRegs);
  216. Function RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  217. Procedure IncludeRegInUsedRegs(reg : TRegister;var regs : TAllUsedRegs);
  218. Procedure ExcludeRegFromUsedRegs(reg: TRegister;var regs : TAllUsedRegs);
  219. Function GetAllocationString(const regs : TAllUsedRegs) : string;
  220. { returns true if the label L is found between hp and the next }
  221. { instruction }
  222. Function FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  223. { inserts new_one between prev and foll in AsmL }
  224. Procedure InsertLLItem(prev, foll, new_one: TLinkedListItem);
  225. { If P is a Tai object releveant to the optimizer, P is returned
  226. If it is not relevant tot he optimizer, the first object after P
  227. that is relevant is returned }
  228. Function SkipHead(P: Tai): Tai;
  229. { returns true if the operands o1 and o2 are completely equal }
  230. Function OpsEqual(const o1,o2:toper): Boolean;
  231. { Returns the next ait_alloc object with ratype ra_dealloc for
  232. Reg is found in the block
  233. of Tai's starting with StartPai and ending with the next "real"
  234. instruction. If none is found, it returns
  235. nil }
  236. Function FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  237. { Returns the next ait_alloc object with ratype ra_dealloc
  238. for Reg which is found in the block of Tai's starting with StartPai
  239. and ending with the next "real" instruction. If none is found, it returns
  240. nil }
  241. Function FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  242. { reg used after p? }
  243. function RegUsedAfterInstruction(reg: Tregister; p: tai; var AllUsedRegs: TAllUsedRegs): Boolean;
  244. { traces sucessive jumps to their final destination and sets it, e.g.
  245. je l1 je l3
  246. <code> <code>
  247. l1: becomes l1:
  248. je l2 je l3
  249. <code> <code>
  250. l2: l2:
  251. jmp l3 jmp l3
  252. the level parameter denotes how deeep we have already followed the jump,
  253. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  254. function GetFinalDestination(hp: taicpu; level: longint): boolean;
  255. function getlabelwithsym(sym: tasmlabel): tai;
  256. { peephole optimizer }
  257. procedure PrePeepHoleOpts;
  258. procedure PeepHoleOptPass1;
  259. procedure PeepHoleOptPass2; virtual;
  260. procedure PostPeepHoleOpts;
  261. { processor dependent methods }
  262. // if it returns true, perform a "continue"
  263. function PeepHoleOptPass1Cpu(var p: tai): boolean; virtual;
  264. function PostPeepHoleOptsCpu(var p: tai): boolean; virtual;
  265. End;
  266. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  267. { ***************************** Implementation **************************** }
  268. Implementation
  269. uses
  270. cutils,
  271. globals,
  272. verbose,
  273. procinfo;
  274. { ************************************************************************* }
  275. { ******************************** TUsedRegs ****************************** }
  276. { ************************************************************************* }
  277. Constructor TUsedRegs.create(aTyp : TRegisterType);
  278. Begin
  279. Typ:=aTyp;
  280. UsedRegs := [];
  281. End;
  282. Constructor TUsedRegs.create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  283. Begin
  284. Typ:=aTyp;
  285. UsedRegs := _RegSet;
  286. End;
  287. {
  288. updates UsedRegs with the RegAlloc Information coming after P
  289. }
  290. Procedure TUsedRegs.Update(p: Tai;IgnoreNewAllocs : Boolean = false);
  291. Begin
  292. { this code is normally not used because updating the register allocation information is done in
  293. TAOptObj.UpdateUsedRegs for speed reasons }
  294. repeat
  295. while assigned(p) and
  296. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  297. ((p.typ = ait_label) and
  298. labelCanBeSkipped(tai_label(p))) or
  299. ((p.typ = ait_marker) and
  300. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  301. p := tai(p.next);
  302. while assigned(p) and
  303. (p.typ=ait_RegAlloc) Do
  304. begin
  305. if (getregtype(tai_regalloc(p).reg) = typ) then
  306. begin
  307. case tai_regalloc(p).ratype of
  308. ra_alloc :
  309. if not(IgnoreNewAllocs) then
  310. Include(UsedRegs, getsupreg(tai_regalloc(p).reg));
  311. ra_dealloc :
  312. Exclude(UsedRegs, getsupreg(tai_regalloc(p).reg));
  313. end;
  314. end;
  315. p := tai(p.next);
  316. end;
  317. until not(assigned(p)) or
  318. (not(p.typ in SkipInstr) and
  319. not((p.typ = ait_label) and
  320. labelCanBeSkipped(tai_label(p))));
  321. End;
  322. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  323. Begin
  324. IsUsed := (getregtype(Reg)=Typ) and (getsupreg(Reg) in UsedRegs);
  325. End;
  326. Function TUsedRegs.GetUsedRegs: TRegSet;
  327. Begin
  328. GetUsedRegs := UsedRegs;
  329. End;
  330. Destructor TUsedRegs.Destroy;
  331. Begin
  332. inherited destroy;
  333. end;
  334. procedure TUsedRegs.Clear;
  335. begin
  336. UsedRegs := [];
  337. end;
  338. { ************************************************************************* }
  339. { **************************** TPaiProp *********************************** }
  340. { ************************************************************************* }
  341. Constructor TPaiProp.Create;
  342. Begin
  343. {!!!!!!
  344. UsedRegs.Init;
  345. CondRegs.init;
  346. }
  347. { DirFlag: TFlagContents; I386 specific}
  348. End;
  349. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  350. {
  351. Var p: Tai;
  352. RegsChecked: TRegSet;
  353. content: TContent;
  354. Counter: Byte;
  355. TmpResult: Boolean;
  356. }
  357. begin
  358. Result:=False; { unimplemented }
  359. (*!!!!!!!!!!1
  360. RegsChecked := [];
  361. content := regs[which];
  362. p := content.StartMod;
  363. TmpResult := False;
  364. Counter := 1;
  365. While Not(TmpResult) And
  366. (Counter <= Content.NrOfMods) Do
  367. Begin
  368. If IsLoadMemReg(p) Then
  369. With PInstr(p)^.oper[LoadSrc]^.ref^ Do
  370. If (Base = ProcInfo.FramePointer)
  371. {$ifdef cpurefshaveindexreg}
  372. And (Index = R_NO)
  373. {$endif cpurefshaveindexreg} Then
  374. Begin
  375. RegsChecked := RegsChecked +
  376. [RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg)];
  377. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg) Then
  378. Break;
  379. End
  380. Else
  381. Begin
  382. If (Base = Reg) And
  383. Not(Base In RegsChecked)
  384. Then TmpResult := True;
  385. {$ifdef cpurefshaveindexreg}
  386. If Not(TmpResult) And
  387. (Index = Reg) And
  388. Not(Index In RegsChecked)
  389. Then TmpResult := True;
  390. {$Endif cpurefshaveindexreg}
  391. End
  392. Else TmpResult := RegInInstruction(Reg, p);
  393. Inc(Counter);
  394. GetNextInstruction(p,p)
  395. End;
  396. RegInSequence := TmpResult
  397. *)
  398. End;
  399. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  400. TInstrSinceLastMod);
  401. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  402. { the contents of registers are loaded with a memory location based on Reg }
  403. {
  404. Var TmpWState, TmpRState: Byte;
  405. Counter: TRegister;
  406. }
  407. Begin
  408. {!!!!!!!
  409. Reg := RegMaxSize(Reg);
  410. If (Reg in [LoGPReg..HiGPReg]) Then
  411. For Counter := LoGPReg to HiGPReg Do
  412. With Regs[Counter] Do
  413. If (Counter = reg) Or
  414. ((Typ = Con_Ref) And
  415. RegInSequence(Reg, Counter)) Then
  416. Begin
  417. InstrSinceLastMod[Counter] := 0;
  418. IncWState(Counter);
  419. TmpWState := GetWState(Counter);
  420. TmpRState := GetRState(Counter);
  421. FillChar(Regs[Counter], SizeOf(TContent), 0);
  422. WState := TmpWState;
  423. RState := TmpRState
  424. End
  425. }
  426. End;
  427. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  428. Begin
  429. Result:=False; { unimplemented }
  430. (*!!!!!!!!!!
  431. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  432. {$ifdef refsHaveSegmentReg}
  433. (R1.Segment = R2.Segment) And
  434. {$endif}
  435. (R1.Base = R2.Base) And
  436. (R1.Symbol=R2.Symbol);
  437. *)
  438. End;
  439. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  440. var InstrSinceLastMod: TInstrSinceLastMod);
  441. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  442. { is the register whose contents are being written to memory (if this proc }
  443. { is called because of a "mov?? %reg, (mem)" instruction) }
  444. {
  445. Var RefsEq: TRefCompare;
  446. Counter: TRegister;
  447. }
  448. Begin
  449. (*!!!!!!!!!!!
  450. WhichReg := RegMaxSize(WhichReg);
  451. If (Ref.base = procinfo.FramePointer) or
  452. Assigned(Ref.Symbol) Then
  453. Begin
  454. If
  455. {$ifdef cpurefshaveindexreg}
  456. (Ref.Index = R_NO) And
  457. {$endif cpurefshaveindexreg}
  458. (Not(Assigned(Ref.Symbol)) or
  459. (Ref.base = R_NO)) Then
  460. { local variable which is not an array }
  461. RefsEq := @RefsEqual
  462. Else
  463. { local variable which is an array }
  464. RefsEq := @ArrayRefsEq;
  465. {write something to a parameter, a local or global variable, so
  466. * with uncertain optimizations on:
  467. - destroy the contents of registers whose contents have somewhere a
  468. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  469. are being written to memory) is not destroyed if it's StartMod is
  470. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  471. pointer or value based on Ref)
  472. * with uncertain optimizations off:
  473. - also destroy registers that contain any pointer}
  474. For Counter := LoGPReg to HiGPReg Do
  475. With Regs[Counter] Do
  476. Begin
  477. If (typ = Con_Ref) And
  478. ((Not(cs_opt_size in current_settings.optimizerswitches) And
  479. (NrOfMods <> 1)
  480. ) Or
  481. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  482. ((Counter <> WhichReg) Or
  483. ((NrOfMods <> 1) And
  484. {StarMod is always of the type ait_instruction}
  485. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  486. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  487. )
  488. )
  489. )
  490. )
  491. Then
  492. DestroyReg(Counter, InstrSinceLastMod)
  493. End
  494. End
  495. Else
  496. {write something to a pointer location, so
  497. * with uncertain optimzations on:
  498. - do not destroy registers which contain a local/global variable or a
  499. parameter, except if DestroyRefs is called because of a "movsl"
  500. * with uncertain optimzations off:
  501. - destroy every register which contains a memory location
  502. }
  503. For Counter := LoGPReg to HiGPReg Do
  504. With Regs[Counter] Do
  505. If (typ = Con_Ref) And
  506. (Not(cs_opt_size in current_settings.optimizerswitches) Or
  507. {$ifdef x86}
  508. {for movsl}
  509. (Ref.Base = R_EDI) Or
  510. {$endif}
  511. {don't destroy if reg contains a parameter, local or global variable}
  512. Not((NrOfMods = 1) And
  513. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  514. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  515. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  516. )
  517. )
  518. )
  519. Then DestroyReg(Counter, InstrSinceLastMod)
  520. *)
  521. End;
  522. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  523. {Var Counter: TRegister;}
  524. Begin {initializes/desrtoys all registers}
  525. (*!!!!!!!!!
  526. For Counter := LoGPReg To HiGPReg Do
  527. Begin
  528. ReadReg(Counter);
  529. DestroyReg(Counter, InstrSinceLastMod);
  530. End;
  531. CondRegs.Init;
  532. { FPURegs.Init; }
  533. *)
  534. End;
  535. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  536. TInstrSinceLastMod);
  537. Begin
  538. {!!!!!!!
  539. Case o.typ Of
  540. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  541. top_ref:
  542. Begin
  543. ReadRef(o.ref);
  544. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  545. End;
  546. top_symbol:;
  547. End;
  548. }
  549. End;
  550. Procedure TPaiProp.ReadReg(Reg: TRegister);
  551. Begin
  552. {!!!!!!!
  553. Reg := RegMaxSize(Reg);
  554. If Reg in General_Registers Then
  555. IncRState(RegMaxSize(Reg))
  556. }
  557. End;
  558. Procedure TPaiProp.ReadRef(Ref: PReference);
  559. Begin
  560. (*!!!!!!
  561. If Ref^.Base <> R_NO Then
  562. ReadReg(Ref^.Base);
  563. {$ifdef cpurefshaveindexreg}
  564. If Ref^.Index <> R_NO Then
  565. ReadReg(Ref^.Index);
  566. {$endif cpurefshaveindexreg}
  567. *)
  568. End;
  569. Procedure TPaiProp.ReadOp(const o:toper);
  570. Begin
  571. Case o.typ Of
  572. top_reg: ReadReg(o.reg);
  573. top_ref: ReadRef(o.ref);
  574. else
  575. internalerror(200410241);
  576. End;
  577. End;
  578. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  579. TInstrSinceLastMod);
  580. Begin
  581. (*!!!!!!!
  582. With Regs[reg] Do
  583. If (Typ = Con_Ref)
  584. Then
  585. Begin
  586. IncState(WState);
  587. {also store how many instructions are part of the sequence in the first
  588. instructions PPaiProp, so it can be easily accessed from within
  589. CheckSequence}
  590. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  591. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  592. InstrSinceLastMod[Reg] := 0;
  593. End
  594. Else
  595. DestroyReg(Reg, InstrSinceLastMod);
  596. *)
  597. End;
  598. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  599. TInstrSinceLastMod);
  600. Begin
  601. If oper.typ = top_reg Then
  602. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  603. Else
  604. Begin
  605. ReadOp(oper);
  606. DestroyOp(oper, InstrSinceLastMod);
  607. End
  608. End;
  609. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  610. Begin
  611. //!!!! IncState(Regs[Reg].WState);
  612. End;
  613. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  614. Begin
  615. //!!!! IncState(Regs[Reg].RState);
  616. End;
  617. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  618. Begin
  619. Result:=0; { unimplemented }
  620. //!!!! GetWState := Regs[Reg].WState
  621. End;
  622. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  623. Begin
  624. Result:=0; { unimplemented }
  625. //!!!! GetRState := Regs[Reg].RState
  626. End;
  627. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  628. Begin
  629. Result:=0; { unimplemented }
  630. //!!!! GetRegContentType := Regs[Reg].typ
  631. End;
  632. Destructor TPaiProp.Done;
  633. Begin
  634. //!!!! UsedRegs.Done;
  635. //!!!! CondRegs.Done;
  636. { DirFlag: TFlagContents; I386 specific}
  637. End;
  638. { ************************ private TPaiProp stuff ************************* }
  639. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  640. Begin
  641. If s <> High(TStateInt) Then Inc(s)
  642. Else s := 0
  643. End;
  644. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  645. RefsEq: TRefCompare): Boolean;
  646. Var Count: AWord;
  647. TmpResult: Boolean;
  648. Begin
  649. TmpResult := False;
  650. If (p.typ = ait_instruction) Then
  651. Begin
  652. Count := 0;
  653. Repeat
  654. If (TInstr(p).oper[Count]^.typ = Top_Ref) Then
  655. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count]^.ref^);
  656. Inc(Count);
  657. Until (Count = MaxOps) or TmpResult;
  658. End;
  659. RefInInstruction := TmpResult;
  660. End;
  661. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  662. RefsEq: TRefCompare): Boolean;
  663. Var p: Tai;
  664. Counter: Byte;
  665. TmpResult: Boolean;
  666. Begin
  667. p := Content.StartMod;
  668. TmpResult := False;
  669. Counter := 1;
  670. While Not(TmpResult) And
  671. (Counter <= Content.NrOfMods) Do
  672. Begin
  673. If (p.typ = ait_instruction) And
  674. RefInInstruction(Ref, p, @references_equal)
  675. Then TmpResult := True;
  676. Inc(Counter);
  677. GetNextInstruction(p,p)
  678. End;
  679. RefInSequence := TmpResult
  680. End;
  681. { ************************************************************************* }
  682. { ***************************** TAoptObj ********************************** }
  683. { ************************************************************************* }
  684. Constructor TAoptObj.create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  685. _LabelInfo: PLabelInfo);
  686. Begin
  687. AsmL := _AsmL;
  688. BlockStart := _BlockStart;
  689. BlockEnd := _BlockEnd;
  690. LabelInfo := _LabelInfo;
  691. CreateUsedRegs(UsedRegs);
  692. End;
  693. destructor TAOptObj.Destroy;
  694. var
  695. i : TRegisterType;
  696. begin
  697. for i:=low(TRegisterType) to high(TRegisterType) do
  698. UsedRegs[i].Destroy;
  699. inherited Destroy;
  700. end;
  701. procedure TAOptObj.CreateUsedRegs(var regs: TAllUsedRegs);
  702. var
  703. i : TRegisterType;
  704. begin
  705. for i:=low(TRegisterType) to high(TRegisterType) do
  706. Regs[i]:=TUsedRegs.Create(i);
  707. end;
  708. procedure TAOptObj.ClearUsedRegs;
  709. var
  710. i : TRegisterType;
  711. begin
  712. for i:=low(TRegisterType) to high(TRegisterType) do
  713. UsedRegs[i].Clear;
  714. end;
  715. procedure TAOptObj.UpdateUsedRegs(p : Tai);
  716. var
  717. i : TRegisterType;
  718. begin
  719. { this code is based on TUsedRegs.Update to avoid multiple passes through the asmlist,
  720. the code is duplicated here }
  721. repeat
  722. while assigned(p) and
  723. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  724. ((p.typ = ait_label) and
  725. labelCanBeSkipped(tai_label(p))) or
  726. ((p.typ = ait_marker) and
  727. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  728. p := tai(p.next);
  729. while assigned(p) and
  730. (p.typ=ait_RegAlloc) Do
  731. begin
  732. case tai_regalloc(p).ratype of
  733. ra_alloc :
  734. Include(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  735. ra_dealloc :
  736. Exclude(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  737. end;
  738. p := tai(p.next);
  739. end;
  740. until not(assigned(p)) or
  741. (not(p.typ in SkipInstr) and
  742. not((p.typ = ait_label) and
  743. labelCanBeSkipped(tai_label(p))));
  744. end;
  745. procedure TAOptObj.UpdateUsedRegs(var Regs : TAllUsedRegs;p : Tai);
  746. var
  747. i : TRegisterType;
  748. begin
  749. for i:=low(TRegisterType) to high(TRegisterType) do
  750. Regs[i].Update(p);
  751. end;
  752. function TAOptObj.CopyUsedRegs(var dest: TAllUsedRegs): boolean;
  753. var
  754. i : TRegisterType;
  755. begin
  756. Result:=true;
  757. for i:=low(TRegisterType) to high(TRegisterType) do
  758. dest[i]:=TUsedRegs.Create_Regset(i,UsedRegs[i].GetUsedRegs);
  759. end;
  760. procedure TAOptObj.ReleaseUsedRegs(const regs: TAllUsedRegs);
  761. var
  762. i : TRegisterType;
  763. begin
  764. for i:=low(TRegisterType) to high(TRegisterType) do
  765. regs[i].Free;
  766. end;
  767. Function TAOptObj.RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  768. begin
  769. result:=regs[getregtype(reg)].IsUsed(reg);
  770. end;
  771. procedure TAOptObj.IncludeRegInUsedRegs(reg: TRegister;
  772. var regs: TAllUsedRegs);
  773. begin
  774. include(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  775. end;
  776. procedure TAOptObj.ExcludeRegFromUsedRegs(reg: TRegister;
  777. var regs: TAllUsedRegs);
  778. begin
  779. exclude(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  780. end;
  781. function TAOptObj.GetAllocationString(const regs: TAllUsedRegs): string;
  782. var
  783. i : TRegisterType;
  784. j : TSuperRegister;
  785. begin
  786. Result:='';
  787. for i:=low(TRegisterType) to high(TRegisterType) do
  788. for j in regs[i].UsedRegs do
  789. Result:=Result+std_regname(newreg(i,j,R_SUBWHOLE))+' ';
  790. end;
  791. Function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  792. Var TempP: Tai;
  793. Begin
  794. TempP := hp;
  795. While Assigned(TempP) and
  796. (TempP.typ In SkipInstr + [ait_label,ait_align]) Do
  797. If (TempP.typ <> ait_Label) Or
  798. (Tai_label(TempP).labsym <> L)
  799. Then GetNextInstruction(TempP, TempP)
  800. Else
  801. Begin
  802. hp := TempP;
  803. FindLabel := True;
  804. exit
  805. End;
  806. FindLabel := False;
  807. End;
  808. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  809. Begin
  810. If Assigned(prev) Then
  811. If Assigned(foll) Then
  812. Begin
  813. If Assigned(new_one) Then
  814. Begin
  815. new_one.previous := prev;
  816. new_one.next := foll;
  817. prev.next := new_one;
  818. foll.previous := new_one;
  819. { should we update line information? }
  820. if (not (tai(new_one).typ in SkipLineInfo)) and
  821. (not (tai(foll).typ in SkipLineInfo)) then
  822. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  823. End
  824. End
  825. Else AsmL.Concat(new_one)
  826. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  827. End;
  828. Function TAOptObj.SkipHead(P: Tai): Tai;
  829. Var OldP: Tai;
  830. Begin
  831. Repeat
  832. OldP := P;
  833. If (P.typ in SkipInstr) Or
  834. ((P.typ = ait_marker) And
  835. (Tai_Marker(P).Kind = mark_AsmBlockEnd)) Then
  836. GetNextInstruction(P, P)
  837. Else If ((P.Typ = Ait_Marker) And
  838. (Tai_Marker(P).Kind = mark_NoPropInfoStart)) Then
  839. { a marker of the type mark_NoPropInfoStart can't be the first instruction of a }
  840. { paasmoutput list }
  841. GetNextInstruction(Tai(P.Previous),P);
  842. If (P.Typ = Ait_Marker) And
  843. (Tai_Marker(P).Kind = mark_AsmBlockStart) Then
  844. Begin
  845. P := Tai(P.Next);
  846. While (P.typ <> Ait_Marker) Or
  847. (Tai_Marker(P).Kind <> mark_AsmBlockEnd) Do
  848. P := Tai(P.Next)
  849. End;
  850. Until P = OldP;
  851. SkipHead := P;
  852. End;
  853. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  854. Begin
  855. if o1.typ=o2.typ then
  856. Case o1.typ Of
  857. Top_Reg :
  858. OpsEqual:=o1.reg=o2.reg;
  859. Top_Ref :
  860. OpsEqual := references_equal(o1.ref^, o2.ref^);
  861. Top_Const :
  862. OpsEqual:=o1.val=o2.val;
  863. Top_None :
  864. OpsEqual := True
  865. else OpsEqual := False
  866. End
  867. else
  868. OpsEqual := False;
  869. End;
  870. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  871. Begin
  872. Result:=nil;
  873. Repeat
  874. While Assigned(StartPai) And
  875. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  876. ((StartPai.typ = ait_label) and
  877. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  878. StartPai := Tai(StartPai.Next);
  879. If Assigned(StartPai) And
  880. (StartPai.typ = ait_regAlloc) Then
  881. Begin
  882. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  883. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  884. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  885. begin
  886. Result:=tai_regalloc(StartPai);
  887. exit;
  888. end;
  889. StartPai := Tai(StartPai.Next);
  890. End
  891. else
  892. exit;
  893. Until false;
  894. End;
  895. function TAOptObj.FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  896. Begin
  897. Result:=nil;
  898. Repeat
  899. While Assigned(StartPai) And
  900. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  901. ((StartPai.typ = ait_label) and
  902. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  903. StartPai := Tai(StartPai.Next);
  904. If Assigned(StartPai) And
  905. (StartPai.typ = ait_regAlloc) Then
  906. Begin
  907. if (tai_regalloc(StartPai).ratype=ra_dealloc) and
  908. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  909. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  910. begin
  911. Result:=tai_regalloc(StartPai);
  912. exit;
  913. end;
  914. StartPai := Tai(StartPai.Next);
  915. End
  916. else
  917. exit;
  918. Until false;
  919. End;
  920. function TAOptObj.RegUsedAfterInstruction(reg: Tregister; p: tai;
  921. var AllUsedRegs: TAllUsedRegs): Boolean;
  922. begin
  923. AllUsedRegs[getregtype(reg)].Update(tai(p.Next));
  924. RegUsedAfterInstruction :=
  925. (AllUsedRegs[getregtype(reg)].IsUsed(reg)); { optimization and
  926. (not(getNextInstruction(p,p)) or
  927. not(regLoadedWithNewValue(supreg,false,p))); }
  928. end;
  929. function SkipLabels(hp: tai; var hp2: tai): boolean;
  930. {skips all labels and returns the next "real" instruction}
  931. begin
  932. while assigned(hp.next) and
  933. (tai(hp.next).typ in SkipInstr + [ait_label,ait_align]) Do
  934. hp := tai(hp.next);
  935. if assigned(hp.next) then
  936. begin
  937. SkipLabels := True;
  938. hp2 := tai(hp.next)
  939. end
  940. else
  941. begin
  942. hp2 := hp;
  943. SkipLabels := False
  944. end;
  945. end;
  946. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  947. begin
  948. FindAnyLabel := false;
  949. while assigned(hp.next) and
  950. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  951. hp := tai(hp.next);
  952. if assigned(hp.next) and
  953. (tai(hp.next).typ = ait_label) then
  954. begin
  955. FindAnyLabel := true;
  956. l := tai_label(hp.next).labsym;
  957. end
  958. end;
  959. {$push}
  960. {$r-}
  961. function tAOptObj.getlabelwithsym(sym: tasmlabel): tai;
  962. begin
  963. if (int64(sym.labelnr) >= int64(labelinfo^.lowlabel)) and
  964. (int64(sym.labelnr) <= int64(labelinfo^.highlabel)) then { range check, a jump can go past an assembler block! }
  965. getlabelwithsym := labelinfo^.labeltable^[sym.labelnr-labelinfo^.lowlabel].paiobj
  966. else
  967. getlabelwithsym := nil;
  968. end;
  969. {$pop}
  970. function TAOptObj.GetFinalDestination(hp: taicpu; level: longint): boolean;
  971. {traces sucessive jumps to their final destination and sets it, e.g.
  972. je l1 je l3
  973. <code> <code>
  974. l1: becomes l1:
  975. je l2 je l3
  976. <code> <code>
  977. l2: l2:
  978. jmp l3 jmp l3
  979. the level parameter denotes how deeep we have already followed the jump,
  980. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  981. var p1, p2: tai;
  982. l: tasmlabel;
  983. begin
  984. GetfinalDestination := false;
  985. if level > 20 then
  986. exit;
  987. p1 := getlabelwithsym(tasmlabel(hp.oper[0]^.ref^.symbol));
  988. if assigned(p1) then
  989. begin
  990. SkipLabels(p1,p1);
  991. if (tai(p1).typ = ait_instruction) and
  992. (taicpu(p1).is_jmp) then
  993. if { the next instruction after the label where the jump hp arrives}
  994. { is unconditional or of the same type as hp, so continue }
  995. (((taicpu(p1).opcode = aopt_uncondjmp) and
  996. {$ifdef arm}
  997. (taicpu(p1).condition = C_None) and
  998. {$endif arm}
  999. (taicpu(p1).oper[0]^.typ = top_ref) and
  1000. (assigned(taicpu(p1).oper[0]^.ref^.symbol)) and
  1001. (taicpu(p1).oper[0]^.ref^.symbol is TAsmLabel)) or
  1002. conditions_equal(taicpu(p1).condition,hp.condition)) or
  1003. { the next instruction after the label where the jump hp arrives
  1004. is the opposite of hp (so this one is never taken), but after
  1005. that one there is a branch that will be taken, so perform a
  1006. little hack: set p1 equal to this instruction (that's what the
  1007. last SkipLabels is for, only works with short bool evaluation)}
  1008. (conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) and
  1009. SkipLabels(p1,p2) and
  1010. (p2.typ = ait_instruction) and
  1011. (taicpu(p2).is_jmp) and
  1012. (((taicpu(p2).opcode = aopt_uncondjmp) and
  1013. {$ifdef arm}
  1014. (taicpu(p1).condition = C_None) and
  1015. {$endif arm}
  1016. (taicpu(p2).oper[0]^.typ = top_ref) and
  1017. (assigned(taicpu(p2).oper[0]^.ref^.symbol)) and
  1018. (taicpu(p2).oper[0]^.ref^.symbol is TAsmLabel)) or
  1019. (conditions_equal(taicpu(p2).condition,hp.condition))) and
  1020. SkipLabels(p1,p1)) then
  1021. begin
  1022. { quick check for loops of the form "l5: ; jmp l5 }
  1023. if (tasmlabel(taicpu(p1).oper[0]^.ref^.symbol).labelnr =
  1024. tasmlabel(hp.oper[0]^.ref^.symbol).labelnr) then
  1025. exit;
  1026. if not GetFinalDestination(taicpu(p1),succ(level)) then
  1027. exit;
  1028. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  1029. hp.oper[0]^.ref^.symbol:=taicpu(p1).oper[0]^.ref^.symbol;
  1030. tasmlabel(hp.oper[0]^.ref^.symbol).increfs;
  1031. end
  1032. else
  1033. if conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) then
  1034. if not FindAnyLabel(p1,l) then
  1035. begin
  1036. {$ifdef finaldestdebug}
  1037. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1038. strpnew('previous label inserted'))));
  1039. {$endif finaldestdebug}
  1040. current_asmdata.getjumplabel(l);
  1041. insertllitem(p1,p1.next,tai_label.Create(l));
  1042. tasmlabel(taicpu(hp).oper[0]^.ref^.symbol).decrefs;
  1043. hp.oper[0]^.ref^.symbol := l;
  1044. l.increfs;
  1045. { this won't work, since the new label isn't in the labeltable }
  1046. { so it will fail the rangecheck. Labeltable should become a }
  1047. { hashtable to support this: }
  1048. { GetFinalDestination(asml, hp); }
  1049. end
  1050. else
  1051. begin
  1052. {$ifdef finaldestdebug}
  1053. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1054. strpnew('next label reused'))));
  1055. {$endif finaldestdebug}
  1056. l.increfs;
  1057. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  1058. hp.oper[0]^.ref^.symbol := l;
  1059. if not GetFinalDestination(hp,succ(level)) then
  1060. exit;
  1061. end;
  1062. end;
  1063. GetFinalDestination := true;
  1064. end;
  1065. procedure TAOptObj.PrePeepHoleOpts;
  1066. begin
  1067. end;
  1068. procedure TAOptObj.PeepHoleOptPass1;
  1069. var
  1070. p,hp1,hp2 : tai;
  1071. begin
  1072. p := BlockStart;
  1073. ClearUsedRegs;
  1074. while (p <> BlockEnd) Do
  1075. begin
  1076. { I'am not sure why this is done, UsedRegs should reflect the register usage before the instruction
  1077. If an instruction needs the information of this, it can easily create a TempUsedRegs (FK)
  1078. UpdateUsedRegs(tai(p.next));
  1079. }
  1080. {$ifdef DEBUG_OPTALLOC}
  1081. if p.Typ=ait_instruction then
  1082. InsertLLItem(tai(p.Previous),p,tai_comment.create(strpnew(GetAllocationString(UsedRegs))));
  1083. {$endif DEBUG_OPTALLOC}
  1084. if PeepHoleOptPass1Cpu(p) then
  1085. begin
  1086. UpdateUsedRegs(p);
  1087. continue;
  1088. end;
  1089. case p.Typ Of
  1090. ait_instruction:
  1091. begin
  1092. { Handle Jmp Optimizations }
  1093. if taicpu(p).is_jmp then
  1094. begin
  1095. { the following if-block removes all code between a jmp and the next label,
  1096. because it can never be executed
  1097. }
  1098. if (taicpu(p).opcode = aopt_uncondjmp) and
  1099. {$ifdef arm}
  1100. (taicpu(p).condition = C_None) and
  1101. {$endif arm}
  1102. (taicpu(p).oper[0]^.typ = top_ref) and
  1103. (assigned(taicpu(p).oper[0]^.ref^.symbol)) and
  1104. (taicpu(p).oper[0]^.ref^.symbol is TAsmLabel) then
  1105. begin
  1106. hp2:=p;
  1107. while GetNextInstruction(hp2, hp1) and
  1108. (hp1.typ <> ait_label) do
  1109. if not(hp1.typ in ([ait_label,ait_align]+skipinstr)) then
  1110. begin
  1111. if (hp1.typ = ait_instruction) and
  1112. taicpu(hp1).is_jmp and
  1113. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1114. assigned(taicpu(hp1).oper[0]^.ref^.symbol) and
  1115. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) then
  1116. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  1117. { don't kill start/end of assembler block,
  1118. no-line-info-start/end etc }
  1119. if hp1.typ<>ait_marker then
  1120. begin
  1121. asml.remove(hp1);
  1122. hp1.free;
  1123. end
  1124. else
  1125. hp2:=hp1;
  1126. end
  1127. else break;
  1128. end;
  1129. { remove jumps to a label coming right after them }
  1130. if GetNextInstruction(p, hp1) then
  1131. begin
  1132. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp1) and
  1133. { TODO: FIXME removing the first instruction fails}
  1134. (p<>blockstart) then
  1135. begin
  1136. hp2:=tai(hp1.next);
  1137. asml.remove(p);
  1138. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  1139. p.free;
  1140. p:=hp2;
  1141. continue;
  1142. end
  1143. else
  1144. begin
  1145. if hp1.typ = ait_label then
  1146. SkipLabels(hp1,hp1);
  1147. if (tai(hp1).typ=ait_instruction) and
  1148. (taicpu(hp1).opcode=aopt_uncondjmp) and
  1149. {$ifdef arm}
  1150. (taicpu(hp1).condition=C_None) and
  1151. {$endif arm}
  1152. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1153. (assigned(taicpu(hp1).oper[0]^.ref^.symbol)) and
  1154. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  1155. GetNextInstruction(hp1, hp2) and
  1156. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp2) then
  1157. begin
  1158. if (taicpu(p).opcode=aopt_condjmp)
  1159. {$ifdef arm}
  1160. and (taicpu(p).condition<>C_None)
  1161. {$endif arm}
  1162. then
  1163. begin
  1164. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  1165. tai_label(hp2).labsym.decrefs;
  1166. taicpu(p).oper[0]^.ref^.symbol:=taicpu(hp1).oper[0]^.ref^.symbol;
  1167. { when freeing hp1, the reference count
  1168. isn't decreased, so don't increase
  1169. taicpu(p).oper[0]^.ref^.symbol.increfs;
  1170. }
  1171. {$ifdef SPARC}
  1172. hp2:=tai(hp1.next);
  1173. asml.remove(hp2);
  1174. hp2.free;
  1175. {$endif SPARC}
  1176. asml.remove(hp1);
  1177. hp1.free;
  1178. GetFinalDestination(taicpu(p),0);
  1179. end
  1180. else
  1181. begin
  1182. GetFinalDestination(taicpu(p),0);
  1183. p:=tai(p.next);
  1184. continue;
  1185. end;
  1186. end
  1187. else
  1188. GetFinalDestination(taicpu(p),0);
  1189. end;
  1190. end;
  1191. end
  1192. else
  1193. { All other optimizes }
  1194. begin
  1195. end; { if is_jmp }
  1196. end;
  1197. end;
  1198. UpdateUsedRegs(p);
  1199. p:=tai(p.next);
  1200. end;
  1201. end;
  1202. procedure TAOptObj.PeepHoleOptPass2;
  1203. begin
  1204. end;
  1205. procedure TAOptObj.PostPeepHoleOpts;
  1206. var
  1207. p: tai;
  1208. begin
  1209. p := BlockStart;
  1210. ClearUsedRegs;
  1211. while (p <> BlockEnd) Do
  1212. begin
  1213. UpdateUsedRegs(tai(p.next));
  1214. if PostPeepHoleOptsCpu(p) then
  1215. continue;
  1216. UpdateUsedRegs(p);
  1217. p:=tai(p.next);
  1218. end;
  1219. end;
  1220. function TAOptObj.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1221. begin
  1222. result := false;
  1223. end;
  1224. function TAOptObj.PostPeepHoleOptsCpu(var p: tai): boolean;
  1225. begin
  1226. result := false;
  1227. end;
  1228. End.