aoptcpu.pas 141 KB

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