AArch64FastISel.cpp 165 KB

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