aoptcpu.pas 136 KB

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