aoptobj.pas 52 KB

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