TargetLowering.cpp 123 KB

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