aoptcpu.pas 132 KB

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