1 //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the TargetLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/TargetLowering.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/CodeGen/CallingConvLower.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineJumpTableInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/CodeGen/TargetSubtargetInfo.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/KnownBits.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include <cctype>
35 using namespace llvm;
36 
37 /// NOTE: The TargetMachine owns TLOF.
38 TargetLowering::TargetLowering(const TargetMachine &tm)
39     : TargetLoweringBase(tm) {}
40 
41 const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
42   return nullptr;
43 }
44 
45 bool TargetLowering::isPositionIndependent() const {
46   return getTargetMachine().isPositionIndependent();
47 }
48 
49 /// Check whether a given call node is in tail position within its function. If
50 /// so, it sets Chain to the input chain of the tail call.
51 bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
52                                           SDValue &Chain) const {
53   const Function &F = DAG.getMachineFunction().getFunction();
54 
55   // First, check if tail calls have been disabled in this function.
56   if (F.getFnAttribute("disable-tail-calls").getValueAsBool())
57     return false;
58 
59   // Conservatively require the attributes of the call to match those of
60   // the return. Ignore following attributes because they don't affect the
61   // call sequence.
62   AttrBuilder CallerAttrs(F.getAttributes(), AttributeList::ReturnIndex);
63   for (const auto &Attr : {Attribute::Alignment, Attribute::Dereferenceable,
64                            Attribute::DereferenceableOrNull, Attribute::NoAlias,
65                            Attribute::NonNull})
66     CallerAttrs.removeAttribute(Attr);
67 
68   if (CallerAttrs.hasAttributes())
69     return false;
70 
71   // It's not safe to eliminate the sign / zero extension of the return value.
72   if (CallerAttrs.contains(Attribute::ZExt) ||
73       CallerAttrs.contains(Attribute::SExt))
74     return false;
75 
76   // Check if the only use is a function return node.
77   return isUsedByReturnOnly(Node, Chain);
78 }
79 
80 bool TargetLowering::parametersInCSRMatch(const MachineRegisterInfo &MRI,
81     const uint32_t *CallerPreservedMask,
82     const SmallVectorImpl<CCValAssign> &ArgLocs,
83     const SmallVectorImpl<SDValue> &OutVals) const {
84   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
85     const CCValAssign &ArgLoc = ArgLocs[I];
86     if (!ArgLoc.isRegLoc())
87       continue;
88     MCRegister Reg = ArgLoc.getLocReg();
89     // Only look at callee saved registers.
90     if (MachineOperand::clobbersPhysReg(CallerPreservedMask, Reg))
91       continue;
92     // Check that we pass the value used for the caller.
93     // (We look for a CopyFromReg reading a virtual register that is used
94     //  for the function live-in value of register Reg)
95     SDValue Value = OutVals[I];
96     if (Value->getOpcode() != ISD::CopyFromReg)
97       return false;
98     Register ArgReg = cast<RegisterSDNode>(Value->getOperand(1))->getReg();
99     if (MRI.getLiveInPhysReg(ArgReg) != Reg)
100       return false;
101   }
102   return true;
103 }
104 
105 /// Set CallLoweringInfo attribute flags based on a call instruction
106 /// and called function attributes.
107 void TargetLoweringBase::ArgListEntry::setAttributes(const CallBase *Call,
108                                                      unsigned ArgIdx) {
109   IsSExt = Call->paramHasAttr(ArgIdx, Attribute::SExt);
110   IsZExt = Call->paramHasAttr(ArgIdx, Attribute::ZExt);
111   IsInReg = Call->paramHasAttr(ArgIdx, Attribute::InReg);
112   IsSRet = Call->paramHasAttr(ArgIdx, Attribute::StructRet);
113   IsNest = Call->paramHasAttr(ArgIdx, Attribute::Nest);
114   IsByVal = Call->paramHasAttr(ArgIdx, Attribute::ByVal);
115   IsPreallocated = Call->paramHasAttr(ArgIdx, Attribute::Preallocated);
116   IsInAlloca = Call->paramHasAttr(ArgIdx, Attribute::InAlloca);
117   IsReturned = Call->paramHasAttr(ArgIdx, Attribute::Returned);
118   IsSwiftSelf = Call->paramHasAttr(ArgIdx, Attribute::SwiftSelf);
119   IsSwiftAsync = Call->paramHasAttr(ArgIdx, Attribute::SwiftAsync);
120   IsSwiftError = Call->paramHasAttr(ArgIdx, Attribute::SwiftError);
121   Alignment = Call->getParamStackAlign(ArgIdx);
122   ByValType = nullptr;
123   if (IsByVal) {
124     ByValType = Call->getParamByValType(ArgIdx);
125     if (!Alignment)
126       Alignment = Call->getParamAlign(ArgIdx);
127   }
128   PreallocatedType = nullptr;
129   if (IsPreallocated)
130     PreallocatedType = Call->getParamPreallocatedType(ArgIdx);
131 }
132 
133 /// Generate a libcall taking the given operands as arguments and returning a
134 /// result of type RetVT.
135 std::pair<SDValue, SDValue>
136 TargetLowering::makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT,
137                             ArrayRef<SDValue> Ops,
138                             MakeLibCallOptions CallOptions,
139                             const SDLoc &dl,
140                             SDValue InChain) const {
141   if (!InChain)
142     InChain = DAG.getEntryNode();
143 
144   TargetLowering::ArgListTy Args;
145   Args.reserve(Ops.size());
146 
147   TargetLowering::ArgListEntry Entry;
148   for (unsigned i = 0; i < Ops.size(); ++i) {
149     SDValue NewOp = Ops[i];
150     Entry.Node = NewOp;
151     Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
152     Entry.IsSExt = shouldSignExtendTypeInLibCall(NewOp.getValueType(),
153                                                  CallOptions.IsSExt);
154     Entry.IsZExt = !Entry.IsSExt;
155 
156     if (CallOptions.IsSoften &&
157         !shouldExtendTypeInLibCall(CallOptions.OpsVTBeforeSoften[i])) {
158       Entry.IsSExt = Entry.IsZExt = false;
159     }
160     Args.push_back(Entry);
161   }
162 
163   if (LC == RTLIB::UNKNOWN_LIBCALL)
164     report_fatal_error("Unsupported library call operation!");
165   SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
166                                          getPointerTy(DAG.getDataLayout()));
167 
168   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
169   TargetLowering::CallLoweringInfo CLI(DAG);
170   bool signExtend = shouldSignExtendTypeInLibCall(RetVT, CallOptions.IsSExt);
171   bool zeroExtend = !signExtend;
172 
173   if (CallOptions.IsSoften &&
174       !shouldExtendTypeInLibCall(CallOptions.RetVTBeforeSoften)) {
175     signExtend = zeroExtend = false;
176   }
177 
178   CLI.setDebugLoc(dl)
179       .setChain(InChain)
180       .setLibCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
181       .setNoReturn(CallOptions.DoesNotReturn)
182       .setDiscardResult(!CallOptions.IsReturnValueUsed)
183       .setIsPostTypeLegalization(CallOptions.IsPostTypeLegalization)
184       .setSExtResult(signExtend)
185       .setZExtResult(zeroExtend);
186   return LowerCallTo(CLI);
187 }
188 
189 bool TargetLowering::findOptimalMemOpLowering(
190     std::vector<EVT> &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS,
191     unsigned SrcAS, const AttributeList &FuncAttributes) const {
192   if (Op.isMemcpyWithFixedDstAlign() && Op.getSrcAlign() < Op.getDstAlign())
193     return false;
194 
195   EVT VT = getOptimalMemOpType(Op, FuncAttributes);
196 
197   if (VT == MVT::Other) {
198     // Use the largest integer type whose alignment constraints are satisfied.
199     // We only need to check DstAlign here as SrcAlign is always greater or
200     // equal to DstAlign (or zero).
201     VT = MVT::i64;
202     if (Op.isFixedDstAlign())
203       while (Op.getDstAlign() < (VT.getSizeInBits() / 8) &&
204              !allowsMisalignedMemoryAccesses(VT, DstAS, Op.getDstAlign()))
205         VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
206     assert(VT.isInteger());
207 
208     // Find the largest legal integer type.
209     MVT LVT = MVT::i64;
210     while (!isTypeLegal(LVT))
211       LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
212     assert(LVT.isInteger());
213 
214     // If the type we've chosen is larger than the largest legal integer type
215     // then use that instead.
216     if (VT.bitsGT(LVT))
217       VT = LVT;
218   }
219 
220   unsigned NumMemOps = 0;
221   uint64_t Size = Op.size();
222   while (Size) {
223     unsigned VTSize = VT.getSizeInBits() / 8;
224     while (VTSize > Size) {
225       // For now, only use non-vector load / store's for the left-over pieces.
226       EVT NewVT = VT;
227       unsigned NewVTSize;
228 
229       bool Found = false;
230       if (VT.isVector() || VT.isFloatingPoint()) {
231         NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
232         if (isOperationLegalOrCustom(ISD::STORE, NewVT) &&
233             isSafeMemOpType(NewVT.getSimpleVT()))
234           Found = true;
235         else if (NewVT == MVT::i64 &&
236                  isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
237                  isSafeMemOpType(MVT::f64)) {
238           // i64 is usually not legal on 32-bit targets, but f64 may be.
239           NewVT = MVT::f64;
240           Found = true;
241         }
242       }
243 
244       if (!Found) {
245         do {
246           NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
247           if (NewVT == MVT::i8)
248             break;
249         } while (!isSafeMemOpType(NewVT.getSimpleVT()));
250       }
251       NewVTSize = NewVT.getSizeInBits() / 8;
252 
253       // If the new VT cannot cover all of the remaining bits, then consider
254       // issuing a (or a pair of) unaligned and overlapping load / store.
255       bool Fast;
256       if (NumMemOps && Op.allowOverlap() && NewVTSize < Size &&
257           allowsMisalignedMemoryAccesses(
258               VT, DstAS, Op.isFixedDstAlign() ? Op.getDstAlign() : Align(1),
259               MachineMemOperand::MONone, &Fast) &&
260           Fast)
261         VTSize = Size;
262       else {
263         VT = NewVT;
264         VTSize = NewVTSize;
265       }
266     }
267 
268     if (++NumMemOps > Limit)
269       return false;
270 
271     MemOps.push_back(VT);
272     Size -= VTSize;
273   }
274 
275   return true;
276 }
277 
278 /// Soften the operands of a comparison. This code is shared among BR_CC,
279 /// SELECT_CC, and SETCC handlers.
280 void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
281                                          SDValue &NewLHS, SDValue &NewRHS,
282                                          ISD::CondCode &CCCode,
283                                          const SDLoc &dl, const SDValue OldLHS,
284                                          const SDValue OldRHS) const {
285   SDValue Chain;
286   return softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, dl, OldLHS,
287                              OldRHS, Chain);
288 }
289 
290 void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT,
291                                          SDValue &NewLHS, SDValue &NewRHS,
292                                          ISD::CondCode &CCCode,
293                                          const SDLoc &dl, const SDValue OldLHS,
294                                          const SDValue OldRHS,
295                                          SDValue &Chain,
296                                          bool IsSignaling) const {
297   // FIXME: Currently we cannot really respect all IEEE predicates due to libgcc
298   // not supporting it. We can update this code when libgcc provides such
299   // functions.
300 
301   assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128)
302          && "Unsupported setcc type!");
303 
304   // Expand into one or more soft-fp libcall(s).
305   RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
306   bool ShouldInvertCC = false;
307   switch (CCCode) {
308   case ISD::SETEQ:
309   case ISD::SETOEQ:
310     LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
311           (VT == MVT::f64) ? RTLIB::OEQ_F64 :
312           (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
313     break;
314   case ISD::SETNE:
315   case ISD::SETUNE:
316     LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 :
317           (VT == MVT::f64) ? RTLIB::UNE_F64 :
318           (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128;
319     break;
320   case ISD::SETGE:
321   case ISD::SETOGE:
322     LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
323           (VT == MVT::f64) ? RTLIB::OGE_F64 :
324           (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
325     break;
326   case ISD::SETLT:
327   case ISD::SETOLT:
328     LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
329           (VT == MVT::f64) ? RTLIB::OLT_F64 :
330           (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
331     break;
332   case ISD::SETLE:
333   case ISD::SETOLE:
334     LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
335           (VT == MVT::f64) ? RTLIB::OLE_F64 :
336           (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
337     break;
338   case ISD::SETGT:
339   case ISD::SETOGT:
340     LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
341           (VT == MVT::f64) ? RTLIB::OGT_F64 :
342           (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
343     break;
344   case ISD::SETO:
345     ShouldInvertCC = true;
346     LLVM_FALLTHROUGH;
347   case ISD::SETUO:
348     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
349           (VT == MVT::f64) ? RTLIB::UO_F64 :
350           (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
351     break;
352   case ISD::SETONE:
353     // SETONE = O && UNE
354     ShouldInvertCC = true;
355     LLVM_FALLTHROUGH;
356   case ISD::SETUEQ:
357     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 :
358           (VT == MVT::f64) ? RTLIB::UO_F64 :
359           (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128;
360     LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 :
361           (VT == MVT::f64) ? RTLIB::OEQ_F64 :
362           (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128;
363     break;
364   default:
365     // Invert CC for unordered comparisons
366     ShouldInvertCC = true;
367     switch (CCCode) {
368     case ISD::SETULT:
369       LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 :
370             (VT == MVT::f64) ? RTLIB::OGE_F64 :
371             (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128;
372       break;
373     case ISD::SETULE:
374       LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 :
375             (VT == MVT::f64) ? RTLIB::OGT_F64 :
376             (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128;
377       break;
378     case ISD::SETUGT:
379       LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 :
380             (VT == MVT::f64) ? RTLIB::OLE_F64 :
381             (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128;
382       break;
383     case ISD::SETUGE:
384       LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 :
385             (VT == MVT::f64) ? RTLIB::OLT_F64 :
386             (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128;
387       break;
388     default: llvm_unreachable("Do not know how to soften this setcc!");
389     }
390   }
391 
392   // Use the target specific return value for comparions lib calls.
393   EVT RetVT = getCmpLibcallReturnType();
394   SDValue Ops[2] = {NewLHS, NewRHS};
395   TargetLowering::MakeLibCallOptions CallOptions;
396   EVT OpsVT[2] = { OldLHS.getValueType(),
397                    OldRHS.getValueType() };
398   CallOptions.setTypeListBeforeSoften(OpsVT, RetVT, true);
399   auto Call = makeLibCall(DAG, LC1, RetVT, Ops, CallOptions, dl, Chain);
400   NewLHS = Call.first;
401   NewRHS = DAG.getConstant(0, dl, RetVT);
402 
403   CCCode = getCmpLibcallCC(LC1);
404   if (ShouldInvertCC) {
405     assert(RetVT.isInteger());
406     CCCode = getSetCCInverse(CCCode, RetVT);
407   }
408 
409   if (LC2 == RTLIB::UNKNOWN_LIBCALL) {
410     // Update Chain.
411     Chain = Call.second;
412   } else {
413     EVT SetCCVT =
414         getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), RetVT);
415     SDValue Tmp = DAG.getSetCC(dl, SetCCVT, NewLHS, NewRHS, CCCode);
416     auto Call2 = makeLibCall(DAG, LC2, RetVT, Ops, CallOptions, dl, Chain);
417     CCCode = getCmpLibcallCC(LC2);
418     if (ShouldInvertCC)
419       CCCode = getSetCCInverse(CCCode, RetVT);
420     NewLHS = DAG.getSetCC(dl, SetCCVT, Call2.first, NewRHS, CCCode);
421     if (Chain)
422       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Call.second,
423                           Call2.second);
424     NewLHS = DAG.getNode(ShouldInvertCC ? ISD::AND : ISD::OR, dl,
425                          Tmp.getValueType(), Tmp, NewLHS);
426     NewRHS = SDValue();
427   }
428 }
429 
430 /// Return the entry encoding for a jump table in the current function. The
431 /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum.
432 unsigned TargetLowering::getJumpTableEncoding() const {
433   // In non-pic modes, just use the address of a block.
434   if (!isPositionIndependent())
435     return MachineJumpTableInfo::EK_BlockAddress;
436 
437   // In PIC mode, if the target supports a GPRel32 directive, use it.
438   if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != nullptr)
439     return MachineJumpTableInfo::EK_GPRel32BlockAddress;
440 
441   // Otherwise, use a label difference.
442   return MachineJumpTableInfo::EK_LabelDifference32;
443 }
444 
445 SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
446                                                  SelectionDAG &DAG) const {
447   // If our PIC model is GP relative, use the global offset table as the base.
448   unsigned JTEncoding = getJumpTableEncoding();
449 
450   if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
451       (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
452     return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(DAG.getDataLayout()));
453 
454   return Table;
455 }
456 
457 /// This returns the relocation base for the given PIC jumptable, the same as
458 /// getPICJumpTableRelocBase, but as an MCExpr.
459 const MCExpr *
460 TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
461                                              unsigned JTI,MCContext &Ctx) const{
462   // The normal PIC reloc base is the label at the start of the jump table.
463   return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx);
464 }
465 
466 bool
467 TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
468   const TargetMachine &TM = getTargetMachine();
469   const GlobalValue *GV = GA->getGlobal();
470 
471   // If the address is not even local to this DSO we will have to load it from
472   // a got and then add the offset.
473   if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
474     return false;
475 
476   // If the code is position independent we will have to add a base register.
477   if (isPositionIndependent())
478     return false;
479 
480   // Otherwise we can do it.
481   return true;
482 }
483 
484 //===----------------------------------------------------------------------===//
485 //  Optimization Methods
486 //===----------------------------------------------------------------------===//
487 
488 /// If the specified instruction has a constant integer operand and there are
489 /// bits set in that constant that are not demanded, then clear those bits and
490 /// return true.
491 bool TargetLowering::ShrinkDemandedConstant(SDValue Op,
492                                             const APInt &DemandedBits,
493                                             const APInt &DemandedElts,
494                                             TargetLoweringOpt &TLO) const {
495   SDLoc DL(Op);
496   unsigned Opcode = Op.getOpcode();
497 
498   // Do target-specific constant optimization.
499   if (targetShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
500     return TLO.New.getNode();
501 
502   // FIXME: ISD::SELECT, ISD::SELECT_CC
503   switch (Opcode) {
504   default:
505     break;
506   case ISD::XOR:
507   case ISD::AND:
508   case ISD::OR: {
509     auto *Op1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
510     if (!Op1C || Op1C->isOpaque())
511       return false;
512 
513     // If this is a 'not' op, don't touch it because that's a canonical form.
514     const APInt &C = Op1C->getAPIntValue();
515     if (Opcode == ISD::XOR && DemandedBits.isSubsetOf(C))
516       return false;
517 
518     if (!C.isSubsetOf(DemandedBits)) {
519       EVT VT = Op.getValueType();
520       SDValue NewC = TLO.DAG.getConstant(DemandedBits & C, DL, VT);
521       SDValue NewOp = TLO.DAG.getNode(Opcode, DL, VT, Op.getOperand(0), NewC);
522       return TLO.CombineTo(Op, NewOp);
523     }
524 
525     break;
526   }
527   }
528 
529   return false;
530 }
531 
532 bool TargetLowering::ShrinkDemandedConstant(SDValue Op,
533                                             const APInt &DemandedBits,
534                                             TargetLoweringOpt &TLO) const {
535   EVT VT = Op.getValueType();
536   APInt DemandedElts = VT.isVector()
537                            ? APInt::getAllOnesValue(VT.getVectorNumElements())
538                            : APInt(1, 1);
539   return ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO);
540 }
541 
542 /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free.
543 /// This uses isZExtFree and ZERO_EXTEND for the widening cast, but it could be
544 /// generalized for targets with other types of implicit widening casts.
545 bool TargetLowering::ShrinkDemandedOp(SDValue Op, unsigned BitWidth,
546                                       const APInt &Demanded,
547                                       TargetLoweringOpt &TLO) const {
548   assert(Op.getNumOperands() == 2 &&
549          "ShrinkDemandedOp only supports binary operators!");
550   assert(Op.getNode()->getNumValues() == 1 &&
551          "ShrinkDemandedOp only supports nodes with one result!");
552 
553   SelectionDAG &DAG = TLO.DAG;
554   SDLoc dl(Op);
555 
556   // Early return, as this function cannot handle vector types.
557   if (Op.getValueType().isVector())
558     return false;
559 
560   // Don't do this if the node has another user, which may require the
561   // full value.
562   if (!Op.getNode()->hasOneUse())
563     return false;
564 
565   // Search for the smallest integer type with free casts to and from
566   // Op's type. For expedience, just check power-of-2 integer types.
567   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
568   unsigned DemandedSize = Demanded.getActiveBits();
569   unsigned SmallVTBits = DemandedSize;
570   if (!isPowerOf2_32(SmallVTBits))
571     SmallVTBits = NextPowerOf2(SmallVTBits);
572   for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
573     EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
574     if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
575         TLI.isZExtFree(SmallVT, Op.getValueType())) {
576       // We found a type with free casts.
577       SDValue X = DAG.getNode(
578           Op.getOpcode(), dl, SmallVT,
579           DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(0)),
580           DAG.getNode(ISD::TRUNCATE, dl, SmallVT, Op.getOperand(1)));
581       assert(DemandedSize <= SmallVTBits && "Narrowed below demanded bits?");
582       SDValue Z = DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(), X);
583       return TLO.CombineTo(Op, Z);
584     }
585   }
586   return false;
587 }
588 
589 bool TargetLowering::SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits,
590                                           DAGCombinerInfo &DCI) const {
591   SelectionDAG &DAG = DCI.DAG;
592   TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
593                         !DCI.isBeforeLegalizeOps());
594   KnownBits Known;
595 
596   bool Simplified = SimplifyDemandedBits(Op, DemandedBits, Known, TLO);
597   if (Simplified) {
598     DCI.AddToWorklist(Op.getNode());
599     DCI.CommitTargetLoweringOpt(TLO);
600   }
601   return Simplified;
602 }
603 
604 bool TargetLowering::SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits,
605                                           KnownBits &Known,
606                                           TargetLoweringOpt &TLO,
607                                           unsigned Depth,
608                                           bool AssumeSingleUse) const {
609   EVT VT = Op.getValueType();
610 
611   // TODO: We can probably do more work on calculating the known bits and
612   // simplifying the operations for scalable vectors, but for now we just
613   // bail out.
614   if (VT.isScalableVector()) {
615     // Pretend we don't know anything for now.
616     Known = KnownBits(DemandedBits.getBitWidth());
617     return false;
618   }
619 
620   APInt DemandedElts = VT.isVector()
621                            ? APInt::getAllOnesValue(VT.getVectorNumElements())
622                            : APInt(1, 1);
623   return SimplifyDemandedBits(Op, DemandedBits, DemandedElts, Known, TLO, Depth,
624                               AssumeSingleUse);
625 }
626 
627 // TODO: Can we merge SelectionDAG::GetDemandedBits into this?
628 // TODO: Under what circumstances can we create nodes? Constant folding?
629 SDValue TargetLowering::SimplifyMultipleUseDemandedBits(
630     SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
631     SelectionDAG &DAG, unsigned Depth) const {
632   // Limit search depth.
633   if (Depth >= SelectionDAG::MaxRecursionDepth)
634     return SDValue();
635 
636   // Ignore UNDEFs.
637   if (Op.isUndef())
638     return SDValue();
639 
640   // Not demanding any bits/elts from Op.
641   if (DemandedBits == 0 || DemandedElts == 0)
642     return DAG.getUNDEF(Op.getValueType());
643 
644   unsigned NumElts = DemandedElts.getBitWidth();
645   unsigned BitWidth = DemandedBits.getBitWidth();
646   KnownBits LHSKnown, RHSKnown;
647   switch (Op.getOpcode()) {
648   case ISD::BITCAST: {
649     SDValue Src = peekThroughBitcasts(Op.getOperand(0));
650     EVT SrcVT = Src.getValueType();
651     EVT DstVT = Op.getValueType();
652     if (SrcVT == DstVT)
653       return Src;
654 
655     unsigned NumSrcEltBits = SrcVT.getScalarSizeInBits();
656     unsigned NumDstEltBits = DstVT.getScalarSizeInBits();
657     if (NumSrcEltBits == NumDstEltBits)
658       if (SDValue V = SimplifyMultipleUseDemandedBits(
659               Src, DemandedBits, DemandedElts, DAG, Depth + 1))
660         return DAG.getBitcast(DstVT, V);
661 
662     // TODO - bigendian once we have test coverage.
663     if (SrcVT.isVector() && (NumDstEltBits % NumSrcEltBits) == 0 &&
664         DAG.getDataLayout().isLittleEndian()) {
665       unsigned Scale = NumDstEltBits / NumSrcEltBits;
666       unsigned NumSrcElts = SrcVT.getVectorNumElements();
667       APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits);
668       APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts);
669       for (unsigned i = 0; i != Scale; ++i) {
670         unsigned Offset = i * NumSrcEltBits;
671         APInt Sub = DemandedBits.extractBits(NumSrcEltBits, Offset);
672         if (!Sub.isNullValue()) {
673           DemandedSrcBits |= Sub;
674           for (unsigned j = 0; j != NumElts; ++j)
675             if (DemandedElts[j])
676               DemandedSrcElts.setBit((j * Scale) + i);
677         }
678       }
679 
680       if (SDValue V = SimplifyMultipleUseDemandedBits(
681               Src, DemandedSrcBits, DemandedSrcElts, DAG, Depth + 1))
682         return DAG.getBitcast(DstVT, V);
683     }
684 
685     // TODO - bigendian once we have test coverage.
686     if ((NumSrcEltBits % NumDstEltBits) == 0 &&
687         DAG.getDataLayout().isLittleEndian()) {
688       unsigned Scale = NumSrcEltBits / NumDstEltBits;
689       unsigned NumSrcElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
690       APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits);
691       APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts);
692       for (unsigned i = 0; i != NumElts; ++i)
693         if (DemandedElts[i]) {
694           unsigned Offset = (i % Scale) * NumDstEltBits;
695           DemandedSrcBits.insertBits(DemandedBits, Offset);
696           DemandedSrcElts.setBit(i / Scale);
697         }
698 
699       if (SDValue V = SimplifyMultipleUseDemandedBits(
700               Src, DemandedSrcBits, DemandedSrcElts, DAG, Depth + 1))
701         return DAG.getBitcast(DstVT, V);
702     }
703 
704     break;
705   }
706   case ISD::AND: {
707     LHSKnown = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
708     RHSKnown = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
709 
710     // If all of the demanded bits are known 1 on one side, return the other.
711     // These bits cannot contribute to the result of the 'and' in this
712     // context.
713     if (DemandedBits.isSubsetOf(LHSKnown.Zero | RHSKnown.One))
714       return Op.getOperand(0);
715     if (DemandedBits.isSubsetOf(RHSKnown.Zero | LHSKnown.One))
716       return Op.getOperand(1);
717     break;
718   }
719   case ISD::OR: {
720     LHSKnown = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
721     RHSKnown = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
722 
723     // If all of the demanded bits are known zero on one side, return the
724     // other.  These bits cannot contribute to the result of the 'or' in this
725     // context.
726     if (DemandedBits.isSubsetOf(LHSKnown.One | RHSKnown.Zero))
727       return Op.getOperand(0);
728     if (DemandedBits.isSubsetOf(RHSKnown.One | LHSKnown.Zero))
729       return Op.getOperand(1);
730     break;
731   }
732   case ISD::XOR: {
733     LHSKnown = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
734     RHSKnown = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
735 
736     // If all of the demanded bits are known zero on one side, return the
737     // other.
738     if (DemandedBits.isSubsetOf(RHSKnown.Zero))
739       return Op.getOperand(0);
740     if (DemandedBits.isSubsetOf(LHSKnown.Zero))
741       return Op.getOperand(1);
742     break;
743   }
744   case ISD::SHL: {
745     // If we are only demanding sign bits then we can use the shift source
746     // directly.
747     if (const APInt *MaxSA =
748             DAG.getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
749       SDValue Op0 = Op.getOperand(0);
750       unsigned ShAmt = MaxSA->getZExtValue();
751       unsigned NumSignBits =
752           DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
753       unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
754       if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= (UpperDemandedBits))
755         return Op0;
756     }
757     break;
758   }
759   case ISD::SETCC: {
760     SDValue Op0 = Op.getOperand(0);
761     SDValue Op1 = Op.getOperand(1);
762     ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
763     // If (1) we only need the sign-bit, (2) the setcc operands are the same
764     // width as the setcc result, and (3) the result of a setcc conforms to 0 or
765     // -1, we may be able to bypass the setcc.
766     if (DemandedBits.isSignMask() &&
767         Op0.getScalarValueSizeInBits() == BitWidth &&
768         getBooleanContents(Op0.getValueType()) ==
769             BooleanContent::ZeroOrNegativeOneBooleanContent) {
770       // If we're testing X < 0, then this compare isn't needed - just use X!
771       // FIXME: We're limiting to integer types here, but this should also work
772       // if we don't care about FP signed-zero. The use of SETLT with FP means
773       // that we don't care about NaNs.
774       if (CC == ISD::SETLT && Op1.getValueType().isInteger() &&
775           (isNullConstant(Op1) || ISD::isBuildVectorAllZeros(Op1.getNode())))
776         return Op0;
777     }
778     break;
779   }
780   case ISD::SIGN_EXTEND_INREG: {
781     // If none of the extended bits are demanded, eliminate the sextinreg.
782     SDValue Op0 = Op.getOperand(0);
783     EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
784     unsigned ExBits = ExVT.getScalarSizeInBits();
785     if (DemandedBits.getActiveBits() <= ExBits)
786       return Op0;
787     // If the input is already sign extended, just drop the extension.
788     unsigned NumSignBits = DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
789     if (NumSignBits >= (BitWidth - ExBits + 1))
790       return Op0;
791     break;
792   }
793   case ISD::ANY_EXTEND_VECTOR_INREG:
794   case ISD::SIGN_EXTEND_VECTOR_INREG:
795   case ISD::ZERO_EXTEND_VECTOR_INREG: {
796     // If we only want the lowest element and none of extended bits, then we can
797     // return the bitcasted source vector.
798     SDValue Src = Op.getOperand(0);
799     EVT SrcVT = Src.getValueType();
800     EVT DstVT = Op.getValueType();
801     if (DemandedElts == 1 && DstVT.getSizeInBits() == SrcVT.getSizeInBits() &&
802         DAG.getDataLayout().isLittleEndian() &&
803         DemandedBits.getActiveBits() <= SrcVT.getScalarSizeInBits()) {
804       return DAG.getBitcast(DstVT, Src);
805     }
806     break;
807   }
808   case ISD::INSERT_VECTOR_ELT: {
809     // If we don't demand the inserted element, return the base vector.
810     SDValue Vec = Op.getOperand(0);
811     auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
812     EVT VecVT = Vec.getValueType();
813     if (CIdx && CIdx->getAPIntValue().ult(VecVT.getVectorNumElements()) &&
814         !DemandedElts[CIdx->getZExtValue()])
815       return Vec;
816     break;
817   }
818   case ISD::INSERT_SUBVECTOR: {
819     // If we don't demand the inserted subvector, return the base vector.
820     SDValue Vec = Op.getOperand(0);
821     SDValue Sub = Op.getOperand(1);
822     uint64_t Idx = Op.getConstantOperandVal(2);
823     unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
824     if (DemandedElts.extractBits(NumSubElts, Idx) == 0)
825       return Vec;
826     break;
827   }
828   case ISD::VECTOR_SHUFFLE: {
829     ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask();
830 
831     // If all the demanded elts are from one operand and are inline,
832     // then we can use the operand directly.
833     bool AllUndef = true, IdentityLHS = true, IdentityRHS = true;
834     for (unsigned i = 0; i != NumElts; ++i) {
835       int M = ShuffleMask[i];
836       if (M < 0 || !DemandedElts[i])
837         continue;
838       AllUndef = false;
839       IdentityLHS &= (M == (int)i);
840       IdentityRHS &= ((M - NumElts) == i);
841     }
842 
843     if (AllUndef)
844       return DAG.getUNDEF(Op.getValueType());
845     if (IdentityLHS)
846       return Op.getOperand(0);
847     if (IdentityRHS)
848       return Op.getOperand(1);
849     break;
850   }
851   default:
852     if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
853       if (SDValue V = SimplifyMultipleUseDemandedBitsForTargetNode(
854               Op, DemandedBits, DemandedElts, DAG, Depth))
855         return V;
856     break;
857   }
858   return SDValue();
859 }
860 
861 SDValue TargetLowering::SimplifyMultipleUseDemandedBits(
862     SDValue Op, const APInt &DemandedBits, SelectionDAG &DAG,
863     unsigned Depth) const {
864   EVT VT = Op.getValueType();
865   APInt DemandedElts = VT.isVector()
866                            ? APInt::getAllOnesValue(VT.getVectorNumElements())
867                            : APInt(1, 1);
868   return SimplifyMultipleUseDemandedBits(Op, DemandedBits, DemandedElts, DAG,
869                                          Depth);
870 }
871 
872 SDValue TargetLowering::SimplifyMultipleUseDemandedVectorElts(
873     SDValue Op, const APInt &DemandedElts, SelectionDAG &DAG,
874     unsigned Depth) const {
875   APInt DemandedBits = APInt::getAllOnesValue(Op.getScalarValueSizeInBits());
876   return SimplifyMultipleUseDemandedBits(Op, DemandedBits, DemandedElts, DAG,
877                                          Depth);
878 }
879 
880 /// Look at Op. At this point, we know that only the OriginalDemandedBits of the
881 /// result of Op are ever used downstream. If we can use this information to
882 /// simplify Op, create a new simplified DAG node and return true, returning the
883 /// original and new nodes in Old and New. Otherwise, analyze the expression and
884 /// return a mask of Known bits for the expression (used to simplify the
885 /// caller).  The Known bits may only be accurate for those bits in the
886 /// OriginalDemandedBits and OriginalDemandedElts.
887 bool TargetLowering::SimplifyDemandedBits(
888     SDValue Op, const APInt &OriginalDemandedBits,
889     const APInt &OriginalDemandedElts, KnownBits &Known, TargetLoweringOpt &TLO,
890     unsigned Depth, bool AssumeSingleUse) const {
891   unsigned BitWidth = OriginalDemandedBits.getBitWidth();
892   assert(Op.getScalarValueSizeInBits() == BitWidth &&
893          "Mask size mismatches value type size!");
894 
895   // Don't know anything.
896   Known = KnownBits(BitWidth);
897 
898   // TODO: We can probably do more work on calculating the known bits and
899   // simplifying the operations for scalable vectors, but for now we just
900   // bail out.
901   if (Op.getValueType().isScalableVector())
902     return false;
903 
904   unsigned NumElts = OriginalDemandedElts.getBitWidth();
905   assert((!Op.getValueType().isVector() ||
906           NumElts == Op.getValueType().getVectorNumElements()) &&
907          "Unexpected vector size");
908 
909   APInt DemandedBits = OriginalDemandedBits;
910   APInt DemandedElts = OriginalDemandedElts;
911   SDLoc dl(Op);
912   auto &DL = TLO.DAG.getDataLayout();
913 
914   // Undef operand.
915   if (Op.isUndef())
916     return false;
917 
918   if (Op.getOpcode() == ISD::Constant) {
919     // We know all of the bits for a constant!
920     Known = KnownBits::makeConstant(cast<ConstantSDNode>(Op)->getAPIntValue());
921     return false;
922   }
923 
924   if (Op.getOpcode() == ISD::ConstantFP) {
925     // We know all of the bits for a floating point constant!
926     Known = KnownBits::makeConstant(
927         cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt());
928     return false;
929   }
930 
931   // Other users may use these bits.
932   EVT VT = Op.getValueType();
933   if (!Op.getNode()->hasOneUse() && !AssumeSingleUse) {
934     if (Depth != 0) {
935       // If not at the root, Just compute the Known bits to
936       // simplify things downstream.
937       Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
938       return false;
939     }
940     // If this is the root being simplified, allow it to have multiple uses,
941     // just set the DemandedBits/Elts to all bits.
942     DemandedBits = APInt::getAllOnesValue(BitWidth);
943     DemandedElts = APInt::getAllOnesValue(NumElts);
944   } else if (OriginalDemandedBits == 0 || OriginalDemandedElts == 0) {
945     // Not demanding any bits/elts from Op.
946     return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
947   } else if (Depth >= SelectionDAG::MaxRecursionDepth) {
948     // Limit search depth.
949     return false;
950   }
951 
952   KnownBits Known2;
953   switch (Op.getOpcode()) {
954   case ISD::TargetConstant:
955     llvm_unreachable("Can't simplify this node");
956   case ISD::SCALAR_TO_VECTOR: {
957     if (!DemandedElts[0])
958       return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
959 
960     KnownBits SrcKnown;
961     SDValue Src = Op.getOperand(0);
962     unsigned SrcBitWidth = Src.getScalarValueSizeInBits();
963     APInt SrcDemandedBits = DemandedBits.zextOrSelf(SrcBitWidth);
964     if (SimplifyDemandedBits(Src, SrcDemandedBits, SrcKnown, TLO, Depth + 1))
965       return true;
966 
967     // Upper elements are undef, so only get the knownbits if we just demand
968     // the bottom element.
969     if (DemandedElts == 1)
970       Known = SrcKnown.anyextOrTrunc(BitWidth);
971     break;
972   }
973   case ISD::BUILD_VECTOR:
974     // Collect the known bits that are shared by every demanded element.
975     // TODO: Call SimplifyDemandedBits for non-constant demanded elements.
976     Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
977     return false; // Don't fall through, will infinitely loop.
978   case ISD::LOAD: {
979     auto *LD = cast<LoadSDNode>(Op);
980     if (getTargetConstantFromLoad(LD)) {
981       Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
982       return false; // Don't fall through, will infinitely loop.
983     }
984     if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
985       // If this is a ZEXTLoad and we are looking at the loaded value.
986       EVT MemVT = LD->getMemoryVT();
987       unsigned MemBits = MemVT.getScalarSizeInBits();
988       Known.Zero.setBitsFrom(MemBits);
989       return false; // Don't fall through, will infinitely loop.
990     }
991     break;
992   }
993   case ISD::INSERT_VECTOR_ELT: {
994     SDValue Vec = Op.getOperand(0);
995     SDValue Scl = Op.getOperand(1);
996     auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
997     EVT VecVT = Vec.getValueType();
998 
999     // If index isn't constant, assume we need all vector elements AND the
1000     // inserted element.
1001     APInt DemandedVecElts(DemandedElts);
1002     if (CIdx && CIdx->getAPIntValue().ult(VecVT.getVectorNumElements())) {
1003       unsigned Idx = CIdx->getZExtValue();
1004       DemandedVecElts.clearBit(Idx);
1005 
1006       // Inserted element is not required.
1007       if (!DemandedElts[Idx])
1008         return TLO.CombineTo(Op, Vec);
1009     }
1010 
1011     KnownBits KnownScl;
1012     unsigned NumSclBits = Scl.getScalarValueSizeInBits();
1013     APInt DemandedSclBits = DemandedBits.zextOrTrunc(NumSclBits);
1014     if (SimplifyDemandedBits(Scl, DemandedSclBits, KnownScl, TLO, Depth + 1))
1015       return true;
1016 
1017     Known = KnownScl.anyextOrTrunc(BitWidth);
1018 
1019     KnownBits KnownVec;
1020     if (SimplifyDemandedBits(Vec, DemandedBits, DemandedVecElts, KnownVec, TLO,
1021                              Depth + 1))
1022       return true;
1023 
1024     if (!!DemandedVecElts)
1025       Known = KnownBits::commonBits(Known, KnownVec);
1026 
1027     return false;
1028   }
1029   case ISD::INSERT_SUBVECTOR: {
1030     // Demand any elements from the subvector and the remainder from the src its
1031     // inserted into.
1032     SDValue Src = Op.getOperand(0);
1033     SDValue Sub = Op.getOperand(1);
1034     uint64_t Idx = Op.getConstantOperandVal(2);
1035     unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
1036     APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
1037     APInt DemandedSrcElts = DemandedElts;
1038     DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx);
1039 
1040     KnownBits KnownSub, KnownSrc;
1041     if (SimplifyDemandedBits(Sub, DemandedBits, DemandedSubElts, KnownSub, TLO,
1042                              Depth + 1))
1043       return true;
1044     if (SimplifyDemandedBits(Src, DemandedBits, DemandedSrcElts, KnownSrc, TLO,
1045                              Depth + 1))
1046       return true;
1047 
1048     Known.Zero.setAllBits();
1049     Known.One.setAllBits();
1050     if (!!DemandedSubElts)
1051       Known = KnownBits::commonBits(Known, KnownSub);
1052     if (!!DemandedSrcElts)
1053       Known = KnownBits::commonBits(Known, KnownSrc);
1054 
1055     // Attempt to avoid multi-use src if we don't need anything from it.
1056     if (!DemandedBits.isAllOnesValue() || !DemandedSubElts.isAllOnesValue() ||
1057         !DemandedSrcElts.isAllOnesValue()) {
1058       SDValue NewSub = SimplifyMultipleUseDemandedBits(
1059           Sub, DemandedBits, DemandedSubElts, TLO.DAG, Depth + 1);
1060       SDValue NewSrc = SimplifyMultipleUseDemandedBits(
1061           Src, DemandedBits, DemandedSrcElts, TLO.DAG, Depth + 1);
1062       if (NewSub || NewSrc) {
1063         NewSub = NewSub ? NewSub : Sub;
1064         NewSrc = NewSrc ? NewSrc : Src;
1065         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc, NewSub,
1066                                         Op.getOperand(2));
1067         return TLO.CombineTo(Op, NewOp);
1068       }
1069     }
1070     break;
1071   }
1072   case ISD::EXTRACT_SUBVECTOR: {
1073     // Offset the demanded elts by the subvector index.
1074     SDValue Src = Op.getOperand(0);
1075     if (Src.getValueType().isScalableVector())
1076       break;
1077     uint64_t Idx = Op.getConstantOperandVal(1);
1078     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
1079     APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
1080 
1081     if (SimplifyDemandedBits(Src, DemandedBits, DemandedSrcElts, Known, TLO,
1082                              Depth + 1))
1083       return true;
1084 
1085     // Attempt to avoid multi-use src if we don't need anything from it.
1086     if (!DemandedBits.isAllOnesValue() || !DemandedSrcElts.isAllOnesValue()) {
1087       SDValue DemandedSrc = SimplifyMultipleUseDemandedBits(
1088           Src, DemandedBits, DemandedSrcElts, TLO.DAG, Depth + 1);
1089       if (DemandedSrc) {
1090         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, DemandedSrc,
1091                                         Op.getOperand(1));
1092         return TLO.CombineTo(Op, NewOp);
1093       }
1094     }
1095     break;
1096   }
1097   case ISD::CONCAT_VECTORS: {
1098     Known.Zero.setAllBits();
1099     Known.One.setAllBits();
1100     EVT SubVT = Op.getOperand(0).getValueType();
1101     unsigned NumSubVecs = Op.getNumOperands();
1102     unsigned NumSubElts = SubVT.getVectorNumElements();
1103     for (unsigned i = 0; i != NumSubVecs; ++i) {
1104       APInt DemandedSubElts =
1105           DemandedElts.extractBits(NumSubElts, i * NumSubElts);
1106       if (SimplifyDemandedBits(Op.getOperand(i), DemandedBits, DemandedSubElts,
1107                                Known2, TLO, Depth + 1))
1108         return true;
1109       // Known bits are shared by every demanded subvector element.
1110       if (!!DemandedSubElts)
1111         Known = KnownBits::commonBits(Known, Known2);
1112     }
1113     break;
1114   }
1115   case ISD::VECTOR_SHUFFLE: {
1116     ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask();
1117 
1118     // Collect demanded elements from shuffle operands..
1119     APInt DemandedLHS(NumElts, 0);
1120     APInt DemandedRHS(NumElts, 0);
1121     for (unsigned i = 0; i != NumElts; ++i) {
1122       if (!DemandedElts[i])
1123         continue;
1124       int M = ShuffleMask[i];
1125       if (M < 0) {
1126         // For UNDEF elements, we don't know anything about the common state of
1127         // the shuffle result.
1128         DemandedLHS.clearAllBits();
1129         DemandedRHS.clearAllBits();
1130         break;
1131       }
1132       assert(0 <= M && M < (int)(2 * NumElts) && "Shuffle index out of range");
1133       if (M < (int)NumElts)
1134         DemandedLHS.setBit(M);
1135       else
1136         DemandedRHS.setBit(M - NumElts);
1137     }
1138 
1139     if (!!DemandedLHS || !!DemandedRHS) {
1140       SDValue Op0 = Op.getOperand(0);
1141       SDValue Op1 = Op.getOperand(1);
1142 
1143       Known.Zero.setAllBits();
1144       Known.One.setAllBits();
1145       if (!!DemandedLHS) {
1146         if (SimplifyDemandedBits(Op0, DemandedBits, DemandedLHS, Known2, TLO,
1147                                  Depth + 1))
1148           return true;
1149         Known = KnownBits::commonBits(Known, Known2);
1150       }
1151       if (!!DemandedRHS) {
1152         if (SimplifyDemandedBits(Op1, DemandedBits, DemandedRHS, Known2, TLO,
1153                                  Depth + 1))
1154           return true;
1155         Known = KnownBits::commonBits(Known, Known2);
1156       }
1157 
1158       // Attempt to avoid multi-use ops if we don't need anything from them.
1159       SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits(
1160           Op0, DemandedBits, DemandedLHS, TLO.DAG, Depth + 1);
1161       SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits(
1162           Op1, DemandedBits, DemandedRHS, TLO.DAG, Depth + 1);
1163       if (DemandedOp0 || DemandedOp1) {
1164         Op0 = DemandedOp0 ? DemandedOp0 : Op0;
1165         Op1 = DemandedOp1 ? DemandedOp1 : Op1;
1166         SDValue NewOp = TLO.DAG.getVectorShuffle(VT, dl, Op0, Op1, ShuffleMask);
1167         return TLO.CombineTo(Op, NewOp);
1168       }
1169     }
1170     break;
1171   }
1172   case ISD::AND: {
1173     SDValue Op0 = Op.getOperand(0);
1174     SDValue Op1 = Op.getOperand(1);
1175 
1176     // If the RHS is a constant, check to see if the LHS would be zero without
1177     // using the bits from the RHS.  Below, we use knowledge about the RHS to
1178     // simplify the LHS, here we're using information from the LHS to simplify
1179     // the RHS.
1180     if (ConstantSDNode *RHSC = isConstOrConstSplat(Op1)) {
1181       // Do not increment Depth here; that can cause an infinite loop.
1182       KnownBits LHSKnown = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth);
1183       // If the LHS already has zeros where RHSC does, this 'and' is dead.
1184       if ((LHSKnown.Zero & DemandedBits) ==
1185           (~RHSC->getAPIntValue() & DemandedBits))
1186         return TLO.CombineTo(Op, Op0);
1187 
1188       // If any of the set bits in the RHS are known zero on the LHS, shrink
1189       // the constant.
1190       if (ShrinkDemandedConstant(Op, ~LHSKnown.Zero & DemandedBits,
1191                                  DemandedElts, TLO))
1192         return true;
1193 
1194       // Bitwise-not (xor X, -1) is a special case: we don't usually shrink its
1195       // constant, but if this 'and' is only clearing bits that were just set by
1196       // the xor, then this 'and' can be eliminated by shrinking the mask of
1197       // the xor. For example, for a 32-bit X:
1198       // and (xor (srl X, 31), -1), 1 --> xor (srl X, 31), 1
1199       if (isBitwiseNot(Op0) && Op0.hasOneUse() &&
1200           LHSKnown.One == ~RHSC->getAPIntValue()) {
1201         SDValue Xor = TLO.DAG.getNode(ISD::XOR, dl, VT, Op0.getOperand(0), Op1);
1202         return TLO.CombineTo(Op, Xor);
1203       }
1204     }
1205 
1206     if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO,
1207                              Depth + 1))
1208       return true;
1209     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1210     if (SimplifyDemandedBits(Op0, ~Known.Zero & DemandedBits, DemandedElts,
1211                              Known2, TLO, Depth + 1))
1212       return true;
1213     assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
1214 
1215     // Attempt to avoid multi-use ops if we don't need anything from them.
1216     if (!DemandedBits.isAllOnesValue() || !DemandedElts.isAllOnesValue()) {
1217       SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits(
1218           Op0, DemandedBits, DemandedElts, TLO.DAG, Depth + 1);
1219       SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits(
1220           Op1, DemandedBits, DemandedElts, TLO.DAG, Depth + 1);
1221       if (DemandedOp0 || DemandedOp1) {
1222         Op0 = DemandedOp0 ? DemandedOp0 : Op0;
1223         Op1 = DemandedOp1 ? DemandedOp1 : Op1;
1224         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1);
1225         return TLO.CombineTo(Op, NewOp);
1226       }
1227     }
1228 
1229     // If all of the demanded bits are known one on one side, return the other.
1230     // These bits cannot contribute to the result of the 'and'.
1231     if (DemandedBits.isSubsetOf(Known2.Zero | Known.One))
1232       return TLO.CombineTo(Op, Op0);
1233     if (DemandedBits.isSubsetOf(Known.Zero | Known2.One))
1234       return TLO.CombineTo(Op, Op1);
1235     // If all of the demanded bits in the inputs are known zeros, return zero.
1236     if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero))
1237       return TLO.CombineTo(Op, TLO.DAG.getConstant(0, dl, VT));
1238     // If the RHS is a constant, see if we can simplify it.
1239     if (ShrinkDemandedConstant(Op, ~Known2.Zero & DemandedBits, DemandedElts,
1240                                TLO))
1241       return true;
1242     // If the operation can be done in a smaller type, do so.
1243     if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
1244       return true;
1245 
1246     Known &= Known2;
1247     break;
1248   }
1249   case ISD::OR: {
1250     SDValue Op0 = Op.getOperand(0);
1251     SDValue Op1 = Op.getOperand(1);
1252 
1253     if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO,
1254                              Depth + 1))
1255       return true;
1256     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1257     if (SimplifyDemandedBits(Op0, ~Known.One & DemandedBits, DemandedElts,
1258                              Known2, TLO, Depth + 1))
1259       return true;
1260     assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
1261 
1262     // Attempt to avoid multi-use ops if we don't need anything from them.
1263     if (!DemandedBits.isAllOnesValue() || !DemandedElts.isAllOnesValue()) {
1264       SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits(
1265           Op0, DemandedBits, DemandedElts, TLO.DAG, Depth + 1);
1266       SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits(
1267           Op1, DemandedBits, DemandedElts, TLO.DAG, Depth + 1);
1268       if (DemandedOp0 || DemandedOp1) {
1269         Op0 = DemandedOp0 ? DemandedOp0 : Op0;
1270         Op1 = DemandedOp1 ? DemandedOp1 : Op1;
1271         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1);
1272         return TLO.CombineTo(Op, NewOp);
1273       }
1274     }
1275 
1276     // If all of the demanded bits are known zero on one side, return the other.
1277     // These bits cannot contribute to the result of the 'or'.
1278     if (DemandedBits.isSubsetOf(Known2.One | Known.Zero))
1279       return TLO.CombineTo(Op, Op0);
1280     if (DemandedBits.isSubsetOf(Known.One | Known2.Zero))
1281       return TLO.CombineTo(Op, Op1);
1282     // If the RHS is a constant, see if we can simplify it.
1283     if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
1284       return true;
1285     // If the operation can be done in a smaller type, do so.
1286     if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
1287       return true;
1288 
1289     Known |= Known2;
1290     break;
1291   }
1292   case ISD::XOR: {
1293     SDValue Op0 = Op.getOperand(0);
1294     SDValue Op1 = Op.getOperand(1);
1295 
1296     if (SimplifyDemandedBits(Op1, DemandedBits, DemandedElts, Known, TLO,
1297                              Depth + 1))
1298       return true;
1299     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1300     if (SimplifyDemandedBits(Op0, DemandedBits, DemandedElts, Known2, TLO,
1301                              Depth + 1))
1302       return true;
1303     assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
1304 
1305     // Attempt to avoid multi-use ops if we don't need anything from them.
1306     if (!DemandedBits.isAllOnesValue() || !DemandedElts.isAllOnesValue()) {
1307       SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits(
1308           Op0, DemandedBits, DemandedElts, TLO.DAG, Depth + 1);
1309       SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits(
1310           Op1, DemandedBits, DemandedElts, TLO.DAG, Depth + 1);
1311       if (DemandedOp0 || DemandedOp1) {
1312         Op0 = DemandedOp0 ? DemandedOp0 : Op0;
1313         Op1 = DemandedOp1 ? DemandedOp1 : Op1;
1314         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1);
1315         return TLO.CombineTo(Op, NewOp);
1316       }
1317     }
1318 
1319     // If all of the demanded bits are known zero on one side, return the other.
1320     // These bits cannot contribute to the result of the 'xor'.
1321     if (DemandedBits.isSubsetOf(Known.Zero))
1322       return TLO.CombineTo(Op, Op0);
1323     if (DemandedBits.isSubsetOf(Known2.Zero))
1324       return TLO.CombineTo(Op, Op1);
1325     // If the operation can be done in a smaller type, do so.
1326     if (ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
1327       return true;
1328 
1329     // If all of the unknown bits are known to be zero on one side or the other
1330     // turn this into an *inclusive* or.
1331     //    e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
1332     if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero))
1333       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, VT, Op0, Op1));
1334 
1335     ConstantSDNode* C = isConstOrConstSplat(Op1, DemandedElts);
1336     if (C) {
1337       // If one side is a constant, and all of the set bits in the constant are
1338       // also known set on the other side, turn this into an AND, as we know
1339       // the bits will be cleared.
1340       //    e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
1341       // NB: it is okay if more bits are known than are requested
1342       if (C->getAPIntValue() == Known2.One) {
1343         SDValue ANDC =
1344             TLO.DAG.getConstant(~C->getAPIntValue() & DemandedBits, dl, VT);
1345         return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, Op0, ANDC));
1346       }
1347 
1348       // If the RHS is a constant, see if we can change it. Don't alter a -1
1349       // constant because that's a 'not' op, and that is better for combining
1350       // and codegen.
1351       if (!C->isAllOnesValue() &&
1352           DemandedBits.isSubsetOf(C->getAPIntValue())) {
1353         // We're flipping all demanded bits. Flip the undemanded bits too.
1354         SDValue New = TLO.DAG.getNOT(dl, Op0, VT);
1355         return TLO.CombineTo(Op, New);
1356       }
1357     }
1358 
1359     // If we can't turn this into a 'not', try to shrink the constant.
1360     if (!C || !C->isAllOnesValue())
1361       if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
1362         return true;
1363 
1364     Known ^= Known2;
1365     break;
1366   }
1367   case ISD::SELECT:
1368     if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, Known, TLO,
1369                              Depth + 1))
1370       return true;
1371     if (SimplifyDemandedBits(Op.getOperand(1), DemandedBits, Known2, TLO,
1372                              Depth + 1))
1373       return true;
1374     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1375     assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
1376 
1377     // If the operands are constants, see if we can simplify them.
1378     if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
1379       return true;
1380 
1381     // Only known if known in both the LHS and RHS.
1382     Known = KnownBits::commonBits(Known, Known2);
1383     break;
1384   case ISD::SELECT_CC:
1385     if (SimplifyDemandedBits(Op.getOperand(3), DemandedBits, Known, TLO,
1386                              Depth + 1))
1387       return true;
1388     if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, Known2, TLO,
1389                              Depth + 1))
1390       return true;
1391     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1392     assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
1393 
1394     // If the operands are constants, see if we can simplify them.
1395     if (ShrinkDemandedConstant(Op, DemandedBits, DemandedElts, TLO))
1396       return true;
1397 
1398     // Only known if known in both the LHS and RHS.
1399     Known = KnownBits::commonBits(Known, Known2);
1400     break;
1401   case ISD::SETCC: {
1402     SDValue Op0 = Op.getOperand(0);
1403     SDValue Op1 = Op.getOperand(1);
1404     ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1405     // If (1) we only need the sign-bit, (2) the setcc operands are the same
1406     // width as the setcc result, and (3) the result of a setcc conforms to 0 or
1407     // -1, we may be able to bypass the setcc.
1408     if (DemandedBits.isSignMask() &&
1409         Op0.getScalarValueSizeInBits() == BitWidth &&
1410         getBooleanContents(Op0.getValueType()) ==
1411             BooleanContent::ZeroOrNegativeOneBooleanContent) {
1412       // If we're testing X < 0, then this compare isn't needed - just use X!
1413       // FIXME: We're limiting to integer types here, but this should also work
1414       // if we don't care about FP signed-zero. The use of SETLT with FP means
1415       // that we don't care about NaNs.
1416       if (CC == ISD::SETLT && Op1.getValueType().isInteger() &&
1417           (isNullConstant(Op1) || ISD::isBuildVectorAllZeros(Op1.getNode())))
1418         return TLO.CombineTo(Op, Op0);
1419 
1420       // TODO: Should we check for other forms of sign-bit comparisons?
1421       // Examples: X <= -1, X >= 0
1422     }
1423     if (getBooleanContents(Op0.getValueType()) ==
1424             TargetLowering::ZeroOrOneBooleanContent &&
1425         BitWidth > 1)
1426       Known.Zero.setBitsFrom(1);
1427     break;
1428   }
1429   case ISD::SHL: {
1430     SDValue Op0 = Op.getOperand(0);
1431     SDValue Op1 = Op.getOperand(1);
1432     EVT ShiftVT = Op1.getValueType();
1433 
1434     if (const APInt *SA =
1435             TLO.DAG.getValidShiftAmountConstant(Op, DemandedElts)) {
1436       unsigned ShAmt = SA->getZExtValue();
1437       if (ShAmt == 0)
1438         return TLO.CombineTo(Op, Op0);
1439 
1440       // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
1441       // single shift.  We can do this if the bottom bits (which are shifted
1442       // out) are never demanded.
1443       // TODO - support non-uniform vector amounts.
1444       if (Op0.getOpcode() == ISD::SRL) {
1445         if (!DemandedBits.intersects(APInt::getLowBitsSet(BitWidth, ShAmt))) {
1446           if (const APInt *SA2 =
1447                   TLO.DAG.getValidShiftAmountConstant(Op0, DemandedElts)) {
1448             unsigned C1 = SA2->getZExtValue();
1449             unsigned Opc = ISD::SHL;
1450             int Diff = ShAmt - C1;
1451             if (Diff < 0) {
1452               Diff = -Diff;
1453               Opc = ISD::SRL;
1454             }
1455             SDValue NewSA = TLO.DAG.getConstant(Diff, dl, ShiftVT);
1456             return TLO.CombineTo(
1457                 Op, TLO.DAG.getNode(Opc, dl, VT, Op0.getOperand(0), NewSA));
1458           }
1459         }
1460       }
1461 
1462       // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
1463       // are not demanded. This will likely allow the anyext to be folded away.
1464       // TODO - support non-uniform vector amounts.
1465       if (Op0.getOpcode() == ISD::ANY_EXTEND) {
1466         SDValue InnerOp = Op0.getOperand(0);
1467         EVT InnerVT = InnerOp.getValueType();
1468         unsigned InnerBits = InnerVT.getScalarSizeInBits();
1469         if (ShAmt < InnerBits && DemandedBits.getActiveBits() <= InnerBits &&
1470             isTypeDesirableForOp(ISD::SHL, InnerVT)) {
1471           EVT ShTy = getShiftAmountTy(InnerVT, DL);
1472           if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
1473             ShTy = InnerVT;
1474           SDValue NarrowShl =
1475               TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
1476                               TLO.DAG.getConstant(ShAmt, dl, ShTy));
1477           return TLO.CombineTo(
1478               Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT, NarrowShl));
1479         }
1480 
1481         // Repeat the SHL optimization above in cases where an extension
1482         // intervenes: (shl (anyext (shr x, c1)), c2) to
1483         // (shl (anyext x), c2-c1).  This requires that the bottom c1 bits
1484         // aren't demanded (as above) and that the shifted upper c1 bits of
1485         // x aren't demanded.
1486         // TODO - support non-uniform vector amounts.
1487         if (Op0.hasOneUse() && InnerOp.getOpcode() == ISD::SRL &&
1488             InnerOp.hasOneUse()) {
1489           if (const APInt *SA2 =
1490                   TLO.DAG.getValidShiftAmountConstant(InnerOp, DemandedElts)) {
1491             unsigned InnerShAmt = SA2->getZExtValue();
1492             if (InnerShAmt < ShAmt && InnerShAmt < InnerBits &&
1493                 DemandedBits.getActiveBits() <=
1494                     (InnerBits - InnerShAmt + ShAmt) &&
1495                 DemandedBits.countTrailingZeros() >= ShAmt) {
1496               SDValue NewSA =
1497                   TLO.DAG.getConstant(ShAmt - InnerShAmt, dl, ShiftVT);
1498               SDValue NewExt = TLO.DAG.getNode(ISD::ANY_EXTEND, dl, VT,
1499                                                InnerOp.getOperand(0));
1500               return TLO.CombineTo(
1501                   Op, TLO.DAG.getNode(ISD::SHL, dl, VT, NewExt, NewSA));
1502             }
1503           }
1504         }
1505       }
1506 
1507       APInt InDemandedMask = DemandedBits.lshr(ShAmt);
1508       if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
1509                                Depth + 1))
1510         return true;
1511       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1512       Known.Zero <<= ShAmt;
1513       Known.One <<= ShAmt;
1514       // low bits known zero.
1515       Known.Zero.setLowBits(ShAmt);
1516 
1517       // Try shrinking the operation as long as the shift amount will still be
1518       // in range.
1519       if ((ShAmt < DemandedBits.getActiveBits()) &&
1520           ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO))
1521         return true;
1522     }
1523 
1524     // If we are only demanding sign bits then we can use the shift source
1525     // directly.
1526     if (const APInt *MaxSA =
1527             TLO.DAG.getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
1528       unsigned ShAmt = MaxSA->getZExtValue();
1529       unsigned NumSignBits =
1530           TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
1531       unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
1532       if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= (UpperDemandedBits))
1533         return TLO.CombineTo(Op, Op0);
1534     }
1535     break;
1536   }
1537   case ISD::SRL: {
1538     SDValue Op0 = Op.getOperand(0);
1539     SDValue Op1 = Op.getOperand(1);
1540     EVT ShiftVT = Op1.getValueType();
1541 
1542     if (const APInt *SA =
1543             TLO.DAG.getValidShiftAmountConstant(Op, DemandedElts)) {
1544       unsigned ShAmt = SA->getZExtValue();
1545       if (ShAmt == 0)
1546         return TLO.CombineTo(Op, Op0);
1547 
1548       // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
1549       // single shift.  We can do this if the top bits (which are shifted out)
1550       // are never demanded.
1551       // TODO - support non-uniform vector amounts.
1552       if (Op0.getOpcode() == ISD::SHL) {
1553         if (!DemandedBits.intersects(APInt::getHighBitsSet(BitWidth, ShAmt))) {
1554           if (const APInt *SA2 =
1555                   TLO.DAG.getValidShiftAmountConstant(Op0, DemandedElts)) {
1556             unsigned C1 = SA2->getZExtValue();
1557             unsigned Opc = ISD::SRL;
1558             int Diff = ShAmt - C1;
1559             if (Diff < 0) {
1560               Diff = -Diff;
1561               Opc = ISD::SHL;
1562             }
1563             SDValue NewSA = TLO.DAG.getConstant(Diff, dl, ShiftVT);
1564             return TLO.CombineTo(
1565                 Op, TLO.DAG.getNode(Opc, dl, VT, Op0.getOperand(0), NewSA));
1566           }
1567         }
1568       }
1569 
1570       APInt InDemandedMask = (DemandedBits << ShAmt);
1571 
1572       // If the shift is exact, then it does demand the low bits (and knows that
1573       // they are zero).
1574       if (Op->getFlags().hasExact())
1575         InDemandedMask.setLowBits(ShAmt);
1576 
1577       // Compute the new bits that are at the top now.
1578       if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
1579                                Depth + 1))
1580         return true;
1581       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1582       Known.Zero.lshrInPlace(ShAmt);
1583       Known.One.lshrInPlace(ShAmt);
1584       // High bits known zero.
1585       Known.Zero.setHighBits(ShAmt);
1586     }
1587     break;
1588   }
1589   case ISD::SRA: {
1590     SDValue Op0 = Op.getOperand(0);
1591     SDValue Op1 = Op.getOperand(1);
1592     EVT ShiftVT = Op1.getValueType();
1593 
1594     // If we only want bits that already match the signbit then we don't need
1595     // to shift.
1596     unsigned NumHiDemandedBits = BitWidth - DemandedBits.countTrailingZeros();
1597     if (TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1) >=
1598         NumHiDemandedBits)
1599       return TLO.CombineTo(Op, Op0);
1600 
1601     // If this is an arithmetic shift right and only the low-bit is set, we can
1602     // always convert this into a logical shr, even if the shift amount is
1603     // variable.  The low bit of the shift cannot be an input sign bit unless
1604     // the shift amount is >= the size of the datatype, which is undefined.
1605     if (DemandedBits.isOneValue())
1606       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1));
1607 
1608     if (const APInt *SA =
1609             TLO.DAG.getValidShiftAmountConstant(Op, DemandedElts)) {
1610       unsigned ShAmt = SA->getZExtValue();
1611       if (ShAmt == 0)
1612         return TLO.CombineTo(Op, Op0);
1613 
1614       APInt InDemandedMask = (DemandedBits << ShAmt);
1615 
1616       // If the shift is exact, then it does demand the low bits (and knows that
1617       // they are zero).
1618       if (Op->getFlags().hasExact())
1619         InDemandedMask.setLowBits(ShAmt);
1620 
1621       // If any of the demanded bits are produced by the sign extension, we also
1622       // demand the input sign bit.
1623       if (DemandedBits.countLeadingZeros() < ShAmt)
1624         InDemandedMask.setSignBit();
1625 
1626       if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
1627                                Depth + 1))
1628         return true;
1629       assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1630       Known.Zero.lshrInPlace(ShAmt);
1631       Known.One.lshrInPlace(ShAmt);
1632 
1633       // If the input sign bit is known to be zero, or if none of the top bits
1634       // are demanded, turn this into an unsigned shift right.
1635       if (Known.Zero[BitWidth - ShAmt - 1] ||
1636           DemandedBits.countLeadingZeros() >= ShAmt) {
1637         SDNodeFlags Flags;
1638         Flags.setExact(Op->getFlags().hasExact());
1639         return TLO.CombineTo(
1640             Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, Op1, Flags));
1641       }
1642 
1643       int Log2 = DemandedBits.exactLogBase2();
1644       if (Log2 >= 0) {
1645         // The bit must come from the sign.
1646         SDValue NewSA = TLO.DAG.getConstant(BitWidth - 1 - Log2, dl, ShiftVT);
1647         return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT, Op0, NewSA));
1648       }
1649 
1650       if (Known.One[BitWidth - ShAmt - 1])
1651         // New bits are known one.
1652         Known.One.setHighBits(ShAmt);
1653 
1654       // Attempt to avoid multi-use ops if we don't need anything from them.
1655       if (!InDemandedMask.isAllOnesValue() || !DemandedElts.isAllOnesValue()) {
1656         SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits(
1657             Op0, InDemandedMask, DemandedElts, TLO.DAG, Depth + 1);
1658         if (DemandedOp0) {
1659           SDValue NewOp = TLO.DAG.getNode(ISD::SRA, dl, VT, DemandedOp0, Op1);
1660           return TLO.CombineTo(Op, NewOp);
1661         }
1662       }
1663     }
1664     break;
1665   }
1666   case ISD::FSHL:
1667   case ISD::FSHR: {
1668     SDValue Op0 = Op.getOperand(0);
1669     SDValue Op1 = Op.getOperand(1);
1670     SDValue Op2 = Op.getOperand(2);
1671     bool IsFSHL = (Op.getOpcode() == ISD::FSHL);
1672 
1673     if (ConstantSDNode *SA = isConstOrConstSplat(Op2, DemandedElts)) {
1674       unsigned Amt = SA->getAPIntValue().urem(BitWidth);
1675 
1676       // For fshl, 0-shift returns the 1st arg.
1677       // For fshr, 0-shift returns the 2nd arg.
1678       if (Amt == 0) {
1679         if (SimplifyDemandedBits(IsFSHL ? Op0 : Op1, DemandedBits, DemandedElts,
1680                                  Known, TLO, Depth + 1))
1681           return true;
1682         break;
1683       }
1684 
1685       // fshl: (Op0 << Amt) | (Op1 >> (BW - Amt))
1686       // fshr: (Op0 << (BW - Amt)) | (Op1 >> Amt)
1687       APInt Demanded0 = DemandedBits.lshr(IsFSHL ? Amt : (BitWidth - Amt));
1688       APInt Demanded1 = DemandedBits << (IsFSHL ? (BitWidth - Amt) : Amt);
1689       if (SimplifyDemandedBits(Op0, Demanded0, DemandedElts, Known2, TLO,
1690                                Depth + 1))
1691         return true;
1692       if (SimplifyDemandedBits(Op1, Demanded1, DemandedElts, Known, TLO,
1693                                Depth + 1))
1694         return true;
1695 
1696       Known2.One <<= (IsFSHL ? Amt : (BitWidth - Amt));
1697       Known2.Zero <<= (IsFSHL ? Amt : (BitWidth - Amt));
1698       Known.One.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt);
1699       Known.Zero.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt);
1700       Known.One |= Known2.One;
1701       Known.Zero |= Known2.Zero;
1702     }
1703 
1704     // For pow-2 bitwidths we only demand the bottom modulo amt bits.
1705     if (isPowerOf2_32(BitWidth)) {
1706       APInt DemandedAmtBits(Op2.getScalarValueSizeInBits(), BitWidth - 1);
1707       if (SimplifyDemandedBits(Op2, DemandedAmtBits, DemandedElts,
1708                                Known2, TLO, Depth + 1))
1709         return true;
1710     }
1711     break;
1712   }
1713   case ISD::ROTL:
1714   case ISD::ROTR: {
1715     SDValue Op0 = Op.getOperand(0);
1716     SDValue Op1 = Op.getOperand(1);
1717 
1718     // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
1719     if (BitWidth == TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1))
1720       return TLO.CombineTo(Op, Op0);
1721 
1722     // For pow-2 bitwidths we only demand the bottom modulo amt bits.
1723     if (isPowerOf2_32(BitWidth)) {
1724       APInt DemandedAmtBits(Op1.getScalarValueSizeInBits(), BitWidth - 1);
1725       if (SimplifyDemandedBits(Op1, DemandedAmtBits, DemandedElts, Known2, TLO,
1726                                Depth + 1))
1727         return true;
1728     }
1729     break;
1730   }
1731   case ISD::UMIN: {
1732     // Check if one arg is always less than (or equal) to the other arg.
1733     SDValue Op0 = Op.getOperand(0);
1734     SDValue Op1 = Op.getOperand(1);
1735     KnownBits Known0 = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth + 1);
1736     KnownBits Known1 = TLO.DAG.computeKnownBits(Op1, DemandedElts, Depth + 1);
1737     Known = KnownBits::umin(Known0, Known1);
1738     if (Optional<bool> IsULE = KnownBits::ule(Known0, Known1))
1739       return TLO.CombineTo(Op, IsULE.getValue() ? Op0 : Op1);
1740     if (Optional<bool> IsULT = KnownBits::ult(Known0, Known1))
1741       return TLO.CombineTo(Op, IsULT.getValue() ? Op0 : Op1);
1742     break;
1743   }
1744   case ISD::UMAX: {
1745     // Check if one arg is always greater than (or equal) to the other arg.
1746     SDValue Op0 = Op.getOperand(0);
1747     SDValue Op1 = Op.getOperand(1);
1748     KnownBits Known0 = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth + 1);
1749     KnownBits Known1 = TLO.DAG.computeKnownBits(Op1, DemandedElts, Depth + 1);
1750     Known = KnownBits::umax(Known0, Known1);
1751     if (Optional<bool> IsUGE = KnownBits::uge(Known0, Known1))
1752       return TLO.CombineTo(Op, IsUGE.getValue() ? Op0 : Op1);
1753     if (Optional<bool> IsUGT = KnownBits::ugt(Known0, Known1))
1754       return TLO.CombineTo(Op, IsUGT.getValue() ? Op0 : Op1);
1755     break;
1756   }
1757   case ISD::BITREVERSE: {
1758     SDValue Src = Op.getOperand(0);
1759     APInt DemandedSrcBits = DemandedBits.reverseBits();
1760     if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedElts, Known2, TLO,
1761                              Depth + 1))
1762       return true;
1763     Known.One = Known2.One.reverseBits();
1764     Known.Zero = Known2.Zero.reverseBits();
1765     break;
1766   }
1767   case ISD::BSWAP: {
1768     SDValue Src = Op.getOperand(0);
1769     APInt DemandedSrcBits = DemandedBits.byteSwap();
1770     if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedElts, Known2, TLO,
1771                              Depth + 1))
1772       return true;
1773     Known.One = Known2.One.byteSwap();
1774     Known.Zero = Known2.Zero.byteSwap();
1775     break;
1776   }
1777   case ISD::CTPOP: {
1778     // If only 1 bit is demanded, replace with PARITY as long as we're before
1779     // op legalization.
1780     // FIXME: Limit to scalars for now.
1781     if (DemandedBits.isOneValue() && !TLO.LegalOps && !VT.isVector())
1782       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::PARITY, dl, VT,
1783                                                Op.getOperand(0)));
1784 
1785     Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
1786     break;
1787   }
1788   case ISD::SIGN_EXTEND_INREG: {
1789     SDValue Op0 = Op.getOperand(0);
1790     EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1791     unsigned ExVTBits = ExVT.getScalarSizeInBits();
1792 
1793     // If we only care about the highest bit, don't bother shifting right.
1794     if (DemandedBits.isSignMask()) {
1795       unsigned NumSignBits =
1796           TLO.DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1);
1797       bool AlreadySignExtended = NumSignBits >= BitWidth - ExVTBits + 1;
1798       // However if the input is already sign extended we expect the sign
1799       // extension to be dropped altogether later and do not simplify.
1800       if (!AlreadySignExtended) {
1801         // Compute the correct shift amount type, which must be getShiftAmountTy
1802         // for scalar types after legalization.
1803         EVT ShiftAmtTy = VT;
1804         if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
1805           ShiftAmtTy = getShiftAmountTy(ShiftAmtTy, DL);
1806 
1807         SDValue ShiftAmt =
1808             TLO.DAG.getConstant(BitWidth - ExVTBits, dl, ShiftAmtTy);
1809         return TLO.CombineTo(Op,
1810                              TLO.DAG.getNode(ISD::SHL, dl, VT, Op0, ShiftAmt));
1811       }
1812     }
1813 
1814     // If none of the extended bits are demanded, eliminate the sextinreg.
1815     if (DemandedBits.getActiveBits() <= ExVTBits)
1816       return TLO.CombineTo(Op, Op0);
1817 
1818     APInt InputDemandedBits = DemandedBits.getLoBits(ExVTBits);
1819 
1820     // Since the sign extended bits are demanded, we know that the sign
1821     // bit is demanded.
1822     InputDemandedBits.setBit(ExVTBits - 1);
1823 
1824     if (SimplifyDemandedBits(Op0, InputDemandedBits, Known, TLO, Depth + 1))
1825       return true;
1826     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1827 
1828     // If the sign bit of the input is known set or clear, then we know the
1829     // top bits of the result.
1830 
1831     // If the input sign bit is known zero, convert this into a zero extension.
1832     if (Known.Zero[ExVTBits - 1])
1833       return TLO.CombineTo(Op, TLO.DAG.getZeroExtendInReg(Op0, dl, ExVT));
1834 
1835     APInt Mask = APInt::getLowBitsSet(BitWidth, ExVTBits);
1836     if (Known.One[ExVTBits - 1]) { // Input sign bit known set
1837       Known.One.setBitsFrom(ExVTBits);
1838       Known.Zero &= Mask;
1839     } else { // Input sign bit unknown
1840       Known.Zero &= Mask;
1841       Known.One &= Mask;
1842     }
1843     break;
1844   }
1845   case ISD::BUILD_PAIR: {
1846     EVT HalfVT = Op.getOperand(0).getValueType();
1847     unsigned HalfBitWidth = HalfVT.getScalarSizeInBits();
1848 
1849     APInt MaskLo = DemandedBits.getLoBits(HalfBitWidth).trunc(HalfBitWidth);
1850     APInt MaskHi = DemandedBits.getHiBits(HalfBitWidth).trunc(HalfBitWidth);
1851 
1852     KnownBits KnownLo, KnownHi;
1853 
1854     if (SimplifyDemandedBits(Op.getOperand(0), MaskLo, KnownLo, TLO, Depth + 1))
1855       return true;
1856 
1857     if (SimplifyDemandedBits(Op.getOperand(1), MaskHi, KnownHi, TLO, Depth + 1))
1858       return true;
1859 
1860     Known.Zero = KnownLo.Zero.zext(BitWidth) |
1861                  KnownHi.Zero.zext(BitWidth).shl(HalfBitWidth);
1862 
1863     Known.One = KnownLo.One.zext(BitWidth) |
1864                 KnownHi.One.zext(BitWidth).shl(HalfBitWidth);
1865     break;
1866   }
1867   case ISD::ZERO_EXTEND:
1868   case ISD::ZERO_EXTEND_VECTOR_INREG: {
1869     SDValue Src = Op.getOperand(0);
1870     EVT SrcVT = Src.getValueType();
1871     unsigned InBits = SrcVT.getScalarSizeInBits();
1872     unsigned InElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
1873     bool IsVecInReg = Op.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG;
1874 
1875     // If none of the top bits are demanded, convert this into an any_extend.
1876     if (DemandedBits.getActiveBits() <= InBits) {
1877       // If we only need the non-extended bits of the bottom element
1878       // then we can just bitcast to the result.
1879       if (IsVecInReg && DemandedElts == 1 &&
1880           VT.getSizeInBits() == SrcVT.getSizeInBits() &&
1881           TLO.DAG.getDataLayout().isLittleEndian())
1882         return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src));
1883 
1884       unsigned Opc =
1885           IsVecInReg ? ISD::ANY_EXTEND_VECTOR_INREG : ISD::ANY_EXTEND;
1886       if (!TLO.LegalOperations() || isOperationLegal(Opc, VT))
1887         return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, Src));
1888     }
1889 
1890     APInt InDemandedBits = DemandedBits.trunc(InBits);
1891     APInt InDemandedElts = DemandedElts.zextOrSelf(InElts);
1892     if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO,
1893                              Depth + 1))
1894       return true;
1895     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1896     assert(Known.getBitWidth() == InBits && "Src width has changed?");
1897     Known = Known.zext(BitWidth);
1898 
1899     // Attempt to avoid multi-use ops if we don't need anything from them.
1900     if (SDValue NewSrc = SimplifyMultipleUseDemandedBits(
1901             Src, InDemandedBits, InDemandedElts, TLO.DAG, Depth + 1))
1902       return TLO.CombineTo(Op, TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc));
1903     break;
1904   }
1905   case ISD::SIGN_EXTEND:
1906   case ISD::SIGN_EXTEND_VECTOR_INREG: {
1907     SDValue Src = Op.getOperand(0);
1908     EVT SrcVT = Src.getValueType();
1909     unsigned InBits = SrcVT.getScalarSizeInBits();
1910     unsigned InElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
1911     bool IsVecInReg = Op.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG;
1912 
1913     // If none of the top bits are demanded, convert this into an any_extend.
1914     if (DemandedBits.getActiveBits() <= InBits) {
1915       // If we only need the non-extended bits of the bottom element
1916       // then we can just bitcast to the result.
1917       if (IsVecInReg && DemandedElts == 1 &&
1918           VT.getSizeInBits() == SrcVT.getSizeInBits() &&
1919           TLO.DAG.getDataLayout().isLittleEndian())
1920         return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src));
1921 
1922       unsigned Opc =
1923           IsVecInReg ? ISD::ANY_EXTEND_VECTOR_INREG : ISD::ANY_EXTEND;
1924       if (!TLO.LegalOperations() || isOperationLegal(Opc, VT))
1925         return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, Src));
1926     }
1927 
1928     APInt InDemandedBits = DemandedBits.trunc(InBits);
1929     APInt InDemandedElts = DemandedElts.zextOrSelf(InElts);
1930 
1931     // Since some of the sign extended bits are demanded, we know that the sign
1932     // bit is demanded.
1933     InDemandedBits.setBit(InBits - 1);
1934 
1935     if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO,
1936                              Depth + 1))
1937       return true;
1938     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1939     assert(Known.getBitWidth() == InBits && "Src width has changed?");
1940 
1941     // If the sign bit is known one, the top bits match.
1942     Known = Known.sext(BitWidth);
1943 
1944     // If the sign bit is known zero, convert this to a zero extend.
1945     if (Known.isNonNegative()) {
1946       unsigned Opc =
1947           IsVecInReg ? ISD::ZERO_EXTEND_VECTOR_INREG : ISD::ZERO_EXTEND;
1948       if (!TLO.LegalOperations() || isOperationLegal(Opc, VT))
1949         return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT, Src));
1950     }
1951 
1952     // Attempt to avoid multi-use ops if we don't need anything from them.
1953     if (SDValue NewSrc = SimplifyMultipleUseDemandedBits(
1954             Src, InDemandedBits, InDemandedElts, TLO.DAG, Depth + 1))
1955       return TLO.CombineTo(Op, TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc));
1956     break;
1957   }
1958   case ISD::ANY_EXTEND:
1959   case ISD::ANY_EXTEND_VECTOR_INREG: {
1960     SDValue Src = Op.getOperand(0);
1961     EVT SrcVT = Src.getValueType();
1962     unsigned InBits = SrcVT.getScalarSizeInBits();
1963     unsigned InElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
1964     bool IsVecInReg = Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG;
1965 
1966     // If we only need the bottom element then we can just bitcast.
1967     // TODO: Handle ANY_EXTEND?
1968     if (IsVecInReg && DemandedElts == 1 &&
1969         VT.getSizeInBits() == SrcVT.getSizeInBits() &&
1970         TLO.DAG.getDataLayout().isLittleEndian())
1971       return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src));
1972 
1973     APInt InDemandedBits = DemandedBits.trunc(InBits);
1974     APInt InDemandedElts = DemandedElts.zextOrSelf(InElts);
1975     if (SimplifyDemandedBits(Src, InDemandedBits, InDemandedElts, Known, TLO,
1976                              Depth + 1))
1977       return true;
1978     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1979     assert(Known.getBitWidth() == InBits && "Src width has changed?");
1980     Known = Known.anyext(BitWidth);
1981 
1982     // Attempt to avoid multi-use ops if we don't need anything from them.
1983     if (SDValue NewSrc = SimplifyMultipleUseDemandedBits(
1984             Src, InDemandedBits, InDemandedElts, TLO.DAG, Depth + 1))
1985       return TLO.CombineTo(Op, TLO.DAG.getNode(Op.getOpcode(), dl, VT, NewSrc));
1986     break;
1987   }
1988   case ISD::TRUNCATE: {
1989     SDValue Src = Op.getOperand(0);
1990 
1991     // Simplify the input, using demanded bit information, and compute the known
1992     // zero/one bits live out.
1993     unsigned OperandBitWidth = Src.getScalarValueSizeInBits();
1994     APInt TruncMask = DemandedBits.zext(OperandBitWidth);
1995     if (SimplifyDemandedBits(Src, TruncMask, DemandedElts, Known, TLO,
1996                              Depth + 1))
1997       return true;
1998     Known = Known.trunc(BitWidth);
1999 
2000     // Attempt to avoid multi-use ops if we don't need anything from them.
2001     if (SDValue NewSrc = SimplifyMultipleUseDemandedBits(
2002             Src, TruncMask, DemandedElts, TLO.DAG, Depth + 1))
2003       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::TRUNCATE, dl, VT, NewSrc));
2004 
2005     // If the input is only used by this truncate, see if we can shrink it based
2006     // on the known demanded bits.
2007     if (Src.getNode()->hasOneUse()) {
2008       switch (Src.getOpcode()) {
2009       default:
2010         break;
2011       case ISD::SRL:
2012         // Shrink SRL by a constant if none of the high bits shifted in are
2013         // demanded.
2014         if (TLO.LegalTypes() && !isTypeDesirableForOp(ISD::SRL, VT))
2015           // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
2016           // undesirable.
2017           break;
2018 
2019         const APInt *ShAmtC =
2020             TLO.DAG.getValidShiftAmountConstant(Src, DemandedElts);
2021         if (!ShAmtC || ShAmtC->uge(BitWidth))
2022           break;
2023         uint64_t ShVal = ShAmtC->getZExtValue();
2024 
2025         APInt HighBits =
2026             APInt::getHighBitsSet(OperandBitWidth, OperandBitWidth - BitWidth);
2027         HighBits.lshrInPlace(ShVal);
2028         HighBits = HighBits.trunc(BitWidth);
2029 
2030         if (!(HighBits & DemandedBits)) {
2031           // None of the shifted in bits are needed.  Add a truncate of the
2032           // shift input, then shift it.
2033           SDValue NewShAmt = TLO.DAG.getConstant(
2034               ShVal, dl, getShiftAmountTy(VT, DL, TLO.LegalTypes()));
2035           SDValue NewTrunc =
2036               TLO.DAG.getNode(ISD::TRUNCATE, dl, VT, Src.getOperand(0));
2037           return TLO.CombineTo(
2038               Op, TLO.DAG.getNode(ISD::SRL, dl, VT, NewTrunc, NewShAmt));
2039         }
2040         break;
2041       }
2042     }
2043 
2044     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
2045     break;
2046   }
2047   case ISD::AssertZext: {
2048     // AssertZext demands all of the high bits, plus any of the low bits
2049     // demanded by its users.
2050     EVT ZVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2051     APInt InMask = APInt::getLowBitsSet(BitWidth, ZVT.getSizeInBits());
2052     if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | DemandedBits, Known,
2053                              TLO, Depth + 1))
2054       return true;
2055     assert(!Known.hasConflict() && "Bits known to be one AND zero?");
2056 
2057     Known.Zero |= ~InMask;
2058     break;
2059   }
2060   case ISD::EXTRACT_VECTOR_ELT: {
2061     SDValue Src = Op.getOperand(0);
2062     SDValue Idx = Op.getOperand(1);
2063     ElementCount SrcEltCnt = Src.getValueType().getVectorElementCount();
2064     unsigned EltBitWidth = Src.getScalarValueSizeInBits();
2065 
2066     if (SrcEltCnt.isScalable())
2067       return false;
2068 
2069     // Demand the bits from every vector element without a constant index.
2070     unsigned NumSrcElts = SrcEltCnt.getFixedValue();
2071     APInt DemandedSrcElts = APInt::getAllOnesValue(NumSrcElts);
2072     if (auto *CIdx = dyn_cast<ConstantSDNode>(Idx))
2073       if (CIdx->getAPIntValue().ult(NumSrcElts))
2074         DemandedSrcElts = APInt::getOneBitSet(NumSrcElts, CIdx->getZExtValue());
2075 
2076     // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
2077     // anything about the extended bits.
2078     APInt DemandedSrcBits = DemandedBits;
2079     if (BitWidth > EltBitWidth)
2080       DemandedSrcBits = DemandedSrcBits.trunc(EltBitWidth);
2081 
2082     if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts, Known2, TLO,
2083                              Depth + 1))
2084       return true;
2085 
2086     // Attempt to avoid multi-use ops if we don't need anything from them.
2087     if (!DemandedSrcBits.isAllOnesValue() ||
2088         !DemandedSrcElts.isAllOnesValue()) {
2089       if (SDValue DemandedSrc = SimplifyMultipleUseDemandedBits(
2090               Src, DemandedSrcBits, DemandedSrcElts, TLO.DAG, Depth + 1)) {
2091         SDValue NewOp =
2092             TLO.DAG.getNode(Op.getOpcode(), dl, VT, DemandedSrc, Idx);
2093         return TLO.CombineTo(Op, NewOp);
2094       }
2095     }
2096 
2097     Known = Known2;
2098     if (BitWidth > EltBitWidth)
2099       Known = Known.anyext(BitWidth);
2100     break;
2101   }
2102   case ISD::BITCAST: {
2103     SDValue Src = Op.getOperand(0);
2104     EVT SrcVT = Src.getValueType();
2105     unsigned NumSrcEltBits = SrcVT.getScalarSizeInBits();
2106 
2107     // If this is an FP->Int bitcast and if the sign bit is the only
2108     // thing demanded, turn this into a FGETSIGN.
2109     if (!TLO.LegalOperations() && !VT.isVector() && !SrcVT.isVector() &&
2110         DemandedBits == APInt::getSignMask(Op.getValueSizeInBits()) &&
2111         SrcVT.isFloatingPoint()) {
2112       bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, VT);
2113       bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
2114       if ((OpVTLegal || i32Legal) && VT.isSimple() && SrcVT != MVT::f16 &&
2115           SrcVT != MVT::f128) {
2116         // Cannot eliminate/lower SHL for f128 yet.
2117         EVT Ty = OpVTLegal ? VT : MVT::i32;
2118         // Make a FGETSIGN + SHL to move the sign bit into the appropriate
2119         // place.  We expect the SHL to be eliminated by other optimizations.
2120         SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Src);
2121         unsigned OpVTSizeInBits = Op.getValueSizeInBits();
2122         if (!OpVTLegal && OpVTSizeInBits > 32)
2123           Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Sign);
2124         unsigned ShVal = Op.getValueSizeInBits() - 1;
2125         SDValue ShAmt = TLO.DAG.getConstant(ShVal, dl, VT);
2126         return TLO.CombineTo(Op,
2127                              TLO.DAG.getNode(ISD::SHL, dl, VT, Sign, ShAmt));
2128       }
2129     }
2130 
2131     // Bitcast from a vector using SimplifyDemanded Bits/VectorElts.
2132     // Demand the elt/bit if any of the original elts/bits are demanded.
2133     // TODO - bigendian once we have test coverage.
2134     if (SrcVT.isVector() && (BitWidth % NumSrcEltBits) == 0 &&
2135         TLO.DAG.getDataLayout().isLittleEndian()) {
2136       unsigned Scale = BitWidth / NumSrcEltBits;
2137       unsigned NumSrcElts = SrcVT.getVectorNumElements();
2138       APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits);
2139       APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts);
2140       for (unsigned i = 0; i != Scale; ++i) {
2141         unsigned Offset = i * NumSrcEltBits;
2142         APInt Sub = DemandedBits.extractBits(NumSrcEltBits, Offset);
2143         if (!Sub.isNullValue()) {
2144           DemandedSrcBits |= Sub;
2145           for (unsigned j = 0; j != NumElts; ++j)
2146             if (DemandedElts[j])
2147               DemandedSrcElts.setBit((j * Scale) + i);
2148         }
2149       }
2150 
2151       APInt KnownSrcUndef, KnownSrcZero;
2152       if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, KnownSrcUndef,
2153                                      KnownSrcZero, TLO, Depth + 1))
2154         return true;
2155 
2156       KnownBits KnownSrcBits;
2157       if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts,
2158                                KnownSrcBits, TLO, Depth + 1))
2159         return true;
2160     } else if ((NumSrcEltBits % BitWidth) == 0 &&
2161                TLO.DAG.getDataLayout().isLittleEndian()) {
2162       unsigned Scale = NumSrcEltBits / BitWidth;
2163       unsigned NumSrcElts = SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1;
2164       APInt DemandedSrcBits = APInt::getNullValue(NumSrcEltBits);
2165       APInt DemandedSrcElts = APInt::getNullValue(NumSrcElts);
2166       for (unsigned i = 0; i != NumElts; ++i)
2167         if (DemandedElts[i]) {
2168           unsigned Offset = (i % Scale) * BitWidth;
2169           DemandedSrcBits.insertBits(DemandedBits, Offset);
2170           DemandedSrcElts.setBit(i / Scale);
2171         }
2172 
2173       if (SrcVT.isVector()) {
2174         APInt KnownSrcUndef, KnownSrcZero;
2175         if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, KnownSrcUndef,
2176                                        KnownSrcZero, TLO, Depth + 1))
2177           return true;
2178       }
2179 
2180       KnownBits KnownSrcBits;
2181       if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedSrcElts,
2182                                KnownSrcBits, TLO, Depth + 1))
2183         return true;
2184     }
2185 
2186     // If this is a bitcast, let computeKnownBits handle it.  Only do this on a
2187     // recursive call where Known may be useful to the caller.
2188     if (Depth > 0) {
2189       Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
2190       return false;
2191     }
2192     break;
2193   }
2194   case ISD::ADD:
2195   case ISD::MUL:
2196   case ISD::SUB: {
2197     // Add, Sub, and Mul don't demand any bits in positions beyond that
2198     // of the highest bit demanded of them.
2199     SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1);
2200     SDNodeFlags Flags = Op.getNode()->getFlags();
2201     unsigned DemandedBitsLZ = DemandedBits.countLeadingZeros();
2202     APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ);
2203     if (SimplifyDemandedBits(Op0, LoMask, DemandedElts, Known2, TLO,
2204                              Depth + 1) ||
2205         SimplifyDemandedBits(Op1, LoMask, DemandedElts, Known2, TLO,
2206                              Depth + 1) ||
2207         // See if the operation should be performed at a smaller bit width.
2208         ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) {
2209       if (Flags.hasNoSignedWrap() || Flags.hasNoUnsignedWrap()) {
2210         // Disable the nsw and nuw flags. We can no longer guarantee that we
2211         // won't wrap after simplification.
2212         Flags.setNoSignedWrap(false);
2213         Flags.setNoUnsignedWrap(false);
2214         SDValue NewOp =
2215             TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1, Flags);
2216         return TLO.CombineTo(Op, NewOp);
2217       }
2218       return true;
2219     }
2220 
2221     // Attempt to avoid multi-use ops if we don't need anything from them.
2222     if (!LoMask.isAllOnesValue() || !DemandedElts.isAllOnesValue()) {
2223       SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits(
2224           Op0, LoMask, DemandedElts, TLO.DAG, Depth + 1);
2225       SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits(
2226           Op1, LoMask, DemandedElts, TLO.DAG, Depth + 1);
2227       if (DemandedOp0 || DemandedOp1) {
2228         Flags.setNoSignedWrap(false);
2229         Flags.setNoUnsignedWrap(false);
2230         Op0 = DemandedOp0 ? DemandedOp0 : Op0;
2231         Op1 = DemandedOp1 ? DemandedOp1 : Op1;
2232         SDValue NewOp =
2233             TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Op1, Flags);
2234         return TLO.CombineTo(Op, NewOp);
2235       }
2236     }
2237 
2238     // If we have a constant operand, we may be able to turn it into -1 if we
2239     // do not demand the high bits. This can make the constant smaller to
2240     // encode, allow more general folding, or match specialized instruction
2241     // patterns (eg, 'blsr' on x86). Don't bother changing 1 to -1 because that
2242     // is probably not useful (and could be detrimental).
2243     ConstantSDNode *C = isConstOrConstSplat(Op1);
2244     APInt HighMask = APInt::getHighBitsSet(BitWidth, DemandedBitsLZ);
2245     if (C && !C->isAllOnesValue() && !C->isOne() &&
2246         (C->getAPIntValue() | HighMask).isAllOnesValue()) {
2247       SDValue Neg1 = TLO.DAG.getAllOnesConstant(dl, VT);
2248       // Disable the nsw and nuw flags. We can no longer guarantee that we
2249       // won't wrap after simplification.
2250       Flags.setNoSignedWrap(false);
2251       Flags.setNoUnsignedWrap(false);
2252       SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), dl, VT, Op0, Neg1, Flags);
2253       return TLO.CombineTo(Op, NewOp);
2254     }
2255 
2256     LLVM_FALLTHROUGH;
2257   }
2258   default:
2259     if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
2260       if (SimplifyDemandedBitsForTargetNode(Op, DemandedBits, DemandedElts,
2261                                             Known, TLO, Depth))
2262         return true;
2263       break;
2264     }
2265 
2266     // Just use computeKnownBits to compute output bits.
2267     Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth);
2268     break;
2269   }
2270 
2271   // If we know the value of all of the demanded bits, return this as a
2272   // constant.
2273   if (DemandedBits.isSubsetOf(Known.Zero | Known.One)) {
2274     // Avoid folding to a constant if any OpaqueConstant is involved.
2275     const SDNode *N = Op.getNode();
2276     for (SDNode *Op :
2277          llvm::make_range(SDNodeIterator::begin(N), SDNodeIterator::end(N))) {
2278       if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
2279         if (C->isOpaque())
2280           return false;
2281     }
2282     if (VT.isInteger())
2283       return TLO.CombineTo(Op, TLO.DAG.getConstant(Known.One, dl, VT));
2284     if (VT.isFloatingPoint())
2285       return TLO.CombineTo(
2286           Op,
2287           TLO.DAG.getConstantFP(
2288               APFloat(TLO.DAG.EVTToAPFloatSemantics(VT), Known.One), dl, VT));
2289   }
2290 
2291   return false;
2292 }
2293 
2294 bool TargetLowering::SimplifyDemandedVectorElts(SDValue Op,
2295                                                 const APInt &DemandedElts,
2296                                                 APInt &KnownUndef,
2297                                                 APInt &KnownZero,
2298                                                 DAGCombinerInfo &DCI) const {
2299   SelectionDAG &DAG = DCI.DAG;
2300   TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2301                         !DCI.isBeforeLegalizeOps());
2302 
2303   bool Simplified =
2304       SimplifyDemandedVectorElts(Op, DemandedElts, KnownUndef, KnownZero, TLO);
2305   if (Simplified) {
2306     DCI.AddToWorklist(Op.getNode());
2307     DCI.CommitTargetLoweringOpt(TLO);
2308   }
2309 
2310   return Simplified;
2311 }
2312 
2313 /// Given a vector binary operation and known undefined elements for each input
2314 /// operand, compute whether each element of the output is undefined.
2315 static APInt getKnownUndefForVectorBinop(SDValue BO, SelectionDAG &DAG,
2316                                          const APInt &UndefOp0,
2317                                          const APInt &UndefOp1) {
2318   EVT VT = BO.getValueType();
2319   assert(DAG.getTargetLoweringInfo().isBinOp(BO.getOpcode()) && VT.isVector() &&
2320          "Vector binop only");
2321 
2322   EVT EltVT = VT.getVectorElementType();
2323   unsigned NumElts = VT.getVectorNumElements();
2324   assert(UndefOp0.getBitWidth() == NumElts &&
2325          UndefOp1.getBitWidth() == NumElts && "Bad type for undef analysis");
2326 
2327   auto getUndefOrConstantElt = [&](SDValue V, unsigned Index,
2328                                    const APInt &UndefVals) {
2329     if (UndefVals[Index])
2330       return DAG.getUNDEF(EltVT);
2331 
2332     if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2333       // Try hard to make sure that the getNode() call is not creating temporary
2334       // nodes. Ignore opaque integers because they do not constant fold.
2335       SDValue Elt = BV->getOperand(Index);
2336       auto *C = dyn_cast<ConstantSDNode>(Elt);
2337       if (isa<ConstantFPSDNode>(Elt) || Elt.isUndef() || (C && !C->isOpaque()))
2338         return Elt;
2339     }
2340 
2341     return SDValue();
2342   };
2343 
2344   APInt KnownUndef = APInt::getNullValue(NumElts);
2345   for (unsigned i = 0; i != NumElts; ++i) {
2346     // If both inputs for this element are either constant or undef and match
2347     // the element type, compute the constant/undef result for this element of
2348     // the vector.
2349     // TODO: Ideally we would use FoldConstantArithmetic() here, but that does
2350     // not handle FP constants. The code within getNode() should be refactored
2351     // to avoid the danger of creating a bogus temporary node here.
2352     SDValue C0 = getUndefOrConstantElt(BO.getOperand(0), i, UndefOp0);
2353     SDValue C1 = getUndefOrConstantElt(BO.getOperand(1), i, UndefOp1);
2354     if (C0 && C1 && C0.getValueType() == EltVT && C1.getValueType() == EltVT)
2355       if (DAG.getNode(BO.getOpcode(), SDLoc(BO), EltVT, C0, C1).isUndef())
2356         KnownUndef.setBit(i);
2357   }
2358   return KnownUndef;
2359 }
2360 
2361 bool TargetLowering::SimplifyDemandedVectorElts(
2362     SDValue Op, const APInt &OriginalDemandedElts, APInt &KnownUndef,
2363     APInt &KnownZero, TargetLoweringOpt &TLO, unsigned Depth,
2364     bool AssumeSingleUse) const {
2365   EVT VT = Op.getValueType();
2366   unsigned Opcode = Op.getOpcode();
2367   APInt DemandedElts = OriginalDemandedElts;
2368   unsigned NumElts = DemandedElts.getBitWidth();
2369   assert(VT.isVector() && "Expected vector op");
2370 
2371   KnownUndef = KnownZero = APInt::getNullValue(NumElts);
2372 
2373   // TODO: For now we assume we know nothing about scalable vectors.
2374   if (VT.isScalableVector())
2375     return false;
2376 
2377   assert(VT.getVectorNumElements() == NumElts &&
2378          "Mask size mismatches value type element count!");
2379 
2380   // Undef operand.
2381   if (Op.isUndef()) {
2382     KnownUndef.setAllBits();
2383     return false;
2384   }
2385 
2386   // If Op has other users, assume that all elements are needed.
2387   if (!Op.getNode()->hasOneUse() && !AssumeSingleUse)
2388     DemandedElts.setAllBits();
2389 
2390   // Not demanding any elements from Op.
2391   if (DemandedElts == 0) {
2392     KnownUndef.setAllBits();
2393     return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
2394   }
2395 
2396   // Limit search depth.
2397   if (Depth >= SelectionDAG::MaxRecursionDepth)
2398     return false;
2399 
2400   SDLoc DL(Op);
2401   unsigned EltSizeInBits = VT.getScalarSizeInBits();
2402 
2403   // Helper for demanding the specified elements and all the bits of both binary
2404   // operands.
2405   auto SimplifyDemandedVectorEltsBinOp = [&](SDValue Op0, SDValue Op1) {
2406     SDValue NewOp0 = SimplifyMultipleUseDemandedVectorElts(Op0, DemandedElts,
2407                                                            TLO.DAG, Depth + 1);
2408     SDValue NewOp1 = SimplifyMultipleUseDemandedVectorElts(Op1, DemandedElts,
2409                                                            TLO.DAG, Depth + 1);
2410     if (NewOp0 || NewOp1) {
2411       SDValue NewOp = TLO.DAG.getNode(
2412           Opcode, SDLoc(Op), VT, NewOp0 ? NewOp0 : Op0, NewOp1 ? NewOp1 : Op1);
2413       return TLO.CombineTo(Op, NewOp);
2414     }
2415     return false;
2416   };
2417 
2418   switch (Opcode) {
2419   case ISD::SCALAR_TO_VECTOR: {
2420     if (!DemandedElts[0]) {
2421       KnownUndef.setAllBits();
2422       return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
2423     }
2424     SDValue ScalarSrc = Op.getOperand(0);
2425     if (ScalarSrc.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
2426       SDValue Src = ScalarSrc.getOperand(0);
2427       SDValue Idx = ScalarSrc.getOperand(1);
2428       EVT SrcVT = Src.getValueType();
2429 
2430       ElementCount SrcEltCnt = SrcVT.getVectorElementCount();
2431 
2432       if (SrcEltCnt.isScalable())
2433         return false;
2434 
2435       unsigned NumSrcElts = SrcEltCnt.getFixedValue();
2436       if (isNullConstant(Idx)) {
2437         APInt SrcDemandedElts = APInt::getOneBitSet(NumSrcElts, 0);
2438         APInt SrcUndef = KnownUndef.zextOrTrunc(NumSrcElts);
2439         APInt SrcZero = KnownZero.zextOrTrunc(NumSrcElts);
2440         if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero,
2441                                        TLO, Depth + 1))
2442           return true;
2443       }
2444     }
2445     KnownUndef.setHighBits(NumElts - 1);
2446     break;
2447   }
2448   case ISD::BITCAST: {
2449     SDValue Src = Op.getOperand(0);
2450     EVT SrcVT = Src.getValueType();
2451 
2452     // We only handle vectors here.
2453     // TODO - investigate calling SimplifyDemandedBits/ComputeKnownBits?
2454     if (!SrcVT.isVector())
2455       break;
2456 
2457     // Fast handling of 'identity' bitcasts.
2458     unsigned NumSrcElts = SrcVT.getVectorNumElements();
2459     if (NumSrcElts == NumElts)
2460       return SimplifyDemandedVectorElts(Src, DemandedElts, KnownUndef,
2461                                         KnownZero, TLO, Depth + 1);
2462 
2463     APInt SrcZero, SrcUndef;
2464     APInt SrcDemandedElts = APInt::getNullValue(NumSrcElts);
2465 
2466     // Bitcast from 'large element' src vector to 'small element' vector, we
2467     // must demand a source element if any DemandedElt maps to it.
2468     if ((NumElts % NumSrcElts) == 0) {
2469       unsigned Scale = NumElts / NumSrcElts;
2470       for (unsigned i = 0; i != NumElts; ++i)
2471         if (DemandedElts[i])
2472           SrcDemandedElts.setBit(i / Scale);
2473 
2474       if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero,
2475                                      TLO, Depth + 1))
2476         return true;
2477 
2478       // Try calling SimplifyDemandedBits, converting demanded elts to the bits
2479       // of the large element.
2480       // TODO - bigendian once we have test coverage.
2481       if (TLO.DAG.getDataLayout().isLittleEndian()) {
2482         unsigned SrcEltSizeInBits = SrcVT.getScalarSizeInBits();
2483         APInt SrcDemandedBits = APInt::getNullValue(SrcEltSizeInBits);
2484         for (unsigned i = 0; i != NumElts; ++i)
2485           if (DemandedElts[i]) {
2486             unsigned Ofs = (i % Scale) * EltSizeInBits;
2487             SrcDemandedBits.setBits(Ofs, Ofs + EltSizeInBits);
2488           }
2489 
2490         KnownBits Known;
2491         if (SimplifyDemandedBits(Src, SrcDemandedBits, SrcDemandedElts, Known,
2492                                  TLO, Depth + 1))
2493           return true;
2494       }
2495 
2496       // If the src element is zero/undef then all the output elements will be -
2497       // only demanded elements are guaranteed to be correct.
2498       for (unsigned i = 0; i != NumSrcElts; ++i) {
2499         if (SrcDemandedElts[i]) {
2500           if (SrcZero[i])
2501             KnownZero.setBits(i * Scale, (i + 1) * Scale);
2502           if (SrcUndef[i])
2503             KnownUndef.setBits(i * Scale, (i + 1) * Scale);
2504         }
2505       }
2506     }
2507 
2508     // Bitcast from 'small element' src vector to 'large element' vector, we
2509     // demand all smaller source elements covered by the larger demanded element
2510     // of this vector.
2511     if ((NumSrcElts % NumElts) == 0) {
2512       unsigned Scale = NumSrcElts / NumElts;
2513       for (unsigned i = 0; i != NumElts; ++i)
2514         if (DemandedElts[i])
2515           SrcDemandedElts.setBits(i * Scale, (i + 1) * Scale);
2516 
2517       if (SimplifyDemandedVectorElts(Src, SrcDemandedElts, SrcUndef, SrcZero,
2518                                      TLO, Depth + 1))
2519         return true;
2520 
2521       // If all the src elements covering an output element are zero/undef, then
2522       // the output element will be as well, assuming it was demanded.
2523       for (unsigned i = 0; i != NumElts; ++i) {
2524         if (DemandedElts[i]) {
2525           if (SrcZero.extractBits(Scale, i * Scale).isAllOnesValue())
2526             KnownZero.setBit(i);
2527           if (SrcUndef.extractBits(Scale, i * Scale).isAllOnesValue())
2528             KnownUndef.setBit(i);
2529         }
2530       }
2531     }
2532     break;
2533   }
2534   case ISD::BUILD_VECTOR: {
2535     // Check all elements and simplify any unused elements with UNDEF.
2536     if (!DemandedElts.isAllOnesValue()) {
2537       // Don't simplify BROADCASTS.
2538       if (llvm::any_of(Op->op_values(),
2539                        [&](SDValue Elt) { return Op.getOperand(0) != Elt; })) {
2540         SmallVector<SDValue, 32> Ops(Op->op_begin(), Op->op_end());
2541         bool Updated = false;
2542         for (unsigned i = 0; i != NumElts; ++i) {
2543           if (!DemandedElts[i] && !Ops[i].isUndef()) {
2544             Ops[i] = TLO.DAG.getUNDEF(Ops[0].getValueType());
2545             KnownUndef.setBit(i);
2546             Updated = true;
2547           }
2548         }
2549         if (Updated)
2550           return TLO.CombineTo(Op, TLO.DAG.getBuildVector(VT, DL, Ops));
2551       }
2552     }
2553     for (unsigned i = 0; i != NumElts; ++i) {
2554       SDValue SrcOp = Op.getOperand(i);
2555       if (SrcOp.isUndef()) {
2556         KnownUndef.setBit(i);
2557       } else if (EltSizeInBits == SrcOp.getScalarValueSizeInBits() &&
2558                  (isNullConstant(SrcOp) || isNullFPConstant(SrcOp))) {
2559         KnownZero.setBit(i);
2560       }
2561     }
2562     break;
2563   }
2564   case ISD::CONCAT_VECTORS: {
2565     EVT SubVT = Op.getOperand(0).getValueType();
2566     unsigned NumSubVecs = Op.getNumOperands();
2567     unsigned NumSubElts = SubVT.getVectorNumElements();
2568     for (unsigned i = 0; i != NumSubVecs; ++i) {
2569       SDValue SubOp = Op.getOperand(i);
2570       APInt SubElts = DemandedElts.extractBits(NumSubElts, i * NumSubElts);
2571       APInt SubUndef, SubZero;
2572       if (SimplifyDemandedVectorElts(SubOp, SubElts, SubUndef, SubZero, TLO,
2573                                      Depth + 1))
2574         return true;
2575       KnownUndef.insertBits(SubUndef, i * NumSubElts);
2576       KnownZero.insertBits(SubZero, i * NumSubElts);
2577     }
2578     break;
2579   }
2580   case ISD::INSERT_SUBVECTOR: {
2581     // Demand any elements from the subvector and the remainder from the src its
2582     // inserted into.
2583     SDValue Src = Op.getOperand(0);
2584     SDValue Sub = Op.getOperand(1);
2585     uint64_t Idx = Op.getConstantOperandVal(2);
2586     unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
2587     APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
2588     APInt DemandedSrcElts = DemandedElts;
2589     DemandedSrcElts.insertBits(APInt::getNullValue(NumSubElts), Idx);
2590 
2591     APInt SubUndef, SubZero;
2592     if (SimplifyDemandedVectorElts(Sub, DemandedSubElts, SubUndef, SubZero, TLO,
2593                                    Depth + 1))
2594       return true;
2595 
2596     // If none of the src operand elements are demanded, replace it with undef.
2597     if (!DemandedSrcElts && !Src.isUndef())
2598       return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT,
2599                                                TLO.DAG.getUNDEF(VT), Sub,
2600                                                Op.getOperand(2)));
2601 
2602     if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, KnownUndef, KnownZero,
2603                                    TLO, Depth + 1))
2604       return true;
2605     KnownUndef.insertBits(SubUndef, Idx);
2606     KnownZero.insertBits(SubZero, Idx);
2607 
2608     // Attempt to avoid multi-use ops if we don't need anything from them.
2609     if (!DemandedSrcElts.isAllOnesValue() ||
2610         !DemandedSubElts.isAllOnesValue()) {
2611       SDValue NewSrc = SimplifyMultipleUseDemandedVectorElts(
2612           Src, DemandedSrcElts, TLO.DAG, Depth + 1);
2613       SDValue NewSub = SimplifyMultipleUseDemandedVectorElts(
2614           Sub, DemandedSubElts, TLO.DAG, Depth + 1);
2615       if (NewSrc || NewSub) {
2616         NewSrc = NewSrc ? NewSrc : Src;
2617         NewSub = NewSub ? NewSub : Sub;
2618         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, NewSrc,
2619                                         NewSub, Op.getOperand(2));
2620         return TLO.CombineTo(Op, NewOp);
2621       }
2622     }
2623     break;
2624   }
2625   case ISD::EXTRACT_SUBVECTOR: {
2626     // Offset the demanded elts by the subvector index.
2627     SDValue Src = Op.getOperand(0);
2628     if (Src.getValueType().isScalableVector())
2629       break;
2630     uint64_t Idx = Op.getConstantOperandVal(1);
2631     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2632     APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts).shl(Idx);
2633 
2634     APInt SrcUndef, SrcZero;
2635     if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, SrcUndef, SrcZero, TLO,
2636                                    Depth + 1))
2637       return true;
2638     KnownUndef = SrcUndef.extractBits(NumElts, Idx);
2639     KnownZero = SrcZero.extractBits(NumElts, Idx);
2640 
2641     // Attempt to avoid multi-use ops if we don't need anything from them.
2642     if (!DemandedElts.isAllOnesValue()) {
2643       SDValue NewSrc = SimplifyMultipleUseDemandedVectorElts(
2644           Src, DemandedSrcElts, TLO.DAG, Depth + 1);
2645       if (NewSrc) {
2646         SDValue NewOp = TLO.DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, NewSrc,
2647                                         Op.getOperand(1));
2648         return TLO.CombineTo(Op, NewOp);
2649       }
2650     }
2651     break;
2652   }
2653   case ISD::INSERT_VECTOR_ELT: {
2654     SDValue Vec = Op.getOperand(0);
2655     SDValue Scl = Op.getOperand(1);
2656     auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2657 
2658     // For a legal, constant insertion index, if we don't need this insertion
2659     // then strip it, else remove it from the demanded elts.
2660     if (CIdx && CIdx->getAPIntValue().ult(NumElts)) {
2661       unsigned Idx = CIdx->getZExtValue();
2662       if (!DemandedElts[Idx])
2663         return TLO.CombineTo(Op, Vec);
2664 
2665       APInt DemandedVecElts(DemandedElts);
2666       DemandedVecElts.clearBit(Idx);
2667       if (SimplifyDemandedVectorElts(Vec, DemandedVecElts, KnownUndef,
2668                                      KnownZero, TLO, Depth + 1))
2669         return true;
2670 
2671       KnownUndef.setBitVal(Idx, Scl.isUndef());
2672 
2673       KnownZero.setBitVal(Idx, isNullConstant(Scl) || isNullFPConstant(Scl));
2674       break;
2675     }
2676 
2677     APInt VecUndef, VecZero;
2678     if (SimplifyDemandedVectorElts(Vec, DemandedElts, VecUndef, VecZero, TLO,
2679                                    Depth + 1))
2680       return true;
2681     // Without knowing the insertion index we can't set KnownUndef/KnownZero.
2682     break;
2683   }
2684   case ISD::VSELECT: {
2685     // Try to transform the select condition based on the current demanded
2686     // elements.
2687     // TODO: If a condition element is undef, we can choose from one arm of the
2688     //       select (and if one arm is undef, then we can propagate that to the
2689     //       result).
2690     // TODO - add support for constant vselect masks (see IR version of this).
2691     APInt UnusedUndef, UnusedZero;
2692     if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, UnusedUndef,
2693                                    UnusedZero, TLO, Depth + 1))
2694       return true;
2695 
2696     // See if we can simplify either vselect operand.
2697     APInt DemandedLHS(DemandedElts);
2698     APInt DemandedRHS(DemandedElts);
2699     APInt UndefLHS, ZeroLHS;
2700     APInt UndefRHS, ZeroRHS;
2701     if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedLHS, UndefLHS,
2702                                    ZeroLHS, TLO, Depth + 1))
2703       return true;
2704     if (SimplifyDemandedVectorElts(Op.getOperand(2), DemandedRHS, UndefRHS,
2705                                    ZeroRHS, TLO, Depth + 1))
2706       return true;
2707 
2708     KnownUndef = UndefLHS & UndefRHS;
2709     KnownZero = ZeroLHS & ZeroRHS;
2710     break;
2711   }
2712   case ISD::VECTOR_SHUFFLE: {
2713     ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(Op)->getMask();
2714 
2715     // Collect demanded elements from shuffle operands..
2716     APInt DemandedLHS(NumElts, 0);
2717     APInt DemandedRHS(NumElts, 0);
2718     for (unsigned i = 0; i != NumElts; ++i) {
2719       int M = ShuffleMask[i];
2720       if (M < 0 || !DemandedElts[i])
2721         continue;
2722       assert(0 <= M && M < (int)(2 * NumElts) && "Shuffle index out of range");
2723       if (M < (int)NumElts)
2724         DemandedLHS.setBit(M);
2725       else
2726         DemandedRHS.setBit(M - NumElts);
2727     }
2728 
2729     // See if we can simplify either shuffle operand.
2730     APInt UndefLHS, ZeroLHS;
2731     APInt UndefRHS, ZeroRHS;
2732     if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedLHS, UndefLHS,
2733                                    ZeroLHS, TLO, Depth + 1))
2734       return true;
2735     if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedRHS, UndefRHS,
2736                                    ZeroRHS, TLO, Depth + 1))
2737       return true;
2738 
2739     // Simplify mask using undef elements from LHS/RHS.
2740     bool Updated = false;
2741     bool IdentityLHS = true, IdentityRHS = true;
2742     SmallVector<int, 32> NewMask(ShuffleMask.begin(), ShuffleMask.end());
2743     for (unsigned i = 0; i != NumElts; ++i) {
2744       int &M = NewMask[i];
2745       if (M < 0)
2746         continue;
2747       if (!DemandedElts[i] || (M < (int)NumElts && UndefLHS[M]) ||
2748           (M >= (int)NumElts && UndefRHS[M - NumElts])) {
2749         Updated = true;
2750         M = -1;
2751       }
2752       IdentityLHS &= (M < 0) || (M == (int)i);
2753       IdentityRHS &= (M < 0) || ((M - NumElts) == i);
2754     }
2755 
2756     // Update legal shuffle masks based on demanded elements if it won't reduce
2757     // to Identity which can cause premature removal of the shuffle mask.
2758     if (Updated && !IdentityLHS && !IdentityRHS && !TLO.LegalOps) {
2759       SDValue LegalShuffle =
2760           buildLegalVectorShuffle(VT, DL, Op.getOperand(0), Op.getOperand(1),
2761                                   NewMask, TLO.DAG);
2762       if (LegalShuffle)
2763         return TLO.CombineTo(Op, LegalShuffle);
2764     }
2765 
2766     // Propagate undef/zero elements from LHS/RHS.
2767     for (unsigned i = 0; i != NumElts; ++i) {
2768       int M = ShuffleMask[i];
2769       if (M < 0) {
2770         KnownUndef.setBit(i);
2771       } else if (M < (int)NumElts) {
2772         if (UndefLHS[M])
2773           KnownUndef.setBit(i);
2774         if (ZeroLHS[M])
2775           KnownZero.setBit(i);
2776       } else {
2777         if (UndefRHS[M - NumElts])
2778           KnownUndef.setBit(i);
2779         if (ZeroRHS[M - NumElts])
2780           KnownZero.setBit(i);
2781       }
2782     }
2783     break;
2784   }
2785   case ISD::ANY_EXTEND_VECTOR_INREG:
2786   case ISD::SIGN_EXTEND_VECTOR_INREG:
2787   case ISD::ZERO_EXTEND_VECTOR_INREG: {
2788     APInt SrcUndef, SrcZero;
2789     SDValue Src = Op.getOperand(0);
2790     unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2791     APInt DemandedSrcElts = DemandedElts.zextOrSelf(NumSrcElts);
2792     if (SimplifyDemandedVectorElts(Src, DemandedSrcElts, SrcUndef, SrcZero, TLO,
2793                                    Depth + 1))
2794       return true;
2795     KnownZero = SrcZero.zextOrTrunc(NumElts);
2796     KnownUndef = SrcUndef.zextOrTrunc(NumElts);
2797 
2798     if (Op.getOpcode() == ISD::ANY_EXTEND_VECTOR_INREG &&
2799         Op.getValueSizeInBits() == Src.getValueSizeInBits() &&
2800         DemandedSrcElts == 1 && TLO.DAG.getDataLayout().isLittleEndian()) {
2801       // aext - if we just need the bottom element then we can bitcast.
2802       return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, Src));
2803     }
2804 
2805     if (Op.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG) {
2806       // zext(undef) upper bits are guaranteed to be zero.
2807       if (DemandedElts.isSubsetOf(KnownUndef))
2808         return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT));
2809       KnownUndef.clearAllBits();
2810     }
2811     break;
2812   }
2813 
2814   // TODO: There are more binop opcodes that could be handled here - MIN,
2815   // MAX, saturated math, etc.
2816   case ISD::OR:
2817   case ISD::XOR:
2818   case ISD::ADD:
2819   case ISD::SUB:
2820   case ISD::FADD:
2821   case ISD::FSUB:
2822   case ISD::FMUL:
2823   case ISD::FDIV:
2824   case ISD::FREM: {
2825     SDValue Op0 = Op.getOperand(0);
2826     SDValue Op1 = Op.getOperand(1);
2827 
2828     APInt UndefRHS, ZeroRHS;
2829     if (SimplifyDemandedVectorElts(Op1, DemandedElts, UndefRHS, ZeroRHS, TLO,
2830                                    Depth + 1))
2831       return true;
2832     APInt UndefLHS, ZeroLHS;
2833     if (SimplifyDemandedVectorElts(Op0, DemandedElts, UndefLHS, ZeroLHS, TLO,
2834                                    Depth + 1))
2835       return true;
2836 
2837     KnownZero = ZeroLHS & ZeroRHS;
2838     KnownUndef = getKnownUndefForVectorBinop(Op, TLO.DAG, UndefLHS, UndefRHS);
2839 
2840     // Attempt to avoid multi-use ops if we don't need anything from them.
2841     // TODO - use KnownUndef to relax the demandedelts?
2842     if (!DemandedElts.isAllOnesValue())
2843       if (SimplifyDemandedVectorEltsBinOp(Op0, Op1))
2844         return true;
2845     break;
2846   }
2847   case ISD::SHL:
2848   case ISD::SRL:
2849   case ISD::SRA:
2850   case ISD::ROTL:
2851   case ISD::ROTR: {
2852     SDValue Op0 = Op.getOperand(0);
2853     SDValue Op1 = Op.getOperand(1);
2854 
2855     APInt UndefRHS, ZeroRHS;
2856     if (SimplifyDemandedVectorElts(Op1, DemandedElts, UndefRHS, ZeroRHS, TLO,
2857                                    Depth + 1))
2858       return true;
2859     APInt UndefLHS, ZeroLHS;
2860     if (SimplifyDemandedVectorElts(Op0, DemandedElts, UndefLHS, ZeroLHS, TLO,
2861                                    Depth + 1))
2862       return true;
2863 
2864     KnownZero = ZeroLHS;
2865     KnownUndef = UndefLHS & UndefRHS; // TODO: use getKnownUndefForVectorBinop?
2866 
2867     // Attempt to avoid multi-use ops if we don't need anything from them.
2868     // TODO - use KnownUndef to relax the demandedelts?
2869     if (!DemandedElts.isAllOnesValue())
2870       if (SimplifyDemandedVectorEltsBinOp(Op0, Op1))
2871         return true;
2872     break;
2873   }
2874   case ISD::MUL:
2875   case ISD::AND: {
2876     SDValue Op0 = Op.getOperand(0);
2877     SDValue Op1 = Op.getOperand(1);
2878 
2879     APInt SrcUndef, SrcZero;
2880     if (SimplifyDemandedVectorElts(Op1, DemandedElts, SrcUndef, SrcZero, TLO,
2881                                    Depth + 1))
2882       return true;
2883     if (SimplifyDemandedVectorElts(Op0, DemandedElts, KnownUndef, KnownZero,
2884                                    TLO, Depth + 1))
2885       return true;
2886 
2887     // If either side has a zero element, then the result element is zero, even
2888     // if the other is an UNDEF.
2889     // TODO: Extend getKnownUndefForVectorBinop to also deal with known zeros
2890     // and then handle 'and' nodes with the rest of the binop opcodes.
2891     KnownZero |= SrcZero;
2892     KnownUndef &= SrcUndef;
2893     KnownUndef &= ~KnownZero;
2894 
2895     // Attempt to avoid multi-use ops if we don't need anything from them.
2896     // TODO - use KnownUndef to relax the demandedelts?
2897     if (!DemandedElts.isAllOnesValue())
2898       if (SimplifyDemandedVectorEltsBinOp(Op0, Op1))
2899         return true;
2900     break;
2901   }
2902   case ISD::TRUNCATE:
2903   case ISD::SIGN_EXTEND:
2904   case ISD::ZERO_EXTEND:
2905     if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, KnownUndef,
2906                                    KnownZero, TLO, Depth + 1))
2907       return true;
2908 
2909     if (Op.getOpcode() == ISD::ZERO_EXTEND) {
2910       // zext(undef) upper bits are guaranteed to be zero.
2911       if (DemandedElts.isSubsetOf(KnownUndef))
2912         return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT));
2913       KnownUndef.clearAllBits();
2914     }
2915     break;
2916   default: {
2917     if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
2918       if (SimplifyDemandedVectorEltsForTargetNode(Op, DemandedElts, KnownUndef,
2919                                                   KnownZero, TLO, Depth))
2920         return true;
2921     } else {
2922       KnownBits Known;
2923       APInt DemandedBits = APInt::getAllOnesValue(EltSizeInBits);
2924       if (SimplifyDemandedBits(Op, DemandedBits, OriginalDemandedElts, Known,
2925                                TLO, Depth, AssumeSingleUse))
2926         return true;
2927     }
2928     break;
2929   }
2930   }
2931   assert((KnownUndef & KnownZero) == 0 && "Elements flagged as undef AND zero");
2932 
2933   // Constant fold all undef cases.
2934   // TODO: Handle zero cases as well.
2935   if (DemandedElts.isSubsetOf(KnownUndef))
2936     return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT));
2937 
2938   return false;
2939 }
2940 
2941 /// Determine which of the bits specified in Mask are known to be either zero or
2942 /// one and return them in the Known.
2943 void TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2944                                                    KnownBits &Known,
2945                                                    const APInt &DemandedElts,
2946                                                    const SelectionDAG &DAG,
2947                                                    unsigned Depth) const {
2948   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2949           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2950           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2951           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
2952          "Should use MaskedValueIsZero if you don't know whether Op"
2953          " is a target node!");
2954   Known.resetAll();
2955 }
2956 
2957 void TargetLowering::computeKnownBitsForTargetInstr(
2958     GISelKnownBits &Analysis, Register R, KnownBits &Known,
2959     const APInt &DemandedElts, const MachineRegisterInfo &MRI,
2960     unsigned Depth) const {
2961   Known.resetAll();
2962 }
2963 
2964 void TargetLowering::computeKnownBitsForFrameIndex(
2965   const int FrameIdx, KnownBits &Known, const MachineFunction &MF) const {
2966   // The low bits are known zero if the pointer is aligned.
2967   Known.Zero.setLowBits(Log2(MF.getFrameInfo().getObjectAlign(FrameIdx)));
2968 }
2969 
2970 Align TargetLowering::computeKnownAlignForTargetInstr(
2971   GISelKnownBits &Analysis, Register R, const MachineRegisterInfo &MRI,
2972   unsigned Depth) const {
2973   return Align(1);
2974 }
2975 
2976 /// This method can be implemented by targets that want to expose additional
2977 /// information about sign bits to the DAG Combiner.
2978 unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
2979                                                          const APInt &,
2980                                                          const SelectionDAG &,
2981                                                          unsigned Depth) const {
2982   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2983           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2984           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2985           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
2986          "Should use ComputeNumSignBits if you don't know whether Op"
2987          " is a target node!");
2988   return 1;
2989 }
2990 
2991 unsigned TargetLowering::computeNumSignBitsForTargetInstr(
2992   GISelKnownBits &Analysis, Register R, const APInt &DemandedElts,
2993   const MachineRegisterInfo &MRI, unsigned Depth) const {
2994   return 1;
2995 }
2996 
2997 bool TargetLowering::SimplifyDemandedVectorEltsForTargetNode(
2998     SDValue Op, const APInt &DemandedElts, APInt &KnownUndef, APInt &KnownZero,
2999     TargetLoweringOpt &TLO, unsigned Depth) const {
3000   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
3001           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
3002           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
3003           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
3004          "Should use SimplifyDemandedVectorElts if you don't know whether Op"
3005          " is a target node!");
3006   return false;
3007 }
3008 
3009 bool TargetLowering::SimplifyDemandedBitsForTargetNode(
3010     SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
3011     KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth) const {
3012   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
3013           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
3014           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
3015           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
3016          "Should use SimplifyDemandedBits if you don't know whether Op"
3017          " is a target node!");
3018   computeKnownBitsForTargetNode(Op, Known, DemandedElts, TLO.DAG, Depth);
3019   return false;
3020 }
3021 
3022 SDValue TargetLowering::SimplifyMultipleUseDemandedBitsForTargetNode(
3023     SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
3024     SelectionDAG &DAG, unsigned Depth) const {
3025   assert(
3026       (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
3027        Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
3028        Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
3029        Op.getOpcode() == ISD::INTRINSIC_VOID) &&
3030       "Should use SimplifyMultipleUseDemandedBits if you don't know whether Op"
3031       " is a target node!");
3032   return SDValue();
3033 }
3034 
3035 SDValue
3036 TargetLowering::buildLegalVectorShuffle(EVT VT, const SDLoc &DL, SDValue N0,
3037                                         SDValue N1, MutableArrayRef<int> Mask,
3038                                         SelectionDAG &DAG) const {
3039   bool LegalMask = isShuffleMaskLegal(Mask, VT);
3040   if (!LegalMask) {
3041     std::swap(N0, N1);
3042     ShuffleVectorSDNode::commuteMask(Mask);
3043     LegalMask = isShuffleMaskLegal(Mask, VT);
3044   }
3045 
3046   if (!LegalMask)
3047     return SDValue();
3048 
3049   return DAG.getVectorShuffle(VT, DL, N0, N1, Mask);
3050 }
3051 
3052 const Constant *TargetLowering::getTargetConstantFromLoad(LoadSDNode*) const {
3053   return nullptr;
3054 }
3055 
3056 bool TargetLowering::isKnownNeverNaNForTargetNode(SDValue Op,
3057                                                   const SelectionDAG &DAG,
3058                                                   bool SNaN,
3059                                                   unsigned Depth) const {
3060   assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
3061           Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
3062           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
3063           Op.getOpcode() == ISD::INTRINSIC_VOID) &&
3064          "Should use isKnownNeverNaN if you don't know whether Op"
3065          " is a target node!");
3066   return false;
3067 }
3068 
3069 // FIXME: Ideally, this would use ISD::isConstantSplatVector(), but that must
3070 // work with truncating build vectors and vectors with elements of less than
3071 // 8 bits.
3072 bool TargetLowering::isConstTrueVal(const SDNode *N) const {
3073   if (!N)
3074     return false;
3075 
3076   APInt CVal;
3077   if (auto *CN = dyn_cast<ConstantSDNode>(N)) {
3078     CVal = CN->getAPIntValue();
3079   } else if (auto *BV = dyn_cast<BuildVectorSDNode>(N)) {
3080     auto *CN = BV->getConstantSplatNode();
3081     if (!CN)
3082       return false;
3083 
3084     // If this is a truncating build vector, truncate the splat value.
3085     // Otherwise, we may fail to match the expected values below.
3086     unsigned BVEltWidth = BV->getValueType(0).getScalarSizeInBits();
3087     CVal = CN->getAPIntValue();
3088     if (BVEltWidth < CVal.getBitWidth())
3089       CVal = CVal.trunc(BVEltWidth);
3090   } else {
3091     return false;
3092   }
3093 
3094   switch (getBooleanContents(N->getValueType(0))) {
3095   case UndefinedBooleanContent:
3096     return CVal[0];
3097   case ZeroOrOneBooleanContent:
3098     return CVal.isOneValue();
3099   case ZeroOrNegativeOneBooleanContent:
3100     return CVal.isAllOnesValue();
3101   }
3102 
3103   llvm_unreachable("Invalid boolean contents");
3104 }
3105 
3106 bool TargetLowering::isConstFalseVal(const SDNode *N) const {
3107   if (!N)
3108     return false;
3109 
3110   const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
3111   if (!CN) {
3112     const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
3113     if (!BV)
3114       return false;
3115 
3116     // Only interested in constant splats, we don't care about undef
3117     // elements in identifying boolean constants and getConstantSplatNode
3118     // returns NULL if all ops are undef;
3119     CN = BV->getConstantSplatNode();
3120     if (!CN)
3121       return false;
3122   }
3123 
3124   if (getBooleanContents(N->getValueType(0)) == UndefinedBooleanContent)
3125     return !CN->getAPIntValue()[0];
3126 
3127   return CN->isNullValue();
3128 }
3129 
3130 bool TargetLowering::isExtendedTrueVal(const ConstantSDNode *N, EVT VT,
3131                                        bool SExt) const {
3132   if (VT == MVT::i1)
3133     return N->isOne();
3134 
3135   TargetLowering::BooleanContent Cnt = getBooleanContents(VT);
3136   switch (Cnt) {
3137   case TargetLowering::ZeroOrOneBooleanContent:
3138     // An extended value of 1 is always true, unless its original type is i1,
3139     // in which case it will be sign extended to -1.
3140     return (N->isOne() && !SExt) || (SExt && (N->getValueType(0) != MVT::i1));
3141   case TargetLowering::UndefinedBooleanContent:
3142   case TargetLowering::ZeroOrNegativeOneBooleanContent:
3143     return N->isAllOnesValue() && SExt;
3144   }
3145   llvm_unreachable("Unexpected enumeration.");
3146 }
3147 
3148 /// This helper function of SimplifySetCC tries to optimize the comparison when
3149 /// either operand of the SetCC node is a bitwise-and instruction.
3150 SDValue TargetLowering::foldSetCCWithAnd(EVT VT, SDValue N0, SDValue N1,
3151                                          ISD::CondCode Cond, const SDLoc &DL,
3152                                          DAGCombinerInfo &DCI) const {
3153   // Match these patterns in any of their permutations:
3154   // (X & Y) == Y
3155   // (X & Y) != Y
3156   if (N1.getOpcode() == ISD::AND && N0.getOpcode() != ISD::AND)
3157     std::swap(N0, N1);
3158 
3159   EVT OpVT = N0.getValueType();
3160   if (N0.getOpcode() != ISD::AND || !OpVT.isInteger() ||
3161       (Cond != ISD::SETEQ && Cond != ISD::SETNE))
3162     return SDValue();
3163 
3164   SDValue X, Y;
3165   if (N0.getOperand(0) == N1) {
3166     X = N0.getOperand(1);
3167     Y = N0.getOperand(0);
3168   } else if (N0.getOperand(1) == N1) {
3169     X = N0.getOperand(0);
3170     Y = N0.getOperand(1);
3171   } else {
3172     return SDValue();
3173   }
3174 
3175   SelectionDAG &DAG = DCI.DAG;
3176   SDValue Zero = DAG.getConstant(0, DL, OpVT);
3177   if (DAG.isKnownToBeAPowerOfTwo(Y)) {
3178     // Simplify X & Y == Y to X & Y != 0 if Y has exactly one bit set.
3179     // Note that where Y is variable and is known to have at most one bit set
3180     // (for example, if it is Z & 1) we cannot do this; the expressions are not
3181     // equivalent when Y == 0.
3182     assert(OpVT.isInteger());
3183     Cond = ISD::getSetCCInverse(Cond, OpVT);
3184     if (DCI.isBeforeLegalizeOps() ||
3185         isCondCodeLegal(Cond, N0.getSimpleValueType()))
3186       return DAG.getSetCC(DL, VT, N0, Zero, Cond);
3187   } else if (N0.hasOneUse() && hasAndNotCompare(Y)) {
3188     // If the target supports an 'and-not' or 'and-complement' logic operation,
3189     // try to use that to make a comparison operation more efficient.
3190     // But don't do this transform if the mask is a single bit because there are
3191     // more efficient ways to deal with that case (for example, 'bt' on x86 or
3192     // 'rlwinm' on PPC).
3193 
3194     // Bail out if the compare operand that we want to turn into a zero is
3195     // already a zero (otherwise, infinite loop).
3196     auto *YConst = dyn_cast<ConstantSDNode>(Y);
3197     if (YConst && YConst->isNullValue())
3198       return SDValue();
3199 
3200     // Transform this into: ~X & Y == 0.
3201     SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT);
3202     SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y);
3203     return DAG.getSetCC(DL, VT, NewAnd, Zero, Cond);
3204   }
3205 
3206   return SDValue();
3207 }
3208 
3209 /// There are multiple IR patterns that could be checking whether certain
3210 /// truncation of a signed number would be lossy or not. The pattern which is
3211 /// best at IR level, may not lower optimally. Thus, we want to unfold it.
3212 /// We are looking for the following pattern: (KeptBits is a constant)
3213 ///   (add %x, (1 << (KeptBits-1))) srccond (1 << KeptBits)
3214 /// KeptBits won't be bitwidth(x), that will be constant-folded to true/false.
3215 /// KeptBits also can't be 1, that would have been folded to  %x dstcond 0
3216 /// We will unfold it into the natural trunc+sext pattern:
3217 ///   ((%x << C) a>> C) dstcond %x
3218 /// Where  C = bitwidth(x) - KeptBits  and  C u< bitwidth(x)
3219 SDValue TargetLowering::optimizeSetCCOfSignedTruncationCheck(
3220     EVT SCCVT, SDValue N0, SDValue N1, ISD::CondCode Cond, DAGCombinerInfo &DCI,
3221     const SDLoc &DL) const {
3222   // We must be comparing with a constant.
3223   ConstantSDNode *C1;
3224   if (!(C1 = dyn_cast<ConstantSDNode>(N1)))
3225     return SDValue();
3226 
3227   // N0 should be:  add %x, (1 << (KeptBits-1))
3228   if (N0->getOpcode() != ISD::ADD)
3229     return SDValue();
3230 
3231   // And we must be 'add'ing a constant.
3232   ConstantSDNode *C01;
3233   if (!(C01 = dyn_cast<ConstantSDNode>(N0->getOperand(1))))
3234     return SDValue();
3235 
3236   SDValue X = N0->getOperand(0);
3237   EVT XVT = X.getValueType();
3238 
3239   // Validate constants ...
3240 
3241   APInt I1 = C1->getAPIntValue();
3242 
3243   ISD::CondCode NewCond;
3244   if (Cond == ISD::CondCode::SETULT) {
3245     NewCond = ISD::CondCode::SETEQ;
3246   } else if (Cond == ISD::CondCode::SETULE) {
3247     NewCond = ISD::CondCode::SETEQ;
3248     // But need to 'canonicalize' the constant.
3249     I1 += 1;
3250   } else if (Cond == ISD::CondCode::SETUGT) {
3251     NewCond = ISD::CondCode::SETNE;
3252     // But need to 'canonicalize' the constant.
3253     I1 += 1;
3254   } else if (Cond == ISD::CondCode::SETUGE) {
3255     NewCond = ISD::CondCode::SETNE;
3256   } else
3257     return SDValue();
3258 
3259   APInt I01 = C01->getAPIntValue();
3260 
3261   auto checkConstants = [&I1, &I01]() -> bool {
3262     // Both of them must be power-of-two, and the constant from setcc is bigger.
3263     return I1.ugt(I01) && I1.isPowerOf2() && I01.isPowerOf2();
3264   };
3265 
3266   if (checkConstants()) {
3267     // Great, e.g. got  icmp ult i16 (add i16 %x, 128), 256
3268   } else {
3269     // What if we invert constants? (and the target predicate)
3270     I1.negate();
3271     I01.negate();
3272     assert(XVT.isInteger());
3273     NewCond = getSetCCInverse(NewCond, XVT);
3274     if (!checkConstants())
3275       return SDValue();
3276     // Great, e.g. got  icmp uge i16 (add i16 %x, -128), -256
3277   }
3278 
3279   // They are power-of-two, so which bit is set?
3280   const unsigned KeptBits = I1.logBase2();
3281   const unsigned KeptBitsMinusOne = I01.logBase2();
3282 
3283   // Magic!
3284   if (KeptBits != (KeptBitsMinusOne + 1))
3285     return SDValue();
3286   assert(KeptBits > 0 && KeptBits < XVT.getSizeInBits() && "unreachable");
3287 
3288   // We don't want to do this in every single case.
3289   SelectionDAG &DAG = DCI.DAG;
3290   if (!DAG.getTargetLoweringInfo().shouldTransformSignedTruncationCheck(
3291           XVT, KeptBits))
3292     return SDValue();
3293 
3294   const unsigned MaskedBits = XVT.getSizeInBits() - KeptBits;
3295   assert(MaskedBits > 0 && MaskedBits < XVT.getSizeInBits() && "unreachable");
3296 
3297   // Unfold into:  ((%x << C) a>> C) cond %x
3298   // Where 'cond' will be either 'eq' or 'ne'.
3299   SDValue ShiftAmt = DAG.getConstant(MaskedBits, DL, XVT);
3300   SDValue T0 = DAG.getNode(ISD::SHL, DL, XVT, X, ShiftAmt);
3301   SDValue T1 = DAG.getNode(ISD::SRA, DL, XVT, T0, ShiftAmt);
3302   SDValue T2 = DAG.getSetCC(DL, SCCVT, T1, X, NewCond);
3303 
3304   return T2;
3305 }
3306 
3307 // (X & (C l>>/<< Y)) ==/!= 0  -->  ((X <</l>> Y) & C) ==/!= 0
3308 SDValue TargetLowering::optimizeSetCCByHoistingAndByConstFromLogicalShift(
3309     EVT SCCVT, SDValue N0, SDValue N1C, ISD::CondCode Cond,
3310     DAGCombinerInfo &DCI, const SDLoc &DL) const {
3311   assert(isConstOrConstSplat(N1C) &&
3312          isConstOrConstSplat(N1C)->getAPIntValue().isNullValue() &&
3313          "Should be a comparison with 0.");
3314   assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3315          "Valid only for [in]equality comparisons.");
3316 
3317   unsigned NewShiftOpcode;
3318   SDValue X, C, Y;
3319 
3320   SelectionDAG &DAG = DCI.DAG;
3321   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3322 
3323   // Look for '(C l>>/<< Y)'.
3324   auto Match = [&NewShiftOpcode, &X, &C, &Y, &TLI, &DAG](SDValue V) {
3325     // The shift should be one-use.
3326     if (!V.hasOneUse())
3327       return false;
3328     unsigned OldShiftOpcode = V.getOpcode();
3329     switch (OldShiftOpcode) {
3330     case ISD::SHL:
3331       NewShiftOpcode = ISD::SRL;
3332       break;
3333     case ISD::SRL:
3334       NewShiftOpcode = ISD::SHL;
3335       break;
3336     default:
3337       return false; // must be a logical shift.
3338     }
3339     // We should be shifting a constant.
3340     // FIXME: best to use isConstantOrConstantVector().
3341     C = V.getOperand(0);
3342     ConstantSDNode *CC =
3343         isConstOrConstSplat(C, /*AllowUndefs=*/true, /*AllowTruncation=*/true);
3344     if (!CC)
3345       return false;
3346     Y = V.getOperand(1);
3347 
3348     ConstantSDNode *XC =
3349         isConstOrConstSplat(X, /*AllowUndefs=*/true, /*AllowTruncation=*/true);
3350     return TLI.shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd(
3351         X, XC, CC, Y, OldShiftOpcode, NewShiftOpcode, DAG);
3352   };
3353 
3354   // LHS of comparison should be an one-use 'and'.
3355   if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
3356     return SDValue();
3357 
3358   X = N0.getOperand(0);
3359   SDValue Mask = N0.getOperand(1);
3360 
3361   // 'and' is commutative!
3362   if (!Match(Mask)) {
3363     std::swap(X, Mask);
3364     if (!Match(Mask))
3365       return SDValue();
3366   }
3367 
3368   EVT VT = X.getValueType();
3369 
3370   // Produce:
3371   // ((X 'OppositeShiftOpcode' Y) & C) Cond 0
3372   SDValue T0 = DAG.getNode(NewShiftOpcode, DL, VT, X, Y);
3373   SDValue T1 = DAG.getNode(ISD::AND, DL, VT, T0, C);
3374   SDValue T2 = DAG.getSetCC(DL, SCCVT, T1, N1C, Cond);
3375   return T2;
3376 }
3377 
3378 /// Try to fold an equality comparison with a {add/sub/xor} binary operation as
3379 /// the 1st operand (N0). Callers are expected to swap the N0/N1 parameters to
3380 /// handle the commuted versions of these patterns.
3381 SDValue TargetLowering::foldSetCCWithBinOp(EVT VT, SDValue N0, SDValue N1,
3382                                            ISD::CondCode Cond, const SDLoc &DL,
3383                                            DAGCombinerInfo &DCI) const {
3384   unsigned BOpcode = N0.getOpcode();
3385   assert((BOpcode == ISD::ADD || BOpcode == ISD::SUB || BOpcode == ISD::XOR) &&
3386          "Unexpected binop");
3387   assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) && "Unexpected condcode");
3388 
3389   // (X + Y) == X --> Y == 0
3390   // (X - Y) == X --> Y == 0
3391   // (X ^ Y) == X --> Y == 0
3392   SelectionDAG &DAG = DCI.DAG;
3393   EVT OpVT = N0.getValueType();
3394   SDValue X = N0.getOperand(0);
3395   SDValue Y = N0.getOperand(1);
3396   if (X == N1)
3397     return DAG.getSetCC(DL, VT, Y, DAG.getConstant(0, DL, OpVT), Cond);
3398 
3399   if (Y != N1)
3400     return SDValue();
3401 
3402   // (X + Y) == Y --> X == 0
3403   // (X ^ Y) == Y --> X == 0
3404   if (BOpcode == ISD::ADD || BOpcode == ISD::XOR)
3405     return DAG.getSetCC(DL, VT, X, DAG.getConstant(0, DL, OpVT), Cond);
3406 
3407   // The shift would not be valid if the operands are boolean (i1).
3408   if (!N0.hasOneUse() || OpVT.getScalarSizeInBits() == 1)
3409     return SDValue();
3410 
3411   // (X - Y) == Y --> X == Y << 1
3412   EVT ShiftVT = getShiftAmountTy(OpVT, DAG.getDataLayout(),
3413                                  !DCI.isBeforeLegalize());
3414   SDValue One = DAG.getConstant(1, DL, ShiftVT);
3415   SDValue YShl1 = DAG.getNode(ISD::SHL, DL, N1.getValueType(), Y, One);
3416   if (!DCI.isCalledByLegalizer())
3417     DCI.AddToWorklist(YShl1.getNode());
3418   return DAG.getSetCC(DL, VT, X, YShl1, Cond);
3419 }
3420 
3421 static SDValue simplifySetCCWithCTPOP(const TargetLowering &TLI, EVT VT,
3422                                       SDValue N0, const APInt &C1,
3423                                       ISD::CondCode Cond, const SDLoc &dl,
3424                                       SelectionDAG &DAG) {
3425   // Look through truncs that don't change the value of a ctpop.
3426   // FIXME: Add vector support? Need to be careful with setcc result type below.
3427   SDValue CTPOP = N0;
3428   if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() && !VT.isVector() &&
3429       N0.getScalarValueSizeInBits() > Log2_32(N0.getOperand(0).getScalarValueSizeInBits()))
3430     CTPOP = N0.getOperand(0);
3431 
3432   if (CTPOP.getOpcode() != ISD::CTPOP || !CTPOP.hasOneUse())
3433     return SDValue();
3434 
3435   EVT CTVT = CTPOP.getValueType();
3436   SDValue CTOp = CTPOP.getOperand(0);
3437 
3438   // If this is a vector CTPOP, keep the CTPOP if it is legal.
3439   // TODO: Should we check if CTPOP is legal(or custom) for scalars?
3440   if (VT.isVector() && TLI.isOperationLegal(ISD::CTPOP, CTVT))
3441     return SDValue();
3442 
3443   // (ctpop x) u< 2 -> (x & x-1) == 0
3444   // (ctpop x) u> 1 -> (x & x-1) != 0
3445   if (Cond == ISD::SETULT || Cond == ISD::SETUGT) {
3446     unsigned CostLimit = TLI.getCustomCtpopCost(CTVT, Cond);
3447     if (C1.ugt(CostLimit + (Cond == ISD::SETULT)))
3448       return SDValue();
3449     if (C1 == 0 && (Cond == ISD::SETULT))
3450       return SDValue(); // This is handled elsewhere.
3451 
3452     unsigned Passes = C1.getLimitedValue() - (Cond == ISD::SETULT);
3453 
3454     SDValue NegOne = DAG.getAllOnesConstant(dl, CTVT);
3455     SDValue Result = CTOp;
3456     for (unsigned i = 0; i < Passes; i++) {
3457       SDValue Add = DAG.getNode(ISD::ADD, dl, CTVT, Result, NegOne);
3458       Result = DAG.getNode(ISD::AND, dl, CTVT, Result, Add);
3459     }
3460     ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
3461     return DAG.getSetCC(dl, VT, Result, DAG.getConstant(0, dl, CTVT), CC);
3462   }
3463 
3464   // If ctpop is not supported, expand a power-of-2 comparison based on it.
3465   if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && C1 == 1) {
3466     // For scalars, keep CTPOP if it is legal or custom.
3467     if (!VT.isVector() && TLI.isOperationLegalOrCustom(ISD::CTPOP, CTVT))
3468       return SDValue();
3469     // This is based on X86's custom lowering for CTPOP which produces more
3470     // instructions than the expansion here.
3471 
3472     // (ctpop x) == 1 --> (x != 0) && ((x & x-1) == 0)
3473     // (ctpop x) != 1 --> (x == 0) || ((x & x-1) != 0)
3474     SDValue Zero = DAG.getConstant(0, dl, CTVT);
3475     SDValue NegOne = DAG.getAllOnesConstant(dl, CTVT);
3476     assert(CTVT.isInteger());
3477     ISD::CondCode InvCond = ISD::getSetCCInverse(Cond, CTVT);
3478     SDValue Add = DAG.getNode(ISD::ADD, dl, CTVT, CTOp, NegOne);
3479     SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Add);
3480     SDValue LHS = DAG.getSetCC(dl, VT, CTOp, Zero, InvCond);
3481     SDValue RHS = DAG.getSetCC(dl, VT, And, Zero, Cond);
3482     unsigned LogicOpcode = Cond == ISD::SETEQ ? ISD::AND : ISD::OR;
3483     return DAG.getNode(LogicOpcode, dl, VT, LHS, RHS);
3484   }
3485 
3486   return SDValue();
3487 }
3488 
3489 /// Try to simplify a setcc built with the specified operands and cc. If it is
3490 /// unable to simplify it, return a null SDValue.
3491 SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
3492                                       ISD::CondCode Cond, bool foldBooleans,
3493                                       DAGCombinerInfo &DCI,
3494                                       const SDLoc &dl) const {
3495   SelectionDAG &DAG = DCI.DAG;
3496   const DataLayout &Layout = DAG.getDataLayout();
3497   EVT OpVT = N0.getValueType();
3498 
3499   // Constant fold or commute setcc.
3500   if (SDValue Fold = DAG.FoldSetCC(VT, N0, N1, Cond, dl))
3501     return Fold;
3502 
3503   // Ensure that the constant occurs on the RHS and fold constant comparisons.
3504   // TODO: Handle non-splat vector constants. All undef causes trouble.
3505   // FIXME: We can't yet fold constant scalable vector splats, so avoid an
3506   // infinite loop here when we encounter one.
3507   ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
3508   if (isConstOrConstSplat(N0) &&
3509       (!OpVT.isScalableVector() || !isConstOrConstSplat(N1)) &&
3510       (DCI.isBeforeLegalizeOps() ||
3511        isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
3512     return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
3513 
3514   // If we have a subtract with the same 2 non-constant operands as this setcc
3515   // -- but in reverse order -- then try to commute the operands of this setcc
3516   // to match. A matching pair of setcc (cmp) and sub may be combined into 1
3517   // instruction on some targets.
3518   if (!isConstOrConstSplat(N0) && !isConstOrConstSplat(N1) &&
3519       (DCI.isBeforeLegalizeOps() ||
3520        isCondCodeLegal(SwappedCC, N0.getSimpleValueType())) &&
3521       DAG.doesNodeExist(ISD::SUB, DAG.getVTList(OpVT), {N1, N0}) &&
3522       !DAG.doesNodeExist(ISD::SUB, DAG.getVTList(OpVT), {N0, N1}))
3523     return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);
3524 
3525   if (auto *N1C = isConstOrConstSplat(N1)) {
3526     const APInt &C1 = N1C->getAPIntValue();
3527 
3528     // Optimize some CTPOP cases.
3529     if (SDValue V = simplifySetCCWithCTPOP(*this, VT, N0, C1, Cond, dl, DAG))
3530       return V;
3531 
3532     // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3533     // equality comparison, then we're just comparing whether X itself is
3534     // zero.
3535     if (N0.getOpcode() == ISD::SRL && (C1.isNullValue() || C1.isOneValue()) &&
3536         N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3537         isPowerOf2_32(N0.getScalarValueSizeInBits())) {
3538       if (ConstantSDNode *ShAmt = isConstOrConstSplat(N0.getOperand(1))) {
3539         if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3540             ShAmt->getAPIntValue() == Log2_32(N0.getScalarValueSizeInBits())) {
3541           if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3542             // (srl (ctlz x), 5) == 0  -> X != 0
3543             // (srl (ctlz x), 5) != 1  -> X != 0
3544             Cond = ISD::SETNE;
3545           } else {
3546             // (srl (ctlz x), 5) != 0  -> X == 0
3547             // (srl (ctlz x), 5) == 1  -> X == 0
3548             Cond = ISD::SETEQ;
3549           }
3550           SDValue Zero = DAG.getConstant(0, dl, N0.getValueType());
3551           return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0), Zero,
3552                               Cond);
3553         }
3554       }
3555     }
3556   }
3557 
3558   // FIXME: Support vectors.
3559   if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
3560     const APInt &C1 = N1C->getAPIntValue();
3561 
3562     // (zext x) == C --> x == (trunc C)
3563     // (sext x) == C --> x == (trunc C)
3564     if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3565         DCI.isBeforeLegalize() && N0->hasOneUse()) {
3566       unsigned MinBits = N0.getValueSizeInBits();
3567       SDValue PreExt;
3568       bool Signed = false;
3569       if (N0->getOpcode() == ISD::ZERO_EXTEND) {
3570         // ZExt
3571         MinBits = N0->getOperand(0).getValueSizeInBits();
3572         PreExt = N0->getOperand(0);
3573       } else if (N0->getOpcode() == ISD::AND) {
3574         // DAGCombine turns costly ZExts into ANDs
3575         if (auto *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
3576           if ((C->getAPIntValue()+1).isPowerOf2()) {
3577             MinBits = C->getAPIntValue().countTrailingOnes();
3578             PreExt = N0->getOperand(0);
3579           }
3580       } else if (N0->getOpcode() == ISD::SIGN_EXTEND) {
3581         // SExt
3582         MinBits = N0->getOperand(0).getValueSizeInBits();
3583         PreExt = N0->getOperand(0);
3584         Signed = true;
3585       } else if (auto *LN0 = dyn_cast<LoadSDNode>(N0)) {
3586         // ZEXTLOAD / SEXTLOAD
3587         if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
3588           MinBits = LN0->getMemoryVT().getSizeInBits();
3589           PreExt = N0;
3590         } else if (LN0->getExtensionType() == ISD::SEXTLOAD) {
3591           Signed = true;
3592           MinBits = LN0->getMemoryVT().getSizeInBits();
3593           PreExt = N0;
3594         }
3595       }
3596 
3597       // Figure out how many bits we need to preserve this constant.
3598       unsigned ReqdBits = Signed ?
3599         C1.getBitWidth() - C1.getNumSignBits() + 1 :
3600         C1.getActiveBits();
3601 
3602       // Make sure we're not losing bits from the constant.
3603       if (MinBits > 0 &&
3604           MinBits < C1.getBitWidth() &&
3605           MinBits >= ReqdBits) {
3606         EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
3607         if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
3608           // Will get folded away.
3609           SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreExt);
3610           if (MinBits == 1 && C1 == 1)
3611             // Invert the condition.
3612             return DAG.getSetCC(dl, VT, Trunc, DAG.getConstant(0, dl, MVT::i1),
3613                                 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3614           SDValue C = DAG.getConstant(C1.trunc(MinBits), dl, MinVT);
3615           return DAG.getSetCC(dl, VT, Trunc, C, Cond);
3616         }
3617 
3618         // If truncating the setcc operands is not desirable, we can still
3619         // simplify the expression in some cases:
3620         // setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> setcc (x, y, cc)
3621         // setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> setcc (x, y, inv(cc))
3622         // setcc (zext (setcc x, y, cc)), 1, setne) -> setcc (x, y, inv(cc))
3623         // setcc (zext (setcc x, y, cc)), 1, seteq) -> setcc (x, y, cc)
3624         // setcc (sext (setcc x, y, cc)), -1, setne) -> setcc (x, y, inv(cc))
3625         // setcc (sext (setcc x, y, cc)), -1, seteq) -> setcc (x, y, cc)
3626         SDValue TopSetCC = N0->getOperand(0);
3627         unsigned N0Opc = N0->getOpcode();
3628         bool SExt = (N0Opc == ISD::SIGN_EXTEND);
3629         if (TopSetCC.getValueType() == MVT::i1 && VT == MVT::i1 &&
3630             TopSetCC.getOpcode() == ISD::SETCC &&
3631             (N0Opc == ISD::ZERO_EXTEND || N0Opc == ISD::SIGN_EXTEND) &&
3632             (isConstFalseVal(N1C) ||
3633              isExtendedTrueVal(N1C, N0->getValueType(0), SExt))) {
3634 
3635           bool Inverse = (N1C->isNullValue() && Cond == ISD::SETEQ) ||
3636                          (!N1C->isNullValue() && Cond == ISD::SETNE);
3637 
3638           if (!Inverse)
3639             return TopSetCC;
3640 
3641           ISD::CondCode InvCond = ISD::getSetCCInverse(
3642               cast<CondCodeSDNode>(TopSetCC.getOperand(2))->get(),
3643               TopSetCC.getOperand(0).getValueType());
3644           return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0),
3645                                       TopSetCC.getOperand(1),
3646                                       InvCond);
3647         }
3648       }
3649     }
3650 
3651     // If the LHS is '(and load, const)', the RHS is 0, the test is for
3652     // equality or unsigned, and all 1 bits of the const are in the same
3653     // partial word, see if we can shorten the load.
3654     if (DCI.isBeforeLegalize() &&
3655         !ISD::isSignedIntSetCC(Cond) &&
3656         N0.getOpcode() == ISD::AND && C1 == 0 &&
3657         N0.getNode()->hasOneUse() &&
3658         isa<LoadSDNode>(N0.getOperand(0)) &&
3659         N0.getOperand(0).getNode()->hasOneUse() &&
3660         isa<ConstantSDNode>(N0.getOperand(1))) {
3661       LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
3662       APInt bestMask;
3663       unsigned bestWidth = 0, bestOffset = 0;
3664       if (Lod->isSimple() && Lod->isUnindexed()) {
3665         unsigned origWidth = N0.getValueSizeInBits();
3666         unsigned maskWidth = origWidth;
3667         // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
3668         // 8 bits, but have to be careful...
3669         if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
3670           origWidth = Lod->getMemoryVT().getSizeInBits();
3671         const APInt &Mask = N0.getConstantOperandAPInt(1);
3672         for (unsigned width = origWidth / 2; width>=8; width /= 2) {
3673           APInt newMask = APInt::getLowBitsSet(maskWidth, width);
3674           for (unsigned offset=0; offset<origWidth/width; offset++) {
3675             if (Mask.isSubsetOf(newMask)) {
3676               if (Layout.isLittleEndian())
3677                 bestOffset = (uint64_t)offset * (width/8);
3678               else
3679                 bestOffset = (origWidth/width - offset - 1) * (width/8);
3680               bestMask = Mask.lshr(offset * (width/8) * 8);
3681               bestWidth = width;
3682               break;
3683             }
3684             newMask <<= width;
3685           }
3686         }
3687       }
3688       if (bestWidth) {
3689         EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
3690         if (newVT.isRound() &&
3691             shouldReduceLoadWidth(Lod, ISD::NON_EXTLOAD, newVT)) {
3692           SDValue Ptr = Lod->getBasePtr();
3693           if (bestOffset != 0)
3694             Ptr =
3695                 DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(bestOffset), dl);
3696           SDValue NewLoad =
3697               DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
3698                           Lod->getPointerInfo().getWithOffset(bestOffset),
3699                           Lod->getOriginalAlign());
3700           return DAG.getSetCC(dl, VT,
3701                               DAG.getNode(ISD::AND, dl, newVT, NewLoad,
3702                                       DAG.getConstant(bestMask.trunc(bestWidth),
3703                                                       dl, newVT)),
3704                               DAG.getConstant(0LL, dl, newVT), Cond);
3705         }
3706       }
3707     }
3708 
3709     // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3710     if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3711       unsigned InSize = N0.getOperand(0).getValueSizeInBits();
3712 
3713       // If the comparison constant has bits in the upper part, the
3714       // zero-extended value could never match.
3715       if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
3716                                               C1.getBitWidth() - InSize))) {
3717         switch (Cond) {
3718         case ISD::SETUGT:
3719         case ISD::SETUGE:
3720         case ISD::SETEQ:
3721           return DAG.getConstant(0, dl, VT);
3722         case ISD::SETULT:
3723         case ISD::SETULE:
3724         case ISD::SETNE:
3725           return DAG.getConstant(1, dl, VT);
3726         case ISD::SETGT:
3727         case ISD::SETGE:
3728           // True if the sign bit of C1 is set.
3729           return DAG.getConstant(C1.isNegative(), dl, VT);
3730         case ISD::SETLT:
3731         case ISD::SETLE:
3732           // True if the sign bit of C1 isn't set.
3733           return DAG.getConstant(C1.isNonNegative(), dl, VT);
3734         default:
3735           break;
3736         }
3737       }
3738 
3739       // Otherwise, we can perform the comparison with the low bits.
3740       switch (Cond) {
3741       case ISD::SETEQ:
3742       case ISD::SETNE:
3743       case ISD::SETUGT:
3744       case ISD::SETUGE:
3745       case ISD::SETULT:
3746       case ISD::SETULE: {
3747         EVT newVT = N0.getOperand(0).getValueType();
3748         if (DCI.isBeforeLegalizeOps() ||
3749             (isOperationLegal(ISD::SETCC, newVT) &&
3750              isCondCodeLegal(Cond, newVT.getSimpleVT()))) {
3751           EVT NewSetCCVT = getSetCCResultType(Layout, *DAG.getContext(), newVT);
3752           SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT);
3753 
3754           SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0),
3755                                           NewConst, Cond);
3756           return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType());
3757         }
3758         break;
3759       }
3760       default:
3761         break; // todo, be more careful with signed comparisons
3762       }
3763     } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3764                (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3765                !isSExtCheaperThanZExt(cast<VTSDNode>(N0.getOperand(1))->getVT(),
3766                                       OpVT)) {
3767       EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3768       unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
3769       EVT ExtDstTy = N0.getValueType();
3770       unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
3771 
3772       // If the constant doesn't fit into the number of bits for the source of
3773       // the sign extension, it is impossible for both sides to be equal.
3774       if (C1.getMinSignedBits() > ExtSrcTyBits)
3775         return DAG.getBoolConstant(Cond == ISD::SETNE, dl, VT, OpVT);
3776 
3777       assert(ExtDstTy == N0.getOperand(0).getValueType() &&
3778              ExtDstTy != ExtSrcTy && "Unexpected types!");
3779       APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
3780       SDValue ZextOp = DAG.getNode(ISD::AND, dl, ExtDstTy, N0.getOperand(0),
3781                                    DAG.getConstant(Imm, dl, ExtDstTy));
3782       if (!DCI.isCalledByLegalizer())
3783         DCI.AddToWorklist(ZextOp.getNode());
3784       // Otherwise, make this a use of a zext.
3785       return DAG.getSetCC(dl, VT, ZextOp,
3786                           DAG.getConstant(C1 & Imm, dl, ExtDstTy), Cond);
3787     } else if ((N1C->isNullValue() || N1C->isOne()) &&
3788                 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3789       // SETCC (SETCC), [0|1], [EQ|NE]  -> SETCC
3790       if (N0.getOpcode() == ISD::SETCC &&
3791           isTypeLegal(VT) && VT.bitsLE(N0.getValueType()) &&
3792           (N0.getValueType() == MVT::i1 ||
3793            getBooleanContents(N0.getOperand(0).getValueType()) ==
3794                        ZeroOrOneBooleanContent)) {
3795         bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (!N1C->isOne());
3796         if (TrueWhenTrue)
3797           return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
3798         // Invert the condition.
3799         ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
3800         CC = ISD::getSetCCInverse(CC, N0.getOperand(0).getValueType());
3801         if (DCI.isBeforeLegalizeOps() ||
3802             isCondCodeLegal(CC, N0.getOperand(0).getSimpleValueType()))
3803           return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
3804       }
3805 
3806       if ((N0.getOpcode() == ISD::XOR ||
3807            (N0.getOpcode() == ISD::AND &&
3808             N0.getOperand(0).getOpcode() == ISD::XOR &&
3809             N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3810           isOneConstant(N0.getOperand(1))) {
3811         // If this is (X^1) == 0/1, swap the RHS and eliminate the xor.  We
3812         // can only do this if the top bits are known zero.
3813         unsigned BitWidth = N0.getValueSizeInBits();
3814         if (DAG.MaskedValueIsZero(N0,
3815                                   APInt::getHighBitsSet(BitWidth,
3816                                                         BitWidth-1))) {
3817           // Okay, get the un-inverted input value.
3818           SDValue Val;
3819           if (N0.getOpcode() == ISD::XOR) {
3820             Val = N0.getOperand(0);
3821           } else {
3822             assert(N0.getOpcode() == ISD::AND &&
3823                     N0.getOperand(0).getOpcode() == ISD::XOR);
3824             // ((X^1)&1)^1 -> X & 1
3825             Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
3826                               N0.getOperand(0).getOperand(0),
3827                               N0.getOperand(1));
3828           }
3829 
3830           return DAG.getSetCC(dl, VT, Val, N1,
3831                               Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3832         }
3833       } else if (N1C->isOne()) {
3834         SDValue Op0 = N0;
3835         if (Op0.getOpcode() == ISD::TRUNCATE)
3836           Op0 = Op0.getOperand(0);
3837 
3838         if ((Op0.getOpcode() == ISD::XOR) &&
3839             Op0.getOperand(0).getOpcode() == ISD::SETCC &&
3840             Op0.getOperand(1).getOpcode() == ISD::SETCC) {
3841           SDValue XorLHS = Op0.getOperand(0);
3842           SDValue XorRHS = Op0.getOperand(1);
3843           // Ensure that the input setccs return an i1 type or 0/1 value.
3844           if (Op0.getValueType() == MVT::i1 ||
3845               (getBooleanContents(XorLHS.getOperand(0).getValueType()) ==
3846                       ZeroOrOneBooleanContent &&
3847                getBooleanContents(XorRHS.getOperand(0).getValueType()) ==
3848                         ZeroOrOneBooleanContent)) {
3849             // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
3850             Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
3851             return DAG.getSetCC(dl, VT, XorLHS, XorRHS, Cond);
3852           }
3853         }
3854         if (Op0.getOpcode() == ISD::AND && isOneConstant(Op0.getOperand(1))) {
3855           // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
3856           if (Op0.getValueType().bitsGT(VT))
3857             Op0 = DAG.getNode(ISD::AND, dl, VT,
3858                           DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
3859                           DAG.getConstant(1, dl, VT));
3860           else if (Op0.getValueType().bitsLT(VT))
3861             Op0 = DAG.getNode(ISD::AND, dl, VT,
3862                         DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
3863                         DAG.getConstant(1, dl, VT));
3864 
3865           return DAG.getSetCC(dl, VT, Op0,
3866                               DAG.getConstant(0, dl, Op0.getValueType()),
3867                               Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3868         }
3869         if (Op0.getOpcode() == ISD::AssertZext &&
3870             cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
3871           return DAG.getSetCC(dl, VT, Op0,
3872                               DAG.getConstant(0, dl, Op0.getValueType()),
3873                               Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3874       }
3875     }
3876 
3877     // Given:
3878     //   icmp eq/ne (urem %x, %y), 0
3879     // Iff %x has 0 or 1 bits set, and %y has at least 2 bits set, omit 'urem':
3880     //   icmp eq/ne %x, 0
3881     if (N0.getOpcode() == ISD::UREM && N1C->isNullValue() &&
3882         (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3883       KnownBits XKnown = DAG.computeKnownBits(N0.getOperand(0));
3884       KnownBits YKnown = DAG.computeKnownBits(N0.getOperand(1));
3885       if (XKnown.countMaxPopulation() == 1 && YKnown.countMinPopulation() >= 2)
3886         return DAG.getSetCC(dl, VT, N0.getOperand(0), N1, Cond);
3887     }
3888 
3889     if (SDValue V =
3890             optimizeSetCCOfSignedTruncationCheck(VT, N0, N1, Cond, DCI, dl))
3891       return V;
3892   }
3893 
3894   // These simplifications apply to splat vectors as well.
3895   // TODO: Handle more splat vector cases.
3896   if (auto *N1C = isConstOrConstSplat(N1)) {
3897     const APInt &C1 = N1C->getAPIntValue();
3898 
3899     APInt MinVal, MaxVal;
3900     unsigned OperandBitSize = N1C->getValueType(0).getScalarSizeInBits();
3901     if (ISD::isSignedIntSetCC(Cond)) {
3902       MinVal = APInt::getSignedMinValue(OperandBitSize);
3903       MaxVal = APInt::getSignedMaxValue(OperandBitSize);
3904     } else {
3905       MinVal = APInt::getMinValue(OperandBitSize);
3906       MaxVal = APInt::getMaxValue(OperandBitSize);
3907     }
3908 
3909     // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3910     if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3911       // X >= MIN --> true
3912       if (C1 == MinVal)
3913         return DAG.getBoolConstant(true, dl, VT, OpVT);
3914 
3915       if (!VT.isVector()) { // TODO: Support this for vectors.
3916         // X >= C0 --> X > (C0 - 1)
3917         APInt C = C1 - 1;
3918         ISD::CondCode NewCC = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
3919         if ((DCI.isBeforeLegalizeOps() ||
3920              isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
3921             (!N1C->isOpaque() || (C.getBitWidth() <= 64 &&
3922                                   isLegalICmpImmediate(C.getSExtValue())))) {
3923           return DAG.getSetCC(dl, VT, N0,
3924                               DAG.getConstant(C, dl, N1.getValueType()),
3925                               NewCC);
3926         }
3927       }
3928     }
3929 
3930     if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3931       // X <= MAX --> true
3932       if (C1 == MaxVal)
3933         return DAG.getBoolConstant(true, dl, VT, OpVT);
3934 
3935       // X <= C0 --> X < (C0 + 1)
3936       if (!VT.isVector()) { // TODO: Support this for vectors.
3937         APInt C = C1 + 1;
3938         ISD::CondCode NewCC = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
3939         if ((DCI.isBeforeLegalizeOps() ||
3940              isCondCodeLegal(NewCC, VT.getSimpleVT())) &&
3941             (!N1C->isOpaque() || (C.getBitWidth() <= 64 &&
3942                                   isLegalICmpImmediate(C.getSExtValue())))) {
3943           return DAG.getSetCC(dl, VT, N0,
3944                               DAG.getConstant(C, dl, N1.getValueType()),
3945                               NewCC);
3946         }
3947       }
3948     }
3949 
3950     if (Cond == ISD::SETLT || Cond == ISD::SETULT) {
3951       if (C1 == MinVal)
3952         return DAG.getBoolConstant(false, dl, VT, OpVT); // X < MIN --> false
3953 
3954       // TODO: Support this for vectors after legalize ops.
3955       if (!VT.isVector() || DCI.isBeforeLegalizeOps()) {
3956         // Canonicalize setlt X, Max --> setne X, Max
3957         if (C1 == MaxVal)
3958           return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
3959 
3960         // If we have setult X, 1, turn it into seteq X, 0
3961         if (C1 == MinVal+1)
3962           return DAG.getSetCC(dl, VT, N0,
3963                               DAG.getConstant(MinVal, dl, N0.getValueType()),
3964                               ISD::SETEQ);
3965       }
3966     }
3967 
3968     if (Cond == ISD::SETGT || Cond == ISD::SETUGT) {
3969       if (C1 == MaxVal)
3970         return DAG.getBoolConstant(false, dl, VT, OpVT); // X > MAX --> false
3971 
3972       // TODO: Support this for vectors after legalize ops.
3973       if (!VT.isVector() || DCI.isBeforeLegalizeOps()) {
3974         // Canonicalize setgt X, Min --> setne X, Min
3975         if (C1 == MinVal)
3976           return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
3977 
3978         // If we have setugt X, Max-1, turn it into seteq X, Max
3979         if (C1 == MaxVal-1)
3980           return DAG.getSetCC(dl, VT, N0,
3981                               DAG.getConstant(MaxVal, dl, N0.getValueType()),
3982                               ISD::SETEQ);
3983       }
3984     }
3985 
3986     if (Cond == ISD::SETEQ || Cond == ISD::SETNE) {
3987       // (X & (C l>>/<< Y)) ==/!= 0  -->  ((X <</l>> Y) & C) ==/!= 0
3988       if (C1.isNullValue())
3989         if (SDValue CC = optimizeSetCCByHoistingAndByConstFromLogicalShift(
3990                 VT, N0, N1, Cond, DCI, dl))
3991           return CC;
3992 
3993       // For all/any comparisons, replace or(x,shl(y,bw/2)) with and/or(x,y).
3994       // For example, when high 32-bits of i64 X are known clear:
3995       // all bits clear: (X | (Y<<32)) ==  0 --> (X | Y) ==  0
3996       // all bits set:   (X | (Y<<32)) == -1 --> (X & Y) == -1
3997       bool CmpZero = N1C->getAPIntValue().isNullValue();
3998       bool CmpNegOne = N1C->getAPIntValue().isAllOnesValue();
3999       if ((CmpZero || CmpNegOne) && N0.hasOneUse()) {
4000         // Match or(lo,shl(hi,bw/2)) pattern.
4001         auto IsConcat = [&](SDValue V, SDValue &Lo, SDValue &Hi) {
4002           unsigned EltBits = V.getScalarValueSizeInBits();
4003           if (V.getOpcode() != ISD::OR || (EltBits % 2) != 0)
4004             return false;
4005           SDValue LHS = V.getOperand(0);
4006           SDValue RHS = V.getOperand(1);
4007           APInt HiBits = APInt::getHighBitsSet(EltBits, EltBits / 2);
4008           // Unshifted element must have zero upperbits.
4009           if (RHS.getOpcode() == ISD::SHL &&
4010               isa<ConstantSDNode>(RHS.getOperand(1)) &&
4011               RHS.getConstantOperandAPInt(1) == (EltBits / 2) &&
4012               DAG.MaskedValueIsZero(LHS, HiBits)) {
4013             Lo = LHS;
4014             Hi = RHS.getOperand(0);
4015             return true;
4016           }
4017           if (LHS.getOpcode() == ISD::SHL &&
4018               isa<ConstantSDNode>(LHS.getOperand(1)) &&
4019               LHS.getConstantOperandAPInt(1) == (EltBits / 2) &&
4020               DAG.MaskedValueIsZero(RHS, HiBits)) {
4021             Lo = RHS;
4022             Hi = LHS.getOperand(0);
4023             return true;
4024           }
4025           return false;
4026         };
4027 
4028         auto MergeConcat = [&](SDValue Lo, SDValue Hi) {
4029           unsigned EltBits = N0.getScalarValueSizeInBits();
4030           unsigned HalfBits = EltBits / 2;
4031           APInt HiBits = APInt::getHighBitsSet(EltBits, HalfBits);
4032           SDValue LoBits = DAG.getConstant(~HiBits, dl, OpVT);
4033           SDValue HiMask = DAG.getNode(ISD::AND, dl, OpVT, Hi, LoBits);
4034           SDValue NewN0 =
4035               DAG.getNode(CmpZero ? ISD::OR : ISD::AND, dl, OpVT, Lo, HiMask);
4036           SDValue NewN1 = CmpZero ? DAG.getConstant(0, dl, OpVT) : LoBits;
4037           return DAG.getSetCC(dl, VT, NewN0, NewN1, Cond);
4038         };
4039 
4040         SDValue Lo, Hi;
4041         if (IsConcat(N0, Lo, Hi))
4042           return MergeConcat(Lo, Hi);
4043 
4044         if (N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR) {
4045           SDValue Lo0, Lo1, Hi0, Hi1;
4046           if (IsConcat(N0.getOperand(0), Lo0, Hi0) &&
4047               IsConcat(N0.getOperand(1), Lo1, Hi1)) {
4048             return MergeConcat(DAG.getNode(N0.getOpcode(), dl, OpVT, Lo0, Lo1),
4049                                DAG.getNode(N0.getOpcode(), dl, OpVT, Hi0, Hi1));
4050           }
4051         }
4052       }
4053     }
4054 
4055     // If we have "setcc X, C0", check to see if we can shrink the immediate
4056     // by changing cc.
4057     // TODO: Support this for vectors after legalize ops.
4058     if (!VT.isVector() || DCI.isBeforeLegalizeOps()) {
4059       // SETUGT X, SINTMAX  -> SETLT X, 0
4060       // SETUGE X, SINTMIN -> SETLT X, 0
4061       if ((Cond == ISD::SETUGT && C1.isMaxSignedValue()) ||
4062           (Cond == ISD::SETUGE && C1.isMinSignedValue()))
4063         return DAG.getSetCC(dl, VT, N0,
4064                             DAG.getConstant(0, dl, N1.getValueType()),
4065                             ISD::SETLT);
4066 
4067       // SETULT X, SINTMIN  -> SETGT X, -1
4068       // SETULE X, SINTMAX  -> SETGT X, -1
4069       if ((Cond == ISD::SETULT && C1.isMinSignedValue()) ||
4070           (Cond == ISD::SETULE && C1.isMaxSignedValue()))
4071         return DAG.getSetCC(dl, VT, N0,
4072                             DAG.getAllOnesConstant(dl, N1.getValueType()),
4073                             ISD::SETGT);
4074     }
4075   }
4076 
4077   // Back to non-vector simplifications.
4078   // TODO: Can we do these for vector splats?
4079   if (auto *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
4080     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4081     const APInt &C1 = N1C->getAPIntValue();
4082     EVT ShValTy = N0.getValueType();
4083 
4084     // Fold bit comparisons when we can. This will result in an
4085     // incorrect value when boolean false is negative one, unless
4086     // the bitsize is 1 in which case the false value is the same
4087     // in practice regardless of the representation.
4088     if ((VT.getSizeInBits() == 1 ||
4089          getBooleanContents(N0.getValueType()) == ZeroOrOneBooleanContent) &&
4090         (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4091         (VT == ShValTy || (isTypeLegal(VT) && VT.bitsLE(ShValTy))) &&
4092         N0.getOpcode() == ISD::AND) {
4093       if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4094         EVT ShiftTy =
4095             getShiftAmountTy(ShValTy, Layout, !DCI.isBeforeLegalize());
4096         if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0  -->  (X & 8) >> 3
4097           // Perform the xform if the AND RHS is a single bit.
4098           unsigned ShCt = AndRHS->getAPIntValue().logBase2();
4099           if (AndRHS->getAPIntValue().isPowerOf2() &&
4100               !TLI.shouldAvoidTransformToShift(ShValTy, ShCt)) {
4101             return DAG.getNode(ISD::TRUNCATE, dl, VT,
4102                                DAG.getNode(ISD::SRL, dl, ShValTy, N0,
4103                                            DAG.getConstant(ShCt, dl, ShiftTy)));
4104           }
4105         } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
4106           // (X & 8) == 8  -->  (X & 8) >> 3
4107           // Perform the xform if C1 is a single bit.
4108           unsigned ShCt = C1.logBase2();
4109           if (C1.isPowerOf2() &&
4110               !TLI.shouldAvoidTransformToShift(ShValTy, ShCt)) {
4111             return DAG.getNode(ISD::TRUNCATE, dl, VT,
4112                                DAG.getNode(ISD::SRL, dl, ShValTy, N0,
4113                                            DAG.getConstant(ShCt, dl, ShiftTy)));
4114           }
4115         }
4116       }
4117     }
4118 
4119     if (C1.getMinSignedBits() <= 64 &&
4120         !isLegalICmpImmediate(C1.getSExtValue())) {
4121       EVT ShiftTy = getShiftAmountTy(ShValTy, Layout, !DCI.isBeforeLegalize());
4122       // (X & -256) == 256 -> (X >> 8) == 1
4123       if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4124           N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
4125         if (auto *AndRHS = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4126           const APInt &AndRHSC = AndRHS->getAPIntValue();
4127           if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
4128             unsigned ShiftBits = AndRHSC.countTrailingZeros();
4129             if (!TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) {
4130               SDValue Shift =
4131                 DAG.getNode(ISD::SRL, dl, ShValTy, N0.getOperand(0),
4132                             DAG.getConstant(ShiftBits, dl, ShiftTy));
4133               SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, ShValTy);
4134               return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
4135             }
4136           }
4137         }
4138       } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
4139                  Cond == ISD::SETULE || Cond == ISD::SETUGT) {
4140         bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
4141         // X <  0x100000000 -> (X >> 32) <  1
4142         // X >= 0x100000000 -> (X >> 32) >= 1
4143         // X <= 0x0ffffffff -> (X >> 32) <  1
4144         // X >  0x0ffffffff -> (X >> 32) >= 1
4145         unsigned ShiftBits;
4146         APInt NewC = C1;
4147         ISD::CondCode NewCond = Cond;
4148         if (AdjOne) {
4149           ShiftBits = C1.countTrailingOnes();
4150           NewC = NewC + 1;
4151           NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
4152         } else {
4153           ShiftBits = C1.countTrailingZeros();
4154         }
4155         NewC.lshrInPlace(ShiftBits);
4156         if (ShiftBits && NewC.getMinSignedBits() <= 64 &&
4157             isLegalICmpImmediate(NewC.getSExtValue()) &&
4158             !TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) {
4159           SDValue Shift = DAG.getNode(ISD::SRL, dl, ShValTy, N0,
4160                                       DAG.getConstant(ShiftBits, dl, ShiftTy));
4161           SDValue CmpRHS = DAG.getConstant(NewC, dl, ShValTy);
4162           return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
4163         }
4164       }
4165     }
4166   }
4167 
4168   if (!isa<ConstantFPSDNode>(N0) && isa<ConstantFPSDNode>(N1)) {
4169     auto *CFP = cast<ConstantFPSDNode>(N1);
4170     assert(!CFP->getValueAPF().isNaN() && "Unexpected NaN value");
4171 
4172     // Otherwise, we know the RHS is not a NaN.  Simplify the node to drop the
4173     // constant if knowing that the operand is non-nan is enough.  We prefer to
4174     // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
4175     // materialize 0.0.
4176     if (Cond == ISD::SETO || Cond == ISD::SETUO)
4177       return DAG.getSetCC(dl, VT, N0, N0, Cond);
4178 
4179     // setcc (fneg x), C -> setcc swap(pred) x, -C
4180     if (N0.getOpcode() == ISD::FNEG) {
4181       ISD::CondCode SwapCond = ISD::getSetCCSwappedOperands(Cond);
4182       if (DCI.isBeforeLegalizeOps() ||
4183           isCondCodeLegal(SwapCond, N0.getSimpleValueType())) {
4184         SDValue NegN1 = DAG.getNode(ISD::FNEG, dl, N0.getValueType(), N1);
4185         return DAG.getSetCC(dl, VT, N0.getOperand(0), NegN1, SwapCond);
4186       }
4187     }
4188 
4189     // If the condition is not legal, see if we can find an equivalent one
4190     // which is legal.
4191     if (!isCondCodeLegal(Cond, N0.getSimpleValueType())) {
4192       // If the comparison was an awkward floating-point == or != and one of
4193       // the comparison operands is infinity or negative infinity, convert the
4194       // condition to a less-awkward <= or >=.
4195       if (CFP->getValueAPF().isInfinity()) {
4196         bool IsNegInf = CFP->getValueAPF().isNegative();
4197         ISD::CondCode NewCond = ISD::SETCC_INVALID;
4198         switch (Cond) {
4199         case ISD::SETOEQ: NewCond = IsNegInf ? ISD::SETOLE : ISD::SETOGE; break;
4200         case ISD::SETUEQ: NewCond = IsNegInf ? ISD::SETULE : ISD::SETUGE; break;
4201         case ISD::SETUNE: NewCond = IsNegInf ? ISD::SETUGT : ISD::SETULT; break;
4202         case ISD::SETONE: NewCond = IsNegInf ? ISD::SETOGT : ISD::SETOLT; break;
4203         default: break;
4204         }
4205         if (NewCond != ISD::SETCC_INVALID &&
4206             isCondCodeLegal(NewCond, N0.getSimpleValueType()))
4207           return DAG.getSetCC(dl, VT, N0, N1, NewCond);
4208       }
4209     }
4210   }
4211 
4212   if (N0 == N1) {
4213     // The sext(setcc()) => setcc() optimization relies on the appropriate
4214     // constant being emitted.
4215     assert(!N0.getValueType().isInteger() &&
4216            "Integer types should be handled by FoldSetCC");
4217 
4218     bool EqTrue = ISD::isTrueWhenEqual(Cond);
4219     unsigned UOF = ISD::getUnorderedFlavor(Cond);
4220     if (UOF == 2) // FP operators that are undefined on NaNs.
4221       return DAG.getBoolConstant(EqTrue, dl, VT, OpVT);
4222     if (UOF == unsigned(EqTrue))
4223       return DAG.getBoolConstant(EqTrue, dl, VT, OpVT);
4224     // Otherwise, we can't fold it.  However, we can simplify it to SETUO/SETO
4225     // if it is not already.
4226     ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
4227     if (NewCond != Cond &&
4228         (DCI.isBeforeLegalizeOps() ||
4229                             isCondCodeLegal(NewCond, N0.getSimpleValueType())))
4230       return DAG.getSetCC(dl, VT, N0, N1, NewCond);
4231   }
4232 
4233   if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
4234       N0.getValueType().isInteger()) {
4235     if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
4236         N0.getOpcode() == ISD::XOR) {
4237       // Simplify (X+Y) == (X+Z) -->  Y == Z
4238       if (N0.getOpcode() == N1.getOpcode()) {
4239         if (N0.getOperand(0) == N1.getOperand(0))
4240           return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
4241         if (N0.getOperand(1) == N1.getOperand(1))
4242           return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
4243         if (isCommutativeBinOp(N0.getOpcode())) {
4244           // If X op Y == Y op X, try other combinations.
4245           if (N0.getOperand(0) == N1.getOperand(1))
4246             return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
4247                                 Cond);
4248           if (N0.getOperand(1) == N1.getOperand(0))
4249             return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
4250                                 Cond);
4251         }
4252       }
4253 
4254       // If RHS is a legal immediate value for a compare instruction, we need
4255       // to be careful about increasing register pressure needlessly.
4256       bool LegalRHSImm = false;
4257 
4258       if (auto *RHSC = dyn_cast<ConstantSDNode>(N1)) {
4259         if (auto *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
4260           // Turn (X+C1) == C2 --> X == C2-C1
4261           if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
4262             return DAG.getSetCC(dl, VT, N0.getOperand(0),
4263                                 DAG.getConstant(RHSC->getAPIntValue()-
4264                                                 LHSR->getAPIntValue(),
4265                                 dl, N0.getValueType()), Cond);
4266           }
4267 
4268           // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
4269           if (N0.getOpcode() == ISD::XOR)
4270             // If we know that all of the inverted bits are zero, don't bother
4271             // performing the inversion.
4272             if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
4273               return
4274                 DAG.getSetCC(dl, VT, N0.getOperand(0),
4275                              DAG.getConstant(LHSR->getAPIntValue() ^
4276                                                RHSC->getAPIntValue(),
4277                                              dl, N0.getValueType()),
4278                              Cond);
4279         }
4280 
4281         // Turn (C1-X) == C2 --> X == C1-C2
4282         if (auto *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
4283           if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
4284             return
4285               DAG.getSetCC(dl, VT, N0.getOperand(1),
4286                            DAG.getConstant(SUBC->getAPIntValue() -
4287                                              RHSC->getAPIntValue(),
4288                                            dl, N0.getValueType()),
4289                            Cond);
4290           }
4291         }
4292 
4293         // Could RHSC fold directly into a compare?
4294         if (RHSC->getValueType(0).getSizeInBits() <= 64)
4295           LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
4296       }
4297 
4298       // (X+Y) == X --> Y == 0 and similar folds.
4299       // Don't do this if X is an immediate that can fold into a cmp
4300       // instruction and X+Y has other uses. It could be an induction variable
4301       // chain, and the transform would increase register pressure.
4302       if (!LegalRHSImm || N0.hasOneUse())
4303         if (SDValue V = foldSetCCWithBinOp(VT, N0, N1, Cond, dl, DCI))
4304           return V;
4305     }
4306 
4307     if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
4308         N1.getOpcode() == ISD::XOR)
4309       if (SDValue V = foldSetCCWithBinOp(VT, N1, N0, Cond, dl, DCI))
4310         return V;
4311 
4312     if (SDValue V = foldSetCCWithAnd(VT, N0, N1, Cond, dl, DCI))
4313       return V;
4314   }
4315 
4316   // Fold remainder of division by a constant.
4317   if ((N0.getOpcode() == ISD::UREM || N0.getOpcode() == ISD::SREM) &&
4318       N0.hasOneUse() && (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
4319     AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
4320 
4321     // When division is cheap or optimizing for minimum size,
4322     // fall through to DIVREM creation by skipping this fold.
4323     if (!isIntDivCheap(VT, Attr) && !Attr.hasFnAttribute(Attribute::MinSize)) {
4324       if (N0.getOpcode() == ISD::UREM) {
4325         if (SDValue Folded = buildUREMEqFold(VT, N0, N1, Cond, DCI, dl))
4326           return Folded;
4327       } else if (N0.getOpcode() == ISD::SREM) {
4328         if (SDValue Folded = buildSREMEqFold(VT, N0, N1, Cond, DCI, dl))
4329           return Folded;
4330       }
4331     }
4332   }
4333 
4334   // Fold away ALL boolean setcc's.
4335   if (N0.getValueType().getScalarType() == MVT::i1 && foldBooleans) {
4336     SDValue Temp;
4337     switch (Cond) {
4338     default: llvm_unreachable("Unknown integer setcc!");
4339     case ISD::SETEQ:  // X == Y  -> ~(X^Y)
4340       Temp = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1);
4341       N0 = DAG.getNOT(dl, Temp, OpVT);
4342       if (!DCI.isCalledByLegalizer())
4343         DCI.AddToWorklist(Temp.getNode());
4344       break;
4345     case ISD::SETNE:  // X != Y   -->  (X^Y)
4346       N0 = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1);
4347       break;
4348     case ISD::SETGT:  // X >s Y   -->  X == 0 & Y == 1  -->  ~X & Y
4349     case ISD::SETULT: // X <u Y   -->  X == 0 & Y == 1  -->  ~X & Y
4350       Temp = DAG.getNOT(dl, N0, OpVT);
4351       N0 = DAG.getNode(ISD::AND, dl, OpVT, N1, Temp);
4352       if (!DCI.isCalledByLegalizer())
4353         DCI.AddToWorklist(Temp.getNode());
4354       break;
4355     case ISD::SETLT:  // X <s Y   --> X == 1 & Y == 0  -->  ~Y & X
4356     case ISD::SETUGT: // X >u Y   --> X == 1 & Y == 0  -->  ~Y & X
4357       Temp = DAG.getNOT(dl, N1, OpVT);
4358       N0 = DAG.getNode(ISD::AND, dl, OpVT, N0, Temp);
4359       if (!DCI.isCalledByLegalizer())
4360         DCI.AddToWorklist(Temp.getNode());
4361       break;
4362     case ISD::SETULE: // X <=u Y  --> X == 0 | Y == 1  -->  ~X | Y
4363     case ISD::SETGE:  // X >=s Y  --> X == 0 | Y == 1  -->  ~X | Y
4364       Temp = DAG.getNOT(dl, N0, OpVT);
4365       N0 = DAG.getNode(ISD::OR, dl, OpVT, N1, Temp);
4366       if (!DCI.isCalledByLegalizer())
4367         DCI.AddToWorklist(Temp.getNode());
4368       break;
4369     case ISD::SETUGE: // X >=u Y  --> X == 1 | Y == 0  -->  ~Y | X
4370     case ISD::SETLE:  // X <=s Y  --> X == 1 | Y == 0  -->  ~Y | X
4371       Temp = DAG.getNOT(dl, N1, OpVT);
4372       N0 = DAG.getNode(ISD::OR, dl, OpVT, N0, Temp);
4373       break;
4374     }
4375     if (VT.getScalarType() != MVT::i1) {
4376       if (!DCI.isCalledByLegalizer())
4377         DCI.AddToWorklist(N0.getNode());
4378       // FIXME: If running after legalize, we probably can't do this.
4379       ISD::NodeType ExtendCode = getExtendForContent(getBooleanContents(OpVT));
4380       N0 = DAG.getNode(ExtendCode, dl, VT, N0);
4381     }
4382     return N0;
4383   }
4384 
4385   // Could not fold it.
4386   return SDValue();
4387 }
4388 
4389 /// Returns true (and the GlobalValue and the offset) if the node is a
4390 /// GlobalAddress + offset.
4391 bool TargetLowering::isGAPlusOffset(SDNode *WN, const GlobalValue *&GA,
4392                                     int64_t &Offset) const {
4393 
4394   SDNode *N = unwrapAddress(SDValue(WN, 0)).getNode();
4395 
4396   if (auto *GASD = dyn_cast<GlobalAddressSDNode>(N)) {
4397     GA = GASD->getGlobal();
4398     Offset += GASD->getOffset();
4399     return true;
4400   }
4401 
4402   if (N->getOpcode() == ISD::ADD) {
4403     SDValue N1 = N->getOperand(0);
4404     SDValue N2 = N->getOperand(1);
4405     if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
4406       if (auto *V = dyn_cast<ConstantSDNode>(N2)) {
4407         Offset += V->getSExtValue();
4408         return true;
4409       }
4410     } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
4411       if (auto *V = dyn_cast<ConstantSDNode>(N1)) {
4412         Offset += V->getSExtValue();
4413         return true;
4414       }
4415     }
4416   }
4417 
4418   return false;
4419 }
4420 
4421 SDValue TargetLowering::PerformDAGCombine(SDNode *N,
4422                                           DAGCombinerInfo &DCI) const {
4423   // Default implementation: no optimization.
4424   return SDValue();
4425 }
4426 
4427 //===----------------------------------------------------------------------===//
4428 //  Inline Assembler Implementation Methods
4429 //===----------------------------------------------------------------------===//
4430 
4431 TargetLowering::ConstraintType
4432 TargetLowering::getConstraintType(StringRef Constraint) const {
4433   unsigned S = Constraint.size();
4434 
4435   if (S == 1) {
4436     switch (Constraint[0]) {
4437     default: break;
4438     case 'r':
4439       return C_RegisterClass;
4440     case 'm': // memory
4441     case 'o': // offsetable
4442     case 'V': // not offsetable
4443       return C_Memory;
4444     case 'n': // Simple Integer
4445     case 'E': // Floating Point Constant
4446     case 'F': // Floating Point Constant
4447       return C_Immediate;
4448     case 'i': // Simple Integer or Relocatable Constant
4449     case 's': // Relocatable Constant
4450     case 'p': // Address.
4451     case 'X': // Allow ANY value.
4452     case 'I': // Target registers.
4453     case 'J':
4454     case 'K':
4455     case 'L':
4456     case 'M':
4457     case 'N':
4458     case 'O':
4459     case 'P':
4460     case '<':
4461     case '>':
4462       return C_Other;
4463     }
4464   }
4465 
4466   if (S > 1 && Constraint[0] == '{' && Constraint[S - 1] == '}') {
4467     if (S == 8 && Constraint.substr(1, 6) == "memory") // "{memory}"
4468       return C_Memory;
4469     return C_Register;
4470   }
4471   return C_Unknown;
4472 }
4473 
4474 /// Try to replace an X constraint, which matches anything, with another that
4475 /// has more specific requirements based on the type of the corresponding
4476 /// operand.
4477 const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const {
4478   if (ConstraintVT.isInteger())
4479     return "r";
4480   if (ConstraintVT.isFloatingPoint())
4481     return "f"; // works for many targets
4482   return nullptr;
4483 }
4484 
4485 SDValue TargetLowering::LowerAsmOutputForConstraint(
4486     SDValue &Chain, SDValue &Flag, const SDLoc &DL,
4487     const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const {
4488   return SDValue();
4489 }
4490 
4491 /// Lower the specified operand into the Ops vector.
4492 /// If it is invalid, don't add anything to Ops.
4493 void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4494                                                   std::string &Constraint,
4495                                                   std::vector<SDValue> &Ops,
4496                                                   SelectionDAG &DAG) const {
4497 
4498   if (Constraint.length() > 1) return;
4499 
4500   char ConstraintLetter = Constraint[0];
4501   switch (ConstraintLetter) {
4502   default: break;
4503   case 'X':     // Allows any operand; labels (basic block) use this.
4504     if (Op.getOpcode() == ISD::BasicBlock ||
4505         Op.getOpcode() == ISD::TargetBlockAddress) {
4506       Ops.push_back(Op);
4507       return;
4508     }
4509     LLVM_FALLTHROUGH;
4510   case 'i':    // Simple Integer or Relocatable Constant
4511   case 'n':    // Simple Integer
4512   case 's': {  // Relocatable Constant
4513 
4514     GlobalAddressSDNode *GA;
4515     ConstantSDNode *C;
4516     BlockAddressSDNode *BA;
4517     uint64_t Offset = 0;
4518 
4519     // Match (GA) or (C) or (GA+C) or (GA-C) or ((GA+C)+C) or (((GA+C)+C)+C),
4520     // etc., since getelementpointer is variadic. We can't use
4521     // SelectionDAG::FoldSymbolOffset because it expects the GA to be accessible
4522     // while in this case the GA may be furthest from the root node which is
4523     // likely an ISD::ADD.
4524     while (1) {
4525       if ((GA = dyn_cast<GlobalAddressSDNode>(Op)) && ConstraintLetter != 'n') {
4526         Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(Op),
4527                                                  GA->getValueType(0),
4528                                                  Offset + GA->getOffset()));
4529         return;
4530       }
4531       if ((C = dyn_cast<ConstantSDNode>(Op)) && ConstraintLetter != 's') {
4532         // gcc prints these as sign extended.  Sign extend value to 64 bits
4533         // now; without this it would get ZExt'd later in
4534         // ScheduleDAGSDNodes::EmitNode, which is very generic.
4535         bool IsBool = C->getConstantIntValue()->getBitWidth() == 1;
4536         BooleanContent BCont = getBooleanContents(MVT::i64);
4537         ISD::NodeType ExtOpc =
4538             IsBool ? getExtendForContent(BCont) : ISD::SIGN_EXTEND;
4539         int64_t ExtVal =
4540             ExtOpc == ISD::ZERO_EXTEND ? C->getZExtValue() : C->getSExtValue();
4541         Ops.push_back(
4542             DAG.getTargetConstant(Offset + ExtVal, SDLoc(C), MVT::i64));
4543         return;
4544       }
4545       if ((BA = dyn_cast<BlockAddressSDNode>(Op)) && ConstraintLetter != 'n') {
4546         Ops.push_back(DAG.getTargetBlockAddress(
4547             BA->getBlockAddress(), BA->getValueType(0),
4548             Offset + BA->getOffset(), BA->getTargetFlags()));
4549         return;
4550       }
4551       const unsigned OpCode = Op.getOpcode();
4552       if (OpCode == ISD::ADD || OpCode == ISD::SUB) {
4553         if ((C = dyn_cast<ConstantSDNode>(Op.getOperand(0))))
4554           Op = Op.getOperand(1);
4555         // Subtraction is not commutative.
4556         else if (OpCode == ISD::ADD &&
4557                  (C = dyn_cast<ConstantSDNode>(Op.getOperand(1))))
4558           Op = Op.getOperand(0);
4559         else
4560           return;
4561         Offset += (OpCode == ISD::ADD ? 1 : -1) * C->getSExtValue();
4562         continue;
4563       }
4564       return;
4565     }
4566     break;
4567   }
4568   }
4569 }
4570 
4571 std::pair<unsigned, const TargetRegisterClass *>
4572 TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *RI,
4573                                              StringRef Constraint,
4574                                              MVT VT) const {
4575   if (Constraint.empty() || Constraint[0] != '{')
4576     return std::make_pair(0u, static_cast<TargetRegisterClass *>(nullptr));
4577   assert(*(Constraint.end() - 1) == '}' && "Not a brace enclosed constraint?");
4578 
4579   // Remove the braces from around the name.
4580   StringRef RegName(Constraint.data() + 1, Constraint.size() - 2);
4581 
4582   std::pair<unsigned, const TargetRegisterClass *> R =
4583       std::make_pair(0u, static_cast<const TargetRegisterClass *>(nullptr));
4584 
4585   // Figure out which register class contains this reg.
4586   for (const TargetRegisterClass *RC : RI->regclasses()) {
4587     // If none of the value types for this register class are valid, we
4588     // can't use it.  For example, 64-bit reg classes on 32-bit targets.
4589     if (!isLegalRC(*RI, *RC))
4590       continue;
4591 
4592     for (const MCPhysReg &PR : *RC) {
4593       if (RegName.equals_insensitive(RI->getRegAsmName(PR))) {
4594         std::pair<unsigned, const TargetRegisterClass *> S =
4595             std::make_pair(PR, RC);
4596 
4597         // If this register class has the requested value type, return it,
4598         // otherwise keep searching and return the first class found
4599         // if no other is found which explicitly has the requested type.
4600         if (RI->isTypeLegalForClass(*RC, VT))
4601           return S;
4602         if (!R.second)
4603           R = S;
4604       }
4605     }
4606   }
4607 
4608   return R;
4609 }
4610 
4611 //===----------------------------------------------------------------------===//
4612 // Constraint Selection.
4613 
4614 /// Return true of this is an input operand that is a matching constraint like
4615 /// "4".
4616 bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
4617   assert(!ConstraintCode.empty() && "No known constraint!");
4618   return isdigit(static_cast<unsigned char>(ConstraintCode[0]));
4619 }
4620 
4621 /// If this is an input matching constraint, this method returns the output
4622 /// operand it matches.
4623 unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
4624   assert(!ConstraintCode.empty() && "No known constraint!");
4625   return atoi(ConstraintCode.c_str());
4626 }
4627 
4628 /// Split up the constraint string from the inline assembly value into the
4629 /// specific constraints and their prefixes, and also tie in the associated
4630 /// operand values.
4631 /// If this returns an empty vector, and if the constraint string itself
4632 /// isn't empty, there was an error parsing.
4633 TargetLowering::AsmOperandInfoVector
4634 TargetLowering::ParseConstraints(const DataLayout &DL,
4635                                  const TargetRegisterInfo *TRI,
4636                                  const CallBase &Call) const {
4637   /// Information about all of the constraints.
4638   AsmOperandInfoVector ConstraintOperands;
4639   const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
4640   unsigned maCount = 0; // Largest number of multiple alternative constraints.
4641 
4642   // Do a prepass over the constraints, canonicalizing them, and building up the
4643   // ConstraintOperands list.
4644   unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4645   unsigned ResNo = 0; // ResNo - The result number of the next output.
4646 
4647   for (InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
4648     ConstraintOperands.emplace_back(std::move(CI));
4649     AsmOperandInfo &OpInfo = ConstraintOperands.back();
4650 
4651     // Update multiple alternative constraint count.
4652     if (OpInfo.multipleAlternatives.size() > maCount)
4653       maCount = OpInfo.multipleAlternatives.size();
4654 
4655     OpInfo.ConstraintVT = MVT::Other;
4656 
4657     // Compute the value type for each operand.
4658     switch (OpInfo.Type) {
4659     case InlineAsm::isOutput:
4660       // Indirect outputs just consume an argument.
4661       if (OpInfo.isIndirect) {
4662         OpInfo.CallOperandVal = Call.getArgOperand(ArgNo++);
4663         break;
4664       }
4665 
4666       // The return value of the call is this value.  As such, there is no
4667       // corresponding argument.
4668       assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
4669       if (StructType *STy = dyn_cast<StructType>(Call.getType())) {
4670         OpInfo.ConstraintVT =
4671             getSimpleValueType(DL, STy->getElementType(ResNo));
4672       } else {
4673         assert(ResNo == 0 && "Asm only has one result!");
4674         OpInfo.ConstraintVT = getSimpleValueType(DL, Call.getType());
4675       }
4676       ++ResNo;
4677       break;
4678     case InlineAsm::isInput:
4679       OpInfo.CallOperandVal = Call.getArgOperand(ArgNo++);
4680       break;
4681     case InlineAsm::isClobber:
4682       // Nothing to do.
4683       break;
4684     }
4685 
4686     if (OpInfo.CallOperandVal) {
4687       llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
4688       if (OpInfo.isIndirect) {
4689         llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4690         if (!PtrTy)
4691           report_fatal_error("Indirect operand for inline asm not a pointer!");
4692         OpTy = PtrTy->getElementType();
4693       }
4694 
4695       // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
4696       if (StructType *STy = dyn_cast<StructType>(OpTy))
4697         if (STy->getNumElements() == 1)
4698           OpTy = STy->getElementType(0);
4699 
4700       // If OpTy is not a single value, it may be a struct/union that we
4701       // can tile with integers.
4702       if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4703         unsigned BitSize = DL.getTypeSizeInBits(OpTy);
4704         switch (BitSize) {
4705         default: break;
4706         case 1:
4707         case 8:
4708         case 16:
4709         case 32:
4710         case 64:
4711         case 128:
4712           OpInfo.ConstraintVT =
4713               MVT::getVT(IntegerType::get(OpTy->getContext(), BitSize), true);
4714           break;
4715         }
4716       } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
4717         unsigned PtrSize = DL.getPointerSizeInBits(PT->getAddressSpace());
4718         OpInfo.ConstraintVT = MVT::getIntegerVT(PtrSize);
4719       } else {
4720         OpInfo.ConstraintVT = MVT::getVT(OpTy, true);
4721       }
4722     }
4723   }
4724 
4725   // If we have multiple alternative constraints, select the best alternative.
4726   if (!ConstraintOperands.empty()) {
4727     if (maCount) {
4728       unsigned bestMAIndex = 0;
4729       int bestWeight = -1;
4730       // weight:  -1 = invalid match, and 0 = so-so match to 5 = good match.
4731       int weight = -1;
4732       unsigned maIndex;
4733       // Compute the sums of the weights for each alternative, keeping track
4734       // of the best (highest weight) one so far.
4735       for (maIndex = 0; maIndex < maCount; ++maIndex) {
4736         int weightSum = 0;
4737         for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
4738              cIndex != eIndex; ++cIndex) {
4739           AsmOperandInfo &OpInfo = ConstraintOperands[cIndex];
4740           if (OpInfo.Type == InlineAsm::isClobber)
4741             continue;
4742 
4743           // If this is an output operand with a matching input operand,
4744           // look up the matching input. If their types mismatch, e.g. one
4745           // is an integer, the other is floating point, or their sizes are
4746           // different, flag it as an maCantMatch.
4747           if (OpInfo.hasMatchingInput()) {
4748             AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
4749             if (OpInfo.ConstraintVT != Input.ConstraintVT) {
4750               if ((OpInfo.ConstraintVT.isInteger() !=
4751                    Input.ConstraintVT.isInteger()) ||
4752                   (OpInfo.ConstraintVT.getSizeInBits() !=
4753                    Input.ConstraintVT.getSizeInBits())) {
4754                 weightSum = -1; // Can't match.
4755                 break;
4756               }
4757             }
4758           }
4759           weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
4760           if (weight == -1) {
4761             weightSum = -1;
4762             break;
4763           }
4764           weightSum += weight;
4765         }
4766         // Update best.
4767         if (weightSum > bestWeight) {
4768           bestWeight = weightSum;
4769           bestMAIndex = maIndex;
4770         }
4771       }
4772 
4773       // Now select chosen alternative in each constraint.
4774       for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
4775            cIndex != eIndex; ++cIndex) {
4776         AsmOperandInfo &cInfo = ConstraintOperands[cIndex];
4777         if (cInfo.Type == InlineAsm::isClobber)
4778           continue;
4779         cInfo.selectAlternative(bestMAIndex);
4780       }
4781     }
4782   }
4783 
4784   // Check and hook up tied operands, choose constraint code to use.
4785   for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
4786        cIndex != eIndex; ++cIndex) {
4787     AsmOperandInfo &OpInfo = ConstraintOperands[cIndex];
4788 
4789     // If this is an output operand with a matching input operand, look up the
4790     // matching input. If their types mismatch, e.g. one is an integer, the
4791     // other is floating point, or their sizes are different, flag it as an
4792     // error.
4793     if (OpInfo.hasMatchingInput()) {
4794       AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
4795 
4796       if (OpInfo.ConstraintVT != Input.ConstraintVT) {
4797         std::pair<unsigned, const TargetRegisterClass *> MatchRC =
4798             getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
4799                                          OpInfo.ConstraintVT);
4800         std::pair<unsigned, const TargetRegisterClass *> InputRC =
4801             getRegForInlineAsmConstraint(TRI, Input.ConstraintCode,
4802                                          Input.ConstraintVT);
4803         if ((OpInfo.ConstraintVT.isInteger() !=
4804              Input.ConstraintVT.isInteger()) ||
4805             (MatchRC.second != InputRC.second)) {
4806           report_fatal_error("Unsupported asm: input constraint"
4807                              " with a matching output constraint of"
4808                              " incompatible type!");
4809         }
4810       }
4811     }
4812   }
4813 
4814   return ConstraintOperands;
4815 }
4816 
4817 /// Return an integer indicating how general CT is.
4818 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
4819   switch (CT) {
4820   case TargetLowering::C_Immediate:
4821   case TargetLowering::C_Other:
4822   case TargetLowering::C_Unknown:
4823     return 0;
4824   case TargetLowering::C_Register:
4825     return 1;
4826   case TargetLowering::C_RegisterClass:
4827     return 2;
4828   case TargetLowering::C_Memory:
4829     return 3;
4830   }
4831   llvm_unreachable("Invalid constraint type");
4832 }
4833 
4834 /// Examine constraint type and operand type and determine a weight value.
4835 /// This object must already have been set up with the operand type
4836 /// and the current alternative constraint selected.
4837 TargetLowering::ConstraintWeight
4838   TargetLowering::getMultipleConstraintMatchWeight(
4839     AsmOperandInfo &info, int maIndex) const {
4840   InlineAsm::ConstraintCodeVector *rCodes;
4841   if (maIndex >= (int)info.multipleAlternatives.size())
4842     rCodes = &info.Codes;
4843   else
4844     rCodes = &info.multipleAlternatives[maIndex].Codes;
4845   ConstraintWeight BestWeight = CW_Invalid;
4846 
4847   // Loop over the options, keeping track of the most general one.
4848   for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
4849     ConstraintWeight weight =
4850       getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
4851     if (weight > BestWeight)
4852       BestWeight = weight;
4853   }
4854 
4855   return BestWeight;
4856 }
4857 
4858 /// Examine constraint type and operand type and determine a weight value.
4859 /// This object must already have been set up with the operand type
4860 /// and the current alternative constraint selected.
4861 TargetLowering::ConstraintWeight
4862   TargetLowering::getSingleConstraintMatchWeight(
4863     AsmOperandInfo &info, const char *constraint) const {
4864   ConstraintWeight weight = CW_Invalid;
4865   Value *CallOperandVal = info.CallOperandVal;
4866     // If we don't have a value, we can't do a match,
4867     // but allow it at the lowest weight.
4868   if (!CallOperandVal)
4869     return CW_Default;
4870   // Look at the constraint type.
4871   switch (*constraint) {
4872     case 'i': // immediate integer.
4873     case 'n': // immediate integer with a known value.
4874       if (isa<ConstantInt>(CallOperandVal))
4875         weight = CW_Constant;
4876       break;
4877     case 's': // non-explicit intregal immediate.
4878       if (isa<GlobalValue>(CallOperandVal))
4879         weight = CW_Constant;
4880       break;
4881     case 'E': // immediate float if host format.
4882     case 'F': // immediate float.
4883       if (isa<ConstantFP>(CallOperandVal))
4884         weight = CW_Constant;
4885       break;
4886     case '<': // memory operand with autodecrement.
4887     case '>': // memory operand with autoincrement.
4888     case 'm': // memory operand.
4889     case 'o': // offsettable memory operand
4890     case 'V': // non-offsettable memory operand
4891       weight = CW_Memory;
4892       break;
4893     case 'r': // general register.
4894     case 'g': // general register, memory operand or immediate integer.
4895               // note: Clang converts "g" to "imr".
4896       if (CallOperandVal->getType()->isIntegerTy())
4897         weight = CW_Register;
4898       break;
4899     case 'X': // any operand.
4900   default:
4901     weight = CW_Default;
4902     break;
4903   }
4904   return weight;
4905 }
4906 
4907 /// If there are multiple different constraints that we could pick for this
4908 /// operand (e.g. "imr") try to pick the 'best' one.
4909 /// This is somewhat tricky: constraints fall into four classes:
4910 ///    Other         -> immediates and magic values
4911 ///    Register      -> one specific register
4912 ///    RegisterClass -> a group of regs
4913 ///    Memory        -> memory
4914 /// Ideally, we would pick the most specific constraint possible: if we have
4915 /// something that fits into a register, we would pick it.  The problem here
4916 /// is that if we have something that could either be in a register or in
4917 /// memory that use of the register could cause selection of *other*
4918 /// operands to fail: they might only succeed if we pick memory.  Because of
4919 /// this the heuristic we use is:
4920 ///
4921 ///  1) If there is an 'other' constraint, and if the operand is valid for
4922 ///     that constraint, use it.  This makes us take advantage of 'i'
4923 ///     constraints when available.
4924 ///  2) Otherwise, pick the most general constraint present.  This prefers
4925 ///     'm' over 'r', for example.
4926 ///
4927 static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
4928                              const TargetLowering &TLI,
4929                              SDValue Op, SelectionDAG *DAG) {
4930   assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
4931   unsigned BestIdx = 0;
4932   TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
4933   int BestGenerality = -1;
4934 
4935   // Loop over the options, keeping track of the most general one.
4936   for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
4937     TargetLowering::ConstraintType CType =
4938       TLI.getConstraintType(OpInfo.Codes[i]);
4939 
4940     // Indirect 'other' or 'immediate' constraints are not allowed.
4941     if (OpInfo.isIndirect && !(CType == TargetLowering::C_Memory ||
4942                                CType == TargetLowering::C_Register ||
4943                                CType == TargetLowering::C_RegisterClass))
4944       continue;
4945 
4946     // If this is an 'other' or 'immediate' constraint, see if the operand is
4947     // valid for it. For example, on X86 we might have an 'rI' constraint. If
4948     // the operand is an integer in the range [0..31] we want to use I (saving a
4949     // load of a register), otherwise we must use 'r'.
4950     if ((CType == TargetLowering::C_Other ||
4951          CType == TargetLowering::C_Immediate) && Op.getNode()) {
4952       assert(OpInfo.Codes[i].size() == 1 &&
4953              "Unhandled multi-letter 'other' constraint");
4954       std::vector<SDValue> ResultOps;
4955       TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
4956                                        ResultOps, *DAG);
4957       if (!ResultOps.empty()) {
4958         BestType = CType;
4959         BestIdx = i;
4960         break;
4961       }
4962     }
4963 
4964     // Things with matching constraints can only be registers, per gcc
4965     // documentation.  This mainly affects "g" constraints.
4966     if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
4967       continue;
4968 
4969     // This constraint letter is more general than the previous one, use it.
4970     int Generality = getConstraintGenerality(CType);
4971     if (Generality > BestGenerality) {
4972       BestType = CType;
4973       BestIdx = i;
4974       BestGenerality = Generality;
4975     }
4976   }
4977 
4978   OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
4979   OpInfo.ConstraintType = BestType;
4980 }
4981 
4982 /// Determines the constraint code and constraint type to use for the specific
4983 /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType.
4984 void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
4985                                             SDValue Op,
4986                                             SelectionDAG *DAG) const {
4987   assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
4988 
4989   // Single-letter constraints ('r') are very common.
4990   if (OpInfo.Codes.size() == 1) {
4991     OpInfo.ConstraintCode = OpInfo.Codes[0];
4992     OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
4993   } else {
4994     ChooseConstraint(OpInfo, *this, Op, DAG);
4995   }
4996 
4997   // 'X' matches anything.
4998   if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
4999     // Labels and constants are handled elsewhere ('X' is the only thing
5000     // that matches labels).  For Functions, the type here is the type of
5001     // the result, which is not what we want to look at; leave them alone.
5002     Value *v = OpInfo.CallOperandVal;
5003     if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
5004       OpInfo.CallOperandVal = v;
5005       return;
5006     }
5007 
5008     if (Op.getNode() && Op.getOpcode() == ISD::TargetBlockAddress)
5009       return;
5010 
5011     // Otherwise, try to resolve it to something we know about by looking at
5012     // the actual operand type.
5013     if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
5014       OpInfo.ConstraintCode = Repl;
5015       OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
5016     }
5017   }
5018 }
5019 
5020 /// Given an exact SDIV by a constant, create a multiplication
5021 /// with the multiplicative inverse of the constant.
5022 static SDValue BuildExactSDIV(const TargetLowering &TLI, SDNode *N,
5023                               const SDLoc &dl, SelectionDAG &DAG,
5024                               SmallVectorImpl<SDNode *> &Created) {
5025   SDValue Op0 = N->getOperand(0);
5026   SDValue Op1 = N->getOperand(1);
5027   EVT VT = N->getValueType(0);
5028   EVT SVT = VT.getScalarType();
5029   EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
5030   EVT ShSVT = ShVT.getScalarType();
5031 
5032   bool UseSRA = false;
5033   SmallVector<SDValue, 16> Shifts, Factors;
5034 
5035   auto BuildSDIVPattern = [&](ConstantSDNode *C) {
5036     if (C->isNullValue())
5037       return false;
5038     APInt Divisor = C->getAPIntValue();
5039     unsigned Shift = Divisor.countTrailingZeros();
5040     if (Shift) {
5041       Divisor.ashrInPlace(Shift);
5042       UseSRA = true;
5043     }
5044     // Calculate the multiplicative inverse, using Newton's method.
5045     APInt t;
5046     APInt Factor = Divisor;
5047     while ((t = Divisor * Factor) != 1)
5048       Factor *= APInt(Divisor.getBitWidth(), 2) - t;
5049     Shifts.push_back(DAG.getConstant(Shift, dl, ShSVT));
5050     Factors.push_back(DAG.getConstant(Factor, dl, SVT));
5051     return true;
5052   };
5053 
5054   // Collect all magic values from the build vector.
5055   if (!ISD::matchUnaryPredicate(Op1, BuildSDIVPattern))
5056     return SDValue();
5057 
5058   SDValue Shift, Factor;
5059   if (Op1.getOpcode() == ISD::BUILD_VECTOR) {
5060     Shift = DAG.getBuildVector(ShVT, dl, Shifts);
5061     Factor = DAG.getBuildVector(VT, dl, Factors);
5062   } else if (Op1.getOpcode() == ISD::SPLAT_VECTOR) {
5063     assert(Shifts.size() == 1 && Factors.size() == 1 &&
5064            "Expected matchUnaryPredicate to return one element for scalable "
5065            "vectors");
5066     Shift = DAG.getSplatVector(ShVT, dl, Shifts[0]);
5067     Factor = DAG.getSplatVector(VT, dl, Factors[0]);
5068   } else {
5069     assert(isa<ConstantSDNode>(Op1) && "Expected a constant");
5070     Shift = Shifts[0];
5071     Factor = Factors[0];
5072   }
5073 
5074   SDValue Res = Op0;
5075 
5076   // Shift the value upfront if it is even, so the LSB is one.
5077   if (UseSRA) {
5078     // TODO: For UDIV use SRL instead of SRA.
5079     SDNodeFlags Flags;
5080     Flags.setExact(true);
5081     Res = DAG.getNode(ISD::SRA, dl, VT, Res, Shift, Flags);
5082     Created.push_back(Res.getNode());
5083   }
5084 
5085   return DAG.getNode(ISD::MUL, dl, VT, Res, Factor);
5086 }
5087 
5088 SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
5089                               SelectionDAG &DAG,
5090                               SmallVectorImpl<SDNode *> &Created) const {
5091   AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
5092   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5093   if (TLI.isIntDivCheap(N->getValueType(0), Attr))
5094     return SDValue(N, 0); // Lower SDIV as SDIV
5095   return SDValue();
5096 }
5097 
5098 /// Given an ISD::SDIV node expressing a divide by constant,
5099 /// return a DAG expression to select that will generate the same value by
5100 /// multiplying by a magic number.
5101 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
5102 SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
5103                                   bool IsAfterLegalization,
5104                                   SmallVectorImpl<SDNode *> &Created) const {
5105   SDLoc dl(N);
5106   EVT VT = N->getValueType(0);
5107   EVT SVT = VT.getScalarType();
5108   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
5109   EVT ShSVT = ShVT.getScalarType();
5110   unsigned EltBits = VT.getScalarSizeInBits();
5111   EVT MulVT;
5112 
5113   // Check to see if we can do this.
5114   // FIXME: We should be more aggressive here.
5115   if (!isTypeLegal(VT)) {
5116     // Limit this to simple scalars for now.
5117     if (VT.isVector() || !VT.isSimple())
5118       return SDValue();
5119 
5120     // If this type will be promoted to a large enough type with a legal
5121     // multiply operation, we can go ahead and do this transform.
5122     if (getTypeAction(VT.getSimpleVT()) != TypePromoteInteger)
5123       return SDValue();
5124 
5125     MulVT = getTypeToTransformTo(*DAG.getContext(), VT);
5126     if (MulVT.getSizeInBits() < (2 * EltBits) ||
5127         !isOperationLegal(ISD::MUL, MulVT))
5128       return SDValue();
5129   }
5130 
5131   // If the sdiv has an 'exact' bit we can use a simpler lowering.
5132   if (N->getFlags().hasExact())
5133     return BuildExactSDIV(*this, N, dl, DAG, Created);
5134 
5135   SmallVector<SDValue, 16> MagicFactors, Factors, Shifts, ShiftMasks;
5136 
5137   auto BuildSDIVPattern = [&](ConstantSDNode *C) {
5138     if (C->isNullValue())
5139       return false;
5140 
5141     const APInt &Divisor = C->getAPIntValue();
5142     APInt::ms magics = Divisor.magic();
5143     int NumeratorFactor = 0;
5144     int ShiftMask = -1;
5145 
5146     if (Divisor.isOneValue() || Divisor.isAllOnesValue()) {
5147       // If d is +1/-1, we just multiply the numerator by +1/-1.
5148       NumeratorFactor = Divisor.getSExtValue();
5149       magics.m = 0;
5150       magics.s = 0;
5151       ShiftMask = 0;
5152     } else if (Divisor.isStrictlyPositive() && magics.m.isNegative()) {
5153       // If d > 0 and m < 0, add the numerator.
5154       NumeratorFactor = 1;
5155     } else if (Divisor.isNegative() && magics.m.isStrictlyPositive()) {
5156       // If d < 0 and m > 0, subtract the numerator.
5157       NumeratorFactor = -1;
5158     }
5159 
5160     MagicFactors.push_back(DAG.getConstant(magics.m, dl, SVT));
5161     Factors.push_back(DAG.getConstant(NumeratorFactor, dl, SVT));
5162     Shifts.push_back(DAG.getConstant(magics.s, dl, ShSVT));
5163     ShiftMasks.push_back(DAG.getConstant(ShiftMask, dl, SVT));
5164     return true;
5165   };
5166 
5167   SDValue N0 = N->getOperand(0);
5168   SDValue N1 = N->getOperand(1);
5169 
5170   // Collect the shifts / magic values from each element.
5171   if (!ISD::matchUnaryPredicate(N1, BuildSDIVPattern))
5172     return SDValue();
5173 
5174   SDValue MagicFactor, Factor, Shift, ShiftMask;
5175   if (N1.getOpcode() == ISD::BUILD_VECTOR) {
5176     MagicFactor = DAG.getBuildVector(VT, dl, MagicFactors);
5177     Factor = DAG.getBuildVector(VT, dl, Factors);
5178     Shift = DAG.getBuildVector(ShVT, dl, Shifts);
5179     ShiftMask = DAG.getBuildVector(VT, dl, ShiftMasks);
5180   } else if (N1.getOpcode() == ISD::SPLAT_VECTOR) {
5181     assert(MagicFactors.size() == 1 && Factors.size() == 1 &&
5182            Shifts.size() == 1 && ShiftMasks.size() == 1 &&
5183            "Expected matchUnaryPredicate to return one element for scalable "
5184            "vectors");
5185     MagicFactor = DAG.getSplatVector(VT, dl, MagicFactors[0]);
5186     Factor = DAG.getSplatVector(VT, dl, Factors[0]);
5187     Shift = DAG.getSplatVector(ShVT, dl, Shifts[0]);
5188     ShiftMask = DAG.getSplatVector(VT, dl, ShiftMasks[0]);
5189   } else {
5190     assert(isa<ConstantSDNode>(N1) && "Expected a constant");
5191     MagicFactor = MagicFactors[0];
5192     Factor = Factors[0];
5193     Shift = Shifts[0];
5194     ShiftMask = ShiftMasks[0];
5195   }
5196 
5197   // Multiply the numerator (operand 0) by the magic value.
5198   // FIXME: We should support doing a MUL in a wider type.
5199   auto GetMULHS = [&](SDValue X, SDValue Y) {
5200     // If the type isn't legal, use a wider mul of the the type calculated
5201     // earlier.
5202     if (!isTypeLegal(VT)) {
5203       X = DAG.getNode(ISD::SIGN_EXTEND, dl, MulVT, X);
5204       Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MulVT, Y);
5205       Y = DAG.getNode(ISD::MUL, dl, MulVT, X, Y);
5206       Y = DAG.getNode(ISD::SRL, dl, MulVT, Y,
5207                       DAG.getShiftAmountConstant(EltBits, MulVT, dl));
5208       return DAG.getNode(ISD::TRUNCATE, dl, VT, Y);
5209     }
5210 
5211     if (isOperationLegalOrCustom(ISD::MULHS, VT, IsAfterLegalization))
5212       return DAG.getNode(ISD::MULHS, dl, VT, X, Y);
5213     if (isOperationLegalOrCustom(ISD::SMUL_LOHI, VT, IsAfterLegalization)) {
5214       SDValue LoHi =
5215           DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT), X, Y);
5216       return SDValue(LoHi.getNode(), 1);
5217     }
5218     return SDValue();
5219   };
5220 
5221   SDValue Q = GetMULHS(N0, MagicFactor);
5222   if (!Q)
5223     return SDValue();
5224 
5225   Created.push_back(Q.getNode());
5226 
5227   // (Optionally) Add/subtract the numerator using Factor.
5228   Factor = DAG.getNode(ISD::MUL, dl, VT, N0, Factor);
5229   Created.push_back(Factor.getNode());
5230   Q = DAG.getNode(ISD::ADD, dl, VT, Q, Factor);
5231   Created.push_back(Q.getNode());
5232 
5233   // Shift right algebraic by shift value.
5234   Q = DAG.getNode(ISD::SRA, dl, VT, Q, Shift);
5235   Created.push_back(Q.getNode());
5236 
5237   // Extract the sign bit, mask it and add it to the quotient.
5238   SDValue SignShift = DAG.getConstant(EltBits - 1, dl, ShVT);
5239   SDValue T = DAG.getNode(ISD::SRL, dl, VT, Q, SignShift);
5240   Created.push_back(T.getNode());
5241   T = DAG.getNode(ISD::AND, dl, VT, T, ShiftMask);
5242   Created.push_back(T.getNode());
5243   return DAG.getNode(ISD::ADD, dl, VT, Q, T);
5244 }
5245 
5246 /// Given an ISD::UDIV node expressing a divide by constant,
5247 /// return a DAG expression to select that will generate the same value by
5248 /// multiplying by a magic number.
5249 /// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
5250 SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
5251                                   bool IsAfterLegalization,
5252                                   SmallVectorImpl<SDNode *> &Created) const {
5253   SDLoc dl(N);
5254   EVT VT = N->getValueType(0);
5255   EVT SVT = VT.getScalarType();
5256   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
5257   EVT ShSVT = ShVT.getScalarType();
5258   unsigned EltBits = VT.getScalarSizeInBits();
5259   EVT MulVT;
5260 
5261   // Check to see if we can do this.
5262   // FIXME: We should be more aggressive here.
5263   if (!isTypeLegal(VT)) {
5264     // Limit this to simple scalars for now.
5265     if (VT.isVector() || !VT.isSimple())
5266       return SDValue();
5267 
5268     // If this type will be promoted to a large enough type with a legal
5269     // multiply operation, we can go ahead and do this transform.
5270     if (getTypeAction(VT.getSimpleVT()) != TypePromoteInteger)
5271       return SDValue();
5272 
5273     MulVT = getTypeToTransformTo(*DAG.getContext(), VT);
5274     if (MulVT.getSizeInBits() < (2 * EltBits) ||
5275         !isOperationLegal(ISD::MUL, MulVT))
5276       return SDValue();
5277   }
5278 
5279   bool UseNPQ = false;
5280   SmallVector<SDValue, 16> PreShifts, PostShifts, MagicFactors, NPQFactors;
5281 
5282   auto BuildUDIVPattern = [&](ConstantSDNode *C) {
5283     if (C->isNullValue())
5284       return false;
5285     // FIXME: We should use a narrower constant when the upper
5286     // bits are known to be zero.
5287     const APInt& Divisor = C->getAPIntValue();
5288     APInt::mu magics = Divisor.magicu();
5289     unsigned PreShift = 0, PostShift = 0;
5290 
5291     // If the divisor is even, we can avoid using the expensive fixup by
5292     // shifting the divided value upfront.
5293     if (magics.a != 0 && !Divisor[0]) {
5294       PreShift = Divisor.countTrailingZeros();
5295       // Get magic number for the shifted divisor.
5296       magics = Divisor.lshr(PreShift).magicu(PreShift);
5297       assert(magics.a == 0 && "Should use cheap fixup now");
5298     }
5299 
5300     APInt Magic = magics.m;
5301 
5302     unsigned SelNPQ;
5303     if (magics.a == 0 || Divisor.isOneValue()) {
5304       assert(magics.s < Divisor.getBitWidth() &&
5305              "We shouldn't generate an undefined shift!");
5306       PostShift = magics.s;
5307       SelNPQ = false;
5308     } else {
5309       PostShift = magics.s - 1;
5310       SelNPQ = true;
5311     }
5312 
5313     PreShifts.push_back(DAG.getConstant(PreShift, dl, ShSVT));
5314     MagicFactors.push_back(DAG.getConstant(Magic, dl, SVT));
5315     NPQFactors.push_back(
5316         DAG.getConstant(SelNPQ ? APInt::getOneBitSet(EltBits, EltBits - 1)
5317                                : APInt::getNullValue(EltBits),
5318                         dl, SVT));
5319     PostShifts.push_back(DAG.getConstant(PostShift, dl, ShSVT));
5320     UseNPQ |= SelNPQ;
5321     return true;
5322   };
5323 
5324   SDValue N0 = N->getOperand(0);
5325   SDValue N1 = N->getOperand(1);
5326 
5327   // Collect the shifts/magic values from each element.
5328   if (!ISD::matchUnaryPredicate(N1, BuildUDIVPattern))
5329     return SDValue();
5330 
5331   SDValue PreShift, PostShift, MagicFactor, NPQFactor;
5332   if (N1.getOpcode() == ISD::BUILD_VECTOR) {
5333     PreShift = DAG.getBuildVector(ShVT, dl, PreShifts);
5334     MagicFactor = DAG.getBuildVector(VT, dl, MagicFactors);
5335     NPQFactor = DAG.getBuildVector(VT, dl, NPQFactors);
5336     PostShift = DAG.getBuildVector(ShVT, dl, PostShifts);
5337   } else if (N1.getOpcode() == ISD::SPLAT_VECTOR) {
5338     assert(PreShifts.size() == 1 && MagicFactors.size() == 1 &&
5339            NPQFactors.size() == 1 && PostShifts.size() == 1 &&
5340            "Expected matchUnaryPredicate to return one for scalable vectors");
5341     PreShift = DAG.getSplatVector(ShVT, dl, PreShifts[0]);
5342     MagicFactor = DAG.getSplatVector(VT, dl, MagicFactors[0]);
5343     NPQFactor = DAG.getSplatVector(VT, dl, NPQFactors[0]);
5344     PostShift = DAG.getSplatVector(ShVT, dl, PostShifts[0]);
5345   } else {
5346     assert(isa<ConstantSDNode>(N1) && "Expected a constant");
5347     PreShift = PreShifts[0];
5348     MagicFactor = MagicFactors[0];
5349     PostShift = PostShifts[0];
5350   }
5351 
5352   SDValue Q = N0;
5353   Q = DAG.getNode(ISD::SRL, dl, VT, Q, PreShift);
5354   Created.push_back(Q.getNode());
5355 
5356   // FIXME: We should support doing a MUL in a wider type.
5357   auto GetMULHU = [&](SDValue X, SDValue Y) {
5358     // If the type isn't legal, use a wider mul of the the type calculated
5359     // earlier.
5360     if (!isTypeLegal(VT)) {
5361       X = DAG.getNode(ISD::ZERO_EXTEND, dl, MulVT, X);
5362       Y = DAG.getNode(ISD::ZERO_EXTEND, dl, MulVT, Y);
5363       Y = DAG.getNode(ISD::MUL, dl, MulVT, X, Y);
5364       Y = DAG.getNode(ISD::SRL, dl, MulVT, Y,
5365                       DAG.getShiftAmountConstant(EltBits, MulVT, dl));
5366       return DAG.getNode(ISD::TRUNCATE, dl, VT, Y);
5367     }
5368 
5369     if (isOperationLegalOrCustom(ISD::MULHU, VT, IsAfterLegalization))
5370       return DAG.getNode(ISD::MULHU, dl, VT, X, Y);
5371     if (isOperationLegalOrCustom(ISD::UMUL_LOHI, VT, IsAfterLegalization)) {
5372       SDValue LoHi =
5373           DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), X, Y);
5374       return SDValue(LoHi.getNode(), 1);
5375     }
5376     return SDValue(); // No mulhu or equivalent
5377   };
5378 
5379   // Multiply the numerator (operand 0) by the magic value.
5380   Q = GetMULHU(Q, MagicFactor);
5381   if (!Q)
5382     return SDValue();
5383 
5384   Created.push_back(Q.getNode());
5385 
5386   if (UseNPQ) {
5387     SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N0, Q);
5388     Created.push_back(NPQ.getNode());
5389 
5390     // For vectors we might have a mix of non-NPQ/NPQ paths, so use
5391     // MULHU to act as a SRL-by-1 for NPQ, else multiply by zero.
5392     if (VT.isVector())
5393       NPQ = GetMULHU(NPQ, NPQFactor);
5394     else
5395       NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ, DAG.getConstant(1, dl, ShVT));
5396 
5397     Created.push_back(NPQ.getNode());
5398 
5399     Q = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
5400     Created.push_back(Q.getNode());
5401   }
5402 
5403   Q = DAG.getNode(ISD::SRL, dl, VT, Q, PostShift);
5404   Created.push_back(Q.getNode());
5405 
5406   EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
5407 
5408   SDValue One = DAG.getConstant(1, dl, VT);
5409   SDValue IsOne = DAG.getSetCC(dl, SetCCVT, N1, One, ISD::SETEQ);
5410   return DAG.getSelect(dl, VT, IsOne, N0, Q);
5411 }
5412 
5413 /// If all values in Values that *don't* match the predicate are same 'splat'
5414 /// value, then replace all values with that splat value.
5415 /// Else, if AlternativeReplacement was provided, then replace all values that
5416 /// do match predicate with AlternativeReplacement value.
5417 static void
5418 turnVectorIntoSplatVector(MutableArrayRef<SDValue> Values,
5419                           std::function<bool(SDValue)> Predicate,
5420                           SDValue AlternativeReplacement = SDValue()) {
5421   SDValue Replacement;
5422   // Is there a value for which the Predicate does *NOT* match? What is it?
5423   auto SplatValue = llvm::find_if_not(Values, Predicate);
5424   if (SplatValue != Values.end()) {
5425     // Does Values consist only of SplatValue's and values matching Predicate?
5426     if (llvm::all_of(Values, [Predicate, SplatValue](SDValue Value) {
5427           return Value == *SplatValue || Predicate(Value);
5428         })) // Then we shall replace values matching predicate with SplatValue.
5429       Replacement = *SplatValue;
5430   }
5431   if (!Replacement) {
5432     // Oops, we did not find the "baseline" splat value.
5433     if (!AlternativeReplacement)
5434       return; // Nothing to do.
5435     // Let's replace with provided value then.
5436     Replacement = AlternativeReplacement;
5437   }
5438   std::replace_if(Values.begin(), Values.end(), Predicate, Replacement);
5439 }
5440 
5441 /// Given an ISD::UREM used only by an ISD::SETEQ or ISD::SETNE
5442 /// where the divisor is constant and the comparison target is zero,
5443 /// return a DAG expression that will generate the same comparison result
5444 /// using only multiplications, additions and shifts/rotations.
5445 /// Ref: "Hacker's Delight" 10-17.
5446 SDValue TargetLowering::buildUREMEqFold(EVT SETCCVT, SDValue REMNode,
5447                                         SDValue CompTargetNode,
5448                                         ISD::CondCode Cond,
5449                                         DAGCombinerInfo &DCI,
5450                                         const SDLoc &DL) const {
5451   SmallVector<SDNode *, 5> Built;
5452   if (SDValue Folded = prepareUREMEqFold(SETCCVT, REMNode, CompTargetNode, Cond,
5453                                          DCI, DL, Built)) {
5454     for (SDNode *N : Built)
5455       DCI.AddToWorklist(N);
5456     return Folded;
5457   }
5458 
5459   return SDValue();
5460 }
5461 
5462 SDValue
5463 TargetLowering::prepareUREMEqFold(EVT SETCCVT, SDValue REMNode,
5464                                   SDValue CompTargetNode, ISD::CondCode Cond,
5465                                   DAGCombinerInfo &DCI, const SDLoc &DL,
5466                                   SmallVectorImpl<SDNode *> &Created) const {
5467   // fold (seteq/ne (urem N, D), 0) -> (setule/ugt (rotr (mul N, P), K), Q)
5468   // - D must be constant, with D = D0 * 2^K where D0 is odd
5469   // - P is the multiplicative inverse of D0 modulo 2^W
5470   // - Q = floor(((2^W) - 1) / D)
5471   // where W is the width of the common type of N and D.
5472   assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
5473          "Only applicable for (in)equality comparisons.");
5474 
5475   SelectionDAG &DAG = DCI.DAG;
5476 
5477   EVT VT = REMNode.getValueType();
5478   EVT SVT = VT.getScalarType();
5479   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout(), !DCI.isBeforeLegalize());
5480   EVT ShSVT = ShVT.getScalarType();
5481 
5482   // If MUL is unavailable, we cannot proceed in any case.
5483   if (!DCI.isBeforeLegalizeOps() && !isOperationLegalOrCustom(ISD::MUL, VT))
5484     return SDValue();
5485 
5486   bool ComparingWithAllZeros = true;
5487   bool AllComparisonsWithNonZerosAreTautological = true;
5488   bool HadTautologicalLanes = false;
5489   bool AllLanesAreTautological = true;
5490   bool HadEvenDivisor = false;
5491   bool AllDivisorsArePowerOfTwo = true;
5492   bool HadTautologicalInvertedLanes = false;
5493   SmallVector<SDValue, 16> PAmts, KAmts, QAmts, IAmts;
5494 
5495   auto BuildUREMPattern = [&](ConstantSDNode *CDiv, ConstantSDNode *CCmp) {
5496     // Division by 0 is UB. Leave it to be constant-folded elsewhere.
5497     if (CDiv->isNullValue())
5498       return false;
5499 
5500     const APInt &D = CDiv->getAPIntValue();
5501     const APInt &Cmp = CCmp->getAPIntValue();
5502 
5503     ComparingWithAllZeros &= Cmp.isNullValue();
5504 
5505     // x u% C1` is *always* less than C1. So given `x u% C1 == C2`,
5506     // if C2 is not less than C1, the comparison is always false.
5507     // But we will only be able to produce the comparison that will give the
5508     // opposive tautological answer. So this lane would need to be fixed up.
5509     bool TautologicalInvertedLane = D.ule(Cmp);
5510     HadTautologicalInvertedLanes |= TautologicalInvertedLane;
5511 
5512     // If all lanes are tautological (either all divisors are ones, or divisor
5513     // is not greater than the constant we are comparing with),
5514     // we will prefer to avoid the fold.
5515     bool TautologicalLane = D.isOneValue() || TautologicalInvertedLane;
5516     HadTautologicalLanes |= TautologicalLane;
5517     AllLanesAreTautological &= TautologicalLane;
5518 
5519     // If we are comparing with non-zero, we need'll need  to subtract said
5520     // comparison value from the LHS. But there is no point in doing that if
5521     // every lane where we are comparing with non-zero is tautological..
5522     if (!Cmp.isNullValue())
5523       AllComparisonsWithNonZerosAreTautological &= TautologicalLane;
5524 
5525     // Decompose D into D0 * 2^K
5526     unsigned K = D.countTrailingZeros();
5527     assert((!D.isOneValue() || (K == 0)) && "For divisor '1' we won't rotate.");
5528     APInt D0 = D.lshr(K);
5529 
5530     // D is even if it has trailing zeros.
5531     HadEvenDivisor |= (K != 0);
5532     // D is a power-of-two if D0 is one.
5533     // If all divisors are power-of-two, we will prefer to avoid the fold.
5534     AllDivisorsArePowerOfTwo &= D0.isOneValue();
5535 
5536     // P = inv(D0, 2^W)
5537     // 2^W requires W + 1 bits, so we have to extend and then truncate.
5538     unsigned W = D.getBitWidth();
5539     APInt P = D0.zext(W + 1)
5540                   .multiplicativeInverse(APInt::getSignedMinValue(W + 1))
5541                   .trunc(W);
5542     assert(!P.isNullValue() && "No multiplicative inverse!"); // unreachable
5543     assert((D0 * P).isOneValue() && "Multiplicative inverse sanity check.");
5544 
5545     // Q = floor((2^W - 1) u/ D)
5546     // R = ((2^W - 1) u% D)
5547     APInt Q, R;
5548     APInt::udivrem(APInt::getAllOnesValue(W), D, Q, R);
5549 
5550     // If we are comparing with zero, then that comparison constant is okay,
5551     // else it may need to be one less than that.
5552     if (Cmp.ugt(R))
5553       Q -= 1;
5554 
5555     assert(APInt::getAllOnesValue(ShSVT.getSizeInBits()).ugt(K) &&
5556            "We are expecting that K is always less than all-ones for ShSVT");
5557 
5558     // If the lane is tautological the result can be constant-folded.
5559     if (TautologicalLane) {
5560       // Set P and K amount to a bogus values so we can try to splat them.
5561       P = 0;
5562       K = -1;
5563       // And ensure that comparison constant is tautological,
5564       // it will always compare true/false.
5565       Q = -1;
5566     }
5567 
5568     PAmts.push_back(DAG.getConstant(P, DL, SVT));
5569     KAmts.push_back(
5570         DAG.getConstant(APInt(ShSVT.getSizeInBits(), K), DL, ShSVT));
5571     QAmts.push_back(DAG.getConstant(Q, DL, SVT));
5572     return true;
5573   };
5574 
5575   SDValue N = REMNode.getOperand(0);
5576   SDValue D = REMNode.getOperand(1);
5577 
5578   // Collect the values from each element.
5579   if (!ISD::matchBinaryPredicate(D, CompTargetNode, BuildUREMPattern))
5580     return SDValue();
5581 
5582   // If all lanes are tautological, the result can be constant-folded.
5583   if (AllLanesAreTautological)
5584     return SDValue();
5585 
5586   // If this is a urem by a powers-of-two, avoid the fold since it can be
5587   // best implemented as a bit test.
5588   if (AllDivisorsArePowerOfTwo)
5589     return SDValue();
5590 
5591   SDValue PVal, KVal, QVal;
5592   if (VT.isVector()) {
5593     if (HadTautologicalLanes) {
5594       // Try to turn PAmts into a splat, since we don't care about the values
5595       // that are currently '0'. If we can't, just keep '0'`s.
5596       turnVectorIntoSplatVector(PAmts, isNullConstant);
5597       // Try to turn KAmts into a splat, since we don't care about the values
5598       // that are currently '-1'. If we can't, change them to '0'`s.
5599       turnVectorIntoSplatVector(KAmts, isAllOnesConstant,
5600                                 DAG.getConstant(0, DL, ShSVT));
5601     }
5602 
5603     PVal = DAG.getBuildVector(VT, DL, PAmts);
5604     KVal = DAG.getBuildVector(ShVT, DL, KAmts);
5605     QVal = DAG.getBuildVector(VT, DL, QAmts);
5606   } else {
5607     PVal = PAmts[0];
5608     KVal = KAmts[0];
5609     QVal = QAmts[0];
5610   }
5611 
5612   if (!ComparingWithAllZeros && !AllComparisonsWithNonZerosAreTautological) {
5613     if (!DCI.isBeforeLegalizeOps() && !isOperationLegalOrCustom(ISD::SUB, VT))
5614       return SDValue(); // FIXME: Could/should use `ISD::ADD`?
5615     assert(CompTargetNode.getValueType() == N.getValueType() &&
5616            "Expecting that the types on LHS and RHS of comparisons match.");
5617     N = DAG.getNode(ISD::SUB, DL, VT, N, CompTargetNode);
5618   }
5619 
5620   // (mul N, P)
5621   SDValue Op0 = DAG.getNode(ISD::MUL, DL, VT, N, PVal);
5622   Created.push_back(Op0.getNode());
5623 
5624   // Rotate right only if any divisor was even. We avoid rotates for all-odd
5625   // divisors as a performance improvement, since rotating by 0 is a no-op.
5626   if (HadEvenDivisor) {
5627     // We need ROTR to do this.
5628     if (!DCI.isBeforeLegalizeOps() && !isOperationLegalOrCustom(ISD::ROTR, VT))
5629       return SDValue();
5630     SDNodeFlags Flags;
5631     Flags.setExact(true);
5632     // UREM: (rotr (mul N, P), K)
5633     Op0 = DAG.getNode(ISD::ROTR, DL, VT, Op0, KVal, Flags);
5634     Created.push_back(Op0.getNode());
5635   }
5636 
5637   // UREM: (setule/setugt (rotr (mul N, P), K), Q)
5638   SDValue NewCC =
5639       DAG.getSetCC(DL, SETCCVT, Op0, QVal,
5640                    ((Cond == ISD::SETEQ) ? ISD::SETULE : ISD::SETUGT));
5641   if (!HadTautologicalInvertedLanes)
5642     return NewCC;
5643 
5644   // If any lanes previously compared always-false, the NewCC will give
5645   // always-true result for them, so we need to fixup those lanes.
5646   // Or the other way around for inequality predicate.
5647   assert(VT.isVector() && "Can/should only get here for vectors.");
5648   Created.push_back(NewCC.getNode());
5649 
5650   // x u% C1` is *always* less than C1. So given `x u% C1 == C2`,
5651   // if C2 is not less than C1, the comparison is always false.
5652   // But we have produced the comparison that will give the
5653   // opposive tautological answer. So these lanes would need to be fixed up.
5654   SDValue TautologicalInvertedChannels =
5655       DAG.getSetCC(DL, SETCCVT, D, CompTargetNode, ISD::SETULE);
5656   Created.push_back(TautologicalInvertedChannels.getNode());
5657 
5658   // NOTE: we avoid letting illegal types through even if we're before legalize
5659   // ops – legalization has a hard time producing good code for this.
5660   if (isOperationLegalOrCustom(ISD::VSELECT, SETCCVT)) {
5661     // If we have a vector select, let's replace the comparison results in the
5662     // affected lanes with the correct tautological result.
5663     SDValue Replacement = DAG.getBoolConstant(Cond == ISD::SETEQ ? false : true,
5664                                               DL, SETCCVT, SETCCVT);
5665     return DAG.getNode(ISD::VSELECT, DL, SETCCVT, TautologicalInvertedChannels,
5666                        Replacement, NewCC);
5667   }
5668 
5669   // Else, we can just invert the comparison result in the appropriate lanes.
5670   //
5671   // NOTE: see the note above VSELECT above.
5672   if (isOperationLegalOrCustom(ISD::XOR, SETCCVT))
5673     return DAG.getNode(ISD::XOR, DL, SETCCVT, NewCC,
5674                        TautologicalInvertedChannels);
5675 
5676   return SDValue(); // Don't know how to lower.
5677 }
5678 
5679 /// Given an ISD::SREM used only by an ISD::SETEQ or ISD::SETNE
5680 /// where the divisor is constant and the comparison target is zero,
5681 /// return a DAG expression that will generate the same comparison result
5682 /// using only multiplications, additions and shifts/rotations.
5683 /// Ref: "Hacker's Delight" 10-17.
5684 SDValue TargetLowering::buildSREMEqFold(EVT SETCCVT, SDValue REMNode,
5685                                         SDValue CompTargetNode,
5686                                         ISD::CondCode Cond,
5687                                         DAGCombinerInfo &DCI,
5688                                         const SDLoc &DL) const {
5689   SmallVector<SDNode *, 7> Built;
5690   if (SDValue Folded = prepareSREMEqFold(SETCCVT, REMNode, CompTargetNode, Cond,
5691                                          DCI, DL, Built)) {
5692     assert(Built.size() <= 7 && "Max size prediction failed.");
5693     for (SDNode *N : Built)
5694       DCI.AddToWorklist(N);
5695     return Folded;
5696   }
5697 
5698   return SDValue();
5699 }
5700 
5701 SDValue
5702 TargetLowering::prepareSREMEqFold(EVT SETCCVT, SDValue REMNode,
5703                                   SDValue CompTargetNode, ISD::CondCode Cond,
5704                                   DAGCombinerInfo &DCI, const SDLoc &DL,
5705                                   SmallVectorImpl<SDNode *> &Created) const {
5706   // Fold:
5707   //   (seteq/ne (srem N, D), 0)
5708   // To:
5709   //   (setule/ugt (rotr (add (mul N, P), A), K), Q)
5710   //
5711   // - D must be constant, with D = D0 * 2^K where D0 is odd
5712   // - P is the multiplicative inverse of D0 modulo 2^W
5713   // - A = bitwiseand(floor((2^(W - 1) - 1) / D0), (-(2^k)))
5714   // - Q = floor((2 * A) / (2^K))
5715   // where W is the width of the common type of N and D.
5716   assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
5717          "Only applicable for (in)equality comparisons.");
5718 
5719   SelectionDAG &DAG = DCI.DAG;
5720 
5721   EVT VT = REMNode.getValueType();
5722   EVT SVT = VT.getScalarType();
5723   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout(), !DCI.isBeforeLegalize());
5724   EVT ShSVT = ShVT.getScalarType();
5725 
5726   // If we are after ops legalization, and MUL is unavailable, we can not
5727   // proceed.
5728   if (!DCI.isBeforeLegalizeOps() && !isOperationLegalOrCustom(ISD::MUL, VT))
5729     return SDValue();
5730 
5731   // TODO: Could support comparing with non-zero too.
5732   ConstantSDNode *CompTarget = isConstOrConstSplat(CompTargetNode);
5733   if (!CompTarget || !CompTarget->isNullValue())
5734     return SDValue();
5735 
5736   bool HadIntMinDivisor = false;
5737   bool HadOneDivisor = false;
5738   bool AllDivisorsAreOnes = true;
5739   bool HadEvenDivisor = false;
5740   bool NeedToApplyOffset = false;
5741   bool AllDivisorsArePowerOfTwo = true;
5742   SmallVector<SDValue, 16> PAmts, AAmts, KAmts, QAmts;
5743 
5744   auto BuildSREMPattern = [&](ConstantSDNode *C) {
5745     // Division by 0 is UB. Leave it to be constant-folded elsewhere.
5746     if (C->isNullValue())
5747       return false;
5748 
5749     // FIXME: we don't fold `rem %X, -C` to `rem %X, C` in DAGCombine.
5750 
5751     // WARNING: this fold is only valid for positive divisors!
5752     APInt D = C->getAPIntValue();
5753     if (D.isNegative())
5754       D.negate(); //  `rem %X, -C` is equivalent to `rem %X, C`
5755 
5756     HadIntMinDivisor |= D.isMinSignedValue();
5757 
5758     // If all divisors are ones, we will prefer to avoid the fold.
5759     HadOneDivisor |= D.isOneValue();
5760     AllDivisorsAreOnes &= D.isOneValue();
5761 
5762     // Decompose D into D0 * 2^K
5763     unsigned K = D.countTrailingZeros();
5764     assert((!D.isOneValue() || (K == 0)) && "For divisor '1' we won't rotate.");
5765     APInt D0 = D.lshr(K);
5766 
5767     if (!D.isMinSignedValue()) {
5768       // D is even if it has trailing zeros; unless it's INT_MIN, in which case
5769       // we don't care about this lane in this fold, we'll special-handle it.
5770       HadEvenDivisor |= (K != 0);
5771     }
5772 
5773     // D is a power-of-two if D0 is one. This includes INT_MIN.
5774     // If all divisors are power-of-two, we will prefer to avoid the fold.
5775     AllDivisorsArePowerOfTwo &= D0.isOneValue();
5776 
5777     // P = inv(D0, 2^W)
5778     // 2^W requires W + 1 bits, so we have to extend and then truncate.
5779     unsigned W = D.getBitWidth();
5780     APInt P = D0.zext(W + 1)
5781                   .multiplicativeInverse(APInt::getSignedMinValue(W + 1))
5782                   .trunc(W);
5783     assert(!P.isNullValue() && "No multiplicative inverse!"); // unreachable
5784     assert((D0 * P).isOneValue() && "Multiplicative inverse sanity check.");
5785 
5786     // A = floor((2^(W - 1) - 1) / D0) & -2^K
5787     APInt A = APInt::getSignedMaxValue(W).udiv(D0);
5788     A.clearLowBits(K);
5789 
5790     if (!D.isMinSignedValue()) {
5791       // If divisor INT_MIN, then we don't care about this lane in this fold,
5792       // we'll special-handle it.
5793       NeedToApplyOffset |= A != 0;
5794     }
5795 
5796     // Q = floor((2 * A) / (2^K))
5797     APInt Q = (2 * A).udiv(APInt::getOneBitSet(W, K));
5798 
5799     assert(APInt::getAllOnesValue(SVT.getSizeInBits()).ugt(A) &&
5800            "We are expecting that A is always less than all-ones for SVT");
5801     assert(APInt::getAllOnesValue(ShSVT.getSizeInBits()).ugt(K) &&
5802            "We are expecting that K is always less than all-ones for ShSVT");
5803 
5804     // If the divisor is 1 the result can be constant-folded. Likewise, we
5805     // don't care about INT_MIN lanes, those can be set to undef if appropriate.
5806     if (D.isOneValue()) {
5807       // Set P, A and K to a bogus values so we can try to splat them.
5808       P = 0;
5809       A = -1;
5810       K = -1;
5811 
5812       // x ?% 1 == 0  <-->  true  <-->  x u<= -1
5813       Q = -1;
5814     }
5815 
5816     PAmts.push_back(DAG.getConstant(P, DL, SVT));
5817     AAmts.push_back(DAG.getConstant(A, DL, SVT));
5818     KAmts.push_back(
5819         DAG.getConstant(APInt(ShSVT.getSizeInBits(), K), DL, ShSVT));
5820     QAmts.push_back(DAG.getConstant(Q, DL, SVT));
5821     return true;
5822   };
5823 
5824   SDValue N = REMNode.getOperand(0);
5825   SDValue D = REMNode.getOperand(1);
5826 
5827   // Collect the values from each element.
5828   if (!ISD::matchUnaryPredicate(D, BuildSREMPattern))
5829     return SDValue();
5830 
5831   // If this is a srem by a one, avoid the fold since it can be constant-folded.
5832   if (AllDivisorsAreOnes)
5833     return SDValue();
5834 
5835   // If this is a srem by a powers-of-two (including INT_MIN), avoid the fold
5836   // since it can be best implemented as a bit test.
5837   if (AllDivisorsArePowerOfTwo)
5838     return SDValue();
5839 
5840   SDValue PVal, AVal, KVal, QVal;
5841   if (D.getOpcode() == ISD::BUILD_VECTOR) {
5842     if (HadOneDivisor) {
5843       // Try to turn PAmts into a splat, since we don't care about the values
5844       // that are currently '0'. If we can't, just keep '0'`s.
5845       turnVectorIntoSplatVector(PAmts, isNullConstant);
5846       // Try to turn AAmts into a splat, since we don't care about the
5847       // values that are currently '-1'. If we can't, change them to '0'`s.
5848       turnVectorIntoSplatVector(AAmts, isAllOnesConstant,
5849                                 DAG.getConstant(0, DL, SVT));
5850       // Try to turn KAmts into a splat, since we don't care about the values
5851       // that are currently '-1'. If we can't, change them to '0'`s.
5852       turnVectorIntoSplatVector(KAmts, isAllOnesConstant,
5853                                 DAG.getConstant(0, DL, ShSVT));
5854     }
5855 
5856     PVal = DAG.getBuildVector(VT, DL, PAmts);
5857     AVal = DAG.getBuildVector(VT, DL, AAmts);
5858     KVal = DAG.getBuildVector(ShVT, DL, KAmts);
5859     QVal = DAG.getBuildVector(VT, DL, QAmts);
5860   } else if (D.getOpcode() == ISD::SPLAT_VECTOR) {
5861     assert(PAmts.size() == 1 && AAmts.size() == 1 && KAmts.size() == 1 &&
5862            QAmts.size() == 1 &&
5863            "Expected matchUnaryPredicate to return one element for scalable "
5864            "vectors");
5865     PVal = DAG.getSplatVector(VT, DL, PAmts[0]);
5866     AVal = DAG.getSplatVector(VT, DL, AAmts[0]);
5867     KVal = DAG.getSplatVector(ShVT, DL, KAmts[0]);
5868     QVal = DAG.getSplatVector(VT, DL, QAmts[0]);
5869   } else {
5870     assert(isa<ConstantSDNode>(D) && "Expected a constant");
5871     PVal = PAmts[0];
5872     AVal = AAmts[0];
5873     KVal = KAmts[0];
5874     QVal = QAmts[0];
5875   }
5876 
5877   // (mul N, P)
5878   SDValue Op0 = DAG.getNode(ISD::MUL, DL, VT, N, PVal);
5879   Created.push_back(Op0.getNode());
5880 
5881   if (NeedToApplyOffset) {
5882     // We need ADD to do this.
5883     if (!DCI.isBeforeLegalizeOps() && !isOperationLegalOrCustom(ISD::ADD, VT))
5884       return SDValue();
5885 
5886     // (add (mul N, P), A)
5887     Op0 = DAG.getNode(ISD::ADD, DL, VT, Op0, AVal);
5888     Created.push_back(Op0.getNode());
5889   }
5890 
5891   // Rotate right only if any divisor was even. We avoid rotates for all-odd
5892   // divisors as a performance improvement, since rotating by 0 is a no-op.
5893   if (HadEvenDivisor) {
5894     // We need ROTR to do this.
5895     if (!DCI.isBeforeLegalizeOps() && !isOperationLegalOrCustom(ISD::ROTR, VT))
5896       return SDValue();
5897     SDNodeFlags Flags;
5898     Flags.setExact(true);
5899     // SREM: (rotr (add (mul N, P), A), K)
5900     Op0 = DAG.getNode(ISD::ROTR, DL, VT, Op0, KVal, Flags);
5901     Created.push_back(Op0.getNode());
5902   }
5903 
5904   // SREM: (setule/setugt (rotr (add (mul N, P), A), K), Q)
5905   SDValue Fold =
5906       DAG.getSetCC(DL, SETCCVT, Op0, QVal,
5907                    ((Cond == ISD::SETEQ) ? ISD::SETULE : ISD::SETUGT));
5908 
5909   // If we didn't have lanes with INT_MIN divisor, then we're done.
5910   if (!HadIntMinDivisor)
5911     return Fold;
5912 
5913   // That fold is only valid for positive divisors. Which effectively means,
5914   // it is invalid for INT_MIN divisors. So if we have such a lane,
5915   // we must fix-up results for said lanes.
5916   assert(VT.isVector() && "Can/should only get here for vectors.");
5917 
5918   // NOTE: we avoid letting illegal types through even if we're before legalize
5919   // ops – legalization has a hard time producing good code for the code that
5920   // follows.
5921   if (!isOperationLegalOrCustom(ISD::SETEQ, VT) ||
5922       !isOperationLegalOrCustom(ISD::AND, VT) ||
5923       !isOperationLegalOrCustom(Cond, VT) ||
5924       !isOperationLegalOrCustom(ISD::VSELECT, VT))
5925     return SDValue();
5926 
5927   Created.push_back(Fold.getNode());
5928 
5929   SDValue IntMin = DAG.getConstant(
5930       APInt::getSignedMinValue(SVT.getScalarSizeInBits()), DL, VT);
5931   SDValue IntMax = DAG.getConstant(
5932       APInt::getSignedMaxValue(SVT.getScalarSizeInBits()), DL, VT);
5933   SDValue Zero =
5934       DAG.getConstant(APInt::getNullValue(SVT.getScalarSizeInBits()), DL, VT);
5935 
5936   // Which lanes had INT_MIN divisors? Divisor is constant, so const-folded.
5937   SDValue DivisorIsIntMin = DAG.getSetCC(DL, SETCCVT, D, IntMin, ISD::SETEQ);
5938   Created.push_back(DivisorIsIntMin.getNode());
5939 
5940   // (N s% INT_MIN) ==/!= 0  <-->  (N & INT_MAX) ==/!= 0
5941   SDValue Masked = DAG.getNode(ISD::AND, DL, VT, N, IntMax);
5942   Created.push_back(Masked.getNode());
5943   SDValue MaskedIsZero = DAG.getSetCC(DL, SETCCVT, Masked, Zero, Cond);
5944   Created.push_back(MaskedIsZero.getNode());
5945 
5946   // To produce final result we need to blend 2 vectors: 'SetCC' and
5947   // 'MaskedIsZero'. If the divisor for channel was *NOT* INT_MIN, we pick
5948   // from 'Fold', else pick from 'MaskedIsZero'. Since 'DivisorIsIntMin' is
5949   // constant-folded, select can get lowered to a shuffle with constant mask.
5950   SDValue Blended =
5951       DAG.getNode(ISD::VSELECT, DL, VT, DivisorIsIntMin, MaskedIsZero, Fold);
5952 
5953   return Blended;
5954 }
5955 
5956 bool TargetLowering::
5957 verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const {
5958   if (!isa<ConstantSDNode>(Op.getOperand(0))) {
5959     DAG.getContext()->emitError("argument to '__builtin_return_address' must "
5960                                 "be a constant integer");
5961     return true;
5962   }
5963 
5964   return false;
5965 }
5966 
5967 SDValue TargetLowering::getSqrtInputTest(SDValue Op, SelectionDAG &DAG,
5968                                          const DenormalMode &Mode) const {
5969   SDLoc DL(Op);
5970   EVT VT = Op.getValueType();
5971   EVT CCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
5972   SDValue FPZero = DAG.getConstantFP(0.0, DL, VT);
5973   // Testing it with denormal inputs to avoid wrong estimate.
5974   if (Mode.Input == DenormalMode::IEEE) {
5975     // This is specifically a check for the handling of denormal inputs,
5976     // not the result.
5977 
5978     // Test = fabs(X) < SmallestNormal
5979     const fltSemantics &FltSem = DAG.EVTToAPFloatSemantics(VT);
5980     APFloat SmallestNorm = APFloat::getSmallestNormalized(FltSem);
5981     SDValue NormC = DAG.getConstantFP(SmallestNorm, DL, VT);
5982     SDValue Fabs = DAG.getNode(ISD::FABS, DL, VT, Op);
5983     return DAG.getSetCC(DL, CCVT, Fabs, NormC, ISD::SETLT);
5984   }
5985   // Test = X == 0.0
5986   return DAG.getSetCC(DL, CCVT, Op, FPZero, ISD::SETEQ);
5987 }
5988 
5989 SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
5990                                              bool LegalOps, bool OptForSize,
5991                                              NegatibleCost &Cost,
5992                                              unsigned Depth) const {
5993   // fneg is removable even if it has multiple uses.
5994   if (Op.getOpcode() == ISD::FNEG) {
5995     Cost = NegatibleCost::Cheaper;
5996     return Op.getOperand(0);
5997   }
5998 
5999   // Don't recurse exponentially.
6000   if (Depth > SelectionDAG::MaxRecursionDepth)
6001     return SDValue();
6002 
6003   // Pre-increment recursion depth for use in recursive calls.
6004   ++Depth;
6005   const SDNodeFlags Flags = Op->getFlags();
6006   const TargetOptions &Options = DAG.getTarget().Options;
6007   EVT VT = Op.getValueType();
6008   unsigned Opcode = Op.getOpcode();
6009 
6010   // Don't allow anything with multiple uses unless we know it is free.
6011   if (!Op.hasOneUse() && Opcode != ISD::ConstantFP) {
6012     bool IsFreeExtend = Opcode == ISD::FP_EXTEND &&
6013                         isFPExtFree(VT, Op.getOperand(0).getValueType());
6014     if (!IsFreeExtend)
6015       return SDValue();
6016   }
6017 
6018   auto RemoveDeadNode = [&](SDValue N) {
6019     if (N && N.getNode()->use_empty())
6020       DAG.RemoveDeadNode(N.getNode());
6021   };
6022 
6023   SDLoc DL(Op);
6024 
6025   // Because getNegatedExpression can delete nodes we need a handle to keep
6026   // temporary nodes alive in case the recursion manages to create an identical
6027   // node.
6028   std::list<HandleSDNode> Handles;
6029 
6030   switch (Opcode) {
6031   case ISD::ConstantFP: {
6032     // Don't invert constant FP values after legalization unless the target says
6033     // the negated constant is legal.
6034     bool IsOpLegal =
6035         isOperationLegal(ISD::ConstantFP, VT) ||
6036         isFPImmLegal(neg(cast<ConstantFPSDNode>(Op)->getValueAPF()), VT,
6037                      OptForSize);
6038 
6039     if (LegalOps && !IsOpLegal)
6040       break;
6041 
6042     APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
6043     V.changeSign();
6044     SDValue CFP = DAG.getConstantFP(V, DL, VT);
6045 
6046     // If we already have the use of the negated floating constant, it is free
6047     // to negate it even it has multiple uses.
6048     if (!Op.hasOneUse() && CFP.use_empty())
6049       break;
6050     Cost = NegatibleCost::Neutral;
6051     return CFP;
6052   }
6053   case ISD::BUILD_VECTOR: {
6054     // Only permit BUILD_VECTOR of constants.
6055     if (llvm::any_of(Op->op_values(), [&](SDValue N) {
6056           return !N.isUndef() && !isa<ConstantFPSDNode>(N);
6057         }))
6058       break;
6059 
6060     bool IsOpLegal =
6061         (isOperationLegal(ISD::ConstantFP, VT) &&
6062          isOperationLegal(ISD::BUILD_VECTOR, VT)) ||
6063         llvm::all_of(Op->op_values(), [&](SDValue N) {
6064           return N.isUndef() ||
6065                  isFPImmLegal(neg(cast<ConstantFPSDNode>(N)->getValueAPF()), VT,
6066                               OptForSize);
6067         });
6068 
6069     if (LegalOps && !IsOpLegal)
6070       break;
6071 
6072     SmallVector<SDValue, 4> Ops;
6073     for (SDValue C : Op->op_values()) {
6074       if (C.isUndef()) {
6075         Ops.push_back(C);
6076         continue;
6077       }
6078       APFloat V = cast<ConstantFPSDNode>(C)->getValueAPF();
6079       V.changeSign();
6080       Ops.push_back(DAG.getConstantFP(V, DL, C.getValueType()));
6081     }
6082     Cost = NegatibleCost::Neutral;
6083     return DAG.getBuildVector(VT, DL, Ops);
6084   }
6085   case ISD::FADD: {
6086     if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros())
6087       break;
6088 
6089     // After operation legalization, it might not be legal to create new FSUBs.
6090     if (LegalOps && !isOperationLegalOrCustom(ISD::FSUB, VT))
6091       break;
6092     SDValue X = Op.getOperand(0), Y = Op.getOperand(1);
6093 
6094     // fold (fneg (fadd X, Y)) -> (fsub (fneg X), Y)
6095     NegatibleCost CostX = NegatibleCost::Expensive;
6096     SDValue NegX =
6097         getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth);
6098     // Prevent this node from being deleted by the next call.
6099     if (NegX)
6100       Handles.emplace_back(NegX);
6101 
6102     // fold (fneg (fadd X, Y)) -> (fsub (fneg Y), X)
6103     NegatibleCost CostY = NegatibleCost::Expensive;
6104     SDValue NegY =
6105         getNegatedExpression(Y, DAG, LegalOps, OptForSize, CostY, Depth);
6106 
6107     // We're done with the handles.
6108     Handles.clear();
6109 
6110     // Negate the X if its cost is less or equal than Y.
6111     if (NegX && (CostX <= CostY)) {
6112       Cost = CostX;
6113       SDValue N = DAG.getNode(ISD::FSUB, DL, VT, NegX, Y, Flags);
6114       if (NegY != N)
6115         RemoveDeadNode(NegY);
6116       return N;
6117     }
6118 
6119     // Negate the Y if it is not expensive.
6120     if (NegY) {
6121       Cost = CostY;
6122       SDValue N = DAG.getNode(ISD::FSUB, DL, VT, NegY, X, Flags);
6123       if (NegX != N)
6124         RemoveDeadNode(NegX);
6125       return N;
6126     }
6127     break;
6128   }
6129   case ISD::FSUB: {
6130     // We can't turn -(A-B) into B-A when we honor signed zeros.
6131     if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros())
6132       break;
6133 
6134     SDValue X = Op.getOperand(0), Y = Op.getOperand(1);
6135     // fold (fneg (fsub 0, Y)) -> Y
6136     if (ConstantFPSDNode *C = isConstOrConstSplatFP(X, /*AllowUndefs*/ true))
6137       if (C->isZero()) {
6138         Cost = NegatibleCost::Cheaper;
6139         return Y;
6140       }
6141 
6142     // fold (fneg (fsub X, Y)) -> (fsub Y, X)
6143     Cost = NegatibleCost::Neutral;
6144     return DAG.getNode(ISD::FSUB, DL, VT, Y, X, Flags);
6145   }
6146   case ISD::FMUL:
6147   case ISD::FDIV: {
6148     SDValue X = Op.getOperand(0), Y = Op.getOperand(1);
6149 
6150     // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
6151     NegatibleCost CostX = NegatibleCost::Expensive;
6152     SDValue NegX =
6153         getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth);
6154     // Prevent this node from being deleted by the next call.
6155     if (NegX)
6156       Handles.emplace_back(NegX);
6157 
6158     // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
6159     NegatibleCost CostY = NegatibleCost::Expensive;
6160     SDValue NegY =
6161         getNegatedExpression(Y, DAG, LegalOps, OptForSize, CostY, Depth);
6162 
6163     // We're done with the handles.
6164     Handles.clear();
6165 
6166     // Negate the X if its cost is less or equal than Y.
6167     if (NegX && (CostX <= CostY)) {
6168       Cost = CostX;
6169       SDValue N = DAG.getNode(Opcode, DL, VT, NegX, Y, Flags);
6170       if (NegY != N)
6171         RemoveDeadNode(NegY);
6172       return N;
6173     }
6174 
6175     // Ignore X * 2.0 because that is expected to be canonicalized to X + X.
6176     if (auto *C = isConstOrConstSplatFP(Op.getOperand(1)))
6177       if (C->isExactlyValue(2.0) && Op.getOpcode() == ISD::FMUL)
6178         break;
6179 
6180     // Negate the Y if it is not expensive.
6181     if (NegY) {
6182       Cost = CostY;
6183       SDValue N = DAG.getNode(Opcode, DL, VT, X, NegY, Flags);
6184       if (NegX != N)
6185         RemoveDeadNode(NegX);
6186       return N;
6187     }
6188     break;
6189   }
6190   case ISD::FMA:
6191   case ISD::FMAD: {
6192     if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros())
6193       break;
6194 
6195     SDValue X = Op.getOperand(0), Y = Op.getOperand(1), Z = Op.getOperand(2);
6196     NegatibleCost CostZ = NegatibleCost::Expensive;
6197     SDValue NegZ =
6198         getNegatedExpression(Z, DAG, LegalOps, OptForSize, CostZ, Depth);
6199     // Give up if fail to negate the Z.
6200     if (!NegZ)
6201       break;
6202 
6203     // Prevent this node from being deleted by the next two calls.
6204     Handles.emplace_back(NegZ);
6205 
6206     // fold (fneg (fma X, Y, Z)) -> (fma (fneg X), Y, (fneg Z))
6207     NegatibleCost CostX = NegatibleCost::Expensive;
6208     SDValue NegX =
6209         getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth);
6210     // Prevent this node from being deleted by the next call.
6211     if (NegX)
6212       Handles.emplace_back(NegX);
6213 
6214     // fold (fneg (fma X, Y, Z)) -> (fma X, (fneg Y), (fneg Z))
6215     NegatibleCost CostY = NegatibleCost::Expensive;
6216     SDValue NegY =
6217         getNegatedExpression(Y, DAG, LegalOps, OptForSize, CostY, Depth);
6218 
6219     // We're done with the handles.
6220     Handles.clear();
6221 
6222     // Negate the X if its cost is less or equal than Y.
6223     if (NegX && (CostX <= CostY)) {
6224       Cost = std::min(CostX, CostZ);
6225       SDValue N = DAG.getNode(Opcode, DL, VT, NegX, Y, NegZ, Flags);
6226       if (NegY != N)
6227         RemoveDeadNode(NegY);
6228       return N;
6229     }
6230 
6231     // Negate the Y if it is not expensive.
6232     if (NegY) {
6233       Cost = std::min(CostY, CostZ);
6234       SDValue N = DAG.getNode(Opcode, DL, VT, X, NegY, NegZ, Flags);
6235       if (NegX != N)
6236         RemoveDeadNode(NegX);
6237       return N;
6238     }
6239     break;
6240   }
6241 
6242   case ISD::FP_EXTEND:
6243   case ISD::FSIN:
6244     if (SDValue NegV = getNegatedExpression(Op.getOperand(0), DAG, LegalOps,
6245                                             OptForSize, Cost, Depth))
6246       return DAG.getNode(Opcode, DL, VT, NegV);
6247     break;
6248   case ISD::FP_ROUND:
6249     if (SDValue NegV = getNegatedExpression(Op.getOperand(0), DAG, LegalOps,
6250                                             OptForSize, Cost, Depth))
6251       return DAG.getNode(ISD::FP_ROUND, DL, VT, NegV, Op.getOperand(1));
6252     break;
6253   }
6254 
6255   return SDValue();
6256 }
6257 
6258 //===----------------------------------------------------------------------===//
6259 // Legalization Utilities
6260 //===----------------------------------------------------------------------===//
6261 
6262 bool TargetLowering::expandMUL_LOHI(unsigned Opcode, EVT VT, const SDLoc &dl,
6263                                     SDValue LHS, SDValue RHS,
6264                                     SmallVectorImpl<SDValue> &Result,
6265                                     EVT HiLoVT, SelectionDAG &DAG,
6266                                     MulExpansionKind Kind, SDValue LL,
6267                                     SDValue LH, SDValue RL, SDValue RH) const {
6268   assert(Opcode == ISD::MUL || Opcode == ISD::UMUL_LOHI ||
6269          Opcode == ISD::SMUL_LOHI);
6270 
6271   bool HasMULHS = (Kind == MulExpansionKind::Always) ||
6272                   isOperationLegalOrCustom(ISD::MULHS, HiLoVT);
6273   bool HasMULHU = (Kind == MulExpansionKind::Always) ||
6274                   isOperationLegalOrCustom(ISD::MULHU, HiLoVT);
6275   bool HasSMUL_LOHI = (Kind == MulExpansionKind::Always) ||
6276                       isOperationLegalOrCustom(ISD::SMUL_LOHI, HiLoVT);
6277   bool HasUMUL_LOHI = (Kind == MulExpansionKind::Always) ||
6278                       isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT);
6279 
6280   if (!HasMULHU && !HasMULHS && !HasUMUL_LOHI && !HasSMUL_LOHI)
6281     return false;
6282 
6283   unsigned OuterBitSize = VT.getScalarSizeInBits();
6284   unsigned InnerBitSize = HiLoVT.getScalarSizeInBits();
6285 
6286   // LL, LH, RL, and RH must be either all NULL or all set to a value.
6287   assert((LL.getNode() && LH.getNode() && RL.getNode() && RH.getNode()) ||
6288          (!LL.getNode() && !LH.getNode() && !RL.getNode() && !RH.getNode()));
6289 
6290   SDVTList VTs = DAG.getVTList(HiLoVT, HiLoVT);
6291   auto MakeMUL_LOHI = [&](SDValue L, SDValue R, SDValue &Lo, SDValue &Hi,
6292                           bool Signed) -> bool {
6293     if ((Signed && HasSMUL_LOHI) || (!Signed && HasUMUL_LOHI)) {
6294       Lo = DAG.getNode(Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI, dl, VTs, L, R);
6295       Hi = SDValue(Lo.getNode(), 1);
6296       return true;
6297     }
6298     if ((Signed && HasMULHS) || (!Signed && HasMULHU)) {
6299       Lo = DAG.getNode(ISD::MUL, dl, HiLoVT, L, R);
6300       Hi = DAG.getNode(Signed ? ISD::MULHS : ISD::MULHU, dl, HiLoVT, L, R);
6301       return true;
6302     }
6303     return false;
6304   };
6305 
6306   SDValue Lo, Hi;
6307 
6308   if (!LL.getNode() && !RL.getNode() &&
6309       isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
6310     LL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LHS);
6311     RL = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RHS);
6312   }
6313 
6314   if (!LL.getNode())
6315     return false;
6316 
6317   APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6318   if (DAG.MaskedValueIsZero(LHS, HighMask) &&
6319       DAG.MaskedValueIsZero(RHS, HighMask)) {
6320     // The inputs are both zero-extended.
6321     if (MakeMUL_LOHI(LL, RL, Lo, Hi, false)) {
6322       Result.push_back(Lo);
6323       Result.push_back(Hi);
6324       if (Opcode != ISD::MUL) {
6325         SDValue Zero = DAG.getConstant(0, dl, HiLoVT);
6326         Result.push_back(Zero);
6327         Result.push_back(Zero);
6328       }
6329       return true;
6330     }
6331   }
6332 
6333   if (!VT.isVector() && Opcode == ISD::MUL &&
6334       DAG.ComputeNumSignBits(LHS) > InnerBitSize &&
6335       DAG.ComputeNumSignBits(RHS) > InnerBitSize) {
6336     // The input values are both sign-extended.
6337     // TODO non-MUL case?
6338     if (MakeMUL_LOHI(LL, RL, Lo, Hi, true)) {
6339       Result.push_back(Lo);
6340       Result.push_back(Hi);
6341       return true;
6342     }
6343   }
6344 
6345   unsigned ShiftAmount = OuterBitSize - InnerBitSize;
6346   EVT ShiftAmountTy = getShiftAmountTy(VT, DAG.getDataLayout());
6347   if (APInt::getMaxValue(ShiftAmountTy.getSizeInBits()).ult(ShiftAmount)) {
6348     // FIXME getShiftAmountTy does not always return a sensible result when VT
6349     // is an illegal type, and so the type may be too small to fit the shift
6350     // amount. Override it with i32. The shift will have to be legalized.
6351     ShiftAmountTy = MVT::i32;
6352   }
6353   SDValue Shift = DAG.getConstant(ShiftAmount, dl, ShiftAmountTy);
6354 
6355   if (!LH.getNode() && !RH.getNode() &&
6356       isOperationLegalOrCustom(ISD::SRL, VT) &&
6357       isOperationLegalOrCustom(ISD::TRUNCATE, HiLoVT)) {
6358     LH = DAG.getNode(ISD::SRL, dl, VT, LHS, Shift);
6359     LH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, LH);
6360     RH = DAG.getNode(ISD::SRL, dl, VT, RHS, Shift);
6361     RH = DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, RH);
6362   }
6363 
6364   if (!LH.getNode())
6365     return false;
6366 
6367   if (!MakeMUL_LOHI(LL, RL, Lo, Hi, false))
6368     return false;
6369 
6370   Result.push_back(Lo);
6371 
6372   if (Opcode == ISD::MUL) {
6373     RH = DAG.getNode(ISD::MUL, dl, HiLoVT, LL, RH);
6374     LH = DAG.getNode(ISD::MUL, dl, HiLoVT, LH, RL);
6375     Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, RH);
6376     Hi = DAG.getNode(ISD::ADD, dl, HiLoVT, Hi, LH);
6377     Result.push_back(Hi);
6378     return true;
6379   }
6380 
6381   // Compute the full width result.
6382   auto Merge = [&](SDValue Lo, SDValue Hi) -> SDValue {
6383     Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
6384     Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi);
6385     Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
6386     return DAG.getNode(ISD::OR, dl, VT, Lo, Hi);
6387   };
6388 
6389   SDValue Next = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Hi);
6390   if (!MakeMUL_LOHI(LL, RH, Lo, Hi, false))
6391     return false;
6392 
6393   // This is effectively the add part of a multiply-add of half-sized operands,
6394   // so it cannot overflow.
6395   Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi));
6396 
6397   if (!MakeMUL_LOHI(LH, RL, Lo, Hi, false))
6398     return false;
6399 
6400   SDValue Zero = DAG.getConstant(0, dl, HiLoVT);
6401   EVT BoolType = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
6402 
6403   bool UseGlue = (isOperationLegalOrCustom(ISD::ADDC, VT) &&
6404                   isOperationLegalOrCustom(ISD::ADDE, VT));
6405   if (UseGlue)
6406     Next = DAG.getNode(ISD::ADDC, dl, DAG.getVTList(VT, MVT::Glue), Next,
6407                        Merge(Lo, Hi));
6408   else
6409     Next = DAG.getNode(ISD::ADDCARRY, dl, DAG.getVTList(VT, BoolType), Next,
6410                        Merge(Lo, Hi), DAG.getConstant(0, dl, BoolType));
6411 
6412   SDValue Carry = Next.getValue(1);
6413   Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
6414   Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift);
6415 
6416   if (!MakeMUL_LOHI(LH, RH, Lo, Hi, Opcode == ISD::SMUL_LOHI))
6417     return false;
6418 
6419   if (UseGlue)
6420     Hi = DAG.getNode(ISD::ADDE, dl, DAG.getVTList(HiLoVT, MVT::Glue), Hi, Zero,
6421                      Carry);
6422   else
6423     Hi = DAG.getNode(ISD::ADDCARRY, dl, DAG.getVTList(HiLoVT, BoolType), Hi,
6424                      Zero, Carry);
6425 
6426   Next = DAG.getNode(ISD::ADD, dl, VT, Next, Merge(Lo, Hi));
6427 
6428   if (Opcode == ISD::SMUL_LOHI) {
6429     SDValue NextSub = DAG.getNode(ISD::SUB, dl, VT, Next,
6430                                   DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RL));
6431     Next = DAG.getSelectCC(dl, LH, Zero, NextSub, Next, ISD::SETLT);
6432 
6433     NextSub = DAG.getNode(ISD::SUB, dl, VT, Next,
6434                           DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LL));
6435     Next = DAG.getSelectCC(dl, RH, Zero, NextSub, Next, ISD::SETLT);
6436   }
6437 
6438   Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
6439   Next = DAG.getNode(ISD::SRL, dl, VT, Next, Shift);
6440   Result.push_back(DAG.getNode(ISD::TRUNCATE, dl, HiLoVT, Next));
6441   return true;
6442 }
6443 
6444 bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT,
6445                                SelectionDAG &DAG, MulExpansionKind Kind,
6446                                SDValue LL, SDValue LH, SDValue RL,
6447                                SDValue RH) const {
6448   SmallVector<SDValue, 2> Result;
6449   bool Ok = expandMUL_LOHI(N->getOpcode(), N->getValueType(0), SDLoc(N),
6450                            N->getOperand(0), N->getOperand(1), Result, HiLoVT,
6451                            DAG, Kind, LL, LH, RL, RH);
6452   if (Ok) {
6453     assert(Result.size() == 2);
6454     Lo = Result[0];
6455     Hi = Result[1];
6456   }
6457   return Ok;
6458 }
6459 
6460 // Check that (every element of) Z is undef or not an exact multiple of BW.
6461 static bool isNonZeroModBitWidthOrUndef(SDValue Z, unsigned BW) {
6462   return ISD::matchUnaryPredicate(
6463       Z,
6464       [=](ConstantSDNode *C) { return !C || C->getAPIntValue().urem(BW) != 0; },
6465       true);
6466 }
6467 
6468 bool TargetLowering::expandFunnelShift(SDNode *Node, SDValue &Result,
6469                                        SelectionDAG &DAG) const {
6470   EVT VT = Node->getValueType(0);
6471 
6472   if (VT.isVector() && (!isOperationLegalOrCustom(ISD::SHL, VT) ||
6473                         !isOperationLegalOrCustom(ISD::SRL, VT) ||
6474                         !isOperationLegalOrCustom(ISD::SUB, VT) ||
6475                         !isOperationLegalOrCustomOrPromote(ISD::OR, VT)))
6476     return false;
6477 
6478   SDValue X = Node->getOperand(0);
6479   SDValue Y = Node->getOperand(1);
6480   SDValue Z = Node->getOperand(2);
6481 
6482   unsigned BW = VT.getScalarSizeInBits();
6483   bool IsFSHL = Node->getOpcode() == ISD::FSHL;
6484   SDLoc DL(SDValue(Node, 0));
6485 
6486   EVT ShVT = Z.getValueType();
6487 
6488   // If a funnel shift in the other direction is more supported, use it.
6489   unsigned RevOpcode = IsFSHL ? ISD::FSHR : ISD::FSHL;
6490   if (!isOperationLegalOrCustom(Node->getOpcode(), VT) &&
6491       isOperationLegalOrCustom(RevOpcode, VT) && isPowerOf2_32(BW)) {
6492     if (isNonZeroModBitWidthOrUndef(Z, BW)) {
6493       // fshl X, Y, Z -> fshr X, Y, -Z
6494       // fshr X, Y, Z -> fshl X, Y, -Z
6495       SDValue Zero = DAG.getConstant(0, DL, ShVT);
6496       Z = DAG.getNode(ISD::SUB, DL, VT, Zero, Z);
6497     } else {
6498       // fshl X, Y, Z -> fshr (srl X, 1), (fshr X, Y, 1), ~Z
6499       // fshr X, Y, Z -> fshl (fshl X, Y, 1), (shl Y, 1), ~Z
6500       SDValue One = DAG.getConstant(1, DL, ShVT);
6501       if (IsFSHL) {
6502         Y = DAG.getNode(RevOpcode, DL, VT, X, Y, One);
6503         X = DAG.getNode(ISD::SRL, DL, VT, X, One);
6504       } else {
6505         X = DAG.getNode(RevOpcode, DL, VT, X, Y, One);
6506         Y = DAG.getNode(ISD::SHL, DL, VT, Y, One);
6507       }
6508       Z = DAG.getNOT(DL, Z, ShVT);
6509     }
6510     Result = DAG.getNode(RevOpcode, DL, VT, X, Y, Z);
6511     return true;
6512   }
6513 
6514   SDValue ShX, ShY;
6515   SDValue ShAmt, InvShAmt;
6516   if (isNonZeroModBitWidthOrUndef(Z, BW)) {
6517     // fshl: X << C | Y >> (BW - C)
6518     // fshr: X << (BW - C) | Y >> C
6519     // where C = Z % BW is not zero
6520     SDValue BitWidthC = DAG.getConstant(BW, DL, ShVT);
6521     ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Z, BitWidthC);
6522     InvShAmt = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthC, ShAmt);
6523     ShX = DAG.getNode(ISD::SHL, DL, VT, X, IsFSHL ? ShAmt : InvShAmt);
6524     ShY = DAG.getNode(ISD::SRL, DL, VT, Y, IsFSHL ? InvShAmt : ShAmt);
6525   } else {
6526     // fshl: X << (Z % BW) | Y >> 1 >> (BW - 1 - (Z % BW))
6527     // fshr: X << 1 << (BW - 1 - (Z % BW)) | Y >> (Z % BW)
6528     SDValue Mask = DAG.getConstant(BW - 1, DL, ShVT);
6529     if (isPowerOf2_32(BW)) {
6530       // Z % BW -> Z & (BW - 1)
6531       ShAmt = DAG.getNode(ISD::AND, DL, ShVT, Z, Mask);
6532       // (BW - 1) - (Z % BW) -> ~Z & (BW - 1)
6533       InvShAmt = DAG.getNode(ISD::AND, DL, ShVT, DAG.getNOT(DL, Z, ShVT), Mask);
6534     } else {
6535       SDValue BitWidthC = DAG.getConstant(BW, DL, ShVT);
6536       ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Z, BitWidthC);
6537       InvShAmt = DAG.getNode(ISD::SUB, DL, ShVT, Mask, ShAmt);
6538     }
6539 
6540     SDValue One = DAG.getConstant(1, DL, ShVT);
6541     if (IsFSHL) {
6542       ShX = DAG.getNode(ISD::SHL, DL, VT, X, ShAmt);
6543       SDValue ShY1 = DAG.getNode(ISD::SRL, DL, VT, Y, One);
6544       ShY = DAG.getNode(ISD::SRL, DL, VT, ShY1, InvShAmt);
6545     } else {
6546       SDValue ShX1 = DAG.getNode(ISD::SHL, DL, VT, X, One);
6547       ShX = DAG.getNode(ISD::SHL, DL, VT, ShX1, InvShAmt);
6548       ShY = DAG.getNode(ISD::SRL, DL, VT, Y, ShAmt);
6549     }
6550   }
6551   Result = DAG.getNode(ISD::OR, DL, VT, ShX, ShY);
6552   return true;
6553 }
6554 
6555 // TODO: Merge with expandFunnelShift.
6556 bool TargetLowering::expandROT(SDNode *Node, bool AllowVectorOps,
6557                                SDValue &Result, SelectionDAG &DAG) const {
6558   EVT VT = Node->getValueType(0);
6559   unsigned EltSizeInBits = VT.getScalarSizeInBits();
6560   bool IsLeft = Node->getOpcode() == ISD::ROTL;
6561   SDValue Op0 = Node->getOperand(0);
6562   SDValue Op1 = Node->getOperand(1);
6563   SDLoc DL(SDValue(Node, 0));
6564 
6565   EVT ShVT = Op1.getValueType();
6566   SDValue Zero = DAG.getConstant(0, DL, ShVT);
6567 
6568   // If a rotate in the other direction is supported, use it.
6569   unsigned RevRot = IsLeft ? ISD::ROTR : ISD::ROTL;
6570   if (isOperationLegalOrCustom(RevRot, VT) && isPowerOf2_32(EltSizeInBits)) {
6571     SDValue Sub = DAG.getNode(ISD::SUB, DL, ShVT, Zero, Op1);
6572     Result = DAG.getNode(RevRot, DL, VT, Op0, Sub);
6573     return true;
6574   }
6575 
6576   if (!AllowVectorOps && VT.isVector() &&
6577       (!isOperationLegalOrCustom(ISD::SHL, VT) ||
6578        !isOperationLegalOrCustom(ISD::SRL, VT) ||
6579        !isOperationLegalOrCustom(ISD::SUB, VT) ||
6580        !isOperationLegalOrCustomOrPromote(ISD::OR, VT) ||
6581        !isOperationLegalOrCustomOrPromote(ISD::AND, VT)))
6582     return false;
6583 
6584   unsigned ShOpc = IsLeft ? ISD::SHL : ISD::SRL;
6585   unsigned HsOpc = IsLeft ? ISD::SRL : ISD::SHL;
6586   SDValue BitWidthMinusOneC = DAG.getConstant(EltSizeInBits - 1, DL, ShVT);
6587   SDValue ShVal;
6588   SDValue HsVal;
6589   if (isPowerOf2_32(EltSizeInBits)) {
6590     // (rotl x, c) -> x << (c & (w - 1)) | x >> (-c & (w - 1))
6591     // (rotr x, c) -> x >> (c & (w - 1)) | x << (-c & (w - 1))
6592     SDValue NegOp1 = DAG.getNode(ISD::SUB, DL, ShVT, Zero, Op1);
6593     SDValue ShAmt = DAG.getNode(ISD::AND, DL, ShVT, Op1, BitWidthMinusOneC);
6594     ShVal = DAG.getNode(ShOpc, DL, VT, Op0, ShAmt);
6595     SDValue HsAmt = DAG.getNode(ISD::AND, DL, ShVT, NegOp1, BitWidthMinusOneC);
6596     HsVal = DAG.getNode(HsOpc, DL, VT, Op0, HsAmt);
6597   } else {
6598     // (rotl x, c) -> x << (c % w) | x >> 1 >> (w - 1 - (c % w))
6599     // (rotr x, c) -> x >> (c % w) | x << 1 << (w - 1 - (c % w))
6600     SDValue BitWidthC = DAG.getConstant(EltSizeInBits, DL, ShVT);
6601     SDValue ShAmt = DAG.getNode(ISD::UREM, DL, ShVT, Op1, BitWidthC);
6602     ShVal = DAG.getNode(ShOpc, DL, VT, Op0, ShAmt);
6603     SDValue HsAmt = DAG.getNode(ISD::SUB, DL, ShVT, BitWidthMinusOneC, ShAmt);
6604     SDValue One = DAG.getConstant(1, DL, ShVT);
6605     HsVal =
6606         DAG.getNode(HsOpc, DL, VT, DAG.getNode(HsOpc, DL, VT, Op0, One), HsAmt);
6607   }
6608   Result = DAG.getNode(ISD::OR, DL, VT, ShVal, HsVal);
6609   return true;
6610 }
6611 
6612 void TargetLowering::expandShiftParts(SDNode *Node, SDValue &Lo, SDValue &Hi,
6613                                       SelectionDAG &DAG) const {
6614   assert(Node->getNumOperands() == 3 && "Not a double-shift!");
6615   EVT VT = Node->getValueType(0);
6616   unsigned VTBits = VT.getScalarSizeInBits();
6617   assert(isPowerOf2_32(VTBits) && "Power-of-two integer type expected");
6618 
6619   bool IsSHL = Node->getOpcode() == ISD::SHL_PARTS;
6620   bool IsSRA = Node->getOpcode() == ISD::SRA_PARTS;
6621   SDValue ShOpLo = Node->getOperand(0);
6622   SDValue ShOpHi = Node->getOperand(1);
6623   SDValue ShAmt = Node->getOperand(2);
6624   EVT ShAmtVT = ShAmt.getValueType();
6625   EVT ShAmtCCVT =
6626       getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ShAmtVT);
6627   SDLoc dl(Node);
6628 
6629   // ISD::FSHL and ISD::FSHR have defined overflow behavior but ISD::SHL and
6630   // ISD::SRA/L nodes haven't. Insert an AND to be safe, it's usually optimized
6631   // away during isel.
6632   SDValue SafeShAmt = DAG.getNode(ISD::AND, dl, ShAmtVT, ShAmt,
6633                                   DAG.getConstant(VTBits - 1, dl, ShAmtVT));
6634   SDValue Tmp1 = IsSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
6635                                      DAG.getConstant(VTBits - 1, dl, ShAmtVT))
6636                        : DAG.getConstant(0, dl, VT);
6637 
6638   SDValue Tmp2, Tmp3;
6639   if (IsSHL) {
6640     Tmp2 = DAG.getNode(ISD::FSHL, dl, VT, ShOpHi, ShOpLo, ShAmt);
6641     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, SafeShAmt);
6642   } else {
6643     Tmp2 = DAG.getNode(ISD::FSHR, dl, VT, ShOpHi, ShOpLo, ShAmt);
6644     Tmp3 = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, SafeShAmt);
6645   }
6646 
6647   // If the shift amount is larger or equal than the width of a part we don't
6648   // use the result from the FSHL/FSHR. Insert a test and select the appropriate
6649   // values for large shift amounts.
6650   SDValue AndNode = DAG.getNode(ISD::AND, dl, ShAmtVT, ShAmt,
6651                                 DAG.getConstant(VTBits, dl, ShAmtVT));
6652   SDValue Cond = DAG.getSetCC(dl, ShAmtCCVT, AndNode,
6653                               DAG.getConstant(0, dl, ShAmtVT), ISD::SETNE);
6654 
6655   if (IsSHL) {
6656     Hi = DAG.getNode(ISD::SELECT, dl, VT, Cond, Tmp3, Tmp2);
6657     Lo = DAG.getNode(ISD::SELECT, dl, VT, Cond, Tmp1, Tmp3);
6658   } else {
6659     Lo = DAG.getNode(ISD::SELECT, dl, VT, Cond, Tmp3, Tmp2);
6660     Hi = DAG.getNode(ISD::SELECT, dl, VT, Cond, Tmp1, Tmp3);
6661   }
6662 }
6663 
6664 bool TargetLowering::expandFP_TO_SINT(SDNode *Node, SDValue &Result,
6665                                       SelectionDAG &DAG) const {
6666   unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
6667   SDValue Src = Node->getOperand(OpNo);
6668   EVT SrcVT = Src.getValueType();
6669   EVT DstVT = Node->getValueType(0);
6670   SDLoc dl(SDValue(Node, 0));
6671 
6672   // FIXME: Only f32 to i64 conversions are supported.
6673   if (SrcVT != MVT::f32 || DstVT != MVT::i64)
6674     return false;
6675 
6676   if (Node->isStrictFPOpcode())
6677     // When a NaN is converted to an integer a trap is allowed. We can't
6678     // use this expansion here because it would eliminate that trap. Other
6679     // traps are also allowed and cannot be eliminated. See
6680     // IEEE 754-2008 sec 5.8.
6681     return false;
6682 
6683   // Expand f32 -> i64 conversion
6684   // This algorithm comes from compiler-rt's implementation of fixsfdi:
6685   // https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/fixsfdi.c
6686   unsigned SrcEltBits = SrcVT.getScalarSizeInBits();
6687   EVT IntVT = SrcVT.changeTypeToInteger();
6688   EVT IntShVT = getShiftAmountTy(IntVT, DAG.getDataLayout());
6689 
6690   SDValue ExponentMask = DAG.getConstant(0x7F800000, dl, IntVT);
6691   SDValue ExponentLoBit = DAG.getConstant(23, dl, IntVT);
6692   SDValue Bias = DAG.getConstant(127, dl, IntVT);
6693   SDValue SignMask = DAG.getConstant(APInt::getSignMask(SrcEltBits), dl, IntVT);
6694   SDValue SignLowBit = DAG.getConstant(SrcEltBits - 1, dl, IntVT);
6695   SDValue MantissaMask = DAG.getConstant(0x007FFFFF, dl, IntVT);
6696 
6697   SDValue Bits = DAG.getNode(ISD::BITCAST, dl, IntVT, Src);
6698 
6699   SDValue ExponentBits = DAG.getNode(
6700       ISD::SRL, dl, IntVT, DAG.getNode(ISD::AND, dl, IntVT, Bits, ExponentMask),
6701       DAG.getZExtOrTrunc(ExponentLoBit, dl, IntShVT));
6702   SDValue Exponent = DAG.getNode(ISD::SUB, dl, IntVT, ExponentBits, Bias);
6703 
6704   SDValue Sign = DAG.getNode(ISD::SRA, dl, IntVT,
6705                              DAG.getNode(ISD::AND, dl, IntVT, Bits, SignMask),
6706                              DAG.getZExtOrTrunc(SignLowBit, dl, IntShVT));
6707   Sign = DAG.getSExtOrTrunc(Sign, dl, DstVT);
6708 
6709   SDValue R = DAG.getNode(ISD::OR, dl, IntVT,
6710                           DAG.getNode(ISD::AND, dl, IntVT, Bits, MantissaMask),
6711                           DAG.getConstant(0x00800000, dl, IntVT));
6712 
6713   R = DAG.getZExtOrTrunc(R, dl, DstVT);
6714 
6715   R = DAG.getSelectCC(
6716       dl, Exponent, ExponentLoBit,
6717       DAG.getNode(ISD::SHL, dl, DstVT, R,
6718                   DAG.getZExtOrTrunc(
6719                       DAG.getNode(ISD::SUB, dl, IntVT, Exponent, ExponentLoBit),
6720                       dl, IntShVT)),
6721       DAG.getNode(ISD::SRL, dl, DstVT, R,
6722                   DAG.getZExtOrTrunc(
6723                       DAG.getNode(ISD::SUB, dl, IntVT, ExponentLoBit, Exponent),
6724                       dl, IntShVT)),
6725       ISD::SETGT);
6726 
6727   SDValue Ret = DAG.getNode(ISD::SUB, dl, DstVT,
6728                             DAG.getNode(ISD::XOR, dl, DstVT, R, Sign), Sign);
6729 
6730   Result = DAG.getSelectCC(dl, Exponent, DAG.getConstant(0, dl, IntVT),
6731                            DAG.getConstant(0, dl, DstVT), Ret, ISD::SETLT);
6732   return true;
6733 }
6734 
6735 bool TargetLowering::expandFP_TO_UINT(SDNode *Node, SDValue &Result,
6736                                       SDValue &Chain,
6737                                       SelectionDAG &DAG) const {
6738   SDLoc dl(SDValue(Node, 0));
6739   unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
6740   SDValue Src = Node->getOperand(OpNo);
6741 
6742   EVT SrcVT = Src.getValueType();
6743   EVT DstVT = Node->getValueType(0);
6744   EVT SetCCVT =
6745       getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), SrcVT);
6746   EVT DstSetCCVT =
6747       getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), DstVT);
6748 
6749   // Only expand vector types if we have the appropriate vector bit operations.
6750   unsigned SIntOpcode = Node->isStrictFPOpcode() ? ISD::STRICT_FP_TO_SINT :
6751                                                    ISD::FP_TO_SINT;
6752   if (DstVT.isVector() && (!isOperationLegalOrCustom(SIntOpcode, DstVT) ||
6753                            !isOperationLegalOrCustomOrPromote(ISD::XOR, SrcVT)))
6754     return false;
6755 
6756   // If the maximum float value is smaller then the signed integer range,
6757   // the destination signmask can't be represented by the float, so we can
6758   // just use FP_TO_SINT directly.
6759   const fltSemantics &APFSem = DAG.EVTToAPFloatSemantics(SrcVT);
6760   APFloat APF(APFSem, APInt::getNullValue(SrcVT.getScalarSizeInBits()));
6761   APInt SignMask = APInt::getSignMask(DstVT.getScalarSizeInBits());
6762   if (APFloat::opOverflow &
6763       APF.convertFromAPInt(SignMask, false, APFloat::rmNearestTiesToEven)) {
6764     if (Node->isStrictFPOpcode()) {
6765       Result = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, { DstVT, MVT::Other },
6766                            { Node->getOperand(0), Src });
6767       Chain = Result.getValue(1);
6768     } else
6769       Result = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src);
6770     return true;
6771   }
6772 
6773   // Don't expand it if there isn't cheap fsub instruction.
6774   if (!isOperationLegalOrCustom(
6775           Node->isStrictFPOpcode() ? ISD::STRICT_FSUB : ISD::FSUB, SrcVT))
6776     return false;
6777 
6778   SDValue Cst = DAG.getConstantFP(APF, dl, SrcVT);
6779   SDValue Sel;
6780 
6781   if (Node->isStrictFPOpcode()) {
6782     Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT,
6783                        Node->getOperand(0), /*IsSignaling*/ true);
6784     Chain = Sel.getValue(1);
6785   } else {
6786     Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT);
6787   }
6788 
6789   bool Strict = Node->isStrictFPOpcode() ||
6790                 shouldUseStrictFP_TO_INT(SrcVT, DstVT, /*IsSigned*/ false);
6791 
6792   if (Strict) {
6793     // Expand based on maximum range of FP_TO_SINT, if the value exceeds the
6794     // signmask then offset (the result of which should be fully representable).
6795     // Sel = Src < 0x8000000000000000
6796     // FltOfs = select Sel, 0, 0x8000000000000000
6797     // IntOfs = select Sel, 0, 0x8000000000000000
6798     // Result = fp_to_sint(Src - FltOfs) ^ IntOfs
6799 
6800     // TODO: Should any fast-math-flags be set for the FSUB?
6801     SDValue FltOfs = DAG.getSelect(dl, SrcVT, Sel,
6802                                    DAG.getConstantFP(0.0, dl, SrcVT), Cst);
6803     Sel = DAG.getBoolExtOrTrunc(Sel, dl, DstSetCCVT, DstVT);
6804     SDValue IntOfs = DAG.getSelect(dl, DstVT, Sel,
6805                                    DAG.getConstant(0, dl, DstVT),
6806                                    DAG.getConstant(SignMask, dl, DstVT));
6807     SDValue SInt;
6808     if (Node->isStrictFPOpcode()) {
6809       SDValue Val = DAG.getNode(ISD::STRICT_FSUB, dl, { SrcVT, MVT::Other },
6810                                 { Chain, Src, FltOfs });
6811       SInt = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, { DstVT, MVT::Other },
6812                          { Val.getValue(1), Val });
6813       Chain = SInt.getValue(1);
6814     } else {
6815       SDValue Val = DAG.getNode(ISD::FSUB, dl, SrcVT, Src, FltOfs);
6816       SInt = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Val);
6817     }
6818     Result = DAG.getNode(ISD::XOR, dl, DstVT, SInt, IntOfs);
6819   } else {
6820     // Expand based on maximum range of FP_TO_SINT:
6821     // True = fp_to_sint(Src)
6822     // False = 0x8000000000000000 + fp_to_sint(Src - 0x8000000000000000)
6823     // Result = select (Src < 0x8000000000000000), True, False
6824 
6825     SDValue True = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src);
6826     // TODO: Should any fast-math-flags be set for the FSUB?
6827     SDValue False = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT,
6828                                 DAG.getNode(ISD::FSUB, dl, SrcVT, Src, Cst));
6829     False = DAG.getNode(ISD::XOR, dl, DstVT, False,
6830                         DAG.getConstant(SignMask, dl, DstVT));
6831     Sel = DAG.getBoolExtOrTrunc(Sel, dl, DstSetCCVT, DstVT);
6832     Result = DAG.getSelect(dl, DstVT, Sel, True, False);
6833   }
6834   return true;
6835 }
6836 
6837 bool TargetLowering::expandUINT_TO_FP(SDNode *Node, SDValue &Result,
6838                                       SDValue &Chain,
6839                                       SelectionDAG &DAG) const {
6840   // This transform is not correct for converting 0 when rounding mode is set
6841   // to round toward negative infinity which will produce -0.0. So disable under
6842   // strictfp.
6843   if (Node->isStrictFPOpcode())
6844     return false;
6845 
6846   SDValue Src = Node->getOperand(0);
6847   EVT SrcVT = Src.getValueType();
6848   EVT DstVT = Node->getValueType(0);
6849 
6850   if (SrcVT.getScalarType() != MVT::i64 || DstVT.getScalarType() != MVT::f64)
6851     return false;
6852 
6853   // Only expand vector types if we have the appropriate vector bit operations.
6854   if (SrcVT.isVector() && (!isOperationLegalOrCustom(ISD::SRL, SrcVT) ||
6855                            !isOperationLegalOrCustom(ISD::FADD, DstVT) ||
6856                            !isOperationLegalOrCustom(ISD::FSUB, DstVT) ||
6857                            !isOperationLegalOrCustomOrPromote(ISD::OR, SrcVT) ||
6858                            !isOperationLegalOrCustomOrPromote(ISD::AND, SrcVT)))
6859     return false;
6860 
6861   SDLoc dl(SDValue(Node, 0));
6862   EVT ShiftVT = getShiftAmountTy(SrcVT, DAG.getDataLayout());
6863 
6864   // Implementation of unsigned i64 to f64 following the algorithm in
6865   // __floatundidf in compiler_rt.  This implementation performs rounding
6866   // correctly in all rounding modes with the exception of converting 0
6867   // when rounding toward negative infinity. In that case the fsub will produce
6868   // -0.0. This will be added to +0.0 and produce -0.0 which is incorrect.
6869   SDValue TwoP52 = DAG.getConstant(UINT64_C(0x4330000000000000), dl, SrcVT);
6870   SDValue TwoP84PlusTwoP52 = DAG.getConstantFP(
6871       BitsToDouble(UINT64_C(0x4530000000100000)), dl, DstVT);
6872   SDValue TwoP84 = DAG.getConstant(UINT64_C(0x4530000000000000), dl, SrcVT);
6873   SDValue LoMask = DAG.getConstant(UINT64_C(0x00000000FFFFFFFF), dl, SrcVT);
6874   SDValue HiShift = DAG.getConstant(32, dl, ShiftVT);
6875 
6876   SDValue Lo = DAG.getNode(ISD::AND, dl, SrcVT, Src, LoMask);
6877   SDValue Hi = DAG.getNode(ISD::SRL, dl, SrcVT, Src, HiShift);
6878   SDValue LoOr = DAG.getNode(ISD::OR, dl, SrcVT, Lo, TwoP52);
6879   SDValue HiOr = DAG.getNode(ISD::OR, dl, SrcVT, Hi, TwoP84);
6880   SDValue LoFlt = DAG.getBitcast(DstVT, LoOr);
6881   SDValue HiFlt = DAG.getBitcast(DstVT, HiOr);
6882   SDValue HiSub =
6883       DAG.getNode(ISD::FSUB, dl, DstVT, HiFlt, TwoP84PlusTwoP52);
6884   Result = DAG.getNode(ISD::FADD, dl, DstVT, LoFlt, HiSub);
6885   return true;
6886 }
6887 
6888 SDValue TargetLowering::expandFMINNUM_FMAXNUM(SDNode *Node,
6889                                               SelectionDAG &DAG) const {
6890   SDLoc dl(Node);
6891   unsigned NewOp = Node->getOpcode() == ISD::FMINNUM ?
6892     ISD::FMINNUM_IEEE : ISD::FMAXNUM_IEEE;
6893   EVT VT = Node->getValueType(0);
6894 
6895   if (VT.isScalableVector())
6896     report_fatal_error(
6897         "Expanding fminnum/fmaxnum for scalable vectors is undefined.");
6898 
6899   if (isOperationLegalOrCustom(NewOp, VT)) {
6900     SDValue Quiet0 = Node->getOperand(0);
6901     SDValue Quiet1 = Node->getOperand(1);
6902 
6903     if (!Node->getFlags().hasNoNaNs()) {
6904       // Insert canonicalizes if it's possible we need to quiet to get correct
6905       // sNaN behavior.
6906       if (!DAG.isKnownNeverSNaN(Quiet0)) {
6907         Quiet0 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet0,
6908                              Node->getFlags());
6909       }
6910       if (!DAG.isKnownNeverSNaN(Quiet1)) {
6911         Quiet1 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet1,
6912                              Node->getFlags());
6913       }
6914     }
6915 
6916     return DAG.getNode(NewOp, dl, VT, Quiet0, Quiet1, Node->getFlags());
6917   }
6918 
6919   // If the target has FMINIMUM/FMAXIMUM but not FMINNUM/FMAXNUM use that
6920   // instead if there are no NaNs.
6921   if (Node->getFlags().hasNoNaNs()) {
6922     unsigned IEEE2018Op =
6923         Node->getOpcode() == ISD::FMINNUM ? ISD::FMINIMUM : ISD::FMAXIMUM;
6924     if (isOperationLegalOrCustom(IEEE2018Op, VT)) {
6925       return DAG.getNode(IEEE2018Op, dl, VT, Node->getOperand(0),
6926                          Node->getOperand(1), Node->getFlags());
6927     }
6928   }
6929 
6930   // If none of the above worked, but there are no NaNs, then expand to
6931   // a compare/select sequence.  This is required for correctness since
6932   // InstCombine might have canonicalized a fcmp+select sequence to a
6933   // FMINNUM/FMAXNUM node.  If we were to fall through to the default
6934   // expansion to libcall, we might introduce a link-time dependency
6935   // on libm into a file that originally did not have one.
6936   if (Node->getFlags().hasNoNaNs()) {
6937     ISD::CondCode Pred =
6938         Node->getOpcode() == ISD::FMINNUM ? ISD::SETLT : ISD::SETGT;
6939     SDValue Op1 = Node->getOperand(0);
6940     SDValue Op2 = Node->getOperand(1);
6941     SDValue SelCC = DAG.getSelectCC(dl, Op1, Op2, Op1, Op2, Pred);
6942     // Copy FMF flags, but always set the no-signed-zeros flag
6943     // as this is implied by the FMINNUM/FMAXNUM semantics.
6944     SDNodeFlags Flags = Node->getFlags();
6945     Flags.setNoSignedZeros(true);
6946     SelCC->setFlags(Flags);
6947     return SelCC;
6948   }
6949 
6950   return SDValue();
6951 }
6952 
6953 bool TargetLowering::expandCTPOP(SDNode *Node, SDValue &Result,
6954                                  SelectionDAG &DAG) const {
6955   SDLoc dl(Node);
6956   EVT VT = Node->getValueType(0);
6957   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
6958   SDValue Op = Node->getOperand(0);
6959   unsigned Len = VT.getScalarSizeInBits();
6960   assert(VT.isInteger() && "CTPOP not implemented for this type.");
6961 
6962   // TODO: Add support for irregular type lengths.
6963   if (!(Len <= 128 && Len % 8 == 0))
6964     return false;
6965 
6966   // Only expand vector types if we have the appropriate vector bit operations.
6967   if (VT.isVector() && (!isOperationLegalOrCustom(ISD::ADD, VT) ||
6968                         !isOperationLegalOrCustom(ISD::SUB, VT) ||
6969                         !isOperationLegalOrCustom(ISD::SRL, VT) ||
6970                         (Len != 8 && !isOperationLegalOrCustom(ISD::MUL, VT)) ||
6971                         !isOperationLegalOrCustomOrPromote(ISD::AND, VT)))
6972     return false;
6973 
6974   // This is the "best" algorithm from
6975   // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
6976   SDValue Mask55 =
6977       DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), dl, VT);
6978   SDValue Mask33 =
6979       DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), dl, VT);
6980   SDValue Mask0F =
6981       DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), dl, VT);
6982   SDValue Mask01 =
6983       DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), dl, VT);
6984 
6985   // v = v - ((v >> 1) & 0x55555555...)
6986   Op = DAG.getNode(ISD::SUB, dl, VT, Op,
6987                    DAG.getNode(ISD::AND, dl, VT,
6988                                DAG.getNode(ISD::SRL, dl, VT, Op,
6989                                            DAG.getConstant(1, dl, ShVT)),
6990                                Mask55));
6991   // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
6992   Op = DAG.getNode(ISD::ADD, dl, VT, DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
6993                    DAG.getNode(ISD::AND, dl, VT,
6994                                DAG.getNode(ISD::SRL, dl, VT, Op,
6995                                            DAG.getConstant(2, dl, ShVT)),
6996                                Mask33));
6997   // v = (v + (v >> 4)) & 0x0F0F0F0F...
6998   Op = DAG.getNode(ISD::AND, dl, VT,
6999                    DAG.getNode(ISD::ADD, dl, VT, Op,
7000                                DAG.getNode(ISD::SRL, dl, VT, Op,
7001                                            DAG.getConstant(4, dl, ShVT))),
7002                    Mask0F);
7003   // v = (v * 0x01010101...) >> (Len - 8)
7004   if (Len > 8)
7005     Op =
7006         DAG.getNode(ISD::SRL, dl, VT, DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
7007                     DAG.getConstant(Len - 8, dl, ShVT));
7008 
7009   Result = Op;
7010   return true;
7011 }
7012 
7013 bool TargetLowering::expandCTLZ(SDNode *Node, SDValue &Result,
7014                                 SelectionDAG &DAG) const {
7015   SDLoc dl(Node);
7016   EVT VT = Node->getValueType(0);
7017   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
7018   SDValue Op = Node->getOperand(0);
7019   unsigned NumBitsPerElt = VT.getScalarSizeInBits();
7020 
7021   // If the non-ZERO_UNDEF version is supported we can use that instead.
7022   if (Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF &&
7023       isOperationLegalOrCustom(ISD::CTLZ, VT)) {
7024     Result = DAG.getNode(ISD::CTLZ, dl, VT, Op);
7025     return true;
7026   }
7027 
7028   // If the ZERO_UNDEF version is supported use that and handle the zero case.
7029   if (isOperationLegalOrCustom(ISD::CTLZ_ZERO_UNDEF, VT)) {
7030     EVT SetCCVT =
7031         getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
7032     SDValue CTLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, VT, Op);
7033     SDValue Zero = DAG.getConstant(0, dl, VT);
7034     SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
7035     Result = DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
7036                          DAG.getConstant(NumBitsPerElt, dl, VT), CTLZ);
7037     return true;
7038   }
7039 
7040   // Only expand vector types if we have the appropriate vector bit operations.
7041   if (VT.isVector() && (!isPowerOf2_32(NumBitsPerElt) ||
7042                         !isOperationLegalOrCustom(ISD::CTPOP, VT) ||
7043                         !isOperationLegalOrCustom(ISD::SRL, VT) ||
7044                         !isOperationLegalOrCustomOrPromote(ISD::OR, VT)))
7045     return false;
7046 
7047   // for now, we do this:
7048   // x = x | (x >> 1);
7049   // x = x | (x >> 2);
7050   // ...
7051   // x = x | (x >>16);
7052   // x = x | (x >>32); // for 64-bit input
7053   // return popcount(~x);
7054   //
7055   // Ref: "Hacker's Delight" by Henry Warren
7056   for (unsigned i = 0; (1U << i) <= (NumBitsPerElt / 2); ++i) {
7057     SDValue Tmp = DAG.getConstant(1ULL << i, dl, ShVT);
7058     Op = DAG.getNode(ISD::OR, dl, VT, Op,
7059                      DAG.getNode(ISD::SRL, dl, VT, Op, Tmp));
7060   }
7061   Op = DAG.getNOT(dl, Op, VT);
7062   Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
7063   return true;
7064 }
7065 
7066 bool TargetLowering::expandCTTZ(SDNode *Node, SDValue &Result,
7067                                 SelectionDAG &DAG) const {
7068   SDLoc dl(Node);
7069   EVT VT = Node->getValueType(0);
7070   SDValue Op = Node->getOperand(0);
7071   unsigned NumBitsPerElt = VT.getScalarSizeInBits();
7072 
7073   // If the non-ZERO_UNDEF version is supported we can use that instead.
7074   if (Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF &&
7075       isOperationLegalOrCustom(ISD::CTTZ, VT)) {
7076     Result = DAG.getNode(ISD::CTTZ, dl, VT, Op);
7077     return true;
7078   }
7079 
7080   // If the ZERO_UNDEF version is supported use that and handle the zero case.
7081   if (isOperationLegalOrCustom(ISD::CTTZ_ZERO_UNDEF, VT)) {
7082     EVT SetCCVT =
7083         getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
7084     SDValue CTTZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, VT, Op);
7085     SDValue Zero = DAG.getConstant(0, dl, VT);
7086     SDValue SrcIsZero = DAG.getSetCC(dl, SetCCVT, Op, Zero, ISD::SETEQ);
7087     Result = DAG.getNode(ISD::SELECT, dl, VT, SrcIsZero,
7088                          DAG.getConstant(NumBitsPerElt, dl, VT), CTTZ);
7089     return true;
7090   }
7091 
7092   // Only expand vector types if we have the appropriate vector bit operations.
7093   if (VT.isVector() && (!isPowerOf2_32(NumBitsPerElt) ||
7094                         (!isOperationLegalOrCustom(ISD::CTPOP, VT) &&
7095                          !isOperationLegalOrCustom(ISD::CTLZ, VT)) ||
7096                         !isOperationLegalOrCustom(ISD::SUB, VT) ||
7097                         !isOperationLegalOrCustomOrPromote(ISD::AND, VT) ||
7098                         !isOperationLegalOrCustomOrPromote(ISD::XOR, VT)))
7099     return false;
7100 
7101   // for now, we use: { return popcount(~x & (x - 1)); }
7102   // unless the target has ctlz but not ctpop, in which case we use:
7103   // { return 32 - nlz(~x & (x-1)); }
7104   // Ref: "Hacker's Delight" by Henry Warren
7105   SDValue Tmp = DAG.getNode(
7106       ISD::AND, dl, VT, DAG.getNOT(dl, Op, VT),
7107       DAG.getNode(ISD::SUB, dl, VT, Op, DAG.getConstant(1, dl, VT)));
7108 
7109   // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
7110   if (isOperationLegal(ISD::CTLZ, VT) && !isOperationLegal(ISD::CTPOP, VT)) {
7111     Result =
7112         DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(NumBitsPerElt, dl, VT),
7113                     DAG.getNode(ISD::CTLZ, dl, VT, Tmp));
7114     return true;
7115   }
7116 
7117   Result = DAG.getNode(ISD::CTPOP, dl, VT, Tmp);
7118   return true;
7119 }
7120 
7121 bool TargetLowering::expandABS(SDNode *N, SDValue &Result,
7122                                SelectionDAG &DAG, bool IsNegative) const {
7123   SDLoc dl(N);
7124   EVT VT = N->getValueType(0);
7125   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
7126   SDValue Op = N->getOperand(0);
7127 
7128   // abs(x) -> smax(x,sub(0,x))
7129   if (!IsNegative && isOperationLegal(ISD::SUB, VT) &&
7130       isOperationLegal(ISD::SMAX, VT)) {
7131     SDValue Zero = DAG.getConstant(0, dl, VT);
7132     Result = DAG.getNode(ISD::SMAX, dl, VT, Op,
7133                          DAG.getNode(ISD::SUB, dl, VT, Zero, Op));
7134     return true;
7135   }
7136 
7137   // abs(x) -> umin(x,sub(0,x))
7138   if (!IsNegative && isOperationLegal(ISD::SUB, VT) &&
7139       isOperationLegal(ISD::UMIN, VT)) {
7140     SDValue Zero = DAG.getConstant(0, dl, VT);
7141     Result = DAG.getNode(ISD::UMIN, dl, VT, Op,
7142                          DAG.getNode(ISD::SUB, dl, VT, Zero, Op));
7143     return true;
7144   }
7145 
7146   // 0 - abs(x) -> smin(x, sub(0,x))
7147   if (IsNegative && isOperationLegal(ISD::SUB, VT) &&
7148       isOperationLegal(ISD::SMIN, VT)) {
7149     SDValue Zero = DAG.getConstant(0, dl, VT);
7150     Result = DAG.getNode(ISD::SMIN, dl, VT, Op,
7151                          DAG.getNode(ISD::SUB, dl, VT, Zero, Op));
7152     return true;
7153   }
7154 
7155   // Only expand vector types if we have the appropriate vector operations.
7156   if (VT.isVector() &&
7157       (!isOperationLegalOrCustom(ISD::SRA, VT) ||
7158        (!IsNegative && !isOperationLegalOrCustom(ISD::ADD, VT)) ||
7159        (IsNegative && !isOperationLegalOrCustom(ISD::SUB, VT)) ||
7160        !isOperationLegalOrCustomOrPromote(ISD::XOR, VT)))
7161     return false;
7162 
7163   SDValue Shift =
7164       DAG.getNode(ISD::SRA, dl, VT, Op,
7165                   DAG.getConstant(VT.getScalarSizeInBits() - 1, dl, ShVT));
7166   if (!IsNegative) {
7167     SDValue Add = DAG.getNode(ISD::ADD, dl, VT, Op, Shift);
7168     Result = DAG.getNode(ISD::XOR, dl, VT, Add, Shift);
7169   } else {
7170     // 0 - abs(x) -> Y = sra (X, size(X)-1); sub (Y, xor (X, Y))
7171     SDValue Xor = DAG.getNode(ISD::XOR, dl, VT, Op, Shift);
7172     Result = DAG.getNode(ISD::SUB, dl, VT, Shift, Xor);
7173   }
7174   return true;
7175 }
7176 
7177 SDValue TargetLowering::expandBSWAP(SDNode *N, SelectionDAG &DAG) const {
7178   SDLoc dl(N);
7179   EVT VT = N->getValueType(0);
7180   SDValue Op = N->getOperand(0);
7181 
7182   if (!VT.isSimple())
7183     return SDValue();
7184 
7185   EVT SHVT = getShiftAmountTy(VT, DAG.getDataLayout());
7186   SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
7187   switch (VT.getSimpleVT().getScalarType().SimpleTy) {
7188   default:
7189     return SDValue();
7190   case MVT::i16:
7191     // Use a rotate by 8. This can be further expanded if necessary.
7192     return DAG.getNode(ISD::ROTL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
7193   case MVT::i32:
7194     Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
7195     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
7196     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
7197     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
7198     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
7199                        DAG.getConstant(0xFF0000, dl, VT));
7200     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT));
7201     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
7202     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
7203     return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
7204   case MVT::i64:
7205     Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
7206     Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
7207     Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
7208     Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
7209     Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT));
7210     Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT));
7211     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT));
7212     Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT));
7213     Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7,
7214                        DAG.getConstant(255ULL<<48, dl, VT));
7215     Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6,
7216                        DAG.getConstant(255ULL<<40, dl, VT));
7217     Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5,
7218                        DAG.getConstant(255ULL<<32, dl, VT));
7219     Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4,
7220                        DAG.getConstant(255ULL<<24, dl, VT));
7221     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3,
7222                        DAG.getConstant(255ULL<<16, dl, VT));
7223     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2,
7224                        DAG.getConstant(255ULL<<8 , dl, VT));
7225     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
7226     Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
7227     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
7228     Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
7229     Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
7230     Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
7231     return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
7232   }
7233 }
7234 
7235 SDValue TargetLowering::expandBITREVERSE(SDNode *N, SelectionDAG &DAG) const {
7236   SDLoc dl(N);
7237   EVT VT = N->getValueType(0);
7238   SDValue Op = N->getOperand(0);
7239   EVT SHVT = getShiftAmountTy(VT, DAG.getDataLayout());
7240   unsigned Sz = VT.getScalarSizeInBits();
7241 
7242   SDValue Tmp, Tmp2, Tmp3;
7243 
7244   // If we can, perform BSWAP first and then the mask+swap the i4, then i2
7245   // and finally the i1 pairs.
7246   // TODO: We can easily support i4/i2 legal types if any target ever does.
7247   if (Sz >= 8 && isPowerOf2_32(Sz)) {
7248     // Create the masks - repeating the pattern every byte.
7249     APInt MaskHi4 = APInt::getSplat(Sz, APInt(8, 0xF0));
7250     APInt MaskHi2 = APInt::getSplat(Sz, APInt(8, 0xCC));
7251     APInt MaskHi1 = APInt::getSplat(Sz, APInt(8, 0xAA));
7252     APInt MaskLo4 = APInt::getSplat(Sz, APInt(8, 0x0F));
7253     APInt MaskLo2 = APInt::getSplat(Sz, APInt(8, 0x33));
7254     APInt MaskLo1 = APInt::getSplat(Sz, APInt(8, 0x55));
7255 
7256     // BSWAP if the type is wider than a single byte.
7257     Tmp = (Sz > 8 ? DAG.getNode(ISD::BSWAP, dl, VT, Op) : Op);
7258 
7259     // swap i4: ((V & 0xF0) >> 4) | ((V & 0x0F) << 4)
7260     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi4, dl, VT));
7261     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo4, dl, VT));
7262     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(4, dl, SHVT));
7263     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(4, dl, SHVT));
7264     Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
7265 
7266     // swap i2: ((V & 0xCC) >> 2) | ((V & 0x33) << 2)
7267     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi2, dl, VT));
7268     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo2, dl, VT));
7269     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(2, dl, SHVT));
7270     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(2, dl, SHVT));
7271     Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
7272 
7273     // swap i1: ((V & 0xAA) >> 1) | ((V & 0x55) << 1)
7274     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskHi1, dl, VT));
7275     Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp, DAG.getConstant(MaskLo1, dl, VT));
7276     Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Tmp2, DAG.getConstant(1, dl, SHVT));
7277     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Tmp3, DAG.getConstant(1, dl, SHVT));
7278     Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
7279     return Tmp;
7280   }
7281 
7282   Tmp = DAG.getConstant(0, dl, VT);
7283   for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) {
7284     if (I < J)
7285       Tmp2 =
7286           DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT));
7287     else
7288       Tmp2 =
7289           DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(I - J, dl, SHVT));
7290 
7291     APInt Shift(Sz, 1);
7292     Shift <<= J;
7293     Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(Shift, dl, VT));
7294     Tmp = DAG.getNode(ISD::OR, dl, VT, Tmp, Tmp2);
7295   }
7296 
7297   return Tmp;
7298 }
7299 
7300 std::pair<SDValue, SDValue>
7301 TargetLowering::scalarizeVectorLoad(LoadSDNode *LD,
7302                                     SelectionDAG &DAG) const {
7303   SDLoc SL(LD);
7304   SDValue Chain = LD->getChain();
7305   SDValue BasePTR = LD->getBasePtr();
7306   EVT SrcVT = LD->getMemoryVT();
7307   EVT DstVT = LD->getValueType(0);
7308   ISD::LoadExtType ExtType = LD->getExtensionType();
7309 
7310   if (SrcVT.isScalableVector())
7311     report_fatal_error("Cannot scalarize scalable vector loads");
7312 
7313   unsigned NumElem = SrcVT.getVectorNumElements();
7314 
7315   EVT SrcEltVT = SrcVT.getScalarType();
7316   EVT DstEltVT = DstVT.getScalarType();
7317 
7318   // A vector must always be stored in memory as-is, i.e. without any padding
7319   // between the elements, since various code depend on it, e.g. in the
7320   // handling of a bitcast of a vector type to int, which may be done with a
7321   // vector store followed by an integer load. A vector that does not have
7322   // elements that are byte-sized must therefore be stored as an integer
7323   // built out of the extracted vector elements.
7324   if (!SrcEltVT.isByteSized()) {
7325     unsigned NumLoadBits = SrcVT.getStoreSizeInBits();
7326     EVT LoadVT = EVT::getIntegerVT(*DAG.getContext(), NumLoadBits);
7327 
7328     unsigned NumSrcBits = SrcVT.getSizeInBits();
7329     EVT SrcIntVT = EVT::getIntegerVT(*DAG.getContext(), NumSrcBits);
7330 
7331     unsigned SrcEltBits = SrcEltVT.getSizeInBits();
7332     SDValue SrcEltBitMask = DAG.getConstant(
7333         APInt::getLowBitsSet(NumLoadBits, SrcEltBits), SL, LoadVT);
7334 
7335     // Load the whole vector and avoid masking off the top bits as it makes
7336     // the codegen worse.
7337     SDValue Load =
7338         DAG.getExtLoad(ISD::EXTLOAD, SL, LoadVT, Chain, BasePTR,
7339                        LD->getPointerInfo(), SrcIntVT, LD->getOriginalAlign(),
7340                        LD->getMemOperand()->getFlags(), LD->getAAInfo());
7341 
7342     SmallVector<SDValue, 8> Vals;
7343     for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
7344       unsigned ShiftIntoIdx =
7345           (DAG.getDataLayout().isBigEndian() ? (NumElem - 1) - Idx : Idx);
7346       SDValue ShiftAmount =
7347           DAG.getShiftAmountConstant(ShiftIntoIdx * SrcEltVT.getSizeInBits(),
7348                                      LoadVT, SL, /*LegalTypes=*/false);
7349       SDValue ShiftedElt = DAG.getNode(ISD::SRL, SL, LoadVT, Load, ShiftAmount);
7350       SDValue Elt =
7351           DAG.getNode(ISD::AND, SL, LoadVT, ShiftedElt, SrcEltBitMask);
7352       SDValue Scalar = DAG.getNode(ISD::TRUNCATE, SL, SrcEltVT, Elt);
7353 
7354       if (ExtType != ISD::NON_EXTLOAD) {
7355         unsigned ExtendOp = ISD::getExtForLoadExtType(false, ExtType);
7356         Scalar = DAG.getNode(ExtendOp, SL, DstEltVT, Scalar);
7357       }
7358 
7359       Vals.push_back(Scalar);
7360     }
7361 
7362     SDValue Value = DAG.getBuildVector(DstVT, SL, Vals);
7363     return std::make_pair(Value, Load.getValue(1));
7364   }
7365 
7366   unsigned Stride = SrcEltVT.getSizeInBits() / 8;
7367   assert(SrcEltVT.isByteSized());
7368 
7369   SmallVector<SDValue, 8> Vals;
7370   SmallVector<SDValue, 8> LoadChains;
7371 
7372   for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
7373     SDValue ScalarLoad =
7374         DAG.getExtLoad(ExtType, SL, DstEltVT, Chain, BasePTR,
7375                        LD->getPointerInfo().getWithOffset(Idx * Stride),
7376                        SrcEltVT, LD->getOriginalAlign(),
7377                        LD->getMemOperand()->getFlags(), LD->getAAInfo());
7378 
7379     BasePTR = DAG.getObjectPtrOffset(SL, BasePTR, TypeSize::Fixed(Stride));
7380 
7381     Vals.push_back(ScalarLoad.getValue(0));
7382     LoadChains.push_back(ScalarLoad.getValue(1));
7383   }
7384 
7385   SDValue NewChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoadChains);
7386   SDValue Value = DAG.getBuildVector(DstVT, SL, Vals);
7387 
7388   return std::make_pair(Value, NewChain);
7389 }
7390 
7391 SDValue TargetLowering::scalarizeVectorStore(StoreSDNode *ST,
7392                                              SelectionDAG &DAG) const {
7393   SDLoc SL(ST);
7394 
7395   SDValue Chain = ST->getChain();
7396   SDValue BasePtr = ST->getBasePtr();
7397   SDValue Value = ST->getValue();
7398   EVT StVT = ST->getMemoryVT();
7399 
7400   if (StVT.isScalableVector())
7401     report_fatal_error("Cannot scalarize scalable vector stores");
7402 
7403   // The type of the data we want to save
7404   EVT RegVT = Value.getValueType();
7405   EVT RegSclVT = RegVT.getScalarType();
7406 
7407   // The type of data as saved in memory.
7408   EVT MemSclVT = StVT.getScalarType();
7409 
7410   unsigned NumElem = StVT.getVectorNumElements();
7411 
7412   // A vector must always be stored in memory as-is, i.e. without any padding
7413   // between the elements, since various code depend on it, e.g. in the
7414   // handling of a bitcast of a vector type to int, which may be done with a
7415   // vector store followed by an integer load. A vector that does not have
7416   // elements that are byte-sized must therefore be stored as an integer
7417   // built out of the extracted vector elements.
7418   if (!MemSclVT.isByteSized()) {
7419     unsigned NumBits = StVT.getSizeInBits();
7420     EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
7421 
7422     SDValue CurrVal = DAG.getConstant(0, SL, IntVT);
7423 
7424     for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
7425       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value,
7426                                 DAG.getVectorIdxConstant(Idx, SL));
7427       SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, MemSclVT, Elt);
7428       SDValue ExtElt = DAG.getNode(ISD::ZERO_EXTEND, SL, IntVT, Trunc);
7429       unsigned ShiftIntoIdx =
7430           (DAG.getDataLayout().isBigEndian() ? (NumElem - 1) - Idx : Idx);
7431       SDValue ShiftAmount =
7432           DAG.getConstant(ShiftIntoIdx * MemSclVT.getSizeInBits(), SL, IntVT);
7433       SDValue ShiftedElt =
7434           DAG.getNode(ISD::SHL, SL, IntVT, ExtElt, ShiftAmount);
7435       CurrVal = DAG.getNode(ISD::OR, SL, IntVT, CurrVal, ShiftedElt);
7436     }
7437 
7438     return DAG.getStore(Chain, SL, CurrVal, BasePtr, ST->getPointerInfo(),
7439                         ST->getOriginalAlign(), ST->getMemOperand()->getFlags(),
7440                         ST->getAAInfo());
7441   }
7442 
7443   // Store Stride in bytes
7444   unsigned Stride = MemSclVT.getSizeInBits() / 8;
7445   assert(Stride && "Zero stride!");
7446   // Extract each of the elements from the original vector and save them into
7447   // memory individually.
7448   SmallVector<SDValue, 8> Stores;
7449   for (unsigned Idx = 0; Idx < NumElem; ++Idx) {
7450     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, RegSclVT, Value,
7451                               DAG.getVectorIdxConstant(Idx, SL));
7452 
7453     SDValue Ptr =
7454         DAG.getObjectPtrOffset(SL, BasePtr, TypeSize::Fixed(Idx * Stride));
7455 
7456     // This scalar TruncStore may be illegal, but we legalize it later.
7457     SDValue Store = DAG.getTruncStore(
7458         Chain, SL, Elt, Ptr, ST->getPointerInfo().getWithOffset(Idx * Stride),
7459         MemSclVT, ST->getOriginalAlign(), ST->getMemOperand()->getFlags(),
7460         ST->getAAInfo());
7461 
7462     Stores.push_back(Store);
7463   }
7464 
7465   return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Stores);
7466 }
7467 
7468 std::pair<SDValue, SDValue>
7469 TargetLowering::expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const {
7470   assert(LD->getAddressingMode() == ISD::UNINDEXED &&
7471          "unaligned indexed loads not implemented!");
7472   SDValue Chain = LD->getChain();
7473   SDValue Ptr = LD->getBasePtr();
7474   EVT VT = LD->getValueType(0);
7475   EVT LoadedVT = LD->getMemoryVT();
7476   SDLoc dl(LD);
7477   auto &MF = DAG.getMachineFunction();
7478 
7479   if (VT.isFloatingPoint() || VT.isVector()) {
7480     EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
7481     if (isTypeLegal(intVT) && isTypeLegal(LoadedVT)) {
7482       if (!isOperationLegalOrCustom(ISD::LOAD, intVT) &&
7483           LoadedVT.isVector()) {
7484         // Scalarize the load and let the individual components be handled.
7485         return scalarizeVectorLoad(LD, DAG);
7486       }
7487 
7488       // Expand to a (misaligned) integer load of the same size,
7489       // then bitconvert to floating point or vector.
7490       SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr,
7491                                     LD->getMemOperand());
7492       SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
7493       if (LoadedVT != VT)
7494         Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND :
7495                              ISD::ANY_EXTEND, dl, VT, Result);
7496 
7497       return std::make_pair(Result, newLoad.getValue(1));
7498     }
7499 
7500     // Copy the value to a (aligned) stack slot using (unaligned) integer
7501     // loads and stores, then do a (aligned) load from the stack slot.
7502     MVT RegVT = getRegisterType(*DAG.getContext(), intVT);
7503     unsigned LoadedBytes = LoadedVT.getStoreSize();
7504     unsigned RegBytes = RegVT.getSizeInBits() / 8;
7505     unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
7506 
7507     // Make sure the stack slot is also aligned for the register type.
7508     SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
7509     auto FrameIndex = cast<FrameIndexSDNode>(StackBase.getNode())->getIndex();
7510     SmallVector<SDValue, 8> Stores;
7511     SDValue StackPtr = StackBase;
7512     unsigned Offset = 0;
7513 
7514     EVT PtrVT = Ptr.getValueType();
7515     EVT StackPtrVT = StackPtr.getValueType();
7516 
7517     SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
7518     SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
7519 
7520     // Do all but one copies using the full register width.
7521     for (unsigned i = 1; i < NumRegs; i++) {
7522       // Load one integer register's worth from the original location.
7523       SDValue Load = DAG.getLoad(
7524           RegVT, dl, Chain, Ptr, LD->getPointerInfo().getWithOffset(Offset),
7525           LD->getOriginalAlign(), LD->getMemOperand()->getFlags(),
7526           LD->getAAInfo());
7527       // Follow the load with a store to the stack slot.  Remember the store.
7528       Stores.push_back(DAG.getStore(
7529           Load.getValue(1), dl, Load, StackPtr,
7530           MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset)));
7531       // Increment the pointers.
7532       Offset += RegBytes;
7533 
7534       Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement);
7535       StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement);
7536     }
7537 
7538     // The last copy may be partial.  Do an extending load.
7539     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
7540                                   8 * (LoadedBytes - Offset));
7541     SDValue Load =
7542         DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
7543                        LD->getPointerInfo().getWithOffset(Offset), MemVT,
7544                        LD->getOriginalAlign(), LD->getMemOperand()->getFlags(),
7545                        LD->getAAInfo());
7546     // Follow the load with a store to the stack slot.  Remember the store.
7547     // On big-endian machines this requires a truncating store to ensure
7548     // that the bits end up in the right place.
7549     Stores.push_back(DAG.getTruncStore(
7550         Load.getValue(1), dl, Load, StackPtr,
7551         MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), MemVT));
7552 
7553     // The order of the stores doesn't matter - say it with a TokenFactor.
7554     SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7555 
7556     // Finally, perform the original load only redirected to the stack slot.
7557     Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
7558                           MachinePointerInfo::getFixedStack(MF, FrameIndex, 0),
7559                           LoadedVT);
7560 
7561     // Callers expect a MERGE_VALUES node.
7562     return std::make_pair(Load, TF);
7563   }
7564 
7565   assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
7566          "Unaligned load of unsupported type.");
7567 
7568   // Compute the new VT that is half the size of the old one.  This is an
7569   // integer MVT.
7570   unsigned NumBits = LoadedVT.getSizeInBits();
7571   EVT NewLoadedVT;
7572   NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
7573   NumBits >>= 1;
7574 
7575   Align Alignment = LD->getOriginalAlign();
7576   unsigned IncrementSize = NumBits / 8;
7577   ISD::LoadExtType HiExtType = LD->getExtensionType();
7578 
7579   // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
7580   if (HiExtType == ISD::NON_EXTLOAD)
7581     HiExtType = ISD::ZEXTLOAD;
7582 
7583   // Load the value in two parts
7584   SDValue Lo, Hi;
7585   if (DAG.getDataLayout().isLittleEndian()) {
7586     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
7587                         NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
7588                         LD->getAAInfo());
7589 
7590     Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
7591     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
7592                         LD->getPointerInfo().getWithOffset(IncrementSize),
7593                         NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
7594                         LD->getAAInfo());
7595   } else {
7596     Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
7597                         NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
7598                         LD->getAAInfo());
7599 
7600     Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
7601     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
7602                         LD->getPointerInfo().getWithOffset(IncrementSize),
7603                         NewLoadedVT, Alignment, LD->getMemOperand()->getFlags(),
7604                         LD->getAAInfo());
7605   }
7606 
7607   // aggregate the two parts
7608   SDValue ShiftAmount =
7609       DAG.getConstant(NumBits, dl, getShiftAmountTy(Hi.getValueType(),
7610                                                     DAG.getDataLayout()));
7611   SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
7612   Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
7613 
7614   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
7615                              Hi.getValue(1));
7616 
7617   return std::make_pair(Result, TF);
7618 }
7619 
7620 SDValue TargetLowering::expandUnalignedStore(StoreSDNode *ST,
7621                                              SelectionDAG &DAG) const {
7622   assert(ST->getAddressingMode() == ISD::UNINDEXED &&
7623          "unaligned indexed stores not implemented!");
7624   SDValue Chain = ST->getChain();
7625   SDValue Ptr = ST->getBasePtr();
7626   SDValue Val = ST->getValue();
7627   EVT VT = Val.getValueType();
7628   Align Alignment = ST->getOriginalAlign();
7629   auto &MF = DAG.getMachineFunction();
7630   EVT StoreMemVT = ST->getMemoryVT();
7631 
7632   SDLoc dl(ST);
7633   if (StoreMemVT.isFloatingPoint() || StoreMemVT.isVector()) {
7634     EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7635     if (isTypeLegal(intVT)) {
7636       if (!isOperationLegalOrCustom(ISD::STORE, intVT) &&
7637           StoreMemVT.isVector()) {
7638         // Scalarize the store and let the individual components be handled.
7639         SDValue Result = scalarizeVectorStore(ST, DAG);
7640         return Result;
7641       }
7642       // Expand to a bitconvert of the value to the integer type of the
7643       // same size, then a (misaligned) int store.
7644       // FIXME: Does not handle truncating floating point stores!
7645       SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
7646       Result = DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
7647                             Alignment, ST->getMemOperand()->getFlags());
7648       return Result;
7649     }
7650     // Do a (aligned) store to a stack slot, then copy from the stack slot
7651     // to the final destination using (unaligned) integer loads and stores.
7652     MVT RegVT = getRegisterType(
7653         *DAG.getContext(),
7654         EVT::getIntegerVT(*DAG.getContext(), StoreMemVT.getSizeInBits()));
7655     EVT PtrVT = Ptr.getValueType();
7656     unsigned StoredBytes = StoreMemVT.getStoreSize();
7657     unsigned RegBytes = RegVT.getSizeInBits() / 8;
7658     unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
7659 
7660     // Make sure the stack slot is also aligned for the register type.
7661     SDValue StackPtr = DAG.CreateStackTemporary(StoreMemVT, RegVT);
7662     auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
7663 
7664     // Perform the original store, only redirected to the stack slot.
7665     SDValue Store = DAG.getTruncStore(
7666         Chain, dl, Val, StackPtr,
7667         MachinePointerInfo::getFixedStack(MF, FrameIndex, 0), StoreMemVT);
7668 
7669     EVT StackPtrVT = StackPtr.getValueType();
7670 
7671     SDValue PtrIncrement = DAG.getConstant(RegBytes, dl, PtrVT);
7672     SDValue StackPtrIncrement = DAG.getConstant(RegBytes, dl, StackPtrVT);
7673     SmallVector<SDValue, 8> Stores;
7674     unsigned Offset = 0;
7675 
7676     // Do all but one copies using the full register width.
7677     for (unsigned i = 1; i < NumRegs; i++) {
7678       // Load one integer register's worth from the stack slot.
7679       SDValue Load = DAG.getLoad(
7680           RegVT, dl, Store, StackPtr,
7681           MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset));
7682       // Store it to the final location.  Remember the store.
7683       Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
7684                                     ST->getPointerInfo().getWithOffset(Offset),
7685                                     ST->getOriginalAlign(),
7686                                     ST->getMemOperand()->getFlags()));
7687       // Increment the pointers.
7688       Offset += RegBytes;
7689       StackPtr = DAG.getObjectPtrOffset(dl, StackPtr, StackPtrIncrement);
7690       Ptr = DAG.getObjectPtrOffset(dl, Ptr, PtrIncrement);
7691     }
7692 
7693     // The last store may be partial.  Do a truncating store.  On big-endian
7694     // machines this requires an extending load from the stack slot to ensure
7695     // that the bits are in the right place.
7696     EVT LoadMemVT =
7697         EVT::getIntegerVT(*DAG.getContext(), 8 * (StoredBytes - Offset));
7698 
7699     // Load from the stack slot.
7700     SDValue Load = DAG.getExtLoad(
7701         ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
7702         MachinePointerInfo::getFixedStack(MF, FrameIndex, Offset), LoadMemVT);
7703 
7704     Stores.push_back(
7705         DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
7706                           ST->getPointerInfo().getWithOffset(Offset), LoadMemVT,
7707                           ST->getOriginalAlign(),
7708                           ST->getMemOperand()->getFlags(), ST->getAAInfo()));
7709     // The order of the stores doesn't matter - say it with a TokenFactor.
7710     SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
7711     return Result;
7712   }
7713 
7714   assert(StoreMemVT.isInteger() && !StoreMemVT.isVector() &&
7715          "Unaligned store of unknown type.");
7716   // Get the half-size VT
7717   EVT NewStoredVT = StoreMemVT.getHalfSizedIntegerVT(*DAG.getContext());
7718   unsigned NumBits = NewStoredVT.getFixedSizeInBits();
7719   unsigned IncrementSize = NumBits / 8;
7720 
7721   // Divide the stored value in two parts.
7722   SDValue ShiftAmount = DAG.getConstant(
7723       NumBits, dl, getShiftAmountTy(Val.getValueType(), DAG.getDataLayout()));
7724   SDValue Lo = Val;
7725   SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
7726 
7727   // Store the two parts
7728   SDValue Store1, Store2;
7729   Store1 = DAG.getTruncStore(Chain, dl,
7730                              DAG.getDataLayout().isLittleEndian() ? Lo : Hi,
7731                              Ptr, ST->getPointerInfo(), NewStoredVT, Alignment,
7732                              ST->getMemOperand()->getFlags());
7733 
7734   Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
7735   Store2 = DAG.getTruncStore(
7736       Chain, dl, DAG.getDataLayout().isLittleEndian() ? Hi : Lo, Ptr,
7737       ST->getPointerInfo().getWithOffset(IncrementSize), NewStoredVT, Alignment,
7738       ST->getMemOperand()->getFlags(), ST->getAAInfo());
7739 
7740   SDValue Result =
7741       DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
7742   return Result;
7743 }
7744 
7745 SDValue
7746 TargetLowering::IncrementMemoryAddress(SDValue Addr, SDValue Mask,
7747                                        const SDLoc &DL, EVT DataVT,
7748                                        SelectionDAG &DAG,
7749                                        bool IsCompressedMemory) const {
7750   SDValue Increment;
7751   EVT AddrVT = Addr.getValueType();
7752   EVT MaskVT = Mask.getValueType();
7753   assert(DataVT.getVectorElementCount() == MaskVT.getVectorElementCount() &&
7754          "Incompatible types of Data and Mask");
7755   if (IsCompressedMemory) {
7756     if (DataVT.isScalableVector())
7757       report_fatal_error(
7758           "Cannot currently handle compressed memory with scalable vectors");
7759     // Incrementing the pointer according to number of '1's in the mask.
7760     EVT MaskIntVT = EVT::getIntegerVT(*DAG.getContext(), MaskVT.getSizeInBits());
7761     SDValue MaskInIntReg = DAG.getBitcast(MaskIntVT, Mask);
7762     if (MaskIntVT.getSizeInBits() < 32) {
7763       MaskInIntReg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, MaskInIntReg);
7764       MaskIntVT = MVT::i32;
7765     }
7766 
7767     // Count '1's with POPCNT.
7768     Increment = DAG.getNode(ISD::CTPOP, DL, MaskIntVT, MaskInIntReg);
7769     Increment = DAG.getZExtOrTrunc(Increment, DL, AddrVT);
7770     // Scale is an element size in bytes.
7771     SDValue Scale = DAG.getConstant(DataVT.getScalarSizeInBits() / 8, DL,
7772                                     AddrVT);
7773     Increment = DAG.getNode(ISD::MUL, DL, AddrVT, Increment, Scale);
7774   } else if (DataVT.isScalableVector()) {
7775     Increment = DAG.getVScale(DL, AddrVT,
7776                               APInt(AddrVT.getFixedSizeInBits(),
7777                                     DataVT.getStoreSize().getKnownMinSize()));
7778   } else
7779     Increment = DAG.getConstant(DataVT.getStoreSize(), DL, AddrVT);
7780 
7781   return DAG.getNode(ISD::ADD, DL, AddrVT, Addr, Increment);
7782 }
7783 
7784 static SDValue clampDynamicVectorIndex(SelectionDAG &DAG, SDValue Idx,
7785                                        EVT VecVT, const SDLoc &dl,
7786                                        unsigned NumSubElts) {
7787   if (!VecVT.isScalableVector() && isa<ConstantSDNode>(Idx))
7788     return Idx;
7789 
7790   EVT IdxVT = Idx.getValueType();
7791   unsigned NElts = VecVT.getVectorMinNumElements();
7792   if (VecVT.isScalableVector()) {
7793     // If this is a constant index and we know the value plus the number of the
7794     // elements in the subvector minus one is less than the minimum number of
7795     // elements then it's safe to return Idx.
7796     if (auto *IdxCst = dyn_cast<ConstantSDNode>(Idx))
7797       if (IdxCst->getZExtValue() + (NumSubElts - 1) < NElts)
7798         return Idx;
7799     SDValue VS =
7800         DAG.getVScale(dl, IdxVT, APInt(IdxVT.getFixedSizeInBits(), NElts));
7801     unsigned SubOpcode = NumSubElts <= NElts ? ISD::SUB : ISD::USUBSAT;
7802     SDValue Sub = DAG.getNode(SubOpcode, dl, IdxVT, VS,
7803                               DAG.getConstant(NumSubElts, dl, IdxVT));
7804     return DAG.getNode(ISD::UMIN, dl, IdxVT, Idx, Sub);
7805   }
7806   if (isPowerOf2_32(NElts) && NumSubElts == 1) {
7807     APInt Imm = APInt::getLowBitsSet(IdxVT.getSizeInBits(), Log2_32(NElts));
7808     return DAG.getNode(ISD::AND, dl, IdxVT, Idx,
7809                        DAG.getConstant(Imm, dl, IdxVT));
7810   }
7811   unsigned MaxIndex = NumSubElts < NElts ? NElts - NumSubElts : 0;
7812   return DAG.getNode(ISD::UMIN, dl, IdxVT, Idx,
7813                      DAG.getConstant(MaxIndex, dl, IdxVT));
7814 }
7815 
7816 SDValue TargetLowering::getVectorElementPointer(SelectionDAG &DAG,
7817                                                 SDValue VecPtr, EVT VecVT,
7818                                                 SDValue Index) const {
7819   return getVectorSubVecPointer(
7820       DAG, VecPtr, VecVT,
7821       EVT::getVectorVT(*DAG.getContext(), VecVT.getVectorElementType(), 1),
7822       Index);
7823 }
7824 
7825 SDValue TargetLowering::getVectorSubVecPointer(SelectionDAG &DAG,
7826                                                SDValue VecPtr, EVT VecVT,
7827                                                EVT SubVecVT,
7828                                                SDValue Index) const {
7829   SDLoc dl(Index);
7830   // Make sure the index type is big enough to compute in.
7831   Index = DAG.getZExtOrTrunc(Index, dl, VecPtr.getValueType());
7832 
7833   EVT EltVT = VecVT.getVectorElementType();
7834 
7835   // Calculate the element offset and add it to the pointer.
7836   unsigned EltSize = EltVT.getFixedSizeInBits() / 8; // FIXME: should be ABI size.
7837   assert(EltSize * 8 == EltVT.getFixedSizeInBits() &&
7838          "Converting bits to bytes lost precision");
7839 
7840   assert(SubVecVT.isFixedLengthVector() &&
7841          SubVecVT.getVectorElementType() == EltVT &&
7842          "Sub-vector must be a fixed vector with matching element type");
7843   Index = clampDynamicVectorIndex(DAG, Index, VecVT, dl,
7844                                   SubVecVT.getVectorNumElements());
7845 
7846   EVT IdxVT = Index.getValueType();
7847 
7848   Index = DAG.getNode(ISD::MUL, dl, IdxVT, Index,
7849                       DAG.getConstant(EltSize, dl, IdxVT));
7850   return DAG.getMemBasePlusOffset(VecPtr, Index, dl);
7851 }
7852 
7853 //===----------------------------------------------------------------------===//
7854 // Implementation of Emulated TLS Model
7855 //===----------------------------------------------------------------------===//
7856 
7857 SDValue TargetLowering::LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
7858                                                 SelectionDAG &DAG) const {
7859   // Access to address of TLS varialbe xyz is lowered to a function call:
7860   //   __emutls_get_address( address of global variable named "__emutls_v.xyz" )
7861   EVT PtrVT = getPointerTy(DAG.getDataLayout());
7862   PointerType *VoidPtrType = Type::getInt8PtrTy(*DAG.getContext());
7863   SDLoc dl(GA);
7864 
7865   ArgListTy Args;
7866   ArgListEntry Entry;
7867   std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str();
7868   Module *VariableModule = const_cast<Module*>(GA->getGlobal()->getParent());
7869   StringRef EmuTlsVarName(NameString);
7870   GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName);
7871   assert(EmuTlsVar && "Cannot find EmuTlsVar ");
7872   Entry.Node = DAG.getGlobalAddress(EmuTlsVar, dl, PtrVT);
7873   Entry.Ty = VoidPtrType;
7874   Args.push_back(Entry);
7875 
7876   SDValue EmuTlsGetAddr = DAG.getExternalSymbol("__emutls_get_address", PtrVT);
7877 
7878   TargetLowering::CallLoweringInfo CLI(DAG);
7879   CLI.setDebugLoc(dl).setChain(DAG.getEntryNode());
7880   CLI.setLibCallee(CallingConv::C, VoidPtrType, EmuTlsGetAddr, std::move(Args));
7881   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
7882 
7883   // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
7884   // At last for X86 targets, maybe good for other targets too?
7885   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
7886   MFI.setAdjustsStack(true); // Is this only for X86 target?
7887   MFI.setHasCalls(true);
7888 
7889   assert((GA->getOffset() == 0) &&
7890          "Emulated TLS must have zero offset in GlobalAddressSDNode");
7891   return CallResult.first;
7892 }
7893 
7894 SDValue TargetLowering::lowerCmpEqZeroToCtlzSrl(SDValue Op,
7895                                                 SelectionDAG &DAG) const {
7896   assert((Op->getOpcode() == ISD::SETCC) && "Input has to be a SETCC node.");
7897   if (!isCtlzFast())
7898     return SDValue();
7899   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7900   SDLoc dl(Op);
7901   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
7902     if (C->isNullValue() && CC == ISD::SETEQ) {
7903       EVT VT = Op.getOperand(0).getValueType();
7904       SDValue Zext = Op.getOperand(0);
7905       if (VT.bitsLT(MVT::i32)) {
7906         VT = MVT::i32;
7907         Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
7908       }
7909       unsigned Log2b = Log2_32(VT.getSizeInBits());
7910       SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
7911       SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
7912                                 DAG.getConstant(Log2b, dl, MVT::i32));
7913       return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
7914     }
7915   }
7916   return SDValue();
7917 }
7918 
7919 // Convert redundant addressing modes (e.g. scaling is redundant
7920 // when accessing bytes).
7921 ISD::MemIndexType
7922 TargetLowering::getCanonicalIndexType(ISD::MemIndexType IndexType, EVT MemVT,
7923                                       SDValue Offsets) const {
7924   bool IsScaledIndex =
7925       (IndexType == ISD::SIGNED_SCALED) || (IndexType == ISD::UNSIGNED_SCALED);
7926   bool IsSignedIndex =
7927       (IndexType == ISD::SIGNED_SCALED) || (IndexType == ISD::SIGNED_UNSCALED);
7928 
7929   // Scaling is unimportant for bytes, canonicalize to unscaled.
7930   if (IsScaledIndex && MemVT.getScalarType() == MVT::i8) {
7931     IsScaledIndex = false;
7932     IndexType = IsSignedIndex ? ISD::SIGNED_UNSCALED : ISD::UNSIGNED_UNSCALED;
7933   }
7934 
7935   return IndexType;
7936 }
7937 
7938 SDValue TargetLowering::expandIntMINMAX(SDNode *Node, SelectionDAG &DAG) const {
7939   SDValue Op0 = Node->getOperand(0);
7940   SDValue Op1 = Node->getOperand(1);
7941   EVT VT = Op0.getValueType();
7942   unsigned Opcode = Node->getOpcode();
7943   SDLoc DL(Node);
7944 
7945   // umin(x,y) -> sub(x,usubsat(x,y))
7946   if (Opcode == ISD::UMIN && isOperationLegal(ISD::SUB, VT) &&
7947       isOperationLegal(ISD::USUBSAT, VT)) {
7948     return DAG.getNode(ISD::SUB, DL, VT, Op0,
7949                        DAG.getNode(ISD::USUBSAT, DL, VT, Op0, Op1));
7950   }
7951 
7952   // umax(x,y) -> add(x,usubsat(y,x))
7953   if (Opcode == ISD::UMAX && isOperationLegal(ISD::ADD, VT) &&
7954       isOperationLegal(ISD::USUBSAT, VT)) {
7955     return DAG.getNode(ISD::ADD, DL, VT, Op0,
7956                        DAG.getNode(ISD::USUBSAT, DL, VT, Op1, Op0));
7957   }
7958 
7959   // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
7960   ISD::CondCode CC;
7961   switch (Opcode) {
7962   default: llvm_unreachable("How did we get here?");
7963   case ISD::SMAX: CC = ISD::SETGT; break;
7964   case ISD::SMIN: CC = ISD::SETLT; break;
7965   case ISD::UMAX: CC = ISD::SETUGT; break;
7966   case ISD::UMIN: CC = ISD::SETULT; break;
7967   }
7968 
7969   // FIXME: Should really try to split the vector in case it's legal on a
7970   // subvector.
7971   if (VT.isVector() && !isOperationLegalOrCustom(ISD::VSELECT, VT))
7972     return DAG.UnrollVectorOp(Node);
7973 
7974   SDValue Cond = DAG.getSetCC(DL, VT, Op0, Op1, CC);
7975   return DAG.getSelect(DL, VT, Cond, Op0, Op1);
7976 }
7977 
7978 SDValue TargetLowering::expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const {
7979   unsigned Opcode = Node->getOpcode();
7980   SDValue LHS = Node->getOperand(0);
7981   SDValue RHS = Node->getOperand(1);
7982   EVT VT = LHS.getValueType();
7983   SDLoc dl(Node);
7984 
7985   assert(VT == RHS.getValueType() && "Expected operands to be the same type");
7986   assert(VT.isInteger() && "Expected operands to be integers");
7987 
7988   // usub.sat(a, b) -> umax(a, b) - b
7989   if (Opcode == ISD::USUBSAT && isOperationLegal(ISD::UMAX, VT)) {
7990     SDValue Max = DAG.getNode(ISD::UMAX, dl, VT, LHS, RHS);
7991     return DAG.getNode(ISD::SUB, dl, VT, Max, RHS);
7992   }
7993 
7994   // uadd.sat(a, b) -> umin(a, ~b) + b
7995   if (Opcode == ISD::UADDSAT && isOperationLegal(ISD::UMIN, VT)) {
7996     SDValue InvRHS = DAG.getNOT(dl, RHS, VT);
7997     SDValue Min = DAG.getNode(ISD::UMIN, dl, VT, LHS, InvRHS);
7998     return DAG.getNode(ISD::ADD, dl, VT, Min, RHS);
7999   }
8000 
8001   unsigned OverflowOp;
8002   switch (Opcode) {
8003   case ISD::SADDSAT:
8004     OverflowOp = ISD::SADDO;
8005     break;
8006   case ISD::UADDSAT:
8007     OverflowOp = ISD::UADDO;
8008     break;
8009   case ISD::SSUBSAT:
8010     OverflowOp = ISD::SSUBO;
8011     break;
8012   case ISD::USUBSAT:
8013     OverflowOp = ISD::USUBO;
8014     break;
8015   default:
8016     llvm_unreachable("Expected method to receive signed or unsigned saturation "
8017                      "addition or subtraction node.");
8018   }
8019 
8020   // FIXME: Should really try to split the vector in case it's legal on a
8021   // subvector.
8022   if (VT.isVector() && !isOperationLegalOrCustom(ISD::VSELECT, VT))
8023     return DAG.UnrollVectorOp(Node);
8024 
8025   unsigned BitWidth = LHS.getScalarValueSizeInBits();
8026   EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
8027   SDValue Result = DAG.getNode(OverflowOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
8028   SDValue SumDiff = Result.getValue(0);
8029   SDValue Overflow = Result.getValue(1);
8030   SDValue Zero = DAG.getConstant(0, dl, VT);
8031   SDValue AllOnes = DAG.getAllOnesConstant(dl, VT);
8032 
8033   if (Opcode == ISD::UADDSAT) {
8034     if (getBooleanContents(VT) == ZeroOrNegativeOneBooleanContent) {
8035       // (LHS + RHS) | OverflowMask
8036       SDValue OverflowMask = DAG.getSExtOrTrunc(Overflow, dl, VT);
8037       return DAG.getNode(ISD::OR, dl, VT, SumDiff, OverflowMask);
8038     }
8039     // Overflow ? 0xffff.... : (LHS + RHS)
8040     return DAG.getSelect(dl, VT, Overflow, AllOnes, SumDiff);
8041   }
8042 
8043   if (Opcode == ISD::USUBSAT) {
8044     if (getBooleanContents(VT) == ZeroOrNegativeOneBooleanContent) {
8045       // (LHS - RHS) & ~OverflowMask
8046       SDValue OverflowMask = DAG.getSExtOrTrunc(Overflow, dl, VT);
8047       SDValue Not = DAG.getNOT(dl, OverflowMask, VT);
8048       return DAG.getNode(ISD::AND, dl, VT, SumDiff, Not);
8049     }
8050     // Overflow ? 0 : (LHS - RHS)
8051     return DAG.getSelect(dl, VT, Overflow, Zero, SumDiff);
8052   }
8053 
8054   // SatMax -> Overflow && SumDiff < 0
8055   // SatMin -> Overflow && SumDiff >= 0
8056   APInt MinVal = APInt::getSignedMinValue(BitWidth);
8057   APInt MaxVal = APInt::getSignedMaxValue(BitWidth);
8058   SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
8059   SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
8060   SDValue SumNeg = DAG.getSetCC(dl, BoolVT, SumDiff, Zero, ISD::SETLT);
8061   Result = DAG.getSelect(dl, VT, SumNeg, SatMax, SatMin);
8062   return DAG.getSelect(dl, VT, Overflow, Result, SumDiff);
8063 }
8064 
8065 SDValue TargetLowering::expandShlSat(SDNode *Node, SelectionDAG &DAG) const {
8066   unsigned Opcode = Node->getOpcode();
8067   bool IsSigned = Opcode == ISD::SSHLSAT;
8068   SDValue LHS = Node->getOperand(0);
8069   SDValue RHS = Node->getOperand(1);
8070   EVT VT = LHS.getValueType();
8071   SDLoc dl(Node);
8072 
8073   assert((Node->getOpcode() == ISD::SSHLSAT ||
8074           Node->getOpcode() == ISD::USHLSAT) &&
8075           "Expected a SHLSAT opcode");
8076   assert(VT == RHS.getValueType() && "Expected operands to be the same type");
8077   assert(VT.isInteger() && "Expected operands to be integers");
8078 
8079   // If LHS != (LHS << RHS) >> RHS, we have overflow and must saturate.
8080 
8081   unsigned BW = VT.getScalarSizeInBits();
8082   SDValue Result = DAG.getNode(ISD::SHL, dl, VT, LHS, RHS);
8083   SDValue Orig =
8084       DAG.getNode(IsSigned ? ISD::SRA : ISD::SRL, dl, VT, Result, RHS);
8085 
8086   SDValue SatVal;
8087   if (IsSigned) {
8088     SDValue SatMin = DAG.getConstant(APInt::getSignedMinValue(BW), dl, VT);
8089     SDValue SatMax = DAG.getConstant(APInt::getSignedMaxValue(BW), dl, VT);
8090     SatVal = DAG.getSelectCC(dl, LHS, DAG.getConstant(0, dl, VT),
8091                              SatMin, SatMax, ISD::SETLT);
8092   } else {
8093     SatVal = DAG.getConstant(APInt::getMaxValue(BW), dl, VT);
8094   }
8095   Result = DAG.getSelectCC(dl, LHS, Orig, SatVal, Result, ISD::SETNE);
8096 
8097   return Result;
8098 }
8099 
8100 SDValue
8101 TargetLowering::expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const {
8102   assert((Node->getOpcode() == ISD::SMULFIX ||
8103           Node->getOpcode() == ISD::UMULFIX ||
8104           Node->getOpcode() == ISD::SMULFIXSAT ||
8105           Node->getOpcode() == ISD::UMULFIXSAT) &&
8106          "Expected a fixed point multiplication opcode");
8107 
8108   SDLoc dl(Node);
8109   SDValue LHS = Node->getOperand(0);
8110   SDValue RHS = Node->getOperand(1);
8111   EVT VT = LHS.getValueType();
8112   unsigned Scale = Node->getConstantOperandVal(2);
8113   bool Saturating = (Node->getOpcode() == ISD::SMULFIXSAT ||
8114                      Node->getOpcode() == ISD::UMULFIXSAT);
8115   bool Signed = (Node->getOpcode() == ISD::SMULFIX ||
8116                  Node->getOpcode() == ISD::SMULFIXSAT);
8117   EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
8118   unsigned VTSize = VT.getScalarSizeInBits();
8119 
8120   if (!Scale) {
8121     // [us]mul.fix(a, b, 0) -> mul(a, b)
8122     if (!Saturating) {
8123       if (isOperationLegalOrCustom(ISD::MUL, VT))
8124         return DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
8125     } else if (Signed && isOperationLegalOrCustom(ISD::SMULO, VT)) {
8126       SDValue Result =
8127           DAG.getNode(ISD::SMULO, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
8128       SDValue Product = Result.getValue(0);
8129       SDValue Overflow = Result.getValue(1);
8130       SDValue Zero = DAG.getConstant(0, dl, VT);
8131 
8132       APInt MinVal = APInt::getSignedMinValue(VTSize);
8133       APInt MaxVal = APInt::getSignedMaxValue(VTSize);
8134       SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
8135       SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
8136       SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Product, Zero, ISD::SETLT);
8137       Result = DAG.getSelect(dl, VT, ProdNeg, SatMax, SatMin);
8138       return DAG.getSelect(dl, VT, Overflow, Result, Product);
8139     } else if (!Signed && isOperationLegalOrCustom(ISD::UMULO, VT)) {
8140       SDValue Result =
8141           DAG.getNode(ISD::UMULO, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
8142       SDValue Product = Result.getValue(0);
8143       SDValue Overflow = Result.getValue(1);
8144 
8145       APInt MaxVal = APInt::getMaxValue(VTSize);
8146       SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
8147       return DAG.getSelect(dl, VT, Overflow, SatMax, Product);
8148     }
8149   }
8150 
8151   assert(((Signed && Scale < VTSize) || (!Signed && Scale <= VTSize)) &&
8152          "Expected scale to be less than the number of bits if signed or at "
8153          "most the number of bits if unsigned.");
8154   assert(LHS.getValueType() == RHS.getValueType() &&
8155          "Expected both operands to be the same type");
8156 
8157   // Get the upper and lower bits of the result.
8158   SDValue Lo, Hi;
8159   unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
8160   unsigned HiOp = Signed ? ISD::MULHS : ISD::MULHU;
8161   if (isOperationLegalOrCustom(LoHiOp, VT)) {
8162     SDValue Result = DAG.getNode(LoHiOp, dl, DAG.getVTList(VT, VT), LHS, RHS);
8163     Lo = Result.getValue(0);
8164     Hi = Result.getValue(1);
8165   } else if (isOperationLegalOrCustom(HiOp, VT)) {
8166     Lo = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
8167     Hi = DAG.getNode(HiOp, dl, VT, LHS, RHS);
8168   } else if (VT.isVector()) {
8169     return SDValue();
8170   } else {
8171     report_fatal_error("Unable to expand fixed point multiplication.");
8172   }
8173 
8174   if (Scale == VTSize)
8175     // Result is just the top half since we'd be shifting by the width of the
8176     // operand. Overflow impossible so this works for both UMULFIX and
8177     // UMULFIXSAT.
8178     return Hi;
8179 
8180   // The result will need to be shifted right by the scale since both operands
8181   // are scaled. The result is given to us in 2 halves, so we only want part of
8182   // both in the result.
8183   EVT ShiftTy = getShiftAmountTy(VT, DAG.getDataLayout());
8184   SDValue Result = DAG.getNode(ISD::FSHR, dl, VT, Hi, Lo,
8185                                DAG.getConstant(Scale, dl, ShiftTy));
8186   if (!Saturating)
8187     return Result;
8188 
8189   if (!Signed) {
8190     // Unsigned overflow happened if the upper (VTSize - Scale) bits (of the
8191     // widened multiplication) aren't all zeroes.
8192 
8193     // Saturate to max if ((Hi >> Scale) != 0),
8194     // which is the same as if (Hi > ((1 << Scale) - 1))
8195     APInt MaxVal = APInt::getMaxValue(VTSize);
8196     SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale),
8197                                       dl, VT);
8198     Result = DAG.getSelectCC(dl, Hi, LowMask,
8199                              DAG.getConstant(MaxVal, dl, VT), Result,
8200                              ISD::SETUGT);
8201 
8202     return Result;
8203   }
8204 
8205   // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of the
8206   // widened multiplication) aren't all ones or all zeroes.
8207 
8208   SDValue SatMin = DAG.getConstant(APInt::getSignedMinValue(VTSize), dl, VT);
8209   SDValue SatMax = DAG.getConstant(APInt::getSignedMaxValue(VTSize), dl, VT);
8210 
8211   if (Scale == 0) {
8212     SDValue Sign = DAG.getNode(ISD::SRA, dl, VT, Lo,
8213                                DAG.getConstant(VTSize - 1, dl, ShiftTy));
8214     SDValue Overflow = DAG.getSetCC(dl, BoolVT, Hi, Sign, ISD::SETNE);
8215     // Saturated to SatMin if wide product is negative, and SatMax if wide
8216     // product is positive ...
8217     SDValue Zero = DAG.getConstant(0, dl, VT);
8218     SDValue ResultIfOverflow = DAG.getSelectCC(dl, Hi, Zero, SatMin, SatMax,
8219                                                ISD::SETLT);
8220     // ... but only if we overflowed.
8221     return DAG.getSelect(dl, VT, Overflow, ResultIfOverflow, Result);
8222   }
8223 
8224   //  We handled Scale==0 above so all the bits to examine is in Hi.
8225 
8226   // Saturate to max if ((Hi >> (Scale - 1)) > 0),
8227   // which is the same as if (Hi > (1 << (Scale - 1)) - 1)
8228   SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale - 1),
8229                                     dl, VT);
8230   Result = DAG.getSelectCC(dl, Hi, LowMask, SatMax, Result, ISD::SETGT);
8231   // Saturate to min if (Hi >> (Scale - 1)) < -1),
8232   // which is the same as if (HI < (-1 << (Scale - 1))
8233   SDValue HighMask =
8234       DAG.getConstant(APInt::getHighBitsSet(VTSize, VTSize - Scale + 1),
8235                       dl, VT);
8236   Result = DAG.getSelectCC(dl, Hi, HighMask, SatMin, Result, ISD::SETLT);
8237   return Result;
8238 }
8239 
8240 SDValue
8241 TargetLowering::expandFixedPointDiv(unsigned Opcode, const SDLoc &dl,
8242                                     SDValue LHS, SDValue RHS,
8243                                     unsigned Scale, SelectionDAG &DAG) const {
8244   assert((Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT ||
8245           Opcode == ISD::UDIVFIX || Opcode == ISD::UDIVFIXSAT) &&
8246          "Expected a fixed point division opcode");
8247 
8248   EVT VT = LHS.getValueType();
8249   bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
8250   bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
8251   EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
8252 
8253   // If there is enough room in the type to upscale the LHS or downscale the
8254   // RHS before the division, we can perform it in this type without having to
8255   // resize. For signed operations, the LHS headroom is the number of
8256   // redundant sign bits, and for unsigned ones it is the number of zeroes.
8257   // The headroom for the RHS is the number of trailing zeroes.
8258   unsigned LHSLead = Signed ? DAG.ComputeNumSignBits(LHS) - 1
8259                             : DAG.computeKnownBits(LHS).countMinLeadingZeros();
8260   unsigned RHSTrail = DAG.computeKnownBits(RHS).countMinTrailingZeros();
8261 
8262   // For signed saturating operations, we need to be able to detect true integer
8263   // division overflow; that is, when you have MIN / -EPS. However, this
8264   // is undefined behavior and if we emit divisions that could take such
8265   // values it may cause undesired behavior (arithmetic exceptions on x86, for
8266   // example).
8267   // Avoid this by requiring an extra bit so that we never get this case.
8268   // FIXME: This is a bit unfortunate as it means that for an 8-bit 7-scale
8269   // signed saturating division, we need to emit a whopping 32-bit division.
8270   if (LHSLead + RHSTrail < Scale + (unsigned)(Saturating && Signed))
8271     return SDValue();
8272 
8273   unsigned LHSShift = std::min(LHSLead, Scale);
8274   unsigned RHSShift = Scale - LHSShift;
8275 
8276   // At this point, we know that if we shift the LHS up by LHSShift and the
8277   // RHS down by RHSShift, we can emit a regular division with a final scaling
8278   // factor of Scale.
8279 
8280   EVT ShiftTy = getShiftAmountTy(VT, DAG.getDataLayout());
8281   if (LHSShift)
8282     LHS = DAG.getNode(ISD::SHL, dl, VT, LHS,
8283                       DAG.getConstant(LHSShift, dl, ShiftTy));
8284   if (RHSShift)
8285     RHS = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, dl, VT, RHS,
8286                       DAG.getConstant(RHSShift, dl, ShiftTy));
8287 
8288   SDValue Quot;
8289   if (Signed) {
8290     // For signed operations, if the resulting quotient is negative and the
8291     // remainder is nonzero, subtract 1 from the quotient to round towards
8292     // negative infinity.
8293     SDValue Rem;
8294     // FIXME: Ideally we would always produce an SDIVREM here, but if the
8295     // type isn't legal, SDIVREM cannot be expanded. There is no reason why
8296     // we couldn't just form a libcall, but the type legalizer doesn't do it.
8297     if (isTypeLegal(VT) &&
8298         isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
8299       Quot = DAG.getNode(ISD::SDIVREM, dl,
8300                          DAG.getVTList(VT, VT),
8301                          LHS, RHS);
8302       Rem = Quot.getValue(1);
8303       Quot = Quot.getValue(0);
8304     } else {
8305       Quot = DAG.getNode(ISD::SDIV, dl, VT,
8306                          LHS, RHS);
8307       Rem = DAG.getNode(ISD::SREM, dl, VT,
8308                         LHS, RHS);
8309     }
8310     SDValue Zero = DAG.getConstant(0, dl, VT);
8311     SDValue RemNonZero = DAG.getSetCC(dl, BoolVT, Rem, Zero, ISD::SETNE);
8312     SDValue LHSNeg = DAG.getSetCC(dl, BoolVT, LHS, Zero, ISD::SETLT);
8313     SDValue RHSNeg = DAG.getSetCC(dl, BoolVT, RHS, Zero, ISD::SETLT);
8314     SDValue QuotNeg = DAG.getNode(ISD::XOR, dl, BoolVT, LHSNeg, RHSNeg);
8315     SDValue Sub1 = DAG.getNode(ISD::SUB, dl, VT, Quot,
8316                                DAG.getConstant(1, dl, VT));
8317     Quot = DAG.getSelect(dl, VT,
8318                          DAG.getNode(ISD::AND, dl, BoolVT, RemNonZero, QuotNeg),
8319                          Sub1, Quot);
8320   } else
8321     Quot = DAG.getNode(ISD::UDIV, dl, VT,
8322                        LHS, RHS);
8323 
8324   return Quot;
8325 }
8326 
8327 void TargetLowering::expandUADDSUBO(
8328     SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const {
8329   SDLoc dl(Node);
8330   SDValue LHS = Node->getOperand(0);
8331   SDValue RHS = Node->getOperand(1);
8332   bool IsAdd = Node->getOpcode() == ISD::UADDO;
8333 
8334   // If ADD/SUBCARRY is legal, use that instead.
8335   unsigned OpcCarry = IsAdd ? ISD::ADDCARRY : ISD::SUBCARRY;
8336   if (isOperationLegalOrCustom(OpcCarry, Node->getValueType(0))) {
8337     SDValue CarryIn = DAG.getConstant(0, dl, Node->getValueType(1));
8338     SDValue NodeCarry = DAG.getNode(OpcCarry, dl, Node->getVTList(),
8339                                     { LHS, RHS, CarryIn });
8340     Result = SDValue(NodeCarry.getNode(), 0);
8341     Overflow = SDValue(NodeCarry.getNode(), 1);
8342     return;
8343   }
8344 
8345   Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl,
8346                             LHS.getValueType(), LHS, RHS);
8347 
8348   EVT ResultType = Node->getValueType(1);
8349   EVT SetCCType = getSetCCResultType(
8350       DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0));
8351   ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT;
8352   SDValue SetCC = DAG.getSetCC(dl, SetCCType, Result, LHS, CC);
8353   Overflow = DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType);
8354 }
8355 
8356 void TargetLowering::expandSADDSUBO(
8357     SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const {
8358   SDLoc dl(Node);
8359   SDValue LHS = Node->getOperand(0);
8360   SDValue RHS = Node->getOperand(1);
8361   bool IsAdd = Node->getOpcode() == ISD::SADDO;
8362 
8363   Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl,
8364                             LHS.getValueType(), LHS, RHS);
8365 
8366   EVT ResultType = Node->getValueType(1);
8367   EVT OType = getSetCCResultType(
8368       DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0));
8369 
8370   // If SADDSAT/SSUBSAT is legal, compare results to detect overflow.
8371   unsigned OpcSat = IsAdd ? ISD::SADDSAT : ISD::SSUBSAT;
8372   if (isOperationLegalOrCustom(OpcSat, LHS.getValueType())) {
8373     SDValue Sat = DAG.getNode(OpcSat, dl, LHS.getValueType(), LHS, RHS);
8374     SDValue SetCC = DAG.getSetCC(dl, OType, Result, Sat, ISD::SETNE);
8375     Overflow = DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType);
8376     return;
8377   }
8378 
8379   SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
8380 
8381   // For an addition, the result should be less than one of the operands (LHS)
8382   // if and only if the other operand (RHS) is negative, otherwise there will
8383   // be overflow.
8384   // For a subtraction, the result should be less than one of the operands
8385   // (LHS) if and only if the other operand (RHS) is (non-zero) positive,
8386   // otherwise there will be overflow.
8387   SDValue ResultLowerThanLHS = DAG.getSetCC(dl, OType, Result, LHS, ISD::SETLT);
8388   SDValue ConditionRHS =
8389       DAG.getSetCC(dl, OType, RHS, Zero, IsAdd ? ISD::SETLT : ISD::SETGT);
8390 
8391   Overflow = DAG.getBoolExtOrTrunc(
8392       DAG.getNode(ISD::XOR, dl, OType, ConditionRHS, ResultLowerThanLHS), dl,
8393       ResultType, ResultType);
8394 }
8395 
8396 bool TargetLowering::expandMULO(SDNode *Node, SDValue &Result,
8397                                 SDValue &Overflow, SelectionDAG &DAG) const {
8398   SDLoc dl(Node);
8399   EVT VT = Node->getValueType(0);
8400   EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
8401   SDValue LHS = Node->getOperand(0);
8402   SDValue RHS = Node->getOperand(1);
8403   bool isSigned = Node->getOpcode() == ISD::SMULO;
8404 
8405   // For power-of-two multiplications we can use a simpler shift expansion.
8406   if (ConstantSDNode *RHSC = isConstOrConstSplat(RHS)) {
8407     const APInt &C = RHSC->getAPIntValue();
8408     // mulo(X, 1 << S) -> { X << S, (X << S) >> S != X }
8409     if (C.isPowerOf2()) {
8410       // smulo(x, signed_min) is same as umulo(x, signed_min).
8411       bool UseArithShift = isSigned && !C.isMinSignedValue();
8412       EVT ShiftAmtTy = getShiftAmountTy(VT, DAG.getDataLayout());
8413       SDValue ShiftAmt = DAG.getConstant(C.logBase2(), dl, ShiftAmtTy);
8414       Result = DAG.getNode(ISD::SHL, dl, VT, LHS, ShiftAmt);
8415       Overflow = DAG.getSetCC(dl, SetCCVT,
8416           DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL,
8417                       dl, VT, Result, ShiftAmt),
8418           LHS, ISD::SETNE);
8419       return true;
8420     }
8421   }
8422 
8423   EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2);
8424   if (VT.isVector())
8425     WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT,
8426                               VT.getVectorNumElements());
8427 
8428   SDValue BottomHalf;
8429   SDValue TopHalf;
8430   static const unsigned Ops[2][3] =
8431       { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
8432         { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
8433   if (isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
8434     BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
8435     TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
8436   } else if (isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
8437     BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
8438                              RHS);
8439     TopHalf = BottomHalf.getValue(1);
8440   } else if (isTypeLegal(WideVT)) {
8441     LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
8442     RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
8443     SDValue Mul = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
8444     BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Mul);
8445     SDValue ShiftAmt = DAG.getConstant(VT.getScalarSizeInBits(), dl,
8446         getShiftAmountTy(WideVT, DAG.getDataLayout()));
8447     TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT,
8448                           DAG.getNode(ISD::SRL, dl, WideVT, Mul, ShiftAmt));
8449   } else {
8450     if (VT.isVector())
8451       return false;
8452 
8453     // We can fall back to a libcall with an illegal type for the MUL if we
8454     // have a libcall big enough.
8455     // Also, we can fall back to a division in some cases, but that's a big
8456     // performance hit in the general case.
8457     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
8458     if (WideVT == MVT::i16)
8459       LC = RTLIB::MUL_I16;
8460     else if (WideVT == MVT::i32)
8461       LC = RTLIB::MUL_I32;
8462     else if (WideVT == MVT::i64)
8463       LC = RTLIB::MUL_I64;
8464     else if (WideVT == MVT::i128)
8465       LC = RTLIB::MUL_I128;
8466     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
8467 
8468     SDValue HiLHS;
8469     SDValue HiRHS;
8470     if (isSigned) {
8471       // The high part is obtained by SRA'ing all but one of the bits of low
8472       // part.
8473       unsigned LoSize = VT.getFixedSizeInBits();
8474       HiLHS =
8475           DAG.getNode(ISD::SRA, dl, VT, LHS,
8476                       DAG.getConstant(LoSize - 1, dl,
8477                                       getPointerTy(DAG.getDataLayout())));
8478       HiRHS =
8479           DAG.getNode(ISD::SRA, dl, VT, RHS,
8480                       DAG.getConstant(LoSize - 1, dl,
8481                                       getPointerTy(DAG.getDataLayout())));
8482     } else {
8483         HiLHS = DAG.getConstant(0, dl, VT);
8484         HiRHS = DAG.getConstant(0, dl, VT);
8485     }
8486 
8487     // Here we're passing the 2 arguments explicitly as 4 arguments that are
8488     // pre-lowered to the correct types. This all depends upon WideVT not
8489     // being a legal type for the architecture and thus has to be split to
8490     // two arguments.
8491     SDValue Ret;
8492     TargetLowering::MakeLibCallOptions CallOptions;
8493     CallOptions.setSExt(isSigned);
8494     CallOptions.setIsPostTypeLegalization(true);
8495     if (shouldSplitFunctionArgumentsAsLittleEndian(DAG.getDataLayout())) {
8496       // Halves of WideVT are packed into registers in different order
8497       // depending on platform endianness. This is usually handled by
8498       // the C calling convention, but we can't defer to it in
8499       // the legalizer.
8500       SDValue Args[] = { LHS, HiLHS, RHS, HiRHS };
8501       Ret = makeLibCall(DAG, LC, WideVT, Args, CallOptions, dl).first;
8502     } else {
8503       SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
8504       Ret = makeLibCall(DAG, LC, WideVT, Args, CallOptions, dl).first;
8505     }
8506     assert(Ret.getOpcode() == ISD::MERGE_VALUES &&
8507            "Ret value is a collection of constituent nodes holding result.");
8508     if (DAG.getDataLayout().isLittleEndian()) {
8509       // Same as above.
8510       BottomHalf = Ret.getOperand(0);
8511       TopHalf = Ret.getOperand(1);
8512     } else {
8513       BottomHalf = Ret.getOperand(1);
8514       TopHalf = Ret.getOperand(0);
8515     }
8516   }
8517 
8518   Result = BottomHalf;
8519   if (isSigned) {
8520     SDValue ShiftAmt = DAG.getConstant(
8521         VT.getScalarSizeInBits() - 1, dl,
8522         getShiftAmountTy(BottomHalf.getValueType(), DAG.getDataLayout()));
8523     SDValue Sign = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
8524     Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf, Sign, ISD::SETNE);
8525   } else {
8526     Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf,
8527                             DAG.getConstant(0, dl, VT), ISD::SETNE);
8528   }
8529 
8530   // Truncate the result if SetCC returns a larger type than needed.
8531   EVT RType = Node->getValueType(1);
8532   if (RType.bitsLT(Overflow.getValueType()))
8533     Overflow = DAG.getNode(ISD::TRUNCATE, dl, RType, Overflow);
8534 
8535   assert(RType.getSizeInBits() == Overflow.getValueSizeInBits() &&
8536          "Unexpected result type for S/UMULO legalization");
8537   return true;
8538 }
8539 
8540 SDValue TargetLowering::expandVecReduce(SDNode *Node, SelectionDAG &DAG) const {
8541   SDLoc dl(Node);
8542   unsigned BaseOpcode = ISD::getVecReduceBaseOpcode(Node->getOpcode());
8543   SDValue Op = Node->getOperand(0);
8544   EVT VT = Op.getValueType();
8545 
8546   if (VT.isScalableVector())
8547     report_fatal_error(
8548         "Expanding reductions for scalable vectors is undefined.");
8549 
8550   // Try to use a shuffle reduction for power of two vectors.
8551   if (VT.isPow2VectorType()) {
8552     while (VT.getVectorNumElements() > 1) {
8553       EVT HalfVT = VT.getHalfNumVectorElementsVT(*DAG.getContext());
8554       if (!isOperationLegalOrCustom(BaseOpcode, HalfVT))
8555         break;
8556 
8557       SDValue Lo, Hi;
8558       std::tie(Lo, Hi) = DAG.SplitVector(Op, dl);
8559       Op = DAG.getNode(BaseOpcode, dl, HalfVT, Lo, Hi);
8560       VT = HalfVT;
8561     }
8562   }
8563 
8564   EVT EltVT = VT.getVectorElementType();
8565   unsigned NumElts = VT.getVectorNumElements();
8566 
8567   SmallVector<SDValue, 8> Ops;
8568   DAG.ExtractVectorElements(Op, Ops, 0, NumElts);
8569 
8570   SDValue Res = Ops[0];
8571   for (unsigned i = 1; i < NumElts; i++)
8572     Res = DAG.getNode(BaseOpcode, dl, EltVT, Res, Ops[i], Node->getFlags());
8573 
8574   // Result type may be wider than element type.
8575   if (EltVT != Node->getValueType(0))
8576     Res = DAG.getNode(ISD::ANY_EXTEND, dl, Node->getValueType(0), Res);
8577   return Res;
8578 }
8579 
8580 SDValue TargetLowering::expandVecReduceSeq(SDNode *Node, SelectionDAG &DAG) const {
8581   SDLoc dl(Node);
8582   SDValue AccOp = Node->getOperand(0);
8583   SDValue VecOp = Node->getOperand(1);
8584   SDNodeFlags Flags = Node->getFlags();
8585 
8586   EVT VT = VecOp.getValueType();
8587   EVT EltVT = VT.getVectorElementType();
8588 
8589   if (VT.isScalableVector())
8590     report_fatal_error(
8591         "Expanding reductions for scalable vectors is undefined.");
8592 
8593   unsigned NumElts = VT.getVectorNumElements();
8594 
8595   SmallVector<SDValue, 8> Ops;
8596   DAG.ExtractVectorElements(VecOp, Ops, 0, NumElts);
8597 
8598   unsigned BaseOpcode = ISD::getVecReduceBaseOpcode(Node->getOpcode());
8599 
8600   SDValue Res = AccOp;
8601   for (unsigned i = 0; i < NumElts; i++)
8602     Res = DAG.getNode(BaseOpcode, dl, EltVT, Res, Ops[i], Flags);
8603 
8604   return Res;
8605 }
8606 
8607 bool TargetLowering::expandREM(SDNode *Node, SDValue &Result,
8608                                SelectionDAG &DAG) const {
8609   EVT VT = Node->getValueType(0);
8610   SDLoc dl(Node);
8611   bool isSigned = Node->getOpcode() == ISD::SREM;
8612   unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
8613   unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
8614   SDValue Dividend = Node->getOperand(0);
8615   SDValue Divisor = Node->getOperand(1);
8616   if (isOperationLegalOrCustom(DivRemOpc, VT)) {
8617     SDVTList VTs = DAG.getVTList(VT, VT);
8618     Result = DAG.getNode(DivRemOpc, dl, VTs, Dividend, Divisor).getValue(1);
8619     return true;
8620   }
8621   if (isOperationLegalOrCustom(DivOpc, VT)) {
8622     // X % Y -> X-X/Y*Y
8623     SDValue Divide = DAG.getNode(DivOpc, dl, VT, Dividend, Divisor);
8624     SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Divide, Divisor);
8625     Result = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul);
8626     return true;
8627   }
8628   return false;
8629 }
8630 
8631 SDValue TargetLowering::expandFP_TO_INT_SAT(SDNode *Node,
8632                                             SelectionDAG &DAG) const {
8633   bool IsSigned = Node->getOpcode() == ISD::FP_TO_SINT_SAT;
8634   SDLoc dl(SDValue(Node, 0));
8635   SDValue Src = Node->getOperand(0);
8636 
8637   // DstVT is the result type, while SatVT is the size to which we saturate
8638   EVT SrcVT = Src.getValueType();
8639   EVT DstVT = Node->getValueType(0);
8640 
8641   EVT SatVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
8642   unsigned SatWidth = SatVT.getScalarSizeInBits();
8643   unsigned DstWidth = DstVT.getScalarSizeInBits();
8644   assert(SatWidth <= DstWidth &&
8645          "Expected saturation width smaller than result width");
8646 
8647   // Determine minimum and maximum integer values and their corresponding
8648   // floating-point values.
8649   APInt MinInt, MaxInt;
8650   if (IsSigned) {
8651     MinInt = APInt::getSignedMinValue(SatWidth).sextOrSelf(DstWidth);
8652     MaxInt = APInt::getSignedMaxValue(SatWidth).sextOrSelf(DstWidth);
8653   } else {
8654     MinInt = APInt::getMinValue(SatWidth).zextOrSelf(DstWidth);
8655     MaxInt = APInt::getMaxValue(SatWidth).zextOrSelf(DstWidth);
8656   }
8657 
8658   // We cannot risk emitting FP_TO_XINT nodes with a source VT of f16, as
8659   // libcall emission cannot handle this. Large result types will fail.
8660   if (SrcVT == MVT::f16) {
8661     Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, Src);
8662     SrcVT = Src.getValueType();
8663   }
8664 
8665   APFloat MinFloat(DAG.EVTToAPFloatSemantics(SrcVT));
8666   APFloat MaxFloat(DAG.EVTToAPFloatSemantics(SrcVT));
8667 
8668   APFloat::opStatus MinStatus =
8669       MinFloat.convertFromAPInt(MinInt, IsSigned, APFloat::rmTowardZero);
8670   APFloat::opStatus MaxStatus =
8671       MaxFloat.convertFromAPInt(MaxInt, IsSigned, APFloat::rmTowardZero);
8672   bool AreExactFloatBounds = !(MinStatus & APFloat::opStatus::opInexact) &&
8673                              !(MaxStatus & APFloat::opStatus::opInexact);
8674 
8675   SDValue MinFloatNode = DAG.getConstantFP(MinFloat, dl, SrcVT);
8676   SDValue MaxFloatNode = DAG.getConstantFP(MaxFloat, dl, SrcVT);
8677 
8678   // If the integer bounds are exactly representable as floats and min/max are
8679   // legal, emit a min+max+fptoi sequence. Otherwise we have to use a sequence
8680   // of comparisons and selects.
8681   bool MinMaxLegal = isOperationLegal(ISD::FMINNUM, SrcVT) &&
8682                      isOperationLegal(ISD::FMAXNUM, SrcVT);
8683   if (AreExactFloatBounds && MinMaxLegal) {
8684     SDValue Clamped = Src;
8685 
8686     // Clamp Src by MinFloat from below. If Src is NaN the result is MinFloat.
8687     Clamped = DAG.getNode(ISD::FMAXNUM, dl, SrcVT, Clamped, MinFloatNode);
8688     // Clamp by MaxFloat from above. NaN cannot occur.
8689     Clamped = DAG.getNode(ISD::FMINNUM, dl, SrcVT, Clamped, MaxFloatNode);
8690     // Convert clamped value to integer.
8691     SDValue FpToInt = DAG.getNode(IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT,
8692                                   dl, DstVT, Clamped);
8693 
8694     // In the unsigned case we're done, because we mapped NaN to MinFloat,
8695     // which will cast to zero.
8696     if (!IsSigned)
8697       return FpToInt;
8698 
8699     // Otherwise, select 0 if Src is NaN.
8700     SDValue ZeroInt = DAG.getConstant(0, dl, DstVT);
8701     return DAG.getSelectCC(dl, Src, Src, ZeroInt, FpToInt,
8702                            ISD::CondCode::SETUO);
8703   }
8704 
8705   SDValue MinIntNode = DAG.getConstant(MinInt, dl, DstVT);
8706   SDValue MaxIntNode = DAG.getConstant(MaxInt, dl, DstVT);
8707 
8708   // Result of direct conversion. The assumption here is that the operation is
8709   // non-trapping and it's fine to apply it to an out-of-range value if we
8710   // select it away later.
8711   SDValue FpToInt =
8712       DAG.getNode(IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, dl, DstVT, Src);
8713 
8714   SDValue Select = FpToInt;
8715 
8716   // If Src ULT MinFloat, select MinInt. In particular, this also selects
8717   // MinInt if Src is NaN.
8718   Select = DAG.getSelectCC(dl, Src, MinFloatNode, MinIntNode, Select,
8719                            ISD::CondCode::SETULT);
8720   // If Src OGT MaxFloat, select MaxInt.
8721   Select = DAG.getSelectCC(dl, Src, MaxFloatNode, MaxIntNode, Select,
8722                            ISD::CondCode::SETOGT);
8723 
8724   // In the unsigned case we are done, because we mapped NaN to MinInt, which
8725   // is already zero.
8726   if (!IsSigned)
8727     return Select;
8728 
8729   // Otherwise, select 0 if Src is NaN.
8730   SDValue ZeroInt = DAG.getConstant(0, dl, DstVT);
8731   return DAG.getSelectCC(dl, Src, Src, ZeroInt, Select, ISD::CondCode::SETUO);
8732 }
8733 
8734 SDValue TargetLowering::expandVectorSplice(SDNode *Node,
8735                                            SelectionDAG &DAG) const {
8736   assert(Node->getOpcode() == ISD::VECTOR_SPLICE && "Unexpected opcode!");
8737   assert(Node->getValueType(0).isScalableVector() &&
8738          "Fixed length vector types expected to use SHUFFLE_VECTOR!");
8739 
8740   EVT VT = Node->getValueType(0);
8741   SDValue V1 = Node->getOperand(0);
8742   SDValue V2 = Node->getOperand(1);
8743   int64_t Imm = cast<ConstantSDNode>(Node->getOperand(2))->getSExtValue();
8744   SDLoc DL(Node);
8745 
8746   // Expand through memory thusly:
8747   //  Alloca CONCAT_VECTORS_TYPES(V1, V2) Ptr
8748   //  Store V1, Ptr
8749   //  Store V2, Ptr + sizeof(V1)
8750   //  If (Imm < 0)
8751   //    TrailingElts = -Imm
8752   //    Ptr = Ptr + sizeof(V1) - (TrailingElts * sizeof(VT.Elt))
8753   //  else
8754   //    Ptr = Ptr + (Imm * sizeof(VT.Elt))
8755   //  Res = Load Ptr
8756 
8757   Align Alignment = DAG.getReducedAlign(VT, /*UseABI=*/false);
8758 
8759   EVT MemVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8760                                VT.getVectorElementCount() * 2);
8761   SDValue StackPtr = DAG.CreateStackTemporary(MemVT.getStoreSize(), Alignment);
8762   EVT PtrVT = StackPtr.getValueType();
8763   auto &MF = DAG.getMachineFunction();
8764   auto FrameIndex = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
8765   auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex);
8766 
8767   // Store the lo part of CONCAT_VECTORS(V1, V2)
8768   SDValue StoreV1 = DAG.getStore(DAG.getEntryNode(), DL, V1, StackPtr, PtrInfo);
8769   // Store the hi part of CONCAT_VECTORS(V1, V2)
8770   SDValue OffsetToV2 = DAG.getVScale(
8771       DL, PtrVT,
8772       APInt(PtrVT.getFixedSizeInBits(), VT.getStoreSize().getKnownMinSize()));
8773   SDValue StackPtr2 = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, OffsetToV2);
8774   SDValue StoreV2 = DAG.getStore(StoreV1, DL, V2, StackPtr2, PtrInfo);
8775 
8776   if (Imm >= 0) {
8777     // Load back the required element. getVectorElementPointer takes care of
8778     // clamping the index if it's out-of-bounds.
8779     StackPtr = getVectorElementPointer(DAG, StackPtr, VT, Node->getOperand(2));
8780     // Load the spliced result
8781     return DAG.getLoad(VT, DL, StoreV2, StackPtr,
8782                        MachinePointerInfo::getUnknownStack(MF));
8783   }
8784 
8785   uint64_t TrailingElts = -Imm;
8786 
8787   // NOTE: TrailingElts must be clamped so as not to read outside of V1:V2.
8788   TypeSize EltByteSize = VT.getVectorElementType().getStoreSize();
8789   SDValue TrailingBytes =
8790       DAG.getConstant(TrailingElts * EltByteSize, DL, PtrVT);
8791 
8792   if (TrailingElts > VT.getVectorMinNumElements()) {
8793     SDValue VLBytes = DAG.getVScale(
8794         DL, PtrVT,
8795         APInt(PtrVT.getFixedSizeInBits(), VT.getStoreSize().getKnownMinSize()));
8796     TrailingBytes = DAG.getNode(ISD::UMIN, DL, PtrVT, TrailingBytes, VLBytes);
8797   }
8798 
8799   // Calculate the start address of the spliced result.
8800   StackPtr2 = DAG.getNode(ISD::SUB, DL, PtrVT, StackPtr2, TrailingBytes);
8801 
8802   // Load the spliced result
8803   return DAG.getLoad(VT, DL, StoreV2, StackPtr2,
8804                      MachinePointerInfo::getUnknownStack(MF));
8805 }
8806 
8807 bool TargetLowering::LegalizeSetCCCondCode(SelectionDAG &DAG, EVT VT,
8808                                            SDValue &LHS, SDValue &RHS,
8809                                            SDValue &CC, bool &NeedInvert,
8810                                            const SDLoc &dl, SDValue &Chain,
8811                                            bool IsSignaling) const {
8812   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8813   MVT OpVT = LHS.getSimpleValueType();
8814   ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
8815   NeedInvert = false;
8816   switch (TLI.getCondCodeAction(CCCode, OpVT)) {
8817   default:
8818     llvm_unreachable("Unknown condition code action!");
8819   case TargetLowering::Legal:
8820     // Nothing to do.
8821     break;
8822   case TargetLowering::Expand: {
8823     ISD::CondCode InvCC = ISD::getSetCCSwappedOperands(CCCode);
8824     if (TLI.isCondCodeLegalOrCustom(InvCC, OpVT)) {
8825       std::swap(LHS, RHS);
8826       CC = DAG.getCondCode(InvCC);
8827       return true;
8828     }
8829     // Swapping operands didn't work. Try inverting the condition.
8830     bool NeedSwap = false;
8831     InvCC = getSetCCInverse(CCCode, OpVT);
8832     if (!TLI.isCondCodeLegalOrCustom(InvCC, OpVT)) {
8833       // If inverting the condition is not enough, try swapping operands
8834       // on top of it.
8835       InvCC = ISD::getSetCCSwappedOperands(InvCC);
8836       NeedSwap = true;
8837     }
8838     if (TLI.isCondCodeLegalOrCustom(InvCC, OpVT)) {
8839       CC = DAG.getCondCode(InvCC);
8840       NeedInvert = true;
8841       if (NeedSwap)
8842         std::swap(LHS, RHS);
8843       return true;
8844     }
8845 
8846     ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
8847     unsigned Opc = 0;
8848     switch (CCCode) {
8849     default:
8850       llvm_unreachable("Don't know how to expand this condition!");
8851     case ISD::SETUO:
8852       if (TLI.isCondCodeLegal(ISD::SETUNE, OpVT)) {
8853         CC1 = ISD::SETUNE;
8854         CC2 = ISD::SETUNE;
8855         Opc = ISD::OR;
8856         break;
8857       }
8858       assert(TLI.isCondCodeLegal(ISD::SETOEQ, OpVT) &&
8859              "If SETUE is expanded, SETOEQ or SETUNE must be legal!");
8860       NeedInvert = true;
8861       LLVM_FALLTHROUGH;
8862     case ISD::SETO:
8863       assert(TLI.isCondCodeLegal(ISD::SETOEQ, OpVT) &&
8864              "If SETO is expanded, SETOEQ must be legal!");
8865       CC1 = ISD::SETOEQ;
8866       CC2 = ISD::SETOEQ;
8867       Opc = ISD::AND;
8868       break;
8869     case ISD::SETONE:
8870     case ISD::SETUEQ:
8871       // If the SETUO or SETO CC isn't legal, we might be able to use
8872       // SETOGT || SETOLT, inverting the result for SETUEQ. We only need one
8873       // of SETOGT/SETOLT to be legal, the other can be emulated by swapping
8874       // the operands.
8875       CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
8876       if (!TLI.isCondCodeLegal(CC2, OpVT) &&
8877           (TLI.isCondCodeLegal(ISD::SETOGT, OpVT) ||
8878            TLI.isCondCodeLegal(ISD::SETOLT, OpVT))) {
8879         CC1 = ISD::SETOGT;
8880         CC2 = ISD::SETOLT;
8881         Opc = ISD::OR;
8882         NeedInvert = ((unsigned)CCCode & 0x8U);
8883         break;
8884       }
8885       LLVM_FALLTHROUGH;
8886     case ISD::SETOEQ:
8887     case ISD::SETOGT:
8888     case ISD::SETOGE:
8889     case ISD::SETOLT:
8890     case ISD::SETOLE:
8891     case ISD::SETUNE:
8892     case ISD::SETUGT:
8893     case ISD::SETUGE:
8894     case ISD::SETULT:
8895     case ISD::SETULE:
8896       // If we are floating point, assign and break, otherwise fall through.
8897       if (!OpVT.isInteger()) {
8898         // We can use the 4th bit to tell if we are the unordered
8899         // or ordered version of the opcode.
8900         CC2 = ((unsigned)CCCode & 0x8U) ? ISD::SETUO : ISD::SETO;
8901         Opc = ((unsigned)CCCode & 0x8U) ? ISD::OR : ISD::AND;
8902         CC1 = (ISD::CondCode)(((int)CCCode & 0x7) | 0x10);
8903         break;
8904       }
8905       // Fallthrough if we are unsigned integer.
8906       LLVM_FALLTHROUGH;
8907     case ISD::SETLE:
8908     case ISD::SETGT:
8909     case ISD::SETGE:
8910     case ISD::SETLT:
8911     case ISD::SETNE:
8912     case ISD::SETEQ:
8913       // If all combinations of inverting the condition and swapping operands
8914       // didn't work then we have no means to expand the condition.
8915       llvm_unreachable("Don't know how to expand this condition!");
8916     }
8917 
8918     SDValue SetCC1, SetCC2;
8919     if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
8920       // If we aren't the ordered or unorder operation,
8921       // then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
8922       SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1, Chain, IsSignaling);
8923       SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2, Chain, IsSignaling);
8924     } else {
8925       // Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
8926       SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1, Chain, IsSignaling);
8927       SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2, Chain, IsSignaling);
8928     }
8929     if (Chain)
8930       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, SetCC1.getValue(1),
8931                           SetCC2.getValue(1));
8932     LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
8933     RHS = SDValue();
8934     CC = SDValue();
8935     return true;
8936   }
8937   }
8938   return false;
8939 }
8940