aoptcpu.pas 124 KB

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