LegalizeIntegerTypes.cpp 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199
  1. //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements integer type expansion and promotion for LegalizeTypes.
  11. // Promotion is the act of changing a computation in an illegal type into a
  12. // computation in a larger type. For example, implementing i8 arithmetic in an
  13. // i32 register (often needed on powerpc).
  14. // Expansion is the act of changing a computation in an illegal type into a
  15. // computation in two identical registers of a smaller type. For example,
  16. // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
  17. // targets).
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "LegalizeTypes.h"
  21. #include "llvm/IR/DerivedTypes.h"
  22. #include "llvm/Support/ErrorHandling.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. DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG); dbgs() << "\n");
  35. SDValue Res = SDValue();
  36. // See if the target wants to custom expand this node.
  37. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  38. return;
  39. switch (N->getOpcode()) {
  40. default:
  41. #ifndef NDEBUG
  42. dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
  43. N->dump(&DAG); dbgs() << "\n";
  44. #endif
  45. llvm_unreachable("Do not know how to promote this operator!");
  46. case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
  47. case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
  48. case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
  49. case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
  50. case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
  51. case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
  52. case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
  53. case ISD::CONVERT_RNDSAT:
  54. Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
  55. case ISD::CTLZ_ZERO_UNDEF:
  56. case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
  57. case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break;
  58. case ISD::CTTZ_ZERO_UNDEF:
  59. case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
  60. case ISD::EXTRACT_VECTOR_ELT:
  61. Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
  62. case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
  63. case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));break;
  64. case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
  65. case ISD::VSELECT: Res = PromoteIntRes_VSELECT(N); break;
  66. case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
  67. case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
  68. case ISD::SMIN:
  69. case ISD::SMAX:
  70. case ISD::UMIN:
  71. case ISD::UMAX: Res = PromoteIntRes_SimpleIntBinOp(N); break;
  72. case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
  73. case ISD::SIGN_EXTEND_INREG:
  74. Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
  75. case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
  76. case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
  77. case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
  78. case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
  79. case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
  80. case ISD::EXTRACT_SUBVECTOR:
  81. Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
  82. case ISD::VECTOR_SHUFFLE:
  83. Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
  84. case ISD::INSERT_VECTOR_ELT:
  85. Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
  86. case ISD::BUILD_VECTOR:
  87. Res = PromoteIntRes_BUILD_VECTOR(N); break;
  88. case ISD::SCALAR_TO_VECTOR:
  89. Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
  90. case ISD::CONCAT_VECTORS:
  91. Res = PromoteIntRes_CONCAT_VECTORS(N); break;
  92. case ISD::SIGN_EXTEND:
  93. case ISD::ZERO_EXTEND:
  94. case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
  95. case ISD::FP_TO_SINT:
  96. case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
  97. case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break;
  98. case ISD::AND:
  99. case ISD::OR:
  100. case ISD::XOR:
  101. case ISD::ADD:
  102. case ISD::SUB:
  103. case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
  104. case ISD::SDIV:
  105. case ISD::SREM: Res = PromoteIntRes_SDIV(N); break;
  106. case ISD::UDIV:
  107. case ISD::UREM: Res = PromoteIntRes_UDIV(N); break;
  108. case ISD::SADDO:
  109. case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
  110. case ISD::UADDO:
  111. case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
  112. case ISD::SMULO:
  113. case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
  114. case ISD::ATOMIC_LOAD:
  115. Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
  116. case ISD::ATOMIC_LOAD_ADD:
  117. case ISD::ATOMIC_LOAD_SUB:
  118. case ISD::ATOMIC_LOAD_AND:
  119. case ISD::ATOMIC_LOAD_OR:
  120. case ISD::ATOMIC_LOAD_XOR:
  121. case ISD::ATOMIC_LOAD_NAND:
  122. case ISD::ATOMIC_LOAD_MIN:
  123. case ISD::ATOMIC_LOAD_MAX:
  124. case ISD::ATOMIC_LOAD_UMIN:
  125. case ISD::ATOMIC_LOAD_UMAX:
  126. case ISD::ATOMIC_SWAP:
  127. Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
  128. case ISD::ATOMIC_CMP_SWAP:
  129. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
  130. Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
  131. break;
  132. }
  133. // If the result is null then the sub-method took care of registering it.
  134. if (Res.getNode())
  135. SetPromotedInteger(SDValue(N, ResNo), Res);
  136. }
  137. SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
  138. unsigned ResNo) {
  139. SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
  140. return GetPromotedInteger(Op);
  141. }
  142. SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
  143. // Sign-extend the new bits, and continue the assertion.
  144. SDValue Op = SExtPromotedInteger(N->getOperand(0));
  145. return DAG.getNode(ISD::AssertSext, SDLoc(N),
  146. Op.getValueType(), Op, N->getOperand(1));
  147. }
  148. SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
  149. // Zero the new bits, and continue the assertion.
  150. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  151. return DAG.getNode(ISD::AssertZext, SDLoc(N),
  152. Op.getValueType(), Op, N->getOperand(1));
  153. }
  154. SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
  155. EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  156. SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
  157. N->getMemoryVT(), ResVT,
  158. N->getChain(), N->getBasePtr(),
  159. N->getMemOperand(), N->getOrdering(),
  160. N->getSynchScope());
  161. // Legalized the chain result - switch anything that used the old chain to
  162. // use the new one.
  163. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  164. return Res;
  165. }
  166. SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
  167. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  168. SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
  169. N->getMemoryVT(),
  170. N->getChain(), N->getBasePtr(),
  171. Op2, N->getMemOperand(), N->getOrdering(),
  172. N->getSynchScope());
  173. // Legalized the chain result - switch anything that used the old chain to
  174. // use the new one.
  175. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  176. return Res;
  177. }
  178. SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
  179. unsigned ResNo) {
  180. if (ResNo == 1) {
  181. assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
  182. EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
  183. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
  184. // Only use the result of getSetCCResultType if it is legal,
  185. // otherwise just use the promoted result type (NVT).
  186. if (!TLI.isTypeLegal(SVT))
  187. SVT = NVT;
  188. SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
  189. SDValue Res = DAG.getAtomicCmpSwap(
  190. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
  191. N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
  192. N->getMemOperand(), N->getSuccessOrdering(), N->getFailureOrdering(),
  193. N->getSynchScope());
  194. ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
  195. ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
  196. return Res.getValue(1);
  197. }
  198. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  199. SDValue Op3 = GetPromotedInteger(N->getOperand(3));
  200. SDVTList VTs =
  201. DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
  202. SDValue Res = DAG.getAtomicCmpSwap(
  203. N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
  204. N->getBasePtr(), Op2, Op3, N->getMemOperand(), N->getSuccessOrdering(),
  205. N->getFailureOrdering(), N->getSynchScope());
  206. // Update the use to N with the newly created Res.
  207. for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
  208. ReplaceValueWith(SDValue(N, i), Res.getValue(i));
  209. return Res;
  210. }
  211. SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
  212. SDValue InOp = N->getOperand(0);
  213. EVT InVT = InOp.getValueType();
  214. EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
  215. EVT OutVT = N->getValueType(0);
  216. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  217. SDLoc dl(N);
  218. switch (getTypeAction(InVT)) {
  219. case TargetLowering::TypeLegal:
  220. break;
  221. case TargetLowering::TypePromoteInteger:
  222. if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
  223. // The input promotes to the same size. Convert the promoted value.
  224. return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
  225. break;
  226. case TargetLowering::TypeSoftenFloat:
  227. // Promote the integer operand by hand.
  228. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
  229. case TargetLowering::TypePromoteFloat: {
  230. // Convert the promoted float by hand.
  231. if (NOutVT.bitsEq(NInVT)) {
  232. SDValue PromotedOp = GetPromotedFloat(InOp);
  233. SDValue Trunc = DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, PromotedOp);
  234. return DAG.getNode(ISD::AssertZext, dl, NOutVT, Trunc,
  235. DAG.getValueType(OutVT));
  236. }
  237. break;
  238. }
  239. case TargetLowering::TypeExpandInteger:
  240. case TargetLowering::TypeExpandFloat:
  241. break;
  242. case TargetLowering::TypeScalarizeVector:
  243. // Convert the element to an integer and promote it by hand.
  244. if (!NOutVT.isVector())
  245. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
  246. BitConvertToInteger(GetScalarizedVector(InOp)));
  247. break;
  248. case TargetLowering::TypeSplitVector: {
  249. // For example, i32 = BITCAST v2i16 on alpha. Convert the split
  250. // pieces of the input into integers and reassemble in the final type.
  251. SDValue Lo, Hi;
  252. GetSplitVector(N->getOperand(0), Lo, Hi);
  253. Lo = BitConvertToInteger(Lo);
  254. Hi = BitConvertToInteger(Hi);
  255. if (DAG.getDataLayout().isBigEndian())
  256. std::swap(Lo, Hi);
  257. InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
  258. EVT::getIntegerVT(*DAG.getContext(),
  259. NOutVT.getSizeInBits()),
  260. JoinIntegers(Lo, Hi));
  261. return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
  262. }
  263. case TargetLowering::TypeWidenVector:
  264. // The input is widened to the same size. Convert to the widened value.
  265. // Make sure that the outgoing value is not a vector, because this would
  266. // make us bitcast between two vectors which are legalized in different ways.
  267. if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector())
  268. return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
  269. }
  270. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
  271. CreateStackStoreLoad(InOp, OutVT));
  272. }
  273. SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
  274. SDValue Op = GetPromotedInteger(N->getOperand(0));
  275. EVT OVT = N->getValueType(0);
  276. EVT NVT = Op.getValueType();
  277. SDLoc dl(N);
  278. unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
  279. return DAG.getNode(
  280. ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
  281. DAG.getConstant(DiffBits, dl,
  282. TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
  283. }
  284. SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
  285. // The pair element type may be legal, or may not promote to the same type as
  286. // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
  287. return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
  288. TLI.getTypeToTransformTo(*DAG.getContext(),
  289. N->getValueType(0)), JoinIntegers(N->getOperand(0),
  290. N->getOperand(1)));
  291. }
  292. SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
  293. EVT VT = N->getValueType(0);
  294. // FIXME there is no actual debug info here
  295. SDLoc dl(N);
  296. // Zero extend things like i1, sign extend everything else. It shouldn't
  297. // matter in theory which one we pick, but this tends to give better code?
  298. unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
  299. SDValue Result = DAG.getNode(Opc, dl,
  300. TLI.getTypeToTransformTo(*DAG.getContext(), VT),
  301. SDValue(N, 0));
  302. assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
  303. return Result;
  304. }
  305. SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
  306. ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
  307. assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
  308. CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
  309. CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
  310. "can only promote integers");
  311. EVT OutVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  312. return DAG.getConvertRndSat(OutVT, SDLoc(N), N->getOperand(0),
  313. N->getOperand(1), N->getOperand(2),
  314. N->getOperand(3), N->getOperand(4), CvtCode);
  315. }
  316. SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
  317. // Zero extend to the promoted type and do the count there.
  318. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  319. SDLoc dl(N);
  320. EVT OVT = N->getValueType(0);
  321. EVT NVT = Op.getValueType();
  322. Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
  323. // Subtract off the extra leading bits in the bigger type.
  324. return DAG.getNode(
  325. ISD::SUB, dl, NVT, Op,
  326. DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
  327. NVT));
  328. }
  329. SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
  330. // Zero extend to the promoted type and do the count there.
  331. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  332. return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op);
  333. }
  334. SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
  335. SDValue Op = GetPromotedInteger(N->getOperand(0));
  336. EVT OVT = N->getValueType(0);
  337. EVT NVT = Op.getValueType();
  338. SDLoc dl(N);
  339. if (N->getOpcode() == ISD::CTTZ) {
  340. // The count is the same in the promoted type except if the original
  341. // value was zero. This can be handled by setting the bit just off
  342. // the top of the original type.
  343. auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
  344. OVT.getScalarSizeInBits());
  345. Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
  346. }
  347. return DAG.getNode(N->getOpcode(), dl, NVT, Op);
  348. }
  349. SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
  350. SDLoc dl(N);
  351. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  352. return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0),
  353. N->getOperand(1));
  354. }
  355. SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
  356. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  357. unsigned NewOpc = N->getOpcode();
  358. SDLoc dl(N);
  359. // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
  360. // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
  361. // and SINT conversions are Custom, there is no way to tell which is
  362. // preferable. We choose SINT because that's the right thing on PPC.)
  363. if (N->getOpcode() == ISD::FP_TO_UINT &&
  364. !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
  365. TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
  366. NewOpc = ISD::FP_TO_SINT;
  367. SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
  368. // Assert that the converted value fits in the original type. If it doesn't
  369. // (eg: because the value being converted is too big), then the result of the
  370. // original operation was undefined anyway, so the assert is still correct.
  371. return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
  372. ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
  373. DAG.getValueType(N->getValueType(0).getScalarType()));
  374. }
  375. SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
  376. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  377. SDLoc dl(N);
  378. SDValue Res = DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  379. return DAG.getNode(ISD::AssertZext, dl,
  380. NVT, Res, DAG.getValueType(N->getValueType(0)));
  381. }
  382. SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
  383. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  384. SDLoc dl(N);
  385. if (getTypeAction(N->getOperand(0).getValueType())
  386. == TargetLowering::TypePromoteInteger) {
  387. SDValue Res = GetPromotedInteger(N->getOperand(0));
  388. assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
  389. // If the result and operand types are the same after promotion, simplify
  390. // to an in-register extension.
  391. if (NVT == Res.getValueType()) {
  392. // The high bits are not guaranteed to be anything. Insert an extend.
  393. if (N->getOpcode() == ISD::SIGN_EXTEND)
  394. return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
  395. DAG.getValueType(N->getOperand(0).getValueType()));
  396. if (N->getOpcode() == ISD::ZERO_EXTEND)
  397. return DAG.getZeroExtendInReg(Res, dl,
  398. N->getOperand(0).getValueType().getScalarType());
  399. assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
  400. return Res;
  401. }
  402. }
  403. // Otherwise, just extend the original operand all the way to the larger type.
  404. return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  405. }
  406. SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
  407. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  408. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  409. ISD::LoadExtType ExtType =
  410. ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
  411. SDLoc dl(N);
  412. SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
  413. N->getMemoryVT(), N->getMemOperand());
  414. // Legalized the chain result - switch anything that used the old chain to
  415. // use the new one.
  416. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  417. return Res;
  418. }
  419. SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
  420. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  421. SDValue ExtSrc0 = GetPromotedInteger(N->getSrc0());
  422. SDValue Mask = N->getMask();
  423. EVT NewMaskVT = getSetCCResultType(NVT);
  424. if (NewMaskVT != N->getMask().getValueType())
  425. Mask = PromoteTargetBoolean(Mask, NewMaskVT);
  426. SDLoc dl(N);
  427. SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
  428. Mask, ExtSrc0, N->getMemoryVT(),
  429. N->getMemOperand(), ISD::SEXTLOAD);
  430. // Legalized the chain result - switch anything that used the old chain to
  431. // use the new one.
  432. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  433. return Res;
  434. }
  435. /// Promote the overflow flag of an overflowing arithmetic node.
  436. SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
  437. // Simply change the return type of the boolean result.
  438. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
  439. EVT ValueVTs[] = { N->getValueType(0), NVT };
  440. SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
  441. SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N),
  442. DAG.getVTList(ValueVTs), Ops);
  443. // Modified the sum result - switch anything that used the old sum to use
  444. // the new one.
  445. ReplaceValueWith(SDValue(N, 0), Res);
  446. return SDValue(Res.getNode(), 1);
  447. }
  448. SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
  449. if (ResNo == 1)
  450. return PromoteIntRes_Overflow(N);
  451. // The operation overflowed iff the result in the larger type is not the
  452. // sign extension of its truncation to the original type.
  453. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  454. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  455. EVT OVT = N->getOperand(0).getValueType();
  456. EVT NVT = LHS.getValueType();
  457. SDLoc dl(N);
  458. // Do the arithmetic in the larger type.
  459. unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
  460. SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
  461. // Calculate the overflow flag: sign extend the arithmetic result from
  462. // the original type.
  463. SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
  464. DAG.getValueType(OVT));
  465. // Overflowed if and only if this is not equal to Res.
  466. Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
  467. // Use the calculated overflow everywhere.
  468. ReplaceValueWith(SDValue(N, 1), Ofl);
  469. return Res;
  470. }
  471. SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
  472. // Sign extend the input.
  473. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  474. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  475. return DAG.getNode(N->getOpcode(), SDLoc(N),
  476. LHS.getValueType(), LHS, RHS);
  477. }
  478. SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
  479. SDValue LHS = GetPromotedInteger(N->getOperand(1));
  480. SDValue RHS = GetPromotedInteger(N->getOperand(2));
  481. return DAG.getSelect(SDLoc(N),
  482. LHS.getValueType(), N->getOperand(0), LHS, RHS);
  483. }
  484. SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
  485. SDValue Mask = N->getOperand(0);
  486. EVT OpTy = N->getOperand(1).getValueType();
  487. // Promote all the way up to the canonical SetCC type.
  488. Mask = PromoteTargetBoolean(Mask, OpTy);
  489. SDValue LHS = GetPromotedInteger(N->getOperand(1));
  490. SDValue RHS = GetPromotedInteger(N->getOperand(2));
  491. return DAG.getNode(ISD::VSELECT, SDLoc(N),
  492. LHS.getValueType(), Mask, LHS, RHS);
  493. }
  494. SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
  495. SDValue LHS = GetPromotedInteger(N->getOperand(2));
  496. SDValue RHS = GetPromotedInteger(N->getOperand(3));
  497. return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
  498. LHS.getValueType(), N->getOperand(0),
  499. N->getOperand(1), LHS, RHS, N->getOperand(4));
  500. }
  501. SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
  502. EVT SVT = getSetCCResultType(N->getOperand(0).getValueType());
  503. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  504. // Only use the result of getSetCCResultType if it is legal,
  505. // otherwise just use the promoted result type (NVT).
  506. if (!TLI.isTypeLegal(SVT))
  507. SVT = NVT;
  508. SDLoc dl(N);
  509. assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() &&
  510. "Vector compare must return a vector result!");
  511. SDValue LHS = N->getOperand(0);
  512. SDValue RHS = N->getOperand(1);
  513. if (LHS.getValueType() != RHS.getValueType()) {
  514. if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger &&
  515. !LHS.getValueType().isVector())
  516. LHS = GetPromotedInteger(LHS);
  517. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger &&
  518. !RHS.getValueType().isVector())
  519. RHS = GetPromotedInteger(RHS);
  520. }
  521. // Get the SETCC result using the canonical SETCC type.
  522. SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, LHS, RHS,
  523. N->getOperand(2));
  524. assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
  525. // Convert to the expected type.
  526. return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
  527. }
  528. SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
  529. SDValue LHS = N->getOperand(0);
  530. SDValue RHS = N->getOperand(1);
  531. if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger)
  532. LHS = GetPromotedInteger(LHS);
  533. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
  534. RHS = ZExtPromotedInteger(RHS);
  535. return DAG.getNode(ISD::SHL, SDLoc(N), LHS.getValueType(), LHS, RHS);
  536. }
  537. SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
  538. SDValue Op = GetPromotedInteger(N->getOperand(0));
  539. return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
  540. Op.getValueType(), Op, N->getOperand(1));
  541. }
  542. SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
  543. // The input may have strange things in the top bits of the registers, but
  544. // these operations don't care. They may have weird bits going out, but
  545. // that too is okay if they are integer operations.
  546. SDValue LHS = GetPromotedInteger(N->getOperand(0));
  547. SDValue RHS = GetPromotedInteger(N->getOperand(1));
  548. return DAG.getNode(N->getOpcode(), SDLoc(N),
  549. LHS.getValueType(), LHS, RHS);
  550. }
  551. SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
  552. SDValue LHS = N->getOperand(0);
  553. SDValue RHS = N->getOperand(1);
  554. // The input value must be properly sign extended.
  555. if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger)
  556. LHS = SExtPromotedInteger(LHS);
  557. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
  558. RHS = ZExtPromotedInteger(RHS);
  559. return DAG.getNode(ISD::SRA, SDLoc(N), LHS.getValueType(), LHS, RHS);
  560. }
  561. SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
  562. SDValue LHS = N->getOperand(0);
  563. SDValue RHS = N->getOperand(1);
  564. // The input value must be properly zero extended.
  565. if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger)
  566. LHS = ZExtPromotedInteger(LHS);
  567. if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
  568. RHS = ZExtPromotedInteger(RHS);
  569. return DAG.getNode(ISD::SRL, SDLoc(N), LHS.getValueType(), LHS, RHS);
  570. }
  571. SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
  572. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  573. SDValue Res;
  574. SDValue InOp = N->getOperand(0);
  575. SDLoc dl(N);
  576. switch (getTypeAction(InOp.getValueType())) {
  577. default: llvm_unreachable("Unknown type action!");
  578. case TargetLowering::TypeLegal:
  579. case TargetLowering::TypeExpandInteger:
  580. Res = InOp;
  581. break;
  582. case TargetLowering::TypePromoteInteger:
  583. Res = GetPromotedInteger(InOp);
  584. break;
  585. case TargetLowering::TypeSplitVector:
  586. EVT InVT = InOp.getValueType();
  587. assert(InVT.isVector() && "Cannot split scalar types");
  588. unsigned NumElts = InVT.getVectorNumElements();
  589. assert(NumElts == NVT.getVectorNumElements() &&
  590. "Dst and Src must have the same number of elements");
  591. assert(isPowerOf2_32(NumElts) &&
  592. "Promoted vector type must be a power of two");
  593. SDValue EOp1, EOp2;
  594. GetSplitVector(InOp, EOp1, EOp2);
  595. EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
  596. NumElts/2);
  597. EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
  598. EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
  599. return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
  600. }
  601. // Truncate to NVT instead of VT
  602. return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
  603. }
  604. SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
  605. if (ResNo == 1)
  606. return PromoteIntRes_Overflow(N);
  607. // The operation overflowed iff the result in the larger type is not the
  608. // zero extension of its truncation to the original type.
  609. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  610. SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
  611. EVT OVT = N->getOperand(0).getValueType();
  612. EVT NVT = LHS.getValueType();
  613. SDLoc dl(N);
  614. // Do the arithmetic in the larger type.
  615. unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
  616. SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
  617. // Calculate the overflow flag: zero extend the arithmetic result from
  618. // the original type.
  619. SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
  620. // Overflowed if and only if this is not equal to Res.
  621. Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
  622. // Use the calculated overflow everywhere.
  623. ReplaceValueWith(SDValue(N, 1), Ofl);
  624. return Res;
  625. }
  626. SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
  627. // Promote the overflow bit trivially.
  628. if (ResNo == 1)
  629. return PromoteIntRes_Overflow(N);
  630. SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
  631. SDLoc DL(N);
  632. EVT SmallVT = LHS.getValueType();
  633. // To determine if the result overflowed in a larger type, we extend the
  634. // input to the larger type, do the multiply (checking if it overflows),
  635. // then also check the high bits of the result to see if overflow happened
  636. // there.
  637. if (N->getOpcode() == ISD::SMULO) {
  638. LHS = SExtPromotedInteger(LHS);
  639. RHS = SExtPromotedInteger(RHS);
  640. } else {
  641. LHS = ZExtPromotedInteger(LHS);
  642. RHS = ZExtPromotedInteger(RHS);
  643. }
  644. SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
  645. SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
  646. // Overflow occurred if it occurred in the larger type, or if the high part
  647. // of the result does not zero/sign-extend the low part. Check this second
  648. // possibility first.
  649. SDValue Overflow;
  650. if (N->getOpcode() == ISD::UMULO) {
  651. // Unsigned overflow occurred if the high part is non-zero.
  652. SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
  653. DAG.getIntPtrConstant(SmallVT.getSizeInBits(),
  654. DL));
  655. Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
  656. DAG.getConstant(0, DL, Hi.getValueType()),
  657. ISD::SETNE);
  658. } else {
  659. // Signed overflow occurred if the high part does not sign extend the low.
  660. SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
  661. Mul, DAG.getValueType(SmallVT));
  662. Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
  663. }
  664. // The only other way for overflow to occur is if the multiplication in the
  665. // larger type itself overflowed.
  666. Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
  667. SDValue(Mul.getNode(), 1));
  668. // Use the calculated overflow everywhere.
  669. ReplaceValueWith(SDValue(N, 1), Overflow);
  670. return Mul;
  671. }
  672. SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
  673. // Zero extend the input.
  674. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  675. SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
  676. return DAG.getNode(N->getOpcode(), SDLoc(N),
  677. LHS.getValueType(), LHS, RHS);
  678. }
  679. SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
  680. return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
  681. N->getValueType(0)));
  682. }
  683. SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
  684. SDValue Chain = N->getOperand(0); // Get the chain.
  685. SDValue Ptr = N->getOperand(1); // Get the pointer.
  686. EVT VT = N->getValueType(0);
  687. SDLoc dl(N);
  688. MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
  689. unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
  690. // The argument is passed as NumRegs registers of type RegVT.
  691. SmallVector<SDValue, 8> Parts(NumRegs);
  692. for (unsigned i = 0; i < NumRegs; ++i) {
  693. Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
  694. N->getConstantOperandVal(3));
  695. Chain = Parts[i].getValue(1);
  696. }
  697. // Handle endianness of the load.
  698. if (DAG.getDataLayout().isBigEndian())
  699. std::reverse(Parts.begin(), Parts.end());
  700. // Assemble the parts in the promoted type.
  701. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  702. SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
  703. for (unsigned i = 1; i < NumRegs; ++i) {
  704. SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
  705. // Shift it to the right position and "or" it in.
  706. Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
  707. DAG.getConstant(i * RegVT.getSizeInBits(), dl,
  708. TLI.getPointerTy(DAG.getDataLayout())));
  709. Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
  710. }
  711. // Modified the chain result - switch anything that used the old chain to
  712. // use the new one.
  713. ReplaceValueWith(SDValue(N, 1), Chain);
  714. return Res;
  715. }
  716. //===----------------------------------------------------------------------===//
  717. // Integer Operand Promotion
  718. //===----------------------------------------------------------------------===//
  719. /// PromoteIntegerOperand - This method is called when the specified operand of
  720. /// the specified node is found to need promotion. At this point, all of the
  721. /// result types of the node are known to be legal, but other operands of the
  722. /// node may need promotion or expansion as well as the specified one.
  723. bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
  724. DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG); dbgs() << "\n");
  725. SDValue Res = SDValue();
  726. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  727. return false;
  728. switch (N->getOpcode()) {
  729. default:
  730. #ifndef NDEBUG
  731. dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
  732. N->dump(&DAG); dbgs() << "\n";
  733. #endif
  734. llvm_unreachable("Do not know how to promote this operator's operand!");
  735. case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
  736. case ISD::ATOMIC_STORE:
  737. Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
  738. break;
  739. case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
  740. case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
  741. case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
  742. case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
  743. case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
  744. case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
  745. case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
  746. case ISD::CONVERT_RNDSAT:
  747. Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
  748. case ISD::INSERT_VECTOR_ELT:
  749. Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
  750. case ISD::SCALAR_TO_VECTOR:
  751. Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
  752. case ISD::VSELECT:
  753. case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
  754. case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
  755. case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
  756. case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
  757. case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
  758. case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
  759. OpNo); break;
  760. case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
  761. OpNo); break;
  762. case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
  763. OpNo); break;
  764. case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
  765. case ISD::FP16_TO_FP:
  766. case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
  767. case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
  768. case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
  769. case ISD::SHL:
  770. case ISD::SRA:
  771. case ISD::SRL:
  772. case ISD::ROTL:
  773. case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
  774. }
  775. // If the result is null, the sub-method took care of registering results etc.
  776. if (!Res.getNode()) return false;
  777. // If the result is N, the sub-method updated N in place. Tell the legalizer
  778. // core about this.
  779. if (Res.getNode() == N)
  780. return true;
  781. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  782. "Invalid operand expansion");
  783. ReplaceValueWith(SDValue(N, 0), Res);
  784. return false;
  785. }
  786. /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
  787. /// shared among BR_CC, SELECT_CC, and SETCC handlers.
  788. void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
  789. ISD::CondCode CCCode) {
  790. // We have to insert explicit sign or zero extends. Note that we could
  791. // insert sign extends for ALL conditions, but zero extend is cheaper on
  792. // many machines (an AND instead of two shifts), so prefer it.
  793. switch (CCCode) {
  794. default: llvm_unreachable("Unknown integer comparison!");
  795. case ISD::SETEQ:
  796. case ISD::SETNE: {
  797. SDValue OpL = GetPromotedInteger(NewLHS);
  798. SDValue OpR = GetPromotedInteger(NewRHS);
  799. // We would prefer to promote the comparison operand with sign extension,
  800. // if we find the operand is actually to truncate an AssertSext. With this
  801. // optimization, we can avoid inserting real truncate instruction, which
  802. // is redudant eventually.
  803. if (OpL->getOpcode() == ISD::AssertSext &&
  804. cast<VTSDNode>(OpL->getOperand(1))->getVT() == NewLHS.getValueType() &&
  805. OpR->getOpcode() == ISD::AssertSext &&
  806. cast<VTSDNode>(OpR->getOperand(1))->getVT() == NewRHS.getValueType()) {
  807. NewLHS = OpL;
  808. NewRHS = OpR;
  809. } else {
  810. NewLHS = ZExtPromotedInteger(NewLHS);
  811. NewRHS = ZExtPromotedInteger(NewRHS);
  812. }
  813. break;
  814. }
  815. case ISD::SETUGE:
  816. case ISD::SETUGT:
  817. case ISD::SETULE:
  818. case ISD::SETULT:
  819. // ALL of these operations will work if we either sign or zero extend
  820. // the operands (including the unsigned comparisons!). Zero extend is
  821. // usually a simpler/cheaper operation, so prefer it.
  822. NewLHS = ZExtPromotedInteger(NewLHS);
  823. NewRHS = ZExtPromotedInteger(NewRHS);
  824. break;
  825. case ISD::SETGE:
  826. case ISD::SETGT:
  827. case ISD::SETLT:
  828. case ISD::SETLE:
  829. NewLHS = SExtPromotedInteger(NewLHS);
  830. NewRHS = SExtPromotedInteger(NewRHS);
  831. break;
  832. }
  833. }
  834. SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
  835. SDValue Op = GetPromotedInteger(N->getOperand(0));
  836. return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
  837. }
  838. SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
  839. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  840. return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
  841. N->getChain(), N->getBasePtr(), Op2, N->getMemOperand(),
  842. N->getOrdering(), N->getSynchScope());
  843. }
  844. SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
  845. // This should only occur in unusual situations like bitcasting to an
  846. // x86_fp80, so just turn it into a store+load
  847. return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
  848. }
  849. SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
  850. assert(OpNo == 2 && "Don't know how to promote this operand!");
  851. SDValue LHS = N->getOperand(2);
  852. SDValue RHS = N->getOperand(3);
  853. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
  854. // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
  855. // legal types.
  856. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  857. N->getOperand(1), LHS, RHS, N->getOperand(4)),
  858. 0);
  859. }
  860. SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
  861. assert(OpNo == 1 && "only know how to promote condition");
  862. // Promote all the way up to the canonical SetCC type.
  863. SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
  864. // The chain (Op#0) and basic block destination (Op#2) are always legal types.
  865. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
  866. N->getOperand(2)), 0);
  867. }
  868. SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
  869. // Since the result type is legal, the operands must promote to it.
  870. EVT OVT = N->getOperand(0).getValueType();
  871. SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
  872. SDValue Hi = GetPromotedInteger(N->getOperand(1));
  873. assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
  874. SDLoc dl(N);
  875. Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
  876. DAG.getConstant(OVT.getSizeInBits(), dl,
  877. TLI.getPointerTy(DAG.getDataLayout())));
  878. return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
  879. }
  880. SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
  881. // The vector type is legal but the element type is not. This implies
  882. // that the vector is a power-of-two in length and that the element
  883. // type does not have a strange size (eg: it is not i1).
  884. EVT VecVT = N->getValueType(0);
  885. unsigned NumElts = VecVT.getVectorNumElements();
  886. assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
  887. "Legal vector of one illegal element?");
  888. // Promote the inserted value. The type does not need to match the
  889. // vector element type. Check that any extra bits introduced will be
  890. // truncated away.
  891. assert(N->getOperand(0).getValueType().getSizeInBits() >=
  892. N->getValueType(0).getVectorElementType().getSizeInBits() &&
  893. "Type of inserted value narrower than vector element type!");
  894. SmallVector<SDValue, 16> NewOps;
  895. for (unsigned i = 0; i < NumElts; ++i)
  896. NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
  897. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  898. }
  899. SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
  900. ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
  901. assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
  902. CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
  903. CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
  904. "can only promote integer arguments");
  905. SDValue InOp = GetPromotedInteger(N->getOperand(0));
  906. return DAG.getConvertRndSat(N->getValueType(0), SDLoc(N), InOp,
  907. N->getOperand(1), N->getOperand(2),
  908. N->getOperand(3), N->getOperand(4), CvtCode);
  909. }
  910. SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
  911. unsigned OpNo) {
  912. if (OpNo == 1) {
  913. // Promote the inserted value. This is valid because the type does not
  914. // have to match the vector element type.
  915. // Check that any extra bits introduced will be truncated away.
  916. assert(N->getOperand(1).getValueType().getSizeInBits() >=
  917. N->getValueType(0).getVectorElementType().getSizeInBits() &&
  918. "Type of inserted value narrower than vector element type!");
  919. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  920. GetPromotedInteger(N->getOperand(1)),
  921. N->getOperand(2)),
  922. 0);
  923. }
  924. assert(OpNo == 2 && "Different operand and result vector types?");
  925. // Promote the index.
  926. SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
  927. TLI.getVectorIdxTy(DAG.getDataLayout()));
  928. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  929. N->getOperand(1), Idx), 0);
  930. }
  931. SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
  932. // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
  933. // the operand in place.
  934. return SDValue(DAG.UpdateNodeOperands(N,
  935. GetPromotedInteger(N->getOperand(0))), 0);
  936. }
  937. SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
  938. assert(OpNo == 0 && "Only know how to promote the condition!");
  939. SDValue Cond = N->getOperand(0);
  940. EVT OpTy = N->getOperand(1).getValueType();
  941. // Promote all the way up to the canonical SetCC type.
  942. EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
  943. Cond = PromoteTargetBoolean(Cond, OpVT);
  944. return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
  945. N->getOperand(2)), 0);
  946. }
  947. SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
  948. assert(OpNo == 0 && "Don't know how to promote this operand!");
  949. SDValue LHS = N->getOperand(0);
  950. SDValue RHS = N->getOperand(1);
  951. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
  952. // The CC (#4) and the possible return values (#2 and #3) have legal types.
  953. return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
  954. N->getOperand(3), N->getOperand(4)), 0);
  955. }
  956. SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
  957. assert(OpNo == 0 && "Don't know how to promote this operand!");
  958. SDValue LHS = N->getOperand(0);
  959. SDValue RHS = N->getOperand(1);
  960. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
  961. // The CC (#2) is always legal.
  962. return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
  963. }
  964. SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
  965. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  966. ZExtPromotedInteger(N->getOperand(1))), 0);
  967. }
  968. SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
  969. SDValue Op = GetPromotedInteger(N->getOperand(0));
  970. SDLoc dl(N);
  971. Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
  972. return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
  973. Op, DAG.getValueType(N->getOperand(0).getValueType()));
  974. }
  975. SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
  976. return SDValue(DAG.UpdateNodeOperands(N,
  977. SExtPromotedInteger(N->getOperand(0))), 0);
  978. }
  979. SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
  980. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  981. SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
  982. SDLoc dl(N);
  983. SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
  984. // Truncate the value and store the result.
  985. return DAG.getTruncStore(Ch, dl, Val, Ptr,
  986. N->getMemoryVT(), N->getMemOperand());
  987. }
  988. SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N, unsigned OpNo){
  989. SDValue DataOp = N->getValue();
  990. EVT DataVT = DataOp.getValueType();
  991. SDValue Mask = N->getMask();
  992. EVT MaskVT = Mask.getValueType();
  993. SDLoc dl(N);
  994. bool TruncateStore = false;
  995. if (!TLI.isTypeLegal(DataVT)) {
  996. if (getTypeAction(DataVT) == TargetLowering::TypePromoteInteger) {
  997. DataOp = GetPromotedInteger(DataOp);
  998. if (!TLI.isTypeLegal(MaskVT))
  999. Mask = PromoteTargetBoolean(Mask, DataOp.getValueType());
  1000. TruncateStore = true;
  1001. }
  1002. else {
  1003. assert(getTypeAction(DataVT) == TargetLowering::TypeWidenVector &&
  1004. "Unexpected data legalization in MSTORE");
  1005. DataOp = GetWidenedVector(DataOp);
  1006. if (getTypeAction(MaskVT) == TargetLowering::TypeWidenVector)
  1007. Mask = GetWidenedVector(Mask);
  1008. else {
  1009. EVT BoolVT = getSetCCResultType(DataOp.getValueType());
  1010. // We can't use ModifyToType() because we should fill the mask with
  1011. // zeroes
  1012. unsigned WidenNumElts = BoolVT.getVectorNumElements();
  1013. unsigned MaskNumElts = MaskVT.getVectorNumElements();
  1014. unsigned NumConcat = WidenNumElts / MaskNumElts;
  1015. SmallVector<SDValue, 16> Ops(NumConcat);
  1016. SDValue ZeroVal = DAG.getConstant(0, dl, MaskVT);
  1017. Ops[0] = Mask;
  1018. for (unsigned i = 1; i != NumConcat; ++i)
  1019. Ops[i] = ZeroVal;
  1020. Mask = DAG.getNode(ISD::CONCAT_VECTORS, dl, BoolVT, Ops);
  1021. }
  1022. }
  1023. }
  1024. else
  1025. Mask = PromoteTargetBoolean(N->getMask(), DataOp.getValueType());
  1026. return DAG.getMaskedStore(N->getChain(), dl, DataOp, N->getBasePtr(), Mask,
  1027. N->getMemoryVT(), N->getMemOperand(),
  1028. TruncateStore);
  1029. }
  1030. SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N, unsigned OpNo){
  1031. assert(OpNo == 2 && "Only know how to promote the mask!");
  1032. EVT DataVT = N->getValueType(0);
  1033. SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
  1034. SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
  1035. NewOps[OpNo] = Mask;
  1036. return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
  1037. }
  1038. SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
  1039. SDValue Op = GetPromotedInteger(N->getOperand(0));
  1040. return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
  1041. }
  1042. SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
  1043. return SDValue(DAG.UpdateNodeOperands(N,
  1044. ZExtPromotedInteger(N->getOperand(0))), 0);
  1045. }
  1046. SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
  1047. SDLoc dl(N);
  1048. SDValue Op = GetPromotedInteger(N->getOperand(0));
  1049. Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
  1050. return DAG.getZeroExtendInReg(Op, dl,
  1051. N->getOperand(0).getValueType().getScalarType());
  1052. }
  1053. //===----------------------------------------------------------------------===//
  1054. // Integer Result Expansion
  1055. //===----------------------------------------------------------------------===//
  1056. /// ExpandIntegerResult - This method is called when the specified result of the
  1057. /// specified node is found to need expansion. At this point, the node may also
  1058. /// have invalid operands or may have other results that need promotion, we just
  1059. /// know that (at least) one result needs expansion.
  1060. void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
  1061. DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG); dbgs() << "\n");
  1062. SDValue Lo, Hi;
  1063. Lo = Hi = SDValue();
  1064. // See if the target wants to custom expand this node.
  1065. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  1066. return;
  1067. switch (N->getOpcode()) {
  1068. default:
  1069. #ifndef NDEBUG
  1070. dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
  1071. N->dump(&DAG); dbgs() << "\n";
  1072. #endif
  1073. llvm_unreachable("Do not know how to expand the result of this operator!");
  1074. case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
  1075. case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
  1076. case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
  1077. case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
  1078. case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
  1079. case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
  1080. case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
  1081. case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
  1082. case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
  1083. case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
  1084. case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
  1085. case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
  1086. case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
  1087. case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
  1088. case ISD::CTLZ_ZERO_UNDEF:
  1089. case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
  1090. case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
  1091. case ISD::CTTZ_ZERO_UNDEF:
  1092. case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
  1093. case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
  1094. case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
  1095. case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
  1096. case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
  1097. case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
  1098. case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
  1099. case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
  1100. case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
  1101. case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
  1102. case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
  1103. case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
  1104. case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
  1105. case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
  1106. case ISD::ATOMIC_LOAD_ADD:
  1107. case ISD::ATOMIC_LOAD_SUB:
  1108. case ISD::ATOMIC_LOAD_AND:
  1109. case ISD::ATOMIC_LOAD_OR:
  1110. case ISD::ATOMIC_LOAD_XOR:
  1111. case ISD::ATOMIC_LOAD_NAND:
  1112. case ISD::ATOMIC_LOAD_MIN:
  1113. case ISD::ATOMIC_LOAD_MAX:
  1114. case ISD::ATOMIC_LOAD_UMIN:
  1115. case ISD::ATOMIC_LOAD_UMAX:
  1116. case ISD::ATOMIC_SWAP:
  1117. case ISD::ATOMIC_CMP_SWAP: {
  1118. std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
  1119. SplitInteger(Tmp.first, Lo, Hi);
  1120. ReplaceValueWith(SDValue(N, 1), Tmp.second);
  1121. break;
  1122. }
  1123. case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
  1124. AtomicSDNode *AN = cast<AtomicSDNode>(N);
  1125. SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
  1126. SDValue Tmp = DAG.getAtomicCmpSwap(
  1127. ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
  1128. N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
  1129. AN->getMemOperand(), AN->getSuccessOrdering(), AN->getFailureOrdering(),
  1130. AN->getSynchScope());
  1131. // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
  1132. // success simply by comparing the loaded value against the ingoing
  1133. // comparison.
  1134. SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
  1135. N->getOperand(2), ISD::SETEQ);
  1136. SplitInteger(Tmp, Lo, Hi);
  1137. ReplaceValueWith(SDValue(N, 1), Success);
  1138. ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
  1139. break;
  1140. }
  1141. case ISD::AND:
  1142. case ISD::OR:
  1143. case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
  1144. case ISD::ADD:
  1145. case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
  1146. case ISD::ADDC:
  1147. case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
  1148. case ISD::ADDE:
  1149. case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
  1150. case ISD::SHL:
  1151. case ISD::SRA:
  1152. case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
  1153. case ISD::SADDO:
  1154. case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
  1155. case ISD::UADDO:
  1156. case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
  1157. case ISD::UMULO:
  1158. case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
  1159. }
  1160. // If Lo/Hi is null, the sub-method took care of registering results etc.
  1161. if (Lo.getNode())
  1162. SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
  1163. }
  1164. /// Lower an atomic node to the appropriate builtin call.
  1165. std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
  1166. unsigned Opc = Node->getOpcode();
  1167. MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
  1168. RTLIB::Libcall LC = RTLIB::getATOMIC(Opc, VT);
  1169. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
  1170. return ExpandChainLibCall(LC, Node, false);
  1171. }
  1172. /// N is a shift by a value that needs to be expanded,
  1173. /// and the shift amount is a constant 'Amt'. Expand the operation.
  1174. void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
  1175. SDValue &Lo, SDValue &Hi) {
  1176. SDLoc DL(N);
  1177. // Expand the incoming operand to be shifted, so that we have its parts
  1178. SDValue InL, InH;
  1179. GetExpandedInteger(N->getOperand(0), InL, InH);
  1180. // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
  1181. // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
  1182. if (!Amt) {
  1183. Lo = InL;
  1184. Hi = InH;
  1185. return;
  1186. }
  1187. EVT NVT = InL.getValueType();
  1188. unsigned VTBits = N->getValueType(0).getSizeInBits();
  1189. unsigned NVTBits = NVT.getSizeInBits();
  1190. EVT ShTy = N->getOperand(1).getValueType();
  1191. if (N->getOpcode() == ISD::SHL) {
  1192. if (Amt.ugt(VTBits)) {
  1193. Lo = Hi = DAG.getConstant(0, DL, NVT);
  1194. } else if (Amt.ugt(NVTBits)) {
  1195. Lo = DAG.getConstant(0, DL, NVT);
  1196. Hi = DAG.getNode(ISD::SHL, DL,
  1197. NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
  1198. } else if (Amt == NVTBits) {
  1199. Lo = DAG.getConstant(0, DL, NVT);
  1200. Hi = InL;
  1201. } else if (Amt == 1 &&
  1202. TLI.isOperationLegalOrCustom(ISD::ADDC,
  1203. TLI.getTypeToExpandTo(*DAG.getContext(), NVT))) {
  1204. // Emit this X << 1 as X+X.
  1205. SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
  1206. SDValue LoOps[2] = { InL, InL };
  1207. Lo = DAG.getNode(ISD::ADDC, DL, VTList, LoOps);
  1208. SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
  1209. Hi = DAG.getNode(ISD::ADDE, DL, VTList, HiOps);
  1210. } else {
  1211. Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
  1212. Hi = DAG.getNode(ISD::OR, DL, NVT,
  1213. DAG.getNode(ISD::SHL, DL, NVT, InH,
  1214. DAG.getConstant(Amt, DL, ShTy)),
  1215. DAG.getNode(ISD::SRL, DL, NVT, InL,
  1216. DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
  1217. }
  1218. return;
  1219. }
  1220. if (N->getOpcode() == ISD::SRL) {
  1221. if (Amt.ugt(VTBits)) {
  1222. Lo = Hi = DAG.getConstant(0, DL, NVT);
  1223. } else if (Amt.ugt(NVTBits)) {
  1224. Lo = DAG.getNode(ISD::SRL, DL,
  1225. NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
  1226. Hi = DAG.getConstant(0, DL, NVT);
  1227. } else if (Amt == NVTBits) {
  1228. Lo = InH;
  1229. Hi = DAG.getConstant(0, DL, NVT);
  1230. } else {
  1231. Lo = DAG.getNode(ISD::OR, DL, NVT,
  1232. DAG.getNode(ISD::SRL, DL, NVT, InL,
  1233. DAG.getConstant(Amt, DL, ShTy)),
  1234. DAG.getNode(ISD::SHL, DL, NVT, InH,
  1235. DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
  1236. Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
  1237. }
  1238. return;
  1239. }
  1240. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  1241. if (Amt.ugt(VTBits)) {
  1242. Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1243. DAG.getConstant(NVTBits - 1, DL, ShTy));
  1244. } else if (Amt.ugt(NVTBits)) {
  1245. Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1246. DAG.getConstant(Amt - NVTBits, DL, ShTy));
  1247. Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1248. DAG.getConstant(NVTBits - 1, DL, ShTy));
  1249. } else if (Amt == NVTBits) {
  1250. Lo = InH;
  1251. Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
  1252. DAG.getConstant(NVTBits - 1, DL, ShTy));
  1253. } else {
  1254. Lo = DAG.getNode(ISD::OR, DL, NVT,
  1255. DAG.getNode(ISD::SRL, DL, NVT, InL,
  1256. DAG.getConstant(Amt, DL, ShTy)),
  1257. DAG.getNode(ISD::SHL, DL, NVT, InH,
  1258. DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
  1259. Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
  1260. }
  1261. }
  1262. /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
  1263. /// this shift based on knowledge of the high bit of the shift amount. If we
  1264. /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
  1265. /// shift amount.
  1266. bool DAGTypeLegalizer::
  1267. ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
  1268. SDValue Amt = N->getOperand(1);
  1269. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1270. EVT ShTy = Amt.getValueType();
  1271. unsigned ShBits = ShTy.getScalarType().getSizeInBits();
  1272. unsigned NVTBits = NVT.getScalarType().getSizeInBits();
  1273. assert(isPowerOf2_32(NVTBits) &&
  1274. "Expanded integer type size not a power of two!");
  1275. SDLoc dl(N);
  1276. APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
  1277. APInt KnownZero, KnownOne;
  1278. DAG.computeKnownBits(N->getOperand(1), KnownZero, KnownOne);
  1279. // If we don't know anything about the high bits, exit.
  1280. if (((KnownZero|KnownOne) & HighBitMask) == 0)
  1281. return false;
  1282. // Get the incoming operand to be shifted.
  1283. SDValue InL, InH;
  1284. GetExpandedInteger(N->getOperand(0), InL, InH);
  1285. // If we know that any of the high bits of the shift amount are one, then we
  1286. // can do this as a couple of simple shifts.
  1287. if (KnownOne.intersects(HighBitMask)) {
  1288. // Mask out the high bit, which we know is set.
  1289. Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
  1290. DAG.getConstant(~HighBitMask, dl, ShTy));
  1291. switch (N->getOpcode()) {
  1292. default: llvm_unreachable("Unknown shift");
  1293. case ISD::SHL:
  1294. Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
  1295. Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
  1296. return true;
  1297. case ISD::SRL:
  1298. Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
  1299. Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
  1300. return true;
  1301. case ISD::SRA:
  1302. Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
  1303. DAG.getConstant(NVTBits - 1, dl, ShTy));
  1304. Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
  1305. return true;
  1306. }
  1307. }
  1308. // If we know that all of the high bits of the shift amount are zero, then we
  1309. // can do this as a couple of simple shifts.
  1310. if ((KnownZero & HighBitMask) == HighBitMask) {
  1311. // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
  1312. // shift if x is zero. We can use XOR here because x is known to be smaller
  1313. // than 32.
  1314. SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
  1315. DAG.getConstant(NVTBits - 1, dl, ShTy));
  1316. unsigned Op1, Op2;
  1317. switch (N->getOpcode()) {
  1318. default: llvm_unreachable("Unknown shift");
  1319. case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
  1320. case ISD::SRL:
  1321. case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
  1322. }
  1323. // When shifting right the arithmetic for Lo and Hi is swapped.
  1324. if (N->getOpcode() != ISD::SHL)
  1325. std::swap(InL, InH);
  1326. // Use a little trick to get the bits that move from Lo to Hi. First
  1327. // shift by one bit.
  1328. SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
  1329. // Then compute the remaining shift with amount-1.
  1330. SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
  1331. Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
  1332. Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
  1333. if (N->getOpcode() != ISD::SHL)
  1334. std::swap(Hi, Lo);
  1335. return true;
  1336. }
  1337. return false;
  1338. }
  1339. /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
  1340. /// of any size.
  1341. bool DAGTypeLegalizer::
  1342. ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
  1343. SDValue Amt = N->getOperand(1);
  1344. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1345. EVT ShTy = Amt.getValueType();
  1346. unsigned NVTBits = NVT.getSizeInBits();
  1347. assert(isPowerOf2_32(NVTBits) &&
  1348. "Expanded integer type size not a power of two!");
  1349. SDLoc dl(N);
  1350. // Get the incoming operand to be shifted.
  1351. SDValue InL, InH;
  1352. GetExpandedInteger(N->getOperand(0), InL, InH);
  1353. SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
  1354. SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
  1355. SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
  1356. SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
  1357. Amt, NVBitsNode, ISD::SETULT);
  1358. SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
  1359. Amt, DAG.getConstant(0, dl, ShTy),
  1360. ISD::SETEQ);
  1361. SDValue LoS, HiS, LoL, HiL;
  1362. switch (N->getOpcode()) {
  1363. default: llvm_unreachable("Unknown shift");
  1364. case ISD::SHL:
  1365. // Short: ShAmt < NVTBits
  1366. LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
  1367. HiS = DAG.getNode(ISD::OR, dl, NVT,
  1368. DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
  1369. DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
  1370. // Long: ShAmt >= NVTBits
  1371. LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
  1372. HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
  1373. Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
  1374. Hi = DAG.getSelect(dl, NVT, isZero, InH,
  1375. DAG.getSelect(dl, NVT, isShort, HiS, HiL));
  1376. return true;
  1377. case ISD::SRL:
  1378. // Short: ShAmt < NVTBits
  1379. HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
  1380. LoS = DAG.getNode(ISD::OR, dl, NVT,
  1381. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
  1382. // FIXME: If Amt is zero, the following shift generates an undefined result
  1383. // on some architectures.
  1384. DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
  1385. // Long: ShAmt >= NVTBits
  1386. HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
  1387. LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
  1388. Lo = DAG.getSelect(dl, NVT, isZero, InL,
  1389. DAG.getSelect(dl, NVT, isShort, LoS, LoL));
  1390. Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
  1391. return true;
  1392. case ISD::SRA:
  1393. // Short: ShAmt < NVTBits
  1394. HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
  1395. LoS = DAG.getNode(ISD::OR, dl, NVT,
  1396. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
  1397. DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
  1398. // Long: ShAmt >= NVTBits
  1399. HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
  1400. DAG.getConstant(NVTBits - 1, dl, ShTy));
  1401. LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
  1402. Lo = DAG.getSelect(dl, NVT, isZero, InL,
  1403. DAG.getSelect(dl, NVT, isShort, LoS, LoL));
  1404. Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
  1405. return true;
  1406. }
  1407. }
  1408. void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
  1409. SDValue &Lo, SDValue &Hi) {
  1410. SDLoc dl(N);
  1411. // Expand the subcomponents.
  1412. SDValue LHSL, LHSH, RHSL, RHSH;
  1413. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1414. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1415. EVT NVT = LHSL.getValueType();
  1416. SDValue LoOps[2] = { LHSL, RHSL };
  1417. SDValue HiOps[3] = { LHSH, RHSH };
  1418. // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
  1419. // them. TODO: Teach operation legalization how to expand unsupported
  1420. // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
  1421. // a carry of type MVT::Glue, but there doesn't seem to be any way to
  1422. // generate a value of this type in the expanded code sequence.
  1423. bool hasCarry =
  1424. TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
  1425. ISD::ADDC : ISD::SUBC,
  1426. TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
  1427. if (hasCarry) {
  1428. SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
  1429. if (N->getOpcode() == ISD::ADD) {
  1430. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
  1431. HiOps[2] = Lo.getValue(1);
  1432. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
  1433. } else {
  1434. Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
  1435. HiOps[2] = Lo.getValue(1);
  1436. Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
  1437. }
  1438. return;
  1439. }
  1440. bool hasOVF =
  1441. TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
  1442. ISD::UADDO : ISD::USUBO,
  1443. TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
  1444. if (hasOVF) {
  1445. SDVTList VTList = DAG.getVTList(NVT, NVT);
  1446. TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
  1447. int RevOpc;
  1448. if (N->getOpcode() == ISD::ADD) {
  1449. RevOpc = ISD::SUB;
  1450. Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
  1451. Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
  1452. } else {
  1453. RevOpc = ISD::ADD;
  1454. Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
  1455. Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
  1456. }
  1457. SDValue OVF = Lo.getValue(1);
  1458. switch (BoolType) {
  1459. case TargetLoweringBase::UndefinedBooleanContent:
  1460. OVF = DAG.getNode(ISD::AND, dl, NVT, DAG.getConstant(1, dl, NVT), OVF);
  1461. // Fallthrough
  1462. case TargetLoweringBase::ZeroOrOneBooleanContent:
  1463. Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
  1464. break;
  1465. case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
  1466. Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
  1467. }
  1468. return;
  1469. }
  1470. if (N->getOpcode() == ISD::ADD) {
  1471. Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
  1472. Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
  1473. SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
  1474. ISD::SETULT);
  1475. SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
  1476. DAG.getConstant(1, dl, NVT),
  1477. DAG.getConstant(0, dl, NVT));
  1478. SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
  1479. ISD::SETULT);
  1480. SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
  1481. DAG.getConstant(1, dl, NVT), Carry1);
  1482. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
  1483. } else {
  1484. Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
  1485. Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
  1486. SDValue Cmp =
  1487. DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
  1488. LoOps[0], LoOps[1], ISD::SETULT);
  1489. SDValue Borrow = DAG.getSelect(dl, NVT, Cmp,
  1490. DAG.getConstant(1, dl, NVT),
  1491. DAG.getConstant(0, dl, NVT));
  1492. Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
  1493. }
  1494. }
  1495. void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
  1496. SDValue &Lo, SDValue &Hi) {
  1497. // Expand the subcomponents.
  1498. SDValue LHSL, LHSH, RHSL, RHSH;
  1499. SDLoc dl(N);
  1500. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1501. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1502. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
  1503. SDValue LoOps[2] = { LHSL, RHSL };
  1504. SDValue HiOps[3] = { LHSH, RHSH };
  1505. if (N->getOpcode() == ISD::ADDC) {
  1506. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
  1507. HiOps[2] = Lo.getValue(1);
  1508. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
  1509. } else {
  1510. Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
  1511. HiOps[2] = Lo.getValue(1);
  1512. Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
  1513. }
  1514. // Legalized the flag result - switch anything that used the old flag to
  1515. // use the new one.
  1516. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  1517. }
  1518. void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
  1519. SDValue &Lo, SDValue &Hi) {
  1520. // Expand the subcomponents.
  1521. SDValue LHSL, LHSH, RHSL, RHSH;
  1522. SDLoc dl(N);
  1523. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1524. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1525. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
  1526. SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
  1527. SDValue HiOps[3] = { LHSH, RHSH };
  1528. Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
  1529. HiOps[2] = Lo.getValue(1);
  1530. Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
  1531. // Legalized the flag result - switch anything that used the old flag to
  1532. // use the new one.
  1533. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  1534. }
  1535. void DAGTypeLegalizer::ExpandIntRes_MERGE_VALUES(SDNode *N, unsigned ResNo,
  1536. SDValue &Lo, SDValue &Hi) {
  1537. SDValue Res = DisintegrateMERGE_VALUES(N, ResNo);
  1538. SplitInteger(Res, Lo, Hi);
  1539. }
  1540. void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
  1541. SDValue &Lo, SDValue &Hi) {
  1542. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1543. SDLoc dl(N);
  1544. SDValue Op = N->getOperand(0);
  1545. if (Op.getValueType().bitsLE(NVT)) {
  1546. // The low part is any extension of the input (which degenerates to a copy).
  1547. Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
  1548. Hi = DAG.getUNDEF(NVT); // The high part is undefined.
  1549. } else {
  1550. // For example, extension of an i48 to an i64. The operand type necessarily
  1551. // promotes to the result type, so will end up being expanded too.
  1552. assert(getTypeAction(Op.getValueType()) ==
  1553. TargetLowering::TypePromoteInteger &&
  1554. "Only know how to promote this result!");
  1555. SDValue Res = GetPromotedInteger(Op);
  1556. assert(Res.getValueType() == N->getValueType(0) &&
  1557. "Operand over promoted?");
  1558. // Split the promoted operand. This will simplify when it is expanded.
  1559. SplitInteger(Res, Lo, Hi);
  1560. }
  1561. }
  1562. void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
  1563. SDValue &Lo, SDValue &Hi) {
  1564. SDLoc dl(N);
  1565. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1566. EVT NVT = Lo.getValueType();
  1567. EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  1568. unsigned NVTBits = NVT.getSizeInBits();
  1569. unsigned EVTBits = EVT.getSizeInBits();
  1570. if (NVTBits < EVTBits) {
  1571. Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
  1572. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  1573. EVTBits - NVTBits)));
  1574. } else {
  1575. Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
  1576. // The high part replicates the sign bit of Lo, make it explicit.
  1577. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  1578. DAG.getConstant(NVTBits - 1, dl,
  1579. TLI.getPointerTy(DAG.getDataLayout())));
  1580. }
  1581. }
  1582. void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
  1583. SDValue &Lo, SDValue &Hi) {
  1584. SDLoc dl(N);
  1585. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1586. EVT NVT = Lo.getValueType();
  1587. EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  1588. unsigned NVTBits = NVT.getSizeInBits();
  1589. unsigned EVTBits = EVT.getSizeInBits();
  1590. if (NVTBits < EVTBits) {
  1591. Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
  1592. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  1593. EVTBits - NVTBits)));
  1594. } else {
  1595. Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
  1596. // The high part must be zero, make it explicit.
  1597. Hi = DAG.getConstant(0, dl, NVT);
  1598. }
  1599. }
  1600. void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
  1601. SDValue &Lo, SDValue &Hi) {
  1602. SDLoc dl(N);
  1603. GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
  1604. Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
  1605. Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
  1606. }
  1607. void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
  1608. SDValue &Lo, SDValue &Hi) {
  1609. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1610. unsigned NBitWidth = NVT.getSizeInBits();
  1611. auto Constant = cast<ConstantSDNode>(N);
  1612. const APInt &Cst = Constant->getAPIntValue();
  1613. bool IsTarget = Constant->isTargetOpcode();
  1614. bool IsOpaque = Constant->isOpaque();
  1615. SDLoc dl(N);
  1616. Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
  1617. Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
  1618. IsOpaque);
  1619. }
  1620. void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
  1621. SDValue &Lo, SDValue &Hi) {
  1622. SDLoc dl(N);
  1623. // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
  1624. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1625. EVT NVT = Lo.getValueType();
  1626. SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
  1627. DAG.getConstant(0, dl, NVT), ISD::SETNE);
  1628. SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
  1629. SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
  1630. Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
  1631. DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
  1632. DAG.getConstant(NVT.getSizeInBits(), dl,
  1633. NVT)));
  1634. Hi = DAG.getConstant(0, dl, NVT);
  1635. }
  1636. void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
  1637. SDValue &Lo, SDValue &Hi) {
  1638. SDLoc dl(N);
  1639. // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
  1640. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1641. EVT NVT = Lo.getValueType();
  1642. Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
  1643. DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
  1644. Hi = DAG.getConstant(0, dl, NVT);
  1645. }
  1646. void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
  1647. SDValue &Lo, SDValue &Hi) {
  1648. SDLoc dl(N);
  1649. // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
  1650. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1651. EVT NVT = Lo.getValueType();
  1652. SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
  1653. DAG.getConstant(0, dl, NVT), ISD::SETNE);
  1654. SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
  1655. SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
  1656. Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
  1657. DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
  1658. DAG.getConstant(NVT.getSizeInBits(), dl,
  1659. NVT)));
  1660. Hi = DAG.getConstant(0, dl, NVT);
  1661. }
  1662. void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
  1663. SDValue &Hi) {
  1664. SDLoc dl(N);
  1665. EVT VT = N->getValueType(0);
  1666. SDValue Op = N->getOperand(0);
  1667. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
  1668. Op = GetPromotedFloat(Op);
  1669. RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
  1670. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
  1671. SplitInteger(TLI.makeLibCall(DAG, LC, VT, &Op, 1, true/*irrelevant*/,
  1672. dl).first,
  1673. Lo, Hi);
  1674. }
  1675. void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
  1676. SDValue &Hi) {
  1677. SDLoc dl(N);
  1678. EVT VT = N->getValueType(0);
  1679. SDValue Op = N->getOperand(0);
  1680. if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
  1681. Op = GetPromotedFloat(Op);
  1682. RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
  1683. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
  1684. SplitInteger(TLI.makeLibCall(DAG, LC, VT, &Op, 1, false/*irrelevant*/,
  1685. dl).first,
  1686. Lo, Hi);
  1687. }
  1688. void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
  1689. SDValue &Lo, SDValue &Hi) {
  1690. if (ISD::isNormalLoad(N)) {
  1691. ExpandRes_NormalLoad(N, Lo, Hi);
  1692. return;
  1693. }
  1694. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  1695. EVT VT = N->getValueType(0);
  1696. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1697. SDValue Ch = N->getChain();
  1698. SDValue Ptr = N->getBasePtr();
  1699. ISD::LoadExtType ExtType = N->getExtensionType();
  1700. unsigned Alignment = N->getAlignment();
  1701. bool isVolatile = N->isVolatile();
  1702. bool isNonTemporal = N->isNonTemporal();
  1703. bool isInvariant = N->isInvariant();
  1704. AAMDNodes AAInfo = N->getAAInfo();
  1705. SDLoc dl(N);
  1706. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1707. if (N->getMemoryVT().bitsLE(NVT)) {
  1708. EVT MemVT = N->getMemoryVT();
  1709. Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
  1710. MemVT, isVolatile, isNonTemporal, isInvariant,
  1711. Alignment, AAInfo);
  1712. // Remember the chain.
  1713. Ch = Lo.getValue(1);
  1714. if (ExtType == ISD::SEXTLOAD) {
  1715. // The high part is obtained by SRA'ing all but one of the bits of the
  1716. // lo part.
  1717. unsigned LoSize = Lo.getValueType().getSizeInBits();
  1718. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  1719. DAG.getConstant(LoSize - 1, dl,
  1720. TLI.getPointerTy(DAG.getDataLayout())));
  1721. } else if (ExtType == ISD::ZEXTLOAD) {
  1722. // The high part is just a zero.
  1723. Hi = DAG.getConstant(0, dl, NVT);
  1724. } else {
  1725. assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
  1726. // The high part is undefined.
  1727. Hi = DAG.getUNDEF(NVT);
  1728. }
  1729. } else if (DAG.getDataLayout().isLittleEndian()) {
  1730. // Little-endian - low bits are at low addresses.
  1731. Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(),
  1732. isVolatile, isNonTemporal, isInvariant, Alignment,
  1733. AAInfo);
  1734. unsigned ExcessBits =
  1735. N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
  1736. EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
  1737. // Increment the pointer to the other half.
  1738. unsigned IncrementSize = NVT.getSizeInBits()/8;
  1739. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  1740. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  1741. Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
  1742. N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
  1743. isVolatile, isNonTemporal, isInvariant,
  1744. MinAlign(Alignment, IncrementSize), AAInfo);
  1745. // Build a factor node to remember that this load is independent of the
  1746. // other one.
  1747. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  1748. Hi.getValue(1));
  1749. } else {
  1750. // Big-endian - high bits are at low addresses. Favor aligned loads at
  1751. // the cost of some bit-fiddling.
  1752. EVT MemVT = N->getMemoryVT();
  1753. unsigned EBytes = MemVT.getStoreSize();
  1754. unsigned IncrementSize = NVT.getSizeInBits()/8;
  1755. unsigned ExcessBits = (EBytes - IncrementSize)*8;
  1756. // Load both the high bits and maybe some of the low bits.
  1757. Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
  1758. EVT::getIntegerVT(*DAG.getContext(),
  1759. MemVT.getSizeInBits() - ExcessBits),
  1760. isVolatile, isNonTemporal, isInvariant, Alignment,
  1761. AAInfo);
  1762. // Increment the pointer to the other half.
  1763. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  1764. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  1765. // Load the rest of the low bits.
  1766. Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
  1767. N->getPointerInfo().getWithOffset(IncrementSize),
  1768. EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
  1769. isVolatile, isNonTemporal, isInvariant,
  1770. MinAlign(Alignment, IncrementSize), AAInfo);
  1771. // Build a factor node to remember that this load is independent of the
  1772. // other one.
  1773. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  1774. Hi.getValue(1));
  1775. if (ExcessBits < NVT.getSizeInBits()) {
  1776. // Transfer low bits from the bottom of Hi to the top of Lo.
  1777. Lo = DAG.getNode(
  1778. ISD::OR, dl, NVT, Lo,
  1779. DAG.getNode(ISD::SHL, dl, NVT, Hi,
  1780. DAG.getConstant(ExcessBits, dl,
  1781. TLI.getPointerTy(DAG.getDataLayout()))));
  1782. // Move high bits to the right position in Hi.
  1783. Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
  1784. Hi,
  1785. DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
  1786. TLI.getPointerTy(DAG.getDataLayout())));
  1787. }
  1788. }
  1789. // Legalized the chain result - switch anything that used the old chain to
  1790. // use the new one.
  1791. ReplaceValueWith(SDValue(N, 1), Ch);
  1792. }
  1793. void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
  1794. SDValue &Lo, SDValue &Hi) {
  1795. SDLoc dl(N);
  1796. SDValue LL, LH, RL, RH;
  1797. GetExpandedInteger(N->getOperand(0), LL, LH);
  1798. GetExpandedInteger(N->getOperand(1), RL, RH);
  1799. Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
  1800. Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
  1801. }
  1802. void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
  1803. SDValue &Lo, SDValue &Hi) {
  1804. EVT VT = N->getValueType(0);
  1805. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1806. SDLoc dl(N);
  1807. SDValue LL, LH, RL, RH;
  1808. GetExpandedInteger(N->getOperand(0), LL, LH);
  1809. GetExpandedInteger(N->getOperand(1), RL, RH);
  1810. if (TLI.expandMUL(N, Lo, Hi, NVT, DAG, LL, LH, RL, RH))
  1811. return;
  1812. // If nothing else, we can make a libcall.
  1813. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1814. if (VT == MVT::i16)
  1815. LC = RTLIB::MUL_I16;
  1816. else if (VT == MVT::i32)
  1817. LC = RTLIB::MUL_I32;
  1818. else if (VT == MVT::i64)
  1819. LC = RTLIB::MUL_I64;
  1820. else if (VT == MVT::i128)
  1821. LC = RTLIB::MUL_I128;
  1822. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
  1823. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1824. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true/*irrelevant*/,
  1825. dl).first,
  1826. Lo, Hi);
  1827. }
  1828. void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
  1829. SDValue &Lo, SDValue &Hi) {
  1830. SDValue LHS = Node->getOperand(0);
  1831. SDValue RHS = Node->getOperand(1);
  1832. SDLoc dl(Node);
  1833. // Expand the result by simply replacing it with the equivalent
  1834. // non-overflow-checking operation.
  1835. SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
  1836. ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
  1837. LHS, RHS);
  1838. SplitInteger(Sum, Lo, Hi);
  1839. // Compute the overflow.
  1840. //
  1841. // LHSSign -> LHS >= 0
  1842. // RHSSign -> RHS >= 0
  1843. // SumSign -> Sum >= 0
  1844. //
  1845. // Add:
  1846. // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
  1847. // Sub:
  1848. // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
  1849. //
  1850. EVT OType = Node->getValueType(1);
  1851. SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
  1852. SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
  1853. SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
  1854. SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
  1855. Node->getOpcode() == ISD::SADDO ?
  1856. ISD::SETEQ : ISD::SETNE);
  1857. SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
  1858. SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
  1859. SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
  1860. // Use the calculated overflow everywhere.
  1861. ReplaceValueWith(SDValue(Node, 1), Cmp);
  1862. }
  1863. void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
  1864. SDValue &Lo, SDValue &Hi) {
  1865. EVT VT = N->getValueType(0);
  1866. SDLoc dl(N);
  1867. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1868. if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
  1869. SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  1870. SplitInteger(Res.getValue(0), Lo, Hi);
  1871. return;
  1872. }
  1873. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1874. if (VT == MVT::i16)
  1875. LC = RTLIB::SDIV_I16;
  1876. else if (VT == MVT::i32)
  1877. LC = RTLIB::SDIV_I32;
  1878. else if (VT == MVT::i64)
  1879. LC = RTLIB::SDIV_I64;
  1880. else if (VT == MVT::i128)
  1881. LC = RTLIB::SDIV_I128;
  1882. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
  1883. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true, dl).first, Lo, Hi);
  1884. }
  1885. void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
  1886. SDValue &Lo, SDValue &Hi) {
  1887. EVT VT = N->getValueType(0);
  1888. SDLoc dl(N);
  1889. // If we can emit an efficient shift operation, do so now. Check to see if
  1890. // the RHS is a constant.
  1891. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
  1892. return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
  1893. // If we can determine that the high bit of the shift is zero or one, even if
  1894. // the low bits are variable, emit this shift in an optimized form.
  1895. if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
  1896. return;
  1897. // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
  1898. unsigned PartsOpc;
  1899. if (N->getOpcode() == ISD::SHL) {
  1900. PartsOpc = ISD::SHL_PARTS;
  1901. } else if (N->getOpcode() == ISD::SRL) {
  1902. PartsOpc = ISD::SRL_PARTS;
  1903. } else {
  1904. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  1905. PartsOpc = ISD::SRA_PARTS;
  1906. }
  1907. // Next check to see if the target supports this SHL_PARTS operation or if it
  1908. // will custom expand it.
  1909. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  1910. TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
  1911. if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
  1912. Action == TargetLowering::Custom) {
  1913. // Expand the subcomponents.
  1914. SDValue LHSL, LHSH;
  1915. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1916. EVT VT = LHSL.getValueType();
  1917. // If the shift amount operand is coming from a vector legalization it may
  1918. // have an illegal type. Fix that first by casting the operand, otherwise
  1919. // the new SHL_PARTS operation would need further legalization.
  1920. SDValue ShiftOp = N->getOperand(1);
  1921. EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
  1922. assert(ShiftTy.getScalarType().getSizeInBits() >=
  1923. Log2_32_Ceil(VT.getScalarType().getSizeInBits()) &&
  1924. "ShiftAmountTy is too small to cover the range of this type!");
  1925. if (ShiftOp.getValueType() != ShiftTy)
  1926. ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
  1927. SDValue Ops[] = { LHSL, LHSH, ShiftOp };
  1928. Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
  1929. Hi = Lo.getValue(1);
  1930. return;
  1931. }
  1932. // Otherwise, emit a libcall.
  1933. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1934. bool isSigned;
  1935. if (N->getOpcode() == ISD::SHL) {
  1936. isSigned = false; /*sign irrelevant*/
  1937. if (VT == MVT::i16)
  1938. LC = RTLIB::SHL_I16;
  1939. else if (VT == MVT::i32)
  1940. LC = RTLIB::SHL_I32;
  1941. else if (VT == MVT::i64)
  1942. LC = RTLIB::SHL_I64;
  1943. else if (VT == MVT::i128)
  1944. LC = RTLIB::SHL_I128;
  1945. } else if (N->getOpcode() == ISD::SRL) {
  1946. isSigned = false;
  1947. if (VT == MVT::i16)
  1948. LC = RTLIB::SRL_I16;
  1949. else if (VT == MVT::i32)
  1950. LC = RTLIB::SRL_I32;
  1951. else if (VT == MVT::i64)
  1952. LC = RTLIB::SRL_I64;
  1953. else if (VT == MVT::i128)
  1954. LC = RTLIB::SRL_I128;
  1955. } else {
  1956. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  1957. isSigned = true;
  1958. if (VT == MVT::i16)
  1959. LC = RTLIB::SRA_I16;
  1960. else if (VT == MVT::i32)
  1961. LC = RTLIB::SRA_I32;
  1962. else if (VT == MVT::i64)
  1963. LC = RTLIB::SRA_I64;
  1964. else if (VT == MVT::i128)
  1965. LC = RTLIB::SRA_I128;
  1966. }
  1967. if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
  1968. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1969. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, isSigned, dl).first, Lo,
  1970. Hi);
  1971. return;
  1972. }
  1973. if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
  1974. llvm_unreachable("Unsupported shift!");
  1975. }
  1976. void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
  1977. SDValue &Lo, SDValue &Hi) {
  1978. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  1979. SDLoc dl(N);
  1980. SDValue Op = N->getOperand(0);
  1981. if (Op.getValueType().bitsLE(NVT)) {
  1982. // The low part is sign extension of the input (degenerates to a copy).
  1983. Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
  1984. // The high part is obtained by SRA'ing all but one of the bits of low part.
  1985. unsigned LoSize = NVT.getSizeInBits();
  1986. Hi = DAG.getNode(
  1987. ISD::SRA, dl, NVT, Lo,
  1988. DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
  1989. } else {
  1990. // For example, extension of an i48 to an i64. The operand type necessarily
  1991. // promotes to the result type, so will end up being expanded too.
  1992. assert(getTypeAction(Op.getValueType()) ==
  1993. TargetLowering::TypePromoteInteger &&
  1994. "Only know how to promote this result!");
  1995. SDValue Res = GetPromotedInteger(Op);
  1996. assert(Res.getValueType() == N->getValueType(0) &&
  1997. "Operand over promoted?");
  1998. // Split the promoted operand. This will simplify when it is expanded.
  1999. SplitInteger(Res, Lo, Hi);
  2000. unsigned ExcessBits =
  2001. Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
  2002. Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
  2003. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  2004. ExcessBits)));
  2005. }
  2006. }
  2007. void DAGTypeLegalizer::
  2008. ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
  2009. SDLoc dl(N);
  2010. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2011. EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  2012. if (EVT.bitsLE(Lo.getValueType())) {
  2013. // sext_inreg the low part if needed.
  2014. Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
  2015. N->getOperand(1));
  2016. // The high part gets the sign extension from the lo-part. This handles
  2017. // things like sextinreg V:i64 from i8.
  2018. Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
  2019. DAG.getConstant(Hi.getValueType().getSizeInBits() - 1, dl,
  2020. TLI.getPointerTy(DAG.getDataLayout())));
  2021. } else {
  2022. // For example, extension of an i48 to an i64. Leave the low part alone,
  2023. // sext_inreg the high part.
  2024. unsigned ExcessBits =
  2025. EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
  2026. Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
  2027. DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
  2028. ExcessBits)));
  2029. }
  2030. }
  2031. void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
  2032. SDValue &Lo, SDValue &Hi) {
  2033. EVT VT = N->getValueType(0);
  2034. SDLoc dl(N);
  2035. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2036. if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
  2037. SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  2038. SplitInteger(Res.getValue(1), Lo, Hi);
  2039. return;
  2040. }
  2041. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2042. if (VT == MVT::i16)
  2043. LC = RTLIB::SREM_I16;
  2044. else if (VT == MVT::i32)
  2045. LC = RTLIB::SREM_I32;
  2046. else if (VT == MVT::i64)
  2047. LC = RTLIB::SREM_I64;
  2048. else if (VT == MVT::i128)
  2049. LC = RTLIB::SREM_I128;
  2050. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
  2051. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, true, dl).first, Lo, Hi);
  2052. }
  2053. void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
  2054. SDValue &Lo, SDValue &Hi) {
  2055. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2056. SDLoc dl(N);
  2057. Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
  2058. Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
  2059. N->getOperand(0),
  2060. DAG.getConstant(NVT.getSizeInBits(), dl,
  2061. TLI.getPointerTy(DAG.getDataLayout())));
  2062. Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
  2063. }
  2064. void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
  2065. SDValue &Lo, SDValue &Hi) {
  2066. SDValue LHS = N->getOperand(0);
  2067. SDValue RHS = N->getOperand(1);
  2068. SDLoc dl(N);
  2069. // Expand the result by simply replacing it with the equivalent
  2070. // non-overflow-checking operation.
  2071. SDValue Sum = DAG.getNode(N->getOpcode() == ISD::UADDO ?
  2072. ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
  2073. LHS, RHS);
  2074. SplitInteger(Sum, Lo, Hi);
  2075. // Calculate the overflow: addition overflows iff a + b < a, and subtraction
  2076. // overflows iff a - b > a.
  2077. SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS,
  2078. N->getOpcode () == ISD::UADDO ?
  2079. ISD::SETULT : ISD::SETUGT);
  2080. // Use the calculated overflow everywhere.
  2081. ReplaceValueWith(SDValue(N, 1), Ofl);
  2082. }
  2083. void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
  2084. SDValue &Lo, SDValue &Hi) {
  2085. EVT VT = N->getValueType(0);
  2086. SDLoc dl(N);
  2087. // A divide for UMULO should be faster than a function call.
  2088. if (N->getOpcode() == ISD::UMULO) {
  2089. SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
  2090. SDValue MUL = DAG.getNode(ISD::MUL, dl, LHS.getValueType(), LHS, RHS);
  2091. SplitInteger(MUL, Lo, Hi);
  2092. // A divide for UMULO will be faster than a function call. Select to
  2093. // make sure we aren't using 0.
  2094. SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(VT),
  2095. RHS, DAG.getConstant(0, dl, VT), ISD::SETEQ);
  2096. SDValue NotZero = DAG.getSelect(dl, VT, isZero,
  2097. DAG.getConstant(1, dl, VT), RHS);
  2098. SDValue DIV = DAG.getNode(ISD::UDIV, dl, VT, MUL, NotZero);
  2099. SDValue Overflow = DAG.getSetCC(dl, N->getValueType(1), DIV, LHS,
  2100. ISD::SETNE);
  2101. Overflow = DAG.getSelect(dl, N->getValueType(1), isZero,
  2102. DAG.getConstant(0, dl, N->getValueType(1)),
  2103. Overflow);
  2104. ReplaceValueWith(SDValue(N, 1), Overflow);
  2105. return;
  2106. }
  2107. Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
  2108. EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
  2109. Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
  2110. // Replace this with a libcall that will check overflow.
  2111. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2112. if (VT == MVT::i32)
  2113. LC = RTLIB::MULO_I32;
  2114. else if (VT == MVT::i64)
  2115. LC = RTLIB::MULO_I64;
  2116. else if (VT == MVT::i128)
  2117. LC = RTLIB::MULO_I128;
  2118. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!");
  2119. SDValue Temp = DAG.CreateStackTemporary(PtrVT);
  2120. // Temporary for the overflow value, default it to zero.
  2121. SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl,
  2122. DAG.getConstant(0, dl, PtrVT), Temp,
  2123. MachinePointerInfo(), false, false, 0);
  2124. TargetLowering::ArgListTy Args;
  2125. TargetLowering::ArgListEntry Entry;
  2126. for (const SDValue &Op : N->op_values()) {
  2127. EVT ArgVT = Op.getValueType();
  2128. Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
  2129. Entry.Node = Op;
  2130. Entry.Ty = ArgTy;
  2131. Entry.isSExt = true;
  2132. Entry.isZExt = false;
  2133. Args.push_back(Entry);
  2134. }
  2135. // Also pass the address of the overflow check.
  2136. Entry.Node = Temp;
  2137. Entry.Ty = PtrTy->getPointerTo();
  2138. Entry.isSExt = true;
  2139. Entry.isZExt = false;
  2140. Args.push_back(Entry);
  2141. SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
  2142. TargetLowering::CallLoweringInfo CLI(DAG);
  2143. CLI.setDebugLoc(dl).setChain(Chain)
  2144. .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args), 0)
  2145. .setSExtResult();
  2146. std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
  2147. SplitInteger(CallInfo.first, Lo, Hi);
  2148. SDValue Temp2 = DAG.getLoad(PtrVT, dl, CallInfo.second, Temp,
  2149. MachinePointerInfo(), false, false, false, 0);
  2150. SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
  2151. DAG.getConstant(0, dl, PtrVT),
  2152. ISD::SETNE);
  2153. // Use the overflow from the libcall everywhere.
  2154. ReplaceValueWith(SDValue(N, 1), Ofl);
  2155. }
  2156. void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
  2157. SDValue &Lo, SDValue &Hi) {
  2158. EVT VT = N->getValueType(0);
  2159. SDLoc dl(N);
  2160. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2161. if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
  2162. SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  2163. SplitInteger(Res.getValue(0), Lo, Hi);
  2164. return;
  2165. }
  2166. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2167. if (VT == MVT::i16)
  2168. LC = RTLIB::UDIV_I16;
  2169. else if (VT == MVT::i32)
  2170. LC = RTLIB::UDIV_I32;
  2171. else if (VT == MVT::i64)
  2172. LC = RTLIB::UDIV_I64;
  2173. else if (VT == MVT::i128)
  2174. LC = RTLIB::UDIV_I128;
  2175. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
  2176. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, false, dl).first, Lo, Hi);
  2177. }
  2178. void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
  2179. SDValue &Lo, SDValue &Hi) {
  2180. EVT VT = N->getValueType(0);
  2181. SDLoc dl(N);
  2182. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  2183. if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
  2184. SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
  2185. SplitInteger(Res.getValue(1), Lo, Hi);
  2186. return;
  2187. }
  2188. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  2189. if (VT == MVT::i16)
  2190. LC = RTLIB::UREM_I16;
  2191. else if (VT == MVT::i32)
  2192. LC = RTLIB::UREM_I32;
  2193. else if (VT == MVT::i64)
  2194. LC = RTLIB::UREM_I64;
  2195. else if (VT == MVT::i128)
  2196. LC = RTLIB::UREM_I128;
  2197. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
  2198. SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, 2, false, dl).first, Lo, Hi);
  2199. }
  2200. void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
  2201. SDValue &Lo, SDValue &Hi) {
  2202. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
  2203. SDLoc dl(N);
  2204. SDValue Op = N->getOperand(0);
  2205. if (Op.getValueType().bitsLE(NVT)) {
  2206. // The low part is zero extension of the input (degenerates to a copy).
  2207. Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
  2208. Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
  2209. } else {
  2210. // For example, extension of an i48 to an i64. The operand type necessarily
  2211. // promotes to the result type, so will end up being expanded too.
  2212. assert(getTypeAction(Op.getValueType()) ==
  2213. TargetLowering::TypePromoteInteger &&
  2214. "Only know how to promote this result!");
  2215. SDValue Res = GetPromotedInteger(Op);
  2216. assert(Res.getValueType() == N->getValueType(0) &&
  2217. "Operand over promoted?");
  2218. // Split the promoted operand. This will simplify when it is expanded.
  2219. SplitInteger(Res, Lo, Hi);
  2220. unsigned ExcessBits =
  2221. Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
  2222. Hi = DAG.getZeroExtendInReg(Hi, dl,
  2223. EVT::getIntegerVT(*DAG.getContext(),
  2224. ExcessBits));
  2225. }
  2226. }
  2227. void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
  2228. SDValue &Lo, SDValue &Hi) {
  2229. SDLoc dl(N);
  2230. EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
  2231. SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
  2232. SDValue Zero = DAG.getConstant(0, dl, VT);
  2233. SDValue Swap = DAG.getAtomicCmpSwap(
  2234. ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
  2235. cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
  2236. N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand(),
  2237. cast<AtomicSDNode>(N)->getOrdering(),
  2238. cast<AtomicSDNode>(N)->getOrdering(),
  2239. cast<AtomicSDNode>(N)->getSynchScope());
  2240. ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
  2241. ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
  2242. }
  2243. //===----------------------------------------------------------------------===//
  2244. // Integer Operand Expansion
  2245. //===----------------------------------------------------------------------===//
  2246. /// ExpandIntegerOperand - This method is called when the specified operand of
  2247. /// the specified node is found to need expansion. At this point, all of the
  2248. /// result types of the node are known to be legal, but other operands of the
  2249. /// node may need promotion or expansion as well as the specified one.
  2250. bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
  2251. DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG); dbgs() << "\n");
  2252. SDValue Res = SDValue();
  2253. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  2254. return false;
  2255. switch (N->getOpcode()) {
  2256. default:
  2257. #ifndef NDEBUG
  2258. dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
  2259. N->dump(&DAG); dbgs() << "\n";
  2260. #endif
  2261. llvm_unreachable("Do not know how to expand this operator's operand!");
  2262. case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
  2263. case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
  2264. case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
  2265. case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
  2266. case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
  2267. case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
  2268. case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
  2269. case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
  2270. case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
  2271. case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
  2272. case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
  2273. case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
  2274. case ISD::SHL:
  2275. case ISD::SRA:
  2276. case ISD::SRL:
  2277. case ISD::ROTL:
  2278. case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
  2279. case ISD::RETURNADDR:
  2280. case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
  2281. case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
  2282. }
  2283. // If the result is null, the sub-method took care of registering results etc.
  2284. if (!Res.getNode()) return false;
  2285. // If the result is N, the sub-method updated N in place. Tell the legalizer
  2286. // core about this.
  2287. if (Res.getNode() == N)
  2288. return true;
  2289. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  2290. "Invalid operand expansion");
  2291. ReplaceValueWith(SDValue(N, 0), Res);
  2292. return false;
  2293. }
  2294. /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
  2295. /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
  2296. void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
  2297. SDValue &NewRHS,
  2298. ISD::CondCode &CCCode,
  2299. SDLoc dl) {
  2300. SDValue LHSLo, LHSHi, RHSLo, RHSHi;
  2301. GetExpandedInteger(NewLHS, LHSLo, LHSHi);
  2302. GetExpandedInteger(NewRHS, RHSLo, RHSHi);
  2303. if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
  2304. if (RHSLo == RHSHi) {
  2305. if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
  2306. if (RHSCST->isAllOnesValue()) {
  2307. // Equality comparison to -1.
  2308. NewLHS = DAG.getNode(ISD::AND, dl,
  2309. LHSLo.getValueType(), LHSLo, LHSHi);
  2310. NewRHS = RHSLo;
  2311. return;
  2312. }
  2313. }
  2314. }
  2315. NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
  2316. NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
  2317. NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
  2318. NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
  2319. return;
  2320. }
  2321. // If this is a comparison of the sign bit, just look at the top part.
  2322. // X > -1, x < 0
  2323. if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
  2324. if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
  2325. (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
  2326. NewLHS = LHSHi;
  2327. NewRHS = RHSHi;
  2328. return;
  2329. }
  2330. // FIXME: This generated code sucks.
  2331. ISD::CondCode LowCC;
  2332. switch (CCCode) {
  2333. default: llvm_unreachable("Unknown integer setcc!");
  2334. case ISD::SETLT:
  2335. case ISD::SETULT: LowCC = ISD::SETULT; break;
  2336. case ISD::SETGT:
  2337. case ISD::SETUGT: LowCC = ISD::SETUGT; break;
  2338. case ISD::SETLE:
  2339. case ISD::SETULE: LowCC = ISD::SETULE; break;
  2340. case ISD::SETGE:
  2341. case ISD::SETUGE: LowCC = ISD::SETUGE; break;
  2342. }
  2343. // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
  2344. // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
  2345. // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
  2346. // NOTE: on targets without efficient SELECT of bools, we can always use
  2347. // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
  2348. TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
  2349. nullptr);
  2350. SDValue Tmp1, Tmp2;
  2351. if (TLI.isTypeLegal(LHSLo.getValueType()) &&
  2352. TLI.isTypeLegal(RHSLo.getValueType()))
  2353. Tmp1 = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()),
  2354. LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
  2355. if (!Tmp1.getNode())
  2356. Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
  2357. LHSLo, RHSLo, LowCC);
  2358. if (TLI.isTypeLegal(LHSHi.getValueType()) &&
  2359. TLI.isTypeLegal(RHSHi.getValueType()))
  2360. Tmp2 = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()),
  2361. LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
  2362. if (!Tmp2.getNode())
  2363. Tmp2 = DAG.getNode(ISD::SETCC, dl,
  2364. getSetCCResultType(LHSHi.getValueType()),
  2365. LHSHi, RHSHi, DAG.getCondCode(CCCode));
  2366. ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
  2367. ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
  2368. if ((Tmp1C && Tmp1C->isNullValue()) ||
  2369. (Tmp2C && Tmp2C->isNullValue() &&
  2370. (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
  2371. CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
  2372. (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
  2373. (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
  2374. CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
  2375. // low part is known false, returns high part.
  2376. // For LE / GE, if high part is known false, ignore the low part.
  2377. // For LT / GT, if high part is known true, ignore the low part.
  2378. NewLHS = Tmp2;
  2379. NewRHS = SDValue();
  2380. return;
  2381. }
  2382. NewLHS = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()),
  2383. LHSHi, RHSHi, ISD::SETEQ, false,
  2384. DagCombineInfo, dl);
  2385. if (!NewLHS.getNode())
  2386. NewLHS = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
  2387. LHSHi, RHSHi, ISD::SETEQ);
  2388. NewLHS = DAG.getSelect(dl, Tmp1.getValueType(),
  2389. NewLHS, Tmp1, Tmp2);
  2390. NewRHS = SDValue();
  2391. }
  2392. SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
  2393. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  2394. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  2395. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  2396. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  2397. // against zero to select between true and false values.
  2398. if (!NewRHS.getNode()) {
  2399. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  2400. CCCode = ISD::SETNE;
  2401. }
  2402. // Update N to have the operands specified.
  2403. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
  2404. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  2405. N->getOperand(4)), 0);
  2406. }
  2407. SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
  2408. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  2409. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  2410. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  2411. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  2412. // against zero to select between true and false values.
  2413. if (!NewRHS.getNode()) {
  2414. NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
  2415. CCCode = ISD::SETNE;
  2416. }
  2417. // Update N to have the operands specified.
  2418. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  2419. N->getOperand(2), N->getOperand(3),
  2420. DAG.getCondCode(CCCode)), 0);
  2421. }
  2422. SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
  2423. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  2424. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  2425. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
  2426. // If ExpandSetCCOperands returned a scalar, use it.
  2427. if (!NewRHS.getNode()) {
  2428. assert(NewLHS.getValueType() == N->getValueType(0) &&
  2429. "Unexpected setcc expansion!");
  2430. return NewLHS;
  2431. }
  2432. // Otherwise, update N to have the operands specified.
  2433. return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
  2434. DAG.getCondCode(CCCode)), 0);
  2435. }
  2436. SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
  2437. // The value being shifted is legal, but the shift amount is too big.
  2438. // It follows that either the result of the shift is undefined, or the
  2439. // upper half of the shift amount is zero. Just use the lower half.
  2440. SDValue Lo, Hi;
  2441. GetExpandedInteger(N->getOperand(1), Lo, Hi);
  2442. return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
  2443. }
  2444. SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
  2445. // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
  2446. // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
  2447. // constant to valid type.
  2448. SDValue Lo, Hi;
  2449. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  2450. return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
  2451. }
  2452. SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
  2453. SDValue Op = N->getOperand(0);
  2454. EVT DstVT = N->getValueType(0);
  2455. RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
  2456. assert(LC != RTLIB::UNKNOWN_LIBCALL &&
  2457. "Don't know how to expand this SINT_TO_FP!");
  2458. return TLI.makeLibCall(DAG, LC, DstVT, &Op, 1, true, SDLoc(N)).first;
  2459. }
  2460. SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
  2461. if (ISD::isNormalStore(N))
  2462. return ExpandOp_NormalStore(N, OpNo);
  2463. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  2464. assert(OpNo == 1 && "Can only expand the stored value so far");
  2465. EVT VT = N->getOperand(1).getValueType();
  2466. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  2467. SDValue Ch = N->getChain();
  2468. SDValue Ptr = N->getBasePtr();
  2469. unsigned Alignment = N->getAlignment();
  2470. bool isVolatile = N->isVolatile();
  2471. bool isNonTemporal = N->isNonTemporal();
  2472. AAMDNodes AAInfo = N->getAAInfo();
  2473. SDLoc dl(N);
  2474. SDValue Lo, Hi;
  2475. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  2476. if (N->getMemoryVT().bitsLE(NVT)) {
  2477. GetExpandedInteger(N->getValue(), Lo, Hi);
  2478. return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
  2479. N->getMemoryVT(), isVolatile, isNonTemporal,
  2480. Alignment, AAInfo);
  2481. }
  2482. if (DAG.getDataLayout().isLittleEndian()) {
  2483. // Little-endian - low bits are at low addresses.
  2484. GetExpandedInteger(N->getValue(), Lo, Hi);
  2485. Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
  2486. isVolatile, isNonTemporal, Alignment, AAInfo);
  2487. unsigned ExcessBits =
  2488. N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
  2489. EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
  2490. // Increment the pointer to the other half.
  2491. unsigned IncrementSize = NVT.getSizeInBits()/8;
  2492. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  2493. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  2494. Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
  2495. N->getPointerInfo().getWithOffset(IncrementSize),
  2496. NEVT, isVolatile, isNonTemporal,
  2497. MinAlign(Alignment, IncrementSize), AAInfo);
  2498. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  2499. }
  2500. // Big-endian - high bits are at low addresses. Favor aligned stores at
  2501. // the cost of some bit-fiddling.
  2502. GetExpandedInteger(N->getValue(), Lo, Hi);
  2503. EVT ExtVT = N->getMemoryVT();
  2504. unsigned EBytes = ExtVT.getStoreSize();
  2505. unsigned IncrementSize = NVT.getSizeInBits()/8;
  2506. unsigned ExcessBits = (EBytes - IncrementSize)*8;
  2507. EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
  2508. ExtVT.getSizeInBits() - ExcessBits);
  2509. if (ExcessBits < NVT.getSizeInBits()) {
  2510. // Transfer high bits from the top of Lo to the bottom of Hi.
  2511. Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
  2512. DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
  2513. TLI.getPointerTy(DAG.getDataLayout())));
  2514. Hi = DAG.getNode(
  2515. ISD::OR, dl, NVT, Hi,
  2516. DAG.getNode(ISD::SRL, dl, NVT, Lo,
  2517. DAG.getConstant(ExcessBits, dl,
  2518. TLI.getPointerTy(DAG.getDataLayout()))));
  2519. }
  2520. // Store both the high bits and maybe some of the low bits.
  2521. Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(),
  2522. HiVT, isVolatile, isNonTemporal, Alignment, AAInfo);
  2523. // Increment the pointer to the other half.
  2524. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  2525. DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
  2526. // Store the lowest ExcessBits bits in the second half.
  2527. Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
  2528. N->getPointerInfo().getWithOffset(IncrementSize),
  2529. EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
  2530. isVolatile, isNonTemporal,
  2531. MinAlign(Alignment, IncrementSize), AAInfo);
  2532. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  2533. }
  2534. SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
  2535. SDValue InL, InH;
  2536. GetExpandedInteger(N->getOperand(0), InL, InH);
  2537. // Just truncate the low part of the source.
  2538. return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
  2539. }
  2540. SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
  2541. SDValue Op = N->getOperand(0);
  2542. EVT SrcVT = Op.getValueType();
  2543. EVT DstVT = N->getValueType(0);
  2544. SDLoc dl(N);
  2545. // The following optimization is valid only if every value in SrcVT (when
  2546. // treated as signed) is representable in DstVT. Check that the mantissa
  2547. // size of DstVT is >= than the number of bits in SrcVT -1.
  2548. const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT);
  2549. if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 &&
  2550. TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
  2551. // Do a signed conversion then adjust the result.
  2552. SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
  2553. SignedConv = TLI.LowerOperation(SignedConv, DAG);
  2554. // The result of the signed conversion needs adjusting if the 'sign bit' of
  2555. // the incoming integer was set. To handle this, we dynamically test to see
  2556. // if it is set, and, if so, add a fudge factor.
  2557. const uint64_t F32TwoE32 = 0x4F800000ULL;
  2558. const uint64_t F32TwoE64 = 0x5F800000ULL;
  2559. const uint64_t F32TwoE128 = 0x7F800000ULL;
  2560. APInt FF(32, 0);
  2561. if (SrcVT == MVT::i32)
  2562. FF = APInt(32, F32TwoE32);
  2563. else if (SrcVT == MVT::i64)
  2564. FF = APInt(32, F32TwoE64);
  2565. else if (SrcVT == MVT::i128)
  2566. FF = APInt(32, F32TwoE128);
  2567. else
  2568. llvm_unreachable("Unsupported UINT_TO_FP!");
  2569. // Check whether the sign bit is set.
  2570. SDValue Lo, Hi;
  2571. GetExpandedInteger(Op, Lo, Hi);
  2572. SDValue SignSet = DAG.getSetCC(dl,
  2573. getSetCCResultType(Hi.getValueType()),
  2574. Hi,
  2575. DAG.getConstant(0, dl, Hi.getValueType()),
  2576. ISD::SETLT);
  2577. // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
  2578. SDValue FudgePtr =
  2579. DAG.getConstantPool(ConstantInt::get(*DAG.getContext(), FF.zext(64)),
  2580. TLI.getPointerTy(DAG.getDataLayout()));
  2581. // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
  2582. SDValue Zero = DAG.getIntPtrConstant(0, dl);
  2583. SDValue Four = DAG.getIntPtrConstant(4, dl);
  2584. if (DAG.getDataLayout().isBigEndian())
  2585. std::swap(Zero, Four);
  2586. SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet,
  2587. Zero, Four);
  2588. unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
  2589. FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(),
  2590. FudgePtr, Offset);
  2591. Alignment = std::min(Alignment, 4u);
  2592. // Load the value out, extending it from f32 to the destination float type.
  2593. // FIXME: Avoid the extend by constructing the right constant pool?
  2594. SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
  2595. FudgePtr,
  2596. MachinePointerInfo::getConstantPool(),
  2597. MVT::f32,
  2598. false, false, false, Alignment);
  2599. return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
  2600. }
  2601. // Otherwise, use a libcall.
  2602. RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
  2603. assert(LC != RTLIB::UNKNOWN_LIBCALL &&
  2604. "Don't know how to expand this UINT_TO_FP!");
  2605. return TLI.makeLibCall(DAG, LC, DstVT, &Op, 1, true, dl).first;
  2606. }
  2607. SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
  2608. SDLoc dl(N);
  2609. SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
  2610. cast<AtomicSDNode>(N)->getMemoryVT(),
  2611. N->getOperand(0),
  2612. N->getOperand(1), N->getOperand(2),
  2613. cast<AtomicSDNode>(N)->getMemOperand(),
  2614. cast<AtomicSDNode>(N)->getOrdering(),
  2615. cast<AtomicSDNode>(N)->getSynchScope());
  2616. return Swap.getValue(1);
  2617. }
  2618. SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
  2619. SDValue InOp0 = N->getOperand(0);
  2620. EVT InVT = InOp0.getValueType();
  2621. EVT OutVT = N->getValueType(0);
  2622. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  2623. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  2624. unsigned OutNumElems = OutVT.getVectorNumElements();
  2625. EVT NOutVTElem = NOutVT.getVectorElementType();
  2626. SDLoc dl(N);
  2627. SDValue BaseIdx = N->getOperand(1);
  2628. SmallVector<SDValue, 8> Ops;
  2629. Ops.reserve(OutNumElems);
  2630. for (unsigned i = 0; i != OutNumElems; ++i) {
  2631. // Extract the element from the original vector.
  2632. SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
  2633. BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
  2634. SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  2635. InVT.getVectorElementType(), N->getOperand(0), Index);
  2636. SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, Ext);
  2637. // Insert the converted element to the new vector.
  2638. Ops.push_back(Op);
  2639. }
  2640. return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops);
  2641. }
  2642. SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
  2643. ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
  2644. EVT VT = N->getValueType(0);
  2645. SDLoc dl(N);
  2646. ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
  2647. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  2648. SDValue V1 = GetPromotedInteger(N->getOperand(1));
  2649. EVT OutVT = V0.getValueType();
  2650. return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
  2651. }
  2652. SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
  2653. EVT OutVT = N->getValueType(0);
  2654. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  2655. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  2656. unsigned NumElems = N->getNumOperands();
  2657. EVT NOutVTElem = NOutVT.getVectorElementType();
  2658. SDLoc dl(N);
  2659. SmallVector<SDValue, 8> Ops;
  2660. Ops.reserve(NumElems);
  2661. for (unsigned i = 0; i != NumElems; ++i) {
  2662. SDValue Op;
  2663. // BUILD_VECTOR integer operand types are allowed to be larger than the
  2664. // result's element type. This may still be true after the promotion. For
  2665. // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
  2666. // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
  2667. if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
  2668. Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
  2669. else
  2670. Op = N->getOperand(i);
  2671. Ops.push_back(Op);
  2672. }
  2673. return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops);
  2674. }
  2675. SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
  2676. SDLoc dl(N);
  2677. assert(!N->getOperand(0).getValueType().isVector() &&
  2678. "Input must be a scalar");
  2679. EVT OutVT = N->getValueType(0);
  2680. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  2681. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  2682. EVT NOutVTElem = NOutVT.getVectorElementType();
  2683. SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
  2684. return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
  2685. }
  2686. SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
  2687. SDLoc dl(N);
  2688. EVT OutVT = N->getValueType(0);
  2689. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  2690. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  2691. EVT InElemTy = OutVT.getVectorElementType();
  2692. EVT OutElemTy = NOutVT.getVectorElementType();
  2693. unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
  2694. unsigned NumOutElem = NOutVT.getVectorNumElements();
  2695. unsigned NumOperands = N->getNumOperands();
  2696. assert(NumElem * NumOperands == NumOutElem &&
  2697. "Unexpected number of elements");
  2698. // Take the elements from the first vector.
  2699. SmallVector<SDValue, 8> Ops(NumOutElem);
  2700. for (unsigned i = 0; i < NumOperands; ++i) {
  2701. SDValue Op = N->getOperand(i);
  2702. for (unsigned j = 0; j < NumElem; ++j) {
  2703. SDValue Ext = DAG.getNode(
  2704. ISD::EXTRACT_VECTOR_ELT, dl, InElemTy, Op,
  2705. DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  2706. Ops[i * NumElem + j] = DAG.getNode(ISD::ANY_EXTEND, dl, OutElemTy, Ext);
  2707. }
  2708. }
  2709. return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops);
  2710. }
  2711. SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
  2712. EVT OutVT = N->getValueType(0);
  2713. EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
  2714. assert(NOutVT.isVector() && "This type must be promoted to a vector type");
  2715. EVT NOutVTElem = NOutVT.getVectorElementType();
  2716. SDLoc dl(N);
  2717. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  2718. SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
  2719. NOutVTElem, N->getOperand(1));
  2720. return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
  2721. V0, ConvElem, N->getOperand(2));
  2722. }
  2723. SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
  2724. SDLoc dl(N);
  2725. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  2726. SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
  2727. TLI.getVectorIdxTy(DAG.getDataLayout()));
  2728. SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  2729. V0->getValueType(0).getScalarType(), V0, V1);
  2730. // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
  2731. // element types. If this is the case then we need to expand the outgoing
  2732. // value and not truncate it.
  2733. return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
  2734. }
  2735. SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
  2736. SDLoc dl(N);
  2737. SDValue V0 = GetPromotedInteger(N->getOperand(0));
  2738. MVT InVT = V0.getValueType().getSimpleVT();
  2739. MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
  2740. N->getValueType(0).getVectorNumElements());
  2741. SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
  2742. return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
  2743. }
  2744. SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
  2745. SDLoc dl(N);
  2746. unsigned NumElems = N->getNumOperands();
  2747. EVT RetSclrTy = N->getValueType(0).getVectorElementType();
  2748. SmallVector<SDValue, 8> NewOps;
  2749. NewOps.reserve(NumElems);
  2750. // For each incoming vector
  2751. for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
  2752. SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
  2753. EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
  2754. unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
  2755. for (unsigned i=0; i<NumElem; ++i) {
  2756. // Extract element from incoming vector
  2757. SDValue Ex = DAG.getNode(
  2758. ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
  2759. DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
  2760. SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
  2761. NewOps.push_back(Tr);
  2762. }
  2763. }
  2764. return DAG.getNode(ISD::BUILD_VECTOR, dl, N->getValueType(0), NewOps);
  2765. }