aoptcpu.pas 135 KB

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