LegalizeIntegerTypes.cpp 165 KB

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