LegalizeIntegerTypes.cpp 155 KB

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