aoptcpu.pas 124 KB

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