aoptobj.pas 59 KB

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