aoptcpu.pas 141 KB

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