aoptcpu.pas 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the ARM optimizer object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit aoptcpu;
  19. {$i fpcdefs.inc}
  20. {$define DEBUG_PREREGSCHEDULER}
  21. {$define DEBUG_AOPTCPU}
  22. Interface
  23. uses cgbase, cpubase, aasmtai, aasmcpu,aopt, aoptcpub, aoptobj, cclasses;
  24. Type
  25. TCpuAsmOptimizer = class(TAsmOptimizer)
  26. { uses the same constructor as TAopObj }
  27. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  28. procedure PeepHoleOptPass2;override;
  29. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;override;
  30. procedure RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  31. function RegUsedAfterInstruction(reg: Tregister; p: tai;
  32. var AllUsedRegs: TAllUsedRegs): Boolean;
  33. { gets the next tai object after current that contains info relevant
  34. to the optimizer in p1 which used the given register or does a
  35. change in program flow.
  36. If there is none, it returns false and
  37. sets p1 to nil }
  38. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  39. { outputs a debug message into the assembler file }
  40. procedure DebugMsg(const s: string; p: tai);
  41. protected
  42. function LookForPostindexedPattern(p: taicpu): boolean;
  43. End;
  44. TCpuPreRegallocScheduler = class(TAsmScheduler)
  45. function SchedulerPass1Cpu(var p: tai): boolean;override;
  46. procedure SwapRegLive(p, hp1: taicpu);
  47. end;
  48. TCpuThumb2AsmOptimizer = class(TCpuAsmOptimizer)
  49. private
  50. function RegAllocated(hp: tai; areg: TRegister): boolean;
  51. public
  52. { uses the same constructor as TAopObj }
  53. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  54. procedure PeepHoleOptPass2;override;
  55. End;
  56. function MustBeLast(p : tai) : boolean;
  57. Implementation
  58. uses
  59. cutils,verbose,globals,
  60. systems,
  61. cpuinfo,
  62. cgobj,cgutils,procinfo,
  63. aasmbase,aasmdata;
  64. function CanBeCond(p : tai) : boolean;
  65. begin
  66. result:=
  67. (p.typ=ait_instruction) and
  68. (taicpu(p).condition=C_None) and
  69. ((taicpu(p).opcode<A_IT) or (taicpu(p).opcode>A_ITTTT)) and
  70. (taicpu(p).opcode<>A_CBZ) and
  71. (taicpu(p).opcode<>A_CBNZ) and
  72. (taicpu(p).opcode<>A_PLD) and
  73. ((taicpu(p).opcode<>A_BLX) or
  74. (taicpu(p).oper[0]^.typ=top_reg));
  75. end;
  76. function RefsEqual(const r1, r2: treference): boolean;
  77. begin
  78. refsequal :=
  79. (r1.offset = r2.offset) and
  80. (r1.base = r2.base) and
  81. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  82. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  83. (r1.relsymbol = r2.relsymbol) and
  84. (r1.signindex = r2.signindex) and
  85. (r1.shiftimm = r2.shiftimm) and
  86. (r1.addressmode = r2.addressmode) and
  87. (r1.shiftmode = r2.shiftmode);
  88. end;
  89. function MatchInstruction(const instr: tai; const op: TCommonAsmOps; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  90. begin
  91. result :=
  92. (instr.typ = ait_instruction) and
  93. ((op = []) or ((ord(taicpu(instr).opcode)<256) and (taicpu(instr).opcode in op))) and
  94. ((cond = []) or (taicpu(instr).condition in cond)) and
  95. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  96. end;
  97. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  98. begin
  99. result :=
  100. (instr.typ = ait_instruction) and
  101. (taicpu(instr).opcode = op) and
  102. ((cond = []) or (taicpu(instr).condition in cond)) and
  103. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  104. end;
  105. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  106. begin
  107. result := oper1.typ = oper2.typ;
  108. if result then
  109. case oper1.typ of
  110. top_const:
  111. Result:=oper1.val = oper2.val;
  112. top_reg:
  113. Result:=oper1.reg = oper2.reg;
  114. top_conditioncode:
  115. Result:=oper1.cc = oper2.cc;
  116. top_ref:
  117. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  118. else Result:=false;
  119. end
  120. end;
  121. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  122. begin
  123. result := (oper.typ = top_reg) and (oper.reg = reg);
  124. end;
  125. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  126. begin
  127. if (taicpu(movp).condition = C_EQ) and
  128. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  129. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  130. begin
  131. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  132. asml.remove(movp);
  133. movp.free;
  134. end;
  135. end;
  136. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  137. var
  138. p: taicpu;
  139. begin
  140. p := taicpu(hp);
  141. regLoadedWithNewValue := false;
  142. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  143. exit;
  144. case p.opcode of
  145. { These operands do not write into a register at all }
  146. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  147. exit;
  148. {Take care of post/preincremented store and loads, they will change their base register}
  149. A_STR, A_LDR:
  150. regLoadedWithNewValue :=
  151. (taicpu(p).oper[1]^.typ=top_ref) and
  152. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  153. (taicpu(p).oper[1]^.ref^.base = reg);
  154. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  155. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  156. regLoadedWithNewValue :=
  157. (p.oper[1]^.typ = top_reg) and
  158. (p.oper[1]^.reg = reg);
  159. {Loads to oper2 from coprocessor}
  160. {
  161. MCR/MRC is currently not supported in FPC
  162. A_MRC:
  163. regLoadedWithNewValue :=
  164. (p.oper[2]^.typ = top_reg) and
  165. (p.oper[2]^.reg = reg);
  166. }
  167. {Loads to all register in the registerset}
  168. A_LDM:
  169. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  170. end;
  171. if regLoadedWithNewValue then
  172. exit;
  173. case p.oper[0]^.typ of
  174. {This is the case}
  175. top_reg:
  176. regLoadedWithNewValue := (p.oper[0]^.reg = reg) or
  177. { LDRD }
  178. (p.opcode=A_LDR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg));
  179. {LDM/STM might write a new value to their index register}
  180. top_ref:
  181. regLoadedWithNewValue :=
  182. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  183. (taicpu(p).oper[0]^.ref^.base = reg);
  184. end;
  185. end;
  186. function AlignedToQWord(const ref : treference) : boolean;
  187. begin
  188. { (safe) heuristics to ensure alignment }
  189. result:=(target_info.abi in [abi_eabi,abi_armeb,abi_eabihf]) and
  190. (((ref.offset>=0) and
  191. ((ref.offset mod 8)=0) and
  192. ((ref.base=NR_R13) or
  193. (ref.index=NR_R13))
  194. ) or
  195. ((ref.offset<=0) and
  196. { when using NR_R11, it has always a value of <qword align>+4 }
  197. ((abs(ref.offset+4) mod 8)=0) and
  198. (current_procinfo.framepointer=NR_R11) and
  199. ((ref.base=NR_R11) or
  200. (ref.index=NR_R11))
  201. )
  202. );
  203. end;
  204. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  205. var
  206. p: taicpu;
  207. i: longint;
  208. begin
  209. instructionLoadsFromReg := false;
  210. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  211. exit;
  212. p:=taicpu(hp);
  213. i:=1;
  214. {For these instructions we have to start on oper[0]}
  215. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  216. A_CMP, A_CMN, A_TST, A_TEQ,
  217. A_B, A_BL, A_BX, A_BLX,
  218. A_SMLAL, A_UMLAL]) then i:=0;
  219. while(i<p.ops) do
  220. begin
  221. case p.oper[I]^.typ of
  222. top_reg:
  223. instructionLoadsFromReg := (p.oper[I]^.reg = reg) or
  224. { STRD }
  225. ((i=0) and (p.opcode=A_STR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  226. top_regset:
  227. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  228. top_shifterop:
  229. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  230. top_ref:
  231. instructionLoadsFromReg :=
  232. (p.oper[I]^.ref^.base = reg) or
  233. (p.oper[I]^.ref^.index = reg);
  234. end;
  235. if instructionLoadsFromReg then exit; {Bailout if we found something}
  236. Inc(I);
  237. end;
  238. end;
  239. function isValidConstLoadStoreOffset(const aoffset: longint; const pf: TOpPostfix) : boolean;
  240. begin
  241. if current_settings.cputype in cpu_thumb2 then
  242. result := (aoffset<4096) and (aoffset>-256)
  243. else
  244. result := ((pf in [PF_None,PF_B]) and
  245. (abs(aoffset)<4096)) or
  246. (abs(aoffset)<256);
  247. end;
  248. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  249. var AllUsedRegs: TAllUsedRegs): Boolean;
  250. begin
  251. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  252. RegUsedAfterInstruction :=
  253. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  254. not(regLoadedWithNewValue(reg,p)) and
  255. (
  256. not(GetNextInstruction(p,p)) or
  257. instructionLoadsFromReg(reg,p) or
  258. not(regLoadedWithNewValue(reg,p))
  259. );
  260. end;
  261. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  262. var Next: tai; reg: TRegister): Boolean;
  263. begin
  264. Next:=Current;
  265. repeat
  266. Result:=GetNextInstruction(Next,Next);
  267. until not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  268. (is_calljmp(taicpu(Next).opcode)) or (RegInInstruction(NR_PC,Next));
  269. end;
  270. {$ifdef DEBUG_AOPTCPU}
  271. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  272. begin
  273. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  274. end;
  275. {$else DEBUG_AOPTCPU}
  276. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  277. begin
  278. end;
  279. {$endif DEBUG_AOPTCPU}
  280. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  281. var
  282. alloc,
  283. dealloc : tai_regalloc;
  284. hp1 : tai;
  285. begin
  286. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  287. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  288. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  289. { don't mess with moves to pc }
  290. (taicpu(movp).oper[0]^.reg<>NR_PC) and
  291. { don't mess with moves to lr }
  292. (taicpu(movp).oper[0]^.reg<>NR_R14) and
  293. { the destination register of the mov might not be used beween p and movp }
  294. not(RegUsedBetween(taicpu(movp).oper[0]^.reg,p,movp)) and
  295. { cb[n]z are thumb instructions which require specific registers, with no wide forms }
  296. (taicpu(p).opcode<>A_CBZ) and
  297. (taicpu(p).opcode<>A_CBNZ) and
  298. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  299. not (
  300. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  301. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg)
  302. ) then
  303. begin
  304. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(movp.Next));
  305. if assigned(dealloc) then
  306. begin
  307. DebugMsg('Peephole '+optimizer+' removed superfluous mov', movp);
  308. { taicpu(p).oper[0]^.reg is not used anymore, try to find its allocation
  309. and remove it if possible }
  310. GetLastInstruction(p,hp1);
  311. asml.Remove(dealloc);
  312. alloc:=FindRegAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  313. if assigned(alloc) then
  314. begin
  315. asml.Remove(alloc);
  316. alloc.free;
  317. dealloc.free;
  318. end
  319. else
  320. asml.InsertAfter(dealloc,p);
  321. { try to move the allocation of the target register }
  322. GetLastInstruction(movp,hp1);
  323. alloc:=FindRegAlloc(taicpu(movp).oper[0]^.reg,tai(hp1.Next));
  324. if assigned(alloc) then
  325. begin
  326. asml.Remove(alloc);
  327. asml.InsertBefore(alloc,p);
  328. { adjust used regs }
  329. IncludeRegInUsedRegs(taicpu(movp).oper[0]^.reg,UsedRegs);
  330. end;
  331. { finally get rid of the mov }
  332. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  333. asml.remove(movp);
  334. movp.free;
  335. end;
  336. end;
  337. end;
  338. {
  339. optimize
  340. ldr/str regX,[reg1]
  341. ...
  342. add/sub reg1,reg1,regY/const
  343. into
  344. ldr/str regX,[reg1], regY/const
  345. }
  346. function TCpuAsmOptimizer.LookForPostindexedPattern(p: taicpu) : boolean;
  347. var
  348. hp1 : tai;
  349. begin
  350. Result:=false;
  351. if (p.oper[1]^.ref^.addressmode=AM_OFFSET) and
  352. (p.oper[1]^.ref^.index=NR_NO) and
  353. (p.oper[1]^.ref^.offset=0) and
  354. GetNextInstructionUsingReg(p, hp1, p.oper[1]^.ref^.base) and
  355. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  356. MatchInstruction(hp1, [A_ADD, A_SUB], [C_None], [PF_None]) and
  357. (taicpu(hp1).oper[0]^.reg=p.oper[1]^.ref^.base) and
  358. (taicpu(hp1).oper[1]^.reg=p.oper[1]^.ref^.base) and
  359. (
  360. (taicpu(hp1).oper[2]^.typ=top_reg) or
  361. { valid offset? }
  362. ((taicpu(hp1).oper[2]^.typ=top_const) and
  363. ((abs(taicpu(hp1).oper[2]^.val)<256) or
  364. ((abs(taicpu(hp1).oper[2]^.val)<4096) and (p.oppostfix in [PF_None,PF_B]))
  365. )
  366. )
  367. ) and
  368. { don't apply the optimization if the base register is loaded }
  369. (p.oper[0]^.reg<>p.oper[1]^.ref^.base) and
  370. not(RegModifiedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) and
  371. { don't apply the optimization if the (new) index register is loaded }
  372. (p.oper[0]^.reg<>taicpu(hp1).oper[2]^.reg) and
  373. not(RegModifiedBetween(taicpu(hp1).oper[2]^.reg,p,hp1)) then
  374. begin
  375. DebugMsg('Peephole Str/LdrAdd/Sub2Str/Ldr Postindex done', p);
  376. p.oper[1]^.ref^.addressmode:=AM_POSTINDEXED;
  377. if taicpu(hp1).oper[2]^.typ=top_const then
  378. begin
  379. if taicpu(hp1).opcode=A_ADD then
  380. p.oper[1]^.ref^.offset:=taicpu(hp1).oper[2]^.val
  381. else
  382. p.oper[1]^.ref^.offset:=-taicpu(hp1).oper[2]^.val;
  383. end
  384. else
  385. begin
  386. p.oper[1]^.ref^.index:=taicpu(hp1).oper[2]^.reg;
  387. if taicpu(hp1).opcode=A_ADD then
  388. p.oper[1]^.ref^.signindex:=1
  389. else
  390. p.oper[1]^.ref^.signindex:=-1;
  391. end;
  392. asml.Remove(hp1);
  393. hp1.Free;
  394. Result:=true;
  395. end;
  396. end;
  397. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  398. var
  399. hp1,hp2: tai;
  400. i, i2: longint;
  401. TmpUsedRegs: TAllUsedRegs;
  402. tempop: tasmop;
  403. function IsPowerOf2(const value: DWord): boolean; inline;
  404. begin
  405. Result:=(value and (value - 1)) = 0;
  406. end;
  407. begin
  408. result := false;
  409. case p.typ of
  410. ait_instruction:
  411. begin
  412. {
  413. change
  414. <op> reg,x,y
  415. cmp reg,#0
  416. into
  417. <op>s reg,x,y
  418. }
  419. { this optimization can applied only to the currently enabled operations because
  420. the other operations do not update all flags and FPC does not track flag usage }
  421. if MatchInstruction(p, [A_ADC,A_ADD,A_BIC,A_SUB,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND,
  422. A_RSB,A_RSC,A_SBC,A_MLA], [C_None], [PF_None]) and
  423. GetNextInstruction(p, hp1) and
  424. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  425. (taicpu(hp1).oper[1]^.typ = top_const) and
  426. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  427. (taicpu(hp1).oper[1]^.val = 0) and
  428. GetNextInstruction(hp1, hp2) and
  429. { be careful here, following instructions could use other flags
  430. however after a jump fpc never depends on the value of flags }
  431. { All above instructions set Z and N according to the following
  432. Z := result = 0;
  433. N := result[31];
  434. EQ = Z=1; NE = Z=0;
  435. MI = N=1; PL = N=0; }
  436. MatchInstruction(hp2, A_B, [C_EQ,C_NE,C_MI,C_PL], []) and
  437. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next))) then
  438. begin
  439. DebugMsg('Peephole OpCmp2OpS done', p);
  440. taicpu(p).oppostfix:=PF_S;
  441. { move flag allocation if possible }
  442. GetLastInstruction(hp1, hp2);
  443. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  444. if assigned(hp2) then
  445. begin
  446. asml.Remove(hp2);
  447. asml.insertbefore(hp2, p);
  448. end;
  449. asml.remove(hp1);
  450. hp1.free;
  451. end
  452. else
  453. case taicpu(p).opcode of
  454. A_STR:
  455. begin
  456. { change
  457. str reg1,ref
  458. ldr reg2,ref
  459. into
  460. str reg1,ref
  461. mov reg2,reg1
  462. }
  463. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  464. (taicpu(p).oppostfix=PF_None) and
  465. GetNextInstruction(p,hp1) and
  466. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  467. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  468. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  469. begin
  470. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  471. begin
  472. DebugMsg('Peephole StrLdr2StrMov 1 done', hp1);
  473. asml.remove(hp1);
  474. hp1.free;
  475. end
  476. else
  477. begin
  478. taicpu(hp1).opcode:=A_MOV;
  479. taicpu(hp1).oppostfix:=PF_None;
  480. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  481. DebugMsg('Peephole StrLdr2StrMov 2 done', hp1);
  482. end;
  483. result := true;
  484. end
  485. { change
  486. str reg1,ref
  487. str reg2,ref
  488. into
  489. strd reg1,ref
  490. }
  491. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  492. (taicpu(p).oppostfix=PF_None) and
  493. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  494. GetNextInstruction(p,hp1) and
  495. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  496. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  497. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  498. { str ensures that either base or index contain no register, else ldr wouldn't
  499. use an offset either
  500. }
  501. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  502. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  503. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  504. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  505. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  506. begin
  507. DebugMsg('Peephole StrStr2Strd done', p);
  508. taicpu(p).oppostfix:=PF_D;
  509. asml.remove(hp1);
  510. hp1.free;
  511. end;
  512. LookForPostindexedPattern(taicpu(p));
  513. end;
  514. A_LDR:
  515. begin
  516. { change
  517. ldr reg1,ref
  518. ldr reg2,ref
  519. into ...
  520. }
  521. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  522. GetNextInstruction(p,hp1) and
  523. { ldrd is not allowed here }
  524. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  525. begin
  526. {
  527. ...
  528. ldr reg1,ref
  529. mov reg2,reg1
  530. }
  531. if RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  532. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  533. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  534. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  535. begin
  536. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  537. begin
  538. DebugMsg('Peephole LdrLdr2Ldr done', hp1);
  539. asml.remove(hp1);
  540. hp1.free;
  541. end
  542. else
  543. begin
  544. DebugMsg('Peephole LdrLdr2LdrMov done', hp1);
  545. taicpu(hp1).opcode:=A_MOV;
  546. taicpu(hp1).oppostfix:=PF_None;
  547. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  548. end;
  549. result := true;
  550. end
  551. {
  552. ...
  553. ldrd reg1,ref
  554. }
  555. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  556. { ldrd does not allow any postfixes ... }
  557. (taicpu(p).oppostfix=PF_None) and
  558. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  559. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  560. { ldr ensures that either base or index contain no register, else ldr wouldn't
  561. use an offset either
  562. }
  563. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  564. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  565. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  566. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  567. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  568. begin
  569. DebugMsg('Peephole LdrLdr2Ldrd done', p);
  570. taicpu(p).oppostfix:=PF_D;
  571. asml.remove(hp1);
  572. hp1.free;
  573. end;
  574. end;
  575. LookForPostindexedPattern(taicpu(p));
  576. { Remove superfluous mov after ldr
  577. changes
  578. ldr reg1, ref
  579. mov reg2, reg1
  580. to
  581. ldr reg2, ref
  582. conditions are:
  583. * no ldrd usage
  584. * reg1 must be released after mov
  585. * mov can not contain shifterops
  586. * ldr+mov have the same conditions
  587. * mov does not set flags
  588. }
  589. if (taicpu(p).oppostfix<>PF_D) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  590. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  591. end;
  592. A_MOV:
  593. begin
  594. { fold
  595. mov reg1,reg0, shift imm1
  596. mov reg1,reg1, shift imm2
  597. }
  598. if (taicpu(p).ops=3) and
  599. (taicpu(p).oper[2]^.typ = top_shifterop) and
  600. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  601. getnextinstruction(p,hp1) and
  602. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  603. (taicpu(hp1).ops=3) and
  604. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  605. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  606. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  607. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  608. begin
  609. { fold
  610. mov reg1,reg0, lsl 16
  611. mov reg1,reg1, lsr 16
  612. strh reg1, ...
  613. dealloc reg1
  614. to
  615. strh reg1, ...
  616. dealloc reg1
  617. }
  618. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  619. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  620. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  621. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  622. getnextinstruction(hp1,hp2) and
  623. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  624. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  625. begin
  626. CopyUsedRegs(TmpUsedRegs);
  627. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  628. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  629. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  630. begin
  631. DebugMsg('Peephole optimizer removed superfluous 16 Bit zero extension', hp1);
  632. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  633. asml.remove(p);
  634. asml.remove(hp1);
  635. p.free;
  636. hp1.free;
  637. p:=hp2;
  638. end;
  639. ReleaseUsedRegs(TmpUsedRegs);
  640. end
  641. { fold
  642. mov reg1,reg0, shift imm1
  643. mov reg1,reg1, shift imm2
  644. to
  645. mov reg1,reg0, shift imm1+imm2
  646. }
  647. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  648. { asr makes no use after a lsr, the asr can be foled into the lsr }
  649. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  650. begin
  651. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  652. { avoid overflows }
  653. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  654. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  655. SM_ROR:
  656. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  657. SM_ASR:
  658. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  659. SM_LSR,
  660. SM_LSL:
  661. begin
  662. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  663. InsertLLItem(p.previous, p.next, hp1);
  664. p.free;
  665. p:=hp1;
  666. end;
  667. else
  668. internalerror(2008072803);
  669. end;
  670. DebugMsg('Peephole ShiftShift2Shift 1 done', p);
  671. asml.remove(hp1);
  672. hp1.free;
  673. result := true;
  674. end
  675. { fold
  676. mov reg1,reg0, shift imm1
  677. mov reg1,reg1, shift imm2
  678. mov reg1,reg1, shift imm3 ...
  679. }
  680. else if getnextinstruction(hp1,hp2) and
  681. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  682. (taicpu(hp2).ops=3) and
  683. MatchOperand(taicpu(hp2).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  684. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  685. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  686. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  687. begin
  688. { mov reg1,reg0, lsl imm1
  689. mov reg1,reg1, lsr/asr imm2
  690. mov reg1,reg1, lsl imm3 ...
  691. if imm3<=imm1 and imm2>=imm3
  692. to
  693. mov reg1,reg0, lsl imm1
  694. mov reg1,reg1, lsr/asr imm2-imm3
  695. }
  696. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  697. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  698. (taicpu(hp2).oper[2]^.shifterop^.shiftimm<=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  699. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(hp2).oper[2]^.shifterop^.shiftimm) then
  700. begin
  701. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  702. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1 done', p);
  703. asml.remove(hp2);
  704. hp2.free;
  705. result := true;
  706. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  707. begin
  708. asml.remove(hp1);
  709. hp1.free;
  710. end;
  711. end
  712. { mov reg1,reg0, lsr/asr imm1
  713. mov reg1,reg1, lsl imm2
  714. mov reg1,reg1, lsr/asr imm3 ...
  715. if imm3>=imm1 and imm2>=imm1
  716. to
  717. mov reg1,reg0, lsl imm2-imm1
  718. mov reg1,reg1, lsr/asr imm3 ...
  719. }
  720. else if (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  721. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  722. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  723. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  724. begin
  725. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  726. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  727. DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
  728. asml.remove(p);
  729. p.free;
  730. p:=hp2;
  731. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  732. begin
  733. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  734. asml.remove(hp1);
  735. hp1.free;
  736. p:=hp2;
  737. end;
  738. result := true;
  739. end;
  740. end;
  741. end;
  742. { Change the common
  743. mov r0, r0, lsr #24
  744. and r0, r0, #255
  745. and remove the superfluous and
  746. This could be extended to handle more cases.
  747. }
  748. if (taicpu(p).ops=3) and
  749. (taicpu(p).oper[2]^.typ = top_shifterop) and
  750. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  751. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  752. (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  753. getnextinstruction(p,hp1) and
  754. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  755. (taicpu(hp1).ops=3) and
  756. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  757. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  758. (taicpu(hp1).oper[2]^.typ = top_const) and
  759. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  760. For LSR #25 and an AndConst of 255 that whould go like this:
  761. 255 and ((2 shl (32-25))-1)
  762. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  763. LSR #25 and AndConst of 254:
  764. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  765. }
  766. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  767. begin
  768. DebugMsg('Peephole LsrAnd2Lsr done', hp1);
  769. asml.remove(hp1);
  770. hp1.free;
  771. end;
  772. {
  773. optimize
  774. mov rX, yyyy
  775. ....
  776. }
  777. if (taicpu(p).ops = 2) and
  778. GetNextInstruction(p,hp1) and
  779. (tai(hp1).typ = ait_instruction) then
  780. begin
  781. {
  782. This changes the very common
  783. mov r0, #0
  784. str r0, [...]
  785. mov r0, #0
  786. str r0, [...]
  787. and removes all superfluous mov instructions
  788. }
  789. if (taicpu(p).oper[1]^.typ = top_const) and
  790. (taicpu(hp1).opcode=A_STR) then
  791. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  792. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  793. GetNextInstruction(hp1, hp2) and
  794. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  795. (taicpu(hp2).ops = 2) and
  796. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  797. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  798. begin
  799. DebugMsg('Peephole MovStrMov done', hp2);
  800. GetNextInstruction(hp2,hp1);
  801. asml.remove(hp2);
  802. hp2.free;
  803. if not assigned(hp1) then break;
  804. end
  805. {
  806. This removes the first mov from
  807. mov rX,...
  808. mov rX,...
  809. }
  810. else if taicpu(hp1).opcode=A_MOV then
  811. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  812. (taicpu(hp1).ops = 2) and
  813. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  814. { don't remove the first mov if the second is a mov rX,rX }
  815. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  816. begin
  817. DebugMsg('Peephole MovMov done', p);
  818. asml.remove(p);
  819. p.free;
  820. p:=hp1;
  821. GetNextInstruction(hp1,hp1);
  822. if not assigned(hp1) then
  823. break;
  824. end;
  825. end;
  826. {
  827. change
  828. mov r1, r0
  829. add r1, r1, #1
  830. to
  831. add r1, r0, #1
  832. Todo: Make it work for mov+cmp too
  833. CAUTION! If this one is successful p might not be a mov instruction anymore!
  834. }
  835. if (taicpu(p).ops = 2) and
  836. (taicpu(p).oper[1]^.typ = top_reg) and
  837. (taicpu(p).oppostfix = PF_NONE) and
  838. GetNextInstruction(p, hp1) and
  839. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  840. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN],
  841. [taicpu(p).condition], []) and
  842. {MOV and MVN might only have 2 ops}
  843. (taicpu(hp1).ops = 3) and
  844. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  845. (taicpu(hp1).oper[1]^.typ = top_reg) and
  846. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop]) then
  847. begin
  848. { When we get here we still don't know if the registers match}
  849. for I:=1 to 2 do
  850. {
  851. If the first loop was successful p will be replaced with hp1.
  852. The checks will still be ok, because all required information
  853. will also be in hp1 then.
  854. }
  855. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  856. begin
  857. DebugMsg('Peephole RedundantMovProcess done', hp1);
  858. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  859. if p<>hp1 then
  860. begin
  861. asml.remove(p);
  862. p.free;
  863. p:=hp1;
  864. end;
  865. end;
  866. end;
  867. { This folds shifterops into following instructions
  868. mov r0, r1, lsl #8
  869. add r2, r3, r0
  870. to
  871. add r2, r3, r1, lsl #8
  872. CAUTION! If this one is successful p might not be a mov instruction anymore!
  873. }
  874. if (taicpu(p).opcode = A_MOV) and
  875. (taicpu(p).ops = 3) and
  876. (taicpu(p).oper[1]^.typ = top_reg) and
  877. (taicpu(p).oper[2]^.typ = top_shifterop) and
  878. (taicpu(p).oppostfix = PF_NONE) and
  879. GetNextInstruction(p, hp1) and
  880. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  881. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  882. A_CMP, A_CMN],
  883. [taicpu(p).condition], [PF_None]) and
  884. (taicpu(hp1).ops >= 2) and {Currently we can't fold into another shifterop}
  885. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  886. (
  887. {Only ONE of the two src operands is allowed to match}
  888. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  889. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  890. ) then
  891. begin
  892. CopyUsedRegs(TmpUsedRegs);
  893. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  894. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  895. I2:=0
  896. else
  897. I2:=1;
  898. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp1,TmpUsedRegs)) then
  899. for I:=I2 to taicpu(hp1).ops-1 do
  900. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  901. begin
  902. { If the parameter matched on the second op from the RIGHT
  903. we have to switch the parameters, this will not happen for CMP
  904. were we're only evaluating the most right parameter
  905. }
  906. if I <> taicpu(hp1).ops-1 then
  907. begin
  908. {The SUB operators need to be changed when we swap parameters}
  909. case taicpu(hp1).opcode of
  910. A_SUB: tempop:=A_RSB;
  911. A_SBC: tempop:=A_RSC;
  912. A_RSB: tempop:=A_SUB;
  913. A_RSC: tempop:=A_SBC;
  914. else tempop:=taicpu(hp1).opcode;
  915. end;
  916. if taicpu(hp1).ops = 3 then
  917. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  918. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  919. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  920. else
  921. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  922. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  923. taicpu(p).oper[2]^.shifterop^);
  924. end
  925. else
  926. if taicpu(hp1).ops = 3 then
  927. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  928. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  929. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  930. else
  931. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  932. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  933. taicpu(p).oper[2]^.shifterop^);
  934. asml.insertbefore(hp2, p);
  935. asml.remove(p);
  936. asml.remove(hp1);
  937. p.free;
  938. hp1.free;
  939. p:=hp2;
  940. GetNextInstruction(p,hp1);
  941. DebugMsg('Peephole FoldShiftProcess done', p);
  942. break;
  943. end;
  944. ReleaseUsedRegs(TmpUsedRegs);
  945. end;
  946. {
  947. Often we see shifts and then a superfluous mov to another register
  948. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  949. }
  950. if (taicpu(p).opcode = A_MOV) and
  951. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  952. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  953. end;
  954. A_ADD,
  955. A_ADC,
  956. A_RSB,
  957. A_RSC,
  958. A_SUB,
  959. A_SBC,
  960. A_AND,
  961. A_BIC,
  962. A_EOR,
  963. A_ORR,
  964. A_MLA,
  965. A_MUL:
  966. begin
  967. {
  968. optimize
  969. and reg2,reg1,const1
  970. ...
  971. }
  972. if (taicpu(p).opcode = A_AND) and
  973. (taicpu(p).oper[1]^.typ = top_reg) and
  974. (taicpu(p).oper[2]^.typ = top_const) then
  975. begin
  976. {
  977. change
  978. and reg2,reg1,const1
  979. and reg3,reg2,const2
  980. to
  981. and reg3,reg1,(const1 and const2)
  982. }
  983. if GetNextInstruction(p, hp1) and
  984. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  985. { either reg3 and reg2 are equal or reg2 is deallocated after the and }
  986. (MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) or
  987. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next)))) and
  988. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  989. (taicpu(hp1).oper[2]^.typ = top_const) then
  990. begin
  991. DebugMsg('Peephole AndAnd2And done', p);
  992. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  993. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  994. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  995. asml.remove(hp1);
  996. hp1.free;
  997. end
  998. {
  999. change
  1000. and reg2,reg1,255
  1001. strb reg2,[...]
  1002. dealloc reg2
  1003. to
  1004. strb reg1,[...]
  1005. }
  1006. else if (taicpu(p).oper[2]^.val = 255) and
  1007. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1008. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1009. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1010. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1011. { the reference in strb might not use reg2 }
  1012. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1013. { reg1 might not be modified inbetween }
  1014. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1015. begin
  1016. DebugMsg('Peephole AndStrb2Strb done', p);
  1017. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1018. asml.remove(p);
  1019. p.free;
  1020. p:=hp1;
  1021. end;
  1022. end;
  1023. {
  1024. change
  1025. add/sub reg2,reg1,const1
  1026. str/ldr reg3,[reg2,const2]
  1027. dealloc reg2
  1028. to
  1029. str/ldr reg3,[reg1,const2+/-const1]
  1030. }
  1031. if (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1032. (taicpu(p).oper[1]^.typ = top_reg) and
  1033. (taicpu(p).oper[2]^.typ = top_const) then
  1034. begin
  1035. hp1:=p;
  1036. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1037. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1038. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1039. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1040. { don't optimize if the register is stored/overwritten }
  1041. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1042. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1043. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1044. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1045. ldr postfix }
  1046. (((taicpu(p).opcode=A_ADD) and
  1047. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1048. ) or
  1049. ((taicpu(p).opcode=A_SUB) and
  1050. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1051. )
  1052. ) do
  1053. begin
  1054. { neither reg1 nor reg2 might be changed inbetween }
  1055. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1056. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1057. break;
  1058. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1059. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1060. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1061. begin
  1062. { remember last instruction }
  1063. hp2:=hp1;
  1064. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1065. hp1:=p;
  1066. { fix all ldr/str }
  1067. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1068. begin
  1069. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1070. if taicpu(p).opcode=A_ADD then
  1071. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1072. else
  1073. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1074. if hp1=hp2 then
  1075. break;
  1076. end;
  1077. GetNextInstruction(p,hp1);
  1078. asml.remove(p);
  1079. p.free;
  1080. p:=hp1;
  1081. break;
  1082. end;
  1083. end;
  1084. end;
  1085. {
  1086. change
  1087. add reg1, ...
  1088. mov reg2, reg1
  1089. to
  1090. add reg2, ...
  1091. }
  1092. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1093. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  1094. end;
  1095. A_MVN:
  1096. begin
  1097. {
  1098. change
  1099. mvn reg2,reg1
  1100. and reg3,reg4,reg2
  1101. dealloc reg2
  1102. to
  1103. bic reg3,reg4,reg1
  1104. }
  1105. if (taicpu(p).oper[1]^.typ = top_reg) and
  1106. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1107. MatchInstruction(hp1,A_AND,[],[]) and
  1108. (((taicpu(hp1).ops=3) and
  1109. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1110. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1111. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1112. ((taicpu(hp1).ops=2) and
  1113. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1114. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1115. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1116. { reg1 might not be modified inbetween }
  1117. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1118. begin
  1119. DebugMsg('Peephole MvnAnd2Bic done', p);
  1120. taicpu(hp1).opcode:=A_BIC;
  1121. if taicpu(hp1).ops=3 then
  1122. begin
  1123. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1124. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1125. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1126. end
  1127. else
  1128. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1129. asml.remove(p);
  1130. p.free;
  1131. p:=hp1;
  1132. end;
  1133. end;
  1134. A_UXTB:
  1135. begin
  1136. {
  1137. change
  1138. uxtb reg2,reg1
  1139. strb reg2,[...]
  1140. dealloc reg2
  1141. to
  1142. strb reg1,[...]
  1143. }
  1144. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1145. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1146. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1147. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1148. { the reference in strb might not use reg2 }
  1149. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1150. { reg1 might not be modified inbetween }
  1151. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1152. begin
  1153. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1154. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1155. asml.remove(p);
  1156. p.free;
  1157. p:=hp1;
  1158. end
  1159. {
  1160. change
  1161. uxtb reg2,reg1
  1162. uxth reg3,reg2
  1163. dealloc reg2
  1164. to
  1165. uxtb reg3,reg1
  1166. }
  1167. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1168. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1169. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1170. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1171. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1172. { reg1 might not be modified inbetween }
  1173. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1174. begin
  1175. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1176. taicpu(hp1).opcode:=A_UXTB;
  1177. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1178. asml.remove(p);
  1179. p.free;
  1180. p:=hp1;
  1181. end;
  1182. end;
  1183. A_UXTH:
  1184. begin
  1185. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1186. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1187. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1188. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1189. { the reference in strb might not use reg2 }
  1190. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1191. { reg1 might not be modified inbetween }
  1192. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1193. begin
  1194. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1195. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1196. asml.remove(p);
  1197. p.free;
  1198. p:=hp1;
  1199. end;
  1200. end;
  1201. A_CMP:
  1202. begin
  1203. {
  1204. change
  1205. cmp reg,const1
  1206. moveq reg,const1
  1207. movne reg,const2
  1208. to
  1209. cmp reg,const1
  1210. movne reg,const2
  1211. }
  1212. if (taicpu(p).oper[1]^.typ = top_const) and
  1213. GetNextInstruction(p, hp1) and
  1214. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1215. (taicpu(hp1).oper[1]^.typ = top_const) and
  1216. GetNextInstruction(hp1, hp2) and
  1217. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1218. (taicpu(hp1).oper[1]^.typ = top_const) then
  1219. begin
  1220. RemoveRedundantMove(p, hp1, asml);
  1221. RemoveRedundantMove(p, hp2, asml);
  1222. end;
  1223. end;
  1224. end;
  1225. end;
  1226. end;
  1227. end;
  1228. { instructions modifying the CPSR can be only the last instruction }
  1229. function MustBeLast(p : tai) : boolean;
  1230. begin
  1231. Result:=(p.typ=ait_instruction) and
  1232. ((taicpu(p).opcode in [A_BL,A_BLX,A_CMP,A_CMN,A_SWI,A_TEQ,A_TST,A_CMF,A_CMFE {,A_MSR}]) or
  1233. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  1234. (taicpu(p).oppostfix=PF_S));
  1235. end;
  1236. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1237. var
  1238. p,hp1,hp2: tai;
  1239. l : longint;
  1240. condition : tasmcond;
  1241. hp3: tai;
  1242. WasLast: boolean;
  1243. { UsedRegs, TmpUsedRegs: TRegSet; }
  1244. begin
  1245. p := BlockStart;
  1246. { UsedRegs := []; }
  1247. while (p <> BlockEnd) Do
  1248. begin
  1249. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  1250. case p.Typ Of
  1251. Ait_Instruction:
  1252. begin
  1253. case taicpu(p).opcode Of
  1254. A_B:
  1255. if taicpu(p).condition<>C_None then
  1256. begin
  1257. { check for
  1258. Bxx xxx
  1259. <several instructions>
  1260. xxx:
  1261. }
  1262. l:=0;
  1263. WasLast:=False;
  1264. GetNextInstruction(p, hp1);
  1265. while assigned(hp1) and
  1266. (l<=4) and
  1267. CanBeCond(hp1) and
  1268. { stop on labels }
  1269. not(hp1.typ=ait_label) do
  1270. begin
  1271. inc(l);
  1272. if MustBeLast(hp1) then
  1273. begin
  1274. WasLast:=True;
  1275. GetNextInstruction(hp1,hp1);
  1276. break;
  1277. end
  1278. else
  1279. GetNextInstruction(hp1,hp1);
  1280. end;
  1281. if assigned(hp1) then
  1282. begin
  1283. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1284. begin
  1285. if (l<=4) and (l>0) then
  1286. begin
  1287. condition:=inverse_cond(taicpu(p).condition);
  1288. hp2:=p;
  1289. GetNextInstruction(p,hp1);
  1290. p:=hp1;
  1291. repeat
  1292. if hp1.typ=ait_instruction then
  1293. taicpu(hp1).condition:=condition;
  1294. if MustBeLast(hp1) then
  1295. begin
  1296. GetNextInstruction(hp1,hp1);
  1297. break;
  1298. end
  1299. else
  1300. GetNextInstruction(hp1,hp1);
  1301. until not(assigned(hp1)) or
  1302. not(CanBeCond(hp1)) or
  1303. (hp1.typ=ait_label);
  1304. { wait with removing else GetNextInstruction could
  1305. ignore the label if it was the only usage in the
  1306. jump moved away }
  1307. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1308. asml.remove(hp2);
  1309. hp2.free;
  1310. continue;
  1311. end;
  1312. end
  1313. else
  1314. { do not perform further optimizations if there is inctructon
  1315. in block #1 which can not be optimized.
  1316. }
  1317. if not WasLast then
  1318. begin
  1319. { check further for
  1320. Bcc xxx
  1321. <several instructions 1>
  1322. B yyy
  1323. xxx:
  1324. <several instructions 2>
  1325. yyy:
  1326. }
  1327. { hp2 points to jmp yyy }
  1328. hp2:=hp1;
  1329. { skip hp1 to xxx }
  1330. GetNextInstruction(hp1, hp1);
  1331. if assigned(hp2) and
  1332. assigned(hp1) and
  1333. (l<=3) and
  1334. (hp2.typ=ait_instruction) and
  1335. (taicpu(hp2).is_jmp) and
  1336. (taicpu(hp2).condition=C_None) and
  1337. { real label and jump, no further references to the
  1338. label are allowed }
  1339. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1340. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1341. begin
  1342. l:=0;
  1343. { skip hp1 to <several moves 2> }
  1344. GetNextInstruction(hp1, hp1);
  1345. while assigned(hp1) and
  1346. CanBeCond(hp1) do
  1347. begin
  1348. inc(l);
  1349. GetNextInstruction(hp1, hp1);
  1350. end;
  1351. { hp1 points to yyy: }
  1352. if assigned(hp1) and
  1353. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1354. begin
  1355. condition:=inverse_cond(taicpu(p).condition);
  1356. GetNextInstruction(p,hp1);
  1357. hp3:=p;
  1358. p:=hp1;
  1359. repeat
  1360. if hp1.typ=ait_instruction then
  1361. taicpu(hp1).condition:=condition;
  1362. GetNextInstruction(hp1,hp1);
  1363. until not(assigned(hp1)) or
  1364. not(CanBeCond(hp1));
  1365. { hp2 is still at jmp yyy }
  1366. GetNextInstruction(hp2,hp1);
  1367. { hp2 is now at xxx: }
  1368. condition:=inverse_cond(condition);
  1369. GetNextInstruction(hp1,hp1);
  1370. { hp1 is now at <several movs 2> }
  1371. repeat
  1372. taicpu(hp1).condition:=condition;
  1373. GetNextInstruction(hp1,hp1);
  1374. until not(assigned(hp1)) or
  1375. not(CanBeCond(hp1)) or
  1376. (hp1.typ=ait_label);
  1377. {
  1378. asml.remove(hp1.next)
  1379. hp1.next.free;
  1380. asml.remove(hp1);
  1381. hp1.free;
  1382. }
  1383. { remove Bcc }
  1384. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1385. asml.remove(hp3);
  1386. hp3.free;
  1387. { remove jmp }
  1388. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1389. asml.remove(hp2);
  1390. hp2.free;
  1391. continue;
  1392. end;
  1393. end;
  1394. end;
  1395. end;
  1396. end;
  1397. end;
  1398. end;
  1399. end;
  1400. p := tai(p.next)
  1401. end;
  1402. end;
  1403. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  1404. begin
  1405. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  1406. Result:=true
  1407. else
  1408. Result:=inherited RegInInstruction(Reg, p1);
  1409. end;
  1410. const
  1411. { set of opcode which might or do write to memory }
  1412. { TODO : extend armins.dat to contain r/w info }
  1413. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  1414. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  1415. { adjust the register live information when swapping the two instructions p and hp1,
  1416. they must follow one after the other }
  1417. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  1418. procedure CheckLiveEnd(reg : tregister);
  1419. var
  1420. supreg : TSuperRegister;
  1421. regtype : TRegisterType;
  1422. begin
  1423. if reg=NR_NO then
  1424. exit;
  1425. regtype:=getregtype(reg);
  1426. supreg:=getsupreg(reg);
  1427. if (cg.rg[regtype].live_end[supreg]=hp1) and
  1428. RegInInstruction(reg,p) then
  1429. cg.rg[regtype].live_end[supreg]:=p;
  1430. end;
  1431. procedure CheckLiveStart(reg : TRegister);
  1432. var
  1433. supreg : TSuperRegister;
  1434. regtype : TRegisterType;
  1435. begin
  1436. if reg=NR_NO then
  1437. exit;
  1438. regtype:=getregtype(reg);
  1439. supreg:=getsupreg(reg);
  1440. if (cg.rg[regtype].live_start[supreg]=p) and
  1441. RegInInstruction(reg,hp1) then
  1442. cg.rg[regtype].live_start[supreg]:=hp1;
  1443. end;
  1444. var
  1445. i : longint;
  1446. r : TSuperRegister;
  1447. begin
  1448. { assumption: p is directly followed by hp1 }
  1449. { if live of any reg used by p starts at p and hp1 uses this register then
  1450. set live start to hp1 }
  1451. for i:=0 to p.ops-1 do
  1452. case p.oper[i]^.typ of
  1453. Top_Reg:
  1454. CheckLiveStart(p.oper[i]^.reg);
  1455. Top_Ref:
  1456. begin
  1457. CheckLiveStart(p.oper[i]^.ref^.base);
  1458. CheckLiveStart(p.oper[i]^.ref^.index);
  1459. end;
  1460. Top_Shifterop:
  1461. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  1462. Top_RegSet:
  1463. for r:=RS_R0 to RS_R15 do
  1464. if r in p.oper[i]^.regset^ then
  1465. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1466. end;
  1467. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  1468. set live end to p }
  1469. for i:=0 to hp1.ops-1 do
  1470. case hp1.oper[i]^.typ of
  1471. Top_Reg:
  1472. CheckLiveEnd(hp1.oper[i]^.reg);
  1473. Top_Ref:
  1474. begin
  1475. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  1476. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  1477. end;
  1478. Top_Shifterop:
  1479. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  1480. Top_RegSet:
  1481. for r:=RS_R0 to RS_R15 do
  1482. if r in hp1.oper[i]^.regset^ then
  1483. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  1484. end;
  1485. end;
  1486. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  1487. { TODO : schedule also forward }
  1488. { TODO : schedule distance > 1 }
  1489. var
  1490. hp1,hp2,hp3,hp4,hp5 : tai;
  1491. list : TAsmList;
  1492. begin
  1493. result:=true;
  1494. list:=TAsmList.Create;
  1495. p:=BlockStart;
  1496. while p<>BlockEnd Do
  1497. begin
  1498. if (p.typ=ait_instruction) and
  1499. GetNextInstruction(p,hp1) and
  1500. (hp1.typ=ait_instruction) and
  1501. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  1502. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  1503. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  1504. not(RegModifiedByInstruction(NR_PC,p))
  1505. ) or
  1506. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  1507. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  1508. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  1509. (taicpu(hp1).oper[1]^.ref^.offset=0)
  1510. )
  1511. ) or
  1512. { try to prove that the memory accesses don't overlapp }
  1513. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  1514. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  1515. (taicpu(p).oppostfix=PF_None) and
  1516. (taicpu(hp1).oppostfix=PF_None) and
  1517. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  1518. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1519. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  1520. (abs(taicpu(p).oper[1]^.ref^.offset-taicpu(hp1).oper[1]^.ref^.offset)>=max(tcgsize2size[reg_cgsize(taicpu(p).oper[0]^.reg)],tcgsize2size[reg_cgsize(taicpu(hp1).oper[0]^.reg)]))
  1521. )
  1522. )
  1523. ) and
  1524. GetNextInstruction(hp1,hp2) and
  1525. (hp2.typ=ait_instruction) and
  1526. { loaded register used by next instruction? }
  1527. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  1528. { loaded register not used by previous instruction? }
  1529. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  1530. { same condition? }
  1531. (taicpu(p).condition=taicpu(hp1).condition) and
  1532. { first instruction might not change the register used as base }
  1533. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  1534. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  1535. ) and
  1536. { first instruction might not change the register used as index }
  1537. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  1538. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  1539. ) then
  1540. begin
  1541. hp3:=tai(p.Previous);
  1542. hp5:=tai(p.next);
  1543. asml.Remove(p);
  1544. { if there is a reg. dealloc instruction associated with p, move it together with p }
  1545. { before the instruction? }
  1546. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  1547. begin
  1548. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  1549. RegInInstruction(tai_regalloc(hp3).reg,p) then
  1550. begin
  1551. hp4:=hp3;
  1552. hp3:=tai(hp3.Previous);
  1553. asml.Remove(hp4);
  1554. list.Concat(hp4);
  1555. end
  1556. else
  1557. hp3:=tai(hp3.Previous);
  1558. end;
  1559. list.Concat(p);
  1560. SwapRegLive(taicpu(p),taicpu(hp1));
  1561. { after the instruction? }
  1562. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  1563. begin
  1564. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  1565. RegInInstruction(tai_regalloc(hp5).reg,p) then
  1566. begin
  1567. hp4:=hp5;
  1568. hp5:=tai(hp5.next);
  1569. asml.Remove(hp4);
  1570. list.Concat(hp4);
  1571. end
  1572. else
  1573. hp5:=tai(hp5.Next);
  1574. end;
  1575. asml.Remove(hp1);
  1576. {$ifdef DEBUG_PREREGSCHEDULER}
  1577. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),hp2);
  1578. {$endif DEBUG_PREREGSCHEDULER}
  1579. asml.InsertBefore(hp1,hp2);
  1580. asml.InsertListBefore(hp2,list);
  1581. p:=tai(p.next)
  1582. end
  1583. else if p.typ=ait_instruction then
  1584. p:=hp1
  1585. else
  1586. p:=tai(p.next);
  1587. end;
  1588. list.Free;
  1589. end;
  1590. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  1591. var
  1592. hp : tai;
  1593. l : longint;
  1594. begin
  1595. hp := tai(p.Previous);
  1596. l := 1;
  1597. while assigned(hp) and
  1598. (l <= 4) do
  1599. begin
  1600. if hp.typ=ait_instruction then
  1601. begin
  1602. if (taicpu(hp).opcode>=A_IT) and
  1603. (taicpu(hp).opcode <= A_ITTTT) then
  1604. begin
  1605. if (taicpu(hp).opcode = A_IT) and
  1606. (l=1) then
  1607. list.Remove(hp)
  1608. else
  1609. case taicpu(hp).opcode of
  1610. A_ITE:
  1611. if l=2 then taicpu(hp).opcode := A_IT;
  1612. A_ITT:
  1613. if l=2 then taicpu(hp).opcode := A_IT;
  1614. A_ITEE:
  1615. if l=3 then taicpu(hp).opcode := A_ITE;
  1616. A_ITTE:
  1617. if l=3 then taicpu(hp).opcode := A_ITT;
  1618. A_ITET:
  1619. if l=3 then taicpu(hp).opcode := A_ITE;
  1620. A_ITTT:
  1621. if l=3 then taicpu(hp).opcode := A_ITT;
  1622. A_ITEEE:
  1623. if l=4 then taicpu(hp).opcode := A_ITEE;
  1624. A_ITTEE:
  1625. if l=4 then taicpu(hp).opcode := A_ITTE;
  1626. A_ITETE:
  1627. if l=4 then taicpu(hp).opcode := A_ITET;
  1628. A_ITTTE:
  1629. if l=4 then taicpu(hp).opcode := A_ITTT;
  1630. A_ITEET:
  1631. if l=4 then taicpu(hp).opcode := A_ITEE;
  1632. A_ITTET:
  1633. if l=4 then taicpu(hp).opcode := A_ITTE;
  1634. A_ITETT:
  1635. if l=4 then taicpu(hp).opcode := A_ITET;
  1636. A_ITTTT:
  1637. if l=4 then taicpu(hp).opcode := A_ITTT;
  1638. end;
  1639. break;
  1640. end;
  1641. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  1642. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  1643. break;}
  1644. inc(l);
  1645. end;
  1646. hp := tai(hp.Previous);
  1647. end;
  1648. end;
  1649. function TCpuThumb2AsmOptimizer.RegAllocated(hp: tai; areg: TRegister): boolean;
  1650. var
  1651. p: tai;
  1652. begin
  1653. result := true;
  1654. p := hp;
  1655. while assigned(p) do
  1656. begin
  1657. if (p.typ=ait_regalloc) and
  1658. (tai_regalloc(p).reg = areg) then
  1659. begin
  1660. if tai_regalloc(p).ratype = ra_alloc then
  1661. exit(false)
  1662. else if tai_regalloc(p).ratype = ra_dealloc then
  1663. exit;
  1664. end;
  1665. p := tai(p.Next);
  1666. end;
  1667. result := false;
  1668. end;
  1669. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1670. var
  1671. hp : taicpu;
  1672. hp1,hp2 : tai;
  1673. begin
  1674. if (p.typ=ait_instruction) and
  1675. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  1676. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1677. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1678. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  1679. begin
  1680. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  1681. AsmL.InsertAfter(hp, p);
  1682. asml.Remove(p);
  1683. p:=hp;
  1684. result:=true;
  1685. end
  1686. else if (p.typ=ait_instruction) and
  1687. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  1688. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  1689. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  1690. (taicpu(p).oper[1]^.ref^.offset=-4) and
  1691. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  1692. begin
  1693. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  1694. asml.InsertAfter(hp, p);
  1695. asml.Remove(p);
  1696. p.Free;
  1697. p:=hp;
  1698. result:=true;
  1699. end
  1700. else if (p.typ=ait_instruction) and
  1701. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  1702. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1703. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1704. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  1705. begin
  1706. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  1707. asml.InsertBefore(hp, p);
  1708. asml.Remove(p);
  1709. p.Free;
  1710. p:=hp;
  1711. result:=true;
  1712. end
  1713. else if (p.typ=ait_instruction) and
  1714. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  1715. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  1716. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  1717. (taicpu(p).oper[1]^.ref^.offset=4) and
  1718. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  1719. begin
  1720. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  1721. asml.InsertBefore(hp, p);
  1722. asml.Remove(p);
  1723. p.Free;
  1724. p:=hp;
  1725. result:=true;
  1726. end
  1727. else if (p.typ=ait_instruction) and
  1728. MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  1729. (taicpu(p).oper[1]^.typ=top_const) and
  1730. (taicpu(p).oper[1]^.val >= 0) and
  1731. (taicpu(p).oper[1]^.val < 256) and
  1732. (not RegAllocated(p, NR_DEFAULTFLAGS)) then
  1733. begin
  1734. taicpu(p).oppostfix:=PF_S;
  1735. result:=true;
  1736. end
  1737. else if (p.typ=ait_instruction) and
  1738. MatchInstruction(p, A_MVN, [], [PF_None]) and
  1739. (taicpu(p).oper[1]^.typ=top_reg) and
  1740. (not RegAllocated(p, NR_DEFAULTFLAGS)) then
  1741. begin
  1742. taicpu(p).oppostfix:=PF_S;
  1743. result:=true;
  1744. end
  1745. else if (p.typ=ait_instruction) and
  1746. MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  1747. (taicpu(p).ops = 3) and
  1748. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  1749. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  1750. (taicpu(p).oper[2]^.typ=top_const) and
  1751. (taicpu(p).oper[2]^.val >= 0) and
  1752. (taicpu(p).oper[2]^.val < 256) and
  1753. (not RegAllocated(p, NR_DEFAULTFLAGS)) then
  1754. begin
  1755. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  1756. taicpu(p).oppostfix:=PF_S;
  1757. taicpu(p).ops := 2;
  1758. result:=true;
  1759. end
  1760. else if (p.typ=ait_instruction) and
  1761. MatchInstruction(p, [A_AND,A_ORR,A_EOR,A_LSL,A_LSR,A_ASR,A_ROR], [C_None], [PF_None,PF_S]) and
  1762. (taicpu(p).ops = 3) and
  1763. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  1764. (taicpu(p).oper[2]^.typ=top_reg) and
  1765. (not RegAllocated(p, NR_DEFAULTFLAGS)) then
  1766. begin
  1767. taicpu(p).ops := 2;
  1768. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  1769. taicpu(p).oppostfix:=PF_S;
  1770. result:=true;
  1771. end
  1772. else if (p.typ=ait_instruction) and
  1773. MatchInstruction(p, [A_AND,A_ORR,A_EOR], [], [PF_None,PF_S]) and
  1774. (taicpu(p).ops = 3) and
  1775. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  1776. (not RegAllocated(p, NR_DEFAULTFLAGS)) then
  1777. begin
  1778. taicpu(p).oppostfix:=PF_S;
  1779. taicpu(p).ops := 2;
  1780. result:=true;
  1781. end
  1782. else if (p.typ=ait_instruction) and
  1783. MatchInstruction(p, [A_AND], [], [PF_None]) and
  1784. (taicpu(p).ops = 2) and
  1785. (taicpu(p).oper[1]^.typ=top_const) and
  1786. ((taicpu(p).oper[1]^.val=255) or
  1787. (taicpu(p).oper[1]^.val=65535)) then
  1788. begin
  1789. if taicpu(p).oper[1]^.val=255 then
  1790. taicpu(p).opcode:=A_UXTB
  1791. else
  1792. taicpu(p).opcode:=A_UXTH;
  1793. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  1794. result := true;
  1795. end
  1796. else if (p.typ=ait_instruction) and
  1797. MatchInstruction(p, [A_AND], [], [PF_None]) and
  1798. (taicpu(p).ops = 3) and
  1799. (taicpu(p).oper[2]^.typ=top_const) and
  1800. ((taicpu(p).oper[2]^.val=255) or
  1801. (taicpu(p).oper[2]^.val=65535)) then
  1802. begin
  1803. if taicpu(p).oper[2]^.val=255 then
  1804. taicpu(p).opcode:=A_UXTB
  1805. else
  1806. taicpu(p).opcode:=A_UXTH;
  1807. taicpu(p).ops:=2;
  1808. result := true;
  1809. end
  1810. {else if (p.typ=ait_instruction) and
  1811. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  1812. (taicpu(p).oper[1]^.typ=top_const) and
  1813. (taicpu(p).oper[1]^.val=0) and
  1814. GetNextInstruction(p,hp1) and
  1815. (taicpu(hp1).opcode=A_B) and
  1816. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  1817. begin
  1818. if taicpu(hp1).condition = C_EQ then
  1819. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  1820. else
  1821. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  1822. taicpu(hp2).is_jmp := true;
  1823. asml.InsertAfter(hp2, hp1);
  1824. asml.Remove(hp1);
  1825. hp1.Free;
  1826. asml.Remove(p);
  1827. p.Free;
  1828. p := hp2;
  1829. result := true;
  1830. end}
  1831. else
  1832. Result := inherited PeepHoleOptPass1Cpu(p);
  1833. end;
  1834. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  1835. var
  1836. p,hp1,hp2: tai;
  1837. l,l2 : longint;
  1838. condition : tasmcond;
  1839. hp3: tai;
  1840. WasLast: boolean;
  1841. { UsedRegs, TmpUsedRegs: TRegSet; }
  1842. begin
  1843. p := BlockStart;
  1844. { UsedRegs := []; }
  1845. while (p <> BlockEnd) Do
  1846. begin
  1847. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  1848. case p.Typ Of
  1849. Ait_Instruction:
  1850. begin
  1851. case taicpu(p).opcode Of
  1852. A_B:
  1853. if taicpu(p).condition<>C_None then
  1854. begin
  1855. { check for
  1856. Bxx xxx
  1857. <several instructions>
  1858. xxx:
  1859. }
  1860. l:=0;
  1861. GetNextInstruction(p, hp1);
  1862. while assigned(hp1) and
  1863. (l<=4) and
  1864. CanBeCond(hp1) and
  1865. { stop on labels }
  1866. not(hp1.typ=ait_label) do
  1867. begin
  1868. inc(l);
  1869. if MustBeLast(hp1) then
  1870. begin
  1871. //hp1:=nil;
  1872. GetNextInstruction(hp1,hp1);
  1873. break;
  1874. end
  1875. else
  1876. GetNextInstruction(hp1,hp1);
  1877. end;
  1878. if assigned(hp1) then
  1879. begin
  1880. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1881. begin
  1882. if (l<=4) and (l>0) then
  1883. begin
  1884. condition:=inverse_cond(taicpu(p).condition);
  1885. hp2:=p;
  1886. GetNextInstruction(p,hp1);
  1887. p:=hp1;
  1888. repeat
  1889. if hp1.typ=ait_instruction then
  1890. taicpu(hp1).condition:=condition;
  1891. if MustBeLast(hp1) then
  1892. begin
  1893. GetNextInstruction(hp1,hp1);
  1894. break;
  1895. end
  1896. else
  1897. GetNextInstruction(hp1,hp1);
  1898. until not(assigned(hp1)) or
  1899. not(CanBeCond(hp1)) or
  1900. (hp1.typ=ait_label);
  1901. { wait with removing else GetNextInstruction could
  1902. ignore the label if it was the only usage in the
  1903. jump moved away }
  1904. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  1905. DecrementPreceedingIT(asml, hp2);
  1906. case l of
  1907. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  1908. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  1909. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  1910. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  1911. end;
  1912. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1913. asml.remove(hp2);
  1914. hp2.free;
  1915. continue;
  1916. end;
  1917. end;
  1918. end;
  1919. end;
  1920. end;
  1921. end;
  1922. end;
  1923. p := tai(p.next)
  1924. end;
  1925. end;
  1926. begin
  1927. casmoptimizer:=TCpuAsmOptimizer;
  1928. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  1929. End.