aoptcpu.pas 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001
  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,reg2,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. taicpu(p).loadref(2,taicpu(p).oper[1]^.ref^);
  624. taicpu(p).loadreg(1, taicpu(hp1).oper[0]^.reg);
  625. taicpu(p).ops:=3;
  626. asml.remove(hp1);
  627. hp1.free;
  628. result:=true;
  629. end;
  630. Result:=LookForPostindexedPattern(taicpu(p)) or Result;
  631. end;
  632. A_LDR:
  633. begin
  634. { change
  635. ldr reg1,ref
  636. ldr reg2,ref
  637. into ...
  638. }
  639. if (taicpu(p).oper[1]^.ref^.addressmode=AM_OFFSET) and
  640. GetNextInstruction(p,hp1) and
  641. { ldrd is not allowed here }
  642. MatchInstruction(hp1, A_LDR, [taicpu(p).condition, C_None], [taicpu(p).oppostfix,PF_None]-[PF_D]) then
  643. begin
  644. {
  645. ...
  646. ldr reg1,ref
  647. mov reg2,reg1
  648. }
  649. if (taicpu(p).oppostfix=taicpu(hp1).oppostfix) and
  650. RefsEqual(taicpu(p).oper[1]^.ref^,taicpu(hp1).oper[1]^.ref^) and
  651. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.index) and
  652. (taicpu(p).oper[0]^.reg<>taicpu(hp1).oper[1]^.ref^.base) and
  653. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) then
  654. begin
  655. if taicpu(hp1).oper[0]^.reg=taicpu(p).oper[0]^.reg then
  656. begin
  657. DebugMsg('Peephole LdrLdr2Ldr done', hp1);
  658. asml.remove(hp1);
  659. hp1.free;
  660. end
  661. else
  662. begin
  663. DebugMsg('Peephole LdrLdr2LdrMov done', hp1);
  664. taicpu(hp1).opcode:=A_MOV;
  665. taicpu(hp1).oppostfix:=PF_None;
  666. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  667. end;
  668. result := true;
  669. end
  670. {
  671. ...
  672. ldrd reg1,reg1+1,ref
  673. }
  674. else if (GenerateARMCode or GenerateThumb2Code) and
  675. (CPUARM_HAS_EDSP in cpu_capabilities[current_settings.cputype]) and
  676. { ldrd does not allow any postfixes ... }
  677. (taicpu(p).oppostfix=PF_None) and
  678. not(odd(getsupreg(taicpu(p).oper[0]^.reg))) and
  679. (getsupreg(taicpu(p).oper[0]^.reg)+1=getsupreg(taicpu(hp1).oper[0]^.reg)) and
  680. { ldr ensures that either base or index contain no register, else ldr wouldn't
  681. use an offset either
  682. }
  683. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  684. (taicpu(p).oper[1]^.ref^.index=taicpu(hp1).oper[1]^.ref^.index) and
  685. (taicpu(p).oper[1]^.ref^.offset+4=taicpu(hp1).oper[1]^.ref^.offset) and
  686. (abs(taicpu(p).oper[1]^.ref^.offset)<256) and
  687. AlignedToQWord(taicpu(p).oper[1]^.ref^) then
  688. begin
  689. DebugMsg('Peephole LdrLdr2Ldrd done', p);
  690. taicpu(p).loadref(2,taicpu(p).oper[1]^.ref^);
  691. taicpu(p).loadreg(1, taicpu(hp1).oper[0]^.reg);
  692. taicpu(p).ops:=3;
  693. taicpu(p).oppostfix:=PF_D;
  694. asml.remove(hp1);
  695. hp1.free;
  696. result:=true;
  697. end;
  698. end;
  699. {
  700. Change
  701. ldrb dst1, [REF]
  702. and dst2, dst1, #255
  703. into
  704. ldrb dst2, [ref]
  705. }
  706. if not(GenerateThumbCode) and
  707. (taicpu(p).oppostfix=PF_B) and
  708. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  709. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_NONE]) and
  710. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[0]^.reg) and
  711. (taicpu(hp1).oper[2]^.typ = top_const) and
  712. (taicpu(hp1).oper[2]^.val = $FF) and
  713. not(RegUsedBetween(taicpu(hp1).oper[0]^.reg, p, hp1)) and
  714. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  715. begin
  716. DebugMsg('Peephole LdrbAnd2Ldrb done', p);
  717. taicpu(p).oper[0]^.reg := taicpu(hp1).oper[0]^.reg;
  718. asml.remove(hp1);
  719. hp1.free;
  720. result:=true;
  721. end;
  722. Result:=LookForPostindexedPattern(taicpu(p)) or Result;
  723. { Remove superfluous mov after ldr
  724. changes
  725. ldr reg1, ref
  726. mov reg2, reg1
  727. to
  728. ldr reg2, ref
  729. conditions are:
  730. * no ldrd usage
  731. * reg1 must be released after mov
  732. * mov can not contain shifterops
  733. * ldr+mov have the same conditions
  734. * mov does not set flags
  735. }
  736. if (taicpu(p).oppostfix<>PF_D) and
  737. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  738. RemoveSuperfluousMove(p, hp1, 'LdrMov2Ldr') then
  739. Result:=true;
  740. end;
  741. A_MOV:
  742. begin
  743. { fold
  744. mov reg1,reg0, shift imm1
  745. mov reg1,reg1, shift imm2
  746. }
  747. if (taicpu(p).ops=3) and
  748. (taicpu(p).oper[2]^.typ = top_shifterop) and
  749. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  750. getnextinstruction(p,hp1) and
  751. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  752. (taicpu(hp1).ops=3) and
  753. MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[0]^.reg) and
  754. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  755. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  756. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) then
  757. begin
  758. { fold
  759. mov reg1,reg0, lsl 16
  760. mov reg1,reg1, lsr 16
  761. strh reg1, ...
  762. dealloc reg1
  763. to
  764. strh reg1, ...
  765. dealloc reg1
  766. }
  767. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  768. (taicpu(p).oper[2]^.shifterop^.shiftimm=16) and
  769. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ASR]) and
  770. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=16) and
  771. getnextinstruction(hp1,hp2) and
  772. MatchInstruction(hp2, A_STR, [taicpu(p).condition], [PF_H]) and
  773. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^.reg) then
  774. begin
  775. CopyUsedRegs(TmpUsedRegs);
  776. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  777. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  778. if not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg,hp2,TmpUsedRegs)) then
  779. begin
  780. DebugMsg('Peephole optimizer removed superfluous 16 Bit zero extension', hp1);
  781. taicpu(hp2).loadreg(0,taicpu(p).oper[1]^.reg);
  782. asml.remove(p);
  783. asml.remove(hp1);
  784. p.free;
  785. hp1.free;
  786. p:=hp2;
  787. Result:=true;
  788. end;
  789. ReleaseUsedRegs(TmpUsedRegs);
  790. end
  791. { fold
  792. mov reg1,reg0, shift imm1
  793. mov reg1,reg1, shift imm2
  794. to
  795. mov reg1,reg0, shift imm1+imm2
  796. }
  797. else if (taicpu(p).oper[2]^.shifterop^.shiftmode=taicpu(hp1).oper[2]^.shifterop^.shiftmode) or
  798. { asr makes no use after a lsr, the asr can be foled into the lsr }
  799. ((taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSR) and (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_ASR) ) then
  800. begin
  801. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  802. { avoid overflows }
  803. if taicpu(p).oper[2]^.shifterop^.shiftimm>31 then
  804. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  805. SM_ROR:
  806. taicpu(p).oper[2]^.shifterop^.shiftimm:=taicpu(p).oper[2]^.shifterop^.shiftimm and 31;
  807. SM_ASR:
  808. taicpu(p).oper[2]^.shifterop^.shiftimm:=31;
  809. SM_LSR,
  810. SM_LSL:
  811. begin
  812. hp2:=taicpu.op_reg_const(A_MOV,taicpu(p).oper[0]^.reg,0);
  813. InsertLLItem(p.previous, p.next, hp2);
  814. p.free;
  815. p:=hp2;
  816. end;
  817. else
  818. internalerror(2008072803);
  819. end;
  820. DebugMsg('Peephole ShiftShift2Shift 1 done', p);
  821. asml.remove(hp1);
  822. hp1.free;
  823. result := true;
  824. end
  825. { fold
  826. mov reg1,reg0, shift imm1
  827. mov reg1,reg1, shift imm2
  828. mov reg1,reg1, shift imm3 ...
  829. mov reg2,reg1, shift imm3 ...
  830. }
  831. else if GetNextInstructionUsingReg(hp1,hp2, taicpu(hp1).oper[0]^.reg) and
  832. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  833. (taicpu(hp2).ops=3) and
  834. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  835. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp2)) and
  836. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  837. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) then
  838. begin
  839. { mov reg1,reg0, lsl imm1
  840. mov reg1,reg1, lsr/asr imm2
  841. mov reg2,reg1, lsl imm3 ...
  842. to
  843. mov reg1,reg0, lsl imm1
  844. mov reg2,reg1, lsr/asr imm2-imm3
  845. if
  846. imm1>=imm2
  847. }
  848. if (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL) and (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  849. (taicpu(hp1).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  850. (taicpu(p).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  851. begin
  852. if (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(hp1).oper[2]^.shifterop^.shiftimm) then
  853. begin
  854. if not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,p,hp1)) and
  855. not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2)) then
  856. begin
  857. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1a done', p);
  858. inc(taicpu(p).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm-taicpu(hp1).oper[2]^.shifterop^.shiftimm);
  859. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  860. asml.remove(hp1);
  861. asml.remove(hp2);
  862. hp1.free;
  863. hp2.free;
  864. if taicpu(p).oper[2]^.shifterop^.shiftimm>=32 then
  865. begin
  866. taicpu(p).freeop(1);
  867. taicpu(p).freeop(2);
  868. taicpu(p).loadconst(1,0);
  869. end;
  870. result := true;
  871. end;
  872. end
  873. else if not(RegUsedBetween(taicpu(hp2).oper[0]^.reg,hp1,hp2)) then
  874. begin
  875. DebugMsg('Peephole ShiftShiftShift2ShiftShift 1b done', p);
  876. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(hp2).oper[2]^.shifterop^.shiftimm);
  877. taicpu(hp1).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  878. asml.remove(hp2);
  879. hp2.free;
  880. result := true;
  881. end;
  882. end
  883. { mov reg1,reg0, lsr/asr imm1
  884. mov reg1,reg1, lsl imm2
  885. mov reg1,reg1, lsr/asr imm3 ...
  886. if imm3>=imm1 and imm2>=imm1
  887. to
  888. mov reg1,reg0, lsl imm2-imm1
  889. mov reg1,reg1, lsr/asr imm3 ...
  890. }
  891. 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
  892. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  893. (taicpu(hp2).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) and
  894. (taicpu(hp1).oper[2]^.shifterop^.shiftimm>=taicpu(p).oper[2]^.shifterop^.shiftimm) then
  895. begin
  896. dec(taicpu(hp1).oper[2]^.shifterop^.shiftimm,taicpu(p).oper[2]^.shifterop^.shiftimm);
  897. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  898. DebugMsg('Peephole ShiftShiftShift2ShiftShift 2 done', p);
  899. asml.remove(p);
  900. p.free;
  901. p:=hp2;
  902. if taicpu(hp1).oper[2]^.shifterop^.shiftimm=0 then
  903. begin
  904. taicpu(hp2).oper[1]^.reg:=taicpu(hp1).oper[1]^.reg;
  905. asml.remove(hp1);
  906. hp1.free;
  907. p:=hp2;
  908. end;
  909. result := true;
  910. end;
  911. end;
  912. end;
  913. { Change the common
  914. mov r0, r0, lsr #xxx
  915. and r0, r0, #yyy/bic r0, r0, #xxx
  916. and remove the superfluous and/bic if possible
  917. This could be extended to handle more cases.
  918. }
  919. if (taicpu(p).ops=3) and
  920. (taicpu(p).oper[2]^.typ = top_shifterop) and
  921. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  922. (taicpu(p).oper[2]^.shifterop^.shiftmode = SM_LSR) and
  923. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  924. (hp1.typ=ait_instruction) and
  925. (taicpu(hp1).ops>=1) and
  926. (taicpu(hp1).oper[0]^.typ=top_reg) and
  927. (not RegModifiedBetween(taicpu(hp1).oper[0]^.reg, p, hp1)) and
  928. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  929. begin
  930. if (taicpu(p).oper[2]^.shifterop^.shiftimm >= 24 ) and
  931. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  932. (taicpu(hp1).ops=3) and
  933. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  934. (taicpu(hp1).oper[2]^.typ = top_const) and
  935. { Check if the AND actually would only mask out bits being already zero because of the shift
  936. }
  937. ((($ffffffff shr taicpu(p).oper[2]^.shifterop^.shiftimm) and taicpu(hp1).oper[2]^.val) =
  938. ($ffffffff shr taicpu(p).oper[2]^.shifterop^.shiftimm)) then
  939. begin
  940. DebugMsg('Peephole LsrAnd2Lsr done', hp1);
  941. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  942. asml.remove(hp1);
  943. hp1.free;
  944. result:=true;
  945. end
  946. else if MatchInstruction(hp1, A_BIC, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  947. (taicpu(hp1).ops=3) and
  948. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  949. (taicpu(hp1).oper[2]^.typ = top_const) and
  950. { Check if the BIC actually would only mask out bits beeing already zero because of the shift }
  951. (taicpu(hp1).oper[2]^.val<>0) and
  952. (BsfDWord(taicpu(hp1).oper[2]^.val)>=32-taicpu(p).oper[2]^.shifterop^.shiftimm) then
  953. begin
  954. DebugMsg('Peephole LsrBic2Lsr done', hp1);
  955. taicpu(p).oper[0]^.reg:=taicpu(hp1).oper[0]^.reg;
  956. asml.remove(hp1);
  957. hp1.free;
  958. result:=true;
  959. end;
  960. end;
  961. { Change
  962. mov rx, ry, lsr/ror #xxx
  963. uxtb/uxth rz,rx/and rz,rx,0xFF
  964. dealloc rx
  965. to
  966. uxtb/uxth rz,ry,ror #xxx
  967. }
  968. if (taicpu(p).ops=3) and
  969. (taicpu(p).oper[2]^.typ = top_shifterop) and
  970. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) and
  971. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSR,SM_ROR]) and
  972. (GenerateThumb2Code) and
  973. GetNextInstructionUsingReg(p,hp1, taicpu(p).oper[0]^.reg) and
  974. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  975. begin
  976. if MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  977. (taicpu(hp1).ops = 2) and
  978. (taicpu(p).oper[2]^.shifterop^.shiftimm in [8,16,24]) and
  979. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  980. begin
  981. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  982. taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
  983. taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
  984. taicpu(hp1).ops := 3;
  985. GetNextInstruction(p,hp1);
  986. asml.Remove(p);
  987. p.Free;
  988. p:=hp1;
  989. result:=true;
  990. exit;
  991. end
  992. else if MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  993. (taicpu(hp1).ops=2) and
  994. (taicpu(p).oper[2]^.shifterop^.shiftimm in [16]) and
  995. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  996. begin
  997. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  998. taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
  999. taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
  1000. taicpu(hp1).ops := 3;
  1001. GetNextInstruction(p,hp1);
  1002. asml.Remove(p);
  1003. p.Free;
  1004. p:=hp1;
  1005. result:=true;
  1006. exit;
  1007. end
  1008. else if MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1009. (taicpu(hp1).ops = 3) and
  1010. (taicpu(hp1).oper[2]^.typ = top_const) and
  1011. (taicpu(hp1).oper[2]^.val = $FF) and
  1012. (taicpu(p).oper[2]^.shifterop^.shiftimm in [8,16,24]) and
  1013. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1014. begin
  1015. taicpu(hp1).ops := 3;
  1016. taicpu(hp1).opcode := A_UXTB;
  1017. taicpu(hp1).oper[1]^.reg := taicpu(p).oper[1]^.reg;
  1018. taicpu(hp1).loadshifterop(2,taicpu(p).oper[2]^.shifterop^);
  1019. taicpu(hp1).oper[2]^.shifterop^.shiftmode:=SM_ROR;
  1020. GetNextInstruction(p,hp1);
  1021. asml.Remove(p);
  1022. p.Free;
  1023. p:=hp1;
  1024. result:=true;
  1025. exit;
  1026. end;
  1027. end;
  1028. {
  1029. optimize
  1030. mov rX, yyyy
  1031. ....
  1032. }
  1033. if (taicpu(p).ops = 2) and
  1034. GetNextInstruction(p,hp1) and
  1035. (tai(hp1).typ = ait_instruction) then
  1036. begin
  1037. {
  1038. This changes the very common
  1039. mov r0, #0
  1040. str r0, [...]
  1041. mov r0, #0
  1042. str r0, [...]
  1043. and removes all superfluous mov instructions
  1044. }
  1045. if (taicpu(p).oper[1]^.typ = top_const) and
  1046. (taicpu(hp1).opcode=A_STR) then
  1047. while MatchInstruction(hp1, A_STR, [taicpu(p).condition], []) and
  1048. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  1049. GetNextInstruction(hp1, hp2) and
  1050. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1051. (taicpu(hp2).ops = 2) and
  1052. MatchOperand(taicpu(hp2).oper[0]^, taicpu(p).oper[0]^) and
  1053. MatchOperand(taicpu(hp2).oper[1]^, taicpu(p).oper[1]^) do
  1054. begin
  1055. DebugMsg('Peephole MovStrMov done', hp2);
  1056. GetNextInstruction(hp2,hp1);
  1057. asml.remove(hp2);
  1058. hp2.free;
  1059. result:=true;
  1060. if not assigned(hp1) then break;
  1061. end
  1062. {
  1063. This removes the first mov from
  1064. mov rX,...
  1065. mov rX,...
  1066. }
  1067. else if taicpu(hp1).opcode=A_MOV then
  1068. while MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [taicpu(p).oppostfix]) and
  1069. (taicpu(hp1).ops = 2) and
  1070. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^) and
  1071. { don't remove the first mov if the second is a mov rX,rX }
  1072. not(MatchOperand(taicpu(hp1).oper[0]^, taicpu(hp1).oper[1]^)) do
  1073. begin
  1074. DebugMsg('Peephole MovMov done', p);
  1075. asml.remove(p);
  1076. p.free;
  1077. p:=hp1;
  1078. GetNextInstruction(hp1,hp1);
  1079. result:=true;
  1080. if not assigned(hp1) then
  1081. break;
  1082. end;
  1083. end;
  1084. {
  1085. change
  1086. mov r1, r0
  1087. add r1, r1, #1
  1088. to
  1089. add r1, r0, #1
  1090. Todo: Make it work for mov+cmp too
  1091. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1092. }
  1093. if (taicpu(p).ops = 2) and
  1094. (taicpu(p).oper[1]^.typ = top_reg) and
  1095. (taicpu(p).oppostfix = PF_NONE) and
  1096. GetNextInstruction(p, hp1) and
  1097. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  1098. A_AND, A_BIC, A_EOR, A_ORR, A_MOV, A_MVN],
  1099. [taicpu(p).condition], []) and
  1100. {MOV and MVN might only have 2 ops}
  1101. (taicpu(hp1).ops >= 2) and
  1102. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg) and
  1103. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1104. (
  1105. (taicpu(hp1).ops = 2) or
  1106. (taicpu(hp1).oper[2]^.typ in [top_reg, top_const, top_shifterop])
  1107. ) then
  1108. begin
  1109. { When we get here we still don't know if the registers match}
  1110. for I:=1 to 2 do
  1111. {
  1112. If the first loop was successful p will be replaced with hp1.
  1113. The checks will still be ok, because all required information
  1114. will also be in hp1 then.
  1115. }
  1116. if (taicpu(hp1).ops > I) and
  1117. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) and
  1118. { prevent certain combinations on thumb(2), this is only a safe approximation }
  1119. (not(GenerateThumbCode or GenerateThumb2Code) or
  1120. ((getsupreg(taicpu(p).oper[1]^.reg)<>RS_R13) and
  1121. (getsupreg(taicpu(p).oper[1]^.reg)<>RS_R15))
  1122. ) then
  1123. begin
  1124. DebugMsg('Peephole RedundantMovProcess done', hp1);
  1125. taicpu(hp1).oper[I]^.reg := taicpu(p).oper[1]^.reg;
  1126. if p<>hp1 then
  1127. begin
  1128. asml.remove(p);
  1129. p.free;
  1130. p:=hp1;
  1131. Result:=true;
  1132. end;
  1133. end;
  1134. end;
  1135. { Fold the very common sequence
  1136. mov regA, regB
  1137. ldr* regA, [regA]
  1138. to
  1139. ldr* regA, [regB]
  1140. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1141. }
  1142. if (taicpu(p).opcode = A_MOV) and
  1143. (taicpu(p).ops = 2) and
  1144. (taicpu(p).oper[1]^.typ = top_reg) and
  1145. (taicpu(p).oppostfix = PF_NONE) and
  1146. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1147. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], []) and
  1148. { We can change the base register only when the instruction uses AM_OFFSET }
  1149. ((taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) or
  1150. ((taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) and
  1151. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg))
  1152. ) and
  1153. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1154. // Make sure that Thumb code doesn't propagate a high register into a reference
  1155. ((GenerateThumbCode and
  1156. (getsupreg(taicpu(p).oper[1]^.reg) < RS_R8)) or
  1157. (not GenerateThumbCode)) and
  1158. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1159. begin
  1160. DebugMsg('Peephole MovLdr2Ldr done', hp1);
  1161. if (taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) and
  1162. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1163. taicpu(hp1).oper[1]^.ref^.base := taicpu(p).oper[1]^.reg;
  1164. if taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg then
  1165. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1166. dealloc:=FindRegDeAlloc(taicpu(p).oper[1]^.reg, taicpu(p.Next));
  1167. if Assigned(dealloc) then
  1168. begin
  1169. asml.remove(dealloc);
  1170. asml.InsertAfter(dealloc,hp1);
  1171. end;
  1172. GetNextInstruction(p, hp1);
  1173. asml.remove(p);
  1174. p.free;
  1175. p:=hp1;
  1176. result:=true;
  1177. end;
  1178. { This folds shifterops into following instructions
  1179. mov r0, r1, lsl #8
  1180. add r2, r3, r0
  1181. to
  1182. add r2, r3, r1, lsl #8
  1183. CAUTION! If this one is successful p might not be a mov instruction anymore!
  1184. }
  1185. if (taicpu(p).opcode = A_MOV) and
  1186. (taicpu(p).ops = 3) and
  1187. (taicpu(p).oper[1]^.typ = top_reg) and
  1188. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1189. (taicpu(p).oppostfix = PF_NONE) and
  1190. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1191. MatchInstruction(hp1, [A_ADD, A_ADC, A_RSB, A_RSC, A_SUB, A_SBC,
  1192. A_AND, A_BIC, A_EOR, A_ORR, A_TEQ, A_TST,
  1193. A_CMP, A_CMN],
  1194. [taicpu(p).condition], [PF_None]) and
  1195. (not ((GenerateThumb2Code) and
  1196. (taicpu(hp1).opcode in [A_SBC]) and
  1197. (((taicpu(hp1).ops=3) and
  1198. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^.reg)) or
  1199. ((taicpu(hp1).ops=2) and
  1200. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[0]^.reg))))) and
  1201. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) and
  1202. (taicpu(hp1).ops >= 2) and
  1203. {Currently we can't fold into another shifterop}
  1204. (taicpu(hp1).oper[taicpu(hp1).ops-1]^.typ = top_reg) and
  1205. {Folding rrx is problematic because of the C-Flag, as we currently can't check
  1206. NR_DEFAULTFLAGS for modification}
  1207. (
  1208. {Everything is fine if we don't use RRX}
  1209. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) or
  1210. (
  1211. {If it is RRX, then check if we're just accessing the next instruction}
  1212. GetNextInstruction(p, hp2) and
  1213. (hp1 = hp2)
  1214. )
  1215. ) and
  1216. { reg1 might not be modified inbetween }
  1217. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1218. { The shifterop can contain a register, might not be modified}
  1219. (
  1220. (taicpu(p).oper[2]^.shifterop^.rs = NR_NO) or
  1221. not(RegModifiedBetween(taicpu(p).oper[2]^.shifterop^.rs, p, hp1))
  1222. ) and
  1223. (
  1224. {Only ONE of the two src operands is allowed to match}
  1225. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-2]^) xor
  1226. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[taicpu(hp1).ops-1]^)
  1227. ) then
  1228. begin
  1229. if taicpu(hp1).opcode in [A_TST, A_TEQ, A_CMN] then
  1230. I2:=0
  1231. else
  1232. I2:=1;
  1233. for I:=I2 to taicpu(hp1).ops-1 do
  1234. if MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[I]^.reg) then
  1235. begin
  1236. { If the parameter matched on the second op from the RIGHT
  1237. we have to switch the parameters, this will not happen for CMP
  1238. were we're only evaluating the most right parameter
  1239. }
  1240. if I <> taicpu(hp1).ops-1 then
  1241. begin
  1242. {The SUB operators need to be changed when we swap parameters}
  1243. case taicpu(hp1).opcode of
  1244. A_SUB: tempop:=A_RSB;
  1245. A_SBC: tempop:=A_RSC;
  1246. A_RSB: tempop:=A_SUB;
  1247. A_RSC: tempop:=A_SBC;
  1248. else tempop:=taicpu(hp1).opcode;
  1249. end;
  1250. if taicpu(hp1).ops = 3 then
  1251. hp2:=taicpu.op_reg_reg_reg_shifterop(tempop,
  1252. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[2]^.reg,
  1253. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1254. else
  1255. hp2:=taicpu.op_reg_reg_shifterop(tempop,
  1256. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1257. taicpu(p).oper[2]^.shifterop^);
  1258. end
  1259. else
  1260. if taicpu(hp1).ops = 3 then
  1261. hp2:=taicpu.op_reg_reg_reg_shifterop(taicpu(hp1).opcode,
  1262. taicpu(hp1).oper[0]^.reg, taicpu(hp1).oper[1]^.reg,
  1263. taicpu(p).oper[1]^.reg, taicpu(p).oper[2]^.shifterop^)
  1264. else
  1265. hp2:=taicpu.op_reg_reg_shifterop(taicpu(hp1).opcode,
  1266. taicpu(hp1).oper[0]^.reg, taicpu(p).oper[1]^.reg,
  1267. taicpu(p).oper[2]^.shifterop^);
  1268. asml.insertbefore(hp2, hp1);
  1269. GetNextInstruction(p, hp2);
  1270. asml.remove(p);
  1271. asml.remove(hp1);
  1272. p.free;
  1273. hp1.free;
  1274. p:=hp2;
  1275. DebugMsg('Peephole FoldShiftProcess done', p);
  1276. Result:=true;
  1277. break;
  1278. end;
  1279. end;
  1280. {
  1281. Fold
  1282. mov r1, r1, lsl #2
  1283. ldr/ldrb r0, [r0, r1]
  1284. to
  1285. ldr/ldrb r0, [r0, r1, lsl #2]
  1286. XXX: This still needs some work, as we quite often encounter something like
  1287. mov r1, r2, lsl #2
  1288. add r2, r3, #imm
  1289. ldr r0, [r2, r1]
  1290. which can't be folded because r2 is overwritten between the shift and the ldr.
  1291. We could try to shuffle the registers around and fold it into.
  1292. add r1, r3, #imm
  1293. ldr r0, [r1, r2, lsl #2]
  1294. }
  1295. if (not(GenerateThumbCode)) and
  1296. (taicpu(p).opcode = A_MOV) and
  1297. (taicpu(p).ops = 3) and
  1298. (taicpu(p).oper[1]^.typ = top_reg) and
  1299. (taicpu(p).oper[2]^.typ = top_shifterop) and
  1300. { RRX is tough to handle, because it requires tracking the C-Flag,
  1301. it is also extremly unlikely to be emitted this way}
  1302. (taicpu(p).oper[2]^.shifterop^.shiftmode <> SM_RRX) and
  1303. (taicpu(p).oper[2]^.shifterop^.shiftimm <> 0) and
  1304. { thumb2 allows only lsl #0..#3 }
  1305. (not(GenerateThumb2Code) or
  1306. ((taicpu(p).oper[2]^.shifterop^.shiftimm in [0..3]) and
  1307. (taicpu(p).oper[2]^.shifterop^.shiftmode=SM_LSL)
  1308. )
  1309. ) and
  1310. (taicpu(p).oppostfix = PF_NONE) and
  1311. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1312. {Only LDR, LDRB, STR, STRB can handle scaled register indexing}
  1313. (MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], [PF_None, PF_B]) or
  1314. (GenerateThumb2Code and
  1315. MatchInstruction(hp1, [A_LDR, A_STR], [taicpu(p).condition], [PF_None, PF_B, PF_SB, PF_H, PF_SH]))
  1316. ) and
  1317. (
  1318. {If this is address by offset, one of the two registers can be used}
  1319. ((taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1320. (
  1321. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) xor
  1322. (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg)
  1323. )
  1324. ) or
  1325. {For post and preindexed only the index register can be used}
  1326. ((taicpu(hp1).oper[1]^.ref^.addressmode in [AM_POSTINDEXED, AM_PREINDEXED]) and
  1327. (
  1328. (taicpu(hp1).oper[1]^.ref^.index = taicpu(p).oper[0]^.reg) and
  1329. (taicpu(hp1).oper[1]^.ref^.base <> taicpu(p).oper[0]^.reg)
  1330. ) and
  1331. (not GenerateThumb2Code)
  1332. )
  1333. ) and
  1334. { Only fold if there isn't another shifterop already, and offset is zero. }
  1335. (taicpu(hp1).oper[1]^.ref^.offset = 0) and
  1336. (taicpu(hp1).oper[1]^.ref^.shiftmode = SM_None) and
  1337. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) and
  1338. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  1339. begin
  1340. { If the register we want to do the shift for resides in base, we need to swap that}
  1341. if (taicpu(hp1).oper[1]^.ref^.base = taicpu(p).oper[0]^.reg) then
  1342. taicpu(hp1).oper[1]^.ref^.base := taicpu(hp1).oper[1]^.ref^.index;
  1343. taicpu(hp1).oper[1]^.ref^.index := taicpu(p).oper[1]^.reg;
  1344. taicpu(hp1).oper[1]^.ref^.shiftmode := taicpu(p).oper[2]^.shifterop^.shiftmode;
  1345. taicpu(hp1).oper[1]^.ref^.shiftimm := taicpu(p).oper[2]^.shifterop^.shiftimm;
  1346. DebugMsg('Peephole FoldShiftLdrStr done', hp1);
  1347. GetNextInstruction(p, hp1);
  1348. asml.remove(p);
  1349. p.free;
  1350. p:=hp1;
  1351. Result:=true;
  1352. end;
  1353. {
  1354. Often we see shifts and then a superfluous mov to another register
  1355. In the future this might be handled in RedundantMovProcess when it uses RegisterTracking
  1356. }
  1357. if (taicpu(p).opcode = A_MOV) and
  1358. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1359. RemoveSuperfluousMove(p, hp1, 'MovMov2Mov') then
  1360. Result:=true;
  1361. end;
  1362. A_ADD,
  1363. A_ADC,
  1364. A_RSB,
  1365. A_RSC,
  1366. A_SUB,
  1367. A_SBC,
  1368. A_AND,
  1369. A_BIC,
  1370. A_EOR,
  1371. A_ORR,
  1372. A_MLA,
  1373. A_MLS,
  1374. A_MUL:
  1375. begin
  1376. {
  1377. optimize
  1378. and reg2,reg1,const1
  1379. ...
  1380. }
  1381. if (taicpu(p).opcode = A_AND) and
  1382. (taicpu(p).ops>2) and
  1383. (taicpu(p).oper[1]^.typ = top_reg) and
  1384. (taicpu(p).oper[2]^.typ = top_const) then
  1385. begin
  1386. {
  1387. change
  1388. and reg2,reg1,const1
  1389. ...
  1390. and reg3,reg2,const2
  1391. to
  1392. and reg3,reg1,(const1 and const2)
  1393. }
  1394. if GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1395. MatchInstruction(hp1, A_AND, [taicpu(p).condition], [PF_None]) and
  1396. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1397. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1398. (taicpu(hp1).oper[2]^.typ = top_const) then
  1399. begin
  1400. if not(RegUsedBetween(taicpu(hp1).oper[0]^.reg,p,hp1)) then
  1401. begin
  1402. DebugMsg('Peephole AndAnd2And done', p);
  1403. taicpu(p).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1404. taicpu(p).oppostfix:=taicpu(hp1).oppostfix;
  1405. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1406. asml.remove(hp1);
  1407. hp1.free;
  1408. Result:=true;
  1409. end
  1410. else if not(RegUsedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1411. begin
  1412. DebugMsg('Peephole AndAnd2And done', hp1);
  1413. taicpu(hp1).loadConst(2,taicpu(p).oper[2]^.val and taicpu(hp1).oper[2]^.val);
  1414. taicpu(hp1).oppostfix:=taicpu(p).oppostfix;
  1415. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1416. GetNextInstruction(p, hp1);
  1417. asml.remove(p);
  1418. p.free;
  1419. p:=hp1;
  1420. Result:=true;
  1421. end;
  1422. end
  1423. {
  1424. change
  1425. and reg2,reg1,$xxxxxxFF
  1426. strb reg2,[...]
  1427. dealloc reg2
  1428. to
  1429. strb reg1,[...]
  1430. }
  1431. else if ((taicpu(p).oper[2]^.val and $FF) = $FF) and
  1432. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1433. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1434. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1435. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1436. { the reference in strb might not use reg2 }
  1437. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1438. { reg1 might not be modified inbetween }
  1439. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1440. begin
  1441. DebugMsg('Peephole AndStrb2Strb done', p);
  1442. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1443. GetNextInstruction(p, hp1);
  1444. asml.remove(p);
  1445. p.free;
  1446. p:=hp1;
  1447. result:=true;
  1448. end
  1449. {
  1450. change
  1451. and reg2,reg1,255
  1452. uxtb/uxth reg3,reg2
  1453. dealloc reg2
  1454. to
  1455. and reg3,reg1,x
  1456. }
  1457. else if (taicpu(p).oper[2]^.val = $FF) and
  1458. MatchInstruction(p, A_AND, [C_None], [PF_None]) and
  1459. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1460. MatchInstruction(hp1, [A_UXTB,A_UXTH], [C_None], [PF_None]) and
  1461. (taicpu(hp1).ops = 2) and
  1462. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1463. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1464. { reg1 might not be modified inbetween }
  1465. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1466. begin
  1467. DebugMsg('Peephole AndUxt2And done', p);
  1468. taicpu(hp1).opcode:=A_AND;
  1469. taicpu(hp1).ops:=3;
  1470. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1471. taicpu(hp1).loadconst(2,255);
  1472. GetNextInstruction(p,hp1);
  1473. asml.remove(p);
  1474. p.Free;
  1475. p:=hp1;
  1476. result:=true;
  1477. end
  1478. {
  1479. from
  1480. and reg1,reg0,2^n-1
  1481. mov reg2,reg1, lsl imm1
  1482. (mov reg3,reg2, lsr/asr imm1)
  1483. remove either the and or the lsl/xsr sequence if possible
  1484. }
  1485. else if cutils.ispowerof2(taicpu(p).oper[2]^.val+1,i) and
  1486. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1487. MatchInstruction(hp1, A_MOV, [taicpu(p).condition], [PF_None]) and
  1488. (taicpu(hp1).ops=3) and
  1489. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1490. (taicpu(hp1).oper[2]^.typ = top_shifterop) and
  1491. (taicpu(hp1).oper[2]^.shifterop^.rs = NR_NO) and
  1492. (taicpu(hp1).oper[2]^.shifterop^.shiftmode=SM_LSL) and
  1493. RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) then
  1494. begin
  1495. {
  1496. and reg1,reg0,2^n-1
  1497. mov reg2,reg1, lsl imm1
  1498. mov reg3,reg2, lsr/asr imm1
  1499. =>
  1500. and reg1,reg0,2^n-1
  1501. if lsr and 2^n-1>=imm1 or asr and 2^n-1>imm1
  1502. }
  1503. if GetNextInstructionUsingReg(hp1,hp2,taicpu(p).oper[0]^.reg) and
  1504. MatchInstruction(hp2, A_MOV, [taicpu(p).condition], [PF_None]) and
  1505. (taicpu(hp2).ops=3) and
  1506. MatchOperand(taicpu(hp2).oper[1]^, taicpu(hp1).oper[0]^.reg) and
  1507. (taicpu(hp2).oper[2]^.typ = top_shifterop) and
  1508. (taicpu(hp2).oper[2]^.shifterop^.rs = NR_NO) and
  1509. (taicpu(hp2).oper[2]^.shifterop^.shiftmode in [SM_ASR,SM_LSR]) and
  1510. (taicpu(hp1).oper[2]^.shifterop^.shiftimm=taicpu(hp2).oper[2]^.shifterop^.shiftimm) and
  1511. RegEndOfLife(taicpu(hp1).oper[0]^.reg,taicpu(hp2)) and
  1512. ((i<32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) or
  1513. ((i=32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1514. (taicpu(hp2).oper[2]^.shifterop^.shiftmode=SM_LSR))) then
  1515. begin
  1516. DebugMsg('Peephole AndLslXsr2And done', p);
  1517. taicpu(p).oper[0]^.reg:=taicpu(hp2).oper[0]^.reg;
  1518. asml.Remove(hp1);
  1519. asml.Remove(hp2);
  1520. hp1.free;
  1521. hp2.free;
  1522. result:=true;
  1523. end
  1524. {
  1525. and reg1,reg0,2^n-1
  1526. mov reg2,reg1, lsl imm1
  1527. =>
  1528. mov reg2,reg0, lsl imm1
  1529. if imm1>i
  1530. }
  1531. else if (i>32-taicpu(hp1).oper[2]^.shifterop^.shiftimm) and
  1532. not(RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) then
  1533. begin
  1534. DebugMsg('Peephole AndLsl2Lsl done', p);
  1535. taicpu(hp1).oper[1]^.reg:=taicpu(p).oper[1]^.reg;
  1536. GetNextInstruction(p, hp1);
  1537. asml.Remove(p);
  1538. p.free;
  1539. p:=hp1;
  1540. result:=true;
  1541. end
  1542. end;
  1543. end;
  1544. {
  1545. change
  1546. add/sub reg2,reg1,const1
  1547. str/ldr reg3,[reg2,const2]
  1548. dealloc reg2
  1549. to
  1550. str/ldr reg3,[reg1,const2+/-const1]
  1551. }
  1552. if (not GenerateThumbCode) and
  1553. (taicpu(p).opcode in [A_ADD,A_SUB]) and
  1554. (taicpu(p).ops>2) and
  1555. (taicpu(p).oper[1]^.typ = top_reg) and
  1556. (taicpu(p).oper[2]^.typ = top_const) then
  1557. begin
  1558. hp1:=p;
  1559. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) and
  1560. { we cannot check NR_DEFAULTFLAGS for modification yet so don't allow a condition }
  1561. MatchInstruction(hp1, [A_LDR, A_STR], [C_None], []) and
  1562. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  1563. { don't optimize if the register is stored/overwritten }
  1564. (taicpu(hp1).oper[0]^.reg<>taicpu(p).oper[1]^.reg) and
  1565. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  1566. (taicpu(hp1).oper[1]^.ref^.addressmode=AM_OFFSET) and
  1567. { new offset must be valid: either in the range of 8 or 12 bit, depend on the
  1568. ldr postfix }
  1569. (((taicpu(p).opcode=A_ADD) and
  1570. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset+taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1571. ) or
  1572. ((taicpu(p).opcode=A_SUB) and
  1573. isValidConstLoadStoreOffset(taicpu(hp1).oper[1]^.ref^.offset-taicpu(p).oper[2]^.val, taicpu(hp1).oppostfix)
  1574. )
  1575. ) do
  1576. begin
  1577. { neither reg1 nor reg2 might be changed inbetween }
  1578. if RegModifiedBetween(taicpu(p).oper[0]^.reg,p,hp1) or
  1579. RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1) then
  1580. break;
  1581. { reg2 must be either overwritten by the ldr or it is deallocated afterwards }
  1582. if ((taicpu(hp1).opcode=A_LDR) and (taicpu(p).oper[0]^.reg=taicpu(hp1).oper[0]^.reg)) or
  1583. assigned(FindRegDeAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) then
  1584. begin
  1585. { remember last instruction }
  1586. hp2:=hp1;
  1587. DebugMsg('Peephole Add/SubLdr2Ldr done', p);
  1588. hp1:=p;
  1589. { fix all ldr/str }
  1590. while GetNextInstructionUsingReg(hp1, hp1, taicpu(p).oper[0]^.reg) do
  1591. begin
  1592. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  1593. if taicpu(p).opcode=A_ADD then
  1594. inc(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val)
  1595. else
  1596. dec(taicpu(hp1).oper[1]^.ref^.offset,taicpu(p).oper[2]^.val);
  1597. if hp1=hp2 then
  1598. break;
  1599. end;
  1600. GetNextInstruction(p,hp1);
  1601. asml.remove(p);
  1602. p.free;
  1603. p:=hp1;
  1604. result:=true;
  1605. break;
  1606. end;
  1607. end;
  1608. end;
  1609. {
  1610. change
  1611. add reg1, ...
  1612. mov reg2, reg1
  1613. to
  1614. add reg2, ...
  1615. }
  1616. if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1617. (taicpu(p).ops>=3) and
  1618. RemoveSuperfluousMove(p, hp1, 'DataMov2Data') then
  1619. Result:=true;
  1620. if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  1621. LookForPreindexedPattern(taicpu(p)) then
  1622. begin
  1623. GetNextInstruction(p,hp1);
  1624. DebugMsg('Peephole Add/Sub to Preindexed done', p);
  1625. asml.remove(p);
  1626. p.free;
  1627. p:=hp1;
  1628. Result:=true;
  1629. end;
  1630. {
  1631. Turn
  1632. mul reg0, z,w
  1633. sub/add x, y, reg0
  1634. dealloc reg0
  1635. into
  1636. mls/mla x,z,w,y
  1637. }
  1638. if MatchInstruction(p, [A_MUL], [C_None], [PF_None]) and
  1639. (taicpu(p).ops=3) and
  1640. (taicpu(p).oper[0]^.typ = top_reg) and
  1641. (taicpu(p).oper[1]^.typ = top_reg) and
  1642. (taicpu(p).oper[2]^.typ = top_reg) and
  1643. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1644. MatchInstruction(hp1,[A_ADD,A_SUB],[C_None],[PF_None]) and
  1645. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p, hp1)) and
  1646. (not RegModifiedBetween(taicpu(p).oper[2]^.reg, p, hp1)) and
  1647. (((taicpu(hp1).opcode=A_ADD) and (current_settings.cputype>=cpu_armv4)) or
  1648. ((taicpu(hp1).opcode=A_SUB) and (current_settings.cputype in [cpu_armv6t2,cpu_armv7,cpu_armv7a,cpu_armv7r,cpu_armv7m,cpu_armv7em]))) and
  1649. // CPUs before ARMv6 don't recommend having the same Rd and Rm for MLA.
  1650. // TODO: A workaround would be to swap Rm and Rs
  1651. (not ((taicpu(hp1).opcode=A_ADD) and (current_settings.cputype<=cpu_armv6) and MatchOperand(taicpu(hp1).oper[0]^, taicpu(p).oper[1]^))) and
  1652. (((taicpu(hp1).ops=3) and
  1653. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1654. ((MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) and
  1655. (not RegModifiedBetween(taicpu(hp1).oper[1]^.reg, p, hp1))) or
  1656. ((MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1657. (taicpu(hp1).opcode=A_ADD) and
  1658. (not RegModifiedBetween(taicpu(hp1).oper[2]^.reg, p, hp1)))))) or
  1659. ((taicpu(hp1).ops=2) and
  1660. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1661. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1662. (RegEndOfLife(taicpu(p).oper[0]^.reg,taicpu(hp1))) then
  1663. begin
  1664. if taicpu(hp1).opcode=A_ADD then
  1665. begin
  1666. taicpu(hp1).opcode:=A_MLA;
  1667. if taicpu(hp1).ops=3 then
  1668. begin
  1669. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^) then
  1670. oldreg:=taicpu(hp1).oper[2]^.reg
  1671. else
  1672. oldreg:=taicpu(hp1).oper[1]^.reg;
  1673. end
  1674. else
  1675. oldreg:=taicpu(hp1).oper[0]^.reg;
  1676. taicpu(hp1).loadreg(1,taicpu(p).oper[1]^.reg);
  1677. taicpu(hp1).loadreg(2,taicpu(p).oper[2]^.reg);
  1678. taicpu(hp1).loadreg(3,oldreg);
  1679. DebugMsg('MulAdd2MLA done', p);
  1680. taicpu(hp1).ops:=4;
  1681. asml.remove(p);
  1682. p.free;
  1683. p:=hp1;
  1684. end
  1685. else
  1686. begin
  1687. taicpu(hp1).opcode:=A_MLS;
  1688. taicpu(hp1).loadreg(3,taicpu(hp1).oper[1]^.reg);
  1689. if taicpu(hp1).ops=2 then
  1690. taicpu(hp1).loadreg(1,taicpu(hp1).oper[0]^.reg)
  1691. else
  1692. taicpu(hp1).loadreg(1,taicpu(p).oper[2]^.reg);
  1693. taicpu(hp1).loadreg(2,taicpu(p).oper[1]^.reg);
  1694. DebugMsg('MulSub2MLS done', p);
  1695. taicpu(hp1).ops:=4;
  1696. asml.remove(p);
  1697. p.free;
  1698. p:=hp1;
  1699. end;
  1700. result:=true;
  1701. end
  1702. end;
  1703. {$ifdef dummy}
  1704. A_MVN:
  1705. begin
  1706. {
  1707. change
  1708. mvn reg2,reg1
  1709. and reg3,reg4,reg2
  1710. dealloc reg2
  1711. to
  1712. bic reg3,reg4,reg1
  1713. }
  1714. if (taicpu(p).oper[1]^.typ = top_reg) and
  1715. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1716. MatchInstruction(hp1,A_AND,[],[]) and
  1717. (((taicpu(hp1).ops=3) and
  1718. (taicpu(hp1).oper[2]^.typ=top_reg) and
  1719. (MatchOperand(taicpu(hp1).oper[2]^, taicpu(p).oper[0]^.reg) or
  1720. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) or
  1721. ((taicpu(hp1).ops=2) and
  1722. (taicpu(hp1).oper[1]^.typ=top_reg) and
  1723. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg))) and
  1724. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1725. { reg1 might not be modified inbetween }
  1726. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1727. begin
  1728. DebugMsg('Peephole MvnAnd2Bic done', p);
  1729. taicpu(hp1).opcode:=A_BIC;
  1730. if taicpu(hp1).ops=3 then
  1731. begin
  1732. if MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) then
  1733. taicpu(hp1).loadReg(1,taicpu(hp1).oper[2]^.reg); // Swap operands
  1734. taicpu(hp1).loadReg(2,taicpu(p).oper[1]^.reg);
  1735. end
  1736. else
  1737. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1738. GetNextInstruction(p, hp1);
  1739. asml.remove(p);
  1740. p.free;
  1741. p:=hp1;
  1742. end;
  1743. end;
  1744. {$endif dummy}
  1745. A_UXTB:
  1746. begin
  1747. {
  1748. change
  1749. uxtb reg2,reg1
  1750. strb reg2,[...]
  1751. dealloc reg2
  1752. to
  1753. strb reg1,[...]
  1754. }
  1755. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1756. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1757. MatchInstruction(hp1, A_STR, [C_None], [PF_B]) and
  1758. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(hp1.Next))) and
  1759. { the reference in strb might not use reg2 }
  1760. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1761. { reg1 might not be modified inbetween }
  1762. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1763. begin
  1764. DebugMsg('Peephole UxtbStrb2Strb done', p);
  1765. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1766. GetNextInstruction(p,hp2);
  1767. asml.remove(p);
  1768. p.free;
  1769. p:=hp2;
  1770. result:=true;
  1771. end
  1772. {
  1773. change
  1774. uxtb reg2,reg1
  1775. uxth reg3,reg2
  1776. dealloc reg2
  1777. to
  1778. uxtb reg3,reg1
  1779. }
  1780. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1781. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1782. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1783. (taicpu(hp1).ops = 2) and
  1784. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1785. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1786. { reg1 might not be modified inbetween }
  1787. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1788. begin
  1789. DebugMsg('Peephole UxtbUxth2Uxtb done', p);
  1790. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1791. asml.remove(hp1);
  1792. hp1.free;
  1793. result:=true;
  1794. end
  1795. {
  1796. change
  1797. uxtb reg2,reg1
  1798. uxtb reg3,reg2
  1799. dealloc reg2
  1800. to
  1801. uxtb reg3,reg1
  1802. }
  1803. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1804. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1805. MatchInstruction(hp1, A_UXTB, [C_None], [PF_None]) and
  1806. (taicpu(hp1).ops = 2) and
  1807. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1808. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1809. { reg1 might not be modified inbetween }
  1810. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1811. begin
  1812. DebugMsg('Peephole UxtbUxtb2Uxtb done', p);
  1813. taicpu(p).loadReg(0,taicpu(hp1).oper[0]^.reg);
  1814. asml.remove(hp1);
  1815. hp1.free;
  1816. result:=true;
  1817. end
  1818. {
  1819. change
  1820. uxtb reg2,reg1
  1821. and reg3,reg2,#0x*FF
  1822. dealloc reg2
  1823. to
  1824. uxtb reg3,reg1
  1825. }
  1826. else if MatchInstruction(p, A_UXTB, [C_None], [PF_None]) and
  1827. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1828. (taicpu(p).ops=2) and
  1829. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1830. (taicpu(hp1).ops=3) and
  1831. (taicpu(hp1).oper[2]^.typ=top_const) and
  1832. ((taicpu(hp1).oper[2]^.val and $FF)=$FF) and
  1833. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1834. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1835. { reg1 might not be modified inbetween }
  1836. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1837. begin
  1838. DebugMsg('Peephole UxtbAndImm2Uxtb done', p);
  1839. taicpu(hp1).opcode:=A_UXTB;
  1840. taicpu(hp1).ops:=2;
  1841. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1842. GetNextInstruction(p,hp2);
  1843. asml.remove(p);
  1844. p.free;
  1845. p:=hp2;
  1846. result:=true;
  1847. end
  1848. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1849. RemoveSuperfluousMove(p, hp1, 'UxtbMov2Data') then
  1850. Result:=true;
  1851. end;
  1852. A_UXTH:
  1853. begin
  1854. {
  1855. change
  1856. uxth reg2,reg1
  1857. strh reg2,[...]
  1858. dealloc reg2
  1859. to
  1860. strh reg1,[...]
  1861. }
  1862. if MatchInstruction(p, taicpu(p).opcode, [C_None], [PF_None]) and
  1863. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1864. MatchInstruction(hp1, A_STR, [C_None], [PF_H]) and
  1865. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1866. { the reference in strb might not use reg2 }
  1867. not(RegInRef(taicpu(p).oper[0]^.reg,taicpu(hp1).oper[1]^.ref^)) and
  1868. { reg1 might not be modified inbetween }
  1869. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1870. begin
  1871. DebugMsg('Peephole UXTHStrh2Strh done', p);
  1872. taicpu(hp1).loadReg(0,taicpu(p).oper[1]^.reg);
  1873. GetNextInstruction(p, hp1);
  1874. asml.remove(p);
  1875. p.free;
  1876. p:=hp1;
  1877. result:=true;
  1878. end
  1879. {
  1880. change
  1881. uxth reg2,reg1
  1882. uxth reg3,reg2
  1883. dealloc reg2
  1884. to
  1885. uxth reg3,reg1
  1886. }
  1887. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1888. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1889. MatchInstruction(hp1, A_UXTH, [C_None], [PF_None]) and
  1890. (taicpu(hp1).ops=2) and
  1891. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1892. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1893. { reg1 might not be modified inbetween }
  1894. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1895. begin
  1896. DebugMsg('Peephole UxthUxth2Uxth done', p);
  1897. taicpu(hp1).opcode:=A_UXTH;
  1898. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1899. GetNextInstruction(p, hp1);
  1900. asml.remove(p);
  1901. p.free;
  1902. p:=hp1;
  1903. result:=true;
  1904. end
  1905. {
  1906. change
  1907. uxth reg2,reg1
  1908. and reg3,reg2,#65535
  1909. dealloc reg2
  1910. to
  1911. uxth reg3,reg1
  1912. }
  1913. else if MatchInstruction(p, A_UXTH, [C_None], [PF_None]) and
  1914. GetNextInstructionUsingReg(p,hp1,taicpu(p).oper[0]^.reg) and
  1915. MatchInstruction(hp1, A_AND, [C_None], [PF_None]) and
  1916. (taicpu(hp1).ops=3) and
  1917. (taicpu(hp1).oper[2]^.typ=top_const) and
  1918. ((taicpu(hp1).oper[2]^.val and $FFFF)=$FFFF) and
  1919. MatchOperand(taicpu(hp1).oper[1]^, taicpu(p).oper[0]^.reg) and
  1920. RegEndofLife(taicpu(p).oper[0]^.reg,taicpu(hp1)) and
  1921. { reg1 might not be modified inbetween }
  1922. not(RegModifiedBetween(taicpu(p).oper[1]^.reg,p,hp1)) then
  1923. begin
  1924. DebugMsg('Peephole UxthAndImm2Uxth done', p);
  1925. taicpu(hp1).opcode:=A_UXTH;
  1926. taicpu(hp1).ops:=2;
  1927. taicpu(hp1).loadReg(1,taicpu(p).oper[1]^.reg);
  1928. GetNextInstruction(p, hp1);
  1929. asml.remove(p);
  1930. p.free;
  1931. p:=hp1;
  1932. result:=true;
  1933. end
  1934. else if GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  1935. RemoveSuperfluousMove(p, hp1, 'UxthMov2Data') then
  1936. Result:=true;
  1937. end;
  1938. A_CMP:
  1939. begin
  1940. {
  1941. change
  1942. cmp reg,const1
  1943. moveq reg,const1
  1944. movne reg,const2
  1945. to
  1946. cmp reg,const1
  1947. movne reg,const2
  1948. }
  1949. if (taicpu(p).oper[1]^.typ = top_const) and
  1950. GetNextInstruction(p, hp1) and
  1951. MatchInstruction(hp1, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1952. (taicpu(hp1).oper[1]^.typ = top_const) and
  1953. GetNextInstruction(hp1, hp2) and
  1954. MatchInstruction(hp2, A_MOV, [C_EQ, C_NE], [PF_NONE]) and
  1955. (taicpu(hp1).oper[1]^.typ = top_const) then
  1956. begin
  1957. Result:=RemoveRedundantMove(p, hp1, asml) or Result;
  1958. Result:=RemoveRedundantMove(p, hp2, asml) or Result;
  1959. end;
  1960. end;
  1961. A_STM:
  1962. begin
  1963. {
  1964. change
  1965. stmfd r13!,[r14]
  1966. sub r13,r13,#4
  1967. bl abc
  1968. add r13,r13,#4
  1969. ldmfd r13!,[r15]
  1970. into
  1971. b abc
  1972. }
  1973. if not(ts_thumb_interworking in current_settings.targetswitches) and
  1974. MatchInstruction(p, A_STM, [C_None], [PF_FD]) and
  1975. GetNextInstruction(p, hp1) and
  1976. GetNextInstruction(hp1, hp2) and
  1977. SkipEntryExitMarker(hp2, hp2) and
  1978. GetNextInstruction(hp2, hp3) and
  1979. SkipEntryExitMarker(hp3, hp3) and
  1980. GetNextInstruction(hp3, hp4) and
  1981. (taicpu(p).oper[0]^.typ = top_ref) and
  1982. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  1983. (taicpu(p).oper[0]^.ref^.base=NR_NO) and
  1984. (taicpu(p).oper[0]^.ref^.offset=0) and
  1985. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  1986. (taicpu(p).oper[1]^.typ = top_regset) and
  1987. (taicpu(p).oper[1]^.regset^ = [RS_R14]) and
  1988. MatchInstruction(hp1, A_SUB, [C_None], [PF_NONE]) and
  1989. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1990. (taicpu(hp1).oper[0]^.reg = NR_STACK_POINTER_REG) and
  1991. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp1).oper[1]^) and
  1992. (taicpu(hp1).oper[2]^.typ = top_const) and
  1993. MatchInstruction(hp3, A_ADD, [C_None], [PF_NONE]) and
  1994. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[0]^) and
  1995. MatchOperand(taicpu(hp1).oper[0]^,taicpu(hp3).oper[1]^) and
  1996. MatchOperand(taicpu(hp1).oper[2]^,taicpu(hp3).oper[2]^) and
  1997. MatchInstruction(hp2, [A_BL,A_BLX], [C_None], [PF_NONE]) and
  1998. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1999. MatchInstruction(hp4, A_LDM, [C_None], [PF_FD]) and
  2000. MatchOperand(taicpu(p).oper[0]^,taicpu(hp4).oper[0]^) and
  2001. (taicpu(hp4).oper[1]^.typ = top_regset) and
  2002. (taicpu(hp4).oper[1]^.regset^ = [RS_R15]) then
  2003. begin
  2004. asml.Remove(p);
  2005. asml.Remove(hp1);
  2006. asml.Remove(hp3);
  2007. asml.Remove(hp4);
  2008. taicpu(hp2).opcode:=A_B;
  2009. p.free;
  2010. hp1.free;
  2011. hp3.free;
  2012. hp4.free;
  2013. p:=hp2;
  2014. DebugMsg('Peephole Bl2B done', p);
  2015. end;
  2016. end;
  2017. end;
  2018. end;
  2019. end;
  2020. end;
  2021. { instructions modifying the CPSR can be only the last instruction }
  2022. function MustBeLast(p : tai) : boolean;
  2023. begin
  2024. Result:=(p.typ=ait_instruction) and
  2025. ((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
  2026. ((taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and (taicpu(p).oper[0]^.reg=NR_PC)) or
  2027. (taicpu(p).oppostfix=PF_S));
  2028. end;
  2029. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  2030. var
  2031. p,hp1,hp2: tai;
  2032. l : longint;
  2033. condition : tasmcond;
  2034. hp3: tai;
  2035. WasLast: boolean;
  2036. { UsedRegs, TmpUsedRegs: TRegSet; }
  2037. begin
  2038. p := BlockStart;
  2039. { UsedRegs := []; }
  2040. while (p <> BlockEnd) Do
  2041. begin
  2042. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2043. case p.Typ Of
  2044. Ait_Instruction:
  2045. begin
  2046. case taicpu(p).opcode Of
  2047. A_B:
  2048. if (taicpu(p).condition<>C_None) and
  2049. not(GenerateThumbCode) then
  2050. begin
  2051. { check for
  2052. Bxx xxx
  2053. <several instructions>
  2054. xxx:
  2055. }
  2056. l:=0;
  2057. WasLast:=False;
  2058. GetNextInstruction(p, hp1);
  2059. while assigned(hp1) and
  2060. (l<=4) and
  2061. CanBeCond(hp1) and
  2062. { stop on labels }
  2063. not(hp1.typ=ait_label) do
  2064. begin
  2065. inc(l);
  2066. if MustBeLast(hp1) then
  2067. begin
  2068. WasLast:=True;
  2069. GetNextInstruction(hp1,hp1);
  2070. break;
  2071. end
  2072. else
  2073. GetNextInstruction(hp1,hp1);
  2074. end;
  2075. if assigned(hp1) then
  2076. begin
  2077. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2078. begin
  2079. if (l<=4) and (l>0) then
  2080. begin
  2081. condition:=inverse_cond(taicpu(p).condition);
  2082. hp2:=p;
  2083. GetNextInstruction(p,hp1);
  2084. p:=hp1;
  2085. repeat
  2086. if hp1.typ=ait_instruction then
  2087. taicpu(hp1).condition:=condition;
  2088. if MustBeLast(hp1) then
  2089. begin
  2090. GetNextInstruction(hp1,hp1);
  2091. break;
  2092. end
  2093. else
  2094. GetNextInstruction(hp1,hp1);
  2095. until not(assigned(hp1)) or
  2096. not(CanBeCond(hp1)) or
  2097. (hp1.typ=ait_label);
  2098. { wait with removing else GetNextInstruction could
  2099. ignore the label if it was the only usage in the
  2100. jump moved away }
  2101. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2102. asml.remove(hp2);
  2103. hp2.free;
  2104. continue;
  2105. end;
  2106. end
  2107. else
  2108. { do not perform further optimizations if there is inctructon
  2109. in block #1 which can not be optimized.
  2110. }
  2111. if not WasLast then
  2112. begin
  2113. { check further for
  2114. Bcc xxx
  2115. <several instructions 1>
  2116. B yyy
  2117. xxx:
  2118. <several instructions 2>
  2119. yyy:
  2120. }
  2121. { hp2 points to jmp yyy }
  2122. hp2:=hp1;
  2123. { skip hp1 to xxx }
  2124. GetNextInstruction(hp1, hp1);
  2125. if assigned(hp2) and
  2126. assigned(hp1) and
  2127. (l<=3) and
  2128. (hp2.typ=ait_instruction) and
  2129. (taicpu(hp2).is_jmp) and
  2130. (taicpu(hp2).condition=C_None) and
  2131. { real label and jump, no further references to the
  2132. label are allowed }
  2133. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  2134. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2135. begin
  2136. l:=0;
  2137. { skip hp1 to <several moves 2> }
  2138. GetNextInstruction(hp1, hp1);
  2139. while assigned(hp1) and
  2140. CanBeCond(hp1) do
  2141. begin
  2142. inc(l);
  2143. GetNextInstruction(hp1, hp1);
  2144. end;
  2145. { hp1 points to yyy: }
  2146. if assigned(hp1) and
  2147. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  2148. begin
  2149. condition:=inverse_cond(taicpu(p).condition);
  2150. GetNextInstruction(p,hp1);
  2151. hp3:=p;
  2152. p:=hp1;
  2153. repeat
  2154. if hp1.typ=ait_instruction then
  2155. taicpu(hp1).condition:=condition;
  2156. GetNextInstruction(hp1,hp1);
  2157. until not(assigned(hp1)) or
  2158. not(CanBeCond(hp1));
  2159. { hp2 is still at jmp yyy }
  2160. GetNextInstruction(hp2,hp1);
  2161. { hp2 is now at xxx: }
  2162. condition:=inverse_cond(condition);
  2163. GetNextInstruction(hp1,hp1);
  2164. { hp1 is now at <several movs 2> }
  2165. repeat
  2166. taicpu(hp1).condition:=condition;
  2167. GetNextInstruction(hp1,hp1);
  2168. until not(assigned(hp1)) or
  2169. not(CanBeCond(hp1)) or
  2170. (hp1.typ=ait_label);
  2171. {
  2172. asml.remove(hp1.next)
  2173. hp1.next.free;
  2174. asml.remove(hp1);
  2175. hp1.free;
  2176. }
  2177. { remove Bcc }
  2178. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  2179. asml.remove(hp3);
  2180. hp3.free;
  2181. { remove jmp }
  2182. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2183. asml.remove(hp2);
  2184. hp2.free;
  2185. continue;
  2186. end;
  2187. end;
  2188. end;
  2189. end;
  2190. end;
  2191. end;
  2192. end;
  2193. end;
  2194. p := tai(p.next)
  2195. end;
  2196. end;
  2197. function TCpuAsmOptimizer.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  2198. begin
  2199. If (p1.typ = ait_instruction) and (taicpu(p1).opcode=A_BL) then
  2200. Result:=true
  2201. else If MatchInstruction(p1, [A_LDR, A_STR], [], [PF_D]) and
  2202. (getsupreg(taicpu(p1).oper[0]^.reg)+1=getsupreg(reg)) then
  2203. Result:=true
  2204. else
  2205. Result:=inherited RegInInstruction(Reg, p1);
  2206. end;
  2207. const
  2208. { set of opcode which might or do write to memory }
  2209. { TODO : extend armins.dat to contain r/w info }
  2210. opcode_could_mem_write = [A_B,A_BL,A_BLX,A_BKPT,A_BX,A_STR,A_STRB,A_STRBT,
  2211. A_STRH,A_STRT,A_STF,A_SFM,A_STM,A_FSTS,A_FSTD];
  2212. { adjust the register live information when swapping the two instructions p and hp1,
  2213. they must follow one after the other }
  2214. procedure TCpuPreRegallocScheduler.SwapRegLive(p,hp1 : taicpu);
  2215. procedure CheckLiveEnd(reg : tregister);
  2216. var
  2217. supreg : TSuperRegister;
  2218. regtype : TRegisterType;
  2219. begin
  2220. if reg=NR_NO then
  2221. exit;
  2222. regtype:=getregtype(reg);
  2223. supreg:=getsupreg(reg);
  2224. if (cg.rg[regtype].live_end[supreg]=hp1) and
  2225. RegInInstruction(reg,p) then
  2226. cg.rg[regtype].live_end[supreg]:=p;
  2227. end;
  2228. procedure CheckLiveStart(reg : TRegister);
  2229. var
  2230. supreg : TSuperRegister;
  2231. regtype : TRegisterType;
  2232. begin
  2233. if reg=NR_NO then
  2234. exit;
  2235. regtype:=getregtype(reg);
  2236. supreg:=getsupreg(reg);
  2237. if (cg.rg[regtype].live_start[supreg]=p) and
  2238. RegInInstruction(reg,hp1) then
  2239. cg.rg[regtype].live_start[supreg]:=hp1;
  2240. end;
  2241. var
  2242. i : longint;
  2243. r : TSuperRegister;
  2244. begin
  2245. { assumption: p is directly followed by hp1 }
  2246. { if live of any reg used by p starts at p and hp1 uses this register then
  2247. set live start to hp1 }
  2248. for i:=0 to p.ops-1 do
  2249. case p.oper[i]^.typ of
  2250. Top_Reg:
  2251. CheckLiveStart(p.oper[i]^.reg);
  2252. Top_Ref:
  2253. begin
  2254. CheckLiveStart(p.oper[i]^.ref^.base);
  2255. CheckLiveStart(p.oper[i]^.ref^.index);
  2256. end;
  2257. Top_Shifterop:
  2258. CheckLiveStart(p.oper[i]^.shifterop^.rs);
  2259. Top_RegSet:
  2260. for r:=RS_R0 to RS_R15 do
  2261. if r in p.oper[i]^.regset^ then
  2262. CheckLiveStart(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2263. end;
  2264. { if live of any reg used by hp1 ends at hp1 and p uses this register then
  2265. set live end to p }
  2266. for i:=0 to hp1.ops-1 do
  2267. case hp1.oper[i]^.typ of
  2268. Top_Reg:
  2269. CheckLiveEnd(hp1.oper[i]^.reg);
  2270. Top_Ref:
  2271. begin
  2272. CheckLiveEnd(hp1.oper[i]^.ref^.base);
  2273. CheckLiveEnd(hp1.oper[i]^.ref^.index);
  2274. end;
  2275. Top_Shifterop:
  2276. CheckLiveStart(hp1.oper[i]^.shifterop^.rs);
  2277. Top_RegSet:
  2278. for r:=RS_R0 to RS_R15 do
  2279. if r in hp1.oper[i]^.regset^ then
  2280. CheckLiveEnd(newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2281. end;
  2282. end;
  2283. function TCpuPreRegallocScheduler.SchedulerPass1Cpu(var p: tai): boolean;
  2284. { TODO : schedule also forward }
  2285. { TODO : schedule distance > 1 }
  2286. var
  2287. hp1,hp2,hp3,hp4,hp5,insertpos : tai;
  2288. list : TAsmList;
  2289. begin
  2290. result:=true;
  2291. list:=TAsmList.create;
  2292. p:=BlockStart;
  2293. while p<>BlockEnd Do
  2294. begin
  2295. if (p.typ=ait_instruction) and
  2296. GetNextInstruction(p,hp1) and
  2297. (hp1.typ=ait_instruction) and
  2298. (taicpu(hp1).opcode in [A_LDR,A_LDRB,A_LDRH,A_LDRSB,A_LDRSH]) and
  2299. (taicpu(hp1).oppostfix in [PF_NONE, PF_B, PF_H, PF_SB, PF_SH]) and
  2300. { for now we don't reschedule if the previous instruction changes potentially a memory location }
  2301. ( (not(taicpu(p).opcode in opcode_could_mem_write) and
  2302. not(RegModifiedByInstruction(NR_PC,p))
  2303. ) or
  2304. ((taicpu(p).opcode in [A_STM,A_STRB,A_STRH,A_STR]) and
  2305. ((taicpu(hp1).oper[1]^.ref^.base=NR_PC) or
  2306. (assigned(taicpu(hp1).oper[1]^.ref^.symboldata) and
  2307. (taicpu(hp1).oper[1]^.ref^.offset=0)
  2308. )
  2309. ) or
  2310. { try to prove that the memory accesses don't overlapp }
  2311. ((taicpu(p).opcode in [A_STRB,A_STRH,A_STR]) and
  2312. (taicpu(p).oper[1]^.ref^.base=taicpu(hp1).oper[1]^.ref^.base) and
  2313. (taicpu(p).oppostfix=PF_None) and
  2314. (taicpu(hp1).oppostfix=PF_None) and
  2315. (taicpu(p).oper[1]^.ref^.index=NR_NO) and
  2316. (taicpu(hp1).oper[1]^.ref^.index=NR_NO) and
  2317. { get operand sizes and check if the offset distance is large enough to ensure no overlapp }
  2318. (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)]))
  2319. )
  2320. )
  2321. ) and
  2322. GetNextInstruction(hp1,hp2) and
  2323. (hp2.typ=ait_instruction) and
  2324. { loaded register used by next instruction? }
  2325. (RegInInstruction(taicpu(hp1).oper[0]^.reg,hp2)) and
  2326. { loaded register not used by previous instruction? }
  2327. not(RegInInstruction(taicpu(hp1).oper[0]^.reg,p)) and
  2328. { same condition? }
  2329. (taicpu(p).condition=taicpu(hp1).condition) and
  2330. { first instruction might not change the register used as base }
  2331. ((taicpu(hp1).oper[1]^.ref^.base=NR_NO) or
  2332. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.base,p))
  2333. ) and
  2334. { first instruction might not change the register used as index }
  2335. ((taicpu(hp1).oper[1]^.ref^.index=NR_NO) or
  2336. not(RegModifiedByInstruction(taicpu(hp1).oper[1]^.ref^.index,p))
  2337. ) and
  2338. { if we modify the basereg AND the first instruction used that reg, we can not schedule }
  2339. ((taicpu(hp1).oper[1]^.ref^.addressmode = AM_OFFSET) or
  2340. not(instructionLoadsFromReg(taicpu(hp1).oper[1]^.ref^.base,p))) then
  2341. begin
  2342. hp3:=tai(p.Previous);
  2343. hp5:=tai(p.next);
  2344. asml.Remove(p);
  2345. { if there is a reg. dealloc instruction associated with p, move it together with p }
  2346. { before the instruction? }
  2347. while assigned(hp3) and (hp3.typ<>ait_instruction) do
  2348. begin
  2349. if (hp3.typ=ait_regalloc) and (tai_regalloc(hp3).ratype in [ra_dealloc]) and
  2350. RegInInstruction(tai_regalloc(hp3).reg,p) then
  2351. begin
  2352. hp4:=hp3;
  2353. hp3:=tai(hp3.Previous);
  2354. asml.Remove(hp4);
  2355. list.Concat(hp4);
  2356. end
  2357. else
  2358. hp3:=tai(hp3.Previous);
  2359. end;
  2360. list.Concat(p);
  2361. SwapRegLive(taicpu(p),taicpu(hp1));
  2362. { after the instruction? }
  2363. while assigned(hp5) and (hp5.typ<>ait_instruction) do
  2364. begin
  2365. if (hp5.typ=ait_regalloc) and (tai_regalloc(hp5).ratype in [ra_dealloc]) and
  2366. RegInInstruction(tai_regalloc(hp5).reg,p) then
  2367. begin
  2368. hp4:=hp5;
  2369. hp5:=tai(hp5.next);
  2370. asml.Remove(hp4);
  2371. list.Concat(hp4);
  2372. end
  2373. else
  2374. hp5:=tai(hp5.Next);
  2375. end;
  2376. asml.Remove(hp1);
  2377. { if there are address labels associated with hp2, those must
  2378. stay with hp2 (e.g. for GOT-less PIC) }
  2379. insertpos:=hp2;
  2380. while assigned(hp2.previous) and
  2381. (tai(hp2.previous).typ<>ait_instruction) do
  2382. begin
  2383. hp2:=tai(hp2.previous);
  2384. if (hp2.typ=ait_label) and
  2385. (tai_label(hp2).labsym.typ=AT_ADDR) then
  2386. insertpos:=hp2;
  2387. end;
  2388. {$ifdef DEBUG_PREREGSCHEDULER}
  2389. asml.insertbefore(tai_comment.Create(strpnew('Rescheduled')),insertpos);
  2390. {$endif DEBUG_PREREGSCHEDULER}
  2391. asml.InsertBefore(hp1,insertpos);
  2392. asml.InsertListBefore(insertpos,list);
  2393. p:=tai(p.next)
  2394. end
  2395. else if p.typ=ait_instruction then
  2396. p:=hp1
  2397. else
  2398. p:=tai(p.next);
  2399. end;
  2400. list.Free;
  2401. end;
  2402. procedure DecrementPreceedingIT(list: TAsmList; p: tai);
  2403. var
  2404. hp : tai;
  2405. l : longint;
  2406. begin
  2407. hp := tai(p.Previous);
  2408. l := 1;
  2409. while assigned(hp) and
  2410. (l <= 4) do
  2411. begin
  2412. if hp.typ=ait_instruction then
  2413. begin
  2414. if (taicpu(hp).opcode>=A_IT) and
  2415. (taicpu(hp).opcode <= A_ITTTT) then
  2416. begin
  2417. if (taicpu(hp).opcode = A_IT) and
  2418. (l=1) then
  2419. list.Remove(hp)
  2420. else
  2421. case taicpu(hp).opcode of
  2422. A_ITE:
  2423. if l=2 then taicpu(hp).opcode := A_IT;
  2424. A_ITT:
  2425. if l=2 then taicpu(hp).opcode := A_IT;
  2426. A_ITEE:
  2427. if l=3 then taicpu(hp).opcode := A_ITE;
  2428. A_ITTE:
  2429. if l=3 then taicpu(hp).opcode := A_ITT;
  2430. A_ITET:
  2431. if l=3 then taicpu(hp).opcode := A_ITE;
  2432. A_ITTT:
  2433. if l=3 then taicpu(hp).opcode := A_ITT;
  2434. A_ITEEE:
  2435. if l=4 then taicpu(hp).opcode := A_ITEE;
  2436. A_ITTEE:
  2437. if l=4 then taicpu(hp).opcode := A_ITTE;
  2438. A_ITETE:
  2439. if l=4 then taicpu(hp).opcode := A_ITET;
  2440. A_ITTTE:
  2441. if l=4 then taicpu(hp).opcode := A_ITTT;
  2442. A_ITEET:
  2443. if l=4 then taicpu(hp).opcode := A_ITEE;
  2444. A_ITTET:
  2445. if l=4 then taicpu(hp).opcode := A_ITTE;
  2446. A_ITETT:
  2447. if l=4 then taicpu(hp).opcode := A_ITET;
  2448. A_ITTTT:
  2449. if l=4 then taicpu(hp).opcode := A_ITTT;
  2450. end;
  2451. break;
  2452. end;
  2453. {else if (taicpu(hp).condition<>taicpu(p).condition) or
  2454. (taicpu(hp).condition<>inverse_cond(taicpu(p).condition)) then
  2455. break;}
  2456. inc(l);
  2457. end;
  2458. hp := tai(hp.Previous);
  2459. end;
  2460. end;
  2461. function TCpuThumb2AsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  2462. var
  2463. hp : taicpu;
  2464. hp1,hp2 : tai;
  2465. oldreg : TRegister;
  2466. begin
  2467. result:=false;
  2468. if inherited PeepHoleOptPass1Cpu(p) then
  2469. result:=true
  2470. else if (p.typ=ait_instruction) and
  2471. MatchInstruction(p, A_STM, [C_None], [PF_FD,PF_DB]) and
  2472. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2473. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2474. ((taicpu(p).oper[1]^.regset^*[8..13,15])=[]) then
  2475. begin
  2476. DebugMsg('Peephole Stm2Push done', p);
  2477. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2478. AsmL.InsertAfter(hp, p);
  2479. asml.Remove(p);
  2480. p:=hp;
  2481. result:=true;
  2482. end
  2483. {else if (p.typ=ait_instruction) and
  2484. MatchInstruction(p, A_STR, [C_None], [PF_None]) and
  2485. (taicpu(p).oper[1]^.ref^.addressmode=AM_PREINDEXED) and
  2486. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2487. (taicpu(p).oper[1]^.ref^.offset=-4) and
  2488. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,14]) then
  2489. begin
  2490. DebugMsg('Peephole Str2Push done', p);
  2491. hp := taicpu.op_regset(A_PUSH, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2492. asml.InsertAfter(hp, p);
  2493. asml.Remove(p);
  2494. p.Free;
  2495. p:=hp;
  2496. result:=true;
  2497. end}
  2498. else if (p.typ=ait_instruction) and
  2499. MatchInstruction(p, A_LDM, [C_None], [PF_FD,PF_IA]) and
  2500. (taicpu(p).oper[0]^.ref^.addressmode=AM_PREINDEXED) and
  2501. (taicpu(p).oper[0]^.ref^.index=NR_STACK_POINTER_REG) and
  2502. ((taicpu(p).oper[1]^.regset^*[8..14])=[]) then
  2503. begin
  2504. DebugMsg('Peephole Ldm2Pop done', p);
  2505. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, taicpu(p).oper[1]^.regset^);
  2506. asml.InsertBefore(hp, p);
  2507. asml.Remove(p);
  2508. p.Free;
  2509. p:=hp;
  2510. result:=true;
  2511. end
  2512. {else if (p.typ=ait_instruction) and
  2513. MatchInstruction(p, A_LDR, [C_None], [PF_None]) and
  2514. (taicpu(p).oper[1]^.ref^.addressmode=AM_POSTINDEXED) and
  2515. (taicpu(p).oper[1]^.ref^.index=NR_STACK_POINTER_REG) and
  2516. (taicpu(p).oper[1]^.ref^.offset=4) and
  2517. (getsupreg(taicpu(p).oper[0]^.reg) in [0..7,15]) then
  2518. begin
  2519. DebugMsg('Peephole Ldr2Pop done', p);
  2520. hp := taicpu.op_regset(A_POP, R_INTREGISTER, R_SUBWHOLE, [getsupreg(taicpu(p).oper[0]^.reg)]);
  2521. asml.InsertBefore(hp, p);
  2522. asml.Remove(p);
  2523. p.Free;
  2524. p:=hp;
  2525. result:=true;
  2526. end}
  2527. else if (p.typ=ait_instruction) and
  2528. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2529. (taicpu(p).ops = 2) and
  2530. (taicpu(p).oper[1]^.typ=top_const) and
  2531. ((taicpu(p).oper[1]^.val=255) or
  2532. (taicpu(p).oper[1]^.val=65535)) then
  2533. begin
  2534. DebugMsg('Peephole AndR2Uxt done', p);
  2535. if taicpu(p).oper[1]^.val=255 then
  2536. taicpu(p).opcode:=A_UXTB
  2537. else
  2538. taicpu(p).opcode:=A_UXTH;
  2539. taicpu(p).loadreg(1, taicpu(p).oper[0]^.reg);
  2540. result := true;
  2541. end
  2542. else if (p.typ=ait_instruction) and
  2543. MatchInstruction(p, [A_AND], [], [PF_None]) and
  2544. (taicpu(p).ops = 3) and
  2545. (taicpu(p).oper[2]^.typ=top_const) and
  2546. ((taicpu(p).oper[2]^.val=255) or
  2547. (taicpu(p).oper[2]^.val=65535)) then
  2548. begin
  2549. DebugMsg('Peephole AndRR2Uxt done', p);
  2550. if taicpu(p).oper[2]^.val=255 then
  2551. taicpu(p).opcode:=A_UXTB
  2552. else
  2553. taicpu(p).opcode:=A_UXTH;
  2554. taicpu(p).ops:=2;
  2555. result := true;
  2556. end
  2557. {else if (p.typ=ait_instruction) and
  2558. MatchInstruction(p, [A_CMP], [C_None], [PF_None]) and
  2559. (taicpu(p).oper[1]^.typ=top_const) and
  2560. (taicpu(p).oper[1]^.val=0) and
  2561. GetNextInstruction(p,hp1) and
  2562. (taicpu(hp1).opcode=A_B) and
  2563. (taicpu(hp1).condition in [C_EQ,C_NE]) then
  2564. begin
  2565. if taicpu(hp1).condition = C_EQ then
  2566. hp2:=taicpu.op_reg_ref(A_CBZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^)
  2567. else
  2568. hp2:=taicpu.op_reg_ref(A_CBNZ, taicpu(p).oper[0]^.reg, taicpu(hp1).oper[0]^.ref^);
  2569. taicpu(hp2).is_jmp := true;
  2570. asml.InsertAfter(hp2, hp1);
  2571. asml.Remove(hp1);
  2572. hp1.Free;
  2573. asml.Remove(p);
  2574. p.Free;
  2575. p := hp2;
  2576. result := true;
  2577. end}
  2578. end;
  2579. procedure TCpuThumb2AsmOptimizer.PeepHoleOptPass2;
  2580. var
  2581. p,hp1,hp2: tai;
  2582. l,l2 : longint;
  2583. condition : tasmcond;
  2584. hp3: tai;
  2585. WasLast: boolean;
  2586. { UsedRegs, TmpUsedRegs: TRegSet; }
  2587. begin
  2588. p := BlockStart;
  2589. { UsedRegs := []; }
  2590. while (p <> BlockEnd) Do
  2591. begin
  2592. { UpdateUsedRegs(UsedRegs, tai(p.next)); }
  2593. case p.Typ Of
  2594. Ait_Instruction:
  2595. begin
  2596. case taicpu(p).opcode Of
  2597. A_B:
  2598. if taicpu(p).condition<>C_None then
  2599. begin
  2600. { check for
  2601. Bxx xxx
  2602. <several instructions>
  2603. xxx:
  2604. }
  2605. l:=0;
  2606. GetNextInstruction(p, hp1);
  2607. while assigned(hp1) and
  2608. (l<=4) and
  2609. CanBeCond(hp1) and
  2610. { stop on labels }
  2611. not(hp1.typ=ait_label) do
  2612. begin
  2613. inc(l);
  2614. if MustBeLast(hp1) then
  2615. begin
  2616. //hp1:=nil;
  2617. GetNextInstruction(hp1,hp1);
  2618. break;
  2619. end
  2620. else
  2621. GetNextInstruction(hp1,hp1);
  2622. end;
  2623. if assigned(hp1) then
  2624. begin
  2625. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  2626. begin
  2627. if (l<=4) and (l>0) then
  2628. begin
  2629. condition:=inverse_cond(taicpu(p).condition);
  2630. hp2:=p;
  2631. GetNextInstruction(p,hp1);
  2632. p:=hp1;
  2633. repeat
  2634. if hp1.typ=ait_instruction then
  2635. taicpu(hp1).condition:=condition;
  2636. if MustBeLast(hp1) then
  2637. begin
  2638. GetNextInstruction(hp1,hp1);
  2639. break;
  2640. end
  2641. else
  2642. GetNextInstruction(hp1,hp1);
  2643. until not(assigned(hp1)) or
  2644. not(CanBeCond(hp1)) or
  2645. (hp1.typ=ait_label);
  2646. { wait with removing else GetNextInstruction could
  2647. ignore the label if it was the only usage in the
  2648. jump moved away }
  2649. asml.InsertAfter(tai_comment.create(strpnew('Collapsed')), hp2);
  2650. DecrementPreceedingIT(asml, hp2);
  2651. case l of
  2652. 1: asml.InsertAfter(taicpu.op_cond(A_IT,condition), hp2);
  2653. 2: asml.InsertAfter(taicpu.op_cond(A_ITT,condition), hp2);
  2654. 3: asml.InsertAfter(taicpu.op_cond(A_ITTT,condition), hp2);
  2655. 4: asml.InsertAfter(taicpu.op_cond(A_ITTTT,condition), hp2);
  2656. end;
  2657. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  2658. asml.remove(hp2);
  2659. hp2.free;
  2660. continue;
  2661. end;
  2662. end;
  2663. end;
  2664. end;
  2665. end;
  2666. end;
  2667. end;
  2668. p := tai(p.next)
  2669. end;
  2670. end;
  2671. function TCpuThumb2AsmOptimizer.PostPeepHoleOptsCpu(var p: tai): boolean;
  2672. begin
  2673. result:=false;
  2674. if p.typ = ait_instruction then
  2675. begin
  2676. if MatchInstruction(p, A_MOV, [C_None], [PF_None]) and
  2677. (taicpu(p).oper[1]^.typ=top_const) and
  2678. (taicpu(p).oper[1]^.val >= 0) and
  2679. (taicpu(p).oper[1]^.val < 256) and
  2680. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2681. begin
  2682. DebugMsg('Peephole Mov2Movs done', p);
  2683. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2684. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2685. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2686. taicpu(p).oppostfix:=PF_S;
  2687. result:=true;
  2688. end
  2689. else if MatchInstruction(p, A_MVN, [C_None], [PF_None]) and
  2690. (taicpu(p).oper[1]^.typ=top_reg) and
  2691. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2692. begin
  2693. DebugMsg('Peephole Mvn2Mvns done', p);
  2694. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2695. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2696. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2697. taicpu(p).oppostfix:=PF_S;
  2698. result:=true;
  2699. end
  2700. else if MatchInstruction(p, A_RSB, [C_None], [PF_None]) and
  2701. (taicpu(p).ops = 3) and
  2702. (taicpu(p).oper[2]^.typ=top_const) and
  2703. (taicpu(p).oper[2]^.val=0) and
  2704. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2705. begin
  2706. DebugMsg('Peephole Rsb2Rsbs done', p);
  2707. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2708. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2709. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2710. taicpu(p).oppostfix:=PF_S;
  2711. result:=true;
  2712. end
  2713. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2714. (taicpu(p).ops = 3) and
  2715. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2716. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2717. (taicpu(p).oper[2]^.typ=top_const) and
  2718. (taicpu(p).oper[2]^.val >= 0) and
  2719. (taicpu(p).oper[2]^.val < 256) and
  2720. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2721. begin
  2722. DebugMsg('Peephole AddSub2*s done', p);
  2723. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2724. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2725. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2726. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2727. taicpu(p).oppostfix:=PF_S;
  2728. taicpu(p).ops := 2;
  2729. result:=true;
  2730. end
  2731. else if MatchInstruction(p, [A_ADD,A_SUB], [C_None], [PF_None]) and
  2732. (taicpu(p).ops = 2) and
  2733. (taicpu(p).oper[1]^.typ=top_reg) and
  2734. (not MatchOperand(taicpu(p).oper[0]^, NR_STACK_POINTER_REG)) and
  2735. (not MatchOperand(taicpu(p).oper[1]^, NR_STACK_POINTER_REG)) and
  2736. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2737. begin
  2738. DebugMsg('Peephole AddSub2*s done', p);
  2739. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2740. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2741. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2742. taicpu(p).oppostfix:=PF_S;
  2743. result:=true;
  2744. end
  2745. else if MatchInstruction(p, [A_ADD], [C_None], [PF_None]) and
  2746. (taicpu(p).ops = 3) and
  2747. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2748. (taicpu(p).oper[2]^.typ=top_reg) then
  2749. begin
  2750. DebugMsg('Peephole AddRRR2AddRR done', p);
  2751. taicpu(p).ops := 2;
  2752. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2753. result:=true;
  2754. end
  2755. 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
  2756. (taicpu(p).ops = 3) and
  2757. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2758. (taicpu(p).oper[2]^.typ=top_reg) and
  2759. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2760. begin
  2761. DebugMsg('Peephole opXXY2opsXY done', p);
  2762. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2763. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2764. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2765. taicpu(p).ops := 2;
  2766. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg);
  2767. taicpu(p).oppostfix:=PF_S;
  2768. result:=true;
  2769. end
  2770. 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
  2771. (taicpu(p).ops = 3) and
  2772. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2773. (taicpu(p).oper[2]^.typ in [top_reg,top_const]) then
  2774. begin
  2775. DebugMsg('Peephole opXXY2opXY done', p);
  2776. taicpu(p).ops := 2;
  2777. if taicpu(p).oper[2]^.typ=top_reg then
  2778. taicpu(p).loadreg(1,taicpu(p).oper[2]^.reg)
  2779. else
  2780. taicpu(p).loadconst(1,taicpu(p).oper[2]^.val);
  2781. result:=true;
  2782. end
  2783. else if MatchInstruction(p, [A_AND,A_ORR,A_EOR], [C_None], [PF_None,PF_S]) and
  2784. (taicpu(p).ops = 3) and
  2785. MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[2]^) and
  2786. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2787. begin
  2788. DebugMsg('Peephole opXYX2opsXY done', p);
  2789. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2790. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2791. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2792. taicpu(p).oppostfix:=PF_S;
  2793. taicpu(p).ops := 2;
  2794. result:=true;
  2795. end
  2796. else if MatchInstruction(p, [A_MOV], [C_None], [PF_None]) and
  2797. (taicpu(p).ops=3) and
  2798. (taicpu(p).oper[2]^.typ=top_shifterop) and
  2799. (taicpu(p).oper[2]^.shifterop^.shiftmode in [SM_LSL,SM_LSR,SM_ASR,SM_ROR]) and
  2800. //MatchOperand(taicpu(p).oper[0]^, taicpu(p).oper[1]^) and
  2801. (not RegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs)) then
  2802. begin
  2803. DebugMsg('Peephole Mov2Shift done', p);
  2804. asml.InsertBefore(tai_regalloc.alloc(NR_DEFAULTFLAGS,p), p);
  2805. asml.InsertAfter(tai_regalloc.dealloc(NR_DEFAULTFLAGS,p), p);
  2806. IncludeRegInUsedRegs(NR_DEFAULTFLAGS,UsedRegs);
  2807. taicpu(p).oppostfix:=PF_S;
  2808. case taicpu(p).oper[2]^.shifterop^.shiftmode of
  2809. SM_LSL: taicpu(p).opcode:=A_LSL;
  2810. SM_LSR: taicpu(p).opcode:=A_LSR;
  2811. SM_ASR: taicpu(p).opcode:=A_ASR;
  2812. SM_ROR: taicpu(p).opcode:=A_ROR;
  2813. end;
  2814. if taicpu(p).oper[2]^.shifterop^.rs<>NR_NO then
  2815. taicpu(p).loadreg(2, taicpu(p).oper[2]^.shifterop^.rs)
  2816. else
  2817. taicpu(p).loadconst(2, taicpu(p).oper[2]^.shifterop^.shiftimm);
  2818. result:=true;
  2819. end
  2820. end;
  2821. end;
  2822. begin
  2823. casmoptimizer:=TCpuAsmOptimizer;
  2824. cpreregallocscheduler:=TCpuPreRegallocScheduler;
  2825. End.