aoptcpu.pas 143 KB

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