TargetLowering.cpp 106 KB

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