aoptobj.pas 48 KB

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