aoptcpu.pas 142 KB

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