aoptcpu.pas 137 KB

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