aoptcpu.pas 142 KB

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