aoptobj.pas 53 KB

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