aoptcpu.pas 134 KB

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