aoptobj.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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);
  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 true if a ait_alloc object for Reg is found in the block
  232. of Tai's starting with StartPai and ending with the next "real"
  233. instruction }
  234. Function FindRegAlloc(Reg: TRegister; StartPai: Tai): Boolean;
  235. { reg used after p? }
  236. function RegUsedAfterInstruction(reg: Tregister; p: tai; var AllUsedRegs: TAllUsedRegs): Boolean;
  237. { traces sucessive jumps to their final destination and sets it, e.g.
  238. je l1 je l3
  239. <code> <code>
  240. l1: becomes l1:
  241. je l2 je l3
  242. <code> <code>
  243. l2: l2:
  244. jmp l3 jmp l3
  245. the level parameter denotes how deeep we have already followed the jump,
  246. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  247. function GetFinalDestination(hp: taicpu; level: longint): boolean;
  248. function getlabelwithsym(sym: tasmlabel): tai;
  249. { peephole optimizer }
  250. procedure PrePeepHoleOpts;
  251. procedure PeepHoleOptPass1;
  252. procedure PeepHoleOptPass2; virtual;
  253. procedure PostPeepHoleOpts;
  254. { processor dependent methods }
  255. // if it returns true, perform a "continue"
  256. function PeepHoleOptPass1Cpu(var p: tai): boolean; virtual;
  257. function PostPeepHoleOptsCpu(var p: tai): boolean; virtual;
  258. End;
  259. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  260. { ***************************** Implementation **************************** }
  261. Implementation
  262. uses
  263. cutils,
  264. globals,
  265. verbose,
  266. procinfo;
  267. { ************************************************************************* }
  268. { ******************************** TUsedRegs ****************************** }
  269. { ************************************************************************* }
  270. Constructor TUsedRegs.create(aTyp : TRegisterType);
  271. Begin
  272. Typ:=aTyp;
  273. UsedRegs := [];
  274. End;
  275. Constructor TUsedRegs.create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  276. Begin
  277. Typ:=aTyp;
  278. UsedRegs := _RegSet;
  279. End;
  280. {
  281. updates UsedRegs with the RegAlloc Information coming after P
  282. }
  283. Procedure TUsedRegs.Update(p: Tai);
  284. Begin
  285. { this code is normally not used because updating the register allocation information is done in
  286. TAOptObj.UpdateUsedRegs for speed reasons }
  287. repeat
  288. while assigned(p) and
  289. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  290. ((p.typ = ait_label) and
  291. labelCanBeSkipped(tai_label(p))) or
  292. ((p.typ = ait_marker) and
  293. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  294. p := tai(p.next);
  295. while assigned(p) and
  296. (p.typ=ait_RegAlloc) Do
  297. begin
  298. if (getregtype(tai_regalloc(p).reg) = typ) then
  299. begin
  300. case tai_regalloc(p).ratype of
  301. ra_alloc :
  302. Include(UsedRegs, getsupreg(tai_regalloc(p).reg));
  303. ra_dealloc :
  304. Exclude(UsedRegs, getsupreg(tai_regalloc(p).reg));
  305. end;
  306. end;
  307. p := tai(p.next);
  308. end;
  309. until not(assigned(p)) or
  310. (not(p.typ in SkipInstr) and
  311. not((p.typ = ait_label) and
  312. labelCanBeSkipped(tai_label(p))));
  313. End;
  314. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  315. Begin
  316. IsUsed := (getregtype(Reg)=Typ) and (getsupreg(Reg) in UsedRegs);
  317. End;
  318. Function TUsedRegs.GetUsedRegs: TRegSet;
  319. Begin
  320. GetUsedRegs := UsedRegs;
  321. End;
  322. Destructor TUsedRegs.Destroy;
  323. Begin
  324. inherited destroy;
  325. end;
  326. procedure TUsedRegs.Clear;
  327. begin
  328. UsedRegs := [];
  329. end;
  330. { ************************************************************************* }
  331. { **************************** TPaiProp *********************************** }
  332. { ************************************************************************* }
  333. Constructor TPaiProp.Create;
  334. Begin
  335. {!!!!!!
  336. UsedRegs.Init;
  337. CondRegs.init;
  338. }
  339. { DirFlag: TFlagContents; I386 specific}
  340. End;
  341. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  342. {
  343. Var p: Tai;
  344. RegsChecked: TRegSet;
  345. content: TContent;
  346. Counter: Byte;
  347. TmpResult: Boolean;
  348. }
  349. begin
  350. Result:=False; { unimplemented }
  351. (*!!!!!!!!!!1
  352. RegsChecked := [];
  353. content := regs[which];
  354. p := content.StartMod;
  355. TmpResult := False;
  356. Counter := 1;
  357. While Not(TmpResult) And
  358. (Counter <= Content.NrOfMods) Do
  359. Begin
  360. If IsLoadMemReg(p) Then
  361. With PInstr(p)^.oper[LoadSrc]^.ref^ Do
  362. If (Base = ProcInfo.FramePointer)
  363. {$ifdef cpurefshaveindexreg}
  364. And (Index = R_NO)
  365. {$endif cpurefshaveindexreg} Then
  366. Begin
  367. RegsChecked := RegsChecked +
  368. [RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg)];
  369. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg) Then
  370. Break;
  371. End
  372. Else
  373. Begin
  374. If (Base = Reg) And
  375. Not(Base In RegsChecked)
  376. Then TmpResult := True;
  377. {$ifdef cpurefshaveindexreg}
  378. If Not(TmpResult) And
  379. (Index = Reg) And
  380. Not(Index In RegsChecked)
  381. Then TmpResult := True;
  382. {$Endif cpurefshaveindexreg}
  383. End
  384. Else TmpResult := RegInInstruction(Reg, p);
  385. Inc(Counter);
  386. GetNextInstruction(p,p)
  387. End;
  388. RegInSequence := TmpResult
  389. *)
  390. End;
  391. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  392. TInstrSinceLastMod);
  393. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  394. { the contents of registers are loaded with a memory location based on Reg }
  395. {
  396. Var TmpWState, TmpRState: Byte;
  397. Counter: TRegister;
  398. }
  399. Begin
  400. {!!!!!!!
  401. Reg := RegMaxSize(Reg);
  402. If (Reg in [LoGPReg..HiGPReg]) Then
  403. For Counter := LoGPReg to HiGPReg Do
  404. With Regs[Counter] Do
  405. If (Counter = reg) Or
  406. ((Typ = Con_Ref) And
  407. RegInSequence(Reg, Counter)) Then
  408. Begin
  409. InstrSinceLastMod[Counter] := 0;
  410. IncWState(Counter);
  411. TmpWState := GetWState(Counter);
  412. TmpRState := GetRState(Counter);
  413. FillChar(Regs[Counter], SizeOf(TContent), 0);
  414. WState := TmpWState;
  415. RState := TmpRState
  416. End
  417. }
  418. End;
  419. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  420. Begin
  421. Result:=False; { unimplemented }
  422. (*!!!!!!!!!!
  423. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  424. {$ifdef refsHaveSegmentReg}
  425. (R1.Segment = R2.Segment) And
  426. {$endif}
  427. (R1.Base = R2.Base) And
  428. (R1.Symbol=R2.Symbol);
  429. *)
  430. End;
  431. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  432. var InstrSinceLastMod: TInstrSinceLastMod);
  433. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  434. { is the register whose contents are being written to memory (if this proc }
  435. { is called because of a "mov?? %reg, (mem)" instruction) }
  436. {
  437. Var RefsEq: TRefCompare;
  438. Counter: TRegister;
  439. }
  440. Begin
  441. (*!!!!!!!!!!!
  442. WhichReg := RegMaxSize(WhichReg);
  443. If (Ref.base = procinfo.FramePointer) or
  444. Assigned(Ref.Symbol) Then
  445. Begin
  446. If
  447. {$ifdef cpurefshaveindexreg}
  448. (Ref.Index = R_NO) And
  449. {$endif cpurefshaveindexreg}
  450. (Not(Assigned(Ref.Symbol)) or
  451. (Ref.base = R_NO)) Then
  452. { local variable which is not an array }
  453. RefsEq := @RefsEqual
  454. Else
  455. { local variable which is an array }
  456. RefsEq := @ArrayRefsEq;
  457. {write something to a parameter, a local or global variable, so
  458. * with uncertain optimizations on:
  459. - destroy the contents of registers whose contents have somewhere a
  460. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  461. are being written to memory) is not destroyed if it's StartMod is
  462. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  463. pointer or value based on Ref)
  464. * with uncertain optimizations off:
  465. - also destroy registers that contain any pointer}
  466. For Counter := LoGPReg to HiGPReg Do
  467. With Regs[Counter] Do
  468. Begin
  469. If (typ = Con_Ref) And
  470. ((Not(cs_opt_size in current_settings.optimizerswitches) And
  471. (NrOfMods <> 1)
  472. ) Or
  473. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  474. ((Counter <> WhichReg) Or
  475. ((NrOfMods <> 1) And
  476. {StarMod is always of the type ait_instruction}
  477. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  478. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  479. )
  480. )
  481. )
  482. )
  483. Then
  484. DestroyReg(Counter, InstrSinceLastMod)
  485. End
  486. End
  487. Else
  488. {write something to a pointer location, so
  489. * with uncertain optimzations on:
  490. - do not destroy registers which contain a local/global variable or a
  491. parameter, except if DestroyRefs is called because of a "movsl"
  492. * with uncertain optimzations off:
  493. - destroy every register which contains a memory location
  494. }
  495. For Counter := LoGPReg to HiGPReg Do
  496. With Regs[Counter] Do
  497. If (typ = Con_Ref) And
  498. (Not(cs_opt_size in current_settings.optimizerswitches) Or
  499. {$ifdef x86}
  500. {for movsl}
  501. (Ref.Base = R_EDI) Or
  502. {$endif}
  503. {don't destroy if reg contains a parameter, local or global variable}
  504. Not((NrOfMods = 1) And
  505. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  506. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  507. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  508. )
  509. )
  510. )
  511. Then DestroyReg(Counter, InstrSinceLastMod)
  512. *)
  513. End;
  514. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  515. {Var Counter: TRegister;}
  516. Begin {initializes/desrtoys all registers}
  517. (*!!!!!!!!!
  518. For Counter := LoGPReg To HiGPReg Do
  519. Begin
  520. ReadReg(Counter);
  521. DestroyReg(Counter, InstrSinceLastMod);
  522. End;
  523. CondRegs.Init;
  524. { FPURegs.Init; }
  525. *)
  526. End;
  527. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  528. TInstrSinceLastMod);
  529. Begin
  530. {!!!!!!!
  531. Case o.typ Of
  532. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  533. top_ref:
  534. Begin
  535. ReadRef(o.ref);
  536. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  537. End;
  538. top_symbol:;
  539. End;
  540. }
  541. End;
  542. Procedure TPaiProp.ReadReg(Reg: TRegister);
  543. Begin
  544. {!!!!!!!
  545. Reg := RegMaxSize(Reg);
  546. If Reg in General_Registers Then
  547. IncRState(RegMaxSize(Reg))
  548. }
  549. End;
  550. Procedure TPaiProp.ReadRef(Ref: PReference);
  551. Begin
  552. (*!!!!!!
  553. If Ref^.Base <> R_NO Then
  554. ReadReg(Ref^.Base);
  555. {$ifdef cpurefshaveindexreg}
  556. If Ref^.Index <> R_NO Then
  557. ReadReg(Ref^.Index);
  558. {$endif cpurefshaveindexreg}
  559. *)
  560. End;
  561. Procedure TPaiProp.ReadOp(const o:toper);
  562. Begin
  563. Case o.typ Of
  564. top_reg: ReadReg(o.reg);
  565. top_ref: ReadRef(o.ref);
  566. else
  567. internalerror(200410241);
  568. End;
  569. End;
  570. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  571. TInstrSinceLastMod);
  572. Begin
  573. (*!!!!!!!
  574. With Regs[reg] Do
  575. If (Typ = Con_Ref)
  576. Then
  577. Begin
  578. IncState(WState);
  579. {also store how many instructions are part of the sequence in the first
  580. instructions PPaiProp, so it can be easily accessed from within
  581. CheckSequence}
  582. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  583. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  584. InstrSinceLastMod[Reg] := 0;
  585. End
  586. Else
  587. DestroyReg(Reg, InstrSinceLastMod);
  588. *)
  589. End;
  590. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  591. TInstrSinceLastMod);
  592. Begin
  593. If oper.typ = top_reg Then
  594. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  595. Else
  596. Begin
  597. ReadOp(oper);
  598. DestroyOp(oper, InstrSinceLastMod);
  599. End
  600. End;
  601. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  602. Begin
  603. //!!!! IncState(Regs[Reg].WState);
  604. End;
  605. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  606. Begin
  607. //!!!! IncState(Regs[Reg].RState);
  608. End;
  609. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  610. Begin
  611. Result:=0; { unimplemented }
  612. //!!!! GetWState := Regs[Reg].WState
  613. End;
  614. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  615. Begin
  616. Result:=0; { unimplemented }
  617. //!!!! GetRState := Regs[Reg].RState
  618. End;
  619. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  620. Begin
  621. Result:=0; { unimplemented }
  622. //!!!! GetRegContentType := Regs[Reg].typ
  623. End;
  624. Destructor TPaiProp.Done;
  625. Begin
  626. //!!!! UsedRegs.Done;
  627. //!!!! CondRegs.Done;
  628. { DirFlag: TFlagContents; I386 specific}
  629. End;
  630. { ************************ private TPaiProp stuff ************************* }
  631. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  632. Begin
  633. If s <> High(TStateInt) Then Inc(s)
  634. Else s := 0
  635. End;
  636. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  637. RefsEq: TRefCompare): Boolean;
  638. Var Count: AWord;
  639. TmpResult: Boolean;
  640. Begin
  641. TmpResult := False;
  642. If (p.typ = ait_instruction) Then
  643. Begin
  644. Count := 0;
  645. Repeat
  646. If (TInstr(p).oper[Count]^.typ = Top_Ref) Then
  647. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count]^.ref^);
  648. Inc(Count);
  649. Until (Count = MaxOps) or TmpResult;
  650. End;
  651. RefInInstruction := TmpResult;
  652. End;
  653. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  654. RefsEq: TRefCompare): Boolean;
  655. Var p: Tai;
  656. Counter: Byte;
  657. TmpResult: Boolean;
  658. Begin
  659. p := Content.StartMod;
  660. TmpResult := False;
  661. Counter := 1;
  662. While Not(TmpResult) And
  663. (Counter <= Content.NrOfMods) Do
  664. Begin
  665. If (p.typ = ait_instruction) And
  666. RefInInstruction(Ref, p, @references_equal)
  667. Then TmpResult := True;
  668. Inc(Counter);
  669. GetNextInstruction(p,p)
  670. End;
  671. RefInSequence := TmpResult
  672. End;
  673. { ************************************************************************* }
  674. { ***************************** TAoptObj ********************************** }
  675. { ************************************************************************* }
  676. Constructor TAoptObj.create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  677. _LabelInfo: PLabelInfo);
  678. Begin
  679. AsmL := _AsmL;
  680. BlockStart := _BlockStart;
  681. BlockEnd := _BlockEnd;
  682. LabelInfo := _LabelInfo;
  683. CreateUsedRegs(UsedRegs);
  684. End;
  685. destructor TAOptObj.Destroy;
  686. var
  687. i : TRegisterType;
  688. begin
  689. for i:=low(TRegisterType) to high(TRegisterType) do
  690. UsedRegs[i].Destroy;
  691. inherited Destroy;
  692. end;
  693. procedure TAOptObj.CreateUsedRegs(var regs: TAllUsedRegs);
  694. var
  695. i : TRegisterType;
  696. begin
  697. for i:=low(TRegisterType) to high(TRegisterType) do
  698. Regs[i]:=TUsedRegs.Create(i);
  699. end;
  700. procedure TAOptObj.ClearUsedRegs;
  701. var
  702. i : TRegisterType;
  703. begin
  704. for i:=low(TRegisterType) to high(TRegisterType) do
  705. UsedRegs[i].Clear;
  706. end;
  707. procedure TAOptObj.UpdateUsedRegs(p : Tai);
  708. var
  709. i : TRegisterType;
  710. begin
  711. { this code is based on TUsedRegs.Update to avoid multiple passes through the asmlist,
  712. the code is duplicated here }
  713. repeat
  714. while assigned(p) and
  715. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  716. ((p.typ = ait_label) and
  717. labelCanBeSkipped(tai_label(p))) or
  718. ((p.typ = ait_marker) and
  719. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  720. p := tai(p.next);
  721. while assigned(p) and
  722. (p.typ=ait_RegAlloc) Do
  723. begin
  724. case tai_regalloc(p).ratype of
  725. ra_alloc :
  726. Include(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  727. ra_dealloc :
  728. Exclude(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  729. end;
  730. p := tai(p.next);
  731. end;
  732. until not(assigned(p)) or
  733. (not(p.typ in SkipInstr) and
  734. not((p.typ = ait_label) and
  735. labelCanBeSkipped(tai_label(p))));
  736. end;
  737. procedure TAOptObj.UpdateUsedRegs(var Regs : TAllUsedRegs;p : Tai);
  738. var
  739. i : TRegisterType;
  740. begin
  741. for i:=low(TRegisterType) to high(TRegisterType) do
  742. Regs[i].Update(p);
  743. end;
  744. function TAOptObj.CopyUsedRegs(var dest: TAllUsedRegs): boolean;
  745. var
  746. i : TRegisterType;
  747. begin
  748. Result:=true;
  749. for i:=low(TRegisterType) to high(TRegisterType) do
  750. dest[i]:=TUsedRegs.Create_Regset(i,UsedRegs[i].GetUsedRegs);
  751. end;
  752. procedure TAOptObj.ReleaseUsedRegs(const regs: TAllUsedRegs);
  753. var
  754. i : TRegisterType;
  755. begin
  756. for i:=low(TRegisterType) to high(TRegisterType) do
  757. regs[i].Free;
  758. end;
  759. Function TAOptObj.RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  760. begin
  761. result:=regs[getregtype(reg)].IsUsed(reg);
  762. end;
  763. procedure TAOptObj.IncludeRegInUsedRegs(reg: TRegister;
  764. var regs: TAllUsedRegs);
  765. begin
  766. include(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  767. end;
  768. procedure TAOptObj.ExcludeRegFromUsedRegs(reg: TRegister;
  769. var regs: TAllUsedRegs);
  770. begin
  771. exclude(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  772. end;
  773. function TAOptObj.GetAllocationString(const regs: TAllUsedRegs): string;
  774. var
  775. i : TRegisterType;
  776. j : TSuperRegister;
  777. begin
  778. Result:='';
  779. for i:=low(TRegisterType) to high(TRegisterType) do
  780. for j in regs[i].UsedRegs do
  781. Result:=Result+std_regname(newreg(i,j,R_SUBWHOLE))+' ';
  782. end;
  783. Function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  784. Var TempP: Tai;
  785. Begin
  786. TempP := hp;
  787. While Assigned(TempP) and
  788. (TempP.typ In SkipInstr + [ait_label]) Do
  789. If (TempP.typ <> ait_Label) Or
  790. (Tai_label(TempP).labsym <> L)
  791. Then GetNextInstruction(TempP, TempP)
  792. Else
  793. Begin
  794. hp := TempP;
  795. FindLabel := True;
  796. exit
  797. End;
  798. FindLabel := False;
  799. End;
  800. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  801. Begin
  802. If Assigned(prev) Then
  803. If Assigned(foll) Then
  804. Begin
  805. If Assigned(new_one) Then
  806. Begin
  807. new_one.previous := prev;
  808. new_one.next := foll;
  809. prev.next := new_one;
  810. foll.previous := new_one;
  811. { should we update line information? }
  812. if (not (tai(new_one).typ in SkipLineInfo)) and
  813. (not (tai(foll).typ in SkipLineInfo)) then
  814. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  815. End
  816. End
  817. Else AsmL.Concat(new_one)
  818. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  819. End;
  820. Function TAOptObj.SkipHead(P: Tai): Tai;
  821. Var OldP: Tai;
  822. Begin
  823. Repeat
  824. OldP := P;
  825. If (P.typ in SkipInstr) Or
  826. ((P.typ = ait_marker) And
  827. (Tai_Marker(P).Kind = mark_AsmBlockEnd)) Then
  828. GetNextInstruction(P, P)
  829. Else If ((P.Typ = Ait_Marker) And
  830. (Tai_Marker(P).Kind = mark_NoPropInfoStart)) Then
  831. { a marker of the type mark_NoPropInfoStart can't be the first instruction of a }
  832. { paasmoutput list }
  833. GetNextInstruction(Tai(P.Previous),P);
  834. If (P.Typ = Ait_Marker) And
  835. (Tai_Marker(P).Kind = mark_AsmBlockStart) Then
  836. Begin
  837. P := Tai(P.Next);
  838. While (P.typ <> Ait_Marker) Or
  839. (Tai_Marker(P).Kind <> mark_AsmBlockEnd) Do
  840. P := Tai(P.Next)
  841. End;
  842. Until P = OldP;
  843. SkipHead := P;
  844. End;
  845. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  846. Begin
  847. if o1.typ=o2.typ then
  848. Case o1.typ Of
  849. Top_Reg :
  850. OpsEqual:=o1.reg=o2.reg;
  851. Top_Ref :
  852. OpsEqual := references_equal(o1.ref^, o2.ref^);
  853. Top_Const :
  854. OpsEqual:=o1.val=o2.val;
  855. Top_None :
  856. OpsEqual := True
  857. else OpsEqual := False
  858. End
  859. else
  860. OpsEqual := False;
  861. End;
  862. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): Boolean;
  863. Begin
  864. FindRegAlloc:=False;
  865. Repeat
  866. While Assigned(StartPai) And
  867. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  868. ((StartPai.typ = ait_label) and
  869. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  870. StartPai := Tai(StartPai.Next);
  871. If Assigned(StartPai) And
  872. (StartPai.typ = ait_regAlloc) Then
  873. Begin
  874. if (tai_regalloc(StartPai).ratype=ra_alloc) and (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  875. begin
  876. FindRegAlloc:=true;
  877. exit;
  878. end;
  879. StartPai := Tai(StartPai.Next);
  880. End
  881. else
  882. exit;
  883. Until false;
  884. End;
  885. function TAOptObj.RegUsedAfterInstruction(reg: Tregister; p: tai;
  886. var AllUsedRegs: TAllUsedRegs): Boolean;
  887. begin
  888. AllUsedRegs[getregtype(reg)].Update(tai(p.Next));
  889. RegUsedAfterInstruction :=
  890. (AllUsedRegs[getregtype(reg)].IsUsed(reg)); { optimization and
  891. (not(getNextInstruction(p,p)) or
  892. not(regLoadedWithNewValue(supreg,false,p))); }
  893. end;
  894. function SkipLabels(hp: tai; var hp2: tai): boolean;
  895. {skips all labels and returns the next "real" instruction}
  896. begin
  897. while assigned(hp.next) and
  898. (tai(hp.next).typ in SkipInstr + [ait_label,ait_align]) Do
  899. hp := tai(hp.next);
  900. if assigned(hp.next) then
  901. begin
  902. SkipLabels := True;
  903. hp2 := tai(hp.next)
  904. end
  905. else
  906. begin
  907. hp2 := hp;
  908. SkipLabels := False
  909. end;
  910. end;
  911. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  912. begin
  913. FindAnyLabel := false;
  914. while assigned(hp.next) and
  915. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  916. hp := tai(hp.next);
  917. if assigned(hp.next) and
  918. (tai(hp.next).typ = ait_label) then
  919. begin
  920. FindAnyLabel := true;
  921. l := tai_label(hp.next).labsym;
  922. end
  923. end;
  924. {$push}
  925. {$r-}
  926. function tAOptObj.getlabelwithsym(sym: tasmlabel): tai;
  927. begin
  928. if (int64(sym.labelnr) >= int64(labelinfo^.lowlabel)) and
  929. (int64(sym.labelnr) <= int64(labelinfo^.highlabel)) then { range check, a jump can go past an assembler block! }
  930. getlabelwithsym := labelinfo^.labeltable^[sym.labelnr-labelinfo^.lowlabel].paiobj
  931. else
  932. getlabelwithsym := nil;
  933. end;
  934. {$pop}
  935. function TAOptObj.GetFinalDestination(hp: taicpu; level: longint): boolean;
  936. {traces sucessive jumps to their final destination and sets it, e.g.
  937. je l1 je l3
  938. <code> <code>
  939. l1: becomes l1:
  940. je l2 je l3
  941. <code> <code>
  942. l2: l2:
  943. jmp l3 jmp l3
  944. the level parameter denotes how deeep we have already followed the jump,
  945. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  946. var p1, p2: tai;
  947. l: tasmlabel;
  948. begin
  949. GetfinalDestination := false;
  950. if level > 20 then
  951. exit;
  952. p1 := getlabelwithsym(tasmlabel(hp.oper[0]^.ref^.symbol));
  953. if assigned(p1) then
  954. begin
  955. SkipLabels(p1,p1);
  956. if (tai(p1).typ = ait_instruction) and
  957. (taicpu(p1).is_jmp) then
  958. if { the next instruction after the label where the jump hp arrives}
  959. { is unconditional or of the same type as hp, so continue }
  960. (((taicpu(p1).opcode = aopt_uncondjmp) and
  961. {$ifdef arm}
  962. (taicpu(p1).condition = C_None) and
  963. {$endif arm}
  964. (taicpu(p1).oper[0]^.typ = top_ref) and
  965. (assigned(taicpu(p1).oper[0]^.ref^.symbol)) and
  966. (taicpu(p1).oper[0]^.ref^.symbol is TAsmLabel)) or
  967. conditions_equal(taicpu(p1).condition,hp.condition)) or
  968. { the next instruction after the label where the jump hp arrives
  969. is the opposite of hp (so this one is never taken), but after
  970. that one there is a branch that will be taken, so perform a
  971. little hack: set p1 equal to this instruction (that's what the
  972. last SkipLabels is for, only works with short bool evaluation)}
  973. (conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) and
  974. SkipLabels(p1,p2) and
  975. (p2.typ = ait_instruction) and
  976. (taicpu(p2).is_jmp) and
  977. (((taicpu(p2).opcode = aopt_uncondjmp) and
  978. {$ifdef arm}
  979. (taicpu(p1).condition = C_None) and
  980. {$endif arm}
  981. (taicpu(p2).oper[0]^.typ = top_ref) and
  982. (assigned(taicpu(p2).oper[0]^.ref^.symbol)) and
  983. (taicpu(p2).oper[0]^.ref^.symbol is TAsmLabel)) or
  984. (conditions_equal(taicpu(p2).condition,hp.condition))) and
  985. SkipLabels(p1,p1)) then
  986. begin
  987. { quick check for loops of the form "l5: ; jmp l5 }
  988. if (tasmlabel(taicpu(p1).oper[0]^.ref^.symbol).labelnr =
  989. tasmlabel(hp.oper[0]^.ref^.symbol).labelnr) then
  990. exit;
  991. if not GetFinalDestination(taicpu(p1),succ(level)) then
  992. exit;
  993. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  994. hp.oper[0]^.ref^.symbol:=taicpu(p1).oper[0]^.ref^.symbol;
  995. tasmlabel(hp.oper[0]^.ref^.symbol).increfs;
  996. end
  997. else
  998. if conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) then
  999. if not FindAnyLabel(p1,l) then
  1000. begin
  1001. {$ifdef finaldestdebug}
  1002. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1003. strpnew('previous label inserted'))));
  1004. {$endif finaldestdebug}
  1005. current_asmdata.getjumplabel(l);
  1006. insertllitem(p1,p1.next,tai_label.Create(l));
  1007. tasmlabel(taicpu(hp).oper[0]^.ref^.symbol).decrefs;
  1008. hp.oper[0]^.ref^.symbol := l;
  1009. l.increfs;
  1010. { this won't work, since the new label isn't in the labeltable }
  1011. { so it will fail the rangecheck. Labeltable should become a }
  1012. { hashtable to support this: }
  1013. { GetFinalDestination(asml, hp); }
  1014. end
  1015. else
  1016. begin
  1017. {$ifdef finaldestdebug}
  1018. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1019. strpnew('next label reused'))));
  1020. {$endif finaldestdebug}
  1021. l.increfs;
  1022. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  1023. hp.oper[0]^.ref^.symbol := l;
  1024. if not GetFinalDestination(hp,succ(level)) then
  1025. exit;
  1026. end;
  1027. end;
  1028. GetFinalDestination := true;
  1029. end;
  1030. procedure TAOptObj.PrePeepHoleOpts;
  1031. begin
  1032. end;
  1033. procedure TAOptObj.PeepHoleOptPass1;
  1034. var
  1035. p,hp1,hp2 : tai;
  1036. begin
  1037. p := BlockStart;
  1038. ClearUsedRegs;
  1039. while (p <> BlockEnd) Do
  1040. begin
  1041. { I'am not sure why this is done, UsedRegs should reflect the register usage before the instruction
  1042. If an instruction needs the information of this, it can easily create a TempUsedRegs (FK)
  1043. UpdateUsedRegs(tai(p.next));
  1044. }
  1045. {$ifdef DEBUG_OPTALLOC}
  1046. if p.Typ=ait_instruction then
  1047. InsertLLItem(tai(p.Previous),p,tai_comment.create(strpnew(GetAllocationString(UsedRegs))));
  1048. {$endif DEBUG_OPTALLOC}
  1049. if PeepHoleOptPass1Cpu(p) then
  1050. continue;
  1051. case p.Typ Of
  1052. ait_instruction:
  1053. begin
  1054. { Handle Jmp Optimizations }
  1055. if taicpu(p).is_jmp then
  1056. begin
  1057. { the following if-block removes all code between a jmp and the next label,
  1058. because it can never be executed
  1059. }
  1060. if (taicpu(p).opcode = aopt_uncondjmp) and
  1061. {$ifdef arm}
  1062. (taicpu(p).condition = C_None) and
  1063. {$endif arm}
  1064. (taicpu(p).oper[0]^.typ = top_ref) and
  1065. (assigned(taicpu(p).oper[0]^.ref^.symbol)) and
  1066. (taicpu(p).oper[0]^.ref^.symbol is TAsmLabel) then
  1067. begin
  1068. hp2:=p;
  1069. while GetNextInstruction(hp2, hp1) and
  1070. (hp1.typ <> ait_label) do
  1071. if not(hp1.typ in ([ait_label,ait_align]+skipinstr)) then
  1072. begin
  1073. if (hp1.typ = ait_instruction) and
  1074. taicpu(hp1).is_jmp and
  1075. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1076. assigned(taicpu(hp1).oper[0]^.ref^.symbol) and
  1077. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) then
  1078. TAsmLabel(taicpu(hp1).oper[0]^.ref^.symbol).decrefs;
  1079. { don't kill start/end of assembler block,
  1080. no-line-info-start/end etc }
  1081. if hp1.typ<>ait_marker then
  1082. begin
  1083. asml.remove(hp1);
  1084. hp1.free;
  1085. end
  1086. else
  1087. hp2:=hp1;
  1088. end
  1089. else break;
  1090. end;
  1091. { remove jumps to a label coming right after them }
  1092. if GetNextInstruction(p, hp1) then
  1093. begin
  1094. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp1) and
  1095. { TODO: FIXME removing the first instruction fails}
  1096. (p<>blockstart) then
  1097. begin
  1098. hp2:=tai(hp1.next);
  1099. asml.remove(p);
  1100. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  1101. p.free;
  1102. p:=hp2;
  1103. continue;
  1104. end
  1105. else
  1106. begin
  1107. if hp1.typ = ait_label then
  1108. SkipLabels(hp1,hp1);
  1109. if (tai(hp1).typ=ait_instruction) and
  1110. (taicpu(hp1).opcode=aopt_uncondjmp) and
  1111. {$ifdef arm}
  1112. (taicpu(hp1).condition=C_None) and
  1113. {$endif arm}
  1114. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1115. (assigned(taicpu(hp1).oper[0]^.ref^.symbol)) and
  1116. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  1117. GetNextInstruction(hp1, hp2) and
  1118. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp2) then
  1119. begin
  1120. if (taicpu(p).opcode=aopt_condjmp)
  1121. {$ifdef arm}
  1122. and (taicpu(p).condition<>C_None)
  1123. {$endif arm}
  1124. then
  1125. begin
  1126. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  1127. tai_label(hp2).labsym.decrefs;
  1128. taicpu(p).oper[0]^.ref^.symbol:=taicpu(hp1).oper[0]^.ref^.symbol;
  1129. { when freeing hp1, the reference count
  1130. isn't decreased, so don't increase
  1131. taicpu(p).oper[0]^.ref^.symbol.increfs;
  1132. }
  1133. {$ifdef SPARC}
  1134. hp2:=tai(hp1.next);
  1135. asml.remove(hp2);
  1136. hp2.free;
  1137. {$endif SPARC}
  1138. asml.remove(hp1);
  1139. hp1.free;
  1140. GetFinalDestination(taicpu(p),0);
  1141. end
  1142. else
  1143. begin
  1144. GetFinalDestination(taicpu(p),0);
  1145. p:=tai(p.next);
  1146. continue;
  1147. end;
  1148. end
  1149. else
  1150. GetFinalDestination(taicpu(p),0);
  1151. end;
  1152. end;
  1153. end
  1154. else
  1155. { All other optimizes }
  1156. begin
  1157. end; { if is_jmp }
  1158. end;
  1159. end;
  1160. UpdateUsedRegs(p);
  1161. p:=tai(p.next);
  1162. end;
  1163. end;
  1164. procedure TAOptObj.PeepHoleOptPass2;
  1165. begin
  1166. end;
  1167. procedure TAOptObj.PostPeepHoleOpts;
  1168. var
  1169. p: tai;
  1170. begin
  1171. p := BlockStart;
  1172. ClearUsedRegs;
  1173. while (p <> BlockEnd) Do
  1174. begin
  1175. UpdateUsedRegs(tai(p.next));
  1176. if PostPeepHoleOptsCpu(p) then
  1177. continue;
  1178. UpdateUsedRegs(p);
  1179. p:=tai(p.next);
  1180. end;
  1181. end;
  1182. function TAOptObj.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1183. begin
  1184. result := false;
  1185. end;
  1186. function TAOptObj.PostPeepHoleOptsCpu(var p: tai): boolean;
  1187. begin
  1188. result := false;
  1189. end;
  1190. End.