aoptcpu.pas 118 KB

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