aoptobj.pas 59 KB

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