aoptcpu.pas 142 KB

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