aoptcpu.pas 112 KB

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