TargetLowering.cpp 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150
  1. //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
  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 implements the TargetLowering class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetLowering.h"
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/CodeGen/Analysis.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  20. #include "llvm/CodeGen/SelectionDAG.h"
  21. #include "llvm/IR/DataLayout.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/GlobalVariable.h"
  24. #include "llvm/IR/LLVMContext.h"
  25. #include "llvm/MC/MCAsmInfo.h"
  26. #include "llvm/MC/MCExpr.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/MathExtras.h"
  30. #include "llvm/Target/TargetLoweringObjectFile.h"
  31. #include "llvm/Target/TargetMachine.h"
  32. #include "llvm/Target/TargetRegisterInfo.h"
  33. #include "llvm/Target/TargetSubtargetInfo.h"
  34. #include <cctype>
  35. using namespace llvm;
  36. /// NOTE: The TargetMachine owns TLOF.
  37. TargetLowering::TargetLowering(const TargetMachine &tm)
  38. : TargetLoweringBase(tm) {}
  39. const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
  40. return nullptr;
  41. }
  42. /// Check whether a given call node is in tail position within its function. If
  43. /// so, it sets Chain to the input chain of the tail call.
  44. bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
  45. SDValue &Chain) const {
  46. const Function *F = DAG.getMachineFunction().getFunction();
  47. // Conservatively require the attributes of the call to match those of
  48. // the return. Ignore noalias because it doesn't affect the call sequence.
  49. AttributeSet CallerAttrs = F->getAttributes();
  50. if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex)
  51. .removeAttribute(Attribute::NoAlias).hasAttributes())
  52. return false;
  53. // It's not safe to eliminate the sign / zero extension of the return value.
  54. if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
  55. CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
  56. return false;
  57. // Check if the only use is a function return node.
  58. return isUsedByReturnOnly(Node, Chain);
  59. }
  60. /// \brief Set CallLoweringInfo attribute flags based on a call instruction
  61. /// and called function attributes.
  62. void TargetLowering::ArgListEntry::setAttributes(ImmutableCallSite *CS,
  63. unsigned AttrIdx) {
  64. isSExt = CS->paramHasAttr(AttrIdx, Attribute::SExt);
  65. isZExt = CS->paramHasAttr(AttrIdx, Attribute::ZExt);
  66. isInReg = CS->paramHasAttr(AttrIdx, Attribute::InReg);
  67. isSRet = CS->paramHasAttr(AttrIdx, Attribute::StructRet);
  68. isNest = CS->paramHasAttr(AttrIdx, Attribute::Nest);
  69. isByVal = CS->paramHasAttr(AttrIdx, Attribute::ByVal);
  70. isInAlloca = CS->paramHasAttr(AttrIdx, Attribute::InAlloca);
  71. isReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned);
  72. isSwiftSelf = CS->paramHasAttr(AttrIdx, Attribute::SwiftSelf);
  73. Alignment = CS->getParamAlignment(AttrIdx);
  74. }
  75. /// Generate a libcall taking the given operands as arguments and returning a
  76. /// result of type RetVT.
  77. std::pair<SDValue, SDValue>
  78. TargetLowering::makeLibCall(SelectionDAG &DAG,
  79. RTLIB::Libcall LC, EVT RetVT,
  80. ArrayRef<SDValue> Ops,
  81. bool isSigned, SDLoc dl,
  82. bool doesNotReturn,
  83. bool isReturnValueUsed) const {
  84. TargetLowering::ArgListTy Args;
  85. Args.reserve(Ops.size());
  86. TargetLowering::ArgListEntry Entry;
  87. for (SDValue Op : Ops) {
  88. Entry.Node = Op;
  89. Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
  90. Entry.isSExt = shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned);
  91. Entry.isZExt = !shouldSignExtendTypeInLibCall(Op.getValueType(), isSigned);
  92. Args.push_back(Entry);
  93. }
  94. if (LC == RTLIB::UNKNOWN_LIBCALL)
  95. report_fatal_error("Unsupported library call operation!");
  96. SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
  97. getPointerTy(DAG.getDataLayout()));
  98. Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
  99. TargetLowering::CallLoweringInfo CLI(DAG);
  100. bool signExtend = shouldSignExtendTypeInLibCall(RetVT, isSigned);
  101. CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
  102. .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
  103. .setNoReturn(doesNotReturn).setDiscardResult(!isReturnValueUsed)
  104. .setSExtResult(signExtend).setZExtResult(!signExtend);
  105. return LowerCallTo(CLI);
  106. }
  107. /// Soften the operands of a comparison. This code is shared among BR_CC,
  108. /// SELECT_CC, and SETCC handlers.
  109. void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
  110. SDValue &NewLHS, SDValue &NewRHS,
  111. ISD::CondCode &CCCode,
  112. SDLoc dl) const {
  113. assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128)
  114. && "Unsupported setcc type!");
  115. // Expand into one or more soft-fp libcall(s).
  116. RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
  117. bool ShouldInvertCC = false;
  118. switch (CCCode) {
  119. case ISD::SETEQ:
  120. case ISD::SETOEQ:
  121. LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
  122. (VT == MVT::f64) ? RTLIB::OEQ_F64 :
  123. (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
  124. break;
  125. case ISD::SETNE:
  126. case ISD::SETUNE:
  127. LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
  128. (VT == MVT::f64) ? RTLIB::UNE_F64 :
  129. (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128;
  130. break;
  131. case ISD::SETGE:
  132. case ISD::SETOGE:
  133. LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
  134. (VT == MVT::f64) ? RTLIB::OGE_F64 :
  135. (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
  136. break;
  137. case ISD::SETLT:
  138. case ISD::SETOLT:
  139. LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
  140. (VT == MVT::f64) ? RTLIB::OLT_F64 :
  141. (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
  142. break;
  143. case ISD::SETLE:
  144. case ISD::SETOLE:
  145. LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
  146. (VT == MVT::f64) ? RTLIB::OLE_F64 :
  147. (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
  148. break;
  149. case ISD::SETGT:
  150. case ISD::SETOGT:
  151. LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
  152. (VT == MVT::f64) ? RTLIB::OGT_F64 :
  153. (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
  154. break;
  155. case ISD::SETUO:
  156. LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
  157. (VT == MVT::f64) ? RTLIB::UO_F64 :
  158. (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
  159. break;
  160. case ISD::SETO:
  161. LC1 = (VT == MVT::f32) ? RTLIB::O_F32 :
  162. (VT == MVT::f64) ? RTLIB::O_F64 :
  163. (VT == MVT::f128) ? RTLIB::O_F128 : RTLIB::O_PPCF128;
  164. break;
  165. case ISD::SETONE:
  166. // SETONE = SETOLT | SETOGT
  167. LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
  168. (VT == MVT::f64) ? RTLIB::OLT_F64 :
  169. (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
  170. LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
  171. (VT == MVT::f64) ? RTLIB::OGT_F64 :
  172. (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
  173. break;
  174. case ISD::SETUEQ:
  175. LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
  176. (VT == MVT::f64) ? RTLIB::UO_F64 :
  177. (VT == MVT::f128) ? RTLIB::UO_F64 : RTLIB::UO_PPCF128;
  178. LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
  179. (VT == MVT::f64) ? RTLIB::OEQ_F64 :
  180. (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
  181. break;
  182. default:
  183. // Invert CC for unordered comparisons
  184. ShouldInvertCC = true;
  185. switch (CCCode) {
  186. case ISD::SETULT:
  187. LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
  188. (VT == MVT::f64) ? RTLIB::OGE_F64 :
  189. (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
  190. break;
  191. case ISD::SETULE:
  192. LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
  193. (VT == MVT::f64) ? RTLIB::OGT_F64 :
  194. (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
  195. break;
  196. case ISD::SETUGT:
  197. LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
  198. (VT == MVT::f64) ? RTLIB::OLE_F64 :
  199. (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
  200. break;
  201. case ISD::SETUGE:
  202. LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
  203. (VT == MVT::f64) ? RTLIB::OLT_F64 :
  204. (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
  205. break;
  206. default: llvm_unreachable("Do not know how to soften this setcc!");
  207. }
  208. }
  209. // Use the target specific return value for comparions lib calls.
  210. EVT RetVT = getCmpLibcallReturnType();
  211. SDValue Ops[2] = {NewLHS, NewRHS};
  212. NewLHS = makeLibCall(DAG, LC1, RetVT, Ops, false /*sign irrelevant*/,
  213. dl).first;
  214. NewRHS = DAG.getConstant(0, dl, RetVT);
  215. CCCode = getCmpLibcallCC(LC1);
  216. if (ShouldInvertCC)
  217. CCCode = getSetCCInverse(CCCode, /*isInteger=*/true);
  218. if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
  219. SDValue Tmp = DAG.getNode(
  220. ISD::SETCC, dl,
  221. getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT),
  222. NewLHS, NewRHS, DAG.getCondCode(CCCode));
  223. NewLHS = makeLibCall(DAG, LC2, RetVT, Ops, false/*sign irrelevant*/,
  224. dl).first;
  225. NewLHS = DAG.getNode(
  226. ISD::SETCC, dl,
  227. getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT),
  228. NewLHS, NewRHS, DAG.getCondCode(getCmpLibcallCC(LC2)));
  229. NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
  230. NewRHS = SDValue();
  231. }
  232. }
  233. /// Return the entry encoding for a jump table in the current function. The
  234. /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum.
  235. unsigned TargetLowering::getJumpTableEncoding() const {
  236. // In non-pic modes, just use the address of a block.
  237. if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
  238. return MachineJumpTableInfo::EK_BlockAddress;
  239. // In PIC mode, if the target supports a GPRel32 directive, use it.
  240. if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr)
  241. return MachineJumpTableInfo::EK_GPRel32BlockAddress;
  242. // Otherwise, use a label difference.
  243. return MachineJumpTableInfo::EK_LabelDifference32;
  244. }
  245. SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
  246. SelectionDAG &DAG) const {
  247. // If our PIC model is GP relative, use the global offset table as the base.
  248. unsigned JTEncoding = getJumpTableEncoding();
  249. if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
  250. (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
  251. return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(DAG.getDataLayout()));
  252. return Table;
  253. }
  254. /// This returns the relocation base for the given PIC jumptable, the same as
  255. /// getPICJumpTableRelocBase, but as an MCExpr.
  256. const MCExpr *
  257. TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
  258. unsigned JTI,MCContext &Ctx) const{
  259. // The normal PIC reloc base is the label at the start of the jump table.
  260. return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx);
  261. }
  262. bool
  263. TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
  264. // Assume that everything is safe in static mode.
  265. if (getTargetMachine().getRelocationModel() == Reloc::Static)
  266. return true;
  267. // In dynamic-no-pic mode, assume that known defined values are safe.
  268. if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
  269. GA && GA->getGlobal()->isStrongDefinitionForLinker())
  270. return true;
  271. // Otherwise assume nothing is safe.
  272. return false;
  273. }
  274. //===----------------------------------------------------------------------===//
  275. // Optimization Methods
  276. //===----------------------------------------------------------------------===//
  277. /// Check to see if the specified operand of the specified instruction is a
  278. /// constant integer. If so, check to see if there are any bits set in the
  279. /// constant that are not demanded. If so, shrink the constant and return true.
  280. bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
  281. const APInt &Demanded) {
  282. SDLoc dl(Op);
  283. // FIXME: ISD::SELECT, ISD::SELECT_CC
  284. switch (Op.getOpcode()) {
  285. default: break;
  286. case ISD::XOR:
  287. case ISD::AND:
  288. case ISD::OR: {
  289. ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
  290. if (!C) return false;
  291. if (Op.getOpcode() == ISD::XOR &&
  292. (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
  293. return false;
  294. // if we can expand it to have all bits set, do it
  295. if (C->getAPIntValue().intersects(~Demanded)) {
  296. EVT VT = Op.getValueType();
  297. SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
  298. DAG.getConstant(Demanded &
  299. C->getAPIntValue(),
  300. dl, VT));
  301. return CombineTo(Op, New);
  302. }
  303. break;
  304. }
  305. }
  306. return false;
  307. }
  308. /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
  309. /// This uses isZExtFree and ZERO_EXTEND for the widening cast, but it could be
  310. /// generalized for targets with other types of implicit widening casts.
  311. bool
  312. TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
  313. unsigned BitWidth,
  314. const APInt &Demanded,
  315. SDLoc dl) {
  316. assert(Op.getNumOperands() == 2 &&
  317. "ShrinkDemandedOp only supports binary operators!");
  318. assert(Op.getNode()->getNumValues() == 1 &&
  319. "ShrinkDemandedOp only supports nodes with one result!");
  320. // Early return, as this function cannot handle vector types.
  321. if (Op.getValueType().isVector())
  322. return false;
  323. // Don't do this if the node has another user, which may require the
  324. // full value.
  325. if (!Op.getNode()->hasOneUse())
  326. return false;
  327. // Search for the smallest integer type with free casts to and from
  328. // Op's type. For expedience, just check power-of-2 integer types.
  329. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  330. unsigned DemandedSize = BitWidth - Demanded.countLeadingZeros();
  331. unsigned SmallVTBits = DemandedSize;
  332. if (!isPowerOf2_32(SmallVTBits))
  333. SmallVTBits = NextPowerOf2(SmallVTBits);
  334. for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
  335. EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
  336. if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
  337. TLI.isZExtFree(SmallVT, Op.getValueType())) {
  338. // We found a type with free casts.
  339. SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
  340. DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
  341. Op.getNode()->getOperand(0)),
  342. DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
  343. Op.getNode()->getOperand(1)));
  344. bool NeedZext = DemandedSize > SmallVTBits;
  345. SDValue Z = DAG.getNode(NeedZext ? ISD::ZERO_EXTEND : ISD::ANY_EXTEND,
  346. dl, Op.getValueType(), X);
  347. return CombineTo(Op, Z);
  348. }
  349. }
  350. return false;
  351. }
  352. /// Look at Op. At this point, we know that only the DemandedMask bits of the
  353. /// result of Op are ever used downstream. If we can use this information to
  354. /// simplify Op, create a new simplified DAG node and return true, returning the
  355. /// original and new nodes in Old and New. Otherwise, analyze the expression and
  356. /// return a mask of KnownOne and KnownZero bits for the expression (used to
  357. /// simplify the caller). The KnownZero/One bits may only be accurate for those
  358. /// bits in the DemandedMask.
  359. bool TargetLowering::SimplifyDemandedBits(SDValue Op,
  360. const APInt &DemandedMask,
  361. APInt &KnownZero,
  362. APInt &KnownOne,
  363. TargetLoweringOpt &TLO,
  364. unsigned Depth) const {
  365. unsigned BitWidth = DemandedMask.getBitWidth();
  366. assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
  367. "Mask size mismatches value type size!");
  368. APInt NewMask = DemandedMask;
  369. SDLoc dl(Op);
  370. auto &DL = TLO.DAG.getDataLayout();
  371. // Don't know anything.
  372. KnownZero = KnownOne = APInt(BitWidth, 0);
  373. // Other users may use these bits.
  374. if (!Op.getNode()->hasOneUse()) {
  375. if (Depth != 0) {
  376. // If not at the root, Just compute the KnownZero/KnownOne bits to
  377. // simplify things downstream.
  378. TLO.DAG.computeKnownBits(Op, KnownZero, KnownOne, Depth);
  379. return false;
  380. }
  381. // If this is the root being simplified, allow it to have multiple uses,
  382. // just set the NewMask to all bits.
  383. NewMask = APInt::getAllOnesValue(BitWidth);
  384. } else if (DemandedMask == 0) {
  385. // Not demanding any bits from Op.
  386. if (!Op.isUndef())
  387. return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
  388. return false;
  389. } else if (Depth == 6) { // Limit search depth.
  390. return false;
  391. }
  392. APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
  393. switch (Op.getOpcode()) {
  394. case ISD::Constant:
  395. // We know all of the bits for a constant!
  396. KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
  397. KnownZero = ~KnownOne;
  398. return false; // Don't fall through, will infinitely loop.
  399. case ISD::AND:
  400. // If the RHS is a constant, check to see if the LHS would be zero without
  401. // using the bits from the RHS. Below, we use knowledge about the RHS to
  402. // simplify the LHS, here we're using information from the LHS to simplify
  403. // the RHS.
  404. if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  405. APInt LHSZero, LHSOne;
  406. // Do not increment Depth here; that can cause an infinite loop.
  407. TLO.DAG.computeKnownBits(Op.getOperand(0), LHSZero, LHSOne, Depth);
  408. // If the LHS already has zeros where RHSC does, this and is dead.
  409. if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
  410. return TLO.CombineTo(Op, Op.getOperand(0));
  411. // If any of the set bits in the RHS are known zero on the LHS, shrink
  412. // the constant.
  413. if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
  414. return true;
  415. }
  416. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
  417. KnownOne, TLO, Depth+1))
  418. return true;
  419. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  420. if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
  421. KnownZero2, KnownOne2, TLO, Depth+1))
  422. return true;
  423. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  424. // If all of the demanded bits are known one on one side, return the other.
  425. // These bits cannot contribute to the result of the 'and'.
  426. if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
  427. return TLO.CombineTo(Op, Op.getOperand(0));
  428. if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
  429. return TLO.CombineTo(Op, Op.getOperand(1));
  430. // If all of the demanded bits in the inputs are known zeros, return zero.
  431. if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
  432. return TLO.CombineTo(Op, TLO.DAG.getConstant(0, dl, Op.getValueType()));
  433. // If the RHS is a constant, see if we can simplify it.
  434. if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
  435. return true;
  436. // If the operation can be done in a smaller type, do so.
  437. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  438. return true;
  439. // Output known-1 bits are only known if set in both the LHS & RHS.
  440. KnownOne &= KnownOne2;
  441. // Output known-0 are known to be clear if zero in either the LHS | RHS.
  442. KnownZero |= KnownZero2;
  443. break;
  444. case ISD::OR:
  445. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
  446. KnownOne, TLO, Depth+1))
  447. return true;
  448. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  449. if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
  450. KnownZero2, KnownOne2, TLO, Depth+1))
  451. return true;
  452. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  453. // If all of the demanded bits are known zero on one side, return the other.
  454. // These bits cannot contribute to the result of the 'or'.
  455. if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
  456. return TLO.CombineTo(Op, Op.getOperand(0));
  457. if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
  458. return TLO.CombineTo(Op, Op.getOperand(1));
  459. // If all of the potentially set bits on one side are known to be set on
  460. // the other side, just use the 'other' side.
  461. if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
  462. return TLO.CombineTo(Op, Op.getOperand(0));
  463. if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
  464. return TLO.CombineTo(Op, Op.getOperand(1));
  465. // If the RHS is a constant, see if we can simplify it.
  466. if (TLO.ShrinkDemandedConstant(Op, NewMask))
  467. return true;
  468. // If the operation can be done in a smaller type, do so.
  469. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  470. return true;
  471. // Output known-0 bits are only known if clear in both the LHS & RHS.
  472. KnownZero &= KnownZero2;
  473. // Output known-1 are known to be set if set in either the LHS | RHS.
  474. KnownOne |= KnownOne2;
  475. break;
  476. case ISD::XOR:
  477. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
  478. KnownOne, TLO, Depth+1))
  479. return true;
  480. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  481. if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
  482. KnownOne2, TLO, Depth+1))
  483. return true;
  484. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  485. // If all of the demanded bits are known zero on one side, return the other.
  486. // These bits cannot contribute to the result of the 'xor'.
  487. if ((KnownZero & NewMask) == NewMask)
  488. return TLO.CombineTo(Op, Op.getOperand(0));
  489. if ((KnownZero2 & NewMask) == NewMask)
  490. return TLO.CombineTo(Op, Op.getOperand(1));
  491. // If the operation can be done in a smaller type, do so.
  492. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  493. return true;
  494. // If all of the unknown bits are known to be zero on one side or the other
  495. // (but not both) turn this into an *inclusive* or.
  496. // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
  497. if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
  498. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
  499. Op.getOperand(0),
  500. Op.getOperand(1)));
  501. // Output known-0 bits are known if clear or set in both the LHS & RHS.
  502. KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
  503. // Output known-1 are known to be set if set in only one of the LHS, RHS.
  504. KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
  505. // If all of the demanded bits on one side are known, and all of the set
  506. // bits on that side are also known to be set on the other side, turn this
  507. // into an AND, as we know the bits will be cleared.
  508. // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
  509. // NB: it is okay if more bits are known than are requested
  510. if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side
  511. if (KnownOne == KnownOne2) { // set bits are the same on both sides
  512. EVT VT = Op.getValueType();
  513. SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, dl, VT);
  514. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
  515. Op.getOperand(0), ANDC));
  516. }
  517. }
  518. // If the RHS is a constant, see if we can simplify it.
  519. // for XOR, we prefer to force bits to 1 if they will make a -1.
  520. // if we can't force bits, try to shrink constant
  521. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  522. APInt Expanded = C->getAPIntValue() | (~NewMask);
  523. // if we can expand it to have all bits set, do it
  524. if (Expanded.isAllOnesValue()) {
  525. if (Expanded != C->getAPIntValue()) {
  526. EVT VT = Op.getValueType();
  527. SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
  528. TLO.DAG.getConstant(Expanded, dl, VT));
  529. return TLO.CombineTo(Op, New);
  530. }
  531. // if it already has all the bits set, nothing to change
  532. // but don't shrink either!
  533. } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
  534. return true;
  535. }
  536. }
  537. KnownZero = KnownZeroOut;
  538. KnownOne = KnownOneOut;
  539. break;
  540. case ISD::SELECT:
  541. if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
  542. KnownOne, TLO, Depth+1))
  543. return true;
  544. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
  545. KnownOne2, TLO, Depth+1))
  546. return true;
  547. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  548. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  549. // If the operands are constants, see if we can simplify them.
  550. if (TLO.ShrinkDemandedConstant(Op, NewMask))
  551. return true;
  552. // Only known if known in both the LHS and RHS.
  553. KnownOne &= KnownOne2;
  554. KnownZero &= KnownZero2;
  555. break;
  556. case ISD::SELECT_CC:
  557. if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
  558. KnownOne, TLO, Depth+1))
  559. return true;
  560. if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
  561. KnownOne2, TLO, Depth+1))
  562. return true;
  563. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  564. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  565. // If the operands are constants, see if we can simplify them.
  566. if (TLO.ShrinkDemandedConstant(Op, NewMask))
  567. return true;
  568. // Only known if known in both the LHS and RHS.
  569. KnownOne &= KnownOne2;
  570. KnownZero &= KnownZero2;
  571. break;
  572. case ISD::SHL:
  573. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  574. unsigned ShAmt = SA->getZExtValue();
  575. SDValue InOp = Op.getOperand(0);
  576. // If the shift count is an invalid immediate, don't do anything.
  577. if (ShAmt >= BitWidth)
  578. break;
  579. // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
  580. // single shift. We can do this if the bottom bits (which are shifted
  581. // out) are never demanded.
  582. if (InOp.getOpcode() == ISD::SRL &&
  583. isa<ConstantSDNode>(InOp.getOperand(1))) {
  584. if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
  585. unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
  586. unsigned Opc = ISD::SHL;
  587. int Diff = ShAmt-C1;
  588. if (Diff < 0) {
  589. Diff = -Diff;
  590. Opc = ISD::SRL;
  591. }
  592. SDValue NewSA =
  593. TLO.DAG.getConstant(Diff, dl, Op.getOperand(1).getValueType());
  594. EVT VT = Op.getValueType();
  595. return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
  596. InOp.getOperand(0), NewSA));
  597. }
  598. }
  599. if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
  600. KnownZero, KnownOne, TLO, Depth+1))
  601. return true;
  602. // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
  603. // are not demanded. This will likely allow the anyext to be folded away.
  604. if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
  605. SDValue InnerOp = InOp.getNode()->getOperand(0);
  606. EVT InnerVT = InnerOp.getValueType();
  607. unsigned InnerBits = InnerVT.getSizeInBits();
  608. if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
  609. isTypeDesirableForOp(ISD::SHL, InnerVT)) {
  610. EVT ShTy = getShiftAmountTy(InnerVT, DL);
  611. if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
  612. ShTy = InnerVT;
  613. SDValue NarrowShl =
  614. TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
  615. TLO.DAG.getConstant(ShAmt, dl, ShTy));
  616. return
  617. TLO.CombineTo(Op,
  618. TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
  619. NarrowShl));
  620. }
  621. // Repeat the SHL optimization above in cases where an extension
  622. // intervenes: (shl (anyext (shr x, c1)), c2) to
  623. // (shl (anyext x), c2-c1). This requires that the bottom c1 bits
  624. // aren't demanded (as above) and that the shifted upper c1 bits of
  625. // x aren't demanded.
  626. if (InOp.hasOneUse() &&
  627. InnerOp.getOpcode() == ISD::SRL &&
  628. InnerOp.hasOneUse() &&
  629. isa<ConstantSDNode>(InnerOp.getOperand(1))) {
  630. uint64_t InnerShAmt = cast<ConstantSDNode>(InnerOp.getOperand(1))
  631. ->getZExtValue();
  632. if (InnerShAmt < ShAmt &&
  633. InnerShAmt < InnerBits &&
  634. NewMask.lshr(InnerBits - InnerShAmt + ShAmt) == 0 &&
  635. NewMask.trunc(ShAmt) == 0) {
  636. SDValue NewSA =
  637. TLO.DAG.getConstant(ShAmt - InnerShAmt, dl,
  638. Op.getOperand(1).getValueType());
  639. EVT VT = Op.getValueType();
  640. SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT,
  641. InnerOp.getOperand(0));
  642. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl, VT,
  643. NewExt, NewSA));
  644. }
  645. }
  646. }
  647. KnownZero <<= SA->getZExtValue();
  648. KnownOne <<= SA->getZExtValue();
  649. // low bits known zero.
  650. KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
  651. }
  652. break;
  653. case ISD::SRL:
  654. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  655. EVT VT = Op.getValueType();
  656. unsigned ShAmt = SA->getZExtValue();
  657. unsigned VTSize = VT.getSizeInBits();
  658. SDValue InOp = Op.getOperand(0);
  659. // If the shift count is an invalid immediate, don't do anything.
  660. if (ShAmt >= BitWidth)
  661. break;
  662. APInt InDemandedMask = (NewMask << ShAmt);
  663. // If the shift is exact, then it does demand the low bits (and knows that
  664. // they are zero).
  665. if (cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact())
  666. InDemandedMask |= APInt::getLowBitsSet(BitWidth, ShAmt);
  667. // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
  668. // single shift. We can do this if the top bits (which are shifted out)
  669. // are never demanded.
  670. if (InOp.getOpcode() == ISD::SHL &&
  671. isa<ConstantSDNode>(InOp.getOperand(1))) {
  672. if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
  673. unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
  674. unsigned Opc = ISD::SRL;
  675. int Diff = ShAmt-C1;
  676. if (Diff < 0) {
  677. Diff = -Diff;
  678. Opc = ISD::SHL;
  679. }
  680. SDValue NewSA =
  681. TLO.DAG.getConstant(Diff, dl, Op.getOperand(1).getValueType());
  682. return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
  683. InOp.getOperand(0), NewSA));
  684. }
  685. }
  686. // Compute the new bits that are at the top now.
  687. if (SimplifyDemandedBits(InOp, InDemandedMask,
  688. KnownZero, KnownOne, TLO, Depth+1))
  689. return true;
  690. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  691. KnownZero = KnownZero.lshr(ShAmt);
  692. KnownOne = KnownOne.lshr(ShAmt);
  693. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
  694. KnownZero |= HighBits; // High bits known zero.
  695. }
  696. break;
  697. case ISD::SRA:
  698. // If this is an arithmetic shift right and only the low-bit is set, we can
  699. // always convert this into a logical shr, even if the shift amount is
  700. // variable. The low bit of the shift cannot be an input sign bit unless
  701. // the shift amount is >= the size of the datatype, which is undefined.
  702. if (NewMask == 1)
  703. return TLO.CombineTo(Op,
  704. TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
  705. Op.getOperand(0), Op.getOperand(1)));
  706. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  707. EVT VT = Op.getValueType();
  708. unsigned ShAmt = SA->getZExtValue();
  709. // If the shift count is an invalid immediate, don't do anything.
  710. if (ShAmt >= BitWidth)
  711. break;
  712. APInt InDemandedMask = (NewMask << ShAmt);
  713. // If the shift is exact, then it does demand the low bits (and knows that
  714. // they are zero).
  715. if (cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact())
  716. InDemandedMask |= APInt::getLowBitsSet(BitWidth, ShAmt);
  717. // If any of the demanded bits are produced by the sign extension, we also
  718. // demand the input sign bit.
  719. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
  720. if (HighBits.intersects(NewMask))
  721. InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
  722. if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
  723. KnownZero, KnownOne, TLO, Depth+1))
  724. return true;
  725. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  726. KnownZero = KnownZero.lshr(ShAmt);
  727. KnownOne = KnownOne.lshr(ShAmt);
  728. // Handle the sign bit, adjusted to where it is now in the mask.
  729. APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
  730. // If the input sign bit is known to be zero, or if none of the top bits
  731. // are demanded, turn this into an unsigned shift right.
  732. if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
  733. SDNodeFlags Flags;
  734. Flags.setExact(cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact());
  735. return TLO.CombineTo(Op,
  736. TLO.DAG.getNode(ISD::SRL, dl, VT, Op.getOperand(0),
  737. Op.getOperand(1), &Flags));
  738. }
  739. int Log2 = NewMask.exactLogBase2();
  740. if (Log2 >= 0) {
  741. // The bit must come from the sign.
  742. SDValue NewSA =
  743. TLO.DAG.getConstant(BitWidth - 1 - Log2, dl,
  744. Op.getOperand(1).getValueType());
  745. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
  746. Op.getOperand(0), NewSA));
  747. }
  748. if (KnownOne.intersects(SignBit))
  749. // New bits are known one.
  750. KnownOne |= HighBits;
  751. }
  752. break;
  753. case ISD::SIGN_EXTEND_INREG: {
  754. EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  755. APInt MsbMask = APInt::getHighBitsSet(BitWidth, 1);
  756. // If we only care about the highest bit, don't bother shifting right.
  757. if (MsbMask == NewMask) {
  758. unsigned ShAmt = ExVT.getScalarType().getSizeInBits();
  759. SDValue InOp = Op.getOperand(0);
  760. unsigned VTBits = Op->getValueType(0).getScalarType().getSizeInBits();
  761. bool AlreadySignExtended =
  762. TLO.DAG.ComputeNumSignBits(InOp) >= VTBits-ShAmt+1;
  763. // However if the input is already sign extended we expect the sign
  764. // extension to be dropped altogether later and do not simplify.
  765. if (!AlreadySignExtended) {
  766. // Compute the correct shift amount type, which must be getShiftAmountTy
  767. // for scalar types after legalization.
  768. EVT ShiftAmtTy = Op.getValueType();
  769. if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
  770. ShiftAmtTy = getShiftAmountTy(ShiftAmtTy, DL);
  771. SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ShAmt, dl,
  772. ShiftAmtTy);
  773. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
  774. Op.getValueType(), InOp,
  775. ShiftAmt));
  776. }
  777. }
  778. // Sign extension. Compute the demanded bits in the result that are not
  779. // present in the input.
  780. APInt NewBits =
  781. APInt::getHighBitsSet(BitWidth,
  782. BitWidth - ExVT.getScalarType().getSizeInBits());
  783. // If none of the extended bits are demanded, eliminate the sextinreg.
  784. if ((NewBits & NewMask) == 0)
  785. return TLO.CombineTo(Op, Op.getOperand(0));
  786. APInt InSignBit =
  787. APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth);
  788. APInt InputDemandedBits =
  789. APInt::getLowBitsSet(BitWidth,
  790. ExVT.getScalarType().getSizeInBits()) &
  791. NewMask;
  792. // Since the sign extended bits are demanded, we know that the sign
  793. // bit is demanded.
  794. InputDemandedBits |= InSignBit;
  795. if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
  796. KnownZero, KnownOne, TLO, Depth+1))
  797. return true;
  798. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  799. // If the sign bit of the input is known set or clear, then we know the
  800. // top bits of the result.
  801. // If the input sign bit is known zero, convert this into a zero extension.
  802. if (KnownZero.intersects(InSignBit))
  803. return TLO.CombineTo(Op,
  804. TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT));
  805. if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
  806. KnownOne |= NewBits;
  807. KnownZero &= ~NewBits;
  808. } else { // Input sign bit unknown
  809. KnownZero &= ~NewBits;
  810. KnownOne &= ~NewBits;
  811. }
  812. break;
  813. }
  814. case ISD::BUILD_PAIR: {
  815. EVT HalfVT = Op.getOperand(0).getValueType();
  816. unsigned HalfBitWidth = HalfVT.getScalarSizeInBits();
  817. APInt MaskLo = NewMask.getLoBits(HalfBitWidth).trunc(HalfBitWidth);
  818. APInt MaskHi = NewMask.getHiBits(HalfBitWidth).trunc(HalfBitWidth);
  819. APInt KnownZeroLo, KnownOneLo;
  820. APInt KnownZeroHi, KnownOneHi;
  821. if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownZeroLo,
  822. KnownOneLo, TLO, Depth + 1))
  823. return true;
  824. if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownZeroHi,
  825. KnownOneHi, TLO, Depth + 1))
  826. return true;
  827. KnownZero = KnownZeroLo.zext(BitWidth) |
  828. KnownZeroHi.zext(BitWidth).shl(HalfBitWidth);
  829. KnownOne = KnownOneLo.zext(BitWidth) |
  830. KnownOneHi.zext(BitWidth).shl(HalfBitWidth);
  831. break;
  832. }
  833. case ISD::ZERO_EXTEND: {
  834. unsigned OperandBitWidth =
  835. Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  836. APInt InMask = NewMask.trunc(OperandBitWidth);
  837. // If none of the top bits are demanded, convert this into an any_extend.
  838. APInt NewBits =
  839. APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
  840. if (!NewBits.intersects(NewMask))
  841. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
  842. Op.getValueType(),
  843. Op.getOperand(0)));
  844. if (SimplifyDemandedBits(Op.getOperand(0), InMask,
  845. KnownZero, KnownOne, TLO, Depth+1))
  846. return true;
  847. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  848. KnownZero = KnownZero.zext(BitWidth);
  849. KnownOne = KnownOne.zext(BitWidth);
  850. KnownZero |= NewBits;
  851. break;
  852. }
  853. case ISD::SIGN_EXTEND: {
  854. EVT InVT = Op.getOperand(0).getValueType();
  855. unsigned InBits = InVT.getScalarType().getSizeInBits();
  856. APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
  857. APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
  858. APInt NewBits = ~InMask & NewMask;
  859. // If none of the top bits are demanded, convert this into an any_extend.
  860. if (NewBits == 0)
  861. return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
  862. Op.getValueType(),
  863. Op.getOperand(0)));
  864. // Since some of the sign extended bits are demanded, we know that the sign
  865. // bit is demanded.
  866. APInt InDemandedBits = InMask & NewMask;
  867. InDemandedBits |= InSignBit;
  868. InDemandedBits = InDemandedBits.trunc(InBits);
  869. if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
  870. KnownOne, TLO, Depth+1))
  871. return true;
  872. KnownZero = KnownZero.zext(BitWidth);
  873. KnownOne = KnownOne.zext(BitWidth);
  874. // If the sign bit is known zero, convert this to a zero extend.
  875. if (KnownZero.intersects(InSignBit))
  876. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
  877. Op.getValueType(),
  878. Op.getOperand(0)));
  879. // If the sign bit is known one, the top bits match.
  880. if (KnownOne.intersects(InSignBit)) {
  881. KnownOne |= NewBits;
  882. assert((KnownZero & NewBits) == 0);
  883. } else { // Otherwise, top bits aren't known.
  884. assert((KnownOne & NewBits) == 0);
  885. assert((KnownZero & NewBits) == 0);
  886. }
  887. break;
  888. }
  889. case ISD::ANY_EXTEND: {
  890. unsigned OperandBitWidth =
  891. Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  892. APInt InMask = NewMask.trunc(OperandBitWidth);
  893. if (SimplifyDemandedBits(Op.getOperand(0), InMask,
  894. KnownZero, KnownOne, TLO, Depth+1))
  895. return true;
  896. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  897. KnownZero = KnownZero.zext(BitWidth);
  898. KnownOne = KnownOne.zext(BitWidth);
  899. break;
  900. }
  901. case ISD::TRUNCATE: {
  902. // Simplify the input, using demanded bit information, and compute the known
  903. // zero/one bits live out.
  904. unsigned OperandBitWidth =
  905. Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  906. APInt TruncMask = NewMask.zext(OperandBitWidth);
  907. if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
  908. KnownZero, KnownOne, TLO, Depth+1))
  909. return true;
  910. KnownZero = KnownZero.trunc(BitWidth);
  911. KnownOne = KnownOne.trunc(BitWidth);
  912. // If the input is only used by this truncate, see if we can shrink it based
  913. // on the known demanded bits.
  914. if (Op.getOperand(0).getNode()->hasOneUse()) {
  915. SDValue In = Op.getOperand(0);
  916. switch (In.getOpcode()) {
  917. default: break;
  918. case ISD::SRL:
  919. // Shrink SRL by a constant if none of the high bits shifted in are
  920. // demanded.
  921. if (TLO.LegalTypes() &&
  922. !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
  923. // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
  924. // undesirable.
  925. break;
  926. ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
  927. if (!ShAmt)
  928. break;
  929. SDValue Shift = In.getOperand(1);
  930. if (TLO.LegalTypes()) {
  931. uint64_t ShVal = ShAmt->getZExtValue();
  932. Shift = TLO.DAG.getConstant(ShVal, dl,
  933. getShiftAmountTy(Op.getValueType(), DL));
  934. }
  935. APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
  936. OperandBitWidth - BitWidth);
  937. HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
  938. if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
  939. // None of the shifted in bits are needed. Add a truncate of the
  940. // shift input, then shift it.
  941. SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
  942. Op.getValueType(),
  943. In.getOperand(0));
  944. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
  945. Op.getValueType(),
  946. NewTrunc,
  947. Shift));
  948. }
  949. break;
  950. }
  951. }
  952. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  953. break;
  954. }
  955. case ISD::AssertZext: {
  956. // AssertZext demands all of the high bits, plus any of the low bits
  957. // demanded by its users.
  958. EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  959. APInt InMask = APInt::getLowBitsSet(BitWidth,
  960. VT.getSizeInBits());
  961. if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
  962. KnownZero, KnownOne, TLO, Depth+1))
  963. return true;
  964. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  965. KnownZero |= ~InMask & NewMask;
  966. break;
  967. }
  968. case ISD::BITCAST:
  969. // If this is an FP->Int bitcast and if the sign bit is the only
  970. // thing demanded, turn this into a FGETSIGN.
  971. if (!TLO.LegalOperations() &&
  972. !Op.getValueType().isVector() &&
  973. !Op.getOperand(0).getValueType().isVector() &&
  974. NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
  975. Op.getOperand(0).getValueType().isFloatingPoint()) {
  976. bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
  977. bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
  978. if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple() &&
  979. Op.getOperand(0).getValueType() != MVT::f128) {
  980. // Cannot eliminate/lower SHL for f128 yet.
  981. EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
  982. // Make a FGETSIGN + SHL to move the sign bit into the appropriate
  983. // place. We expect the SHL to be eliminated by other optimizations.
  984. SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
  985. unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
  986. if (!OpVTLegal && OpVTSizeInBits > 32)
  987. Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
  988. unsigned ShVal = Op.getValueType().getSizeInBits()-1;
  989. SDValue ShAmt = TLO.DAG.getConstant(ShVal, dl, Op.getValueType());
  990. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
  991. Op.getValueType(),
  992. Sign, ShAmt));
  993. }
  994. }
  995. break;
  996. case ISD::ADD:
  997. case ISD::MUL:
  998. case ISD::SUB: {
  999. // Add, Sub, and Mul don't demand any bits in positions beyond that
  1000. // of the highest bit demanded of them.
  1001. APInt LoMask = APInt::getLowBitsSet(BitWidth,
  1002. BitWidth - NewMask.countLeadingZeros());
  1003. if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
  1004. KnownOne2, TLO, Depth+1))
  1005. return true;
  1006. if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
  1007. KnownOne2, TLO, Depth+1))
  1008. return true;
  1009. // See if the operation should be performed at a smaller bit width.
  1010. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  1011. return true;
  1012. }
  1013. // FALL THROUGH
  1014. default:
  1015. // Just use computeKnownBits to compute output bits.
  1016. TLO.DAG.computeKnownBits(Op, KnownZero, KnownOne, Depth);
  1017. break;
  1018. }
  1019. // If we know the value of all of the demanded bits, return this as a
  1020. // constant.
  1021. if ((NewMask & (KnownZero|KnownOne)) == NewMask) {
  1022. // Avoid folding to a constant if any OpaqueConstant is involved.
  1023. const SDNode *N = Op.getNode();
  1024. for (SDNodeIterator I = SDNodeIterator::begin(N),
  1025. E = SDNodeIterator::end(N); I != E; ++I) {
  1026. SDNode *Op = *I;
  1027. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
  1028. if (C->isOpaque())
  1029. return false;
  1030. }
  1031. return TLO.CombineTo(Op,
  1032. TLO.DAG.getConstant(KnownOne, dl, Op.getValueType()));
  1033. }
  1034. return false;
  1035. }
  1036. /// Determine which of the bits specified in Mask are known to be either zero or
  1037. /// one and return them in the KnownZero/KnownOne bitsets.
  1038. void TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
  1039. APInt &KnownZero,
  1040. APInt &KnownOne,
  1041. const SelectionDAG &DAG,
  1042. unsigned Depth) const {
  1043. assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
  1044. Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
  1045. Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
  1046. Op.getOpcode() == ISD::INTRINSIC_VOID) &&
  1047. "Should use MaskedValueIsZero if you don't know whether Op"
  1048. " is a target node!");
  1049. KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
  1050. }
  1051. /// This method can be implemented by targets that want to expose additional
  1052. /// information about sign bits to the DAG Combiner.
  1053. unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
  1054. const SelectionDAG &,
  1055. unsigned Depth) const {
  1056. assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
  1057. Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
  1058. Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
  1059. Op.getOpcode() == ISD::INTRINSIC_VOID) &&
  1060. "Should use ComputeNumSignBits if you don't know whether Op"
  1061. " is a target node!");
  1062. return 1;
  1063. }
  1064. /// Test if the given value is known to have exactly one bit set. This differs
  1065. /// from computeKnownBits in that it doesn't need to determine which bit is set.
  1066. static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
  1067. // A left-shift of a constant one will have exactly one bit set, because
  1068. // shifting the bit off the end is undefined.
  1069. if (Val.getOpcode() == ISD::SHL)
  1070. if (ConstantSDNode *C =
  1071. dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
  1072. if (C->getAPIntValue() == 1)
  1073. return true;
  1074. // Similarly, a right-shift of a constant sign-bit will have exactly
  1075. // one bit set.
  1076. if (Val.getOpcode() == ISD::SRL)
  1077. if (ConstantSDNode *C =
  1078. dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
  1079. if (C->getAPIntValue().isSignBit())
  1080. return true;
  1081. // More could be done here, though the above checks are enough
  1082. // to handle some common cases.
  1083. // Fall back to computeKnownBits to catch other known cases.
  1084. EVT OpVT = Val.getValueType();
  1085. unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
  1086. APInt KnownZero, KnownOne;
  1087. DAG.computeKnownBits(Val, KnownZero, KnownOne);
  1088. return (KnownZero.countPopulation() == BitWidth - 1) &&
  1089. (KnownOne.countPopulation() == 1);
  1090. }
  1091. bool TargetLowering::isConstTrueVal(const SDNode *N) const {
  1092. if (!N)
  1093. return false;
  1094. const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
  1095. if (!CN) {
  1096. const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
  1097. if (!BV)
  1098. return false;
  1099. BitVector UndefElements;
  1100. CN = BV->getConstantSplatNode(&UndefElements);
  1101. // Only interested in constant splats, and we don't try to handle undef
  1102. // elements in identifying boolean constants.
  1103. if (!CN || UndefElements.none())
  1104. return false;
  1105. }
  1106. switch (getBooleanContents(N->getValueType(0))) {
  1107. case UndefinedBooleanContent:
  1108. return CN->getAPIntValue()[0];
  1109. case ZeroOrOneBooleanContent:
  1110. return CN->isOne();
  1111. case ZeroOrNegativeOneBooleanContent:
  1112. return CN->isAllOnesValue();
  1113. }
  1114. llvm_unreachable("Invalid boolean contents");
  1115. }
  1116. bool TargetLowering::isConstFalseVal(const SDNode *N) const {
  1117. if (!N)
  1118. return false;
  1119. const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
  1120. if (!CN) {
  1121. const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
  1122. if (!BV)
  1123. return false;
  1124. BitVector UndefElements;
  1125. CN = BV->getConstantSplatNode(&UndefElements);
  1126. // Only interested in constant splats, and we don't try to handle undef
  1127. // elements in identifying boolean constants.
  1128. if (!CN || UndefElements.none())
  1129. return false;
  1130. }
  1131. if (getBooleanContents(N->getValueType(0)) == UndefinedBooleanContent)
  1132. return !CN->getAPIntValue()[0];
  1133. return CN->isNullValue();
  1134. }
  1135. bool TargetLowering::isExtendedTrueVal(const ConstantSDNode *N, EVT VT,
  1136. bool SExt) const {
  1137. if (VT == MVT::i1)
  1138. return N->isOne();
  1139. TargetLowering::BooleanContent Cnt = getBooleanContents(VT);
  1140. switch (Cnt) {
  1141. case TargetLowering::ZeroOrOneBooleanContent:
  1142. // An extended value of 1 is always true, unless its original type is i1,
  1143. // in which case it will be sign extended to -1.
  1144. return (N->isOne() && !SExt) || (SExt && (N->getValueType(0) != MVT::i1));
  1145. case TargetLowering::UndefinedBooleanContent:
  1146. case TargetLowering::ZeroOrNegativeOneBooleanContent:
  1147. return N->isAllOnesValue() && SExt;
  1148. }
  1149. llvm_unreachable("Unexpected enumeration.");
  1150. }
  1151. /// Try to simplify a setcc built with the specified operands and cc. If it is
  1152. /// unable to simplify it, return a null SDValue.
  1153. SDValue
  1154. TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
  1155. ISD::CondCode Cond, bool foldBooleans,
  1156. DAGCombinerInfo &DCI, SDLoc dl) const {
  1157. SelectionDAG &DAG = DCI.DAG;
  1158. // These setcc operations always fold.
  1159. switch (Cond) {
  1160. default: break;
  1161. case ISD::SETFALSE:
  1162. case ISD::SETFALSE2: return DAG.getConstant(0, dl, VT);
  1163. case ISD::SETTRUE:
  1164. case ISD::SETTRUE2: {
  1165. TargetLowering::BooleanContent Cnt =
  1166. getBooleanContents(N0->getValueType(0));
  1167. return DAG.getConstant(
  1168. Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
  1169. VT);
  1170. }
  1171. }
  1172. // Ensure that the constant occurs on the RHS, and fold constant
  1173. // comparisons.
  1174. ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
  1175. if (isa<ConstantSDNode>(N0.getNode()) &&
  1176. (DCI.isBeforeLegalizeOps() ||
  1177. isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
  1178. return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
  1179. if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
  1180. const APInt &C1 = N1C->getAPIntValue();
  1181. // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
  1182. // equality comparison, then we're just comparing whether X itself is
  1183. // zero.
  1184. if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
  1185. N0.getOperand(0).getOpcode() == ISD::CTLZ &&
  1186. N0.getOperand(1).getOpcode() == ISD::Constant) {
  1187. const APInt &ShAmt
  1188. = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
  1189. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  1190. ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
  1191. if ((C1 == 0) == (Cond == ISD::SETEQ)) {
  1192. // (srl (ctlz x), 5) == 0 -> X != 0
  1193. // (srl (ctlz x), 5) != 1 -> X != 0
  1194. Cond = ISD::SETNE;
  1195. } else {
  1196. // (srl (ctlz x), 5) != 0 -> X == 0
  1197. // (srl (ctlz x), 5) == 1 -> X == 0
  1198. Cond = ISD::SETEQ;
  1199. }
  1200. SDValue Zero = DAG.getConstant(0, dl, N0.getValueType());
  1201. return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
  1202. Zero, Cond);
  1203. }
  1204. }
  1205. SDValue CTPOP = N0;
  1206. // Look through truncs that don't change the value of a ctpop.
  1207. if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
  1208. CTPOP = N0.getOperand(0);
  1209. if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
  1210. (N0 == CTPOP || N0.getValueType().getSizeInBits() >
  1211. Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
  1212. EVT CTVT = CTPOP.getValueType();
  1213. SDValue CTOp = CTPOP.getOperand(0);
  1214. // (ctpop x) u< 2 -> (x & x-1) == 0
  1215. // (ctpop x) u> 1 -> (x & x-1) != 0
  1216. if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
  1217. SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
  1218. DAG.getConstant(1, dl, CTVT));
  1219. SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
  1220. ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
  1221. return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, dl, CTVT), CC);
  1222. }
  1223. // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
  1224. }
  1225. // (zext x) == C --> x == (trunc C)
  1226. // (sext x) == C --> x == (trunc C)
  1227. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  1228. DCI.isBeforeLegalize() && N0->hasOneUse()) {
  1229. unsigned MinBits = N0.getValueSizeInBits();
  1230. SDValue PreExt;
  1231. bool Signed = false;
  1232. if (N0->getOpcode() == ISD::ZERO_EXTEND) {
  1233. // ZExt
  1234. MinBits = N0->getOperand(0).getValueSizeInBits();
  1235. PreExt = N0->getOperand(0);
  1236. } else if (N0->getOpcode() == ISD::AND) {
  1237. // DAGCombine turns costly ZExts into ANDs
  1238. if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
  1239. if ((C->getAPIntValue()+1).isPowerOf2()) {
  1240. MinBits = C->getAPIntValue().countTrailingOnes();
  1241. PreExt = N0->getOperand(0);
  1242. }
  1243. } else if (N0->getOpcode() == ISD::SIGN_EXTEND) {
  1244. // SExt
  1245. MinBits = N0->getOperand(0).getValueSizeInBits();
  1246. PreExt = N0->getOperand(0);
  1247. Signed = true;
  1248. } else if (auto *LN0 = dyn_cast<LoadSDNode>(N0)) {
  1249. // ZEXTLOAD / SEXTLOAD
  1250. if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
  1251. MinBits = LN0->getMemoryVT().getSizeInBits();
  1252. PreExt = N0;
  1253. } else if (LN0->getExtensionType() == ISD::SEXTLOAD) {
  1254. Signed = true;
  1255. MinBits = LN0->getMemoryVT().getSizeInBits();
  1256. PreExt = N0;
  1257. }
  1258. }
  1259. // Figure out how many bits we need to preserve this constant.
  1260. unsigned ReqdBits = Signed ?
  1261. C1.getBitWidth() - C1.getNumSignBits() + 1 :
  1262. C1.getActiveBits();
  1263. // Make sure we're not losing bits from the constant.
  1264. if (MinBits > 0 &&
  1265. MinBits < C1.getBitWidth() &&
  1266. MinBits >= ReqdBits) {
  1267. EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
  1268. if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
  1269. // Will get folded away.
  1270. SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreExt);
  1271. SDValue C = DAG.getConstant(C1.trunc(MinBits), dl, MinVT);
  1272. return DAG.getSetCC(dl, VT, Trunc, C, Cond);
  1273. }
  1274. // If truncating the setcc operands is not desirable, we can still
  1275. // simplify the expression in some cases:
  1276. // setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> setcc (x, y, cc)
  1277. // setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> setcc (x, y, inv(cc))
  1278. // setcc (zext (setcc x, y, cc)), 1, setne) -> setcc (x, y, inv(cc))
  1279. // setcc (zext (setcc x, y, cc)), 1, seteq) -> setcc (x, y, cc)
  1280. // setcc (sext (setcc x, y, cc)), -1, setne) -> setcc (x, y, inv(cc))
  1281. // setcc (sext (setcc x, y, cc)), -1, seteq) -> setcc (x, y, cc)
  1282. SDValue TopSetCC = N0->getOperand(0);
  1283. unsigned N0Opc = N0->getOpcode();
  1284. bool SExt = (N0Opc == ISD::SIGN_EXTEND);
  1285. if (TopSetCC.getValueType() == MVT::i1 && VT == MVT::i1 &&
  1286. TopSetCC.getOpcode() == ISD::SETCC &&
  1287. (N0Opc == ISD::ZERO_EXTEND || N0Opc == ISD::SIGN_EXTEND) &&
  1288. (isConstFalseVal(N1C) ||
  1289. isExtendedTrueVal(N1C, N0->getValueType(0), SExt))) {
  1290. bool Inverse = (N1C->isNullValue() && Cond == ISD::SETEQ) ||
  1291. (!N1C->isNullValue() && Cond == ISD::SETNE);
  1292. if (!Inverse)
  1293. return TopSetCC;
  1294. ISD::CondCode InvCond = ISD::getSetCCInverse(
  1295. cast<CondCodeSDNode>(TopSetCC.getOperand(2))->get(),
  1296. TopSetCC.getOperand(0).getValueType().isInteger());
  1297. return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0),
  1298. TopSetCC.getOperand(1),
  1299. InvCond);
  1300. }
  1301. }
  1302. }
  1303. // If the LHS is '(and load, const)', the RHS is 0,
  1304. // the test is for equality or unsigned, and all 1 bits of the const are
  1305. // in the same partial word, see if we can shorten the load.
  1306. if (DCI.isBeforeLegalize() &&
  1307. !ISD::isSignedIntSetCC(Cond) &&
  1308. N0.getOpcode() == ISD::AND && C1 == 0 &&
  1309. N0.getNode()->hasOneUse() &&
  1310. isa<LoadSDNode>(N0.getOperand(0)) &&
  1311. N0.getOperand(0).getNode()->hasOneUse() &&
  1312. isa<ConstantSDNode>(N0.getOperand(1))) {
  1313. LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
  1314. APInt bestMask;
  1315. unsigned bestWidth = 0, bestOffset = 0;
  1316. if (!Lod->isVolatile() && Lod->isUnindexed()) {
  1317. unsigned origWidth = N0.getValueType().getSizeInBits();
  1318. unsigned maskWidth = origWidth;
  1319. // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
  1320. // 8 bits, but have to be careful...
  1321. if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
  1322. origWidth = Lod->getMemoryVT().getSizeInBits();
  1323. const APInt &Mask =
  1324. cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
  1325. for (unsigned width = origWidth / 2; width>=8; width /= 2) {
  1326. APInt newMask = APInt::getLowBitsSet(maskWidth, width);
  1327. for (unsigned offset=0; offset<origWidth/width; offset++) {
  1328. if ((newMask & Mask) == Mask) {
  1329. if (!DAG.getDataLayout().isLittleEndian())
  1330. bestOffset = (origWidth/width - offset - 1) * (width/8);
  1331. else
  1332. bestOffset = (uint64_t)offset * (width/8);
  1333. bestMask = Mask.lshr(offset * (width/8) * 8);
  1334. bestWidth = width;
  1335. break;
  1336. }
  1337. newMask = newMask << width;
  1338. }
  1339. }
  1340. }
  1341. if (bestWidth) {
  1342. EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
  1343. if (newVT.isRound()) {
  1344. EVT PtrType = Lod->getOperand(1).getValueType();
  1345. SDValue Ptr = Lod->getBasePtr();
  1346. if (bestOffset != 0)
  1347. Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
  1348. DAG.getConstant(bestOffset, dl, PtrType));
  1349. unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
  1350. SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
  1351. Lod->getPointerInfo().getWithOffset(bestOffset),
  1352. false, false, false, NewAlign);
  1353. return DAG.getSetCC(dl, VT,
  1354. DAG.getNode(ISD::AND, dl, newVT, NewLoad,
  1355. DAG.getConstant(bestMask.trunc(bestWidth),
  1356. dl, newVT)),
  1357. DAG.getConstant(0LL, dl, newVT), Cond);
  1358. }
  1359. }
  1360. }
  1361. // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
  1362. if (N0.getOpcode() == ISD::ZERO_EXTEND) {
  1363. unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
  1364. // If the comparison constant has bits in the upper part, the
  1365. // zero-extended value could never match.
  1366. if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
  1367. C1.getBitWidth() - InSize))) {
  1368. switch (Cond) {
  1369. case ISD::SETUGT:
  1370. case ISD::SETUGE:
  1371. case ISD::SETEQ: return DAG.getConstant(0, dl, VT);
  1372. case ISD::SETULT:
  1373. case ISD::SETULE:
  1374. case ISD::SETNE: return DAG.getConstant(1, dl, VT);
  1375. case ISD::SETGT:
  1376. case ISD::SETGE:
  1377. // True if the sign bit of C1 is set.
  1378. return DAG.getConstant(C1.isNegative(), dl, VT);
  1379. case ISD::SETLT:
  1380. case ISD::SETLE:
  1381. // True if the sign bit of C1 isn't set.
  1382. return DAG.getConstant(C1.isNonNegative(), dl, VT);
  1383. default:
  1384. break;
  1385. }
  1386. }
  1387. // Otherwise, we can perform the comparison with the low bits.
  1388. switch (Cond) {
  1389. case ISD::SETEQ:
  1390. case ISD::SETNE:
  1391. case ISD::SETUGT:
  1392. case ISD::SETUGE:
  1393. case ISD::SETULT:
  1394. case ISD::SETULE: {
  1395. EVT newVT = N0.getOperand(0).getValueType();
  1396. if (DCI.isBeforeLegalizeOps() ||
  1397. (isOperationLegal(ISD::SETCC, newVT) &&
  1398. getCondCodeAction(Cond, newVT.getSimpleVT()) == Legal)) {
  1399. EVT NewSetCCVT =
  1400. getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), newVT);
  1401. SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT);
  1402. SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0),
  1403. NewConst, Cond);
  1404. return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType());
  1405. }
  1406. break;
  1407. }
  1408. default:
  1409. break; // todo, be more careful with signed comparisons
  1410. }
  1411. } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
  1412. (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
  1413. EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
  1414. unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
  1415. EVT ExtDstTy = N0.getValueType();
  1416. unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
  1417. // If the constant doesn't fit into the number of bits for the source of
  1418. // the sign extension, it is impossible for both sides to be equal.
  1419. if (C1.getMinSignedBits() > ExtSrcTyBits)
  1420. return DAG.getConstant(Cond == ISD::SETNE, dl, VT);
  1421. SDValue ZextOp;
  1422. EVT Op0Ty = N0.getOperand(0).getValueType();
  1423. if (Op0Ty == ExtSrcTy) {
  1424. ZextOp = N0.getOperand(0);
  1425. } else {
  1426. APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
  1427. ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
  1428. DAG.getConstant(Imm, dl, Op0Ty));
  1429. }
  1430. if (!DCI.isCalledByLegalizer())
  1431. DCI.AddToWorklist(ZextOp.getNode());
  1432. // Otherwise, make this a use of a zext.
  1433. return DAG.getSetCC(dl, VT, ZextOp,
  1434. DAG.getConstant(C1 & APInt::getLowBitsSet(
  1435. ExtDstTyBits,
  1436. ExtSrcTyBits),
  1437. dl, ExtDstTy),
  1438. Cond);
  1439. } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
  1440. (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
  1441. // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
  1442. if (N0.getOpcode() == ISD::SETCC &&
  1443. isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
  1444. bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
  1445. if (TrueWhenTrue)
  1446. return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
  1447. // Invert the condition.
  1448. ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
  1449. CC = ISD::getSetCCInverse(CC,
  1450. N0.getOperand(0).getValueType().isInteger());
  1451. if (DCI.isBeforeLegalizeOps() ||
  1452. isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType()))
  1453. return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
  1454. }
  1455. if ((N0.getOpcode() == ISD::XOR ||
  1456. (N0.getOpcode() == ISD::AND &&
  1457. N0.getOperand(0).getOpcode() == ISD::XOR &&
  1458. N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
  1459. isa<ConstantSDNode>(N0.getOperand(1)) &&
  1460. cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
  1461. // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
  1462. // can only do this if the top bits are known zero.
  1463. unsigned BitWidth = N0.getValueSizeInBits();
  1464. if (DAG.MaskedValueIsZero(N0,
  1465. APInt::getHighBitsSet(BitWidth,
  1466. BitWidth-1))) {
  1467. // Okay, get the un-inverted input value.
  1468. SDValue Val;
  1469. if (N0.getOpcode() == ISD::XOR)
  1470. Val = N0.getOperand(0);
  1471. else {
  1472. assert(N0.getOpcode() == ISD::AND &&
  1473. N0.getOperand(0).getOpcode() == ISD::XOR);
  1474. // ((X^1)&1)^1 -> X & 1
  1475. Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
  1476. N0.getOperand(0).getOperand(0),
  1477. N0.getOperand(1));
  1478. }
  1479. return DAG.getSetCC(dl, VT, Val, N1,
  1480. Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
  1481. }
  1482. } else if (N1C->getAPIntValue() == 1 &&
  1483. (VT == MVT::i1 ||
  1484. getBooleanContents(N0->getValueType(0)) ==
  1485. ZeroOrOneBooleanContent)) {
  1486. SDValue Op0 = N0;
  1487. if (Op0.getOpcode() == ISD::TRUNCATE)
  1488. Op0 = Op0.getOperand(0);
  1489. if ((Op0.getOpcode() == ISD::XOR) &&
  1490. Op0.getOperand(0).getOpcode() == ISD::SETCC &&
  1491. Op0.getOperand(1).getOpcode() == ISD::SETCC) {
  1492. // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
  1493. Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
  1494. return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
  1495. Cond);
  1496. }
  1497. if (Op0.getOpcode() == ISD::AND &&
  1498. isa<ConstantSDNode>(Op0.getOperand(1)) &&
  1499. cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
  1500. // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
  1501. if (Op0.getValueType().bitsGT(VT))
  1502. Op0 = DAG.getNode(ISD::AND, dl, VT,
  1503. DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
  1504. DAG.getConstant(1, dl, VT));
  1505. else if (Op0.getValueType().bitsLT(VT))
  1506. Op0 = DAG.getNode(ISD::AND, dl, VT,
  1507. DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
  1508. DAG.getConstant(1, dl, VT));
  1509. return DAG.getSetCC(dl, VT, Op0,
  1510. DAG.getConstant(0, dl, Op0.getValueType()),
  1511. Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
  1512. }
  1513. if (Op0.getOpcode() == ISD::AssertZext &&
  1514. cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
  1515. return DAG.getSetCC(dl, VT, Op0,
  1516. DAG.getConstant(0, dl, Op0.getValueType()),
  1517. Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
  1518. }
  1519. }
  1520. APInt MinVal, MaxVal;
  1521. unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
  1522. if (ISD::isSignedIntSetCC(Cond)) {
  1523. MinVal = APInt::getSignedMinValue(OperandBitSize);
  1524. MaxVal = APInt::getSignedMaxValue(OperandBitSize);
  1525. } else {
  1526. MinVal = APInt::getMinValue(OperandBitSize);
  1527. MaxVal = APInt::getMaxValue(OperandBitSize);
  1528. }
  1529. // Canonicalize GE/LE comparisons to use GT/LT comparisons.
  1530. if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
  1531. if (C1 == MinVal) return DAG.getConstant(1, dl, VT); // X >= MIN --> true
  1532. // X >= C0 --> X > (C0 - 1)
  1533. APInt C = C1 - 1;
  1534. ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
  1535. if ((DCI.isBeforeLegalizeOps() ||
  1536. isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
  1537. (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
  1538. isLegalICmpImmediate(C.getSExtValue())))) {
  1539. return DAG.getSetCC(dl, VT, N0,
  1540. DAG.getConstant(C, dl, N1.getValueType()),
  1541. NewCC);
  1542. }
  1543. }
  1544. if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
  1545. if (C1 == MaxVal) return DAG.getConstant(1, dl, VT); // X <= MAX --> true
  1546. // X <= C0 --> X < (C0 + 1)
  1547. APInt C = C1 + 1;
  1548. ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
  1549. if ((DCI.isBeforeLegalizeOps() ||
  1550. isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
  1551. (!N1C->isOpaque() || (N1C->isOpaque() && C.getBitWidth() <= 64 &&
  1552. isLegalICmpImmediate(C.getSExtValue())))) {
  1553. return DAG.getSetCC(dl, VT, N0,
  1554. DAG.getConstant(C, dl, N1.getValueType()),
  1555. NewCC);
  1556. }
  1557. }
  1558. if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
  1559. return DAG.getConstant(0, dl, VT); // X < MIN --> false
  1560. if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
  1561. return DAG.getConstant(1, dl, VT); // X >= MIN --> true
  1562. if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
  1563. return DAG.getConstant(0, dl, VT); // X > MAX --> false
  1564. if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
  1565. return DAG.getConstant(1, dl, VT); // X <= MAX --> true
  1566. // Canonicalize setgt X, Min --> setne X, Min
  1567. if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
  1568. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
  1569. // Canonicalize setlt X, Max --> setne X, Max
  1570. if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
  1571. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
  1572. // If we have setult X, 1, turn it into seteq X, 0
  1573. if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
  1574. return DAG.getSetCC(dl, VT, N0,
  1575. DAG.getConstant(MinVal, dl, N0.getValueType()),
  1576. ISD::SETEQ);
  1577. // If we have setugt X, Max-1, turn it into seteq X, Max
  1578. if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
  1579. return DAG.getSetCC(dl, VT, N0,
  1580. DAG.getConstant(MaxVal, dl, N0.getValueType()),
  1581. ISD::SETEQ);
  1582. // If we have "setcc X, C0", check to see if we can shrink the immediate
  1583. // by changing cc.
  1584. // SETUGT X, SINTMAX -> SETLT X, 0
  1585. if (Cond == ISD::SETUGT &&
  1586. C1 == APInt::getSignedMaxValue(OperandBitSize))
  1587. return DAG.getSetCC(dl, VT, N0,
  1588. DAG.getConstant(0, dl, N1.getValueType()),
  1589. ISD::SETLT);
  1590. // SETULT X, SINTMIN -> SETGT X, -1
  1591. if (Cond == ISD::SETULT &&
  1592. C1 == APInt::getSignedMinValue(OperandBitSize)) {
  1593. SDValue ConstMinusOne =
  1594. DAG.getConstant(APInt::getAllOnesValue(OperandBitSize), dl,
  1595. N1.getValueType());
  1596. return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
  1597. }
  1598. // Fold bit comparisons when we can.
  1599. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  1600. (VT == N0.getValueType() ||
  1601. (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
  1602. N0.getOpcode() == ISD::AND) {
  1603. auto &DL = DAG.getDataLayout();
  1604. if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
  1605. EVT ShiftTy = DCI.isBeforeLegalize()
  1606. ? getPointerTy(DL)
  1607. : getShiftAmountTy(N0.getValueType(), DL);
  1608. if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
  1609. // Perform the xform if the AND RHS is a single bit.
  1610. if (AndRHS->getAPIntValue().isPowerOf2()) {
  1611. return DAG.getNode(ISD::TRUNCATE, dl, VT,
  1612. DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
  1613. DAG.getConstant(AndRHS->getAPIntValue().logBase2(), dl,
  1614. ShiftTy)));
  1615. }
  1616. } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
  1617. // (X & 8) == 8 --> (X & 8) >> 3
  1618. // Perform the xform if C1 is a single bit.
  1619. if (C1.isPowerOf2()) {
  1620. return DAG.getNode(ISD::TRUNCATE, dl, VT,
  1621. DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
  1622. DAG.getConstant(C1.logBase2(), dl,
  1623. ShiftTy)));
  1624. }
  1625. }
  1626. }
  1627. }
  1628. if (C1.getMinSignedBits() <= 64 &&
  1629. !isLegalICmpImmediate(C1.getSExtValue())) {
  1630. // (X & -256) == 256 -> (X >> 8) == 1
  1631. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  1632. N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
  1633. if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
  1634. const APInt &AndRHSC = AndRHS->getAPIntValue();
  1635. if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
  1636. unsigned ShiftBits = AndRHSC.countTrailingZeros();
  1637. auto &DL = DAG.getDataLayout();
  1638. EVT ShiftTy = DCI.isBeforeLegalize()
  1639. ? getPointerTy(DL)
  1640. : getShiftAmountTy(N0.getValueType(), DL);
  1641. EVT CmpTy = N0.getValueType();
  1642. SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
  1643. DAG.getConstant(ShiftBits, dl,
  1644. ShiftTy));
  1645. SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, CmpTy);
  1646. return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
  1647. }
  1648. }
  1649. } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
  1650. Cond == ISD::SETULE || Cond == ISD::SETUGT) {
  1651. bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
  1652. // X < 0x100000000 -> (X >> 32) < 1
  1653. // X >= 0x100000000 -> (X >> 32) >= 1
  1654. // X <= 0x0ffffffff -> (X >> 32) < 1
  1655. // X > 0x0ffffffff -> (X >> 32) >= 1
  1656. unsigned ShiftBits;
  1657. APInt NewC = C1;
  1658. ISD::CondCode NewCond = Cond;
  1659. if (AdjOne) {
  1660. ShiftBits = C1.countTrailingOnes();
  1661. NewC = NewC + 1;
  1662. NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
  1663. } else {
  1664. ShiftBits = C1.countTrailingZeros();
  1665. }
  1666. NewC = NewC.lshr(ShiftBits);
  1667. if (ShiftBits && NewC.getMinSignedBits() <= 64 &&
  1668. isLegalICmpImmediate(NewC.getSExtValue())) {
  1669. auto &DL = DAG.getDataLayout();
  1670. EVT ShiftTy = DCI.isBeforeLegalize()
  1671. ? getPointerTy(DL)
  1672. : getShiftAmountTy(N0.getValueType(), DL);
  1673. EVT CmpTy = N0.getValueType();
  1674. SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
  1675. DAG.getConstant(ShiftBits, dl, ShiftTy));
  1676. SDValue CmpRHS = DAG.getConstant(NewC, dl, CmpTy);
  1677. return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
  1678. }
  1679. }
  1680. }
  1681. }
  1682. if (isa<ConstantFPSDNode>(N0.getNode())) {
  1683. // Constant fold or commute setcc.
  1684. SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
  1685. if (O.getNode()) return O;
  1686. } else if (auto *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
  1687. // If the RHS of an FP comparison is a constant, simplify it away in
  1688. // some cases.
  1689. if (CFP->getValueAPF().isNaN()) {
  1690. // If an operand is known to be a nan, we can fold it.
  1691. switch (ISD::getUnorderedFlavor(Cond)) {
  1692. default: llvm_unreachable("Unknown flavor!");
  1693. case 0: // Known false.
  1694. return DAG.getConstant(0, dl, VT);
  1695. case 1: // Known true.
  1696. return DAG.getConstant(1, dl, VT);
  1697. case 2: // Undefined.
  1698. return DAG.getUNDEF(VT);
  1699. }
  1700. }
  1701. // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
  1702. // constant if knowing that the operand is non-nan is enough. We prefer to
  1703. // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
  1704. // materialize 0.0.
  1705. if (Cond == ISD::SETO || Cond == ISD::SETUO)
  1706. return DAG.getSetCC(dl, VT, N0, N0, Cond);
  1707. // If the condition is not legal, see if we can find an equivalent one
  1708. // which is legal.
  1709. if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
  1710. // If the comparison was an awkward floating-point == or != and one of
  1711. // the comparison operands is infinity or negative infinity, convert the
  1712. // condition to a less-awkward <= or >=.
  1713. if (CFP->getValueAPF().isInfinity()) {
  1714. if (CFP->getValueAPF().isNegative()) {
  1715. if (Cond == ISD::SETOEQ &&
  1716. isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
  1717. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
  1718. if (Cond == ISD::SETUEQ &&
  1719. isCondCodeLegal(ISD::SETOLE, N0.getSimpleValueType()))
  1720. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
  1721. if (Cond == ISD::SETUNE &&
  1722. isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
  1723. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
  1724. if (Cond == ISD::SETONE &&
  1725. isCondCodeLegal(ISD::SETUGT, N0.getSimpleValueType()))
  1726. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
  1727. } else {
  1728. if (Cond == ISD::SETOEQ &&
  1729. isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
  1730. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
  1731. if (Cond == ISD::SETUEQ &&
  1732. isCondCodeLegal(ISD::SETOGE, N0.getSimpleValueType()))
  1733. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
  1734. if (Cond == ISD::SETUNE &&
  1735. isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
  1736. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
  1737. if (Cond == ISD::SETONE &&
  1738. isCondCodeLegal(ISD::SETULT, N0.getSimpleValueType()))
  1739. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
  1740. }
  1741. }
  1742. }
  1743. }
  1744. if (N0 == N1) {
  1745. // The sext(setcc()) => setcc() optimization relies on the appropriate
  1746. // constant being emitted.
  1747. uint64_t EqVal = 0;
  1748. switch (getBooleanContents(N0.getValueType())) {
  1749. case UndefinedBooleanContent:
  1750. case ZeroOrOneBooleanContent:
  1751. EqVal = ISD::isTrueWhenEqual(Cond);
  1752. break;
  1753. case ZeroOrNegativeOneBooleanContent:
  1754. EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
  1755. break;
  1756. }
  1757. // We can always fold X == X for integer setcc's.
  1758. if (N0.getValueType().isInteger()) {
  1759. return DAG.getConstant(EqVal, dl, VT);
  1760. }
  1761. unsigned UOF = ISD::getUnorderedFlavor(Cond);
  1762. if (UOF == 2) // FP operators that are undefined on NaNs.
  1763. return DAG.getConstant(EqVal, dl, VT);
  1764. if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
  1765. return DAG.getConstant(EqVal, dl, VT);
  1766. // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
  1767. // if it is not already.
  1768. ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
  1769. if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
  1770. getCondCodeAction(NewCond, N0.getSimpleValueType()) == Legal))
  1771. return DAG.getSetCC(dl, VT, N0, N1, NewCond);
  1772. }
  1773. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  1774. N0.getValueType().isInteger()) {
  1775. if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
  1776. N0.getOpcode() == ISD::XOR) {
  1777. // Simplify (X+Y) == (X+Z) --> Y == Z
  1778. if (N0.getOpcode() == N1.getOpcode()) {
  1779. if (N0.getOperand(0) == N1.getOperand(0))
  1780. return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
  1781. if (N0.getOperand(1) == N1.getOperand(1))
  1782. return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
  1783. if (DAG.isCommutativeBinOp(N0.getOpcode())) {
  1784. // If X op Y == Y op X, try other combinations.
  1785. if (N0.getOperand(0) == N1.getOperand(1))
  1786. return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
  1787. Cond);
  1788. if (N0.getOperand(1) == N1.getOperand(0))
  1789. return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
  1790. Cond);
  1791. }
  1792. }
  1793. // If RHS is a legal immediate value for a compare instruction, we need
  1794. // to be careful about increasing register pressure needlessly.
  1795. bool LegalRHSImm = false;
  1796. if (auto *RHSC = dyn_cast<ConstantSDNode>(N1)) {
  1797. if (auto *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
  1798. // Turn (X+C1) == C2 --> X == C2-C1
  1799. if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
  1800. return DAG.getSetCC(dl, VT, N0.getOperand(0),
  1801. DAG.getConstant(RHSC->getAPIntValue()-
  1802. LHSR->getAPIntValue(),
  1803. dl, N0.getValueType()), Cond);
  1804. }
  1805. // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
  1806. if (N0.getOpcode() == ISD::XOR)
  1807. // If we know that all of the inverted bits are zero, don't bother
  1808. // performing the inversion.
  1809. if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
  1810. return
  1811. DAG.getSetCC(dl, VT, N0.getOperand(0),
  1812. DAG.getConstant(LHSR->getAPIntValue() ^
  1813. RHSC->getAPIntValue(),
  1814. dl, N0.getValueType()),
  1815. Cond);
  1816. }
  1817. // Turn (C1-X) == C2 --> X == C1-C2
  1818. if (auto *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
  1819. if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
  1820. return
  1821. DAG.getSetCC(dl, VT, N0.getOperand(1),
  1822. DAG.getConstant(SUBC->getAPIntValue() -
  1823. RHSC->getAPIntValue(),
  1824. dl, N0.getValueType()),
  1825. Cond);
  1826. }
  1827. }
  1828. // Could RHSC fold directly into a compare?
  1829. if (RHSC->getValueType(0).getSizeInBits() <= 64)
  1830. LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
  1831. }
  1832. // Simplify (X+Z) == X --> Z == 0
  1833. // Don't do this if X is an immediate that can fold into a cmp
  1834. // instruction and X+Z has other uses. It could be an induction variable
  1835. // chain, and the transform would increase register pressure.
  1836. if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
  1837. if (N0.getOperand(0) == N1)
  1838. return DAG.getSetCC(dl, VT, N0.getOperand(1),
  1839. DAG.getConstant(0, dl, N0.getValueType()), Cond);
  1840. if (N0.getOperand(1) == N1) {
  1841. if (DAG.isCommutativeBinOp(N0.getOpcode()))
  1842. return DAG.getSetCC(dl, VT, N0.getOperand(0),
  1843. DAG.getConstant(0, dl, N0.getValueType()),
  1844. Cond);
  1845. if (N0.getNode()->hasOneUse()) {
  1846. assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
  1847. auto &DL = DAG.getDataLayout();
  1848. // (Z-X) == X --> Z == X<<1
  1849. SDValue SH = DAG.getNode(
  1850. ISD::SHL, dl, N1.getValueType(), N1,
  1851. DAG.getConstant(1, dl,
  1852. getShiftAmountTy(N1.getValueType(), DL)));
  1853. if (!DCI.isCalledByLegalizer())
  1854. DCI.AddToWorklist(SH.getNode());
  1855. return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
  1856. }
  1857. }
  1858. }
  1859. }
  1860. if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
  1861. N1.getOpcode() == ISD::XOR) {
  1862. // Simplify X == (X+Z) --> Z == 0
  1863. if (N1.getOperand(0) == N0)
  1864. return DAG.getSetCC(dl, VT, N1.getOperand(1),
  1865. DAG.getConstant(0, dl, N1.getValueType()), Cond);
  1866. if (N1.getOperand(1) == N0) {
  1867. if (DAG.isCommutativeBinOp(N1.getOpcode()))
  1868. return DAG.getSetCC(dl, VT, N1.getOperand(0),
  1869. DAG.getConstant(0, dl, N1.getValueType()), Cond);
  1870. if (N1.getNode()->hasOneUse()) {
  1871. assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
  1872. auto &DL = DAG.getDataLayout();
  1873. // X == (Z-X) --> X<<1 == Z
  1874. SDValue SH = DAG.getNode(
  1875. ISD::SHL, dl, N1.getValueType(), N0,
  1876. DAG.getConstant(1, dl, getShiftAmountTy(N0.getValueType(), DL)));
  1877. if (!DCI.isCalledByLegalizer())
  1878. DCI.AddToWorklist(SH.getNode());
  1879. return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
  1880. }
  1881. }
  1882. }
  1883. // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
  1884. // Note that where y is variable and is known to have at most
  1885. // one bit set (for example, if it is z&1) we cannot do this;
  1886. // the expressions are not equivalent when y==0.
  1887. if (N0.getOpcode() == ISD::AND)
  1888. if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
  1889. if (ValueHasExactlyOneBitSet(N1, DAG)) {
  1890. Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
  1891. if (DCI.isBeforeLegalizeOps() ||
  1892. isCondCodeLegal(Cond, N0.getSimpleValueType())) {
  1893. SDValue Zero = DAG.getConstant(0, dl, N1.getValueType());
  1894. return DAG.getSetCC(dl, VT, N0, Zero, Cond);
  1895. }
  1896. }
  1897. }
  1898. if (N1.getOpcode() == ISD::AND)
  1899. if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
  1900. if (ValueHasExactlyOneBitSet(N0, DAG)) {
  1901. Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
  1902. if (DCI.isBeforeLegalizeOps() ||
  1903. isCondCodeLegal(Cond, N1.getSimpleValueType())) {
  1904. SDValue Zero = DAG.getConstant(0, dl, N0.getValueType());
  1905. return DAG.getSetCC(dl, VT, N1, Zero, Cond);
  1906. }
  1907. }
  1908. }
  1909. }
  1910. // Fold away ALL boolean setcc's.
  1911. SDValue Temp;
  1912. if (N0.getValueType() == MVT::i1 && foldBooleans) {
  1913. switch (Cond) {
  1914. default: llvm_unreachable("Unknown integer setcc!");
  1915. case ISD::SETEQ: // X == Y -> ~(X^Y)
  1916. Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
  1917. N0 = DAG.getNOT(dl, Temp, MVT::i1);
  1918. if (!DCI.isCalledByLegalizer())
  1919. DCI.AddToWorklist(Temp.getNode());
  1920. break;
  1921. case ISD::SETNE: // X != Y --> (X^Y)
  1922. N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
  1923. break;
  1924. case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
  1925. case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
  1926. Temp = DAG.getNOT(dl, N0, MVT::i1);
  1927. N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
  1928. if (!DCI.isCalledByLegalizer())
  1929. DCI.AddToWorklist(Temp.getNode());
  1930. break;
  1931. case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
  1932. case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
  1933. Temp = DAG.getNOT(dl, N1, MVT::i1);
  1934. N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
  1935. if (!DCI.isCalledByLegalizer())
  1936. DCI.AddToWorklist(Temp.getNode());
  1937. break;
  1938. case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
  1939. case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
  1940. Temp = DAG.getNOT(dl, N0, MVT::i1);
  1941. N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
  1942. if (!DCI.isCalledByLegalizer())
  1943. DCI.AddToWorklist(Temp.getNode());
  1944. break;
  1945. case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
  1946. case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
  1947. Temp = DAG.getNOT(dl, N1, MVT::i1);
  1948. N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
  1949. break;
  1950. }
  1951. if (VT != MVT::i1) {
  1952. if (!DCI.isCalledByLegalizer())
  1953. DCI.AddToWorklist(N0.getNode());
  1954. // FIXME: If running after legalize, we probably can't do this.
  1955. N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
  1956. }
  1957. return N0;
  1958. }
  1959. // Could not fold it.
  1960. return SDValue();
  1961. }
  1962. /// Returns true (and the GlobalValue and the offset) if the node is a
  1963. /// GlobalAddress + offset.
  1964. bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
  1965. int64_t &Offset) const {
  1966. if (auto *GASD = dyn_cast<GlobalAddressSDNode>(N)) {
  1967. GA = GASD->getGlobal();
  1968. Offset += GASD->getOffset();
  1969. return true;
  1970. }
  1971. if (N->getOpcode() == ISD::ADD) {
  1972. SDValue N1 = N->getOperand(0);
  1973. SDValue N2 = N->getOperand(1);
  1974. if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
  1975. if (auto *V = dyn_cast<ConstantSDNode>(N2)) {
  1976. Offset += V->getSExtValue();
  1977. return true;
  1978. }
  1979. } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
  1980. if (auto *V = dyn_cast<ConstantSDNode>(N1)) {
  1981. Offset += V->getSExtValue();
  1982. return true;
  1983. }
  1984. }
  1985. }
  1986. return false;
  1987. }
  1988. SDValue TargetLowering::PerformDAGCombine(SDNode *N,
  1989. DAGCombinerInfo &DCI) const {
  1990. // Default implementation: no optimization.
  1991. return SDValue();
  1992. }
  1993. //===----------------------------------------------------------------------===//
  1994. // Inline Assembler Implementation Methods
  1995. //===----------------------------------------------------------------------===//
  1996. TargetLowering::ConstraintType
  1997. TargetLowering::getConstraintType(StringRef Constraint) const {
  1998. unsigned S = Constraint.size();
  1999. if (S == 1) {
  2000. switch (Constraint[0]) {
  2001. default: break;
  2002. case 'r': return C_RegisterClass;
  2003. case 'm': // memory
  2004. case 'o': // offsetable
  2005. case 'V': // not offsetable
  2006. return C_Memory;
  2007. case 'i': // Simple Integer or Relocatable Constant
  2008. case 'n': // Simple Integer
  2009. case 'E': // Floating Point Constant
  2010. case 'F': // Floating Point Constant
  2011. case 's': // Relocatable Constant
  2012. case 'p': // Address.
  2013. case 'X': // Allow ANY value.
  2014. case 'I': // Target registers.
  2015. case 'J':
  2016. case 'K':
  2017. case 'L':
  2018. case 'M':
  2019. case 'N':
  2020. case 'O':
  2021. case 'P':
  2022. case '<':
  2023. case '>':
  2024. return C_Other;
  2025. }
  2026. }
  2027. if (S > 1 && Constraint[0] == '{' && Constraint[S-1] == '}') {
  2028. if (S == 8 && Constraint.substr(1, 6) == "memory") // "{memory}"
  2029. return C_Memory;
  2030. return C_Register;
  2031. }
  2032. return C_Unknown;
  2033. }
  2034. /// Try to replace an X constraint, which matches anything, with another that
  2035. /// has more specific requirements based on the type of the corresponding
  2036. /// operand.
  2037. const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
  2038. if (ConstraintVT.isInteger())
  2039. return "r";
  2040. if (ConstraintVT.isFloatingPoint())
  2041. return "f"; // works for many targets
  2042. return nullptr;
  2043. }
  2044. /// Lower the specified operand into the Ops vector.
  2045. /// If it is invalid, don't add anything to Ops.
  2046. void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
  2047. std::string &Constraint,
  2048. std::vector<SDValue> &Ops,
  2049. SelectionDAG &DAG) const {
  2050. if (Constraint.length() > 1) return;
  2051. char ConstraintLetter = Constraint[0];
  2052. switch (ConstraintLetter) {
  2053. default: break;
  2054. case 'X': // Allows any operand; labels (basic block) use this.
  2055. if (Op.getOpcode() == ISD::BasicBlock) {
  2056. Ops.push_back(Op);
  2057. return;
  2058. }
  2059. // fall through
  2060. case 'i': // Simple Integer or Relocatable Constant
  2061. case 'n': // Simple Integer
  2062. case 's': { // Relocatable Constant
  2063. // These operands are interested in values of the form (GV+C), where C may
  2064. // be folded in as an offset of GV, or it may be explicitly added. Also, it
  2065. // is possible and fine if either GV or C are missing.
  2066. ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
  2067. GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
  2068. // If we have "(add GV, C)", pull out GV/C
  2069. if (Op.getOpcode() == ISD::ADD) {
  2070. C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
  2071. GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
  2072. if (!C || !GA) {
  2073. C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
  2074. GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
  2075. }
  2076. if (!C || !GA) {
  2077. C = nullptr;
  2078. GA = nullptr;
  2079. }
  2080. }
  2081. // If we find a valid operand, map to the TargetXXX version so that the
  2082. // value itself doesn't get selected.
  2083. if (GA) { // Either &GV or &GV+C
  2084. if (ConstraintLetter != 'n') {
  2085. int64_t Offs = GA->getOffset();
  2086. if (C) Offs += C->getZExtValue();
  2087. Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
  2088. C ? SDLoc(C) : SDLoc(),
  2089. Op.getValueType(), Offs));
  2090. }
  2091. return;
  2092. }
  2093. if (C) { // just C, no GV.
  2094. // Simple constants are not allowed for 's'.
  2095. if (ConstraintLetter != 's') {
  2096. // gcc prints these as sign extended. Sign extend value to 64 bits
  2097. // now; without this it would get ZExt'd later in
  2098. // ScheduleDAGSDNodes::EmitNode, which is very generic.
  2099. Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
  2100. SDLoc(C), MVT::i64));
  2101. }
  2102. return;
  2103. }
  2104. break;
  2105. }
  2106. }
  2107. }
  2108. std::pair<unsigned, const TargetRegisterClass *>
  2109. TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *RI,
  2110. StringRef Constraint,
  2111. MVT VT) const {
  2112. if (Constraint.empty() || Constraint[0] != '{')
  2113. return std::make_pair(0u, static_cast<TargetRegisterClass*>(nullptr));
  2114. assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
  2115. // Remove the braces from around the name.
  2116. StringRef RegName(Constraint.data()+1, Constraint.size()-2);
  2117. std::pair<unsigned, const TargetRegisterClass*> R =
  2118. std::make_pair(0u, static_cast<const TargetRegisterClass*>(nullptr));
  2119. // Figure out which register class contains this reg.
  2120. for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
  2121. E = RI->regclass_end(); RCI != E; ++RCI) {
  2122. const TargetRegisterClass *RC = *RCI;
  2123. // If none of the value types for this register class are valid, we
  2124. // can't use it. For example, 64-bit reg classes on 32-bit targets.
  2125. if (!isLegalRC(RC))
  2126. continue;
  2127. for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
  2128. I != E; ++I) {
  2129. if (RegName.equals_lower(RI->getName(*I))) {
  2130. std::pair<unsigned, const TargetRegisterClass*> S =
  2131. std::make_pair(*I, RC);
  2132. // If this register class has the requested value type, return it,
  2133. // otherwise keep searching and return the first class found
  2134. // if no other is found which explicitly has the requested type.
  2135. if (RC->hasType(VT))
  2136. return S;
  2137. else if (!R.second)
  2138. R = S;
  2139. }
  2140. }
  2141. }
  2142. return R;
  2143. }
  2144. //===----------------------------------------------------------------------===//
  2145. // Constraint Selection.
  2146. /// Return true of this is an input operand that is a matching constraint like
  2147. /// "4".
  2148. bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
  2149. assert(!ConstraintCode.empty() && "No known constraint!");
  2150. return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
  2151. }
  2152. /// If this is an input matching constraint, this method returns the output
  2153. /// operand it matches.
  2154. unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
  2155. assert(!ConstraintCode.empty() && "No known constraint!");
  2156. return atoi(ConstraintCode.c_str());
  2157. }
  2158. /// Split up the constraint string from the inline assembly value into the
  2159. /// specific constraints and their prefixes, and also tie in the associated
  2160. /// operand values.
  2161. /// If this returns an empty vector, and if the constraint string itself
  2162. /// isn't empty, there was an error parsing.
  2163. TargetLowering::AsmOperandInfoVector
  2164. TargetLowering::ParseConstraints(const DataLayout &DL,
  2165. const TargetRegisterInfo *TRI,
  2166. ImmutableCallSite CS) const {
  2167. /// Information about all of the constraints.
  2168. AsmOperandInfoVector ConstraintOperands;
  2169. const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
  2170. unsigned maCount = 0; // Largest number of multiple alternative constraints.
  2171. // Do a prepass over the constraints, canonicalizing them, and building up the
  2172. // ConstraintOperands list.
  2173. unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
  2174. unsigned ResNo = 0; // ResNo - The result number of the next output.
  2175. for (InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
  2176. ConstraintOperands.emplace_back(std::move(CI));
  2177. AsmOperandInfo &OpInfo = ConstraintOperands.back();
  2178. // Update multiple alternative constraint count.
  2179. if (OpInfo.multipleAlternatives.size() > maCount)
  2180. maCount = OpInfo.multipleAlternatives.size();
  2181. OpInfo.ConstraintVT = MVT::Other;
  2182. // Compute the value type for each operand.
  2183. switch (OpInfo.Type) {
  2184. case InlineAsm::isOutput:
  2185. // Indirect outputs just consume an argument.
  2186. if (OpInfo.isIndirect) {
  2187. OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
  2188. break;
  2189. }
  2190. // The return value of the call is this value. As such, there is no
  2191. // corresponding argument.
  2192. assert(!CS.getType()->isVoidTy() &&
  2193. "Bad inline asm!");
  2194. if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
  2195. OpInfo.ConstraintVT =
  2196. getSimpleValueType(DL, STy->getElementType(ResNo));
  2197. } else {
  2198. assert(ResNo == 0 && "Asm only has one result!");
  2199. OpInfo.ConstraintVT = getSimpleValueType(DL, CS.getType());
  2200. }
  2201. ++ResNo;
  2202. break;
  2203. case InlineAsm::isInput:
  2204. OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
  2205. break;
  2206. case InlineAsm::isClobber:
  2207. // Nothing to do.
  2208. break;
  2209. }
  2210. if (OpInfo.CallOperandVal) {
  2211. llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
  2212. if (OpInfo.isIndirect) {
  2213. llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
  2214. if (!PtrTy)
  2215. report_fatal_error("Indirect operand for inline asm not a pointer!");
  2216. OpTy = PtrTy->getElementType();
  2217. }
  2218. // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
  2219. if (StructType *STy = dyn_cast<StructType>(OpTy))
  2220. if (STy->getNumElements() == 1)
  2221. OpTy = STy->getElementType(0);
  2222. // If OpTy is not a single value, it may be a struct/union that we
  2223. // can tile with integers.
  2224. if (!OpTy->isSingleValueType() && OpTy->isSized()) {
  2225. unsigned BitSize = DL.getTypeSizeInBits(OpTy);
  2226. switch (BitSize) {
  2227. default: break;
  2228. case 1:
  2229. case 8:
  2230. case 16:
  2231. case 32:
  2232. case 64:
  2233. case 128:
  2234. OpInfo.ConstraintVT =
  2235. MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
  2236. break;
  2237. }
  2238. } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
  2239. unsigned PtrSize = DL.getPointerSizeInBits(PT->getAddressSpace());
  2240. OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize);
  2241. } else {
  2242. OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
  2243. }
  2244. }
  2245. }
  2246. // If we have multiple alternative constraints, select the best alternative.
  2247. if (!ConstraintOperands.empty()) {
  2248. if (maCount) {
  2249. unsigned bestMAIndex = 0;
  2250. int bestWeight = -1;
  2251. // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
  2252. int weight = -1;
  2253. unsigned maIndex;
  2254. // Compute the sums of the weights for each alternative, keeping track
  2255. // of the best (highest weight) one so far.
  2256. for (maIndex = 0; maIndex < maCount; ++maIndex) {
  2257. int weightSum = 0;
  2258. for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
  2259. cIndex != eIndex; ++cIndex) {
  2260. AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
  2261. if (OpInfo.Type == InlineAsm::isClobber)
  2262. continue;
  2263. // If this is an output operand with a matching input operand,
  2264. // look up the matching input. If their types mismatch, e.g. one
  2265. // is an integer, the other is floating point, or their sizes are
  2266. // different, flag it as an maCantMatch.
  2267. if (OpInfo.hasMatchingInput()) {
  2268. AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
  2269. if (OpInfo.ConstraintVT != Input.ConstraintVT) {
  2270. if ((OpInfo.ConstraintVT.isInteger() !=
  2271. Input.ConstraintVT.isInteger()) ||
  2272. (OpInfo.ConstraintVT.getSizeInBits() !=
  2273. Input.ConstraintVT.getSizeInBits())) {
  2274. weightSum = -1; // Can't match.
  2275. break;
  2276. }
  2277. }
  2278. }
  2279. weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
  2280. if (weight == -1) {
  2281. weightSum = -1;
  2282. break;
  2283. }
  2284. weightSum += weight;
  2285. }
  2286. // Update best.
  2287. if (weightSum > bestWeight) {
  2288. bestWeight = weightSum;
  2289. bestMAIndex = maIndex;
  2290. }
  2291. }
  2292. // Now select chosen alternative in each constraint.
  2293. for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
  2294. cIndex != eIndex; ++cIndex) {
  2295. AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
  2296. if (cInfo.Type == InlineAsm::isClobber)
  2297. continue;
  2298. cInfo.selectAlternative(bestMAIndex);
  2299. }
  2300. }
  2301. }
  2302. // Check and hook up tied operands, choose constraint code to use.
  2303. for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
  2304. cIndex != eIndex; ++cIndex) {
  2305. AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
  2306. // If this is an output operand with a matching input operand, look up the
  2307. // matching input. If their types mismatch, e.g. one is an integer, the
  2308. // other is floating point, or their sizes are different, flag it as an
  2309. // error.
  2310. if (OpInfo.hasMatchingInput()) {
  2311. AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
  2312. if (OpInfo.ConstraintVT != Input.ConstraintVT) {
  2313. std::pair<unsigned, const TargetRegisterClass *> MatchRC =
  2314. getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
  2315. OpInfo.ConstraintVT);
  2316. std::pair<unsigned, const TargetRegisterClass *> InputRC =
  2317. getRegForInlineAsmConstraint(TRI, Input.ConstraintCode,
  2318. Input.ConstraintVT);
  2319. if ((OpInfo.ConstraintVT.isInteger() !=
  2320. Input.ConstraintVT.isInteger()) ||
  2321. (MatchRC.second != InputRC.second)) {
  2322. report_fatal_error("Unsupported asm: input constraint"
  2323. " with a matching output constraint of"
  2324. " incompatible type!");
  2325. }
  2326. }
  2327. }
  2328. }
  2329. return ConstraintOperands;
  2330. }
  2331. /// Return an integer indicating how general CT is.
  2332. static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
  2333. switch (CT) {
  2334. case TargetLowering::C_Other:
  2335. case TargetLowering::C_Unknown:
  2336. return 0;
  2337. case TargetLowering::C_Register:
  2338. return 1;
  2339. case TargetLowering::C_RegisterClass:
  2340. return 2;
  2341. case TargetLowering::C_Memory:
  2342. return 3;
  2343. }
  2344. llvm_unreachable("Invalid constraint type");
  2345. }
  2346. /// Examine constraint type and operand type and determine a weight value.
  2347. /// This object must already have been set up with the operand type
  2348. /// and the current alternative constraint selected.
  2349. TargetLowering::ConstraintWeight
  2350. TargetLowering::getMultipleConstraintMatchWeight(
  2351. AsmOperandInfo &info, int maIndex) const {
  2352. InlineAsm::ConstraintCodeVector *rCodes;
  2353. if (maIndex >= (int)info.multipleAlternatives.size())
  2354. rCodes = &info.Codes;
  2355. else
  2356. rCodes = &info.multipleAlternatives[maIndex].Codes;
  2357. ConstraintWeight BestWeight = CW_Invalid;
  2358. // Loop over the options, keeping track of the most general one.
  2359. for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
  2360. ConstraintWeight weight =
  2361. getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
  2362. if (weight > BestWeight)
  2363. BestWeight = weight;
  2364. }
  2365. return BestWeight;
  2366. }
  2367. /// Examine constraint type and operand type and determine a weight value.
  2368. /// This object must already have been set up with the operand type
  2369. /// and the current alternative constraint selected.
  2370. TargetLowering::ConstraintWeight
  2371. TargetLowering::getSingleConstraintMatchWeight(
  2372. AsmOperandInfo &info, const char *constraint) const {
  2373. ConstraintWeight weight = CW_Invalid;
  2374. Value *CallOperandVal = info.CallOperandVal;
  2375. // If we don't have a value, we can't do a match,
  2376. // but allow it at the lowest weight.
  2377. if (!CallOperandVal)
  2378. return CW_Default;
  2379. // Look at the constraint type.
  2380. switch (*constraint) {
  2381. case 'i': // immediate integer.
  2382. case 'n': // immediate integer with a known value.
  2383. if (isa<ConstantInt>(CallOperandVal))
  2384. weight = CW_Constant;
  2385. break;
  2386. case 's': // non-explicit intregal immediate.
  2387. if (isa<GlobalValue>(CallOperandVal))
  2388. weight = CW_Constant;
  2389. break;
  2390. case 'E': // immediate float if host format.
  2391. case 'F': // immediate float.
  2392. if (isa<ConstantFP>(CallOperandVal))
  2393. weight = CW_Constant;
  2394. break;
  2395. case '<': // memory operand with autodecrement.
  2396. case '>': // memory operand with autoincrement.
  2397. case 'm': // memory operand.
  2398. case 'o': // offsettable memory operand
  2399. case 'V': // non-offsettable memory operand
  2400. weight = CW_Memory;
  2401. break;
  2402. case 'r': // general register.
  2403. case 'g': // general register, memory operand or immediate integer.
  2404. // note: Clang converts "g" to "imr".
  2405. if (CallOperandVal->getType()->isIntegerTy())
  2406. weight = CW_Register;
  2407. break;
  2408. case 'X': // any operand.
  2409. default:
  2410. weight = CW_Default;
  2411. break;
  2412. }
  2413. return weight;
  2414. }
  2415. /// If there are multiple different constraints that we could pick for this
  2416. /// operand (e.g. "imr") try to pick the 'best' one.
  2417. /// This is somewhat tricky: constraints fall into four classes:
  2418. /// Other -> immediates and magic values
  2419. /// Register -> one specific register
  2420. /// RegisterClass -> a group of regs
  2421. /// Memory -> memory
  2422. /// Ideally, we would pick the most specific constraint possible: if we have
  2423. /// something that fits into a register, we would pick it. The problem here
  2424. /// is that if we have something that could either be in a register or in
  2425. /// memory that use of the register could cause selection of *other*
  2426. /// operands to fail: they might only succeed if we pick memory. Because of
  2427. /// this the heuristic we use is:
  2428. ///
  2429. /// 1) If there is an 'other' constraint, and if the operand is valid for
  2430. /// that constraint, use it. This makes us take advantage of 'i'
  2431. /// constraints when available.
  2432. /// 2) Otherwise, pick the most general constraint present. This prefers
  2433. /// 'm' over 'r', for example.
  2434. ///
  2435. static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
  2436. const TargetLowering &TLI,
  2437. SDValue Op, SelectionDAG *DAG) {
  2438. assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
  2439. unsigned BestIdx = 0;
  2440. TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
  2441. int BestGenerality = -1;
  2442. // Loop over the options, keeping track of the most general one.
  2443. for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
  2444. TargetLowering::ConstraintType CType =
  2445. TLI.getConstraintType(OpInfo.Codes[i]);
  2446. // If this is an 'other' constraint, see if the operand is valid for it.
  2447. // For example, on X86 we might have an 'rI' constraint. If the operand
  2448. // is an integer in the range [0..31] we want to use I (saving a load
  2449. // of a register), otherwise we must use 'r'.
  2450. if (CType == TargetLowering::C_Other && Op.getNode()) {
  2451. assert(OpInfo.Codes[i].size() == 1 &&
  2452. "Unhandled multi-letter 'other' constraint");
  2453. std::vector<SDValue> ResultOps;
  2454. TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
  2455. ResultOps, *DAG);
  2456. if (!ResultOps.empty()) {
  2457. BestType = CType;
  2458. BestIdx = i;
  2459. break;
  2460. }
  2461. }
  2462. // Things with matching constraints can only be registers, per gcc
  2463. // documentation. This mainly affects "g" constraints.
  2464. if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
  2465. continue;
  2466. // This constraint letter is more general than the previous one, use it.
  2467. int Generality = getConstraintGenerality(CType);
  2468. if (Generality > BestGenerality) {
  2469. BestType = CType;
  2470. BestIdx = i;
  2471. BestGenerality = Generality;
  2472. }
  2473. }
  2474. OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
  2475. OpInfo.ConstraintType = BestType;
  2476. }
  2477. /// Determines the constraint code and constraint type to use for the specific
  2478. /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType.
  2479. void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
  2480. SDValue Op,
  2481. SelectionDAG *DAG) const {
  2482. assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
  2483. // Single-letter constraints ('r') are very common.
  2484. if (OpInfo.Codes.size() == 1) {
  2485. OpInfo.ConstraintCode = OpInfo.Codes[0];
  2486. OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
  2487. } else {
  2488. ChooseConstraint(OpInfo, *this, Op, DAG);
  2489. }
  2490. // 'X' matches anything.
  2491. if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
  2492. // Labels and constants are handled elsewhere ('X' is the only thing
  2493. // that matches labels). For Functions, the type here is the type of
  2494. // the result, which is not what we want to look at; leave them alone.
  2495. Value *v = OpInfo.CallOperandVal;
  2496. if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
  2497. OpInfo.CallOperandVal = v;
  2498. return;
  2499. }
  2500. // Otherwise, try to resolve it to something we know about by looking at
  2501. // the actual operand type.
  2502. if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
  2503. OpInfo.ConstraintCode = Repl;
  2504. OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
  2505. }
  2506. }
  2507. }
  2508. /// \brief Given an exact SDIV by a constant, create a multiplication
  2509. /// with the multiplicative inverse of the constant.
  2510. static SDValue BuildExactSDIV(const TargetLowering &TLI, SDValue Op1, APInt d,
  2511. SDLoc dl, SelectionDAG &DAG,
  2512. std::vector<SDNode *> &Created) {
  2513. assert(d != 0 && "Division by zero!");
  2514. // Shift the value upfront if it is even, so the LSB is one.
  2515. unsigned ShAmt = d.countTrailingZeros();
  2516. if (ShAmt) {
  2517. // TODO: For UDIV use SRL instead of SRA.
  2518. SDValue Amt =
  2519. DAG.getConstant(ShAmt, dl, TLI.getShiftAmountTy(Op1.getValueType(),
  2520. DAG.getDataLayout()));
  2521. SDNodeFlags Flags;
  2522. Flags.setExact(true);
  2523. Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt, &Flags);
  2524. Created.push_back(Op1.getNode());
  2525. d = d.ashr(ShAmt);
  2526. }
  2527. // Calculate the multiplicative inverse, using Newton's method.
  2528. APInt t, xn = d;
  2529. while ((t = d*xn) != 1)
  2530. xn *= APInt(d.getBitWidth(), 2) - t;
  2531. SDValue Op2 = DAG.getConstant(xn, dl, Op1.getValueType());
  2532. SDValue Mul = DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
  2533. Created.push_back(Mul.getNode());
  2534. return Mul;
  2535. }
  2536. SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
  2537. SelectionDAG &DAG,
  2538. std::vector<SDNode *> *Created) const {
  2539. AttributeSet Attr = DAG.getMachineFunction().getFunction()->getAttributes();
  2540. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  2541. if (TLI.isIntDivCheap(N->getValueType(0), Attr))
  2542. return SDValue(N,0); // Lower SDIV as SDIV
  2543. return SDValue();
  2544. }
  2545. /// \brief Given an ISD::SDIV node expressing a divide by constant,
  2546. /// return a DAG expression to select that will generate the same value by
  2547. /// multiplying by a magic number.
  2548. /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
  2549. SDValue TargetLowering::BuildSDIV(SDNode *N, const APInt &Divisor,
  2550. SelectionDAG &DAG, bool IsAfterLegalization,
  2551. std::vector<SDNode *> *Created) const {
  2552. assert(Created && "No vector to hold sdiv ops.");
  2553. EVT VT = N->getValueType(0);
  2554. SDLoc dl(N);
  2555. // Check to see if we can do this.
  2556. // FIXME: We should be more aggressive here.
  2557. if (!isTypeLegal(VT))
  2558. return SDValue();
  2559. // If the sdiv has an 'exact' bit we can use a simpler lowering.
  2560. if (cast<BinaryWithFlagsSDNode>(N)->Flags.hasExact())
  2561. return BuildExactSDIV(*this, N->getOperand(0), Divisor, dl, DAG, *Created);
  2562. APInt::ms magics = Divisor.magic();
  2563. // Multiply the numerator (operand 0) by the magic value
  2564. // FIXME: We should support doing a MUL in a wider type
  2565. SDValue Q;
  2566. if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
  2567. isOperationLegalOrCustom(ISD::MULHS, VT))
  2568. Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
  2569. DAG.getConstant(magics.m, dl, VT));
  2570. else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
  2571. isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
  2572. Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
  2573. N->getOperand(0),
  2574. DAG.getConstant(magics.m, dl, VT)).getNode(), 1);
  2575. else
  2576. return SDValue(); // No mulhs or equvialent
  2577. // If d > 0 and m < 0, add the numerator
  2578. if (Divisor.isStrictlyPositive() && magics.m.isNegative()) {
  2579. Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
  2580. Created->push_back(Q.getNode());
  2581. }
  2582. // If d < 0 and m > 0, subtract the numerator.
  2583. if (Divisor.isNegative() && magics.m.isStrictlyPositive()) {
  2584. Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
  2585. Created->push_back(Q.getNode());
  2586. }
  2587. auto &DL = DAG.getDataLayout();
  2588. // Shift right algebraic if shift value is nonzero
  2589. if (magics.s > 0) {
  2590. Q = DAG.getNode(
  2591. ISD::SRA, dl, VT, Q,
  2592. DAG.getConstant(magics.s, dl, getShiftAmountTy(Q.getValueType(), DL)));
  2593. Created->push_back(Q.getNode());
  2594. }
  2595. // Extract the sign bit and add it to the quotient
  2596. SDValue T =
  2597. DAG.getNode(ISD::SRL, dl, VT, Q,
  2598. DAG.getConstant(VT.getScalarSizeInBits() - 1, dl,
  2599. getShiftAmountTy(Q.getValueType(), DL)));
  2600. Created->push_back(T.getNode());
  2601. return DAG.getNode(ISD::ADD, dl, VT, Q, T);
  2602. }
  2603. /// \brief Given an ISD::UDIV node expressing a divide by constant,
  2604. /// return a DAG expression to select that will generate the same value by
  2605. /// multiplying by a magic number.
  2606. /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
  2607. SDValue TargetLowering::BuildUDIV(SDNode *N, const APInt &Divisor,
  2608. SelectionDAG &DAG, bool IsAfterLegalization,
  2609. std::vector<SDNode *> *Created) const {
  2610. assert(Created && "No vector to hold udiv ops.");
  2611. EVT VT = N->getValueType(0);
  2612. SDLoc dl(N);
  2613. auto &DL = DAG.getDataLayout();
  2614. // Check to see if we can do this.
  2615. // FIXME: We should be more aggressive here.
  2616. if (!isTypeLegal(VT))
  2617. return SDValue();
  2618. // FIXME: We should use a narrower constant when the upper
  2619. // bits are known to be zero.
  2620. APInt::mu magics = Divisor.magicu();
  2621. SDValue Q = N->getOperand(0);
  2622. // If the divisor is even, we can avoid using the expensive fixup by shifting
  2623. // the divided value upfront.
  2624. if (magics.a != 0 && !Divisor[0]) {
  2625. unsigned Shift = Divisor.countTrailingZeros();
  2626. Q = DAG.getNode(
  2627. ISD::SRL, dl, VT, Q,
  2628. DAG.getConstant(Shift, dl, getShiftAmountTy(Q.getValueType(), DL)));
  2629. Created->push_back(Q.getNode());
  2630. // Get magic number for the shifted divisor.
  2631. magics = Divisor.lshr(Shift).magicu(Shift);
  2632. assert(magics.a == 0 && "Should use cheap fixup now");
  2633. }
  2634. // Multiply the numerator (operand 0) by the magic value
  2635. // FIXME: We should support doing a MUL in a wider type
  2636. if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
  2637. isOperationLegalOrCustom(ISD::MULHU, VT))
  2638. Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, dl, VT));
  2639. else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
  2640. isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
  2641. Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
  2642. DAG.getConstant(magics.m, dl, VT)).getNode(), 1);
  2643. else
  2644. return SDValue(); // No mulhu or equvialent
  2645. Created->push_back(Q.getNode());
  2646. if (magics.a == 0) {
  2647. assert(magics.s < Divisor.getBitWidth() &&
  2648. "We shouldn't generate an undefined shift!");
  2649. return DAG.getNode(
  2650. ISD::SRL, dl, VT, Q,
  2651. DAG.getConstant(magics.s, dl, getShiftAmountTy(Q.getValueType(), DL)));
  2652. } else {
  2653. SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
  2654. Created->push_back(NPQ.getNode());
  2655. NPQ = DAG.getNode(
  2656. ISD::SRL, dl, VT, NPQ,
  2657. DAG.getConstant(1, dl, getShiftAmountTy(NPQ.getValueType(), DL)));
  2658. Created->push_back(NPQ.getNode());
  2659. NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
  2660. Created->push_back(NPQ.getNode());
  2661. return DAG.getNode(
  2662. ISD::SRL, dl, VT, NPQ,
  2663. DAG.getConstant(magics.s - 1, dl,
  2664. getShiftAmountTy(NPQ.getValueType(), DL)));
  2665. }
  2666. }
  2667. bool TargetLowering::
  2668. verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const {
  2669. if (!isa<ConstantSDNode>(Op.getOperand(0))) {
  2670. DAG.getContext()->emitError("argument to '__builtin_return_address' must "
  2671. "be a constant integer");
  2672. return true;
  2673. }
  2674. return false;
  2675. }
  2676. //===----------------------------------------------------------------------===//
  2677. // Legalization Utilities
  2678. //===----------------------------------------------------------------------===//
  2679. bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT,
  2680. SelectionDAG &DAG, SDValue LL, SDValue LH,
  2681. SDValue RL, SDValue RH) const {
  2682. EVT VT = N->getValueType(0);
  2683. SDLoc dl(N);
  2684. bool HasMULHS = isOperationLegalOrCustom(ISD::MULHS, HiLoVT);
  2685. bool HasMULHU = isOperationLegalOrCustom(ISD::MULHU, HiLoVT);
  2686. bool HasSMUL_LOHI = isOperationLegalOrCustom(ISD::SMUL_LOHI, HiLoVT);
  2687. bool HasUMUL_LOHI = isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT);
  2688. if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
  2689. unsigned OuterBitSize = VT.getSizeInBits();
  2690. unsigned InnerBitSize = HiLoVT.getSizeInBits();
  2691. unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
  2692. unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
  2693. // LL, LH, RL, and RH must be either all NULL or all set to a value.
  2694. assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) ||
  2695. (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode()));
  2696. if (!LL.getNode() && !RL.getNode() &&
  2697. isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
  2698. LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, N->getOperand(0));
  2699. RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, N->getOperand(1));
  2700. }
  2701. if (!LL.getNode())
  2702. return false;
  2703. APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
  2704. if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
  2705. DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
  2706. // The inputs are both zero-extended.
  2707. if (HasUMUL_LOHI) {
  2708. // We can emit a umul_lohi.
  2709. Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(HiLoVT, HiLoVT), LL,
  2710. RL);
  2711. Hi = SDValue(Lo.getNode(), 1);
  2712. return true;
  2713. }
  2714. if (HasMULHU) {
  2715. // We can emit a mulhu+mul.
  2716. Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL);
  2717. Hi = DAG.getNode(ISD::MULHU, dl, HiLoVT, LL, RL);
  2718. return true;
  2719. }
  2720. }
  2721. if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
  2722. // The input values are both sign-extended.
  2723. if (HasSMUL_LOHI) {
  2724. // We can emit a smul_lohi.
  2725. Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(HiLoVT, HiLoVT), LL,
  2726. RL);
  2727. Hi = SDValue(Lo.getNode(), 1);
  2728. return true;
  2729. }
  2730. if (HasMULHS) {
  2731. // We can emit a mulhs+mul.
  2732. Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL);
  2733. Hi = DAG.getNode(ISD::MULHS, dl, HiLoVT, LL, RL);
  2734. return true;
  2735. }
  2736. }
  2737. if (!LH.getNode() && !RH.getNode() &&
  2738. isOperationLegalOrCustom(ISD::SRL, VT) &&
  2739. isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
  2740. auto &DL = DAG.getDataLayout();
  2741. unsigned ShiftAmt = VT.getSizeInBits() - HiLoVT.getSizeInBits();
  2742. SDValue Shift = DAG.getConstant(ShiftAmt, dl, getShiftAmountTy(VT, DL));
  2743. LH = DAG.getNode(ISD::SRL, dl, VT, N->getOperand(0), Shift);
  2744. LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH);
  2745. RH = DAG.getNode(ISD::SRL, dl, VT, N->getOperand(1), Shift);
  2746. RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH);
  2747. }
  2748. if (!LH.getNode())
  2749. return false;
  2750. if (HasUMUL_LOHI) {
  2751. // Lo,Hi = umul LHS, RHS.
  2752. SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
  2753. DAG.getVTList(HiLoVT, HiLoVT), LL, RL);
  2754. Lo = UMulLOHI;
  2755. Hi = UMulLOHI.getValue(1);
  2756. RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
  2757. LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
  2758. Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
  2759. Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
  2760. return true;
  2761. }
  2762. if (HasMULHU) {
  2763. Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RL);
  2764. Hi = DAG.getNode(ISD::MULHU, dl, HiLoVT, LL, RL);
  2765. RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
  2766. LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
  2767. Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
  2768. Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
  2769. return true;
  2770. }
  2771. }
  2772. return false;
  2773. }
  2774. bool TargetLowering::expandFP_TO_SINT(SDNode *Node, SDValue &Result,
  2775. SelectionDAG &DAG) const {
  2776. EVT VT = Node->getOperand(0).getValueType();
  2777. EVT NVT = Node->getValueType(0);
  2778. SDLoc dl(SDValue(Node, 0));
  2779. // FIXME: Only f32 to i64 conversions are supported.
  2780. if (VT != MVT::f32 || NVT != MVT::i64)
  2781. return false;
  2782. // Expand f32 -> i64 conversion
  2783. // This algorithm comes from compiler-rt's implementation of fixsfdi:
  2784. // https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/fixsfdi.c
  2785. EVT IntVT = EVT::getIntegerVT(*DAG.getContext(),
  2786. VT.getSizeInBits());
  2787. SDValue ExponentMask = DAG.getConstant(0x7F800000, dl, IntVT);
  2788. SDValue ExponentLoBit = DAG.getConstant(23, dl, IntVT);
  2789. SDValue Bias = DAG.getConstant(127, dl, IntVT);
  2790. SDValue SignMask = DAG.getConstant(APInt::getSignBit(VT.getSizeInBits()), dl,
  2791. IntVT);
  2792. SDValue SignLowBit = DAG.getConstant(VT.getSizeInBits() - 1, dl, IntVT);
  2793. SDValue MantissaMask = DAG.getConstant(0x007FFFFF, dl, IntVT);
  2794. SDValue Bits = DAG.getNode(ISD::BITCAST, dl, IntVT, Node->getOperand(0));
  2795. auto &DL = DAG.getDataLayout();
  2796. SDValue ExponentBits = DAG.getNode(
  2797. ISD::SRL, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, ExponentMask),
  2798. DAG.getZExtOrTrunc(ExponentLoBit, dl, getShiftAmountTy(IntVT, DL)));
  2799. SDValue Exponent = DAG.getNode(ISD::SUB, dl, IntVT, ExponentBits, Bias);
  2800. SDValue Sign = DAG.getNode(
  2801. ISD::SRA, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, SignMask),
  2802. DAG.getZExtOrTrunc(SignLowBit, dl, getShiftAmountTy(IntVT, DL)));
  2803. Sign = DAG.getSExtOrTrunc(Sign, dl, NVT);
  2804. SDValue R = DAG.getNode(ISD::OR, dl, IntVT,
  2805. DAG.getNode(ISD::AND, dl, IntVT, Bits, MantissaMask),
  2806. DAG.getConstant(0x00800000, dl, IntVT));
  2807. R = DAG.getZExtOrTrunc(R, dl, NVT);
  2808. R = DAG.getSelectCC(
  2809. dl, Exponent, ExponentLoBit,
  2810. DAG.getNode(ISD::SHL, dl, NVT, R,
  2811. DAG.getZExtOrTrunc(
  2812. DAG.getNode(ISD::SUB, dl, IntVT, Exponent, ExponentLoBit),
  2813. dl, getShiftAmountTy(IntVT, DL))),
  2814. DAG.getNode(ISD::SRL, dl, NVT, R,
  2815. DAG.getZExtOrTrunc(
  2816. DAG.getNode(ISD::SUB, dl, IntVT, ExponentLoBit, Exponent),
  2817. dl, getShiftAmountTy(IntVT, DL))),
  2818. ISD::SETGT);
  2819. SDValue Ret = DAG.getNode(ISD::SUB, dl, NVT,
  2820. DAG.getNode(ISD::XOR, dl, NVT, R, Sign),
  2821. Sign);
  2822. Result = DAG.getSelectCC(dl, Exponent, DAG.getConstant(0, dl, IntVT),
  2823. DAG.getConstant(0, dl, NVT), Ret, ISD::SETLT);
  2824. return true;
  2825. }
  2826. //===----------------------------------------------------------------------===//
  2827. // Implementation of Emulated TLS Model
  2828. //===----------------------------------------------------------------------===//
  2829. SDValue TargetLowering::LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
  2830. SelectionDAG &DAG) const {
  2831. // Access to address of TLS varialbe xyz is lowered to a function call:
  2832. // __emutls_get_address( address of global variable named "__emutls_v.xyz" )
  2833. EVT PtrVT = getPointerTy(DAG.getDataLayout());
  2834. PointerType *VoidPtrType = Type::getInt8PtrTy(*DAG.getContext());
  2835. SDLoc dl(GA);
  2836. ArgListTy Args;
  2837. ArgListEntry Entry;
  2838. std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str();
  2839. Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent());
  2840. StringRef EmuTlsVarName(NameString);
  2841. GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName);
  2842. assert(EmuTlsVar && "Cannot find EmuTlsVar ");
  2843. Entry.Node = DAG.getGlobalAddress(EmuTlsVar, dl, PtrVT);
  2844. Entry.Ty = VoidPtrType;
  2845. Args.push_back(Entry);
  2846. SDValue EmuTlsGetAddr = DAG.getExternalSymbol("__emutls_get_address", PtrVT);
  2847. TargetLowering::CallLoweringInfo CLI(DAG);
  2848. CLI.setDebugLoc(dl).setChain(DAG.getEntryNode());
  2849. CLI.setCallee(CallingConv::C, VoidPtrType, EmuTlsGetAddr, std::move(Args), 0);
  2850. std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
  2851. // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
  2852. // At last for X86 targets, maybe good for other targets too?
  2853. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  2854. MFI->setAdjustsStack(true); // Is this only for X86 target?
  2855. MFI->setHasCalls(true);
  2856. assert((GA->getOffset() == 0) &&
  2857. "Emulated TLS must have zero offset in GlobalAddressSDNode");
  2858. return CallResult.first;
  2859. }