aoptcpu.pas 134 KB

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