aoptcpu.pas 138 KB

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