aoptcpu.pas 150 KB

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