LegalizeIntegerTypes.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  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/CodeGen/PseudoSourceValue.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. using namespace llvm;
  24. //===----------------------------------------------------------------------===//
  25. // Integer Result Promotion
  26. //===----------------------------------------------------------------------===//
  27. /// PromoteIntegerResult - This method is called when a result of a node is
  28. /// found to be in need of promotion to a larger type. At this point, the node
  29. /// may also have invalid operands or may have other results that need
  30. /// expansion, we just know that (at least) one result needs promotion.
  31. void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
  32. DEBUG(cerr << "Promote integer result: "; N->dump(&DAG); cerr << "\n");
  33. SDValue Res = SDValue();
  34. // See if the target wants to custom expand this node.
  35. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  36. return;
  37. switch (N->getOpcode()) {
  38. default:
  39. #ifndef NDEBUG
  40. cerr << "PromoteIntegerResult #" << ResNo << ": ";
  41. N->dump(&DAG); cerr << "\n";
  42. #endif
  43. LLVM_UNREACHABLE("Do not know how to promote this operator!");
  44. case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
  45. case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
  46. case ISD::BIT_CONVERT: Res = PromoteIntRes_BIT_CONVERT(N); break;
  47. case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
  48. case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
  49. case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
  50. case ISD::CONVERT_RNDSAT:
  51. Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
  52. case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
  53. case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break;
  54. case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
  55. case ISD::EXTRACT_VECTOR_ELT:
  56. Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
  57. case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
  58. case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
  59. case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
  60. case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
  61. case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
  62. case ISD::SIGN_EXTEND_INREG:
  63. Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
  64. case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
  65. case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
  66. case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
  67. case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
  68. case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
  69. case ISD::SIGN_EXTEND:
  70. case ISD::ZERO_EXTEND:
  71. case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
  72. case ISD::FP_TO_SINT:
  73. case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
  74. case ISD::AND:
  75. case ISD::OR:
  76. case ISD::XOR:
  77. case ISD::ADD:
  78. case ISD::SUB:
  79. case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
  80. case ISD::SDIV:
  81. case ISD::SREM: Res = PromoteIntRes_SDIV(N); break;
  82. case ISD::UDIV:
  83. case ISD::UREM: Res = PromoteIntRes_UDIV(N); break;
  84. case ISD::SADDO:
  85. case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
  86. case ISD::UADDO:
  87. case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
  88. case ISD::SMULO:
  89. case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
  90. case ISD::ATOMIC_LOAD_ADD:
  91. case ISD::ATOMIC_LOAD_SUB:
  92. case ISD::ATOMIC_LOAD_AND:
  93. case ISD::ATOMIC_LOAD_OR:
  94. case ISD::ATOMIC_LOAD_XOR:
  95. case ISD::ATOMIC_LOAD_NAND:
  96. case ISD::ATOMIC_LOAD_MIN:
  97. case ISD::ATOMIC_LOAD_MAX:
  98. case ISD::ATOMIC_LOAD_UMIN:
  99. case ISD::ATOMIC_LOAD_UMAX:
  100. case ISD::ATOMIC_SWAP:
  101. Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
  102. case ISD::ATOMIC_CMP_SWAP:
  103. Res = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
  104. }
  105. // If the result is null then the sub-method took care of registering it.
  106. if (Res.getNode())
  107. SetPromotedInteger(SDValue(N, ResNo), Res);
  108. }
  109. SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
  110. // Sign-extend the new bits, and continue the assertion.
  111. SDValue Op = SExtPromotedInteger(N->getOperand(0));
  112. return DAG.getNode(ISD::AssertSext, N->getDebugLoc(),
  113. Op.getValueType(), Op, N->getOperand(1));
  114. }
  115. SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
  116. // Zero the new bits, and continue the assertion.
  117. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  118. return DAG.getNode(ISD::AssertZext, N->getDebugLoc(),
  119. Op.getValueType(), Op, N->getOperand(1));
  120. }
  121. SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
  122. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  123. SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
  124. N->getMemoryVT(),
  125. N->getChain(), N->getBasePtr(),
  126. Op2, N->getSrcValue(), N->getAlignment());
  127. // Legalized the chain result - switch anything that used the old chain to
  128. // use the new one.
  129. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  130. return Res;
  131. }
  132. SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
  133. SDValue Op2 = GetPromotedInteger(N->getOperand(2));
  134. SDValue Op3 = GetPromotedInteger(N->getOperand(3));
  135. SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
  136. N->getMemoryVT(), N->getChain(), N->getBasePtr(),
  137. Op2, Op3, N->getSrcValue(), N->getAlignment());
  138. // Legalized the chain result - switch anything that used the old chain to
  139. // use the new one.
  140. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  141. return Res;
  142. }
  143. SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
  144. SDValue InOp = N->getOperand(0);
  145. MVT InVT = InOp.getValueType();
  146. MVT NInVT = TLI.getTypeToTransformTo(InVT);
  147. MVT OutVT = N->getValueType(0);
  148. MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
  149. DebugLoc dl = N->getDebugLoc();
  150. switch (getTypeAction(InVT)) {
  151. default:
  152. assert(false && "Unknown type action!");
  153. break;
  154. case Legal:
  155. break;
  156. case PromoteInteger:
  157. if (NOutVT.bitsEq(NInVT))
  158. // The input promotes to the same size. Convert the promoted value.
  159. return DAG.getNode(ISD::BIT_CONVERT, dl,
  160. NOutVT, GetPromotedInteger(InOp));
  161. break;
  162. case SoftenFloat:
  163. // Promote the integer operand by hand.
  164. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
  165. case ExpandInteger:
  166. case ExpandFloat:
  167. break;
  168. case ScalarizeVector:
  169. // Convert the element to an integer and promote it by hand.
  170. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
  171. BitConvertToInteger(GetScalarizedVector(InOp)));
  172. case SplitVector: {
  173. // For example, i32 = BIT_CONVERT v2i16 on alpha. Convert the split
  174. // pieces of the input into integers and reassemble in the final type.
  175. SDValue Lo, Hi;
  176. GetSplitVector(N->getOperand(0), Lo, Hi);
  177. Lo = BitConvertToInteger(Lo);
  178. Hi = BitConvertToInteger(Hi);
  179. if (TLI.isBigEndian())
  180. std::swap(Lo, Hi);
  181. InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
  182. MVT::getIntegerVT(NOutVT.getSizeInBits()),
  183. JoinIntegers(Lo, Hi));
  184. return DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, InOp);
  185. }
  186. case WidenVector:
  187. if (OutVT.bitsEq(NInVT))
  188. // The input is widened to the same size. Convert to the widened value.
  189. return DAG.getNode(ISD::BIT_CONVERT, dl, OutVT, GetWidenedVector(InOp));
  190. }
  191. return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
  192. CreateStackStoreLoad(InOp, OutVT));
  193. }
  194. SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
  195. SDValue Op = GetPromotedInteger(N->getOperand(0));
  196. MVT OVT = N->getValueType(0);
  197. MVT NVT = Op.getValueType();
  198. DebugLoc dl = N->getDebugLoc();
  199. unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
  200. return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
  201. DAG.getConstant(DiffBits, TLI.getPointerTy()));
  202. }
  203. SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
  204. // The pair element type may be legal, or may not promote to the same type as
  205. // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
  206. return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(),
  207. TLI.getTypeToTransformTo(N->getValueType(0)),
  208. JoinIntegers(N->getOperand(0), N->getOperand(1)));
  209. }
  210. SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
  211. MVT VT = N->getValueType(0);
  212. // FIXME there is no actual debug info here
  213. DebugLoc dl = N->getDebugLoc();
  214. // Zero extend things like i1, sign extend everything else. It shouldn't
  215. // matter in theory which one we pick, but this tends to give better code?
  216. unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
  217. SDValue Result = DAG.getNode(Opc, dl, TLI.getTypeToTransformTo(VT),
  218. SDValue(N, 0));
  219. assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
  220. return Result;
  221. }
  222. SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
  223. ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
  224. assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
  225. CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
  226. CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
  227. "can only promote integers");
  228. MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
  229. return DAG.getConvertRndSat(OutVT, N->getDebugLoc(), N->getOperand(0),
  230. N->getOperand(1), N->getOperand(2),
  231. N->getOperand(3), N->getOperand(4), CvtCode);
  232. }
  233. SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
  234. // Zero extend to the promoted type and do the count there.
  235. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  236. DebugLoc dl = N->getDebugLoc();
  237. MVT OVT = N->getValueType(0);
  238. MVT NVT = Op.getValueType();
  239. Op = DAG.getNode(ISD::CTLZ, dl, NVT, Op);
  240. // Subtract off the extra leading bits in the bigger type.
  241. return DAG.getNode(ISD::SUB, dl, NVT, Op,
  242. DAG.getConstant(NVT.getSizeInBits() -
  243. OVT.getSizeInBits(), NVT));
  244. }
  245. SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
  246. // Zero extend to the promoted type and do the count there.
  247. SDValue Op = ZExtPromotedInteger(N->getOperand(0));
  248. return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), Op.getValueType(), Op);
  249. }
  250. SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
  251. SDValue Op = GetPromotedInteger(N->getOperand(0));
  252. MVT OVT = N->getValueType(0);
  253. MVT NVT = Op.getValueType();
  254. DebugLoc dl = N->getDebugLoc();
  255. // The count is the same in the promoted type except if the original
  256. // value was zero. This can be handled by setting the bit just off
  257. // the top of the original type.
  258. APInt TopBit(NVT.getSizeInBits(), 0);
  259. TopBit.set(OVT.getSizeInBits());
  260. Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT));
  261. return DAG.getNode(ISD::CTTZ, dl, NVT, Op);
  262. }
  263. SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
  264. DebugLoc dl = N->getDebugLoc();
  265. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  266. return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0),
  267. N->getOperand(1));
  268. }
  269. SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
  270. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  271. unsigned NewOpc = N->getOpcode();
  272. DebugLoc dl = N->getDebugLoc();
  273. // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
  274. // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
  275. // and SINT conversions are Custom, there is no way to tell which is preferable.
  276. // We choose SINT because that's the right thing on PPC.)
  277. if (N->getOpcode() == ISD::FP_TO_UINT &&
  278. !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
  279. TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
  280. NewOpc = ISD::FP_TO_SINT;
  281. SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
  282. // Assert that the converted value fits in the original type. If it doesn't
  283. // (eg: because the value being converted is too big), then the result of the
  284. // original operation was undefined anyway, so the assert is still correct.
  285. return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
  286. ISD::AssertZext : ISD::AssertSext, dl,
  287. NVT, Res, DAG.getValueType(N->getValueType(0)));
  288. }
  289. SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
  290. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  291. DebugLoc dl = N->getDebugLoc();
  292. if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
  293. SDValue Res = GetPromotedInteger(N->getOperand(0));
  294. assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
  295. // If the result and operand types are the same after promotion, simplify
  296. // to an in-register extension.
  297. if (NVT == Res.getValueType()) {
  298. // The high bits are not guaranteed to be anything. Insert an extend.
  299. if (N->getOpcode() == ISD::SIGN_EXTEND)
  300. return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
  301. DAG.getValueType(N->getOperand(0).getValueType()));
  302. if (N->getOpcode() == ISD::ZERO_EXTEND)
  303. return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
  304. assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
  305. return Res;
  306. }
  307. }
  308. // Otherwise, just extend the original operand all the way to the larger type.
  309. return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
  310. }
  311. SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
  312. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  313. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  314. ISD::LoadExtType ExtType =
  315. ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
  316. DebugLoc dl = N->getDebugLoc();
  317. SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
  318. N->getSrcValue(), N->getSrcValueOffset(),
  319. N->getMemoryVT(), N->isVolatile(),
  320. N->getAlignment());
  321. // Legalized the chain result - switch anything that used the old chain to
  322. // use the new one.
  323. ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
  324. return Res;
  325. }
  326. /// Promote the overflow flag of an overflowing arithmetic node.
  327. SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
  328. // Simply change the return type of the boolean result.
  329. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(1));
  330. MVT ValueVTs[] = { N->getValueType(0), NVT };
  331. SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
  332. SDValue Res = DAG.getNode(N->getOpcode(), N->getDebugLoc(),
  333. DAG.getVTList(ValueVTs, 2), Ops, 2);
  334. // Modified the sum result - switch anything that used the old sum to use
  335. // the new one.
  336. ReplaceValueWith(SDValue(N, 0), Res);
  337. return SDValue(Res.getNode(), 1);
  338. }
  339. SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
  340. if (ResNo == 1)
  341. return PromoteIntRes_Overflow(N);
  342. // The operation overflowed iff the result in the larger type is not the
  343. // sign extension of its truncation to the original type.
  344. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  345. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  346. MVT OVT = N->getOperand(0).getValueType();
  347. MVT NVT = LHS.getValueType();
  348. DebugLoc dl = N->getDebugLoc();
  349. // Do the arithmetic in the larger type.
  350. unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
  351. SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
  352. // Calculate the overflow flag: sign extend the arithmetic result from
  353. // the original type.
  354. SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
  355. DAG.getValueType(OVT));
  356. // Overflowed if and only if this is not equal to Res.
  357. Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
  358. // Use the calculated overflow everywhere.
  359. ReplaceValueWith(SDValue(N, 1), Ofl);
  360. return Res;
  361. }
  362. SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
  363. // Sign extend the input.
  364. SDValue LHS = SExtPromotedInteger(N->getOperand(0));
  365. SDValue RHS = SExtPromotedInteger(N->getOperand(1));
  366. return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
  367. LHS.getValueType(), LHS, RHS);
  368. }
  369. SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
  370. SDValue LHS = GetPromotedInteger(N->getOperand(1));
  371. SDValue RHS = GetPromotedInteger(N->getOperand(2));
  372. return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
  373. LHS.getValueType(), N->getOperand(0),LHS,RHS);
  374. }
  375. SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
  376. SDValue LHS = GetPromotedInteger(N->getOperand(2));
  377. SDValue RHS = GetPromotedInteger(N->getOperand(3));
  378. return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
  379. LHS.getValueType(), N->getOperand(0),
  380. N->getOperand(1), LHS, RHS, N->getOperand(4));
  381. }
  382. SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
  383. MVT SVT = TLI.getSetCCResultType(N->getOperand(0).getValueType());
  384. assert(isTypeLegal(SVT) && "Illegal SetCC type!");
  385. DebugLoc dl = N->getDebugLoc();
  386. // Get the SETCC result using the canonical SETCC type.
  387. SDValue SetCC = DAG.getNode(ISD::SETCC, dl, SVT, N->getOperand(0),
  388. N->getOperand(1), N->getOperand(2));
  389. // Convert to the expected type.
  390. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  391. assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
  392. return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
  393. }
  394. SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
  395. return DAG.getNode(ISD::SHL, N->getDebugLoc(),
  396. TLI.getTypeToTransformTo(N->getValueType(0)),
  397. GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
  398. }
  399. SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
  400. SDValue Op = GetPromotedInteger(N->getOperand(0));
  401. return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(),
  402. Op.getValueType(), Op, N->getOperand(1));
  403. }
  404. SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
  405. // The input may have strange things in the top bits of the registers, but
  406. // these operations don't care. They may have weird bits going out, but
  407. // that too is okay if they are integer operations.
  408. SDValue LHS = GetPromotedInteger(N->getOperand(0));
  409. SDValue RHS = GetPromotedInteger(N->getOperand(1));
  410. return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
  411. LHS.getValueType(), LHS, RHS);
  412. }
  413. SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
  414. // The input value must be properly sign extended.
  415. SDValue Res = SExtPromotedInteger(N->getOperand(0));
  416. return DAG.getNode(ISD::SRA, N->getDebugLoc(),
  417. Res.getValueType(), Res, N->getOperand(1));
  418. }
  419. SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
  420. // The input value must be properly zero extended.
  421. MVT VT = N->getValueType(0);
  422. MVT NVT = TLI.getTypeToTransformTo(VT);
  423. SDValue Res = ZExtPromotedInteger(N->getOperand(0));
  424. return DAG.getNode(ISD::SRL, N->getDebugLoc(), NVT, Res, N->getOperand(1));
  425. }
  426. SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
  427. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  428. SDValue Res;
  429. switch (getTypeAction(N->getOperand(0).getValueType())) {
  430. default: LLVM_UNREACHABLE("Unknown type action!");
  431. case Legal:
  432. case ExpandInteger:
  433. Res = N->getOperand(0);
  434. break;
  435. case PromoteInteger:
  436. Res = GetPromotedInteger(N->getOperand(0));
  437. break;
  438. }
  439. // Truncate to NVT instead of VT
  440. return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Res);
  441. }
  442. SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
  443. if (ResNo == 1)
  444. return PromoteIntRes_Overflow(N);
  445. // The operation overflowed iff the result in the larger type is not the
  446. // zero extension of its truncation to the original type.
  447. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  448. SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
  449. MVT OVT = N->getOperand(0).getValueType();
  450. MVT NVT = LHS.getValueType();
  451. DebugLoc dl = N->getDebugLoc();
  452. // Do the arithmetic in the larger type.
  453. unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
  454. SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
  455. // Calculate the overflow flag: zero extend the arithmetic result from
  456. // the original type.
  457. SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
  458. // Overflowed if and only if this is not equal to Res.
  459. Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
  460. // Use the calculated overflow everywhere.
  461. ReplaceValueWith(SDValue(N, 1), Ofl);
  462. return Res;
  463. }
  464. SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
  465. // Zero extend the input.
  466. SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
  467. SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
  468. return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
  469. LHS.getValueType(), LHS, RHS);
  470. }
  471. SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
  472. return DAG.getUNDEF(TLI.getTypeToTransformTo(N->getValueType(0)));
  473. }
  474. SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
  475. SDValue Chain = N->getOperand(0); // Get the chain.
  476. SDValue Ptr = N->getOperand(1); // Get the pointer.
  477. MVT VT = N->getValueType(0);
  478. DebugLoc dl = N->getDebugLoc();
  479. MVT RegVT = TLI.getRegisterType(VT);
  480. unsigned NumRegs = TLI.getNumRegisters(VT);
  481. // The argument is passed as NumRegs registers of type RegVT.
  482. SmallVector<SDValue, 8> Parts(NumRegs);
  483. for (unsigned i = 0; i < NumRegs; ++i) {
  484. Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2));
  485. Chain = Parts[i].getValue(1);
  486. }
  487. // Handle endianness of the load.
  488. if (TLI.isBigEndian())
  489. std::reverse(Parts.begin(), Parts.end());
  490. // Assemble the parts in the promoted type.
  491. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  492. SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
  493. for (unsigned i = 1; i < NumRegs; ++i) {
  494. SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
  495. // Shift it to the right position and "or" it in.
  496. Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
  497. DAG.getConstant(i * RegVT.getSizeInBits(),
  498. TLI.getPointerTy()));
  499. Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
  500. }
  501. // Modified the chain result - switch anything that used the old chain to
  502. // use the new one.
  503. ReplaceValueWith(SDValue(N, 1), Chain);
  504. return Res;
  505. }
  506. SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
  507. assert(ResNo == 1 && "Only boolean result promotion currently supported!");
  508. return PromoteIntRes_Overflow(N);
  509. }
  510. //===----------------------------------------------------------------------===//
  511. // Integer Operand Promotion
  512. //===----------------------------------------------------------------------===//
  513. /// PromoteIntegerOperand - This method is called when the specified operand of
  514. /// the specified node is found to need promotion. At this point, all of the
  515. /// result types of the node are known to be legal, but other operands of the
  516. /// node may need promotion or expansion as well as the specified one.
  517. bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
  518. DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
  519. SDValue Res = SDValue();
  520. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  521. return false;
  522. switch (N->getOpcode()) {
  523. default:
  524. #ifndef NDEBUG
  525. cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
  526. N->dump(&DAG); cerr << "\n";
  527. #endif
  528. LLVM_UNREACHABLE("Do not know how to promote this operator's operand!");
  529. case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
  530. case ISD::BIT_CONVERT: Res = PromoteIntOp_BIT_CONVERT(N); break;
  531. case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
  532. case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
  533. case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
  534. case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
  535. case ISD::CONVERT_RNDSAT:
  536. Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
  537. case ISD::INSERT_VECTOR_ELT:
  538. Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
  539. case ISD::MEMBARRIER: Res = PromoteIntOp_MEMBARRIER(N); break;
  540. case ISD::SCALAR_TO_VECTOR:
  541. Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
  542. case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
  543. case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
  544. case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
  545. case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
  546. case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
  547. case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
  548. OpNo); break;
  549. case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
  550. case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
  551. case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
  552. case ISD::SHL:
  553. case ISD::SRA:
  554. case ISD::SRL:
  555. case ISD::ROTL:
  556. case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
  557. }
  558. // If the result is null, the sub-method took care of registering results etc.
  559. if (!Res.getNode()) return false;
  560. // If the result is N, the sub-method updated N in place. Tell the legalizer
  561. // core about this.
  562. if (Res.getNode() == N)
  563. return true;
  564. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  565. "Invalid operand expansion");
  566. ReplaceValueWith(SDValue(N, 0), Res);
  567. return false;
  568. }
  569. /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
  570. /// shared among BR_CC, SELECT_CC, and SETCC handlers.
  571. void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
  572. ISD::CondCode CCCode) {
  573. // We have to insert explicit sign or zero extends. Note that we could
  574. // insert sign extends for ALL conditions, but zero extend is cheaper on
  575. // many machines (an AND instead of two shifts), so prefer it.
  576. switch (CCCode) {
  577. default: LLVM_UNREACHABLE("Unknown integer comparison!");
  578. case ISD::SETEQ:
  579. case ISD::SETNE:
  580. case ISD::SETUGE:
  581. case ISD::SETUGT:
  582. case ISD::SETULE:
  583. case ISD::SETULT:
  584. // ALL of these operations will work if we either sign or zero extend
  585. // the operands (including the unsigned comparisons!). Zero extend is
  586. // usually a simpler/cheaper operation, so prefer it.
  587. NewLHS = ZExtPromotedInteger(NewLHS);
  588. NewRHS = ZExtPromotedInteger(NewRHS);
  589. break;
  590. case ISD::SETGE:
  591. case ISD::SETGT:
  592. case ISD::SETLT:
  593. case ISD::SETLE:
  594. NewLHS = SExtPromotedInteger(NewLHS);
  595. NewRHS = SExtPromotedInteger(NewRHS);
  596. break;
  597. }
  598. }
  599. SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
  600. SDValue Op = GetPromotedInteger(N->getOperand(0));
  601. return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
  602. }
  603. SDValue DAGTypeLegalizer::PromoteIntOp_BIT_CONVERT(SDNode *N) {
  604. // This should only occur in unusual situations like bitcasting to an
  605. // x86_fp80, so just turn it into a store+load
  606. return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
  607. }
  608. SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
  609. assert(OpNo == 2 && "Don't know how to promote this operand!");
  610. SDValue LHS = N->getOperand(2);
  611. SDValue RHS = N->getOperand(3);
  612. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
  613. // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
  614. // legal types.
  615. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
  616. N->getOperand(1), LHS, RHS, N->getOperand(4));
  617. }
  618. SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
  619. assert(OpNo == 1 && "only know how to promote condition");
  620. // Promote all the way up to the canonical SetCC type.
  621. MVT SVT = TLI.getSetCCResultType(MVT::Other);
  622. SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
  623. // The chain (Op#0) and basic block destination (Op#2) are always legal types.
  624. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
  625. N->getOperand(2));
  626. }
  627. SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
  628. // Since the result type is legal, the operands must promote to it.
  629. MVT OVT = N->getOperand(0).getValueType();
  630. SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
  631. SDValue Hi = GetPromotedInteger(N->getOperand(1));
  632. assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
  633. DebugLoc dl = N->getDebugLoc();
  634. Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
  635. DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy()));
  636. return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
  637. }
  638. SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
  639. // The vector type is legal but the element type is not. This implies
  640. // that the vector is a power-of-two in length and that the element
  641. // type does not have a strange size (eg: it is not i1).
  642. MVT VecVT = N->getValueType(0);
  643. unsigned NumElts = VecVT.getVectorNumElements();
  644. assert(!(NumElts & 1) && "Legal vector of one illegal element?");
  645. // Promote the inserted value. The type does not need to match the
  646. // vector element type. Check that any extra bits introduced will be
  647. // truncated away.
  648. assert(N->getOperand(0).getValueType().getSizeInBits() >=
  649. N->getValueType(0).getVectorElementType().getSizeInBits() &&
  650. "Type of inserted value narrower than vector element type!");
  651. SmallVector<SDValue, 16> NewOps;
  652. for (unsigned i = 0; i < NumElts; ++i)
  653. NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
  654. return DAG.UpdateNodeOperands(SDValue(N, 0), &NewOps[0], NumElts);
  655. }
  656. SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
  657. ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
  658. assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
  659. CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
  660. CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
  661. "can only promote integer arguments");
  662. SDValue InOp = GetPromotedInteger(N->getOperand(0));
  663. return DAG.getConvertRndSat(N->getValueType(0), N->getDebugLoc(), InOp,
  664. N->getOperand(1), N->getOperand(2),
  665. N->getOperand(3), N->getOperand(4), CvtCode);
  666. }
  667. SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
  668. unsigned OpNo) {
  669. if (OpNo == 1) {
  670. // Promote the inserted value. This is valid because the type does not
  671. // have to match the vector element type.
  672. // Check that any extra bits introduced will be truncated away.
  673. assert(N->getOperand(1).getValueType().getSizeInBits() >=
  674. N->getValueType(0).getVectorElementType().getSizeInBits() &&
  675. "Type of inserted value narrower than vector element type!");
  676. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
  677. GetPromotedInteger(N->getOperand(1)),
  678. N->getOperand(2));
  679. }
  680. assert(OpNo == 2 && "Different operand and result vector types?");
  681. // Promote the index.
  682. SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
  683. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
  684. N->getOperand(1), Idx);
  685. }
  686. SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
  687. SDValue NewOps[6];
  688. DebugLoc dl = N->getDebugLoc();
  689. NewOps[0] = N->getOperand(0);
  690. for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
  691. SDValue Flag = GetPromotedInteger(N->getOperand(i));
  692. NewOps[i] = DAG.getZeroExtendInReg(Flag, dl, MVT::i1);
  693. }
  694. return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
  695. array_lengthof(NewOps));
  696. }
  697. SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
  698. // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
  699. // the operand in place.
  700. return DAG.UpdateNodeOperands(SDValue(N, 0),
  701. GetPromotedInteger(N->getOperand(0)));
  702. }
  703. SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
  704. assert(OpNo == 0 && "Only know how to promote condition");
  705. // Promote all the way up to the canonical SetCC type.
  706. MVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
  707. SDValue Cond = PromoteTargetBoolean(N->getOperand(0), SVT);
  708. return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
  709. N->getOperand(1), N->getOperand(2));
  710. }
  711. SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
  712. assert(OpNo == 0 && "Don't know how to promote this operand!");
  713. SDValue LHS = N->getOperand(0);
  714. SDValue RHS = N->getOperand(1);
  715. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
  716. // The CC (#4) and the possible return values (#2 and #3) have legal types.
  717. return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
  718. N->getOperand(3), N->getOperand(4));
  719. }
  720. SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
  721. assert(OpNo == 0 && "Don't know how to promote this operand!");
  722. SDValue LHS = N->getOperand(0);
  723. SDValue RHS = N->getOperand(1);
  724. PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
  725. // The CC (#2) is always legal.
  726. return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
  727. }
  728. SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
  729. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
  730. ZExtPromotedInteger(N->getOperand(1)));
  731. }
  732. SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
  733. SDValue Op = GetPromotedInteger(N->getOperand(0));
  734. DebugLoc dl = N->getDebugLoc();
  735. Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
  736. return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
  737. Op, DAG.getValueType(N->getOperand(0).getValueType()));
  738. }
  739. SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
  740. return DAG.UpdateNodeOperands(SDValue(N, 0),
  741. SExtPromotedInteger(N->getOperand(0)));
  742. }
  743. SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
  744. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  745. SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
  746. int SVOffset = N->getSrcValueOffset();
  747. unsigned Alignment = N->getAlignment();
  748. bool isVolatile = N->isVolatile();
  749. DebugLoc dl = N->getDebugLoc();
  750. SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
  751. // Truncate the value and store the result.
  752. return DAG.getTruncStore(Ch, dl, Val, Ptr, N->getSrcValue(),
  753. SVOffset, N->getMemoryVT(),
  754. isVolatile, Alignment);
  755. }
  756. SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
  757. SDValue Op = GetPromotedInteger(N->getOperand(0));
  758. return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), Op);
  759. }
  760. SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
  761. return DAG.UpdateNodeOperands(SDValue(N, 0),
  762. ZExtPromotedInteger(N->getOperand(0)));
  763. }
  764. SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
  765. DebugLoc dl = N->getDebugLoc();
  766. SDValue Op = GetPromotedInteger(N->getOperand(0));
  767. Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
  768. return DAG.getZeroExtendInReg(Op, dl, N->getOperand(0).getValueType());
  769. }
  770. //===----------------------------------------------------------------------===//
  771. // Integer Result Expansion
  772. //===----------------------------------------------------------------------===//
  773. /// ExpandIntegerResult - This method is called when the specified result of the
  774. /// specified node is found to need expansion. At this point, the node may also
  775. /// have invalid operands or may have other results that need promotion, we just
  776. /// know that (at least) one result needs expansion.
  777. void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
  778. DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
  779. SDValue Lo, Hi;
  780. Lo = Hi = SDValue();
  781. // See if the target wants to custom expand this node.
  782. if (CustomLowerNode(N, N->getValueType(ResNo), true))
  783. return;
  784. switch (N->getOpcode()) {
  785. default:
  786. #ifndef NDEBUG
  787. cerr << "ExpandIntegerResult #" << ResNo << ": ";
  788. N->dump(&DAG); cerr << "\n";
  789. #endif
  790. LLVM_UNREACHABLE("Do not know how to expand the result of this operator!");
  791. case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
  792. case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
  793. case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
  794. case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
  795. case ISD::BIT_CONVERT: ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
  796. case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
  797. case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
  798. case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
  799. case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
  800. case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
  801. case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
  802. case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
  803. case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
  804. case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
  805. case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
  806. case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
  807. case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
  808. case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
  809. case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
  810. case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
  811. case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
  812. case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
  813. case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
  814. case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
  815. case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
  816. case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
  817. case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
  818. case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
  819. case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
  820. case ISD::AND:
  821. case ISD::OR:
  822. case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
  823. case ISD::ADD:
  824. case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
  825. case ISD::ADDC:
  826. case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
  827. case ISD::ADDE:
  828. case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
  829. case ISD::SHL:
  830. case ISD::SRA:
  831. case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
  832. }
  833. // If Lo/Hi is null, the sub-method took care of registering results etc.
  834. if (Lo.getNode())
  835. SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
  836. }
  837. /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
  838. /// and the shift amount is a constant 'Amt'. Expand the operation.
  839. void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
  840. SDValue &Lo, SDValue &Hi) {
  841. DebugLoc dl = N->getDebugLoc();
  842. // Expand the incoming operand to be shifted, so that we have its parts
  843. SDValue InL, InH;
  844. GetExpandedInteger(N->getOperand(0), InL, InH);
  845. MVT NVT = InL.getValueType();
  846. unsigned VTBits = N->getValueType(0).getSizeInBits();
  847. unsigned NVTBits = NVT.getSizeInBits();
  848. MVT ShTy = N->getOperand(1).getValueType();
  849. if (N->getOpcode() == ISD::SHL) {
  850. if (Amt > VTBits) {
  851. Lo = Hi = DAG.getConstant(0, NVT);
  852. } else if (Amt > NVTBits) {
  853. Lo = DAG.getConstant(0, NVT);
  854. Hi = DAG.getNode(ISD::SHL, dl,
  855. NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
  856. } else if (Amt == NVTBits) {
  857. Lo = DAG.getConstant(0, NVT);
  858. Hi = InL;
  859. } else if (Amt == 1 &&
  860. TLI.isOperationLegalOrCustom(ISD::ADDC,
  861. TLI.getTypeToExpandTo(NVT))) {
  862. // Emit this X << 1 as X+X.
  863. SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
  864. SDValue LoOps[2] = { InL, InL };
  865. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
  866. SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
  867. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
  868. } else {
  869. Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Amt, ShTy));
  870. Hi = DAG.getNode(ISD::OR, dl, NVT,
  871. DAG.getNode(ISD::SHL, dl, NVT, InH,
  872. DAG.getConstant(Amt, ShTy)),
  873. DAG.getNode(ISD::SRL, dl, NVT, InL,
  874. DAG.getConstant(NVTBits-Amt, ShTy)));
  875. }
  876. return;
  877. }
  878. if (N->getOpcode() == ISD::SRL) {
  879. if (Amt > VTBits) {
  880. Lo = DAG.getConstant(0, NVT);
  881. Hi = DAG.getConstant(0, NVT);
  882. } else if (Amt > NVTBits) {
  883. Lo = DAG.getNode(ISD::SRL, dl,
  884. NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
  885. Hi = DAG.getConstant(0, NVT);
  886. } else if (Amt == NVTBits) {
  887. Lo = InH;
  888. Hi = DAG.getConstant(0, NVT);
  889. } else {
  890. Lo = DAG.getNode(ISD::OR, dl, NVT,
  891. DAG.getNode(ISD::SRL, dl, NVT, InL,
  892. DAG.getConstant(Amt, ShTy)),
  893. DAG.getNode(ISD::SHL, dl, NVT, InH,
  894. DAG.getConstant(NVTBits-Amt, ShTy)));
  895. Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
  896. }
  897. return;
  898. }
  899. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  900. if (Amt > VTBits) {
  901. Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
  902. DAG.getConstant(NVTBits-1, ShTy));
  903. } else if (Amt > NVTBits) {
  904. Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
  905. DAG.getConstant(Amt-NVTBits, ShTy));
  906. Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
  907. DAG.getConstant(NVTBits-1, ShTy));
  908. } else if (Amt == NVTBits) {
  909. Lo = InH;
  910. Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
  911. DAG.getConstant(NVTBits-1, ShTy));
  912. } else {
  913. Lo = DAG.getNode(ISD::OR, dl, NVT,
  914. DAG.getNode(ISD::SRL, dl, NVT, InL,
  915. DAG.getConstant(Amt, ShTy)),
  916. DAG.getNode(ISD::SHL, dl, NVT, InH,
  917. DAG.getConstant(NVTBits-Amt, ShTy)));
  918. Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
  919. }
  920. }
  921. /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
  922. /// this shift based on knowledge of the high bit of the shift amount. If we
  923. /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
  924. /// shift amount.
  925. bool DAGTypeLegalizer::
  926. ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
  927. SDValue Amt = N->getOperand(1);
  928. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  929. MVT ShTy = Amt.getValueType();
  930. unsigned ShBits = ShTy.getSizeInBits();
  931. unsigned NVTBits = NVT.getSizeInBits();
  932. assert(isPowerOf2_32(NVTBits) &&
  933. "Expanded integer type size not a power of two!");
  934. DebugLoc dl = N->getDebugLoc();
  935. APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
  936. APInt KnownZero, KnownOne;
  937. DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
  938. // If we don't know anything about the high bits, exit.
  939. if (((KnownZero|KnownOne) & HighBitMask) == 0)
  940. return false;
  941. // Get the incoming operand to be shifted.
  942. SDValue InL, InH;
  943. GetExpandedInteger(N->getOperand(0), InL, InH);
  944. // If we know that any of the high bits of the shift amount are one, then we
  945. // can do this as a couple of simple shifts.
  946. if (KnownOne.intersects(HighBitMask)) {
  947. // Mask out the high bit, which we know is set.
  948. Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
  949. DAG.getConstant(~HighBitMask, ShTy));
  950. switch (N->getOpcode()) {
  951. default: LLVM_UNREACHABLE("Unknown shift");
  952. case ISD::SHL:
  953. Lo = DAG.getConstant(0, NVT); // Low part is zero.
  954. Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
  955. return true;
  956. case ISD::SRL:
  957. Hi = DAG.getConstant(0, NVT); // Hi part is zero.
  958. Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
  959. return true;
  960. case ISD::SRA:
  961. Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
  962. DAG.getConstant(NVTBits-1, ShTy));
  963. Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
  964. return true;
  965. }
  966. }
  967. #if 0
  968. // FIXME: This code is broken for shifts with a zero amount!
  969. // If we know that all of the high bits of the shift amount are zero, then we
  970. // can do this as a couple of simple shifts.
  971. if ((KnownZero & HighBitMask) == HighBitMask) {
  972. // Compute 32-amt.
  973. SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
  974. DAG.getConstant(NVTBits, ShTy),
  975. Amt);
  976. unsigned Op1, Op2;
  977. switch (N->getOpcode()) {
  978. default: LLVM_UNREACHABLE("Unknown shift");
  979. case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
  980. case ISD::SRL:
  981. case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
  982. }
  983. Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
  984. Hi = DAG.getNode(ISD::OR, NVT,
  985. DAG.getNode(Op1, NVT, InH, Amt),
  986. DAG.getNode(Op2, NVT, InL, Amt2));
  987. return true;
  988. }
  989. #endif
  990. return false;
  991. }
  992. /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
  993. /// of any size.
  994. bool DAGTypeLegalizer::
  995. ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
  996. SDValue Amt = N->getOperand(1);
  997. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  998. MVT ShTy = Amt.getValueType();
  999. unsigned NVTBits = NVT.getSizeInBits();
  1000. assert(isPowerOf2_32(NVTBits) &&
  1001. "Expanded integer type size not a power of two!");
  1002. DebugLoc dl = N->getDebugLoc();
  1003. // Get the incoming operand to be shifted.
  1004. SDValue InL, InH;
  1005. GetExpandedInteger(N->getOperand(0), InL, InH);
  1006. SDValue NVBitsNode = DAG.getConstant(NVTBits, ShTy);
  1007. SDValue Amt2 = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
  1008. SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(ShTy),
  1009. Amt, NVBitsNode, ISD::SETULT);
  1010. SDValue Lo1, Hi1, Lo2, Hi2;
  1011. switch (N->getOpcode()) {
  1012. default: LLVM_UNREACHABLE("Unknown shift");
  1013. case ISD::SHL:
  1014. // ShAmt < NVTBits
  1015. Lo1 = DAG.getConstant(0, NVT); // Low part is zero.
  1016. Hi1 = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
  1017. // ShAmt >= NVTBits
  1018. Lo2 = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
  1019. Hi2 = DAG.getNode(ISD::OR, dl, NVT,
  1020. DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
  1021. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt2));
  1022. Lo = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Lo1, Lo2);
  1023. Hi = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Hi1, Hi2);
  1024. return true;
  1025. case ISD::SRL:
  1026. // ShAmt < NVTBits
  1027. Hi1 = DAG.getConstant(0, NVT); // Hi part is zero.
  1028. Lo1 = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
  1029. // ShAmt >= NVTBits
  1030. Hi2 = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
  1031. Lo2 = DAG.getNode(ISD::OR, dl, NVT,
  1032. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
  1033. DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
  1034. Lo = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Lo1, Lo2);
  1035. Hi = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Hi1, Hi2);
  1036. return true;
  1037. case ISD::SRA:
  1038. // ShAmt < NVTBits
  1039. Hi1 = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
  1040. DAG.getConstant(NVTBits-1, ShTy));
  1041. Lo1 = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
  1042. // ShAmt >= NVTBits
  1043. Hi2 = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
  1044. Lo2 = DAG.getNode(ISD::OR, dl, NVT,
  1045. DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
  1046. DAG.getNode(ISD::SHL, dl, NVT, InH, Amt2));
  1047. Lo = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Lo1, Lo2);
  1048. Hi = DAG.getNode(ISD::SELECT, dl, NVT, Cmp, Hi1, Hi2);
  1049. return true;
  1050. }
  1051. return false;
  1052. }
  1053. void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
  1054. SDValue &Lo, SDValue &Hi) {
  1055. DebugLoc dl = N->getDebugLoc();
  1056. // Expand the subcomponents.
  1057. SDValue LHSL, LHSH, RHSL, RHSH;
  1058. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1059. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1060. MVT NVT = LHSL.getValueType();
  1061. SDValue LoOps[2] = { LHSL, RHSL };
  1062. SDValue HiOps[3] = { LHSH, RHSH };
  1063. // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
  1064. // them. TODO: Teach operation legalization how to expand unsupported
  1065. // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
  1066. // a carry of type MVT::Flag, but there doesn't seem to be any way to
  1067. // generate a value of this type in the expanded code sequence.
  1068. bool hasCarry =
  1069. TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
  1070. ISD::ADDC : ISD::SUBC,
  1071. TLI.getTypeToExpandTo(NVT));
  1072. if (hasCarry) {
  1073. SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
  1074. if (N->getOpcode() == ISD::ADD) {
  1075. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
  1076. HiOps[2] = Lo.getValue(1);
  1077. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
  1078. } else {
  1079. Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
  1080. HiOps[2] = Lo.getValue(1);
  1081. Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
  1082. }
  1083. } else {
  1084. if (N->getOpcode() == ISD::ADD) {
  1085. Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
  1086. Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
  1087. SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[0],
  1088. ISD::SETULT);
  1089. SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
  1090. DAG.getConstant(1, NVT),
  1091. DAG.getConstant(0, NVT));
  1092. SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[1],
  1093. ISD::SETULT);
  1094. SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
  1095. DAG.getConstant(1, NVT), Carry1);
  1096. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
  1097. } else {
  1098. Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
  1099. Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
  1100. SDValue Cmp =
  1101. DAG.getSetCC(dl, TLI.getSetCCResultType(LoOps[0].getValueType()),
  1102. LoOps[0], LoOps[1], ISD::SETULT);
  1103. SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
  1104. DAG.getConstant(1, NVT),
  1105. DAG.getConstant(0, NVT));
  1106. Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
  1107. }
  1108. }
  1109. }
  1110. void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
  1111. SDValue &Lo, SDValue &Hi) {
  1112. // Expand the subcomponents.
  1113. SDValue LHSL, LHSH, RHSL, RHSH;
  1114. DebugLoc dl = N->getDebugLoc();
  1115. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1116. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1117. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
  1118. SDValue LoOps[2] = { LHSL, RHSL };
  1119. SDValue HiOps[3] = { LHSH, RHSH };
  1120. if (N->getOpcode() == ISD::ADDC) {
  1121. Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
  1122. HiOps[2] = Lo.getValue(1);
  1123. Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
  1124. } else {
  1125. Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
  1126. HiOps[2] = Lo.getValue(1);
  1127. Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
  1128. }
  1129. // Legalized the flag result - switch anything that used the old flag to
  1130. // use the new one.
  1131. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  1132. }
  1133. void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
  1134. SDValue &Lo, SDValue &Hi) {
  1135. // Expand the subcomponents.
  1136. SDValue LHSL, LHSH, RHSL, RHSH;
  1137. DebugLoc dl = N->getDebugLoc();
  1138. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1139. GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
  1140. SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
  1141. SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
  1142. SDValue HiOps[3] = { LHSH, RHSH };
  1143. Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps, 3);
  1144. HiOps[2] = Lo.getValue(1);
  1145. Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps, 3);
  1146. // Legalized the flag result - switch anything that used the old flag to
  1147. // use the new one.
  1148. ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
  1149. }
  1150. void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
  1151. SDValue &Lo, SDValue &Hi) {
  1152. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  1153. DebugLoc dl = N->getDebugLoc();
  1154. SDValue Op = N->getOperand(0);
  1155. if (Op.getValueType().bitsLE(NVT)) {
  1156. // The low part is any extension of the input (which degenerates to a copy).
  1157. Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
  1158. Hi = DAG.getUNDEF(NVT); // The high part is undefined.
  1159. } else {
  1160. // For example, extension of an i48 to an i64. The operand type necessarily
  1161. // promotes to the result type, so will end up being expanded too.
  1162. assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
  1163. "Only know how to promote this result!");
  1164. SDValue Res = GetPromotedInteger(Op);
  1165. assert(Res.getValueType() == N->getValueType(0) &&
  1166. "Operand over promoted?");
  1167. // Split the promoted operand. This will simplify when it is expanded.
  1168. SplitInteger(Res, Lo, Hi);
  1169. }
  1170. }
  1171. void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
  1172. SDValue &Lo, SDValue &Hi) {
  1173. DebugLoc dl = N->getDebugLoc();
  1174. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1175. MVT NVT = Lo.getValueType();
  1176. MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  1177. unsigned NVTBits = NVT.getSizeInBits();
  1178. unsigned EVTBits = EVT.getSizeInBits();
  1179. if (NVTBits < EVTBits) {
  1180. Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
  1181. DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
  1182. } else {
  1183. Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
  1184. // The high part replicates the sign bit of Lo, make it explicit.
  1185. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  1186. DAG.getConstant(NVTBits-1, TLI.getPointerTy()));
  1187. }
  1188. }
  1189. void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
  1190. SDValue &Lo, SDValue &Hi) {
  1191. DebugLoc dl = N->getDebugLoc();
  1192. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1193. MVT NVT = Lo.getValueType();
  1194. MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  1195. unsigned NVTBits = NVT.getSizeInBits();
  1196. unsigned EVTBits = EVT.getSizeInBits();
  1197. if (NVTBits < EVTBits) {
  1198. Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
  1199. DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
  1200. } else {
  1201. Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
  1202. // The high part must be zero, make it explicit.
  1203. Hi = DAG.getConstant(0, NVT);
  1204. }
  1205. }
  1206. void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
  1207. SDValue &Lo, SDValue &Hi) {
  1208. DebugLoc dl = N->getDebugLoc();
  1209. GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
  1210. Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
  1211. Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
  1212. }
  1213. void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
  1214. SDValue &Lo, SDValue &Hi) {
  1215. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  1216. unsigned NBitWidth = NVT.getSizeInBits();
  1217. const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
  1218. Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
  1219. Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
  1220. }
  1221. void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
  1222. SDValue &Lo, SDValue &Hi) {
  1223. DebugLoc dl = N->getDebugLoc();
  1224. // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
  1225. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1226. MVT NVT = Lo.getValueType();
  1227. SDValue HiNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Hi,
  1228. DAG.getConstant(0, NVT), ISD::SETNE);
  1229. SDValue LoLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
  1230. SDValue HiLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
  1231. Lo = DAG.getNode(ISD::SELECT, dl, NVT, HiNotZero, HiLZ,
  1232. DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
  1233. DAG.getConstant(NVT.getSizeInBits(), NVT)));
  1234. Hi = DAG.getConstant(0, NVT);
  1235. }
  1236. void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
  1237. SDValue &Lo, SDValue &Hi) {
  1238. DebugLoc dl = N->getDebugLoc();
  1239. // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
  1240. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1241. MVT NVT = Lo.getValueType();
  1242. Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
  1243. DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
  1244. Hi = DAG.getConstant(0, NVT);
  1245. }
  1246. void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
  1247. SDValue &Lo, SDValue &Hi) {
  1248. DebugLoc dl = N->getDebugLoc();
  1249. // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
  1250. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1251. MVT NVT = Lo.getValueType();
  1252. SDValue LoNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo,
  1253. DAG.getConstant(0, NVT), ISD::SETNE);
  1254. SDValue LoLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
  1255. SDValue HiLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
  1256. Lo = DAG.getNode(ISD::SELECT, dl, NVT, LoNotZero, LoLZ,
  1257. DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
  1258. DAG.getConstant(NVT.getSizeInBits(), NVT)));
  1259. Hi = DAG.getConstant(0, NVT);
  1260. }
  1261. void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
  1262. SDValue &Hi) {
  1263. DebugLoc dl = N->getDebugLoc();
  1264. MVT VT = N->getValueType(0);
  1265. SDValue Op = N->getOperand(0);
  1266. RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
  1267. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
  1268. SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*irrelevant*/, dl), Lo, Hi);
  1269. }
  1270. void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
  1271. SDValue &Hi) {
  1272. DebugLoc dl = N->getDebugLoc();
  1273. MVT VT = N->getValueType(0);
  1274. SDValue Op = N->getOperand(0);
  1275. RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
  1276. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
  1277. SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*irrelevant*/, dl), Lo, Hi);
  1278. }
  1279. void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
  1280. SDValue &Lo, SDValue &Hi) {
  1281. if (ISD::isNormalLoad(N)) {
  1282. ExpandRes_NormalLoad(N, Lo, Hi);
  1283. return;
  1284. }
  1285. assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
  1286. MVT VT = N->getValueType(0);
  1287. MVT NVT = TLI.getTypeToTransformTo(VT);
  1288. SDValue Ch = N->getChain();
  1289. SDValue Ptr = N->getBasePtr();
  1290. ISD::LoadExtType ExtType = N->getExtensionType();
  1291. int SVOffset = N->getSrcValueOffset();
  1292. unsigned Alignment = N->getAlignment();
  1293. bool isVolatile = N->isVolatile();
  1294. DebugLoc dl = N->getDebugLoc();
  1295. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1296. if (N->getMemoryVT().bitsLE(NVT)) {
  1297. MVT EVT = N->getMemoryVT();
  1298. Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
  1299. EVT, isVolatile, Alignment);
  1300. // Remember the chain.
  1301. Ch = Lo.getValue(1);
  1302. if (ExtType == ISD::SEXTLOAD) {
  1303. // The high part is obtained by SRA'ing all but one of the bits of the
  1304. // lo part.
  1305. unsigned LoSize = Lo.getValueType().getSizeInBits();
  1306. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  1307. DAG.getConstant(LoSize-1, TLI.getPointerTy()));
  1308. } else if (ExtType == ISD::ZEXTLOAD) {
  1309. // The high part is just a zero.
  1310. Hi = DAG.getConstant(0, NVT);
  1311. } else {
  1312. assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
  1313. // The high part is undefined.
  1314. Hi = DAG.getUNDEF(NVT);
  1315. }
  1316. } else if (TLI.isLittleEndian()) {
  1317. // Little-endian - low bits are at low addresses.
  1318. Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getSrcValue(), SVOffset,
  1319. isVolatile, Alignment);
  1320. unsigned ExcessBits =
  1321. N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
  1322. MVT NEVT = MVT::getIntegerVT(ExcessBits);
  1323. // Increment the pointer to the other half.
  1324. unsigned IncrementSize = NVT.getSizeInBits()/8;
  1325. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  1326. DAG.getIntPtrConstant(IncrementSize));
  1327. Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(),
  1328. SVOffset+IncrementSize, NEVT,
  1329. isVolatile, MinAlign(Alignment, IncrementSize));
  1330. // Build a factor node to remember that this load is independent of the
  1331. // other one.
  1332. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  1333. Hi.getValue(1));
  1334. } else {
  1335. // Big-endian - high bits are at low addresses. Favor aligned loads at
  1336. // the cost of some bit-fiddling.
  1337. MVT EVT = N->getMemoryVT();
  1338. unsigned EBytes = EVT.getStoreSizeInBits()/8;
  1339. unsigned IncrementSize = NVT.getSizeInBits()/8;
  1340. unsigned ExcessBits = (EBytes - IncrementSize)*8;
  1341. // Load both the high bits and maybe some of the low bits.
  1342. Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
  1343. MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
  1344. isVolatile, Alignment);
  1345. // Increment the pointer to the other half.
  1346. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  1347. DAG.getIntPtrConstant(IncrementSize));
  1348. // Load the rest of the low bits.
  1349. Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr, N->getSrcValue(),
  1350. SVOffset+IncrementSize,
  1351. MVT::getIntegerVT(ExcessBits),
  1352. isVolatile, MinAlign(Alignment, IncrementSize));
  1353. // Build a factor node to remember that this load is independent of the
  1354. // other one.
  1355. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  1356. Hi.getValue(1));
  1357. if (ExcessBits < NVT.getSizeInBits()) {
  1358. // Transfer low bits from the bottom of Hi to the top of Lo.
  1359. Lo = DAG.getNode(ISD::OR, dl, NVT, Lo,
  1360. DAG.getNode(ISD::SHL, dl, NVT, Hi,
  1361. DAG.getConstant(ExcessBits,
  1362. TLI.getPointerTy())));
  1363. // Move high bits to the right position in Hi.
  1364. Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl,
  1365. NVT, Hi,
  1366. DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
  1367. TLI.getPointerTy()));
  1368. }
  1369. }
  1370. // Legalized the chain result - switch anything that used the old chain to
  1371. // use the new one.
  1372. ReplaceValueWith(SDValue(N, 1), Ch);
  1373. }
  1374. void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
  1375. SDValue &Lo, SDValue &Hi) {
  1376. DebugLoc dl = N->getDebugLoc();
  1377. SDValue LL, LH, RL, RH;
  1378. GetExpandedInteger(N->getOperand(0), LL, LH);
  1379. GetExpandedInteger(N->getOperand(1), RL, RH);
  1380. Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
  1381. Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
  1382. }
  1383. void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
  1384. SDValue &Lo, SDValue &Hi) {
  1385. MVT VT = N->getValueType(0);
  1386. MVT NVT = TLI.getTypeToTransformTo(VT);
  1387. DebugLoc dl = N->getDebugLoc();
  1388. bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
  1389. bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
  1390. bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
  1391. bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
  1392. if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
  1393. SDValue LL, LH, RL, RH;
  1394. GetExpandedInteger(N->getOperand(0), LL, LH);
  1395. GetExpandedInteger(N->getOperand(1), RL, RH);
  1396. unsigned OuterBitSize = VT.getSizeInBits();
  1397. unsigned InnerBitSize = NVT.getSizeInBits();
  1398. unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
  1399. unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
  1400. APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
  1401. if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
  1402. DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
  1403. // The inputs are both zero-extended.
  1404. if (HasUMUL_LOHI) {
  1405. // We can emit a umul_lohi.
  1406. Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
  1407. Hi = SDValue(Lo.getNode(), 1);
  1408. return;
  1409. }
  1410. if (HasMULHU) {
  1411. // We can emit a mulhu+mul.
  1412. Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
  1413. Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
  1414. return;
  1415. }
  1416. }
  1417. if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
  1418. // The input values are both sign-extended.
  1419. if (HasSMUL_LOHI) {
  1420. // We can emit a smul_lohi.
  1421. Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
  1422. Hi = SDValue(Lo.getNode(), 1);
  1423. return;
  1424. }
  1425. if (HasMULHS) {
  1426. // We can emit a mulhs+mul.
  1427. Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
  1428. Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
  1429. return;
  1430. }
  1431. }
  1432. if (HasUMUL_LOHI) {
  1433. // Lo,Hi = umul LHS, RHS.
  1434. SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
  1435. DAG.getVTList(NVT, NVT), LL, RL);
  1436. Lo = UMulLOHI;
  1437. Hi = UMulLOHI.getValue(1);
  1438. RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
  1439. LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
  1440. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
  1441. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
  1442. return;
  1443. }
  1444. if (HasMULHU) {
  1445. Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
  1446. Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
  1447. RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
  1448. LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
  1449. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
  1450. Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
  1451. return;
  1452. }
  1453. }
  1454. // If nothing else, we can make a libcall.
  1455. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1456. if (VT == MVT::i16)
  1457. LC = RTLIB::MUL_I16;
  1458. else if (VT == MVT::i32)
  1459. LC = RTLIB::MUL_I32;
  1460. else if (VT == MVT::i64)
  1461. LC = RTLIB::MUL_I64;
  1462. else if (VT == MVT::i128)
  1463. LC = RTLIB::MUL_I128;
  1464. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
  1465. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1466. SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*irrelevant*/, dl), Lo, Hi);
  1467. }
  1468. void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
  1469. SDValue &Lo, SDValue &Hi) {
  1470. MVT VT = N->getValueType(0);
  1471. DebugLoc dl = N->getDebugLoc();
  1472. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1473. if (VT == MVT::i16)
  1474. LC = RTLIB::SDIV_I16;
  1475. else if (VT == MVT::i32)
  1476. LC = RTLIB::SDIV_I32;
  1477. else if (VT == MVT::i64)
  1478. LC = RTLIB::SDIV_I64;
  1479. else if (VT == MVT::i128)
  1480. LC = RTLIB::SDIV_I128;
  1481. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
  1482. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1483. SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
  1484. }
  1485. void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
  1486. SDValue &Lo, SDValue &Hi) {
  1487. MVT VT = N->getValueType(0);
  1488. DebugLoc dl = N->getDebugLoc();
  1489. // If we can emit an efficient shift operation, do so now. Check to see if
  1490. // the RHS is a constant.
  1491. if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
  1492. return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
  1493. // If we can determine that the high bit of the shift is zero or one, even if
  1494. // the low bits are variable, emit this shift in an optimized form.
  1495. if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
  1496. return;
  1497. // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
  1498. unsigned PartsOpc;
  1499. if (N->getOpcode() == ISD::SHL) {
  1500. PartsOpc = ISD::SHL_PARTS;
  1501. } else if (N->getOpcode() == ISD::SRL) {
  1502. PartsOpc = ISD::SRL_PARTS;
  1503. } else {
  1504. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  1505. PartsOpc = ISD::SRA_PARTS;
  1506. }
  1507. // Next check to see if the target supports this SHL_PARTS operation or if it
  1508. // will custom expand it.
  1509. MVT NVT = TLI.getTypeToTransformTo(VT);
  1510. TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
  1511. if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
  1512. Action == TargetLowering::Custom) {
  1513. // Expand the subcomponents.
  1514. SDValue LHSL, LHSH;
  1515. GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
  1516. SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
  1517. MVT VT = LHSL.getValueType();
  1518. Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops, 3);
  1519. Hi = Lo.getValue(1);
  1520. return;
  1521. }
  1522. // Otherwise, emit a libcall.
  1523. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1524. bool isSigned;
  1525. if (N->getOpcode() == ISD::SHL) {
  1526. isSigned = false; /*sign irrelevant*/
  1527. if (VT == MVT::i16)
  1528. LC = RTLIB::SHL_I16;
  1529. else if (VT == MVT::i32)
  1530. LC = RTLIB::SHL_I32;
  1531. else if (VT == MVT::i64)
  1532. LC = RTLIB::SHL_I64;
  1533. else if (VT == MVT::i128)
  1534. LC = RTLIB::SHL_I128;
  1535. } else if (N->getOpcode() == ISD::SRL) {
  1536. isSigned = false;
  1537. if (VT == MVT::i16)
  1538. LC = RTLIB::SRL_I16;
  1539. else if (VT == MVT::i32)
  1540. LC = RTLIB::SRL_I32;
  1541. else if (VT == MVT::i64)
  1542. LC = RTLIB::SRL_I64;
  1543. else if (VT == MVT::i128)
  1544. LC = RTLIB::SRL_I128;
  1545. } else {
  1546. assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
  1547. isSigned = true;
  1548. if (VT == MVT::i16)
  1549. LC = RTLIB::SRA_I16;
  1550. else if (VT == MVT::i32)
  1551. LC = RTLIB::SRA_I32;
  1552. else if (VT == MVT::i64)
  1553. LC = RTLIB::SRA_I64;
  1554. else if (VT == MVT::i128)
  1555. LC = RTLIB::SRA_I128;
  1556. }
  1557. if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
  1558. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1559. SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned, dl), Lo, Hi);
  1560. return;
  1561. }
  1562. if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
  1563. LLVM_UNREACHABLE("Unsupported shift!");
  1564. }
  1565. void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
  1566. SDValue &Lo, SDValue &Hi) {
  1567. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  1568. DebugLoc dl = N->getDebugLoc();
  1569. SDValue Op = N->getOperand(0);
  1570. if (Op.getValueType().bitsLE(NVT)) {
  1571. // The low part is sign extension of the input (degenerates to a copy).
  1572. Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
  1573. // The high part is obtained by SRA'ing all but one of the bits of low part.
  1574. unsigned LoSize = NVT.getSizeInBits();
  1575. Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
  1576. DAG.getConstant(LoSize-1, TLI.getPointerTy()));
  1577. } else {
  1578. // For example, extension of an i48 to an i64. The operand type necessarily
  1579. // promotes to the result type, so will end up being expanded too.
  1580. assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
  1581. "Only know how to promote this result!");
  1582. SDValue Res = GetPromotedInteger(Op);
  1583. assert(Res.getValueType() == N->getValueType(0) &&
  1584. "Operand over promoted?");
  1585. // Split the promoted operand. This will simplify when it is expanded.
  1586. SplitInteger(Res, Lo, Hi);
  1587. unsigned ExcessBits =
  1588. Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
  1589. Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
  1590. DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
  1591. }
  1592. }
  1593. void DAGTypeLegalizer::
  1594. ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
  1595. DebugLoc dl = N->getDebugLoc();
  1596. GetExpandedInteger(N->getOperand(0), Lo, Hi);
  1597. MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
  1598. if (EVT.bitsLE(Lo.getValueType())) {
  1599. // sext_inreg the low part if needed.
  1600. Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
  1601. N->getOperand(1));
  1602. // The high part gets the sign extension from the lo-part. This handles
  1603. // things like sextinreg V:i64 from i8.
  1604. Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
  1605. DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
  1606. TLI.getPointerTy()));
  1607. } else {
  1608. // For example, extension of an i48 to an i64. Leave the low part alone,
  1609. // sext_inreg the high part.
  1610. unsigned ExcessBits =
  1611. EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
  1612. Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
  1613. DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
  1614. }
  1615. }
  1616. void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
  1617. SDValue &Lo, SDValue &Hi) {
  1618. MVT VT = N->getValueType(0);
  1619. DebugLoc dl = N->getDebugLoc();
  1620. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1621. if (VT == MVT::i16)
  1622. LC = RTLIB::SREM_I16;
  1623. else if (VT == MVT::i32)
  1624. LC = RTLIB::SREM_I32;
  1625. else if (VT == MVT::i64)
  1626. LC = RTLIB::SREM_I64;
  1627. else if (VT == MVT::i128)
  1628. LC = RTLIB::SREM_I128;
  1629. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
  1630. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1631. SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
  1632. }
  1633. void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
  1634. SDValue &Lo, SDValue &Hi) {
  1635. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  1636. DebugLoc dl = N->getDebugLoc();
  1637. Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
  1638. Hi = DAG.getNode(ISD::SRL, dl,
  1639. N->getOperand(0).getValueType(), N->getOperand(0),
  1640. DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy()));
  1641. Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
  1642. }
  1643. void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
  1644. SDValue &Lo, SDValue &Hi) {
  1645. MVT VT = N->getValueType(0);
  1646. DebugLoc dl = N->getDebugLoc();
  1647. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1648. if (VT == MVT::i16)
  1649. LC = RTLIB::UDIV_I16;
  1650. else if (VT == MVT::i32)
  1651. LC = RTLIB::UDIV_I32;
  1652. else if (VT == MVT::i64)
  1653. LC = RTLIB::UDIV_I64;
  1654. else if (VT == MVT::i128)
  1655. LC = RTLIB::UDIV_I128;
  1656. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
  1657. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1658. SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
  1659. }
  1660. void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
  1661. SDValue &Lo, SDValue &Hi) {
  1662. MVT VT = N->getValueType(0);
  1663. DebugLoc dl = N->getDebugLoc();
  1664. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  1665. if (VT == MVT::i16)
  1666. LC = RTLIB::UREM_I16;
  1667. else if (VT == MVT::i32)
  1668. LC = RTLIB::UREM_I32;
  1669. else if (VT == MVT::i64)
  1670. LC = RTLIB::UREM_I64;
  1671. else if (VT == MVT::i128)
  1672. LC = RTLIB::UREM_I128;
  1673. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
  1674. SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
  1675. SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
  1676. }
  1677. void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
  1678. SDValue &Lo, SDValue &Hi) {
  1679. MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
  1680. DebugLoc dl = N->getDebugLoc();
  1681. SDValue Op = N->getOperand(0);
  1682. if (Op.getValueType().bitsLE(NVT)) {
  1683. // The low part is zero extension of the input (degenerates to a copy).
  1684. Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
  1685. Hi = DAG.getConstant(0, NVT); // The high part is just a zero.
  1686. } else {
  1687. // For example, extension of an i48 to an i64. The operand type necessarily
  1688. // promotes to the result type, so will end up being expanded too.
  1689. assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
  1690. "Only know how to promote this result!");
  1691. SDValue Res = GetPromotedInteger(Op);
  1692. assert(Res.getValueType() == N->getValueType(0) &&
  1693. "Operand over promoted?");
  1694. // Split the promoted operand. This will simplify when it is expanded.
  1695. SplitInteger(Res, Lo, Hi);
  1696. unsigned ExcessBits =
  1697. Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
  1698. Hi = DAG.getZeroExtendInReg(Hi, dl, MVT::getIntegerVT(ExcessBits));
  1699. }
  1700. }
  1701. //===----------------------------------------------------------------------===//
  1702. // Integer Operand Expansion
  1703. //===----------------------------------------------------------------------===//
  1704. /// ExpandIntegerOperand - This method is called when the specified operand of
  1705. /// the specified node is found to need expansion. At this point, all of the
  1706. /// result types of the node are known to be legal, but other operands of the
  1707. /// node may need promotion or expansion as well as the specified one.
  1708. bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
  1709. DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
  1710. SDValue Res = SDValue();
  1711. if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
  1712. return false;
  1713. switch (N->getOpcode()) {
  1714. default:
  1715. #ifndef NDEBUG
  1716. cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
  1717. N->dump(&DAG); cerr << "\n";
  1718. #endif
  1719. LLVM_UNREACHABLE("Do not know how to expand this operator's operand!");
  1720. case ISD::BIT_CONVERT: Res = ExpandOp_BIT_CONVERT(N); break;
  1721. case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
  1722. case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
  1723. case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
  1724. case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
  1725. case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
  1726. case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
  1727. case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
  1728. case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
  1729. case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
  1730. case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
  1731. case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
  1732. case ISD::SHL:
  1733. case ISD::SRA:
  1734. case ISD::SRL:
  1735. case ISD::ROTL:
  1736. case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
  1737. }
  1738. // If the result is null, the sub-method took care of registering results etc.
  1739. if (!Res.getNode()) return false;
  1740. // If the result is N, the sub-method updated N in place. Tell the legalizer
  1741. // core about this.
  1742. if (Res.getNode() == N)
  1743. return true;
  1744. assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
  1745. "Invalid operand expansion");
  1746. ReplaceValueWith(SDValue(N, 0), Res);
  1747. return false;
  1748. }
  1749. /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
  1750. /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
  1751. void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
  1752. SDValue &NewRHS,
  1753. ISD::CondCode &CCCode,
  1754. DebugLoc dl) {
  1755. SDValue LHSLo, LHSHi, RHSLo, RHSHi;
  1756. GetExpandedInteger(NewLHS, LHSLo, LHSHi);
  1757. GetExpandedInteger(NewRHS, RHSLo, RHSHi);
  1758. MVT VT = NewLHS.getValueType();
  1759. if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
  1760. if (RHSLo == RHSHi) {
  1761. if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
  1762. if (RHSCST->isAllOnesValue()) {
  1763. // Equality comparison to -1.
  1764. NewLHS = DAG.getNode(ISD::AND, dl,
  1765. LHSLo.getValueType(), LHSLo, LHSHi);
  1766. NewRHS = RHSLo;
  1767. return;
  1768. }
  1769. }
  1770. }
  1771. NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
  1772. NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
  1773. NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
  1774. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  1775. return;
  1776. }
  1777. // If this is a comparison of the sign bit, just look at the top part.
  1778. // X > -1, x < 0
  1779. if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
  1780. if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
  1781. (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
  1782. NewLHS = LHSHi;
  1783. NewRHS = RHSHi;
  1784. return;
  1785. }
  1786. // FIXME: This generated code sucks.
  1787. ISD::CondCode LowCC;
  1788. switch (CCCode) {
  1789. default: LLVM_UNREACHABLE("Unknown integer setcc!");
  1790. case ISD::SETLT:
  1791. case ISD::SETULT: LowCC = ISD::SETULT; break;
  1792. case ISD::SETGT:
  1793. case ISD::SETUGT: LowCC = ISD::SETUGT; break;
  1794. case ISD::SETLE:
  1795. case ISD::SETULE: LowCC = ISD::SETULE; break;
  1796. case ISD::SETGE:
  1797. case ISD::SETUGE: LowCC = ISD::SETUGE; break;
  1798. }
  1799. // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
  1800. // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
  1801. // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
  1802. // NOTE: on targets without efficient SELECT of bools, we can always use
  1803. // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
  1804. TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
  1805. SDValue Tmp1, Tmp2;
  1806. Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
  1807. LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
  1808. if (!Tmp1.getNode())
  1809. Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
  1810. LHSLo, RHSLo, LowCC);
  1811. Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
  1812. LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
  1813. if (!Tmp2.getNode())
  1814. Tmp2 = DAG.getNode(ISD::SETCC, dl,
  1815. TLI.getSetCCResultType(LHSHi.getValueType()),
  1816. LHSHi, RHSHi, DAG.getCondCode(CCCode));
  1817. ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
  1818. ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
  1819. if ((Tmp1C && Tmp1C->isNullValue()) ||
  1820. (Tmp2C && Tmp2C->isNullValue() &&
  1821. (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
  1822. CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
  1823. (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
  1824. (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
  1825. CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
  1826. // low part is known false, returns high part.
  1827. // For LE / GE, if high part is known false, ignore the low part.
  1828. // For LT / GT, if high part is known true, ignore the low part.
  1829. NewLHS = Tmp2;
  1830. NewRHS = SDValue();
  1831. return;
  1832. }
  1833. NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
  1834. LHSHi, RHSHi, ISD::SETEQ, false,
  1835. DagCombineInfo, dl);
  1836. if (!NewLHS.getNode())
  1837. NewLHS = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
  1838. LHSHi, RHSHi, ISD::SETEQ);
  1839. NewLHS = DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
  1840. NewLHS, Tmp1, Tmp2);
  1841. NewRHS = SDValue();
  1842. }
  1843. SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
  1844. SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
  1845. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
  1846. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
  1847. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  1848. // against zero to select between true and false values.
  1849. if (NewRHS.getNode() == 0) {
  1850. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  1851. CCCode = ISD::SETNE;
  1852. }
  1853. // Update N to have the operands specified.
  1854. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
  1855. DAG.getCondCode(CCCode), NewLHS, NewRHS,
  1856. N->getOperand(4));
  1857. }
  1858. SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
  1859. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  1860. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
  1861. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
  1862. // If ExpandSetCCOperands returned a scalar, we need to compare the result
  1863. // against zero to select between true and false values.
  1864. if (NewRHS.getNode() == 0) {
  1865. NewRHS = DAG.getConstant(0, NewLHS.getValueType());
  1866. CCCode = ISD::SETNE;
  1867. }
  1868. // Update N to have the operands specified.
  1869. return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
  1870. N->getOperand(2), N->getOperand(3),
  1871. DAG.getCondCode(CCCode));
  1872. }
  1873. SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
  1874. SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
  1875. ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
  1876. IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
  1877. // If ExpandSetCCOperands returned a scalar, use it.
  1878. if (NewRHS.getNode() == 0) {
  1879. assert(NewLHS.getValueType() == N->getValueType(0) &&
  1880. "Unexpected setcc expansion!");
  1881. return NewLHS;
  1882. }
  1883. // Otherwise, update N to have the operands specified.
  1884. return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
  1885. DAG.getCondCode(CCCode));
  1886. }
  1887. SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
  1888. // The value being shifted is legal, but the shift amount is too big.
  1889. // It follows that either the result of the shift is undefined, or the
  1890. // upper half of the shift amount is zero. Just use the lower half.
  1891. SDValue Lo, Hi;
  1892. GetExpandedInteger(N->getOperand(1), Lo, Hi);
  1893. return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Lo);
  1894. }
  1895. SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
  1896. SDValue Op = N->getOperand(0);
  1897. MVT DstVT = N->getValueType(0);
  1898. RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
  1899. assert(LC != RTLIB::UNKNOWN_LIBCALL &&
  1900. "Don't know how to expand this SINT_TO_FP!");
  1901. return MakeLibCall(LC, DstVT, &Op, 1, true, N->getDebugLoc());
  1902. }
  1903. SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
  1904. if (ISD::isNormalStore(N))
  1905. return ExpandOp_NormalStore(N, OpNo);
  1906. assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
  1907. assert(OpNo == 1 && "Can only expand the stored value so far");
  1908. MVT VT = N->getOperand(1).getValueType();
  1909. MVT NVT = TLI.getTypeToTransformTo(VT);
  1910. SDValue Ch = N->getChain();
  1911. SDValue Ptr = N->getBasePtr();
  1912. int SVOffset = N->getSrcValueOffset();
  1913. unsigned Alignment = N->getAlignment();
  1914. bool isVolatile = N->isVolatile();
  1915. DebugLoc dl = N->getDebugLoc();
  1916. SDValue Lo, Hi;
  1917. assert(NVT.isByteSized() && "Expanded type not byte sized!");
  1918. if (N->getMemoryVT().bitsLE(NVT)) {
  1919. GetExpandedInteger(N->getValue(), Lo, Hi);
  1920. return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
  1921. N->getMemoryVT(), isVolatile, Alignment);
  1922. } else if (TLI.isLittleEndian()) {
  1923. // Little-endian - low bits are at low addresses.
  1924. GetExpandedInteger(N->getValue(), Lo, Hi);
  1925. Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
  1926. isVolatile, Alignment);
  1927. unsigned ExcessBits =
  1928. N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
  1929. MVT NEVT = MVT::getIntegerVT(ExcessBits);
  1930. // Increment the pointer to the other half.
  1931. unsigned IncrementSize = NVT.getSizeInBits()/8;
  1932. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  1933. DAG.getIntPtrConstant(IncrementSize));
  1934. Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
  1935. SVOffset+IncrementSize, NEVT,
  1936. isVolatile, MinAlign(Alignment, IncrementSize));
  1937. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  1938. } else {
  1939. // Big-endian - high bits are at low addresses. Favor aligned stores at
  1940. // the cost of some bit-fiddling.
  1941. GetExpandedInteger(N->getValue(), Lo, Hi);
  1942. MVT EVT = N->getMemoryVT();
  1943. unsigned EBytes = EVT.getStoreSizeInBits()/8;
  1944. unsigned IncrementSize = NVT.getSizeInBits()/8;
  1945. unsigned ExcessBits = (EBytes - IncrementSize)*8;
  1946. MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
  1947. if (ExcessBits < NVT.getSizeInBits()) {
  1948. // Transfer high bits from the top of Lo to the bottom of Hi.
  1949. Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
  1950. DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
  1951. TLI.getPointerTy()));
  1952. Hi = DAG.getNode(ISD::OR, dl, NVT, Hi,
  1953. DAG.getNode(ISD::SRL, dl, NVT, Lo,
  1954. DAG.getConstant(ExcessBits,
  1955. TLI.getPointerTy())));
  1956. }
  1957. // Store both the high bits and maybe some of the low bits.
  1958. Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
  1959. SVOffset, HiVT, isVolatile, Alignment);
  1960. // Increment the pointer to the other half.
  1961. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  1962. DAG.getIntPtrConstant(IncrementSize));
  1963. // Store the lowest ExcessBits bits in the second half.
  1964. Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(),
  1965. SVOffset+IncrementSize,
  1966. MVT::getIntegerVT(ExcessBits),
  1967. isVolatile, MinAlign(Alignment, IncrementSize));
  1968. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  1969. }
  1970. }
  1971. SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
  1972. SDValue InL, InH;
  1973. GetExpandedInteger(N->getOperand(0), InL, InH);
  1974. // Just truncate the low part of the source.
  1975. return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), InL);
  1976. }
  1977. SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
  1978. SDValue Op = N->getOperand(0);
  1979. MVT SrcVT = Op.getValueType();
  1980. MVT DstVT = N->getValueType(0);
  1981. DebugLoc dl = N->getDebugLoc();
  1982. if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
  1983. // Do a signed conversion then adjust the result.
  1984. SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
  1985. SignedConv = TLI.LowerOperation(SignedConv, DAG);
  1986. // The result of the signed conversion needs adjusting if the 'sign bit' of
  1987. // the incoming integer was set. To handle this, we dynamically test to see
  1988. // if it is set, and, if so, add a fudge factor.
  1989. const uint64_t F32TwoE32 = 0x4F800000ULL;
  1990. const uint64_t F32TwoE64 = 0x5F800000ULL;
  1991. const uint64_t F32TwoE128 = 0x7F800000ULL;
  1992. APInt FF(32, 0);
  1993. if (SrcVT == MVT::i32)
  1994. FF = APInt(32, F32TwoE32);
  1995. else if (SrcVT == MVT::i64)
  1996. FF = APInt(32, F32TwoE64);
  1997. else if (SrcVT == MVT::i128)
  1998. FF = APInt(32, F32TwoE128);
  1999. else
  2000. assert(false && "Unsupported UINT_TO_FP!");
  2001. // Check whether the sign bit is set.
  2002. SDValue Lo, Hi;
  2003. GetExpandedInteger(Op, Lo, Hi);
  2004. SDValue SignSet = DAG.getSetCC(dl,
  2005. TLI.getSetCCResultType(Hi.getValueType()),
  2006. Hi, DAG.getConstant(0, Hi.getValueType()),
  2007. ISD::SETLT);
  2008. // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
  2009. SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
  2010. TLI.getPointerTy());
  2011. // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
  2012. SDValue Zero = DAG.getIntPtrConstant(0);
  2013. SDValue Four = DAG.getIntPtrConstant(4);
  2014. if (TLI.isBigEndian()) std::swap(Zero, Four);
  2015. SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
  2016. Zero, Four);
  2017. unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
  2018. FudgePtr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), FudgePtr, Offset);
  2019. Alignment = std::min(Alignment, 4u);
  2020. // Load the value out, extending it from f32 to the destination float type.
  2021. // FIXME: Avoid the extend by constructing the right constant pool?
  2022. SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
  2023. FudgePtr, NULL, 0, MVT::f32,
  2024. false, Alignment);
  2025. return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
  2026. }
  2027. // Otherwise, use a libcall.
  2028. RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
  2029. assert(LC != RTLIB::UNKNOWN_LIBCALL &&
  2030. "Don't know how to expand this UINT_TO_FP!");
  2031. return MakeLibCall(LC, DstVT, &Op, 1, true, dl);
  2032. }