aoptcpu.pas 134 KB

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