aoptobj.pas 50 KB

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