aoptobj.pas 65 KB

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