aoptobj.pas 51 KB

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