AArch64FastISel.cpp 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114
  1. //===- AArch6464FastISel.cpp - AArch64 FastISel implementation ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the AArch64-specific support for the FastISel class. Some
  10. // of the target-specific code is generated by tablegen in the file
  11. // AArch64GenFastISel.inc, which is #included here.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "AArch64.h"
  15. #include "AArch64CallingConvention.h"
  16. #include "AArch64MachineFunctionInfo.h"
  17. #include "AArch64RegisterInfo.h"
  18. #include "AArch64Subtarget.h"
  19. #include "MCTargetDesc/AArch64AddressingModes.h"
  20. #include "Utils/AArch64BaseInfo.h"
  21. #include "llvm/ADT/APFloat.h"
  22. #include "llvm/ADT/APInt.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/Analysis/BranchProbabilityInfo.h"
  26. #include "llvm/CodeGen/CallingConvLower.h"
  27. #include "llvm/CodeGen/FastISel.h"
  28. #include "llvm/CodeGen/FunctionLoweringInfo.h"
  29. #include "llvm/CodeGen/ISDOpcodes.h"
  30. #include "llvm/CodeGen/MachineBasicBlock.h"
  31. #include "llvm/CodeGen/MachineConstantPool.h"
  32. #include "llvm/CodeGen/MachineFrameInfo.h"
  33. #include "llvm/CodeGen/MachineInstr.h"
  34. #include "llvm/CodeGen/MachineInstrBuilder.h"
  35. #include "llvm/CodeGen/MachineMemOperand.h"
  36. #include "llvm/CodeGen/MachineRegisterInfo.h"
  37. #include "llvm/CodeGen/RuntimeLibcalls.h"
  38. #include "llvm/CodeGen/ValueTypes.h"
  39. #include "llvm/IR/Argument.h"
  40. #include "llvm/IR/Attributes.h"
  41. #include "llvm/IR/BasicBlock.h"
  42. #include "llvm/IR/CallingConv.h"
  43. #include "llvm/IR/Constant.h"
  44. #include "llvm/IR/Constants.h"
  45. #include "llvm/IR/DataLayout.h"
  46. #include "llvm/IR/DerivedTypes.h"
  47. #include "llvm/IR/Function.h"
  48. #include "llvm/IR/GetElementPtrTypeIterator.h"
  49. #include "llvm/IR/GlobalValue.h"
  50. #include "llvm/IR/InstrTypes.h"
  51. #include "llvm/IR/Instruction.h"
  52. #include "llvm/IR/Instructions.h"
  53. #include "llvm/IR/IntrinsicInst.h"
  54. #include "llvm/IR/Intrinsics.h"
  55. #include "llvm/IR/Operator.h"
  56. #include "llvm/IR/Type.h"
  57. #include "llvm/IR/User.h"
  58. #include "llvm/IR/Value.h"
  59. #include "llvm/MC/MCInstrDesc.h"
  60. #include "llvm/MC/MCRegisterInfo.h"
  61. #include "llvm/MC/MCSymbol.h"
  62. #include "llvm/Support/AtomicOrdering.h"
  63. #include "llvm/Support/Casting.h"
  64. #include "llvm/Support/CodeGen.h"
  65. #include "llvm/Support/Compiler.h"
  66. #include "llvm/Support/ErrorHandling.h"
  67. #include "llvm/Support/MachineValueType.h"
  68. #include "llvm/Support/MathExtras.h"
  69. #include <algorithm>
  70. #include <cassert>
  71. #include <cstdint>
  72. #include <iterator>
  73. #include <utility>
  74. using namespace llvm;
  75. namespace {
  76. class AArch64FastISel final : public FastISel {
  77. class Address {
  78. public:
  79. using BaseKind = enum {
  80. RegBase,
  81. FrameIndexBase
  82. };
  83. private:
  84. BaseKind Kind = RegBase;
  85. AArch64_AM::ShiftExtendType ExtType = AArch64_AM::InvalidShiftExtend;
  86. union {
  87. unsigned Reg;
  88. int FI;
  89. } Base;
  90. unsigned OffsetReg = 0;
  91. unsigned Shift = 0;
  92. int64_t Offset = 0;
  93. const GlobalValue *GV = nullptr;
  94. public:
  95. Address() { Base.Reg = 0; }
  96. void setKind(BaseKind K) { Kind = K; }
  97. BaseKind getKind() const { return Kind; }
  98. void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
  99. AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
  100. bool isRegBase() const { return Kind == RegBase; }
  101. bool isFIBase() const { return Kind == FrameIndexBase; }
  102. void setReg(unsigned Reg) {
  103. assert(isRegBase() && "Invalid base register access!");
  104. Base.Reg = Reg;
  105. }
  106. unsigned getReg() const {
  107. assert(isRegBase() && "Invalid base register access!");
  108. return Base.Reg;
  109. }
  110. void setOffsetReg(unsigned Reg) {
  111. OffsetReg = Reg;
  112. }
  113. unsigned getOffsetReg() const {
  114. return OffsetReg;
  115. }
  116. void setFI(unsigned FI) {
  117. assert(isFIBase() && "Invalid base frame index access!");
  118. Base.FI = FI;
  119. }
  120. unsigned getFI() const {
  121. assert(isFIBase() && "Invalid base frame index access!");
  122. return Base.FI;
  123. }
  124. void setOffset(int64_t O) { Offset = O; }
  125. int64_t getOffset() { return Offset; }
  126. void setShift(unsigned S) { Shift = S; }
  127. unsigned getShift() { return Shift; }
  128. void setGlobalValue(const GlobalValue *G) { GV = G; }
  129. const GlobalValue *getGlobalValue() { return GV; }
  130. };
  131. /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
  132. /// make the right decision when generating code for different targets.
  133. const AArch64Subtarget *Subtarget;
  134. LLVMContext *Context;
  135. bool fastLowerArguments() override;
  136. bool fastLowerCall(CallLoweringInfo &CLI) override;
  137. bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
  138. private:
  139. // Selection routines.
  140. bool selectAddSub(const Instruction *I);
  141. bool selectLogicalOp(const Instruction *I);
  142. bool selectLoad(const Instruction *I);
  143. bool selectStore(const Instruction *I);
  144. bool selectBranch(const Instruction *I);
  145. bool selectIndirectBr(const Instruction *I);
  146. bool selectCmp(const Instruction *I);
  147. bool selectSelect(const Instruction *I);
  148. bool selectFPExt(const Instruction *I);
  149. bool selectFPTrunc(const Instruction *I);
  150. bool selectFPToInt(const Instruction *I, bool Signed);
  151. bool selectIntToFP(const Instruction *I, bool Signed);
  152. bool selectRem(const Instruction *I, unsigned ISDOpcode);
  153. bool selectRet(const Instruction *I);
  154. bool selectTrunc(const Instruction *I);
  155. bool selectIntExt(const Instruction *I);
  156. bool selectMul(const Instruction *I);
  157. bool selectShift(const Instruction *I);
  158. bool selectBitCast(const Instruction *I);
  159. bool selectFRem(const Instruction *I);
  160. bool selectSDiv(const Instruction *I);
  161. bool selectGetElementPtr(const Instruction *I);
  162. bool selectAtomicCmpXchg(const AtomicCmpXchgInst *I);
  163. // Utility helper routines.
  164. bool isTypeLegal(Type *Ty, MVT &VT);
  165. bool isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed = false);
  166. bool isValueAvailable(const Value *V) const;
  167. bool computeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
  168. bool computeCallAddress(const Value *V, Address &Addr);
  169. bool simplifyAddress(Address &Addr, MVT VT);
  170. void addLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
  171. MachineMemOperand::Flags Flags,
  172. unsigned ScaleFactor, MachineMemOperand *MMO);
  173. bool isMemCpySmall(uint64_t Len, unsigned Alignment);
  174. bool tryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
  175. unsigned Alignment);
  176. bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
  177. const Value *Cond);
  178. bool optimizeIntExtLoad(const Instruction *I, MVT RetVT, MVT SrcVT);
  179. bool optimizeSelect(const SelectInst *SI);
  180. unsigned getRegForGEPIndex(const Value *Idx);
  181. // Emit helper routines.
  182. unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
  183. const Value *RHS, bool SetFlags = false,
  184. bool WantResult = true, bool IsZExt = false);
  185. unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
  186. unsigned RHSReg, bool SetFlags = false,
  187. bool WantResult = true);
  188. unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
  189. uint64_t Imm, bool SetFlags = false,
  190. bool WantResult = true);
  191. unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
  192. unsigned RHSReg, AArch64_AM::ShiftExtendType ShiftType,
  193. uint64_t ShiftImm, bool SetFlags = false,
  194. bool WantResult = true);
  195. unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
  196. unsigned RHSReg, AArch64_AM::ShiftExtendType ExtType,
  197. uint64_t ShiftImm, bool SetFlags = false,
  198. bool WantResult = true);
  199. // Emit functions.
  200. bool emitCompareAndBranch(const BranchInst *BI);
  201. bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
  202. bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
  203. bool emitICmp_ri(MVT RetVT, unsigned LHSReg, uint64_t Imm);
  204. bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
  205. unsigned emitLoad(MVT VT, MVT ResultVT, Address Addr, bool WantZExt = true,
  206. MachineMemOperand *MMO = nullptr);
  207. bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
  208. MachineMemOperand *MMO = nullptr);
  209. bool emitStoreRelease(MVT VT, unsigned SrcReg, unsigned AddrReg,
  210. MachineMemOperand *MMO = nullptr);
  211. unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
  212. unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
  213. unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
  214. bool SetFlags = false, bool WantResult = true,
  215. bool IsZExt = false);
  216. unsigned emitAdd_ri_(MVT VT, unsigned Op0, int64_t Imm);
  217. unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
  218. bool SetFlags = false, bool WantResult = true,
  219. bool IsZExt = false);
  220. unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, unsigned RHSReg,
  221. bool WantResult = true);
  222. unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, unsigned RHSReg,
  223. AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
  224. bool WantResult = true);
  225. unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
  226. const Value *RHS);
  227. unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
  228. uint64_t Imm);
  229. unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
  230. unsigned RHSReg, uint64_t ShiftImm);
  231. unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, uint64_t Imm);
  232. unsigned emitMul_rr(MVT RetVT, unsigned Op0, unsigned Op1);
  233. unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, unsigned Op1);
  234. unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, unsigned Op1);
  235. unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, unsigned Op1Reg);
  236. unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, uint64_t Imm,
  237. bool IsZExt = true);
  238. unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, unsigned Op1Reg);
  239. unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, uint64_t Imm,
  240. bool IsZExt = true);
  241. unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, unsigned Op1Reg);
  242. unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, uint64_t Imm,
  243. bool IsZExt = false);
  244. unsigned materializeInt(const ConstantInt *CI, MVT VT);
  245. unsigned materializeFP(const ConstantFP *CFP, MVT VT);
  246. unsigned materializeGV(const GlobalValue *GV);
  247. // Call handling routines.
  248. private:
  249. CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
  250. bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
  251. unsigned &NumBytes);
  252. bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
  253. public:
  254. // Backend specific FastISel code.
  255. unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
  256. unsigned fastMaterializeConstant(const Constant *C) override;
  257. unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
  258. explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
  259. const TargetLibraryInfo *LibInfo)
  260. : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
  261. Subtarget =
  262. &static_cast<const AArch64Subtarget &>(FuncInfo.MF->getSubtarget());
  263. Context = &FuncInfo.Fn->getContext();
  264. }
  265. bool fastSelectInstruction(const Instruction *I) override;
  266. #include "AArch64GenFastISel.inc"
  267. };
  268. } // end anonymous namespace
  269. /// Check if the sign-/zero-extend will be a noop.
  270. static bool isIntExtFree(const Instruction *I) {
  271. assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
  272. "Unexpected integer extend instruction.");
  273. assert(!I->getType()->isVectorTy() && I->getType()->isIntegerTy() &&
  274. "Unexpected value type.");
  275. bool IsZExt = isa<ZExtInst>(I);
  276. if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0)))
  277. if (LI->hasOneUse())
  278. return true;
  279. if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0)))
  280. if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr()))
  281. return true;
  282. return false;
  283. }
  284. /// Determine the implicit scale factor that is applied by a memory
  285. /// operation for a given value type.
  286. static unsigned getImplicitScaleFactor(MVT VT) {
  287. switch (VT.SimpleTy) {
  288. default:
  289. return 0; // invalid
  290. case MVT::i1: // fall-through
  291. case MVT::i8:
  292. return 1;
  293. case MVT::i16:
  294. return 2;
  295. case MVT::i32: // fall-through
  296. case MVT::f32:
  297. return 4;
  298. case MVT::i64: // fall-through
  299. case MVT::f64:
  300. return 8;
  301. }
  302. }
  303. CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
  304. if (CC == CallingConv::WebKit_JS)
  305. return CC_AArch64_WebKit_JS;
  306. if (CC == CallingConv::GHC)
  307. return CC_AArch64_GHC;
  308. if (CC == CallingConv::CFGuard_Check)
  309. return CC_AArch64_Win64_CFGuard_Check;
  310. return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
  311. }
  312. unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
  313. assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i64 &&
  314. "Alloca should always return a pointer.");
  315. // Don't handle dynamic allocas.
  316. if (!FuncInfo.StaticAllocaMap.count(AI))
  317. return 0;
  318. DenseMap<const AllocaInst *, int>::iterator SI =
  319. FuncInfo.StaticAllocaMap.find(AI);
  320. if (SI != FuncInfo.StaticAllocaMap.end()) {
  321. Register ResultReg = createResultReg(&AArch64::GPR64spRegClass);
  322. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
  323. ResultReg)
  324. .addFrameIndex(SI->second)
  325. .addImm(0)
  326. .addImm(0);
  327. return ResultReg;
  328. }
  329. return 0;
  330. }
  331. unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
  332. if (VT > MVT::i64)
  333. return 0;
  334. if (!CI->isZero())
  335. return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
  336. // Create a copy from the zero register to materialize a "0" value.
  337. const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
  338. : &AArch64::GPR32RegClass;
  339. unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
  340. Register ResultReg = createResultReg(RC);
  341. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
  342. ResultReg).addReg(ZeroReg, getKillRegState(true));
  343. return ResultReg;
  344. }
  345. unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
  346. // Positive zero (+0.0) has to be materialized with a fmov from the zero
  347. // register, because the immediate version of fmov cannot encode zero.
  348. if (CFP->isNullValue())
  349. return fastMaterializeFloatZero(CFP);
  350. if (VT != MVT::f32 && VT != MVT::f64)
  351. return 0;
  352. const APFloat Val = CFP->getValueAPF();
  353. bool Is64Bit = (VT == MVT::f64);
  354. // This checks to see if we can use FMOV instructions to materialize
  355. // a constant, otherwise we have to materialize via the constant pool.
  356. int Imm =
  357. Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
  358. if (Imm != -1) {
  359. unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
  360. return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
  361. }
  362. // For the large code model materialize the FP constant in code.
  363. if (TM.getCodeModel() == CodeModel::Large) {
  364. unsigned Opc1 = Is64Bit ? AArch64::MOVi64imm : AArch64::MOVi32imm;
  365. const TargetRegisterClass *RC = Is64Bit ?
  366. &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  367. Register TmpReg = createResultReg(RC);
  368. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc1), TmpReg)
  369. .addImm(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
  370. Register ResultReg = createResultReg(TLI.getRegClassFor(VT));
  371. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  372. TII.get(TargetOpcode::COPY), ResultReg)
  373. .addReg(TmpReg, getKillRegState(true));
  374. return ResultReg;
  375. }
  376. // Materialize via constant pool. MachineConstantPool wants an explicit
  377. // alignment.
  378. Align Alignment = DL.getPrefTypeAlign(CFP->getType());
  379. unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Alignment);
  380. Register ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
  381. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
  382. ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
  383. unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
  384. Register ResultReg = createResultReg(TLI.getRegClassFor(VT));
  385. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
  386. .addReg(ADRPReg)
  387. .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
  388. return ResultReg;
  389. }
  390. unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
  391. // We can't handle thread-local variables quickly yet.
  392. if (GV->isThreadLocal())
  393. return 0;
  394. // MachO still uses GOT for large code-model accesses, but ELF requires
  395. // movz/movk sequences, which FastISel doesn't handle yet.
  396. if (!Subtarget->useSmallAddressing() && !Subtarget->isTargetMachO())
  397. return 0;
  398. unsigned OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
  399. EVT DestEVT = TLI.getValueType(DL, GV->getType(), true);
  400. if (!DestEVT.isSimple())
  401. return 0;
  402. Register ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
  403. unsigned ResultReg;
  404. if (OpFlags & AArch64II::MO_GOT) {
  405. // ADRP + LDRX
  406. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
  407. ADRPReg)
  408. .addGlobalAddress(GV, 0, AArch64II::MO_PAGE | OpFlags);
  409. unsigned LdrOpc;
  410. if (Subtarget->isTargetILP32()) {
  411. ResultReg = createResultReg(&AArch64::GPR32RegClass);
  412. LdrOpc = AArch64::LDRWui;
  413. } else {
  414. ResultReg = createResultReg(&AArch64::GPR64RegClass);
  415. LdrOpc = AArch64::LDRXui;
  416. }
  417. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(LdrOpc),
  418. ResultReg)
  419. .addReg(ADRPReg)
  420. .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
  421. AArch64II::MO_NC | OpFlags);
  422. if (!Subtarget->isTargetILP32())
  423. return ResultReg;
  424. // LDRWui produces a 32-bit register, but pointers in-register are 64-bits
  425. // so we must extend the result on ILP32.
  426. Register Result64 = createResultReg(&AArch64::GPR64RegClass);
  427. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  428. TII.get(TargetOpcode::SUBREG_TO_REG))
  429. .addDef(Result64)
  430. .addImm(0)
  431. .addReg(ResultReg, RegState::Kill)
  432. .addImm(AArch64::sub_32);
  433. return Result64;
  434. } else {
  435. // ADRP + ADDX
  436. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
  437. ADRPReg)
  438. .addGlobalAddress(GV, 0, AArch64II::MO_PAGE | OpFlags);
  439. ResultReg = createResultReg(&AArch64::GPR64spRegClass);
  440. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
  441. ResultReg)
  442. .addReg(ADRPReg)
  443. .addGlobalAddress(GV, 0,
  444. AArch64II::MO_PAGEOFF | AArch64II::MO_NC | OpFlags)
  445. .addImm(0);
  446. }
  447. return ResultReg;
  448. }
  449. unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
  450. EVT CEVT = TLI.getValueType(DL, C->getType(), true);
  451. // Only handle simple types.
  452. if (!CEVT.isSimple())
  453. return 0;
  454. MVT VT = CEVT.getSimpleVT();
  455. // arm64_32 has 32-bit pointers held in 64-bit registers. Because of that,
  456. // 'null' pointers need to have a somewhat special treatment.
  457. if (isa<ConstantPointerNull>(C)) {
  458. assert(VT == MVT::i64 && "Expected 64-bit pointers");
  459. return materializeInt(ConstantInt::get(Type::getInt64Ty(*Context), 0), VT);
  460. }
  461. if (const auto *CI = dyn_cast<ConstantInt>(C))
  462. return materializeInt(CI, VT);
  463. else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
  464. return materializeFP(CFP, VT);
  465. else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
  466. return materializeGV(GV);
  467. return 0;
  468. }
  469. unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
  470. assert(CFP->isNullValue() &&
  471. "Floating-point constant is not a positive zero.");
  472. MVT VT;
  473. if (!isTypeLegal(CFP->getType(), VT))
  474. return 0;
  475. if (VT != MVT::f32 && VT != MVT::f64)
  476. return 0;
  477. bool Is64Bit = (VT == MVT::f64);
  478. unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
  479. unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
  480. return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg);
  481. }
  482. /// Check if the multiply is by a power-of-2 constant.
  483. static bool isMulPowOf2(const Value *I) {
  484. if (const auto *MI = dyn_cast<MulOperator>(I)) {
  485. if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(0)))
  486. if (C->getValue().isPowerOf2())
  487. return true;
  488. if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(1)))
  489. if (C->getValue().isPowerOf2())
  490. return true;
  491. }
  492. return false;
  493. }
  494. // Computes the address to get to an object.
  495. bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
  496. {
  497. const User *U = nullptr;
  498. unsigned Opcode = Instruction::UserOp1;
  499. if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
  500. // Don't walk into other basic blocks unless the object is an alloca from
  501. // another block, otherwise it may not have a virtual register assigned.
  502. if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
  503. FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
  504. Opcode = I->getOpcode();
  505. U = I;
  506. }
  507. } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
  508. Opcode = C->getOpcode();
  509. U = C;
  510. }
  511. if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
  512. if (Ty->getAddressSpace() > 255)
  513. // Fast instruction selection doesn't support the special
  514. // address spaces.
  515. return false;
  516. switch (Opcode) {
  517. default:
  518. break;
  519. case Instruction::BitCast:
  520. // Look through bitcasts.
  521. return computeAddress(U->getOperand(0), Addr, Ty);
  522. case Instruction::IntToPtr:
  523. // Look past no-op inttoptrs.
  524. if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
  525. TLI.getPointerTy(DL))
  526. return computeAddress(U->getOperand(0), Addr, Ty);
  527. break;
  528. case Instruction::PtrToInt:
  529. // Look past no-op ptrtoints.
  530. if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
  531. return computeAddress(U->getOperand(0), Addr, Ty);
  532. break;
  533. case Instruction::GetElementPtr: {
  534. Address SavedAddr = Addr;
  535. uint64_t TmpOffset = Addr.getOffset();
  536. // Iterate through the GEP folding the constants into offsets where
  537. // we can.
  538. for (gep_type_iterator GTI = gep_type_begin(U), E = gep_type_end(U);
  539. GTI != E; ++GTI) {
  540. const Value *Op = GTI.getOperand();
  541. if (StructType *STy = GTI.getStructTypeOrNull()) {
  542. const StructLayout *SL = DL.getStructLayout(STy);
  543. unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
  544. TmpOffset += SL->getElementOffset(Idx);
  545. } else {
  546. uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
  547. while (true) {
  548. if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
  549. // Constant-offset addressing.
  550. TmpOffset += CI->getSExtValue() * S;
  551. break;
  552. }
  553. if (canFoldAddIntoGEP(U, Op)) {
  554. // A compatible add with a constant operand. Fold the constant.
  555. ConstantInt *CI =
  556. cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
  557. TmpOffset += CI->getSExtValue() * S;
  558. // Iterate on the other operand.
  559. Op = cast<AddOperator>(Op)->getOperand(0);
  560. continue;
  561. }
  562. // Unsupported
  563. goto unsupported_gep;
  564. }
  565. }
  566. }
  567. // Try to grab the base operand now.
  568. Addr.setOffset(TmpOffset);
  569. if (computeAddress(U->getOperand(0), Addr, Ty))
  570. return true;
  571. // We failed, restore everything and try the other options.
  572. Addr = SavedAddr;
  573. unsupported_gep:
  574. break;
  575. }
  576. case Instruction::Alloca: {
  577. const AllocaInst *AI = cast<AllocaInst>(Obj);
  578. DenseMap<const AllocaInst *, int>::iterator SI =
  579. FuncInfo.StaticAllocaMap.find(AI);
  580. if (SI != FuncInfo.StaticAllocaMap.end()) {
  581. Addr.setKind(Address::FrameIndexBase);
  582. Addr.setFI(SI->second);
  583. return true;
  584. }
  585. break;
  586. }
  587. case Instruction::Add: {
  588. // Adds of constants are common and easy enough.
  589. const Value *LHS = U->getOperand(0);
  590. const Value *RHS = U->getOperand(1);
  591. if (isa<ConstantInt>(LHS))
  592. std::swap(LHS, RHS);
  593. if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
  594. Addr.setOffset(Addr.getOffset() + CI->getSExtValue());
  595. return computeAddress(LHS, Addr, Ty);
  596. }
  597. Address Backup = Addr;
  598. if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
  599. return true;
  600. Addr = Backup;
  601. break;
  602. }
  603. case Instruction::Sub: {
  604. // Subs of constants are common and easy enough.
  605. const Value *LHS = U->getOperand(0);
  606. const Value *RHS = U->getOperand(1);
  607. if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
  608. Addr.setOffset(Addr.getOffset() - CI->getSExtValue());
  609. return computeAddress(LHS, Addr, Ty);
  610. }
  611. break;
  612. }
  613. case Instruction::Shl: {
  614. if (Addr.getOffsetReg())
  615. break;
  616. const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1));
  617. if (!CI)
  618. break;
  619. unsigned Val = CI->getZExtValue();
  620. if (Val < 1 || Val > 3)
  621. break;
  622. uint64_t NumBytes = 0;
  623. if (Ty && Ty->isSized()) {
  624. uint64_t NumBits = DL.getTypeSizeInBits(Ty);
  625. NumBytes = NumBits / 8;
  626. if (!isPowerOf2_64(NumBits))
  627. NumBytes = 0;
  628. }
  629. if (NumBytes != (1ULL << Val))
  630. break;
  631. Addr.setShift(Val);
  632. Addr.setExtendType(AArch64_AM::LSL);
  633. const Value *Src = U->getOperand(0);
  634. if (const auto *I = dyn_cast<Instruction>(Src)) {
  635. if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
  636. // Fold the zext or sext when it won't become a noop.
  637. if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
  638. if (!isIntExtFree(ZE) &&
  639. ZE->getOperand(0)->getType()->isIntegerTy(32)) {
  640. Addr.setExtendType(AArch64_AM::UXTW);
  641. Src = ZE->getOperand(0);
  642. }
  643. } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
  644. if (!isIntExtFree(SE) &&
  645. SE->getOperand(0)->getType()->isIntegerTy(32)) {
  646. Addr.setExtendType(AArch64_AM::SXTW);
  647. Src = SE->getOperand(0);
  648. }
  649. }
  650. }
  651. }
  652. if (const auto *AI = dyn_cast<BinaryOperator>(Src))
  653. if (AI->getOpcode() == Instruction::And) {
  654. const Value *LHS = AI->getOperand(0);
  655. const Value *RHS = AI->getOperand(1);
  656. if (const auto *C = dyn_cast<ConstantInt>(LHS))
  657. if (C->getValue() == 0xffffffff)
  658. std::swap(LHS, RHS);
  659. if (const auto *C = dyn_cast<ConstantInt>(RHS))
  660. if (C->getValue() == 0xffffffff) {
  661. Addr.setExtendType(AArch64_AM::UXTW);
  662. Register Reg = getRegForValue(LHS);
  663. if (!Reg)
  664. return false;
  665. Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, AArch64::sub_32);
  666. Addr.setOffsetReg(Reg);
  667. return true;
  668. }
  669. }
  670. Register Reg = getRegForValue(Src);
  671. if (!Reg)
  672. return false;
  673. Addr.setOffsetReg(Reg);
  674. return true;
  675. }
  676. case Instruction::Mul: {
  677. if (Addr.getOffsetReg())
  678. break;
  679. if (!isMulPowOf2(U))
  680. break;
  681. const Value *LHS = U->getOperand(0);
  682. const Value *RHS = U->getOperand(1);
  683. // Canonicalize power-of-2 value to the RHS.
  684. if (const auto *C = dyn_cast<ConstantInt>(LHS))
  685. if (C->getValue().isPowerOf2())
  686. std::swap(LHS, RHS);
  687. assert(isa<ConstantInt>(RHS) && "Expected an ConstantInt.");
  688. const auto *C = cast<ConstantInt>(RHS);
  689. unsigned Val = C->getValue().logBase2();
  690. if (Val < 1 || Val > 3)
  691. break;
  692. uint64_t NumBytes = 0;
  693. if (Ty && Ty->isSized()) {
  694. uint64_t NumBits = DL.getTypeSizeInBits(Ty);
  695. NumBytes = NumBits / 8;
  696. if (!isPowerOf2_64(NumBits))
  697. NumBytes = 0;
  698. }
  699. if (NumBytes != (1ULL << Val))
  700. break;
  701. Addr.setShift(Val);
  702. Addr.setExtendType(AArch64_AM::LSL);
  703. const Value *Src = LHS;
  704. if (const auto *I = dyn_cast<Instruction>(Src)) {
  705. if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
  706. // Fold the zext or sext when it won't become a noop.
  707. if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
  708. if (!isIntExtFree(ZE) &&
  709. ZE->getOperand(0)->getType()->isIntegerTy(32)) {
  710. Addr.setExtendType(AArch64_AM::UXTW);
  711. Src = ZE->getOperand(0);
  712. }
  713. } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
  714. if (!isIntExtFree(SE) &&
  715. SE->getOperand(0)->getType()->isIntegerTy(32)) {
  716. Addr.setExtendType(AArch64_AM::SXTW);
  717. Src = SE->getOperand(0);
  718. }
  719. }
  720. }
  721. }
  722. Register Reg = getRegForValue(Src);
  723. if (!Reg)
  724. return false;
  725. Addr.setOffsetReg(Reg);
  726. return true;
  727. }
  728. case Instruction::And: {
  729. if (Addr.getOffsetReg())
  730. break;
  731. if (!Ty || DL.getTypeSizeInBits(Ty) != 8)
  732. break;
  733. const Value *LHS = U->getOperand(0);
  734. const Value *RHS = U->getOperand(1);
  735. if (const auto *C = dyn_cast<ConstantInt>(LHS))
  736. if (C->getValue() == 0xffffffff)
  737. std::swap(LHS, RHS);
  738. if (const auto *C = dyn_cast<ConstantInt>(RHS))
  739. if (C->getValue() == 0xffffffff) {
  740. Addr.setShift(0);
  741. Addr.setExtendType(AArch64_AM::LSL);
  742. Addr.setExtendType(AArch64_AM::UXTW);
  743. Register Reg = getRegForValue(LHS);
  744. if (!Reg)
  745. return false;
  746. Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, AArch64::sub_32);
  747. Addr.setOffsetReg(Reg);
  748. return true;
  749. }
  750. break;
  751. }
  752. case Instruction::SExt:
  753. case Instruction::ZExt: {
  754. if (!Addr.getReg() || Addr.getOffsetReg())
  755. break;
  756. const Value *Src = nullptr;
  757. // Fold the zext or sext when it won't become a noop.
  758. if (const auto *ZE = dyn_cast<ZExtInst>(U)) {
  759. if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
  760. Addr.setExtendType(AArch64_AM::UXTW);
  761. Src = ZE->getOperand(0);
  762. }
  763. } else if (const auto *SE = dyn_cast<SExtInst>(U)) {
  764. if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
  765. Addr.setExtendType(AArch64_AM::SXTW);
  766. Src = SE->getOperand(0);
  767. }
  768. }
  769. if (!Src)
  770. break;
  771. Addr.setShift(0);
  772. Register Reg = getRegForValue(Src);
  773. if (!Reg)
  774. return false;
  775. Addr.setOffsetReg(Reg);
  776. return true;
  777. }
  778. } // end switch
  779. if (Addr.isRegBase() && !Addr.getReg()) {
  780. Register Reg = getRegForValue(Obj);
  781. if (!Reg)
  782. return false;
  783. Addr.setReg(Reg);
  784. return true;
  785. }
  786. if (!Addr.getOffsetReg()) {
  787. Register Reg = getRegForValue(Obj);
  788. if (!Reg)
  789. return false;
  790. Addr.setOffsetReg(Reg);
  791. return true;
  792. }
  793. return false;
  794. }
  795. bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
  796. const User *U = nullptr;
  797. unsigned Opcode = Instruction::UserOp1;
  798. bool InMBB = true;
  799. if (const auto *I = dyn_cast<Instruction>(V)) {
  800. Opcode = I->getOpcode();
  801. U = I;
  802. InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
  803. } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
  804. Opcode = C->getOpcode();
  805. U = C;
  806. }
  807. switch (Opcode) {
  808. default: break;
  809. case Instruction::BitCast:
  810. // Look past bitcasts if its operand is in the same BB.
  811. if (InMBB)
  812. return computeCallAddress(U->getOperand(0), Addr);
  813. break;
  814. case Instruction::IntToPtr:
  815. // Look past no-op inttoptrs if its operand is in the same BB.
  816. if (InMBB &&
  817. TLI.getValueType(DL, U->getOperand(0)->getType()) ==
  818. TLI.getPointerTy(DL))
  819. return computeCallAddress(U->getOperand(0), Addr);
  820. break;
  821. case Instruction::PtrToInt:
  822. // Look past no-op ptrtoints if its operand is in the same BB.
  823. if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
  824. return computeCallAddress(U->getOperand(0), Addr);
  825. break;
  826. }
  827. if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
  828. Addr.setGlobalValue(GV);
  829. return true;
  830. }
  831. // If all else fails, try to materialize the value in a register.
  832. if (!Addr.getGlobalValue()) {
  833. Addr.setReg(getRegForValue(V));
  834. return Addr.getReg() != 0;
  835. }
  836. return false;
  837. }
  838. bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
  839. EVT evt = TLI.getValueType(DL, Ty, true);
  840. if (Subtarget->isTargetILP32() && Ty->isPointerTy())
  841. return false;
  842. // Only handle simple types.
  843. if (evt == MVT::Other || !evt.isSimple())
  844. return false;
  845. VT = evt.getSimpleVT();
  846. // This is a legal type, but it's not something we handle in fast-isel.
  847. if (VT == MVT::f128)
  848. return false;
  849. // Handle all other legal types, i.e. a register that will directly hold this
  850. // value.
  851. return TLI.isTypeLegal(VT);
  852. }
  853. /// Determine if the value type is supported by FastISel.
  854. ///
  855. /// FastISel for AArch64 can handle more value types than are legal. This adds
  856. /// simple value type such as i1, i8, and i16.
  857. bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
  858. if (Ty->isVectorTy() && !IsVectorAllowed)
  859. return false;
  860. if (isTypeLegal(Ty, VT))
  861. return true;
  862. // If this is a type than can be sign or zero-extended to a basic operation
  863. // go ahead and accept it now.
  864. if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
  865. return true;
  866. return false;
  867. }
  868. bool AArch64FastISel::isValueAvailable(const Value *V) const {
  869. if (!isa<Instruction>(V))
  870. return true;
  871. const auto *I = cast<Instruction>(V);
  872. return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
  873. }
  874. bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
  875. if (Subtarget->isTargetILP32())
  876. return false;
  877. unsigned ScaleFactor = getImplicitScaleFactor(VT);
  878. if (!ScaleFactor)
  879. return false;
  880. bool ImmediateOffsetNeedsLowering = false;
  881. bool RegisterOffsetNeedsLowering = false;
  882. int64_t Offset = Addr.getOffset();
  883. if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
  884. ImmediateOffsetNeedsLowering = true;
  885. else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
  886. !isUInt<12>(Offset / ScaleFactor))
  887. ImmediateOffsetNeedsLowering = true;
  888. // Cannot encode an offset register and an immediate offset in the same
  889. // instruction. Fold the immediate offset into the load/store instruction and
  890. // emit an additional add to take care of the offset register.
  891. if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.getOffsetReg())
  892. RegisterOffsetNeedsLowering = true;
  893. // Cannot encode zero register as base.
  894. if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
  895. RegisterOffsetNeedsLowering = true;
  896. // If this is a stack pointer and the offset needs to be simplified then put
  897. // the alloca address into a register, set the base type back to register and
  898. // continue. This should almost never happen.
  899. if ((ImmediateOffsetNeedsLowering || Addr.getOffsetReg()) && Addr.isFIBase())
  900. {
  901. Register ResultReg = createResultReg(&AArch64::GPR64spRegClass);
  902. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
  903. ResultReg)
  904. .addFrameIndex(Addr.getFI())
  905. .addImm(0)
  906. .addImm(0);
  907. Addr.setKind(Address::RegBase);
  908. Addr.setReg(ResultReg);
  909. }
  910. if (RegisterOffsetNeedsLowering) {
  911. unsigned ResultReg = 0;
  912. if (Addr.getReg()) {
  913. if (Addr.getExtendType() == AArch64_AM::SXTW ||
  914. Addr.getExtendType() == AArch64_AM::UXTW )
  915. ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
  916. Addr.getOffsetReg(), Addr.getExtendType(),
  917. Addr.getShift());
  918. else
  919. ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
  920. Addr.getOffsetReg(), AArch64_AM::LSL,
  921. Addr.getShift());
  922. } else {
  923. if (Addr.getExtendType() == AArch64_AM::UXTW)
  924. ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
  925. Addr.getShift(), /*IsZExt=*/true);
  926. else if (Addr.getExtendType() == AArch64_AM::SXTW)
  927. ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
  928. Addr.getShift(), /*IsZExt=*/false);
  929. else
  930. ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
  931. Addr.getShift());
  932. }
  933. if (!ResultReg)
  934. return false;
  935. Addr.setReg(ResultReg);
  936. Addr.setOffsetReg(0);
  937. Addr.setShift(0);
  938. Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
  939. }
  940. // Since the offset is too large for the load/store instruction get the
  941. // reg+offset into a register.
  942. if (ImmediateOffsetNeedsLowering) {
  943. unsigned ResultReg;
  944. if (Addr.getReg())
  945. // Try to fold the immediate into the add instruction.
  946. ResultReg = emitAdd_ri_(MVT::i64, Addr.getReg(), Offset);
  947. else
  948. ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
  949. if (!ResultReg)
  950. return false;
  951. Addr.setReg(ResultReg);
  952. Addr.setOffset(0);
  953. }
  954. return true;
  955. }
  956. void AArch64FastISel::addLoadStoreOperands(Address &Addr,
  957. const MachineInstrBuilder &MIB,
  958. MachineMemOperand::Flags Flags,
  959. unsigned ScaleFactor,
  960. MachineMemOperand *MMO) {
  961. int64_t Offset = Addr.getOffset() / ScaleFactor;
  962. // Frame base works a bit differently. Handle it separately.
  963. if (Addr.isFIBase()) {
  964. int FI = Addr.getFI();
  965. // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
  966. // and alignment should be based on the VT.
  967. MMO = FuncInfo.MF->getMachineMemOperand(
  968. MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
  969. MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
  970. // Now add the rest of the operands.
  971. MIB.addFrameIndex(FI).addImm(Offset);
  972. } else {
  973. assert(Addr.isRegBase() && "Unexpected address kind.");
  974. const MCInstrDesc &II = MIB->getDesc();
  975. unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
  976. Addr.setReg(
  977. constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
  978. Addr.setOffsetReg(
  979. constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
  980. if (Addr.getOffsetReg()) {
  981. assert(Addr.getOffset() == 0 && "Unexpected offset");
  982. bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
  983. Addr.getExtendType() == AArch64_AM::SXTX;
  984. MIB.addReg(Addr.getReg());
  985. MIB.addReg(Addr.getOffsetReg());
  986. MIB.addImm(IsSigned);
  987. MIB.addImm(Addr.getShift() != 0);
  988. } else
  989. MIB.addReg(Addr.getReg()).addImm(Offset);
  990. }
  991. if (MMO)
  992. MIB.addMemOperand(MMO);
  993. }
  994. unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
  995. const Value *RHS, bool SetFlags,
  996. bool WantResult, bool IsZExt) {
  997. AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
  998. bool NeedExtend = false;
  999. switch (RetVT.SimpleTy) {
  1000. default:
  1001. return 0;
  1002. case MVT::i1:
  1003. NeedExtend = true;
  1004. break;
  1005. case MVT::i8:
  1006. NeedExtend = true;
  1007. ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
  1008. break;
  1009. case MVT::i16:
  1010. NeedExtend = true;
  1011. ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
  1012. break;
  1013. case MVT::i32: // fall-through
  1014. case MVT::i64:
  1015. break;
  1016. }
  1017. MVT SrcVT = RetVT;
  1018. RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
  1019. // Canonicalize immediates to the RHS first.
  1020. if (UseAdd && isa<Constant>(LHS) && !isa<Constant>(RHS))
  1021. std::swap(LHS, RHS);
  1022. // Canonicalize mul by power of 2 to the RHS.
  1023. if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
  1024. if (isMulPowOf2(LHS))
  1025. std::swap(LHS, RHS);
  1026. // Canonicalize shift immediate to the RHS.
  1027. if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
  1028. if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
  1029. if (isa<ConstantInt>(SI->getOperand(1)))
  1030. if (SI->getOpcode() == Instruction::Shl ||
  1031. SI->getOpcode() == Instruction::LShr ||
  1032. SI->getOpcode() == Instruction::AShr )
  1033. std::swap(LHS, RHS);
  1034. Register LHSReg = getRegForValue(LHS);
  1035. if (!LHSReg)
  1036. return 0;
  1037. if (NeedExtend)
  1038. LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
  1039. unsigned ResultReg = 0;
  1040. if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
  1041. uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
  1042. if (C->isNegative())
  1043. ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, -Imm, SetFlags,
  1044. WantResult);
  1045. else
  1046. ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, Imm, SetFlags,
  1047. WantResult);
  1048. } else if (const auto *C = dyn_cast<Constant>(RHS))
  1049. if (C->isNullValue())
  1050. ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, 0, SetFlags, WantResult);
  1051. if (ResultReg)
  1052. return ResultReg;
  1053. // Only extend the RHS within the instruction if there is a valid extend type.
  1054. if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
  1055. isValueAvailable(RHS)) {
  1056. if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
  1057. if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
  1058. if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
  1059. Register RHSReg = getRegForValue(SI->getOperand(0));
  1060. if (!RHSReg)
  1061. return 0;
  1062. return emitAddSub_rx(UseAdd, RetVT, LHSReg, RHSReg, ExtendType,
  1063. C->getZExtValue(), SetFlags, WantResult);
  1064. }
  1065. Register RHSReg = getRegForValue(RHS);
  1066. if (!RHSReg)
  1067. return 0;
  1068. return emitAddSub_rx(UseAdd, RetVT, LHSReg, RHSReg, ExtendType, 0,
  1069. SetFlags, WantResult);
  1070. }
  1071. // Check if the mul can be folded into the instruction.
  1072. if (RHS->hasOneUse() && isValueAvailable(RHS)) {
  1073. if (isMulPowOf2(RHS)) {
  1074. const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
  1075. const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
  1076. if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
  1077. if (C->getValue().isPowerOf2())
  1078. std::swap(MulLHS, MulRHS);
  1079. assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
  1080. uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
  1081. Register RHSReg = getRegForValue(MulLHS);
  1082. if (!RHSReg)
  1083. return 0;
  1084. ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, RHSReg, AArch64_AM::LSL,
  1085. ShiftVal, SetFlags, WantResult);
  1086. if (ResultReg)
  1087. return ResultReg;
  1088. }
  1089. }
  1090. // Check if the shift can be folded into the instruction.
  1091. if (RHS->hasOneUse() && isValueAvailable(RHS)) {
  1092. if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
  1093. if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
  1094. AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
  1095. switch (SI->getOpcode()) {
  1096. default: break;
  1097. case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
  1098. case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
  1099. case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
  1100. }
  1101. uint64_t ShiftVal = C->getZExtValue();
  1102. if (ShiftType != AArch64_AM::InvalidShiftExtend) {
  1103. Register RHSReg = getRegForValue(SI->getOperand(0));
  1104. if (!RHSReg)
  1105. return 0;
  1106. ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, RHSReg, ShiftType,
  1107. ShiftVal, SetFlags, WantResult);
  1108. if (ResultReg)
  1109. return ResultReg;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. Register RHSReg = getRegForValue(RHS);
  1115. if (!RHSReg)
  1116. return 0;
  1117. if (NeedExtend)
  1118. RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
  1119. return emitAddSub_rr(UseAdd, RetVT, LHSReg, RHSReg, SetFlags, WantResult);
  1120. }
  1121. unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
  1122. unsigned RHSReg, bool SetFlags,
  1123. bool WantResult) {
  1124. assert(LHSReg && RHSReg && "Invalid register number.");
  1125. if (LHSReg == AArch64::SP || LHSReg == AArch64::WSP ||
  1126. RHSReg == AArch64::SP || RHSReg == AArch64::WSP)
  1127. return 0;
  1128. if (RetVT != MVT::i32 && RetVT != MVT::i64)
  1129. return 0;
  1130. static const unsigned OpcTable[2][2][2] = {
  1131. { { AArch64::SUBWrr, AArch64::SUBXrr },
  1132. { AArch64::ADDWrr, AArch64::ADDXrr } },
  1133. { { AArch64::SUBSWrr, AArch64::SUBSXrr },
  1134. { AArch64::ADDSWrr, AArch64::ADDSXrr } }
  1135. };
  1136. bool Is64Bit = RetVT == MVT::i64;
  1137. unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
  1138. const TargetRegisterClass *RC =
  1139. Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  1140. unsigned ResultReg;
  1141. if (WantResult)
  1142. ResultReg = createResultReg(RC);
  1143. else
  1144. ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
  1145. const MCInstrDesc &II = TII.get(Opc);
  1146. LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
  1147. RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
  1148. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1149. .addReg(LHSReg)
  1150. .addReg(RHSReg);
  1151. return ResultReg;
  1152. }
  1153. unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
  1154. uint64_t Imm, bool SetFlags,
  1155. bool WantResult) {
  1156. assert(LHSReg && "Invalid register number.");
  1157. if (RetVT != MVT::i32 && RetVT != MVT::i64)
  1158. return 0;
  1159. unsigned ShiftImm;
  1160. if (isUInt<12>(Imm))
  1161. ShiftImm = 0;
  1162. else if ((Imm & 0xfff000) == Imm) {
  1163. ShiftImm = 12;
  1164. Imm >>= 12;
  1165. } else
  1166. return 0;
  1167. static const unsigned OpcTable[2][2][2] = {
  1168. { { AArch64::SUBWri, AArch64::SUBXri },
  1169. { AArch64::ADDWri, AArch64::ADDXri } },
  1170. { { AArch64::SUBSWri, AArch64::SUBSXri },
  1171. { AArch64::ADDSWri, AArch64::ADDSXri } }
  1172. };
  1173. bool Is64Bit = RetVT == MVT::i64;
  1174. unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
  1175. const TargetRegisterClass *RC;
  1176. if (SetFlags)
  1177. RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  1178. else
  1179. RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
  1180. unsigned ResultReg;
  1181. if (WantResult)
  1182. ResultReg = createResultReg(RC);
  1183. else
  1184. ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
  1185. const MCInstrDesc &II = TII.get(Opc);
  1186. LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
  1187. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1188. .addReg(LHSReg)
  1189. .addImm(Imm)
  1190. .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
  1191. return ResultReg;
  1192. }
  1193. unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
  1194. unsigned RHSReg,
  1195. AArch64_AM::ShiftExtendType ShiftType,
  1196. uint64_t ShiftImm, bool SetFlags,
  1197. bool WantResult) {
  1198. assert(LHSReg && RHSReg && "Invalid register number.");
  1199. assert(LHSReg != AArch64::SP && LHSReg != AArch64::WSP &&
  1200. RHSReg != AArch64::SP && RHSReg != AArch64::WSP);
  1201. if (RetVT != MVT::i32 && RetVT != MVT::i64)
  1202. return 0;
  1203. // Don't deal with undefined shifts.
  1204. if (ShiftImm >= RetVT.getSizeInBits())
  1205. return 0;
  1206. static const unsigned OpcTable[2][2][2] = {
  1207. { { AArch64::SUBWrs, AArch64::SUBXrs },
  1208. { AArch64::ADDWrs, AArch64::ADDXrs } },
  1209. { { AArch64::SUBSWrs, AArch64::SUBSXrs },
  1210. { AArch64::ADDSWrs, AArch64::ADDSXrs } }
  1211. };
  1212. bool Is64Bit = RetVT == MVT::i64;
  1213. unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
  1214. const TargetRegisterClass *RC =
  1215. Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  1216. unsigned ResultReg;
  1217. if (WantResult)
  1218. ResultReg = createResultReg(RC);
  1219. else
  1220. ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
  1221. const MCInstrDesc &II = TII.get(Opc);
  1222. LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
  1223. RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
  1224. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1225. .addReg(LHSReg)
  1226. .addReg(RHSReg)
  1227. .addImm(getShifterImm(ShiftType, ShiftImm));
  1228. return ResultReg;
  1229. }
  1230. unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
  1231. unsigned RHSReg,
  1232. AArch64_AM::ShiftExtendType ExtType,
  1233. uint64_t ShiftImm, bool SetFlags,
  1234. bool WantResult) {
  1235. assert(LHSReg && RHSReg && "Invalid register number.");
  1236. assert(LHSReg != AArch64::XZR && LHSReg != AArch64::WZR &&
  1237. RHSReg != AArch64::XZR && RHSReg != AArch64::WZR);
  1238. if (RetVT != MVT::i32 && RetVT != MVT::i64)
  1239. return 0;
  1240. if (ShiftImm >= 4)
  1241. return 0;
  1242. static const unsigned OpcTable[2][2][2] = {
  1243. { { AArch64::SUBWrx, AArch64::SUBXrx },
  1244. { AArch64::ADDWrx, AArch64::ADDXrx } },
  1245. { { AArch64::SUBSWrx, AArch64::SUBSXrx },
  1246. { AArch64::ADDSWrx, AArch64::ADDSXrx } }
  1247. };
  1248. bool Is64Bit = RetVT == MVT::i64;
  1249. unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
  1250. const TargetRegisterClass *RC = nullptr;
  1251. if (SetFlags)
  1252. RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  1253. else
  1254. RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
  1255. unsigned ResultReg;
  1256. if (WantResult)
  1257. ResultReg = createResultReg(RC);
  1258. else
  1259. ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
  1260. const MCInstrDesc &II = TII.get(Opc);
  1261. LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
  1262. RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
  1263. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1264. .addReg(LHSReg)
  1265. .addReg(RHSReg)
  1266. .addImm(getArithExtendImm(ExtType, ShiftImm));
  1267. return ResultReg;
  1268. }
  1269. bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
  1270. Type *Ty = LHS->getType();
  1271. EVT EVT = TLI.getValueType(DL, Ty, true);
  1272. if (!EVT.isSimple())
  1273. return false;
  1274. MVT VT = EVT.getSimpleVT();
  1275. switch (VT.SimpleTy) {
  1276. default:
  1277. return false;
  1278. case MVT::i1:
  1279. case MVT::i8:
  1280. case MVT::i16:
  1281. case MVT::i32:
  1282. case MVT::i64:
  1283. return emitICmp(VT, LHS, RHS, IsZExt);
  1284. case MVT::f32:
  1285. case MVT::f64:
  1286. return emitFCmp(VT, LHS, RHS);
  1287. }
  1288. }
  1289. bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
  1290. bool IsZExt) {
  1291. return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
  1292. IsZExt) != 0;
  1293. }
  1294. bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, uint64_t Imm) {
  1295. return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, Imm,
  1296. /*SetFlags=*/true, /*WantResult=*/false) != 0;
  1297. }
  1298. bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
  1299. if (RetVT != MVT::f32 && RetVT != MVT::f64)
  1300. return false;
  1301. // Check to see if the 2nd operand is a constant that we can encode directly
  1302. // in the compare.
  1303. bool UseImm = false;
  1304. if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
  1305. if (CFP->isZero() && !CFP->isNegative())
  1306. UseImm = true;
  1307. Register LHSReg = getRegForValue(LHS);
  1308. if (!LHSReg)
  1309. return false;
  1310. if (UseImm) {
  1311. unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
  1312. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
  1313. .addReg(LHSReg);
  1314. return true;
  1315. }
  1316. Register RHSReg = getRegForValue(RHS);
  1317. if (!RHSReg)
  1318. return false;
  1319. unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
  1320. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
  1321. .addReg(LHSReg)
  1322. .addReg(RHSReg);
  1323. return true;
  1324. }
  1325. unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
  1326. bool SetFlags, bool WantResult, bool IsZExt) {
  1327. return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
  1328. IsZExt);
  1329. }
  1330. /// This method is a wrapper to simplify add emission.
  1331. ///
  1332. /// First try to emit an add with an immediate operand using emitAddSub_ri. If
  1333. /// that fails, then try to materialize the immediate into a register and use
  1334. /// emitAddSub_rr instead.
  1335. unsigned AArch64FastISel::emitAdd_ri_(MVT VT, unsigned Op0, int64_t Imm) {
  1336. unsigned ResultReg;
  1337. if (Imm < 0)
  1338. ResultReg = emitAddSub_ri(false, VT, Op0, -Imm);
  1339. else
  1340. ResultReg = emitAddSub_ri(true, VT, Op0, Imm);
  1341. if (ResultReg)
  1342. return ResultReg;
  1343. unsigned CReg = fastEmit_i(VT, VT, ISD::Constant, Imm);
  1344. if (!CReg)
  1345. return 0;
  1346. ResultReg = emitAddSub_rr(true, VT, Op0, CReg);
  1347. return ResultReg;
  1348. }
  1349. unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
  1350. bool SetFlags, bool WantResult, bool IsZExt) {
  1351. return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
  1352. IsZExt);
  1353. }
  1354. unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
  1355. unsigned RHSReg, bool WantResult) {
  1356. return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, RHSReg,
  1357. /*SetFlags=*/true, WantResult);
  1358. }
  1359. unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
  1360. unsigned RHSReg,
  1361. AArch64_AM::ShiftExtendType ShiftType,
  1362. uint64_t ShiftImm, bool WantResult) {
  1363. return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, RHSReg, ShiftType,
  1364. ShiftImm, /*SetFlags=*/true, WantResult);
  1365. }
  1366. unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
  1367. const Value *LHS, const Value *RHS) {
  1368. // Canonicalize immediates to the RHS first.
  1369. if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
  1370. std::swap(LHS, RHS);
  1371. // Canonicalize mul by power-of-2 to the RHS.
  1372. if (LHS->hasOneUse() && isValueAvailable(LHS))
  1373. if (isMulPowOf2(LHS))
  1374. std::swap(LHS, RHS);
  1375. // Canonicalize shift immediate to the RHS.
  1376. if (LHS->hasOneUse() && isValueAvailable(LHS))
  1377. if (const auto *SI = dyn_cast<ShlOperator>(LHS))
  1378. if (isa<ConstantInt>(SI->getOperand(1)))
  1379. std::swap(LHS, RHS);
  1380. Register LHSReg = getRegForValue(LHS);
  1381. if (!LHSReg)
  1382. return 0;
  1383. unsigned ResultReg = 0;
  1384. if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
  1385. uint64_t Imm = C->getZExtValue();
  1386. ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, Imm);
  1387. }
  1388. if (ResultReg)
  1389. return ResultReg;
  1390. // Check if the mul can be folded into the instruction.
  1391. if (RHS->hasOneUse() && isValueAvailable(RHS)) {
  1392. if (isMulPowOf2(RHS)) {
  1393. const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
  1394. const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
  1395. if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
  1396. if (C->getValue().isPowerOf2())
  1397. std::swap(MulLHS, MulRHS);
  1398. assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
  1399. uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
  1400. Register RHSReg = getRegForValue(MulLHS);
  1401. if (!RHSReg)
  1402. return 0;
  1403. ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, RHSReg, ShiftVal);
  1404. if (ResultReg)
  1405. return ResultReg;
  1406. }
  1407. }
  1408. // Check if the shift can be folded into the instruction.
  1409. if (RHS->hasOneUse() && isValueAvailable(RHS)) {
  1410. if (const auto *SI = dyn_cast<ShlOperator>(RHS))
  1411. if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
  1412. uint64_t ShiftVal = C->getZExtValue();
  1413. Register RHSReg = getRegForValue(SI->getOperand(0));
  1414. if (!RHSReg)
  1415. return 0;
  1416. ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, RHSReg, ShiftVal);
  1417. if (ResultReg)
  1418. return ResultReg;
  1419. }
  1420. }
  1421. Register RHSReg = getRegForValue(RHS);
  1422. if (!RHSReg)
  1423. return 0;
  1424. MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
  1425. ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, RHSReg);
  1426. if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
  1427. uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
  1428. ResultReg = emitAnd_ri(MVT::i32, ResultReg, Mask);
  1429. }
  1430. return ResultReg;
  1431. }
  1432. unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
  1433. unsigned LHSReg, uint64_t Imm) {
  1434. static_assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR),
  1435. "ISD nodes are not consecutive!");
  1436. static const unsigned OpcTable[3][2] = {
  1437. { AArch64::ANDWri, AArch64::ANDXri },
  1438. { AArch64::ORRWri, AArch64::ORRXri },
  1439. { AArch64::EORWri, AArch64::EORXri }
  1440. };
  1441. const TargetRegisterClass *RC;
  1442. unsigned Opc;
  1443. unsigned RegSize;
  1444. switch (RetVT.SimpleTy) {
  1445. default:
  1446. return 0;
  1447. case MVT::i1:
  1448. case MVT::i8:
  1449. case MVT::i16:
  1450. case MVT::i32: {
  1451. unsigned Idx = ISDOpc - ISD::AND;
  1452. Opc = OpcTable[Idx][0];
  1453. RC = &AArch64::GPR32spRegClass;
  1454. RegSize = 32;
  1455. break;
  1456. }
  1457. case MVT::i64:
  1458. Opc = OpcTable[ISDOpc - ISD::AND][1];
  1459. RC = &AArch64::GPR64spRegClass;
  1460. RegSize = 64;
  1461. break;
  1462. }
  1463. if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
  1464. return 0;
  1465. Register ResultReg =
  1466. fastEmitInst_ri(Opc, RC, LHSReg,
  1467. AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
  1468. if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
  1469. uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
  1470. ResultReg = emitAnd_ri(MVT::i32, ResultReg, Mask);
  1471. }
  1472. return ResultReg;
  1473. }
  1474. unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
  1475. unsigned LHSReg, unsigned RHSReg,
  1476. uint64_t ShiftImm) {
  1477. static_assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR),
  1478. "ISD nodes are not consecutive!");
  1479. static const unsigned OpcTable[3][2] = {
  1480. { AArch64::ANDWrs, AArch64::ANDXrs },
  1481. { AArch64::ORRWrs, AArch64::ORRXrs },
  1482. { AArch64::EORWrs, AArch64::EORXrs }
  1483. };
  1484. // Don't deal with undefined shifts.
  1485. if (ShiftImm >= RetVT.getSizeInBits())
  1486. return 0;
  1487. const TargetRegisterClass *RC;
  1488. unsigned Opc;
  1489. switch (RetVT.SimpleTy) {
  1490. default:
  1491. return 0;
  1492. case MVT::i1:
  1493. case MVT::i8:
  1494. case MVT::i16:
  1495. case MVT::i32:
  1496. Opc = OpcTable[ISDOpc - ISD::AND][0];
  1497. RC = &AArch64::GPR32RegClass;
  1498. break;
  1499. case MVT::i64:
  1500. Opc = OpcTable[ISDOpc - ISD::AND][1];
  1501. RC = &AArch64::GPR64RegClass;
  1502. break;
  1503. }
  1504. Register ResultReg =
  1505. fastEmitInst_rri(Opc, RC, LHSReg, RHSReg,
  1506. AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
  1507. if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
  1508. uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
  1509. ResultReg = emitAnd_ri(MVT::i32, ResultReg, Mask);
  1510. }
  1511. return ResultReg;
  1512. }
  1513. unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg,
  1514. uint64_t Imm) {
  1515. return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, Imm);
  1516. }
  1517. unsigned AArch64FastISel::emitLoad(MVT VT, MVT RetVT, Address Addr,
  1518. bool WantZExt, MachineMemOperand *MMO) {
  1519. if (!TLI.allowsMisalignedMemoryAccesses(VT))
  1520. return 0;
  1521. // Simplify this down to something we can handle.
  1522. if (!simplifyAddress(Addr, VT))
  1523. return 0;
  1524. unsigned ScaleFactor = getImplicitScaleFactor(VT);
  1525. if (!ScaleFactor)
  1526. llvm_unreachable("Unexpected value type.");
  1527. // Negative offsets require unscaled, 9-bit, signed immediate offsets.
  1528. // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
  1529. bool UseScaled = true;
  1530. if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
  1531. UseScaled = false;
  1532. ScaleFactor = 1;
  1533. }
  1534. static const unsigned GPOpcTable[2][8][4] = {
  1535. // Sign-extend.
  1536. { { AArch64::LDURSBWi, AArch64::LDURSHWi, AArch64::LDURWi,
  1537. AArch64::LDURXi },
  1538. { AArch64::LDURSBXi, AArch64::LDURSHXi, AArch64::LDURSWi,
  1539. AArch64::LDURXi },
  1540. { AArch64::LDRSBWui, AArch64::LDRSHWui, AArch64::LDRWui,
  1541. AArch64::LDRXui },
  1542. { AArch64::LDRSBXui, AArch64::LDRSHXui, AArch64::LDRSWui,
  1543. AArch64::LDRXui },
  1544. { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
  1545. AArch64::LDRXroX },
  1546. { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
  1547. AArch64::LDRXroX },
  1548. { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
  1549. AArch64::LDRXroW },
  1550. { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
  1551. AArch64::LDRXroW }
  1552. },
  1553. // Zero-extend.
  1554. { { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
  1555. AArch64::LDURXi },
  1556. { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
  1557. AArch64::LDURXi },
  1558. { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
  1559. AArch64::LDRXui },
  1560. { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
  1561. AArch64::LDRXui },
  1562. { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
  1563. AArch64::LDRXroX },
  1564. { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
  1565. AArch64::LDRXroX },
  1566. { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
  1567. AArch64::LDRXroW },
  1568. { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
  1569. AArch64::LDRXroW }
  1570. }
  1571. };
  1572. static const unsigned FPOpcTable[4][2] = {
  1573. { AArch64::LDURSi, AArch64::LDURDi },
  1574. { AArch64::LDRSui, AArch64::LDRDui },
  1575. { AArch64::LDRSroX, AArch64::LDRDroX },
  1576. { AArch64::LDRSroW, AArch64::LDRDroW }
  1577. };
  1578. unsigned Opc;
  1579. const TargetRegisterClass *RC;
  1580. bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
  1581. Addr.getOffsetReg();
  1582. unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
  1583. if (Addr.getExtendType() == AArch64_AM::UXTW ||
  1584. Addr.getExtendType() == AArch64_AM::SXTW)
  1585. Idx++;
  1586. bool IsRet64Bit = RetVT == MVT::i64;
  1587. switch (VT.SimpleTy) {
  1588. default:
  1589. llvm_unreachable("Unexpected value type.");
  1590. case MVT::i1: // Intentional fall-through.
  1591. case MVT::i8:
  1592. Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
  1593. RC = (IsRet64Bit && !WantZExt) ?
  1594. &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
  1595. break;
  1596. case MVT::i16:
  1597. Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
  1598. RC = (IsRet64Bit && !WantZExt) ?
  1599. &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
  1600. break;
  1601. case MVT::i32:
  1602. Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
  1603. RC = (IsRet64Bit && !WantZExt) ?
  1604. &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
  1605. break;
  1606. case MVT::i64:
  1607. Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
  1608. RC = &AArch64::GPR64RegClass;
  1609. break;
  1610. case MVT::f32:
  1611. Opc = FPOpcTable[Idx][0];
  1612. RC = &AArch64::FPR32RegClass;
  1613. break;
  1614. case MVT::f64:
  1615. Opc = FPOpcTable[Idx][1];
  1616. RC = &AArch64::FPR64RegClass;
  1617. break;
  1618. }
  1619. // Create the base instruction, then add the operands.
  1620. Register ResultReg = createResultReg(RC);
  1621. MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1622. TII.get(Opc), ResultReg);
  1623. addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
  1624. // Loading an i1 requires special handling.
  1625. if (VT == MVT::i1) {
  1626. unsigned ANDReg = emitAnd_ri(MVT::i32, ResultReg, 1);
  1627. assert(ANDReg && "Unexpected AND instruction emission failure.");
  1628. ResultReg = ANDReg;
  1629. }
  1630. // For zero-extending loads to 64bit we emit a 32bit load and then convert
  1631. // the 32bit reg to a 64bit reg.
  1632. if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
  1633. Register Reg64 = createResultReg(&AArch64::GPR64RegClass);
  1634. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1635. TII.get(AArch64::SUBREG_TO_REG), Reg64)
  1636. .addImm(0)
  1637. .addReg(ResultReg, getKillRegState(true))
  1638. .addImm(AArch64::sub_32);
  1639. ResultReg = Reg64;
  1640. }
  1641. return ResultReg;
  1642. }
  1643. bool AArch64FastISel::selectAddSub(const Instruction *I) {
  1644. MVT VT;
  1645. if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
  1646. return false;
  1647. if (VT.isVector())
  1648. return selectOperator(I, I->getOpcode());
  1649. unsigned ResultReg;
  1650. switch (I->getOpcode()) {
  1651. default:
  1652. llvm_unreachable("Unexpected instruction.");
  1653. case Instruction::Add:
  1654. ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
  1655. break;
  1656. case Instruction::Sub:
  1657. ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
  1658. break;
  1659. }
  1660. if (!ResultReg)
  1661. return false;
  1662. updateValueMap(I, ResultReg);
  1663. return true;
  1664. }
  1665. bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
  1666. MVT VT;
  1667. if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
  1668. return false;
  1669. if (VT.isVector())
  1670. return selectOperator(I, I->getOpcode());
  1671. unsigned ResultReg;
  1672. switch (I->getOpcode()) {
  1673. default:
  1674. llvm_unreachable("Unexpected instruction.");
  1675. case Instruction::And:
  1676. ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
  1677. break;
  1678. case Instruction::Or:
  1679. ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
  1680. break;
  1681. case Instruction::Xor:
  1682. ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
  1683. break;
  1684. }
  1685. if (!ResultReg)
  1686. return false;
  1687. updateValueMap(I, ResultReg);
  1688. return true;
  1689. }
  1690. bool AArch64FastISel::selectLoad(const Instruction *I) {
  1691. MVT VT;
  1692. // Verify we have a legal type before going any further. Currently, we handle
  1693. // simple types that will directly fit in a register (i32/f32/i64/f64) or
  1694. // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
  1695. if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
  1696. cast<LoadInst>(I)->isAtomic())
  1697. return false;
  1698. const Value *SV = I->getOperand(0);
  1699. if (TLI.supportSwiftError()) {
  1700. // Swifterror values can come from either a function parameter with
  1701. // swifterror attribute or an alloca with swifterror attribute.
  1702. if (const Argument *Arg = dyn_cast<Argument>(SV)) {
  1703. if (Arg->hasSwiftErrorAttr())
  1704. return false;
  1705. }
  1706. if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
  1707. if (Alloca->isSwiftError())
  1708. return false;
  1709. }
  1710. }
  1711. // See if we can handle this address.
  1712. Address Addr;
  1713. if (!computeAddress(I->getOperand(0), Addr, I->getType()))
  1714. return false;
  1715. // Fold the following sign-/zero-extend into the load instruction.
  1716. bool WantZExt = true;
  1717. MVT RetVT = VT;
  1718. const Value *IntExtVal = nullptr;
  1719. if (I->hasOneUse()) {
  1720. if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
  1721. if (isTypeSupported(ZE->getType(), RetVT))
  1722. IntExtVal = ZE;
  1723. else
  1724. RetVT = VT;
  1725. } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
  1726. if (isTypeSupported(SE->getType(), RetVT))
  1727. IntExtVal = SE;
  1728. else
  1729. RetVT = VT;
  1730. WantZExt = false;
  1731. }
  1732. }
  1733. unsigned ResultReg =
  1734. emitLoad(VT, RetVT, Addr, WantZExt, createMachineMemOperandFor(I));
  1735. if (!ResultReg)
  1736. return false;
  1737. // There are a few different cases we have to handle, because the load or the
  1738. // sign-/zero-extend might not be selected by FastISel if we fall-back to
  1739. // SelectionDAG. There is also an ordering issue when both instructions are in
  1740. // different basic blocks.
  1741. // 1.) The load instruction is selected by FastISel, but the integer extend
  1742. // not. This usually happens when the integer extend is in a different
  1743. // basic block and SelectionDAG took over for that basic block.
  1744. // 2.) The load instruction is selected before the integer extend. This only
  1745. // happens when the integer extend is in a different basic block.
  1746. // 3.) The load instruction is selected by SelectionDAG and the integer extend
  1747. // by FastISel. This happens if there are instructions between the load
  1748. // and the integer extend that couldn't be selected by FastISel.
  1749. if (IntExtVal) {
  1750. // The integer extend hasn't been emitted yet. FastISel or SelectionDAG
  1751. // could select it. Emit a copy to subreg if necessary. FastISel will remove
  1752. // it when it selects the integer extend.
  1753. Register Reg = lookUpRegForValue(IntExtVal);
  1754. auto *MI = MRI.getUniqueVRegDef(Reg);
  1755. if (!MI) {
  1756. if (RetVT == MVT::i64 && VT <= MVT::i32) {
  1757. if (WantZExt) {
  1758. // Delete the last emitted instruction from emitLoad (SUBREG_TO_REG).
  1759. MachineBasicBlock::iterator I(std::prev(FuncInfo.InsertPt));
  1760. ResultReg = std::prev(I)->getOperand(0).getReg();
  1761. removeDeadCode(I, std::next(I));
  1762. } else
  1763. ResultReg = fastEmitInst_extractsubreg(MVT::i32, ResultReg,
  1764. AArch64::sub_32);
  1765. }
  1766. updateValueMap(I, ResultReg);
  1767. return true;
  1768. }
  1769. // The integer extend has already been emitted - delete all the instructions
  1770. // that have been emitted by the integer extend lowering code and use the
  1771. // result from the load instruction directly.
  1772. while (MI) {
  1773. Reg = 0;
  1774. for (auto &Opnd : MI->uses()) {
  1775. if (Opnd.isReg()) {
  1776. Reg = Opnd.getReg();
  1777. break;
  1778. }
  1779. }
  1780. MachineBasicBlock::iterator I(MI);
  1781. removeDeadCode(I, std::next(I));
  1782. MI = nullptr;
  1783. if (Reg)
  1784. MI = MRI.getUniqueVRegDef(Reg);
  1785. }
  1786. updateValueMap(IntExtVal, ResultReg);
  1787. return true;
  1788. }
  1789. updateValueMap(I, ResultReg);
  1790. return true;
  1791. }
  1792. bool AArch64FastISel::emitStoreRelease(MVT VT, unsigned SrcReg,
  1793. unsigned AddrReg,
  1794. MachineMemOperand *MMO) {
  1795. unsigned Opc;
  1796. switch (VT.SimpleTy) {
  1797. default: return false;
  1798. case MVT::i8: Opc = AArch64::STLRB; break;
  1799. case MVT::i16: Opc = AArch64::STLRH; break;
  1800. case MVT::i32: Opc = AArch64::STLRW; break;
  1801. case MVT::i64: Opc = AArch64::STLRX; break;
  1802. }
  1803. const MCInstrDesc &II = TII.get(Opc);
  1804. SrcReg = constrainOperandRegClass(II, SrcReg, 0);
  1805. AddrReg = constrainOperandRegClass(II, AddrReg, 1);
  1806. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1807. .addReg(SrcReg)
  1808. .addReg(AddrReg)
  1809. .addMemOperand(MMO);
  1810. return true;
  1811. }
  1812. bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
  1813. MachineMemOperand *MMO) {
  1814. if (!TLI.allowsMisalignedMemoryAccesses(VT))
  1815. return false;
  1816. // Simplify this down to something we can handle.
  1817. if (!simplifyAddress(Addr, VT))
  1818. return false;
  1819. unsigned ScaleFactor = getImplicitScaleFactor(VT);
  1820. if (!ScaleFactor)
  1821. llvm_unreachable("Unexpected value type.");
  1822. // Negative offsets require unscaled, 9-bit, signed immediate offsets.
  1823. // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
  1824. bool UseScaled = true;
  1825. if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
  1826. UseScaled = false;
  1827. ScaleFactor = 1;
  1828. }
  1829. static const unsigned OpcTable[4][6] = {
  1830. { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
  1831. AArch64::STURSi, AArch64::STURDi },
  1832. { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
  1833. AArch64::STRSui, AArch64::STRDui },
  1834. { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
  1835. AArch64::STRSroX, AArch64::STRDroX },
  1836. { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
  1837. AArch64::STRSroW, AArch64::STRDroW }
  1838. };
  1839. unsigned Opc;
  1840. bool VTIsi1 = false;
  1841. bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
  1842. Addr.getOffsetReg();
  1843. unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
  1844. if (Addr.getExtendType() == AArch64_AM::UXTW ||
  1845. Addr.getExtendType() == AArch64_AM::SXTW)
  1846. Idx++;
  1847. switch (VT.SimpleTy) {
  1848. default: llvm_unreachable("Unexpected value type.");
  1849. case MVT::i1: VTIsi1 = true; LLVM_FALLTHROUGH;
  1850. case MVT::i8: Opc = OpcTable[Idx][0]; break;
  1851. case MVT::i16: Opc = OpcTable[Idx][1]; break;
  1852. case MVT::i32: Opc = OpcTable[Idx][2]; break;
  1853. case MVT::i64: Opc = OpcTable[Idx][3]; break;
  1854. case MVT::f32: Opc = OpcTable[Idx][4]; break;
  1855. case MVT::f64: Opc = OpcTable[Idx][5]; break;
  1856. }
  1857. // Storing an i1 requires special handling.
  1858. if (VTIsi1 && SrcReg != AArch64::WZR) {
  1859. unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, 1);
  1860. assert(ANDReg && "Unexpected AND instruction emission failure.");
  1861. SrcReg = ANDReg;
  1862. }
  1863. // Create the base instruction, then add the operands.
  1864. const MCInstrDesc &II = TII.get(Opc);
  1865. SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
  1866. MachineInstrBuilder MIB =
  1867. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
  1868. addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
  1869. return true;
  1870. }
  1871. bool AArch64FastISel::selectStore(const Instruction *I) {
  1872. MVT VT;
  1873. const Value *Op0 = I->getOperand(0);
  1874. // Verify we have a legal type before going any further. Currently, we handle
  1875. // simple types that will directly fit in a register (i32/f32/i64/f64) or
  1876. // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
  1877. if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true))
  1878. return false;
  1879. const Value *PtrV = I->getOperand(1);
  1880. if (TLI.supportSwiftError()) {
  1881. // Swifterror values can come from either a function parameter with
  1882. // swifterror attribute or an alloca with swifterror attribute.
  1883. if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
  1884. if (Arg->hasSwiftErrorAttr())
  1885. return false;
  1886. }
  1887. if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
  1888. if (Alloca->isSwiftError())
  1889. return false;
  1890. }
  1891. }
  1892. // Get the value to be stored into a register. Use the zero register directly
  1893. // when possible to avoid an unnecessary copy and a wasted register.
  1894. unsigned SrcReg = 0;
  1895. if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
  1896. if (CI->isZero())
  1897. SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
  1898. } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
  1899. if (CF->isZero() && !CF->isNegative()) {
  1900. VT = MVT::getIntegerVT(VT.getSizeInBits());
  1901. SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
  1902. }
  1903. }
  1904. if (!SrcReg)
  1905. SrcReg = getRegForValue(Op0);
  1906. if (!SrcReg)
  1907. return false;
  1908. auto *SI = cast<StoreInst>(I);
  1909. // Try to emit a STLR for seq_cst/release.
  1910. if (SI->isAtomic()) {
  1911. AtomicOrdering Ord = SI->getOrdering();
  1912. // The non-atomic instructions are sufficient for relaxed stores.
  1913. if (isReleaseOrStronger(Ord)) {
  1914. // The STLR addressing mode only supports a base reg; pass that directly.
  1915. Register AddrReg = getRegForValue(PtrV);
  1916. return emitStoreRelease(VT, SrcReg, AddrReg,
  1917. createMachineMemOperandFor(I));
  1918. }
  1919. }
  1920. // See if we can handle this address.
  1921. Address Addr;
  1922. if (!computeAddress(PtrV, Addr, Op0->getType()))
  1923. return false;
  1924. if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
  1925. return false;
  1926. return true;
  1927. }
  1928. static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
  1929. switch (Pred) {
  1930. case CmpInst::FCMP_ONE:
  1931. case CmpInst::FCMP_UEQ:
  1932. default:
  1933. // AL is our "false" for now. The other two need more compares.
  1934. return AArch64CC::AL;
  1935. case CmpInst::ICMP_EQ:
  1936. case CmpInst::FCMP_OEQ:
  1937. return AArch64CC::EQ;
  1938. case CmpInst::ICMP_SGT:
  1939. case CmpInst::FCMP_OGT:
  1940. return AArch64CC::GT;
  1941. case CmpInst::ICMP_SGE:
  1942. case CmpInst::FCMP_OGE:
  1943. return AArch64CC::GE;
  1944. case CmpInst::ICMP_UGT:
  1945. case CmpInst::FCMP_UGT:
  1946. return AArch64CC::HI;
  1947. case CmpInst::FCMP_OLT:
  1948. return AArch64CC::MI;
  1949. case CmpInst::ICMP_ULE:
  1950. case CmpInst::FCMP_OLE:
  1951. return AArch64CC::LS;
  1952. case CmpInst::FCMP_ORD:
  1953. return AArch64CC::VC;
  1954. case CmpInst::FCMP_UNO:
  1955. return AArch64CC::VS;
  1956. case CmpInst::FCMP_UGE:
  1957. return AArch64CC::PL;
  1958. case CmpInst::ICMP_SLT:
  1959. case CmpInst::FCMP_ULT:
  1960. return AArch64CC::LT;
  1961. case CmpInst::ICMP_SLE:
  1962. case CmpInst::FCMP_ULE:
  1963. return AArch64CC::LE;
  1964. case CmpInst::FCMP_UNE:
  1965. case CmpInst::ICMP_NE:
  1966. return AArch64CC::NE;
  1967. case CmpInst::ICMP_UGE:
  1968. return AArch64CC::HS;
  1969. case CmpInst::ICMP_ULT:
  1970. return AArch64CC::LO;
  1971. }
  1972. }
  1973. /// Try to emit a combined compare-and-branch instruction.
  1974. bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
  1975. // Speculation tracking/SLH assumes that optimized TB(N)Z/CB(N)Z instructions
  1976. // will not be produced, as they are conditional branch instructions that do
  1977. // not set flags.
  1978. if (FuncInfo.MF->getFunction().hasFnAttribute(
  1979. Attribute::SpeculativeLoadHardening))
  1980. return false;
  1981. assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
  1982. const CmpInst *CI = cast<CmpInst>(BI->getCondition());
  1983. CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
  1984. const Value *LHS = CI->getOperand(0);
  1985. const Value *RHS = CI->getOperand(1);
  1986. MVT VT;
  1987. if (!isTypeSupported(LHS->getType(), VT))
  1988. return false;
  1989. unsigned BW = VT.getSizeInBits();
  1990. if (BW > 64)
  1991. return false;
  1992. MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
  1993. MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
  1994. // Try to take advantage of fallthrough opportunities.
  1995. if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
  1996. std::swap(TBB, FBB);
  1997. Predicate = CmpInst::getInversePredicate(Predicate);
  1998. }
  1999. int TestBit = -1;
  2000. bool IsCmpNE;
  2001. switch (Predicate) {
  2002. default:
  2003. return false;
  2004. case CmpInst::ICMP_EQ:
  2005. case CmpInst::ICMP_NE:
  2006. if (isa<Constant>(LHS) && cast<Constant>(LHS)->isNullValue())
  2007. std::swap(LHS, RHS);
  2008. if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
  2009. return false;
  2010. if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
  2011. if (AI->getOpcode() == Instruction::And && isValueAvailable(AI)) {
  2012. const Value *AndLHS = AI->getOperand(0);
  2013. const Value *AndRHS = AI->getOperand(1);
  2014. if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
  2015. if (C->getValue().isPowerOf2())
  2016. std::swap(AndLHS, AndRHS);
  2017. if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
  2018. if (C->getValue().isPowerOf2()) {
  2019. TestBit = C->getValue().logBase2();
  2020. LHS = AndLHS;
  2021. }
  2022. }
  2023. if (VT == MVT::i1)
  2024. TestBit = 0;
  2025. IsCmpNE = Predicate == CmpInst::ICMP_NE;
  2026. break;
  2027. case CmpInst::ICMP_SLT:
  2028. case CmpInst::ICMP_SGE:
  2029. if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
  2030. return false;
  2031. TestBit = BW - 1;
  2032. IsCmpNE = Predicate == CmpInst::ICMP_SLT;
  2033. break;
  2034. case CmpInst::ICMP_SGT:
  2035. case CmpInst::ICMP_SLE:
  2036. if (!isa<ConstantInt>(RHS))
  2037. return false;
  2038. if (cast<ConstantInt>(RHS)->getValue() != APInt(BW, -1, true))
  2039. return false;
  2040. TestBit = BW - 1;
  2041. IsCmpNE = Predicate == CmpInst::ICMP_SLE;
  2042. break;
  2043. } // end switch
  2044. static const unsigned OpcTable[2][2][2] = {
  2045. { {AArch64::CBZW, AArch64::CBZX },
  2046. {AArch64::CBNZW, AArch64::CBNZX} },
  2047. { {AArch64::TBZW, AArch64::TBZX },
  2048. {AArch64::TBNZW, AArch64::TBNZX} }
  2049. };
  2050. bool IsBitTest = TestBit != -1;
  2051. bool Is64Bit = BW == 64;
  2052. if (TestBit < 32 && TestBit >= 0)
  2053. Is64Bit = false;
  2054. unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
  2055. const MCInstrDesc &II = TII.get(Opc);
  2056. Register SrcReg = getRegForValue(LHS);
  2057. if (!SrcReg)
  2058. return false;
  2059. if (BW == 64 && !Is64Bit)
  2060. SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, AArch64::sub_32);
  2061. if ((BW < 32) && !IsBitTest)
  2062. SrcReg = emitIntExt(VT, SrcReg, MVT::i32, /*isZExt=*/true);
  2063. // Emit the combined compare and branch instruction.
  2064. SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
  2065. MachineInstrBuilder MIB =
  2066. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
  2067. .addReg(SrcReg);
  2068. if (IsBitTest)
  2069. MIB.addImm(TestBit);
  2070. MIB.addMBB(TBB);
  2071. finishCondBranch(BI->getParent(), TBB, FBB);
  2072. return true;
  2073. }
  2074. bool AArch64FastISel::selectBranch(const Instruction *I) {
  2075. const BranchInst *BI = cast<BranchInst>(I);
  2076. if (BI->isUnconditional()) {
  2077. MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
  2078. fastEmitBranch(MSucc, BI->getDebugLoc());
  2079. return true;
  2080. }
  2081. MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
  2082. MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
  2083. if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
  2084. if (CI->hasOneUse() && isValueAvailable(CI)) {
  2085. // Try to optimize or fold the cmp.
  2086. CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
  2087. switch (Predicate) {
  2088. default:
  2089. break;
  2090. case CmpInst::FCMP_FALSE:
  2091. fastEmitBranch(FBB, DbgLoc);
  2092. return true;
  2093. case CmpInst::FCMP_TRUE:
  2094. fastEmitBranch(TBB, DbgLoc);
  2095. return true;
  2096. }
  2097. // Try to emit a combined compare-and-branch first.
  2098. if (emitCompareAndBranch(BI))
  2099. return true;
  2100. // Try to take advantage of fallthrough opportunities.
  2101. if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
  2102. std::swap(TBB, FBB);
  2103. Predicate = CmpInst::getInversePredicate(Predicate);
  2104. }
  2105. // Emit the cmp.
  2106. if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
  2107. return false;
  2108. // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
  2109. // instruction.
  2110. AArch64CC::CondCode CC = getCompareCC(Predicate);
  2111. AArch64CC::CondCode ExtraCC = AArch64CC::AL;
  2112. switch (Predicate) {
  2113. default:
  2114. break;
  2115. case CmpInst::FCMP_UEQ:
  2116. ExtraCC = AArch64CC::EQ;
  2117. CC = AArch64CC::VS;
  2118. break;
  2119. case CmpInst::FCMP_ONE:
  2120. ExtraCC = AArch64CC::MI;
  2121. CC = AArch64CC::GT;
  2122. break;
  2123. }
  2124. assert((CC != AArch64CC::AL) && "Unexpected condition code.");
  2125. // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
  2126. if (ExtraCC != AArch64CC::AL) {
  2127. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
  2128. .addImm(ExtraCC)
  2129. .addMBB(TBB);
  2130. }
  2131. // Emit the branch.
  2132. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
  2133. .addImm(CC)
  2134. .addMBB(TBB);
  2135. finishCondBranch(BI->getParent(), TBB, FBB);
  2136. return true;
  2137. }
  2138. } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
  2139. uint64_t Imm = CI->getZExtValue();
  2140. MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
  2141. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
  2142. .addMBB(Target);
  2143. // Obtain the branch probability and add the target to the successor list.
  2144. if (FuncInfo.BPI) {
  2145. auto BranchProbability = FuncInfo.BPI->getEdgeProbability(
  2146. BI->getParent(), Target->getBasicBlock());
  2147. FuncInfo.MBB->addSuccessor(Target, BranchProbability);
  2148. } else
  2149. FuncInfo.MBB->addSuccessorWithoutProb(Target);
  2150. return true;
  2151. } else {
  2152. AArch64CC::CondCode CC = AArch64CC::NE;
  2153. if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
  2154. // Fake request the condition, otherwise the intrinsic might be completely
  2155. // optimized away.
  2156. Register CondReg = getRegForValue(BI->getCondition());
  2157. if (!CondReg)
  2158. return false;
  2159. // Emit the branch.
  2160. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
  2161. .addImm(CC)
  2162. .addMBB(TBB);
  2163. finishCondBranch(BI->getParent(), TBB, FBB);
  2164. return true;
  2165. }
  2166. }
  2167. Register CondReg = getRegForValue(BI->getCondition());
  2168. if (CondReg == 0)
  2169. return false;
  2170. // i1 conditions come as i32 values, test the lowest bit with tb(n)z.
  2171. unsigned Opcode = AArch64::TBNZW;
  2172. if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
  2173. std::swap(TBB, FBB);
  2174. Opcode = AArch64::TBZW;
  2175. }
  2176. const MCInstrDesc &II = TII.get(Opcode);
  2177. Register ConstrainedCondReg
  2178. = constrainOperandRegClass(II, CondReg, II.getNumDefs());
  2179. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  2180. .addReg(ConstrainedCondReg)
  2181. .addImm(0)
  2182. .addMBB(TBB);
  2183. finishCondBranch(BI->getParent(), TBB, FBB);
  2184. return true;
  2185. }
  2186. bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
  2187. const IndirectBrInst *BI = cast<IndirectBrInst>(I);
  2188. Register AddrReg = getRegForValue(BI->getOperand(0));
  2189. if (AddrReg == 0)
  2190. return false;
  2191. // Emit the indirect branch.
  2192. const MCInstrDesc &II = TII.get(AArch64::BR);
  2193. AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
  2194. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
  2195. // Make sure the CFG is up-to-date.
  2196. for (auto *Succ : BI->successors())
  2197. FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[Succ]);
  2198. return true;
  2199. }
  2200. bool AArch64FastISel::selectCmp(const Instruction *I) {
  2201. const CmpInst *CI = cast<CmpInst>(I);
  2202. // Vectors of i1 are weird: bail out.
  2203. if (CI->getType()->isVectorTy())
  2204. return false;
  2205. // Try to optimize or fold the cmp.
  2206. CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
  2207. unsigned ResultReg = 0;
  2208. switch (Predicate) {
  2209. default:
  2210. break;
  2211. case CmpInst::FCMP_FALSE:
  2212. ResultReg = createResultReg(&AArch64::GPR32RegClass);
  2213. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  2214. TII.get(TargetOpcode::COPY), ResultReg)
  2215. .addReg(AArch64::WZR, getKillRegState(true));
  2216. break;
  2217. case CmpInst::FCMP_TRUE:
  2218. ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
  2219. break;
  2220. }
  2221. if (ResultReg) {
  2222. updateValueMap(I, ResultReg);
  2223. return true;
  2224. }
  2225. // Emit the cmp.
  2226. if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
  2227. return false;
  2228. ResultReg = createResultReg(&AArch64::GPR32RegClass);
  2229. // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
  2230. // condition codes are inverted, because they are used by CSINC.
  2231. static unsigned CondCodeTable[2][2] = {
  2232. { AArch64CC::NE, AArch64CC::VC },
  2233. { AArch64CC::PL, AArch64CC::LE }
  2234. };
  2235. unsigned *CondCodes = nullptr;
  2236. switch (Predicate) {
  2237. default:
  2238. break;
  2239. case CmpInst::FCMP_UEQ:
  2240. CondCodes = &CondCodeTable[0][0];
  2241. break;
  2242. case CmpInst::FCMP_ONE:
  2243. CondCodes = &CondCodeTable[1][0];
  2244. break;
  2245. }
  2246. if (CondCodes) {
  2247. Register TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
  2248. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
  2249. TmpReg1)
  2250. .addReg(AArch64::WZR, getKillRegState(true))
  2251. .addReg(AArch64::WZR, getKillRegState(true))
  2252. .addImm(CondCodes[0]);
  2253. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
  2254. ResultReg)
  2255. .addReg(TmpReg1, getKillRegState(true))
  2256. .addReg(AArch64::WZR, getKillRegState(true))
  2257. .addImm(CondCodes[1]);
  2258. updateValueMap(I, ResultReg);
  2259. return true;
  2260. }
  2261. // Now set a register based on the comparison.
  2262. AArch64CC::CondCode CC = getCompareCC(Predicate);
  2263. assert((CC != AArch64CC::AL) && "Unexpected condition code.");
  2264. AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
  2265. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
  2266. ResultReg)
  2267. .addReg(AArch64::WZR, getKillRegState(true))
  2268. .addReg(AArch64::WZR, getKillRegState(true))
  2269. .addImm(invertedCC);
  2270. updateValueMap(I, ResultReg);
  2271. return true;
  2272. }
  2273. /// Optimize selects of i1 if one of the operands has a 'true' or 'false'
  2274. /// value.
  2275. bool AArch64FastISel::optimizeSelect(const SelectInst *SI) {
  2276. if (!SI->getType()->isIntegerTy(1))
  2277. return false;
  2278. const Value *Src1Val, *Src2Val;
  2279. unsigned Opc = 0;
  2280. bool NeedExtraOp = false;
  2281. if (auto *CI = dyn_cast<ConstantInt>(SI->getTrueValue())) {
  2282. if (CI->isOne()) {
  2283. Src1Val = SI->getCondition();
  2284. Src2Val = SI->getFalseValue();
  2285. Opc = AArch64::ORRWrr;
  2286. } else {
  2287. assert(CI->isZero());
  2288. Src1Val = SI->getFalseValue();
  2289. Src2Val = SI->getCondition();
  2290. Opc = AArch64::BICWrr;
  2291. }
  2292. } else if (auto *CI = dyn_cast<ConstantInt>(SI->getFalseValue())) {
  2293. if (CI->isOne()) {
  2294. Src1Val = SI->getCondition();
  2295. Src2Val = SI->getTrueValue();
  2296. Opc = AArch64::ORRWrr;
  2297. NeedExtraOp = true;
  2298. } else {
  2299. assert(CI->isZero());
  2300. Src1Val = SI->getCondition();
  2301. Src2Val = SI->getTrueValue();
  2302. Opc = AArch64::ANDWrr;
  2303. }
  2304. }
  2305. if (!Opc)
  2306. return false;
  2307. Register Src1Reg = getRegForValue(Src1Val);
  2308. if (!Src1Reg)
  2309. return false;
  2310. Register Src2Reg = getRegForValue(Src2Val);
  2311. if (!Src2Reg)
  2312. return false;
  2313. if (NeedExtraOp)
  2314. Src1Reg = emitLogicalOp_ri(ISD::XOR, MVT::i32, Src1Reg, 1);
  2315. Register ResultReg = fastEmitInst_rr(Opc, &AArch64::GPR32RegClass, Src1Reg,
  2316. Src2Reg);
  2317. updateValueMap(SI, ResultReg);
  2318. return true;
  2319. }
  2320. bool AArch64FastISel::selectSelect(const Instruction *I) {
  2321. assert(isa<SelectInst>(I) && "Expected a select instruction.");
  2322. MVT VT;
  2323. if (!isTypeSupported(I->getType(), VT))
  2324. return false;
  2325. unsigned Opc;
  2326. const TargetRegisterClass *RC;
  2327. switch (VT.SimpleTy) {
  2328. default:
  2329. return false;
  2330. case MVT::i1:
  2331. case MVT::i8:
  2332. case MVT::i16:
  2333. case MVT::i32:
  2334. Opc = AArch64::CSELWr;
  2335. RC = &AArch64::GPR32RegClass;
  2336. break;
  2337. case MVT::i64:
  2338. Opc = AArch64::CSELXr;
  2339. RC = &AArch64::GPR64RegClass;
  2340. break;
  2341. case MVT::f32:
  2342. Opc = AArch64::FCSELSrrr;
  2343. RC = &AArch64::FPR32RegClass;
  2344. break;
  2345. case MVT::f64:
  2346. Opc = AArch64::FCSELDrrr;
  2347. RC = &AArch64::FPR64RegClass;
  2348. break;
  2349. }
  2350. const SelectInst *SI = cast<SelectInst>(I);
  2351. const Value *Cond = SI->getCondition();
  2352. AArch64CC::CondCode CC = AArch64CC::NE;
  2353. AArch64CC::CondCode ExtraCC = AArch64CC::AL;
  2354. if (optimizeSelect(SI))
  2355. return true;
  2356. // Try to pickup the flags, so we don't have to emit another compare.
  2357. if (foldXALUIntrinsic(CC, I, Cond)) {
  2358. // Fake request the condition to force emission of the XALU intrinsic.
  2359. Register CondReg = getRegForValue(Cond);
  2360. if (!CondReg)
  2361. return false;
  2362. } else if (isa<CmpInst>(Cond) && cast<CmpInst>(Cond)->hasOneUse() &&
  2363. isValueAvailable(Cond)) {
  2364. const auto *Cmp = cast<CmpInst>(Cond);
  2365. // Try to optimize or fold the cmp.
  2366. CmpInst::Predicate Predicate = optimizeCmpPredicate(Cmp);
  2367. const Value *FoldSelect = nullptr;
  2368. switch (Predicate) {
  2369. default:
  2370. break;
  2371. case CmpInst::FCMP_FALSE:
  2372. FoldSelect = SI->getFalseValue();
  2373. break;
  2374. case CmpInst::FCMP_TRUE:
  2375. FoldSelect = SI->getTrueValue();
  2376. break;
  2377. }
  2378. if (FoldSelect) {
  2379. Register SrcReg = getRegForValue(FoldSelect);
  2380. if (!SrcReg)
  2381. return false;
  2382. updateValueMap(I, SrcReg);
  2383. return true;
  2384. }
  2385. // Emit the cmp.
  2386. if (!emitCmp(Cmp->getOperand(0), Cmp->getOperand(1), Cmp->isUnsigned()))
  2387. return false;
  2388. // FCMP_UEQ and FCMP_ONE cannot be checked with a single select instruction.
  2389. CC = getCompareCC(Predicate);
  2390. switch (Predicate) {
  2391. default:
  2392. break;
  2393. case CmpInst::FCMP_UEQ:
  2394. ExtraCC = AArch64CC::EQ;
  2395. CC = AArch64CC::VS;
  2396. break;
  2397. case CmpInst::FCMP_ONE:
  2398. ExtraCC = AArch64CC::MI;
  2399. CC = AArch64CC::GT;
  2400. break;
  2401. }
  2402. assert((CC != AArch64CC::AL) && "Unexpected condition code.");
  2403. } else {
  2404. Register CondReg = getRegForValue(Cond);
  2405. if (!CondReg)
  2406. return false;
  2407. const MCInstrDesc &II = TII.get(AArch64::ANDSWri);
  2408. CondReg = constrainOperandRegClass(II, CondReg, 1);
  2409. // Emit a TST instruction (ANDS wzr, reg, #imm).
  2410. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
  2411. AArch64::WZR)
  2412. .addReg(CondReg)
  2413. .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
  2414. }
  2415. Register Src1Reg = getRegForValue(SI->getTrueValue());
  2416. Register Src2Reg = getRegForValue(SI->getFalseValue());
  2417. if (!Src1Reg || !Src2Reg)
  2418. return false;
  2419. if (ExtraCC != AArch64CC::AL)
  2420. Src2Reg = fastEmitInst_rri(Opc, RC, Src1Reg, Src2Reg, ExtraCC);
  2421. Register ResultReg = fastEmitInst_rri(Opc, RC, Src1Reg, Src2Reg, CC);
  2422. updateValueMap(I, ResultReg);
  2423. return true;
  2424. }
  2425. bool AArch64FastISel::selectFPExt(const Instruction *I) {
  2426. Value *V = I->getOperand(0);
  2427. if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
  2428. return false;
  2429. Register Op = getRegForValue(V);
  2430. if (Op == 0)
  2431. return false;
  2432. Register ResultReg = createResultReg(&AArch64::FPR64RegClass);
  2433. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
  2434. ResultReg).addReg(Op);
  2435. updateValueMap(I, ResultReg);
  2436. return true;
  2437. }
  2438. bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
  2439. Value *V = I->getOperand(0);
  2440. if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
  2441. return false;
  2442. Register Op = getRegForValue(V);
  2443. if (Op == 0)
  2444. return false;
  2445. Register ResultReg = createResultReg(&AArch64::FPR32RegClass);
  2446. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
  2447. ResultReg).addReg(Op);
  2448. updateValueMap(I, ResultReg);
  2449. return true;
  2450. }
  2451. // FPToUI and FPToSI
  2452. bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
  2453. MVT DestVT;
  2454. if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
  2455. return false;
  2456. Register SrcReg = getRegForValue(I->getOperand(0));
  2457. if (SrcReg == 0)
  2458. return false;
  2459. EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
  2460. if (SrcVT == MVT::f128 || SrcVT == MVT::f16)
  2461. return false;
  2462. unsigned Opc;
  2463. if (SrcVT == MVT::f64) {
  2464. if (Signed)
  2465. Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
  2466. else
  2467. Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
  2468. } else {
  2469. if (Signed)
  2470. Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
  2471. else
  2472. Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
  2473. }
  2474. Register ResultReg = createResultReg(
  2475. DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
  2476. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
  2477. .addReg(SrcReg);
  2478. updateValueMap(I, ResultReg);
  2479. return true;
  2480. }
  2481. bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
  2482. MVT DestVT;
  2483. if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
  2484. return false;
  2485. // Let regular ISEL handle FP16
  2486. if (DestVT == MVT::f16)
  2487. return false;
  2488. assert((DestVT == MVT::f32 || DestVT == MVT::f64) &&
  2489. "Unexpected value type.");
  2490. Register SrcReg = getRegForValue(I->getOperand(0));
  2491. if (!SrcReg)
  2492. return false;
  2493. EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
  2494. // Handle sign-extension.
  2495. if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
  2496. SrcReg =
  2497. emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
  2498. if (!SrcReg)
  2499. return false;
  2500. }
  2501. unsigned Opc;
  2502. if (SrcVT == MVT::i64) {
  2503. if (Signed)
  2504. Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
  2505. else
  2506. Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
  2507. } else {
  2508. if (Signed)
  2509. Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
  2510. else
  2511. Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
  2512. }
  2513. Register ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg);
  2514. updateValueMap(I, ResultReg);
  2515. return true;
  2516. }
  2517. bool AArch64FastISel::fastLowerArguments() {
  2518. if (!FuncInfo.CanLowerReturn)
  2519. return false;
  2520. const Function *F = FuncInfo.Fn;
  2521. if (F->isVarArg())
  2522. return false;
  2523. CallingConv::ID CC = F->getCallingConv();
  2524. if (CC != CallingConv::C && CC != CallingConv::Swift)
  2525. return false;
  2526. if (Subtarget->hasCustomCallingConv())
  2527. return false;
  2528. // Only handle simple cases of up to 8 GPR and FPR each.
  2529. unsigned GPRCnt = 0;
  2530. unsigned FPRCnt = 0;
  2531. for (auto const &Arg : F->args()) {
  2532. if (Arg.hasAttribute(Attribute::ByVal) ||
  2533. Arg.hasAttribute(Attribute::InReg) ||
  2534. Arg.hasAttribute(Attribute::StructRet) ||
  2535. Arg.hasAttribute(Attribute::SwiftSelf) ||
  2536. Arg.hasAttribute(Attribute::SwiftAsync) ||
  2537. Arg.hasAttribute(Attribute::SwiftError) ||
  2538. Arg.hasAttribute(Attribute::Nest))
  2539. return false;
  2540. Type *ArgTy = Arg.getType();
  2541. if (ArgTy->isStructTy() || ArgTy->isArrayTy())
  2542. return false;
  2543. EVT ArgVT = TLI.getValueType(DL, ArgTy);
  2544. if (!ArgVT.isSimple())
  2545. return false;
  2546. MVT VT = ArgVT.getSimpleVT().SimpleTy;
  2547. if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
  2548. return false;
  2549. if (VT.isVector() &&
  2550. (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
  2551. return false;
  2552. if (VT >= MVT::i1 && VT <= MVT::i64)
  2553. ++GPRCnt;
  2554. else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
  2555. VT.is128BitVector())
  2556. ++FPRCnt;
  2557. else
  2558. return false;
  2559. if (GPRCnt > 8 || FPRCnt > 8)
  2560. return false;
  2561. }
  2562. static const MCPhysReg Registers[6][8] = {
  2563. { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
  2564. AArch64::W5, AArch64::W6, AArch64::W7 },
  2565. { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
  2566. AArch64::X5, AArch64::X6, AArch64::X7 },
  2567. { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
  2568. AArch64::H5, AArch64::H6, AArch64::H7 },
  2569. { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
  2570. AArch64::S5, AArch64::S6, AArch64::S7 },
  2571. { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
  2572. AArch64::D5, AArch64::D6, AArch64::D7 },
  2573. { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
  2574. AArch64::Q5, AArch64::Q6, AArch64::Q7 }
  2575. };
  2576. unsigned GPRIdx = 0;
  2577. unsigned FPRIdx = 0;
  2578. for (auto const &Arg : F->args()) {
  2579. MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
  2580. unsigned SrcReg;
  2581. const TargetRegisterClass *RC;
  2582. if (VT >= MVT::i1 && VT <= MVT::i32) {
  2583. SrcReg = Registers[0][GPRIdx++];
  2584. RC = &AArch64::GPR32RegClass;
  2585. VT = MVT::i32;
  2586. } else if (VT == MVT::i64) {
  2587. SrcReg = Registers[1][GPRIdx++];
  2588. RC = &AArch64::GPR64RegClass;
  2589. } else if (VT == MVT::f16) {
  2590. SrcReg = Registers[2][FPRIdx++];
  2591. RC = &AArch64::FPR16RegClass;
  2592. } else if (VT == MVT::f32) {
  2593. SrcReg = Registers[3][FPRIdx++];
  2594. RC = &AArch64::FPR32RegClass;
  2595. } else if ((VT == MVT::f64) || VT.is64BitVector()) {
  2596. SrcReg = Registers[4][FPRIdx++];
  2597. RC = &AArch64::FPR64RegClass;
  2598. } else if (VT.is128BitVector()) {
  2599. SrcReg = Registers[5][FPRIdx++];
  2600. RC = &AArch64::FPR128RegClass;
  2601. } else
  2602. llvm_unreachable("Unexpected value type.");
  2603. Register DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
  2604. // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
  2605. // Without this, EmitLiveInCopies may eliminate the livein if its only
  2606. // use is a bitcast (which isn't turned into an instruction).
  2607. Register ResultReg = createResultReg(RC);
  2608. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  2609. TII.get(TargetOpcode::COPY), ResultReg)
  2610. .addReg(DstReg, getKillRegState(true));
  2611. updateValueMap(&Arg, ResultReg);
  2612. }
  2613. return true;
  2614. }
  2615. bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
  2616. SmallVectorImpl<MVT> &OutVTs,
  2617. unsigned &NumBytes) {
  2618. CallingConv::ID CC = CLI.CallConv;
  2619. SmallVector<CCValAssign, 16> ArgLocs;
  2620. CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
  2621. CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
  2622. // Get a count of how many bytes are to be pushed on the stack.
  2623. NumBytes = CCInfo.getNextStackOffset();
  2624. // Issue CALLSEQ_START
  2625. unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
  2626. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
  2627. .addImm(NumBytes).addImm(0);
  2628. // Process the args.
  2629. for (CCValAssign &VA : ArgLocs) {
  2630. const Value *ArgVal = CLI.OutVals[VA.getValNo()];
  2631. MVT ArgVT = OutVTs[VA.getValNo()];
  2632. Register ArgReg = getRegForValue(ArgVal);
  2633. if (!ArgReg)
  2634. return false;
  2635. // Handle arg promotion: SExt, ZExt, AExt.
  2636. switch (VA.getLocInfo()) {
  2637. case CCValAssign::Full:
  2638. break;
  2639. case CCValAssign::SExt: {
  2640. MVT DestVT = VA.getLocVT();
  2641. MVT SrcVT = ArgVT;
  2642. ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
  2643. if (!ArgReg)
  2644. return false;
  2645. break;
  2646. }
  2647. case CCValAssign::AExt:
  2648. // Intentional fall-through.
  2649. case CCValAssign::ZExt: {
  2650. MVT DestVT = VA.getLocVT();
  2651. MVT SrcVT = ArgVT;
  2652. ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
  2653. if (!ArgReg)
  2654. return false;
  2655. break;
  2656. }
  2657. default:
  2658. llvm_unreachable("Unknown arg promotion!");
  2659. }
  2660. // Now copy/store arg to correct locations.
  2661. if (VA.isRegLoc() && !VA.needsCustom()) {
  2662. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  2663. TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
  2664. CLI.OutRegs.push_back(VA.getLocReg());
  2665. } else if (VA.needsCustom()) {
  2666. // FIXME: Handle custom args.
  2667. return false;
  2668. } else {
  2669. assert(VA.isMemLoc() && "Assuming store on stack.");
  2670. // Don't emit stores for undef values.
  2671. if (isa<UndefValue>(ArgVal))
  2672. continue;
  2673. // Need to store on the stack.
  2674. unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
  2675. unsigned BEAlign = 0;
  2676. if (ArgSize < 8 && !Subtarget->isLittleEndian())
  2677. BEAlign = 8 - ArgSize;
  2678. Address Addr;
  2679. Addr.setKind(Address::RegBase);
  2680. Addr.setReg(AArch64::SP);
  2681. Addr.setOffset(VA.getLocMemOffset() + BEAlign);
  2682. Align Alignment = DL.getABITypeAlign(ArgVal->getType());
  2683. MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
  2684. MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
  2685. MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
  2686. if (!emitStore(ArgVT, ArgReg, Addr, MMO))
  2687. return false;
  2688. }
  2689. }
  2690. return true;
  2691. }
  2692. bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
  2693. unsigned NumBytes) {
  2694. CallingConv::ID CC = CLI.CallConv;
  2695. // Issue CALLSEQ_END
  2696. unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
  2697. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
  2698. .addImm(NumBytes).addImm(0);
  2699. // Now the return value.
  2700. if (RetVT != MVT::isVoid) {
  2701. SmallVector<CCValAssign, 16> RVLocs;
  2702. CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
  2703. CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
  2704. // Only handle a single return value.
  2705. if (RVLocs.size() != 1)
  2706. return false;
  2707. // Copy all of the result registers out of their specified physreg.
  2708. MVT CopyVT = RVLocs[0].getValVT();
  2709. // TODO: Handle big-endian results
  2710. if (CopyVT.isVector() && !Subtarget->isLittleEndian())
  2711. return false;
  2712. Register ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
  2713. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  2714. TII.get(TargetOpcode::COPY), ResultReg)
  2715. .addReg(RVLocs[0].getLocReg());
  2716. CLI.InRegs.push_back(RVLocs[0].getLocReg());
  2717. CLI.ResultReg = ResultReg;
  2718. CLI.NumResultRegs = 1;
  2719. }
  2720. return true;
  2721. }
  2722. bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
  2723. CallingConv::ID CC = CLI.CallConv;
  2724. bool IsTailCall = CLI.IsTailCall;
  2725. bool IsVarArg = CLI.IsVarArg;
  2726. const Value *Callee = CLI.Callee;
  2727. MCSymbol *Symbol = CLI.Symbol;
  2728. if (!Callee && !Symbol)
  2729. return false;
  2730. // Allow SelectionDAG isel to handle calls to functions like setjmp that need
  2731. // a bti instruction following the call.
  2732. if (CLI.CB && CLI.CB->hasFnAttr(Attribute::ReturnsTwice) &&
  2733. !Subtarget->noBTIAtReturnTwice() &&
  2734. MF->getInfo<AArch64FunctionInfo>()->branchTargetEnforcement())
  2735. return false;
  2736. // Allow SelectionDAG isel to handle tail calls.
  2737. if (IsTailCall)
  2738. return false;
  2739. // FIXME: we could and should support this, but for now correctness at -O0 is
  2740. // more important.
  2741. if (Subtarget->isTargetILP32())
  2742. return false;
  2743. CodeModel::Model CM = TM.getCodeModel();
  2744. // Only support the small-addressing and large code models.
  2745. if (CM != CodeModel::Large && !Subtarget->useSmallAddressing())
  2746. return false;
  2747. // FIXME: Add large code model support for ELF.
  2748. if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
  2749. return false;
  2750. // Let SDISel handle vararg functions.
  2751. if (IsVarArg)
  2752. return false;
  2753. // FIXME: Only handle *simple* calls for now.
  2754. MVT RetVT;
  2755. if (CLI.RetTy->isVoidTy())
  2756. RetVT = MVT::isVoid;
  2757. else if (!isTypeLegal(CLI.RetTy, RetVT))
  2758. return false;
  2759. for (auto Flag : CLI.OutFlags)
  2760. if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal() ||
  2761. Flag.isSwiftSelf() || Flag.isSwiftAsync() || Flag.isSwiftError())
  2762. return false;
  2763. // Set up the argument vectors.
  2764. SmallVector<MVT, 16> OutVTs;
  2765. OutVTs.reserve(CLI.OutVals.size());
  2766. for (auto *Val : CLI.OutVals) {
  2767. MVT VT;
  2768. if (!isTypeLegal(Val->getType(), VT) &&
  2769. !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
  2770. return false;
  2771. // We don't handle vector parameters yet.
  2772. if (VT.isVector() || VT.getSizeInBits() > 64)
  2773. return false;
  2774. OutVTs.push_back(VT);
  2775. }
  2776. Address Addr;
  2777. if (Callee && !computeCallAddress(Callee, Addr))
  2778. return false;
  2779. // The weak function target may be zero; in that case we must use indirect
  2780. // addressing via a stub on windows as it may be out of range for a
  2781. // PC-relative jump.
  2782. if (Subtarget->isTargetWindows() && Addr.getGlobalValue() &&
  2783. Addr.getGlobalValue()->hasExternalWeakLinkage())
  2784. return false;
  2785. // Handle the arguments now that we've gotten them.
  2786. unsigned NumBytes;
  2787. if (!processCallArgs(CLI, OutVTs, NumBytes))
  2788. return false;
  2789. const AArch64RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
  2790. if (RegInfo->isAnyArgRegReserved(*MF))
  2791. RegInfo->emitReservedArgRegCallError(*MF);
  2792. // Issue the call.
  2793. MachineInstrBuilder MIB;
  2794. if (Subtarget->useSmallAddressing()) {
  2795. const MCInstrDesc &II =
  2796. TII.get(Addr.getReg() ? getBLRCallOpcode(*MF) : (unsigned)AArch64::BL);
  2797. MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
  2798. if (Symbol)
  2799. MIB.addSym(Symbol, 0);
  2800. else if (Addr.getGlobalValue())
  2801. MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
  2802. else if (Addr.getReg()) {
  2803. Register Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
  2804. MIB.addReg(Reg);
  2805. } else
  2806. return false;
  2807. } else {
  2808. unsigned CallReg = 0;
  2809. if (Symbol) {
  2810. Register ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
  2811. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
  2812. ADRPReg)
  2813. .addSym(Symbol, AArch64II::MO_GOT | AArch64II::MO_PAGE);
  2814. CallReg = createResultReg(&AArch64::GPR64RegClass);
  2815. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  2816. TII.get(AArch64::LDRXui), CallReg)
  2817. .addReg(ADRPReg)
  2818. .addSym(Symbol,
  2819. AArch64II::MO_GOT | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
  2820. } else if (Addr.getGlobalValue())
  2821. CallReg = materializeGV(Addr.getGlobalValue());
  2822. else if (Addr.getReg())
  2823. CallReg = Addr.getReg();
  2824. if (!CallReg)
  2825. return false;
  2826. const MCInstrDesc &II = TII.get(getBLRCallOpcode(*MF));
  2827. CallReg = constrainOperandRegClass(II, CallReg, 0);
  2828. MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
  2829. }
  2830. // Add implicit physical register uses to the call.
  2831. for (auto Reg : CLI.OutRegs)
  2832. MIB.addReg(Reg, RegState::Implicit);
  2833. // Add a register mask with the call-preserved registers.
  2834. // Proper defs for return values will be added by setPhysRegsDeadExcept().
  2835. MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
  2836. CLI.Call = MIB;
  2837. // Finish off the call including any return values.
  2838. return finishCall(CLI, RetVT, NumBytes);
  2839. }
  2840. bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
  2841. if (Alignment)
  2842. return Len / Alignment <= 4;
  2843. else
  2844. return Len < 32;
  2845. }
  2846. bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
  2847. uint64_t Len, unsigned Alignment) {
  2848. // Make sure we don't bloat code by inlining very large memcpy's.
  2849. if (!isMemCpySmall(Len, Alignment))
  2850. return false;
  2851. int64_t UnscaledOffset = 0;
  2852. Address OrigDest = Dest;
  2853. Address OrigSrc = Src;
  2854. while (Len) {
  2855. MVT VT;
  2856. if (!Alignment || Alignment >= 8) {
  2857. if (Len >= 8)
  2858. VT = MVT::i64;
  2859. else if (Len >= 4)
  2860. VT = MVT::i32;
  2861. else if (Len >= 2)
  2862. VT = MVT::i16;
  2863. else {
  2864. VT = MVT::i8;
  2865. }
  2866. } else {
  2867. // Bound based on alignment.
  2868. if (Len >= 4 && Alignment == 4)
  2869. VT = MVT::i32;
  2870. else if (Len >= 2 && Alignment == 2)
  2871. VT = MVT::i16;
  2872. else {
  2873. VT = MVT::i8;
  2874. }
  2875. }
  2876. unsigned ResultReg = emitLoad(VT, VT, Src);
  2877. if (!ResultReg)
  2878. return false;
  2879. if (!emitStore(VT, ResultReg, Dest))
  2880. return false;
  2881. int64_t Size = VT.getSizeInBits() / 8;
  2882. Len -= Size;
  2883. UnscaledOffset += Size;
  2884. // We need to recompute the unscaled offset for each iteration.
  2885. Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
  2886. Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
  2887. }
  2888. return true;
  2889. }
  2890. /// Check if it is possible to fold the condition from the XALU intrinsic
  2891. /// into the user. The condition code will only be updated on success.
  2892. bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
  2893. const Instruction *I,
  2894. const Value *Cond) {
  2895. if (!isa<ExtractValueInst>(Cond))
  2896. return false;
  2897. const auto *EV = cast<ExtractValueInst>(Cond);
  2898. if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
  2899. return false;
  2900. const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
  2901. MVT RetVT;
  2902. const Function *Callee = II->getCalledFunction();
  2903. Type *RetTy =
  2904. cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
  2905. if (!isTypeLegal(RetTy, RetVT))
  2906. return false;
  2907. if (RetVT != MVT::i32 && RetVT != MVT::i64)
  2908. return false;
  2909. const Value *LHS = II->getArgOperand(0);
  2910. const Value *RHS = II->getArgOperand(1);
  2911. // Canonicalize immediate to the RHS.
  2912. if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) && II->isCommutative())
  2913. std::swap(LHS, RHS);
  2914. // Simplify multiplies.
  2915. Intrinsic::ID IID = II->getIntrinsicID();
  2916. switch (IID) {
  2917. default:
  2918. break;
  2919. case Intrinsic::smul_with_overflow:
  2920. if (const auto *C = dyn_cast<ConstantInt>(RHS))
  2921. if (C->getValue() == 2)
  2922. IID = Intrinsic::sadd_with_overflow;
  2923. break;
  2924. case Intrinsic::umul_with_overflow:
  2925. if (const auto *C = dyn_cast<ConstantInt>(RHS))
  2926. if (C->getValue() == 2)
  2927. IID = Intrinsic::uadd_with_overflow;
  2928. break;
  2929. }
  2930. AArch64CC::CondCode TmpCC;
  2931. switch (IID) {
  2932. default:
  2933. return false;
  2934. case Intrinsic::sadd_with_overflow:
  2935. case Intrinsic::ssub_with_overflow:
  2936. TmpCC = AArch64CC::VS;
  2937. break;
  2938. case Intrinsic::uadd_with_overflow:
  2939. TmpCC = AArch64CC::HS;
  2940. break;
  2941. case Intrinsic::usub_with_overflow:
  2942. TmpCC = AArch64CC::LO;
  2943. break;
  2944. case Intrinsic::smul_with_overflow:
  2945. case Intrinsic::umul_with_overflow:
  2946. TmpCC = AArch64CC::NE;
  2947. break;
  2948. }
  2949. // Check if both instructions are in the same basic block.
  2950. if (!isValueAvailable(II))
  2951. return false;
  2952. // Make sure nothing is in the way
  2953. BasicBlock::const_iterator Start(I);
  2954. BasicBlock::const_iterator End(II);
  2955. for (auto Itr = std::prev(Start); Itr != End; --Itr) {
  2956. // We only expect extractvalue instructions between the intrinsic and the
  2957. // instruction to be selected.
  2958. if (!isa<ExtractValueInst>(Itr))
  2959. return false;
  2960. // Check that the extractvalue operand comes from the intrinsic.
  2961. const auto *EVI = cast<ExtractValueInst>(Itr);
  2962. if (EVI->getAggregateOperand() != II)
  2963. return false;
  2964. }
  2965. CC = TmpCC;
  2966. return true;
  2967. }
  2968. bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
  2969. // FIXME: Handle more intrinsics.
  2970. switch (II->getIntrinsicID()) {
  2971. default: return false;
  2972. case Intrinsic::frameaddress: {
  2973. MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
  2974. MFI.setFrameAddressIsTaken(true);
  2975. const AArch64RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
  2976. Register FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
  2977. Register SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
  2978. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  2979. TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
  2980. // Recursively load frame address
  2981. // ldr x0, [fp]
  2982. // ldr x0, [x0]
  2983. // ldr x0, [x0]
  2984. // ...
  2985. unsigned DestReg;
  2986. unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
  2987. while (Depth--) {
  2988. DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
  2989. SrcReg, 0);
  2990. assert(DestReg && "Unexpected LDR instruction emission failure.");
  2991. SrcReg = DestReg;
  2992. }
  2993. updateValueMap(II, SrcReg);
  2994. return true;
  2995. }
  2996. case Intrinsic::sponentry: {
  2997. MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
  2998. // SP = FP + Fixed Object + 16
  2999. int FI = MFI.CreateFixedObject(4, 0, false);
  3000. Register ResultReg = createResultReg(&AArch64::GPR64spRegClass);
  3001. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3002. TII.get(AArch64::ADDXri), ResultReg)
  3003. .addFrameIndex(FI)
  3004. .addImm(0)
  3005. .addImm(0);
  3006. updateValueMap(II, ResultReg);
  3007. return true;
  3008. }
  3009. case Intrinsic::memcpy:
  3010. case Intrinsic::memmove: {
  3011. const auto *MTI = cast<MemTransferInst>(II);
  3012. // Don't handle volatile.
  3013. if (MTI->isVolatile())
  3014. return false;
  3015. // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
  3016. // we would emit dead code because we don't currently handle memmoves.
  3017. bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
  3018. if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
  3019. // Small memcpy's are common enough that we want to do them without a call
  3020. // if possible.
  3021. uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
  3022. unsigned Alignment = MinAlign(MTI->getDestAlignment(),
  3023. MTI->getSourceAlignment());
  3024. if (isMemCpySmall(Len, Alignment)) {
  3025. Address Dest, Src;
  3026. if (!computeAddress(MTI->getRawDest(), Dest) ||
  3027. !computeAddress(MTI->getRawSource(), Src))
  3028. return false;
  3029. if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
  3030. return true;
  3031. }
  3032. }
  3033. if (!MTI->getLength()->getType()->isIntegerTy(64))
  3034. return false;
  3035. if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
  3036. // Fast instruction selection doesn't support the special
  3037. // address spaces.
  3038. return false;
  3039. const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
  3040. return lowerCallTo(II, IntrMemName, II->arg_size() - 1);
  3041. }
  3042. case Intrinsic::memset: {
  3043. const MemSetInst *MSI = cast<MemSetInst>(II);
  3044. // Don't handle volatile.
  3045. if (MSI->isVolatile())
  3046. return false;
  3047. if (!MSI->getLength()->getType()->isIntegerTy(64))
  3048. return false;
  3049. if (MSI->getDestAddressSpace() > 255)
  3050. // Fast instruction selection doesn't support the special
  3051. // address spaces.
  3052. return false;
  3053. return lowerCallTo(II, "memset", II->arg_size() - 1);
  3054. }
  3055. case Intrinsic::sin:
  3056. case Intrinsic::cos:
  3057. case Intrinsic::pow: {
  3058. MVT RetVT;
  3059. if (!isTypeLegal(II->getType(), RetVT))
  3060. return false;
  3061. if (RetVT != MVT::f32 && RetVT != MVT::f64)
  3062. return false;
  3063. static const RTLIB::Libcall LibCallTable[3][2] = {
  3064. { RTLIB::SIN_F32, RTLIB::SIN_F64 },
  3065. { RTLIB::COS_F32, RTLIB::COS_F64 },
  3066. { RTLIB::POW_F32, RTLIB::POW_F64 }
  3067. };
  3068. RTLIB::Libcall LC;
  3069. bool Is64Bit = RetVT == MVT::f64;
  3070. switch (II->getIntrinsicID()) {
  3071. default:
  3072. llvm_unreachable("Unexpected intrinsic.");
  3073. case Intrinsic::sin:
  3074. LC = LibCallTable[0][Is64Bit];
  3075. break;
  3076. case Intrinsic::cos:
  3077. LC = LibCallTable[1][Is64Bit];
  3078. break;
  3079. case Intrinsic::pow:
  3080. LC = LibCallTable[2][Is64Bit];
  3081. break;
  3082. }
  3083. ArgListTy Args;
  3084. Args.reserve(II->arg_size());
  3085. // Populate the argument list.
  3086. for (auto &Arg : II->args()) {
  3087. ArgListEntry Entry;
  3088. Entry.Val = Arg;
  3089. Entry.Ty = Arg->getType();
  3090. Args.push_back(Entry);
  3091. }
  3092. CallLoweringInfo CLI;
  3093. MCContext &Ctx = MF->getContext();
  3094. CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), II->getType(),
  3095. TLI.getLibcallName(LC), std::move(Args));
  3096. if (!lowerCallTo(CLI))
  3097. return false;
  3098. updateValueMap(II, CLI.ResultReg);
  3099. return true;
  3100. }
  3101. case Intrinsic::fabs: {
  3102. MVT VT;
  3103. if (!isTypeLegal(II->getType(), VT))
  3104. return false;
  3105. unsigned Opc;
  3106. switch (VT.SimpleTy) {
  3107. default:
  3108. return false;
  3109. case MVT::f32:
  3110. Opc = AArch64::FABSSr;
  3111. break;
  3112. case MVT::f64:
  3113. Opc = AArch64::FABSDr;
  3114. break;
  3115. }
  3116. Register SrcReg = getRegForValue(II->getOperand(0));
  3117. if (!SrcReg)
  3118. return false;
  3119. Register ResultReg = createResultReg(TLI.getRegClassFor(VT));
  3120. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
  3121. .addReg(SrcReg);
  3122. updateValueMap(II, ResultReg);
  3123. return true;
  3124. }
  3125. case Intrinsic::trap:
  3126. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
  3127. .addImm(1);
  3128. return true;
  3129. case Intrinsic::debugtrap:
  3130. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
  3131. .addImm(0xF000);
  3132. return true;
  3133. case Intrinsic::sqrt: {
  3134. Type *RetTy = II->getCalledFunction()->getReturnType();
  3135. MVT VT;
  3136. if (!isTypeLegal(RetTy, VT))
  3137. return false;
  3138. Register Op0Reg = getRegForValue(II->getOperand(0));
  3139. if (!Op0Reg)
  3140. return false;
  3141. unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg);
  3142. if (!ResultReg)
  3143. return false;
  3144. updateValueMap(II, ResultReg);
  3145. return true;
  3146. }
  3147. case Intrinsic::sadd_with_overflow:
  3148. case Intrinsic::uadd_with_overflow:
  3149. case Intrinsic::ssub_with_overflow:
  3150. case Intrinsic::usub_with_overflow:
  3151. case Intrinsic::smul_with_overflow:
  3152. case Intrinsic::umul_with_overflow: {
  3153. // This implements the basic lowering of the xalu with overflow intrinsics.
  3154. const Function *Callee = II->getCalledFunction();
  3155. auto *Ty = cast<StructType>(Callee->getReturnType());
  3156. Type *RetTy = Ty->getTypeAtIndex(0U);
  3157. MVT VT;
  3158. if (!isTypeLegal(RetTy, VT))
  3159. return false;
  3160. if (VT != MVT::i32 && VT != MVT::i64)
  3161. return false;
  3162. const Value *LHS = II->getArgOperand(0);
  3163. const Value *RHS = II->getArgOperand(1);
  3164. // Canonicalize immediate to the RHS.
  3165. if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) && II->isCommutative())
  3166. std::swap(LHS, RHS);
  3167. // Simplify multiplies.
  3168. Intrinsic::ID IID = II->getIntrinsicID();
  3169. switch (IID) {
  3170. default:
  3171. break;
  3172. case Intrinsic::smul_with_overflow:
  3173. if (const auto *C = dyn_cast<ConstantInt>(RHS))
  3174. if (C->getValue() == 2) {
  3175. IID = Intrinsic::sadd_with_overflow;
  3176. RHS = LHS;
  3177. }
  3178. break;
  3179. case Intrinsic::umul_with_overflow:
  3180. if (const auto *C = dyn_cast<ConstantInt>(RHS))
  3181. if (C->getValue() == 2) {
  3182. IID = Intrinsic::uadd_with_overflow;
  3183. RHS = LHS;
  3184. }
  3185. break;
  3186. }
  3187. unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
  3188. AArch64CC::CondCode CC = AArch64CC::Invalid;
  3189. switch (IID) {
  3190. default: llvm_unreachable("Unexpected intrinsic!");
  3191. case Intrinsic::sadd_with_overflow:
  3192. ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
  3193. CC = AArch64CC::VS;
  3194. break;
  3195. case Intrinsic::uadd_with_overflow:
  3196. ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
  3197. CC = AArch64CC::HS;
  3198. break;
  3199. case Intrinsic::ssub_with_overflow:
  3200. ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
  3201. CC = AArch64CC::VS;
  3202. break;
  3203. case Intrinsic::usub_with_overflow:
  3204. ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
  3205. CC = AArch64CC::LO;
  3206. break;
  3207. case Intrinsic::smul_with_overflow: {
  3208. CC = AArch64CC::NE;
  3209. Register LHSReg = getRegForValue(LHS);
  3210. if (!LHSReg)
  3211. return false;
  3212. Register RHSReg = getRegForValue(RHS);
  3213. if (!RHSReg)
  3214. return false;
  3215. if (VT == MVT::i32) {
  3216. MulReg = emitSMULL_rr(MVT::i64, LHSReg, RHSReg);
  3217. Register MulSubReg =
  3218. fastEmitInst_extractsubreg(VT, MulReg, AArch64::sub_32);
  3219. // cmp xreg, wreg, sxtw
  3220. emitAddSub_rx(/*UseAdd=*/false, MVT::i64, MulReg, MulSubReg,
  3221. AArch64_AM::SXTW, /*ShiftImm=*/0, /*SetFlags=*/true,
  3222. /*WantResult=*/false);
  3223. MulReg = MulSubReg;
  3224. } else {
  3225. assert(VT == MVT::i64 && "Unexpected value type.");
  3226. // LHSReg and RHSReg cannot be killed by this Mul, since they are
  3227. // reused in the next instruction.
  3228. MulReg = emitMul_rr(VT, LHSReg, RHSReg);
  3229. unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, RHSReg);
  3230. emitSubs_rs(VT, SMULHReg, MulReg, AArch64_AM::ASR, 63,
  3231. /*WantResult=*/false);
  3232. }
  3233. break;
  3234. }
  3235. case Intrinsic::umul_with_overflow: {
  3236. CC = AArch64CC::NE;
  3237. Register LHSReg = getRegForValue(LHS);
  3238. if (!LHSReg)
  3239. return false;
  3240. Register RHSReg = getRegForValue(RHS);
  3241. if (!RHSReg)
  3242. return false;
  3243. if (VT == MVT::i32) {
  3244. MulReg = emitUMULL_rr(MVT::i64, LHSReg, RHSReg);
  3245. // tst xreg, #0xffffffff00000000
  3246. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3247. TII.get(AArch64::ANDSXri), AArch64::XZR)
  3248. .addReg(MulReg)
  3249. .addImm(AArch64_AM::encodeLogicalImmediate(0xFFFFFFFF00000000, 64));
  3250. MulReg = fastEmitInst_extractsubreg(VT, MulReg, AArch64::sub_32);
  3251. } else {
  3252. assert(VT == MVT::i64 && "Unexpected value type.");
  3253. // LHSReg and RHSReg cannot be killed by this Mul, since they are
  3254. // reused in the next instruction.
  3255. MulReg = emitMul_rr(VT, LHSReg, RHSReg);
  3256. unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, RHSReg);
  3257. emitSubs_rr(VT, AArch64::XZR, UMULHReg, /*WantResult=*/false);
  3258. }
  3259. break;
  3260. }
  3261. }
  3262. if (MulReg) {
  3263. ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
  3264. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3265. TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
  3266. }
  3267. if (!ResultReg1)
  3268. return false;
  3269. ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
  3270. AArch64::WZR, AArch64::WZR,
  3271. getInvertedCondCode(CC));
  3272. (void)ResultReg2;
  3273. assert((ResultReg1 + 1) == ResultReg2 &&
  3274. "Nonconsecutive result registers.");
  3275. updateValueMap(II, ResultReg1, 2);
  3276. return true;
  3277. }
  3278. }
  3279. return false;
  3280. }
  3281. bool AArch64FastISel::selectRet(const Instruction *I) {
  3282. const ReturnInst *Ret = cast<ReturnInst>(I);
  3283. const Function &F = *I->getParent()->getParent();
  3284. if (!FuncInfo.CanLowerReturn)
  3285. return false;
  3286. if (F.isVarArg())
  3287. return false;
  3288. if (TLI.supportSwiftError() &&
  3289. F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
  3290. return false;
  3291. if (TLI.supportSplitCSR(FuncInfo.MF))
  3292. return false;
  3293. // Build a list of return value registers.
  3294. SmallVector<unsigned, 4> RetRegs;
  3295. if (Ret->getNumOperands() > 0) {
  3296. CallingConv::ID CC = F.getCallingConv();
  3297. SmallVector<ISD::OutputArg, 4> Outs;
  3298. GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
  3299. // Analyze operands of the call, assigning locations to each operand.
  3300. SmallVector<CCValAssign, 16> ValLocs;
  3301. CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
  3302. CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
  3303. : RetCC_AArch64_AAPCS;
  3304. CCInfo.AnalyzeReturn(Outs, RetCC);
  3305. // Only handle a single return value for now.
  3306. if (ValLocs.size() != 1)
  3307. return false;
  3308. CCValAssign &VA = ValLocs[0];
  3309. const Value *RV = Ret->getOperand(0);
  3310. // Don't bother handling odd stuff for now.
  3311. if ((VA.getLocInfo() != CCValAssign::Full) &&
  3312. (VA.getLocInfo() != CCValAssign::BCvt))
  3313. return false;
  3314. // Only handle register returns for now.
  3315. if (!VA.isRegLoc())
  3316. return false;
  3317. Register Reg = getRegForValue(RV);
  3318. if (Reg == 0)
  3319. return false;
  3320. unsigned SrcReg = Reg + VA.getValNo();
  3321. Register DestReg = VA.getLocReg();
  3322. // Avoid a cross-class copy. This is very unlikely.
  3323. if (!MRI.getRegClass(SrcReg)->contains(DestReg))
  3324. return false;
  3325. EVT RVEVT = TLI.getValueType(DL, RV->getType());
  3326. if (!RVEVT.isSimple())
  3327. return false;
  3328. // Vectors (of > 1 lane) in big endian need tricky handling.
  3329. if (RVEVT.isVector() && RVEVT.getVectorElementCount().isVector() &&
  3330. !Subtarget->isLittleEndian())
  3331. return false;
  3332. MVT RVVT = RVEVT.getSimpleVT();
  3333. if (RVVT == MVT::f128)
  3334. return false;
  3335. MVT DestVT = VA.getValVT();
  3336. // Special handling for extended integers.
  3337. if (RVVT != DestVT) {
  3338. if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
  3339. return false;
  3340. if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
  3341. return false;
  3342. bool IsZExt = Outs[0].Flags.isZExt();
  3343. SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
  3344. if (SrcReg == 0)
  3345. return false;
  3346. }
  3347. // "Callee" (i.e. value producer) zero extends pointers at function
  3348. // boundary.
  3349. if (Subtarget->isTargetILP32() && RV->getType()->isPointerTy())
  3350. SrcReg = emitAnd_ri(MVT::i64, SrcReg, 0xffffffff);
  3351. // Make the copy.
  3352. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3353. TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
  3354. // Add register to return instruction.
  3355. RetRegs.push_back(VA.getLocReg());
  3356. }
  3357. MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3358. TII.get(AArch64::RET_ReallyLR));
  3359. for (unsigned RetReg : RetRegs)
  3360. MIB.addReg(RetReg, RegState::Implicit);
  3361. return true;
  3362. }
  3363. bool AArch64FastISel::selectTrunc(const Instruction *I) {
  3364. Type *DestTy = I->getType();
  3365. Value *Op = I->getOperand(0);
  3366. Type *SrcTy = Op->getType();
  3367. EVT SrcEVT = TLI.getValueType(DL, SrcTy, true);
  3368. EVT DestEVT = TLI.getValueType(DL, DestTy, true);
  3369. if (!SrcEVT.isSimple())
  3370. return false;
  3371. if (!DestEVT.isSimple())
  3372. return false;
  3373. MVT SrcVT = SrcEVT.getSimpleVT();
  3374. MVT DestVT = DestEVT.getSimpleVT();
  3375. if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
  3376. SrcVT != MVT::i8)
  3377. return false;
  3378. if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
  3379. DestVT != MVT::i1)
  3380. return false;
  3381. Register SrcReg = getRegForValue(Op);
  3382. if (!SrcReg)
  3383. return false;
  3384. // If we're truncating from i64 to a smaller non-legal type then generate an
  3385. // AND. Otherwise, we know the high bits are undefined and a truncate only
  3386. // generate a COPY. We cannot mark the source register also as result
  3387. // register, because this can incorrectly transfer the kill flag onto the
  3388. // source register.
  3389. unsigned ResultReg;
  3390. if (SrcVT == MVT::i64) {
  3391. uint64_t Mask = 0;
  3392. switch (DestVT.SimpleTy) {
  3393. default:
  3394. // Trunc i64 to i32 is handled by the target-independent fast-isel.
  3395. return false;
  3396. case MVT::i1:
  3397. Mask = 0x1;
  3398. break;
  3399. case MVT::i8:
  3400. Mask = 0xff;
  3401. break;
  3402. case MVT::i16:
  3403. Mask = 0xffff;
  3404. break;
  3405. }
  3406. // Issue an extract_subreg to get the lower 32-bits.
  3407. Register Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg,
  3408. AArch64::sub_32);
  3409. // Create the AND instruction which performs the actual truncation.
  3410. ResultReg = emitAnd_ri(MVT::i32, Reg32, Mask);
  3411. assert(ResultReg && "Unexpected AND instruction emission failure.");
  3412. } else {
  3413. ResultReg = createResultReg(&AArch64::GPR32RegClass);
  3414. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3415. TII.get(TargetOpcode::COPY), ResultReg)
  3416. .addReg(SrcReg);
  3417. }
  3418. updateValueMap(I, ResultReg);
  3419. return true;
  3420. }
  3421. unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
  3422. assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
  3423. DestVT == MVT::i64) &&
  3424. "Unexpected value type.");
  3425. // Handle i8 and i16 as i32.
  3426. if (DestVT == MVT::i8 || DestVT == MVT::i16)
  3427. DestVT = MVT::i32;
  3428. if (IsZExt) {
  3429. unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, 1);
  3430. assert(ResultReg && "Unexpected AND instruction emission failure.");
  3431. if (DestVT == MVT::i64) {
  3432. // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
  3433. // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
  3434. Register Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
  3435. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3436. TII.get(AArch64::SUBREG_TO_REG), Reg64)
  3437. .addImm(0)
  3438. .addReg(ResultReg)
  3439. .addImm(AArch64::sub_32);
  3440. ResultReg = Reg64;
  3441. }
  3442. return ResultReg;
  3443. } else {
  3444. if (DestVT == MVT::i64) {
  3445. // FIXME: We're SExt i1 to i64.
  3446. return 0;
  3447. }
  3448. return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
  3449. 0, 0);
  3450. }
  3451. }
  3452. unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, unsigned Op1) {
  3453. unsigned Opc, ZReg;
  3454. switch (RetVT.SimpleTy) {
  3455. default: return 0;
  3456. case MVT::i8:
  3457. case MVT::i16:
  3458. case MVT::i32:
  3459. RetVT = MVT::i32;
  3460. Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
  3461. case MVT::i64:
  3462. Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
  3463. }
  3464. const TargetRegisterClass *RC =
  3465. (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3466. return fastEmitInst_rrr(Opc, RC, Op0, Op1, ZReg);
  3467. }
  3468. unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, unsigned Op1) {
  3469. if (RetVT != MVT::i64)
  3470. return 0;
  3471. return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
  3472. Op0, Op1, AArch64::XZR);
  3473. }
  3474. unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, unsigned Op1) {
  3475. if (RetVT != MVT::i64)
  3476. return 0;
  3477. return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
  3478. Op0, Op1, AArch64::XZR);
  3479. }
  3480. unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg,
  3481. unsigned Op1Reg) {
  3482. unsigned Opc = 0;
  3483. bool NeedTrunc = false;
  3484. uint64_t Mask = 0;
  3485. switch (RetVT.SimpleTy) {
  3486. default: return 0;
  3487. case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
  3488. case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
  3489. case MVT::i32: Opc = AArch64::LSLVWr; break;
  3490. case MVT::i64: Opc = AArch64::LSLVXr; break;
  3491. }
  3492. const TargetRegisterClass *RC =
  3493. (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3494. if (NeedTrunc)
  3495. Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Mask);
  3496. Register ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op1Reg);
  3497. if (NeedTrunc)
  3498. ResultReg = emitAnd_ri(MVT::i32, ResultReg, Mask);
  3499. return ResultReg;
  3500. }
  3501. unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
  3502. uint64_t Shift, bool IsZExt) {
  3503. assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
  3504. "Unexpected source/return type pair.");
  3505. assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
  3506. SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
  3507. "Unexpected source value type.");
  3508. assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
  3509. RetVT == MVT::i64) && "Unexpected return value type.");
  3510. bool Is64Bit = (RetVT == MVT::i64);
  3511. unsigned RegSize = Is64Bit ? 64 : 32;
  3512. unsigned DstBits = RetVT.getSizeInBits();
  3513. unsigned SrcBits = SrcVT.getSizeInBits();
  3514. const TargetRegisterClass *RC =
  3515. Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3516. // Just emit a copy for "zero" shifts.
  3517. if (Shift == 0) {
  3518. if (RetVT == SrcVT) {
  3519. Register ResultReg = createResultReg(RC);
  3520. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3521. TII.get(TargetOpcode::COPY), ResultReg)
  3522. .addReg(Op0);
  3523. return ResultReg;
  3524. } else
  3525. return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
  3526. }
  3527. // Don't deal with undefined shifts.
  3528. if (Shift >= DstBits)
  3529. return 0;
  3530. // For immediate shifts we can fold the zero-/sign-extension into the shift.
  3531. // {S|U}BFM Wd, Wn, #r, #s
  3532. // Wd<32+s-r,32-r> = Wn<s:0> when r > s
  3533. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3534. // %2 = shl i16 %1, 4
  3535. // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
  3536. // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
  3537. // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
  3538. // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
  3539. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3540. // %2 = shl i16 %1, 8
  3541. // Wd<32+7-24,32-24> = Wn<7:0>
  3542. // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
  3543. // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
  3544. // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
  3545. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3546. // %2 = shl i16 %1, 12
  3547. // Wd<32+3-20,32-20> = Wn<3:0>
  3548. // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
  3549. // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
  3550. // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
  3551. unsigned ImmR = RegSize - Shift;
  3552. // Limit the width to the length of the source type.
  3553. unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
  3554. static const unsigned OpcTable[2][2] = {
  3555. {AArch64::SBFMWri, AArch64::SBFMXri},
  3556. {AArch64::UBFMWri, AArch64::UBFMXri}
  3557. };
  3558. unsigned Opc = OpcTable[IsZExt][Is64Bit];
  3559. if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
  3560. Register TmpReg = MRI.createVirtualRegister(RC);
  3561. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3562. TII.get(AArch64::SUBREG_TO_REG), TmpReg)
  3563. .addImm(0)
  3564. .addReg(Op0)
  3565. .addImm(AArch64::sub_32);
  3566. Op0 = TmpReg;
  3567. }
  3568. return fastEmitInst_rii(Opc, RC, Op0, ImmR, ImmS);
  3569. }
  3570. unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg,
  3571. unsigned Op1Reg) {
  3572. unsigned Opc = 0;
  3573. bool NeedTrunc = false;
  3574. uint64_t Mask = 0;
  3575. switch (RetVT.SimpleTy) {
  3576. default: return 0;
  3577. case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
  3578. case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
  3579. case MVT::i32: Opc = AArch64::LSRVWr; break;
  3580. case MVT::i64: Opc = AArch64::LSRVXr; break;
  3581. }
  3582. const TargetRegisterClass *RC =
  3583. (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3584. if (NeedTrunc) {
  3585. Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Mask);
  3586. Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Mask);
  3587. }
  3588. Register ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op1Reg);
  3589. if (NeedTrunc)
  3590. ResultReg = emitAnd_ri(MVT::i32, ResultReg, Mask);
  3591. return ResultReg;
  3592. }
  3593. unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
  3594. uint64_t Shift, bool IsZExt) {
  3595. assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
  3596. "Unexpected source/return type pair.");
  3597. assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
  3598. SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
  3599. "Unexpected source value type.");
  3600. assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
  3601. RetVT == MVT::i64) && "Unexpected return value type.");
  3602. bool Is64Bit = (RetVT == MVT::i64);
  3603. unsigned RegSize = Is64Bit ? 64 : 32;
  3604. unsigned DstBits = RetVT.getSizeInBits();
  3605. unsigned SrcBits = SrcVT.getSizeInBits();
  3606. const TargetRegisterClass *RC =
  3607. Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3608. // Just emit a copy for "zero" shifts.
  3609. if (Shift == 0) {
  3610. if (RetVT == SrcVT) {
  3611. Register ResultReg = createResultReg(RC);
  3612. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3613. TII.get(TargetOpcode::COPY), ResultReg)
  3614. .addReg(Op0);
  3615. return ResultReg;
  3616. } else
  3617. return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
  3618. }
  3619. // Don't deal with undefined shifts.
  3620. if (Shift >= DstBits)
  3621. return 0;
  3622. // For immediate shifts we can fold the zero-/sign-extension into the shift.
  3623. // {S|U}BFM Wd, Wn, #r, #s
  3624. // Wd<s-r:0> = Wn<s:r> when r <= s
  3625. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3626. // %2 = lshr i16 %1, 4
  3627. // Wd<7-4:0> = Wn<7:4>
  3628. // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
  3629. // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
  3630. // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
  3631. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3632. // %2 = lshr i16 %1, 8
  3633. // Wd<7-7,0> = Wn<7:7>
  3634. // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
  3635. // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
  3636. // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
  3637. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3638. // %2 = lshr i16 %1, 12
  3639. // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
  3640. // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
  3641. // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
  3642. // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
  3643. if (Shift >= SrcBits && IsZExt)
  3644. return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
  3645. // It is not possible to fold a sign-extend into the LShr instruction. In this
  3646. // case emit a sign-extend.
  3647. if (!IsZExt) {
  3648. Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
  3649. if (!Op0)
  3650. return 0;
  3651. SrcVT = RetVT;
  3652. SrcBits = SrcVT.getSizeInBits();
  3653. IsZExt = true;
  3654. }
  3655. unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
  3656. unsigned ImmS = SrcBits - 1;
  3657. static const unsigned OpcTable[2][2] = {
  3658. {AArch64::SBFMWri, AArch64::SBFMXri},
  3659. {AArch64::UBFMWri, AArch64::UBFMXri}
  3660. };
  3661. unsigned Opc = OpcTable[IsZExt][Is64Bit];
  3662. if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
  3663. Register TmpReg = MRI.createVirtualRegister(RC);
  3664. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3665. TII.get(AArch64::SUBREG_TO_REG), TmpReg)
  3666. .addImm(0)
  3667. .addReg(Op0)
  3668. .addImm(AArch64::sub_32);
  3669. Op0 = TmpReg;
  3670. }
  3671. return fastEmitInst_rii(Opc, RC, Op0, ImmR, ImmS);
  3672. }
  3673. unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg,
  3674. unsigned Op1Reg) {
  3675. unsigned Opc = 0;
  3676. bool NeedTrunc = false;
  3677. uint64_t Mask = 0;
  3678. switch (RetVT.SimpleTy) {
  3679. default: return 0;
  3680. case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
  3681. case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
  3682. case MVT::i32: Opc = AArch64::ASRVWr; break;
  3683. case MVT::i64: Opc = AArch64::ASRVXr; break;
  3684. }
  3685. const TargetRegisterClass *RC =
  3686. (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3687. if (NeedTrunc) {
  3688. Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*isZExt=*/false);
  3689. Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Mask);
  3690. }
  3691. Register ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op1Reg);
  3692. if (NeedTrunc)
  3693. ResultReg = emitAnd_ri(MVT::i32, ResultReg, Mask);
  3694. return ResultReg;
  3695. }
  3696. unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
  3697. uint64_t Shift, bool IsZExt) {
  3698. assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
  3699. "Unexpected source/return type pair.");
  3700. assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
  3701. SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
  3702. "Unexpected source value type.");
  3703. assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
  3704. RetVT == MVT::i64) && "Unexpected return value type.");
  3705. bool Is64Bit = (RetVT == MVT::i64);
  3706. unsigned RegSize = Is64Bit ? 64 : 32;
  3707. unsigned DstBits = RetVT.getSizeInBits();
  3708. unsigned SrcBits = SrcVT.getSizeInBits();
  3709. const TargetRegisterClass *RC =
  3710. Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3711. // Just emit a copy for "zero" shifts.
  3712. if (Shift == 0) {
  3713. if (RetVT == SrcVT) {
  3714. Register ResultReg = createResultReg(RC);
  3715. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3716. TII.get(TargetOpcode::COPY), ResultReg)
  3717. .addReg(Op0);
  3718. return ResultReg;
  3719. } else
  3720. return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
  3721. }
  3722. // Don't deal with undefined shifts.
  3723. if (Shift >= DstBits)
  3724. return 0;
  3725. // For immediate shifts we can fold the zero-/sign-extension into the shift.
  3726. // {S|U}BFM Wd, Wn, #r, #s
  3727. // Wd<s-r:0> = Wn<s:r> when r <= s
  3728. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3729. // %2 = ashr i16 %1, 4
  3730. // Wd<7-4:0> = Wn<7:4>
  3731. // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
  3732. // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
  3733. // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
  3734. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3735. // %2 = ashr i16 %1, 8
  3736. // Wd<7-7,0> = Wn<7:7>
  3737. // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
  3738. // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
  3739. // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
  3740. // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
  3741. // %2 = ashr i16 %1, 12
  3742. // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
  3743. // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
  3744. // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
  3745. // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
  3746. if (Shift >= SrcBits && IsZExt)
  3747. return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
  3748. unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
  3749. unsigned ImmS = SrcBits - 1;
  3750. static const unsigned OpcTable[2][2] = {
  3751. {AArch64::SBFMWri, AArch64::SBFMXri},
  3752. {AArch64::UBFMWri, AArch64::UBFMXri}
  3753. };
  3754. unsigned Opc = OpcTable[IsZExt][Is64Bit];
  3755. if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
  3756. Register TmpReg = MRI.createVirtualRegister(RC);
  3757. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3758. TII.get(AArch64::SUBREG_TO_REG), TmpReg)
  3759. .addImm(0)
  3760. .addReg(Op0)
  3761. .addImm(AArch64::sub_32);
  3762. Op0 = TmpReg;
  3763. }
  3764. return fastEmitInst_rii(Opc, RC, Op0, ImmR, ImmS);
  3765. }
  3766. unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
  3767. bool IsZExt) {
  3768. assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
  3769. // FastISel does not have plumbing to deal with extensions where the SrcVT or
  3770. // DestVT are odd things, so test to make sure that they are both types we can
  3771. // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
  3772. // bail out to SelectionDAG.
  3773. if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
  3774. (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
  3775. ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
  3776. (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
  3777. return 0;
  3778. unsigned Opc;
  3779. unsigned Imm = 0;
  3780. switch (SrcVT.SimpleTy) {
  3781. default:
  3782. return 0;
  3783. case MVT::i1:
  3784. return emiti1Ext(SrcReg, DestVT, IsZExt);
  3785. case MVT::i8:
  3786. if (DestVT == MVT::i64)
  3787. Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
  3788. else
  3789. Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
  3790. Imm = 7;
  3791. break;
  3792. case MVT::i16:
  3793. if (DestVT == MVT::i64)
  3794. Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
  3795. else
  3796. Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
  3797. Imm = 15;
  3798. break;
  3799. case MVT::i32:
  3800. assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
  3801. Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
  3802. Imm = 31;
  3803. break;
  3804. }
  3805. // Handle i8 and i16 as i32.
  3806. if (DestVT == MVT::i8 || DestVT == MVT::i16)
  3807. DestVT = MVT::i32;
  3808. else if (DestVT == MVT::i64) {
  3809. Register Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
  3810. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3811. TII.get(AArch64::SUBREG_TO_REG), Src64)
  3812. .addImm(0)
  3813. .addReg(SrcReg)
  3814. .addImm(AArch64::sub_32);
  3815. SrcReg = Src64;
  3816. }
  3817. const TargetRegisterClass *RC =
  3818. (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3819. return fastEmitInst_rii(Opc, RC, SrcReg, 0, Imm);
  3820. }
  3821. static bool isZExtLoad(const MachineInstr *LI) {
  3822. switch (LI->getOpcode()) {
  3823. default:
  3824. return false;
  3825. case AArch64::LDURBBi:
  3826. case AArch64::LDURHHi:
  3827. case AArch64::LDURWi:
  3828. case AArch64::LDRBBui:
  3829. case AArch64::LDRHHui:
  3830. case AArch64::LDRWui:
  3831. case AArch64::LDRBBroX:
  3832. case AArch64::LDRHHroX:
  3833. case AArch64::LDRWroX:
  3834. case AArch64::LDRBBroW:
  3835. case AArch64::LDRHHroW:
  3836. case AArch64::LDRWroW:
  3837. return true;
  3838. }
  3839. }
  3840. static bool isSExtLoad(const MachineInstr *LI) {
  3841. switch (LI->getOpcode()) {
  3842. default:
  3843. return false;
  3844. case AArch64::LDURSBWi:
  3845. case AArch64::LDURSHWi:
  3846. case AArch64::LDURSBXi:
  3847. case AArch64::LDURSHXi:
  3848. case AArch64::LDURSWi:
  3849. case AArch64::LDRSBWui:
  3850. case AArch64::LDRSHWui:
  3851. case AArch64::LDRSBXui:
  3852. case AArch64::LDRSHXui:
  3853. case AArch64::LDRSWui:
  3854. case AArch64::LDRSBWroX:
  3855. case AArch64::LDRSHWroX:
  3856. case AArch64::LDRSBXroX:
  3857. case AArch64::LDRSHXroX:
  3858. case AArch64::LDRSWroX:
  3859. case AArch64::LDRSBWroW:
  3860. case AArch64::LDRSHWroW:
  3861. case AArch64::LDRSBXroW:
  3862. case AArch64::LDRSHXroW:
  3863. case AArch64::LDRSWroW:
  3864. return true;
  3865. }
  3866. }
  3867. bool AArch64FastISel::optimizeIntExtLoad(const Instruction *I, MVT RetVT,
  3868. MVT SrcVT) {
  3869. const auto *LI = dyn_cast<LoadInst>(I->getOperand(0));
  3870. if (!LI || !LI->hasOneUse())
  3871. return false;
  3872. // Check if the load instruction has already been selected.
  3873. Register Reg = lookUpRegForValue(LI);
  3874. if (!Reg)
  3875. return false;
  3876. MachineInstr *MI = MRI.getUniqueVRegDef(Reg);
  3877. if (!MI)
  3878. return false;
  3879. // Check if the correct load instruction has been emitted - SelectionDAG might
  3880. // have emitted a zero-extending load, but we need a sign-extending load.
  3881. bool IsZExt = isa<ZExtInst>(I);
  3882. const auto *LoadMI = MI;
  3883. if (LoadMI->getOpcode() == TargetOpcode::COPY &&
  3884. LoadMI->getOperand(1).getSubReg() == AArch64::sub_32) {
  3885. Register LoadReg = MI->getOperand(1).getReg();
  3886. LoadMI = MRI.getUniqueVRegDef(LoadReg);
  3887. assert(LoadMI && "Expected valid instruction");
  3888. }
  3889. if (!(IsZExt && isZExtLoad(LoadMI)) && !(!IsZExt && isSExtLoad(LoadMI)))
  3890. return false;
  3891. // Nothing to be done.
  3892. if (RetVT != MVT::i64 || SrcVT > MVT::i32) {
  3893. updateValueMap(I, Reg);
  3894. return true;
  3895. }
  3896. if (IsZExt) {
  3897. Register Reg64 = createResultReg(&AArch64::GPR64RegClass);
  3898. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3899. TII.get(AArch64::SUBREG_TO_REG), Reg64)
  3900. .addImm(0)
  3901. .addReg(Reg, getKillRegState(true))
  3902. .addImm(AArch64::sub_32);
  3903. Reg = Reg64;
  3904. } else {
  3905. assert((MI->getOpcode() == TargetOpcode::COPY &&
  3906. MI->getOperand(1).getSubReg() == AArch64::sub_32) &&
  3907. "Expected copy instruction");
  3908. Reg = MI->getOperand(1).getReg();
  3909. MachineBasicBlock::iterator I(MI);
  3910. removeDeadCode(I, std::next(I));
  3911. }
  3912. updateValueMap(I, Reg);
  3913. return true;
  3914. }
  3915. bool AArch64FastISel::selectIntExt(const Instruction *I) {
  3916. assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
  3917. "Unexpected integer extend instruction.");
  3918. MVT RetVT;
  3919. MVT SrcVT;
  3920. if (!isTypeSupported(I->getType(), RetVT))
  3921. return false;
  3922. if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
  3923. return false;
  3924. // Try to optimize already sign-/zero-extended values from load instructions.
  3925. if (optimizeIntExtLoad(I, RetVT, SrcVT))
  3926. return true;
  3927. Register SrcReg = getRegForValue(I->getOperand(0));
  3928. if (!SrcReg)
  3929. return false;
  3930. // Try to optimize already sign-/zero-extended values from function arguments.
  3931. bool IsZExt = isa<ZExtInst>(I);
  3932. if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
  3933. if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
  3934. if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
  3935. Register ResultReg = createResultReg(&AArch64::GPR64RegClass);
  3936. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  3937. TII.get(AArch64::SUBREG_TO_REG), ResultReg)
  3938. .addImm(0)
  3939. .addReg(SrcReg)
  3940. .addImm(AArch64::sub_32);
  3941. SrcReg = ResultReg;
  3942. }
  3943. updateValueMap(I, SrcReg);
  3944. return true;
  3945. }
  3946. }
  3947. unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
  3948. if (!ResultReg)
  3949. return false;
  3950. updateValueMap(I, ResultReg);
  3951. return true;
  3952. }
  3953. bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
  3954. EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
  3955. if (!DestEVT.isSimple())
  3956. return false;
  3957. MVT DestVT = DestEVT.getSimpleVT();
  3958. if (DestVT != MVT::i64 && DestVT != MVT::i32)
  3959. return false;
  3960. unsigned DivOpc;
  3961. bool Is64bit = (DestVT == MVT::i64);
  3962. switch (ISDOpcode) {
  3963. default:
  3964. return false;
  3965. case ISD::SREM:
  3966. DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
  3967. break;
  3968. case ISD::UREM:
  3969. DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
  3970. break;
  3971. }
  3972. unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
  3973. Register Src0Reg = getRegForValue(I->getOperand(0));
  3974. if (!Src0Reg)
  3975. return false;
  3976. Register Src1Reg = getRegForValue(I->getOperand(1));
  3977. if (!Src1Reg)
  3978. return false;
  3979. const TargetRegisterClass *RC =
  3980. (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
  3981. Register QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, Src1Reg);
  3982. assert(QuotReg && "Unexpected DIV instruction emission failure.");
  3983. // The remainder is computed as numerator - (quotient * denominator) using the
  3984. // MSUB instruction.
  3985. Register ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, Src1Reg, Src0Reg);
  3986. updateValueMap(I, ResultReg);
  3987. return true;
  3988. }
  3989. bool AArch64FastISel::selectMul(const Instruction *I) {
  3990. MVT VT;
  3991. if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
  3992. return false;
  3993. if (VT.isVector())
  3994. return selectBinaryOp(I, ISD::MUL);
  3995. const Value *Src0 = I->getOperand(0);
  3996. const Value *Src1 = I->getOperand(1);
  3997. if (const auto *C = dyn_cast<ConstantInt>(Src0))
  3998. if (C->getValue().isPowerOf2())
  3999. std::swap(Src0, Src1);
  4000. // Try to simplify to a shift instruction.
  4001. if (const auto *C = dyn_cast<ConstantInt>(Src1))
  4002. if (C->getValue().isPowerOf2()) {
  4003. uint64_t ShiftVal = C->getValue().logBase2();
  4004. MVT SrcVT = VT;
  4005. bool IsZExt = true;
  4006. if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
  4007. if (!isIntExtFree(ZExt)) {
  4008. MVT VT;
  4009. if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
  4010. SrcVT = VT;
  4011. IsZExt = true;
  4012. Src0 = ZExt->getOperand(0);
  4013. }
  4014. }
  4015. } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
  4016. if (!isIntExtFree(SExt)) {
  4017. MVT VT;
  4018. if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
  4019. SrcVT = VT;
  4020. IsZExt = false;
  4021. Src0 = SExt->getOperand(0);
  4022. }
  4023. }
  4024. }
  4025. Register Src0Reg = getRegForValue(Src0);
  4026. if (!Src0Reg)
  4027. return false;
  4028. unsigned ResultReg =
  4029. emitLSL_ri(VT, SrcVT, Src0Reg, ShiftVal, IsZExt);
  4030. if (ResultReg) {
  4031. updateValueMap(I, ResultReg);
  4032. return true;
  4033. }
  4034. }
  4035. Register Src0Reg = getRegForValue(I->getOperand(0));
  4036. if (!Src0Reg)
  4037. return false;
  4038. Register Src1Reg = getRegForValue(I->getOperand(1));
  4039. if (!Src1Reg)
  4040. return false;
  4041. unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src1Reg);
  4042. if (!ResultReg)
  4043. return false;
  4044. updateValueMap(I, ResultReg);
  4045. return true;
  4046. }
  4047. bool AArch64FastISel::selectShift(const Instruction *I) {
  4048. MVT RetVT;
  4049. if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
  4050. return false;
  4051. if (RetVT.isVector())
  4052. return selectOperator(I, I->getOpcode());
  4053. if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
  4054. unsigned ResultReg = 0;
  4055. uint64_t ShiftVal = C->getZExtValue();
  4056. MVT SrcVT = RetVT;
  4057. bool IsZExt = I->getOpcode() != Instruction::AShr;
  4058. const Value *Op0 = I->getOperand(0);
  4059. if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
  4060. if (!isIntExtFree(ZExt)) {
  4061. MVT TmpVT;
  4062. if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
  4063. SrcVT = TmpVT;
  4064. IsZExt = true;
  4065. Op0 = ZExt->getOperand(0);
  4066. }
  4067. }
  4068. } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
  4069. if (!isIntExtFree(SExt)) {
  4070. MVT TmpVT;
  4071. if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
  4072. SrcVT = TmpVT;
  4073. IsZExt = false;
  4074. Op0 = SExt->getOperand(0);
  4075. }
  4076. }
  4077. }
  4078. Register Op0Reg = getRegForValue(Op0);
  4079. if (!Op0Reg)
  4080. return false;
  4081. switch (I->getOpcode()) {
  4082. default: llvm_unreachable("Unexpected instruction.");
  4083. case Instruction::Shl:
  4084. ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, ShiftVal, IsZExt);
  4085. break;
  4086. case Instruction::AShr:
  4087. ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, ShiftVal, IsZExt);
  4088. break;
  4089. case Instruction::LShr:
  4090. ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, ShiftVal, IsZExt);
  4091. break;
  4092. }
  4093. if (!ResultReg)
  4094. return false;
  4095. updateValueMap(I, ResultReg);
  4096. return true;
  4097. }
  4098. Register Op0Reg = getRegForValue(I->getOperand(0));
  4099. if (!Op0Reg)
  4100. return false;
  4101. Register Op1Reg = getRegForValue(I->getOperand(1));
  4102. if (!Op1Reg)
  4103. return false;
  4104. unsigned ResultReg = 0;
  4105. switch (I->getOpcode()) {
  4106. default: llvm_unreachable("Unexpected instruction.");
  4107. case Instruction::Shl:
  4108. ResultReg = emitLSL_rr(RetVT, Op0Reg, Op1Reg);
  4109. break;
  4110. case Instruction::AShr:
  4111. ResultReg = emitASR_rr(RetVT, Op0Reg, Op1Reg);
  4112. break;
  4113. case Instruction::LShr:
  4114. ResultReg = emitLSR_rr(RetVT, Op0Reg, Op1Reg);
  4115. break;
  4116. }
  4117. if (!ResultReg)
  4118. return false;
  4119. updateValueMap(I, ResultReg);
  4120. return true;
  4121. }
  4122. bool AArch64FastISel::selectBitCast(const Instruction *I) {
  4123. MVT RetVT, SrcVT;
  4124. if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
  4125. return false;
  4126. if (!isTypeLegal(I->getType(), RetVT))
  4127. return false;
  4128. unsigned Opc;
  4129. if (RetVT == MVT::f32 && SrcVT == MVT::i32)
  4130. Opc = AArch64::FMOVWSr;
  4131. else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
  4132. Opc = AArch64::FMOVXDr;
  4133. else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
  4134. Opc = AArch64::FMOVSWr;
  4135. else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
  4136. Opc = AArch64::FMOVDXr;
  4137. else
  4138. return false;
  4139. const TargetRegisterClass *RC = nullptr;
  4140. switch (RetVT.SimpleTy) {
  4141. default: llvm_unreachable("Unexpected value type.");
  4142. case MVT::i32: RC = &AArch64::GPR32RegClass; break;
  4143. case MVT::i64: RC = &AArch64::GPR64RegClass; break;
  4144. case MVT::f32: RC = &AArch64::FPR32RegClass; break;
  4145. case MVT::f64: RC = &AArch64::FPR64RegClass; break;
  4146. }
  4147. Register Op0Reg = getRegForValue(I->getOperand(0));
  4148. if (!Op0Reg)
  4149. return false;
  4150. Register ResultReg = fastEmitInst_r(Opc, RC, Op0Reg);
  4151. if (!ResultReg)
  4152. return false;
  4153. updateValueMap(I, ResultReg);
  4154. return true;
  4155. }
  4156. bool AArch64FastISel::selectFRem(const Instruction *I) {
  4157. MVT RetVT;
  4158. if (!isTypeLegal(I->getType(), RetVT))
  4159. return false;
  4160. RTLIB::Libcall LC;
  4161. switch (RetVT.SimpleTy) {
  4162. default:
  4163. return false;
  4164. case MVT::f32:
  4165. LC = RTLIB::REM_F32;
  4166. break;
  4167. case MVT::f64:
  4168. LC = RTLIB::REM_F64;
  4169. break;
  4170. }
  4171. ArgListTy Args;
  4172. Args.reserve(I->getNumOperands());
  4173. // Populate the argument list.
  4174. for (auto &Arg : I->operands()) {
  4175. ArgListEntry Entry;
  4176. Entry.Val = Arg;
  4177. Entry.Ty = Arg->getType();
  4178. Args.push_back(Entry);
  4179. }
  4180. CallLoweringInfo CLI;
  4181. MCContext &Ctx = MF->getContext();
  4182. CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), I->getType(),
  4183. TLI.getLibcallName(LC), std::move(Args));
  4184. if (!lowerCallTo(CLI))
  4185. return false;
  4186. updateValueMap(I, CLI.ResultReg);
  4187. return true;
  4188. }
  4189. bool AArch64FastISel::selectSDiv(const Instruction *I) {
  4190. MVT VT;
  4191. if (!isTypeLegal(I->getType(), VT))
  4192. return false;
  4193. if (!isa<ConstantInt>(I->getOperand(1)))
  4194. return selectBinaryOp(I, ISD::SDIV);
  4195. const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
  4196. if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
  4197. !(C.isPowerOf2() || C.isNegatedPowerOf2()))
  4198. return selectBinaryOp(I, ISD::SDIV);
  4199. unsigned Lg2 = C.countTrailingZeros();
  4200. Register Src0Reg = getRegForValue(I->getOperand(0));
  4201. if (!Src0Reg)
  4202. return false;
  4203. if (cast<BinaryOperator>(I)->isExact()) {
  4204. unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Lg2);
  4205. if (!ResultReg)
  4206. return false;
  4207. updateValueMap(I, ResultReg);
  4208. return true;
  4209. }
  4210. int64_t Pow2MinusOne = (1ULL << Lg2) - 1;
  4211. unsigned AddReg = emitAdd_ri_(VT, Src0Reg, Pow2MinusOne);
  4212. if (!AddReg)
  4213. return false;
  4214. // (Src0 < 0) ? Pow2 - 1 : 0;
  4215. if (!emitICmp_ri(VT, Src0Reg, 0))
  4216. return false;
  4217. unsigned SelectOpc;
  4218. const TargetRegisterClass *RC;
  4219. if (VT == MVT::i64) {
  4220. SelectOpc = AArch64::CSELXr;
  4221. RC = &AArch64::GPR64RegClass;
  4222. } else {
  4223. SelectOpc = AArch64::CSELWr;
  4224. RC = &AArch64::GPR32RegClass;
  4225. }
  4226. Register SelectReg = fastEmitInst_rri(SelectOpc, RC, AddReg, Src0Reg,
  4227. AArch64CC::LT);
  4228. if (!SelectReg)
  4229. return false;
  4230. // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
  4231. // negate the result.
  4232. unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
  4233. unsigned ResultReg;
  4234. if (C.isNegative())
  4235. ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, SelectReg,
  4236. AArch64_AM::ASR, Lg2);
  4237. else
  4238. ResultReg = emitASR_ri(VT, VT, SelectReg, Lg2);
  4239. if (!ResultReg)
  4240. return false;
  4241. updateValueMap(I, ResultReg);
  4242. return true;
  4243. }
  4244. /// This is mostly a copy of the existing FastISel getRegForGEPIndex code. We
  4245. /// have to duplicate it for AArch64, because otherwise we would fail during the
  4246. /// sign-extend emission.
  4247. unsigned AArch64FastISel::getRegForGEPIndex(const Value *Idx) {
  4248. Register IdxN = getRegForValue(Idx);
  4249. if (IdxN == 0)
  4250. // Unhandled operand. Halt "fast" selection and bail.
  4251. return 0;
  4252. // If the index is smaller or larger than intptr_t, truncate or extend it.
  4253. MVT PtrVT = TLI.getPointerTy(DL);
  4254. EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
  4255. if (IdxVT.bitsLT(PtrVT)) {
  4256. IdxN = emitIntExt(IdxVT.getSimpleVT(), IdxN, PtrVT, /*isZExt=*/false);
  4257. } else if (IdxVT.bitsGT(PtrVT))
  4258. llvm_unreachable("AArch64 FastISel doesn't support types larger than i64");
  4259. return IdxN;
  4260. }
  4261. /// This is mostly a copy of the existing FastISel GEP code, but we have to
  4262. /// duplicate it for AArch64, because otherwise we would bail out even for
  4263. /// simple cases. This is because the standard fastEmit functions don't cover
  4264. /// MUL at all and ADD is lowered very inefficientily.
  4265. bool AArch64FastISel::selectGetElementPtr(const Instruction *I) {
  4266. if (Subtarget->isTargetILP32())
  4267. return false;
  4268. Register N = getRegForValue(I->getOperand(0));
  4269. if (!N)
  4270. return false;
  4271. // Keep a running tab of the total offset to coalesce multiple N = N + Offset
  4272. // into a single N = N + TotalOffset.
  4273. uint64_t TotalOffs = 0;
  4274. MVT VT = TLI.getPointerTy(DL);
  4275. for (gep_type_iterator GTI = gep_type_begin(I), E = gep_type_end(I);
  4276. GTI != E; ++GTI) {
  4277. const Value *Idx = GTI.getOperand();
  4278. if (auto *StTy = GTI.getStructTypeOrNull()) {
  4279. unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
  4280. // N = N + Offset
  4281. if (Field)
  4282. TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
  4283. } else {
  4284. Type *Ty = GTI.getIndexedType();
  4285. // If this is a constant subscript, handle it quickly.
  4286. if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
  4287. if (CI->isZero())
  4288. continue;
  4289. // N = N + Offset
  4290. TotalOffs +=
  4291. DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
  4292. continue;
  4293. }
  4294. if (TotalOffs) {
  4295. N = emitAdd_ri_(VT, N, TotalOffs);
  4296. if (!N)
  4297. return false;
  4298. TotalOffs = 0;
  4299. }
  4300. // N = N + Idx * ElementSize;
  4301. uint64_t ElementSize = DL.getTypeAllocSize(Ty);
  4302. unsigned IdxN = getRegForGEPIndex(Idx);
  4303. if (!IdxN)
  4304. return false;
  4305. if (ElementSize != 1) {
  4306. unsigned C = fastEmit_i(VT, VT, ISD::Constant, ElementSize);
  4307. if (!C)
  4308. return false;
  4309. IdxN = emitMul_rr(VT, IdxN, C);
  4310. if (!IdxN)
  4311. return false;
  4312. }
  4313. N = fastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
  4314. if (!N)
  4315. return false;
  4316. }
  4317. }
  4318. if (TotalOffs) {
  4319. N = emitAdd_ri_(VT, N, TotalOffs);
  4320. if (!N)
  4321. return false;
  4322. }
  4323. updateValueMap(I, N);
  4324. return true;
  4325. }
  4326. bool AArch64FastISel::selectAtomicCmpXchg(const AtomicCmpXchgInst *I) {
  4327. assert(TM.getOptLevel() == CodeGenOpt::None &&
  4328. "cmpxchg survived AtomicExpand at optlevel > -O0");
  4329. auto *RetPairTy = cast<StructType>(I->getType());
  4330. Type *RetTy = RetPairTy->getTypeAtIndex(0U);
  4331. assert(RetPairTy->getTypeAtIndex(1U)->isIntegerTy(1) &&
  4332. "cmpxchg has a non-i1 status result");
  4333. MVT VT;
  4334. if (!isTypeLegal(RetTy, VT))
  4335. return false;
  4336. const TargetRegisterClass *ResRC;
  4337. unsigned Opc, CmpOpc;
  4338. // This only supports i32/i64, because i8/i16 aren't legal, and the generic
  4339. // extractvalue selection doesn't support that.
  4340. if (VT == MVT::i32) {
  4341. Opc = AArch64::CMP_SWAP_32;
  4342. CmpOpc = AArch64::SUBSWrs;
  4343. ResRC = &AArch64::GPR32RegClass;
  4344. } else if (VT == MVT::i64) {
  4345. Opc = AArch64::CMP_SWAP_64;
  4346. CmpOpc = AArch64::SUBSXrs;
  4347. ResRC = &AArch64::GPR64RegClass;
  4348. } else {
  4349. return false;
  4350. }
  4351. const MCInstrDesc &II = TII.get(Opc);
  4352. const Register AddrReg = constrainOperandRegClass(
  4353. II, getRegForValue(I->getPointerOperand()), II.getNumDefs());
  4354. const Register DesiredReg = constrainOperandRegClass(
  4355. II, getRegForValue(I->getCompareOperand()), II.getNumDefs() + 1);
  4356. const Register NewReg = constrainOperandRegClass(
  4357. II, getRegForValue(I->getNewValOperand()), II.getNumDefs() + 2);
  4358. const Register ResultReg1 = createResultReg(ResRC);
  4359. const Register ResultReg2 = createResultReg(&AArch64::GPR32RegClass);
  4360. const Register ScratchReg = createResultReg(&AArch64::GPR32RegClass);
  4361. // FIXME: MachineMemOperand doesn't support cmpxchg yet.
  4362. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  4363. .addDef(ResultReg1)
  4364. .addDef(ScratchReg)
  4365. .addUse(AddrReg)
  4366. .addUse(DesiredReg)
  4367. .addUse(NewReg);
  4368. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
  4369. .addDef(VT == MVT::i32 ? AArch64::WZR : AArch64::XZR)
  4370. .addUse(ResultReg1)
  4371. .addUse(DesiredReg)
  4372. .addImm(0);
  4373. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr))
  4374. .addDef(ResultReg2)
  4375. .addUse(AArch64::WZR)
  4376. .addUse(AArch64::WZR)
  4377. .addImm(AArch64CC::NE);
  4378. assert((ResultReg1 + 1) == ResultReg2 && "Nonconsecutive result registers.");
  4379. updateValueMap(I, ResultReg1, 2);
  4380. return true;
  4381. }
  4382. bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
  4383. switch (I->getOpcode()) {
  4384. default:
  4385. break;
  4386. case Instruction::Add:
  4387. case Instruction::Sub:
  4388. return selectAddSub(I);
  4389. case Instruction::Mul:
  4390. return selectMul(I);
  4391. case Instruction::SDiv:
  4392. return selectSDiv(I);
  4393. case Instruction::SRem:
  4394. if (!selectBinaryOp(I, ISD::SREM))
  4395. return selectRem(I, ISD::SREM);
  4396. return true;
  4397. case Instruction::URem:
  4398. if (!selectBinaryOp(I, ISD::UREM))
  4399. return selectRem(I, ISD::UREM);
  4400. return true;
  4401. case Instruction::Shl:
  4402. case Instruction::LShr:
  4403. case Instruction::AShr:
  4404. return selectShift(I);
  4405. case Instruction::And:
  4406. case Instruction::Or:
  4407. case Instruction::Xor:
  4408. return selectLogicalOp(I);
  4409. case Instruction::Br:
  4410. return selectBranch(I);
  4411. case Instruction::IndirectBr:
  4412. return selectIndirectBr(I);
  4413. case Instruction::BitCast:
  4414. if (!FastISel::selectBitCast(I))
  4415. return selectBitCast(I);
  4416. return true;
  4417. case Instruction::FPToSI:
  4418. if (!selectCast(I, ISD::FP_TO_SINT))
  4419. return selectFPToInt(I, /*Signed=*/true);
  4420. return true;
  4421. case Instruction::FPToUI:
  4422. return selectFPToInt(I, /*Signed=*/false);
  4423. case Instruction::ZExt:
  4424. case Instruction::SExt:
  4425. return selectIntExt(I);
  4426. case Instruction::Trunc:
  4427. if (!selectCast(I, ISD::TRUNCATE))
  4428. return selectTrunc(I);
  4429. return true;
  4430. case Instruction::FPExt:
  4431. return selectFPExt(I);
  4432. case Instruction::FPTrunc:
  4433. return selectFPTrunc(I);
  4434. case Instruction::SIToFP:
  4435. if (!selectCast(I, ISD::SINT_TO_FP))
  4436. return selectIntToFP(I, /*Signed=*/true);
  4437. return true;
  4438. case Instruction::UIToFP:
  4439. return selectIntToFP(I, /*Signed=*/false);
  4440. case Instruction::Load:
  4441. return selectLoad(I);
  4442. case Instruction::Store:
  4443. return selectStore(I);
  4444. case Instruction::FCmp:
  4445. case Instruction::ICmp:
  4446. return selectCmp(I);
  4447. case Instruction::Select:
  4448. return selectSelect(I);
  4449. case Instruction::Ret:
  4450. return selectRet(I);
  4451. case Instruction::FRem:
  4452. return selectFRem(I);
  4453. case Instruction::GetElementPtr:
  4454. return selectGetElementPtr(I);
  4455. case Instruction::AtomicCmpXchg:
  4456. return selectAtomicCmpXchg(cast<AtomicCmpXchgInst>(I));
  4457. }
  4458. // fall-back to target-independent instruction selection.
  4459. return selectOperator(I, I->getOpcode());
  4460. }
  4461. FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
  4462. const TargetLibraryInfo *LibInfo) {
  4463. return new AArch64FastISel(FuncInfo, LibInfo);
  4464. }