TargetLowering.cpp 110 KB

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