LegalizeDAG.cpp 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927
  1. //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
  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 the SelectionDAG::Legalize method.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/SelectionDAG.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/CodeGen/Analysis.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  20. #include "llvm/DebugInfo.h"
  21. #include "llvm/IR/CallingConv.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/DataLayout.h"
  24. #include "llvm/IR/DerivedTypes.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/MathExtras.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "llvm/Target/TargetFrameLowering.h"
  32. #include "llvm/Target/TargetLowering.h"
  33. #include "llvm/Target/TargetMachine.h"
  34. using namespace llvm;
  35. //===----------------------------------------------------------------------===//
  36. /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
  37. /// hacks on it until the target machine can handle it. This involves
  38. /// eliminating value sizes the machine cannot handle (promoting small sizes to
  39. /// large sizes or splitting up large values into small values) as well as
  40. /// eliminating operations the machine cannot handle.
  41. ///
  42. /// This code also does a small amount of optimization and recognition of idioms
  43. /// as part of its processing. For example, if a target does not support a
  44. /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
  45. /// will attempt merge setcc and brc instructions into brcc's.
  46. ///
  47. namespace {
  48. class SelectionDAGLegalize : public SelectionDAG::DAGUpdateListener {
  49. const TargetMachine &TM;
  50. const TargetLowering &TLI;
  51. SelectionDAG &DAG;
  52. /// LegalizePosition - The iterator for walking through the node list.
  53. SelectionDAG::allnodes_iterator LegalizePosition;
  54. /// LegalizedNodes - The set of nodes which have already been legalized.
  55. SmallPtrSet<SDNode *, 16> LegalizedNodes;
  56. EVT getSetCCResultType(EVT VT) const {
  57. return TLI.getSetCCResultType(*DAG.getContext(), VT);
  58. }
  59. // Libcall insertion helpers.
  60. public:
  61. explicit SelectionDAGLegalize(SelectionDAG &DAG);
  62. void LegalizeDAG();
  63. private:
  64. /// LegalizeOp - Legalizes the given operation.
  65. void LegalizeOp(SDNode *Node);
  66. SDValue OptimizeFloatStore(StoreSDNode *ST);
  67. void LegalizeLoadOps(SDNode *Node);
  68. void LegalizeStoreOps(SDNode *Node);
  69. /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
  70. /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
  71. /// is necessary to spill the vector being inserted into to memory, perform
  72. /// the insert there, and then read the result back.
  73. SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
  74. SDValue Idx, SDLoc dl);
  75. SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
  76. SDValue Idx, SDLoc dl);
  77. /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
  78. /// performs the same shuffe in terms of order or result bytes, but on a type
  79. /// whose vector element type is narrower than the original shuffle type.
  80. /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
  81. SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl,
  82. SDValue N1, SDValue N2,
  83. ArrayRef<int> Mask) const;
  84. void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
  85. SDLoc dl);
  86. SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
  87. SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops,
  88. unsigned NumOps, bool isSigned, SDLoc dl);
  89. std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
  90. SDNode *Node, bool isSigned);
  91. SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
  92. RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
  93. RTLIB::Libcall Call_F128,
  94. RTLIB::Libcall Call_PPCF128);
  95. SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
  96. RTLIB::Libcall Call_I8,
  97. RTLIB::Libcall Call_I16,
  98. RTLIB::Libcall Call_I32,
  99. RTLIB::Libcall Call_I64,
  100. RTLIB::Libcall Call_I128);
  101. void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  102. void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
  103. SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, SDLoc dl);
  104. SDValue ExpandBUILD_VECTOR(SDNode *Node);
  105. SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
  106. void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
  107. SmallVectorImpl<SDValue> &Results);
  108. SDValue ExpandFCOPYSIGN(SDNode *Node);
  109. SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
  110. SDLoc dl);
  111. SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
  112. SDLoc dl);
  113. SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
  114. SDLoc dl);
  115. SDValue ExpandBSWAP(SDValue Op, SDLoc dl);
  116. SDValue ExpandBitCount(unsigned Opc, SDValue Op, SDLoc dl);
  117. SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
  118. SDValue ExpandInsertToVectorThroughStack(SDValue Op);
  119. SDValue ExpandVectorBuildThroughStack(SDNode* Node);
  120. SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
  121. std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
  122. void ExpandNode(SDNode *Node);
  123. void PromoteNode(SDNode *Node);
  124. void ForgetNode(SDNode *N) {
  125. LegalizedNodes.erase(N);
  126. if (LegalizePosition == SelectionDAG::allnodes_iterator(N))
  127. ++LegalizePosition;
  128. }
  129. public:
  130. // DAGUpdateListener implementation.
  131. virtual void NodeDeleted(SDNode *N, SDNode *E) {
  132. ForgetNode(N);
  133. }
  134. virtual void NodeUpdated(SDNode *N) {}
  135. // Node replacement helpers
  136. void ReplacedNode(SDNode *N) {
  137. if (N->use_empty()) {
  138. DAG.RemoveDeadNode(N);
  139. } else {
  140. ForgetNode(N);
  141. }
  142. }
  143. void ReplaceNode(SDNode *Old, SDNode *New) {
  144. DAG.ReplaceAllUsesWith(Old, New);
  145. ReplacedNode(Old);
  146. }
  147. void ReplaceNode(SDValue Old, SDValue New) {
  148. DAG.ReplaceAllUsesWith(Old, New);
  149. ReplacedNode(Old.getNode());
  150. }
  151. void ReplaceNode(SDNode *Old, const SDValue *New) {
  152. DAG.ReplaceAllUsesWith(Old, New);
  153. ReplacedNode(Old);
  154. }
  155. };
  156. }
  157. /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
  158. /// performs the same shuffe in terms of order or result bytes, but on a type
  159. /// whose vector element type is narrower than the original shuffle type.
  160. /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
  161. SDValue
  162. SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, SDLoc dl,
  163. SDValue N1, SDValue N2,
  164. ArrayRef<int> Mask) const {
  165. unsigned NumMaskElts = VT.getVectorNumElements();
  166. unsigned NumDestElts = NVT.getVectorNumElements();
  167. unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
  168. assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
  169. if (NumEltsGrowth == 1)
  170. return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
  171. SmallVector<int, 8> NewMask;
  172. for (unsigned i = 0; i != NumMaskElts; ++i) {
  173. int Idx = Mask[i];
  174. for (unsigned j = 0; j != NumEltsGrowth; ++j) {
  175. if (Idx < 0)
  176. NewMask.push_back(-1);
  177. else
  178. NewMask.push_back(Idx * NumEltsGrowth + j);
  179. }
  180. }
  181. assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
  182. assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
  183. return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
  184. }
  185. SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
  186. : SelectionDAG::DAGUpdateListener(dag),
  187. TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
  188. DAG(dag) {
  189. }
  190. void SelectionDAGLegalize::LegalizeDAG() {
  191. DAG.AssignTopologicalOrder();
  192. // Visit all the nodes. We start in topological order, so that we see
  193. // nodes with their original operands intact. Legalization can produce
  194. // new nodes which may themselves need to be legalized. Iterate until all
  195. // nodes have been legalized.
  196. for (;;) {
  197. bool AnyLegalized = false;
  198. for (LegalizePosition = DAG.allnodes_end();
  199. LegalizePosition != DAG.allnodes_begin(); ) {
  200. --LegalizePosition;
  201. SDNode *N = LegalizePosition;
  202. if (LegalizedNodes.insert(N)) {
  203. AnyLegalized = true;
  204. LegalizeOp(N);
  205. }
  206. }
  207. if (!AnyLegalized)
  208. break;
  209. }
  210. // Remove dead nodes now.
  211. DAG.RemoveDeadNodes();
  212. }
  213. /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
  214. /// a load from the constant pool.
  215. SDValue
  216. SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
  217. bool Extend = false;
  218. SDLoc dl(CFP);
  219. // If a FP immediate is precise when represented as a float and if the
  220. // target can do an extending load from float to double, we put it into
  221. // the constant pool as a float, even if it's is statically typed as a
  222. // double. This shrinks FP constants and canonicalizes them for targets where
  223. // an FP extending load is the same cost as a normal load (such as on the x87
  224. // fp stack or PPC FP unit).
  225. EVT VT = CFP->getValueType(0);
  226. ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
  227. if (!UseCP) {
  228. assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
  229. return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
  230. (VT == MVT::f64) ? MVT::i64 : MVT::i32);
  231. }
  232. EVT OrigVT = VT;
  233. EVT SVT = VT;
  234. while (SVT != MVT::f32) {
  235. SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
  236. if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
  237. // Only do this if the target has a native EXTLOAD instruction from
  238. // smaller type.
  239. TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
  240. TLI.ShouldShrinkFPConstant(OrigVT)) {
  241. Type *SType = SVT.getTypeForEVT(*DAG.getContext());
  242. LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
  243. VT = SVT;
  244. Extend = true;
  245. }
  246. }
  247. SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
  248. unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
  249. if (Extend) {
  250. SDValue Result =
  251. DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
  252. DAG.getEntryNode(),
  253. CPIdx, MachinePointerInfo::getConstantPool(),
  254. VT, false, false, Alignment);
  255. return Result;
  256. }
  257. SDValue Result =
  258. DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
  259. MachinePointerInfo::getConstantPool(), false, false, false,
  260. Alignment);
  261. return Result;
  262. }
  263. /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
  264. static void ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
  265. const TargetLowering &TLI,
  266. SelectionDAGLegalize *DAGLegalize) {
  267. assert(ST->getAddressingMode() == ISD::UNINDEXED &&
  268. "unaligned indexed stores not implemented!");
  269. SDValue Chain = ST->getChain();
  270. SDValue Ptr = ST->getBasePtr();
  271. SDValue Val = ST->getValue();
  272. EVT VT = Val.getValueType();
  273. int Alignment = ST->getAlignment();
  274. SDLoc dl(ST);
  275. if (ST->getMemoryVT().isFloatingPoint() ||
  276. ST->getMemoryVT().isVector()) {
  277. EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
  278. if (TLI.isTypeLegal(intVT)) {
  279. // Expand to a bitconvert of the value to the integer type of the
  280. // same size, then a (misaligned) int store.
  281. // FIXME: Does not handle truncating floating point stores!
  282. SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
  283. Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
  284. ST->isVolatile(), ST->isNonTemporal(), Alignment);
  285. DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
  286. return;
  287. }
  288. // Do a (aligned) store to a stack slot, then copy from the stack slot
  289. // to the final destination using (unaligned) integer loads and stores.
  290. EVT StoredVT = ST->getMemoryVT();
  291. MVT RegVT =
  292. TLI.getRegisterType(*DAG.getContext(),
  293. EVT::getIntegerVT(*DAG.getContext(),
  294. StoredVT.getSizeInBits()));
  295. unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
  296. unsigned RegBytes = RegVT.getSizeInBits() / 8;
  297. unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
  298. // Make sure the stack slot is also aligned for the register type.
  299. SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
  300. // Perform the original store, only redirected to the stack slot.
  301. SDValue Store = DAG.getTruncStore(Chain, dl,
  302. Val, StackPtr, MachinePointerInfo(),
  303. StoredVT, false, false, 0);
  304. SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
  305. SmallVector<SDValue, 8> Stores;
  306. unsigned Offset = 0;
  307. // Do all but one copies using the full register width.
  308. for (unsigned i = 1; i < NumRegs; i++) {
  309. // Load one integer register's worth from the stack slot.
  310. SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
  311. MachinePointerInfo(),
  312. false, false, false, 0);
  313. // Store it to the final location. Remember the store.
  314. Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
  315. ST->getPointerInfo().getWithOffset(Offset),
  316. ST->isVolatile(), ST->isNonTemporal(),
  317. MinAlign(ST->getAlignment(), Offset)));
  318. // Increment the pointers.
  319. Offset += RegBytes;
  320. StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
  321. Increment);
  322. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
  323. }
  324. // The last store may be partial. Do a truncating store. On big-endian
  325. // machines this requires an extending load from the stack slot to ensure
  326. // that the bits are in the right place.
  327. EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
  328. 8 * (StoredBytes - Offset));
  329. // Load from the stack slot.
  330. SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
  331. MachinePointerInfo(),
  332. MemVT, false, false, 0);
  333. Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
  334. ST->getPointerInfo()
  335. .getWithOffset(Offset),
  336. MemVT, ST->isVolatile(),
  337. ST->isNonTemporal(),
  338. MinAlign(ST->getAlignment(), Offset)));
  339. // The order of the stores doesn't matter - say it with a TokenFactor.
  340. SDValue Result =
  341. DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
  342. Stores.size());
  343. DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
  344. return;
  345. }
  346. assert(ST->getMemoryVT().isInteger() &&
  347. !ST->getMemoryVT().isVector() &&
  348. "Unaligned store of unknown type.");
  349. // Get the half-size VT
  350. EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
  351. int NumBits = NewStoredVT.getSizeInBits();
  352. int IncrementSize = NumBits / 8;
  353. // Divide the stored value in two parts.
  354. SDValue ShiftAmount = DAG.getConstant(NumBits,
  355. TLI.getShiftAmountTy(Val.getValueType()));
  356. SDValue Lo = Val;
  357. SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
  358. // Store the two parts
  359. SDValue Store1, Store2;
  360. Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
  361. ST->getPointerInfo(), NewStoredVT,
  362. ST->isVolatile(), ST->isNonTemporal(), Alignment);
  363. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  364. DAG.getConstant(IncrementSize, TLI.getPointerTy()));
  365. Alignment = MinAlign(Alignment, IncrementSize);
  366. Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
  367. ST->getPointerInfo().getWithOffset(IncrementSize),
  368. NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
  369. Alignment);
  370. SDValue Result =
  371. DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
  372. DAGLegalize->ReplaceNode(SDValue(ST, 0), Result);
  373. }
  374. /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
  375. static void
  376. ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
  377. const TargetLowering &TLI,
  378. SDValue &ValResult, SDValue &ChainResult) {
  379. assert(LD->getAddressingMode() == ISD::UNINDEXED &&
  380. "unaligned indexed loads not implemented!");
  381. SDValue Chain = LD->getChain();
  382. SDValue Ptr = LD->getBasePtr();
  383. EVT VT = LD->getValueType(0);
  384. EVT LoadedVT = LD->getMemoryVT();
  385. SDLoc dl(LD);
  386. if (VT.isFloatingPoint() || VT.isVector()) {
  387. EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
  388. if (TLI.isTypeLegal(intVT) && TLI.isTypeLegal(LoadedVT)) {
  389. // Expand to a (misaligned) integer load of the same size,
  390. // then bitconvert to floating point or vector.
  391. SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
  392. LD->isVolatile(),
  393. LD->isNonTemporal(),
  394. LD->isInvariant(), LD->getAlignment());
  395. SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
  396. if (LoadedVT != VT)
  397. Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
  398. ISD::ANY_EXTEND, dl, VT, Result);
  399. ValResult = Result;
  400. ChainResult = Chain;
  401. return;
  402. }
  403. // Copy the value to a (aligned) stack slot using (unaligned) integer
  404. // loads and stores, then do a (aligned) load from the stack slot.
  405. MVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
  406. unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
  407. unsigned RegBytes = RegVT.getSizeInBits() / 8;
  408. unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
  409. // Make sure the stack slot is also aligned for the register type.
  410. SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
  411. SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
  412. SmallVector<SDValue, 8> Stores;
  413. SDValue StackPtr = StackBase;
  414. unsigned Offset = 0;
  415. // Do all but one copies using the full register width.
  416. for (unsigned i = 1; i < NumRegs; i++) {
  417. // Load one integer register's worth from the original location.
  418. SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
  419. LD->getPointerInfo().getWithOffset(Offset),
  420. LD->isVolatile(), LD->isNonTemporal(),
  421. LD->isInvariant(),
  422. MinAlign(LD->getAlignment(), Offset));
  423. // Follow the load with a store to the stack slot. Remember the store.
  424. Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
  425. MachinePointerInfo(), false, false, 0));
  426. // Increment the pointers.
  427. Offset += RegBytes;
  428. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
  429. StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
  430. Increment);
  431. }
  432. // The last copy may be partial. Do an extending load.
  433. EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
  434. 8 * (LoadedBytes - Offset));
  435. SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
  436. LD->getPointerInfo().getWithOffset(Offset),
  437. MemVT, LD->isVolatile(),
  438. LD->isNonTemporal(),
  439. MinAlign(LD->getAlignment(), Offset));
  440. // Follow the load with a store to the stack slot. Remember the store.
  441. // On big-endian machines this requires a truncating store to ensure
  442. // that the bits end up in the right place.
  443. Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
  444. MachinePointerInfo(), MemVT,
  445. false, false, 0));
  446. // The order of the stores doesn't matter - say it with a TokenFactor.
  447. SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
  448. Stores.size());
  449. // Finally, perform the original load only redirected to the stack slot.
  450. Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
  451. MachinePointerInfo(), LoadedVT, false, false, 0);
  452. // Callers expect a MERGE_VALUES node.
  453. ValResult = Load;
  454. ChainResult = TF;
  455. return;
  456. }
  457. assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
  458. "Unaligned load of unsupported type.");
  459. // Compute the new VT that is half the size of the old one. This is an
  460. // integer MVT.
  461. unsigned NumBits = LoadedVT.getSizeInBits();
  462. EVT NewLoadedVT;
  463. NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
  464. NumBits >>= 1;
  465. unsigned Alignment = LD->getAlignment();
  466. unsigned IncrementSize = NumBits / 8;
  467. ISD::LoadExtType HiExtType = LD->getExtensionType();
  468. // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
  469. if (HiExtType == ISD::NON_EXTLOAD)
  470. HiExtType = ISD::ZEXTLOAD;
  471. // Load the value in two parts
  472. SDValue Lo, Hi;
  473. if (TLI.isLittleEndian()) {
  474. Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
  475. NewLoadedVT, LD->isVolatile(),
  476. LD->isNonTemporal(), Alignment);
  477. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  478. DAG.getConstant(IncrementSize, TLI.getPointerTy()));
  479. Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
  480. LD->getPointerInfo().getWithOffset(IncrementSize),
  481. NewLoadedVT, LD->isVolatile(),
  482. LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
  483. } else {
  484. Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
  485. NewLoadedVT, LD->isVolatile(),
  486. LD->isNonTemporal(), Alignment);
  487. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  488. DAG.getConstant(IncrementSize, TLI.getPointerTy()));
  489. Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
  490. LD->getPointerInfo().getWithOffset(IncrementSize),
  491. NewLoadedVT, LD->isVolatile(),
  492. LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
  493. }
  494. // aggregate the two parts
  495. SDValue ShiftAmount = DAG.getConstant(NumBits,
  496. TLI.getShiftAmountTy(Hi.getValueType()));
  497. SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
  498. Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
  499. SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  500. Hi.getValue(1));
  501. ValResult = Result;
  502. ChainResult = TF;
  503. }
  504. /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
  505. /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
  506. /// is necessary to spill the vector being inserted into to memory, perform
  507. /// the insert there, and then read the result back.
  508. SDValue SelectionDAGLegalize::
  509. PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
  510. SDLoc dl) {
  511. SDValue Tmp1 = Vec;
  512. SDValue Tmp2 = Val;
  513. SDValue Tmp3 = Idx;
  514. // If the target doesn't support this, we have to spill the input vector
  515. // to a temporary stack slot, update the element, then reload it. This is
  516. // badness. We could also load the value into a vector register (either
  517. // with a "move to register" or "extload into register" instruction, then
  518. // permute it into place, if the idx is a constant and if the idx is
  519. // supported by the target.
  520. EVT VT = Tmp1.getValueType();
  521. EVT EltVT = VT.getVectorElementType();
  522. EVT IdxVT = Tmp3.getValueType();
  523. EVT PtrVT = TLI.getPointerTy();
  524. SDValue StackPtr = DAG.CreateStackTemporary(VT);
  525. int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
  526. // Store the vector.
  527. SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
  528. MachinePointerInfo::getFixedStack(SPFI),
  529. false, false, 0);
  530. // Truncate or zero extend offset to target pointer type.
  531. unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
  532. Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
  533. // Add the offset to the index.
  534. unsigned EltSize = EltVT.getSizeInBits()/8;
  535. Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
  536. SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
  537. // Store the scalar value.
  538. Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
  539. false, false, 0);
  540. // Load the updated vector.
  541. return DAG.getLoad(VT, dl, Ch, StackPtr,
  542. MachinePointerInfo::getFixedStack(SPFI), false, false,
  543. false, 0);
  544. }
  545. SDValue SelectionDAGLegalize::
  546. ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, SDLoc dl) {
  547. if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
  548. // SCALAR_TO_VECTOR requires that the type of the value being inserted
  549. // match the element type of the vector being created, except for
  550. // integers in which case the inserted value can be over width.
  551. EVT EltVT = Vec.getValueType().getVectorElementType();
  552. if (Val.getValueType() == EltVT ||
  553. (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
  554. SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
  555. Vec.getValueType(), Val);
  556. unsigned NumElts = Vec.getValueType().getVectorNumElements();
  557. // We generate a shuffle of InVec and ScVec, so the shuffle mask
  558. // should be 0,1,2,3,4,5... with the appropriate element replaced with
  559. // elt 0 of the RHS.
  560. SmallVector<int, 8> ShufOps;
  561. for (unsigned i = 0; i != NumElts; ++i)
  562. ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
  563. return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
  564. &ShufOps[0]);
  565. }
  566. }
  567. return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
  568. }
  569. SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
  570. // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
  571. // FIXME: We shouldn't do this for TargetConstantFP's.
  572. // FIXME: move this to the DAG Combiner! Note that we can't regress due
  573. // to phase ordering between legalized code and the dag combiner. This
  574. // probably means that we need to integrate dag combiner and legalizer
  575. // together.
  576. // We generally can't do this one for long doubles.
  577. SDValue Chain = ST->getChain();
  578. SDValue Ptr = ST->getBasePtr();
  579. unsigned Alignment = ST->getAlignment();
  580. bool isVolatile = ST->isVolatile();
  581. bool isNonTemporal = ST->isNonTemporal();
  582. SDLoc dl(ST);
  583. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
  584. if (CFP->getValueType(0) == MVT::f32 &&
  585. TLI.isTypeLegal(MVT::i32)) {
  586. SDValue Con = DAG.getConstant(CFP->getValueAPF().
  587. bitcastToAPInt().zextOrTrunc(32),
  588. MVT::i32);
  589. return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
  590. isVolatile, isNonTemporal, Alignment);
  591. }
  592. if (CFP->getValueType(0) == MVT::f64) {
  593. // If this target supports 64-bit registers, do a single 64-bit store.
  594. if (TLI.isTypeLegal(MVT::i64)) {
  595. SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
  596. zextOrTrunc(64), MVT::i64);
  597. return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
  598. isVolatile, isNonTemporal, Alignment);
  599. }
  600. if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
  601. // Otherwise, if the target supports 32-bit registers, use 2 32-bit
  602. // stores. If the target supports neither 32- nor 64-bits, this
  603. // xform is certainly not worth it.
  604. const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
  605. SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
  606. SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
  607. if (TLI.isBigEndian()) std::swap(Lo, Hi);
  608. Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), isVolatile,
  609. isNonTemporal, Alignment);
  610. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  611. DAG.getIntPtrConstant(4));
  612. Hi = DAG.getStore(Chain, dl, Hi, Ptr,
  613. ST->getPointerInfo().getWithOffset(4),
  614. isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
  615. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  616. }
  617. }
  618. }
  619. return SDValue(0, 0);
  620. }
  621. void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
  622. StoreSDNode *ST = cast<StoreSDNode>(Node);
  623. SDValue Chain = ST->getChain();
  624. SDValue Ptr = ST->getBasePtr();
  625. SDLoc dl(Node);
  626. unsigned Alignment = ST->getAlignment();
  627. bool isVolatile = ST->isVolatile();
  628. bool isNonTemporal = ST->isNonTemporal();
  629. if (!ST->isTruncatingStore()) {
  630. if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
  631. ReplaceNode(ST, OptStore);
  632. return;
  633. }
  634. {
  635. SDValue Value = ST->getValue();
  636. MVT VT = Value.getSimpleValueType();
  637. switch (TLI.getOperationAction(ISD::STORE, VT)) {
  638. default: llvm_unreachable("This action is not supported yet!");
  639. case TargetLowering::Legal:
  640. // If this is an unaligned store and the target doesn't support it,
  641. // expand it.
  642. if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
  643. Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
  644. unsigned ABIAlignment= TLI.getDataLayout()->getABITypeAlignment(Ty);
  645. if (ST->getAlignment() < ABIAlignment)
  646. ExpandUnalignedStore(cast<StoreSDNode>(Node),
  647. DAG, TLI, this);
  648. }
  649. break;
  650. case TargetLowering::Custom: {
  651. SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
  652. if (Res.getNode())
  653. ReplaceNode(SDValue(Node, 0), Res);
  654. return;
  655. }
  656. case TargetLowering::Promote: {
  657. MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
  658. assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
  659. "Can only promote stores to same size type");
  660. Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
  661. SDValue Result =
  662. DAG.getStore(Chain, dl, Value, Ptr,
  663. ST->getPointerInfo(), isVolatile,
  664. isNonTemporal, Alignment);
  665. ReplaceNode(SDValue(Node, 0), Result);
  666. break;
  667. }
  668. }
  669. return;
  670. }
  671. } else {
  672. SDValue Value = ST->getValue();
  673. EVT StVT = ST->getMemoryVT();
  674. unsigned StWidth = StVT.getSizeInBits();
  675. if (StWidth != StVT.getStoreSizeInBits()) {
  676. // Promote to a byte-sized store with upper bits zero if not
  677. // storing an integral number of bytes. For example, promote
  678. // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
  679. EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
  680. StVT.getStoreSizeInBits());
  681. Value = DAG.getZeroExtendInReg(Value, dl, StVT);
  682. SDValue Result =
  683. DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
  684. NVT, isVolatile, isNonTemporal, Alignment);
  685. ReplaceNode(SDValue(Node, 0), Result);
  686. } else if (StWidth & (StWidth - 1)) {
  687. // If not storing a power-of-2 number of bits, expand as two stores.
  688. assert(!StVT.isVector() && "Unsupported truncstore!");
  689. unsigned RoundWidth = 1 << Log2_32(StWidth);
  690. assert(RoundWidth < StWidth);
  691. unsigned ExtraWidth = StWidth - RoundWidth;
  692. assert(ExtraWidth < RoundWidth);
  693. assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
  694. "Store size not an integral number of bytes!");
  695. EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
  696. EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
  697. SDValue Lo, Hi;
  698. unsigned IncrementSize;
  699. if (TLI.isLittleEndian()) {
  700. // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
  701. // Store the bottom RoundWidth bits.
  702. Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
  703. RoundVT,
  704. isVolatile, isNonTemporal, Alignment);
  705. // Store the remaining ExtraWidth bits.
  706. IncrementSize = RoundWidth / 8;
  707. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  708. DAG.getIntPtrConstant(IncrementSize));
  709. Hi = DAG.getNode(ISD::SRL, dl, Value.getValueType(), Value,
  710. DAG.getConstant(RoundWidth,
  711. TLI.getShiftAmountTy(Value.getValueType())));
  712. Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
  713. ST->getPointerInfo().getWithOffset(IncrementSize),
  714. ExtraVT, isVolatile, isNonTemporal,
  715. MinAlign(Alignment, IncrementSize));
  716. } else {
  717. // Big endian - avoid unaligned stores.
  718. // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
  719. // Store the top RoundWidth bits.
  720. Hi = DAG.getNode(ISD::SRL, dl, Value.getValueType(), Value,
  721. DAG.getConstant(ExtraWidth,
  722. TLI.getShiftAmountTy(Value.getValueType())));
  723. Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(),
  724. RoundVT, isVolatile, isNonTemporal, Alignment);
  725. // Store the remaining ExtraWidth bits.
  726. IncrementSize = RoundWidth / 8;
  727. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  728. DAG.getIntPtrConstant(IncrementSize));
  729. Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
  730. ST->getPointerInfo().getWithOffset(IncrementSize),
  731. ExtraVT, isVolatile, isNonTemporal,
  732. MinAlign(Alignment, IncrementSize));
  733. }
  734. // The order of the stores doesn't matter.
  735. SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
  736. ReplaceNode(SDValue(Node, 0), Result);
  737. } else {
  738. switch (TLI.getTruncStoreAction(ST->getValue().getSimpleValueType(),
  739. StVT.getSimpleVT())) {
  740. default: llvm_unreachable("This action is not supported yet!");
  741. case TargetLowering::Legal:
  742. // If this is an unaligned store and the target doesn't support it,
  743. // expand it.
  744. if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
  745. Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
  746. unsigned ABIAlignment= TLI.getDataLayout()->getABITypeAlignment(Ty);
  747. if (ST->getAlignment() < ABIAlignment)
  748. ExpandUnalignedStore(cast<StoreSDNode>(Node), DAG, TLI, this);
  749. }
  750. break;
  751. case TargetLowering::Custom: {
  752. SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
  753. if (Res.getNode())
  754. ReplaceNode(SDValue(Node, 0), Res);
  755. return;
  756. }
  757. case TargetLowering::Expand:
  758. assert(!StVT.isVector() &&
  759. "Vector Stores are handled in LegalizeVectorOps");
  760. // TRUNCSTORE:i16 i32 -> STORE i16
  761. assert(TLI.isTypeLegal(StVT) &&
  762. "Do not know how to expand this store!");
  763. Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
  764. SDValue Result =
  765. DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
  766. isVolatile, isNonTemporal, Alignment);
  767. ReplaceNode(SDValue(Node, 0), Result);
  768. break;
  769. }
  770. }
  771. }
  772. }
  773. void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
  774. LoadSDNode *LD = cast<LoadSDNode>(Node);
  775. SDValue Chain = LD->getChain(); // The chain.
  776. SDValue Ptr = LD->getBasePtr(); // The base pointer.
  777. SDValue Value; // The value returned by the load op.
  778. SDLoc dl(Node);
  779. ISD::LoadExtType ExtType = LD->getExtensionType();
  780. if (ExtType == ISD::NON_EXTLOAD) {
  781. MVT VT = Node->getSimpleValueType(0);
  782. SDValue RVal = SDValue(Node, 0);
  783. SDValue RChain = SDValue(Node, 1);
  784. switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
  785. default: llvm_unreachable("This action is not supported yet!");
  786. case TargetLowering::Legal:
  787. // If this is an unaligned load and the target doesn't support it,
  788. // expand it.
  789. if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
  790. Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
  791. unsigned ABIAlignment =
  792. TLI.getDataLayout()->getABITypeAlignment(Ty);
  793. if (LD->getAlignment() < ABIAlignment){
  794. ExpandUnalignedLoad(cast<LoadSDNode>(Node), DAG, TLI, RVal, RChain);
  795. }
  796. }
  797. break;
  798. case TargetLowering::Custom: {
  799. SDValue Res = TLI.LowerOperation(RVal, DAG);
  800. if (Res.getNode()) {
  801. RVal = Res;
  802. RChain = Res.getValue(1);
  803. }
  804. break;
  805. }
  806. case TargetLowering::Promote: {
  807. MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
  808. assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
  809. "Can only promote loads to same size type");
  810. SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getPointerInfo(),
  811. LD->isVolatile(), LD->isNonTemporal(),
  812. LD->isInvariant(), LD->getAlignment());
  813. RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
  814. RChain = Res.getValue(1);
  815. break;
  816. }
  817. }
  818. if (RChain.getNode() != Node) {
  819. assert(RVal.getNode() != Node && "Load must be completely replaced");
  820. DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
  821. DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
  822. ReplacedNode(Node);
  823. }
  824. return;
  825. }
  826. EVT SrcVT = LD->getMemoryVT();
  827. unsigned SrcWidth = SrcVT.getSizeInBits();
  828. unsigned Alignment = LD->getAlignment();
  829. bool isVolatile = LD->isVolatile();
  830. bool isNonTemporal = LD->isNonTemporal();
  831. if (SrcWidth != SrcVT.getStoreSizeInBits() &&
  832. // Some targets pretend to have an i1 loading operation, and actually
  833. // load an i8. This trick is correct for ZEXTLOAD because the top 7
  834. // bits are guaranteed to be zero; it helps the optimizers understand
  835. // that these bits are zero. It is also useful for EXTLOAD, since it
  836. // tells the optimizers that those bits are undefined. It would be
  837. // nice to have an effective generic way of getting these benefits...
  838. // Until such a way is found, don't insist on promoting i1 here.
  839. (SrcVT != MVT::i1 ||
  840. TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
  841. // Promote to a byte-sized load if not loading an integral number of
  842. // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
  843. unsigned NewWidth = SrcVT.getStoreSizeInBits();
  844. EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
  845. SDValue Ch;
  846. // The extra bits are guaranteed to be zero, since we stored them that
  847. // way. A zext load from NVT thus automatically gives zext from SrcVT.
  848. ISD::LoadExtType NewExtType =
  849. ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
  850. SDValue Result =
  851. DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
  852. Chain, Ptr, LD->getPointerInfo(),
  853. NVT, isVolatile, isNonTemporal, Alignment);
  854. Ch = Result.getValue(1); // The chain.
  855. if (ExtType == ISD::SEXTLOAD)
  856. // Having the top bits zero doesn't help when sign extending.
  857. Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
  858. Result.getValueType(),
  859. Result, DAG.getValueType(SrcVT));
  860. else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
  861. // All the top bits are guaranteed to be zero - inform the optimizers.
  862. Result = DAG.getNode(ISD::AssertZext, dl,
  863. Result.getValueType(), Result,
  864. DAG.getValueType(SrcVT));
  865. Value = Result;
  866. Chain = Ch;
  867. } else if (SrcWidth & (SrcWidth - 1)) {
  868. // If not loading a power-of-2 number of bits, expand as two loads.
  869. assert(!SrcVT.isVector() && "Unsupported extload!");
  870. unsigned RoundWidth = 1 << Log2_32(SrcWidth);
  871. assert(RoundWidth < SrcWidth);
  872. unsigned ExtraWidth = SrcWidth - RoundWidth;
  873. assert(ExtraWidth < RoundWidth);
  874. assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
  875. "Load size not an integral number of bytes!");
  876. EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
  877. EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
  878. SDValue Lo, Hi, Ch;
  879. unsigned IncrementSize;
  880. if (TLI.isLittleEndian()) {
  881. // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
  882. // Load the bottom RoundWidth bits.
  883. Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
  884. Chain, Ptr,
  885. LD->getPointerInfo(), RoundVT, isVolatile,
  886. isNonTemporal, Alignment);
  887. // Load the remaining ExtraWidth bits.
  888. IncrementSize = RoundWidth / 8;
  889. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  890. DAG.getIntPtrConstant(IncrementSize));
  891. Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
  892. LD->getPointerInfo().getWithOffset(IncrementSize),
  893. ExtraVT, isVolatile, isNonTemporal,
  894. MinAlign(Alignment, IncrementSize));
  895. // Build a factor node to remember that this load is independent of
  896. // the other one.
  897. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  898. Hi.getValue(1));
  899. // Move the top bits to the right place.
  900. Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
  901. DAG.getConstant(RoundWidth,
  902. TLI.getShiftAmountTy(Hi.getValueType())));
  903. // Join the hi and lo parts.
  904. Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
  905. } else {
  906. // Big endian - avoid unaligned loads.
  907. // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
  908. // Load the top RoundWidth bits.
  909. Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
  910. LD->getPointerInfo(), RoundVT, isVolatile,
  911. isNonTemporal, Alignment);
  912. // Load the remaining ExtraWidth bits.
  913. IncrementSize = RoundWidth / 8;
  914. Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
  915. DAG.getIntPtrConstant(IncrementSize));
  916. Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
  917. dl, Node->getValueType(0), Chain, Ptr,
  918. LD->getPointerInfo().getWithOffset(IncrementSize),
  919. ExtraVT, isVolatile, isNonTemporal,
  920. MinAlign(Alignment, IncrementSize));
  921. // Build a factor node to remember that this load is independent of
  922. // the other one.
  923. Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
  924. Hi.getValue(1));
  925. // Move the top bits to the right place.
  926. Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
  927. DAG.getConstant(ExtraWidth,
  928. TLI.getShiftAmountTy(Hi.getValueType())));
  929. // Join the hi and lo parts.
  930. Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
  931. }
  932. Chain = Ch;
  933. } else {
  934. bool isCustom = false;
  935. switch (TLI.getLoadExtAction(ExtType, SrcVT.getSimpleVT())) {
  936. default: llvm_unreachable("This action is not supported yet!");
  937. case TargetLowering::Custom:
  938. isCustom = true;
  939. // FALLTHROUGH
  940. case TargetLowering::Legal: {
  941. Value = SDValue(Node, 0);
  942. Chain = SDValue(Node, 1);
  943. if (isCustom) {
  944. SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
  945. if (Res.getNode()) {
  946. Value = Res;
  947. Chain = Res.getValue(1);
  948. }
  949. } else {
  950. // If this is an unaligned load and the target doesn't support it,
  951. // expand it.
  952. if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
  953. Type *Ty =
  954. LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
  955. unsigned ABIAlignment =
  956. TLI.getDataLayout()->getABITypeAlignment(Ty);
  957. if (LD->getAlignment() < ABIAlignment){
  958. ExpandUnalignedLoad(cast<LoadSDNode>(Node),
  959. DAG, TLI, Value, Chain);
  960. }
  961. }
  962. }
  963. break;
  964. }
  965. case TargetLowering::Expand:
  966. if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) {
  967. SDValue Load = DAG.getLoad(SrcVT, dl, Chain, Ptr,
  968. LD->getPointerInfo(),
  969. LD->isVolatile(), LD->isNonTemporal(),
  970. LD->isInvariant(), LD->getAlignment());
  971. unsigned ExtendOp;
  972. switch (ExtType) {
  973. case ISD::EXTLOAD:
  974. ExtendOp = (SrcVT.isFloatingPoint() ?
  975. ISD::FP_EXTEND : ISD::ANY_EXTEND);
  976. break;
  977. case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
  978. case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
  979. default: llvm_unreachable("Unexpected extend load type!");
  980. }
  981. Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
  982. Chain = Load.getValue(1);
  983. break;
  984. }
  985. assert(!SrcVT.isVector() &&
  986. "Vector Loads are handled in LegalizeVectorOps");
  987. // FIXME: This does not work for vectors on most targets. Sign- and
  988. // zero-extend operations are currently folded into extending loads,
  989. // whether they are legal or not, and then we end up here without any
  990. // support for legalizing them.
  991. assert(ExtType != ISD::EXTLOAD &&
  992. "EXTLOAD should always be supported!");
  993. // Turn the unsupported load into an EXTLOAD followed by an explicit
  994. // zero/sign extend inreg.
  995. SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
  996. Chain, Ptr, LD->getPointerInfo(), SrcVT,
  997. LD->isVolatile(), LD->isNonTemporal(),
  998. LD->getAlignment());
  999. SDValue ValRes;
  1000. if (ExtType == ISD::SEXTLOAD)
  1001. ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
  1002. Result.getValueType(),
  1003. Result, DAG.getValueType(SrcVT));
  1004. else
  1005. ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
  1006. Value = ValRes;
  1007. Chain = Result.getValue(1);
  1008. break;
  1009. }
  1010. }
  1011. // Since loads produce two values, make sure to remember that we legalized
  1012. // both of them.
  1013. if (Chain.getNode() != Node) {
  1014. assert(Value.getNode() != Node && "Load must be completely replaced");
  1015. DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
  1016. DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
  1017. ReplacedNode(Node);
  1018. }
  1019. }
  1020. /// LegalizeOp - Return a legal replacement for the given operation, with
  1021. /// all legal operands.
  1022. void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
  1023. if (Node->getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
  1024. return;
  1025. for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
  1026. assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
  1027. TargetLowering::TypeLegal &&
  1028. "Unexpected illegal type!");
  1029. for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
  1030. assert((TLI.getTypeAction(*DAG.getContext(),
  1031. Node->getOperand(i).getValueType()) ==
  1032. TargetLowering::TypeLegal ||
  1033. Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
  1034. "Unexpected illegal type!");
  1035. // Figure out the correct action; the way to query this varies by opcode
  1036. TargetLowering::LegalizeAction Action = TargetLowering::Legal;
  1037. bool SimpleFinishLegalizing = true;
  1038. switch (Node->getOpcode()) {
  1039. case ISD::INTRINSIC_W_CHAIN:
  1040. case ISD::INTRINSIC_WO_CHAIN:
  1041. case ISD::INTRINSIC_VOID:
  1042. case ISD::STACKSAVE:
  1043. Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
  1044. break;
  1045. case ISD::VAARG:
  1046. Action = TLI.getOperationAction(Node->getOpcode(),
  1047. Node->getValueType(0));
  1048. if (Action != TargetLowering::Promote)
  1049. Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
  1050. break;
  1051. case ISD::SINT_TO_FP:
  1052. case ISD::UINT_TO_FP:
  1053. case ISD::EXTRACT_VECTOR_ELT:
  1054. Action = TLI.getOperationAction(Node->getOpcode(),
  1055. Node->getOperand(0).getValueType());
  1056. break;
  1057. case ISD::FP_ROUND_INREG:
  1058. case ISD::SIGN_EXTEND_INREG: {
  1059. EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
  1060. Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
  1061. break;
  1062. }
  1063. case ISD::ATOMIC_STORE: {
  1064. Action = TLI.getOperationAction(Node->getOpcode(),
  1065. Node->getOperand(2).getValueType());
  1066. break;
  1067. }
  1068. case ISD::SELECT_CC:
  1069. case ISD::SETCC:
  1070. case ISD::BR_CC: {
  1071. unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
  1072. Node->getOpcode() == ISD::SETCC ? 2 : 1;
  1073. unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
  1074. MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
  1075. ISD::CondCode CCCode =
  1076. cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
  1077. Action = TLI.getCondCodeAction(CCCode, OpVT);
  1078. if (Action == TargetLowering::Legal) {
  1079. if (Node->getOpcode() == ISD::SELECT_CC)
  1080. Action = TLI.getOperationAction(Node->getOpcode(),
  1081. Node->getValueType(0));
  1082. else
  1083. Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
  1084. }
  1085. break;
  1086. }
  1087. case ISD::LOAD:
  1088. case ISD::STORE:
  1089. // FIXME: Model these properly. LOAD and STORE are complicated, and
  1090. // STORE expects the unlegalized operand in some cases.
  1091. SimpleFinishLegalizing = false;
  1092. break;
  1093. case ISD::CALLSEQ_START:
  1094. case ISD::CALLSEQ_END:
  1095. // FIXME: This shouldn't be necessary. These nodes have special properties
  1096. // dealing with the recursive nature of legalization. Removing this
  1097. // special case should be done as part of making LegalizeDAG non-recursive.
  1098. SimpleFinishLegalizing = false;
  1099. break;
  1100. case ISD::EXTRACT_ELEMENT:
  1101. case ISD::FLT_ROUNDS_:
  1102. case ISD::SADDO:
  1103. case ISD::SSUBO:
  1104. case ISD::UADDO:
  1105. case ISD::USUBO:
  1106. case ISD::SMULO:
  1107. case ISD::UMULO:
  1108. case ISD::FPOWI:
  1109. case ISD::MERGE_VALUES:
  1110. case ISD::EH_RETURN:
  1111. case ISD::FRAME_TO_ARGS_OFFSET:
  1112. case ISD::EH_SJLJ_SETJMP:
  1113. case ISD::EH_SJLJ_LONGJMP:
  1114. // These operations lie about being legal: when they claim to be legal,
  1115. // they should actually be expanded.
  1116. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  1117. if (Action == TargetLowering::Legal)
  1118. Action = TargetLowering::Expand;
  1119. break;
  1120. case ISD::INIT_TRAMPOLINE:
  1121. case ISD::ADJUST_TRAMPOLINE:
  1122. case ISD::FRAMEADDR:
  1123. case ISD::RETURNADDR:
  1124. // These operations lie about being legal: when they claim to be legal,
  1125. // they should actually be custom-lowered.
  1126. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  1127. if (Action == TargetLowering::Legal)
  1128. Action = TargetLowering::Custom;
  1129. break;
  1130. case ISD::DEBUGTRAP:
  1131. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  1132. if (Action == TargetLowering::Expand) {
  1133. // replace ISD::DEBUGTRAP with ISD::TRAP
  1134. SDValue NewVal;
  1135. NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
  1136. Node->getOperand(0));
  1137. ReplaceNode(Node, NewVal.getNode());
  1138. LegalizeOp(NewVal.getNode());
  1139. return;
  1140. }
  1141. break;
  1142. default:
  1143. if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
  1144. Action = TargetLowering::Legal;
  1145. } else {
  1146. Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
  1147. }
  1148. break;
  1149. }
  1150. if (SimpleFinishLegalizing) {
  1151. SDNode *NewNode = Node;
  1152. switch (Node->getOpcode()) {
  1153. default: break;
  1154. case ISD::SHL:
  1155. case ISD::SRL:
  1156. case ISD::SRA:
  1157. case ISD::ROTL:
  1158. case ISD::ROTR:
  1159. // Legalizing shifts/rotates requires adjusting the shift amount
  1160. // to the appropriate width.
  1161. if (!Node->getOperand(1).getValueType().isVector()) {
  1162. SDValue SAO =
  1163. DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
  1164. Node->getOperand(1));
  1165. HandleSDNode Handle(SAO);
  1166. LegalizeOp(SAO.getNode());
  1167. NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
  1168. Handle.getValue());
  1169. }
  1170. break;
  1171. case ISD::SRL_PARTS:
  1172. case ISD::SRA_PARTS:
  1173. case ISD::SHL_PARTS:
  1174. // Legalizing shifts/rotates requires adjusting the shift amount
  1175. // to the appropriate width.
  1176. if (!Node->getOperand(2).getValueType().isVector()) {
  1177. SDValue SAO =
  1178. DAG.getShiftAmountOperand(Node->getOperand(0).getValueType(),
  1179. Node->getOperand(2));
  1180. HandleSDNode Handle(SAO);
  1181. LegalizeOp(SAO.getNode());
  1182. NewNode = DAG.UpdateNodeOperands(Node, Node->getOperand(0),
  1183. Node->getOperand(1),
  1184. Handle.getValue());
  1185. }
  1186. break;
  1187. }
  1188. if (NewNode != Node) {
  1189. DAG.ReplaceAllUsesWith(Node, NewNode);
  1190. for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
  1191. DAG.TransferDbgValues(SDValue(Node, i), SDValue(NewNode, i));
  1192. ReplacedNode(Node);
  1193. Node = NewNode;
  1194. }
  1195. switch (Action) {
  1196. case TargetLowering::Legal:
  1197. return;
  1198. case TargetLowering::Custom: {
  1199. // FIXME: The handling for custom lowering with multiple results is
  1200. // a complete mess.
  1201. SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
  1202. if (Res.getNode()) {
  1203. SmallVector<SDValue, 8> ResultVals;
  1204. for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
  1205. if (e == 1)
  1206. ResultVals.push_back(Res);
  1207. else
  1208. ResultVals.push_back(Res.getValue(i));
  1209. }
  1210. if (Res.getNode() != Node || Res.getResNo() != 0) {
  1211. DAG.ReplaceAllUsesWith(Node, ResultVals.data());
  1212. for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
  1213. DAG.TransferDbgValues(SDValue(Node, i), ResultVals[i]);
  1214. ReplacedNode(Node);
  1215. }
  1216. return;
  1217. }
  1218. }
  1219. // FALL THROUGH
  1220. case TargetLowering::Expand:
  1221. ExpandNode(Node);
  1222. return;
  1223. case TargetLowering::Promote:
  1224. PromoteNode(Node);
  1225. return;
  1226. }
  1227. }
  1228. switch (Node->getOpcode()) {
  1229. default:
  1230. #ifndef NDEBUG
  1231. dbgs() << "NODE: ";
  1232. Node->dump( &DAG);
  1233. dbgs() << "\n";
  1234. #endif
  1235. llvm_unreachable("Do not know how to legalize this operator!");
  1236. case ISD::CALLSEQ_START:
  1237. case ISD::CALLSEQ_END:
  1238. break;
  1239. case ISD::LOAD: {
  1240. return LegalizeLoadOps(Node);
  1241. }
  1242. case ISD::STORE: {
  1243. return LegalizeStoreOps(Node);
  1244. }
  1245. }
  1246. }
  1247. SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
  1248. SDValue Vec = Op.getOperand(0);
  1249. SDValue Idx = Op.getOperand(1);
  1250. SDLoc dl(Op);
  1251. // Store the value to a temporary stack slot, then LOAD the returned part.
  1252. SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
  1253. SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
  1254. MachinePointerInfo(), false, false, 0);
  1255. // Add the offset to the index.
  1256. unsigned EltSize =
  1257. Vec.getValueType().getVectorElementType().getSizeInBits()/8;
  1258. Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
  1259. DAG.getConstant(EltSize, Idx.getValueType()));
  1260. if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
  1261. Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
  1262. else
  1263. Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
  1264. StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
  1265. if (Op.getValueType().isVector())
  1266. return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
  1267. false, false, false, 0);
  1268. return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
  1269. MachinePointerInfo(),
  1270. Vec.getValueType().getVectorElementType(),
  1271. false, false, 0);
  1272. }
  1273. SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
  1274. assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
  1275. SDValue Vec = Op.getOperand(0);
  1276. SDValue Part = Op.getOperand(1);
  1277. SDValue Idx = Op.getOperand(2);
  1278. SDLoc dl(Op);
  1279. // Store the value to a temporary stack slot, then LOAD the returned part.
  1280. SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
  1281. int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
  1282. MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
  1283. // First store the whole vector.
  1284. SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
  1285. false, false, 0);
  1286. // Then store the inserted part.
  1287. // Add the offset to the index.
  1288. unsigned EltSize =
  1289. Vec.getValueType().getVectorElementType().getSizeInBits()/8;
  1290. Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
  1291. DAG.getConstant(EltSize, Idx.getValueType()));
  1292. if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
  1293. Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
  1294. else
  1295. Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
  1296. SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
  1297. StackPtr);
  1298. // Store the subvector.
  1299. Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
  1300. MachinePointerInfo(), false, false, 0);
  1301. // Finally, load the updated vector.
  1302. return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
  1303. false, false, false, 0);
  1304. }
  1305. SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
  1306. // We can't handle this case efficiently. Allocate a sufficiently
  1307. // aligned object on the stack, store each element into it, then load
  1308. // the result as a vector.
  1309. // Create the stack frame object.
  1310. EVT VT = Node->getValueType(0);
  1311. EVT EltVT = VT.getVectorElementType();
  1312. SDLoc dl(Node);
  1313. SDValue FIPtr = DAG.CreateStackTemporary(VT);
  1314. int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
  1315. MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
  1316. // Emit a store of each element to the stack slot.
  1317. SmallVector<SDValue, 8> Stores;
  1318. unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
  1319. // Store (in the right endianness) the elements to memory.
  1320. for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
  1321. // Ignore undef elements.
  1322. if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
  1323. unsigned Offset = TypeByteSize*i;
  1324. SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
  1325. Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
  1326. // If the destination vector element type is narrower than the source
  1327. // element type, only store the bits necessary.
  1328. if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
  1329. Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
  1330. Node->getOperand(i), Idx,
  1331. PtrInfo.getWithOffset(Offset),
  1332. EltVT, false, false, 0));
  1333. } else
  1334. Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
  1335. Node->getOperand(i), Idx,
  1336. PtrInfo.getWithOffset(Offset),
  1337. false, false, 0));
  1338. }
  1339. SDValue StoreChain;
  1340. if (!Stores.empty()) // Not all undef elements?
  1341. StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  1342. &Stores[0], Stores.size());
  1343. else
  1344. StoreChain = DAG.getEntryNode();
  1345. // Result is a load from the stack slot.
  1346. return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo,
  1347. false, false, false, 0);
  1348. }
  1349. SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
  1350. SDLoc dl(Node);
  1351. SDValue Tmp1 = Node->getOperand(0);
  1352. SDValue Tmp2 = Node->getOperand(1);
  1353. // Get the sign bit of the RHS. First obtain a value that has the same
  1354. // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
  1355. SDValue SignBit;
  1356. EVT FloatVT = Tmp2.getValueType();
  1357. EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
  1358. if (TLI.isTypeLegal(IVT)) {
  1359. // Convert to an integer with the same sign bit.
  1360. SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
  1361. } else {
  1362. // Store the float to memory, then load the sign part out as an integer.
  1363. MVT LoadTy = TLI.getPointerTy();
  1364. // First create a temporary that is aligned for both the load and store.
  1365. SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
  1366. // Then store the float to it.
  1367. SDValue Ch =
  1368. DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
  1369. false, false, 0);
  1370. if (TLI.isBigEndian()) {
  1371. assert(FloatVT.isByteSized() && "Unsupported floating point type!");
  1372. // Load out a legal integer with the same sign bit as the float.
  1373. SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
  1374. false, false, false, 0);
  1375. } else { // Little endian
  1376. SDValue LoadPtr = StackPtr;
  1377. // The float may be wider than the integer we are going to load. Advance
  1378. // the pointer so that the loaded integer will contain the sign bit.
  1379. unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
  1380. unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
  1381. LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
  1382. LoadPtr, DAG.getIntPtrConstant(ByteOffset));
  1383. // Load a legal integer containing the sign bit.
  1384. SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
  1385. false, false, false, 0);
  1386. // Move the sign bit to the top bit of the loaded integer.
  1387. unsigned BitShift = LoadTy.getSizeInBits() -
  1388. (FloatVT.getSizeInBits() - 8 * ByteOffset);
  1389. assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
  1390. if (BitShift)
  1391. SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
  1392. DAG.getConstant(BitShift,
  1393. TLI.getShiftAmountTy(SignBit.getValueType())));
  1394. }
  1395. }
  1396. // Now get the sign bit proper, by seeing whether the value is negative.
  1397. SignBit = DAG.getSetCC(dl, getSetCCResultType(SignBit.getValueType()),
  1398. SignBit, DAG.getConstant(0, SignBit.getValueType()),
  1399. ISD::SETLT);
  1400. // Get the absolute value of the result.
  1401. SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
  1402. // Select between the nabs and abs value based on the sign bit of
  1403. // the input.
  1404. return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
  1405. DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
  1406. AbsVal);
  1407. }
  1408. void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
  1409. SmallVectorImpl<SDValue> &Results) {
  1410. unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
  1411. assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
  1412. " not tell us which reg is the stack pointer!");
  1413. SDLoc dl(Node);
  1414. EVT VT = Node->getValueType(0);
  1415. SDValue Tmp1 = SDValue(Node, 0);
  1416. SDValue Tmp2 = SDValue(Node, 1);
  1417. SDValue Tmp3 = Node->getOperand(2);
  1418. SDValue Chain = Tmp1.getOperand(0);
  1419. // Chain the dynamic stack allocation so that it doesn't modify the stack
  1420. // pointer when other instructions are using the stack.
  1421. Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
  1422. SDValue Size = Tmp2.getOperand(1);
  1423. SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
  1424. Chain = SP.getValue(1);
  1425. unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
  1426. unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
  1427. if (Align > StackAlign)
  1428. SP = DAG.getNode(ISD::AND, dl, VT, SP,
  1429. DAG.getConstant(-(uint64_t)Align, VT));
  1430. Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
  1431. Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
  1432. Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
  1433. DAG.getIntPtrConstant(0, true), SDValue());
  1434. Results.push_back(Tmp1);
  1435. Results.push_back(Tmp2);
  1436. }
  1437. /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
  1438. /// condition code CC on the current target. This routine expands SETCC with
  1439. /// illegal condition code into AND / OR of multiple SETCC values.
  1440. void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
  1441. SDValue &LHS, SDValue &RHS,
  1442. SDValue &CC,
  1443. SDLoc dl) {
  1444. MVT OpVT = LHS.getSimpleValueType();
  1445. ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
  1446. switch (TLI.getCondCodeAction(CCCode, OpVT)) {
  1447. default: llvm_unreachable("Unknown condition code action!");
  1448. case TargetLowering::Legal:
  1449. // Nothing to do.
  1450. break;
  1451. case TargetLowering::Expand: {
  1452. ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
  1453. ISD::CondCode InvCC = ISD::SETCC_INVALID;
  1454. unsigned Opc = 0;
  1455. switch (CCCode) {
  1456. default: llvm_unreachable("Don't know how to expand this condition!");
  1457. case ISD::SETO:
  1458. assert(TLI.getCondCodeAction(ISD::SETOEQ, OpVT)
  1459. == TargetLowering::Legal
  1460. && "If SETO is expanded, SETOEQ must be legal!");
  1461. CC1 = ISD::SETOEQ; CC2 = ISD::SETOEQ; Opc = ISD::AND; break;
  1462. case ISD::SETUO:
  1463. assert(TLI.getCondCodeAction(ISD::SETUNE, OpVT)
  1464. == TargetLowering::Legal
  1465. && "If SETUO is expanded, SETUNE must be legal!");
  1466. CC1 = ISD::SETUNE; CC2 = ISD::SETUNE; Opc = ISD::OR; break;
  1467. case ISD::SETOEQ:
  1468. case ISD::SETOGT:
  1469. case ISD::SETOGE:
  1470. case ISD::SETOLT:
  1471. case ISD::SETOLE:
  1472. case ISD::SETONE:
  1473. case ISD::SETUEQ:
  1474. case ISD::SETUNE:
  1475. case ISD::SETUGT:
  1476. case ISD::SETUGE:
  1477. case ISD::SETULT:
  1478. case ISD::SETULE:
  1479. // If we are floating point, assign and break, otherwise fall through.
  1480. if (!OpVT.isInteger()) {
  1481. // We can use the 4th bit to tell if we are the unordered
  1482. // or ordered version of the opcode.
  1483. CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
  1484. Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
  1485. CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
  1486. break;
  1487. }
  1488. // Fallthrough if we are unsigned integer.
  1489. case ISD::SETLE:
  1490. case ISD::SETGT:
  1491. case ISD::SETGE:
  1492. case ISD::SETLT:
  1493. case ISD::SETNE:
  1494. case ISD::SETEQ:
  1495. InvCC = ISD::getSetCCSwappedOperands(CCCode);
  1496. if (TLI.getCondCodeAction(InvCC, OpVT) == TargetLowering::Expand) {
  1497. // We only support using the inverted operation and not a
  1498. // different manner of supporting expanding these cases.
  1499. llvm_unreachable("Don't know how to expand this condition!");
  1500. }
  1501. LHS = DAG.getSetCC(dl, VT, RHS, LHS, InvCC);
  1502. RHS = SDValue();
  1503. CC = SDValue();
  1504. return;
  1505. }
  1506. SDValue SetCC1, SetCC2;
  1507. if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
  1508. // If we aren't the ordered or unorder operation,
  1509. // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
  1510. SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
  1511. SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
  1512. } else {
  1513. // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
  1514. SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1);
  1515. SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2);
  1516. }
  1517. LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
  1518. RHS = SDValue();
  1519. CC = SDValue();
  1520. break;
  1521. }
  1522. }
  1523. }
  1524. /// EmitStackConvert - Emit a store/load combination to the stack. This stores
  1525. /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
  1526. /// a load from the stack slot to DestVT, extending it if needed.
  1527. /// The resultant code need not be legal.
  1528. SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
  1529. EVT SlotVT,
  1530. EVT DestVT,
  1531. SDLoc dl) {
  1532. // Create the stack frame object.
  1533. unsigned SrcAlign =
  1534. TLI.getDataLayout()->getPrefTypeAlignment(SrcOp.getValueType().
  1535. getTypeForEVT(*DAG.getContext()));
  1536. SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
  1537. FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
  1538. int SPFI = StackPtrFI->getIndex();
  1539. MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
  1540. unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
  1541. unsigned SlotSize = SlotVT.getSizeInBits();
  1542. unsigned DestSize = DestVT.getSizeInBits();
  1543. Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
  1544. unsigned DestAlign = TLI.getDataLayout()->getPrefTypeAlignment(DestType);
  1545. // Emit a store to the stack slot. Use a truncstore if the input value is
  1546. // later than DestVT.
  1547. SDValue Store;
  1548. if (SrcSize > SlotSize)
  1549. Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
  1550. PtrInfo, SlotVT, false, false, SrcAlign);
  1551. else {
  1552. assert(SrcSize == SlotSize && "Invalid store");
  1553. Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
  1554. PtrInfo, false, false, SrcAlign);
  1555. }
  1556. // Result is a load from the stack slot.
  1557. if (SlotSize == DestSize)
  1558. return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
  1559. false, false, false, DestAlign);
  1560. assert(SlotSize < DestSize && "Unknown extension!");
  1561. return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
  1562. PtrInfo, SlotVT, false, false, DestAlign);
  1563. }
  1564. SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
  1565. SDLoc dl(Node);
  1566. // Create a vector sized/aligned stack slot, store the value to element #0,
  1567. // then load the whole vector back out.
  1568. SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
  1569. FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
  1570. int SPFI = StackPtrFI->getIndex();
  1571. SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
  1572. StackPtr,
  1573. MachinePointerInfo::getFixedStack(SPFI),
  1574. Node->getValueType(0).getVectorElementType(),
  1575. false, false, 0);
  1576. return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
  1577. MachinePointerInfo::getFixedStack(SPFI),
  1578. false, false, false, 0);
  1579. }
  1580. /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
  1581. /// support the operation, but do support the resultant vector type.
  1582. SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
  1583. unsigned NumElems = Node->getNumOperands();
  1584. SDValue Value1, Value2;
  1585. SDLoc dl(Node);
  1586. EVT VT = Node->getValueType(0);
  1587. EVT OpVT = Node->getOperand(0).getValueType();
  1588. EVT EltVT = VT.getVectorElementType();
  1589. // If the only non-undef value is the low element, turn this into a
  1590. // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
  1591. bool isOnlyLowElement = true;
  1592. bool MoreThanTwoValues = false;
  1593. bool isConstant = true;
  1594. for (unsigned i = 0; i < NumElems; ++i) {
  1595. SDValue V = Node->getOperand(i);
  1596. if (V.getOpcode() == ISD::UNDEF)
  1597. continue;
  1598. if (i > 0)
  1599. isOnlyLowElement = false;
  1600. if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
  1601. isConstant = false;
  1602. if (!Value1.getNode()) {
  1603. Value1 = V;
  1604. } else if (!Value2.getNode()) {
  1605. if (V != Value1)
  1606. Value2 = V;
  1607. } else if (V != Value1 && V != Value2) {
  1608. MoreThanTwoValues = true;
  1609. }
  1610. }
  1611. if (!Value1.getNode())
  1612. return DAG.getUNDEF(VT);
  1613. if (isOnlyLowElement)
  1614. return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
  1615. // If all elements are constants, create a load from the constant pool.
  1616. if (isConstant) {
  1617. SmallVector<Constant*, 16> CV;
  1618. for (unsigned i = 0, e = NumElems; i != e; ++i) {
  1619. if (ConstantFPSDNode *V =
  1620. dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
  1621. CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
  1622. } else if (ConstantSDNode *V =
  1623. dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
  1624. if (OpVT==EltVT)
  1625. CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
  1626. else {
  1627. // If OpVT and EltVT don't match, EltVT is not legal and the
  1628. // element values have been promoted/truncated earlier. Undo this;
  1629. // we don't want a v16i8 to become a v16i32 for example.
  1630. const ConstantInt *CI = V->getConstantIntValue();
  1631. CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
  1632. CI->getZExtValue()));
  1633. }
  1634. } else {
  1635. assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
  1636. Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
  1637. CV.push_back(UndefValue::get(OpNTy));
  1638. }
  1639. }
  1640. Constant *CP = ConstantVector::get(CV);
  1641. SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
  1642. unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
  1643. return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
  1644. MachinePointerInfo::getConstantPool(),
  1645. false, false, false, Alignment);
  1646. }
  1647. if (!MoreThanTwoValues) {
  1648. SmallVector<int, 8> ShuffleVec(NumElems, -1);
  1649. for (unsigned i = 0; i < NumElems; ++i) {
  1650. SDValue V = Node->getOperand(i);
  1651. if (V.getOpcode() == ISD::UNDEF)
  1652. continue;
  1653. ShuffleVec[i] = V == Value1 ? 0 : NumElems;
  1654. }
  1655. if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
  1656. // Get the splatted value into the low element of a vector register.
  1657. SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
  1658. SDValue Vec2;
  1659. if (Value2.getNode())
  1660. Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
  1661. else
  1662. Vec2 = DAG.getUNDEF(VT);
  1663. // Return shuffle(LowValVec, undef, <0,0,0,0>)
  1664. return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
  1665. }
  1666. }
  1667. // Otherwise, we can't handle this case efficiently.
  1668. return ExpandVectorBuildThroughStack(Node);
  1669. }
  1670. // ExpandLibCall - Expand a node into a call to a libcall. If the result value
  1671. // does not fit into a register, return the lo part and set the hi part to the
  1672. // by-reg argument. If it does fit into a single register, return the result
  1673. // and leave the Hi part unset.
  1674. SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
  1675. bool isSigned) {
  1676. TargetLowering::ArgListTy Args;
  1677. TargetLowering::ArgListEntry Entry;
  1678. for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
  1679. EVT ArgVT = Node->getOperand(i).getValueType();
  1680. Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
  1681. Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
  1682. Entry.isSExt = isSigned;
  1683. Entry.isZExt = !isSigned;
  1684. Args.push_back(Entry);
  1685. }
  1686. SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
  1687. TLI.getPointerTy());
  1688. Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
  1689. // By default, the input chain to this libcall is the entry node of the
  1690. // function. If the libcall is going to be emitted as a tail call then
  1691. // TLI.isUsedByReturnOnly will change it to the right chain if the return
  1692. // node which is being folded has a non-entry input chain.
  1693. SDValue InChain = DAG.getEntryNode();
  1694. // isTailCall may be true since the callee does not reference caller stack
  1695. // frame. Check if it's in the right position.
  1696. SDValue TCChain = InChain;
  1697. bool isTailCall = TLI.isInTailCallPosition(DAG, Node, TCChain);
  1698. if (isTailCall)
  1699. InChain = TCChain;
  1700. TargetLowering::
  1701. CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, false,
  1702. 0, TLI.getLibcallCallingConv(LC), isTailCall,
  1703. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  1704. Callee, Args, DAG, SDLoc(Node));
  1705. std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
  1706. if (!CallInfo.second.getNode())
  1707. // It's a tailcall, return the chain (which is the DAG root).
  1708. return DAG.getRoot();
  1709. return CallInfo.first;
  1710. }
  1711. /// ExpandLibCall - Generate a libcall taking the given operands as arguments
  1712. /// and returning a result of type RetVT.
  1713. SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT,
  1714. const SDValue *Ops, unsigned NumOps,
  1715. bool isSigned, SDLoc dl) {
  1716. TargetLowering::ArgListTy Args;
  1717. Args.reserve(NumOps);
  1718. TargetLowering::ArgListEntry Entry;
  1719. for (unsigned i = 0; i != NumOps; ++i) {
  1720. Entry.Node = Ops[i];
  1721. Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
  1722. Entry.isSExt = isSigned;
  1723. Entry.isZExt = !isSigned;
  1724. Args.push_back(Entry);
  1725. }
  1726. SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
  1727. TLI.getPointerTy());
  1728. Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
  1729. TargetLowering::
  1730. CallLoweringInfo CLI(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
  1731. false, 0, TLI.getLibcallCallingConv(LC),
  1732. /*isTailCall=*/false,
  1733. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  1734. Callee, Args, DAG, dl);
  1735. std::pair<SDValue,SDValue> CallInfo = TLI.LowerCallTo(CLI);
  1736. return CallInfo.first;
  1737. }
  1738. // ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
  1739. // ExpandLibCall except that the first operand is the in-chain.
  1740. std::pair<SDValue, SDValue>
  1741. SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
  1742. SDNode *Node,
  1743. bool isSigned) {
  1744. SDValue InChain = Node->getOperand(0);
  1745. TargetLowering::ArgListTy Args;
  1746. TargetLowering::ArgListEntry Entry;
  1747. for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
  1748. EVT ArgVT = Node->getOperand(i).getValueType();
  1749. Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
  1750. Entry.Node = Node->getOperand(i);
  1751. Entry.Ty = ArgTy;
  1752. Entry.isSExt = isSigned;
  1753. Entry.isZExt = !isSigned;
  1754. Args.push_back(Entry);
  1755. }
  1756. SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
  1757. TLI.getPointerTy());
  1758. Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
  1759. TargetLowering::
  1760. CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, false,
  1761. 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
  1762. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  1763. Callee, Args, DAG, SDLoc(Node));
  1764. std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
  1765. return CallInfo;
  1766. }
  1767. SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
  1768. RTLIB::Libcall Call_F32,
  1769. RTLIB::Libcall Call_F64,
  1770. RTLIB::Libcall Call_F80,
  1771. RTLIB::Libcall Call_F128,
  1772. RTLIB::Libcall Call_PPCF128) {
  1773. RTLIB::Libcall LC;
  1774. switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
  1775. default: llvm_unreachable("Unexpected request for libcall!");
  1776. case MVT::f32: LC = Call_F32; break;
  1777. case MVT::f64: LC = Call_F64; break;
  1778. case MVT::f80: LC = Call_F80; break;
  1779. case MVT::f128: LC = Call_F128; break;
  1780. case MVT::ppcf128: LC = Call_PPCF128; break;
  1781. }
  1782. return ExpandLibCall(LC, Node, false);
  1783. }
  1784. SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
  1785. RTLIB::Libcall Call_I8,
  1786. RTLIB::Libcall Call_I16,
  1787. RTLIB::Libcall Call_I32,
  1788. RTLIB::Libcall Call_I64,
  1789. RTLIB::Libcall Call_I128) {
  1790. RTLIB::Libcall LC;
  1791. switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
  1792. default: llvm_unreachable("Unexpected request for libcall!");
  1793. case MVT::i8: LC = Call_I8; break;
  1794. case MVT::i16: LC = Call_I16; break;
  1795. case MVT::i32: LC = Call_I32; break;
  1796. case MVT::i64: LC = Call_I64; break;
  1797. case MVT::i128: LC = Call_I128; break;
  1798. }
  1799. return ExpandLibCall(LC, Node, isSigned);
  1800. }
  1801. /// isDivRemLibcallAvailable - Return true if divmod libcall is available.
  1802. static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
  1803. const TargetLowering &TLI) {
  1804. RTLIB::Libcall LC;
  1805. switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
  1806. default: llvm_unreachable("Unexpected request for libcall!");
  1807. case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
  1808. case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
  1809. case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
  1810. case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
  1811. case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
  1812. }
  1813. return TLI.getLibcallName(LC) != 0;
  1814. }
  1815. /// useDivRem - Only issue divrem libcall if both quotient and remainder are
  1816. /// needed.
  1817. static bool useDivRem(SDNode *Node, bool isSigned, bool isDIV) {
  1818. // The other use might have been replaced with a divrem already.
  1819. unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
  1820. unsigned OtherOpcode = 0;
  1821. if (isSigned)
  1822. OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
  1823. else
  1824. OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
  1825. SDValue Op0 = Node->getOperand(0);
  1826. SDValue Op1 = Node->getOperand(1);
  1827. for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
  1828. UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
  1829. SDNode *User = *UI;
  1830. if (User == Node)
  1831. continue;
  1832. if ((User->getOpcode() == OtherOpcode || User->getOpcode() == DivRemOpc) &&
  1833. User->getOperand(0) == Op0 &&
  1834. User->getOperand(1) == Op1)
  1835. return true;
  1836. }
  1837. return false;
  1838. }
  1839. /// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
  1840. /// pairs.
  1841. void
  1842. SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
  1843. SmallVectorImpl<SDValue> &Results) {
  1844. unsigned Opcode = Node->getOpcode();
  1845. bool isSigned = Opcode == ISD::SDIVREM;
  1846. RTLIB::Libcall LC;
  1847. switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
  1848. default: llvm_unreachable("Unexpected request for libcall!");
  1849. case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
  1850. case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
  1851. case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
  1852. case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
  1853. case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
  1854. }
  1855. // The input chain to this libcall is the entry node of the function.
  1856. // Legalizing the call will automatically add the previous call to the
  1857. // dependence.
  1858. SDValue InChain = DAG.getEntryNode();
  1859. EVT RetVT = Node->getValueType(0);
  1860. Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
  1861. TargetLowering::ArgListTy Args;
  1862. TargetLowering::ArgListEntry Entry;
  1863. for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
  1864. EVT ArgVT = Node->getOperand(i).getValueType();
  1865. Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
  1866. Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
  1867. Entry.isSExt = isSigned;
  1868. Entry.isZExt = !isSigned;
  1869. Args.push_back(Entry);
  1870. }
  1871. // Also pass the return address of the remainder.
  1872. SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
  1873. Entry.Node = FIPtr;
  1874. Entry.Ty = RetTy->getPointerTo();
  1875. Entry.isSExt = isSigned;
  1876. Entry.isZExt = !isSigned;
  1877. Args.push_back(Entry);
  1878. SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
  1879. TLI.getPointerTy());
  1880. SDLoc dl(Node);
  1881. TargetLowering::
  1882. CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, false,
  1883. 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
  1884. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  1885. Callee, Args, DAG, dl);
  1886. std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
  1887. // Remainder is loaded back from the stack frame.
  1888. SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr,
  1889. MachinePointerInfo(), false, false, false, 0);
  1890. Results.push_back(CallInfo.first);
  1891. Results.push_back(Rem);
  1892. }
  1893. /// isSinCosLibcallAvailable - Return true if sincos libcall is available.
  1894. static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
  1895. RTLIB::Libcall LC;
  1896. switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
  1897. default: llvm_unreachable("Unexpected request for libcall!");
  1898. case MVT::f32: LC = RTLIB::SINCOS_F32; break;
  1899. case MVT::f64: LC = RTLIB::SINCOS_F64; break;
  1900. case MVT::f80: LC = RTLIB::SINCOS_F80; break;
  1901. case MVT::f128: LC = RTLIB::SINCOS_F128; break;
  1902. case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
  1903. }
  1904. return TLI.getLibcallName(LC) != 0;
  1905. }
  1906. /// canCombineSinCosLibcall - Return true if sincos libcall is available and
  1907. /// can be used to combine sin and cos.
  1908. static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
  1909. const TargetMachine &TM) {
  1910. if (!isSinCosLibcallAvailable(Node, TLI))
  1911. return false;
  1912. // GNU sin/cos functions set errno while sincos does not. Therefore
  1913. // combining sin and cos is only safe if unsafe-fpmath is enabled.
  1914. bool isGNU = Triple(TM.getTargetTriple()).getEnvironment() == Triple::GNU;
  1915. if (isGNU && !TM.Options.UnsafeFPMath)
  1916. return false;
  1917. return true;
  1918. }
  1919. /// useSinCos - Only issue sincos libcall if both sin and cos are
  1920. /// needed.
  1921. static bool useSinCos(SDNode *Node) {
  1922. unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
  1923. ? ISD::FCOS : ISD::FSIN;
  1924. SDValue Op0 = Node->getOperand(0);
  1925. for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
  1926. UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
  1927. SDNode *User = *UI;
  1928. if (User == Node)
  1929. continue;
  1930. // The other user might have been turned into sincos already.
  1931. if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
  1932. return true;
  1933. }
  1934. return false;
  1935. }
  1936. /// ExpandSinCosLibCall - Issue libcalls to sincos to compute sin / cos
  1937. /// pairs.
  1938. void
  1939. SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
  1940. SmallVectorImpl<SDValue> &Results) {
  1941. RTLIB::Libcall LC;
  1942. switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
  1943. default: llvm_unreachable("Unexpected request for libcall!");
  1944. case MVT::f32: LC = RTLIB::SINCOS_F32; break;
  1945. case MVT::f64: LC = RTLIB::SINCOS_F64; break;
  1946. case MVT::f80: LC = RTLIB::SINCOS_F80; break;
  1947. case MVT::f128: LC = RTLIB::SINCOS_F128; break;
  1948. case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
  1949. }
  1950. // The input chain to this libcall is the entry node of the function.
  1951. // Legalizing the call will automatically add the previous call to the
  1952. // dependence.
  1953. SDValue InChain = DAG.getEntryNode();
  1954. EVT RetVT = Node->getValueType(0);
  1955. Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
  1956. TargetLowering::ArgListTy Args;
  1957. TargetLowering::ArgListEntry Entry;
  1958. // Pass the argument.
  1959. Entry.Node = Node->getOperand(0);
  1960. Entry.Ty = RetTy;
  1961. Entry.isSExt = false;
  1962. Entry.isZExt = false;
  1963. Args.push_back(Entry);
  1964. // Pass the return address of sin.
  1965. SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
  1966. Entry.Node = SinPtr;
  1967. Entry.Ty = RetTy->getPointerTo();
  1968. Entry.isSExt = false;
  1969. Entry.isZExt = false;
  1970. Args.push_back(Entry);
  1971. // Also pass the return address of the cos.
  1972. SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
  1973. Entry.Node = CosPtr;
  1974. Entry.Ty = RetTy->getPointerTo();
  1975. Entry.isSExt = false;
  1976. Entry.isZExt = false;
  1977. Args.push_back(Entry);
  1978. SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
  1979. TLI.getPointerTy());
  1980. SDLoc dl(Node);
  1981. TargetLowering::
  1982. CallLoweringInfo CLI(InChain, Type::getVoidTy(*DAG.getContext()),
  1983. false, false, false, false,
  1984. 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
  1985. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  1986. Callee, Args, DAG, dl);
  1987. std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
  1988. Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr,
  1989. MachinePointerInfo(), false, false, false, 0));
  1990. Results.push_back(DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr,
  1991. MachinePointerInfo(), false, false, false, 0));
  1992. }
  1993. /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
  1994. /// INT_TO_FP operation of the specified operand when the target requests that
  1995. /// we expand it. At this point, we know that the result and operand types are
  1996. /// legal for the target.
  1997. SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
  1998. SDValue Op0,
  1999. EVT DestVT,
  2000. SDLoc dl) {
  2001. if (Op0.getValueType() == MVT::i32 && TLI.isTypeLegal(MVT::f64)) {
  2002. // simple 32-bit [signed|unsigned] integer to float/double expansion
  2003. // Get the stack frame index of a 8 byte buffer.
  2004. SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
  2005. // word offset constant for Hi/Lo address computation
  2006. SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
  2007. // set up Hi and Lo (into buffer) address based on endian
  2008. SDValue Hi = StackSlot;
  2009. SDValue Lo = DAG.getNode(ISD::ADD, dl,
  2010. TLI.getPointerTy(), StackSlot, WordOff);
  2011. if (TLI.isLittleEndian())
  2012. std::swap(Hi, Lo);
  2013. // if signed map to unsigned space
  2014. SDValue Op0Mapped;
  2015. if (isSigned) {
  2016. // constant used to invert sign bit (signed to unsigned mapping)
  2017. SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
  2018. Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
  2019. } else {
  2020. Op0Mapped = Op0;
  2021. }
  2022. // store the lo of the constructed double - based on integer input
  2023. SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
  2024. Op0Mapped, Lo, MachinePointerInfo(),
  2025. false, false, 0);
  2026. // initial hi portion of constructed double
  2027. SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
  2028. // store the hi of the constructed double - biased exponent
  2029. SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
  2030. MachinePointerInfo(),
  2031. false, false, 0);
  2032. // load the constructed double
  2033. SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
  2034. MachinePointerInfo(), false, false, false, 0);
  2035. // FP constant to bias correct the final result
  2036. SDValue Bias = DAG.getConstantFP(isSigned ?
  2037. BitsToDouble(0x4330000080000000ULL) :
  2038. BitsToDouble(0x4330000000000000ULL),
  2039. MVT::f64);
  2040. // subtract the bias
  2041. SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
  2042. // final result
  2043. SDValue Result;
  2044. // handle final rounding
  2045. if (DestVT == MVT::f64) {
  2046. // do nothing
  2047. Result = Sub;
  2048. } else if (DestVT.bitsLT(MVT::f64)) {
  2049. Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
  2050. DAG.getIntPtrConstant(0));
  2051. } else if (DestVT.bitsGT(MVT::f64)) {
  2052. Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
  2053. }
  2054. return Result;
  2055. }
  2056. assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
  2057. // Code below here assumes !isSigned without checking again.
  2058. // Implementation of unsigned i64 to f64 following the algorithm in
  2059. // __floatundidf in compiler_rt. This implementation has the advantage
  2060. // of performing rounding correctly, both in the default rounding mode
  2061. // and in all alternate rounding modes.
  2062. // TODO: Generalize this for use with other types.
  2063. if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
  2064. SDValue TwoP52 =
  2065. DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
  2066. SDValue TwoP84PlusTwoP52 =
  2067. DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
  2068. SDValue TwoP84 =
  2069. DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
  2070. SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
  2071. SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
  2072. DAG.getConstant(32, MVT::i64));
  2073. SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
  2074. SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
  2075. SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
  2076. SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
  2077. SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
  2078. TwoP84PlusTwoP52);
  2079. return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
  2080. }
  2081. // Implementation of unsigned i64 to f32.
  2082. // TODO: Generalize this for use with other types.
  2083. if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
  2084. // For unsigned conversions, convert them to signed conversions using the
  2085. // algorithm from the x86_64 __floatundidf in compiler_rt.
  2086. if (!isSigned) {
  2087. SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
  2088. SDValue ShiftConst =
  2089. DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
  2090. SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
  2091. SDValue AndConst = DAG.getConstant(1, MVT::i64);
  2092. SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
  2093. SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
  2094. SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
  2095. SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
  2096. // TODO: This really should be implemented using a branch rather than a
  2097. // select. We happen to get lucky and machinesink does the right
  2098. // thing most of the time. This would be a good candidate for a
  2099. //pseudo-op, or, even better, for whole-function isel.
  2100. SDValue SignBitTest = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
  2101. Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
  2102. return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
  2103. }
  2104. // Otherwise, implement the fully general conversion.
  2105. SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
  2106. DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
  2107. SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
  2108. DAG.getConstant(UINT64_C(0x800), MVT::i64));
  2109. SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
  2110. DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
  2111. SDValue Ne = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
  2112. And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
  2113. SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
  2114. SDValue Ge = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
  2115. Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
  2116. ISD::SETUGE);
  2117. SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
  2118. EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
  2119. SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
  2120. DAG.getConstant(32, SHVT));
  2121. SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
  2122. SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
  2123. SDValue TwoP32 =
  2124. DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
  2125. SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
  2126. SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
  2127. SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
  2128. SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
  2129. return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
  2130. DAG.getIntPtrConstant(0));
  2131. }
  2132. SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
  2133. SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(Op0.getValueType()),
  2134. Op0, DAG.getConstant(0, Op0.getValueType()),
  2135. ISD::SETLT);
  2136. SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
  2137. SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
  2138. SignSet, Four, Zero);
  2139. // If the sign bit of the integer is set, the large number will be treated
  2140. // as a negative number. To counteract this, the dynamic code adds an
  2141. // offset depending on the data type.
  2142. uint64_t FF;
  2143. switch (Op0.getValueType().getSimpleVT().SimpleTy) {
  2144. default: llvm_unreachable("Unsupported integer type!");
  2145. case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
  2146. case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
  2147. case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
  2148. case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
  2149. }
  2150. if (TLI.isLittleEndian()) FF <<= 32;
  2151. Constant *FudgeFactor = ConstantInt::get(
  2152. Type::getInt64Ty(*DAG.getContext()), FF);
  2153. SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
  2154. unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
  2155. CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
  2156. Alignment = std::min(Alignment, 4u);
  2157. SDValue FudgeInReg;
  2158. if (DestVT == MVT::f32)
  2159. FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
  2160. MachinePointerInfo::getConstantPool(),
  2161. false, false, false, Alignment);
  2162. else {
  2163. SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
  2164. DAG.getEntryNode(), CPIdx,
  2165. MachinePointerInfo::getConstantPool(),
  2166. MVT::f32, false, false, Alignment);
  2167. HandleSDNode Handle(Load);
  2168. LegalizeOp(Load.getNode());
  2169. FudgeInReg = Handle.getValue();
  2170. }
  2171. return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
  2172. }
  2173. /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
  2174. /// *INT_TO_FP operation of the specified operand when the target requests that
  2175. /// we promote it. At this point, we know that the result and operand types are
  2176. /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
  2177. /// operation that takes a larger input.
  2178. SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
  2179. EVT DestVT,
  2180. bool isSigned,
  2181. SDLoc dl) {
  2182. // First step, figure out the appropriate *INT_TO_FP operation to use.
  2183. EVT NewInTy = LegalOp.getValueType();
  2184. unsigned OpToUse = 0;
  2185. // Scan for the appropriate larger type to use.
  2186. while (1) {
  2187. NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
  2188. assert(NewInTy.isInteger() && "Ran out of possibilities!");
  2189. // If the target supports SINT_TO_FP of this type, use it.
  2190. if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
  2191. OpToUse = ISD::SINT_TO_FP;
  2192. break;
  2193. }
  2194. if (isSigned) continue;
  2195. // If the target supports UINT_TO_FP of this type, use it.
  2196. if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
  2197. OpToUse = ISD::UINT_TO_FP;
  2198. break;
  2199. }
  2200. // Otherwise, try a larger type.
  2201. }
  2202. // Okay, we found the operation and type to use. Zero extend our input to the
  2203. // desired type then run the operation on it.
  2204. return DAG.getNode(OpToUse, dl, DestVT,
  2205. DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
  2206. dl, NewInTy, LegalOp));
  2207. }
  2208. /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
  2209. /// FP_TO_*INT operation of the specified operand when the target requests that
  2210. /// we promote it. At this point, we know that the result and operand types are
  2211. /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
  2212. /// operation that returns a larger result.
  2213. SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
  2214. EVT DestVT,
  2215. bool isSigned,
  2216. SDLoc dl) {
  2217. // First step, figure out the appropriate FP_TO*INT operation to use.
  2218. EVT NewOutTy = DestVT;
  2219. unsigned OpToUse = 0;
  2220. // Scan for the appropriate larger type to use.
  2221. while (1) {
  2222. NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
  2223. assert(NewOutTy.isInteger() && "Ran out of possibilities!");
  2224. if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
  2225. OpToUse = ISD::FP_TO_SINT;
  2226. break;
  2227. }
  2228. if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
  2229. OpToUse = ISD::FP_TO_UINT;
  2230. break;
  2231. }
  2232. // Otherwise, try a larger type.
  2233. }
  2234. // Okay, we found the operation and type to use.
  2235. SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
  2236. // Truncate the result of the extended FP_TO_*INT operation to the desired
  2237. // size.
  2238. return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
  2239. }
  2240. /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
  2241. ///
  2242. SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, SDLoc dl) {
  2243. EVT VT = Op.getValueType();
  2244. EVT SHVT = TLI.getShiftAmountTy(VT);
  2245. SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
  2246. switch (VT.getSimpleVT().SimpleTy) {
  2247. default: llvm_unreachable("Unhandled Expand type in BSWAP!");
  2248. case MVT::i16:
  2249. Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
  2250. Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
  2251. return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
  2252. case MVT::i32:
  2253. Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
  2254. Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
  2255. Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
  2256. Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
  2257. Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
  2258. Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
  2259. Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
  2260. Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
  2261. return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
  2262. case MVT::i64:
  2263. Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
  2264. Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
  2265. Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
  2266. Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
  2267. Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
  2268. Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
  2269. Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
  2270. Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
  2271. Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
  2272. Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
  2273. Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
  2274. Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
  2275. Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
  2276. Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
  2277. Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
  2278. Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
  2279. Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
  2280. Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
  2281. Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
  2282. Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
  2283. return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
  2284. }
  2285. }
  2286. /// ExpandBitCount - Expand the specified bitcount instruction into operations.
  2287. ///
  2288. SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
  2289. SDLoc dl) {
  2290. switch (Opc) {
  2291. default: llvm_unreachable("Cannot expand this yet!");
  2292. case ISD::CTPOP: {
  2293. EVT VT = Op.getValueType();
  2294. EVT ShVT = TLI.getShiftAmountTy(VT);
  2295. unsigned Len = VT.getSizeInBits();
  2296. assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
  2297. "CTPOP not implemented for this type.");
  2298. // This is the "best" algorithm from
  2299. // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  2300. SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), VT);
  2301. SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), VT);
  2302. SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), VT);
  2303. SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), VT);
  2304. // v = v - ((v >> 1) & 0x55555555...)
  2305. Op = DAG.getNode(ISD::SUB, dl, VT, Op,
  2306. DAG.getNode(ISD::AND, dl, VT,
  2307. DAG.getNode(ISD::SRL, dl, VT, Op,
  2308. DAG.getConstant(1, ShVT)),
  2309. Mask55));
  2310. // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
  2311. Op = DAG.getNode(ISD::ADD, dl, VT,
  2312. DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
  2313. DAG.getNode(ISD::AND, dl, VT,
  2314. DAG.getNode(ISD::SRL, dl, VT, Op,
  2315. DAG.getConstant(2, ShVT)),
  2316. Mask33));
  2317. // v = (v + (v >> 4)) & 0x0F0F0F0F...
  2318. Op = DAG.getNode(ISD::AND, dl, VT,
  2319. DAG.getNode(ISD::ADD, dl, VT, Op,
  2320. DAG.getNode(ISD::SRL, dl, VT, Op,
  2321. DAG.getConstant(4, ShVT))),
  2322. Mask0F);
  2323. // v = (v * 0x01010101...) >> (Len - 8)
  2324. Op = DAG.getNode(ISD::SRL, dl, VT,
  2325. DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
  2326. DAG.getConstant(Len - 8, ShVT));
  2327. return Op;
  2328. }
  2329. case ISD::CTLZ_ZERO_UNDEF:
  2330. // This trivially expands to CTLZ.
  2331. return DAG.getNode(ISD::CTLZ, dl, Op.getValueType(), Op);
  2332. case ISD::CTLZ: {
  2333. // for now, we do this:
  2334. // x = x | (x >> 1);
  2335. // x = x | (x >> 2);
  2336. // ...
  2337. // x = x | (x >>16);
  2338. // x = x | (x >>32); // for 64-bit input
  2339. // return popcount(~x);
  2340. //
  2341. // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
  2342. EVT VT = Op.getValueType();
  2343. EVT ShVT = TLI.getShiftAmountTy(VT);
  2344. unsigned len = VT.getSizeInBits();
  2345. for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
  2346. SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
  2347. Op = DAG.getNode(ISD::OR, dl, VT, Op,
  2348. DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
  2349. }
  2350. Op = DAG.getNOT(dl, Op, VT);
  2351. return DAG.getNode(ISD::CTPOP, dl, VT, Op);
  2352. }
  2353. case ISD::CTTZ_ZERO_UNDEF:
  2354. // This trivially expands to CTTZ.
  2355. return DAG.getNode(ISD::CTTZ, dl, Op.getValueType(), Op);
  2356. case ISD::CTTZ: {
  2357. // for now, we use: { return popcount(~x & (x - 1)); }
  2358. // unless the target has ctlz but not ctpop, in which case we use:
  2359. // { return 32 - nlz(~x & (x-1)); }
  2360. // see also http://www.hackersdelight.org/HDcode/ntz.cc
  2361. EVT VT = Op.getValueType();
  2362. SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
  2363. DAG.getNOT(dl, Op, VT),
  2364. DAG.getNode(ISD::SUB, dl, VT, Op,
  2365. DAG.getConstant(1, VT)));
  2366. // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
  2367. if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
  2368. TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
  2369. return DAG.getNode(ISD::SUB, dl, VT,
  2370. DAG.getConstant(VT.getSizeInBits(), VT),
  2371. DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
  2372. return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
  2373. }
  2374. }
  2375. }
  2376. std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
  2377. unsigned Opc = Node->getOpcode();
  2378. MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
  2379. RTLIB::Libcall LC;
  2380. switch (Opc) {
  2381. default:
  2382. llvm_unreachable("Unhandled atomic intrinsic Expand!");
  2383. case ISD::ATOMIC_SWAP:
  2384. switch (VT.SimpleTy) {
  2385. default: llvm_unreachable("Unexpected value type for atomic!");
  2386. case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
  2387. case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
  2388. case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
  2389. case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
  2390. }
  2391. break;
  2392. case ISD::ATOMIC_CMP_SWAP:
  2393. switch (VT.SimpleTy) {
  2394. default: llvm_unreachable("Unexpected value type for atomic!");
  2395. case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
  2396. case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
  2397. case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
  2398. case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
  2399. }
  2400. break;
  2401. case ISD::ATOMIC_LOAD_ADD:
  2402. switch (VT.SimpleTy) {
  2403. default: llvm_unreachable("Unexpected value type for atomic!");
  2404. case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
  2405. case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
  2406. case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
  2407. case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
  2408. }
  2409. break;
  2410. case ISD::ATOMIC_LOAD_SUB:
  2411. switch (VT.SimpleTy) {
  2412. default: llvm_unreachable("Unexpected value type for atomic!");
  2413. case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
  2414. case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
  2415. case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
  2416. case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
  2417. }
  2418. break;
  2419. case ISD::ATOMIC_LOAD_AND:
  2420. switch (VT.SimpleTy) {
  2421. default: llvm_unreachable("Unexpected value type for atomic!");
  2422. case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
  2423. case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
  2424. case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
  2425. case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
  2426. }
  2427. break;
  2428. case ISD::ATOMIC_LOAD_OR:
  2429. switch (VT.SimpleTy) {
  2430. default: llvm_unreachable("Unexpected value type for atomic!");
  2431. case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
  2432. case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
  2433. case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
  2434. case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
  2435. }
  2436. break;
  2437. case ISD::ATOMIC_LOAD_XOR:
  2438. switch (VT.SimpleTy) {
  2439. default: llvm_unreachable("Unexpected value type for atomic!");
  2440. case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
  2441. case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
  2442. case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
  2443. case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
  2444. }
  2445. break;
  2446. case ISD::ATOMIC_LOAD_NAND:
  2447. switch (VT.SimpleTy) {
  2448. default: llvm_unreachable("Unexpected value type for atomic!");
  2449. case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
  2450. case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
  2451. case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
  2452. case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
  2453. }
  2454. break;
  2455. }
  2456. return ExpandChainLibCall(LC, Node, false);
  2457. }
  2458. void SelectionDAGLegalize::ExpandNode(SDNode *Node) {
  2459. SmallVector<SDValue, 8> Results;
  2460. SDLoc dl(Node);
  2461. SDValue Tmp1, Tmp2, Tmp3, Tmp4;
  2462. switch (Node->getOpcode()) {
  2463. case ISD::CTPOP:
  2464. case ISD::CTLZ:
  2465. case ISD::CTLZ_ZERO_UNDEF:
  2466. case ISD::CTTZ:
  2467. case ISD::CTTZ_ZERO_UNDEF:
  2468. Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
  2469. Results.push_back(Tmp1);
  2470. break;
  2471. case ISD::BSWAP:
  2472. Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
  2473. break;
  2474. case ISD::FRAMEADDR:
  2475. case ISD::RETURNADDR:
  2476. case ISD::FRAME_TO_ARGS_OFFSET:
  2477. Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
  2478. break;
  2479. case ISD::FLT_ROUNDS_:
  2480. Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
  2481. break;
  2482. case ISD::EH_RETURN:
  2483. case ISD::EH_LABEL:
  2484. case ISD::PREFETCH:
  2485. case ISD::VAEND:
  2486. case ISD::EH_SJLJ_LONGJMP:
  2487. // If the target didn't expand these, there's nothing to do, so just
  2488. // preserve the chain and be done.
  2489. Results.push_back(Node->getOperand(0));
  2490. break;
  2491. case ISD::EH_SJLJ_SETJMP:
  2492. // If the target didn't expand this, just return 'zero' and preserve the
  2493. // chain.
  2494. Results.push_back(DAG.getConstant(0, MVT::i32));
  2495. Results.push_back(Node->getOperand(0));
  2496. break;
  2497. case ISD::ATOMIC_FENCE: {
  2498. // If the target didn't lower this, lower it to '__sync_synchronize()' call
  2499. // FIXME: handle "fence singlethread" more efficiently.
  2500. TargetLowering::ArgListTy Args;
  2501. TargetLowering::
  2502. CallLoweringInfo CLI(Node->getOperand(0),
  2503. Type::getVoidTy(*DAG.getContext()),
  2504. false, false, false, false, 0, CallingConv::C,
  2505. /*isTailCall=*/false,
  2506. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  2507. DAG.getExternalSymbol("__sync_synchronize",
  2508. TLI.getPointerTy()),
  2509. Args, DAG, dl);
  2510. std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
  2511. Results.push_back(CallResult.second);
  2512. break;
  2513. }
  2514. case ISD::ATOMIC_LOAD: {
  2515. // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
  2516. SDValue Zero = DAG.getConstant(0, Node->getValueType(0));
  2517. SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
  2518. cast<AtomicSDNode>(Node)->getMemoryVT(),
  2519. Node->getOperand(0),
  2520. Node->getOperand(1), Zero, Zero,
  2521. cast<AtomicSDNode>(Node)->getMemOperand(),
  2522. cast<AtomicSDNode>(Node)->getOrdering(),
  2523. cast<AtomicSDNode>(Node)->getSynchScope());
  2524. Results.push_back(Swap.getValue(0));
  2525. Results.push_back(Swap.getValue(1));
  2526. break;
  2527. }
  2528. case ISD::ATOMIC_STORE: {
  2529. // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
  2530. SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
  2531. cast<AtomicSDNode>(Node)->getMemoryVT(),
  2532. Node->getOperand(0),
  2533. Node->getOperand(1), Node->getOperand(2),
  2534. cast<AtomicSDNode>(Node)->getMemOperand(),
  2535. cast<AtomicSDNode>(Node)->getOrdering(),
  2536. cast<AtomicSDNode>(Node)->getSynchScope());
  2537. Results.push_back(Swap.getValue(1));
  2538. break;
  2539. }
  2540. // By default, atomic intrinsics are marked Legal and lowered. Targets
  2541. // which don't support them directly, however, may want libcalls, in which
  2542. // case they mark them Expand, and we get here.
  2543. case ISD::ATOMIC_SWAP:
  2544. case ISD::ATOMIC_LOAD_ADD:
  2545. case ISD::ATOMIC_LOAD_SUB:
  2546. case ISD::ATOMIC_LOAD_AND:
  2547. case ISD::ATOMIC_LOAD_OR:
  2548. case ISD::ATOMIC_LOAD_XOR:
  2549. case ISD::ATOMIC_LOAD_NAND:
  2550. case ISD::ATOMIC_LOAD_MIN:
  2551. case ISD::ATOMIC_LOAD_MAX:
  2552. case ISD::ATOMIC_LOAD_UMIN:
  2553. case ISD::ATOMIC_LOAD_UMAX:
  2554. case ISD::ATOMIC_CMP_SWAP: {
  2555. std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
  2556. Results.push_back(Tmp.first);
  2557. Results.push_back(Tmp.second);
  2558. break;
  2559. }
  2560. case ISD::DYNAMIC_STACKALLOC:
  2561. ExpandDYNAMIC_STACKALLOC(Node, Results);
  2562. break;
  2563. case ISD::MERGE_VALUES:
  2564. for (unsigned i = 0; i < Node->getNumValues(); i++)
  2565. Results.push_back(Node->getOperand(i));
  2566. break;
  2567. case ISD::UNDEF: {
  2568. EVT VT = Node->getValueType(0);
  2569. if (VT.isInteger())
  2570. Results.push_back(DAG.getConstant(0, VT));
  2571. else {
  2572. assert(VT.isFloatingPoint() && "Unknown value type!");
  2573. Results.push_back(DAG.getConstantFP(0, VT));
  2574. }
  2575. break;
  2576. }
  2577. case ISD::TRAP: {
  2578. // If this operation is not supported, lower it to 'abort()' call
  2579. TargetLowering::ArgListTy Args;
  2580. TargetLowering::
  2581. CallLoweringInfo CLI(Node->getOperand(0),
  2582. Type::getVoidTy(*DAG.getContext()),
  2583. false, false, false, false, 0, CallingConv::C,
  2584. /*isTailCall=*/false,
  2585. /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
  2586. DAG.getExternalSymbol("abort", TLI.getPointerTy()),
  2587. Args, DAG, dl);
  2588. std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
  2589. Results.push_back(CallResult.second);
  2590. break;
  2591. }
  2592. case ISD::FP_ROUND:
  2593. case ISD::BITCAST:
  2594. Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
  2595. Node->getValueType(0), dl);
  2596. Results.push_back(Tmp1);
  2597. break;
  2598. case ISD::FP_EXTEND:
  2599. Tmp1 = EmitStackConvert(Node->getOperand(0),
  2600. Node->getOperand(0).getValueType(),
  2601. Node->getValueType(0), dl);
  2602. Results.push_back(Tmp1);
  2603. break;
  2604. case ISD::SIGN_EXTEND_INREG: {
  2605. // NOTE: we could fall back on load/store here too for targets without
  2606. // SAR. However, it is doubtful that any exist.
  2607. EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
  2608. EVT VT = Node->getValueType(0);
  2609. EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
  2610. if (VT.isVector())
  2611. ShiftAmountTy = VT;
  2612. unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
  2613. ExtraVT.getScalarType().getSizeInBits();
  2614. SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
  2615. Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
  2616. Node->getOperand(0), ShiftCst);
  2617. Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
  2618. Results.push_back(Tmp1);
  2619. break;
  2620. }
  2621. case ISD::FP_ROUND_INREG: {
  2622. // The only way we can lower this is to turn it into a TRUNCSTORE,
  2623. // EXTLOAD pair, targeting a temporary location (a stack slot).
  2624. // NOTE: there is a choice here between constantly creating new stack
  2625. // slots and always reusing the same one. We currently always create
  2626. // new ones, as reuse may inhibit scheduling.
  2627. EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
  2628. Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
  2629. Node->getValueType(0), dl);
  2630. Results.push_back(Tmp1);
  2631. break;
  2632. }
  2633. case ISD::SINT_TO_FP:
  2634. case ISD::UINT_TO_FP:
  2635. Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
  2636. Node->getOperand(0), Node->getValueType(0), dl);
  2637. Results.push_back(Tmp1);
  2638. break;
  2639. case ISD::FP_TO_UINT: {
  2640. SDValue True, False;
  2641. EVT VT = Node->getOperand(0).getValueType();
  2642. EVT NVT = Node->getValueType(0);
  2643. APFloat apf(DAG.EVTToAPFloatSemantics(VT),
  2644. APInt::getNullValue(VT.getSizeInBits()));
  2645. APInt x = APInt::getSignBit(NVT.getSizeInBits());
  2646. (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
  2647. Tmp1 = DAG.getConstantFP(apf, VT);
  2648. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(VT),
  2649. Node->getOperand(0),
  2650. Tmp1, ISD::SETLT);
  2651. True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
  2652. False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
  2653. DAG.getNode(ISD::FSUB, dl, VT,
  2654. Node->getOperand(0), Tmp1));
  2655. False = DAG.getNode(ISD::XOR, dl, NVT, False,
  2656. DAG.getConstant(x, NVT));
  2657. Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
  2658. Results.push_back(Tmp1);
  2659. break;
  2660. }
  2661. case ISD::VAARG: {
  2662. const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
  2663. EVT VT = Node->getValueType(0);
  2664. Tmp1 = Node->getOperand(0);
  2665. Tmp2 = Node->getOperand(1);
  2666. unsigned Align = Node->getConstantOperandVal(3);
  2667. SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
  2668. MachinePointerInfo(V),
  2669. false, false, false, 0);
  2670. SDValue VAList = VAListLoad;
  2671. if (Align > TLI.getMinStackArgumentAlignment()) {
  2672. assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
  2673. VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
  2674. DAG.getConstant(Align - 1,
  2675. TLI.getPointerTy()));
  2676. VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
  2677. DAG.getConstant(-(int64_t)Align,
  2678. TLI.getPointerTy()));
  2679. }
  2680. // Increment the pointer, VAList, to the next vaarg
  2681. Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
  2682. DAG.getConstant(TLI.getDataLayout()->
  2683. getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
  2684. TLI.getPointerTy()));
  2685. // Store the incremented VAList to the legalized pointer
  2686. Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
  2687. MachinePointerInfo(V), false, false, 0);
  2688. // Load the actual argument out of the pointer VAList
  2689. Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
  2690. false, false, false, 0));
  2691. Results.push_back(Results[0].getValue(1));
  2692. break;
  2693. }
  2694. case ISD::VACOPY: {
  2695. // This defaults to loading a pointer from the input and storing it to the
  2696. // output, returning the chain.
  2697. const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
  2698. const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
  2699. Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
  2700. Node->getOperand(2), MachinePointerInfo(VS),
  2701. false, false, false, 0);
  2702. Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
  2703. MachinePointerInfo(VD), false, false, 0);
  2704. Results.push_back(Tmp1);
  2705. break;
  2706. }
  2707. case ISD::EXTRACT_VECTOR_ELT:
  2708. if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
  2709. // This must be an access of the only element. Return it.
  2710. Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
  2711. Node->getOperand(0));
  2712. else
  2713. Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
  2714. Results.push_back(Tmp1);
  2715. break;
  2716. case ISD::EXTRACT_SUBVECTOR:
  2717. Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
  2718. break;
  2719. case ISD::INSERT_SUBVECTOR:
  2720. Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
  2721. break;
  2722. case ISD::CONCAT_VECTORS: {
  2723. Results.push_back(ExpandVectorBuildThroughStack(Node));
  2724. break;
  2725. }
  2726. case ISD::SCALAR_TO_VECTOR:
  2727. Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
  2728. break;
  2729. case ISD::INSERT_VECTOR_ELT:
  2730. Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
  2731. Node->getOperand(1),
  2732. Node->getOperand(2), dl));
  2733. break;
  2734. case ISD::VECTOR_SHUFFLE: {
  2735. SmallVector<int, 32> NewMask;
  2736. ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
  2737. EVT VT = Node->getValueType(0);
  2738. EVT EltVT = VT.getVectorElementType();
  2739. SDValue Op0 = Node->getOperand(0);
  2740. SDValue Op1 = Node->getOperand(1);
  2741. if (!TLI.isTypeLegal(EltVT)) {
  2742. EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
  2743. // BUILD_VECTOR operands are allowed to be wider than the element type.
  2744. // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept it
  2745. if (NewEltVT.bitsLT(EltVT)) {
  2746. // Convert shuffle node.
  2747. // If original node was v4i64 and the new EltVT is i32,
  2748. // cast operands to v8i32 and re-build the mask.
  2749. // Calculate new VT, the size of the new VT should be equal to original.
  2750. EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltVT,
  2751. VT.getSizeInBits()/NewEltVT.getSizeInBits());
  2752. assert(NewVT.bitsEq(VT));
  2753. // cast operands to new VT
  2754. Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
  2755. Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
  2756. // Convert the shuffle mask
  2757. unsigned int factor = NewVT.getVectorNumElements()/VT.getVectorNumElements();
  2758. // EltVT gets smaller
  2759. assert(factor > 0);
  2760. for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
  2761. if (Mask[i] < 0) {
  2762. for (unsigned fi = 0; fi < factor; ++fi)
  2763. NewMask.push_back(Mask[i]);
  2764. }
  2765. else {
  2766. for (unsigned fi = 0; fi < factor; ++fi)
  2767. NewMask.push_back(Mask[i]*factor+fi);
  2768. }
  2769. }
  2770. Mask = NewMask;
  2771. VT = NewVT;
  2772. }
  2773. EltVT = NewEltVT;
  2774. }
  2775. unsigned NumElems = VT.getVectorNumElements();
  2776. SmallVector<SDValue, 16> Ops;
  2777. for (unsigned i = 0; i != NumElems; ++i) {
  2778. if (Mask[i] < 0) {
  2779. Ops.push_back(DAG.getUNDEF(EltVT));
  2780. continue;
  2781. }
  2782. unsigned Idx = Mask[i];
  2783. if (Idx < NumElems)
  2784. Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
  2785. Op0,
  2786. DAG.getIntPtrConstant(Idx)));
  2787. else
  2788. Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
  2789. Op1,
  2790. DAG.getIntPtrConstant(Idx - NumElems)));
  2791. }
  2792. Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
  2793. // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
  2794. Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
  2795. Results.push_back(Tmp1);
  2796. break;
  2797. }
  2798. case ISD::EXTRACT_ELEMENT: {
  2799. EVT OpTy = Node->getOperand(0).getValueType();
  2800. if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
  2801. // 1 -> Hi
  2802. Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
  2803. DAG.getConstant(OpTy.getSizeInBits()/2,
  2804. TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
  2805. Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
  2806. } else {
  2807. // 0 -> Lo
  2808. Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
  2809. Node->getOperand(0));
  2810. }
  2811. Results.push_back(Tmp1);
  2812. break;
  2813. }
  2814. case ISD::STACKSAVE:
  2815. // Expand to CopyFromReg if the target set
  2816. // StackPointerRegisterToSaveRestore.
  2817. if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
  2818. Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
  2819. Node->getValueType(0)));
  2820. Results.push_back(Results[0].getValue(1));
  2821. } else {
  2822. Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
  2823. Results.push_back(Node->getOperand(0));
  2824. }
  2825. break;
  2826. case ISD::STACKRESTORE:
  2827. // Expand to CopyToReg if the target set
  2828. // StackPointerRegisterToSaveRestore.
  2829. if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
  2830. Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
  2831. Node->getOperand(1)));
  2832. } else {
  2833. Results.push_back(Node->getOperand(0));
  2834. }
  2835. break;
  2836. case ISD::FCOPYSIGN:
  2837. Results.push_back(ExpandFCOPYSIGN(Node));
  2838. break;
  2839. case ISD::FNEG:
  2840. // Expand Y = FNEG(X) -> Y = SUB -0.0, X
  2841. Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
  2842. Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
  2843. Node->getOperand(0));
  2844. Results.push_back(Tmp1);
  2845. break;
  2846. case ISD::FABS: {
  2847. // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
  2848. EVT VT = Node->getValueType(0);
  2849. Tmp1 = Node->getOperand(0);
  2850. Tmp2 = DAG.getConstantFP(0.0, VT);
  2851. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(Tmp1.getValueType()),
  2852. Tmp1, Tmp2, ISD::SETUGT);
  2853. Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
  2854. Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
  2855. Results.push_back(Tmp1);
  2856. break;
  2857. }
  2858. case ISD::FSQRT:
  2859. Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
  2860. RTLIB::SQRT_F80, RTLIB::SQRT_F128,
  2861. RTLIB::SQRT_PPCF128));
  2862. break;
  2863. case ISD::FSIN:
  2864. case ISD::FCOS: {
  2865. EVT VT = Node->getValueType(0);
  2866. bool isSIN = Node->getOpcode() == ISD::FSIN;
  2867. // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
  2868. // fcos which share the same operand and both are used.
  2869. if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
  2870. canCombineSinCosLibcall(Node, TLI, TM))
  2871. && useSinCos(Node)) {
  2872. SDVTList VTs = DAG.getVTList(VT, VT);
  2873. Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
  2874. if (!isSIN)
  2875. Tmp1 = Tmp1.getValue(1);
  2876. Results.push_back(Tmp1);
  2877. } else if (isSIN) {
  2878. Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
  2879. RTLIB::SIN_F80, RTLIB::SIN_F128,
  2880. RTLIB::SIN_PPCF128));
  2881. } else {
  2882. Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
  2883. RTLIB::COS_F80, RTLIB::COS_F128,
  2884. RTLIB::COS_PPCF128));
  2885. }
  2886. break;
  2887. }
  2888. case ISD::FSINCOS:
  2889. // Expand into sincos libcall.
  2890. ExpandSinCosLibCall(Node, Results);
  2891. break;
  2892. case ISD::FLOG:
  2893. Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
  2894. RTLIB::LOG_F80, RTLIB::LOG_F128,
  2895. RTLIB::LOG_PPCF128));
  2896. break;
  2897. case ISD::FLOG2:
  2898. Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
  2899. RTLIB::LOG2_F80, RTLIB::LOG2_F128,
  2900. RTLIB::LOG2_PPCF128));
  2901. break;
  2902. case ISD::FLOG10:
  2903. Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
  2904. RTLIB::LOG10_F80, RTLIB::LOG10_F128,
  2905. RTLIB::LOG10_PPCF128));
  2906. break;
  2907. case ISD::FEXP:
  2908. Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
  2909. RTLIB::EXP_F80, RTLIB::EXP_F128,
  2910. RTLIB::EXP_PPCF128));
  2911. break;
  2912. case ISD::FEXP2:
  2913. Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
  2914. RTLIB::EXP2_F80, RTLIB::EXP2_F128,
  2915. RTLIB::EXP2_PPCF128));
  2916. break;
  2917. case ISD::FTRUNC:
  2918. Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
  2919. RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
  2920. RTLIB::TRUNC_PPCF128));
  2921. break;
  2922. case ISD::FFLOOR:
  2923. Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
  2924. RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
  2925. RTLIB::FLOOR_PPCF128));
  2926. break;
  2927. case ISD::FCEIL:
  2928. Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
  2929. RTLIB::CEIL_F80, RTLIB::CEIL_F128,
  2930. RTLIB::CEIL_PPCF128));
  2931. break;
  2932. case ISD::FRINT:
  2933. Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
  2934. RTLIB::RINT_F80, RTLIB::RINT_F128,
  2935. RTLIB::RINT_PPCF128));
  2936. break;
  2937. case ISD::FNEARBYINT:
  2938. Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
  2939. RTLIB::NEARBYINT_F64,
  2940. RTLIB::NEARBYINT_F80,
  2941. RTLIB::NEARBYINT_F128,
  2942. RTLIB::NEARBYINT_PPCF128));
  2943. break;
  2944. case ISD::FPOWI:
  2945. Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
  2946. RTLIB::POWI_F80, RTLIB::POWI_F128,
  2947. RTLIB::POWI_PPCF128));
  2948. break;
  2949. case ISD::FPOW:
  2950. Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
  2951. RTLIB::POW_F80, RTLIB::POW_F128,
  2952. RTLIB::POW_PPCF128));
  2953. break;
  2954. case ISD::FDIV:
  2955. Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
  2956. RTLIB::DIV_F80, RTLIB::DIV_F128,
  2957. RTLIB::DIV_PPCF128));
  2958. break;
  2959. case ISD::FREM:
  2960. Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
  2961. RTLIB::REM_F80, RTLIB::REM_F128,
  2962. RTLIB::REM_PPCF128));
  2963. break;
  2964. case ISD::FMA:
  2965. Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
  2966. RTLIB::FMA_F80, RTLIB::FMA_F128,
  2967. RTLIB::FMA_PPCF128));
  2968. break;
  2969. case ISD::FP16_TO_FP32:
  2970. Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
  2971. break;
  2972. case ISD::FP32_TO_FP16:
  2973. Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
  2974. break;
  2975. case ISD::ConstantFP: {
  2976. ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
  2977. // Check to see if this FP immediate is already legal.
  2978. // If this is a legal constant, turn it into a TargetConstantFP node.
  2979. if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
  2980. Results.push_back(ExpandConstantFP(CFP, true));
  2981. break;
  2982. }
  2983. case ISD::EHSELECTION: {
  2984. unsigned Reg = TLI.getExceptionSelectorRegister();
  2985. assert(Reg && "Can't expand to unknown register!");
  2986. Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
  2987. Node->getValueType(0)));
  2988. Results.push_back(Results[0].getValue(1));
  2989. break;
  2990. }
  2991. case ISD::EXCEPTIONADDR: {
  2992. unsigned Reg = TLI.getExceptionPointerRegister();
  2993. assert(Reg && "Can't expand to unknown register!");
  2994. Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
  2995. Node->getValueType(0)));
  2996. Results.push_back(Results[0].getValue(1));
  2997. break;
  2998. }
  2999. case ISD::FSUB: {
  3000. EVT VT = Node->getValueType(0);
  3001. assert(TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
  3002. TLI.isOperationLegalOrCustom(ISD::FNEG, VT) &&
  3003. "Don't know how to expand this FP subtraction!");
  3004. Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
  3005. Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1);
  3006. Results.push_back(Tmp1);
  3007. break;
  3008. }
  3009. case ISD::SUB: {
  3010. EVT VT = Node->getValueType(0);
  3011. assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
  3012. TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
  3013. "Don't know how to expand this subtraction!");
  3014. Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
  3015. DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
  3016. Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, VT));
  3017. Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
  3018. break;
  3019. }
  3020. case ISD::UREM:
  3021. case ISD::SREM: {
  3022. EVT VT = Node->getValueType(0);
  3023. bool isSigned = Node->getOpcode() == ISD::SREM;
  3024. unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
  3025. unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
  3026. Tmp2 = Node->getOperand(0);
  3027. Tmp3 = Node->getOperand(1);
  3028. if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
  3029. (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
  3030. // If div is legal, it's better to do the normal expansion
  3031. !TLI.isOperationLegalOrCustom(DivOpc, Node->getValueType(0)) &&
  3032. useDivRem(Node, isSigned, false))) {
  3033. SDVTList VTs = DAG.getVTList(VT, VT);
  3034. Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
  3035. } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
  3036. // X % Y -> X-X/Y*Y
  3037. Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
  3038. Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
  3039. Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
  3040. } else if (isSigned)
  3041. Tmp1 = ExpandIntLibCall(Node, true,
  3042. RTLIB::SREM_I8,
  3043. RTLIB::SREM_I16, RTLIB::SREM_I32,
  3044. RTLIB::SREM_I64, RTLIB::SREM_I128);
  3045. else
  3046. Tmp1 = ExpandIntLibCall(Node, false,
  3047. RTLIB::UREM_I8,
  3048. RTLIB::UREM_I16, RTLIB::UREM_I32,
  3049. RTLIB::UREM_I64, RTLIB::UREM_I128);
  3050. Results.push_back(Tmp1);
  3051. break;
  3052. }
  3053. case ISD::UDIV:
  3054. case ISD::SDIV: {
  3055. bool isSigned = Node->getOpcode() == ISD::SDIV;
  3056. unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
  3057. EVT VT = Node->getValueType(0);
  3058. SDVTList VTs = DAG.getVTList(VT, VT);
  3059. if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
  3060. (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
  3061. useDivRem(Node, isSigned, true)))
  3062. Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
  3063. Node->getOperand(1));
  3064. else if (isSigned)
  3065. Tmp1 = ExpandIntLibCall(Node, true,
  3066. RTLIB::SDIV_I8,
  3067. RTLIB::SDIV_I16, RTLIB::SDIV_I32,
  3068. RTLIB::SDIV_I64, RTLIB::SDIV_I128);
  3069. else
  3070. Tmp1 = ExpandIntLibCall(Node, false,
  3071. RTLIB::UDIV_I8,
  3072. RTLIB::UDIV_I16, RTLIB::UDIV_I32,
  3073. RTLIB::UDIV_I64, RTLIB::UDIV_I128);
  3074. Results.push_back(Tmp1);
  3075. break;
  3076. }
  3077. case ISD::MULHU:
  3078. case ISD::MULHS: {
  3079. unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
  3080. ISD::SMUL_LOHI;
  3081. EVT VT = Node->getValueType(0);
  3082. SDVTList VTs = DAG.getVTList(VT, VT);
  3083. assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
  3084. "If this wasn't legal, it shouldn't have been created!");
  3085. Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
  3086. Node->getOperand(1));
  3087. Results.push_back(Tmp1.getValue(1));
  3088. break;
  3089. }
  3090. case ISD::SDIVREM:
  3091. case ISD::UDIVREM:
  3092. // Expand into divrem libcall
  3093. ExpandDivRemLibCall(Node, Results);
  3094. break;
  3095. case ISD::MUL: {
  3096. EVT VT = Node->getValueType(0);
  3097. SDVTList VTs = DAG.getVTList(VT, VT);
  3098. // See if multiply or divide can be lowered using two-result operations.
  3099. // We just need the low half of the multiply; try both the signed
  3100. // and unsigned forms. If the target supports both SMUL_LOHI and
  3101. // UMUL_LOHI, form a preference by checking which forms of plain
  3102. // MULH it supports.
  3103. bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
  3104. bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
  3105. bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
  3106. bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
  3107. unsigned OpToUse = 0;
  3108. if (HasSMUL_LOHI && !HasMULHS) {
  3109. OpToUse = ISD::SMUL_LOHI;
  3110. } else if (HasUMUL_LOHI && !HasMULHU) {
  3111. OpToUse = ISD::UMUL_LOHI;
  3112. } else if (HasSMUL_LOHI) {
  3113. OpToUse = ISD::SMUL_LOHI;
  3114. } else if (HasUMUL_LOHI) {
  3115. OpToUse = ISD::UMUL_LOHI;
  3116. }
  3117. if (OpToUse) {
  3118. Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
  3119. Node->getOperand(1)));
  3120. break;
  3121. }
  3122. Tmp1 = ExpandIntLibCall(Node, false,
  3123. RTLIB::MUL_I8,
  3124. RTLIB::MUL_I16, RTLIB::MUL_I32,
  3125. RTLIB::MUL_I64, RTLIB::MUL_I128);
  3126. Results.push_back(Tmp1);
  3127. break;
  3128. }
  3129. case ISD::SADDO:
  3130. case ISD::SSUBO: {
  3131. SDValue LHS = Node->getOperand(0);
  3132. SDValue RHS = Node->getOperand(1);
  3133. SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
  3134. ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
  3135. LHS, RHS);
  3136. Results.push_back(Sum);
  3137. EVT OType = Node->getValueType(1);
  3138. SDValue Zero = DAG.getConstant(0, LHS.getValueType());
  3139. // LHSSign -> LHS >= 0
  3140. // RHSSign -> RHS >= 0
  3141. // SumSign -> Sum >= 0
  3142. //
  3143. // Add:
  3144. // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
  3145. // Sub:
  3146. // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
  3147. //
  3148. SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
  3149. SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
  3150. SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
  3151. Node->getOpcode() == ISD::SADDO ?
  3152. ISD::SETEQ : ISD::SETNE);
  3153. SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
  3154. SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
  3155. SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
  3156. Results.push_back(Cmp);
  3157. break;
  3158. }
  3159. case ISD::UADDO:
  3160. case ISD::USUBO: {
  3161. SDValue LHS = Node->getOperand(0);
  3162. SDValue RHS = Node->getOperand(1);
  3163. SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
  3164. ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
  3165. LHS, RHS);
  3166. Results.push_back(Sum);
  3167. Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
  3168. Node->getOpcode () == ISD::UADDO ?
  3169. ISD::SETULT : ISD::SETUGT));
  3170. break;
  3171. }
  3172. case ISD::UMULO:
  3173. case ISD::SMULO: {
  3174. EVT VT = Node->getValueType(0);
  3175. EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
  3176. SDValue LHS = Node->getOperand(0);
  3177. SDValue RHS = Node->getOperand(1);
  3178. SDValue BottomHalf;
  3179. SDValue TopHalf;
  3180. static const unsigned Ops[2][3] =
  3181. { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
  3182. { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
  3183. bool isSigned = Node->getOpcode() == ISD::SMULO;
  3184. if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
  3185. BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
  3186. TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
  3187. } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
  3188. BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
  3189. RHS);
  3190. TopHalf = BottomHalf.getValue(1);
  3191. } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
  3192. VT.getSizeInBits() * 2))) {
  3193. LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
  3194. RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
  3195. Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
  3196. BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
  3197. DAG.getIntPtrConstant(0));
  3198. TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
  3199. DAG.getIntPtrConstant(1));
  3200. } else {
  3201. // We can fall back to a libcall with an illegal type for the MUL if we
  3202. // have a libcall big enough.
  3203. // Also, we can fall back to a division in some cases, but that's a big
  3204. // performance hit in the general case.
  3205. RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
  3206. if (WideVT == MVT::i16)
  3207. LC = RTLIB::MUL_I16;
  3208. else if (WideVT == MVT::i32)
  3209. LC = RTLIB::MUL_I32;
  3210. else if (WideVT == MVT::i64)
  3211. LC = RTLIB::MUL_I64;
  3212. else if (WideVT == MVT::i128)
  3213. LC = RTLIB::MUL_I128;
  3214. assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
  3215. // The high part is obtained by SRA'ing all but one of the bits of low
  3216. // part.
  3217. unsigned LoSize = VT.getSizeInBits();
  3218. SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS,
  3219. DAG.getConstant(LoSize-1, TLI.getPointerTy()));
  3220. SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS,
  3221. DAG.getConstant(LoSize-1, TLI.getPointerTy()));
  3222. // Here we're passing the 2 arguments explicitly as 4 arguments that are
  3223. // pre-lowered to the correct types. This all depends upon WideVT not
  3224. // being a legal type for the architecture and thus has to be split to
  3225. // two arguments.
  3226. SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
  3227. SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl);
  3228. BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
  3229. DAG.getIntPtrConstant(0));
  3230. TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret,
  3231. DAG.getIntPtrConstant(1));
  3232. // Ret is a node with an illegal type. Because such things are not
  3233. // generally permitted during this phase of legalization, delete the
  3234. // node. The above EXTRACT_ELEMENT nodes should have been folded.
  3235. DAG.DeleteNode(Ret.getNode());
  3236. }
  3237. if (isSigned) {
  3238. Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
  3239. TLI.getShiftAmountTy(BottomHalf.getValueType()));
  3240. Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
  3241. TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf, Tmp1,
  3242. ISD::SETNE);
  3243. } else {
  3244. TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
  3245. DAG.getConstant(0, VT), ISD::SETNE);
  3246. }
  3247. Results.push_back(BottomHalf);
  3248. Results.push_back(TopHalf);
  3249. break;
  3250. }
  3251. case ISD::BUILD_PAIR: {
  3252. EVT PairTy = Node->getValueType(0);
  3253. Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
  3254. Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
  3255. Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
  3256. DAG.getConstant(PairTy.getSizeInBits()/2,
  3257. TLI.getShiftAmountTy(PairTy)));
  3258. Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
  3259. break;
  3260. }
  3261. case ISD::SELECT:
  3262. Tmp1 = Node->getOperand(0);
  3263. Tmp2 = Node->getOperand(1);
  3264. Tmp3 = Node->getOperand(2);
  3265. if (Tmp1.getOpcode() == ISD::SETCC) {
  3266. Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
  3267. Tmp2, Tmp3,
  3268. cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
  3269. } else {
  3270. Tmp1 = DAG.getSelectCC(dl, Tmp1,
  3271. DAG.getConstant(0, Tmp1.getValueType()),
  3272. Tmp2, Tmp3, ISD::SETNE);
  3273. }
  3274. Results.push_back(Tmp1);
  3275. break;
  3276. case ISD::BR_JT: {
  3277. SDValue Chain = Node->getOperand(0);
  3278. SDValue Table = Node->getOperand(1);
  3279. SDValue Index = Node->getOperand(2);
  3280. EVT PTy = TLI.getPointerTy();
  3281. const DataLayout &TD = *TLI.getDataLayout();
  3282. unsigned EntrySize =
  3283. DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
  3284. Index = DAG.getNode(ISD::MUL, dl, PTy,
  3285. Index, DAG.getConstant(EntrySize, PTy));
  3286. SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
  3287. EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
  3288. SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
  3289. MachinePointerInfo::getJumpTable(), MemVT,
  3290. false, false, 0);
  3291. Addr = LD;
  3292. if (TM.getRelocationModel() == Reloc::PIC_) {
  3293. // For PIC, the sequence is:
  3294. // BRIND(load(Jumptable + index) + RelocBase)
  3295. // RelocBase can be JumpTable, GOT or some sort of global base.
  3296. Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
  3297. TLI.getPICJumpTableRelocBase(Table, DAG));
  3298. }
  3299. Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
  3300. Results.push_back(Tmp1);
  3301. break;
  3302. }
  3303. case ISD::BRCOND:
  3304. // Expand brcond's setcc into its constituent parts and create a BR_CC
  3305. // Node.
  3306. Tmp1 = Node->getOperand(0);
  3307. Tmp2 = Node->getOperand(1);
  3308. if (Tmp2.getOpcode() == ISD::SETCC) {
  3309. Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
  3310. Tmp1, Tmp2.getOperand(2),
  3311. Tmp2.getOperand(0), Tmp2.getOperand(1),
  3312. Node->getOperand(2));
  3313. } else {
  3314. // We test only the i1 bit. Skip the AND if UNDEF.
  3315. Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 :
  3316. DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
  3317. DAG.getConstant(1, Tmp2.getValueType()));
  3318. Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
  3319. DAG.getCondCode(ISD::SETNE), Tmp3,
  3320. DAG.getConstant(0, Tmp3.getValueType()),
  3321. Node->getOperand(2));
  3322. }
  3323. Results.push_back(Tmp1);
  3324. break;
  3325. case ISD::SETCC: {
  3326. Tmp1 = Node->getOperand(0);
  3327. Tmp2 = Node->getOperand(1);
  3328. Tmp3 = Node->getOperand(2);
  3329. LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
  3330. // If we expanded the SETCC into an AND/OR, return the new node
  3331. if (Tmp2.getNode() == 0) {
  3332. Results.push_back(Tmp1);
  3333. break;
  3334. }
  3335. // Otherwise, SETCC for the given comparison type must be completely
  3336. // illegal; expand it into a SELECT_CC.
  3337. EVT VT = Node->getValueType(0);
  3338. int TrueValue;
  3339. switch (TLI.getBooleanContents(VT.isVector())) {
  3340. case TargetLowering::ZeroOrOneBooleanContent:
  3341. case TargetLowering::UndefinedBooleanContent:
  3342. TrueValue = 1;
  3343. break;
  3344. case TargetLowering::ZeroOrNegativeOneBooleanContent:
  3345. TrueValue = -1;
  3346. break;
  3347. }
  3348. Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
  3349. DAG.getConstant(TrueValue, VT), DAG.getConstant(0, VT),
  3350. Tmp3);
  3351. Results.push_back(Tmp1);
  3352. break;
  3353. }
  3354. case ISD::SELECT_CC: {
  3355. Tmp1 = Node->getOperand(0); // LHS
  3356. Tmp2 = Node->getOperand(1); // RHS
  3357. Tmp3 = Node->getOperand(2); // True
  3358. Tmp4 = Node->getOperand(3); // False
  3359. SDValue CC = Node->getOperand(4);
  3360. LegalizeSetCCCondCode(getSetCCResultType(Tmp1.getValueType()),
  3361. Tmp1, Tmp2, CC, dl);
  3362. assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
  3363. Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
  3364. CC = DAG.getCondCode(ISD::SETNE);
  3365. Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
  3366. Tmp3, Tmp4, CC);
  3367. Results.push_back(Tmp1);
  3368. break;
  3369. }
  3370. case ISD::BR_CC: {
  3371. Tmp1 = Node->getOperand(0); // Chain
  3372. Tmp2 = Node->getOperand(2); // LHS
  3373. Tmp3 = Node->getOperand(3); // RHS
  3374. Tmp4 = Node->getOperand(1); // CC
  3375. LegalizeSetCCCondCode(getSetCCResultType(Tmp2.getValueType()),
  3376. Tmp2, Tmp3, Tmp4, dl);
  3377. assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
  3378. Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
  3379. Tmp4 = DAG.getCondCode(ISD::SETNE);
  3380. Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
  3381. Tmp3, Node->getOperand(4));
  3382. Results.push_back(Tmp1);
  3383. break;
  3384. }
  3385. case ISD::BUILD_VECTOR:
  3386. Results.push_back(ExpandBUILD_VECTOR(Node));
  3387. break;
  3388. case ISD::SRA:
  3389. case ISD::SRL:
  3390. case ISD::SHL: {
  3391. // Scalarize vector SRA/SRL/SHL.
  3392. EVT VT = Node->getValueType(0);
  3393. assert(VT.isVector() && "Unable to legalize non-vector shift");
  3394. assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
  3395. unsigned NumElem = VT.getVectorNumElements();
  3396. SmallVector<SDValue, 8> Scalars;
  3397. for (unsigned Idx = 0; Idx < NumElem; Idx++) {
  3398. SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  3399. VT.getScalarType(),
  3400. Node->getOperand(0), DAG.getIntPtrConstant(Idx));
  3401. SDValue Sh = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  3402. VT.getScalarType(),
  3403. Node->getOperand(1), DAG.getIntPtrConstant(Idx));
  3404. Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
  3405. VT.getScalarType(), Ex, Sh));
  3406. }
  3407. SDValue Result =
  3408. DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
  3409. &Scalars[0], Scalars.size());
  3410. ReplaceNode(SDValue(Node, 0), Result);
  3411. break;
  3412. }
  3413. case ISD::GLOBAL_OFFSET_TABLE:
  3414. case ISD::GlobalAddress:
  3415. case ISD::GlobalTLSAddress:
  3416. case ISD::ExternalSymbol:
  3417. case ISD::ConstantPool:
  3418. case ISD::JumpTable:
  3419. case ISD::INTRINSIC_W_CHAIN:
  3420. case ISD::INTRINSIC_WO_CHAIN:
  3421. case ISD::INTRINSIC_VOID:
  3422. // FIXME: Custom lowering for these operations shouldn't return null!
  3423. break;
  3424. }
  3425. // Replace the original node with the legalized result.
  3426. if (!Results.empty())
  3427. ReplaceNode(Node, Results.data());
  3428. }
  3429. void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
  3430. SmallVector<SDValue, 8> Results;
  3431. MVT OVT = Node->getSimpleValueType(0);
  3432. if (Node->getOpcode() == ISD::UINT_TO_FP ||
  3433. Node->getOpcode() == ISD::SINT_TO_FP ||
  3434. Node->getOpcode() == ISD::SETCC) {
  3435. OVT = Node->getOperand(0).getSimpleValueType();
  3436. }
  3437. MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
  3438. SDLoc dl(Node);
  3439. SDValue Tmp1, Tmp2, Tmp3;
  3440. switch (Node->getOpcode()) {
  3441. case ISD::CTTZ:
  3442. case ISD::CTTZ_ZERO_UNDEF:
  3443. case ISD::CTLZ:
  3444. case ISD::CTLZ_ZERO_UNDEF:
  3445. case ISD::CTPOP:
  3446. // Zero extend the argument.
  3447. Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
  3448. // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
  3449. // already the correct result.
  3450. Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
  3451. if (Node->getOpcode() == ISD::CTTZ) {
  3452. // FIXME: This should set a bit in the zero extended value instead.
  3453. Tmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT),
  3454. Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
  3455. ISD::SETEQ);
  3456. Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
  3457. DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
  3458. } else if (Node->getOpcode() == ISD::CTLZ ||
  3459. Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
  3460. // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
  3461. Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
  3462. DAG.getConstant(NVT.getSizeInBits() -
  3463. OVT.getSizeInBits(), NVT));
  3464. }
  3465. Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
  3466. break;
  3467. case ISD::BSWAP: {
  3468. unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
  3469. Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
  3470. Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
  3471. Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
  3472. DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
  3473. Results.push_back(Tmp1);
  3474. break;
  3475. }
  3476. case ISD::FP_TO_UINT:
  3477. case ISD::FP_TO_SINT:
  3478. Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
  3479. Node->getOpcode() == ISD::FP_TO_SINT, dl);
  3480. Results.push_back(Tmp1);
  3481. break;
  3482. case ISD::UINT_TO_FP:
  3483. case ISD::SINT_TO_FP:
  3484. Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
  3485. Node->getOpcode() == ISD::SINT_TO_FP, dl);
  3486. Results.push_back(Tmp1);
  3487. break;
  3488. case ISD::VAARG: {
  3489. SDValue Chain = Node->getOperand(0); // Get the chain.
  3490. SDValue Ptr = Node->getOperand(1); // Get the pointer.
  3491. unsigned TruncOp;
  3492. if (OVT.isVector()) {
  3493. TruncOp = ISD::BITCAST;
  3494. } else {
  3495. assert(OVT.isInteger()
  3496. && "VAARG promotion is supported only for vectors or integer types");
  3497. TruncOp = ISD::TRUNCATE;
  3498. }
  3499. // Perform the larger operation, then convert back
  3500. Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
  3501. Node->getConstantOperandVal(3));
  3502. Chain = Tmp1.getValue(1);
  3503. Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
  3504. // Modified the chain result - switch anything that used the old chain to
  3505. // use the new one.
  3506. DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
  3507. DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
  3508. ReplacedNode(Node);
  3509. break;
  3510. }
  3511. case ISD::AND:
  3512. case ISD::OR:
  3513. case ISD::XOR: {
  3514. unsigned ExtOp, TruncOp;
  3515. if (OVT.isVector()) {
  3516. ExtOp = ISD::BITCAST;
  3517. TruncOp = ISD::BITCAST;
  3518. } else {
  3519. assert(OVT.isInteger() && "Cannot promote logic operation");
  3520. ExtOp = ISD::ANY_EXTEND;
  3521. TruncOp = ISD::TRUNCATE;
  3522. }
  3523. // Promote each of the values to the new type.
  3524. Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
  3525. Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
  3526. // Perform the larger operation, then convert back
  3527. Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
  3528. Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
  3529. break;
  3530. }
  3531. case ISD::SELECT: {
  3532. unsigned ExtOp, TruncOp;
  3533. if (Node->getValueType(0).isVector()) {
  3534. ExtOp = ISD::BITCAST;
  3535. TruncOp = ISD::BITCAST;
  3536. } else if (Node->getValueType(0).isInteger()) {
  3537. ExtOp = ISD::ANY_EXTEND;
  3538. TruncOp = ISD::TRUNCATE;
  3539. } else {
  3540. ExtOp = ISD::FP_EXTEND;
  3541. TruncOp = ISD::FP_ROUND;
  3542. }
  3543. Tmp1 = Node->getOperand(0);
  3544. // Promote each of the values to the new type.
  3545. Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
  3546. Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
  3547. // Perform the larger operation, then round down.
  3548. Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
  3549. if (TruncOp != ISD::FP_ROUND)
  3550. Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
  3551. else
  3552. Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
  3553. DAG.getIntPtrConstant(0));
  3554. Results.push_back(Tmp1);
  3555. break;
  3556. }
  3557. case ISD::VECTOR_SHUFFLE: {
  3558. ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
  3559. // Cast the two input vectors.
  3560. Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
  3561. Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
  3562. // Convert the shuffle mask to the right # elements.
  3563. Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
  3564. Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
  3565. Results.push_back(Tmp1);
  3566. break;
  3567. }
  3568. case ISD::SETCC: {
  3569. unsigned ExtOp = ISD::FP_EXTEND;
  3570. if (NVT.isInteger()) {
  3571. ISD::CondCode CCCode =
  3572. cast<CondCodeSDNode>(Node->getOperand(2))->get();
  3573. ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
  3574. }
  3575. Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
  3576. Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
  3577. Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
  3578. Tmp1, Tmp2, Node->getOperand(2)));
  3579. break;
  3580. }
  3581. case ISD::FDIV:
  3582. case ISD::FREM:
  3583. case ISD::FPOW: {
  3584. Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
  3585. Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
  3586. Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
  3587. Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
  3588. Tmp3, DAG.getIntPtrConstant(0)));
  3589. break;
  3590. }
  3591. case ISD::FLOG2:
  3592. case ISD::FEXP2:
  3593. case ISD::FLOG:
  3594. case ISD::FEXP: {
  3595. Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
  3596. Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
  3597. Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT,
  3598. Tmp2, DAG.getIntPtrConstant(0)));
  3599. break;
  3600. }
  3601. }
  3602. // Replace the original node with the legalized result.
  3603. if (!Results.empty())
  3604. ReplaceNode(Node, Results.data());
  3605. }
  3606. // SelectionDAG::Legalize - This is the entry point for the file.
  3607. //
  3608. void SelectionDAG::Legalize() {
  3609. /// run - This is the main entry point to this class.
  3610. ///
  3611. SelectionDAGLegalize(*this).LegalizeDAG();
  3612. }