LegalizeIntegerTypes.cpp 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249
  1. //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
  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 implements integer type expansion and promotion for LegalizeTypes.
  10. // Promotion is the act of changing a computation in an illegal type into a
  11. // computation in a larger type. For example, implementing i8 arithmetic in an
  12. // i32 register (often needed on powerpc).
  13. // Expansion is the act of changing a computation in an illegal type into a
  14. // computation in two identical registers of a smaller type. For example,
  15. // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
  16. // targets).
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "LegalizeTypes.h"
  20. #include "llvm/IR/DerivedTypes.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/KnownBits.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "legalize-types"
  26. //===----------------------------------------------------------------------===//
  27. // Integer Result Promotion
  28. //===----------------------------------------------------------------------===//
  29. /// PromoteIntegerResult - This method is called when a result of a node is
  30. /// found to be in need of promotion to a larger type. At this point, the node
  31. /// may also have invalid operands or may have other results that need
  32. /// expansion, we just know that (at least) one result needs promotion.
  33. void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
  34. LLVM_DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG);
  35. dbgs() << "\n");
  36. SDValue Res = SDValue();
  37. // See if the target wants to custom expand this node.
  38. if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
  39. LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
  40. return;
  41. }
  42. switch (N->getOpcode()) {
  43. default:
  44. #ifndef NDEBUG
  45. dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
  46. N->dump(&DAG); dbgs() << "\n";
  47. #endif
  48. llvm_unreachable("Do not know how to promote this operator!");
  49. case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
  50. case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
  51. case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
  52. case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
  53. case ISD::BITREVERSE: Res = PromoteIntRes_BITREVERSE(N); break;
  54. case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
  55. case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
  56. case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
  57. case ISD::CTLZ_ZERO_UNDEF:
  58. case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
  59. case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break;
  60. case ISD::CTTZ_ZERO_UNDEF:
  61. case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
  62. case ISD::EXTRACT_VECTOR_ELT:
  63. Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
  64. case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
  65. case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
  66. break;
  67. case ISD::MGATHER: Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
  68. break;
  69. case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
  70. case ISD::VSELECT: Res = PromoteIntRes_VSELECT(N); break;
  71. case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
  72. case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
  73. case ISD::SMIN:
  74. case ISD::SMAX: Res = PromoteIntRes_SExtIntBinOp(N); break;
  75. case ISD::UMIN:
  76. case ISD::UMAX: Res = PromoteIntRes_ZExtIntBinOp(N); break;
  77. case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
  78. case ISD::SIGN_EXTEND_INREG:
  79. Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
  80. case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
  81. case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
  82. case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
  83. case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
  84. case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
  85. case ISD::EXTRACT_SUBVECTOR:
  86. Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
  87. case ISD::VECTOR_SHUFFLE:
  88. Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
  89. case ISD::INSERT_VECTOR_ELT:
  90. Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
  91. case ISD::BUILD_VECTOR:
  92. Res = PromoteIntRes_BUILD_VECTOR(N); break;
  93. case ISD::SCALAR_TO_VECTOR:
  94. Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
  95. case ISD::CONCAT_VECTORS:
  96. Res = PromoteIntRes_CONCAT_VECTORS(N); break;
  97. case ISD::ANY_EXTEND_VECTOR_INREG:
  98. case ISD::SIGN_EXTEND_VECTOR_INREG:
  99. case ISD::ZERO_EXTEND_VECTOR_INREG:
  100. Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
  101. case ISD::SIGN_EXTEND:
  102. case ISD::ZERO_EXTEND:
  103. case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
  104. case ISD::STRICT_FP_TO_SINT:
  105. case ISD::STRICT_FP_TO_UINT:
  106. case ISD::FP_TO_SINT:
  107. case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
  108. case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break;
  109. case ISD::FLT_ROUNDS_: Res = PromoteIntRes_FLT_ROUNDS(N); break;
  110. case ISD::AND:
  111. case ISD::OR:
  112. case ISD::XOR:
  113. case ISD::ADD:
  114. case ISD::SUB:
  115. case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
  116. case ISD::SDIV:
  117. case ISD::SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
  118. case ISD::UDIV:
  119. case ISD::UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
  120. case ISD::SADDO:
  121. case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
  122. case ISD::UADDO:
  123. case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
  124. case ISD::SMULO:
  125. case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
  126. case ISD::ADDE:
  127. case ISD::SUBE:
  128. case ISD::ADDCARRY:
  129. case ISD::SUBCARRY: Res = PromoteIntRes_ADDSUBCARRY(N, ResNo); break;
  130. case ISD::SADDSAT:
  131. case ISD::UADDSAT:
  132. case ISD::SSUBSAT:
  133. case ISD::USUBSAT: Res = PromoteIntRes_ADDSUBSAT(N); break;
  134. case ISD::SMULFIX:
  135. case ISD::SMULFIXSAT:
  136. case ISD::UMULFIX:
  137. case ISD::UMULFIXSAT: Res = PromoteIntRes_MULFIX(N); break;
  138. case ISD::ABS: Res = PromoteIntRes_ABS(N); break;
  139. case ISD::ATOMIC_LOAD:
  140. Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
  141. case ISD::ATOMIC_LOAD_ADD:
  142. case ISD::ATOMIC_LOAD_SUB:
  143. case ISD::ATOMIC_LOAD_AND:
  144. case ISD::ATOMIC_LOAD_CLR:
  145. case ISD::ATOMIC_LOAD_OR:
  146. case ISD::ATOMIC_LOAD_XOR:
  147. case ISD::ATOMIC_LOAD_NAND:
  148. case ISD::ATOMIC_LOAD_MIN:
  149. case ISD::ATOMIC_LOAD_MAX:
  150. case ISD::ATOMIC_LOAD_UMIN:
  151. case ISD::ATOMIC_LOAD_UMAX:
  152. case ISD::ATOMIC_SWAP:
  153. Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
  154. case ISD::ATOMIC_CMP_SWAP:
  155. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
  156. Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
  157. break;
  158. case ISD::VECREDUCE_ADD:
  159. case ISD::VECREDUCE_MUL:
  160. case ISD::VECREDUCE_AND:
  161. case ISD::VECREDUCE_OR:
  162. case ISD::VECREDUCE_XOR:
  163. case ISD::VECREDUCE_SMAX:
  164. case ISD::VECREDUCE_SMIN:
  165. case ISD::VECREDUCE_UMAX:
  166. case ISD::VECREDUCE_UMIN:
  167. Res = PromoteIntRes_VECREDUCE(N);
  168. break;
  169. }
  170. // If the result is null then the sub-method took care of registering it.
  171. if (Res.getNode())
  172. SetPromotedInteger(SDValue(N, ResNo), Res);
  173. }
  174. SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
  175. unsigned ResNo) {
  176. SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
  177. return GetPromotedInteger(Op);
  178. }
  179. SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
  180. // Sign-extend the new bits, and continue the assertion.
  181. SDValue Op = SExtPromotedInteger(N->getOperand(0));
  182. return DAG.getNode(ISD::AssertSext, SDLoc(N),
  183. Op.getValueType(), Op, N->getOperand(1));
  184. }
  185. SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
  186. // Zero the new bits, and continue the assertion.
  187. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  188. return DAG.getNode(ISD::AssertZext, SDLoc(N),
  189. Op.getValueType(), Op, N->getOperand(1));
  190. }
  191. SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
  192. EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  193. SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
  194. N->getMemoryVT(), ResVT,
  195. N->getChain(), N->getBasePtr(),
  196. N->getMemOperand());
  197. // Legalize the chain result - switch anything that used the old chain to
  198. // use the new one.
  199. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  200. return Res;
  201. }
  202. SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
  203. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  204. SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
  205. N->getMemoryVT(),
  206. N->getChain(), N->getBasePtr(),
  207. Op2, N->getMemOperand());
  208. // Legalize the chain result - switch anything that used the old chain to
  209. // use the new one.
  210. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  211. return Res;
  212. }
  213. SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
  214. unsigned ResNo) {
  215. if (ResNo == 1) {
  216. assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
  217. EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
  218. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
  219. // Only use the result of getSetCCResultType if it is legal,
  220. // otherwise just use the promoted result type (NVT).
  221. if (!TLI.isTypeLegal(SVT))
  222. SVT = NVT;
  223. SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
  224. SDValue Res = DAG.getAtomicCmpSwap(
  225. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
  226. N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
  227. N->getMemOperand());
  228. ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
  229. ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
  230. return Res.getValue(1);
  231. }
  232. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  233. SDValue Op3 = GetPromotedInteger(N->getOperand(3));
  234. SDVTList VTs =
  235. DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
  236. SDValue Res = DAG.getAtomicCmpSwap(
  237. N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
  238. N->getBasePtr(), Op2, Op3, N->getMemOperand());
  239. // Update the use to N with the newly created Res.
  240. for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
  241. ReplaceValueWith(SDValue(N, i), Res.getValue(i));
  242. return Res;
  243. }
  244. SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
  245. SDValue InOp = N->getOperand(0);
  246. EVT InVT = InOp.getValueType();
  247. EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
  248. EVT OutVT = N->getValueType(0);
  249. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  250. SDLoc dl(N);
  251. switch (getTypeAction(InVT)) {
  252. case TargetLowering::TypeLegal:
  253. break;
  254. case TargetLowering::TypePromoteInteger:
  255. if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
  256. // The input promotes to the same size. Convert the promoted value.
  257. return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
  258. break;
  259. case TargetLowering::TypeSoftenFloat:
  260. // Promote the integer operand by hand.
  261. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
  262. case TargetLowering::TypePromoteFloat: {
  263. // Convert the promoted float by hand.
  264. if (!NOutVT.isVector())
  265. return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, GetPromotedFloat(InOp));
  266. break;
  267. }
  268. case TargetLowering::TypeExpandInteger:
  269. case TargetLowering::TypeExpandFloat:
  270. break;
  271. case TargetLowering::TypeScalarizeVector:
  272. // Convert the element to an integer and promote it by hand.
  273. if (!NOutVT.isVector())
  274. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
  275. BitConvertToInteger(GetScalarizedVector(InOp)));
  276. break;
  277. case TargetLowering::TypeSplitVector: {
  278. if (!NOutVT.isVector()) {
  279. // For example, i32 = BITCAST v2i16 on alpha. Convert the split
  280. // pieces of the input into integers and reassemble in the final type.
  281. SDValue Lo, Hi;
  282. GetSplitVector(N->getOperand(0), Lo, Hi);
  283. Lo = BitConvertToInteger(Lo);
  284. Hi = BitConvertToInteger(Hi);
  285. if (DAG.getDataLayout().isBigEndian())
  286. std::swap(Lo, Hi);
  287. InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
  288. EVT::getIntegerVT(*DAG.getContext(),
  289. NOutVT.getSizeInBits()),
  290. JoinIntegers(Lo, Hi));
  291. return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
  292. }
  293. break;
  294. }
  295. case TargetLowering::TypeWidenVector:
  296. // The input is widened to the same size. Convert to the widened value.
  297. // Make sure that the outgoing value is not a vector, because this would
  298. // make us bitcast between two vectors which are legalized in different ways.
  299. if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector())
  300. return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
  301. // If the output type is also a vector and widening it to the same size
  302. // as the widened input type would be a legal type, we can widen the bitcast
  303. // and handle the promotion after.
  304. if (NOutVT.isVector()) {
  305. unsigned WidenInSize = NInVT.getSizeInBits();
  306. unsigned OutSize = OutVT.getSizeInBits();
  307. if (WidenInSize % OutSize == 0) {
  308. unsigned Scale = WidenInSize / OutSize;
  309. EVT WideOutVT = EVT::getVectorVT(*DAG.getContext(),
  310. OutVT.getVectorElementType(),
  311. OutVT.getVectorNumElements() * Scale);
  312. if (isTypeLegal(WideOutVT)) {
  313. InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
  314. MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
  315. InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
  316. DAG.getConstant(0, dl, IdxTy));
  317. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
  318. }
  319. }
  320. }
  321. }
  322. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
  323. CreateStackStoreLoad(InOp, OutVT));
  324. }
  325. // Helper for BSWAP/BITREVERSE promotion to ensure we can fit the shift amount
  326. // in the VT returned by getShiftAmountTy and to return a safe VT if we can't.
  327. static EVT getShiftAmountTyForConstant(unsigned Val, EVT VT,
  328. const TargetLowering &TLI,
  329. SelectionDAG &DAG) {
  330. EVT ShiftVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
  331. // If the value won't fit in the prefered type, just use something safe. It
  332. // will be legalized when the shift is expanded.
  333. if ((Log2_32(Val) + 1) > ShiftVT.getScalarSizeInBits())
  334. ShiftVT = MVT::i32;
  335. return ShiftVT;
  336. }
  337. SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
  338. SDValue Op = GetPromotedInteger(N->getOperand(0));
  339. EVT OVT = N->getValueType(0);
  340. EVT NVT = Op.getValueType();
  341. SDLoc dl(N);
  342. unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
  343. EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
  344. return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
  345. DAG.getConstant(DiffBits, dl, ShiftVT));
  346. }
  347. SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
  348. SDValue Op = GetPromotedInteger(N->getOperand(0));
  349. EVT OVT = N->getValueType(0);
  350. EVT NVT = Op.getValueType();
  351. SDLoc dl(N);
  352. unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
  353. EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
  354. return DAG.getNode(ISD::SRL, dl, NVT,
  355. DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
  356. DAG.getConstant(DiffBits, dl, ShiftVT));
  357. }
  358. SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
  359. // The pair element type may be legal, or may not promote to the same type as
  360. // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
  361. return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
  362. TLI.getTypeToTransformTo(*DAG.getContext(),
  363. N->getValueType(0)), JoinIntegers(N->getOperand(0),
  364. N->getOperand(1)));
  365. }
  366. SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
  367. EVT VT = N->getValueType(0);
  368. // FIXME there is no actual debug info here
  369. SDLoc dl(N);
  370. // Zero extend things like i1, sign extend everything else. It shouldn't
  371. // matter in theory which one we pick, but this tends to give better code?
  372. unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
  373. SDValue Result = DAG.getNode(Opc, dl,
  374. TLI.getTypeToTransformTo(*DAG.getContext(), VT),
  375. SDValue(N, 0));
  376. assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
  377. return Result;
  378. }
  379. SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
  380. // Zero extend to the promoted type and do the count there.
  381. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  382. SDLoc dl(N);
  383. EVT OVT = N->getValueType(0);
  384. EVT NVT = Op.getValueType();
  385. Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
  386. // Subtract off the extra leading bits in the bigger type.
  387. return DAG.getNode(
  388. ISD::SUB, dl, NVT, Op,
  389. DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
  390. NVT));
  391. }
  392. SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
  393. // Zero extend to the promoted type and do the count there.
  394. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  395. return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op);
  396. }
  397. SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
  398. SDValue Op = GetPromotedInteger(N->getOperand(0));
  399. EVT OVT = N->getValueType(0);
  400. EVT NVT = Op.getValueType();
  401. SDLoc dl(N);
  402. if (N->getOpcode() == ISD::CTTZ) {
  403. // The count is the same in the promoted type except if the original
  404. // value was zero. This can be handled by setting the bit just off
  405. // the top of the original type.
  406. auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
  407. OVT.getScalarSizeInBits());
  408. Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
  409. }
  410. return DAG.getNode(N->getOpcode(), dl, NVT, Op);
  411. }
  412. SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
  413. SDLoc dl(N);
  414. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  415. SDValue Op0 = N->getOperand(0);
  416. SDValue Op1 = N->getOperand(1);
  417. // If the input also needs to be promoted, do that first so we can get a
  418. // get a good idea for the output type.
  419. if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
  420. == TargetLowering::TypePromoteInteger) {
  421. SDValue In = GetPromotedInteger(Op0);
  422. // If the new type is larger than NVT, use it. We probably won't need to
  423. // promote it again.
  424. EVT SVT = In.getValueType().getScalarType();
  425. if (SVT.bitsGE(NVT)) {
  426. SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
  427. return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
  428. }
  429. }
  430. return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
  431. }
  432. SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
  433. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  434. unsigned NewOpc = N->getOpcode();
  435. SDLoc dl(N);
  436. // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
  437. // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
  438. // and SINT conversions are Custom, there is no way to tell which is
  439. // preferable. We choose SINT because that's the right thing on PPC.)
  440. if (N->getOpcode() == ISD::FP_TO_UINT &&
  441. !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
  442. TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
  443. NewOpc = ISD::FP_TO_SINT;
  444. if (N->getOpcode() == ISD::STRICT_FP_TO_UINT &&
  445. !TLI.isOperationLegal(ISD::STRICT_FP_TO_UINT, NVT) &&
  446. TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
  447. NewOpc = ISD::STRICT_FP_TO_SINT;
  448. SDValue Res;
  449. if (N->isStrictFPOpcode()) {
  450. Res = DAG.getNode(NewOpc, dl, { NVT, MVT::Other },
  451. { N->getOperand(0), N->getOperand(1) });
  452. // Legalize the chain result - switch anything that used the old chain to
  453. // use the new one.
  454. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  455. } else
  456. Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
  457. // Assert that the converted value fits in the original type. If it doesn't
  458. // (eg: because the value being converted is too big), then the result of the
  459. // original operation was undefined anyway, so the assert is still correct.
  460. //
  461. // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
  462. // before legalization: fp-to-uint16, 65534. -> 0xfffe
  463. // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
  464. return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
  465. N->getOpcode() == ISD::STRICT_FP_TO_UINT) ?
  466. ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
  467. DAG.getValueType(N->getValueType(0).getScalarType()));
  468. }
  469. SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
  470. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  471. SDLoc dl(N);
  472. return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  473. }
  474. SDValue DAGTypeLegalizer::PromoteIntRes_FLT_ROUNDS(SDNode *N) {
  475. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  476. SDLoc dl(N);
  477. return DAG.getNode(N->getOpcode(), dl, NVT);
  478. }
  479. SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
  480. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  481. SDLoc dl(N);
  482. if (getTypeAction(N->getOperand(0).getValueType())
  483. == TargetLowering::TypePromoteInteger) {
  484. SDValue Res = GetPromotedInteger(N->getOperand(0));
  485. assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
  486. // If the result and operand types are the same after promotion, simplify
  487. // to an in-register extension.
  488. if (NVT == Res.getValueType()) {
  489. // The high bits are not guaranteed to be anything. Insert an extend.
  490. if (N->getOpcode() == ISD::SIGN_EXTEND)
  491. return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
  492. DAG.getValueType(N->getOperand(0).getValueType()));
  493. if (N->getOpcode() == ISD::ZERO_EXTEND)
  494. return DAG.getZeroExtendInReg(Res, dl,
  495. N->getOperand(0).getValueType().getScalarType());
  496. assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
  497. return Res;
  498. }
  499. }
  500. // Otherwise, just extend the original operand all the way to the larger type.
  501. return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  502. }
  503. SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
  504. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  505. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  506. ISD::LoadExtType ExtType =
  507. ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
  508. SDLoc dl(N);
  509. SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
  510. N->getMemoryVT(), N->getMemOperand());
  511. // Legalize the chain result - switch anything that used the old chain to
  512. // use the new one.
  513. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  514. return Res;
  515. }
  516. SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
  517. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  518. SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
  519. SDLoc dl(N);
  520. SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
  521. N->getMask(), ExtPassThru, N->getMemoryVT(),
  522. N->getMemOperand(), ISD::EXTLOAD);
  523. // Legalize the chain result - switch anything that used the old chain to
  524. // use the new one.
  525. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  526. return Res;
  527. }
  528. SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
  529. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  530. SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
  531. assert(NVT == ExtPassThru.getValueType() &&
  532. "Gather result type and the passThru agrument type should be the same");
  533. SDLoc dl(N);
  534. SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
  535. N->getIndex(), N->getScale() };
  536. SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
  537. N->getMemoryVT(), dl, Ops,
  538. N->getMemOperand(), N->getIndexType());
  539. // Legalize the chain result - switch anything that used the old chain to
  540. // use the new one.
  541. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  542. return Res;
  543. }
  544. /// Promote the overflow flag of an overflowing arithmetic node.
  545. SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
  546. // Change the return type of the boolean result while obeying
  547. // getSetCCResultType.
  548. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
  549. EVT VT = N->getValueType(0);
  550. EVT SVT = getSetCCResultType(VT);
  551. SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
  552. unsigned NumOps = N->getNumOperands();
  553. assert(NumOps <= 3 && "Too many operands");
  554. if (NumOps == 3)
  555. Ops[2] = N->getOperand(2);
  556. SDLoc dl(N);
  557. SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
  558. makeArrayRef(Ops, NumOps));
  559. // Modified the sum result - switch anything that used the old sum to use
  560. // the new one.
  561. ReplaceValueWith(SDValue(N, 0), Res);
  562. // Convert to the expected type.
  563. return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
  564. }
  565. SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSAT(SDNode *N) {
  566. // For promoting iN -> iM, this can be expanded by
  567. // 1. ANY_EXTEND iN to iM
  568. // 2. SHL by M-N
  569. // 3. [US][ADD|SUB]SAT
  570. // 4. L/ASHR by M-N
  571. SDLoc dl(N);
  572. SDValue Op1 = N->getOperand(0);
  573. SDValue Op2 = N->getOperand(1);
  574. unsigned OldBits = Op1.getScalarValueSizeInBits();
  575. unsigned Opcode = N->getOpcode();
  576. unsigned ShiftOp;
  577. switch (Opcode) {
  578. case ISD::SADDSAT:
  579. case ISD::SSUBSAT:
  580. ShiftOp = ISD::SRA;
  581. break;
  582. case ISD::UADDSAT:
  583. case ISD::USUBSAT:
  584. ShiftOp = ISD::SRL;
  585. break;
  586. default:
  587. llvm_unreachable("Expected opcode to be signed or unsigned saturation "
  588. "addition or subtraction");
  589. }
  590. SDValue Op1Promoted = GetPromotedInteger(Op1);
  591. SDValue Op2Promoted = GetPromotedInteger(Op2);
  592. EVT PromotedType = Op1Promoted.getValueType();
  593. unsigned NewBits = PromotedType.getScalarSizeInBits();
  594. unsigned SHLAmount = NewBits - OldBits;
  595. EVT SHVT = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
  596. SDValue ShiftAmount = DAG.getConstant(SHLAmount, dl, SHVT);
  597. Op1Promoted =
  598. DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted, ShiftAmount);
  599. Op2Promoted =
  600. DAG.getNode(ISD::SHL, dl, PromotedType, Op2Promoted, ShiftAmount);
  601. SDValue Result =
  602. DAG.getNode(Opcode, dl, PromotedType, Op1Promoted, Op2Promoted);
  603. return DAG.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
  604. }
  605. SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
  606. // Can just promote the operands then continue with operation.
  607. SDLoc dl(N);
  608. SDValue Op1Promoted, Op2Promoted;
  609. bool Signed =
  610. N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
  611. bool Saturating =
  612. N->getOpcode() == ISD::SMULFIXSAT || N->getOpcode() == ISD::UMULFIXSAT;
  613. if (Signed) {
  614. Op1Promoted = SExtPromotedInteger(N->getOperand(0));
  615. Op2Promoted = SExtPromotedInteger(N->getOperand(1));
  616. } else {
  617. Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
  618. Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
  619. }
  620. EVT OldType = N->getOperand(0).getValueType();
  621. EVT PromotedType = Op1Promoted.getValueType();
  622. unsigned DiffSize =
  623. PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
  624. if (Saturating) {
  625. // Promoting the operand and result values changes the saturation width,
  626. // which is extends the values that we clamp to on saturation. This could be
  627. // resolved by shifting one of the operands the same amount, which would
  628. // also shift the result we compare against, then shifting back.
  629. EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
  630. Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
  631. DAG.getConstant(DiffSize, dl, ShiftTy));
  632. SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
  633. Op2Promoted, N->getOperand(2));
  634. unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
  635. return DAG.getNode(ShiftOp, dl, PromotedType, Result,
  636. DAG.getConstant(DiffSize, dl, ShiftTy));
  637. }
  638. return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
  639. N->getOperand(2));
  640. }
  641. SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
  642. if (ResNo == 1)
  643. return PromoteIntRes_Overflow(N);
  644. // The operation overflowed iff the result in the larger type is not the
  645. // sign extension of its truncation to the original type.
  646. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  647. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  648. EVT OVT = N->getOperand(0).getValueType();
  649. EVT NVT = LHS.getValueType();
  650. SDLoc dl(N);
  651. // Do the arithmetic in the larger type.
  652. unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
  653. SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
  654. // Calculate the overflow flag: sign extend the arithmetic result from
  655. // the original type.
  656. SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
  657. DAG.getValueType(OVT));
  658. // Overflowed if and only if this is not equal to Res.
  659. Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
  660. // Use the calculated overflow everywhere.
  661. ReplaceValueWith(SDValue(N, 1), Ofl);
  662. return Res;
  663. }
  664. SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
  665. SDValue LHS = GetPromotedInteger(N->getOperand(1));
  666. SDValue RHS = GetPromotedInteger(N->getOperand(2));
  667. return DAG.getSelect(SDLoc(N),
  668. LHS.getValueType(), N->getOperand(0), LHS, RHS);
  669. }
  670. SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
  671. SDValue Mask = N->getOperand(0);
  672. SDValue LHS = GetPromotedInteger(N->getOperand(1));
  673. SDValue RHS = GetPromotedInteger(N->getOperand(2));
  674. return DAG.getNode(ISD::VSELECT, SDLoc(N),
  675. LHS.getValueType(), Mask, LHS, RHS);
  676. }
  677. SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
  678. SDValue LHS = GetPromotedInteger(N->getOperand(2));
  679. SDValue RHS = GetPromotedInteger(N->getOperand(3));
  680. return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
  681. LHS.getValueType(), N->getOperand(0),
  682. N->getOperand(1), LHS, RHS, N->getOperand(4));
  683. }
  684. SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
  685. EVT InVT = N->getOperand(0).getValueType();
  686. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  687. EVT SVT = getSetCCResultType(InVT);
  688. // If we got back a type that needs to be promoted, this likely means the
  689. // the input type also needs to be promoted. So get the promoted type for
  690. // the input and try the query again.
  691. if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
  692. if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
  693. InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
  694. SVT = getSetCCResultType(InVT);
  695. } else {
  696. // Input type isn't promoted, just use the default promoted type.
  697. SVT = NVT;
  698. }
  699. }
  700. SDLoc dl(N);
  701. assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() &&
  702. "Vector compare must return a vector result!");
  703. // Get the SETCC result using the canonical SETCC type.
  704. SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
  705. N->getOperand(1), N->getOperand(2));
  706. // Convert to the expected type.
  707. return DAG.getSExtOrTrunc(SetCC, dl, NVT);
  708. }
  709. SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
  710. SDValue LHS = GetPromotedInteger(N->getOperand(0));
  711. SDValue RHS = N->getOperand(1);
  712. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
  713. RHS = ZExtPromotedInteger(RHS);
  714. return DAG.getNode(ISD::SHL, SDLoc(N), LHS.getValueType(), LHS, RHS);
  715. }
  716. SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
  717. SDValue Op = GetPromotedInteger(N->getOperand(0));
  718. return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
  719. Op.getValueType(), Op, N->getOperand(1));
  720. }
  721. SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
  722. // The input may have strange things in the top bits of the registers, but
  723. // these operations don't care. They may have weird bits going out, but
  724. // that too is okay if they are integer operations.
  725. SDValue LHS = GetPromotedInteger(N->getOperand(0));
  726. SDValue RHS = GetPromotedInteger(N->getOperand(1));
  727. return DAG.getNode(N->getOpcode(), SDLoc(N),
  728. LHS.getValueType(), LHS, RHS);
  729. }
  730. SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
  731. // Sign extend the input.
  732. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  733. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  734. return DAG.getNode(N->getOpcode(), SDLoc(N),
  735. LHS.getValueType(), LHS, RHS);
  736. }
  737. SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
  738. // Zero extend the input.
  739. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  740. SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
  741. return DAG.getNode(N->getOpcode(), SDLoc(N),
  742. LHS.getValueType(), LHS, RHS);
  743. }
  744. SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
  745. // The input value must be properly sign extended.
  746. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  747. SDValue RHS = N->getOperand(1);
  748. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
  749. RHS = ZExtPromotedInteger(RHS);
  750. return DAG.getNode(ISD::SRA, SDLoc(N), LHS.getValueType(), LHS, RHS);
  751. }
  752. SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
  753. // The input value must be properly zero extended.
  754. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  755. SDValue RHS = N->getOperand(1);
  756. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
  757. RHS = ZExtPromotedInteger(RHS);
  758. return DAG.getNode(ISD::SRL, SDLoc(N), LHS.getValueType(), LHS, RHS);
  759. }
  760. SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
  761. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  762. SDValue Res;
  763. SDValue InOp = N->getOperand(0);
  764. SDLoc dl(N);
  765. switch (getTypeAction(InOp.getValueType())) {
  766. default: llvm_unreachable("Unknown type action!");
  767. case TargetLowering::TypeLegal:
  768. case TargetLowering::TypeExpandInteger:
  769. Res = InOp;
  770. break;
  771. case TargetLowering::TypePromoteInteger:
  772. Res = GetPromotedInteger(InOp);
  773. break;
  774. case TargetLowering::TypeSplitVector: {
  775. EVT InVT = InOp.getValueType();
  776. assert(InVT.isVector() && "Cannot split scalar types");
  777. unsigned NumElts = InVT.getVectorNumElements();
  778. assert(NumElts == NVT.getVectorNumElements() &&
  779. "Dst and Src must have the same number of elements");
  780. assert(isPowerOf2_32(NumElts) &&
  781. "Promoted vector type must be a power of two");
  782. SDValue EOp1, EOp2;
  783. GetSplitVector(InOp, EOp1, EOp2);
  784. EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
  785. NumElts/2);
  786. EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
  787. EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
  788. return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
  789. }
  790. case TargetLowering::TypeWidenVector: {
  791. SDValue WideInOp = GetWidenedVector(InOp);
  792. // Truncate widened InOp.
  793. unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
  794. EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
  795. N->getValueType(0).getScalarType(), NumElem);
  796. SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
  797. // Zero extend so that the elements are of same type as those of NVT
  798. EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
  799. NumElem);
  800. SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
  801. // Extract the low NVT subvector.
  802. MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
  803. SDValue ZeroIdx = DAG.getConstant(0, dl, IdxTy);
  804. return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
  805. }
  806. }
  807. // Truncate to NVT instead of VT
  808. return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
  809. }
  810. SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
  811. if (ResNo == 1)
  812. return PromoteIntRes_Overflow(N);
  813. // The operation overflowed iff the result in the larger type is not the
  814. // zero extension of its truncation to the original type.
  815. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  816. SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
  817. EVT OVT = N->getOperand(0).getValueType();
  818. EVT NVT = LHS.getValueType();
  819. SDLoc dl(N);
  820. // Do the arithmetic in the larger type.
  821. unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
  822. SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
  823. // Calculate the overflow flag: zero extend the arithmetic result from
  824. // the original type.
  825. SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT.getScalarType());
  826. // Overflowed if and only if this is not equal to Res.
  827. Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
  828. // Use the calculated overflow everywhere.
  829. ReplaceValueWith(SDValue(N, 1), Ofl);
  830. return Res;
  831. }
  832. // Handle promotion for the ADDE/SUBE/ADDCARRY/SUBCARRY nodes. Notice that
  833. // the third operand of ADDE/SUBE nodes is carry flag, which differs from
  834. // the ADDCARRY/SUBCARRY nodes in that the third operand is carry Boolean.
  835. SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBCARRY(SDNode *N, unsigned ResNo) {
  836. if (ResNo == 1)
  837. return PromoteIntRes_Overflow(N);
  838. // We need to sign-extend the operands so the carry value computed by the
  839. // wide operation will be equivalent to the carry value computed by the
  840. // narrow operation.
  841. // An ADDCARRY can generate carry only if any of the operands has its
  842. // most significant bit set. Sign extension propagates the most significant
  843. // bit into the higher bits which means the extra bit that the narrow
  844. // addition would need (i.e. the carry) will be propagated through the higher
  845. // bits of the wide addition.
  846. // A SUBCARRY can generate borrow only if LHS < RHS and this property will be
  847. // preserved by sign extension.
  848. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  849. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  850. EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
  851. // Do the arithmetic in the wide type.
  852. SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
  853. LHS, RHS, N->getOperand(2));
  854. // Update the users of the original carry/borrow value.
  855. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  856. return SDValue(Res.getNode(), 0);
  857. }
  858. SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
  859. SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
  860. return DAG.getNode(ISD::ABS, SDLoc(N), Op0.getValueType(), Op0);
  861. }
  862. SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
  863. // Promote the overflow bit trivially.
  864. if (ResNo == 1)
  865. return PromoteIntRes_Overflow(N);
  866. SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
  867. SDLoc DL(N);
  868. EVT SmallVT = LHS.getValueType();
  869. // To determine if the result overflowed in a larger type, we extend the
  870. // input to the larger type, do the multiply (checking if it overflows),
  871. // then also check the high bits of the result to see if overflow happened
  872. // there.
  873. if (N->getOpcode() == ISD::SMULO) {
  874. LHS = SExtPromotedInteger(LHS);
  875. RHS = SExtPromotedInteger(RHS);
  876. } else {
  877. LHS = ZExtPromotedInteger(LHS);
  878. RHS = ZExtPromotedInteger(RHS);
  879. }
  880. SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
  881. SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
  882. // Overflow occurred if it occurred in the larger type, or if the high part
  883. // of the result does not zero/sign-extend the low part. Check this second
  884. // possibility first.
  885. SDValue Overflow;
  886. if (N->getOpcode() == ISD::UMULO) {
  887. // Unsigned overflow occurred if the high part is non-zero.
  888. unsigned Shift = SmallVT.getScalarSizeInBits();
  889. EVT ShiftTy = getShiftAmountTyForConstant(Shift, Mul.getValueType(),
  890. TLI, DAG);
  891. SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
  892. DAG.getConstant(Shift, DL, ShiftTy));
  893. Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
  894. DAG.getConstant(0, DL, Hi.getValueType()),
  895. ISD::SETNE);
  896. } else {
  897. // Signed overflow occurred if the high part does not sign extend the low.
  898. SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
  899. Mul, DAG.getValueType(SmallVT));
  900. Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
  901. }
  902. // The only other way for overflow to occur is if the multiplication in the
  903. // larger type itself overflowed.
  904. Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
  905. SDValue(Mul.getNode(), 1));
  906. // Use the calculated overflow everywhere.
  907. ReplaceValueWith(SDValue(N, 1), Overflow);
  908. return Mul;
  909. }
  910. SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
  911. return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
  912. N->getValueType(0)));
  913. }
  914. SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
  915. SDValue Chain = N->getOperand(0); // Get the chain.
  916. SDValue Ptr = N->getOperand(1); // Get the pointer.
  917. EVT VT = N->getValueType(0);
  918. SDLoc dl(N);
  919. MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
  920. unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
  921. // The argument is passed as NumRegs registers of type RegVT.
  922. SmallVector<SDValue, 8> Parts(NumRegs);
  923. for (unsigned i = 0; i < NumRegs; ++i) {
  924. Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
  925. N->getConstantOperandVal(3));
  926. Chain = Parts[i].getValue(1);
  927. }
  928. // Handle endianness of the load.
  929. if (DAG.getDataLayout().isBigEndian())
  930. std::reverse(Parts.begin(), Parts.end());
  931. // Assemble the parts in the promoted type.
  932. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  933. SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
  934. for (unsigned i = 1; i < NumRegs; ++i) {
  935. SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
  936. // Shift it to the right position and "or" it in.
  937. Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
  938. DAG.getConstant(i * RegVT.getSizeInBits(), dl,
  939. TLI.getPointerTy(DAG.getDataLayout())));
  940. Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
  941. }
  942. // Modified the chain result - switch anything that used the old chain to
  943. // use the new one.
  944. ReplaceValueWith(SDValue(N, 1), Chain);
  945. return Res;
  946. }
  947. //===----------------------------------------------------------------------===//
  948. // Integer Operand Promotion
  949. //===----------------------------------------------------------------------===//
  950. /// PromoteIntegerOperand - This method is called when the specified operand of
  951. /// the specified node is found to need promotion. At this point, all of the
  952. /// result types of the node are known to be legal, but other operands of the
  953. /// node may need promotion or expansion as well as the specified one.
  954. bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
  955. LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG);
  956. dbgs() << "\n");
  957. SDValue Res = SDValue();
  958. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
  959. LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
  960. return false;
  961. }
  962. switch (N->getOpcode()) {
  963. default:
  964. #ifndef NDEBUG
  965. dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
  966. N->dump(&DAG); dbgs() << "\n";
  967. #endif
  968. llvm_unreachable("Do not know how to promote this operator's operand!");
  969. case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
  970. case ISD::ATOMIC_STORE:
  971. Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
  972. break;
  973. case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
  974. case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
  975. case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
  976. case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
  977. case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
  978. case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
  979. case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
  980. case ISD::INSERT_VECTOR_ELT:
  981. Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
  982. case ISD::SCALAR_TO_VECTOR:
  983. Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
  984. case ISD::VSELECT:
  985. case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
  986. case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
  987. case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
  988. case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
  989. case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
  990. case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
  991. OpNo); break;
  992. case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
  993. OpNo); break;
  994. case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
  995. OpNo); break;
  996. case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
  997. OpNo); break;
  998. case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
  999. OpNo); break;
  1000. case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
  1001. case ISD::FP16_TO_FP:
  1002. case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
  1003. case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
  1004. case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
  1005. case ISD::SHL:
  1006. case ISD::SRA:
  1007. case ISD::SRL:
  1008. case ISD::ROTL:
  1009. case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
  1010. case ISD::ADDCARRY:
  1011. case ISD::SUBCARRY: Res = PromoteIntOp_ADDSUBCARRY(N, OpNo); break;
  1012. case ISD::FRAMEADDR:
  1013. case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
  1014. case ISD::PREFETCH: Res = PromoteIntOp_PREFETCH(N, OpNo); break;
  1015. case ISD::SMULFIX:
  1016. case ISD::SMULFIXSAT:
  1017. case ISD::UMULFIX:
  1018. case ISD::UMULFIXSAT: Res = PromoteIntOp_MULFIX(N); break;
  1019. case ISD::FPOWI: Res = PromoteIntOp_FPOWI(N); break;
  1020. case ISD::VECREDUCE_ADD:
  1021. case ISD::VECREDUCE_MUL:
  1022. case ISD::VECREDUCE_AND:
  1023. case ISD::VECREDUCE_OR:
  1024. case ISD::VECREDUCE_XOR:
  1025. case ISD::VECREDUCE_SMAX:
  1026. case ISD::VECREDUCE_SMIN:
  1027. case ISD::VECREDUCE_UMAX:
  1028. case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
  1029. }
  1030. // If the result is null, the sub-method took care of registering results etc.
  1031. if (!Res.getNode()) return false;
  1032. // If the result is N, the sub-method updated N in place. Tell the legalizer
  1033. // core about this.
  1034. if (Res.getNode() == N)
  1035. return true;
  1036. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  1037. "Invalid operand expansion");
  1038. ReplaceValueWith(SDValue(N, 0), Res);
  1039. return false;
  1040. }
  1041. /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
  1042. /// shared among BR_CC, SELECT_CC, and SETCC handlers.
  1043. void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
  1044. ISD::CondCode CCCode) {
  1045. // We have to insert explicit sign or zero extends. Note that we could
  1046. // insert sign extends for ALL conditions. For those operations where either
  1047. // zero or sign extension would be valid, use SExtOrZExtPromotedInteger
  1048. // which will choose the cheapest for the target.
  1049. switch (CCCode) {
  1050. default: llvm_unreachable("Unknown integer comparison!");
  1051. case ISD::SETEQ:
  1052. case ISD::SETNE: {
  1053. SDValue OpL = GetPromotedInteger(NewLHS);
  1054. SDValue OpR = GetPromotedInteger(NewRHS);
  1055. // We would prefer to promote the comparison operand with sign extension.
  1056. // If the width of OpL/OpR excluding the duplicated sign bits is no greater
  1057. // than the width of NewLHS/NewRH, we can avoid inserting real truncate
  1058. // instruction, which is redundant eventually.
  1059. unsigned OpLEffectiveBits =
  1060. OpL.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpL) + 1;
  1061. unsigned OpREffectiveBits =
  1062. OpR.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpR) + 1;
  1063. if (OpLEffectiveBits <= NewLHS.getScalarValueSizeInBits() &&
  1064. OpREffectiveBits <= NewRHS.getScalarValueSizeInBits()) {
  1065. NewLHS = OpL;
  1066. NewRHS = OpR;
  1067. } else {
  1068. NewLHS = SExtOrZExtPromotedInteger(NewLHS);
  1069. NewRHS = SExtOrZExtPromotedInteger(NewRHS);
  1070. }
  1071. break;
  1072. }
  1073. case ISD::SETUGE:
  1074. case ISD::SETUGT:
  1075. case ISD::SETULE:
  1076. case ISD::SETULT:
  1077. NewLHS = SExtOrZExtPromotedInteger(NewLHS);
  1078. NewRHS = SExtOrZExtPromotedInteger(NewRHS);
  1079. break;
  1080. case ISD::SETGE:
  1081. case ISD::SETGT:
  1082. case ISD::SETLT:
  1083. case ISD::SETLE:
  1084. NewLHS = SExtPromotedInteger(NewLHS);
  1085. NewRHS = SExtPromotedInteger(NewRHS);
  1086. break;
  1087. }
  1088. }
  1089. SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
  1090. SDValue Op = GetPromotedInteger(N->getOperand(0));
  1091. return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
  1092. }
  1093. SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
  1094. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  1095. return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
  1096. N->getChain(), N->getBasePtr(), Op2, N->getMemOperand());
  1097. }
  1098. SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
  1099. // This should only occur in unusual situations like bitcasting to an
  1100. // x86_fp80, so just turn it into a store+load
  1101. return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
  1102. }
  1103. SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
  1104. assert(OpNo == 2 && "Don't know how to promote this operand!");
  1105. SDValue LHS = N->getOperand(2);
  1106. SDValue RHS = N->getOperand(3);
  1107. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
  1108. // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
  1109. // legal types.
  1110. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  1111. N->getOperand(1), LHS, RHS, N->getOperand(4)),
  1112. 0);
  1113. }
  1114. SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
  1115. assert(OpNo == 1 && "only know how to promote condition");
  1116. // Promote all the way up to the canonical SetCC type.
  1117. SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
  1118. // The chain (Op#0) and basic block destination (Op#2) are always legal types.
  1119. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
  1120. N->getOperand(2)), 0);
  1121. }
  1122. SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
  1123. // Since the result type is legal, the operands must promote to it.
  1124. EVT OVT = N->getOperand(0).getValueType();
  1125. SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
  1126. SDValue Hi = GetPromotedInteger(N->getOperand(1));
  1127. assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
  1128. SDLoc dl(N);
  1129. Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
  1130. DAG.getConstant(OVT.getSizeInBits(), dl,
  1131. TLI.getPointerTy(DAG.getDataLayout())));
  1132. return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
  1133. }
  1134. SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
  1135. // The vector type is legal but the element type is not. This implies
  1136. // that the vector is a power-of-two in length and that the element
  1137. // type does not have a strange size (eg: it is not i1).
  1138. EVT VecVT = N->getValueType(0);
  1139. unsigned NumElts = VecVT.getVectorNumElements();
  1140. assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
  1141. "Legal vector of one illegal element?");
  1142. // Promote the inserted value. The type does not need to match the
  1143. // vector element type. Check that any extra bits introduced will be
  1144. // truncated away.
  1145. assert(N->getOperand(0).getValueSizeInBits() >=
  1146. N->getValueType(0).getScalarSizeInBits() &&
  1147. "Type of inserted value narrower than vector element type!");
  1148. SmallVector<SDValue, 16> NewOps;
  1149. for (unsigned i = 0; i < NumElts; ++i)
  1150. NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
  1151. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  1152. }
  1153. SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
  1154. unsigned OpNo) {
  1155. if (OpNo == 1) {
  1156. // Promote the inserted value. This is valid because the type does not
  1157. // have to match the vector element type.
  1158. // Check that any extra bits introduced will be truncated away.
  1159. assert(N->getOperand(1).getValueSizeInBits() >=
  1160. N->getValueType(0).getScalarSizeInBits() &&
  1161. "Type of inserted value narrower than vector element type!");
  1162. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  1163. GetPromotedInteger(N->getOperand(1)),
  1164. N->getOperand(2)),
  1165. 0);
  1166. }
  1167. assert(OpNo == 2 && "Different operand and result vector types?");
  1168. // Promote the index.
  1169. SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
  1170. TLI.getVectorIdxTy(DAG.getDataLayout()));
  1171. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  1172. N->getOperand(1), Idx), 0);
  1173. }
  1174. SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
  1175. // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
  1176. // the operand in place.
  1177. return SDValue(DAG.UpdateNodeOperands(N,
  1178. GetPromotedInteger(N->getOperand(0))), 0);
  1179. }
  1180. SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
  1181. assert(OpNo == 0 && "Only know how to promote the condition!");
  1182. SDValue Cond = N->getOperand(0);
  1183. EVT OpTy = N->getOperand(1).getValueType();
  1184. if (N->getOpcode() == ISD::VSELECT)
  1185. if (SDValue Res = WidenVSELECTAndMask(N))
  1186. return Res;
  1187. // Promote all the way up to the canonical SetCC type.
  1188. EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
  1189. Cond = PromoteTargetBoolean(Cond, OpVT);
  1190. return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
  1191. N->getOperand(2)), 0);
  1192. }
  1193. SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
  1194. assert(OpNo == 0 && "Don't know how to promote this operand!");
  1195. SDValue LHS = N->getOperand(0);
  1196. SDValue RHS = N->getOperand(1);
  1197. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
  1198. // The CC (#4) and the possible return values (#2 and #3) have legal types.
  1199. return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
  1200. N->getOperand(3), N->getOperand(4)), 0);
  1201. }
  1202. SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
  1203. assert(OpNo == 0 && "Don't know how to promote this operand!");
  1204. SDValue LHS = N->getOperand(0);
  1205. SDValue RHS = N->getOperand(1);
  1206. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
  1207. // The CC (#2) is always legal.
  1208. return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
  1209. }
  1210. SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
  1211. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  1212. ZExtPromotedInteger(N->getOperand(1))), 0);
  1213. }
  1214. SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
  1215. SDValue Op = GetPromotedInteger(N->getOperand(0));
  1216. SDLoc dl(N);
  1217. Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
  1218. return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
  1219. Op, DAG.getValueType(N->getOperand(0).getValueType()));
  1220. }
  1221. SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
  1222. return SDValue(DAG.UpdateNodeOperands(N,
  1223. SExtPromotedInteger(N->getOperand(0))), 0);
  1224. }
  1225. SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
  1226. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  1227. SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
  1228. SDLoc dl(N);
  1229. SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
  1230. // Truncate the value and store the result.
  1231. return DAG.getTruncStore(Ch, dl, Val, Ptr,
  1232. N->getMemoryVT(), N->getMemOperand());
  1233. }
  1234. SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
  1235. unsigned OpNo) {
  1236. SDValue DataOp = N->getValue();
  1237. EVT DataVT = DataOp.getValueType();
  1238. SDValue Mask = N->getMask();
  1239. SDLoc dl(N);
  1240. bool TruncateStore = false;
  1241. if (OpNo == 3) {
  1242. Mask = PromoteTargetBoolean(Mask, DataVT);
  1243. // Update in place.
  1244. SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
  1245. NewOps[3] = Mask;
  1246. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  1247. } else { // Data operand
  1248. assert(OpNo == 1 && "Unexpected operand for promotion");
  1249. DataOp = GetPromotedInteger(DataOp);
  1250. TruncateStore = true;
  1251. }
  1252. return DAG.getMaskedStore(N->getChain(), dl, DataOp, N->getBasePtr(), Mask,
  1253. N->getMemoryVT(), N->getMemOperand(),
  1254. TruncateStore, N->isCompressingStore());
  1255. }
  1256. SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
  1257. unsigned OpNo) {
  1258. assert(OpNo == 2 && "Only know how to promote the mask!");
  1259. EVT DataVT = N->getValueType(0);
  1260. SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
  1261. SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
  1262. NewOps[OpNo] = Mask;
  1263. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  1264. }
  1265. SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
  1266. unsigned OpNo) {
  1267. SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
  1268. if (OpNo == 2) {
  1269. // The Mask
  1270. EVT DataVT = N->getValueType(0);
  1271. NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
  1272. } else if (OpNo == 4) {
  1273. // The Index
  1274. if (N->isIndexSigned())
  1275. // Need to sign extend the index since the bits will likely be used.
  1276. NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
  1277. else
  1278. NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
  1279. } else
  1280. NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
  1281. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  1282. }
  1283. SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
  1284. unsigned OpNo) {
  1285. SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
  1286. if (OpNo == 2) {
  1287. // The Mask
  1288. EVT DataVT = N->getValue().getValueType();
  1289. NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
  1290. } else if (OpNo == 4) {
  1291. // The Index
  1292. if (N->isIndexSigned())
  1293. // Need to sign extend the index since the bits will likely be used.
  1294. NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
  1295. else
  1296. NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
  1297. } else
  1298. NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
  1299. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  1300. }
  1301. SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
  1302. SDValue Op = GetPromotedInteger(N->getOperand(0));
  1303. return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
  1304. }
  1305. SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
  1306. return SDValue(DAG.UpdateNodeOperands(N,
  1307. ZExtPromotedInteger(N->getOperand(0))), 0);
  1308. }
  1309. SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
  1310. SDLoc dl(N);
  1311. SDValue Op = GetPromotedInteger(N->getOperand(0));
  1312. Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
  1313. return DAG.getZeroExtendInReg(Op, dl,
  1314. N->getOperand(0).getValueType().getScalarType());
  1315. }
  1316. SDValue DAGTypeLegalizer::PromoteIntOp_ADDSUBCARRY(SDNode *N, unsigned OpNo) {
  1317. assert(OpNo == 2 && "Don't know how to promote this operand!");
  1318. SDValue LHS = N->getOperand(0);
  1319. SDValue RHS = N->getOperand(1);
  1320. SDValue Carry = N->getOperand(2);
  1321. SDLoc DL(N);
  1322. Carry = PromoteTargetBoolean(Carry, LHS.getValueType());
  1323. return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, Carry), 0);
  1324. }
  1325. SDValue DAGTypeLegalizer::PromoteIntOp_MULFIX(SDNode *N) {
  1326. SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
  1327. return SDValue(
  1328. DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
  1329. }
  1330. SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
  1331. // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
  1332. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  1333. return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
  1334. }
  1335. SDValue DAGTypeLegalizer::PromoteIntOp_PREFETCH(SDNode *N, unsigned OpNo) {
  1336. assert(OpNo > 1 && "Don't know how to promote this operand!");
  1337. // Promote the rw, locality, and cache type arguments to a supported integer
  1338. // width.
  1339. SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
  1340. SDValue Op3 = ZExtPromotedInteger(N->getOperand(3));
  1341. SDValue Op4 = ZExtPromotedInteger(N->getOperand(4));
  1342. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
  1343. Op2, Op3, Op4),
  1344. 0);
  1345. }
  1346. SDValue DAGTypeLegalizer::PromoteIntOp_FPOWI(SDNode *N) {
  1347. SDValue Op = SExtPromotedInteger(N->getOperand(1));
  1348. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
  1349. }
  1350. SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
  1351. SDLoc dl(N);
  1352. SDValue Op;
  1353. switch (N->getOpcode()) {
  1354. default: llvm_unreachable("Expected integer vector reduction");
  1355. case ISD::VECREDUCE_ADD:
  1356. case ISD::VECREDUCE_MUL:
  1357. case ISD::VECREDUCE_AND:
  1358. case ISD::VECREDUCE_OR:
  1359. case ISD::VECREDUCE_XOR:
  1360. Op = GetPromotedInteger(N->getOperand(0));
  1361. break;
  1362. case ISD::VECREDUCE_SMAX:
  1363. case ISD::VECREDUCE_SMIN:
  1364. Op = SExtPromotedInteger(N->getOperand(0));
  1365. break;
  1366. case ISD::VECREDUCE_UMAX:
  1367. case ISD::VECREDUCE_UMIN:
  1368. Op = ZExtPromotedInteger(N->getOperand(0));
  1369. break;
  1370. }
  1371. EVT EltVT = Op.getValueType().getVectorElementType();
  1372. EVT VT = N->getValueType(0);
  1373. if (VT.bitsGE(EltVT))
  1374. return DAG.getNode(N->getOpcode(), SDLoc(N), VT, Op);
  1375. // Result size must be >= element size. If this is not the case after
  1376. // promotion, also promote the result type and then truncate.
  1377. SDValue Reduce = DAG.getNode(N->getOpcode(), dl, EltVT, Op);
  1378. return DAG.getNode(ISD::TRUNCATE, dl, VT, Reduce);
  1379. }
  1380. //===----------------------------------------------------------------------===//
  1381. // Integer Result Expansion
  1382. //===----------------------------------------------------------------------===//
  1383. /// ExpandIntegerResult - This method is called when the specified result of the
  1384. /// specified node is found to need expansion. At this point, the node may also
  1385. /// have invalid operands or may have other results that need promotion, we just
  1386. /// know that (at least) one result needs expansion.
  1387. void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
  1388. LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG);
  1389. dbgs() << "\n");
  1390. SDValue Lo, Hi;
  1391. Lo = Hi = SDValue();
  1392. // See if the target wants to custom expand this node.
  1393. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  1394. return;
  1395. switch (N->getOpcode()) {
  1396. default:
  1397. #ifndef NDEBUG
  1398. dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
  1399. N->dump(&DAG); dbgs() << "\n";
  1400. #endif
  1401. report_fatal_error("Do not know how to expand the result of this "
  1402. "operator!");
  1403. case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
  1404. case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
  1405. case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
  1406. case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
  1407. case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
  1408. case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
  1409. case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
  1410. case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
  1411. case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
  1412. case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
  1413. case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
  1414. case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
  1415. case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
  1416. case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
  1417. case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
  1418. case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
  1419. case ISD::CTLZ_ZERO_UNDEF:
  1420. case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
  1421. case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
  1422. case ISD::CTTZ_ZERO_UNDEF:
  1423. case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
  1424. case ISD::FLT_ROUNDS_: ExpandIntRes_FLT_ROUNDS(N, Lo, Hi); break;
  1425. case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
  1426. case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
  1427. case ISD::LLROUND: ExpandIntRes_LLROUND(N, Lo, Hi); break;
  1428. case ISD::LLRINT: ExpandIntRes_LLRINT(N, Lo, Hi); break;
  1429. case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
  1430. case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
  1431. case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
  1432. case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
  1433. case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
  1434. case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
  1435. case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
  1436. case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
  1437. case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
  1438. case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
  1439. case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
  1440. case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
  1441. case ISD::ATOMIC_LOAD_ADD:
  1442. case ISD::ATOMIC_LOAD_SUB:
  1443. case ISD::ATOMIC_LOAD_AND:
  1444. case ISD::ATOMIC_LOAD_CLR:
  1445. case ISD::ATOMIC_LOAD_OR:
  1446. case ISD::ATOMIC_LOAD_XOR:
  1447. case ISD::ATOMIC_LOAD_NAND:
  1448. case ISD::ATOMIC_LOAD_MIN:
  1449. case ISD::ATOMIC_LOAD_MAX:
  1450. case ISD::ATOMIC_LOAD_UMIN:
  1451. case ISD::ATOMIC_LOAD_UMAX:
  1452. case ISD::ATOMIC_SWAP:
  1453. case ISD::ATOMIC_CMP_SWAP: {
  1454. std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
  1455. SplitInteger(Tmp.first, Lo, Hi);
  1456. ReplaceValueWith(SDValue(N, 1), Tmp.second);
  1457. break;
  1458. }
  1459. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
  1460. AtomicSDNode *AN = cast<AtomicSDNode>(N);
  1461. SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
  1462. SDValue Tmp = DAG.getAtomicCmpSwap(
  1463. ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
  1464. N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
  1465. AN->getMemOperand());
  1466. // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
  1467. // success simply by comparing the loaded value against the ingoing
  1468. // comparison.
  1469. SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
  1470. N->getOperand(2), ISD::SETEQ);
  1471. SplitInteger(Tmp, Lo, Hi);
  1472. ReplaceValueWith(SDValue(N, 1), Success);
  1473. ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
  1474. break;
  1475. }
  1476. case ISD::AND:
  1477. case ISD::OR:
  1478. case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
  1479. case ISD::UMAX:
  1480. case ISD::SMAX:
  1481. case ISD::UMIN:
  1482. case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
  1483. case ISD::ADD:
  1484. case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
  1485. case ISD::ADDC:
  1486. case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
  1487. case ISD::ADDE:
  1488. case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
  1489. case ISD::ADDCARRY:
  1490. case ISD::SUBCARRY: ExpandIntRes_ADDSUBCARRY(N, Lo, Hi); break;
  1491. case ISD::SHL:
  1492. case ISD::SRA:
  1493. case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
  1494. case ISD::SADDO:
  1495. case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
  1496. case ISD::UADDO:
  1497. case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
  1498. case ISD::UMULO:
  1499. case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
  1500. case ISD::SADDSAT:
  1501. case ISD::UADDSAT:
  1502. case ISD::SSUBSAT:
  1503. case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
  1504. case ISD::SMULFIX:
  1505. case ISD::SMULFIXSAT:
  1506. case ISD::UMULFIX:
  1507. case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
  1508. case ISD::VECREDUCE_ADD:
  1509. case ISD::VECREDUCE_MUL:
  1510. case ISD::VECREDUCE_AND:
  1511. case ISD::VECREDUCE_OR:
  1512. case ISD::VECREDUCE_XOR:
  1513. case ISD::VECREDUCE_SMAX:
  1514. case ISD::VECREDUCE_SMIN:
  1515. case ISD::VECREDUCE_UMAX:
  1516. case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
  1517. }
  1518. // If Lo/Hi is null, the sub-method took care of registering results etc.
  1519. if (Lo.getNode())
  1520. SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
  1521. }
  1522. /// Lower an atomic node to the appropriate builtin call.
  1523. std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
  1524. unsigned Opc = Node->getOpcode();
  1525. MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
  1526. RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT);
  1527. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
  1528. return ExpandChainLibCall(LC, Node, false);
  1529. }
  1530. /// N is a shift by a value that needs to be expanded,
  1531. /// and the shift amount is a constant 'Amt'. Expand the operation.
  1532. void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
  1533. SDValue &Lo, SDValue &Hi) {
  1534. SDLoc DL(N);
  1535. // Expand the incoming operand to be shifted, so that we have its parts
  1536. SDValue InL, InH;
  1537. GetExpandedInteger(N->getOperand(0), InL, InH);
  1538. // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
  1539. // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
  1540. if (!Amt) {
  1541. Lo = InL;
  1542. Hi = InH;
  1543. return;
  1544. }
  1545. EVT NVT = InL.getValueType();
  1546. unsigned VTBits = N->getValueType(0).getSizeInBits();
  1547. unsigned NVTBits = NVT.getSizeInBits();
  1548. EVT ShTy = N->getOperand(1).getValueType();
  1549. if (N->getOpcode() == ISD::SHL) {
  1550. if (Amt.ugt(VTBits)) {
  1551. Lo = Hi = DAG.getConstant(0, DL, NVT);
  1552. } else if (Amt.ugt(NVTBits)) {
  1553. Lo = DAG.getConstant(0, DL, NVT);
  1554. Hi = DAG.getNode(ISD::SHL, DL,
  1555. NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
  1556. } else if (Amt == NVTBits) {
  1557. Lo = DAG.getConstant(0, DL, NVT);
  1558. Hi = InL;
  1559. } else {
  1560. Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
  1561. Hi = DAG.getNode(ISD::OR, DL, NVT,
  1562. DAG.getNode(ISD::SHL, DL, NVT, InH,
  1563. DAG.getConstant(Amt, DL, ShTy)),
  1564. DAG.getNode(ISD::SRL, DL, NVT, InL,
  1565. DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
  1566. }
  1567. return;
  1568. }
  1569. if (N->getOpcode() == ISD::SRL) {
  1570. if (Amt.ugt(VTBits)) {
  1571. Lo = Hi = DAG.getConstant(0, DL, NVT);
  1572. } else if (Amt.ugt(NVTBits)) {
  1573. Lo = DAG.getNode(ISD::SRL, DL,
  1574. NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
  1575. Hi = DAG.getConstant(0, DL, NVT);
  1576. } else if (Amt == NVTBits) {
  1577. Lo = InH;
  1578. Hi = DAG.getConstant(0, DL, NVT);
  1579. } else {
  1580. Lo = DAG.getNode(ISD::OR, DL, NVT,
  1581. DAG.getNode(ISD::SRL, DL, NVT, InL,
  1582. DAG.getConstant(Amt, DL, ShTy)),
  1583. DAG.getNode(ISD::SHL, DL, NVT, InH,
  1584. DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
  1585. Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
  1586. }
  1587. return;
  1588. }
  1589. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  1590. if (Amt.ugt(VTBits)) {
  1591. Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1592. DAG.getConstant(NVTBits - 1, DL, ShTy));
  1593. } else if (Amt.ugt(NVTBits)) {
  1594. Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1595. DAG.getConstant(Amt - NVTBits, DL, ShTy));
  1596. Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1597. DAG.getConstant(NVTBits - 1, DL, ShTy));
  1598. } else if (Amt == NVTBits) {
  1599. Lo = InH;
  1600. Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1601. DAG.getConstant(NVTBits - 1, DL, ShTy));
  1602. } else {
  1603. Lo = DAG.getNode(ISD::OR, DL, NVT,
  1604. DAG.getNode(ISD::SRL, DL, NVT, InL,
  1605. DAG.getConstant(Amt, DL, ShTy)),
  1606. DAG.getNode(ISD::SHL, DL, NVT, InH,
  1607. DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
  1608. Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
  1609. }
  1610. }
  1611. /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
  1612. /// this shift based on knowledge of the high bit of the shift amount. If we
  1613. /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
  1614. /// shift amount.
  1615. bool DAGTypeLegalizer::
  1616. ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
  1617. SDValue Amt = N->getOperand(1);
  1618. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1619. EVT ShTy = Amt.getValueType();
  1620. unsigned ShBits = ShTy.getScalarSizeInBits();
  1621. unsigned NVTBits = NVT.getScalarSizeInBits();
  1622. assert(isPowerOf2_32(NVTBits) &&
  1623. "Expanded integer type size not a power of two!");
  1624. SDLoc dl(N);
  1625. APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
  1626. KnownBits Known = DAG.computeKnownBits(N->getOperand(1));
  1627. // If we don't know anything about the high bits, exit.
  1628. if (((Known.Zero|Known.One) & HighBitMask) == 0)
  1629. return false;
  1630. // Get the incoming operand to be shifted.
  1631. SDValue InL, InH;
  1632. GetExpandedInteger(N->getOperand(0), InL, InH);
  1633. // If we know that any of the high bits of the shift amount are one, then we
  1634. // can do this as a couple of simple shifts.
  1635. if (Known.One.intersects(HighBitMask)) {
  1636. // Mask out the high bit, which we know is set.
  1637. Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
  1638. DAG.getConstant(~HighBitMask, dl, ShTy));
  1639. switch (N->getOpcode()) {
  1640. default: llvm_unreachable("Unknown shift");
  1641. case ISD::SHL:
  1642. Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
  1643. Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
  1644. return true;
  1645. case ISD::SRL:
  1646. Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
  1647. Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
  1648. return true;
  1649. case ISD::SRA:
  1650. Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
  1651. DAG.getConstant(NVTBits - 1, dl, ShTy));
  1652. Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
  1653. return true;
  1654. }
  1655. }
  1656. // If we know that all of the high bits of the shift amount are zero, then we
  1657. // can do this as a couple of simple shifts.
  1658. if (HighBitMask.isSubsetOf(Known.Zero)) {
  1659. // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
  1660. // shift if x is zero. We can use XOR here because x is known to be smaller
  1661. // than 32.
  1662. SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
  1663. DAG.getConstant(NVTBits - 1, dl, ShTy));
  1664. unsigned Op1, Op2;
  1665. switch (N->getOpcode()) {
  1666. default: llvm_unreachable("Unknown shift");
  1667. case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
  1668. case ISD::SRL:
  1669. case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
  1670. }
  1671. // When shifting right the arithmetic for Lo and Hi is swapped.
  1672. if (N->getOpcode() != ISD::SHL)
  1673. std::swap(InL, InH);
  1674. // Use a little trick to get the bits that move from Lo to Hi. First
  1675. // shift by one bit.
  1676. SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
  1677. // Then compute the remaining shift with amount-1.
  1678. SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
  1679. Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
  1680. Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
  1681. if (N->getOpcode() != ISD::SHL)
  1682. std::swap(Hi, Lo);
  1683. return true;
  1684. }
  1685. return false;
  1686. }
  1687. /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
  1688. /// of any size.
  1689. bool DAGTypeLegalizer::
  1690. ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
  1691. SDValue Amt = N->getOperand(1);
  1692. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1693. EVT ShTy = Amt.getValueType();
  1694. unsigned NVTBits = NVT.getSizeInBits();
  1695. assert(isPowerOf2_32(NVTBits) &&
  1696. "Expanded integer type size not a power of two!");
  1697. SDLoc dl(N);
  1698. // Get the incoming operand to be shifted.
  1699. SDValue InL, InH;
  1700. GetExpandedInteger(N->getOperand(0), InL, InH);
  1701. SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
  1702. SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
  1703. SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
  1704. SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
  1705. Amt, NVBitsNode, ISD::SETULT);
  1706. SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
  1707. Amt, DAG.getConstant(0, dl, ShTy),
  1708. ISD::SETEQ);
  1709. SDValue LoS, HiS, LoL, HiL;
  1710. switch (N->getOpcode()) {
  1711. default: llvm_unreachable("Unknown shift");
  1712. case ISD::SHL:
  1713. // Short: ShAmt < NVTBits
  1714. LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
  1715. HiS = DAG.getNode(ISD::OR, dl, NVT,
  1716. DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
  1717. DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
  1718. // Long: ShAmt >= NVTBits
  1719. LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
  1720. HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
  1721. Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
  1722. Hi = DAG.getSelect(dl, NVT, isZero, InH,
  1723. DAG.getSelect(dl, NVT, isShort, HiS, HiL));
  1724. return true;
  1725. case ISD::SRL:
  1726. // Short: ShAmt < NVTBits
  1727. HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
  1728. LoS = DAG.getNode(ISD::OR, dl, NVT,
  1729. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
  1730. // FIXME: If Amt is zero, the following shift generates an undefined result
  1731. // on some architectures.
  1732. DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
  1733. // Long: ShAmt >= NVTBits
  1734. HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
  1735. LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
  1736. Lo = DAG.getSelect(dl, NVT, isZero, InL,
  1737. DAG.getSelect(dl, NVT, isShort, LoS, LoL));
  1738. Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
  1739. return true;
  1740. case ISD::SRA:
  1741. // Short: ShAmt < NVTBits
  1742. HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
  1743. LoS = DAG.getNode(ISD::OR, dl, NVT,
  1744. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
  1745. DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
  1746. // Long: ShAmt >= NVTBits
  1747. HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
  1748. DAG.getConstant(NVTBits - 1, dl, ShTy));
  1749. LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
  1750. Lo = DAG.getSelect(dl, NVT, isZero, InL,
  1751. DAG.getSelect(dl, NVT, isShort, LoS, LoL));
  1752. Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
  1753. return true;
  1754. }
  1755. }
  1756. static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
  1757. switch (Op) {
  1758. default: llvm_unreachable("invalid min/max opcode");
  1759. case ISD::SMAX:
  1760. return std::make_pair(ISD::SETGT, ISD::UMAX);
  1761. case ISD::UMAX:
  1762. return std::make_pair(ISD::SETUGT, ISD::UMAX);
  1763. case ISD::SMIN:
  1764. return std::make_pair(ISD::SETLT, ISD::UMIN);
  1765. case ISD::UMIN:
  1766. return std::make_pair(ISD::SETULT, ISD::UMIN);
  1767. }
  1768. }
  1769. void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
  1770. SDValue &Lo, SDValue &Hi) {
  1771. SDLoc DL(N);
  1772. ISD::NodeType LoOpc;
  1773. ISD::CondCode CondC;
  1774. std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
  1775. // Expand the subcomponents.
  1776. SDValue LHSL, LHSH, RHSL, RHSH;
  1777. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1778. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1779. // Value types
  1780. EVT NVT = LHSL.getValueType();
  1781. EVT CCT = getSetCCResultType(NVT);
  1782. // Hi part is always the same op
  1783. Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
  1784. // We need to know whether to select Lo part that corresponds to 'winning'
  1785. // Hi part or if Hi parts are equal.
  1786. SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
  1787. SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
  1788. // Lo part corresponding to the 'winning' Hi part
  1789. SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
  1790. // Recursed Lo part if Hi parts are equal, this uses unsigned version
  1791. SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
  1792. Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
  1793. }
  1794. void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
  1795. SDValue &Lo, SDValue &Hi) {
  1796. SDLoc dl(N);
  1797. // Expand the subcomponents.
  1798. SDValue LHSL, LHSH, RHSL, RHSH;
  1799. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1800. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1801. EVT NVT = LHSL.getValueType();
  1802. SDValue LoOps[2] = { LHSL, RHSL };
  1803. SDValue HiOps[3] = { LHSH, RHSH };
  1804. bool HasOpCarry = TLI.isOperationLegalOrCustom(
  1805. N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
  1806. TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
  1807. if (HasOpCarry) {
  1808. SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
  1809. if (N->getOpcode() == ISD::ADD) {
  1810. Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
  1811. HiOps[2] = Lo.getValue(1);
  1812. Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, HiOps);
  1813. } else {
  1814. Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
  1815. HiOps[2] = Lo.getValue(1);
  1816. Hi = DAG.getNode(ISD::SUBCARRY, dl, VTList, HiOps);
  1817. }
  1818. return;
  1819. }
  1820. // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
  1821. // them. TODO: Teach operation legalization how to expand unsupported
  1822. // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
  1823. // a carry of type MVT::Glue, but there doesn't seem to be any way to
  1824. // generate a value of this type in the expanded code sequence.
  1825. bool hasCarry =
  1826. TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
  1827. ISD::ADDC : ISD::SUBC,
  1828. TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
  1829. if (hasCarry) {
  1830. SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
  1831. if (N->getOpcode() == ISD::ADD) {
  1832. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
  1833. HiOps[2] = Lo.getValue(1);
  1834. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
  1835. } else {
  1836. Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
  1837. HiOps[2] = Lo.getValue(1);
  1838. Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
  1839. }
  1840. return;
  1841. }
  1842. bool hasOVF =
  1843. TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
  1844. ISD::UADDO : ISD::USUBO,
  1845. TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
  1846. TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
  1847. if (hasOVF) {
  1848. EVT OvfVT = getSetCCResultType(NVT);
  1849. SDVTList VTList = DAG.getVTList(NVT, OvfVT);
  1850. int RevOpc;
  1851. if (N->getOpcode() == ISD::ADD) {
  1852. RevOpc = ISD::SUB;
  1853. Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
  1854. Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
  1855. } else {
  1856. RevOpc = ISD::ADD;
  1857. Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
  1858. Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
  1859. }
  1860. SDValue OVF = Lo.getValue(1);
  1861. switch (BoolType) {
  1862. case TargetLoweringBase::UndefinedBooleanContent:
  1863. OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
  1864. LLVM_FALLTHROUGH;
  1865. case TargetLoweringBase::ZeroOrOneBooleanContent:
  1866. OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
  1867. Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
  1868. break;
  1869. case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
  1870. OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
  1871. Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
  1872. }
  1873. return;
  1874. }
  1875. if (N->getOpcode() == ISD::ADD) {
  1876. Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
  1877. Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
  1878. SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
  1879. ISD::SETULT);
  1880. if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent) {
  1881. SDValue Carry = DAG.getZExtOrTrunc(Cmp1, dl, NVT);
  1882. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
  1883. return;
  1884. }
  1885. SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
  1886. DAG.getConstant(1, dl, NVT),
  1887. DAG.getConstant(0, dl, NVT));
  1888. SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
  1889. ISD::SETULT);
  1890. SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
  1891. DAG.getConstant(1, dl, NVT), Carry1);
  1892. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
  1893. } else {
  1894. Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
  1895. Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
  1896. SDValue Cmp =
  1897. DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
  1898. LoOps[0], LoOps[1], ISD::SETULT);
  1899. SDValue Borrow;
  1900. if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent)
  1901. Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
  1902. else
  1903. Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
  1904. DAG.getConstant(0, dl, NVT));
  1905. Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
  1906. }
  1907. }
  1908. void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
  1909. SDValue &Lo, SDValue &Hi) {
  1910. // Expand the subcomponents.
  1911. SDValue LHSL, LHSH, RHSL, RHSH;
  1912. SDLoc dl(N);
  1913. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1914. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1915. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
  1916. SDValue LoOps[2] = { LHSL, RHSL };
  1917. SDValue HiOps[3] = { LHSH, RHSH };
  1918. if (N->getOpcode() == ISD::ADDC) {
  1919. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
  1920. HiOps[2] = Lo.getValue(1);
  1921. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
  1922. } else {
  1923. Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
  1924. HiOps[2] = Lo.getValue(1);
  1925. Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
  1926. }
  1927. // Legalized the flag result - switch anything that used the old flag to
  1928. // use the new one.
  1929. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  1930. }
  1931. void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
  1932. SDValue &Lo, SDValue &Hi) {
  1933. // Expand the subcomponents.
  1934. SDValue LHSL, LHSH, RHSL, RHSH;
  1935. SDLoc dl(N);
  1936. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1937. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1938. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
  1939. SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
  1940. SDValue HiOps[3] = { LHSH, RHSH };
  1941. Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
  1942. HiOps[2] = Lo.getValue(1);
  1943. Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
  1944. // Legalized the flag result - switch anything that used the old flag to
  1945. // use the new one.
  1946. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  1947. }
  1948. void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
  1949. SDValue &Lo, SDValue &Hi) {
  1950. SDValue LHS = N->getOperand(0);
  1951. SDValue RHS = N->getOperand(1);
  1952. SDLoc dl(N);
  1953. SDValue Ovf;
  1954. bool HasOpCarry = TLI.isOperationLegalOrCustom(
  1955. N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
  1956. TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
  1957. if (HasOpCarry) {
  1958. // Expand the subcomponents.
  1959. SDValue LHSL, LHSH, RHSL, RHSH;
  1960. GetExpandedInteger(LHS, LHSL, LHSH);
  1961. GetExpandedInteger(RHS, RHSL, RHSH);
  1962. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
  1963. SDValue LoOps[2] = { LHSL, RHSL };
  1964. SDValue HiOps[3] = { LHSH, RHSH };
  1965. unsigned Opc = N->getOpcode() == ISD::UADDO ? ISD::ADDCARRY : ISD::SUBCARRY;
  1966. Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
  1967. HiOps[2] = Lo.getValue(1);
  1968. Hi = DAG.getNode(Opc, dl, VTList, HiOps);
  1969. Ovf = Hi.getValue(1);
  1970. } else {
  1971. // Expand the result by simply replacing it with the equivalent
  1972. // non-overflow-checking operation.
  1973. auto Opc = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
  1974. SDValue Sum = DAG.getNode(Opc, dl, LHS.getValueType(), LHS, RHS);
  1975. SplitInteger(Sum, Lo, Hi);
  1976. // Calculate the overflow: addition overflows iff a + b < a, and subtraction
  1977. // overflows iff a - b > a.
  1978. auto Cond = N->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
  1979. Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
  1980. }
  1981. // Legalized the flag result - switch anything that used the old flag to
  1982. // use the new one.
  1983. ReplaceValueWith(SDValue(N, 1), Ovf);
  1984. }
  1985. void DAGTypeLegalizer::ExpandIntRes_ADDSUBCARRY(SDNode *N,
  1986. SDValue &Lo, SDValue &Hi) {
  1987. // Expand the subcomponents.
  1988. SDValue LHSL, LHSH, RHSL, RHSH;
  1989. SDLoc dl(N);
  1990. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1991. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1992. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
  1993. SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
  1994. SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
  1995. Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
  1996. HiOps[2] = Lo.getValue(1);
  1997. Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
  1998. // Legalized the flag result - switch anything that used the old flag to
  1999. // use the new one.
  2000. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  2001. }
  2002. void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
  2003. SDValue &Lo, SDValue &Hi) {
  2004. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2005. SDLoc dl(N);
  2006. SDValue Op = N->getOperand(0);
  2007. if (Op.getValueType().bitsLE(NVT)) {
  2008. // The low part is any extension of the input (which degenerates to a copy).
  2009. Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
  2010. Hi = DAG.getUNDEF(NVT); // The high part is undefined.
  2011. } else {
  2012. // For example, extension of an i48 to an i64. The operand type necessarily
  2013. // promotes to the result type, so will end up being expanded too.
  2014. assert(getTypeAction(Op.getValueType()) ==
  2015. TargetLowering::TypePromoteInteger &&
  2016. "Only know how to promote this result!");
  2017. SDValue Res = GetPromotedInteger(Op);
  2018. assert(Res.getValueType() == N->getValueType(0) &&
  2019. "Operand over promoted?");
  2020. // Split the promoted operand. This will simplify when it is expanded.
  2021. SplitInteger(Res, Lo, Hi);
  2022. }
  2023. }
  2024. void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
  2025. SDValue &Lo, SDValue &Hi) {
  2026. SDLoc dl(N);
  2027. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2028. EVT NVT = Lo.getValueType();
  2029. EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  2030. unsigned NVTBits = NVT.getSizeInBits();
  2031. unsigned EVTBits = EVT.getSizeInBits();
  2032. if (NVTBits < EVTBits) {
  2033. Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
  2034. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  2035. EVTBits - NVTBits)));
  2036. } else {
  2037. Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
  2038. // The high part replicates the sign bit of Lo, make it explicit.
  2039. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  2040. DAG.getConstant(NVTBits - 1, dl,
  2041. TLI.getPointerTy(DAG.getDataLayout())));
  2042. }
  2043. }
  2044. void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
  2045. SDValue &Lo, SDValue &Hi) {
  2046. SDLoc dl(N);
  2047. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2048. EVT NVT = Lo.getValueType();
  2049. EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  2050. unsigned NVTBits = NVT.getSizeInBits();
  2051. unsigned EVTBits = EVT.getSizeInBits();
  2052. if (NVTBits < EVTBits) {
  2053. Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
  2054. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  2055. EVTBits - NVTBits)));
  2056. } else {
  2057. Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
  2058. // The high part must be zero, make it explicit.
  2059. Hi = DAG.getConstant(0, dl, NVT);
  2060. }
  2061. }
  2062. void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
  2063. SDValue &Lo, SDValue &Hi) {
  2064. SDLoc dl(N);
  2065. GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
  2066. Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
  2067. Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
  2068. }
  2069. void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
  2070. SDValue &Lo, SDValue &Hi) {
  2071. SDLoc dl(N);
  2072. GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
  2073. Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
  2074. Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
  2075. }
  2076. void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
  2077. SDValue &Lo, SDValue &Hi) {
  2078. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2079. unsigned NBitWidth = NVT.getSizeInBits();
  2080. auto Constant = cast<ConstantSDNode>(N);
  2081. const APInt &Cst = Constant->getAPIntValue();
  2082. bool IsTarget = Constant->isTargetOpcode();
  2083. bool IsOpaque = Constant->isOpaque();
  2084. SDLoc dl(N);
  2085. Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
  2086. Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
  2087. IsOpaque);
  2088. }
  2089. void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
  2090. SDLoc dl(N);
  2091. // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
  2092. EVT VT = N->getValueType(0);
  2093. SDValue N0 = N->getOperand(0);
  2094. SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
  2095. DAG.getConstant(0, dl, VT), N0);
  2096. SDValue NegLo, NegHi;
  2097. SplitInteger(Neg, NegLo, NegHi);
  2098. GetExpandedInteger(N0, Lo, Hi);
  2099. EVT NVT = Lo.getValueType();
  2100. SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT),
  2101. DAG.getConstant(0, dl, NVT), Hi, ISD::SETGT);
  2102. Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
  2103. Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
  2104. }
  2105. void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
  2106. SDValue &Lo, SDValue &Hi) {
  2107. SDLoc dl(N);
  2108. // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
  2109. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2110. EVT NVT = Lo.getValueType();
  2111. SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
  2112. DAG.getConstant(0, dl, NVT), ISD::SETNE);
  2113. SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
  2114. SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
  2115. Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
  2116. DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
  2117. DAG.getConstant(NVT.getSizeInBits(), dl,
  2118. NVT)));
  2119. Hi = DAG.getConstant(0, dl, NVT);
  2120. }
  2121. void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
  2122. SDValue &Lo, SDValue &Hi) {
  2123. SDLoc dl(N);
  2124. // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
  2125. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2126. EVT NVT = Lo.getValueType();
  2127. Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
  2128. DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
  2129. Hi = DAG.getConstant(0, dl, NVT);
  2130. }
  2131. void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
  2132. SDValue &Lo, SDValue &Hi) {
  2133. SDLoc dl(N);
  2134. // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
  2135. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2136. EVT NVT = Lo.getValueType();
  2137. SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
  2138. DAG.getConstant(0, dl, NVT), ISD::SETNE);
  2139. SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
  2140. SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
  2141. Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
  2142. DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
  2143. DAG.getConstant(NVT.getSizeInBits(), dl,
  2144. NVT)));
  2145. Hi = DAG.getConstant(0, dl, NVT);
  2146. }
  2147. void DAGTypeLegalizer::ExpandIntRes_FLT_ROUNDS(SDNode *N, SDValue &Lo,
  2148. SDValue &Hi) {
  2149. SDLoc dl(N);
  2150. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2151. unsigned NBitWidth = NVT.getSizeInBits();
  2152. EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
  2153. Lo = DAG.getNode(ISD::FLT_ROUNDS_, dl, NVT);
  2154. // The high part is the sign of Lo, as -1 is a valid value for FLT_ROUNDS
  2155. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  2156. DAG.getConstant(NBitWidth - 1, dl, ShiftAmtTy));
  2157. }
  2158. void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
  2159. SDValue &Hi) {
  2160. SDLoc dl(N);
  2161. EVT VT = N->getValueType(0);
  2162. SDValue Op = N->getOperand(0);
  2163. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
  2164. Op = GetPromotedFloat(Op);
  2165. RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
  2166. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
  2167. TargetLowering::MakeLibCallOptions CallOptions;
  2168. CallOptions.setSExt(true);
  2169. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, dl).first,
  2170. Lo, Hi);
  2171. }
  2172. void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
  2173. SDValue &Hi) {
  2174. SDLoc dl(N);
  2175. EVT VT = N->getValueType(0);
  2176. SDValue Op = N->getOperand(0);
  2177. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
  2178. Op = GetPromotedFloat(Op);
  2179. RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
  2180. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
  2181. TargetLowering::MakeLibCallOptions CallOptions;
  2182. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, dl).first,
  2183. Lo, Hi);
  2184. }
  2185. void DAGTypeLegalizer::ExpandIntRes_LLROUND(SDNode *N, SDValue &Lo,
  2186. SDValue &Hi) {
  2187. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2188. EVT VT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
  2189. if (VT == MVT::f32)
  2190. LC = RTLIB::LLROUND_F32;
  2191. else if (VT == MVT::f64)
  2192. LC = RTLIB::LLROUND_F64;
  2193. else if (VT == MVT::f80)
  2194. LC = RTLIB::LLROUND_F80;
  2195. else if (VT == MVT::f128)
  2196. LC = RTLIB::LLROUND_F128;
  2197. else if (VT == MVT::ppcf128)
  2198. LC = RTLIB::LLROUND_PPCF128;
  2199. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
  2200. SDValue Op = N->getOperand(0);
  2201. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
  2202. Op = GetPromotedFloat(Op);
  2203. SDLoc dl(N);
  2204. EVT RetVT = N->getValueType(0);
  2205. TargetLowering::MakeLibCallOptions CallOptions;
  2206. CallOptions.setSExt(true);
  2207. SplitInteger(TLI.makeLibCall(DAG, LC, RetVT, Op, CallOptions, dl).first,
  2208. Lo, Hi);
  2209. }
  2210. void DAGTypeLegalizer::ExpandIntRes_LLRINT(SDNode *N, SDValue &Lo,
  2211. SDValue &Hi) {
  2212. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2213. EVT VT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
  2214. if (VT == MVT::f32)
  2215. LC = RTLIB::LLRINT_F32;
  2216. else if (VT == MVT::f64)
  2217. LC = RTLIB::LLRINT_F64;
  2218. else if (VT == MVT::f80)
  2219. LC = RTLIB::LLRINT_F80;
  2220. else if (VT == MVT::f128)
  2221. LC = RTLIB::LLRINT_F128;
  2222. else if (VT == MVT::ppcf128)
  2223. LC = RTLIB::LLRINT_PPCF128;
  2224. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
  2225. SDValue Op = N->getOperand(0);
  2226. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
  2227. Op = GetPromotedFloat(Op);
  2228. SDLoc dl(N);
  2229. EVT RetVT = N->getValueType(0);
  2230. TargetLowering::MakeLibCallOptions CallOptions;
  2231. CallOptions.setSExt(true);
  2232. SplitInteger(TLI.makeLibCall(DAG, LC, RetVT, Op, CallOptions, dl).first,
  2233. Lo, Hi);
  2234. }
  2235. void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
  2236. SDValue &Lo, SDValue &Hi) {
  2237. if (ISD::isNormalLoad(N)) {
  2238. ExpandRes_NormalLoad(N, Lo, Hi);
  2239. return;
  2240. }
  2241. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  2242. EVT VT = N->getValueType(0);
  2243. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  2244. SDValue Ch = N->getChain();
  2245. SDValue Ptr = N->getBasePtr();
  2246. ISD::LoadExtType ExtType = N->getExtensionType();
  2247. unsigned Alignment = N->getAlignment();
  2248. MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
  2249. AAMDNodes AAInfo = N->getAAInfo();
  2250. SDLoc dl(N);
  2251. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  2252. if (N->getMemoryVT().bitsLE(NVT)) {
  2253. EVT MemVT = N->getMemoryVT();
  2254. Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
  2255. Alignment, MMOFlags, AAInfo);
  2256. // Remember the chain.
  2257. Ch = Lo.getValue(1);
  2258. if (ExtType == ISD::SEXTLOAD) {
  2259. // The high part is obtained by SRA'ing all but one of the bits of the
  2260. // lo part.
  2261. unsigned LoSize = Lo.getValueSizeInBits();
  2262. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  2263. DAG.getConstant(LoSize - 1, dl,
  2264. TLI.getPointerTy(DAG.getDataLayout())));
  2265. } else if (ExtType == ISD::ZEXTLOAD) {
  2266. // The high part is just a zero.
  2267. Hi = DAG.getConstant(0, dl, NVT);
  2268. } else {
  2269. assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
  2270. // The high part is undefined.
  2271. Hi = DAG.getUNDEF(NVT);
  2272. }
  2273. } else if (DAG.getDataLayout().isLittleEndian()) {
  2274. // Little-endian - low bits are at low addresses.
  2275. Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
  2276. AAInfo);
  2277. unsigned ExcessBits =
  2278. N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
  2279. EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
  2280. // Increment the pointer to the other half.
  2281. unsigned IncrementSize = NVT.getSizeInBits()/8;
  2282. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  2283. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  2284. Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
  2285. N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
  2286. MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
  2287. // Build a factor node to remember that this load is independent of the
  2288. // other one.
  2289. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  2290. Hi.getValue(1));
  2291. } else {
  2292. // Big-endian - high bits are at low addresses. Favor aligned loads at
  2293. // the cost of some bit-fiddling.
  2294. EVT MemVT = N->getMemoryVT();
  2295. unsigned EBytes = MemVT.getStoreSize();
  2296. unsigned IncrementSize = NVT.getSizeInBits()/8;
  2297. unsigned ExcessBits = (EBytes - IncrementSize)*8;
  2298. // Load both the high bits and maybe some of the low bits.
  2299. Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
  2300. EVT::getIntegerVT(*DAG.getContext(),
  2301. MemVT.getSizeInBits() - ExcessBits),
  2302. Alignment, MMOFlags, AAInfo);
  2303. // Increment the pointer to the other half.
  2304. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  2305. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  2306. // Load the rest of the low bits.
  2307. Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
  2308. N->getPointerInfo().getWithOffset(IncrementSize),
  2309. EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
  2310. MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
  2311. // Build a factor node to remember that this load is independent of the
  2312. // other one.
  2313. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  2314. Hi.getValue(1));
  2315. if (ExcessBits < NVT.getSizeInBits()) {
  2316. // Transfer low bits from the bottom of Hi to the top of Lo.
  2317. Lo = DAG.getNode(
  2318. ISD::OR, dl, NVT, Lo,
  2319. DAG.getNode(ISD::SHL, dl, NVT, Hi,
  2320. DAG.getConstant(ExcessBits, dl,
  2321. TLI.getPointerTy(DAG.getDataLayout()))));
  2322. // Move high bits to the right position in Hi.
  2323. Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
  2324. Hi,
  2325. DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
  2326. TLI.getPointerTy(DAG.getDataLayout())));
  2327. }
  2328. }
  2329. // Legalize the chain result - switch anything that used the old chain to
  2330. // use the new one.
  2331. ReplaceValueWith(SDValue(N, 1), Ch);
  2332. }
  2333. void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
  2334. SDValue &Lo, SDValue &Hi) {
  2335. SDLoc dl(N);
  2336. SDValue LL, LH, RL, RH;
  2337. GetExpandedInteger(N->getOperand(0), LL, LH);
  2338. GetExpandedInteger(N->getOperand(1), RL, RH);
  2339. Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
  2340. Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
  2341. }
  2342. void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
  2343. SDValue &Lo, SDValue &Hi) {
  2344. EVT VT = N->getValueType(0);
  2345. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  2346. SDLoc dl(N);
  2347. SDValue LL, LH, RL, RH;
  2348. GetExpandedInteger(N->getOperand(0), LL, LH);
  2349. GetExpandedInteger(N->getOperand(1), RL, RH);
  2350. if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
  2351. TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
  2352. LL, LH, RL, RH))
  2353. return;
  2354. // If nothing else, we can make a libcall.
  2355. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2356. if (VT == MVT::i16)
  2357. LC = RTLIB::MUL_I16;
  2358. else if (VT == MVT::i32)
  2359. LC = RTLIB::MUL_I32;
  2360. else if (VT == MVT::i64)
  2361. LC = RTLIB::MUL_I64;
  2362. else if (VT == MVT::i128)
  2363. LC = RTLIB::MUL_I128;
  2364. if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
  2365. // We'll expand the multiplication by brute force because we have no other
  2366. // options. This is a trivially-generalized version of the code from
  2367. // Hacker's Delight (itself derived from Knuth's Algorithm M from section
  2368. // 4.3.1).
  2369. unsigned Bits = NVT.getSizeInBits();
  2370. unsigned HalfBits = Bits >> 1;
  2371. SDValue Mask = DAG.getConstant(APInt::getLowBitsSet(Bits, HalfBits), dl,
  2372. NVT);
  2373. SDValue LLL = DAG.getNode(ISD::AND, dl, NVT, LL, Mask);
  2374. SDValue RLL = DAG.getNode(ISD::AND, dl, NVT, RL, Mask);
  2375. SDValue T = DAG.getNode(ISD::MUL, dl, NVT, LLL, RLL);
  2376. SDValue TL = DAG.getNode(ISD::AND, dl, NVT, T, Mask);
  2377. EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
  2378. if (APInt::getMaxValue(ShiftAmtTy.getSizeInBits()).ult(HalfBits)) {
  2379. // The type from TLI is too small to fit the shift amount we want.
  2380. // Override it with i32. The shift will have to be legalized.
  2381. ShiftAmtTy = MVT::i32;
  2382. }
  2383. SDValue Shift = DAG.getConstant(HalfBits, dl, ShiftAmtTy);
  2384. SDValue TH = DAG.getNode(ISD::SRL, dl, NVT, T, Shift);
  2385. SDValue LLH = DAG.getNode(ISD::SRL, dl, NVT, LL, Shift);
  2386. SDValue RLH = DAG.getNode(ISD::SRL, dl, NVT, RL, Shift);
  2387. SDValue U = DAG.getNode(ISD::ADD, dl, NVT,
  2388. DAG.getNode(ISD::MUL, dl, NVT, LLH, RLL), TH);
  2389. SDValue UL = DAG.getNode(ISD::AND, dl, NVT, U, Mask);
  2390. SDValue UH = DAG.getNode(ISD::SRL, dl, NVT, U, Shift);
  2391. SDValue V = DAG.getNode(ISD::ADD, dl, NVT,
  2392. DAG.getNode(ISD::MUL, dl, NVT, LLL, RLH), UL);
  2393. SDValue VH = DAG.getNode(ISD::SRL, dl, NVT, V, Shift);
  2394. SDValue W = DAG.getNode(ISD::ADD, dl, NVT,
  2395. DAG.getNode(ISD::MUL, dl, NVT, LLH, RLH),
  2396. DAG.getNode(ISD::ADD, dl, NVT, UH, VH));
  2397. Lo = DAG.getNode(ISD::ADD, dl, NVT, TL,
  2398. DAG.getNode(ISD::SHL, dl, NVT, V, Shift));
  2399. Hi = DAG.getNode(ISD::ADD, dl, NVT, W,
  2400. DAG.getNode(ISD::ADD, dl, NVT,
  2401. DAG.getNode(ISD::MUL, dl, NVT, RH, LL),
  2402. DAG.getNode(ISD::MUL, dl, NVT, RL, LH)));
  2403. return;
  2404. }
  2405. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2406. TargetLowering::MakeLibCallOptions CallOptions;
  2407. CallOptions.setSExt(true);
  2408. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first,
  2409. Lo, Hi);
  2410. }
  2411. void DAGTypeLegalizer::ExpandIntRes_READCYCLECOUNTER(SDNode *N, SDValue &Lo,
  2412. SDValue &Hi) {
  2413. SDLoc DL(N);
  2414. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2415. SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
  2416. SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
  2417. Lo = R.getValue(0);
  2418. Hi = R.getValue(1);
  2419. ReplaceValueWith(SDValue(N, 1), R.getValue(2));
  2420. }
  2421. void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
  2422. SDValue &Hi) {
  2423. SDValue Result = TLI.expandAddSubSat(N, DAG);
  2424. SplitInteger(Result, Lo, Hi);
  2425. }
  2426. /// This performs an expansion of the integer result for a fixed point
  2427. /// multiplication. The default expansion performs rounding down towards
  2428. /// negative infinity, though targets that do care about rounding should specify
  2429. /// a target hook for rounding and provide their own expansion or lowering of
  2430. /// fixed point multiplication to be consistent with rounding.
  2431. void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
  2432. SDValue &Hi) {
  2433. SDLoc dl(N);
  2434. EVT VT = N->getValueType(0);
  2435. unsigned VTSize = VT.getScalarSizeInBits();
  2436. SDValue LHS = N->getOperand(0);
  2437. SDValue RHS = N->getOperand(1);
  2438. uint64_t Scale = N->getConstantOperandVal(2);
  2439. bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
  2440. N->getOpcode() == ISD::UMULFIXSAT);
  2441. bool Signed = (N->getOpcode() == ISD::SMULFIX ||
  2442. N->getOpcode() == ISD::SMULFIXSAT);
  2443. // Handle special case when scale is equal to zero.
  2444. if (!Scale) {
  2445. SDValue Result;
  2446. if (!Saturating) {
  2447. Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
  2448. } else {
  2449. EVT BoolVT = getSetCCResultType(VT);
  2450. unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
  2451. Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
  2452. SDValue Product = Result.getValue(0);
  2453. SDValue Overflow = Result.getValue(1);
  2454. if (Signed) {
  2455. APInt MinVal = APInt::getSignedMinValue(VTSize);
  2456. APInt MaxVal = APInt::getSignedMaxValue(VTSize);
  2457. SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
  2458. SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
  2459. SDValue Zero = DAG.getConstant(0, dl, VT);
  2460. SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Product, Zero, ISD::SETLT);
  2461. Result = DAG.getSelect(dl, VT, ProdNeg, SatMax, SatMin);
  2462. Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
  2463. } else {
  2464. // For unsigned multiplication, we only need to check the max since we
  2465. // can't really overflow towards zero.
  2466. APInt MaxVal = APInt::getMaxValue(VTSize);
  2467. SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
  2468. Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
  2469. }
  2470. }
  2471. SplitInteger(Result, Lo, Hi);
  2472. return;
  2473. }
  2474. // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
  2475. // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
  2476. assert(Scale <= VTSize && "Scale can't be larger than the value type size.");
  2477. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  2478. SDValue LL, LH, RL, RH;
  2479. GetExpandedInteger(LHS, LL, LH);
  2480. GetExpandedInteger(RHS, RL, RH);
  2481. SmallVector<SDValue, 4> Result;
  2482. unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
  2483. if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
  2484. TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
  2485. LL, LH, RL, RH)) {
  2486. report_fatal_error("Unable to expand MUL_FIX using MUL_LOHI.");
  2487. return;
  2488. }
  2489. unsigned NVTSize = NVT.getScalarSizeInBits();
  2490. assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
  2491. "the size of the current value type");
  2492. EVT ShiftTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
  2493. // After getting the multiplication result in 4 parts, we need to perform a
  2494. // shift right by the amount of the scale to get the result in that scale.
  2495. //
  2496. // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
  2497. // 128 bits that are cut into 4 32-bit parts:
  2498. //
  2499. // HH HL LH LL
  2500. // |---32---|---32---|---32---|---32---|
  2501. // 128 96 64 32 0
  2502. //
  2503. // |------VTSize-----|
  2504. //
  2505. // |NVTSize-|
  2506. //
  2507. // The resulting Lo and Hi would normally be in LL and LH after the shift. But
  2508. // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
  2509. // amount and get Lo and Hi using two funnel shifts. Or for the special case
  2510. // when Scale is a multiple of NVTSize we can just pick the result without
  2511. // shifting.
  2512. uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
  2513. if (Scale % NVTSize) {
  2514. SDValue ShiftAmount = DAG.getConstant(Scale % NVTSize, dl, ShiftTy);
  2515. Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
  2516. ShiftAmount);
  2517. Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
  2518. ShiftAmount);
  2519. } else {
  2520. Lo = Result[Part0];
  2521. Hi = Result[Part0 + 1];
  2522. }
  2523. // Unless saturation is requested we are done. The result is in <Hi,Lo>.
  2524. if (!Saturating)
  2525. return;
  2526. // Can not overflow when there is no integer part.
  2527. if (Scale == VTSize)
  2528. return;
  2529. // To handle saturation we must check for overflow in the multiplication.
  2530. //
  2531. // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
  2532. // aren't all zeroes.
  2533. //
  2534. // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
  2535. // aren't all ones or all zeroes.
  2536. //
  2537. // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
  2538. // highest bit of HH determines saturation direction in the event of signed
  2539. // saturation.
  2540. SDValue ResultHL = Result[2];
  2541. SDValue ResultHH = Result[3];
  2542. SDValue SatMax, SatMin;
  2543. SDValue NVTZero = DAG.getConstant(0, dl, NVT);
  2544. SDValue NVTNeg1 = DAG.getConstant(-1, dl, NVT);
  2545. EVT BoolNVT = getSetCCResultType(NVT);
  2546. if (!Signed) {
  2547. if (Scale < NVTSize) {
  2548. // Overflow happened if ((HH | (HL >> Scale)) != 0).
  2549. SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
  2550. DAG.getConstant(Scale, dl, ShiftTy));
  2551. SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
  2552. SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
  2553. } else if (Scale == NVTSize) {
  2554. // Overflow happened if (HH != 0).
  2555. SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
  2556. } else if (Scale < VTSize) {
  2557. // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
  2558. SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
  2559. DAG.getConstant(Scale - NVTSize, dl,
  2560. ShiftTy));
  2561. SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
  2562. } else
  2563. llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
  2564. "(and saturation can't happen with Scale==VTSize).");
  2565. Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
  2566. Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
  2567. return;
  2568. }
  2569. if (Scale < NVTSize) {
  2570. // The number of overflow bits we can check are VTSize - Scale + 1 (we
  2571. // include the sign bit). If these top bits are > 0, then we overflowed past
  2572. // the max value. If these top bits are < -1, then we overflowed past the
  2573. // min value. Otherwise, we did not overflow.
  2574. unsigned OverflowBits = VTSize - Scale + 1;
  2575. assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
  2576. "Extent of overflow bits must start within HL");
  2577. SDValue HLHiMask = DAG.getConstant(
  2578. APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
  2579. SDValue HLLoMask = DAG.getConstant(
  2580. APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
  2581. // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
  2582. SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
  2583. SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
  2584. SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
  2585. SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
  2586. DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
  2587. // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
  2588. SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
  2589. SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
  2590. SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
  2591. SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
  2592. DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
  2593. } else if (Scale == NVTSize) {
  2594. // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
  2595. SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
  2596. SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
  2597. SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
  2598. SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
  2599. DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
  2600. // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
  2601. SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
  2602. SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
  2603. SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
  2604. SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
  2605. DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
  2606. } else if (Scale < VTSize) {
  2607. // This is similar to the case when we saturate if Scale < NVTSize, but we
  2608. // only need to check HH.
  2609. unsigned OverflowBits = VTSize - Scale + 1;
  2610. SDValue HHHiMask = DAG.getConstant(
  2611. APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
  2612. SDValue HHLoMask = DAG.getConstant(
  2613. APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
  2614. SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
  2615. SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
  2616. } else
  2617. llvm_unreachable("Illegal scale for signed fixed point mul.");
  2618. // Saturate to signed maximum.
  2619. APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
  2620. APInt MaxLo = APInt::getAllOnesValue(NVTSize);
  2621. Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
  2622. Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
  2623. // Saturate to signed minimum.
  2624. APInt MinHi = APInt::getSignedMinValue(NVTSize);
  2625. Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
  2626. Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
  2627. }
  2628. void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
  2629. SDValue &Lo, SDValue &Hi) {
  2630. SDValue LHS = Node->getOperand(0);
  2631. SDValue RHS = Node->getOperand(1);
  2632. SDLoc dl(Node);
  2633. // Expand the result by simply replacing it with the equivalent
  2634. // non-overflow-checking operation.
  2635. SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
  2636. ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
  2637. LHS, RHS);
  2638. SplitInteger(Sum, Lo, Hi);
  2639. // Compute the overflow.
  2640. //
  2641. // LHSSign -> LHS >= 0
  2642. // RHSSign -> RHS >= 0
  2643. // SumSign -> Sum >= 0
  2644. //
  2645. // Add:
  2646. // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
  2647. // Sub:
  2648. // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
  2649. //
  2650. EVT OType = Node->getValueType(1);
  2651. SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
  2652. SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
  2653. SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
  2654. SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
  2655. Node->getOpcode() == ISD::SADDO ?
  2656. ISD::SETEQ : ISD::SETNE);
  2657. SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
  2658. SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
  2659. SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
  2660. // Use the calculated overflow everywhere.
  2661. ReplaceValueWith(SDValue(Node, 1), Cmp);
  2662. }
  2663. void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
  2664. SDValue &Lo, SDValue &Hi) {
  2665. EVT VT = N->getValueType(0);
  2666. SDLoc dl(N);
  2667. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2668. if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
  2669. SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  2670. SplitInteger(Res.getValue(0), Lo, Hi);
  2671. return;
  2672. }
  2673. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2674. if (VT == MVT::i16)
  2675. LC = RTLIB::SDIV_I16;
  2676. else if (VT == MVT::i32)
  2677. LC = RTLIB::SDIV_I32;
  2678. else if (VT == MVT::i64)
  2679. LC = RTLIB::SDIV_I64;
  2680. else if (VT == MVT::i128)
  2681. LC = RTLIB::SDIV_I128;
  2682. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
  2683. TargetLowering::MakeLibCallOptions CallOptions;
  2684. CallOptions.setSExt(true);
  2685. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
  2686. }
  2687. void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
  2688. SDValue &Lo, SDValue &Hi) {
  2689. EVT VT = N->getValueType(0);
  2690. SDLoc dl(N);
  2691. // If we can emit an efficient shift operation, do so now. Check to see if
  2692. // the RHS is a constant.
  2693. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
  2694. return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
  2695. // If we can determine that the high bit of the shift is zero or one, even if
  2696. // the low bits are variable, emit this shift in an optimized form.
  2697. if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
  2698. return;
  2699. // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
  2700. unsigned PartsOpc;
  2701. if (N->getOpcode() == ISD::SHL) {
  2702. PartsOpc = ISD::SHL_PARTS;
  2703. } else if (N->getOpcode() == ISD::SRL) {
  2704. PartsOpc = ISD::SRL_PARTS;
  2705. } else {
  2706. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  2707. PartsOpc = ISD::SRA_PARTS;
  2708. }
  2709. // Next check to see if the target supports this SHL_PARTS operation or if it
  2710. // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
  2711. // size, but create a libcall instead.
  2712. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  2713. TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
  2714. const bool LegalOrCustom =
  2715. (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
  2716. Action == TargetLowering::Custom;
  2717. if (LegalOrCustom && TLI.shouldExpandShift(DAG, N)) {
  2718. // Expand the subcomponents.
  2719. SDValue LHSL, LHSH;
  2720. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  2721. EVT VT = LHSL.getValueType();
  2722. // If the shift amount operand is coming from a vector legalization it may
  2723. // have an illegal type. Fix that first by casting the operand, otherwise
  2724. // the new SHL_PARTS operation would need further legalization.
  2725. SDValue ShiftOp = N->getOperand(1);
  2726. EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
  2727. assert(ShiftTy.getScalarSizeInBits() >=
  2728. Log2_32_Ceil(VT.getScalarSizeInBits()) &&
  2729. "ShiftAmountTy is too small to cover the range of this type!");
  2730. if (ShiftOp.getValueType() != ShiftTy)
  2731. ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
  2732. SDValue Ops[] = { LHSL, LHSH, ShiftOp };
  2733. Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
  2734. Hi = Lo.getValue(1);
  2735. return;
  2736. }
  2737. // Otherwise, emit a libcall.
  2738. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2739. bool isSigned;
  2740. if (N->getOpcode() == ISD::SHL) {
  2741. isSigned = false; /*sign irrelevant*/
  2742. if (VT == MVT::i16)
  2743. LC = RTLIB::SHL_I16;
  2744. else if (VT == MVT::i32)
  2745. LC = RTLIB::SHL_I32;
  2746. else if (VT == MVT::i64)
  2747. LC = RTLIB::SHL_I64;
  2748. else if (VT == MVT::i128)
  2749. LC = RTLIB::SHL_I128;
  2750. } else if (N->getOpcode() == ISD::SRL) {
  2751. isSigned = false;
  2752. if (VT == MVT::i16)
  2753. LC = RTLIB::SRL_I16;
  2754. else if (VT == MVT::i32)
  2755. LC = RTLIB::SRL_I32;
  2756. else if (VT == MVT::i64)
  2757. LC = RTLIB::SRL_I64;
  2758. else if (VT == MVT::i128)
  2759. LC = RTLIB::SRL_I128;
  2760. } else {
  2761. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  2762. isSigned = true;
  2763. if (VT == MVT::i16)
  2764. LC = RTLIB::SRA_I16;
  2765. else if (VT == MVT::i32)
  2766. LC = RTLIB::SRA_I32;
  2767. else if (VT == MVT::i64)
  2768. LC = RTLIB::SRA_I64;
  2769. else if (VT == MVT::i128)
  2770. LC = RTLIB::SRA_I128;
  2771. }
  2772. if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
  2773. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2774. TargetLowering::MakeLibCallOptions CallOptions;
  2775. CallOptions.setSExt(isSigned);
  2776. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
  2777. return;
  2778. }
  2779. if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
  2780. llvm_unreachable("Unsupported shift!");
  2781. }
  2782. void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
  2783. SDValue &Lo, SDValue &Hi) {
  2784. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2785. SDLoc dl(N);
  2786. SDValue Op = N->getOperand(0);
  2787. if (Op.getValueType().bitsLE(NVT)) {
  2788. // The low part is sign extension of the input (degenerates to a copy).
  2789. Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
  2790. // The high part is obtained by SRA'ing all but one of the bits of low part.
  2791. unsigned LoSize = NVT.getSizeInBits();
  2792. Hi = DAG.getNode(
  2793. ISD::SRA, dl, NVT, Lo,
  2794. DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
  2795. } else {
  2796. // For example, extension of an i48 to an i64. The operand type necessarily
  2797. // promotes to the result type, so will end up being expanded too.
  2798. assert(getTypeAction(Op.getValueType()) ==
  2799. TargetLowering::TypePromoteInteger &&
  2800. "Only know how to promote this result!");
  2801. SDValue Res = GetPromotedInteger(Op);
  2802. assert(Res.getValueType() == N->getValueType(0) &&
  2803. "Operand over promoted?");
  2804. // Split the promoted operand. This will simplify when it is expanded.
  2805. SplitInteger(Res, Lo, Hi);
  2806. unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
  2807. Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
  2808. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  2809. ExcessBits)));
  2810. }
  2811. }
  2812. void DAGTypeLegalizer::
  2813. ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
  2814. SDLoc dl(N);
  2815. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2816. EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  2817. if (EVT.bitsLE(Lo.getValueType())) {
  2818. // sext_inreg the low part if needed.
  2819. Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
  2820. N->getOperand(1));
  2821. // The high part gets the sign extension from the lo-part. This handles
  2822. // things like sextinreg V:i64 from i8.
  2823. Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
  2824. DAG.getConstant(Hi.getValueSizeInBits() - 1, dl,
  2825. TLI.getPointerTy(DAG.getDataLayout())));
  2826. } else {
  2827. // For example, extension of an i48 to an i64. Leave the low part alone,
  2828. // sext_inreg the high part.
  2829. unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
  2830. Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
  2831. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  2832. ExcessBits)));
  2833. }
  2834. }
  2835. void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
  2836. SDValue &Lo, SDValue &Hi) {
  2837. EVT VT = N->getValueType(0);
  2838. SDLoc dl(N);
  2839. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2840. if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
  2841. SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  2842. SplitInteger(Res.getValue(1), Lo, Hi);
  2843. return;
  2844. }
  2845. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2846. if (VT == MVT::i16)
  2847. LC = RTLIB::SREM_I16;
  2848. else if (VT == MVT::i32)
  2849. LC = RTLIB::SREM_I32;
  2850. else if (VT == MVT::i64)
  2851. LC = RTLIB::SREM_I64;
  2852. else if (VT == MVT::i128)
  2853. LC = RTLIB::SREM_I128;
  2854. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
  2855. TargetLowering::MakeLibCallOptions CallOptions;
  2856. CallOptions.setSExt(true);
  2857. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
  2858. }
  2859. void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
  2860. SDValue &Lo, SDValue &Hi) {
  2861. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2862. SDLoc dl(N);
  2863. Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
  2864. Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
  2865. N->getOperand(0),
  2866. DAG.getConstant(NVT.getSizeInBits(), dl,
  2867. TLI.getPointerTy(DAG.getDataLayout())));
  2868. Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
  2869. }
  2870. void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
  2871. SDValue &Lo, SDValue &Hi) {
  2872. EVT VT = N->getValueType(0);
  2873. SDLoc dl(N);
  2874. if (N->getOpcode() == ISD::UMULO) {
  2875. // This section expands the operation into the following sequence of
  2876. // instructions. `iNh` here refers to a type which has half the bit width of
  2877. // the type the original operation operated on.
  2878. //
  2879. // %0 = %LHS.HI != 0 && %RHS.HI != 0
  2880. // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
  2881. // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
  2882. // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
  2883. // %4 = add iN (%1.0 as iN) << Nh, (%2.0 as iN) << Nh
  2884. // %5 = { iN, i1 } @uadd.with.overflow.iN( %4, %3 )
  2885. //
  2886. // %res = { %5.0, %0 || %1.1 || %2.1 || %5.1 }
  2887. SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
  2888. SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
  2889. SplitInteger(LHS, LHSLow, LHSHigh);
  2890. SplitInteger(RHS, RHSLow, RHSHigh);
  2891. EVT HalfVT = LHSLow.getValueType()
  2892. , BitVT = N->getValueType(1);
  2893. SDVTList VTHalfMulO = DAG.getVTList(HalfVT, BitVT);
  2894. SDVTList VTFullAddO = DAG.getVTList(VT, BitVT);
  2895. SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
  2896. SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
  2897. DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
  2898. DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
  2899. SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfMulO, LHSHigh, RHSLow);
  2900. Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
  2901. SDValue OneInHigh = DAG.getNode(ISD::BUILD_PAIR, dl, VT, HalfZero,
  2902. One.getValue(0));
  2903. SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfMulO, RHSHigh, LHSLow);
  2904. Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
  2905. SDValue TwoInHigh = DAG.getNode(ISD::BUILD_PAIR, dl, VT, HalfZero,
  2906. Two.getValue(0));
  2907. // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
  2908. // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
  2909. // operation recursively legalized?).
  2910. //
  2911. // Many backends understand this pattern and will convert into LOHI
  2912. // themselves, if applicable.
  2913. SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
  2914. DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
  2915. DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
  2916. SDValue Four = DAG.getNode(ISD::ADD, dl, VT, OneInHigh, TwoInHigh);
  2917. SDValue Five = DAG.getNode(ISD::UADDO, dl, VTFullAddO, Three, Four);
  2918. Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Five.getValue(1));
  2919. SplitInteger(Five, Lo, Hi);
  2920. ReplaceValueWith(SDValue(N, 1), Overflow);
  2921. return;
  2922. }
  2923. Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
  2924. EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
  2925. Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
  2926. // Replace this with a libcall that will check overflow.
  2927. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2928. if (VT == MVT::i32)
  2929. LC = RTLIB::MULO_I32;
  2930. else if (VT == MVT::i64)
  2931. LC = RTLIB::MULO_I64;
  2932. else if (VT == MVT::i128)
  2933. LC = RTLIB::MULO_I128;
  2934. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!");
  2935. SDValue Temp = DAG.CreateStackTemporary(PtrVT);
  2936. // Temporary for the overflow value, default it to zero.
  2937. SDValue Chain =
  2938. DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
  2939. MachinePointerInfo());
  2940. TargetLowering::ArgListTy Args;
  2941. TargetLowering::ArgListEntry Entry;
  2942. for (const SDValue &Op : N->op_values()) {
  2943. EVT ArgVT = Op.getValueType();
  2944. Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
  2945. Entry.Node = Op;
  2946. Entry.Ty = ArgTy;
  2947. Entry.IsSExt = true;
  2948. Entry.IsZExt = false;
  2949. Args.push_back(Entry);
  2950. }
  2951. // Also pass the address of the overflow check.
  2952. Entry.Node = Temp;
  2953. Entry.Ty = PtrTy->getPointerTo();
  2954. Entry.IsSExt = true;
  2955. Entry.IsZExt = false;
  2956. Args.push_back(Entry);
  2957. SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
  2958. TargetLowering::CallLoweringInfo CLI(DAG);
  2959. CLI.setDebugLoc(dl)
  2960. .setChain(Chain)
  2961. .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args))
  2962. .setSExtResult();
  2963. std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
  2964. SplitInteger(CallInfo.first, Lo, Hi);
  2965. SDValue Temp2 =
  2966. DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
  2967. SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
  2968. DAG.getConstant(0, dl, PtrVT),
  2969. ISD::SETNE);
  2970. // Use the overflow from the libcall everywhere.
  2971. ReplaceValueWith(SDValue(N, 1), Ofl);
  2972. }
  2973. void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
  2974. SDValue &Lo, SDValue &Hi) {
  2975. EVT VT = N->getValueType(0);
  2976. SDLoc dl(N);
  2977. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2978. if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
  2979. SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  2980. SplitInteger(Res.getValue(0), Lo, Hi);
  2981. return;
  2982. }
  2983. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2984. if (VT == MVT::i16)
  2985. LC = RTLIB::UDIV_I16;
  2986. else if (VT == MVT::i32)
  2987. LC = RTLIB::UDIV_I32;
  2988. else if (VT == MVT::i64)
  2989. LC = RTLIB::UDIV_I64;
  2990. else if (VT == MVT::i128)
  2991. LC = RTLIB::UDIV_I128;
  2992. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
  2993. TargetLowering::MakeLibCallOptions CallOptions;
  2994. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
  2995. }
  2996. void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
  2997. SDValue &Lo, SDValue &Hi) {
  2998. EVT VT = N->getValueType(0);
  2999. SDLoc dl(N);
  3000. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  3001. if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
  3002. SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  3003. SplitInteger(Res.getValue(1), Lo, Hi);
  3004. return;
  3005. }
  3006. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  3007. if (VT == MVT::i16)
  3008. LC = RTLIB::UREM_I16;
  3009. else if (VT == MVT::i32)
  3010. LC = RTLIB::UREM_I32;
  3011. else if (VT == MVT::i64)
  3012. LC = RTLIB::UREM_I64;
  3013. else if (VT == MVT::i128)
  3014. LC = RTLIB::UREM_I128;
  3015. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
  3016. TargetLowering::MakeLibCallOptions CallOptions;
  3017. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
  3018. }
  3019. void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
  3020. SDValue &Lo, SDValue &Hi) {
  3021. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  3022. SDLoc dl(N);
  3023. SDValue Op = N->getOperand(0);
  3024. if (Op.getValueType().bitsLE(NVT)) {
  3025. // The low part is zero extension of the input (degenerates to a copy).
  3026. Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
  3027. Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
  3028. } else {
  3029. // For example, extension of an i48 to an i64. The operand type necessarily
  3030. // promotes to the result type, so will end up being expanded too.
  3031. assert(getTypeAction(Op.getValueType()) ==
  3032. TargetLowering::TypePromoteInteger &&
  3033. "Only know how to promote this result!");
  3034. SDValue Res = GetPromotedInteger(Op);
  3035. assert(Res.getValueType() == N->getValueType(0) &&
  3036. "Operand over promoted?");
  3037. // Split the promoted operand. This will simplify when it is expanded.
  3038. SplitInteger(Res, Lo, Hi);
  3039. unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
  3040. Hi = DAG.getZeroExtendInReg(Hi, dl,
  3041. EVT::getIntegerVT(*DAG.getContext(),
  3042. ExcessBits));
  3043. }
  3044. }
  3045. void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
  3046. SDValue &Lo, SDValue &Hi) {
  3047. SDLoc dl(N);
  3048. EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
  3049. SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
  3050. SDValue Zero = DAG.getConstant(0, dl, VT);
  3051. SDValue Swap = DAG.getAtomicCmpSwap(
  3052. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
  3053. cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
  3054. N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
  3055. ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
  3056. ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
  3057. }
  3058. void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
  3059. SDValue &Lo, SDValue &Hi) {
  3060. // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
  3061. // both halves independently.
  3062. SDValue Res = TLI.expandVecReduce(N, DAG);
  3063. SplitInteger(Res, Lo, Hi);
  3064. }
  3065. //===----------------------------------------------------------------------===//
  3066. // Integer Operand Expansion
  3067. //===----------------------------------------------------------------------===//
  3068. /// ExpandIntegerOperand - This method is called when the specified operand of
  3069. /// the specified node is found to need expansion. At this point, all of the
  3070. /// result types of the node are known to be legal, but other operands of the
  3071. /// node may need promotion or expansion as well as the specified one.
  3072. bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
  3073. LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG);
  3074. dbgs() << "\n");
  3075. SDValue Res = SDValue();
  3076. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  3077. return false;
  3078. switch (N->getOpcode()) {
  3079. default:
  3080. #ifndef NDEBUG
  3081. dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
  3082. N->dump(&DAG); dbgs() << "\n";
  3083. #endif
  3084. report_fatal_error("Do not know how to expand this operator's operand!");
  3085. case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
  3086. case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
  3087. case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
  3088. case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
  3089. case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
  3090. case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
  3091. case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
  3092. case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
  3093. case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
  3094. case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
  3095. case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
  3096. case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
  3097. case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
  3098. case ISD::SHL:
  3099. case ISD::SRA:
  3100. case ISD::SRL:
  3101. case ISD::ROTL:
  3102. case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
  3103. case ISD::RETURNADDR:
  3104. case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
  3105. case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
  3106. }
  3107. // If the result is null, the sub-method took care of registering results etc.
  3108. if (!Res.getNode()) return false;
  3109. // If the result is N, the sub-method updated N in place. Tell the legalizer
  3110. // core about this.
  3111. if (Res.getNode() == N)
  3112. return true;
  3113. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  3114. "Invalid operand expansion");
  3115. ReplaceValueWith(SDValue(N, 0), Res);
  3116. return false;
  3117. }
  3118. /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
  3119. /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
  3120. void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
  3121. SDValue &NewRHS,
  3122. ISD::CondCode &CCCode,
  3123. const SDLoc &dl) {
  3124. SDValue LHSLo, LHSHi, RHSLo, RHSHi;
  3125. GetExpandedInteger(NewLHS, LHSLo, LHSHi);
  3126. GetExpandedInteger(NewRHS, RHSLo, RHSHi);
  3127. if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
  3128. if (RHSLo == RHSHi) {
  3129. if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
  3130. if (RHSCST->isAllOnesValue()) {
  3131. // Equality comparison to -1.
  3132. NewLHS = DAG.getNode(ISD::AND, dl,
  3133. LHSLo.getValueType(), LHSLo, LHSHi);
  3134. NewRHS = RHSLo;
  3135. return;
  3136. }
  3137. }
  3138. }
  3139. NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
  3140. NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
  3141. NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
  3142. NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
  3143. return;
  3144. }
  3145. // If this is a comparison of the sign bit, just look at the top part.
  3146. // X > -1, x < 0
  3147. if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
  3148. if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
  3149. (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
  3150. NewLHS = LHSHi;
  3151. NewRHS = RHSHi;
  3152. return;
  3153. }
  3154. // FIXME: This generated code sucks.
  3155. ISD::CondCode LowCC;
  3156. switch (CCCode) {
  3157. default: llvm_unreachable("Unknown integer setcc!");
  3158. case ISD::SETLT:
  3159. case ISD::SETULT: LowCC = ISD::SETULT; break;
  3160. case ISD::SETGT:
  3161. case ISD::SETUGT: LowCC = ISD::SETUGT; break;
  3162. case ISD::SETLE:
  3163. case ISD::SETULE: LowCC = ISD::SETULE; break;
  3164. case ISD::SETGE:
  3165. case ISD::SETUGE: LowCC = ISD::SETUGE; break;
  3166. }
  3167. // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
  3168. // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
  3169. // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
  3170. // NOTE: on targets without efficient SELECT of bools, we can always use
  3171. // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
  3172. TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
  3173. nullptr);
  3174. SDValue LoCmp, HiCmp;
  3175. if (TLI.isTypeLegal(LHSLo.getValueType()) &&
  3176. TLI.isTypeLegal(RHSLo.getValueType()))
  3177. LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
  3178. RHSLo, LowCC, false, DagCombineInfo, dl);
  3179. if (!LoCmp.getNode())
  3180. LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
  3181. RHSLo, LowCC);
  3182. if (TLI.isTypeLegal(LHSHi.getValueType()) &&
  3183. TLI.isTypeLegal(RHSHi.getValueType()))
  3184. HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
  3185. RHSHi, CCCode, false, DagCombineInfo, dl);
  3186. if (!HiCmp.getNode())
  3187. HiCmp =
  3188. DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
  3189. LHSHi, RHSHi, DAG.getCondCode(CCCode));
  3190. ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
  3191. ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
  3192. bool EqAllowed = (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
  3193. CCCode == ISD::SETUGE || CCCode == ISD::SETULE);
  3194. if ((EqAllowed && (HiCmpC && HiCmpC->isNullValue())) ||
  3195. (!EqAllowed && ((HiCmpC && (HiCmpC->getAPIntValue() == 1)) ||
  3196. (LoCmpC && LoCmpC->isNullValue())))) {
  3197. // For LE / GE, if high part is known false, ignore the low part.
  3198. // For LT / GT: if low part is known false, return the high part.
  3199. // if high part is known true, ignore the low part.
  3200. NewLHS = HiCmp;
  3201. NewRHS = SDValue();
  3202. return;
  3203. }
  3204. if (LHSHi == RHSHi) {
  3205. // Comparing the low bits is enough.
  3206. NewLHS = LoCmp;
  3207. NewRHS = SDValue();
  3208. return;
  3209. }
  3210. // Lower with SETCCCARRY if the target supports it.
  3211. EVT HiVT = LHSHi.getValueType();
  3212. EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
  3213. bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
  3214. // FIXME: Make all targets support this, then remove the other lowering.
  3215. if (HasSETCCCARRY) {
  3216. // SETCCCARRY can detect < and >= directly. For > and <=, flip
  3217. // operands and condition code.
  3218. bool FlipOperands = false;
  3219. switch (CCCode) {
  3220. case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
  3221. case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
  3222. case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
  3223. case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
  3224. default: break;
  3225. }
  3226. if (FlipOperands) {
  3227. std::swap(LHSLo, RHSLo);
  3228. std::swap(LHSHi, RHSHi);
  3229. }
  3230. // Perform a wide subtraction, feeding the carry from the low part into
  3231. // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
  3232. // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
  3233. // zero or positive iff LHS >= RHS.
  3234. EVT LoVT = LHSLo.getValueType();
  3235. SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
  3236. SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
  3237. SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
  3238. LHSHi, RHSHi, LowCmp.getValue(1),
  3239. DAG.getCondCode(CCCode));
  3240. NewLHS = Res;
  3241. NewRHS = SDValue();
  3242. return;
  3243. }
  3244. NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
  3245. false, DagCombineInfo, dl);
  3246. if (!NewLHS.getNode())
  3247. NewLHS =
  3248. DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
  3249. NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
  3250. NewRHS = SDValue();
  3251. }
  3252. SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
  3253. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  3254. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  3255. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  3256. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  3257. // against zero to select between true and false values.
  3258. if (!NewRHS.getNode()) {
  3259. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  3260. CCCode = ISD::SETNE;
  3261. }
  3262. // Update N to have the operands specified.
  3263. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  3264. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  3265. N->getOperand(4)), 0);
  3266. }
  3267. SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
  3268. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  3269. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  3270. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  3271. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  3272. // against zero to select between true and false values.
  3273. if (!NewRHS.getNode()) {
  3274. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  3275. CCCode = ISD::SETNE;
  3276. }
  3277. // Update N to have the operands specified.
  3278. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  3279. N->getOperand(2), N->getOperand(3),
  3280. DAG.getCondCode(CCCode)), 0);
  3281. }
  3282. SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
  3283. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  3284. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  3285. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  3286. // If ExpandSetCCOperands returned a scalar, use it.
  3287. if (!NewRHS.getNode()) {
  3288. assert(NewLHS.getValueType() == N->getValueType(0) &&
  3289. "Unexpected setcc expansion!");
  3290. return NewLHS;
  3291. }
  3292. // Otherwise, update N to have the operands specified.
  3293. return SDValue(
  3294. DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
  3295. }
  3296. SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
  3297. SDValue LHS = N->getOperand(0);
  3298. SDValue RHS = N->getOperand(1);
  3299. SDValue Carry = N->getOperand(2);
  3300. SDValue Cond = N->getOperand(3);
  3301. SDLoc dl = SDLoc(N);
  3302. SDValue LHSLo, LHSHi, RHSLo, RHSHi;
  3303. GetExpandedInteger(LHS, LHSLo, LHSHi);
  3304. GetExpandedInteger(RHS, RHSLo, RHSHi);
  3305. // Expand to a SUBE for the low part and a smaller SETCCCARRY for the high.
  3306. SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
  3307. SDValue LowCmp = DAG.getNode(ISD::SUBCARRY, dl, VTList, LHSLo, RHSLo, Carry);
  3308. return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
  3309. LowCmp.getValue(1), Cond);
  3310. }
  3311. SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
  3312. // The value being shifted is legal, but the shift amount is too big.
  3313. // It follows that either the result of the shift is undefined, or the
  3314. // upper half of the shift amount is zero. Just use the lower half.
  3315. SDValue Lo, Hi;
  3316. GetExpandedInteger(N->getOperand(1), Lo, Hi);
  3317. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
  3318. }
  3319. SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
  3320. // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
  3321. // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
  3322. // constant to valid type.
  3323. SDValue Lo, Hi;
  3324. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  3325. return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
  3326. }
  3327. SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
  3328. SDValue Op = N->getOperand(0);
  3329. EVT DstVT = N->getValueType(0);
  3330. RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
  3331. assert(LC != RTLIB::UNKNOWN_LIBCALL &&
  3332. "Don't know how to expand this SINT_TO_FP!");
  3333. TargetLowering::MakeLibCallOptions CallOptions;
  3334. CallOptions.setSExt(true);
  3335. return TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N)).first;
  3336. }
  3337. SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
  3338. if (ISD::isNormalStore(N))
  3339. return ExpandOp_NormalStore(N, OpNo);
  3340. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  3341. assert(OpNo == 1 && "Can only expand the stored value so far");
  3342. EVT VT = N->getOperand(1).getValueType();
  3343. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  3344. SDValue Ch = N->getChain();
  3345. SDValue Ptr = N->getBasePtr();
  3346. unsigned Alignment = N->getAlignment();
  3347. MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
  3348. AAMDNodes AAInfo = N->getAAInfo();
  3349. SDLoc dl(N);
  3350. SDValue Lo, Hi;
  3351. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  3352. if (N->getMemoryVT().bitsLE(NVT)) {
  3353. GetExpandedInteger(N->getValue(), Lo, Hi);
  3354. return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
  3355. N->getMemoryVT(), Alignment, MMOFlags, AAInfo);
  3356. }
  3357. if (DAG.getDataLayout().isLittleEndian()) {
  3358. // Little-endian - low bits are at low addresses.
  3359. GetExpandedInteger(N->getValue(), Lo, Hi);
  3360. Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
  3361. AAInfo);
  3362. unsigned ExcessBits =
  3363. N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
  3364. EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
  3365. // Increment the pointer to the other half.
  3366. unsigned IncrementSize = NVT.getSizeInBits()/8;
  3367. Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
  3368. Hi = DAG.getTruncStore(
  3369. Ch, dl, Hi, Ptr, N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
  3370. MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
  3371. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  3372. }
  3373. // Big-endian - high bits are at low addresses. Favor aligned stores at
  3374. // the cost of some bit-fiddling.
  3375. GetExpandedInteger(N->getValue(), Lo, Hi);
  3376. EVT ExtVT = N->getMemoryVT();
  3377. unsigned EBytes = ExtVT.getStoreSize();
  3378. unsigned IncrementSize = NVT.getSizeInBits()/8;
  3379. unsigned ExcessBits = (EBytes - IncrementSize)*8;
  3380. EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
  3381. ExtVT.getSizeInBits() - ExcessBits);
  3382. if (ExcessBits < NVT.getSizeInBits()) {
  3383. // Transfer high bits from the top of Lo to the bottom of Hi.
  3384. Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
  3385. DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
  3386. TLI.getPointerTy(DAG.getDataLayout())));
  3387. Hi = DAG.getNode(
  3388. ISD::OR, dl, NVT, Hi,
  3389. DAG.getNode(ISD::SRL, dl, NVT, Lo,
  3390. DAG.getConstant(ExcessBits, dl,
  3391. TLI.getPointerTy(DAG.getDataLayout()))));
  3392. }
  3393. // Store both the high bits and maybe some of the low bits.
  3394. Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT, Alignment,
  3395. MMOFlags, AAInfo);
  3396. // Increment the pointer to the other half.
  3397. Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
  3398. // Store the lowest ExcessBits bits in the second half.
  3399. Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
  3400. N->getPointerInfo().getWithOffset(IncrementSize),
  3401. EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
  3402. MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
  3403. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  3404. }
  3405. SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
  3406. SDValue InL, InH;
  3407. GetExpandedInteger(N->getOperand(0), InL, InH);
  3408. // Just truncate the low part of the source.
  3409. return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
  3410. }
  3411. SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
  3412. SDValue Op = N->getOperand(0);
  3413. EVT SrcVT = Op.getValueType();
  3414. EVT DstVT = N->getValueType(0);
  3415. SDLoc dl(N);
  3416. // The following optimization is valid only if every value in SrcVT (when
  3417. // treated as signed) is representable in DstVT. Check that the mantissa
  3418. // size of DstVT is >= than the number of bits in SrcVT -1.
  3419. const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT);
  3420. if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 &&
  3421. TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
  3422. // Do a signed conversion then adjust the result.
  3423. SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
  3424. SignedConv = TLI.LowerOperation(SignedConv, DAG);
  3425. // The result of the signed conversion needs adjusting if the 'sign bit' of
  3426. // the incoming integer was set. To handle this, we dynamically test to see
  3427. // if it is set, and, if so, add a fudge factor.
  3428. const uint64_t F32TwoE32 = 0x4F800000ULL;
  3429. const uint64_t F32TwoE64 = 0x5F800000ULL;
  3430. const uint64_t F32TwoE128 = 0x7F800000ULL;
  3431. APInt FF(32, 0);
  3432. if (SrcVT == MVT::i32)
  3433. FF = APInt(32, F32TwoE32);
  3434. else if (SrcVT == MVT::i64)
  3435. FF = APInt(32, F32TwoE64);
  3436. else if (SrcVT == MVT::i128)
  3437. FF = APInt(32, F32TwoE128);
  3438. else
  3439. llvm_unreachable("Unsupported UINT_TO_FP!");
  3440. // Check whether the sign bit is set.
  3441. SDValue Lo, Hi;
  3442. GetExpandedInteger(Op, Lo, Hi);
  3443. SDValue SignSet = DAG.getSetCC(dl,
  3444. getSetCCResultType(Hi.getValueType()),
  3445. Hi,
  3446. DAG.getConstant(0, dl, Hi.getValueType()),
  3447. ISD::SETLT);
  3448. // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
  3449. SDValue FudgePtr =
  3450. DAG.getConstantPool(ConstantInt::get(*DAG.getContext(), FF.zext(64)),
  3451. TLI.getPointerTy(DAG.getDataLayout()));
  3452. // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
  3453. SDValue Zero = DAG.getIntPtrConstant(0, dl);
  3454. SDValue Four = DAG.getIntPtrConstant(4, dl);
  3455. if (DAG.getDataLayout().isBigEndian())
  3456. std::swap(Zero, Four);
  3457. SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet,
  3458. Zero, Four);
  3459. unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
  3460. FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(),
  3461. FudgePtr, Offset);
  3462. Alignment = std::min(Alignment, 4u);
  3463. // Load the value out, extending it from f32 to the destination float type.
  3464. // FIXME: Avoid the extend by constructing the right constant pool?
  3465. SDValue Fudge = DAG.getExtLoad(
  3466. ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(), FudgePtr,
  3467. MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
  3468. Alignment);
  3469. return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
  3470. }
  3471. // Otherwise, use a libcall.
  3472. RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
  3473. assert(LC != RTLIB::UNKNOWN_LIBCALL &&
  3474. "Don't know how to expand this UINT_TO_FP!");
  3475. TargetLowering::MakeLibCallOptions CallOptions;
  3476. CallOptions.setSExt(true);
  3477. return TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, dl).first;
  3478. }
  3479. SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
  3480. SDLoc dl(N);
  3481. SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
  3482. cast<AtomicSDNode>(N)->getMemoryVT(),
  3483. N->getOperand(0),
  3484. N->getOperand(1), N->getOperand(2),
  3485. cast<AtomicSDNode>(N)->getMemOperand());
  3486. return Swap.getValue(1);
  3487. }
  3488. SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
  3489. EVT OutVT = N->getValueType(0);
  3490. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  3491. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  3492. unsigned OutNumElems = OutVT.getVectorNumElements();
  3493. EVT NOutVTElem = NOutVT.getVectorElementType();
  3494. SDLoc dl(N);
  3495. SDValue BaseIdx = N->getOperand(1);
  3496. SDValue InOp0 = N->getOperand(0);
  3497. if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
  3498. InOp0 = GetPromotedInteger(N->getOperand(0));
  3499. EVT InVT = InOp0.getValueType();
  3500. SmallVector<SDValue, 8> Ops;
  3501. Ops.reserve(OutNumElems);
  3502. for (unsigned i = 0; i != OutNumElems; ++i) {
  3503. // Extract the element from the original vector.
  3504. SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
  3505. BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
  3506. SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  3507. InVT.getVectorElementType(), N->getOperand(0), Index);
  3508. SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
  3509. // Insert the converted element to the new vector.
  3510. Ops.push_back(Op);
  3511. }
  3512. return DAG.getBuildVector(NOutVT, dl, Ops);
  3513. }
  3514. SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
  3515. ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
  3516. EVT VT = N->getValueType(0);
  3517. SDLoc dl(N);
  3518. ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
  3519. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  3520. SDValue V1 = GetPromotedInteger(N->getOperand(1));
  3521. EVT OutVT = V0.getValueType();
  3522. return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
  3523. }
  3524. SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
  3525. EVT OutVT = N->getValueType(0);
  3526. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  3527. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  3528. unsigned NumElems = N->getNumOperands();
  3529. EVT NOutVTElem = NOutVT.getVectorElementType();
  3530. SDLoc dl(N);
  3531. SmallVector<SDValue, 8> Ops;
  3532. Ops.reserve(NumElems);
  3533. for (unsigned i = 0; i != NumElems; ++i) {
  3534. SDValue Op;
  3535. // BUILD_VECTOR integer operand types are allowed to be larger than the
  3536. // result's element type. This may still be true after the promotion. For
  3537. // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
  3538. // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
  3539. if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
  3540. Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
  3541. else
  3542. Op = N->getOperand(i);
  3543. Ops.push_back(Op);
  3544. }
  3545. return DAG.getBuildVector(NOutVT, dl, Ops);
  3546. }
  3547. SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
  3548. SDLoc dl(N);
  3549. assert(!N->getOperand(0).getValueType().isVector() &&
  3550. "Input must be a scalar");
  3551. EVT OutVT = N->getValueType(0);
  3552. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  3553. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  3554. EVT NOutVTElem = NOutVT.getVectorElementType();
  3555. SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
  3556. return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
  3557. }
  3558. SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
  3559. SDLoc dl(N);
  3560. EVT OutVT = N->getValueType(0);
  3561. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  3562. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  3563. EVT OutElemTy = NOutVT.getVectorElementType();
  3564. unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
  3565. unsigned NumOutElem = NOutVT.getVectorNumElements();
  3566. unsigned NumOperands = N->getNumOperands();
  3567. assert(NumElem * NumOperands == NumOutElem &&
  3568. "Unexpected number of elements");
  3569. // Take the elements from the first vector.
  3570. SmallVector<SDValue, 8> Ops(NumOutElem);
  3571. for (unsigned i = 0; i < NumOperands; ++i) {
  3572. SDValue Op = N->getOperand(i);
  3573. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
  3574. Op = GetPromotedInteger(Op);
  3575. EVT SclrTy = Op.getValueType().getVectorElementType();
  3576. assert(NumElem == Op.getValueType().getVectorNumElements() &&
  3577. "Unexpected number of elements");
  3578. for (unsigned j = 0; j < NumElem; ++j) {
  3579. SDValue Ext = DAG.getNode(
  3580. ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
  3581. DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  3582. Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
  3583. }
  3584. }
  3585. return DAG.getBuildVector(NOutVT, dl, Ops);
  3586. }
  3587. SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
  3588. EVT VT = N->getValueType(0);
  3589. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  3590. assert(NVT.isVector() && "This type must be promoted to a vector type");
  3591. SDLoc dl(N);
  3592. // For operands whose TypeAction is to promote, extend the promoted node
  3593. // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
  3594. // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
  3595. // type..
  3596. if (getTypeAction(N->getOperand(0).getValueType())
  3597. == TargetLowering::TypePromoteInteger) {
  3598. SDValue Promoted;
  3599. switch(N->getOpcode()) {
  3600. case ISD::SIGN_EXTEND_VECTOR_INREG:
  3601. Promoted = SExtPromotedInteger(N->getOperand(0));
  3602. break;
  3603. case ISD::ZERO_EXTEND_VECTOR_INREG:
  3604. Promoted = ZExtPromotedInteger(N->getOperand(0));
  3605. break;
  3606. case ISD::ANY_EXTEND_VECTOR_INREG:
  3607. Promoted = GetPromotedInteger(N->getOperand(0));
  3608. break;
  3609. default:
  3610. llvm_unreachable("Node has unexpected Opcode");
  3611. }
  3612. return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
  3613. }
  3614. // Directly extend to the appropriate transform-to type.
  3615. return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  3616. }
  3617. SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
  3618. EVT OutVT = N->getValueType(0);
  3619. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  3620. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  3621. EVT NOutVTElem = NOutVT.getVectorElementType();
  3622. SDLoc dl(N);
  3623. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  3624. SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
  3625. NOutVTElem, N->getOperand(1));
  3626. return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
  3627. V0, ConvElem, N->getOperand(2));
  3628. }
  3629. SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
  3630. // The VECREDUCE result size may be larger than the element size, so
  3631. // we can simply change the result type.
  3632. SDLoc dl(N);
  3633. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  3634. return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  3635. }
  3636. SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
  3637. SDLoc dl(N);
  3638. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  3639. SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
  3640. TLI.getVectorIdxTy(DAG.getDataLayout()));
  3641. SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  3642. V0->getValueType(0).getScalarType(), V0, V1);
  3643. // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
  3644. // element types. If this is the case then we need to expand the outgoing
  3645. // value and not truncate it.
  3646. return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
  3647. }
  3648. SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
  3649. SDLoc dl(N);
  3650. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  3651. MVT InVT = V0.getValueType().getSimpleVT();
  3652. MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
  3653. N->getValueType(0).getVectorNumElements());
  3654. SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
  3655. return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
  3656. }
  3657. SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
  3658. SDLoc dl(N);
  3659. unsigned NumElems = N->getNumOperands();
  3660. EVT RetSclrTy = N->getValueType(0).getVectorElementType();
  3661. SmallVector<SDValue, 8> NewOps;
  3662. NewOps.reserve(NumElems);
  3663. // For each incoming vector
  3664. for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
  3665. SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
  3666. EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
  3667. unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
  3668. for (unsigned i=0; i<NumElem; ++i) {
  3669. // Extract element from incoming vector
  3670. SDValue Ex = DAG.getNode(
  3671. ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
  3672. DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  3673. SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
  3674. NewOps.push_back(Tr);
  3675. }
  3676. }
  3677. return DAG.getBuildVector(N->getValueType(0), dl, NewOps);
  3678. }