LegalizeIntegerTypes.cpp 120 KB

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