aoptcpu.pas 124 KB

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