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