aoptcpu.pas 134 KB

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