aoptcpu.pas 111 KB

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