aoptobj.pas 58 KB

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