aoptcpu.pas 104 KB

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