2
0

aoptcpu.pas 141 KB

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