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. not(current_settings.cputype in cpu_thumb) and
  70. (p.typ=ait_instruction) and
  71. (taicpu(p).condition=C_None) and
  72. ((taicpu(p).opcode<A_IT) or (taicpu(p).opcode>A_ITTTT)) and
  73. (taicpu(p).opcode<>A_CBZ) and
  74. (taicpu(p).opcode<>A_CBNZ) and
  75. (taicpu(p).opcode<>A_PLD) and
  76. ((taicpu(p).opcode<>A_BLX) or
  77. (taicpu(p).oper[0]^.typ=top_reg));
  78. end;
  79. function RefsEqual(const r1, r2: treference): boolean;
  80. begin
  81. refsequal :=
  82. (r1.offset = r2.offset) and
  83. (r1.base = r2.base) and
  84. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  85. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  86. (r1.relsymbol = r2.relsymbol) and
  87. (r1.signindex = r2.signindex) and
  88. (r1.shiftimm = r2.shiftimm) and
  89. (r1.addressmode = r2.addressmode) and
  90. (r1.shiftmode = r2.shiftmode);
  91. end;
  92. function MatchInstruction(const instr: tai; const op: TCommonAsmOps; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  93. begin
  94. result :=
  95. (instr.typ = ait_instruction) and
  96. ((op = []) or ((ord(taicpu(instr).opcode)<256) and (taicpu(instr).opcode in op))) and
  97. ((cond = []) or (taicpu(instr).condition in cond)) and
  98. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  99. end;
  100. function MatchInstruction(const instr: tai; const op: TAsmOp; const cond: TAsmConds; const postfix: TOpPostfixes): boolean;
  101. begin
  102. result :=
  103. (instr.typ = ait_instruction) and
  104. (taicpu(instr).opcode = op) and
  105. ((cond = []) or (taicpu(instr).condition in cond)) and
  106. ((postfix = []) or (taicpu(instr).oppostfix in postfix));
  107. end;
  108. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  109. begin
  110. result := oper1.typ = oper2.typ;
  111. if result then
  112. case oper1.typ of
  113. top_const:
  114. Result:=oper1.val = oper2.val;
  115. top_reg:
  116. Result:=oper1.reg = oper2.reg;
  117. top_conditioncode:
  118. Result:=oper1.cc = oper2.cc;
  119. top_ref:
  120. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  121. else Result:=false;
  122. end
  123. end;
  124. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  125. begin
  126. result := (oper.typ = top_reg) and (oper.reg = reg);
  127. end;
  128. procedure RemoveRedundantMove(const cmpp: tai; movp: tai; asml: TAsmList);
  129. begin
  130. if (taicpu(movp).condition = C_EQ) and
  131. (taicpu(cmpp).oper[0]^.reg = taicpu(movp).oper[0]^.reg) and
  132. (taicpu(cmpp).oper[1]^.val = taicpu(movp).oper[1]^.val) then
  133. begin
  134. asml.insertafter(tai_comment.Create(strpnew('Peephole CmpMovMov - Removed redundant moveq')), movp);
  135. asml.remove(movp);
  136. movp.free;
  137. end;
  138. end;
  139. function regLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  140. var
  141. p: taicpu;
  142. begin
  143. p := taicpu(hp);
  144. regLoadedWithNewValue := false;
  145. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  146. exit;
  147. case p.opcode of
  148. { These operands do not write into a register at all }
  149. A_CMP, A_CMN, A_TST, A_TEQ, A_B, A_BL, A_BX, A_BLX, A_SWI, A_MSR, A_PLD:
  150. exit;
  151. {Take care of post/preincremented store and loads, they will change their base register}
  152. A_STR, A_LDR:
  153. begin
  154. regLoadedWithNewValue :=
  155. (taicpu(p).oper[1]^.typ=top_ref) and
  156. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  157. (taicpu(p).oper[1]^.ref^.base = reg);
  158. {STR does not load into it's first register}
  159. if p.opcode = A_STR then exit;
  160. end;
  161. { These four are writing into the first 2 register, UMLAL and SMLAL will also read from them }
  162. A_UMLAL, A_UMULL, A_SMLAL, A_SMULL:
  163. regLoadedWithNewValue :=
  164. (p.oper[1]^.typ = top_reg) and
  165. (p.oper[1]^.reg = reg);
  166. {Loads to oper2 from coprocessor}
  167. {
  168. MCR/MRC is currently not supported in FPC
  169. A_MRC:
  170. regLoadedWithNewValue :=
  171. (p.oper[2]^.typ = top_reg) and
  172. (p.oper[2]^.reg = reg);
  173. }
  174. {Loads to all register in the registerset}
  175. A_LDM:
  176. regLoadedWithNewValue := (getsupreg(reg) in p.oper[1]^.regset^);
  177. end;
  178. if regLoadedWithNewValue then
  179. exit;
  180. case p.oper[0]^.typ of
  181. {This is the case}
  182. top_reg:
  183. regLoadedWithNewValue := (p.oper[0]^.reg = reg) or
  184. { LDRD }
  185. (p.opcode=A_LDR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg));
  186. {LDM/STM might write a new value to their index register}
  187. top_ref:
  188. regLoadedWithNewValue :=
  189. (taicpu(p).oper[0]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) and
  190. (taicpu(p).oper[0]^.ref^.base = reg);
  191. end;
  192. end;
  193. function AlignedToQWord(const ref : treference) : boolean;
  194. begin
  195. { (safe) heuristics to ensure alignment }
  196. result:=(target_info.abi in [abi_eabi,abi_armeb,abi_eabihf]) and
  197. (((ref.offset>=0) and
  198. ((ref.offset mod 8)=0) and
  199. ((ref.base=NR_R13) or
  200. (ref.index=NR_R13))
  201. ) or
  202. ((ref.offset<=0) and
  203. { when using NR_R11, it has always a value of <qword align>+4 }
  204. ((abs(ref.offset+4) mod 8)=0) and
  205. (current_procinfo.framepointer=NR_R11) and
  206. ((ref.base=NR_R11) or
  207. (ref.index=NR_R11))
  208. )
  209. );
  210. end;
  211. function instructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  212. var
  213. p: taicpu;
  214. i: longint;
  215. begin
  216. instructionLoadsFromReg := false;
  217. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  218. exit;
  219. p:=taicpu(hp);
  220. i:=1;
  221. {For these instructions we have to start on oper[0]}
  222. if (p.opcode in [A_STR, A_LDM, A_STM, A_PLD,
  223. A_CMP, A_CMN, A_TST, A_TEQ,
  224. A_B, A_BL, A_BX, A_BLX,
  225. A_SMLAL, A_UMLAL]) then i:=0;
  226. while(i<p.ops) do
  227. begin
  228. case p.oper[I]^.typ of
  229. top_reg:
  230. instructionLoadsFromReg := (p.oper[I]^.reg = reg) or
  231. { STRD }
  232. ((i=0) and (p.opcode=A_STR) and (p.oppostfix=PF_D) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  233. top_regset:
  234. instructionLoadsFromReg := (getsupreg(reg) in p.oper[I]^.regset^);
  235. top_shifterop:
  236. instructionLoadsFromReg := p.oper[I]^.shifterop^.rs = reg;
  237. top_ref:
  238. instructionLoadsFromReg :=
  239. (p.oper[I]^.ref^.base = reg) or
  240. (p.oper[I]^.ref^.index = reg);
  241. end;
  242. if instructionLoadsFromReg then exit; {Bailout if we found something}
  243. Inc(I);
  244. end;
  245. end;
  246. function isValidConstLoadStoreOffset(const aoffset: longint; const pf: TOpPostfix) : boolean;
  247. begin
  248. if current_settings.cputype in cpu_thumb2 then
  249. result := (aoffset<4096) and (aoffset>-256)
  250. else
  251. result := ((pf in [PF_None,PF_B]) and
  252. (abs(aoffset)<4096)) or
  253. (abs(aoffset)<256);
  254. end;
  255. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  256. var AllUsedRegs: TAllUsedRegs): Boolean;
  257. begin
  258. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  259. RegUsedAfterInstruction :=
  260. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  261. not(regLoadedWithNewValue(reg,p)) and
  262. (
  263. not(GetNextInstruction(p,p)) or
  264. instructionLoadsFromReg(reg,p) or
  265. not(regLoadedWithNewValue(reg,p))
  266. );
  267. end;
  268. function TCpuAsmOptimizer.RegEndOfLife(reg : TRegister;p : taicpu) : boolean;
  269. begin
  270. Result:=assigned(FindRegDealloc(reg,tai(p.Next))) or
  271. RegLoadedWithNewValue(reg,p);
  272. end;
  273. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  274. var Next: tai; reg: TRegister): Boolean;
  275. begin
  276. Next:=Current;
  277. repeat
  278. Result:=GetNextInstruction(Next,Next);
  279. until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  280. (is_calljmp(taicpu(Next).opcode)) or (RegInInstruction(NR_PC,Next));
  281. end;
  282. {$ifdef DEBUG_AOPTCPU}
  283. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  284. begin
  285. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  286. end;
  287. {$else DEBUG_AOPTCPU}
  288. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  289. begin
  290. end;
  291. {$endif DEBUG_AOPTCPU}
  292. procedure TCpuAsmOptimizer.RemoveSuperfluousMove(const p: tai; movp: tai; const optimizer: string);
  293. var
  294. alloc,
  295. dealloc : tai_regalloc;
  296. hp1 : tai;
  297. begin
  298. if MatchInstruction(movp, A_MOV, [taicpu(p).condition], [PF_None]) and
  299. (taicpu(movp).ops=2) and {We can't optimize if there is a shiftop}
  300. MatchOperand(taicpu(movp).oper[1]^, taicpu(p).oper[0]^.reg) and
  301. { don't mess with moves to pc }
  302. (taicpu(movp).oper[0]^.reg<>NR_PC) and
  303. { don't mess with moves to lr }
  304. (taicpu(movp).oper[0]^.reg<>NR_R14) and
  305. { the destination register of the mov might not be used beween p and movp }
  306. not(RegUsedBetween(taicpu(movp).oper[0]^.reg,p,movp)) and
  307. { cb[n]z are thumb instructions which require specific registers, with no wide forms }
  308. (taicpu(p).opcode<>A_CBZ) and
  309. (taicpu(p).opcode<>A_CBNZ) and
  310. {There is a special requirement for MUL and MLA, oper[0] and oper[1] are not allowed to be the same}
  311. not (
  312. (taicpu(p).opcode in [A_MLA, A_MUL]) and
  313. (taicpu(p).oper[1]^.reg = taicpu(movp).oper[0]^.reg) and
  314. (current_settings.cputype < cpu_armv6)
  315. ) and
  316. { Take care to only do this for instructions which REALLY load to the first register.
  317. Otherwise
  318. str reg0, [reg1]
  319. mov reg2, reg0
  320. will be optimized to
  321. str reg2, [reg1]
  322. }
  323. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, p) then
  324. begin
  325. dealloc:=FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(movp.Next));
  326. if assigned(dealloc) then
  327. begin
  328. DebugMsg('Peephole '+optimizer+' removed superfluous mov', movp);
  329. { taicpu(p).oper[0]^.reg is not used anymore, try to find its allocation
  330. and remove it if possible }
  331. GetLastInstruction(p,hp1);
  332. asml.Remove(dealloc);
  333. alloc:=FindRegAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  334. if assigned(alloc) then
  335. begin
  336. asml.Remove(alloc);
  337. alloc.free;
  338. dealloc.free;
  339. end
  340. else
  341. asml.InsertAfter(dealloc,p);
  342. { try to move the allocation of the target register }
  343. GetLastInstruction(movp,hp1);
  344. alloc:=FindRegAlloc(taicpu(movp).oper[0]^.reg,tai(hp1.Next));
  345. if assigned(alloc) then
  346. begin
  347. asml.Remove(alloc);
  348. asml.InsertBefore(alloc,p);
  349. { adjust used regs }
  350. IncludeRegInUsedRegs(taicpu(movp).oper[0]^.reg,UsedRegs);
  351. end;
  352. { finally get rid of the mov }
  353. taicpu(p).loadreg(0,taicpu(movp).oper[0]^.reg);
  354. asml.remove(movp);
  355. movp.free;
  356. end;
  357. end;
  358. end;
  359. {
  360. optimize
  361. ldr/str regX,[reg1]
  362. ...
  363. add/sub reg1,reg1,regY/const
  364. into
  365. ldr/str regX,[reg1], regY/const
  366. }
  367. function TCpuAsmOptimizer.LookForPostindexedPattern(p: taicpu) : boolean;
  368. var
  369. hp1 : tai;
  370. begin
  371. Result:=false;
  372. if (p.oper[1]^.ref^.addressmode=AM_OFFSET) and
  373. (p.oper[1]^.ref^.index=NR_NO) and
  374. (p.oper[1]^.ref^.offset=0) and
  375. GetNextInstructionUsingReg(p, hp1, p.oper[1]^.ref^.base) and
  376. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  377. MatchInstruction(hp1, [A_ADD, A_SUB], [C_None], [PF_None]) and
  378. (taicpu(hp1).oper[0]^.reg=p.oper[1]^.ref^.base) and
  379. (taicpu(hp1).oper[1]^.reg=p.oper[1]^.ref^.base) and
  380. (
  381. (taicpu(hp1).oper[2]^.typ=top_reg) or
  382. { valid offset? }
  383. ((taicpu(hp1).oper[2]^.typ=top_const) and
  384. ((abs(taicpu(hp1).oper[2]^.val)<256) or
  385. ((abs(taicpu(hp1).oper[2]^.val)<4096) and (p.oppostfix in [PF_None,PF_B]))
  386. )
  387. )
  388. ) and
  389. { don't apply the optimization if the base register is loaded }
  390. (p.oper[0]^.reg<>p.oper[1]^.ref^.base) and
  391. not(RegModifiedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) and
  392. { don't apply the optimization if the (new) index register is loaded }
  393. (p.oper[0]^.reg<>taicpu(hp1).oper[2]^.reg) and
  394. not(RegModifiedBetween(taicpu(hp1).oper[2]^.reg,p,hp1)) then
  395. begin
  396. DebugMsg('Peephole Str/LdrAdd/Sub2Str/Ldr Postindex done', p);
  397. p.oper[1]^.ref^.addressmode:=AM_POSTINDEXED;
  398. if taicpu(hp1).oper[2]^.typ=top_const then
  399. begin
  400. if taicpu(hp1).opcode=A_ADD then
  401. p.oper[1]^.ref^.offset:=taicpu(hp1).oper[2]^.val
  402. else
  403. p.oper[1]^.ref^.offset:=-taicpu(hp1).oper[2]^.val;
  404. end
  405. else
  406. begin
  407. p.oper[1]^.ref^.index:=taicpu(hp1).oper[2]^.reg;
  408. if taicpu(hp1).opcode=A_ADD then
  409. p.oper[1]^.ref^.signindex:=1
  410. else
  411. p.oper[1]^.ref^.signindex:=-1;
  412. end;
  413. asml.Remove(hp1);
  414. hp1.Free;
  415. Result:=true;
  416. end;
  417. end;
  418. { skip harmless marker marking entry/exit code, so it can be optimized as well }
  419. function TCpuAsmOptimizer.SkipEntryExitMarker(current : tai;var next : tai) : boolean;
  420. begin
  421. result:=true;
  422. if current.typ<>ait_marker then
  423. exit;
  424. next:=current;
  425. while GetNextInstruction(next,next) do
  426. begin
  427. if (next.typ<>ait_marker) or not(tai_marker(next).Kind in [mark_Position,mark_BlockStart]) then
  428. exit;
  429. end;
  430. result:=false;
  431. end;
  432. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  433. var
  434. hp1,hp2,hp3,hp4: tai;
  435. i, i2: longint;
  436. TmpUsedRegs: TAllUsedRegs;
  437. tempop: tasmop;
  438. function IsPowerOf2(const value: DWord): boolean; inline;
  439. begin
  440. Result:=(value and (value - 1)) = 0;
  441. end;
  442. begin
  443. result := false;
  444. case p.typ of
  445. ait_instruction:
  446. begin
  447. {
  448. change
  449. <op> reg,x,y
  450. cmp reg,#0
  451. into
  452. <op>s reg,x,y
  453. }
  454. { this optimization can applied only to the currently enabled operations because
  455. the other operations do not update all flags and FPC does not track flag usage }
  456. if MatchInstruction(p, [A_ADC,A_ADD,A_BIC,A_SUB,A_MUL,A_MVN,A_MOV,A_ORR,A_EOR,A_AND,
  457. A_RSB,A_RSC,A_SBC,A_MLA], [C_None], [PF_None]) and
  458. GetNextInstruction(p, hp1) and
  459. MatchInstruction(hp1, A_CMP, [C_None], [PF_None]) and
  460. (taicpu(hp1).oper[1]^.typ = top_const) and
  461. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg) and
  462. (taicpu(hp1).oper[1]^.val = 0) and
  463. GetNextInstruction(hp1, hp2) and
  464. { be careful here, following instructions could use other flags
  465. however after a jump fpc never depends on the value of flags }
  466. { All above instructions set Z and N according to the following
  467. Z := result = 0;
  468. N := result[31];
  469. EQ = Z=1; NE = Z=0;
  470. MI = N=1; PL = N=0; }
  471. MatchInstruction(hp2, A_B, [C_EQ,C_NE,C_MI,C_PL], []) and
  472. assigned(FindRegDealloc(NR_DEFAULTFLAGS,tai(hp2.Next))) then
  473. begin
  474. DebugMsg('Peephole OpCmp2OpS done', p);
  475. taicpu(p).oppostfix:=PF_S;
  476. { move flag allocation if possible }
  477. GetLastInstruction(hp1, hp2);
  478. hp2:=FindRegAlloc(NR_DEFAULTFLAGS,tai(hp2.Next));
  479. if assigned(hp2) then
  480. begin
  481. asml.Remove(hp2);
  482. asml.insertbefore(hp2, p);
  483. end;
  484. asml.remove(hp1);
  485. hp1.free;
  486. end
  487. else
  488. case taicpu(p).opcode of
  489. A_STR:
  490. begin
  491. { change
  492. str reg1,ref
  493. ldr reg2,ref
  494. into
  495. str reg1,ref
  496. mov reg2,reg1
  497. }
  498. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  499. (taicpu(p).oppostfix=PF_None) and
  500. GetNextInstruction(p,hp1) and
  501. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [PF_None]) and
  502. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  503. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  504. begin
  505. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  506. begin
  507. DebugMsg('Peephole StrLdr2StrMov 1 done', hp1);
  508. asml.remove(hp1);
  509. hp1.free;
  510. end
  511. else
  512. begin
  513. taicpu(hp1).opcode:=A_MOV;
  514. taicpu(hp1).oppostfix:=PF_None;
  515. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  516. DebugMsg('Peephole StrLdr2StrMov 2 done', hp1);
  517. end;
  518. result := true;
  519. end
  520. { change
  521. str reg1,ref
  522. str reg2,ref
  523. into
  524. strd reg1,ref
  525. }
  526. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  527. (taicpu(p).oppostfix=PF_None) and
  528. (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  529. GetNextInstruction(p,hp1) and
  530. MatchInstruction(hp1, A_STR, [taicpu(p).condition, C_None], [PF_None]) and
  531. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  532. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  533. { str ensures that either base or index contain no register, else ldr wouldn't
  534. use an offset either
  535. }
  536. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  537. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  538. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  539. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  540. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  541. begin
  542. DebugMsg('Peephole StrStr2Strd done', p);
  543. taicpu(p).oppostfix:=PF_D;
  544. asml.remove(hp1);
  545. hp1.free;
  546. end;
  547. LookForPostindexedPattern(taicpu(p));
  548. end;
  549. A_LDR:
  550. begin
  551. { change
  552. ldr reg1,ref
  553. ldr reg2,ref
  554. into ...
  555. }
  556. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  557. GetNextInstruction(p,hp1) and
  558. { ldrd is not allowed here }
  559. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  560. begin
  561. {
  562. ...
  563. ldr reg1,ref
  564. mov reg2,reg1
  565. }
  566. if RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  567. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  568. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  569. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  570. begin
  571. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  572. begin
  573. DebugMsg('Peephole LdrLdr2Ldr done', hp1);
  574. asml.remove(hp1);
  575. hp1.free;
  576. end
  577. else
  578. begin
  579. DebugMsg('Peephole LdrLdr2LdrMov done', hp1);
  580. taicpu(hp1).opcode:=A_MOV;
  581. taicpu(hp1).oppostfix:=PF_None;
  582. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  583. end;
  584. result := true;
  585. end
  586. {
  587. ...
  588. ldrd reg1,ref
  589. }
  590. else if (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  591. { ldrd does not allow any postfixes ... }
  592. (taicpu(p).oppostfix=PF_None) and
  593. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  594. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  595. { ldr ensures that either base or index contain no register, else ldr wouldn't
  596. use an offset either
  597. }
  598. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  599. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  600. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  601. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  602. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  603. begin
  604. DebugMsg('Peephole LdrLdr2Ldrd done', p);
  605. taicpu(p).oppostfix:=PF_D;
  606. asml.remove(hp1);
  607. hp1.free;
  608. end;
  609. end;
  610. LookForPostindexedPattern(taicpu(p));
  611. { Remove superfluous mov after ldr
  612. changes
  613. ldr reg1, ref
  614. mov reg2, reg1
  615. to
  616. ldr reg2, ref
  617. conditions are:
  618. * no ldrd usage
  619. * reg1 must be released after mov
  620. * mov can not contain shifterops
  621. * ldr+mov have the same conditions
  622. * mov does not set flags
  623. }
  624. if (taicpu(p).oppostfix<>PF_D) and GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  625. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr');
  626. end;
  627. A_MOV:
  628. begin
  629. { fold
  630. mov reg1,reg0, shift imm1
  631. mov reg1,reg1, shift imm2
  632. }
  633. if (taicpu(p).ops=3) and
  634. (taicpu(p).oper[2]^.typ = top_shifterop) and
  635. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  636. getnextinstruction(p,hp1) and
  637. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  638. (taicpu(hp1).ops=3) and
  639. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  640. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  641. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  642. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  643. begin
  644. { fold
  645. mov reg1,reg0, lsl 16
  646. mov reg1,reg1, lsr 16
  647. strh reg1, ...
  648. dealloc reg1
  649. to
  650. strh reg1, ...
  651. dealloc reg1
  652. }
  653. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  654. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  655. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  656. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  657. getnextinstruction(hp1,hp2) and
  658. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  659. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  660. begin
  661. CopyUsedRegs(TmpUsedRegs);
  662. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  663. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  664. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  665. begin
  666. DebugMsg('Peephole optimizer removed superfluous 16 Bit zero extension', hp1);
  667. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  668. asml.remove(p);
  669. asml.remove(hp1);
  670. p.free;
  671. hp1.free;
  672. p:=hp2;
  673. end;
  674. ReleaseUsedRegs(TmpUsedRegs);
  675. end
  676. { fold
  677. mov reg1,reg0, shift imm1
  678. mov reg1,reg1, shift imm2
  679. to
  680. mov reg1,reg0, shift imm1+imm2
  681. }
  682. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  683. { asr makes no use after a lsr, the asr can be foled into the lsr }
  684. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  685. begin
  686. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  687. { avoid overflows }
  688. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  689. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  690. SM_ROR:
  691. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  692. SM_ASR:
  693. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  694. SM_LSR,
  695. SM_LSL:
  696. begin
  697. hp1:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  698. InsertLLItem(p.previous, p.next, hp1);
  699. p.free;
  700. p:=hp1;
  701. end;
  702. else
  703. internalerror(2008072803);
  704. end;
  705. DebugMsg('Peephole ShiftShift2Shift 1 done', p);
  706. asml.remove(hp1);
  707. hp1.free;
  708. result := true;
  709. end
  710. { fold
  711. mov reg1,reg0, shift imm1
  712. mov reg1,reg1, shift imm2
  713. mov reg1,reg1, shift imm3 ...
  714. mov reg2,reg1, shift imm3 ...
  715. }
  716. else if GetNextInstructionUsingReg(hp1,hp2, taicpu(hp1).oper[0]^.reg) and
  717. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  718. (taicpu(hp2).ops=3) and
  719. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  720. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp2)) and
  721. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  722. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  723. begin
  724. { mov reg1,reg0, lsl imm1
  725. mov reg1,reg1, lsr/asr imm2
  726. mov reg2,reg1, lsl imm3 ...
  727. to
  728. mov reg1,reg0, lsl imm1
  729. mov reg2,reg1, lsr/asr imm2-imm3
  730. if
  731. imm1>=imm2
  732. }
  733. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  734. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  735. (taicpu(p).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  736. begin
  737. if (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  738. begin
  739. if not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,p,hp1)) and
  740. not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2)) then
  741. begin
  742. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1a done', p);
  743. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm-taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  744. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  745. asml.remove(hp1);
  746. asml.remove(hp2);
  747. hp1.free;
  748. hp2.free;
  749. if taicpu(p).oper[2]^.shifterop^.shiftimm>=32 then
  750. begin
  751. taicpu(p).freeop(1);
  752. taicpu(p).freeop(2);
  753. taicpu(p).loadconst(1,0);
  754. end;
  755. result := true;
  756. end;
  757. end
  758. else if not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2)) then
  759. begin
  760. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1b done', p);
  761. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  762. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  763. asml.remove(hp2);
  764. hp2.free;
  765. result := true;
  766. end;
  767. end
  768. { mov reg1,reg0, lsr/asr imm1
  769. mov reg1,reg1, lsl imm2
  770. mov reg1,reg1, lsr/asr imm3 ...
  771. if imm3>=imm1 and imm2>=imm1
  772. to
  773. mov reg1,reg0, lsl imm2-imm1
  774. mov reg1,reg1, lsr/asr imm3 ...
  775. }
  776. 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
  777. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  778. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  779. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  780. begin
  781. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  782. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  783. DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
  784. asml.remove(p);
  785. p.free;
  786. p:=hp2;
  787. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  788. begin
  789. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  790. asml.remove(hp1);
  791. hp1.free;
  792. p:=hp2;
  793. end;
  794. result := true;
  795. end;
  796. end;
  797. end;
  798. { Change the common
  799. mov r0, r0, lsr #xxx
  800. and r0, r0, #yyy/bic r0, r0, #xxx
  801. and remove the superfluous and/bic if possible
  802. This could be extended to handle more cases.
  803. }
  804. if (taicpu(p).ops=3) and
  805. (taicpu(p).oper[2]^.typ = top_shifterop) and
  806. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  807. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  808. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  809. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  810. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) then
  811. begin
  812. if (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  813. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  814. (taicpu(hp1).ops=3) and
  815. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  816. (taicpu(hp1).oper[2]^.typ = top_const) and
  817. { Check if the AND actually would only mask out bits beeing already zero because of the shift
  818. For LSR #25 and an AndConst of 255 that whould go like this:
  819. 255 and ((2 shl (32-25))-1)
  820. which results in 127, which is one less a power-of-2, meaning all lower bits are set.
  821. LSR #25 and AndConst of 254:
  822. 254 and ((2 shl (32-25))-1) = 126 -> lowest bit is clear, so we can't remove it.
  823. }
  824. ispowerof2((taicpu(hp1).oper[2]^.val and ((2 shl (32-taicpu(p).oper[2]^.shifterop^.shiftimm))-1))+1) then
  825. begin
  826. DebugMsg('Peephole LsrAnd2Lsr done', hp1);
  827. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  828. asml.remove(hp1);
  829. hp1.free;
  830. result:=true;
  831. end
  832. else if MatchInstruction(hp1, A_BIC, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  833. (taicpu(hp1).ops=3) and
  834. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  835. (taicpu(hp1).oper[2]^.typ = top_const) and
  836. { Check if the BIC actually would only mask out bits beeing already zero because of the shift }
  837. (taicpu(hp1).oper[2]^.val<>0) and
  838. (BsfDWord(taicpu(hp1).oper[2]^.val)>=32-taicpu(p).oper[2]^.shifterop^.shiftimm) then
  839. begin
  840. DebugMsg('Peephole LsrBic2Lsr done', hp1);
  841. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  842. asml.remove(hp1);
  843. hp1.free;
  844. result:=true;
  845. end;
  846. end;
  847. {
  848. optimize
  849. mov rX, yyyy
  850. ....
  851. }
  852. if (taicpu(p).ops = 2) and
  853. GetNextInstruction(p,hp1) and
  854. (tai(hp1).typ = ait_instruction) then
  855. begin
  856. {
  857. This changes the very common
  858. mov r0, #0
  859. str r0, [...]
  860. mov r0, #0
  861. str r0, [...]
  862. and removes all superfluous mov instructions
  863. }
  864. if (taicpu(p).oper[1]^.typ = top_const) and
  865. (taicpu(hp1).opcode=A_STR) then
  866. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  867. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  868. GetNextInstruction(hp1, hp2) and
  869. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  870. (taicpu(hp2).ops = 2) and
  871. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  872. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  873. begin
  874. DebugMsg('Peephole MovStrMov done', hp2);
  875. GetNextInstruction(hp2,hp1);
  876. asml.remove(hp2);
  877. hp2.free;
  878. if not assigned(hp1) then break;
  879. end
  880. {
  881. This removes the first mov from
  882. mov rX,...
  883. mov rX,...
  884. }
  885. else if taicpu(hp1).opcode=A_MOV then
  886. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  887. (taicpu(hp1).ops = 2) and
  888. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  889. { don't remove the first mov if the second is a mov rX,rX }
  890. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  891. begin
  892. DebugMsg('Peephole MovMov done', p);
  893. asml.remove(p);
  894. p.free;
  895. p:=hp1;
  896. GetNextInstruction(hp1,hp1);
  897. if not assigned(hp1) then
  898. break;
  899. end;
  900. end;
  901. {
  902. change
  903. mov r1, r0
  904. add r1, r1, #1
  905. to
  906. add r1, r0, #1
  907. Todo: Make it work for mov+cmp too
  908. CAUTION! If this one is successful p might not be a mov instruction anymore!
  909. }
  910. if (taicpu(p).ops = 2) and
  911. (taicpu(p).oper[1]^.typ = top_reg) and
  912. (taicpu(p).oppostfix = PF_NONE) and
  913. GetNextInstruction(p, hp1) and
  914. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  915. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN],
  916. [taicpu(p).condition], []) and
  917. {MOV and MVN might only have 2 ops}
  918. (taicpu(hp1).ops >= 2) and
  919. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  920. (taicpu(hp1).oper[1]^.typ = top_reg) and
  921. (
  922. (taicpu(hp1).ops = 2) or
  923. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop])
  924. ) then
  925. begin
  926. { When we get here we still don't know if the registers match}
  927. for I:=1 to 2 do
  928. {
  929. If the first loop was successful p will be replaced with hp1.
  930. The checks will still be ok, because all required information
  931. will also be in hp1 then.
  932. }
  933. if (taicpu(hp1).ops > I) and
  934. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  935. begin
  936. DebugMsg('Peephole RedundantMovProcess done', hp1);
  937. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  938. if p<>hp1 then
  939. begin
  940. asml.remove(p);
  941. p.free;
  942. p:=hp1;
  943. end;
  944. end;
  945. end;
  946. { This folds shifterops into following instructions
  947. mov r0, r1, lsl #8
  948. add r2, r3, r0
  949. to
  950. add r2, r3, r1, lsl #8
  951. CAUTION! If this one is successful p might not be a mov instruction anymore!
  952. }
  953. if (taicpu(p).opcode = A_MOV) and
  954. (taicpu(p).ops = 3) and
  955. (taicpu(p).oper[1]^.typ = top_reg) and
  956. (taicpu(p).oper[2]^.typ = top_shifterop) and
  957. (taicpu(p).oppostfix = PF_NONE) and
  958. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  959. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  960. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  961. A_CMP, A_CMN],
  962. [taicpu(p).condition], [PF_None]) and
  963. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  964. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) and
  965. (taicpu(hp1).ops >= 2) and
  966. {Currently we can't fold into another shifterop}
  967. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  968. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  969. NR_DEFAULTFLAGS for modification}
  970. (
  971. {Everything is fine if we don't use RRX}
  972. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  973. (
  974. {If it is RRX, then check if we're just accessing the next instruction}
  975. GetNextInstruction(p, hp2) and
  976. (hp1 = hp2)
  977. )
  978. ) and
  979. { reg1 might not be modified inbetween }
  980. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  981. { The shifterop can contain a register, might not be modified}
  982. (
  983. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  984. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  985. ) and
  986. (
  987. {Only ONE of the two src operands is allowed to match}
  988. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  989. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  990. ) then
  991. begin
  992. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  993. I2:=0
  994. else
  995. I2:=1;
  996. for I:=I2 to taicpu(hp1).ops-1 do
  997. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  998. begin
  999. { If the parameter matched on the second op from the RIGHT
  1000. we have to switch the parameters, this will not happen for CMP
  1001. were we're only evaluating the most right parameter
  1002. }
  1003. if I <> taicpu(hp1).ops-1 then
  1004. begin
  1005. {The SUB operators need to be changed when we swap parameters}
  1006. case taicpu(hp1).opcode of
  1007. A_SUB: tempop:=A_RSB;
  1008. A_SBC: tempop:=A_RSC;
  1009. A_RSB: tempop:=A_SUB;
  1010. A_RSC: tempop:=A_SBC;
  1011. else tempop:=taicpu(hp1).opcode;
  1012. end;
  1013. if taicpu(hp1).ops = 3 then
  1014. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  1015. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  1016. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1017. else
  1018. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  1019. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1020. taicpu(p).oper[2]^.shifterop^);
  1021. end
  1022. else
  1023. if taicpu(hp1).ops = 3 then
  1024. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  1025. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  1026. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1027. else
  1028. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  1029. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1030. taicpu(p).oper[2]^.shifterop^);
  1031. asml.insertbefore(hp2, hp1);
  1032. asml.remove(p);
  1033. asml.remove(hp1);
  1034. p.free;
  1035. hp1.free;
  1036. p:=hp2;
  1037. GetNextInstruction(p,hp1);
  1038. DebugMsg('Peephole FoldShiftProcess done', p);
  1039. break;
  1040. end;
  1041. end;
  1042. {
  1043. Fold
  1044. mov r1, r1, lsl #2
  1045. ldr/ldrb r0, [r0, r1]
  1046. to
  1047. ldr/ldrb r0, [r0, r1, lsl #2]
  1048. XXX: This still needs some work, as we quite often encounter something like
  1049. mov r1, r2, lsl #2
  1050. add r2, r3, #imm
  1051. ldr r0, [r2, r1]
  1052. which can't be folded because r2 is overwritten between the shift and the ldr.
  1053. We could try to shuffle the registers around and fold it into.
  1054. add r1, r3, #imm
  1055. ldr r0, [r1, r2, lsl #2]
  1056. }
  1057. if (taicpu(p).opcode = A_MOV) and
  1058. (taicpu(p).ops = 3) and
  1059. (taicpu(p).oper[1]^.typ = top_reg) and
  1060. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1061. { RRX is tough to handle, because it requires tracking the C-Flag,
  1062. it is also extremly unlikely to be emitted this way}
  1063. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) and
  1064. (taicpu(p).oper[2]^.shifterop^.shiftimm <> 0) and
  1065. (taicpu(p).oppostfix = PF_NONE) and
  1066. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1067. {Only LDR, LDRB, STR, STRB can handle scaled register indexing}
  1068. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition],
  1069. [PF_None, PF_B]) and
  1070. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) and
  1071. (taicpu(hp1).oper[1]^.ref^.base <> taicpu(p).oper[0]^.reg) and
  1072. { Only fold if there isn't another shifterop already. }
  1073. (taicpu(hp1).oper[1]^.ref^.shiftmode = SM_None) and
  1074. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1075. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1076. regLoadedWithNewValue(taicpu(p).oper[0]^.reg, hp1)) then
  1077. begin
  1078. DebugMsg('Peephole FoldShiftLdrStr done', hp1);
  1079. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1080. taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
  1081. taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
  1082. asml.remove(p);
  1083. p.free;
  1084. p:=hp1;
  1085. end;
  1086. {
  1087. Often we see shifts and then a superfluous mov to another register
  1088. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  1089. }
  1090. if (taicpu(p).opcode = A_MOV) and
  1091. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1092. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov');
  1093. end;
  1094. A_ADD,
  1095. A_ADC,
  1096. A_RSB,
  1097. A_RSC,
  1098. A_SUB,
  1099. A_SBC,
  1100. A_AND,
  1101. A_BIC,
  1102. A_EOR,
  1103. A_ORR,
  1104. A_MLA,
  1105. A_MUL:
  1106. begin
  1107. {
  1108. optimize
  1109. and reg2,reg1,const1
  1110. ...
  1111. }
  1112. if (taicpu(p).opcode = A_AND) and
  1113. (taicpu(p).ops>2) and
  1114. (taicpu(p).oper[1]^.typ = top_reg) and
  1115. (taicpu(p).oper[2]^.typ = top_const) then
  1116. begin
  1117. {
  1118. change
  1119. and reg2,reg1,const1
  1120. ...
  1121. and reg3,reg2,const2
  1122. to
  1123. and reg3,reg1,(const1 and const2)
  1124. }
  1125. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1126. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1127. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1128. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1129. (taicpu(hp1).oper[2]^.typ = top_const) then
  1130. begin
  1131. if not(RegUsedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) then
  1132. begin
  1133. DebugMsg('Peephole AndAnd2And done', p);
  1134. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1135. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1136. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1137. asml.remove(hp1);
  1138. hp1.free;
  1139. Result:=true;
  1140. end
  1141. else if not(RegUsedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1142. begin
  1143. DebugMsg('Peephole AndAnd2And done', hp1);
  1144. taicpu(hp1).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1145. taicpu(hp1).oppostfix:=taicpu(p).oppostfix;
  1146. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1147. asml.remove(p);
  1148. p.free;
  1149. p:=hp1;
  1150. Result:=true;
  1151. end;
  1152. end
  1153. {
  1154. change
  1155. and reg2,reg1,255
  1156. strb reg2,[...]
  1157. dealloc reg2
  1158. to
  1159. strb reg1,[...]
  1160. }
  1161. else if (taicpu(p).oper[2]^.val = 255) and
  1162. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1163. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1164. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1165. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1166. { the reference in strb might not use reg2 }
  1167. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1168. { reg1 might not be modified inbetween }
  1169. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1170. begin
  1171. DebugMsg('Peephole AndStrb2Strb done', p);
  1172. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1173. asml.remove(p);
  1174. p.free;
  1175. p:=hp1;
  1176. end
  1177. {
  1178. from
  1179. and reg1,reg0,2^n-1
  1180. mov reg2,reg1, lsl imm1
  1181. (mov reg3,reg2, lsr/asr imm1)
  1182. remove either the and or the lsl/xsr sequence if possible
  1183. }
  1184. else if cutils.ispowerof2(taicpu(p).oper[2]^.val+1,i) and
  1185. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1186. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  1187. (taicpu(hp1).ops=3) and
  1188. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1189. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  1190. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  1191. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  1192. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) then
  1193. begin
  1194. {
  1195. and reg1,reg0,2^n-1
  1196. mov reg2,reg1, lsl imm1
  1197. mov reg3,reg2, lsr/asr imm1
  1198. =>
  1199. and reg1,reg0,2^n-1
  1200. if lsr and 2^n-1>=imm1 or asr and 2^n-1>imm1
  1201. }
  1202. if GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[0]^.reg) and
  1203. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1204. (taicpu(hp2).ops=3) and
  1205. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  1206. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  1207. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) and
  1208. (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  1209. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=taicpu(hp2).oper[2]^.shifterop^.shiftimm) and
  1210. RegEndOfLife(taicpu(hp1).oper[0]^.reg,taicpu(hp2)) and
  1211. ((i<32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) or
  1212. ((i=32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1213. (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSR))) then
  1214. begin
  1215. DebugMsg('Peephole AndLslXsr2And done', p);
  1216. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  1217. asml.Remove(hp1);
  1218. asml.Remove(hp2);
  1219. hp1.free;
  1220. hp2.free;
  1221. result:=true;
  1222. end
  1223. {
  1224. and reg1,reg0,2^n-1
  1225. mov reg2,reg1, lsl imm1
  1226. =>
  1227. mov reg2,reg1, lsl imm1
  1228. if imm1>i
  1229. }
  1230. else if i>32-taicpu(hp1).oper[2]^.shifterop^.shiftimm then
  1231. begin
  1232. DebugMsg('Peephole AndLsl2Lsl done', p);
  1233. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[0]^.reg;
  1234. asml.Remove(p);
  1235. p.free;
  1236. p:=hp1;
  1237. result:=true;
  1238. end
  1239. end;
  1240. end;
  1241. {
  1242. change
  1243. add/sub reg2,reg1,const1
  1244. str/ldr reg3,[reg2,const2]
  1245. dealloc reg2
  1246. to
  1247. str/ldr reg3,[reg1,const2+/-const1]
  1248. }
  1249. if (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1250. (taicpu(p).ops>2) and
  1251. (taicpu(p).oper[1]^.typ = top_reg) and
  1252. (taicpu(p).oper[2]^.typ = top_const) then
  1253. begin
  1254. hp1:=p;
  1255. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1256. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1257. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1258. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1259. { don't optimize if the register is stored/overwritten }
  1260. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1261. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1262. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1263. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1264. ldr postfix }
  1265. (((taicpu(p).opcode=A_ADD) and
  1266. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1267. ) or
  1268. ((taicpu(p).opcode=A_SUB) and
  1269. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1270. )
  1271. ) do
  1272. begin
  1273. { neither reg1 nor reg2 might be changed inbetween }
  1274. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1275. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1276. break;
  1277. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1278. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1279. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1280. begin
  1281. { remember last instruction }
  1282. hp2:=hp1;
  1283. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1284. hp1:=p;
  1285. { fix all ldr/str }
  1286. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1287. begin
  1288. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1289. if taicpu(p).opcode=A_ADD then
  1290. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1291. else
  1292. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1293. if hp1=hp2 then
  1294. break;
  1295. end;
  1296. GetNextInstruction(p,hp1);
  1297. asml.remove(p);
  1298. p.free;
  1299. p:=hp1;
  1300. break;
  1301. end;
  1302. end;
  1303. end;
  1304. {
  1305. change
  1306. add reg1, ...
  1307. mov reg2, reg1
  1308. to
  1309. add reg2, ...
  1310. }
  1311. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) then
  1312. begin
  1313. if (taicpu(p).ops=3) then
  1314. RemoveSuperfluousMove(p, hp1, 'DataMov2Data');
  1315. end;
  1316. end;
  1317. {$ifdef dummy}
  1318. A_MVN:
  1319. begin
  1320. {
  1321. change
  1322. mvn reg2,reg1
  1323. and reg3,reg4,reg2
  1324. dealloc reg2
  1325. to
  1326. bic reg3,reg4,reg1
  1327. }
  1328. if (taicpu(p).oper[1]^.typ = top_reg) and
  1329. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1330. MatchInstruction(hp1,A_AND,[],[]) and
  1331. (((taicpu(hp1).ops=3) and
  1332. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1333. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1334. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1335. ((taicpu(hp1).ops=2) and
  1336. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1337. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1338. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1339. { reg1 might not be modified inbetween }
  1340. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1341. begin
  1342. DebugMsg('Peephole MvnAnd2Bic done', p);
  1343. taicpu(hp1).opcode:=A_BIC;
  1344. if taicpu(hp1).ops=3 then
  1345. begin
  1346. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1347. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1348. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1349. end
  1350. else
  1351. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1352. asml.remove(p);
  1353. p.free;
  1354. p:=hp1;
  1355. end;
  1356. end;
  1357. {$endif dummy}
  1358. A_UXTB:
  1359. begin
  1360. {
  1361. change
  1362. uxtb reg2,reg1
  1363. strb reg2,[...]
  1364. dealloc reg2
  1365. to
  1366. strb reg1,[...]
  1367. }
  1368. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1369. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1370. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1371. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1372. { the reference in strb might not use reg2 }
  1373. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1374. { reg1 might not be modified inbetween }
  1375. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1376. begin
  1377. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1378. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1379. asml.remove(p);
  1380. p.free;
  1381. p:=hp1;
  1382. end
  1383. {
  1384. change
  1385. uxtb reg2,reg1
  1386. uxth reg3,reg2
  1387. dealloc reg2
  1388. to
  1389. uxtb reg3,reg1
  1390. }
  1391. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1392. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1393. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1394. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1395. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1396. { reg1 might not be modified inbetween }
  1397. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1398. begin
  1399. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1400. taicpu(hp1).opcode:=A_UXTB;
  1401. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1402. asml.remove(p);
  1403. p.free;
  1404. p:=hp1;
  1405. end
  1406. {
  1407. change
  1408. uxtb reg2,reg1
  1409. uxtb reg3,reg2
  1410. dealloc reg2
  1411. to
  1412. uxtb reg3,reg1
  1413. }
  1414. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1415. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1416. MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  1417. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1418. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1419. { reg1 might not be modified inbetween }
  1420. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1421. begin
  1422. DebugMsg('Peephole UxtbUxtb2Uxtb done', p);
  1423. taicpu(hp1).opcode:=A_UXTB;
  1424. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1425. asml.remove(p);
  1426. p.free;
  1427. p:=hp1;
  1428. end
  1429. {
  1430. change
  1431. uxth reg2,reg1
  1432. uxth reg3,reg2
  1433. dealloc reg2
  1434. to
  1435. uxth reg3,reg1
  1436. }
  1437. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1438. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1439. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1440. (assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) or
  1441. (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[0]^.reg)) and
  1442. { reg1 might not be modified inbetween }
  1443. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1444. begin
  1445. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1446. taicpu(hp1).opcode:=A_UXTH;
  1447. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1448. asml.remove(p);
  1449. p.free;
  1450. p:=hp1;
  1451. end;
  1452. end;
  1453. A_UXTH:
  1454. begin
  1455. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1456. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1457. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1458. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1459. { the reference in strb might not use reg2 }
  1460. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1461. { reg1 might not be modified inbetween }
  1462. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1463. begin
  1464. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1465. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1466. asml.remove(p);
  1467. p.free;
  1468. p:=hp1;
  1469. end;
  1470. end;
  1471. A_CMP:
  1472. begin
  1473. {
  1474. change
  1475. cmp reg,const1
  1476. moveq reg,const1
  1477. movne reg,const2
  1478. to
  1479. cmp reg,const1
  1480. movne reg,const2
  1481. }
  1482. if (taicpu(p).oper[1]^.typ = top_const) and
  1483. GetNextInstruction(p, hp1) and
  1484. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1485. (taicpu(hp1).oper[1]^.typ = top_const) and
  1486. GetNextInstruction(hp1, hp2) and
  1487. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1488. (taicpu(hp1).oper[1]^.typ = top_const) then
  1489. begin
  1490. RemoveRedundantMove(p, hp1, asml);
  1491. RemoveRedundantMove(p, hp2, asml);
  1492. end;
  1493. end;
  1494. A_STM:
  1495. begin
  1496. {
  1497. change
  1498. stmfd r13!,[r14]
  1499. sub r13,r13,#4
  1500. bl abc
  1501. add r13,r13,#4
  1502. ldmfd r13!,[r15]
  1503. into
  1504. b abc
  1505. }
  1506. if not(ts_thumb_interworking in current_settings.targetswitches) and
  1507. MatchInstruction(p, A_STM, [C_None], [PF_FD]) and
  1508. GetNextInstruction(p, hp1) and
  1509. GetNextInstruction(hp1, hp2) and
  1510. SkipEntryExitMarker(hp2, hp2) and
  1511. GetNextInstruction(hp2, hp3) and
  1512. SkipEntryExitMarker(hp3, hp3) and
  1513. GetNextInstruction(hp3, hp4) and
  1514. (taicpu(p).oper[0]^.typ = top_ref) and
  1515. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1516. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  1517. (taicpu(p).oper[0]^.ref^.offset=0) and
  1518. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1519. (taicpu(p).oper[1]^.typ = top_regset) and
  1520. (taicpu(p).oper[1]^.regset^ = [RS_R14]) and
  1521. MatchInstruction(hp1, A_SUB, [C_None], [PF_NONE]) and
  1522. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1523. (taicpu(hp1).oper[0]^.reg = NR_STACK_POINTER_REG) and
  1524. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) and
  1525. (taicpu(hp1).oper[2]^.typ = top_const) and
  1526. MatchInstruction(hp3, A_ADD, [C_None], [PF_NONE]) and
  1527. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[0]^) and
  1528. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[1]^) and
  1529. MatchOperand(taicpu(hp1).oper[2]^,taicpu(hp3).oper[2]^) and
  1530. MatchInstruction(hp2, [A_BL,A_BLX], [C_None], [PF_NONE]) and
  1531. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1532. MatchInstruction(hp4, A_LDM, [C_None], [PF_FD]) and
  1533. MatchOperand(taicpu(p).oper[0]^,taicpu(hp4).oper[0]^) and
  1534. (taicpu(hp4).oper[1]^.typ = top_regset) and
  1535. (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
  1536. begin
  1537. asml.Remove(p);
  1538. asml.Remove(hp1);
  1539. asml.Remove(hp3);
  1540. asml.Remove(hp4);
  1541. taicpu(hp2).opcode:=A_B;
  1542. p.free;
  1543. hp1.free;
  1544. hp3.free;
  1545. hp4.free;
  1546. p:=hp2;
  1547. DebugMsg('Peephole Bl2B done', p);
  1548. end;
  1549. end;
  1550. end;
  1551. end;
  1552. end;
  1553. end;
  1554. { instructions modifying the CPSR can be only the last instruction }
  1555. function MustBeLast(p : tai) : boolean;
  1556. begin
  1557. Result:=(p.typ=ait_instruction) and
  1558. ((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
  1559. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  1560. (taicpu(p).oppostfix=PF_S));
  1561. end;
  1562. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  1563. var
  1564. p,hp1,hp2: tai;
  1565. l : longint;
  1566. condition : tasmcond;
  1567. hp3: tai;
  1568. WasLast: boolean;
  1569. { UsedRegs, TmpUsedRegs: TRegSet; }
  1570. begin
  1571. p := BlockStart;
  1572. { UsedRegs := []; }
  1573. while (p <> BlockEnd) Do
  1574. begin
  1575. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  1576. case p.Typ Of
  1577. Ait_Instruction:
  1578. begin
  1579. case taicpu(p).opcode Of
  1580. A_B:
  1581. if (taicpu(p).condition<>C_None) and
  1582. not(current_settings.cputype in cpu_thumb) 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.